1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 # include "sendmail.h" 10 11 #ifndef lint 12 #ifdef QUEUE 13 static char sccsid[] = "@(#)queue.c 8.31 (Berkeley) 12/16/93 (with queueing)"; 14 #else 15 static char sccsid[] = "@(#)queue.c 8.31 (Berkeley) 12/16/93 (without queueing)"; 16 #endif 17 #endif /* not lint */ 18 19 # include <errno.h> 20 # include <pwd.h> 21 # include <dirent.h> 22 23 # ifdef QUEUE 24 25 /* 26 ** Work queue. 27 */ 28 29 struct work 30 { 31 char *w_name; /* name of control file */ 32 long w_pri; /* priority of message, see below */ 33 time_t w_ctime; /* creation time of message */ 34 struct work *w_next; /* next in queue */ 35 }; 36 37 typedef struct work WORK; 38 39 WORK *WorkQ; /* queue of things to be done */ 40 /* 41 ** QUEUEUP -- queue a message up for future transmission. 42 ** 43 ** Parameters: 44 ** e -- the envelope to queue up. 45 ** queueall -- if TRUE, queue all addresses, rather than 46 ** just those with the QQUEUEUP flag set. 47 ** announce -- if TRUE, tell when you are queueing up. 48 ** 49 ** Returns: 50 ** none. 51 ** 52 ** Side Effects: 53 ** The current request are saved in a control file. 54 ** The queue file is left locked. 55 */ 56 57 queueup(e, queueall, announce) 58 register ENVELOPE *e; 59 bool queueall; 60 bool announce; 61 { 62 char *qf; 63 register FILE *tfp; 64 register HDR *h; 65 register ADDRESS *q; 66 int fd; 67 int i; 68 bool newid; 69 register char *p; 70 MAILER nullmailer; 71 char buf[MAXLINE], tf[MAXLINE]; 72 73 /* 74 ** Create control file. 75 */ 76 77 newid = (e->e_id == NULL) || !bitset(EF_INQUEUE, e->e_flags); 78 79 /* if newid, queuename will create a locked qf file in e->lockfp */ 80 strcpy(tf, queuename(e, 't')); 81 tfp = e->e_lockfp; 82 if (tfp == NULL) 83 newid = FALSE; 84 85 /* if newid, just write the qf file directly (instead of tf file) */ 86 if (!newid) 87 { 88 /* get a locked tf file */ 89 for (i = 0; i < 128; i++) 90 { 91 fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode); 92 if (fd < 0) 93 { 94 if (errno != EEXIST) 95 break; 96 #ifdef LOG 97 if (LogLevel > 0 && (i % 32) == 0) 98 syslog(LOG_ALERT, "queueup: cannot create %s, uid=%d: %s", 99 tf, geteuid(), errstring(errno)); 100 #endif 101 } 102 else 103 { 104 if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB)) 105 break; 106 #ifdef LOG 107 else if (LogLevel > 0 && (i % 32) == 0) 108 syslog(LOG_ALERT, "queueup: cannot lock %s: %s", 109 tf, errstring(errno)); 110 #endif 111 close(fd); 112 } 113 114 if ((i % 32) == 31) 115 { 116 /* save the old temp file away */ 117 (void) rename(tf, queuename(e, 'T')); 118 } 119 else 120 sleep(i % 32); 121 } 122 if (fd < 0 || (tfp = fdopen(fd, "w")) == NULL) 123 { 124 printopenfds(TRUE); 125 syserr("!queueup: cannot create queue temp file %s, uid=%d", 126 tf, geteuid()); 127 } 128 } 129 130 if (tTd(40, 1)) 131 printf("\n>>>>> queueing %s%s >>>>>\n", e->e_id, 132 newid ? " (new id)" : ""); 133 if (tTd(40, 9)) 134 { 135 printf(" tfp="); 136 dumpfd(fileno(tfp), TRUE, FALSE); 137 printf(" lockfp="); 138 if (e->e_lockfp == NULL) 139 printf("NULL\n"); 140 else 141 dumpfd(fileno(e->e_lockfp), TRUE, FALSE); 142 } 143 144 /* 145 ** If there is no data file yet, create one. 146 */ 147 148 if (e->e_df == NULL) 149 { 150 register FILE *dfp; 151 extern putbody(); 152 153 e->e_df = queuename(e, 'd'); 154 e->e_df = newstr(e->e_df); 155 fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode); 156 if (fd < 0 || (dfp = fdopen(fd, "w")) == NULL) 157 syserr("!queueup: cannot create data temp file %s, uid=%d", 158 e->e_df, geteuid()); 159 (*e->e_putbody)(dfp, FileMailer, e, NULL); 160 (void) xfclose(dfp, "queueup dfp", e->e_id); 161 e->e_putbody = putbody; 162 } 163 164 /* 165 ** Output future work requests. 166 ** Priority and creation time should be first, since 167 ** they are required by orderq. 168 */ 169 170 /* output message priority */ 171 fprintf(tfp, "P%ld\n", e->e_msgpriority); 172 173 /* output creation time */ 174 fprintf(tfp, "T%ld\n", e->e_ctime); 175 176 /* output type and name of data file */ 177 if (e->e_bodytype != NULL) 178 fprintf(tfp, "B%s\n", e->e_bodytype); 179 fprintf(tfp, "D%s\n", e->e_df); 180 181 /* message from envelope, if it exists */ 182 if (e->e_message != NULL) 183 fprintf(tfp, "M%s\n", e->e_message); 184 185 /* send various flag bits through */ 186 p = buf; 187 if (bitset(EF_WARNING, e->e_flags)) 188 *p++ = 'w'; 189 if (bitset(EF_RESPONSE, e->e_flags)) 190 *p++ = 'r'; 191 *p++ = '\0'; 192 if (buf[0] != '\0') 193 fprintf(tfp, "F%s\n", buf); 194 195 /* $r and $s and $_ macro values */ 196 if ((p = macvalue('r', e)) != NULL) 197 fprintf(tfp, "$r%s\n", p); 198 if ((p = macvalue('s', e)) != NULL) 199 fprintf(tfp, "$s%s\n", p); 200 if ((p = macvalue('_', e)) != NULL) 201 fprintf(tfp, "$_%s\n", p); 202 203 /* output name of sender */ 204 fprintf(tfp, "S%s\n", e->e_from.q_paddr); 205 206 /* output list of error recipients */ 207 printctladdr(NULL, NULL); 208 for (q = e->e_errorqueue; q != NULL; q = q->q_next) 209 { 210 if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 211 { 212 printctladdr(q, tfp); 213 fprintf(tfp, "E%s\n", q->q_paddr); 214 } 215 } 216 217 /* output list of recipient addresses */ 218 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 219 { 220 if (bitset(QQUEUEUP, q->q_flags) || 221 (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags))) 222 { 223 printctladdr(q, tfp); 224 fprintf(tfp, "R%s\n", q->q_paddr); 225 if (announce) 226 { 227 e->e_to = q->q_paddr; 228 message("queued"); 229 if (LogLevel > 8) 230 logdelivery(NULL, NULL, "queued", NULL, e); 231 e->e_to = NULL; 232 } 233 if (tTd(40, 1)) 234 { 235 printf("queueing "); 236 printaddr(q, FALSE); 237 } 238 } 239 } 240 241 /* 242 ** Output headers for this message. 243 ** Expand macros completely here. Queue run will deal with 244 ** everything as absolute headers. 245 ** All headers that must be relative to the recipient 246 ** can be cracked later. 247 ** We set up a "null mailer" -- i.e., a mailer that will have 248 ** no effect on the addresses as they are output. 249 */ 250 251 bzero((char *) &nullmailer, sizeof nullmailer); 252 nullmailer.m_re_rwset = nullmailer.m_rh_rwset = 253 nullmailer.m_se_rwset = nullmailer.m_sh_rwset = 0; 254 nullmailer.m_eol = "\n"; 255 256 define('g', "\201f", e); 257 for (h = e->e_header; h != NULL; h = h->h_link) 258 { 259 extern bool bitzerop(); 260 261 /* don't output null headers */ 262 if (h->h_value == NULL || h->h_value[0] == '\0') 263 continue; 264 265 /* don't output resent headers on non-resent messages */ 266 if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 267 continue; 268 269 /* output this header */ 270 fprintf(tfp, "H"); 271 272 /* if conditional, output the set of conditions */ 273 if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 274 { 275 int j; 276 277 (void) putc('?', tfp); 278 for (j = '\0'; j <= '\177'; j++) 279 if (bitnset(j, h->h_mflags)) 280 (void) putc(j, tfp); 281 (void) putc('?', tfp); 282 } 283 284 /* output the header: expand macros, convert addresses */ 285 if (bitset(H_DEFAULT, h->h_flags)) 286 { 287 (void) expand(h->h_value, buf, &buf[sizeof buf], e); 288 fprintf(tfp, "%s: %s\n", h->h_field, buf); 289 } 290 else if (bitset(H_FROM|H_RCPT, h->h_flags)) 291 { 292 bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 293 FILE *savetrace = TrafficLogFile; 294 295 TrafficLogFile = NULL; 296 297 if (bitset(H_FROM, h->h_flags)) 298 oldstyle = FALSE; 299 300 commaize(h, h->h_value, tfp, oldstyle, 301 &nullmailer, e); 302 303 TrafficLogFile = savetrace; 304 } 305 else 306 fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 307 } 308 309 /* 310 ** Clean up. 311 */ 312 313 if (fflush(tfp) < 0 || fsync(fileno(tfp)) < 0 || ferror(tfp)) 314 { 315 if (newid) 316 syserr("!552 Error writing control file %s", tf); 317 else 318 syserr("!452 Error writing control file %s", tf); 319 } 320 321 if (!newid) 322 { 323 /* rename (locked) tf to be (locked) qf */ 324 qf = queuename(e, 'q'); 325 if (rename(tf, qf) < 0) 326 syserr("cannot rename(%s, %s), df=%s, uid=%d", 327 tf, qf, e->e_df, geteuid()); 328 329 /* close and unlock old (locked) qf */ 330 if (e->e_lockfp != NULL) 331 (void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id); 332 e->e_lockfp = tfp; 333 } 334 else 335 qf = tf; 336 errno = 0; 337 e->e_flags |= EF_INQUEUE; 338 339 # ifdef LOG 340 /* save log info */ 341 if (LogLevel > 79) 342 syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 343 # endif /* LOG */ 344 345 if (tTd(40, 1)) 346 printf("<<<<< done queueing %s <<<<<\n\n", e->e_id); 347 return; 348 } 349 350 printctladdr(a, tfp) 351 register ADDRESS *a; 352 FILE *tfp; 353 { 354 char *uname; 355 register struct passwd *pw; 356 register ADDRESS *q; 357 uid_t uid; 358 static ADDRESS *lastctladdr; 359 static uid_t lastuid; 360 361 /* initialization */ 362 if (a == NULL || a->q_alias == NULL || tfp == NULL) 363 { 364 if (lastctladdr != NULL && tfp != NULL) 365 fprintf(tfp, "C\n"); 366 lastctladdr = NULL; 367 lastuid = 0; 368 return; 369 } 370 371 /* find the active uid */ 372 q = getctladdr(a); 373 if (q == NULL) 374 uid = 0; 375 else 376 uid = q->q_uid; 377 a = a->q_alias; 378 379 /* check to see if this is the same as last time */ 380 if (lastctladdr != NULL && uid == lastuid && 381 strcmp(lastctladdr->q_paddr, a->q_paddr) == 0) 382 return; 383 lastuid = uid; 384 lastctladdr = a; 385 386 if (uid == 0 || (pw = getpwuid(uid)) == NULL) 387 uname = ""; 388 else 389 uname = pw->pw_name; 390 391 fprintf(tfp, "C%s:%s\n", uname, a->q_paddr); 392 } 393 394 /* 395 ** RUNQUEUE -- run the jobs in the queue. 396 ** 397 ** Gets the stuff out of the queue in some presumably logical 398 ** order and processes them. 399 ** 400 ** Parameters: 401 ** forkflag -- TRUE if the queue scanning should be done in 402 ** a child process. We double-fork so it is not our 403 ** child and we don't have to clean up after it. 404 ** 405 ** Returns: 406 ** none. 407 ** 408 ** Side Effects: 409 ** runs things in the mail queue. 410 */ 411 412 ENVELOPE QueueEnvelope; /* the queue run envelope */ 413 414 runqueue(forkflag) 415 bool forkflag; 416 { 417 register ENVELOPE *e; 418 extern ENVELOPE BlankEnvelope; 419 420 /* 421 ** If no work will ever be selected, don't even bother reading 422 ** the queue. 423 */ 424 425 CurrentLA = getla(); /* get load average */ 426 427 if (shouldqueue(0L, curtime())) 428 { 429 if (Verbose) 430 printf("Skipping queue run -- load average too high\n"); 431 if (forkflag && QueueIntvl != 0) 432 (void) setevent(QueueIntvl, runqueue, TRUE); 433 return; 434 } 435 436 /* 437 ** See if we want to go off and do other useful work. 438 */ 439 440 if (forkflag) 441 { 442 int pid; 443 444 pid = dofork(); 445 if (pid != 0) 446 { 447 extern void reapchild(); 448 449 /* parent -- pick up intermediate zombie */ 450 #ifndef SIGCHLD 451 (void) waitfor(pid); 452 #else /* SIGCHLD */ 453 (void) setsignal(SIGCHLD, reapchild); 454 #endif /* SIGCHLD */ 455 if (QueueIntvl != 0) 456 (void) setevent(QueueIntvl, runqueue, TRUE); 457 return; 458 } 459 /* child -- double fork */ 460 #ifndef SIGCHLD 461 if (fork() != 0) 462 exit(EX_OK); 463 #else /* SIGCHLD */ 464 (void) setsignal(SIGCHLD, SIG_DFL); 465 #endif /* SIGCHLD */ 466 } 467 468 setproctitle("running queue: %s", QueueDir); 469 470 # ifdef LOG 471 if (LogLevel > 69) 472 syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 473 QueueDir, getpid(), forkflag); 474 # endif /* LOG */ 475 476 /* 477 ** Release any resources used by the daemon code. 478 */ 479 480 # ifdef DAEMON 481 clrdaemon(); 482 # endif /* DAEMON */ 483 484 /* force it to run expensive jobs */ 485 NoConnect = FALSE; 486 487 /* 488 ** Create ourselves an envelope 489 */ 490 491 CurEnv = &QueueEnvelope; 492 e = newenvelope(&QueueEnvelope, CurEnv); 493 e->e_flags = BlankEnvelope.e_flags; 494 495 /* 496 ** Make sure the alias database is open. 497 */ 498 499 initmaps(FALSE, e); 500 501 /* 502 ** Start making passes through the queue. 503 ** First, read and sort the entire queue. 504 ** Then, process the work in that order. 505 ** But if you take too long, start over. 506 */ 507 508 /* order the existing work requests */ 509 (void) orderq(FALSE); 510 511 /* process them once at a time */ 512 while (WorkQ != NULL) 513 { 514 WORK *w = WorkQ; 515 516 WorkQ = WorkQ->w_next; 517 518 /* 519 ** Ignore jobs that are too expensive for the moment. 520 */ 521 522 if (shouldqueue(w->w_pri, w->w_ctime)) 523 { 524 if (Verbose) 525 printf("\nSkipping %s\n", w->w_name + 2); 526 } 527 else 528 { 529 pid_t pid; 530 extern pid_t dowork(); 531 532 pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e); 533 errno = 0; 534 (void) waitfor(pid); 535 } 536 free(w->w_name); 537 free((char *) w); 538 } 539 540 /* exit without the usual cleanup */ 541 e->e_id = NULL; 542 finis(); 543 } 544 /* 545 ** ORDERQ -- order the work queue. 546 ** 547 ** Parameters: 548 ** doall -- if set, include everything in the queue (even 549 ** the jobs that cannot be run because the load 550 ** average is too high). Otherwise, exclude those 551 ** jobs. 552 ** 553 ** Returns: 554 ** The number of request in the queue (not necessarily 555 ** the number of requests in WorkQ however). 556 ** 557 ** Side Effects: 558 ** Sets WorkQ to the queue of available work, in order. 559 */ 560 561 # define NEED_P 001 562 # define NEED_T 002 563 # define NEED_R 004 564 # define NEED_S 010 565 566 orderq(doall) 567 bool doall; 568 { 569 register struct dirent *d; 570 register WORK *w; 571 DIR *f; 572 register int i; 573 WORK wlist[QUEUESIZE+1]; 574 int wn = -1; 575 extern workcmpf(); 576 577 if (tTd(41, 1)) 578 { 579 printf("orderq:\n"); 580 if (QueueLimitId != NULL) 581 printf("\tQueueLimitId = %s\n", QueueLimitId); 582 if (QueueLimitSender != NULL) 583 printf("\tQueueLimitSender = %s\n", QueueLimitSender); 584 if (QueueLimitRecipient != NULL) 585 printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 586 } 587 588 /* clear out old WorkQ */ 589 for (w = WorkQ; w != NULL; ) 590 { 591 register WORK *nw = w->w_next; 592 593 WorkQ = nw; 594 free(w->w_name); 595 free((char *) w); 596 w = nw; 597 } 598 599 /* open the queue directory */ 600 f = opendir("."); 601 if (f == NULL) 602 { 603 syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 604 return (0); 605 } 606 607 /* 608 ** Read the work directory. 609 */ 610 611 while ((d = readdir(f)) != NULL) 612 { 613 FILE *cf; 614 register char *p; 615 char lbuf[MAXNAME]; 616 extern bool strcontainedin(); 617 618 /* is this an interesting entry? */ 619 if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 620 continue; 621 622 if (QueueLimitId != NULL && 623 !strcontainedin(QueueLimitId, d->d_name)) 624 continue; 625 626 /* 627 ** Check queue name for plausibility. This handles 628 ** both old and new type ids. 629 */ 630 631 p = d->d_name + 2; 632 if (isupper(p[0]) && isupper(p[2])) 633 p += 3; 634 else if (isupper(p[1])) 635 p += 2; 636 else 637 p = d->d_name; 638 for (i = 0; isdigit(*p); p++) 639 i++; 640 if (i < 5 || *p != '\0') 641 { 642 if (Verbose) 643 printf("orderq: bogus qf name %s\n", d->d_name); 644 #ifdef LOG 645 if (LogLevel > 3) 646 syslog(LOG_CRIT, "orderq: bogus qf name %s", 647 d->d_name); 648 #endif 649 if (strlen(d->d_name) >= MAXNAME) 650 d->d_name[MAXNAME - 1] = '\0'; 651 strcpy(lbuf, d->d_name); 652 lbuf[0] = 'Q'; 653 (void) rename(d->d_name, lbuf); 654 continue; 655 } 656 657 /* yes -- open control file (if not too many files) */ 658 if (++wn >= QUEUESIZE) 659 continue; 660 661 cf = fopen(d->d_name, "r"); 662 if (cf == NULL) 663 { 664 /* this may be some random person sending hir msgs */ 665 /* syserr("orderq: cannot open %s", cbuf); */ 666 if (tTd(41, 2)) 667 printf("orderq: cannot open %s (%d)\n", 668 d->d_name, errno); 669 errno = 0; 670 wn--; 671 continue; 672 } 673 w = &wlist[wn]; 674 w->w_name = newstr(d->d_name); 675 676 /* make sure jobs in creation don't clog queue */ 677 w->w_pri = 0x7fffffff; 678 w->w_ctime = 0; 679 680 /* extract useful information */ 681 i = NEED_P | NEED_T; 682 if (QueueLimitSender != NULL) 683 i |= NEED_S; 684 if (QueueLimitRecipient != NULL) 685 i |= NEED_R; 686 while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 687 { 688 extern long atol(); 689 extern bool strcontainedin(); 690 691 switch (lbuf[0]) 692 { 693 case 'P': 694 w->w_pri = atol(&lbuf[1]); 695 i &= ~NEED_P; 696 break; 697 698 case 'T': 699 w->w_ctime = atol(&lbuf[1]); 700 i &= ~NEED_T; 701 break; 702 703 case 'R': 704 if (QueueLimitRecipient != NULL && 705 strcontainedin(QueueLimitRecipient, &lbuf[1])) 706 i &= ~NEED_R; 707 break; 708 709 case 'S': 710 if (QueueLimitSender != NULL && 711 strcontainedin(QueueLimitSender, &lbuf[1])) 712 i &= ~NEED_S; 713 break; 714 } 715 } 716 (void) fclose(cf); 717 718 if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 719 bitset(NEED_R|NEED_S, i)) 720 { 721 /* don't even bother sorting this job in */ 722 wn--; 723 } 724 } 725 (void) closedir(f); 726 wn++; 727 728 /* 729 ** Sort the work directory. 730 */ 731 732 qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 733 734 /* 735 ** Convert the work list into canonical form. 736 ** Should be turning it into a list of envelopes here perhaps. 737 */ 738 739 WorkQ = NULL; 740 for (i = min(wn, QUEUESIZE); --i >= 0; ) 741 { 742 w = (WORK *) xalloc(sizeof *w); 743 w->w_name = wlist[i].w_name; 744 w->w_pri = wlist[i].w_pri; 745 w->w_ctime = wlist[i].w_ctime; 746 w->w_next = WorkQ; 747 WorkQ = w; 748 } 749 750 if (tTd(40, 1)) 751 { 752 for (w = WorkQ; w != NULL; w = w->w_next) 753 printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 754 } 755 756 return (wn); 757 } 758 /* 759 ** WORKCMPF -- compare function for ordering work. 760 ** 761 ** Parameters: 762 ** a -- the first argument. 763 ** b -- the second argument. 764 ** 765 ** Returns: 766 ** -1 if a < b 767 ** 0 if a == b 768 ** +1 if a > b 769 ** 770 ** Side Effects: 771 ** none. 772 */ 773 774 workcmpf(a, b) 775 register WORK *a; 776 register WORK *b; 777 { 778 long pa = a->w_pri; 779 long pb = b->w_pri; 780 781 if (pa == pb) 782 return (0); 783 else if (pa > pb) 784 return (1); 785 else 786 return (-1); 787 } 788 /* 789 ** DOWORK -- do a work request. 790 ** 791 ** Parameters: 792 ** id -- the ID of the job to run. 793 ** forkflag -- if set, run this in background. 794 ** requeueflag -- if set, reinstantiate the queue quickly. 795 ** This is used when expanding aliases in the queue. 796 ** If forkflag is also set, it doesn't wait for the 797 ** child. 798 ** e - the envelope in which to run it. 799 ** 800 ** Returns: 801 ** process id of process that is running the queue job. 802 ** 803 ** Side Effects: 804 ** The work request is satisfied if possible. 805 */ 806 807 pid_t 808 dowork(id, forkflag, requeueflag, e) 809 char *id; 810 bool forkflag; 811 bool requeueflag; 812 register ENVELOPE *e; 813 { 814 register pid_t pid; 815 extern bool readqf(); 816 817 if (tTd(40, 1)) 818 printf("dowork(%s)\n", id); 819 820 /* 821 ** Fork for work. 822 */ 823 824 if (forkflag) 825 { 826 pid = fork(); 827 if (pid < 0) 828 { 829 syserr("dowork: cannot fork"); 830 return 0; 831 } 832 else if (pid > 0) 833 { 834 /* parent -- clean out connection cache */ 835 mci_flush(FALSE, NULL); 836 } 837 } 838 else 839 { 840 pid = 0; 841 } 842 843 if (pid == 0) 844 { 845 /* 846 ** CHILD 847 ** Lock the control file to avoid duplicate deliveries. 848 ** Then run the file as though we had just read it. 849 ** We save an idea of the temporary name so we 850 ** can recover on interrupt. 851 */ 852 853 /* set basic modes, etc. */ 854 (void) alarm(0); 855 clearenvelope(e, FALSE); 856 e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS; 857 e->e_errormode = EM_MAIL; 858 e->e_id = id; 859 GrabTo = UseErrorsTo = FALSE; 860 if (forkflag) 861 { 862 disconnect(1, e); 863 OpMode = MD_DELIVER; 864 } 865 # ifdef LOG 866 if (LogLevel > 76) 867 syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 868 getpid()); 869 # endif /* LOG */ 870 871 /* don't use the headers from sendmail.cf... */ 872 e->e_header = NULL; 873 874 /* read the queue control file -- return if locked */ 875 if (!readqf(e, !requeueflag)) 876 { 877 if (tTd(40, 4)) 878 printf("readqf(%s) failed\n", e->e_id); 879 if (forkflag) 880 exit(EX_OK); 881 else 882 return; 883 } 884 885 e->e_flags |= EF_INQUEUE; 886 eatheader(e, requeueflag); 887 888 if (requeueflag) 889 queueup(e, TRUE, FALSE); 890 891 /* do the delivery */ 892 sendall(e, SM_DELIVER); 893 894 /* finish up and exit */ 895 if (forkflag) 896 finis(); 897 else 898 dropenvelope(e); 899 } 900 e->e_id = NULL; 901 return pid; 902 } 903 /* 904 ** READQF -- read queue file and set up environment. 905 ** 906 ** Parameters: 907 ** e -- the envelope of the job to run. 908 ** announcefile -- if set, announce the name of the queue 909 ** file in error messages. 910 ** 911 ** Returns: 912 ** TRUE if it successfully read the queue file. 913 ** FALSE otherwise. 914 ** 915 ** Side Effects: 916 ** The queue file is returned locked. 917 */ 918 919 bool 920 readqf(e, announcefile) 921 register ENVELOPE *e; 922 bool announcefile; 923 { 924 register FILE *qfp; 925 ADDRESS *ctladdr; 926 struct stat st; 927 char *bp; 928 char qf[20]; 929 char buf[MAXLINE]; 930 extern long atol(); 931 extern ADDRESS *setctluser(); 932 933 /* 934 ** Read and process the file. 935 */ 936 937 strcpy(qf, queuename(e, 'q')); 938 qfp = fopen(qf, "r+"); 939 if (qfp == NULL) 940 { 941 if (tTd(40, 8)) 942 printf("readqf(%s): fopen failure (%s)\n", 943 qf, errstring(errno)); 944 if (errno != ENOENT) 945 syserr("readqf: no control file %s", qf); 946 return FALSE; 947 } 948 949 if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB)) 950 { 951 /* being processed by another queuer */ 952 if (tTd(40, 8)) 953 printf("readqf(%s): locked\n", qf); 954 if (Verbose) 955 printf("%s: locked\n", e->e_id); 956 # ifdef LOG 957 if (LogLevel > 19) 958 syslog(LOG_DEBUG, "%s: locked", e->e_id); 959 # endif /* LOG */ 960 (void) fclose(qfp); 961 return FALSE; 962 } 963 964 /* 965 ** Check the queue file for plausibility to avoid attacks. 966 */ 967 968 if (fstat(fileno(qfp), &st) < 0) 969 { 970 /* must have been being processed by someone else */ 971 if (tTd(40, 8)) 972 printf("readqf(%s): fstat failure (%s)\n", 973 qf, errstring(errno)); 974 fclose(qfp); 975 return FALSE; 976 } 977 978 if (st.st_uid != geteuid()) 979 { 980 # ifdef LOG 981 if (LogLevel > 0) 982 { 983 syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 984 e->e_id, st.st_uid, st.st_mode); 985 } 986 # endif /* LOG */ 987 if (tTd(40, 8)) 988 printf("readqf(%s): bogus file\n", qf); 989 rename(qf, queuename(e, 'Q')); 990 fclose(qfp); 991 return FALSE; 992 } 993 994 if (st.st_size == 0) 995 { 996 /* must be a bogus file -- just remove it */ 997 (void) unlink(qf); 998 fclose(qfp); 999 return FALSE; 1000 } 1001 1002 if (st.st_nlink == 0) 1003 { 1004 /* 1005 ** Race condition -- we got a file just as it was being 1006 ** unlinked. Just assume it is zero length. 1007 */ 1008 1009 fclose(qfp); 1010 return FALSE; 1011 } 1012 1013 /* good file -- save this lock */ 1014 e->e_lockfp = qfp; 1015 1016 /* do basic system initialization */ 1017 initsys(e); 1018 define('i', e->e_id, e); 1019 1020 if (announcefile) 1021 FileName = qf; 1022 LineNumber = 0; 1023 e->e_flags |= EF_GLOBALERRS; 1024 OpMode = MD_DELIVER; 1025 if (Verbose) 1026 printf("\nRunning %s\n", e->e_id); 1027 ctladdr = NULL; 1028 while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 1029 { 1030 register char *p; 1031 struct stat st; 1032 1033 if (tTd(40, 4)) 1034 printf("+++++ %s\n", bp); 1035 switch (bp[0]) 1036 { 1037 case 'C': /* specify controlling user */ 1038 ctladdr = setctluser(&bp[1]); 1039 break; 1040 1041 case 'R': /* specify recipient */ 1042 (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 1043 break; 1044 1045 case 'E': /* specify error recipient */ 1046 (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 1047 break; 1048 1049 case 'H': /* header */ 1050 (void) chompheader(&bp[1], FALSE, e); 1051 break; 1052 1053 case 'M': /* message */ 1054 /* ignore this; we want a new message next time */ 1055 break; 1056 1057 case 'S': /* sender */ 1058 setsender(newstr(&bp[1]), e, NULL, TRUE); 1059 break; 1060 1061 case 'B': /* body type */ 1062 e->e_bodytype = newstr(&bp[1]); 1063 break; 1064 1065 case 'D': /* data file name */ 1066 e->e_df = newstr(&bp[1]); 1067 e->e_dfp = fopen(e->e_df, "r"); 1068 if (e->e_dfp == NULL) 1069 { 1070 syserr("readqf: cannot open %s", e->e_df); 1071 e->e_msgsize = -1; 1072 } 1073 else if (fstat(fileno(e->e_dfp), &st) >= 0) 1074 e->e_msgsize = st.st_size; 1075 break; 1076 1077 case 'T': /* init time */ 1078 e->e_ctime = atol(&bp[1]); 1079 break; 1080 1081 case 'P': /* message priority */ 1082 e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 1083 break; 1084 1085 case 'F': /* flag bits */ 1086 for (p = &bp[1]; *p != '\0'; p++) 1087 { 1088 switch (*p) 1089 { 1090 case 'w': /* warning sent */ 1091 e->e_flags |= EF_WARNING; 1092 break; 1093 1094 case 'r': /* response */ 1095 e->e_flags |= EF_RESPONSE; 1096 break; 1097 } 1098 } 1099 break; 1100 1101 case '$': /* define macro */ 1102 define(bp[1], newstr(&bp[2]), e); 1103 break; 1104 1105 case '\0': /* blank line; ignore */ 1106 break; 1107 1108 default: 1109 syserr("readqf: bad line \"%s\"", e->e_id, 1110 LineNumber, bp); 1111 fclose(qfp); 1112 rename(qf, queuename(e, 'Q')); 1113 return FALSE; 1114 } 1115 1116 if (bp != buf) 1117 free(bp); 1118 } 1119 1120 FileName = NULL; 1121 1122 /* 1123 ** If we haven't read any lines, this queue file is empty. 1124 ** Arrange to remove it without referencing any null pointers. 1125 */ 1126 1127 if (LineNumber == 0) 1128 { 1129 errno = 0; 1130 e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 1131 } 1132 return TRUE; 1133 } 1134 /* 1135 ** PRINTQUEUE -- print out a representation of the mail queue 1136 ** 1137 ** Parameters: 1138 ** none. 1139 ** 1140 ** Returns: 1141 ** none. 1142 ** 1143 ** Side Effects: 1144 ** Prints a listing of the mail queue on the standard output. 1145 */ 1146 1147 printqueue() 1148 { 1149 register WORK *w; 1150 FILE *f; 1151 int nrequests; 1152 char buf[MAXLINE]; 1153 1154 /* 1155 ** Check for permission to print the queue 1156 */ 1157 1158 if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0) 1159 { 1160 struct stat st; 1161 # ifdef NGROUPS 1162 int n; 1163 GIDSET_T gidset[NGROUPS]; 1164 # endif 1165 1166 if (stat(QueueDir, &st) < 0) 1167 { 1168 syserr("Cannot stat %s", QueueDir); 1169 return; 1170 } 1171 # ifdef NGROUPS 1172 n = getgroups(NGROUPS, gidset); 1173 while (--n >= 0) 1174 { 1175 if (gidset[n] == st.st_gid) 1176 break; 1177 } 1178 if (n < 0) 1179 # else 1180 if (RealGid != st.st_gid) 1181 # endif 1182 { 1183 usrerr("510 You are not permitted to see the queue"); 1184 setstat(EX_NOPERM); 1185 return; 1186 } 1187 } 1188 1189 /* 1190 ** Read and order the queue. 1191 */ 1192 1193 nrequests = orderq(TRUE); 1194 1195 /* 1196 ** Print the work list that we have read. 1197 */ 1198 1199 /* first see if there is anything */ 1200 if (nrequests <= 0) 1201 { 1202 printf("Mail queue is empty\n"); 1203 return; 1204 } 1205 1206 CurrentLA = getla(); /* get load average */ 1207 1208 printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 1209 if (nrequests > QUEUESIZE) 1210 printf(", only %d printed", QUEUESIZE); 1211 if (Verbose) 1212 printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 1213 else 1214 printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 1215 for (w = WorkQ; w != NULL; w = w->w_next) 1216 { 1217 struct stat st; 1218 auto time_t submittime = 0; 1219 long dfsize = -1; 1220 int flags = 0; 1221 char message[MAXLINE]; 1222 char bodytype[MAXNAME]; 1223 1224 printf("%8s", w->w_name + 2); 1225 f = fopen(w->w_name, "r"); 1226 if (f == NULL) 1227 { 1228 printf(" (job completed)\n"); 1229 errno = 0; 1230 continue; 1231 } 1232 if (!lockfile(fileno(f), w->w_name, NULL, LOCK_SH|LOCK_NB)) 1233 printf("*"); 1234 else if (shouldqueue(w->w_pri, w->w_ctime)) 1235 printf("X"); 1236 else 1237 printf(" "); 1238 errno = 0; 1239 1240 message[0] = bodytype[0] = '\0'; 1241 while (fgets(buf, sizeof buf, f) != NULL) 1242 { 1243 register int i; 1244 register char *p; 1245 1246 fixcrlf(buf, TRUE); 1247 switch (buf[0]) 1248 { 1249 case 'M': /* error message */ 1250 if ((i = strlen(&buf[1])) >= sizeof message) 1251 i = sizeof message - 1; 1252 bcopy(&buf[1], message, i); 1253 message[i] = '\0'; 1254 break; 1255 1256 case 'B': /* body type */ 1257 if ((i = strlen(&buf[1])) >= sizeof bodytype) 1258 i = sizeof bodytype - 1; 1259 bcopy(&buf[1], bodytype, i); 1260 bodytype[i] = '\0'; 1261 break; 1262 1263 case 'S': /* sender name */ 1264 if (Verbose) 1265 printf("%8ld %10ld%c%.12s %.38s", 1266 dfsize, 1267 w->w_pri, 1268 bitset(EF_WARNING, flags) ? '+' : ' ', 1269 ctime(&submittime) + 4, 1270 &buf[1]); 1271 else 1272 printf("%8ld %.16s %.45s", dfsize, 1273 ctime(&submittime), &buf[1]); 1274 if (message[0] != '\0' || bodytype[0] != '\0') 1275 { 1276 printf("\n %10.10s", bodytype); 1277 if (message[0] != '\0') 1278 printf(" (%.60s)", message); 1279 } 1280 break; 1281 1282 case 'C': /* controlling user */ 1283 if (Verbose) 1284 printf("\n\t\t\t\t (---%.34s---)", 1285 &buf[1]); 1286 break; 1287 1288 case 'R': /* recipient name */ 1289 if (Verbose) 1290 printf("\n\t\t\t\t\t %.38s", &buf[1]); 1291 else 1292 printf("\n\t\t\t\t %.45s", &buf[1]); 1293 break; 1294 1295 case 'T': /* creation time */ 1296 submittime = atol(&buf[1]); 1297 break; 1298 1299 case 'D': /* data file name */ 1300 if (stat(&buf[1], &st) >= 0) 1301 dfsize = st.st_size; 1302 break; 1303 1304 case 'F': /* flag bits */ 1305 for (p = &buf[1]; *p != '\0'; p++) 1306 { 1307 switch (*p) 1308 { 1309 case 'w': 1310 flags |= EF_WARNING; 1311 break; 1312 } 1313 } 1314 } 1315 } 1316 if (submittime == (time_t) 0) 1317 printf(" (no control file)"); 1318 printf("\n"); 1319 (void) fclose(f); 1320 } 1321 } 1322 1323 # endif /* QUEUE */ 1324 /* 1325 ** QUEUENAME -- build a file name in the queue directory for this envelope. 1326 ** 1327 ** Assigns an id code if one does not already exist. 1328 ** This code is very careful to avoid trashing existing files 1329 ** under any circumstances. 1330 ** 1331 ** Parameters: 1332 ** e -- envelope to build it in/from. 1333 ** type -- the file type, used as the first character 1334 ** of the file name. 1335 ** 1336 ** Returns: 1337 ** a pointer to the new file name (in a static buffer). 1338 ** 1339 ** Side Effects: 1340 ** If no id code is already assigned, queuename will 1341 ** assign an id code, create a qf file, and leave a 1342 ** locked, open-for-write file pointer in the envelope. 1343 */ 1344 1345 char * 1346 queuename(e, type) 1347 register ENVELOPE *e; 1348 int type; 1349 { 1350 static int pid = -1; 1351 static char c0; 1352 static char c1; 1353 static char c2; 1354 time_t now; 1355 struct tm *tm; 1356 static char buf[MAXNAME]; 1357 1358 if (e->e_id == NULL) 1359 { 1360 char qf[20]; 1361 1362 /* find a unique id */ 1363 if (pid != getpid()) 1364 { 1365 /* new process -- start back at "AA" */ 1366 pid = getpid(); 1367 now = curtime(); 1368 tm = localtime(&now); 1369 c0 = 'A' + tm->tm_hour; 1370 c1 = 'A'; 1371 c2 = 'A' - 1; 1372 } 1373 (void) sprintf(qf, "qf%cAA%05d", c0, pid); 1374 1375 while (c1 < '~' || c2 < 'Z') 1376 { 1377 int i; 1378 1379 if (c2 >= 'Z') 1380 { 1381 c1++; 1382 c2 = 'A' - 1; 1383 } 1384 qf[3] = c1; 1385 qf[4] = ++c2; 1386 if (tTd(7, 20)) 1387 printf("queuename: trying \"%s\"\n", qf); 1388 1389 i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 1390 if (i < 0) 1391 { 1392 if (errno == EEXIST) 1393 continue; 1394 syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 1395 qf, QueueDir, geteuid()); 1396 exit(EX_UNAVAILABLE); 1397 } 1398 if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB)) 1399 { 1400 e->e_lockfp = fdopen(i, "w"); 1401 break; 1402 } 1403 1404 /* a reader got the file; abandon it and try again */ 1405 (void) close(i); 1406 } 1407 if (c1 >= '~' && c2 >= 'Z') 1408 { 1409 syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 1410 qf, QueueDir, geteuid()); 1411 exit(EX_OSERR); 1412 } 1413 e->e_id = newstr(&qf[2]); 1414 define('i', e->e_id, e); 1415 if (tTd(7, 1)) 1416 printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 1417 if (tTd(7, 9)) 1418 { 1419 printf(" lockfd="); 1420 dumpfd(fileno(e->e_lockfp), TRUE, FALSE); 1421 } 1422 # ifdef LOG 1423 if (LogLevel > 93) 1424 syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 1425 # endif /* LOG */ 1426 } 1427 1428 if (type == '\0') 1429 return (NULL); 1430 (void) sprintf(buf, "%cf%s", type, e->e_id); 1431 if (tTd(7, 2)) 1432 printf("queuename: %s\n", buf); 1433 return (buf); 1434 } 1435 /* 1436 ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 1437 ** 1438 ** Parameters: 1439 ** e -- the envelope to unlock. 1440 ** 1441 ** Returns: 1442 ** none 1443 ** 1444 ** Side Effects: 1445 ** unlocks the queue for `e'. 1446 */ 1447 1448 unlockqueue(e) 1449 ENVELOPE *e; 1450 { 1451 if (tTd(51, 4)) 1452 printf("unlockqueue(%s)\n", e->e_id); 1453 1454 /* if there is a lock file in the envelope, close it */ 1455 if (e->e_lockfp != NULL) 1456 xfclose(e->e_lockfp, "unlockqueue", e->e_id); 1457 e->e_lockfp = NULL; 1458 1459 /* don't create a queue id if we don't already have one */ 1460 if (e->e_id == NULL) 1461 return; 1462 1463 /* remove the transcript */ 1464 # ifdef LOG 1465 if (LogLevel > 87) 1466 syslog(LOG_DEBUG, "%s: unlock", e->e_id); 1467 # endif /* LOG */ 1468 if (!tTd(51, 104)) 1469 xunlink(queuename(e, 'x')); 1470 1471 } 1472 /* 1473 ** SETCTLUSER -- create a controlling address 1474 ** 1475 ** Create a fake "address" given only a local login name; this is 1476 ** used as a "controlling user" for future recipient addresses. 1477 ** 1478 ** Parameters: 1479 ** user -- the user name of the controlling user. 1480 ** 1481 ** Returns: 1482 ** An address descriptor for the controlling user. 1483 ** 1484 ** Side Effects: 1485 ** none. 1486 */ 1487 1488 ADDRESS * 1489 setctluser(user) 1490 char *user; 1491 { 1492 register ADDRESS *a; 1493 struct passwd *pw; 1494 char *p; 1495 1496 /* 1497 ** See if this clears our concept of controlling user. 1498 */ 1499 1500 if (user == NULL || *user == '\0') 1501 return NULL; 1502 1503 /* 1504 ** Set up addr fields for controlling user. 1505 */ 1506 1507 a = (ADDRESS *) xalloc(sizeof *a); 1508 bzero((char *) a, sizeof *a); 1509 1510 p = strchr(user, ':'); 1511 if (p != NULL) 1512 *p++ = '\0'; 1513 if (*user != '\0' && (pw = getpwnam(user)) != NULL) 1514 { 1515 a->q_home = newstr(pw->pw_dir); 1516 a->q_uid = pw->pw_uid; 1517 a->q_gid = pw->pw_gid; 1518 a->q_user = newstr(user); 1519 a->q_flags |= QGOODUID; 1520 } 1521 else 1522 { 1523 a->q_user = newstr(DefUser); 1524 } 1525 1526 a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 1527 a->q_mailer = LocalMailer; 1528 if (p == NULL) 1529 a->q_paddr = a->q_user; 1530 else 1531 a->q_paddr = newstr(p); 1532 return a; 1533 } 1534