1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988 Regents of the University of California. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms are permitted 7 * provided that the above copyright notice and this paragraph are 8 * duplicated in all such forms and that any documentation, 9 * advertising materials, and other materials related to such 10 * distribution and use acknowledge that the software was developed 11 * by the University of California, Berkeley. The name of the 12 * University may not be used to endorse or promote products derived 13 * from this software without specific prior written permission. 14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 16 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 17 */ 18 19 #ifndef lint 20 static char sccsid[] = "@(#)collect.c 5.6 (Berkeley) 11/17/88"; 21 #endif /* not lint */ 22 23 # include <errno.h> 24 # include "sendmail.h" 25 26 /* 27 ** COLLECT -- read & parse message header & make temp file. 28 ** 29 ** Creates a temporary file name and copies the standard 30 ** input to that file. Leading UNIX-style "From" lines are 31 ** stripped off (after important information is extracted). 32 ** 33 ** Parameters: 34 ** sayok -- if set, give an ARPANET style message 35 ** to say we are ready to collect input. 36 ** 37 ** Returns: 38 ** none. 39 ** 40 ** Side Effects: 41 ** Temp file is created and filled. 42 ** The from person may be set. 43 */ 44 45 collect(sayok) 46 bool sayok; 47 { 48 register FILE *tf; 49 char buf[MAXFIELD+2]; 50 register char *p; 51 extern char *hvalue(); 52 53 /* 54 ** Create the temp file name and create the file. 55 */ 56 57 CurEnv->e_df = newstr(queuename(CurEnv, 'd')); 58 if ((tf = dfopen(CurEnv->e_df, "w")) == NULL) 59 { 60 syserr("Cannot create %s", CurEnv->e_df); 61 NoReturn = TRUE; 62 finis(); 63 } 64 (void) chmod(CurEnv->e_df, FileMode); 65 66 /* 67 ** Tell ARPANET to go ahead. 68 */ 69 70 if (sayok) 71 message("354", "Enter mail, end with \".\" on a line by itself"); 72 73 /* 74 ** Try to read a UNIX-style From line 75 */ 76 77 (void) sfgets(buf, sizeof buf, InChannel); 78 fixcrlf(buf, FALSE); 79 # ifndef NOTUNIX 80 if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 81 { 82 eatfrom(buf); 83 (void) sfgets(buf, sizeof buf, InChannel); 84 fixcrlf(buf, FALSE); 85 } 86 # endif NOTUNIX 87 88 /* 89 ** Copy InChannel to temp file & do message editing. 90 ** To keep certain mailers from getting confused, 91 ** and to keep the output clean, lines that look 92 ** like UNIX "From" lines are deleted in the header. 93 */ 94 95 do 96 { 97 int c; 98 extern bool isheader(); 99 100 /* drop out on error */ 101 if (ferror(InChannel)) 102 break; 103 104 /* if the line is too long, throw the rest away */ 105 if (index(buf, '\n') == NULL) 106 { 107 while ((c = getc(InChannel)) != '\n' && c != EOF) 108 continue; 109 /* give an error? */ 110 } 111 112 fixcrlf(buf, TRUE); 113 114 /* see if the header is over */ 115 if (!isheader(buf)) 116 break; 117 118 /* get the rest of this field */ 119 while ((c = getc(InChannel)) == ' ' || c == '\t') 120 { 121 p = &buf[strlen(buf)]; 122 *p++ = '\n'; 123 *p++ = c; 124 if (sfgets(p, MAXFIELD - (p - buf), InChannel) == NULL) 125 break; 126 fixcrlf(p, TRUE); 127 } 128 if (!feof(InChannel) && !ferror(InChannel)) 129 (void) ungetc(c, InChannel); 130 131 CurEnv->e_msgsize += strlen(buf); 132 133 /* 134 ** Snarf header away. 135 */ 136 137 if (bitset(H_EOH, chompheader(buf, FALSE))) 138 break; 139 } while (sfgets(buf, MAXFIELD, InChannel) != NULL); 140 141 # ifdef DEBUG 142 if (tTd(30, 1)) 143 printf("EOH\n"); 144 # endif DEBUG 145 146 /* throw away a blank line */ 147 if (buf[0] == '\0') 148 (void) sfgets(buf, MAXFIELD, InChannel); 149 150 /* 151 ** Collect the body of the message. 152 */ 153 154 do 155 { 156 register char *bp = buf; 157 158 fixcrlf(buf, TRUE); 159 160 /* check for end-of-message */ 161 if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 162 break; 163 164 /* check for transparent dot */ 165 if (OpMode == MD_SMTP && !IgnrDot && bp[0] == '.' && bp[1] == '.') 166 bp++; 167 168 /* 169 ** Figure message length, output the line to the temp 170 ** file, and insert a newline if missing. 171 */ 172 173 CurEnv->e_msgsize += strlen(bp) + 1; 174 fputs(bp, tf); 175 fputs("\n", tf); 176 if (ferror(tf)) 177 tferror(tf); 178 } while (sfgets(buf, MAXFIELD, InChannel) != NULL); 179 if (fflush(tf) != 0) 180 tferror(tf); 181 (void) fclose(tf); 182 183 /* An EOF when running SMTP is an error */ 184 if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP) 185 { 186 # ifdef LOG 187 if (RealHostName != NULL && LogLevel > 0) 188 syslog(LOG_NOTICE, 189 "collect: unexpected close on connection from %s: %m\n", 190 CurEnv->e_from.q_paddr, RealHostName); 191 # endif 192 usrerr("collect: unexpected close, from=%s", CurEnv->e_from.q_paddr); 193 194 /* don't return an error indication */ 195 CurEnv->e_to = NULL; 196 CurEnv->e_flags &= ~EF_FATALERRS; 197 198 /* and don't try to deliver the partial message either */ 199 finis(); 200 } 201 202 /* 203 ** Find out some information from the headers. 204 ** Examples are who is the from person & the date. 205 */ 206 207 eatheader(CurEnv); 208 209 /* 210 ** Add an Apparently-To: line if we have no recipient lines. 211 */ 212 213 if (hvalue("to") == NULL && hvalue("cc") == NULL && 214 hvalue("bcc") == NULL && hvalue("apparently-to") == NULL) 215 { 216 register ADDRESS *q; 217 218 /* create an Apparently-To: field */ 219 /* that or reject the message.... */ 220 for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next) 221 { 222 if (q->q_alias != NULL) 223 continue; 224 # ifdef DEBUG 225 if (tTd(30, 3)) 226 printf("Adding Apparently-To: %s\n", q->q_paddr); 227 # endif DEBUG 228 addheader("apparently-to", q->q_paddr, CurEnv); 229 } 230 } 231 232 if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL) 233 syserr("Cannot reopen %s", CurEnv->e_df); 234 } 235 /* 236 ** TFERROR -- signal error on writing the temporary file. 237 ** 238 ** Parameters: 239 ** tf -- the file pointer for the temporary file. 240 ** 241 ** Returns: 242 ** none. 243 ** 244 ** Side Effects: 245 ** Gives an error message. 246 ** Arranges for following output to go elsewhere. 247 */ 248 249 tferror(tf) 250 FILE *tf; 251 { 252 if (errno == ENOSPC) 253 { 254 (void) freopen(CurEnv->e_df, "w", tf); 255 fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 256 usrerr("452 Out of disk space for temp file"); 257 } 258 else 259 syserr("collect: Cannot write %s", CurEnv->e_df); 260 (void) freopen("/dev/null", "w", tf); 261 } 262 /* 263 ** EATFROM -- chew up a UNIX style from line and process 264 ** 265 ** This does indeed make some assumptions about the format 266 ** of UNIX messages. 267 ** 268 ** Parameters: 269 ** fm -- the from line. 270 ** 271 ** Returns: 272 ** none. 273 ** 274 ** Side Effects: 275 ** extracts what information it can from the header, 276 ** such as the date. 277 */ 278 279 # ifndef NOTUNIX 280 281 char *DowList[] = 282 { 283 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 284 }; 285 286 char *MonthList[] = 287 { 288 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 289 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 290 NULL 291 }; 292 293 eatfrom(fm) 294 char *fm; 295 { 296 register char *p; 297 register char **dt; 298 299 # ifdef DEBUG 300 if (tTd(30, 2)) 301 printf("eatfrom(%s)\n", fm); 302 # endif DEBUG 303 304 /* find the date part */ 305 p = fm; 306 while (*p != '\0') 307 { 308 /* skip a word */ 309 while (*p != '\0' && *p != ' ') 310 p++; 311 while (*p == ' ') 312 p++; 313 if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 314 continue; 315 316 /* we have a possible date */ 317 for (dt = DowList; *dt != NULL; dt++) 318 if (strncmp(*dt, p, 3) == 0) 319 break; 320 if (*dt == NULL) 321 continue; 322 323 for (dt = MonthList; *dt != NULL; dt++) 324 if (strncmp(*dt, &p[4], 3) == 0) 325 break; 326 if (*dt != NULL) 327 break; 328 } 329 330 if (*p != NULL) 331 { 332 char *q; 333 extern char *arpadate(); 334 335 /* we have found a date */ 336 q = xalloc(25); 337 (void) strncpy(q, p, 25); 338 q[24] = '\0'; 339 define('d', q, CurEnv); 340 q = arpadate(q); 341 define('a', newstr(q), CurEnv); 342 } 343 } 344 345 # endif NOTUNIX 346