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*64771Seric static char sccsid[] = "@(#)recipient.c 8.21 (Berkeley) 10/29/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++; 8764284Seric a = parseaddr(p, NULLADDR, RF_COPYALL, 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 16664146Seric /* if this is primary, add it to the original recipient list */ 16764146Seric if (a->q_alias == NULL) 16864146Seric { 16964146Seric if (e->e_origrcpt == NULL) 17064146Seric e->e_origrcpt = a->q_paddr; 17164146Seric else if (e->e_origrcpt != a->q_paddr) 17264146Seric e->e_origrcpt = ""; 17364146Seric } 17464146Seric 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 */ 19964761Seric if (a->q_alias == NULL && m == ProgMailer) 2004174Seric { 20158680Seric a->q_flags |= QBADADDR; 20263847Seric usrerr("550 Cannot mail directly to programs"); 2034174Seric } 2044174Seric 2054174Seric /* 2064419Seric ** Look up this person in the recipient list. 2074419Seric ** If they are there already, return, otherwise continue. 2084419Seric ** If the list is empty, just add it. Notice the cute 2094419Seric ** hack to make from addresses suppress things correctly: 2104419Seric ** the QDONTSEND bit will be set in the send list. 2114419Seric ** [Please note: the emphasis is on "hack."] 2124174Seric */ 2134174Seric 2145006Seric for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 2154174Seric { 21658294Seric if (sameaddr(q, a)) 2174174Seric { 2187676Seric if (tTd(26, 1)) 2194444Seric { 2204444Seric printf("%s in sendq: ", a->q_paddr); 2214444Seric printaddr(q, FALSE); 2224444Seric } 2234423Seric if (!bitset(QPRIMARY, q->q_flags)) 22458065Seric { 22558065Seric if (!bitset(QDONTSEND, a->q_flags)) 22658151Seric message("duplicate suppressed"); 2274423Seric q->q_flags |= a->q_flags; 22858065Seric } 22963847Seric a = q; 23063847Seric goto testselfdestruct; 2314174Seric } 2324319Seric } 2334174Seric 2344319Seric /* add address on list */ 23558884Seric *pq = a; 23658884Seric a->q_next = NULL; 2374174Seric 2384174Seric /* 23957402Seric ** Alias the name and handle special mailer types. 2404174Seric */ 2414174Seric 24253735Seric trylocaluser: 24355354Seric if (tTd(29, 7)) 24455354Seric printf("at trylocaluser %s\n", a->q_user); 24555354Seric 24658680Seric if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags)) 24763847Seric goto testselfdestruct; 24857402Seric 24957402Seric if (m == InclMailer) 2504174Seric { 25157402Seric a->q_flags |= QDONTSEND; 25264761Seric if (a->q_alias == NULL) 2534174Seric { 25458680Seric a->q_flags |= QBADADDR; 25558151Seric usrerr("550 Cannot mail directly to :include:s"); 2564174Seric } 2574174Seric else 25850556Seric { 25959563Seric int ret; 26058247Seric 26158151Seric message("including file %s", a->q_user); 26259563Seric ret = include(a->q_user, FALSE, a, sendq, e); 26359563Seric if (transienterror(ret)) 26459563Seric { 26559563Seric #ifdef LOG 26659563Seric if (LogLevel > 2) 26759615Seric syslog(LOG_ERR, "%s: include %s: transient error: %e", 26859623Seric e->e_id, a->q_user, errstring(ret)); 26959563Seric #endif 27063853Seric a->q_flags |= QQUEUEUP; 27159563Seric usrerr("451 Cannot open %s: %s", 27259563Seric a->q_user, errstring(ret)); 27359563Seric } 27459563Seric else if (ret != 0) 27559563Seric { 27663938Seric a->q_flags |= QBADADDR; 27759563Seric usrerr("550 Cannot open %s: %s", 27859563Seric a->q_user, errstring(ret)); 27959563Seric } 28050556Seric } 2814174Seric } 28257642Seric else if (m == FileMailer) 2834174Seric { 2844329Seric struct stat stb; 2854329Seric extern bool writable(); 2864174Seric 28756795Seric p = strrchr(buf, '/'); 28851317Seric /* check if writable or creatable */ 28964761Seric if (a->q_alias == NULL) 2904174Seric { 29158680Seric a->q_flags |= QBADADDR; 29258151Seric usrerr("550 Cannot mail directly to files"); 2934174Seric } 29451317Seric else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) : 29564083Seric (*p = '\0', safefile(buf, RealUid, RealGid, NULL, TRUE, S_IWRITE|S_IEXEC) != 0)) 29651317Seric { 29758680Seric a->q_flags |= QBADADDR; 298*64771Seric giveresponse(EX_CANTCREAT, m, NULL, a->q_alias, e); 29951317Seric } 30051317Seric } 30151317Seric 30257402Seric if (m != LocalMailer) 30357642Seric { 30457642Seric if (!bitset(QDONTSEND, a->q_flags)) 30557642Seric e->e_nrcpts++; 30663847Seric goto testselfdestruct; 30757642Seric } 30857402Seric 30957402Seric /* try aliasing */ 31057402Seric alias(a, sendq, e); 31157402Seric 31257402Seric # ifdef USERDB 31357402Seric /* if not aliased, look it up in the user database */ 31458918Seric if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags)) 31557402Seric { 31657402Seric extern int udbexpand(); 31759615Seric extern int errno; 31857402Seric 31957402Seric if (udbexpand(a, sendq, e) == EX_TEMPFAIL) 32057402Seric { 32163853Seric a->q_flags |= QQUEUEUP; 32257402Seric if (e->e_message == NULL) 32357402Seric e->e_message = newstr("Deferred: user database error"); 32457402Seric # ifdef LOG 32558020Seric if (LogLevel > 8) 32659623Seric syslog(LOG_INFO, "%s: deferred: udbexpand: %s", 32759623Seric e->e_id, errstring(errno)); 32857402Seric # endif 32959615Seric message("queued (user database error): %s", 33059615Seric errstring(errno)); 33157642Seric e->e_nrcpts++; 33263847Seric goto testselfdestruct; 33357402Seric } 33457402Seric } 33557402Seric # endif 33657402Seric 33757402Seric /* if it was an alias or a UDB expansion, just return now */ 33858247Seric if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags)) 33963847Seric goto testselfdestruct; 34057402Seric 34151317Seric /* 34251317Seric ** If we have a level two config file, then pass the name through 34351317Seric ** Ruleset 5 before sending it off. Ruleset 5 has the right 34451317Seric ** to send rewrite it to another mailer. This gives us a hook 34551317Seric ** after local aliasing has been done. 34651317Seric */ 34751317Seric 34851317Seric if (tTd(29, 5)) 34951317Seric { 35051317Seric printf("recipient: testing local? cl=%d, rr5=%x\n\t", 35151317Seric ConfigLevel, RewriteRules[5]); 35251317Seric printaddr(a, FALSE); 35351317Seric } 35451317Seric if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 && 35551317Seric RewriteRules[5] != NULL) 35651317Seric { 35755012Seric maplocaluser(a, sendq, e); 35851317Seric } 35951317Seric 36051317Seric /* 36151317Seric ** If it didn't get rewritten to another mailer, go ahead 36251317Seric ** and deliver it. 36351317Seric */ 36451317Seric 36558247Seric if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags)) 36651317Seric { 36755354Seric auto bool fuzzy; 36851317Seric register struct passwd *pw; 36951317Seric extern struct passwd *finduser(); 37051317Seric 37151317Seric /* warning -- finduser may trash buf */ 37255354Seric pw = finduser(buf, &fuzzy); 37351317Seric if (pw == NULL) 37451317Seric { 37558680Seric a->q_flags |= QBADADDR; 376*64771Seric giveresponse(EX_NOUSER, m, NULL, a->q_alias, e); 37751317Seric } 3784174Seric else 3794174Seric { 38051317Seric char nbuf[MAXNAME]; 3814373Seric 38255354Seric if (fuzzy) 3834174Seric { 38453735Seric /* name was a fuzzy match */ 38551317Seric a->q_user = newstr(pw->pw_name); 38653735Seric if (findusercount++ > 3) 38753735Seric { 38858680Seric a->q_flags |= QBADADDR; 38958151Seric usrerr("554 aliasing/forwarding loop for %s broken", 39053735Seric pw->pw_name); 39153735Seric return (a); 39253735Seric } 39353735Seric 39453735Seric /* see if it aliases */ 39551317Seric (void) strcpy(buf, pw->pw_name); 39653735Seric goto trylocaluser; 3974174Seric } 39851317Seric a->q_home = newstr(pw->pw_dir); 39951317Seric a->q_uid = pw->pw_uid; 40051317Seric a->q_gid = pw->pw_gid; 40159083Seric a->q_ruser = newstr(pw->pw_name); 40251317Seric a->q_flags |= QGOODUID; 40351317Seric buildfname(pw->pw_gecos, pw->pw_name, nbuf); 40451317Seric if (nbuf[0] != '\0') 40551317Seric a->q_fullname = newstr(nbuf); 40651317Seric if (!quoted) 40755012Seric forward(a, sendq, e); 4084174Seric } 4094174Seric } 41057642Seric if (!bitset(QDONTSEND, a->q_flags)) 41157642Seric e->e_nrcpts++; 41263847Seric 41363847Seric testselfdestruct: 41463978Seric if (tTd(26, 8)) 41563847Seric { 41663978Seric printf("testselfdestruct: "); 41763978Seric printaddr(a, TRUE); 41863978Seric } 41963978Seric if (a->q_alias == NULL && a != &e->e_from && 42063978Seric bitset(QDONTSEND, a->q_flags)) 42163978Seric { 42263978Seric q = *sendq; 42363965Seric while (q != NULL && bitset(QDONTSEND, q->q_flags)) 42463847Seric q = q->q_next; 42563978Seric if (q == NULL) 42663847Seric { 42763847Seric a->q_flags |= QBADADDR; 42863847Seric usrerr("554 aliasing/forwarding loop broken"); 42963847Seric } 43063847Seric } 43112613Seric return (a); 4324174Seric } 4334174Seric /* 4344373Seric ** FINDUSER -- find the password entry for a user. 4354373Seric ** 4364373Seric ** This looks a lot like getpwnam, except that it may want to 4374373Seric ** do some fancier pattern matching in /etc/passwd. 4384373Seric ** 4399379Seric ** This routine contains most of the time of many sendmail runs. 4409379Seric ** It deserves to be optimized. 4419379Seric ** 4424373Seric ** Parameters: 4434373Seric ** name -- the name to match against. 44455354Seric ** fuzzyp -- an outarg that is set to TRUE if this entry 44555354Seric ** was found using the fuzzy matching algorithm; 44655354Seric ** set to FALSE otherwise. 4474373Seric ** 4484373Seric ** Returns: 4494373Seric ** A pointer to a pw struct. 4504373Seric ** NULL if name is unknown or ambiguous. 4514373Seric ** 4524373Seric ** Side Effects: 4534407Seric ** may modify name. 4544373Seric */ 4554373Seric 4564373Seric struct passwd * 45755354Seric finduser(name, fuzzyp) 4584373Seric char *name; 45955354Seric bool *fuzzyp; 4604373Seric { 4614376Seric register struct passwd *pw; 4624407Seric register char *p; 46315325Seric extern struct passwd *getpwent(); 46415325Seric extern struct passwd *getpwnam(); 4654373Seric 46655354Seric if (tTd(29, 4)) 46755354Seric printf("finduser(%s): ", name); 46855354Seric 46955354Seric *fuzzyp = FALSE; 4704407Seric 47164673Seric /* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */ 47264673Seric for (p = name; *p != '\0'; p++) 47364673Seric if (!isascii(*p) || !isdigit(*p)) 47464673Seric break; 47564673Seric if (*p == '\0') 47664673Seric { 47764673Seric if (tTd(29, 4)) 47864673Seric printf("failed (numeric input)\n"); 47964673Seric return NULL; 48064673Seric } 48164673Seric 48225777Seric /* look up this login name using fast path */ 48312634Seric if ((pw = getpwnam(name)) != NULL) 48455354Seric { 48555354Seric if (tTd(29, 4)) 48655354Seric printf("found (non-fuzzy)\n"); 48712634Seric return (pw); 48855354Seric } 48912634Seric 49053735Seric #ifdef MATCHGECOS 49153735Seric /* see if fuzzy matching allowed */ 49253735Seric if (!MatchGecos) 49355354Seric { 49455354Seric if (tTd(29, 4)) 49555354Seric printf("not found (fuzzy disabled)\n"); 49653735Seric return NULL; 49755354Seric } 49853735Seric 49912634Seric /* search for a matching full name instead */ 50025777Seric for (p = name; *p != '\0'; p++) 50125777Seric { 50225777Seric if (*p == (SpaceSub & 0177) || *p == '_') 50325777Seric *p = ' '; 50425777Seric } 50523107Seric (void) setpwent(); 5064376Seric while ((pw = getpwent()) != NULL) 5074376Seric { 5084998Seric char buf[MAXNAME]; 5094376Seric 5104998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 51156795Seric if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name)) 5124381Seric { 51355354Seric if (tTd(29, 4)) 51455354Seric printf("fuzzy matches %s\n", pw->pw_name); 51558151Seric message("sending to login name %s", pw->pw_name); 51655354Seric *fuzzyp = TRUE; 5174376Seric return (pw); 5184377Seric } 5194376Seric } 52055354Seric if (tTd(29, 4)) 52155354Seric printf("no fuzzy match found\n"); 52259015Seric #else 52359015Seric if (tTd(29, 4)) 52459015Seric printf("not found (fuzzy disabled)\n"); 52559015Seric #endif 5264376Seric return (NULL); 5274373Seric } 5284373Seric /* 5294329Seric ** WRITABLE -- predicate returning if the file is writable. 5304329Seric ** 5314329Seric ** This routine must duplicate the algorithm in sys/fio.c. 5324329Seric ** Unfortunately, we cannot use the access call since we 5334329Seric ** won't necessarily be the real uid when we try to 5344329Seric ** actually open the file. 5354329Seric ** 5364329Seric ** Notice that ANY file with ANY execute bit is automatically 5374329Seric ** not writable. This is also enforced by mailfile. 5384329Seric ** 5394329Seric ** Parameters: 5404329Seric ** s -- pointer to a stat struct for the file. 5414329Seric ** 5424329Seric ** Returns: 5434329Seric ** TRUE -- if we will be able to write this file. 5444329Seric ** FALSE -- if we cannot write this file. 5454329Seric ** 5464329Seric ** Side Effects: 5474329Seric ** none. 5484329Seric */ 5494329Seric 5504329Seric bool 5514329Seric writable(s) 5524329Seric register struct stat *s; 5534329Seric { 55455372Seric uid_t euid; 55555372Seric gid_t egid; 5564329Seric int bits; 5574329Seric 5584329Seric if (bitset(0111, s->st_mode)) 5594329Seric return (FALSE); 56063787Seric euid = RealUid; 56163787Seric egid = RealGid; 5624329Seric if (geteuid() == 0) 5634329Seric { 5644329Seric if (bitset(S_ISUID, s->st_mode)) 5654329Seric euid = s->st_uid; 5664329Seric if (bitset(S_ISGID, s->st_mode)) 5674329Seric egid = s->st_gid; 5684329Seric } 5694329Seric 5704329Seric if (euid == 0) 5714329Seric return (TRUE); 5724329Seric bits = S_IWRITE; 5734329Seric if (euid != s->st_uid) 5744329Seric { 5754329Seric bits >>= 3; 5764329Seric if (egid != s->st_gid) 5774329Seric bits >>= 3; 5784329Seric } 5794329Seric return ((s->st_mode & bits) != 0); 5804329Seric } 5814329Seric /* 5824174Seric ** INCLUDE -- handle :include: specification. 5834174Seric ** 5844174Seric ** Parameters: 5854174Seric ** fname -- filename to include. 58653037Seric ** forwarding -- if TRUE, we are reading a .forward file. 58753037Seric ** if FALSE, it's a :include: file. 5884399Seric ** ctladdr -- address template to use to fill in these 5894399Seric ** addresses -- effective user/group id are 5904399Seric ** the important things. 5915006Seric ** sendq -- a pointer to the head of the send queue 5925006Seric ** to put these addresses in. 5934174Seric ** 5944174Seric ** Returns: 59557136Seric ** open error status 5964174Seric ** 5974174Seric ** Side Effects: 5984174Seric ** reads the :include: file and sends to everyone 5994174Seric ** listed in that file. 6004174Seric */ 6014174Seric 60253037Seric static jmp_buf CtxIncludeTimeout; 60363937Seric static int includetimeout(); 60453037Seric 60557136Seric int 60655012Seric include(fname, forwarding, ctladdr, sendq, e) 6074174Seric char *fname; 60853037Seric bool forwarding; 6094399Seric ADDRESS *ctladdr; 6105006Seric ADDRESS **sendq; 61155012Seric ENVELOPE *e; 6124174Seric { 61364570Seric register FILE *fp = NULL; 61455012Seric char *oldto = e->e_to; 6159379Seric char *oldfilename = FileName; 6169379Seric int oldlinenumber = LineNumber; 61753037Seric register EVENT *ev = NULL; 61858082Seric int nincludes; 61964325Seric register ADDRESS *ca; 62064325Seric uid_t saveduid, uid; 62164325Seric gid_t savedgid, gid; 62264083Seric char *uname; 62364325Seric int rval = 0; 62453037Seric char buf[MAXLINE]; 6254174Seric 62657186Seric if (tTd(27, 2)) 62757186Seric printf("include(%s)\n", fname); 62863902Seric if (tTd(27, 4)) 62963902Seric printf(" ruid=%d euid=%d\n", getuid(), geteuid()); 63063581Seric if (tTd(27, 14)) 63163581Seric { 63263581Seric printf("ctladdr "); 63363581Seric printaddr(ctladdr, FALSE); 63463581Seric } 63557186Seric 63664325Seric if (tTd(27, 9)) 63764325Seric printf("include: old uid = %d/%d\n", getuid(), geteuid()); 63853037Seric 63963581Seric ca = getctladdr(ctladdr); 64063581Seric if (ca == NULL) 64164083Seric { 64263581Seric uid = 0; 64364083Seric gid = 0; 64464083Seric uname = NULL; 64564325Seric saveduid = -1; 64664083Seric } 64763581Seric else 64864083Seric { 64963581Seric uid = ca->q_uid; 65064083Seric gid = ca->q_gid; 65164083Seric uname = ca->q_user; 65264325Seric #ifdef HASSETREUID 65364325Seric saveduid = geteuid(); 65464325Seric savedgid = getegid(); 65564325Seric if (saveduid == 0) 65664325Seric { 65764325Seric initgroups(uname, gid); 65864325Seric if (uid != 0) 65964325Seric (void) setreuid(0, uid); 66064325Seric } 66164325Seric #endif 66264083Seric } 66363581Seric 66464325Seric if (tTd(27, 9)) 66564325Seric printf("include: new uid = %d/%d\n", getuid(), geteuid()); 66664325Seric 66764325Seric /* 66864325Seric ** If home directory is remote mounted but server is down, 66964325Seric ** this can hang or give errors; use a timeout to avoid this 67064325Seric */ 67164325Seric 67253037Seric if (setjmp(CtxIncludeTimeout) != 0) 67353037Seric { 67463853Seric ctladdr->q_flags |= QQUEUEUP; 67553037Seric errno = 0; 67653037Seric usrerr("451 open timeout on %s", fname); 67763993Seric 67863993Seric /* return pseudo-error code */ 67964325Seric rval = EOPENTIMEOUT; 68064325Seric goto resetuid; 68153037Seric } 68253037Seric ev = setevent((time_t) 60, includetimeout, 0); 68353037Seric 68463581Seric /* the input file must be marked safe */ 68564329Seric rval = safefile(fname, uid, gid, uname, forwarding, S_IREAD); 68664329Seric if (rval != 0) 68753037Seric { 68864325Seric /* don't use this :include: file */ 68953037Seric clrevent(ev); 69057186Seric if (tTd(27, 4)) 69158247Seric printf("include: not safe (uid=%d): %s\n", 69264329Seric uid, errstring(rval)); 69364329Seric goto resetuid; 69453037Seric } 69553037Seric 6964174Seric fp = fopen(fname, "r"); 6974174Seric if (fp == NULL) 6984174Seric { 69964329Seric rval = errno; 70063902Seric if (tTd(27, 4)) 70164329Seric printf("include: open: %s\n", errstring(rval)); 7024174Seric } 70364570Seric else if (ca == NULL) 7044406Seric { 7054406Seric struct stat st; 7064174Seric 7074406Seric if (fstat(fileno(fp), &st) < 0) 70858061Seric { 70964329Seric rval = errno; 7104406Seric syserr("Cannot fstat %s!", fname); 71158061Seric } 71264570Seric else 71364570Seric { 71464570Seric ctladdr->q_uid = st.st_uid; 71564570Seric ctladdr->q_gid = st.st_gid; 71664570Seric ctladdr->q_flags |= QGOODUID; 71764570Seric } 7184406Seric } 7194406Seric 72053037Seric clrevent(ev); 72153037Seric 72264570Seric resetuid: 72364570Seric 72464570Seric #ifdef HASSETREUID 72564570Seric if (saveduid == 0) 72664570Seric { 72764570Seric if (uid != 0) 72864570Seric if (setreuid(-1, 0) < 0 || setreuid(RealUid, 0) < 0) 72964570Seric syserr("setreuid(%d, 0) failure (real=%d, eff=%d)", 73064570Seric RealUid, getuid(), geteuid()); 73164570Seric setgid(savedgid); 73264570Seric } 73364570Seric #endif 73464570Seric 73564570Seric if (tTd(27, 9)) 73664570Seric printf("include: reset uid = %d/%d\n", getuid(), geteuid()); 73764570Seric 73864570Seric if (fp == NULL) 73964570Seric return rval; 74064570Seric 74158092Seric if (bitset(EF_VRFYONLY, e->e_flags)) 74258092Seric { 74358092Seric /* don't do any more now */ 74458868Seric ctladdr->q_flags |= QVERIFIED; 74558884Seric e->e_nrcpts++; 74658680Seric xfclose(fp, "include", fname); 74764570Seric return rval; 74858092Seric } 74958092Seric 7504174Seric /* read the file -- each line is a comma-separated list. */ 7519379Seric FileName = fname; 7529379Seric LineNumber = 0; 75358082Seric ctladdr->q_flags &= ~QSELFREF; 75458082Seric nincludes = 0; 7554174Seric while (fgets(buf, sizeof buf, fp) != NULL) 7564174Seric { 75756795Seric register char *p = strchr(buf, '\n'); 7584174Seric 75940963Sbostic LineNumber++; 7604174Seric if (p != NULL) 7614174Seric *p = '\0'; 76257186Seric if (buf[0] == '#' || buf[0] == '\0') 76357139Seric continue; 76458008Seric e->e_to = NULL; 76558151Seric message("%s to %s", 76653037Seric forwarding ? "forwarding" : "sending", buf); 76757977Seric #ifdef LOG 76858020Seric if (forwarding && LogLevel > 9) 76957977Seric syslog(LOG_INFO, "%s: forward %s => %s", 77057977Seric e->e_id, oldto, buf); 77157977Seric #endif 77257977Seric 7734176Seric AliasLevel++; 77458082Seric nincludes += sendtolist(buf, ctladdr, sendq, e); 7754176Seric AliasLevel--; 7764174Seric } 77763902Seric 77863902Seric if (ferror(fp) && tTd(27, 3)) 77963902Seric printf("include: read error: %s\n", errstring(errno)); 78058082Seric if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags)) 78158065Seric { 78258065Seric if (tTd(27, 5)) 78358065Seric { 78458065Seric printf("include: QDONTSEND "); 78558065Seric printaddr(ctladdr, FALSE); 78658065Seric } 78758065Seric ctladdr->q_flags |= QDONTSEND; 78858065Seric } 7894174Seric 79058680Seric (void) xfclose(fp, "include", fname); 7919379Seric FileName = oldfilename; 7929379Seric LineNumber = oldlinenumber; 79363847Seric e->e_to = oldto; 79464325Seric return rval; 7954174Seric } 79653037Seric 79753037Seric static 79853037Seric includetimeout() 79953037Seric { 80053037Seric longjmp(CtxIncludeTimeout, 1); 80153037Seric } 8024324Seric /* 8034324Seric ** SENDTOARGV -- send to an argument vector. 8044324Seric ** 8054324Seric ** Parameters: 8064324Seric ** argv -- argument vector to send to. 80758247Seric ** e -- the current envelope. 8084324Seric ** 8094324Seric ** Returns: 8104324Seric ** none. 8114324Seric ** 8124324Seric ** Side Effects: 8134324Seric ** puts all addresses on the argument vector onto the 8144324Seric ** send queue. 8154324Seric */ 8164324Seric 81755012Seric sendtoargv(argv, e) 8184324Seric register char **argv; 81955012Seric register ENVELOPE *e; 8204324Seric { 8214324Seric register char *p; 8224324Seric 8234324Seric while ((p = *argv++) != NULL) 8244324Seric { 82564284Seric (void) sendtolist(p, NULLADDR, &e->e_sendqueue, e); 8264324Seric } 8274324Seric } 8284399Seric /* 8294399Seric ** GETCTLADDR -- get controlling address from an address header. 8304399Seric ** 8314399Seric ** If none, get one corresponding to the effective userid. 8324399Seric ** 8334399Seric ** Parameters: 8344399Seric ** a -- the address to find the controller of. 8354399Seric ** 8364399Seric ** Returns: 8374399Seric ** the controlling address. 8384399Seric ** 8394399Seric ** Side Effects: 8404399Seric ** none. 8414399Seric */ 8424399Seric 8434399Seric ADDRESS * 8444399Seric getctladdr(a) 8454399Seric register ADDRESS *a; 8464399Seric { 8474404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 8484399Seric a = a->q_alias; 8494399Seric return (a); 8504399Seric } 851