1 # include <signal.h> 2 # include <errno.h> 3 # include "sendmail.h" 4 # ifdef LOG 5 # include <syslog.h> 6 # endif LOG 7 8 static char SccsId[] = "@(#)deliver.c 3.25 08/20/81"; 9 10 /* 11 ** DELIVER -- Deliver a message to a particular address. 12 ** 13 ** Parameters: 14 ** to -- the address to deliver the message to. 15 ** editfcn -- if non-NULL, we want to call this function 16 ** to output the letter (instead of just out- 17 ** putting it raw). 18 ** 19 ** Returns: 20 ** zero -- successfully delivered. 21 ** else -- some failure, see ExitStat for more info. 22 ** 23 ** Side Effects: 24 ** The standard input is passed off to someone. 25 */ 26 27 deliver(to, editfcn) 28 ADDRESS *to; 29 int (*editfcn)(); 30 { 31 char *host; 32 char *user; 33 char **pvp; 34 register char **mvp; 35 register char *p; 36 register struct mailer *m; 37 register int i; 38 extern putmessage(); 39 extern bool checkcompat(); 40 char *pv[MAXPV+1]; 41 char tobuf[MAXLINE]; 42 char buf[MAXNAME]; 43 bool firstone; 44 45 if (bitset(QDONTSEND, to->q_flags)) 46 return (0); 47 48 # ifdef DEBUG 49 if (Debug) 50 printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 51 to->q_mailer, to->q_host, to->q_user); 52 # endif DEBUG 53 54 /* 55 ** Do initial argv setup. 56 ** Insert the mailer name. Notice that $x expansion is 57 ** NOT done on the mailer name. Then, if the mailer has 58 ** a picky -f flag, we insert it as appropriate. This 59 ** code does not check for 'pv' overflow; this places a 60 ** manifest lower limit of 4 for MAXPV. 61 */ 62 63 m = Mailer[to->q_mailer]; 64 host = to->q_host; 65 define('g', m->m_from); /* translated from address */ 66 define('h', host); /* to host */ 67 Errors = 0; 68 errno = 0; 69 pvp = pv; 70 *pvp++ = m->m_argv[0]; 71 72 /* insert -f or -r flag as appropriate */ 73 if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag) 74 { 75 if (bitset(M_FOPT, m->m_flags)) 76 *pvp++ = "-f"; 77 else 78 *pvp++ = "-r"; 79 (void) expand("$g", buf, &buf[sizeof buf - 1]); 80 *pvp++ = newstr(buf); 81 } 82 83 /* 84 ** Append the other fixed parts of the argv. These run 85 ** up to the first entry containing "$u". There can only 86 ** be one of these, and there are only a few more slots 87 ** in the pv after it. 88 */ 89 90 for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 91 { 92 while ((p = index(p, '$')) != NULL) 93 if (*++p == 'u') 94 break; 95 if (p != NULL) 96 break; 97 98 /* this entry is safe -- go ahead and process it */ 99 (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 100 *pvp++ = newstr(buf); 101 if (pvp >= &pv[MAXPV - 3]) 102 { 103 syserr("Too many parameters to %s before $u", pv[0]); 104 return (-1); 105 } 106 } 107 if (*mvp == NULL) 108 syserr("No $u in mailer argv for %s", pv[0]); 109 110 /* 111 ** At this point *mvp points to the argument with $u. We 112 ** run through our address list and append all the addresses 113 ** we can. If we run out of space, do not fret! We can 114 ** always send another copy later. 115 */ 116 117 tobuf[0] = '\0'; 118 firstone = TRUE; 119 To = tobuf; 120 for (; to != NULL; to = to->q_next) 121 { 122 /* avoid sending multiple recipients to dumb mailers */ 123 if (!firstone && !bitset(M_MUSER, m->m_flags)) 124 break; 125 126 /* if already sent or not for this host, don't send */ 127 if (bitset(QDONTSEND, to->q_flags) || strcmp(to->q_host, host) != 0) 128 continue; 129 user = to->q_user; 130 To = to->q_paddr; 131 to->q_flags |= QDONTSEND; 132 firstone = FALSE; 133 134 # ifdef DEBUG 135 if (Debug) 136 printf(" send to `%s'\n", user); 137 # endif DEBUG 138 139 /* 140 ** Check to see that these people are allowed to 141 ** talk to each other. 142 */ 143 144 if (!checkcompat(to)) 145 { 146 giveresponse(EX_UNAVAILABLE, TRUE, m); 147 continue; 148 } 149 150 /* 151 ** Strip quote bits from names if the mailer is dumb 152 ** about them. 153 */ 154 155 if (bitset(M_STRIPQ, m->m_flags)) 156 { 157 stripquotes(user, TRUE); 158 stripquotes(host, TRUE); 159 } 160 else 161 { 162 stripquotes(user, FALSE); 163 stripquotes(host, FALSE); 164 } 165 166 /* 167 ** If an error message has already been given, don't 168 ** bother to send to this address. 169 ** 170 ** >>>>>>>>>> This clause assumes that the local mailer 171 ** >> NOTE >> cannot do any further aliasing; that 172 ** >>>>>>>>>> function is subsumed by sendmail. 173 */ 174 175 if (bitset(QBADADDR, to->q_flags)) 176 continue; 177 178 /* 179 ** See if this user name is "special". 180 ** If the user name has a slash in it, assume that this 181 ** is a file -- send it off without further ado. 182 ** Note that this means that editfcn's will not 183 ** be applied to the message. Also note that 184 ** this type of addresses is not processed along 185 ** with the others, so we fudge on the To person. 186 */ 187 188 if (m == Mailer[M_LOCAL]) 189 { 190 if (index(user, '/') != NULL) 191 { 192 i = mailfile(user); 193 giveresponse(i, TRUE, m); 194 continue; 195 } 196 } 197 198 /* create list of users for error messages */ 199 if (tobuf[0] != '\0') 200 (void) strcat(tobuf, ","); 201 (void) strcat(tobuf, to->q_paddr); 202 define('u', user); /* to user */ 203 define('z', to->q_home); /* user's home */ 204 205 /* expand out this user */ 206 (void) expand(user, buf, &buf[sizeof buf - 1]); 207 *pvp++ = newstr(buf); 208 if (pvp >= &pv[MAXPV - 2]) 209 { 210 /* allow some space for trailing parms */ 211 break; 212 } 213 } 214 215 /* see if any addresses still exist */ 216 if (tobuf[0] == '\0') 217 return (0); 218 219 /* print out messages as full list */ 220 To = tobuf; 221 222 /* 223 ** Fill out any parameters after the $u parameter. 224 */ 225 226 while (*++mvp != NULL) 227 { 228 (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 229 *pvp++ = newstr(buf); 230 if (pvp >= &pv[MAXPV]) 231 syserr("deliver: pv overflow after $u for %s", pv[0]); 232 } 233 *pvp++ = NULL; 234 235 /* 236 ** Call the mailer. 237 ** The argument vector gets built, pipes 238 ** are created as necessary, and we fork & exec as 239 ** appropriate. 240 ** 241 ** Notice the tacky hack to handle private mailers. 242 */ 243 244 if (editfcn == NULL) 245 editfcn = putmessage; 246 i = sendoff(m, pv, editfcn); 247 248 return (i); 249 } 250 /* 251 ** SENDOFF -- send off call to mailer & collect response. 252 ** 253 ** Parameters: 254 ** m -- mailer descriptor. 255 ** pvp -- parameter vector to send to it. 256 ** editfcn -- function to pipe it through. 257 ** 258 ** Returns: 259 ** exit status of mailer. 260 ** 261 ** Side Effects: 262 ** none. 263 */ 264 265 #define NFORKTRIES 5 266 267 sendoff(m, pvp, editfcn) 268 struct mailer *m; 269 char **pvp; 270 int (*editfcn)(); 271 { 272 auto int st; 273 register int i; 274 int pid; 275 int pvect[2]; 276 FILE *mfile; 277 extern putmessage(); 278 extern FILE *fdopen(); 279 280 # ifdef DEBUG 281 if (Debug) 282 { 283 printf("Sendoff:\n"); 284 printav(pvp); 285 } 286 # endif DEBUG 287 288 /* create a pipe to shove the mail through */ 289 if (pipe(pvect) < 0) 290 { 291 syserr("pipe"); 292 return (-1); 293 } 294 for (i = NFORKTRIES; i-- > 0; ) 295 { 296 # ifdef VFORK 297 pid = vfork(); 298 # else 299 pid = fork(); 300 # endif 301 if (pid >= 0) 302 break; 303 sleep((unsigned) NFORKTRIES - i); 304 } 305 if (pid < 0) 306 { 307 syserr("Cannot fork"); 308 (void) close(pvect[0]); 309 (void) close(pvect[1]); 310 return (-1); 311 } 312 else if (pid == 0) 313 { 314 /* child -- set up input & exec mailer */ 315 /* make diagnostic output be standard output */ 316 (void) close(2); 317 (void) dup(1); 318 (void) signal(SIGINT, SIG_IGN); 319 (void) close(0); 320 if (dup(pvect[0]) < 0) 321 { 322 syserr("Cannot dup to zero!"); 323 _exit(EX_OSERR); 324 } 325 (void) close(pvect[0]); 326 (void) close(pvect[1]); 327 if (!bitset(M_RESTR, m->m_flags)) 328 (void) setuid(getuid()); 329 # ifndef VFORK 330 /* 331 ** We have to be careful with vfork - we can't mung up the 332 ** memory but we don't want the mailer to inherit any extra 333 ** open files. Chances are the mailer won't 334 ** care about an extra file, but then again you never know. 335 ** Actually, we would like to close(fileno(pwf)), but it's 336 ** declared static so we can't. But if we fclose(pwf), which 337 ** is what endpwent does, it closes it in the parent too and 338 ** the next getpwnam will be slower. If you have a weird 339 ** mailer that chokes on the extra file you should do the 340 ** endpwent(). 341 ** 342 ** Similar comments apply to log. However, openlog is 343 ** clever enough to set the FIOCLEX mode on the file, 344 ** so it will be closed automatically on the exec. 345 */ 346 347 endpwent(); 348 # ifdef LOG 349 closelog(); 350 # endif LOG 351 # endif VFORK 352 execv(m->m_mailer, pvp); 353 /* syserr fails because log is closed */ 354 /* syserr("Cannot exec %s", m->m_mailer); */ 355 printf("Cannot exec %s\n", m->m_mailer); 356 (void) fflush(stdout); 357 _exit(EX_UNAVAILABLE); 358 } 359 360 /* write out message to mailer */ 361 (void) close(pvect[0]); 362 (void) signal(SIGPIPE, SIG_IGN); 363 mfile = fdopen(pvect[1], "w"); 364 if (editfcn == NULL) 365 editfcn = putmessage; 366 (*editfcn)(mfile, m); 367 (void) fclose(mfile); 368 369 /* 370 ** Wait for child to die and report status. 371 ** We should never get fatal errors (e.g., segmentation 372 ** violation), so we report those specially. For other 373 ** errors, we choose a status message (into statmsg), 374 ** and if it represents an error, we print it. 375 */ 376 377 while ((i = wait(&st)) > 0 && i != pid) 378 continue; 379 if (i < 0) 380 { 381 syserr("wait"); 382 return (-1); 383 } 384 if ((st & 0377) != 0) 385 { 386 syserr("%s: stat %o", pvp[0], st); 387 ExitStat = EX_UNAVAILABLE; 388 return (-1); 389 } 390 i = (st >> 8) & 0377; 391 giveresponse(i, TRUE, m); 392 return (i); 393 } 394 /* 395 ** GIVERESPONSE -- Interpret an error response from a mailer 396 ** 397 ** Parameters: 398 ** stat -- the status code from the mailer (high byte 399 ** only; core dumps must have been taken care of 400 ** already). 401 ** force -- if set, force an error message output, even 402 ** if the mailer seems to like to print its own 403 ** messages. 404 ** m -- the mailer descriptor for this mailer. 405 ** 406 ** Returns: 407 ** none. 408 ** 409 ** Side Effects: 410 ** Errors may be incremented. 411 ** ExitStat may be set. 412 ** 413 ** Called By: 414 ** deliver 415 */ 416 417 giveresponse(stat, force, m) 418 int stat; 419 int force; 420 register struct mailer *m; 421 { 422 register char *statmsg; 423 extern char *SysExMsg[]; 424 register int i; 425 extern int N_SysEx; 426 extern long MsgSize; 427 char buf[30]; 428 429 i = stat - EX__BASE; 430 if (i < 0 || i > N_SysEx) 431 statmsg = NULL; 432 else 433 statmsg = SysExMsg[i]; 434 if (stat == 0) 435 { 436 if (bitset(M_FINAL, m->m_flags)) 437 statmsg = "delivered"; 438 else 439 statmsg = "queued"; 440 if (Verbose) 441 message(Arpa_Info, statmsg); 442 } 443 else 444 { 445 Errors++; 446 if (statmsg == NULL && m->m_badstat != 0) 447 { 448 stat = m->m_badstat; 449 i = stat - EX__BASE; 450 # ifdef DEBUG 451 if (i < 0 || i >= N_SysEx) 452 syserr("Bad m_badstat %d", stat); 453 else 454 # endif DEBUG 455 statmsg = SysExMsg[i]; 456 } 457 if (statmsg == NULL) 458 usrerr("unknown mailer response %d", stat); 459 else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 460 usrerr("%s", statmsg); 461 } 462 463 /* 464 ** Final cleanup. 465 ** Log a record of the transaction. Compute the new 466 ** ExitStat -- if we already had an error, stick with 467 ** that. 468 */ 469 470 if (statmsg == NULL) 471 { 472 (void) sprintf(buf, "error %d", stat); 473 statmsg = buf; 474 } 475 476 # ifdef LOG 477 syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg); 478 # endif LOG 479 setstat(stat); 480 } 481 /* 482 ** PUTMESSAGE -- output a message to the final mailer. 483 ** 484 ** This routine takes care of recreating the header from the 485 ** in-core copy, etc. 486 ** 487 ** Parameters: 488 ** fp -- file to output onto. 489 ** m -- a mailer descriptor. 490 ** 491 ** Returns: 492 ** none. 493 ** 494 ** Side Effects: 495 ** The message is written onto fp. 496 */ 497 498 putmessage(fp, m) 499 FILE *fp; 500 struct mailer *m; 501 { 502 char buf[BUFSIZ]; 503 register int i; 504 HDR *h; 505 register char *p; 506 extern char *arpadate(); 507 bool anyheader = FALSE; 508 extern char *capitalize(); 509 510 /* output "From" line unless supressed */ 511 if (!bitset(M_NHDR, m->m_flags)) 512 fprintf(fp, "%s\n", FromLine); 513 514 /* output all header lines */ 515 for (h = Header; h != NULL; h = h->h_link) 516 { 517 if (bitset(H_DELETE, h->h_flags)) 518 continue; 519 if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 520 continue; 521 if (bitset(H_DEFAULT, h->h_flags)) 522 { 523 (void) expand(h->h_value, buf, &buf[sizeof buf]); 524 p = buf; 525 } 526 else 527 p = h->h_value; 528 if (*p == '\0') 529 continue; 530 fprintf(fp, "%s: %s\n", capitalize(h->h_field), p); 531 h->h_flags |= H_USED; 532 anyheader = TRUE; 533 } 534 535 if (anyheader) 536 fprintf(fp, "\n"); 537 538 /* output the body of the message */ 539 rewind(TempFile); 540 while (!ferror(fp) && (i = fread(buf, 1, BUFSIZ, TempFile)) > 0) 541 (void) fwrite(buf, 1, i, fp); 542 543 if (ferror(fp) && errno != EPIPE) 544 { 545 syserr("putmessage: write error"); 546 setstat(EX_IOERR); 547 } 548 errno = 0; 549 } 550 /* 551 ** MAILFILE -- Send a message to a file. 552 ** 553 ** Parameters: 554 ** filename -- the name of the file to send to. 555 ** 556 ** Returns: 557 ** The exit code associated with the operation. 558 ** 559 ** Side Effects: 560 ** none. 561 ** 562 ** Called By: 563 ** deliver 564 */ 565 566 mailfile(filename) 567 char *filename; 568 { 569 register FILE *f; 570 571 f = fopen(filename, "a"); 572 if (f == NULL) 573 return (EX_CANTCREAT); 574 575 putmessage(f, Mailer[1]); 576 fputs("\n", f); 577 (void) fclose(f); 578 return (EX_OK); 579 } 580