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