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*65164Seric static char sccsid[] = "@(#)queue.c 8.31 (Berkeley) 12/16/93 (with queueing)"; 1433731Sbostic #else 15*65164Seric static char sccsid[] = "@(#)queue.c 8.31 (Berkeley) 12/16/93 (without queueing)"; 1633731Sbostic #endif 1733731Sbostic #endif /* not lint */ 1833731Sbostic 194632Seric # include <errno.h> 2040973Sbostic # include <pwd.h> 2157736Seric # include <dirent.h> 224632Seric 2333731Sbostic # ifdef QUEUE 244632Seric 254632Seric /* 269377Seric ** Work queue. 279377Seric */ 289377Seric 299377Seric struct work 309377Seric { 319377Seric char *w_name; /* name of control file */ 329377Seric long w_pri; /* priority of message, see below */ 3325013Seric time_t w_ctime; /* creation time of message */ 349377Seric struct work *w_next; /* next in queue */ 359377Seric }; 369377Seric 379377Seric typedef struct work WORK; 389377Seric 399377Seric WORK *WorkQ; /* queue of things to be done */ 409377Seric /* 414632Seric ** QUEUEUP -- queue a message up for future transmission. 424632Seric ** 434632Seric ** Parameters: 446980Seric ** e -- the envelope to queue up. 456999Seric ** queueall -- if TRUE, queue all addresses, rather than 466999Seric ** just those with the QQUEUEUP flag set. 479377Seric ** announce -- if TRUE, tell when you are queueing up. 484632Seric ** 494632Seric ** Returns: 5051920Seric ** none. 514632Seric ** 524632Seric ** Side Effects: 539377Seric ** The current request are saved in a control file. 5451920Seric ** The queue file is left locked. 554632Seric */ 564632Seric 579377Seric queueup(e, queueall, announce) 586980Seric register ENVELOPE *e; 596999Seric bool queueall; 609377Seric bool announce; 614632Seric { 627812Seric char *qf; 637812Seric register FILE *tfp; 644632Seric register HDR *h; 655007Seric register ADDRESS *q; 6651920Seric int fd; 6751920Seric int i; 6851920Seric bool newid; 6953400Seric register char *p; 7010173Seric MAILER nullmailer; 7151920Seric char buf[MAXLINE], tf[MAXLINE]; 724632Seric 735037Seric /* 7417477Seric ** Create control file. 755037Seric */ 764632Seric 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 = 25364746Seric nullmailer.m_se_rwset = nullmailer.m_sh_rwset = 0; 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 26912015Seric /* output this header */ 2707812Seric fprintf(tfp, "H"); 27112015Seric 27212015Seric /* if conditional, output the set of conditions */ 27310686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 27410686Seric { 27510686Seric int j; 27610686Seric 27723098Seric (void) putc('?', tfp); 27810686Seric for (j = '\0'; j <= '\177'; j++) 27910686Seric if (bitnset(j, h->h_mflags)) 28023098Seric (void) putc(j, tfp); 28123098Seric (void) putc('?', tfp); 28210686Seric } 28312015Seric 28412015Seric /* output the header: expand macros, convert addresses */ 2857763Seric if (bitset(H_DEFAULT, h->h_flags)) 2867763Seric { 2877763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 2888236Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 2897763Seric } 2908245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 2919348Seric { 29258737Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 29363753Seric FILE *savetrace = TrafficLogFile; 29458737Seric 29563753Seric TrafficLogFile = NULL; 29663753Seric 29758737Seric if (bitset(H_FROM, h->h_flags)) 29858737Seric oldstyle = FALSE; 29958737Seric 30058737Seric commaize(h, h->h_value, tfp, oldstyle, 30155012Seric &nullmailer, e); 30263753Seric 30363753Seric TrafficLogFile = savetrace; 3049348Seric } 3057763Seric else 3068245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 3074632Seric } 3084632Seric 3094632Seric /* 3104632Seric ** Clean up. 3114632Seric */ 3124632Seric 31364762Seric if (fflush(tfp) < 0 || fsync(fileno(tfp)) < 0 || ferror(tfp)) 31458732Seric { 31558732Seric if (newid) 31658732Seric syserr("!552 Error writing control file %s", tf); 31758732Seric else 31858732Seric syserr("!452 Error writing control file %s", tf); 31958732Seric } 32058732Seric 32151920Seric if (!newid) 32251920Seric { 32364277Seric /* rename (locked) tf to be (locked) qf */ 32451920Seric qf = queuename(e, 'q'); 32551920Seric if (rename(tf, qf) < 0) 32665090Seric syserr("cannot rename(%s, %s), df=%s, uid=%d", 32765090Seric tf, qf, e->e_df, geteuid()); 32864277Seric 32964277Seric /* close and unlock old (locked) qf */ 33051920Seric if (e->e_lockfp != NULL) 33158680Seric (void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id); 33251920Seric e->e_lockfp = tfp; 33351920Seric } 33451920Seric else 33551920Seric qf = tf; 33641636Srick errno = 0; 33764745Seric e->e_flags |= EF_INQUEUE; 3387391Seric 3397677Seric # ifdef LOG 3407677Seric /* save log info */ 34158020Seric if (LogLevel > 79) 3427878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 34356795Seric # endif /* LOG */ 34464307Seric 34564307Seric if (tTd(40, 1)) 34664307Seric printf("<<<<< done queueing %s <<<<<\n\n", e->e_id); 34751920Seric return; 3484632Seric } 34954974Seric 35054974Seric printctladdr(a, tfp) 35159113Seric register ADDRESS *a; 35254974Seric FILE *tfp; 35354974Seric { 35459113Seric char *uname; 35559113Seric register struct passwd *pw; 35659113Seric register ADDRESS *q; 35759113Seric uid_t uid; 35859113Seric static ADDRESS *lastctladdr; 35959113Seric static uid_t lastuid; 36054974Seric 36159113Seric /* initialization */ 36263850Seric if (a == NULL || a->q_alias == NULL || tfp == NULL) 36354974Seric { 36459670Seric if (lastctladdr != NULL && tfp != NULL) 36559113Seric fprintf(tfp, "C\n"); 36659113Seric lastctladdr = NULL; 36759113Seric lastuid = 0; 36854974Seric return; 36954974Seric } 37059113Seric 37159113Seric /* find the active uid */ 37259113Seric q = getctladdr(a); 37359113Seric if (q == NULL) 37459113Seric uid = 0; 37554974Seric else 37659113Seric uid = q->q_uid; 37763850Seric a = a->q_alias; 37859113Seric 37959113Seric /* check to see if this is the same as last time */ 38059113Seric if (lastctladdr != NULL && uid == lastuid && 38159113Seric strcmp(lastctladdr->q_paddr, a->q_paddr) == 0) 38259113Seric return; 38359113Seric lastuid = uid; 38459113Seric lastctladdr = a; 38559113Seric 38659113Seric if (uid == 0 || (pw = getpwuid(uid)) == NULL) 38759270Seric uname = ""; 38859113Seric else 38959113Seric uname = pw->pw_name; 39059113Seric 39159113Seric fprintf(tfp, "C%s:%s\n", uname, a->q_paddr); 39254974Seric } 39354974Seric 3944632Seric /* 3954632Seric ** RUNQUEUE -- run the jobs in the queue. 3964632Seric ** 3974632Seric ** Gets the stuff out of the queue in some presumably logical 3984632Seric ** order and processes them. 3994632Seric ** 4004632Seric ** Parameters: 40124941Seric ** forkflag -- TRUE if the queue scanning should be done in 40224941Seric ** a child process. We double-fork so it is not our 40324941Seric ** child and we don't have to clean up after it. 4044632Seric ** 4054632Seric ** Returns: 4064632Seric ** none. 4074632Seric ** 4084632Seric ** Side Effects: 4094632Seric ** runs things in the mail queue. 4104632Seric */ 4114632Seric 41255360Seric ENVELOPE QueueEnvelope; /* the queue run envelope */ 41355360Seric 41455360Seric runqueue(forkflag) 4154639Seric bool forkflag; 4164632Seric { 41755360Seric register ENVELOPE *e; 41855360Seric extern ENVELOPE BlankEnvelope; 41924953Seric 4207466Seric /* 42124953Seric ** If no work will ever be selected, don't even bother reading 42224953Seric ** the queue. 42324953Seric */ 42424953Seric 42551920Seric CurrentLA = getla(); /* get load average */ 42640934Srick 42758132Seric if (shouldqueue(0L, curtime())) 42824953Seric { 42924953Seric if (Verbose) 43024953Seric printf("Skipping queue run -- load average too high\n"); 43158107Seric if (forkflag && QueueIntvl != 0) 43258107Seric (void) setevent(QueueIntvl, runqueue, TRUE); 43355360Seric return; 43424953Seric } 43524953Seric 43624953Seric /* 4377466Seric ** See if we want to go off and do other useful work. 4387466Seric */ 4394639Seric 4404639Seric if (forkflag) 4414639Seric { 4427943Seric int pid; 4437943Seric 4447943Seric pid = dofork(); 4457943Seric if (pid != 0) 4464639Seric { 44746928Sbostic extern void reapchild(); 44825184Seric 4497943Seric /* parent -- pick up intermediate zombie */ 45025184Seric #ifndef SIGCHLD 4519377Seric (void) waitfor(pid); 45256795Seric #else /* SIGCHLD */ 45364035Seric (void) setsignal(SIGCHLD, reapchild); 45456795Seric #endif /* SIGCHLD */ 4557690Seric if (QueueIntvl != 0) 4569348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 4574639Seric return; 4584639Seric } 4597943Seric /* child -- double fork */ 46025184Seric #ifndef SIGCHLD 4617943Seric if (fork() != 0) 4627943Seric exit(EX_OK); 46356795Seric #else /* SIGCHLD */ 46464035Seric (void) setsignal(SIGCHLD, SIG_DFL); 46556795Seric #endif /* SIGCHLD */ 4664639Seric } 46724941Seric 46840934Srick setproctitle("running queue: %s", QueueDir); 46924941Seric 4707876Seric # ifdef LOG 47158020Seric if (LogLevel > 69) 47255360Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 47355360Seric QueueDir, getpid(), forkflag); 47456795Seric # endif /* LOG */ 4754639Seric 4767466Seric /* 47710205Seric ** Release any resources used by the daemon code. 47810205Seric */ 47910205Seric 48010205Seric # ifdef DAEMON 48110205Seric clrdaemon(); 48256795Seric # endif /* DAEMON */ 48310205Seric 48464658Seric /* force it to run expensive jobs */ 48564658Seric NoConnect = FALSE; 48664658Seric 48710205Seric /* 48855360Seric ** Create ourselves an envelope 48955360Seric */ 49055360Seric 49155360Seric CurEnv = &QueueEnvelope; 49258179Seric e = newenvelope(&QueueEnvelope, CurEnv); 49355360Seric e->e_flags = BlankEnvelope.e_flags; 49455360Seric 49555360Seric /* 49627175Seric ** Make sure the alias database is open. 49727175Seric */ 49827175Seric 49960537Seric initmaps(FALSE, e); 50027175Seric 50127175Seric /* 5027466Seric ** Start making passes through the queue. 5037466Seric ** First, read and sort the entire queue. 5047466Seric ** Then, process the work in that order. 5057466Seric ** But if you take too long, start over. 5067466Seric */ 5077466Seric 5087943Seric /* order the existing work requests */ 50924954Seric (void) orderq(FALSE); 5107690Seric 5117943Seric /* process them once at a time */ 5127943Seric while (WorkQ != NULL) 5134639Seric { 5147943Seric WORK *w = WorkQ; 5157881Seric 5167943Seric WorkQ = WorkQ->w_next; 51758884Seric 51858884Seric /* 51958884Seric ** Ignore jobs that are too expensive for the moment. 52058884Seric */ 52158884Seric 52258884Seric if (shouldqueue(w->w_pri, w->w_ctime)) 52358884Seric { 52458884Seric if (Verbose) 52558884Seric printf("\nSkipping %s\n", w->w_name + 2); 52658884Seric } 52758930Seric else 52858930Seric { 52964296Seric pid_t pid; 53064296Seric extern pid_t dowork(); 53164296Seric 53264296Seric pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e); 53364296Seric errno = 0; 53464296Seric (void) waitfor(pid); 53558930Seric } 5367943Seric free(w->w_name); 5377943Seric free((char *) w); 5384639Seric } 53929866Seric 54029866Seric /* exit without the usual cleanup */ 54155467Seric e->e_id = NULL; 54255467Seric finis(); 5434634Seric } 5444634Seric /* 5454632Seric ** ORDERQ -- order the work queue. 5464632Seric ** 5474632Seric ** Parameters: 54824941Seric ** doall -- if set, include everything in the queue (even 54924941Seric ** the jobs that cannot be run because the load 55024941Seric ** average is too high). Otherwise, exclude those 55124941Seric ** jobs. 5524632Seric ** 5534632Seric ** Returns: 55410121Seric ** The number of request in the queue (not necessarily 55510121Seric ** the number of requests in WorkQ however). 5564632Seric ** 5574632Seric ** Side Effects: 5584632Seric ** Sets WorkQ to the queue of available work, in order. 5594632Seric */ 5604632Seric 56125687Seric # define NEED_P 001 56225687Seric # define NEED_T 002 56358318Seric # define NEED_R 004 56458318Seric # define NEED_S 010 5654632Seric 56624941Seric orderq(doall) 56724941Seric bool doall; 5684632Seric { 56960219Seric register struct dirent *d; 5704632Seric register WORK *w; 5716625Sglickman DIR *f; 5724632Seric register int i; 57325687Seric WORK wlist[QUEUESIZE+1]; 57410070Seric int wn = -1; 5754632Seric extern workcmpf(); 5764632Seric 57758318Seric if (tTd(41, 1)) 57858318Seric { 57958318Seric printf("orderq:\n"); 58058318Seric if (QueueLimitId != NULL) 58158318Seric printf("\tQueueLimitId = %s\n", QueueLimitId); 58258318Seric if (QueueLimitSender != NULL) 58358318Seric printf("\tQueueLimitSender = %s\n", QueueLimitSender); 58458318Seric if (QueueLimitRecipient != NULL) 58558318Seric printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 58658318Seric } 58758318Seric 5884632Seric /* clear out old WorkQ */ 5894632Seric for (w = WorkQ; w != NULL; ) 5904632Seric { 5914632Seric register WORK *nw = w->w_next; 5924632Seric 5934632Seric WorkQ = nw; 5944632Seric free(w->w_name); 5954632Seric free((char *) w); 5964632Seric w = nw; 5974632Seric } 5984632Seric 5994632Seric /* open the queue directory */ 6008148Seric f = opendir("."); 6014632Seric if (f == NULL) 6024632Seric { 6038148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 60410070Seric return (0); 6054632Seric } 6064632Seric 6074632Seric /* 6084632Seric ** Read the work directory. 6094632Seric */ 6104632Seric 61110070Seric while ((d = readdir(f)) != NULL) 6124632Seric { 6139377Seric FILE *cf; 61464492Seric register char *p; 6154632Seric char lbuf[MAXNAME]; 61658318Seric extern bool strcontainedin(); 6174632Seric 6184632Seric /* is this an interesting entry? */ 6197812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 6204632Seric continue; 6214632Seric 62258318Seric if (QueueLimitId != NULL && 62358318Seric !strcontainedin(QueueLimitId, d->d_name)) 62458318Seric continue; 62558318Seric 62658722Seric /* 62758722Seric ** Check queue name for plausibility. This handles 62858722Seric ** both old and new type ids. 62958722Seric */ 63058722Seric 63164492Seric p = d->d_name + 2; 63264492Seric if (isupper(p[0]) && isupper(p[2])) 63364492Seric p += 3; 63464492Seric else if (isupper(p[1])) 63564492Seric p += 2; 63664492Seric else 63764492Seric p = d->d_name; 63864492Seric for (i = 0; isdigit(*p); p++) 63964492Seric i++; 64064492Seric if (i < 5 || *p != '\0') 64158020Seric { 64258020Seric if (Verbose) 64358020Seric printf("orderq: bogus qf name %s\n", d->d_name); 64458020Seric #ifdef LOG 64558020Seric if (LogLevel > 3) 64659615Seric syslog(LOG_CRIT, "orderq: bogus qf name %s", 64758020Seric d->d_name); 64858020Seric #endif 64958020Seric if (strlen(d->d_name) >= MAXNAME) 65058020Seric d->d_name[MAXNAME - 1] = '\0'; 65158020Seric strcpy(lbuf, d->d_name); 65258020Seric lbuf[0] = 'Q'; 65358020Seric (void) rename(d->d_name, lbuf); 65458020Seric continue; 65558020Seric } 65658020Seric 65710070Seric /* yes -- open control file (if not too many files) */ 65825687Seric if (++wn >= QUEUESIZE) 65910070Seric continue; 66058318Seric 6618148Seric cf = fopen(d->d_name, "r"); 6624632Seric if (cf == NULL) 6634632Seric { 6647055Seric /* this may be some random person sending hir msgs */ 6657055Seric /* syserr("orderq: cannot open %s", cbuf); */ 66610090Seric if (tTd(41, 2)) 66710090Seric printf("orderq: cannot open %s (%d)\n", 66810090Seric d->d_name, errno); 6697055Seric errno = 0; 67010090Seric wn--; 6714632Seric continue; 6724632Seric } 67325687Seric w = &wlist[wn]; 67425687Seric w->w_name = newstr(d->d_name); 6754632Seric 67625027Seric /* make sure jobs in creation don't clog queue */ 67725687Seric w->w_pri = 0x7fffffff; 67825687Seric w->w_ctime = 0; 67925027Seric 6804632Seric /* extract useful information */ 68125687Seric i = NEED_P | NEED_T; 68258318Seric if (QueueLimitSender != NULL) 68358318Seric i |= NEED_S; 68458318Seric if (QueueLimitRecipient != NULL) 68558318Seric i |= NEED_R; 68625687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 6874632Seric { 68824954Seric extern long atol(); 68958318Seric extern bool strcontainedin(); 69024954Seric 69124941Seric switch (lbuf[0]) 6924632Seric { 69324941Seric case 'P': 69425687Seric w->w_pri = atol(&lbuf[1]); 69525687Seric i &= ~NEED_P; 6964632Seric break; 69725013Seric 69825013Seric case 'T': 69925687Seric w->w_ctime = atol(&lbuf[1]); 70025687Seric i &= ~NEED_T; 70125013Seric break; 70258318Seric 70358318Seric case 'R': 70458318Seric if (QueueLimitRecipient != NULL && 70558318Seric strcontainedin(QueueLimitRecipient, &lbuf[1])) 70658318Seric i &= ~NEED_R; 70758318Seric break; 70858318Seric 70958318Seric case 'S': 71058318Seric if (QueueLimitSender != NULL && 71158318Seric strcontainedin(QueueLimitSender, &lbuf[1])) 71258318Seric i &= ~NEED_S; 71358318Seric break; 7144632Seric } 7154632Seric } 7164632Seric (void) fclose(cf); 71724953Seric 71858318Seric if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 71958318Seric bitset(NEED_R|NEED_S, i)) 72024953Seric { 72124953Seric /* don't even bother sorting this job in */ 72224953Seric wn--; 72324953Seric } 7244632Seric } 7256625Sglickman (void) closedir(f); 72610090Seric wn++; 7274632Seric 7284632Seric /* 7294632Seric ** Sort the work directory. 7304632Seric */ 7314632Seric 73225687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 7334632Seric 7344632Seric /* 7354632Seric ** Convert the work list into canonical form. 7369377Seric ** Should be turning it into a list of envelopes here perhaps. 7374632Seric */ 7384632Seric 73924981Seric WorkQ = NULL; 74025687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 7414632Seric { 7424632Seric w = (WORK *) xalloc(sizeof *w); 7434632Seric w->w_name = wlist[i].w_name; 7444632Seric w->w_pri = wlist[i].w_pri; 74525013Seric w->w_ctime = wlist[i].w_ctime; 74624981Seric w->w_next = WorkQ; 74724981Seric WorkQ = w; 7484632Seric } 7494632Seric 7507677Seric if (tTd(40, 1)) 7514632Seric { 7524632Seric for (w = WorkQ; w != NULL; w = w->w_next) 7535037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 7544632Seric } 75510070Seric 75610090Seric return (wn); 7574632Seric } 7584632Seric /* 7597677Seric ** WORKCMPF -- compare function for ordering work. 7604632Seric ** 7614632Seric ** Parameters: 7624632Seric ** a -- the first argument. 7634632Seric ** b -- the second argument. 7644632Seric ** 7654632Seric ** Returns: 76624981Seric ** -1 if a < b 76724981Seric ** 0 if a == b 76824981Seric ** +1 if a > b 7694632Seric ** 7704632Seric ** Side Effects: 7714632Seric ** none. 7724632Seric */ 7734632Seric 7744632Seric workcmpf(a, b) 7755037Seric register WORK *a; 7765037Seric register WORK *b; 7774632Seric { 77857438Seric long pa = a->w_pri; 77957438Seric long pb = b->w_pri; 78024941Seric 78124941Seric if (pa == pb) 7824632Seric return (0); 78324941Seric else if (pa > pb) 78424981Seric return (1); 78524981Seric else 78610121Seric return (-1); 7874632Seric } 7884632Seric /* 7894632Seric ** DOWORK -- do a work request. 7904632Seric ** 7914632Seric ** Parameters: 79258884Seric ** id -- the ID of the job to run. 79358884Seric ** forkflag -- if set, run this in background. 79458924Seric ** requeueflag -- if set, reinstantiate the queue quickly. 79558924Seric ** This is used when expanding aliases in the queue. 79661094Seric ** If forkflag is also set, it doesn't wait for the 79761094Seric ** child. 79858884Seric ** e - the envelope in which to run it. 7994632Seric ** 8004632Seric ** Returns: 80164296Seric ** process id of process that is running the queue job. 8024632Seric ** 8034632Seric ** Side Effects: 8044632Seric ** The work request is satisfied if possible. 8054632Seric */ 8064632Seric 80764296Seric pid_t 80858924Seric dowork(id, forkflag, requeueflag, e) 80958884Seric char *id; 81058884Seric bool forkflag; 81158924Seric bool requeueflag; 81255012Seric register ENVELOPE *e; 8134632Seric { 81464296Seric register pid_t pid; 81551920Seric extern bool readqf(); 8164632Seric 8177677Seric if (tTd(40, 1)) 81858884Seric printf("dowork(%s)\n", id); 8194632Seric 8204632Seric /* 82124941Seric ** Fork for work. 82224941Seric */ 82324941Seric 82458884Seric if (forkflag) 82524941Seric { 82664296Seric pid = fork(); 82764296Seric if (pid < 0) 82824941Seric { 82924941Seric syserr("dowork: cannot fork"); 83064296Seric return 0; 83124941Seric } 83264839Seric else if (pid > 0) 83364839Seric { 83464839Seric /* parent -- clean out connection cache */ 83564839Seric mci_flush(FALSE, NULL); 83664839Seric } 83724941Seric } 83824941Seric else 83924941Seric { 84064296Seric pid = 0; 84124941Seric } 84224941Seric 84364296Seric if (pid == 0) 8444632Seric { 8454632Seric /* 8464632Seric ** CHILD 8478148Seric ** Lock the control file to avoid duplicate deliveries. 8488148Seric ** Then run the file as though we had just read it. 8497350Seric ** We save an idea of the temporary name so we 8507350Seric ** can recover on interrupt. 8514632Seric */ 8524632Seric 8537763Seric /* set basic modes, etc. */ 8547356Seric (void) alarm(0); 85555012Seric clearenvelope(e, FALSE); 85664554Seric e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS; 85758734Seric e->e_errormode = EM_MAIL; 85858884Seric e->e_id = id; 85964765Seric GrabTo = UseErrorsTo = FALSE; 86063846Seric if (forkflag) 86164554Seric { 86264296Seric disconnect(1, e); 86364554Seric OpMode = MD_DELIVER; 86464554Seric } 8657876Seric # ifdef LOG 86658020Seric if (LogLevel > 76) 86755012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 8687881Seric getpid()); 86956795Seric # endif /* LOG */ 8707763Seric 8717763Seric /* don't use the headers from sendmail.cf... */ 87255012Seric e->e_header = NULL; 8737763Seric 87451920Seric /* read the queue control file -- return if locked */ 87563850Seric if (!readqf(e, !requeueflag)) 8766980Seric { 87758884Seric if (tTd(40, 4)) 87858884Seric printf("readqf(%s) failed\n", e->e_id); 87958884Seric if (forkflag) 88024941Seric exit(EX_OK); 88124941Seric else 88224941Seric return; 8836980Seric } 8846980Seric 88555012Seric e->e_flags |= EF_INQUEUE; 88658929Seric eatheader(e, requeueflag); 8876980Seric 88858924Seric if (requeueflag) 88958924Seric queueup(e, TRUE, FALSE); 89058924Seric 8916980Seric /* do the delivery */ 89258915Seric sendall(e, SM_DELIVER); 8936980Seric 8946980Seric /* finish up and exit */ 89558884Seric if (forkflag) 89624941Seric finis(); 89724941Seric else 89855012Seric dropenvelope(e); 8994632Seric } 90064296Seric e->e_id = NULL; 90164296Seric return pid; 9024632Seric } 9034632Seric /* 9044632Seric ** READQF -- read queue file and set up environment. 9054632Seric ** 9064632Seric ** Parameters: 9079377Seric ** e -- the envelope of the job to run. 90863850Seric ** announcefile -- if set, announce the name of the queue 90963850Seric ** file in error messages. 9104632Seric ** 9114632Seric ** Returns: 91251920Seric ** TRUE if it successfully read the queue file. 91351920Seric ** FALSE otherwise. 9144632Seric ** 9154632Seric ** Side Effects: 91651920Seric ** The queue file is returned locked. 9174632Seric */ 9184632Seric 91951920Seric bool 92063850Seric readqf(e, announcefile) 9219377Seric register ENVELOPE *e; 92263850Seric bool announcefile; 9234632Seric { 92417477Seric register FILE *qfp; 92554974Seric ADDRESS *ctladdr; 92656400Seric struct stat st; 92757135Seric char *bp; 92858915Seric char qf[20]; 92957135Seric char buf[MAXLINE]; 93024954Seric extern long atol(); 93154974Seric extern ADDRESS *setctluser(); 9324632Seric 9334632Seric /* 93417468Seric ** Read and process the file. 9354632Seric */ 9364632Seric 93758915Seric strcpy(qf, queuename(e, 'q')); 93851937Seric qfp = fopen(qf, "r+"); 93917477Seric if (qfp == NULL) 94017477Seric { 94158884Seric if (tTd(40, 8)) 94258884Seric printf("readqf(%s): fopen failure (%s)\n", 94358884Seric qf, errstring(errno)); 94440934Srick if (errno != ENOENT) 94540934Srick syserr("readqf: no control file %s", qf); 94651920Seric return FALSE; 94717477Seric } 94840934Srick 94964335Seric if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB)) 95064277Seric { 95164277Seric /* being processed by another queuer */ 95264277Seric if (tTd(40, 8)) 95364277Seric printf("readqf(%s): locked\n", qf); 95464277Seric if (Verbose) 95564277Seric printf("%s: locked\n", e->e_id); 95664277Seric # ifdef LOG 95764277Seric if (LogLevel > 19) 95864277Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 95964277Seric # endif /* LOG */ 96064277Seric (void) fclose(qfp); 96164277Seric return FALSE; 96264277Seric } 96364277Seric 96456400Seric /* 96556400Seric ** Check the queue file for plausibility to avoid attacks. 96656400Seric */ 96756400Seric 96856400Seric if (fstat(fileno(qfp), &st) < 0) 96956400Seric { 97056400Seric /* must have been being processed by someone else */ 97158884Seric if (tTd(40, 8)) 97258884Seric printf("readqf(%s): fstat failure (%s)\n", 97358884Seric qf, errstring(errno)); 97456400Seric fclose(qfp); 97556400Seric return FALSE; 97656400Seric } 97756400Seric 97864137Seric if (st.st_uid != geteuid()) 97956400Seric { 98056400Seric # ifdef LOG 98156400Seric if (LogLevel > 0) 98256400Seric { 98356400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 98456400Seric e->e_id, st.st_uid, st.st_mode); 98556400Seric } 98656795Seric # endif /* LOG */ 98758884Seric if (tTd(40, 8)) 98858884Seric printf("readqf(%s): bogus file\n", qf); 98964277Seric rename(qf, queuename(e, 'Q')); 99056400Seric fclose(qfp); 99156400Seric return FALSE; 99256400Seric } 99356400Seric 99464277Seric if (st.st_size == 0) 99540934Srick { 99664277Seric /* must be a bogus file -- just remove it */ 99764277Seric (void) unlink(qf); 99864277Seric fclose(qfp); 99951920Seric return FALSE; 100040934Srick } 100140934Srick 100264277Seric if (st.st_nlink == 0) 100359101Seric { 100464277Seric /* 100564277Seric ** Race condition -- we got a file just as it was being 100664277Seric ** unlinked. Just assume it is zero length. 100764277Seric */ 100864277Seric 100959101Seric fclose(qfp); 101059101Seric return FALSE; 101159101Seric } 101259101Seric 101364277Seric /* good file -- save this lock */ 101451920Seric e->e_lockfp = qfp; 101551920Seric 101640934Srick /* do basic system initialization */ 101755012Seric initsys(e); 1018*65164Seric define('i', e->e_id, e); 101940934Srick 102063850Seric if (announcefile) 102163850Seric FileName = qf; 10229377Seric LineNumber = 0; 102363850Seric e->e_flags |= EF_GLOBALERRS; 102463850Seric OpMode = MD_DELIVER; 102551920Seric if (Verbose) 10269377Seric printf("\nRunning %s\n", e->e_id); 102754974Seric ctladdr = NULL; 102857135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 10294632Seric { 103058737Seric register char *p; 103157529Seric struct stat st; 103257529Seric 103326504Seric if (tTd(40, 4)) 103457135Seric printf("+++++ %s\n", bp); 103557135Seric switch (bp[0]) 10364632Seric { 103740973Sbostic case 'C': /* specify controlling user */ 103857135Seric ctladdr = setctluser(&bp[1]); 103940973Sbostic break; 104040973Sbostic 10414632Seric case 'R': /* specify recipient */ 104258082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 10434632Seric break; 10444632Seric 104525687Seric case 'E': /* specify error recipient */ 104658082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 104725687Seric break; 104825687Seric 10494632Seric case 'H': /* header */ 105057135Seric (void) chompheader(&bp[1], FALSE, e); 10514632Seric break; 10524632Seric 105310108Seric case 'M': /* message */ 105464705Seric /* ignore this; we want a new message next time */ 105510108Seric break; 105610108Seric 10574632Seric case 'S': /* sender */ 105858704Seric setsender(newstr(&bp[1]), e, NULL, TRUE); 10594632Seric break; 10604632Seric 106159093Seric case 'B': /* body type */ 106259093Seric e->e_bodytype = newstr(&bp[1]); 106359093Seric break; 106459093Seric 10654632Seric case 'D': /* data file name */ 106657135Seric e->e_df = newstr(&bp[1]); 10679544Seric e->e_dfp = fopen(e->e_df, "r"); 10689544Seric if (e->e_dfp == NULL) 106958020Seric { 10709377Seric syserr("readqf: cannot open %s", e->e_df); 107158020Seric e->e_msgsize = -1; 107258020Seric } 107358020Seric else if (fstat(fileno(e->e_dfp), &st) >= 0) 107457529Seric e->e_msgsize = st.st_size; 10754632Seric break; 10764632Seric 10777860Seric case 'T': /* init time */ 107857135Seric e->e_ctime = atol(&bp[1]); 10794632Seric break; 10804632Seric 10814634Seric case 'P': /* message priority */ 108257135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 10834634Seric break; 10844634Seric 108558737Seric case 'F': /* flag bits */ 108658737Seric for (p = &bp[1]; *p != '\0'; p++) 108758737Seric { 108858737Seric switch (*p) 108958737Seric { 109058737Seric case 'w': /* warning sent */ 109158737Seric e->e_flags |= EF_WARNING; 109258737Seric break; 109358737Seric 109458737Seric case 'r': /* response */ 109558737Seric e->e_flags |= EF_RESPONSE; 109658737Seric break; 109758737Seric } 109858737Seric } 109958737Seric break; 110058737Seric 110153400Seric case '$': /* define macro */ 110257135Seric define(bp[1], newstr(&bp[2]), e); 110353400Seric break; 110453400Seric 110524941Seric case '\0': /* blank line; ignore */ 110624941Seric break; 110724941Seric 11084632Seric default: 110959700Seric syserr("readqf: bad line \"%s\"", e->e_id, 111057135Seric LineNumber, bp); 111163753Seric fclose(qfp); 111263753Seric rename(qf, queuename(e, 'Q')); 111363753Seric return FALSE; 11144632Seric } 111557135Seric 111657135Seric if (bp != buf) 111757135Seric free(bp); 11184632Seric } 11199377Seric 11209377Seric FileName = NULL; 112124941Seric 112224941Seric /* 112324941Seric ** If we haven't read any lines, this queue file is empty. 112424941Seric ** Arrange to remove it without referencing any null pointers. 112524941Seric */ 112624941Seric 112724941Seric if (LineNumber == 0) 112824941Seric { 112924941Seric errno = 0; 113024941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 113124941Seric } 113251920Seric return TRUE; 11334632Seric } 11344632Seric /* 11359630Seric ** PRINTQUEUE -- print out a representation of the mail queue 11369630Seric ** 11379630Seric ** Parameters: 11389630Seric ** none. 11399630Seric ** 11409630Seric ** Returns: 11419630Seric ** none. 11429630Seric ** 11439630Seric ** Side Effects: 11449630Seric ** Prints a listing of the mail queue on the standard output. 11459630Seric */ 11465182Seric 11479630Seric printqueue() 11489630Seric { 11499630Seric register WORK *w; 11509630Seric FILE *f; 115110070Seric int nrequests; 11529630Seric char buf[MAXLINE]; 11539630Seric 11549630Seric /* 115558250Seric ** Check for permission to print the queue 115658250Seric */ 115758250Seric 115864333Seric if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0) 115958250Seric { 116058250Seric struct stat st; 116158523Seric # ifdef NGROUPS 116258523Seric int n; 116363937Seric GIDSET_T gidset[NGROUPS]; 116458523Seric # endif 116558250Seric 116658517Seric if (stat(QueueDir, &st) < 0) 116758250Seric { 116858250Seric syserr("Cannot stat %s", QueueDir); 116958250Seric return; 117058250Seric } 117158523Seric # ifdef NGROUPS 117258523Seric n = getgroups(NGROUPS, gidset); 117358523Seric while (--n >= 0) 117458523Seric { 117558523Seric if (gidset[n] == st.st_gid) 117658523Seric break; 117758523Seric } 117858523Seric if (n < 0) 117958523Seric # else 118063787Seric if (RealGid != st.st_gid) 118158523Seric # endif 118258250Seric { 118358250Seric usrerr("510 You are not permitted to see the queue"); 118458250Seric setstat(EX_NOPERM); 118558250Seric return; 118658250Seric } 118758250Seric } 118858250Seric 118958250Seric /* 11909630Seric ** Read and order the queue. 11919630Seric */ 11929630Seric 119324941Seric nrequests = orderq(TRUE); 11949630Seric 11959630Seric /* 11969630Seric ** Print the work list that we have read. 11979630Seric */ 11989630Seric 11999630Seric /* first see if there is anything */ 120010070Seric if (nrequests <= 0) 12019630Seric { 120210070Seric printf("Mail queue is empty\n"); 12039630Seric return; 12049630Seric } 12059630Seric 120651920Seric CurrentLA = getla(); /* get load average */ 120740934Srick 120810096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 120925687Seric if (nrequests > QUEUESIZE) 121025687Seric printf(", only %d printed", QUEUESIZE); 121124979Seric if (Verbose) 121258716Seric printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 121324979Seric else 121458716Seric printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 12159630Seric for (w = WorkQ; w != NULL; w = w->w_next) 12169630Seric { 12179630Seric struct stat st; 121810070Seric auto time_t submittime = 0; 121910070Seric long dfsize = -1; 122058737Seric int flags = 0; 122110108Seric char message[MAXLINE]; 122259093Seric char bodytype[MAXNAME]; 12239630Seric 122464355Seric printf("%8s", w->w_name + 2); 122517468Seric f = fopen(w->w_name, "r"); 122617468Seric if (f == NULL) 122717468Seric { 122864355Seric printf(" (job completed)\n"); 122917468Seric errno = 0; 123017468Seric continue; 123117468Seric } 123264335Seric if (!lockfile(fileno(f), w->w_name, NULL, LOCK_SH|LOCK_NB)) 123310070Seric printf("*"); 123457438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 123524941Seric printf("X"); 123610070Seric else 123710070Seric printf(" "); 123810070Seric errno = 0; 123917468Seric 124059093Seric message[0] = bodytype[0] = '\0'; 12419630Seric while (fgets(buf, sizeof buf, f) != NULL) 12429630Seric { 124353400Seric register int i; 124458737Seric register char *p; 124553400Seric 12469630Seric fixcrlf(buf, TRUE); 12479630Seric switch (buf[0]) 12489630Seric { 124910108Seric case 'M': /* error message */ 125053400Seric if ((i = strlen(&buf[1])) >= sizeof message) 125158737Seric i = sizeof message - 1; 125253400Seric bcopy(&buf[1], message, i); 125353400Seric message[i] = '\0'; 125410108Seric break; 125510108Seric 125659093Seric case 'B': /* body type */ 125759093Seric if ((i = strlen(&buf[1])) >= sizeof bodytype) 125859093Seric i = sizeof bodytype - 1; 125959093Seric bcopy(&buf[1], bodytype, i); 126059093Seric bodytype[i] = '\0'; 126159093Seric break; 126259093Seric 12639630Seric case 'S': /* sender name */ 126424979Seric if (Verbose) 126558737Seric printf("%8ld %10ld%c%.12s %.38s", 126658737Seric dfsize, 126758737Seric w->w_pri, 126858737Seric bitset(EF_WARNING, flags) ? '+' : ' ', 126958737Seric ctime(&submittime) + 4, 127024979Seric &buf[1]); 127124979Seric else 127224979Seric printf("%8ld %.16s %.45s", dfsize, 127324979Seric ctime(&submittime), &buf[1]); 127459093Seric if (message[0] != '\0' || bodytype[0] != '\0') 127559093Seric { 127659093Seric printf("\n %10.10s", bodytype); 127759093Seric if (message[0] != '\0') 127859093Seric printf(" (%.60s)", message); 127959093Seric } 12809630Seric break; 128151920Seric 128240973Sbostic case 'C': /* controlling user */ 128354974Seric if (Verbose) 128458716Seric printf("\n\t\t\t\t (---%.34s---)", 128558716Seric &buf[1]); 128640973Sbostic break; 12879630Seric 12889630Seric case 'R': /* recipient name */ 128924979Seric if (Verbose) 129058716Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 129124979Seric else 129258716Seric printf("\n\t\t\t\t %.45s", &buf[1]); 12939630Seric break; 12949630Seric 12959630Seric case 'T': /* creation time */ 129624941Seric submittime = atol(&buf[1]); 12979630Seric break; 129810070Seric 129910070Seric case 'D': /* data file name */ 130010070Seric if (stat(&buf[1], &st) >= 0) 130110070Seric dfsize = st.st_size; 130210070Seric break; 130358737Seric 130458737Seric case 'F': /* flag bits */ 130558737Seric for (p = &buf[1]; *p != '\0'; p++) 130658737Seric { 130758737Seric switch (*p) 130858737Seric { 130958737Seric case 'w': 131058737Seric flags |= EF_WARNING; 131158737Seric break; 131258737Seric } 131358737Seric } 13149630Seric } 13159630Seric } 131610070Seric if (submittime == (time_t) 0) 131710070Seric printf(" (no control file)"); 13189630Seric printf("\n"); 131923098Seric (void) fclose(f); 13209630Seric } 13219630Seric } 13229630Seric 132356795Seric # endif /* QUEUE */ 132417468Seric /* 132517468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 132617468Seric ** 132717468Seric ** Assigns an id code if one does not already exist. 132817468Seric ** This code is very careful to avoid trashing existing files 132917468Seric ** under any circumstances. 133017468Seric ** 133117468Seric ** Parameters: 133217468Seric ** e -- envelope to build it in/from. 133317468Seric ** type -- the file type, used as the first character 133417468Seric ** of the file name. 133517468Seric ** 133617468Seric ** Returns: 133717468Seric ** a pointer to the new file name (in a static buffer). 133817468Seric ** 133917468Seric ** Side Effects: 134051920Seric ** If no id code is already assigned, queuename will 134151920Seric ** assign an id code, create a qf file, and leave a 134251920Seric ** locked, open-for-write file pointer in the envelope. 134317468Seric */ 134417468Seric 134517468Seric char * 134617468Seric queuename(e, type) 134717468Seric register ENVELOPE *e; 134859700Seric int type; 134917468Seric { 135017468Seric static int pid = -1; 135158741Seric static char c0; 135258741Seric static char c1; 135358741Seric static char c2; 135458716Seric time_t now; 135558716Seric struct tm *tm; 135658689Seric static char buf[MAXNAME]; 135717468Seric 135817468Seric if (e->e_id == NULL) 135917468Seric { 136017468Seric char qf[20]; 136117468Seric 136217468Seric /* find a unique id */ 136317468Seric if (pid != getpid()) 136417468Seric { 136517468Seric /* new process -- start back at "AA" */ 136617468Seric pid = getpid(); 136758716Seric now = curtime(); 136858716Seric tm = localtime(&now); 136958716Seric c0 = 'A' + tm->tm_hour; 137017468Seric c1 = 'A'; 137117468Seric c2 = 'A' - 1; 137217468Seric } 137358716Seric (void) sprintf(qf, "qf%cAA%05d", c0, pid); 137417468Seric 137517468Seric while (c1 < '~' || c2 < 'Z') 137617468Seric { 137717468Seric int i; 137817468Seric 137917468Seric if (c2 >= 'Z') 138017468Seric { 138117468Seric c1++; 138217468Seric c2 = 'A' - 1; 138317468Seric } 138458716Seric qf[3] = c1; 138558716Seric qf[4] = ++c2; 138617468Seric if (tTd(7, 20)) 138740934Srick printf("queuename: trying \"%s\"\n", qf); 138817468Seric 138940934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 139051920Seric if (i < 0) 139151920Seric { 139251920Seric if (errno == EEXIST) 139351920Seric continue; 139464705Seric syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 139564705Seric qf, QueueDir, geteuid()); 139651920Seric exit(EX_UNAVAILABLE); 139751920Seric } 139864335Seric if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB)) 139951920Seric { 140051920Seric e->e_lockfp = fdopen(i, "w"); 140140934Srick break; 140217468Seric } 140351920Seric 140451920Seric /* a reader got the file; abandon it and try again */ 140551920Seric (void) close(i); 140617468Seric } 140717468Seric if (c1 >= '~' && c2 >= 'Z') 140817468Seric { 140964705Seric syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 141064705Seric qf, QueueDir, geteuid()); 141117468Seric exit(EX_OSERR); 141217468Seric } 141317468Seric e->e_id = newstr(&qf[2]); 141417468Seric define('i', e->e_id, e); 141517468Seric if (tTd(7, 1)) 141617468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 141764745Seric if (tTd(7, 9)) 141864745Seric { 141964745Seric printf(" lockfd="); 142064745Seric dumpfd(fileno(e->e_lockfp), TRUE, FALSE); 142164745Seric } 142217468Seric # ifdef LOG 142358020Seric if (LogLevel > 93) 142417468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 142556795Seric # endif /* LOG */ 142617468Seric } 142717468Seric 142817468Seric if (type == '\0') 142917468Seric return (NULL); 143017468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 143117468Seric if (tTd(7, 2)) 143217468Seric printf("queuename: %s\n", buf); 143317468Seric return (buf); 143417468Seric } 143517468Seric /* 143617468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 143717468Seric ** 143817468Seric ** Parameters: 143917468Seric ** e -- the envelope to unlock. 144017468Seric ** 144117468Seric ** Returns: 144217468Seric ** none 144317468Seric ** 144417468Seric ** Side Effects: 144517468Seric ** unlocks the queue for `e'. 144617468Seric */ 144717468Seric 144817468Seric unlockqueue(e) 144917468Seric ENVELOPE *e; 145017468Seric { 145158680Seric if (tTd(51, 4)) 145258680Seric printf("unlockqueue(%s)\n", e->e_id); 145358680Seric 145451920Seric /* if there is a lock file in the envelope, close it */ 145551920Seric if (e->e_lockfp != NULL) 145658680Seric xfclose(e->e_lockfp, "unlockqueue", e->e_id); 145751920Seric e->e_lockfp = NULL; 145851920Seric 145958728Seric /* don't create a queue id if we don't already have one */ 146058728Seric if (e->e_id == NULL) 146158728Seric return; 146258728Seric 146317468Seric /* remove the transcript */ 146417468Seric # ifdef LOG 146558020Seric if (LogLevel > 87) 146617468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 146756795Seric # endif /* LOG */ 146858680Seric if (!tTd(51, 104)) 146917468Seric xunlink(queuename(e, 'x')); 147017468Seric 147117468Seric } 147240973Sbostic /* 147354974Seric ** SETCTLUSER -- create a controlling address 147440973Sbostic ** 147554974Seric ** Create a fake "address" given only a local login name; this is 147654974Seric ** used as a "controlling user" for future recipient addresses. 147740973Sbostic ** 147840973Sbostic ** Parameters: 147954974Seric ** user -- the user name of the controlling user. 148040973Sbostic ** 148140973Sbostic ** Returns: 148254974Seric ** An address descriptor for the controlling user. 148340973Sbostic ** 148440973Sbostic ** Side Effects: 148540973Sbostic ** none. 148640973Sbostic */ 148740973Sbostic 148854974Seric ADDRESS * 148954974Seric setctluser(user) 149054974Seric char *user; 149140973Sbostic { 149254974Seric register ADDRESS *a; 149340973Sbostic struct passwd *pw; 149459113Seric char *p; 149540973Sbostic 149640973Sbostic /* 149754974Seric ** See if this clears our concept of controlling user. 149840973Sbostic */ 149940973Sbostic 150063850Seric if (user == NULL || *user == '\0') 150163850Seric return NULL; 150240973Sbostic 150340973Sbostic /* 150454974Seric ** Set up addr fields for controlling user. 150540973Sbostic */ 150640973Sbostic 150754974Seric a = (ADDRESS *) xalloc(sizeof *a); 150854974Seric bzero((char *) a, sizeof *a); 150959113Seric 151059113Seric p = strchr(user, ':'); 151159113Seric if (p != NULL) 151259113Seric *p++ = '\0'; 151359270Seric if (*user != '\0' && (pw = getpwnam(user)) != NULL) 151440973Sbostic { 151540973Sbostic a->q_home = newstr(pw->pw_dir); 151640973Sbostic a->q_uid = pw->pw_uid; 151740973Sbostic a->q_gid = pw->pw_gid; 151857642Seric a->q_user = newstr(user); 151959270Seric a->q_flags |= QGOODUID; 152040973Sbostic } 152140973Sbostic else 152240973Sbostic { 152357642Seric a->q_user = newstr(DefUser); 152440973Sbostic } 152540973Sbostic 152659270Seric a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 152756328Seric a->q_mailer = LocalMailer; 152859113Seric if (p == NULL) 152959113Seric a->q_paddr = a->q_user; 153059113Seric else 153159113Seric a->q_paddr = newstr(p); 153254974Seric return a; 153340973Sbostic } 1534