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*46928Sbostic static char sccsid[] = "@(#)recipient.c 5.19 (Berkeley) 03/02/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 140*46928Sbostic extern ADDRESS *getctladdr(); 141*46928Sbostic 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 2555006Seric alias(a, sendq); 2564174Seric } 2574174Seric 2584174Seric /* 2594174Seric ** If the user is local and still being sent, verify that 2604174Seric ** the address is good. If it is, try to forward. 2614174Seric ** If the address is already good, we have a forwarding 2624174Seric ** loop. This can be broken by just sending directly to 2634174Seric ** the user (which is probably correct anyway). 2644174Seric */ 2654174Seric 2669210Seric if (!bitset(QDONTSEND, a->q_flags) && m == LocalMailer) 2674174Seric { 2684329Seric struct stat stb; 2694329Seric extern bool writable(); 2704174Seric 2714174Seric /* see if this is to a file */ 2725600Seric if (buf[0] == '/') 2734174Seric { 2745600Seric p = rindex(buf, '/'); 2754201Seric /* check if writable or creatable */ 27636231Sbostic if (a->q_alias == NULL && !QueueRun && !ForceMail) 2774399Seric { 27829915Seric a->q_flags |= QDONTSEND|QBADADDR; 2794399Seric usrerr("Cannot mail directly to files"); 2804399Seric } 2814399Seric else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) : 2824539Seric (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC))) 2834174Seric { 2844174Seric a->q_flags |= QBADADDR; 28510109Seric giveresponse(EX_CANTCREAT, m, CurEnv); 2864174Seric } 2874174Seric } 2884174Seric else 2894174Seric { 2904174Seric register struct passwd *pw; 2914373Seric extern struct passwd *finduser(); 2924373Seric 2934407Seric /* warning -- finduser may trash buf */ 2944373Seric pw = finduser(buf); 2954174Seric if (pw == NULL) 2964174Seric { 2974174Seric a->q_flags |= QBADADDR; 29810109Seric giveresponse(EX_NOUSER, m, CurEnv); 2994174Seric } 3004174Seric else 3014174Seric { 3024993Seric char nbuf[MAXNAME]; 3034993Seric 3044376Seric if (strcmp(a->q_user, pw->pw_name) != 0) 3054376Seric { 3064376Seric a->q_user = newstr(pw->pw_name); 3077008Seric (void) strcpy(buf, pw->pw_name); 3084376Seric } 3094174Seric a->q_home = newstr(pw->pw_dir); 3104213Seric a->q_uid = pw->pw_uid; 3114399Seric a->q_gid = pw->pw_gid; 3124404Seric a->q_flags |= QGOODUID; 3134998Seric buildfname(pw->pw_gecos, pw->pw_name, nbuf); 3144993Seric if (nbuf[0] != '\0') 3154993Seric a->q_fullname = newstr(nbuf); 3164399Seric if (!quoted) 3175006Seric forward(a, sendq); 3184174Seric } 3194174Seric } 3204174Seric } 32112613Seric return (a); 3224174Seric } 3234174Seric /* 3244373Seric ** FINDUSER -- find the password entry for a user. 3254373Seric ** 3264373Seric ** This looks a lot like getpwnam, except that it may want to 3274373Seric ** do some fancier pattern matching in /etc/passwd. 3284373Seric ** 3299379Seric ** This routine contains most of the time of many sendmail runs. 3309379Seric ** It deserves to be optimized. 3319379Seric ** 3324373Seric ** Parameters: 3334373Seric ** name -- the name to match against. 3344373Seric ** 3354373Seric ** Returns: 3364373Seric ** A pointer to a pw struct. 3374373Seric ** NULL if name is unknown or ambiguous. 3384373Seric ** 3394373Seric ** Side Effects: 3404407Seric ** may modify name. 3414373Seric */ 3424373Seric 3434373Seric struct passwd * 3444373Seric finduser(name) 3454373Seric char *name; 3464373Seric { 3474376Seric register struct passwd *pw; 3484407Seric register char *p; 34915325Seric extern struct passwd *getpwent(); 35015325Seric extern struct passwd *getpwnam(); 3514373Seric 35225777Seric /* map upper => lower case */ 3534407Seric for (p = name; *p != '\0'; p++) 3544407Seric { 35525777Seric if (isascii(*p) && isupper(*p)) 35625568Seric *p = tolower(*p); 3574407Seric } 3584407Seric 35925777Seric /* look up this login name using fast path */ 36012634Seric if ((pw = getpwnam(name)) != NULL) 36112634Seric return (pw); 36212634Seric 36312634Seric /* search for a matching full name instead */ 36425777Seric for (p = name; *p != '\0'; p++) 36525777Seric { 36625777Seric if (*p == (SpaceSub & 0177) || *p == '_') 36725777Seric *p = ' '; 36825777Seric } 36923107Seric (void) setpwent(); 3704376Seric while ((pw = getpwent()) != NULL) 3714376Seric { 3724998Seric char buf[MAXNAME]; 3734376Seric 3744998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 37533725Sbostic if (index(buf, ' ') != NULL && !strcasecmp(buf, name)) 3764381Seric { 3777054Seric message(Arpa_Info, "sending to login name %s", pw->pw_name); 3784376Seric return (pw); 3794377Seric } 3804376Seric } 3814376Seric return (NULL); 3824373Seric } 3834373Seric /* 3844329Seric ** WRITABLE -- predicate returning if the file is writable. 3854329Seric ** 3864329Seric ** This routine must duplicate the algorithm in sys/fio.c. 3874329Seric ** Unfortunately, we cannot use the access call since we 3884329Seric ** won't necessarily be the real uid when we try to 3894329Seric ** actually open the file. 3904329Seric ** 3914329Seric ** Notice that ANY file with ANY execute bit is automatically 3924329Seric ** not writable. This is also enforced by mailfile. 3934329Seric ** 3944329Seric ** Parameters: 3954329Seric ** s -- pointer to a stat struct for the file. 3964329Seric ** 3974329Seric ** Returns: 3984329Seric ** TRUE -- if we will be able to write this file. 3994329Seric ** FALSE -- if we cannot write this file. 4004329Seric ** 4014329Seric ** Side Effects: 4024329Seric ** none. 4034329Seric */ 4044329Seric 4054329Seric bool 4064329Seric writable(s) 4074329Seric register struct stat *s; 4084329Seric { 4094329Seric int euid, egid; 4104329Seric int bits; 4114329Seric 4124329Seric if (bitset(0111, s->st_mode)) 4134329Seric return (FALSE); 4144329Seric euid = getruid(); 4154329Seric egid = getrgid(); 4164329Seric if (geteuid() == 0) 4174329Seric { 4184329Seric if (bitset(S_ISUID, s->st_mode)) 4194329Seric euid = s->st_uid; 4204329Seric if (bitset(S_ISGID, s->st_mode)) 4214329Seric egid = s->st_gid; 4224329Seric } 4234329Seric 4244329Seric if (euid == 0) 4254329Seric return (TRUE); 4264329Seric bits = S_IWRITE; 4274329Seric if (euid != s->st_uid) 4284329Seric { 4294329Seric bits >>= 3; 4304329Seric if (egid != s->st_gid) 4314329Seric bits >>= 3; 4324329Seric } 4334329Seric return ((s->st_mode & bits) != 0); 4344329Seric } 4354329Seric /* 4364174Seric ** INCLUDE -- handle :include: specification. 4374174Seric ** 4384174Seric ** Parameters: 4394174Seric ** fname -- filename to include. 4404176Seric ** msg -- message to print in verbose mode. 4414399Seric ** ctladdr -- address template to use to fill in these 4424399Seric ** addresses -- effective user/group id are 4434399Seric ** the important things. 4445006Seric ** sendq -- a pointer to the head of the send queue 4455006Seric ** to put these addresses in. 4464174Seric ** 4474174Seric ** Returns: 4484174Seric ** none. 4494174Seric ** 4504174Seric ** Side Effects: 4514174Seric ** reads the :include: file and sends to everyone 4524174Seric ** listed in that file. 4534174Seric */ 4544174Seric 4555006Seric include(fname, msg, ctladdr, sendq) 4564174Seric char *fname; 4574176Seric char *msg; 4584399Seric ADDRESS *ctladdr; 4595006Seric ADDRESS **sendq; 4604174Seric { 4614174Seric char buf[MAXLINE]; 4624174Seric register FILE *fp; 4636906Seric char *oldto = CurEnv->e_to; 4649379Seric char *oldfilename = FileName; 4659379Seric int oldlinenumber = LineNumber; 4664174Seric 4674174Seric fp = fopen(fname, "r"); 4684174Seric if (fp == NULL) 4694174Seric { 4704174Seric usrerr("Cannot open %s", fname); 4714174Seric return; 4724174Seric } 4734406Seric if (getctladdr(ctladdr) == NULL) 4744406Seric { 4754406Seric struct stat st; 4764174Seric 4774406Seric if (fstat(fileno(fp), &st) < 0) 4784406Seric syserr("Cannot fstat %s!", fname); 4794406Seric ctladdr->q_uid = st.st_uid; 4804406Seric ctladdr->q_gid = st.st_gid; 4814406Seric ctladdr->q_flags |= QGOODUID; 4824406Seric } 4834406Seric 4844174Seric /* read the file -- each line is a comma-separated list. */ 4859379Seric FileName = fname; 4869379Seric LineNumber = 0; 4874174Seric while (fgets(buf, sizeof buf, fp) != NULL) 4884174Seric { 4894174Seric register char *p = index(buf, '\n'); 4904174Seric 49140963Sbostic LineNumber++; 4924174Seric if (p != NULL) 4934174Seric *p = '\0'; 4944174Seric if (buf[0] == '\0') 4954174Seric continue; 4966906Seric CurEnv->e_to = oldto; 4977054Seric message(Arpa_Info, "%s to %s", msg, buf); 4984176Seric AliasLevel++; 4999622Seric sendtolist(buf, ctladdr, sendq); 5004176Seric AliasLevel--; 5014174Seric } 5024174Seric 5034319Seric (void) fclose(fp); 5049379Seric FileName = oldfilename; 5059379Seric LineNumber = oldlinenumber; 5064174Seric } 5074324Seric /* 5084324Seric ** SENDTOARGV -- send to an argument vector. 5094324Seric ** 5104324Seric ** Parameters: 5114324Seric ** argv -- argument vector to send to. 5124324Seric ** 5134324Seric ** Returns: 5144324Seric ** none. 5154324Seric ** 5164324Seric ** Side Effects: 5174324Seric ** puts all addresses on the argument vector onto the 5184324Seric ** send queue. 5194324Seric */ 5204324Seric 5214324Seric sendtoargv(argv) 5224324Seric register char **argv; 5234324Seric { 5244324Seric register char *p; 5254324Seric 5264324Seric while ((p = *argv++) != NULL) 5274324Seric { 52833725Sbostic if (argv[0] != NULL && argv[1] != NULL && !strcasecmp(argv[0], "at")) 5294324Seric { 5304324Seric char nbuf[MAXNAME]; 5314324Seric 5324324Seric if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf) 5334324Seric usrerr("address overflow"); 5344324Seric else 5354324Seric { 5364324Seric (void) strcpy(nbuf, p); 5374324Seric (void) strcat(nbuf, "@"); 5384324Seric (void) strcat(nbuf, argv[1]); 5394324Seric p = newstr(nbuf); 5404324Seric argv += 2; 5414324Seric } 5424324Seric } 5439622Seric sendtolist(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 5444324Seric } 5454324Seric } 5464399Seric /* 5474399Seric ** GETCTLADDR -- get controlling address from an address header. 5484399Seric ** 5494399Seric ** If none, get one corresponding to the effective userid. 5504399Seric ** 5514399Seric ** Parameters: 5524399Seric ** a -- the address to find the controller of. 5534399Seric ** 5544399Seric ** Returns: 5554399Seric ** the controlling address. 5564399Seric ** 5574399Seric ** Side Effects: 5584399Seric ** none. 5594399Seric */ 5604399Seric 5614399Seric ADDRESS * 5624399Seric getctladdr(a) 5634399Seric register ADDRESS *a; 5644399Seric { 5654404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 5664399Seric a = a->q_alias; 5674399Seric return (a); 5684399Seric } 569