1 # include <errno.h> 2 # include "sendmail.h" 3 4 SCCSID(@(#)collect.c 3.57 02/18/83); 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 if (fgets(buf, sizeof buf, InChannel) == NULL) 58 return; 59 fixcrlf(buf, FALSE); 60 # ifndef NOTUNIX 61 if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 62 { 63 eatfrom(buf); 64 (void) fgets(buf, sizeof buf, InChannel); 65 fixcrlf(buf, FALSE); 66 } 67 # endif NOTUNIX 68 69 /* 70 ** Copy InChannel to temp file & do message editing. 71 ** To keep certain mailers from getting confused, 72 ** and to keep the output clean, lines that look 73 ** like UNIX "From" lines are deleted in the header, 74 ** and prepended with ">" in the body. 75 */ 76 77 for (; !feof(InChannel); !feof(InChannel) && 78 fgets(buf, MAXFIELD, InChannel) != NULL) 79 { 80 register char c; 81 extern bool isheader(); 82 83 /* if the line is too long, throw the rest away */ 84 if (index(buf, '\n') == NULL) 85 { 86 while ((c = getc(InChannel)) != '\n') 87 continue; 88 /* give an error? */ 89 } 90 91 fixcrlf(buf, TRUE); 92 93 /* see if the header is over */ 94 if (!isheader(buf)) 95 break; 96 97 /* get the rest of this field */ 98 while ((c = getc(InChannel)) == ' ' || c == '\t') 99 { 100 p = &buf[strlen(buf)]; 101 *p++ = '\n'; 102 *p++ = c; 103 if (fgets(p, MAXFIELD - (p - buf), InChannel) == NULL) 104 break; 105 fixcrlf(p, TRUE); 106 } 107 if (!feof(InChannel)) 108 (void) ungetc(c, InChannel); 109 110 CurEnv->e_msgsize += strlen(buf); 111 112 /* 113 ** Snarf header away. 114 */ 115 116 if (bitset(H_EOH, chompheader(buf, FALSE))) 117 break; 118 } 119 120 # ifdef DEBUG 121 if (tTd(30, 1)) 122 printf("EOH\n"); 123 # endif DEBUG 124 125 /* throw away a blank line */ 126 if (buf[0] == '\0') 127 { 128 (void) fgets(buf, MAXFIELD, InChannel); 129 fixcrlf(buf, TRUE); 130 } 131 132 /* 133 ** Collect the body of the message. 134 */ 135 136 for (; !feof(InChannel); !feof(InChannel) && 137 fgets(buf, sizeof buf, InChannel) != NULL) 138 { 139 register char *bp = buf; 140 141 fixcrlf(buf, TRUE); 142 143 /* check for end-of-message */ 144 if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 145 break; 146 147 /* check for transparent dot */ 148 if (OpMode == MD_SMTP && !IgnrDot && bp[0] == '.' && bp[1] == '.') 149 bp++; 150 151 # ifndef NOTUNIX 152 /* Hide UNIX-like From lines */ 153 if (strncmp(bp, "From ", 5) == 0) 154 { 155 fputs(">", tf); 156 CurEnv->e_msgsize++; 157 } 158 # endif NOTUNIX 159 160 /* 161 ** Figure message length, output the line to the temp 162 ** file, and insert a newline if missing. 163 */ 164 165 CurEnv->e_msgsize += strlen(bp) + 1; 166 fputs(bp, tf); 167 fputs("\n", tf); 168 if (ferror(tf)) 169 { 170 if (errno == ENOSPC) 171 { 172 (void) freopen(CurEnv->e_df, "w", tf); 173 fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 174 usrerr("452 Out of disk space for temp file"); 175 } 176 else 177 syserr("collect: Cannot write %s", CurEnv->e_df); 178 (void) freopen("/dev/null", "w", tf); 179 } 180 } 181 (void) fclose(tf); 182 183 /* An EOF when running SMTP is an error */ 184 if (feof(InChannel) && OpMode == MD_SMTP) 185 syserr("collect: unexpected close"); 186 187 /* 188 ** Find out some information from the headers. 189 ** Examples are who is the from person & the date. 190 */ 191 192 eatheader(CurEnv); 193 194 /* 195 ** Add an Apparently-To: line if we have no recipient lines. 196 */ 197 198 if (hvalue("to") == NULL && hvalue("cc") == NULL && 199 hvalue("bcc") == NULL && hvalue("apparently-to") == NULL) 200 { 201 register ADDRESS *q; 202 203 /* create an Apparently-To: field */ 204 /* that or reject the message.... */ 205 for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next) 206 { 207 if (q->q_alias != NULL) 208 continue; 209 # ifdef DEBUG 210 if (tTd(30, 3)) 211 printf("Adding Apparently-To: %s\n", q->q_paddr); 212 # endif DEBUG 213 addheader("apparently-to", q->q_paddr, CurEnv); 214 } 215 } 216 217 if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL) 218 syserr("Cannot reopen %s", CurEnv->e_df); 219 220 /* 221 ** Log collection information. 222 */ 223 224 # ifdef LOG 225 if (LogLevel > 1) 226 syslog(LOG_INFO, "%s: from=%s, size=%ld, class=%d\n", 227 CurEnv->e_id, CurEnv->e_from.q_paddr, CurEnv->e_msgsize, 228 CurEnv->e_class); 229 # endif LOG 230 return; 231 } 232 /* 233 ** EATFROM -- chew up a UNIX style from line and process 234 ** 235 ** This does indeed make some assumptions about the format 236 ** of UNIX messages. 237 ** 238 ** Parameters: 239 ** fm -- the from line. 240 ** 241 ** Returns: 242 ** none. 243 ** 244 ** Side Effects: 245 ** extracts what information it can from the header, 246 ** such as the date. 247 */ 248 249 # ifndef NOTUNIX 250 251 char *DowList[] = 252 { 253 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 254 }; 255 256 char *MonthList[] = 257 { 258 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 259 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 260 NULL 261 }; 262 263 eatfrom(fm) 264 char *fm; 265 { 266 register char *p; 267 register char **dt; 268 269 # ifdef DEBUG 270 if (tTd(30, 2)) 271 printf("eatfrom(%s)\n", fm); 272 # endif DEBUG 273 274 /* find the date part */ 275 p = fm; 276 while (*p != '\0') 277 { 278 /* skip a word */ 279 while (*p != '\0' && *p != ' ') 280 *p++; 281 while (*p == ' ') 282 *p++; 283 if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 284 continue; 285 286 /* we have a possible date */ 287 for (dt = DowList; *dt != NULL; dt++) 288 if (strncmp(*dt, p, 3) == 0) 289 break; 290 if (*dt == NULL) 291 continue; 292 293 for (dt = MonthList; *dt != NULL; dt++) 294 if (strncmp(*dt, &p[4], 3) == 0) 295 break; 296 if (*dt != NULL) 297 break; 298 } 299 300 if (*p != NULL) 301 { 302 char *q; 303 extern char *arpadate(); 304 305 /* we have found a date */ 306 q = xalloc(25); 307 strncpy(q, p, 25); 308 q[24] = '\0'; 309 define('d', q, CurEnv); 310 q = arpadate(q); 311 define('a', newstr(q), CurEnv); 312 } 313 } 314 315 # endif NOTUNIX 316