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