xref: /freebsd/libexec/revnetgroup/hash.c (revision 98fc0505155575df4170c863107153df2a751a13)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1995
5  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #ifndef lint
36 static const char rcsid[] =
37   "$FreeBSD$";
38 #endif /* not lint */
39 
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <sys/types.h>
44 #include "hash.h"
45 
46 /*
47  * This hash function is stolen directly from the
48  * Berkeley DB package. It already exists inside libc, but
49  * it's declared static which prevents us from calling it
50  * from here.
51  */
52 /*
53  * OZ's original sdbm hash
54  */
55 u_int32_t
56 hash(const void *keyarg, size_t len)
57 {
58 	const u_char *key;
59 	size_t loop;
60 	u_int32_t h;
61 
62 #define HASHC   h = *key++ + 65599 * h
63 
64 	h = 0;
65 	key = keyarg;
66 	if (len > 0) {
67 		loop = (len + 8 - 1) >> 3;
68 
69 		switch (len & (8 - 1)) {
70 		case 0:
71 			do {
72 				HASHC;
73 				/* FALLTHROUGH */
74 		case 7:
75 				HASHC;
76 				/* FALLTHROUGH */
77 		case 6:
78 				HASHC;
79 				/* FALLTHROUGH */
80 		case 5:
81 				HASHC;
82 				/* FALLTHROUGH */
83 		case 4:
84 				HASHC;
85 				/* FALLTHROUGH */
86 		case 3:
87 				HASHC;
88 				/* FALLTHROUGH */
89 		case 2:
90 				HASHC;
91 				/* FALLTHROUGH */
92 		case 1:
93 				HASHC;
94 			} while (--loop);
95 		}
96 	}
97 	return (h);
98 }
99 
100 /*
101  * Generate a hash value for a given key (character string).
102  * We mask off all but the lower 8 bits since our table array
103  * can only hold 256 elements.
104  */
105 u_int32_t
106 hashkey(char *key)
107 {
108 
109 	if (key == NULL)
110 		return (-1);
111 	return(hash((void *)key, strlen(key)) & HASH_MASK);
112 }
113 
114 /* Find an entry in the hash table (may be hanging off a linked list). */
115 char *
116 lookup(struct group_entry *table[], char *key)
117 {
118 	struct group_entry *cur;
119 
120 	cur = table[hashkey(key)];
121 
122 	while (cur) {
123 		if (!strcmp(cur->key, key))
124 			return(cur->data);
125 		cur = cur->next;
126 	}
127 
128 	return(NULL);
129 }
130 
131 /*
132  * Store an entry in the main netgroup hash table. Here's how this
133  * works: the table can only be so big when we initialize it (TABLESIZE)
134  * but the number of netgroups in the /etc/netgroup file could easily be
135  * much larger than the table. Since our hash values are adjusted to
136  * never be greater than TABLESIZE too, this means it won't be long before
137  * we find ourselves with two keys that hash to the same value.
138  *
139  * One way to deal with this is to malloc(2) a second table and start
140  * doing indirection, but this is a pain in the butt and it's not worth
141  * going to all that trouble for a dinky little program like this. Instead,
142  * we turn each table entry into a linked list and simply link keys
143  * with the same hash value together at the same index location within
144  * the table.
145  *
146  * That's a lot of comment for such a small piece of code, isn't it.
147  */
148 void
149 store(struct group_entry *table[], char *key, char *data)
150 {
151 	struct group_entry *new;
152 	u_int32_t i;
153 
154 	i = hashkey(key);
155 
156 	new = (struct group_entry *)malloc(sizeof(struct group_entry));
157 	new->key = strdup(key);
158 	new->data = strdup(data);
159 	new->next = table[i];
160 	table[i] = new;
161 
162 	return;
163 }
164 
165 /*
166  * Store a group member entry and/or update its grouplist. This is
167  * a bit more complicated than the previous function since we have to
168  * maintain not only the hash table of group members, each group member
169  * structure also has a linked list of groups hung off it. If handed
170  * a member name that we haven't encountered before, we have to do
171  * two things: add that member to the table (possibly hanging them
172  * off the end of a linked list, as above), and add a group name to
173  * the member's grouplist list. If we're handed a name that already has
174  * an entry in the table, then we just have to do one thing, which is
175  * to update its grouplist.
176  */
177 void
178 mstore(struct member_entry *table[], char *key, char *data, char *domain)
179 {
180 	struct member_entry *cur, *new;
181 	struct grouplist *tmp;
182 	u_int32_t i;
183 
184 	i = hashkey(key);
185 	cur = table[i];
186 
187 	tmp = (struct grouplist *)malloc(sizeof(struct grouplist));
188 	tmp->groupname = strdup(data);
189 	tmp->next = NULL;
190 
191 	/* Check if all we have to do is insert a new groupname. */
192 	while (cur) {
193 		if (!strcmp(cur->key, key)) {
194 			tmp->next = cur->groups;
195 			cur->groups = tmp;
196 			return;
197 		}
198 		cur = cur->next;
199 	}
200 
201 	/* Didn't find a match -- add the whole mess to the table. */
202 	new = (struct member_entry *)malloc(sizeof(struct member_entry));
203 	new->key = strdup(key);
204 	new->domain = domain ? strdup(domain) : "*";
205 	new->groups = tmp;
206 	new->next = table[i];
207 	table[i] = new;
208 
209 	return;
210 }
211