1 /* Common code for DB-based databases in nss_db module.
2 Copyright (C) 1996-2017 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/>. */
23 #include <libc-lock.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)
42 #define DBFILE _PATH_VARDB DATABASE ".db"
45 # define H_ERRNO_PROTO , int *herrnop
46 # define H_ERRNO_ARG , herrnop
47 # define H_ERRNO_SET(val) (*herrnop = (val))
49 # define H_ERRNO_PROTO
51 # define H_ERRNO_SET(val) ((void) 0)
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. */
61 static const char *entidx
;
64 /* Open the database. */
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. */
79 /* Reset the sequential index. */
83 __libc_lock_unlock (lock
);
91 CONCAT(_nss_db_end
,ENTNAME
) (void)
93 __libc_lock_lock (lock
);
95 internal_endent (&state
);
97 /* Reset STAYOPEN flag. */
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;
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...)\
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) \
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) \
142 H_ERRNO_SET (NETDB_INTERNAL); \
146 const struct nss_db_header *header = state.header; \
148 for (i = 0; i < header->ndbs; ++i) \
149 if (header->dbs[i].id == db_char) \
151 if (i == header->ndbs) \
153 status = NSS_STATUS_UNAVAIL; \
158 if (db_char == '.') \
159 key = (char *) IGNOREPATTERN keypattern; \
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; \
183 /* No room to copy the data to. */ \
185 H_ERRNO_SET (NETDB_INTERNAL); \
186 status = NSS_STATUS_TRYAGAIN; \
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; \
202 status = NSS_STATUS_SUCCESS; \
204 status = NSS_STATUS_NOTFOUND; \
206 else if (err == -1) \
208 H_ERRNO_SET (NETDB_INTERNAL); \
209 status = NSS_STATUS_TRYAGAIN; \
214 if (status == NSS_STATUS_NOTFOUND) \
215 H_ERRNO_SET (HOST_NOT_FOUND); \
218 internal_endent (&state); \
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. */
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
)
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
)
253 H_ERRNO_SET (NETDB_INTERNAL
);
259 /* Start from the beginning if freshly initialized or reset
260 requested by set*ent. */
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
);
272 const char *next
= rawmemchr (entidx
, '\0') + 1;
273 size_t len
= next
- entidx
;
277 /* No room to copy the data to. */
279 H_ERRNO_SET (NETDB_INTERNAL
);
280 status
= NSS_STATUS_TRYAGAIN
;
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
);
291 status
= NSS_STATUS_SUCCESS
;
297 H_ERRNO_SET (NETDB_INTERNAL
);
298 status
= NSS_STATUS_TRYAGAIN
;
302 /* Continue with the next record, this one is ill-formed. */
308 __libc_lock_unlock (lock
);