122710Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 363589Sbostic * Copyright (c) 1988, 1993 463589Sbostic * The Regents of the University of California. All rights reserved. 533731Sbostic * 642829Sbostic * %sccs.include.redist.c% 733731Sbostic */ 822710Sdist 922710Sdist #ifndef lint 10*64146Seric static char sccsid[] = "@(#)recipient.c 8.14 (Berkeley) 08/08/93"; 1133731Sbostic #endif /* not lint */ 1222710Sdist 1358332Seric # include "sendmail.h" 144174Seric # include <pwd.h> 154174Seric 164174Seric /* 179622Seric ** SENDTOLIST -- Designate a send list. 184174Seric ** 194174Seric ** The parameter is a comma-separated list of people to send to. 204174Seric ** This routine arranges to send to all of them. 214174Seric ** 224174Seric ** Parameters: 234174Seric ** list -- the send list. 244399Seric ** ctladdr -- the address template for the person to 254399Seric ** send to -- effective uid/gid are important. 265006Seric ** This is typically the alias that caused this 275006Seric ** expansion. 285006Seric ** sendq -- a pointer to the head of a queue to put 295006Seric ** these people into. 3058247Seric ** e -- the envelope in which to add these recipients. 314174Seric ** 324174Seric ** Returns: 3358082Seric ** The number of addresses actually on the list. 344174Seric ** 354174Seric ** Side Effects: 364174Seric ** none. 374174Seric */ 384174Seric 394174Seric # define MAXRCRSN 10 404174Seric 4155012Seric sendtolist(list, ctladdr, sendq, e) 424174Seric char *list; 434399Seric ADDRESS *ctladdr; 445198Seric ADDRESS **sendq; 4555012Seric register ENVELOPE *e; 464174Seric { 474174Seric register char *p; 488223Seric register ADDRESS *al; /* list of addresses to send to */ 494423Seric bool firstone; /* set on first address sent */ 5011446Seric char delimiter; /* the address delimiter */ 5158082Seric int naddrs; 5263847Seric char *oldto = e->e_to; 534174Seric 5464131Seric if (list == NULL) 5564131Seric { 5664131Seric syserr("sendtolist: null list"); 5764131Seric return 0; 5864131Seric } 5964131Seric 607676Seric if (tTd(25, 1)) 614444Seric { 624444Seric printf("sendto: %s\n ctladdr=", list); 634444Seric printaddr(ctladdr, FALSE); 644444Seric } 654324Seric 668223Seric /* heuristic to determine old versus new style addresses */ 678230Seric if (ctladdr == NULL && 6856795Seric (strchr(list, ',') != NULL || strchr(list, ';') != NULL || 6956795Seric strchr(list, '<') != NULL || strchr(list, '(') != NULL)) 7055012Seric e->e_flags &= ~EF_OLDSTYLE; 7111446Seric delimiter = ' '; 7255012Seric if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL) 7311446Seric delimiter = ','; 748223Seric 754423Seric firstone = TRUE; 764324Seric al = NULL; 7758082Seric naddrs = 0; 788223Seric 798081Seric for (p = list; *p != '\0'; ) 804174Seric { 8158333Seric auto char *delimptr; 828081Seric register ADDRESS *a; 834319Seric 848081Seric /* parse the address */ 8558050Seric while ((isascii(*p) && isspace(*p)) || *p == ',') 864174Seric p++; 8758333Seric a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter, &delimptr, e); 8858333Seric p = delimptr; 899297Seric if (a == NULL) 904174Seric continue; 914324Seric a->q_next = al; 924399Seric a->q_alias = ctladdr; 934444Seric 944444Seric /* see if this should be marked as a primary address */ 954423Seric if (ctladdr == NULL || 968081Seric (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags))) 974423Seric a->q_flags |= QPRIMARY; 984444Seric 999379Seric if (ctladdr != NULL && sameaddr(ctladdr, a)) 10058061Seric ctladdr->q_flags |= QSELFREF; 10157731Seric al = a; 1024423Seric firstone = FALSE; 1034324Seric } 1044324Seric 1054324Seric /* arrange to send to everyone on the local send list */ 1064324Seric while (al != NULL) 1074324Seric { 1084324Seric register ADDRESS *a = al; 1094324Seric 1104324Seric al = a->q_next; 11155012Seric a = recipient(a, sendq, e); 1124993Seric 1134998Seric /* arrange to inherit full name */ 1144998Seric if (a->q_fullname == NULL && ctladdr != NULL) 1154998Seric a->q_fullname = ctladdr->q_fullname; 11658082Seric naddrs++; 1174174Seric } 1184324Seric 11963847Seric e->e_to = oldto; 12058082Seric return (naddrs); 1214174Seric } 1224174Seric /* 1234174Seric ** RECIPIENT -- Designate a message recipient 1244174Seric ** 1254174Seric ** Saves the named person for future mailing. 1264174Seric ** 1274174Seric ** Parameters: 1284174Seric ** a -- the (preparsed) address header for the recipient. 1295006Seric ** sendq -- a pointer to the head of a queue to put the 1305006Seric ** recipient in. Duplicate supression is done 1315006Seric ** in this queue. 13257731Seric ** e -- the current envelope. 1334174Seric ** 1344174Seric ** Returns: 13512613Seric ** The actual address in the queue. This will be "a" if 13612613Seric ** the address is not a duplicate, else the original address. 1374174Seric ** 1384174Seric ** Side Effects: 1394174Seric ** none. 1404174Seric */ 1414174Seric 14212613Seric ADDRESS * 14355012Seric recipient(a, sendq, e) 1444174Seric register ADDRESS *a; 1455006Seric register ADDRESS **sendq; 14655012Seric register ENVELOPE *e; 1474174Seric { 1484174Seric register ADDRESS *q; 1494319Seric ADDRESS **pq; 1504174Seric register struct mailer *m; 1519210Seric register char *p; 1529210Seric bool quoted = FALSE; /* set if the addr has a quote bit */ 15353735Seric int findusercount = 0; 1549210Seric char buf[MAXNAME]; /* unquoted image of the user name */ 15558247Seric extern int safefile(); 1564174Seric 15755012Seric e->e_to = a->q_paddr; 1584600Seric m = a->q_mailer; 1594174Seric errno = 0; 1607676Seric if (tTd(26, 1)) 1614444Seric { 1624444Seric printf("\nrecipient: "); 1634444Seric printaddr(a, FALSE); 1644444Seric } 1654174Seric 166*64146Seric /* if this is primary, add it to the original recipient list */ 167*64146Seric if (a->q_alias == NULL) 168*64146Seric { 169*64146Seric if (e->e_origrcpt == NULL) 170*64146Seric e->e_origrcpt = a->q_paddr; 171*64146Seric else if (e->e_origrcpt != a->q_paddr) 172*64146Seric e->e_origrcpt = ""; 173*64146Seric } 174*64146Seric 1754174Seric /* break aliasing loops */ 1764174Seric if (AliasLevel > MAXRCRSN) 1774174Seric { 17858151Seric usrerr("554 aliasing/forwarding loop broken"); 17912613Seric return (a); 1804174Seric } 1814174Seric 1824174Seric /* 1834627Seric ** Finish setting up address structure. 1844174Seric */ 1854174Seric 18616160Seric /* set the queue timeout */ 18758737Seric a->q_timeout = TimeOuts.to_q_return; 1884627Seric 18916160Seric /* get unquoted user for file, program or user.name check */ 1909210Seric (void) strcpy(buf, a->q_user); 1919210Seric for (p = buf; *p != '\0' && !quoted; p++) 1929210Seric { 19354993Seric if (*p == '\\') 1949210Seric quoted = TRUE; 1959210Seric } 19654983Seric stripquotes(buf); 1979210Seric 19857402Seric /* check for direct mailing to restricted mailers */ 19958737Seric if (a->q_alias == NULL && m == ProgMailer && 20058737Seric !bitset(EF_QUEUERUN, e->e_flags)) 2014174Seric { 20258680Seric a->q_flags |= QBADADDR; 20363847Seric usrerr("550 Cannot mail directly to programs"); 2044174Seric } 2054174Seric 2064174Seric /* 2074419Seric ** Look up this person in the recipient list. 2084419Seric ** If they are there already, return, otherwise continue. 2094419Seric ** If the list is empty, just add it. Notice the cute 2104419Seric ** hack to make from addresses suppress things correctly: 2114419Seric ** the QDONTSEND bit will be set in the send list. 2124419Seric ** [Please note: the emphasis is on "hack."] 2134174Seric */ 2144174Seric 2155006Seric for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 2164174Seric { 21758294Seric if (sameaddr(q, a)) 2184174Seric { 2197676Seric if (tTd(26, 1)) 2204444Seric { 2214444Seric printf("%s in sendq: ", a->q_paddr); 2224444Seric printaddr(q, FALSE); 2234444Seric } 2244423Seric if (!bitset(QPRIMARY, q->q_flags)) 22558065Seric { 22658065Seric if (!bitset(QDONTSEND, a->q_flags)) 22758151Seric message("duplicate suppressed"); 2284423Seric q->q_flags |= a->q_flags; 22958065Seric } 23063847Seric a = q; 23163847Seric goto testselfdestruct; 2324174Seric } 2334319Seric } 2344174Seric 2354319Seric /* add address on list */ 23658884Seric *pq = a; 23758884Seric a->q_next = NULL; 2384174Seric 2394174Seric /* 24057402Seric ** Alias the name and handle special mailer types. 2414174Seric */ 2424174Seric 24353735Seric trylocaluser: 24455354Seric if (tTd(29, 7)) 24555354Seric printf("at trylocaluser %s\n", a->q_user); 24655354Seric 24758680Seric if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags)) 24863847Seric goto testselfdestruct; 24957402Seric 25057402Seric if (m == InclMailer) 2514174Seric { 25257402Seric a->q_flags |= QDONTSEND; 25358737Seric if (a->q_alias == NULL && !bitset(EF_QUEUERUN, e->e_flags)) 2544174Seric { 25558680Seric a->q_flags |= QBADADDR; 25658151Seric usrerr("550 Cannot mail directly to :include:s"); 2574174Seric } 2584174Seric else 25950556Seric { 26059563Seric int ret; 26158247Seric 26258151Seric message("including file %s", a->q_user); 26359563Seric ret = include(a->q_user, FALSE, a, sendq, e); 26459563Seric if (transienterror(ret)) 26559563Seric { 26659563Seric #ifdef LOG 26759563Seric if (LogLevel > 2) 26859615Seric syslog(LOG_ERR, "%s: include %s: transient error: %e", 26959623Seric e->e_id, a->q_user, errstring(ret)); 27059563Seric #endif 27163853Seric a->q_flags |= QQUEUEUP; 27259563Seric usrerr("451 Cannot open %s: %s", 27359563Seric a->q_user, errstring(ret)); 27459563Seric } 27559563Seric else if (ret != 0) 27659563Seric { 27763938Seric a->q_flags |= QBADADDR; 27859563Seric usrerr("550 Cannot open %s: %s", 27959563Seric a->q_user, errstring(ret)); 28059563Seric } 28150556Seric } 2824174Seric } 28357642Seric else if (m == FileMailer) 2844174Seric { 2854329Seric struct stat stb; 2864329Seric extern bool writable(); 2874174Seric 28856795Seric p = strrchr(buf, '/'); 28951317Seric /* check if writable or creatable */ 29058737Seric if (a->q_alias == NULL && !bitset(EF_QUEUERUN, e->e_flags)) 2914174Seric { 29258680Seric a->q_flags |= QBADADDR; 29358151Seric usrerr("550 Cannot mail directly to files"); 2944174Seric } 29551317Seric else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) : 29664083Seric (*p = '\0', safefile(buf, RealUid, RealGid, NULL, TRUE, S_IWRITE|S_IEXEC) != 0)) 29751317Seric { 29858680Seric a->q_flags |= QBADADDR; 29958337Seric giveresponse(EX_CANTCREAT, m, NULL, e); 30051317Seric } 30151317Seric } 30251317Seric 30357402Seric if (m != LocalMailer) 30457642Seric { 30557642Seric if (!bitset(QDONTSEND, a->q_flags)) 30657642Seric e->e_nrcpts++; 30763847Seric goto testselfdestruct; 30857642Seric } 30957402Seric 31057402Seric /* try aliasing */ 31157402Seric alias(a, sendq, e); 31257402Seric 31357402Seric # ifdef USERDB 31457402Seric /* if not aliased, look it up in the user database */ 31558918Seric if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags)) 31657402Seric { 31757402Seric extern int udbexpand(); 31859615Seric extern int errno; 31957402Seric 32057402Seric if (udbexpand(a, sendq, e) == EX_TEMPFAIL) 32157402Seric { 32263853Seric a->q_flags |= QQUEUEUP; 32357402Seric if (e->e_message == NULL) 32457402Seric e->e_message = newstr("Deferred: user database error"); 32557402Seric # ifdef LOG 32658020Seric if (LogLevel > 8) 32759623Seric syslog(LOG_INFO, "%s: deferred: udbexpand: %s", 32859623Seric e->e_id, errstring(errno)); 32957402Seric # endif 33059615Seric message("queued (user database error): %s", 33159615Seric errstring(errno)); 33257642Seric e->e_nrcpts++; 33363847Seric goto testselfdestruct; 33457402Seric } 33557402Seric } 33657402Seric # endif 33757402Seric 33857402Seric /* if it was an alias or a UDB expansion, just return now */ 33958247Seric if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags)) 34063847Seric goto testselfdestruct; 34157402Seric 34251317Seric /* 34351317Seric ** If we have a level two config file, then pass the name through 34451317Seric ** Ruleset 5 before sending it off. Ruleset 5 has the right 34551317Seric ** to send rewrite it to another mailer. This gives us a hook 34651317Seric ** after local aliasing has been done. 34751317Seric */ 34851317Seric 34951317Seric if (tTd(29, 5)) 35051317Seric { 35151317Seric printf("recipient: testing local? cl=%d, rr5=%x\n\t", 35251317Seric ConfigLevel, RewriteRules[5]); 35351317Seric printaddr(a, FALSE); 35451317Seric } 35551317Seric if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 && 35651317Seric RewriteRules[5] != NULL) 35751317Seric { 35855012Seric maplocaluser(a, sendq, e); 35951317Seric } 36051317Seric 36151317Seric /* 36251317Seric ** If it didn't get rewritten to another mailer, go ahead 36351317Seric ** and deliver it. 36451317Seric */ 36551317Seric 36658247Seric if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags)) 36751317Seric { 36855354Seric auto bool fuzzy; 36951317Seric register struct passwd *pw; 37051317Seric extern struct passwd *finduser(); 37151317Seric 37251317Seric /* warning -- finduser may trash buf */ 37355354Seric pw = finduser(buf, &fuzzy); 37451317Seric if (pw == NULL) 37551317Seric { 37658680Seric a->q_flags |= QBADADDR; 37758337Seric giveresponse(EX_NOUSER, m, NULL, e); 37851317Seric } 3794174Seric else 3804174Seric { 38151317Seric char nbuf[MAXNAME]; 3824373Seric 38355354Seric if (fuzzy) 3844174Seric { 38553735Seric /* name was a fuzzy match */ 38651317Seric a->q_user = newstr(pw->pw_name); 38753735Seric if (findusercount++ > 3) 38853735Seric { 38958680Seric a->q_flags |= QBADADDR; 39058151Seric usrerr("554 aliasing/forwarding loop for %s broken", 39153735Seric pw->pw_name); 39253735Seric return (a); 39353735Seric } 39453735Seric 39553735Seric /* see if it aliases */ 39651317Seric (void) strcpy(buf, pw->pw_name); 39753735Seric goto trylocaluser; 3984174Seric } 39951317Seric a->q_home = newstr(pw->pw_dir); 40051317Seric a->q_uid = pw->pw_uid; 40151317Seric a->q_gid = pw->pw_gid; 40259083Seric a->q_ruser = newstr(pw->pw_name); 40351317Seric a->q_flags |= QGOODUID; 40451317Seric buildfname(pw->pw_gecos, pw->pw_name, nbuf); 40551317Seric if (nbuf[0] != '\0') 40651317Seric a->q_fullname = newstr(nbuf); 40751317Seric if (!quoted) 40855012Seric forward(a, sendq, e); 4094174Seric } 4104174Seric } 41157642Seric if (!bitset(QDONTSEND, a->q_flags)) 41257642Seric e->e_nrcpts++; 41363847Seric 41463847Seric testselfdestruct: 41563978Seric if (tTd(26, 8)) 41663847Seric { 41763978Seric printf("testselfdestruct: "); 41863978Seric printaddr(a, TRUE); 41963978Seric } 42063978Seric if (a->q_alias == NULL && a != &e->e_from && 42163978Seric bitset(QDONTSEND, a->q_flags)) 42263978Seric { 42363978Seric q = *sendq; 42463965Seric while (q != NULL && bitset(QDONTSEND, q->q_flags)) 42563847Seric q = q->q_next; 42663978Seric if (q == NULL) 42763847Seric { 42863847Seric a->q_flags |= QBADADDR; 42963847Seric usrerr("554 aliasing/forwarding loop broken"); 43063847Seric } 43163847Seric } 43212613Seric return (a); 4334174Seric } 4344174Seric /* 4354373Seric ** FINDUSER -- find the password entry for a user. 4364373Seric ** 4374373Seric ** This looks a lot like getpwnam, except that it may want to 4384373Seric ** do some fancier pattern matching in /etc/passwd. 4394373Seric ** 4409379Seric ** This routine contains most of the time of many sendmail runs. 4419379Seric ** It deserves to be optimized. 4429379Seric ** 4434373Seric ** Parameters: 4444373Seric ** name -- the name to match against. 44555354Seric ** fuzzyp -- an outarg that is set to TRUE if this entry 44655354Seric ** was found using the fuzzy matching algorithm; 44755354Seric ** set to FALSE otherwise. 4484373Seric ** 4494373Seric ** Returns: 4504373Seric ** A pointer to a pw struct. 4514373Seric ** NULL if name is unknown or ambiguous. 4524373Seric ** 4534373Seric ** Side Effects: 4544407Seric ** may modify name. 4554373Seric */ 4564373Seric 4574373Seric struct passwd * 45855354Seric finduser(name, fuzzyp) 4594373Seric char *name; 46055354Seric bool *fuzzyp; 4614373Seric { 4624376Seric register struct passwd *pw; 4634407Seric register char *p; 46415325Seric extern struct passwd *getpwent(); 46515325Seric extern struct passwd *getpwnam(); 4664373Seric 46755354Seric if (tTd(29, 4)) 46855354Seric printf("finduser(%s): ", name); 46955354Seric 47055354Seric *fuzzyp = FALSE; 4714407Seric 47225777Seric /* look up this login name using fast path */ 47312634Seric if ((pw = getpwnam(name)) != NULL) 47455354Seric { 47555354Seric if (tTd(29, 4)) 47655354Seric printf("found (non-fuzzy)\n"); 47712634Seric return (pw); 47855354Seric } 47912634Seric 48053735Seric #ifdef MATCHGECOS 48153735Seric /* see if fuzzy matching allowed */ 48253735Seric if (!MatchGecos) 48355354Seric { 48455354Seric if (tTd(29, 4)) 48555354Seric printf("not found (fuzzy disabled)\n"); 48653735Seric return NULL; 48755354Seric } 48853735Seric 48912634Seric /* search for a matching full name instead */ 49025777Seric for (p = name; *p != '\0'; p++) 49125777Seric { 49225777Seric if (*p == (SpaceSub & 0177) || *p == '_') 49325777Seric *p = ' '; 49425777Seric } 49523107Seric (void) setpwent(); 4964376Seric while ((pw = getpwent()) != NULL) 4974376Seric { 4984998Seric char buf[MAXNAME]; 4994376Seric 5004998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 50156795Seric if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name)) 5024381Seric { 50355354Seric if (tTd(29, 4)) 50455354Seric printf("fuzzy matches %s\n", pw->pw_name); 50558151Seric message("sending to login name %s", pw->pw_name); 50655354Seric *fuzzyp = TRUE; 5074376Seric return (pw); 5084377Seric } 5094376Seric } 51055354Seric if (tTd(29, 4)) 51155354Seric printf("no fuzzy match found\n"); 51259015Seric #else 51359015Seric if (tTd(29, 4)) 51459015Seric printf("not found (fuzzy disabled)\n"); 51559015Seric #endif 5164376Seric return (NULL); 5174373Seric } 5184373Seric /* 5194329Seric ** WRITABLE -- predicate returning if the file is writable. 5204329Seric ** 5214329Seric ** This routine must duplicate the algorithm in sys/fio.c. 5224329Seric ** Unfortunately, we cannot use the access call since we 5234329Seric ** won't necessarily be the real uid when we try to 5244329Seric ** actually open the file. 5254329Seric ** 5264329Seric ** Notice that ANY file with ANY execute bit is automatically 5274329Seric ** not writable. This is also enforced by mailfile. 5284329Seric ** 5294329Seric ** Parameters: 5304329Seric ** s -- pointer to a stat struct for the file. 5314329Seric ** 5324329Seric ** Returns: 5334329Seric ** TRUE -- if we will be able to write this file. 5344329Seric ** FALSE -- if we cannot write this file. 5354329Seric ** 5364329Seric ** Side Effects: 5374329Seric ** none. 5384329Seric */ 5394329Seric 5404329Seric bool 5414329Seric writable(s) 5424329Seric register struct stat *s; 5434329Seric { 54455372Seric uid_t euid; 54555372Seric gid_t egid; 5464329Seric int bits; 5474329Seric 5484329Seric if (bitset(0111, s->st_mode)) 5494329Seric return (FALSE); 55063787Seric euid = RealUid; 55163787Seric egid = RealGid; 5524329Seric if (geteuid() == 0) 5534329Seric { 5544329Seric if (bitset(S_ISUID, s->st_mode)) 5554329Seric euid = s->st_uid; 5564329Seric if (bitset(S_ISGID, s->st_mode)) 5574329Seric egid = s->st_gid; 5584329Seric } 5594329Seric 5604329Seric if (euid == 0) 5614329Seric return (TRUE); 5624329Seric bits = S_IWRITE; 5634329Seric if (euid != s->st_uid) 5644329Seric { 5654329Seric bits >>= 3; 5664329Seric if (egid != s->st_gid) 5674329Seric bits >>= 3; 5684329Seric } 5694329Seric return ((s->st_mode & bits) != 0); 5704329Seric } 5714329Seric /* 5724174Seric ** INCLUDE -- handle :include: specification. 5734174Seric ** 5744174Seric ** Parameters: 5754174Seric ** fname -- filename to include. 57653037Seric ** forwarding -- if TRUE, we are reading a .forward file. 57753037Seric ** if FALSE, it's a :include: file. 5784399Seric ** ctladdr -- address template to use to fill in these 5794399Seric ** addresses -- effective user/group id are 5804399Seric ** the important things. 5815006Seric ** sendq -- a pointer to the head of the send queue 5825006Seric ** to put these addresses in. 5834174Seric ** 5844174Seric ** Returns: 58557136Seric ** open error status 5864174Seric ** 5874174Seric ** Side Effects: 5884174Seric ** reads the :include: file and sends to everyone 5894174Seric ** listed in that file. 5904174Seric */ 5914174Seric 59253037Seric static jmp_buf CtxIncludeTimeout; 59363937Seric static int includetimeout(); 59453037Seric 59557136Seric int 59655012Seric include(fname, forwarding, ctladdr, sendq, e) 5974174Seric char *fname; 59853037Seric bool forwarding; 5994399Seric ADDRESS *ctladdr; 6005006Seric ADDRESS **sendq; 60155012Seric ENVELOPE *e; 6024174Seric { 6034174Seric register FILE *fp; 60455012Seric char *oldto = e->e_to; 6059379Seric char *oldfilename = FileName; 6069379Seric int oldlinenumber = LineNumber; 60753037Seric register EVENT *ev = NULL; 60858082Seric int nincludes; 60958247Seric int ret; 61063581Seric ADDRESS *ca; 61163581Seric uid_t uid; 61264083Seric gid_t gid; 61364083Seric char *uname; 61453037Seric char buf[MAXLINE]; 6154174Seric 61657186Seric if (tTd(27, 2)) 61757186Seric printf("include(%s)\n", fname); 61863902Seric if (tTd(27, 4)) 61963902Seric printf(" ruid=%d euid=%d\n", getuid(), geteuid()); 62063581Seric if (tTd(27, 14)) 62163581Seric { 62263581Seric printf("ctladdr "); 62363581Seric printaddr(ctladdr, FALSE); 62463581Seric } 62557186Seric 62653037Seric /* 62753037Seric ** If home directory is remote mounted but server is down, 62853037Seric ** this can hang or give errors; use a timeout to avoid this 62953037Seric */ 63053037Seric 63163581Seric ca = getctladdr(ctladdr); 63263581Seric if (ca == NULL) 63364083Seric { 63463581Seric uid = 0; 63564083Seric gid = 0; 63664083Seric uname = NULL; 63764083Seric } 63863581Seric else 63964083Seric { 64063581Seric uid = ca->q_uid; 64164083Seric gid = ca->q_gid; 64264083Seric uname = ca->q_user; 64364083Seric } 64463581Seric 64553037Seric if (setjmp(CtxIncludeTimeout) != 0) 64653037Seric { 64763853Seric ctladdr->q_flags |= QQUEUEUP; 64853037Seric errno = 0; 64953037Seric usrerr("451 open timeout on %s", fname); 65063993Seric 65163993Seric /* return pseudo-error code */ 65263993Seric return EOPENTIMEOUT; 65353037Seric } 65453037Seric ev = setevent((time_t) 60, includetimeout, 0); 65553037Seric 65663581Seric /* the input file must be marked safe */ 65764083Seric ret = safefile(fname, uid, gid, uname, forwarding, S_IREAD); 65864083Seric if (ret != 0) 65953037Seric { 66053037Seric /* don't use this .forward file */ 66153037Seric clrevent(ev); 66257186Seric if (tTd(27, 4)) 66358247Seric printf("include: not safe (uid=%d): %s\n", 66463581Seric uid, errstring(ret)); 66558247Seric return ret; 66653037Seric } 66753037Seric 6684174Seric fp = fopen(fname, "r"); 6694174Seric if (fp == NULL) 6704174Seric { 67157136Seric int ret = errno; 67257136Seric 67358061Seric clrevent(ev); 67463902Seric if (tTd(27, 4)) 67563902Seric printf("include: open: %s\n", errstring(ret)); 67657136Seric return ret; 6774174Seric } 67853037Seric 67963581Seric if (ca == NULL) 6804406Seric { 6814406Seric struct stat st; 6824174Seric 6834406Seric if (fstat(fileno(fp), &st) < 0) 68458061Seric { 68558061Seric int ret = errno; 68658061Seric 68758061Seric clrevent(ev); 6884406Seric syserr("Cannot fstat %s!", fname); 68958061Seric return ret; 69058061Seric } 6914406Seric ctladdr->q_uid = st.st_uid; 6924406Seric ctladdr->q_gid = st.st_gid; 6934406Seric ctladdr->q_flags |= QGOODUID; 6944406Seric } 6954406Seric 69653037Seric clrevent(ev); 69753037Seric 69858092Seric if (bitset(EF_VRFYONLY, e->e_flags)) 69958092Seric { 70058092Seric /* don't do any more now */ 70158868Seric ctladdr->q_flags |= QVERIFIED; 70258884Seric e->e_nrcpts++; 70358680Seric xfclose(fp, "include", fname); 70458092Seric return 0; 70558092Seric } 70658092Seric 7074174Seric /* read the file -- each line is a comma-separated list. */ 7089379Seric FileName = fname; 7099379Seric LineNumber = 0; 71058082Seric ctladdr->q_flags &= ~QSELFREF; 71158082Seric nincludes = 0; 7124174Seric while (fgets(buf, sizeof buf, fp) != NULL) 7134174Seric { 71456795Seric register char *p = strchr(buf, '\n'); 7154174Seric 71640963Sbostic LineNumber++; 7174174Seric if (p != NULL) 7184174Seric *p = '\0'; 71957186Seric if (buf[0] == '#' || buf[0] == '\0') 72057139Seric continue; 72158008Seric e->e_to = NULL; 72258151Seric message("%s to %s", 72353037Seric forwarding ? "forwarding" : "sending", buf); 72457977Seric #ifdef LOG 72558020Seric if (forwarding && LogLevel > 9) 72657977Seric syslog(LOG_INFO, "%s: forward %s => %s", 72757977Seric e->e_id, oldto, buf); 72857977Seric #endif 72957977Seric 7304176Seric AliasLevel++; 73158082Seric nincludes += sendtolist(buf, ctladdr, sendq, e); 7324176Seric AliasLevel--; 7334174Seric } 73463902Seric 73563902Seric if (ferror(fp) && tTd(27, 3)) 73663902Seric printf("include: read error: %s\n", errstring(errno)); 73758082Seric if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags)) 73858065Seric { 73958065Seric if (tTd(27, 5)) 74058065Seric { 74158065Seric printf("include: QDONTSEND "); 74258065Seric printaddr(ctladdr, FALSE); 74358065Seric } 74458065Seric ctladdr->q_flags |= QDONTSEND; 74558065Seric } 7464174Seric 74758680Seric (void) xfclose(fp, "include", fname); 7489379Seric FileName = oldfilename; 7499379Seric LineNumber = oldlinenumber; 75063847Seric e->e_to = oldto; 75157136Seric return 0; 7524174Seric } 75353037Seric 75453037Seric static 75553037Seric includetimeout() 75653037Seric { 75753037Seric longjmp(CtxIncludeTimeout, 1); 75853037Seric } 7594324Seric /* 7604324Seric ** SENDTOARGV -- send to an argument vector. 7614324Seric ** 7624324Seric ** Parameters: 7634324Seric ** argv -- argument vector to send to. 76458247Seric ** e -- the current envelope. 7654324Seric ** 7664324Seric ** Returns: 7674324Seric ** none. 7684324Seric ** 7694324Seric ** Side Effects: 7704324Seric ** puts all addresses on the argument vector onto the 7714324Seric ** send queue. 7724324Seric */ 7734324Seric 77455012Seric sendtoargv(argv, e) 7754324Seric register char **argv; 77655012Seric register ENVELOPE *e; 7774324Seric { 7784324Seric register char *p; 7794324Seric 7804324Seric while ((p = *argv++) != NULL) 7814324Seric { 78258082Seric (void) sendtolist(p, (ADDRESS *) NULL, &e->e_sendqueue, e); 7834324Seric } 7844324Seric } 7854399Seric /* 7864399Seric ** GETCTLADDR -- get controlling address from an address header. 7874399Seric ** 7884399Seric ** If none, get one corresponding to the effective userid. 7894399Seric ** 7904399Seric ** Parameters: 7914399Seric ** a -- the address to find the controller of. 7924399Seric ** 7934399Seric ** Returns: 7944399Seric ** the controlling address. 7954399Seric ** 7964399Seric ** Side Effects: 7974399Seric ** none. 7984399Seric */ 7994399Seric 8004399Seric ADDRESS * 8014399Seric getctladdr(a) 8024399Seric register ADDRESS *a; 8034399Seric { 8044404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 8054399Seric a = a->q_alias; 8064399Seric return (a); 8074399Seric } 808