1 # include <stdio.h> 2 # include <ctype.h> 3 # include <errno.h> 4 # include "sendmail.h" 5 6 static char SccsId[] = "@(#)collect.c 3.7 03/20/81"; 7 8 /* 9 ** COLLECT -- read & parse message header & make temp file. 10 ** 11 ** Creates a temporary file name and copies the standard 12 ** input to that file. While it is doing it, it looks for 13 ** "From:" and "Sender:" fields to use as the from-person 14 ** (but only if the -a flag is specified). It prefers to 15 ** to use the "Sender:" field. 16 ** 17 ** MIT seems to like to produce "Sent-By:" fields instead 18 ** of "Sender:" fields. We used to catch this, but it turns 19 ** out that the "Sent-By:" field doesn't always correspond 20 ** to someone real ("___057", for instance), as required by 21 ** the protocol. So we limp by..... 22 ** 23 ** Parameters: 24 ** none 25 ** 26 ** Returns: 27 ** Name of temp file. 28 ** 29 ** Side Effects: 30 ** Temp file is created and filled. 31 ** 32 ** Called By: 33 ** main 34 ** 35 ** Notes: 36 ** This is broken off from main largely so that the 37 ** temp buffer can be deallocated. 38 */ 39 40 char *MsgId; /* message-id, determined or created */ 41 long MsgSize; /* size of message in bytes */ 42 char *Date; /* UNIX-style origination date */ 43 44 char * 45 collect() 46 { 47 register FILE *tf; 48 char buf[MAXFIELD+1]; 49 register char *p; 50 char c; 51 extern int errno; 52 register HDR *h; 53 HDR **hp; 54 extern bool isheader(); 55 extern char *newstr(); 56 extern char *xalloc(); 57 char *fname; 58 char *fvalue; 59 extern char *index(), *rindex(); 60 char *xfrom; 61 extern char *hvalue(); 62 struct hdrinfo *hi; 63 extern char *strcpy(), *strcat(), *mktemp(); 64 65 /* 66 ** Create the temp file name and create the file. 67 */ 68 69 mktemp(InFileName); 70 close(creat(InFileName, 0600)); 71 if ((tf = fopen(InFileName, "w")) == NULL) 72 { 73 syserr("Cannot create %s", InFileName); 74 return (NULL); 75 } 76 77 /* try to read a UNIX-style From line */ 78 if (fgets(buf, sizeof buf, stdin) == NULL) 79 return (NULL); 80 if (strncmp(buf, "From ", 5) == 0) 81 { 82 eatfrom(buf); 83 fgets(buf, sizeof buf, stdin); 84 } 85 86 /* 87 ** Copy stdin to temp file & do message editting. 88 ** To keep certain mailers from getting confused, 89 ** and to keep the output clean, lines that look 90 ** like UNIX "From" lines are deleted in the header, 91 ** and prepended with ">" in the body. 92 */ 93 94 for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin)) 95 { 96 /* see if the header is over */ 97 if (!isheader(buf)) 98 break; 99 100 /* get the rest of this field */ 101 while ((c = getc(stdin)) == ' ' || c == '\t') 102 { 103 p = &buf[strlen(buf)]; 104 *p++ = c; 105 if (fgets(p, sizeof buf - (p - buf), stdin) == NULL) 106 break; 107 } 108 if (c != EOF) 109 ungetc(c, stdin); 110 111 MsgSize += strlen(buf); 112 113 /* 114 ** Snarf header away. 115 */ 116 117 /* strip off trailing newline */ 118 p = rindex(buf, '\n'); 119 if (p != NULL) 120 *p = '\0'; 121 122 /* find canonical name */ 123 fname = buf; 124 p = index(buf, ':'); 125 fvalue = &p[1]; 126 while (isspace(*--p)) 127 continue; 128 *++p = '\0'; 129 makelower(fname); 130 131 /* strip field value on front */ 132 if (*fvalue == ' ') 133 fvalue++; 134 135 /* search header list for this header */ 136 for (hp = &Header, h = Header; h != NULL; hp = &h->h_link, h = h->h_link) 137 { 138 if (strcmp(fname, h->h_field) == 0 && bitset(H_DEFAULT, h->h_flags)) 139 break; 140 } 141 142 /* see if it is a known type */ 143 for (hi = HdrInfo; hi->hi_field != NULL; hi++) 144 { 145 if (strcmp(hi->hi_field, fname) == 0) 146 break; 147 } 148 149 /* if this means "end of header" quit now */ 150 if (bitset(H_EOH, hi->hi_flags)) 151 break; 152 153 /* create/fill in a new node */ 154 if (h == NULL) 155 { 156 /* create a new node */ 157 *hp = h = (HDR *) xalloc(sizeof *h); 158 h->h_field = newstr(fname); 159 h->h_value = NULL; 160 h->h_link = NULL; 161 h->h_flags = hi->hi_flags; 162 } 163 if (h->h_value != NULL) 164 free(h->h_value); 165 h->h_value = newstr(fvalue); 166 167 /* save the location of this field */ 168 if (hi->hi_pptr != NULL) 169 *hi->hi_pptr = h->h_value; 170 } 171 172 # ifdef DEBUG 173 if (Debug) 174 printf("EOH\n"); 175 # endif DEBUG 176 177 /* throw away a blank line */ 178 if (buf[0] == '\n') 179 fgets(buf, sizeof buf, stdin); 180 181 /* 182 ** Collect the body of the message. 183 */ 184 185 for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin) != NULL) 186 { 187 /* check for end-of-message */ 188 if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 189 break; 190 191 /* Hide UNIX-like From lines */ 192 if (strncmp(buf, "From ", 5) == 0) 193 { 194 fputs(">", tf); 195 MsgSize++; 196 } 197 MsgSize += strlen(buf); 198 fputs(buf, tf); 199 if (ferror(tf)) 200 { 201 if (errno == ENOSPC) 202 { 203 freopen(InFileName, "w", tf); 204 fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 205 syserr("Out of disk space for temp file"); 206 } 207 else 208 syserr("Cannot write %s", InFileName); 209 freopen("/dev/null", "w", tf); 210 } 211 } 212 fclose(tf); 213 214 /* 215 ** Find out some information from the headers. 216 ** Examples are who is the from person & the date. Some 217 ** fields, e.g., Message-Id, may have been handled by 218 ** the hi_pptr mechanism. 219 */ 220 221 /* from person */ 222 xfrom = hvalue("sender"); 223 if (xfrom == NULL) 224 xfrom = hvalue("from"); 225 226 /* date message originated */ 227 /* we don't seem to have a good way to do canonical conversion .... 228 p = hvalue("date"); 229 if (p != NULL) 230 Date = newstr(arpatounix(p)); 231 .... so we will ignore the problem for the time being */ 232 if (Date == NULL) 233 { 234 extern char *ctime(); 235 236 Date = newstr(ctime(&CurTime)); 237 } 238 239 if (freopen(InFileName, "r", stdin) == NULL) 240 syserr("Cannot reopen %s", InFileName); 241 242 # ifdef DEBUG 243 if (Debug) 244 { 245 printf("----- collected header -----\n"); 246 for (h = Header; h != NULL; h = h->h_link) 247 printf("%s: %s\n", capitalize(h->h_field), h->h_value); 248 printf("----------------------------\n"); 249 } 250 # endif DEBUG 251 return (ArpaFmt ? xfrom : NULL); 252 } 253 /* 254 ** EATFROM -- chew up a UNIX style from line and process 255 ** 256 ** This does indeed make some assumptions about the format 257 ** of UNIX messages. 258 ** 259 ** Parameters: 260 ** fm -- the from line. 261 ** 262 ** Returns: 263 ** none. 264 ** 265 ** Side Effects: 266 ** extracts what information it can from the header, 267 ** such as the Date. 268 */ 269 270 char *MonthList[] = 271 { 272 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 273 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 274 NULL 275 }; 276 277 eatfrom(fm) 278 char *fm; 279 { 280 register char *p; 281 register char **dt; 282 283 /* find the date part */ 284 p = fm; 285 while (*p != '\0') 286 { 287 /* skip a word */ 288 while (*p != '\0' && *p != ' ') 289 *p++; 290 while (*p == ' ') 291 *p++; 292 if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 293 continue; 294 295 /* we have a possible date */ 296 for (dt = MonthList; *dt != NULL; dt++) 297 if (strncmp(*dt, p, 3) == 0) 298 break; 299 300 if (*dt != NULL) 301 break; 302 } 303 304 if (*p != NULL) 305 { 306 /* we have found a date */ 307 Date = xalloc(25); 308 strncpy(Date, p, 25); 309 Date[24] = '\0'; 310 } 311 } 312 /* 313 ** HVALUE -- return value of a header. 314 ** 315 ** Parameters: 316 ** field -- the field name. 317 ** 318 ** Returns: 319 ** pointer to the value part. 320 ** NULL if not found. 321 ** 322 ** Side Effects: 323 ** sets the H_USED bit in the header if found. 324 */ 325 326 char * 327 hvalue(field) 328 char *field; 329 { 330 register HDR *h; 331 332 for (h = Header; h != NULL; h = h->h_link) 333 { 334 if (strcmp(h->h_field, field) == 0) 335 { 336 h->h_flags |= H_USED; 337 return (h->h_value); 338 } 339 } 340 return (NULL); 341 } 342 /* 343 ** MAKEMSGID -- Compute a message id for this process. 344 ** 345 ** This routine creates a message id for a message if 346 ** it did not have one already. If the MESSAGEID compile 347 ** flag is set, the messageid will be added to any message 348 ** that does not already have one. Currently it is more 349 ** of an artifact, but I suggest that if you are hacking, 350 ** you leave it in -- I may want to use it someday if 351 ** duplicate messages turn out to be a problem. 352 ** 353 ** Parameters: 354 ** none. 355 ** 356 ** Returns: 357 ** a message id. 358 ** 359 ** Side Effects: 360 ** none. 361 */ 362 363 char * 364 makemsgid() 365 { 366 static char buf[50]; 367 extern char *expand(); 368 369 expand("<$m>", buf, &buf[sizeof buf - 1]); 370 return (buf); 371 } 372 /* 373 ** ISHEADER -- predicate telling if argument is a header. 374 ** 375 ** Parameters: 376 ** s -- string to check for possible headerness. 377 ** 378 ** Returns: 379 ** TRUE if s is a header. 380 ** FALSE otherwise. 381 ** 382 ** Side Effects: 383 ** none. 384 */ 385 386 bool 387 isheader(s) 388 register char *s; 389 { 390 if (!isalnum(*s)) 391 return (FALSE); 392 while (!isspace(*s) && *s != ':') 393 s++; 394 while (isspace(*s)) 395 s++; 396 return (*s == ':'); 397 } 398