xref: /csrg-svn/usr.sbin/sendmail/src/udb.c (revision 58050)
1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988 Regents of the University of California.
4  * All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 #ifdef USERDB
11 static char sccsid [] = "@(#)udb.c	6.7 (Berkeley) 02/18/93 (with USERDB)";
12 #else
13 static char sccsid [] = "@(#)udb.c	6.7 (Berkeley) 02/18/93 (without USERDB)";
14 #endif
15 #endif
16 
17 #include "sendmail.h"
18 
19 #ifdef USERDB
20 
21 #include <sys/time.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <netdb.h>
25 #include <db.h>
26 
27 /*
28 **  UDB.C -- interface between sendmail and Berkeley User Data Base.
29 **
30 **	This depends on the 4.4BSD db package.
31 */
32 
33 
34 struct udbent
35 {
36 	char	*udb_spec;		/* string version of spec */
37 	int	udb_type;		/* type of entry */
38 	char	*udb_default;		/* default host for outgoing mail */
39 	union
40 	{
41 		/* type UE_REMOTE -- do remote call for lookup */
42 		struct
43 		{
44 			struct sockaddr_in _udb_addr;	/* address */
45 			int		_udb_timeout;	/* timeout */
46 		} udb_remote;
47 #define udb_addr	udb_u.udb_remote._udb_addr
48 #define udb_timeout	udb_u.udb_remote._udb_timeout
49 
50 		/* type UE_FORWARD -- forward message to remote */
51 		struct
52 		{
53 			char	*_udb_fwdhost;	/* name of forward host */
54 		} udb_forward;
55 #define udb_fwdhost	udb_u.udb_forward._udb_fwdhost
56 
57 		/* type UE_FETCH -- lookup in local database */
58 		struct
59 		{
60 			char	*_udb_dbname;	/* pathname of database */
61 			DB	*_udb_dbp;	/* open database ptr */
62 		} udb_lookup;
63 #define udb_dbname	udb_u.udb_lookup._udb_dbname
64 #define udb_dbp		udb_u.udb_lookup._udb_dbp
65 	} udb_u;
66 };
67 
68 #define UDB_EOLIST	0	/* end of list */
69 #define UDB_SKIP	1	/* skip this entry */
70 #define UDB_REMOTE	2	/* look up in remote database */
71 #define UDB_DBFETCH	3	/* look up in local database */
72 #define UDB_FORWARD	4	/* forward to remote host */
73 
74 #define MAXUDBENT	10	/* maximum number of UDB entries */
75 
76 
77 struct option
78 {
79 	char	*name;
80 	char	*val;
81 };
82 /*
83 **  UDBEXPAND -- look up user in database and expand
84 **
85 **	Parameters:
86 **		a -- address to expand.
87 **		sendq -- pointer to head of sendq to put the expansions in.
88 **
89 **	Returns:
90 **		EX_TEMPFAIL -- if something "odd" happened -- probably due
91 **			to accessing a file on an NFS server that is down.
92 **		EX_OK -- otherwise.
93 **
94 **	Side Effects:
95 **		Modifies sendq.
96 */
97 
98 int	UdbPort = 1616;
99 int	UdbTimeout = 10;
100 
101 struct udbent	UdbEnts[MAXUDBENT + 1];
102 int		UdbSock = -1;
103 bool		UdbInitialized = FALSE;
104 
105 int
106 udbexpand(a, sendq, e)
107 	register ADDRESS *a;
108 	ADDRESS **sendq;
109 	register ENVELOPE *e;
110 {
111 	int i;
112 	register char *p;
113 	DBT key;
114 	DBT info;
115 	bool breakout;
116 	register struct udbent *up;
117 	int keylen;
118 	char keybuf[MAXKEY];
119 	char buf[BUFSIZ];
120 
121 	if (tTd(28, 1))
122 		printf("expand(%s)\n", a->q_paddr);
123 
124 	/* make certain we are supposed to send to this address */
125 	if (bitset(QDONTSEND, a->q_flags))
126 		return EX_OK;
127 	e->e_to = a->q_paddr;
128 
129 	/* on first call, locate the database */
130 	if (!UdbInitialized)
131 	{
132 		extern int _udbx_init();
133 
134 		if (_udbx_init() == EX_TEMPFAIL)
135 			return EX_TEMPFAIL;
136 	}
137 
138 	/* short circuit the process if no chance of a match */
139 	if (UdbSpec == NULL || UdbSpec[0] == '\0')
140 		return EX_OK;
141 
142 	/* if name is too long, assume it won't match */
143 	if (strlen(a->q_user) > sizeof keybuf - 12)
144 		return EX_OK;
145 
146 	/* if name begins with a colon, it indicates our metadata */
147 	if (a->q_user[0] == ':')
148 		return EX_OK;
149 
150 	/* build actual database key */
151 	(void) strcpy(keybuf, a->q_user);
152 	(void) strcat(keybuf, ":maildrop");
153 	keylen = strlen(keybuf);
154 
155 	breakout = FALSE;
156 	for (up = UdbEnts; !breakout; up++)
157 	{
158 		char *user;
159 
160 		/*
161 		**  Select action based on entry type.
162 		**
163 		**	On dropping out of this switch, "class" should
164 		**	explain the type of the data, and "user" should
165 		**	contain the user information.
166 		*/
167 
168 		switch (up->udb_type)
169 		{
170 		  case UDB_DBFETCH:
171 			key.data = keybuf;
172 			key.size = keylen;
173 			i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_CURSOR);
174 			if (i > 0 || info.size <= 0)
175 			{
176 				if (tTd(28, 2))
177 					printf("expand: no match on %s\n", keybuf);
178 				continue;
179 			}
180 
181 			while (i == 0 && key.size == keylen &&
182 					bcmp(key.data, keybuf, keylen) == 0)
183 			{
184 				breakout = TRUE;
185 				if (info.size < sizeof buf)
186 					user = buf;
187 				else
188 					user = xalloc(info.size + 1);
189 				bcopy(info.data, user, info.size);
190 				user[info.size] = '\0';
191 
192 				message(Arpa_Info, "expanded to %s", user);
193 #ifdef LOG
194 				if (LogLevel >= 10)
195 					syslog(LOG_INFO, "%s: expand %s => %s",
196 						e->e_id, e->e_to, user);
197 #endif
198 				AliasLevel++;
199 				sendtolist(user, a, sendq, e);
200 				AliasLevel--;
201 
202 				if (user != buf)
203 					free(user);
204 
205 				/* get the next record */
206 				i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_NEXT);
207 			}
208 			if (i < 0)
209 			{
210 				syserr("udbexpand: db-get %.*s stat %d",
211 					key.size, key.data, i);
212 				return EX_TEMPFAIL;
213 			}
214 			break;
215 
216 		  case UDB_REMOTE:
217 			/* not yet implemented */
218 			continue;
219 
220 		  case UDB_FORWARD:
221 			i = strlen(up->udb_fwdhost) + strlen(a->q_user) + 1;
222 			if (i < sizeof buf)
223 				user = buf;
224 			else
225 				user = xalloc(i + 1);
226 			(void) sprintf(user, "%s@%s", a->q_user, up->udb_fwdhost);
227 			message(Arpa_Info, "expanded to %s", user);
228 			AliasLevel++;
229 			sendtolist(user, a, sendq, e);
230 			AliasLevel--;
231 			if (user != buf)
232 				free(user);
233 			breakout = TRUE;
234 			break;
235 
236 		  case UDB_EOLIST:
237 			breakout = TRUE;
238 			continue;
239 
240 		  default:
241 			/* unknown entry type */
242 			continue;
243 		}
244 	}
245 	return EX_OK;
246 }
247 /*
248 **  UDBSENDER -- return canonical external name of sender, given local name
249 **
250 **	Parameters:
251 **		sender -- the name of the sender on the local machine.
252 **
253 **	Returns:
254 **		The external name for this sender, if derivable from the
255 **			database.
256 **		NULL -- if nothing is changed from the database.
257 **
258 **	Side Effects:
259 **		none.
260 */
261 
262 char *
263 udbsender(sender)
264 	char *sender;
265 {
266 	register char *p;
267 	register struct udbent *up;
268 	int i;
269 	int keylen;
270 	DBT key, info;
271 	char keybuf[MAXKEY];
272 
273 	if (tTd(28, 1))
274 		printf("udbsender(%s)\n", sender);
275 
276 	if (!UdbInitialized)
277 	{
278 		if (_udbx_init() == EX_TEMPFAIL)
279 			return NULL;
280 	}
281 
282 	/* short circuit if no spec */
283 	if (UdbSpec == NULL || UdbSpec[0] == '\0')
284 		return NULL;
285 
286 	/* long names can never match and are a pain to deal with */
287 	if (strlen(sender) > sizeof keybuf - 12)
288 		return NULL;
289 
290 	/* names beginning with colons indicate metadata */
291 	if (sender[0] == ':')
292 		return NULL;
293 
294 	/* build database key */
295 	(void) strcpy(keybuf, sender);
296 	(void) strcat(keybuf, ":mailname");
297 	keylen = strlen(keybuf);
298 
299 	for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
300 	{
301 		/*
302 		**  Select action based on entry type.
303 		*/
304 
305 		switch (up->udb_type)
306 		{
307 		  case UDB_DBFETCH:
308 			key.data = keybuf;
309 			key.size = keylen;
310 			i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
311 			if (i != 0 || info.size <= 0)
312 			{
313 				if (tTd(28, 2))
314 					printf("udbsender: no match on %s\n",
315 							keybuf);
316 				continue;
317 			}
318 
319 			p = xalloc(info.size + 1);
320 			bcopy(info.data, p, info.size);
321 			p[info.size] = '\0';
322 			if (tTd(28, 1))
323 				printf("udbsender ==> %s\n", p);
324 			return p;
325 		}
326 	}
327 
328 	/*
329 	**  Nothing yet.  Search again for a default case.  But only
330 	**  use it if we also have a forward (:maildrop) pointer already
331 	**  in the database.
332 	*/
333 
334 	/* build database key */
335 	(void) strcpy(keybuf, sender);
336 	(void) strcat(keybuf, ":maildrop");
337 	keylen = strlen(keybuf);
338 
339 	for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
340 	{
341 		switch (up->udb_type)
342 		{
343 		  case UDB_DBFETCH:
344 			/* get the default case for this database */
345 			if (up->udb_default == NULL)
346 			{
347 				key.data = ":default:mailname";
348 				key.size = strlen(key.data);
349 				i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
350 				if (i != 0 || info.size <= 0)
351 				{
352 					/* no default case */
353 					up->udb_default = "";
354 					continue;
355 				}
356 
357 				/* save the default case */
358 				up->udb_default = xalloc(info.size + 1);
359 				bcopy(info.data, up->udb_default, info.size);
360 				up->udb_default[info.size] = '\0';
361 			}
362 			else if (up->udb_default[0] == '\0')
363 				continue;
364 
365 			/* we have a default case -- verify user:maildrop */
366 			key.data = keybuf;
367 			key.size = keylen;
368 			i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0);
369 			if (i != 0 || info.size <= 0)
370 			{
371 				/* nope -- no aliasing for this user */
372 				continue;
373 			}
374 
375 			/* they exist -- build the actual address */
376 			p = xalloc(strlen(sender) + strlen(up->udb_default) + 2);
377 			(void) strcpy(p, sender);
378 			(void) strcat(p, "@");
379 			(void) strcat(p, up->udb_default);
380 			if (tTd(28, 1))
381 				printf("udbsender ==> %s\n", p);
382 			return p;
383 		}
384 	}
385 
386 	/* still nothing....  too bad */
387 	return NULL;
388 }
389 /*
390 **  _UDBX_INIT -- parse the UDB specification, opening any valid entries.
391 **
392 **	Parameters:
393 **		none.
394 **
395 **	Returns:
396 **		EX_TEMPFAIL -- if it appeared it couldn't get hold of a
397 **			database due to a host being down or some similar
398 **			(recoverable) situation.
399 **		EX_OK -- otherwise.
400 **
401 **	Side Effects:
402 **		Fills in the UdbEnts structure from UdbSpec.
403 */
404 
405 #define MAXUDBOPTS	27
406 
407 int
408 _udbx_init()
409 {
410 	register char *p;
411 	int i;
412 	register struct udbent *up;
413 	char buf[BUFSIZ];
414 
415 	if (UdbInitialized)
416 		return EX_OK;
417 
418 # ifdef UDB_DEFAULT_SPEC
419 	if (UdbSpec == NULL)
420 		UdbSpec = UDB_DEFAULT_SPEC;
421 # endif
422 
423 	p = UdbSpec;
424 	up = UdbEnts;
425 	while (p != NULL)
426 	{
427 		char *spec;
428 		auto int rcode;
429 		int nopts;
430 		int nmx;
431 		register struct hostent *h;
432 		char *mxhosts[MAXMXHOSTS + 1];
433 		struct option opts[MAXUDBOPTS + 1];
434 
435 		while (*p == ' ' || *p == '\t' || *p == ',')
436 			p++;
437 		if (*p == '\0')
438 			break;
439 		spec = p;
440 		p = strchr(p, ',');
441 		if (p != NULL)
442 			*p++ = '\0';
443 
444 		/* extract options */
445 		nopts = _udb_parsespec(spec, opts, MAXUDBOPTS);
446 
447 		/*
448 		**  Decode database specification.
449 		**
450 		**	In the sendmail tradition, the leading character
451 		**	defines the semantics of the rest of the entry.
452 		**
453 		**	+hostname --	send a datagram to the udb server
454 		**			on host "hostname" asking for the
455 		**			home mail server for this user.
456 		**	*hostname --	similar to +hostname, except that the
457 		**			hostname is searched as an MX record;
458 		**			resulting hosts are searched as for
459 		**			+mxhostname.  If no MX host is found,
460 		**			this is the same as +hostname.
461 		**	@hostname --	forward email to the indicated host.
462 		**			This should be the last in the list,
463 		**			since it always matches the input.
464 		**	/dbname	 --	search the named database on the local
465 		**			host using the Berkeley db package.
466 		*/
467 
468 		switch (*spec)
469 		{
470 		  case '+':	/* search remote database */
471 		  case '*':	/* search remote database (expand MX) */
472 			if (*spec == '*')
473 			{
474 #ifdef NAMED_BIND
475 				nmx = getmxrr(spec + 1, mxhosts, "", &rcode);
476 #else
477 				mxhosts[0] = spec + 1;
478 				nmx = 1;
479 				rcode = 0;
480 #endif
481 				if (tTd(28, 16))
482 				{
483 					int i;
484 
485 					printf("getmxrr(%s): %d", spec + 1, nmx);
486 					for (i = 0; i <= nmx; i++)
487 						printf(" %s", mxhosts[i]);
488 					printf("\n");
489 				}
490 			}
491 			else
492 			{
493 				nmx = 1;
494 				mxhosts[0] = spec + 1;
495 			}
496 
497 			for (i = 0; i < nmx; i++)
498 			{
499 				h = gethostbyname(mxhosts[i]);
500 				if (h == NULL)
501 					continue;
502 				up->udb_type = UDB_REMOTE;
503 				up->udb_addr.sin_family = h->h_addrtype;
504 				bcopy(h->h_addr_list[0],
505 				      (char *) &up->udb_addr.sin_addr,
506 				      h->h_length);
507 				up->udb_addr.sin_port = UdbPort;
508 				up->udb_timeout = UdbTimeout;
509 				up++;
510 			}
511 
512 			/* set up a datagram socket */
513 			if (UdbSock < 0)
514 			{
515 				UdbSock = socket(AF_INET, SOCK_DGRAM, 0);
516 				(void) fcntl(UdbSock, F_SETFD, 1);
517 			}
518 			break;
519 
520 		  case '@':	/* forward to remote host */
521 			up->udb_type = UDB_FORWARD;
522 			up->udb_fwdhost = spec + 1;
523 			up++;
524 			break;
525 
526 		  case '/':	/* look up remote name */
527 			up->udb_dbname = spec;
528 			errno = 0;
529 			up->udb_dbp = dbopen(spec, O_RDONLY, 0644, DB_BTREE, NULL);
530 			if (up->udb_dbp == NULL)
531 			{
532 				if (errno != ENOENT && errno != EACCES)
533 				{
534 					up->udb_type = UDB_EOLIST;
535 					goto tempfail;
536 				}
537 				break;
538 			}
539 			up->udb_type = UDB_DBFETCH;
540 			up++;
541 			break;
542 		}
543 	}
544 	up->udb_type = UDB_EOLIST;
545 
546 	if (tTd(28, 4))
547 	{
548 		for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
549 		{
550 			switch (up->udb_type)
551 			{
552 			  case UDB_REMOTE:
553 				printf("REMOTE: addr %s, timeo %d\n",
554 					inet_ntoa(up->udb_addr.sin_addr),
555 					up->udb_timeout);
556 				break;
557 
558 			  case UDB_DBFETCH:
559 				printf("FETCH: file %s\n",
560 					up->udb_dbname);
561 				break;
562 
563 			  case UDB_FORWARD:
564 				printf("FORWARD: host %s\n",
565 					up->udb_fwdhost);
566 				break;
567 
568 			  default:
569 				printf("UNKNOWN\n");
570 				break;
571 			}
572 		}
573 	}
574 
575 	UdbInitialized = TRUE;
576 	errno = 0;
577 	return EX_OK;
578 
579 	/*
580 	**  On temporary failure, back out anything we've already done
581 	*/
582 
583   tempfail:
584 	for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++)
585 	{
586 		if (up->udb_type == UDB_DBFETCH)
587 		{
588 			(*up->udb_dbp->close)(up->udb_dbp);
589 		}
590 	}
591 	return EX_TEMPFAIL;
592 }
593 
594 int
595 _udb_parsespec(udbspec, opt, maxopts)
596 	char *udbspec;
597 	struct option opt[];
598 	int maxopts;
599 {
600 	register char *spec;
601 	register char *spec_end;
602 	register int optnum;
603 
604 	spec_end = strchr(udbspec, ':');
605 	for (optnum = 0; optnum < maxopts && (spec = spec_end) != NULL; optnum++)
606 	{
607 		register char *p;
608 
609 		while (isascii(*spec) && isspace(*spec))
610 			spec++;
611 		spec_end = strchr(spec, ':');
612 		if (spec_end != NULL)
613 			*spec_end++ = '\0';
614 
615 		opt[optnum].name = spec;
616 		opt[optnum].val = NULL;
617 		p = strchr(spec, '=');
618 		if (p != NULL)
619 			opt[optnum].val = ++p;
620 	}
621 	return optnum;
622 }
623 
624 #else /* not USERDB */
625 
626 int
627 udbexpand(a, sendq, e)
628 	ADDRESS *a;
629 	ADDRESS **sendq;
630 	ENVELOPE *e;
631 {
632 	return EX_OK;
633 }
634 
635 #endif /* USERDB */
636