14174Seric # include <pwd.h>
24329Seric # include <sys/types.h>
34329Seric # include <sys/stat.h>
44174Seric # include "sendmail.h"
54174Seric 
6*4407Seric static char SccsId[] = "@(#)recipient.c	3.21	09/16/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 */
374174Seric 
384324Seric # ifdef DEBUG
394324Seric 	if (Debug > 1)
404324Seric 		printf("sendto: %s\n", list);
414324Seric # endif DEBUG
424324Seric 
434174Seric 	more = TRUE;
444324Seric 	al = NULL;
454174Seric 	for (p = list; more; )
464174Seric 	{
474319Seric 		register char *q;
484319Seric 		register char c;
494319Seric 		ADDRESS *a;
504319Seric 
514174Seric 		/* find the end of this address */
524174Seric 		while (*p == ' ' || *p == '\t')
534174Seric 			p++;
544174Seric 		q = p;
554174Seric 		while ((c = *p++) != '\0' && c != ',' && c != '\n')
564174Seric 			continue;
574174Seric 		more = c != '\0';
584174Seric 		*--p = '\0';
594174Seric 		if (more)
604174Seric 			p++;
614324Seric 		if (*q == '\0')
624324Seric 			continue;
634174Seric 
644174Seric 		/* parse the address */
654174Seric 		if ((a = parse(q, (ADDRESS *) NULL, copyf)) == NULL)
664174Seric 			continue;
674174Seric 
684324Seric 		/* put it on the local send list */
694324Seric 		a->q_next = al;
704399Seric 		a->q_alias = ctladdr;
714324Seric 		al = a;
724324Seric 	}
734324Seric 
744324Seric 	/* arrange to send to everyone on the local send list */
754324Seric 	while (al != NULL)
764324Seric 	{
774324Seric 		register ADDRESS *a = al;
784324Seric 
794324Seric 		al = a->q_next;
804174Seric 		recipient(a);
814174Seric 	}
824324Seric 
834174Seric 	To = NULL;
844174Seric }
854174Seric /*
864174Seric **  RECIPIENT -- Designate a message recipient
874174Seric **
884174Seric **	Saves the named person for future mailing.
894174Seric **
904174Seric **	Parameters:
914174Seric **		a -- the (preparsed) address header for the recipient.
924174Seric **
934174Seric **	Returns:
944174Seric **		none.
954174Seric **
964174Seric **	Side Effects:
974174Seric **		none.
984174Seric */
994174Seric 
1004174Seric recipient(a)
1014174Seric 	register ADDRESS *a;
1024174Seric {
1034174Seric 	register ADDRESS *q;
1044319Seric 	ADDRESS **pq;
1054174Seric 	register struct mailer *m;
1064399Seric 	extern ADDRESS *getctladdr();
1074174Seric 
1084174Seric 	To = a->q_paddr;
1094174Seric 	m = Mailer[a->q_mailer];
1104174Seric 	errno = 0;
1114174Seric # ifdef DEBUG
1124174Seric 	if (Debug)
1134174Seric 		printf("recipient(%s)\n", To);
1144174Seric # endif DEBUG
1154174Seric 
1164174Seric 	/* break aliasing loops */
1174174Seric 	if (AliasLevel > MAXRCRSN)
1184174Seric 	{
1194174Seric 		usrerr("aliasing/forwarding loop broken");
1204174Seric 		return;
1214174Seric 	}
1224174Seric 
1234174Seric 	/*
1244174Seric 	**  Do sickly crude mapping for program mailing, etc.
1254174Seric 	*/
1264174Seric 
1274197Seric 	if (a->q_mailer == MN_LOCAL)
1284174Seric 	{
1294174Seric 		if (a->q_user[0] == '|')
1304174Seric 		{
1314197Seric 			a->q_mailer = MN_PROG;
1324197Seric 			m = Mailer[MN_PROG];
1334174Seric 			a->q_user++;
1344406Seric 			if (a->q_alias == NULL && Debug == 0)
1354217Seric 			{
1364217Seric 				usrerr("Cannot mail directly to programs");
1374217Seric 				a->q_flags |= QDONTSEND;
1384217Seric 			}
1394174Seric 		}
1404174Seric 	}
1414174Seric 
1424174Seric 	/*
1434174Seric 	**  Look up this person in the recipient list.  If they
1444174Seric 	**  are there already, return, otherwise continue.
1454174Seric 	**  If the list is empty, just add it.
1464174Seric 	*/
1474174Seric 
1484319Seric 	for (pq = &m->m_sendq; (q = *pq) != NULL; pq = &q->q_next)
1494174Seric 	{
1504319Seric 		if (!ForceMail && sameaddr(q, a, FALSE))
1514174Seric 		{
1524174Seric # ifdef DEBUG
1534319Seric 			if (Debug)
1544319Seric 				printf("(%s in sendq)\n", a->q_paddr);
1554174Seric # endif DEBUG
1564319Seric 			if (Verbose && !bitset(QDONTSEND, a->q_flags))
1574324Seric 				message(Arpa_Info, "duplicate suppressed");
1584319Seric 			return;
1594174Seric 		}
1604319Seric 	}
1614174Seric 
1624319Seric 	/* add address on list */
1634319Seric 	*pq = a;
1644174Seric 	a->q_next = NULL;
1654247Seric 	if (DontSend)
1664247Seric 		a->q_flags |= QDONTSEND;
1674174Seric 
1684174Seric 	/*
1694174Seric 	**  Alias the name and handle :include: specs.
1704174Seric 	*/
1714174Seric 
1724197Seric 	if (a->q_mailer == MN_LOCAL)
1734174Seric 	{
1744174Seric 		if (strncmp(a->q_user, ":include:", 9) == 0)
1754174Seric 		{
1764174Seric 			a->q_flags |= QDONTSEND;
1774406Seric 			if (a->q_alias == NULL && Debug == 0)
1784399Seric 				usrerr("Cannot mail directly to :include:s");
1794399Seric 			else
1804399Seric 			{
1814399Seric 				if (Verbose)
1824399Seric 					message(Arpa_Info, "including file %s", &a->q_user[9]);
1834399Seric 				include(&a->q_user[9], " sending", a);
1844399Seric 			}
1854174Seric 		}
1864174Seric 		else
1874174Seric 			alias(a);
1884174Seric 	}
1894174Seric 
1904174Seric 	/*
1914174Seric 	**  If the user is local and still being sent, verify that
1924174Seric 	**  the address is good.  If it is, try to forward.
1934174Seric 	**  If the address is already good, we have a forwarding
1944174Seric 	**  loop.  This can be broken by just sending directly to
1954174Seric 	**  the user (which is probably correct anyway).
1964174Seric 	*/
1974174Seric 
1984197Seric 	if (!bitset(QDONTSEND, a->q_flags) && a->q_mailer == MN_LOCAL)
1994174Seric 	{
2004174Seric 		char buf[MAXNAME];
2014201Seric 		register char *p;
2024329Seric 		struct stat stb;
2034329Seric 		extern bool writable();
2044399Seric 		bool quoted = FALSE;
2054174Seric 
2064174Seric 		strcpy(buf, a->q_user);
2074399Seric 		for (p = buf; *p != '\0' && !quoted; p++)
2084399Seric 		{
2094399Seric 			if (!isascii(*p))
2104399Seric 				quoted = TRUE;
2114399Seric 		}
2124174Seric 		stripquotes(buf, TRUE);
2134174Seric 
2144174Seric 		/* see if this is to a file */
2154201Seric 		if ((p = rindex(buf, '/')) != NULL)
2164174Seric 		{
2174201Seric 			/* check if writable or creatable */
2184406Seric 			if (a->q_alias == NULL && Debug == 0)
2194399Seric 			{
2204399Seric 				usrerr("Cannot mail directly to files");
2214399Seric 				a->q_flags |= QDONTSEND;
2224399Seric 			}
2234399Seric 			else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
2244201Seric 			    (*p = '\0', access(buf, 3) < 0))
2254174Seric 			{
2264174Seric 				a->q_flags |= QBADADDR;
2274174Seric 				giveresponse(EX_CANTCREAT, TRUE, m);
2284174Seric 			}
2294174Seric 		}
2304174Seric 		else
2314174Seric 		{
2324174Seric 			register struct passwd *pw;
2334373Seric 			extern struct passwd *finduser();
2344373Seric 
235*4407Seric 			/* warning -- finduser may trash buf */
2364373Seric 			pw = finduser(buf);
2374174Seric 			if (pw == NULL)
2384174Seric 			{
2394174Seric 				a->q_flags |= QBADADDR;
2404174Seric 				giveresponse(EX_NOUSER, TRUE, m);
2414174Seric 			}
2424174Seric 			else
2434174Seric 			{
2444376Seric 				if (strcmp(a->q_user, pw->pw_name) != 0)
2454376Seric 				{
2464376Seric 					a->q_user = newstr(pw->pw_name);
2474376Seric 					strcpy(buf, pw->pw_name);
2484376Seric 				}
2494174Seric 				a->q_home = newstr(pw->pw_dir);
2504213Seric 				a->q_uid = pw->pw_uid;
2514399Seric 				a->q_gid = pw->pw_gid;
2524404Seric 				a->q_flags |= QGOODUID;
2534399Seric 				if (!quoted)
2544174Seric 					forward(a);
2554174Seric 			}
2564174Seric 		}
2574174Seric 	}
2584174Seric }
2594174Seric /*
2604373Seric **  FINDUSER -- find the password entry for a user.
2614373Seric **
2624373Seric **	This looks a lot like getpwnam, except that it may want to
2634373Seric **	do some fancier pattern matching in /etc/passwd.
2644373Seric **
2654373Seric **	Parameters:
2664373Seric **		name -- the name to match against.
2674373Seric **
2684373Seric **	Returns:
2694373Seric **		A pointer to a pw struct.
2704373Seric **		NULL if name is unknown or ambiguous.
2714373Seric **
2724373Seric **	Side Effects:
273*4407Seric **		may modify name.
2744373Seric */
2754373Seric 
2764373Seric struct passwd *
2774373Seric finduser(name)
2784373Seric 	char *name;
2794373Seric {
2804376Seric 	extern struct passwd *getpwent();
2814376Seric 	register struct passwd *pw;
282*4407Seric 	register char *p;
2834373Seric 
284*4407Seric 	/*
285*4407Seric 	**  Make name canonical.
286*4407Seric 	*/
287*4407Seric 
288*4407Seric 	for (p = name; *p != '\0'; p++)
289*4407Seric 	{
290*4407Seric 		if (*p == (SPACESUB & 0177) || *p == '_')
291*4407Seric 			*p = ' ';
292*4407Seric 	}
293*4407Seric 
2944376Seric 	setpwent();
2954376Seric 	while ((pw = getpwent()) != NULL)
2964376Seric 	{
2974376Seric 		char buf[MAXNAME];
2984376Seric 		extern bool sameword();
2994376Seric 
3004376Seric 		if (strcmp(pw->pw_name, name) == 0)
3014376Seric 			return (pw);
3024376Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
303*4407Seric 		if (index(buf, ' ') != NULL && sameword(buf, name))
3044381Seric 		{
3054377Seric 			if (Verbose)
3064381Seric 				message(Arpa_Info, "sending to login name %s",
3074381Seric 				    pw->pw_name);
3084376Seric 			return (pw);
3094377Seric 		}
3104376Seric 	}
3114376Seric 	return (NULL);
3124373Seric }
3134373Seric /*
3144329Seric **  WRITABLE -- predicate returning if the file is writable.
3154329Seric **
3164329Seric **	This routine must duplicate the algorithm in sys/fio.c.
3174329Seric **	Unfortunately, we cannot use the access call since we
3184329Seric **	won't necessarily be the real uid when we try to
3194329Seric **	actually open the file.
3204329Seric **
3214329Seric **	Notice that ANY file with ANY execute bit is automatically
3224329Seric **	not writable.  This is also enforced by mailfile.
3234329Seric **
3244329Seric **	Parameters:
3254329Seric **		s -- pointer to a stat struct for the file.
3264329Seric **
3274329Seric **	Returns:
3284329Seric **		TRUE -- if we will be able to write this file.
3294329Seric **		FALSE -- if we cannot write this file.
3304329Seric **
3314329Seric **	Side Effects:
3324329Seric **		none.
3334329Seric */
3344329Seric 
3354329Seric bool
3364329Seric writable(s)
3374329Seric 	register struct stat *s;
3384329Seric {
3394329Seric 	int euid, egid;
3404329Seric 	int bits;
3414329Seric 
3424329Seric 	if (bitset(0111, s->st_mode))
3434329Seric 		return (FALSE);
3444329Seric 	euid = getruid();
3454329Seric 	egid = getrgid();
3464329Seric 	if (geteuid() == 0)
3474329Seric 	{
3484329Seric 		if (bitset(S_ISUID, s->st_mode))
3494329Seric 			euid = s->st_uid;
3504329Seric 		if (bitset(S_ISGID, s->st_mode))
3514329Seric 			egid = s->st_gid;
3524329Seric 	}
3534329Seric 
3544329Seric 	if (euid == 0)
3554329Seric 		return (TRUE);
3564329Seric 	bits = S_IWRITE;
3574329Seric 	if (euid != s->st_uid)
3584329Seric 	{
3594329Seric 		bits >>= 3;
3604329Seric 		if (egid != s->st_gid)
3614329Seric 			bits >>= 3;
3624329Seric 	}
3634329Seric 	return ((s->st_mode & bits) != 0);
3644329Seric }
3654329Seric /*
3664174Seric **  INCLUDE -- handle :include: specification.
3674174Seric **
3684174Seric **	Parameters:
3694174Seric **		fname -- filename to include.
3704176Seric **		msg -- message to print in verbose mode.
3714399Seric **		ctladdr -- address template to use to fill in these
3724399Seric **			addresses -- effective user/group id are
3734399Seric **			the important things.
3744174Seric **
3754174Seric **	Returns:
3764174Seric **		none.
3774174Seric **
3784174Seric **	Side Effects:
3794174Seric **		reads the :include: file and sends to everyone
3804174Seric **		listed in that file.
3814174Seric */
3824174Seric 
3834399Seric include(fname, msg, ctladdr)
3844174Seric 	char *fname;
3854176Seric 	char *msg;
3864399Seric 	ADDRESS *ctladdr;
3874174Seric {
3884174Seric 	char buf[MAXLINE];
3894174Seric 	register FILE *fp;
3904178Seric 	char *oldto = To;
3914174Seric 
3924174Seric 	fp = fopen(fname, "r");
3934174Seric 	if (fp == NULL)
3944174Seric 	{
3954174Seric 		usrerr("Cannot open %s", fname);
3964174Seric 		return;
3974174Seric 	}
3984406Seric 	if (getctladdr(ctladdr) == NULL)
3994406Seric 	{
4004406Seric 		struct stat st;
4014174Seric 
4024406Seric 		if (fstat(fileno(fp), &st) < 0)
4034406Seric 			syserr("Cannot fstat %s!", fname);
4044406Seric 		ctladdr->q_uid = st.st_uid;
4054406Seric 		ctladdr->q_gid = st.st_gid;
4064406Seric 		ctladdr->q_flags |= QGOODUID;
4074406Seric 	}
4084406Seric 
4094174Seric 	/* read the file -- each line is a comma-separated list. */
4104174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
4114174Seric 	{
4124174Seric 		register char *p = index(buf, '\n');
4134174Seric 
4144174Seric 		if (p != NULL)
4154174Seric 			*p = '\0';
4164174Seric 		if (buf[0] == '\0')
4174174Seric 			continue;
4184178Seric 		To = oldto;
4194174Seric 		if (Verbose)
4204176Seric 			message(Arpa_Info, "%s to %s", msg, buf);
4214176Seric 		AliasLevel++;
4224399Seric 		sendto(buf, 1, ctladdr);
4234176Seric 		AliasLevel--;
4244174Seric 	}
4254174Seric 
4264319Seric 	(void) fclose(fp);
4274174Seric }
4284324Seric /*
4294324Seric **  SENDTOARGV -- send to an argument vector.
4304324Seric **
4314324Seric **	Parameters:
4324324Seric **		argv -- argument vector to send to.
4334324Seric **
4344324Seric **	Returns:
4354324Seric **		none.
4364324Seric **
4374324Seric **	Side Effects:
4384324Seric **		puts all addresses on the argument vector onto the
4394324Seric **			send queue.
4404324Seric */
4414324Seric 
4424324Seric sendtoargv(argv)
4434324Seric 	register char **argv;
4444324Seric {
4454324Seric 	register char *p;
4464324Seric 	extern bool sameword();
4474324Seric 
4484324Seric 	while ((p = *argv++) != NULL)
4494324Seric 	{
4504324Seric 		if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at"))
4514324Seric 		{
4524324Seric 			char nbuf[MAXNAME];
4534324Seric 
4544324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
4554324Seric 				usrerr("address overflow");
4564324Seric 			else
4574324Seric 			{
4584324Seric 				(void) strcpy(nbuf, p);
4594324Seric 				(void) strcat(nbuf, "@");
4604324Seric 				(void) strcat(nbuf, argv[1]);
4614324Seric 				p = newstr(nbuf);
4624324Seric 				argv += 2;
4634324Seric 			}
4644324Seric 		}
4654402Seric 		sendto(p, 0, NULL);
4664324Seric 	}
4674324Seric }
4684399Seric /*
4694399Seric **  GETCTLADDR -- get controlling address from an address header.
4704399Seric **
4714399Seric **	If none, get one corresponding to the effective userid.
4724399Seric **
4734399Seric **	Parameters:
4744399Seric **		a -- the address to find the controller of.
4754399Seric **
4764399Seric **	Returns:
4774399Seric **		the controlling address.
4784399Seric **
4794399Seric **	Side Effects:
4804399Seric **		none.
4814399Seric */
4824399Seric 
4834399Seric ADDRESS *
4844399Seric getctladdr(a)
4854399Seric 	register ADDRESS *a;
4864399Seric {
4874404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
4884399Seric 		a = a->q_alias;
4894399Seric 	return (a);
4904399Seric }
491