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 <ctype.h> |
27 |
#include <string.h> |
28 |
#include <errno.h> |
29 |
#include <sys/types.h> |
30 |
#include <sys/param.h> |
31 |
#include <netdb.h> |
32 |
#include <fcntl.h> |
33 |
#include "xmalloc.h" |
34 |
#include "common.h" |
35 |
#include "part.h" |
36 |
|
37 |
#ifndef MAXHOSTNAMELEN |
38 |
#define MAXHOSTNAMELEN 64 |
39 |
#endif |
40 |
|
41 |
extern int errno; |
42 |
/* extern char *malloc(); */ |
43 |
extern char *getenv(); |
44 |
|
45 |
int overwrite_files = 0; |
46 |
int didchat; |
47 |
|
48 |
/* The name of the file we're writing */ |
49 |
static char *output_fname = 0; |
50 |
|
51 |
/* Characters that shouldn't be in filenames */ |
52 |
#define BADCHARS "!$&*()|\'\";<>[]{}?/`\\ \t" |
53 |
|
54 |
/* Generate a message-id */ |
55 |
char *os_genid(void) |
56 |
{ |
57 |
static int pid = 0; |
58 |
static time_t curtime; |
59 |
static char hostname[MAXHOSTNAMELEN+1]; |
60 |
char *result; |
61 |
struct hostent *hp; |
62 |
|
63 |
|
64 |
if (pid == 0) { |
65 |
pid = getpid(); |
66 |
time(&curtime); |
67 |
gethostname(hostname, sizeof(hostname)); |
68 |
|
69 |
/* If we don't have a FQDN, try canonicalizing with gethostbyname */ |
70 |
if (!strchr(hostname, '.')) { |
71 |
hp = gethostbyname(hostname); |
72 |
if (hp) { |
73 |
strcpy(hostname, hp->h_name); |
74 |
} |
75 |
} |
76 |
} |
77 |
|
78 |
result = (char *)malloc(25+strlen(hostname)); |
79 |
sprintf(result, "%d.%d@%s", pid, curtime++, hostname); |
80 |
return result; |
81 |
} |
82 |
|
83 |
/* Create and return directory for a message-id */ |
84 |
char *os_idtodir(char *id) |
85 |
{ |
86 |
static char buf[4096]; |
87 |
char *p; |
88 |
|
89 |
if (getenv("TMPDIR")) { |
90 |
strcpy(buf, getenv("TMPDIR")); |
91 |
} |
92 |
else { |
93 |
strcpy(buf, "/usr/tmp"); |
94 |
} |
95 |
strcat(buf, "/m-prts-"); |
96 |
p = getenv("USER"); |
97 |
if (!p) p = getenv("LOGNAME"); |
98 |
if (!p) p = "x"; |
99 |
strcat(buf, p); |
100 |
|
101 |
(void)mkdir(buf, 0700); |
102 |
|
103 |
p = buf + strlen(buf); |
104 |
*p++ = '/'; |
105 |
while (*id && p < buf+sizeof(buf)-10 ) { |
106 |
if (isprint(*id) && !strchr(BADCHARS, *id)) *p++ = *id; |
107 |
id++; |
108 |
} |
109 |
*p = '\0'; |
110 |
if (mkdir(buf, 0700) == -1 && errno != EEXIST) { |
111 |
perror(buf); |
112 |
return 0; |
113 |
} |
114 |
*p++ = '/'; |
115 |
*p = '\0'; |
116 |
return buf; |
117 |
} |
118 |
|
119 |
/* |
120 |
* We are done with the directory returned by os_idtodir() |
121 |
* Remove it |
122 |
*/ |
123 |
void os_donewithdir(char *dir) |
124 |
{ |
125 |
char *p; |
126 |
|
127 |
/* Remove trailing slash */ |
128 |
p = dir + strlen(dir) - 1; |
129 |
*p = '\0'; |
130 |
|
131 |
rmdir(dir); |
132 |
} |
133 |
|
134 |
FILE *os_createnewfile(char *fname) |
135 |
{ |
136 |
int fd; |
137 |
FILE *ret; |
138 |
|
139 |
#ifdef O_EXCL |
140 |
fd=open(fname, O_RDWR|O_CREAT|O_EXCL, 0644); |
141 |
#else |
142 |
fd=open(fname, O_RDWR|O_CREAT|O_TRUNC, 0644); |
143 |
#endif |
144 |
|
145 |
if (fd == -1) |
146 |
return NULL; |
147 |
|
148 |
ret=fdopen(fd, "w"); |
149 |
return ret; |
150 |
} |
151 |
|
152 |
|
153 |
/* |
154 |
* Create a new file, with suggested filename "fname". |
155 |
* "fname" may have come from an insecure source, so clean it up first. |
156 |
* It may also be null. |
157 |
* "contentType" is passed in for use by systems that have typed filesystems. |
158 |
* "flags" contains a bit pattern describing attributes of the new file. |
159 |
*/ |
160 |
FILE *os_newtypedfile(char *fname, char *contentType, int flags, params contentParams) |
161 |
{ |
162 |
char *p; |
163 |
static int filesuffix=0; |
164 |
char buf[128], *descfname=0; |
165 |
FILE *outfile = 0; |
166 |
|
167 |
if (!fname) fname = ""; |
168 |
|
169 |
/* If absolute path name, chop to tail */ |
170 |
if (*fname == '/') { |
171 |
p = strrchr(fname, '/'); |
172 |
fname = p+1; |
173 |
} |
174 |
|
175 |
/* Get rid of leading ~ or ~/ */ |
176 |
while (*fname == '~' || *fname == '/') fname++; |
177 |
|
178 |
while (!strncmp(fname, "../", 3)) fname += 3; |
179 |
|
180 |
/* Clean out bad characters, create directories along path */ |
181 |
for (p=fname; *p; p++) { |
182 |
if (*p == '/') { |
183 |
if (!strncmp(p, "/../", 4)) { |
184 |
p[1] = p[2] = 'X'; |
185 |
} |
186 |
*p = '\0'; |
187 |
(void) mkdir(fname, 0777); |
188 |
*p = '/'; |
189 |
} |
190 |
else if (!isprint(*p) || strchr(BADCHARS, *p)) *p = 'X'; |
191 |
} |
192 |
|
193 |
if (!fname[0]) { |
194 |
do { |
195 |
if (outfile) fclose(outfile); |
196 |
sprintf(buf, "part%d", ++filesuffix); |
197 |
} while (outfile = fopen(buf, "r")); |
198 |
fname = buf; |
199 |
} |
200 |
else if (!overwrite_files && (outfile = fopen(fname, "r"))) { |
201 |
do { |
202 |
fclose(outfile); |
203 |
sprintf(buf, "%s.%d", fname, ++filesuffix); |
204 |
|
205 |
} while (outfile = fopen(buf, "r")); |
206 |
fname = buf; |
207 |
} |
208 |
|
209 |
if (overwrite_files) |
210 |
outfile = fopen(fname, "w"); |
211 |
else |
212 |
outfile = os_createnewfile(fname); |
213 |
|
214 |
if (!outfile) { |
215 |
perror(fname); |
216 |
} |
217 |
|
218 |
if (output_fname) free(output_fname); |
219 |
output_fname = strsave(fname); |
220 |
|
221 |
if (strlen(fname) > sizeof(buf)-6) { |
222 |
descfname = xmalloc(strlen(fname)+6); |
223 |
} |
224 |
else { |
225 |
descfname = buf; |
226 |
} |
227 |
strcpy(descfname, fname); |
228 |
|
229 |
p = strchr(descfname, '/'); |
230 |
if (!p) p = descfname; |
231 |
if (p = strrchr(p, '.')) *p = '\0'; |
232 |
|
233 |
strcat(descfname, ".desc"); |
234 |
(void) rename(TEMPFILENAME, descfname); |
235 |
if (descfname != buf) free(descfname); |
236 |
|
237 |
fprintf(stdout, "%s (%s)\n", output_fname, contentType); |
238 |
didchat = 1; |
239 |
|
240 |
return outfile; |
241 |
} |
242 |
|
243 |
/* |
244 |
* Close a file opened by os_newTypedFile() |
245 |
*/ |
246 |
void os_closetypedfile(FILE *outfile) |
247 |
{ |
248 |
fclose(outfile); |
249 |
} |
250 |
|
251 |
/* |
252 |
* (Don't) Handle a BinHex'ed file |
253 |
*/ |
254 |
int |
255 |
os_binhex(struct part *inpart, int part, int nparts) |
256 |
{ |
257 |
return 1; |
258 |
} |
259 |
|
260 |
/* |
261 |
* Warn user that the MD5 digest of the last file created by os_newtypedfile() |
262 |
* did not match that supplied in the Content-MD5: header. |
263 |
*/ |
264 |
void os_warnMD5mismatch(void) |
265 |
{ |
266 |
char *warning; |
267 |
|
268 |
warning = xmalloc(strlen(output_fname) + 100); |
269 |
sprintf(warning, "%s was corrupted in transit", |
270 |
output_fname); |
271 |
warn(warning); |
272 |
free(warning); |
273 |
} |
274 |
|
275 |
/* |
276 |
* Report an error (in errno) concerning a filename |
277 |
*/ |
278 |
void os_perror(char *file) |
279 |
{ |
280 |
perror(file); |
281 |
} |