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