14174Seric # include <pwd.h>
24329Seric # include <sys/types.h>
34329Seric # include <sys/stat.h>
44174Seric # include "sendmail.h"
54174Seric 
6*4399Seric static char SccsId[] = "@(#)recipient.c	3.17	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.
17*4399Seric **		ctladdr -- the address template for the person to
18*4399Seric **			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 
29*4399Seric sendto(list, copyf, ctladdr)
304174Seric 	char *list;
314174Seric 	int copyf;
32*4399Seric 	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;
70*4399Seric 		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;
106*4399Seric 	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++;
134*4399Seric 			if (getctladdr(a) == &From && 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;
177*4399Seric 			if (getctladdr(a) == &From && Debug == 0)
178*4399Seric 				usrerr("Cannot mail directly to :include:s");
179*4399Seric 			else
180*4399Seric 			{
181*4399Seric 				if (Verbose)
182*4399Seric 					message(Arpa_Info, "including file %s", &a->q_user[9]);
183*4399Seric 				include(&a->q_user[9], " sending", a);
184*4399Seric 			}
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();
204*4399Seric 		bool quoted = FALSE;
2054174Seric 
2064174Seric 		strcpy(buf, a->q_user);
207*4399Seric 		for (p = buf; *p != '\0' && !quoted; p++)
208*4399Seric 		{
209*4399Seric 			if (!isascii(*p))
210*4399Seric 				quoted = TRUE;
211*4399Seric 		}
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 */
218*4399Seric 			if (getctladdr(a) == &From && Debug == 0)
219*4399Seric 			{
220*4399Seric 				usrerr("Cannot mail directly to files");
221*4399Seric 				a->q_flags |= QDONTSEND;
222*4399Seric 			}
223*4399Seric 			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 
2354373Seric 			pw = finduser(buf);
2364174Seric 			if (pw == NULL)
2374174Seric 			{
2384174Seric 				a->q_flags |= QBADADDR;
2394174Seric 				giveresponse(EX_NOUSER, TRUE, m);
2404174Seric 			}
2414174Seric 			else
2424174Seric 			{
2434376Seric 				if (strcmp(a->q_user, pw->pw_name) != 0)
2444376Seric 				{
2454376Seric 					a->q_user = newstr(pw->pw_name);
2464376Seric 					strcpy(buf, pw->pw_name);
2474376Seric 				}
2484174Seric 				a->q_home = newstr(pw->pw_dir);
2494213Seric 				a->q_uid = pw->pw_uid;
250*4399Seric 				a->q_gid = pw->pw_gid;
251*4399Seric 				if (!quoted)
2524174Seric 					forward(a);
2534174Seric 			}
2544174Seric 		}
2554174Seric 	}
2564174Seric }
2574174Seric /*
2584373Seric **  FINDUSER -- find the password entry for a user.
2594373Seric **
2604373Seric **	This looks a lot like getpwnam, except that it may want to
2614373Seric **	do some fancier pattern matching in /etc/passwd.
2624373Seric **
2634373Seric **	Parameters:
2644373Seric **		name -- the name to match against.
2654373Seric **
2664373Seric **	Returns:
2674373Seric **		A pointer to a pw struct.
2684373Seric **		NULL if name is unknown or ambiguous.
2694373Seric **
2704373Seric **	Side Effects:
2714373Seric **		none.
2724373Seric */
2734373Seric 
2744373Seric struct passwd *
2754373Seric finduser(name)
2764373Seric 	char *name;
2774373Seric {
2784376Seric 	extern struct passwd *getpwent();
2794376Seric 	register struct passwd *pw;
2804373Seric 
2814376Seric 	setpwent();
2824376Seric 	while ((pw = getpwent()) != NULL)
2834376Seric 	{
2844376Seric 		char buf[MAXNAME];
2854376Seric 		register char *p;
2864376Seric 		extern bool sameword();
2874381Seric 		bool gotaspace;
2884376Seric 
2894376Seric 		if (strcmp(pw->pw_name, name) == 0)
2904376Seric 			return (pw);
2914376Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
2924381Seric 		gotaspace = FALSE;
2934376Seric 		for (p = buf; (p = index(p, ' ')) != NULL; )
2944381Seric 		{
2954376Seric 			*p++ = SPACESUB & 0177;
2964381Seric 			gotaspace = TRUE;
2974381Seric 		}
2984381Seric 		if (gotaspace && sameword(buf, name))
2994377Seric 		{
3004377Seric 			if (Verbose)
3014381Seric 				message(Arpa_Info, "sending to login name %s",
3024381Seric 				    pw->pw_name);
3034376Seric 			return (pw);
3044377Seric 		}
3054376Seric 	}
3064376Seric 	return (NULL);
3074373Seric }
3084373Seric /*
3094329Seric **  WRITABLE -- predicate returning if the file is writable.
3104329Seric **
3114329Seric **	This routine must duplicate the algorithm in sys/fio.c.
3124329Seric **	Unfortunately, we cannot use the access call since we
3134329Seric **	won't necessarily be the real uid when we try to
3144329Seric **	actually open the file.
3154329Seric **
3164329Seric **	Notice that ANY file with ANY execute bit is automatically
3174329Seric **	not writable.  This is also enforced by mailfile.
3184329Seric **
3194329Seric **	Parameters:
3204329Seric **		s -- pointer to a stat struct for the file.
3214329Seric **
3224329Seric **	Returns:
3234329Seric **		TRUE -- if we will be able to write this file.
3244329Seric **		FALSE -- if we cannot write this file.
3254329Seric **
3264329Seric **	Side Effects:
3274329Seric **		none.
3284329Seric */
3294329Seric 
3304329Seric bool
3314329Seric writable(s)
3324329Seric 	register struct stat *s;
3334329Seric {
3344329Seric 	int euid, egid;
3354329Seric 	int bits;
3364329Seric 
3374329Seric 	if (bitset(0111, s->st_mode))
3384329Seric 		return (FALSE);
3394329Seric 	euid = getruid();
3404329Seric 	egid = getrgid();
3414329Seric 	if (geteuid() == 0)
3424329Seric 	{
3434329Seric 		if (bitset(S_ISUID, s->st_mode))
3444329Seric 			euid = s->st_uid;
3454329Seric 		if (bitset(S_ISGID, s->st_mode))
3464329Seric 			egid = s->st_gid;
3474329Seric 	}
3484329Seric 
3494329Seric 	if (euid == 0)
3504329Seric 		return (TRUE);
3514329Seric 	bits = S_IWRITE;
3524329Seric 	if (euid != s->st_uid)
3534329Seric 	{
3544329Seric 		bits >>= 3;
3554329Seric 		if (egid != s->st_gid)
3564329Seric 			bits >>= 3;
3574329Seric 	}
3584329Seric 	return ((s->st_mode & bits) != 0);
3594329Seric }
3604329Seric /*
3614174Seric **  INCLUDE -- handle :include: specification.
3624174Seric **
3634174Seric **	Parameters:
3644174Seric **		fname -- filename to include.
3654176Seric **		msg -- message to print in verbose mode.
366*4399Seric **		ctladdr -- address template to use to fill in these
367*4399Seric **			addresses -- effective user/group id are
368*4399Seric **			the important things.
3694174Seric **
3704174Seric **	Returns:
3714174Seric **		none.
3724174Seric **
3734174Seric **	Side Effects:
3744174Seric **		reads the :include: file and sends to everyone
3754174Seric **		listed in that file.
3764174Seric */
3774174Seric 
378*4399Seric include(fname, msg, ctladdr)
3794174Seric 	char *fname;
3804176Seric 	char *msg;
381*4399Seric 	ADDRESS *ctladdr;
3824174Seric {
3834174Seric 	char buf[MAXLINE];
3844174Seric 	register FILE *fp;
3854178Seric 	char *oldto = To;
3864174Seric 
3874174Seric 	fp = fopen(fname, "r");
3884174Seric 	if (fp == NULL)
3894174Seric 	{
3904174Seric 		usrerr("Cannot open %s", fname);
3914174Seric 		return;
3924174Seric 	}
3934174Seric 
3944174Seric 	/* read the file -- each line is a comma-separated list. */
3954174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
3964174Seric 	{
3974174Seric 		register char *p = index(buf, '\n');
3984174Seric 
3994174Seric 		if (p != NULL)
4004174Seric 			*p = '\0';
4014174Seric 		if (buf[0] == '\0')
4024174Seric 			continue;
4034178Seric 		To = oldto;
4044174Seric 		if (Verbose)
4054176Seric 			message(Arpa_Info, "%s to %s", msg, buf);
4064176Seric 		AliasLevel++;
407*4399Seric 		sendto(buf, 1, ctladdr);
4084176Seric 		AliasLevel--;
4094174Seric 	}
4104174Seric 
4114319Seric 	(void) fclose(fp);
4124174Seric }
4134324Seric /*
4144324Seric **  SENDTOARGV -- send to an argument vector.
4154324Seric **
4164324Seric **	Parameters:
4174324Seric **		argv -- argument vector to send to.
4184324Seric **
4194324Seric **	Returns:
4204324Seric **		none.
4214324Seric **
4224324Seric **	Side Effects:
4234324Seric **		puts all addresses on the argument vector onto the
4244324Seric **			send queue.
4254324Seric */
4264324Seric 
4274324Seric sendtoargv(argv)
4284324Seric 	register char **argv;
4294324Seric {
4304324Seric 	register char *p;
4314324Seric 	extern bool sameword();
4324324Seric 
4334324Seric 	while ((p = *argv++) != NULL)
4344324Seric 	{
4354324Seric 		if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at"))
4364324Seric 		{
4374324Seric 			char nbuf[MAXNAME];
4384324Seric 
4394324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
4404324Seric 				usrerr("address overflow");
4414324Seric 			else
4424324Seric 			{
4434324Seric 				(void) strcpy(nbuf, p);
4444324Seric 				(void) strcat(nbuf, "@");
4454324Seric 				(void) strcat(nbuf, argv[1]);
4464324Seric 				p = newstr(nbuf);
4474324Seric 				argv += 2;
4484324Seric 			}
4494324Seric 		}
450*4399Seric 		sendto(p, 0, &From);
4514324Seric 	}
4524324Seric }
453*4399Seric /*
454*4399Seric **  GETCTLADDR -- get controlling address from an address header.
455*4399Seric **
456*4399Seric **	If none, get one corresponding to the effective userid.
457*4399Seric **
458*4399Seric **	Parameters:
459*4399Seric **		a -- the address to find the controller of.
460*4399Seric **
461*4399Seric **	Returns:
462*4399Seric **		the controlling address.
463*4399Seric **
464*4399Seric **	Side Effects:
465*4399Seric **		none.
466*4399Seric */
467*4399Seric 
468*4399Seric ADDRESS *
469*4399Seric getctladdr(a)
470*4399Seric 	register ADDRESS *a;
471*4399Seric {
472*4399Seric 	while (a != NULL && a->q_home == NULL)
473*4399Seric 		a = a->q_alias;
474*4399Seric 	if (a == NULL)
475*4399Seric 		return (&From);
476*4399Seric 	return (a);
477*4399Seric }
478