122708Sdist /* 268839Seric * Copyright (c) 1983, 1995 Eric P. Allman 362528Sbostic * Copyright (c) 1988, 1993 462528Sbostic * The Regents of the University of California. All rights reserved. 533731Sbostic * 642829Sbostic * %sccs.include.redist.c% 733731Sbostic */ 822708Sdist 933731Sbostic # include "sendmail.h" 1022708Sdist 1133731Sbostic #ifndef lint 1233731Sbostic #ifdef QUEUE 13*69722Seric static char sccsid[] = "@(#)queue.c 8.82 (Berkeley) 05/27/95 (with queueing)"; 1433731Sbostic #else 15*69722Seric static char sccsid[] = "@(#)queue.c 8.82 (Berkeley) 05/27/95 (without queueing)"; 1633731Sbostic #endif 1733731Sbostic #endif /* not lint */ 1833731Sbostic 194632Seric # include <errno.h> 2057736Seric # include <dirent.h> 214632Seric 2233731Sbostic # ifdef QUEUE 234632Seric 244632Seric /* 259377Seric ** Work queue. 269377Seric */ 279377Seric 289377Seric struct work 299377Seric { 309377Seric char *w_name; /* name of control file */ 3168481Seric char *w_host; /* name of recipient host */ 3268481Seric bool w_lock; /* is message locked? */ 339377Seric long w_pri; /* priority of message, see below */ 3425013Seric time_t w_ctime; /* creation time of message */ 359377Seric struct work *w_next; /* next in queue */ 369377Seric }; 379377Seric 389377Seric typedef struct work WORK; 399377Seric 409377Seric WORK *WorkQ; /* queue of things to be done */ 4168481Seric 4268481Seric #define QF_VERSION 1 /* version number of this queue format */ 439377Seric /* 444632Seric ** QUEUEUP -- queue a message up for future transmission. 454632Seric ** 464632Seric ** Parameters: 476980Seric ** e -- the envelope to queue up. 486999Seric ** queueall -- if TRUE, queue all addresses, rather than 496999Seric ** just those with the QQUEUEUP flag set. 509377Seric ** announce -- if TRUE, tell when you are queueing up. 514632Seric ** 524632Seric ** Returns: 5351920Seric ** none. 544632Seric ** 554632Seric ** Side Effects: 569377Seric ** The current request are saved in a control file. 5751920Seric ** The queue file is left locked. 584632Seric */ 594632Seric 609377Seric queueup(e, queueall, announce) 616980Seric register ENVELOPE *e; 626999Seric bool queueall; 639377Seric bool announce; 644632Seric { 657812Seric char *qf; 667812Seric register FILE *tfp; 674632Seric register HDR *h; 685007Seric register ADDRESS *q; 6951920Seric int fd; 7051920Seric int i; 7151920Seric bool newid; 7253400Seric register char *p; 7310173Seric MAILER nullmailer; 7465870Seric MCI mcibuf; 7551920Seric char buf[MAXLINE], tf[MAXLINE]; 764632Seric 775037Seric /* 7817477Seric ** Create control file. 795037Seric */ 804632Seric 8164745Seric newid = (e->e_id == NULL) || !bitset(EF_INQUEUE, e->e_flags); 8264277Seric 8364277Seric /* if newid, queuename will create a locked qf file in e->lockfp */ 8451920Seric strcpy(tf, queuename(e, 't')); 8551920Seric tfp = e->e_lockfp; 8651920Seric if (tfp == NULL) 8751920Seric newid = FALSE; 8864277Seric 8964277Seric /* if newid, just write the qf file directly (instead of tf file) */ 9064745Seric if (!newid) 9151835Seric { 9251920Seric /* get a locked tf file */ 9364070Seric for (i = 0; i < 128; i++) 9451835Seric { 9551920Seric fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode); 9651920Seric if (fd < 0) 9751835Seric { 9864070Seric if (errno != EEXIST) 9964070Seric break; 10064070Seric #ifdef LOG 10164070Seric if (LogLevel > 0 && (i % 32) == 0) 10265090Seric syslog(LOG_ALERT, "queueup: cannot create %s, uid=%d: %s", 10365090Seric tf, geteuid(), errstring(errno)); 10464070Seric #endif 10541636Srick } 10665090Seric else 10765090Seric { 10865090Seric if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB)) 10965090Seric break; 11064070Seric #ifdef LOG 11165090Seric else if (LogLevel > 0 && (i % 32) == 0) 11265090Seric syslog(LOG_ALERT, "queueup: cannot lock %s: %s", 11365090Seric tf, errstring(errno)); 11464070Seric #endif 11565090Seric close(fd); 11665090Seric } 11758689Seric 11864070Seric if ((i % 32) == 31) 11964070Seric { 12064070Seric /* save the old temp file away */ 12164070Seric (void) rename(tf, queuename(e, 'T')); 12264070Seric } 12364070Seric else 12464070Seric sleep(i % 32); 12541636Srick } 12664724Seric if (fd < 0 || (tfp = fdopen(fd, "w")) == NULL) 12764921Seric { 12864921Seric printopenfds(TRUE); 12965090Seric syserr("!queueup: cannot create queue temp file %s, uid=%d", 13065090Seric tf, geteuid()); 13164921Seric } 13251920Seric } 1334632Seric 1347677Seric if (tTd(40, 1)) 13568481Seric printf("\n>>>>> queueing %s%s queueall=%d >>>>>\n", e->e_id, 13668481Seric newid ? " (new id)" : "", queueall); 13768603Seric if (tTd(40, 3)) 13868603Seric { 13968603Seric extern void printenvflags(); 14068603Seric 14168603Seric printf(" e_flags="); 14268603Seric printenvflags(e); 14368603Seric } 14468481Seric if (tTd(40, 32)) 14568481Seric { 14668481Seric printf(" sendq="); 14768481Seric printaddr(e->e_sendqueue, TRUE); 14868481Seric } 14964745Seric if (tTd(40, 9)) 15064745Seric { 15164745Seric printf(" tfp="); 15264745Seric dumpfd(fileno(tfp), TRUE, FALSE); 15364745Seric printf(" lockfp="); 15464745Seric if (e->e_lockfp == NULL) 15564745Seric printf("NULL\n"); 15664745Seric else 15764745Seric dumpfd(fileno(e->e_lockfp), TRUE, FALSE); 15864745Seric } 1594632Seric 1604632Seric /* 1616980Seric ** If there is no data file yet, create one. 1626980Seric */ 1636980Seric 16468564Seric if (!bitset(EF_HAS_DF, e->e_flags)) 1656980Seric { 1666980Seric register FILE *dfp; 16768564Seric char dfname[20]; 16868481Seric struct stat stbuf; 1699389Seric extern putbody(); 1706980Seric 17168564Seric strcpy(dfname, queuename(e, 'd')); 17268564Seric fd = open(dfname, O_WRONLY|O_CREAT|O_TRUNC, FileMode); 17364724Seric if (fd < 0 || (dfp = fdopen(fd, "w")) == NULL) 17465090Seric syserr("!queueup: cannot create data temp file %s, uid=%d", 17568564Seric dfname, geteuid()); 17668481Seric if (fstat(fd, &stbuf) < 0) 17768481Seric e->e_dfino = -1; 17868481Seric else 17968481Seric { 18068481Seric e->e_dfdev = stbuf.st_dev; 18168481Seric e->e_dfino = stbuf.st_ino; 18268481Seric } 18368564Seric e->e_flags |= EF_HAS_DF; 18465870Seric bzero(&mcibuf, sizeof mcibuf); 18565870Seric mcibuf.mci_out = dfp; 18665870Seric mcibuf.mci_mailer = FileMailer; 18768228Seric (*e->e_putbody)(&mcibuf, e, NULL); 18858680Seric (void) xfclose(dfp, "queueup dfp", e->e_id); 1899389Seric e->e_putbody = putbody; 1906980Seric } 1916980Seric 1926980Seric /* 1934632Seric ** Output future work requests. 19425687Seric ** Priority and creation time should be first, since 19525687Seric ** they are required by orderq. 1964632Seric */ 1974632Seric 19868481Seric /* output queue version number (must be first!) */ 19968481Seric fprintf(tfp, "V%d\n", QF_VERSION); 20068481Seric 2019377Seric /* output message priority */ 2029377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 2039377Seric 2049630Seric /* output creation time */ 2059630Seric fprintf(tfp, "T%ld\n", e->e_ctime); 2069630Seric 20768481Seric /* output last delivery time */ 20868481Seric fprintf(tfp, "K%ld\n", e->e_dtime); 20968481Seric 21068481Seric /* output number of delivery attempts */ 21168481Seric fprintf(tfp, "N%d\n", e->e_ntries); 21268481Seric 21368481Seric /* output inode number of data file */ 21468481Seric /* XXX should probably include device major/minor too */ 21568481Seric if (e->e_dfino != -1) 21668481Seric fprintf(tfp, "I%d/%d/%ld\n", 21768481Seric major(e->e_dfdev), minor(e->e_dfdev), e->e_dfino); 21868481Seric 21968564Seric /* output body type */ 22059093Seric if (e->e_bodytype != NULL) 22159093Seric fprintf(tfp, "B%s\n", e->e_bodytype); 2224632Seric 22310108Seric /* message from envelope, if it exists */ 22410108Seric if (e->e_message != NULL) 22568478Seric fprintf(tfp, "M%s\n", denlstring(e->e_message, TRUE, FALSE)); 22610108Seric 22758737Seric /* send various flag bits through */ 22858737Seric p = buf; 22958737Seric if (bitset(EF_WARNING, e->e_flags)) 23058737Seric *p++ = 'w'; 23158737Seric if (bitset(EF_RESPONSE, e->e_flags)) 23258737Seric *p++ = 'r'; 23368481Seric if (bitset(EF_HAS8BIT, e->e_flags)) 23468481Seric *p++ = '8'; 23558737Seric *p++ = '\0'; 23658737Seric if (buf[0] != '\0') 23758737Seric fprintf(tfp, "F%s\n", buf); 23858737Seric 23958957Seric /* $r and $s and $_ macro values */ 24053400Seric if ((p = macvalue('r', e)) != NULL) 24168478Seric fprintf(tfp, "$r%s\n", denlstring(p, TRUE, FALSE)); 24253400Seric if ((p = macvalue('s', e)) != NULL) 24368478Seric fprintf(tfp, "$s%s\n", denlstring(p, TRUE, FALSE)); 24458957Seric if ((p = macvalue('_', e)) != NULL) 24568478Seric fprintf(tfp, "$_%s\n", denlstring(p, TRUE, FALSE)); 24653400Seric 2474632Seric /* output name of sender */ 24868481Seric if (bitnset(M_UDBENVELOPE, e->e_from.q_mailer->m_flags)) 24968481Seric p = e->e_sender; 25068481Seric else 25168481Seric p = e->e_from.q_paddr; 25268481Seric fprintf(tfp, "S%s\n", denlstring(p, TRUE, FALSE)); 2534632Seric 25468481Seric /* output ESMTP-supplied "original" information */ 25568481Seric if (e->e_envid != NULL) 25668481Seric fprintf(tfp, "Z%s\n", denlstring(e->e_envid, TRUE, FALSE)); 25768481Seric 25868558Seric /* output list of recipient addresses */ 25959670Seric printctladdr(NULL, NULL); 2606980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 2614632Seric { 26258250Seric if (bitset(QQUEUEUP, q->q_flags) || 26358680Seric (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags))) 2648245Seric { 26559113Seric printctladdr(q, tfp); 26668481Seric if (q->q_orcpt != NULL) 26768481Seric fprintf(tfp, "Q%s\n", 26868481Seric denlstring(q->q_orcpt, TRUE, FALSE)); 26968481Seric putc('R', tfp); 27068481Seric if (bitset(QPRIMARY, q->q_flags)) 27168481Seric putc('P', tfp); 27268481Seric if (bitset(QPINGONSUCCESS, q->q_flags)) 27368481Seric putc('S', tfp); 27468481Seric if (bitset(QPINGONFAILURE, q->q_flags)) 27568481Seric putc('F', tfp); 27668481Seric if (bitset(QPINGONDELAY, q->q_flags)) 27768481Seric putc('D', tfp); 27868481Seric putc(':', tfp); 27968481Seric fprintf(tfp, "%s\n", denlstring(q->q_paddr, TRUE, FALSE)); 2809377Seric if (announce) 2819377Seric { 2829377Seric e->e_to = q->q_paddr; 28358151Seric message("queued"); 28458020Seric if (LogLevel > 8) 28568481Seric logdelivery(NULL, NULL, "queued", 28668481Seric NULL, (time_t) 0, e); 2879377Seric e->e_to = NULL; 2889377Seric } 2899387Seric if (tTd(40, 1)) 2909387Seric { 2919387Seric printf("queueing "); 2929387Seric printaddr(q, FALSE); 2939387Seric } 2948245Seric } 2954632Seric } 2964632Seric 2979377Seric /* 2989377Seric ** Output headers for this message. 2999377Seric ** Expand macros completely here. Queue run will deal with 3009377Seric ** everything as absolute headers. 3019377Seric ** All headers that must be relative to the recipient 3029377Seric ** can be cracked later. 30310173Seric ** We set up a "null mailer" -- i.e., a mailer that will have 30410173Seric ** no effect on the addresses as they are output. 3059377Seric */ 3069377Seric 30710686Seric bzero((char *) &nullmailer, sizeof nullmailer); 30858020Seric nullmailer.m_re_rwset = nullmailer.m_rh_rwset = 30965584Seric nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1; 31010349Seric nullmailer.m_eol = "\n"; 31165870Seric bzero(&mcibuf, sizeof mcibuf); 31265870Seric mcibuf.mci_mailer = &nullmailer; 31365870Seric mcibuf.mci_out = tfp; 31410173Seric 31558050Seric define('g', "\201f", e); 3166980Seric for (h = e->e_header; h != NULL; h = h->h_link) 3174632Seric { 31810686Seric extern bool bitzerop(); 31910686Seric 32012015Seric /* don't output null headers */ 3214632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 3224632Seric continue; 32312015Seric 32412015Seric /* don't output resent headers on non-resent messages */ 32512015Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 32612015Seric continue; 32712015Seric 32865267Seric /* expand macros; if null, don't output header at all */ 32965267Seric if (bitset(H_DEFAULT, h->h_flags)) 33065267Seric { 33168529Seric (void) expand(h->h_value, buf, sizeof buf, e); 33265267Seric if (buf[0] == '\0') 33365267Seric continue; 33465267Seric } 33565267Seric 33612015Seric /* output this header */ 3377812Seric fprintf(tfp, "H"); 33812015Seric 33912015Seric /* if conditional, output the set of conditions */ 34010686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 34110686Seric { 34210686Seric int j; 34310686Seric 34423098Seric (void) putc('?', tfp); 34510686Seric for (j = '\0'; j <= '\177'; j++) 34610686Seric if (bitnset(j, h->h_mflags)) 34723098Seric (void) putc(j, tfp); 34823098Seric (void) putc('?', tfp); 34910686Seric } 35012015Seric 35112015Seric /* output the header: expand macros, convert addresses */ 3527763Seric if (bitset(H_DEFAULT, h->h_flags)) 3537763Seric { 35465267Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 3557763Seric } 3568245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 3579348Seric { 35858737Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 35963753Seric FILE *savetrace = TrafficLogFile; 36058737Seric 36163753Seric TrafficLogFile = NULL; 36263753Seric 36358737Seric if (bitset(H_FROM, h->h_flags)) 36458737Seric oldstyle = FALSE; 36558737Seric 36665870Seric commaize(h, h->h_value, oldstyle, &mcibuf, e); 36763753Seric 36863753Seric TrafficLogFile = savetrace; 3699348Seric } 3707763Seric else 3718245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 3724632Seric } 3734632Seric 3744632Seric /* 3754632Seric ** Clean up. 3764632Seric */ 3774632Seric 37864762Seric if (fflush(tfp) < 0 || fsync(fileno(tfp)) < 0 || ferror(tfp)) 37958732Seric { 38058732Seric if (newid) 38158732Seric syserr("!552 Error writing control file %s", tf); 38258732Seric else 38358732Seric syserr("!452 Error writing control file %s", tf); 38458732Seric } 38558732Seric 38651920Seric if (!newid) 38751920Seric { 38864277Seric /* rename (locked) tf to be (locked) qf */ 38951920Seric qf = queuename(e, 'q'); 39051920Seric if (rename(tf, qf) < 0) 39168564Seric syserr("cannot rename(%s, %s), uid=%d", 39268564Seric tf, qf, geteuid()); 39364277Seric 39464277Seric /* close and unlock old (locked) qf */ 39551920Seric if (e->e_lockfp != NULL) 39658680Seric (void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id); 39751920Seric e->e_lockfp = tfp; 39851920Seric } 39951920Seric else 40051920Seric qf = tf; 40141636Srick errno = 0; 40264745Seric e->e_flags |= EF_INQUEUE; 4037391Seric 4047677Seric # ifdef LOG 4057677Seric /* save log info */ 40658020Seric if (LogLevel > 79) 40768564Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s", e->e_id, qf); 40856795Seric # endif /* LOG */ 40964307Seric 41064307Seric if (tTd(40, 1)) 41164307Seric printf("<<<<< done queueing %s <<<<<\n\n", e->e_id); 41251920Seric return; 4134632Seric } 41454974Seric 41554974Seric printctladdr(a, tfp) 41659113Seric register ADDRESS *a; 41754974Seric FILE *tfp; 41854974Seric { 41959113Seric char *uname; 42059113Seric register struct passwd *pw; 42159113Seric register ADDRESS *q; 42259113Seric uid_t uid; 42359113Seric static ADDRESS *lastctladdr; 42459113Seric static uid_t lastuid; 42554974Seric 42659113Seric /* initialization */ 42763850Seric if (a == NULL || a->q_alias == NULL || tfp == NULL) 42854974Seric { 42959670Seric if (lastctladdr != NULL && tfp != NULL) 43059113Seric fprintf(tfp, "C\n"); 43159113Seric lastctladdr = NULL; 43259113Seric lastuid = 0; 43354974Seric return; 43454974Seric } 43559113Seric 43659113Seric /* find the active uid */ 43759113Seric q = getctladdr(a); 43859113Seric if (q == NULL) 43959113Seric uid = 0; 44054974Seric else 44159113Seric uid = q->q_uid; 44263850Seric a = a->q_alias; 44359113Seric 44459113Seric /* check to see if this is the same as last time */ 44559113Seric if (lastctladdr != NULL && uid == lastuid && 44659113Seric strcmp(lastctladdr->q_paddr, a->q_paddr) == 0) 44759113Seric return; 44859113Seric lastuid = uid; 44959113Seric lastctladdr = a; 45059113Seric 45168693Seric if (uid == 0 || (pw = sm_getpwuid(uid)) == NULL) 45259270Seric uname = ""; 45359113Seric else 45459113Seric uname = pw->pw_name; 45559113Seric 45668478Seric fprintf(tfp, "C%s:%s\n", uname, denlstring(a->q_paddr, TRUE, FALSE)); 45754974Seric } 4584632Seric /* 4594632Seric ** RUNQUEUE -- run the jobs in the queue. 4604632Seric ** 4614632Seric ** Gets the stuff out of the queue in some presumably logical 4624632Seric ** order and processes them. 4634632Seric ** 4644632Seric ** Parameters: 46524941Seric ** forkflag -- TRUE if the queue scanning should be done in 46624941Seric ** a child process. We double-fork so it is not our 46724941Seric ** child and we don't have to clean up after it. 4684632Seric ** 4694632Seric ** Returns: 4704632Seric ** none. 4714632Seric ** 4724632Seric ** Side Effects: 4734632Seric ** runs things in the mail queue. 4744632Seric */ 4754632Seric 47655360Seric ENVELOPE QueueEnvelope; /* the queue run envelope */ 47755360Seric 47868481Seric void 47955360Seric runqueue(forkflag) 4804639Seric bool forkflag; 4814632Seric { 48255360Seric register ENVELOPE *e; 48368481Seric int njobs; 48468481Seric int sequenceno = 0; 48555360Seric extern ENVELOPE BlankEnvelope; 48624953Seric 4877466Seric /* 48824953Seric ** If no work will ever be selected, don't even bother reading 48924953Seric ** the queue. 49024953Seric */ 49124953Seric 49251920Seric CurrentLA = getla(); /* get load average */ 49340934Srick 49458132Seric if (shouldqueue(0L, curtime())) 49524953Seric { 49668854Seric char *msg = "Skipping queue run -- load average too high"; 49768854Seric 49824953Seric if (Verbose) 49968854Seric printf("%s\n", msg); 50068854Seric #ifdef LOG 50168854Seric if (LogLevel > 8) 50268854Seric syslog(LOG_INFO, "runqueue: %s", msg); 50368854Seric #endif 50458107Seric if (forkflag && QueueIntvl != 0) 50558107Seric (void) setevent(QueueIntvl, runqueue, TRUE); 50655360Seric return; 50724953Seric } 50824953Seric 50924953Seric /* 5107466Seric ** See if we want to go off and do other useful work. 5117466Seric */ 5124639Seric 5134639Seric if (forkflag) 5144639Seric { 5157943Seric int pid; 51665223Seric #ifdef SIGCHLD 51765223Seric extern void reapchild(); 5187943Seric 51965223Seric (void) setsignal(SIGCHLD, reapchild); 52065223Seric #endif 52165223Seric 5227943Seric pid = dofork(); 5237943Seric if (pid != 0) 5244639Seric { 5257943Seric /* parent -- pick up intermediate zombie */ 52625184Seric #ifndef SIGCHLD 5279377Seric (void) waitfor(pid); 52856795Seric #endif /* SIGCHLD */ 5297690Seric if (QueueIntvl != 0) 5309348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 5314639Seric return; 5324639Seric } 5337943Seric /* child -- double fork */ 53425184Seric #ifndef SIGCHLD 5357943Seric if (fork() != 0) 5367943Seric exit(EX_OK); 53756795Seric #else /* SIGCHLD */ 53864035Seric (void) setsignal(SIGCHLD, SIG_DFL); 53956795Seric #endif /* SIGCHLD */ 5404639Seric } 54124941Seric 54240934Srick setproctitle("running queue: %s", QueueDir); 54324941Seric 5447876Seric # ifdef LOG 54558020Seric if (LogLevel > 69) 54655360Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 54755360Seric QueueDir, getpid(), forkflag); 54856795Seric # endif /* LOG */ 5494639Seric 5507466Seric /* 55110205Seric ** Release any resources used by the daemon code. 55210205Seric */ 55310205Seric 55410205Seric # ifdef DAEMON 55510205Seric clrdaemon(); 55656795Seric # endif /* DAEMON */ 55710205Seric 55864658Seric /* force it to run expensive jobs */ 55964658Seric NoConnect = FALSE; 56064658Seric 56110205Seric /* 56255360Seric ** Create ourselves an envelope 56355360Seric */ 56455360Seric 56555360Seric CurEnv = &QueueEnvelope; 56658179Seric e = newenvelope(&QueueEnvelope, CurEnv); 56755360Seric e->e_flags = BlankEnvelope.e_flags; 56855360Seric 56955360Seric /* 57027175Seric ** Make sure the alias database is open. 57127175Seric */ 57227175Seric 57360537Seric initmaps(FALSE, e); 57427175Seric 57527175Seric /* 5767466Seric ** Start making passes through the queue. 5777466Seric ** First, read and sort the entire queue. 5787466Seric ** Then, process the work in that order. 5797466Seric ** But if you take too long, start over. 5807466Seric */ 5817466Seric 5827943Seric /* order the existing work requests */ 58368481Seric njobs = orderq(FALSE); 5847690Seric 5857943Seric /* process them once at a time */ 5867943Seric while (WorkQ != NULL) 5874639Seric { 5887943Seric WORK *w = WorkQ; 5897881Seric 5907943Seric WorkQ = WorkQ->w_next; 59158884Seric 59258884Seric /* 59358884Seric ** Ignore jobs that are too expensive for the moment. 59458884Seric */ 59558884Seric 59668481Seric sequenceno++; 59758884Seric if (shouldqueue(w->w_pri, w->w_ctime)) 59858884Seric { 59958884Seric if (Verbose) 60068481Seric printf("\nSkipping %s (sequence %d of %d)\n", 60168481Seric w->w_name + 2, sequenceno, njobs); 60258884Seric } 60358930Seric else 60458930Seric { 60564296Seric pid_t pid; 60664296Seric extern pid_t dowork(); 60764296Seric 60868481Seric if (Verbose) 60968481Seric printf("\nRunning %s (sequence %d of %d)\n", 61068481Seric w->w_name + 2, sequenceno, njobs); 61164296Seric pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e); 61264296Seric errno = 0; 61366316Seric if (pid != 0) 61466316Seric (void) waitfor(pid); 61558930Seric } 6167943Seric free(w->w_name); 61768481Seric if (w->w_host) 61868481Seric free(w->w_host); 6197943Seric free((char *) w); 6204639Seric } 62129866Seric 62229866Seric /* exit without the usual cleanup */ 62355467Seric e->e_id = NULL; 62455467Seric finis(); 6254634Seric } 6264634Seric /* 6274632Seric ** ORDERQ -- order the work queue. 6284632Seric ** 6294632Seric ** Parameters: 63024941Seric ** doall -- if set, include everything in the queue (even 63124941Seric ** the jobs that cannot be run because the load 63224941Seric ** average is too high). Otherwise, exclude those 63324941Seric ** jobs. 6344632Seric ** 6354632Seric ** Returns: 63610121Seric ** The number of request in the queue (not necessarily 63710121Seric ** the number of requests in WorkQ however). 6384632Seric ** 6394632Seric ** Side Effects: 6404632Seric ** Sets WorkQ to the queue of available work, in order. 6414632Seric */ 6424632Seric 64325687Seric # define NEED_P 001 64425687Seric # define NEED_T 002 64558318Seric # define NEED_R 004 64658318Seric # define NEED_S 010 6474632Seric 648*69722Seric static WORK *WorkList = NULL; 649*69722Seric static int WorkListSize = 0; 650*69722Seric 65124941Seric orderq(doall) 65224941Seric bool doall; 6534632Seric { 65460219Seric register struct dirent *d; 6554632Seric register WORK *w; 6566625Sglickman DIR *f; 6574632Seric register int i; 65810070Seric int wn = -1; 65968481Seric int wc; 6604632Seric 66158318Seric if (tTd(41, 1)) 66258318Seric { 66358318Seric printf("orderq:\n"); 66458318Seric if (QueueLimitId != NULL) 66558318Seric printf("\tQueueLimitId = %s\n", QueueLimitId); 66658318Seric if (QueueLimitSender != NULL) 66758318Seric printf("\tQueueLimitSender = %s\n", QueueLimitSender); 66858318Seric if (QueueLimitRecipient != NULL) 66958318Seric printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 67058318Seric } 67158318Seric 6724632Seric /* clear out old WorkQ */ 6734632Seric for (w = WorkQ; w != NULL; ) 6744632Seric { 6754632Seric register WORK *nw = w->w_next; 6764632Seric 6774632Seric WorkQ = nw; 6784632Seric free(w->w_name); 67968481Seric if (w->w_host) 68068481Seric free(w->w_host); 6814632Seric free((char *) w); 6824632Seric w = nw; 6834632Seric } 6844632Seric 6854632Seric /* open the queue directory */ 6868148Seric f = opendir("."); 6874632Seric if (f == NULL) 6884632Seric { 6898148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 69010070Seric return (0); 6914632Seric } 6924632Seric 6934632Seric /* 6944632Seric ** Read the work directory. 6954632Seric */ 6964632Seric 69710070Seric while ((d = readdir(f)) != NULL) 6984632Seric { 6999377Seric FILE *cf; 70064492Seric register char *p; 70168528Seric char lbuf[MAXNAME + 1]; 70258318Seric extern bool strcontainedin(); 7034632Seric 7044632Seric /* is this an interesting entry? */ 7057812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 7064632Seric continue; 7074632Seric 70858318Seric if (QueueLimitId != NULL && 70958318Seric !strcontainedin(QueueLimitId, d->d_name)) 71058318Seric continue; 71158318Seric 71268563Seric #ifdef PICKY_QF_NAME_CHECK 71358722Seric /* 71458722Seric ** Check queue name for plausibility. This handles 71558722Seric ** both old and new type ids. 71658722Seric */ 71758722Seric 71864492Seric p = d->d_name + 2; 71964492Seric if (isupper(p[0]) && isupper(p[2])) 72064492Seric p += 3; 72164492Seric else if (isupper(p[1])) 72264492Seric p += 2; 72364492Seric else 72464492Seric p = d->d_name; 72564492Seric for (i = 0; isdigit(*p); p++) 72664492Seric i++; 72764492Seric if (i < 5 || *p != '\0') 72858020Seric { 72958020Seric if (Verbose) 73058020Seric printf("orderq: bogus qf name %s\n", d->d_name); 73168563Seric # ifdef LOG 73268563Seric if (LogLevel > 0) 73368563Seric syslog(LOG_ALERT, "orderq: bogus qf name %s", 73458020Seric d->d_name); 73568563Seric # endif 73668706Seric if (strlen(d->d_name) > (SIZE_T) MAXNAME) 73768528Seric d->d_name[MAXNAME] = '\0'; 73858020Seric strcpy(lbuf, d->d_name); 73958020Seric lbuf[0] = 'Q'; 74058020Seric (void) rename(d->d_name, lbuf); 74158020Seric continue; 74258020Seric } 74368563Seric #endif 74458020Seric 74568563Seric /* open control file (if not too many files) */ 746*69722Seric if (++wn >= WorkListSize) 747*69722Seric { 748*69722Seric grow_wlist(); 749*69722Seric if (wn >= WorkListSize) 750*69722Seric continue; 751*69722Seric } 75258318Seric 7538148Seric cf = fopen(d->d_name, "r"); 7544632Seric if (cf == NULL) 7554632Seric { 7567055Seric /* this may be some random person sending hir msgs */ 7577055Seric /* syserr("orderq: cannot open %s", cbuf); */ 75810090Seric if (tTd(41, 2)) 75910090Seric printf("orderq: cannot open %s (%d)\n", 76010090Seric d->d_name, errno); 7617055Seric errno = 0; 76210090Seric wn--; 7634632Seric continue; 7644632Seric } 765*69722Seric w = &WorkList[wn]; 76625687Seric w->w_name = newstr(d->d_name); 76768481Seric w->w_host = NULL; 76868481Seric w->w_lock = !lockfile(fileno(cf), w->w_name, NULL, LOCK_SH|LOCK_NB); 7694632Seric 77025027Seric /* make sure jobs in creation don't clog queue */ 77125687Seric w->w_pri = 0x7fffffff; 77225687Seric w->w_ctime = 0; 77325027Seric 7744632Seric /* extract useful information */ 77525687Seric i = NEED_P | NEED_T; 77658318Seric if (QueueLimitSender != NULL) 77758318Seric i |= NEED_S; 77868481Seric if (QueueSortOrder == QS_BYHOST || QueueLimitRecipient != NULL) 77958318Seric i |= NEED_R; 78025687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 7814632Seric { 78258318Seric extern bool strcontainedin(); 78324954Seric 78424941Seric switch (lbuf[0]) 7854632Seric { 78624941Seric case 'P': 78725687Seric w->w_pri = atol(&lbuf[1]); 78825687Seric i &= ~NEED_P; 7894632Seric break; 79025013Seric 79125013Seric case 'T': 79225687Seric w->w_ctime = atol(&lbuf[1]); 79325687Seric i &= ~NEED_T; 79425013Seric break; 79558318Seric 79658318Seric case 'R': 79768481Seric if (w->w_host == NULL && 79868481Seric (p = strrchr(&lbuf[1], '@')) != NULL) 79968481Seric w->w_host = newstr(&p[1]); 80068481Seric if (QueueLimitRecipient == NULL || 80158318Seric strcontainedin(QueueLimitRecipient, &lbuf[1])) 80258318Seric i &= ~NEED_R; 80358318Seric break; 80458318Seric 80558318Seric case 'S': 80658318Seric if (QueueLimitSender != NULL && 80758318Seric strcontainedin(QueueLimitSender, &lbuf[1])) 80858318Seric i &= ~NEED_S; 80958318Seric break; 8104632Seric } 8114632Seric } 8124632Seric (void) fclose(cf); 81324953Seric 81458318Seric if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 81558318Seric bitset(NEED_R|NEED_S, i)) 81624953Seric { 81724953Seric /* don't even bother sorting this job in */ 81868481Seric free(w->w_name); 81968481Seric if (w->w_host) 82068481Seric free(w->w_host); 82124953Seric wn--; 82224953Seric } 8234632Seric } 8246625Sglickman (void) closedir(f); 82510090Seric wn++; 8264632Seric 827*69722Seric wc = min(wn, WorkListSize); 8284632Seric 82968481Seric if (QueueSortOrder == QS_BYHOST) 83068481Seric { 83168481Seric extern workcmpf1(); 83268481Seric extern workcmpf2(); 83367612Seric 83468481Seric /* 83568481Seric ** Sort the work directory for the first time, 83668481Seric ** based on host name, lock status, and priority. 83768481Seric */ 83868481Seric 839*69722Seric qsort((char *) WorkList, wc, sizeof *WorkList, workcmpf1); 84068481Seric 84168481Seric /* 84268481Seric ** If one message to host is locked, "lock" all messages 84368481Seric ** to that host. 84468481Seric */ 84568481Seric 84668481Seric i = 0; 84768481Seric while (i < wc) 84868481Seric { 849*69722Seric if (!WorkList[i].w_lock) 85068481Seric { 85168481Seric i++; 85268481Seric continue; 85368481Seric } 854*69722Seric w = &WorkList[i]; 85568481Seric while (++i < wc) 85668481Seric { 857*69722Seric if (WorkList[i].w_host == NULL && 85868481Seric w->w_host == NULL) 859*69722Seric WorkList[i].w_lock = TRUE; 860*69722Seric else if (WorkList[i].w_host != NULL && 86168481Seric w->w_host != NULL && 862*69722Seric strcmp(WorkList[i].w_host, w->w_host) == 0) 863*69722Seric WorkList[i].w_lock = TRUE; 86468481Seric else 86568481Seric break; 86668481Seric } 86768481Seric } 86868481Seric 86968481Seric /* 87068481Seric ** Sort the work directory for the second time, 87168481Seric ** based on lock status, host name, and priority. 87268481Seric */ 87368481Seric 874*69722Seric qsort((char *) WorkList, wc, sizeof *WorkList, workcmpf2); 87568481Seric } 87668481Seric else 87768481Seric { 87868481Seric extern workcmpf0(); 87968481Seric 88068481Seric /* 88168481Seric ** Simple sort based on queue priority only. 88268481Seric */ 88368481Seric 884*69722Seric qsort((char *) WorkList, wc, sizeof *WorkList, workcmpf0); 88568481Seric } 88668481Seric 8874632Seric /* 8884632Seric ** Convert the work list into canonical form. 8899377Seric ** Should be turning it into a list of envelopes here perhaps. 8904632Seric */ 8914632Seric 89224981Seric WorkQ = NULL; 89368481Seric for (i = wc; --i >= 0; ) 8944632Seric { 8954632Seric w = (WORK *) xalloc(sizeof *w); 896*69722Seric w->w_name = WorkList[i].w_name; 897*69722Seric w->w_host = WorkList[i].w_host; 898*69722Seric w->w_lock = WorkList[i].w_lock; 899*69722Seric w->w_pri = WorkList[i].w_pri; 900*69722Seric w->w_ctime = WorkList[i].w_ctime; 90124981Seric w->w_next = WorkQ; 90224981Seric WorkQ = w; 9034632Seric } 904*69722Seric free(WorkList); 905*69722Seric WorkList = NULL; 9064632Seric 9077677Seric if (tTd(40, 1)) 9084632Seric { 9094632Seric for (w = WorkQ; w != NULL; w = w->w_next) 9105037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 9114632Seric } 91210070Seric 91310090Seric return (wn); 9144632Seric } 915*69722Seric 916*69722Seric grow_wlist() 917*69722Seric { 918*69722Seric if (tTd(41, 1)) 919*69722Seric printf("grow_wlist: WorkListSize=%d\n", WorkListSize); 920*69722Seric if (WorkListSize >= MAXQUEUESIZE) 921*69722Seric { 922*69722Seric # ifdef LOG 923*69722Seric if (LogLevel > 0) 924*69722Seric syslog(LOG_ALERT, "WorkList for %s maxed out at %d", 925*69722Seric QueueDir, WorkListSize); 926*69722Seric # endif 927*69722Seric } 928*69722Seric else if (WorkList == NULL) 929*69722Seric { 930*69722Seric WorkList = (WORK *) xalloc(sizeof(WORK) * (QUEUESEGSIZE + 1)); 931*69722Seric WorkListSize = QUEUESEGSIZE; 932*69722Seric } 933*69722Seric else 934*69722Seric { 935*69722Seric int newsize = WorkListSize + QUEUESEGSIZE; 936*69722Seric WORK *newlist = (WORK *) realloc((char *)WorkList, 937*69722Seric (unsigned)sizeof(WORK) * (newsize + 1)); 938*69722Seric 939*69722Seric if (newlist != NULL) 940*69722Seric { 941*69722Seric WorkListSize = newsize; 942*69722Seric WorkList = newlist; 943*69722Seric # ifdef LOG 944*69722Seric if (LogLevel > 1) 945*69722Seric { 946*69722Seric syslog(LOG_NOTICE, "grew WorkList for %s to %d", 947*69722Seric QueueDir, WorkListSize); 948*69722Seric } 949*69722Seric } 950*69722Seric else if (LogLevel > 0) 951*69722Seric { 952*69722Seric syslog(LOG_ALERT, "FAILED to grow WorkList for %s to %d", 953*69722Seric QueueDir, newsize); 954*69722Seric # endif 955*69722Seric } 956*69722Seric } 957*69722Seric if (tTd(41, 1)) 958*69722Seric printf("grow_wlist: WorkListSize now %d\n", WorkListSize); 959*69722Seric } 9604632Seric /* 96168481Seric ** WORKCMPF0 -- simple priority-only compare function. 9624632Seric ** 9634632Seric ** Parameters: 9644632Seric ** a -- the first argument. 9654632Seric ** b -- the second argument. 9664632Seric ** 9674632Seric ** Returns: 96824981Seric ** -1 if a < b 96924981Seric ** 0 if a == b 97024981Seric ** +1 if a > b 9714632Seric ** 9724632Seric ** Side Effects: 9734632Seric ** none. 9744632Seric */ 9754632Seric 97668481Seric workcmpf0(a, b) 9775037Seric register WORK *a; 9785037Seric register WORK *b; 9794632Seric { 98057438Seric long pa = a->w_pri; 98157438Seric long pb = b->w_pri; 98224941Seric 98324941Seric if (pa == pb) 98468481Seric return 0; 98524941Seric else if (pa > pb) 98668481Seric return 1; 98724981Seric else 98868481Seric return -1; 9894632Seric } 9904632Seric /* 99168481Seric ** WORKCMPF1 -- first compare function for ordering work based on host name. 99268481Seric ** 99368481Seric ** Sorts on host name, lock status, and priority in that order. 99468481Seric ** 99568481Seric ** Parameters: 99668481Seric ** a -- the first argument. 99768481Seric ** b -- the second argument. 99868481Seric ** 99968481Seric ** Returns: 100068481Seric ** <0 if a < b 100168481Seric ** 0 if a == b 100268481Seric ** >0 if a > b 100368481Seric ** 100468481Seric ** Side Effects: 100568481Seric ** none. 100668481Seric */ 100768481Seric 100868481Seric workcmpf1(a, b) 100968481Seric register WORK *a; 101068481Seric register WORK *b; 101168481Seric { 101268481Seric int i; 101368481Seric 101468481Seric /* host name */ 101568481Seric if (a->w_host != NULL && b->w_host == NULL) 101668481Seric return 1; 101768481Seric else if (a->w_host == NULL && b->w_host != NULL) 101868481Seric return -1; 101968481Seric if (a->w_host != NULL && b->w_host != NULL && 102068481Seric (i = strcmp(a->w_host, b->w_host))) 102168481Seric return i; 102268481Seric 102368481Seric /* lock status */ 102468481Seric if (a->w_lock != b->w_lock) 102568481Seric return b->w_lock - a->w_lock; 102668481Seric 102768481Seric /* job priority */ 102868481Seric return a->w_pri - b->w_pri; 102968481Seric } 103068481Seric /* 103168481Seric ** WORKCMPF2 -- second compare function for ordering work based on host name. 103268481Seric ** 103368481Seric ** Sorts on lock status, host name, and priority in that order. 103468481Seric ** 103568481Seric ** Parameters: 103668481Seric ** a -- the first argument. 103768481Seric ** b -- the second argument. 103868481Seric ** 103968481Seric ** Returns: 104068481Seric ** <0 if a < b 104168481Seric ** 0 if a == b 104268481Seric ** >0 if a > b 104368481Seric ** 104468481Seric ** Side Effects: 104568481Seric ** none. 104668481Seric */ 104768481Seric 104868481Seric workcmpf2(a, b) 104968481Seric register WORK *a; 105068481Seric register WORK *b; 105168481Seric { 105268481Seric int i; 105368481Seric 105468481Seric /* lock status */ 105568481Seric if (a->w_lock != b->w_lock) 105668481Seric return a->w_lock - b->w_lock; 105768481Seric 105868481Seric /* host name */ 105968481Seric if (a->w_host != NULL && b->w_host == NULL) 106068481Seric return 1; 106168481Seric else if (a->w_host == NULL && b->w_host != NULL) 106268481Seric return -1; 106368481Seric if (a->w_host != NULL && b->w_host != NULL && 106468481Seric (i = strcmp(a->w_host, b->w_host))) 106568481Seric return i; 106668481Seric 106768481Seric /* job priority */ 106868481Seric return a->w_pri - b->w_pri; 106968481Seric } 107068481Seric /* 10714632Seric ** DOWORK -- do a work request. 10724632Seric ** 10734632Seric ** Parameters: 107458884Seric ** id -- the ID of the job to run. 107558884Seric ** forkflag -- if set, run this in background. 107658924Seric ** requeueflag -- if set, reinstantiate the queue quickly. 107758924Seric ** This is used when expanding aliases in the queue. 107861094Seric ** If forkflag is also set, it doesn't wait for the 107961094Seric ** child. 108058884Seric ** e - the envelope in which to run it. 10814632Seric ** 10824632Seric ** Returns: 108364296Seric ** process id of process that is running the queue job. 10844632Seric ** 10854632Seric ** Side Effects: 10864632Seric ** The work request is satisfied if possible. 10874632Seric */ 10884632Seric 108964296Seric pid_t 109058924Seric dowork(id, forkflag, requeueflag, e) 109158884Seric char *id; 109258884Seric bool forkflag; 109358924Seric bool requeueflag; 109455012Seric register ENVELOPE *e; 10954632Seric { 109664296Seric register pid_t pid; 109751920Seric extern bool readqf(); 10984632Seric 10997677Seric if (tTd(40, 1)) 110058884Seric printf("dowork(%s)\n", id); 11014632Seric 11024632Seric /* 110324941Seric ** Fork for work. 110424941Seric */ 110524941Seric 110658884Seric if (forkflag) 110724941Seric { 110864296Seric pid = fork(); 110964296Seric if (pid < 0) 111024941Seric { 111124941Seric syserr("dowork: cannot fork"); 111264296Seric return 0; 111324941Seric } 111464839Seric else if (pid > 0) 111564839Seric { 111664839Seric /* parent -- clean out connection cache */ 111764839Seric mci_flush(FALSE, NULL); 111864839Seric } 111924941Seric } 112024941Seric else 112124941Seric { 112264296Seric pid = 0; 112324941Seric } 112424941Seric 112564296Seric if (pid == 0) 11264632Seric { 11274632Seric /* 11284632Seric ** CHILD 11298148Seric ** Lock the control file to avoid duplicate deliveries. 11308148Seric ** Then run the file as though we had just read it. 11317350Seric ** We save an idea of the temporary name so we 11327350Seric ** can recover on interrupt. 11334632Seric */ 11344632Seric 11357763Seric /* set basic modes, etc. */ 11367356Seric (void) alarm(0); 113755012Seric clearenvelope(e, FALSE); 113864554Seric e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS; 113958734Seric e->e_errormode = EM_MAIL; 114058884Seric e->e_id = id; 114164765Seric GrabTo = UseErrorsTo = FALSE; 114266316Seric ExitStat = EX_OK; 114363846Seric if (forkflag) 114464554Seric { 114564296Seric disconnect(1, e); 114664554Seric OpMode = MD_DELIVER; 114764554Seric } 11487876Seric # ifdef LOG 114958020Seric if (LogLevel > 76) 115055012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 11517881Seric getpid()); 115256795Seric # endif /* LOG */ 11537763Seric 11547763Seric /* don't use the headers from sendmail.cf... */ 115555012Seric e->e_header = NULL; 11567763Seric 115751920Seric /* read the queue control file -- return if locked */ 115865200Seric if (!readqf(e)) 11596980Seric { 116058884Seric if (tTd(40, 4)) 116158884Seric printf("readqf(%s) failed\n", e->e_id); 116258884Seric if (forkflag) 116324941Seric exit(EX_OK); 116424941Seric else 116566316Seric return 0; 11666980Seric } 11676980Seric 116855012Seric e->e_flags |= EF_INQUEUE; 11696980Seric 117068481Seric /* if this has been tried recently, let it be */ 117168481Seric if (e->e_ntries > 0 && (curtime() - e->e_dtime) < MinQueueAge) 117268481Seric { 117368481Seric char *howlong = pintvl(curtime() - e->e_dtime, TRUE); 117458924Seric 117568481Seric e->e_flags |= EF_KEEPQUEUE; 117668481Seric if (Verbose || tTd(40, 8)) 117768481Seric printf("%s: too young (%s)\n", 117868481Seric e->e_id, howlong); 117968481Seric #ifdef LOG 118068481Seric if (LogLevel > 19) 118168481Seric syslog(LOG_DEBUG, "%s: too young (%s)", 118268481Seric e->e_id, howlong); 118368481Seric #endif 118468481Seric } 118568481Seric else 118668481Seric { 118768481Seric eatheader(e, requeueflag); 11886980Seric 118968481Seric if (requeueflag) 119068481Seric queueup(e, TRUE, FALSE); 119168481Seric 119268481Seric /* do the delivery */ 119368481Seric sendall(e, SM_DELIVER); 119468481Seric } 119568481Seric 11966980Seric /* finish up and exit */ 119758884Seric if (forkflag) 119824941Seric finis(); 119924941Seric else 120055012Seric dropenvelope(e); 12014632Seric } 120264296Seric e->e_id = NULL; 120364296Seric return pid; 12044632Seric } 12054632Seric /* 12064632Seric ** READQF -- read queue file and set up environment. 12074632Seric ** 12084632Seric ** Parameters: 12099377Seric ** e -- the envelope of the job to run. 12104632Seric ** 12114632Seric ** Returns: 121251920Seric ** TRUE if it successfully read the queue file. 121351920Seric ** FALSE otherwise. 12144632Seric ** 12154632Seric ** Side Effects: 121651920Seric ** The queue file is returned locked. 12174632Seric */ 12184632Seric 121951920Seric bool 122065200Seric readqf(e) 12219377Seric register ENVELOPE *e; 12224632Seric { 122317477Seric register FILE *qfp; 122454974Seric ADDRESS *ctladdr; 122556400Seric struct stat st; 122657135Seric char *bp; 122768481Seric int qfver = 0; 122868481Seric register char *p; 122968481Seric char *orcpt = NULL; 123058915Seric char qf[20]; 123157135Seric char buf[MAXLINE]; 123254974Seric extern ADDRESS *setctluser(); 123368490Seric extern void loseqfile(); 12344632Seric 12354632Seric /* 123617468Seric ** Read and process the file. 12374632Seric */ 12384632Seric 123958915Seric strcpy(qf, queuename(e, 'q')); 124051937Seric qfp = fopen(qf, "r+"); 124117477Seric if (qfp == NULL) 124217477Seric { 124358884Seric if (tTd(40, 8)) 124458884Seric printf("readqf(%s): fopen failure (%s)\n", 124558884Seric qf, errstring(errno)); 124640934Srick if (errno != ENOENT) 124740934Srick syserr("readqf: no control file %s", qf); 124851920Seric return FALSE; 124917477Seric } 125040934Srick 125164335Seric if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB)) 125264277Seric { 125364277Seric /* being processed by another queuer */ 125468481Seric if (Verbose || tTd(40, 8)) 125564277Seric printf("%s: locked\n", e->e_id); 125664277Seric # ifdef LOG 125764277Seric if (LogLevel > 19) 125864277Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 125964277Seric # endif /* LOG */ 126064277Seric (void) fclose(qfp); 126164277Seric return FALSE; 126264277Seric } 126364277Seric 126456400Seric /* 126556400Seric ** Check the queue file for plausibility to avoid attacks. 126656400Seric */ 126756400Seric 126856400Seric if (fstat(fileno(qfp), &st) < 0) 126956400Seric { 127056400Seric /* must have been being processed by someone else */ 127158884Seric if (tTd(40, 8)) 127258884Seric printf("readqf(%s): fstat failure (%s)\n", 127358884Seric qf, errstring(errno)); 127456400Seric fclose(qfp); 127556400Seric return FALSE; 127656400Seric } 127756400Seric 127868563Seric if (st.st_uid != geteuid() || bitset(S_IWOTH|S_IWGRP, st.st_mode)) 127956400Seric { 128056400Seric # ifdef LOG 128156400Seric if (LogLevel > 0) 128256400Seric { 128356400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 128456400Seric e->e_id, st.st_uid, st.st_mode); 128556400Seric } 128656795Seric # endif /* LOG */ 128758884Seric if (tTd(40, 8)) 128858884Seric printf("readqf(%s): bogus file\n", qf); 128968490Seric loseqfile(e, "bogus file uid in mqueue"); 129056400Seric fclose(qfp); 129156400Seric return FALSE; 129256400Seric } 129356400Seric 129464277Seric if (st.st_size == 0) 129540934Srick { 129664277Seric /* must be a bogus file -- just remove it */ 129764277Seric (void) unlink(qf); 129864277Seric fclose(qfp); 129951920Seric return FALSE; 130040934Srick } 130140934Srick 130264277Seric if (st.st_nlink == 0) 130359101Seric { 130464277Seric /* 130564277Seric ** Race condition -- we got a file just as it was being 130664277Seric ** unlinked. Just assume it is zero length. 130764277Seric */ 130864277Seric 130959101Seric fclose(qfp); 131059101Seric return FALSE; 131159101Seric } 131259101Seric 131364277Seric /* good file -- save this lock */ 131451920Seric e->e_lockfp = qfp; 131551920Seric 131640934Srick /* do basic system initialization */ 131755012Seric initsys(e); 131865164Seric define('i', e->e_id, e); 131940934Srick 13209377Seric LineNumber = 0; 132163850Seric e->e_flags |= EF_GLOBALERRS; 132263850Seric OpMode = MD_DELIVER; 132354974Seric ctladdr = NULL; 132468481Seric e->e_dfino = -1; 132568564Seric e->e_msgsize = -1; 132657135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 13274632Seric { 132858737Seric register char *p; 132968481Seric u_long qflags; 133068481Seric ADDRESS *q; 133157529Seric 133226504Seric if (tTd(40, 4)) 133357135Seric printf("+++++ %s\n", bp); 133457135Seric switch (bp[0]) 13354632Seric { 133668481Seric case 'V': /* queue file version number */ 133768481Seric qfver = atoi(&bp[1]); 133868481Seric if (qfver > QF_VERSION) 133968481Seric { 134068481Seric syserr("Version number in qf (%d) greater than max (%d)", 134168481Seric qfver, QF_VERSION); 134268481Seric } 134368481Seric break; 134468481Seric 134540973Sbostic case 'C': /* specify controlling user */ 134657135Seric ctladdr = setctluser(&bp[1]); 134740973Sbostic break; 134840973Sbostic 134968481Seric case 'Q': /* original recipient */ 135068481Seric orcpt = newstr(&bp[1]); 135168481Seric break; 135268481Seric 13534632Seric case 'R': /* specify recipient */ 135468481Seric p = bp; 135568481Seric qflags = 0; 135668481Seric if (qfver >= 1) 135768481Seric { 135868481Seric /* get flag bits */ 135968481Seric while (*++p != '\0' && *p != ':') 136068481Seric { 136168481Seric switch (*p) 136268481Seric { 136368481Seric case 'S': 136468481Seric qflags |= QPINGONSUCCESS; 136568481Seric break; 136668481Seric 136768481Seric case 'F': 136868481Seric qflags |= QPINGONFAILURE; 136968481Seric break; 137068481Seric 137168481Seric case 'D': 137268481Seric qflags |= QPINGONDELAY; 137368481Seric break; 137468481Seric 137568481Seric case 'P': 137668481Seric qflags |= QPRIMARY; 137768481Seric break; 137868481Seric } 137968481Seric } 138068481Seric } 138168481Seric else 138268481Seric qflags |= QPRIMARY; 138368481Seric q = parseaddr(++p, NULLADDR, RF_COPYALL, '\0', NULL, e); 138468481Seric if (q != NULL) 138568481Seric { 138668481Seric q->q_alias = ctladdr; 138768481Seric q->q_flags |= qflags; 138868481Seric q->q_orcpt = orcpt; 138968481Seric (void) recipient(q, &e->e_sendqueue, 0, e); 139068481Seric } 139168481Seric orcpt = NULL; 13924632Seric break; 13934632Seric 139425687Seric case 'E': /* specify error recipient */ 139568558Seric /* no longer used */ 139625687Seric break; 139725687Seric 13984632Seric case 'H': /* header */ 139968717Seric (void) chompheader(&bp[1], FALSE, NULL, e); 14004632Seric break; 14014632Seric 140210108Seric case 'M': /* message */ 140364705Seric /* ignore this; we want a new message next time */ 140410108Seric break; 140510108Seric 14064632Seric case 'S': /* sender */ 140758704Seric setsender(newstr(&bp[1]), e, NULL, TRUE); 14084632Seric break; 14094632Seric 141059093Seric case 'B': /* body type */ 141159093Seric e->e_bodytype = newstr(&bp[1]); 141259093Seric break; 141359093Seric 14144632Seric case 'D': /* data file name */ 141568564Seric /* obsolete -- ignore */ 14164632Seric break; 14174632Seric 14187860Seric case 'T': /* init time */ 141957135Seric e->e_ctime = atol(&bp[1]); 14204632Seric break; 14214632Seric 142268481Seric case 'I': /* data file's inode number */ 142368481Seric if (e->e_dfino == -1) 142468481Seric e->e_dfino = atol(&buf[1]); 142568481Seric break; 142668481Seric 142768481Seric case 'K': /* time of last deliver attempt */ 142868481Seric e->e_dtime = atol(&buf[1]); 142968481Seric break; 143068481Seric 143168481Seric case 'N': /* number of delivery attempts */ 143268481Seric e->e_ntries = atoi(&buf[1]); 143368481Seric break; 143468481Seric 14354634Seric case 'P': /* message priority */ 143657135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 14374634Seric break; 14384634Seric 143958737Seric case 'F': /* flag bits */ 144058737Seric for (p = &bp[1]; *p != '\0'; p++) 144158737Seric { 144258737Seric switch (*p) 144358737Seric { 144458737Seric case 'w': /* warning sent */ 144558737Seric e->e_flags |= EF_WARNING; 144658737Seric break; 144758737Seric 144858737Seric case 'r': /* response */ 144958737Seric e->e_flags |= EF_RESPONSE; 145058737Seric break; 145168481Seric 145268481Seric case '8': /* has 8 bit data */ 145368481Seric e->e_flags |= EF_HAS8BIT; 145468481Seric break; 145558737Seric } 145658737Seric } 145758737Seric break; 145858737Seric 145968481Seric case 'Z': /* original envelope id from ESMTP */ 146068481Seric e->e_envid = newstr(&bp[1]); 146168481Seric break; 146268481Seric 146353400Seric case '$': /* define macro */ 146457135Seric define(bp[1], newstr(&bp[2]), e); 146553400Seric break; 146653400Seric 146724941Seric case '\0': /* blank line; ignore */ 146824941Seric break; 146924941Seric 14704632Seric default: 147165960Seric syserr("readqf: %s: line %d: bad line \"%s\"", 147265200Seric qf, LineNumber, bp); 147363753Seric fclose(qfp); 147468490Seric loseqfile(e, "unrecognized line"); 147563753Seric return FALSE; 14764632Seric } 147757135Seric 147857135Seric if (bp != buf) 147957135Seric free(bp); 14804632Seric } 14819377Seric 148224941Seric /* 148324941Seric ** If we haven't read any lines, this queue file is empty. 148424941Seric ** Arrange to remove it without referencing any null pointers. 148524941Seric */ 148624941Seric 148724941Seric if (LineNumber == 0) 148824941Seric { 148924941Seric errno = 0; 149024941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 149124941Seric } 149268564Seric else 149368564Seric { 149468564Seric /* 149568564Seric ** Arrange to read the data file. 149668564Seric */ 149768564Seric 149868564Seric p = queuename(e, 'd'); 149968564Seric e->e_dfp = fopen(p, "r"); 150068564Seric if (e->e_dfp == NULL) 150168564Seric { 150268564Seric syserr("readqf: cannot open %s", p); 150368564Seric } 150468603Seric else 150568564Seric { 150668603Seric e->e_flags |= EF_HAS_DF; 150768603Seric if (fstat(fileno(e->e_dfp), &st) >= 0) 150868603Seric { 150968603Seric e->e_msgsize = st.st_size; 151068603Seric e->e_dfdev = st.st_dev; 151168603Seric e->e_dfino = st.st_ino; 151268603Seric } 151368564Seric } 151468564Seric } 151568564Seric 151651920Seric return TRUE; 15174632Seric } 15184632Seric /* 15199630Seric ** PRINTQUEUE -- print out a representation of the mail queue 15209630Seric ** 15219630Seric ** Parameters: 15229630Seric ** none. 15239630Seric ** 15249630Seric ** Returns: 15259630Seric ** none. 15269630Seric ** 15279630Seric ** Side Effects: 15289630Seric ** Prints a listing of the mail queue on the standard output. 15299630Seric */ 15305182Seric 15319630Seric printqueue() 15329630Seric { 15339630Seric register WORK *w; 15349630Seric FILE *f; 153510070Seric int nrequests; 15369630Seric char buf[MAXLINE]; 15379630Seric 15389630Seric /* 153958250Seric ** Check for permission to print the queue 154058250Seric */ 154158250Seric 154264333Seric if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0) 154358250Seric { 154458250Seric struct stat st; 154558523Seric # ifdef NGROUPS 154658523Seric int n; 154763937Seric GIDSET_T gidset[NGROUPS]; 154858523Seric # endif 154958250Seric 155058517Seric if (stat(QueueDir, &st) < 0) 155158250Seric { 155258250Seric syserr("Cannot stat %s", QueueDir); 155358250Seric return; 155458250Seric } 155558523Seric # ifdef NGROUPS 155658523Seric n = getgroups(NGROUPS, gidset); 155758523Seric while (--n >= 0) 155858523Seric { 155958523Seric if (gidset[n] == st.st_gid) 156058523Seric break; 156158523Seric } 156258523Seric if (n < 0) 156358523Seric # else 156463787Seric if (RealGid != st.st_gid) 156558523Seric # endif 156658250Seric { 156758250Seric usrerr("510 You are not permitted to see the queue"); 156858250Seric setstat(EX_NOPERM); 156958250Seric return; 157058250Seric } 157158250Seric } 157258250Seric 157358250Seric /* 15749630Seric ** Read and order the queue. 15759630Seric */ 15769630Seric 157724941Seric nrequests = orderq(TRUE); 15789630Seric 15799630Seric /* 15809630Seric ** Print the work list that we have read. 15819630Seric */ 15829630Seric 15839630Seric /* first see if there is anything */ 158410070Seric if (nrequests <= 0) 15859630Seric { 158610070Seric printf("Mail queue is empty\n"); 15879630Seric return; 15889630Seric } 15899630Seric 159051920Seric CurrentLA = getla(); /* get load average */ 159140934Srick 159210096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 1593*69722Seric if (nrequests > WorkListSize) 1594*69722Seric printf(", only %d printed", WorkListSize); 159524979Seric if (Verbose) 159658716Seric printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 159724979Seric else 159858716Seric printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 15999630Seric for (w = WorkQ; w != NULL; w = w->w_next) 16009630Seric { 16019630Seric struct stat st; 160210070Seric auto time_t submittime = 0; 160368565Seric long dfsize; 160458737Seric int flags = 0; 160568481Seric int qfver; 160669538Seric char statmsg[MAXLINE]; 160768528Seric char bodytype[MAXNAME + 1]; 16089630Seric 160964355Seric printf("%8s", w->w_name + 2); 161017468Seric f = fopen(w->w_name, "r"); 161117468Seric if (f == NULL) 161217468Seric { 161364355Seric printf(" (job completed)\n"); 161417468Seric errno = 0; 161517468Seric continue; 161617468Seric } 161768565Seric w->w_name[0] = 'd'; 161868565Seric if (stat(w->w_name, &st) >= 0) 161968565Seric dfsize = st.st_size; 162068565Seric else 162168565Seric dfsize = -1; 162268481Seric if (w->w_lock) 162310070Seric printf("*"); 162457438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 162524941Seric printf("X"); 162610070Seric else 162710070Seric printf(" "); 162810070Seric errno = 0; 162917468Seric 163069538Seric statmsg[0] = bodytype[0] = '\0'; 163168481Seric qfver = 0; 16329630Seric while (fgets(buf, sizeof buf, f) != NULL) 16339630Seric { 163453400Seric register int i; 163558737Seric register char *p; 163653400Seric 16379630Seric fixcrlf(buf, TRUE); 16389630Seric switch (buf[0]) 16399630Seric { 164068481Seric case 'V': /* queue file version */ 164168481Seric qfver = atoi(&buf[1]); 164268481Seric break; 164368481Seric 164410108Seric case 'M': /* error message */ 164569538Seric if ((i = strlen(&buf[1])) >= sizeof statmsg) 164669538Seric i = sizeof statmsg - 1; 164769538Seric bcopy(&buf[1], statmsg, i); 164869538Seric statmsg[i] = '\0'; 164910108Seric break; 165010108Seric 165159093Seric case 'B': /* body type */ 165259093Seric if ((i = strlen(&buf[1])) >= sizeof bodytype) 165359093Seric i = sizeof bodytype - 1; 165459093Seric bcopy(&buf[1], bodytype, i); 165559093Seric bodytype[i] = '\0'; 165659093Seric break; 165759093Seric 16589630Seric case 'S': /* sender name */ 165924979Seric if (Verbose) 166058737Seric printf("%8ld %10ld%c%.12s %.38s", 166158737Seric dfsize, 166258737Seric w->w_pri, 166358737Seric bitset(EF_WARNING, flags) ? '+' : ' ', 166458737Seric ctime(&submittime) + 4, 166524979Seric &buf[1]); 166624979Seric else 166724979Seric printf("%8ld %.16s %.45s", dfsize, 166824979Seric ctime(&submittime), &buf[1]); 166969538Seric if (statmsg[0] != '\0' || bodytype[0] != '\0') 167059093Seric { 167159093Seric printf("\n %10.10s", bodytype); 167269538Seric if (statmsg[0] != '\0') 167369538Seric printf(" (%.60s)", statmsg); 167459093Seric } 16759630Seric break; 167651920Seric 167740973Sbostic case 'C': /* controlling user */ 167854974Seric if (Verbose) 167958716Seric printf("\n\t\t\t\t (---%.34s---)", 168058716Seric &buf[1]); 168140973Sbostic break; 16829630Seric 16839630Seric case 'R': /* recipient name */ 168468481Seric p = &buf[1]; 168568481Seric if (qfver >= 1) 168668481Seric { 168768481Seric p = strchr(p, ':'); 168868481Seric if (p == NULL) 168968481Seric break; 169068481Seric p++; 169168481Seric } 169224979Seric if (Verbose) 169368481Seric printf("\n\t\t\t\t\t %.38s", p); 169424979Seric else 169568481Seric printf("\n\t\t\t\t %.45s", p); 16969630Seric break; 16979630Seric 16989630Seric case 'T': /* creation time */ 169924941Seric submittime = atol(&buf[1]); 17009630Seric break; 170110070Seric 170258737Seric case 'F': /* flag bits */ 170358737Seric for (p = &buf[1]; *p != '\0'; p++) 170458737Seric { 170558737Seric switch (*p) 170658737Seric { 170758737Seric case 'w': 170858737Seric flags |= EF_WARNING; 170958737Seric break; 171058737Seric } 171158737Seric } 17129630Seric } 17139630Seric } 171410070Seric if (submittime == (time_t) 0) 171510070Seric printf(" (no control file)"); 17169630Seric printf("\n"); 171723098Seric (void) fclose(f); 17189630Seric } 17199630Seric } 17209630Seric 172156795Seric # endif /* QUEUE */ 172217468Seric /* 172317468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 172417468Seric ** 172517468Seric ** Assigns an id code if one does not already exist. 172617468Seric ** This code is very careful to avoid trashing existing files 172717468Seric ** under any circumstances. 172817468Seric ** 172917468Seric ** Parameters: 173017468Seric ** e -- envelope to build it in/from. 173117468Seric ** type -- the file type, used as the first character 173217468Seric ** of the file name. 173317468Seric ** 173417468Seric ** Returns: 173517468Seric ** a pointer to the new file name (in a static buffer). 173617468Seric ** 173717468Seric ** Side Effects: 173851920Seric ** If no id code is already assigned, queuename will 173951920Seric ** assign an id code, create a qf file, and leave a 174051920Seric ** locked, open-for-write file pointer in the envelope. 174117468Seric */ 174217468Seric 174317468Seric char * 174417468Seric queuename(e, type) 174517468Seric register ENVELOPE *e; 174659700Seric int type; 174717468Seric { 174817468Seric static int pid = -1; 174958741Seric static char c0; 175058741Seric static char c1; 175158741Seric static char c2; 175258716Seric time_t now; 175358716Seric struct tm *tm; 175468528Seric static char buf[MAXNAME + 1]; 175517468Seric 175617468Seric if (e->e_id == NULL) 175717468Seric { 175817468Seric char qf[20]; 175917468Seric 176017468Seric /* find a unique id */ 176117468Seric if (pid != getpid()) 176217468Seric { 176317468Seric /* new process -- start back at "AA" */ 176417468Seric pid = getpid(); 176558716Seric now = curtime(); 176658716Seric tm = localtime(&now); 176758716Seric c0 = 'A' + tm->tm_hour; 176817468Seric c1 = 'A'; 176917468Seric c2 = 'A' - 1; 177017468Seric } 177158716Seric (void) sprintf(qf, "qf%cAA%05d", c0, pid); 177217468Seric 177317468Seric while (c1 < '~' || c2 < 'Z') 177417468Seric { 177517468Seric int i; 177617468Seric 177717468Seric if (c2 >= 'Z') 177817468Seric { 177917468Seric c1++; 178017468Seric c2 = 'A' - 1; 178117468Seric } 178258716Seric qf[3] = c1; 178358716Seric qf[4] = ++c2; 178417468Seric if (tTd(7, 20)) 178540934Srick printf("queuename: trying \"%s\"\n", qf); 178617468Seric 178740934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 178851920Seric if (i < 0) 178951920Seric { 179051920Seric if (errno == EEXIST) 179151920Seric continue; 179264705Seric syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 179364705Seric qf, QueueDir, geteuid()); 179451920Seric exit(EX_UNAVAILABLE); 179551920Seric } 179664335Seric if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB)) 179751920Seric { 179851920Seric e->e_lockfp = fdopen(i, "w"); 179940934Srick break; 180017468Seric } 180151920Seric 180251920Seric /* a reader got the file; abandon it and try again */ 180351920Seric (void) close(i); 180417468Seric } 180517468Seric if (c1 >= '~' && c2 >= 'Z') 180617468Seric { 180764705Seric syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 180864705Seric qf, QueueDir, geteuid()); 180917468Seric exit(EX_OSERR); 181017468Seric } 181117468Seric e->e_id = newstr(&qf[2]); 181217468Seric define('i', e->e_id, e); 181317468Seric if (tTd(7, 1)) 181417468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 181564745Seric if (tTd(7, 9)) 181664745Seric { 181764745Seric printf(" lockfd="); 181864745Seric dumpfd(fileno(e->e_lockfp), TRUE, FALSE); 181964745Seric } 182017468Seric # ifdef LOG 182158020Seric if (LogLevel > 93) 182217468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 182356795Seric # endif /* LOG */ 182417468Seric } 182517468Seric 182617468Seric if (type == '\0') 182717468Seric return (NULL); 182817468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 182917468Seric if (tTd(7, 2)) 183017468Seric printf("queuename: %s\n", buf); 183117468Seric return (buf); 183217468Seric } 183317468Seric /* 183417468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 183517468Seric ** 183617468Seric ** Parameters: 183717468Seric ** e -- the envelope to unlock. 183817468Seric ** 183917468Seric ** Returns: 184017468Seric ** none 184117468Seric ** 184217468Seric ** Side Effects: 184317468Seric ** unlocks the queue for `e'. 184417468Seric */ 184517468Seric 184617468Seric unlockqueue(e) 184717468Seric ENVELOPE *e; 184817468Seric { 184958680Seric if (tTd(51, 4)) 185058680Seric printf("unlockqueue(%s)\n", e->e_id); 185158680Seric 185251920Seric /* if there is a lock file in the envelope, close it */ 185351920Seric if (e->e_lockfp != NULL) 185458680Seric xfclose(e->e_lockfp, "unlockqueue", e->e_id); 185551920Seric e->e_lockfp = NULL; 185651920Seric 185758728Seric /* don't create a queue id if we don't already have one */ 185858728Seric if (e->e_id == NULL) 185958728Seric return; 186058728Seric 186117468Seric /* remove the transcript */ 186217468Seric # ifdef LOG 186358020Seric if (LogLevel > 87) 186417468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 186556795Seric # endif /* LOG */ 186658680Seric if (!tTd(51, 104)) 186717468Seric xunlink(queuename(e, 'x')); 186817468Seric 186917468Seric } 187040973Sbostic /* 187154974Seric ** SETCTLUSER -- create a controlling address 187240973Sbostic ** 187354974Seric ** Create a fake "address" given only a local login name; this is 187454974Seric ** used as a "controlling user" for future recipient addresses. 187540973Sbostic ** 187640973Sbostic ** Parameters: 187754974Seric ** user -- the user name of the controlling user. 187840973Sbostic ** 187940973Sbostic ** Returns: 188054974Seric ** An address descriptor for the controlling user. 188140973Sbostic ** 188240973Sbostic ** Side Effects: 188340973Sbostic ** none. 188440973Sbostic */ 188540973Sbostic 188654974Seric ADDRESS * 188754974Seric setctluser(user) 188854974Seric char *user; 188940973Sbostic { 189054974Seric register ADDRESS *a; 189140973Sbostic struct passwd *pw; 189259113Seric char *p; 189340973Sbostic 189440973Sbostic /* 189554974Seric ** See if this clears our concept of controlling user. 189640973Sbostic */ 189740973Sbostic 189863850Seric if (user == NULL || *user == '\0') 189963850Seric return NULL; 190040973Sbostic 190140973Sbostic /* 190254974Seric ** Set up addr fields for controlling user. 190340973Sbostic */ 190440973Sbostic 190554974Seric a = (ADDRESS *) xalloc(sizeof *a); 190654974Seric bzero((char *) a, sizeof *a); 190759113Seric 190859113Seric p = strchr(user, ':'); 190959113Seric if (p != NULL) 191059113Seric *p++ = '\0'; 191168693Seric if (*user != '\0' && (pw = sm_getpwnam(user)) != NULL) 191240973Sbostic { 191365822Seric if (strcmp(pw->pw_dir, "/") == 0) 191465822Seric a->q_home = ""; 191565822Seric else 191665822Seric a->q_home = newstr(pw->pw_dir); 191740973Sbostic a->q_uid = pw->pw_uid; 191840973Sbostic a->q_gid = pw->pw_gid; 191959270Seric a->q_flags |= QGOODUID; 192040973Sbostic } 192168603Seric 192268603Seric if (*user != '\0') 192368603Seric a->q_user = newstr(user); 192468603Seric else if (p != NULL) 192568603Seric a->q_user = newstr(p); 192640973Sbostic else 192757642Seric a->q_user = newstr(DefUser); 192840973Sbostic 192959270Seric a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 193056328Seric a->q_mailer = LocalMailer; 193159113Seric if (p == NULL) 193259113Seric a->q_paddr = a->q_user; 193359113Seric else 193459113Seric a->q_paddr = newstr(p); 193554974Seric return a; 193640973Sbostic } 193768490Seric /* 193868490Seric ** LOSEQFILE -- save the qf as Qf and try to let someone know 193968490Seric ** 194068490Seric ** Parameters: 194168490Seric ** e -- the envelope (e->e_id will be used). 194268490Seric ** why -- reported to whomever can hear. 194368490Seric ** 194468490Seric ** Returns: 194568490Seric ** none. 194668490Seric */ 194768490Seric 194868490Seric void 194968490Seric loseqfile(e, why) 195068490Seric register ENVELOPE *e; 195168490Seric char *why; 195268490Seric { 195368563Seric char *p; 195468490Seric char buf[40]; 195568490Seric 195668490Seric if (e == NULL || e->e_id == NULL) 195768490Seric return; 195868490Seric if (strlen(e->e_id) > sizeof buf - 4) 195968490Seric return; 196068490Seric strcpy(buf, queuename(e, 'q')); 196168563Seric p = queuename(e, 'Q'); 196268563Seric if (rename(buf, p) < 0) 196368563Seric syserr("cannot rename(%s, %s), uid=%d", buf, p, geteuid()); 196468490Seric #ifdef LOG 196568563Seric else if (LogLevel > 0) 196668563Seric syslog(LOG_ALERT, "Losing %s: %s", buf, why); 196768490Seric #endif 196868490Seric } 1969