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*58930Seric static char sccsid[] = "@(#)queue.c 6.45 (Berkeley) 04/01/93 (with queueing)"; 1433731Sbostic #else 15*58930Seric static char sccsid[] = "@(#)queue.c 6.45 (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); 48158884Seric } 482*58930Seric else 483*58930Seric { 484*58930Seric dowork(w->w_name + 2, ForkQueueRuns, FALSE, e); 485*58930Seric } 4867943Seric free(w->w_name); 4877943Seric free((char *) w); 4884639Seric } 48929866Seric 49029866Seric /* exit without the usual cleanup */ 49155467Seric e->e_id = NULL; 49255467Seric finis(); 4934634Seric } 4944634Seric /* 4954632Seric ** ORDERQ -- order the work queue. 4964632Seric ** 4974632Seric ** Parameters: 49824941Seric ** doall -- if set, include everything in the queue (even 49924941Seric ** the jobs that cannot be run because the load 50024941Seric ** average is too high). Otherwise, exclude those 50124941Seric ** jobs. 5024632Seric ** 5034632Seric ** Returns: 50410121Seric ** The number of request in the queue (not necessarily 50510121Seric ** the number of requests in WorkQ however). 5064632Seric ** 5074632Seric ** Side Effects: 5084632Seric ** Sets WorkQ to the queue of available work, in order. 5094632Seric */ 5104632Seric 51125687Seric # define NEED_P 001 51225687Seric # define NEED_T 002 51358318Seric # define NEED_R 004 51458318Seric # define NEED_S 010 5154632Seric 51624941Seric orderq(doall) 51724941Seric bool doall; 5184632Seric { 5196625Sglickman register struct direct *d; 5204632Seric register WORK *w; 5216625Sglickman DIR *f; 5224632Seric register int i; 52325687Seric WORK wlist[QUEUESIZE+1]; 52410070Seric int wn = -1; 5254632Seric extern workcmpf(); 5264632Seric 52758318Seric if (tTd(41, 1)) 52858318Seric { 52958318Seric printf("orderq:\n"); 53058318Seric if (QueueLimitId != NULL) 53158318Seric printf("\tQueueLimitId = %s\n", QueueLimitId); 53258318Seric if (QueueLimitSender != NULL) 53358318Seric printf("\tQueueLimitSender = %s\n", QueueLimitSender); 53458318Seric if (QueueLimitRecipient != NULL) 53558318Seric printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 53658318Seric } 53758318Seric 5384632Seric /* clear out old WorkQ */ 5394632Seric for (w = WorkQ; w != NULL; ) 5404632Seric { 5414632Seric register WORK *nw = w->w_next; 5424632Seric 5434632Seric WorkQ = nw; 5444632Seric free(w->w_name); 5454632Seric free((char *) w); 5464632Seric w = nw; 5474632Seric } 5484632Seric 5494632Seric /* open the queue directory */ 5508148Seric f = opendir("."); 5514632Seric if (f == NULL) 5524632Seric { 5538148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 55410070Seric return (0); 5554632Seric } 5564632Seric 5574632Seric /* 5584632Seric ** Read the work directory. 5594632Seric */ 5604632Seric 56110070Seric while ((d = readdir(f)) != NULL) 5624632Seric { 5639377Seric FILE *cf; 5644632Seric char lbuf[MAXNAME]; 56557642Seric extern bool shouldqueue(); 56658318Seric extern bool strcontainedin(); 5674632Seric 5684632Seric /* is this an interesting entry? */ 5697812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 5704632Seric continue; 5714632Seric 57258318Seric if (QueueLimitId != NULL && 57358318Seric !strcontainedin(QueueLimitId, d->d_name)) 57458318Seric continue; 57558318Seric 57658722Seric /* 57758722Seric ** Check queue name for plausibility. This handles 57858722Seric ** both old and new type ids. 57958722Seric */ 58058722Seric 58158722Seric i = strlen(d->d_name); 58258722Seric if (i != 9 && i != 10) 58358020Seric { 58458020Seric if (Verbose) 58558020Seric printf("orderq: bogus qf name %s\n", d->d_name); 58658020Seric #ifdef LOG 58758020Seric if (LogLevel > 3) 58858020Seric syslog(LOG_NOTICE, "orderq: bogus qf name %s", 58958020Seric d->d_name); 59058020Seric #endif 59158020Seric if (strlen(d->d_name) >= MAXNAME) 59258020Seric d->d_name[MAXNAME - 1] = '\0'; 59358020Seric strcpy(lbuf, d->d_name); 59458020Seric lbuf[0] = 'Q'; 59558020Seric (void) rename(d->d_name, lbuf); 59658020Seric continue; 59758020Seric } 59858020Seric 59910070Seric /* yes -- open control file (if not too many files) */ 60025687Seric if (++wn >= QUEUESIZE) 60110070Seric continue; 60258318Seric 6038148Seric cf = fopen(d->d_name, "r"); 6044632Seric if (cf == NULL) 6054632Seric { 6067055Seric /* this may be some random person sending hir msgs */ 6077055Seric /* syserr("orderq: cannot open %s", cbuf); */ 60810090Seric if (tTd(41, 2)) 60910090Seric printf("orderq: cannot open %s (%d)\n", 61010090Seric d->d_name, errno); 6117055Seric errno = 0; 61210090Seric wn--; 6134632Seric continue; 6144632Seric } 61525687Seric w = &wlist[wn]; 61625687Seric w->w_name = newstr(d->d_name); 6174632Seric 61825027Seric /* make sure jobs in creation don't clog queue */ 61925687Seric w->w_pri = 0x7fffffff; 62025687Seric w->w_ctime = 0; 62125027Seric 6224632Seric /* extract useful information */ 62325687Seric i = NEED_P | NEED_T; 62458318Seric if (QueueLimitSender != NULL) 62558318Seric i |= NEED_S; 62658318Seric if (QueueLimitRecipient != NULL) 62758318Seric i |= NEED_R; 62825687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 6294632Seric { 63024954Seric extern long atol(); 63158318Seric extern bool strcontainedin(); 63224954Seric 63324941Seric switch (lbuf[0]) 6344632Seric { 63524941Seric case 'P': 63625687Seric w->w_pri = atol(&lbuf[1]); 63725687Seric i &= ~NEED_P; 6384632Seric break; 63925013Seric 64025013Seric case 'T': 64125687Seric w->w_ctime = atol(&lbuf[1]); 64225687Seric i &= ~NEED_T; 64325013Seric break; 64458318Seric 64558318Seric case 'R': 64658318Seric if (QueueLimitRecipient != NULL && 64758318Seric strcontainedin(QueueLimitRecipient, &lbuf[1])) 64858318Seric i &= ~NEED_R; 64958318Seric break; 65058318Seric 65158318Seric case 'S': 65258318Seric if (QueueLimitSender != NULL && 65358318Seric strcontainedin(QueueLimitSender, &lbuf[1])) 65458318Seric i &= ~NEED_S; 65558318Seric break; 6564632Seric } 6574632Seric } 6584632Seric (void) fclose(cf); 65924953Seric 66058318Seric if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 66158318Seric bitset(NEED_R|NEED_S, i)) 66224953Seric { 66324953Seric /* don't even bother sorting this job in */ 66424953Seric wn--; 66524953Seric } 6664632Seric } 6676625Sglickman (void) closedir(f); 66810090Seric wn++; 6694632Seric 6704632Seric /* 6714632Seric ** Sort the work directory. 6724632Seric */ 6734632Seric 67425687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 6754632Seric 6764632Seric /* 6774632Seric ** Convert the work list into canonical form. 6789377Seric ** Should be turning it into a list of envelopes here perhaps. 6794632Seric */ 6804632Seric 68124981Seric WorkQ = NULL; 68225687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 6834632Seric { 6844632Seric w = (WORK *) xalloc(sizeof *w); 6854632Seric w->w_name = wlist[i].w_name; 6864632Seric w->w_pri = wlist[i].w_pri; 68725013Seric w->w_ctime = wlist[i].w_ctime; 68824981Seric w->w_next = WorkQ; 68924981Seric WorkQ = w; 6904632Seric } 6914632Seric 6927677Seric if (tTd(40, 1)) 6934632Seric { 6944632Seric for (w = WorkQ; w != NULL; w = w->w_next) 6955037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 6964632Seric } 69710070Seric 69810090Seric return (wn); 6994632Seric } 7004632Seric /* 7017677Seric ** WORKCMPF -- compare function for ordering work. 7024632Seric ** 7034632Seric ** Parameters: 7044632Seric ** a -- the first argument. 7054632Seric ** b -- the second argument. 7064632Seric ** 7074632Seric ** Returns: 70824981Seric ** -1 if a < b 70924981Seric ** 0 if a == b 71024981Seric ** +1 if a > b 7114632Seric ** 7124632Seric ** Side Effects: 7134632Seric ** none. 7144632Seric */ 7154632Seric 7164632Seric workcmpf(a, b) 7175037Seric register WORK *a; 7185037Seric register WORK *b; 7194632Seric { 72057438Seric long pa = a->w_pri; 72157438Seric long pb = b->w_pri; 72224941Seric 72324941Seric if (pa == pb) 7244632Seric return (0); 72524941Seric else if (pa > pb) 72624981Seric return (1); 72724981Seric else 72810121Seric return (-1); 7294632Seric } 7304632Seric /* 7314632Seric ** DOWORK -- do a work request. 7324632Seric ** 7334632Seric ** Parameters: 73458884Seric ** id -- the ID of the job to run. 73558884Seric ** forkflag -- if set, run this in background. 73658924Seric ** requeueflag -- if set, reinstantiate the queue quickly. 73758924Seric ** This is used when expanding aliases in the queue. 73858884Seric ** e - the envelope in which to run it. 7394632Seric ** 7404632Seric ** Returns: 7414632Seric ** none. 7424632Seric ** 7434632Seric ** Side Effects: 7444632Seric ** The work request is satisfied if possible. 7454632Seric */ 7464632Seric 74758924Seric dowork(id, forkflag, requeueflag, e) 74858884Seric char *id; 74958884Seric bool forkflag; 75058924Seric bool requeueflag; 75155012Seric register ENVELOPE *e; 7524632Seric { 7534632Seric register int i; 75451920Seric extern bool readqf(); 7554632Seric 7567677Seric if (tTd(40, 1)) 75758884Seric printf("dowork(%s)\n", id); 7584632Seric 7594632Seric /* 76024941Seric ** Fork for work. 76124941Seric */ 76224941Seric 76358884Seric if (forkflag) 76424941Seric { 76524941Seric i = fork(); 76624941Seric if (i < 0) 76724941Seric { 76824941Seric syserr("dowork: cannot fork"); 76924941Seric return; 77024941Seric } 77124941Seric } 77224941Seric else 77324941Seric { 77424941Seric i = 0; 77524941Seric } 77624941Seric 7774632Seric if (i == 0) 7784632Seric { 7794632Seric /* 7804632Seric ** CHILD 7818148Seric ** Lock the control file to avoid duplicate deliveries. 7828148Seric ** Then run the file as though we had just read it. 7837350Seric ** We save an idea of the temporary name so we 7847350Seric ** can recover on interrupt. 7854632Seric */ 7864632Seric 7877763Seric /* set basic modes, etc. */ 7887356Seric (void) alarm(0); 78955012Seric clearenvelope(e, FALSE); 79058737Seric e->e_flags |= EF_QUEUERUN; 79158734Seric e->e_errormode = EM_MAIL; 79258884Seric e->e_id = id; 7937876Seric # ifdef LOG 79458020Seric if (LogLevel > 76) 79555012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 7967881Seric getpid()); 79756795Seric # endif /* LOG */ 7987763Seric 7997763Seric /* don't use the headers from sendmail.cf... */ 80055012Seric e->e_header = NULL; 8017763Seric 80251920Seric /* read the queue control file -- return if locked */ 80355012Seric if (!readqf(e)) 8046980Seric { 80558884Seric if (tTd(40, 4)) 80658884Seric printf("readqf(%s) failed\n", e->e_id); 80758884Seric if (forkflag) 80824941Seric exit(EX_OK); 80924941Seric else 81024941Seric return; 8116980Seric } 8126980Seric 81355012Seric e->e_flags |= EF_INQUEUE; 81458929Seric eatheader(e, requeueflag); 8156980Seric 81658924Seric if (requeueflag) 81758924Seric queueup(e, TRUE, FALSE); 81858924Seric 8196980Seric /* do the delivery */ 82058915Seric sendall(e, SM_DELIVER); 8216980Seric 8226980Seric /* finish up and exit */ 82358884Seric if (forkflag) 82424941Seric finis(); 82524941Seric else 82655012Seric dropenvelope(e); 8274632Seric } 82824941Seric else 82924941Seric { 83024941Seric /* 83124941Seric ** Parent -- pick up results. 83224941Seric */ 8334632Seric 83424941Seric errno = 0; 83524941Seric (void) waitfor(i); 83624941Seric } 8374632Seric } 8384632Seric /* 8394632Seric ** READQF -- read queue file and set up environment. 8404632Seric ** 8414632Seric ** Parameters: 8429377Seric ** e -- the envelope of the job to run. 8434632Seric ** 8444632Seric ** Returns: 84551920Seric ** TRUE if it successfully read the queue file. 84651920Seric ** FALSE otherwise. 8474632Seric ** 8484632Seric ** Side Effects: 84951920Seric ** The queue file is returned locked. 8504632Seric */ 8514632Seric 85251920Seric bool 85351920Seric readqf(e) 8549377Seric register ENVELOPE *e; 8554632Seric { 85617477Seric register FILE *qfp; 85754974Seric ADDRESS *ctladdr; 85856400Seric struct stat st; 85957135Seric char *bp; 86058915Seric char qf[20]; 86157135Seric char buf[MAXLINE]; 8629348Seric extern char *fgetfolded(); 86324954Seric extern long atol(); 86454974Seric extern ADDRESS *setctluser(); 86558689Seric extern bool lockfile(); 8664632Seric 8674632Seric /* 86817468Seric ** Read and process the file. 8694632Seric */ 8704632Seric 87158915Seric strcpy(qf, queuename(e, 'q')); 87251937Seric qfp = fopen(qf, "r+"); 87317477Seric if (qfp == NULL) 87417477Seric { 87558884Seric if (tTd(40, 8)) 87658884Seric printf("readqf(%s): fopen failure (%s)\n", 87758884Seric qf, errstring(errno)); 87840934Srick if (errno != ENOENT) 87940934Srick syserr("readqf: no control file %s", qf); 88051920Seric return FALSE; 88117477Seric } 88240934Srick 88356400Seric /* 88456400Seric ** Check the queue file for plausibility to avoid attacks. 88556400Seric */ 88656400Seric 88756400Seric if (fstat(fileno(qfp), &st) < 0) 88856400Seric { 88956400Seric /* must have been being processed by someone else */ 89058884Seric if (tTd(40, 8)) 89158884Seric printf("readqf(%s): fstat failure (%s)\n", 89258884Seric qf, errstring(errno)); 89356400Seric fclose(qfp); 89456400Seric return FALSE; 89556400Seric } 89656400Seric 89757135Seric if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode) 89856400Seric { 89956400Seric # ifdef LOG 90056400Seric if (LogLevel > 0) 90156400Seric { 90256400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 90356400Seric e->e_id, st.st_uid, st.st_mode); 90456400Seric } 90556795Seric # endif /* LOG */ 90658884Seric if (tTd(40, 8)) 90758884Seric printf("readqf(%s): bogus file\n", qf); 90856400Seric fclose(qfp); 90956400Seric return FALSE; 91056400Seric } 91156400Seric 91258689Seric if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB)) 91340934Srick { 91458689Seric /* being processed by another queuer */ 91558884Seric if (tTd(40, 8)) 91658884Seric printf("readqf(%s): locked\n", qf); 91758689Seric if (Verbose) 91858689Seric printf("%s: locked\n", e->e_id); 91951920Seric # ifdef LOG 92058689Seric if (LogLevel > 19) 92158689Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 92256795Seric # endif /* LOG */ 92340934Srick (void) fclose(qfp); 92451920Seric return FALSE; 92540934Srick } 92640934Srick 92751920Seric /* save this lock */ 92851920Seric e->e_lockfp = qfp; 92951920Seric 93040934Srick /* do basic system initialization */ 93155012Seric initsys(e); 93240934Srick 93317477Seric FileName = qf; 9349377Seric LineNumber = 0; 93551920Seric if (Verbose) 9369377Seric printf("\nRunning %s\n", e->e_id); 93754974Seric ctladdr = NULL; 93857135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 9394632Seric { 94058737Seric register char *p; 94157529Seric struct stat st; 94257529Seric 94326504Seric if (tTd(40, 4)) 94457135Seric printf("+++++ %s\n", bp); 94557135Seric switch (bp[0]) 9464632Seric { 94740973Sbostic case 'C': /* specify controlling user */ 94857135Seric ctladdr = setctluser(&bp[1]); 94940973Sbostic break; 95040973Sbostic 9514632Seric case 'R': /* specify recipient */ 95258082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 9534632Seric break; 9544632Seric 95525687Seric case 'E': /* specify error recipient */ 95658082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 95725687Seric break; 95825687Seric 9594632Seric case 'H': /* header */ 96057135Seric (void) chompheader(&bp[1], FALSE, e); 9614632Seric break; 9624632Seric 96310108Seric case 'M': /* message */ 96457135Seric e->e_message = newstr(&bp[1]); 96510108Seric break; 96610108Seric 9674632Seric case 'S': /* sender */ 96858704Seric setsender(newstr(&bp[1]), e, NULL, TRUE); 9694632Seric break; 9704632Seric 9714632Seric case 'D': /* data file name */ 97257135Seric e->e_df = newstr(&bp[1]); 9739544Seric e->e_dfp = fopen(e->e_df, "r"); 9749544Seric if (e->e_dfp == NULL) 97558020Seric { 9769377Seric syserr("readqf: cannot open %s", e->e_df); 97758020Seric e->e_msgsize = -1; 97858020Seric } 97958020Seric else if (fstat(fileno(e->e_dfp), &st) >= 0) 98057529Seric e->e_msgsize = st.st_size; 9814632Seric break; 9824632Seric 9837860Seric case 'T': /* init time */ 98457135Seric e->e_ctime = atol(&bp[1]); 9854632Seric break; 9864632Seric 9874634Seric case 'P': /* message priority */ 98857135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 9894634Seric break; 9904634Seric 99158737Seric case 'F': /* flag bits */ 99258737Seric for (p = &bp[1]; *p != '\0'; p++) 99358737Seric { 99458737Seric switch (*p) 99558737Seric { 99658737Seric case 'w': /* warning sent */ 99758737Seric e->e_flags |= EF_WARNING; 99858737Seric break; 99958737Seric 100058737Seric case 'r': /* response */ 100158737Seric e->e_flags |= EF_RESPONSE; 100258737Seric break; 100358737Seric } 100458737Seric } 100558737Seric break; 100658737Seric 100753400Seric case '$': /* define macro */ 100857135Seric define(bp[1], newstr(&bp[2]), e); 100953400Seric break; 101053400Seric 101124941Seric case '\0': /* blank line; ignore */ 101224941Seric break; 101324941Seric 10144632Seric default: 101524941Seric syserr("readqf(%s:%d): bad line \"%s\"", e->e_id, 101657135Seric LineNumber, bp); 10174632Seric break; 10184632Seric } 101957135Seric 102057135Seric if (bp != buf) 102157135Seric free(bp); 10224632Seric } 10239377Seric 10249377Seric FileName = NULL; 102524941Seric 102624941Seric /* 102724941Seric ** If we haven't read any lines, this queue file is empty. 102824941Seric ** Arrange to remove it without referencing any null pointers. 102924941Seric */ 103024941Seric 103124941Seric if (LineNumber == 0) 103224941Seric { 103324941Seric errno = 0; 103424941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 103524941Seric } 103651920Seric return TRUE; 10374632Seric } 10384632Seric /* 10399630Seric ** PRINTQUEUE -- print out a representation of the mail queue 10409630Seric ** 10419630Seric ** Parameters: 10429630Seric ** none. 10439630Seric ** 10449630Seric ** Returns: 10459630Seric ** none. 10469630Seric ** 10479630Seric ** Side Effects: 10489630Seric ** Prints a listing of the mail queue on the standard output. 10499630Seric */ 10505182Seric 10519630Seric printqueue() 10529630Seric { 10539630Seric register WORK *w; 10549630Seric FILE *f; 105510070Seric int nrequests; 10569630Seric char buf[MAXLINE]; 10579630Seric 10589630Seric /* 105958250Seric ** Check for permission to print the queue 106058250Seric */ 106158250Seric 106258523Seric if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && getuid() != 0) 106358250Seric { 106458250Seric struct stat st; 106558523Seric # ifdef NGROUPS 106658523Seric int n; 106758523Seric int gidset[NGROUPS]; 106858523Seric # endif 106958250Seric 107058517Seric if (stat(QueueDir, &st) < 0) 107158250Seric { 107258250Seric syserr("Cannot stat %s", QueueDir); 107358250Seric return; 107458250Seric } 107558523Seric # ifdef NGROUPS 107658523Seric n = getgroups(NGROUPS, gidset); 107758523Seric while (--n >= 0) 107858523Seric { 107958523Seric if (gidset[n] == st.st_gid) 108058523Seric break; 108158523Seric } 108258523Seric if (n < 0) 108358523Seric # else 108458250Seric if (getgid() != st.st_gid) 108558523Seric # endif 108658250Seric { 108758250Seric usrerr("510 You are not permitted to see the queue"); 108858250Seric setstat(EX_NOPERM); 108958250Seric return; 109058250Seric } 109158250Seric } 109258250Seric 109358250Seric /* 10949630Seric ** Read and order the queue. 10959630Seric */ 10969630Seric 109724941Seric nrequests = orderq(TRUE); 10989630Seric 10999630Seric /* 11009630Seric ** Print the work list that we have read. 11019630Seric */ 11029630Seric 11039630Seric /* first see if there is anything */ 110410070Seric if (nrequests <= 0) 11059630Seric { 110610070Seric printf("Mail queue is empty\n"); 11079630Seric return; 11089630Seric } 11099630Seric 111051920Seric CurrentLA = getla(); /* get load average */ 111140934Srick 111210096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 111325687Seric if (nrequests > QUEUESIZE) 111425687Seric printf(", only %d printed", QUEUESIZE); 111524979Seric if (Verbose) 111658716Seric printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 111724979Seric else 111858716Seric printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 11199630Seric for (w = WorkQ; w != NULL; w = w->w_next) 11209630Seric { 11219630Seric struct stat st; 112210070Seric auto time_t submittime = 0; 112310070Seric long dfsize = -1; 112458737Seric int flags = 0; 112510108Seric char message[MAXLINE]; 112624941Seric extern bool shouldqueue(); 112758689Seric extern bool lockfile(); 11289630Seric 112917468Seric f = fopen(w->w_name, "r"); 113017468Seric if (f == NULL) 113117468Seric { 113217468Seric errno = 0; 113317468Seric continue; 113417468Seric } 113558724Seric printf("%8s", w->w_name + 2); 113658689Seric if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB)) 113710070Seric printf("*"); 113857438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 113924941Seric printf("X"); 114010070Seric else 114110070Seric printf(" "); 114210070Seric errno = 0; 114317468Seric 114410108Seric message[0] = '\0'; 11459630Seric while (fgets(buf, sizeof buf, f) != NULL) 11469630Seric { 114753400Seric register int i; 114858737Seric register char *p; 114953400Seric 11509630Seric fixcrlf(buf, TRUE); 11519630Seric switch (buf[0]) 11529630Seric { 115310108Seric case 'M': /* error message */ 115453400Seric if ((i = strlen(&buf[1])) >= sizeof message) 115558737Seric i = sizeof message - 1; 115653400Seric bcopy(&buf[1], message, i); 115753400Seric message[i] = '\0'; 115810108Seric break; 115910108Seric 11609630Seric case 'S': /* sender name */ 116124979Seric if (Verbose) 116258737Seric printf("%8ld %10ld%c%.12s %.38s", 116358737Seric dfsize, 116458737Seric w->w_pri, 116558737Seric bitset(EF_WARNING, flags) ? '+' : ' ', 116658737Seric ctime(&submittime) + 4, 116724979Seric &buf[1]); 116824979Seric else 116924979Seric printf("%8ld %.16s %.45s", dfsize, 117024979Seric ctime(&submittime), &buf[1]); 117110108Seric if (message[0] != '\0') 117225027Seric printf("\n\t\t (%.60s)", message); 11739630Seric break; 117451920Seric 117540973Sbostic case 'C': /* controlling user */ 117654974Seric if (Verbose) 117758716Seric printf("\n\t\t\t\t (---%.34s---)", 117858716Seric &buf[1]); 117940973Sbostic break; 11809630Seric 11819630Seric case 'R': /* recipient name */ 118224979Seric if (Verbose) 118358716Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 118424979Seric else 118558716Seric printf("\n\t\t\t\t %.45s", &buf[1]); 11869630Seric break; 11879630Seric 11889630Seric case 'T': /* creation time */ 118924941Seric submittime = atol(&buf[1]); 11909630Seric break; 119110070Seric 119210070Seric case 'D': /* data file name */ 119310070Seric if (stat(&buf[1], &st) >= 0) 119410070Seric dfsize = st.st_size; 119510070Seric break; 119658737Seric 119758737Seric case 'F': /* flag bits */ 119858737Seric for (p = &buf[1]; *p != '\0'; p++) 119958737Seric { 120058737Seric switch (*p) 120158737Seric { 120258737Seric case 'w': 120358737Seric flags |= EF_WARNING; 120458737Seric break; 120558737Seric } 120658737Seric } 12079630Seric } 12089630Seric } 120910070Seric if (submittime == (time_t) 0) 121010070Seric printf(" (no control file)"); 12119630Seric printf("\n"); 121223098Seric (void) fclose(f); 12139630Seric } 12149630Seric } 12159630Seric 121656795Seric # endif /* QUEUE */ 121717468Seric /* 121817468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 121917468Seric ** 122017468Seric ** Assigns an id code if one does not already exist. 122117468Seric ** This code is very careful to avoid trashing existing files 122217468Seric ** under any circumstances. 122317468Seric ** 122417468Seric ** Parameters: 122517468Seric ** e -- envelope to build it in/from. 122617468Seric ** type -- the file type, used as the first character 122717468Seric ** of the file name. 122817468Seric ** 122917468Seric ** Returns: 123017468Seric ** a pointer to the new file name (in a static buffer). 123117468Seric ** 123217468Seric ** Side Effects: 123351920Seric ** If no id code is already assigned, queuename will 123451920Seric ** assign an id code, create a qf file, and leave a 123551920Seric ** locked, open-for-write file pointer in the envelope. 123617468Seric */ 123717468Seric 123817468Seric char * 123917468Seric queuename(e, type) 124017468Seric register ENVELOPE *e; 124117468Seric char type; 124217468Seric { 124317468Seric static int pid = -1; 124458741Seric static char c0; 124558741Seric static char c1; 124658741Seric static char c2; 124758716Seric time_t now; 124858716Seric struct tm *tm; 124958689Seric static char buf[MAXNAME]; 125058689Seric extern bool lockfile(); 125117468Seric 125217468Seric if (e->e_id == NULL) 125317468Seric { 125417468Seric char qf[20]; 125517468Seric 125617468Seric /* find a unique id */ 125717468Seric if (pid != getpid()) 125817468Seric { 125917468Seric /* new process -- start back at "AA" */ 126017468Seric pid = getpid(); 126158716Seric now = curtime(); 126258716Seric tm = localtime(&now); 126358716Seric c0 = 'A' + tm->tm_hour; 126417468Seric c1 = 'A'; 126517468Seric c2 = 'A' - 1; 126617468Seric } 126758716Seric (void) sprintf(qf, "qf%cAA%05d", c0, pid); 126817468Seric 126917468Seric while (c1 < '~' || c2 < 'Z') 127017468Seric { 127117468Seric int i; 127217468Seric 127317468Seric if (c2 >= 'Z') 127417468Seric { 127517468Seric c1++; 127617468Seric c2 = 'A' - 1; 127717468Seric } 127858716Seric qf[3] = c1; 127958716Seric qf[4] = ++c2; 128017468Seric if (tTd(7, 20)) 128140934Srick printf("queuename: trying \"%s\"\n", qf); 128217468Seric 128340934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 128451920Seric if (i < 0) 128551920Seric { 128651920Seric if (errno == EEXIST) 128751920Seric continue; 128851920Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 128951920Seric qf, QueueDir); 129051920Seric exit(EX_UNAVAILABLE); 129151920Seric } 129258689Seric if (lockfile(i, qf, LOCK_EX|LOCK_NB)) 129351920Seric { 129451920Seric e->e_lockfp = fdopen(i, "w"); 129540934Srick break; 129617468Seric } 129751920Seric 129851920Seric /* a reader got the file; abandon it and try again */ 129951920Seric (void) close(i); 130017468Seric } 130117468Seric if (c1 >= '~' && c2 >= 'Z') 130217468Seric { 130317468Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 130417468Seric qf, QueueDir); 130517468Seric exit(EX_OSERR); 130617468Seric } 130717468Seric e->e_id = newstr(&qf[2]); 130817468Seric define('i', e->e_id, e); 130917468Seric if (tTd(7, 1)) 131017468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 131117468Seric # ifdef LOG 131258020Seric if (LogLevel > 93) 131317468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 131456795Seric # endif /* LOG */ 131517468Seric } 131617468Seric 131717468Seric if (type == '\0') 131817468Seric return (NULL); 131917468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 132017468Seric if (tTd(7, 2)) 132117468Seric printf("queuename: %s\n", buf); 132217468Seric return (buf); 132317468Seric } 132417468Seric /* 132517468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 132617468Seric ** 132717468Seric ** Parameters: 132817468Seric ** e -- the envelope to unlock. 132917468Seric ** 133017468Seric ** Returns: 133117468Seric ** none 133217468Seric ** 133317468Seric ** Side Effects: 133417468Seric ** unlocks the queue for `e'. 133517468Seric */ 133617468Seric 133717468Seric unlockqueue(e) 133817468Seric ENVELOPE *e; 133917468Seric { 134058680Seric if (tTd(51, 4)) 134158680Seric printf("unlockqueue(%s)\n", e->e_id); 134258680Seric 134351920Seric /* if there is a lock file in the envelope, close it */ 134451920Seric if (e->e_lockfp != NULL) 134558680Seric xfclose(e->e_lockfp, "unlockqueue", e->e_id); 134651920Seric e->e_lockfp = NULL; 134751920Seric 134858728Seric /* don't create a queue id if we don't already have one */ 134958728Seric if (e->e_id == NULL) 135058728Seric return; 135158728Seric 135217468Seric /* remove the transcript */ 135317468Seric # ifdef LOG 135458020Seric if (LogLevel > 87) 135517468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 135656795Seric # endif /* LOG */ 135758680Seric if (!tTd(51, 104)) 135817468Seric xunlink(queuename(e, 'x')); 135917468Seric 136017468Seric } 136140973Sbostic /* 136254974Seric ** SETCTLUSER -- create a controlling address 136340973Sbostic ** 136454974Seric ** Create a fake "address" given only a local login name; this is 136554974Seric ** used as a "controlling user" for future recipient addresses. 136640973Sbostic ** 136740973Sbostic ** Parameters: 136854974Seric ** user -- the user name of the controlling user. 136940973Sbostic ** 137040973Sbostic ** Returns: 137154974Seric ** An address descriptor for the controlling user. 137240973Sbostic ** 137340973Sbostic ** Side Effects: 137440973Sbostic ** none. 137540973Sbostic */ 137640973Sbostic 137754974Seric ADDRESS * 137854974Seric setctluser(user) 137954974Seric char *user; 138040973Sbostic { 138154974Seric register ADDRESS *a; 138240973Sbostic struct passwd *pw; 138340973Sbostic 138440973Sbostic /* 138554974Seric ** See if this clears our concept of controlling user. 138640973Sbostic */ 138740973Sbostic 138854974Seric if (user == NULL || *user == '\0') 138958704Seric user = DefUser; 139040973Sbostic 139140973Sbostic /* 139254974Seric ** Set up addr fields for controlling user. 139340973Sbostic */ 139440973Sbostic 139554974Seric a = (ADDRESS *) xalloc(sizeof *a); 139654974Seric bzero((char *) a, sizeof *a); 139754974Seric if ((pw = getpwnam(user)) != NULL) 139840973Sbostic { 139940973Sbostic a->q_home = newstr(pw->pw_dir); 140040973Sbostic a->q_uid = pw->pw_uid; 140140973Sbostic a->q_gid = pw->pw_gid; 140257642Seric a->q_user = newstr(user); 140340973Sbostic } 140440973Sbostic else 140540973Sbostic { 140640973Sbostic a->q_uid = DefUid; 140740973Sbostic a->q_gid = DefGid; 140857642Seric a->q_user = newstr(DefUser); 140940973Sbostic } 141040973Sbostic 141158294Seric a->q_flags |= QGOODUID|QPRIMARY; /* flag as a "ctladdr" */ 141256328Seric a->q_mailer = LocalMailer; 141354974Seric return a; 141440973Sbostic } 1415