14174Seric # include <pwd.h>
24627Seric # include "sendmail.h"
34329Seric # include <sys/stat.h>
44174Seric 
5*9210Seric SCCSID(@(#)recipient.c	3.44		11/14/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;
378223Seric 	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 
498223Seric 	/* heuristic to determine old versus new style addresses */
508230Seric 	if (ctladdr == NULL &&
518230Seric 	    (index(list, ',') != NULL || index(list, ';') != NULL ||
528230Seric 	     index(list, '<') != NULL || index(list, '(') != NULL))
538223Seric 		CurEnv->e_oldstyle = FALSE;
548223Seric 
554423Seric 	firstone = TRUE;
564444Seric 	selfref = FALSE;
574324Seric 	al = NULL;
588223Seric 
598081Seric 	for (p = list; *p != '\0'; )
604174Seric 	{
618081Seric 		register ADDRESS *a;
628081Seric 		extern char *DelimChar;		/* defined in prescan */
634319Seric 
648081Seric 		/* parse the address */
658081Seric 		while (isspace(*p) || *p == ',')
664174Seric 			p++;
678081Seric 		if ((a = parse(p, (ADDRESS *) NULL, 1)) == NULL)
684174Seric 			continue;
698081Seric 		p = DelimChar;
704324Seric 		a->q_next = al;
714399Seric 		a->q_alias = ctladdr;
724444Seric 
734444Seric 		/* see if this should be marked as a primary address */
744423Seric 		if (ctladdr == NULL ||
758081Seric 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
764423Seric 			a->q_flags |= QPRIMARY;
774444Seric 
784444Seric 		/* put on send queue or suppress self-reference */
794444Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a, FALSE))
804444Seric 			selfref = TRUE;
814444Seric 		else
824444Seric 			al = a;
834423Seric 		firstone = FALSE;
844324Seric 	}
854324Seric 
864444Seric 	/* if this alias doesn't include itself, delete ctladdr */
874444Seric 	if (!selfref && ctladdr != NULL)
884444Seric 		ctladdr->q_flags |= QDONTSEND;
894444Seric 
904324Seric 	/* arrange to send to everyone on the local send list */
914324Seric 	while (al != NULL)
924324Seric 	{
934324Seric 		register ADDRESS *a = al;
944324Seric 
954324Seric 		al = a->q_next;
965006Seric 		recipient(a, sendq);
974993Seric 
984998Seric 		/* arrange to inherit full name */
994998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1004998Seric 			a->q_fullname = ctladdr->q_fullname;
1014174Seric 	}
1024324Seric 
1036906Seric 	CurEnv->e_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.
1125006Seric **		sendq -- a pointer to the head of a queue to put the
1135006Seric **			recipient in.  Duplicate supression is done
1145006Seric **			in this queue.
1154174Seric **
1164174Seric **	Returns:
1174998Seric **		none.
1184174Seric **
1194174Seric **	Side Effects:
1204174Seric **		none.
1214174Seric */
1224174Seric 
1235006Seric recipient(a, sendq)
1244174Seric 	register ADDRESS *a;
1255006Seric 	register ADDRESS **sendq;
1264174Seric {
1274174Seric 	register ADDRESS *q;
1284319Seric 	ADDRESS **pq;
1294174Seric 	register struct mailer *m;
130*9210Seric 	register char *p;
131*9210Seric 	bool quoted = FALSE;		/* set if the addr has a quote bit */
132*9210Seric 	char buf[MAXNAME];		/* unquoted image of the user name */
1334399Seric 	extern ADDRESS *getctladdr();
1344627Seric 	extern bool safefile();
1354174Seric 
1366906Seric 	CurEnv->e_to = a->q_paddr;
1374600Seric 	m = a->q_mailer;
1384174Seric 	errno = 0;
1394174Seric # ifdef DEBUG
1407676Seric 	if (tTd(26, 1))
1414444Seric 	{
1424444Seric 		printf("\nrecipient: ");
1434444Seric 		printaddr(a, FALSE);
1444444Seric 	}
1454174Seric # endif DEBUG
1464174Seric 
1474174Seric 	/* break aliasing loops */
1484174Seric 	if (AliasLevel > MAXRCRSN)
1494174Seric 	{
1504174Seric 		usrerr("aliasing/forwarding loop broken");
1514998Seric 		return;
1524174Seric 	}
1534174Seric 
1544174Seric 	/*
1554627Seric 	**  Finish setting up address structure.
1564174Seric 	*/
1574174Seric 
1584627Seric 	a->q_timeout = TimeOut;
1594627Seric 
160*9210Seric 	(void) strcpy(buf, a->q_user);
161*9210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
162*9210Seric 	{
163*9210Seric 		if (!isascii(*p) && (*p & 0377) != (SpaceSub & 0377))
164*9210Seric 			quoted = TRUE;
165*9210Seric 	}
166*9210Seric 	stripquotes(buf, TRUE);
167*9210Seric 
1684627Seric 	/* do sickly crude mapping for program mailing, etc. */
169*9210Seric 	if (m == LocalMailer && buf[0] == '|')
1704174Seric 	{
171*9210Seric 		a->q_mailer = m = ProgMailer;
172*9210Seric 		a->q_user++;
173*9210Seric 		if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
1744174Seric 		{
175*9210Seric 			usrerr("Cannot mail directly to programs");
176*9210Seric 			a->q_flags |= QDONTSEND;
1774174Seric 		}
1784174Seric 	}
1794174Seric 
1804174Seric 	/*
1814419Seric 	**  Look up this person in the recipient list.
1824419Seric 	**	If they are there already, return, otherwise continue.
1834419Seric 	**	If the list is empty, just add it.  Notice the cute
1844419Seric 	**	hack to make from addresses suppress things correctly:
1854419Seric 	**	the QDONTSEND bit will be set in the send list.
1864419Seric 	**	[Please note: the emphasis is on "hack."]
1874174Seric 	*/
1884174Seric 
1895006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
1904174Seric 	{
1914319Seric 		if (!ForceMail && sameaddr(q, a, FALSE))
1924174Seric 		{
1934174Seric # ifdef DEBUG
1947676Seric 			if (tTd(26, 1))
1954444Seric 			{
1964444Seric 				printf("%s in sendq: ", a->q_paddr);
1974444Seric 				printaddr(q, FALSE);
1984444Seric 			}
1994174Seric # endif DEBUG
2007054Seric 			if (!bitset(QDONTSEND, a->q_flags))
2014324Seric 				message(Arpa_Info, "duplicate suppressed");
2024423Seric 			if (!bitset(QPRIMARY, q->q_flags))
2034423Seric 				q->q_flags |= a->q_flags;
2044998Seric 			return;
2054174Seric 		}
2064319Seric 	}
2074174Seric 
2084319Seric 	/* add address on list */
2094319Seric 	*pq = a;
2104174Seric 	a->q_next = NULL;
2114247Seric 	if (DontSend)
2124247Seric 		a->q_flags |= QDONTSEND;
2134174Seric 
2144174Seric 	/*
2154174Seric 	**  Alias the name and handle :include: specs.
2164174Seric 	*/
2174174Seric 
218*9210Seric 	if (m == LocalMailer && !bitset(QDONTSEND, a->q_flags))
2194174Seric 	{
2204174Seric 		if (strncmp(a->q_user, ":include:", 9) == 0)
2214174Seric 		{
2224174Seric 			a->q_flags |= QDONTSEND;
2237676Seric 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
2244399Seric 				usrerr("Cannot mail directly to :include:s");
2254399Seric 			else
2264399Seric 			{
2277054Seric 				message(Arpa_Info, "including file %s", &a->q_user[9]);
2285006Seric 				include(&a->q_user[9], " sending", a, sendq);
2294399Seric 			}
2304174Seric 		}
2314174Seric 		else
2325006Seric 			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 
243*9210Seric 	if (!bitset(QDONTSEND, a->q_flags) && m == LocalMailer)
2444174Seric 	{
2454329Seric 		struct stat stb;
2464329Seric 		extern bool writable();
2474174Seric 
2484174Seric 		/* see if this is to a file */
2495600Seric 		if (buf[0] == '/')
2504174Seric 		{
2515600Seric 			p = rindex(buf, '/');
2524201Seric 			/* check if writable or creatable */
2537676Seric 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
2544399Seric 			{
2554399Seric 				usrerr("Cannot mail directly to files");
2564399Seric 				a->q_flags |= QDONTSEND;
2574399Seric 			}
2584399Seric 			else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
2594539Seric 			    (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
2604174Seric 			{
2614174Seric 				a->q_flags |= QBADADDR;
2624174Seric 				giveresponse(EX_CANTCREAT, TRUE, m);
2634174Seric 			}
2644174Seric 		}
2654174Seric 		else
2664174Seric 		{
2674174Seric 			register struct passwd *pw;
2684373Seric 			extern struct passwd *finduser();
2694373Seric 
2704407Seric 			/* warning -- finduser may trash buf */
2714373Seric 			pw = finduser(buf);
2724174Seric 			if (pw == NULL)
2734174Seric 			{
2744174Seric 				a->q_flags |= QBADADDR;
2754174Seric 				giveresponse(EX_NOUSER, TRUE, m);
2764174Seric 			}
2774174Seric 			else
2784174Seric 			{
2794993Seric 				char nbuf[MAXNAME];
2804993Seric 
2814376Seric 				if (strcmp(a->q_user, pw->pw_name) != 0)
2824376Seric 				{
2834376Seric 					a->q_user = newstr(pw->pw_name);
2847008Seric 					(void) strcpy(buf, pw->pw_name);
2854376Seric 				}
2864174Seric 				a->q_home = newstr(pw->pw_dir);
2874213Seric 				a->q_uid = pw->pw_uid;
2884399Seric 				a->q_gid = pw->pw_gid;
2894404Seric 				a->q_flags |= QGOODUID;
2904998Seric 				buildfname(pw->pw_gecos, pw->pw_name, nbuf);
2914993Seric 				if (nbuf[0] != '\0')
2924993Seric 					a->q_fullname = newstr(nbuf);
2934399Seric 				if (!quoted)
2945006Seric 					forward(a, sendq);
2954174Seric 			}
2964174Seric 		}
2974174Seric 	}
2984174Seric }
2994174Seric /*
3004373Seric **  FINDUSER -- find the password entry for a user.
3014373Seric **
3024373Seric **	This looks a lot like getpwnam, except that it may want to
3034373Seric **	do some fancier pattern matching in /etc/passwd.
3044373Seric **
3054373Seric **	Parameters:
3064373Seric **		name -- the name to match against.
3074373Seric **
3084373Seric **	Returns:
3094373Seric **		A pointer to a pw struct.
3104373Seric **		NULL if name is unknown or ambiguous.
3114373Seric **
3124373Seric **	Side Effects:
3134407Seric **		may modify name.
3144373Seric */
3154373Seric 
3164373Seric struct passwd *
3174373Seric finduser(name)
3184373Seric 	char *name;
3194373Seric {
3204376Seric 	extern struct passwd *getpwent();
3214376Seric 	register struct passwd *pw;
3224407Seric 	register char *p;
3234373Seric 
3244407Seric 	/*
3254407Seric 	**  Make name canonical.
3264407Seric 	*/
3274407Seric 
3284407Seric 	for (p = name; *p != '\0'; p++)
3294407Seric 	{
3309044Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
3314407Seric 			*p = ' ';
3324407Seric 	}
3334407Seric 
3344376Seric 	setpwent();
3354376Seric 	while ((pw = getpwent()) != NULL)
3364376Seric 	{
3374998Seric 		char buf[MAXNAME];
3384993Seric 		extern bool sameword();
3394376Seric 
3404376Seric 		if (strcmp(pw->pw_name, name) == 0)
3414376Seric 			return (pw);
3424998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
3434407Seric 		if (index(buf, ' ') != NULL && sameword(buf, name))
3444381Seric 		{
3457054Seric 			message(Arpa_Info, "sending to login name %s", pw->pw_name);
3464376Seric 			return (pw);
3474377Seric 		}
3484376Seric 	}
3494376Seric 	return (NULL);
3504373Seric }
3514373Seric /*
3524329Seric **  WRITABLE -- predicate returning if the file is writable.
3534329Seric **
3544329Seric **	This routine must duplicate the algorithm in sys/fio.c.
3554329Seric **	Unfortunately, we cannot use the access call since we
3564329Seric **	won't necessarily be the real uid when we try to
3574329Seric **	actually open the file.
3584329Seric **
3594329Seric **	Notice that ANY file with ANY execute bit is automatically
3604329Seric **	not writable.  This is also enforced by mailfile.
3614329Seric **
3624329Seric **	Parameters:
3634329Seric **		s -- pointer to a stat struct for the file.
3644329Seric **
3654329Seric **	Returns:
3664329Seric **		TRUE -- if we will be able to write this file.
3674329Seric **		FALSE -- if we cannot write this file.
3684329Seric **
3694329Seric **	Side Effects:
3704329Seric **		none.
3714329Seric */
3724329Seric 
3734329Seric bool
3744329Seric writable(s)
3754329Seric 	register struct stat *s;
3764329Seric {
3774329Seric 	int euid, egid;
3784329Seric 	int bits;
3794329Seric 
3804329Seric 	if (bitset(0111, s->st_mode))
3814329Seric 		return (FALSE);
3824329Seric 	euid = getruid();
3834329Seric 	egid = getrgid();
3844329Seric 	if (geteuid() == 0)
3854329Seric 	{
3864329Seric 		if (bitset(S_ISUID, s->st_mode))
3874329Seric 			euid = s->st_uid;
3884329Seric 		if (bitset(S_ISGID, s->st_mode))
3894329Seric 			egid = s->st_gid;
3904329Seric 	}
3914329Seric 
3924329Seric 	if (euid == 0)
3934329Seric 		return (TRUE);
3944329Seric 	bits = S_IWRITE;
3954329Seric 	if (euid != s->st_uid)
3964329Seric 	{
3974329Seric 		bits >>= 3;
3984329Seric 		if (egid != s->st_gid)
3994329Seric 			bits >>= 3;
4004329Seric 	}
4014329Seric 	return ((s->st_mode & bits) != 0);
4024329Seric }
4034329Seric /*
4044174Seric **  INCLUDE -- handle :include: specification.
4054174Seric **
4064174Seric **	Parameters:
4074174Seric **		fname -- filename to include.
4084176Seric **		msg -- message to print in verbose mode.
4094399Seric **		ctladdr -- address template to use to fill in these
4104399Seric **			addresses -- effective user/group id are
4114399Seric **			the important things.
4125006Seric **		sendq -- a pointer to the head of the send queue
4135006Seric **			to put these addresses in.
4144174Seric **
4154174Seric **	Returns:
4164174Seric **		none.
4174174Seric **
4184174Seric **	Side Effects:
4194174Seric **		reads the :include: file and sends to everyone
4204174Seric **		listed in that file.
4214174Seric */
4224174Seric 
4235006Seric include(fname, msg, ctladdr, sendq)
4244174Seric 	char *fname;
4254176Seric 	char *msg;
4264399Seric 	ADDRESS *ctladdr;
4275006Seric 	ADDRESS **sendq;
4284174Seric {
4294174Seric 	char buf[MAXLINE];
4304174Seric 	register FILE *fp;
4316906Seric 	char *oldto = CurEnv->e_to;
4324174Seric 
4334174Seric 	fp = fopen(fname, "r");
4344174Seric 	if (fp == NULL)
4354174Seric 	{
4364174Seric 		usrerr("Cannot open %s", fname);
4374174Seric 		return;
4384174Seric 	}
4394406Seric 	if (getctladdr(ctladdr) == NULL)
4404406Seric 	{
4414406Seric 		struct stat st;
4424174Seric 
4434406Seric 		if (fstat(fileno(fp), &st) < 0)
4444406Seric 			syserr("Cannot fstat %s!", fname);
4454406Seric 		ctladdr->q_uid = st.st_uid;
4464406Seric 		ctladdr->q_gid = st.st_gid;
4474406Seric 		ctladdr->q_flags |= QGOODUID;
4484406Seric 	}
4494406Seric 
4504174Seric 	/* read the file -- each line is a comma-separated list. */
4514174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
4524174Seric 	{
4534174Seric 		register char *p = index(buf, '\n');
4544174Seric 
4554174Seric 		if (p != NULL)
4564174Seric 			*p = '\0';
4574174Seric 		if (buf[0] == '\0')
4584174Seric 			continue;
4596906Seric 		CurEnv->e_to = oldto;
4607054Seric 		message(Arpa_Info, "%s to %s", msg, buf);
4614176Seric 		AliasLevel++;
4628081Seric 		sendto(buf, ctladdr, sendq);
4634176Seric 		AliasLevel--;
4644174Seric 	}
4654174Seric 
4664319Seric 	(void) fclose(fp);
4674174Seric }
4684324Seric /*
4694324Seric **  SENDTOARGV -- send to an argument vector.
4704324Seric **
4714324Seric **	Parameters:
4724324Seric **		argv -- argument vector to send to.
4734324Seric **
4744324Seric **	Returns:
4754324Seric **		none.
4764324Seric **
4774324Seric **	Side Effects:
4784324Seric **		puts all addresses on the argument vector onto the
4794324Seric **			send queue.
4804324Seric */
4814324Seric 
4824324Seric sendtoargv(argv)
4834324Seric 	register char **argv;
4844324Seric {
4854324Seric 	register char *p;
4864324Seric 	extern bool sameword();
4874324Seric 
4884324Seric 	while ((p = *argv++) != NULL)
4894324Seric 	{
4904324Seric 		if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at"))
4914324Seric 		{
4924324Seric 			char nbuf[MAXNAME];
4934324Seric 
4944324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
4954324Seric 				usrerr("address overflow");
4964324Seric 			else
4974324Seric 			{
4984324Seric 				(void) strcpy(nbuf, p);
4994324Seric 				(void) strcat(nbuf, "@");
5004324Seric 				(void) strcat(nbuf, argv[1]);
5014324Seric 				p = newstr(nbuf);
5024324Seric 				argv += 2;
5034324Seric 			}
5044324Seric 		}
5058081Seric 		sendto(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
5064324Seric 	}
5074324Seric }
5084399Seric /*
5094399Seric **  GETCTLADDR -- get controlling address from an address header.
5104399Seric **
5114399Seric **	If none, get one corresponding to the effective userid.
5124399Seric **
5134399Seric **	Parameters:
5144399Seric **		a -- the address to find the controller of.
5154399Seric **
5164399Seric **	Returns:
5174399Seric **		the controlling address.
5184399Seric **
5194399Seric **	Side Effects:
5204399Seric **		none.
5214399Seric */
5224399Seric 
5234399Seric ADDRESS *
5244399Seric getctladdr(a)
5254399Seric 	register ADDRESS *a;
5264399Seric {
5274404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
5284399Seric 		a = a->q_alias;
5294399Seric 	return (a);
5304399Seric }
531