1 # include <errno.h> 2 # include "sendmail.h" 3 4 SCCSID(@(#)collect.c 3.60 04/17/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 /* 152 ** Figure message length, output the line to the temp 153 ** file, and insert a newline if missing. 154 */ 155 156 CurEnv->e_msgsize += strlen(bp) + 1; 157 fputs(bp, tf); 158 fputs("\n", tf); 159 if (ferror(tf)) 160 tferror(tf); 161 } 162 if (fflush(tf) != 0) 163 tferror(tf); 164 (void) fclose(tf); 165 166 /* An EOF when running SMTP is an error */ 167 if (feof(InChannel) && OpMode == MD_SMTP) 168 syserr("collect: unexpected close"); 169 170 /* 171 ** Find out some information from the headers. 172 ** Examples are who is the from person & the date. 173 */ 174 175 eatheader(CurEnv); 176 177 /* 178 ** Add an Apparently-To: line if we have no recipient lines. 179 */ 180 181 if (hvalue("to") == NULL && hvalue("cc") == NULL && 182 hvalue("bcc") == NULL && hvalue("apparently-to") == NULL) 183 { 184 register ADDRESS *q; 185 186 /* create an Apparently-To: field */ 187 /* that or reject the message.... */ 188 for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next) 189 { 190 if (q->q_alias != NULL) 191 continue; 192 # ifdef DEBUG 193 if (tTd(30, 3)) 194 printf("Adding Apparently-To: %s\n", q->q_paddr); 195 # endif DEBUG 196 addheader("apparently-to", q->q_paddr, CurEnv); 197 } 198 } 199 200 if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL) 201 syserr("Cannot reopen %s", CurEnv->e_df); 202 } 203 /* 204 ** TFERROR -- signal error on writing the temporary file. 205 ** 206 ** Parameters: 207 ** tf -- the file pointer for the temporary file. 208 ** 209 ** Returns: 210 ** none. 211 ** 212 ** Side Effects: 213 ** Gives an error message. 214 ** Arranges for following output to go elsewhere. 215 */ 216 217 tferror(tf) 218 FILE *tf; 219 { 220 if (errno == ENOSPC) 221 { 222 (void) freopen(CurEnv->e_df, "w", tf); 223 fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 224 usrerr("452 Out of disk space for temp file"); 225 } 226 else 227 syserr("collect: Cannot write %s", CurEnv->e_df); 228 (void) freopen("/dev/null", "w", tf); 229 } 230 /* 231 ** EATFROM -- chew up a UNIX style from line and process 232 ** 233 ** This does indeed make some assumptions about the format 234 ** of UNIX messages. 235 ** 236 ** Parameters: 237 ** fm -- the from line. 238 ** 239 ** Returns: 240 ** none. 241 ** 242 ** Side Effects: 243 ** extracts what information it can from the header, 244 ** such as the date. 245 */ 246 247 # ifndef NOTUNIX 248 249 char *DowList[] = 250 { 251 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 252 }; 253 254 char *MonthList[] = 255 { 256 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 257 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 258 NULL 259 }; 260 261 eatfrom(fm) 262 char *fm; 263 { 264 register char *p; 265 register char **dt; 266 267 # ifdef DEBUG 268 if (tTd(30, 2)) 269 printf("eatfrom(%s)\n", fm); 270 # endif DEBUG 271 272 /* find the date part */ 273 p = fm; 274 while (*p != '\0') 275 { 276 /* skip a word */ 277 while (*p != '\0' && *p != ' ') 278 *p++; 279 while (*p == ' ') 280 *p++; 281 if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 282 continue; 283 284 /* we have a possible date */ 285 for (dt = DowList; *dt != NULL; dt++) 286 if (strncmp(*dt, p, 3) == 0) 287 break; 288 if (*dt == NULL) 289 continue; 290 291 for (dt = MonthList; *dt != NULL; dt++) 292 if (strncmp(*dt, &p[4], 3) == 0) 293 break; 294 if (*dt != NULL) 295 break; 296 } 297 298 if (*p != NULL) 299 { 300 char *q; 301 extern char *arpadate(); 302 303 /* we have found a date */ 304 q = xalloc(25); 305 strncpy(q, p, 25); 306 q[24] = '\0'; 307 define('d', q, CurEnv); 308 q = arpadate(q); 309 define('a', newstr(q), CurEnv); 310 } 311 } 312 313 # endif NOTUNIX 314