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*58724Seric static char sccsid[] = "@(#)queue.c 6.33 (Berkeley) 03/18/93 (with queueing)"; 1433731Sbostic #else 15*58724Seric static char sccsid[] = "@(#)queue.c 6.33 (Berkeley) 03/18/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; 10658690Seric syserr("!queueup: cannot create temp file %s", tf); 10741636Srick } 10858689Seric 10958689Seric if (lockfile(fd, tf, LOCK_EX|LOCK_NB)) 11051920Seric break; 11158689Seric 11251920Seric close(fd); 11341636Srick } 11441636Srick 11551920Seric tfp = fdopen(fd, "w"); 11651920Seric } 1174632Seric 1187677Seric if (tTd(40, 1)) 11917468Seric printf("queueing %s\n", e->e_id); 1204632Seric 1214632Seric /* 1226980Seric ** If there is no data file yet, create one. 1236980Seric */ 1246980Seric 1256980Seric if (e->e_df == NULL) 1266980Seric { 1276980Seric register FILE *dfp; 1289389Seric extern putbody(); 1296980Seric 1307812Seric e->e_df = newstr(queuename(e, 'd')); 13140934Srick fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode); 13240934Srick if (fd < 0) 13358690Seric syserr("!queueup: cannot create %s", e->e_df); 13440934Srick dfp = fdopen(fd, "w"); 13510173Seric (*e->e_putbody)(dfp, ProgMailer, e); 13658680Seric (void) xfclose(dfp, "queueup dfp", e->e_id); 1379389Seric e->e_putbody = putbody; 1386980Seric } 1396980Seric 1406980Seric /* 1414632Seric ** Output future work requests. 14225687Seric ** Priority and creation time should be first, since 14325687Seric ** they are required by orderq. 1444632Seric */ 1454632Seric 1469377Seric /* output message priority */ 1479377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 1489377Seric 1499630Seric /* output creation time */ 1509630Seric fprintf(tfp, "T%ld\n", e->e_ctime); 1519630Seric 1524632Seric /* output name of data file */ 1537812Seric fprintf(tfp, "D%s\n", e->e_df); 1544632Seric 15510108Seric /* message from envelope, if it exists */ 15610108Seric if (e->e_message != NULL) 15710108Seric fprintf(tfp, "M%s\n", e->e_message); 15810108Seric 15953400Seric /* $r and $s macro values */ 16053400Seric if ((p = macvalue('r', e)) != NULL) 16153400Seric fprintf(tfp, "$r%s\n", p); 16253400Seric if ((p = macvalue('s', e)) != NULL) 16353400Seric fprintf(tfp, "$s%s\n", p); 16453400Seric 1654632Seric /* output name of sender */ 1667812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 1674632Seric 16855360Seric /* output list of error recipients */ 16955360Seric lastctladdr = NULL; 17055360Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 17155360Seric { 17258680Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 17355360Seric { 17455360Seric ADDRESS *ctladdr; 17555360Seric 17655360Seric ctladdr = getctladdr(q); 17755360Seric if (ctladdr != lastctladdr) 17855360Seric { 17955360Seric printctladdr(ctladdr, tfp); 18055360Seric lastctladdr = ctladdr; 18155360Seric } 18255360Seric fprintf(tfp, "E%s\n", q->q_paddr); 18355360Seric } 18455360Seric } 18555360Seric 1864632Seric /* output list of recipient addresses */ 1876980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1884632Seric { 18958250Seric if (bitset(QQUEUEUP, q->q_flags) || 19058680Seric (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags))) 1918245Seric { 19254974Seric ADDRESS *ctladdr; 19340973Sbostic 19454975Seric ctladdr = getctladdr(q); 19554975Seric if (ctladdr != lastctladdr) 19654974Seric { 19754974Seric printctladdr(ctladdr, tfp); 19854974Seric lastctladdr = ctladdr; 19954974Seric } 2007812Seric fprintf(tfp, "R%s\n", q->q_paddr); 2019377Seric if (announce) 2029377Seric { 2039377Seric e->e_to = q->q_paddr; 20458151Seric message("queued"); 20558020Seric if (LogLevel > 8) 20658337Seric logdelivery(NULL, NULL, "queued", e); 2079377Seric e->e_to = NULL; 2089377Seric } 2099387Seric if (tTd(40, 1)) 2109387Seric { 2119387Seric printf("queueing "); 2129387Seric printaddr(q, FALSE); 2139387Seric } 2148245Seric } 2154632Seric } 2164632Seric 2179377Seric /* 2189377Seric ** Output headers for this message. 2199377Seric ** Expand macros completely here. Queue run will deal with 2209377Seric ** everything as absolute headers. 2219377Seric ** All headers that must be relative to the recipient 2229377Seric ** can be cracked later. 22310173Seric ** We set up a "null mailer" -- i.e., a mailer that will have 22410173Seric ** no effect on the addresses as they are output. 2259377Seric */ 2269377Seric 22710686Seric bzero((char *) &nullmailer, sizeof nullmailer); 22858020Seric nullmailer.m_re_rwset = nullmailer.m_rh_rwset = 22958020Seric nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1; 23010349Seric nullmailer.m_eol = "\n"; 23110173Seric 23258050Seric define('g', "\201f", e); 2336980Seric for (h = e->e_header; h != NULL; h = h->h_link) 2344632Seric { 23510686Seric extern bool bitzerop(); 23610686Seric 23712015Seric /* don't output null headers */ 2384632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 2394632Seric continue; 24012015Seric 24112015Seric /* don't output resent headers on non-resent messages */ 24212015Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 24312015Seric continue; 24412015Seric 24512015Seric /* output this header */ 2467812Seric fprintf(tfp, "H"); 24712015Seric 24812015Seric /* if conditional, output the set of conditions */ 24910686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 25010686Seric { 25110686Seric int j; 25210686Seric 25323098Seric (void) putc('?', tfp); 25410686Seric for (j = '\0'; j <= '\177'; j++) 25510686Seric if (bitnset(j, h->h_mflags)) 25623098Seric (void) putc(j, tfp); 25723098Seric (void) putc('?', tfp); 25810686Seric } 25912015Seric 26012015Seric /* output the header: expand macros, convert addresses */ 2617763Seric if (bitset(H_DEFAULT, h->h_flags)) 2627763Seric { 2637763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 2648236Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 2657763Seric } 2668245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 2679348Seric { 26855012Seric commaize(h, h->h_value, tfp, 26955012Seric bitset(EF_OLDSTYLE, e->e_flags), 27055012Seric &nullmailer, e); 2719348Seric } 2727763Seric else 2738245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 2744632Seric } 2754632Seric 2764632Seric /* 2774632Seric ** Clean up. 2784632Seric */ 2794632Seric 28051920Seric if (!newid) 28151920Seric { 28251920Seric qf = queuename(e, 'q'); 28351920Seric if (rename(tf, qf) < 0) 28451920Seric syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df); 28551920Seric if (e->e_lockfp != NULL) 28658680Seric (void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id); 28751920Seric e->e_lockfp = tfp; 28851920Seric } 28951920Seric else 29051920Seric qf = tf; 29141636Srick errno = 0; 2927391Seric 2937677Seric # ifdef LOG 2947677Seric /* save log info */ 29558020Seric if (LogLevel > 79) 2967878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 29756795Seric # endif /* LOG */ 29840934Srick fflush(tfp); 29951920Seric return; 3004632Seric } 30154974Seric 30254974Seric printctladdr(a, tfp) 30354974Seric ADDRESS *a; 30454974Seric FILE *tfp; 30554974Seric { 30654974Seric char *u; 30754974Seric struct passwd *pw; 30854974Seric extern struct passwd *getpwuid(); 30954974Seric 31054974Seric if (a == NULL) 31154974Seric { 31254974Seric fprintf(tfp, "C\n"); 31354974Seric return; 31454974Seric } 31554974Seric if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL) 31654974Seric u = DefUser; 31754974Seric else 31854974Seric u = pw->pw_name; 31954974Seric fprintf(tfp, "C%s\n", u); 32054974Seric } 32154974Seric 3224632Seric /* 3234632Seric ** RUNQUEUE -- run the jobs in the queue. 3244632Seric ** 3254632Seric ** Gets the stuff out of the queue in some presumably logical 3264632Seric ** order and processes them. 3274632Seric ** 3284632Seric ** Parameters: 32924941Seric ** forkflag -- TRUE if the queue scanning should be done in 33024941Seric ** a child process. We double-fork so it is not our 33124941Seric ** child and we don't have to clean up after it. 3324632Seric ** 3334632Seric ** Returns: 3344632Seric ** none. 3354632Seric ** 3364632Seric ** Side Effects: 3374632Seric ** runs things in the mail queue. 3384632Seric */ 3394632Seric 34055360Seric ENVELOPE QueueEnvelope; /* the queue run envelope */ 34155360Seric 34255360Seric runqueue(forkflag) 3434639Seric bool forkflag; 3444632Seric { 34524953Seric extern bool shouldqueue(); 34655360Seric register ENVELOPE *e; 34755360Seric extern ENVELOPE BlankEnvelope; 34855360Seric extern ENVELOPE *newenvelope(); 34924953Seric 3507466Seric /* 35124953Seric ** If no work will ever be selected, don't even bother reading 35224953Seric ** the queue. 35324953Seric */ 35424953Seric 35551920Seric CurrentLA = getla(); /* get load average */ 35640934Srick 35758132Seric if (shouldqueue(0L, curtime())) 35824953Seric { 35924953Seric if (Verbose) 36024953Seric printf("Skipping queue run -- load average too high\n"); 36158107Seric if (forkflag && QueueIntvl != 0) 36258107Seric (void) setevent(QueueIntvl, runqueue, TRUE); 36355360Seric return; 36424953Seric } 36524953Seric 36624953Seric /* 3677466Seric ** See if we want to go off and do other useful work. 3687466Seric */ 3694639Seric 3704639Seric if (forkflag) 3714639Seric { 3727943Seric int pid; 3737943Seric 3747943Seric pid = dofork(); 3757943Seric if (pid != 0) 3764639Seric { 37746928Sbostic extern void reapchild(); 37825184Seric 3797943Seric /* parent -- pick up intermediate zombie */ 38025184Seric #ifndef SIGCHLD 3819377Seric (void) waitfor(pid); 38256795Seric #else /* SIGCHLD */ 38325184Seric (void) signal(SIGCHLD, reapchild); 38456795Seric #endif /* SIGCHLD */ 3857690Seric if (QueueIntvl != 0) 3869348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 3874639Seric return; 3884639Seric } 3897943Seric /* child -- double fork */ 39025184Seric #ifndef SIGCHLD 3917943Seric if (fork() != 0) 3927943Seric exit(EX_OK); 39356795Seric #else /* SIGCHLD */ 39425184Seric (void) signal(SIGCHLD, SIG_DFL); 39556795Seric #endif /* SIGCHLD */ 3964639Seric } 39724941Seric 39840934Srick setproctitle("running queue: %s", QueueDir); 39924941Seric 4007876Seric # ifdef LOG 40158020Seric if (LogLevel > 69) 40255360Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 40355360Seric QueueDir, getpid(), forkflag); 40456795Seric # endif /* LOG */ 4054639Seric 4067466Seric /* 40710205Seric ** Release any resources used by the daemon code. 40810205Seric */ 40910205Seric 41010205Seric # ifdef DAEMON 41110205Seric clrdaemon(); 41256795Seric # endif /* DAEMON */ 41310205Seric 41410205Seric /* 41555360Seric ** Create ourselves an envelope 41655360Seric */ 41755360Seric 41855360Seric CurEnv = &QueueEnvelope; 41958179Seric e = newenvelope(&QueueEnvelope, CurEnv); 42055360Seric e->e_flags = BlankEnvelope.e_flags; 42155360Seric 42255360Seric /* 42327175Seric ** Make sure the alias database is open. 42427175Seric */ 42527175Seric 42655012Seric initaliases(AliasFile, FALSE, e); 42727175Seric 42827175Seric /* 4297466Seric ** Start making passes through the queue. 4307466Seric ** First, read and sort the entire queue. 4317466Seric ** Then, process the work in that order. 4327466Seric ** But if you take too long, start over. 4337466Seric */ 4347466Seric 4357943Seric /* order the existing work requests */ 43624954Seric (void) orderq(FALSE); 4377690Seric 4387943Seric /* process them once at a time */ 4397943Seric while (WorkQ != NULL) 4404639Seric { 4417943Seric WORK *w = WorkQ; 4427881Seric 4437943Seric WorkQ = WorkQ->w_next; 44455012Seric dowork(w, e); 4457943Seric free(w->w_name); 4467943Seric free((char *) w); 4474639Seric } 44829866Seric 44929866Seric /* exit without the usual cleanup */ 45055467Seric e->e_id = NULL; 45155467Seric finis(); 4524634Seric } 4534634Seric /* 4544632Seric ** ORDERQ -- order the work queue. 4554632Seric ** 4564632Seric ** Parameters: 45724941Seric ** doall -- if set, include everything in the queue (even 45824941Seric ** the jobs that cannot be run because the load 45924941Seric ** average is too high). Otherwise, exclude those 46024941Seric ** jobs. 4614632Seric ** 4624632Seric ** Returns: 46310121Seric ** The number of request in the queue (not necessarily 46410121Seric ** the number of requests in WorkQ however). 4654632Seric ** 4664632Seric ** Side Effects: 4674632Seric ** Sets WorkQ to the queue of available work, in order. 4684632Seric */ 4694632Seric 47025687Seric # define NEED_P 001 47125687Seric # define NEED_T 002 47258318Seric # define NEED_R 004 47358318Seric # define NEED_S 010 4744632Seric 47524941Seric orderq(doall) 47624941Seric bool doall; 4774632Seric { 4786625Sglickman register struct direct *d; 4794632Seric register WORK *w; 4806625Sglickman DIR *f; 4814632Seric register int i; 48225687Seric WORK wlist[QUEUESIZE+1]; 48310070Seric int wn = -1; 4844632Seric extern workcmpf(); 4854632Seric 48658318Seric if (tTd(41, 1)) 48758318Seric { 48858318Seric printf("orderq:\n"); 48958318Seric if (QueueLimitId != NULL) 49058318Seric printf("\tQueueLimitId = %s\n", QueueLimitId); 49158318Seric if (QueueLimitSender != NULL) 49258318Seric printf("\tQueueLimitSender = %s\n", QueueLimitSender); 49358318Seric if (QueueLimitRecipient != NULL) 49458318Seric printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 49558318Seric } 49658318Seric 4974632Seric /* clear out old WorkQ */ 4984632Seric for (w = WorkQ; w != NULL; ) 4994632Seric { 5004632Seric register WORK *nw = w->w_next; 5014632Seric 5024632Seric WorkQ = nw; 5034632Seric free(w->w_name); 5044632Seric free((char *) w); 5054632Seric w = nw; 5064632Seric } 5074632Seric 5084632Seric /* open the queue directory */ 5098148Seric f = opendir("."); 5104632Seric if (f == NULL) 5114632Seric { 5128148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 51310070Seric return (0); 5144632Seric } 5154632Seric 5164632Seric /* 5174632Seric ** Read the work directory. 5184632Seric */ 5194632Seric 52010070Seric while ((d = readdir(f)) != NULL) 5214632Seric { 5229377Seric FILE *cf; 5234632Seric char lbuf[MAXNAME]; 52457642Seric extern bool shouldqueue(); 52558318Seric extern bool strcontainedin(); 5264632Seric 5274632Seric /* is this an interesting entry? */ 5287812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 5294632Seric continue; 5304632Seric 53158318Seric if (QueueLimitId != NULL && 53258318Seric !strcontainedin(QueueLimitId, d->d_name)) 53358318Seric continue; 53458318Seric 53558722Seric /* 53658722Seric ** Check queue name for plausibility. This handles 53758722Seric ** both old and new type ids. 53858722Seric */ 53958722Seric 54058722Seric i = strlen(d->d_name); 54158722Seric if (i != 9 && i != 10) 54258020Seric { 54358020Seric if (Verbose) 54458020Seric printf("orderq: bogus qf name %s\n", d->d_name); 54558020Seric #ifdef LOG 54658020Seric if (LogLevel > 3) 54758020Seric syslog(LOG_NOTICE, "orderq: bogus qf name %s", 54858020Seric d->d_name); 54958020Seric #endif 55058020Seric if (strlen(d->d_name) >= MAXNAME) 55158020Seric d->d_name[MAXNAME - 1] = '\0'; 55258020Seric strcpy(lbuf, d->d_name); 55358020Seric lbuf[0] = 'Q'; 55458020Seric (void) rename(d->d_name, lbuf); 55558020Seric continue; 55658020Seric } 55758020Seric 55810070Seric /* yes -- open control file (if not too many files) */ 55925687Seric if (++wn >= QUEUESIZE) 56010070Seric continue; 56158318Seric 5628148Seric cf = fopen(d->d_name, "r"); 5634632Seric if (cf == NULL) 5644632Seric { 5657055Seric /* this may be some random person sending hir msgs */ 5667055Seric /* syserr("orderq: cannot open %s", cbuf); */ 56710090Seric if (tTd(41, 2)) 56810090Seric printf("orderq: cannot open %s (%d)\n", 56910090Seric d->d_name, errno); 5707055Seric errno = 0; 57110090Seric wn--; 5724632Seric continue; 5734632Seric } 57425687Seric w = &wlist[wn]; 57525687Seric w->w_name = newstr(d->d_name); 5764632Seric 57725027Seric /* make sure jobs in creation don't clog queue */ 57825687Seric w->w_pri = 0x7fffffff; 57925687Seric w->w_ctime = 0; 58025027Seric 5814632Seric /* extract useful information */ 58225687Seric i = NEED_P | NEED_T; 58358318Seric if (QueueLimitSender != NULL) 58458318Seric i |= NEED_S; 58558318Seric if (QueueLimitRecipient != NULL) 58658318Seric i |= NEED_R; 58725687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 5884632Seric { 58924954Seric extern long atol(); 59058318Seric extern bool strcontainedin(); 59124954Seric 59224941Seric switch (lbuf[0]) 5934632Seric { 59424941Seric case 'P': 59525687Seric w->w_pri = atol(&lbuf[1]); 59625687Seric i &= ~NEED_P; 5974632Seric break; 59825013Seric 59925013Seric case 'T': 60025687Seric w->w_ctime = atol(&lbuf[1]); 60125687Seric i &= ~NEED_T; 60225013Seric break; 60358318Seric 60458318Seric case 'R': 60558318Seric if (QueueLimitRecipient != NULL && 60658318Seric strcontainedin(QueueLimitRecipient, &lbuf[1])) 60758318Seric i &= ~NEED_R; 60858318Seric break; 60958318Seric 61058318Seric case 'S': 61158318Seric if (QueueLimitSender != NULL && 61258318Seric strcontainedin(QueueLimitSender, &lbuf[1])) 61358318Seric i &= ~NEED_S; 61458318Seric break; 6154632Seric } 6164632Seric } 6174632Seric (void) fclose(cf); 61824953Seric 61958318Seric if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 62058318Seric bitset(NEED_R|NEED_S, i)) 62124953Seric { 62224953Seric /* don't even bother sorting this job in */ 62324953Seric wn--; 62424953Seric } 6254632Seric } 6266625Sglickman (void) closedir(f); 62710090Seric wn++; 6284632Seric 6294632Seric /* 6304632Seric ** Sort the work directory. 6314632Seric */ 6324632Seric 63325687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 6344632Seric 6354632Seric /* 6364632Seric ** Convert the work list into canonical form. 6379377Seric ** Should be turning it into a list of envelopes here perhaps. 6384632Seric */ 6394632Seric 64024981Seric WorkQ = NULL; 64125687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 6424632Seric { 6434632Seric w = (WORK *) xalloc(sizeof *w); 6444632Seric w->w_name = wlist[i].w_name; 6454632Seric w->w_pri = wlist[i].w_pri; 64625013Seric w->w_ctime = wlist[i].w_ctime; 64724981Seric w->w_next = WorkQ; 64824981Seric WorkQ = w; 6494632Seric } 6504632Seric 6517677Seric if (tTd(40, 1)) 6524632Seric { 6534632Seric for (w = WorkQ; w != NULL; w = w->w_next) 6545037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 6554632Seric } 65610070Seric 65710090Seric return (wn); 6584632Seric } 6594632Seric /* 6607677Seric ** WORKCMPF -- compare function for ordering work. 6614632Seric ** 6624632Seric ** Parameters: 6634632Seric ** a -- the first argument. 6644632Seric ** b -- the second argument. 6654632Seric ** 6664632Seric ** Returns: 66724981Seric ** -1 if a < b 66824981Seric ** 0 if a == b 66924981Seric ** +1 if a > b 6704632Seric ** 6714632Seric ** Side Effects: 6724632Seric ** none. 6734632Seric */ 6744632Seric 6754632Seric workcmpf(a, b) 6765037Seric register WORK *a; 6775037Seric register WORK *b; 6784632Seric { 67957438Seric long pa = a->w_pri; 68057438Seric long pb = b->w_pri; 68124941Seric 68224941Seric if (pa == pb) 6834632Seric return (0); 68424941Seric else if (pa > pb) 68524981Seric return (1); 68624981Seric else 68710121Seric return (-1); 6884632Seric } 6894632Seric /* 6904632Seric ** DOWORK -- do a work request. 6914632Seric ** 6924632Seric ** Parameters: 6934632Seric ** w -- the work request to be satisfied. 6944632Seric ** 6954632Seric ** Returns: 6964632Seric ** none. 6974632Seric ** 6984632Seric ** Side Effects: 6994632Seric ** The work request is satisfied if possible. 7004632Seric */ 7014632Seric 70255012Seric dowork(w, e) 7034632Seric register WORK *w; 70455012Seric register ENVELOPE *e; 7054632Seric { 7064632Seric register int i; 70724941Seric extern bool shouldqueue(); 70851920Seric extern bool readqf(); 7094632Seric 7107677Seric if (tTd(40, 1)) 7115037Seric printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 7124632Seric 7134632Seric /* 71424941Seric ** Ignore jobs that are too expensive for the moment. 7154632Seric */ 7164632Seric 71757438Seric if (shouldqueue(w->w_pri, w->w_ctime)) 7184632Seric { 71924941Seric if (Verbose) 72024967Seric printf("\nSkipping %s\n", w->w_name + 2); 7214632Seric return; 7224632Seric } 7234632Seric 72424941Seric /* 72524941Seric ** Fork for work. 72624941Seric */ 72724941Seric 72824941Seric if (ForkQueueRuns) 72924941Seric { 73024941Seric i = fork(); 73124941Seric if (i < 0) 73224941Seric { 73324941Seric syserr("dowork: cannot fork"); 73424941Seric return; 73524941Seric } 73624941Seric } 73724941Seric else 73824941Seric { 73924941Seric i = 0; 74024941Seric } 74124941Seric 7424632Seric if (i == 0) 7434632Seric { 7444632Seric /* 7454632Seric ** CHILD 7468148Seric ** Lock the control file to avoid duplicate deliveries. 7478148Seric ** Then run the file as though we had just read it. 7487350Seric ** We save an idea of the temporary name so we 7497350Seric ** can recover on interrupt. 7504632Seric */ 7514632Seric 7527763Seric /* set basic modes, etc. */ 7537356Seric (void) alarm(0); 75455012Seric clearenvelope(e, FALSE); 7554632Seric QueueRun = TRUE; 7569377Seric ErrorMode = EM_MAIL; 75755012Seric e->e_id = &w->w_name[2]; 7587876Seric # ifdef LOG 75958020Seric if (LogLevel > 76) 76055012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 7617881Seric getpid()); 76256795Seric # endif /* LOG */ 7637763Seric 7647763Seric /* don't use the headers from sendmail.cf... */ 76555012Seric e->e_header = NULL; 7667763Seric 76751920Seric /* read the queue control file -- return if locked */ 76855012Seric if (!readqf(e)) 7696980Seric { 77024941Seric if (ForkQueueRuns) 77124941Seric exit(EX_OK); 77224941Seric else 77324941Seric return; 7746980Seric } 7756980Seric 77655012Seric e->e_flags |= EF_INQUEUE; 77757642Seric eatheader(e, TRUE); 7786980Seric 7796980Seric /* do the delivery */ 78055012Seric if (!bitset(EF_FATALERRS, e->e_flags)) 78155012Seric sendall(e, SM_DELIVER); 7826980Seric 7836980Seric /* finish up and exit */ 78424941Seric if (ForkQueueRuns) 78524941Seric finis(); 78624941Seric else 78755012Seric dropenvelope(e); 7884632Seric } 78924941Seric else 79024941Seric { 79124941Seric /* 79224941Seric ** Parent -- pick up results. 79324941Seric */ 7944632Seric 79524941Seric errno = 0; 79624941Seric (void) waitfor(i); 79724941Seric } 7984632Seric } 7994632Seric /* 8004632Seric ** READQF -- read queue file and set up environment. 8014632Seric ** 8024632Seric ** Parameters: 8039377Seric ** e -- the envelope of the job to run. 8044632Seric ** 8054632Seric ** Returns: 80651920Seric ** TRUE if it successfully read the queue file. 80751920Seric ** FALSE otherwise. 8084632Seric ** 8094632Seric ** Side Effects: 81051920Seric ** The queue file is returned locked. 8114632Seric */ 8124632Seric 81351920Seric bool 81451920Seric readqf(e) 8159377Seric register ENVELOPE *e; 8164632Seric { 81717477Seric char *qf; 81817477Seric register FILE *qfp; 81954974Seric ADDRESS *ctladdr; 82056400Seric struct stat st; 82157135Seric char *bp; 82257135Seric char buf[MAXLINE]; 8239348Seric extern char *fgetfolded(); 82424954Seric extern long atol(); 82554974Seric extern ADDRESS *setctluser(); 82658689Seric extern bool lockfile(); 8274632Seric 8284632Seric /* 82917468Seric ** Read and process the file. 8304632Seric */ 8314632Seric 83217477Seric qf = queuename(e, 'q'); 83351937Seric qfp = fopen(qf, "r+"); 83417477Seric if (qfp == NULL) 83517477Seric { 83640934Srick if (errno != ENOENT) 83740934Srick syserr("readqf: no control file %s", qf); 83851920Seric return FALSE; 83917477Seric } 84040934Srick 84156400Seric /* 84256400Seric ** Check the queue file for plausibility to avoid attacks. 84356400Seric */ 84456400Seric 84556400Seric if (fstat(fileno(qfp), &st) < 0) 84656400Seric { 84756400Seric /* must have been being processed by someone else */ 84856400Seric fclose(qfp); 84956400Seric return FALSE; 85056400Seric } 85156400Seric 85257135Seric if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode) 85356400Seric { 85456400Seric # ifdef LOG 85556400Seric if (LogLevel > 0) 85656400Seric { 85756400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 85856400Seric e->e_id, st.st_uid, st.st_mode); 85956400Seric } 86056795Seric # endif /* LOG */ 86156400Seric fclose(qfp); 86256400Seric return FALSE; 86356400Seric } 86456400Seric 86558689Seric if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB)) 86640934Srick { 86758689Seric /* being processed by another queuer */ 86858689Seric if (Verbose) 86958689Seric printf("%s: locked\n", e->e_id); 87051920Seric # ifdef LOG 87158689Seric if (LogLevel > 19) 87258689Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 87356795Seric # endif /* LOG */ 87440934Srick (void) fclose(qfp); 87551920Seric return FALSE; 87640934Srick } 87740934Srick 87851920Seric /* save this lock */ 87951920Seric e->e_lockfp = qfp; 88051920Seric 88140934Srick /* do basic system initialization */ 88255012Seric initsys(e); 88340934Srick 88417477Seric FileName = qf; 8859377Seric LineNumber = 0; 88651920Seric if (Verbose) 8879377Seric printf("\nRunning %s\n", e->e_id); 88854974Seric ctladdr = NULL; 88957135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 8904632Seric { 89157529Seric struct stat st; 89257529Seric 89326504Seric if (tTd(40, 4)) 89457135Seric printf("+++++ %s\n", bp); 89557135Seric switch (bp[0]) 8964632Seric { 89740973Sbostic case 'C': /* specify controlling user */ 89857135Seric ctladdr = setctluser(&bp[1]); 89940973Sbostic break; 90040973Sbostic 9014632Seric case 'R': /* specify recipient */ 90258082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 9034632Seric break; 9044632Seric 90525687Seric case 'E': /* specify error recipient */ 90658082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 90725687Seric break; 90825687Seric 9094632Seric case 'H': /* header */ 91057135Seric (void) chompheader(&bp[1], FALSE, e); 9114632Seric break; 9124632Seric 91310108Seric case 'M': /* message */ 91457135Seric e->e_message = newstr(&bp[1]); 91510108Seric break; 91610108Seric 9174632Seric case 'S': /* sender */ 91858704Seric setsender(newstr(&bp[1]), e, NULL, TRUE); 9194632Seric break; 9204632Seric 9214632Seric case 'D': /* data file name */ 92257135Seric e->e_df = newstr(&bp[1]); 9239544Seric e->e_dfp = fopen(e->e_df, "r"); 9249544Seric if (e->e_dfp == NULL) 92558020Seric { 9269377Seric syserr("readqf: cannot open %s", e->e_df); 92758020Seric e->e_msgsize = -1; 92858020Seric } 92958020Seric else if (fstat(fileno(e->e_dfp), &st) >= 0) 93057529Seric e->e_msgsize = st.st_size; 9314632Seric break; 9324632Seric 9337860Seric case 'T': /* init time */ 93457135Seric e->e_ctime = atol(&bp[1]); 9354632Seric break; 9364632Seric 9374634Seric case 'P': /* message priority */ 93857135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 9394634Seric break; 9404634Seric 94153400Seric case '$': /* define macro */ 94257135Seric define(bp[1], newstr(&bp[2]), e); 94353400Seric break; 94453400Seric 94524941Seric case '\0': /* blank line; ignore */ 94624941Seric break; 94724941Seric 9484632Seric default: 94924941Seric syserr("readqf(%s:%d): bad line \"%s\"", e->e_id, 95057135Seric LineNumber, bp); 9514632Seric break; 9524632Seric } 95357135Seric 95457135Seric if (bp != buf) 95557135Seric free(bp); 9564632Seric } 9579377Seric 9589377Seric FileName = NULL; 95924941Seric 96024941Seric /* 96124941Seric ** If we haven't read any lines, this queue file is empty. 96224941Seric ** Arrange to remove it without referencing any null pointers. 96324941Seric */ 96424941Seric 96524941Seric if (LineNumber == 0) 96624941Seric { 96724941Seric errno = 0; 96824941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 96924941Seric } 97051920Seric return TRUE; 9714632Seric } 9724632Seric /* 9739630Seric ** PRINTQUEUE -- print out a representation of the mail queue 9749630Seric ** 9759630Seric ** Parameters: 9769630Seric ** none. 9779630Seric ** 9789630Seric ** Returns: 9799630Seric ** none. 9809630Seric ** 9819630Seric ** Side Effects: 9829630Seric ** Prints a listing of the mail queue on the standard output. 9839630Seric */ 9845182Seric 9859630Seric printqueue() 9869630Seric { 9879630Seric register WORK *w; 9889630Seric FILE *f; 98910070Seric int nrequests; 9909630Seric char buf[MAXLINE]; 9919630Seric 9929630Seric /* 99358250Seric ** Check for permission to print the queue 99458250Seric */ 99558250Seric 99658523Seric if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && getuid() != 0) 99758250Seric { 99858250Seric struct stat st; 99958523Seric # ifdef NGROUPS 100058523Seric int n; 100158523Seric int gidset[NGROUPS]; 100258523Seric # endif 100358250Seric 100458517Seric if (stat(QueueDir, &st) < 0) 100558250Seric { 100658250Seric syserr("Cannot stat %s", QueueDir); 100758250Seric return; 100858250Seric } 100958523Seric # ifdef NGROUPS 101058523Seric n = getgroups(NGROUPS, gidset); 101158523Seric while (--n >= 0) 101258523Seric { 101358523Seric if (gidset[n] == st.st_gid) 101458523Seric break; 101558523Seric } 101658523Seric if (n < 0) 101758523Seric # else 101858250Seric if (getgid() != st.st_gid) 101958523Seric # endif 102058250Seric { 102158250Seric usrerr("510 You are not permitted to see the queue"); 102258250Seric setstat(EX_NOPERM); 102358250Seric return; 102458250Seric } 102558250Seric } 102658250Seric 102758250Seric /* 10289630Seric ** Read and order the queue. 10299630Seric */ 10309630Seric 103124941Seric nrequests = orderq(TRUE); 10329630Seric 10339630Seric /* 10349630Seric ** Print the work list that we have read. 10359630Seric */ 10369630Seric 10379630Seric /* first see if there is anything */ 103810070Seric if (nrequests <= 0) 10399630Seric { 104010070Seric printf("Mail queue is empty\n"); 10419630Seric return; 10429630Seric } 10439630Seric 104451920Seric CurrentLA = getla(); /* get load average */ 104540934Srick 104610096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 104725687Seric if (nrequests > QUEUESIZE) 104825687Seric printf(", only %d printed", QUEUESIZE); 104924979Seric if (Verbose) 105058716Seric printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 105124979Seric else 105258716Seric printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 10539630Seric for (w = WorkQ; w != NULL; w = w->w_next) 10549630Seric { 10559630Seric struct stat st; 105610070Seric auto time_t submittime = 0; 105710070Seric long dfsize = -1; 105810108Seric char message[MAXLINE]; 105924941Seric extern bool shouldqueue(); 106058689Seric extern bool lockfile(); 10619630Seric 106217468Seric f = fopen(w->w_name, "r"); 106317468Seric if (f == NULL) 106417468Seric { 106517468Seric errno = 0; 106617468Seric continue; 106717468Seric } 1068*58724Seric printf("%8s", w->w_name + 2); 106958689Seric if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB)) 107010070Seric printf("*"); 107157438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 107224941Seric printf("X"); 107310070Seric else 107410070Seric printf(" "); 107510070Seric errno = 0; 107617468Seric 107710108Seric message[0] = '\0'; 10789630Seric while (fgets(buf, sizeof buf, f) != NULL) 10799630Seric { 108053400Seric register int i; 108153400Seric 10829630Seric fixcrlf(buf, TRUE); 10839630Seric switch (buf[0]) 10849630Seric { 108510108Seric case 'M': /* error message */ 108653400Seric if ((i = strlen(&buf[1])) >= sizeof message) 108753400Seric i = sizeof message; 108853400Seric bcopy(&buf[1], message, i); 108953400Seric message[i] = '\0'; 109010108Seric break; 109110108Seric 10929630Seric case 'S': /* sender name */ 109324979Seric if (Verbose) 109425027Seric printf("%8ld %10ld %.12s %.38s", dfsize, 109525027Seric w->w_pri, ctime(&submittime) + 4, 109624979Seric &buf[1]); 109724979Seric else 109824979Seric printf("%8ld %.16s %.45s", dfsize, 109924979Seric ctime(&submittime), &buf[1]); 110010108Seric if (message[0] != '\0') 110125027Seric printf("\n\t\t (%.60s)", message); 11029630Seric break; 110351920Seric 110440973Sbostic case 'C': /* controlling user */ 110554974Seric if (Verbose) 110658716Seric printf("\n\t\t\t\t (---%.34s---)", 110758716Seric &buf[1]); 110840973Sbostic break; 11099630Seric 11109630Seric case 'R': /* recipient name */ 111124979Seric if (Verbose) 111258716Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 111324979Seric else 111458716Seric 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 int pid = -1; 116258716Seric char c0; 116358680Seric static char c1 = 'A'; 116458680Seric static char c2 = 'A'; 116558716Seric time_t now; 116658716Seric struct tm *tm; 116758689Seric static char buf[MAXNAME]; 116858689Seric extern bool lockfile(); 116917468Seric 117017468Seric if (e->e_id == NULL) 117117468Seric { 117217468Seric char qf[20]; 117317468Seric 117417468Seric /* find a unique id */ 117517468Seric if (pid != getpid()) 117617468Seric { 117717468Seric /* new process -- start back at "AA" */ 117817468Seric pid = getpid(); 117958716Seric now = curtime(); 118058716Seric tm = localtime(&now); 118158716Seric c0 = 'A' + tm->tm_hour; 118217468Seric c1 = 'A'; 118317468Seric c2 = 'A' - 1; 118417468Seric } 118558716Seric (void) sprintf(qf, "qf%cAA%05d", c0, pid); 118617468Seric 118717468Seric while (c1 < '~' || c2 < 'Z') 118817468Seric { 118917468Seric int i; 119017468Seric 119117468Seric if (c2 >= 'Z') 119217468Seric { 119317468Seric c1++; 119417468Seric c2 = 'A' - 1; 119517468Seric } 119658716Seric qf[3] = c1; 119758716Seric qf[4] = ++c2; 119817468Seric if (tTd(7, 20)) 119940934Srick printf("queuename: trying \"%s\"\n", qf); 120017468Seric 120140934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 120251920Seric if (i < 0) 120351920Seric { 120451920Seric if (errno == EEXIST) 120551920Seric continue; 120651920Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 120751920Seric qf, QueueDir); 120851920Seric exit(EX_UNAVAILABLE); 120951920Seric } 121058689Seric if (lockfile(i, qf, LOCK_EX|LOCK_NB)) 121151920Seric { 121251920Seric e->e_lockfp = fdopen(i, "w"); 121340934Srick break; 121417468Seric } 121551920Seric 121651920Seric /* a reader got the file; abandon it and try again */ 121751920Seric (void) close(i); 121817468Seric } 121917468Seric if (c1 >= '~' && c2 >= 'Z') 122017468Seric { 122117468Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 122217468Seric qf, QueueDir); 122317468Seric exit(EX_OSERR); 122417468Seric } 122517468Seric e->e_id = newstr(&qf[2]); 122617468Seric define('i', e->e_id, e); 122717468Seric if (tTd(7, 1)) 122817468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 122917468Seric # ifdef LOG 123058020Seric if (LogLevel > 93) 123117468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 123256795Seric # endif /* LOG */ 123317468Seric } 123417468Seric 123517468Seric if (type == '\0') 123617468Seric return (NULL); 123717468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 123817468Seric if (tTd(7, 2)) 123917468Seric printf("queuename: %s\n", buf); 124017468Seric return (buf); 124117468Seric } 124217468Seric /* 124317468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 124417468Seric ** 124517468Seric ** Parameters: 124617468Seric ** e -- the envelope to unlock. 124717468Seric ** 124817468Seric ** Returns: 124917468Seric ** none 125017468Seric ** 125117468Seric ** Side Effects: 125217468Seric ** unlocks the queue for `e'. 125317468Seric */ 125417468Seric 125517468Seric unlockqueue(e) 125617468Seric ENVELOPE *e; 125717468Seric { 125858680Seric if (tTd(51, 4)) 125958680Seric printf("unlockqueue(%s)\n", e->e_id); 126058680Seric 126151920Seric /* if there is a lock file in the envelope, close it */ 126251920Seric if (e->e_lockfp != NULL) 126358680Seric xfclose(e->e_lockfp, "unlockqueue", e->e_id); 126451920Seric e->e_lockfp = NULL; 126551920Seric 126617468Seric /* remove the transcript */ 126717468Seric # ifdef LOG 126858020Seric if (LogLevel > 87) 126917468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 127056795Seric # endif /* LOG */ 127158680Seric if (!tTd(51, 104)) 127217468Seric xunlink(queuename(e, 'x')); 127317468Seric 127417468Seric } 127540973Sbostic /* 127654974Seric ** SETCTLUSER -- create a controlling address 127740973Sbostic ** 127854974Seric ** Create a fake "address" given only a local login name; this is 127954974Seric ** used as a "controlling user" for future recipient addresses. 128040973Sbostic ** 128140973Sbostic ** Parameters: 128254974Seric ** user -- the user name of the controlling user. 128340973Sbostic ** 128440973Sbostic ** Returns: 128554974Seric ** An address descriptor for the controlling user. 128640973Sbostic ** 128740973Sbostic ** Side Effects: 128840973Sbostic ** none. 128940973Sbostic */ 129040973Sbostic 129154974Seric ADDRESS * 129254974Seric setctluser(user) 129354974Seric char *user; 129440973Sbostic { 129554974Seric register ADDRESS *a; 129640973Sbostic struct passwd *pw; 129740973Sbostic 129840973Sbostic /* 129954974Seric ** See if this clears our concept of controlling user. 130040973Sbostic */ 130140973Sbostic 130254974Seric if (user == NULL || *user == '\0') 130358704Seric user = DefUser; 130440973Sbostic 130540973Sbostic /* 130654974Seric ** Set up addr fields for controlling user. 130740973Sbostic */ 130840973Sbostic 130954974Seric a = (ADDRESS *) xalloc(sizeof *a); 131054974Seric bzero((char *) a, sizeof *a); 131154974Seric if ((pw = getpwnam(user)) != NULL) 131240973Sbostic { 131340973Sbostic a->q_home = newstr(pw->pw_dir); 131440973Sbostic a->q_uid = pw->pw_uid; 131540973Sbostic a->q_gid = pw->pw_gid; 131657642Seric a->q_user = newstr(user); 131740973Sbostic } 131840973Sbostic else 131940973Sbostic { 132040973Sbostic a->q_uid = DefUid; 132140973Sbostic a->q_gid = DefGid; 132257642Seric a->q_user = newstr(DefUser); 132340973Sbostic } 132440973Sbostic 132558294Seric a->q_flags |= QGOODUID|QPRIMARY; /* flag as a "ctladdr" */ 132656328Seric a->q_mailer = LocalMailer; 132754974Seric return a; 132840973Sbostic } 1329