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