1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988 Regents of the University of California. 4 * 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 5.34 (Berkeley) 12/05/91 (with queueing)"; 14 #else 15 static char sccsid[] = "@(#)queue.c 5.34 (Berkeley) 12/05/91 (without queueing)"; 16 #endif 17 #endif /* not lint */ 18 19 # include <sys/stat.h> 20 # include <sys/dir.h> 21 # include <sys/file.h> 22 # include <signal.h> 23 # include <errno.h> 24 # include <pwd.h> 25 26 # ifdef QUEUE 27 28 # ifdef LOCKF 29 # include <unistd.h> 30 # endif 31 32 /* 33 ** Work queue. 34 */ 35 36 struct work 37 { 38 char *w_name; /* name of control file */ 39 long w_pri; /* priority of message, see below */ 40 time_t w_ctime; /* creation time of message */ 41 struct work *w_next; /* next in queue */ 42 }; 43 44 typedef struct work WORK; 45 extern int la; 46 47 WORK *WorkQ; /* queue of things to be done */ 48 /* 49 ** QUEUEUP -- queue a message up for future transmission. 50 ** 51 ** Parameters: 52 ** e -- the envelope to queue up. 53 ** queueall -- if TRUE, queue all addresses, rather than 54 ** just those with the QQUEUEUP flag set. 55 ** announce -- if TRUE, tell when you are queueing up. 56 ** 57 ** Returns: 58 ** locked FILE* to q file 59 ** 60 ** Side Effects: 61 ** The current request are saved in a control file. 62 */ 63 64 FILE * 65 queueup(e, queueall, announce) 66 register ENVELOPE *e; 67 bool queueall; 68 bool announce; 69 { 70 char *qf; 71 char buf[MAXLINE], tf[MAXLINE]; 72 register FILE *tfp; 73 register HDR *h; 74 register ADDRESS *q; 75 MAILER nullmailer; 76 int fd, ret; 77 78 /* 79 ** Create control file. 80 */ 81 82 do 83 { 84 strcpy(tf, queuename(e, 't')); 85 fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode); 86 if (fd < 0) 87 { 88 if (errno != EEXIST) 89 { 90 syserr("queueup: cannot create temp file %s", 91 tf); 92 return NULL; 93 } 94 } 95 else 96 { 97 # ifdef LOCKF 98 if (lockf(fd, F_TLOCK, 0) < 0) 99 { 100 if (errno != EACCES) 101 syserr("cannot lockf(%s)", tf); 102 close(fd); 103 fd = -1; 104 } 105 # else 106 if (flock(fd, LOCK_EX|LOCK_NB) < 0) 107 { 108 if (errno != EWOULDBLOCK) 109 syserr("cannot flock(%s)", tf); 110 close(fd); 111 fd = -1; 112 } 113 # endif 114 } 115 } while (fd < 0); 116 117 tfp = fdopen(fd, "w"); 118 119 if (tTd(40, 1)) 120 printf("queueing %s\n", e->e_id); 121 122 /* 123 ** If there is no data file yet, create one. 124 */ 125 126 if (e->e_df == NULL) 127 { 128 register FILE *dfp; 129 extern putbody(); 130 131 e->e_df = newstr(queuename(e, 'd')); 132 fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode); 133 if (fd < 0) 134 { 135 syserr("queueup: cannot create %s", e->e_df); 136 (void) fclose(tfp); 137 return NULL; 138 } 139 dfp = fdopen(fd, "w"); 140 (*e->e_putbody)(dfp, ProgMailer, e); 141 (void) fclose(dfp); 142 e->e_putbody = putbody; 143 } 144 145 /* 146 ** Output future work requests. 147 ** Priority and creation time should be first, since 148 ** they are required by orderq. 149 */ 150 151 /* output message priority */ 152 fprintf(tfp, "P%ld\n", e->e_msgpriority); 153 154 /* output creation time */ 155 fprintf(tfp, "T%ld\n", e->e_ctime); 156 157 /* output name of data file */ 158 fprintf(tfp, "D%s\n", e->e_df); 159 160 /* message from envelope, if it exists */ 161 if (e->e_message != NULL) 162 fprintf(tfp, "M%s\n", e->e_message); 163 164 /* output name of sender */ 165 fprintf(tfp, "S%s\n", e->e_from.q_paddr); 166 167 /* output list of recipient addresses */ 168 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 169 { 170 if (queueall ? !bitset(QDONTSEND|QSENT, q->q_flags) : 171 bitset(QQUEUEUP, q->q_flags)) 172 { 173 char *ctluser, *getctluser(); 174 175 if ((ctluser = getctluser(q)) != NULL) 176 fprintf(tfp, "C%s\n", ctluser); 177 fprintf(tfp, "R%s\n", q->q_paddr); 178 if (announce) 179 { 180 e->e_to = q->q_paddr; 181 message(Arpa_Info, "queued"); 182 if (LogLevel > 4) 183 logdelivery("queued"); 184 e->e_to = NULL; 185 } 186 if (tTd(40, 1)) 187 { 188 printf("queueing "); 189 printaddr(q, FALSE); 190 } 191 } 192 } 193 194 /* output list of error recipients */ 195 for (q = e->e_errorqueue; q != NULL; q = q->q_next) 196 { 197 if (!bitset(QDONTSEND, q->q_flags)) 198 { 199 char *ctluser, *getctluser(); 200 201 if ((ctluser = getctluser(q)) != NULL) 202 fprintf(tfp, "C%s\n", ctluser); 203 fprintf(tfp, "E%s\n", q->q_paddr); 204 } 205 } 206 207 /* 208 ** Output headers for this message. 209 ** Expand macros completely here. Queue run will deal with 210 ** everything as absolute headers. 211 ** All headers that must be relative to the recipient 212 ** can be cracked later. 213 ** We set up a "null mailer" -- i.e., a mailer that will have 214 ** no effect on the addresses as they are output. 215 */ 216 217 bzero((char *) &nullmailer, sizeof nullmailer); 218 nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1; 219 nullmailer.m_eol = "\n"; 220 221 define('g', "\001f", e); 222 for (h = e->e_header; h != NULL; h = h->h_link) 223 { 224 extern bool bitzerop(); 225 226 /* don't output null headers */ 227 if (h->h_value == NULL || h->h_value[0] == '\0') 228 continue; 229 230 /* don't output resent headers on non-resent messages */ 231 if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 232 continue; 233 234 /* output this header */ 235 fprintf(tfp, "H"); 236 237 /* if conditional, output the set of conditions */ 238 if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 239 { 240 int j; 241 242 (void) putc('?', tfp); 243 for (j = '\0'; j <= '\177'; j++) 244 if (bitnset(j, h->h_mflags)) 245 (void) putc(j, tfp); 246 (void) putc('?', tfp); 247 } 248 249 /* output the header: expand macros, convert addresses */ 250 if (bitset(H_DEFAULT, h->h_flags)) 251 { 252 (void) expand(h->h_value, buf, &buf[sizeof buf], e); 253 fprintf(tfp, "%s: %s\n", h->h_field, buf); 254 } 255 else if (bitset(H_FROM|H_RCPT, h->h_flags)) 256 { 257 commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags), 258 &nullmailer); 259 } 260 else 261 fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 262 } 263 264 /* 265 ** Clean up. 266 */ 267 268 qf = queuename(e, 'q'); 269 if (rename(tf, qf) < 0) 270 syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df); 271 errno = 0; 272 273 # ifdef LOG 274 /* save log info */ 275 if (LogLevel > 15) 276 syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 277 # endif LOG 278 fflush(tfp); 279 return tfp; 280 } 281 /* 282 ** RUNQUEUE -- run the jobs in the queue. 283 ** 284 ** Gets the stuff out of the queue in some presumably logical 285 ** order and processes them. 286 ** 287 ** Parameters: 288 ** forkflag -- TRUE if the queue scanning should be done in 289 ** a child process. We double-fork so it is not our 290 ** child and we don't have to clean up after it. 291 ** 292 ** Returns: 293 ** none. 294 ** 295 ** Side Effects: 296 ** runs things in the mail queue. 297 */ 298 299 runqueue(forkflag) 300 bool forkflag; 301 { 302 extern bool shouldqueue(); 303 304 /* 305 ** If no work will ever be selected, don't even bother reading 306 ** the queue. 307 */ 308 309 la = getla(); /* get load average */ 310 311 if (shouldqueue(-100000000L)) 312 { 313 if (Verbose) 314 printf("Skipping queue run -- load average too high\n"); 315 316 if (forkflag) 317 return; 318 finis(); 319 } 320 321 /* 322 ** See if we want to go off and do other useful work. 323 */ 324 325 if (forkflag) 326 { 327 int pid; 328 329 pid = dofork(); 330 if (pid != 0) 331 { 332 extern void reapchild(); 333 334 /* parent -- pick up intermediate zombie */ 335 #ifndef SIGCHLD 336 (void) waitfor(pid); 337 #else SIGCHLD 338 (void) signal(SIGCHLD, reapchild); 339 #endif SIGCHLD 340 if (QueueIntvl != 0) 341 (void) setevent(QueueIntvl, runqueue, TRUE); 342 return; 343 } 344 /* child -- double fork */ 345 #ifndef SIGCHLD 346 if (fork() != 0) 347 exit(EX_OK); 348 #else SIGCHLD 349 (void) signal(SIGCHLD, SIG_DFL); 350 #endif SIGCHLD 351 } 352 353 setproctitle("running queue: %s", QueueDir); 354 355 # ifdef LOG 356 if (LogLevel > 11) 357 syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid()); 358 # endif LOG 359 360 /* 361 ** Release any resources used by the daemon code. 362 */ 363 364 # ifdef DAEMON 365 clrdaemon(); 366 # endif DAEMON 367 368 /* 369 ** Make sure the alias database is open. 370 */ 371 372 initaliases(AliasFile, FALSE); 373 374 /* 375 ** Start making passes through the queue. 376 ** First, read and sort the entire queue. 377 ** Then, process the work in that order. 378 ** But if you take too long, start over. 379 */ 380 381 /* order the existing work requests */ 382 (void) orderq(FALSE); 383 384 /* process them once at a time */ 385 while (WorkQ != NULL) 386 { 387 WORK *w = WorkQ; 388 389 WorkQ = WorkQ->w_next; 390 dowork(w); 391 free(w->w_name); 392 free((char *) w); 393 } 394 395 /* exit without the usual cleanup */ 396 exit(ExitStat); 397 } 398 /* 399 ** ORDERQ -- order the work queue. 400 ** 401 ** Parameters: 402 ** doall -- if set, include everything in the queue (even 403 ** the jobs that cannot be run because the load 404 ** average is too high). Otherwise, exclude those 405 ** jobs. 406 ** 407 ** Returns: 408 ** The number of request in the queue (not necessarily 409 ** the number of requests in WorkQ however). 410 ** 411 ** Side Effects: 412 ** Sets WorkQ to the queue of available work, in order. 413 */ 414 415 # define NEED_P 001 416 # define NEED_T 002 417 418 orderq(doall) 419 bool doall; 420 { 421 register struct direct *d; 422 register WORK *w; 423 DIR *f; 424 register int i; 425 WORK wlist[QUEUESIZE+1]; 426 int wn = -1; 427 extern workcmpf(); 428 429 /* clear out old WorkQ */ 430 for (w = WorkQ; w != NULL; ) 431 { 432 register WORK *nw = w->w_next; 433 434 WorkQ = nw; 435 free(w->w_name); 436 free((char *) w); 437 w = nw; 438 } 439 440 /* open the queue directory */ 441 f = opendir("."); 442 if (f == NULL) 443 { 444 syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 445 return (0); 446 } 447 448 /* 449 ** Read the work directory. 450 */ 451 452 while ((d = readdir(f)) != NULL) 453 { 454 FILE *cf; 455 char lbuf[MAXNAME]; 456 457 /* is this an interesting entry? */ 458 if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 459 continue; 460 461 /* yes -- open control file (if not too many files) */ 462 if (++wn >= QUEUESIZE) 463 continue; 464 cf = fopen(d->d_name, "r"); 465 if (cf == NULL) 466 { 467 /* this may be some random person sending hir msgs */ 468 /* syserr("orderq: cannot open %s", cbuf); */ 469 if (tTd(41, 2)) 470 printf("orderq: cannot open %s (%d)\n", 471 d->d_name, errno); 472 errno = 0; 473 wn--; 474 continue; 475 } 476 w = &wlist[wn]; 477 w->w_name = newstr(d->d_name); 478 479 /* make sure jobs in creation don't clog queue */ 480 w->w_pri = 0x7fffffff; 481 w->w_ctime = 0; 482 483 /* extract useful information */ 484 i = NEED_P | NEED_T; 485 while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 486 { 487 extern long atol(); 488 489 switch (lbuf[0]) 490 { 491 case 'P': 492 w->w_pri = atol(&lbuf[1]); 493 i &= ~NEED_P; 494 break; 495 496 case 'T': 497 w->w_ctime = atol(&lbuf[1]); 498 i &= ~NEED_T; 499 break; 500 } 501 } 502 (void) fclose(cf); 503 504 if (!doall && shouldqueue(w->w_pri)) 505 { 506 /* don't even bother sorting this job in */ 507 wn--; 508 } 509 } 510 (void) closedir(f); 511 wn++; 512 513 /* 514 ** Sort the work directory. 515 */ 516 517 qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 518 519 /* 520 ** Convert the work list into canonical form. 521 ** Should be turning it into a list of envelopes here perhaps. 522 */ 523 524 WorkQ = NULL; 525 for (i = min(wn, QUEUESIZE); --i >= 0; ) 526 { 527 w = (WORK *) xalloc(sizeof *w); 528 w->w_name = wlist[i].w_name; 529 w->w_pri = wlist[i].w_pri; 530 w->w_ctime = wlist[i].w_ctime; 531 w->w_next = WorkQ; 532 WorkQ = w; 533 } 534 535 if (tTd(40, 1)) 536 { 537 for (w = WorkQ; w != NULL; w = w->w_next) 538 printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 539 } 540 541 return (wn); 542 } 543 /* 544 ** WORKCMPF -- compare function for ordering work. 545 ** 546 ** Parameters: 547 ** a -- the first argument. 548 ** b -- the second argument. 549 ** 550 ** Returns: 551 ** -1 if a < b 552 ** 0 if a == b 553 ** +1 if a > b 554 ** 555 ** Side Effects: 556 ** none. 557 */ 558 559 workcmpf(a, b) 560 register WORK *a; 561 register WORK *b; 562 { 563 long pa = a->w_pri + a->w_ctime; 564 long pb = b->w_pri + b->w_ctime; 565 566 if (pa == pb) 567 return (0); 568 else if (pa > pb) 569 return (1); 570 else 571 return (-1); 572 } 573 /* 574 ** DOWORK -- do a work request. 575 ** 576 ** Parameters: 577 ** w -- the work request to be satisfied. 578 ** 579 ** Returns: 580 ** none. 581 ** 582 ** Side Effects: 583 ** The work request is satisfied if possible. 584 */ 585 586 dowork(w) 587 register WORK *w; 588 { 589 register int i; 590 extern bool shouldqueue(); 591 592 if (tTd(40, 1)) 593 printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 594 595 /* 596 ** Ignore jobs that are too expensive for the moment. 597 */ 598 599 if (shouldqueue(w->w_pri)) 600 { 601 if (Verbose) 602 printf("\nSkipping %s\n", w->w_name + 2); 603 return; 604 } 605 606 /* 607 ** Fork for work. 608 */ 609 610 if (ForkQueueRuns) 611 { 612 i = fork(); 613 if (i < 0) 614 { 615 syserr("dowork: cannot fork"); 616 return; 617 } 618 } 619 else 620 { 621 i = 0; 622 } 623 624 if (i == 0) 625 { 626 FILE *qflock, *readqf(); 627 628 /* 629 ** CHILD 630 ** Lock the control file to avoid duplicate deliveries. 631 ** Then run the file as though we had just read it. 632 ** We save an idea of the temporary name so we 633 ** can recover on interrupt. 634 */ 635 636 /* set basic modes, etc. */ 637 (void) alarm(0); 638 clearenvelope(CurEnv, FALSE); 639 QueueRun = TRUE; 640 ErrorMode = EM_MAIL; 641 CurEnv->e_id = &w->w_name[2]; 642 # ifdef LOG 643 if (LogLevel > 11) 644 syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id, 645 getpid()); 646 # endif LOG 647 648 /* don't use the headers from sendmail.cf... */ 649 CurEnv->e_header = NULL; 650 651 /* read the queue control file */ 652 /* and lock the control file during processing */ 653 if ((qflock = readqf(CurEnv, TRUE)) == NULL) 654 { 655 if (ForkQueueRuns) 656 exit(EX_OK); 657 else 658 return; 659 } 660 661 CurEnv->e_flags |= EF_INQUEUE; 662 eatheader(CurEnv); 663 664 /* do the delivery */ 665 if (!bitset(EF_FATALERRS, CurEnv->e_flags)) 666 sendall(CurEnv, SM_DELIVER); 667 668 /* finish up and exit */ 669 if (ForkQueueRuns) 670 finis(); 671 else 672 dropenvelope(CurEnv); 673 fclose(qflock); 674 } 675 else 676 { 677 /* 678 ** Parent -- pick up results. 679 */ 680 681 errno = 0; 682 (void) waitfor(i); 683 } 684 } 685 /* 686 ** READQF -- read queue file and set up environment. 687 ** 688 ** Parameters: 689 ** e -- the envelope of the job to run. 690 ** full -- if set, read in all information. Otherwise just 691 ** read in info needed for a queue print. 692 ** 693 ** Returns: 694 ** FILE * pointing to flock()ed fd so it can be closed 695 ** after the mail is delivered 696 ** 697 ** Side Effects: 698 ** cf is read and created as the current job, as though 699 ** we had been invoked by argument. 700 */ 701 702 FILE * 703 readqf(e, full) 704 register ENVELOPE *e; 705 bool full; 706 { 707 char *qf; 708 register FILE *qfp; 709 char buf[MAXFIELD]; 710 extern char *fgetfolded(); 711 extern long atol(); 712 int gotctluser = 0; 713 int fd; 714 715 /* 716 ** Read and process the file. 717 */ 718 719 qf = queuename(e, 'q'); 720 qfp = fopen(qf, "r"); 721 if (qfp == NULL) 722 { 723 if (errno != ENOENT) 724 syserr("readqf: no control file %s", qf); 725 return NULL; 726 } 727 728 # ifdef LOCKF 729 if (lockf(fileno(qfp), F_TLOCK, 0) < 0) 730 # else 731 if (flock(fileno(qfp), LOCK_EX|LOCK_NB) < 0) 732 # endif 733 { 734 # ifdef LOG 735 /* being processed by another queuer */ 736 if (Verbose) 737 printf("%s: locked\n", CurEnv->e_id); 738 # endif LOG 739 (void) fclose(qfp); 740 return NULL; 741 } 742 743 /* do basic system initialization */ 744 initsys(); 745 746 FileName = qf; 747 LineNumber = 0; 748 if (Verbose && full) 749 printf("\nRunning %s\n", e->e_id); 750 while (fgetfolded(buf, sizeof buf, qfp) != NULL) 751 { 752 if (tTd(40, 4)) 753 printf("+++++ %s\n", buf); 754 switch (buf[0]) 755 { 756 case 'C': /* specify controlling user */ 757 setctluser(&buf[1]); 758 gotctluser = 1; 759 break; 760 761 case 'R': /* specify recipient */ 762 sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue); 763 break; 764 765 case 'E': /* specify error recipient */ 766 sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_errorqueue); 767 break; 768 769 case 'H': /* header */ 770 if (full) 771 (void) chompheader(&buf[1], FALSE); 772 break; 773 774 case 'M': /* message */ 775 e->e_message = newstr(&buf[1]); 776 break; 777 778 case 'S': /* sender */ 779 setsender(newstr(&buf[1])); 780 break; 781 782 case 'D': /* data file name */ 783 if (!full) 784 break; 785 e->e_df = newstr(&buf[1]); 786 e->e_dfp = fopen(e->e_df, "r"); 787 if (e->e_dfp == NULL) 788 syserr("readqf: cannot open %s", e->e_df); 789 break; 790 791 case 'T': /* init time */ 792 e->e_ctime = atol(&buf[1]); 793 break; 794 795 case 'P': /* message priority */ 796 e->e_msgpriority = atol(&buf[1]) + WkTimeFact; 797 break; 798 799 case '\0': /* blank line; ignore */ 800 break; 801 802 default: 803 syserr("readqf(%s:%d): bad line \"%s\"", e->e_id, 804 LineNumber, buf); 805 break; 806 } 807 /* 808 ** The `C' queue file command operates on the next line, 809 ** so we use "gotctluser" to maintain state as follows: 810 ** 0 - no controlling user, 811 ** 1 - controlling user has been set but not used, 812 ** 2 - controlling user must be used on next iteration. 813 */ 814 if (gotctluser == 1) 815 gotctluser++; 816 else if (gotctluser == 2) 817 { 818 clrctluser(); 819 gotctluser = 0; 820 } 821 } 822 823 /* clear controlling user in case we break out prematurely */ 824 clrctluser(); 825 826 FileName = NULL; 827 828 /* 829 ** If we haven't read any lines, this queue file is empty. 830 ** Arrange to remove it without referencing any null pointers. 831 */ 832 833 if (LineNumber == 0) 834 { 835 errno = 0; 836 e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 837 } 838 return qfp; 839 } 840 /* 841 ** PRINTQUEUE -- print out a representation of the mail queue 842 ** 843 ** Parameters: 844 ** none. 845 ** 846 ** Returns: 847 ** none. 848 ** 849 ** Side Effects: 850 ** Prints a listing of the mail queue on the standard output. 851 */ 852 853 printqueue() 854 { 855 register WORK *w; 856 FILE *f; 857 int nrequests; 858 char buf[MAXLINE]; 859 char cbuf[MAXLINE]; 860 861 /* 862 ** Read and order the queue. 863 */ 864 865 nrequests = orderq(TRUE); 866 867 /* 868 ** Print the work list that we have read. 869 */ 870 871 /* first see if there is anything */ 872 if (nrequests <= 0) 873 { 874 printf("Mail queue is empty\n"); 875 return; 876 } 877 878 la = getla(); /* get load average */ 879 880 printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 881 if (nrequests > QUEUESIZE) 882 printf(", only %d printed", QUEUESIZE); 883 if (Verbose) 884 printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 885 else 886 printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 887 for (w = WorkQ; w != NULL; w = w->w_next) 888 { 889 struct stat st; 890 auto time_t submittime = 0; 891 long dfsize = -1; 892 char message[MAXLINE]; 893 extern bool shouldqueue(); 894 895 f = fopen(w->w_name, "r"); 896 if (f == NULL) 897 { 898 errno = 0; 899 continue; 900 } 901 printf("%7s", w->w_name + 2); 902 # ifdef LOCKF 903 if (lockf(fileno(f), F_TEST, 0) < 0) 904 # else 905 if (flock(fileno(f), LOCK_SH|LOCK_NB) < 0) 906 # endif 907 printf("*"); 908 else if (shouldqueue(w->w_pri)) 909 printf("X"); 910 else 911 printf(" "); 912 errno = 0; 913 914 message[0] = '\0'; 915 cbuf[0] = '\0'; 916 while (fgets(buf, sizeof buf, f) != NULL) 917 { 918 fixcrlf(buf, TRUE); 919 switch (buf[0]) 920 { 921 case 'M': /* error message */ 922 (void) strcpy(message, &buf[1]); 923 break; 924 925 case 'S': /* sender name */ 926 if (Verbose) 927 printf("%8ld %10ld %.12s %.38s", dfsize, 928 w->w_pri, ctime(&submittime) + 4, 929 &buf[1]); 930 else 931 printf("%8ld %.16s %.45s", dfsize, 932 ctime(&submittime), &buf[1]); 933 if (message[0] != '\0') 934 printf("\n\t\t (%.60s)", message); 935 break; 936 case 'C': /* controlling user */ 937 if (strlen(buf) < MAXLINE-3) /* sanity */ 938 (void) strcat(buf, ") "); 939 cbuf[0] = cbuf[1] = '('; 940 (void) strncpy(&cbuf[2], &buf[1], MAXLINE-1); 941 cbuf[MAXLINE-1] = '\0'; 942 break; 943 944 case 'R': /* recipient name */ 945 if (cbuf[0] != '\0') { 946 /* prepend controlling user to `buf' */ 947 (void) strncat(cbuf, &buf[1], 948 MAXLINE-strlen(cbuf)); 949 cbuf[MAXLINE-1] = '\0'; 950 (void) strcpy(buf, cbuf); 951 cbuf[0] = '\0'; 952 } 953 if (Verbose) 954 printf("\n\t\t\t\t\t %.38s", &buf[1]); 955 else 956 printf("\n\t\t\t\t %.45s", &buf[1]); 957 break; 958 959 case 'T': /* creation time */ 960 submittime = atol(&buf[1]); 961 break; 962 963 case 'D': /* data file name */ 964 if (stat(&buf[1], &st) >= 0) 965 dfsize = st.st_size; 966 break; 967 } 968 } 969 if (submittime == (time_t) 0) 970 printf(" (no control file)"); 971 printf("\n"); 972 (void) fclose(f); 973 } 974 } 975 976 # endif QUEUE 977 /* 978 ** QUEUENAME -- build a file name in the queue directory for this envelope. 979 ** 980 ** Assigns an id code if one does not already exist. 981 ** This code is very careful to avoid trashing existing files 982 ** under any circumstances. 983 ** 984 ** Parameters: 985 ** e -- envelope to build it in/from. 986 ** type -- the file type, used as the first character 987 ** of the file name. 988 ** 989 ** Returns: 990 ** a pointer to the new file name (in a static buffer). 991 ** 992 ** Side Effects: 993 ** Will create the qf file if no id code is 994 ** already assigned. This will cause the envelope 995 ** to be modified. 996 */ 997 998 char * 999 queuename(e, type) 1000 register ENVELOPE *e; 1001 char type; 1002 { 1003 static char buf[MAXNAME]; 1004 static int pid = -1; 1005 char c1 = 'A'; 1006 char c2 = 'A'; 1007 1008 if (e->e_id == NULL) 1009 { 1010 char qf[20]; 1011 1012 /* find a unique id */ 1013 if (pid != getpid()) 1014 { 1015 /* new process -- start back at "AA" */ 1016 pid = getpid(); 1017 c1 = 'A'; 1018 c2 = 'A' - 1; 1019 } 1020 (void) sprintf(qf, "qfAA%05d", pid); 1021 1022 while (c1 < '~' || c2 < 'Z') 1023 { 1024 int i; 1025 1026 if (c2 >= 'Z') 1027 { 1028 c1++; 1029 c2 = 'A' - 1; 1030 } 1031 qf[2] = c1; 1032 qf[3] = ++c2; 1033 if (tTd(7, 20)) 1034 printf("queuename: trying \"%s\"\n", qf); 1035 1036 i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 1037 if (i < 0) { 1038 if (errno != EEXIST) { 1039 syserr("queuename: Cannot create \"%s\" in \"%s\"", 1040 qf, QueueDir); 1041 exit(EX_UNAVAILABLE); 1042 } 1043 } else { 1044 (void) close(i); 1045 break; 1046 } 1047 } 1048 if (c1 >= '~' && c2 >= 'Z') 1049 { 1050 syserr("queuename: Cannot create \"%s\" in \"%s\"", 1051 qf, QueueDir); 1052 exit(EX_OSERR); 1053 } 1054 e->e_id = newstr(&qf[2]); 1055 define('i', e->e_id, e); 1056 if (tTd(7, 1)) 1057 printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 1058 # ifdef LOG 1059 if (LogLevel > 16) 1060 syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 1061 # endif LOG 1062 } 1063 1064 if (type == '\0') 1065 return (NULL); 1066 (void) sprintf(buf, "%cf%s", type, e->e_id); 1067 if (tTd(7, 2)) 1068 printf("queuename: %s\n", buf); 1069 return (buf); 1070 } 1071 /* 1072 ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 1073 ** 1074 ** Parameters: 1075 ** e -- the envelope to unlock. 1076 ** 1077 ** Returns: 1078 ** none 1079 ** 1080 ** Side Effects: 1081 ** unlocks the queue for `e'. 1082 */ 1083 1084 unlockqueue(e) 1085 ENVELOPE *e; 1086 { 1087 /* remove the transcript */ 1088 # ifdef LOG 1089 if (LogLevel > 19) 1090 syslog(LOG_DEBUG, "%s: unlock", e->e_id); 1091 # endif LOG 1092 if (!tTd(51, 4)) 1093 xunlink(queuename(e, 'x')); 1094 1095 } 1096 /* 1097 ** GETCTLUSER -- return controlling user if mailing to prog or file 1098 ** 1099 ** Check for a "|" or "/" at the beginning of the address. If 1100 ** found, return a controlling username. 1101 ** 1102 ** Parameters: 1103 ** a - the address to check out 1104 ** 1105 ** Returns: 1106 ** Either NULL, if we werent mailing to a program or file, 1107 ** or a controlling user name (possibly in getpwuid's 1108 ** static buffer). 1109 ** 1110 ** Side Effects: 1111 ** none. 1112 */ 1113 1114 char * 1115 getctluser(a) 1116 ADDRESS *a; 1117 { 1118 extern ADDRESS *getctladdr(); 1119 struct passwd *pw; 1120 char *retstr; 1121 1122 /* 1123 ** Get unquoted user for file, program or user.name check. 1124 ** N.B. remove this code block to always emit controlling 1125 ** addresses (at the expense of backward compatibility). 1126 */ 1127 1128 { 1129 char buf[MAXNAME]; 1130 (void) strncpy(buf, a->q_paddr, MAXNAME); 1131 buf[MAXNAME-1] = '\0'; 1132 stripquotes(buf, TRUE); 1133 1134 if (buf[0] != '|' && buf[0] != '/') 1135 return((char *)NULL); 1136 } 1137 1138 a = getctladdr(a); /* find controlling address */ 1139 1140 if (a != NULL && a->q_uid != 0 && (pw = getpwuid(a->q_uid)) != NULL) 1141 retstr = pw->pw_name; 1142 else /* use default user */ 1143 retstr = DefUser; 1144 1145 if (tTd(40, 5)) 1146 printf("Set controlling user for `%s' to `%s'\n", 1147 (a == NULL)? "<null>": a->q_paddr, retstr); 1148 1149 return(retstr); 1150 } 1151 /* 1152 ** SETCTLUSER - sets `CtlUser' to controlling user 1153 ** CLRCTLUSER - clears controlling user (no params, nothing returned) 1154 ** 1155 ** These routines manipulate `CtlUser'. 1156 ** 1157 ** Parameters: 1158 ** str - controlling user as passed to setctluser() 1159 ** 1160 ** Returns: 1161 ** None. 1162 ** 1163 ** Side Effects: 1164 ** `CtlUser' is changed. 1165 */ 1166 1167 static char CtlUser[MAXNAME]; 1168 1169 setctluser(str) 1170 register char *str; 1171 { 1172 (void) strncpy(CtlUser, str, MAXNAME); 1173 CtlUser[MAXNAME-1] = '\0'; 1174 } 1175 1176 clrctluser() 1177 { 1178 CtlUser[0] = '\0'; 1179 } 1180 1181 /* 1182 ** SETCTLADDR -- create a controlling address 1183 ** 1184 ** If global variable `CtlUser' is set and we are given a valid 1185 ** address, make that address a controlling address; change the 1186 ** `q_uid', `q_gid', and `q_ruser' fields and set QGOODUID. 1187 ** 1188 ** Parameters: 1189 ** a - address for which control uid/gid info may apply 1190 ** 1191 ** Returns: 1192 ** None. 1193 ** 1194 ** Side Effects: 1195 ** Fills in uid/gid fields in address and sets QGOODUID 1196 ** flag if appropriate. 1197 */ 1198 1199 setctladdr(a) 1200 ADDRESS *a; 1201 { 1202 struct passwd *pw; 1203 1204 /* 1205 ** If there is no current controlling user, or we were passed a 1206 ** NULL addr ptr or we already have a controlling user, return. 1207 */ 1208 1209 if (CtlUser[0] == '\0' || a == NULL || a->q_ruser) 1210 return; 1211 1212 /* 1213 ** Set up addr fields for controlling user. If `CtlUser' is no 1214 ** longer valid, use the default user/group. 1215 */ 1216 1217 if ((pw = getpwnam(CtlUser)) != NULL) 1218 { 1219 if (a->q_home) 1220 free(a->q_home); 1221 a->q_home = newstr(pw->pw_dir); 1222 a->q_uid = pw->pw_uid; 1223 a->q_gid = pw->pw_gid; 1224 a->q_ruser = newstr(CtlUser); 1225 } 1226 else 1227 { 1228 a->q_uid = DefUid; 1229 a->q_gid = DefGid; 1230 a->q_ruser = newstr(DefUser); 1231 } 1232 1233 a->q_flags |= QGOODUID; /* flag as a "ctladdr" */ 1234 1235 if (tTd(40, 5)) 1236 printf("Restored controlling user for `%s' to `%s'\n", 1237 a->q_paddr, a->q_ruser); 1238 } 1239