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*58929Seric static char sccsid[] = "@(#)queue.c 6.44 (Berkeley) 04/01/93 (with queueing)"; 1433731Sbostic #else 15*58929Seric static char sccsid[] = "@(#)queue.c 6.44 (Berkeley) 04/01/93 (without queueing)"; 1633731Sbostic #endif 1733731Sbostic #endif /* not lint */ 1833731Sbostic 194632Seric # include <sys/stat.h> 2013707Ssam # include <sys/dir.h> 214634Seric # include <signal.h> 224632Seric # include <errno.h> 2340973Sbostic # include <pwd.h> 2457977Seric # ifndef MAXNAMLEN 2557736Seric # include <dirent.h> 2657736Seric # endif 274632Seric 2833731Sbostic # ifdef QUEUE 294632Seric 304632Seric /* 319377Seric ** Work queue. 329377Seric */ 339377Seric 349377Seric struct work 359377Seric { 369377Seric char *w_name; /* name of control file */ 379377Seric long w_pri; /* priority of message, see below */ 3825013Seric time_t w_ctime; /* creation time of message */ 399377Seric struct work *w_next; /* next in queue */ 409377Seric }; 419377Seric 429377Seric typedef struct work WORK; 439377Seric 449377Seric WORK *WorkQ; /* queue of things to be done */ 459377Seric /* 464632Seric ** QUEUEUP -- queue a message up for future transmission. 474632Seric ** 484632Seric ** Parameters: 496980Seric ** e -- the envelope to queue up. 506999Seric ** queueall -- if TRUE, queue all addresses, rather than 516999Seric ** just those with the QQUEUEUP flag set. 529377Seric ** announce -- if TRUE, tell when you are queueing up. 534632Seric ** 544632Seric ** Returns: 5551920Seric ** none. 564632Seric ** 574632Seric ** Side Effects: 589377Seric ** The current request are saved in a control file. 5951920Seric ** The queue file is left locked. 604632Seric */ 614632Seric 629377Seric queueup(e, queueall, announce) 636980Seric register ENVELOPE *e; 646999Seric bool queueall; 659377Seric bool announce; 664632Seric { 677812Seric char *qf; 687812Seric register FILE *tfp; 694632Seric register HDR *h; 705007Seric register ADDRESS *q; 7151920Seric int fd; 7251920Seric int i; 7351920Seric bool newid; 7453400Seric register char *p; 7510173Seric MAILER nullmailer; 7654974Seric ADDRESS *lastctladdr; 7751920Seric char buf[MAXLINE], tf[MAXLINE]; 7853400Seric extern char *macvalue(); 7954974Seric extern ADDRESS *getctladdr(); 804632Seric 815037Seric /* 8217477Seric ** Create control file. 835037Seric */ 844632Seric 8551920Seric newid = (e->e_id == NULL); 8651920Seric strcpy(tf, queuename(e, 't')); 8751920Seric tfp = e->e_lockfp; 8851920Seric if (tfp == NULL) 8951920Seric newid = FALSE; 9051920Seric if (newid) 9151835Seric { 9251920Seric tfp = e->e_lockfp; 9351920Seric } 9451920Seric else 9551920Seric { 9651920Seric /* get a locked tf file */ 9751920Seric for (i = 100; --i >= 0; ) 9851835Seric { 9958689Seric extern bool lockfile(); 10051937Seric 10151920Seric fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode); 10251920Seric if (fd < 0) 10351835Seric { 10451920Seric if (errno == EEXIST) 10551920Seric continue; 10658801Seric notemp: 10758690Seric syserr("!queueup: cannot create temp file %s", tf); 10841636Srick } 10958689Seric 11058689Seric if (lockfile(fd, tf, LOCK_EX|LOCK_NB)) 11151920Seric break; 11258689Seric 11351920Seric close(fd); 11458801Seric sleep(i); 11541636Srick } 11658801Seric if (fd < 0) 11758801Seric goto notemp; 11841636Srick 11951920Seric tfp = fdopen(fd, "w"); 12051920Seric } 1214632Seric 1227677Seric if (tTd(40, 1)) 12317468Seric printf("queueing %s\n", e->e_id); 1244632Seric 1254632Seric /* 1266980Seric ** If there is no data file yet, create one. 1276980Seric */ 1286980Seric 1296980Seric if (e->e_df == NULL) 1306980Seric { 1316980Seric register FILE *dfp; 1329389Seric extern putbody(); 1336980Seric 1347812Seric e->e_df = newstr(queuename(e, 'd')); 13540934Srick fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode); 13640934Srick if (fd < 0) 13758690Seric syserr("!queueup: cannot create %s", e->e_df); 13840934Srick dfp = fdopen(fd, "w"); 13910173Seric (*e->e_putbody)(dfp, ProgMailer, e); 14058680Seric (void) xfclose(dfp, "queueup dfp", e->e_id); 1419389Seric e->e_putbody = putbody; 1426980Seric } 1436980Seric 1446980Seric /* 1454632Seric ** Output future work requests. 14625687Seric ** Priority and creation time should be first, since 14725687Seric ** they are required by orderq. 1484632Seric */ 1494632Seric 1509377Seric /* output message priority */ 1519377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 1529377Seric 1539630Seric /* output creation time */ 1549630Seric fprintf(tfp, "T%ld\n", e->e_ctime); 1559630Seric 1564632Seric /* output name of data file */ 1577812Seric fprintf(tfp, "D%s\n", e->e_df); 1584632Seric 15910108Seric /* message from envelope, if it exists */ 16010108Seric if (e->e_message != NULL) 16110108Seric fprintf(tfp, "M%s\n", e->e_message); 16210108Seric 16358737Seric /* send various flag bits through */ 16458737Seric p = buf; 16558737Seric if (bitset(EF_WARNING, e->e_flags)) 16658737Seric *p++ = 'w'; 16758737Seric if (bitset(EF_RESPONSE, e->e_flags)) 16858737Seric *p++ = 'r'; 16958737Seric *p++ = '\0'; 17058737Seric if (buf[0] != '\0') 17158737Seric fprintf(tfp, "F%s\n", buf); 17258737Seric 17353400Seric /* $r and $s macro values */ 17453400Seric if ((p = macvalue('r', e)) != NULL) 17553400Seric fprintf(tfp, "$r%s\n", p); 17653400Seric if ((p = macvalue('s', e)) != NULL) 17753400Seric fprintf(tfp, "$s%s\n", p); 17853400Seric 1794632Seric /* output name of sender */ 1807812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 1814632Seric 18255360Seric /* output list of error recipients */ 18355360Seric lastctladdr = NULL; 18455360Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 18555360Seric { 18658680Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 18755360Seric { 18855360Seric ADDRESS *ctladdr; 18955360Seric 19055360Seric ctladdr = getctladdr(q); 19155360Seric if (ctladdr != lastctladdr) 19255360Seric { 19355360Seric printctladdr(ctladdr, tfp); 19455360Seric lastctladdr = ctladdr; 19555360Seric } 19655360Seric fprintf(tfp, "E%s\n", q->q_paddr); 19755360Seric } 19855360Seric } 19955360Seric 2004632Seric /* output list of recipient addresses */ 2016980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 2024632Seric { 20358250Seric if (bitset(QQUEUEUP, q->q_flags) || 20458680Seric (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags))) 2058245Seric { 20654974Seric ADDRESS *ctladdr; 20740973Sbostic 20854975Seric ctladdr = getctladdr(q); 20954975Seric if (ctladdr != lastctladdr) 21054974Seric { 21154974Seric printctladdr(ctladdr, tfp); 21254974Seric lastctladdr = ctladdr; 21354974Seric } 2147812Seric fprintf(tfp, "R%s\n", q->q_paddr); 2159377Seric if (announce) 2169377Seric { 2179377Seric e->e_to = q->q_paddr; 21858151Seric message("queued"); 21958020Seric if (LogLevel > 8) 22058337Seric logdelivery(NULL, NULL, "queued", e); 2219377Seric e->e_to = NULL; 2229377Seric } 2239387Seric if (tTd(40, 1)) 2249387Seric { 2259387Seric printf("queueing "); 2269387Seric printaddr(q, FALSE); 2279387Seric } 2288245Seric } 2294632Seric } 2304632Seric 2319377Seric /* 2329377Seric ** Output headers for this message. 2339377Seric ** Expand macros completely here. Queue run will deal with 2349377Seric ** everything as absolute headers. 2359377Seric ** All headers that must be relative to the recipient 2369377Seric ** can be cracked later. 23710173Seric ** We set up a "null mailer" -- i.e., a mailer that will have 23810173Seric ** no effect on the addresses as they are output. 2399377Seric */ 2409377Seric 24110686Seric bzero((char *) &nullmailer, sizeof nullmailer); 24258020Seric nullmailer.m_re_rwset = nullmailer.m_rh_rwset = 24358020Seric nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1; 24410349Seric nullmailer.m_eol = "\n"; 24510173Seric 24658050Seric define('g', "\201f", e); 2476980Seric for (h = e->e_header; h != NULL; h = h->h_link) 2484632Seric { 24910686Seric extern bool bitzerop(); 25010686Seric 25112015Seric /* don't output null headers */ 2524632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 2534632Seric continue; 25412015Seric 25512015Seric /* don't output resent headers on non-resent messages */ 25612015Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 25712015Seric continue; 25812015Seric 25912015Seric /* output this header */ 2607812Seric fprintf(tfp, "H"); 26112015Seric 26212015Seric /* if conditional, output the set of conditions */ 26310686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 26410686Seric { 26510686Seric int j; 26610686Seric 26723098Seric (void) putc('?', tfp); 26810686Seric for (j = '\0'; j <= '\177'; j++) 26910686Seric if (bitnset(j, h->h_mflags)) 27023098Seric (void) putc(j, tfp); 27123098Seric (void) putc('?', tfp); 27210686Seric } 27312015Seric 27412015Seric /* output the header: expand macros, convert addresses */ 2757763Seric if (bitset(H_DEFAULT, h->h_flags)) 2767763Seric { 2777763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 2788236Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 2797763Seric } 2808245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 2819348Seric { 28258737Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 28358737Seric 28458737Seric if (bitset(H_FROM, h->h_flags)) 28558737Seric oldstyle = FALSE; 28658737Seric 28758737Seric commaize(h, h->h_value, tfp, oldstyle, 28855012Seric &nullmailer, e); 2899348Seric } 2907763Seric else 2918245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 2924632Seric } 2934632Seric 2944632Seric /* 2954632Seric ** Clean up. 2964632Seric */ 2974632Seric 29858732Seric fflush(tfp); 29958732Seric if (ferror(tfp)) 30058732Seric { 30158732Seric if (newid) 30258732Seric syserr("!552 Error writing control file %s", tf); 30358732Seric else 30458732Seric syserr("!452 Error writing control file %s", tf); 30558732Seric } 30658732Seric 30751920Seric if (!newid) 30851920Seric { 30951920Seric qf = queuename(e, 'q'); 31051920Seric if (rename(tf, qf) < 0) 31151920Seric syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df); 31251920Seric if (e->e_lockfp != NULL) 31358680Seric (void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id); 31451920Seric e->e_lockfp = tfp; 31551920Seric } 31651920Seric else 31751920Seric qf = tf; 31841636Srick errno = 0; 3197391Seric 3207677Seric # ifdef LOG 3217677Seric /* save log info */ 32258020Seric if (LogLevel > 79) 3237878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 32456795Seric # endif /* LOG */ 32540934Srick fflush(tfp); 32651920Seric return; 3274632Seric } 32854974Seric 32954974Seric printctladdr(a, tfp) 33054974Seric ADDRESS *a; 33154974Seric FILE *tfp; 33254974Seric { 33354974Seric char *u; 33454974Seric struct passwd *pw; 33554974Seric extern struct passwd *getpwuid(); 33654974Seric 33754974Seric if (a == NULL) 33854974Seric { 33954974Seric fprintf(tfp, "C\n"); 34054974Seric return; 34154974Seric } 34254974Seric if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL) 34354974Seric u = DefUser; 34454974Seric else 34554974Seric u = pw->pw_name; 34654974Seric fprintf(tfp, "C%s\n", u); 34754974Seric } 34854974Seric 3494632Seric /* 3504632Seric ** RUNQUEUE -- run the jobs in the queue. 3514632Seric ** 3524632Seric ** Gets the stuff out of the queue in some presumably logical 3534632Seric ** order and processes them. 3544632Seric ** 3554632Seric ** Parameters: 35624941Seric ** forkflag -- TRUE if the queue scanning should be done in 35724941Seric ** a child process. We double-fork so it is not our 35824941Seric ** child and we don't have to clean up after it. 3594632Seric ** 3604632Seric ** Returns: 3614632Seric ** none. 3624632Seric ** 3634632Seric ** Side Effects: 3644632Seric ** runs things in the mail queue. 3654632Seric */ 3664632Seric 36755360Seric ENVELOPE QueueEnvelope; /* the queue run envelope */ 36855360Seric 36955360Seric runqueue(forkflag) 3704639Seric bool forkflag; 3714632Seric { 37224953Seric extern bool shouldqueue(); 37355360Seric register ENVELOPE *e; 37455360Seric extern ENVELOPE BlankEnvelope; 37555360Seric extern ENVELOPE *newenvelope(); 37624953Seric 3777466Seric /* 37824953Seric ** If no work will ever be selected, don't even bother reading 37924953Seric ** the queue. 38024953Seric */ 38124953Seric 38251920Seric CurrentLA = getla(); /* get load average */ 38340934Srick 38458132Seric if (shouldqueue(0L, curtime())) 38524953Seric { 38624953Seric if (Verbose) 38724953Seric printf("Skipping queue run -- load average too high\n"); 38858107Seric if (forkflag && QueueIntvl != 0) 38958107Seric (void) setevent(QueueIntvl, runqueue, TRUE); 39055360Seric return; 39124953Seric } 39224953Seric 39324953Seric /* 3947466Seric ** See if we want to go off and do other useful work. 3957466Seric */ 3964639Seric 3974639Seric if (forkflag) 3984639Seric { 3997943Seric int pid; 4007943Seric 4017943Seric pid = dofork(); 4027943Seric if (pid != 0) 4034639Seric { 40446928Sbostic extern void reapchild(); 40525184Seric 4067943Seric /* parent -- pick up intermediate zombie */ 40725184Seric #ifndef SIGCHLD 4089377Seric (void) waitfor(pid); 40956795Seric #else /* SIGCHLD */ 41025184Seric (void) signal(SIGCHLD, reapchild); 41156795Seric #endif /* SIGCHLD */ 4127690Seric if (QueueIntvl != 0) 4139348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 4144639Seric return; 4154639Seric } 4167943Seric /* child -- double fork */ 41725184Seric #ifndef SIGCHLD 4187943Seric if (fork() != 0) 4197943Seric exit(EX_OK); 42056795Seric #else /* SIGCHLD */ 42125184Seric (void) signal(SIGCHLD, SIG_DFL); 42256795Seric #endif /* SIGCHLD */ 4234639Seric } 42424941Seric 42540934Srick setproctitle("running queue: %s", QueueDir); 42624941Seric 4277876Seric # ifdef LOG 42858020Seric if (LogLevel > 69) 42955360Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 43055360Seric QueueDir, getpid(), forkflag); 43156795Seric # endif /* LOG */ 4324639Seric 4337466Seric /* 43410205Seric ** Release any resources used by the daemon code. 43510205Seric */ 43610205Seric 43710205Seric # ifdef DAEMON 43810205Seric clrdaemon(); 43956795Seric # endif /* DAEMON */ 44010205Seric 44110205Seric /* 44255360Seric ** Create ourselves an envelope 44355360Seric */ 44455360Seric 44555360Seric CurEnv = &QueueEnvelope; 44658179Seric e = newenvelope(&QueueEnvelope, CurEnv); 44755360Seric e->e_flags = BlankEnvelope.e_flags; 44855360Seric 44955360Seric /* 45027175Seric ** Make sure the alias database is open. 45127175Seric */ 45227175Seric 45355012Seric initaliases(AliasFile, FALSE, e); 45427175Seric 45527175Seric /* 4567466Seric ** Start making passes through the queue. 4577466Seric ** First, read and sort the entire queue. 4587466Seric ** Then, process the work in that order. 4597466Seric ** But if you take too long, start over. 4607466Seric */ 4617466Seric 4627943Seric /* order the existing work requests */ 46324954Seric (void) orderq(FALSE); 4647690Seric 4657943Seric /* process them once at a time */ 4667943Seric while (WorkQ != NULL) 4674639Seric { 4687943Seric WORK *w = WorkQ; 46958884Seric extern bool shouldqueue(); 4707881Seric 4717943Seric WorkQ = WorkQ->w_next; 47258884Seric 47358884Seric /* 47458884Seric ** Ignore jobs that are too expensive for the moment. 47558884Seric */ 47658884Seric 47758884Seric if (shouldqueue(w->w_pri, w->w_ctime)) 47858884Seric { 47958884Seric if (Verbose) 48058884Seric printf("\nSkipping %s\n", w->w_name + 2); 48158908Seric continue; 48258884Seric } 48358884Seric 48458924Seric dowork(w->w_name + 2, ForkQueueRuns, FALSE, e); 4857943Seric free(w->w_name); 4867943Seric free((char *) w); 4874639Seric } 48829866Seric 48929866Seric /* exit without the usual cleanup */ 49055467Seric e->e_id = NULL; 49155467Seric finis(); 4924634Seric } 4934634Seric /* 4944632Seric ** ORDERQ -- order the work queue. 4954632Seric ** 4964632Seric ** Parameters: 49724941Seric ** doall -- if set, include everything in the queue (even 49824941Seric ** the jobs that cannot be run because the load 49924941Seric ** average is too high). Otherwise, exclude those 50024941Seric ** jobs. 5014632Seric ** 5024632Seric ** Returns: 50310121Seric ** The number of request in the queue (not necessarily 50410121Seric ** the number of requests in WorkQ however). 5054632Seric ** 5064632Seric ** Side Effects: 5074632Seric ** Sets WorkQ to the queue of available work, in order. 5084632Seric */ 5094632Seric 51025687Seric # define NEED_P 001 51125687Seric # define NEED_T 002 51258318Seric # define NEED_R 004 51358318Seric # define NEED_S 010 5144632Seric 51524941Seric orderq(doall) 51624941Seric bool doall; 5174632Seric { 5186625Sglickman register struct direct *d; 5194632Seric register WORK *w; 5206625Sglickman DIR *f; 5214632Seric register int i; 52225687Seric WORK wlist[QUEUESIZE+1]; 52310070Seric int wn = -1; 5244632Seric extern workcmpf(); 5254632Seric 52658318Seric if (tTd(41, 1)) 52758318Seric { 52858318Seric printf("orderq:\n"); 52958318Seric if (QueueLimitId != NULL) 53058318Seric printf("\tQueueLimitId = %s\n", QueueLimitId); 53158318Seric if (QueueLimitSender != NULL) 53258318Seric printf("\tQueueLimitSender = %s\n", QueueLimitSender); 53358318Seric if (QueueLimitRecipient != NULL) 53458318Seric printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 53558318Seric } 53658318Seric 5374632Seric /* clear out old WorkQ */ 5384632Seric for (w = WorkQ; w != NULL; ) 5394632Seric { 5404632Seric register WORK *nw = w->w_next; 5414632Seric 5424632Seric WorkQ = nw; 5434632Seric free(w->w_name); 5444632Seric free((char *) w); 5454632Seric w = nw; 5464632Seric } 5474632Seric 5484632Seric /* open the queue directory */ 5498148Seric f = opendir("."); 5504632Seric if (f == NULL) 5514632Seric { 5528148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 55310070Seric return (0); 5544632Seric } 5554632Seric 5564632Seric /* 5574632Seric ** Read the work directory. 5584632Seric */ 5594632Seric 56010070Seric while ((d = readdir(f)) != NULL) 5614632Seric { 5629377Seric FILE *cf; 5634632Seric char lbuf[MAXNAME]; 56457642Seric extern bool shouldqueue(); 56558318Seric extern bool strcontainedin(); 5664632Seric 5674632Seric /* is this an interesting entry? */ 5687812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 5694632Seric continue; 5704632Seric 57158318Seric if (QueueLimitId != NULL && 57258318Seric !strcontainedin(QueueLimitId, d->d_name)) 57358318Seric continue; 57458318Seric 57558722Seric /* 57658722Seric ** Check queue name for plausibility. This handles 57758722Seric ** both old and new type ids. 57858722Seric */ 57958722Seric 58058722Seric i = strlen(d->d_name); 58158722Seric if (i != 9 && i != 10) 58258020Seric { 58358020Seric if (Verbose) 58458020Seric printf("orderq: bogus qf name %s\n", d->d_name); 58558020Seric #ifdef LOG 58658020Seric if (LogLevel > 3) 58758020Seric syslog(LOG_NOTICE, "orderq: bogus qf name %s", 58858020Seric d->d_name); 58958020Seric #endif 59058020Seric if (strlen(d->d_name) >= MAXNAME) 59158020Seric d->d_name[MAXNAME - 1] = '\0'; 59258020Seric strcpy(lbuf, d->d_name); 59358020Seric lbuf[0] = 'Q'; 59458020Seric (void) rename(d->d_name, lbuf); 59558020Seric continue; 59658020Seric } 59758020Seric 59810070Seric /* yes -- open control file (if not too many files) */ 59925687Seric if (++wn >= QUEUESIZE) 60010070Seric continue; 60158318Seric 6028148Seric cf = fopen(d->d_name, "r"); 6034632Seric if (cf == NULL) 6044632Seric { 6057055Seric /* this may be some random person sending hir msgs */ 6067055Seric /* syserr("orderq: cannot open %s", cbuf); */ 60710090Seric if (tTd(41, 2)) 60810090Seric printf("orderq: cannot open %s (%d)\n", 60910090Seric d->d_name, errno); 6107055Seric errno = 0; 61110090Seric wn--; 6124632Seric continue; 6134632Seric } 61425687Seric w = &wlist[wn]; 61525687Seric w->w_name = newstr(d->d_name); 6164632Seric 61725027Seric /* make sure jobs in creation don't clog queue */ 61825687Seric w->w_pri = 0x7fffffff; 61925687Seric w->w_ctime = 0; 62025027Seric 6214632Seric /* extract useful information */ 62225687Seric i = NEED_P | NEED_T; 62358318Seric if (QueueLimitSender != NULL) 62458318Seric i |= NEED_S; 62558318Seric if (QueueLimitRecipient != NULL) 62658318Seric i |= NEED_R; 62725687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 6284632Seric { 62924954Seric extern long atol(); 63058318Seric extern bool strcontainedin(); 63124954Seric 63224941Seric switch (lbuf[0]) 6334632Seric { 63424941Seric case 'P': 63525687Seric w->w_pri = atol(&lbuf[1]); 63625687Seric i &= ~NEED_P; 6374632Seric break; 63825013Seric 63925013Seric case 'T': 64025687Seric w->w_ctime = atol(&lbuf[1]); 64125687Seric i &= ~NEED_T; 64225013Seric break; 64358318Seric 64458318Seric case 'R': 64558318Seric if (QueueLimitRecipient != NULL && 64658318Seric strcontainedin(QueueLimitRecipient, &lbuf[1])) 64758318Seric i &= ~NEED_R; 64858318Seric break; 64958318Seric 65058318Seric case 'S': 65158318Seric if (QueueLimitSender != NULL && 65258318Seric strcontainedin(QueueLimitSender, &lbuf[1])) 65358318Seric i &= ~NEED_S; 65458318Seric break; 6554632Seric } 6564632Seric } 6574632Seric (void) fclose(cf); 65824953Seric 65958318Seric if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 66058318Seric bitset(NEED_R|NEED_S, i)) 66124953Seric { 66224953Seric /* don't even bother sorting this job in */ 66324953Seric wn--; 66424953Seric } 6654632Seric } 6666625Sglickman (void) closedir(f); 66710090Seric wn++; 6684632Seric 6694632Seric /* 6704632Seric ** Sort the work directory. 6714632Seric */ 6724632Seric 67325687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 6744632Seric 6754632Seric /* 6764632Seric ** Convert the work list into canonical form. 6779377Seric ** Should be turning it into a list of envelopes here perhaps. 6784632Seric */ 6794632Seric 68024981Seric WorkQ = NULL; 68125687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 6824632Seric { 6834632Seric w = (WORK *) xalloc(sizeof *w); 6844632Seric w->w_name = wlist[i].w_name; 6854632Seric w->w_pri = wlist[i].w_pri; 68625013Seric w->w_ctime = wlist[i].w_ctime; 68724981Seric w->w_next = WorkQ; 68824981Seric WorkQ = w; 6894632Seric } 6904632Seric 6917677Seric if (tTd(40, 1)) 6924632Seric { 6934632Seric for (w = WorkQ; w != NULL; w = w->w_next) 6945037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 6954632Seric } 69610070Seric 69710090Seric return (wn); 6984632Seric } 6994632Seric /* 7007677Seric ** WORKCMPF -- compare function for ordering work. 7014632Seric ** 7024632Seric ** Parameters: 7034632Seric ** a -- the first argument. 7044632Seric ** b -- the second argument. 7054632Seric ** 7064632Seric ** Returns: 70724981Seric ** -1 if a < b 70824981Seric ** 0 if a == b 70924981Seric ** +1 if a > b 7104632Seric ** 7114632Seric ** Side Effects: 7124632Seric ** none. 7134632Seric */ 7144632Seric 7154632Seric workcmpf(a, b) 7165037Seric register WORK *a; 7175037Seric register WORK *b; 7184632Seric { 71957438Seric long pa = a->w_pri; 72057438Seric long pb = b->w_pri; 72124941Seric 72224941Seric if (pa == pb) 7234632Seric return (0); 72424941Seric else if (pa > pb) 72524981Seric return (1); 72624981Seric else 72710121Seric return (-1); 7284632Seric } 7294632Seric /* 7304632Seric ** DOWORK -- do a work request. 7314632Seric ** 7324632Seric ** Parameters: 73358884Seric ** id -- the ID of the job to run. 73458884Seric ** forkflag -- if set, run this in background. 73558924Seric ** requeueflag -- if set, reinstantiate the queue quickly. 73658924Seric ** This is used when expanding aliases in the queue. 73758884Seric ** e - the envelope in which to run it. 7384632Seric ** 7394632Seric ** Returns: 7404632Seric ** none. 7414632Seric ** 7424632Seric ** Side Effects: 7434632Seric ** The work request is satisfied if possible. 7444632Seric */ 7454632Seric 74658924Seric dowork(id, forkflag, requeueflag, e) 74758884Seric char *id; 74858884Seric bool forkflag; 74958924Seric bool requeueflag; 75055012Seric register ENVELOPE *e; 7514632Seric { 7524632Seric register int i; 75351920Seric extern bool readqf(); 7544632Seric 7557677Seric if (tTd(40, 1)) 75658884Seric printf("dowork(%s)\n", id); 7574632Seric 7584632Seric /* 75924941Seric ** Fork for work. 76024941Seric */ 76124941Seric 76258884Seric if (forkflag) 76324941Seric { 76424941Seric i = fork(); 76524941Seric if (i < 0) 76624941Seric { 76724941Seric syserr("dowork: cannot fork"); 76824941Seric return; 76924941Seric } 77024941Seric } 77124941Seric else 77224941Seric { 77324941Seric i = 0; 77424941Seric } 77524941Seric 7764632Seric if (i == 0) 7774632Seric { 7784632Seric /* 7794632Seric ** CHILD 7808148Seric ** Lock the control file to avoid duplicate deliveries. 7818148Seric ** Then run the file as though we had just read it. 7827350Seric ** We save an idea of the temporary name so we 7837350Seric ** can recover on interrupt. 7844632Seric */ 7854632Seric 7867763Seric /* set basic modes, etc. */ 7877356Seric (void) alarm(0); 78855012Seric clearenvelope(e, FALSE); 78958737Seric e->e_flags |= EF_QUEUERUN; 79058734Seric e->e_errormode = EM_MAIL; 79158884Seric e->e_id = id; 7927876Seric # ifdef LOG 79358020Seric if (LogLevel > 76) 79455012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 7957881Seric getpid()); 79656795Seric # endif /* LOG */ 7977763Seric 7987763Seric /* don't use the headers from sendmail.cf... */ 79955012Seric e->e_header = NULL; 8007763Seric 80151920Seric /* read the queue control file -- return if locked */ 80255012Seric if (!readqf(e)) 8036980Seric { 80458884Seric if (tTd(40, 4)) 80558884Seric printf("readqf(%s) failed\n", e->e_id); 80658884Seric if (forkflag) 80724941Seric exit(EX_OK); 80824941Seric else 80924941Seric return; 8106980Seric } 8116980Seric 81255012Seric e->e_flags |= EF_INQUEUE; 813*58929Seric eatheader(e, requeueflag); 8146980Seric 81558924Seric if (requeueflag) 81658924Seric queueup(e, TRUE, FALSE); 81758924Seric 8186980Seric /* do the delivery */ 81958915Seric sendall(e, SM_DELIVER); 8206980Seric 8216980Seric /* finish up and exit */ 82258884Seric if (forkflag) 82324941Seric finis(); 82424941Seric else 82555012Seric dropenvelope(e); 8264632Seric } 82724941Seric else 82824941Seric { 82924941Seric /* 83024941Seric ** Parent -- pick up results. 83124941Seric */ 8324632Seric 83324941Seric errno = 0; 83424941Seric (void) waitfor(i); 83524941Seric } 8364632Seric } 8374632Seric /* 8384632Seric ** READQF -- read queue file and set up environment. 8394632Seric ** 8404632Seric ** Parameters: 8419377Seric ** e -- the envelope of the job to run. 8424632Seric ** 8434632Seric ** Returns: 84451920Seric ** TRUE if it successfully read the queue file. 84551920Seric ** FALSE otherwise. 8464632Seric ** 8474632Seric ** Side Effects: 84851920Seric ** The queue file is returned locked. 8494632Seric */ 8504632Seric 85151920Seric bool 85251920Seric readqf(e) 8539377Seric register ENVELOPE *e; 8544632Seric { 85517477Seric register FILE *qfp; 85654974Seric ADDRESS *ctladdr; 85756400Seric struct stat st; 85857135Seric char *bp; 85958915Seric char qf[20]; 86057135Seric char buf[MAXLINE]; 8619348Seric extern char *fgetfolded(); 86224954Seric extern long atol(); 86354974Seric extern ADDRESS *setctluser(); 86458689Seric extern bool lockfile(); 8654632Seric 8664632Seric /* 86717468Seric ** Read and process the file. 8684632Seric */ 8694632Seric 87058915Seric strcpy(qf, queuename(e, 'q')); 87151937Seric qfp = fopen(qf, "r+"); 87217477Seric if (qfp == NULL) 87317477Seric { 87458884Seric if (tTd(40, 8)) 87558884Seric printf("readqf(%s): fopen failure (%s)\n", 87658884Seric qf, errstring(errno)); 87740934Srick if (errno != ENOENT) 87840934Srick syserr("readqf: no control file %s", qf); 87951920Seric return FALSE; 88017477Seric } 88140934Srick 88256400Seric /* 88356400Seric ** Check the queue file for plausibility to avoid attacks. 88456400Seric */ 88556400Seric 88656400Seric if (fstat(fileno(qfp), &st) < 0) 88756400Seric { 88856400Seric /* must have been being processed by someone else */ 88958884Seric if (tTd(40, 8)) 89058884Seric printf("readqf(%s): fstat failure (%s)\n", 89158884Seric qf, errstring(errno)); 89256400Seric fclose(qfp); 89356400Seric return FALSE; 89456400Seric } 89556400Seric 89657135Seric if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode) 89756400Seric { 89856400Seric # ifdef LOG 89956400Seric if (LogLevel > 0) 90056400Seric { 90156400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 90256400Seric e->e_id, st.st_uid, st.st_mode); 90356400Seric } 90456795Seric # endif /* LOG */ 90558884Seric if (tTd(40, 8)) 90658884Seric printf("readqf(%s): bogus file\n", qf); 90756400Seric fclose(qfp); 90856400Seric return FALSE; 90956400Seric } 91056400Seric 91158689Seric if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB)) 91240934Srick { 91358689Seric /* being processed by another queuer */ 91458884Seric if (tTd(40, 8)) 91558884Seric printf("readqf(%s): locked\n", qf); 91658689Seric if (Verbose) 91758689Seric printf("%s: locked\n", e->e_id); 91851920Seric # ifdef LOG 91958689Seric if (LogLevel > 19) 92058689Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 92156795Seric # endif /* LOG */ 92240934Srick (void) fclose(qfp); 92351920Seric return FALSE; 92440934Srick } 92540934Srick 92651920Seric /* save this lock */ 92751920Seric e->e_lockfp = qfp; 92851920Seric 92940934Srick /* do basic system initialization */ 93055012Seric initsys(e); 93140934Srick 93217477Seric FileName = qf; 9339377Seric LineNumber = 0; 93451920Seric if (Verbose) 9359377Seric printf("\nRunning %s\n", e->e_id); 93654974Seric ctladdr = NULL; 93757135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 9384632Seric { 93958737Seric register char *p; 94057529Seric struct stat st; 94157529Seric 94226504Seric if (tTd(40, 4)) 94357135Seric printf("+++++ %s\n", bp); 94457135Seric switch (bp[0]) 9454632Seric { 94640973Sbostic case 'C': /* specify controlling user */ 94757135Seric ctladdr = setctluser(&bp[1]); 94840973Sbostic break; 94940973Sbostic 9504632Seric case 'R': /* specify recipient */ 95158082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 9524632Seric break; 9534632Seric 95425687Seric case 'E': /* specify error recipient */ 95558082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 95625687Seric break; 95725687Seric 9584632Seric case 'H': /* header */ 95957135Seric (void) chompheader(&bp[1], FALSE, e); 9604632Seric break; 9614632Seric 96210108Seric case 'M': /* message */ 96357135Seric e->e_message = newstr(&bp[1]); 96410108Seric break; 96510108Seric 9664632Seric case 'S': /* sender */ 96758704Seric setsender(newstr(&bp[1]), e, NULL, TRUE); 9684632Seric break; 9694632Seric 9704632Seric case 'D': /* data file name */ 97157135Seric e->e_df = newstr(&bp[1]); 9729544Seric e->e_dfp = fopen(e->e_df, "r"); 9739544Seric if (e->e_dfp == NULL) 97458020Seric { 9759377Seric syserr("readqf: cannot open %s", e->e_df); 97658020Seric e->e_msgsize = -1; 97758020Seric } 97858020Seric else if (fstat(fileno(e->e_dfp), &st) >= 0) 97957529Seric e->e_msgsize = st.st_size; 9804632Seric break; 9814632Seric 9827860Seric case 'T': /* init time */ 98357135Seric e->e_ctime = atol(&bp[1]); 9844632Seric break; 9854632Seric 9864634Seric case 'P': /* message priority */ 98757135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 9884634Seric break; 9894634Seric 99058737Seric case 'F': /* flag bits */ 99158737Seric for (p = &bp[1]; *p != '\0'; p++) 99258737Seric { 99358737Seric switch (*p) 99458737Seric { 99558737Seric case 'w': /* warning sent */ 99658737Seric e->e_flags |= EF_WARNING; 99758737Seric break; 99858737Seric 99958737Seric case 'r': /* response */ 100058737Seric e->e_flags |= EF_RESPONSE; 100158737Seric break; 100258737Seric } 100358737Seric } 100458737Seric break; 100558737Seric 100653400Seric case '$': /* define macro */ 100757135Seric define(bp[1], newstr(&bp[2]), e); 100853400Seric break; 100953400Seric 101024941Seric case '\0': /* blank line; ignore */ 101124941Seric break; 101224941Seric 10134632Seric default: 101424941Seric syserr("readqf(%s:%d): bad line \"%s\"", e->e_id, 101557135Seric LineNumber, bp); 10164632Seric break; 10174632Seric } 101857135Seric 101957135Seric if (bp != buf) 102057135Seric free(bp); 10214632Seric } 10229377Seric 10239377Seric FileName = NULL; 102424941Seric 102524941Seric /* 102624941Seric ** If we haven't read any lines, this queue file is empty. 102724941Seric ** Arrange to remove it without referencing any null pointers. 102824941Seric */ 102924941Seric 103024941Seric if (LineNumber == 0) 103124941Seric { 103224941Seric errno = 0; 103324941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 103424941Seric } 103551920Seric return TRUE; 10364632Seric } 10374632Seric /* 10389630Seric ** PRINTQUEUE -- print out a representation of the mail queue 10399630Seric ** 10409630Seric ** Parameters: 10419630Seric ** none. 10429630Seric ** 10439630Seric ** Returns: 10449630Seric ** none. 10459630Seric ** 10469630Seric ** Side Effects: 10479630Seric ** Prints a listing of the mail queue on the standard output. 10489630Seric */ 10495182Seric 10509630Seric printqueue() 10519630Seric { 10529630Seric register WORK *w; 10539630Seric FILE *f; 105410070Seric int nrequests; 10559630Seric char buf[MAXLINE]; 10569630Seric 10579630Seric /* 105858250Seric ** Check for permission to print the queue 105958250Seric */ 106058250Seric 106158523Seric if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && getuid() != 0) 106258250Seric { 106358250Seric struct stat st; 106458523Seric # ifdef NGROUPS 106558523Seric int n; 106658523Seric int gidset[NGROUPS]; 106758523Seric # endif 106858250Seric 106958517Seric if (stat(QueueDir, &st) < 0) 107058250Seric { 107158250Seric syserr("Cannot stat %s", QueueDir); 107258250Seric return; 107358250Seric } 107458523Seric # ifdef NGROUPS 107558523Seric n = getgroups(NGROUPS, gidset); 107658523Seric while (--n >= 0) 107758523Seric { 107858523Seric if (gidset[n] == st.st_gid) 107958523Seric break; 108058523Seric } 108158523Seric if (n < 0) 108258523Seric # else 108358250Seric if (getgid() != st.st_gid) 108458523Seric # endif 108558250Seric { 108658250Seric usrerr("510 You are not permitted to see the queue"); 108758250Seric setstat(EX_NOPERM); 108858250Seric return; 108958250Seric } 109058250Seric } 109158250Seric 109258250Seric /* 10939630Seric ** Read and order the queue. 10949630Seric */ 10959630Seric 109624941Seric nrequests = orderq(TRUE); 10979630Seric 10989630Seric /* 10999630Seric ** Print the work list that we have read. 11009630Seric */ 11019630Seric 11029630Seric /* first see if there is anything */ 110310070Seric if (nrequests <= 0) 11049630Seric { 110510070Seric printf("Mail queue is empty\n"); 11069630Seric return; 11079630Seric } 11089630Seric 110951920Seric CurrentLA = getla(); /* get load average */ 111040934Srick 111110096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 111225687Seric if (nrequests > QUEUESIZE) 111325687Seric printf(", only %d printed", QUEUESIZE); 111424979Seric if (Verbose) 111558716Seric printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 111624979Seric else 111758716Seric printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 11189630Seric for (w = WorkQ; w != NULL; w = w->w_next) 11199630Seric { 11209630Seric struct stat st; 112110070Seric auto time_t submittime = 0; 112210070Seric long dfsize = -1; 112358737Seric int flags = 0; 112410108Seric char message[MAXLINE]; 112524941Seric extern bool shouldqueue(); 112658689Seric extern bool lockfile(); 11279630Seric 112817468Seric f = fopen(w->w_name, "r"); 112917468Seric if (f == NULL) 113017468Seric { 113117468Seric errno = 0; 113217468Seric continue; 113317468Seric } 113458724Seric printf("%8s", w->w_name + 2); 113558689Seric if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB)) 113610070Seric printf("*"); 113757438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 113824941Seric printf("X"); 113910070Seric else 114010070Seric printf(" "); 114110070Seric errno = 0; 114217468Seric 114310108Seric message[0] = '\0'; 11449630Seric while (fgets(buf, sizeof buf, f) != NULL) 11459630Seric { 114653400Seric register int i; 114758737Seric register char *p; 114853400Seric 11499630Seric fixcrlf(buf, TRUE); 11509630Seric switch (buf[0]) 11519630Seric { 115210108Seric case 'M': /* error message */ 115353400Seric if ((i = strlen(&buf[1])) >= sizeof message) 115458737Seric i = sizeof message - 1; 115553400Seric bcopy(&buf[1], message, i); 115653400Seric message[i] = '\0'; 115710108Seric break; 115810108Seric 11599630Seric case 'S': /* sender name */ 116024979Seric if (Verbose) 116158737Seric printf("%8ld %10ld%c%.12s %.38s", 116258737Seric dfsize, 116358737Seric w->w_pri, 116458737Seric bitset(EF_WARNING, flags) ? '+' : ' ', 116558737Seric ctime(&submittime) + 4, 116624979Seric &buf[1]); 116724979Seric else 116824979Seric printf("%8ld %.16s %.45s", dfsize, 116924979Seric ctime(&submittime), &buf[1]); 117010108Seric if (message[0] != '\0') 117125027Seric printf("\n\t\t (%.60s)", message); 11729630Seric break; 117351920Seric 117440973Sbostic case 'C': /* controlling user */ 117554974Seric if (Verbose) 117658716Seric printf("\n\t\t\t\t (---%.34s---)", 117758716Seric &buf[1]); 117840973Sbostic break; 11799630Seric 11809630Seric case 'R': /* recipient name */ 118124979Seric if (Verbose) 118258716Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 118324979Seric else 118458716Seric printf("\n\t\t\t\t %.45s", &buf[1]); 11859630Seric break; 11869630Seric 11879630Seric case 'T': /* creation time */ 118824941Seric submittime = atol(&buf[1]); 11899630Seric break; 119010070Seric 119110070Seric case 'D': /* data file name */ 119210070Seric if (stat(&buf[1], &st) >= 0) 119310070Seric dfsize = st.st_size; 119410070Seric break; 119558737Seric 119658737Seric case 'F': /* flag bits */ 119758737Seric for (p = &buf[1]; *p != '\0'; p++) 119858737Seric { 119958737Seric switch (*p) 120058737Seric { 120158737Seric case 'w': 120258737Seric flags |= EF_WARNING; 120358737Seric break; 120458737Seric } 120558737Seric } 12069630Seric } 12079630Seric } 120810070Seric if (submittime == (time_t) 0) 120910070Seric printf(" (no control file)"); 12109630Seric printf("\n"); 121123098Seric (void) fclose(f); 12129630Seric } 12139630Seric } 12149630Seric 121556795Seric # endif /* QUEUE */ 121617468Seric /* 121717468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 121817468Seric ** 121917468Seric ** Assigns an id code if one does not already exist. 122017468Seric ** This code is very careful to avoid trashing existing files 122117468Seric ** under any circumstances. 122217468Seric ** 122317468Seric ** Parameters: 122417468Seric ** e -- envelope to build it in/from. 122517468Seric ** type -- the file type, used as the first character 122617468Seric ** of the file name. 122717468Seric ** 122817468Seric ** Returns: 122917468Seric ** a pointer to the new file name (in a static buffer). 123017468Seric ** 123117468Seric ** Side Effects: 123251920Seric ** If no id code is already assigned, queuename will 123351920Seric ** assign an id code, create a qf file, and leave a 123451920Seric ** locked, open-for-write file pointer in the envelope. 123517468Seric */ 123617468Seric 123717468Seric char * 123817468Seric queuename(e, type) 123917468Seric register ENVELOPE *e; 124017468Seric char type; 124117468Seric { 124217468Seric static int pid = -1; 124358741Seric static char c0; 124458741Seric static char c1; 124558741Seric static char c2; 124658716Seric time_t now; 124758716Seric struct tm *tm; 124858689Seric static char buf[MAXNAME]; 124958689Seric extern bool lockfile(); 125017468Seric 125117468Seric if (e->e_id == NULL) 125217468Seric { 125317468Seric char qf[20]; 125417468Seric 125517468Seric /* find a unique id */ 125617468Seric if (pid != getpid()) 125717468Seric { 125817468Seric /* new process -- start back at "AA" */ 125917468Seric pid = getpid(); 126058716Seric now = curtime(); 126158716Seric tm = localtime(&now); 126258716Seric c0 = 'A' + tm->tm_hour; 126317468Seric c1 = 'A'; 126417468Seric c2 = 'A' - 1; 126517468Seric } 126658716Seric (void) sprintf(qf, "qf%cAA%05d", c0, pid); 126717468Seric 126817468Seric while (c1 < '~' || c2 < 'Z') 126917468Seric { 127017468Seric int i; 127117468Seric 127217468Seric if (c2 >= 'Z') 127317468Seric { 127417468Seric c1++; 127517468Seric c2 = 'A' - 1; 127617468Seric } 127758716Seric qf[3] = c1; 127858716Seric qf[4] = ++c2; 127917468Seric if (tTd(7, 20)) 128040934Srick printf("queuename: trying \"%s\"\n", qf); 128117468Seric 128240934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 128351920Seric if (i < 0) 128451920Seric { 128551920Seric if (errno == EEXIST) 128651920Seric continue; 128751920Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 128851920Seric qf, QueueDir); 128951920Seric exit(EX_UNAVAILABLE); 129051920Seric } 129158689Seric if (lockfile(i, qf, LOCK_EX|LOCK_NB)) 129251920Seric { 129351920Seric e->e_lockfp = fdopen(i, "w"); 129440934Srick break; 129517468Seric } 129651920Seric 129751920Seric /* a reader got the file; abandon it and try again */ 129851920Seric (void) close(i); 129917468Seric } 130017468Seric if (c1 >= '~' && c2 >= 'Z') 130117468Seric { 130217468Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 130317468Seric qf, QueueDir); 130417468Seric exit(EX_OSERR); 130517468Seric } 130617468Seric e->e_id = newstr(&qf[2]); 130717468Seric define('i', e->e_id, e); 130817468Seric if (tTd(7, 1)) 130917468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 131017468Seric # ifdef LOG 131158020Seric if (LogLevel > 93) 131217468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 131356795Seric # endif /* LOG */ 131417468Seric } 131517468Seric 131617468Seric if (type == '\0') 131717468Seric return (NULL); 131817468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 131917468Seric if (tTd(7, 2)) 132017468Seric printf("queuename: %s\n", buf); 132117468Seric return (buf); 132217468Seric } 132317468Seric /* 132417468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 132517468Seric ** 132617468Seric ** Parameters: 132717468Seric ** e -- the envelope to unlock. 132817468Seric ** 132917468Seric ** Returns: 133017468Seric ** none 133117468Seric ** 133217468Seric ** Side Effects: 133317468Seric ** unlocks the queue for `e'. 133417468Seric */ 133517468Seric 133617468Seric unlockqueue(e) 133717468Seric ENVELOPE *e; 133817468Seric { 133958680Seric if (tTd(51, 4)) 134058680Seric printf("unlockqueue(%s)\n", e->e_id); 134158680Seric 134251920Seric /* if there is a lock file in the envelope, close it */ 134351920Seric if (e->e_lockfp != NULL) 134458680Seric xfclose(e->e_lockfp, "unlockqueue", e->e_id); 134551920Seric e->e_lockfp = NULL; 134651920Seric 134758728Seric /* don't create a queue id if we don't already have one */ 134858728Seric if (e->e_id == NULL) 134958728Seric return; 135058728Seric 135117468Seric /* remove the transcript */ 135217468Seric # ifdef LOG 135358020Seric if (LogLevel > 87) 135417468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 135556795Seric # endif /* LOG */ 135658680Seric if (!tTd(51, 104)) 135717468Seric xunlink(queuename(e, 'x')); 135817468Seric 135917468Seric } 136040973Sbostic /* 136154974Seric ** SETCTLUSER -- create a controlling address 136240973Sbostic ** 136354974Seric ** Create a fake "address" given only a local login name; this is 136454974Seric ** used as a "controlling user" for future recipient addresses. 136540973Sbostic ** 136640973Sbostic ** Parameters: 136754974Seric ** user -- the user name of the controlling user. 136840973Sbostic ** 136940973Sbostic ** Returns: 137054974Seric ** An address descriptor for the controlling user. 137140973Sbostic ** 137240973Sbostic ** Side Effects: 137340973Sbostic ** none. 137440973Sbostic */ 137540973Sbostic 137654974Seric ADDRESS * 137754974Seric setctluser(user) 137854974Seric char *user; 137940973Sbostic { 138054974Seric register ADDRESS *a; 138140973Sbostic struct passwd *pw; 138240973Sbostic 138340973Sbostic /* 138454974Seric ** See if this clears our concept of controlling user. 138540973Sbostic */ 138640973Sbostic 138754974Seric if (user == NULL || *user == '\0') 138858704Seric user = DefUser; 138940973Sbostic 139040973Sbostic /* 139154974Seric ** Set up addr fields for controlling user. 139240973Sbostic */ 139340973Sbostic 139454974Seric a = (ADDRESS *) xalloc(sizeof *a); 139554974Seric bzero((char *) a, sizeof *a); 139654974Seric if ((pw = getpwnam(user)) != NULL) 139740973Sbostic { 139840973Sbostic a->q_home = newstr(pw->pw_dir); 139940973Sbostic a->q_uid = pw->pw_uid; 140040973Sbostic a->q_gid = pw->pw_gid; 140157642Seric a->q_user = newstr(user); 140240973Sbostic } 140340973Sbostic else 140440973Sbostic { 140540973Sbostic a->q_uid = DefUid; 140640973Sbostic a->q_gid = DefGid; 140757642Seric a->q_user = newstr(DefUser); 140840973Sbostic } 140940973Sbostic 141058294Seric a->q_flags |= QGOODUID|QPRIMARY; /* flag as a "ctladdr" */ 141156328Seric a->q_mailer = LocalMailer; 141254974Seric return a; 141340973Sbostic } 1414