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*65870Seric static char sccsid[] = "@(#)queue.c 8.38 (Berkeley) 01/24/94 (with queueing)"; 1433731Sbostic #else 15*65870Seric static char sccsid[] = "@(#)queue.c 8.38 (Berkeley) 01/24/94 (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; 71*65870Seric 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)) 13264745Seric printf("\n>>>>> queueing %s%s >>>>>\n", e->e_id, 13364745Seric 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); 15640934Srick fd = open(e->e_df, O_WRONLY|O_CREAT, 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()); 160*65870Seric bzero(&mcibuf, sizeof mcibuf); 161*65870Seric mcibuf.mci_out = dfp; 162*65870Seric mcibuf.mci_mailer = FileMailer; 163*65870Seric (*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) 18710108Seric fprintf(tfp, "M%s\n", e->e_message); 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) 20153400Seric fprintf(tfp, "$r%s\n", p); 20253400Seric if ((p = macvalue('s', e)) != NULL) 20353400Seric fprintf(tfp, "$s%s\n", p); 20458957Seric if ((p = macvalue('_', e)) != NULL) 20558957Seric fprintf(tfp, "$_%s\n", p); 20653400Seric 2074632Seric /* output name of sender */ 2087812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 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); 21755360Seric fprintf(tfp, "E%s\n", q->q_paddr); 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); 2287812Seric fprintf(tfp, "R%s\n", q->q_paddr); 2299377Seric if (announce) 2309377Seric { 2319377Seric e->e_to = q->q_paddr; 23258151Seric message("queued"); 23358020Seric if (LogLevel > 8) 23464771Seric 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"; 259*65870Seric bzero(&mcibuf, sizeof mcibuf); 260*65870Seric mcibuf.mci_mailer = &nullmailer; 261*65870Seric 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 314*65870Seric 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 40459113Seric fprintf(tfp, "C%s:%s\n", uname, a->q_paddr); 40554974Seric } 40654974Seric 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 */ 52324954Seric (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) 53958884Seric 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; 54864296Seric (void) waitfor(pid); 54958930Seric } 5507943Seric free(w->w_name); 5517943Seric free((char *) w); 5524639Seric } 55329866Seric 55429866Seric /* exit without the usual cleanup */ 55555467Seric e->e_id = NULL; 55655467Seric finis(); 5574634Seric } 5584634Seric /* 5594632Seric ** ORDERQ -- order the work queue. 5604632Seric ** 5614632Seric ** Parameters: 56224941Seric ** doall -- if set, include everything in the queue (even 56324941Seric ** the jobs that cannot be run because the load 56424941Seric ** average is too high). Otherwise, exclude those 56524941Seric ** jobs. 5664632Seric ** 5674632Seric ** Returns: 56810121Seric ** The number of request in the queue (not necessarily 56910121Seric ** the number of requests in WorkQ however). 5704632Seric ** 5714632Seric ** Side Effects: 5724632Seric ** Sets WorkQ to the queue of available work, in order. 5734632Seric */ 5744632Seric 57525687Seric # define NEED_P 001 57625687Seric # define NEED_T 002 57758318Seric # define NEED_R 004 57858318Seric # define NEED_S 010 5794632Seric 58024941Seric orderq(doall) 58124941Seric bool doall; 5824632Seric { 58360219Seric register struct dirent *d; 5844632Seric register WORK *w; 5856625Sglickman DIR *f; 5864632Seric register int i; 58725687Seric WORK wlist[QUEUESIZE+1]; 58810070Seric int wn = -1; 5894632Seric extern workcmpf(); 5904632Seric 59158318Seric if (tTd(41, 1)) 59258318Seric { 59358318Seric printf("orderq:\n"); 59458318Seric if (QueueLimitId != NULL) 59558318Seric printf("\tQueueLimitId = %s\n", QueueLimitId); 59658318Seric if (QueueLimitSender != NULL) 59758318Seric printf("\tQueueLimitSender = %s\n", QueueLimitSender); 59858318Seric if (QueueLimitRecipient != NULL) 59958318Seric printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 60058318Seric } 60158318Seric 6024632Seric /* clear out old WorkQ */ 6034632Seric for (w = WorkQ; w != NULL; ) 6044632Seric { 6054632Seric register WORK *nw = w->w_next; 6064632Seric 6074632Seric WorkQ = nw; 6084632Seric free(w->w_name); 6094632Seric free((char *) w); 6104632Seric w = nw; 6114632Seric } 6124632Seric 6134632Seric /* open the queue directory */ 6148148Seric f = opendir("."); 6154632Seric if (f == NULL) 6164632Seric { 6178148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 61810070Seric return (0); 6194632Seric } 6204632Seric 6214632Seric /* 6224632Seric ** Read the work directory. 6234632Seric */ 6244632Seric 62510070Seric while ((d = readdir(f)) != NULL) 6264632Seric { 6279377Seric FILE *cf; 62864492Seric register char *p; 6294632Seric char lbuf[MAXNAME]; 63058318Seric extern bool strcontainedin(); 6314632Seric 6324632Seric /* is this an interesting entry? */ 6337812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 6344632Seric continue; 6354632Seric 63658318Seric if (QueueLimitId != NULL && 63758318Seric !strcontainedin(QueueLimitId, d->d_name)) 63858318Seric continue; 63958318Seric 64058722Seric /* 64158722Seric ** Check queue name for plausibility. This handles 64258722Seric ** both old and new type ids. 64358722Seric */ 64458722Seric 64564492Seric p = d->d_name + 2; 64664492Seric if (isupper(p[0]) && isupper(p[2])) 64764492Seric p += 3; 64864492Seric else if (isupper(p[1])) 64964492Seric p += 2; 65064492Seric else 65164492Seric p = d->d_name; 65264492Seric for (i = 0; isdigit(*p); p++) 65364492Seric i++; 65464492Seric if (i < 5 || *p != '\0') 65558020Seric { 65658020Seric if (Verbose) 65758020Seric printf("orderq: bogus qf name %s\n", d->d_name); 65858020Seric #ifdef LOG 65958020Seric if (LogLevel > 3) 66059615Seric syslog(LOG_CRIT, "orderq: bogus qf name %s", 66158020Seric d->d_name); 66258020Seric #endif 66358020Seric if (strlen(d->d_name) >= MAXNAME) 66458020Seric d->d_name[MAXNAME - 1] = '\0'; 66558020Seric strcpy(lbuf, d->d_name); 66658020Seric lbuf[0] = 'Q'; 66758020Seric (void) rename(d->d_name, lbuf); 66858020Seric continue; 66958020Seric } 67058020Seric 67110070Seric /* yes -- open control file (if not too many files) */ 67225687Seric if (++wn >= QUEUESIZE) 67310070Seric continue; 67458318Seric 6758148Seric cf = fopen(d->d_name, "r"); 6764632Seric if (cf == NULL) 6774632Seric { 6787055Seric /* this may be some random person sending hir msgs */ 6797055Seric /* syserr("orderq: cannot open %s", cbuf); */ 68010090Seric if (tTd(41, 2)) 68110090Seric printf("orderq: cannot open %s (%d)\n", 68210090Seric d->d_name, errno); 6837055Seric errno = 0; 68410090Seric wn--; 6854632Seric continue; 6864632Seric } 68725687Seric w = &wlist[wn]; 68825687Seric w->w_name = newstr(d->d_name); 6894632Seric 69025027Seric /* make sure jobs in creation don't clog queue */ 69125687Seric w->w_pri = 0x7fffffff; 69225687Seric w->w_ctime = 0; 69325027Seric 6944632Seric /* extract useful information */ 69525687Seric i = NEED_P | NEED_T; 69658318Seric if (QueueLimitSender != NULL) 69758318Seric i |= NEED_S; 69858318Seric if (QueueLimitRecipient != NULL) 69958318Seric i |= NEED_R; 70025687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 7014632Seric { 70224954Seric extern long atol(); 70358318Seric extern bool strcontainedin(); 70424954Seric 70524941Seric switch (lbuf[0]) 7064632Seric { 70724941Seric case 'P': 70825687Seric w->w_pri = atol(&lbuf[1]); 70925687Seric i &= ~NEED_P; 7104632Seric break; 71125013Seric 71225013Seric case 'T': 71325687Seric w->w_ctime = atol(&lbuf[1]); 71425687Seric i &= ~NEED_T; 71525013Seric break; 71658318Seric 71758318Seric case 'R': 71858318Seric if (QueueLimitRecipient != NULL && 71958318Seric strcontainedin(QueueLimitRecipient, &lbuf[1])) 72058318Seric i &= ~NEED_R; 72158318Seric break; 72258318Seric 72358318Seric case 'S': 72458318Seric if (QueueLimitSender != NULL && 72558318Seric strcontainedin(QueueLimitSender, &lbuf[1])) 72658318Seric i &= ~NEED_S; 72758318Seric break; 7284632Seric } 7294632Seric } 7304632Seric (void) fclose(cf); 73124953Seric 73258318Seric if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 73358318Seric bitset(NEED_R|NEED_S, i)) 73424953Seric { 73524953Seric /* don't even bother sorting this job in */ 73624953Seric wn--; 73724953Seric } 7384632Seric } 7396625Sglickman (void) closedir(f); 74010090Seric wn++; 7414632Seric 7424632Seric /* 7434632Seric ** Sort the work directory. 7444632Seric */ 7454632Seric 74625687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 7474632Seric 7484632Seric /* 7494632Seric ** Convert the work list into canonical form. 7509377Seric ** Should be turning it into a list of envelopes here perhaps. 7514632Seric */ 7524632Seric 75324981Seric WorkQ = NULL; 75425687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 7554632Seric { 7564632Seric w = (WORK *) xalloc(sizeof *w); 7574632Seric w->w_name = wlist[i].w_name; 7584632Seric w->w_pri = wlist[i].w_pri; 75925013Seric w->w_ctime = wlist[i].w_ctime; 76024981Seric w->w_next = WorkQ; 76124981Seric WorkQ = w; 7624632Seric } 7634632Seric 7647677Seric if (tTd(40, 1)) 7654632Seric { 7664632Seric for (w = WorkQ; w != NULL; w = w->w_next) 7675037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 7684632Seric } 76910070Seric 77010090Seric return (wn); 7714632Seric } 7724632Seric /* 7737677Seric ** WORKCMPF -- compare function for ordering work. 7744632Seric ** 7754632Seric ** Parameters: 7764632Seric ** a -- the first argument. 7774632Seric ** b -- the second argument. 7784632Seric ** 7794632Seric ** Returns: 78024981Seric ** -1 if a < b 78124981Seric ** 0 if a == b 78224981Seric ** +1 if a > b 7834632Seric ** 7844632Seric ** Side Effects: 7854632Seric ** none. 7864632Seric */ 7874632Seric 7884632Seric workcmpf(a, b) 7895037Seric register WORK *a; 7905037Seric register WORK *b; 7914632Seric { 79257438Seric long pa = a->w_pri; 79357438Seric long pb = b->w_pri; 79424941Seric 79524941Seric if (pa == pb) 7964632Seric return (0); 79724941Seric else if (pa > pb) 79824981Seric return (1); 79924981Seric else 80010121Seric return (-1); 8014632Seric } 8024632Seric /* 8034632Seric ** DOWORK -- do a work request. 8044632Seric ** 8054632Seric ** Parameters: 80658884Seric ** id -- the ID of the job to run. 80758884Seric ** forkflag -- if set, run this in background. 80858924Seric ** requeueflag -- if set, reinstantiate the queue quickly. 80958924Seric ** This is used when expanding aliases in the queue. 81061094Seric ** If forkflag is also set, it doesn't wait for the 81161094Seric ** child. 81258884Seric ** e - the envelope in which to run it. 8134632Seric ** 8144632Seric ** Returns: 81564296Seric ** process id of process that is running the queue job. 8164632Seric ** 8174632Seric ** Side Effects: 8184632Seric ** The work request is satisfied if possible. 8194632Seric */ 8204632Seric 82164296Seric pid_t 82258924Seric dowork(id, forkflag, requeueflag, e) 82358884Seric char *id; 82458884Seric bool forkflag; 82558924Seric bool requeueflag; 82655012Seric register ENVELOPE *e; 8274632Seric { 82864296Seric register pid_t pid; 82951920Seric extern bool readqf(); 8304632Seric 8317677Seric if (tTd(40, 1)) 83258884Seric printf("dowork(%s)\n", id); 8334632Seric 8344632Seric /* 83524941Seric ** Fork for work. 83624941Seric */ 83724941Seric 83858884Seric if (forkflag) 83924941Seric { 84064296Seric pid = fork(); 84164296Seric if (pid < 0) 84224941Seric { 84324941Seric syserr("dowork: cannot fork"); 84464296Seric return 0; 84524941Seric } 84664839Seric else if (pid > 0) 84764839Seric { 84864839Seric /* parent -- clean out connection cache */ 84964839Seric mci_flush(FALSE, NULL); 85064839Seric } 85124941Seric } 85224941Seric else 85324941Seric { 85464296Seric pid = 0; 85524941Seric } 85624941Seric 85764296Seric if (pid == 0) 8584632Seric { 8594632Seric /* 8604632Seric ** CHILD 8618148Seric ** Lock the control file to avoid duplicate deliveries. 8628148Seric ** Then run the file as though we had just read it. 8637350Seric ** We save an idea of the temporary name so we 8647350Seric ** can recover on interrupt. 8654632Seric */ 8664632Seric 8677763Seric /* set basic modes, etc. */ 8687356Seric (void) alarm(0); 86955012Seric clearenvelope(e, FALSE); 87064554Seric e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS; 87158734Seric e->e_errormode = EM_MAIL; 87258884Seric e->e_id = id; 87364765Seric GrabTo = UseErrorsTo = FALSE; 87463846Seric if (forkflag) 87564554Seric { 87664296Seric disconnect(1, e); 87764554Seric OpMode = MD_DELIVER; 87864554Seric } 8797876Seric # ifdef LOG 88058020Seric if (LogLevel > 76) 88155012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 8827881Seric getpid()); 88356795Seric # endif /* LOG */ 8847763Seric 8857763Seric /* don't use the headers from sendmail.cf... */ 88655012Seric e->e_header = NULL; 8877763Seric 88851920Seric /* read the queue control file -- return if locked */ 88965200Seric if (!readqf(e)) 8906980Seric { 89158884Seric if (tTd(40, 4)) 89258884Seric printf("readqf(%s) failed\n", e->e_id); 89358884Seric if (forkflag) 89424941Seric exit(EX_OK); 89524941Seric else 89624941Seric return; 8976980Seric } 8986980Seric 89955012Seric e->e_flags |= EF_INQUEUE; 90058929Seric eatheader(e, requeueflag); 9016980Seric 90258924Seric if (requeueflag) 90358924Seric queueup(e, TRUE, FALSE); 90458924Seric 9056980Seric /* do the delivery */ 90658915Seric sendall(e, SM_DELIVER); 9076980Seric 9086980Seric /* finish up and exit */ 90958884Seric if (forkflag) 91024941Seric finis(); 91124941Seric else 91255012Seric dropenvelope(e); 9134632Seric } 91464296Seric e->e_id = NULL; 91564296Seric return pid; 9164632Seric } 9174632Seric /* 9184632Seric ** READQF -- read queue file and set up environment. 9194632Seric ** 9204632Seric ** Parameters: 9219377Seric ** e -- the envelope of the job to run. 9224632Seric ** 9234632Seric ** Returns: 92451920Seric ** TRUE if it successfully read the queue file. 92551920Seric ** FALSE otherwise. 9264632Seric ** 9274632Seric ** Side Effects: 92851920Seric ** The queue file is returned locked. 9294632Seric */ 9304632Seric 93151920Seric bool 93265200Seric readqf(e) 9339377Seric register ENVELOPE *e; 9344632Seric { 93517477Seric register FILE *qfp; 93654974Seric ADDRESS *ctladdr; 93756400Seric struct stat st; 93857135Seric char *bp; 93958915Seric char qf[20]; 94057135Seric char buf[MAXLINE]; 94124954Seric extern long atol(); 94254974Seric extern ADDRESS *setctluser(); 9434632Seric 9444632Seric /* 94517468Seric ** Read and process the file. 9464632Seric */ 9474632Seric 94858915Seric strcpy(qf, queuename(e, 'q')); 94951937Seric qfp = fopen(qf, "r+"); 95017477Seric if (qfp == NULL) 95117477Seric { 95258884Seric if (tTd(40, 8)) 95358884Seric printf("readqf(%s): fopen failure (%s)\n", 95458884Seric qf, errstring(errno)); 95540934Srick if (errno != ENOENT) 95640934Srick syserr("readqf: no control file %s", qf); 95751920Seric return FALSE; 95817477Seric } 95940934Srick 96064335Seric if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB)) 96164277Seric { 96264277Seric /* being processed by another queuer */ 96364277Seric if (tTd(40, 8)) 96464277Seric printf("readqf(%s): locked\n", qf); 96564277Seric if (Verbose) 96664277Seric printf("%s: locked\n", e->e_id); 96764277Seric # ifdef LOG 96864277Seric if (LogLevel > 19) 96964277Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 97064277Seric # endif /* LOG */ 97164277Seric (void) fclose(qfp); 97264277Seric return FALSE; 97364277Seric } 97464277Seric 97556400Seric /* 97656400Seric ** Check the queue file for plausibility to avoid attacks. 97756400Seric */ 97856400Seric 97956400Seric if (fstat(fileno(qfp), &st) < 0) 98056400Seric { 98156400Seric /* must have been being processed by someone else */ 98258884Seric if (tTd(40, 8)) 98358884Seric printf("readqf(%s): fstat failure (%s)\n", 98458884Seric qf, errstring(errno)); 98556400Seric fclose(qfp); 98656400Seric return FALSE; 98756400Seric } 98856400Seric 98964137Seric if (st.st_uid != geteuid()) 99056400Seric { 99156400Seric # ifdef LOG 99256400Seric if (LogLevel > 0) 99356400Seric { 99456400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 99556400Seric e->e_id, st.st_uid, st.st_mode); 99656400Seric } 99756795Seric # endif /* LOG */ 99858884Seric if (tTd(40, 8)) 99958884Seric printf("readqf(%s): bogus file\n", qf); 100064277Seric rename(qf, queuename(e, 'Q')); 100156400Seric fclose(qfp); 100256400Seric return FALSE; 100356400Seric } 100456400Seric 100564277Seric if (st.st_size == 0) 100640934Srick { 100764277Seric /* must be a bogus file -- just remove it */ 100864277Seric (void) unlink(qf); 100964277Seric fclose(qfp); 101051920Seric return FALSE; 101140934Srick } 101240934Srick 101364277Seric if (st.st_nlink == 0) 101459101Seric { 101564277Seric /* 101664277Seric ** Race condition -- we got a file just as it was being 101764277Seric ** unlinked. Just assume it is zero length. 101864277Seric */ 101964277Seric 102059101Seric fclose(qfp); 102159101Seric return FALSE; 102259101Seric } 102359101Seric 102464277Seric /* good file -- save this lock */ 102551920Seric e->e_lockfp = qfp; 102651920Seric 102740934Srick /* do basic system initialization */ 102855012Seric initsys(e); 102965164Seric define('i', e->e_id, e); 103040934Srick 10319377Seric LineNumber = 0; 103263850Seric e->e_flags |= EF_GLOBALERRS; 103363850Seric OpMode = MD_DELIVER; 103451920Seric if (Verbose) 10359377Seric printf("\nRunning %s\n", e->e_id); 103654974Seric ctladdr = NULL; 103757135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 10384632Seric { 103958737Seric register char *p; 104057529Seric struct stat st; 104157529Seric 104226504Seric if (tTd(40, 4)) 104357135Seric printf("+++++ %s\n", bp); 104457135Seric switch (bp[0]) 10454632Seric { 104640973Sbostic case 'C': /* specify controlling user */ 104757135Seric ctladdr = setctluser(&bp[1]); 104840973Sbostic break; 104940973Sbostic 10504632Seric case 'R': /* specify recipient */ 105158082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 10524632Seric break; 10534632Seric 105425687Seric case 'E': /* specify error recipient */ 105558082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 105625687Seric break; 105725687Seric 10584632Seric case 'H': /* header */ 105957135Seric (void) chompheader(&bp[1], FALSE, e); 10604632Seric break; 10614632Seric 106210108Seric case 'M': /* message */ 106364705Seric /* ignore this; we want a new message next time */ 106410108Seric break; 106510108Seric 10664632Seric case 'S': /* sender */ 106758704Seric setsender(newstr(&bp[1]), e, NULL, TRUE); 10684632Seric break; 10694632Seric 107059093Seric case 'B': /* body type */ 107159093Seric e->e_bodytype = newstr(&bp[1]); 107259093Seric break; 107359093Seric 10744632Seric case 'D': /* data file name */ 107557135Seric e->e_df = newstr(&bp[1]); 10769544Seric e->e_dfp = fopen(e->e_df, "r"); 10779544Seric if (e->e_dfp == NULL) 107858020Seric { 10799377Seric syserr("readqf: cannot open %s", e->e_df); 108058020Seric e->e_msgsize = -1; 108158020Seric } 108258020Seric else if (fstat(fileno(e->e_dfp), &st) >= 0) 108357529Seric e->e_msgsize = st.st_size; 10844632Seric break; 10854632Seric 10867860Seric case 'T': /* init time */ 108757135Seric e->e_ctime = atol(&bp[1]); 10884632Seric break; 10894632Seric 10904634Seric case 'P': /* message priority */ 109157135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 10924634Seric break; 10934634Seric 109458737Seric case 'F': /* flag bits */ 109558737Seric for (p = &bp[1]; *p != '\0'; p++) 109658737Seric { 109758737Seric switch (*p) 109858737Seric { 109958737Seric case 'w': /* warning sent */ 110058737Seric e->e_flags |= EF_WARNING; 110158737Seric break; 110258737Seric 110358737Seric case 'r': /* response */ 110458737Seric e->e_flags |= EF_RESPONSE; 110558737Seric break; 110658737Seric } 110758737Seric } 110858737Seric break; 110958737Seric 111053400Seric case '$': /* define macro */ 111157135Seric define(bp[1], newstr(&bp[2]), e); 111253400Seric break; 111353400Seric 111424941Seric case '\0': /* blank line; ignore */ 111524941Seric break; 111624941Seric 11174632Seric default: 111865200Seric syserr("readqf: %s: line %s: bad line \"%s\"", 111965200Seric qf, LineNumber, bp); 112063753Seric fclose(qfp); 112163753Seric rename(qf, queuename(e, 'Q')); 112263753Seric return FALSE; 11234632Seric } 112457135Seric 112557135Seric if (bp != buf) 112657135Seric free(bp); 11274632Seric } 11289377Seric 112924941Seric /* 113024941Seric ** If we haven't read any lines, this queue file is empty. 113124941Seric ** Arrange to remove it without referencing any null pointers. 113224941Seric */ 113324941Seric 113424941Seric if (LineNumber == 0) 113524941Seric { 113624941Seric errno = 0; 113724941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 113824941Seric } 113951920Seric return TRUE; 11404632Seric } 11414632Seric /* 11429630Seric ** PRINTQUEUE -- print out a representation of the mail queue 11439630Seric ** 11449630Seric ** Parameters: 11459630Seric ** none. 11469630Seric ** 11479630Seric ** Returns: 11489630Seric ** none. 11499630Seric ** 11509630Seric ** Side Effects: 11519630Seric ** Prints a listing of the mail queue on the standard output. 11529630Seric */ 11535182Seric 11549630Seric printqueue() 11559630Seric { 11569630Seric register WORK *w; 11579630Seric FILE *f; 115810070Seric int nrequests; 11599630Seric char buf[MAXLINE]; 11609630Seric 11619630Seric /* 116258250Seric ** Check for permission to print the queue 116358250Seric */ 116458250Seric 116564333Seric if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0) 116658250Seric { 116758250Seric struct stat st; 116858523Seric # ifdef NGROUPS 116958523Seric int n; 117063937Seric GIDSET_T gidset[NGROUPS]; 117158523Seric # endif 117258250Seric 117358517Seric if (stat(QueueDir, &st) < 0) 117458250Seric { 117558250Seric syserr("Cannot stat %s", QueueDir); 117658250Seric return; 117758250Seric } 117858523Seric # ifdef NGROUPS 117958523Seric n = getgroups(NGROUPS, gidset); 118058523Seric while (--n >= 0) 118158523Seric { 118258523Seric if (gidset[n] == st.st_gid) 118358523Seric break; 118458523Seric } 118558523Seric if (n < 0) 118658523Seric # else 118763787Seric if (RealGid != st.st_gid) 118858523Seric # endif 118958250Seric { 119058250Seric usrerr("510 You are not permitted to see the queue"); 119158250Seric setstat(EX_NOPERM); 119258250Seric return; 119358250Seric } 119458250Seric } 119558250Seric 119658250Seric /* 11979630Seric ** Read and order the queue. 11989630Seric */ 11999630Seric 120024941Seric nrequests = orderq(TRUE); 12019630Seric 12029630Seric /* 12039630Seric ** Print the work list that we have read. 12049630Seric */ 12059630Seric 12069630Seric /* first see if there is anything */ 120710070Seric if (nrequests <= 0) 12089630Seric { 120910070Seric printf("Mail queue is empty\n"); 12109630Seric return; 12119630Seric } 12129630Seric 121351920Seric CurrentLA = getla(); /* get load average */ 121440934Srick 121510096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 121625687Seric if (nrequests > QUEUESIZE) 121725687Seric printf(", only %d printed", QUEUESIZE); 121824979Seric if (Verbose) 121958716Seric printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 122024979Seric else 122158716Seric printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 12229630Seric for (w = WorkQ; w != NULL; w = w->w_next) 12239630Seric { 12249630Seric struct stat st; 122510070Seric auto time_t submittime = 0; 122610070Seric long dfsize = -1; 122758737Seric int flags = 0; 122810108Seric char message[MAXLINE]; 122959093Seric char bodytype[MAXNAME]; 12309630Seric 123164355Seric printf("%8s", w->w_name + 2); 123217468Seric f = fopen(w->w_name, "r"); 123317468Seric if (f == NULL) 123417468Seric { 123564355Seric printf(" (job completed)\n"); 123617468Seric errno = 0; 123717468Seric continue; 123817468Seric } 123964335Seric if (!lockfile(fileno(f), w->w_name, NULL, LOCK_SH|LOCK_NB)) 124010070Seric printf("*"); 124157438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 124224941Seric printf("X"); 124310070Seric else 124410070Seric printf(" "); 124510070Seric errno = 0; 124617468Seric 124759093Seric message[0] = bodytype[0] = '\0'; 12489630Seric while (fgets(buf, sizeof buf, f) != NULL) 12499630Seric { 125053400Seric register int i; 125158737Seric register char *p; 125253400Seric 12539630Seric fixcrlf(buf, TRUE); 12549630Seric switch (buf[0]) 12559630Seric { 125610108Seric case 'M': /* error message */ 125753400Seric if ((i = strlen(&buf[1])) >= sizeof message) 125858737Seric i = sizeof message - 1; 125953400Seric bcopy(&buf[1], message, i); 126053400Seric message[i] = '\0'; 126110108Seric break; 126210108Seric 126359093Seric case 'B': /* body type */ 126459093Seric if ((i = strlen(&buf[1])) >= sizeof bodytype) 126559093Seric i = sizeof bodytype - 1; 126659093Seric bcopy(&buf[1], bodytype, i); 126759093Seric bodytype[i] = '\0'; 126859093Seric break; 126959093Seric 12709630Seric case 'S': /* sender name */ 127124979Seric if (Verbose) 127258737Seric printf("%8ld %10ld%c%.12s %.38s", 127358737Seric dfsize, 127458737Seric w->w_pri, 127558737Seric bitset(EF_WARNING, flags) ? '+' : ' ', 127658737Seric ctime(&submittime) + 4, 127724979Seric &buf[1]); 127824979Seric else 127924979Seric printf("%8ld %.16s %.45s", dfsize, 128024979Seric ctime(&submittime), &buf[1]); 128159093Seric if (message[0] != '\0' || bodytype[0] != '\0') 128259093Seric { 128359093Seric printf("\n %10.10s", bodytype); 128459093Seric if (message[0] != '\0') 128559093Seric printf(" (%.60s)", message); 128659093Seric } 12879630Seric break; 128851920Seric 128940973Sbostic case 'C': /* controlling user */ 129054974Seric if (Verbose) 129158716Seric printf("\n\t\t\t\t (---%.34s---)", 129258716Seric &buf[1]); 129340973Sbostic break; 12949630Seric 12959630Seric case 'R': /* recipient name */ 129624979Seric if (Verbose) 129758716Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 129824979Seric else 129958716Seric printf("\n\t\t\t\t %.45s", &buf[1]); 13009630Seric break; 13019630Seric 13029630Seric case 'T': /* creation time */ 130324941Seric submittime = atol(&buf[1]); 13049630Seric break; 130510070Seric 130610070Seric case 'D': /* data file name */ 130710070Seric if (stat(&buf[1], &st) >= 0) 130810070Seric dfsize = st.st_size; 130910070Seric break; 131058737Seric 131158737Seric case 'F': /* flag bits */ 131258737Seric for (p = &buf[1]; *p != '\0'; p++) 131358737Seric { 131458737Seric switch (*p) 131558737Seric { 131658737Seric case 'w': 131758737Seric flags |= EF_WARNING; 131858737Seric break; 131958737Seric } 132058737Seric } 13219630Seric } 13229630Seric } 132310070Seric if (submittime == (time_t) 0) 132410070Seric printf(" (no control file)"); 13259630Seric printf("\n"); 132623098Seric (void) fclose(f); 13279630Seric } 13289630Seric } 13299630Seric 133056795Seric # endif /* QUEUE */ 133117468Seric /* 133217468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 133317468Seric ** 133417468Seric ** Assigns an id code if one does not already exist. 133517468Seric ** This code is very careful to avoid trashing existing files 133617468Seric ** under any circumstances. 133717468Seric ** 133817468Seric ** Parameters: 133917468Seric ** e -- envelope to build it in/from. 134017468Seric ** type -- the file type, used as the first character 134117468Seric ** of the file name. 134217468Seric ** 134317468Seric ** Returns: 134417468Seric ** a pointer to the new file name (in a static buffer). 134517468Seric ** 134617468Seric ** Side Effects: 134751920Seric ** If no id code is already assigned, queuename will 134851920Seric ** assign an id code, create a qf file, and leave a 134951920Seric ** locked, open-for-write file pointer in the envelope. 135017468Seric */ 135117468Seric 135217468Seric char * 135317468Seric queuename(e, type) 135417468Seric register ENVELOPE *e; 135559700Seric int type; 135617468Seric { 135717468Seric static int pid = -1; 135858741Seric static char c0; 135958741Seric static char c1; 136058741Seric static char c2; 136158716Seric time_t now; 136258716Seric struct tm *tm; 136358689Seric static char buf[MAXNAME]; 136417468Seric 136517468Seric if (e->e_id == NULL) 136617468Seric { 136717468Seric char qf[20]; 136817468Seric 136917468Seric /* find a unique id */ 137017468Seric if (pid != getpid()) 137117468Seric { 137217468Seric /* new process -- start back at "AA" */ 137317468Seric pid = getpid(); 137458716Seric now = curtime(); 137558716Seric tm = localtime(&now); 137658716Seric c0 = 'A' + tm->tm_hour; 137717468Seric c1 = 'A'; 137817468Seric c2 = 'A' - 1; 137917468Seric } 138058716Seric (void) sprintf(qf, "qf%cAA%05d", c0, pid); 138117468Seric 138217468Seric while (c1 < '~' || c2 < 'Z') 138317468Seric { 138417468Seric int i; 138517468Seric 138617468Seric if (c2 >= 'Z') 138717468Seric { 138817468Seric c1++; 138917468Seric c2 = 'A' - 1; 139017468Seric } 139158716Seric qf[3] = c1; 139258716Seric qf[4] = ++c2; 139317468Seric if (tTd(7, 20)) 139440934Srick printf("queuename: trying \"%s\"\n", qf); 139517468Seric 139640934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 139751920Seric if (i < 0) 139851920Seric { 139951920Seric if (errno == EEXIST) 140051920Seric continue; 140164705Seric syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 140264705Seric qf, QueueDir, geteuid()); 140351920Seric exit(EX_UNAVAILABLE); 140451920Seric } 140564335Seric if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB)) 140651920Seric { 140751920Seric e->e_lockfp = fdopen(i, "w"); 140840934Srick break; 140917468Seric } 141051920Seric 141151920Seric /* a reader got the file; abandon it and try again */ 141251920Seric (void) close(i); 141317468Seric } 141417468Seric if (c1 >= '~' && c2 >= 'Z') 141517468Seric { 141664705Seric syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 141764705Seric qf, QueueDir, geteuid()); 141817468Seric exit(EX_OSERR); 141917468Seric } 142017468Seric e->e_id = newstr(&qf[2]); 142117468Seric define('i', e->e_id, e); 142217468Seric if (tTd(7, 1)) 142317468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 142464745Seric if (tTd(7, 9)) 142564745Seric { 142664745Seric printf(" lockfd="); 142764745Seric dumpfd(fileno(e->e_lockfp), TRUE, FALSE); 142864745Seric } 142917468Seric # ifdef LOG 143058020Seric if (LogLevel > 93) 143117468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 143256795Seric # endif /* LOG */ 143317468Seric } 143417468Seric 143517468Seric if (type == '\0') 143617468Seric return (NULL); 143717468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 143817468Seric if (tTd(7, 2)) 143917468Seric printf("queuename: %s\n", buf); 144017468Seric return (buf); 144117468Seric } 144217468Seric /* 144317468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 144417468Seric ** 144517468Seric ** Parameters: 144617468Seric ** e -- the envelope to unlock. 144717468Seric ** 144817468Seric ** Returns: 144917468Seric ** none 145017468Seric ** 145117468Seric ** Side Effects: 145217468Seric ** unlocks the queue for `e'. 145317468Seric */ 145417468Seric 145517468Seric unlockqueue(e) 145617468Seric ENVELOPE *e; 145717468Seric { 145858680Seric if (tTd(51, 4)) 145958680Seric printf("unlockqueue(%s)\n", e->e_id); 146058680Seric 146151920Seric /* if there is a lock file in the envelope, close it */ 146251920Seric if (e->e_lockfp != NULL) 146358680Seric xfclose(e->e_lockfp, "unlockqueue", e->e_id); 146451920Seric e->e_lockfp = NULL; 146551920Seric 146658728Seric /* don't create a queue id if we don't already have one */ 146758728Seric if (e->e_id == NULL) 146858728Seric return; 146958728Seric 147017468Seric /* remove the transcript */ 147117468Seric # ifdef LOG 147258020Seric if (LogLevel > 87) 147317468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 147456795Seric # endif /* LOG */ 147558680Seric if (!tTd(51, 104)) 147617468Seric xunlink(queuename(e, 'x')); 147717468Seric 147817468Seric } 147940973Sbostic /* 148054974Seric ** SETCTLUSER -- create a controlling address 148140973Sbostic ** 148254974Seric ** Create a fake "address" given only a local login name; this is 148354974Seric ** used as a "controlling user" for future recipient addresses. 148440973Sbostic ** 148540973Sbostic ** Parameters: 148654974Seric ** user -- the user name of the controlling user. 148740973Sbostic ** 148840973Sbostic ** Returns: 148954974Seric ** An address descriptor for the controlling user. 149040973Sbostic ** 149140973Sbostic ** Side Effects: 149240973Sbostic ** none. 149340973Sbostic */ 149440973Sbostic 149554974Seric ADDRESS * 149654974Seric setctluser(user) 149754974Seric char *user; 149840973Sbostic { 149954974Seric register ADDRESS *a; 150040973Sbostic struct passwd *pw; 150159113Seric char *p; 150240973Sbostic 150340973Sbostic /* 150454974Seric ** See if this clears our concept of controlling user. 150540973Sbostic */ 150640973Sbostic 150763850Seric if (user == NULL || *user == '\0') 150863850Seric return NULL; 150940973Sbostic 151040973Sbostic /* 151154974Seric ** Set up addr fields for controlling user. 151240973Sbostic */ 151340973Sbostic 151454974Seric a = (ADDRESS *) xalloc(sizeof *a); 151554974Seric bzero((char *) a, sizeof *a); 151659113Seric 151759113Seric p = strchr(user, ':'); 151859113Seric if (p != NULL) 151959113Seric *p++ = '\0'; 152059270Seric if (*user != '\0' && (pw = getpwnam(user)) != NULL) 152140973Sbostic { 152265822Seric if (strcmp(pw->pw_dir, "/") == 0) 152365822Seric a->q_home = ""; 152465822Seric else 152565822Seric a->q_home = newstr(pw->pw_dir); 152640973Sbostic a->q_uid = pw->pw_uid; 152740973Sbostic a->q_gid = pw->pw_gid; 152857642Seric a->q_user = newstr(user); 152959270Seric a->q_flags |= QGOODUID; 153040973Sbostic } 153140973Sbostic else 153240973Sbostic { 153357642Seric a->q_user = newstr(DefUser); 153440973Sbostic } 153540973Sbostic 153659270Seric a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 153756328Seric a->q_mailer = LocalMailer; 153859113Seric if (p == NULL) 153959113Seric a->q_paddr = a->q_user; 154059113Seric else 154159113Seric a->q_paddr = newstr(p); 154254974Seric return a; 154340973Sbostic } 1544