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*64724Seric static char sccsid[] = "@(#)queue.c 8.21 (Berkeley) 10/16/93 (with queueing)"; 1433731Sbostic #else 15*64724Seric static char sccsid[] = "@(#)queue.c 8.21 (Berkeley) 10/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 7751920Seric newid = (e->e_id == NULL); 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) */ 8651920Seric if (newid) 8751835Seric { 8851920Seric tfp = e->e_lockfp; 8951920Seric } 9051920Seric else 9151920Seric { 9251920Seric /* get a locked tf file */ 9364070Seric for (i = 0; i < 128; i++) 9451835Seric { 9551920Seric fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode); 9651920Seric if (fd < 0) 9751835Seric { 9864070Seric if (errno != EEXIST) 9964070Seric break; 10064070Seric #ifdef LOG 10164070Seric if (LogLevel > 0 && (i % 32) == 0) 10264070Seric syslog(LOG_ALERT, "queueup: cannot create %s: %s", 10364070Seric tf, errstring(errno)); 10464070Seric #endif 10564070Seric continue; 10641636Srick } 10758689Seric 10864335Seric if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB)) 10951920Seric break; 11064070Seric #ifdef LOG 11164070Seric else if (LogLevel > 0 && (i % 32) == 0) 11264070Seric syslog(LOG_ALERT, "queueup: cannot lock %s: %s", 11364070Seric tf, errstring(errno)); 11464070Seric #endif 11558689Seric 11651920Seric close(fd); 11764070Seric 11864070Seric if ((i % 32) == 31) 11964070Seric { 12064070Seric /* save the old temp file away */ 12164070Seric (void) rename(tf, queuename(e, 'T')); 12264070Seric } 12364070Seric else 12464070Seric sleep(i % 32); 12541636Srick } 126*64724Seric if (fd < 0 || (tfp = fdopen(fd, "w")) == NULL) 127*64724Seric syserr("!queueup: cannot create queue temp file %s", tf); 12851920Seric } 1294632Seric 1307677Seric if (tTd(40, 1)) 13164307Seric printf("\n>>>>> queueing %s >>>>>\n", e->e_id); 1324632Seric 1334632Seric /* 1346980Seric ** If there is no data file yet, create one. 1356980Seric */ 1366980Seric 1376980Seric if (e->e_df == NULL) 1386980Seric { 1396980Seric register FILE *dfp; 1409389Seric extern putbody(); 1416980Seric 14264086Seric e->e_df = queuename(e, 'd'); 14364086Seric e->e_df = newstr(e->e_df); 14440934Srick fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode); 145*64724Seric if (fd < 0 || (dfp = fdopen(fd, "w")) == NULL) 146*64724Seric syserr("!queueup: cannot create data temp file %s", 147*64724Seric e->e_df); 14859730Seric (*e->e_putbody)(dfp, FileMailer, e, NULL); 14958680Seric (void) xfclose(dfp, "queueup dfp", e->e_id); 1509389Seric e->e_putbody = putbody; 1516980Seric } 1526980Seric 1536980Seric /* 1544632Seric ** Output future work requests. 15525687Seric ** Priority and creation time should be first, since 15625687Seric ** they are required by orderq. 1574632Seric */ 1584632Seric 1599377Seric /* output message priority */ 1609377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 1619377Seric 1629630Seric /* output creation time */ 1639630Seric fprintf(tfp, "T%ld\n", e->e_ctime); 1649630Seric 16559093Seric /* output type and name of data file */ 16659093Seric if (e->e_bodytype != NULL) 16759093Seric fprintf(tfp, "B%s\n", e->e_bodytype); 1687812Seric fprintf(tfp, "D%s\n", e->e_df); 1694632Seric 17010108Seric /* message from envelope, if it exists */ 17110108Seric if (e->e_message != NULL) 17210108Seric fprintf(tfp, "M%s\n", e->e_message); 17310108Seric 17458737Seric /* send various flag bits through */ 17558737Seric p = buf; 17658737Seric if (bitset(EF_WARNING, e->e_flags)) 17758737Seric *p++ = 'w'; 17858737Seric if (bitset(EF_RESPONSE, e->e_flags)) 17958737Seric *p++ = 'r'; 18058737Seric *p++ = '\0'; 18158737Seric if (buf[0] != '\0') 18258737Seric fprintf(tfp, "F%s\n", buf); 18358737Seric 18458957Seric /* $r and $s and $_ macro values */ 18553400Seric if ((p = macvalue('r', e)) != NULL) 18653400Seric fprintf(tfp, "$r%s\n", p); 18753400Seric if ((p = macvalue('s', e)) != NULL) 18853400Seric fprintf(tfp, "$s%s\n", p); 18958957Seric if ((p = macvalue('_', e)) != NULL) 19058957Seric fprintf(tfp, "$_%s\n", p); 19153400Seric 1924632Seric /* output name of sender */ 1937812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 1944632Seric 19555360Seric /* output list of error recipients */ 19659670Seric printctladdr(NULL, NULL); 19755360Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 19855360Seric { 19958680Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 20055360Seric { 20159113Seric printctladdr(q, tfp); 20255360Seric fprintf(tfp, "E%s\n", q->q_paddr); 20355360Seric } 20455360Seric } 20555360Seric 2064632Seric /* output list of recipient addresses */ 2076980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 2084632Seric { 20958250Seric if (bitset(QQUEUEUP, q->q_flags) || 21058680Seric (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags))) 2118245Seric { 21259113Seric printctladdr(q, tfp); 2137812Seric fprintf(tfp, "R%s\n", q->q_paddr); 2149377Seric if (announce) 2159377Seric { 2169377Seric e->e_to = q->q_paddr; 21758151Seric message("queued"); 21858020Seric if (LogLevel > 8) 21958337Seric logdelivery(NULL, NULL, "queued", e); 2209377Seric e->e_to = NULL; 2219377Seric } 2229387Seric if (tTd(40, 1)) 2239387Seric { 2249387Seric printf("queueing "); 2259387Seric printaddr(q, FALSE); 2269387Seric } 2278245Seric } 2284632Seric } 2294632Seric 2309377Seric /* 2319377Seric ** Output headers for this message. 2329377Seric ** Expand macros completely here. Queue run will deal with 2339377Seric ** everything as absolute headers. 2349377Seric ** All headers that must be relative to the recipient 2359377Seric ** can be cracked later. 23610173Seric ** We set up a "null mailer" -- i.e., a mailer that will have 23710173Seric ** no effect on the addresses as they are output. 2389377Seric */ 2399377Seric 24010686Seric bzero((char *) &nullmailer, sizeof nullmailer); 24158020Seric nullmailer.m_re_rwset = nullmailer.m_rh_rwset = 24258020Seric nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1; 24310349Seric nullmailer.m_eol = "\n"; 24410173Seric 24558050Seric define('g', "\201f", e); 2466980Seric for (h = e->e_header; h != NULL; h = h->h_link) 2474632Seric { 24810686Seric extern bool bitzerop(); 24910686Seric 25012015Seric /* don't output null headers */ 2514632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 2524632Seric continue; 25312015Seric 25412015Seric /* don't output resent headers on non-resent messages */ 25512015Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 25612015Seric continue; 25712015Seric 25812015Seric /* output this header */ 2597812Seric fprintf(tfp, "H"); 26012015Seric 26112015Seric /* if conditional, output the set of conditions */ 26210686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 26310686Seric { 26410686Seric int j; 26510686Seric 26623098Seric (void) putc('?', tfp); 26710686Seric for (j = '\0'; j <= '\177'; j++) 26810686Seric if (bitnset(j, h->h_mflags)) 26923098Seric (void) putc(j, tfp); 27023098Seric (void) putc('?', tfp); 27110686Seric } 27212015Seric 27312015Seric /* output the header: expand macros, convert addresses */ 2747763Seric if (bitset(H_DEFAULT, h->h_flags)) 2757763Seric { 2767763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 2778236Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 2787763Seric } 2798245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 2809348Seric { 28158737Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 28263753Seric FILE *savetrace = TrafficLogFile; 28358737Seric 28463753Seric TrafficLogFile = NULL; 28563753Seric 28658737Seric if (bitset(H_FROM, h->h_flags)) 28758737Seric oldstyle = FALSE; 28858737Seric 28958737Seric commaize(h, h->h_value, tfp, oldstyle, 29055012Seric &nullmailer, e); 29163753Seric 29263753Seric TrafficLogFile = savetrace; 2939348Seric } 2947763Seric else 2958245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 2964632Seric } 2974632Seric 2984632Seric /* 2994632Seric ** Clean up. 3004632Seric */ 3014632Seric 30258732Seric fflush(tfp); 30360603Seric fsync(fileno(tfp)); 30458732Seric if (ferror(tfp)) 30558732Seric { 30658732Seric if (newid) 30758732Seric syserr("!552 Error writing control file %s", tf); 30858732Seric else 30958732Seric syserr("!452 Error writing control file %s", tf); 31058732Seric } 31158732Seric 31251920Seric if (!newid) 31351920Seric { 31464277Seric /* rename (locked) tf to be (locked) qf */ 31551920Seric qf = queuename(e, 'q'); 31651920Seric if (rename(tf, qf) < 0) 31751920Seric syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df); 31864277Seric 31964277Seric /* close and unlock old (locked) qf */ 32051920Seric if (e->e_lockfp != NULL) 32158680Seric (void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id); 32251920Seric e->e_lockfp = tfp; 32351920Seric } 32451920Seric else 32551920Seric qf = tf; 32641636Srick errno = 0; 3277391Seric 3287677Seric # ifdef LOG 3297677Seric /* save log info */ 33058020Seric if (LogLevel > 79) 3317878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 33256795Seric # endif /* LOG */ 33364307Seric 33464307Seric if (tTd(40, 1)) 33564307Seric printf("<<<<< done queueing %s <<<<<\n\n", e->e_id); 33651920Seric return; 3374632Seric } 33854974Seric 33954974Seric printctladdr(a, tfp) 34059113Seric register ADDRESS *a; 34154974Seric FILE *tfp; 34254974Seric { 34359113Seric char *uname; 34459113Seric register struct passwd *pw; 34559113Seric register ADDRESS *q; 34659113Seric uid_t uid; 34759113Seric static ADDRESS *lastctladdr; 34859113Seric static uid_t lastuid; 34954974Seric 35059113Seric /* initialization */ 35163850Seric if (a == NULL || a->q_alias == NULL || tfp == NULL) 35254974Seric { 35359670Seric if (lastctladdr != NULL && tfp != NULL) 35459113Seric fprintf(tfp, "C\n"); 35559113Seric lastctladdr = NULL; 35659113Seric lastuid = 0; 35754974Seric return; 35854974Seric } 35959113Seric 36059113Seric /* find the active uid */ 36159113Seric q = getctladdr(a); 36259113Seric if (q == NULL) 36359113Seric uid = 0; 36454974Seric else 36559113Seric uid = q->q_uid; 36663850Seric a = a->q_alias; 36759113Seric 36859113Seric /* check to see if this is the same as last time */ 36959113Seric if (lastctladdr != NULL && uid == lastuid && 37059113Seric strcmp(lastctladdr->q_paddr, a->q_paddr) == 0) 37159113Seric return; 37259113Seric lastuid = uid; 37359113Seric lastctladdr = a; 37459113Seric 37559113Seric if (uid == 0 || (pw = getpwuid(uid)) == NULL) 37659270Seric uname = ""; 37759113Seric else 37859113Seric uname = pw->pw_name; 37959113Seric 38059113Seric fprintf(tfp, "C%s:%s\n", uname, a->q_paddr); 38154974Seric } 38254974Seric 3834632Seric /* 3844632Seric ** RUNQUEUE -- run the jobs in the queue. 3854632Seric ** 3864632Seric ** Gets the stuff out of the queue in some presumably logical 3874632Seric ** order and processes them. 3884632Seric ** 3894632Seric ** Parameters: 39024941Seric ** forkflag -- TRUE if the queue scanning should be done in 39124941Seric ** a child process. We double-fork so it is not our 39224941Seric ** child and we don't have to clean up after it. 3934632Seric ** 3944632Seric ** Returns: 3954632Seric ** none. 3964632Seric ** 3974632Seric ** Side Effects: 3984632Seric ** runs things in the mail queue. 3994632Seric */ 4004632Seric 40155360Seric ENVELOPE QueueEnvelope; /* the queue run envelope */ 40255360Seric 40355360Seric runqueue(forkflag) 4044639Seric bool forkflag; 4054632Seric { 40655360Seric register ENVELOPE *e; 40755360Seric extern ENVELOPE BlankEnvelope; 40824953Seric 4097466Seric /* 41024953Seric ** If no work will ever be selected, don't even bother reading 41124953Seric ** the queue. 41224953Seric */ 41324953Seric 41451920Seric CurrentLA = getla(); /* get load average */ 41540934Srick 41658132Seric if (shouldqueue(0L, curtime())) 41724953Seric { 41824953Seric if (Verbose) 41924953Seric printf("Skipping queue run -- load average too high\n"); 42058107Seric if (forkflag && QueueIntvl != 0) 42158107Seric (void) setevent(QueueIntvl, runqueue, TRUE); 42255360Seric return; 42324953Seric } 42424953Seric 42524953Seric /* 4267466Seric ** See if we want to go off and do other useful work. 4277466Seric */ 4284639Seric 4294639Seric if (forkflag) 4304639Seric { 4317943Seric int pid; 4327943Seric 4337943Seric pid = dofork(); 4347943Seric if (pid != 0) 4354639Seric { 43646928Sbostic extern void reapchild(); 43725184Seric 4387943Seric /* parent -- pick up intermediate zombie */ 43925184Seric #ifndef SIGCHLD 4409377Seric (void) waitfor(pid); 44156795Seric #else /* SIGCHLD */ 44264035Seric (void) setsignal(SIGCHLD, reapchild); 44356795Seric #endif /* SIGCHLD */ 4447690Seric if (QueueIntvl != 0) 4459348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 4464639Seric return; 4474639Seric } 4487943Seric /* child -- double fork */ 44925184Seric #ifndef SIGCHLD 4507943Seric if (fork() != 0) 4517943Seric exit(EX_OK); 45256795Seric #else /* SIGCHLD */ 45364035Seric (void) setsignal(SIGCHLD, SIG_DFL); 45456795Seric #endif /* SIGCHLD */ 4554639Seric } 45624941Seric 45740934Srick setproctitle("running queue: %s", QueueDir); 45824941Seric 4597876Seric # ifdef LOG 46058020Seric if (LogLevel > 69) 46155360Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 46255360Seric QueueDir, getpid(), forkflag); 46356795Seric # endif /* LOG */ 4644639Seric 4657466Seric /* 46610205Seric ** Release any resources used by the daemon code. 46710205Seric */ 46810205Seric 46910205Seric # ifdef DAEMON 47010205Seric clrdaemon(); 47156795Seric # endif /* DAEMON */ 47210205Seric 47364658Seric /* force it to run expensive jobs */ 47464658Seric NoConnect = FALSE; 47564658Seric 47610205Seric /* 47755360Seric ** Create ourselves an envelope 47855360Seric */ 47955360Seric 48055360Seric CurEnv = &QueueEnvelope; 48158179Seric e = newenvelope(&QueueEnvelope, CurEnv); 48255360Seric e->e_flags = BlankEnvelope.e_flags; 48355360Seric 48455360Seric /* 48527175Seric ** Make sure the alias database is open. 48627175Seric */ 48727175Seric 48860537Seric initmaps(FALSE, e); 48927175Seric 49027175Seric /* 4917466Seric ** Start making passes through the queue. 4927466Seric ** First, read and sort the entire queue. 4937466Seric ** Then, process the work in that order. 4947466Seric ** But if you take too long, start over. 4957466Seric */ 4967466Seric 4977943Seric /* order the existing work requests */ 49824954Seric (void) orderq(FALSE); 4997690Seric 5007943Seric /* process them once at a time */ 5017943Seric while (WorkQ != NULL) 5024639Seric { 5037943Seric WORK *w = WorkQ; 5047881Seric 5057943Seric WorkQ = WorkQ->w_next; 50658884Seric 50758884Seric /* 50858884Seric ** Ignore jobs that are too expensive for the moment. 50958884Seric */ 51058884Seric 51158884Seric if (shouldqueue(w->w_pri, w->w_ctime)) 51258884Seric { 51358884Seric if (Verbose) 51458884Seric printf("\nSkipping %s\n", w->w_name + 2); 51558884Seric } 51658930Seric else 51758930Seric { 51864296Seric pid_t pid; 51964296Seric extern pid_t dowork(); 52064296Seric 52164296Seric pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e); 52264296Seric errno = 0; 52364296Seric (void) waitfor(pid); 52458930Seric } 5257943Seric free(w->w_name); 5267943Seric free((char *) w); 5274639Seric } 52829866Seric 52929866Seric /* exit without the usual cleanup */ 53055467Seric e->e_id = NULL; 53155467Seric finis(); 5324634Seric } 5334634Seric /* 5344632Seric ** ORDERQ -- order the work queue. 5354632Seric ** 5364632Seric ** Parameters: 53724941Seric ** doall -- if set, include everything in the queue (even 53824941Seric ** the jobs that cannot be run because the load 53924941Seric ** average is too high). Otherwise, exclude those 54024941Seric ** jobs. 5414632Seric ** 5424632Seric ** Returns: 54310121Seric ** The number of request in the queue (not necessarily 54410121Seric ** the number of requests in WorkQ however). 5454632Seric ** 5464632Seric ** Side Effects: 5474632Seric ** Sets WorkQ to the queue of available work, in order. 5484632Seric */ 5494632Seric 55025687Seric # define NEED_P 001 55125687Seric # define NEED_T 002 55258318Seric # define NEED_R 004 55358318Seric # define NEED_S 010 5544632Seric 55524941Seric orderq(doall) 55624941Seric bool doall; 5574632Seric { 55860219Seric register struct dirent *d; 5594632Seric register WORK *w; 5606625Sglickman DIR *f; 5614632Seric register int i; 56225687Seric WORK wlist[QUEUESIZE+1]; 56310070Seric int wn = -1; 5644632Seric extern workcmpf(); 5654632Seric 56658318Seric if (tTd(41, 1)) 56758318Seric { 56858318Seric printf("orderq:\n"); 56958318Seric if (QueueLimitId != NULL) 57058318Seric printf("\tQueueLimitId = %s\n", QueueLimitId); 57158318Seric if (QueueLimitSender != NULL) 57258318Seric printf("\tQueueLimitSender = %s\n", QueueLimitSender); 57358318Seric if (QueueLimitRecipient != NULL) 57458318Seric printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 57558318Seric } 57658318Seric 5774632Seric /* clear out old WorkQ */ 5784632Seric for (w = WorkQ; w != NULL; ) 5794632Seric { 5804632Seric register WORK *nw = w->w_next; 5814632Seric 5824632Seric WorkQ = nw; 5834632Seric free(w->w_name); 5844632Seric free((char *) w); 5854632Seric w = nw; 5864632Seric } 5874632Seric 5884632Seric /* open the queue directory */ 5898148Seric f = opendir("."); 5904632Seric if (f == NULL) 5914632Seric { 5928148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 59310070Seric return (0); 5944632Seric } 5954632Seric 5964632Seric /* 5974632Seric ** Read the work directory. 5984632Seric */ 5994632Seric 60010070Seric while ((d = readdir(f)) != NULL) 6014632Seric { 6029377Seric FILE *cf; 60364492Seric register char *p; 6044632Seric char lbuf[MAXNAME]; 60558318Seric extern bool strcontainedin(); 6064632Seric 6074632Seric /* is this an interesting entry? */ 6087812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 6094632Seric continue; 6104632Seric 61158318Seric if (QueueLimitId != NULL && 61258318Seric !strcontainedin(QueueLimitId, d->d_name)) 61358318Seric continue; 61458318Seric 61558722Seric /* 61658722Seric ** Check queue name for plausibility. This handles 61758722Seric ** both old and new type ids. 61858722Seric */ 61958722Seric 62064492Seric p = d->d_name + 2; 62164492Seric if (isupper(p[0]) && isupper(p[2])) 62264492Seric p += 3; 62364492Seric else if (isupper(p[1])) 62464492Seric p += 2; 62564492Seric else 62664492Seric p = d->d_name; 62764492Seric for (i = 0; isdigit(*p); p++) 62864492Seric i++; 62964492Seric if (i < 5 || *p != '\0') 63058020Seric { 63158020Seric if (Verbose) 63258020Seric printf("orderq: bogus qf name %s\n", d->d_name); 63358020Seric #ifdef LOG 63458020Seric if (LogLevel > 3) 63559615Seric syslog(LOG_CRIT, "orderq: bogus qf name %s", 63658020Seric d->d_name); 63758020Seric #endif 63858020Seric if (strlen(d->d_name) >= MAXNAME) 63958020Seric d->d_name[MAXNAME - 1] = '\0'; 64058020Seric strcpy(lbuf, d->d_name); 64158020Seric lbuf[0] = 'Q'; 64258020Seric (void) rename(d->d_name, lbuf); 64358020Seric continue; 64458020Seric } 64558020Seric 64610070Seric /* yes -- open control file (if not too many files) */ 64725687Seric if (++wn >= QUEUESIZE) 64810070Seric continue; 64958318Seric 6508148Seric cf = fopen(d->d_name, "r"); 6514632Seric if (cf == NULL) 6524632Seric { 6537055Seric /* this may be some random person sending hir msgs */ 6547055Seric /* syserr("orderq: cannot open %s", cbuf); */ 65510090Seric if (tTd(41, 2)) 65610090Seric printf("orderq: cannot open %s (%d)\n", 65710090Seric d->d_name, errno); 6587055Seric errno = 0; 65910090Seric wn--; 6604632Seric continue; 6614632Seric } 66225687Seric w = &wlist[wn]; 66325687Seric w->w_name = newstr(d->d_name); 6644632Seric 66525027Seric /* make sure jobs in creation don't clog queue */ 66625687Seric w->w_pri = 0x7fffffff; 66725687Seric w->w_ctime = 0; 66825027Seric 6694632Seric /* extract useful information */ 67025687Seric i = NEED_P | NEED_T; 67158318Seric if (QueueLimitSender != NULL) 67258318Seric i |= NEED_S; 67358318Seric if (QueueLimitRecipient != NULL) 67458318Seric i |= NEED_R; 67525687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 6764632Seric { 67724954Seric extern long atol(); 67858318Seric extern bool strcontainedin(); 67924954Seric 68024941Seric switch (lbuf[0]) 6814632Seric { 68224941Seric case 'P': 68325687Seric w->w_pri = atol(&lbuf[1]); 68425687Seric i &= ~NEED_P; 6854632Seric break; 68625013Seric 68725013Seric case 'T': 68825687Seric w->w_ctime = atol(&lbuf[1]); 68925687Seric i &= ~NEED_T; 69025013Seric break; 69158318Seric 69258318Seric case 'R': 69358318Seric if (QueueLimitRecipient != NULL && 69458318Seric strcontainedin(QueueLimitRecipient, &lbuf[1])) 69558318Seric i &= ~NEED_R; 69658318Seric break; 69758318Seric 69858318Seric case 'S': 69958318Seric if (QueueLimitSender != NULL && 70058318Seric strcontainedin(QueueLimitSender, &lbuf[1])) 70158318Seric i &= ~NEED_S; 70258318Seric break; 7034632Seric } 7044632Seric } 7054632Seric (void) fclose(cf); 70624953Seric 70758318Seric if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 70858318Seric bitset(NEED_R|NEED_S, i)) 70924953Seric { 71024953Seric /* don't even bother sorting this job in */ 71124953Seric wn--; 71224953Seric } 7134632Seric } 7146625Sglickman (void) closedir(f); 71510090Seric wn++; 7164632Seric 7174632Seric /* 7184632Seric ** Sort the work directory. 7194632Seric */ 7204632Seric 72125687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 7224632Seric 7234632Seric /* 7244632Seric ** Convert the work list into canonical form. 7259377Seric ** Should be turning it into a list of envelopes here perhaps. 7264632Seric */ 7274632Seric 72824981Seric WorkQ = NULL; 72925687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 7304632Seric { 7314632Seric w = (WORK *) xalloc(sizeof *w); 7324632Seric w->w_name = wlist[i].w_name; 7334632Seric w->w_pri = wlist[i].w_pri; 73425013Seric w->w_ctime = wlist[i].w_ctime; 73524981Seric w->w_next = WorkQ; 73624981Seric WorkQ = w; 7374632Seric } 7384632Seric 7397677Seric if (tTd(40, 1)) 7404632Seric { 7414632Seric for (w = WorkQ; w != NULL; w = w->w_next) 7425037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 7434632Seric } 74410070Seric 74510090Seric return (wn); 7464632Seric } 7474632Seric /* 7487677Seric ** WORKCMPF -- compare function for ordering work. 7494632Seric ** 7504632Seric ** Parameters: 7514632Seric ** a -- the first argument. 7524632Seric ** b -- the second argument. 7534632Seric ** 7544632Seric ** Returns: 75524981Seric ** -1 if a < b 75624981Seric ** 0 if a == b 75724981Seric ** +1 if a > b 7584632Seric ** 7594632Seric ** Side Effects: 7604632Seric ** none. 7614632Seric */ 7624632Seric 7634632Seric workcmpf(a, b) 7645037Seric register WORK *a; 7655037Seric register WORK *b; 7664632Seric { 76757438Seric long pa = a->w_pri; 76857438Seric long pb = b->w_pri; 76924941Seric 77024941Seric if (pa == pb) 7714632Seric return (0); 77224941Seric else if (pa > pb) 77324981Seric return (1); 77424981Seric else 77510121Seric return (-1); 7764632Seric } 7774632Seric /* 7784632Seric ** DOWORK -- do a work request. 7794632Seric ** 7804632Seric ** Parameters: 78158884Seric ** id -- the ID of the job to run. 78258884Seric ** forkflag -- if set, run this in background. 78358924Seric ** requeueflag -- if set, reinstantiate the queue quickly. 78458924Seric ** This is used when expanding aliases in the queue. 78561094Seric ** If forkflag is also set, it doesn't wait for the 78661094Seric ** child. 78758884Seric ** e - the envelope in which to run it. 7884632Seric ** 7894632Seric ** Returns: 79064296Seric ** process id of process that is running the queue job. 7914632Seric ** 7924632Seric ** Side Effects: 7934632Seric ** The work request is satisfied if possible. 7944632Seric */ 7954632Seric 79664296Seric pid_t 79758924Seric dowork(id, forkflag, requeueflag, e) 79858884Seric char *id; 79958884Seric bool forkflag; 80058924Seric bool requeueflag; 80155012Seric register ENVELOPE *e; 8024632Seric { 80364296Seric register pid_t pid; 80451920Seric extern bool readqf(); 8054632Seric 8067677Seric if (tTd(40, 1)) 80758884Seric printf("dowork(%s)\n", id); 8084632Seric 8094632Seric /* 81024941Seric ** Fork for work. 81124941Seric */ 81224941Seric 81358884Seric if (forkflag) 81424941Seric { 81564296Seric pid = fork(); 81664296Seric if (pid < 0) 81724941Seric { 81824941Seric syserr("dowork: cannot fork"); 81964296Seric return 0; 82024941Seric } 82124941Seric } 82224941Seric else 82324941Seric { 82464296Seric pid = 0; 82524941Seric } 82624941Seric 82764296Seric if (pid == 0) 8284632Seric { 8294632Seric /* 8304632Seric ** CHILD 8318148Seric ** Lock the control file to avoid duplicate deliveries. 8328148Seric ** Then run the file as though we had just read it. 8337350Seric ** We save an idea of the temporary name so we 8347350Seric ** can recover on interrupt. 8354632Seric */ 8364632Seric 8377763Seric /* set basic modes, etc. */ 8387356Seric (void) alarm(0); 83955012Seric clearenvelope(e, FALSE); 84064554Seric e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS; 84158734Seric e->e_errormode = EM_MAIL; 84258884Seric e->e_id = id; 84363846Seric if (forkflag) 84464554Seric { 84564296Seric disconnect(1, e); 84664554Seric OpMode = MD_DELIVER; 84764554Seric } 8487876Seric # ifdef LOG 84958020Seric if (LogLevel > 76) 85055012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 8517881Seric getpid()); 85256795Seric # endif /* LOG */ 8537763Seric 8547763Seric /* don't use the headers from sendmail.cf... */ 85555012Seric e->e_header = NULL; 8567763Seric 85751920Seric /* read the queue control file -- return if locked */ 85863850Seric if (!readqf(e, !requeueflag)) 8596980Seric { 86058884Seric if (tTd(40, 4)) 86158884Seric printf("readqf(%s) failed\n", e->e_id); 86258884Seric if (forkflag) 86324941Seric exit(EX_OK); 86424941Seric else 86524941Seric return; 8666980Seric } 8676980Seric 86855012Seric e->e_flags |= EF_INQUEUE; 86958929Seric eatheader(e, requeueflag); 8706980Seric 87158924Seric if (requeueflag) 87258924Seric queueup(e, TRUE, FALSE); 87358924Seric 8746980Seric /* do the delivery */ 87558915Seric sendall(e, SM_DELIVER); 8766980Seric 8776980Seric /* finish up and exit */ 87858884Seric if (forkflag) 87924941Seric finis(); 88024941Seric else 88155012Seric dropenvelope(e); 8824632Seric } 88364296Seric e->e_id = NULL; 88464296Seric return pid; 8854632Seric } 8864632Seric /* 8874632Seric ** READQF -- read queue file and set up environment. 8884632Seric ** 8894632Seric ** Parameters: 8909377Seric ** e -- the envelope of the job to run. 89163850Seric ** announcefile -- if set, announce the name of the queue 89263850Seric ** file in error messages. 8934632Seric ** 8944632Seric ** Returns: 89551920Seric ** TRUE if it successfully read the queue file. 89651920Seric ** FALSE otherwise. 8974632Seric ** 8984632Seric ** Side Effects: 89951920Seric ** The queue file is returned locked. 9004632Seric */ 9014632Seric 90251920Seric bool 90363850Seric readqf(e, announcefile) 9049377Seric register ENVELOPE *e; 90563850Seric bool announcefile; 9064632Seric { 90717477Seric register FILE *qfp; 90854974Seric ADDRESS *ctladdr; 90956400Seric struct stat st; 91057135Seric char *bp; 91158915Seric char qf[20]; 91257135Seric char buf[MAXLINE]; 91324954Seric extern long atol(); 91454974Seric extern ADDRESS *setctluser(); 9154632Seric 9164632Seric /* 91717468Seric ** Read and process the file. 9184632Seric */ 9194632Seric 92058915Seric strcpy(qf, queuename(e, 'q')); 92151937Seric qfp = fopen(qf, "r+"); 92217477Seric if (qfp == NULL) 92317477Seric { 92458884Seric if (tTd(40, 8)) 92558884Seric printf("readqf(%s): fopen failure (%s)\n", 92658884Seric qf, errstring(errno)); 92740934Srick if (errno != ENOENT) 92840934Srick syserr("readqf: no control file %s", qf); 92951920Seric return FALSE; 93017477Seric } 93140934Srick 93264335Seric if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB)) 93364277Seric { 93464277Seric /* being processed by another queuer */ 93564277Seric if (tTd(40, 8)) 93664277Seric printf("readqf(%s): locked\n", qf); 93764277Seric if (Verbose) 93864277Seric printf("%s: locked\n", e->e_id); 93964277Seric # ifdef LOG 94064277Seric if (LogLevel > 19) 94164277Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 94264277Seric # endif /* LOG */ 94364277Seric (void) fclose(qfp); 94464277Seric return FALSE; 94564277Seric } 94664277Seric 94756400Seric /* 94856400Seric ** Check the queue file for plausibility to avoid attacks. 94956400Seric */ 95056400Seric 95156400Seric if (fstat(fileno(qfp), &st) < 0) 95256400Seric { 95356400Seric /* must have been being processed by someone else */ 95458884Seric if (tTd(40, 8)) 95558884Seric printf("readqf(%s): fstat failure (%s)\n", 95658884Seric qf, errstring(errno)); 95756400Seric fclose(qfp); 95856400Seric return FALSE; 95956400Seric } 96056400Seric 96164137Seric if (st.st_uid != geteuid()) 96256400Seric { 96356400Seric # ifdef LOG 96456400Seric if (LogLevel > 0) 96556400Seric { 96656400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 96756400Seric e->e_id, st.st_uid, st.st_mode); 96856400Seric } 96956795Seric # endif /* LOG */ 97058884Seric if (tTd(40, 8)) 97158884Seric printf("readqf(%s): bogus file\n", qf); 97264277Seric rename(qf, queuename(e, 'Q')); 97356400Seric fclose(qfp); 97456400Seric return FALSE; 97556400Seric } 97656400Seric 97764277Seric if (st.st_size == 0) 97840934Srick { 97964277Seric /* must be a bogus file -- just remove it */ 98064277Seric (void) unlink(qf); 98164277Seric fclose(qfp); 98251920Seric return FALSE; 98340934Srick } 98440934Srick 98564277Seric if (st.st_nlink == 0) 98659101Seric { 98764277Seric /* 98864277Seric ** Race condition -- we got a file just as it was being 98964277Seric ** unlinked. Just assume it is zero length. 99064277Seric */ 99164277Seric 99259101Seric fclose(qfp); 99359101Seric return FALSE; 99459101Seric } 99559101Seric 99664277Seric /* good file -- save this lock */ 99751920Seric e->e_lockfp = qfp; 99851920Seric 99940934Srick /* do basic system initialization */ 100055012Seric initsys(e); 100140934Srick 100263850Seric if (announcefile) 100363850Seric FileName = qf; 10049377Seric LineNumber = 0; 100563850Seric e->e_flags |= EF_GLOBALERRS; 100663850Seric OpMode = MD_DELIVER; 100751920Seric if (Verbose) 10089377Seric printf("\nRunning %s\n", e->e_id); 100954974Seric ctladdr = NULL; 101057135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 10114632Seric { 101258737Seric register char *p; 101357529Seric struct stat st; 101457529Seric 101526504Seric if (tTd(40, 4)) 101657135Seric printf("+++++ %s\n", bp); 101757135Seric switch (bp[0]) 10184632Seric { 101940973Sbostic case 'C': /* specify controlling user */ 102057135Seric ctladdr = setctluser(&bp[1]); 102140973Sbostic break; 102240973Sbostic 10234632Seric case 'R': /* specify recipient */ 102458082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 10254632Seric break; 10264632Seric 102725687Seric case 'E': /* specify error recipient */ 102858082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 102925687Seric break; 103025687Seric 10314632Seric case 'H': /* header */ 103257135Seric (void) chompheader(&bp[1], FALSE, e); 10334632Seric break; 10344632Seric 103510108Seric case 'M': /* message */ 103664705Seric /* ignore this; we want a new message next time */ 103710108Seric break; 103810108Seric 10394632Seric case 'S': /* sender */ 104058704Seric setsender(newstr(&bp[1]), e, NULL, TRUE); 10414632Seric break; 10424632Seric 104359093Seric case 'B': /* body type */ 104459093Seric e->e_bodytype = newstr(&bp[1]); 104559093Seric break; 104659093Seric 10474632Seric case 'D': /* data file name */ 104857135Seric e->e_df = newstr(&bp[1]); 10499544Seric e->e_dfp = fopen(e->e_df, "r"); 10509544Seric if (e->e_dfp == NULL) 105158020Seric { 10529377Seric syserr("readqf: cannot open %s", e->e_df); 105358020Seric e->e_msgsize = -1; 105458020Seric } 105558020Seric else if (fstat(fileno(e->e_dfp), &st) >= 0) 105657529Seric e->e_msgsize = st.st_size; 10574632Seric break; 10584632Seric 10597860Seric case 'T': /* init time */ 106057135Seric e->e_ctime = atol(&bp[1]); 10614632Seric break; 10624632Seric 10634634Seric case 'P': /* message priority */ 106457135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 10654634Seric break; 10664634Seric 106758737Seric case 'F': /* flag bits */ 106858737Seric for (p = &bp[1]; *p != '\0'; p++) 106958737Seric { 107058737Seric switch (*p) 107158737Seric { 107258737Seric case 'w': /* warning sent */ 107358737Seric e->e_flags |= EF_WARNING; 107458737Seric break; 107558737Seric 107658737Seric case 'r': /* response */ 107758737Seric e->e_flags |= EF_RESPONSE; 107858737Seric break; 107958737Seric } 108058737Seric } 108158737Seric break; 108258737Seric 108353400Seric case '$': /* define macro */ 108457135Seric define(bp[1], newstr(&bp[2]), e); 108553400Seric break; 108653400Seric 108724941Seric case '\0': /* blank line; ignore */ 108824941Seric break; 108924941Seric 10904632Seric default: 109159700Seric syserr("readqf: bad line \"%s\"", e->e_id, 109257135Seric LineNumber, bp); 109363753Seric fclose(qfp); 109463753Seric rename(qf, queuename(e, 'Q')); 109563753Seric return FALSE; 10964632Seric } 109757135Seric 109857135Seric if (bp != buf) 109957135Seric free(bp); 11004632Seric } 11019377Seric 11029377Seric FileName = NULL; 110324941Seric 110424941Seric /* 110524941Seric ** If we haven't read any lines, this queue file is empty. 110624941Seric ** Arrange to remove it without referencing any null pointers. 110724941Seric */ 110824941Seric 110924941Seric if (LineNumber == 0) 111024941Seric { 111124941Seric errno = 0; 111224941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 111324941Seric } 111451920Seric return TRUE; 11154632Seric } 11164632Seric /* 11179630Seric ** PRINTQUEUE -- print out a representation of the mail queue 11189630Seric ** 11199630Seric ** Parameters: 11209630Seric ** none. 11219630Seric ** 11229630Seric ** Returns: 11239630Seric ** none. 11249630Seric ** 11259630Seric ** Side Effects: 11269630Seric ** Prints a listing of the mail queue on the standard output. 11279630Seric */ 11285182Seric 11299630Seric printqueue() 11309630Seric { 11319630Seric register WORK *w; 11329630Seric FILE *f; 113310070Seric int nrequests; 11349630Seric char buf[MAXLINE]; 11359630Seric 11369630Seric /* 113758250Seric ** Check for permission to print the queue 113858250Seric */ 113958250Seric 114064333Seric if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0) 114158250Seric { 114258250Seric struct stat st; 114358523Seric # ifdef NGROUPS 114458523Seric int n; 114563937Seric GIDSET_T gidset[NGROUPS]; 114658523Seric # endif 114758250Seric 114858517Seric if (stat(QueueDir, &st) < 0) 114958250Seric { 115058250Seric syserr("Cannot stat %s", QueueDir); 115158250Seric return; 115258250Seric } 115358523Seric # ifdef NGROUPS 115458523Seric n = getgroups(NGROUPS, gidset); 115558523Seric while (--n >= 0) 115658523Seric { 115758523Seric if (gidset[n] == st.st_gid) 115858523Seric break; 115958523Seric } 116058523Seric if (n < 0) 116158523Seric # else 116263787Seric if (RealGid != st.st_gid) 116358523Seric # endif 116458250Seric { 116558250Seric usrerr("510 You are not permitted to see the queue"); 116658250Seric setstat(EX_NOPERM); 116758250Seric return; 116858250Seric } 116958250Seric } 117058250Seric 117158250Seric /* 11729630Seric ** Read and order the queue. 11739630Seric */ 11749630Seric 117524941Seric nrequests = orderq(TRUE); 11769630Seric 11779630Seric /* 11789630Seric ** Print the work list that we have read. 11799630Seric */ 11809630Seric 11819630Seric /* first see if there is anything */ 118210070Seric if (nrequests <= 0) 11839630Seric { 118410070Seric printf("Mail queue is empty\n"); 11859630Seric return; 11869630Seric } 11879630Seric 118851920Seric CurrentLA = getla(); /* get load average */ 118940934Srick 119010096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 119125687Seric if (nrequests > QUEUESIZE) 119225687Seric printf(", only %d printed", QUEUESIZE); 119324979Seric if (Verbose) 119458716Seric printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 119524979Seric else 119658716Seric printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 11979630Seric for (w = WorkQ; w != NULL; w = w->w_next) 11989630Seric { 11999630Seric struct stat st; 120010070Seric auto time_t submittime = 0; 120110070Seric long dfsize = -1; 120258737Seric int flags = 0; 120310108Seric char message[MAXLINE]; 120459093Seric char bodytype[MAXNAME]; 12059630Seric 120664355Seric printf("%8s", w->w_name + 2); 120717468Seric f = fopen(w->w_name, "r"); 120817468Seric if (f == NULL) 120917468Seric { 121064355Seric printf(" (job completed)\n"); 121117468Seric errno = 0; 121217468Seric continue; 121317468Seric } 121464335Seric if (!lockfile(fileno(f), w->w_name, NULL, LOCK_SH|LOCK_NB)) 121510070Seric printf("*"); 121657438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 121724941Seric printf("X"); 121810070Seric else 121910070Seric printf(" "); 122010070Seric errno = 0; 122117468Seric 122259093Seric message[0] = bodytype[0] = '\0'; 12239630Seric while (fgets(buf, sizeof buf, f) != NULL) 12249630Seric { 122553400Seric register int i; 122658737Seric register char *p; 122753400Seric 12289630Seric fixcrlf(buf, TRUE); 12299630Seric switch (buf[0]) 12309630Seric { 123110108Seric case 'M': /* error message */ 123253400Seric if ((i = strlen(&buf[1])) >= sizeof message) 123358737Seric i = sizeof message - 1; 123453400Seric bcopy(&buf[1], message, i); 123553400Seric message[i] = '\0'; 123610108Seric break; 123710108Seric 123859093Seric case 'B': /* body type */ 123959093Seric if ((i = strlen(&buf[1])) >= sizeof bodytype) 124059093Seric i = sizeof bodytype - 1; 124159093Seric bcopy(&buf[1], bodytype, i); 124259093Seric bodytype[i] = '\0'; 124359093Seric break; 124459093Seric 12459630Seric case 'S': /* sender name */ 124624979Seric if (Verbose) 124758737Seric printf("%8ld %10ld%c%.12s %.38s", 124858737Seric dfsize, 124958737Seric w->w_pri, 125058737Seric bitset(EF_WARNING, flags) ? '+' : ' ', 125158737Seric ctime(&submittime) + 4, 125224979Seric &buf[1]); 125324979Seric else 125424979Seric printf("%8ld %.16s %.45s", dfsize, 125524979Seric ctime(&submittime), &buf[1]); 125659093Seric if (message[0] != '\0' || bodytype[0] != '\0') 125759093Seric { 125859093Seric printf("\n %10.10s", bodytype); 125959093Seric if (message[0] != '\0') 126059093Seric printf(" (%.60s)", message); 126159093Seric } 12629630Seric break; 126351920Seric 126440973Sbostic case 'C': /* controlling user */ 126554974Seric if (Verbose) 126658716Seric printf("\n\t\t\t\t (---%.34s---)", 126758716Seric &buf[1]); 126840973Sbostic break; 12699630Seric 12709630Seric case 'R': /* recipient name */ 127124979Seric if (Verbose) 127258716Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 127324979Seric else 127458716Seric printf("\n\t\t\t\t %.45s", &buf[1]); 12759630Seric break; 12769630Seric 12779630Seric case 'T': /* creation time */ 127824941Seric submittime = atol(&buf[1]); 12799630Seric break; 128010070Seric 128110070Seric case 'D': /* data file name */ 128210070Seric if (stat(&buf[1], &st) >= 0) 128310070Seric dfsize = st.st_size; 128410070Seric break; 128558737Seric 128658737Seric case 'F': /* flag bits */ 128758737Seric for (p = &buf[1]; *p != '\0'; p++) 128858737Seric { 128958737Seric switch (*p) 129058737Seric { 129158737Seric case 'w': 129258737Seric flags |= EF_WARNING; 129358737Seric break; 129458737Seric } 129558737Seric } 12969630Seric } 12979630Seric } 129810070Seric if (submittime == (time_t) 0) 129910070Seric printf(" (no control file)"); 13009630Seric printf("\n"); 130123098Seric (void) fclose(f); 13029630Seric } 13039630Seric } 13049630Seric 130556795Seric # endif /* QUEUE */ 130617468Seric /* 130717468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 130817468Seric ** 130917468Seric ** Assigns an id code if one does not already exist. 131017468Seric ** This code is very careful to avoid trashing existing files 131117468Seric ** under any circumstances. 131217468Seric ** 131317468Seric ** Parameters: 131417468Seric ** e -- envelope to build it in/from. 131517468Seric ** type -- the file type, used as the first character 131617468Seric ** of the file name. 131717468Seric ** 131817468Seric ** Returns: 131917468Seric ** a pointer to the new file name (in a static buffer). 132017468Seric ** 132117468Seric ** Side Effects: 132251920Seric ** If no id code is already assigned, queuename will 132351920Seric ** assign an id code, create a qf file, and leave a 132451920Seric ** locked, open-for-write file pointer in the envelope. 132517468Seric */ 132617468Seric 132717468Seric char * 132817468Seric queuename(e, type) 132917468Seric register ENVELOPE *e; 133059700Seric int type; 133117468Seric { 133217468Seric static int pid = -1; 133358741Seric static char c0; 133458741Seric static char c1; 133558741Seric static char c2; 133658716Seric time_t now; 133758716Seric struct tm *tm; 133858689Seric static char buf[MAXNAME]; 133917468Seric 134017468Seric if (e->e_id == NULL) 134117468Seric { 134217468Seric char qf[20]; 134317468Seric 134417468Seric /* find a unique id */ 134517468Seric if (pid != getpid()) 134617468Seric { 134717468Seric /* new process -- start back at "AA" */ 134817468Seric pid = getpid(); 134958716Seric now = curtime(); 135058716Seric tm = localtime(&now); 135158716Seric c0 = 'A' + tm->tm_hour; 135217468Seric c1 = 'A'; 135317468Seric c2 = 'A' - 1; 135417468Seric } 135558716Seric (void) sprintf(qf, "qf%cAA%05d", c0, pid); 135617468Seric 135717468Seric while (c1 < '~' || c2 < 'Z') 135817468Seric { 135917468Seric int i; 136017468Seric 136117468Seric if (c2 >= 'Z') 136217468Seric { 136317468Seric c1++; 136417468Seric c2 = 'A' - 1; 136517468Seric } 136658716Seric qf[3] = c1; 136758716Seric qf[4] = ++c2; 136817468Seric if (tTd(7, 20)) 136940934Srick printf("queuename: trying \"%s\"\n", qf); 137017468Seric 137140934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 137251920Seric if (i < 0) 137351920Seric { 137451920Seric if (errno == EEXIST) 137551920Seric continue; 137664705Seric syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 137764705Seric qf, QueueDir, geteuid()); 137851920Seric exit(EX_UNAVAILABLE); 137951920Seric } 138064335Seric if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB)) 138151920Seric { 138251920Seric e->e_lockfp = fdopen(i, "w"); 138340934Srick break; 138417468Seric } 138551920Seric 138651920Seric /* a reader got the file; abandon it and try again */ 138751920Seric (void) close(i); 138817468Seric } 138917468Seric if (c1 >= '~' && c2 >= 'Z') 139017468Seric { 139164705Seric syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)", 139264705Seric qf, QueueDir, geteuid()); 139317468Seric exit(EX_OSERR); 139417468Seric } 139517468Seric e->e_id = newstr(&qf[2]); 139617468Seric define('i', e->e_id, e); 139717468Seric if (tTd(7, 1)) 139817468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 139917468Seric # ifdef LOG 140058020Seric if (LogLevel > 93) 140117468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 140256795Seric # endif /* LOG */ 140317468Seric } 140417468Seric 140517468Seric if (type == '\0') 140617468Seric return (NULL); 140717468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 140817468Seric if (tTd(7, 2)) 140917468Seric printf("queuename: %s\n", buf); 141017468Seric return (buf); 141117468Seric } 141217468Seric /* 141317468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 141417468Seric ** 141517468Seric ** Parameters: 141617468Seric ** e -- the envelope to unlock. 141717468Seric ** 141817468Seric ** Returns: 141917468Seric ** none 142017468Seric ** 142117468Seric ** Side Effects: 142217468Seric ** unlocks the queue for `e'. 142317468Seric */ 142417468Seric 142517468Seric unlockqueue(e) 142617468Seric ENVELOPE *e; 142717468Seric { 142858680Seric if (tTd(51, 4)) 142958680Seric printf("unlockqueue(%s)\n", e->e_id); 143058680Seric 143151920Seric /* if there is a lock file in the envelope, close it */ 143251920Seric if (e->e_lockfp != NULL) 143358680Seric xfclose(e->e_lockfp, "unlockqueue", e->e_id); 143451920Seric e->e_lockfp = NULL; 143551920Seric 143658728Seric /* don't create a queue id if we don't already have one */ 143758728Seric if (e->e_id == NULL) 143858728Seric return; 143958728Seric 144017468Seric /* remove the transcript */ 144117468Seric # ifdef LOG 144258020Seric if (LogLevel > 87) 144317468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 144456795Seric # endif /* LOG */ 144558680Seric if (!tTd(51, 104)) 144617468Seric xunlink(queuename(e, 'x')); 144717468Seric 144817468Seric } 144940973Sbostic /* 145054974Seric ** SETCTLUSER -- create a controlling address 145140973Sbostic ** 145254974Seric ** Create a fake "address" given only a local login name; this is 145354974Seric ** used as a "controlling user" for future recipient addresses. 145440973Sbostic ** 145540973Sbostic ** Parameters: 145654974Seric ** user -- the user name of the controlling user. 145740973Sbostic ** 145840973Sbostic ** Returns: 145954974Seric ** An address descriptor for the controlling user. 146040973Sbostic ** 146140973Sbostic ** Side Effects: 146240973Sbostic ** none. 146340973Sbostic */ 146440973Sbostic 146554974Seric ADDRESS * 146654974Seric setctluser(user) 146754974Seric char *user; 146840973Sbostic { 146954974Seric register ADDRESS *a; 147040973Sbostic struct passwd *pw; 147159113Seric char *p; 147240973Sbostic 147340973Sbostic /* 147454974Seric ** See if this clears our concept of controlling user. 147540973Sbostic */ 147640973Sbostic 147763850Seric if (user == NULL || *user == '\0') 147863850Seric return NULL; 147940973Sbostic 148040973Sbostic /* 148154974Seric ** Set up addr fields for controlling user. 148240973Sbostic */ 148340973Sbostic 148454974Seric a = (ADDRESS *) xalloc(sizeof *a); 148554974Seric bzero((char *) a, sizeof *a); 148659113Seric 148759113Seric p = strchr(user, ':'); 148859113Seric if (p != NULL) 148959113Seric *p++ = '\0'; 149059270Seric if (*user != '\0' && (pw = getpwnam(user)) != NULL) 149140973Sbostic { 149240973Sbostic a->q_home = newstr(pw->pw_dir); 149340973Sbostic a->q_uid = pw->pw_uid; 149440973Sbostic a->q_gid = pw->pw_gid; 149557642Seric a->q_user = newstr(user); 149659270Seric a->q_flags |= QGOODUID; 149740973Sbostic } 149840973Sbostic else 149940973Sbostic { 150057642Seric a->q_user = newstr(DefUser); 150140973Sbostic } 150240973Sbostic 150359270Seric a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 150456328Seric a->q_mailer = LocalMailer; 150559113Seric if (p == NULL) 150659113Seric a->q_paddr = a->q_user; 150759113Seric else 150859113Seric a->q_paddr = newstr(p); 150954974Seric return a; 151040973Sbostic } 1511