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