122708Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 333731Sbostic * Copyright (c) 1988 Regents of the University of California. 433731Sbostic * All rights reserved. 533731Sbostic * 642829Sbostic * %sccs.include.redist.c% 733731Sbostic */ 822708Sdist 933731Sbostic # include "sendmail.h" 1022708Sdist 1133731Sbostic #ifndef lint 1233731Sbostic #ifdef QUEUE 13*61094Seric static char sccsid[] = "@(#)queue.c 6.61 (Berkeley) 06/03/93 (with queueing)"; 1433731Sbostic #else 15*61094Seric static char sccsid[] = "@(#)queue.c 6.61 (Berkeley) 06/03/93 (without queueing)"; 1633731Sbostic #endif 1733731Sbostic #endif /* not lint */ 1833731Sbostic 194634Seric # include <signal.h> 204632Seric # include <errno.h> 2140973Sbostic # include <pwd.h> 2257736Seric # include <dirent.h> 234632Seric 2433731Sbostic # ifdef QUEUE 254632Seric 264632Seric /* 279377Seric ** Work queue. 289377Seric */ 299377Seric 309377Seric struct work 319377Seric { 329377Seric char *w_name; /* name of control file */ 339377Seric long w_pri; /* priority of message, see below */ 3425013Seric time_t w_ctime; /* creation time of message */ 359377Seric struct work *w_next; /* next in queue */ 369377Seric }; 379377Seric 389377Seric typedef struct work WORK; 399377Seric 409377Seric WORK *WorkQ; /* queue of things to be done */ 419377Seric /* 424632Seric ** QUEUEUP -- queue a message up for future transmission. 434632Seric ** 444632Seric ** Parameters: 456980Seric ** e -- the envelope to queue up. 466999Seric ** queueall -- if TRUE, queue all addresses, rather than 476999Seric ** just those with the QQUEUEUP flag set. 489377Seric ** announce -- if TRUE, tell when you are queueing up. 494632Seric ** 504632Seric ** Returns: 5151920Seric ** none. 524632Seric ** 534632Seric ** Side Effects: 549377Seric ** The current request are saved in a control file. 5551920Seric ** The queue file is left locked. 564632Seric */ 574632Seric 589377Seric queueup(e, queueall, announce) 596980Seric register ENVELOPE *e; 606999Seric bool queueall; 619377Seric bool announce; 624632Seric { 637812Seric char *qf; 647812Seric register FILE *tfp; 654632Seric register HDR *h; 665007Seric register ADDRESS *q; 6751920Seric int fd; 6851920Seric int i; 6951920Seric bool newid; 7053400Seric register char *p; 7110173Seric MAILER nullmailer; 7251920Seric char buf[MAXLINE], tf[MAXLINE]; 734632Seric 745037Seric /* 7517477Seric ** Create control file. 765037Seric */ 774632Seric 7851920Seric newid = (e->e_id == NULL); 7951920Seric strcpy(tf, queuename(e, 't')); 8051920Seric tfp = e->e_lockfp; 8151920Seric if (tfp == NULL) 8251920Seric newid = FALSE; 8351920Seric if (newid) 8451835Seric { 8551920Seric tfp = e->e_lockfp; 8651920Seric } 8751920Seric else 8851920Seric { 8951920Seric /* get a locked tf file */ 9051920Seric for (i = 100; --i >= 0; ) 9151835Seric { 9251920Seric fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode); 9351920Seric if (fd < 0) 9451835Seric { 9551920Seric if (errno == EEXIST) 9651920Seric continue; 9758801Seric notemp: 9858690Seric syserr("!queueup: cannot create temp file %s", tf); 9941636Srick } 10058689Seric 10158689Seric if (lockfile(fd, tf, LOCK_EX|LOCK_NB)) 10251920Seric break; 10358689Seric 10451920Seric close(fd); 10558801Seric sleep(i); 10641636Srick } 10758801Seric if (fd < 0) 10858801Seric goto notemp; 10941636Srick 11051920Seric tfp = fdopen(fd, "w"); 11151920Seric } 1124632Seric 1137677Seric if (tTd(40, 1)) 11417468Seric printf("queueing %s\n", e->e_id); 1154632Seric 1164632Seric /* 1176980Seric ** If there is no data file yet, create one. 1186980Seric */ 1196980Seric 1206980Seric if (e->e_df == NULL) 1216980Seric { 1226980Seric register FILE *dfp; 1239389Seric extern putbody(); 1246980Seric 1257812Seric e->e_df = newstr(queuename(e, 'd')); 12640934Srick fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode); 12740934Srick if (fd < 0) 12858690Seric syserr("!queueup: cannot create %s", e->e_df); 12940934Srick dfp = fdopen(fd, "w"); 13059730Seric (*e->e_putbody)(dfp, FileMailer, e, NULL); 13158680Seric (void) xfclose(dfp, "queueup dfp", e->e_id); 1329389Seric e->e_putbody = putbody; 1336980Seric } 1346980Seric 1356980Seric /* 1364632Seric ** Output future work requests. 13725687Seric ** Priority and creation time should be first, since 13825687Seric ** they are required by orderq. 1394632Seric */ 1404632Seric 1419377Seric /* output message priority */ 1429377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 1439377Seric 1449630Seric /* output creation time */ 1459630Seric fprintf(tfp, "T%ld\n", e->e_ctime); 1469630Seric 14759093Seric /* output type and name of data file */ 14859093Seric if (e->e_bodytype != NULL) 14959093Seric fprintf(tfp, "B%s\n", e->e_bodytype); 1507812Seric fprintf(tfp, "D%s\n", e->e_df); 1514632Seric 15210108Seric /* message from envelope, if it exists */ 15310108Seric if (e->e_message != NULL) 15410108Seric fprintf(tfp, "M%s\n", e->e_message); 15510108Seric 15658737Seric /* send various flag bits through */ 15758737Seric p = buf; 15858737Seric if (bitset(EF_WARNING, e->e_flags)) 15958737Seric *p++ = 'w'; 16058737Seric if (bitset(EF_RESPONSE, e->e_flags)) 16158737Seric *p++ = 'r'; 16258737Seric *p++ = '\0'; 16358737Seric if (buf[0] != '\0') 16458737Seric fprintf(tfp, "F%s\n", buf); 16558737Seric 16658957Seric /* $r and $s and $_ macro values */ 16753400Seric if ((p = macvalue('r', e)) != NULL) 16853400Seric fprintf(tfp, "$r%s\n", p); 16953400Seric if ((p = macvalue('s', e)) != NULL) 17053400Seric fprintf(tfp, "$s%s\n", p); 17158957Seric if ((p = macvalue('_', e)) != NULL) 17258957Seric fprintf(tfp, "$_%s\n", p); 17353400Seric 1744632Seric /* output name of sender */ 1757812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 1764632Seric 17755360Seric /* output list of error recipients */ 17859670Seric printctladdr(NULL, NULL); 17955360Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 18055360Seric { 18158680Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 18255360Seric { 18359113Seric printctladdr(q, tfp); 18455360Seric fprintf(tfp, "E%s\n", q->q_paddr); 18555360Seric } 18655360Seric } 18755360Seric 1884632Seric /* output list of recipient addresses */ 1896980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1904632Seric { 19158250Seric if (bitset(QQUEUEUP, q->q_flags) || 19258680Seric (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags))) 1938245Seric { 19459113Seric printctladdr(q, tfp); 1957812Seric fprintf(tfp, "R%s\n", q->q_paddr); 1969377Seric if (announce) 1979377Seric { 1989377Seric e->e_to = q->q_paddr; 19958151Seric message("queued"); 20058020Seric if (LogLevel > 8) 20158337Seric logdelivery(NULL, NULL, "queued", e); 2029377Seric e->e_to = NULL; 2039377Seric } 2049387Seric if (tTd(40, 1)) 2059387Seric { 2069387Seric printf("queueing "); 2079387Seric printaddr(q, FALSE); 2089387Seric } 2098245Seric } 2104632Seric } 2114632Seric 2129377Seric /* 2139377Seric ** Output headers for this message. 2149377Seric ** Expand macros completely here. Queue run will deal with 2159377Seric ** everything as absolute headers. 2169377Seric ** All headers that must be relative to the recipient 2179377Seric ** can be cracked later. 21810173Seric ** We set up a "null mailer" -- i.e., a mailer that will have 21910173Seric ** no effect on the addresses as they are output. 2209377Seric */ 2219377Seric 22210686Seric bzero((char *) &nullmailer, sizeof nullmailer); 22358020Seric nullmailer.m_re_rwset = nullmailer.m_rh_rwset = 22458020Seric nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1; 22510349Seric nullmailer.m_eol = "\n"; 22610173Seric 22758050Seric define('g', "\201f", e); 2286980Seric for (h = e->e_header; h != NULL; h = h->h_link) 2294632Seric { 23010686Seric extern bool bitzerop(); 23110686Seric 23212015Seric /* don't output null headers */ 2334632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 2344632Seric continue; 23512015Seric 23612015Seric /* don't output resent headers on non-resent messages */ 23712015Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 23812015Seric continue; 23912015Seric 24012015Seric /* output this header */ 2417812Seric fprintf(tfp, "H"); 24212015Seric 24312015Seric /* if conditional, output the set of conditions */ 24410686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 24510686Seric { 24610686Seric int j; 24710686Seric 24823098Seric (void) putc('?', tfp); 24910686Seric for (j = '\0'; j <= '\177'; j++) 25010686Seric if (bitnset(j, h->h_mflags)) 25123098Seric (void) putc(j, tfp); 25223098Seric (void) putc('?', tfp); 25310686Seric } 25412015Seric 25512015Seric /* output the header: expand macros, convert addresses */ 2567763Seric if (bitset(H_DEFAULT, h->h_flags)) 2577763Seric { 2587763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 2598236Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 2607763Seric } 2618245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 2629348Seric { 26358737Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 26458737Seric 26558737Seric if (bitset(H_FROM, h->h_flags)) 26658737Seric oldstyle = FALSE; 26758737Seric 26858737Seric commaize(h, h->h_value, tfp, oldstyle, 26955012Seric &nullmailer, e); 2709348Seric } 2717763Seric else 2728245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 2734632Seric } 2744632Seric 2754632Seric /* 2764632Seric ** Clean up. 2774632Seric */ 2784632Seric 27958732Seric fflush(tfp); 28060603Seric fsync(fileno(tfp)); 28158732Seric if (ferror(tfp)) 28258732Seric { 28358732Seric if (newid) 28458732Seric syserr("!552 Error writing control file %s", tf); 28558732Seric else 28658732Seric syserr("!452 Error writing control file %s", tf); 28758732Seric } 28858732Seric 28951920Seric if (!newid) 29051920Seric { 29151920Seric qf = queuename(e, 'q'); 29251920Seric if (rename(tf, qf) < 0) 29351920Seric syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df); 29451920Seric if (e->e_lockfp != NULL) 29558680Seric (void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id); 29651920Seric e->e_lockfp = tfp; 29751920Seric } 29851920Seric else 29951920Seric qf = tf; 30041636Srick errno = 0; 3017391Seric 3027677Seric # ifdef LOG 3037677Seric /* save log info */ 30458020Seric if (LogLevel > 79) 3057878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 30656795Seric # endif /* LOG */ 30751920Seric return; 3084632Seric } 30954974Seric 31054974Seric printctladdr(a, tfp) 31159113Seric register ADDRESS *a; 31254974Seric FILE *tfp; 31354974Seric { 31459113Seric char *uname; 31559113Seric register struct passwd *pw; 31659113Seric register ADDRESS *q; 31759113Seric uid_t uid; 31859113Seric static ADDRESS *lastctladdr; 31959113Seric static uid_t lastuid; 32054974Seric 32159113Seric /* initialization */ 32259670Seric if (a == NULL || tfp == NULL) 32354974Seric { 32459670Seric if (lastctladdr != NULL && tfp != NULL) 32559113Seric fprintf(tfp, "C\n"); 32659113Seric lastctladdr = NULL; 32759113Seric lastuid = 0; 32854974Seric return; 32954974Seric } 33059113Seric 33159113Seric /* find the active uid */ 33259113Seric q = getctladdr(a); 33359113Seric if (q == NULL) 33459113Seric uid = 0; 33554974Seric else 33659113Seric uid = q->q_uid; 33759113Seric 33859113Seric /* if a is an alias, use that for printing */ 33959113Seric if (a->q_alias != NULL) 34059113Seric a = a->q_alias; 34159113Seric 34259113Seric /* check to see if this is the same as last time */ 34359113Seric if (lastctladdr != NULL && uid == lastuid && 34459113Seric strcmp(lastctladdr->q_paddr, a->q_paddr) == 0) 34559113Seric return; 34659113Seric lastuid = uid; 34759113Seric lastctladdr = a; 34859113Seric 34959113Seric if (uid == 0 || (pw = getpwuid(uid)) == NULL) 35059270Seric uname = ""; 35159113Seric else 35259113Seric uname = pw->pw_name; 35359113Seric 35459113Seric fprintf(tfp, "C%s:%s\n", uname, a->q_paddr); 35554974Seric } 35654974Seric 3574632Seric /* 3584632Seric ** RUNQUEUE -- run the jobs in the queue. 3594632Seric ** 3604632Seric ** Gets the stuff out of the queue in some presumably logical 3614632Seric ** order and processes them. 3624632Seric ** 3634632Seric ** Parameters: 36424941Seric ** forkflag -- TRUE if the queue scanning should be done in 36524941Seric ** a child process. We double-fork so it is not our 36624941Seric ** child and we don't have to clean up after it. 3674632Seric ** 3684632Seric ** Returns: 3694632Seric ** none. 3704632Seric ** 3714632Seric ** Side Effects: 3724632Seric ** runs things in the mail queue. 3734632Seric */ 3744632Seric 37555360Seric ENVELOPE QueueEnvelope; /* the queue run envelope */ 37655360Seric 37755360Seric runqueue(forkflag) 3784639Seric bool forkflag; 3794632Seric { 38055360Seric register ENVELOPE *e; 38155360Seric extern ENVELOPE BlankEnvelope; 38224953Seric 3837466Seric /* 38424953Seric ** If no work will ever be selected, don't even bother reading 38524953Seric ** the queue. 38624953Seric */ 38724953Seric 38851920Seric CurrentLA = getla(); /* get load average */ 38940934Srick 39058132Seric if (shouldqueue(0L, curtime())) 39124953Seric { 39224953Seric if (Verbose) 39324953Seric printf("Skipping queue run -- load average too high\n"); 39458107Seric if (forkflag && QueueIntvl != 0) 39558107Seric (void) setevent(QueueIntvl, runqueue, TRUE); 39655360Seric return; 39724953Seric } 39824953Seric 39924953Seric /* 4007466Seric ** See if we want to go off and do other useful work. 4017466Seric */ 4024639Seric 4034639Seric if (forkflag) 4044639Seric { 4057943Seric int pid; 4067943Seric 4077943Seric pid = dofork(); 4087943Seric if (pid != 0) 4094639Seric { 41046928Sbostic extern void reapchild(); 41125184Seric 4127943Seric /* parent -- pick up intermediate zombie */ 41325184Seric #ifndef SIGCHLD 4149377Seric (void) waitfor(pid); 41556795Seric #else /* SIGCHLD */ 41625184Seric (void) signal(SIGCHLD, reapchild); 41756795Seric #endif /* SIGCHLD */ 4187690Seric if (QueueIntvl != 0) 4199348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 4204639Seric return; 4214639Seric } 4227943Seric /* child -- double fork */ 42325184Seric #ifndef SIGCHLD 4247943Seric if (fork() != 0) 4257943Seric exit(EX_OK); 42656795Seric #else /* SIGCHLD */ 42725184Seric (void) signal(SIGCHLD, SIG_DFL); 42856795Seric #endif /* SIGCHLD */ 4294639Seric } 43024941Seric 43140934Srick setproctitle("running queue: %s", QueueDir); 43224941Seric 4337876Seric # ifdef LOG 43458020Seric if (LogLevel > 69) 43555360Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 43655360Seric QueueDir, getpid(), forkflag); 43756795Seric # endif /* LOG */ 4384639Seric 4397466Seric /* 44010205Seric ** Release any resources used by the daemon code. 44110205Seric */ 44210205Seric 44310205Seric # ifdef DAEMON 44410205Seric clrdaemon(); 44556795Seric # endif /* DAEMON */ 44610205Seric 44710205Seric /* 44855360Seric ** Create ourselves an envelope 44955360Seric */ 45055360Seric 45155360Seric CurEnv = &QueueEnvelope; 45258179Seric e = newenvelope(&QueueEnvelope, CurEnv); 45355360Seric e->e_flags = BlankEnvelope.e_flags; 45455360Seric 45555360Seric /* 45627175Seric ** Make sure the alias database is open. 45727175Seric */ 45827175Seric 45960537Seric initmaps(FALSE, e); 46027175Seric 46127175Seric /* 4627466Seric ** Start making passes through the queue. 4637466Seric ** First, read and sort the entire queue. 4647466Seric ** Then, process the work in that order. 4657466Seric ** But if you take too long, start over. 4667466Seric */ 4677466Seric 4687943Seric /* order the existing work requests */ 46924954Seric (void) orderq(FALSE); 4707690Seric 4717943Seric /* process them once at a time */ 4727943Seric while (WorkQ != NULL) 4734639Seric { 4747943Seric WORK *w = WorkQ; 4757881Seric 4767943Seric WorkQ = WorkQ->w_next; 47758884Seric 47858884Seric /* 47958884Seric ** Ignore jobs that are too expensive for the moment. 48058884Seric */ 48158884Seric 48258884Seric if (shouldqueue(w->w_pri, w->w_ctime)) 48358884Seric { 48458884Seric if (Verbose) 48558884Seric printf("\nSkipping %s\n", w->w_name + 2); 48658884Seric } 48758930Seric else 48858930Seric { 48958930Seric dowork(w->w_name + 2, ForkQueueRuns, FALSE, e); 49058930Seric } 4917943Seric free(w->w_name); 4927943Seric free((char *) w); 4934639Seric } 49429866Seric 49529866Seric /* exit without the usual cleanup */ 49655467Seric e->e_id = NULL; 49755467Seric finis(); 4984634Seric } 4994634Seric /* 5004632Seric ** ORDERQ -- order the work queue. 5014632Seric ** 5024632Seric ** Parameters: 50324941Seric ** doall -- if set, include everything in the queue (even 50424941Seric ** the jobs that cannot be run because the load 50524941Seric ** average is too high). Otherwise, exclude those 50624941Seric ** jobs. 5074632Seric ** 5084632Seric ** Returns: 50910121Seric ** The number of request in the queue (not necessarily 51010121Seric ** the number of requests in WorkQ however). 5114632Seric ** 5124632Seric ** Side Effects: 5134632Seric ** Sets WorkQ to the queue of available work, in order. 5144632Seric */ 5154632Seric 51625687Seric # define NEED_P 001 51725687Seric # define NEED_T 002 51858318Seric # define NEED_R 004 51958318Seric # define NEED_S 010 5204632Seric 52124941Seric orderq(doall) 52224941Seric bool doall; 5234632Seric { 52460219Seric register struct dirent *d; 5254632Seric register WORK *w; 5266625Sglickman DIR *f; 5274632Seric register int i; 52825687Seric WORK wlist[QUEUESIZE+1]; 52910070Seric int wn = -1; 5304632Seric extern workcmpf(); 5314632Seric 53258318Seric if (tTd(41, 1)) 53358318Seric { 53458318Seric printf("orderq:\n"); 53558318Seric if (QueueLimitId != NULL) 53658318Seric printf("\tQueueLimitId = %s\n", QueueLimitId); 53758318Seric if (QueueLimitSender != NULL) 53858318Seric printf("\tQueueLimitSender = %s\n", QueueLimitSender); 53958318Seric if (QueueLimitRecipient != NULL) 54058318Seric printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 54158318Seric } 54258318Seric 5434632Seric /* clear out old WorkQ */ 5444632Seric for (w = WorkQ; w != NULL; ) 5454632Seric { 5464632Seric register WORK *nw = w->w_next; 5474632Seric 5484632Seric WorkQ = nw; 5494632Seric free(w->w_name); 5504632Seric free((char *) w); 5514632Seric w = nw; 5524632Seric } 5534632Seric 5544632Seric /* open the queue directory */ 5558148Seric f = opendir("."); 5564632Seric if (f == NULL) 5574632Seric { 5588148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 55910070Seric return (0); 5604632Seric } 5614632Seric 5624632Seric /* 5634632Seric ** Read the work directory. 5644632Seric */ 5654632Seric 56610070Seric while ((d = readdir(f)) != NULL) 5674632Seric { 5689377Seric FILE *cf; 5694632Seric char lbuf[MAXNAME]; 57058318Seric extern bool strcontainedin(); 5714632Seric 5724632Seric /* is this an interesting entry? */ 5737812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 5744632Seric continue; 5754632Seric 57658318Seric if (QueueLimitId != NULL && 57758318Seric !strcontainedin(QueueLimitId, d->d_name)) 57858318Seric continue; 57958318Seric 58058722Seric /* 58158722Seric ** Check queue name for plausibility. This handles 58258722Seric ** both old and new type ids. 58358722Seric */ 58458722Seric 58558722Seric i = strlen(d->d_name); 58658722Seric if (i != 9 && i != 10) 58758020Seric { 58858020Seric if (Verbose) 58958020Seric printf("orderq: bogus qf name %s\n", d->d_name); 59058020Seric #ifdef LOG 59158020Seric if (LogLevel > 3) 59259615Seric syslog(LOG_CRIT, "orderq: bogus qf name %s", 59358020Seric d->d_name); 59458020Seric #endif 59558020Seric if (strlen(d->d_name) >= MAXNAME) 59658020Seric d->d_name[MAXNAME - 1] = '\0'; 59758020Seric strcpy(lbuf, d->d_name); 59858020Seric lbuf[0] = 'Q'; 59958020Seric (void) rename(d->d_name, lbuf); 60058020Seric continue; 60158020Seric } 60258020Seric 60310070Seric /* yes -- open control file (if not too many files) */ 60425687Seric if (++wn >= QUEUESIZE) 60510070Seric continue; 60658318Seric 6078148Seric cf = fopen(d->d_name, "r"); 6084632Seric if (cf == NULL) 6094632Seric { 6107055Seric /* this may be some random person sending hir msgs */ 6117055Seric /* syserr("orderq: cannot open %s", cbuf); */ 61210090Seric if (tTd(41, 2)) 61310090Seric printf("orderq: cannot open %s (%d)\n", 61410090Seric d->d_name, errno); 6157055Seric errno = 0; 61610090Seric wn--; 6174632Seric continue; 6184632Seric } 61925687Seric w = &wlist[wn]; 62025687Seric w->w_name = newstr(d->d_name); 6214632Seric 62225027Seric /* make sure jobs in creation don't clog queue */ 62325687Seric w->w_pri = 0x7fffffff; 62425687Seric w->w_ctime = 0; 62525027Seric 6264632Seric /* extract useful information */ 62725687Seric i = NEED_P | NEED_T; 62858318Seric if (QueueLimitSender != NULL) 62958318Seric i |= NEED_S; 63058318Seric if (QueueLimitRecipient != NULL) 63158318Seric i |= NEED_R; 63225687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 6334632Seric { 63424954Seric extern long atol(); 63558318Seric extern bool strcontainedin(); 63624954Seric 63724941Seric switch (lbuf[0]) 6384632Seric { 63924941Seric case 'P': 64025687Seric w->w_pri = atol(&lbuf[1]); 64125687Seric i &= ~NEED_P; 6424632Seric break; 64325013Seric 64425013Seric case 'T': 64525687Seric w->w_ctime = atol(&lbuf[1]); 64625687Seric i &= ~NEED_T; 64725013Seric break; 64858318Seric 64958318Seric case 'R': 65058318Seric if (QueueLimitRecipient != NULL && 65158318Seric strcontainedin(QueueLimitRecipient, &lbuf[1])) 65258318Seric i &= ~NEED_R; 65358318Seric break; 65458318Seric 65558318Seric case 'S': 65658318Seric if (QueueLimitSender != NULL && 65758318Seric strcontainedin(QueueLimitSender, &lbuf[1])) 65858318Seric i &= ~NEED_S; 65958318Seric break; 6604632Seric } 6614632Seric } 6624632Seric (void) fclose(cf); 66324953Seric 66458318Seric if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 66558318Seric bitset(NEED_R|NEED_S, i)) 66624953Seric { 66724953Seric /* don't even bother sorting this job in */ 66824953Seric wn--; 66924953Seric } 6704632Seric } 6716625Sglickman (void) closedir(f); 67210090Seric wn++; 6734632Seric 6744632Seric /* 6754632Seric ** Sort the work directory. 6764632Seric */ 6774632Seric 67825687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 6794632Seric 6804632Seric /* 6814632Seric ** Convert the work list into canonical form. 6829377Seric ** Should be turning it into a list of envelopes here perhaps. 6834632Seric */ 6844632Seric 68524981Seric WorkQ = NULL; 68625687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 6874632Seric { 6884632Seric w = (WORK *) xalloc(sizeof *w); 6894632Seric w->w_name = wlist[i].w_name; 6904632Seric w->w_pri = wlist[i].w_pri; 69125013Seric w->w_ctime = wlist[i].w_ctime; 69224981Seric w->w_next = WorkQ; 69324981Seric WorkQ = w; 6944632Seric } 6954632Seric 6967677Seric if (tTd(40, 1)) 6974632Seric { 6984632Seric for (w = WorkQ; w != NULL; w = w->w_next) 6995037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 7004632Seric } 70110070Seric 70210090Seric return (wn); 7034632Seric } 7044632Seric /* 7057677Seric ** WORKCMPF -- compare function for ordering work. 7064632Seric ** 7074632Seric ** Parameters: 7084632Seric ** a -- the first argument. 7094632Seric ** b -- the second argument. 7104632Seric ** 7114632Seric ** Returns: 71224981Seric ** -1 if a < b 71324981Seric ** 0 if a == b 71424981Seric ** +1 if a > b 7154632Seric ** 7164632Seric ** Side Effects: 7174632Seric ** none. 7184632Seric */ 7194632Seric 7204632Seric workcmpf(a, b) 7215037Seric register WORK *a; 7225037Seric register WORK *b; 7234632Seric { 72457438Seric long pa = a->w_pri; 72557438Seric long pb = b->w_pri; 72624941Seric 72724941Seric if (pa == pb) 7284632Seric return (0); 72924941Seric else if (pa > pb) 73024981Seric return (1); 73124981Seric else 73210121Seric return (-1); 7334632Seric } 7344632Seric /* 7354632Seric ** DOWORK -- do a work request. 7364632Seric ** 7374632Seric ** Parameters: 73858884Seric ** id -- the ID of the job to run. 73958884Seric ** forkflag -- if set, run this in background. 74058924Seric ** requeueflag -- if set, reinstantiate the queue quickly. 74158924Seric ** This is used when expanding aliases in the queue. 742*61094Seric ** If forkflag is also set, it doesn't wait for the 743*61094Seric ** child. 74458884Seric ** e - the envelope in which to run it. 7454632Seric ** 7464632Seric ** Returns: 7474632Seric ** none. 7484632Seric ** 7494632Seric ** Side Effects: 7504632Seric ** The work request is satisfied if possible. 7514632Seric */ 7524632Seric 75358924Seric dowork(id, forkflag, requeueflag, e) 75458884Seric char *id; 75558884Seric bool forkflag; 75658924Seric bool requeueflag; 75755012Seric register ENVELOPE *e; 7584632Seric { 7594632Seric register int i; 76051920Seric extern bool readqf(); 7614632Seric 7627677Seric if (tTd(40, 1)) 76358884Seric printf("dowork(%s)\n", id); 7644632Seric 7654632Seric /* 76624941Seric ** Fork for work. 76724941Seric */ 76824941Seric 76958884Seric if (forkflag) 77024941Seric { 77124941Seric i = fork(); 77224941Seric if (i < 0) 77324941Seric { 77424941Seric syserr("dowork: cannot fork"); 77524941Seric return; 77624941Seric } 77724941Seric } 77824941Seric else 77924941Seric { 78024941Seric i = 0; 78124941Seric } 78224941Seric 7834632Seric if (i == 0) 7844632Seric { 7854632Seric /* 7864632Seric ** CHILD 7878148Seric ** Lock the control file to avoid duplicate deliveries. 7888148Seric ** Then run the file as though we had just read it. 7897350Seric ** We save an idea of the temporary name so we 7907350Seric ** can recover on interrupt. 7914632Seric */ 7924632Seric 7937763Seric /* set basic modes, etc. */ 7947356Seric (void) alarm(0); 79555012Seric clearenvelope(e, FALSE); 79658737Seric e->e_flags |= EF_QUEUERUN; 79758734Seric e->e_errormode = EM_MAIL; 79858884Seric e->e_id = id; 7997876Seric # ifdef LOG 80058020Seric if (LogLevel > 76) 80155012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 8027881Seric getpid()); 80356795Seric # endif /* LOG */ 8047763Seric 8057763Seric /* don't use the headers from sendmail.cf... */ 80655012Seric e->e_header = NULL; 8077763Seric 80851920Seric /* read the queue control file -- return if locked */ 80955012Seric if (!readqf(e)) 8106980Seric { 81158884Seric if (tTd(40, 4)) 81258884Seric printf("readqf(%s) failed\n", e->e_id); 81358884Seric if (forkflag) 81424941Seric exit(EX_OK); 81524941Seric else 81624941Seric return; 8176980Seric } 8186980Seric 81955012Seric e->e_flags |= EF_INQUEUE; 82058929Seric eatheader(e, requeueflag); 8216980Seric 82258924Seric if (requeueflag) 82358924Seric queueup(e, TRUE, FALSE); 82458924Seric 8256980Seric /* do the delivery */ 82658915Seric sendall(e, SM_DELIVER); 8276980Seric 8286980Seric /* finish up and exit */ 82958884Seric if (forkflag) 83024941Seric finis(); 83124941Seric else 83255012Seric dropenvelope(e); 8334632Seric } 834*61094Seric else if (!requeueflag) 83524941Seric { 83624941Seric /* 83724941Seric ** Parent -- pick up results. 83824941Seric */ 8394632Seric 84024941Seric errno = 0; 84124941Seric (void) waitfor(i); 84224941Seric } 8434632Seric } 8444632Seric /* 8454632Seric ** READQF -- read queue file and set up environment. 8464632Seric ** 8474632Seric ** Parameters: 8489377Seric ** e -- the envelope of the job to run. 8494632Seric ** 8504632Seric ** Returns: 85151920Seric ** TRUE if it successfully read the queue file. 85251920Seric ** FALSE otherwise. 8534632Seric ** 8544632Seric ** Side Effects: 85551920Seric ** The queue file is returned locked. 8564632Seric */ 8574632Seric 85851920Seric bool 85951920Seric readqf(e) 8609377Seric register ENVELOPE *e; 8614632Seric { 86217477Seric register FILE *qfp; 86354974Seric ADDRESS *ctladdr; 86456400Seric struct stat st; 86557135Seric char *bp; 86658915Seric char qf[20]; 86757135Seric char buf[MAXLINE]; 86824954Seric extern long atol(); 86954974Seric extern ADDRESS *setctluser(); 8704632Seric 8714632Seric /* 87217468Seric ** Read and process the file. 8734632Seric */ 8744632Seric 87558915Seric strcpy(qf, queuename(e, 'q')); 87651937Seric qfp = fopen(qf, "r+"); 87717477Seric if (qfp == NULL) 87817477Seric { 87958884Seric if (tTd(40, 8)) 88058884Seric printf("readqf(%s): fopen failure (%s)\n", 88158884Seric qf, errstring(errno)); 88240934Srick if (errno != ENOENT) 88340934Srick syserr("readqf: no control file %s", qf); 88451920Seric return FALSE; 88517477Seric } 88640934Srick 88756400Seric /* 88856400Seric ** Check the queue file for plausibility to avoid attacks. 88956400Seric */ 89056400Seric 89156400Seric if (fstat(fileno(qfp), &st) < 0) 89256400Seric { 89356400Seric /* must have been being processed by someone else */ 89458884Seric if (tTd(40, 8)) 89558884Seric printf("readqf(%s): fstat failure (%s)\n", 89658884Seric qf, errstring(errno)); 89756400Seric fclose(qfp); 89856400Seric return FALSE; 89956400Seric } 90056400Seric 90157135Seric if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode) 90256400Seric { 90356400Seric # ifdef LOG 90456400Seric if (LogLevel > 0) 90556400Seric { 90656400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 90756400Seric e->e_id, st.st_uid, st.st_mode); 90856400Seric } 90956795Seric # endif /* LOG */ 91058884Seric if (tTd(40, 8)) 91158884Seric printf("readqf(%s): bogus file\n", qf); 91256400Seric fclose(qfp); 91356400Seric return FALSE; 91456400Seric } 91556400Seric 91658689Seric if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB)) 91740934Srick { 91858689Seric /* being processed by another queuer */ 91958884Seric if (tTd(40, 8)) 92058884Seric printf("readqf(%s): locked\n", qf); 92158689Seric if (Verbose) 92258689Seric printf("%s: locked\n", e->e_id); 92351920Seric # ifdef LOG 92458689Seric if (LogLevel > 19) 92558689Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 92656795Seric # endif /* LOG */ 92740934Srick (void) fclose(qfp); 92851920Seric return FALSE; 92940934Srick } 93040934Srick 93159101Seric if (st.st_size == 0) 93259101Seric { 93359101Seric /* must be a bogus file -- just remove it */ 93459101Seric (void) unlink(qf); 93559101Seric fclose(qfp); 93659101Seric return FALSE; 93759101Seric } 93859101Seric 93951920Seric /* save this lock */ 94051920Seric e->e_lockfp = qfp; 94151920Seric 94240934Srick /* do basic system initialization */ 94355012Seric initsys(e); 94440934Srick 94517477Seric FileName = qf; 9469377Seric LineNumber = 0; 94751920Seric if (Verbose) 9489377Seric printf("\nRunning %s\n", e->e_id); 94954974Seric ctladdr = NULL; 95057135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 9514632Seric { 95258737Seric register char *p; 95357529Seric struct stat st; 95457529Seric 95526504Seric if (tTd(40, 4)) 95657135Seric printf("+++++ %s\n", bp); 95757135Seric switch (bp[0]) 9584632Seric { 95940973Sbostic case 'C': /* specify controlling user */ 96057135Seric ctladdr = setctluser(&bp[1]); 96140973Sbostic break; 96240973Sbostic 9634632Seric case 'R': /* specify recipient */ 96458082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 9654632Seric break; 9664632Seric 96725687Seric case 'E': /* specify error recipient */ 96858082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 96925687Seric break; 97025687Seric 9714632Seric case 'H': /* header */ 97257135Seric (void) chompheader(&bp[1], FALSE, e); 9734632Seric break; 9744632Seric 97510108Seric case 'M': /* message */ 97657135Seric e->e_message = newstr(&bp[1]); 97710108Seric break; 97810108Seric 9794632Seric case 'S': /* sender */ 98058704Seric setsender(newstr(&bp[1]), e, NULL, TRUE); 9814632Seric break; 9824632Seric 98359093Seric case 'B': /* body type */ 98459093Seric e->e_bodytype = newstr(&bp[1]); 98559093Seric break; 98659093Seric 9874632Seric case 'D': /* data file name */ 98857135Seric e->e_df = newstr(&bp[1]); 9899544Seric e->e_dfp = fopen(e->e_df, "r"); 9909544Seric if (e->e_dfp == NULL) 99158020Seric { 9929377Seric syserr("readqf: cannot open %s", e->e_df); 99358020Seric e->e_msgsize = -1; 99458020Seric } 99558020Seric else if (fstat(fileno(e->e_dfp), &st) >= 0) 99657529Seric e->e_msgsize = st.st_size; 9974632Seric break; 9984632Seric 9997860Seric case 'T': /* init time */ 100057135Seric e->e_ctime = atol(&bp[1]); 10014632Seric break; 10024632Seric 10034634Seric case 'P': /* message priority */ 100457135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 10054634Seric break; 10064634Seric 100758737Seric case 'F': /* flag bits */ 100858737Seric for (p = &bp[1]; *p != '\0'; p++) 100958737Seric { 101058737Seric switch (*p) 101158737Seric { 101258737Seric case 'w': /* warning sent */ 101358737Seric e->e_flags |= EF_WARNING; 101458737Seric break; 101558737Seric 101658737Seric case 'r': /* response */ 101758737Seric e->e_flags |= EF_RESPONSE; 101858737Seric break; 101958737Seric } 102058737Seric } 102158737Seric break; 102258737Seric 102353400Seric case '$': /* define macro */ 102457135Seric define(bp[1], newstr(&bp[2]), e); 102553400Seric break; 102653400Seric 102724941Seric case '\0': /* blank line; ignore */ 102824941Seric break; 102924941Seric 10304632Seric default: 103159700Seric syserr("readqf: bad line \"%s\"", e->e_id, 103257135Seric LineNumber, bp); 10334632Seric break; 10344632Seric } 103557135Seric 103657135Seric if (bp != buf) 103757135Seric free(bp); 10384632Seric } 10399377Seric 10409377Seric FileName = NULL; 104124941Seric 104224941Seric /* 104324941Seric ** If we haven't read any lines, this queue file is empty. 104424941Seric ** Arrange to remove it without referencing any null pointers. 104524941Seric */ 104624941Seric 104724941Seric if (LineNumber == 0) 104824941Seric { 104924941Seric errno = 0; 105024941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 105124941Seric } 105251920Seric return TRUE; 10534632Seric } 10544632Seric /* 10559630Seric ** PRINTQUEUE -- print out a representation of the mail queue 10569630Seric ** 10579630Seric ** Parameters: 10589630Seric ** none. 10599630Seric ** 10609630Seric ** Returns: 10619630Seric ** none. 10629630Seric ** 10639630Seric ** Side Effects: 10649630Seric ** Prints a listing of the mail queue on the standard output. 10659630Seric */ 10665182Seric 10679630Seric printqueue() 10689630Seric { 10699630Seric register WORK *w; 10709630Seric FILE *f; 107110070Seric int nrequests; 10729630Seric char buf[MAXLINE]; 10739630Seric 10749630Seric /* 107558250Seric ** Check for permission to print the queue 107658250Seric */ 107758250Seric 107858523Seric if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && getuid() != 0) 107958250Seric { 108058250Seric struct stat st; 108158523Seric # ifdef NGROUPS 108258523Seric int n; 108358523Seric int gidset[NGROUPS]; 108458523Seric # endif 108558250Seric 108658517Seric if (stat(QueueDir, &st) < 0) 108758250Seric { 108858250Seric syserr("Cannot stat %s", QueueDir); 108958250Seric return; 109058250Seric } 109158523Seric # ifdef NGROUPS 109258523Seric n = getgroups(NGROUPS, gidset); 109358523Seric while (--n >= 0) 109458523Seric { 109558523Seric if (gidset[n] == st.st_gid) 109658523Seric break; 109758523Seric } 109858523Seric if (n < 0) 109958523Seric # else 110058250Seric if (getgid() != st.st_gid) 110158523Seric # endif 110258250Seric { 110358250Seric usrerr("510 You are not permitted to see the queue"); 110458250Seric setstat(EX_NOPERM); 110558250Seric return; 110658250Seric } 110758250Seric } 110858250Seric 110958250Seric /* 11109630Seric ** Read and order the queue. 11119630Seric */ 11129630Seric 111324941Seric nrequests = orderq(TRUE); 11149630Seric 11159630Seric /* 11169630Seric ** Print the work list that we have read. 11179630Seric */ 11189630Seric 11199630Seric /* first see if there is anything */ 112010070Seric if (nrequests <= 0) 11219630Seric { 112210070Seric printf("Mail queue is empty\n"); 11239630Seric return; 11249630Seric } 11259630Seric 112651920Seric CurrentLA = getla(); /* get load average */ 112740934Srick 112810096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 112925687Seric if (nrequests > QUEUESIZE) 113025687Seric printf(", only %d printed", QUEUESIZE); 113124979Seric if (Verbose) 113258716Seric printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 113324979Seric else 113458716Seric printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 11359630Seric for (w = WorkQ; w != NULL; w = w->w_next) 11369630Seric { 11379630Seric struct stat st; 113810070Seric auto time_t submittime = 0; 113910070Seric long dfsize = -1; 114058737Seric int flags = 0; 114110108Seric char message[MAXLINE]; 114259093Seric char bodytype[MAXNAME]; 11439630Seric 114417468Seric f = fopen(w->w_name, "r"); 114517468Seric if (f == NULL) 114617468Seric { 114717468Seric errno = 0; 114817468Seric continue; 114917468Seric } 115058724Seric printf("%8s", w->w_name + 2); 115158689Seric if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB)) 115210070Seric printf("*"); 115357438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 115424941Seric printf("X"); 115510070Seric else 115610070Seric printf(" "); 115710070Seric errno = 0; 115817468Seric 115959093Seric message[0] = bodytype[0] = '\0'; 11609630Seric while (fgets(buf, sizeof buf, f) != NULL) 11619630Seric { 116253400Seric register int i; 116358737Seric register char *p; 116453400Seric 11659630Seric fixcrlf(buf, TRUE); 11669630Seric switch (buf[0]) 11679630Seric { 116810108Seric case 'M': /* error message */ 116953400Seric if ((i = strlen(&buf[1])) >= sizeof message) 117058737Seric i = sizeof message - 1; 117153400Seric bcopy(&buf[1], message, i); 117253400Seric message[i] = '\0'; 117310108Seric break; 117410108Seric 117559093Seric case 'B': /* body type */ 117659093Seric if ((i = strlen(&buf[1])) >= sizeof bodytype) 117759093Seric i = sizeof bodytype - 1; 117859093Seric bcopy(&buf[1], bodytype, i); 117959093Seric bodytype[i] = '\0'; 118059093Seric break; 118159093Seric 11829630Seric case 'S': /* sender name */ 118324979Seric if (Verbose) 118458737Seric printf("%8ld %10ld%c%.12s %.38s", 118558737Seric dfsize, 118658737Seric w->w_pri, 118758737Seric bitset(EF_WARNING, flags) ? '+' : ' ', 118858737Seric ctime(&submittime) + 4, 118924979Seric &buf[1]); 119024979Seric else 119124979Seric printf("%8ld %.16s %.45s", dfsize, 119224979Seric ctime(&submittime), &buf[1]); 119359093Seric if (message[0] != '\0' || bodytype[0] != '\0') 119459093Seric { 119559093Seric printf("\n %10.10s", bodytype); 119659093Seric if (message[0] != '\0') 119759093Seric printf(" (%.60s)", message); 119859093Seric } 11999630Seric break; 120051920Seric 120140973Sbostic case 'C': /* controlling user */ 120254974Seric if (Verbose) 120358716Seric printf("\n\t\t\t\t (---%.34s---)", 120458716Seric &buf[1]); 120540973Sbostic break; 12069630Seric 12079630Seric case 'R': /* recipient name */ 120824979Seric if (Verbose) 120958716Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 121024979Seric else 121158716Seric printf("\n\t\t\t\t %.45s", &buf[1]); 12129630Seric break; 12139630Seric 12149630Seric case 'T': /* creation time */ 121524941Seric submittime = atol(&buf[1]); 12169630Seric break; 121710070Seric 121810070Seric case 'D': /* data file name */ 121910070Seric if (stat(&buf[1], &st) >= 0) 122010070Seric dfsize = st.st_size; 122110070Seric break; 122258737Seric 122358737Seric case 'F': /* flag bits */ 122458737Seric for (p = &buf[1]; *p != '\0'; p++) 122558737Seric { 122658737Seric switch (*p) 122758737Seric { 122858737Seric case 'w': 122958737Seric flags |= EF_WARNING; 123058737Seric break; 123158737Seric } 123258737Seric } 12339630Seric } 12349630Seric } 123510070Seric if (submittime == (time_t) 0) 123610070Seric printf(" (no control file)"); 12379630Seric printf("\n"); 123823098Seric (void) fclose(f); 12399630Seric } 12409630Seric } 12419630Seric 124256795Seric # endif /* QUEUE */ 124317468Seric /* 124417468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 124517468Seric ** 124617468Seric ** Assigns an id code if one does not already exist. 124717468Seric ** This code is very careful to avoid trashing existing files 124817468Seric ** under any circumstances. 124917468Seric ** 125017468Seric ** Parameters: 125117468Seric ** e -- envelope to build it in/from. 125217468Seric ** type -- the file type, used as the first character 125317468Seric ** of the file name. 125417468Seric ** 125517468Seric ** Returns: 125617468Seric ** a pointer to the new file name (in a static buffer). 125717468Seric ** 125817468Seric ** Side Effects: 125951920Seric ** If no id code is already assigned, queuename will 126051920Seric ** assign an id code, create a qf file, and leave a 126151920Seric ** locked, open-for-write file pointer in the envelope. 126217468Seric */ 126317468Seric 126417468Seric char * 126517468Seric queuename(e, type) 126617468Seric register ENVELOPE *e; 126759700Seric int type; 126817468Seric { 126917468Seric static int pid = -1; 127058741Seric static char c0; 127158741Seric static char c1; 127258741Seric static char c2; 127358716Seric time_t now; 127458716Seric struct tm *tm; 127558689Seric static char buf[MAXNAME]; 127617468Seric 127717468Seric if (e->e_id == NULL) 127817468Seric { 127917468Seric char qf[20]; 128017468Seric 128117468Seric /* find a unique id */ 128217468Seric if (pid != getpid()) 128317468Seric { 128417468Seric /* new process -- start back at "AA" */ 128517468Seric pid = getpid(); 128658716Seric now = curtime(); 128758716Seric tm = localtime(&now); 128858716Seric c0 = 'A' + tm->tm_hour; 128917468Seric c1 = 'A'; 129017468Seric c2 = 'A' - 1; 129117468Seric } 129258716Seric (void) sprintf(qf, "qf%cAA%05d", c0, pid); 129317468Seric 129417468Seric while (c1 < '~' || c2 < 'Z') 129517468Seric { 129617468Seric int i; 129717468Seric 129817468Seric if (c2 >= 'Z') 129917468Seric { 130017468Seric c1++; 130117468Seric c2 = 'A' - 1; 130217468Seric } 130358716Seric qf[3] = c1; 130458716Seric qf[4] = ++c2; 130517468Seric if (tTd(7, 20)) 130640934Srick printf("queuename: trying \"%s\"\n", qf); 130717468Seric 130840934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 130951920Seric if (i < 0) 131051920Seric { 131151920Seric if (errno == EEXIST) 131251920Seric continue; 131351920Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 131451920Seric qf, QueueDir); 131551920Seric exit(EX_UNAVAILABLE); 131651920Seric } 131758689Seric if (lockfile(i, qf, LOCK_EX|LOCK_NB)) 131851920Seric { 131951920Seric e->e_lockfp = fdopen(i, "w"); 132040934Srick break; 132117468Seric } 132251920Seric 132351920Seric /* a reader got the file; abandon it and try again */ 132451920Seric (void) close(i); 132517468Seric } 132617468Seric if (c1 >= '~' && c2 >= 'Z') 132717468Seric { 132817468Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 132917468Seric qf, QueueDir); 133017468Seric exit(EX_OSERR); 133117468Seric } 133217468Seric e->e_id = newstr(&qf[2]); 133317468Seric define('i', e->e_id, e); 133417468Seric if (tTd(7, 1)) 133517468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 133617468Seric # ifdef LOG 133758020Seric if (LogLevel > 93) 133817468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 133956795Seric # endif /* LOG */ 134017468Seric } 134117468Seric 134217468Seric if (type == '\0') 134317468Seric return (NULL); 134417468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 134517468Seric if (tTd(7, 2)) 134617468Seric printf("queuename: %s\n", buf); 134717468Seric return (buf); 134817468Seric } 134917468Seric /* 135017468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 135117468Seric ** 135217468Seric ** Parameters: 135317468Seric ** e -- the envelope to unlock. 135417468Seric ** 135517468Seric ** Returns: 135617468Seric ** none 135717468Seric ** 135817468Seric ** Side Effects: 135917468Seric ** unlocks the queue for `e'. 136017468Seric */ 136117468Seric 136217468Seric unlockqueue(e) 136317468Seric ENVELOPE *e; 136417468Seric { 136558680Seric if (tTd(51, 4)) 136658680Seric printf("unlockqueue(%s)\n", e->e_id); 136758680Seric 136851920Seric /* if there is a lock file in the envelope, close it */ 136951920Seric if (e->e_lockfp != NULL) 137058680Seric xfclose(e->e_lockfp, "unlockqueue", e->e_id); 137151920Seric e->e_lockfp = NULL; 137251920Seric 137358728Seric /* don't create a queue id if we don't already have one */ 137458728Seric if (e->e_id == NULL) 137558728Seric return; 137658728Seric 137717468Seric /* remove the transcript */ 137817468Seric # ifdef LOG 137958020Seric if (LogLevel > 87) 138017468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 138156795Seric # endif /* LOG */ 138258680Seric if (!tTd(51, 104)) 138317468Seric xunlink(queuename(e, 'x')); 138417468Seric 138517468Seric } 138640973Sbostic /* 138754974Seric ** SETCTLUSER -- create a controlling address 138840973Sbostic ** 138954974Seric ** Create a fake "address" given only a local login name; this is 139054974Seric ** used as a "controlling user" for future recipient addresses. 139140973Sbostic ** 139240973Sbostic ** Parameters: 139354974Seric ** user -- the user name of the controlling user. 139440973Sbostic ** 139540973Sbostic ** Returns: 139654974Seric ** An address descriptor for the controlling user. 139740973Sbostic ** 139840973Sbostic ** Side Effects: 139940973Sbostic ** none. 140040973Sbostic */ 140140973Sbostic 140254974Seric ADDRESS * 140354974Seric setctluser(user) 140454974Seric char *user; 140540973Sbostic { 140654974Seric register ADDRESS *a; 140740973Sbostic struct passwd *pw; 140859113Seric char *p; 140940973Sbostic 141040973Sbostic /* 141154974Seric ** See if this clears our concept of controlling user. 141240973Sbostic */ 141340973Sbostic 141459270Seric if (user == NULL) 141559270Seric user = ""; 141640973Sbostic 141740973Sbostic /* 141854974Seric ** Set up addr fields for controlling user. 141940973Sbostic */ 142040973Sbostic 142154974Seric a = (ADDRESS *) xalloc(sizeof *a); 142254974Seric bzero((char *) a, sizeof *a); 142359113Seric 142459113Seric p = strchr(user, ':'); 142559113Seric if (p != NULL) 142659113Seric *p++ = '\0'; 142759270Seric if (*user != '\0' && (pw = getpwnam(user)) != NULL) 142840973Sbostic { 142940973Sbostic a->q_home = newstr(pw->pw_dir); 143040973Sbostic a->q_uid = pw->pw_uid; 143140973Sbostic a->q_gid = pw->pw_gid; 143257642Seric a->q_user = newstr(user); 143359270Seric a->q_flags |= QGOODUID; 143440973Sbostic } 143540973Sbostic else 143640973Sbostic { 143757642Seric a->q_user = newstr(DefUser); 143840973Sbostic } 143940973Sbostic 144059270Seric a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 144156328Seric a->q_mailer = LocalMailer; 144259113Seric if (p == NULL) 144359113Seric a->q_paddr = a->q_user; 144459113Seric else 144559113Seric a->q_paddr = newstr(p); 144654974Seric return a; 144740973Sbostic } 1448