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*68271Seric static char sccsid[] = "@(#)recipient.c 8.44.1.2 (Berkeley) 02/10/95"; 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 3968268Seric # define MAXRCRSN 10 404174Seric 4168268Seric 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; 53*68271Seric static char *bufp = NULL; 54*68271Seric static int buflen; 55*68271Seric char buf[MAXNAME + 1]; 564174Seric 5764131Seric if (list == NULL) 5864131Seric { 5964131Seric syserr("sendtolist: null list"); 6064131Seric return 0; 6164131Seric } 6264131Seric 637676Seric if (tTd(25, 1)) 644444Seric { 654444Seric printf("sendto: %s\n ctladdr=", list); 664444Seric printaddr(ctladdr, FALSE); 674444Seric } 684324Seric 698223Seric /* heuristic to determine old versus new style addresses */ 708230Seric if (ctladdr == NULL && 7156795Seric (strchr(list, ',') != NULL || strchr(list, ';') != NULL || 7256795Seric strchr(list, '<') != NULL || strchr(list, '(') != NULL)) 7355012Seric e->e_flags &= ~EF_OLDSTYLE; 7411446Seric delimiter = ' '; 7555012Seric if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL) 7611446Seric delimiter = ','; 778223Seric 784423Seric firstone = TRUE; 794324Seric al = NULL; 8058082Seric naddrs = 0; 818223Seric 82*68271Seric if (buf == NULL) 834174Seric { 84*68271Seric bufp = buf; 85*68271Seric buflen = sizeof buf - 1; 86*68271Seric } 87*68271Seric if (strlen(list) > buflen) 88*68271Seric { 89*68271Seric /* allocate additional space */ 90*68271Seric if (bufp != buf) 91*68271Seric free(bufp); 92*68271Seric buflen = strlen(list); 93*68271Seric bufp = malloc(buflen + 1); 94*68271Seric } 95*68271Seric strcpy(bufp, list); 96*68271Seric 97*68271Seric for (p = bufp; *p != '\0'; ) 98*68271Seric { 9958333Seric auto char *delimptr; 1008081Seric register ADDRESS *a; 1014319Seric 1028081Seric /* parse the address */ 10358050Seric while ((isascii(*p) && isspace(*p)) || *p == ',') 1044174Seric p++; 10564284Seric a = parseaddr(p, NULLADDR, RF_COPYALL, delimiter, &delimptr, e); 10658333Seric p = delimptr; 1079297Seric if (a == NULL) 1084174Seric continue; 1094324Seric a->q_next = al; 1104399Seric a->q_alias = ctladdr; 1114444Seric 1124444Seric /* see if this should be marked as a primary address */ 1134423Seric if (ctladdr == NULL || 1148081Seric (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags))) 1154423Seric a->q_flags |= QPRIMARY; 1164444Seric 11768268Seric if (ctladdr != NULL && sameaddr(ctladdr, a)) 11868268Seric ctladdr->q_flags |= QSELFREF; 11957731Seric al = a; 1204423Seric firstone = FALSE; 1214324Seric } 1224324Seric 1234324Seric /* arrange to send to everyone on the local send list */ 1244324Seric while (al != NULL) 1254324Seric { 1264324Seric register ADDRESS *a = al; 1274324Seric 1284324Seric al = a->q_next; 12968268Seric a = recipient(a, sendq, e); 13068268Seric 13168268Seric /* arrange to inherit full name */ 13268268Seric if (a->q_fullname == NULL && ctladdr != NULL) 13368268Seric a->q_fullname = ctladdr->q_fullname; 13458082Seric naddrs++; 1354174Seric } 1364324Seric 13763847Seric e->e_to = oldto; 13858082Seric return (naddrs); 1394174Seric } 1404174Seric /* 1414174Seric ** RECIPIENT -- Designate a message recipient 1424174Seric ** 1434174Seric ** Saves the named person for future mailing. 1444174Seric ** 1454174Seric ** Parameters: 1464174Seric ** a -- the (preparsed) address header for the recipient. 1475006Seric ** sendq -- a pointer to the head of a queue to put the 1485006Seric ** recipient in. Duplicate supression is done 1495006Seric ** in this queue. 15057731Seric ** e -- the current envelope. 1514174Seric ** 1524174Seric ** Returns: 15312613Seric ** The actual address in the queue. This will be "a" if 15412613Seric ** the address is not a duplicate, else the original address. 1554174Seric ** 1564174Seric ** Side Effects: 1574174Seric ** none. 1584174Seric */ 1594174Seric 16012613Seric ADDRESS * 16168268Seric recipient(a, sendq, e) 1624174Seric register ADDRESS *a; 1635006Seric register ADDRESS **sendq; 16455012Seric register ENVELOPE *e; 1654174Seric { 1664174Seric register ADDRESS *q; 1674319Seric ADDRESS **pq; 1684174Seric register struct mailer *m; 1699210Seric register char *p; 1709210Seric bool quoted = FALSE; /* set if the addr has a quote bit */ 17153735Seric int findusercount = 0; 17268268Seric char buf[MAXNAME]; /* unquoted image of the user name */ 17358247Seric extern int safefile(); 1744174Seric 17555012Seric e->e_to = a->q_paddr; 1764600Seric m = a->q_mailer; 1774174Seric errno = 0; 1787676Seric if (tTd(26, 1)) 1794444Seric { 1804444Seric printf("\nrecipient: "); 1814444Seric printaddr(a, FALSE); 1824444Seric } 1834174Seric 18464146Seric /* if this is primary, add it to the original recipient list */ 18564146Seric if (a->q_alias == NULL) 18664146Seric { 18764146Seric if (e->e_origrcpt == NULL) 18864146Seric e->e_origrcpt = a->q_paddr; 18964146Seric else if (e->e_origrcpt != a->q_paddr) 19064146Seric e->e_origrcpt = ""; 19164146Seric } 19264146Seric 1934174Seric /* break aliasing loops */ 19468268Seric if (AliasLevel > MAXRCRSN) 1954174Seric { 19668268Seric usrerr("554 aliasing/forwarding loop broken"); 19712613Seric return (a); 1984174Seric } 1994174Seric 2004174Seric /* 2014627Seric ** Finish setting up address structure. 2024174Seric */ 2034174Seric 20468268Seric /* set the queue timeout */ 20568268Seric a->q_timeout = TimeOuts.to_q_return; 20668268Seric 20716160Seric /* get unquoted user for file, program or user.name check */ 2089210Seric (void) strcpy(buf, a->q_user); 2099210Seric for (p = buf; *p != '\0' && !quoted; p++) 2109210Seric { 21154993Seric if (*p == '\\') 2129210Seric quoted = TRUE; 2139210Seric } 21454983Seric stripquotes(buf); 2159210Seric 21657402Seric /* check for direct mailing to restricted mailers */ 21765496Seric if (m == ProgMailer) 2184174Seric { 21965496Seric if (a->q_alias == NULL) 22065496Seric { 22165496Seric a->q_flags |= QBADADDR; 22265496Seric usrerr("550 Cannot mail directly to programs"); 22365496Seric } 22465496Seric else if (bitset(QBOGUSSHELL, a->q_alias->q_flags)) 22565496Seric { 22665496Seric a->q_flags |= QBADADDR; 22765496Seric usrerr("550 User %s@%s doesn't have a valid shell for mailing to programs", 22865496Seric a->q_alias->q_ruser, MyHostName); 22965496Seric } 23065496Seric else if (bitset(QUNSAFEADDR, a->q_alias->q_flags)) 23165496Seric { 23265496Seric a->q_flags |= QBADADDR; 23365496Seric usrerr("550 Address %s is unsafe for mailing to programs", 23465496Seric a->q_alias->q_paddr); 23565496Seric } 2364174Seric } 2374174Seric 2384174Seric /* 2394419Seric ** Look up this person in the recipient list. 2404419Seric ** If they are there already, return, otherwise continue. 2414419Seric ** If the list is empty, just add it. Notice the cute 2424419Seric ** hack to make from addresses suppress things correctly: 2434419Seric ** the QDONTSEND bit will be set in the send list. 2444419Seric ** [Please note: the emphasis is on "hack."] 2454174Seric */ 2464174Seric 2475006Seric for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 2484174Seric { 24958294Seric if (sameaddr(q, a)) 2504174Seric { 2517676Seric if (tTd(26, 1)) 2524444Seric { 2534444Seric printf("%s in sendq: ", a->q_paddr); 2544444Seric printaddr(q, FALSE); 2554444Seric } 25665593Seric if (!bitset(QPRIMARY, q->q_flags)) 25758065Seric { 25865593Seric if (!bitset(QDONTSEND, a->q_flags)) 25958151Seric message("duplicate suppressed"); 26065593Seric q->q_flags |= a->q_flags; 26165593Seric } 26265593Seric else if (bitset(QSELFREF, q->q_flags)) 26365579Seric q->q_flags |= a->q_flags & ~QDONTSEND; 26463847Seric a = q; 26568268Seric goto testselfdestruct; 2664174Seric } 2674319Seric } 2684174Seric 2694319Seric /* add address on list */ 27058884Seric *pq = a; 27158884Seric a->q_next = NULL; 2724174Seric 2734174Seric /* 27457402Seric ** Alias the name and handle special mailer types. 2754174Seric */ 2764174Seric 27753735Seric trylocaluser: 27855354Seric if (tTd(29, 7)) 27955354Seric printf("at trylocaluser %s\n", a->q_user); 28055354Seric 28158680Seric if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags)) 28263847Seric goto testselfdestruct; 28357402Seric 28457402Seric if (m == InclMailer) 2854174Seric { 28657402Seric a->q_flags |= QDONTSEND; 28764761Seric if (a->q_alias == NULL) 2884174Seric { 28958680Seric a->q_flags |= QBADADDR; 29058151Seric usrerr("550 Cannot mail directly to :include:s"); 2914174Seric } 2924174Seric else 29350556Seric { 29459563Seric int ret; 29558247Seric 29658151Seric message("including file %s", a->q_user); 29768268Seric ret = include(a->q_user, FALSE, a, sendq, e); 29859563Seric if (transienterror(ret)) 29959563Seric { 30059563Seric #ifdef LOG 30159563Seric if (LogLevel > 2) 30266239Seric syslog(LOG_ERR, "%s: include %s: transient error: %s", 30366284Seric e->e_id == NULL ? "NOQUEUE" : e->e_id, 30466284Seric a->q_user, errstring(ret)); 30559563Seric #endif 30663853Seric a->q_flags |= QQUEUEUP; 30765215Seric a->q_flags &= ~QDONTSEND; 30859563Seric usrerr("451 Cannot open %s: %s", 30959563Seric a->q_user, errstring(ret)); 31059563Seric } 31159563Seric else if (ret != 0) 31259563Seric { 31363938Seric a->q_flags |= QBADADDR; 31459563Seric usrerr("550 Cannot open %s: %s", 31559563Seric a->q_user, errstring(ret)); 31659563Seric } 31750556Seric } 3184174Seric } 31957642Seric else if (m == FileMailer) 3204174Seric { 3214329Seric extern bool writable(); 3224174Seric 32351317Seric /* check if writable or creatable */ 32464761Seric if (a->q_alias == NULL) 3254174Seric { 32658680Seric a->q_flags |= QBADADDR; 32758151Seric usrerr("550 Cannot mail directly to files"); 3284174Seric } 32965496Seric else if (bitset(QBOGUSSHELL, a->q_alias->q_flags)) 33065496Seric { 33165496Seric a->q_flags |= QBADADDR; 33265496Seric usrerr("550 User %s@%s doesn't have a valid shell for mailing to files", 33365496Seric a->q_alias->q_ruser, MyHostName); 33465496Seric } 33565496Seric else if (bitset(QUNSAFEADDR, a->q_alias->q_flags)) 33665496Seric { 33765496Seric a->q_flags |= QBADADDR; 33865496Seric usrerr("550 Address %s is unsafe for mailing to files", 33965496Seric a->q_alias->q_paddr); 34065496Seric } 34165112Seric else if (!writable(buf, getctladdr(a), SFF_ANYFILE)) 34251317Seric { 34358680Seric a->q_flags |= QBADADDR; 34468268Seric giveresponse(EX_CANTCREAT, m, NULL, a->q_alias, e); 34551317Seric } 34651317Seric } 34751317Seric 34868268Seric if (m != LocalMailer) 34968268Seric { 35068268Seric if (!bitset(QDONTSEND, a->q_flags)) 35168268Seric e->e_nrcpts++; 35268268Seric goto testselfdestruct; 35368268Seric } 35468268Seric 35557402Seric /* try aliasing */ 35668268Seric alias(a, sendq, e); 35757402Seric 35857402Seric # ifdef USERDB 35957402Seric /* if not aliased, look it up in the user database */ 36068268Seric if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags)) 36157402Seric { 36257402Seric extern int udbexpand(); 36357402Seric 36468268Seric if (udbexpand(a, sendq, e) == EX_TEMPFAIL) 36557402Seric { 36663853Seric a->q_flags |= QQUEUEUP; 36757402Seric if (e->e_message == NULL) 36857402Seric e->e_message = newstr("Deferred: user database error"); 36957402Seric # ifdef LOG 37058020Seric if (LogLevel > 8) 37159623Seric syslog(LOG_INFO, "%s: deferred: udbexpand: %s", 37266284Seric e->e_id == NULL ? "NOQUEUE" : e->e_id, 37366284Seric errstring(errno)); 37457402Seric # endif 37559615Seric message("queued (user database error): %s", 37659615Seric errstring(errno)); 37757642Seric e->e_nrcpts++; 37863847Seric goto testselfdestruct; 37957402Seric } 38057402Seric } 38157402Seric # endif 38257402Seric 38368268Seric /* if it was an alias or a UDB expansion, just return now */ 38468268Seric if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags)) 38568268Seric goto testselfdestruct; 38668268Seric 38751317Seric /* 38851317Seric ** If we have a level two config file, then pass the name through 38951317Seric ** Ruleset 5 before sending it off. Ruleset 5 has the right 39051317Seric ** to send rewrite it to another mailer. This gives us a hook 39151317Seric ** after local aliasing has been done. 39251317Seric */ 39351317Seric 39451317Seric if (tTd(29, 5)) 39551317Seric { 39651317Seric printf("recipient: testing local? cl=%d, rr5=%x\n\t", 39751317Seric ConfigLevel, RewriteRules[5]); 39851317Seric printaddr(a, FALSE); 39951317Seric } 40068268Seric if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 && 40168268Seric RewriteRules[5] != NULL) 40251317Seric { 40368268Seric maplocaluser(a, sendq, e); 40451317Seric } 40551317Seric 40651317Seric /* 40751317Seric ** If it didn't get rewritten to another mailer, go ahead 40851317Seric ** and deliver it. 40951317Seric */ 41051317Seric 41168268Seric if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags)) 41251317Seric { 41355354Seric auto bool fuzzy; 41451317Seric register struct passwd *pw; 41551317Seric extern struct passwd *finduser(); 41651317Seric 41751317Seric /* warning -- finduser may trash buf */ 41855354Seric pw = finduser(buf, &fuzzy); 41951317Seric if (pw == NULL) 42051317Seric { 42158680Seric a->q_flags |= QBADADDR; 42268268Seric giveresponse(EX_NOUSER, m, NULL, a->q_alias, e); 42351317Seric } 4244174Seric else 4254174Seric { 42651317Seric char nbuf[MAXNAME]; 4274373Seric 42855354Seric if (fuzzy) 4294174Seric { 43053735Seric /* name was a fuzzy match */ 43151317Seric a->q_user = newstr(pw->pw_name); 43253735Seric if (findusercount++ > 3) 43353735Seric { 43458680Seric a->q_flags |= QBADADDR; 43558151Seric usrerr("554 aliasing/forwarding loop for %s broken", 43653735Seric pw->pw_name); 43768268Seric return (a); 43853735Seric } 43953735Seric 44053735Seric /* see if it aliases */ 44151317Seric (void) strcpy(buf, pw->pw_name); 44253735Seric goto trylocaluser; 4434174Seric } 44465822Seric if (strcmp(pw->pw_dir, "/") == 0) 44565822Seric a->q_home = ""; 44665822Seric else 44765822Seric a->q_home = newstr(pw->pw_dir); 44851317Seric a->q_uid = pw->pw_uid; 44951317Seric a->q_gid = pw->pw_gid; 45059083Seric a->q_ruser = newstr(pw->pw_name); 45151317Seric a->q_flags |= QGOODUID; 45251317Seric buildfname(pw->pw_gecos, pw->pw_name, nbuf); 45351317Seric if (nbuf[0] != '\0') 45451317Seric a->q_fullname = newstr(nbuf); 45565211Seric if (pw->pw_shell != NULL && pw->pw_shell[0] != '\0' && 45665211Seric !usershellok(pw->pw_shell)) 45765206Seric { 45865211Seric a->q_flags |= QBOGUSSHELL; 45965206Seric } 46051317Seric if (!quoted) 46168268Seric forward(a, sendq, e); 4624174Seric } 4634174Seric } 46457642Seric if (!bitset(QDONTSEND, a->q_flags)) 46557642Seric e->e_nrcpts++; 46663847Seric 46763847Seric testselfdestruct: 46863978Seric if (tTd(26, 8)) 46963847Seric { 47063978Seric printf("testselfdestruct: "); 47163978Seric printaddr(a, TRUE); 47263978Seric } 47363978Seric if (a->q_alias == NULL && a != &e->e_from && 47463978Seric bitset(QDONTSEND, a->q_flags)) 47563978Seric { 47663978Seric q = *sendq; 47763965Seric while (q != NULL && bitset(QDONTSEND, q->q_flags)) 47863847Seric q = q->q_next; 47963978Seric if (q == NULL) 48063847Seric { 48163847Seric a->q_flags |= QBADADDR; 48263847Seric usrerr("554 aliasing/forwarding loop broken"); 48363847Seric } 48463847Seric } 48512613Seric return (a); 4864174Seric } 4874174Seric /* 4884373Seric ** FINDUSER -- find the password entry for a user. 4894373Seric ** 4904373Seric ** This looks a lot like getpwnam, except that it may want to 4914373Seric ** do some fancier pattern matching in /etc/passwd. 4924373Seric ** 4939379Seric ** This routine contains most of the time of many sendmail runs. 4949379Seric ** It deserves to be optimized. 4959379Seric ** 4964373Seric ** Parameters: 4974373Seric ** name -- the name to match against. 49855354Seric ** fuzzyp -- an outarg that is set to TRUE if this entry 49955354Seric ** was found using the fuzzy matching algorithm; 50055354Seric ** set to FALSE otherwise. 5014373Seric ** 5024373Seric ** Returns: 5034373Seric ** A pointer to a pw struct. 5044373Seric ** NULL if name is unknown or ambiguous. 5054373Seric ** 5064373Seric ** Side Effects: 5074407Seric ** may modify name. 5084373Seric */ 5094373Seric 5104373Seric struct passwd * 51155354Seric finduser(name, fuzzyp) 5124373Seric char *name; 51355354Seric bool *fuzzyp; 5144373Seric { 5154376Seric register struct passwd *pw; 5164407Seric register char *p; 51715325Seric extern struct passwd *getpwent(); 51815325Seric extern struct passwd *getpwnam(); 5194373Seric 52055354Seric if (tTd(29, 4)) 52155354Seric printf("finduser(%s): ", name); 52255354Seric 52355354Seric *fuzzyp = FALSE; 5244407Seric 52564673Seric /* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */ 52664673Seric for (p = name; *p != '\0'; p++) 52764673Seric if (!isascii(*p) || !isdigit(*p)) 52864673Seric break; 52964673Seric if (*p == '\0') 53064673Seric { 53164673Seric if (tTd(29, 4)) 53264673Seric printf("failed (numeric input)\n"); 53364673Seric return NULL; 53464673Seric } 53564673Seric 53625777Seric /* look up this login name using fast path */ 53712634Seric if ((pw = getpwnam(name)) != NULL) 53855354Seric { 53955354Seric if (tTd(29, 4)) 54055354Seric printf("found (non-fuzzy)\n"); 54112634Seric return (pw); 54255354Seric } 54312634Seric 54453735Seric #ifdef MATCHGECOS 54553735Seric /* see if fuzzy matching allowed */ 54653735Seric if (!MatchGecos) 54755354Seric { 54855354Seric if (tTd(29, 4)) 54955354Seric printf("not found (fuzzy disabled)\n"); 55053735Seric return NULL; 55155354Seric } 55253735Seric 55312634Seric /* search for a matching full name instead */ 55425777Seric for (p = name; *p != '\0'; p++) 55525777Seric { 55625777Seric if (*p == (SpaceSub & 0177) || *p == '_') 55725777Seric *p = ' '; 55825777Seric } 55923107Seric (void) setpwent(); 5604376Seric while ((pw = getpwent()) != NULL) 5614376Seric { 5624998Seric char buf[MAXNAME]; 5634376Seric 5644998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 56556795Seric if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name)) 5664381Seric { 56755354Seric if (tTd(29, 4)) 56855354Seric printf("fuzzy matches %s\n", pw->pw_name); 56958151Seric message("sending to login name %s", pw->pw_name); 57055354Seric *fuzzyp = TRUE; 5714376Seric return (pw); 5724377Seric } 5734376Seric } 57455354Seric if (tTd(29, 4)) 57555354Seric printf("no fuzzy match found\n"); 57659015Seric #else 57759015Seric if (tTd(29, 4)) 57859015Seric printf("not found (fuzzy disabled)\n"); 57959015Seric #endif 5804376Seric return (NULL); 5814373Seric } 5824373Seric /* 5834329Seric ** WRITABLE -- predicate returning if the file is writable. 5844329Seric ** 5854329Seric ** This routine must duplicate the algorithm in sys/fio.c. 5864329Seric ** Unfortunately, we cannot use the access call since we 5874329Seric ** won't necessarily be the real uid when we try to 5884329Seric ** actually open the file. 5894329Seric ** 5904329Seric ** Notice that ANY file with ANY execute bit is automatically 5914329Seric ** not writable. This is also enforced by mailfile. 5924329Seric ** 5934329Seric ** Parameters: 59465064Seric ** filename -- the file name to check. 59565112Seric ** ctladdr -- the controlling address for this file. 59665064Seric ** flags -- SFF_* flags to control the function. 5974329Seric ** 5984329Seric ** Returns: 5994329Seric ** TRUE -- if we will be able to write this file. 6004329Seric ** FALSE -- if we cannot write this file. 6014329Seric ** 6024329Seric ** Side Effects: 6034329Seric ** none. 6044329Seric */ 6054329Seric 6064329Seric bool 60765112Seric writable(filename, ctladdr, flags) 60864819Seric char *filename; 60965112Seric ADDRESS *ctladdr; 61065064Seric int flags; 6114329Seric { 61255372Seric uid_t euid; 61355372Seric gid_t egid; 6144329Seric int bits; 61564944Seric register char *p; 61664944Seric char *uname; 61764944Seric struct stat stb; 61864944Seric extern char RealUserName[]; 6194329Seric 62064819Seric if (tTd(29, 5)) 62165064Seric printf("writable(%s, %x)\n", filename, flags); 62264944Seric 62364944Seric #ifdef HASLSTAT 62465064Seric if ((bitset(SFF_NOSLINK, flags) ? lstat(filename, &stb) 62565064Seric : stat(filename, &stb)) < 0) 62664944Seric #else 62764944Seric if (stat(filename, &stb) < 0) 62864944Seric #endif 62964944Seric { 63064944Seric /* file does not exist -- see if directory is safe */ 63164944Seric p = strrchr(filename, '/'); 63264944Seric if (p == NULL) 63364944Seric { 63465067Seric errno = ENOTDIR; 63564944Seric return FALSE; 63664944Seric } 63765067Seric *p = '\0'; 63865067Seric errno = safefile(filename, RealUid, RealGid, RealUserName, 63965067Seric SFF_MUSTOWN, S_IWRITE|S_IEXEC); 64064944Seric *p = '/'; 64165067Seric return errno == 0; 64264944Seric } 64364944Seric 64465225Seric #ifdef SUID_ROOT_FILES_OK 64565225Seric /* really ought to be passed down -- and not a good idea */ 64665225Seric flags |= SFF_ROOTOK; 64765225Seric #endif 64865225Seric 64964944Seric /* 65064944Seric ** File does exist -- check that it is writable. 65164944Seric */ 65264944Seric 65364944Seric if (bitset(0111, stb.st_mode)) 65465022Seric { 65565022Seric if (tTd(29, 5)) 65665022Seric printf("failed (mode %o: x bits)\n", stb.st_mode); 65765067Seric errno = EPERM; 6584329Seric return (FALSE); 65965022Seric } 66064944Seric 66165112Seric if (ctladdr != NULL && geteuid() == 0) 66264944Seric { 66365112Seric euid = ctladdr->q_uid; 66465112Seric egid = ctladdr->q_gid; 66565112Seric uname = ctladdr->q_user; 66664944Seric } 66765112Seric else 66865112Seric { 66965112Seric euid = RealUid; 67065112Seric egid = RealGid; 67165112Seric uname = RealUserName; 67265112Seric } 67365138Seric if (euid == 0) 67465138Seric { 67565138Seric euid = DefUid; 67665138Seric uname = DefUser; 67765138Seric } 67865138Seric if (egid == 0) 67965138Seric egid = DefGid; 6804329Seric if (geteuid() == 0) 6814329Seric { 68265225Seric if (bitset(S_ISUID, stb.st_mode) && 68365225Seric (stb.st_uid != 0 || bitset(SFF_ROOTOK, flags))) 68464944Seric { 68564944Seric euid = stb.st_uid; 68664944Seric uname = NULL; 68764944Seric } 68865225Seric if (bitset(S_ISGID, stb.st_mode) && 68965225Seric (stb.st_gid != 0 || bitset(SFF_ROOTOK, flags))) 69064944Seric egid = stb.st_gid; 6914329Seric } 6924329Seric 69364819Seric if (tTd(29, 5)) 69464819Seric printf("\teu/gid=%d/%d, st_u/gid=%d/%d\n", 69564944Seric euid, egid, stb.st_uid, stb.st_gid); 69664819Seric 69765067Seric errno = safefile(filename, euid, egid, uname, flags, S_IWRITE); 69865067Seric return errno == 0; 6994329Seric } 7004329Seric /* 7014174Seric ** INCLUDE -- handle :include: specification. 7024174Seric ** 7034174Seric ** Parameters: 7044174Seric ** fname -- filename to include. 70553037Seric ** forwarding -- if TRUE, we are reading a .forward file. 70653037Seric ** if FALSE, it's a :include: file. 7074399Seric ** ctladdr -- address template to use to fill in these 7084399Seric ** addresses -- effective user/group id are 7094399Seric ** the important things. 7105006Seric ** sendq -- a pointer to the head of the send queue 7115006Seric ** to put these addresses in. 7124174Seric ** 7134174Seric ** Returns: 71457136Seric ** open error status 7154174Seric ** 7164174Seric ** Side Effects: 7174174Seric ** reads the :include: file and sends to everyone 7184174Seric ** listed in that file. 71965909Seric ** 72065909Seric ** Security Note: 72165909Seric ** If you have restricted chown (that is, you can't 72265909Seric ** give a file away), it is reasonable to allow programs 72365909Seric ** and files called from this :include: file to be to be 72465909Seric ** run as the owner of the :include: file. This is bogus 72565909Seric ** if there is any chance of someone giving away a file. 72665909Seric ** We assume that pre-POSIX systems can give away files. 72765909Seric ** 72865909Seric ** There is an additional restriction that if you 72965909Seric ** forward to a :include: file, it will not take on 73065909Seric ** the ownership of the :include: file. This may not 73165909Seric ** be necessary, but shouldn't hurt. 7324174Seric */ 7334174Seric 73453037Seric static jmp_buf CtxIncludeTimeout; 73563937Seric static int includetimeout(); 73653037Seric 73765496Seric #ifndef S_IWOTH 73865496Seric # define S_IWOTH (S_IWRITE >> 6) 73965496Seric #endif 74065496Seric 74157136Seric int 74268268Seric include(fname, forwarding, ctladdr, sendq, e) 7434174Seric char *fname; 74453037Seric bool forwarding; 7454399Seric ADDRESS *ctladdr; 7465006Seric ADDRESS **sendq; 74755012Seric ENVELOPE *e; 7484174Seric { 74964570Seric register FILE *fp = NULL; 75055012Seric char *oldto = e->e_to; 7519379Seric char *oldfilename = FileName; 7529379Seric int oldlinenumber = LineNumber; 75353037Seric register EVENT *ev = NULL; 75458082Seric int nincludes; 75564325Seric register ADDRESS *ca; 75664325Seric uid_t saveduid, uid; 75764325Seric gid_t savedgid, gid; 75864083Seric char *uname; 75964325Seric int rval = 0; 76065064Seric int sfflags = forwarding ? SFF_MUSTOWN : SFF_ANYFILE; 76165496Seric struct stat st; 76265948Seric char buf[MAXLINE]; 76365909Seric #ifdef _POSIX_CHOWN_RESTRICTED 76465948Seric # if _POSIX_CHOWN_RESTRICTED == -1 76565948Seric # define safechown FALSE 76665948Seric # else 76765948Seric # define safechown TRUE 76865948Seric # endif 76965948Seric #else 77065948Seric # ifdef _PC_CHOWN_RESTRICTED 77165909Seric bool safechown; 77265948Seric # else 77365948Seric # ifdef BSD 77465948Seric # define safechown TRUE 77565948Seric # else 77665948Seric # define safechown FALSE 77765948Seric # endif 77865948Seric # endif 77965909Seric #endif 78065948Seric extern bool chownsafe(); 7814174Seric 78257186Seric if (tTd(27, 2)) 78357186Seric printf("include(%s)\n", fname); 78463902Seric if (tTd(27, 4)) 78563902Seric printf(" ruid=%d euid=%d\n", getuid(), geteuid()); 78663581Seric if (tTd(27, 14)) 78763581Seric { 78863581Seric printf("ctladdr "); 78963581Seric printaddr(ctladdr, FALSE); 79063581Seric } 79157186Seric 79264325Seric if (tTd(27, 9)) 79364325Seric printf("include: old uid = %d/%d\n", getuid(), geteuid()); 79453037Seric 79563581Seric ca = getctladdr(ctladdr); 79663581Seric if (ca == NULL) 79764083Seric { 79864846Seric uid = DefUid; 79964846Seric gid = DefGid; 80064846Seric uname = DefUser; 80164325Seric saveduid = -1; 80264083Seric } 80363581Seric else 80464083Seric { 80563581Seric uid = ca->q_uid; 80664083Seric gid = ca->q_gid; 80764083Seric uname = ca->q_user; 80864325Seric #ifdef HASSETREUID 80964325Seric saveduid = geteuid(); 81064325Seric savedgid = getegid(); 81164325Seric if (saveduid == 0) 81264325Seric { 81364325Seric initgroups(uname, gid); 81464325Seric if (uid != 0) 81568268Seric (void) setreuid(0, uid); 81664325Seric } 81764325Seric #endif 81864083Seric } 81963581Seric 82064325Seric if (tTd(27, 9)) 82164325Seric printf("include: new uid = %d/%d\n", getuid(), geteuid()); 82264325Seric 82364325Seric /* 82464325Seric ** If home directory is remote mounted but server is down, 82564325Seric ** this can hang or give errors; use a timeout to avoid this 82664325Seric */ 82764325Seric 82853037Seric if (setjmp(CtxIncludeTimeout) != 0) 82953037Seric { 83063853Seric ctladdr->q_flags |= QQUEUEUP; 83153037Seric errno = 0; 83263993Seric 83363993Seric /* return pseudo-error code */ 83464325Seric rval = EOPENTIMEOUT; 83564325Seric goto resetuid; 83653037Seric } 83768268Seric ev = setevent((time_t) 60, includetimeout, 0); 83853037Seric 83963581Seric /* the input file must be marked safe */ 84064944Seric rval = safefile(fname, uid, gid, uname, sfflags, S_IREAD); 84164329Seric if (rval != 0) 84253037Seric { 84364325Seric /* don't use this :include: file */ 84457186Seric if (tTd(27, 4)) 84558247Seric printf("include: not safe (uid=%d): %s\n", 84664329Seric uid, errstring(rval)); 84753037Seric } 84865496Seric else 8494174Seric { 85065496Seric fp = fopen(fname, "r"); 85165496Seric if (fp == NULL) 85258061Seric { 85364329Seric rval = errno; 85465496Seric if (tTd(27, 4)) 85565496Seric printf("include: open: %s\n", errstring(rval)); 85658061Seric } 8574406Seric } 85868268Seric clrevent(ev); 85953037Seric 86064570Seric resetuid: 86164570Seric 86264570Seric #ifdef HASSETREUID 86364570Seric if (saveduid == 0) 86464570Seric { 86564570Seric if (uid != 0) 86668268Seric if (setreuid(-1, 0) < 0 || setreuid(RealUid, 0) < 0) 86764570Seric syserr("setreuid(%d, 0) failure (real=%d, eff=%d)", 86864570Seric RealUid, getuid(), geteuid()); 86964570Seric setgid(savedgid); 87064570Seric } 87164570Seric #endif 87264570Seric 87364570Seric if (tTd(27, 9)) 87464570Seric printf("include: reset uid = %d/%d\n", getuid(), geteuid()); 87564570Seric 87665593Seric if (rval == EOPENTIMEOUT) 87765593Seric usrerr("451 open timeout on %s", fname); 87865593Seric 87964570Seric if (fp == NULL) 88064570Seric return rval; 88164570Seric 88265496Seric if (fstat(fileno(fp), &st) < 0) 88365496Seric { 88465496Seric rval = errno; 88565496Seric syserr("Cannot fstat %s!", fname); 88665496Seric return rval; 88765496Seric } 88865496Seric 88965948Seric #ifndef safechown 89065948Seric safechown = chownsafe(fileno(fp)); 89165948Seric #endif 89265909Seric if (ca == NULL && safechown) 89365496Seric { 89465496Seric ctladdr->q_uid = st.st_uid; 89565496Seric ctladdr->q_gid = st.st_gid; 89665496Seric ctladdr->q_flags |= QGOODUID; 89765496Seric } 89865496Seric if (ca != NULL && ca->q_uid == st.st_uid) 89965496Seric { 90065496Seric /* optimization -- avoid getpwuid if we already have info */ 90165496Seric ctladdr->q_flags |= ca->q_flags & QBOGUSSHELL; 90265496Seric ctladdr->q_ruser = ca->q_ruser; 90365496Seric } 90465496Seric else 90565496Seric { 90668268Seric char *sh; 90765496Seric register struct passwd *pw; 90865496Seric 90968268Seric sh = "/SENDMAIL/ANY/SHELL/"; 91065496Seric pw = getpwuid(st.st_uid); 91168268Seric if (pw != NULL) 91268268Seric { 91368268Seric ctladdr->q_ruser = newstr(pw->pw_name); 91468268Seric if (safechown) 91568268Seric sh = pw->pw_shell; 91668268Seric } 91767940Seric if (pw == NULL) 91867940Seric ctladdr->q_flags |= QBOGUSSHELL; 91968268Seric else if(!usershellok(sh)) 92065496Seric { 92165909Seric if (safechown) 92268268Seric ctladdr->q_flags |= QBOGUSSHELL; 92365909Seric else 92468268Seric ctladdr->q_flags |= QUNSAFEADDR; 92565496Seric } 92665496Seric } 92765496Seric 92858092Seric if (bitset(EF_VRFYONLY, e->e_flags)) 92958092Seric { 93058092Seric /* don't do any more now */ 93158868Seric ctladdr->q_flags |= QVERIFIED; 93258884Seric e->e_nrcpts++; 93358680Seric xfclose(fp, "include", fname); 93464570Seric return rval; 93558092Seric } 93658092Seric 93765496Seric /* 93865496Seric ** Check to see if some bad guy can write this file 93965496Seric ** 94065496Seric ** This should really do something clever with group 94165496Seric ** permissions; currently we just view world writable 94265496Seric ** as unsafe. Also, we don't check for writable 94365496Seric ** directories in the path. We've got to leave 94465496Seric ** something for the local sysad to do. 94565496Seric */ 94665496Seric 94765496Seric if (bitset(S_IWOTH, st.st_mode)) 94865496Seric ctladdr->q_flags |= QUNSAFEADDR; 94965496Seric 9504174Seric /* read the file -- each line is a comma-separated list. */ 9519379Seric FileName = fname; 9529379Seric LineNumber = 0; 95358082Seric ctladdr->q_flags &= ~QSELFREF; 95458082Seric nincludes = 0; 9554174Seric while (fgets(buf, sizeof buf, fp) != NULL) 9564174Seric { 95756795Seric register char *p = strchr(buf, '\n'); 9584174Seric 95940963Sbostic LineNumber++; 9604174Seric if (p != NULL) 9614174Seric *p = '\0'; 96257186Seric if (buf[0] == '#' || buf[0] == '\0') 96357139Seric continue; 96458008Seric e->e_to = NULL; 96558151Seric message("%s to %s", 96653037Seric forwarding ? "forwarding" : "sending", buf); 96757977Seric #ifdef LOG 96858020Seric if (forwarding && LogLevel > 9) 96957977Seric syslog(LOG_INFO, "%s: forward %s => %s", 97066284Seric e->e_id == NULL ? "NOQUEUE" : e->e_id, 97166284Seric oldto, buf); 97257977Seric #endif 97357977Seric 97468268Seric AliasLevel++; 97568268Seric nincludes += sendtolist(buf, ctladdr, sendq, e); 97668268Seric AliasLevel--; 9774174Seric } 97863902Seric 97963902Seric if (ferror(fp) && tTd(27, 3)) 98063902Seric printf("include: read error: %s\n", errstring(errno)); 98158082Seric if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags)) 98258065Seric { 98358065Seric if (tTd(27, 5)) 98458065Seric { 98558065Seric printf("include: QDONTSEND "); 98658065Seric printaddr(ctladdr, FALSE); 98758065Seric } 98858065Seric ctladdr->q_flags |= QDONTSEND; 98958065Seric } 9904174Seric 99158680Seric (void) xfclose(fp, "include", fname); 9929379Seric FileName = oldfilename; 9939379Seric LineNumber = oldlinenumber; 99463847Seric e->e_to = oldto; 99564325Seric return rval; 9964174Seric } 99753037Seric 99853037Seric static 99953037Seric includetimeout() 100053037Seric { 100153037Seric longjmp(CtxIncludeTimeout, 1); 100253037Seric } 10034324Seric /* 10044324Seric ** SENDTOARGV -- send to an argument vector. 10054324Seric ** 10064324Seric ** Parameters: 10074324Seric ** argv -- argument vector to send to. 100858247Seric ** e -- the current envelope. 10094324Seric ** 10104324Seric ** Returns: 10114324Seric ** none. 10124324Seric ** 10134324Seric ** Side Effects: 10144324Seric ** puts all addresses on the argument vector onto the 10154324Seric ** send queue. 10164324Seric */ 10174324Seric 101855012Seric sendtoargv(argv, e) 10194324Seric register char **argv; 102055012Seric register ENVELOPE *e; 10214324Seric { 10224324Seric register char *p; 10234324Seric 10244324Seric while ((p = *argv++) != NULL) 10254324Seric { 102668268Seric (void) sendtolist(denlstring(p), NULLADDR, &e->e_sendqueue, e); 10274324Seric } 10284324Seric } 10294399Seric /* 10304399Seric ** GETCTLADDR -- get controlling address from an address header. 10314399Seric ** 10324399Seric ** If none, get one corresponding to the effective userid. 10334399Seric ** 10344399Seric ** Parameters: 10354399Seric ** a -- the address to find the controller of. 10364399Seric ** 10374399Seric ** Returns: 10384399Seric ** the controlling address. 10394399Seric ** 10404399Seric ** Side Effects: 10414399Seric ** none. 10424399Seric */ 10434399Seric 10444399Seric ADDRESS * 10454399Seric getctladdr(a) 10464399Seric register ADDRESS *a; 10474399Seric { 10484404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 10494399Seric a = a->q_alias; 10504399Seric return (a); 10514399Seric } 1052