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.84 (Berkeley) 05/28/95 (with queueing)"; 14 #else 15 static char sccsid[] = "@(#)queue.c 8.84 (Berkeley) 05/28/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 char *msg = "Skipping queue run -- load average too high"; 497 498 if (Verbose) 499 printf("%s\n", msg); 500 #ifdef LOG 501 if (LogLevel > 8) 502 syslog(LOG_INFO, "runqueue: %s", msg); 503 #endif 504 if (forkflag && QueueIntvl != 0) 505 (void) setevent(QueueIntvl, runqueue, TRUE); 506 return; 507 } 508 509 /* 510 ** See if we want to go off and do other useful work. 511 */ 512 513 if (forkflag) 514 { 515 int pid; 516 #ifdef SIGCHLD 517 extern void reapchild(); 518 519 (void) setsignal(SIGCHLD, reapchild); 520 #endif 521 522 pid = dofork(); 523 if (pid != 0) 524 { 525 /* parent -- pick up intermediate zombie */ 526 #ifndef SIGCHLD 527 (void) waitfor(pid); 528 #endif /* SIGCHLD */ 529 if (QueueIntvl != 0) 530 (void) setevent(QueueIntvl, runqueue, TRUE); 531 return; 532 } 533 /* child -- double fork */ 534 #ifndef SIGCHLD 535 if (fork() != 0) 536 exit(EX_OK); 537 #else /* SIGCHLD */ 538 (void) setsignal(SIGCHLD, SIG_DFL); 539 #endif /* SIGCHLD */ 540 } 541 542 setproctitle("running queue: %s", QueueDir); 543 544 # ifdef LOG 545 if (LogLevel > 69) 546 syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 547 QueueDir, getpid(), forkflag); 548 # endif /* LOG */ 549 550 /* 551 ** Release any resources used by the daemon code. 552 */ 553 554 # ifdef DAEMON 555 clrdaemon(); 556 # endif /* DAEMON */ 557 558 /* force it to run expensive jobs */ 559 NoConnect = FALSE; 560 561 /* 562 ** Create ourselves an envelope 563 */ 564 565 CurEnv = &QueueEnvelope; 566 e = newenvelope(&QueueEnvelope, CurEnv); 567 e->e_flags = BlankEnvelope.e_flags; 568 569 /* 570 ** Make sure the alias database is open. 571 */ 572 573 initmaps(FALSE, e); 574 575 /* 576 ** Start making passes through the queue. 577 ** First, read and sort the entire queue. 578 ** Then, process the work in that order. 579 ** But if you take too long, start over. 580 */ 581 582 /* order the existing work requests */ 583 njobs = orderq(FALSE); 584 585 /* process them once at a time */ 586 while (WorkQ != NULL) 587 { 588 WORK *w = WorkQ; 589 590 WorkQ = WorkQ->w_next; 591 592 /* 593 ** Ignore jobs that are too expensive for the moment. 594 */ 595 596 sequenceno++; 597 if (shouldqueue(w->w_pri, w->w_ctime)) 598 { 599 if (Verbose) 600 printf("\nSkipping %s (sequence %d of %d)\n", 601 w->w_name + 2, sequenceno, njobs); 602 } 603 else 604 { 605 pid_t pid; 606 extern pid_t dowork(); 607 608 if (Verbose) 609 printf("\nRunning %s (sequence %d of %d)\n", 610 w->w_name + 2, sequenceno, njobs); 611 pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e); 612 errno = 0; 613 if (pid != 0) 614 (void) waitfor(pid); 615 } 616 free(w->w_name); 617 if (w->w_host) 618 free(w->w_host); 619 free((char *) w); 620 } 621 622 /* exit without the usual cleanup */ 623 e->e_id = NULL; 624 finis(); 625 } 626 /* 627 ** ORDERQ -- order the work queue. 628 ** 629 ** Parameters: 630 ** doall -- if set, include everything in the queue (even 631 ** the jobs that cannot be run because the load 632 ** average is too high). Otherwise, exclude those 633 ** jobs. 634 ** 635 ** Returns: 636 ** The number of request in the queue (not necessarily 637 ** the number of requests in WorkQ however). 638 ** 639 ** Side Effects: 640 ** Sets WorkQ to the queue of available work, in order. 641 */ 642 643 # define NEED_P 001 644 # define NEED_T 002 645 # define NEED_R 004 646 # define NEED_S 010 647 648 static WORK *WorkList = NULL; 649 static int WorkListSize = 0; 650 651 orderq(doall) 652 bool doall; 653 { 654 register struct dirent *d; 655 register WORK *w; 656 DIR *f; 657 register int i; 658 int wn = -1; 659 int wc; 660 661 if (tTd(41, 1)) 662 { 663 printf("orderq:\n"); 664 if (QueueLimitId != NULL) 665 printf("\tQueueLimitId = %s\n", QueueLimitId); 666 if (QueueLimitSender != NULL) 667 printf("\tQueueLimitSender = %s\n", QueueLimitSender); 668 if (QueueLimitRecipient != NULL) 669 printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 670 } 671 672 /* clear out old WorkQ */ 673 for (w = WorkQ; w != NULL; ) 674 { 675 register WORK *nw = w->w_next; 676 677 WorkQ = nw; 678 free(w->w_name); 679 if (w->w_host) 680 free(w->w_host); 681 free((char *) w); 682 w = nw; 683 } 684 685 /* open the queue directory */ 686 f = opendir("."); 687 if (f == NULL) 688 { 689 syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 690 return (0); 691 } 692 693 /* 694 ** Read the work directory. 695 */ 696 697 while ((d = readdir(f)) != NULL) 698 { 699 FILE *cf; 700 register char *p; 701 char lbuf[MAXNAME + 1]; 702 extern bool strcontainedin(); 703 704 /* is this an interesting entry? */ 705 if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 706 continue; 707 708 if (QueueLimitId != NULL && 709 !strcontainedin(QueueLimitId, d->d_name)) 710 continue; 711 712 #ifdef PICKY_QF_NAME_CHECK 713 /* 714 ** Check queue name for plausibility. This handles 715 ** both old and new type ids. 716 */ 717 718 p = d->d_name + 2; 719 if (isupper(p[0]) && isupper(p[2])) 720 p += 3; 721 else if (isupper(p[1])) 722 p += 2; 723 else 724 p = d->d_name; 725 for (i = 0; isdigit(*p); p++) 726 i++; 727 if (i < 5 || *p != '\0') 728 { 729 if (Verbose) 730 printf("orderq: bogus qf name %s\n", d->d_name); 731 # ifdef LOG 732 if (LogLevel > 0) 733 syslog(LOG_ALERT, "orderq: bogus qf name %s", 734 d->d_name); 735 # endif 736 if (strlen(d->d_name) > (SIZE_T) MAXNAME) 737 d->d_name[MAXNAME] = '\0'; 738 strcpy(lbuf, d->d_name); 739 lbuf[0] = 'Q'; 740 (void) rename(d->d_name, lbuf); 741 continue; 742 } 743 #endif 744 745 /* open control file (if not too many files) */ 746 if (++wn > MaxQueueRun && MaxQueueRun > 0) 747 { 748 # ifdef LOG 749 if (wn == MaxQueueRun + 1 && LogLevel > 0) 750 syslog(LOG_ALERT, "WorkList for %s maxed out at %d", 751 QueueDir, MaxQueueRun); 752 # endif 753 continue; 754 } 755 if (wn >= WorkListSize) 756 { 757 grow_wlist(); 758 if (wn >= WorkListSize) 759 continue; 760 } 761 762 cf = fopen(d->d_name, "r"); 763 if (cf == NULL) 764 { 765 /* this may be some random person sending hir msgs */ 766 /* syserr("orderq: cannot open %s", cbuf); */ 767 if (tTd(41, 2)) 768 printf("orderq: cannot open %s (%d)\n", 769 d->d_name, errno); 770 errno = 0; 771 wn--; 772 continue; 773 } 774 w = &WorkList[wn]; 775 w->w_name = newstr(d->d_name); 776 w->w_host = NULL; 777 w->w_lock = !lockfile(fileno(cf), w->w_name, NULL, LOCK_SH|LOCK_NB); 778 779 /* make sure jobs in creation don't clog queue */ 780 w->w_pri = 0x7fffffff; 781 w->w_ctime = 0; 782 783 /* extract useful information */ 784 i = NEED_P | NEED_T; 785 if (QueueLimitSender != NULL) 786 i |= NEED_S; 787 if (QueueSortOrder == QS_BYHOST || QueueLimitRecipient != NULL) 788 i |= NEED_R; 789 while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 790 { 791 extern bool strcontainedin(); 792 793 switch (lbuf[0]) 794 { 795 case 'P': 796 w->w_pri = atol(&lbuf[1]); 797 i &= ~NEED_P; 798 break; 799 800 case 'T': 801 w->w_ctime = atol(&lbuf[1]); 802 i &= ~NEED_T; 803 break; 804 805 case 'R': 806 if (w->w_host == NULL && 807 (p = strrchr(&lbuf[1], '@')) != NULL) 808 w->w_host = newstr(&p[1]); 809 if (QueueLimitRecipient == NULL || 810 strcontainedin(QueueLimitRecipient, &lbuf[1])) 811 i &= ~NEED_R; 812 break; 813 814 case 'S': 815 if (QueueLimitSender != NULL && 816 strcontainedin(QueueLimitSender, &lbuf[1])) 817 i &= ~NEED_S; 818 break; 819 } 820 } 821 (void) fclose(cf); 822 823 if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 824 bitset(NEED_R|NEED_S, i)) 825 { 826 /* don't even bother sorting this job in */ 827 free(w->w_name); 828 if (w->w_host) 829 free(w->w_host); 830 wn--; 831 } 832 } 833 (void) closedir(f); 834 wn++; 835 836 wc = min(wn, WorkListSize); 837 838 if (QueueSortOrder == QS_BYHOST) 839 { 840 extern workcmpf1(); 841 extern workcmpf2(); 842 843 /* 844 ** Sort the work directory for the first time, 845 ** based on host name, lock status, and priority. 846 */ 847 848 qsort((char *) WorkList, wc, sizeof *WorkList, workcmpf1); 849 850 /* 851 ** If one message to host is locked, "lock" all messages 852 ** to that host. 853 */ 854 855 i = 0; 856 while (i < wc) 857 { 858 if (!WorkList[i].w_lock) 859 { 860 i++; 861 continue; 862 } 863 w = &WorkList[i]; 864 while (++i < wc) 865 { 866 if (WorkList[i].w_host == NULL && 867 w->w_host == NULL) 868 WorkList[i].w_lock = TRUE; 869 else if (WorkList[i].w_host != NULL && 870 w->w_host != NULL && 871 strcmp(WorkList[i].w_host, w->w_host) == 0) 872 WorkList[i].w_lock = TRUE; 873 else 874 break; 875 } 876 } 877 878 /* 879 ** Sort the work directory for the second time, 880 ** based on lock status, host name, and priority. 881 */ 882 883 qsort((char *) WorkList, wc, sizeof *WorkList, workcmpf2); 884 } 885 else 886 { 887 extern workcmpf0(); 888 889 /* 890 ** Simple sort based on queue priority only. 891 */ 892 893 qsort((char *) WorkList, wc, sizeof *WorkList, workcmpf0); 894 } 895 896 /* 897 ** Convert the work list into canonical form. 898 ** Should be turning it into a list of envelopes here perhaps. 899 */ 900 901 WorkQ = NULL; 902 for (i = wc; --i >= 0; ) 903 { 904 w = (WORK *) xalloc(sizeof *w); 905 w->w_name = WorkList[i].w_name; 906 w->w_host = WorkList[i].w_host; 907 w->w_lock = WorkList[i].w_lock; 908 w->w_pri = WorkList[i].w_pri; 909 w->w_ctime = WorkList[i].w_ctime; 910 w->w_next = WorkQ; 911 WorkQ = w; 912 } 913 free(WorkList); 914 WorkList = NULL; 915 916 if (tTd(40, 1)) 917 { 918 for (w = WorkQ; w != NULL; w = w->w_next) 919 printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 920 } 921 922 return (wn); 923 } 924 /* 925 ** GROW_WLIST -- make the work list larger 926 ** 927 ** Parameters: 928 ** none. 929 ** 930 ** Returns: 931 ** none. 932 ** 933 ** Side Effects: 934 ** Adds another QUEUESEGSIZE entries to WorkList if possible. 935 ** It can fail if there isn't enough memory, so WorkListSize 936 ** should be checked again upon return. 937 */ 938 939 grow_wlist() 940 { 941 if (tTd(41, 1)) 942 printf("grow_wlist: WorkListSize=%d\n", WorkListSize); 943 if (WorkList == NULL) 944 { 945 WorkList = (WORK *) xalloc(sizeof(WORK) * (QUEUESEGSIZE + 1)); 946 WorkListSize = QUEUESEGSIZE; 947 } 948 else 949 { 950 int newsize = WorkListSize + QUEUESEGSIZE; 951 WORK *newlist = (WORK *) realloc((char *)WorkList, 952 (unsigned)sizeof(WORK) * (newsize + 1)); 953 954 if (newlist != NULL) 955 { 956 WorkListSize = newsize; 957 WorkList = newlist; 958 # ifdef LOG 959 if (LogLevel > 1) 960 { 961 syslog(LOG_NOTICE, "grew WorkList for %s to %d", 962 QueueDir, WorkListSize); 963 } 964 } 965 else if (LogLevel > 0) 966 { 967 syslog(LOG_ALERT, "FAILED to grow WorkList for %s to %d", 968 QueueDir, newsize); 969 # endif 970 } 971 } 972 if (tTd(41, 1)) 973 printf("grow_wlist: WorkListSize now %d\n", WorkListSize); 974 } 975 /* 976 ** WORKCMPF0 -- simple priority-only compare function. 977 ** 978 ** Parameters: 979 ** a -- the first argument. 980 ** b -- the second argument. 981 ** 982 ** Returns: 983 ** -1 if a < b 984 ** 0 if a == b 985 ** +1 if a > b 986 ** 987 ** Side Effects: 988 ** none. 989 */ 990 991 workcmpf0(a, b) 992 register WORK *a; 993 register WORK *b; 994 { 995 long pa = a->w_pri; 996 long pb = b->w_pri; 997 998 if (pa == pb) 999 return 0; 1000 else if (pa > pb) 1001 return 1; 1002 else 1003 return -1; 1004 } 1005 /* 1006 ** WORKCMPF1 -- first compare function for ordering work based on host name. 1007 ** 1008 ** Sorts on host name, lock status, and priority in that order. 1009 ** 1010 ** Parameters: 1011 ** a -- the first argument. 1012 ** b -- the second argument. 1013 ** 1014 ** Returns: 1015 ** <0 if a < b 1016 ** 0 if a == b 1017 ** >0 if a > b 1018 ** 1019 ** Side Effects: 1020 ** none. 1021 */ 1022 1023 workcmpf1(a, b) 1024 register WORK *a; 1025 register WORK *b; 1026 { 1027 int i; 1028 1029 /* host name */ 1030 if (a->w_host != NULL && b->w_host == NULL) 1031 return 1; 1032 else if (a->w_host == NULL && b->w_host != NULL) 1033 return -1; 1034 if (a->w_host != NULL && b->w_host != NULL && 1035 (i = strcmp(a->w_host, b->w_host))) 1036 return i; 1037 1038 /* lock status */ 1039 if (a->w_lock != b->w_lock) 1040 return b->w_lock - a->w_lock; 1041 1042 /* job priority */ 1043 return a->w_pri - b->w_pri; 1044 } 1045 /* 1046 ** WORKCMPF2 -- second compare function for ordering work based on host name. 1047 ** 1048 ** Sorts on lock status, host name, and priority in that order. 1049 ** 1050 ** Parameters: 1051 ** a -- the first argument. 1052 ** b -- the second argument. 1053 ** 1054 ** Returns: 1055 ** <0 if a < b 1056 ** 0 if a == b 1057 ** >0 if a > b 1058 ** 1059 ** Side Effects: 1060 ** none. 1061 */ 1062 1063 workcmpf2(a, b) 1064 register WORK *a; 1065 register WORK *b; 1066 { 1067 int i; 1068 1069 /* lock status */ 1070 if (a->w_lock != b->w_lock) 1071 return a->w_lock - b->w_lock; 1072 1073 /* host name */ 1074 if (a->w_host != NULL && b->w_host == NULL) 1075 return 1; 1076 else if (a->w_host == NULL && b->w_host != NULL) 1077 return -1; 1078 if (a->w_host != NULL && b->w_host != NULL && 1079 (i = strcmp(a->w_host, b->w_host))) 1080 return i; 1081 1082 /* job priority */ 1083 return a->w_pri - b->w_pri; 1084 } 1085 /* 1086 ** DOWORK -- do a work request. 1087 ** 1088 ** Parameters: 1089 ** id -- the ID of the job to run. 1090 ** forkflag -- if set, run this in background. 1091 ** requeueflag -- if set, reinstantiate the queue quickly. 1092 ** This is used when expanding aliases in the queue. 1093 ** If forkflag is also set, it doesn't wait for the 1094 ** child. 1095 ** e - the envelope in which to run it. 1096 ** 1097 ** Returns: 1098 ** process id of process that is running the queue job. 1099 ** 1100 ** Side Effects: 1101 ** The work request is satisfied if possible. 1102 */ 1103 1104 pid_t 1105 dowork(id, forkflag, requeueflag, e) 1106 char *id; 1107 bool forkflag; 1108 bool requeueflag; 1109 register ENVELOPE *e; 1110 { 1111 register pid_t pid; 1112 extern bool readqf(); 1113 1114 if (tTd(40, 1)) 1115 printf("dowork(%s)\n", id); 1116 1117 /* 1118 ** Fork for work. 1119 */ 1120 1121 if (forkflag) 1122 { 1123 pid = fork(); 1124 if (pid < 0) 1125 { 1126 syserr("dowork: cannot fork"); 1127 return 0; 1128 } 1129 else if (pid > 0) 1130 { 1131 /* parent -- clean out connection cache */ 1132 mci_flush(FALSE, NULL); 1133 } 1134 } 1135 else 1136 { 1137 pid = 0; 1138 } 1139 1140 if (pid == 0) 1141 { 1142 /* 1143 ** CHILD 1144 ** Lock the control file to avoid duplicate deliveries. 1145 ** Then run the file as though we had just read it. 1146 ** We save an idea of the temporary name so we 1147 ** can recover on interrupt. 1148 */ 1149 1150 /* set basic modes, etc. */ 1151 (void) alarm(0); 1152 clearenvelope(e, FALSE); 1153 e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS; 1154 e->e_errormode = EM_MAIL; 1155 e->e_id = id; 1156 GrabTo = UseErrorsTo = FALSE; 1157 ExitStat = EX_OK; 1158 if (forkflag) 1159 { 1160 disconnect(1, e); 1161 OpMode = MD_DELIVER; 1162 } 1163 # ifdef LOG 1164 if (LogLevel > 76) 1165 syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 1166 getpid()); 1167 # endif /* LOG */ 1168 1169 /* don't use the headers from sendmail.cf... */ 1170 e->e_header = NULL; 1171 1172 /* read the queue control file -- return if locked */ 1173 if (!readqf(e)) 1174 { 1175 if (tTd(40, 4)) 1176 printf("readqf(%s) failed\n", e->e_id); 1177 if (forkflag) 1178 exit(EX_OK); 1179 else 1180 return 0; 1181 } 1182 1183 e->e_flags |= EF_INQUEUE; 1184 1185 /* if this has been tried recently, let it be */ 1186 if (e->e_ntries > 0 && (curtime() - e->e_dtime) < MinQueueAge) 1187 { 1188 char *howlong = pintvl(curtime() - e->e_dtime, TRUE); 1189 1190 e->e_flags |= EF_KEEPQUEUE; 1191 if (Verbose || tTd(40, 8)) 1192 printf("%s: too young (%s)\n", 1193 e->e_id, howlong); 1194 #ifdef LOG 1195 if (LogLevel > 19) 1196 syslog(LOG_DEBUG, "%s: too young (%s)", 1197 e->e_id, howlong); 1198 #endif 1199 } 1200 else 1201 { 1202 eatheader(e, requeueflag); 1203 1204 if (requeueflag) 1205 queueup(e, TRUE, FALSE); 1206 1207 /* do the delivery */ 1208 sendall(e, SM_DELIVER); 1209 } 1210 1211 /* finish up and exit */ 1212 if (forkflag) 1213 finis(); 1214 else 1215 dropenvelope(e); 1216 } 1217 e->e_id = NULL; 1218 return pid; 1219 } 1220 /* 1221 ** READQF -- read queue file and set up environment. 1222 ** 1223 ** Parameters: 1224 ** e -- the envelope of the job to run. 1225 ** 1226 ** Returns: 1227 ** TRUE if it successfully read the queue file. 1228 ** FALSE otherwise. 1229 ** 1230 ** Side Effects: 1231 ** The queue file is returned locked. 1232 */ 1233 1234 bool 1235 readqf(e) 1236 register ENVELOPE *e; 1237 { 1238 register FILE *qfp; 1239 ADDRESS *ctladdr; 1240 struct stat st; 1241 char *bp; 1242 int qfver = 0; 1243 register char *p; 1244 char *orcpt = NULL; 1245 char qf[20]; 1246 char buf[MAXLINE]; 1247 extern ADDRESS *setctluser(); 1248 extern void loseqfile(); 1249 1250 /* 1251 ** Read and process the file. 1252 */ 1253 1254 strcpy(qf, queuename(e, 'q')); 1255 qfp = fopen(qf, "r+"); 1256 if (qfp == NULL) 1257 { 1258 if (tTd(40, 8)) 1259 printf("readqf(%s): fopen failure (%s)\n", 1260 qf, errstring(errno)); 1261 if (errno != ENOENT) 1262 syserr("readqf: no control file %s", qf); 1263 return FALSE; 1264 } 1265 1266 if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB)) 1267 { 1268 /* being processed by another queuer */ 1269 if (Verbose || tTd(40, 8)) 1270 printf("%s: locked\n", e->e_id); 1271 # ifdef LOG 1272 if (LogLevel > 19) 1273 syslog(LOG_DEBUG, "%s: locked", e->e_id); 1274 # endif /* LOG */ 1275 (void) fclose(qfp); 1276 return FALSE; 1277 } 1278 1279 /* 1280 ** Check the queue file for plausibility to avoid attacks. 1281 */ 1282 1283 if (fstat(fileno(qfp), &st) < 0) 1284 { 1285 /* must have been being processed by someone else */ 1286 if (tTd(40, 8)) 1287 printf("readqf(%s): fstat failure (%s)\n", 1288 qf, errstring(errno)); 1289 fclose(qfp); 1290 return FALSE; 1291 } 1292 1293 if (st.st_uid != geteuid() || bitset(S_IWOTH|S_IWGRP, st.st_mode)) 1294 { 1295 # ifdef LOG 1296 if (LogLevel > 0) 1297 { 1298 syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 1299 e->e_id, st.st_uid, st.st_mode); 1300 } 1301 # endif /* LOG */ 1302 if (tTd(40, 8)) 1303 printf("readqf(%s): bogus file\n", qf); 1304 loseqfile(e, "bogus file uid in mqueue"); 1305 fclose(qfp); 1306 return FALSE; 1307 } 1308 1309 if (st.st_size == 0) 1310 { 1311 /* must be a bogus file -- just remove it */ 1312 (void) unlink(qf); 1313 fclose(qfp); 1314 return FALSE; 1315 } 1316 1317 if (st.st_nlink == 0) 1318 { 1319 /* 1320 ** Race condition -- we got a file just as it was being 1321 ** unlinked. Just assume it is zero length. 1322 */ 1323 1324 fclose(qfp); 1325 return FALSE; 1326 } 1327 1328 /* good file -- save this lock */ 1329 e->e_lockfp = qfp; 1330 1331 /* do basic system initialization */ 1332 initsys(e); 1333 define('i', e->e_id, e); 1334 1335 LineNumber = 0; 1336 e->e_flags |= EF_GLOBALERRS; 1337 OpMode = MD_DELIVER; 1338 ctladdr = NULL; 1339 e->e_dfino = -1; 1340 e->e_msgsize = -1; 1341 while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 1342 { 1343 register char *p; 1344 u_long qflags; 1345 ADDRESS *q; 1346 1347 if (tTd(40, 4)) 1348 printf("+++++ %s\n", bp); 1349 switch (bp[0]) 1350 { 1351 case 'V': /* queue file version number */ 1352 qfver = atoi(&bp[1]); 1353 if (qfver > QF_VERSION) 1354 { 1355 syserr("Version number in qf (%d) greater than max (%d)", 1356 qfver, QF_VERSION); 1357 } 1358 break; 1359 1360 case 'C': /* specify controlling user */ 1361 ctladdr = setctluser(&bp[1]); 1362 break; 1363 1364 case 'Q': /* original recipient */ 1365 orcpt = newstr(&bp[1]); 1366 break; 1367 1368 case 'R': /* specify recipient */ 1369 p = bp; 1370 qflags = 0; 1371 if (qfver >= 1) 1372 { 1373 /* get flag bits */ 1374 while (*++p != '\0' && *p != ':') 1375 { 1376 switch (*p) 1377 { 1378 case 'S': 1379 qflags |= QPINGONSUCCESS; 1380 break; 1381 1382 case 'F': 1383 qflags |= QPINGONFAILURE; 1384 break; 1385 1386 case 'D': 1387 qflags |= QPINGONDELAY; 1388 break; 1389 1390 case 'P': 1391 qflags |= QPRIMARY; 1392 break; 1393 } 1394 } 1395 } 1396 else 1397 qflags |= QPRIMARY; 1398 q = parseaddr(++p, NULLADDR, RF_COPYALL, '\0', NULL, e); 1399 if (q != NULL) 1400 { 1401 q->q_alias = ctladdr; 1402 q->q_flags |= qflags; 1403 q->q_orcpt = orcpt; 1404 (void) recipient(q, &e->e_sendqueue, 0, e); 1405 } 1406 orcpt = NULL; 1407 break; 1408 1409 case 'E': /* specify error recipient */ 1410 /* no longer used */ 1411 break; 1412 1413 case 'H': /* header */ 1414 (void) chompheader(&bp[1], FALSE, NULL, e); 1415 break; 1416 1417 case 'M': /* message */ 1418 /* ignore this; we want a new message next time */ 1419 break; 1420 1421 case 'S': /* sender */ 1422 setsender(newstr(&bp[1]), e, NULL, TRUE); 1423 break; 1424 1425 case 'B': /* body type */ 1426 e->e_bodytype = newstr(&bp[1]); 1427 break; 1428 1429 case 'D': /* data file name */ 1430 /* obsolete -- ignore */ 1431 break; 1432 1433 case 'T': /* init time */ 1434 e->e_ctime = atol(&bp[1]); 1435 break; 1436 1437 case 'I': /* data file's inode number */ 1438 if (e->e_dfino == -1) 1439 e->e_dfino = atol(&buf[1]); 1440 break; 1441 1442 case 'K': /* time of last deliver attempt */ 1443 e->e_dtime = atol(&buf[1]); 1444 break; 1445 1446 case 'N': /* number of delivery attempts */ 1447 e->e_ntries = atoi(&buf[1]); 1448 break; 1449 1450 case 'P': /* message priority */ 1451 e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 1452 break; 1453 1454 case 'F': /* flag bits */ 1455 for (p = &bp[1]; *p != '\0'; p++) 1456 { 1457 switch (*p) 1458 { 1459 case 'w': /* warning sent */ 1460 e->e_flags |= EF_WARNING; 1461 break; 1462 1463 case 'r': /* response */ 1464 e->e_flags |= EF_RESPONSE; 1465 break; 1466 1467 case '8': /* has 8 bit data */ 1468 e->e_flags |= EF_HAS8BIT; 1469 break; 1470 } 1471 } 1472 break; 1473 1474 case 'Z': /* original envelope id from ESMTP */ 1475 e->e_envid = newstr(&bp[1]); 1476 break; 1477 1478 case '$': /* define macro */ 1479 define(bp[1], newstr(&bp[2]), e); 1480 break; 1481 1482 case '\0': /* blank line; ignore */ 1483 break; 1484 1485 default: 1486 syserr("readqf: %s: line %d: bad line \"%s\"", 1487 qf, LineNumber, bp); 1488 fclose(qfp); 1489 loseqfile(e, "unrecognized line"); 1490 return FALSE; 1491 } 1492 1493 if (bp != buf) 1494 free(bp); 1495 } 1496 1497 /* 1498 ** If we haven't read any lines, this queue file is empty. 1499 ** Arrange to remove it without referencing any null pointers. 1500 */ 1501 1502 if (LineNumber == 0) 1503 { 1504 errno = 0; 1505 e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 1506 } 1507 else 1508 { 1509 /* 1510 ** Arrange to read the data file. 1511 */ 1512 1513 p = queuename(e, 'd'); 1514 e->e_dfp = fopen(p, "r"); 1515 if (e->e_dfp == NULL) 1516 { 1517 syserr("readqf: cannot open %s", p); 1518 } 1519 else 1520 { 1521 e->e_flags |= EF_HAS_DF; 1522 if (fstat(fileno(e->e_dfp), &st) >= 0) 1523 { 1524 e->e_msgsize = st.st_size; 1525 e->e_dfdev = st.st_dev; 1526 e->e_dfino = st.st_ino; 1527 } 1528 } 1529 } 1530 1531 return TRUE; 1532 } 1533 /* 1534 ** PRINTQUEUE -- print out a representation of the mail queue 1535 ** 1536 ** Parameters: 1537 ** none. 1538 ** 1539 ** Returns: 1540 ** none. 1541 ** 1542 ** Side Effects: 1543 ** Prints a listing of the mail queue on the standard output. 1544 */ 1545 1546 printqueue() 1547 { 1548 register WORK *w; 1549 FILE *f; 1550 int nrequests; 1551 char buf[MAXLINE]; 1552 1553 /* 1554 ** Check for permission to print the queue 1555 */ 1556 1557 if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0) 1558 { 1559 struct stat st; 1560 # ifdef NGROUPS 1561 int n; 1562 GIDSET_T gidset[NGROUPS]; 1563 # endif 1564 1565 if (stat(QueueDir, &st) < 0) 1566 { 1567 syserr("Cannot stat %s", QueueDir); 1568 return; 1569 } 1570 # ifdef NGROUPS 1571 n = getgroups(NGROUPS, gidset); 1572 while (--n >= 0) 1573 { 1574 if (gidset[n] == st.st_gid) 1575 break; 1576 } 1577 if (n < 0) 1578 # else 1579 if (RealGid != st.st_gid) 1580 # endif 1581 { 1582 usrerr("510 You are not permitted to see the queue"); 1583 setstat(EX_NOPERM); 1584 return; 1585 } 1586 } 1587 1588 /* 1589 ** Read and order the queue. 1590 */ 1591 1592 nrequests = orderq(TRUE); 1593 1594 /* 1595 ** Print the work list that we have read. 1596 */ 1597 1598 /* first see if there is anything */ 1599 if (nrequests <= 0) 1600 { 1601 printf("Mail queue is empty\n"); 1602 return; 1603 } 1604 1605 CurrentLA = getla(); /* get load average */ 1606 1607 printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 1608 if (nrequests > WorkListSize) 1609 printf(", only %d printed", WorkListSize); 1610 if (Verbose) 1611 printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 1612 else 1613 printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 1614 for (w = WorkQ; w != NULL; w = w->w_next) 1615 { 1616 struct stat st; 1617 auto time_t submittime = 0; 1618 long dfsize; 1619 int flags = 0; 1620 int qfver; 1621 char statmsg[MAXLINE]; 1622 char bodytype[MAXNAME + 1]; 1623 1624 printf("%8s", w->w_name + 2); 1625 f = fopen(w->w_name, "r"); 1626 if (f == NULL) 1627 { 1628 printf(" (job completed)\n"); 1629 errno = 0; 1630 continue; 1631 } 1632 w->w_name[0] = 'd'; 1633 if (stat(w->w_name, &st) >= 0) 1634 dfsize = st.st_size; 1635 else 1636 dfsize = -1; 1637 if (w->w_lock) 1638 printf("*"); 1639 else if (shouldqueue(w->w_pri, w->w_ctime)) 1640 printf("X"); 1641 else 1642 printf(" "); 1643 errno = 0; 1644 1645 statmsg[0] = bodytype[0] = '\0'; 1646 qfver = 0; 1647 while (fgets(buf, sizeof buf, f) != NULL) 1648 { 1649 register int i; 1650 register char *p; 1651 1652 fixcrlf(buf, TRUE); 1653 switch (buf[0]) 1654 { 1655 case 'V': /* queue file version */ 1656 qfver = atoi(&buf[1]); 1657 break; 1658 1659 case 'M': /* error message */ 1660 if ((i = strlen(&buf[1])) >= sizeof statmsg) 1661 i = sizeof statmsg - 1; 1662 bcopy(&buf[1], statmsg, i); 1663 statmsg[i] = '\0'; 1664 break; 1665 1666 case 'B': /* body type */ 1667 if ((i = strlen(&buf[1])) >= sizeof bodytype) 1668 i = sizeof bodytype - 1; 1669 bcopy(&buf[1], bodytype, i); 1670 bodytype[i] = '\0'; 1671 break; 1672 1673 case 'S': /* sender name */ 1674 if (Verbose) 1675 printf("%8ld %10ld%c%.12s %.38s", 1676 dfsize, 1677 w->w_pri, 1678 bitset(EF_WARNING, flags) ? '+' : ' ', 1679 ctime(&submittime) + 4, 1680 &buf[1]); 1681 else 1682 printf("%8ld %.16s %.45s", dfsize, 1683 ctime(&submittime), &buf[1]); 1684 if (statmsg[0] != '\0' || bodytype[0] != '\0') 1685 { 1686 printf("\n %10.10s", bodytype); 1687 if (statmsg[0] != '\0') 1688 printf(" (%.60s)", statmsg); 1689 } 1690 break; 1691 1692 case 'C': /* controlling user */ 1693 if (Verbose) 1694 printf("\n\t\t\t\t (---%.34s---)", 1695 &buf[1]); 1696 break; 1697 1698 case 'R': /* recipient name */ 1699 p = &buf[1]; 1700 if (qfver >= 1) 1701 { 1702 p = strchr(p, ':'); 1703 if (p == NULL) 1704 break; 1705 p++; 1706 } 1707 if (Verbose) 1708 printf("\n\t\t\t\t\t %.38s", p); 1709 else 1710 printf("\n\t\t\t\t %.45s", p); 1711 break; 1712 1713 case 'T': /* creation time */ 1714 submittime = atol(&buf[1]); 1715 break; 1716 1717 case 'F': /* flag bits */ 1718 for (p = &buf[1]; *p != '\0'; p++) 1719 { 1720 switch (*p) 1721 { 1722 case 'w': 1723 flags |= EF_WARNING; 1724 break; 1725 } 1726 } 1727 } 1728 } 1729 if (submittime == (time_t) 0) 1730 printf(" (no control file)"); 1731 printf("\n"); 1732 (void) fclose(f); 1733 } 1734 } 1735 1736 # endif /* QUEUE */ 1737 /* 1738 ** QUEUENAME -- build a file name in the queue directory for this envelope. 1739 ** 1740 ** Assigns an id code if one does not already exist. 1741 ** This code is very careful to avoid trashing existing files 1742 ** under any circumstances. 1743 ** 1744 ** Parameters: 1745 ** e -- envelope to build it in/from. 1746 ** type -- the file type, used as the first character 1747 ** of the file name. 1748 ** 1749 ** Returns: 1750 ** a pointer to the new file name (in a static buffer). 1751 ** 1752 ** Side Effects: 1753 ** If no id code is already assigned, queuename will 1754 ** assign an id code, create a qf file, and leave a 1755 ** locked, open-for-write file pointer in the envelope. 1756 */ 1757 1758 char * 1759 queuename(e, type) 1760 register ENVELOPE *e; 1761 int type; 1762 { 1763 static int pid = -1; 1764 static char c0; 1765 static char c1; 1766 static char c2; 1767 time_t now; 1768 struct tm *tm; 1769 static char buf[MAXNAME + 1]; 1770 1771 if (e->e_id == NULL) 1772 { 1773 char qf[20]; 1774 1775 /* find a unique id */ 1776 if (pid != getpid()) 1777 { 1778 /* new process -- start back at "AA" */ 1779 pid = getpid(); 1780 now = curtime(); 1781 tm = localtime(&now); 1782 c0 = 'A' + tm->tm_hour; 1783 c1 = 'A'; 1784 c2 = 'A' - 1; 1785 } 1786 (void) sprintf(qf, "qf%cAA%05d", c0, pid); 1787 1788 while (c1 < '~' || c2 < 'Z') 1789 { 1790 int i; 1791 1792 if (c2 >= 'Z') 1793 { 1794 c1++; 1795 c2 = 'A' - 1; 1796 } 1797 qf[3] = c1; 1798 qf[4] = ++c2; 1799 if (tTd(7, 20)) 1800 printf("queuename: trying \"%s\"\n", qf); 1801 1802 i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 1803 if (i < 0) 1804 { 1805 if (errno == EEXIST) 1806 continue; 1807 syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 1808 qf, QueueDir, geteuid()); 1809 exit(EX_UNAVAILABLE); 1810 } 1811 if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB)) 1812 { 1813 e->e_lockfp = fdopen(i, "w"); 1814 break; 1815 } 1816 1817 /* a reader got the file; abandon it and try again */ 1818 (void) close(i); 1819 } 1820 if (c1 >= '~' && c2 >= 'Z') 1821 { 1822 syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 1823 qf, QueueDir, geteuid()); 1824 exit(EX_OSERR); 1825 } 1826 e->e_id = newstr(&qf[2]); 1827 define('i', e->e_id, e); 1828 if (tTd(7, 1)) 1829 printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 1830 if (tTd(7, 9)) 1831 { 1832 printf(" lockfd="); 1833 dumpfd(fileno(e->e_lockfp), TRUE, FALSE); 1834 } 1835 # ifdef LOG 1836 if (LogLevel > 93) 1837 syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 1838 # endif /* LOG */ 1839 } 1840 1841 if (type == '\0') 1842 return (NULL); 1843 (void) sprintf(buf, "%cf%s", type, e->e_id); 1844 if (tTd(7, 2)) 1845 printf("queuename: %s\n", buf); 1846 return (buf); 1847 } 1848 /* 1849 ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 1850 ** 1851 ** Parameters: 1852 ** e -- the envelope to unlock. 1853 ** 1854 ** Returns: 1855 ** none 1856 ** 1857 ** Side Effects: 1858 ** unlocks the queue for `e'. 1859 */ 1860 1861 unlockqueue(e) 1862 ENVELOPE *e; 1863 { 1864 if (tTd(51, 4)) 1865 printf("unlockqueue(%s)\n", e->e_id); 1866 1867 /* if there is a lock file in the envelope, close it */ 1868 if (e->e_lockfp != NULL) 1869 xfclose(e->e_lockfp, "unlockqueue", e->e_id); 1870 e->e_lockfp = NULL; 1871 1872 /* don't create a queue id if we don't already have one */ 1873 if (e->e_id == NULL) 1874 return; 1875 1876 /* remove the transcript */ 1877 # ifdef LOG 1878 if (LogLevel > 87) 1879 syslog(LOG_DEBUG, "%s: unlock", e->e_id); 1880 # endif /* LOG */ 1881 if (!tTd(51, 104)) 1882 xunlink(queuename(e, 'x')); 1883 1884 } 1885 /* 1886 ** SETCTLUSER -- create a controlling address 1887 ** 1888 ** Create a fake "address" given only a local login name; this is 1889 ** used as a "controlling user" for future recipient addresses. 1890 ** 1891 ** Parameters: 1892 ** user -- the user name of the controlling user. 1893 ** 1894 ** Returns: 1895 ** An address descriptor for the controlling user. 1896 ** 1897 ** Side Effects: 1898 ** none. 1899 */ 1900 1901 ADDRESS * 1902 setctluser(user) 1903 char *user; 1904 { 1905 register ADDRESS *a; 1906 struct passwd *pw; 1907 char *p; 1908 1909 /* 1910 ** See if this clears our concept of controlling user. 1911 */ 1912 1913 if (user == NULL || *user == '\0') 1914 return NULL; 1915 1916 /* 1917 ** Set up addr fields for controlling user. 1918 */ 1919 1920 a = (ADDRESS *) xalloc(sizeof *a); 1921 bzero((char *) a, sizeof *a); 1922 1923 p = strchr(user, ':'); 1924 if (p != NULL) 1925 *p++ = '\0'; 1926 if (*user != '\0' && (pw = sm_getpwnam(user)) != NULL) 1927 { 1928 if (strcmp(pw->pw_dir, "/") == 0) 1929 a->q_home = ""; 1930 else 1931 a->q_home = newstr(pw->pw_dir); 1932 a->q_uid = pw->pw_uid; 1933 a->q_gid = pw->pw_gid; 1934 a->q_flags |= QGOODUID; 1935 } 1936 1937 if (*user != '\0') 1938 a->q_user = newstr(user); 1939 else if (p != NULL) 1940 a->q_user = newstr(p); 1941 else 1942 a->q_user = newstr(DefUser); 1943 1944 a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 1945 a->q_mailer = LocalMailer; 1946 if (p == NULL) 1947 a->q_paddr = a->q_user; 1948 else 1949 a->q_paddr = newstr(p); 1950 return a; 1951 } 1952 /* 1953 ** LOSEQFILE -- save the qf as Qf and try to let someone know 1954 ** 1955 ** Parameters: 1956 ** e -- the envelope (e->e_id will be used). 1957 ** why -- reported to whomever can hear. 1958 ** 1959 ** Returns: 1960 ** none. 1961 */ 1962 1963 void 1964 loseqfile(e, why) 1965 register ENVELOPE *e; 1966 char *why; 1967 { 1968 char *p; 1969 char buf[40]; 1970 1971 if (e == NULL || e->e_id == NULL) 1972 return; 1973 if (strlen(e->e_id) > sizeof buf - 4) 1974 return; 1975 strcpy(buf, queuename(e, 'q')); 1976 p = queuename(e, 'Q'); 1977 if (rename(buf, p) < 0) 1978 syserr("cannot rename(%s, %s), uid=%d", buf, p, geteuid()); 1979 #ifdef LOG 1980 else if (LogLevel > 0) 1981 syslog(LOG_ALERT, "Losing %s: %s", buf, why); 1982 #endif 1983 } 1984