14174Seric # include <pwd.h> 24329Seric # include <sys/types.h> 34329Seric # include <sys/stat.h> 44174Seric # include "sendmail.h" 54174Seric 6*4373Seric static char SccsId[] = "@(#)recipient.c 3.13 09/12/81"; 74174Seric 84174Seric /* 94174Seric ** SENDTO -- Designate a send list. 104174Seric ** 114174Seric ** The parameter is a comma-separated list of people to send to. 124174Seric ** This routine arranges to send to all of them. 134174Seric ** 144174Seric ** Parameters: 154174Seric ** list -- the send list. 164174Seric ** copyf -- the copy flag; passed to parse. 174174Seric ** 184174Seric ** Returns: 194174Seric ** none 204174Seric ** 214174Seric ** Side Effects: 224174Seric ** none. 234174Seric */ 244174Seric 254174Seric # define MAXRCRSN 10 264174Seric 274174Seric sendto(list, copyf) 284174Seric char *list; 294174Seric int copyf; 304174Seric { 314174Seric register char *p; 324319Seric bool more; /* set if more addresses to send to */ 334324Seric ADDRESS *al; /* list of addresses to send to */ 344174Seric 354324Seric # ifdef DEBUG 364324Seric if (Debug > 1) 374324Seric printf("sendto: %s\n", list); 384324Seric # endif DEBUG 394324Seric 404174Seric more = TRUE; 414324Seric al = NULL; 424174Seric for (p = list; more; ) 434174Seric { 444319Seric register char *q; 454319Seric register char c; 464319Seric ADDRESS *a; 474319Seric 484174Seric /* find the end of this address */ 494174Seric while (*p == ' ' || *p == '\t') 504174Seric p++; 514174Seric q = p; 524174Seric while ((c = *p++) != '\0' && c != ',' && c != '\n') 534174Seric continue; 544174Seric more = c != '\0'; 554174Seric *--p = '\0'; 564174Seric if (more) 574174Seric p++; 584324Seric if (*q == '\0') 594324Seric continue; 604174Seric 614174Seric /* parse the address */ 624174Seric if ((a = parse(q, (ADDRESS *) NULL, copyf)) == NULL) 634174Seric continue; 644174Seric 654324Seric /* put it on the local send list */ 664324Seric a->q_next = al; 674324Seric al = a; 684324Seric } 694324Seric 704324Seric /* arrange to send to everyone on the local send list */ 714324Seric while (al != NULL) 724324Seric { 734324Seric register ADDRESS *a = al; 744324Seric 754324Seric al = a->q_next; 764174Seric recipient(a); 774174Seric } 784324Seric 794174Seric To = NULL; 804174Seric } 814174Seric /* 824174Seric ** RECIPIENT -- Designate a message recipient 834174Seric ** 844174Seric ** Saves the named person for future mailing. 854174Seric ** 864174Seric ** Parameters: 874174Seric ** a -- the (preparsed) address header for the recipient. 884174Seric ** 894174Seric ** Returns: 904174Seric ** none. 914174Seric ** 924174Seric ** Side Effects: 934174Seric ** none. 944174Seric */ 954174Seric 964174Seric recipient(a) 974174Seric register ADDRESS *a; 984174Seric { 994174Seric register ADDRESS *q; 1004319Seric ADDRESS **pq; 1014174Seric register struct mailer *m; 1024174Seric 1034174Seric To = a->q_paddr; 1044174Seric m = Mailer[a->q_mailer]; 1054174Seric errno = 0; 1064174Seric # ifdef DEBUG 1074174Seric if (Debug) 1084174Seric printf("recipient(%s)\n", To); 1094174Seric # endif DEBUG 1104174Seric 1114174Seric /* break aliasing loops */ 1124174Seric if (AliasLevel > MAXRCRSN) 1134174Seric { 1144174Seric usrerr("aliasing/forwarding loop broken"); 1154174Seric return; 1164174Seric } 1174174Seric 1184174Seric /* 1194174Seric ** Do sickly crude mapping for program mailing, etc. 1204174Seric */ 1214174Seric 1224197Seric if (a->q_mailer == MN_LOCAL) 1234174Seric { 1244174Seric if (a->q_user[0] == '|') 1254174Seric { 1264197Seric a->q_mailer = MN_PROG; 1274197Seric m = Mailer[MN_PROG]; 1284174Seric a->q_user++; 1294217Seric # ifdef PARANOID 1304217Seric if (AliasLevel <= 0) 1314217Seric { 1324217Seric usrerr("Cannot mail directly to programs"); 1334217Seric a->q_flags |= QDONTSEND; 1344217Seric } 1354217Seric # endif PARANOID 1364174Seric } 1374174Seric } 1384174Seric 1394174Seric /* 1404174Seric ** Look up this person in the recipient list. If they 1414174Seric ** are there already, return, otherwise continue. 1424174Seric ** If the list is empty, just add it. 1434174Seric */ 1444174Seric 1454319Seric for (pq = &m->m_sendq; (q = *pq) != NULL; pq = &q->q_next) 1464174Seric { 1474319Seric if (!ForceMail && sameaddr(q, a, FALSE)) 1484174Seric { 1494174Seric # ifdef DEBUG 1504319Seric if (Debug) 1514319Seric printf("(%s in sendq)\n", a->q_paddr); 1524174Seric # endif DEBUG 1534319Seric if (Verbose && !bitset(QDONTSEND, a->q_flags)) 1544324Seric message(Arpa_Info, "duplicate suppressed"); 1554319Seric return; 1564174Seric } 1574319Seric } 1584174Seric 1594319Seric /* add address on list */ 1604319Seric *pq = a; 1614174Seric a->q_next = NULL; 1624247Seric if (DontSend) 1634247Seric a->q_flags |= QDONTSEND; 1644174Seric 1654174Seric /* 1664174Seric ** Alias the name and handle :include: specs. 1674174Seric */ 1684174Seric 1694197Seric if (a->q_mailer == MN_LOCAL) 1704174Seric { 1714174Seric if (strncmp(a->q_user, ":include:", 9) == 0) 1724174Seric { 1734174Seric a->q_flags |= QDONTSEND; 1744176Seric if (Verbose) 1754176Seric message(Arpa_Info, "including file %s", &a->q_user[9]); 1764176Seric include(&a->q_user[9], " sending"); 1774174Seric } 1784174Seric else 1794174Seric alias(a); 1804174Seric } 1814174Seric 1824174Seric /* 1834174Seric ** If the user is local and still being sent, verify that 1844174Seric ** the address is good. If it is, try to forward. 1854174Seric ** If the address is already good, we have a forwarding 1864174Seric ** loop. This can be broken by just sending directly to 1874174Seric ** the user (which is probably correct anyway). 1884174Seric */ 1894174Seric 1904197Seric if (!bitset(QDONTSEND, a->q_flags) && a->q_mailer == MN_LOCAL) 1914174Seric { 1924174Seric char buf[MAXNAME]; 1934201Seric register char *p; 1944329Seric struct stat stb; 1954329Seric extern bool writable(); 1964174Seric 1974174Seric strcpy(buf, a->q_user); 1984174Seric stripquotes(buf, TRUE); 1994174Seric 2004174Seric /* see if this is to a file */ 2014201Seric if ((p = rindex(buf, '/')) != NULL) 2024174Seric { 2034201Seric /* check if writable or creatable */ 2044329Seric if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) : 2054201Seric (*p = '\0', access(buf, 3) < 0)) 2064174Seric { 2074174Seric a->q_flags |= QBADADDR; 2084174Seric giveresponse(EX_CANTCREAT, TRUE, m); 2094174Seric } 2104174Seric } 2114174Seric else 2124174Seric { 2134174Seric register struct passwd *pw; 214*4373Seric extern struct passwd *finduser(); 215*4373Seric 216*4373Seric pw = finduser(buf); 2174174Seric if (pw == NULL) 2184174Seric { 2194174Seric a->q_flags |= QBADADDR; 2204174Seric giveresponse(EX_NOUSER, TRUE, m); 2214174Seric } 2224174Seric else 2234174Seric { 2244174Seric a->q_home = newstr(pw->pw_dir); 2254213Seric a->q_uid = pw->pw_uid; 2264174Seric if (strcmp(buf, a->q_user) == 0) 2274174Seric forward(a); 2284174Seric } 2294174Seric } 2304174Seric } 2314174Seric } 2324174Seric /* 233*4373Seric ** FINDUSER -- find the password entry for a user. 234*4373Seric ** 235*4373Seric ** This looks a lot like getpwnam, except that it may want to 236*4373Seric ** do some fancier pattern matching in /etc/passwd. 237*4373Seric ** 238*4373Seric ** Parameters: 239*4373Seric ** name -- the name to match against. 240*4373Seric ** 241*4373Seric ** Returns: 242*4373Seric ** A pointer to a pw struct. 243*4373Seric ** NULL if name is unknown or ambiguous. 244*4373Seric ** 245*4373Seric ** Side Effects: 246*4373Seric ** none. 247*4373Seric */ 248*4373Seric 249*4373Seric struct passwd * 250*4373Seric finduser(name) 251*4373Seric char *name; 252*4373Seric { 253*4373Seric extern struct passwd *getpwnam(); 254*4373Seric 255*4373Seric return (getpwnam(name)); 256*4373Seric } 257*4373Seric /* 2584329Seric ** WRITABLE -- predicate returning if the file is writable. 2594329Seric ** 2604329Seric ** This routine must duplicate the algorithm in sys/fio.c. 2614329Seric ** Unfortunately, we cannot use the access call since we 2624329Seric ** won't necessarily be the real uid when we try to 2634329Seric ** actually open the file. 2644329Seric ** 2654329Seric ** Notice that ANY file with ANY execute bit is automatically 2664329Seric ** not writable. This is also enforced by mailfile. 2674329Seric ** 2684329Seric ** Parameters: 2694329Seric ** s -- pointer to a stat struct for the file. 2704329Seric ** 2714329Seric ** Returns: 2724329Seric ** TRUE -- if we will be able to write this file. 2734329Seric ** FALSE -- if we cannot write this file. 2744329Seric ** 2754329Seric ** Side Effects: 2764329Seric ** none. 2774329Seric */ 2784329Seric 2794329Seric bool 2804329Seric writable(s) 2814329Seric register struct stat *s; 2824329Seric { 2834329Seric int euid, egid; 2844329Seric int bits; 2854329Seric 2864329Seric if (bitset(0111, s->st_mode)) 2874329Seric return (FALSE); 2884329Seric euid = getruid(); 2894329Seric egid = getrgid(); 2904329Seric if (geteuid() == 0) 2914329Seric { 2924329Seric if (bitset(S_ISUID, s->st_mode)) 2934329Seric euid = s->st_uid; 2944329Seric if (bitset(S_ISGID, s->st_mode)) 2954329Seric egid = s->st_gid; 2964329Seric } 2974329Seric 2984329Seric if (euid == 0) 2994329Seric return (TRUE); 3004329Seric bits = S_IWRITE; 3014329Seric if (euid != s->st_uid) 3024329Seric { 3034329Seric bits >>= 3; 3044329Seric if (egid != s->st_gid) 3054329Seric bits >>= 3; 3064329Seric } 3074329Seric return ((s->st_mode & bits) != 0); 3084329Seric } 3094329Seric /* 3104174Seric ** INCLUDE -- handle :include: specification. 3114174Seric ** 3124174Seric ** Parameters: 3134174Seric ** fname -- filename to include. 3144176Seric ** msg -- message to print in verbose mode. 3154174Seric ** 3164174Seric ** Returns: 3174174Seric ** none. 3184174Seric ** 3194174Seric ** Side Effects: 3204174Seric ** reads the :include: file and sends to everyone 3214174Seric ** listed in that file. 3224174Seric */ 3234174Seric 3244176Seric include(fname, msg) 3254174Seric char *fname; 3264176Seric char *msg; 3274174Seric { 3284174Seric char buf[MAXLINE]; 3294174Seric register FILE *fp; 3304178Seric char *oldto = To; 3314174Seric 3324174Seric fp = fopen(fname, "r"); 3334174Seric if (fp == NULL) 3344174Seric { 3354174Seric usrerr("Cannot open %s", fname); 3364174Seric return; 3374174Seric } 3384174Seric 3394174Seric /* read the file -- each line is a comma-separated list. */ 3404174Seric while (fgets(buf, sizeof buf, fp) != NULL) 3414174Seric { 3424174Seric register char *p = index(buf, '\n'); 3434174Seric 3444174Seric if (p != NULL) 3454174Seric *p = '\0'; 3464174Seric if (buf[0] == '\0') 3474174Seric continue; 3484178Seric To = oldto; 3494174Seric if (Verbose) 3504176Seric message(Arpa_Info, "%s to %s", msg, buf); 3514176Seric AliasLevel++; 3524174Seric sendto(buf, 1); 3534176Seric AliasLevel--; 3544174Seric } 3554174Seric 3564319Seric (void) fclose(fp); 3574174Seric } 3584324Seric /* 3594324Seric ** SENDTOARGV -- send to an argument vector. 3604324Seric ** 3614324Seric ** Parameters: 3624324Seric ** argv -- argument vector to send to. 3634324Seric ** 3644324Seric ** Returns: 3654324Seric ** none. 3664324Seric ** 3674324Seric ** Side Effects: 3684324Seric ** puts all addresses on the argument vector onto the 3694324Seric ** send queue. 3704324Seric */ 3714324Seric 3724324Seric sendtoargv(argv) 3734324Seric register char **argv; 3744324Seric { 3754324Seric register char *p; 3764324Seric extern bool sameword(); 3774324Seric 3784324Seric while ((p = *argv++) != NULL) 3794324Seric { 3804324Seric if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at")) 3814324Seric { 3824324Seric char nbuf[MAXNAME]; 3834324Seric 3844324Seric if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf) 3854324Seric usrerr("address overflow"); 3864324Seric else 3874324Seric { 3884324Seric (void) strcpy(nbuf, p); 3894324Seric (void) strcat(nbuf, "@"); 3904324Seric (void) strcat(nbuf, argv[1]); 3914324Seric p = newstr(nbuf); 3924324Seric argv += 2; 3934324Seric } 3944324Seric } 3954324Seric sendto(p, 0); 3964324Seric } 3974324Seric } 398