xf.li | bfc6e71 | 2025-02-07 01:54:34 -0800 | [diff] [blame^] | 1 | /* Netgroup file parser in nss_db modules. |
| 2 | Copyright (C) 1996-2016 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <ctype.h> |
| 21 | #include <dlfcn.h> |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <netgroup.h> |
| 25 | #include <string.h> |
| 26 | #include <stdint.h> |
| 27 | #include <libc-lock.h> |
| 28 | #include <paths.h> |
| 29 | #include <stdlib.h> |
| 30 | |
| 31 | #include "nsswitch.h" |
| 32 | #include "nss_db.h" |
| 33 | |
| 34 | /* The hashing function we use. */ |
| 35 | #include "../intl/hash-string.h" |
| 36 | |
| 37 | |
| 38 | #define DBFILE _PATH_VARDB "netgroup.db" |
| 39 | |
| 40 | /* Maintenance of the shared handle open on the database. */ |
| 41 | enum nss_status |
| 42 | _nss_db_setnetgrent (const char *group, struct __netgrent *result) |
| 43 | { |
| 44 | struct nss_db_map state; |
| 45 | enum nss_status status = internal_setent (DBFILE, &state); |
| 46 | |
| 47 | if (status == NSS_STATUS_SUCCESS) |
| 48 | { |
| 49 | const struct nss_db_header *header = state.header; |
| 50 | const stridx_t *hashtable |
| 51 | = (const stridx_t *) ((const char *) header |
| 52 | + header->dbs[0].hashoffset); |
| 53 | const char *valstrtab = (const char *) header + header->valstroffset; |
| 54 | uint32_t hashval = __hash_string (group); |
| 55 | size_t grouplen = strlen (group); |
| 56 | size_t hidx = hashval % header->dbs[0].hashsize; |
| 57 | size_t hval2 = 1 + hashval % (header->dbs[0].hashsize - 2); |
| 58 | |
| 59 | status = NSS_STATUS_NOTFOUND; |
| 60 | while (hashtable[hidx] != ~((stridx_t) 0)) |
| 61 | { |
| 62 | const char *valstr = valstrtab + hashtable[hidx]; |
| 63 | |
| 64 | if (strncmp (valstr, group, grouplen) == 0 |
| 65 | && isblank (valstr[grouplen])) |
| 66 | { |
| 67 | const char *cp = &valstr[grouplen + 1]; |
| 68 | while (isblank (*cp)) |
| 69 | ++cp; |
| 70 | if (*cp != '\0') |
| 71 | { |
| 72 | result->data = strdup (cp); |
| 73 | if (result->data == NULL) |
| 74 | status = NSS_STATUS_TRYAGAIN; |
| 75 | else |
| 76 | { |
| 77 | status = NSS_STATUS_SUCCESS; |
| 78 | result->cursor = result->data; |
| 79 | } |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if ((hidx += hval2) >= header->dbs[0].hashsize) |
| 85 | hidx -= header->dbs[0].hashsize; |
| 86 | } |
| 87 | |
| 88 | internal_endent (&state); |
| 89 | } |
| 90 | |
| 91 | return status; |
| 92 | |
| 93 | } |
| 94 | |
| 95 | |
| 96 | enum nss_status |
| 97 | _nss_db_endnetgrent (struct __netgrent *result) |
| 98 | { |
| 99 | free (result->data); |
| 100 | result->data = NULL; |
| 101 | result->data_size = 0; |
| 102 | result->cursor = NULL; |
| 103 | return NSS_STATUS_SUCCESS; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | extern enum nss_status _nss_netgroup_parseline (char **cursor, |
| 108 | struct __netgrent *result, |
| 109 | char *buffer, size_t buflen, |
| 110 | int *errnop); |
| 111 | |
| 112 | enum nss_status |
| 113 | _nss_db_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen, |
| 114 | int *errnop) |
| 115 | { |
| 116 | enum nss_status status; |
| 117 | |
| 118 | status = _nss_netgroup_parseline (&result->cursor, result, buffer, buflen, |
| 119 | errnop); |
| 120 | |
| 121 | return status; |
| 122 | } |