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.60 (Berkeley) 12/29/94 (with queueing)"; 14 #else 15 static char sccsid[] = "@(#)queue.c 8.60 (Berkeley) 12/29/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 if (q->q_orcpt != NULL) 273 fprintf(tfp, "Q%s\n", q->q_orcpt); 274 putc('R', tfp); 275 if (bitset(QPRIMARY, q->q_flags)) 276 putc('P', tfp); 277 if (bitset(QPINGONSUCCESS, q->q_flags)) 278 putc('S', tfp); 279 if (bitset(QPINGONFAILURE, q->q_flags)) 280 putc('F', tfp); 281 if (bitset(QPINGONDELAY, q->q_flags)) 282 putc('D', tfp); 283 if (bitset(QHAS_RET_PARAM, q->q_flags)) 284 { 285 if (bitset(QRET_HDRS, q->q_flags)) 286 putc('N', tfp); 287 else 288 putc('B', tfp); 289 } 290 putc(':', tfp); 291 fprintf(tfp, "%s\n", q->q_paddr); 292 if (announce) 293 { 294 e->e_to = q->q_paddr; 295 message("queued"); 296 if (LogLevel > 8) 297 logdelivery(NULL, NULL, "queued", 298 NULL, (time_t) 0, e); 299 e->e_to = NULL; 300 } 301 if (tTd(40, 1)) 302 { 303 printf("queueing "); 304 printaddr(q, FALSE); 305 } 306 } 307 } 308 309 /* 310 ** Output headers for this message. 311 ** Expand macros completely here. Queue run will deal with 312 ** everything as absolute headers. 313 ** All headers that must be relative to the recipient 314 ** can be cracked later. 315 ** We set up a "null mailer" -- i.e., a mailer that will have 316 ** no effect on the addresses as they are output. 317 */ 318 319 bzero((char *) &nullmailer, sizeof nullmailer); 320 nullmailer.m_re_rwset = nullmailer.m_rh_rwset = 321 nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1; 322 nullmailer.m_eol = "\n"; 323 bzero(&mcibuf, sizeof mcibuf); 324 mcibuf.mci_mailer = &nullmailer; 325 mcibuf.mci_out = tfp; 326 327 define('g', "\201f", e); 328 for (h = e->e_header; h != NULL; h = h->h_link) 329 { 330 extern bool bitzerop(); 331 332 /* don't output null headers */ 333 if (h->h_value == NULL || h->h_value[0] == '\0') 334 continue; 335 336 /* don't output resent headers on non-resent messages */ 337 if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 338 continue; 339 340 /* expand macros; if null, don't output header at all */ 341 if (bitset(H_DEFAULT, h->h_flags)) 342 { 343 (void) expand(h->h_value, buf, &buf[sizeof buf], e); 344 if (buf[0] == '\0') 345 continue; 346 } 347 348 /* output this header */ 349 fprintf(tfp, "H"); 350 351 /* if conditional, output the set of conditions */ 352 if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 353 { 354 int j; 355 356 (void) putc('?', tfp); 357 for (j = '\0'; j <= '\177'; j++) 358 if (bitnset(j, h->h_mflags)) 359 (void) putc(j, tfp); 360 (void) putc('?', tfp); 361 } 362 363 /* output the header: expand macros, convert addresses */ 364 if (bitset(H_DEFAULT, h->h_flags)) 365 { 366 fprintf(tfp, "%s: %s\n", h->h_field, buf); 367 } 368 else if (bitset(H_FROM|H_RCPT, h->h_flags)) 369 { 370 bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 371 FILE *savetrace = TrafficLogFile; 372 373 TrafficLogFile = NULL; 374 375 if (bitset(H_FROM, h->h_flags)) 376 oldstyle = FALSE; 377 378 commaize(h, h->h_value, oldstyle, &mcibuf, e); 379 380 TrafficLogFile = savetrace; 381 } 382 else 383 fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 384 } 385 386 /* 387 ** Clean up. 388 */ 389 390 if (fflush(tfp) < 0 || fsync(fileno(tfp)) < 0 || ferror(tfp)) 391 { 392 if (newid) 393 syserr("!552 Error writing control file %s", tf); 394 else 395 syserr("!452 Error writing control file %s", tf); 396 } 397 398 if (!newid) 399 { 400 /* rename (locked) tf to be (locked) qf */ 401 qf = queuename(e, 'q'); 402 if (rename(tf, qf) < 0) 403 syserr("cannot rename(%s, %s), df=%s, uid=%d", 404 tf, qf, e->e_df, geteuid()); 405 406 /* close and unlock old (locked) qf */ 407 if (e->e_lockfp != NULL) 408 (void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id); 409 e->e_lockfp = tfp; 410 } 411 else 412 qf = tf; 413 errno = 0; 414 e->e_flags |= EF_INQUEUE; 415 416 # ifdef LOG 417 /* save log info */ 418 if (LogLevel > 79) 419 syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 420 # endif /* LOG */ 421 422 if (tTd(40, 1)) 423 printf("<<<<< done queueing %s <<<<<\n\n", e->e_id); 424 return; 425 } 426 427 printctladdr(a, tfp) 428 register ADDRESS *a; 429 FILE *tfp; 430 { 431 char *uname; 432 register struct passwd *pw; 433 register ADDRESS *q; 434 uid_t uid; 435 static ADDRESS *lastctladdr; 436 static uid_t lastuid; 437 438 /* initialization */ 439 if (a == NULL || a->q_alias == NULL || tfp == NULL) 440 { 441 if (lastctladdr != NULL && tfp != NULL) 442 fprintf(tfp, "C\n"); 443 lastctladdr = NULL; 444 lastuid = 0; 445 return; 446 } 447 448 /* find the active uid */ 449 q = getctladdr(a); 450 if (q == NULL) 451 uid = 0; 452 else 453 uid = q->q_uid; 454 a = a->q_alias; 455 456 /* check to see if this is the same as last time */ 457 if (lastctladdr != NULL && uid == lastuid && 458 strcmp(lastctladdr->q_paddr, a->q_paddr) == 0) 459 return; 460 lastuid = uid; 461 lastctladdr = a; 462 463 if (uid == 0 || (pw = getpwuid(uid)) == NULL) 464 uname = ""; 465 else 466 uname = pw->pw_name; 467 468 fprintf(tfp, "C%s:%s\n", uname, a->q_paddr); 469 } 470 /* 471 ** RUNQUEUE -- run the jobs in the queue. 472 ** 473 ** Gets the stuff out of the queue in some presumably logical 474 ** order and processes them. 475 ** 476 ** Parameters: 477 ** forkflag -- TRUE if the queue scanning should be done in 478 ** a child process. We double-fork so it is not our 479 ** child and we don't have to clean up after it. 480 ** 481 ** Returns: 482 ** none. 483 ** 484 ** Side Effects: 485 ** runs things in the mail queue. 486 */ 487 488 ENVELOPE QueueEnvelope; /* the queue run envelope */ 489 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 1186 /* 1187 ** Read and process the file. 1188 */ 1189 1190 strcpy(qf, queuename(e, 'q')); 1191 qfp = fopen(qf, "r+"); 1192 if (qfp == NULL) 1193 { 1194 if (tTd(40, 8)) 1195 printf("readqf(%s): fopen failure (%s)\n", 1196 qf, errstring(errno)); 1197 if (errno != ENOENT) 1198 syserr("readqf: no control file %s", qf); 1199 return FALSE; 1200 } 1201 1202 if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB)) 1203 { 1204 /* being processed by another queuer */ 1205 if (Verbose || tTd(40, 8)) 1206 printf("%s: locked\n", e->e_id); 1207 # ifdef LOG 1208 if (LogLevel > 19) 1209 syslog(LOG_DEBUG, "%s: locked", e->e_id); 1210 # endif /* LOG */ 1211 (void) fclose(qfp); 1212 return FALSE; 1213 } 1214 1215 /* 1216 ** Check the queue file for plausibility to avoid attacks. 1217 */ 1218 1219 if (fstat(fileno(qfp), &st) < 0) 1220 { 1221 /* must have been being processed by someone else */ 1222 if (tTd(40, 8)) 1223 printf("readqf(%s): fstat failure (%s)\n", 1224 qf, errstring(errno)); 1225 fclose(qfp); 1226 return FALSE; 1227 } 1228 1229 if (st.st_uid != geteuid()) 1230 { 1231 # ifdef LOG 1232 if (LogLevel > 0) 1233 { 1234 syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 1235 e->e_id, st.st_uid, st.st_mode); 1236 } 1237 # endif /* LOG */ 1238 if (tTd(40, 8)) 1239 printf("readqf(%s): bogus file\n", qf); 1240 rename(qf, queuename(e, 'Q')); 1241 fclose(qfp); 1242 return FALSE; 1243 } 1244 1245 if (st.st_size == 0) 1246 { 1247 /* must be a bogus file -- just remove it */ 1248 (void) unlink(qf); 1249 fclose(qfp); 1250 return FALSE; 1251 } 1252 1253 if (st.st_nlink == 0) 1254 { 1255 /* 1256 ** Race condition -- we got a file just as it was being 1257 ** unlinked. Just assume it is zero length. 1258 */ 1259 1260 fclose(qfp); 1261 return FALSE; 1262 } 1263 1264 /* good file -- save this lock */ 1265 e->e_lockfp = qfp; 1266 1267 /* do basic system initialization */ 1268 initsys(e); 1269 define('i', e->e_id, e); 1270 1271 LineNumber = 0; 1272 e->e_flags |= EF_GLOBALERRS; 1273 OpMode = MD_DELIVER; 1274 ctladdr = NULL; 1275 e->e_dfino = -1; 1276 while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 1277 { 1278 register char *p; 1279 struct stat st; 1280 u_long qflags; 1281 ADDRESS *q; 1282 1283 if (tTd(40, 4)) 1284 printf("+++++ %s\n", bp); 1285 switch (bp[0]) 1286 { 1287 case 'V': /* queue file version number */ 1288 qfver = atoi(&bp[1]); 1289 if (qfver > QF_VERSION) 1290 { 1291 syserr("Version number in qf (%d) greater than max (%d)", 1292 qfver, QF_VERSION); 1293 } 1294 break; 1295 1296 case 'C': /* specify controlling user */ 1297 ctladdr = setctluser(&bp[1]); 1298 break; 1299 1300 case 'Q': /* original recipient */ 1301 orcpt = newstr(&bp[1]); 1302 break; 1303 1304 case 'R': /* specify recipient */ 1305 p = bp; 1306 qflags = 0; 1307 if (qfver >= 1) 1308 { 1309 /* get flag bits */ 1310 while (*++p != '\0' && *p != ':') 1311 { 1312 switch (*p) 1313 { 1314 case 'S': 1315 qflags |= QPINGONSUCCESS; 1316 break; 1317 1318 case 'F': 1319 qflags |= QPINGONFAILURE; 1320 break; 1321 1322 case 'D': 1323 qflags |= QPINGONDELAY; 1324 break; 1325 1326 case 'B': 1327 qflags |= QHAS_RET_PARAM; 1328 break; 1329 1330 case 'N': 1331 qflags |= QHAS_RET_PARAM|QRET_HDRS; 1332 break; 1333 1334 case 'P': 1335 qflags |= QPRIMARY; 1336 break; 1337 } 1338 } 1339 } 1340 else 1341 qflags |= QPRIMARY; 1342 q = parseaddr(++p, NULLADDR, RF_COPYALL, '\0', NULL, e); 1343 if (q != NULL) 1344 { 1345 q->q_alias = ctladdr; 1346 q->q_flags |= qflags; 1347 q->q_orcpt = orcpt; 1348 (void) recipient(q, &e->e_sendqueue, 0, e); 1349 } 1350 orcpt = NULL; 1351 break; 1352 1353 case 'E': /* specify error recipient */ 1354 (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, 0, e); 1355 break; 1356 1357 case 'H': /* header */ 1358 (void) chompheader(&bp[1], FALSE, e); 1359 break; 1360 1361 case 'M': /* message */ 1362 /* ignore this; we want a new message next time */ 1363 break; 1364 1365 case 'S': /* sender */ 1366 setsender(newstr(&bp[1]), e, NULL, TRUE); 1367 break; 1368 1369 case 'B': /* body type */ 1370 e->e_bodytype = newstr(&bp[1]); 1371 break; 1372 1373 case 'D': /* data file name */ 1374 e->e_df = newstr(&bp[1]); 1375 e->e_dfp = fopen(e->e_df, "r"); 1376 if (e->e_dfp == NULL) 1377 { 1378 syserr("readqf: cannot open %s", e->e_df); 1379 e->e_msgsize = -1; 1380 } 1381 else if (fstat(fileno(e->e_dfp), &st) >= 0) 1382 { 1383 e->e_msgsize = st.st_size; 1384 e->e_dfdev = st.st_dev; 1385 e->e_dfino = st.st_ino; 1386 } 1387 break; 1388 1389 case 'T': /* init time */ 1390 e->e_ctime = atol(&bp[1]); 1391 break; 1392 1393 case 'I': /* data file's inode number */ 1394 if (e->e_dfino == -1) 1395 e->e_dfino = atol(&buf[1]); 1396 break; 1397 1398 case 'K': /* time of last deliver attempt */ 1399 e->e_dtime = atol(&buf[1]); 1400 break; 1401 1402 case 'N': /* number of delivery attempts */ 1403 e->e_ntries = atoi(&buf[1]); 1404 break; 1405 1406 case 'P': /* message priority */ 1407 e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 1408 break; 1409 1410 case 'F': /* flag bits */ 1411 for (p = &bp[1]; *p != '\0'; p++) 1412 { 1413 switch (*p) 1414 { 1415 case 'w': /* warning sent */ 1416 e->e_flags |= EF_WARNING; 1417 break; 1418 1419 case 'r': /* response */ 1420 e->e_flags |= EF_RESPONSE; 1421 break; 1422 1423 case '8': /* has 8 bit data */ 1424 e->e_flags |= EF_HAS8BIT; 1425 break; 1426 } 1427 } 1428 break; 1429 1430 case 'O': /* original MTS from ESMTP */ 1431 e->e_omts = newstr(&bp[1]); 1432 break; 1433 1434 case 'Z': /* original envelope id from ESMTP */ 1435 e->e_envid = newstr(&bp[1]); 1436 break; 1437 1438 case '$': /* define macro */ 1439 define(bp[1], newstr(&bp[2]), e); 1440 break; 1441 1442 case '\0': /* blank line; ignore */ 1443 break; 1444 1445 default: 1446 syserr("readqf: %s: line %d: bad line \"%s\"", 1447 qf, LineNumber, bp); 1448 fclose(qfp); 1449 rename(qf, queuename(e, 'Q')); 1450 return FALSE; 1451 } 1452 1453 if (bp != buf) 1454 free(bp); 1455 } 1456 1457 /* 1458 ** If we haven't read any lines, this queue file is empty. 1459 ** Arrange to remove it without referencing any null pointers. 1460 */ 1461 1462 if (LineNumber == 0) 1463 { 1464 errno = 0; 1465 e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 1466 } 1467 return TRUE; 1468 } 1469 /* 1470 ** PRINTQUEUE -- print out a representation of the mail queue 1471 ** 1472 ** Parameters: 1473 ** none. 1474 ** 1475 ** Returns: 1476 ** none. 1477 ** 1478 ** Side Effects: 1479 ** Prints a listing of the mail queue on the standard output. 1480 */ 1481 1482 printqueue() 1483 { 1484 register WORK *w; 1485 FILE *f; 1486 int nrequests; 1487 char buf[MAXLINE]; 1488 1489 /* 1490 ** Check for permission to print the queue 1491 */ 1492 1493 if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0) 1494 { 1495 struct stat st; 1496 # ifdef NGROUPS 1497 int n; 1498 GIDSET_T gidset[NGROUPS]; 1499 # endif 1500 1501 if (stat(QueueDir, &st) < 0) 1502 { 1503 syserr("Cannot stat %s", QueueDir); 1504 return; 1505 } 1506 # ifdef NGROUPS 1507 n = getgroups(NGROUPS, gidset); 1508 while (--n >= 0) 1509 { 1510 if (gidset[n] == st.st_gid) 1511 break; 1512 } 1513 if (n < 0) 1514 # else 1515 if (RealGid != st.st_gid) 1516 # endif 1517 { 1518 usrerr("510 You are not permitted to see the queue"); 1519 setstat(EX_NOPERM); 1520 return; 1521 } 1522 } 1523 1524 /* 1525 ** Read and order the queue. 1526 */ 1527 1528 nrequests = orderq(TRUE); 1529 1530 /* 1531 ** Print the work list that we have read. 1532 */ 1533 1534 /* first see if there is anything */ 1535 if (nrequests <= 0) 1536 { 1537 printf("Mail queue is empty\n"); 1538 return; 1539 } 1540 1541 CurrentLA = getla(); /* get load average */ 1542 1543 printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 1544 if (nrequests > QUEUESIZE) 1545 printf(", only %d printed", QUEUESIZE); 1546 if (Verbose) 1547 printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 1548 else 1549 printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 1550 for (w = WorkQ; w != NULL; w = w->w_next) 1551 { 1552 struct stat st; 1553 auto time_t submittime = 0; 1554 long dfsize = -1; 1555 int flags = 0; 1556 int qfver; 1557 char message[MAXLINE]; 1558 char bodytype[MAXNAME]; 1559 1560 printf("%8s", w->w_name + 2); 1561 f = fopen(w->w_name, "r"); 1562 if (f == NULL) 1563 { 1564 printf(" (job completed)\n"); 1565 errno = 0; 1566 continue; 1567 } 1568 if (w->w_lock) 1569 printf("*"); 1570 else if (shouldqueue(w->w_pri, w->w_ctime)) 1571 printf("X"); 1572 else 1573 printf(" "); 1574 errno = 0; 1575 1576 message[0] = bodytype[0] = '\0'; 1577 qfver = 0; 1578 while (fgets(buf, sizeof buf, f) != NULL) 1579 { 1580 register int i; 1581 register char *p; 1582 1583 fixcrlf(buf, TRUE); 1584 switch (buf[0]) 1585 { 1586 case 'V': /* queue file version */ 1587 qfver = atoi(&buf[1]); 1588 break; 1589 1590 case 'M': /* error message */ 1591 if ((i = strlen(&buf[1])) >= sizeof message) 1592 i = sizeof message - 1; 1593 bcopy(&buf[1], message, i); 1594 message[i] = '\0'; 1595 break; 1596 1597 case 'B': /* body type */ 1598 if ((i = strlen(&buf[1])) >= sizeof bodytype) 1599 i = sizeof bodytype - 1; 1600 bcopy(&buf[1], bodytype, i); 1601 bodytype[i] = '\0'; 1602 break; 1603 1604 case 'S': /* sender name */ 1605 if (Verbose) 1606 printf("%8ld %10ld%c%.12s %.38s", 1607 dfsize, 1608 w->w_pri, 1609 bitset(EF_WARNING, flags) ? '+' : ' ', 1610 ctime(&submittime) + 4, 1611 &buf[1]); 1612 else 1613 printf("%8ld %.16s %.45s", dfsize, 1614 ctime(&submittime), &buf[1]); 1615 if (message[0] != '\0' || bodytype[0] != '\0') 1616 { 1617 printf("\n %10.10s", bodytype); 1618 if (message[0] != '\0') 1619 printf(" (%.60s)", message); 1620 } 1621 break; 1622 1623 case 'C': /* controlling user */ 1624 if (Verbose) 1625 printf("\n\t\t\t\t (---%.34s---)", 1626 &buf[1]); 1627 break; 1628 1629 case 'R': /* recipient name */ 1630 p = &buf[1]; 1631 if (qfver >= 1) 1632 { 1633 p = strchr(p, ':'); 1634 if (p == NULL) 1635 break; 1636 p++; 1637 } 1638 if (Verbose) 1639 printf("\n\t\t\t\t\t %.38s", p); 1640 else 1641 printf("\n\t\t\t\t %.45s", p); 1642 break; 1643 1644 case 'T': /* creation time */ 1645 submittime = atol(&buf[1]); 1646 break; 1647 1648 case 'D': /* data file name */ 1649 if (stat(&buf[1], &st) >= 0) 1650 dfsize = st.st_size; 1651 break; 1652 1653 case 'F': /* flag bits */ 1654 for (p = &buf[1]; *p != '\0'; p++) 1655 { 1656 switch (*p) 1657 { 1658 case 'w': 1659 flags |= EF_WARNING; 1660 break; 1661 } 1662 } 1663 } 1664 } 1665 if (submittime == (time_t) 0) 1666 printf(" (no control file)"); 1667 printf("\n"); 1668 (void) fclose(f); 1669 } 1670 } 1671 1672 # endif /* QUEUE */ 1673 /* 1674 ** QUEUENAME -- build a file name in the queue directory for this envelope. 1675 ** 1676 ** Assigns an id code if one does not already exist. 1677 ** This code is very careful to avoid trashing existing files 1678 ** under any circumstances. 1679 ** 1680 ** Parameters: 1681 ** e -- envelope to build it in/from. 1682 ** type -- the file type, used as the first character 1683 ** of the file name. 1684 ** 1685 ** Returns: 1686 ** a pointer to the new file name (in a static buffer). 1687 ** 1688 ** Side Effects: 1689 ** If no id code is already assigned, queuename will 1690 ** assign an id code, create a qf file, and leave a 1691 ** locked, open-for-write file pointer in the envelope. 1692 */ 1693 1694 char * 1695 queuename(e, type) 1696 register ENVELOPE *e; 1697 int type; 1698 { 1699 static int pid = -1; 1700 static char c0; 1701 static char c1; 1702 static char c2; 1703 time_t now; 1704 struct tm *tm; 1705 static char buf[MAXNAME]; 1706 1707 if (e->e_id == NULL) 1708 { 1709 char qf[20]; 1710 1711 /* find a unique id */ 1712 if (pid != getpid()) 1713 { 1714 /* new process -- start back at "AA" */ 1715 pid = getpid(); 1716 now = curtime(); 1717 tm = localtime(&now); 1718 c0 = 'A' + tm->tm_hour; 1719 c1 = 'A'; 1720 c2 = 'A' - 1; 1721 } 1722 (void) sprintf(qf, "qf%cAA%05d", c0, pid); 1723 1724 while (c1 < '~' || c2 < 'Z') 1725 { 1726 int i; 1727 1728 if (c2 >= 'Z') 1729 { 1730 c1++; 1731 c2 = 'A' - 1; 1732 } 1733 qf[3] = c1; 1734 qf[4] = ++c2; 1735 if (tTd(7, 20)) 1736 printf("queuename: trying \"%s\"\n", qf); 1737 1738 i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 1739 if (i < 0) 1740 { 1741 if (errno == EEXIST) 1742 continue; 1743 syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 1744 qf, QueueDir, geteuid()); 1745 exit(EX_UNAVAILABLE); 1746 } 1747 if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB)) 1748 { 1749 e->e_lockfp = fdopen(i, "w"); 1750 break; 1751 } 1752 1753 /* a reader got the file; abandon it and try again */ 1754 (void) close(i); 1755 } 1756 if (c1 >= '~' && c2 >= 'Z') 1757 { 1758 syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 1759 qf, QueueDir, geteuid()); 1760 exit(EX_OSERR); 1761 } 1762 e->e_id = newstr(&qf[2]); 1763 define('i', e->e_id, e); 1764 if (tTd(7, 1)) 1765 printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 1766 if (tTd(7, 9)) 1767 { 1768 printf(" lockfd="); 1769 dumpfd(fileno(e->e_lockfp), TRUE, FALSE); 1770 } 1771 # ifdef LOG 1772 if (LogLevel > 93) 1773 syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 1774 # endif /* LOG */ 1775 } 1776 1777 if (type == '\0') 1778 return (NULL); 1779 (void) sprintf(buf, "%cf%s", type, e->e_id); 1780 if (tTd(7, 2)) 1781 printf("queuename: %s\n", buf); 1782 return (buf); 1783 } 1784 /* 1785 ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 1786 ** 1787 ** Parameters: 1788 ** e -- the envelope to unlock. 1789 ** 1790 ** Returns: 1791 ** none 1792 ** 1793 ** Side Effects: 1794 ** unlocks the queue for `e'. 1795 */ 1796 1797 unlockqueue(e) 1798 ENVELOPE *e; 1799 { 1800 if (tTd(51, 4)) 1801 printf("unlockqueue(%s)\n", e->e_id); 1802 1803 /* if there is a lock file in the envelope, close it */ 1804 if (e->e_lockfp != NULL) 1805 xfclose(e->e_lockfp, "unlockqueue", e->e_id); 1806 e->e_lockfp = NULL; 1807 1808 /* don't create a queue id if we don't already have one */ 1809 if (e->e_id == NULL) 1810 return; 1811 1812 /* remove the transcript */ 1813 # ifdef LOG 1814 if (LogLevel > 87) 1815 syslog(LOG_DEBUG, "%s: unlock", e->e_id); 1816 # endif /* LOG */ 1817 if (!tTd(51, 104)) 1818 xunlink(queuename(e, 'x')); 1819 1820 } 1821 /* 1822 ** SETCTLUSER -- create a controlling address 1823 ** 1824 ** Create a fake "address" given only a local login name; this is 1825 ** used as a "controlling user" for future recipient addresses. 1826 ** 1827 ** Parameters: 1828 ** user -- the user name of the controlling user. 1829 ** 1830 ** Returns: 1831 ** An address descriptor for the controlling user. 1832 ** 1833 ** Side Effects: 1834 ** none. 1835 */ 1836 1837 ADDRESS * 1838 setctluser(user) 1839 char *user; 1840 { 1841 register ADDRESS *a; 1842 struct passwd *pw; 1843 char *p; 1844 1845 /* 1846 ** See if this clears our concept of controlling user. 1847 */ 1848 1849 if (user == NULL || *user == '\0') 1850 return NULL; 1851 1852 /* 1853 ** Set up addr fields for controlling user. 1854 */ 1855 1856 a = (ADDRESS *) xalloc(sizeof *a); 1857 bzero((char *) a, sizeof *a); 1858 1859 p = strchr(user, ':'); 1860 if (p != NULL) 1861 *p++ = '\0'; 1862 if (*user != '\0' && (pw = getpwnam(user)) != NULL) 1863 { 1864 if (strcmp(pw->pw_dir, "/") == 0) 1865 a->q_home = ""; 1866 else 1867 a->q_home = newstr(pw->pw_dir); 1868 a->q_uid = pw->pw_uid; 1869 a->q_gid = pw->pw_gid; 1870 a->q_user = newstr(user); 1871 a->q_flags |= QGOODUID; 1872 } 1873 else 1874 { 1875 a->q_user = newstr(DefUser); 1876 } 1877 1878 a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 1879 a->q_mailer = LocalMailer; 1880 if (p == NULL) 1881 a->q_paddr = a->q_user; 1882 else 1883 a->q_paddr = newstr(p); 1884 return a; 1885 } 1886