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