xref: /freebsd/libexec/revnetgroup/hash.c (revision eba230afba4932f02a1ca44efc797cf7499a5cb0)
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 
35a44e4d14SBill Paul #include <stdio.h>
36a44e4d14SBill Paul #include <stdlib.h>
37a44e4d14SBill Paul #include <string.h>
38a44e4d14SBill Paul #include <sys/types.h>
39a44e4d14SBill Paul #include "hash.h"
40a44e4d14SBill Paul 
41a44e4d14SBill Paul /*
42a44e4d14SBill Paul  * This hash function is stolen directly from the
43a44e4d14SBill Paul  * Berkeley DB package. It already exists inside libc, but
44a44e4d14SBill Paul  * it's declared static which prevents us from calling it
45a44e4d14SBill Paul  * from here.
46a44e4d14SBill Paul  */
47a44e4d14SBill Paul /*
48a44e4d14SBill Paul  * OZ's original sdbm hash
49a44e4d14SBill Paul  */
50a44e4d14SBill Paul u_int32_t
hash(const void * keyarg,size_t len)51266ebcd3SWarner Losh hash(const void *keyarg, size_t len)
52a44e4d14SBill Paul {
53266ebcd3SWarner Losh 	const u_char *key;
54266ebcd3SWarner Losh 	size_t loop;
55266ebcd3SWarner Losh 	u_int32_t h;
56a44e4d14SBill Paul 
57a44e4d14SBill Paul #define HASHC   h = *key++ + 65599 * h
58a44e4d14SBill Paul 
59a44e4d14SBill Paul 	h = 0;
60a44e4d14SBill Paul 	key = keyarg;
61a44e4d14SBill Paul 	if (len > 0) {
62a44e4d14SBill Paul 		loop = (len + 8 - 1) >> 3;
63a44e4d14SBill Paul 
64a44e4d14SBill Paul 		switch (len & (8 - 1)) {
65a44e4d14SBill Paul 		case 0:
66a44e4d14SBill Paul 			do {
67a44e4d14SBill Paul 				HASHC;
68a44e4d14SBill Paul 				/* FALLTHROUGH */
69a44e4d14SBill Paul 		case 7:
70a44e4d14SBill Paul 				HASHC;
71a44e4d14SBill Paul 				/* FALLTHROUGH */
72a44e4d14SBill Paul 		case 6:
73a44e4d14SBill Paul 				HASHC;
74a44e4d14SBill Paul 				/* FALLTHROUGH */
75a44e4d14SBill Paul 		case 5:
76a44e4d14SBill Paul 				HASHC;
77a44e4d14SBill Paul 				/* FALLTHROUGH */
78a44e4d14SBill Paul 		case 4:
79a44e4d14SBill Paul 				HASHC;
80a44e4d14SBill Paul 				/* FALLTHROUGH */
81a44e4d14SBill Paul 		case 3:
82a44e4d14SBill Paul 				HASHC;
83a44e4d14SBill Paul 				/* FALLTHROUGH */
84a44e4d14SBill Paul 		case 2:
85a44e4d14SBill Paul 				HASHC;
86a44e4d14SBill Paul 				/* FALLTHROUGH */
87a44e4d14SBill Paul 		case 1:
88a44e4d14SBill Paul 				HASHC;
89a44e4d14SBill Paul 			} while (--loop);
90a44e4d14SBill Paul 		}
91a44e4d14SBill Paul 	}
92a44e4d14SBill Paul 	return (h);
93a44e4d14SBill Paul }
94a44e4d14SBill Paul 
95a44e4d14SBill Paul /*
96a44e4d14SBill Paul  * Generate a hash value for a given key (character string).
97a44e4d14SBill Paul  * We mask off all but the lower 8 bits since our table array
98d7b71c67SBill Paul  * can only hold 256 elements.
99a44e4d14SBill Paul  */
100266ebcd3SWarner Losh u_int32_t
hashkey(char * key)101266ebcd3SWarner Losh hashkey(char *key)
102a44e4d14SBill Paul {
103a44e4d14SBill Paul 
104a44e4d14SBill Paul 	if (key == NULL)
105a44e4d14SBill Paul 		return (-1);
106a44e4d14SBill Paul 	return(hash((void *)key, strlen(key)) & HASH_MASK);
107a44e4d14SBill Paul }
108a44e4d14SBill Paul 
109a44e4d14SBill Paul /* Find an entry in the hash table (may be hanging off a linked list). */
110266ebcd3SWarner Losh char *
lookup(struct group_entry * table[],char * key)111266ebcd3SWarner Losh lookup(struct group_entry *table[], char *key)
112a44e4d14SBill Paul {
113a44e4d14SBill Paul 	struct group_entry *cur;
114a44e4d14SBill Paul 
115a44e4d14SBill Paul 	cur = table[hashkey(key)];
116a44e4d14SBill Paul 
117a44e4d14SBill Paul 	while (cur) {
118a44e4d14SBill Paul 		if (!strcmp(cur->key, key))
119a44e4d14SBill Paul 			return(cur->data);
120a44e4d14SBill Paul 		cur = cur->next;
121a44e4d14SBill Paul 	}
122a44e4d14SBill Paul 
123a44e4d14SBill Paul 	return(NULL);
124a44e4d14SBill Paul }
125a44e4d14SBill Paul 
126a44e4d14SBill Paul /*
127a44e4d14SBill Paul  * Store an entry in the main netgroup hash table. Here's how this
128a44e4d14SBill Paul  * works: the table can only be so big when we initialize it (TABLESIZE)
129a44e4d14SBill Paul  * but the number of netgroups in the /etc/netgroup file could easily be
130a44e4d14SBill Paul  * much larger than the table. Since our hash values are adjusted to
131a44e4d14SBill Paul  * never be greater than TABLESIZE too, this means it won't be long before
132a44e4d14SBill Paul  * we find ourselves with two keys that hash to the same value.
133a44e4d14SBill Paul  *
134a44e4d14SBill Paul  * One way to deal with this is to malloc(2) a second table and start
135a44e4d14SBill Paul  * doing indirection, but this is a pain in the butt and it's not worth
136d7b71c67SBill Paul  * going to all that trouble for a dinky little program like this. Instead,
137a44e4d14SBill Paul  * we turn each table entry into a linked list and simply link keys
138a44e4d14SBill Paul  * with the same hash value together at the same index location within
139a44e4d14SBill Paul  * the table.
140a44e4d14SBill Paul  *
141a44e4d14SBill Paul  * That's a lot of comment for such a small piece of code, isn't it.
142a44e4d14SBill Paul  */
143266ebcd3SWarner Losh void
store(struct group_entry * table[],char * key,char * data)144266ebcd3SWarner Losh store(struct group_entry *table[], char *key, char *data)
145a44e4d14SBill Paul {
146a44e4d14SBill Paul 	struct group_entry *new;
147a44e4d14SBill Paul 	u_int32_t i;
148a44e4d14SBill Paul 
149a44e4d14SBill Paul 	i = hashkey(key);
150a44e4d14SBill Paul 
151a44e4d14SBill Paul 	new = (struct group_entry *)malloc(sizeof(struct group_entry));
152a44e4d14SBill Paul 	new->key = strdup(key);
153a44e4d14SBill Paul 	new->data = strdup(data);
154a44e4d14SBill Paul 	new->next = table[i];
155a44e4d14SBill Paul 	table[i] = new;
156a44e4d14SBill Paul 
157a44e4d14SBill Paul 	return;
158a44e4d14SBill Paul }
159a44e4d14SBill Paul 
160a44e4d14SBill Paul /*
161d7b71c67SBill Paul  * Store a group member entry and/or update its grouplist. This is
162a44e4d14SBill Paul  * a bit more complicated than the previous function since we have to
163a44e4d14SBill Paul  * maintain not only the hash table of group members, each group member
164a44e4d14SBill Paul  * structure also has a linked list of groups hung off it. If handed
165a44e4d14SBill Paul  * a member name that we haven't encountered before, we have to do
166a44e4d14SBill Paul  * two things: add that member to the table (possibly hanging them
167a44e4d14SBill Paul  * off the end of a linked list, as above), and add a group name to
168a44e4d14SBill Paul  * the member's grouplist list. If we're handed a name that already has
169a44e4d14SBill Paul  * an entry in the table, then we just have to do one thing, which is
170a44e4d14SBill Paul  * to update its grouplist.
171a44e4d14SBill Paul  */
172266ebcd3SWarner Losh void
mstore(struct member_entry * table[],char * key,char * data,char * domain)173266ebcd3SWarner Losh mstore(struct member_entry *table[], char *key, char *data, char *domain)
174a44e4d14SBill Paul {
175a44e4d14SBill Paul 	struct member_entry *cur, *new;
176a44e4d14SBill Paul 	struct grouplist *tmp;
177a44e4d14SBill Paul 	u_int32_t i;
178a44e4d14SBill Paul 
179a44e4d14SBill Paul 	i = hashkey(key);
180a44e4d14SBill Paul 	cur = table[i];
181a44e4d14SBill Paul 
182a44e4d14SBill Paul 	tmp = (struct grouplist *)malloc(sizeof(struct grouplist));
183a44e4d14SBill Paul 	tmp->groupname = strdup(data);
184a44e4d14SBill Paul 	tmp->next = NULL;
185a44e4d14SBill Paul 
186a44e4d14SBill Paul 	/* Check if all we have to do is insert a new groupname. */
187a44e4d14SBill Paul 	while (cur) {
188a44e4d14SBill Paul 		if (!strcmp(cur->key, key)) {
189a44e4d14SBill Paul 			tmp->next = cur->groups;
190a44e4d14SBill Paul 			cur->groups = tmp;
191a44e4d14SBill Paul 			return;
192a44e4d14SBill Paul 		}
193a44e4d14SBill Paul 		cur = cur->next;
194a44e4d14SBill Paul 	}
195a44e4d14SBill Paul 
196a44e4d14SBill Paul 	/* Didn't find a match -- add the whole mess to the table. */
197a44e4d14SBill Paul 	new = (struct member_entry *)malloc(sizeof(struct member_entry));
198a44e4d14SBill Paul 	new->key = strdup(key);
199a44e4d14SBill Paul 	new->domain = domain ? strdup(domain) : "*";
200a44e4d14SBill Paul 	new->groups = tmp;
201a44e4d14SBill Paul 	new->next = table[i];
202a44e4d14SBill Paul 	table[i] = new;
203a44e4d14SBill Paul 
204a44e4d14SBill Paul 	return;
205a44e4d14SBill Paul }
206