Debian Codegraph @

unixos.c

raw symbols ◀ ▶ SCIP
3 Debian patches touch this file
1 /* (C) Copyright 1993,1994 by Carnegie Mellon University
2 * All Rights Reserved.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software
5 * and its documentation for any purpose is hereby granted without
6 * fee, provided that the above copyright notice appear in all copies
7 * and that both that copyright notice and this permission notice
8 * appear in supporting documentation, and that the name of Carnegie
9 * Mellon University not be used in advertising or publicity
10 * pertaining to distribution of the software without specific,
11 * written prior permission. Carnegie Mellon University makes no
12 * representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied
14 * warranty.
15 *
16 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
17 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
19 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
21 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
22 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23 * SOFTWARE.
24 */
25 #include <stdio.h>
•26 #include <stdlib.h>
27 #include <ctype.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <sys/types.h>
•31 #include <sys/stat.h>
32 #include <sys/param.h>
•33 #include <time.h>
34 #include <netdb.h>
35 #include <fcntl.h>
•36 #include <unistd.h>
37 #include "xmalloc.h"
38 #include "common.h"
39 #include "part.h"
40
•41 extern void warn(char *s);
42
43 #ifndef MAXHOSTNAMELEN
44 #define MAXHOSTNAMELEN 64
45 #endif
46
•47 #ifndef errno
48 extern int errno;
•49 #endif
50
51 int overwrite_files = 0;
52 int didchat;
•53 char replacement_char = 'X';
54
55 /* The name of the file we're writing */
56 static char *output_fname = 0;
57
58 /* Characters that shouldn't be in filenames */
59 #define BADCHARS "!$&*()|\'\";<>[]{}?/`\\ \t"
60
61 /* Generate a message-id */
62 char *os_genid(void)
63 {
64 static int pid = 0;
65 static time_t curtime;
66 static char hostname[MAXHOSTNAMELEN+1];
67 char *result;
68 struct hostent *hp;
69
70
71 if (pid == 0) {
72 pid = getpid();
73 time(&curtime);
74 gethostname(hostname, sizeof(hostname));
75
76 /* If we don't have a FQDN, try canonicalizing with gethostbyname */
77 if (!strchr(hostname, '.')) {
78 hp = gethostbyname(hostname);
79 if (hp) {
80 strcpy(hostname, hp->h_name);
81 }
82 }
83 }
84
85 result = malloc(25+strlen(hostname));
•86 sprintf(result, "%d.%lu@%s", pid, (unsigned long) curtime++, hostname);
87 return result;
88 }
89
90 /* Create and return directory for a message-id */
91 char *os_idtodir(char *id)
92 {
93 static char buf[4096];
94 char *p;
95
96 if (getenv("TMPDIR")) {
97 strcpy(buf, getenv("TMPDIR"));
98 }
99 else {
•100 strcpy(buf, "/tmp");
101 }
102 strcat(buf, "/m-prts-");
103 p = getenv("USER");
104 if (!p) p = getenv("LOGNAME");
105 if (!p) p = "x";
106 strcat(buf, p);
107
108 (void)mkdir(buf, 0700);
109
110 p = buf + strlen(buf);
111 *p++ = '/';
112 while (*id && p < buf+sizeof(buf)-10 ) {
113 if (isprint(*id) && !strchr(BADCHARS, *id)) *p++ = *id;
114 id++;
115 }
116 *p = '\0';
117 if (mkdir(buf, 0700) == -1 && errno != EEXIST) {
118 perror(buf);
119 return 0;
120 }
121 *p++ = '/';
122 *p = '\0';
123 return buf;
124 }
125
126 /*
127 * We are done with the directory returned by os_idtodir()
128 * Remove it
129 */
130 void os_donewithdir(char *dir)
131 {
132 char *p;
133
134 /* Remove trailing slash */
135 p = dir + strlen(dir) - 1;
136 *p = '\0';
137
138 rmdir(dir);
139 }
140
141 FILE *os_createnewfile(char *fname)
142 {
143 int fd;
144 FILE *ret;
145
146 #ifdef O_EXCL
•147 struct stat statbuf;
148
149 if ((stat(fname, &statbuf) == 0) && (S_ISCHR(statbuf.st_mode))) {
150 fd=open(fname, O_RDWR);
151 }
152 else {
•153 fd=open(fname, O_RDWR|O_CREAT|O_EXCL, 0600);
154 }
155 #else
•156 fd=open(fname, O_RDWR|O_CREAT|O_TRUNC, 0600);
157 #endif
158
159 if (fd == -1)
160 return NULL;
161
162 ret=fdopen(fd, "w");
163 return ret;
164 }
165
166
167 /*
168 * Create a new file, with suggested filename "fname".
169 * "fname" may have come from an insecure source, so clean it up first.
170 * It may also be null.
171 * "contentType" is passed in for use by systems that have typed filesystems.
172 * "flags" contains a bit pattern describing attributes of the new file.
173 */
174 FILE *os_newtypedfile(char *fname, char *contentType, int flags, params contentParams)
175 {
176 char *p;
177 static int filesuffix=0;
178 char buf[128], *descfname=0;
179 FILE *outfile = 0;
180
181 if (!fname) fname = "";
182
183 /* If absolute path name, chop to tail */
184 if (*fname == '/') {
185 p = strrchr(fname, '/');
186 fname = p+1;
187 }
188
189 /* Get rid of leading ~ or ~/ */
190 while (*fname == '~' || *fname == '/') fname++;
191
192 while (!strncmp(fname, "../", 3)) fname += 3;
193
194 /* Clean out bad characters, create directories along path */
195 for (p=fname; *p; p++) {
196 if (*p == '/') {
197 if (!strncmp(p, "/../", 4)) {
•198 p[1] = p[2] = replacement_char;
199 }
200 *p = '\0';
201 (void) mkdir(fname, 0777);
202 *p = '/';
203 }
•204 else if (!isprint(*p) || strchr(BADCHARS, *p)) *p = replacement_char;
205 }
206
207 if (!fname[0]) {
208 do {
209 if (outfile) fclose(outfile);
210 sprintf(buf, "part%d", ++filesuffix);
•211 } while ((outfile = fopen(buf, "r")));
212 fname = buf;
213 }
214 else if (!overwrite_files && (outfile = fopen(fname, "r"))) {
215 do {
216 fclose(outfile);
217 sprintf(buf, "%s.%d", fname, ++filesuffix);
218
•219 } while ((outfile = fopen(buf, "r")));
220 fname = buf;
221 }
222
223 if (overwrite_files)
224 outfile = fopen(fname, "w");
225 else
226 outfile = os_createnewfile(fname);
227
228 if (!outfile) {
229 perror(fname);
230 }
231
232 if (output_fname) free(output_fname);
233 output_fname = strsave(fname);
234
235 if (strlen(fname) > sizeof(buf)-6) {
236 descfname = xmalloc(strlen(fname)+6);
237 }
238 else {
239 descfname = buf;
240 }
241 strcpy(descfname, fname);
242
243 p = strchr(descfname, '/');
244 if (!p) p = descfname;
•245 if ((p = strrchr(p, '.'))) *p = '\0';
246
247 strcat(descfname, ".desc");
248 (void) rename(TEMPFILENAME, descfname);
249 if (descfname != buf) free(descfname);
250
251 fprintf(stdout, "%s (%s)\n", output_fname, contentType);
252 didchat = 1;
253
254 return outfile;
255 }
256
257 /*
258 * Close a file opened by os_newTypedFile()
259 */
260 void os_closetypedfile(FILE *outfile)
261 {
262 fclose(outfile);
263 }
264
265 /*
266 * (Don't) Handle a BinHex'ed file
267 */
268 int
269 os_binhex(struct part *inpart, int part, int nparts)
270 {
271 return 1;
272 }
273
274 /*
275 * Warn user that the MD5 digest of the last file created by os_newtypedfile()
276 * did not match that supplied in the Content-MD5: header.
277 */
278 void os_warnMD5mismatch(void)
279 {
280 char *warning;
281
282 warning = xmalloc(strlen(output_fname) + 100);
283 sprintf(warning, "%s was corrupted in transit",
284 output_fname);
285 warn(warning);
286 free(warning);
287 }
288
289 /*
290 * Report an error (in errno) concerning a filename
291 */
292 void os_perror(char *file)
293 {
294 perror(file);
295 }