14174Seric # include <pwd.h> 24329Seric # include <sys/types.h> 34329Seric # include <sys/stat.h> 44174Seric # include "sendmail.h" 54174Seric 6*4376Seric static char SccsId[] = "@(#)recipient.c 3.14 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; 2144373Seric extern struct passwd *finduser(); 2154373Seric 2164373Seric pw = finduser(buf); 2174174Seric if (pw == NULL) 2184174Seric { 2194174Seric a->q_flags |= QBADADDR; 2204174Seric giveresponse(EX_NOUSER, TRUE, m); 2214174Seric } 2224174Seric else 2234174Seric { 224*4376Seric if (strcmp(a->q_user, pw->pw_name) != 0) 225*4376Seric { 226*4376Seric a->q_user = newstr(pw->pw_name); 227*4376Seric strcpy(buf, pw->pw_name); 228*4376Seric } 2294174Seric a->q_home = newstr(pw->pw_dir); 2304213Seric a->q_uid = pw->pw_uid; 2314174Seric if (strcmp(buf, a->q_user) == 0) 2324174Seric forward(a); 2334174Seric } 2344174Seric } 2354174Seric } 2364174Seric } 2374174Seric /* 2384373Seric ** FINDUSER -- find the password entry for a user. 2394373Seric ** 2404373Seric ** This looks a lot like getpwnam, except that it may want to 2414373Seric ** do some fancier pattern matching in /etc/passwd. 2424373Seric ** 2434373Seric ** Parameters: 2444373Seric ** name -- the name to match against. 2454373Seric ** 2464373Seric ** Returns: 2474373Seric ** A pointer to a pw struct. 2484373Seric ** NULL if name is unknown or ambiguous. 2494373Seric ** 2504373Seric ** Side Effects: 2514373Seric ** none. 2524373Seric */ 2534373Seric 2544373Seric struct passwd * 2554373Seric finduser(name) 2564373Seric char *name; 2574373Seric { 258*4376Seric extern struct passwd *getpwent(); 259*4376Seric register struct passwd *pw; 2604373Seric 261*4376Seric setpwent(); 262*4376Seric while ((pw = getpwent()) != NULL) 263*4376Seric { 264*4376Seric char buf[MAXNAME]; 265*4376Seric register char *p; 266*4376Seric extern bool sameword(); 267*4376Seric 268*4376Seric if (strcmp(pw->pw_name, name) == 0) 269*4376Seric return (pw); 270*4376Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 271*4376Seric for (p = buf; (p = index(p, ' ')) != NULL; ) 272*4376Seric *p++ = SPACESUB & 0177; 273*4376Seric if (sameword(buf, name)) 274*4376Seric return (pw); 275*4376Seric } 276*4376Seric return (NULL); 2774373Seric } 2784373Seric /* 2794329Seric ** WRITABLE -- predicate returning if the file is writable. 2804329Seric ** 2814329Seric ** This routine must duplicate the algorithm in sys/fio.c. 2824329Seric ** Unfortunately, we cannot use the access call since we 2834329Seric ** won't necessarily be the real uid when we try to 2844329Seric ** actually open the file. 2854329Seric ** 2864329Seric ** Notice that ANY file with ANY execute bit is automatically 2874329Seric ** not writable. This is also enforced by mailfile. 2884329Seric ** 2894329Seric ** Parameters: 2904329Seric ** s -- pointer to a stat struct for the file. 2914329Seric ** 2924329Seric ** Returns: 2934329Seric ** TRUE -- if we will be able to write this file. 2944329Seric ** FALSE -- if we cannot write this file. 2954329Seric ** 2964329Seric ** Side Effects: 2974329Seric ** none. 2984329Seric */ 2994329Seric 3004329Seric bool 3014329Seric writable(s) 3024329Seric register struct stat *s; 3034329Seric { 3044329Seric int euid, egid; 3054329Seric int bits; 3064329Seric 3074329Seric if (bitset(0111, s->st_mode)) 3084329Seric return (FALSE); 3094329Seric euid = getruid(); 3104329Seric egid = getrgid(); 3114329Seric if (geteuid() == 0) 3124329Seric { 3134329Seric if (bitset(S_ISUID, s->st_mode)) 3144329Seric euid = s->st_uid; 3154329Seric if (bitset(S_ISGID, s->st_mode)) 3164329Seric egid = s->st_gid; 3174329Seric } 3184329Seric 3194329Seric if (euid == 0) 3204329Seric return (TRUE); 3214329Seric bits = S_IWRITE; 3224329Seric if (euid != s->st_uid) 3234329Seric { 3244329Seric bits >>= 3; 3254329Seric if (egid != s->st_gid) 3264329Seric bits >>= 3; 3274329Seric } 3284329Seric return ((s->st_mode & bits) != 0); 3294329Seric } 3304329Seric /* 3314174Seric ** INCLUDE -- handle :include: specification. 3324174Seric ** 3334174Seric ** Parameters: 3344174Seric ** fname -- filename to include. 3354176Seric ** msg -- message to print in verbose mode. 3364174Seric ** 3374174Seric ** Returns: 3384174Seric ** none. 3394174Seric ** 3404174Seric ** Side Effects: 3414174Seric ** reads the :include: file and sends to everyone 3424174Seric ** listed in that file. 3434174Seric */ 3444174Seric 3454176Seric include(fname, msg) 3464174Seric char *fname; 3474176Seric char *msg; 3484174Seric { 3494174Seric char buf[MAXLINE]; 3504174Seric register FILE *fp; 3514178Seric char *oldto = To; 3524174Seric 3534174Seric fp = fopen(fname, "r"); 3544174Seric if (fp == NULL) 3554174Seric { 3564174Seric usrerr("Cannot open %s", fname); 3574174Seric return; 3584174Seric } 3594174Seric 3604174Seric /* read the file -- each line is a comma-separated list. */ 3614174Seric while (fgets(buf, sizeof buf, fp) != NULL) 3624174Seric { 3634174Seric register char *p = index(buf, '\n'); 3644174Seric 3654174Seric if (p != NULL) 3664174Seric *p = '\0'; 3674174Seric if (buf[0] == '\0') 3684174Seric continue; 3694178Seric To = oldto; 3704174Seric if (Verbose) 3714176Seric message(Arpa_Info, "%s to %s", msg, buf); 3724176Seric AliasLevel++; 3734174Seric sendto(buf, 1); 3744176Seric AliasLevel--; 3754174Seric } 3764174Seric 3774319Seric (void) fclose(fp); 3784174Seric } 3794324Seric /* 3804324Seric ** SENDTOARGV -- send to an argument vector. 3814324Seric ** 3824324Seric ** Parameters: 3834324Seric ** argv -- argument vector to send to. 3844324Seric ** 3854324Seric ** Returns: 3864324Seric ** none. 3874324Seric ** 3884324Seric ** Side Effects: 3894324Seric ** puts all addresses on the argument vector onto the 3904324Seric ** send queue. 3914324Seric */ 3924324Seric 3934324Seric sendtoargv(argv) 3944324Seric register char **argv; 3954324Seric { 3964324Seric register char *p; 3974324Seric extern bool sameword(); 3984324Seric 3994324Seric while ((p = *argv++) != NULL) 4004324Seric { 4014324Seric if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at")) 4024324Seric { 4034324Seric char nbuf[MAXNAME]; 4044324Seric 4054324Seric if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf) 4064324Seric usrerr("address overflow"); 4074324Seric else 4084324Seric { 4094324Seric (void) strcpy(nbuf, p); 4104324Seric (void) strcat(nbuf, "@"); 4114324Seric (void) strcat(nbuf, argv[1]); 4124324Seric p = newstr(nbuf); 4134324Seric argv += 2; 4144324Seric } 4154324Seric } 4164324Seric sendto(p, 0); 4174324Seric } 4184324Seric } 419