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