1 /* $OpenBSD: hash.c,v 1.4 2003/07/15 06:10:46 deraadt Exp $ */ 2 /* 3 * Copyright (c) 1995 4 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Bill Paul. 17 * 4. Neither the name of the author nor the names of any co-contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * $FreeBSD: hash.c,v 1.4 1997/02/22 14:22:01 peter Exp $ 34 */ 35 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <sys/types.h> 40 #include "hash.h" 41 42 #ifndef lint 43 static const char rcsid[] = "$OpenBSD: hash.c,v 1.4 2003/07/15 06:10:46 deraadt Exp $"; 44 #endif 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 static 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 static 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 163 /* 164 * Store a group member entry and/or update its grouplist. This is 165 * a bit more complicated than the previous function since we have to 166 * maintain not only the hash table of group members, each group member 167 * structure also has a linked list of groups hung off it. If handed 168 * a member name that we haven't encountered before, we have to do 169 * two things: add that member to the table (possibly hanging them 170 * off the end of a linked list, as above), and add a group name to 171 * the member's grouplist list. If we're handed a name that already has 172 * an entry in the table, then we just have to do one thing, which is 173 * to update its grouplist. 174 */ 175 void 176 mstore(struct member_entry *table[], char *key, char *data, char *domain) 177 { 178 struct member_entry *cur, *new; 179 struct grouplist *tmp,*p; 180 u_int32_t i; 181 182 i = hashkey(key); 183 cur = table[i]; 184 185 tmp = (struct grouplist *)malloc(sizeof(struct grouplist)); 186 tmp->groupname = strdup(data); 187 tmp->next = NULL; 188 189 /* Check if all we have to do is insert a new groupname. */ 190 while (cur) { 191 if (!strcmp(cur->key, key) && !strcmp(cur->domain,domain)) { 192 p = cur->groups; 193 while (p) { 194 if (!strcmp(p->groupname,data)) 195 return; 196 p = p->next; 197 } 198 tmp->next = cur->groups; 199 cur->groups = tmp; 200 return; 201 } 202 cur = cur->next; 203 } 204 205 /* Didn't find a match -- add the whole mess to the table. */ 206 new = (struct member_entry *)malloc(sizeof(struct member_entry)); 207 new->key = strdup(key); 208 new->domain = strdup(domain); 209 new->groups = tmp; 210 new->next = table[i]; 211 table[i] = new; 212 } 213