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*65090Seric static char sccsid[] = "@(#)queue.c 8.30 (Berkeley) 12/11/93 (with queueing)"; 1433731Sbostic #else 15*65090Seric static char sccsid[] = "@(#)queue.c 8.30 (Berkeley) 12/11/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) 98*65090Seric syslog(LOG_ALERT, "queueup: cannot create %s, uid=%d: %s", 99*65090Seric tf, geteuid(), errstring(errno)); 10064070Seric #endif 10141636Srick } 102*65090Seric else 103*65090Seric { 104*65090Seric if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB)) 105*65090Seric break; 10664070Seric #ifdef LOG 107*65090Seric else if (LogLevel > 0 && (i % 32) == 0) 108*65090Seric syslog(LOG_ALERT, "queueup: cannot lock %s: %s", 109*65090Seric tf, errstring(errno)); 11064070Seric #endif 111*65090Seric close(fd); 112*65090Seric } 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); 125*65090Seric syserr("!queueup: cannot create queue temp file %s, uid=%d", 126*65090Seric 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) 157*65090Seric syserr("!queueup: cannot create data temp file %s, uid=%d", 158*65090Seric 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) 326*65090Seric syserr("cannot rename(%s, %s), df=%s, uid=%d", 327*65090Seric 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); 101840934Srick 101963850Seric if (announcefile) 102063850Seric FileName = qf; 10219377Seric LineNumber = 0; 102263850Seric e->e_flags |= EF_GLOBALERRS; 102363850Seric OpMode = MD_DELIVER; 102451920Seric if (Verbose) 10259377Seric printf("\nRunning %s\n", e->e_id); 102654974Seric ctladdr = NULL; 102757135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 10284632Seric { 102958737Seric register char *p; 103057529Seric struct stat st; 103157529Seric 103226504Seric if (tTd(40, 4)) 103357135Seric printf("+++++ %s\n", bp); 103457135Seric switch (bp[0]) 10354632Seric { 103640973Sbostic case 'C': /* specify controlling user */ 103757135Seric ctladdr = setctluser(&bp[1]); 103840973Sbostic break; 103940973Sbostic 10404632Seric case 'R': /* specify recipient */ 104158082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 10424632Seric break; 10434632Seric 104425687Seric case 'E': /* specify error recipient */ 104558082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 104625687Seric break; 104725687Seric 10484632Seric case 'H': /* header */ 104957135Seric (void) chompheader(&bp[1], FALSE, e); 10504632Seric break; 10514632Seric 105210108Seric case 'M': /* message */ 105364705Seric /* ignore this; we want a new message next time */ 105410108Seric break; 105510108Seric 10564632Seric case 'S': /* sender */ 105758704Seric setsender(newstr(&bp[1]), e, NULL, TRUE); 10584632Seric break; 10594632Seric 106059093Seric case 'B': /* body type */ 106159093Seric e->e_bodytype = newstr(&bp[1]); 106259093Seric break; 106359093Seric 10644632Seric case 'D': /* data file name */ 106557135Seric e->e_df = newstr(&bp[1]); 10669544Seric e->e_dfp = fopen(e->e_df, "r"); 10679544Seric if (e->e_dfp == NULL) 106858020Seric { 10699377Seric syserr("readqf: cannot open %s", e->e_df); 107058020Seric e->e_msgsize = -1; 107158020Seric } 107258020Seric else if (fstat(fileno(e->e_dfp), &st) >= 0) 107357529Seric e->e_msgsize = st.st_size; 10744632Seric break; 10754632Seric 10767860Seric case 'T': /* init time */ 107757135Seric e->e_ctime = atol(&bp[1]); 10784632Seric break; 10794632Seric 10804634Seric case 'P': /* message priority */ 108157135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 10824634Seric break; 10834634Seric 108458737Seric case 'F': /* flag bits */ 108558737Seric for (p = &bp[1]; *p != '\0'; p++) 108658737Seric { 108758737Seric switch (*p) 108858737Seric { 108958737Seric case 'w': /* warning sent */ 109058737Seric e->e_flags |= EF_WARNING; 109158737Seric break; 109258737Seric 109358737Seric case 'r': /* response */ 109458737Seric e->e_flags |= EF_RESPONSE; 109558737Seric break; 109658737Seric } 109758737Seric } 109858737Seric break; 109958737Seric 110053400Seric case '$': /* define macro */ 110157135Seric define(bp[1], newstr(&bp[2]), e); 110253400Seric break; 110353400Seric 110424941Seric case '\0': /* blank line; ignore */ 110524941Seric break; 110624941Seric 11074632Seric default: 110859700Seric syserr("readqf: bad line \"%s\"", e->e_id, 110957135Seric LineNumber, bp); 111063753Seric fclose(qfp); 111163753Seric rename(qf, queuename(e, 'Q')); 111263753Seric return FALSE; 11134632Seric } 111457135Seric 111557135Seric if (bp != buf) 111657135Seric free(bp); 11174632Seric } 11189377Seric 11199377Seric FileName = NULL; 112024941Seric 112124941Seric /* 112224941Seric ** If we haven't read any lines, this queue file is empty. 112324941Seric ** Arrange to remove it without referencing any null pointers. 112424941Seric */ 112524941Seric 112624941Seric if (LineNumber == 0) 112724941Seric { 112824941Seric errno = 0; 112924941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 113024941Seric } 113151920Seric return TRUE; 11324632Seric } 11334632Seric /* 11349630Seric ** PRINTQUEUE -- print out a representation of the mail queue 11359630Seric ** 11369630Seric ** Parameters: 11379630Seric ** none. 11389630Seric ** 11399630Seric ** Returns: 11409630Seric ** none. 11419630Seric ** 11429630Seric ** Side Effects: 11439630Seric ** Prints a listing of the mail queue on the standard output. 11449630Seric */ 11455182Seric 11469630Seric printqueue() 11479630Seric { 11489630Seric register WORK *w; 11499630Seric FILE *f; 115010070Seric int nrequests; 11519630Seric char buf[MAXLINE]; 11529630Seric 11539630Seric /* 115458250Seric ** Check for permission to print the queue 115558250Seric */ 115658250Seric 115764333Seric if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0) 115858250Seric { 115958250Seric struct stat st; 116058523Seric # ifdef NGROUPS 116158523Seric int n; 116263937Seric GIDSET_T gidset[NGROUPS]; 116358523Seric # endif 116458250Seric 116558517Seric if (stat(QueueDir, &st) < 0) 116658250Seric { 116758250Seric syserr("Cannot stat %s", QueueDir); 116858250Seric return; 116958250Seric } 117058523Seric # ifdef NGROUPS 117158523Seric n = getgroups(NGROUPS, gidset); 117258523Seric while (--n >= 0) 117358523Seric { 117458523Seric if (gidset[n] == st.st_gid) 117558523Seric break; 117658523Seric } 117758523Seric if (n < 0) 117858523Seric # else 117963787Seric if (RealGid != st.st_gid) 118058523Seric # endif 118158250Seric { 118258250Seric usrerr("510 You are not permitted to see the queue"); 118358250Seric setstat(EX_NOPERM); 118458250Seric return; 118558250Seric } 118658250Seric } 118758250Seric 118858250Seric /* 11899630Seric ** Read and order the queue. 11909630Seric */ 11919630Seric 119224941Seric nrequests = orderq(TRUE); 11939630Seric 11949630Seric /* 11959630Seric ** Print the work list that we have read. 11969630Seric */ 11979630Seric 11989630Seric /* first see if there is anything */ 119910070Seric if (nrequests <= 0) 12009630Seric { 120110070Seric printf("Mail queue is empty\n"); 12029630Seric return; 12039630Seric } 12049630Seric 120551920Seric CurrentLA = getla(); /* get load average */ 120640934Srick 120710096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 120825687Seric if (nrequests > QUEUESIZE) 120925687Seric printf(", only %d printed", QUEUESIZE); 121024979Seric if (Verbose) 121158716Seric printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 121224979Seric else 121358716Seric printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 12149630Seric for (w = WorkQ; w != NULL; w = w->w_next) 12159630Seric { 12169630Seric struct stat st; 121710070Seric auto time_t submittime = 0; 121810070Seric long dfsize = -1; 121958737Seric int flags = 0; 122010108Seric char message[MAXLINE]; 122159093Seric char bodytype[MAXNAME]; 12229630Seric 122364355Seric printf("%8s", w->w_name + 2); 122417468Seric f = fopen(w->w_name, "r"); 122517468Seric if (f == NULL) 122617468Seric { 122764355Seric printf(" (job completed)\n"); 122817468Seric errno = 0; 122917468Seric continue; 123017468Seric } 123164335Seric if (!lockfile(fileno(f), w->w_name, NULL, LOCK_SH|LOCK_NB)) 123210070Seric printf("*"); 123357438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 123424941Seric printf("X"); 123510070Seric else 123610070Seric printf(" "); 123710070Seric errno = 0; 123817468Seric 123959093Seric message[0] = bodytype[0] = '\0'; 12409630Seric while (fgets(buf, sizeof buf, f) != NULL) 12419630Seric { 124253400Seric register int i; 124358737Seric register char *p; 124453400Seric 12459630Seric fixcrlf(buf, TRUE); 12469630Seric switch (buf[0]) 12479630Seric { 124810108Seric case 'M': /* error message */ 124953400Seric if ((i = strlen(&buf[1])) >= sizeof message) 125058737Seric i = sizeof message - 1; 125153400Seric bcopy(&buf[1], message, i); 125253400Seric message[i] = '\0'; 125310108Seric break; 125410108Seric 125559093Seric case 'B': /* body type */ 125659093Seric if ((i = strlen(&buf[1])) >= sizeof bodytype) 125759093Seric i = sizeof bodytype - 1; 125859093Seric bcopy(&buf[1], bodytype, i); 125959093Seric bodytype[i] = '\0'; 126059093Seric break; 126159093Seric 12629630Seric case 'S': /* sender name */ 126324979Seric if (Verbose) 126458737Seric printf("%8ld %10ld%c%.12s %.38s", 126558737Seric dfsize, 126658737Seric w->w_pri, 126758737Seric bitset(EF_WARNING, flags) ? '+' : ' ', 126858737Seric ctime(&submittime) + 4, 126924979Seric &buf[1]); 127024979Seric else 127124979Seric printf("%8ld %.16s %.45s", dfsize, 127224979Seric ctime(&submittime), &buf[1]); 127359093Seric if (message[0] != '\0' || bodytype[0] != '\0') 127459093Seric { 127559093Seric printf("\n %10.10s", bodytype); 127659093Seric if (message[0] != '\0') 127759093Seric printf(" (%.60s)", message); 127859093Seric } 12799630Seric break; 128051920Seric 128140973Sbostic case 'C': /* controlling user */ 128254974Seric if (Verbose) 128358716Seric printf("\n\t\t\t\t (---%.34s---)", 128458716Seric &buf[1]); 128540973Sbostic break; 12869630Seric 12879630Seric case 'R': /* recipient name */ 128824979Seric if (Verbose) 128958716Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 129024979Seric else 129158716Seric printf("\n\t\t\t\t %.45s", &buf[1]); 12929630Seric break; 12939630Seric 12949630Seric case 'T': /* creation time */ 129524941Seric submittime = atol(&buf[1]); 12969630Seric break; 129710070Seric 129810070Seric case 'D': /* data file name */ 129910070Seric if (stat(&buf[1], &st) >= 0) 130010070Seric dfsize = st.st_size; 130110070Seric break; 130258737Seric 130358737Seric case 'F': /* flag bits */ 130458737Seric for (p = &buf[1]; *p != '\0'; p++) 130558737Seric { 130658737Seric switch (*p) 130758737Seric { 130858737Seric case 'w': 130958737Seric flags |= EF_WARNING; 131058737Seric break; 131158737Seric } 131258737Seric } 13139630Seric } 13149630Seric } 131510070Seric if (submittime == (time_t) 0) 131610070Seric printf(" (no control file)"); 13179630Seric printf("\n"); 131823098Seric (void) fclose(f); 13199630Seric } 13209630Seric } 13219630Seric 132256795Seric # endif /* QUEUE */ 132317468Seric /* 132417468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 132517468Seric ** 132617468Seric ** Assigns an id code if one does not already exist. 132717468Seric ** This code is very careful to avoid trashing existing files 132817468Seric ** under any circumstances. 132917468Seric ** 133017468Seric ** Parameters: 133117468Seric ** e -- envelope to build it in/from. 133217468Seric ** type -- the file type, used as the first character 133317468Seric ** of the file name. 133417468Seric ** 133517468Seric ** Returns: 133617468Seric ** a pointer to the new file name (in a static buffer). 133717468Seric ** 133817468Seric ** Side Effects: 133951920Seric ** If no id code is already assigned, queuename will 134051920Seric ** assign an id code, create a qf file, and leave a 134151920Seric ** locked, open-for-write file pointer in the envelope. 134217468Seric */ 134317468Seric 134417468Seric char * 134517468Seric queuename(e, type) 134617468Seric register ENVELOPE *e; 134759700Seric int type; 134817468Seric { 134917468Seric static int pid = -1; 135058741Seric static char c0; 135158741Seric static char c1; 135258741Seric static char c2; 135358716Seric time_t now; 135458716Seric struct tm *tm; 135558689Seric static char buf[MAXNAME]; 135617468Seric 135717468Seric if (e->e_id == NULL) 135817468Seric { 135917468Seric char qf[20]; 136017468Seric 136117468Seric /* find a unique id */ 136217468Seric if (pid != getpid()) 136317468Seric { 136417468Seric /* new process -- start back at "AA" */ 136517468Seric pid = getpid(); 136658716Seric now = curtime(); 136758716Seric tm = localtime(&now); 136858716Seric c0 = 'A' + tm->tm_hour; 136917468Seric c1 = 'A'; 137017468Seric c2 = 'A' - 1; 137117468Seric } 137258716Seric (void) sprintf(qf, "qf%cAA%05d", c0, pid); 137317468Seric 137417468Seric while (c1 < '~' || c2 < 'Z') 137517468Seric { 137617468Seric int i; 137717468Seric 137817468Seric if (c2 >= 'Z') 137917468Seric { 138017468Seric c1++; 138117468Seric c2 = 'A' - 1; 138217468Seric } 138358716Seric qf[3] = c1; 138458716Seric qf[4] = ++c2; 138517468Seric if (tTd(7, 20)) 138640934Srick printf("queuename: trying \"%s\"\n", qf); 138717468Seric 138840934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 138951920Seric if (i < 0) 139051920Seric { 139151920Seric if (errno == EEXIST) 139251920Seric continue; 139364705Seric syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 139464705Seric qf, QueueDir, geteuid()); 139551920Seric exit(EX_UNAVAILABLE); 139651920Seric } 139764335Seric if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB)) 139851920Seric { 139951920Seric e->e_lockfp = fdopen(i, "w"); 140040934Srick break; 140117468Seric } 140251920Seric 140351920Seric /* a reader got the file; abandon it and try again */ 140451920Seric (void) close(i); 140517468Seric } 140617468Seric if (c1 >= '~' && c2 >= 'Z') 140717468Seric { 140864705Seric syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 140964705Seric qf, QueueDir, geteuid()); 141017468Seric exit(EX_OSERR); 141117468Seric } 141217468Seric e->e_id = newstr(&qf[2]); 141317468Seric define('i', e->e_id, e); 141417468Seric if (tTd(7, 1)) 141517468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 141664745Seric if (tTd(7, 9)) 141764745Seric { 141864745Seric printf(" lockfd="); 141964745Seric dumpfd(fileno(e->e_lockfp), TRUE, FALSE); 142064745Seric } 142117468Seric # ifdef LOG 142258020Seric if (LogLevel > 93) 142317468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 142456795Seric # endif /* LOG */ 142517468Seric } 142617468Seric 142717468Seric if (type == '\0') 142817468Seric return (NULL); 142917468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 143017468Seric if (tTd(7, 2)) 143117468Seric printf("queuename: %s\n", buf); 143217468Seric return (buf); 143317468Seric } 143417468Seric /* 143517468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 143617468Seric ** 143717468Seric ** Parameters: 143817468Seric ** e -- the envelope to unlock. 143917468Seric ** 144017468Seric ** Returns: 144117468Seric ** none 144217468Seric ** 144317468Seric ** Side Effects: 144417468Seric ** unlocks the queue for `e'. 144517468Seric */ 144617468Seric 144717468Seric unlockqueue(e) 144817468Seric ENVELOPE *e; 144917468Seric { 145058680Seric if (tTd(51, 4)) 145158680Seric printf("unlockqueue(%s)\n", e->e_id); 145258680Seric 145351920Seric /* if there is a lock file in the envelope, close it */ 145451920Seric if (e->e_lockfp != NULL) 145558680Seric xfclose(e->e_lockfp, "unlockqueue", e->e_id); 145651920Seric e->e_lockfp = NULL; 145751920Seric 145858728Seric /* don't create a queue id if we don't already have one */ 145958728Seric if (e->e_id == NULL) 146058728Seric return; 146158728Seric 146217468Seric /* remove the transcript */ 146317468Seric # ifdef LOG 146458020Seric if (LogLevel > 87) 146517468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 146656795Seric # endif /* LOG */ 146758680Seric if (!tTd(51, 104)) 146817468Seric xunlink(queuename(e, 'x')); 146917468Seric 147017468Seric } 147140973Sbostic /* 147254974Seric ** SETCTLUSER -- create a controlling address 147340973Sbostic ** 147454974Seric ** Create a fake "address" given only a local login name; this is 147554974Seric ** used as a "controlling user" for future recipient addresses. 147640973Sbostic ** 147740973Sbostic ** Parameters: 147854974Seric ** user -- the user name of the controlling user. 147940973Sbostic ** 148040973Sbostic ** Returns: 148154974Seric ** An address descriptor for the controlling user. 148240973Sbostic ** 148340973Sbostic ** Side Effects: 148440973Sbostic ** none. 148540973Sbostic */ 148640973Sbostic 148754974Seric ADDRESS * 148854974Seric setctluser(user) 148954974Seric char *user; 149040973Sbostic { 149154974Seric register ADDRESS *a; 149240973Sbostic struct passwd *pw; 149359113Seric char *p; 149440973Sbostic 149540973Sbostic /* 149654974Seric ** See if this clears our concept of controlling user. 149740973Sbostic */ 149840973Sbostic 149963850Seric if (user == NULL || *user == '\0') 150063850Seric return NULL; 150140973Sbostic 150240973Sbostic /* 150354974Seric ** Set up addr fields for controlling user. 150440973Sbostic */ 150540973Sbostic 150654974Seric a = (ADDRESS *) xalloc(sizeof *a); 150754974Seric bzero((char *) a, sizeof *a); 150859113Seric 150959113Seric p = strchr(user, ':'); 151059113Seric if (p != NULL) 151159113Seric *p++ = '\0'; 151259270Seric if (*user != '\0' && (pw = getpwnam(user)) != NULL) 151340973Sbostic { 151440973Sbostic a->q_home = newstr(pw->pw_dir); 151540973Sbostic a->q_uid = pw->pw_uid; 151640973Sbostic a->q_gid = pw->pw_gid; 151757642Seric a->q_user = newstr(user); 151859270Seric a->q_flags |= QGOODUID; 151940973Sbostic } 152040973Sbostic else 152140973Sbostic { 152257642Seric a->q_user = newstr(DefUser); 152340973Sbostic } 152440973Sbostic 152559270Seric a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 152656328Seric a->q_mailer = LocalMailer; 152759113Seric if (p == NULL) 152859113Seric a->q_paddr = a->q_user; 152959113Seric else 153059113Seric a->q_paddr = newstr(p); 153154974Seric return a; 153240973Sbostic } 1533