1
/* Common code for DB-based databases in nss_db module.
2
Copyright (C) 1996, 1997, 1998, 1999, 2000 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 Library General Public License as
7
published by the Free Software Foundation; either version 2 of the
8
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
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public
16
License along with the GNU C Library; see the file COPYING.LIB. If not,
17
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
Boston, MA 02111-1307, USA. */
27
#include <bits/libc-lock.h>
31
/* These symbols are defined by the including source file:
33
ENTNAME -- database name of the structure and functions (hostent, pwent).
34
STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
35
DATABASE -- database file name, ("hosts", "passwd")
37
NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
40
#define ENTNAME_r CONCAT(ENTNAME,_r)
43
#define DBFILE _PATH_VARDB DATABASE ".db"
46
#define H_ERRNO_PROTO , int *herrnop
47
#define H_ERRNO_ARG , herrnop
48
#define H_ERRNO_SET(val) (*herrnop = (val))
52
#define H_ERRNO_SET(val) ((void) 0)
55
/* Locks the static variables in this file. */
56
__libc_lock_define_initialized (static, lock)
58
/* Maintenance of the shared handle open on the database. */
65
/* Open the database. */
67
CONCAT(_nss_db_set,ENTNAME) (int stayopen)
69
enum nss_status status;
71
__libc_lock_lock (lock);
73
status = internal_setent (DBFILE, &db);
75
/* Remember STAYOPEN flag. */
78
/* Reset the sequential index. */
81
__libc_lock_unlock (lock);
89
CONCAT(_nss_db_end,ENTNAME) (void)
91
__libc_lock_lock (lock);
93
internal_endent (&db);
95
/* Reset STAYOPEN flag. */
98
__libc_lock_unlock (lock);
100
return NSS_STATUS_SUCCESS;
103
/* Do a database lookup for KEY. */
104
static enum nss_status
105
lookup (DBT *key, struct STRUCTURE *result,
106
void *buffer, size_t buflen, int *errnop H_ERRNO_PROTO EXTRA_ARGS_DECL)
109
enum nss_status status;
113
/* Open the database. */
116
status = internal_setent (DBFILE, &db);
117
if (status != NSS_STATUS_SUCCESS)
120
H_ERRNO_SET (NETDB_INTERNAL);
125
/* Succeed iff it matches a value that parses correctly. */
127
err = db->get (db, NULL, key, &value, 0);
133
H_ERRNO_SET (NETDB_INTERNAL);
134
status = NSS_STATUS_UNAVAIL;
141
H_ERRNO_SET (HOST_NOT_FOUND);
142
status = NSS_STATUS_NOTFOUND;
146
H_ERRNO_SET (NO_RECOVERY);
147
status = NSS_STATUS_UNAVAIL;
152
else if (buflen < value.size)
154
/* No room to copy the data to. */
156
H_ERRNO_SET (NETDB_INTERNAL);
157
status = NSS_STATUS_TRYAGAIN;
161
/* Copy the result to a safe place. */
162
p = (char *) memcpy (buffer, value.data, value.size);
164
/* Skip leading blanks. */
168
err = parse_line (p, result, buffer, buflen, errnop EXTRA_ARGS);
172
/* If the key begins with '0' we are trying to get the next
173
entry. We want to ignore unparsable lines in this case. */
174
if (((char *) key->data)[0] == '0')
176
/* Super magical return value. We need to tell our caller
177
that it should continue looping. This value cannot
178
happen in other cases. */
179
status = NSS_STATUS_RETURN;
183
H_ERRNO_SET (HOST_NOT_FOUND);
184
status = NSS_STATUS_NOTFOUND;
189
H_ERRNO_SET (NETDB_INTERNAL);
190
status = NSS_STATUS_TRYAGAIN;
193
status = NSS_STATUS_SUCCESS;
197
internal_endent (&db);
203
/* Macro for defining lookup functions for this DB-based database.
205
NAME is the name of the lookup; e.g. `pwnam'.
207
KEYPATTERN gives `printf' args to construct a key string;
208
e.g. `(".%s", name)'.
210
KEYSIZE gives the allocation size of a buffer to construct it in;
211
e.g. `1 + strlen (name)'.
213
PROTO describes the arguments for the lookup key;
214
e.g. `const char *name'.
216
BREAK_IF_MATCH is ignored, but used by ../nss_files/files-XXX.c. */
218
#define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
220
_nss_db_get##name##_r (proto, \
221
struct STRUCTURE *result, \
222
char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO)\
225
enum nss_status status; \
226
const size_t size = (keysize) + 1; \
227
key.data = alloca (size); \
228
key.size = KEYPRINTF keypattern; \
230
__libc_lock_lock (lock); \
231
status = lookup (&key, result, buffer, buflen, errnop H_ERRNO_ARG \
233
__libc_lock_unlock (lock); \
237
#define KEYPRINTF(pattern, args...) snprintf (key.data, size, pattern ,##args)
242
/* Return the next entry from the database file, doing locking. */
244
CONCAT(_nss_db_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer,
245
size_t buflen, int *errnop H_ERRNO_PROTO)
247
/* Return next entry in host file. */
248
enum nss_status status;
252
__libc_lock_lock (lock);
254
/* Loop until we find a valid entry or hit EOF. See above for the
255
special meaning of the status value. */
258
key.size = snprintf (key.data = buf, sizeof buf, "0%u", entidx++);
260
status = lookup (&key, result, buffer, buflen, errnop H_ERRNO_ARG
262
if (status == NSS_STATUS_TRYAGAIN
264
&& *herrnop == NETDB_INTERNAL
266
&& *errnop == ERANGE)
267
/* Give the user a chance to get the same entry with a larger
271
while (status == NSS_STATUS_RETURN);
273
__libc_lock_unlock (lock);