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