Update copyright notices with scripts/update-copyrights
[glibc.git] / nss / nss_db / db-XXX.c
blob89b1a126c22ea5f4dc3bc4d0680c2bc5dfd7271e
1 /* Common code for DB-based databases in nss_db module.
2 Copyright (C) 1996-2014 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 <http://www.gnu.org/licenses/>. */
19 #include <dlfcn.h>
20 #include <fcntl.h>
21 #include <stdint.h>
22 #include <sys/mman.h>
23 #include <bits/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 = (const char *) state.header + state.header->valstroffset;
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); \
194 if (err > 0) \
196 status = NSS_STATUS_SUCCESS; \
197 break_if_match; \
198 status = NSS_STATUS_NOTFOUND; \
200 else if (err == -1) \
202 H_ERRNO_SET (NETDB_INTERNAL); \
203 status = NSS_STATUS_TRYAGAIN; \
204 break; \
207 if ((hidx += hval2) >= header->dbs[i].hashsize) \
208 hidx -= header->dbs[i].hashsize; \
211 if (status == NSS_STATUS_NOTFOUND) \
212 H_ERRNO_SET (HOST_NOT_FOUND); \
214 out: \
215 internal_endent (&state); \
217 return status; \
220 #define KEYPRINTF(pattern, args...) snprintf (key, size, pattern ,##args)
221 #define IGNOREPATTERN(pattern, arg1, args...) (char *) (uintptr_t) arg1
226 /* Return the next entry from the database file, doing locking. */
227 enum nss_status
228 CONCAT(_nss_db_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer,
229 size_t buflen, int *errnop H_ERRNO_PROTO)
231 /* Return next entry in host file. */
232 enum nss_status status;
233 struct parser_data *data = (void *) buffer;
235 if (buflen < sizeof *data)
237 *errnop = ERANGE;
238 H_ERRNO_SET (NETDB_INTERNAL);
239 return NSS_STATUS_TRYAGAIN;
242 __libc_lock_lock (lock);
244 if (state.header == NULL)
246 status = internal_setent (DBFILE, &state);
247 if (status != NSS_STATUS_SUCCESS)
249 *errnop = errno;
250 H_ERRNO_SET (NETDB_INTERNAL);
251 goto out;
255 status = NSS_STATUS_UNAVAIL;
256 if (state.header != MAP_FAILED)
258 const char *const end = ((const char *) state.header
259 + state.header->valstroffset
260 + state.header->valstrlen);
261 while (entidx < end)
263 const char *next = rawmemchr (entidx, '\0') + 1;
264 size_t len = next - entidx;
266 if (len > buflen)
268 /* No room to copy the data to. */
269 *errnop = ERANGE;
270 H_ERRNO_SET (NETDB_INTERNAL);
271 status = NSS_STATUS_TRYAGAIN;
272 break;
275 /* Copy the string to a place where it can be modified. */
276 char *p = memcpy (buffer, entidx, len);
278 int err = parse_line (p, result, data, buflen, errnop EXTRA_ARGS);
280 if (err > 0)
282 status = NSS_STATUS_SUCCESS;
283 entidx = next;
284 break;
286 if (err < 0)
288 H_ERRNO_SET (HOST_NOT_FOUND);
289 status = NSS_STATUS_NOTFOUND;
290 break;
293 /* Continue with the next record, this one is ill-formed. */
294 entidx = next;
298 out:
299 __libc_lock_unlock (lock);
301 return status;