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*68478Seric static char sccsid[] = "@(#)queue.c 8.41.1.3 (Berkeley) 03/05/95 (with queueing)"; 1433731Sbostic #else 15*68478Seric static char sccsid[] = "@(#)queue.c 8.41.1.3 (Berkeley) 03/05/95 (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; 7165870Seric MCI mcibuf; 7251920Seric char buf[MAXLINE], tf[MAXLINE]; 734632Seric 745037Seric /* 7517477Seric ** Create control file. 765037Seric */ 774632Seric 7864745Seric newid = (e->e_id == NULL) || !bitset(EF_INQUEUE, e->e_flags); 7964277Seric 8064277Seric /* if newid, queuename will create a locked qf file in e->lockfp */ 8151920Seric strcpy(tf, queuename(e, 't')); 8251920Seric tfp = e->e_lockfp; 8351920Seric if (tfp == NULL) 8451920Seric newid = FALSE; 8564277Seric 8664277Seric /* if newid, just write the qf file directly (instead of tf file) */ 8764745Seric if (!newid) 8851835Seric { 8951920Seric /* get a locked tf file */ 9064070Seric for (i = 0; i < 128; i++) 9151835Seric { 9251920Seric fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode); 9351920Seric if (fd < 0) 9451835Seric { 9564070Seric if (errno != EEXIST) 9664070Seric break; 9764070Seric #ifdef LOG 9864070Seric if (LogLevel > 0 && (i % 32) == 0) 9965090Seric syslog(LOG_ALERT, "queueup: cannot create %s, uid=%d: %s", 10065090Seric tf, geteuid(), errstring(errno)); 10164070Seric #endif 10241636Srick } 10365090Seric else 10465090Seric { 10565090Seric if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB)) 10665090Seric break; 10764070Seric #ifdef LOG 10865090Seric else if (LogLevel > 0 && (i % 32) == 0) 10965090Seric syslog(LOG_ALERT, "queueup: cannot lock %s: %s", 11065090Seric tf, errstring(errno)); 11164070Seric #endif 11265090Seric close(fd); 11365090Seric } 11458689Seric 11564070Seric if ((i % 32) == 31) 11664070Seric { 11764070Seric /* save the old temp file away */ 11864070Seric (void) rename(tf, queuename(e, 'T')); 11964070Seric } 12064070Seric else 12164070Seric sleep(i % 32); 12241636Srick } 12364724Seric if (fd < 0 || (tfp = fdopen(fd, "w")) == NULL) 12464921Seric { 12564921Seric printopenfds(TRUE); 12665090Seric syserr("!queueup: cannot create queue temp file %s, uid=%d", 12765090Seric tf, geteuid()); 12864921Seric } 12951920Seric } 1304632Seric 1317677Seric if (tTd(40, 1)) 132*68478Seric printf("\n>>>>> queueing %s%s >>>>>\n", e->e_id, 133*68478Seric newid ? " (new id)" : ""); 13464745Seric if (tTd(40, 9)) 13564745Seric { 13664745Seric printf(" tfp="); 13764745Seric dumpfd(fileno(tfp), TRUE, FALSE); 13864745Seric printf(" lockfp="); 13964745Seric if (e->e_lockfp == NULL) 14064745Seric printf("NULL\n"); 14164745Seric else 14264745Seric dumpfd(fileno(e->e_lockfp), TRUE, FALSE); 14364745Seric } 1444632Seric 1454632Seric /* 1466980Seric ** If there is no data file yet, create one. 1476980Seric */ 1486980Seric 1496980Seric if (e->e_df == NULL) 1506980Seric { 1516980Seric register FILE *dfp; 1529389Seric extern putbody(); 1536980Seric 15464086Seric e->e_df = queuename(e, 'd'); 15564086Seric e->e_df = newstr(e->e_df); 15666891Seric fd = open(e->e_df, O_WRONLY|O_CREAT|O_TRUNC, FileMode); 15764724Seric if (fd < 0 || (dfp = fdopen(fd, "w")) == NULL) 15865090Seric syserr("!queueup: cannot create data temp file %s, uid=%d", 15965090Seric e->e_df, geteuid()); 16065870Seric bzero(&mcibuf, sizeof mcibuf); 16165870Seric mcibuf.mci_out = dfp; 16265870Seric mcibuf.mci_mailer = FileMailer; 16368228Seric (*e->e_putbody)(&mcibuf, e, NULL); 16458680Seric (void) xfclose(dfp, "queueup dfp", e->e_id); 1659389Seric e->e_putbody = putbody; 1666980Seric } 1676980Seric 1686980Seric /* 1694632Seric ** Output future work requests. 17025687Seric ** Priority and creation time should be first, since 17125687Seric ** they are required by orderq. 1724632Seric */ 1734632Seric 1749377Seric /* output message priority */ 1759377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 1769377Seric 1779630Seric /* output creation time */ 1789630Seric fprintf(tfp, "T%ld\n", e->e_ctime); 1799630Seric 18059093Seric /* output type and name of data file */ 18159093Seric if (e->e_bodytype != NULL) 18259093Seric fprintf(tfp, "B%s\n", e->e_bodytype); 1837812Seric fprintf(tfp, "D%s\n", e->e_df); 1844632Seric 18510108Seric /* message from envelope, if it exists */ 18610108Seric if (e->e_message != NULL) 187*68478Seric fprintf(tfp, "M%s\n", denlstring(e->e_message, TRUE, FALSE)); 18810108Seric 18958737Seric /* send various flag bits through */ 19058737Seric p = buf; 19158737Seric if (bitset(EF_WARNING, e->e_flags)) 19258737Seric *p++ = 'w'; 19358737Seric if (bitset(EF_RESPONSE, e->e_flags)) 19458737Seric *p++ = 'r'; 19558737Seric *p++ = '\0'; 19658737Seric if (buf[0] != '\0') 19758737Seric fprintf(tfp, "F%s\n", buf); 19858737Seric 19958957Seric /* $r and $s and $_ macro values */ 20053400Seric if ((p = macvalue('r', e)) != NULL) 201*68478Seric fprintf(tfp, "$r%s\n", denlstring(p, TRUE, FALSE)); 20253400Seric if ((p = macvalue('s', e)) != NULL) 203*68478Seric fprintf(tfp, "$s%s\n", denlstring(p, TRUE, FALSE)); 20458957Seric if ((p = macvalue('_', e)) != NULL) 205*68478Seric fprintf(tfp, "$_%s\n", denlstring(p, TRUE, FALSE)); 20653400Seric 2074632Seric /* output name of sender */ 208*68478Seric fprintf(tfp, "S%s\n", denlstring(e->e_from.q_paddr, TRUE, FALSE)); 2094632Seric 21055360Seric /* output list of error recipients */ 21159670Seric printctladdr(NULL, NULL); 21255360Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 21355360Seric { 21458680Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 21555360Seric { 21659113Seric printctladdr(q, tfp); 217*68478Seric fprintf(tfp, "E%s\n", denlstring(q->q_paddr, TRUE, FALSE)); 21855360Seric } 21955360Seric } 22055360Seric 2214632Seric /* output list of recipient addresses */ 2226980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 2234632Seric { 22458250Seric if (bitset(QQUEUEUP, q->q_flags) || 22558680Seric (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags))) 2268245Seric { 22759113Seric printctladdr(q, tfp); 228*68478Seric fprintf(tfp, "R%s\n", denlstring(q->q_paddr, TRUE, FALSE)); 2299377Seric if (announce) 2309377Seric { 2319377Seric e->e_to = q->q_paddr; 23258151Seric message("queued"); 23358020Seric if (LogLevel > 8) 234*68478Seric logdelivery(NULL, NULL, "queued", NULL, e); 2359377Seric e->e_to = NULL; 2369377Seric } 2379387Seric if (tTd(40, 1)) 2389387Seric { 2399387Seric printf("queueing "); 2409387Seric printaddr(q, FALSE); 2419387Seric } 2428245Seric } 2434632Seric } 2444632Seric 2459377Seric /* 2469377Seric ** Output headers for this message. 2479377Seric ** Expand macros completely here. Queue run will deal with 2489377Seric ** everything as absolute headers. 2499377Seric ** All headers that must be relative to the recipient 2509377Seric ** can be cracked later. 25110173Seric ** We set up a "null mailer" -- i.e., a mailer that will have 25210173Seric ** no effect on the addresses as they are output. 2539377Seric */ 2549377Seric 25510686Seric bzero((char *) &nullmailer, sizeof nullmailer); 25658020Seric nullmailer.m_re_rwset = nullmailer.m_rh_rwset = 25765584Seric nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1; 25810349Seric nullmailer.m_eol = "\n"; 25965870Seric bzero(&mcibuf, sizeof mcibuf); 26065870Seric mcibuf.mci_mailer = &nullmailer; 26165870Seric mcibuf.mci_out = tfp; 26210173Seric 26358050Seric define('g', "\201f", e); 2646980Seric for (h = e->e_header; h != NULL; h = h->h_link) 2654632Seric { 26610686Seric extern bool bitzerop(); 26710686Seric 26812015Seric /* don't output null headers */ 2694632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 2704632Seric continue; 27112015Seric 27212015Seric /* don't output resent headers on non-resent messages */ 27312015Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 27412015Seric continue; 27512015Seric 27665267Seric /* expand macros; if null, don't output header at all */ 27765267Seric if (bitset(H_DEFAULT, h->h_flags)) 27865267Seric { 27965267Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 28065267Seric if (buf[0] == '\0') 28165267Seric continue; 28265267Seric } 28365267Seric 28412015Seric /* output this header */ 2857812Seric fprintf(tfp, "H"); 28612015Seric 28712015Seric /* if conditional, output the set of conditions */ 28810686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 28910686Seric { 29010686Seric int j; 29110686Seric 29223098Seric (void) putc('?', tfp); 29310686Seric for (j = '\0'; j <= '\177'; j++) 29410686Seric if (bitnset(j, h->h_mflags)) 29523098Seric (void) putc(j, tfp); 29623098Seric (void) putc('?', tfp); 29710686Seric } 29812015Seric 29912015Seric /* output the header: expand macros, convert addresses */ 3007763Seric if (bitset(H_DEFAULT, h->h_flags)) 3017763Seric { 30265267Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 3037763Seric } 3048245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 3059348Seric { 30658737Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 30763753Seric FILE *savetrace = TrafficLogFile; 30858737Seric 30963753Seric TrafficLogFile = NULL; 31063753Seric 31158737Seric if (bitset(H_FROM, h->h_flags)) 31258737Seric oldstyle = FALSE; 31358737Seric 31465870Seric commaize(h, h->h_value, oldstyle, &mcibuf, e); 31563753Seric 31663753Seric TrafficLogFile = savetrace; 3179348Seric } 3187763Seric else 3198245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 3204632Seric } 3214632Seric 3224632Seric /* 3234632Seric ** Clean up. 3244632Seric */ 3254632Seric 32664762Seric if (fflush(tfp) < 0 || fsync(fileno(tfp)) < 0 || ferror(tfp)) 32758732Seric { 32858732Seric if (newid) 32958732Seric syserr("!552 Error writing control file %s", tf); 33058732Seric else 33158732Seric syserr("!452 Error writing control file %s", tf); 33258732Seric } 33358732Seric 33451920Seric if (!newid) 33551920Seric { 33664277Seric /* rename (locked) tf to be (locked) qf */ 33751920Seric qf = queuename(e, 'q'); 33851920Seric if (rename(tf, qf) < 0) 33965090Seric syserr("cannot rename(%s, %s), df=%s, uid=%d", 34065090Seric tf, qf, e->e_df, geteuid()); 34164277Seric 34264277Seric /* close and unlock old (locked) qf */ 34351920Seric if (e->e_lockfp != NULL) 34458680Seric (void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id); 34551920Seric e->e_lockfp = tfp; 34651920Seric } 34751920Seric else 34851920Seric qf = tf; 34941636Srick errno = 0; 35064745Seric e->e_flags |= EF_INQUEUE; 3517391Seric 3527677Seric # ifdef LOG 3537677Seric /* save log info */ 35458020Seric if (LogLevel > 79) 3557878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 35656795Seric # endif /* LOG */ 35764307Seric 35864307Seric if (tTd(40, 1)) 35964307Seric printf("<<<<< done queueing %s <<<<<\n\n", e->e_id); 36051920Seric return; 3614632Seric } 36254974Seric 36354974Seric printctladdr(a, tfp) 36459113Seric register ADDRESS *a; 36554974Seric FILE *tfp; 36654974Seric { 36759113Seric char *uname; 36859113Seric register struct passwd *pw; 36959113Seric register ADDRESS *q; 37059113Seric uid_t uid; 37159113Seric static ADDRESS *lastctladdr; 37259113Seric static uid_t lastuid; 37354974Seric 37459113Seric /* initialization */ 37563850Seric if (a == NULL || a->q_alias == NULL || tfp == NULL) 37654974Seric { 37759670Seric if (lastctladdr != NULL && tfp != NULL) 37859113Seric fprintf(tfp, "C\n"); 37959113Seric lastctladdr = NULL; 38059113Seric lastuid = 0; 38154974Seric return; 38254974Seric } 38359113Seric 38459113Seric /* find the active uid */ 38559113Seric q = getctladdr(a); 38659113Seric if (q == NULL) 38759113Seric uid = 0; 38854974Seric else 38959113Seric uid = q->q_uid; 39063850Seric a = a->q_alias; 39159113Seric 39259113Seric /* check to see if this is the same as last time */ 39359113Seric if (lastctladdr != NULL && uid == lastuid && 39459113Seric strcmp(lastctladdr->q_paddr, a->q_paddr) == 0) 39559113Seric return; 39659113Seric lastuid = uid; 39759113Seric lastctladdr = a; 39859113Seric 39959113Seric if (uid == 0 || (pw = getpwuid(uid)) == NULL) 40059270Seric uname = ""; 40159113Seric else 40259113Seric uname = pw->pw_name; 40359113Seric 404*68478Seric fprintf(tfp, "C%s:%s\n", uname, denlstring(a->q_paddr, TRUE, FALSE)); 40554974Seric } 406*68478Seric 4074632Seric /* 4084632Seric ** RUNQUEUE -- run the jobs in the queue. 4094632Seric ** 4104632Seric ** Gets the stuff out of the queue in some presumably logical 4114632Seric ** order and processes them. 4124632Seric ** 4134632Seric ** Parameters: 41424941Seric ** forkflag -- TRUE if the queue scanning should be done in 41524941Seric ** a child process. We double-fork so it is not our 41624941Seric ** child and we don't have to clean up after it. 4174632Seric ** 4184632Seric ** Returns: 4194632Seric ** none. 4204632Seric ** 4214632Seric ** Side Effects: 4224632Seric ** runs things in the mail queue. 4234632Seric */ 4244632Seric 42555360Seric ENVELOPE QueueEnvelope; /* the queue run envelope */ 42655360Seric 42755360Seric runqueue(forkflag) 4284639Seric bool forkflag; 4294632Seric { 43055360Seric register ENVELOPE *e; 43155360Seric extern ENVELOPE BlankEnvelope; 43224953Seric 4337466Seric /* 43424953Seric ** If no work will ever be selected, don't even bother reading 43524953Seric ** the queue. 43624953Seric */ 43724953Seric 43851920Seric CurrentLA = getla(); /* get load average */ 43940934Srick 44058132Seric if (shouldqueue(0L, curtime())) 44124953Seric { 44224953Seric if (Verbose) 44324953Seric printf("Skipping queue run -- load average too high\n"); 44458107Seric if (forkflag && QueueIntvl != 0) 44558107Seric (void) setevent(QueueIntvl, runqueue, TRUE); 44655360Seric return; 44724953Seric } 44824953Seric 44924953Seric /* 4507466Seric ** See if we want to go off and do other useful work. 4517466Seric */ 4524639Seric 4534639Seric if (forkflag) 4544639Seric { 4557943Seric int pid; 45665223Seric #ifdef SIGCHLD 45765223Seric extern void reapchild(); 4587943Seric 45965223Seric (void) setsignal(SIGCHLD, reapchild); 46065223Seric #endif 46165223Seric 4627943Seric pid = dofork(); 4637943Seric if (pid != 0) 4644639Seric { 4657943Seric /* parent -- pick up intermediate zombie */ 46625184Seric #ifndef SIGCHLD 4679377Seric (void) waitfor(pid); 46856795Seric #endif /* SIGCHLD */ 4697690Seric if (QueueIntvl != 0) 4709348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 4714639Seric return; 4724639Seric } 4737943Seric /* child -- double fork */ 47425184Seric #ifndef SIGCHLD 4757943Seric if (fork() != 0) 4767943Seric exit(EX_OK); 47756795Seric #else /* SIGCHLD */ 47864035Seric (void) setsignal(SIGCHLD, SIG_DFL); 47956795Seric #endif /* SIGCHLD */ 4804639Seric } 48124941Seric 48240934Srick setproctitle("running queue: %s", QueueDir); 48324941Seric 4847876Seric # ifdef LOG 48558020Seric if (LogLevel > 69) 48655360Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 48755360Seric QueueDir, getpid(), forkflag); 48856795Seric # endif /* LOG */ 4894639Seric 4907466Seric /* 49110205Seric ** Release any resources used by the daemon code. 49210205Seric */ 49310205Seric 49410205Seric # ifdef DAEMON 49510205Seric clrdaemon(); 49656795Seric # endif /* DAEMON */ 49710205Seric 49864658Seric /* force it to run expensive jobs */ 49964658Seric NoConnect = FALSE; 50064658Seric 50110205Seric /* 50255360Seric ** Create ourselves an envelope 50355360Seric */ 50455360Seric 50555360Seric CurEnv = &QueueEnvelope; 50658179Seric e = newenvelope(&QueueEnvelope, CurEnv); 50755360Seric e->e_flags = BlankEnvelope.e_flags; 50855360Seric 50955360Seric /* 51027175Seric ** Make sure the alias database is open. 51127175Seric */ 51227175Seric 51360537Seric initmaps(FALSE, e); 51427175Seric 51527175Seric /* 5167466Seric ** Start making passes through the queue. 5177466Seric ** First, read and sort the entire queue. 5187466Seric ** Then, process the work in that order. 5197466Seric ** But if you take too long, start over. 5207466Seric */ 5217466Seric 5227943Seric /* order the existing work requests */ 523*68478Seric (void) orderq(FALSE); 5247690Seric 5257943Seric /* process them once at a time */ 5267943Seric while (WorkQ != NULL) 5274639Seric { 5287943Seric WORK *w = WorkQ; 5297881Seric 5307943Seric WorkQ = WorkQ->w_next; 53158884Seric 53258884Seric /* 53358884Seric ** Ignore jobs that are too expensive for the moment. 53458884Seric */ 53558884Seric 53658884Seric if (shouldqueue(w->w_pri, w->w_ctime)) 53758884Seric { 53858884Seric if (Verbose) 539*68478Seric printf("\nSkipping %s\n", w->w_name + 2); 54058884Seric } 54158930Seric else 54258930Seric { 54364296Seric pid_t pid; 54464296Seric extern pid_t dowork(); 54564296Seric 54664296Seric pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e); 54764296Seric errno = 0; 54866316Seric if (pid != 0) 54966316Seric (void) waitfor(pid); 55058930Seric } 5517943Seric free(w->w_name); 5527943Seric free((char *) w); 5534639Seric } 55429866Seric 55529866Seric /* exit without the usual cleanup */ 55655467Seric e->e_id = NULL; 55755467Seric finis(); 5584634Seric } 5594634Seric /* 5604632Seric ** ORDERQ -- order the work queue. 5614632Seric ** 5624632Seric ** Parameters: 56324941Seric ** doall -- if set, include everything in the queue (even 56424941Seric ** the jobs that cannot be run because the load 56524941Seric ** average is too high). Otherwise, exclude those 56624941Seric ** jobs. 5674632Seric ** 5684632Seric ** Returns: 56910121Seric ** The number of request in the queue (not necessarily 57010121Seric ** the number of requests in WorkQ however). 5714632Seric ** 5724632Seric ** Side Effects: 5734632Seric ** Sets WorkQ to the queue of available work, in order. 5744632Seric */ 5754632Seric 57625687Seric # define NEED_P 001 57725687Seric # define NEED_T 002 57858318Seric # define NEED_R 004 57958318Seric # define NEED_S 010 5804632Seric 58124941Seric orderq(doall) 58224941Seric bool doall; 5834632Seric { 58460219Seric register struct dirent *d; 5854632Seric register WORK *w; 5866625Sglickman DIR *f; 5874632Seric register int i; 58825687Seric WORK wlist[QUEUESIZE+1]; 58910070Seric int wn = -1; 590*68478Seric extern workcmpf(); 5914632Seric 59258318Seric if (tTd(41, 1)) 59358318Seric { 59458318Seric printf("orderq:\n"); 59558318Seric if (QueueLimitId != NULL) 59658318Seric printf("\tQueueLimitId = %s\n", QueueLimitId); 59758318Seric if (QueueLimitSender != NULL) 59858318Seric printf("\tQueueLimitSender = %s\n", QueueLimitSender); 59958318Seric if (QueueLimitRecipient != NULL) 60058318Seric printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 60158318Seric } 60258318Seric 6034632Seric /* clear out old WorkQ */ 6044632Seric for (w = WorkQ; w != NULL; ) 6054632Seric { 6064632Seric register WORK *nw = w->w_next; 6074632Seric 6084632Seric WorkQ = nw; 6094632Seric free(w->w_name); 6104632Seric free((char *) w); 6114632Seric w = nw; 6124632Seric } 6134632Seric 6144632Seric /* open the queue directory */ 6158148Seric f = opendir("."); 6164632Seric if (f == NULL) 6174632Seric { 6188148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 61910070Seric return (0); 6204632Seric } 6214632Seric 6224632Seric /* 6234632Seric ** Read the work directory. 6244632Seric */ 6254632Seric 62610070Seric while ((d = readdir(f)) != NULL) 6274632Seric { 6289377Seric FILE *cf; 62964492Seric register char *p; 6304632Seric char lbuf[MAXNAME]; 63158318Seric extern bool strcontainedin(); 6324632Seric 6334632Seric /* is this an interesting entry? */ 6347812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 6354632Seric continue; 6364632Seric 63758318Seric if (QueueLimitId != NULL && 63858318Seric !strcontainedin(QueueLimitId, d->d_name)) 63958318Seric continue; 64058318Seric 64158722Seric /* 64258722Seric ** Check queue name for plausibility. This handles 64358722Seric ** both old and new type ids. 64458722Seric */ 64558722Seric 64664492Seric p = d->d_name + 2; 64764492Seric if (isupper(p[0]) && isupper(p[2])) 64864492Seric p += 3; 64964492Seric else if (isupper(p[1])) 65064492Seric p += 2; 65164492Seric else 65264492Seric p = d->d_name; 65364492Seric for (i = 0; isdigit(*p); p++) 65464492Seric i++; 65564492Seric if (i < 5 || *p != '\0') 65658020Seric { 65758020Seric if (Verbose) 65858020Seric printf("orderq: bogus qf name %s\n", d->d_name); 65958020Seric #ifdef LOG 66058020Seric if (LogLevel > 3) 66159615Seric syslog(LOG_CRIT, "orderq: bogus qf name %s", 66258020Seric d->d_name); 66358020Seric #endif 66458020Seric if (strlen(d->d_name) >= MAXNAME) 66558020Seric d->d_name[MAXNAME - 1] = '\0'; 66658020Seric strcpy(lbuf, d->d_name); 66758020Seric lbuf[0] = 'Q'; 66858020Seric (void) rename(d->d_name, lbuf); 66958020Seric continue; 67058020Seric } 67158020Seric 67210070Seric /* yes -- open control file (if not too many files) */ 67325687Seric if (++wn >= QUEUESIZE) 67410070Seric continue; 67558318Seric 6768148Seric cf = fopen(d->d_name, "r"); 6774632Seric if (cf == NULL) 6784632Seric { 6797055Seric /* this may be some random person sending hir msgs */ 6807055Seric /* syserr("orderq: cannot open %s", cbuf); */ 68110090Seric if (tTd(41, 2)) 68210090Seric printf("orderq: cannot open %s (%d)\n", 68310090Seric d->d_name, errno); 6847055Seric errno = 0; 68510090Seric wn--; 6864632Seric continue; 6874632Seric } 68825687Seric w = &wlist[wn]; 68925687Seric w->w_name = newstr(d->d_name); 6904632Seric 69125027Seric /* make sure jobs in creation don't clog queue */ 69225687Seric w->w_pri = 0x7fffffff; 69325687Seric w->w_ctime = 0; 69425027Seric 6954632Seric /* extract useful information */ 69625687Seric i = NEED_P | NEED_T; 69758318Seric if (QueueLimitSender != NULL) 69858318Seric i |= NEED_S; 699*68478Seric if (QueueLimitRecipient != NULL) 70058318Seric i |= NEED_R; 70125687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 7024632Seric { 70324954Seric extern long atol(); 70458318Seric extern bool strcontainedin(); 70524954Seric 70624941Seric switch (lbuf[0]) 7074632Seric { 70824941Seric case 'P': 70925687Seric w->w_pri = atol(&lbuf[1]); 71025687Seric i &= ~NEED_P; 7114632Seric break; 71225013Seric 71325013Seric case 'T': 71425687Seric w->w_ctime = atol(&lbuf[1]); 71525687Seric i &= ~NEED_T; 71625013Seric break; 71758318Seric 71858318Seric case 'R': 719*68478Seric if (QueueLimitRecipient != NULL && 72058318Seric strcontainedin(QueueLimitRecipient, &lbuf[1])) 72158318Seric i &= ~NEED_R; 72258318Seric break; 72358318Seric 72458318Seric case 'S': 72558318Seric if (QueueLimitSender != NULL && 72658318Seric strcontainedin(QueueLimitSender, &lbuf[1])) 72758318Seric i &= ~NEED_S; 72858318Seric break; 7294632Seric } 7304632Seric } 7314632Seric (void) fclose(cf); 73224953Seric 73358318Seric if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 73458318Seric bitset(NEED_R|NEED_S, i)) 73524953Seric { 73624953Seric /* don't even bother sorting this job in */ 73724953Seric wn--; 73824953Seric } 7394632Seric } 7406625Sglickman (void) closedir(f); 74110090Seric wn++; 7424632Seric 743*68478Seric /* 744*68478Seric ** Sort the work directory. 745*68478Seric */ 7464632Seric 747*68478Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 74867612Seric 7494632Seric /* 7504632Seric ** Convert the work list into canonical form. 7519377Seric ** Should be turning it into a list of envelopes here perhaps. 7524632Seric */ 7534632Seric 75424981Seric WorkQ = NULL; 755*68478Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 7564632Seric { 7574632Seric w = (WORK *) xalloc(sizeof *w); 7584632Seric w->w_name = wlist[i].w_name; 7594632Seric w->w_pri = wlist[i].w_pri; 76025013Seric w->w_ctime = wlist[i].w_ctime; 76124981Seric w->w_next = WorkQ; 76224981Seric WorkQ = w; 7634632Seric } 7644632Seric 7657677Seric if (tTd(40, 1)) 7664632Seric { 7674632Seric for (w = WorkQ; w != NULL; w = w->w_next) 7685037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 7694632Seric } 77010070Seric 77110090Seric return (wn); 7724632Seric } 7734632Seric /* 774*68478Seric ** WORKCMPF -- compare function for ordering work. 7754632Seric ** 7764632Seric ** Parameters: 7774632Seric ** a -- the first argument. 7784632Seric ** b -- the second argument. 7794632Seric ** 7804632Seric ** Returns: 78124981Seric ** -1 if a < b 78224981Seric ** 0 if a == b 78324981Seric ** +1 if a > b 7844632Seric ** 7854632Seric ** Side Effects: 7864632Seric ** none. 7874632Seric */ 7884632Seric 789*68478Seric workcmpf(a, b) 7905037Seric register WORK *a; 7915037Seric register WORK *b; 7924632Seric { 79357438Seric long pa = a->w_pri; 79457438Seric long pb = b->w_pri; 79524941Seric 79624941Seric if (pa == pb) 797*68478Seric return (0); 79824941Seric else if (pa > pb) 799*68478Seric return (1); 80024981Seric else 801*68478Seric return (-1); 8024632Seric } 8034632Seric /* 8044632Seric ** DOWORK -- do a work request. 8054632Seric ** 8064632Seric ** Parameters: 80758884Seric ** id -- the ID of the job to run. 80858884Seric ** forkflag -- if set, run this in background. 80958924Seric ** requeueflag -- if set, reinstantiate the queue quickly. 81058924Seric ** This is used when expanding aliases in the queue. 81161094Seric ** If forkflag is also set, it doesn't wait for the 81261094Seric ** child. 81358884Seric ** e - the envelope in which to run it. 8144632Seric ** 8154632Seric ** Returns: 81664296Seric ** process id of process that is running the queue job. 8174632Seric ** 8184632Seric ** Side Effects: 8194632Seric ** The work request is satisfied if possible. 8204632Seric */ 8214632Seric 82264296Seric pid_t 82358924Seric dowork(id, forkflag, requeueflag, e) 82458884Seric char *id; 82558884Seric bool forkflag; 82658924Seric bool requeueflag; 82755012Seric register ENVELOPE *e; 8284632Seric { 82964296Seric register pid_t pid; 83051920Seric extern bool readqf(); 8314632Seric 8327677Seric if (tTd(40, 1)) 83358884Seric printf("dowork(%s)\n", id); 8344632Seric 8354632Seric /* 83624941Seric ** Fork for work. 83724941Seric */ 83824941Seric 83958884Seric if (forkflag) 84024941Seric { 84164296Seric pid = fork(); 84264296Seric if (pid < 0) 84324941Seric { 84424941Seric syserr("dowork: cannot fork"); 84564296Seric return 0; 84624941Seric } 84764839Seric else if (pid > 0) 84864839Seric { 84964839Seric /* parent -- clean out connection cache */ 85064839Seric mci_flush(FALSE, NULL); 85164839Seric } 85224941Seric } 85324941Seric else 85424941Seric { 85564296Seric pid = 0; 85624941Seric } 85724941Seric 85864296Seric if (pid == 0) 8594632Seric { 8604632Seric /* 8614632Seric ** CHILD 8628148Seric ** Lock the control file to avoid duplicate deliveries. 8638148Seric ** Then run the file as though we had just read it. 8647350Seric ** We save an idea of the temporary name so we 8657350Seric ** can recover on interrupt. 8664632Seric */ 8674632Seric 8687763Seric /* set basic modes, etc. */ 8697356Seric (void) alarm(0); 87055012Seric clearenvelope(e, FALSE); 87164554Seric e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS; 87258734Seric e->e_errormode = EM_MAIL; 87358884Seric e->e_id = id; 87464765Seric GrabTo = UseErrorsTo = FALSE; 87566316Seric ExitStat = EX_OK; 87663846Seric if (forkflag) 87764554Seric { 87864296Seric disconnect(1, e); 87964554Seric OpMode = MD_DELIVER; 88064554Seric } 8817876Seric # ifdef LOG 88258020Seric if (LogLevel > 76) 88355012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 8847881Seric getpid()); 88556795Seric # endif /* LOG */ 8867763Seric 8877763Seric /* don't use the headers from sendmail.cf... */ 88855012Seric e->e_header = NULL; 8897763Seric 89051920Seric /* read the queue control file -- return if locked */ 89165200Seric if (!readqf(e)) 8926980Seric { 89358884Seric if (tTd(40, 4)) 89458884Seric printf("readqf(%s) failed\n", e->e_id); 89558884Seric if (forkflag) 89624941Seric exit(EX_OK); 89724941Seric else 89866316Seric return 0; 8996980Seric } 9006980Seric 90155012Seric e->e_flags |= EF_INQUEUE; 902*68478Seric eatheader(e, requeueflag); 9036980Seric 904*68478Seric if (requeueflag) 905*68478Seric queueup(e, TRUE, FALSE); 90658924Seric 907*68478Seric /* do the delivery */ 908*68478Seric sendall(e, SM_DELIVER); 9096980Seric 9106980Seric /* finish up and exit */ 91158884Seric if (forkflag) 91224941Seric finis(); 91324941Seric else 91455012Seric dropenvelope(e); 9154632Seric } 91664296Seric e->e_id = NULL; 91764296Seric return pid; 9184632Seric } 9194632Seric /* 9204632Seric ** READQF -- read queue file and set up environment. 9214632Seric ** 9224632Seric ** Parameters: 9239377Seric ** e -- the envelope of the job to run. 9244632Seric ** 9254632Seric ** Returns: 92651920Seric ** TRUE if it successfully read the queue file. 92751920Seric ** FALSE otherwise. 9284632Seric ** 9294632Seric ** Side Effects: 93051920Seric ** The queue file is returned locked. 9314632Seric */ 9324632Seric 93351920Seric bool 93465200Seric readqf(e) 9359377Seric register ENVELOPE *e; 9364632Seric { 93717477Seric register FILE *qfp; 93854974Seric ADDRESS *ctladdr; 93956400Seric struct stat st; 94057135Seric char *bp; 94158915Seric char qf[20]; 94257135Seric char buf[MAXLINE]; 94324954Seric extern long atol(); 94454974Seric extern ADDRESS *setctluser(); 9454632Seric 9464632Seric /* 94717468Seric ** Read and process the file. 9484632Seric */ 9494632Seric 95058915Seric strcpy(qf, queuename(e, 'q')); 95151937Seric qfp = fopen(qf, "r+"); 95217477Seric if (qfp == NULL) 95317477Seric { 95458884Seric if (tTd(40, 8)) 95558884Seric printf("readqf(%s): fopen failure (%s)\n", 95658884Seric qf, errstring(errno)); 95740934Srick if (errno != ENOENT) 95840934Srick syserr("readqf: no control file %s", qf); 95951920Seric return FALSE; 96017477Seric } 96140934Srick 96264335Seric if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB)) 96364277Seric { 96464277Seric /* being processed by another queuer */ 965*68478Seric if (tTd(40, 8)) 966*68478Seric printf("readqf(%s): locked\n", qf); 967*68478Seric if (Verbose) 96864277Seric printf("%s: locked\n", e->e_id); 96964277Seric # ifdef LOG 97064277Seric if (LogLevel > 19) 97164277Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 97264277Seric # endif /* LOG */ 97364277Seric (void) fclose(qfp); 97464277Seric return FALSE; 97564277Seric } 97664277Seric 97756400Seric /* 97856400Seric ** Check the queue file for plausibility to avoid attacks. 97956400Seric */ 98056400Seric 98156400Seric if (fstat(fileno(qfp), &st) < 0) 98256400Seric { 98356400Seric /* must have been being processed by someone else */ 98458884Seric if (tTd(40, 8)) 98558884Seric printf("readqf(%s): fstat failure (%s)\n", 98658884Seric qf, errstring(errno)); 98756400Seric fclose(qfp); 98856400Seric return FALSE; 98956400Seric } 99056400Seric 99164137Seric if (st.st_uid != geteuid()) 99256400Seric { 99356400Seric # ifdef LOG 99456400Seric if (LogLevel > 0) 99556400Seric { 99656400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 99756400Seric e->e_id, st.st_uid, st.st_mode); 99856400Seric } 99956795Seric # endif /* LOG */ 100058884Seric if (tTd(40, 8)) 100158884Seric printf("readqf(%s): bogus file\n", qf); 100264277Seric rename(qf, queuename(e, 'Q')); 100356400Seric fclose(qfp); 100456400Seric return FALSE; 100556400Seric } 100656400Seric 100764277Seric if (st.st_size == 0) 100840934Srick { 100964277Seric /* must be a bogus file -- just remove it */ 101064277Seric (void) unlink(qf); 101164277Seric fclose(qfp); 101251920Seric return FALSE; 101340934Srick } 101440934Srick 101564277Seric if (st.st_nlink == 0) 101659101Seric { 101764277Seric /* 101864277Seric ** Race condition -- we got a file just as it was being 101964277Seric ** unlinked. Just assume it is zero length. 102064277Seric */ 102164277Seric 102259101Seric fclose(qfp); 102359101Seric return FALSE; 102459101Seric } 102559101Seric 102664277Seric /* good file -- save this lock */ 102751920Seric e->e_lockfp = qfp; 102851920Seric 102940934Srick /* do basic system initialization */ 103055012Seric initsys(e); 103165164Seric define('i', e->e_id, e); 103240934Srick 10339377Seric LineNumber = 0; 103463850Seric e->e_flags |= EF_GLOBALERRS; 103563850Seric OpMode = MD_DELIVER; 1036*68478Seric if (Verbose) 1037*68478Seric printf("\nRunning %s\n", e->e_id); 103854974Seric ctladdr = NULL; 103957135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 10404632Seric { 104158737Seric register char *p; 104257529Seric struct stat st; 104357529Seric 104426504Seric if (tTd(40, 4)) 104557135Seric printf("+++++ %s\n", bp); 104657135Seric switch (bp[0]) 10474632Seric { 104840973Sbostic case 'C': /* specify controlling user */ 104957135Seric ctladdr = setctluser(&bp[1]); 105040973Sbostic break; 105140973Sbostic 10524632Seric case 'R': /* specify recipient */ 1053*68478Seric (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 10544632Seric break; 10554632Seric 105625687Seric case 'E': /* specify error recipient */ 1057*68478Seric (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 105825687Seric break; 105925687Seric 10604632Seric case 'H': /* header */ 106157135Seric (void) chompheader(&bp[1], FALSE, e); 10624632Seric break; 10634632Seric 106410108Seric case 'M': /* message */ 106564705Seric /* ignore this; we want a new message next time */ 106610108Seric break; 106710108Seric 10684632Seric case 'S': /* sender */ 106958704Seric setsender(newstr(&bp[1]), e, NULL, TRUE); 10704632Seric break; 10714632Seric 107259093Seric case 'B': /* body type */ 107359093Seric e->e_bodytype = newstr(&bp[1]); 107459093Seric break; 107559093Seric 10764632Seric case 'D': /* data file name */ 107757135Seric e->e_df = newstr(&bp[1]); 10789544Seric e->e_dfp = fopen(e->e_df, "r"); 10799544Seric if (e->e_dfp == NULL) 108058020Seric { 10819377Seric syserr("readqf: cannot open %s", e->e_df); 108258020Seric e->e_msgsize = -1; 108358020Seric } 108458020Seric else if (fstat(fileno(e->e_dfp), &st) >= 0) 108557529Seric e->e_msgsize = st.st_size; 10864632Seric break; 10874632Seric 10887860Seric case 'T': /* init time */ 108957135Seric e->e_ctime = atol(&bp[1]); 10904632Seric break; 10914632Seric 10924634Seric case 'P': /* message priority */ 109357135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 10944634Seric break; 10954634Seric 109658737Seric case 'F': /* flag bits */ 109758737Seric for (p = &bp[1]; *p != '\0'; p++) 109858737Seric { 109958737Seric switch (*p) 110058737Seric { 110158737Seric case 'w': /* warning sent */ 110258737Seric e->e_flags |= EF_WARNING; 110358737Seric break; 110458737Seric 110558737Seric case 'r': /* response */ 110658737Seric e->e_flags |= EF_RESPONSE; 110758737Seric break; 110858737Seric } 110958737Seric } 111058737Seric break; 111158737Seric 111253400Seric case '$': /* define macro */ 111357135Seric define(bp[1], newstr(&bp[2]), e); 111453400Seric break; 111553400Seric 111624941Seric case '\0': /* blank line; ignore */ 111724941Seric break; 111824941Seric 11194632Seric default: 112065960Seric syserr("readqf: %s: line %d: bad line \"%s\"", 112165200Seric qf, LineNumber, bp); 112263753Seric fclose(qfp); 112363753Seric rename(qf, queuename(e, 'Q')); 112463753Seric return FALSE; 11254632Seric } 112657135Seric 112757135Seric if (bp != buf) 112857135Seric free(bp); 11294632Seric } 11309377Seric 113124941Seric /* 113224941Seric ** If we haven't read any lines, this queue file is empty. 113324941Seric ** Arrange to remove it without referencing any null pointers. 113424941Seric */ 113524941Seric 113624941Seric if (LineNumber == 0) 113724941Seric { 113824941Seric errno = 0; 113924941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 114024941Seric } 114151920Seric return TRUE; 11424632Seric } 11434632Seric /* 11449630Seric ** PRINTQUEUE -- print out a representation of the mail queue 11459630Seric ** 11469630Seric ** Parameters: 11479630Seric ** none. 11489630Seric ** 11499630Seric ** Returns: 11509630Seric ** none. 11519630Seric ** 11529630Seric ** Side Effects: 11539630Seric ** Prints a listing of the mail queue on the standard output. 11549630Seric */ 11555182Seric 11569630Seric printqueue() 11579630Seric { 11589630Seric register WORK *w; 11599630Seric FILE *f; 116010070Seric int nrequests; 11619630Seric char buf[MAXLINE]; 11629630Seric 11639630Seric /* 116458250Seric ** Check for permission to print the queue 116558250Seric */ 116658250Seric 116764333Seric if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0) 116858250Seric { 116958250Seric struct stat st; 117058523Seric # ifdef NGROUPS 117158523Seric int n; 117263937Seric GIDSET_T gidset[NGROUPS]; 117358523Seric # endif 117458250Seric 117558517Seric if (stat(QueueDir, &st) < 0) 117658250Seric { 117758250Seric syserr("Cannot stat %s", QueueDir); 117858250Seric return; 117958250Seric } 118058523Seric # ifdef NGROUPS 118158523Seric n = getgroups(NGROUPS, gidset); 118258523Seric while (--n >= 0) 118358523Seric { 118458523Seric if (gidset[n] == st.st_gid) 118558523Seric break; 118658523Seric } 118758523Seric if (n < 0) 118858523Seric # else 118963787Seric if (RealGid != st.st_gid) 119058523Seric # endif 119158250Seric { 119258250Seric usrerr("510 You are not permitted to see the queue"); 119358250Seric setstat(EX_NOPERM); 119458250Seric return; 119558250Seric } 119658250Seric } 119758250Seric 119858250Seric /* 11999630Seric ** Read and order the queue. 12009630Seric */ 12019630Seric 120224941Seric nrequests = orderq(TRUE); 12039630Seric 12049630Seric /* 12059630Seric ** Print the work list that we have read. 12069630Seric */ 12079630Seric 12089630Seric /* first see if there is anything */ 120910070Seric if (nrequests <= 0) 12109630Seric { 121110070Seric printf("Mail queue is empty\n"); 12129630Seric return; 12139630Seric } 12149630Seric 121551920Seric CurrentLA = getla(); /* get load average */ 121640934Srick 121710096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 121825687Seric if (nrequests > QUEUESIZE) 121925687Seric printf(", only %d printed", QUEUESIZE); 122024979Seric if (Verbose) 122158716Seric printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 122224979Seric else 122358716Seric printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 12249630Seric for (w = WorkQ; w != NULL; w = w->w_next) 12259630Seric { 12269630Seric struct stat st; 122710070Seric auto time_t submittime = 0; 122810070Seric long dfsize = -1; 122958737Seric int flags = 0; 123010108Seric char message[MAXLINE]; 123159093Seric char bodytype[MAXNAME]; 12329630Seric 123364355Seric printf("%8s", w->w_name + 2); 123417468Seric f = fopen(w->w_name, "r"); 123517468Seric if (f == NULL) 123617468Seric { 123764355Seric printf(" (job completed)\n"); 123817468Seric errno = 0; 123917468Seric continue; 124017468Seric } 1241*68478Seric if (!lockfile(fileno(f), w->w_name, NULL, LOCK_SH|LOCK_NB)) 124210070Seric printf("*"); 124357438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 124424941Seric printf("X"); 124510070Seric else 124610070Seric printf(" "); 124710070Seric errno = 0; 124817468Seric 124959093Seric message[0] = bodytype[0] = '\0'; 12509630Seric while (fgets(buf, sizeof buf, f) != NULL) 12519630Seric { 125253400Seric register int i; 125358737Seric register char *p; 125453400Seric 12559630Seric fixcrlf(buf, TRUE); 12569630Seric switch (buf[0]) 12579630Seric { 125810108Seric case 'M': /* error message */ 125953400Seric if ((i = strlen(&buf[1])) >= sizeof message) 126058737Seric i = sizeof message - 1; 126153400Seric bcopy(&buf[1], message, i); 126253400Seric message[i] = '\0'; 126310108Seric break; 126410108Seric 126559093Seric case 'B': /* body type */ 126659093Seric if ((i = strlen(&buf[1])) >= sizeof bodytype) 126759093Seric i = sizeof bodytype - 1; 126859093Seric bcopy(&buf[1], bodytype, i); 126959093Seric bodytype[i] = '\0'; 127059093Seric break; 127159093Seric 12729630Seric case 'S': /* sender name */ 127324979Seric if (Verbose) 127458737Seric printf("%8ld %10ld%c%.12s %.38s", 127558737Seric dfsize, 127658737Seric w->w_pri, 127758737Seric bitset(EF_WARNING, flags) ? '+' : ' ', 127858737Seric ctime(&submittime) + 4, 127924979Seric &buf[1]); 128024979Seric else 128124979Seric printf("%8ld %.16s %.45s", dfsize, 128224979Seric ctime(&submittime), &buf[1]); 128359093Seric if (message[0] != '\0' || bodytype[0] != '\0') 128459093Seric { 128559093Seric printf("\n %10.10s", bodytype); 128659093Seric if (message[0] != '\0') 128759093Seric printf(" (%.60s)", message); 128859093Seric } 12899630Seric break; 129051920Seric 129140973Sbostic case 'C': /* controlling user */ 129254974Seric if (Verbose) 129358716Seric printf("\n\t\t\t\t (---%.34s---)", 129458716Seric &buf[1]); 129540973Sbostic break; 12969630Seric 12979630Seric case 'R': /* recipient name */ 129824979Seric if (Verbose) 1299*68478Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 130024979Seric else 1301*68478Seric printf("\n\t\t\t\t %.45s", &buf[1]); 13029630Seric break; 13039630Seric 13049630Seric case 'T': /* creation time */ 130524941Seric submittime = atol(&buf[1]); 13069630Seric break; 130710070Seric 130810070Seric case 'D': /* data file name */ 130910070Seric if (stat(&buf[1], &st) >= 0) 131010070Seric dfsize = st.st_size; 131110070Seric break; 131258737Seric 131358737Seric case 'F': /* flag bits */ 131458737Seric for (p = &buf[1]; *p != '\0'; p++) 131558737Seric { 131658737Seric switch (*p) 131758737Seric { 131858737Seric case 'w': 131958737Seric flags |= EF_WARNING; 132058737Seric break; 132158737Seric } 132258737Seric } 13239630Seric } 13249630Seric } 132510070Seric if (submittime == (time_t) 0) 132610070Seric printf(" (no control file)"); 13279630Seric printf("\n"); 132823098Seric (void) fclose(f); 13299630Seric } 13309630Seric } 13319630Seric 133256795Seric # endif /* QUEUE */ 133317468Seric /* 133417468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 133517468Seric ** 133617468Seric ** Assigns an id code if one does not already exist. 133717468Seric ** This code is very careful to avoid trashing existing files 133817468Seric ** under any circumstances. 133917468Seric ** 134017468Seric ** Parameters: 134117468Seric ** e -- envelope to build it in/from. 134217468Seric ** type -- the file type, used as the first character 134317468Seric ** of the file name. 134417468Seric ** 134517468Seric ** Returns: 134617468Seric ** a pointer to the new file name (in a static buffer). 134717468Seric ** 134817468Seric ** Side Effects: 134951920Seric ** If no id code is already assigned, queuename will 135051920Seric ** assign an id code, create a qf file, and leave a 135151920Seric ** locked, open-for-write file pointer in the envelope. 135217468Seric */ 135317468Seric 135417468Seric char * 135517468Seric queuename(e, type) 135617468Seric register ENVELOPE *e; 135759700Seric int type; 135817468Seric { 135917468Seric static int pid = -1; 136058741Seric static char c0; 136158741Seric static char c1; 136258741Seric static char c2; 136358716Seric time_t now; 136458716Seric struct tm *tm; 136558689Seric static char buf[MAXNAME]; 136617468Seric 136717468Seric if (e->e_id == NULL) 136817468Seric { 136917468Seric char qf[20]; 137017468Seric 137117468Seric /* find a unique id */ 137217468Seric if (pid != getpid()) 137317468Seric { 137417468Seric /* new process -- start back at "AA" */ 137517468Seric pid = getpid(); 137658716Seric now = curtime(); 137758716Seric tm = localtime(&now); 137858716Seric c0 = 'A' + tm->tm_hour; 137917468Seric c1 = 'A'; 138017468Seric c2 = 'A' - 1; 138117468Seric } 138258716Seric (void) sprintf(qf, "qf%cAA%05d", c0, pid); 138317468Seric 138417468Seric while (c1 < '~' || c2 < 'Z') 138517468Seric { 138617468Seric int i; 138717468Seric 138817468Seric if (c2 >= 'Z') 138917468Seric { 139017468Seric c1++; 139117468Seric c2 = 'A' - 1; 139217468Seric } 139358716Seric qf[3] = c1; 139458716Seric qf[4] = ++c2; 139517468Seric if (tTd(7, 20)) 139640934Srick printf("queuename: trying \"%s\"\n", qf); 139717468Seric 139840934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 139951920Seric if (i < 0) 140051920Seric { 140151920Seric if (errno == EEXIST) 140251920Seric continue; 140364705Seric syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 140464705Seric qf, QueueDir, geteuid()); 140551920Seric exit(EX_UNAVAILABLE); 140651920Seric } 140764335Seric if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB)) 140851920Seric { 140951920Seric e->e_lockfp = fdopen(i, "w"); 141040934Srick break; 141117468Seric } 141251920Seric 141351920Seric /* a reader got the file; abandon it and try again */ 141451920Seric (void) close(i); 141517468Seric } 141617468Seric if (c1 >= '~' && c2 >= 'Z') 141717468Seric { 141864705Seric syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 141964705Seric qf, QueueDir, geteuid()); 142017468Seric exit(EX_OSERR); 142117468Seric } 142217468Seric e->e_id = newstr(&qf[2]); 142317468Seric define('i', e->e_id, e); 142417468Seric if (tTd(7, 1)) 142517468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 142664745Seric if (tTd(7, 9)) 142764745Seric { 142864745Seric printf(" lockfd="); 142964745Seric dumpfd(fileno(e->e_lockfp), TRUE, FALSE); 143064745Seric } 143117468Seric # ifdef LOG 143258020Seric if (LogLevel > 93) 143317468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 143456795Seric # endif /* LOG */ 143517468Seric } 143617468Seric 143717468Seric if (type == '\0') 143817468Seric return (NULL); 143917468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 144017468Seric if (tTd(7, 2)) 144117468Seric printf("queuename: %s\n", buf); 144217468Seric return (buf); 144317468Seric } 144417468Seric /* 144517468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 144617468Seric ** 144717468Seric ** Parameters: 144817468Seric ** e -- the envelope to unlock. 144917468Seric ** 145017468Seric ** Returns: 145117468Seric ** none 145217468Seric ** 145317468Seric ** Side Effects: 145417468Seric ** unlocks the queue for `e'. 145517468Seric */ 145617468Seric 145717468Seric unlockqueue(e) 145817468Seric ENVELOPE *e; 145917468Seric { 146058680Seric if (tTd(51, 4)) 146158680Seric printf("unlockqueue(%s)\n", e->e_id); 146258680Seric 146351920Seric /* if there is a lock file in the envelope, close it */ 146451920Seric if (e->e_lockfp != NULL) 146558680Seric xfclose(e->e_lockfp, "unlockqueue", e->e_id); 146651920Seric e->e_lockfp = NULL; 146751920Seric 146858728Seric /* don't create a queue id if we don't already have one */ 146958728Seric if (e->e_id == NULL) 147058728Seric return; 147158728Seric 147217468Seric /* remove the transcript */ 147317468Seric # ifdef LOG 147458020Seric if (LogLevel > 87) 147517468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 147656795Seric # endif /* LOG */ 147758680Seric if (!tTd(51, 104)) 147817468Seric xunlink(queuename(e, 'x')); 147917468Seric 148017468Seric } 148140973Sbostic /* 148254974Seric ** SETCTLUSER -- create a controlling address 148340973Sbostic ** 148454974Seric ** Create a fake "address" given only a local login name; this is 148554974Seric ** used as a "controlling user" for future recipient addresses. 148640973Sbostic ** 148740973Sbostic ** Parameters: 148854974Seric ** user -- the user name of the controlling user. 148940973Sbostic ** 149040973Sbostic ** Returns: 149154974Seric ** An address descriptor for the controlling user. 149240973Sbostic ** 149340973Sbostic ** Side Effects: 149440973Sbostic ** none. 149540973Sbostic */ 149640973Sbostic 149754974Seric ADDRESS * 149854974Seric setctluser(user) 149954974Seric char *user; 150040973Sbostic { 150154974Seric register ADDRESS *a; 150240973Sbostic struct passwd *pw; 150359113Seric char *p; 150440973Sbostic 150540973Sbostic /* 150654974Seric ** See if this clears our concept of controlling user. 150740973Sbostic */ 150840973Sbostic 150963850Seric if (user == NULL || *user == '\0') 151063850Seric return NULL; 151140973Sbostic 151240973Sbostic /* 151354974Seric ** Set up addr fields for controlling user. 151440973Sbostic */ 151540973Sbostic 151654974Seric a = (ADDRESS *) xalloc(sizeof *a); 151754974Seric bzero((char *) a, sizeof *a); 151859113Seric 151959113Seric p = strchr(user, ':'); 152059113Seric if (p != NULL) 152159113Seric *p++ = '\0'; 152259270Seric if (*user != '\0' && (pw = getpwnam(user)) != NULL) 152340973Sbostic { 152465822Seric if (strcmp(pw->pw_dir, "/") == 0) 152565822Seric a->q_home = ""; 152665822Seric else 152765822Seric a->q_home = newstr(pw->pw_dir); 152840973Sbostic a->q_uid = pw->pw_uid; 152940973Sbostic a->q_gid = pw->pw_gid; 153057642Seric a->q_user = newstr(user); 153159270Seric a->q_flags |= QGOODUID; 153240973Sbostic } 153340973Sbostic else 153440973Sbostic { 153557642Seric a->q_user = newstr(DefUser); 153640973Sbostic } 153740973Sbostic 153859270Seric a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 153956328Seric a->q_mailer = LocalMailer; 154059113Seric if (p == NULL) 154159113Seric a->q_paddr = a->q_user; 154259113Seric else 154359113Seric a->q_paddr = newstr(p); 154454974Seric return a; 154540973Sbostic } 1546