122710Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 333731Sbostic * Copyright (c) 1988 Regents of the University of California. 433731Sbostic * All rights reserved. 533731Sbostic * 642829Sbostic * %sccs.include.redist.c% 733731Sbostic */ 822710Sdist 922710Sdist #ifndef lint 10*57136Seric static char sccsid[] = "@(#)recipient.c 5.38 (Berkeley) 12/15/92"; 1133731Sbostic #endif /* not lint */ 1222710Sdist 1336928Sbostic # include <sys/types.h> 1436928Sbostic # include <sys/stat.h> 1552046Seric # include <sys/file.h> 164174Seric # include <pwd.h> 174627Seric # include "sendmail.h" 184174Seric 194174Seric /* 209622Seric ** SENDTOLIST -- Designate a send list. 214174Seric ** 224174Seric ** The parameter is a comma-separated list of people to send to. 234174Seric ** This routine arranges to send to all of them. 244174Seric ** 254174Seric ** Parameters: 264174Seric ** list -- the send list. 274399Seric ** ctladdr -- the address template for the person to 284399Seric ** send to -- effective uid/gid are important. 295006Seric ** This is typically the alias that caused this 305006Seric ** expansion. 315006Seric ** sendq -- a pointer to the head of a queue to put 325006Seric ** these people into. 334174Seric ** 344174Seric ** Returns: 354998Seric ** none 364174Seric ** 374174Seric ** Side Effects: 384174Seric ** none. 394174Seric */ 404174Seric 414174Seric # define MAXRCRSN 10 424174Seric 4355012Seric sendtolist(list, ctladdr, sendq, e) 444174Seric char *list; 454399Seric ADDRESS *ctladdr; 465198Seric ADDRESS **sendq; 4755012Seric register ENVELOPE *e; 484174Seric { 494174Seric register char *p; 508223Seric register ADDRESS *al; /* list of addresses to send to */ 514423Seric bool firstone; /* set on first address sent */ 524444Seric bool selfref; /* set if this list includes ctladdr */ 5311446Seric char delimiter; /* the address delimiter */ 544174Seric 557676Seric if (tTd(25, 1)) 564444Seric { 574444Seric printf("sendto: %s\n ctladdr=", list); 584444Seric printaddr(ctladdr, FALSE); 594444Seric } 604324Seric 618223Seric /* heuristic to determine old versus new style addresses */ 628230Seric if (ctladdr == NULL && 6356795Seric (strchr(list, ',') != NULL || strchr(list, ';') != NULL || 6456795Seric strchr(list, '<') != NULL || strchr(list, '(') != NULL)) 6555012Seric e->e_flags &= ~EF_OLDSTYLE; 6611446Seric delimiter = ' '; 6755012Seric if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL) 6811446Seric delimiter = ','; 698223Seric 704423Seric firstone = TRUE; 714444Seric selfref = FALSE; 724324Seric al = NULL; 738223Seric 748081Seric for (p = list; *p != '\0'; ) 754174Seric { 768081Seric register ADDRESS *a; 778081Seric extern char *DelimChar; /* defined in prescan */ 784319Seric 798081Seric /* parse the address */ 808081Seric while (isspace(*p) || *p == ',') 814174Seric p++; 8255012Seric a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter, e); 839297Seric p = DelimChar; 849297Seric if (a == NULL) 854174Seric continue; 864324Seric a->q_next = al; 874399Seric a->q_alias = ctladdr; 884444Seric 894444Seric /* see if this should be marked as a primary address */ 904423Seric if (ctladdr == NULL || 918081Seric (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags))) 924423Seric a->q_flags |= QPRIMARY; 934444Seric 944444Seric /* put on send queue or suppress self-reference */ 959379Seric if (ctladdr != NULL && sameaddr(ctladdr, a)) 964444Seric selfref = TRUE; 974444Seric else 984444Seric al = a; 994423Seric firstone = FALSE; 1004324Seric } 1014324Seric 1024444Seric /* if this alias doesn't include itself, delete ctladdr */ 1034444Seric if (!selfref && ctladdr != NULL) 1044444Seric ctladdr->q_flags |= QDONTSEND; 1054444Seric 1064324Seric /* arrange to send to everyone on the local send list */ 1074324Seric while (al != NULL) 1084324Seric { 1094324Seric register ADDRESS *a = al; 11012613Seric extern ADDRESS *recipient(); 1114324Seric 1124324Seric al = a->q_next; 11355012Seric a = recipient(a, sendq, e); 1144993Seric 1154998Seric /* arrange to inherit full name */ 1164998Seric if (a->q_fullname == NULL && ctladdr != NULL) 1174998Seric a->q_fullname = ctladdr->q_fullname; 1184174Seric } 1194324Seric 12055012Seric e->e_to = NULL; 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. 1324174Seric ** 1334174Seric ** Returns: 13412613Seric ** The actual address in the queue. This will be "a" if 13512613Seric ** the address is not a duplicate, else the original address. 1364174Seric ** 1374174Seric ** Side Effects: 1384174Seric ** none. 1394174Seric */ 1404174Seric 14146928Sbostic extern ADDRESS *getctladdr(); 14252046Seric extern char *RcptLogFile; 14346928Sbostic 14412613Seric ADDRESS * 14555012Seric recipient(a, sendq, e) 1464174Seric register ADDRESS *a; 1475006Seric register ADDRESS **sendq; 14855012Seric register ENVELOPE *e; 1494174Seric { 1504174Seric register ADDRESS *q; 1514319Seric ADDRESS **pq; 1524174Seric register struct mailer *m; 1539210Seric register char *p; 1549210Seric bool quoted = FALSE; /* set if the addr has a quote bit */ 15553735Seric int findusercount = 0; 1569210Seric char buf[MAXNAME]; /* unquoted image of the user name */ 1574627Seric extern bool safefile(); 1584174Seric 15955012Seric e->e_to = a->q_paddr; 1604600Seric m = a->q_mailer; 1614174Seric errno = 0; 1627676Seric if (tTd(26, 1)) 1634444Seric { 1644444Seric printf("\nrecipient: "); 1654444Seric printaddr(a, FALSE); 1664444Seric } 1674174Seric 1684174Seric /* break aliasing loops */ 1694174Seric if (AliasLevel > MAXRCRSN) 1704174Seric { 1714174Seric usrerr("aliasing/forwarding loop broken"); 17212613Seric return (a); 1734174Seric } 1744174Seric 1754174Seric /* 1764627Seric ** Finish setting up address structure. 1774174Seric */ 1784174Seric 17916160Seric /* set the queue timeout */ 1804627Seric a->q_timeout = TimeOut; 1814627Seric 18216160Seric /* map user & host to lower case if requested on non-aliases */ 18316160Seric if (a->q_alias == NULL) 18416160Seric loweraddr(a); 18516160Seric 18616160Seric /* get unquoted user for file, program or user.name check */ 1879210Seric (void) strcpy(buf, a->q_user); 1889210Seric for (p = buf; *p != '\0' && !quoted; p++) 1899210Seric { 19054993Seric if (*p == '\\') 1919210Seric quoted = TRUE; 1929210Seric } 19354983Seric stripquotes(buf); 1949210Seric 1954627Seric /* do sickly crude mapping for program mailing, etc. */ 1969210Seric if (m == LocalMailer && buf[0] == '|') 1974174Seric { 1989210Seric a->q_mailer = m = ProgMailer; 1999210Seric a->q_user++; 20054975Seric if (a->q_alias == NULL && !ForceMail) 2014174Seric { 20229915Seric a->q_flags |= QDONTSEND|QBADADDR; 2039210Seric usrerr("Cannot mail directly to programs"); 2044174Seric } 2054174Seric } 2064174Seric 2074174Seric /* 2084419Seric ** Look up this person in the recipient list. 2094419Seric ** If they are there already, return, otherwise continue. 2104419Seric ** If the list is empty, just add it. Notice the cute 2114419Seric ** hack to make from addresses suppress things correctly: 2124419Seric ** the QDONTSEND bit will be set in the send list. 2134419Seric ** [Please note: the emphasis is on "hack."] 2144174Seric */ 2154174Seric 2165006Seric for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 2174174Seric { 2189379Seric if (!ForceMail && sameaddr(q, a)) 2194174Seric { 2207676Seric if (tTd(26, 1)) 2214444Seric { 2224444Seric printf("%s in sendq: ", a->q_paddr); 2234444Seric printaddr(q, FALSE); 2244444Seric } 2257054Seric if (!bitset(QDONTSEND, a->q_flags)) 2264324Seric message(Arpa_Info, "duplicate suppressed"); 2274423Seric if (!bitset(QPRIMARY, q->q_flags)) 2284423Seric q->q_flags |= a->q_flags; 22912613Seric return (q); 2304174Seric } 2314319Seric } 2324174Seric 2334319Seric /* add address on list */ 2344319Seric *pq = a; 2354174Seric a->q_next = NULL; 23655012Seric e->e_nrcpts++; 2374174Seric 23852046Seric if (a->q_alias == NULL && RcptLogFile != NULL && 23952046Seric !bitset(QDONTSEND, a->q_flags)) 24052046Seric { 24152046Seric static int RcptLogFd = -1; 24252046Seric 24352046Seric /* 24452046Seric ** Log the incoming recipient name before aliasing, 24552046Seric ** expanding, forwarding, rewriting, and all that jazz. 24652046Seric ** We'll use this to track down out-of-date aliases, 24752046Seric ** host names, and so forth. 24852046Seric */ 24952046Seric 25052046Seric if (RcptLogFd < 0) 25152046Seric { 25252046Seric /* try to open the log file */ 25352046Seric RcptLogFd = open(RcptLogFile, O_WRONLY|O_APPEND|O_CREAT, 0666); 25452047Seric if (RcptLogFd >= 0) 25552047Seric (void) fcntl(RcptLogFd, F_SETFD, 1); 25652046Seric } 25752046Seric if (RcptLogFd >= 0) 25852046Seric { 25952046Seric int l = strlen(a->q_paddr); 26052046Seric 26152046Seric a->q_paddr[l] = '\n'; 26252046Seric if (write(RcptLogFd, a->q_paddr, l + 1) < 0) 26352046Seric { 26452046Seric (void) close(RcptLogFd); 26552046Seric RcptLogFd = -1; 26652046Seric } 26752046Seric a->q_paddr[l] = '\0'; 26852046Seric } 26952046Seric } 27052046Seric 2714174Seric /* 2724174Seric ** Alias the name and handle :include: specs. 2734174Seric */ 2744174Seric 27553735Seric trylocaluser: 27655354Seric if (tTd(29, 7)) 27755354Seric printf("at trylocaluser %s\n", a->q_user); 27855354Seric 2799210Seric if (m == LocalMailer && !bitset(QDONTSEND, a->q_flags)) 2804174Seric { 2814174Seric if (strncmp(a->q_user, ":include:", 9) == 0) 2824174Seric { 2834174Seric a->q_flags |= QDONTSEND; 28454975Seric if (a->q_alias == NULL && !ForceMail) 28529915Seric { 28629915Seric a->q_flags |= QBADADDR; 2874399Seric usrerr("Cannot mail directly to :include:s"); 28829915Seric } 2894399Seric else 2904399Seric { 2917054Seric message(Arpa_Info, "including file %s", &a->q_user[9]); 292*57136Seric (void) include(&a->q_user[9], FALSE, a, sendq, e); 2934399Seric } 2944174Seric } 2954174Seric else 29650556Seric { 29750556Seric /* try aliasing */ 29855012Seric alias(a, sendq, e); 29950556Seric 30050556Seric # ifdef USERDB 30151923Seric /* if not aliased, look it up in the user database */ 30251360Seric if (!bitset(QDONTSEND|QNOTREMOTE, a->q_flags)) 30351923Seric { 30451923Seric extern int udbexpand(); 30551923Seric 30655012Seric if (udbexpand(a, sendq, e) == EX_TEMPFAIL) 30751923Seric { 30851923Seric a->q_flags |= QQUEUEUP; 30955012Seric if (e->e_message == NULL) 31055012Seric e->e_message = newstr("Deferred: user database error"); 31151923Seric # ifdef LOG 31251923Seric if (LogLevel > 3) 31351923Seric syslog(LOG_INFO, "%s: deferred: udbexpand", 31455012Seric e->e_id); 31550556Seric # endif 31651923Seric message(Arpa_Info, "queued (user database error)"); 31751923Seric return (a); 31851923Seric } 31951923Seric } 32051923Seric # endif 32150556Seric } 3224174Seric } 3234174Seric 3244174Seric /* 3254174Seric ** If the user is local and still being sent, verify that 3264174Seric ** the address is good. If it is, try to forward. 3274174Seric ** If the address is already good, we have a forwarding 3284174Seric ** loop. This can be broken by just sending directly to 3294174Seric ** the user (which is probably correct anyway). 3304174Seric */ 3314174Seric 33251317Seric if (bitset(QDONTSEND, a->q_flags) || m != LocalMailer) 33351317Seric return (a); 33451317Seric 33551317Seric /* see if this is to a file */ 33651317Seric if (buf[0] == '/') 3374174Seric { 3384329Seric struct stat stb; 3394329Seric extern bool writable(); 3404174Seric 34156795Seric p = strrchr(buf, '/'); 34251317Seric /* check if writable or creatable */ 34351317Seric if (a->q_alias == NULL && !QueueRun && !ForceMail) 3444174Seric { 34551317Seric a->q_flags |= QDONTSEND|QBADADDR; 34651317Seric usrerr("Cannot mail directly to files"); 3474174Seric } 34851317Seric else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) : 34951317Seric (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC))) 35051317Seric { 35151317Seric a->q_flags |= QBADADDR; 35255012Seric giveresponse(EX_CANTCREAT, m, e); 35351317Seric } 35451317Seric return (a); 35551317Seric } 35651317Seric 35751317Seric /* 35851317Seric ** If we have a level two config file, then pass the name through 35951317Seric ** Ruleset 5 before sending it off. Ruleset 5 has the right 36051317Seric ** to send rewrite it to another mailer. This gives us a hook 36151317Seric ** after local aliasing has been done. 36251317Seric */ 36351317Seric 36451317Seric if (tTd(29, 5)) 36551317Seric { 36651317Seric printf("recipient: testing local? cl=%d, rr5=%x\n\t", 36751317Seric ConfigLevel, RewriteRules[5]); 36851317Seric printaddr(a, FALSE); 36951317Seric } 37051317Seric if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 && 37151317Seric RewriteRules[5] != NULL) 37251317Seric { 37355012Seric maplocaluser(a, sendq, e); 37451317Seric } 37551317Seric 37651317Seric /* 37751317Seric ** If it didn't get rewritten to another mailer, go ahead 37851317Seric ** and deliver it. 37951317Seric */ 38051317Seric 38151317Seric if (!bitset(QDONTSEND, a->q_flags)) 38251317Seric { 38355354Seric auto bool fuzzy; 38451317Seric register struct passwd *pw; 38551317Seric extern struct passwd *finduser(); 38651317Seric 38751317Seric /* warning -- finduser may trash buf */ 38855354Seric pw = finduser(buf, &fuzzy); 38951317Seric if (pw == NULL) 39051317Seric { 39151317Seric a->q_flags |= QBADADDR; 39255012Seric giveresponse(EX_NOUSER, m, e); 39351317Seric } 3944174Seric else 3954174Seric { 39651317Seric char nbuf[MAXNAME]; 3974373Seric 39855354Seric if (fuzzy) 3994174Seric { 40053735Seric /* name was a fuzzy match */ 40151317Seric a->q_user = newstr(pw->pw_name); 40253735Seric if (findusercount++ > 3) 40353735Seric { 40453735Seric usrerr("aliasing/forwarding loop for %s broken", 40553735Seric pw->pw_name); 40653735Seric return (a); 40753735Seric } 40853735Seric 40953735Seric /* see if it aliases */ 41051317Seric (void) strcpy(buf, pw->pw_name); 41153735Seric goto trylocaluser; 4124174Seric } 41351317Seric a->q_home = newstr(pw->pw_dir); 41451317Seric a->q_uid = pw->pw_uid; 41551317Seric a->q_gid = pw->pw_gid; 41651317Seric a->q_flags |= QGOODUID; 41751317Seric buildfname(pw->pw_gecos, pw->pw_name, nbuf); 41851317Seric if (nbuf[0] != '\0') 41951317Seric a->q_fullname = newstr(nbuf); 42051317Seric if (!quoted) 42155012Seric forward(a, sendq, e); 4224174Seric } 4234174Seric } 42412613Seric return (a); 4254174Seric } 4264174Seric /* 4274373Seric ** FINDUSER -- find the password entry for a user. 4284373Seric ** 4294373Seric ** This looks a lot like getpwnam, except that it may want to 4304373Seric ** do some fancier pattern matching in /etc/passwd. 4314373Seric ** 4329379Seric ** This routine contains most of the time of many sendmail runs. 4339379Seric ** It deserves to be optimized. 4349379Seric ** 4354373Seric ** Parameters: 4364373Seric ** name -- the name to match against. 43755354Seric ** fuzzyp -- an outarg that is set to TRUE if this entry 43855354Seric ** was found using the fuzzy matching algorithm; 43955354Seric ** set to FALSE otherwise. 4404373Seric ** 4414373Seric ** Returns: 4424373Seric ** A pointer to a pw struct. 4434373Seric ** NULL if name is unknown or ambiguous. 4444373Seric ** 4454373Seric ** Side Effects: 4464407Seric ** may modify name. 4474373Seric */ 4484373Seric 4494373Seric struct passwd * 45055354Seric finduser(name, fuzzyp) 4514373Seric char *name; 45255354Seric bool *fuzzyp; 4534373Seric { 4544376Seric register struct passwd *pw; 4554407Seric register char *p; 45615325Seric extern struct passwd *getpwent(); 45715325Seric extern struct passwd *getpwnam(); 4584373Seric 45955354Seric if (tTd(29, 4)) 46055354Seric printf("finduser(%s): ", name); 46155354Seric 46225777Seric /* map upper => lower case */ 4634407Seric for (p = name; *p != '\0'; p++) 4644407Seric { 46525777Seric if (isascii(*p) && isupper(*p)) 46625568Seric *p = tolower(*p); 4674407Seric } 46855354Seric *fuzzyp = FALSE; 4694407Seric 47025777Seric /* look up this login name using fast path */ 47112634Seric if ((pw = getpwnam(name)) != NULL) 47255354Seric { 47355354Seric if (tTd(29, 4)) 47455354Seric printf("found (non-fuzzy)\n"); 47512634Seric return (pw); 47655354Seric } 47712634Seric 47853735Seric #ifdef MATCHGECOS 47953735Seric /* see if fuzzy matching allowed */ 48053735Seric if (!MatchGecos) 48155354Seric { 48255354Seric if (tTd(29, 4)) 48355354Seric printf("not found (fuzzy disabled)\n"); 48453735Seric return NULL; 48555354Seric } 48653735Seric 48712634Seric /* search for a matching full name instead */ 48825777Seric for (p = name; *p != '\0'; p++) 48925777Seric { 49025777Seric if (*p == (SpaceSub & 0177) || *p == '_') 49125777Seric *p = ' '; 49225777Seric } 49323107Seric (void) setpwent(); 4944376Seric while ((pw = getpwent()) != NULL) 4954376Seric { 4964998Seric char buf[MAXNAME]; 4974376Seric 4984998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 49956795Seric if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name)) 5004381Seric { 50155354Seric if (tTd(29, 4)) 50255354Seric printf("fuzzy matches %s\n", pw->pw_name); 5037054Seric message(Arpa_Info, "sending to login name %s", pw->pw_name); 50455354Seric *fuzzyp = TRUE; 5054376Seric return (pw); 5064377Seric } 5074376Seric } 50853735Seric #endif 50955354Seric if (tTd(29, 4)) 51055354Seric printf("no fuzzy match found\n"); 5114376Seric return (NULL); 5124373Seric } 5134373Seric /* 5144329Seric ** WRITABLE -- predicate returning if the file is writable. 5154329Seric ** 5164329Seric ** This routine must duplicate the algorithm in sys/fio.c. 5174329Seric ** Unfortunately, we cannot use the access call since we 5184329Seric ** won't necessarily be the real uid when we try to 5194329Seric ** actually open the file. 5204329Seric ** 5214329Seric ** Notice that ANY file with ANY execute bit is automatically 5224329Seric ** not writable. This is also enforced by mailfile. 5234329Seric ** 5244329Seric ** Parameters: 5254329Seric ** s -- pointer to a stat struct for the file. 5264329Seric ** 5274329Seric ** Returns: 5284329Seric ** TRUE -- if we will be able to write this file. 5294329Seric ** FALSE -- if we cannot write this file. 5304329Seric ** 5314329Seric ** Side Effects: 5324329Seric ** none. 5334329Seric */ 5344329Seric 5354329Seric bool 5364329Seric writable(s) 5374329Seric register struct stat *s; 5384329Seric { 53955372Seric uid_t euid; 54055372Seric gid_t egid; 5414329Seric int bits; 5424329Seric 5434329Seric if (bitset(0111, s->st_mode)) 5444329Seric return (FALSE); 5454329Seric euid = getruid(); 5464329Seric egid = getrgid(); 5474329Seric if (geteuid() == 0) 5484329Seric { 5494329Seric if (bitset(S_ISUID, s->st_mode)) 5504329Seric euid = s->st_uid; 5514329Seric if (bitset(S_ISGID, s->st_mode)) 5524329Seric egid = s->st_gid; 5534329Seric } 5544329Seric 5554329Seric if (euid == 0) 5564329Seric return (TRUE); 5574329Seric bits = S_IWRITE; 5584329Seric if (euid != s->st_uid) 5594329Seric { 5604329Seric bits >>= 3; 5614329Seric if (egid != s->st_gid) 5624329Seric bits >>= 3; 5634329Seric } 5644329Seric return ((s->st_mode & bits) != 0); 5654329Seric } 5664329Seric /* 5674174Seric ** INCLUDE -- handle :include: specification. 5684174Seric ** 5694174Seric ** Parameters: 5704174Seric ** fname -- filename to include. 57153037Seric ** forwarding -- if TRUE, we are reading a .forward file. 57253037Seric ** if FALSE, it's a :include: file. 5734399Seric ** ctladdr -- address template to use to fill in these 5744399Seric ** addresses -- effective user/group id are 5754399Seric ** the important things. 5765006Seric ** sendq -- a pointer to the head of the send queue 5775006Seric ** to put these addresses in. 5784174Seric ** 5794174Seric ** Returns: 580*57136Seric ** open error status 5814174Seric ** 5824174Seric ** Side Effects: 5834174Seric ** reads the :include: file and sends to everyone 5844174Seric ** listed in that file. 5854174Seric */ 5864174Seric 58753037Seric static jmp_buf CtxIncludeTimeout; 58853037Seric 589*57136Seric int 59055012Seric include(fname, forwarding, ctladdr, sendq, e) 5914174Seric char *fname; 59253037Seric bool forwarding; 5934399Seric ADDRESS *ctladdr; 5945006Seric ADDRESS **sendq; 59555012Seric ENVELOPE *e; 5964174Seric { 5974174Seric register FILE *fp; 59855012Seric char *oldto = e->e_to; 5999379Seric char *oldfilename = FileName; 6009379Seric int oldlinenumber = LineNumber; 60153037Seric register EVENT *ev = NULL; 60253037Seric char buf[MAXLINE]; 60353037Seric static int includetimeout(); 6044174Seric 60553037Seric /* 60653037Seric ** If home directory is remote mounted but server is down, 60753037Seric ** this can hang or give errors; use a timeout to avoid this 60853037Seric */ 60953037Seric 61053037Seric if (setjmp(CtxIncludeTimeout) != 0) 61153037Seric { 61253037Seric ctladdr->q_flags |= QQUEUEUP|QDONTSEND; 61353037Seric errno = 0; 61453037Seric usrerr("451 open timeout on %s", fname); 615*57136Seric return ETIMEDOUT; 61653037Seric } 61753037Seric ev = setevent((time_t) 60, includetimeout, 0); 61853037Seric 61953037Seric /* if forwarding, the input file must be marked safe */ 62053037Seric if (forwarding && !safefile(fname, ctladdr->q_uid, S_IREAD)) 62153037Seric { 62253037Seric /* don't use this .forward file */ 62353037Seric clrevent(ev); 624*57136Seric return EPERM; 62553037Seric } 62653037Seric 6274174Seric fp = fopen(fname, "r"); 6284174Seric if (fp == NULL) 6294174Seric { 630*57136Seric int ret = errno; 631*57136Seric 6324174Seric usrerr("Cannot open %s", fname); 633*57136Seric return ret; 6344174Seric } 63553037Seric 6364406Seric if (getctladdr(ctladdr) == NULL) 6374406Seric { 6384406Seric struct stat st; 6394174Seric 6404406Seric if (fstat(fileno(fp), &st) < 0) 6414406Seric syserr("Cannot fstat %s!", fname); 6424406Seric ctladdr->q_uid = st.st_uid; 6434406Seric ctladdr->q_gid = st.st_gid; 6444406Seric ctladdr->q_flags |= QGOODUID; 6454406Seric } 6464406Seric 64753037Seric clrevent(ev); 64853037Seric 6494174Seric /* read the file -- each line is a comma-separated list. */ 6509379Seric FileName = fname; 6519379Seric LineNumber = 0; 6524174Seric while (fgets(buf, sizeof buf, fp) != NULL) 6534174Seric { 65456795Seric register char *p = strchr(buf, '\n'); 6554174Seric 65640963Sbostic LineNumber++; 6574174Seric if (p != NULL) 6584174Seric *p = '\0'; 65955012Seric e->e_to = oldto; 66053037Seric message(Arpa_Info, "%s to %s", 66153037Seric forwarding ? "forwarding" : "sending", buf); 6624176Seric AliasLevel++; 66355012Seric sendtolist(buf, ctladdr, sendq, e); 6644176Seric AliasLevel--; 6654174Seric } 6664174Seric 6674319Seric (void) fclose(fp); 6689379Seric FileName = oldfilename; 6699379Seric LineNumber = oldlinenumber; 670*57136Seric return 0; 6714174Seric } 67253037Seric 67353037Seric static 67453037Seric includetimeout() 67553037Seric { 67653037Seric longjmp(CtxIncludeTimeout, 1); 67753037Seric } 6784324Seric /* 6794324Seric ** SENDTOARGV -- send to an argument vector. 6804324Seric ** 6814324Seric ** Parameters: 6824324Seric ** argv -- argument vector to send to. 6834324Seric ** 6844324Seric ** Returns: 6854324Seric ** none. 6864324Seric ** 6874324Seric ** Side Effects: 6884324Seric ** puts all addresses on the argument vector onto the 6894324Seric ** send queue. 6904324Seric */ 6914324Seric 69255012Seric sendtoargv(argv, e) 6934324Seric register char **argv; 69455012Seric register ENVELOPE *e; 6954324Seric { 6964324Seric register char *p; 6974324Seric 6984324Seric while ((p = *argv++) != NULL) 6994324Seric { 70033725Sbostic if (argv[0] != NULL && argv[1] != NULL && !strcasecmp(argv[0], "at")) 7014324Seric { 7024324Seric char nbuf[MAXNAME]; 7034324Seric 7044324Seric if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf) 7054324Seric usrerr("address overflow"); 7064324Seric else 7074324Seric { 7084324Seric (void) strcpy(nbuf, p); 7094324Seric (void) strcat(nbuf, "@"); 7104324Seric (void) strcat(nbuf, argv[1]); 7114324Seric p = newstr(nbuf); 7124324Seric argv += 2; 7134324Seric } 7144324Seric } 71555012Seric sendtolist(p, (ADDRESS *) NULL, &e->e_sendqueue, e); 7164324Seric } 7174324Seric } 7184399Seric /* 7194399Seric ** GETCTLADDR -- get controlling address from an address header. 7204399Seric ** 7214399Seric ** If none, get one corresponding to the effective userid. 7224399Seric ** 7234399Seric ** Parameters: 7244399Seric ** a -- the address to find the controller of. 7254399Seric ** 7264399Seric ** Returns: 7274399Seric ** the controlling address. 7284399Seric ** 7294399Seric ** Side Effects: 7304399Seric ** none. 7314399Seric */ 7324399Seric 7334399Seric ADDRESS * 7344399Seric getctladdr(a) 7354399Seric register ADDRESS *a; 7364399Seric { 7374404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 7384399Seric a = a->q_alias; 7394399Seric return (a); 7404399Seric } 741