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