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