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*65822Seric static char sccsid[] = "@(#)queue.c 8.37 (Berkeley) 01/22/94 (with queueing)"; 1433731Sbostic #else 15*65822Seric static char sccsid[] = "@(#)queue.c 8.37 (Berkeley) 01/22/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; 7151920Seric char buf[MAXLINE], tf[MAXLINE]; 724632Seric 735037Seric /* 7417477Seric ** Create control file. 755037Seric */ 764632Seric 7764745Seric newid = (e->e_id == NULL) || !bitset(EF_INQUEUE, e->e_flags); 7864277Seric 7964277Seric /* if newid, queuename will create a locked qf file in e->lockfp */ 8051920Seric strcpy(tf, queuename(e, 't')); 8151920Seric tfp = e->e_lockfp; 8251920Seric if (tfp == NULL) 8351920Seric newid = FALSE; 8464277Seric 8564277Seric /* if newid, just write the qf file directly (instead of tf file) */ 8664745Seric if (!newid) 8751835Seric { 8851920Seric /* get a locked tf file */ 8964070Seric for (i = 0; i < 128; i++) 9051835Seric { 9151920Seric fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode); 9251920Seric if (fd < 0) 9351835Seric { 9464070Seric if (errno != EEXIST) 9564070Seric break; 9664070Seric #ifdef LOG 9764070Seric if (LogLevel > 0 && (i % 32) == 0) 9865090Seric syslog(LOG_ALERT, "queueup: cannot create %s, uid=%d: %s", 9965090Seric tf, geteuid(), errstring(errno)); 10064070Seric #endif 10141636Srick } 10265090Seric else 10365090Seric { 10465090Seric if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB)) 10565090Seric break; 10664070Seric #ifdef LOG 10765090Seric else if (LogLevel > 0 && (i % 32) == 0) 10865090Seric syslog(LOG_ALERT, "queueup: cannot lock %s: %s", 10965090Seric tf, errstring(errno)); 11064070Seric #endif 11165090Seric close(fd); 11265090Seric } 11358689Seric 11464070Seric if ((i % 32) == 31) 11564070Seric { 11664070Seric /* save the old temp file away */ 11764070Seric (void) rename(tf, queuename(e, 'T')); 11864070Seric } 11964070Seric else 12064070Seric sleep(i % 32); 12141636Srick } 12264724Seric if (fd < 0 || (tfp = fdopen(fd, "w")) == NULL) 12364921Seric { 12464921Seric printopenfds(TRUE); 12565090Seric syserr("!queueup: cannot create queue temp file %s, uid=%d", 12665090Seric tf, geteuid()); 12764921Seric } 12851920Seric } 1294632Seric 1307677Seric if (tTd(40, 1)) 13164745Seric printf("\n>>>>> queueing %s%s >>>>>\n", e->e_id, 13264745Seric newid ? " (new id)" : ""); 13364745Seric if (tTd(40, 9)) 13464745Seric { 13564745Seric printf(" tfp="); 13664745Seric dumpfd(fileno(tfp), TRUE, FALSE); 13764745Seric printf(" lockfp="); 13864745Seric if (e->e_lockfp == NULL) 13964745Seric printf("NULL\n"); 14064745Seric else 14164745Seric dumpfd(fileno(e->e_lockfp), TRUE, FALSE); 14264745Seric } 1434632Seric 1444632Seric /* 1456980Seric ** If there is no data file yet, create one. 1466980Seric */ 1476980Seric 1486980Seric if (e->e_df == NULL) 1496980Seric { 1506980Seric register FILE *dfp; 1519389Seric extern putbody(); 1526980Seric 15364086Seric e->e_df = queuename(e, 'd'); 15464086Seric e->e_df = newstr(e->e_df); 15540934Srick fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode); 15664724Seric if (fd < 0 || (dfp = fdopen(fd, "w")) == NULL) 15765090Seric syserr("!queueup: cannot create data temp file %s, uid=%d", 15865090Seric e->e_df, geteuid()); 15959730Seric (*e->e_putbody)(dfp, FileMailer, e, NULL); 16058680Seric (void) xfclose(dfp, "queueup dfp", e->e_id); 1619389Seric e->e_putbody = putbody; 1626980Seric } 1636980Seric 1646980Seric /* 1654632Seric ** Output future work requests. 16625687Seric ** Priority and creation time should be first, since 16725687Seric ** they are required by orderq. 1684632Seric */ 1694632Seric 1709377Seric /* output message priority */ 1719377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 1729377Seric 1739630Seric /* output creation time */ 1749630Seric fprintf(tfp, "T%ld\n", e->e_ctime); 1759630Seric 17659093Seric /* output type and name of data file */ 17759093Seric if (e->e_bodytype != NULL) 17859093Seric fprintf(tfp, "B%s\n", e->e_bodytype); 1797812Seric fprintf(tfp, "D%s\n", e->e_df); 1804632Seric 18110108Seric /* message from envelope, if it exists */ 18210108Seric if (e->e_message != NULL) 18310108Seric fprintf(tfp, "M%s\n", e->e_message); 18410108Seric 18558737Seric /* send various flag bits through */ 18658737Seric p = buf; 18758737Seric if (bitset(EF_WARNING, e->e_flags)) 18858737Seric *p++ = 'w'; 18958737Seric if (bitset(EF_RESPONSE, e->e_flags)) 19058737Seric *p++ = 'r'; 19158737Seric *p++ = '\0'; 19258737Seric if (buf[0] != '\0') 19358737Seric fprintf(tfp, "F%s\n", buf); 19458737Seric 19558957Seric /* $r and $s and $_ macro values */ 19653400Seric if ((p = macvalue('r', e)) != NULL) 19753400Seric fprintf(tfp, "$r%s\n", p); 19853400Seric if ((p = macvalue('s', e)) != NULL) 19953400Seric fprintf(tfp, "$s%s\n", p); 20058957Seric if ((p = macvalue('_', e)) != NULL) 20158957Seric fprintf(tfp, "$_%s\n", p); 20253400Seric 2034632Seric /* output name of sender */ 2047812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 2054632Seric 20655360Seric /* output list of error recipients */ 20759670Seric printctladdr(NULL, NULL); 20855360Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 20955360Seric { 21058680Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 21155360Seric { 21259113Seric printctladdr(q, tfp); 21355360Seric fprintf(tfp, "E%s\n", q->q_paddr); 21455360Seric } 21555360Seric } 21655360Seric 2174632Seric /* output list of recipient addresses */ 2186980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 2194632Seric { 22058250Seric if (bitset(QQUEUEUP, q->q_flags) || 22158680Seric (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags))) 2228245Seric { 22359113Seric printctladdr(q, tfp); 2247812Seric fprintf(tfp, "R%s\n", q->q_paddr); 2259377Seric if (announce) 2269377Seric { 2279377Seric e->e_to = q->q_paddr; 22858151Seric message("queued"); 22958020Seric if (LogLevel > 8) 23064771Seric logdelivery(NULL, NULL, "queued", NULL, e); 2319377Seric e->e_to = NULL; 2329377Seric } 2339387Seric if (tTd(40, 1)) 2349387Seric { 2359387Seric printf("queueing "); 2369387Seric printaddr(q, FALSE); 2379387Seric } 2388245Seric } 2394632Seric } 2404632Seric 2419377Seric /* 2429377Seric ** Output headers for this message. 2439377Seric ** Expand macros completely here. Queue run will deal with 2449377Seric ** everything as absolute headers. 2459377Seric ** All headers that must be relative to the recipient 2469377Seric ** can be cracked later. 24710173Seric ** We set up a "null mailer" -- i.e., a mailer that will have 24810173Seric ** no effect on the addresses as they are output. 2499377Seric */ 2509377Seric 25110686Seric bzero((char *) &nullmailer, sizeof nullmailer); 25258020Seric nullmailer.m_re_rwset = nullmailer.m_rh_rwset = 25365584Seric nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1; 25410349Seric nullmailer.m_eol = "\n"; 25510173Seric 25658050Seric define('g', "\201f", e); 2576980Seric for (h = e->e_header; h != NULL; h = h->h_link) 2584632Seric { 25910686Seric extern bool bitzerop(); 26010686Seric 26112015Seric /* don't output null headers */ 2624632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 2634632Seric continue; 26412015Seric 26512015Seric /* don't output resent headers on non-resent messages */ 26612015Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 26712015Seric continue; 26812015Seric 26965267Seric /* expand macros; if null, don't output header at all */ 27065267Seric if (bitset(H_DEFAULT, h->h_flags)) 27165267Seric { 27265267Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 27365267Seric if (buf[0] == '\0') 27465267Seric continue; 27565267Seric } 27665267Seric 27712015Seric /* output this header */ 2787812Seric fprintf(tfp, "H"); 27912015Seric 28012015Seric /* if conditional, output the set of conditions */ 28110686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 28210686Seric { 28310686Seric int j; 28410686Seric 28523098Seric (void) putc('?', tfp); 28610686Seric for (j = '\0'; j <= '\177'; j++) 28710686Seric if (bitnset(j, h->h_mflags)) 28823098Seric (void) putc(j, tfp); 28923098Seric (void) putc('?', tfp); 29010686Seric } 29112015Seric 29212015Seric /* output the header: expand macros, convert addresses */ 2937763Seric if (bitset(H_DEFAULT, h->h_flags)) 2947763Seric { 29565267Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 2967763Seric } 2978245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 2989348Seric { 29958737Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 30063753Seric FILE *savetrace = TrafficLogFile; 30158737Seric 30263753Seric TrafficLogFile = NULL; 30363753Seric 30458737Seric if (bitset(H_FROM, h->h_flags)) 30558737Seric oldstyle = FALSE; 30658737Seric 30758737Seric commaize(h, h->h_value, tfp, oldstyle, 30855012Seric &nullmailer, e); 30963753Seric 31063753Seric TrafficLogFile = savetrace; 3119348Seric } 3127763Seric else 3138245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 3144632Seric } 3154632Seric 3164632Seric /* 3174632Seric ** Clean up. 3184632Seric */ 3194632Seric 32064762Seric if (fflush(tfp) < 0 || fsync(fileno(tfp)) < 0 || ferror(tfp)) 32158732Seric { 32258732Seric if (newid) 32358732Seric syserr("!552 Error writing control file %s", tf); 32458732Seric else 32558732Seric syserr("!452 Error writing control file %s", tf); 32658732Seric } 32758732Seric 32851920Seric if (!newid) 32951920Seric { 33064277Seric /* rename (locked) tf to be (locked) qf */ 33151920Seric qf = queuename(e, 'q'); 33251920Seric if (rename(tf, qf) < 0) 33365090Seric syserr("cannot rename(%s, %s), df=%s, uid=%d", 33465090Seric tf, qf, e->e_df, geteuid()); 33564277Seric 33664277Seric /* close and unlock old (locked) qf */ 33751920Seric if (e->e_lockfp != NULL) 33858680Seric (void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id); 33951920Seric e->e_lockfp = tfp; 34051920Seric } 34151920Seric else 34251920Seric qf = tf; 34341636Srick errno = 0; 34464745Seric e->e_flags |= EF_INQUEUE; 3457391Seric 3467677Seric # ifdef LOG 3477677Seric /* save log info */ 34858020Seric if (LogLevel > 79) 3497878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 35056795Seric # endif /* LOG */ 35164307Seric 35264307Seric if (tTd(40, 1)) 35364307Seric printf("<<<<< done queueing %s <<<<<\n\n", e->e_id); 35451920Seric return; 3554632Seric } 35654974Seric 35754974Seric printctladdr(a, tfp) 35859113Seric register ADDRESS *a; 35954974Seric FILE *tfp; 36054974Seric { 36159113Seric char *uname; 36259113Seric register struct passwd *pw; 36359113Seric register ADDRESS *q; 36459113Seric uid_t uid; 36559113Seric static ADDRESS *lastctladdr; 36659113Seric static uid_t lastuid; 36754974Seric 36859113Seric /* initialization */ 36963850Seric if (a == NULL || a->q_alias == NULL || tfp == NULL) 37054974Seric { 37159670Seric if (lastctladdr != NULL && tfp != NULL) 37259113Seric fprintf(tfp, "C\n"); 37359113Seric lastctladdr = NULL; 37459113Seric lastuid = 0; 37554974Seric return; 37654974Seric } 37759113Seric 37859113Seric /* find the active uid */ 37959113Seric q = getctladdr(a); 38059113Seric if (q == NULL) 38159113Seric uid = 0; 38254974Seric else 38359113Seric uid = q->q_uid; 38463850Seric a = a->q_alias; 38559113Seric 38659113Seric /* check to see if this is the same as last time */ 38759113Seric if (lastctladdr != NULL && uid == lastuid && 38859113Seric strcmp(lastctladdr->q_paddr, a->q_paddr) == 0) 38959113Seric return; 39059113Seric lastuid = uid; 39159113Seric lastctladdr = a; 39259113Seric 39359113Seric if (uid == 0 || (pw = getpwuid(uid)) == NULL) 39459270Seric uname = ""; 39559113Seric else 39659113Seric uname = pw->pw_name; 39759113Seric 39859113Seric fprintf(tfp, "C%s:%s\n", uname, a->q_paddr); 39954974Seric } 40054974Seric 4014632Seric /* 4024632Seric ** RUNQUEUE -- run the jobs in the queue. 4034632Seric ** 4044632Seric ** Gets the stuff out of the queue in some presumably logical 4054632Seric ** order and processes them. 4064632Seric ** 4074632Seric ** Parameters: 40824941Seric ** forkflag -- TRUE if the queue scanning should be done in 40924941Seric ** a child process. We double-fork so it is not our 41024941Seric ** child and we don't have to clean up after it. 4114632Seric ** 4124632Seric ** Returns: 4134632Seric ** none. 4144632Seric ** 4154632Seric ** Side Effects: 4164632Seric ** runs things in the mail queue. 4174632Seric */ 4184632Seric 41955360Seric ENVELOPE QueueEnvelope; /* the queue run envelope */ 42055360Seric 42155360Seric runqueue(forkflag) 4224639Seric bool forkflag; 4234632Seric { 42455360Seric register ENVELOPE *e; 42555360Seric extern ENVELOPE BlankEnvelope; 42624953Seric 4277466Seric /* 42824953Seric ** If no work will ever be selected, don't even bother reading 42924953Seric ** the queue. 43024953Seric */ 43124953Seric 43251920Seric CurrentLA = getla(); /* get load average */ 43340934Srick 43458132Seric if (shouldqueue(0L, curtime())) 43524953Seric { 43624953Seric if (Verbose) 43724953Seric printf("Skipping queue run -- load average too high\n"); 43858107Seric if (forkflag && QueueIntvl != 0) 43958107Seric (void) setevent(QueueIntvl, runqueue, TRUE); 44055360Seric return; 44124953Seric } 44224953Seric 44324953Seric /* 4447466Seric ** See if we want to go off and do other useful work. 4457466Seric */ 4464639Seric 4474639Seric if (forkflag) 4484639Seric { 4497943Seric int pid; 45065223Seric #ifdef SIGCHLD 45165223Seric extern void reapchild(); 4527943Seric 45365223Seric (void) setsignal(SIGCHLD, reapchild); 45465223Seric #endif 45565223Seric 4567943Seric pid = dofork(); 4577943Seric if (pid != 0) 4584639Seric { 4597943Seric /* parent -- pick up intermediate zombie */ 46025184Seric #ifndef SIGCHLD 4619377Seric (void) waitfor(pid); 46256795Seric #endif /* SIGCHLD */ 4637690Seric if (QueueIntvl != 0) 4649348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 4654639Seric return; 4664639Seric } 4677943Seric /* child -- double fork */ 46825184Seric #ifndef SIGCHLD 4697943Seric if (fork() != 0) 4707943Seric exit(EX_OK); 47156795Seric #else /* SIGCHLD */ 47264035Seric (void) setsignal(SIGCHLD, SIG_DFL); 47356795Seric #endif /* SIGCHLD */ 4744639Seric } 47524941Seric 47640934Srick setproctitle("running queue: %s", QueueDir); 47724941Seric 4787876Seric # ifdef LOG 47958020Seric if (LogLevel > 69) 48055360Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 48155360Seric QueueDir, getpid(), forkflag); 48256795Seric # endif /* LOG */ 4834639Seric 4847466Seric /* 48510205Seric ** Release any resources used by the daemon code. 48610205Seric */ 48710205Seric 48810205Seric # ifdef DAEMON 48910205Seric clrdaemon(); 49056795Seric # endif /* DAEMON */ 49110205Seric 49264658Seric /* force it to run expensive jobs */ 49364658Seric NoConnect = FALSE; 49464658Seric 49510205Seric /* 49655360Seric ** Create ourselves an envelope 49755360Seric */ 49855360Seric 49955360Seric CurEnv = &QueueEnvelope; 50058179Seric e = newenvelope(&QueueEnvelope, CurEnv); 50155360Seric e->e_flags = BlankEnvelope.e_flags; 50255360Seric 50355360Seric /* 50427175Seric ** Make sure the alias database is open. 50527175Seric */ 50627175Seric 50760537Seric initmaps(FALSE, e); 50827175Seric 50927175Seric /* 5107466Seric ** Start making passes through the queue. 5117466Seric ** First, read and sort the entire queue. 5127466Seric ** Then, process the work in that order. 5137466Seric ** But if you take too long, start over. 5147466Seric */ 5157466Seric 5167943Seric /* order the existing work requests */ 51724954Seric (void) orderq(FALSE); 5187690Seric 5197943Seric /* process them once at a time */ 5207943Seric while (WorkQ != NULL) 5214639Seric { 5227943Seric WORK *w = WorkQ; 5237881Seric 5247943Seric WorkQ = WorkQ->w_next; 52558884Seric 52658884Seric /* 52758884Seric ** Ignore jobs that are too expensive for the moment. 52858884Seric */ 52958884Seric 53058884Seric if (shouldqueue(w->w_pri, w->w_ctime)) 53158884Seric { 53258884Seric if (Verbose) 53358884Seric printf("\nSkipping %s\n", w->w_name + 2); 53458884Seric } 53558930Seric else 53658930Seric { 53764296Seric pid_t pid; 53864296Seric extern pid_t dowork(); 53964296Seric 54064296Seric pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e); 54164296Seric errno = 0; 54264296Seric (void) waitfor(pid); 54358930Seric } 5447943Seric free(w->w_name); 5457943Seric free((char *) w); 5464639Seric } 54729866Seric 54829866Seric /* exit without the usual cleanup */ 54955467Seric e->e_id = NULL; 55055467Seric finis(); 5514634Seric } 5524634Seric /* 5534632Seric ** ORDERQ -- order the work queue. 5544632Seric ** 5554632Seric ** Parameters: 55624941Seric ** doall -- if set, include everything in the queue (even 55724941Seric ** the jobs that cannot be run because the load 55824941Seric ** average is too high). Otherwise, exclude those 55924941Seric ** jobs. 5604632Seric ** 5614632Seric ** Returns: 56210121Seric ** The number of request in the queue (not necessarily 56310121Seric ** the number of requests in WorkQ however). 5644632Seric ** 5654632Seric ** Side Effects: 5664632Seric ** Sets WorkQ to the queue of available work, in order. 5674632Seric */ 5684632Seric 56925687Seric # define NEED_P 001 57025687Seric # define NEED_T 002 57158318Seric # define NEED_R 004 57258318Seric # define NEED_S 010 5734632Seric 57424941Seric orderq(doall) 57524941Seric bool doall; 5764632Seric { 57760219Seric register struct dirent *d; 5784632Seric register WORK *w; 5796625Sglickman DIR *f; 5804632Seric register int i; 58125687Seric WORK wlist[QUEUESIZE+1]; 58210070Seric int wn = -1; 5834632Seric extern workcmpf(); 5844632Seric 58558318Seric if (tTd(41, 1)) 58658318Seric { 58758318Seric printf("orderq:\n"); 58858318Seric if (QueueLimitId != NULL) 58958318Seric printf("\tQueueLimitId = %s\n", QueueLimitId); 59058318Seric if (QueueLimitSender != NULL) 59158318Seric printf("\tQueueLimitSender = %s\n", QueueLimitSender); 59258318Seric if (QueueLimitRecipient != NULL) 59358318Seric printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 59458318Seric } 59558318Seric 5964632Seric /* clear out old WorkQ */ 5974632Seric for (w = WorkQ; w != NULL; ) 5984632Seric { 5994632Seric register WORK *nw = w->w_next; 6004632Seric 6014632Seric WorkQ = nw; 6024632Seric free(w->w_name); 6034632Seric free((char *) w); 6044632Seric w = nw; 6054632Seric } 6064632Seric 6074632Seric /* open the queue directory */ 6088148Seric f = opendir("."); 6094632Seric if (f == NULL) 6104632Seric { 6118148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 61210070Seric return (0); 6134632Seric } 6144632Seric 6154632Seric /* 6164632Seric ** Read the work directory. 6174632Seric */ 6184632Seric 61910070Seric while ((d = readdir(f)) != NULL) 6204632Seric { 6219377Seric FILE *cf; 62264492Seric register char *p; 6234632Seric char lbuf[MAXNAME]; 62458318Seric extern bool strcontainedin(); 6254632Seric 6264632Seric /* is this an interesting entry? */ 6277812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 6284632Seric continue; 6294632Seric 63058318Seric if (QueueLimitId != NULL && 63158318Seric !strcontainedin(QueueLimitId, d->d_name)) 63258318Seric continue; 63358318Seric 63458722Seric /* 63558722Seric ** Check queue name for plausibility. This handles 63658722Seric ** both old and new type ids. 63758722Seric */ 63858722Seric 63964492Seric p = d->d_name + 2; 64064492Seric if (isupper(p[0]) && isupper(p[2])) 64164492Seric p += 3; 64264492Seric else if (isupper(p[1])) 64364492Seric p += 2; 64464492Seric else 64564492Seric p = d->d_name; 64664492Seric for (i = 0; isdigit(*p); p++) 64764492Seric i++; 64864492Seric if (i < 5 || *p != '\0') 64958020Seric { 65058020Seric if (Verbose) 65158020Seric printf("orderq: bogus qf name %s\n", d->d_name); 65258020Seric #ifdef LOG 65358020Seric if (LogLevel > 3) 65459615Seric syslog(LOG_CRIT, "orderq: bogus qf name %s", 65558020Seric d->d_name); 65658020Seric #endif 65758020Seric if (strlen(d->d_name) >= MAXNAME) 65858020Seric d->d_name[MAXNAME - 1] = '\0'; 65958020Seric strcpy(lbuf, d->d_name); 66058020Seric lbuf[0] = 'Q'; 66158020Seric (void) rename(d->d_name, lbuf); 66258020Seric continue; 66358020Seric } 66458020Seric 66510070Seric /* yes -- open control file (if not too many files) */ 66625687Seric if (++wn >= QUEUESIZE) 66710070Seric continue; 66858318Seric 6698148Seric cf = fopen(d->d_name, "r"); 6704632Seric if (cf == NULL) 6714632Seric { 6727055Seric /* this may be some random person sending hir msgs */ 6737055Seric /* syserr("orderq: cannot open %s", cbuf); */ 67410090Seric if (tTd(41, 2)) 67510090Seric printf("orderq: cannot open %s (%d)\n", 67610090Seric d->d_name, errno); 6777055Seric errno = 0; 67810090Seric wn--; 6794632Seric continue; 6804632Seric } 68125687Seric w = &wlist[wn]; 68225687Seric w->w_name = newstr(d->d_name); 6834632Seric 68425027Seric /* make sure jobs in creation don't clog queue */ 68525687Seric w->w_pri = 0x7fffffff; 68625687Seric w->w_ctime = 0; 68725027Seric 6884632Seric /* extract useful information */ 68925687Seric i = NEED_P | NEED_T; 69058318Seric if (QueueLimitSender != NULL) 69158318Seric i |= NEED_S; 69258318Seric if (QueueLimitRecipient != NULL) 69358318Seric i |= NEED_R; 69425687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 6954632Seric { 69624954Seric extern long atol(); 69758318Seric extern bool strcontainedin(); 69824954Seric 69924941Seric switch (lbuf[0]) 7004632Seric { 70124941Seric case 'P': 70225687Seric w->w_pri = atol(&lbuf[1]); 70325687Seric i &= ~NEED_P; 7044632Seric break; 70525013Seric 70625013Seric case 'T': 70725687Seric w->w_ctime = atol(&lbuf[1]); 70825687Seric i &= ~NEED_T; 70925013Seric break; 71058318Seric 71158318Seric case 'R': 71258318Seric if (QueueLimitRecipient != NULL && 71358318Seric strcontainedin(QueueLimitRecipient, &lbuf[1])) 71458318Seric i &= ~NEED_R; 71558318Seric break; 71658318Seric 71758318Seric case 'S': 71858318Seric if (QueueLimitSender != NULL && 71958318Seric strcontainedin(QueueLimitSender, &lbuf[1])) 72058318Seric i &= ~NEED_S; 72158318Seric break; 7224632Seric } 7234632Seric } 7244632Seric (void) fclose(cf); 72524953Seric 72658318Seric if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 72758318Seric bitset(NEED_R|NEED_S, i)) 72824953Seric { 72924953Seric /* don't even bother sorting this job in */ 73024953Seric wn--; 73124953Seric } 7324632Seric } 7336625Sglickman (void) closedir(f); 73410090Seric wn++; 7354632Seric 7364632Seric /* 7374632Seric ** Sort the work directory. 7384632Seric */ 7394632Seric 74025687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 7414632Seric 7424632Seric /* 7434632Seric ** Convert the work list into canonical form. 7449377Seric ** Should be turning it into a list of envelopes here perhaps. 7454632Seric */ 7464632Seric 74724981Seric WorkQ = NULL; 74825687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 7494632Seric { 7504632Seric w = (WORK *) xalloc(sizeof *w); 7514632Seric w->w_name = wlist[i].w_name; 7524632Seric w->w_pri = wlist[i].w_pri; 75325013Seric w->w_ctime = wlist[i].w_ctime; 75424981Seric w->w_next = WorkQ; 75524981Seric WorkQ = w; 7564632Seric } 7574632Seric 7587677Seric if (tTd(40, 1)) 7594632Seric { 7604632Seric for (w = WorkQ; w != NULL; w = w->w_next) 7615037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 7624632Seric } 76310070Seric 76410090Seric return (wn); 7654632Seric } 7664632Seric /* 7677677Seric ** WORKCMPF -- compare function for ordering work. 7684632Seric ** 7694632Seric ** Parameters: 7704632Seric ** a -- the first argument. 7714632Seric ** b -- the second argument. 7724632Seric ** 7734632Seric ** Returns: 77424981Seric ** -1 if a < b 77524981Seric ** 0 if a == b 77624981Seric ** +1 if a > b 7774632Seric ** 7784632Seric ** Side Effects: 7794632Seric ** none. 7804632Seric */ 7814632Seric 7824632Seric workcmpf(a, b) 7835037Seric register WORK *a; 7845037Seric register WORK *b; 7854632Seric { 78657438Seric long pa = a->w_pri; 78757438Seric long pb = b->w_pri; 78824941Seric 78924941Seric if (pa == pb) 7904632Seric return (0); 79124941Seric else if (pa > pb) 79224981Seric return (1); 79324981Seric else 79410121Seric return (-1); 7954632Seric } 7964632Seric /* 7974632Seric ** DOWORK -- do a work request. 7984632Seric ** 7994632Seric ** Parameters: 80058884Seric ** id -- the ID of the job to run. 80158884Seric ** forkflag -- if set, run this in background. 80258924Seric ** requeueflag -- if set, reinstantiate the queue quickly. 80358924Seric ** This is used when expanding aliases in the queue. 80461094Seric ** If forkflag is also set, it doesn't wait for the 80561094Seric ** child. 80658884Seric ** e - the envelope in which to run it. 8074632Seric ** 8084632Seric ** Returns: 80964296Seric ** process id of process that is running the queue job. 8104632Seric ** 8114632Seric ** Side Effects: 8124632Seric ** The work request is satisfied if possible. 8134632Seric */ 8144632Seric 81564296Seric pid_t 81658924Seric dowork(id, forkflag, requeueflag, e) 81758884Seric char *id; 81858884Seric bool forkflag; 81958924Seric bool requeueflag; 82055012Seric register ENVELOPE *e; 8214632Seric { 82264296Seric register pid_t pid; 82351920Seric extern bool readqf(); 8244632Seric 8257677Seric if (tTd(40, 1)) 82658884Seric printf("dowork(%s)\n", id); 8274632Seric 8284632Seric /* 82924941Seric ** Fork for work. 83024941Seric */ 83124941Seric 83258884Seric if (forkflag) 83324941Seric { 83464296Seric pid = fork(); 83564296Seric if (pid < 0) 83624941Seric { 83724941Seric syserr("dowork: cannot fork"); 83864296Seric return 0; 83924941Seric } 84064839Seric else if (pid > 0) 84164839Seric { 84264839Seric /* parent -- clean out connection cache */ 84364839Seric mci_flush(FALSE, NULL); 84464839Seric } 84524941Seric } 84624941Seric else 84724941Seric { 84864296Seric pid = 0; 84924941Seric } 85024941Seric 85164296Seric if (pid == 0) 8524632Seric { 8534632Seric /* 8544632Seric ** CHILD 8558148Seric ** Lock the control file to avoid duplicate deliveries. 8568148Seric ** Then run the file as though we had just read it. 8577350Seric ** We save an idea of the temporary name so we 8587350Seric ** can recover on interrupt. 8594632Seric */ 8604632Seric 8617763Seric /* set basic modes, etc. */ 8627356Seric (void) alarm(0); 86355012Seric clearenvelope(e, FALSE); 86464554Seric e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS; 86558734Seric e->e_errormode = EM_MAIL; 86658884Seric e->e_id = id; 86764765Seric GrabTo = UseErrorsTo = FALSE; 86863846Seric if (forkflag) 86964554Seric { 87064296Seric disconnect(1, e); 87164554Seric OpMode = MD_DELIVER; 87264554Seric } 8737876Seric # ifdef LOG 87458020Seric if (LogLevel > 76) 87555012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 8767881Seric getpid()); 87756795Seric # endif /* LOG */ 8787763Seric 8797763Seric /* don't use the headers from sendmail.cf... */ 88055012Seric e->e_header = NULL; 8817763Seric 88251920Seric /* read the queue control file -- return if locked */ 88365200Seric if (!readqf(e)) 8846980Seric { 88558884Seric if (tTd(40, 4)) 88658884Seric printf("readqf(%s) failed\n", e->e_id); 88758884Seric if (forkflag) 88824941Seric exit(EX_OK); 88924941Seric else 89024941Seric return; 8916980Seric } 8926980Seric 89355012Seric e->e_flags |= EF_INQUEUE; 89458929Seric eatheader(e, requeueflag); 8956980Seric 89658924Seric if (requeueflag) 89758924Seric queueup(e, TRUE, FALSE); 89858924Seric 8996980Seric /* do the delivery */ 90058915Seric sendall(e, SM_DELIVER); 9016980Seric 9026980Seric /* finish up and exit */ 90358884Seric if (forkflag) 90424941Seric finis(); 90524941Seric else 90655012Seric dropenvelope(e); 9074632Seric } 90864296Seric e->e_id = NULL; 90964296Seric return pid; 9104632Seric } 9114632Seric /* 9124632Seric ** READQF -- read queue file and set up environment. 9134632Seric ** 9144632Seric ** Parameters: 9159377Seric ** e -- the envelope of the job to run. 9164632Seric ** 9174632Seric ** Returns: 91851920Seric ** TRUE if it successfully read the queue file. 91951920Seric ** FALSE otherwise. 9204632Seric ** 9214632Seric ** Side Effects: 92251920Seric ** The queue file is returned locked. 9234632Seric */ 9244632Seric 92551920Seric bool 92665200Seric readqf(e) 9279377Seric register ENVELOPE *e; 9284632Seric { 92917477Seric register FILE *qfp; 93054974Seric ADDRESS *ctladdr; 93156400Seric struct stat st; 93257135Seric char *bp; 93358915Seric char qf[20]; 93457135Seric char buf[MAXLINE]; 93524954Seric extern long atol(); 93654974Seric extern ADDRESS *setctluser(); 9374632Seric 9384632Seric /* 93917468Seric ** Read and process the file. 9404632Seric */ 9414632Seric 94258915Seric strcpy(qf, queuename(e, 'q')); 94351937Seric qfp = fopen(qf, "r+"); 94417477Seric if (qfp == NULL) 94517477Seric { 94658884Seric if (tTd(40, 8)) 94758884Seric printf("readqf(%s): fopen failure (%s)\n", 94858884Seric qf, errstring(errno)); 94940934Srick if (errno != ENOENT) 95040934Srick syserr("readqf: no control file %s", qf); 95151920Seric return FALSE; 95217477Seric } 95340934Srick 95464335Seric if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB)) 95564277Seric { 95664277Seric /* being processed by another queuer */ 95764277Seric if (tTd(40, 8)) 95864277Seric printf("readqf(%s): locked\n", qf); 95964277Seric if (Verbose) 96064277Seric printf("%s: locked\n", e->e_id); 96164277Seric # ifdef LOG 96264277Seric if (LogLevel > 19) 96364277Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 96464277Seric # endif /* LOG */ 96564277Seric (void) fclose(qfp); 96664277Seric return FALSE; 96764277Seric } 96864277Seric 96956400Seric /* 97056400Seric ** Check the queue file for plausibility to avoid attacks. 97156400Seric */ 97256400Seric 97356400Seric if (fstat(fileno(qfp), &st) < 0) 97456400Seric { 97556400Seric /* must have been being processed by someone else */ 97658884Seric if (tTd(40, 8)) 97758884Seric printf("readqf(%s): fstat failure (%s)\n", 97858884Seric qf, errstring(errno)); 97956400Seric fclose(qfp); 98056400Seric return FALSE; 98156400Seric } 98256400Seric 98364137Seric if (st.st_uid != geteuid()) 98456400Seric { 98556400Seric # ifdef LOG 98656400Seric if (LogLevel > 0) 98756400Seric { 98856400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 98956400Seric e->e_id, st.st_uid, st.st_mode); 99056400Seric } 99156795Seric # endif /* LOG */ 99258884Seric if (tTd(40, 8)) 99358884Seric printf("readqf(%s): bogus file\n", qf); 99464277Seric rename(qf, queuename(e, 'Q')); 99556400Seric fclose(qfp); 99656400Seric return FALSE; 99756400Seric } 99856400Seric 99964277Seric if (st.st_size == 0) 100040934Srick { 100164277Seric /* must be a bogus file -- just remove it */ 100264277Seric (void) unlink(qf); 100364277Seric fclose(qfp); 100451920Seric return FALSE; 100540934Srick } 100640934Srick 100764277Seric if (st.st_nlink == 0) 100859101Seric { 100964277Seric /* 101064277Seric ** Race condition -- we got a file just as it was being 101164277Seric ** unlinked. Just assume it is zero length. 101264277Seric */ 101364277Seric 101459101Seric fclose(qfp); 101559101Seric return FALSE; 101659101Seric } 101759101Seric 101864277Seric /* good file -- save this lock */ 101951920Seric e->e_lockfp = qfp; 102051920Seric 102140934Srick /* do basic system initialization */ 102255012Seric initsys(e); 102365164Seric define('i', e->e_id, e); 102440934Srick 10259377Seric LineNumber = 0; 102663850Seric e->e_flags |= EF_GLOBALERRS; 102763850Seric OpMode = MD_DELIVER; 102851920Seric if (Verbose) 10299377Seric printf("\nRunning %s\n", e->e_id); 103054974Seric ctladdr = NULL; 103157135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 10324632Seric { 103358737Seric register char *p; 103457529Seric struct stat st; 103557529Seric 103626504Seric if (tTd(40, 4)) 103757135Seric printf("+++++ %s\n", bp); 103857135Seric switch (bp[0]) 10394632Seric { 104040973Sbostic case 'C': /* specify controlling user */ 104157135Seric ctladdr = setctluser(&bp[1]); 104240973Sbostic break; 104340973Sbostic 10444632Seric case 'R': /* specify recipient */ 104558082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 10464632Seric break; 10474632Seric 104825687Seric case 'E': /* specify error recipient */ 104958082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 105025687Seric break; 105125687Seric 10524632Seric case 'H': /* header */ 105357135Seric (void) chompheader(&bp[1], FALSE, e); 10544632Seric break; 10554632Seric 105610108Seric case 'M': /* message */ 105764705Seric /* ignore this; we want a new message next time */ 105810108Seric break; 105910108Seric 10604632Seric case 'S': /* sender */ 106158704Seric setsender(newstr(&bp[1]), e, NULL, TRUE); 10624632Seric break; 10634632Seric 106459093Seric case 'B': /* body type */ 106559093Seric e->e_bodytype = newstr(&bp[1]); 106659093Seric break; 106759093Seric 10684632Seric case 'D': /* data file name */ 106957135Seric e->e_df = newstr(&bp[1]); 10709544Seric e->e_dfp = fopen(e->e_df, "r"); 10719544Seric if (e->e_dfp == NULL) 107258020Seric { 10739377Seric syserr("readqf: cannot open %s", e->e_df); 107458020Seric e->e_msgsize = -1; 107558020Seric } 107658020Seric else if (fstat(fileno(e->e_dfp), &st) >= 0) 107757529Seric e->e_msgsize = st.st_size; 10784632Seric break; 10794632Seric 10807860Seric case 'T': /* init time */ 108157135Seric e->e_ctime = atol(&bp[1]); 10824632Seric break; 10834632Seric 10844634Seric case 'P': /* message priority */ 108557135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 10864634Seric break; 10874634Seric 108858737Seric case 'F': /* flag bits */ 108958737Seric for (p = &bp[1]; *p != '\0'; p++) 109058737Seric { 109158737Seric switch (*p) 109258737Seric { 109358737Seric case 'w': /* warning sent */ 109458737Seric e->e_flags |= EF_WARNING; 109558737Seric break; 109658737Seric 109758737Seric case 'r': /* response */ 109858737Seric e->e_flags |= EF_RESPONSE; 109958737Seric break; 110058737Seric } 110158737Seric } 110258737Seric break; 110358737Seric 110453400Seric case '$': /* define macro */ 110557135Seric define(bp[1], newstr(&bp[2]), e); 110653400Seric break; 110753400Seric 110824941Seric case '\0': /* blank line; ignore */ 110924941Seric break; 111024941Seric 11114632Seric default: 111265200Seric syserr("readqf: %s: line %s: bad line \"%s\"", 111365200Seric qf, LineNumber, bp); 111463753Seric fclose(qfp); 111563753Seric rename(qf, queuename(e, 'Q')); 111663753Seric return FALSE; 11174632Seric } 111857135Seric 111957135Seric if (bp != buf) 112057135Seric free(bp); 11214632Seric } 11229377Seric 112324941Seric /* 112424941Seric ** If we haven't read any lines, this queue file is empty. 112524941Seric ** Arrange to remove it without referencing any null pointers. 112624941Seric */ 112724941Seric 112824941Seric if (LineNumber == 0) 112924941Seric { 113024941Seric errno = 0; 113124941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 113224941Seric } 113351920Seric return TRUE; 11344632Seric } 11354632Seric /* 11369630Seric ** PRINTQUEUE -- print out a representation of the mail queue 11379630Seric ** 11389630Seric ** Parameters: 11399630Seric ** none. 11409630Seric ** 11419630Seric ** Returns: 11429630Seric ** none. 11439630Seric ** 11449630Seric ** Side Effects: 11459630Seric ** Prints a listing of the mail queue on the standard output. 11469630Seric */ 11475182Seric 11489630Seric printqueue() 11499630Seric { 11509630Seric register WORK *w; 11519630Seric FILE *f; 115210070Seric int nrequests; 11539630Seric char buf[MAXLINE]; 11549630Seric 11559630Seric /* 115658250Seric ** Check for permission to print the queue 115758250Seric */ 115858250Seric 115964333Seric if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0) 116058250Seric { 116158250Seric struct stat st; 116258523Seric # ifdef NGROUPS 116358523Seric int n; 116463937Seric GIDSET_T gidset[NGROUPS]; 116558523Seric # endif 116658250Seric 116758517Seric if (stat(QueueDir, &st) < 0) 116858250Seric { 116958250Seric syserr("Cannot stat %s", QueueDir); 117058250Seric return; 117158250Seric } 117258523Seric # ifdef NGROUPS 117358523Seric n = getgroups(NGROUPS, gidset); 117458523Seric while (--n >= 0) 117558523Seric { 117658523Seric if (gidset[n] == st.st_gid) 117758523Seric break; 117858523Seric } 117958523Seric if (n < 0) 118058523Seric # else 118163787Seric if (RealGid != st.st_gid) 118258523Seric # endif 118358250Seric { 118458250Seric usrerr("510 You are not permitted to see the queue"); 118558250Seric setstat(EX_NOPERM); 118658250Seric return; 118758250Seric } 118858250Seric } 118958250Seric 119058250Seric /* 11919630Seric ** Read and order the queue. 11929630Seric */ 11939630Seric 119424941Seric nrequests = orderq(TRUE); 11959630Seric 11969630Seric /* 11979630Seric ** Print the work list that we have read. 11989630Seric */ 11999630Seric 12009630Seric /* first see if there is anything */ 120110070Seric if (nrequests <= 0) 12029630Seric { 120310070Seric printf("Mail queue is empty\n"); 12049630Seric return; 12059630Seric } 12069630Seric 120751920Seric CurrentLA = getla(); /* get load average */ 120840934Srick 120910096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 121025687Seric if (nrequests > QUEUESIZE) 121125687Seric printf(", only %d printed", QUEUESIZE); 121224979Seric if (Verbose) 121358716Seric printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 121424979Seric else 121558716Seric printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 12169630Seric for (w = WorkQ; w != NULL; w = w->w_next) 12179630Seric { 12189630Seric struct stat st; 121910070Seric auto time_t submittime = 0; 122010070Seric long dfsize = -1; 122158737Seric int flags = 0; 122210108Seric char message[MAXLINE]; 122359093Seric char bodytype[MAXNAME]; 12249630Seric 122564355Seric printf("%8s", w->w_name + 2); 122617468Seric f = fopen(w->w_name, "r"); 122717468Seric if (f == NULL) 122817468Seric { 122964355Seric printf(" (job completed)\n"); 123017468Seric errno = 0; 123117468Seric continue; 123217468Seric } 123364335Seric if (!lockfile(fileno(f), w->w_name, NULL, LOCK_SH|LOCK_NB)) 123410070Seric printf("*"); 123557438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 123624941Seric printf("X"); 123710070Seric else 123810070Seric printf(" "); 123910070Seric errno = 0; 124017468Seric 124159093Seric message[0] = bodytype[0] = '\0'; 12429630Seric while (fgets(buf, sizeof buf, f) != NULL) 12439630Seric { 124453400Seric register int i; 124558737Seric register char *p; 124653400Seric 12479630Seric fixcrlf(buf, TRUE); 12489630Seric switch (buf[0]) 12499630Seric { 125010108Seric case 'M': /* error message */ 125153400Seric if ((i = strlen(&buf[1])) >= sizeof message) 125258737Seric i = sizeof message - 1; 125353400Seric bcopy(&buf[1], message, i); 125453400Seric message[i] = '\0'; 125510108Seric break; 125610108Seric 125759093Seric case 'B': /* body type */ 125859093Seric if ((i = strlen(&buf[1])) >= sizeof bodytype) 125959093Seric i = sizeof bodytype - 1; 126059093Seric bcopy(&buf[1], bodytype, i); 126159093Seric bodytype[i] = '\0'; 126259093Seric break; 126359093Seric 12649630Seric case 'S': /* sender name */ 126524979Seric if (Verbose) 126658737Seric printf("%8ld %10ld%c%.12s %.38s", 126758737Seric dfsize, 126858737Seric w->w_pri, 126958737Seric bitset(EF_WARNING, flags) ? '+' : ' ', 127058737Seric ctime(&submittime) + 4, 127124979Seric &buf[1]); 127224979Seric else 127324979Seric printf("%8ld %.16s %.45s", dfsize, 127424979Seric ctime(&submittime), &buf[1]); 127559093Seric if (message[0] != '\0' || bodytype[0] != '\0') 127659093Seric { 127759093Seric printf("\n %10.10s", bodytype); 127859093Seric if (message[0] != '\0') 127959093Seric printf(" (%.60s)", message); 128059093Seric } 12819630Seric break; 128251920Seric 128340973Sbostic case 'C': /* controlling user */ 128454974Seric if (Verbose) 128558716Seric printf("\n\t\t\t\t (---%.34s---)", 128658716Seric &buf[1]); 128740973Sbostic break; 12889630Seric 12899630Seric case 'R': /* recipient name */ 129024979Seric if (Verbose) 129158716Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 129224979Seric else 129358716Seric printf("\n\t\t\t\t %.45s", &buf[1]); 12949630Seric break; 12959630Seric 12969630Seric case 'T': /* creation time */ 129724941Seric submittime = atol(&buf[1]); 12989630Seric break; 129910070Seric 130010070Seric case 'D': /* data file name */ 130110070Seric if (stat(&buf[1], &st) >= 0) 130210070Seric dfsize = st.st_size; 130310070Seric break; 130458737Seric 130558737Seric case 'F': /* flag bits */ 130658737Seric for (p = &buf[1]; *p != '\0'; p++) 130758737Seric { 130858737Seric switch (*p) 130958737Seric { 131058737Seric case 'w': 131158737Seric flags |= EF_WARNING; 131258737Seric break; 131358737Seric } 131458737Seric } 13159630Seric } 13169630Seric } 131710070Seric if (submittime == (time_t) 0) 131810070Seric printf(" (no control file)"); 13199630Seric printf("\n"); 132023098Seric (void) fclose(f); 13219630Seric } 13229630Seric } 13239630Seric 132456795Seric # endif /* QUEUE */ 132517468Seric /* 132617468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 132717468Seric ** 132817468Seric ** Assigns an id code if one does not already exist. 132917468Seric ** This code is very careful to avoid trashing existing files 133017468Seric ** under any circumstances. 133117468Seric ** 133217468Seric ** Parameters: 133317468Seric ** e -- envelope to build it in/from. 133417468Seric ** type -- the file type, used as the first character 133517468Seric ** of the file name. 133617468Seric ** 133717468Seric ** Returns: 133817468Seric ** a pointer to the new file name (in a static buffer). 133917468Seric ** 134017468Seric ** Side Effects: 134151920Seric ** If no id code is already assigned, queuename will 134251920Seric ** assign an id code, create a qf file, and leave a 134351920Seric ** locked, open-for-write file pointer in the envelope. 134417468Seric */ 134517468Seric 134617468Seric char * 134717468Seric queuename(e, type) 134817468Seric register ENVELOPE *e; 134959700Seric int type; 135017468Seric { 135117468Seric static int pid = -1; 135258741Seric static char c0; 135358741Seric static char c1; 135458741Seric static char c2; 135558716Seric time_t now; 135658716Seric struct tm *tm; 135758689Seric static char buf[MAXNAME]; 135817468Seric 135917468Seric if (e->e_id == NULL) 136017468Seric { 136117468Seric char qf[20]; 136217468Seric 136317468Seric /* find a unique id */ 136417468Seric if (pid != getpid()) 136517468Seric { 136617468Seric /* new process -- start back at "AA" */ 136717468Seric pid = getpid(); 136858716Seric now = curtime(); 136958716Seric tm = localtime(&now); 137058716Seric c0 = 'A' + tm->tm_hour; 137117468Seric c1 = 'A'; 137217468Seric c2 = 'A' - 1; 137317468Seric } 137458716Seric (void) sprintf(qf, "qf%cAA%05d", c0, pid); 137517468Seric 137617468Seric while (c1 < '~' || c2 < 'Z') 137717468Seric { 137817468Seric int i; 137917468Seric 138017468Seric if (c2 >= 'Z') 138117468Seric { 138217468Seric c1++; 138317468Seric c2 = 'A' - 1; 138417468Seric } 138558716Seric qf[3] = c1; 138658716Seric qf[4] = ++c2; 138717468Seric if (tTd(7, 20)) 138840934Srick printf("queuename: trying \"%s\"\n", qf); 138917468Seric 139040934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 139151920Seric if (i < 0) 139251920Seric { 139351920Seric if (errno == EEXIST) 139451920Seric continue; 139564705Seric syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 139664705Seric qf, QueueDir, geteuid()); 139751920Seric exit(EX_UNAVAILABLE); 139851920Seric } 139964335Seric if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB)) 140051920Seric { 140151920Seric e->e_lockfp = fdopen(i, "w"); 140240934Srick break; 140317468Seric } 140451920Seric 140551920Seric /* a reader got the file; abandon it and try again */ 140651920Seric (void) close(i); 140717468Seric } 140817468Seric if (c1 >= '~' && c2 >= 'Z') 140917468Seric { 141064705Seric syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 141164705Seric qf, QueueDir, geteuid()); 141217468Seric exit(EX_OSERR); 141317468Seric } 141417468Seric e->e_id = newstr(&qf[2]); 141517468Seric define('i', e->e_id, e); 141617468Seric if (tTd(7, 1)) 141717468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 141864745Seric if (tTd(7, 9)) 141964745Seric { 142064745Seric printf(" lockfd="); 142164745Seric dumpfd(fileno(e->e_lockfp), TRUE, FALSE); 142264745Seric } 142317468Seric # ifdef LOG 142458020Seric if (LogLevel > 93) 142517468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 142656795Seric # endif /* LOG */ 142717468Seric } 142817468Seric 142917468Seric if (type == '\0') 143017468Seric return (NULL); 143117468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 143217468Seric if (tTd(7, 2)) 143317468Seric printf("queuename: %s\n", buf); 143417468Seric return (buf); 143517468Seric } 143617468Seric /* 143717468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 143817468Seric ** 143917468Seric ** Parameters: 144017468Seric ** e -- the envelope to unlock. 144117468Seric ** 144217468Seric ** Returns: 144317468Seric ** none 144417468Seric ** 144517468Seric ** Side Effects: 144617468Seric ** unlocks the queue for `e'. 144717468Seric */ 144817468Seric 144917468Seric unlockqueue(e) 145017468Seric ENVELOPE *e; 145117468Seric { 145258680Seric if (tTd(51, 4)) 145358680Seric printf("unlockqueue(%s)\n", e->e_id); 145458680Seric 145551920Seric /* if there is a lock file in the envelope, close it */ 145651920Seric if (e->e_lockfp != NULL) 145758680Seric xfclose(e->e_lockfp, "unlockqueue", e->e_id); 145851920Seric e->e_lockfp = NULL; 145951920Seric 146058728Seric /* don't create a queue id if we don't already have one */ 146158728Seric if (e->e_id == NULL) 146258728Seric return; 146358728Seric 146417468Seric /* remove the transcript */ 146517468Seric # ifdef LOG 146658020Seric if (LogLevel > 87) 146717468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 146856795Seric # endif /* LOG */ 146958680Seric if (!tTd(51, 104)) 147017468Seric xunlink(queuename(e, 'x')); 147117468Seric 147217468Seric } 147340973Sbostic /* 147454974Seric ** SETCTLUSER -- create a controlling address 147540973Sbostic ** 147654974Seric ** Create a fake "address" given only a local login name; this is 147754974Seric ** used as a "controlling user" for future recipient addresses. 147840973Sbostic ** 147940973Sbostic ** Parameters: 148054974Seric ** user -- the user name of the controlling user. 148140973Sbostic ** 148240973Sbostic ** Returns: 148354974Seric ** An address descriptor for the controlling user. 148440973Sbostic ** 148540973Sbostic ** Side Effects: 148640973Sbostic ** none. 148740973Sbostic */ 148840973Sbostic 148954974Seric ADDRESS * 149054974Seric setctluser(user) 149154974Seric char *user; 149240973Sbostic { 149354974Seric register ADDRESS *a; 149440973Sbostic struct passwd *pw; 149559113Seric char *p; 149640973Sbostic 149740973Sbostic /* 149854974Seric ** See if this clears our concept of controlling user. 149940973Sbostic */ 150040973Sbostic 150163850Seric if (user == NULL || *user == '\0') 150263850Seric return NULL; 150340973Sbostic 150440973Sbostic /* 150554974Seric ** Set up addr fields for controlling user. 150640973Sbostic */ 150740973Sbostic 150854974Seric a = (ADDRESS *) xalloc(sizeof *a); 150954974Seric bzero((char *) a, sizeof *a); 151059113Seric 151159113Seric p = strchr(user, ':'); 151259113Seric if (p != NULL) 151359113Seric *p++ = '\0'; 151459270Seric if (*user != '\0' && (pw = getpwnam(user)) != NULL) 151540973Sbostic { 1516*65822Seric if (strcmp(pw->pw_dir, "/") == 0) 1517*65822Seric a->q_home = ""; 1518*65822Seric else 1519*65822Seric a->q_home = newstr(pw->pw_dir); 152040973Sbostic a->q_uid = pw->pw_uid; 152140973Sbostic a->q_gid = pw->pw_gid; 152257642Seric a->q_user = newstr(user); 152359270Seric a->q_flags |= QGOODUID; 152440973Sbostic } 152540973Sbostic else 152640973Sbostic { 152757642Seric a->q_user = newstr(DefUser); 152840973Sbostic } 152940973Sbostic 153059270Seric a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 153156328Seric a->q_mailer = LocalMailer; 153259113Seric if (p == NULL) 153359113Seric a->q_paddr = a->q_user; 153459113Seric else 153559113Seric a->q_paddr = newstr(p); 153654974Seric return a; 153740973Sbostic } 1538