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*64658Seric static char sccsid[] = "@(#)queue.c 8.19 (Berkeley) 09/26/93 (with queueing)"; 1433731Sbostic #else 15*64658Seric static char sccsid[] = "@(#)queue.c 8.19 (Berkeley) 09/26/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 7751920Seric newid = (e->e_id == NULL); 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) */ 8651920Seric if (newid) 8751835Seric { 8851920Seric tfp = e->e_lockfp; 8951920Seric } 9051920Seric else 9151920Seric { 9251920Seric /* get a locked tf file */ 9364070Seric for (i = 0; i < 128; i++) 9451835Seric { 9551920Seric fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode); 9651920Seric if (fd < 0) 9751835Seric { 9864070Seric if (errno != EEXIST) 9964070Seric break; 10064070Seric #ifdef LOG 10164070Seric if (LogLevel > 0 && (i % 32) == 0) 10264070Seric syslog(LOG_ALERT, "queueup: cannot create %s: %s", 10364070Seric tf, errstring(errno)); 10464070Seric #endif 10564070Seric continue; 10641636Srick } 10758689Seric 10864335Seric if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB)) 10951920Seric break; 11064070Seric #ifdef LOG 11164070Seric else if (LogLevel > 0 && (i % 32) == 0) 11264070Seric syslog(LOG_ALERT, "queueup: cannot lock %s: %s", 11364070Seric tf, errstring(errno)); 11464070Seric #endif 11558689Seric 11651920Seric close(fd); 11764070Seric 11864070Seric if ((i % 32) == 31) 11964070Seric { 12064070Seric /* save the old temp file away */ 12164070Seric (void) rename(tf, queuename(e, 'T')); 12264070Seric } 12364070Seric else 12464070Seric sleep(i % 32); 12541636Srick } 12658801Seric if (fd < 0) 12764070Seric syserr("!queueup: cannot create temp file %s", tf); 12841636Srick 12951920Seric tfp = fdopen(fd, "w"); 13051920Seric } 1314632Seric 1327677Seric if (tTd(40, 1)) 13364307Seric printf("\n>>>>> queueing %s >>>>>\n", e->e_id); 1344632Seric 1354632Seric /* 1366980Seric ** If there is no data file yet, create one. 1376980Seric */ 1386980Seric 1396980Seric if (e->e_df == NULL) 1406980Seric { 1416980Seric register FILE *dfp; 1429389Seric extern putbody(); 1436980Seric 14464086Seric e->e_df = queuename(e, 'd'); 14564086Seric e->e_df = newstr(e->e_df); 14640934Srick fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode); 14740934Srick if (fd < 0) 14858690Seric syserr("!queueup: cannot create %s", e->e_df); 14940934Srick dfp = fdopen(fd, "w"); 15059730Seric (*e->e_putbody)(dfp, FileMailer, e, NULL); 15158680Seric (void) xfclose(dfp, "queueup dfp", e->e_id); 1529389Seric e->e_putbody = putbody; 1536980Seric } 1546980Seric 1556980Seric /* 1564632Seric ** Output future work requests. 15725687Seric ** Priority and creation time should be first, since 15825687Seric ** they are required by orderq. 1594632Seric */ 1604632Seric 1619377Seric /* output message priority */ 1629377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 1639377Seric 1649630Seric /* output creation time */ 1659630Seric fprintf(tfp, "T%ld\n", e->e_ctime); 1669630Seric 16759093Seric /* output type and name of data file */ 16859093Seric if (e->e_bodytype != NULL) 16959093Seric fprintf(tfp, "B%s\n", e->e_bodytype); 1707812Seric fprintf(tfp, "D%s\n", e->e_df); 1714632Seric 17210108Seric /* message from envelope, if it exists */ 17310108Seric if (e->e_message != NULL) 17410108Seric fprintf(tfp, "M%s\n", e->e_message); 17510108Seric 17658737Seric /* send various flag bits through */ 17758737Seric p = buf; 17858737Seric if (bitset(EF_WARNING, e->e_flags)) 17958737Seric *p++ = 'w'; 18058737Seric if (bitset(EF_RESPONSE, e->e_flags)) 18158737Seric *p++ = 'r'; 18258737Seric *p++ = '\0'; 18358737Seric if (buf[0] != '\0') 18458737Seric fprintf(tfp, "F%s\n", buf); 18558737Seric 18658957Seric /* $r and $s and $_ macro values */ 18753400Seric if ((p = macvalue('r', e)) != NULL) 18853400Seric fprintf(tfp, "$r%s\n", p); 18953400Seric if ((p = macvalue('s', e)) != NULL) 19053400Seric fprintf(tfp, "$s%s\n", p); 19158957Seric if ((p = macvalue('_', e)) != NULL) 19258957Seric fprintf(tfp, "$_%s\n", p); 19353400Seric 1944632Seric /* output name of sender */ 1957812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 1964632Seric 19755360Seric /* output list of error recipients */ 19859670Seric printctladdr(NULL, NULL); 19955360Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 20055360Seric { 20158680Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 20255360Seric { 20359113Seric printctladdr(q, tfp); 20455360Seric fprintf(tfp, "E%s\n", q->q_paddr); 20555360Seric } 20655360Seric } 20755360Seric 2084632Seric /* output list of recipient addresses */ 2096980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 2104632Seric { 21158250Seric if (bitset(QQUEUEUP, q->q_flags) || 21258680Seric (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags))) 2138245Seric { 21459113Seric printctladdr(q, tfp); 2157812Seric fprintf(tfp, "R%s\n", q->q_paddr); 2169377Seric if (announce) 2179377Seric { 2189377Seric e->e_to = q->q_paddr; 21958151Seric message("queued"); 22058020Seric if (LogLevel > 8) 22158337Seric logdelivery(NULL, NULL, "queued", e); 2229377Seric e->e_to = NULL; 2239377Seric } 2249387Seric if (tTd(40, 1)) 2259387Seric { 2269387Seric printf("queueing "); 2279387Seric printaddr(q, FALSE); 2289387Seric } 2298245Seric } 2304632Seric } 2314632Seric 2329377Seric /* 2339377Seric ** Output headers for this message. 2349377Seric ** Expand macros completely here. Queue run will deal with 2359377Seric ** everything as absolute headers. 2369377Seric ** All headers that must be relative to the recipient 2379377Seric ** can be cracked later. 23810173Seric ** We set up a "null mailer" -- i.e., a mailer that will have 23910173Seric ** no effect on the addresses as they are output. 2409377Seric */ 2419377Seric 24210686Seric bzero((char *) &nullmailer, sizeof nullmailer); 24358020Seric nullmailer.m_re_rwset = nullmailer.m_rh_rwset = 24458020Seric nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1; 24510349Seric nullmailer.m_eol = "\n"; 24610173Seric 24758050Seric define('g', "\201f", e); 2486980Seric for (h = e->e_header; h != NULL; h = h->h_link) 2494632Seric { 25010686Seric extern bool bitzerop(); 25110686Seric 25212015Seric /* don't output null headers */ 2534632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 2544632Seric continue; 25512015Seric 25612015Seric /* don't output resent headers on non-resent messages */ 25712015Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 25812015Seric continue; 25912015Seric 26012015Seric /* output this header */ 2617812Seric fprintf(tfp, "H"); 26212015Seric 26312015Seric /* if conditional, output the set of conditions */ 26410686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 26510686Seric { 26610686Seric int j; 26710686Seric 26823098Seric (void) putc('?', tfp); 26910686Seric for (j = '\0'; j <= '\177'; j++) 27010686Seric if (bitnset(j, h->h_mflags)) 27123098Seric (void) putc(j, tfp); 27223098Seric (void) putc('?', tfp); 27310686Seric } 27412015Seric 27512015Seric /* output the header: expand macros, convert addresses */ 2767763Seric if (bitset(H_DEFAULT, h->h_flags)) 2777763Seric { 2787763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 2798236Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 2807763Seric } 2818245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 2829348Seric { 28358737Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 28463753Seric FILE *savetrace = TrafficLogFile; 28558737Seric 28663753Seric TrafficLogFile = NULL; 28763753Seric 28858737Seric if (bitset(H_FROM, h->h_flags)) 28958737Seric oldstyle = FALSE; 29058737Seric 29158737Seric commaize(h, h->h_value, tfp, oldstyle, 29255012Seric &nullmailer, e); 29363753Seric 29463753Seric TrafficLogFile = savetrace; 2959348Seric } 2967763Seric else 2978245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 2984632Seric } 2994632Seric 3004632Seric /* 3014632Seric ** Clean up. 3024632Seric */ 3034632Seric 30458732Seric fflush(tfp); 30560603Seric fsync(fileno(tfp)); 30658732Seric if (ferror(tfp)) 30758732Seric { 30858732Seric if (newid) 30958732Seric syserr("!552 Error writing control file %s", tf); 31058732Seric else 31158732Seric syserr("!452 Error writing control file %s", tf); 31258732Seric } 31358732Seric 31451920Seric if (!newid) 31551920Seric { 31664277Seric /* rename (locked) tf to be (locked) qf */ 31751920Seric qf = queuename(e, 'q'); 31851920Seric if (rename(tf, qf) < 0) 31951920Seric syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df); 32064277Seric 32164277Seric /* close and unlock old (locked) qf */ 32251920Seric if (e->e_lockfp != NULL) 32358680Seric (void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id); 32451920Seric e->e_lockfp = tfp; 32551920Seric } 32651920Seric else 32751920Seric qf = tf; 32841636Srick errno = 0; 3297391Seric 3307677Seric # ifdef LOG 3317677Seric /* save log info */ 33258020Seric if (LogLevel > 79) 3337878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 33456795Seric # endif /* LOG */ 33564307Seric 33664307Seric if (tTd(40, 1)) 33764307Seric printf("<<<<< done queueing %s <<<<<\n\n", e->e_id); 33851920Seric return; 3394632Seric } 34054974Seric 34154974Seric printctladdr(a, tfp) 34259113Seric register ADDRESS *a; 34354974Seric FILE *tfp; 34454974Seric { 34559113Seric char *uname; 34659113Seric register struct passwd *pw; 34759113Seric register ADDRESS *q; 34859113Seric uid_t uid; 34959113Seric static ADDRESS *lastctladdr; 35059113Seric static uid_t lastuid; 35154974Seric 35259113Seric /* initialization */ 35363850Seric if (a == NULL || a->q_alias == NULL || tfp == NULL) 35454974Seric { 35559670Seric if (lastctladdr != NULL && tfp != NULL) 35659113Seric fprintf(tfp, "C\n"); 35759113Seric lastctladdr = NULL; 35859113Seric lastuid = 0; 35954974Seric return; 36054974Seric } 36159113Seric 36259113Seric /* find the active uid */ 36359113Seric q = getctladdr(a); 36459113Seric if (q == NULL) 36559113Seric uid = 0; 36654974Seric else 36759113Seric uid = q->q_uid; 36863850Seric a = a->q_alias; 36959113Seric 37059113Seric /* check to see if this is the same as last time */ 37159113Seric if (lastctladdr != NULL && uid == lastuid && 37259113Seric strcmp(lastctladdr->q_paddr, a->q_paddr) == 0) 37359113Seric return; 37459113Seric lastuid = uid; 37559113Seric lastctladdr = a; 37659113Seric 37759113Seric if (uid == 0 || (pw = getpwuid(uid)) == NULL) 37859270Seric uname = ""; 37959113Seric else 38059113Seric uname = pw->pw_name; 38159113Seric 38259113Seric fprintf(tfp, "C%s:%s\n", uname, a->q_paddr); 38354974Seric } 38454974Seric 3854632Seric /* 3864632Seric ** RUNQUEUE -- run the jobs in the queue. 3874632Seric ** 3884632Seric ** Gets the stuff out of the queue in some presumably logical 3894632Seric ** order and processes them. 3904632Seric ** 3914632Seric ** Parameters: 39224941Seric ** forkflag -- TRUE if the queue scanning should be done in 39324941Seric ** a child process. We double-fork so it is not our 39424941Seric ** child and we don't have to clean up after it. 3954632Seric ** 3964632Seric ** Returns: 3974632Seric ** none. 3984632Seric ** 3994632Seric ** Side Effects: 4004632Seric ** runs things in the mail queue. 4014632Seric */ 4024632Seric 40355360Seric ENVELOPE QueueEnvelope; /* the queue run envelope */ 40455360Seric 40555360Seric runqueue(forkflag) 4064639Seric bool forkflag; 4074632Seric { 40855360Seric register ENVELOPE *e; 40955360Seric extern ENVELOPE BlankEnvelope; 41024953Seric 4117466Seric /* 41224953Seric ** If no work will ever be selected, don't even bother reading 41324953Seric ** the queue. 41424953Seric */ 41524953Seric 41651920Seric CurrentLA = getla(); /* get load average */ 41740934Srick 41858132Seric if (shouldqueue(0L, curtime())) 41924953Seric { 42024953Seric if (Verbose) 42124953Seric printf("Skipping queue run -- load average too high\n"); 42258107Seric if (forkflag && QueueIntvl != 0) 42358107Seric (void) setevent(QueueIntvl, runqueue, TRUE); 42455360Seric return; 42524953Seric } 42624953Seric 42724953Seric /* 4287466Seric ** See if we want to go off and do other useful work. 4297466Seric */ 4304639Seric 4314639Seric if (forkflag) 4324639Seric { 4337943Seric int pid; 4347943Seric 4357943Seric pid = dofork(); 4367943Seric if (pid != 0) 4374639Seric { 43846928Sbostic extern void reapchild(); 43925184Seric 4407943Seric /* parent -- pick up intermediate zombie */ 44125184Seric #ifndef SIGCHLD 4429377Seric (void) waitfor(pid); 44356795Seric #else /* SIGCHLD */ 44464035Seric (void) setsignal(SIGCHLD, reapchild); 44556795Seric #endif /* SIGCHLD */ 4467690Seric if (QueueIntvl != 0) 4479348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 4484639Seric return; 4494639Seric } 4507943Seric /* child -- double fork */ 45125184Seric #ifndef SIGCHLD 4527943Seric if (fork() != 0) 4537943Seric exit(EX_OK); 45456795Seric #else /* SIGCHLD */ 45564035Seric (void) setsignal(SIGCHLD, SIG_DFL); 45656795Seric #endif /* SIGCHLD */ 4574639Seric } 45824941Seric 45940934Srick setproctitle("running queue: %s", QueueDir); 46024941Seric 4617876Seric # ifdef LOG 46258020Seric if (LogLevel > 69) 46355360Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 46455360Seric QueueDir, getpid(), forkflag); 46556795Seric # endif /* LOG */ 4664639Seric 4677466Seric /* 46810205Seric ** Release any resources used by the daemon code. 46910205Seric */ 47010205Seric 47110205Seric # ifdef DAEMON 47210205Seric clrdaemon(); 47356795Seric # endif /* DAEMON */ 47410205Seric 475*64658Seric /* force it to run expensive jobs */ 476*64658Seric NoConnect = FALSE; 477*64658Seric 47810205Seric /* 47955360Seric ** Create ourselves an envelope 48055360Seric */ 48155360Seric 48255360Seric CurEnv = &QueueEnvelope; 48358179Seric e = newenvelope(&QueueEnvelope, CurEnv); 48455360Seric e->e_flags = BlankEnvelope.e_flags; 48555360Seric 48655360Seric /* 48727175Seric ** Make sure the alias database is open. 48827175Seric */ 48927175Seric 49060537Seric initmaps(FALSE, e); 49127175Seric 49227175Seric /* 4937466Seric ** Start making passes through the queue. 4947466Seric ** First, read and sort the entire queue. 4957466Seric ** Then, process the work in that order. 4967466Seric ** But if you take too long, start over. 4977466Seric */ 4987466Seric 4997943Seric /* order the existing work requests */ 50024954Seric (void) orderq(FALSE); 5017690Seric 5027943Seric /* process them once at a time */ 5037943Seric while (WorkQ != NULL) 5044639Seric { 5057943Seric WORK *w = WorkQ; 5067881Seric 5077943Seric WorkQ = WorkQ->w_next; 50858884Seric 50958884Seric /* 51058884Seric ** Ignore jobs that are too expensive for the moment. 51158884Seric */ 51258884Seric 51358884Seric if (shouldqueue(w->w_pri, w->w_ctime)) 51458884Seric { 51558884Seric if (Verbose) 51658884Seric printf("\nSkipping %s\n", w->w_name + 2); 51758884Seric } 51858930Seric else 51958930Seric { 52064296Seric pid_t pid; 52164296Seric extern pid_t dowork(); 52264296Seric 52364296Seric pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e); 52464296Seric errno = 0; 52564296Seric (void) waitfor(pid); 52658930Seric } 5277943Seric free(w->w_name); 5287943Seric free((char *) w); 5294639Seric } 53029866Seric 53129866Seric /* exit without the usual cleanup */ 53255467Seric e->e_id = NULL; 53355467Seric finis(); 5344634Seric } 5354634Seric /* 5364632Seric ** ORDERQ -- order the work queue. 5374632Seric ** 5384632Seric ** Parameters: 53924941Seric ** doall -- if set, include everything in the queue (even 54024941Seric ** the jobs that cannot be run because the load 54124941Seric ** average is too high). Otherwise, exclude those 54224941Seric ** jobs. 5434632Seric ** 5444632Seric ** Returns: 54510121Seric ** The number of request in the queue (not necessarily 54610121Seric ** the number of requests in WorkQ however). 5474632Seric ** 5484632Seric ** Side Effects: 5494632Seric ** Sets WorkQ to the queue of available work, in order. 5504632Seric */ 5514632Seric 55225687Seric # define NEED_P 001 55325687Seric # define NEED_T 002 55458318Seric # define NEED_R 004 55558318Seric # define NEED_S 010 5564632Seric 55724941Seric orderq(doall) 55824941Seric bool doall; 5594632Seric { 56060219Seric register struct dirent *d; 5614632Seric register WORK *w; 5626625Sglickman DIR *f; 5634632Seric register int i; 56425687Seric WORK wlist[QUEUESIZE+1]; 56510070Seric int wn = -1; 5664632Seric extern workcmpf(); 5674632Seric 56858318Seric if (tTd(41, 1)) 56958318Seric { 57058318Seric printf("orderq:\n"); 57158318Seric if (QueueLimitId != NULL) 57258318Seric printf("\tQueueLimitId = %s\n", QueueLimitId); 57358318Seric if (QueueLimitSender != NULL) 57458318Seric printf("\tQueueLimitSender = %s\n", QueueLimitSender); 57558318Seric if (QueueLimitRecipient != NULL) 57658318Seric printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 57758318Seric } 57858318Seric 5794632Seric /* clear out old WorkQ */ 5804632Seric for (w = WorkQ; w != NULL; ) 5814632Seric { 5824632Seric register WORK *nw = w->w_next; 5834632Seric 5844632Seric WorkQ = nw; 5854632Seric free(w->w_name); 5864632Seric free((char *) w); 5874632Seric w = nw; 5884632Seric } 5894632Seric 5904632Seric /* open the queue directory */ 5918148Seric f = opendir("."); 5924632Seric if (f == NULL) 5934632Seric { 5948148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 59510070Seric return (0); 5964632Seric } 5974632Seric 5984632Seric /* 5994632Seric ** Read the work directory. 6004632Seric */ 6014632Seric 60210070Seric while ((d = readdir(f)) != NULL) 6034632Seric { 6049377Seric FILE *cf; 60564492Seric register char *p; 6064632Seric char lbuf[MAXNAME]; 60758318Seric extern bool strcontainedin(); 6084632Seric 6094632Seric /* is this an interesting entry? */ 6107812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 6114632Seric continue; 6124632Seric 61358318Seric if (QueueLimitId != NULL && 61458318Seric !strcontainedin(QueueLimitId, d->d_name)) 61558318Seric continue; 61658318Seric 61758722Seric /* 61858722Seric ** Check queue name for plausibility. This handles 61958722Seric ** both old and new type ids. 62058722Seric */ 62158722Seric 62264492Seric p = d->d_name + 2; 62364492Seric if (isupper(p[0]) && isupper(p[2])) 62464492Seric p += 3; 62564492Seric else if (isupper(p[1])) 62664492Seric p += 2; 62764492Seric else 62864492Seric p = d->d_name; 62964492Seric for (i = 0; isdigit(*p); p++) 63064492Seric i++; 63164492Seric if (i < 5 || *p != '\0') 63258020Seric { 63358020Seric if (Verbose) 63458020Seric printf("orderq: bogus qf name %s\n", d->d_name); 63558020Seric #ifdef LOG 63658020Seric if (LogLevel > 3) 63759615Seric syslog(LOG_CRIT, "orderq: bogus qf name %s", 63858020Seric d->d_name); 63958020Seric #endif 64058020Seric if (strlen(d->d_name) >= MAXNAME) 64158020Seric d->d_name[MAXNAME - 1] = '\0'; 64258020Seric strcpy(lbuf, d->d_name); 64358020Seric lbuf[0] = 'Q'; 64458020Seric (void) rename(d->d_name, lbuf); 64558020Seric continue; 64658020Seric } 64758020Seric 64810070Seric /* yes -- open control file (if not too many files) */ 64925687Seric if (++wn >= QUEUESIZE) 65010070Seric continue; 65158318Seric 6528148Seric cf = fopen(d->d_name, "r"); 6534632Seric if (cf == NULL) 6544632Seric { 6557055Seric /* this may be some random person sending hir msgs */ 6567055Seric /* syserr("orderq: cannot open %s", cbuf); */ 65710090Seric if (tTd(41, 2)) 65810090Seric printf("orderq: cannot open %s (%d)\n", 65910090Seric d->d_name, errno); 6607055Seric errno = 0; 66110090Seric wn--; 6624632Seric continue; 6634632Seric } 66425687Seric w = &wlist[wn]; 66525687Seric w->w_name = newstr(d->d_name); 6664632Seric 66725027Seric /* make sure jobs in creation don't clog queue */ 66825687Seric w->w_pri = 0x7fffffff; 66925687Seric w->w_ctime = 0; 67025027Seric 6714632Seric /* extract useful information */ 67225687Seric i = NEED_P | NEED_T; 67358318Seric if (QueueLimitSender != NULL) 67458318Seric i |= NEED_S; 67558318Seric if (QueueLimitRecipient != NULL) 67658318Seric i |= NEED_R; 67725687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 6784632Seric { 67924954Seric extern long atol(); 68058318Seric extern bool strcontainedin(); 68124954Seric 68224941Seric switch (lbuf[0]) 6834632Seric { 68424941Seric case 'P': 68525687Seric w->w_pri = atol(&lbuf[1]); 68625687Seric i &= ~NEED_P; 6874632Seric break; 68825013Seric 68925013Seric case 'T': 69025687Seric w->w_ctime = atol(&lbuf[1]); 69125687Seric i &= ~NEED_T; 69225013Seric break; 69358318Seric 69458318Seric case 'R': 69558318Seric if (QueueLimitRecipient != NULL && 69658318Seric strcontainedin(QueueLimitRecipient, &lbuf[1])) 69758318Seric i &= ~NEED_R; 69858318Seric break; 69958318Seric 70058318Seric case 'S': 70158318Seric if (QueueLimitSender != NULL && 70258318Seric strcontainedin(QueueLimitSender, &lbuf[1])) 70358318Seric i &= ~NEED_S; 70458318Seric break; 7054632Seric } 7064632Seric } 7074632Seric (void) fclose(cf); 70824953Seric 70958318Seric if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 71058318Seric bitset(NEED_R|NEED_S, i)) 71124953Seric { 71224953Seric /* don't even bother sorting this job in */ 71324953Seric wn--; 71424953Seric } 7154632Seric } 7166625Sglickman (void) closedir(f); 71710090Seric wn++; 7184632Seric 7194632Seric /* 7204632Seric ** Sort the work directory. 7214632Seric */ 7224632Seric 72325687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 7244632Seric 7254632Seric /* 7264632Seric ** Convert the work list into canonical form. 7279377Seric ** Should be turning it into a list of envelopes here perhaps. 7284632Seric */ 7294632Seric 73024981Seric WorkQ = NULL; 73125687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 7324632Seric { 7334632Seric w = (WORK *) xalloc(sizeof *w); 7344632Seric w->w_name = wlist[i].w_name; 7354632Seric w->w_pri = wlist[i].w_pri; 73625013Seric w->w_ctime = wlist[i].w_ctime; 73724981Seric w->w_next = WorkQ; 73824981Seric WorkQ = w; 7394632Seric } 7404632Seric 7417677Seric if (tTd(40, 1)) 7424632Seric { 7434632Seric for (w = WorkQ; w != NULL; w = w->w_next) 7445037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 7454632Seric } 74610070Seric 74710090Seric return (wn); 7484632Seric } 7494632Seric /* 7507677Seric ** WORKCMPF -- compare function for ordering work. 7514632Seric ** 7524632Seric ** Parameters: 7534632Seric ** a -- the first argument. 7544632Seric ** b -- the second argument. 7554632Seric ** 7564632Seric ** Returns: 75724981Seric ** -1 if a < b 75824981Seric ** 0 if a == b 75924981Seric ** +1 if a > b 7604632Seric ** 7614632Seric ** Side Effects: 7624632Seric ** none. 7634632Seric */ 7644632Seric 7654632Seric workcmpf(a, b) 7665037Seric register WORK *a; 7675037Seric register WORK *b; 7684632Seric { 76957438Seric long pa = a->w_pri; 77057438Seric long pb = b->w_pri; 77124941Seric 77224941Seric if (pa == pb) 7734632Seric return (0); 77424941Seric else if (pa > pb) 77524981Seric return (1); 77624981Seric else 77710121Seric return (-1); 7784632Seric } 7794632Seric /* 7804632Seric ** DOWORK -- do a work request. 7814632Seric ** 7824632Seric ** Parameters: 78358884Seric ** id -- the ID of the job to run. 78458884Seric ** forkflag -- if set, run this in background. 78558924Seric ** requeueflag -- if set, reinstantiate the queue quickly. 78658924Seric ** This is used when expanding aliases in the queue. 78761094Seric ** If forkflag is also set, it doesn't wait for the 78861094Seric ** child. 78958884Seric ** e - the envelope in which to run it. 7904632Seric ** 7914632Seric ** Returns: 79264296Seric ** process id of process that is running the queue job. 7934632Seric ** 7944632Seric ** Side Effects: 7954632Seric ** The work request is satisfied if possible. 7964632Seric */ 7974632Seric 79864296Seric pid_t 79958924Seric dowork(id, forkflag, requeueflag, e) 80058884Seric char *id; 80158884Seric bool forkflag; 80258924Seric bool requeueflag; 80355012Seric register ENVELOPE *e; 8044632Seric { 80564296Seric register pid_t pid; 80651920Seric extern bool readqf(); 8074632Seric 8087677Seric if (tTd(40, 1)) 80958884Seric printf("dowork(%s)\n", id); 8104632Seric 8114632Seric /* 81224941Seric ** Fork for work. 81324941Seric */ 81424941Seric 81558884Seric if (forkflag) 81624941Seric { 81764296Seric pid = fork(); 81864296Seric if (pid < 0) 81924941Seric { 82024941Seric syserr("dowork: cannot fork"); 82164296Seric return 0; 82224941Seric } 82324941Seric } 82424941Seric else 82524941Seric { 82664296Seric pid = 0; 82724941Seric } 82824941Seric 82964296Seric if (pid == 0) 8304632Seric { 8314632Seric /* 8324632Seric ** CHILD 8338148Seric ** Lock the control file to avoid duplicate deliveries. 8348148Seric ** Then run the file as though we had just read it. 8357350Seric ** We save an idea of the temporary name so we 8367350Seric ** can recover on interrupt. 8374632Seric */ 8384632Seric 8397763Seric /* set basic modes, etc. */ 8407356Seric (void) alarm(0); 84155012Seric clearenvelope(e, FALSE); 84264554Seric e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS; 84358734Seric e->e_errormode = EM_MAIL; 84458884Seric e->e_id = id; 84563846Seric if (forkflag) 84664554Seric { 84764296Seric disconnect(1, e); 84864554Seric OpMode = MD_DELIVER; 84964554Seric } 8507876Seric # ifdef LOG 85158020Seric if (LogLevel > 76) 85255012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 8537881Seric getpid()); 85456795Seric # endif /* LOG */ 8557763Seric 8567763Seric /* don't use the headers from sendmail.cf... */ 85755012Seric e->e_header = NULL; 8587763Seric 85951920Seric /* read the queue control file -- return if locked */ 86063850Seric if (!readqf(e, !requeueflag)) 8616980Seric { 86258884Seric if (tTd(40, 4)) 86358884Seric printf("readqf(%s) failed\n", e->e_id); 86458884Seric if (forkflag) 86524941Seric exit(EX_OK); 86624941Seric else 86724941Seric return; 8686980Seric } 8696980Seric 87055012Seric e->e_flags |= EF_INQUEUE; 87158929Seric eatheader(e, requeueflag); 8726980Seric 87358924Seric if (requeueflag) 87458924Seric queueup(e, TRUE, FALSE); 87558924Seric 8766980Seric /* do the delivery */ 87758915Seric sendall(e, SM_DELIVER); 8786980Seric 8796980Seric /* finish up and exit */ 88058884Seric if (forkflag) 88124941Seric finis(); 88224941Seric else 88355012Seric dropenvelope(e); 8844632Seric } 88564296Seric e->e_id = NULL; 88664296Seric return pid; 8874632Seric } 8884632Seric /* 8894632Seric ** READQF -- read queue file and set up environment. 8904632Seric ** 8914632Seric ** Parameters: 8929377Seric ** e -- the envelope of the job to run. 89363850Seric ** announcefile -- if set, announce the name of the queue 89463850Seric ** file in error messages. 8954632Seric ** 8964632Seric ** Returns: 89751920Seric ** TRUE if it successfully read the queue file. 89851920Seric ** FALSE otherwise. 8994632Seric ** 9004632Seric ** Side Effects: 90151920Seric ** The queue file is returned locked. 9024632Seric */ 9034632Seric 90451920Seric bool 90563850Seric readqf(e, announcefile) 9069377Seric register ENVELOPE *e; 90763850Seric bool announcefile; 9084632Seric { 90917477Seric register FILE *qfp; 91054974Seric ADDRESS *ctladdr; 91156400Seric struct stat st; 91257135Seric char *bp; 91358915Seric char qf[20]; 91457135Seric char buf[MAXLINE]; 91524954Seric extern long atol(); 91654974Seric extern ADDRESS *setctluser(); 9174632Seric 9184632Seric /* 91917468Seric ** Read and process the file. 9204632Seric */ 9214632Seric 92258915Seric strcpy(qf, queuename(e, 'q')); 92351937Seric qfp = fopen(qf, "r+"); 92417477Seric if (qfp == NULL) 92517477Seric { 92658884Seric if (tTd(40, 8)) 92758884Seric printf("readqf(%s): fopen failure (%s)\n", 92858884Seric qf, errstring(errno)); 92940934Srick if (errno != ENOENT) 93040934Srick syserr("readqf: no control file %s", qf); 93151920Seric return FALSE; 93217477Seric } 93340934Srick 93464335Seric if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB)) 93564277Seric { 93664277Seric /* being processed by another queuer */ 93764277Seric if (tTd(40, 8)) 93864277Seric printf("readqf(%s): locked\n", qf); 93964277Seric if (Verbose) 94064277Seric printf("%s: locked\n", e->e_id); 94164277Seric # ifdef LOG 94264277Seric if (LogLevel > 19) 94364277Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 94464277Seric # endif /* LOG */ 94564277Seric (void) fclose(qfp); 94664277Seric return FALSE; 94764277Seric } 94864277Seric 94956400Seric /* 95056400Seric ** Check the queue file for plausibility to avoid attacks. 95156400Seric */ 95256400Seric 95356400Seric if (fstat(fileno(qfp), &st) < 0) 95456400Seric { 95556400Seric /* must have been being processed by someone else */ 95658884Seric if (tTd(40, 8)) 95758884Seric printf("readqf(%s): fstat failure (%s)\n", 95858884Seric qf, errstring(errno)); 95956400Seric fclose(qfp); 96056400Seric return FALSE; 96156400Seric } 96256400Seric 96364137Seric if (st.st_uid != geteuid()) 96456400Seric { 96556400Seric # ifdef LOG 96656400Seric if (LogLevel > 0) 96756400Seric { 96856400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 96956400Seric e->e_id, st.st_uid, st.st_mode); 97056400Seric } 97156795Seric # endif /* LOG */ 97258884Seric if (tTd(40, 8)) 97358884Seric printf("readqf(%s): bogus file\n", qf); 97464277Seric rename(qf, queuename(e, 'Q')); 97556400Seric fclose(qfp); 97656400Seric return FALSE; 97756400Seric } 97856400Seric 97964277Seric if (st.st_size == 0) 98040934Srick { 98164277Seric /* must be a bogus file -- just remove it */ 98264277Seric (void) unlink(qf); 98364277Seric fclose(qfp); 98451920Seric return FALSE; 98540934Srick } 98640934Srick 98764277Seric if (st.st_nlink == 0) 98859101Seric { 98964277Seric /* 99064277Seric ** Race condition -- we got a file just as it was being 99164277Seric ** unlinked. Just assume it is zero length. 99264277Seric */ 99364277Seric 99459101Seric fclose(qfp); 99559101Seric return FALSE; 99659101Seric } 99759101Seric 99864277Seric /* good file -- save this lock */ 99951920Seric e->e_lockfp = qfp; 100051920Seric 100140934Srick /* do basic system initialization */ 100255012Seric initsys(e); 100340934Srick 100463850Seric if (announcefile) 100563850Seric FileName = qf; 10069377Seric LineNumber = 0; 100763850Seric e->e_flags |= EF_GLOBALERRS; 100863850Seric OpMode = MD_DELIVER; 100951920Seric if (Verbose) 10109377Seric printf("\nRunning %s\n", e->e_id); 101154974Seric ctladdr = NULL; 101257135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 10134632Seric { 101458737Seric register char *p; 101557529Seric struct stat st; 101657529Seric 101726504Seric if (tTd(40, 4)) 101857135Seric printf("+++++ %s\n", bp); 101957135Seric switch (bp[0]) 10204632Seric { 102140973Sbostic case 'C': /* specify controlling user */ 102257135Seric ctladdr = setctluser(&bp[1]); 102340973Sbostic break; 102440973Sbostic 10254632Seric case 'R': /* specify recipient */ 102658082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 10274632Seric break; 10284632Seric 102925687Seric case 'E': /* specify error recipient */ 103058082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 103125687Seric break; 103225687Seric 10334632Seric case 'H': /* header */ 103457135Seric (void) chompheader(&bp[1], FALSE, e); 10354632Seric break; 10364632Seric 103710108Seric case 'M': /* message */ 103857135Seric e->e_message = newstr(&bp[1]); 103910108Seric break; 104010108Seric 10414632Seric case 'S': /* sender */ 104258704Seric setsender(newstr(&bp[1]), e, NULL, TRUE); 10434632Seric break; 10444632Seric 104559093Seric case 'B': /* body type */ 104659093Seric e->e_bodytype = newstr(&bp[1]); 104759093Seric break; 104859093Seric 10494632Seric case 'D': /* data file name */ 105057135Seric e->e_df = newstr(&bp[1]); 10519544Seric e->e_dfp = fopen(e->e_df, "r"); 10529544Seric if (e->e_dfp == NULL) 105358020Seric { 10549377Seric syserr("readqf: cannot open %s", e->e_df); 105558020Seric e->e_msgsize = -1; 105658020Seric } 105758020Seric else if (fstat(fileno(e->e_dfp), &st) >= 0) 105857529Seric e->e_msgsize = st.st_size; 10594632Seric break; 10604632Seric 10617860Seric case 'T': /* init time */ 106257135Seric e->e_ctime = atol(&bp[1]); 10634632Seric break; 10644632Seric 10654634Seric case 'P': /* message priority */ 106657135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 10674634Seric break; 10684634Seric 106958737Seric case 'F': /* flag bits */ 107058737Seric for (p = &bp[1]; *p != '\0'; p++) 107158737Seric { 107258737Seric switch (*p) 107358737Seric { 107458737Seric case 'w': /* warning sent */ 107558737Seric e->e_flags |= EF_WARNING; 107658737Seric break; 107758737Seric 107858737Seric case 'r': /* response */ 107958737Seric e->e_flags |= EF_RESPONSE; 108058737Seric break; 108158737Seric } 108258737Seric } 108358737Seric break; 108458737Seric 108553400Seric case '$': /* define macro */ 108657135Seric define(bp[1], newstr(&bp[2]), e); 108753400Seric break; 108853400Seric 108924941Seric case '\0': /* blank line; ignore */ 109024941Seric break; 109124941Seric 10924632Seric default: 109359700Seric syserr("readqf: bad line \"%s\"", e->e_id, 109457135Seric LineNumber, bp); 109563753Seric fclose(qfp); 109663753Seric rename(qf, queuename(e, 'Q')); 109763753Seric return FALSE; 10984632Seric } 109957135Seric 110057135Seric if (bp != buf) 110157135Seric free(bp); 11024632Seric } 11039377Seric 11049377Seric FileName = NULL; 110524941Seric 110624941Seric /* 110724941Seric ** If we haven't read any lines, this queue file is empty. 110824941Seric ** Arrange to remove it without referencing any null pointers. 110924941Seric */ 111024941Seric 111124941Seric if (LineNumber == 0) 111224941Seric { 111324941Seric errno = 0; 111424941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 111524941Seric } 111651920Seric return TRUE; 11174632Seric } 11184632Seric /* 11199630Seric ** PRINTQUEUE -- print out a representation of the mail queue 11209630Seric ** 11219630Seric ** Parameters: 11229630Seric ** none. 11239630Seric ** 11249630Seric ** Returns: 11259630Seric ** none. 11269630Seric ** 11279630Seric ** Side Effects: 11289630Seric ** Prints a listing of the mail queue on the standard output. 11299630Seric */ 11305182Seric 11319630Seric printqueue() 11329630Seric { 11339630Seric register WORK *w; 11349630Seric FILE *f; 113510070Seric int nrequests; 11369630Seric char buf[MAXLINE]; 11379630Seric 11389630Seric /* 113958250Seric ** Check for permission to print the queue 114058250Seric */ 114158250Seric 114264333Seric if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0) 114358250Seric { 114458250Seric struct stat st; 114558523Seric # ifdef NGROUPS 114658523Seric int n; 114763937Seric GIDSET_T gidset[NGROUPS]; 114858523Seric # endif 114958250Seric 115058517Seric if (stat(QueueDir, &st) < 0) 115158250Seric { 115258250Seric syserr("Cannot stat %s", QueueDir); 115358250Seric return; 115458250Seric } 115558523Seric # ifdef NGROUPS 115658523Seric n = getgroups(NGROUPS, gidset); 115758523Seric while (--n >= 0) 115858523Seric { 115958523Seric if (gidset[n] == st.st_gid) 116058523Seric break; 116158523Seric } 116258523Seric if (n < 0) 116358523Seric # else 116463787Seric if (RealGid != st.st_gid) 116558523Seric # endif 116658250Seric { 116758250Seric usrerr("510 You are not permitted to see the queue"); 116858250Seric setstat(EX_NOPERM); 116958250Seric return; 117058250Seric } 117158250Seric } 117258250Seric 117358250Seric /* 11749630Seric ** Read and order the queue. 11759630Seric */ 11769630Seric 117724941Seric nrequests = orderq(TRUE); 11789630Seric 11799630Seric /* 11809630Seric ** Print the work list that we have read. 11819630Seric */ 11829630Seric 11839630Seric /* first see if there is anything */ 118410070Seric if (nrequests <= 0) 11859630Seric { 118610070Seric printf("Mail queue is empty\n"); 11879630Seric return; 11889630Seric } 11899630Seric 119051920Seric CurrentLA = getla(); /* get load average */ 119140934Srick 119210096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 119325687Seric if (nrequests > QUEUESIZE) 119425687Seric printf(", only %d printed", QUEUESIZE); 119524979Seric if (Verbose) 119658716Seric printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 119724979Seric else 119858716Seric printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 11999630Seric for (w = WorkQ; w != NULL; w = w->w_next) 12009630Seric { 12019630Seric struct stat st; 120210070Seric auto time_t submittime = 0; 120310070Seric long dfsize = -1; 120458737Seric int flags = 0; 120510108Seric char message[MAXLINE]; 120659093Seric char bodytype[MAXNAME]; 12079630Seric 120864355Seric printf("%8s", w->w_name + 2); 120917468Seric f = fopen(w->w_name, "r"); 121017468Seric if (f == NULL) 121117468Seric { 121264355Seric printf(" (job completed)\n"); 121317468Seric errno = 0; 121417468Seric continue; 121517468Seric } 121664335Seric if (!lockfile(fileno(f), w->w_name, NULL, LOCK_SH|LOCK_NB)) 121710070Seric printf("*"); 121857438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 121924941Seric printf("X"); 122010070Seric else 122110070Seric printf(" "); 122210070Seric errno = 0; 122317468Seric 122459093Seric message[0] = bodytype[0] = '\0'; 12259630Seric while (fgets(buf, sizeof buf, f) != NULL) 12269630Seric { 122753400Seric register int i; 122858737Seric register char *p; 122953400Seric 12309630Seric fixcrlf(buf, TRUE); 12319630Seric switch (buf[0]) 12329630Seric { 123310108Seric case 'M': /* error message */ 123453400Seric if ((i = strlen(&buf[1])) >= sizeof message) 123558737Seric i = sizeof message - 1; 123653400Seric bcopy(&buf[1], message, i); 123753400Seric message[i] = '\0'; 123810108Seric break; 123910108Seric 124059093Seric case 'B': /* body type */ 124159093Seric if ((i = strlen(&buf[1])) >= sizeof bodytype) 124259093Seric i = sizeof bodytype - 1; 124359093Seric bcopy(&buf[1], bodytype, i); 124459093Seric bodytype[i] = '\0'; 124559093Seric break; 124659093Seric 12479630Seric case 'S': /* sender name */ 124824979Seric if (Verbose) 124958737Seric printf("%8ld %10ld%c%.12s %.38s", 125058737Seric dfsize, 125158737Seric w->w_pri, 125258737Seric bitset(EF_WARNING, flags) ? '+' : ' ', 125358737Seric ctime(&submittime) + 4, 125424979Seric &buf[1]); 125524979Seric else 125624979Seric printf("%8ld %.16s %.45s", dfsize, 125724979Seric ctime(&submittime), &buf[1]); 125859093Seric if (message[0] != '\0' || bodytype[0] != '\0') 125959093Seric { 126059093Seric printf("\n %10.10s", bodytype); 126159093Seric if (message[0] != '\0') 126259093Seric printf(" (%.60s)", message); 126359093Seric } 12649630Seric break; 126551920Seric 126640973Sbostic case 'C': /* controlling user */ 126754974Seric if (Verbose) 126858716Seric printf("\n\t\t\t\t (---%.34s---)", 126958716Seric &buf[1]); 127040973Sbostic break; 12719630Seric 12729630Seric case 'R': /* recipient name */ 127324979Seric if (Verbose) 127458716Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 127524979Seric else 127658716Seric printf("\n\t\t\t\t %.45s", &buf[1]); 12779630Seric break; 12789630Seric 12799630Seric case 'T': /* creation time */ 128024941Seric submittime = atol(&buf[1]); 12819630Seric break; 128210070Seric 128310070Seric case 'D': /* data file name */ 128410070Seric if (stat(&buf[1], &st) >= 0) 128510070Seric dfsize = st.st_size; 128610070Seric break; 128758737Seric 128858737Seric case 'F': /* flag bits */ 128958737Seric for (p = &buf[1]; *p != '\0'; p++) 129058737Seric { 129158737Seric switch (*p) 129258737Seric { 129358737Seric case 'w': 129458737Seric flags |= EF_WARNING; 129558737Seric break; 129658737Seric } 129758737Seric } 12989630Seric } 12999630Seric } 130010070Seric if (submittime == (time_t) 0) 130110070Seric printf(" (no control file)"); 13029630Seric printf("\n"); 130323098Seric (void) fclose(f); 13049630Seric } 13059630Seric } 13069630Seric 130756795Seric # endif /* QUEUE */ 130817468Seric /* 130917468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 131017468Seric ** 131117468Seric ** Assigns an id code if one does not already exist. 131217468Seric ** This code is very careful to avoid trashing existing files 131317468Seric ** under any circumstances. 131417468Seric ** 131517468Seric ** Parameters: 131617468Seric ** e -- envelope to build it in/from. 131717468Seric ** type -- the file type, used as the first character 131817468Seric ** of the file name. 131917468Seric ** 132017468Seric ** Returns: 132117468Seric ** a pointer to the new file name (in a static buffer). 132217468Seric ** 132317468Seric ** Side Effects: 132451920Seric ** If no id code is already assigned, queuename will 132551920Seric ** assign an id code, create a qf file, and leave a 132651920Seric ** locked, open-for-write file pointer in the envelope. 132717468Seric */ 132817468Seric 132917468Seric char * 133017468Seric queuename(e, type) 133117468Seric register ENVELOPE *e; 133259700Seric int type; 133317468Seric { 133417468Seric static int pid = -1; 133558741Seric static char c0; 133658741Seric static char c1; 133758741Seric static char c2; 133858716Seric time_t now; 133958716Seric struct tm *tm; 134058689Seric static char buf[MAXNAME]; 134117468Seric 134217468Seric if (e->e_id == NULL) 134317468Seric { 134417468Seric char qf[20]; 134517468Seric 134617468Seric /* find a unique id */ 134717468Seric if (pid != getpid()) 134817468Seric { 134917468Seric /* new process -- start back at "AA" */ 135017468Seric pid = getpid(); 135158716Seric now = curtime(); 135258716Seric tm = localtime(&now); 135358716Seric c0 = 'A' + tm->tm_hour; 135417468Seric c1 = 'A'; 135517468Seric c2 = 'A' - 1; 135617468Seric } 135758716Seric (void) sprintf(qf, "qf%cAA%05d", c0, pid); 135817468Seric 135917468Seric while (c1 < '~' || c2 < 'Z') 136017468Seric { 136117468Seric int i; 136217468Seric 136317468Seric if (c2 >= 'Z') 136417468Seric { 136517468Seric c1++; 136617468Seric c2 = 'A' - 1; 136717468Seric } 136858716Seric qf[3] = c1; 136958716Seric qf[4] = ++c2; 137017468Seric if (tTd(7, 20)) 137140934Srick printf("queuename: trying \"%s\"\n", qf); 137217468Seric 137340934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 137451920Seric if (i < 0) 137551920Seric { 137651920Seric if (errno == EEXIST) 137751920Seric continue; 137851920Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 137951920Seric qf, QueueDir); 138051920Seric exit(EX_UNAVAILABLE); 138151920Seric } 138264335Seric if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB)) 138351920Seric { 138451920Seric e->e_lockfp = fdopen(i, "w"); 138540934Srick break; 138617468Seric } 138751920Seric 138851920Seric /* a reader got the file; abandon it and try again */ 138951920Seric (void) close(i); 139017468Seric } 139117468Seric if (c1 >= '~' && c2 >= 'Z') 139217468Seric { 139317468Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 139417468Seric qf, QueueDir); 139517468Seric exit(EX_OSERR); 139617468Seric } 139717468Seric e->e_id = newstr(&qf[2]); 139817468Seric define('i', e->e_id, e); 139917468Seric if (tTd(7, 1)) 140017468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 140117468Seric # ifdef LOG 140258020Seric if (LogLevel > 93) 140317468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 140456795Seric # endif /* LOG */ 140517468Seric } 140617468Seric 140717468Seric if (type == '\0') 140817468Seric return (NULL); 140917468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 141017468Seric if (tTd(7, 2)) 141117468Seric printf("queuename: %s\n", buf); 141217468Seric return (buf); 141317468Seric } 141417468Seric /* 141517468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 141617468Seric ** 141717468Seric ** Parameters: 141817468Seric ** e -- the envelope to unlock. 141917468Seric ** 142017468Seric ** Returns: 142117468Seric ** none 142217468Seric ** 142317468Seric ** Side Effects: 142417468Seric ** unlocks the queue for `e'. 142517468Seric */ 142617468Seric 142717468Seric unlockqueue(e) 142817468Seric ENVELOPE *e; 142917468Seric { 143058680Seric if (tTd(51, 4)) 143158680Seric printf("unlockqueue(%s)\n", e->e_id); 143258680Seric 143351920Seric /* if there is a lock file in the envelope, close it */ 143451920Seric if (e->e_lockfp != NULL) 143558680Seric xfclose(e->e_lockfp, "unlockqueue", e->e_id); 143651920Seric e->e_lockfp = NULL; 143751920Seric 143858728Seric /* don't create a queue id if we don't already have one */ 143958728Seric if (e->e_id == NULL) 144058728Seric return; 144158728Seric 144217468Seric /* remove the transcript */ 144317468Seric # ifdef LOG 144458020Seric if (LogLevel > 87) 144517468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 144656795Seric # endif /* LOG */ 144758680Seric if (!tTd(51, 104)) 144817468Seric xunlink(queuename(e, 'x')); 144917468Seric 145017468Seric } 145140973Sbostic /* 145254974Seric ** SETCTLUSER -- create a controlling address 145340973Sbostic ** 145454974Seric ** Create a fake "address" given only a local login name; this is 145554974Seric ** used as a "controlling user" for future recipient addresses. 145640973Sbostic ** 145740973Sbostic ** Parameters: 145854974Seric ** user -- the user name of the controlling user. 145940973Sbostic ** 146040973Sbostic ** Returns: 146154974Seric ** An address descriptor for the controlling user. 146240973Sbostic ** 146340973Sbostic ** Side Effects: 146440973Sbostic ** none. 146540973Sbostic */ 146640973Sbostic 146754974Seric ADDRESS * 146854974Seric setctluser(user) 146954974Seric char *user; 147040973Sbostic { 147154974Seric register ADDRESS *a; 147240973Sbostic struct passwd *pw; 147359113Seric char *p; 147440973Sbostic 147540973Sbostic /* 147654974Seric ** See if this clears our concept of controlling user. 147740973Sbostic */ 147840973Sbostic 147963850Seric if (user == NULL || *user == '\0') 148063850Seric return NULL; 148140973Sbostic 148240973Sbostic /* 148354974Seric ** Set up addr fields for controlling user. 148440973Sbostic */ 148540973Sbostic 148654974Seric a = (ADDRESS *) xalloc(sizeof *a); 148754974Seric bzero((char *) a, sizeof *a); 148859113Seric 148959113Seric p = strchr(user, ':'); 149059113Seric if (p != NULL) 149159113Seric *p++ = '\0'; 149259270Seric if (*user != '\0' && (pw = getpwnam(user)) != NULL) 149340973Sbostic { 149440973Sbostic a->q_home = newstr(pw->pw_dir); 149540973Sbostic a->q_uid = pw->pw_uid; 149640973Sbostic a->q_gid = pw->pw_gid; 149757642Seric a->q_user = newstr(user); 149859270Seric a->q_flags |= QGOODUID; 149940973Sbostic } 150040973Sbostic else 150140973Sbostic { 150257642Seric a->q_user = newstr(DefUser); 150340973Sbostic } 150440973Sbostic 150559270Seric a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 150656328Seric a->q_mailer = LocalMailer; 150759113Seric if (p == NULL) 150859113Seric a->q_paddr = a->q_user; 150959113Seric else 151059113Seric a->q_paddr = newstr(p); 151154974Seric return a; 151240973Sbostic } 1513