14174Seric # include <pwd.h>
2*4329Seric # include <sys/types.h>
3*4329Seric # include <sys/stat.h>
44174Seric # include "sendmail.h"
54174Seric 
6*4329Seric static char SccsId[] = "@(#)recipient.c	3.12	09/07/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;
194*4329Seric 		struct stat stb;
195*4329Seric 		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 */
204*4329Seric 			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;
2144174Seric 			extern struct passwd *getpwnam();
2154174Seric 			pw = getpwnam(buf);
2164174Seric 			if (pw == NULL)
2174174Seric 			{
2184174Seric 				a->q_flags |= QBADADDR;
2194174Seric 				giveresponse(EX_NOUSER, TRUE, m);
2204174Seric 			}
2214174Seric 			else
2224174Seric 			{
2234174Seric 				a->q_home = newstr(pw->pw_dir);
2244213Seric 				a->q_uid = pw->pw_uid;
2254174Seric 				if (strcmp(buf, a->q_user) == 0)
2264174Seric 					forward(a);
2274174Seric 			}
2284174Seric 		}
2294174Seric 	}
2304174Seric }
2314174Seric /*
232*4329Seric **  WRITABLE -- predicate returning if the file is writable.
233*4329Seric **
234*4329Seric **	This routine must duplicate the algorithm in sys/fio.c.
235*4329Seric **	Unfortunately, we cannot use the access call since we
236*4329Seric **	won't necessarily be the real uid when we try to
237*4329Seric **	actually open the file.
238*4329Seric **
239*4329Seric **	Notice that ANY file with ANY execute bit is automatically
240*4329Seric **	not writable.  This is also enforced by mailfile.
241*4329Seric **
242*4329Seric **	Parameters:
243*4329Seric **		s -- pointer to a stat struct for the file.
244*4329Seric **
245*4329Seric **	Returns:
246*4329Seric **		TRUE -- if we will be able to write this file.
247*4329Seric **		FALSE -- if we cannot write this file.
248*4329Seric **
249*4329Seric **	Side Effects:
250*4329Seric **		none.
251*4329Seric */
252*4329Seric 
253*4329Seric bool
254*4329Seric writable(s)
255*4329Seric 	register struct stat *s;
256*4329Seric {
257*4329Seric 	int euid, egid;
258*4329Seric 	int bits;
259*4329Seric 
260*4329Seric 	if (bitset(0111, s->st_mode))
261*4329Seric 		return (FALSE);
262*4329Seric 	euid = getruid();
263*4329Seric 	egid = getrgid();
264*4329Seric 	if (geteuid() == 0)
265*4329Seric 	{
266*4329Seric 		if (bitset(S_ISUID, s->st_mode))
267*4329Seric 			euid = s->st_uid;
268*4329Seric 		if (bitset(S_ISGID, s->st_mode))
269*4329Seric 			egid = s->st_gid;
270*4329Seric 	}
271*4329Seric 
272*4329Seric 	if (euid == 0)
273*4329Seric 		return (TRUE);
274*4329Seric 	bits = S_IWRITE;
275*4329Seric 	if (euid != s->st_uid)
276*4329Seric 	{
277*4329Seric 		bits >>= 3;
278*4329Seric 		if (egid != s->st_gid)
279*4329Seric 			bits >>= 3;
280*4329Seric 	}
281*4329Seric 	return ((s->st_mode & bits) != 0);
282*4329Seric }
283*4329Seric /*
2844174Seric **  INCLUDE -- handle :include: specification.
2854174Seric **
2864174Seric **	Parameters:
2874174Seric **		fname -- filename to include.
2884176Seric **		msg -- message to print in verbose mode.
2894174Seric **
2904174Seric **	Returns:
2914174Seric **		none.
2924174Seric **
2934174Seric **	Side Effects:
2944174Seric **		reads the :include: file and sends to everyone
2954174Seric **		listed in that file.
2964174Seric */
2974174Seric 
2984176Seric include(fname, msg)
2994174Seric 	char *fname;
3004176Seric 	char *msg;
3014174Seric {
3024174Seric 	char buf[MAXLINE];
3034174Seric 	register FILE *fp;
3044178Seric 	char *oldto = To;
3054174Seric 
3064174Seric 	fp = fopen(fname, "r");
3074174Seric 	if (fp == NULL)
3084174Seric 	{
3094174Seric 		usrerr("Cannot open %s", fname);
3104174Seric 		return;
3114174Seric 	}
3124174Seric 
3134174Seric 	/* read the file -- each line is a comma-separated list. */
3144174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
3154174Seric 	{
3164174Seric 		register char *p = index(buf, '\n');
3174174Seric 
3184174Seric 		if (p != NULL)
3194174Seric 			*p = '\0';
3204174Seric 		if (buf[0] == '\0')
3214174Seric 			continue;
3224178Seric 		To = oldto;
3234174Seric 		if (Verbose)
3244176Seric 			message(Arpa_Info, "%s to %s", msg, buf);
3254176Seric 		AliasLevel++;
3264174Seric 		sendto(buf, 1);
3274176Seric 		AliasLevel--;
3284174Seric 	}
3294174Seric 
3304319Seric 	(void) fclose(fp);
3314174Seric }
3324324Seric /*
3334324Seric **  SENDTOARGV -- send to an argument vector.
3344324Seric **
3354324Seric **	Parameters:
3364324Seric **		argv -- argument vector to send to.
3374324Seric **
3384324Seric **	Returns:
3394324Seric **		none.
3404324Seric **
3414324Seric **	Side Effects:
3424324Seric **		puts all addresses on the argument vector onto the
3434324Seric **			send queue.
3444324Seric */
3454324Seric 
3464324Seric sendtoargv(argv)
3474324Seric 	register char **argv;
3484324Seric {
3494324Seric 	register char *p;
3504324Seric 	extern bool sameword();
3514324Seric 
3524324Seric 	while ((p = *argv++) != NULL)
3534324Seric 	{
3544324Seric 		if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at"))
3554324Seric 		{
3564324Seric 			char nbuf[MAXNAME];
3574324Seric 
3584324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
3594324Seric 				usrerr("address overflow");
3604324Seric 			else
3614324Seric 			{
3624324Seric 				(void) strcpy(nbuf, p);
3634324Seric 				(void) strcat(nbuf, "@");
3644324Seric 				(void) strcat(nbuf, argv[1]);
3654324Seric 				p = newstr(nbuf);
3664324Seric 				argv += 2;
3674324Seric 			}
3684324Seric 		}
3694324Seric 		sendto(p, 0);
3704324Seric 	}
3714324Seric }
372