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