iconvdata/tst-table-charmap.sh: remove handling of old, borrowed format
[glibc.git] / nss / nss_db / db-XXX.c
blob224737a0645e0450774bcf1062b274ea136a99ee
1 /* Common code for DB-based databases in nss_db module.
2 Copyright (C) 1996-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <dlfcn.h>
20 #include <fcntl.h>
21 #include <stdint.h>
22 #include <sys/mman.h>
23 #include <libc-lock.h>
24 #include "nsswitch.h"
25 #include "nss_db.h"
27 /* The hashing function we use. */
28 #include "../intl/hash-string.h"
30 /* These symbols are defined by the including source file:
32 ENTNAME -- database name of the structure and functions (hostent, pwent).
33 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
34 DATABASE -- database file name, ("hosts", "passwd")
36 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
39 #define ENTNAME_r CONCAT(ENTNAME,_r)
41 #include <paths.h>
42 #define DBFILE _PATH_VARDB DATABASE ".db"
44 #ifdef NEED_H_ERRNO
45 # define H_ERRNO_PROTO , int *herrnop
46 # define H_ERRNO_ARG , herrnop
47 # define H_ERRNO_SET(val) (*herrnop = (val))
48 #else
49 # define H_ERRNO_PROTO
50 # define H_ERRNO_ARG
51 # define H_ERRNO_SET(val) ((void) 0)
52 #endif
54 /* State for this database. */
55 static struct nss_db_map state;
56 /* Lock to protect the state and global variables. */
57 __libc_lock_define (static , lock);
59 /* Maintenance of the shared handle open on the database. */
60 static int keep_db;
61 static const char *entidx;
64 /* Open the database. */
65 enum nss_status
66 CONCAT(_nss_db_set,ENTNAME) (int stayopen)
68 enum nss_status status;
70 __libc_lock_lock (lock);
72 status = internal_setent (DBFILE, &state);
74 if (status == NSS_STATUS_SUCCESS)
76 /* Remember STAYOPEN flag. */
77 keep_db |= stayopen;
79 /* Reset the sequential index. */
80 entidx = NULL;
83 __libc_lock_unlock (lock);
85 return status;
89 /* Close it again. */
90 enum nss_status
91 CONCAT(_nss_db_end,ENTNAME) (void)
93 __libc_lock_lock (lock);
95 internal_endent (&state);
97 /* Reset STAYOPEN flag. */
98 keep_db = 0;
100 __libc_lock_unlock (lock);
102 return NSS_STATUS_SUCCESS;
106 /* Macro for defining lookup functions for this DB-based database.
108 NAME is the name of the lookup; e.g. `pwnam'.
110 DB_CHAR is index indicator for the database.
112 KEYPATTERN gives `printf' args to construct a key string;
113 e.g. `("%d", id)'.
115 KEYSIZE gives the allocation size of a buffer to construct it in;
116 e.g. `1 + sizeof (id) * 4'.
118 PROTO is the potentially empty list of other parameters.
120 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
121 to the lookup key arguments and does `break;' if they match. */
123 #define DB_LOOKUP(name, db_char, keysize, keypattern, break_if_match, proto...)\
124 enum nss_status \
125 _nss_db_get##name##_r (proto, struct STRUCTURE *result, \
126 char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO)\
128 struct parser_data *data = (void *) buffer; \
130 if (buflen < sizeof *data) \
132 *errnop = ERANGE; \
133 H_ERRNO_SET (NETDB_INTERNAL); \
134 return NSS_STATUS_TRYAGAIN; \
137 struct nss_db_map state = { NULL, 0 }; \
138 enum nss_status status = internal_setent (DBFILE, &state); \
139 if (status != NSS_STATUS_SUCCESS) \
141 *errnop = errno; \
142 H_ERRNO_SET (NETDB_INTERNAL); \
143 return status; \
146 const struct nss_db_header *header = state.header; \
147 int i; \
148 for (i = 0; i < header->ndbs; ++i) \
149 if (header->dbs[i].id == db_char) \
150 break; \
151 if (i == header->ndbs) \
153 status = NSS_STATUS_UNAVAIL; \
154 goto out; \
157 char *key; \
158 if (db_char == '.') \
159 key = (char *) IGNOREPATTERN keypattern; \
160 else \
162 const size_t size = (keysize) + 1; \
163 key = alloca (size); \
165 KEYPRINTF keypattern; \
168 const stridx_t *hashtable \
169 = (const stridx_t *) ((const char *) header \
170 + header->dbs[i].hashoffset); \
171 const char *valstrtab = (const char *) header + header->valstroffset; \
172 uint32_t hashval = __hash_string (key); \
173 size_t hidx = hashval % header->dbs[i].hashsize; \
174 size_t hval2 = 1 + hashval % (header->dbs[i].hashsize - 2); \
176 status = NSS_STATUS_NOTFOUND; \
177 while (hashtable[hidx] != ~((stridx_t) 0)) \
179 const char *valstr = valstrtab + hashtable[hidx]; \
180 size_t len = strlen (valstr) + 1; \
181 if (len > buflen) \
183 /* No room to copy the data to. */ \
184 *errnop = ERANGE; \
185 H_ERRNO_SET (NETDB_INTERNAL); \
186 status = NSS_STATUS_TRYAGAIN; \
187 break; \
190 /* Copy the string to a place where it can be modified. */ \
191 char *p = memcpy (buffer, valstr, len); \
193 int err = parse_line (p, result, data, buflen, errnop EXTRA_ARGS); \
195 /* Advance before break_if_match, lest it uses continue to skip
196 to the next entry. */ \
197 if ((hidx += hval2) >= header->dbs[i].hashsize) \
198 hidx -= header->dbs[i].hashsize; \
200 if (err > 0) \
202 status = NSS_STATUS_SUCCESS; \
203 break_if_match; \
204 status = NSS_STATUS_NOTFOUND; \
206 else if (err == -1) \
208 H_ERRNO_SET (NETDB_INTERNAL); \
209 status = NSS_STATUS_TRYAGAIN; \
210 break; \
214 if (status == NSS_STATUS_NOTFOUND) \
215 H_ERRNO_SET (HOST_NOT_FOUND); \
217 out: \
218 internal_endent (&state); \
220 return status; \
223 #define KEYPRINTF(pattern, args...) snprintf (key, size, pattern ,##args)
224 #define IGNOREPATTERN(pattern, arg1, args...) (char *) (uintptr_t) arg1
229 /* Return the next entry from the database file, doing locking. */
230 enum nss_status
231 CONCAT(_nss_db_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer,
232 size_t buflen, int *errnop H_ERRNO_PROTO)
234 /* Return next entry in host file. */
235 enum nss_status status;
236 struct parser_data *data = (void *) buffer;
238 if (buflen < sizeof *data)
240 *errnop = ERANGE;
241 H_ERRNO_SET (NETDB_INTERNAL);
242 return NSS_STATUS_TRYAGAIN;
245 __libc_lock_lock (lock);
247 if (state.header == NULL)
249 status = internal_setent (DBFILE, &state);
250 if (status != NSS_STATUS_SUCCESS)
252 *errnop = errno;
253 H_ERRNO_SET (NETDB_INTERNAL);
254 goto out;
256 entidx = NULL;
259 /* Start from the beginning if freshly initialized or reset
260 requested by set*ent. */
261 if (entidx == NULL)
262 entidx = (const char *) state.header + state.header->valstroffset;
264 status = NSS_STATUS_UNAVAIL;
265 if (state.header != MAP_FAILED)
267 const char *const end = ((const char *) state.header
268 + state.header->valstroffset
269 + state.header->valstrlen);
270 while (entidx < end)
272 const char *next = rawmemchr (entidx, '\0') + 1;
273 size_t len = next - entidx;
275 if (len > buflen)
277 /* No room to copy the data to. */
278 *errnop = ERANGE;
279 H_ERRNO_SET (NETDB_INTERNAL);
280 status = NSS_STATUS_TRYAGAIN;
281 break;
284 /* Copy the string to a place where it can be modified. */
285 char *p = memcpy (buffer, entidx, len);
287 int err = parse_line (p, result, data, buflen, errnop EXTRA_ARGS);
289 if (err > 0)
291 status = NSS_STATUS_SUCCESS;
292 entidx = next;
293 break;
295 if (err < 0)
297 H_ERRNO_SET (NETDB_INTERNAL);
298 status = NSS_STATUS_TRYAGAIN;
299 break;
302 /* Continue with the next record, this one is ill-formed. */
303 entidx = next;
307 out:
308 __libc_lock_unlock (lock);
310 return status;