xref: /csrg-svn/usr.sbin/sendmail/src/udb.c (revision 51830)
150581Seric /*
250581Seric  * Copyright (c) 1983 Eric P. Allman
350581Seric  * Copyright (c) 1988 Regents of the University of California.
450581Seric  * All rights reserved.
550581Seric  *
650581Seric  * %sccs.include.redist.c%
750581Seric  */
850581Seric 
950581Seric #ifndef lint
1051360Seric #ifdef USERDB
11*51830Seric static char sccsid [] = "@(#)udb.c	5.10 (Berkeley) 12/04/91 (with USERDB)";
1251360Seric #else
13*51830Seric static char sccsid [] = "@(#)udb.c	5.10 (Berkeley) 12/04/91 (without USERDB)";
1450581Seric #endif
1551360Seric #endif
1650581Seric 
1750581Seric #include "sendmail.h"
1850581Seric 
1950581Seric #ifdef USERDB
2050581Seric 
2150581Seric #include <sys/file.h>
2251360Seric #include <sys/time.h>
2351360Seric #include <fcntl.h>
2451360Seric #include <netdb.h>
2550581Seric #include <db.h>
2650581Seric 
2750581Seric /*
2851363Seric **  UDBEXPAND.C -- interface between sendmail and Berkeley User Data Base.
2950581Seric **
3051363Seric **	This depends on the 4.4BSD db package.
3150581Seric */
3250581Seric 
3351362Seric 
3451360Seric struct udbent
3551360Seric {
3651360Seric 	char	*udb_spec;		/* string version of spec */
3751360Seric 	int	udb_type;		/* type of entry */
3851360Seric 	union
3951360Seric 	{
4051360Seric 		/* type UE_REMOTE -- do remote call for lookup */
4151360Seric 		struct
4251360Seric 		{
4351360Seric 			struct sockaddr_in _udb_addr;	/* address */
4451360Seric 			int		_udb_timeout;	/* timeout */
4551360Seric 		} udb_remote;
4651360Seric #define udb_addr	udb_u.udb_remote._udb_addr
4751360Seric #define udb_timeout	udb_u.udb_remote._udb_timeout
4851360Seric 
4951360Seric 		/* type UE_FORWARD -- forward message to remote */
5051360Seric 		struct
5151360Seric 		{
5251360Seric 			char	*_udb_fwdhost;	/* name of forward host */
5351360Seric 		} udb_forward;
5451360Seric #define udb_fwdhost	udb_u.udb_forward._udb_fwdhost
5551360Seric 
5651360Seric 		/* type UE_LOOKUP -- lookup in local database */
5751360Seric 		struct
5851360Seric 		{
5951360Seric 			char	*_udb_dbname;	/* pathname of database */
6051360Seric 			DB	*_udb_dbp;	/* open database ptr */
6151360Seric 		} udb_lookup;
6251360Seric #define udb_dbname	udb_u.udb_lookup._udb_dbname
6351360Seric #define udb_dbp		udb_u.udb_lookup._udb_dbp
6451360Seric 	} udb_u;
6551360Seric };
6651360Seric 
6751360Seric #define UDB_EOLIST	0	/* end of list */
6851360Seric #define UDB_SKIP	1	/* skip this entry */
6951360Seric #define UDB_REMOTE	2	/* look up in remote database */
7051360Seric #define UDB_LOOKUP	3	/* look up in local database */
7151360Seric #define UDB_FORWARD	4	/* forward to remote host */
7251360Seric 
7351360Seric #define MAXUDBENT	10	/* maximum number of UDB entries */
7451360Seric 
7551363Seric 
7651363Seric struct option
7751363Seric {
7851363Seric 	char	*name;
7951363Seric 	char	*val;
8051363Seric };
8151363Seric /*
8251363Seric **  UDBEXPAND -- look up user in database and expand
8351363Seric **
8451363Seric **	Parameters:
8551363Seric **		a -- address to expand.
8651363Seric **		sendq -- pointer to head of sendq to put the expansions in.
8751363Seric **
8851363Seric **	Returns:
8951363Seric **		none.
9051363Seric **
9151363Seric **	Side Effects:
9251363Seric **		Modifies sendq.
9351363Seric */
9451363Seric 
9551363Seric int	UdbPort = 1616;
9651363Seric int	UdbTimeout = 10;
9751363Seric 
9851362Seric struct udbent	UdbEnts[MAXUDBENT + 1];
9951362Seric int		UdbSock = -1;
10051360Seric 
10150581Seric void
10250581Seric udbexpand(a, sendq)
10350581Seric 	register ADDRESS *a;
10450581Seric 	ADDRESS **sendq;
10550581Seric {
10650581Seric 	int i;
10750581Seric 	register char *p;
10850581Seric 	DBT key;
10950581Seric 	DBT info;
11051360Seric 	static bool firstcall = TRUE;
11151360Seric 	bool breakout;
11251360Seric 	register struct udbent *up;
11351362Seric 	int keylen;
11451362Seric 	char keybuf[128];
11550581Seric 	char buf[8192];
11650581Seric 
11750581Seric 	if (tTd(28, 1))
11850581Seric 		printf("expand(%s)\n", a->q_paddr);
11950581Seric 
12050581Seric 	/* make certain we are supposed to send to this address */
12151360Seric 	if (bitset(QDONTSEND, a->q_flags) ||
12251360Seric 	    UdbSpec == NULL || UdbSpec[0] == '\0')
12350581Seric 		return;
12450581Seric 	CurEnv->e_to = a->q_paddr;
12550581Seric 
12651360Seric 	/* on first call, locate the database */
12751360Seric 	if (firstcall)
12850581Seric 	{
12951362Seric 		extern void _udbx_init();
13051362Seric 
13151362Seric 		_udbx_init();
13251360Seric 		firstcall = FALSE;
13351362Seric 	}
13450581Seric 
13551362Seric 	/* if name is too long, assume it won't match */
13651362Seric 	if (strlen(a->q_user) > sizeof keybuf - 12)
13751362Seric 		return;
13851360Seric 
13951362Seric 	/* if name begins with a colon, it indicates our metadata */
14051362Seric 	if (a->q_user[0] == ':')
14151362Seric 		return;
14251360Seric 
14351362Seric 	/* build actual database key */
14451362Seric 	(void) strcpy(keybuf, a->q_user);
14551362Seric 	(void) strcat(keybuf, ":maildrop");
14651362Seric 	keylen = strlen(keybuf);
14751360Seric 
14851360Seric 	breakout = FALSE;
14951362Seric 	for (up = UdbEnts; !breakout; up++)
15050581Seric 	{
15151360Seric 		char *user;
15251360Seric 		struct timeval timeout;
15351360Seric 		fd_set fdset;
15450581Seric 
15551360Seric 		/*
15651360Seric 		**  Select action based on entry type.
15751360Seric 		**
15851360Seric 		**	On dropping out of this switch, "class" should
15951360Seric 		**	explain the type of the data, and "user" should
16051360Seric 		**	contain the user information.
16151360Seric 		*/
16250581Seric 
16351360Seric 		switch (up->udb_type)
16451360Seric 		{
16551360Seric 		  case UDB_LOOKUP:
16651362Seric 			key.data = keybuf;
16751362Seric 			key.size = keylen;
16851362Seric 			i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_CURSOR);
16951360Seric 			if (i != 0 || info.size <= 0)
17051360Seric 			{
17151360Seric 				if (i < 0)
17251360Seric 					syserr("udbexpand: db-get stat %s");
17351360Seric 				if (tTd(28, 2))
17451362Seric 					printf("expand: no match on %s\n", keybuf);
17551360Seric 				continue;
17651360Seric 			}
17750581Seric 
178*51830Seric 			while (i == 0 && key.size == keylen &&
179*51830Seric 					bcmp(key.data, keybuf, keylen) == 0)
18051362Seric 			{
181*51830Seric 				breakout = TRUE;
18251362Seric 				if (info.size < sizeof buf)
18351362Seric 					user = buf;
18451362Seric 				else
18551362Seric 					user = xalloc(info.size + 1);
18651362Seric 				bcopy(info.data, user, info.size);
18751362Seric 				user[info.size] = '\0';
18850581Seric 
18951362Seric 				message(Arpa_Info, "expanded to %s", user);
19051362Seric 				AliasLevel++;
19151362Seric 				sendtolist(user, a, sendq);
19251362Seric 				AliasLevel--;
19351362Seric 
19451362Seric 				if (user != buf)
19551362Seric 					free(user);
19651362Seric 
19751362Seric 				/* get the next record */
19851362Seric 				i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_NEXT);
199*51830Seric 			}
20051360Seric 			break;
20151360Seric 
20251360Seric 		  case UDB_REMOTE:
20351741Seric 			/* not yet implemented */
20451741Seric 			continue;
20551362Seric 
20651360Seric 		  case UDB_FORWARD:
20751360Seric 			i = strlen(up->udb_fwdhost) + strlen(a->q_user) + 1;
20851360Seric 			if (i < sizeof buf)
20951360Seric 				user = buf;
21051360Seric 			else
21151360Seric 				user = xalloc(i + 1);
21251360Seric 			(void) sprintf(user, "%s@%s", a->q_user, up->udb_fwdhost);
21351362Seric 			message(Arpa_Info, "expanded to %s", user);
21451362Seric 			AliasLevel++;
21551362Seric 			sendtolist(user, a, sendq);
21651362Seric 			AliasLevel--;
21751362Seric 			if (user != buf)
21851362Seric 				free(user);
21951362Seric 			breakout = TRUE;
22051360Seric 			break;
22151360Seric 
22251360Seric 		  case UDB_EOLIST:
22351360Seric 			breakout = TRUE;
22451360Seric 			continue;
22551360Seric 
22651360Seric 		  default:
22751360Seric 			/* unknown entry type */
22851360Seric 			continue;
22951360Seric 		}
23051362Seric 	}
23151362Seric }
23251360Seric 
23351363Seric #define MAXUDBOPTS	27
23451363Seric 
23551362Seric void
23651362Seric _udbx_init()
23751362Seric {
23851362Seric 	register char *p;
23951362Seric 	int i;
24051362Seric 	register struct udbent *up;
24151362Seric 	char buf[8192];
24251360Seric 
24351362Seric 	p = UdbSpec;
24451362Seric 	up = UdbEnts;
24551762Seric 	while (p != NULL)
24651362Seric 	{
24751362Seric 		char *spec;
24851362Seric 		auto int rcode;
24951363Seric 		int nopts;
25051362Seric 		int nmx;
25151362Seric 		register struct hostent *h;
25251362Seric 		char *mxhosts[MAXMXHOSTS + 1];
25351363Seric 		struct option opts[MAXUDBOPTS + 1];
25451362Seric 
25551362Seric 		while (*p == ' ' || *p == '\t' || *p == ',')
25651362Seric 			p++;
25751362Seric 		if (*p == '\0')
25851362Seric 			break;
25951362Seric 		spec = p;
26051362Seric 		p = index(p, ',');
26151761Seric 		if (p != NULL)
26251362Seric 			*p++ = '\0';
26351363Seric 
26451363Seric 		/* extract options */
26551363Seric 		nopts = _udb_parsespec(spec, opts, MAXUDBOPTS);
26651363Seric 
26751363Seric 		/*
26851363Seric 		**  Decode database specification.
26951363Seric 		**
27051363Seric 		**	In the sendmail tradition, the leading character
27151363Seric 		**	defines the semantics of the rest of the entry.
27251363Seric 		**
27351363Seric 		**	+hostname --	send a datagram to the udb server
27451363Seric 		**			on host "hostname" asking for the
27551363Seric 		**			home mail server for this user.
27651363Seric 		**	*hostname --	similar to +hostname, except that the
27751363Seric 		**			hostname is searched as an MX record;
27851363Seric 		**			resulting hosts are searched as for
27951363Seric 		**			+mxhostname.  If no MX host is found,
28051363Seric 		**			this is the same as +hostname.
28151363Seric 		**	@hostname --	forward email to the indicated host.
28251363Seric 		**			This should be the last in the list,
28351363Seric 		**			since it always matches the input.
28451363Seric 		**	/dbname	 --	search the named database on the local
28551363Seric 		**			host using the Berkeley db package.
28651363Seric 		*/
28751363Seric 
28851362Seric 		switch (*spec)
28951360Seric 		{
29051362Seric 		  case '+':	/* search remote database */
29151363Seric 		  case '*':	/* search remote database (expand MX) */
29251363Seric 			if (*spec == '*')
29351363Seric 			{
29451363Seric 				nmx = getmxrr(spec + 1, mxhosts, "", &rcode);
29551363Seric 				if (tTd(28, 16))
29651363Seric 				{
29751363Seric 					int i;
29851362Seric 
29951363Seric 					printf("getmxrr(%s): %d", spec + 1, nmx);
30051363Seric 					for (i = 0; i <= nmx; i++)
30151363Seric 						printf(" %s", mxhosts[i]);
30251363Seric 					printf("\n");
30351363Seric 				}
30451363Seric 			}
30551363Seric 			else
30651362Seric 			{
30751363Seric 				nmx = 1;
30851363Seric 				mxhosts[0] = spec + 1;
30951362Seric 			}
31051362Seric 
31151362Seric 			for (i = 0; i < nmx; i++)
31251362Seric 			{
31351362Seric 				h = gethostbyname(mxhosts[i]);
31451362Seric 				if (h == NULL)
31551362Seric 					continue;
31651362Seric 				up->udb_type = UDB_REMOTE;
31751362Seric 				up->udb_addr.sin_family = h->h_addrtype;
31851362Seric 				bcopy(h->h_addr_list[0],
31951362Seric 				      (char *) &up->udb_addr.sin_addr,
32051362Seric 				      h->h_length);
32151362Seric 				up->udb_addr.sin_port = UdbPort;
32251362Seric 				up->udb_timeout = UdbTimeout;
32351362Seric 				up++;
32451362Seric 			}
32551362Seric 
32651362Seric 			/* set up a datagram socket */
32751362Seric 			if (UdbSock < 0)
32851362Seric 			{
32951362Seric 				UdbSock = socket(AF_INET, SOCK_DGRAM, 0);
33051362Seric 				(void) fcntl(UdbSock, F_SETFD, 1);
33151362Seric 			}
33251362Seric 			break;
33351362Seric 
33451362Seric 		  case '@':	/* forward to remote host */
33551362Seric 			up->udb_type = UDB_FORWARD;
33651362Seric 			up->udb_fwdhost = spec + 1;
33751362Seric 			up++;
33851362Seric 			break;
33951362Seric 
34051362Seric 		  case '/':	/* look up remote name */
34151362Seric 			up->udb_dbp = dbopen(spec, O_RDONLY, 0644, DB_BTREE, NULL);
34251362Seric 			if (up->udb_dbp == NULL)
34351362Seric 				break;
34451362Seric 			up->udb_type = UDB_LOOKUP;
34551362Seric 			up++;
34651362Seric 			break;
34751360Seric 		}
34851362Seric 	}
34951362Seric 	up->udb_type = UDB_EOLIST;
35051360Seric 
35151362Seric 	if (tTd(28, 4))
35251362Seric 	{
35351362Seric 		for (up = UdbEnts; ; up++)
35451360Seric 		{
35551362Seric 			switch (up->udb_type)
35651362Seric 			{
35751362Seric 			  case UDB_EOLIST:
35851362Seric 				return;
35951362Seric 
36051362Seric 			  case UDB_REMOTE:
36151362Seric 				printf("REMOTE: addr %s, timeo %d\n",
36251362Seric 					inet_ntoa(up->udb_addr.sin_addr),
36351362Seric 					up->udb_timeout);
36451362Seric 				break;
36551362Seric 
36651362Seric 			  case UDB_LOOKUP:
367*51830Seric 				printf("LOOKUP: file %s\n",
368*51830Seric 					up->udb_dbname);
36951362Seric 				break;
37051362Seric 
37151362Seric 			  case UDB_FORWARD:
37251362Seric 				printf("FORWARD: host %s\n",
37351362Seric 					up->udb_fwdhost);
37451362Seric 				break;
37551362Seric 
37651362Seric 			  default:
37751362Seric 				printf("UNKNOWN\n");
37851362Seric 				break;
37951362Seric 			}
38051360Seric 		}
38150581Seric 	}
38251360Seric }
38350581Seric 
38451363Seric int
38551363Seric _udb_parsespec(udbspec, opt, maxopts)
38651363Seric 	char *udbspec;
38751363Seric 	struct option opt[];
38851363Seric 	int maxopts;
38951363Seric {
39051363Seric 	register char *spec;
39151363Seric 	register char *spec_end;
39251363Seric 	register int optnum;
39351363Seric 
39451363Seric 	spec_end = index(udbspec, ':');
39551363Seric 	for (optnum = 0; optnum < maxopts && (spec = spec_end) != NULL; optnum++)
39651363Seric 	{
39751363Seric 		register char *p;
39851363Seric 
39951363Seric 		while (isspace(*spec))
40051363Seric 			spec++;
40151363Seric 		spec_end = index(spec, ':');
40251363Seric 		if (spec_end != NULL)
40351363Seric 			*spec_end++ = '\0';
40451363Seric 
40551363Seric 		opt[optnum].name = spec;
40651363Seric 		opt[optnum].val = NULL;
40751363Seric 		p = index(spec, '=');
40851363Seric 		if (p != NULL)
40951363Seric 			opt[optnum].val = ++p;
41051363Seric 	}
41151363Seric 	return optnum;
41251363Seric }
41351363Seric 
41451360Seric #else /* not USERDB */
41551360Seric 
41651360Seric void
41751360Seric udbexpand(a, sendq)
41851360Seric 	ADDRESS *a;
41951360Seric 	ADDRESS **sendq;
42051360Seric {
42151360Seric 	return;
42250581Seric }
42350581Seric 
42450581Seric #endif /* USERDB */
425