/*-1* SPDX-License-Identifier: BSD-4-Clause2*3* Copyright (c) 19954* Bill Paul <[email protected]>. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. All advertising materials mentioning features or use of this software15* 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-contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include <stdio.h>35#include <stdlib.h>36#include <string.h>37#include <sys/types.h>38#include "hash.h"3940/*41* This hash function is stolen directly from the42* Berkeley DB package. It already exists inside libc, but43* it's declared static which prevents us from calling it44* from here.45*/46/*47* OZ's original sdbm hash48*/49u_int32_t50hash(const void *keyarg, size_t len)51{52const u_char *key;53size_t loop;54u_int32_t h;5556#define HASHC h = *key++ + 65599 * h5758h = 0;59key = keyarg;60if (len > 0) {61loop = (len + 8 - 1) >> 3;6263switch (len & (8 - 1)) {64case 0:65do {66HASHC;67/* FALLTHROUGH */68case 7:69HASHC;70/* FALLTHROUGH */71case 6:72HASHC;73/* FALLTHROUGH */74case 5:75HASHC;76/* FALLTHROUGH */77case 4:78HASHC;79/* FALLTHROUGH */80case 3:81HASHC;82/* FALLTHROUGH */83case 2:84HASHC;85/* FALLTHROUGH */86case 1:87HASHC;88} while (--loop);89}90}91return (h);92}9394/*95* Generate a hash value for a given key (character string).96* We mask off all but the lower 8 bits since our table array97* can only hole 256 elements.98*/99u_int32_t hashkey(char *key)100{101102if (key == NULL)103return (-1);104return(hash((void *)key, strlen(key)) & HASH_MASK);105}106107/* Find an entry in the hash table (may be hanging off a linked list). */108struct grouplist *lookup(struct member_entry *table[], char *key)109{110struct member_entry *cur;111112cur = table[hashkey(key)];113114while (cur) {115if (!strcmp(cur->key, key))116return(cur->groups);117cur = cur->next;118}119120return(NULL);121}122123struct grouplist dummy = { 99999, NULL };124125/*126* Store a group member entry and/or update its grouplist.127*/128void mstore (struct member_entry *table[], char *key, int gid, int dup)129{130struct member_entry *cur, *new;131struct grouplist *tmp;132u_int32_t i;133134i = hashkey(key);135cur = table[i];136137if (!dup) {138tmp = (struct grouplist *)malloc(sizeof(struct grouplist));139tmp->groupid = gid;140tmp->next = NULL;141}142143/* Check if all we have to do is insert a new groupname. */144while (cur) {145if (!dup && !strcmp(cur->key, key)) {146tmp->next = cur->groups;147cur->groups = tmp;148return;149}150cur = cur->next;151}152153/* Didn't find a match -- add the whole mess to the table. */154new = (struct member_entry *)malloc(sizeof(struct member_entry));155new->key = strdup(key);156if (!dup)157new->groups = tmp;158else159new->groups = (struct grouplist *)&dummy;160new->next = table[i];161table[i] = new;162163return;164}165166167