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*54967Seric static char sccsid[] = "@(#)queue.c 5.40 (Berkeley) 07/11/92 (with queueing)"; 1433731Sbostic #else 15*54967Seric static char sccsid[] = "@(#)queue.c 5.40 (Berkeley) 07/11/92 (without queueing)"; 1633731Sbostic #endif 1733731Sbostic #endif /* not lint */ 1833731Sbostic 194632Seric # include <sys/stat.h> 2013707Ssam # include <sys/dir.h> 2140934Srick # include <sys/file.h> 224634Seric # include <signal.h> 234632Seric # include <errno.h> 2440973Sbostic # include <pwd.h> 2551937Seric # ifdef LOCKF 2651937Seric # include <fcntl.h> 2751937Seric # endif 284632Seric 2933731Sbostic # ifdef QUEUE 304632Seric 314632Seric /* 329377Seric ** Work queue. 339377Seric */ 349377Seric 359377Seric struct work 369377Seric { 379377Seric char *w_name; /* name of control file */ 389377Seric long w_pri; /* priority of message, see below */ 3925013Seric time_t w_ctime; /* creation time of message */ 409377Seric struct work *w_next; /* next in queue */ 419377Seric }; 429377Seric 439377Seric typedef struct work WORK; 449377Seric 459377Seric WORK *WorkQ; /* queue of things to be done */ 469377Seric /* 474632Seric ** QUEUEUP -- queue a message up for future transmission. 484632Seric ** 494632Seric ** Parameters: 506980Seric ** e -- the envelope to queue up. 516999Seric ** queueall -- if TRUE, queue all addresses, rather than 526999Seric ** just those with the QQUEUEUP flag set. 539377Seric ** announce -- if TRUE, tell when you are queueing up. 544632Seric ** 554632Seric ** Returns: 5651920Seric ** none. 574632Seric ** 584632Seric ** Side Effects: 599377Seric ** The current request are saved in a control file. 6051920Seric ** The queue file is left locked. 614632Seric */ 624632Seric 639377Seric queueup(e, queueall, announce) 646980Seric register ENVELOPE *e; 656999Seric bool queueall; 669377Seric bool announce; 674632Seric { 687812Seric char *qf; 697812Seric register FILE *tfp; 704632Seric register HDR *h; 715007Seric register ADDRESS *q; 7251920Seric int fd; 7351920Seric int i; 7451920Seric bool newid; 7553400Seric register char *p; 7610173Seric MAILER nullmailer; 7751920Seric char buf[MAXLINE], tf[MAXLINE]; 7853400Seric extern char *macvalue(); 794632Seric 805037Seric /* 8117477Seric ** Create control file. 825037Seric */ 834632Seric 8451920Seric newid = (e->e_id == NULL); 8551920Seric strcpy(tf, queuename(e, 't')); 8651920Seric tfp = e->e_lockfp; 8751920Seric if (tfp == NULL) 8851920Seric newid = FALSE; 8951920Seric if (newid) 9051835Seric { 9151920Seric tfp = e->e_lockfp; 9251920Seric } 9351920Seric else 9451920Seric { 9551920Seric /* get a locked tf file */ 9651920Seric for (i = 100; --i >= 0; ) 9751835Seric { 9851937Seric # ifdef LOCKF 9951937Seric struct flock lfd; 10051937Seric # endif 10151937Seric 10251920Seric fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode); 10351920Seric if (fd < 0) 10451835Seric { 10551920Seric if (errno == EEXIST) 10651920Seric continue; 10751920Seric syserr("queueup: cannot create temp file %s", tf); 10851920Seric return; 10941636Srick } 11051835Seric # ifdef LOCKF 11151937Seric lfd.l_type = F_WRLCK; 11251937Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 11351937Seric if (fcntl(fd, F_SETLK, &lfd) >= 0) 11451920Seric break; 11551920Seric if (errno != EACCES && errno != EAGAIN) 11651920Seric syserr("cannot lockf(%s)", tf); 11751835Seric # else 11851920Seric if (flock(fd, LOCK_EX|LOCK_NB) >= 0) 11951920Seric break; 12051920Seric if (errno != EWOULDBLOCK) 12151920Seric syserr("cannot flock(%s)", tf); 12251835Seric # endif 12351920Seric close(fd); 12441636Srick } 12541636Srick 12651920Seric tfp = fdopen(fd, "w"); 12751920Seric } 1284632Seric 1297677Seric if (tTd(40, 1)) 13017468Seric printf("queueing %s\n", e->e_id); 1314632Seric 1324632Seric /* 1336980Seric ** If there is no data file yet, create one. 1346980Seric */ 1356980Seric 1366980Seric if (e->e_df == NULL) 1376980Seric { 1386980Seric register FILE *dfp; 1399389Seric extern putbody(); 1406980Seric 1417812Seric e->e_df = newstr(queuename(e, 'd')); 14240934Srick fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode); 14340934Srick if (fd < 0) 1446980Seric { 1456980Seric syserr("queueup: cannot create %s", e->e_df); 14651920Seric if (!newid) 14751920Seric (void) fclose(tfp); 14851920Seric return; 1496980Seric } 15040934Srick dfp = fdopen(fd, "w"); 15110173Seric (*e->e_putbody)(dfp, ProgMailer, e); 1527009Seric (void) fclose(dfp); 1539389Seric e->e_putbody = putbody; 1546980Seric } 1556980Seric 1566980Seric /* 1574632Seric ** Output future work requests. 15825687Seric ** Priority and creation time should be first, since 15925687Seric ** they are required by orderq. 1604632Seric */ 1614632Seric 1629377Seric /* output message priority */ 1639377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 1649377Seric 1659630Seric /* output creation time */ 1669630Seric fprintf(tfp, "T%ld\n", e->e_ctime); 1679630Seric 1684632Seric /* output name of data file */ 1697812Seric fprintf(tfp, "D%s\n", e->e_df); 1704632Seric 17110108Seric /* message from envelope, if it exists */ 17210108Seric if (e->e_message != NULL) 17310108Seric fprintf(tfp, "M%s\n", e->e_message); 17410108Seric 17553400Seric /* $r and $s macro values */ 17653400Seric if ((p = macvalue('r', e)) != NULL) 17753400Seric fprintf(tfp, "$r%s\n", p); 17853400Seric if ((p = macvalue('s', e)) != NULL) 17953400Seric fprintf(tfp, "$s%s\n", p); 18053400Seric 1814632Seric /* output name of sender */ 1827812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 1834632Seric 1844632Seric /* output list of recipient addresses */ 1856980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1864632Seric { 18747284Seric if (queueall ? !bitset(QDONTSEND|QSENT, q->q_flags) : 1887763Seric bitset(QQUEUEUP, q->q_flags)) 1898245Seric { 19040973Sbostic char *ctluser, *getctluser(); 19140973Sbostic 19240973Sbostic if ((ctluser = getctluser(q)) != NULL) 19340973Sbostic fprintf(tfp, "C%s\n", ctluser); 1947812Seric fprintf(tfp, "R%s\n", q->q_paddr); 1959377Seric if (announce) 1969377Seric { 1979377Seric e->e_to = q->q_paddr; 1989377Seric message(Arpa_Info, "queued"); 1999377Seric if (LogLevel > 4) 200*54967Seric logdelivery("queued", e); 2019377Seric e->e_to = NULL; 2029377Seric } 2039387Seric if (tTd(40, 1)) 2049387Seric { 2059387Seric printf("queueing "); 2069387Seric printaddr(q, FALSE); 2079387Seric } 2088245Seric } 2094632Seric } 2104632Seric 21125687Seric /* output list of error recipients */ 21225687Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 21325687Seric { 21426504Seric if (!bitset(QDONTSEND, q->q_flags)) 21540973Sbostic { 21640973Sbostic char *ctluser, *getctluser(); 21740973Sbostic 21840973Sbostic if ((ctluser = getctluser(q)) != NULL) 21940973Sbostic fprintf(tfp, "C%s\n", ctluser); 22026504Seric fprintf(tfp, "E%s\n", q->q_paddr); 22140973Sbostic } 22225687Seric } 22325687Seric 2249377Seric /* 2259377Seric ** Output headers for this message. 2269377Seric ** Expand macros completely here. Queue run will deal with 2279377Seric ** everything as absolute headers. 2289377Seric ** All headers that must be relative to the recipient 2299377Seric ** can be cracked later. 23010173Seric ** We set up a "null mailer" -- i.e., a mailer that will have 23110173Seric ** no effect on the addresses as they are output. 2329377Seric */ 2339377Seric 23410686Seric bzero((char *) &nullmailer, sizeof nullmailer); 23510173Seric nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1; 23610349Seric nullmailer.m_eol = "\n"; 23710173Seric 23816147Seric define('g', "\001f", e); 2396980Seric for (h = e->e_header; h != NULL; h = h->h_link) 2404632Seric { 24110686Seric extern bool bitzerop(); 24210686Seric 24312015Seric /* don't output null headers */ 2444632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 2454632Seric continue; 24612015Seric 24712015Seric /* don't output resent headers on non-resent messages */ 24812015Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 24912015Seric continue; 25012015Seric 25112015Seric /* output this header */ 2527812Seric fprintf(tfp, "H"); 25312015Seric 25412015Seric /* if conditional, output the set of conditions */ 25510686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 25610686Seric { 25710686Seric int j; 25810686Seric 25923098Seric (void) putc('?', tfp); 26010686Seric for (j = '\0'; j <= '\177'; j++) 26110686Seric if (bitnset(j, h->h_mflags)) 26223098Seric (void) putc(j, tfp); 26323098Seric (void) putc('?', tfp); 26410686Seric } 26512015Seric 26612015Seric /* output the header: expand macros, convert addresses */ 2677763Seric if (bitset(H_DEFAULT, h->h_flags)) 2687763Seric { 2697763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 2708236Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 2717763Seric } 2728245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 2739348Seric { 2749348Seric commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags), 27510173Seric &nullmailer); 2769348Seric } 2777763Seric else 2788245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 2794632Seric } 2804632Seric 2814632Seric /* 2824632Seric ** Clean up. 2834632Seric */ 2844632Seric 28551920Seric if (!newid) 28651920Seric { 28751920Seric qf = queuename(e, 'q'); 28851920Seric if (rename(tf, qf) < 0) 28951920Seric syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df); 29051920Seric if (e->e_lockfp != NULL) 29151920Seric (void) fclose(e->e_lockfp); 29251920Seric e->e_lockfp = tfp; 29351920Seric } 29451920Seric else 29551920Seric qf = tf; 29641636Srick errno = 0; 2977391Seric 2987677Seric # ifdef LOG 2997677Seric /* save log info */ 3007878Seric if (LogLevel > 15) 3017878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 3027677Seric # endif LOG 30340934Srick fflush(tfp); 30451920Seric return; 3054632Seric } 3064632Seric /* 3074632Seric ** RUNQUEUE -- run the jobs in the queue. 3084632Seric ** 3094632Seric ** Gets the stuff out of the queue in some presumably logical 3104632Seric ** order and processes them. 3114632Seric ** 3124632Seric ** Parameters: 31324941Seric ** forkflag -- TRUE if the queue scanning should be done in 31424941Seric ** a child process. We double-fork so it is not our 31524941Seric ** child and we don't have to clean up after it. 3164632Seric ** 3174632Seric ** Returns: 3184632Seric ** none. 3194632Seric ** 3204632Seric ** Side Effects: 3214632Seric ** runs things in the mail queue. 3224632Seric */ 3234632Seric 3244639Seric runqueue(forkflag) 3254639Seric bool forkflag; 3264632Seric { 32724953Seric extern bool shouldqueue(); 32824953Seric 3297466Seric /* 33024953Seric ** If no work will ever be selected, don't even bother reading 33124953Seric ** the queue. 33224953Seric */ 33324953Seric 33451920Seric CurrentLA = getla(); /* get load average */ 33540934Srick 33624953Seric if (shouldqueue(-100000000L)) 33724953Seric { 33824953Seric if (Verbose) 33924953Seric printf("Skipping queue run -- load average too high\n"); 34024953Seric 34124953Seric if (forkflag) 34224953Seric return; 34324953Seric finis(); 34424953Seric } 34524953Seric 34624953Seric /* 3477466Seric ** See if we want to go off and do other useful work. 3487466Seric */ 3494639Seric 3504639Seric if (forkflag) 3514639Seric { 3527943Seric int pid; 3537943Seric 3547943Seric pid = dofork(); 3557943Seric if (pid != 0) 3564639Seric { 35746928Sbostic extern void reapchild(); 35825184Seric 3597943Seric /* parent -- pick up intermediate zombie */ 36025184Seric #ifndef SIGCHLD 3619377Seric (void) waitfor(pid); 36225184Seric #else SIGCHLD 36325184Seric (void) signal(SIGCHLD, reapchild); 36425184Seric #endif SIGCHLD 3657690Seric if (QueueIntvl != 0) 3669348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 3674639Seric return; 3684639Seric } 3697943Seric /* child -- double fork */ 37025184Seric #ifndef SIGCHLD 3717943Seric if (fork() != 0) 3727943Seric exit(EX_OK); 37325184Seric #else SIGCHLD 37425184Seric (void) signal(SIGCHLD, SIG_DFL); 37525184Seric #endif SIGCHLD 3764639Seric } 37724941Seric 37840934Srick setproctitle("running queue: %s", QueueDir); 37924941Seric 3807876Seric # ifdef LOG 3817876Seric if (LogLevel > 11) 3827943Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid()); 3837876Seric # endif LOG 3844639Seric 3857466Seric /* 38610205Seric ** Release any resources used by the daemon code. 38710205Seric */ 38810205Seric 38910205Seric # ifdef DAEMON 39010205Seric clrdaemon(); 39110205Seric # endif DAEMON 39210205Seric 39310205Seric /* 39427175Seric ** Make sure the alias database is open. 39527175Seric */ 39627175Seric 39727175Seric initaliases(AliasFile, FALSE); 39827175Seric 39927175Seric /* 4007466Seric ** Start making passes through the queue. 4017466Seric ** First, read and sort the entire queue. 4027466Seric ** Then, process the work in that order. 4037466Seric ** But if you take too long, start over. 4047466Seric */ 4057466Seric 4067943Seric /* order the existing work requests */ 40724954Seric (void) orderq(FALSE); 4087690Seric 4097943Seric /* process them once at a time */ 4107943Seric while (WorkQ != NULL) 4114639Seric { 4127943Seric WORK *w = WorkQ; 4137881Seric 4147943Seric WorkQ = WorkQ->w_next; 4157943Seric dowork(w); 4167943Seric free(w->w_name); 4177943Seric free((char *) w); 4184639Seric } 41929866Seric 42029866Seric /* exit without the usual cleanup */ 42129866Seric exit(ExitStat); 4224634Seric } 4234634Seric /* 4244632Seric ** ORDERQ -- order the work queue. 4254632Seric ** 4264632Seric ** Parameters: 42724941Seric ** doall -- if set, include everything in the queue (even 42824941Seric ** the jobs that cannot be run because the load 42924941Seric ** average is too high). Otherwise, exclude those 43024941Seric ** jobs. 4314632Seric ** 4324632Seric ** Returns: 43310121Seric ** The number of request in the queue (not necessarily 43410121Seric ** the number of requests in WorkQ however). 4354632Seric ** 4364632Seric ** Side Effects: 4374632Seric ** Sets WorkQ to the queue of available work, in order. 4384632Seric */ 4394632Seric 44025687Seric # define NEED_P 001 44125687Seric # define NEED_T 002 4424632Seric 44324941Seric orderq(doall) 44424941Seric bool doall; 4454632Seric { 4466625Sglickman register struct direct *d; 4474632Seric register WORK *w; 4486625Sglickman DIR *f; 4494632Seric register int i; 45025687Seric WORK wlist[QUEUESIZE+1]; 45110070Seric int wn = -1; 4524632Seric extern workcmpf(); 4534632Seric 4544632Seric /* clear out old WorkQ */ 4554632Seric for (w = WorkQ; w != NULL; ) 4564632Seric { 4574632Seric register WORK *nw = w->w_next; 4584632Seric 4594632Seric WorkQ = nw; 4604632Seric free(w->w_name); 4614632Seric free((char *) w); 4624632Seric w = nw; 4634632Seric } 4644632Seric 4654632Seric /* open the queue directory */ 4668148Seric f = opendir("."); 4674632Seric if (f == NULL) 4684632Seric { 4698148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 47010070Seric return (0); 4714632Seric } 4724632Seric 4734632Seric /* 4744632Seric ** Read the work directory. 4754632Seric */ 4764632Seric 47710070Seric while ((d = readdir(f)) != NULL) 4784632Seric { 4799377Seric FILE *cf; 4804632Seric char lbuf[MAXNAME]; 4814632Seric 4824632Seric /* is this an interesting entry? */ 4837812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 4844632Seric continue; 4854632Seric 48610070Seric /* yes -- open control file (if not too many files) */ 48725687Seric if (++wn >= QUEUESIZE) 48810070Seric continue; 4898148Seric cf = fopen(d->d_name, "r"); 4904632Seric if (cf == NULL) 4914632Seric { 4927055Seric /* this may be some random person sending hir msgs */ 4937055Seric /* syserr("orderq: cannot open %s", cbuf); */ 49410090Seric if (tTd(41, 2)) 49510090Seric printf("orderq: cannot open %s (%d)\n", 49610090Seric d->d_name, errno); 4977055Seric errno = 0; 49810090Seric wn--; 4994632Seric continue; 5004632Seric } 50125687Seric w = &wlist[wn]; 50225687Seric w->w_name = newstr(d->d_name); 5034632Seric 50425027Seric /* make sure jobs in creation don't clog queue */ 50525687Seric w->w_pri = 0x7fffffff; 50625687Seric w->w_ctime = 0; 50725027Seric 5084632Seric /* extract useful information */ 50925687Seric i = NEED_P | NEED_T; 51025687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 5114632Seric { 51224954Seric extern long atol(); 51324954Seric 51424941Seric switch (lbuf[0]) 5154632Seric { 51624941Seric case 'P': 51725687Seric w->w_pri = atol(&lbuf[1]); 51825687Seric i &= ~NEED_P; 5194632Seric break; 52025013Seric 52125013Seric case 'T': 52225687Seric w->w_ctime = atol(&lbuf[1]); 52325687Seric i &= ~NEED_T; 52425013Seric break; 5254632Seric } 5264632Seric } 5274632Seric (void) fclose(cf); 52824953Seric 52925687Seric if (!doall && shouldqueue(w->w_pri)) 53024953Seric { 53124953Seric /* don't even bother sorting this job in */ 53224953Seric wn--; 53324953Seric } 5344632Seric } 5356625Sglickman (void) closedir(f); 53610090Seric wn++; 5374632Seric 5384632Seric /* 5394632Seric ** Sort the work directory. 5404632Seric */ 5414632Seric 54225687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 5434632Seric 5444632Seric /* 5454632Seric ** Convert the work list into canonical form. 5469377Seric ** Should be turning it into a list of envelopes here perhaps. 5474632Seric */ 5484632Seric 54924981Seric WorkQ = NULL; 55025687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 5514632Seric { 5524632Seric w = (WORK *) xalloc(sizeof *w); 5534632Seric w->w_name = wlist[i].w_name; 5544632Seric w->w_pri = wlist[i].w_pri; 55525013Seric w->w_ctime = wlist[i].w_ctime; 55624981Seric w->w_next = WorkQ; 55724981Seric WorkQ = w; 5584632Seric } 5594632Seric 5607677Seric if (tTd(40, 1)) 5614632Seric { 5624632Seric for (w = WorkQ; w != NULL; w = w->w_next) 5635037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 5644632Seric } 56510070Seric 56610090Seric return (wn); 5674632Seric } 5684632Seric /* 5697677Seric ** WORKCMPF -- compare function for ordering work. 5704632Seric ** 5714632Seric ** Parameters: 5724632Seric ** a -- the first argument. 5734632Seric ** b -- the second argument. 5744632Seric ** 5754632Seric ** Returns: 57624981Seric ** -1 if a < b 57724981Seric ** 0 if a == b 57824981Seric ** +1 if a > b 5794632Seric ** 5804632Seric ** Side Effects: 5814632Seric ** none. 5824632Seric */ 5834632Seric 5844632Seric workcmpf(a, b) 5855037Seric register WORK *a; 5865037Seric register WORK *b; 5874632Seric { 58825013Seric long pa = a->w_pri + a->w_ctime; 58925013Seric long pb = b->w_pri + b->w_ctime; 59024941Seric 59124941Seric if (pa == pb) 5924632Seric return (0); 59324941Seric else if (pa > pb) 59424981Seric return (1); 59524981Seric else 59610121Seric return (-1); 5974632Seric } 5984632Seric /* 5994632Seric ** DOWORK -- do a work request. 6004632Seric ** 6014632Seric ** Parameters: 6024632Seric ** w -- the work request to be satisfied. 6034632Seric ** 6044632Seric ** Returns: 6054632Seric ** none. 6064632Seric ** 6074632Seric ** Side Effects: 6084632Seric ** The work request is satisfied if possible. 6094632Seric */ 6104632Seric 6114632Seric dowork(w) 6124632Seric register WORK *w; 6134632Seric { 6144632Seric register int i; 61524941Seric extern bool shouldqueue(); 61651920Seric extern bool readqf(); 6174632Seric 6187677Seric if (tTd(40, 1)) 6195037Seric printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 6204632Seric 6214632Seric /* 62224941Seric ** Ignore jobs that are too expensive for the moment. 6234632Seric */ 6244632Seric 62524941Seric if (shouldqueue(w->w_pri)) 6264632Seric { 62724941Seric if (Verbose) 62824967Seric printf("\nSkipping %s\n", w->w_name + 2); 6294632Seric return; 6304632Seric } 6314632Seric 63224941Seric /* 63324941Seric ** Fork for work. 63424941Seric */ 63524941Seric 63624941Seric if (ForkQueueRuns) 63724941Seric { 63824941Seric i = fork(); 63924941Seric if (i < 0) 64024941Seric { 64124941Seric syserr("dowork: cannot fork"); 64224941Seric return; 64324941Seric } 64424941Seric } 64524941Seric else 64624941Seric { 64724941Seric i = 0; 64824941Seric } 64924941Seric 6504632Seric if (i == 0) 6514632Seric { 6524632Seric /* 6534632Seric ** CHILD 6548148Seric ** Lock the control file to avoid duplicate deliveries. 6558148Seric ** Then run the file as though we had just read it. 6567350Seric ** We save an idea of the temporary name so we 6577350Seric ** can recover on interrupt. 6584632Seric */ 6594632Seric 6607763Seric /* set basic modes, etc. */ 6617356Seric (void) alarm(0); 66225612Seric clearenvelope(CurEnv, FALSE); 6634632Seric QueueRun = TRUE; 6649377Seric ErrorMode = EM_MAIL; 6658148Seric CurEnv->e_id = &w->w_name[2]; 6667876Seric # ifdef LOG 6677876Seric if (LogLevel > 11) 6687881Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id, 6697881Seric getpid()); 6707876Seric # endif LOG 6717763Seric 6727763Seric /* don't use the headers from sendmail.cf... */ 6737763Seric CurEnv->e_header = NULL; 6747763Seric 67551920Seric /* read the queue control file -- return if locked */ 67651920Seric if (!readqf(CurEnv)) 6776980Seric { 67824941Seric if (ForkQueueRuns) 67924941Seric exit(EX_OK); 68024941Seric else 68124941Seric return; 6826980Seric } 6836980Seric 6849338Seric CurEnv->e_flags |= EF_INQUEUE; 6859377Seric eatheader(CurEnv); 6866980Seric 6876980Seric /* do the delivery */ 6889338Seric if (!bitset(EF_FATALERRS, CurEnv->e_flags)) 6899282Seric sendall(CurEnv, SM_DELIVER); 6906980Seric 6916980Seric /* finish up and exit */ 69224941Seric if (ForkQueueRuns) 69324941Seric finis(); 69424941Seric else 69524941Seric dropenvelope(CurEnv); 6964632Seric } 69724941Seric else 69824941Seric { 69924941Seric /* 70024941Seric ** Parent -- pick up results. 70124941Seric */ 7024632Seric 70324941Seric errno = 0; 70424941Seric (void) waitfor(i); 70524941Seric } 7064632Seric } 7074632Seric /* 7084632Seric ** READQF -- read queue file and set up environment. 7094632Seric ** 7104632Seric ** Parameters: 7119377Seric ** e -- the envelope of the job to run. 7124632Seric ** 7134632Seric ** Returns: 71451920Seric ** TRUE if it successfully read the queue file. 71551920Seric ** FALSE otherwise. 7164632Seric ** 7174632Seric ** Side Effects: 71851920Seric ** The queue file is returned locked. 7194632Seric */ 7204632Seric 72151920Seric bool 72251920Seric readqf(e) 7239377Seric register ENVELOPE *e; 7244632Seric { 72517477Seric char *qf; 72617477Seric register FILE *qfp; 7277785Seric char buf[MAXFIELD]; 7289348Seric extern char *fgetfolded(); 72924954Seric extern long atol(); 73040973Sbostic int gotctluser = 0; 73140934Srick int fd; 73251937Seric # ifdef LOCKF 73351937Seric struct flock lfd; 73451937Seric # endif 7354632Seric 7364632Seric /* 73717468Seric ** Read and process the file. 7384632Seric */ 7394632Seric 74017477Seric qf = queuename(e, 'q'); 74151937Seric qfp = fopen(qf, "r+"); 74217477Seric if (qfp == NULL) 74317477Seric { 74440934Srick if (errno != ENOENT) 74540934Srick syserr("readqf: no control file %s", qf); 74651920Seric return FALSE; 74717477Seric } 74840934Srick 74951835Seric # ifdef LOCKF 75051937Seric lfd.l_type = F_WRLCK; 75151937Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 75251937Seric if (fcntl(fileno(qfp), F_SETLK, &lfd) < 0) 75351835Seric # else 75440934Srick if (flock(fileno(qfp), LOCK_EX|LOCK_NB) < 0) 75551835Seric # endif 75640934Srick { 75740934Srick /* being processed by another queuer */ 75840934Srick if (Verbose) 75941636Srick printf("%s: locked\n", CurEnv->e_id); 76051920Seric # ifdef LOG 76151920Seric if (LogLevel > 9) 76251920Seric syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id); 76340934Srick # endif LOG 76440934Srick (void) fclose(qfp); 76551920Seric return FALSE; 76640934Srick } 76740934Srick 76851920Seric /* save this lock */ 76951920Seric e->e_lockfp = qfp; 77051920Seric 77140934Srick /* do basic system initialization */ 77240934Srick initsys(); 77340934Srick 77417477Seric FileName = qf; 7759377Seric LineNumber = 0; 77651920Seric if (Verbose) 7779377Seric printf("\nRunning %s\n", e->e_id); 77817468Seric while (fgetfolded(buf, sizeof buf, qfp) != NULL) 7794632Seric { 78026504Seric if (tTd(40, 4)) 78126504Seric printf("+++++ %s\n", buf); 7824632Seric switch (buf[0]) 7834632Seric { 78440973Sbostic case 'C': /* specify controlling user */ 78540973Sbostic setctluser(&buf[1]); 78640973Sbostic gotctluser = 1; 78740973Sbostic break; 78840973Sbostic 7894632Seric case 'R': /* specify recipient */ 7909618Seric sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue); 7914632Seric break; 7924632Seric 79325687Seric case 'E': /* specify error recipient */ 79425687Seric sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_errorqueue); 79525687Seric break; 79625687Seric 7974632Seric case 'H': /* header */ 79851920Seric (void) chompheader(&buf[1], FALSE); 7994632Seric break; 8004632Seric 80110108Seric case 'M': /* message */ 80210108Seric e->e_message = newstr(&buf[1]); 80310108Seric break; 80410108Seric 8054632Seric case 'S': /* sender */ 80653182Seric setsender(newstr(&buf[1]), CurEnv); 8074632Seric break; 8084632Seric 8094632Seric case 'D': /* data file name */ 8109377Seric e->e_df = newstr(&buf[1]); 8119544Seric e->e_dfp = fopen(e->e_df, "r"); 8129544Seric if (e->e_dfp == NULL) 8139377Seric syserr("readqf: cannot open %s", e->e_df); 8144632Seric break; 8154632Seric 8167860Seric case 'T': /* init time */ 81724941Seric e->e_ctime = atol(&buf[1]); 8184632Seric break; 8194632Seric 8204634Seric case 'P': /* message priority */ 82125008Seric e->e_msgpriority = atol(&buf[1]) + WkTimeFact; 8224634Seric break; 8234634Seric 82453400Seric case '$': /* define macro */ 82553400Seric define(buf[1], newstr(&buf[2]), e); 82653400Seric break; 82753400Seric 82824941Seric case '\0': /* blank line; ignore */ 82924941Seric break; 83024941Seric 8314632Seric default: 83224941Seric syserr("readqf(%s:%d): bad line \"%s\"", e->e_id, 83324941Seric LineNumber, buf); 8344632Seric break; 8354632Seric } 83640973Sbostic /* 83740973Sbostic ** The `C' queue file command operates on the next line, 83840973Sbostic ** so we use "gotctluser" to maintain state as follows: 83940973Sbostic ** 0 - no controlling user, 84040973Sbostic ** 1 - controlling user has been set but not used, 84140973Sbostic ** 2 - controlling user must be used on next iteration. 84240973Sbostic */ 84340973Sbostic if (gotctluser == 1) 84440973Sbostic gotctluser++; 84540973Sbostic else if (gotctluser == 2) 84640973Sbostic { 84740973Sbostic clrctluser(); 84840973Sbostic gotctluser = 0; 84940973Sbostic } 8504632Seric } 8519377Seric 85240973Sbostic /* clear controlling user in case we break out prematurely */ 85340973Sbostic clrctluser(); 85440973Sbostic 8559377Seric FileName = NULL; 85624941Seric 85724941Seric /* 85824941Seric ** If we haven't read any lines, this queue file is empty. 85924941Seric ** Arrange to remove it without referencing any null pointers. 86024941Seric */ 86124941Seric 86224941Seric if (LineNumber == 0) 86324941Seric { 86424941Seric errno = 0; 86524941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 86624941Seric } 86751920Seric return TRUE; 8684632Seric } 8694632Seric /* 8709630Seric ** PRINTQUEUE -- print out a representation of the mail queue 8719630Seric ** 8729630Seric ** Parameters: 8739630Seric ** none. 8749630Seric ** 8759630Seric ** Returns: 8769630Seric ** none. 8779630Seric ** 8789630Seric ** Side Effects: 8799630Seric ** Prints a listing of the mail queue on the standard output. 8809630Seric */ 8815182Seric 8829630Seric printqueue() 8839630Seric { 8849630Seric register WORK *w; 8859630Seric FILE *f; 88610070Seric int nrequests; 8879630Seric char buf[MAXLINE]; 88840973Sbostic char cbuf[MAXLINE]; 8899630Seric 8909630Seric /* 8919630Seric ** Read and order the queue. 8929630Seric */ 8939630Seric 89424941Seric nrequests = orderq(TRUE); 8959630Seric 8969630Seric /* 8979630Seric ** Print the work list that we have read. 8989630Seric */ 8999630Seric 9009630Seric /* first see if there is anything */ 90110070Seric if (nrequests <= 0) 9029630Seric { 90310070Seric printf("Mail queue is empty\n"); 9049630Seric return; 9059630Seric } 9069630Seric 90751920Seric CurrentLA = getla(); /* get load average */ 90840934Srick 90910096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 91025687Seric if (nrequests > QUEUESIZE) 91125687Seric printf(", only %d printed", QUEUESIZE); 91224979Seric if (Verbose) 91325032Seric printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 91424979Seric else 91524979Seric printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 9169630Seric for (w = WorkQ; w != NULL; w = w->w_next) 9179630Seric { 9189630Seric struct stat st; 91910070Seric auto time_t submittime = 0; 92010070Seric long dfsize = -1; 92110108Seric char message[MAXLINE]; 92251937Seric # ifdef LOCKF 92351937Seric struct flock lfd; 92451937Seric # endif 92524941Seric extern bool shouldqueue(); 9269630Seric 92717468Seric f = fopen(w->w_name, "r"); 92817468Seric if (f == NULL) 92917468Seric { 93017468Seric errno = 0; 93117468Seric continue; 93217468Seric } 9339630Seric printf("%7s", w->w_name + 2); 93451835Seric # ifdef LOCKF 93551937Seric lfd.l_type = F_RDLCK; 93651937Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 93751937Seric if (fcntl(fileno(f), F_GETLK, &lfd) < 0 || lfd.l_type != F_UNLCK) 93851835Seric # else 93940934Srick if (flock(fileno(f), LOCK_SH|LOCK_NB) < 0) 94051835Seric # endif 94110070Seric printf("*"); 94224941Seric else if (shouldqueue(w->w_pri)) 94324941Seric printf("X"); 94410070Seric else 94510070Seric printf(" "); 94610070Seric errno = 0; 94717468Seric 94810108Seric message[0] = '\0'; 94940973Sbostic cbuf[0] = '\0'; 9509630Seric while (fgets(buf, sizeof buf, f) != NULL) 9519630Seric { 95253400Seric register int i; 95353400Seric 9549630Seric fixcrlf(buf, TRUE); 9559630Seric switch (buf[0]) 9569630Seric { 95710108Seric case 'M': /* error message */ 95853400Seric if ((i = strlen(&buf[1])) >= sizeof message) 95953400Seric i = sizeof message; 96053400Seric bcopy(&buf[1], message, i); 96153400Seric message[i] = '\0'; 96210108Seric break; 96310108Seric 9649630Seric case 'S': /* sender name */ 96524979Seric if (Verbose) 96625027Seric printf("%8ld %10ld %.12s %.38s", dfsize, 96725027Seric w->w_pri, ctime(&submittime) + 4, 96824979Seric &buf[1]); 96924979Seric else 97024979Seric printf("%8ld %.16s %.45s", dfsize, 97124979Seric ctime(&submittime), &buf[1]); 97210108Seric if (message[0] != '\0') 97325027Seric printf("\n\t\t (%.60s)", message); 9749630Seric break; 97551920Seric 97640973Sbostic case 'C': /* controlling user */ 97740973Sbostic if (strlen(buf) < MAXLINE-3) /* sanity */ 97840973Sbostic (void) strcat(buf, ") "); 97940973Sbostic cbuf[0] = cbuf[1] = '('; 98040973Sbostic (void) strncpy(&cbuf[2], &buf[1], MAXLINE-1); 98140973Sbostic cbuf[MAXLINE-1] = '\0'; 98240973Sbostic break; 9839630Seric 9849630Seric case 'R': /* recipient name */ 98540973Sbostic if (cbuf[0] != '\0') { 98640973Sbostic /* prepend controlling user to `buf' */ 98740973Sbostic (void) strncat(cbuf, &buf[1], 98840973Sbostic MAXLINE-strlen(cbuf)); 98940973Sbostic cbuf[MAXLINE-1] = '\0'; 99040973Sbostic (void) strcpy(buf, cbuf); 99140973Sbostic cbuf[0] = '\0'; 99240973Sbostic } 99324979Seric if (Verbose) 99425027Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 99524979Seric else 99624979Seric printf("\n\t\t\t\t %.45s", &buf[1]); 9979630Seric break; 9989630Seric 9999630Seric case 'T': /* creation time */ 100024941Seric submittime = atol(&buf[1]); 10019630Seric break; 100210070Seric 100310070Seric case 'D': /* data file name */ 100410070Seric if (stat(&buf[1], &st) >= 0) 100510070Seric dfsize = st.st_size; 100610070Seric break; 10079630Seric } 10089630Seric } 100910070Seric if (submittime == (time_t) 0) 101010070Seric printf(" (no control file)"); 10119630Seric printf("\n"); 101223098Seric (void) fclose(f); 10139630Seric } 10149630Seric } 10159630Seric 10165182Seric # endif QUEUE 101717468Seric /* 101817468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 101917468Seric ** 102017468Seric ** Assigns an id code if one does not already exist. 102117468Seric ** This code is very careful to avoid trashing existing files 102217468Seric ** under any circumstances. 102317468Seric ** 102417468Seric ** Parameters: 102517468Seric ** e -- envelope to build it in/from. 102617468Seric ** type -- the file type, used as the first character 102717468Seric ** of the file name. 102817468Seric ** 102917468Seric ** Returns: 103017468Seric ** a pointer to the new file name (in a static buffer). 103117468Seric ** 103217468Seric ** Side Effects: 103351920Seric ** If no id code is already assigned, queuename will 103451920Seric ** assign an id code, create a qf file, and leave a 103551920Seric ** locked, open-for-write file pointer in the envelope. 103617468Seric */ 103717468Seric 103817468Seric char * 103917468Seric queuename(e, type) 104017468Seric register ENVELOPE *e; 104117468Seric char type; 104217468Seric { 104317468Seric static char buf[MAXNAME]; 104417468Seric static int pid = -1; 104517468Seric char c1 = 'A'; 104617468Seric char c2 = 'A'; 104717468Seric 104817468Seric if (e->e_id == NULL) 104917468Seric { 105017468Seric char qf[20]; 105117468Seric 105217468Seric /* find a unique id */ 105317468Seric if (pid != getpid()) 105417468Seric { 105517468Seric /* new process -- start back at "AA" */ 105617468Seric pid = getpid(); 105717468Seric c1 = 'A'; 105817468Seric c2 = 'A' - 1; 105917468Seric } 106017468Seric (void) sprintf(qf, "qfAA%05d", pid); 106117468Seric 106217468Seric while (c1 < '~' || c2 < 'Z') 106317468Seric { 106417468Seric int i; 106551937Seric # ifdef LOCKF 106651937Seric struct flock lfd; 106751937Seric # endif 106817468Seric 106917468Seric if (c2 >= 'Z') 107017468Seric { 107117468Seric c1++; 107217468Seric c2 = 'A' - 1; 107317468Seric } 107440934Srick qf[2] = c1; 107540934Srick qf[3] = ++c2; 107617468Seric if (tTd(7, 20)) 107740934Srick printf("queuename: trying \"%s\"\n", qf); 107817468Seric 107940934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 108051920Seric if (i < 0) 108151920Seric { 108251920Seric if (errno == EEXIST) 108351920Seric continue; 108451920Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 108551920Seric qf, QueueDir); 108651920Seric exit(EX_UNAVAILABLE); 108751920Seric } 108851920Seric # ifdef LOCKF 108951937Seric lfd.l_type = F_WRLCK; 109051937Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 109151937Seric if (fcntl(i, F_SETLK, &lfd) >= 0) 109251920Seric # else 109351920Seric if (flock(i, LOCK_EX|LOCK_NB) >= 0) 109451920Seric # endif 109551920Seric { 109651920Seric e->e_lockfp = fdopen(i, "w"); 109740934Srick break; 109817468Seric } 109951920Seric 110051920Seric /* a reader got the file; abandon it and try again */ 110151920Seric (void) close(i); 110217468Seric } 110317468Seric if (c1 >= '~' && c2 >= 'Z') 110417468Seric { 110517468Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 110617468Seric qf, QueueDir); 110717468Seric exit(EX_OSERR); 110817468Seric } 110917468Seric e->e_id = newstr(&qf[2]); 111017468Seric define('i', e->e_id, e); 111117468Seric if (tTd(7, 1)) 111217468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 111317468Seric # ifdef LOG 111417468Seric if (LogLevel > 16) 111517468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 111617468Seric # endif LOG 111717468Seric } 111817468Seric 111917468Seric if (type == '\0') 112017468Seric return (NULL); 112117468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 112217468Seric if (tTd(7, 2)) 112317468Seric printf("queuename: %s\n", buf); 112417468Seric return (buf); 112517468Seric } 112617468Seric /* 112717468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 112817468Seric ** 112917468Seric ** Parameters: 113017468Seric ** e -- the envelope to unlock. 113117468Seric ** 113217468Seric ** Returns: 113317468Seric ** none 113417468Seric ** 113517468Seric ** Side Effects: 113617468Seric ** unlocks the queue for `e'. 113717468Seric */ 113817468Seric 113917468Seric unlockqueue(e) 114017468Seric ENVELOPE *e; 114117468Seric { 114251920Seric /* if there is a lock file in the envelope, close it */ 114351920Seric if (e->e_lockfp != NULL) 114451920Seric fclose(e->e_lockfp); 114551920Seric e->e_lockfp = NULL; 114651920Seric 114717468Seric /* remove the transcript */ 114817468Seric # ifdef LOG 114917468Seric if (LogLevel > 19) 115017468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 115117468Seric # endif LOG 115217468Seric if (!tTd(51, 4)) 115317468Seric xunlink(queuename(e, 'x')); 115417468Seric 115517468Seric } 115640973Sbostic /* 115740973Sbostic ** GETCTLUSER -- return controlling user if mailing to prog or file 115840973Sbostic ** 115940973Sbostic ** Check for a "|" or "/" at the beginning of the address. If 116040973Sbostic ** found, return a controlling username. 116140973Sbostic ** 116240973Sbostic ** Parameters: 116340973Sbostic ** a - the address to check out 116440973Sbostic ** 116540973Sbostic ** Returns: 116640973Sbostic ** Either NULL, if we werent mailing to a program or file, 116740973Sbostic ** or a controlling user name (possibly in getpwuid's 116840973Sbostic ** static buffer). 116940973Sbostic ** 117040973Sbostic ** Side Effects: 117140973Sbostic ** none. 117240973Sbostic */ 117340973Sbostic 117440973Sbostic char * 117540973Sbostic getctluser(a) 117640973Sbostic ADDRESS *a; 117740973Sbostic { 117840973Sbostic extern ADDRESS *getctladdr(); 117940973Sbostic struct passwd *pw; 118040973Sbostic char *retstr; 118140973Sbostic 118240973Sbostic /* 118340973Sbostic ** Get unquoted user for file, program or user.name check. 118440973Sbostic ** N.B. remove this code block to always emit controlling 118540973Sbostic ** addresses (at the expense of backward compatibility). 118640973Sbostic */ 118740973Sbostic 118840973Sbostic { 118940973Sbostic char buf[MAXNAME]; 119040973Sbostic (void) strncpy(buf, a->q_paddr, MAXNAME); 119140973Sbostic buf[MAXNAME-1] = '\0'; 119240973Sbostic stripquotes(buf, TRUE); 119340973Sbostic 119440973Sbostic if (buf[0] != '|' && buf[0] != '/') 119540973Sbostic return((char *)NULL); 119640973Sbostic } 119740973Sbostic 119840973Sbostic a = getctladdr(a); /* find controlling address */ 119940973Sbostic 120040973Sbostic if (a != NULL && a->q_uid != 0 && (pw = getpwuid(a->q_uid)) != NULL) 120140973Sbostic retstr = pw->pw_name; 120240973Sbostic else /* use default user */ 120340973Sbostic retstr = DefUser; 120440973Sbostic 120540973Sbostic if (tTd(40, 5)) 120640973Sbostic printf("Set controlling user for `%s' to `%s'\n", 120740973Sbostic (a == NULL)? "<null>": a->q_paddr, retstr); 120840973Sbostic 120940973Sbostic return(retstr); 121040973Sbostic } 121140973Sbostic /* 121240973Sbostic ** SETCTLUSER - sets `CtlUser' to controlling user 121340973Sbostic ** CLRCTLUSER - clears controlling user (no params, nothing returned) 121440973Sbostic ** 121540973Sbostic ** These routines manipulate `CtlUser'. 121640973Sbostic ** 121740973Sbostic ** Parameters: 121840973Sbostic ** str - controlling user as passed to setctluser() 121940973Sbostic ** 122040973Sbostic ** Returns: 122140973Sbostic ** None. 122240973Sbostic ** 122340973Sbostic ** Side Effects: 122440973Sbostic ** `CtlUser' is changed. 122540973Sbostic */ 122640973Sbostic 122740973Sbostic static char CtlUser[MAXNAME]; 122840973Sbostic 122940973Sbostic setctluser(str) 123040973Sbostic register char *str; 123140973Sbostic { 123240973Sbostic (void) strncpy(CtlUser, str, MAXNAME); 123340973Sbostic CtlUser[MAXNAME-1] = '\0'; 123440973Sbostic } 123540973Sbostic 123640973Sbostic clrctluser() 123740973Sbostic { 123840973Sbostic CtlUser[0] = '\0'; 123940973Sbostic } 124040973Sbostic 124140973Sbostic /* 124240973Sbostic ** SETCTLADDR -- create a controlling address 124340973Sbostic ** 124440973Sbostic ** If global variable `CtlUser' is set and we are given a valid 124540973Sbostic ** address, make that address a controlling address; change the 124640973Sbostic ** `q_uid', `q_gid', and `q_ruser' fields and set QGOODUID. 124740973Sbostic ** 124840973Sbostic ** Parameters: 124940973Sbostic ** a - address for which control uid/gid info may apply 125040973Sbostic ** 125140973Sbostic ** Returns: 125240973Sbostic ** None. 125340973Sbostic ** 125440973Sbostic ** Side Effects: 125540973Sbostic ** Fills in uid/gid fields in address and sets QGOODUID 125640973Sbostic ** flag if appropriate. 125740973Sbostic */ 125840973Sbostic 125940973Sbostic setctladdr(a) 126040973Sbostic ADDRESS *a; 126140973Sbostic { 126240973Sbostic struct passwd *pw; 126340973Sbostic 126440973Sbostic /* 126540973Sbostic ** If there is no current controlling user, or we were passed a 126640973Sbostic ** NULL addr ptr or we already have a controlling user, return. 126740973Sbostic */ 126840973Sbostic 126940973Sbostic if (CtlUser[0] == '\0' || a == NULL || a->q_ruser) 127040973Sbostic return; 127140973Sbostic 127240973Sbostic /* 127340973Sbostic ** Set up addr fields for controlling user. If `CtlUser' is no 127440973Sbostic ** longer valid, use the default user/group. 127540973Sbostic */ 127640973Sbostic 127740973Sbostic if ((pw = getpwnam(CtlUser)) != NULL) 127840973Sbostic { 127940973Sbostic if (a->q_home) 128040973Sbostic free(a->q_home); 128140973Sbostic a->q_home = newstr(pw->pw_dir); 128240973Sbostic a->q_uid = pw->pw_uid; 128340973Sbostic a->q_gid = pw->pw_gid; 128440973Sbostic a->q_ruser = newstr(CtlUser); 128540973Sbostic } 128640973Sbostic else 128740973Sbostic { 128840973Sbostic a->q_uid = DefUid; 128940973Sbostic a->q_gid = DefGid; 129040973Sbostic a->q_ruser = newstr(DefUser); 129140973Sbostic } 129240973Sbostic 129340973Sbostic a->q_flags |= QGOODUID; /* flag as a "ctladdr" */ 129440973Sbostic 129540973Sbostic if (tTd(40, 5)) 129640973Sbostic printf("Restored controlling user for `%s' to `%s'\n", 129740973Sbostic a->q_paddr, a->q_ruser); 129840973Sbostic } 1299