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*58250Seric static char sccsid[] = "@(#)queue.c 6.19 (Berkeley) 02/26/93 (with queueing)"; 1433731Sbostic #else 15*58250Seric static char sccsid[] = "@(#)queue.c 6.19 (Berkeley) 02/26/93 (without queueing)"; 1633731Sbostic #endif 1733731Sbostic #endif /* not lint */ 1833731Sbostic 194632Seric # include <sys/stat.h> 2013707Ssam # include <sys/dir.h> 2157948Seric # include <sys/file.h> 224634Seric # include <signal.h> 234632Seric # include <errno.h> 2440973Sbostic # include <pwd.h> 2551937Seric # include <fcntl.h> 2657977Seric # ifndef MAXNAMLEN 2757736Seric # include <dirent.h> 2857736Seric # endif 294632Seric 3033731Sbostic # ifdef QUEUE 314632Seric 324632Seric /* 339377Seric ** Work queue. 349377Seric */ 359377Seric 369377Seric struct work 379377Seric { 389377Seric char *w_name; /* name of control file */ 399377Seric long w_pri; /* priority of message, see below */ 4025013Seric time_t w_ctime; /* creation time of message */ 419377Seric struct work *w_next; /* next in queue */ 429377Seric }; 439377Seric 449377Seric typedef struct work WORK; 459377Seric 469377Seric WORK *WorkQ; /* queue of things to be done */ 479377Seric /* 484632Seric ** QUEUEUP -- queue a message up for future transmission. 494632Seric ** 504632Seric ** Parameters: 516980Seric ** e -- the envelope to queue up. 526999Seric ** queueall -- if TRUE, queue all addresses, rather than 536999Seric ** just those with the QQUEUEUP flag set. 549377Seric ** announce -- if TRUE, tell when you are queueing up. 554632Seric ** 564632Seric ** Returns: 5751920Seric ** none. 584632Seric ** 594632Seric ** Side Effects: 609377Seric ** The current request are saved in a control file. 6151920Seric ** The queue file is left locked. 624632Seric */ 634632Seric 649377Seric queueup(e, queueall, announce) 656980Seric register ENVELOPE *e; 666999Seric bool queueall; 679377Seric bool announce; 684632Seric { 697812Seric char *qf; 707812Seric register FILE *tfp; 714632Seric register HDR *h; 725007Seric register ADDRESS *q; 7351920Seric int fd; 7451920Seric int i; 7551920Seric bool newid; 7653400Seric register char *p; 7710173Seric MAILER nullmailer; 7854974Seric ADDRESS *lastctladdr; 7954975Seric static ADDRESS *nullctladdr = NULL; 8051920Seric char buf[MAXLINE], tf[MAXLINE]; 8153400Seric extern char *macvalue(); 8254974Seric extern ADDRESS *getctladdr(); 834632Seric 845037Seric /* 8554975Seric ** If we don't have nullctladdr, create one 8654975Seric */ 8754975Seric 8854975Seric if (nullctladdr == NULL) 8954975Seric { 9054975Seric nullctladdr = (ADDRESS *) xalloc(sizeof *nullctladdr); 9154975Seric bzero((char *) nullctladdr, sizeof nullctladdr); 9254975Seric } 9354975Seric 9454975Seric /* 9517477Seric ** Create control file. 965037Seric */ 974632Seric 9851920Seric newid = (e->e_id == NULL); 9951920Seric strcpy(tf, queuename(e, 't')); 10051920Seric tfp = e->e_lockfp; 10151920Seric if (tfp == NULL) 10251920Seric newid = FALSE; 10351920Seric if (newid) 10451835Seric { 10551920Seric tfp = e->e_lockfp; 10651920Seric } 10751920Seric else 10851920Seric { 10951920Seric /* get a locked tf file */ 11051920Seric for (i = 100; --i >= 0; ) 11151835Seric { 11251937Seric # ifdef LOCKF 11351937Seric struct flock lfd; 11451937Seric # endif 11551937Seric 11651920Seric fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode); 11751920Seric if (fd < 0) 11851835Seric { 11951920Seric if (errno == EEXIST) 12051920Seric continue; 12151920Seric syserr("queueup: cannot create temp file %s", tf); 12251920Seric return; 12341636Srick } 12451835Seric # ifdef LOCKF 12551937Seric lfd.l_type = F_WRLCK; 12651937Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 12751937Seric if (fcntl(fd, F_SETLK, &lfd) >= 0) 12851920Seric break; 12951920Seric if (errno != EACCES && errno != EAGAIN) 13051920Seric syserr("cannot lockf(%s)", tf); 13151835Seric # else 13251920Seric if (flock(fd, LOCK_EX|LOCK_NB) >= 0) 13351920Seric break; 13451920Seric if (errno != EWOULDBLOCK) 13551920Seric syserr("cannot flock(%s)", tf); 13651835Seric # endif 13751920Seric close(fd); 13841636Srick } 13941636Srick 14051920Seric tfp = fdopen(fd, "w"); 14151920Seric } 1424632Seric 1437677Seric if (tTd(40, 1)) 14417468Seric printf("queueing %s\n", e->e_id); 1454632Seric 1464632Seric /* 1476980Seric ** If there is no data file yet, create one. 1486980Seric */ 1496980Seric 1506980Seric if (e->e_df == NULL) 1516980Seric { 1526980Seric register FILE *dfp; 1539389Seric extern putbody(); 1546980Seric 1557812Seric e->e_df = newstr(queuename(e, 'd')); 15640934Srick fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode); 15740934Srick if (fd < 0) 1586980Seric { 1596980Seric syserr("queueup: cannot create %s", e->e_df); 16051920Seric if (!newid) 16151920Seric (void) fclose(tfp); 16251920Seric return; 1636980Seric } 16440934Srick dfp = fdopen(fd, "w"); 16510173Seric (*e->e_putbody)(dfp, ProgMailer, e); 1667009Seric (void) fclose(dfp); 1679389Seric e->e_putbody = putbody; 1686980Seric } 1696980Seric 1706980Seric /* 1714632Seric ** Output future work requests. 17225687Seric ** Priority and creation time should be first, since 17325687Seric ** they are required by orderq. 1744632Seric */ 1754632Seric 1769377Seric /* output message priority */ 1779377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 1789377Seric 1799630Seric /* output creation time */ 1809630Seric fprintf(tfp, "T%ld\n", e->e_ctime); 1819630Seric 1824632Seric /* output name of data file */ 1837812Seric fprintf(tfp, "D%s\n", e->e_df); 1844632Seric 18510108Seric /* message from envelope, if it exists */ 18610108Seric if (e->e_message != NULL) 18710108Seric fprintf(tfp, "M%s\n", e->e_message); 18810108Seric 18953400Seric /* $r and $s macro values */ 19053400Seric if ((p = macvalue('r', e)) != NULL) 19153400Seric fprintf(tfp, "$r%s\n", p); 19253400Seric if ((p = macvalue('s', e)) != NULL) 19353400Seric fprintf(tfp, "$s%s\n", p); 19453400Seric 1954632Seric /* output name of sender */ 1967812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 1974632Seric 19855360Seric /* output list of error recipients */ 19955360Seric lastctladdr = NULL; 20055360Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 20155360Seric { 20255360Seric if (!bitset(QDONTSEND, q->q_flags)) 20355360Seric { 20455360Seric ADDRESS *ctladdr; 20555360Seric 20655360Seric ctladdr = getctladdr(q); 20755360Seric if (ctladdr == NULL && q->q_alias != NULL) 20855360Seric ctladdr = nullctladdr; 20955360Seric if (ctladdr != lastctladdr) 21055360Seric { 21155360Seric printctladdr(ctladdr, tfp); 21255360Seric lastctladdr = ctladdr; 21355360Seric } 21455360Seric fprintf(tfp, "E%s\n", q->q_paddr); 21555360Seric } 21655360Seric } 21755360Seric 2184632Seric /* output list of recipient addresses */ 2196980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 2204632Seric { 221*58250Seric if (bitset(QQUEUEUP, q->q_flags) || 222*58250Seric (queueall && !bitset(QDONTSEND|QSENT, q->q_flags))) 2238245Seric { 22454974Seric ADDRESS *ctladdr; 22540973Sbostic 22654975Seric ctladdr = getctladdr(q); 22754975Seric if (ctladdr == NULL && q->q_alias != NULL) 22854975Seric ctladdr = nullctladdr; 22954975Seric if (ctladdr != lastctladdr) 23054974Seric { 23154974Seric printctladdr(ctladdr, tfp); 23254974Seric lastctladdr = ctladdr; 23354974Seric } 2347812Seric fprintf(tfp, "R%s\n", q->q_paddr); 2359377Seric if (announce) 2369377Seric { 2379377Seric e->e_to = q->q_paddr; 23858151Seric message("queued"); 23958020Seric if (LogLevel > 8) 24054967Seric logdelivery("queued", e); 2419377Seric e->e_to = NULL; 2429377Seric } 2439387Seric if (tTd(40, 1)) 2449387Seric { 2459387Seric printf("queueing "); 2469387Seric printaddr(q, FALSE); 2479387Seric } 2488245Seric } 2494632Seric } 2504632Seric 2519377Seric /* 2529377Seric ** Output headers for this message. 2539377Seric ** Expand macros completely here. Queue run will deal with 2549377Seric ** everything as absolute headers. 2559377Seric ** All headers that must be relative to the recipient 2569377Seric ** can be cracked later. 25710173Seric ** We set up a "null mailer" -- i.e., a mailer that will have 25810173Seric ** no effect on the addresses as they are output. 2599377Seric */ 2609377Seric 26110686Seric bzero((char *) &nullmailer, sizeof nullmailer); 26258020Seric nullmailer.m_re_rwset = nullmailer.m_rh_rwset = 26358020Seric nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1; 26410349Seric nullmailer.m_eol = "\n"; 26510173Seric 26658050Seric define('g', "\201f", e); 2676980Seric for (h = e->e_header; h != NULL; h = h->h_link) 2684632Seric { 26910686Seric extern bool bitzerop(); 27010686Seric 27112015Seric /* don't output null headers */ 2724632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 2734632Seric continue; 27412015Seric 27512015Seric /* don't output resent headers on non-resent messages */ 27612015Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 27712015Seric continue; 27812015Seric 27912015Seric /* output this header */ 2807812Seric fprintf(tfp, "H"); 28112015Seric 28212015Seric /* if conditional, output the set of conditions */ 28310686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 28410686Seric { 28510686Seric int j; 28610686Seric 28723098Seric (void) putc('?', tfp); 28810686Seric for (j = '\0'; j <= '\177'; j++) 28910686Seric if (bitnset(j, h->h_mflags)) 29023098Seric (void) putc(j, tfp); 29123098Seric (void) putc('?', tfp); 29210686Seric } 29312015Seric 29412015Seric /* output the header: expand macros, convert addresses */ 2957763Seric if (bitset(H_DEFAULT, h->h_flags)) 2967763Seric { 2977763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 2988236Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 2997763Seric } 3008245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 3019348Seric { 30255012Seric commaize(h, h->h_value, tfp, 30355012Seric bitset(EF_OLDSTYLE, e->e_flags), 30455012Seric &nullmailer, e); 3059348Seric } 3067763Seric else 3078245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 3084632Seric } 3094632Seric 3104632Seric /* 3114632Seric ** Clean up. 3124632Seric */ 3134632Seric 31451920Seric if (!newid) 31551920Seric { 31651920Seric qf = queuename(e, 'q'); 31751920Seric if (rename(tf, qf) < 0) 31851920Seric syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df); 31951920Seric if (e->e_lockfp != NULL) 32051920Seric (void) fclose(e->e_lockfp); 32151920Seric e->e_lockfp = tfp; 32251920Seric } 32351920Seric else 32451920Seric qf = tf; 32541636Srick errno = 0; 3267391Seric 3277677Seric # ifdef LOG 3287677Seric /* save log info */ 32958020Seric if (LogLevel > 79) 3307878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 33156795Seric # endif /* LOG */ 33240934Srick fflush(tfp); 33351920Seric return; 3344632Seric } 33554974Seric 33654974Seric printctladdr(a, tfp) 33754974Seric ADDRESS *a; 33854974Seric FILE *tfp; 33954974Seric { 34054974Seric char *u; 34154974Seric struct passwd *pw; 34254974Seric extern struct passwd *getpwuid(); 34354974Seric 34454974Seric if (a == NULL) 34554974Seric { 34654974Seric fprintf(tfp, "C\n"); 34754974Seric return; 34854974Seric } 34954974Seric if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL) 35054974Seric u = DefUser; 35154974Seric else 35254974Seric u = pw->pw_name; 35354974Seric fprintf(tfp, "C%s\n", u); 35454974Seric } 35554974Seric 3564632Seric /* 3574632Seric ** RUNQUEUE -- run the jobs in the queue. 3584632Seric ** 3594632Seric ** Gets the stuff out of the queue in some presumably logical 3604632Seric ** order and processes them. 3614632Seric ** 3624632Seric ** Parameters: 36324941Seric ** forkflag -- TRUE if the queue scanning should be done in 36424941Seric ** a child process. We double-fork so it is not our 36524941Seric ** child and we don't have to clean up after it. 3664632Seric ** 3674632Seric ** Returns: 3684632Seric ** none. 3694632Seric ** 3704632Seric ** Side Effects: 3714632Seric ** runs things in the mail queue. 3724632Seric */ 3734632Seric 37455360Seric ENVELOPE QueueEnvelope; /* the queue run envelope */ 37555360Seric 37655360Seric runqueue(forkflag) 3774639Seric bool forkflag; 3784632Seric { 37924953Seric extern bool shouldqueue(); 38055360Seric register ENVELOPE *e; 38155360Seric extern ENVELOPE BlankEnvelope; 38255360Seric extern ENVELOPE *newenvelope(); 38324953Seric 3847466Seric /* 38524953Seric ** If no work will ever be selected, don't even bother reading 38624953Seric ** the queue. 38724953Seric */ 38824953Seric 38951920Seric CurrentLA = getla(); /* get load average */ 39040934Srick 39158132Seric if (shouldqueue(0L, curtime())) 39224953Seric { 39324953Seric if (Verbose) 39424953Seric printf("Skipping queue run -- load average too high\n"); 39558107Seric if (forkflag && QueueIntvl != 0) 39658107Seric (void) setevent(QueueIntvl, runqueue, TRUE); 39755360Seric return; 39824953Seric } 39924953Seric 40024953Seric /* 4017466Seric ** See if we want to go off and do other useful work. 4027466Seric */ 4034639Seric 4044639Seric if (forkflag) 4054639Seric { 4067943Seric int pid; 4077943Seric 4087943Seric pid = dofork(); 4097943Seric if (pid != 0) 4104639Seric { 41146928Sbostic extern void reapchild(); 41225184Seric 4137943Seric /* parent -- pick up intermediate zombie */ 41425184Seric #ifndef SIGCHLD 4159377Seric (void) waitfor(pid); 41656795Seric #else /* SIGCHLD */ 41725184Seric (void) signal(SIGCHLD, reapchild); 41856795Seric #endif /* SIGCHLD */ 4197690Seric if (QueueIntvl != 0) 4209348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 4214639Seric return; 4224639Seric } 4237943Seric /* child -- double fork */ 42425184Seric #ifndef SIGCHLD 4257943Seric if (fork() != 0) 4267943Seric exit(EX_OK); 42756795Seric #else /* SIGCHLD */ 42825184Seric (void) signal(SIGCHLD, SIG_DFL); 42956795Seric #endif /* SIGCHLD */ 4304639Seric } 43124941Seric 43240934Srick setproctitle("running queue: %s", QueueDir); 43357731Seric ForceMail = TRUE; 43424941Seric 4357876Seric # ifdef LOG 43658020Seric if (LogLevel > 69) 43755360Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 43855360Seric QueueDir, getpid(), forkflag); 43956795Seric # endif /* LOG */ 4404639Seric 4417466Seric /* 44210205Seric ** Release any resources used by the daemon code. 44310205Seric */ 44410205Seric 44510205Seric # ifdef DAEMON 44610205Seric clrdaemon(); 44756795Seric # endif /* DAEMON */ 44810205Seric 44910205Seric /* 45055360Seric ** Create ourselves an envelope 45155360Seric */ 45255360Seric 45355360Seric CurEnv = &QueueEnvelope; 45458179Seric e = newenvelope(&QueueEnvelope, CurEnv); 45555360Seric e->e_flags = BlankEnvelope.e_flags; 45655360Seric 45755360Seric /* 45827175Seric ** Make sure the alias database is open. 45927175Seric */ 46027175Seric 46155012Seric initaliases(AliasFile, FALSE, e); 46227175Seric 46327175Seric /* 4647466Seric ** Start making passes through the queue. 4657466Seric ** First, read and sort the entire queue. 4667466Seric ** Then, process the work in that order. 4677466Seric ** But if you take too long, start over. 4687466Seric */ 4697466Seric 4707943Seric /* order the existing work requests */ 47124954Seric (void) orderq(FALSE); 4727690Seric 4737943Seric /* process them once at a time */ 4747943Seric while (WorkQ != NULL) 4754639Seric { 4767943Seric WORK *w = WorkQ; 4777881Seric 4787943Seric WorkQ = WorkQ->w_next; 47955012Seric dowork(w, e); 4807943Seric free(w->w_name); 4817943Seric free((char *) w); 4824639Seric } 48329866Seric 48429866Seric /* exit without the usual cleanup */ 48555467Seric e->e_id = NULL; 48655467Seric finis(); 4874634Seric } 4884634Seric /* 4894632Seric ** ORDERQ -- order the work queue. 4904632Seric ** 4914632Seric ** Parameters: 49224941Seric ** doall -- if set, include everything in the queue (even 49324941Seric ** the jobs that cannot be run because the load 49424941Seric ** average is too high). Otherwise, exclude those 49524941Seric ** jobs. 4964632Seric ** 4974632Seric ** Returns: 49810121Seric ** The number of request in the queue (not necessarily 49910121Seric ** the number of requests in WorkQ however). 5004632Seric ** 5014632Seric ** Side Effects: 5024632Seric ** Sets WorkQ to the queue of available work, in order. 5034632Seric */ 5044632Seric 50525687Seric # define NEED_P 001 50625687Seric # define NEED_T 002 5074632Seric 50824941Seric orderq(doall) 50924941Seric bool doall; 5104632Seric { 5116625Sglickman register struct direct *d; 5124632Seric register WORK *w; 5136625Sglickman DIR *f; 5144632Seric register int i; 51525687Seric WORK wlist[QUEUESIZE+1]; 51610070Seric int wn = -1; 5174632Seric extern workcmpf(); 5184632Seric 5194632Seric /* clear out old WorkQ */ 5204632Seric for (w = WorkQ; w != NULL; ) 5214632Seric { 5224632Seric register WORK *nw = w->w_next; 5234632Seric 5244632Seric WorkQ = nw; 5254632Seric free(w->w_name); 5264632Seric free((char *) w); 5274632Seric w = nw; 5284632Seric } 5294632Seric 5304632Seric /* open the queue directory */ 5318148Seric f = opendir("."); 5324632Seric if (f == NULL) 5334632Seric { 5348148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 53510070Seric return (0); 5364632Seric } 5374632Seric 5384632Seric /* 5394632Seric ** Read the work directory. 5404632Seric */ 5414632Seric 54210070Seric while ((d = readdir(f)) != NULL) 5434632Seric { 5449377Seric FILE *cf; 5454632Seric char lbuf[MAXNAME]; 54657642Seric extern bool shouldqueue(); 5474632Seric 5484632Seric /* is this an interesting entry? */ 5497812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 5504632Seric continue; 5514632Seric 55258020Seric if (strlen(d->d_name) != 9) 55358020Seric { 55458020Seric if (Verbose) 55558020Seric printf("orderq: bogus qf name %s\n", d->d_name); 55658020Seric #ifdef LOG 55758020Seric if (LogLevel > 3) 55858020Seric syslog(LOG_NOTICE, "orderq: bogus qf name %s", 55958020Seric d->d_name); 56058020Seric #endif 56158020Seric if (strlen(d->d_name) >= MAXNAME) 56258020Seric d->d_name[MAXNAME - 1] = '\0'; 56358020Seric strcpy(lbuf, d->d_name); 56458020Seric lbuf[0] = 'Q'; 56558020Seric (void) rename(d->d_name, lbuf); 56658020Seric continue; 56758020Seric } 56858020Seric 56910070Seric /* yes -- open control file (if not too many files) */ 57025687Seric if (++wn >= QUEUESIZE) 57110070Seric continue; 5728148Seric cf = fopen(d->d_name, "r"); 5734632Seric if (cf == NULL) 5744632Seric { 5757055Seric /* this may be some random person sending hir msgs */ 5767055Seric /* syserr("orderq: cannot open %s", cbuf); */ 57710090Seric if (tTd(41, 2)) 57810090Seric printf("orderq: cannot open %s (%d)\n", 57910090Seric d->d_name, errno); 5807055Seric errno = 0; 58110090Seric wn--; 5824632Seric continue; 5834632Seric } 58425687Seric w = &wlist[wn]; 58525687Seric w->w_name = newstr(d->d_name); 5864632Seric 58725027Seric /* make sure jobs in creation don't clog queue */ 58825687Seric w->w_pri = 0x7fffffff; 58925687Seric w->w_ctime = 0; 59025027Seric 5914632Seric /* extract useful information */ 59225687Seric i = NEED_P | NEED_T; 59325687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 5944632Seric { 59524954Seric extern long atol(); 59624954Seric 59724941Seric switch (lbuf[0]) 5984632Seric { 59924941Seric case 'P': 60025687Seric w->w_pri = atol(&lbuf[1]); 60125687Seric i &= ~NEED_P; 6024632Seric break; 60325013Seric 60425013Seric case 'T': 60525687Seric w->w_ctime = atol(&lbuf[1]); 60625687Seric i &= ~NEED_T; 60725013Seric break; 6084632Seric } 6094632Seric } 6104632Seric (void) fclose(cf); 61124953Seric 61257438Seric if (!doall && shouldqueue(w->w_pri, w->w_ctime)) 61324953Seric { 61424953Seric /* don't even bother sorting this job in */ 61524953Seric wn--; 61624953Seric } 6174632Seric } 6186625Sglickman (void) closedir(f); 61910090Seric wn++; 6204632Seric 6214632Seric /* 6224632Seric ** Sort the work directory. 6234632Seric */ 6244632Seric 62525687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 6264632Seric 6274632Seric /* 6284632Seric ** Convert the work list into canonical form. 6299377Seric ** Should be turning it into a list of envelopes here perhaps. 6304632Seric */ 6314632Seric 63224981Seric WorkQ = NULL; 63325687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 6344632Seric { 6354632Seric w = (WORK *) xalloc(sizeof *w); 6364632Seric w->w_name = wlist[i].w_name; 6374632Seric w->w_pri = wlist[i].w_pri; 63825013Seric w->w_ctime = wlist[i].w_ctime; 63924981Seric w->w_next = WorkQ; 64024981Seric WorkQ = w; 6414632Seric } 6424632Seric 6437677Seric if (tTd(40, 1)) 6444632Seric { 6454632Seric for (w = WorkQ; w != NULL; w = w->w_next) 6465037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 6474632Seric } 64810070Seric 64910090Seric return (wn); 6504632Seric } 6514632Seric /* 6527677Seric ** WORKCMPF -- compare function for ordering work. 6534632Seric ** 6544632Seric ** Parameters: 6554632Seric ** a -- the first argument. 6564632Seric ** b -- the second argument. 6574632Seric ** 6584632Seric ** Returns: 65924981Seric ** -1 if a < b 66024981Seric ** 0 if a == b 66124981Seric ** +1 if a > b 6624632Seric ** 6634632Seric ** Side Effects: 6644632Seric ** none. 6654632Seric */ 6664632Seric 6674632Seric workcmpf(a, b) 6685037Seric register WORK *a; 6695037Seric register WORK *b; 6704632Seric { 67157438Seric long pa = a->w_pri; 67257438Seric long pb = b->w_pri; 67324941Seric 67424941Seric if (pa == pb) 6754632Seric return (0); 67624941Seric else if (pa > pb) 67724981Seric return (1); 67824981Seric else 67910121Seric return (-1); 6804632Seric } 6814632Seric /* 6824632Seric ** DOWORK -- do a work request. 6834632Seric ** 6844632Seric ** Parameters: 6854632Seric ** w -- the work request to be satisfied. 6864632Seric ** 6874632Seric ** Returns: 6884632Seric ** none. 6894632Seric ** 6904632Seric ** Side Effects: 6914632Seric ** The work request is satisfied if possible. 6924632Seric */ 6934632Seric 69455012Seric dowork(w, e) 6954632Seric register WORK *w; 69655012Seric register ENVELOPE *e; 6974632Seric { 6984632Seric register int i; 69924941Seric extern bool shouldqueue(); 70051920Seric extern bool readqf(); 7014632Seric 7027677Seric if (tTd(40, 1)) 7035037Seric printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 7044632Seric 7054632Seric /* 70624941Seric ** Ignore jobs that are too expensive for the moment. 7074632Seric */ 7084632Seric 70957438Seric if (shouldqueue(w->w_pri, w->w_ctime)) 7104632Seric { 71124941Seric if (Verbose) 71224967Seric printf("\nSkipping %s\n", w->w_name + 2); 7134632Seric return; 7144632Seric } 7154632Seric 71624941Seric /* 71724941Seric ** Fork for work. 71824941Seric */ 71924941Seric 72024941Seric if (ForkQueueRuns) 72124941Seric { 72224941Seric i = fork(); 72324941Seric if (i < 0) 72424941Seric { 72524941Seric syserr("dowork: cannot fork"); 72624941Seric return; 72724941Seric } 72824941Seric } 72924941Seric else 73024941Seric { 73124941Seric i = 0; 73224941Seric } 73324941Seric 7344632Seric if (i == 0) 7354632Seric { 7364632Seric /* 7374632Seric ** CHILD 7388148Seric ** Lock the control file to avoid duplicate deliveries. 7398148Seric ** Then run the file as though we had just read it. 7407350Seric ** We save an idea of the temporary name so we 7417350Seric ** can recover on interrupt. 7424632Seric */ 7434632Seric 7447763Seric /* set basic modes, etc. */ 7457356Seric (void) alarm(0); 74655012Seric clearenvelope(e, FALSE); 7474632Seric QueueRun = TRUE; 7489377Seric ErrorMode = EM_MAIL; 74955012Seric e->e_id = &w->w_name[2]; 7507876Seric # ifdef LOG 75158020Seric if (LogLevel > 76) 75255012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 7537881Seric getpid()); 75456795Seric # endif /* LOG */ 7557763Seric 7567763Seric /* don't use the headers from sendmail.cf... */ 75755012Seric e->e_header = NULL; 7587763Seric 75951920Seric /* read the queue control file -- return if locked */ 76055012Seric if (!readqf(e)) 7616980Seric { 76224941Seric if (ForkQueueRuns) 76324941Seric exit(EX_OK); 76424941Seric else 76524941Seric return; 7666980Seric } 7676980Seric 76855012Seric e->e_flags |= EF_INQUEUE; 76957642Seric eatheader(e, TRUE); 7706980Seric 7716980Seric /* do the delivery */ 77255012Seric if (!bitset(EF_FATALERRS, e->e_flags)) 77355012Seric sendall(e, SM_DELIVER); 7746980Seric 7756980Seric /* finish up and exit */ 77624941Seric if (ForkQueueRuns) 77724941Seric finis(); 77824941Seric else 77955012Seric dropenvelope(e); 7804632Seric } 78124941Seric else 78224941Seric { 78324941Seric /* 78424941Seric ** Parent -- pick up results. 78524941Seric */ 7864632Seric 78724941Seric errno = 0; 78824941Seric (void) waitfor(i); 78924941Seric } 7904632Seric } 7914632Seric /* 7924632Seric ** READQF -- read queue file and set up environment. 7934632Seric ** 7944632Seric ** Parameters: 7959377Seric ** e -- the envelope of the job to run. 7964632Seric ** 7974632Seric ** Returns: 79851920Seric ** TRUE if it successfully read the queue file. 79951920Seric ** FALSE otherwise. 8004632Seric ** 8014632Seric ** Side Effects: 80251920Seric ** The queue file is returned locked. 8034632Seric */ 8044632Seric 80551920Seric bool 80651920Seric readqf(e) 8079377Seric register ENVELOPE *e; 8084632Seric { 80917477Seric char *qf; 81017477Seric register FILE *qfp; 81154974Seric ADDRESS *ctladdr; 81256400Seric struct stat st; 81357135Seric char *bp; 81457135Seric char buf[MAXLINE]; 8159348Seric extern char *fgetfolded(); 81624954Seric extern long atol(); 81754974Seric extern ADDRESS *setctluser(); 81851937Seric # ifdef LOCKF 81951937Seric struct flock lfd; 82051937Seric # endif 8214632Seric 8224632Seric /* 82317468Seric ** Read and process the file. 8244632Seric */ 8254632Seric 82617477Seric qf = queuename(e, 'q'); 82751937Seric qfp = fopen(qf, "r+"); 82817477Seric if (qfp == NULL) 82917477Seric { 83040934Srick if (errno != ENOENT) 83140934Srick syserr("readqf: no control file %s", qf); 83251920Seric return FALSE; 83317477Seric } 83440934Srick 83556400Seric /* 83656400Seric ** Check the queue file for plausibility to avoid attacks. 83756400Seric */ 83856400Seric 83956400Seric if (fstat(fileno(qfp), &st) < 0) 84056400Seric { 84156400Seric /* must have been being processed by someone else */ 84256400Seric fclose(qfp); 84356400Seric return FALSE; 84456400Seric } 84556400Seric 84657135Seric if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode) 84756400Seric { 84856400Seric # ifdef LOG 84956400Seric if (LogLevel > 0) 85056400Seric { 85156400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 85256400Seric e->e_id, st.st_uid, st.st_mode); 85356400Seric } 85456795Seric # endif /* LOG */ 85556400Seric fclose(qfp); 85656400Seric return FALSE; 85756400Seric } 85856400Seric 85951835Seric # ifdef LOCKF 86051937Seric lfd.l_type = F_WRLCK; 86151937Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 86251937Seric if (fcntl(fileno(qfp), F_SETLK, &lfd) < 0) 86351835Seric # else 86440934Srick if (flock(fileno(qfp), LOCK_EX|LOCK_NB) < 0) 86551835Seric # endif 86640934Srick { 86758062Seric if (errno == EWOULDBLOCK) 86858062Seric { 86958062Seric /* being processed by another queuer */ 87058062Seric if (Verbose) 87158062Seric printf("%s: locked\n", e->e_id); 87251920Seric # ifdef LOG 87358062Seric if (LogLevel > 19) 87458062Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 87556795Seric # endif /* LOG */ 87658062Seric } 87758062Seric else 87858062Seric { 87958062Seric syserr("%s: flock failure", e->e_id); 88058062Seric } 88140934Srick (void) fclose(qfp); 88251920Seric return FALSE; 88340934Srick } 88440934Srick 88551920Seric /* save this lock */ 88651920Seric e->e_lockfp = qfp; 88751920Seric 88840934Srick /* do basic system initialization */ 88955012Seric initsys(e); 89040934Srick 89117477Seric FileName = qf; 8929377Seric LineNumber = 0; 89351920Seric if (Verbose) 8949377Seric printf("\nRunning %s\n", e->e_id); 89554974Seric ctladdr = NULL; 89657135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 8974632Seric { 89857529Seric struct stat st; 89957529Seric 90026504Seric if (tTd(40, 4)) 90157135Seric printf("+++++ %s\n", bp); 90257135Seric switch (bp[0]) 9034632Seric { 90440973Sbostic case 'C': /* specify controlling user */ 90557135Seric ctladdr = setctluser(&bp[1]); 90640973Sbostic break; 90740973Sbostic 9084632Seric case 'R': /* specify recipient */ 90958082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 9104632Seric break; 9114632Seric 91225687Seric case 'E': /* specify error recipient */ 91358082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 91425687Seric break; 91525687Seric 9164632Seric case 'H': /* header */ 91757135Seric (void) chompheader(&bp[1], FALSE, e); 9184632Seric break; 9194632Seric 92010108Seric case 'M': /* message */ 92157135Seric e->e_message = newstr(&bp[1]); 92210108Seric break; 92310108Seric 9244632Seric case 'S': /* sender */ 92557135Seric setsender(newstr(&bp[1]), e); 9264632Seric break; 9274632Seric 9284632Seric case 'D': /* data file name */ 92957135Seric e->e_df = newstr(&bp[1]); 9309544Seric e->e_dfp = fopen(e->e_df, "r"); 9319544Seric if (e->e_dfp == NULL) 93258020Seric { 9339377Seric syserr("readqf: cannot open %s", e->e_df); 93458020Seric e->e_msgsize = -1; 93558020Seric } 93658020Seric else if (fstat(fileno(e->e_dfp), &st) >= 0) 93757529Seric e->e_msgsize = st.st_size; 9384632Seric break; 9394632Seric 9407860Seric case 'T': /* init time */ 94157135Seric e->e_ctime = atol(&bp[1]); 9424632Seric break; 9434632Seric 9444634Seric case 'P': /* message priority */ 94557135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 9464634Seric break; 9474634Seric 94853400Seric case '$': /* define macro */ 94957135Seric define(bp[1], newstr(&bp[2]), e); 95053400Seric break; 95153400Seric 95224941Seric case '\0': /* blank line; ignore */ 95324941Seric break; 95424941Seric 9554632Seric default: 95624941Seric syserr("readqf(%s:%d): bad line \"%s\"", e->e_id, 95757135Seric LineNumber, bp); 9584632Seric break; 9594632Seric } 96057135Seric 96157135Seric if (bp != buf) 96257135Seric free(bp); 9634632Seric } 9649377Seric 9659377Seric FileName = NULL; 96624941Seric 96724941Seric /* 96824941Seric ** If we haven't read any lines, this queue file is empty. 96924941Seric ** Arrange to remove it without referencing any null pointers. 97024941Seric */ 97124941Seric 97224941Seric if (LineNumber == 0) 97324941Seric { 97424941Seric errno = 0; 97524941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 97624941Seric } 97751920Seric return TRUE; 9784632Seric } 9794632Seric /* 9809630Seric ** PRINTQUEUE -- print out a representation of the mail queue 9819630Seric ** 9829630Seric ** Parameters: 9839630Seric ** none. 9849630Seric ** 9859630Seric ** Returns: 9869630Seric ** none. 9879630Seric ** 9889630Seric ** Side Effects: 9899630Seric ** Prints a listing of the mail queue on the standard output. 9909630Seric */ 9915182Seric 9929630Seric printqueue() 9939630Seric { 9949630Seric register WORK *w; 9959630Seric FILE *f; 99610070Seric int nrequests; 9979630Seric char buf[MAXLINE]; 9989630Seric 9999630Seric /* 1000*58250Seric ** Check for permission to print the queue 1001*58250Seric */ 1002*58250Seric 1003*58250Seric if (bitset(PRIV_RESTRMAILQ, PrivacyFlags)) 1004*58250Seric { 1005*58250Seric struct stat st; 1006*58250Seric 1007*58250Seric if (stat(QueueDir, &st) <= 0) 1008*58250Seric { 1009*58250Seric syserr("Cannot stat %s", QueueDir); 1010*58250Seric return; 1011*58250Seric } 1012*58250Seric if (getgid() != st.st_gid) 1013*58250Seric { 1014*58250Seric usrerr("510 You are not permitted to see the queue"); 1015*58250Seric setstat(EX_NOPERM); 1016*58250Seric return; 1017*58250Seric } 1018*58250Seric } 1019*58250Seric 1020*58250Seric /* 10219630Seric ** Read and order the queue. 10229630Seric */ 10239630Seric 102424941Seric nrequests = orderq(TRUE); 10259630Seric 10269630Seric /* 10279630Seric ** Print the work list that we have read. 10289630Seric */ 10299630Seric 10309630Seric /* first see if there is anything */ 103110070Seric if (nrequests <= 0) 10329630Seric { 103310070Seric printf("Mail queue is empty\n"); 10349630Seric return; 10359630Seric } 10369630Seric 103751920Seric CurrentLA = getla(); /* get load average */ 103840934Srick 103910096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 104025687Seric if (nrequests > QUEUESIZE) 104125687Seric printf(", only %d printed", QUEUESIZE); 104224979Seric if (Verbose) 104325032Seric printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 104424979Seric else 104524979Seric printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 10469630Seric for (w = WorkQ; w != NULL; w = w->w_next) 10479630Seric { 10489630Seric struct stat st; 104910070Seric auto time_t submittime = 0; 105010070Seric long dfsize = -1; 105110108Seric char message[MAXLINE]; 105251937Seric # ifdef LOCKF 105351937Seric struct flock lfd; 105451937Seric # endif 105524941Seric extern bool shouldqueue(); 10569630Seric 105717468Seric f = fopen(w->w_name, "r"); 105817468Seric if (f == NULL) 105917468Seric { 106017468Seric errno = 0; 106117468Seric continue; 106217468Seric } 10639630Seric printf("%7s", w->w_name + 2); 106451835Seric # ifdef LOCKF 106551937Seric lfd.l_type = F_RDLCK; 106651937Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 106751937Seric if (fcntl(fileno(f), F_GETLK, &lfd) < 0 || lfd.l_type != F_UNLCK) 106851835Seric # else 106940934Srick if (flock(fileno(f), LOCK_SH|LOCK_NB) < 0) 107051835Seric # endif 107110070Seric printf("*"); 107257438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 107324941Seric printf("X"); 107410070Seric else 107510070Seric printf(" "); 107610070Seric errno = 0; 107717468Seric 107810108Seric message[0] = '\0'; 10799630Seric while (fgets(buf, sizeof buf, f) != NULL) 10809630Seric { 108153400Seric register int i; 108253400Seric 10839630Seric fixcrlf(buf, TRUE); 10849630Seric switch (buf[0]) 10859630Seric { 108610108Seric case 'M': /* error message */ 108753400Seric if ((i = strlen(&buf[1])) >= sizeof message) 108853400Seric i = sizeof message; 108953400Seric bcopy(&buf[1], message, i); 109053400Seric message[i] = '\0'; 109110108Seric break; 109210108Seric 10939630Seric case 'S': /* sender name */ 109424979Seric if (Verbose) 109525027Seric printf("%8ld %10ld %.12s %.38s", dfsize, 109625027Seric w->w_pri, ctime(&submittime) + 4, 109724979Seric &buf[1]); 109824979Seric else 109924979Seric printf("%8ld %.16s %.45s", dfsize, 110024979Seric ctime(&submittime), &buf[1]); 110110108Seric if (message[0] != '\0') 110225027Seric printf("\n\t\t (%.60s)", message); 11039630Seric break; 110451920Seric 110540973Sbostic case 'C': /* controlling user */ 110654974Seric if (Verbose) 110754975Seric printf("\n\t\t\t\t (---%.34s---)", &buf[1]); 110840973Sbostic break; 11099630Seric 11109630Seric case 'R': /* recipient name */ 111124979Seric if (Verbose) 111225027Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 111324979Seric else 111424979Seric printf("\n\t\t\t\t %.45s", &buf[1]); 11159630Seric break; 11169630Seric 11179630Seric case 'T': /* creation time */ 111824941Seric submittime = atol(&buf[1]); 11199630Seric break; 112010070Seric 112110070Seric case 'D': /* data file name */ 112210070Seric if (stat(&buf[1], &st) >= 0) 112310070Seric dfsize = st.st_size; 112410070Seric break; 11259630Seric } 11269630Seric } 112710070Seric if (submittime == (time_t) 0) 112810070Seric printf(" (no control file)"); 11299630Seric printf("\n"); 113023098Seric (void) fclose(f); 11319630Seric } 11329630Seric } 11339630Seric 113456795Seric # endif /* QUEUE */ 113517468Seric /* 113617468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 113717468Seric ** 113817468Seric ** Assigns an id code if one does not already exist. 113917468Seric ** This code is very careful to avoid trashing existing files 114017468Seric ** under any circumstances. 114117468Seric ** 114217468Seric ** Parameters: 114317468Seric ** e -- envelope to build it in/from. 114417468Seric ** type -- the file type, used as the first character 114517468Seric ** of the file name. 114617468Seric ** 114717468Seric ** Returns: 114817468Seric ** a pointer to the new file name (in a static buffer). 114917468Seric ** 115017468Seric ** Side Effects: 115151920Seric ** If no id code is already assigned, queuename will 115251920Seric ** assign an id code, create a qf file, and leave a 115351920Seric ** locked, open-for-write file pointer in the envelope. 115417468Seric */ 115517468Seric 115617468Seric char * 115717468Seric queuename(e, type) 115817468Seric register ENVELOPE *e; 115917468Seric char type; 116017468Seric { 116117468Seric static char buf[MAXNAME]; 116217468Seric static int pid = -1; 116317468Seric char c1 = 'A'; 116417468Seric char c2 = 'A'; 116517468Seric 116617468Seric if (e->e_id == NULL) 116717468Seric { 116817468Seric char qf[20]; 116917468Seric 117017468Seric /* find a unique id */ 117117468Seric if (pid != getpid()) 117217468Seric { 117317468Seric /* new process -- start back at "AA" */ 117417468Seric pid = getpid(); 117517468Seric c1 = 'A'; 117617468Seric c2 = 'A' - 1; 117717468Seric } 117817468Seric (void) sprintf(qf, "qfAA%05d", pid); 117917468Seric 118017468Seric while (c1 < '~' || c2 < 'Z') 118117468Seric { 118217468Seric int i; 118351937Seric # ifdef LOCKF 118451937Seric struct flock lfd; 118551937Seric # endif 118617468Seric 118717468Seric if (c2 >= 'Z') 118817468Seric { 118917468Seric c1++; 119017468Seric c2 = 'A' - 1; 119117468Seric } 119240934Srick qf[2] = c1; 119340934Srick qf[3] = ++c2; 119417468Seric if (tTd(7, 20)) 119540934Srick printf("queuename: trying \"%s\"\n", qf); 119617468Seric 119740934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 119851920Seric if (i < 0) 119951920Seric { 120051920Seric if (errno == EEXIST) 120151920Seric continue; 120251920Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 120351920Seric qf, QueueDir); 120451920Seric exit(EX_UNAVAILABLE); 120551920Seric } 120651920Seric # ifdef LOCKF 120751937Seric lfd.l_type = F_WRLCK; 120851937Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 120951937Seric if (fcntl(i, F_SETLK, &lfd) >= 0) 121051920Seric # else 121151920Seric if (flock(i, LOCK_EX|LOCK_NB) >= 0) 121251920Seric # endif 121351920Seric { 121451920Seric e->e_lockfp = fdopen(i, "w"); 121540934Srick break; 121617468Seric } 121751920Seric 121851920Seric /* a reader got the file; abandon it and try again */ 121951920Seric (void) close(i); 122017468Seric } 122117468Seric if (c1 >= '~' && c2 >= 'Z') 122217468Seric { 122317468Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 122417468Seric qf, QueueDir); 122517468Seric exit(EX_OSERR); 122617468Seric } 122717468Seric e->e_id = newstr(&qf[2]); 122817468Seric define('i', e->e_id, e); 122917468Seric if (tTd(7, 1)) 123017468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 123117468Seric # ifdef LOG 123258020Seric if (LogLevel > 93) 123317468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 123456795Seric # endif /* LOG */ 123517468Seric } 123617468Seric 123717468Seric if (type == '\0') 123817468Seric return (NULL); 123917468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 124017468Seric if (tTd(7, 2)) 124117468Seric printf("queuename: %s\n", buf); 124217468Seric return (buf); 124317468Seric } 124417468Seric /* 124517468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 124617468Seric ** 124717468Seric ** Parameters: 124817468Seric ** e -- the envelope to unlock. 124917468Seric ** 125017468Seric ** Returns: 125117468Seric ** none 125217468Seric ** 125317468Seric ** Side Effects: 125417468Seric ** unlocks the queue for `e'. 125517468Seric */ 125617468Seric 125717468Seric unlockqueue(e) 125817468Seric ENVELOPE *e; 125917468Seric { 126051920Seric /* if there is a lock file in the envelope, close it */ 126151920Seric if (e->e_lockfp != NULL) 126251920Seric fclose(e->e_lockfp); 126351920Seric e->e_lockfp = NULL; 126451920Seric 126517468Seric /* remove the transcript */ 126617468Seric # ifdef LOG 126758020Seric if (LogLevel > 87) 126817468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 126956795Seric # endif /* LOG */ 127017468Seric if (!tTd(51, 4)) 127117468Seric xunlink(queuename(e, 'x')); 127217468Seric 127317468Seric } 127440973Sbostic /* 127554974Seric ** SETCTLUSER -- create a controlling address 127640973Sbostic ** 127754974Seric ** Create a fake "address" given only a local login name; this is 127854974Seric ** used as a "controlling user" for future recipient addresses. 127940973Sbostic ** 128040973Sbostic ** Parameters: 128154974Seric ** user -- the user name of the controlling user. 128240973Sbostic ** 128340973Sbostic ** Returns: 128454974Seric ** An address descriptor for the controlling user. 128540973Sbostic ** 128640973Sbostic ** Side Effects: 128740973Sbostic ** none. 128840973Sbostic */ 128940973Sbostic 129054974Seric ADDRESS * 129154974Seric setctluser(user) 129254974Seric char *user; 129340973Sbostic { 129454974Seric register ADDRESS *a; 129540973Sbostic struct passwd *pw; 129640973Sbostic 129740973Sbostic /* 129854974Seric ** See if this clears our concept of controlling user. 129940973Sbostic */ 130040973Sbostic 130154974Seric if (user == NULL || *user == '\0') 130254974Seric return NULL; 130340973Sbostic 130440973Sbostic /* 130554974Seric ** Set up addr fields for controlling user. 130640973Sbostic */ 130740973Sbostic 130854974Seric a = (ADDRESS *) xalloc(sizeof *a); 130954974Seric bzero((char *) a, sizeof *a); 131054974Seric if ((pw = getpwnam(user)) != NULL) 131140973Sbostic { 131240973Sbostic a->q_home = newstr(pw->pw_dir); 131340973Sbostic a->q_uid = pw->pw_uid; 131440973Sbostic a->q_gid = pw->pw_gid; 131557642Seric a->q_user = newstr(user); 131640973Sbostic } 131740973Sbostic else 131840973Sbostic { 131940973Sbostic a->q_uid = DefUid; 132040973Sbostic a->q_gid = DefGid; 132157642Seric a->q_user = newstr(DefUser); 132240973Sbostic } 132340973Sbostic 132440973Sbostic a->q_flags |= QGOODUID; /* flag as a "ctladdr" */ 132556328Seric a->q_mailer = LocalMailer; 132654974Seric return a; 132740973Sbostic } 1328