1 # include <stdio.h> 2 # include <pwd.h> 3 # include <signal.h> 4 # include <ctype.h> 5 # include "sendmail.h" 6 # ifdef LOG 7 # include <syslog.h> 8 # endif LOG 9 10 static char SccsId[] = "@(#)deliver.c 3.11 04/27/81"; 11 12 /* 13 ** DELIVER -- Deliver a message to a particular address. 14 ** 15 ** Parameters: 16 ** to -- the address to deliver the message to. 17 ** editfcn -- if non-NULL, we want to call this function 18 ** to output the letter (instead of just out- 19 ** putting it raw). 20 ** 21 ** Returns: 22 ** zero -- successfully delivered. 23 ** else -- some failure, see ExitStat for more info. 24 ** 25 ** Side Effects: 26 ** The standard input is passed off to someone. 27 */ 28 29 deliver(to, editfcn) 30 ADDRESS *to; 31 int (*editfcn)(); 32 { 33 char *host; 34 char *user; 35 extern struct passwd *getpwnam(); 36 char **pvp; 37 register char **mvp; 38 register char *p; 39 register struct mailer *m; 40 register int i; 41 extern int errno; 42 extern putmessage(); 43 extern char *index(); 44 extern bool checkcompat(); 45 char *pv[MAXPV+1]; 46 extern char *newstr(); 47 char tobuf[MAXLINE]; 48 char buf[MAXNAME]; 49 extern char *expand(); 50 bool firstone; 51 52 if (bitset(QDONTSEND, to->q_flags)) 53 return (0); 54 55 # ifdef DEBUG 56 if (Debug) 57 printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 58 to->q_mailer, to->q_host, to->q_user); 59 # endif DEBUG 60 61 /* 62 ** Do initial argv setup. 63 ** Insert the mailer name. Notice that $x expansion is 64 ** NOT done on the mailer name. Then, if the mailer has 65 ** a picky -f flag, we insert it as appropriate. This 66 ** code does not check for 'pv' overflow; this places a 67 ** manifest lower limit of 4 for MAXPV. 68 */ 69 70 m = Mailer[to->q_mailer]; 71 host = to->q_host; 72 define('g', m->m_from); /* translated from address */ 73 define('h', host); /* to host */ 74 Errors = 0; 75 errno = 0; 76 pvp = pv; 77 *pvp++ = m->m_argv[0]; 78 79 /* insert -f or -r flag as appropriate */ 80 if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag) 81 { 82 if (bitset(M_FOPT, m->m_flags)) 83 *pvp++ = "-f"; 84 else 85 *pvp++ = "-r"; 86 expand("$g", buf, &buf[sizeof buf - 1]); 87 *pvp++ = newstr(buf); 88 } 89 90 /* 91 ** Append the other fixed parts of the argv. These run 92 ** up to the first entry containing "$u". There can only 93 ** be one of these, and there are only a few more slots 94 ** in the pv after it. 95 */ 96 97 for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 98 { 99 while ((p = index(p, '$')) != NULL) 100 if (*++p == 'u') 101 break; 102 if (p != NULL) 103 break; 104 105 /* this entry is safe -- go ahead and process it */ 106 expand(*mvp, buf, &buf[sizeof buf - 1]); 107 *pvp++ = newstr(buf); 108 if (pvp >= &pv[MAXPV - 3]) 109 { 110 syserr("Too many parameters to %s before $u", pv[0]); 111 return (-1); 112 } 113 } 114 if (*mvp == NULL) 115 syserr("No $u in mailer argv for %s", pv[0]); 116 117 /* 118 ** At this point *mvp points to the argument with $u. We 119 ** run through our address list and append all the addresses 120 ** we can. If we run out of space, do not fret! We can 121 ** always send another copy later. 122 */ 123 124 tobuf[0] = '\0'; 125 firstone = TRUE; 126 To = tobuf; 127 for (; to != NULL; to = to->q_next) 128 { 129 /* avoid sending multiple recipients to dumb mailers */ 130 if (!firstone && !bitset(M_MUSER, m->m_flags)) 131 break; 132 133 /* if already sent or not for this host, don't send */ 134 if (bitset(QDONTSEND, to->q_flags) || strcmp(to->q_host, host) != 0) 135 continue; 136 user = to->q_user; 137 To = to->q_paddr; 138 to->q_flags |= QDONTSEND; 139 firstone = FALSE; 140 141 # ifdef DEBUG 142 if (Debug) 143 printf(" send to `%s'\n", user); 144 # endif DEBUG 145 146 /* 147 ** Check to see that these people are allowed to 148 ** talk to each other. 149 */ 150 151 if (!checkcompat(to)) 152 { 153 giveresponse(EX_UNAVAILABLE, TRUE, m); 154 continue; 155 } 156 157 /* 158 ** Remove quote bits from user/host. 159 */ 160 161 for (p = user; (*p++ &= 0177) != '\0'; ) 162 continue; 163 if (host != NULL) 164 for (p = host; (*p++ &= 0177) != '\0'; ) 165 continue; 166 167 /* 168 ** Strip quote bits from names if the mailer wants it. 169 */ 170 171 if (bitset(M_STRIPQ, m->m_flags)) 172 { 173 stripquotes(user); 174 stripquotes(host); 175 } 176 177 /* 178 ** See if this user name is "special". 179 ** If the user name has a slash in it, assume that this 180 ** is a file -- send it off without further ado. 181 ** Note that this means that editfcn's will not 182 ** be applied to the message. Also note that 183 ** this type of addresses is not processed along 184 ** with the others, so we fudge on the To person. 185 */ 186 187 if (m == Mailer[0]) 188 { 189 if (index(user, '/') != NULL) 190 { 191 i = mailfile(user); 192 giveresponse(i, TRUE, m); 193 continue; 194 } 195 } 196 197 /* 198 ** See if the user exists. 199 ** Strictly, this is only needed to print a pretty 200 ** error message. 201 ** 202 ** >>>>>>>>>> This clause assumes that the local mailer 203 ** >> NOTE >> cannot do any further aliasing; that 204 ** >>>>>>>>>> function is subsumed by sendmail. 205 */ 206 207 if (m == Mailer[0]) 208 { 209 if (getpwnam(user) == NULL) 210 { 211 giveresponse(EX_NOUSER, TRUE, m); 212 continue; 213 } 214 } 215 216 /* create list of users for error messages */ 217 if (tobuf[0] != '\0') 218 strcat(tobuf, ","); 219 strcat(tobuf, to->q_paddr); 220 define('u', user); /* to user */ 221 222 /* expand out this user */ 223 expand(user, buf, &buf[sizeof buf - 1]); 224 *pvp++ = newstr(buf); 225 if (pvp >= &pv[MAXPV - 2]) 226 { 227 /* allow some space for trailing parms */ 228 break; 229 } 230 } 231 232 /* print out messages as full list */ 233 To = tobuf; 234 235 /* 236 ** Fill out any parameters after the $u parameter. 237 */ 238 239 while (*++mvp != NULL) 240 { 241 expand(*mvp, buf, &buf[sizeof buf - 1]); 242 *pvp++ = newstr(buf); 243 if (pvp >= &pv[MAXPV]) 244 syserr("deliver: pv overflow after $u for %s", pv[0]); 245 } 246 *pvp++ = NULL; 247 248 /* 249 ** Call the mailer. 250 ** The argument vector gets built, pipes 251 ** are created as necessary, and we fork & exec as 252 ** appropriate. 253 */ 254 255 if (editfcn == NULL) 256 editfcn = putmessage; 257 i = sendoff(m, pv, editfcn); 258 259 return (i); 260 } 261 /* 262 ** SENDOFF -- send off call to mailer & collect response. 263 ** 264 ** Parameters: 265 ** m -- mailer descriptor. 266 ** pvp -- parameter vector to send to it. 267 ** editfcn -- function to pipe it through. 268 ** 269 ** Returns: 270 ** exit status of mailer. 271 ** 272 ** Side Effects: 273 ** none. 274 */ 275 276 sendoff(m, pvp, editfcn) 277 struct mailer *m; 278 char **pvp; 279 int (*editfcn)(); 280 { 281 auto int st; 282 register int i; 283 int pid; 284 int pvect[2]; 285 FILE *mfile; 286 extern putmessage(); 287 extern pipesig(); 288 extern FILE *fdopen(); 289 290 # ifdef DEBUG 291 if (Debug) 292 { 293 printf("Sendoff:\n"); 294 printav(pvp); 295 } 296 # endif DEBUG 297 298 rewind(stdin); 299 300 /* create a pipe to shove the mail through */ 301 if (pipe(pvect) < 0) 302 { 303 syserr("pipe"); 304 return (-1); 305 } 306 # ifdef VFORK 307 pid = vfork(); 308 # else 309 pid = fork(); 310 # endif 311 if (pid < 0) 312 { 313 syserr("Cannot fork"); 314 close(pvect[0]); 315 close(pvect[1]); 316 return (-1); 317 } 318 else if (pid == 0) 319 { 320 /* child -- set up input & exec mailer */ 321 /* make diagnostic output be standard output */ 322 close(2); 323 dup(1); 324 signal(SIGINT, SIG_IGN); 325 close(0); 326 if (dup(pvect[0]) < 0) 327 { 328 syserr("Cannot dup to zero!"); 329 _exit(EX_OSERR); 330 } 331 close(pvect[0]); 332 close(pvect[1]); 333 if (!bitset(M_RESTR, m->m_flags)) 334 setuid(getuid()); 335 # ifndef VFORK 336 /* 337 ** We have to be careful with vfork - we can't mung up the 338 ** memory but we don't want the mailer to inherit any extra 339 ** open files. Chances are the mailer won't 340 ** care about an extra file, but then again you never know. 341 ** Actually, we would like to close(fileno(pwf)), but it's 342 ** declared static so we can't. But if we fclose(pwf), which 343 ** is what endpwent does, it closes it in the parent too and 344 ** the next getpwnam will be slower. If you have a weird 345 ** mailer that chokes on the extra file you should do the 346 ** endpwent(). 347 ** 348 ** Similar comments apply to log. However, openlog is 349 ** clever enough to set the FIOCLEX mode on the file, 350 ** so it will be closed automatically on the exec. 351 */ 352 353 endpwent(); 354 # ifdef LOG 355 closelog(); 356 # endif LOG 357 # endif VFORK 358 execv(m->m_mailer, pvp); 359 /* syserr fails because log is closed */ 360 /* syserr("Cannot exec %s", m->m_mailer); */ 361 printf("Cannot exec %s\n", m->m_mailer); 362 fflush(stdout); 363 _exit(EX_UNAVAILABLE); 364 } 365 366 /* write out message to mailer */ 367 close(pvect[0]); 368 signal(SIGPIPE, pipesig); 369 mfile = fdopen(pvect[1], "w"); 370 if (editfcn == NULL) 371 editfcn = putmessage; 372 (*editfcn)(mfile, m); 373 fclose(mfile); 374 375 /* 376 ** Wait for child to die and report status. 377 ** We should never get fatal errors (e.g., segmentation 378 ** violation), so we report those specially. For other 379 ** errors, we choose a status message (into statmsg), 380 ** and if it represents an error, we print it. 381 */ 382 383 while ((i = wait(&st)) > 0 && i != pid) 384 continue; 385 if (i < 0) 386 { 387 syserr("wait"); 388 return (-1); 389 } 390 if ((st & 0377) != 0) 391 { 392 syserr("%s: stat %o", pvp[0], st); 393 ExitStat = EX_UNAVAILABLE; 394 return (-1); 395 } 396 i = (st >> 8) & 0377; 397 giveresponse(i, TRUE, m); 398 return (i); 399 } 400 /* 401 ** GIVERESPONSE -- Interpret an error response from a mailer 402 ** 403 ** Parameters: 404 ** stat -- the status code from the mailer (high byte 405 ** only; core dumps must have been taken care of 406 ** already). 407 ** force -- if set, force an error message output, even 408 ** if the mailer seems to like to print its own 409 ** messages. 410 ** m -- the mailer descriptor for this mailer. 411 ** 412 ** Returns: 413 ** stat. 414 ** 415 ** Side Effects: 416 ** Errors may be incremented. 417 ** ExitStat may be set. 418 ** 419 ** Called By: 420 ** deliver 421 */ 422 423 giveresponse(stat, force, m) 424 int stat; 425 int force; 426 register struct mailer *m; 427 { 428 register char *statmsg; 429 extern char *SysExMsg[]; 430 register int i; 431 extern int N_SysEx; 432 extern long MsgSize; 433 char buf[30]; 434 extern char *sprintf(); 435 436 i = stat - EX__BASE; 437 if (i < 0 || i > N_SysEx) 438 statmsg = NULL; 439 else 440 statmsg = SysExMsg[i]; 441 if (stat == 0) 442 statmsg = "ok"; 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)) 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 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 return (stat); 481 } 482 /* 483 ** PUTMESSAGE -- output a message to the final mailer. 484 ** 485 ** This routine takes care of recreating the header from the 486 ** in-core copy, etc. 487 ** 488 ** Parameters: 489 ** fp -- file to output onto. 490 ** m -- a mailer descriptor. 491 ** 492 ** Returns: 493 ** none. 494 ** 495 ** Side Effects: 496 ** The message is written onto fp. 497 */ 498 499 putmessage(fp, m) 500 FILE *fp; 501 struct mailer *m; 502 { 503 char buf[BUFSIZ]; 504 register int i; 505 HDR *h; 506 register char *p; 507 extern char *arpadate(); 508 bool anyheader = FALSE; 509 extern char *expand(); 510 extern char *capitalize(); 511 512 /* output "From" line unless supressed */ 513 if (!bitset(M_NHDR, m->m_flags)) 514 fprintf(fp, "%s\n", FromLine); 515 516 /* output all header lines */ 517 for (h = Header; h != NULL; h = h->h_link) 518 { 519 if (bitset(H_DELETE, h->h_flags)) 520 continue; 521 if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 522 continue; 523 if (bitset(H_DEFAULT, h->h_flags)) 524 { 525 expand(h->h_value, buf, &buf[sizeof buf]); 526 p = buf; 527 } 528 else 529 p = h->h_value; 530 if (*p == '\0') 531 continue; 532 fprintf(fp, "%s: %s\n", capitalize(h->h_field), p); 533 h->h_flags |= H_USED; 534 anyheader = TRUE; 535 } 536 537 if (anyheader) 538 fprintf(fp, "\n"); 539 540 /* output the body of the message */ 541 while (!ferror(fp) && (i = read(0, buf, BUFSIZ)) > 0) 542 fwrite(buf, 1, i, fp); 543 544 if (ferror(fp)) 545 { 546 syserr("putmessage: write error"); 547 setstat(EX_IOERR); 548 } 549 } 550 /* 551 ** PIPESIG -- Handle broken pipe signals 552 ** 553 ** This just logs an error. 554 ** 555 ** Parameters: 556 ** none 557 ** 558 ** Returns: 559 ** none 560 ** 561 ** Side Effects: 562 ** logs an error message. 563 */ 564 565 pipesig() 566 { 567 syserr("Broken pipe"); 568 signal(SIGPIPE, SIG_IGN); 569 } 570 /* 571 ** SENDTO -- Designate a send list. 572 ** 573 ** The parameter is a comma-separated list of people to send to. 574 ** This routine arranges to send to all of them. 575 ** 576 ** Parameters: 577 ** list -- the send list. 578 ** copyf -- the copy flag; passed to parse. 579 ** 580 ** Returns: 581 ** none 582 ** 583 ** Side Effects: 584 ** none. 585 */ 586 587 sendto(list, copyf) 588 char *list; 589 int copyf; 590 { 591 register char *p; 592 register char *q; 593 register char c; 594 ADDRESS *a; 595 extern ADDRESS *parse(); 596 bool more; 597 598 /* more keeps track of what the previous delimiter was */ 599 more = TRUE; 600 for (p = list; more; ) 601 { 602 /* find the end of this address */ 603 q = p; 604 while ((c = *p++) != '\0' && c != ',' && c != '\n') 605 continue; 606 more = c != '\0'; 607 *--p = '\0'; 608 if (more) 609 p++; 610 611 /* parse the address */ 612 if ((a = parse(q, (ADDRESS *) NULL, copyf)) == NULL) 613 continue; 614 615 /* arrange to send to this person */ 616 recipient(a); 617 } 618 To = NULL; 619 } 620 /* 621 ** RECIPIENT -- Designate a message recipient 622 ** 623 ** Saves the named person for future mailing. 624 ** 625 ** Parameters: 626 ** a -- the (preparsed) address header for the recipient. 627 ** 628 ** Returns: 629 ** none. 630 ** 631 ** Side Effects: 632 ** none. 633 */ 634 635 recipient(a) 636 register ADDRESS *a; 637 { 638 register ADDRESS *q; 639 register struct mailer *m; 640 extern bool forward(); 641 extern int errno; 642 extern bool sameaddr(); 643 644 To = a->q_paddr; 645 m = Mailer[a->q_mailer]; 646 errno = 0; 647 # ifdef DEBUG 648 if (Debug) 649 printf("recipient(%s)\n", To); 650 # endif DEBUG 651 652 /* 653 ** Do sickly crude mapping for program mailing, etc. 654 */ 655 656 if (a->q_mailer == 0 && a->q_user[0] == '|') 657 { 658 a->q_mailer = 1; 659 m = Mailer[1]; 660 a->q_user++; 661 } 662 663 /* 664 ** Look up this person in the recipient list. If they 665 ** are there already, return, otherwise continue. 666 ** If the list is empty, just add it. 667 */ 668 669 if (m->m_sendq == NULL) 670 { 671 m->m_sendq = a; 672 } 673 else 674 { 675 ADDRESS *pq; 676 677 for (q = m->m_sendq; q != NULL; pq = q, q = q->q_next) 678 { 679 if (!ForceMail && sameaddr(q, a, FALSE)) 680 { 681 # ifdef DEBUG 682 if (Debug) 683 printf("(%s in sendq)\n", a->q_paddr); 684 # endif DEBUG 685 return; 686 } 687 } 688 689 /* add address on list */ 690 q = pq; 691 q->q_next = a; 692 } 693 a->q_next = NULL; 694 695 /* 696 ** See if the user wants hir mail forwarded. 697 ** `Forward' must do the forwarding recursively. 698 */ 699 700 if (m == Mailer[0] && !NoAlias && forward(a)) 701 setbit(QDONTSEND, a->q_flags); 702 703 return; 704 } 705 /* 706 ** MAILFILE -- Send a message to a file. 707 ** 708 ** Parameters: 709 ** filename -- the name of the file to send to. 710 ** 711 ** Returns: 712 ** The exit code associated with the operation. 713 ** 714 ** Side Effects: 715 ** none. 716 ** 717 ** Called By: 718 ** deliver 719 */ 720 721 mailfile(filename) 722 char *filename; 723 { 724 register FILE *f; 725 726 f = fopen(filename, "a"); 727 if (f == NULL) 728 return (EX_CANTCREAT); 729 730 putmessage(f, Mailer[1]); 731 fputs("\n", f); 732 fclose(f); 733 return (EX_OK); 734 } 735