xref: /csrg-svn/usr.sbin/sendmail/src/udb.c (revision 50581)
1*50581Seric /*
2*50581Seric  * Copyright (c) 1983 Eric P. Allman
3*50581Seric  * Copyright (c) 1988 Regents of the University of California.
4*50581Seric  * All rights reserved.
5*50581Seric  *
6*50581Seric  * %sccs.include.redist.c%
7*50581Seric  */
8*50581Seric 
9*50581Seric #ifndef lint
10*50581Seric static char sccsid [] = "@(#)udb.c	5.1 (Berkeley) 07/26/91";
11*50581Seric #endif
12*50581Seric 
13*50581Seric #include "sendmail.h"
14*50581Seric 
15*50581Seric #ifdef USERDB
16*50581Seric 
17*50581Seric #include <sys/file.h>
18*50581Seric #include <db.h>
19*50581Seric 
20*50581Seric /*
21*50581Seric **  UDBEXPAND -- look up user in database and expand
22*50581Seric **
23*50581Seric **	Parameters:
24*50581Seric **		a -- address to expand.
25*50581Seric **		sendq -- pointer to head of sendq to put the expansions in.
26*50581Seric **
27*50581Seric **	Returns:
28*50581Seric **		none.
29*50581Seric **
30*50581Seric **	Side Effects:
31*50581Seric **		Modifies sendq.
32*50581Seric */
33*50581Seric 
34*50581Seric void
35*50581Seric udbexpand(a, sendq)
36*50581Seric 	register ADDRESS *a;
37*50581Seric 	ADDRESS **sendq;
38*50581Seric {
39*50581Seric 	int i;
40*50581Seric 	register char *p;
41*50581Seric 	auto char *class;
42*50581Seric 	auto char *list;
43*50581Seric 	DBT key;
44*50581Seric 	DBT info;
45*50581Seric 	static DB *dbp = NULL;
46*50581Seric 	register char *bp;
47*50581Seric 	char buf[8192];
48*50581Seric 
49*50581Seric 	if (tTd(28, 1))
50*50581Seric 		printf("expand(%s)\n", a->q_paddr);
51*50581Seric 
52*50581Seric 	/* make certain we are supposed to send to this address */
53*50581Seric 	if (bitset(QDONTSEND, a->q_flags))
54*50581Seric 		return;
55*50581Seric 	CurEnv->e_to = a->q_paddr;
56*50581Seric 
57*50581Seric 	/* if necessary, open the database */
58*50581Seric 	if (dbp == NULL)
59*50581Seric 	{
60*50581Seric 		if (UdbFileName == NULL || UdbFileName[0] == '\0')
61*50581Seric 		{
62*50581Seric 			if (tTd(28, 4))
63*50581Seric 				printf("no userdb specified\n");
64*50581Seric 			return;
65*50581Seric 		}
66*50581Seric 		dbp = hash_open(UdbFileName, O_RDONLY, 0644, NULL);
67*50581Seric 		if (dbp == NULL)
68*50581Seric 		{
69*50581Seric 			extern int errno;
70*50581Seric 
71*50581Seric 			if (tTd(28, 2))
72*50581Seric 				printf("cannot open %s: %d\n", UdbFileName, errno);
73*50581Seric 			return;
74*50581Seric 		}
75*50581Seric 	}
76*50581Seric 
77*50581Seric 	key.data = a->q_user;
78*50581Seric 	key.size = strlen(key.data);
79*50581Seric 	i = dbp->get(dbp, &key, &info, R_NOOVERWRITE);
80*50581Seric 	if (i != 0 || info.size <= 0)
81*50581Seric 	{
82*50581Seric 		if (i < 0)
83*50581Seric 			syserr("udbexpand: db-get stat %s");
84*50581Seric 		if (tTd(28, 2))
85*50581Seric 			printf("expand: no match on %s\n", key.data);
86*50581Seric 		return;
87*50581Seric 	}
88*50581Seric 
89*50581Seric 	/* extract the class (first string) and data (second string) */
90*50581Seric 	i = strlen((char *) info.data) + 1;
91*50581Seric 	p = (char *) info.data + i;
92*50581Seric 	i = info.size - i;
93*50581Seric 
94*50581Seric 	/* use internal buffer if it will fit; otherwise malloc */
95*50581Seric 	if (i < sizeof buf)
96*50581Seric 		bp = buf;
97*50581Seric 	else
98*50581Seric 		bp = xalloc(i + 1);
99*50581Seric 	bcopy(p, bp, i);
100*50581Seric 	bp[i] = '\0';
101*50581Seric 
102*50581Seric 	if (tTd(28, 1))
103*50581Seric 		printf("Class %s: %s\n", info.data, bp);
104*50581Seric 
105*50581Seric 	/* do special processing based on class */
106*50581Seric 	if (strcmp((char *) info.data, "user") == 0)
107*50581Seric 	{
108*50581Seric 		message(Arpa_Info, "expanded to (%s) %s", info.data, bp);
109*50581Seric 		AliasLevel++;
110*50581Seric 		sendtolist(bp, a, sendq);
111*50581Seric 		AliasLevel--;
112*50581Seric 	}
113*50581Seric 
114*50581Seric 	/* free memory if we allocated it */
115*50581Seric 	if (bp != buf)
116*50581Seric 		free(bp);
117*50581Seric }
118*50581Seric 
119*50581Seric #endif /* USERDB */
120