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*50556Seric static char sccsid[] = "@(#)recipient.c 5.20 (Berkeley) 07/26/91"; 1133731Sbostic #endif /* not lint */ 1222710Sdist 1336928Sbostic # include <sys/types.h> 1436928Sbostic # include <sys/stat.h> 154174Seric # include <pwd.h> 164627Seric # include "sendmail.h" 174174Seric 184174Seric /* 199622Seric ** SENDTOLIST -- Designate a send list. 204174Seric ** 214174Seric ** The parameter is a comma-separated list of people to send to. 224174Seric ** This routine arranges to send to all of them. 234174Seric ** 244174Seric ** Parameters: 254174Seric ** list -- the send list. 264399Seric ** ctladdr -- the address template for the person to 274399Seric ** send to -- effective uid/gid are important. 285006Seric ** This is typically the alias that caused this 295006Seric ** expansion. 305006Seric ** sendq -- a pointer to the head of a queue to put 315006Seric ** these people into. 324174Seric ** 334174Seric ** Returns: 344998Seric ** none 354174Seric ** 364174Seric ** Side Effects: 374174Seric ** none. 384174Seric */ 394174Seric 404174Seric # define MAXRCRSN 10 414174Seric 429622Seric sendtolist(list, ctladdr, sendq) 434174Seric char *list; 444399Seric ADDRESS *ctladdr; 455198Seric ADDRESS **sendq; 464174Seric { 474174Seric register char *p; 488223Seric register ADDRESS *al; /* list of addresses to send to */ 494423Seric bool firstone; /* set on first address sent */ 504444Seric bool selfref; /* set if this list includes ctladdr */ 5111446Seric char delimiter; /* the address delimiter */ 524174Seric 537676Seric if (tTd(25, 1)) 544444Seric { 554444Seric printf("sendto: %s\n ctladdr=", list); 564444Seric printaddr(ctladdr, FALSE); 574444Seric } 584324Seric 598223Seric /* heuristic to determine old versus new style addresses */ 608230Seric if (ctladdr == NULL && 618230Seric (index(list, ',') != NULL || index(list, ';') != NULL || 628230Seric index(list, '<') != NULL || index(list, '(') != NULL)) 639340Seric CurEnv->e_flags &= ~EF_OLDSTYLE; 6411446Seric delimiter = ' '; 6511446Seric if (!bitset(EF_OLDSTYLE, CurEnv->e_flags) || ctladdr != NULL) 6611446Seric delimiter = ','; 678223Seric 684423Seric firstone = TRUE; 694444Seric selfref = FALSE; 704324Seric al = NULL; 718223Seric 728081Seric for (p = list; *p != '\0'; ) 734174Seric { 748081Seric register ADDRESS *a; 758081Seric extern char *DelimChar; /* defined in prescan */ 764319Seric 778081Seric /* parse the address */ 788081Seric while (isspace(*p) || *p == ',') 794174Seric p++; 8011446Seric a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter); 819297Seric p = DelimChar; 829297Seric if (a == NULL) 834174Seric continue; 844324Seric a->q_next = al; 854399Seric a->q_alias = ctladdr; 864444Seric 874444Seric /* see if this should be marked as a primary address */ 884423Seric if (ctladdr == NULL || 898081Seric (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags))) 904423Seric a->q_flags |= QPRIMARY; 914444Seric 924444Seric /* put on send queue or suppress self-reference */ 939379Seric if (ctladdr != NULL && sameaddr(ctladdr, a)) 944444Seric selfref = TRUE; 954444Seric else 964444Seric al = a; 974423Seric firstone = FALSE; 984324Seric } 994324Seric 1004444Seric /* if this alias doesn't include itself, delete ctladdr */ 1014444Seric if (!selfref && ctladdr != NULL) 1024444Seric ctladdr->q_flags |= QDONTSEND; 1034444Seric 1044324Seric /* arrange to send to everyone on the local send list */ 1054324Seric while (al != NULL) 1064324Seric { 1074324Seric register ADDRESS *a = al; 10812613Seric extern ADDRESS *recipient(); 1094324Seric 1104324Seric al = a->q_next; 11140973Sbostic setctladdr(a); 11212613Seric a = recipient(a, sendq); 1134993Seric 1144998Seric /* arrange to inherit full name */ 1154998Seric if (a->q_fullname == NULL && ctladdr != NULL) 1164998Seric a->q_fullname = ctladdr->q_fullname; 1174174Seric } 1184324Seric 1196906Seric CurEnv->e_to = NULL; 1204174Seric } 1214174Seric /* 1224174Seric ** RECIPIENT -- Designate a message recipient 1234174Seric ** 1244174Seric ** Saves the named person for future mailing. 1254174Seric ** 1264174Seric ** Parameters: 1274174Seric ** a -- the (preparsed) address header for the recipient. 1285006Seric ** sendq -- a pointer to the head of a queue to put the 1295006Seric ** recipient in. Duplicate supression is done 1305006Seric ** in this queue. 1314174Seric ** 1324174Seric ** Returns: 13312613Seric ** The actual address in the queue. This will be "a" if 13412613Seric ** the address is not a duplicate, else the original address. 1354174Seric ** 1364174Seric ** Side Effects: 1374174Seric ** none. 1384174Seric */ 1394174Seric 14046928Sbostic extern ADDRESS *getctladdr(); 14146928Sbostic 14212613Seric ADDRESS * 1435006Seric recipient(a, sendq) 1444174Seric register ADDRESS *a; 1455006Seric register ADDRESS **sendq; 1464174Seric { 1474174Seric register ADDRESS *q; 1484319Seric ADDRESS **pq; 1494174Seric register struct mailer *m; 1509210Seric register char *p; 1519210Seric bool quoted = FALSE; /* set if the addr has a quote bit */ 1529210Seric char buf[MAXNAME]; /* unquoted image of the user name */ 1534627Seric extern bool safefile(); 1544174Seric 1556906Seric CurEnv->e_to = a->q_paddr; 1564600Seric m = a->q_mailer; 1574174Seric errno = 0; 1587676Seric if (tTd(26, 1)) 1594444Seric { 1604444Seric printf("\nrecipient: "); 1614444Seric printaddr(a, FALSE); 1624444Seric } 1634174Seric 1644174Seric /* break aliasing loops */ 1654174Seric if (AliasLevel > MAXRCRSN) 1664174Seric { 1674174Seric usrerr("aliasing/forwarding loop broken"); 16812613Seric return (a); 1694174Seric } 1704174Seric 1714174Seric /* 1724627Seric ** Finish setting up address structure. 1734174Seric */ 1744174Seric 17516160Seric /* set the queue timeout */ 1764627Seric a->q_timeout = TimeOut; 1774627Seric 17816160Seric /* map user & host to lower case if requested on non-aliases */ 17916160Seric if (a->q_alias == NULL) 18016160Seric loweraddr(a); 18116160Seric 18216160Seric /* get unquoted user for file, program or user.name check */ 1839210Seric (void) strcpy(buf, a->q_user); 1849210Seric for (p = buf; *p != '\0' && !quoted; p++) 1859210Seric { 1869210Seric if (!isascii(*p) && (*p & 0377) != (SpaceSub & 0377)) 1879210Seric quoted = TRUE; 1889210Seric } 1899210Seric stripquotes(buf, TRUE); 1909210Seric 1914627Seric /* do sickly crude mapping for program mailing, etc. */ 1929210Seric if (m == LocalMailer && buf[0] == '|') 1934174Seric { 1949210Seric a->q_mailer = m = ProgMailer; 1959210Seric a->q_user++; 19636231Sbostic if (a->q_alias == NULL && !QueueRun && !ForceMail) 1974174Seric { 19829915Seric a->q_flags |= QDONTSEND|QBADADDR; 1999210Seric usrerr("Cannot mail directly to programs"); 2004174Seric } 2014174Seric } 2024174Seric 2034174Seric /* 2044419Seric ** Look up this person in the recipient list. 2054419Seric ** If they are there already, return, otherwise continue. 2064419Seric ** If the list is empty, just add it. Notice the cute 2074419Seric ** hack to make from addresses suppress things correctly: 2084419Seric ** the QDONTSEND bit will be set in the send list. 2094419Seric ** [Please note: the emphasis is on "hack."] 2104174Seric */ 2114174Seric 2125006Seric for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 2134174Seric { 2149379Seric if (!ForceMail && sameaddr(q, a)) 2154174Seric { 2167676Seric if (tTd(26, 1)) 2174444Seric { 2184444Seric printf("%s in sendq: ", a->q_paddr); 2194444Seric printaddr(q, FALSE); 2204444Seric } 2217054Seric if (!bitset(QDONTSEND, a->q_flags)) 2224324Seric message(Arpa_Info, "duplicate suppressed"); 2234423Seric if (!bitset(QPRIMARY, q->q_flags)) 2244423Seric q->q_flags |= a->q_flags; 22512613Seric return (q); 2264174Seric } 2274319Seric } 2284174Seric 2294319Seric /* add address on list */ 2304319Seric *pq = a; 2314174Seric a->q_next = NULL; 23224951Seric CurEnv->e_nrcpts++; 2334174Seric 2344174Seric /* 2354174Seric ** Alias the name and handle :include: specs. 2364174Seric */ 2374174Seric 2389210Seric if (m == LocalMailer && !bitset(QDONTSEND, a->q_flags)) 2394174Seric { 2404174Seric if (strncmp(a->q_user, ":include:", 9) == 0) 2414174Seric { 2424174Seric a->q_flags |= QDONTSEND; 24336231Sbostic if (a->q_alias == NULL && !QueueRun && !ForceMail) 24429915Seric { 24529915Seric a->q_flags |= QBADADDR; 2464399Seric usrerr("Cannot mail directly to :include:s"); 24729915Seric } 2484399Seric else 2494399Seric { 2507054Seric message(Arpa_Info, "including file %s", &a->q_user[9]); 2515006Seric include(&a->q_user[9], " sending", a, sendq); 2524399Seric } 2534174Seric } 2544174Seric else 255*50556Seric { 256*50556Seric /* try aliasing */ 2575006Seric alias(a, sendq); 258*50556Seric 259*50556Seric # ifdef USERDB 260*50556Seric /* if not aliased, look it up in the user database */ 261*50556Seric if (!bitset(QDONTSEND, a->q_flags)) 262*50556Seric udbexpand(a, sendq); 263*50556Seric # endif 264*50556Seric } 2654174Seric } 2664174Seric 2674174Seric /* 2684174Seric ** If the user is local and still being sent, verify that 2694174Seric ** the address is good. If it is, try to forward. 2704174Seric ** If the address is already good, we have a forwarding 2714174Seric ** loop. This can be broken by just sending directly to 2724174Seric ** the user (which is probably correct anyway). 2734174Seric */ 2744174Seric 2759210Seric if (!bitset(QDONTSEND, a->q_flags) && m == LocalMailer) 2764174Seric { 2774329Seric struct stat stb; 2784329Seric extern bool writable(); 2794174Seric 2804174Seric /* see if this is to a file */ 2815600Seric if (buf[0] == '/') 2824174Seric { 2835600Seric p = rindex(buf, '/'); 2844201Seric /* check if writable or creatable */ 28536231Sbostic if (a->q_alias == NULL && !QueueRun && !ForceMail) 2864399Seric { 28729915Seric a->q_flags |= QDONTSEND|QBADADDR; 2884399Seric usrerr("Cannot mail directly to files"); 2894399Seric } 2904399Seric else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) : 2914539Seric (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC))) 2924174Seric { 2934174Seric a->q_flags |= QBADADDR; 29410109Seric giveresponse(EX_CANTCREAT, m, CurEnv); 2954174Seric } 2964174Seric } 2974174Seric else 2984174Seric { 2994174Seric register struct passwd *pw; 3004373Seric extern struct passwd *finduser(); 3014373Seric 3024407Seric /* warning -- finduser may trash buf */ 3034373Seric pw = finduser(buf); 3044174Seric if (pw == NULL) 3054174Seric { 3064174Seric a->q_flags |= QBADADDR; 30710109Seric giveresponse(EX_NOUSER, m, CurEnv); 3084174Seric } 3094174Seric else 3104174Seric { 3114993Seric char nbuf[MAXNAME]; 3124993Seric 3134376Seric if (strcmp(a->q_user, pw->pw_name) != 0) 3144376Seric { 3154376Seric a->q_user = newstr(pw->pw_name); 3167008Seric (void) strcpy(buf, pw->pw_name); 3174376Seric } 3184174Seric a->q_home = newstr(pw->pw_dir); 3194213Seric a->q_uid = pw->pw_uid; 3204399Seric a->q_gid = pw->pw_gid; 3214404Seric a->q_flags |= QGOODUID; 3224998Seric buildfname(pw->pw_gecos, pw->pw_name, nbuf); 3234993Seric if (nbuf[0] != '\0') 3244993Seric a->q_fullname = newstr(nbuf); 3254399Seric if (!quoted) 3265006Seric forward(a, sendq); 3274174Seric } 3284174Seric } 3294174Seric } 33012613Seric return (a); 3314174Seric } 3324174Seric /* 3334373Seric ** FINDUSER -- find the password entry for a user. 3344373Seric ** 3354373Seric ** This looks a lot like getpwnam, except that it may want to 3364373Seric ** do some fancier pattern matching in /etc/passwd. 3374373Seric ** 3389379Seric ** This routine contains most of the time of many sendmail runs. 3399379Seric ** It deserves to be optimized. 3409379Seric ** 3414373Seric ** Parameters: 3424373Seric ** name -- the name to match against. 3434373Seric ** 3444373Seric ** Returns: 3454373Seric ** A pointer to a pw struct. 3464373Seric ** NULL if name is unknown or ambiguous. 3474373Seric ** 3484373Seric ** Side Effects: 3494407Seric ** may modify name. 3504373Seric */ 3514373Seric 3524373Seric struct passwd * 3534373Seric finduser(name) 3544373Seric char *name; 3554373Seric { 3564376Seric register struct passwd *pw; 3574407Seric register char *p; 35815325Seric extern struct passwd *getpwent(); 35915325Seric extern struct passwd *getpwnam(); 3604373Seric 36125777Seric /* map upper => lower case */ 3624407Seric for (p = name; *p != '\0'; p++) 3634407Seric { 36425777Seric if (isascii(*p) && isupper(*p)) 36525568Seric *p = tolower(*p); 3664407Seric } 3674407Seric 36825777Seric /* look up this login name using fast path */ 36912634Seric if ((pw = getpwnam(name)) != NULL) 37012634Seric return (pw); 37112634Seric 37212634Seric /* search for a matching full name instead */ 37325777Seric for (p = name; *p != '\0'; p++) 37425777Seric { 37525777Seric if (*p == (SpaceSub & 0177) || *p == '_') 37625777Seric *p = ' '; 37725777Seric } 37823107Seric (void) setpwent(); 3794376Seric while ((pw = getpwent()) != NULL) 3804376Seric { 3814998Seric char buf[MAXNAME]; 3824376Seric 3834998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 38433725Sbostic if (index(buf, ' ') != NULL && !strcasecmp(buf, name)) 3854381Seric { 3867054Seric message(Arpa_Info, "sending to login name %s", pw->pw_name); 3874376Seric return (pw); 3884377Seric } 3894376Seric } 3904376Seric return (NULL); 3914373Seric } 3924373Seric /* 3934329Seric ** WRITABLE -- predicate returning if the file is writable. 3944329Seric ** 3954329Seric ** This routine must duplicate the algorithm in sys/fio.c. 3964329Seric ** Unfortunately, we cannot use the access call since we 3974329Seric ** won't necessarily be the real uid when we try to 3984329Seric ** actually open the file. 3994329Seric ** 4004329Seric ** Notice that ANY file with ANY execute bit is automatically 4014329Seric ** not writable. This is also enforced by mailfile. 4024329Seric ** 4034329Seric ** Parameters: 4044329Seric ** s -- pointer to a stat struct for the file. 4054329Seric ** 4064329Seric ** Returns: 4074329Seric ** TRUE -- if we will be able to write this file. 4084329Seric ** FALSE -- if we cannot write this file. 4094329Seric ** 4104329Seric ** Side Effects: 4114329Seric ** none. 4124329Seric */ 4134329Seric 4144329Seric bool 4154329Seric writable(s) 4164329Seric register struct stat *s; 4174329Seric { 4184329Seric int euid, egid; 4194329Seric int bits; 4204329Seric 4214329Seric if (bitset(0111, s->st_mode)) 4224329Seric return (FALSE); 4234329Seric euid = getruid(); 4244329Seric egid = getrgid(); 4254329Seric if (geteuid() == 0) 4264329Seric { 4274329Seric if (bitset(S_ISUID, s->st_mode)) 4284329Seric euid = s->st_uid; 4294329Seric if (bitset(S_ISGID, s->st_mode)) 4304329Seric egid = s->st_gid; 4314329Seric } 4324329Seric 4334329Seric if (euid == 0) 4344329Seric return (TRUE); 4354329Seric bits = S_IWRITE; 4364329Seric if (euid != s->st_uid) 4374329Seric { 4384329Seric bits >>= 3; 4394329Seric if (egid != s->st_gid) 4404329Seric bits >>= 3; 4414329Seric } 4424329Seric return ((s->st_mode & bits) != 0); 4434329Seric } 4444329Seric /* 4454174Seric ** INCLUDE -- handle :include: specification. 4464174Seric ** 4474174Seric ** Parameters: 4484174Seric ** fname -- filename to include. 4494176Seric ** msg -- message to print in verbose mode. 4504399Seric ** ctladdr -- address template to use to fill in these 4514399Seric ** addresses -- effective user/group id are 4524399Seric ** the important things. 4535006Seric ** sendq -- a pointer to the head of the send queue 4545006Seric ** to put these addresses in. 4554174Seric ** 4564174Seric ** Returns: 4574174Seric ** none. 4584174Seric ** 4594174Seric ** Side Effects: 4604174Seric ** reads the :include: file and sends to everyone 4614174Seric ** listed in that file. 4624174Seric */ 4634174Seric 4645006Seric include(fname, msg, ctladdr, sendq) 4654174Seric char *fname; 4664176Seric char *msg; 4674399Seric ADDRESS *ctladdr; 4685006Seric ADDRESS **sendq; 4694174Seric { 4704174Seric char buf[MAXLINE]; 4714174Seric register FILE *fp; 4726906Seric char *oldto = CurEnv->e_to; 4739379Seric char *oldfilename = FileName; 4749379Seric int oldlinenumber = LineNumber; 4754174Seric 4764174Seric fp = fopen(fname, "r"); 4774174Seric if (fp == NULL) 4784174Seric { 4794174Seric usrerr("Cannot open %s", fname); 4804174Seric return; 4814174Seric } 4824406Seric if (getctladdr(ctladdr) == NULL) 4834406Seric { 4844406Seric struct stat st; 4854174Seric 4864406Seric if (fstat(fileno(fp), &st) < 0) 4874406Seric syserr("Cannot fstat %s!", fname); 4884406Seric ctladdr->q_uid = st.st_uid; 4894406Seric ctladdr->q_gid = st.st_gid; 4904406Seric ctladdr->q_flags |= QGOODUID; 4914406Seric } 4924406Seric 4934174Seric /* read the file -- each line is a comma-separated list. */ 4949379Seric FileName = fname; 4959379Seric LineNumber = 0; 4964174Seric while (fgets(buf, sizeof buf, fp) != NULL) 4974174Seric { 4984174Seric register char *p = index(buf, '\n'); 4994174Seric 50040963Sbostic LineNumber++; 5014174Seric if (p != NULL) 5024174Seric *p = '\0'; 5034174Seric if (buf[0] == '\0') 5044174Seric continue; 5056906Seric CurEnv->e_to = oldto; 5067054Seric message(Arpa_Info, "%s to %s", msg, buf); 5074176Seric AliasLevel++; 5089622Seric sendtolist(buf, ctladdr, sendq); 5094176Seric AliasLevel--; 5104174Seric } 5114174Seric 5124319Seric (void) fclose(fp); 5139379Seric FileName = oldfilename; 5149379Seric LineNumber = oldlinenumber; 5154174Seric } 5164324Seric /* 5174324Seric ** SENDTOARGV -- send to an argument vector. 5184324Seric ** 5194324Seric ** Parameters: 5204324Seric ** argv -- argument vector to send to. 5214324Seric ** 5224324Seric ** Returns: 5234324Seric ** none. 5244324Seric ** 5254324Seric ** Side Effects: 5264324Seric ** puts all addresses on the argument vector onto the 5274324Seric ** send queue. 5284324Seric */ 5294324Seric 5304324Seric sendtoargv(argv) 5314324Seric register char **argv; 5324324Seric { 5334324Seric register char *p; 5344324Seric 5354324Seric while ((p = *argv++) != NULL) 5364324Seric { 53733725Sbostic if (argv[0] != NULL && argv[1] != NULL && !strcasecmp(argv[0], "at")) 5384324Seric { 5394324Seric char nbuf[MAXNAME]; 5404324Seric 5414324Seric if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf) 5424324Seric usrerr("address overflow"); 5434324Seric else 5444324Seric { 5454324Seric (void) strcpy(nbuf, p); 5464324Seric (void) strcat(nbuf, "@"); 5474324Seric (void) strcat(nbuf, argv[1]); 5484324Seric p = newstr(nbuf); 5494324Seric argv += 2; 5504324Seric } 5514324Seric } 5529622Seric sendtolist(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 5534324Seric } 5544324Seric } 5554399Seric /* 5564399Seric ** GETCTLADDR -- get controlling address from an address header. 5574399Seric ** 5584399Seric ** If none, get one corresponding to the effective userid. 5594399Seric ** 5604399Seric ** Parameters: 5614399Seric ** a -- the address to find the controller of. 5624399Seric ** 5634399Seric ** Returns: 5644399Seric ** the controlling address. 5654399Seric ** 5664399Seric ** Side Effects: 5674399Seric ** none. 5684399Seric */ 5694399Seric 5704399Seric ADDRESS * 5714399Seric getctladdr(a) 5724399Seric register ADDRESS *a; 5734399Seric { 5744404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 5754399Seric a = a->q_alias; 5764399Seric return (a); 5774399Seric } 578