14174Seric # include <pwd.h> 24627Seric # include "sendmail.h" 34329Seric # include <sys/stat.h> 44174Seric 5*16160Seric SCCSID(@(#)recipient.c 4.3 03/11/84); 64174Seric 74174Seric /* 89622Seric ** SENDTOLIST -- Designate a send list. 94174Seric ** 104174Seric ** The parameter is a comma-separated list of people to send to. 114174Seric ** This routine arranges to send to all of them. 124174Seric ** 134174Seric ** Parameters: 144174Seric ** list -- the send list. 154399Seric ** ctladdr -- the address template for the person to 164399Seric ** send to -- effective uid/gid are important. 175006Seric ** This is typically the alias that caused this 185006Seric ** expansion. 195006Seric ** sendq -- a pointer to the head of a queue to put 205006Seric ** these people into. 214174Seric ** 224174Seric ** Returns: 234998Seric ** none 244174Seric ** 254174Seric ** Side Effects: 264174Seric ** none. 274174Seric */ 284174Seric 294174Seric # define MAXRCRSN 10 304174Seric 319622Seric sendtolist(list, ctladdr, sendq) 324174Seric char *list; 334399Seric ADDRESS *ctladdr; 345198Seric ADDRESS **sendq; 354174Seric { 364174Seric register char *p; 378223Seric register ADDRESS *al; /* list of addresses to send to */ 384423Seric bool firstone; /* set on first address sent */ 394444Seric bool selfref; /* set if this list includes ctladdr */ 4011446Seric char delimiter; /* the address delimiter */ 414174Seric 424324Seric # ifdef DEBUG 437676Seric if (tTd(25, 1)) 444444Seric { 454444Seric printf("sendto: %s\n ctladdr=", list); 464444Seric printaddr(ctladdr, FALSE); 474444Seric } 484324Seric # endif DEBUG 494324Seric 508223Seric /* heuristic to determine old versus new style addresses */ 518230Seric if (ctladdr == NULL && 528230Seric (index(list, ',') != NULL || index(list, ';') != NULL || 538230Seric index(list, '<') != NULL || index(list, '(') != NULL)) 549340Seric CurEnv->e_flags &= ~EF_OLDSTYLE; 5511446Seric delimiter = ' '; 5611446Seric if (!bitset(EF_OLDSTYLE, CurEnv->e_flags) || ctladdr != NULL) 5711446Seric delimiter = ','; 588223Seric 594423Seric firstone = TRUE; 604444Seric selfref = FALSE; 614324Seric al = NULL; 628223Seric 638081Seric for (p = list; *p != '\0'; ) 644174Seric { 658081Seric register ADDRESS *a; 668081Seric extern char *DelimChar; /* defined in prescan */ 674319Seric 688081Seric /* parse the address */ 698081Seric while (isspace(*p) || *p == ',') 704174Seric p++; 7111446Seric a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter); 729297Seric p = DelimChar; 739297Seric if (a == NULL) 744174Seric continue; 754324Seric a->q_next = al; 764399Seric a->q_alias = ctladdr; 774444Seric 784444Seric /* see if this should be marked as a primary address */ 794423Seric if (ctladdr == NULL || 808081Seric (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags))) 814423Seric a->q_flags |= QPRIMARY; 824444Seric 834444Seric /* put on send queue or suppress self-reference */ 849379Seric if (ctladdr != NULL && sameaddr(ctladdr, a)) 854444Seric selfref = TRUE; 864444Seric else 874444Seric al = a; 884423Seric firstone = FALSE; 894324Seric } 904324Seric 914444Seric /* if this alias doesn't include itself, delete ctladdr */ 924444Seric if (!selfref && ctladdr != NULL) 934444Seric ctladdr->q_flags |= QDONTSEND; 944444Seric 954324Seric /* arrange to send to everyone on the local send list */ 964324Seric while (al != NULL) 974324Seric { 984324Seric register ADDRESS *a = al; 9912613Seric extern ADDRESS *recipient(); 1004324Seric 1014324Seric al = a->q_next; 10212613Seric a = recipient(a, sendq); 1034993Seric 1044998Seric /* arrange to inherit full name */ 1054998Seric if (a->q_fullname == NULL && ctladdr != NULL) 1064998Seric a->q_fullname = ctladdr->q_fullname; 1074174Seric } 1084324Seric 1096906Seric CurEnv->e_to = NULL; 1104174Seric } 1114174Seric /* 1124174Seric ** RECIPIENT -- Designate a message recipient 1134174Seric ** 1144174Seric ** Saves the named person for future mailing. 1154174Seric ** 1164174Seric ** Parameters: 1174174Seric ** a -- the (preparsed) address header for the recipient. 1185006Seric ** sendq -- a pointer to the head of a queue to put the 1195006Seric ** recipient in. Duplicate supression is done 1205006Seric ** in this queue. 1214174Seric ** 1224174Seric ** Returns: 12312613Seric ** The actual address in the queue. This will be "a" if 12412613Seric ** the address is not a duplicate, else the original address. 1254174Seric ** 1264174Seric ** Side Effects: 1274174Seric ** none. 1284174Seric */ 1294174Seric 13012613Seric ADDRESS * 1315006Seric recipient(a, sendq) 1324174Seric register ADDRESS *a; 1335006Seric register ADDRESS **sendq; 1344174Seric { 1354174Seric register ADDRESS *q; 1364319Seric ADDRESS **pq; 1374174Seric register struct mailer *m; 1389210Seric register char *p; 1399210Seric bool quoted = FALSE; /* set if the addr has a quote bit */ 1409210Seric char buf[MAXNAME]; /* unquoted image of the user name */ 1414399Seric extern ADDRESS *getctladdr(); 1424627Seric extern bool safefile(); 1434174Seric 1446906Seric CurEnv->e_to = a->q_paddr; 1454600Seric m = a->q_mailer; 1464174Seric errno = 0; 1474174Seric # ifdef DEBUG 1487676Seric if (tTd(26, 1)) 1494444Seric { 1504444Seric printf("\nrecipient: "); 1514444Seric printaddr(a, FALSE); 1524444Seric } 1534174Seric # endif DEBUG 1544174Seric 1554174Seric /* break aliasing loops */ 1564174Seric if (AliasLevel > MAXRCRSN) 1574174Seric { 1584174Seric usrerr("aliasing/forwarding loop broken"); 15912613Seric return (a); 1604174Seric } 1614174Seric 1624174Seric /* 1634627Seric ** Finish setting up address structure. 1644174Seric */ 1654174Seric 166*16160Seric /* set the queue timeout */ 1674627Seric a->q_timeout = TimeOut; 1684627Seric 169*16160Seric /* map user & host to lower case if requested on non-aliases */ 170*16160Seric if (a->q_alias == NULL) 171*16160Seric loweraddr(a); 172*16160Seric 173*16160Seric /* get unquoted user for file, program or user.name check */ 1749210Seric (void) strcpy(buf, a->q_user); 1759210Seric for (p = buf; *p != '\0' && !quoted; p++) 1769210Seric { 1779210Seric if (!isascii(*p) && (*p & 0377) != (SpaceSub & 0377)) 1789210Seric quoted = TRUE; 1799210Seric } 1809210Seric stripquotes(buf, TRUE); 1819210Seric 1824627Seric /* do sickly crude mapping for program mailing, etc. */ 1839210Seric if (m == LocalMailer && buf[0] == '|') 1844174Seric { 1859210Seric a->q_mailer = m = ProgMailer; 1869210Seric a->q_user++; 1879210Seric if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail) 1884174Seric { 1899210Seric usrerr("Cannot mail directly to programs"); 1909210Seric a->q_flags |= QDONTSEND; 1914174Seric } 1924174Seric } 1934174Seric 1944174Seric /* 1954419Seric ** Look up this person in the recipient list. 1964419Seric ** If they are there already, return, otherwise continue. 1974419Seric ** If the list is empty, just add it. Notice the cute 1984419Seric ** hack to make from addresses suppress things correctly: 1994419Seric ** the QDONTSEND bit will be set in the send list. 2004419Seric ** [Please note: the emphasis is on "hack."] 2014174Seric */ 2024174Seric 2035006Seric for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 2044174Seric { 2059379Seric if (!ForceMail && sameaddr(q, a)) 2064174Seric { 2074174Seric # ifdef DEBUG 2087676Seric if (tTd(26, 1)) 2094444Seric { 2104444Seric printf("%s in sendq: ", a->q_paddr); 2114444Seric printaddr(q, FALSE); 2124444Seric } 2134174Seric # endif DEBUG 2147054Seric if (!bitset(QDONTSEND, a->q_flags)) 2154324Seric message(Arpa_Info, "duplicate suppressed"); 2164423Seric if (!bitset(QPRIMARY, q->q_flags)) 2174423Seric q->q_flags |= a->q_flags; 21812613Seric return (q); 2194174Seric } 2204319Seric } 2214174Seric 2224319Seric /* add address on list */ 2234319Seric *pq = a; 2244174Seric a->q_next = NULL; 2254174Seric 2264174Seric /* 2274174Seric ** Alias the name and handle :include: specs. 2284174Seric */ 2294174Seric 2309210Seric if (m == LocalMailer && !bitset(QDONTSEND, a->q_flags)) 2314174Seric { 2324174Seric if (strncmp(a->q_user, ":include:", 9) == 0) 2334174Seric { 2344174Seric a->q_flags |= QDONTSEND; 2357676Seric if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail) 2364399Seric usrerr("Cannot mail directly to :include:s"); 2374399Seric else 2384399Seric { 2397054Seric message(Arpa_Info, "including file %s", &a->q_user[9]); 2405006Seric include(&a->q_user[9], " sending", a, sendq); 2414399Seric } 2424174Seric } 2434174Seric else 2445006Seric alias(a, sendq); 2454174Seric } 2464174Seric 2474174Seric /* 2484174Seric ** If the user is local and still being sent, verify that 2494174Seric ** the address is good. If it is, try to forward. 2504174Seric ** If the address is already good, we have a forwarding 2514174Seric ** loop. This can be broken by just sending directly to 2524174Seric ** the user (which is probably correct anyway). 2534174Seric */ 2544174Seric 2559210Seric if (!bitset(QDONTSEND, a->q_flags) && m == LocalMailer) 2564174Seric { 2574329Seric struct stat stb; 2584329Seric extern bool writable(); 2594174Seric 2604174Seric /* see if this is to a file */ 2615600Seric if (buf[0] == '/') 2624174Seric { 2635600Seric p = rindex(buf, '/'); 2644201Seric /* check if writable or creatable */ 2657676Seric if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail) 2664399Seric { 2674399Seric usrerr("Cannot mail directly to files"); 2684399Seric a->q_flags |= QDONTSEND; 2694399Seric } 2704399Seric else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) : 2714539Seric (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC))) 2724174Seric { 2734174Seric a->q_flags |= QBADADDR; 27410109Seric giveresponse(EX_CANTCREAT, m, CurEnv); 2754174Seric } 2764174Seric } 2774174Seric else 2784174Seric { 2794174Seric register struct passwd *pw; 2804373Seric extern struct passwd *finduser(); 2814373Seric 2824407Seric /* warning -- finduser may trash buf */ 2834373Seric pw = finduser(buf); 2844174Seric if (pw == NULL) 2854174Seric { 2864174Seric a->q_flags |= QBADADDR; 28710109Seric giveresponse(EX_NOUSER, m, CurEnv); 2884174Seric } 2894174Seric else 2904174Seric { 2914993Seric char nbuf[MAXNAME]; 2924993Seric 2934376Seric if (strcmp(a->q_user, pw->pw_name) != 0) 2944376Seric { 2954376Seric a->q_user = newstr(pw->pw_name); 2967008Seric (void) strcpy(buf, pw->pw_name); 2974376Seric } 2984174Seric a->q_home = newstr(pw->pw_dir); 2994213Seric a->q_uid = pw->pw_uid; 3004399Seric a->q_gid = pw->pw_gid; 3014404Seric a->q_flags |= QGOODUID; 3024998Seric buildfname(pw->pw_gecos, pw->pw_name, nbuf); 3034993Seric if (nbuf[0] != '\0') 3044993Seric a->q_fullname = newstr(nbuf); 3054399Seric if (!quoted) 3065006Seric forward(a, sendq); 3074174Seric } 3084174Seric } 3094174Seric } 31012613Seric return (a); 3114174Seric } 3124174Seric /* 3134373Seric ** FINDUSER -- find the password entry for a user. 3144373Seric ** 3154373Seric ** This looks a lot like getpwnam, except that it may want to 3164373Seric ** do some fancier pattern matching in /etc/passwd. 3174373Seric ** 3189379Seric ** This routine contains most of the time of many sendmail runs. 3199379Seric ** It deserves to be optimized. 3209379Seric ** 3214373Seric ** Parameters: 3224373Seric ** name -- the name to match against. 3234373Seric ** 3244373Seric ** Returns: 3254373Seric ** A pointer to a pw struct. 3264373Seric ** NULL if name is unknown or ambiguous. 3274373Seric ** 3284373Seric ** Side Effects: 3294407Seric ** may modify name. 3304373Seric */ 3314373Seric 3324373Seric struct passwd * 3334373Seric finduser(name) 3344373Seric char *name; 3354373Seric { 3364376Seric register struct passwd *pw; 3374407Seric register char *p; 33815325Seric extern struct passwd *getpwent(); 33915325Seric extern struct passwd *getpwnam(); 3404373Seric 3414407Seric /* 3424407Seric ** Make name canonical. 3434407Seric */ 3444407Seric 3454407Seric for (p = name; *p != '\0'; p++) 3464407Seric { 3479044Seric if (*p == (SpaceSub & 0177) || *p == '_') 3484407Seric *p = ' '; 3494407Seric } 3504407Seric 35112634Seric /* look up this login name */ 35212634Seric if ((pw = getpwnam(name)) != NULL) 35312634Seric return (pw); 35412634Seric 35512634Seric /* search for a matching full name instead */ 3564376Seric setpwent(); 3574376Seric while ((pw = getpwent()) != NULL) 3584376Seric { 3594998Seric char buf[MAXNAME]; 3604993Seric extern bool sameword(); 3614376Seric 3624376Seric if (strcmp(pw->pw_name, name) == 0) 3634376Seric return (pw); 3644998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 3654407Seric if (index(buf, ' ') != NULL && sameword(buf, name)) 3664381Seric { 3677054Seric message(Arpa_Info, "sending to login name %s", pw->pw_name); 3684376Seric return (pw); 3694377Seric } 3704376Seric } 3714376Seric return (NULL); 3724373Seric } 3734373Seric /* 3744329Seric ** WRITABLE -- predicate returning if the file is writable. 3754329Seric ** 3764329Seric ** This routine must duplicate the algorithm in sys/fio.c. 3774329Seric ** Unfortunately, we cannot use the access call since we 3784329Seric ** won't necessarily be the real uid when we try to 3794329Seric ** actually open the file. 3804329Seric ** 3814329Seric ** Notice that ANY file with ANY execute bit is automatically 3824329Seric ** not writable. This is also enforced by mailfile. 3834329Seric ** 3844329Seric ** Parameters: 3854329Seric ** s -- pointer to a stat struct for the file. 3864329Seric ** 3874329Seric ** Returns: 3884329Seric ** TRUE -- if we will be able to write this file. 3894329Seric ** FALSE -- if we cannot write this file. 3904329Seric ** 3914329Seric ** Side Effects: 3924329Seric ** none. 3934329Seric */ 3944329Seric 3954329Seric bool 3964329Seric writable(s) 3974329Seric register struct stat *s; 3984329Seric { 3994329Seric int euid, egid; 4004329Seric int bits; 4014329Seric 4024329Seric if (bitset(0111, s->st_mode)) 4034329Seric return (FALSE); 4044329Seric euid = getruid(); 4054329Seric egid = getrgid(); 4064329Seric if (geteuid() == 0) 4074329Seric { 4084329Seric if (bitset(S_ISUID, s->st_mode)) 4094329Seric euid = s->st_uid; 4104329Seric if (bitset(S_ISGID, s->st_mode)) 4114329Seric egid = s->st_gid; 4124329Seric } 4134329Seric 4144329Seric if (euid == 0) 4154329Seric return (TRUE); 4164329Seric bits = S_IWRITE; 4174329Seric if (euid != s->st_uid) 4184329Seric { 4194329Seric bits >>= 3; 4204329Seric if (egid != s->st_gid) 4214329Seric bits >>= 3; 4224329Seric } 4234329Seric return ((s->st_mode & bits) != 0); 4244329Seric } 4254329Seric /* 4264174Seric ** INCLUDE -- handle :include: specification. 4274174Seric ** 4284174Seric ** Parameters: 4294174Seric ** fname -- filename to include. 4304176Seric ** msg -- message to print in verbose mode. 4314399Seric ** ctladdr -- address template to use to fill in these 4324399Seric ** addresses -- effective user/group id are 4334399Seric ** the important things. 4345006Seric ** sendq -- a pointer to the head of the send queue 4355006Seric ** to put these addresses in. 4364174Seric ** 4374174Seric ** Returns: 4384174Seric ** none. 4394174Seric ** 4404174Seric ** Side Effects: 4414174Seric ** reads the :include: file and sends to everyone 4424174Seric ** listed in that file. 4434174Seric */ 4444174Seric 4455006Seric include(fname, msg, ctladdr, sendq) 4464174Seric char *fname; 4474176Seric char *msg; 4484399Seric ADDRESS *ctladdr; 4495006Seric ADDRESS **sendq; 4504174Seric { 4514174Seric char buf[MAXLINE]; 4524174Seric register FILE *fp; 4536906Seric char *oldto = CurEnv->e_to; 4549379Seric char *oldfilename = FileName; 4559379Seric int oldlinenumber = LineNumber; 4564174Seric 4574174Seric fp = fopen(fname, "r"); 4584174Seric if (fp == NULL) 4594174Seric { 4604174Seric usrerr("Cannot open %s", fname); 4614174Seric return; 4624174Seric } 4634406Seric if (getctladdr(ctladdr) == NULL) 4644406Seric { 4654406Seric struct stat st; 4664174Seric 4674406Seric if (fstat(fileno(fp), &st) < 0) 4684406Seric syserr("Cannot fstat %s!", fname); 4694406Seric ctladdr->q_uid = st.st_uid; 4704406Seric ctladdr->q_gid = st.st_gid; 4714406Seric ctladdr->q_flags |= QGOODUID; 4724406Seric } 4734406Seric 4744174Seric /* read the file -- each line is a comma-separated list. */ 4759379Seric FileName = fname; 4769379Seric LineNumber = 0; 4774174Seric while (fgets(buf, sizeof buf, fp) != NULL) 4784174Seric { 4794174Seric register char *p = index(buf, '\n'); 4804174Seric 4814174Seric if (p != NULL) 4824174Seric *p = '\0'; 4834174Seric if (buf[0] == '\0') 4844174Seric continue; 4856906Seric CurEnv->e_to = oldto; 4867054Seric message(Arpa_Info, "%s to %s", msg, buf); 4874176Seric AliasLevel++; 4889622Seric sendtolist(buf, ctladdr, sendq); 4894176Seric AliasLevel--; 4904174Seric } 4914174Seric 4924319Seric (void) fclose(fp); 4939379Seric FileName = oldfilename; 4949379Seric LineNumber = oldlinenumber; 4954174Seric } 4964324Seric /* 4974324Seric ** SENDTOARGV -- send to an argument vector. 4984324Seric ** 4994324Seric ** Parameters: 5004324Seric ** argv -- argument vector to send to. 5014324Seric ** 5024324Seric ** Returns: 5034324Seric ** none. 5044324Seric ** 5054324Seric ** Side Effects: 5064324Seric ** puts all addresses on the argument vector onto the 5074324Seric ** send queue. 5084324Seric */ 5094324Seric 5104324Seric sendtoargv(argv) 5114324Seric register char **argv; 5124324Seric { 5134324Seric register char *p; 5144324Seric extern bool sameword(); 5154324Seric 5164324Seric while ((p = *argv++) != NULL) 5174324Seric { 5184324Seric if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at")) 5194324Seric { 5204324Seric char nbuf[MAXNAME]; 5214324Seric 5224324Seric if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf) 5234324Seric usrerr("address overflow"); 5244324Seric else 5254324Seric { 5264324Seric (void) strcpy(nbuf, p); 5274324Seric (void) strcat(nbuf, "@"); 5284324Seric (void) strcat(nbuf, argv[1]); 5294324Seric p = newstr(nbuf); 5304324Seric argv += 2; 5314324Seric } 5324324Seric } 5339622Seric sendtolist(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 5344324Seric } 5354324Seric } 5364399Seric /* 5374399Seric ** GETCTLADDR -- get controlling address from an address header. 5384399Seric ** 5394399Seric ** If none, get one corresponding to the effective userid. 5404399Seric ** 5414399Seric ** Parameters: 5424399Seric ** a -- the address to find the controller of. 5434399Seric ** 5444399Seric ** Returns: 5454399Seric ** the controlling address. 5464399Seric ** 5474399Seric ** Side Effects: 5484399Seric ** none. 5494399Seric */ 5504399Seric 5514399Seric ADDRESS * 5524399Seric getctladdr(a) 5534399Seric register ADDRESS *a; 5544399Seric { 5554404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 5564399Seric a = a->q_alias; 5574399Seric return (a); 5584399Seric } 559