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