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. */
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))
51
#define H_ERRNO_SET(val) ((void) 0)
54
/* Locks the static variables in this file. */
55
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
57
/* Maintenance of the shared handle open on the database. */
64
/* Open the database. */
66
CONCAT(_nss_db_set,ENTNAME) (int stayopen)
68
enum nss_status status;
70
pthread_mutex_lock (&lock);
72
status = internal_setent (DBFILE, &db);
74
/* Remember STAYOPEN flag. */
77
/* Reset the sequential index. */
80
pthread_mutex_unlock (&lock);
88
CONCAT(_nss_db_end,ENTNAME) (void)
90
pthread_mutex_lock (&lock);
92
internal_endent (&db);
94
/* Reset STAYOPEN flag. */
97
pthread_mutex_unlock (&lock);
99
return NSS_STATUS_SUCCESS;
102
/* Do a database lookup for KEY. */
103
static enum nss_status
104
lookup (DBT *key, struct STRUCTURE *result,
105
void *buffer, size_t buflen, int *errnop H_ERRNO_PROTO EXTRA_ARGS_DECL)
108
enum nss_status status;
112
/* Open the database. */
115
status = internal_setent (DBFILE, &db);
116
if (status != NSS_STATUS_SUCCESS)
119
H_ERRNO_SET (NETDB_INTERNAL);
124
/* Succeed iff it matches a value that parses correctly. */
126
err = db->get (db, NULL, key, &value, 0);
132
H_ERRNO_SET (NETDB_INTERNAL);
133
status = NSS_STATUS_UNAVAIL;
140
H_ERRNO_SET (HOST_NOT_FOUND);
141
status = NSS_STATUS_NOTFOUND;
145
H_ERRNO_SET (NO_RECOVERY);
146
status = NSS_STATUS_UNAVAIL;
151
else if (buflen < value.size)
153
/* No room to copy the data to. */
155
H_ERRNO_SET (NETDB_INTERNAL);
156
status = NSS_STATUS_TRYAGAIN;
160
/* Copy the result to a safe place. */
161
p = (char *) memcpy (buffer, value.data, value.size);
163
/* Skip leading blanks. */
167
err = parse_line (p, result, buffer, buflen, errnop EXTRA_ARGS);
171
/* If the key begins with '0' we are trying to get the next
172
entry. We want to ignore unparsable lines in this case. */
173
if (((char *) key->data)[0] == '0')
175
/* Super magical return value. We need to tell our caller
176
that it should continue looping. This value cannot
177
happen in other cases. */
178
status = NSS_STATUS_RETURN;
182
H_ERRNO_SET (HOST_NOT_FOUND);
183
status = NSS_STATUS_NOTFOUND;
188
H_ERRNO_SET (NETDB_INTERNAL);
189
status = NSS_STATUS_TRYAGAIN;
192
status = NSS_STATUS_SUCCESS;
196
internal_endent (&db);
202
/* Macro for defining lookup functions for this DB-based database.
204
NAME is the name of the lookup; e.g. `pwnam'.
206
KEYPATTERN gives `printf' args to construct a key string;
207
e.g. `(".%s", name)'.
209
KEYSIZE gives the allocation size of a buffer to construct it in;
210
e.g. `1 + strlen (name)'.
212
PROTO describes the arguments for the lookup key;
213
e.g. `const char *name'.
215
BREAK_IF_MATCH is ignored, but used by ../nss_files/files-XXX.c. */
217
#define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
219
_nss_db_get##name##_r (proto, \
220
struct STRUCTURE *result, \
221
char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO)\
224
enum nss_status status; \
225
const size_t size = (keysize) + 1; \
226
key.data = alloca (size); \
227
key.size = KEYPRINTF keypattern; \
229
pthread_mutex_lock (&lock); \
230
status = lookup (&key, result, buffer, buflen, errnop H_ERRNO_ARG \
232
pthread_mutex_unlock (&lock); \
236
#define KEYPRINTF(pattern, args...) snprintf (key.data, size, pattern ,##args)
241
/* Return the next entry from the database file, doing locking. */
243
CONCAT(_nss_db_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer,
244
size_t buflen, int *errnop H_ERRNO_PROTO)
246
/* Return next entry in host file. */
247
enum nss_status status;
251
pthread_mutex_lock (&lock);
253
/* Loop until we find a valid entry or hit EOF. See above for the
254
special meaning of the status value. */
257
key.size = snprintf (key.data = buf, sizeof buf, "0%u", entidx++);
259
status = lookup (&key, result, buffer, buflen, errnop H_ERRNO_ARG
261
if (status == NSS_STATUS_TRYAGAIN
263
&& *herrnop == NETDB_INTERNAL
265
&& *errnop == ERANGE)
266
/* Give the user a chance to get the same entry with a larger
270
while (status == NSS_STATUS_RETURN);
272
pthread_mutex_unlock (&lock);