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*64492Seric static char sccsid[] = "@(#)queue.c 8.17 (Berkeley) 09/18/93 (with queueing)"; 1433731Sbostic #else 15*64492Seric static char sccsid[] = "@(#)queue.c 8.17 (Berkeley) 09/18/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 47510205Seric /* 47655360Seric ** Create ourselves an envelope 47755360Seric */ 47855360Seric 47955360Seric CurEnv = &QueueEnvelope; 48058179Seric e = newenvelope(&QueueEnvelope, CurEnv); 48155360Seric e->e_flags = BlankEnvelope.e_flags; 48255360Seric 48355360Seric /* 48427175Seric ** Make sure the alias database is open. 48527175Seric */ 48627175Seric 48760537Seric initmaps(FALSE, e); 48827175Seric 48927175Seric /* 4907466Seric ** Start making passes through the queue. 4917466Seric ** First, read and sort the entire queue. 4927466Seric ** Then, process the work in that order. 4937466Seric ** But if you take too long, start over. 4947466Seric */ 4957466Seric 4967943Seric /* order the existing work requests */ 49724954Seric (void) orderq(FALSE); 4987690Seric 4997943Seric /* process them once at a time */ 5007943Seric while (WorkQ != NULL) 5014639Seric { 5027943Seric WORK *w = WorkQ; 5037881Seric 5047943Seric WorkQ = WorkQ->w_next; 50558884Seric 50658884Seric /* 50758884Seric ** Ignore jobs that are too expensive for the moment. 50858884Seric */ 50958884Seric 51058884Seric if (shouldqueue(w->w_pri, w->w_ctime)) 51158884Seric { 51258884Seric if (Verbose) 51358884Seric printf("\nSkipping %s\n", w->w_name + 2); 51458884Seric } 51558930Seric else 51658930Seric { 51764296Seric pid_t pid; 51864296Seric extern pid_t dowork(); 51964296Seric 52064296Seric pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e); 52164296Seric errno = 0; 52264296Seric (void) waitfor(pid); 52358930Seric } 5247943Seric free(w->w_name); 5257943Seric free((char *) w); 5264639Seric } 52729866Seric 52829866Seric /* exit without the usual cleanup */ 52955467Seric e->e_id = NULL; 53055467Seric finis(); 5314634Seric } 5324634Seric /* 5334632Seric ** ORDERQ -- order the work queue. 5344632Seric ** 5354632Seric ** Parameters: 53624941Seric ** doall -- if set, include everything in the queue (even 53724941Seric ** the jobs that cannot be run because the load 53824941Seric ** average is too high). Otherwise, exclude those 53924941Seric ** jobs. 5404632Seric ** 5414632Seric ** Returns: 54210121Seric ** The number of request in the queue (not necessarily 54310121Seric ** the number of requests in WorkQ however). 5444632Seric ** 5454632Seric ** Side Effects: 5464632Seric ** Sets WorkQ to the queue of available work, in order. 5474632Seric */ 5484632Seric 54925687Seric # define NEED_P 001 55025687Seric # define NEED_T 002 55158318Seric # define NEED_R 004 55258318Seric # define NEED_S 010 5534632Seric 55424941Seric orderq(doall) 55524941Seric bool doall; 5564632Seric { 55760219Seric register struct dirent *d; 5584632Seric register WORK *w; 5596625Sglickman DIR *f; 5604632Seric register int i; 56125687Seric WORK wlist[QUEUESIZE+1]; 56210070Seric int wn = -1; 5634632Seric extern workcmpf(); 5644632Seric 56558318Seric if (tTd(41, 1)) 56658318Seric { 56758318Seric printf("orderq:\n"); 56858318Seric if (QueueLimitId != NULL) 56958318Seric printf("\tQueueLimitId = %s\n", QueueLimitId); 57058318Seric if (QueueLimitSender != NULL) 57158318Seric printf("\tQueueLimitSender = %s\n", QueueLimitSender); 57258318Seric if (QueueLimitRecipient != NULL) 57358318Seric printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 57458318Seric } 57558318Seric 5764632Seric /* clear out old WorkQ */ 5774632Seric for (w = WorkQ; w != NULL; ) 5784632Seric { 5794632Seric register WORK *nw = w->w_next; 5804632Seric 5814632Seric WorkQ = nw; 5824632Seric free(w->w_name); 5834632Seric free((char *) w); 5844632Seric w = nw; 5854632Seric } 5864632Seric 5874632Seric /* open the queue directory */ 5888148Seric f = opendir("."); 5894632Seric if (f == NULL) 5904632Seric { 5918148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 59210070Seric return (0); 5934632Seric } 5944632Seric 5954632Seric /* 5964632Seric ** Read the work directory. 5974632Seric */ 5984632Seric 59910070Seric while ((d = readdir(f)) != NULL) 6004632Seric { 6019377Seric FILE *cf; 602*64492Seric register char *p; 6034632Seric char lbuf[MAXNAME]; 60458318Seric extern bool strcontainedin(); 6054632Seric 6064632Seric /* is this an interesting entry? */ 6077812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 6084632Seric continue; 6094632Seric 61058318Seric if (QueueLimitId != NULL && 61158318Seric !strcontainedin(QueueLimitId, d->d_name)) 61258318Seric continue; 61358318Seric 61458722Seric /* 61558722Seric ** Check queue name for plausibility. This handles 61658722Seric ** both old and new type ids. 61758722Seric */ 61858722Seric 619*64492Seric p = d->d_name + 2; 620*64492Seric if (isupper(p[0]) && isupper(p[2])) 621*64492Seric p += 3; 622*64492Seric else if (isupper(p[1])) 623*64492Seric p += 2; 624*64492Seric else 625*64492Seric p = d->d_name; 626*64492Seric for (i = 0; isdigit(*p); p++) 627*64492Seric i++; 628*64492Seric if (i < 5 || *p != '\0') 62958020Seric { 63058020Seric if (Verbose) 63158020Seric printf("orderq: bogus qf name %s\n", d->d_name); 63258020Seric #ifdef LOG 63358020Seric if (LogLevel > 3) 63459615Seric syslog(LOG_CRIT, "orderq: bogus qf name %s", 63558020Seric d->d_name); 63658020Seric #endif 63758020Seric if (strlen(d->d_name) >= MAXNAME) 63858020Seric d->d_name[MAXNAME - 1] = '\0'; 63958020Seric strcpy(lbuf, d->d_name); 64058020Seric lbuf[0] = 'Q'; 64158020Seric (void) rename(d->d_name, lbuf); 64258020Seric continue; 64358020Seric } 64458020Seric 64510070Seric /* yes -- open control file (if not too many files) */ 64625687Seric if (++wn >= QUEUESIZE) 64710070Seric continue; 64858318Seric 6498148Seric cf = fopen(d->d_name, "r"); 6504632Seric if (cf == NULL) 6514632Seric { 6527055Seric /* this may be some random person sending hir msgs */ 6537055Seric /* syserr("orderq: cannot open %s", cbuf); */ 65410090Seric if (tTd(41, 2)) 65510090Seric printf("orderq: cannot open %s (%d)\n", 65610090Seric d->d_name, errno); 6577055Seric errno = 0; 65810090Seric wn--; 6594632Seric continue; 6604632Seric } 66125687Seric w = &wlist[wn]; 66225687Seric w->w_name = newstr(d->d_name); 6634632Seric 66425027Seric /* make sure jobs in creation don't clog queue */ 66525687Seric w->w_pri = 0x7fffffff; 66625687Seric w->w_ctime = 0; 66725027Seric 6684632Seric /* extract useful information */ 66925687Seric i = NEED_P | NEED_T; 67058318Seric if (QueueLimitSender != NULL) 67158318Seric i |= NEED_S; 67258318Seric if (QueueLimitRecipient != NULL) 67358318Seric i |= NEED_R; 67425687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 6754632Seric { 67624954Seric extern long atol(); 67758318Seric extern bool strcontainedin(); 67824954Seric 67924941Seric switch (lbuf[0]) 6804632Seric { 68124941Seric case 'P': 68225687Seric w->w_pri = atol(&lbuf[1]); 68325687Seric i &= ~NEED_P; 6844632Seric break; 68525013Seric 68625013Seric case 'T': 68725687Seric w->w_ctime = atol(&lbuf[1]); 68825687Seric i &= ~NEED_T; 68925013Seric break; 69058318Seric 69158318Seric case 'R': 69258318Seric if (QueueLimitRecipient != NULL && 69358318Seric strcontainedin(QueueLimitRecipient, &lbuf[1])) 69458318Seric i &= ~NEED_R; 69558318Seric break; 69658318Seric 69758318Seric case 'S': 69858318Seric if (QueueLimitSender != NULL && 69958318Seric strcontainedin(QueueLimitSender, &lbuf[1])) 70058318Seric i &= ~NEED_S; 70158318Seric break; 7024632Seric } 7034632Seric } 7044632Seric (void) fclose(cf); 70524953Seric 70658318Seric if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 70758318Seric bitset(NEED_R|NEED_S, i)) 70824953Seric { 70924953Seric /* don't even bother sorting this job in */ 71024953Seric wn--; 71124953Seric } 7124632Seric } 7136625Sglickman (void) closedir(f); 71410090Seric wn++; 7154632Seric 7164632Seric /* 7174632Seric ** Sort the work directory. 7184632Seric */ 7194632Seric 72025687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 7214632Seric 7224632Seric /* 7234632Seric ** Convert the work list into canonical form. 7249377Seric ** Should be turning it into a list of envelopes here perhaps. 7254632Seric */ 7264632Seric 72724981Seric WorkQ = NULL; 72825687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 7294632Seric { 7304632Seric w = (WORK *) xalloc(sizeof *w); 7314632Seric w->w_name = wlist[i].w_name; 7324632Seric w->w_pri = wlist[i].w_pri; 73325013Seric w->w_ctime = wlist[i].w_ctime; 73424981Seric w->w_next = WorkQ; 73524981Seric WorkQ = w; 7364632Seric } 7374632Seric 7387677Seric if (tTd(40, 1)) 7394632Seric { 7404632Seric for (w = WorkQ; w != NULL; w = w->w_next) 7415037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 7424632Seric } 74310070Seric 74410090Seric return (wn); 7454632Seric } 7464632Seric /* 7477677Seric ** WORKCMPF -- compare function for ordering work. 7484632Seric ** 7494632Seric ** Parameters: 7504632Seric ** a -- the first argument. 7514632Seric ** b -- the second argument. 7524632Seric ** 7534632Seric ** Returns: 75424981Seric ** -1 if a < b 75524981Seric ** 0 if a == b 75624981Seric ** +1 if a > b 7574632Seric ** 7584632Seric ** Side Effects: 7594632Seric ** none. 7604632Seric */ 7614632Seric 7624632Seric workcmpf(a, b) 7635037Seric register WORK *a; 7645037Seric register WORK *b; 7654632Seric { 76657438Seric long pa = a->w_pri; 76757438Seric long pb = b->w_pri; 76824941Seric 76924941Seric if (pa == pb) 7704632Seric return (0); 77124941Seric else if (pa > pb) 77224981Seric return (1); 77324981Seric else 77410121Seric return (-1); 7754632Seric } 7764632Seric /* 7774632Seric ** DOWORK -- do a work request. 7784632Seric ** 7794632Seric ** Parameters: 78058884Seric ** id -- the ID of the job to run. 78158884Seric ** forkflag -- if set, run this in background. 78258924Seric ** requeueflag -- if set, reinstantiate the queue quickly. 78358924Seric ** This is used when expanding aliases in the queue. 78461094Seric ** If forkflag is also set, it doesn't wait for the 78561094Seric ** child. 78658884Seric ** e - the envelope in which to run it. 7874632Seric ** 7884632Seric ** Returns: 78964296Seric ** process id of process that is running the queue job. 7904632Seric ** 7914632Seric ** Side Effects: 7924632Seric ** The work request is satisfied if possible. 7934632Seric */ 7944632Seric 79564296Seric pid_t 79658924Seric dowork(id, forkflag, requeueflag, e) 79758884Seric char *id; 79858884Seric bool forkflag; 79958924Seric bool requeueflag; 80055012Seric register ENVELOPE *e; 8014632Seric { 80264296Seric register pid_t pid; 80351920Seric extern bool readqf(); 8044632Seric 8057677Seric if (tTd(40, 1)) 80658884Seric printf("dowork(%s)\n", id); 8074632Seric 8084632Seric /* 80924941Seric ** Fork for work. 81024941Seric */ 81124941Seric 81258884Seric if (forkflag) 81324941Seric { 81464296Seric pid = fork(); 81564296Seric if (pid < 0) 81624941Seric { 81724941Seric syserr("dowork: cannot fork"); 81864296Seric return 0; 81924941Seric } 82024941Seric } 82124941Seric else 82224941Seric { 82364296Seric pid = 0; 82424941Seric } 82524941Seric 82664296Seric if (pid == 0) 8274632Seric { 8284632Seric /* 8294632Seric ** CHILD 8308148Seric ** Lock the control file to avoid duplicate deliveries. 8318148Seric ** Then run the file as though we had just read it. 8327350Seric ** We save an idea of the temporary name so we 8337350Seric ** can recover on interrupt. 8344632Seric */ 8354632Seric 8367763Seric /* set basic modes, etc. */ 8377356Seric (void) alarm(0); 83855012Seric clearenvelope(e, FALSE); 83958737Seric e->e_flags |= EF_QUEUERUN; 84058734Seric e->e_errormode = EM_MAIL; 84158884Seric e->e_id = id; 84263846Seric if (forkflag) 84364296Seric disconnect(1, e); 8447876Seric # ifdef LOG 84558020Seric if (LogLevel > 76) 84655012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 8477881Seric getpid()); 84856795Seric # endif /* LOG */ 8497763Seric 8507763Seric /* don't use the headers from sendmail.cf... */ 85155012Seric e->e_header = NULL; 8527763Seric 85351920Seric /* read the queue control file -- return if locked */ 85463850Seric if (!readqf(e, !requeueflag)) 8556980Seric { 85658884Seric if (tTd(40, 4)) 85758884Seric printf("readqf(%s) failed\n", e->e_id); 85858884Seric if (forkflag) 85924941Seric exit(EX_OK); 86024941Seric else 86124941Seric return; 8626980Seric } 8636980Seric 86455012Seric e->e_flags |= EF_INQUEUE; 86558929Seric eatheader(e, requeueflag); 8666980Seric 86758924Seric if (requeueflag) 86858924Seric queueup(e, TRUE, FALSE); 86958924Seric 8706980Seric /* do the delivery */ 87158915Seric sendall(e, SM_DELIVER); 8726980Seric 8736980Seric /* finish up and exit */ 87458884Seric if (forkflag) 87524941Seric finis(); 87624941Seric else 87755012Seric dropenvelope(e); 8784632Seric } 87964296Seric e->e_id = NULL; 88064296Seric return pid; 8814632Seric } 8824632Seric /* 8834632Seric ** READQF -- read queue file and set up environment. 8844632Seric ** 8854632Seric ** Parameters: 8869377Seric ** e -- the envelope of the job to run. 88763850Seric ** announcefile -- if set, announce the name of the queue 88863850Seric ** file in error messages. 8894632Seric ** 8904632Seric ** Returns: 89151920Seric ** TRUE if it successfully read the queue file. 89251920Seric ** FALSE otherwise. 8934632Seric ** 8944632Seric ** Side Effects: 89551920Seric ** The queue file is returned locked. 8964632Seric */ 8974632Seric 89851920Seric bool 89963850Seric readqf(e, announcefile) 9009377Seric register ENVELOPE *e; 90163850Seric bool announcefile; 9024632Seric { 90317477Seric register FILE *qfp; 90454974Seric ADDRESS *ctladdr; 90556400Seric struct stat st; 90657135Seric char *bp; 90758915Seric char qf[20]; 90857135Seric char buf[MAXLINE]; 90924954Seric extern long atol(); 91054974Seric extern ADDRESS *setctluser(); 9114632Seric 9124632Seric /* 91317468Seric ** Read and process the file. 9144632Seric */ 9154632Seric 91658915Seric strcpy(qf, queuename(e, 'q')); 91751937Seric qfp = fopen(qf, "r+"); 91817477Seric if (qfp == NULL) 91917477Seric { 92058884Seric if (tTd(40, 8)) 92158884Seric printf("readqf(%s): fopen failure (%s)\n", 92258884Seric qf, errstring(errno)); 92340934Srick if (errno != ENOENT) 92440934Srick syserr("readqf: no control file %s", qf); 92551920Seric return FALSE; 92617477Seric } 92740934Srick 92864335Seric if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB)) 92964277Seric { 93064277Seric /* being processed by another queuer */ 93164277Seric if (tTd(40, 8)) 93264277Seric printf("readqf(%s): locked\n", qf); 93364277Seric if (Verbose) 93464277Seric printf("%s: locked\n", e->e_id); 93564277Seric # ifdef LOG 93664277Seric if (LogLevel > 19) 93764277Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 93864277Seric # endif /* LOG */ 93964277Seric (void) fclose(qfp); 94064277Seric return FALSE; 94164277Seric } 94264277Seric 94356400Seric /* 94456400Seric ** Check the queue file for plausibility to avoid attacks. 94556400Seric */ 94656400Seric 94756400Seric if (fstat(fileno(qfp), &st) < 0) 94856400Seric { 94956400Seric /* must have been being processed by someone else */ 95058884Seric if (tTd(40, 8)) 95158884Seric printf("readqf(%s): fstat failure (%s)\n", 95258884Seric qf, errstring(errno)); 95356400Seric fclose(qfp); 95456400Seric return FALSE; 95556400Seric } 95656400Seric 95764137Seric if (st.st_uid != geteuid()) 95856400Seric { 95956400Seric # ifdef LOG 96056400Seric if (LogLevel > 0) 96156400Seric { 96256400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 96356400Seric e->e_id, st.st_uid, st.st_mode); 96456400Seric } 96556795Seric # endif /* LOG */ 96658884Seric if (tTd(40, 8)) 96758884Seric printf("readqf(%s): bogus file\n", qf); 96864277Seric rename(qf, queuename(e, 'Q')); 96956400Seric fclose(qfp); 97056400Seric return FALSE; 97156400Seric } 97256400Seric 97364277Seric if (st.st_size == 0) 97440934Srick { 97564277Seric /* must be a bogus file -- just remove it */ 97664277Seric (void) unlink(qf); 97764277Seric fclose(qfp); 97851920Seric return FALSE; 97940934Srick } 98040934Srick 98164277Seric if (st.st_nlink == 0) 98259101Seric { 98364277Seric /* 98464277Seric ** Race condition -- we got a file just as it was being 98564277Seric ** unlinked. Just assume it is zero length. 98664277Seric */ 98764277Seric 98859101Seric fclose(qfp); 98959101Seric return FALSE; 99059101Seric } 99159101Seric 99264277Seric /* good file -- save this lock */ 99351920Seric e->e_lockfp = qfp; 99451920Seric 99540934Srick /* do basic system initialization */ 99655012Seric initsys(e); 99740934Srick 99863850Seric if (announcefile) 99963850Seric FileName = qf; 10009377Seric LineNumber = 0; 100163850Seric e->e_flags |= EF_GLOBALERRS; 100263850Seric OpMode = MD_DELIVER; 100351920Seric if (Verbose) 10049377Seric printf("\nRunning %s\n", e->e_id); 100554974Seric ctladdr = NULL; 100657135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 10074632Seric { 100858737Seric register char *p; 100957529Seric struct stat st; 101057529Seric 101126504Seric if (tTd(40, 4)) 101257135Seric printf("+++++ %s\n", bp); 101357135Seric switch (bp[0]) 10144632Seric { 101540973Sbostic case 'C': /* specify controlling user */ 101657135Seric ctladdr = setctluser(&bp[1]); 101740973Sbostic break; 101840973Sbostic 10194632Seric case 'R': /* specify recipient */ 102058082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 10214632Seric break; 10224632Seric 102325687Seric case 'E': /* specify error recipient */ 102458082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 102525687Seric break; 102625687Seric 10274632Seric case 'H': /* header */ 102857135Seric (void) chompheader(&bp[1], FALSE, e); 10294632Seric break; 10304632Seric 103110108Seric case 'M': /* message */ 103257135Seric e->e_message = newstr(&bp[1]); 103310108Seric break; 103410108Seric 10354632Seric case 'S': /* sender */ 103658704Seric setsender(newstr(&bp[1]), e, NULL, TRUE); 10374632Seric break; 10384632Seric 103959093Seric case 'B': /* body type */ 104059093Seric e->e_bodytype = newstr(&bp[1]); 104159093Seric break; 104259093Seric 10434632Seric case 'D': /* data file name */ 104457135Seric e->e_df = newstr(&bp[1]); 10459544Seric e->e_dfp = fopen(e->e_df, "r"); 10469544Seric if (e->e_dfp == NULL) 104758020Seric { 10489377Seric syserr("readqf: cannot open %s", e->e_df); 104958020Seric e->e_msgsize = -1; 105058020Seric } 105158020Seric else if (fstat(fileno(e->e_dfp), &st) >= 0) 105257529Seric e->e_msgsize = st.st_size; 10534632Seric break; 10544632Seric 10557860Seric case 'T': /* init time */ 105657135Seric e->e_ctime = atol(&bp[1]); 10574632Seric break; 10584632Seric 10594634Seric case 'P': /* message priority */ 106057135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 10614634Seric break; 10624634Seric 106358737Seric case 'F': /* flag bits */ 106458737Seric for (p = &bp[1]; *p != '\0'; p++) 106558737Seric { 106658737Seric switch (*p) 106758737Seric { 106858737Seric case 'w': /* warning sent */ 106958737Seric e->e_flags |= EF_WARNING; 107058737Seric break; 107158737Seric 107258737Seric case 'r': /* response */ 107358737Seric e->e_flags |= EF_RESPONSE; 107458737Seric break; 107558737Seric } 107658737Seric } 107758737Seric break; 107858737Seric 107953400Seric case '$': /* define macro */ 108057135Seric define(bp[1], newstr(&bp[2]), e); 108153400Seric break; 108253400Seric 108324941Seric case '\0': /* blank line; ignore */ 108424941Seric break; 108524941Seric 10864632Seric default: 108759700Seric syserr("readqf: bad line \"%s\"", e->e_id, 108857135Seric LineNumber, bp); 108963753Seric fclose(qfp); 109063753Seric rename(qf, queuename(e, 'Q')); 109163753Seric return FALSE; 10924632Seric } 109357135Seric 109457135Seric if (bp != buf) 109557135Seric free(bp); 10964632Seric } 10979377Seric 10989377Seric FileName = NULL; 109924941Seric 110024941Seric /* 110124941Seric ** If we haven't read any lines, this queue file is empty. 110224941Seric ** Arrange to remove it without referencing any null pointers. 110324941Seric */ 110424941Seric 110524941Seric if (LineNumber == 0) 110624941Seric { 110724941Seric errno = 0; 110824941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 110924941Seric } 111051920Seric return TRUE; 11114632Seric } 11124632Seric /* 11139630Seric ** PRINTQUEUE -- print out a representation of the mail queue 11149630Seric ** 11159630Seric ** Parameters: 11169630Seric ** none. 11179630Seric ** 11189630Seric ** Returns: 11199630Seric ** none. 11209630Seric ** 11219630Seric ** Side Effects: 11229630Seric ** Prints a listing of the mail queue on the standard output. 11239630Seric */ 11245182Seric 11259630Seric printqueue() 11269630Seric { 11279630Seric register WORK *w; 11289630Seric FILE *f; 112910070Seric int nrequests; 11309630Seric char buf[MAXLINE]; 11319630Seric 11329630Seric /* 113358250Seric ** Check for permission to print the queue 113458250Seric */ 113558250Seric 113664333Seric if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0) 113758250Seric { 113858250Seric struct stat st; 113958523Seric # ifdef NGROUPS 114058523Seric int n; 114163937Seric GIDSET_T gidset[NGROUPS]; 114258523Seric # endif 114358250Seric 114458517Seric if (stat(QueueDir, &st) < 0) 114558250Seric { 114658250Seric syserr("Cannot stat %s", QueueDir); 114758250Seric return; 114858250Seric } 114958523Seric # ifdef NGROUPS 115058523Seric n = getgroups(NGROUPS, gidset); 115158523Seric while (--n >= 0) 115258523Seric { 115358523Seric if (gidset[n] == st.st_gid) 115458523Seric break; 115558523Seric } 115658523Seric if (n < 0) 115758523Seric # else 115863787Seric if (RealGid != st.st_gid) 115958523Seric # endif 116058250Seric { 116158250Seric usrerr("510 You are not permitted to see the queue"); 116258250Seric setstat(EX_NOPERM); 116358250Seric return; 116458250Seric } 116558250Seric } 116658250Seric 116758250Seric /* 11689630Seric ** Read and order the queue. 11699630Seric */ 11709630Seric 117124941Seric nrequests = orderq(TRUE); 11729630Seric 11739630Seric /* 11749630Seric ** Print the work list that we have read. 11759630Seric */ 11769630Seric 11779630Seric /* first see if there is anything */ 117810070Seric if (nrequests <= 0) 11799630Seric { 118010070Seric printf("Mail queue is empty\n"); 11819630Seric return; 11829630Seric } 11839630Seric 118451920Seric CurrentLA = getla(); /* get load average */ 118540934Srick 118610096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 118725687Seric if (nrequests > QUEUESIZE) 118825687Seric printf(", only %d printed", QUEUESIZE); 118924979Seric if (Verbose) 119058716Seric printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 119124979Seric else 119258716Seric printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 11939630Seric for (w = WorkQ; w != NULL; w = w->w_next) 11949630Seric { 11959630Seric struct stat st; 119610070Seric auto time_t submittime = 0; 119710070Seric long dfsize = -1; 119858737Seric int flags = 0; 119910108Seric char message[MAXLINE]; 120059093Seric char bodytype[MAXNAME]; 12019630Seric 120264355Seric printf("%8s", w->w_name + 2); 120317468Seric f = fopen(w->w_name, "r"); 120417468Seric if (f == NULL) 120517468Seric { 120664355Seric printf(" (job completed)\n"); 120717468Seric errno = 0; 120817468Seric continue; 120917468Seric } 121064335Seric if (!lockfile(fileno(f), w->w_name, NULL, LOCK_SH|LOCK_NB)) 121110070Seric printf("*"); 121257438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 121324941Seric printf("X"); 121410070Seric else 121510070Seric printf(" "); 121610070Seric errno = 0; 121717468Seric 121859093Seric message[0] = bodytype[0] = '\0'; 12199630Seric while (fgets(buf, sizeof buf, f) != NULL) 12209630Seric { 122153400Seric register int i; 122258737Seric register char *p; 122353400Seric 12249630Seric fixcrlf(buf, TRUE); 12259630Seric switch (buf[0]) 12269630Seric { 122710108Seric case 'M': /* error message */ 122853400Seric if ((i = strlen(&buf[1])) >= sizeof message) 122958737Seric i = sizeof message - 1; 123053400Seric bcopy(&buf[1], message, i); 123153400Seric message[i] = '\0'; 123210108Seric break; 123310108Seric 123459093Seric case 'B': /* body type */ 123559093Seric if ((i = strlen(&buf[1])) >= sizeof bodytype) 123659093Seric i = sizeof bodytype - 1; 123759093Seric bcopy(&buf[1], bodytype, i); 123859093Seric bodytype[i] = '\0'; 123959093Seric break; 124059093Seric 12419630Seric case 'S': /* sender name */ 124224979Seric if (Verbose) 124358737Seric printf("%8ld %10ld%c%.12s %.38s", 124458737Seric dfsize, 124558737Seric w->w_pri, 124658737Seric bitset(EF_WARNING, flags) ? '+' : ' ', 124758737Seric ctime(&submittime) + 4, 124824979Seric &buf[1]); 124924979Seric else 125024979Seric printf("%8ld %.16s %.45s", dfsize, 125124979Seric ctime(&submittime), &buf[1]); 125259093Seric if (message[0] != '\0' || bodytype[0] != '\0') 125359093Seric { 125459093Seric printf("\n %10.10s", bodytype); 125559093Seric if (message[0] != '\0') 125659093Seric printf(" (%.60s)", message); 125759093Seric } 12589630Seric break; 125951920Seric 126040973Sbostic case 'C': /* controlling user */ 126154974Seric if (Verbose) 126258716Seric printf("\n\t\t\t\t (---%.34s---)", 126358716Seric &buf[1]); 126440973Sbostic break; 12659630Seric 12669630Seric case 'R': /* recipient name */ 126724979Seric if (Verbose) 126858716Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 126924979Seric else 127058716Seric printf("\n\t\t\t\t %.45s", &buf[1]); 12719630Seric break; 12729630Seric 12739630Seric case 'T': /* creation time */ 127424941Seric submittime = atol(&buf[1]); 12759630Seric break; 127610070Seric 127710070Seric case 'D': /* data file name */ 127810070Seric if (stat(&buf[1], &st) >= 0) 127910070Seric dfsize = st.st_size; 128010070Seric break; 128158737Seric 128258737Seric case 'F': /* flag bits */ 128358737Seric for (p = &buf[1]; *p != '\0'; p++) 128458737Seric { 128558737Seric switch (*p) 128658737Seric { 128758737Seric case 'w': 128858737Seric flags |= EF_WARNING; 128958737Seric break; 129058737Seric } 129158737Seric } 12929630Seric } 12939630Seric } 129410070Seric if (submittime == (time_t) 0) 129510070Seric printf(" (no control file)"); 12969630Seric printf("\n"); 129723098Seric (void) fclose(f); 12989630Seric } 12999630Seric } 13009630Seric 130156795Seric # endif /* QUEUE */ 130217468Seric /* 130317468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 130417468Seric ** 130517468Seric ** Assigns an id code if one does not already exist. 130617468Seric ** This code is very careful to avoid trashing existing files 130717468Seric ** under any circumstances. 130817468Seric ** 130917468Seric ** Parameters: 131017468Seric ** e -- envelope to build it in/from. 131117468Seric ** type -- the file type, used as the first character 131217468Seric ** of the file name. 131317468Seric ** 131417468Seric ** Returns: 131517468Seric ** a pointer to the new file name (in a static buffer). 131617468Seric ** 131717468Seric ** Side Effects: 131851920Seric ** If no id code is already assigned, queuename will 131951920Seric ** assign an id code, create a qf file, and leave a 132051920Seric ** locked, open-for-write file pointer in the envelope. 132117468Seric */ 132217468Seric 132317468Seric char * 132417468Seric queuename(e, type) 132517468Seric register ENVELOPE *e; 132659700Seric int type; 132717468Seric { 132817468Seric static int pid = -1; 132958741Seric static char c0; 133058741Seric static char c1; 133158741Seric static char c2; 133258716Seric time_t now; 133358716Seric struct tm *tm; 133458689Seric static char buf[MAXNAME]; 133517468Seric 133617468Seric if (e->e_id == NULL) 133717468Seric { 133817468Seric char qf[20]; 133917468Seric 134017468Seric /* find a unique id */ 134117468Seric if (pid != getpid()) 134217468Seric { 134317468Seric /* new process -- start back at "AA" */ 134417468Seric pid = getpid(); 134558716Seric now = curtime(); 134658716Seric tm = localtime(&now); 134758716Seric c0 = 'A' + tm->tm_hour; 134817468Seric c1 = 'A'; 134917468Seric c2 = 'A' - 1; 135017468Seric } 135158716Seric (void) sprintf(qf, "qf%cAA%05d", c0, pid); 135217468Seric 135317468Seric while (c1 < '~' || c2 < 'Z') 135417468Seric { 135517468Seric int i; 135617468Seric 135717468Seric if (c2 >= 'Z') 135817468Seric { 135917468Seric c1++; 136017468Seric c2 = 'A' - 1; 136117468Seric } 136258716Seric qf[3] = c1; 136358716Seric qf[4] = ++c2; 136417468Seric if (tTd(7, 20)) 136540934Srick printf("queuename: trying \"%s\"\n", qf); 136617468Seric 136740934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 136851920Seric if (i < 0) 136951920Seric { 137051920Seric if (errno == EEXIST) 137151920Seric continue; 137251920Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 137351920Seric qf, QueueDir); 137451920Seric exit(EX_UNAVAILABLE); 137551920Seric } 137664335Seric if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB)) 137751920Seric { 137851920Seric e->e_lockfp = fdopen(i, "w"); 137940934Srick break; 138017468Seric } 138151920Seric 138251920Seric /* a reader got the file; abandon it and try again */ 138351920Seric (void) close(i); 138417468Seric } 138517468Seric if (c1 >= '~' && c2 >= 'Z') 138617468Seric { 138717468Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 138817468Seric qf, QueueDir); 138917468Seric exit(EX_OSERR); 139017468Seric } 139117468Seric e->e_id = newstr(&qf[2]); 139217468Seric define('i', e->e_id, e); 139317468Seric if (tTd(7, 1)) 139417468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 139517468Seric # ifdef LOG 139658020Seric if (LogLevel > 93) 139717468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 139856795Seric # endif /* LOG */ 139917468Seric } 140017468Seric 140117468Seric if (type == '\0') 140217468Seric return (NULL); 140317468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 140417468Seric if (tTd(7, 2)) 140517468Seric printf("queuename: %s\n", buf); 140617468Seric return (buf); 140717468Seric } 140817468Seric /* 140917468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 141017468Seric ** 141117468Seric ** Parameters: 141217468Seric ** e -- the envelope to unlock. 141317468Seric ** 141417468Seric ** Returns: 141517468Seric ** none 141617468Seric ** 141717468Seric ** Side Effects: 141817468Seric ** unlocks the queue for `e'. 141917468Seric */ 142017468Seric 142117468Seric unlockqueue(e) 142217468Seric ENVELOPE *e; 142317468Seric { 142458680Seric if (tTd(51, 4)) 142558680Seric printf("unlockqueue(%s)\n", e->e_id); 142658680Seric 142751920Seric /* if there is a lock file in the envelope, close it */ 142851920Seric if (e->e_lockfp != NULL) 142958680Seric xfclose(e->e_lockfp, "unlockqueue", e->e_id); 143051920Seric e->e_lockfp = NULL; 143151920Seric 143258728Seric /* don't create a queue id if we don't already have one */ 143358728Seric if (e->e_id == NULL) 143458728Seric return; 143558728Seric 143617468Seric /* remove the transcript */ 143717468Seric # ifdef LOG 143858020Seric if (LogLevel > 87) 143917468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 144056795Seric # endif /* LOG */ 144158680Seric if (!tTd(51, 104)) 144217468Seric xunlink(queuename(e, 'x')); 144317468Seric 144417468Seric } 144540973Sbostic /* 144654974Seric ** SETCTLUSER -- create a controlling address 144740973Sbostic ** 144854974Seric ** Create a fake "address" given only a local login name; this is 144954974Seric ** used as a "controlling user" for future recipient addresses. 145040973Sbostic ** 145140973Sbostic ** Parameters: 145254974Seric ** user -- the user name of the controlling user. 145340973Sbostic ** 145440973Sbostic ** Returns: 145554974Seric ** An address descriptor for the controlling user. 145640973Sbostic ** 145740973Sbostic ** Side Effects: 145840973Sbostic ** none. 145940973Sbostic */ 146040973Sbostic 146154974Seric ADDRESS * 146254974Seric setctluser(user) 146354974Seric char *user; 146440973Sbostic { 146554974Seric register ADDRESS *a; 146640973Sbostic struct passwd *pw; 146759113Seric char *p; 146840973Sbostic 146940973Sbostic /* 147054974Seric ** See if this clears our concept of controlling user. 147140973Sbostic */ 147240973Sbostic 147363850Seric if (user == NULL || *user == '\0') 147463850Seric return NULL; 147540973Sbostic 147640973Sbostic /* 147754974Seric ** Set up addr fields for controlling user. 147840973Sbostic */ 147940973Sbostic 148054974Seric a = (ADDRESS *) xalloc(sizeof *a); 148154974Seric bzero((char *) a, sizeof *a); 148259113Seric 148359113Seric p = strchr(user, ':'); 148459113Seric if (p != NULL) 148559113Seric *p++ = '\0'; 148659270Seric if (*user != '\0' && (pw = getpwnam(user)) != NULL) 148740973Sbostic { 148840973Sbostic a->q_home = newstr(pw->pw_dir); 148940973Sbostic a->q_uid = pw->pw_uid; 149040973Sbostic a->q_gid = pw->pw_gid; 149157642Seric a->q_user = newstr(user); 149259270Seric a->q_flags |= QGOODUID; 149340973Sbostic } 149440973Sbostic else 149540973Sbostic { 149657642Seric a->q_user = newstr(DefUser); 149740973Sbostic } 149840973Sbostic 149959270Seric a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 150056328Seric a->q_mailer = LocalMailer; 150159113Seric if (p == NULL) 150259113Seric a->q_paddr = a->q_user; 150359113Seric else 150459113Seric a->q_paddr = newstr(p); 150554974Seric return a; 150640973Sbostic } 1507