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*60603Seric static char sccsid[] = "@(#)queue.c 6.60 (Berkeley) 05/30/93 (with queueing)"; 1433731Sbostic #else 15*60603Seric static char sccsid[] = "@(#)queue.c 6.60 (Berkeley) 05/30/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); 280*60603Seric 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. 74258884Seric ** e - the envelope in which to run it. 7434632Seric ** 7444632Seric ** Returns: 7454632Seric ** none. 7464632Seric ** 7474632Seric ** Side Effects: 7484632Seric ** The work request is satisfied if possible. 7494632Seric */ 7504632Seric 75158924Seric dowork(id, forkflag, requeueflag, e) 75258884Seric char *id; 75358884Seric bool forkflag; 75458924Seric bool requeueflag; 75555012Seric register ENVELOPE *e; 7564632Seric { 7574632Seric register int i; 75851920Seric extern bool readqf(); 7594632Seric 7607677Seric if (tTd(40, 1)) 76158884Seric printf("dowork(%s)\n", id); 7624632Seric 7634632Seric /* 76424941Seric ** Fork for work. 76524941Seric */ 76624941Seric 76758884Seric if (forkflag) 76824941Seric { 76924941Seric i = fork(); 77024941Seric if (i < 0) 77124941Seric { 77224941Seric syserr("dowork: cannot fork"); 77324941Seric return; 77424941Seric } 77524941Seric } 77624941Seric else 77724941Seric { 77824941Seric i = 0; 77924941Seric } 78024941Seric 7814632Seric if (i == 0) 7824632Seric { 7834632Seric /* 7844632Seric ** CHILD 7858148Seric ** Lock the control file to avoid duplicate deliveries. 7868148Seric ** Then run the file as though we had just read it. 7877350Seric ** We save an idea of the temporary name so we 7887350Seric ** can recover on interrupt. 7894632Seric */ 7904632Seric 7917763Seric /* set basic modes, etc. */ 7927356Seric (void) alarm(0); 79355012Seric clearenvelope(e, FALSE); 79458737Seric e->e_flags |= EF_QUEUERUN; 79558734Seric e->e_errormode = EM_MAIL; 79658884Seric e->e_id = id; 7977876Seric # ifdef LOG 79858020Seric if (LogLevel > 76) 79955012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 8007881Seric getpid()); 80156795Seric # endif /* LOG */ 8027763Seric 8037763Seric /* don't use the headers from sendmail.cf... */ 80455012Seric e->e_header = NULL; 8057763Seric 80651920Seric /* read the queue control file -- return if locked */ 80755012Seric if (!readqf(e)) 8086980Seric { 80958884Seric if (tTd(40, 4)) 81058884Seric printf("readqf(%s) failed\n", e->e_id); 81158884Seric if (forkflag) 81224941Seric exit(EX_OK); 81324941Seric else 81424941Seric return; 8156980Seric } 8166980Seric 81755012Seric e->e_flags |= EF_INQUEUE; 81858929Seric eatheader(e, requeueflag); 8196980Seric 82058924Seric if (requeueflag) 82158924Seric queueup(e, TRUE, FALSE); 82258924Seric 8236980Seric /* do the delivery */ 82458915Seric sendall(e, SM_DELIVER); 8256980Seric 8266980Seric /* finish up and exit */ 82758884Seric if (forkflag) 82824941Seric finis(); 82924941Seric else 83055012Seric dropenvelope(e); 8314632Seric } 83224941Seric else 83324941Seric { 83424941Seric /* 83524941Seric ** Parent -- pick up results. 83624941Seric */ 8374632Seric 83824941Seric errno = 0; 83924941Seric (void) waitfor(i); 84024941Seric } 8414632Seric } 8424632Seric /* 8434632Seric ** READQF -- read queue file and set up environment. 8444632Seric ** 8454632Seric ** Parameters: 8469377Seric ** e -- the envelope of the job to run. 8474632Seric ** 8484632Seric ** Returns: 84951920Seric ** TRUE if it successfully read the queue file. 85051920Seric ** FALSE otherwise. 8514632Seric ** 8524632Seric ** Side Effects: 85351920Seric ** The queue file is returned locked. 8544632Seric */ 8554632Seric 85651920Seric bool 85751920Seric readqf(e) 8589377Seric register ENVELOPE *e; 8594632Seric { 86017477Seric register FILE *qfp; 86154974Seric ADDRESS *ctladdr; 86256400Seric struct stat st; 86357135Seric char *bp; 86458915Seric char qf[20]; 86557135Seric char buf[MAXLINE]; 86624954Seric extern long atol(); 86754974Seric extern ADDRESS *setctluser(); 8684632Seric 8694632Seric /* 87017468Seric ** Read and process the file. 8714632Seric */ 8724632Seric 87358915Seric strcpy(qf, queuename(e, 'q')); 87451937Seric qfp = fopen(qf, "r+"); 87517477Seric if (qfp == NULL) 87617477Seric { 87758884Seric if (tTd(40, 8)) 87858884Seric printf("readqf(%s): fopen failure (%s)\n", 87958884Seric qf, errstring(errno)); 88040934Srick if (errno != ENOENT) 88140934Srick syserr("readqf: no control file %s", qf); 88251920Seric return FALSE; 88317477Seric } 88440934Srick 88556400Seric /* 88656400Seric ** Check the queue file for plausibility to avoid attacks. 88756400Seric */ 88856400Seric 88956400Seric if (fstat(fileno(qfp), &st) < 0) 89056400Seric { 89156400Seric /* must have been being processed by someone else */ 89258884Seric if (tTd(40, 8)) 89358884Seric printf("readqf(%s): fstat failure (%s)\n", 89458884Seric qf, errstring(errno)); 89556400Seric fclose(qfp); 89656400Seric return FALSE; 89756400Seric } 89856400Seric 89957135Seric if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode) 90056400Seric { 90156400Seric # ifdef LOG 90256400Seric if (LogLevel > 0) 90356400Seric { 90456400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 90556400Seric e->e_id, st.st_uid, st.st_mode); 90656400Seric } 90756795Seric # endif /* LOG */ 90858884Seric if (tTd(40, 8)) 90958884Seric printf("readqf(%s): bogus file\n", qf); 91056400Seric fclose(qfp); 91156400Seric return FALSE; 91256400Seric } 91356400Seric 91458689Seric if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB)) 91540934Srick { 91658689Seric /* being processed by another queuer */ 91758884Seric if (tTd(40, 8)) 91858884Seric printf("readqf(%s): locked\n", qf); 91958689Seric if (Verbose) 92058689Seric printf("%s: locked\n", e->e_id); 92151920Seric # ifdef LOG 92258689Seric if (LogLevel > 19) 92358689Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 92456795Seric # endif /* LOG */ 92540934Srick (void) fclose(qfp); 92651920Seric return FALSE; 92740934Srick } 92840934Srick 92959101Seric if (st.st_size == 0) 93059101Seric { 93159101Seric /* must be a bogus file -- just remove it */ 93259101Seric (void) unlink(qf); 93359101Seric fclose(qfp); 93459101Seric return FALSE; 93559101Seric } 93659101Seric 93751920Seric /* save this lock */ 93851920Seric e->e_lockfp = qfp; 93951920Seric 94040934Srick /* do basic system initialization */ 94155012Seric initsys(e); 94240934Srick 94317477Seric FileName = qf; 9449377Seric LineNumber = 0; 94551920Seric if (Verbose) 9469377Seric printf("\nRunning %s\n", e->e_id); 94754974Seric ctladdr = NULL; 94857135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 9494632Seric { 95058737Seric register char *p; 95157529Seric struct stat st; 95257529Seric 95326504Seric if (tTd(40, 4)) 95457135Seric printf("+++++ %s\n", bp); 95557135Seric switch (bp[0]) 9564632Seric { 95740973Sbostic case 'C': /* specify controlling user */ 95857135Seric ctladdr = setctluser(&bp[1]); 95940973Sbostic break; 96040973Sbostic 9614632Seric case 'R': /* specify recipient */ 96258082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 9634632Seric break; 9644632Seric 96525687Seric case 'E': /* specify error recipient */ 96658082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 96725687Seric break; 96825687Seric 9694632Seric case 'H': /* header */ 97057135Seric (void) chompheader(&bp[1], FALSE, e); 9714632Seric break; 9724632Seric 97310108Seric case 'M': /* message */ 97457135Seric e->e_message = newstr(&bp[1]); 97510108Seric break; 97610108Seric 9774632Seric case 'S': /* sender */ 97858704Seric setsender(newstr(&bp[1]), e, NULL, TRUE); 9794632Seric break; 9804632Seric 98159093Seric case 'B': /* body type */ 98259093Seric e->e_bodytype = newstr(&bp[1]); 98359093Seric break; 98459093Seric 9854632Seric case 'D': /* data file name */ 98657135Seric e->e_df = newstr(&bp[1]); 9879544Seric e->e_dfp = fopen(e->e_df, "r"); 9889544Seric if (e->e_dfp == NULL) 98958020Seric { 9909377Seric syserr("readqf: cannot open %s", e->e_df); 99158020Seric e->e_msgsize = -1; 99258020Seric } 99358020Seric else if (fstat(fileno(e->e_dfp), &st) >= 0) 99457529Seric e->e_msgsize = st.st_size; 9954632Seric break; 9964632Seric 9977860Seric case 'T': /* init time */ 99857135Seric e->e_ctime = atol(&bp[1]); 9994632Seric break; 10004632Seric 10014634Seric case 'P': /* message priority */ 100257135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 10034634Seric break; 10044634Seric 100558737Seric case 'F': /* flag bits */ 100658737Seric for (p = &bp[1]; *p != '\0'; p++) 100758737Seric { 100858737Seric switch (*p) 100958737Seric { 101058737Seric case 'w': /* warning sent */ 101158737Seric e->e_flags |= EF_WARNING; 101258737Seric break; 101358737Seric 101458737Seric case 'r': /* response */ 101558737Seric e->e_flags |= EF_RESPONSE; 101658737Seric break; 101758737Seric } 101858737Seric } 101958737Seric break; 102058737Seric 102153400Seric case '$': /* define macro */ 102257135Seric define(bp[1], newstr(&bp[2]), e); 102353400Seric break; 102453400Seric 102524941Seric case '\0': /* blank line; ignore */ 102624941Seric break; 102724941Seric 10284632Seric default: 102959700Seric syserr("readqf: bad line \"%s\"", e->e_id, 103057135Seric LineNumber, bp); 10314632Seric break; 10324632Seric } 103357135Seric 103457135Seric if (bp != buf) 103557135Seric free(bp); 10364632Seric } 10379377Seric 10389377Seric FileName = NULL; 103924941Seric 104024941Seric /* 104124941Seric ** If we haven't read any lines, this queue file is empty. 104224941Seric ** Arrange to remove it without referencing any null pointers. 104324941Seric */ 104424941Seric 104524941Seric if (LineNumber == 0) 104624941Seric { 104724941Seric errno = 0; 104824941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 104924941Seric } 105051920Seric return TRUE; 10514632Seric } 10524632Seric /* 10539630Seric ** PRINTQUEUE -- print out a representation of the mail queue 10549630Seric ** 10559630Seric ** Parameters: 10569630Seric ** none. 10579630Seric ** 10589630Seric ** Returns: 10599630Seric ** none. 10609630Seric ** 10619630Seric ** Side Effects: 10629630Seric ** Prints a listing of the mail queue on the standard output. 10639630Seric */ 10645182Seric 10659630Seric printqueue() 10669630Seric { 10679630Seric register WORK *w; 10689630Seric FILE *f; 106910070Seric int nrequests; 10709630Seric char buf[MAXLINE]; 10719630Seric 10729630Seric /* 107358250Seric ** Check for permission to print the queue 107458250Seric */ 107558250Seric 107658523Seric if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && getuid() != 0) 107758250Seric { 107858250Seric struct stat st; 107958523Seric # ifdef NGROUPS 108058523Seric int n; 108158523Seric int gidset[NGROUPS]; 108258523Seric # endif 108358250Seric 108458517Seric if (stat(QueueDir, &st) < 0) 108558250Seric { 108658250Seric syserr("Cannot stat %s", QueueDir); 108758250Seric return; 108858250Seric } 108958523Seric # ifdef NGROUPS 109058523Seric n = getgroups(NGROUPS, gidset); 109158523Seric while (--n >= 0) 109258523Seric { 109358523Seric if (gidset[n] == st.st_gid) 109458523Seric break; 109558523Seric } 109658523Seric if (n < 0) 109758523Seric # else 109858250Seric if (getgid() != st.st_gid) 109958523Seric # endif 110058250Seric { 110158250Seric usrerr("510 You are not permitted to see the queue"); 110258250Seric setstat(EX_NOPERM); 110358250Seric return; 110458250Seric } 110558250Seric } 110658250Seric 110758250Seric /* 11089630Seric ** Read and order the queue. 11099630Seric */ 11109630Seric 111124941Seric nrequests = orderq(TRUE); 11129630Seric 11139630Seric /* 11149630Seric ** Print the work list that we have read. 11159630Seric */ 11169630Seric 11179630Seric /* first see if there is anything */ 111810070Seric if (nrequests <= 0) 11199630Seric { 112010070Seric printf("Mail queue is empty\n"); 11219630Seric return; 11229630Seric } 11239630Seric 112451920Seric CurrentLA = getla(); /* get load average */ 112540934Srick 112610096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 112725687Seric if (nrequests > QUEUESIZE) 112825687Seric printf(", only %d printed", QUEUESIZE); 112924979Seric if (Verbose) 113058716Seric printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 113124979Seric else 113258716Seric printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 11339630Seric for (w = WorkQ; w != NULL; w = w->w_next) 11349630Seric { 11359630Seric struct stat st; 113610070Seric auto time_t submittime = 0; 113710070Seric long dfsize = -1; 113858737Seric int flags = 0; 113910108Seric char message[MAXLINE]; 114059093Seric char bodytype[MAXNAME]; 11419630Seric 114217468Seric f = fopen(w->w_name, "r"); 114317468Seric if (f == NULL) 114417468Seric { 114517468Seric errno = 0; 114617468Seric continue; 114717468Seric } 114858724Seric printf("%8s", w->w_name + 2); 114958689Seric if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB)) 115010070Seric printf("*"); 115157438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 115224941Seric printf("X"); 115310070Seric else 115410070Seric printf(" "); 115510070Seric errno = 0; 115617468Seric 115759093Seric message[0] = bodytype[0] = '\0'; 11589630Seric while (fgets(buf, sizeof buf, f) != NULL) 11599630Seric { 116053400Seric register int i; 116158737Seric register char *p; 116253400Seric 11639630Seric fixcrlf(buf, TRUE); 11649630Seric switch (buf[0]) 11659630Seric { 116610108Seric case 'M': /* error message */ 116753400Seric if ((i = strlen(&buf[1])) >= sizeof message) 116858737Seric i = sizeof message - 1; 116953400Seric bcopy(&buf[1], message, i); 117053400Seric message[i] = '\0'; 117110108Seric break; 117210108Seric 117359093Seric case 'B': /* body type */ 117459093Seric if ((i = strlen(&buf[1])) >= sizeof bodytype) 117559093Seric i = sizeof bodytype - 1; 117659093Seric bcopy(&buf[1], bodytype, i); 117759093Seric bodytype[i] = '\0'; 117859093Seric break; 117959093Seric 11809630Seric case 'S': /* sender name */ 118124979Seric if (Verbose) 118258737Seric printf("%8ld %10ld%c%.12s %.38s", 118358737Seric dfsize, 118458737Seric w->w_pri, 118558737Seric bitset(EF_WARNING, flags) ? '+' : ' ', 118658737Seric ctime(&submittime) + 4, 118724979Seric &buf[1]); 118824979Seric else 118924979Seric printf("%8ld %.16s %.45s", dfsize, 119024979Seric ctime(&submittime), &buf[1]); 119159093Seric if (message[0] != '\0' || bodytype[0] != '\0') 119259093Seric { 119359093Seric printf("\n %10.10s", bodytype); 119459093Seric if (message[0] != '\0') 119559093Seric printf(" (%.60s)", message); 119659093Seric } 11979630Seric break; 119851920Seric 119940973Sbostic case 'C': /* controlling user */ 120054974Seric if (Verbose) 120158716Seric printf("\n\t\t\t\t (---%.34s---)", 120258716Seric &buf[1]); 120340973Sbostic break; 12049630Seric 12059630Seric case 'R': /* recipient name */ 120624979Seric if (Verbose) 120758716Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 120824979Seric else 120958716Seric printf("\n\t\t\t\t %.45s", &buf[1]); 12109630Seric break; 12119630Seric 12129630Seric case 'T': /* creation time */ 121324941Seric submittime = atol(&buf[1]); 12149630Seric break; 121510070Seric 121610070Seric case 'D': /* data file name */ 121710070Seric if (stat(&buf[1], &st) >= 0) 121810070Seric dfsize = st.st_size; 121910070Seric break; 122058737Seric 122158737Seric case 'F': /* flag bits */ 122258737Seric for (p = &buf[1]; *p != '\0'; p++) 122358737Seric { 122458737Seric switch (*p) 122558737Seric { 122658737Seric case 'w': 122758737Seric flags |= EF_WARNING; 122858737Seric break; 122958737Seric } 123058737Seric } 12319630Seric } 12329630Seric } 123310070Seric if (submittime == (time_t) 0) 123410070Seric printf(" (no control file)"); 12359630Seric printf("\n"); 123623098Seric (void) fclose(f); 12379630Seric } 12389630Seric } 12399630Seric 124056795Seric # endif /* QUEUE */ 124117468Seric /* 124217468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 124317468Seric ** 124417468Seric ** Assigns an id code if one does not already exist. 124517468Seric ** This code is very careful to avoid trashing existing files 124617468Seric ** under any circumstances. 124717468Seric ** 124817468Seric ** Parameters: 124917468Seric ** e -- envelope to build it in/from. 125017468Seric ** type -- the file type, used as the first character 125117468Seric ** of the file name. 125217468Seric ** 125317468Seric ** Returns: 125417468Seric ** a pointer to the new file name (in a static buffer). 125517468Seric ** 125617468Seric ** Side Effects: 125751920Seric ** If no id code is already assigned, queuename will 125851920Seric ** assign an id code, create a qf file, and leave a 125951920Seric ** locked, open-for-write file pointer in the envelope. 126017468Seric */ 126117468Seric 126217468Seric char * 126317468Seric queuename(e, type) 126417468Seric register ENVELOPE *e; 126559700Seric int type; 126617468Seric { 126717468Seric static int pid = -1; 126858741Seric static char c0; 126958741Seric static char c1; 127058741Seric static char c2; 127158716Seric time_t now; 127258716Seric struct tm *tm; 127358689Seric static char buf[MAXNAME]; 127417468Seric 127517468Seric if (e->e_id == NULL) 127617468Seric { 127717468Seric char qf[20]; 127817468Seric 127917468Seric /* find a unique id */ 128017468Seric if (pid != getpid()) 128117468Seric { 128217468Seric /* new process -- start back at "AA" */ 128317468Seric pid = getpid(); 128458716Seric now = curtime(); 128558716Seric tm = localtime(&now); 128658716Seric c0 = 'A' + tm->tm_hour; 128717468Seric c1 = 'A'; 128817468Seric c2 = 'A' - 1; 128917468Seric } 129058716Seric (void) sprintf(qf, "qf%cAA%05d", c0, pid); 129117468Seric 129217468Seric while (c1 < '~' || c2 < 'Z') 129317468Seric { 129417468Seric int i; 129517468Seric 129617468Seric if (c2 >= 'Z') 129717468Seric { 129817468Seric c1++; 129917468Seric c2 = 'A' - 1; 130017468Seric } 130158716Seric qf[3] = c1; 130258716Seric qf[4] = ++c2; 130317468Seric if (tTd(7, 20)) 130440934Srick printf("queuename: trying \"%s\"\n", qf); 130517468Seric 130640934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 130751920Seric if (i < 0) 130851920Seric { 130951920Seric if (errno == EEXIST) 131051920Seric continue; 131151920Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 131251920Seric qf, QueueDir); 131351920Seric exit(EX_UNAVAILABLE); 131451920Seric } 131558689Seric if (lockfile(i, qf, LOCK_EX|LOCK_NB)) 131651920Seric { 131751920Seric e->e_lockfp = fdopen(i, "w"); 131840934Srick break; 131917468Seric } 132051920Seric 132151920Seric /* a reader got the file; abandon it and try again */ 132251920Seric (void) close(i); 132317468Seric } 132417468Seric if (c1 >= '~' && c2 >= 'Z') 132517468Seric { 132617468Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 132717468Seric qf, QueueDir); 132817468Seric exit(EX_OSERR); 132917468Seric } 133017468Seric e->e_id = newstr(&qf[2]); 133117468Seric define('i', e->e_id, e); 133217468Seric if (tTd(7, 1)) 133317468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 133417468Seric # ifdef LOG 133558020Seric if (LogLevel > 93) 133617468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 133756795Seric # endif /* LOG */ 133817468Seric } 133917468Seric 134017468Seric if (type == '\0') 134117468Seric return (NULL); 134217468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 134317468Seric if (tTd(7, 2)) 134417468Seric printf("queuename: %s\n", buf); 134517468Seric return (buf); 134617468Seric } 134717468Seric /* 134817468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 134917468Seric ** 135017468Seric ** Parameters: 135117468Seric ** e -- the envelope to unlock. 135217468Seric ** 135317468Seric ** Returns: 135417468Seric ** none 135517468Seric ** 135617468Seric ** Side Effects: 135717468Seric ** unlocks the queue for `e'. 135817468Seric */ 135917468Seric 136017468Seric unlockqueue(e) 136117468Seric ENVELOPE *e; 136217468Seric { 136358680Seric if (tTd(51, 4)) 136458680Seric printf("unlockqueue(%s)\n", e->e_id); 136558680Seric 136651920Seric /* if there is a lock file in the envelope, close it */ 136751920Seric if (e->e_lockfp != NULL) 136858680Seric xfclose(e->e_lockfp, "unlockqueue", e->e_id); 136951920Seric e->e_lockfp = NULL; 137051920Seric 137158728Seric /* don't create a queue id if we don't already have one */ 137258728Seric if (e->e_id == NULL) 137358728Seric return; 137458728Seric 137517468Seric /* remove the transcript */ 137617468Seric # ifdef LOG 137758020Seric if (LogLevel > 87) 137817468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 137956795Seric # endif /* LOG */ 138058680Seric if (!tTd(51, 104)) 138117468Seric xunlink(queuename(e, 'x')); 138217468Seric 138317468Seric } 138440973Sbostic /* 138554974Seric ** SETCTLUSER -- create a controlling address 138640973Sbostic ** 138754974Seric ** Create a fake "address" given only a local login name; this is 138854974Seric ** used as a "controlling user" for future recipient addresses. 138940973Sbostic ** 139040973Sbostic ** Parameters: 139154974Seric ** user -- the user name of the controlling user. 139240973Sbostic ** 139340973Sbostic ** Returns: 139454974Seric ** An address descriptor for the controlling user. 139540973Sbostic ** 139640973Sbostic ** Side Effects: 139740973Sbostic ** none. 139840973Sbostic */ 139940973Sbostic 140054974Seric ADDRESS * 140154974Seric setctluser(user) 140254974Seric char *user; 140340973Sbostic { 140454974Seric register ADDRESS *a; 140540973Sbostic struct passwd *pw; 140659113Seric char *p; 140740973Sbostic 140840973Sbostic /* 140954974Seric ** See if this clears our concept of controlling user. 141040973Sbostic */ 141140973Sbostic 141259270Seric if (user == NULL) 141359270Seric user = ""; 141440973Sbostic 141540973Sbostic /* 141654974Seric ** Set up addr fields for controlling user. 141740973Sbostic */ 141840973Sbostic 141954974Seric a = (ADDRESS *) xalloc(sizeof *a); 142054974Seric bzero((char *) a, sizeof *a); 142159113Seric 142259113Seric p = strchr(user, ':'); 142359113Seric if (p != NULL) 142459113Seric *p++ = '\0'; 142559270Seric if (*user != '\0' && (pw = getpwnam(user)) != NULL) 142640973Sbostic { 142740973Sbostic a->q_home = newstr(pw->pw_dir); 142840973Sbostic a->q_uid = pw->pw_uid; 142940973Sbostic a->q_gid = pw->pw_gid; 143057642Seric a->q_user = newstr(user); 143159270Seric a->q_flags |= QGOODUID; 143240973Sbostic } 143340973Sbostic else 143440973Sbostic { 143557642Seric a->q_user = newstr(DefUser); 143640973Sbostic } 143740973Sbostic 143859270Seric a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 143956328Seric a->q_mailer = LocalMailer; 144059113Seric if (p == NULL) 144159113Seric a->q_paddr = a->q_user; 144259113Seric else 144359113Seric a->q_paddr = newstr(p); 144454974Seric return a; 144540973Sbostic } 1446