14174Seric # include <pwd.h> 24627Seric # include "sendmail.h" 34329Seric # include <sys/stat.h> 44174Seric 5*5600Seric SCCSID(@(#)recipient.c 3.33 01/23/82); 64174Seric 74174Seric /* 84174Seric ** SENDTO -- 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. 154174Seric ** copyf -- the copy flag; passed to parse. 164399Seric ** ctladdr -- the address template for the person to 174399Seric ** send to -- effective uid/gid are important. 185006Seric ** This is typically the alias that caused this 195006Seric ** expansion. 205006Seric ** sendq -- a pointer to the head of a queue to put 215006Seric ** these people into. 224174Seric ** 234174Seric ** Returns: 244998Seric ** none 254174Seric ** 264174Seric ** Side Effects: 274174Seric ** none. 284174Seric */ 294174Seric 304174Seric # define MAXRCRSN 10 314174Seric 325006Seric sendto(list, copyf, ctladdr, sendq) 334174Seric char *list; 344174Seric int copyf; 354399Seric ADDRESS *ctladdr; 365198Seric ADDRESS **sendq; 374174Seric { 384174Seric register char *p; 394319Seric bool more; /* set if more addresses to send to */ 404324Seric ADDRESS *al; /* list of addresses to send to */ 414423Seric bool firstone; /* set on first address sent */ 424444Seric bool selfref; /* set if this list includes ctladdr */ 434174Seric 444324Seric # ifdef DEBUG 454324Seric if (Debug > 1) 464444Seric { 474444Seric printf("sendto: %s\n ctladdr=", list); 484444Seric printaddr(ctladdr, FALSE); 494444Seric } 504324Seric # endif DEBUG 514324Seric 524174Seric more = TRUE; 534423Seric firstone = TRUE; 544444Seric selfref = FALSE; 554324Seric al = NULL; 564174Seric for (p = list; more; ) 574174Seric { 584319Seric register char *q; 594319Seric register char c; 604319Seric ADDRESS *a; 614319Seric 624174Seric /* find the end of this address */ 634174Seric while (*p == ' ' || *p == '\t') 644174Seric p++; 654174Seric q = p; 664174Seric while ((c = *p++) != '\0' && c != ',' && c != '\n') 674174Seric continue; 684174Seric more = c != '\0'; 694174Seric *--p = '\0'; 704174Seric if (more) 714174Seric p++; 724324Seric if (*q == '\0') 734324Seric continue; 744174Seric 754174Seric /* parse the address */ 764174Seric if ((a = parse(q, (ADDRESS *) NULL, copyf)) == NULL) 774174Seric continue; 784324Seric a->q_next = al; 794399Seric a->q_alias = ctladdr; 804444Seric 814444Seric /* see if this should be marked as a primary address */ 824423Seric if (ctladdr == NULL || 834423Seric (firstone && !more && bitset(QPRIMARY, ctladdr->q_flags))) 844423Seric a->q_flags |= QPRIMARY; 854444Seric 864444Seric /* put on send queue or suppress self-reference */ 874444Seric if (ctladdr != NULL && sameaddr(ctladdr, a, FALSE)) 884444Seric selfref = TRUE; 894444Seric else 904444Seric al = a; 914423Seric firstone = FALSE; 924324Seric } 934324Seric 944444Seric /* if this alias doesn't include itself, delete ctladdr */ 954444Seric if (!selfref && ctladdr != NULL) 964444Seric ctladdr->q_flags |= QDONTSEND; 974444Seric 984324Seric /* arrange to send to everyone on the local send list */ 994324Seric while (al != NULL) 1004324Seric { 1014324Seric register ADDRESS *a = al; 1024324Seric 1034324Seric al = a->q_next; 1045006Seric recipient(a, sendq); 1054993Seric 1064998Seric /* arrange to inherit full name */ 1074998Seric if (a->q_fullname == NULL && ctladdr != NULL) 1084998Seric a->q_fullname = ctladdr->q_fullname; 1094174Seric } 1104324Seric 1114174Seric To = NULL; 1124174Seric } 1134174Seric /* 1144174Seric ** RECIPIENT -- Designate a message recipient 1154174Seric ** 1164174Seric ** Saves the named person for future mailing. 1174174Seric ** 1184174Seric ** Parameters: 1194174Seric ** a -- the (preparsed) address header for the recipient. 1205006Seric ** sendq -- a pointer to the head of a queue to put the 1215006Seric ** recipient in. Duplicate supression is done 1225006Seric ** in this queue. 1234174Seric ** 1244174Seric ** Returns: 1254998Seric ** none. 1264174Seric ** 1274174Seric ** Side Effects: 1284174Seric ** none. 1294174Seric */ 1304174Seric 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; 1384399Seric extern ADDRESS *getctladdr(); 1394627Seric extern bool safefile(); 1404174Seric 1414174Seric To = a->q_paddr; 1424600Seric m = a->q_mailer; 1434174Seric errno = 0; 1444174Seric # ifdef DEBUG 1454174Seric if (Debug) 1464444Seric { 1474444Seric printf("\nrecipient: "); 1484444Seric printaddr(a, FALSE); 1494444Seric } 1504174Seric # endif DEBUG 1514174Seric 1524174Seric /* break aliasing loops */ 1534174Seric if (AliasLevel > MAXRCRSN) 1544174Seric { 1554174Seric usrerr("aliasing/forwarding loop broken"); 1564998Seric return; 1574174Seric } 1584174Seric 1594174Seric /* 1604627Seric ** Finish setting up address structure. 1614174Seric */ 1624174Seric 1634627Seric a->q_timeout = TimeOut; 1644627Seric 1654627Seric /* do sickly crude mapping for program mailing, etc. */ 1664600Seric if (a->q_mailer == LocalMailer) 1674174Seric { 1684174Seric if (a->q_user[0] == '|') 1694174Seric { 1704600Seric a->q_mailer = m = ProgMailer; 1714174Seric a->q_user++; 1725316Seric if (a->q_alias == NULL && Debug == 0 && !QueueRun && !ForceMail) 1734217Seric { 1744217Seric usrerr("Cannot mail directly to programs"); 1754217Seric a->q_flags |= QDONTSEND; 1764217Seric } 1774174Seric } 1784174Seric } 1794174Seric 1804174Seric /* 1814419Seric ** Look up this person in the recipient list. 1824419Seric ** If they are there already, return, otherwise continue. 1834419Seric ** If the list is empty, just add it. Notice the cute 1844419Seric ** hack to make from addresses suppress things correctly: 1854419Seric ** the QDONTSEND bit will be set in the send list. 1864419Seric ** [Please note: the emphasis is on "hack."] 1874174Seric */ 1884174Seric 1895006Seric for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 1904174Seric { 1914319Seric if (!ForceMail && sameaddr(q, a, FALSE)) 1924174Seric { 1934174Seric # ifdef DEBUG 1944319Seric if (Debug) 1954444Seric { 1964444Seric printf("%s in sendq: ", a->q_paddr); 1974444Seric printaddr(q, FALSE); 1984444Seric } 1994174Seric # endif DEBUG 2004998Seric if (Verbose && !bitset(QDONTSEND, a->q_flags)) 2014324Seric message(Arpa_Info, "duplicate suppressed"); 2024423Seric if (!bitset(QPRIMARY, q->q_flags)) 2034423Seric q->q_flags |= a->q_flags; 2044998Seric return; 2054174Seric } 2064319Seric } 2074174Seric 2084319Seric /* add address on list */ 2094319Seric *pq = a; 2104174Seric a->q_next = NULL; 2114247Seric if (DontSend) 2124247Seric a->q_flags |= QDONTSEND; 2134174Seric 2144174Seric /* 2154174Seric ** Alias the name and handle :include: specs. 2164174Seric */ 2174174Seric 2184600Seric if (a->q_mailer == LocalMailer) 2194174Seric { 2204174Seric if (strncmp(a->q_user, ":include:", 9) == 0) 2214174Seric { 2224174Seric a->q_flags |= QDONTSEND; 2235316Seric if (a->q_alias == NULL && Debug == 0 && !QueueRun && !ForceMail) 2244399Seric usrerr("Cannot mail directly to :include:s"); 2254399Seric else 2264399Seric { 2274399Seric if (Verbose) 2284399Seric message(Arpa_Info, "including file %s", &a->q_user[9]); 2295006Seric include(&a->q_user[9], " sending", a, sendq); 2304399Seric } 2314174Seric } 2324174Seric else 2335006Seric alias(a, sendq); 2344174Seric } 2354174Seric 2364174Seric /* 2374174Seric ** If the user is local and still being sent, verify that 2384174Seric ** the address is good. If it is, try to forward. 2394174Seric ** If the address is already good, we have a forwarding 2404174Seric ** loop. This can be broken by just sending directly to 2414174Seric ** the user (which is probably correct anyway). 2424174Seric */ 2434174Seric 2444600Seric if (!bitset(QDONTSEND, a->q_flags) && a->q_mailer == LocalMailer) 2454174Seric { 2464174Seric char buf[MAXNAME]; 2474201Seric register char *p; 2484329Seric struct stat stb; 2494329Seric extern bool writable(); 2504399Seric bool quoted = FALSE; 2514174Seric 2524174Seric strcpy(buf, a->q_user); 2534399Seric for (p = buf; *p != '\0' && !quoted; p++) 2544399Seric { 2554998Seric if (!isascii(*p) && (*p & 0377) != (SPACESUB & 0377)) 2564399Seric quoted = TRUE; 2574399Seric } 2584174Seric stripquotes(buf, TRUE); 2594174Seric 2604174Seric /* see if this is to a file */ 261*5600Seric if (buf[0] == '/') 2624174Seric { 263*5600Seric p = rindex(buf, '/'); 2644201Seric /* check if writable or creatable */ 2655316Seric if (a->q_alias == NULL && Debug == 0 && !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; 2744174Seric giveresponse(EX_CANTCREAT, TRUE, m); 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; 2874174Seric giveresponse(EX_NOUSER, TRUE, m); 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); 2964376Seric 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 } 3104174Seric } 3114174Seric /* 3124373Seric ** FINDUSER -- find the password entry for a user. 3134373Seric ** 3144373Seric ** This looks a lot like getpwnam, except that it may want to 3154373Seric ** do some fancier pattern matching in /etc/passwd. 3164373Seric ** 3174373Seric ** Parameters: 3184373Seric ** name -- the name to match against. 3194373Seric ** 3204373Seric ** Returns: 3214373Seric ** A pointer to a pw struct. 3224373Seric ** NULL if name is unknown or ambiguous. 3234373Seric ** 3244373Seric ** Side Effects: 3254407Seric ** may modify name. 3264373Seric */ 3274373Seric 3284373Seric struct passwd * 3294373Seric finduser(name) 3304373Seric char *name; 3314373Seric { 3324376Seric extern struct passwd *getpwent(); 3334376Seric register struct passwd *pw; 3344407Seric register char *p; 3354373Seric 3364407Seric /* 3374407Seric ** Make name canonical. 3384407Seric */ 3394407Seric 3404407Seric for (p = name; *p != '\0'; p++) 3414407Seric { 3424407Seric if (*p == (SPACESUB & 0177) || *p == '_') 3434407Seric *p = ' '; 3444407Seric } 3454407Seric 3464376Seric setpwent(); 3474376Seric while ((pw = getpwent()) != NULL) 3484376Seric { 3494998Seric char buf[MAXNAME]; 3504993Seric extern bool sameword(); 3514376Seric 3524376Seric if (strcmp(pw->pw_name, name) == 0) 3534376Seric return (pw); 3544998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 3554407Seric if (index(buf, ' ') != NULL && sameword(buf, name)) 3564381Seric { 3574377Seric if (Verbose) 3584998Seric message(Arpa_Info, "sending to login name %s", 3594998Seric pw->pw_name); 3604376Seric return (pw); 3614377Seric } 3624376Seric } 3634376Seric return (NULL); 3644373Seric } 3654373Seric /* 3664329Seric ** WRITABLE -- predicate returning if the file is writable. 3674329Seric ** 3684329Seric ** This routine must duplicate the algorithm in sys/fio.c. 3694329Seric ** Unfortunately, we cannot use the access call since we 3704329Seric ** won't necessarily be the real uid when we try to 3714329Seric ** actually open the file. 3724329Seric ** 3734329Seric ** Notice that ANY file with ANY execute bit is automatically 3744329Seric ** not writable. This is also enforced by mailfile. 3754329Seric ** 3764329Seric ** Parameters: 3774329Seric ** s -- pointer to a stat struct for the file. 3784329Seric ** 3794329Seric ** Returns: 3804329Seric ** TRUE -- if we will be able to write this file. 3814329Seric ** FALSE -- if we cannot write this file. 3824329Seric ** 3834329Seric ** Side Effects: 3844329Seric ** none. 3854329Seric */ 3864329Seric 3874329Seric bool 3884329Seric writable(s) 3894329Seric register struct stat *s; 3904329Seric { 3914329Seric int euid, egid; 3924329Seric int bits; 3934329Seric 3944329Seric if (bitset(0111, s->st_mode)) 3954329Seric return (FALSE); 3964329Seric euid = getruid(); 3974329Seric egid = getrgid(); 3984329Seric if (geteuid() == 0) 3994329Seric { 4004329Seric if (bitset(S_ISUID, s->st_mode)) 4014329Seric euid = s->st_uid; 4024329Seric if (bitset(S_ISGID, s->st_mode)) 4034329Seric egid = s->st_gid; 4044329Seric } 4054329Seric 4064329Seric if (euid == 0) 4074329Seric return (TRUE); 4084329Seric bits = S_IWRITE; 4094329Seric if (euid != s->st_uid) 4104329Seric { 4114329Seric bits >>= 3; 4124329Seric if (egid != s->st_gid) 4134329Seric bits >>= 3; 4144329Seric } 4154329Seric return ((s->st_mode & bits) != 0); 4164329Seric } 4174329Seric /* 4184174Seric ** INCLUDE -- handle :include: specification. 4194174Seric ** 4204174Seric ** Parameters: 4214174Seric ** fname -- filename to include. 4224176Seric ** msg -- message to print in verbose mode. 4234399Seric ** ctladdr -- address template to use to fill in these 4244399Seric ** addresses -- effective user/group id are 4254399Seric ** the important things. 4265006Seric ** sendq -- a pointer to the head of the send queue 4275006Seric ** to put these addresses in. 4284174Seric ** 4294174Seric ** Returns: 4304174Seric ** none. 4314174Seric ** 4324174Seric ** Side Effects: 4334174Seric ** reads the :include: file and sends to everyone 4344174Seric ** listed in that file. 4354174Seric */ 4364174Seric 4375006Seric include(fname, msg, ctladdr, sendq) 4384174Seric char *fname; 4394176Seric char *msg; 4404399Seric ADDRESS *ctladdr; 4415006Seric ADDRESS **sendq; 4424174Seric { 4434174Seric char buf[MAXLINE]; 4444174Seric register FILE *fp; 4454178Seric char *oldto = To; 4464174Seric 4474174Seric fp = fopen(fname, "r"); 4484174Seric if (fp == NULL) 4494174Seric { 4504174Seric usrerr("Cannot open %s", fname); 4514174Seric return; 4524174Seric } 4534406Seric if (getctladdr(ctladdr) == NULL) 4544406Seric { 4554406Seric struct stat st; 4564174Seric 4574406Seric if (fstat(fileno(fp), &st) < 0) 4584406Seric syserr("Cannot fstat %s!", fname); 4594406Seric ctladdr->q_uid = st.st_uid; 4604406Seric ctladdr->q_gid = st.st_gid; 4614406Seric ctladdr->q_flags |= QGOODUID; 4624406Seric } 4634406Seric 4644174Seric /* read the file -- each line is a comma-separated list. */ 4654174Seric while (fgets(buf, sizeof buf, fp) != NULL) 4664174Seric { 4674174Seric register char *p = index(buf, '\n'); 4684174Seric 4694174Seric if (p != NULL) 4704174Seric *p = '\0'; 4714174Seric if (buf[0] == '\0') 4724174Seric continue; 4734178Seric To = oldto; 4744174Seric if (Verbose) 4754176Seric message(Arpa_Info, "%s to %s", msg, buf); 4764176Seric AliasLevel++; 4775006Seric sendto(buf, 1, ctladdr, sendq); 4784176Seric AliasLevel--; 4794174Seric } 4804174Seric 4814319Seric (void) fclose(fp); 4824174Seric } 4834324Seric /* 4844324Seric ** SENDTOARGV -- send to an argument vector. 4854324Seric ** 4864324Seric ** Parameters: 4874324Seric ** argv -- argument vector to send to. 4884324Seric ** 4894324Seric ** Returns: 4904324Seric ** none. 4914324Seric ** 4924324Seric ** Side Effects: 4934324Seric ** puts all addresses on the argument vector onto the 4944324Seric ** send queue. 4954324Seric */ 4964324Seric 4974324Seric sendtoargv(argv) 4984324Seric register char **argv; 4994324Seric { 5004324Seric register char *p; 5014324Seric extern bool sameword(); 5024324Seric 5034324Seric while ((p = *argv++) != NULL) 5044324Seric { 5054324Seric if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at")) 5064324Seric { 5074324Seric char nbuf[MAXNAME]; 5084324Seric 5094324Seric if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf) 5104324Seric usrerr("address overflow"); 5114324Seric else 5124324Seric { 5134324Seric (void) strcpy(nbuf, p); 5144324Seric (void) strcat(nbuf, "@"); 5154324Seric (void) strcat(nbuf, argv[1]); 5164324Seric p = newstr(nbuf); 5174324Seric argv += 2; 5184324Seric } 5194324Seric } 5205006Seric sendto(p, 0, (ADDRESS *) NULL, &SendQueue); 5214324Seric } 5224324Seric } 5234399Seric /* 5244399Seric ** GETCTLADDR -- get controlling address from an address header. 5254399Seric ** 5264399Seric ** If none, get one corresponding to the effective userid. 5274399Seric ** 5284399Seric ** Parameters: 5294399Seric ** a -- the address to find the controller of. 5304399Seric ** 5314399Seric ** Returns: 5324399Seric ** the controlling address. 5334399Seric ** 5344399Seric ** Side Effects: 5354399Seric ** none. 5364399Seric */ 5374399Seric 5384399Seric ADDRESS * 5394399Seric getctladdr(a) 5404399Seric register ADDRESS *a; 5414399Seric { 5424404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 5434399Seric a = a->q_alias; 5444399Seric return (a); 5454399Seric } 546