122708Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 362528Sbostic * Copyright (c) 1988, 1993 462528Sbostic * The Regents of the University of California. All rights reserved. 533731Sbostic * 642829Sbostic * %sccs.include.redist.c% 733731Sbostic */ 822708Sdist 933731Sbostic # include "sendmail.h" 1022708Sdist 1133731Sbostic #ifndef lint 1233731Sbostic #ifdef QUEUE 13*65170Seric static char sccsid[] = "@(#)queue.c 8.32 (Berkeley) 12/17/93 (with queueing)"; 1433731Sbostic #else 15*65170Seric static char sccsid[] = "@(#)queue.c 8.32 (Berkeley) 12/17/93 (without queueing)"; 1633731Sbostic #endif 1733731Sbostic #endif /* not lint */ 1833731Sbostic 194632Seric # include <errno.h> 2040973Sbostic # include <pwd.h> 2157736Seric # include <dirent.h> 224632Seric 2333731Sbostic # ifdef QUEUE 244632Seric 254632Seric /* 269377Seric ** Work queue. 279377Seric */ 289377Seric 299377Seric struct work 309377Seric { 319377Seric char *w_name; /* name of control file */ 329377Seric long w_pri; /* priority of message, see below */ 3325013Seric time_t w_ctime; /* creation time of message */ 349377Seric struct work *w_next; /* next in queue */ 359377Seric }; 369377Seric 379377Seric typedef struct work WORK; 389377Seric 399377Seric WORK *WorkQ; /* queue of things to be done */ 409377Seric /* 414632Seric ** QUEUEUP -- queue a message up for future transmission. 424632Seric ** 434632Seric ** Parameters: 446980Seric ** e -- the envelope to queue up. 456999Seric ** queueall -- if TRUE, queue all addresses, rather than 466999Seric ** just those with the QQUEUEUP flag set. 479377Seric ** announce -- if TRUE, tell when you are queueing up. 484632Seric ** 494632Seric ** Returns: 5051920Seric ** none. 514632Seric ** 524632Seric ** Side Effects: 539377Seric ** The current request are saved in a control file. 5451920Seric ** The queue file is left locked. 554632Seric */ 564632Seric 579377Seric queueup(e, queueall, announce) 586980Seric register ENVELOPE *e; 596999Seric bool queueall; 609377Seric bool announce; 614632Seric { 627812Seric char *qf; 637812Seric register FILE *tfp; 644632Seric register HDR *h; 655007Seric register ADDRESS *q; 6651920Seric int fd; 6751920Seric int i; 6851920Seric bool newid; 6953400Seric register char *p; 7010173Seric MAILER nullmailer; 7151920Seric char buf[MAXLINE], tf[MAXLINE]; 724632Seric 735037Seric /* 7417477Seric ** Create control file. 755037Seric */ 764632Seric 7764745Seric newid = (e->e_id == NULL) || !bitset(EF_INQUEUE, e->e_flags); 7864277Seric 7964277Seric /* if newid, queuename will create a locked qf file in e->lockfp */ 8051920Seric strcpy(tf, queuename(e, 't')); 8151920Seric tfp = e->e_lockfp; 8251920Seric if (tfp == NULL) 8351920Seric newid = FALSE; 8464277Seric 8564277Seric /* if newid, just write the qf file directly (instead of tf file) */ 8664745Seric if (!newid) 8751835Seric { 8851920Seric /* get a locked tf file */ 8964070Seric for (i = 0; i < 128; i++) 9051835Seric { 9151920Seric fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode); 9251920Seric if (fd < 0) 9351835Seric { 9464070Seric if (errno != EEXIST) 9564070Seric break; 9664070Seric #ifdef LOG 9764070Seric if (LogLevel > 0 && (i % 32) == 0) 9865090Seric syslog(LOG_ALERT, "queueup: cannot create %s, uid=%d: %s", 9965090Seric tf, geteuid(), errstring(errno)); 10064070Seric #endif 10141636Srick } 10265090Seric else 10365090Seric { 10465090Seric if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB)) 10565090Seric break; 10664070Seric #ifdef LOG 10765090Seric else if (LogLevel > 0 && (i % 32) == 0) 10865090Seric syslog(LOG_ALERT, "queueup: cannot lock %s: %s", 10965090Seric tf, errstring(errno)); 11064070Seric #endif 11165090Seric close(fd); 11265090Seric } 11358689Seric 11464070Seric if ((i % 32) == 31) 11564070Seric { 11664070Seric /* save the old temp file away */ 11764070Seric (void) rename(tf, queuename(e, 'T')); 11864070Seric } 11964070Seric else 12064070Seric sleep(i % 32); 12141636Srick } 12264724Seric if (fd < 0 || (tfp = fdopen(fd, "w")) == NULL) 12364921Seric { 12464921Seric printopenfds(TRUE); 12565090Seric syserr("!queueup: cannot create queue temp file %s, uid=%d", 12665090Seric tf, geteuid()); 12764921Seric } 12851920Seric } 1294632Seric 1307677Seric if (tTd(40, 1)) 13164745Seric printf("\n>>>>> queueing %s%s >>>>>\n", e->e_id, 13264745Seric newid ? " (new id)" : ""); 13364745Seric if (tTd(40, 9)) 13464745Seric { 13564745Seric printf(" tfp="); 13664745Seric dumpfd(fileno(tfp), TRUE, FALSE); 13764745Seric printf(" lockfp="); 13864745Seric if (e->e_lockfp == NULL) 13964745Seric printf("NULL\n"); 14064745Seric else 14164745Seric dumpfd(fileno(e->e_lockfp), TRUE, FALSE); 14264745Seric } 1434632Seric 1444632Seric /* 1456980Seric ** If there is no data file yet, create one. 1466980Seric */ 1476980Seric 1486980Seric if (e->e_df == NULL) 1496980Seric { 1506980Seric register FILE *dfp; 1519389Seric extern putbody(); 1526980Seric 15364086Seric e->e_df = queuename(e, 'd'); 15464086Seric e->e_df = newstr(e->e_df); 15540934Srick fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode); 15664724Seric if (fd < 0 || (dfp = fdopen(fd, "w")) == NULL) 15765090Seric syserr("!queueup: cannot create data temp file %s, uid=%d", 15865090Seric e->e_df, geteuid()); 15959730Seric (*e->e_putbody)(dfp, FileMailer, e, NULL); 16058680Seric (void) xfclose(dfp, "queueup dfp", e->e_id); 1619389Seric e->e_putbody = putbody; 1626980Seric } 1636980Seric 1646980Seric /* 1654632Seric ** Output future work requests. 16625687Seric ** Priority and creation time should be first, since 16725687Seric ** they are required by orderq. 1684632Seric */ 1694632Seric 1709377Seric /* output message priority */ 1719377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 1729377Seric 1739630Seric /* output creation time */ 1749630Seric fprintf(tfp, "T%ld\n", e->e_ctime); 1759630Seric 17659093Seric /* output type and name of data file */ 17759093Seric if (e->e_bodytype != NULL) 17859093Seric fprintf(tfp, "B%s\n", e->e_bodytype); 1797812Seric fprintf(tfp, "D%s\n", e->e_df); 1804632Seric 18110108Seric /* message from envelope, if it exists */ 18210108Seric if (e->e_message != NULL) 18310108Seric fprintf(tfp, "M%s\n", e->e_message); 18410108Seric 18558737Seric /* send various flag bits through */ 18658737Seric p = buf; 18758737Seric if (bitset(EF_WARNING, e->e_flags)) 18858737Seric *p++ = 'w'; 18958737Seric if (bitset(EF_RESPONSE, e->e_flags)) 19058737Seric *p++ = 'r'; 19158737Seric *p++ = '\0'; 19258737Seric if (buf[0] != '\0') 19358737Seric fprintf(tfp, "F%s\n", buf); 19458737Seric 19558957Seric /* $r and $s and $_ macro values */ 19653400Seric if ((p = macvalue('r', e)) != NULL) 19753400Seric fprintf(tfp, "$r%s\n", p); 19853400Seric if ((p = macvalue('s', e)) != NULL) 19953400Seric fprintf(tfp, "$s%s\n", p); 20058957Seric if ((p = macvalue('_', e)) != NULL) 20158957Seric fprintf(tfp, "$_%s\n", p); 20253400Seric 2034632Seric /* output name of sender */ 2047812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 2054632Seric 20655360Seric /* output list of error recipients */ 20759670Seric printctladdr(NULL, NULL); 20855360Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 20955360Seric { 21058680Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 21155360Seric { 21259113Seric printctladdr(q, tfp); 21355360Seric fprintf(tfp, "E%s\n", q->q_paddr); 21455360Seric } 21555360Seric } 21655360Seric 2174632Seric /* output list of recipient addresses */ 2186980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 2194632Seric { 22058250Seric if (bitset(QQUEUEUP, q->q_flags) || 22158680Seric (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags))) 2228245Seric { 22359113Seric printctladdr(q, tfp); 2247812Seric fprintf(tfp, "R%s\n", q->q_paddr); 2259377Seric if (announce) 2269377Seric { 2279377Seric e->e_to = q->q_paddr; 22858151Seric message("queued"); 22958020Seric if (LogLevel > 8) 23064771Seric logdelivery(NULL, NULL, "queued", NULL, e); 2319377Seric e->e_to = NULL; 2329377Seric } 2339387Seric if (tTd(40, 1)) 2349387Seric { 2359387Seric printf("queueing "); 2369387Seric printaddr(q, FALSE); 2379387Seric } 2388245Seric } 2394632Seric } 2404632Seric 2419377Seric /* 2429377Seric ** Output headers for this message. 2439377Seric ** Expand macros completely here. Queue run will deal with 2449377Seric ** everything as absolute headers. 2459377Seric ** All headers that must be relative to the recipient 2469377Seric ** can be cracked later. 24710173Seric ** We set up a "null mailer" -- i.e., a mailer that will have 24810173Seric ** no effect on the addresses as they are output. 2499377Seric */ 2509377Seric 25110686Seric bzero((char *) &nullmailer, sizeof nullmailer); 25258020Seric nullmailer.m_re_rwset = nullmailer.m_rh_rwset = 25364746Seric nullmailer.m_se_rwset = nullmailer.m_sh_rwset = 0; 25410349Seric nullmailer.m_eol = "\n"; 25510173Seric 25658050Seric define('g', "\201f", e); 2576980Seric for (h = e->e_header; h != NULL; h = h->h_link) 2584632Seric { 25910686Seric extern bool bitzerop(); 26010686Seric 26112015Seric /* don't output null headers */ 2624632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 2634632Seric continue; 26412015Seric 26512015Seric /* don't output resent headers on non-resent messages */ 26612015Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 26712015Seric continue; 26812015Seric 26912015Seric /* output this header */ 2707812Seric fprintf(tfp, "H"); 27112015Seric 27212015Seric /* if conditional, output the set of conditions */ 27310686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 27410686Seric { 27510686Seric int j; 27610686Seric 27723098Seric (void) putc('?', tfp); 27810686Seric for (j = '\0'; j <= '\177'; j++) 27910686Seric if (bitnset(j, h->h_mflags)) 28023098Seric (void) putc(j, tfp); 28123098Seric (void) putc('?', tfp); 28210686Seric } 28312015Seric 28412015Seric /* output the header: expand macros, convert addresses */ 2857763Seric if (bitset(H_DEFAULT, h->h_flags)) 2867763Seric { 2877763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 288*65170Seric if (buf[0] != '\0') 289*65170Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 2907763Seric } 2918245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 2929348Seric { 29358737Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 29463753Seric FILE *savetrace = TrafficLogFile; 29558737Seric 29663753Seric TrafficLogFile = NULL; 29763753Seric 29858737Seric if (bitset(H_FROM, h->h_flags)) 29958737Seric oldstyle = FALSE; 30058737Seric 30158737Seric commaize(h, h->h_value, tfp, oldstyle, 30255012Seric &nullmailer, e); 30363753Seric 30463753Seric TrafficLogFile = savetrace; 3059348Seric } 3067763Seric else 3078245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 3084632Seric } 3094632Seric 3104632Seric /* 3114632Seric ** Clean up. 3124632Seric */ 3134632Seric 31464762Seric if (fflush(tfp) < 0 || fsync(fileno(tfp)) < 0 || ferror(tfp)) 31558732Seric { 31658732Seric if (newid) 31758732Seric syserr("!552 Error writing control file %s", tf); 31858732Seric else 31958732Seric syserr("!452 Error writing control file %s", tf); 32058732Seric } 32158732Seric 32251920Seric if (!newid) 32351920Seric { 32464277Seric /* rename (locked) tf to be (locked) qf */ 32551920Seric qf = queuename(e, 'q'); 32651920Seric if (rename(tf, qf) < 0) 32765090Seric syserr("cannot rename(%s, %s), df=%s, uid=%d", 32865090Seric tf, qf, e->e_df, geteuid()); 32964277Seric 33064277Seric /* close and unlock old (locked) qf */ 33151920Seric if (e->e_lockfp != NULL) 33258680Seric (void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id); 33351920Seric e->e_lockfp = tfp; 33451920Seric } 33551920Seric else 33651920Seric qf = tf; 33741636Srick errno = 0; 33864745Seric e->e_flags |= EF_INQUEUE; 3397391Seric 3407677Seric # ifdef LOG 3417677Seric /* save log info */ 34258020Seric if (LogLevel > 79) 3437878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 34456795Seric # endif /* LOG */ 34564307Seric 34664307Seric if (tTd(40, 1)) 34764307Seric printf("<<<<< done queueing %s <<<<<\n\n", e->e_id); 34851920Seric return; 3494632Seric } 35054974Seric 35154974Seric printctladdr(a, tfp) 35259113Seric register ADDRESS *a; 35354974Seric FILE *tfp; 35454974Seric { 35559113Seric char *uname; 35659113Seric register struct passwd *pw; 35759113Seric register ADDRESS *q; 35859113Seric uid_t uid; 35959113Seric static ADDRESS *lastctladdr; 36059113Seric static uid_t lastuid; 36154974Seric 36259113Seric /* initialization */ 36363850Seric if (a == NULL || a->q_alias == NULL || tfp == NULL) 36454974Seric { 36559670Seric if (lastctladdr != NULL && tfp != NULL) 36659113Seric fprintf(tfp, "C\n"); 36759113Seric lastctladdr = NULL; 36859113Seric lastuid = 0; 36954974Seric return; 37054974Seric } 37159113Seric 37259113Seric /* find the active uid */ 37359113Seric q = getctladdr(a); 37459113Seric if (q == NULL) 37559113Seric uid = 0; 37654974Seric else 37759113Seric uid = q->q_uid; 37863850Seric a = a->q_alias; 37959113Seric 38059113Seric /* check to see if this is the same as last time */ 38159113Seric if (lastctladdr != NULL && uid == lastuid && 38259113Seric strcmp(lastctladdr->q_paddr, a->q_paddr) == 0) 38359113Seric return; 38459113Seric lastuid = uid; 38559113Seric lastctladdr = a; 38659113Seric 38759113Seric if (uid == 0 || (pw = getpwuid(uid)) == NULL) 38859270Seric uname = ""; 38959113Seric else 39059113Seric uname = pw->pw_name; 39159113Seric 39259113Seric fprintf(tfp, "C%s:%s\n", uname, a->q_paddr); 39354974Seric } 39454974Seric 3954632Seric /* 3964632Seric ** RUNQUEUE -- run the jobs in the queue. 3974632Seric ** 3984632Seric ** Gets the stuff out of the queue in some presumably logical 3994632Seric ** order and processes them. 4004632Seric ** 4014632Seric ** Parameters: 40224941Seric ** forkflag -- TRUE if the queue scanning should be done in 40324941Seric ** a child process. We double-fork so it is not our 40424941Seric ** child and we don't have to clean up after it. 4054632Seric ** 4064632Seric ** Returns: 4074632Seric ** none. 4084632Seric ** 4094632Seric ** Side Effects: 4104632Seric ** runs things in the mail queue. 4114632Seric */ 4124632Seric 41355360Seric ENVELOPE QueueEnvelope; /* the queue run envelope */ 41455360Seric 41555360Seric runqueue(forkflag) 4164639Seric bool forkflag; 4174632Seric { 41855360Seric register ENVELOPE *e; 41955360Seric extern ENVELOPE BlankEnvelope; 42024953Seric 4217466Seric /* 42224953Seric ** If no work will ever be selected, don't even bother reading 42324953Seric ** the queue. 42424953Seric */ 42524953Seric 42651920Seric CurrentLA = getla(); /* get load average */ 42740934Srick 42858132Seric if (shouldqueue(0L, curtime())) 42924953Seric { 43024953Seric if (Verbose) 43124953Seric printf("Skipping queue run -- load average too high\n"); 43258107Seric if (forkflag && QueueIntvl != 0) 43358107Seric (void) setevent(QueueIntvl, runqueue, TRUE); 43455360Seric return; 43524953Seric } 43624953Seric 43724953Seric /* 4387466Seric ** See if we want to go off and do other useful work. 4397466Seric */ 4404639Seric 4414639Seric if (forkflag) 4424639Seric { 4437943Seric int pid; 4447943Seric 4457943Seric pid = dofork(); 4467943Seric if (pid != 0) 4474639Seric { 44846928Sbostic extern void reapchild(); 44925184Seric 4507943Seric /* parent -- pick up intermediate zombie */ 45125184Seric #ifndef SIGCHLD 4529377Seric (void) waitfor(pid); 45356795Seric #else /* SIGCHLD */ 45464035Seric (void) setsignal(SIGCHLD, reapchild); 45556795Seric #endif /* SIGCHLD */ 4567690Seric if (QueueIntvl != 0) 4579348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 4584639Seric return; 4594639Seric } 4607943Seric /* child -- double fork */ 46125184Seric #ifndef SIGCHLD 4627943Seric if (fork() != 0) 4637943Seric exit(EX_OK); 46456795Seric #else /* SIGCHLD */ 46564035Seric (void) setsignal(SIGCHLD, SIG_DFL); 46656795Seric #endif /* SIGCHLD */ 4674639Seric } 46824941Seric 46940934Srick setproctitle("running queue: %s", QueueDir); 47024941Seric 4717876Seric # ifdef LOG 47258020Seric if (LogLevel > 69) 47355360Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 47455360Seric QueueDir, getpid(), forkflag); 47556795Seric # endif /* LOG */ 4764639Seric 4777466Seric /* 47810205Seric ** Release any resources used by the daemon code. 47910205Seric */ 48010205Seric 48110205Seric # ifdef DAEMON 48210205Seric clrdaemon(); 48356795Seric # endif /* DAEMON */ 48410205Seric 48564658Seric /* force it to run expensive jobs */ 48664658Seric NoConnect = FALSE; 48764658Seric 48810205Seric /* 48955360Seric ** Create ourselves an envelope 49055360Seric */ 49155360Seric 49255360Seric CurEnv = &QueueEnvelope; 49358179Seric e = newenvelope(&QueueEnvelope, CurEnv); 49455360Seric e->e_flags = BlankEnvelope.e_flags; 49555360Seric 49655360Seric /* 49727175Seric ** Make sure the alias database is open. 49827175Seric */ 49927175Seric 50060537Seric initmaps(FALSE, e); 50127175Seric 50227175Seric /* 5037466Seric ** Start making passes through the queue. 5047466Seric ** First, read and sort the entire queue. 5057466Seric ** Then, process the work in that order. 5067466Seric ** But if you take too long, start over. 5077466Seric */ 5087466Seric 5097943Seric /* order the existing work requests */ 51024954Seric (void) orderq(FALSE); 5117690Seric 5127943Seric /* process them once at a time */ 5137943Seric while (WorkQ != NULL) 5144639Seric { 5157943Seric WORK *w = WorkQ; 5167881Seric 5177943Seric WorkQ = WorkQ->w_next; 51858884Seric 51958884Seric /* 52058884Seric ** Ignore jobs that are too expensive for the moment. 52158884Seric */ 52258884Seric 52358884Seric if (shouldqueue(w->w_pri, w->w_ctime)) 52458884Seric { 52558884Seric if (Verbose) 52658884Seric printf("\nSkipping %s\n", w->w_name + 2); 52758884Seric } 52858930Seric else 52958930Seric { 53064296Seric pid_t pid; 53164296Seric extern pid_t dowork(); 53264296Seric 53364296Seric pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e); 53464296Seric errno = 0; 53564296Seric (void) waitfor(pid); 53658930Seric } 5377943Seric free(w->w_name); 5387943Seric free((char *) w); 5394639Seric } 54029866Seric 54129866Seric /* exit without the usual cleanup */ 54255467Seric e->e_id = NULL; 54355467Seric finis(); 5444634Seric } 5454634Seric /* 5464632Seric ** ORDERQ -- order the work queue. 5474632Seric ** 5484632Seric ** Parameters: 54924941Seric ** doall -- if set, include everything in the queue (even 55024941Seric ** the jobs that cannot be run because the load 55124941Seric ** average is too high). Otherwise, exclude those 55224941Seric ** jobs. 5534632Seric ** 5544632Seric ** Returns: 55510121Seric ** The number of request in the queue (not necessarily 55610121Seric ** the number of requests in WorkQ however). 5574632Seric ** 5584632Seric ** Side Effects: 5594632Seric ** Sets WorkQ to the queue of available work, in order. 5604632Seric */ 5614632Seric 56225687Seric # define NEED_P 001 56325687Seric # define NEED_T 002 56458318Seric # define NEED_R 004 56558318Seric # define NEED_S 010 5664632Seric 56724941Seric orderq(doall) 56824941Seric bool doall; 5694632Seric { 57060219Seric register struct dirent *d; 5714632Seric register WORK *w; 5726625Sglickman DIR *f; 5734632Seric register int i; 57425687Seric WORK wlist[QUEUESIZE+1]; 57510070Seric int wn = -1; 5764632Seric extern workcmpf(); 5774632Seric 57858318Seric if (tTd(41, 1)) 57958318Seric { 58058318Seric printf("orderq:\n"); 58158318Seric if (QueueLimitId != NULL) 58258318Seric printf("\tQueueLimitId = %s\n", QueueLimitId); 58358318Seric if (QueueLimitSender != NULL) 58458318Seric printf("\tQueueLimitSender = %s\n", QueueLimitSender); 58558318Seric if (QueueLimitRecipient != NULL) 58658318Seric printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 58758318Seric } 58858318Seric 5894632Seric /* clear out old WorkQ */ 5904632Seric for (w = WorkQ; w != NULL; ) 5914632Seric { 5924632Seric register WORK *nw = w->w_next; 5934632Seric 5944632Seric WorkQ = nw; 5954632Seric free(w->w_name); 5964632Seric free((char *) w); 5974632Seric w = nw; 5984632Seric } 5994632Seric 6004632Seric /* open the queue directory */ 6018148Seric f = opendir("."); 6024632Seric if (f == NULL) 6034632Seric { 6048148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 60510070Seric return (0); 6064632Seric } 6074632Seric 6084632Seric /* 6094632Seric ** Read the work directory. 6104632Seric */ 6114632Seric 61210070Seric while ((d = readdir(f)) != NULL) 6134632Seric { 6149377Seric FILE *cf; 61564492Seric register char *p; 6164632Seric char lbuf[MAXNAME]; 61758318Seric extern bool strcontainedin(); 6184632Seric 6194632Seric /* is this an interesting entry? */ 6207812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 6214632Seric continue; 6224632Seric 62358318Seric if (QueueLimitId != NULL && 62458318Seric !strcontainedin(QueueLimitId, d->d_name)) 62558318Seric continue; 62658318Seric 62758722Seric /* 62858722Seric ** Check queue name for plausibility. This handles 62958722Seric ** both old and new type ids. 63058722Seric */ 63158722Seric 63264492Seric p = d->d_name + 2; 63364492Seric if (isupper(p[0]) && isupper(p[2])) 63464492Seric p += 3; 63564492Seric else if (isupper(p[1])) 63664492Seric p += 2; 63764492Seric else 63864492Seric p = d->d_name; 63964492Seric for (i = 0; isdigit(*p); p++) 64064492Seric i++; 64164492Seric if (i < 5 || *p != '\0') 64258020Seric { 64358020Seric if (Verbose) 64458020Seric printf("orderq: bogus qf name %s\n", d->d_name); 64558020Seric #ifdef LOG 64658020Seric if (LogLevel > 3) 64759615Seric syslog(LOG_CRIT, "orderq: bogus qf name %s", 64858020Seric d->d_name); 64958020Seric #endif 65058020Seric if (strlen(d->d_name) >= MAXNAME) 65158020Seric d->d_name[MAXNAME - 1] = '\0'; 65258020Seric strcpy(lbuf, d->d_name); 65358020Seric lbuf[0] = 'Q'; 65458020Seric (void) rename(d->d_name, lbuf); 65558020Seric continue; 65658020Seric } 65758020Seric 65810070Seric /* yes -- open control file (if not too many files) */ 65925687Seric if (++wn >= QUEUESIZE) 66010070Seric continue; 66158318Seric 6628148Seric cf = fopen(d->d_name, "r"); 6634632Seric if (cf == NULL) 6644632Seric { 6657055Seric /* this may be some random person sending hir msgs */ 6667055Seric /* syserr("orderq: cannot open %s", cbuf); */ 66710090Seric if (tTd(41, 2)) 66810090Seric printf("orderq: cannot open %s (%d)\n", 66910090Seric d->d_name, errno); 6707055Seric errno = 0; 67110090Seric wn--; 6724632Seric continue; 6734632Seric } 67425687Seric w = &wlist[wn]; 67525687Seric w->w_name = newstr(d->d_name); 6764632Seric 67725027Seric /* make sure jobs in creation don't clog queue */ 67825687Seric w->w_pri = 0x7fffffff; 67925687Seric w->w_ctime = 0; 68025027Seric 6814632Seric /* extract useful information */ 68225687Seric i = NEED_P | NEED_T; 68358318Seric if (QueueLimitSender != NULL) 68458318Seric i |= NEED_S; 68558318Seric if (QueueLimitRecipient != NULL) 68658318Seric i |= NEED_R; 68725687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 6884632Seric { 68924954Seric extern long atol(); 69058318Seric extern bool strcontainedin(); 69124954Seric 69224941Seric switch (lbuf[0]) 6934632Seric { 69424941Seric case 'P': 69525687Seric w->w_pri = atol(&lbuf[1]); 69625687Seric i &= ~NEED_P; 6974632Seric break; 69825013Seric 69925013Seric case 'T': 70025687Seric w->w_ctime = atol(&lbuf[1]); 70125687Seric i &= ~NEED_T; 70225013Seric break; 70358318Seric 70458318Seric case 'R': 70558318Seric if (QueueLimitRecipient != NULL && 70658318Seric strcontainedin(QueueLimitRecipient, &lbuf[1])) 70758318Seric i &= ~NEED_R; 70858318Seric break; 70958318Seric 71058318Seric case 'S': 71158318Seric if (QueueLimitSender != NULL && 71258318Seric strcontainedin(QueueLimitSender, &lbuf[1])) 71358318Seric i &= ~NEED_S; 71458318Seric break; 7154632Seric } 7164632Seric } 7174632Seric (void) fclose(cf); 71824953Seric 71958318Seric if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 72058318Seric bitset(NEED_R|NEED_S, i)) 72124953Seric { 72224953Seric /* don't even bother sorting this job in */ 72324953Seric wn--; 72424953Seric } 7254632Seric } 7266625Sglickman (void) closedir(f); 72710090Seric wn++; 7284632Seric 7294632Seric /* 7304632Seric ** Sort the work directory. 7314632Seric */ 7324632Seric 73325687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 7344632Seric 7354632Seric /* 7364632Seric ** Convert the work list into canonical form. 7379377Seric ** Should be turning it into a list of envelopes here perhaps. 7384632Seric */ 7394632Seric 74024981Seric WorkQ = NULL; 74125687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 7424632Seric { 7434632Seric w = (WORK *) xalloc(sizeof *w); 7444632Seric w->w_name = wlist[i].w_name; 7454632Seric w->w_pri = wlist[i].w_pri; 74625013Seric w->w_ctime = wlist[i].w_ctime; 74724981Seric w->w_next = WorkQ; 74824981Seric WorkQ = w; 7494632Seric } 7504632Seric 7517677Seric if (tTd(40, 1)) 7524632Seric { 7534632Seric for (w = WorkQ; w != NULL; w = w->w_next) 7545037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 7554632Seric } 75610070Seric 75710090Seric return (wn); 7584632Seric } 7594632Seric /* 7607677Seric ** WORKCMPF -- compare function for ordering work. 7614632Seric ** 7624632Seric ** Parameters: 7634632Seric ** a -- the first argument. 7644632Seric ** b -- the second argument. 7654632Seric ** 7664632Seric ** Returns: 76724981Seric ** -1 if a < b 76824981Seric ** 0 if a == b 76924981Seric ** +1 if a > b 7704632Seric ** 7714632Seric ** Side Effects: 7724632Seric ** none. 7734632Seric */ 7744632Seric 7754632Seric workcmpf(a, b) 7765037Seric register WORK *a; 7775037Seric register WORK *b; 7784632Seric { 77957438Seric long pa = a->w_pri; 78057438Seric long pb = b->w_pri; 78124941Seric 78224941Seric if (pa == pb) 7834632Seric return (0); 78424941Seric else if (pa > pb) 78524981Seric return (1); 78624981Seric else 78710121Seric return (-1); 7884632Seric } 7894632Seric /* 7904632Seric ** DOWORK -- do a work request. 7914632Seric ** 7924632Seric ** Parameters: 79358884Seric ** id -- the ID of the job to run. 79458884Seric ** forkflag -- if set, run this in background. 79558924Seric ** requeueflag -- if set, reinstantiate the queue quickly. 79658924Seric ** This is used when expanding aliases in the queue. 79761094Seric ** If forkflag is also set, it doesn't wait for the 79861094Seric ** child. 79958884Seric ** e - the envelope in which to run it. 8004632Seric ** 8014632Seric ** Returns: 80264296Seric ** process id of process that is running the queue job. 8034632Seric ** 8044632Seric ** Side Effects: 8054632Seric ** The work request is satisfied if possible. 8064632Seric */ 8074632Seric 80864296Seric pid_t 80958924Seric dowork(id, forkflag, requeueflag, e) 81058884Seric char *id; 81158884Seric bool forkflag; 81258924Seric bool requeueflag; 81355012Seric register ENVELOPE *e; 8144632Seric { 81564296Seric register pid_t pid; 81651920Seric extern bool readqf(); 8174632Seric 8187677Seric if (tTd(40, 1)) 81958884Seric printf("dowork(%s)\n", id); 8204632Seric 8214632Seric /* 82224941Seric ** Fork for work. 82324941Seric */ 82424941Seric 82558884Seric if (forkflag) 82624941Seric { 82764296Seric pid = fork(); 82864296Seric if (pid < 0) 82924941Seric { 83024941Seric syserr("dowork: cannot fork"); 83164296Seric return 0; 83224941Seric } 83364839Seric else if (pid > 0) 83464839Seric { 83564839Seric /* parent -- clean out connection cache */ 83664839Seric mci_flush(FALSE, NULL); 83764839Seric } 83824941Seric } 83924941Seric else 84024941Seric { 84164296Seric pid = 0; 84224941Seric } 84324941Seric 84464296Seric if (pid == 0) 8454632Seric { 8464632Seric /* 8474632Seric ** CHILD 8488148Seric ** Lock the control file to avoid duplicate deliveries. 8498148Seric ** Then run the file as though we had just read it. 8507350Seric ** We save an idea of the temporary name so we 8517350Seric ** can recover on interrupt. 8524632Seric */ 8534632Seric 8547763Seric /* set basic modes, etc. */ 8557356Seric (void) alarm(0); 85655012Seric clearenvelope(e, FALSE); 85764554Seric e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS; 85858734Seric e->e_errormode = EM_MAIL; 85958884Seric e->e_id = id; 86064765Seric GrabTo = UseErrorsTo = FALSE; 86163846Seric if (forkflag) 86264554Seric { 86364296Seric disconnect(1, e); 86464554Seric OpMode = MD_DELIVER; 86564554Seric } 8667876Seric # ifdef LOG 86758020Seric if (LogLevel > 76) 86855012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 8697881Seric getpid()); 87056795Seric # endif /* LOG */ 8717763Seric 8727763Seric /* don't use the headers from sendmail.cf... */ 87355012Seric e->e_header = NULL; 8747763Seric 87551920Seric /* read the queue control file -- return if locked */ 87663850Seric if (!readqf(e, !requeueflag)) 8776980Seric { 87858884Seric if (tTd(40, 4)) 87958884Seric printf("readqf(%s) failed\n", e->e_id); 88058884Seric if (forkflag) 88124941Seric exit(EX_OK); 88224941Seric else 88324941Seric return; 8846980Seric } 8856980Seric 88655012Seric e->e_flags |= EF_INQUEUE; 88758929Seric eatheader(e, requeueflag); 8886980Seric 88958924Seric if (requeueflag) 89058924Seric queueup(e, TRUE, FALSE); 89158924Seric 8926980Seric /* do the delivery */ 89358915Seric sendall(e, SM_DELIVER); 8946980Seric 8956980Seric /* finish up and exit */ 89658884Seric if (forkflag) 89724941Seric finis(); 89824941Seric else 89955012Seric dropenvelope(e); 9004632Seric } 90164296Seric e->e_id = NULL; 90264296Seric return pid; 9034632Seric } 9044632Seric /* 9054632Seric ** READQF -- read queue file and set up environment. 9064632Seric ** 9074632Seric ** Parameters: 9089377Seric ** e -- the envelope of the job to run. 90963850Seric ** announcefile -- if set, announce the name of the queue 91063850Seric ** file in error messages. 9114632Seric ** 9124632Seric ** Returns: 91351920Seric ** TRUE if it successfully read the queue file. 91451920Seric ** FALSE otherwise. 9154632Seric ** 9164632Seric ** Side Effects: 91751920Seric ** The queue file is returned locked. 9184632Seric */ 9194632Seric 92051920Seric bool 92163850Seric readqf(e, announcefile) 9229377Seric register ENVELOPE *e; 92363850Seric bool announcefile; 9244632Seric { 92517477Seric register FILE *qfp; 92654974Seric ADDRESS *ctladdr; 92756400Seric struct stat st; 92857135Seric char *bp; 92958915Seric char qf[20]; 93057135Seric char buf[MAXLINE]; 93124954Seric extern long atol(); 93254974Seric extern ADDRESS *setctluser(); 9334632Seric 9344632Seric /* 93517468Seric ** Read and process the file. 9364632Seric */ 9374632Seric 93858915Seric strcpy(qf, queuename(e, 'q')); 93951937Seric qfp = fopen(qf, "r+"); 94017477Seric if (qfp == NULL) 94117477Seric { 94258884Seric if (tTd(40, 8)) 94358884Seric printf("readqf(%s): fopen failure (%s)\n", 94458884Seric qf, errstring(errno)); 94540934Srick if (errno != ENOENT) 94640934Srick syserr("readqf: no control file %s", qf); 94751920Seric return FALSE; 94817477Seric } 94940934Srick 95064335Seric if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB)) 95164277Seric { 95264277Seric /* being processed by another queuer */ 95364277Seric if (tTd(40, 8)) 95464277Seric printf("readqf(%s): locked\n", qf); 95564277Seric if (Verbose) 95664277Seric printf("%s: locked\n", e->e_id); 95764277Seric # ifdef LOG 95864277Seric if (LogLevel > 19) 95964277Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 96064277Seric # endif /* LOG */ 96164277Seric (void) fclose(qfp); 96264277Seric return FALSE; 96364277Seric } 96464277Seric 96556400Seric /* 96656400Seric ** Check the queue file for plausibility to avoid attacks. 96756400Seric */ 96856400Seric 96956400Seric if (fstat(fileno(qfp), &st) < 0) 97056400Seric { 97156400Seric /* must have been being processed by someone else */ 97258884Seric if (tTd(40, 8)) 97358884Seric printf("readqf(%s): fstat failure (%s)\n", 97458884Seric qf, errstring(errno)); 97556400Seric fclose(qfp); 97656400Seric return FALSE; 97756400Seric } 97856400Seric 97964137Seric if (st.st_uid != geteuid()) 98056400Seric { 98156400Seric # ifdef LOG 98256400Seric if (LogLevel > 0) 98356400Seric { 98456400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 98556400Seric e->e_id, st.st_uid, st.st_mode); 98656400Seric } 98756795Seric # endif /* LOG */ 98858884Seric if (tTd(40, 8)) 98958884Seric printf("readqf(%s): bogus file\n", qf); 99064277Seric rename(qf, queuename(e, 'Q')); 99156400Seric fclose(qfp); 99256400Seric return FALSE; 99356400Seric } 99456400Seric 99564277Seric if (st.st_size == 0) 99640934Srick { 99764277Seric /* must be a bogus file -- just remove it */ 99864277Seric (void) unlink(qf); 99964277Seric fclose(qfp); 100051920Seric return FALSE; 100140934Srick } 100240934Srick 100364277Seric if (st.st_nlink == 0) 100459101Seric { 100564277Seric /* 100664277Seric ** Race condition -- we got a file just as it was being 100764277Seric ** unlinked. Just assume it is zero length. 100864277Seric */ 100964277Seric 101059101Seric fclose(qfp); 101159101Seric return FALSE; 101259101Seric } 101359101Seric 101464277Seric /* good file -- save this lock */ 101551920Seric e->e_lockfp = qfp; 101651920Seric 101740934Srick /* do basic system initialization */ 101855012Seric initsys(e); 101965164Seric define('i', e->e_id, e); 102040934Srick 102163850Seric if (announcefile) 102263850Seric FileName = qf; 10239377Seric LineNumber = 0; 102463850Seric e->e_flags |= EF_GLOBALERRS; 102563850Seric OpMode = MD_DELIVER; 102651920Seric if (Verbose) 10279377Seric printf("\nRunning %s\n", e->e_id); 102854974Seric ctladdr = NULL; 102957135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 10304632Seric { 103158737Seric register char *p; 103257529Seric struct stat st; 103357529Seric 103426504Seric if (tTd(40, 4)) 103557135Seric printf("+++++ %s\n", bp); 103657135Seric switch (bp[0]) 10374632Seric { 103840973Sbostic case 'C': /* specify controlling user */ 103957135Seric ctladdr = setctluser(&bp[1]); 104040973Sbostic break; 104140973Sbostic 10424632Seric case 'R': /* specify recipient */ 104358082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 10444632Seric break; 10454632Seric 104625687Seric case 'E': /* specify error recipient */ 104758082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 104825687Seric break; 104925687Seric 10504632Seric case 'H': /* header */ 105157135Seric (void) chompheader(&bp[1], FALSE, e); 10524632Seric break; 10534632Seric 105410108Seric case 'M': /* message */ 105564705Seric /* ignore this; we want a new message next time */ 105610108Seric break; 105710108Seric 10584632Seric case 'S': /* sender */ 105958704Seric setsender(newstr(&bp[1]), e, NULL, TRUE); 10604632Seric break; 10614632Seric 106259093Seric case 'B': /* body type */ 106359093Seric e->e_bodytype = newstr(&bp[1]); 106459093Seric break; 106559093Seric 10664632Seric case 'D': /* data file name */ 106757135Seric e->e_df = newstr(&bp[1]); 10689544Seric e->e_dfp = fopen(e->e_df, "r"); 10699544Seric if (e->e_dfp == NULL) 107058020Seric { 10719377Seric syserr("readqf: cannot open %s", e->e_df); 107258020Seric e->e_msgsize = -1; 107358020Seric } 107458020Seric else if (fstat(fileno(e->e_dfp), &st) >= 0) 107557529Seric e->e_msgsize = st.st_size; 10764632Seric break; 10774632Seric 10787860Seric case 'T': /* init time */ 107957135Seric e->e_ctime = atol(&bp[1]); 10804632Seric break; 10814632Seric 10824634Seric case 'P': /* message priority */ 108357135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 10844634Seric break; 10854634Seric 108658737Seric case 'F': /* flag bits */ 108758737Seric for (p = &bp[1]; *p != '\0'; p++) 108858737Seric { 108958737Seric switch (*p) 109058737Seric { 109158737Seric case 'w': /* warning sent */ 109258737Seric e->e_flags |= EF_WARNING; 109358737Seric break; 109458737Seric 109558737Seric case 'r': /* response */ 109658737Seric e->e_flags |= EF_RESPONSE; 109758737Seric break; 109858737Seric } 109958737Seric } 110058737Seric break; 110158737Seric 110253400Seric case '$': /* define macro */ 110357135Seric define(bp[1], newstr(&bp[2]), e); 110453400Seric break; 110553400Seric 110624941Seric case '\0': /* blank line; ignore */ 110724941Seric break; 110824941Seric 11094632Seric default: 111059700Seric syserr("readqf: bad line \"%s\"", e->e_id, 111157135Seric LineNumber, bp); 111263753Seric fclose(qfp); 111363753Seric rename(qf, queuename(e, 'Q')); 111463753Seric return FALSE; 11154632Seric } 111657135Seric 111757135Seric if (bp != buf) 111857135Seric free(bp); 11194632Seric } 11209377Seric 11219377Seric FileName = NULL; 112224941Seric 112324941Seric /* 112424941Seric ** If we haven't read any lines, this queue file is empty. 112524941Seric ** Arrange to remove it without referencing any null pointers. 112624941Seric */ 112724941Seric 112824941Seric if (LineNumber == 0) 112924941Seric { 113024941Seric errno = 0; 113124941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 113224941Seric } 113351920Seric return TRUE; 11344632Seric } 11354632Seric /* 11369630Seric ** PRINTQUEUE -- print out a representation of the mail queue 11379630Seric ** 11389630Seric ** Parameters: 11399630Seric ** none. 11409630Seric ** 11419630Seric ** Returns: 11429630Seric ** none. 11439630Seric ** 11449630Seric ** Side Effects: 11459630Seric ** Prints a listing of the mail queue on the standard output. 11469630Seric */ 11475182Seric 11489630Seric printqueue() 11499630Seric { 11509630Seric register WORK *w; 11519630Seric FILE *f; 115210070Seric int nrequests; 11539630Seric char buf[MAXLINE]; 11549630Seric 11559630Seric /* 115658250Seric ** Check for permission to print the queue 115758250Seric */ 115858250Seric 115964333Seric if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0) 116058250Seric { 116158250Seric struct stat st; 116258523Seric # ifdef NGROUPS 116358523Seric int n; 116463937Seric GIDSET_T gidset[NGROUPS]; 116558523Seric # endif 116658250Seric 116758517Seric if (stat(QueueDir, &st) < 0) 116858250Seric { 116958250Seric syserr("Cannot stat %s", QueueDir); 117058250Seric return; 117158250Seric } 117258523Seric # ifdef NGROUPS 117358523Seric n = getgroups(NGROUPS, gidset); 117458523Seric while (--n >= 0) 117558523Seric { 117658523Seric if (gidset[n] == st.st_gid) 117758523Seric break; 117858523Seric } 117958523Seric if (n < 0) 118058523Seric # else 118163787Seric if (RealGid != st.st_gid) 118258523Seric # endif 118358250Seric { 118458250Seric usrerr("510 You are not permitted to see the queue"); 118558250Seric setstat(EX_NOPERM); 118658250Seric return; 118758250Seric } 118858250Seric } 118958250Seric 119058250Seric /* 11919630Seric ** Read and order the queue. 11929630Seric */ 11939630Seric 119424941Seric nrequests = orderq(TRUE); 11959630Seric 11969630Seric /* 11979630Seric ** Print the work list that we have read. 11989630Seric */ 11999630Seric 12009630Seric /* first see if there is anything */ 120110070Seric if (nrequests <= 0) 12029630Seric { 120310070Seric printf("Mail queue is empty\n"); 12049630Seric return; 12059630Seric } 12069630Seric 120751920Seric CurrentLA = getla(); /* get load average */ 120840934Srick 120910096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 121025687Seric if (nrequests > QUEUESIZE) 121125687Seric printf(", only %d printed", QUEUESIZE); 121224979Seric if (Verbose) 121358716Seric printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 121424979Seric else 121558716Seric printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 12169630Seric for (w = WorkQ; w != NULL; w = w->w_next) 12179630Seric { 12189630Seric struct stat st; 121910070Seric auto time_t submittime = 0; 122010070Seric long dfsize = -1; 122158737Seric int flags = 0; 122210108Seric char message[MAXLINE]; 122359093Seric char bodytype[MAXNAME]; 12249630Seric 122564355Seric printf("%8s", w->w_name + 2); 122617468Seric f = fopen(w->w_name, "r"); 122717468Seric if (f == NULL) 122817468Seric { 122964355Seric printf(" (job completed)\n"); 123017468Seric errno = 0; 123117468Seric continue; 123217468Seric } 123364335Seric if (!lockfile(fileno(f), w->w_name, NULL, LOCK_SH|LOCK_NB)) 123410070Seric printf("*"); 123557438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 123624941Seric printf("X"); 123710070Seric else 123810070Seric printf(" "); 123910070Seric errno = 0; 124017468Seric 124159093Seric message[0] = bodytype[0] = '\0'; 12429630Seric while (fgets(buf, sizeof buf, f) != NULL) 12439630Seric { 124453400Seric register int i; 124558737Seric register char *p; 124653400Seric 12479630Seric fixcrlf(buf, TRUE); 12489630Seric switch (buf[0]) 12499630Seric { 125010108Seric case 'M': /* error message */ 125153400Seric if ((i = strlen(&buf[1])) >= sizeof message) 125258737Seric i = sizeof message - 1; 125353400Seric bcopy(&buf[1], message, i); 125453400Seric message[i] = '\0'; 125510108Seric break; 125610108Seric 125759093Seric case 'B': /* body type */ 125859093Seric if ((i = strlen(&buf[1])) >= sizeof bodytype) 125959093Seric i = sizeof bodytype - 1; 126059093Seric bcopy(&buf[1], bodytype, i); 126159093Seric bodytype[i] = '\0'; 126259093Seric break; 126359093Seric 12649630Seric case 'S': /* sender name */ 126524979Seric if (Verbose) 126658737Seric printf("%8ld %10ld%c%.12s %.38s", 126758737Seric dfsize, 126858737Seric w->w_pri, 126958737Seric bitset(EF_WARNING, flags) ? '+' : ' ', 127058737Seric ctime(&submittime) + 4, 127124979Seric &buf[1]); 127224979Seric else 127324979Seric printf("%8ld %.16s %.45s", dfsize, 127424979Seric ctime(&submittime), &buf[1]); 127559093Seric if (message[0] != '\0' || bodytype[0] != '\0') 127659093Seric { 127759093Seric printf("\n %10.10s", bodytype); 127859093Seric if (message[0] != '\0') 127959093Seric printf(" (%.60s)", message); 128059093Seric } 12819630Seric break; 128251920Seric 128340973Sbostic case 'C': /* controlling user */ 128454974Seric if (Verbose) 128558716Seric printf("\n\t\t\t\t (---%.34s---)", 128658716Seric &buf[1]); 128740973Sbostic break; 12889630Seric 12899630Seric case 'R': /* recipient name */ 129024979Seric if (Verbose) 129158716Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 129224979Seric else 129358716Seric printf("\n\t\t\t\t %.45s", &buf[1]); 12949630Seric break; 12959630Seric 12969630Seric case 'T': /* creation time */ 129724941Seric submittime = atol(&buf[1]); 12989630Seric break; 129910070Seric 130010070Seric case 'D': /* data file name */ 130110070Seric if (stat(&buf[1], &st) >= 0) 130210070Seric dfsize = st.st_size; 130310070Seric break; 130458737Seric 130558737Seric case 'F': /* flag bits */ 130658737Seric for (p = &buf[1]; *p != '\0'; p++) 130758737Seric { 130858737Seric switch (*p) 130958737Seric { 131058737Seric case 'w': 131158737Seric flags |= EF_WARNING; 131258737Seric break; 131358737Seric } 131458737Seric } 13159630Seric } 13169630Seric } 131710070Seric if (submittime == (time_t) 0) 131810070Seric printf(" (no control file)"); 13199630Seric printf("\n"); 132023098Seric (void) fclose(f); 13219630Seric } 13229630Seric } 13239630Seric 132456795Seric # endif /* QUEUE */ 132517468Seric /* 132617468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 132717468Seric ** 132817468Seric ** Assigns an id code if one does not already exist. 132917468Seric ** This code is very careful to avoid trashing existing files 133017468Seric ** under any circumstances. 133117468Seric ** 133217468Seric ** Parameters: 133317468Seric ** e -- envelope to build it in/from. 133417468Seric ** type -- the file type, used as the first character 133517468Seric ** of the file name. 133617468Seric ** 133717468Seric ** Returns: 133817468Seric ** a pointer to the new file name (in a static buffer). 133917468Seric ** 134017468Seric ** Side Effects: 134151920Seric ** If no id code is already assigned, queuename will 134251920Seric ** assign an id code, create a qf file, and leave a 134351920Seric ** locked, open-for-write file pointer in the envelope. 134417468Seric */ 134517468Seric 134617468Seric char * 134717468Seric queuename(e, type) 134817468Seric register ENVELOPE *e; 134959700Seric int type; 135017468Seric { 135117468Seric static int pid = -1; 135258741Seric static char c0; 135358741Seric static char c1; 135458741Seric static char c2; 135558716Seric time_t now; 135658716Seric struct tm *tm; 135758689Seric static char buf[MAXNAME]; 135817468Seric 135917468Seric if (e->e_id == NULL) 136017468Seric { 136117468Seric char qf[20]; 136217468Seric 136317468Seric /* find a unique id */ 136417468Seric if (pid != getpid()) 136517468Seric { 136617468Seric /* new process -- start back at "AA" */ 136717468Seric pid = getpid(); 136858716Seric now = curtime(); 136958716Seric tm = localtime(&now); 137058716Seric c0 = 'A' + tm->tm_hour; 137117468Seric c1 = 'A'; 137217468Seric c2 = 'A' - 1; 137317468Seric } 137458716Seric (void) sprintf(qf, "qf%cAA%05d", c0, pid); 137517468Seric 137617468Seric while (c1 < '~' || c2 < 'Z') 137717468Seric { 137817468Seric int i; 137917468Seric 138017468Seric if (c2 >= 'Z') 138117468Seric { 138217468Seric c1++; 138317468Seric c2 = 'A' - 1; 138417468Seric } 138558716Seric qf[3] = c1; 138658716Seric qf[4] = ++c2; 138717468Seric if (tTd(7, 20)) 138840934Srick printf("queuename: trying \"%s\"\n", qf); 138917468Seric 139040934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 139151920Seric if (i < 0) 139251920Seric { 139351920Seric if (errno == EEXIST) 139451920Seric continue; 139564705Seric syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 139664705Seric qf, QueueDir, geteuid()); 139751920Seric exit(EX_UNAVAILABLE); 139851920Seric } 139964335Seric if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB)) 140051920Seric { 140151920Seric e->e_lockfp = fdopen(i, "w"); 140240934Srick break; 140317468Seric } 140451920Seric 140551920Seric /* a reader got the file; abandon it and try again */ 140651920Seric (void) close(i); 140717468Seric } 140817468Seric if (c1 >= '~' && c2 >= 'Z') 140917468Seric { 141064705Seric syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 141164705Seric qf, QueueDir, geteuid()); 141217468Seric exit(EX_OSERR); 141317468Seric } 141417468Seric e->e_id = newstr(&qf[2]); 141517468Seric define('i', e->e_id, e); 141617468Seric if (tTd(7, 1)) 141717468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 141864745Seric if (tTd(7, 9)) 141964745Seric { 142064745Seric printf(" lockfd="); 142164745Seric dumpfd(fileno(e->e_lockfp), TRUE, FALSE); 142264745Seric } 142317468Seric # ifdef LOG 142458020Seric if (LogLevel > 93) 142517468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 142656795Seric # endif /* LOG */ 142717468Seric } 142817468Seric 142917468Seric if (type == '\0') 143017468Seric return (NULL); 143117468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 143217468Seric if (tTd(7, 2)) 143317468Seric printf("queuename: %s\n", buf); 143417468Seric return (buf); 143517468Seric } 143617468Seric /* 143717468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 143817468Seric ** 143917468Seric ** Parameters: 144017468Seric ** e -- the envelope to unlock. 144117468Seric ** 144217468Seric ** Returns: 144317468Seric ** none 144417468Seric ** 144517468Seric ** Side Effects: 144617468Seric ** unlocks the queue for `e'. 144717468Seric */ 144817468Seric 144917468Seric unlockqueue(e) 145017468Seric ENVELOPE *e; 145117468Seric { 145258680Seric if (tTd(51, 4)) 145358680Seric printf("unlockqueue(%s)\n", e->e_id); 145458680Seric 145551920Seric /* if there is a lock file in the envelope, close it */ 145651920Seric if (e->e_lockfp != NULL) 145758680Seric xfclose(e->e_lockfp, "unlockqueue", e->e_id); 145851920Seric e->e_lockfp = NULL; 145951920Seric 146058728Seric /* don't create a queue id if we don't already have one */ 146158728Seric if (e->e_id == NULL) 146258728Seric return; 146358728Seric 146417468Seric /* remove the transcript */ 146517468Seric # ifdef LOG 146658020Seric if (LogLevel > 87) 146717468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 146856795Seric # endif /* LOG */ 146958680Seric if (!tTd(51, 104)) 147017468Seric xunlink(queuename(e, 'x')); 147117468Seric 147217468Seric } 147340973Sbostic /* 147454974Seric ** SETCTLUSER -- create a controlling address 147540973Sbostic ** 147654974Seric ** Create a fake "address" given only a local login name; this is 147754974Seric ** used as a "controlling user" for future recipient addresses. 147840973Sbostic ** 147940973Sbostic ** Parameters: 148054974Seric ** user -- the user name of the controlling user. 148140973Sbostic ** 148240973Sbostic ** Returns: 148354974Seric ** An address descriptor for the controlling user. 148440973Sbostic ** 148540973Sbostic ** Side Effects: 148640973Sbostic ** none. 148740973Sbostic */ 148840973Sbostic 148954974Seric ADDRESS * 149054974Seric setctluser(user) 149154974Seric char *user; 149240973Sbostic { 149354974Seric register ADDRESS *a; 149440973Sbostic struct passwd *pw; 149559113Seric char *p; 149640973Sbostic 149740973Sbostic /* 149854974Seric ** See if this clears our concept of controlling user. 149940973Sbostic */ 150040973Sbostic 150163850Seric if (user == NULL || *user == '\0') 150263850Seric return NULL; 150340973Sbostic 150440973Sbostic /* 150554974Seric ** Set up addr fields for controlling user. 150640973Sbostic */ 150740973Sbostic 150854974Seric a = (ADDRESS *) xalloc(sizeof *a); 150954974Seric bzero((char *) a, sizeof *a); 151059113Seric 151159113Seric p = strchr(user, ':'); 151259113Seric if (p != NULL) 151359113Seric *p++ = '\0'; 151459270Seric if (*user != '\0' && (pw = getpwnam(user)) != NULL) 151540973Sbostic { 151640973Sbostic a->q_home = newstr(pw->pw_dir); 151740973Sbostic a->q_uid = pw->pw_uid; 151840973Sbostic a->q_gid = pw->pw_gid; 151957642Seric a->q_user = newstr(user); 152059270Seric a->q_flags |= QGOODUID; 152140973Sbostic } 152240973Sbostic else 152340973Sbostic { 152457642Seric a->q_user = newstr(DefUser); 152540973Sbostic } 152640973Sbostic 152759270Seric a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 152856328Seric a->q_mailer = LocalMailer; 152959113Seric if (p == NULL) 153059113Seric a->q_paddr = a->q_user; 153159113Seric else 153259113Seric a->q_paddr = newstr(p); 153354974Seric return a; 153440973Sbostic } 1535