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