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