1 /* 2 * Copyright (c) 1983, 1995 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.79 (Berkeley) 04/21/95 (with queueing)"; 14 #else 15 static char sccsid[] = "@(#)queue.c 8.79 (Berkeley) 04/21/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) > (SIZE_T) 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 bool strcontainedin(); 771 772 switch (lbuf[0]) 773 { 774 case 'P': 775 w->w_pri = atol(&lbuf[1]); 776 i &= ~NEED_P; 777 break; 778 779 case 'T': 780 w->w_ctime = atol(&lbuf[1]); 781 i &= ~NEED_T; 782 break; 783 784 case 'R': 785 if (w->w_host == NULL && 786 (p = strrchr(&lbuf[1], '@')) != NULL) 787 w->w_host = newstr(&p[1]); 788 if (QueueLimitRecipient == NULL || 789 strcontainedin(QueueLimitRecipient, &lbuf[1])) 790 i &= ~NEED_R; 791 break; 792 793 case 'S': 794 if (QueueLimitSender != NULL && 795 strcontainedin(QueueLimitSender, &lbuf[1])) 796 i &= ~NEED_S; 797 break; 798 } 799 } 800 (void) fclose(cf); 801 802 if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 803 bitset(NEED_R|NEED_S, i)) 804 { 805 /* don't even bother sorting this job in */ 806 free(w->w_name); 807 if (w->w_host) 808 free(w->w_host); 809 wn--; 810 } 811 } 812 (void) closedir(f); 813 wn++; 814 815 wc = min(wn, QUEUESIZE); 816 817 if (QueueSortOrder == QS_BYHOST) 818 { 819 extern workcmpf1(); 820 extern workcmpf2(); 821 822 /* 823 ** Sort the work directory for the first time, 824 ** based on host name, lock status, and priority. 825 */ 826 827 qsort((char *) wlist, wc, sizeof *wlist, workcmpf1); 828 829 /* 830 ** If one message to host is locked, "lock" all messages 831 ** to that host. 832 */ 833 834 i = 0; 835 while (i < wc) 836 { 837 if (!wlist[i].w_lock) 838 { 839 i++; 840 continue; 841 } 842 w = &wlist[i]; 843 while (++i < wc) 844 { 845 if (wlist[i].w_host == NULL && 846 w->w_host == NULL) 847 wlist[i].w_lock = TRUE; 848 else if (wlist[i].w_host != NULL && 849 w->w_host != NULL && 850 strcmp(wlist[i].w_host, w->w_host) == 0) 851 wlist[i].w_lock = TRUE; 852 else 853 break; 854 } 855 } 856 857 /* 858 ** Sort the work directory for the second time, 859 ** based on lock status, host name, and priority. 860 */ 861 862 qsort((char *) wlist, wc, sizeof *wlist, workcmpf2); 863 } 864 else 865 { 866 extern workcmpf0(); 867 868 /* 869 ** Simple sort based on queue priority only. 870 */ 871 872 qsort((char *) wlist, wc, sizeof *wlist, workcmpf0); 873 } 874 875 /* 876 ** Convert the work list into canonical form. 877 ** Should be turning it into a list of envelopes here perhaps. 878 */ 879 880 WorkQ = NULL; 881 for (i = wc; --i >= 0; ) 882 { 883 w = (WORK *) xalloc(sizeof *w); 884 w->w_name = wlist[i].w_name; 885 w->w_host = wlist[i].w_host; 886 w->w_lock = wlist[i].w_lock; 887 w->w_pri = wlist[i].w_pri; 888 w->w_ctime = wlist[i].w_ctime; 889 w->w_next = WorkQ; 890 WorkQ = w; 891 } 892 893 if (tTd(40, 1)) 894 { 895 for (w = WorkQ; w != NULL; w = w->w_next) 896 printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 897 } 898 899 return (wn); 900 } 901 /* 902 ** WORKCMPF0 -- simple priority-only compare function. 903 ** 904 ** Parameters: 905 ** a -- the first argument. 906 ** b -- the second argument. 907 ** 908 ** Returns: 909 ** -1 if a < b 910 ** 0 if a == b 911 ** +1 if a > b 912 ** 913 ** Side Effects: 914 ** none. 915 */ 916 917 workcmpf0(a, b) 918 register WORK *a; 919 register WORK *b; 920 { 921 long pa = a->w_pri; 922 long pb = b->w_pri; 923 924 if (pa == pb) 925 return 0; 926 else if (pa > pb) 927 return 1; 928 else 929 return -1; 930 } 931 /* 932 ** WORKCMPF1 -- first compare function for ordering work based on host name. 933 ** 934 ** Sorts on host name, lock status, and priority in that order. 935 ** 936 ** Parameters: 937 ** a -- the first argument. 938 ** b -- the second argument. 939 ** 940 ** Returns: 941 ** <0 if a < b 942 ** 0 if a == b 943 ** >0 if a > b 944 ** 945 ** Side Effects: 946 ** none. 947 */ 948 949 workcmpf1(a, b) 950 register WORK *a; 951 register WORK *b; 952 { 953 int i; 954 955 /* host name */ 956 if (a->w_host != NULL && b->w_host == NULL) 957 return 1; 958 else if (a->w_host == NULL && b->w_host != NULL) 959 return -1; 960 if (a->w_host != NULL && b->w_host != NULL && 961 (i = strcmp(a->w_host, b->w_host))) 962 return i; 963 964 /* lock status */ 965 if (a->w_lock != b->w_lock) 966 return b->w_lock - a->w_lock; 967 968 /* job priority */ 969 return a->w_pri - b->w_pri; 970 } 971 /* 972 ** WORKCMPF2 -- second compare function for ordering work based on host name. 973 ** 974 ** Sorts on lock status, host name, and priority in that order. 975 ** 976 ** Parameters: 977 ** a -- the first argument. 978 ** b -- the second argument. 979 ** 980 ** Returns: 981 ** <0 if a < b 982 ** 0 if a == b 983 ** >0 if a > b 984 ** 985 ** Side Effects: 986 ** none. 987 */ 988 989 workcmpf2(a, b) 990 register WORK *a; 991 register WORK *b; 992 { 993 int i; 994 995 /* lock status */ 996 if (a->w_lock != b->w_lock) 997 return a->w_lock - b->w_lock; 998 999 /* host name */ 1000 if (a->w_host != NULL && b->w_host == NULL) 1001 return 1; 1002 else if (a->w_host == NULL && b->w_host != NULL) 1003 return -1; 1004 if (a->w_host != NULL && b->w_host != NULL && 1005 (i = strcmp(a->w_host, b->w_host))) 1006 return i; 1007 1008 /* job priority */ 1009 return a->w_pri - b->w_pri; 1010 } 1011 /* 1012 ** DOWORK -- do a work request. 1013 ** 1014 ** Parameters: 1015 ** id -- the ID of the job to run. 1016 ** forkflag -- if set, run this in background. 1017 ** requeueflag -- if set, reinstantiate the queue quickly. 1018 ** This is used when expanding aliases in the queue. 1019 ** If forkflag is also set, it doesn't wait for the 1020 ** child. 1021 ** e - the envelope in which to run it. 1022 ** 1023 ** Returns: 1024 ** process id of process that is running the queue job. 1025 ** 1026 ** Side Effects: 1027 ** The work request is satisfied if possible. 1028 */ 1029 1030 pid_t 1031 dowork(id, forkflag, requeueflag, e) 1032 char *id; 1033 bool forkflag; 1034 bool requeueflag; 1035 register ENVELOPE *e; 1036 { 1037 register pid_t pid; 1038 extern bool readqf(); 1039 1040 if (tTd(40, 1)) 1041 printf("dowork(%s)\n", id); 1042 1043 /* 1044 ** Fork for work. 1045 */ 1046 1047 if (forkflag) 1048 { 1049 pid = fork(); 1050 if (pid < 0) 1051 { 1052 syserr("dowork: cannot fork"); 1053 return 0; 1054 } 1055 else if (pid > 0) 1056 { 1057 /* parent -- clean out connection cache */ 1058 mci_flush(FALSE, NULL); 1059 } 1060 } 1061 else 1062 { 1063 pid = 0; 1064 } 1065 1066 if (pid == 0) 1067 { 1068 /* 1069 ** CHILD 1070 ** Lock the control file to avoid duplicate deliveries. 1071 ** Then run the file as though we had just read it. 1072 ** We save an idea of the temporary name so we 1073 ** can recover on interrupt. 1074 */ 1075 1076 /* set basic modes, etc. */ 1077 (void) alarm(0); 1078 clearenvelope(e, FALSE); 1079 e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS; 1080 e->e_errormode = EM_MAIL; 1081 e->e_id = id; 1082 GrabTo = UseErrorsTo = FALSE; 1083 ExitStat = EX_OK; 1084 if (forkflag) 1085 { 1086 disconnect(1, e); 1087 OpMode = MD_DELIVER; 1088 } 1089 # ifdef LOG 1090 if (LogLevel > 76) 1091 syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 1092 getpid()); 1093 # endif /* LOG */ 1094 1095 /* don't use the headers from sendmail.cf... */ 1096 e->e_header = NULL; 1097 1098 /* read the queue control file -- return if locked */ 1099 if (!readqf(e)) 1100 { 1101 if (tTd(40, 4)) 1102 printf("readqf(%s) failed\n", e->e_id); 1103 if (forkflag) 1104 exit(EX_OK); 1105 else 1106 return 0; 1107 } 1108 1109 e->e_flags |= EF_INQUEUE; 1110 1111 /* if this has been tried recently, let it be */ 1112 if (e->e_ntries > 0 && (curtime() - e->e_dtime) < MinQueueAge) 1113 { 1114 char *howlong = pintvl(curtime() - e->e_dtime, TRUE); 1115 1116 e->e_flags |= EF_KEEPQUEUE; 1117 if (Verbose || tTd(40, 8)) 1118 printf("%s: too young (%s)\n", 1119 e->e_id, howlong); 1120 #ifdef LOG 1121 if (LogLevel > 19) 1122 syslog(LOG_DEBUG, "%s: too young (%s)", 1123 e->e_id, howlong); 1124 #endif 1125 } 1126 else 1127 { 1128 eatheader(e, requeueflag); 1129 1130 if (requeueflag) 1131 queueup(e, TRUE, FALSE); 1132 1133 /* do the delivery */ 1134 sendall(e, SM_DELIVER); 1135 } 1136 1137 /* finish up and exit */ 1138 if (forkflag) 1139 finis(); 1140 else 1141 dropenvelope(e); 1142 } 1143 e->e_id = NULL; 1144 return pid; 1145 } 1146 /* 1147 ** READQF -- read queue file and set up environment. 1148 ** 1149 ** Parameters: 1150 ** e -- the envelope of the job to run. 1151 ** 1152 ** Returns: 1153 ** TRUE if it successfully read the queue file. 1154 ** FALSE otherwise. 1155 ** 1156 ** Side Effects: 1157 ** The queue file is returned locked. 1158 */ 1159 1160 bool 1161 readqf(e) 1162 register ENVELOPE *e; 1163 { 1164 register FILE *qfp; 1165 ADDRESS *ctladdr; 1166 struct stat st; 1167 char *bp; 1168 int qfver = 0; 1169 register char *p; 1170 char *orcpt = NULL; 1171 char qf[20]; 1172 char buf[MAXLINE]; 1173 extern ADDRESS *setctluser(); 1174 extern void loseqfile(); 1175 1176 /* 1177 ** Read and process the file. 1178 */ 1179 1180 strcpy(qf, queuename(e, 'q')); 1181 qfp = fopen(qf, "r+"); 1182 if (qfp == NULL) 1183 { 1184 if (tTd(40, 8)) 1185 printf("readqf(%s): fopen failure (%s)\n", 1186 qf, errstring(errno)); 1187 if (errno != ENOENT) 1188 syserr("readqf: no control file %s", qf); 1189 return FALSE; 1190 } 1191 1192 if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB)) 1193 { 1194 /* being processed by another queuer */ 1195 if (Verbose || tTd(40, 8)) 1196 printf("%s: locked\n", e->e_id); 1197 # ifdef LOG 1198 if (LogLevel > 19) 1199 syslog(LOG_DEBUG, "%s: locked", e->e_id); 1200 # endif /* LOG */ 1201 (void) fclose(qfp); 1202 return FALSE; 1203 } 1204 1205 /* 1206 ** Check the queue file for plausibility to avoid attacks. 1207 */ 1208 1209 if (fstat(fileno(qfp), &st) < 0) 1210 { 1211 /* must have been being processed by someone else */ 1212 if (tTd(40, 8)) 1213 printf("readqf(%s): fstat failure (%s)\n", 1214 qf, errstring(errno)); 1215 fclose(qfp); 1216 return FALSE; 1217 } 1218 1219 if (st.st_uid != geteuid() || bitset(S_IWOTH|S_IWGRP, st.st_mode)) 1220 { 1221 # ifdef LOG 1222 if (LogLevel > 0) 1223 { 1224 syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 1225 e->e_id, st.st_uid, st.st_mode); 1226 } 1227 # endif /* LOG */ 1228 if (tTd(40, 8)) 1229 printf("readqf(%s): bogus file\n", qf); 1230 loseqfile(e, "bogus file uid in mqueue"); 1231 fclose(qfp); 1232 return FALSE; 1233 } 1234 1235 if (st.st_size == 0) 1236 { 1237 /* must be a bogus file -- just remove it */ 1238 (void) unlink(qf); 1239 fclose(qfp); 1240 return FALSE; 1241 } 1242 1243 if (st.st_nlink == 0) 1244 { 1245 /* 1246 ** Race condition -- we got a file just as it was being 1247 ** unlinked. Just assume it is zero length. 1248 */ 1249 1250 fclose(qfp); 1251 return FALSE; 1252 } 1253 1254 /* good file -- save this lock */ 1255 e->e_lockfp = qfp; 1256 1257 /* do basic system initialization */ 1258 initsys(e); 1259 define('i', e->e_id, e); 1260 1261 LineNumber = 0; 1262 e->e_flags |= EF_GLOBALERRS; 1263 OpMode = MD_DELIVER; 1264 ctladdr = NULL; 1265 e->e_dfino = -1; 1266 e->e_msgsize = -1; 1267 while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 1268 { 1269 register char *p; 1270 u_long qflags; 1271 ADDRESS *q; 1272 1273 if (tTd(40, 4)) 1274 printf("+++++ %s\n", bp); 1275 switch (bp[0]) 1276 { 1277 case 'V': /* queue file version number */ 1278 qfver = atoi(&bp[1]); 1279 if (qfver > QF_VERSION) 1280 { 1281 syserr("Version number in qf (%d) greater than max (%d)", 1282 qfver, QF_VERSION); 1283 } 1284 break; 1285 1286 case 'C': /* specify controlling user */ 1287 ctladdr = setctluser(&bp[1]); 1288 break; 1289 1290 case 'Q': /* original recipient */ 1291 orcpt = newstr(&bp[1]); 1292 break; 1293 1294 case 'R': /* specify recipient */ 1295 p = bp; 1296 qflags = 0; 1297 if (qfver >= 1) 1298 { 1299 /* get flag bits */ 1300 while (*++p != '\0' && *p != ':') 1301 { 1302 switch (*p) 1303 { 1304 case 'S': 1305 qflags |= QPINGONSUCCESS; 1306 break; 1307 1308 case 'F': 1309 qflags |= QPINGONFAILURE; 1310 break; 1311 1312 case 'D': 1313 qflags |= QPINGONDELAY; 1314 break; 1315 1316 case 'P': 1317 qflags |= QPRIMARY; 1318 break; 1319 } 1320 } 1321 } 1322 else 1323 qflags |= QPRIMARY; 1324 q = parseaddr(++p, NULLADDR, RF_COPYALL, '\0', NULL, e); 1325 if (q != NULL) 1326 { 1327 q->q_alias = ctladdr; 1328 q->q_flags |= qflags; 1329 q->q_orcpt = orcpt; 1330 (void) recipient(q, &e->e_sendqueue, 0, e); 1331 } 1332 orcpt = NULL; 1333 break; 1334 1335 case 'E': /* specify error recipient */ 1336 /* no longer used */ 1337 break; 1338 1339 case 'H': /* header */ 1340 (void) chompheader(&bp[1], FALSE, NULL, e); 1341 break; 1342 1343 case 'M': /* message */ 1344 /* ignore this; we want a new message next time */ 1345 break; 1346 1347 case 'S': /* sender */ 1348 setsender(newstr(&bp[1]), e, NULL, TRUE); 1349 break; 1350 1351 case 'B': /* body type */ 1352 e->e_bodytype = newstr(&bp[1]); 1353 break; 1354 1355 case 'D': /* data file name */ 1356 /* obsolete -- ignore */ 1357 break; 1358 1359 case 'T': /* init time */ 1360 e->e_ctime = atol(&bp[1]); 1361 break; 1362 1363 case 'I': /* data file's inode number */ 1364 if (e->e_dfino == -1) 1365 e->e_dfino = atol(&buf[1]); 1366 break; 1367 1368 case 'K': /* time of last deliver attempt */ 1369 e->e_dtime = atol(&buf[1]); 1370 break; 1371 1372 case 'N': /* number of delivery attempts */ 1373 e->e_ntries = atoi(&buf[1]); 1374 break; 1375 1376 case 'P': /* message priority */ 1377 e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 1378 break; 1379 1380 case 'F': /* flag bits */ 1381 for (p = &bp[1]; *p != '\0'; p++) 1382 { 1383 switch (*p) 1384 { 1385 case 'w': /* warning sent */ 1386 e->e_flags |= EF_WARNING; 1387 break; 1388 1389 case 'r': /* response */ 1390 e->e_flags |= EF_RESPONSE; 1391 break; 1392 1393 case '8': /* has 8 bit data */ 1394 e->e_flags |= EF_HAS8BIT; 1395 break; 1396 } 1397 } 1398 break; 1399 1400 case 'Z': /* original envelope id from ESMTP */ 1401 e->e_envid = newstr(&bp[1]); 1402 break; 1403 1404 case '$': /* define macro */ 1405 define(bp[1], newstr(&bp[2]), e); 1406 break; 1407 1408 case '\0': /* blank line; ignore */ 1409 break; 1410 1411 default: 1412 syserr("readqf: %s: line %d: bad line \"%s\"", 1413 qf, LineNumber, bp); 1414 fclose(qfp); 1415 loseqfile(e, "unrecognized line"); 1416 return FALSE; 1417 } 1418 1419 if (bp != buf) 1420 free(bp); 1421 } 1422 1423 /* 1424 ** If we haven't read any lines, this queue file is empty. 1425 ** Arrange to remove it without referencing any null pointers. 1426 */ 1427 1428 if (LineNumber == 0) 1429 { 1430 errno = 0; 1431 e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 1432 } 1433 else 1434 { 1435 /* 1436 ** Arrange to read the data file. 1437 */ 1438 1439 p = queuename(e, 'd'); 1440 e->e_dfp = fopen(p, "r"); 1441 if (e->e_dfp == NULL) 1442 { 1443 syserr("readqf: cannot open %s", p); 1444 } 1445 else 1446 { 1447 e->e_flags |= EF_HAS_DF; 1448 if (fstat(fileno(e->e_dfp), &st) >= 0) 1449 { 1450 e->e_msgsize = st.st_size; 1451 e->e_dfdev = st.st_dev; 1452 e->e_dfino = st.st_ino; 1453 } 1454 } 1455 } 1456 1457 return TRUE; 1458 } 1459 /* 1460 ** PRINTQUEUE -- print out a representation of the mail queue 1461 ** 1462 ** Parameters: 1463 ** none. 1464 ** 1465 ** Returns: 1466 ** none. 1467 ** 1468 ** Side Effects: 1469 ** Prints a listing of the mail queue on the standard output. 1470 */ 1471 1472 printqueue() 1473 { 1474 register WORK *w; 1475 FILE *f; 1476 int nrequests; 1477 char buf[MAXLINE]; 1478 1479 /* 1480 ** Check for permission to print the queue 1481 */ 1482 1483 if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0) 1484 { 1485 struct stat st; 1486 # ifdef NGROUPS 1487 int n; 1488 GIDSET_T gidset[NGROUPS]; 1489 # endif 1490 1491 if (stat(QueueDir, &st) < 0) 1492 { 1493 syserr("Cannot stat %s", QueueDir); 1494 return; 1495 } 1496 # ifdef NGROUPS 1497 n = getgroups(NGROUPS, gidset); 1498 while (--n >= 0) 1499 { 1500 if (gidset[n] == st.st_gid) 1501 break; 1502 } 1503 if (n < 0) 1504 # else 1505 if (RealGid != st.st_gid) 1506 # endif 1507 { 1508 usrerr("510 You are not permitted to see the queue"); 1509 setstat(EX_NOPERM); 1510 return; 1511 } 1512 } 1513 1514 /* 1515 ** Read and order the queue. 1516 */ 1517 1518 nrequests = orderq(TRUE); 1519 1520 /* 1521 ** Print the work list that we have read. 1522 */ 1523 1524 /* first see if there is anything */ 1525 if (nrequests <= 0) 1526 { 1527 printf("Mail queue is empty\n"); 1528 return; 1529 } 1530 1531 CurrentLA = getla(); /* get load average */ 1532 1533 printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 1534 if (nrequests > QUEUESIZE) 1535 printf(", only %d printed", QUEUESIZE); 1536 if (Verbose) 1537 printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 1538 else 1539 printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 1540 for (w = WorkQ; w != NULL; w = w->w_next) 1541 { 1542 struct stat st; 1543 auto time_t submittime = 0; 1544 long dfsize; 1545 int flags = 0; 1546 int qfver; 1547 char message[MAXLINE]; 1548 char bodytype[MAXNAME + 1]; 1549 1550 printf("%8s", w->w_name + 2); 1551 f = fopen(w->w_name, "r"); 1552 if (f == NULL) 1553 { 1554 printf(" (job completed)\n"); 1555 errno = 0; 1556 continue; 1557 } 1558 w->w_name[0] = 'd'; 1559 if (stat(w->w_name, &st) >= 0) 1560 dfsize = st.st_size; 1561 else 1562 dfsize = -1; 1563 if (w->w_lock) 1564 printf("*"); 1565 else if (shouldqueue(w->w_pri, w->w_ctime)) 1566 printf("X"); 1567 else 1568 printf(" "); 1569 errno = 0; 1570 1571 message[0] = bodytype[0] = '\0'; 1572 qfver = 0; 1573 while (fgets(buf, sizeof buf, f) != NULL) 1574 { 1575 register int i; 1576 register char *p; 1577 1578 fixcrlf(buf, TRUE); 1579 switch (buf[0]) 1580 { 1581 case 'V': /* queue file version */ 1582 qfver = atoi(&buf[1]); 1583 break; 1584 1585 case 'M': /* error message */ 1586 if ((i = strlen(&buf[1])) >= sizeof message) 1587 i = sizeof message - 1; 1588 bcopy(&buf[1], message, i); 1589 message[i] = '\0'; 1590 break; 1591 1592 case 'B': /* body type */ 1593 if ((i = strlen(&buf[1])) >= sizeof bodytype) 1594 i = sizeof bodytype - 1; 1595 bcopy(&buf[1], bodytype, i); 1596 bodytype[i] = '\0'; 1597 break; 1598 1599 case 'S': /* sender name */ 1600 if (Verbose) 1601 printf("%8ld %10ld%c%.12s %.38s", 1602 dfsize, 1603 w->w_pri, 1604 bitset(EF_WARNING, flags) ? '+' : ' ', 1605 ctime(&submittime) + 4, 1606 &buf[1]); 1607 else 1608 printf("%8ld %.16s %.45s", dfsize, 1609 ctime(&submittime), &buf[1]); 1610 if (message[0] != '\0' || bodytype[0] != '\0') 1611 { 1612 printf("\n %10.10s", bodytype); 1613 if (message[0] != '\0') 1614 printf(" (%.60s)", message); 1615 } 1616 break; 1617 1618 case 'C': /* controlling user */ 1619 if (Verbose) 1620 printf("\n\t\t\t\t (---%.34s---)", 1621 &buf[1]); 1622 break; 1623 1624 case 'R': /* recipient name */ 1625 p = &buf[1]; 1626 if (qfver >= 1) 1627 { 1628 p = strchr(p, ':'); 1629 if (p == NULL) 1630 break; 1631 p++; 1632 } 1633 if (Verbose) 1634 printf("\n\t\t\t\t\t %.38s", p); 1635 else 1636 printf("\n\t\t\t\t %.45s", p); 1637 break; 1638 1639 case 'T': /* creation time */ 1640 submittime = atol(&buf[1]); 1641 break; 1642 1643 case 'F': /* flag bits */ 1644 for (p = &buf[1]; *p != '\0'; p++) 1645 { 1646 switch (*p) 1647 { 1648 case 'w': 1649 flags |= EF_WARNING; 1650 break; 1651 } 1652 } 1653 } 1654 } 1655 if (submittime == (time_t) 0) 1656 printf(" (no control file)"); 1657 printf("\n"); 1658 (void) fclose(f); 1659 } 1660 } 1661 1662 # endif /* QUEUE */ 1663 /* 1664 ** QUEUENAME -- build a file name in the queue directory for this envelope. 1665 ** 1666 ** Assigns an id code if one does not already exist. 1667 ** This code is very careful to avoid trashing existing files 1668 ** under any circumstances. 1669 ** 1670 ** Parameters: 1671 ** e -- envelope to build it in/from. 1672 ** type -- the file type, used as the first character 1673 ** of the file name. 1674 ** 1675 ** Returns: 1676 ** a pointer to the new file name (in a static buffer). 1677 ** 1678 ** Side Effects: 1679 ** If no id code is already assigned, queuename will 1680 ** assign an id code, create a qf file, and leave a 1681 ** locked, open-for-write file pointer in the envelope. 1682 */ 1683 1684 char * 1685 queuename(e, type) 1686 register ENVELOPE *e; 1687 int type; 1688 { 1689 static int pid = -1; 1690 static char c0; 1691 static char c1; 1692 static char c2; 1693 time_t now; 1694 struct tm *tm; 1695 static char buf[MAXNAME + 1]; 1696 1697 if (e->e_id == NULL) 1698 { 1699 char qf[20]; 1700 1701 /* find a unique id */ 1702 if (pid != getpid()) 1703 { 1704 /* new process -- start back at "AA" */ 1705 pid = getpid(); 1706 now = curtime(); 1707 tm = localtime(&now); 1708 c0 = 'A' + tm->tm_hour; 1709 c1 = 'A'; 1710 c2 = 'A' - 1; 1711 } 1712 (void) sprintf(qf, "qf%cAA%05d", c0, pid); 1713 1714 while (c1 < '~' || c2 < 'Z') 1715 { 1716 int i; 1717 1718 if (c2 >= 'Z') 1719 { 1720 c1++; 1721 c2 = 'A' - 1; 1722 } 1723 qf[3] = c1; 1724 qf[4] = ++c2; 1725 if (tTd(7, 20)) 1726 printf("queuename: trying \"%s\"\n", qf); 1727 1728 i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 1729 if (i < 0) 1730 { 1731 if (errno == EEXIST) 1732 continue; 1733 syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 1734 qf, QueueDir, geteuid()); 1735 exit(EX_UNAVAILABLE); 1736 } 1737 if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB)) 1738 { 1739 e->e_lockfp = fdopen(i, "w"); 1740 break; 1741 } 1742 1743 /* a reader got the file; abandon it and try again */ 1744 (void) close(i); 1745 } 1746 if (c1 >= '~' && c2 >= 'Z') 1747 { 1748 syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 1749 qf, QueueDir, geteuid()); 1750 exit(EX_OSERR); 1751 } 1752 e->e_id = newstr(&qf[2]); 1753 define('i', e->e_id, e); 1754 if (tTd(7, 1)) 1755 printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 1756 if (tTd(7, 9)) 1757 { 1758 printf(" lockfd="); 1759 dumpfd(fileno(e->e_lockfp), TRUE, FALSE); 1760 } 1761 # ifdef LOG 1762 if (LogLevel > 93) 1763 syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 1764 # endif /* LOG */ 1765 } 1766 1767 if (type == '\0') 1768 return (NULL); 1769 (void) sprintf(buf, "%cf%s", type, e->e_id); 1770 if (tTd(7, 2)) 1771 printf("queuename: %s\n", buf); 1772 return (buf); 1773 } 1774 /* 1775 ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 1776 ** 1777 ** Parameters: 1778 ** e -- the envelope to unlock. 1779 ** 1780 ** Returns: 1781 ** none 1782 ** 1783 ** Side Effects: 1784 ** unlocks the queue for `e'. 1785 */ 1786 1787 unlockqueue(e) 1788 ENVELOPE *e; 1789 { 1790 if (tTd(51, 4)) 1791 printf("unlockqueue(%s)\n", e->e_id); 1792 1793 /* if there is a lock file in the envelope, close it */ 1794 if (e->e_lockfp != NULL) 1795 xfclose(e->e_lockfp, "unlockqueue", e->e_id); 1796 e->e_lockfp = NULL; 1797 1798 /* don't create a queue id if we don't already have one */ 1799 if (e->e_id == NULL) 1800 return; 1801 1802 /* remove the transcript */ 1803 # ifdef LOG 1804 if (LogLevel > 87) 1805 syslog(LOG_DEBUG, "%s: unlock", e->e_id); 1806 # endif /* LOG */ 1807 if (!tTd(51, 104)) 1808 xunlink(queuename(e, 'x')); 1809 1810 } 1811 /* 1812 ** SETCTLUSER -- create a controlling address 1813 ** 1814 ** Create a fake "address" given only a local login name; this is 1815 ** used as a "controlling user" for future recipient addresses. 1816 ** 1817 ** Parameters: 1818 ** user -- the user name of the controlling user. 1819 ** 1820 ** Returns: 1821 ** An address descriptor for the controlling user. 1822 ** 1823 ** Side Effects: 1824 ** none. 1825 */ 1826 1827 ADDRESS * 1828 setctluser(user) 1829 char *user; 1830 { 1831 register ADDRESS *a; 1832 struct passwd *pw; 1833 char *p; 1834 1835 /* 1836 ** See if this clears our concept of controlling user. 1837 */ 1838 1839 if (user == NULL || *user == '\0') 1840 return NULL; 1841 1842 /* 1843 ** Set up addr fields for controlling user. 1844 */ 1845 1846 a = (ADDRESS *) xalloc(sizeof *a); 1847 bzero((char *) a, sizeof *a); 1848 1849 p = strchr(user, ':'); 1850 if (p != NULL) 1851 *p++ = '\0'; 1852 if (*user != '\0' && (pw = sm_getpwnam(user)) != NULL) 1853 { 1854 if (strcmp(pw->pw_dir, "/") == 0) 1855 a->q_home = ""; 1856 else 1857 a->q_home = newstr(pw->pw_dir); 1858 a->q_uid = pw->pw_uid; 1859 a->q_gid = pw->pw_gid; 1860 a->q_flags |= QGOODUID; 1861 } 1862 1863 if (*user != '\0') 1864 a->q_user = newstr(user); 1865 else if (p != NULL) 1866 a->q_user = newstr(p); 1867 else 1868 a->q_user = newstr(DefUser); 1869 1870 a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 1871 a->q_mailer = LocalMailer; 1872 if (p == NULL) 1873 a->q_paddr = a->q_user; 1874 else 1875 a->q_paddr = newstr(p); 1876 return a; 1877 } 1878 /* 1879 ** LOSEQFILE -- save the qf as Qf and try to let someone know 1880 ** 1881 ** Parameters: 1882 ** e -- the envelope (e->e_id will be used). 1883 ** why -- reported to whomever can hear. 1884 ** 1885 ** Returns: 1886 ** none. 1887 */ 1888 1889 void 1890 loseqfile(e, why) 1891 register ENVELOPE *e; 1892 char *why; 1893 { 1894 char *p; 1895 char buf[40]; 1896 1897 if (e == NULL || e->e_id == NULL) 1898 return; 1899 if (strlen(e->e_id) > sizeof buf - 4) 1900 return; 1901 strcpy(buf, queuename(e, 'q')); 1902 p = queuename(e, 'Q'); 1903 if (rename(buf, p) < 0) 1904 syserr("cannot rename(%s, %s), uid=%d", buf, p, geteuid()); 1905 #ifdef LOG 1906 else if (LogLevel > 0) 1907 syslog(LOG_ALERT, "Losing %s: %s", buf, why); 1908 #endif 1909 } 1910