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