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*66284Seric static char sccsid[] = "@(#)recipient.c 8.44 (Berkeley) 02/28/94"; 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 */ 19965496Seric if (m == ProgMailer) 2004174Seric { 20165496Seric if (a->q_alias == NULL) 20265496Seric { 20365496Seric a->q_flags |= QBADADDR; 20465496Seric usrerr("550 Cannot mail directly to programs"); 20565496Seric } 20665496Seric else if (bitset(QBOGUSSHELL, a->q_alias->q_flags)) 20765496Seric { 20865496Seric a->q_flags |= QBADADDR; 20965496Seric usrerr("550 User %s@%s doesn't have a valid shell for mailing to programs", 21065496Seric a->q_alias->q_ruser, MyHostName); 21165496Seric } 21265496Seric else if (bitset(QUNSAFEADDR, a->q_alias->q_flags)) 21365496Seric { 21465496Seric a->q_flags |= QBADADDR; 21565496Seric usrerr("550 Address %s is unsafe for mailing to programs", 21665496Seric a->q_alias->q_paddr); 21765496Seric } 2184174Seric } 2194174Seric 2204174Seric /* 2214419Seric ** Look up this person in the recipient list. 2224419Seric ** If they are there already, return, otherwise continue. 2234419Seric ** If the list is empty, just add it. Notice the cute 2244419Seric ** hack to make from addresses suppress things correctly: 2254419Seric ** the QDONTSEND bit will be set in the send list. 2264419Seric ** [Please note: the emphasis is on "hack."] 2274174Seric */ 2284174Seric 2295006Seric for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 2304174Seric { 23158294Seric if (sameaddr(q, a)) 2324174Seric { 2337676Seric if (tTd(26, 1)) 2344444Seric { 2354444Seric printf("%s in sendq: ", a->q_paddr); 2364444Seric printaddr(q, FALSE); 2374444Seric } 23865593Seric if (!bitset(QPRIMARY, q->q_flags)) 23958065Seric { 24065593Seric if (!bitset(QDONTSEND, a->q_flags)) 24158151Seric message("duplicate suppressed"); 24265593Seric q->q_flags |= a->q_flags; 24365593Seric } 24465593Seric else if (bitset(QSELFREF, q->q_flags)) 24565579Seric q->q_flags |= a->q_flags & ~QDONTSEND; 24663847Seric a = q; 24763847Seric goto testselfdestruct; 2484174Seric } 2494319Seric } 2504174Seric 2514319Seric /* add address on list */ 25258884Seric *pq = a; 25358884Seric a->q_next = NULL; 2544174Seric 2554174Seric /* 25657402Seric ** Alias the name and handle special mailer types. 2574174Seric */ 2584174Seric 25953735Seric trylocaluser: 26055354Seric if (tTd(29, 7)) 26155354Seric printf("at trylocaluser %s\n", a->q_user); 26255354Seric 26358680Seric if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags)) 26463847Seric goto testselfdestruct; 26557402Seric 26657402Seric if (m == InclMailer) 2674174Seric { 26857402Seric a->q_flags |= QDONTSEND; 26964761Seric if (a->q_alias == NULL) 2704174Seric { 27158680Seric a->q_flags |= QBADADDR; 27258151Seric usrerr("550 Cannot mail directly to :include:s"); 2734174Seric } 2744174Seric else 27550556Seric { 27659563Seric int ret; 27758247Seric 27858151Seric message("including file %s", a->q_user); 27959563Seric ret = include(a->q_user, FALSE, a, sendq, e); 28059563Seric if (transienterror(ret)) 28159563Seric { 28259563Seric #ifdef LOG 28359563Seric if (LogLevel > 2) 28466239Seric syslog(LOG_ERR, "%s: include %s: transient error: %s", 285*66284Seric e->e_id == NULL ? "NOQUEUE" : e->e_id, 286*66284Seric a->q_user, errstring(ret)); 28759563Seric #endif 28863853Seric a->q_flags |= QQUEUEUP; 28965215Seric a->q_flags &= ~QDONTSEND; 29059563Seric usrerr("451 Cannot open %s: %s", 29159563Seric a->q_user, errstring(ret)); 29259563Seric } 29359563Seric else if (ret != 0) 29459563Seric { 29563938Seric a->q_flags |= QBADADDR; 29659563Seric usrerr("550 Cannot open %s: %s", 29759563Seric a->q_user, errstring(ret)); 29859563Seric } 29950556Seric } 3004174Seric } 30157642Seric else if (m == FileMailer) 3024174Seric { 3034329Seric extern bool writable(); 3044174Seric 30551317Seric /* check if writable or creatable */ 30664761Seric if (a->q_alias == NULL) 3074174Seric { 30858680Seric a->q_flags |= QBADADDR; 30958151Seric usrerr("550 Cannot mail directly to files"); 3104174Seric } 31165496Seric else if (bitset(QBOGUSSHELL, a->q_alias->q_flags)) 31265496Seric { 31365496Seric a->q_flags |= QBADADDR; 31465496Seric usrerr("550 User %s@%s doesn't have a valid shell for mailing to files", 31565496Seric a->q_alias->q_ruser, MyHostName); 31665496Seric } 31765496Seric else if (bitset(QUNSAFEADDR, a->q_alias->q_flags)) 31865496Seric { 31965496Seric a->q_flags |= QBADADDR; 32065496Seric usrerr("550 Address %s is unsafe for mailing to files", 32165496Seric a->q_alias->q_paddr); 32265496Seric } 32365112Seric else if (!writable(buf, getctladdr(a), SFF_ANYFILE)) 32451317Seric { 32558680Seric a->q_flags |= QBADADDR; 32664771Seric giveresponse(EX_CANTCREAT, m, NULL, a->q_alias, e); 32751317Seric } 32851317Seric } 32951317Seric 33057402Seric if (m != LocalMailer) 33157642Seric { 33257642Seric if (!bitset(QDONTSEND, a->q_flags)) 33357642Seric e->e_nrcpts++; 33463847Seric goto testselfdestruct; 33557642Seric } 33657402Seric 33757402Seric /* try aliasing */ 33857402Seric alias(a, sendq, e); 33957402Seric 34057402Seric # ifdef USERDB 34157402Seric /* if not aliased, look it up in the user database */ 34258918Seric if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags)) 34357402Seric { 34457402Seric extern int udbexpand(); 34557402Seric 34657402Seric if (udbexpand(a, sendq, e) == EX_TEMPFAIL) 34757402Seric { 34863853Seric a->q_flags |= QQUEUEUP; 34957402Seric if (e->e_message == NULL) 35057402Seric e->e_message = newstr("Deferred: user database error"); 35157402Seric # ifdef LOG 35258020Seric if (LogLevel > 8) 35359623Seric syslog(LOG_INFO, "%s: deferred: udbexpand: %s", 354*66284Seric e->e_id == NULL ? "NOQUEUE" : e->e_id, 355*66284Seric errstring(errno)); 35657402Seric # endif 35759615Seric message("queued (user database error): %s", 35859615Seric errstring(errno)); 35957642Seric e->e_nrcpts++; 36063847Seric goto testselfdestruct; 36157402Seric } 36257402Seric } 36357402Seric # endif 36457402Seric 36557402Seric /* if it was an alias or a UDB expansion, just return now */ 36658247Seric if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags)) 36763847Seric goto testselfdestruct; 36857402Seric 36951317Seric /* 37051317Seric ** If we have a level two config file, then pass the name through 37151317Seric ** Ruleset 5 before sending it off. Ruleset 5 has the right 37251317Seric ** to send rewrite it to another mailer. This gives us a hook 37351317Seric ** after local aliasing has been done. 37451317Seric */ 37551317Seric 37651317Seric if (tTd(29, 5)) 37751317Seric { 37851317Seric printf("recipient: testing local? cl=%d, rr5=%x\n\t", 37951317Seric ConfigLevel, RewriteRules[5]); 38051317Seric printaddr(a, FALSE); 38151317Seric } 38251317Seric if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 && 38351317Seric RewriteRules[5] != NULL) 38451317Seric { 38555012Seric maplocaluser(a, sendq, e); 38651317Seric } 38751317Seric 38851317Seric /* 38951317Seric ** If it didn't get rewritten to another mailer, go ahead 39051317Seric ** and deliver it. 39151317Seric */ 39251317Seric 39358247Seric if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags)) 39451317Seric { 39555354Seric auto bool fuzzy; 39651317Seric register struct passwd *pw; 39751317Seric extern struct passwd *finduser(); 39851317Seric 39951317Seric /* warning -- finduser may trash buf */ 40055354Seric pw = finduser(buf, &fuzzy); 40151317Seric if (pw == NULL) 40251317Seric { 40358680Seric a->q_flags |= QBADADDR; 40464771Seric giveresponse(EX_NOUSER, m, NULL, a->q_alias, e); 40551317Seric } 4064174Seric else 4074174Seric { 40851317Seric char nbuf[MAXNAME]; 4094373Seric 41055354Seric if (fuzzy) 4114174Seric { 41253735Seric /* name was a fuzzy match */ 41351317Seric a->q_user = newstr(pw->pw_name); 41453735Seric if (findusercount++ > 3) 41553735Seric { 41658680Seric a->q_flags |= QBADADDR; 41758151Seric usrerr("554 aliasing/forwarding loop for %s broken", 41853735Seric pw->pw_name); 41953735Seric return (a); 42053735Seric } 42153735Seric 42253735Seric /* see if it aliases */ 42351317Seric (void) strcpy(buf, pw->pw_name); 42453735Seric goto trylocaluser; 4254174Seric } 42665822Seric if (strcmp(pw->pw_dir, "/") == 0) 42765822Seric a->q_home = ""; 42865822Seric else 42965822Seric a->q_home = newstr(pw->pw_dir); 43051317Seric a->q_uid = pw->pw_uid; 43151317Seric a->q_gid = pw->pw_gid; 43259083Seric a->q_ruser = newstr(pw->pw_name); 43351317Seric a->q_flags |= QGOODUID; 43451317Seric buildfname(pw->pw_gecos, pw->pw_name, nbuf); 43551317Seric if (nbuf[0] != '\0') 43651317Seric a->q_fullname = newstr(nbuf); 43765211Seric if (pw->pw_shell != NULL && pw->pw_shell[0] != '\0' && 43865211Seric !usershellok(pw->pw_shell)) 43965206Seric { 44065211Seric a->q_flags |= QBOGUSSHELL; 44165206Seric } 44251317Seric if (!quoted) 44355012Seric forward(a, sendq, e); 4444174Seric } 4454174Seric } 44657642Seric if (!bitset(QDONTSEND, a->q_flags)) 44757642Seric e->e_nrcpts++; 44863847Seric 44963847Seric testselfdestruct: 45063978Seric if (tTd(26, 8)) 45163847Seric { 45263978Seric printf("testselfdestruct: "); 45363978Seric printaddr(a, TRUE); 45463978Seric } 45563978Seric if (a->q_alias == NULL && a != &e->e_from && 45663978Seric bitset(QDONTSEND, a->q_flags)) 45763978Seric { 45863978Seric q = *sendq; 45963965Seric while (q != NULL && bitset(QDONTSEND, q->q_flags)) 46063847Seric q = q->q_next; 46163978Seric if (q == NULL) 46263847Seric { 46363847Seric a->q_flags |= QBADADDR; 46463847Seric usrerr("554 aliasing/forwarding loop broken"); 46563847Seric } 46663847Seric } 46712613Seric return (a); 4684174Seric } 4694174Seric /* 4704373Seric ** FINDUSER -- find the password entry for a user. 4714373Seric ** 4724373Seric ** This looks a lot like getpwnam, except that it may want to 4734373Seric ** do some fancier pattern matching in /etc/passwd. 4744373Seric ** 4759379Seric ** This routine contains most of the time of many sendmail runs. 4769379Seric ** It deserves to be optimized. 4779379Seric ** 4784373Seric ** Parameters: 4794373Seric ** name -- the name to match against. 48055354Seric ** fuzzyp -- an outarg that is set to TRUE if this entry 48155354Seric ** was found using the fuzzy matching algorithm; 48255354Seric ** set to FALSE otherwise. 4834373Seric ** 4844373Seric ** Returns: 4854373Seric ** A pointer to a pw struct. 4864373Seric ** NULL if name is unknown or ambiguous. 4874373Seric ** 4884373Seric ** Side Effects: 4894407Seric ** may modify name. 4904373Seric */ 4914373Seric 4924373Seric struct passwd * 49355354Seric finduser(name, fuzzyp) 4944373Seric char *name; 49555354Seric bool *fuzzyp; 4964373Seric { 4974376Seric register struct passwd *pw; 4984407Seric register char *p; 49915325Seric extern struct passwd *getpwent(); 50015325Seric extern struct passwd *getpwnam(); 5014373Seric 50255354Seric if (tTd(29, 4)) 50355354Seric printf("finduser(%s): ", name); 50455354Seric 50555354Seric *fuzzyp = FALSE; 5064407Seric 50764673Seric /* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */ 50864673Seric for (p = name; *p != '\0'; p++) 50964673Seric if (!isascii(*p) || !isdigit(*p)) 51064673Seric break; 51164673Seric if (*p == '\0') 51264673Seric { 51364673Seric if (tTd(29, 4)) 51464673Seric printf("failed (numeric input)\n"); 51564673Seric return NULL; 51664673Seric } 51764673Seric 51825777Seric /* look up this login name using fast path */ 51912634Seric if ((pw = getpwnam(name)) != NULL) 52055354Seric { 52155354Seric if (tTd(29, 4)) 52255354Seric printf("found (non-fuzzy)\n"); 52312634Seric return (pw); 52455354Seric } 52512634Seric 52653735Seric #ifdef MATCHGECOS 52753735Seric /* see if fuzzy matching allowed */ 52853735Seric if (!MatchGecos) 52955354Seric { 53055354Seric if (tTd(29, 4)) 53155354Seric printf("not found (fuzzy disabled)\n"); 53253735Seric return NULL; 53355354Seric } 53453735Seric 53512634Seric /* search for a matching full name instead */ 53625777Seric for (p = name; *p != '\0'; p++) 53725777Seric { 53825777Seric if (*p == (SpaceSub & 0177) || *p == '_') 53925777Seric *p = ' '; 54025777Seric } 54123107Seric (void) setpwent(); 5424376Seric while ((pw = getpwent()) != NULL) 5434376Seric { 5444998Seric char buf[MAXNAME]; 5454376Seric 5464998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 54756795Seric if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name)) 5484381Seric { 54955354Seric if (tTd(29, 4)) 55055354Seric printf("fuzzy matches %s\n", pw->pw_name); 55158151Seric message("sending to login name %s", pw->pw_name); 55255354Seric *fuzzyp = TRUE; 5534376Seric return (pw); 5544377Seric } 5554376Seric } 55655354Seric if (tTd(29, 4)) 55755354Seric printf("no fuzzy match found\n"); 55859015Seric #else 55959015Seric if (tTd(29, 4)) 56059015Seric printf("not found (fuzzy disabled)\n"); 56159015Seric #endif 5624376Seric return (NULL); 5634373Seric } 5644373Seric /* 5654329Seric ** WRITABLE -- predicate returning if the file is writable. 5664329Seric ** 5674329Seric ** This routine must duplicate the algorithm in sys/fio.c. 5684329Seric ** Unfortunately, we cannot use the access call since we 5694329Seric ** won't necessarily be the real uid when we try to 5704329Seric ** actually open the file. 5714329Seric ** 5724329Seric ** Notice that ANY file with ANY execute bit is automatically 5734329Seric ** not writable. This is also enforced by mailfile. 5744329Seric ** 5754329Seric ** Parameters: 57665064Seric ** filename -- the file name to check. 57765112Seric ** ctladdr -- the controlling address for this file. 57865064Seric ** flags -- SFF_* flags to control the function. 5794329Seric ** 5804329Seric ** Returns: 5814329Seric ** TRUE -- if we will be able to write this file. 5824329Seric ** FALSE -- if we cannot write this file. 5834329Seric ** 5844329Seric ** Side Effects: 5854329Seric ** none. 5864329Seric */ 5874329Seric 5884329Seric bool 58965112Seric writable(filename, ctladdr, flags) 59064819Seric char *filename; 59165112Seric ADDRESS *ctladdr; 59265064Seric int flags; 5934329Seric { 59455372Seric uid_t euid; 59555372Seric gid_t egid; 5964329Seric int bits; 59764944Seric register char *p; 59864944Seric char *uname; 59964944Seric struct stat stb; 60064944Seric extern char RealUserName[]; 6014329Seric 60264819Seric if (tTd(29, 5)) 60365064Seric printf("writable(%s, %x)\n", filename, flags); 60464944Seric 60564944Seric #ifdef HASLSTAT 60665064Seric if ((bitset(SFF_NOSLINK, flags) ? lstat(filename, &stb) 60765064Seric : stat(filename, &stb)) < 0) 60864944Seric #else 60964944Seric if (stat(filename, &stb) < 0) 61064944Seric #endif 61164944Seric { 61264944Seric /* file does not exist -- see if directory is safe */ 61364944Seric p = strrchr(filename, '/'); 61464944Seric if (p == NULL) 61564944Seric { 61665067Seric errno = ENOTDIR; 61764944Seric return FALSE; 61864944Seric } 61965067Seric *p = '\0'; 62065067Seric errno = safefile(filename, RealUid, RealGid, RealUserName, 62165067Seric SFF_MUSTOWN, S_IWRITE|S_IEXEC); 62264944Seric *p = '/'; 62365067Seric return errno == 0; 62464944Seric } 62564944Seric 62665225Seric #ifdef SUID_ROOT_FILES_OK 62765225Seric /* really ought to be passed down -- and not a good idea */ 62865225Seric flags |= SFF_ROOTOK; 62965225Seric #endif 63065225Seric 63164944Seric /* 63264944Seric ** File does exist -- check that it is writable. 63364944Seric */ 63464944Seric 63564944Seric if (bitset(0111, stb.st_mode)) 63665022Seric { 63765022Seric if (tTd(29, 5)) 63865022Seric printf("failed (mode %o: x bits)\n", stb.st_mode); 63965067Seric errno = EPERM; 6404329Seric return (FALSE); 64165022Seric } 64264944Seric 64365112Seric if (ctladdr != NULL && geteuid() == 0) 64464944Seric { 64565112Seric euid = ctladdr->q_uid; 64665112Seric egid = ctladdr->q_gid; 64765112Seric uname = ctladdr->q_user; 64864944Seric } 64965112Seric else 65065112Seric { 65165112Seric euid = RealUid; 65265112Seric egid = RealGid; 65365112Seric uname = RealUserName; 65465112Seric } 65565138Seric if (euid == 0) 65665138Seric { 65765138Seric euid = DefUid; 65865138Seric uname = DefUser; 65965138Seric } 66065138Seric if (egid == 0) 66165138Seric egid = DefGid; 6624329Seric if (geteuid() == 0) 6634329Seric { 66465225Seric if (bitset(S_ISUID, stb.st_mode) && 66565225Seric (stb.st_uid != 0 || bitset(SFF_ROOTOK, flags))) 66664944Seric { 66764944Seric euid = stb.st_uid; 66864944Seric uname = NULL; 66964944Seric } 67065225Seric if (bitset(S_ISGID, stb.st_mode) && 67165225Seric (stb.st_gid != 0 || bitset(SFF_ROOTOK, flags))) 67264944Seric egid = stb.st_gid; 6734329Seric } 6744329Seric 67564819Seric if (tTd(29, 5)) 67664819Seric printf("\teu/gid=%d/%d, st_u/gid=%d/%d\n", 67764944Seric euid, egid, stb.st_uid, stb.st_gid); 67864819Seric 67965067Seric errno = safefile(filename, euid, egid, uname, flags, S_IWRITE); 68065067Seric return errno == 0; 6814329Seric } 6824329Seric /* 6834174Seric ** INCLUDE -- handle :include: specification. 6844174Seric ** 6854174Seric ** Parameters: 6864174Seric ** fname -- filename to include. 68753037Seric ** forwarding -- if TRUE, we are reading a .forward file. 68853037Seric ** if FALSE, it's a :include: file. 6894399Seric ** ctladdr -- address template to use to fill in these 6904399Seric ** addresses -- effective user/group id are 6914399Seric ** the important things. 6925006Seric ** sendq -- a pointer to the head of the send queue 6935006Seric ** to put these addresses in. 6944174Seric ** 6954174Seric ** Returns: 69657136Seric ** open error status 6974174Seric ** 6984174Seric ** Side Effects: 6994174Seric ** reads the :include: file and sends to everyone 7004174Seric ** listed in that file. 70165909Seric ** 70265909Seric ** Security Note: 70365909Seric ** If you have restricted chown (that is, you can't 70465909Seric ** give a file away), it is reasonable to allow programs 70565909Seric ** and files called from this :include: file to be to be 70665909Seric ** run as the owner of the :include: file. This is bogus 70765909Seric ** if there is any chance of someone giving away a file. 70865909Seric ** We assume that pre-POSIX systems can give away files. 70965909Seric ** 71065909Seric ** There is an additional restriction that if you 71165909Seric ** forward to a :include: file, it will not take on 71265909Seric ** the ownership of the :include: file. This may not 71365909Seric ** be necessary, but shouldn't hurt. 7144174Seric */ 7154174Seric 71653037Seric static jmp_buf CtxIncludeTimeout; 71763937Seric static int includetimeout(); 71853037Seric 71965496Seric #ifndef S_IWOTH 72065496Seric # define S_IWOTH (S_IWRITE >> 6) 72165496Seric #endif 72265496Seric 72357136Seric int 72455012Seric include(fname, forwarding, ctladdr, sendq, e) 7254174Seric char *fname; 72653037Seric bool forwarding; 7274399Seric ADDRESS *ctladdr; 7285006Seric ADDRESS **sendq; 72955012Seric ENVELOPE *e; 7304174Seric { 73164570Seric register FILE *fp = NULL; 73255012Seric char *oldto = e->e_to; 7339379Seric char *oldfilename = FileName; 7349379Seric int oldlinenumber = LineNumber; 73553037Seric register EVENT *ev = NULL; 73658082Seric int nincludes; 73764325Seric register ADDRESS *ca; 73864325Seric uid_t saveduid, uid; 73964325Seric gid_t savedgid, gid; 74064083Seric char *uname; 74164325Seric int rval = 0; 74265064Seric int sfflags = forwarding ? SFF_MUSTOWN : SFF_ANYFILE; 74365496Seric struct stat st; 74465948Seric char buf[MAXLINE]; 74565909Seric #ifdef _POSIX_CHOWN_RESTRICTED 74665948Seric # if _POSIX_CHOWN_RESTRICTED == -1 74765948Seric # define safechown FALSE 74865948Seric # else 74965948Seric # define safechown TRUE 75065948Seric # endif 75165948Seric #else 75265948Seric # ifdef _PC_CHOWN_RESTRICTED 75365909Seric bool safechown; 75465948Seric # else 75565948Seric # ifdef BSD 75665948Seric # define safechown TRUE 75765948Seric # else 75865948Seric # define safechown FALSE 75965948Seric # endif 76065948Seric # endif 76165909Seric #endif 76265948Seric extern bool chownsafe(); 7634174Seric 76457186Seric if (tTd(27, 2)) 76557186Seric printf("include(%s)\n", fname); 76663902Seric if (tTd(27, 4)) 76763902Seric printf(" ruid=%d euid=%d\n", getuid(), geteuid()); 76863581Seric if (tTd(27, 14)) 76963581Seric { 77063581Seric printf("ctladdr "); 77163581Seric printaddr(ctladdr, FALSE); 77263581Seric } 77357186Seric 77464325Seric if (tTd(27, 9)) 77564325Seric printf("include: old uid = %d/%d\n", getuid(), geteuid()); 77653037Seric 77763581Seric ca = getctladdr(ctladdr); 77863581Seric if (ca == NULL) 77964083Seric { 78064846Seric uid = DefUid; 78164846Seric gid = DefGid; 78264846Seric uname = DefUser; 78364325Seric saveduid = -1; 78464083Seric } 78563581Seric else 78664083Seric { 78763581Seric uid = ca->q_uid; 78864083Seric gid = ca->q_gid; 78964083Seric uname = ca->q_user; 79064325Seric #ifdef HASSETREUID 79164325Seric saveduid = geteuid(); 79264325Seric savedgid = getegid(); 79364325Seric if (saveduid == 0) 79464325Seric { 79564325Seric initgroups(uname, gid); 79664325Seric if (uid != 0) 79764325Seric (void) setreuid(0, uid); 79864325Seric } 79964325Seric #endif 80064083Seric } 80163581Seric 80264325Seric if (tTd(27, 9)) 80364325Seric printf("include: new uid = %d/%d\n", getuid(), geteuid()); 80464325Seric 80564325Seric /* 80664325Seric ** If home directory is remote mounted but server is down, 80764325Seric ** this can hang or give errors; use a timeout to avoid this 80864325Seric */ 80964325Seric 81053037Seric if (setjmp(CtxIncludeTimeout) != 0) 81153037Seric { 81263853Seric ctladdr->q_flags |= QQUEUEUP; 81353037Seric errno = 0; 81463993Seric 81563993Seric /* return pseudo-error code */ 81664325Seric rval = EOPENTIMEOUT; 81764325Seric goto resetuid; 81853037Seric } 81953037Seric ev = setevent((time_t) 60, includetimeout, 0); 82053037Seric 82163581Seric /* the input file must be marked safe */ 82264944Seric rval = safefile(fname, uid, gid, uname, sfflags, S_IREAD); 82364329Seric if (rval != 0) 82453037Seric { 82564325Seric /* don't use this :include: file */ 82657186Seric if (tTd(27, 4)) 82758247Seric printf("include: not safe (uid=%d): %s\n", 82864329Seric uid, errstring(rval)); 82953037Seric } 83065496Seric else 8314174Seric { 83265496Seric fp = fopen(fname, "r"); 83365496Seric if (fp == NULL) 83458061Seric { 83564329Seric rval = errno; 83665496Seric if (tTd(27, 4)) 83765496Seric printf("include: open: %s\n", errstring(rval)); 83858061Seric } 8394406Seric } 84053037Seric clrevent(ev); 84153037Seric 84264570Seric resetuid: 84364570Seric 84464570Seric #ifdef HASSETREUID 84564570Seric if (saveduid == 0) 84664570Seric { 84764570Seric if (uid != 0) 84864570Seric if (setreuid(-1, 0) < 0 || setreuid(RealUid, 0) < 0) 84964570Seric syserr("setreuid(%d, 0) failure (real=%d, eff=%d)", 85064570Seric RealUid, getuid(), geteuid()); 85164570Seric setgid(savedgid); 85264570Seric } 85364570Seric #endif 85464570Seric 85564570Seric if (tTd(27, 9)) 85664570Seric printf("include: reset uid = %d/%d\n", getuid(), geteuid()); 85764570Seric 85865593Seric if (rval == EOPENTIMEOUT) 85965593Seric usrerr("451 open timeout on %s", fname); 86065593Seric 86164570Seric if (fp == NULL) 86264570Seric return rval; 86364570Seric 86465496Seric if (fstat(fileno(fp), &st) < 0) 86565496Seric { 86665496Seric rval = errno; 86765496Seric syserr("Cannot fstat %s!", fname); 86865496Seric return rval; 86965496Seric } 87065496Seric 87165948Seric #ifndef safechown 87265948Seric safechown = chownsafe(fileno(fp)); 87365948Seric #endif 87465909Seric if (ca == NULL && safechown) 87565496Seric { 87665496Seric ctladdr->q_uid = st.st_uid; 87765496Seric ctladdr->q_gid = st.st_gid; 87865496Seric ctladdr->q_flags |= QGOODUID; 87965496Seric } 88065496Seric if (ca != NULL && ca->q_uid == st.st_uid) 88165496Seric { 88265496Seric /* optimization -- avoid getpwuid if we already have info */ 88365496Seric ctladdr->q_flags |= ca->q_flags & QBOGUSSHELL; 88465496Seric ctladdr->q_ruser = ca->q_ruser; 88565496Seric } 88665496Seric else 88765496Seric { 88865909Seric char *sh; 88965496Seric register struct passwd *pw; 89065496Seric 89165909Seric sh = "/SENDMAIL/ANY/SHELL/"; 89265496Seric pw = getpwuid(st.st_uid); 89365909Seric if (pw != NULL) 89465496Seric { 89565496Seric ctladdr->q_ruser = newstr(pw->pw_name); 89665909Seric if (safechown) 89765909Seric sh = pw->pw_shell; 89865909Seric } 89965909Seric if (pw == NULL) 90065496Seric ctladdr->q_flags |= QBOGUSSHELL; 90165909Seric else if(!usershellok(sh)) 90265909Seric { 90365909Seric if (safechown) 90465909Seric ctladdr->q_flags |= QBOGUSSHELL; 90565909Seric else 90665909Seric ctladdr->q_flags |= QUNSAFEADDR; 90765496Seric } 90865496Seric } 90965496Seric 91058092Seric if (bitset(EF_VRFYONLY, e->e_flags)) 91158092Seric { 91258092Seric /* don't do any more now */ 91358868Seric ctladdr->q_flags |= QVERIFIED; 91458884Seric e->e_nrcpts++; 91558680Seric xfclose(fp, "include", fname); 91664570Seric return rval; 91758092Seric } 91858092Seric 91965496Seric /* 92065496Seric ** Check to see if some bad guy can write this file 92165496Seric ** 92265496Seric ** This should really do something clever with group 92365496Seric ** permissions; currently we just view world writable 92465496Seric ** as unsafe. Also, we don't check for writable 92565496Seric ** directories in the path. We've got to leave 92665496Seric ** something for the local sysad to do. 92765496Seric */ 92865496Seric 92965496Seric if (bitset(S_IWOTH, st.st_mode)) 93065496Seric ctladdr->q_flags |= QUNSAFEADDR; 93165496Seric 9324174Seric /* read the file -- each line is a comma-separated list. */ 9339379Seric FileName = fname; 9349379Seric LineNumber = 0; 93558082Seric ctladdr->q_flags &= ~QSELFREF; 93658082Seric nincludes = 0; 9374174Seric while (fgets(buf, sizeof buf, fp) != NULL) 9384174Seric { 93956795Seric register char *p = strchr(buf, '\n'); 9404174Seric 94140963Sbostic LineNumber++; 9424174Seric if (p != NULL) 9434174Seric *p = '\0'; 94457186Seric if (buf[0] == '#' || buf[0] == '\0') 94557139Seric continue; 94658008Seric e->e_to = NULL; 94758151Seric message("%s to %s", 94853037Seric forwarding ? "forwarding" : "sending", buf); 94957977Seric #ifdef LOG 95058020Seric if (forwarding && LogLevel > 9) 95157977Seric syslog(LOG_INFO, "%s: forward %s => %s", 952*66284Seric e->e_id == NULL ? "NOQUEUE" : e->e_id, 953*66284Seric oldto, buf); 95457977Seric #endif 95557977Seric 9564176Seric AliasLevel++; 95758082Seric nincludes += sendtolist(buf, ctladdr, sendq, e); 9584176Seric AliasLevel--; 9594174Seric } 96063902Seric 96163902Seric if (ferror(fp) && tTd(27, 3)) 96263902Seric printf("include: read error: %s\n", errstring(errno)); 96358082Seric if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags)) 96458065Seric { 96558065Seric if (tTd(27, 5)) 96658065Seric { 96758065Seric printf("include: QDONTSEND "); 96858065Seric printaddr(ctladdr, FALSE); 96958065Seric } 97058065Seric ctladdr->q_flags |= QDONTSEND; 97158065Seric } 9724174Seric 97358680Seric (void) xfclose(fp, "include", fname); 9749379Seric FileName = oldfilename; 9759379Seric LineNumber = oldlinenumber; 97663847Seric e->e_to = oldto; 97764325Seric return rval; 9784174Seric } 97953037Seric 98053037Seric static 98153037Seric includetimeout() 98253037Seric { 98353037Seric longjmp(CtxIncludeTimeout, 1); 98453037Seric } 9854324Seric /* 9864324Seric ** SENDTOARGV -- send to an argument vector. 9874324Seric ** 9884324Seric ** Parameters: 9894324Seric ** argv -- argument vector to send to. 99058247Seric ** e -- the current envelope. 9914324Seric ** 9924324Seric ** Returns: 9934324Seric ** none. 9944324Seric ** 9954324Seric ** Side Effects: 9964324Seric ** puts all addresses on the argument vector onto the 9974324Seric ** send queue. 9984324Seric */ 9994324Seric 100055012Seric sendtoargv(argv, e) 10014324Seric register char **argv; 100255012Seric register ENVELOPE *e; 10034324Seric { 10044324Seric register char *p; 10054324Seric 10064324Seric while ((p = *argv++) != NULL) 10074324Seric { 100864284Seric (void) sendtolist(p, NULLADDR, &e->e_sendqueue, e); 10094324Seric } 10104324Seric } 10114399Seric /* 10124399Seric ** GETCTLADDR -- get controlling address from an address header. 10134399Seric ** 10144399Seric ** If none, get one corresponding to the effective userid. 10154399Seric ** 10164399Seric ** Parameters: 10174399Seric ** a -- the address to find the controller of. 10184399Seric ** 10194399Seric ** Returns: 10204399Seric ** the controlling address. 10214399Seric ** 10224399Seric ** Side Effects: 10234399Seric ** none. 10244399Seric */ 10254399Seric 10264399Seric ADDRESS * 10274399Seric getctladdr(a) 10284399Seric register ADDRESS *a; 10294399Seric { 10304404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 10314399Seric a = a->q_alias; 10324399Seric return (a); 10334399Seric } 1034