14174Seric # include <pwd.h>
24627Seric # include "sendmail.h"
34329Seric # include <sys/stat.h>
44174Seric 
5*8223Seric SCCSID(@(#)recipient.c	3.41		09/12/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 
318081Seric sendto(list, ctladdr, sendq)
324174Seric 	char *list;
334399Seric 	ADDRESS *ctladdr;
345198Seric 	ADDRESS **sendq;
354174Seric {
364174Seric 	register char *p;
37*8223Seric 	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 
49*8223Seric 	/* heuristic to determine old versus new style addresses */
50*8223Seric 	if (index(list, ',') != NULL || index(list, ';') != NULL ||
51*8223Seric 	    index(list, '<') != NULL || index(list, '(') != NULL)
52*8223Seric 		CurEnv->e_oldstyle = FALSE;
53*8223Seric 
544423Seric 	firstone = TRUE;
554444Seric 	selfref = FALSE;
564324Seric 	al = NULL;
57*8223Seric 
588081Seric 	for (p = list; *p != '\0'; )
594174Seric 	{
608081Seric 		register ADDRESS *a;
618081Seric 		extern char *DelimChar;		/* defined in prescan */
624319Seric 
638081Seric 		/* parse the address */
648081Seric 		while (isspace(*p) || *p == ',')
654174Seric 			p++;
668081Seric 		if ((a = parse(p, (ADDRESS *) NULL, 1)) == NULL)
674174Seric 			continue;
688081Seric 		p = DelimChar;
694324Seric 		a->q_next = al;
704399Seric 		a->q_alias = ctladdr;
714444Seric 
724444Seric 		/* see if this should be marked as a primary address */
734423Seric 		if (ctladdr == NULL ||
748081Seric 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
754423Seric 			a->q_flags |= QPRIMARY;
764444Seric 
774444Seric 		/* put on send queue or suppress self-reference */
784444Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a, FALSE))
794444Seric 			selfref = TRUE;
804444Seric 		else
814444Seric 			al = a;
824423Seric 		firstone = FALSE;
834324Seric 	}
844324Seric 
854444Seric 	/* if this alias doesn't include itself, delete ctladdr */
864444Seric 	if (!selfref && ctladdr != NULL)
874444Seric 		ctladdr->q_flags |= QDONTSEND;
884444Seric 
894324Seric 	/* arrange to send to everyone on the local send list */
904324Seric 	while (al != NULL)
914324Seric 	{
924324Seric 		register ADDRESS *a = al;
934324Seric 
944324Seric 		al = a->q_next;
955006Seric 		recipient(a, sendq);
964993Seric 
974998Seric 		/* arrange to inherit full name */
984998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
994998Seric 			a->q_fullname = ctladdr->q_fullname;
1004174Seric 	}
1014324Seric 
1026906Seric 	CurEnv->e_to = NULL;
1034174Seric }
1044174Seric /*
1054174Seric **  RECIPIENT -- Designate a message recipient
1064174Seric **
1074174Seric **	Saves the named person for future mailing.
1084174Seric **
1094174Seric **	Parameters:
1104174Seric **		a -- the (preparsed) address header for the recipient.
1115006Seric **		sendq -- a pointer to the head of a queue to put the
1125006Seric **			recipient in.  Duplicate supression is done
1135006Seric **			in this queue.
1144174Seric **
1154174Seric **	Returns:
1164998Seric **		none.
1174174Seric **
1184174Seric **	Side Effects:
1194174Seric **		none.
1204174Seric */
1214174Seric 
1225006Seric recipient(a, sendq)
1234174Seric 	register ADDRESS *a;
1245006Seric 	register ADDRESS **sendq;
1254174Seric {
1264174Seric 	register ADDRESS *q;
1274319Seric 	ADDRESS **pq;
1284174Seric 	register struct mailer *m;
1294399Seric 	extern ADDRESS *getctladdr();
1304627Seric 	extern bool safefile();
1314174Seric 
1326906Seric 	CurEnv->e_to = a->q_paddr;
1334600Seric 	m = a->q_mailer;
1344174Seric 	errno = 0;
1354174Seric # ifdef DEBUG
1367676Seric 	if (tTd(26, 1))
1374444Seric 	{
1384444Seric 		printf("\nrecipient: ");
1394444Seric 		printaddr(a, FALSE);
1404444Seric 	}
1414174Seric # endif DEBUG
1424174Seric 
1434174Seric 	/* break aliasing loops */
1444174Seric 	if (AliasLevel > MAXRCRSN)
1454174Seric 	{
1464174Seric 		usrerr("aliasing/forwarding loop broken");
1474998Seric 		return;
1484174Seric 	}
1494174Seric 
1504174Seric 	/*
1514627Seric 	**  Finish setting up address structure.
1524174Seric 	*/
1534174Seric 
1544627Seric 	a->q_timeout = TimeOut;
1554627Seric 
1564627Seric 	/* do sickly crude mapping for program mailing, etc. */
1574600Seric 	if (a->q_mailer == LocalMailer)
1584174Seric 	{
1594174Seric 		if (a->q_user[0] == '|')
1604174Seric 		{
1614600Seric 			a->q_mailer = m = ProgMailer;
1624174Seric 			a->q_user++;
1637676Seric 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
1644217Seric 			{
1654217Seric 				usrerr("Cannot mail directly to programs");
1664217Seric 				a->q_flags |= QDONTSEND;
1674217Seric 			}
1684174Seric 		}
1694174Seric 	}
1704174Seric 
1714174Seric 	/*
1724419Seric 	**  Look up this person in the recipient list.
1734419Seric 	**	If they are there already, return, otherwise continue.
1744419Seric 	**	If the list is empty, just add it.  Notice the cute
1754419Seric 	**	hack to make from addresses suppress things correctly:
1764419Seric 	**	the QDONTSEND bit will be set in the send list.
1774419Seric 	**	[Please note: the emphasis is on "hack."]
1784174Seric 	*/
1794174Seric 
1805006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
1814174Seric 	{
1824319Seric 		if (!ForceMail && sameaddr(q, a, FALSE))
1834174Seric 		{
1844174Seric # ifdef DEBUG
1857676Seric 			if (tTd(26, 1))
1864444Seric 			{
1874444Seric 				printf("%s in sendq: ", a->q_paddr);
1884444Seric 				printaddr(q, FALSE);
1894444Seric 			}
1904174Seric # endif DEBUG
1917054Seric 			if (!bitset(QDONTSEND, a->q_flags))
1924324Seric 				message(Arpa_Info, "duplicate suppressed");
1934423Seric 			if (!bitset(QPRIMARY, q->q_flags))
1944423Seric 				q->q_flags |= a->q_flags;
1954998Seric 			return;
1964174Seric 		}
1974319Seric 	}
1984174Seric 
1994319Seric 	/* add address on list */
2004319Seric 	*pq = a;
2014174Seric 	a->q_next = NULL;
2024247Seric 	if (DontSend)
2034247Seric 		a->q_flags |= QDONTSEND;
2044174Seric 
2054174Seric 	/*
2064174Seric 	**  Alias the name and handle :include: specs.
2074174Seric 	*/
2084174Seric 
2094600Seric 	if (a->q_mailer == LocalMailer)
2104174Seric 	{
2114174Seric 		if (strncmp(a->q_user, ":include:", 9) == 0)
2124174Seric 		{
2134174Seric 			a->q_flags |= QDONTSEND;
2147676Seric 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
2154399Seric 				usrerr("Cannot mail directly to :include:s");
2164399Seric 			else
2174399Seric 			{
2187054Seric 				message(Arpa_Info, "including file %s", &a->q_user[9]);
2195006Seric 				include(&a->q_user[9], " sending", a, sendq);
2204399Seric 			}
2214174Seric 		}
2224174Seric 		else
2235006Seric 			alias(a, sendq);
2244174Seric 	}
2254174Seric 
2264174Seric 	/*
2274174Seric 	**  If the user is local and still being sent, verify that
2284174Seric 	**  the address is good.  If it is, try to forward.
2294174Seric 	**  If the address is already good, we have a forwarding
2304174Seric 	**  loop.  This can be broken by just sending directly to
2314174Seric 	**  the user (which is probably correct anyway).
2324174Seric 	*/
2334174Seric 
2344600Seric 	if (!bitset(QDONTSEND, a->q_flags) && a->q_mailer == LocalMailer)
2354174Seric 	{
2364174Seric 		char buf[MAXNAME];
2374201Seric 		register char *p;
2384329Seric 		struct stat stb;
2394329Seric 		extern bool writable();
2404399Seric 		bool quoted = FALSE;
2414174Seric 
2427008Seric 		(void) strcpy(buf, a->q_user);
2434399Seric 		for (p = buf; *p != '\0' && !quoted; p++)
2444399Seric 		{
2454998Seric 			if (!isascii(*p) && (*p & 0377) != (SPACESUB & 0377))
2464399Seric 				quoted = TRUE;
2474399Seric 		}
2484174Seric 		stripquotes(buf, TRUE);
2494174Seric 
2504174Seric 		/* see if this is to a file */
2515600Seric 		if (buf[0] == '/')
2524174Seric 		{
2535600Seric 			p = rindex(buf, '/');
2544201Seric 			/* check if writable or creatable */
2557676Seric 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
2564399Seric 			{
2574399Seric 				usrerr("Cannot mail directly to files");
2584399Seric 				a->q_flags |= QDONTSEND;
2594399Seric 			}
2604399Seric 			else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
2614539Seric 			    (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
2624174Seric 			{
2634174Seric 				a->q_flags |= QBADADDR;
2644174Seric 				giveresponse(EX_CANTCREAT, TRUE, m);
2654174Seric 			}
2664174Seric 		}
2674174Seric 		else
2684174Seric 		{
2694174Seric 			register struct passwd *pw;
2704373Seric 			extern struct passwd *finduser();
2714373Seric 
2724407Seric 			/* warning -- finduser may trash buf */
2734373Seric 			pw = finduser(buf);
2744174Seric 			if (pw == NULL)
2754174Seric 			{
2764174Seric 				a->q_flags |= QBADADDR;
2774174Seric 				giveresponse(EX_NOUSER, TRUE, m);
2784174Seric 			}
2794174Seric 			else
2804174Seric 			{
2814993Seric 				char nbuf[MAXNAME];
2824993Seric 
2834376Seric 				if (strcmp(a->q_user, pw->pw_name) != 0)
2844376Seric 				{
2854376Seric 					a->q_user = newstr(pw->pw_name);
2867008Seric 					(void) strcpy(buf, pw->pw_name);
2874376Seric 				}
2884174Seric 				a->q_home = newstr(pw->pw_dir);
2894213Seric 				a->q_uid = pw->pw_uid;
2904399Seric 				a->q_gid = pw->pw_gid;
2914404Seric 				a->q_flags |= QGOODUID;
2924998Seric 				buildfname(pw->pw_gecos, pw->pw_name, nbuf);
2934993Seric 				if (nbuf[0] != '\0')
2944993Seric 					a->q_fullname = newstr(nbuf);
2954399Seric 				if (!quoted)
2965006Seric 					forward(a, sendq);
2974174Seric 			}
2984174Seric 		}
2994174Seric 	}
3004174Seric }
3014174Seric /*
3024373Seric **  FINDUSER -- find the password entry for a user.
3034373Seric **
3044373Seric **	This looks a lot like getpwnam, except that it may want to
3054373Seric **	do some fancier pattern matching in /etc/passwd.
3064373Seric **
3074373Seric **	Parameters:
3084373Seric **		name -- the name to match against.
3094373Seric **
3104373Seric **	Returns:
3114373Seric **		A pointer to a pw struct.
3124373Seric **		NULL if name is unknown or ambiguous.
3134373Seric **
3144373Seric **	Side Effects:
3154407Seric **		may modify name.
3164373Seric */
3174373Seric 
3184373Seric struct passwd *
3194373Seric finduser(name)
3204373Seric 	char *name;
3214373Seric {
3224376Seric 	extern struct passwd *getpwent();
3234376Seric 	register struct passwd *pw;
3244407Seric 	register char *p;
3254373Seric 
3264407Seric 	/*
3274407Seric 	**  Make name canonical.
3284407Seric 	*/
3294407Seric 
3304407Seric 	for (p = name; *p != '\0'; p++)
3314407Seric 	{
3324407Seric 		if (*p == (SPACESUB & 0177) || *p == '_')
3334407Seric 			*p = ' ';
3344407Seric 	}
3354407Seric 
3364376Seric 	setpwent();
3374376Seric 	while ((pw = getpwent()) != NULL)
3384376Seric 	{
3394998Seric 		char buf[MAXNAME];
3404993Seric 		extern bool sameword();
3414376Seric 
3424376Seric 		if (strcmp(pw->pw_name, name) == 0)
3434376Seric 			return (pw);
3444998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
3454407Seric 		if (index(buf, ' ') != NULL && sameword(buf, name))
3464381Seric 		{
3477054Seric 			message(Arpa_Info, "sending to login name %s", pw->pw_name);
3484376Seric 			return (pw);
3494377Seric 		}
3504376Seric 	}
3514376Seric 	return (NULL);
3524373Seric }
3534373Seric /*
3544329Seric **  WRITABLE -- predicate returning if the file is writable.
3554329Seric **
3564329Seric **	This routine must duplicate the algorithm in sys/fio.c.
3574329Seric **	Unfortunately, we cannot use the access call since we
3584329Seric **	won't necessarily be the real uid when we try to
3594329Seric **	actually open the file.
3604329Seric **
3614329Seric **	Notice that ANY file with ANY execute bit is automatically
3624329Seric **	not writable.  This is also enforced by mailfile.
3634329Seric **
3644329Seric **	Parameters:
3654329Seric **		s -- pointer to a stat struct for the file.
3664329Seric **
3674329Seric **	Returns:
3684329Seric **		TRUE -- if we will be able to write this file.
3694329Seric **		FALSE -- if we cannot write this file.
3704329Seric **
3714329Seric **	Side Effects:
3724329Seric **		none.
3734329Seric */
3744329Seric 
3754329Seric bool
3764329Seric writable(s)
3774329Seric 	register struct stat *s;
3784329Seric {
3794329Seric 	int euid, egid;
3804329Seric 	int bits;
3814329Seric 
3824329Seric 	if (bitset(0111, s->st_mode))
3834329Seric 		return (FALSE);
3844329Seric 	euid = getruid();
3854329Seric 	egid = getrgid();
3864329Seric 	if (geteuid() == 0)
3874329Seric 	{
3884329Seric 		if (bitset(S_ISUID, s->st_mode))
3894329Seric 			euid = s->st_uid;
3904329Seric 		if (bitset(S_ISGID, s->st_mode))
3914329Seric 			egid = s->st_gid;
3924329Seric 	}
3934329Seric 
3944329Seric 	if (euid == 0)
3954329Seric 		return (TRUE);
3964329Seric 	bits = S_IWRITE;
3974329Seric 	if (euid != s->st_uid)
3984329Seric 	{
3994329Seric 		bits >>= 3;
4004329Seric 		if (egid != s->st_gid)
4014329Seric 			bits >>= 3;
4024329Seric 	}
4034329Seric 	return ((s->st_mode & bits) != 0);
4044329Seric }
4054329Seric /*
4064174Seric **  INCLUDE -- handle :include: specification.
4074174Seric **
4084174Seric **	Parameters:
4094174Seric **		fname -- filename to include.
4104176Seric **		msg -- message to print in verbose mode.
4114399Seric **		ctladdr -- address template to use to fill in these
4124399Seric **			addresses -- effective user/group id are
4134399Seric **			the important things.
4145006Seric **		sendq -- a pointer to the head of the send queue
4155006Seric **			to put these addresses in.
4164174Seric **
4174174Seric **	Returns:
4184174Seric **		none.
4194174Seric **
4204174Seric **	Side Effects:
4214174Seric **		reads the :include: file and sends to everyone
4224174Seric **		listed in that file.
4234174Seric */
4244174Seric 
4255006Seric include(fname, msg, ctladdr, sendq)
4264174Seric 	char *fname;
4274176Seric 	char *msg;
4284399Seric 	ADDRESS *ctladdr;
4295006Seric 	ADDRESS **sendq;
4304174Seric {
4314174Seric 	char buf[MAXLINE];
4324174Seric 	register FILE *fp;
4336906Seric 	char *oldto = CurEnv->e_to;
4344174Seric 
4354174Seric 	fp = fopen(fname, "r");
4364174Seric 	if (fp == NULL)
4374174Seric 	{
4384174Seric 		usrerr("Cannot open %s", fname);
4394174Seric 		return;
4404174Seric 	}
4414406Seric 	if (getctladdr(ctladdr) == NULL)
4424406Seric 	{
4434406Seric 		struct stat st;
4444174Seric 
4454406Seric 		if (fstat(fileno(fp), &st) < 0)
4464406Seric 			syserr("Cannot fstat %s!", fname);
4474406Seric 		ctladdr->q_uid = st.st_uid;
4484406Seric 		ctladdr->q_gid = st.st_gid;
4494406Seric 		ctladdr->q_flags |= QGOODUID;
4504406Seric 	}
4514406Seric 
4524174Seric 	/* read the file -- each line is a comma-separated list. */
4534174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
4544174Seric 	{
4554174Seric 		register char *p = index(buf, '\n');
4564174Seric 
4574174Seric 		if (p != NULL)
4584174Seric 			*p = '\0';
4594174Seric 		if (buf[0] == '\0')
4604174Seric 			continue;
4616906Seric 		CurEnv->e_to = oldto;
4627054Seric 		message(Arpa_Info, "%s to %s", msg, buf);
4634176Seric 		AliasLevel++;
4648081Seric 		sendto(buf, ctladdr, sendq);
4654176Seric 		AliasLevel--;
4664174Seric 	}
4674174Seric 
4684319Seric 	(void) fclose(fp);
4694174Seric }
4704324Seric /*
4714324Seric **  SENDTOARGV -- send to an argument vector.
4724324Seric **
4734324Seric **	Parameters:
4744324Seric **		argv -- argument vector to send to.
4754324Seric **
4764324Seric **	Returns:
4774324Seric **		none.
4784324Seric **
4794324Seric **	Side Effects:
4804324Seric **		puts all addresses on the argument vector onto the
4814324Seric **			send queue.
4824324Seric */
4834324Seric 
4844324Seric sendtoargv(argv)
4854324Seric 	register char **argv;
4864324Seric {
4874324Seric 	register char *p;
4884324Seric 	extern bool sameword();
4894324Seric 
4904324Seric 	while ((p = *argv++) != NULL)
4914324Seric 	{
4924324Seric 		if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at"))
4934324Seric 		{
4944324Seric 			char nbuf[MAXNAME];
4954324Seric 
4964324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
4974324Seric 				usrerr("address overflow");
4984324Seric 			else
4994324Seric 			{
5004324Seric 				(void) strcpy(nbuf, p);
5014324Seric 				(void) strcat(nbuf, "@");
5024324Seric 				(void) strcat(nbuf, argv[1]);
5034324Seric 				p = newstr(nbuf);
5044324Seric 				argv += 2;
5054324Seric 			}
5064324Seric 		}
5078081Seric 		sendto(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
5084324Seric 	}
5094324Seric }
5104399Seric /*
5114399Seric **  GETCTLADDR -- get controlling address from an address header.
5124399Seric **
5134399Seric **	If none, get one corresponding to the effective userid.
5144399Seric **
5154399Seric **	Parameters:
5164399Seric **		a -- the address to find the controller of.
5174399Seric **
5184399Seric **	Returns:
5194399Seric **		the controlling address.
5204399Seric **
5214399Seric **	Side Effects:
5224399Seric **		none.
5234399Seric */
5244399Seric 
5254399Seric ADDRESS *
5264399Seric getctladdr(a)
5274399Seric 	register ADDRESS *a;
5284399Seric {
5294404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
5304399Seric 		a = a->q_alias;
5314399Seric 	return (a);
5324399Seric }
533