14174Seric # include <pwd.h>
24627Seric # include "sendmail.h"
34329Seric # include <sys/stat.h>
44174Seric 
5*5006Seric static char SccsId[] = "@(#)recipient.c	3.30	11/21/81";
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.
154174Seric **		copyf -- the copy flag; passed to parse.
164399Seric **		ctladdr -- the address template for the person to
174399Seric **			send to -- effective uid/gid are important.
18*5006Seric **			This is typically the alias that caused this
19*5006Seric **			expansion.
20*5006Seric **		sendq -- a pointer to the head of a queue to put
21*5006Seric **			these people into.
224174Seric **
234174Seric **	Returns:
244998Seric **		none
254174Seric **
264174Seric **	Side Effects:
274174Seric **		none.
284174Seric */
294174Seric 
304174Seric # define MAXRCRSN	10
314174Seric 
32*5006Seric sendto(list, copyf, ctladdr, sendq)
334174Seric 	char *list;
344174Seric 	int copyf;
354399Seric 	ADDRESS *ctladdr;
364174Seric {
374174Seric 	register char *p;
384319Seric 	bool more;		/* set if more addresses to send to */
394324Seric 	ADDRESS *al;		/* list of addresses to send to */
404423Seric 	bool firstone;		/* set on first address sent */
414444Seric 	bool selfref;		/* set if this list includes ctladdr */
424174Seric 
434324Seric # ifdef DEBUG
444324Seric 	if (Debug > 1)
454444Seric 	{
464444Seric 		printf("sendto: %s\n   ctladdr=", list);
474444Seric 		printaddr(ctladdr, FALSE);
484444Seric 	}
494324Seric # endif DEBUG
504324Seric 
514174Seric 	more = TRUE;
524423Seric 	firstone = TRUE;
534444Seric 	selfref = FALSE;
544324Seric 	al = NULL;
554174Seric 	for (p = list; more; )
564174Seric 	{
574319Seric 		register char *q;
584319Seric 		register char c;
594319Seric 		ADDRESS *a;
604319Seric 
614174Seric 		/* find the end of this address */
624174Seric 		while (*p == ' ' || *p == '\t')
634174Seric 			p++;
644174Seric 		q = p;
654174Seric 		while ((c = *p++) != '\0' && c != ',' && c != '\n')
664174Seric 			continue;
674174Seric 		more = c != '\0';
684174Seric 		*--p = '\0';
694174Seric 		if (more)
704174Seric 			p++;
714324Seric 		if (*q == '\0')
724324Seric 			continue;
734174Seric 
744174Seric 		/* parse the address */
754174Seric 		if ((a = parse(q, (ADDRESS *) NULL, copyf)) == NULL)
764174Seric 			continue;
774324Seric 		a->q_next = al;
784399Seric 		a->q_alias = ctladdr;
794444Seric 
804444Seric 		/* see if this should be marked as a primary address */
814423Seric 		if (ctladdr == NULL ||
824423Seric 		    (firstone && !more && bitset(QPRIMARY, ctladdr->q_flags)))
834423Seric 			a->q_flags |= QPRIMARY;
844444Seric 
854444Seric 		/* put on send queue or suppress self-reference */
864444Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a, FALSE))
874444Seric 			selfref = TRUE;
884444Seric 		else
894444Seric 			al = a;
904423Seric 		firstone = FALSE;
914324Seric 	}
924324Seric 
934444Seric 	/* if this alias doesn't include itself, delete ctladdr */
944444Seric 	if (!selfref && ctladdr != NULL)
954444Seric 		ctladdr->q_flags |= QDONTSEND;
964444Seric 
974324Seric 	/* arrange to send to everyone on the local send list */
984324Seric 	while (al != NULL)
994324Seric 	{
1004324Seric 		register ADDRESS *a = al;
1014324Seric 
1024324Seric 		al = a->q_next;
103*5006Seric 		recipient(a, sendq);
1044993Seric 
1054998Seric 		/* arrange to inherit full name */
1064998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1074998Seric 			a->q_fullname = ctladdr->q_fullname;
1084174Seric 	}
1094324Seric 
1104174Seric 	To = NULL;
1114174Seric }
1124174Seric /*
1134174Seric **  RECIPIENT -- Designate a message recipient
1144174Seric **
1154174Seric **	Saves the named person for future mailing.
1164174Seric **
1174174Seric **	Parameters:
1184174Seric **		a -- the (preparsed) address header for the recipient.
119*5006Seric **		sendq -- a pointer to the head of a queue to put the
120*5006Seric **			recipient in.  Duplicate supression is done
121*5006Seric **			in this queue.
1224174Seric **
1234174Seric **	Returns:
1244998Seric **		none.
1254174Seric **
1264174Seric **	Side Effects:
1274174Seric **		none.
1284174Seric */
1294174Seric 
130*5006Seric recipient(a, sendq)
1314174Seric 	register ADDRESS *a;
132*5006Seric 	register ADDRESS **sendq;
1334174Seric {
1344174Seric 	register ADDRESS *q;
1354319Seric 	ADDRESS **pq;
1364174Seric 	register struct mailer *m;
1374399Seric 	extern ADDRESS *getctladdr();
1384627Seric 	extern bool safefile();
1394174Seric 
1404174Seric 	To = a->q_paddr;
1414600Seric 	m = a->q_mailer;
1424174Seric 	errno = 0;
1434174Seric # ifdef DEBUG
1444174Seric 	if (Debug)
1454444Seric 	{
1464444Seric 		printf("\nrecipient: ");
1474444Seric 		printaddr(a, FALSE);
1484444Seric 	}
1494174Seric # endif DEBUG
1504174Seric 
1514174Seric 	/* break aliasing loops */
1524174Seric 	if (AliasLevel > MAXRCRSN)
1534174Seric 	{
1544174Seric 		usrerr("aliasing/forwarding loop broken");
1554998Seric 		return;
1564174Seric 	}
1574174Seric 
1584174Seric 	/*
1594627Seric 	**  Finish setting up address structure.
1604174Seric 	*/
1614174Seric 
1624627Seric 	a->q_timeout = TimeOut;
1634627Seric 
1644627Seric 	/* do sickly crude mapping for program mailing, etc. */
1654600Seric 	if (a->q_mailer == LocalMailer)
1664174Seric 	{
1674174Seric 		if (a->q_user[0] == '|')
1684174Seric 		{
1694600Seric 			a->q_mailer = m = ProgMailer;
1704174Seric 			a->q_user++;
1714627Seric 			if (a->q_alias == NULL && Debug == 0 && !QueueRun)
1724217Seric 			{
1734217Seric 				usrerr("Cannot mail directly to programs");
1744217Seric 				a->q_flags |= QDONTSEND;
1754217Seric 			}
1764174Seric 		}
1774174Seric 	}
1784174Seric 
1794174Seric 	/*
1804419Seric 	**  Look up this person in the recipient list.
1814419Seric 	**	If they are there already, return, otherwise continue.
1824419Seric 	**	If the list is empty, just add it.  Notice the cute
1834419Seric 	**	hack to make from addresses suppress things correctly:
1844419Seric 	**	the QDONTSEND bit will be set in the send list.
1854419Seric 	**	[Please note: the emphasis is on "hack."]
1864174Seric 	*/
1874174Seric 
188*5006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
1894174Seric 	{
1904319Seric 		if (!ForceMail && sameaddr(q, a, FALSE))
1914174Seric 		{
1924174Seric # ifdef DEBUG
1934319Seric 			if (Debug)
1944444Seric 			{
1954444Seric 				printf("%s in sendq: ", a->q_paddr);
1964444Seric 				printaddr(q, FALSE);
1974444Seric 			}
1984174Seric # endif DEBUG
1994998Seric 			if (Verbose && !bitset(QDONTSEND, a->q_flags))
2004324Seric 				message(Arpa_Info, "duplicate suppressed");
2014423Seric 			if (!bitset(QPRIMARY, q->q_flags))
2024423Seric 				q->q_flags |= a->q_flags;
2034998Seric 			return;
2044174Seric 		}
2054319Seric 	}
2064174Seric 
2074319Seric 	/* add address on list */
2084319Seric 	*pq = a;
2094174Seric 	a->q_next = NULL;
2104247Seric 	if (DontSend)
2114247Seric 		a->q_flags |= QDONTSEND;
2124174Seric 
2134174Seric 	/*
2144174Seric 	**  Alias the name and handle :include: specs.
2154174Seric 	*/
2164174Seric 
2174600Seric 	if (a->q_mailer == LocalMailer)
2184174Seric 	{
2194174Seric 		if (strncmp(a->q_user, ":include:", 9) == 0)
2204174Seric 		{
2214174Seric 			a->q_flags |= QDONTSEND;
2224627Seric 			if (a->q_alias == NULL && Debug == 0 && !QueueRun)
2234399Seric 				usrerr("Cannot mail directly to :include:s");
2244399Seric 			else
2254399Seric 			{
2264399Seric 				if (Verbose)
2274399Seric 					message(Arpa_Info, "including file %s", &a->q_user[9]);
228*5006Seric 				include(&a->q_user[9], " sending", a, sendq);
2294399Seric 			}
2304174Seric 		}
2314174Seric 		else
232*5006Seric 			alias(a, sendq);
2334174Seric 	}
2344174Seric 
2354174Seric 	/*
2364174Seric 	**  If the user is local and still being sent, verify that
2374174Seric 	**  the address is good.  If it is, try to forward.
2384174Seric 	**  If the address is already good, we have a forwarding
2394174Seric 	**  loop.  This can be broken by just sending directly to
2404174Seric 	**  the user (which is probably correct anyway).
2414174Seric 	*/
2424174Seric 
2434600Seric 	if (!bitset(QDONTSEND, a->q_flags) && a->q_mailer == LocalMailer)
2444174Seric 	{
2454174Seric 		char buf[MAXNAME];
2464201Seric 		register char *p;
2474329Seric 		struct stat stb;
2484329Seric 		extern bool writable();
2494399Seric 		bool quoted = FALSE;
2504174Seric 
2514174Seric 		strcpy(buf, a->q_user);
2524399Seric 		for (p = buf; *p != '\0' && !quoted; p++)
2534399Seric 		{
2544998Seric 			if (!isascii(*p) && (*p & 0377) != (SPACESUB & 0377))
2554399Seric 				quoted = TRUE;
2564399Seric 		}
2574174Seric 		stripquotes(buf, TRUE);
2584174Seric 
2594174Seric 		/* see if this is to a file */
2604201Seric 		if ((p = rindex(buf, '/')) != NULL)
2614174Seric 		{
2624201Seric 			/* check if writable or creatable */
2634627Seric 			if (a->q_alias == NULL && Debug == 0 && !QueueRun)
2644399Seric 			{
2654399Seric 				usrerr("Cannot mail directly to files");
2664399Seric 				a->q_flags |= QDONTSEND;
2674399Seric 			}
2684399Seric 			else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
2694539Seric 			    (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
2704174Seric 			{
2714174Seric 				a->q_flags |= QBADADDR;
2724174Seric 				giveresponse(EX_CANTCREAT, TRUE, m);
2734174Seric 			}
2744174Seric 		}
2754174Seric 		else
2764174Seric 		{
2774174Seric 			register struct passwd *pw;
2784373Seric 			extern struct passwd *finduser();
2794373Seric 
2804407Seric 			/* warning -- finduser may trash buf */
2814373Seric 			pw = finduser(buf);
2824174Seric 			if (pw == NULL)
2834174Seric 			{
2844174Seric 				a->q_flags |= QBADADDR;
2854174Seric 				giveresponse(EX_NOUSER, TRUE, m);
2864174Seric 			}
2874174Seric 			else
2884174Seric 			{
2894993Seric 				char nbuf[MAXNAME];
2904993Seric 
2914376Seric 				if (strcmp(a->q_user, pw->pw_name) != 0)
2924376Seric 				{
2934376Seric 					a->q_user = newstr(pw->pw_name);
2944376Seric 					strcpy(buf, pw->pw_name);
2954376Seric 				}
2964174Seric 				a->q_home = newstr(pw->pw_dir);
2974213Seric 				a->q_uid = pw->pw_uid;
2984399Seric 				a->q_gid = pw->pw_gid;
2994404Seric 				a->q_flags |= QGOODUID;
3004998Seric 				buildfname(pw->pw_gecos, pw->pw_name, nbuf);
3014993Seric 				if (nbuf[0] != '\0')
3024993Seric 					a->q_fullname = newstr(nbuf);
3034399Seric 				if (!quoted)
304*5006Seric 					forward(a, sendq);
3054174Seric 			}
3064174Seric 		}
3074174Seric 	}
3084174Seric }
3094174Seric /*
3104373Seric **  FINDUSER -- find the password entry for a user.
3114373Seric **
3124373Seric **	This looks a lot like getpwnam, except that it may want to
3134373Seric **	do some fancier pattern matching in /etc/passwd.
3144373Seric **
3154373Seric **	Parameters:
3164373Seric **		name -- the name to match against.
3174373Seric **
3184373Seric **	Returns:
3194373Seric **		A pointer to a pw struct.
3204373Seric **		NULL if name is unknown or ambiguous.
3214373Seric **
3224373Seric **	Side Effects:
3234407Seric **		may modify name.
3244373Seric */
3254373Seric 
3264373Seric struct passwd *
3274373Seric finduser(name)
3284373Seric 	char *name;
3294373Seric {
3304376Seric 	extern struct passwd *getpwent();
3314376Seric 	register struct passwd *pw;
3324407Seric 	register char *p;
3334373Seric 
3344407Seric 	/*
3354407Seric 	**  Make name canonical.
3364407Seric 	*/
3374407Seric 
3384407Seric 	for (p = name; *p != '\0'; p++)
3394407Seric 	{
3404407Seric 		if (*p == (SPACESUB & 0177) || *p == '_')
3414407Seric 			*p = ' ';
3424407Seric 	}
3434407Seric 
3444376Seric 	setpwent();
3454376Seric 	while ((pw = getpwent()) != NULL)
3464376Seric 	{
3474998Seric 		char buf[MAXNAME];
3484993Seric 		extern bool sameword();
3494376Seric 
3504376Seric 		if (strcmp(pw->pw_name, name) == 0)
3514376Seric 			return (pw);
3524998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
3534407Seric 		if (index(buf, ' ') != NULL && sameword(buf, name))
3544381Seric 		{
3554377Seric 			if (Verbose)
3564998Seric 				message(Arpa_Info, "sending to login name %s",
3574998Seric 				    pw->pw_name);
3584376Seric 			return (pw);
3594377Seric 		}
3604376Seric 	}
3614376Seric 	return (NULL);
3624373Seric }
3634373Seric /*
3644329Seric **  WRITABLE -- predicate returning if the file is writable.
3654329Seric **
3664329Seric **	This routine must duplicate the algorithm in sys/fio.c.
3674329Seric **	Unfortunately, we cannot use the access call since we
3684329Seric **	won't necessarily be the real uid when we try to
3694329Seric **	actually open the file.
3704329Seric **
3714329Seric **	Notice that ANY file with ANY execute bit is automatically
3724329Seric **	not writable.  This is also enforced by mailfile.
3734329Seric **
3744329Seric **	Parameters:
3754329Seric **		s -- pointer to a stat struct for the file.
3764329Seric **
3774329Seric **	Returns:
3784329Seric **		TRUE -- if we will be able to write this file.
3794329Seric **		FALSE -- if we cannot write this file.
3804329Seric **
3814329Seric **	Side Effects:
3824329Seric **		none.
3834329Seric */
3844329Seric 
3854329Seric bool
3864329Seric writable(s)
3874329Seric 	register struct stat *s;
3884329Seric {
3894329Seric 	int euid, egid;
3904329Seric 	int bits;
3914329Seric 
3924329Seric 	if (bitset(0111, s->st_mode))
3934329Seric 		return (FALSE);
3944329Seric 	euid = getruid();
3954329Seric 	egid = getrgid();
3964329Seric 	if (geteuid() == 0)
3974329Seric 	{
3984329Seric 		if (bitset(S_ISUID, s->st_mode))
3994329Seric 			euid = s->st_uid;
4004329Seric 		if (bitset(S_ISGID, s->st_mode))
4014329Seric 			egid = s->st_gid;
4024329Seric 	}
4034329Seric 
4044329Seric 	if (euid == 0)
4054329Seric 		return (TRUE);
4064329Seric 	bits = S_IWRITE;
4074329Seric 	if (euid != s->st_uid)
4084329Seric 	{
4094329Seric 		bits >>= 3;
4104329Seric 		if (egid != s->st_gid)
4114329Seric 			bits >>= 3;
4124329Seric 	}
4134329Seric 	return ((s->st_mode & bits) != 0);
4144329Seric }
4154329Seric /*
4164174Seric **  INCLUDE -- handle :include: specification.
4174174Seric **
4184174Seric **	Parameters:
4194174Seric **		fname -- filename to include.
4204176Seric **		msg -- message to print in verbose mode.
4214399Seric **		ctladdr -- address template to use to fill in these
4224399Seric **			addresses -- effective user/group id are
4234399Seric **			the important things.
424*5006Seric **		sendq -- a pointer to the head of the send queue
425*5006Seric **			to put these addresses in.
4264174Seric **
4274174Seric **	Returns:
4284174Seric **		none.
4294174Seric **
4304174Seric **	Side Effects:
4314174Seric **		reads the :include: file and sends to everyone
4324174Seric **		listed in that file.
4334174Seric */
4344174Seric 
435*5006Seric include(fname, msg, ctladdr, sendq)
4364174Seric 	char *fname;
4374176Seric 	char *msg;
4384399Seric 	ADDRESS *ctladdr;
439*5006Seric 	ADDRESS **sendq;
4404174Seric {
4414174Seric 	char buf[MAXLINE];
4424174Seric 	register FILE *fp;
4434178Seric 	char *oldto = To;
4444174Seric 
4454174Seric 	fp = fopen(fname, "r");
4464174Seric 	if (fp == NULL)
4474174Seric 	{
4484174Seric 		usrerr("Cannot open %s", fname);
4494174Seric 		return;
4504174Seric 	}
4514406Seric 	if (getctladdr(ctladdr) == NULL)
4524406Seric 	{
4534406Seric 		struct stat st;
4544174Seric 
4554406Seric 		if (fstat(fileno(fp), &st) < 0)
4564406Seric 			syserr("Cannot fstat %s!", fname);
4574406Seric 		ctladdr->q_uid = st.st_uid;
4584406Seric 		ctladdr->q_gid = st.st_gid;
4594406Seric 		ctladdr->q_flags |= QGOODUID;
4604406Seric 	}
4614406Seric 
4624174Seric 	/* read the file -- each line is a comma-separated list. */
4634174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
4644174Seric 	{
4654174Seric 		register char *p = index(buf, '\n');
4664174Seric 
4674174Seric 		if (p != NULL)
4684174Seric 			*p = '\0';
4694174Seric 		if (buf[0] == '\0')
4704174Seric 			continue;
4714178Seric 		To = oldto;
4724174Seric 		if (Verbose)
4734176Seric 			message(Arpa_Info, "%s to %s", msg, buf);
4744176Seric 		AliasLevel++;
475*5006Seric 		sendto(buf, 1, ctladdr, sendq);
4764176Seric 		AliasLevel--;
4774174Seric 	}
4784174Seric 
4794319Seric 	(void) fclose(fp);
4804174Seric }
4814324Seric /*
4824324Seric **  SENDTOARGV -- send to an argument vector.
4834324Seric **
4844324Seric **	Parameters:
4854324Seric **		argv -- argument vector to send to.
4864324Seric **
4874324Seric **	Returns:
4884324Seric **		none.
4894324Seric **
4904324Seric **	Side Effects:
4914324Seric **		puts all addresses on the argument vector onto the
4924324Seric **			send queue.
4934324Seric */
4944324Seric 
4954324Seric sendtoargv(argv)
4964324Seric 	register char **argv;
4974324Seric {
4984324Seric 	register char *p;
4994324Seric 	extern bool sameword();
5004324Seric 
5014324Seric 	while ((p = *argv++) != NULL)
5024324Seric 	{
5034324Seric 		if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at"))
5044324Seric 		{
5054324Seric 			char nbuf[MAXNAME];
5064324Seric 
5074324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
5084324Seric 				usrerr("address overflow");
5094324Seric 			else
5104324Seric 			{
5114324Seric 				(void) strcpy(nbuf, p);
5124324Seric 				(void) strcat(nbuf, "@");
5134324Seric 				(void) strcat(nbuf, argv[1]);
5144324Seric 				p = newstr(nbuf);
5154324Seric 				argv += 2;
5164324Seric 			}
5174324Seric 		}
518*5006Seric 		sendto(p, 0, (ADDRESS *) NULL, &SendQueue);
5194324Seric 	}
5204324Seric }
5214399Seric /*
5224399Seric **  GETCTLADDR -- get controlling address from an address header.
5234399Seric **
5244399Seric **	If none, get one corresponding to the effective userid.
5254399Seric **
5264399Seric **	Parameters:
5274399Seric **		a -- the address to find the controller of.
5284399Seric **
5294399Seric **	Returns:
5304399Seric **		the controlling address.
5314399Seric **
5324399Seric **	Side Effects:
5334399Seric **		none.
5344399Seric */
5354399Seric 
5364399Seric ADDRESS *
5374399Seric getctladdr(a)
5384399Seric 	register ADDRESS *a;
5394399Seric {
5404404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
5414399Seric 		a = a->q_alias;
5424399Seric 	return (a);
5434399Seric }
544