Update copyright notices with scripts/update-copyrights.
[glibc.git] / nss / nss_db / db-XXX.c
blob9399ce205a926f2055469962883c26e97ea41bf5
1 /* Common code for DB-based databases in nss_db module.
2 Copyright (C) 1996-2013 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 <sys/mman.h>
22 #include <bits/libc-lock.h>
23 #include "nsswitch.h"
24 #include "nss_db.h"
26 /* The hashing function we use. */
27 #include "../intl/hash-string.h"
29 /* These symbols are defined by the including source file:
31 ENTNAME -- database name of the structure and functions (hostent, pwent).
32 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
33 DATABASE -- database file name, ("hosts", "passwd")
35 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
38 #define ENTNAME_r CONCAT(ENTNAME,_r)
40 #include <paths.h>
41 #define DBFILE _PATH_VARDB DATABASE ".db"
43 #ifdef NEED_H_ERRNO
44 # define H_ERRNO_PROTO , int *herrnop
45 # define H_ERRNO_ARG , herrnop
46 # define H_ERRNO_SET(val) (*herrnop = (val))
47 #else
48 # define H_ERRNO_PROTO
49 # define H_ERRNO_ARG
50 # define H_ERRNO_SET(val) ((void) 0)
51 #endif
53 /* State for this database. */
54 static struct nss_db_map state;
55 /* Lock to protect the state and global variables. */
56 __libc_lock_define (static , lock);
58 /* Maintenance of the shared handle open on the database. */
59 static int keep_db;
60 static const char *entidx;
63 /* Open the database. */
64 enum nss_status
65 CONCAT(_nss_db_set,ENTNAME) (int stayopen)
67 enum nss_status status;
69 __libc_lock_lock (lock);
71 status = internal_setent (DBFILE, &state);
73 if (status == NSS_STATUS_SUCCESS)
75 /* Remember STAYOPEN flag. */
76 keep_db |= stayopen;
78 /* Reset the sequential index. */
79 entidx = (const char *) state.header + state.header->valstroffset;
82 __libc_lock_unlock (lock);
84 return status;
88 /* Close it again. */
89 enum nss_status
90 CONCAT(_nss_db_end,ENTNAME) (void)
92 __libc_lock_lock (lock);
94 internal_endent (&state);
96 /* Reset STAYOPEN flag. */
97 keep_db = 0;
99 __libc_lock_unlock (lock);
101 return NSS_STATUS_SUCCESS;
105 /* Macro for defining lookup functions for this DB-based database.
107 NAME is the name of the lookup; e.g. `pwnam'.
109 DB_CHAR is index indicator for the database.
111 KEYPATTERN gives `printf' args to construct a key string;
112 e.g. `("%d", id)'.
114 KEYSIZE gives the allocation size of a buffer to construct it in;
115 e.g. `1 + sizeof (id) * 4'.
117 PROTO is the potentially empty list of other parameters.
119 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
120 to the lookup key arguments and does `break;' if they match. */
122 #define DB_LOOKUP(name, db_char, keysize, keypattern, break_if_match, proto...)\
123 enum nss_status \
124 _nss_db_get##name##_r (proto, struct STRUCTURE *result, \
125 char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO)\
127 struct parser_data *data = (void *) buffer; \
129 if (buflen < sizeof *data) \
131 *errnop = ERANGE; \
132 H_ERRNO_SET (NETDB_INTERNAL); \
133 return NSS_STATUS_TRYAGAIN; \
136 struct nss_db_map state = { NULL, 0 }; \
137 enum nss_status status = internal_setent (DBFILE, &state); \
138 if (status != NSS_STATUS_SUCCESS) \
140 *errnop = errno; \
141 H_ERRNO_SET (NETDB_INTERNAL); \
142 return status; \
145 const struct nss_db_header *header = state.header; \
146 int i; \
147 for (i = 0; i < header->ndbs; ++i) \
148 if (header->dbs[i].id == db_char) \
149 break; \
150 if (i == header->ndbs) \
152 status = NSS_STATUS_UNAVAIL; \
153 goto out; \
156 char *key; \
157 if (db_char == '.') \
158 key = (char *) IGNOREPATTERN keypattern; \
159 else \
161 const size_t size = (keysize) + 1; \
162 key = alloca (size); \
164 KEYPRINTF keypattern; \
167 const stridx_t *hashtable \
168 = (const stridx_t *) ((const char *) header \
169 + header->dbs[i].hashoffset); \
170 const char *valstrtab = (const char *) header + header->valstroffset; \
171 uint32_t hashval = __hash_string (key); \
172 size_t hidx = hashval % header->dbs[i].hashsize; \
173 size_t hval2 = 1 + hashval % (header->dbs[i].hashsize - 2); \
175 status = NSS_STATUS_NOTFOUND; \
176 while (hashtable[hidx] != ~((stridx_t) 0)) \
178 const char *valstr = valstrtab + hashtable[hidx]; \
179 size_t len = strlen (valstr) + 1; \
180 if (len > buflen) \
182 /* No room to copy the data to. */ \
183 *errnop = ERANGE; \
184 H_ERRNO_SET (NETDB_INTERNAL); \
185 status = NSS_STATUS_TRYAGAIN; \
186 break; \
189 /* Copy the string to a place where it can be modified. */ \
190 char *p = memcpy (buffer, valstr, len); \
192 int err = parse_line (p, result, data, buflen, errnop EXTRA_ARGS); \
193 if (err > 0) \
195 status = NSS_STATUS_SUCCESS; \
196 break_if_match; \
197 status = NSS_STATUS_NOTFOUND; \
199 else if (err == -1) \
201 H_ERRNO_SET (NETDB_INTERNAL); \
202 status = NSS_STATUS_TRYAGAIN; \
203 break; \
206 if ((hidx += hval2) >= header->dbs[i].hashsize) \
207 hidx -= header->dbs[i].hashsize; \
210 if (status == NSS_STATUS_NOTFOUND) \
211 H_ERRNO_SET (HOST_NOT_FOUND); \
213 out: \
214 internal_endent (&state); \
216 return status; \
219 #define KEYPRINTF(pattern, args...) snprintf (key, size, pattern ,##args)
220 #define IGNOREPATTERN(pattern, arg1, args...) (char *) (uintptr_t) arg1
225 /* Return the next entry from the database file, doing locking. */
226 enum nss_status
227 CONCAT(_nss_db_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer,
228 size_t buflen, int *errnop H_ERRNO_PROTO)
230 /* Return next entry in host file. */
231 enum nss_status status;
232 struct parser_data *data = (void *) buffer;
234 if (buflen < sizeof *data)
236 *errnop = ERANGE;
237 H_ERRNO_SET (NETDB_INTERNAL);
238 return NSS_STATUS_TRYAGAIN;
241 __libc_lock_lock (lock);
243 if (state.header == NULL)
245 status = internal_setent (DBFILE, &state);
246 if (status != NSS_STATUS_SUCCESS)
248 *errnop = errno;
249 H_ERRNO_SET (NETDB_INTERNAL);
250 goto out;
254 status = NSS_STATUS_UNAVAIL;
255 if (state.header != MAP_FAILED)
257 const char *const end = ((const char *) state.header
258 + state.header->valstroffset
259 + state.header->valstrlen);
260 while (entidx < end)
262 const char *next = rawmemchr (entidx, '\0') + 1;
263 size_t len = next - entidx;
265 if (len > buflen)
267 /* No room to copy the data to. */
268 *errnop = ERANGE;
269 H_ERRNO_SET (NETDB_INTERNAL);
270 status = NSS_STATUS_TRYAGAIN;
271 break;
274 /* Copy the string to a place where it can be modified. */
275 char *p = memcpy (buffer, entidx, len);
277 int err = parse_line (p, result, data, buflen, errnop EXTRA_ARGS);
279 if (err > 0)
281 status = NSS_STATUS_SUCCESS;
282 entidx = next;
283 break;
285 if (err < 0)
287 H_ERRNO_SET (HOST_NOT_FOUND);
288 status = NSS_STATUS_NOTFOUND;
289 break;
292 /* Continue with the next record, this one is ill-formed. */
293 entidx = next;
297 out:
298 __libc_lock_unlock (lock);
300 return status;