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