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*65206Seric static char sccsid[] = "@(#)recipient.c 8.31 (Berkeley) 12/24/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 extern bool writable(); 2854174Seric 28651317Seric /* check if writable or creatable */ 28764761Seric if (a->q_alias == NULL) 2884174Seric { 28958680Seric a->q_flags |= QBADADDR; 29058151Seric usrerr("550 Cannot mail directly to files"); 2914174Seric } 29265112Seric else if (!writable(buf, getctladdr(a), SFF_ANYFILE)) 29351317Seric { 29458680Seric a->q_flags |= QBADADDR; 29564771Seric giveresponse(EX_CANTCREAT, m, NULL, a->q_alias, e); 29651317Seric } 29751317Seric } 29851317Seric 29957402Seric if (m != LocalMailer) 30057642Seric { 30157642Seric if (!bitset(QDONTSEND, a->q_flags)) 30257642Seric e->e_nrcpts++; 30363847Seric goto testselfdestruct; 30457642Seric } 30557402Seric 30657402Seric /* try aliasing */ 30757402Seric alias(a, sendq, e); 30857402Seric 30957402Seric # ifdef USERDB 31057402Seric /* if not aliased, look it up in the user database */ 31158918Seric if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags)) 31257402Seric { 31357402Seric extern int udbexpand(); 31457402Seric 31557402Seric if (udbexpand(a, sendq, e) == EX_TEMPFAIL) 31657402Seric { 31763853Seric a->q_flags |= QQUEUEUP; 31857402Seric if (e->e_message == NULL) 31957402Seric e->e_message = newstr("Deferred: user database error"); 32057402Seric # ifdef LOG 32158020Seric if (LogLevel > 8) 32259623Seric syslog(LOG_INFO, "%s: deferred: udbexpand: %s", 32359623Seric e->e_id, errstring(errno)); 32457402Seric # endif 32559615Seric message("queued (user database error): %s", 32659615Seric errstring(errno)); 32757642Seric e->e_nrcpts++; 32863847Seric goto testselfdestruct; 32957402Seric } 33057402Seric } 33157402Seric # endif 33257402Seric 33357402Seric /* if it was an alias or a UDB expansion, just return now */ 33458247Seric if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags)) 33563847Seric goto testselfdestruct; 33657402Seric 33751317Seric /* 33851317Seric ** If we have a level two config file, then pass the name through 33951317Seric ** Ruleset 5 before sending it off. Ruleset 5 has the right 34051317Seric ** to send rewrite it to another mailer. This gives us a hook 34151317Seric ** after local aliasing has been done. 34251317Seric */ 34351317Seric 34451317Seric if (tTd(29, 5)) 34551317Seric { 34651317Seric printf("recipient: testing local? cl=%d, rr5=%x\n\t", 34751317Seric ConfigLevel, RewriteRules[5]); 34851317Seric printaddr(a, FALSE); 34951317Seric } 35051317Seric if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 && 35151317Seric RewriteRules[5] != NULL) 35251317Seric { 35355012Seric maplocaluser(a, sendq, e); 35451317Seric } 35551317Seric 35651317Seric /* 35751317Seric ** If it didn't get rewritten to another mailer, go ahead 35851317Seric ** and deliver it. 35951317Seric */ 36051317Seric 36158247Seric if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags)) 36251317Seric { 36355354Seric auto bool fuzzy; 36451317Seric register struct passwd *pw; 36551317Seric extern struct passwd *finduser(); 36651317Seric 36751317Seric /* warning -- finduser may trash buf */ 36855354Seric pw = finduser(buf, &fuzzy); 36951317Seric if (pw == NULL) 37051317Seric { 37158680Seric a->q_flags |= QBADADDR; 37264771Seric giveresponse(EX_NOUSER, m, NULL, a->q_alias, e); 37351317Seric } 3744174Seric else 3754174Seric { 37651317Seric char nbuf[MAXNAME]; 3774373Seric 37855354Seric if (fuzzy) 3794174Seric { 38053735Seric /* name was a fuzzy match */ 38151317Seric a->q_user = newstr(pw->pw_name); 38253735Seric if (findusercount++ > 3) 38353735Seric { 38458680Seric a->q_flags |= QBADADDR; 38558151Seric usrerr("554 aliasing/forwarding loop for %s broken", 38653735Seric pw->pw_name); 38753735Seric return (a); 38853735Seric } 38953735Seric 39053735Seric /* see if it aliases */ 39151317Seric (void) strcpy(buf, pw->pw_name); 39253735Seric goto trylocaluser; 3934174Seric } 39451317Seric a->q_home = newstr(pw->pw_dir); 39551317Seric a->q_uid = pw->pw_uid; 39651317Seric a->q_gid = pw->pw_gid; 39759083Seric a->q_ruser = newstr(pw->pw_name); 39851317Seric a->q_flags |= QGOODUID; 39951317Seric buildfname(pw->pw_gecos, pw->pw_name, nbuf); 40051317Seric if (nbuf[0] != '\0') 40151317Seric a->q_fullname = newstr(nbuf); 402*65206Seric #ifndef NEEDGETUSERSHELL 403*65206Seric if (pw->pw_shell != NULL && pw->pw_shell[0] != '\0') 404*65206Seric { 405*65206Seric extern char *getusershell(); 406*65206Seric 407*65206Seric setusershell(); 408*65206Seric while ((p = getusershell()) != NULL) 409*65206Seric if (strcmp(p, pw->pw_shell) == 0) 410*65206Seric break; 411*65206Seric endusershell(); 412*65206Seric if (p == NULL) 413*65206Seric a->q_flags |= QBOGUSSHELL; 414*65206Seric } 415*65206Seric #endif 41651317Seric if (!quoted) 41755012Seric forward(a, sendq, e); 4184174Seric } 4194174Seric } 42057642Seric if (!bitset(QDONTSEND, a->q_flags)) 42157642Seric e->e_nrcpts++; 42263847Seric 42363847Seric testselfdestruct: 42463978Seric if (tTd(26, 8)) 42563847Seric { 42663978Seric printf("testselfdestruct: "); 42763978Seric printaddr(a, TRUE); 42863978Seric } 42963978Seric if (a->q_alias == NULL && a != &e->e_from && 43063978Seric bitset(QDONTSEND, a->q_flags)) 43163978Seric { 43263978Seric q = *sendq; 43363965Seric while (q != NULL && bitset(QDONTSEND, q->q_flags)) 43463847Seric q = q->q_next; 43563978Seric if (q == NULL) 43663847Seric { 43763847Seric a->q_flags |= QBADADDR; 43863847Seric usrerr("554 aliasing/forwarding loop broken"); 43963847Seric } 44063847Seric } 44112613Seric return (a); 4424174Seric } 4434174Seric /* 4444373Seric ** FINDUSER -- find the password entry for a user. 4454373Seric ** 4464373Seric ** This looks a lot like getpwnam, except that it may want to 4474373Seric ** do some fancier pattern matching in /etc/passwd. 4484373Seric ** 4499379Seric ** This routine contains most of the time of many sendmail runs. 4509379Seric ** It deserves to be optimized. 4519379Seric ** 4524373Seric ** Parameters: 4534373Seric ** name -- the name to match against. 45455354Seric ** fuzzyp -- an outarg that is set to TRUE if this entry 45555354Seric ** was found using the fuzzy matching algorithm; 45655354Seric ** set to FALSE otherwise. 4574373Seric ** 4584373Seric ** Returns: 4594373Seric ** A pointer to a pw struct. 4604373Seric ** NULL if name is unknown or ambiguous. 4614373Seric ** 4624373Seric ** Side Effects: 4634407Seric ** may modify name. 4644373Seric */ 4654373Seric 4664373Seric struct passwd * 46755354Seric finduser(name, fuzzyp) 4684373Seric char *name; 46955354Seric bool *fuzzyp; 4704373Seric { 4714376Seric register struct passwd *pw; 4724407Seric register char *p; 47315325Seric extern struct passwd *getpwent(); 47415325Seric extern struct passwd *getpwnam(); 4754373Seric 47655354Seric if (tTd(29, 4)) 47755354Seric printf("finduser(%s): ", name); 47855354Seric 47955354Seric *fuzzyp = FALSE; 4804407Seric 48164673Seric /* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */ 48264673Seric for (p = name; *p != '\0'; p++) 48364673Seric if (!isascii(*p) || !isdigit(*p)) 48464673Seric break; 48564673Seric if (*p == '\0') 48664673Seric { 48764673Seric if (tTd(29, 4)) 48864673Seric printf("failed (numeric input)\n"); 48964673Seric return NULL; 49064673Seric } 49164673Seric 49225777Seric /* look up this login name using fast path */ 49312634Seric if ((pw = getpwnam(name)) != NULL) 49455354Seric { 49555354Seric if (tTd(29, 4)) 49655354Seric printf("found (non-fuzzy)\n"); 49712634Seric return (pw); 49855354Seric } 49912634Seric 50053735Seric #ifdef MATCHGECOS 50153735Seric /* see if fuzzy matching allowed */ 50253735Seric if (!MatchGecos) 50355354Seric { 50455354Seric if (tTd(29, 4)) 50555354Seric printf("not found (fuzzy disabled)\n"); 50653735Seric return NULL; 50755354Seric } 50853735Seric 50912634Seric /* search for a matching full name instead */ 51025777Seric for (p = name; *p != '\0'; p++) 51125777Seric { 51225777Seric if (*p == (SpaceSub & 0177) || *p == '_') 51325777Seric *p = ' '; 51425777Seric } 51523107Seric (void) setpwent(); 5164376Seric while ((pw = getpwent()) != NULL) 5174376Seric { 5184998Seric char buf[MAXNAME]; 5194376Seric 5204998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 52156795Seric if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name)) 5224381Seric { 52355354Seric if (tTd(29, 4)) 52455354Seric printf("fuzzy matches %s\n", pw->pw_name); 52558151Seric message("sending to login name %s", pw->pw_name); 52655354Seric *fuzzyp = TRUE; 5274376Seric return (pw); 5284377Seric } 5294376Seric } 53055354Seric if (tTd(29, 4)) 53155354Seric printf("no fuzzy match found\n"); 53259015Seric #else 53359015Seric if (tTd(29, 4)) 53459015Seric printf("not found (fuzzy disabled)\n"); 53559015Seric #endif 5364376Seric return (NULL); 5374373Seric } 5384373Seric /* 5394329Seric ** WRITABLE -- predicate returning if the file is writable. 5404329Seric ** 5414329Seric ** This routine must duplicate the algorithm in sys/fio.c. 5424329Seric ** Unfortunately, we cannot use the access call since we 5434329Seric ** won't necessarily be the real uid when we try to 5444329Seric ** actually open the file. 5454329Seric ** 5464329Seric ** Notice that ANY file with ANY execute bit is automatically 5474329Seric ** not writable. This is also enforced by mailfile. 5484329Seric ** 5494329Seric ** Parameters: 55065064Seric ** filename -- the file name to check. 55165112Seric ** ctladdr -- the controlling address for this file. 55265064Seric ** flags -- SFF_* flags to control the function. 5534329Seric ** 5544329Seric ** Returns: 5554329Seric ** TRUE -- if we will be able to write this file. 5564329Seric ** FALSE -- if we cannot write this file. 5574329Seric ** 5584329Seric ** Side Effects: 5594329Seric ** none. 5604329Seric */ 5614329Seric 5624329Seric bool 56365112Seric writable(filename, ctladdr, flags) 56464819Seric char *filename; 56565112Seric ADDRESS *ctladdr; 56665064Seric int flags; 5674329Seric { 56855372Seric uid_t euid; 56955372Seric gid_t egid; 5704329Seric int bits; 57164944Seric register char *p; 57264944Seric char *uname; 57364944Seric struct stat stb; 57464944Seric extern char RealUserName[]; 5754329Seric 57664819Seric if (tTd(29, 5)) 57765064Seric printf("writable(%s, %x)\n", filename, flags); 57864944Seric 57964944Seric #ifdef HASLSTAT 58065064Seric if ((bitset(SFF_NOSLINK, flags) ? lstat(filename, &stb) 58165064Seric : stat(filename, &stb)) < 0) 58264944Seric #else 58364944Seric if (stat(filename, &stb) < 0) 58464944Seric #endif 58564944Seric { 58664944Seric /* file does not exist -- see if directory is safe */ 58764944Seric p = strrchr(filename, '/'); 58864944Seric if (p == NULL) 58964944Seric { 59065067Seric errno = ENOTDIR; 59164944Seric return FALSE; 59264944Seric } 59365067Seric *p = '\0'; 59465067Seric errno = safefile(filename, RealUid, RealGid, RealUserName, 59565067Seric SFF_MUSTOWN, S_IWRITE|S_IEXEC); 59664944Seric *p = '/'; 59765067Seric return errno == 0; 59864944Seric } 59964944Seric 60064944Seric /* 60164944Seric ** File does exist -- check that it is writable. 60264944Seric */ 60364944Seric 60464944Seric if (bitset(0111, stb.st_mode)) 60565022Seric { 60665022Seric if (tTd(29, 5)) 60765022Seric printf("failed (mode %o: x bits)\n", stb.st_mode); 60865067Seric errno = EPERM; 6094329Seric return (FALSE); 61065022Seric } 61164944Seric 61265112Seric if (ctladdr != NULL && geteuid() == 0) 61364944Seric { 61465112Seric euid = ctladdr->q_uid; 61565112Seric egid = ctladdr->q_gid; 61665112Seric uname = ctladdr->q_user; 61764944Seric } 61865112Seric else 61965112Seric { 62065112Seric euid = RealUid; 62165112Seric egid = RealGid; 62265112Seric uname = RealUserName; 62365112Seric } 62465138Seric if (euid == 0) 62565138Seric { 62665138Seric euid = DefUid; 62765138Seric uname = DefUser; 62865138Seric } 62965138Seric if (egid == 0) 63065138Seric egid = DefGid; 6314329Seric if (geteuid() == 0) 6324329Seric { 63365138Seric #ifdef SUID_ROOT_FILES_OK 63464944Seric if (bitset(S_ISUID, stb.st_mode)) 63565138Seric #else 63665138Seric if (bitset(S_ISUID, stb.st_mode) && stb.st_uid != 0) 63765138Seric #endif 63864944Seric { 63965138Seric flags |= SFF_ROOTOK; 64064944Seric euid = stb.st_uid; 64164944Seric uname = NULL; 64264944Seric } 64365138Seric if (bitset(S_ISGID, stb.st_mode) && stb.st_gid != 0) 64464944Seric egid = stb.st_gid; 6454329Seric } 6464329Seric 64764819Seric if (tTd(29, 5)) 64864819Seric printf("\teu/gid=%d/%d, st_u/gid=%d/%d\n", 64964944Seric euid, egid, stb.st_uid, stb.st_gid); 65064819Seric 65165067Seric errno = safefile(filename, euid, egid, uname, flags, S_IWRITE); 65265067Seric return errno == 0; 6534329Seric } 6544329Seric /* 6554174Seric ** INCLUDE -- handle :include: specification. 6564174Seric ** 6574174Seric ** Parameters: 6584174Seric ** fname -- filename to include. 65953037Seric ** forwarding -- if TRUE, we are reading a .forward file. 66053037Seric ** if FALSE, it's a :include: file. 6614399Seric ** ctladdr -- address template to use to fill in these 6624399Seric ** addresses -- effective user/group id are 6634399Seric ** the important things. 6645006Seric ** sendq -- a pointer to the head of the send queue 6655006Seric ** to put these addresses in. 6664174Seric ** 6674174Seric ** Returns: 66857136Seric ** open error status 6694174Seric ** 6704174Seric ** Side Effects: 6714174Seric ** reads the :include: file and sends to everyone 6724174Seric ** listed in that file. 6734174Seric */ 6744174Seric 67553037Seric static jmp_buf CtxIncludeTimeout; 67663937Seric static int includetimeout(); 67753037Seric 67857136Seric int 67955012Seric include(fname, forwarding, ctladdr, sendq, e) 6804174Seric char *fname; 68153037Seric bool forwarding; 6824399Seric ADDRESS *ctladdr; 6835006Seric ADDRESS **sendq; 68455012Seric ENVELOPE *e; 6854174Seric { 68664570Seric register FILE *fp = NULL; 68755012Seric char *oldto = e->e_to; 6889379Seric char *oldfilename = FileName; 6899379Seric int oldlinenumber = LineNumber; 69053037Seric register EVENT *ev = NULL; 69158082Seric int nincludes; 69264325Seric register ADDRESS *ca; 69364325Seric uid_t saveduid, uid; 69464325Seric gid_t savedgid, gid; 69564083Seric char *uname; 69664325Seric int rval = 0; 69765064Seric int sfflags = forwarding ? SFF_MUSTOWN : SFF_ANYFILE; 69853037Seric char buf[MAXLINE]; 6994174Seric 70057186Seric if (tTd(27, 2)) 70157186Seric printf("include(%s)\n", fname); 70263902Seric if (tTd(27, 4)) 70363902Seric printf(" ruid=%d euid=%d\n", getuid(), geteuid()); 70463581Seric if (tTd(27, 14)) 70563581Seric { 70663581Seric printf("ctladdr "); 70763581Seric printaddr(ctladdr, FALSE); 70863581Seric } 70957186Seric 71064325Seric if (tTd(27, 9)) 71164325Seric printf("include: old uid = %d/%d\n", getuid(), geteuid()); 71253037Seric 71363581Seric ca = getctladdr(ctladdr); 71463581Seric if (ca == NULL) 71564083Seric { 71664846Seric uid = DefUid; 71764846Seric gid = DefGid; 71864846Seric uname = DefUser; 71964325Seric saveduid = -1; 72064083Seric } 72163581Seric else 72264083Seric { 72363581Seric uid = ca->q_uid; 72464083Seric gid = ca->q_gid; 72564083Seric uname = ca->q_user; 72664325Seric #ifdef HASSETREUID 72764325Seric saveduid = geteuid(); 72864325Seric savedgid = getegid(); 72964325Seric if (saveduid == 0) 73064325Seric { 73164325Seric initgroups(uname, gid); 73264325Seric if (uid != 0) 73364325Seric (void) setreuid(0, uid); 73464325Seric } 73564325Seric #endif 73664083Seric } 73763581Seric 73864325Seric if (tTd(27, 9)) 73964325Seric printf("include: new uid = %d/%d\n", getuid(), geteuid()); 74064325Seric 74164325Seric /* 74264325Seric ** If home directory is remote mounted but server is down, 74364325Seric ** this can hang or give errors; use a timeout to avoid this 74464325Seric */ 74564325Seric 74653037Seric if (setjmp(CtxIncludeTimeout) != 0) 74753037Seric { 74863853Seric ctladdr->q_flags |= QQUEUEUP; 74953037Seric errno = 0; 75053037Seric usrerr("451 open timeout on %s", fname); 75163993Seric 75263993Seric /* return pseudo-error code */ 75364325Seric rval = EOPENTIMEOUT; 75464325Seric goto resetuid; 75553037Seric } 75653037Seric ev = setevent((time_t) 60, includetimeout, 0); 75753037Seric 75863581Seric /* the input file must be marked safe */ 75964944Seric rval = safefile(fname, uid, gid, uname, sfflags, S_IREAD); 76064329Seric if (rval != 0) 76153037Seric { 76264325Seric /* don't use this :include: file */ 76353037Seric clrevent(ev); 76457186Seric if (tTd(27, 4)) 76558247Seric printf("include: not safe (uid=%d): %s\n", 76664329Seric uid, errstring(rval)); 76764329Seric goto resetuid; 76853037Seric } 76953037Seric 7704174Seric fp = fopen(fname, "r"); 7714174Seric if (fp == NULL) 7724174Seric { 77364329Seric rval = errno; 77463902Seric if (tTd(27, 4)) 77564329Seric printf("include: open: %s\n", errstring(rval)); 7764174Seric } 77764570Seric else if (ca == NULL) 7784406Seric { 7794406Seric struct stat st; 7804174Seric 7814406Seric if (fstat(fileno(fp), &st) < 0) 78258061Seric { 78364329Seric rval = errno; 7844406Seric syserr("Cannot fstat %s!", fname); 78558061Seric } 78664570Seric else 78764570Seric { 78864570Seric ctladdr->q_uid = st.st_uid; 78964570Seric ctladdr->q_gid = st.st_gid; 79064570Seric ctladdr->q_flags |= QGOODUID; 79164570Seric } 7924406Seric } 7934406Seric 79453037Seric clrevent(ev); 79553037Seric 79664570Seric resetuid: 79764570Seric 79864570Seric #ifdef HASSETREUID 79964570Seric if (saveduid == 0) 80064570Seric { 80164570Seric if (uid != 0) 80264570Seric if (setreuid(-1, 0) < 0 || setreuid(RealUid, 0) < 0) 80364570Seric syserr("setreuid(%d, 0) failure (real=%d, eff=%d)", 80464570Seric RealUid, getuid(), geteuid()); 80564570Seric setgid(savedgid); 80664570Seric } 80764570Seric #endif 80864570Seric 80964570Seric if (tTd(27, 9)) 81064570Seric printf("include: reset uid = %d/%d\n", getuid(), geteuid()); 81164570Seric 81264570Seric if (fp == NULL) 81364570Seric return rval; 81464570Seric 81558092Seric if (bitset(EF_VRFYONLY, e->e_flags)) 81658092Seric { 81758092Seric /* don't do any more now */ 81858868Seric ctladdr->q_flags |= QVERIFIED; 81958884Seric e->e_nrcpts++; 82058680Seric xfclose(fp, "include", fname); 82164570Seric return rval; 82258092Seric } 82358092Seric 8244174Seric /* read the file -- each line is a comma-separated list. */ 8259379Seric FileName = fname; 8269379Seric LineNumber = 0; 82758082Seric ctladdr->q_flags &= ~QSELFREF; 82858082Seric nincludes = 0; 8294174Seric while (fgets(buf, sizeof buf, fp) != NULL) 8304174Seric { 83156795Seric register char *p = strchr(buf, '\n'); 8324174Seric 83340963Sbostic LineNumber++; 8344174Seric if (p != NULL) 8354174Seric *p = '\0'; 83657186Seric if (buf[0] == '#' || buf[0] == '\0') 83757139Seric continue; 83858008Seric e->e_to = NULL; 83958151Seric message("%s to %s", 84053037Seric forwarding ? "forwarding" : "sending", buf); 84157977Seric #ifdef LOG 84258020Seric if (forwarding && LogLevel > 9) 84357977Seric syslog(LOG_INFO, "%s: forward %s => %s", 84457977Seric e->e_id, oldto, buf); 84557977Seric #endif 84657977Seric 8474176Seric AliasLevel++; 84858082Seric nincludes += sendtolist(buf, ctladdr, sendq, e); 8494176Seric AliasLevel--; 8504174Seric } 85163902Seric 85263902Seric if (ferror(fp) && tTd(27, 3)) 85363902Seric printf("include: read error: %s\n", errstring(errno)); 85458082Seric if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags)) 85558065Seric { 85658065Seric if (tTd(27, 5)) 85758065Seric { 85858065Seric printf("include: QDONTSEND "); 85958065Seric printaddr(ctladdr, FALSE); 86058065Seric } 86158065Seric ctladdr->q_flags |= QDONTSEND; 86258065Seric } 8634174Seric 86458680Seric (void) xfclose(fp, "include", fname); 8659379Seric FileName = oldfilename; 8669379Seric LineNumber = oldlinenumber; 86763847Seric e->e_to = oldto; 86864325Seric return rval; 8694174Seric } 87053037Seric 87153037Seric static 87253037Seric includetimeout() 87353037Seric { 87453037Seric longjmp(CtxIncludeTimeout, 1); 87553037Seric } 8764324Seric /* 8774324Seric ** SENDTOARGV -- send to an argument vector. 8784324Seric ** 8794324Seric ** Parameters: 8804324Seric ** argv -- argument vector to send to. 88158247Seric ** e -- the current envelope. 8824324Seric ** 8834324Seric ** Returns: 8844324Seric ** none. 8854324Seric ** 8864324Seric ** Side Effects: 8874324Seric ** puts all addresses on the argument vector onto the 8884324Seric ** send queue. 8894324Seric */ 8904324Seric 89155012Seric sendtoargv(argv, e) 8924324Seric register char **argv; 89355012Seric register ENVELOPE *e; 8944324Seric { 8954324Seric register char *p; 8964324Seric 8974324Seric while ((p = *argv++) != NULL) 8984324Seric { 89964284Seric (void) sendtolist(p, NULLADDR, &e->e_sendqueue, e); 9004324Seric } 9014324Seric } 9024399Seric /* 9034399Seric ** GETCTLADDR -- get controlling address from an address header. 9044399Seric ** 9054399Seric ** If none, get one corresponding to the effective userid. 9064399Seric ** 9074399Seric ** Parameters: 9084399Seric ** a -- the address to find the controller of. 9094399Seric ** 9104399Seric ** Returns: 9114399Seric ** the controlling address. 9124399Seric ** 9134399Seric ** Side Effects: 9144399Seric ** none. 9154399Seric */ 9164399Seric 9174399Seric ADDRESS * 9184399Seric getctladdr(a) 9194399Seric register ADDRESS *a; 9204399Seric { 9214404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 9224399Seric a = a->q_alias; 9234399Seric return (a); 9244399Seric } 925