14174Seric # include <pwd.h> 24627Seric # include "sendmail.h" 34329Seric # include <sys/stat.h> 44174Seric 5*8081Seric SCCSID(@(#)recipient.c 3.40 09/06/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. 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 31*8081Seric sendto(list, ctladdr, sendq) 324174Seric char *list; 334399Seric ADDRESS *ctladdr; 345198Seric ADDRESS **sendq; 354174Seric { 364174Seric register char *p; 37*8081Seric 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 */ 404174Seric 414324Seric # ifdef DEBUG 427676Seric if (tTd(25, 1)) 434444Seric { 444444Seric printf("sendto: %s\n ctladdr=", list); 454444Seric printaddr(ctladdr, FALSE); 464444Seric } 474324Seric # endif DEBUG 484324Seric 494423Seric firstone = TRUE; 504444Seric selfref = FALSE; 514324Seric al = NULL; 52*8081Seric for (p = list; *p != '\0'; ) 534174Seric { 54*8081Seric register ADDRESS *a; 55*8081Seric extern char *DelimChar; /* defined in prescan */ 564319Seric 57*8081Seric /* parse the address */ 58*8081Seric while (isspace(*p) || *p == ',') 594174Seric p++; 60*8081Seric if ((a = parse(p, (ADDRESS *) NULL, 1)) == NULL) 614174Seric continue; 62*8081Seric p = DelimChar; 634324Seric a->q_next = al; 644399Seric a->q_alias = ctladdr; 654444Seric 664444Seric /* see if this should be marked as a primary address */ 674423Seric if (ctladdr == NULL || 68*8081Seric (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags))) 694423Seric a->q_flags |= QPRIMARY; 704444Seric 714444Seric /* put on send queue or suppress self-reference */ 724444Seric if (ctladdr != NULL && sameaddr(ctladdr, a, FALSE)) 734444Seric selfref = TRUE; 744444Seric else 754444Seric al = a; 764423Seric firstone = FALSE; 774324Seric } 784324Seric 794444Seric /* if this alias doesn't include itself, delete ctladdr */ 804444Seric if (!selfref && ctladdr != NULL) 814444Seric ctladdr->q_flags |= QDONTSEND; 824444Seric 834324Seric /* arrange to send to everyone on the local send list */ 844324Seric while (al != NULL) 854324Seric { 864324Seric register ADDRESS *a = al; 874324Seric 884324Seric al = a->q_next; 895006Seric recipient(a, sendq); 904993Seric 914998Seric /* arrange to inherit full name */ 924998Seric if (a->q_fullname == NULL && ctladdr != NULL) 934998Seric a->q_fullname = ctladdr->q_fullname; 944174Seric } 954324Seric 966906Seric CurEnv->e_to = NULL; 974174Seric } 984174Seric /* 994174Seric ** RECIPIENT -- Designate a message recipient 1004174Seric ** 1014174Seric ** Saves the named person for future mailing. 1024174Seric ** 1034174Seric ** Parameters: 1044174Seric ** a -- the (preparsed) address header for the recipient. 1055006Seric ** sendq -- a pointer to the head of a queue to put the 1065006Seric ** recipient in. Duplicate supression is done 1075006Seric ** in this queue. 1084174Seric ** 1094174Seric ** Returns: 1104998Seric ** none. 1114174Seric ** 1124174Seric ** Side Effects: 1134174Seric ** none. 1144174Seric */ 1154174Seric 1165006Seric recipient(a, sendq) 1174174Seric register ADDRESS *a; 1185006Seric register ADDRESS **sendq; 1194174Seric { 1204174Seric register ADDRESS *q; 1214319Seric ADDRESS **pq; 1224174Seric register struct mailer *m; 1234399Seric extern ADDRESS *getctladdr(); 1244627Seric extern bool safefile(); 1254174Seric 1266906Seric CurEnv->e_to = a->q_paddr; 1274600Seric m = a->q_mailer; 1284174Seric errno = 0; 1294174Seric # ifdef DEBUG 1307676Seric if (tTd(26, 1)) 1314444Seric { 1324444Seric printf("\nrecipient: "); 1334444Seric printaddr(a, FALSE); 1344444Seric } 1354174Seric # endif DEBUG 1364174Seric 1374174Seric /* break aliasing loops */ 1384174Seric if (AliasLevel > MAXRCRSN) 1394174Seric { 1404174Seric usrerr("aliasing/forwarding loop broken"); 1414998Seric return; 1424174Seric } 1434174Seric 1444174Seric /* 1454627Seric ** Finish setting up address structure. 1464174Seric */ 1474174Seric 1484627Seric a->q_timeout = TimeOut; 1494627Seric 1504627Seric /* do sickly crude mapping for program mailing, etc. */ 1514600Seric if (a->q_mailer == LocalMailer) 1524174Seric { 1534174Seric if (a->q_user[0] == '|') 1544174Seric { 1554600Seric a->q_mailer = m = ProgMailer; 1564174Seric a->q_user++; 1577676Seric if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail) 1584217Seric { 1594217Seric usrerr("Cannot mail directly to programs"); 1604217Seric a->q_flags |= QDONTSEND; 1614217Seric } 1624174Seric } 1634174Seric } 1644174Seric 1654174Seric /* 1664419Seric ** Look up this person in the recipient list. 1674419Seric ** If they are there already, return, otherwise continue. 1684419Seric ** If the list is empty, just add it. Notice the cute 1694419Seric ** hack to make from addresses suppress things correctly: 1704419Seric ** the QDONTSEND bit will be set in the send list. 1714419Seric ** [Please note: the emphasis is on "hack."] 1724174Seric */ 1734174Seric 1745006Seric for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 1754174Seric { 1764319Seric if (!ForceMail && sameaddr(q, a, FALSE)) 1774174Seric { 1784174Seric # ifdef DEBUG 1797676Seric if (tTd(26, 1)) 1804444Seric { 1814444Seric printf("%s in sendq: ", a->q_paddr); 1824444Seric printaddr(q, FALSE); 1834444Seric } 1844174Seric # endif DEBUG 1857054Seric if (!bitset(QDONTSEND, a->q_flags)) 1864324Seric message(Arpa_Info, "duplicate suppressed"); 1874423Seric if (!bitset(QPRIMARY, q->q_flags)) 1884423Seric q->q_flags |= a->q_flags; 1894998Seric return; 1904174Seric } 1914319Seric } 1924174Seric 1934319Seric /* add address on list */ 1944319Seric *pq = a; 1954174Seric a->q_next = NULL; 1964247Seric if (DontSend) 1974247Seric a->q_flags |= QDONTSEND; 1984174Seric 1994174Seric /* 2004174Seric ** Alias the name and handle :include: specs. 2014174Seric */ 2024174Seric 2034600Seric if (a->q_mailer == LocalMailer) 2044174Seric { 2054174Seric if (strncmp(a->q_user, ":include:", 9) == 0) 2064174Seric { 2074174Seric a->q_flags |= QDONTSEND; 2087676Seric if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail) 2094399Seric usrerr("Cannot mail directly to :include:s"); 2104399Seric else 2114399Seric { 2127054Seric message(Arpa_Info, "including file %s", &a->q_user[9]); 2135006Seric include(&a->q_user[9], " sending", a, sendq); 2144399Seric } 2154174Seric } 2164174Seric else 2175006Seric alias(a, sendq); 2184174Seric } 2194174Seric 2204174Seric /* 2214174Seric ** If the user is local and still being sent, verify that 2224174Seric ** the address is good. If it is, try to forward. 2234174Seric ** If the address is already good, we have a forwarding 2244174Seric ** loop. This can be broken by just sending directly to 2254174Seric ** the user (which is probably correct anyway). 2264174Seric */ 2274174Seric 2284600Seric if (!bitset(QDONTSEND, a->q_flags) && a->q_mailer == LocalMailer) 2294174Seric { 2304174Seric char buf[MAXNAME]; 2314201Seric register char *p; 2324329Seric struct stat stb; 2334329Seric extern bool writable(); 2344399Seric bool quoted = FALSE; 2354174Seric 2367008Seric (void) strcpy(buf, a->q_user); 2374399Seric for (p = buf; *p != '\0' && !quoted; p++) 2384399Seric { 2394998Seric if (!isascii(*p) && (*p & 0377) != (SPACESUB & 0377)) 2404399Seric quoted = TRUE; 2414399Seric } 2424174Seric stripquotes(buf, TRUE); 2434174Seric 2444174Seric /* see if this is to a file */ 2455600Seric if (buf[0] == '/') 2464174Seric { 2475600Seric p = rindex(buf, '/'); 2484201Seric /* check if writable or creatable */ 2497676Seric if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail) 2504399Seric { 2514399Seric usrerr("Cannot mail directly to files"); 2524399Seric a->q_flags |= QDONTSEND; 2534399Seric } 2544399Seric else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) : 2554539Seric (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC))) 2564174Seric { 2574174Seric a->q_flags |= QBADADDR; 2584174Seric giveresponse(EX_CANTCREAT, TRUE, m); 2594174Seric } 2604174Seric } 2614174Seric else 2624174Seric { 2634174Seric register struct passwd *pw; 2644373Seric extern struct passwd *finduser(); 2654373Seric 2664407Seric /* warning -- finduser may trash buf */ 2674373Seric pw = finduser(buf); 2684174Seric if (pw == NULL) 2694174Seric { 2704174Seric a->q_flags |= QBADADDR; 2714174Seric giveresponse(EX_NOUSER, TRUE, m); 2724174Seric } 2734174Seric else 2744174Seric { 2754993Seric char nbuf[MAXNAME]; 2764993Seric 2774376Seric if (strcmp(a->q_user, pw->pw_name) != 0) 2784376Seric { 2794376Seric a->q_user = newstr(pw->pw_name); 2807008Seric (void) strcpy(buf, pw->pw_name); 2814376Seric } 2824174Seric a->q_home = newstr(pw->pw_dir); 2834213Seric a->q_uid = pw->pw_uid; 2844399Seric a->q_gid = pw->pw_gid; 2854404Seric a->q_flags |= QGOODUID; 2864998Seric buildfname(pw->pw_gecos, pw->pw_name, nbuf); 2874993Seric if (nbuf[0] != '\0') 2884993Seric a->q_fullname = newstr(nbuf); 2894399Seric if (!quoted) 2905006Seric forward(a, sendq); 2914174Seric } 2924174Seric } 2934174Seric } 2944174Seric } 2954174Seric /* 2964373Seric ** FINDUSER -- find the password entry for a user. 2974373Seric ** 2984373Seric ** This looks a lot like getpwnam, except that it may want to 2994373Seric ** do some fancier pattern matching in /etc/passwd. 3004373Seric ** 3014373Seric ** Parameters: 3024373Seric ** name -- the name to match against. 3034373Seric ** 3044373Seric ** Returns: 3054373Seric ** A pointer to a pw struct. 3064373Seric ** NULL if name is unknown or ambiguous. 3074373Seric ** 3084373Seric ** Side Effects: 3094407Seric ** may modify name. 3104373Seric */ 3114373Seric 3124373Seric struct passwd * 3134373Seric finduser(name) 3144373Seric char *name; 3154373Seric { 3164376Seric extern struct passwd *getpwent(); 3174376Seric register struct passwd *pw; 3184407Seric register char *p; 3194373Seric 3204407Seric /* 3214407Seric ** Make name canonical. 3224407Seric */ 3234407Seric 3244407Seric for (p = name; *p != '\0'; p++) 3254407Seric { 3264407Seric if (*p == (SPACESUB & 0177) || *p == '_') 3274407Seric *p = ' '; 3284407Seric } 3294407Seric 3304376Seric setpwent(); 3314376Seric while ((pw = getpwent()) != NULL) 3324376Seric { 3334998Seric char buf[MAXNAME]; 3344993Seric extern bool sameword(); 3354376Seric 3364376Seric if (strcmp(pw->pw_name, name) == 0) 3374376Seric return (pw); 3384998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 3394407Seric if (index(buf, ' ') != NULL && sameword(buf, name)) 3404381Seric { 3417054Seric message(Arpa_Info, "sending to login name %s", pw->pw_name); 3424376Seric return (pw); 3434377Seric } 3444376Seric } 3454376Seric return (NULL); 3464373Seric } 3474373Seric /* 3484329Seric ** WRITABLE -- predicate returning if the file is writable. 3494329Seric ** 3504329Seric ** This routine must duplicate the algorithm in sys/fio.c. 3514329Seric ** Unfortunately, we cannot use the access call since we 3524329Seric ** won't necessarily be the real uid when we try to 3534329Seric ** actually open the file. 3544329Seric ** 3554329Seric ** Notice that ANY file with ANY execute bit is automatically 3564329Seric ** not writable. This is also enforced by mailfile. 3574329Seric ** 3584329Seric ** Parameters: 3594329Seric ** s -- pointer to a stat struct for the file. 3604329Seric ** 3614329Seric ** Returns: 3624329Seric ** TRUE -- if we will be able to write this file. 3634329Seric ** FALSE -- if we cannot write this file. 3644329Seric ** 3654329Seric ** Side Effects: 3664329Seric ** none. 3674329Seric */ 3684329Seric 3694329Seric bool 3704329Seric writable(s) 3714329Seric register struct stat *s; 3724329Seric { 3734329Seric int euid, egid; 3744329Seric int bits; 3754329Seric 3764329Seric if (bitset(0111, s->st_mode)) 3774329Seric return (FALSE); 3784329Seric euid = getruid(); 3794329Seric egid = getrgid(); 3804329Seric if (geteuid() == 0) 3814329Seric { 3824329Seric if (bitset(S_ISUID, s->st_mode)) 3834329Seric euid = s->st_uid; 3844329Seric if (bitset(S_ISGID, s->st_mode)) 3854329Seric egid = s->st_gid; 3864329Seric } 3874329Seric 3884329Seric if (euid == 0) 3894329Seric return (TRUE); 3904329Seric bits = S_IWRITE; 3914329Seric if (euid != s->st_uid) 3924329Seric { 3934329Seric bits >>= 3; 3944329Seric if (egid != s->st_gid) 3954329Seric bits >>= 3; 3964329Seric } 3974329Seric return ((s->st_mode & bits) != 0); 3984329Seric } 3994329Seric /* 4004174Seric ** INCLUDE -- handle :include: specification. 4014174Seric ** 4024174Seric ** Parameters: 4034174Seric ** fname -- filename to include. 4044176Seric ** msg -- message to print in verbose mode. 4054399Seric ** ctladdr -- address template to use to fill in these 4064399Seric ** addresses -- effective user/group id are 4074399Seric ** the important things. 4085006Seric ** sendq -- a pointer to the head of the send queue 4095006Seric ** to put these addresses in. 4104174Seric ** 4114174Seric ** Returns: 4124174Seric ** none. 4134174Seric ** 4144174Seric ** Side Effects: 4154174Seric ** reads the :include: file and sends to everyone 4164174Seric ** listed in that file. 4174174Seric */ 4184174Seric 4195006Seric include(fname, msg, ctladdr, sendq) 4204174Seric char *fname; 4214176Seric char *msg; 4224399Seric ADDRESS *ctladdr; 4235006Seric ADDRESS **sendq; 4244174Seric { 4254174Seric char buf[MAXLINE]; 4264174Seric register FILE *fp; 4276906Seric char *oldto = CurEnv->e_to; 4284174Seric 4294174Seric fp = fopen(fname, "r"); 4304174Seric if (fp == NULL) 4314174Seric { 4324174Seric usrerr("Cannot open %s", fname); 4334174Seric return; 4344174Seric } 4354406Seric if (getctladdr(ctladdr) == NULL) 4364406Seric { 4374406Seric struct stat st; 4384174Seric 4394406Seric if (fstat(fileno(fp), &st) < 0) 4404406Seric syserr("Cannot fstat %s!", fname); 4414406Seric ctladdr->q_uid = st.st_uid; 4424406Seric ctladdr->q_gid = st.st_gid; 4434406Seric ctladdr->q_flags |= QGOODUID; 4444406Seric } 4454406Seric 4464174Seric /* read the file -- each line is a comma-separated list. */ 4474174Seric while (fgets(buf, sizeof buf, fp) != NULL) 4484174Seric { 4494174Seric register char *p = index(buf, '\n'); 4504174Seric 4514174Seric if (p != NULL) 4524174Seric *p = '\0'; 4534174Seric if (buf[0] == '\0') 4544174Seric continue; 4556906Seric CurEnv->e_to = oldto; 4567054Seric message(Arpa_Info, "%s to %s", msg, buf); 4574176Seric AliasLevel++; 458*8081Seric sendto(buf, ctladdr, sendq); 4594176Seric AliasLevel--; 4604174Seric } 4614174Seric 4624319Seric (void) fclose(fp); 4634174Seric } 4644324Seric /* 4654324Seric ** SENDTOARGV -- send to an argument vector. 4664324Seric ** 4674324Seric ** Parameters: 4684324Seric ** argv -- argument vector to send to. 4694324Seric ** 4704324Seric ** Returns: 4714324Seric ** none. 4724324Seric ** 4734324Seric ** Side Effects: 4744324Seric ** puts all addresses on the argument vector onto the 4754324Seric ** send queue. 4764324Seric */ 4774324Seric 4784324Seric sendtoargv(argv) 4794324Seric register char **argv; 4804324Seric { 4814324Seric register char *p; 4824324Seric extern bool sameword(); 4834324Seric 4844324Seric while ((p = *argv++) != NULL) 4854324Seric { 4864324Seric if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at")) 4874324Seric { 4884324Seric char nbuf[MAXNAME]; 4894324Seric 4904324Seric if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf) 4914324Seric usrerr("address overflow"); 4924324Seric else 4934324Seric { 4944324Seric (void) strcpy(nbuf, p); 4954324Seric (void) strcat(nbuf, "@"); 4964324Seric (void) strcat(nbuf, argv[1]); 4974324Seric p = newstr(nbuf); 4984324Seric argv += 2; 4994324Seric } 5004324Seric } 501*8081Seric sendto(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 5024324Seric } 5034324Seric } 5044399Seric /* 5054399Seric ** GETCTLADDR -- get controlling address from an address header. 5064399Seric ** 5074399Seric ** If none, get one corresponding to the effective userid. 5084399Seric ** 5094399Seric ** Parameters: 5104399Seric ** a -- the address to find the controller of. 5114399Seric ** 5124399Seric ** Returns: 5134399Seric ** the controlling address. 5144399Seric ** 5154399Seric ** Side Effects: 5164399Seric ** none. 5174399Seric */ 5184399Seric 5194399Seric ADDRESS * 5204399Seric getctladdr(a) 5214399Seric register ADDRESS *a; 5224399Seric { 5234404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 5244399Seric a = a->q_alias; 5254399Seric return (a); 5264399Seric } 527