14174Seric # include <pwd.h>
24329Seric # include <sys/types.h>
34329Seric # include <sys/stat.h>
44174Seric # include "sendmail.h"
54174Seric 
6*4600Seric static char SccsId[] = "@(#)recipient.c	3.27	10/23/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.
174399Seric **		ctladdr -- the address template for the person to
184399Seric **			send to -- effective uid/gid are important.
194174Seric **
204174Seric **	Returns:
214174Seric **		none
224174Seric **
234174Seric **	Side Effects:
244174Seric **		none.
254174Seric */
264174Seric 
274174Seric # define MAXRCRSN	10
284174Seric 
294399Seric sendto(list, copyf, ctladdr)
304174Seric 	char *list;
314174Seric 	int copyf;
324399Seric 	ADDRESS *ctladdr;
334174Seric {
344174Seric 	register char *p;
354319Seric 	bool more;		/* set if more addresses to send to */
364324Seric 	ADDRESS *al;		/* list of addresses to send to */
374423Seric 	bool firstone;		/* set on first address sent */
384444Seric 	bool selfref;		/* set if this list includes ctladdr */
394174Seric 
404324Seric # ifdef DEBUG
414324Seric 	if (Debug > 1)
424444Seric 	{
434444Seric 		printf("sendto: %s\n   ctladdr=", list);
444444Seric 		printaddr(ctladdr, FALSE);
454444Seric 	}
464324Seric # endif DEBUG
474324Seric 
484174Seric 	more = TRUE;
494423Seric 	firstone = TRUE;
504444Seric 	selfref = FALSE;
514324Seric 	al = NULL;
524174Seric 	for (p = list; more; )
534174Seric 	{
544319Seric 		register char *q;
554319Seric 		register char c;
564319Seric 		ADDRESS *a;
574319Seric 
584174Seric 		/* find the end of this address */
594174Seric 		while (*p == ' ' || *p == '\t')
604174Seric 			p++;
614174Seric 		q = p;
624174Seric 		while ((c = *p++) != '\0' && c != ',' && c != '\n')
634174Seric 			continue;
644174Seric 		more = c != '\0';
654174Seric 		*--p = '\0';
664174Seric 		if (more)
674174Seric 			p++;
684324Seric 		if (*q == '\0')
694324Seric 			continue;
704174Seric 
714174Seric 		/* parse the address */
724174Seric 		if ((a = parse(q, (ADDRESS *) NULL, copyf)) == NULL)
734174Seric 			continue;
744324Seric 		a->q_next = al;
754399Seric 		a->q_alias = ctladdr;
764444Seric 
774444Seric 		/* see if this should be marked as a primary address */
784423Seric 		if (ctladdr == NULL ||
794423Seric 		    (firstone && !more && bitset(QPRIMARY, ctladdr->q_flags)))
804423Seric 			a->q_flags |= QPRIMARY;
814444Seric 
824444Seric 		/* put on send queue or suppress self-reference */
834444Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a, FALSE))
844444Seric 			selfref = TRUE;
854444Seric 		else
864444Seric 			al = a;
874423Seric 		firstone = FALSE;
884324Seric 	}
894324Seric 
904444Seric 	/* if this alias doesn't include itself, delete ctladdr */
914444Seric 	if (!selfref && ctladdr != NULL)
924444Seric 		ctladdr->q_flags |= QDONTSEND;
934444Seric 
944324Seric 	/* arrange to send to everyone on the local send list */
954324Seric 	while (al != NULL)
964324Seric 	{
974324Seric 		register ADDRESS *a = al;
984324Seric 
994324Seric 		al = a->q_next;
1004174Seric 		recipient(a);
1014174Seric 	}
1024324Seric 
1034174Seric 	To = NULL;
1044174Seric }
1054174Seric /*
1064174Seric **  RECIPIENT -- Designate a message recipient
1074174Seric **
1084174Seric **	Saves the named person for future mailing.
1094174Seric **
1104174Seric **	Parameters:
1114174Seric **		a -- the (preparsed) address header for the recipient.
1124174Seric **
1134174Seric **	Returns:
1144174Seric **		none.
1154174Seric **
1164174Seric **	Side Effects:
1174174Seric **		none.
1184174Seric */
1194174Seric 
1204174Seric recipient(a)
1214174Seric 	register ADDRESS *a;
1224174Seric {
1234174Seric 	register ADDRESS *q;
1244319Seric 	ADDRESS **pq;
1254174Seric 	register struct mailer *m;
1264399Seric 	extern ADDRESS *getctladdr();
1274174Seric 
1284174Seric 	To = a->q_paddr;
129*4600Seric 	m = a->q_mailer;
1304174Seric 	errno = 0;
1314174Seric # ifdef DEBUG
1324174Seric 	if (Debug)
1334444Seric 	{
1344444Seric 		printf("\nrecipient: ");
1354444Seric 		printaddr(a, FALSE);
1364444Seric 	}
1374174Seric # endif DEBUG
1384174Seric 
1394174Seric 	/* break aliasing loops */
1404174Seric 	if (AliasLevel > MAXRCRSN)
1414174Seric 	{
1424174Seric 		usrerr("aliasing/forwarding loop broken");
1434174Seric 		return;
1444174Seric 	}
1454174Seric 
1464174Seric 	/*
1474174Seric 	**  Do sickly crude mapping for program mailing, etc.
1484174Seric 	*/
1494174Seric 
150*4600Seric 	if (a->q_mailer == LocalMailer)
1514174Seric 	{
1524174Seric 		if (a->q_user[0] == '|')
1534174Seric 		{
154*4600Seric 			a->q_mailer = m = ProgMailer;
1554174Seric 			a->q_user++;
1564406Seric 			if (a->q_alias == NULL && Debug == 0)
1574217Seric 			{
1584217Seric 				usrerr("Cannot mail directly to programs");
1594217Seric 				a->q_flags |= QDONTSEND;
1604217Seric 			}
1614174Seric 		}
1624174Seric 	}
1634174Seric 
1644174Seric 	/*
1654419Seric 	**  Look up this person in the recipient list.
1664419Seric 	**	If they are there already, return, otherwise continue.
1674419Seric 	**	If the list is empty, just add it.  Notice the cute
1684419Seric 	**	hack to make from addresses suppress things correctly:
1694419Seric 	**	the QDONTSEND bit will be set in the send list.
1704419Seric 	**	[Please note: the emphasis is on "hack."]
1714174Seric 	*/
1724174Seric 
1734319Seric 	for (pq = &m->m_sendq; (q = *pq) != NULL; pq = &q->q_next)
1744174Seric 	{
1754319Seric 		if (!ForceMail && sameaddr(q, a, FALSE))
1764174Seric 		{
1774174Seric # ifdef DEBUG
1784319Seric 			if (Debug)
1794444Seric 			{
1804444Seric 				printf("%s in sendq: ", a->q_paddr);
1814444Seric 				printaddr(q, FALSE);
1824444Seric 			}
1834174Seric # endif DEBUG
1844319Seric 			if (Verbose && !bitset(QDONTSEND, a->q_flags))
1854324Seric 				message(Arpa_Info, "duplicate suppressed");
1864423Seric 			if (!bitset(QPRIMARY, q->q_flags))
1874423Seric 				q->q_flags |= a->q_flags;
1884319Seric 			return;
1894174Seric 		}
1904319Seric 	}
1914174Seric 
1924319Seric 	/* add address on list */
1934319Seric 	*pq = a;
1944174Seric 	a->q_next = NULL;
1954247Seric 	if (DontSend)
1964247Seric 		a->q_flags |= QDONTSEND;
1974174Seric 
1984174Seric 	/*
1994174Seric 	**  Alias the name and handle :include: specs.
2004174Seric 	*/
2014174Seric 
202*4600Seric 	if (a->q_mailer == LocalMailer)
2034174Seric 	{
2044174Seric 		if (strncmp(a->q_user, ":include:", 9) == 0)
2054174Seric 		{
2064174Seric 			a->q_flags |= QDONTSEND;
2074406Seric 			if (a->q_alias == NULL && Debug == 0)
2084399Seric 				usrerr("Cannot mail directly to :include:s");
2094399Seric 			else
2104399Seric 			{
2114399Seric 				if (Verbose)
2124399Seric 					message(Arpa_Info, "including file %s", &a->q_user[9]);
2134399Seric 				include(&a->q_user[9], " sending", a);
2144399Seric 			}
2154174Seric 		}
2164174Seric 		else
2174174Seric 			alias(a);
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 
228*4600Seric 	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 
2364174Seric 		strcpy(buf, a->q_user);
2374399Seric 		for (p = buf; *p != '\0' && !quoted; p++)
2384399Seric 		{
2394399Seric 			if (!isascii(*p))
2404399Seric 				quoted = TRUE;
2414399Seric 		}
2424174Seric 		stripquotes(buf, TRUE);
2434174Seric 
2444174Seric 		/* see if this is to a file */
2454201Seric 		if ((p = rindex(buf, '/')) != NULL)
2464174Seric 		{
2474201Seric 			/* check if writable or creatable */
2484406Seric 			if (a->q_alias == NULL && Debug == 0)
2494399Seric 			{
2504399Seric 				usrerr("Cannot mail directly to files");
2514399Seric 				a->q_flags |= QDONTSEND;
2524399Seric 			}
2534399Seric 			else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
2544539Seric 			    (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
2554174Seric 			{
2564174Seric 				a->q_flags |= QBADADDR;
2574174Seric 				giveresponse(EX_CANTCREAT, TRUE, m);
2584174Seric 			}
2594174Seric 		}
2604174Seric 		else
2614174Seric 		{
2624174Seric 			register struct passwd *pw;
2634373Seric 			extern struct passwd *finduser();
2644373Seric 
2654407Seric 			/* warning -- finduser may trash buf */
2664373Seric 			pw = finduser(buf);
2674174Seric 			if (pw == NULL)
2684174Seric 			{
2694174Seric 				a->q_flags |= QBADADDR;
2704174Seric 				giveresponse(EX_NOUSER, TRUE, m);
2714174Seric 			}
2724174Seric 			else
2734174Seric 			{
2744376Seric 				if (strcmp(a->q_user, pw->pw_name) != 0)
2754376Seric 				{
2764376Seric 					a->q_user = newstr(pw->pw_name);
2774376Seric 					strcpy(buf, pw->pw_name);
2784376Seric 				}
2794174Seric 				a->q_home = newstr(pw->pw_dir);
2804213Seric 				a->q_uid = pw->pw_uid;
2814399Seric 				a->q_gid = pw->pw_gid;
2824404Seric 				a->q_flags |= QGOODUID;
2834399Seric 				if (!quoted)
2844174Seric 					forward(a);
2854174Seric 			}
2864174Seric 		}
2874174Seric 	}
2884174Seric }
2894174Seric /*
2904373Seric **  FINDUSER -- find the password entry for a user.
2914373Seric **
2924373Seric **	This looks a lot like getpwnam, except that it may want to
2934373Seric **	do some fancier pattern matching in /etc/passwd.
2944373Seric **
2954373Seric **	Parameters:
2964373Seric **		name -- the name to match against.
2974373Seric **
2984373Seric **	Returns:
2994373Seric **		A pointer to a pw struct.
3004373Seric **		NULL if name is unknown or ambiguous.
3014373Seric **
3024373Seric **	Side Effects:
3034407Seric **		may modify name.
3044373Seric */
3054373Seric 
3064373Seric struct passwd *
3074373Seric finduser(name)
3084373Seric 	char *name;
3094373Seric {
3104376Seric 	extern struct passwd *getpwent();
3114376Seric 	register struct passwd *pw;
3124407Seric 	register char *p;
3134373Seric 
3144407Seric 	/*
3154407Seric 	**  Make name canonical.
3164407Seric 	*/
3174407Seric 
3184407Seric 	for (p = name; *p != '\0'; p++)
3194407Seric 	{
3204407Seric 		if (*p == (SPACESUB & 0177) || *p == '_')
3214407Seric 			*p = ' ';
3224407Seric 	}
3234407Seric 
3244376Seric 	setpwent();
3254376Seric 	while ((pw = getpwent()) != NULL)
3264376Seric 	{
3274376Seric 		char buf[MAXNAME];
3284376Seric 		extern bool sameword();
3294376Seric 
3304376Seric 		if (strcmp(pw->pw_name, name) == 0)
3314376Seric 			return (pw);
3324376Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
3334407Seric 		if (index(buf, ' ') != NULL && sameword(buf, name))
3344381Seric 		{
3354377Seric 			if (Verbose)
3364381Seric 				message(Arpa_Info, "sending to login name %s",
3374381Seric 				    pw->pw_name);
3384376Seric 			return (pw);
3394377Seric 		}
3404376Seric 	}
3414376Seric 	return (NULL);
3424373Seric }
3434373Seric /*
3444329Seric **  WRITABLE -- predicate returning if the file is writable.
3454329Seric **
3464329Seric **	This routine must duplicate the algorithm in sys/fio.c.
3474329Seric **	Unfortunately, we cannot use the access call since we
3484329Seric **	won't necessarily be the real uid when we try to
3494329Seric **	actually open the file.
3504329Seric **
3514329Seric **	Notice that ANY file with ANY execute bit is automatically
3524329Seric **	not writable.  This is also enforced by mailfile.
3534329Seric **
3544329Seric **	Parameters:
3554329Seric **		s -- pointer to a stat struct for the file.
3564329Seric **
3574329Seric **	Returns:
3584329Seric **		TRUE -- if we will be able to write this file.
3594329Seric **		FALSE -- if we cannot write this file.
3604329Seric **
3614329Seric **	Side Effects:
3624329Seric **		none.
3634329Seric */
3644329Seric 
3654329Seric bool
3664329Seric writable(s)
3674329Seric 	register struct stat *s;
3684329Seric {
3694329Seric 	int euid, egid;
3704329Seric 	int bits;
3714329Seric 
3724329Seric 	if (bitset(0111, s->st_mode))
3734329Seric 		return (FALSE);
3744329Seric 	euid = getruid();
3754329Seric 	egid = getrgid();
3764329Seric 	if (geteuid() == 0)
3774329Seric 	{
3784329Seric 		if (bitset(S_ISUID, s->st_mode))
3794329Seric 			euid = s->st_uid;
3804329Seric 		if (bitset(S_ISGID, s->st_mode))
3814329Seric 			egid = s->st_gid;
3824329Seric 	}
3834329Seric 
3844329Seric 	if (euid == 0)
3854329Seric 		return (TRUE);
3864329Seric 	bits = S_IWRITE;
3874329Seric 	if (euid != s->st_uid)
3884329Seric 	{
3894329Seric 		bits >>= 3;
3904329Seric 		if (egid != s->st_gid)
3914329Seric 			bits >>= 3;
3924329Seric 	}
3934329Seric 	return ((s->st_mode & bits) != 0);
3944329Seric }
3954329Seric /*
3964174Seric **  INCLUDE -- handle :include: specification.
3974174Seric **
3984174Seric **	Parameters:
3994174Seric **		fname -- filename to include.
4004176Seric **		msg -- message to print in verbose mode.
4014399Seric **		ctladdr -- address template to use to fill in these
4024399Seric **			addresses -- effective user/group id are
4034399Seric **			the important things.
4044174Seric **
4054174Seric **	Returns:
4064174Seric **		none.
4074174Seric **
4084174Seric **	Side Effects:
4094174Seric **		reads the :include: file and sends to everyone
4104174Seric **		listed in that file.
4114174Seric */
4124174Seric 
4134399Seric include(fname, msg, ctladdr)
4144174Seric 	char *fname;
4154176Seric 	char *msg;
4164399Seric 	ADDRESS *ctladdr;
4174174Seric {
4184174Seric 	char buf[MAXLINE];
4194174Seric 	register FILE *fp;
4204178Seric 	char *oldto = To;
4214174Seric 
4224174Seric 	fp = fopen(fname, "r");
4234174Seric 	if (fp == NULL)
4244174Seric 	{
4254174Seric 		usrerr("Cannot open %s", fname);
4264174Seric 		return;
4274174Seric 	}
4284406Seric 	if (getctladdr(ctladdr) == NULL)
4294406Seric 	{
4304406Seric 		struct stat st;
4314174Seric 
4324406Seric 		if (fstat(fileno(fp), &st) < 0)
4334406Seric 			syserr("Cannot fstat %s!", fname);
4344406Seric 		ctladdr->q_uid = st.st_uid;
4354406Seric 		ctladdr->q_gid = st.st_gid;
4364406Seric 		ctladdr->q_flags |= QGOODUID;
4374406Seric 	}
4384406Seric 
4394174Seric 	/* read the file -- each line is a comma-separated list. */
4404174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
4414174Seric 	{
4424174Seric 		register char *p = index(buf, '\n');
4434174Seric 
4444174Seric 		if (p != NULL)
4454174Seric 			*p = '\0';
4464174Seric 		if (buf[0] == '\0')
4474174Seric 			continue;
4484178Seric 		To = oldto;
4494174Seric 		if (Verbose)
4504176Seric 			message(Arpa_Info, "%s to %s", msg, buf);
4514176Seric 		AliasLevel++;
4524399Seric 		sendto(buf, 1, ctladdr);
4534176Seric 		AliasLevel--;
4544174Seric 	}
4554174Seric 
4564319Seric 	(void) fclose(fp);
4574174Seric }
4584324Seric /*
4594324Seric **  SENDTOARGV -- send to an argument vector.
4604324Seric **
4614324Seric **	Parameters:
4624324Seric **		argv -- argument vector to send to.
4634324Seric **
4644324Seric **	Returns:
4654324Seric **		none.
4664324Seric **
4674324Seric **	Side Effects:
4684324Seric **		puts all addresses on the argument vector onto the
4694324Seric **			send queue.
4704324Seric */
4714324Seric 
4724324Seric sendtoargv(argv)
4734324Seric 	register char **argv;
4744324Seric {
4754324Seric 	register char *p;
4764324Seric 	extern bool sameword();
4774324Seric 
4784324Seric 	while ((p = *argv++) != NULL)
4794324Seric 	{
4804324Seric 		if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at"))
4814324Seric 		{
4824324Seric 			char nbuf[MAXNAME];
4834324Seric 
4844324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
4854324Seric 				usrerr("address overflow");
4864324Seric 			else
4874324Seric 			{
4884324Seric 				(void) strcpy(nbuf, p);
4894324Seric 				(void) strcat(nbuf, "@");
4904324Seric 				(void) strcat(nbuf, argv[1]);
4914324Seric 				p = newstr(nbuf);
4924324Seric 				argv += 2;
4934324Seric 			}
4944324Seric 		}
4954402Seric 		sendto(p, 0, NULL);
4964324Seric 	}
4974324Seric }
4984399Seric /*
4994399Seric **  GETCTLADDR -- get controlling address from an address header.
5004399Seric **
5014399Seric **	If none, get one corresponding to the effective userid.
5024399Seric **
5034399Seric **	Parameters:
5044399Seric **		a -- the address to find the controller of.
5054399Seric **
5064399Seric **	Returns:
5074399Seric **		the controlling address.
5084399Seric **
5094399Seric **	Side Effects:
5104399Seric **		none.
5114399Seric */
5124399Seric 
5134399Seric ADDRESS *
5144399Seric getctladdr(a)
5154399Seric 	register ADDRESS *a;
5164399Seric {
5174404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
5184399Seric 		a = a->q_alias;
5194399Seric 	return (a);
5204399Seric }
521