122708Sdist /* 234921Sbostic * Copyright (c) 1983 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*64277Seric static char sccsid[] = "@(#)queue.c 8.11 (Berkeley) 08/16/93 (with queueing)"; 1433731Sbostic #else 15*64277Seric static char sccsid[] = "@(#)queue.c 8.11 (Berkeley) 08/16/93 (without queueing)"; 1633731Sbostic #endif 1733731Sbostic #endif /* not lint */ 1833731Sbostic 194632Seric # include <errno.h> 2040973Sbostic # include <pwd.h> 2157736Seric # include <dirent.h> 224632Seric 2333731Sbostic # ifdef QUEUE 244632Seric 254632Seric /* 269377Seric ** Work queue. 279377Seric */ 289377Seric 299377Seric struct work 309377Seric { 319377Seric char *w_name; /* name of control file */ 329377Seric long w_pri; /* priority of message, see below */ 3325013Seric time_t w_ctime; /* creation time of message */ 349377Seric struct work *w_next; /* next in queue */ 359377Seric }; 369377Seric 379377Seric typedef struct work WORK; 389377Seric 399377Seric WORK *WorkQ; /* queue of things to be done */ 409377Seric /* 414632Seric ** QUEUEUP -- queue a message up for future transmission. 424632Seric ** 434632Seric ** Parameters: 446980Seric ** e -- the envelope to queue up. 456999Seric ** queueall -- if TRUE, queue all addresses, rather than 466999Seric ** just those with the QQUEUEUP flag set. 479377Seric ** announce -- if TRUE, tell when you are queueing up. 484632Seric ** 494632Seric ** Returns: 5051920Seric ** none. 514632Seric ** 524632Seric ** Side Effects: 539377Seric ** The current request are saved in a control file. 5451920Seric ** The queue file is left locked. 554632Seric */ 564632Seric 579377Seric queueup(e, queueall, announce) 586980Seric register ENVELOPE *e; 596999Seric bool queueall; 609377Seric bool announce; 614632Seric { 627812Seric char *qf; 637812Seric register FILE *tfp; 644632Seric register HDR *h; 655007Seric register ADDRESS *q; 6651920Seric int fd; 6751920Seric int i; 6851920Seric bool newid; 6953400Seric register char *p; 7010173Seric MAILER nullmailer; 7151920Seric char buf[MAXLINE], tf[MAXLINE]; 724632Seric 735037Seric /* 7417477Seric ** Create control file. 755037Seric */ 764632Seric 7751920Seric newid = (e->e_id == NULL); 78*64277Seric 79*64277Seric /* if newid, queuename will create a locked qf file in e->lockfp */ 8051920Seric strcpy(tf, queuename(e, 't')); 8151920Seric tfp = e->e_lockfp; 8251920Seric if (tfp == NULL) 8351920Seric newid = FALSE; 84*64277Seric 85*64277Seric /* if newid, just write the qf file directly (instead of tf file) */ 8651920Seric if (newid) 8751835Seric { 8851920Seric tfp = e->e_lockfp; 8951920Seric } 9051920Seric else 9151920Seric { 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) 10264070Seric syslog(LOG_ALERT, "queueup: cannot create %s: %s", 10364070Seric tf, errstring(errno)); 10464070Seric #endif 10564070Seric continue; 10641636Srick } 10758689Seric 10858689Seric if (lockfile(fd, tf, LOCK_EX|LOCK_NB)) 10951920Seric break; 11064070Seric #ifdef LOG 11164070Seric else if (LogLevel > 0 && (i % 32) == 0) 11264070Seric syslog(LOG_ALERT, "queueup: cannot lock %s: %s", 11364070Seric tf, errstring(errno)); 11464070Seric #endif 11558689Seric 11651920Seric close(fd); 11764070Seric 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 } 12658801Seric if (fd < 0) 12764070Seric syserr("!queueup: cannot create temp file %s", tf); 12841636Srick 12951920Seric tfp = fdopen(fd, "w"); 13051920Seric } 1314632Seric 1327677Seric if (tTd(40, 1)) 13317468Seric printf("queueing %s\n", e->e_id); 1344632Seric 1354632Seric /* 1366980Seric ** If there is no data file yet, create one. 1376980Seric */ 1386980Seric 1396980Seric if (e->e_df == NULL) 1406980Seric { 1416980Seric register FILE *dfp; 1429389Seric extern putbody(); 1436980Seric 14464086Seric e->e_df = queuename(e, 'd'); 14564086Seric e->e_df = newstr(e->e_df); 14640934Srick fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode); 14740934Srick if (fd < 0) 14858690Seric syserr("!queueup: cannot create %s", e->e_df); 14940934Srick dfp = fdopen(fd, "w"); 15059730Seric (*e->e_putbody)(dfp, FileMailer, e, NULL); 15158680Seric (void) xfclose(dfp, "queueup dfp", e->e_id); 1529389Seric e->e_putbody = putbody; 1536980Seric } 1546980Seric 1556980Seric /* 1564632Seric ** Output future work requests. 15725687Seric ** Priority and creation time should be first, since 15825687Seric ** they are required by orderq. 1594632Seric */ 1604632Seric 1619377Seric /* output message priority */ 1629377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 1639377Seric 1649630Seric /* output creation time */ 1659630Seric fprintf(tfp, "T%ld\n", e->e_ctime); 1669630Seric 16759093Seric /* output type and name of data file */ 16859093Seric if (e->e_bodytype != NULL) 16959093Seric fprintf(tfp, "B%s\n", e->e_bodytype); 1707812Seric fprintf(tfp, "D%s\n", e->e_df); 1714632Seric 17210108Seric /* message from envelope, if it exists */ 17310108Seric if (e->e_message != NULL) 17410108Seric fprintf(tfp, "M%s\n", e->e_message); 17510108Seric 17658737Seric /* send various flag bits through */ 17758737Seric p = buf; 17858737Seric if (bitset(EF_WARNING, e->e_flags)) 17958737Seric *p++ = 'w'; 18058737Seric if (bitset(EF_RESPONSE, e->e_flags)) 18158737Seric *p++ = 'r'; 18258737Seric *p++ = '\0'; 18358737Seric if (buf[0] != '\0') 18458737Seric fprintf(tfp, "F%s\n", buf); 18558737Seric 18658957Seric /* $r and $s and $_ macro values */ 18753400Seric if ((p = macvalue('r', e)) != NULL) 18853400Seric fprintf(tfp, "$r%s\n", p); 18953400Seric if ((p = macvalue('s', e)) != NULL) 19053400Seric fprintf(tfp, "$s%s\n", p); 19158957Seric if ((p = macvalue('_', e)) != NULL) 19258957Seric fprintf(tfp, "$_%s\n", p); 19353400Seric 1944632Seric /* output name of sender */ 1957812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 1964632Seric 19755360Seric /* output list of error recipients */ 19859670Seric printctladdr(NULL, NULL); 19955360Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 20055360Seric { 20158680Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 20255360Seric { 20359113Seric printctladdr(q, tfp); 20455360Seric fprintf(tfp, "E%s\n", q->q_paddr); 20555360Seric } 20655360Seric } 20755360Seric 2084632Seric /* output list of recipient addresses */ 2096980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 2104632Seric { 21158250Seric if (bitset(QQUEUEUP, q->q_flags) || 21258680Seric (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags))) 2138245Seric { 21459113Seric printctladdr(q, tfp); 2157812Seric fprintf(tfp, "R%s\n", q->q_paddr); 2169377Seric if (announce) 2179377Seric { 2189377Seric e->e_to = q->q_paddr; 21958151Seric message("queued"); 22058020Seric if (LogLevel > 8) 22158337Seric logdelivery(NULL, NULL, "queued", e); 2229377Seric e->e_to = NULL; 2239377Seric } 2249387Seric if (tTd(40, 1)) 2259387Seric { 2269387Seric printf("queueing "); 2279387Seric printaddr(q, FALSE); 2289387Seric } 2298245Seric } 2304632Seric } 2314632Seric 2329377Seric /* 2339377Seric ** Output headers for this message. 2349377Seric ** Expand macros completely here. Queue run will deal with 2359377Seric ** everything as absolute headers. 2369377Seric ** All headers that must be relative to the recipient 2379377Seric ** can be cracked later. 23810173Seric ** We set up a "null mailer" -- i.e., a mailer that will have 23910173Seric ** no effect on the addresses as they are output. 2409377Seric */ 2419377Seric 24210686Seric bzero((char *) &nullmailer, sizeof nullmailer); 24358020Seric nullmailer.m_re_rwset = nullmailer.m_rh_rwset = 24458020Seric nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1; 24510349Seric nullmailer.m_eol = "\n"; 24610173Seric 24758050Seric define('g', "\201f", e); 2486980Seric for (h = e->e_header; h != NULL; h = h->h_link) 2494632Seric { 25010686Seric extern bool bitzerop(); 25110686Seric 25212015Seric /* don't output null headers */ 2534632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 2544632Seric continue; 25512015Seric 25612015Seric /* don't output resent headers on non-resent messages */ 25712015Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 25812015Seric continue; 25912015Seric 26012015Seric /* output this header */ 2617812Seric fprintf(tfp, "H"); 26212015Seric 26312015Seric /* if conditional, output the set of conditions */ 26410686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 26510686Seric { 26610686Seric int j; 26710686Seric 26823098Seric (void) putc('?', tfp); 26910686Seric for (j = '\0'; j <= '\177'; j++) 27010686Seric if (bitnset(j, h->h_mflags)) 27123098Seric (void) putc(j, tfp); 27223098Seric (void) putc('?', tfp); 27310686Seric } 27412015Seric 27512015Seric /* output the header: expand macros, convert addresses */ 2767763Seric if (bitset(H_DEFAULT, h->h_flags)) 2777763Seric { 2787763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 2798236Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 2807763Seric } 2818245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 2829348Seric { 28358737Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 28463753Seric FILE *savetrace = TrafficLogFile; 28558737Seric 28663753Seric TrafficLogFile = NULL; 28763753Seric 28858737Seric if (bitset(H_FROM, h->h_flags)) 28958737Seric oldstyle = FALSE; 29058737Seric 29158737Seric commaize(h, h->h_value, tfp, oldstyle, 29255012Seric &nullmailer, e); 29363753Seric 29463753Seric TrafficLogFile = savetrace; 2959348Seric } 2967763Seric else 2978245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 2984632Seric } 2994632Seric 3004632Seric /* 3014632Seric ** Clean up. 3024632Seric */ 3034632Seric 30458732Seric fflush(tfp); 30560603Seric fsync(fileno(tfp)); 30658732Seric if (ferror(tfp)) 30758732Seric { 30858732Seric if (newid) 30958732Seric syserr("!552 Error writing control file %s", tf); 31058732Seric else 31158732Seric syserr("!452 Error writing control file %s", tf); 31258732Seric } 31358732Seric 31451920Seric if (!newid) 31551920Seric { 316*64277Seric /* rename (locked) tf to be (locked) qf */ 31751920Seric qf = queuename(e, 'q'); 31851920Seric if (rename(tf, qf) < 0) 31951920Seric syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df); 320*64277Seric 321*64277Seric /* close and unlock old (locked) qf */ 32251920Seric if (e->e_lockfp != NULL) 32358680Seric (void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id); 32451920Seric e->e_lockfp = tfp; 32551920Seric } 32651920Seric else 32751920Seric qf = tf; 32841636Srick errno = 0; 3297391Seric 3307677Seric # ifdef LOG 3317677Seric /* save log info */ 33258020Seric if (LogLevel > 79) 3337878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 33456795Seric # endif /* LOG */ 33551920Seric return; 3364632Seric } 33754974Seric 33854974Seric printctladdr(a, tfp) 33959113Seric register ADDRESS *a; 34054974Seric FILE *tfp; 34154974Seric { 34259113Seric char *uname; 34359113Seric register struct passwd *pw; 34459113Seric register ADDRESS *q; 34559113Seric uid_t uid; 34659113Seric static ADDRESS *lastctladdr; 34759113Seric static uid_t lastuid; 34854974Seric 34959113Seric /* initialization */ 35063850Seric if (a == NULL || a->q_alias == NULL || tfp == NULL) 35154974Seric { 35259670Seric if (lastctladdr != NULL && tfp != NULL) 35359113Seric fprintf(tfp, "C\n"); 35459113Seric lastctladdr = NULL; 35559113Seric lastuid = 0; 35654974Seric return; 35754974Seric } 35859113Seric 35959113Seric /* find the active uid */ 36059113Seric q = getctladdr(a); 36159113Seric if (q == NULL) 36259113Seric uid = 0; 36354974Seric else 36459113Seric uid = q->q_uid; 36563850Seric a = a->q_alias; 36659113Seric 36759113Seric /* check to see if this is the same as last time */ 36859113Seric if (lastctladdr != NULL && uid == lastuid && 36959113Seric strcmp(lastctladdr->q_paddr, a->q_paddr) == 0) 37059113Seric return; 37159113Seric lastuid = uid; 37259113Seric lastctladdr = a; 37359113Seric 37459113Seric if (uid == 0 || (pw = getpwuid(uid)) == NULL) 37559270Seric uname = ""; 37659113Seric else 37759113Seric uname = pw->pw_name; 37859113Seric 37959113Seric fprintf(tfp, "C%s:%s\n", uname, a->q_paddr); 38054974Seric } 38154974Seric 3824632Seric /* 3834632Seric ** RUNQUEUE -- run the jobs in the queue. 3844632Seric ** 3854632Seric ** Gets the stuff out of the queue in some presumably logical 3864632Seric ** order and processes them. 3874632Seric ** 3884632Seric ** Parameters: 38924941Seric ** forkflag -- TRUE if the queue scanning should be done in 39024941Seric ** a child process. We double-fork so it is not our 39124941Seric ** child and we don't have to clean up after it. 3924632Seric ** 3934632Seric ** Returns: 3944632Seric ** none. 3954632Seric ** 3964632Seric ** Side Effects: 3974632Seric ** runs things in the mail queue. 3984632Seric */ 3994632Seric 40055360Seric ENVELOPE QueueEnvelope; /* the queue run envelope */ 40155360Seric 40255360Seric runqueue(forkflag) 4034639Seric bool forkflag; 4044632Seric { 40555360Seric register ENVELOPE *e; 40655360Seric extern ENVELOPE BlankEnvelope; 40724953Seric 4087466Seric /* 40924953Seric ** If no work will ever be selected, don't even bother reading 41024953Seric ** the queue. 41124953Seric */ 41224953Seric 41351920Seric CurrentLA = getla(); /* get load average */ 41440934Srick 41558132Seric if (shouldqueue(0L, curtime())) 41624953Seric { 41724953Seric if (Verbose) 41824953Seric printf("Skipping queue run -- load average too high\n"); 41958107Seric if (forkflag && QueueIntvl != 0) 42058107Seric (void) setevent(QueueIntvl, runqueue, TRUE); 42155360Seric return; 42224953Seric } 42324953Seric 42424953Seric /* 4257466Seric ** See if we want to go off and do other useful work. 4267466Seric */ 4274639Seric 4284639Seric if (forkflag) 4294639Seric { 4307943Seric int pid; 4317943Seric 4327943Seric pid = dofork(); 4337943Seric if (pid != 0) 4344639Seric { 43546928Sbostic extern void reapchild(); 43625184Seric 4377943Seric /* parent -- pick up intermediate zombie */ 43825184Seric #ifndef SIGCHLD 4399377Seric (void) waitfor(pid); 44056795Seric #else /* SIGCHLD */ 44164035Seric (void) setsignal(SIGCHLD, reapchild); 44256795Seric #endif /* SIGCHLD */ 4437690Seric if (QueueIntvl != 0) 4449348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 4454639Seric return; 4464639Seric } 4477943Seric /* child -- double fork */ 44825184Seric #ifndef SIGCHLD 4497943Seric if (fork() != 0) 4507943Seric exit(EX_OK); 45156795Seric #else /* SIGCHLD */ 45264035Seric (void) setsignal(SIGCHLD, SIG_DFL); 45356795Seric #endif /* SIGCHLD */ 4544639Seric } 45524941Seric 45640934Srick setproctitle("running queue: %s", QueueDir); 45724941Seric 4587876Seric # ifdef LOG 45958020Seric if (LogLevel > 69) 46055360Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 46155360Seric QueueDir, getpid(), forkflag); 46256795Seric # endif /* LOG */ 4634639Seric 4647466Seric /* 46510205Seric ** Release any resources used by the daemon code. 46610205Seric */ 46710205Seric 46810205Seric # ifdef DAEMON 46910205Seric clrdaemon(); 47056795Seric # endif /* DAEMON */ 47110205Seric 47210205Seric /* 47355360Seric ** Create ourselves an envelope 47455360Seric */ 47555360Seric 47655360Seric CurEnv = &QueueEnvelope; 47758179Seric e = newenvelope(&QueueEnvelope, CurEnv); 47855360Seric e->e_flags = BlankEnvelope.e_flags; 47955360Seric 48055360Seric /* 48127175Seric ** Make sure the alias database is open. 48227175Seric */ 48327175Seric 48460537Seric initmaps(FALSE, e); 48527175Seric 48627175Seric /* 4877466Seric ** Start making passes through the queue. 4887466Seric ** First, read and sort the entire queue. 4897466Seric ** Then, process the work in that order. 4907466Seric ** But if you take too long, start over. 4917466Seric */ 4927466Seric 4937943Seric /* order the existing work requests */ 49424954Seric (void) orderq(FALSE); 4957690Seric 4967943Seric /* process them once at a time */ 4977943Seric while (WorkQ != NULL) 4984639Seric { 4997943Seric WORK *w = WorkQ; 5007881Seric 5017943Seric WorkQ = WorkQ->w_next; 50258884Seric 50358884Seric /* 50458884Seric ** Ignore jobs that are too expensive for the moment. 50558884Seric */ 50658884Seric 50758884Seric if (shouldqueue(w->w_pri, w->w_ctime)) 50858884Seric { 50958884Seric if (Verbose) 51058884Seric printf("\nSkipping %s\n", w->w_name + 2); 51158884Seric } 51258930Seric else 51358930Seric { 51458930Seric dowork(w->w_name + 2, ForkQueueRuns, FALSE, e); 51558930Seric } 5167943Seric free(w->w_name); 5177943Seric free((char *) w); 5184639Seric } 51929866Seric 52029866Seric /* exit without the usual cleanup */ 52155467Seric e->e_id = NULL; 52255467Seric finis(); 5234634Seric } 5244634Seric /* 5254632Seric ** ORDERQ -- order the work queue. 5264632Seric ** 5274632Seric ** Parameters: 52824941Seric ** doall -- if set, include everything in the queue (even 52924941Seric ** the jobs that cannot be run because the load 53024941Seric ** average is too high). Otherwise, exclude those 53124941Seric ** jobs. 5324632Seric ** 5334632Seric ** Returns: 53410121Seric ** The number of request in the queue (not necessarily 53510121Seric ** the number of requests in WorkQ however). 5364632Seric ** 5374632Seric ** Side Effects: 5384632Seric ** Sets WorkQ to the queue of available work, in order. 5394632Seric */ 5404632Seric 54125687Seric # define NEED_P 001 54225687Seric # define NEED_T 002 54358318Seric # define NEED_R 004 54458318Seric # define NEED_S 010 5454632Seric 54624941Seric orderq(doall) 54724941Seric bool doall; 5484632Seric { 54960219Seric register struct dirent *d; 5504632Seric register WORK *w; 5516625Sglickman DIR *f; 5524632Seric register int i; 55325687Seric WORK wlist[QUEUESIZE+1]; 55410070Seric int wn = -1; 5554632Seric extern workcmpf(); 5564632Seric 55758318Seric if (tTd(41, 1)) 55858318Seric { 55958318Seric printf("orderq:\n"); 56058318Seric if (QueueLimitId != NULL) 56158318Seric printf("\tQueueLimitId = %s\n", QueueLimitId); 56258318Seric if (QueueLimitSender != NULL) 56358318Seric printf("\tQueueLimitSender = %s\n", QueueLimitSender); 56458318Seric if (QueueLimitRecipient != NULL) 56558318Seric printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 56658318Seric } 56758318Seric 5684632Seric /* clear out old WorkQ */ 5694632Seric for (w = WorkQ; w != NULL; ) 5704632Seric { 5714632Seric register WORK *nw = w->w_next; 5724632Seric 5734632Seric WorkQ = nw; 5744632Seric free(w->w_name); 5754632Seric free((char *) w); 5764632Seric w = nw; 5774632Seric } 5784632Seric 5794632Seric /* open the queue directory */ 5808148Seric f = opendir("."); 5814632Seric if (f == NULL) 5824632Seric { 5838148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 58410070Seric return (0); 5854632Seric } 5864632Seric 5874632Seric /* 5884632Seric ** Read the work directory. 5894632Seric */ 5904632Seric 59110070Seric while ((d = readdir(f)) != NULL) 5924632Seric { 5939377Seric FILE *cf; 5944632Seric char lbuf[MAXNAME]; 59558318Seric extern bool strcontainedin(); 5964632Seric 5974632Seric /* is this an interesting entry? */ 5987812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 5994632Seric continue; 6004632Seric 60158318Seric if (QueueLimitId != NULL && 60258318Seric !strcontainedin(QueueLimitId, d->d_name)) 60358318Seric continue; 60458318Seric 60558722Seric /* 60658722Seric ** Check queue name for plausibility. This handles 60758722Seric ** both old and new type ids. 60858722Seric */ 60958722Seric 61058722Seric i = strlen(d->d_name); 61158722Seric if (i != 9 && i != 10) 61258020Seric { 61358020Seric if (Verbose) 61458020Seric printf("orderq: bogus qf name %s\n", d->d_name); 61558020Seric #ifdef LOG 61658020Seric if (LogLevel > 3) 61759615Seric syslog(LOG_CRIT, "orderq: bogus qf name %s", 61858020Seric d->d_name); 61958020Seric #endif 62058020Seric if (strlen(d->d_name) >= MAXNAME) 62158020Seric d->d_name[MAXNAME - 1] = '\0'; 62258020Seric strcpy(lbuf, d->d_name); 62358020Seric lbuf[0] = 'Q'; 62458020Seric (void) rename(d->d_name, lbuf); 62558020Seric continue; 62658020Seric } 62758020Seric 62810070Seric /* yes -- open control file (if not too many files) */ 62925687Seric if (++wn >= QUEUESIZE) 63010070Seric continue; 63158318Seric 6328148Seric cf = fopen(d->d_name, "r"); 6334632Seric if (cf == NULL) 6344632Seric { 6357055Seric /* this may be some random person sending hir msgs */ 6367055Seric /* syserr("orderq: cannot open %s", cbuf); */ 63710090Seric if (tTd(41, 2)) 63810090Seric printf("orderq: cannot open %s (%d)\n", 63910090Seric d->d_name, errno); 6407055Seric errno = 0; 64110090Seric wn--; 6424632Seric continue; 6434632Seric } 64425687Seric w = &wlist[wn]; 64525687Seric w->w_name = newstr(d->d_name); 6464632Seric 64725027Seric /* make sure jobs in creation don't clog queue */ 64825687Seric w->w_pri = 0x7fffffff; 64925687Seric w->w_ctime = 0; 65025027Seric 6514632Seric /* extract useful information */ 65225687Seric i = NEED_P | NEED_T; 65358318Seric if (QueueLimitSender != NULL) 65458318Seric i |= NEED_S; 65558318Seric if (QueueLimitRecipient != NULL) 65658318Seric i |= NEED_R; 65725687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 6584632Seric { 65924954Seric extern long atol(); 66058318Seric extern bool strcontainedin(); 66124954Seric 66224941Seric switch (lbuf[0]) 6634632Seric { 66424941Seric case 'P': 66525687Seric w->w_pri = atol(&lbuf[1]); 66625687Seric i &= ~NEED_P; 6674632Seric break; 66825013Seric 66925013Seric case 'T': 67025687Seric w->w_ctime = atol(&lbuf[1]); 67125687Seric i &= ~NEED_T; 67225013Seric break; 67358318Seric 67458318Seric case 'R': 67558318Seric if (QueueLimitRecipient != NULL && 67658318Seric strcontainedin(QueueLimitRecipient, &lbuf[1])) 67758318Seric i &= ~NEED_R; 67858318Seric break; 67958318Seric 68058318Seric case 'S': 68158318Seric if (QueueLimitSender != NULL && 68258318Seric strcontainedin(QueueLimitSender, &lbuf[1])) 68358318Seric i &= ~NEED_S; 68458318Seric break; 6854632Seric } 6864632Seric } 6874632Seric (void) fclose(cf); 68824953Seric 68958318Seric if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 69058318Seric bitset(NEED_R|NEED_S, i)) 69124953Seric { 69224953Seric /* don't even bother sorting this job in */ 69324953Seric wn--; 69424953Seric } 6954632Seric } 6966625Sglickman (void) closedir(f); 69710090Seric wn++; 6984632Seric 6994632Seric /* 7004632Seric ** Sort the work directory. 7014632Seric */ 7024632Seric 70325687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 7044632Seric 7054632Seric /* 7064632Seric ** Convert the work list into canonical form. 7079377Seric ** Should be turning it into a list of envelopes here perhaps. 7084632Seric */ 7094632Seric 71024981Seric WorkQ = NULL; 71125687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 7124632Seric { 7134632Seric w = (WORK *) xalloc(sizeof *w); 7144632Seric w->w_name = wlist[i].w_name; 7154632Seric w->w_pri = wlist[i].w_pri; 71625013Seric w->w_ctime = wlist[i].w_ctime; 71724981Seric w->w_next = WorkQ; 71824981Seric WorkQ = w; 7194632Seric } 7204632Seric 7217677Seric if (tTd(40, 1)) 7224632Seric { 7234632Seric for (w = WorkQ; w != NULL; w = w->w_next) 7245037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 7254632Seric } 72610070Seric 72710090Seric return (wn); 7284632Seric } 7294632Seric /* 7307677Seric ** WORKCMPF -- compare function for ordering work. 7314632Seric ** 7324632Seric ** Parameters: 7334632Seric ** a -- the first argument. 7344632Seric ** b -- the second argument. 7354632Seric ** 7364632Seric ** Returns: 73724981Seric ** -1 if a < b 73824981Seric ** 0 if a == b 73924981Seric ** +1 if a > b 7404632Seric ** 7414632Seric ** Side Effects: 7424632Seric ** none. 7434632Seric */ 7444632Seric 7454632Seric workcmpf(a, b) 7465037Seric register WORK *a; 7475037Seric register WORK *b; 7484632Seric { 74957438Seric long pa = a->w_pri; 75057438Seric long pb = b->w_pri; 75124941Seric 75224941Seric if (pa == pb) 7534632Seric return (0); 75424941Seric else if (pa > pb) 75524981Seric return (1); 75624981Seric else 75710121Seric return (-1); 7584632Seric } 7594632Seric /* 7604632Seric ** DOWORK -- do a work request. 7614632Seric ** 7624632Seric ** Parameters: 76358884Seric ** id -- the ID of the job to run. 76458884Seric ** forkflag -- if set, run this in background. 76558924Seric ** requeueflag -- if set, reinstantiate the queue quickly. 76658924Seric ** This is used when expanding aliases in the queue. 76761094Seric ** If forkflag is also set, it doesn't wait for the 76861094Seric ** child. 76958884Seric ** e - the envelope in which to run it. 7704632Seric ** 7714632Seric ** Returns: 7724632Seric ** none. 7734632Seric ** 7744632Seric ** Side Effects: 7754632Seric ** The work request is satisfied if possible. 7764632Seric */ 7774632Seric 77858924Seric dowork(id, forkflag, requeueflag, e) 77958884Seric char *id; 78058884Seric bool forkflag; 78158924Seric bool requeueflag; 78255012Seric register ENVELOPE *e; 7834632Seric { 7844632Seric register int i; 78551920Seric extern bool readqf(); 7864632Seric 7877677Seric if (tTd(40, 1)) 78858884Seric printf("dowork(%s)\n", id); 7894632Seric 7904632Seric /* 79124941Seric ** Fork for work. 79224941Seric */ 79324941Seric 79458884Seric if (forkflag) 79524941Seric { 79624941Seric i = fork(); 79724941Seric if (i < 0) 79824941Seric { 79924941Seric syserr("dowork: cannot fork"); 80024941Seric return; 80124941Seric } 80224941Seric } 80324941Seric else 80424941Seric { 80524941Seric i = 0; 80624941Seric } 80724941Seric 8084632Seric if (i == 0) 8094632Seric { 8104632Seric /* 8114632Seric ** CHILD 8128148Seric ** Lock the control file to avoid duplicate deliveries. 8138148Seric ** Then run the file as though we had just read it. 8147350Seric ** We save an idea of the temporary name so we 8157350Seric ** can recover on interrupt. 8164632Seric */ 8174632Seric 8187763Seric /* set basic modes, etc. */ 8197356Seric (void) alarm(0); 82055012Seric clearenvelope(e, FALSE); 82158737Seric e->e_flags |= EF_QUEUERUN; 82258734Seric e->e_errormode = EM_MAIL; 82358884Seric e->e_id = id; 82463846Seric if (forkflag) 82563846Seric disconnect(0, e); 8267876Seric # ifdef LOG 82758020Seric if (LogLevel > 76) 82855012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 8297881Seric getpid()); 83056795Seric # endif /* LOG */ 8317763Seric 8327763Seric /* don't use the headers from sendmail.cf... */ 83355012Seric e->e_header = NULL; 8347763Seric 83551920Seric /* read the queue control file -- return if locked */ 83663850Seric if (!readqf(e, !requeueflag)) 8376980Seric { 83858884Seric if (tTd(40, 4)) 83958884Seric printf("readqf(%s) failed\n", e->e_id); 84058884Seric if (forkflag) 84124941Seric exit(EX_OK); 84224941Seric else 84324941Seric return; 8446980Seric } 8456980Seric 84655012Seric e->e_flags |= EF_INQUEUE; 84758929Seric eatheader(e, requeueflag); 8486980Seric 84958924Seric if (requeueflag) 85058924Seric queueup(e, TRUE, FALSE); 85158924Seric 8526980Seric /* do the delivery */ 85358915Seric sendall(e, SM_DELIVER); 8546980Seric 8556980Seric /* finish up and exit */ 85658884Seric if (forkflag) 85724941Seric finis(); 85824941Seric else 85955012Seric dropenvelope(e); 8604632Seric } 86161094Seric else if (!requeueflag) 86224941Seric { 86324941Seric /* 86424941Seric ** Parent -- pick up results. 86524941Seric */ 8664632Seric 86724941Seric errno = 0; 86824941Seric (void) waitfor(i); 86924941Seric } 8704632Seric } 8714632Seric /* 8724632Seric ** READQF -- read queue file and set up environment. 8734632Seric ** 8744632Seric ** Parameters: 8759377Seric ** e -- the envelope of the job to run. 87663850Seric ** announcefile -- if set, announce the name of the queue 87763850Seric ** file in error messages. 8784632Seric ** 8794632Seric ** Returns: 88051920Seric ** TRUE if it successfully read the queue file. 88151920Seric ** FALSE otherwise. 8824632Seric ** 8834632Seric ** Side Effects: 88451920Seric ** The queue file is returned locked. 8854632Seric */ 8864632Seric 88751920Seric bool 88863850Seric readqf(e, announcefile) 8899377Seric register ENVELOPE *e; 89063850Seric bool announcefile; 8914632Seric { 89217477Seric register FILE *qfp; 89354974Seric ADDRESS *ctladdr; 89456400Seric struct stat st; 89557135Seric char *bp; 89658915Seric char qf[20]; 89757135Seric char buf[MAXLINE]; 89824954Seric extern long atol(); 89954974Seric extern ADDRESS *setctluser(); 9004632Seric 9014632Seric /* 90217468Seric ** Read and process the file. 9034632Seric */ 9044632Seric 90558915Seric strcpy(qf, queuename(e, 'q')); 90651937Seric qfp = fopen(qf, "r+"); 90717477Seric if (qfp == NULL) 90817477Seric { 90958884Seric if (tTd(40, 8)) 91058884Seric printf("readqf(%s): fopen failure (%s)\n", 91158884Seric qf, errstring(errno)); 91240934Srick if (errno != ENOENT) 91340934Srick syserr("readqf: no control file %s", qf); 91451920Seric return FALSE; 91517477Seric } 91640934Srick 917*64277Seric if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB)) 918*64277Seric { 919*64277Seric /* being processed by another queuer */ 920*64277Seric if (tTd(40, 8)) 921*64277Seric printf("readqf(%s): locked\n", qf); 922*64277Seric if (Verbose) 923*64277Seric printf("%s: locked\n", e->e_id); 924*64277Seric # ifdef LOG 925*64277Seric if (LogLevel > 19) 926*64277Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 927*64277Seric # endif /* LOG */ 928*64277Seric (void) fclose(qfp); 929*64277Seric return FALSE; 930*64277Seric } 931*64277Seric 93256400Seric /* 93356400Seric ** Check the queue file for plausibility to avoid attacks. 93456400Seric */ 93556400Seric 93656400Seric if (fstat(fileno(qfp), &st) < 0) 93756400Seric { 93856400Seric /* must have been being processed by someone else */ 93958884Seric if (tTd(40, 8)) 94058884Seric printf("readqf(%s): fstat failure (%s)\n", 94158884Seric qf, errstring(errno)); 94256400Seric fclose(qfp); 94356400Seric return FALSE; 94456400Seric } 94556400Seric 94664137Seric if (st.st_uid != geteuid()) 94756400Seric { 94856400Seric # ifdef LOG 94956400Seric if (LogLevel > 0) 95056400Seric { 95156400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 95256400Seric e->e_id, st.st_uid, st.st_mode); 95356400Seric } 95456795Seric # endif /* LOG */ 95558884Seric if (tTd(40, 8)) 95658884Seric printf("readqf(%s): bogus file\n", qf); 957*64277Seric rename(qf, queuename(e, 'Q')); 95856400Seric fclose(qfp); 95956400Seric return FALSE; 96056400Seric } 96156400Seric 962*64277Seric if (st.st_size == 0) 96340934Srick { 964*64277Seric /* must be a bogus file -- just remove it */ 965*64277Seric (void) unlink(qf); 966*64277Seric fclose(qfp); 96751920Seric return FALSE; 96840934Srick } 96940934Srick 970*64277Seric if (st.st_nlink == 0) 97159101Seric { 972*64277Seric /* 973*64277Seric ** Race condition -- we got a file just as it was being 974*64277Seric ** unlinked. Just assume it is zero length. 975*64277Seric */ 976*64277Seric 97759101Seric fclose(qfp); 97859101Seric return FALSE; 97959101Seric } 98059101Seric 981*64277Seric /* good file -- save this lock */ 98251920Seric e->e_lockfp = qfp; 98351920Seric 98440934Srick /* do basic system initialization */ 98555012Seric initsys(e); 98640934Srick 98763850Seric if (announcefile) 98863850Seric FileName = qf; 9899377Seric LineNumber = 0; 99063850Seric e->e_flags |= EF_GLOBALERRS; 99163850Seric OpMode = MD_DELIVER; 99251920Seric if (Verbose) 9939377Seric printf("\nRunning %s\n", e->e_id); 99454974Seric ctladdr = NULL; 99557135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 9964632Seric { 99758737Seric register char *p; 99857529Seric struct stat st; 99957529Seric 100026504Seric if (tTd(40, 4)) 100157135Seric printf("+++++ %s\n", bp); 100257135Seric switch (bp[0]) 10034632Seric { 100440973Sbostic case 'C': /* specify controlling user */ 100557135Seric ctladdr = setctluser(&bp[1]); 100640973Sbostic break; 100740973Sbostic 10084632Seric case 'R': /* specify recipient */ 100958082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 10104632Seric break; 10114632Seric 101225687Seric case 'E': /* specify error recipient */ 101358082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 101425687Seric break; 101525687Seric 10164632Seric case 'H': /* header */ 101757135Seric (void) chompheader(&bp[1], FALSE, e); 10184632Seric break; 10194632Seric 102010108Seric case 'M': /* message */ 102157135Seric e->e_message = newstr(&bp[1]); 102210108Seric break; 102310108Seric 10244632Seric case 'S': /* sender */ 102558704Seric setsender(newstr(&bp[1]), e, NULL, TRUE); 10264632Seric break; 10274632Seric 102859093Seric case 'B': /* body type */ 102959093Seric e->e_bodytype = newstr(&bp[1]); 103059093Seric break; 103159093Seric 10324632Seric case 'D': /* data file name */ 103357135Seric e->e_df = newstr(&bp[1]); 10349544Seric e->e_dfp = fopen(e->e_df, "r"); 10359544Seric if (e->e_dfp == NULL) 103658020Seric { 10379377Seric syserr("readqf: cannot open %s", e->e_df); 103858020Seric e->e_msgsize = -1; 103958020Seric } 104058020Seric else if (fstat(fileno(e->e_dfp), &st) >= 0) 104157529Seric e->e_msgsize = st.st_size; 10424632Seric break; 10434632Seric 10447860Seric case 'T': /* init time */ 104557135Seric e->e_ctime = atol(&bp[1]); 10464632Seric break; 10474632Seric 10484634Seric case 'P': /* message priority */ 104957135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 10504634Seric break; 10514634Seric 105258737Seric case 'F': /* flag bits */ 105358737Seric for (p = &bp[1]; *p != '\0'; p++) 105458737Seric { 105558737Seric switch (*p) 105658737Seric { 105758737Seric case 'w': /* warning sent */ 105858737Seric e->e_flags |= EF_WARNING; 105958737Seric break; 106058737Seric 106158737Seric case 'r': /* response */ 106258737Seric e->e_flags |= EF_RESPONSE; 106358737Seric break; 106458737Seric } 106558737Seric } 106658737Seric break; 106758737Seric 106853400Seric case '$': /* define macro */ 106957135Seric define(bp[1], newstr(&bp[2]), e); 107053400Seric break; 107153400Seric 107224941Seric case '\0': /* blank line; ignore */ 107324941Seric break; 107424941Seric 10754632Seric default: 107659700Seric syserr("readqf: bad line \"%s\"", e->e_id, 107757135Seric LineNumber, bp); 107863753Seric fclose(qfp); 107963753Seric rename(qf, queuename(e, 'Q')); 108063753Seric return FALSE; 10814632Seric } 108257135Seric 108357135Seric if (bp != buf) 108457135Seric free(bp); 10854632Seric } 10869377Seric 10879377Seric FileName = NULL; 108824941Seric 108924941Seric /* 109024941Seric ** If we haven't read any lines, this queue file is empty. 109124941Seric ** Arrange to remove it without referencing any null pointers. 109224941Seric */ 109324941Seric 109424941Seric if (LineNumber == 0) 109524941Seric { 109624941Seric errno = 0; 109724941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 109824941Seric } 109951920Seric return TRUE; 11004632Seric } 11014632Seric /* 11029630Seric ** PRINTQUEUE -- print out a representation of the mail queue 11039630Seric ** 11049630Seric ** Parameters: 11059630Seric ** none. 11069630Seric ** 11079630Seric ** Returns: 11089630Seric ** none. 11099630Seric ** 11109630Seric ** Side Effects: 11119630Seric ** Prints a listing of the mail queue on the standard output. 11129630Seric */ 11135182Seric 11149630Seric printqueue() 11159630Seric { 11169630Seric register WORK *w; 11179630Seric FILE *f; 111810070Seric int nrequests; 11199630Seric char buf[MAXLINE]; 11209630Seric 11219630Seric /* 112258250Seric ** Check for permission to print the queue 112358250Seric */ 112458250Seric 112563787Seric if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && RealUid != 0) 112658250Seric { 112758250Seric struct stat st; 112858523Seric # ifdef NGROUPS 112958523Seric int n; 113063937Seric GIDSET_T gidset[NGROUPS]; 113158523Seric # endif 113258250Seric 113358517Seric if (stat(QueueDir, &st) < 0) 113458250Seric { 113558250Seric syserr("Cannot stat %s", QueueDir); 113658250Seric return; 113758250Seric } 113858523Seric # ifdef NGROUPS 113958523Seric n = getgroups(NGROUPS, gidset); 114058523Seric while (--n >= 0) 114158523Seric { 114258523Seric if (gidset[n] == st.st_gid) 114358523Seric break; 114458523Seric } 114558523Seric if (n < 0) 114658523Seric # else 114763787Seric if (RealGid != st.st_gid) 114858523Seric # endif 114958250Seric { 115058250Seric usrerr("510 You are not permitted to see the queue"); 115158250Seric setstat(EX_NOPERM); 115258250Seric return; 115358250Seric } 115458250Seric } 115558250Seric 115658250Seric /* 11579630Seric ** Read and order the queue. 11589630Seric */ 11599630Seric 116024941Seric nrequests = orderq(TRUE); 11619630Seric 11629630Seric /* 11639630Seric ** Print the work list that we have read. 11649630Seric */ 11659630Seric 11669630Seric /* first see if there is anything */ 116710070Seric if (nrequests <= 0) 11689630Seric { 116910070Seric printf("Mail queue is empty\n"); 11709630Seric return; 11719630Seric } 11729630Seric 117351920Seric CurrentLA = getla(); /* get load average */ 117440934Srick 117510096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 117625687Seric if (nrequests > QUEUESIZE) 117725687Seric printf(", only %d printed", QUEUESIZE); 117824979Seric if (Verbose) 117958716Seric printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 118024979Seric else 118158716Seric printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 11829630Seric for (w = WorkQ; w != NULL; w = w->w_next) 11839630Seric { 11849630Seric struct stat st; 118510070Seric auto time_t submittime = 0; 118610070Seric long dfsize = -1; 118758737Seric int flags = 0; 118810108Seric char message[MAXLINE]; 118959093Seric char bodytype[MAXNAME]; 11909630Seric 119117468Seric f = fopen(w->w_name, "r"); 119217468Seric if (f == NULL) 119317468Seric { 119417468Seric errno = 0; 119517468Seric continue; 119617468Seric } 119758724Seric printf("%8s", w->w_name + 2); 119858689Seric if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB)) 119910070Seric printf("*"); 120057438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 120124941Seric printf("X"); 120210070Seric else 120310070Seric printf(" "); 120410070Seric errno = 0; 120517468Seric 120659093Seric message[0] = bodytype[0] = '\0'; 12079630Seric while (fgets(buf, sizeof buf, f) != NULL) 12089630Seric { 120953400Seric register int i; 121058737Seric register char *p; 121153400Seric 12129630Seric fixcrlf(buf, TRUE); 12139630Seric switch (buf[0]) 12149630Seric { 121510108Seric case 'M': /* error message */ 121653400Seric if ((i = strlen(&buf[1])) >= sizeof message) 121758737Seric i = sizeof message - 1; 121853400Seric bcopy(&buf[1], message, i); 121953400Seric message[i] = '\0'; 122010108Seric break; 122110108Seric 122259093Seric case 'B': /* body type */ 122359093Seric if ((i = strlen(&buf[1])) >= sizeof bodytype) 122459093Seric i = sizeof bodytype - 1; 122559093Seric bcopy(&buf[1], bodytype, i); 122659093Seric bodytype[i] = '\0'; 122759093Seric break; 122859093Seric 12299630Seric case 'S': /* sender name */ 123024979Seric if (Verbose) 123158737Seric printf("%8ld %10ld%c%.12s %.38s", 123258737Seric dfsize, 123358737Seric w->w_pri, 123458737Seric bitset(EF_WARNING, flags) ? '+' : ' ', 123558737Seric ctime(&submittime) + 4, 123624979Seric &buf[1]); 123724979Seric else 123824979Seric printf("%8ld %.16s %.45s", dfsize, 123924979Seric ctime(&submittime), &buf[1]); 124059093Seric if (message[0] != '\0' || bodytype[0] != '\0') 124159093Seric { 124259093Seric printf("\n %10.10s", bodytype); 124359093Seric if (message[0] != '\0') 124459093Seric printf(" (%.60s)", message); 124559093Seric } 12469630Seric break; 124751920Seric 124840973Sbostic case 'C': /* controlling user */ 124954974Seric if (Verbose) 125058716Seric printf("\n\t\t\t\t (---%.34s---)", 125158716Seric &buf[1]); 125240973Sbostic break; 12539630Seric 12549630Seric case 'R': /* recipient name */ 125524979Seric if (Verbose) 125658716Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 125724979Seric else 125858716Seric printf("\n\t\t\t\t %.45s", &buf[1]); 12599630Seric break; 12609630Seric 12619630Seric case 'T': /* creation time */ 126224941Seric submittime = atol(&buf[1]); 12639630Seric break; 126410070Seric 126510070Seric case 'D': /* data file name */ 126610070Seric if (stat(&buf[1], &st) >= 0) 126710070Seric dfsize = st.st_size; 126810070Seric break; 126958737Seric 127058737Seric case 'F': /* flag bits */ 127158737Seric for (p = &buf[1]; *p != '\0'; p++) 127258737Seric { 127358737Seric switch (*p) 127458737Seric { 127558737Seric case 'w': 127658737Seric flags |= EF_WARNING; 127758737Seric break; 127858737Seric } 127958737Seric } 12809630Seric } 12819630Seric } 128210070Seric if (submittime == (time_t) 0) 128310070Seric printf(" (no control file)"); 12849630Seric printf("\n"); 128523098Seric (void) fclose(f); 12869630Seric } 12879630Seric } 12889630Seric 128956795Seric # endif /* QUEUE */ 129017468Seric /* 129117468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 129217468Seric ** 129317468Seric ** Assigns an id code if one does not already exist. 129417468Seric ** This code is very careful to avoid trashing existing files 129517468Seric ** under any circumstances. 129617468Seric ** 129717468Seric ** Parameters: 129817468Seric ** e -- envelope to build it in/from. 129917468Seric ** type -- the file type, used as the first character 130017468Seric ** of the file name. 130117468Seric ** 130217468Seric ** Returns: 130317468Seric ** a pointer to the new file name (in a static buffer). 130417468Seric ** 130517468Seric ** Side Effects: 130651920Seric ** If no id code is already assigned, queuename will 130751920Seric ** assign an id code, create a qf file, and leave a 130851920Seric ** locked, open-for-write file pointer in the envelope. 130917468Seric */ 131017468Seric 131117468Seric char * 131217468Seric queuename(e, type) 131317468Seric register ENVELOPE *e; 131459700Seric int type; 131517468Seric { 131617468Seric static int pid = -1; 131758741Seric static char c0; 131858741Seric static char c1; 131958741Seric static char c2; 132058716Seric time_t now; 132158716Seric struct tm *tm; 132258689Seric static char buf[MAXNAME]; 132317468Seric 132417468Seric if (e->e_id == NULL) 132517468Seric { 132617468Seric char qf[20]; 132717468Seric 132817468Seric /* find a unique id */ 132917468Seric if (pid != getpid()) 133017468Seric { 133117468Seric /* new process -- start back at "AA" */ 133217468Seric pid = getpid(); 133358716Seric now = curtime(); 133458716Seric tm = localtime(&now); 133558716Seric c0 = 'A' + tm->tm_hour; 133617468Seric c1 = 'A'; 133717468Seric c2 = 'A' - 1; 133817468Seric } 133958716Seric (void) sprintf(qf, "qf%cAA%05d", c0, pid); 134017468Seric 134117468Seric while (c1 < '~' || c2 < 'Z') 134217468Seric { 134317468Seric int i; 134417468Seric 134517468Seric if (c2 >= 'Z') 134617468Seric { 134717468Seric c1++; 134817468Seric c2 = 'A' - 1; 134917468Seric } 135058716Seric qf[3] = c1; 135158716Seric qf[4] = ++c2; 135217468Seric if (tTd(7, 20)) 135340934Srick printf("queuename: trying \"%s\"\n", qf); 135417468Seric 135540934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 135651920Seric if (i < 0) 135751920Seric { 135851920Seric if (errno == EEXIST) 135951920Seric continue; 136051920Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 136151920Seric qf, QueueDir); 136251920Seric exit(EX_UNAVAILABLE); 136351920Seric } 136458689Seric if (lockfile(i, qf, LOCK_EX|LOCK_NB)) 136551920Seric { 136651920Seric e->e_lockfp = fdopen(i, "w"); 136740934Srick break; 136817468Seric } 136951920Seric 137051920Seric /* a reader got the file; abandon it and try again */ 137151920Seric (void) close(i); 137217468Seric } 137317468Seric if (c1 >= '~' && c2 >= 'Z') 137417468Seric { 137517468Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 137617468Seric qf, QueueDir); 137717468Seric exit(EX_OSERR); 137817468Seric } 137917468Seric e->e_id = newstr(&qf[2]); 138017468Seric define('i', e->e_id, e); 138117468Seric if (tTd(7, 1)) 138217468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 138317468Seric # ifdef LOG 138458020Seric if (LogLevel > 93) 138517468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 138656795Seric # endif /* LOG */ 138717468Seric } 138817468Seric 138917468Seric if (type == '\0') 139017468Seric return (NULL); 139117468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 139217468Seric if (tTd(7, 2)) 139317468Seric printf("queuename: %s\n", buf); 139417468Seric return (buf); 139517468Seric } 139617468Seric /* 139717468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 139817468Seric ** 139917468Seric ** Parameters: 140017468Seric ** e -- the envelope to unlock. 140117468Seric ** 140217468Seric ** Returns: 140317468Seric ** none 140417468Seric ** 140517468Seric ** Side Effects: 140617468Seric ** unlocks the queue for `e'. 140717468Seric */ 140817468Seric 140917468Seric unlockqueue(e) 141017468Seric ENVELOPE *e; 141117468Seric { 141258680Seric if (tTd(51, 4)) 141358680Seric printf("unlockqueue(%s)\n", e->e_id); 141458680Seric 141551920Seric /* if there is a lock file in the envelope, close it */ 141651920Seric if (e->e_lockfp != NULL) 141758680Seric xfclose(e->e_lockfp, "unlockqueue", e->e_id); 141851920Seric e->e_lockfp = NULL; 141951920Seric 142058728Seric /* don't create a queue id if we don't already have one */ 142158728Seric if (e->e_id == NULL) 142258728Seric return; 142358728Seric 142417468Seric /* remove the transcript */ 142517468Seric # ifdef LOG 142658020Seric if (LogLevel > 87) 142717468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 142856795Seric # endif /* LOG */ 142958680Seric if (!tTd(51, 104)) 143017468Seric xunlink(queuename(e, 'x')); 143117468Seric 143217468Seric } 143340973Sbostic /* 143454974Seric ** SETCTLUSER -- create a controlling address 143540973Sbostic ** 143654974Seric ** Create a fake "address" given only a local login name; this is 143754974Seric ** used as a "controlling user" for future recipient addresses. 143840973Sbostic ** 143940973Sbostic ** Parameters: 144054974Seric ** user -- the user name of the controlling user. 144140973Sbostic ** 144240973Sbostic ** Returns: 144354974Seric ** An address descriptor for the controlling user. 144440973Sbostic ** 144540973Sbostic ** Side Effects: 144640973Sbostic ** none. 144740973Sbostic */ 144840973Sbostic 144954974Seric ADDRESS * 145054974Seric setctluser(user) 145154974Seric char *user; 145240973Sbostic { 145354974Seric register ADDRESS *a; 145440973Sbostic struct passwd *pw; 145559113Seric char *p; 145640973Sbostic 145740973Sbostic /* 145854974Seric ** See if this clears our concept of controlling user. 145940973Sbostic */ 146040973Sbostic 146163850Seric if (user == NULL || *user == '\0') 146263850Seric return NULL; 146340973Sbostic 146440973Sbostic /* 146554974Seric ** Set up addr fields for controlling user. 146640973Sbostic */ 146740973Sbostic 146854974Seric a = (ADDRESS *) xalloc(sizeof *a); 146954974Seric bzero((char *) a, sizeof *a); 147059113Seric 147159113Seric p = strchr(user, ':'); 147259113Seric if (p != NULL) 147359113Seric *p++ = '\0'; 147459270Seric if (*user != '\0' && (pw = getpwnam(user)) != NULL) 147540973Sbostic { 147640973Sbostic a->q_home = newstr(pw->pw_dir); 147740973Sbostic a->q_uid = pw->pw_uid; 147840973Sbostic a->q_gid = pw->pw_gid; 147957642Seric a->q_user = newstr(user); 148059270Seric a->q_flags |= QGOODUID; 148140973Sbostic } 148240973Sbostic else 148340973Sbostic { 148457642Seric a->q_user = newstr(DefUser); 148540973Sbostic } 148640973Sbostic 148759270Seric a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 148856328Seric a->q_mailer = LocalMailer; 148959113Seric if (p == NULL) 149059113Seric a->q_paddr = a->q_user; 149159113Seric else 149259113Seric a->q_paddr = newstr(p); 149354974Seric return a; 149440973Sbostic } 1495