Update.
[glibc.git] / nss / nss_db / db-XXX.c
blob1cbe8c291ea273e97d5250ea1d3dbe7583c7e8d7
1 /* Common code for DB-based databases in nss_db module.
2 Copyright (C) 1996, 1997, 1998, 1999 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. */
20 #include <db.h>
21 #include <fcntl.h>
22 #include <bits/libc-lock.h>
23 #include "nsswitch.h"
25 /* These symbols are defined by the including source file:
27 ENTNAME -- database name of the structure and functions (hostent, pwent).
28 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
29 DATABASE -- database file name, ("hosts", "passwd")
31 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
34 #define ENTNAME_r CONCAT(ENTNAME,_r)
36 #include <paths.h>
37 #define DBFILE _PATH_VARDB DATABASE ".db"
39 #ifdef NEED_H_ERRNO
40 #define H_ERRNO_PROTO , int *herrnop
41 #define H_ERRNO_ARG , herrnop
42 #define H_ERRNO_SET(val) (*herrnop = (val))
43 #else
44 #define H_ERRNO_PROTO
45 #define H_ERRNO_ARG
46 #define H_ERRNO_SET(val) ((void) 0)
47 #endif
49 /* Locks the static variables in this file. */
50 __libc_lock_define_initialized (static, lock)
52 /* Maintenance of the shared handle open on the database. */
54 static DB *db;
55 static int keep_db;
56 static unsigned int entidx; /* Index for `getENTNAME'. */
58 /* Open database file if not already opened. */
59 static enum nss_status
60 internal_setent (int stayopen)
62 enum nss_status status = NSS_STATUS_SUCCESS;
63 int err;
65 if (db == NULL)
67 err = __nss_db_open (DBFILE, DB_BTREE, DB_RDONLY, 0, NULL, NULL, &db);
69 if (err != 0)
71 __set_errno (err);
72 status = err == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
74 else
76 /* We have to make sure the file is `closed on exec'. */
77 int fd;
78 int result;
80 err = db->fd (db, &fd);
81 if (err != 0)
83 __set_errno (err);
84 result = -1;
86 else
88 int flags = result = fcntl (fd, F_GETFD, 0);
90 if (result >= 0)
92 flags |= FD_CLOEXEC;
93 result = fcntl (fd, F_SETFD, flags);
96 if (result < 0)
98 /* Something went wrong. Close the stream and return a
99 failure. */
100 db->close (db, 0);
101 db = NULL;
102 status = NSS_STATUS_UNAVAIL;
107 /* Remember STAYOPEN flag. */
108 if (db != NULL)
109 keep_db |= stayopen;
111 return status;
115 /* Thread-safe, exported version of that. */
116 enum nss_status
117 CONCAT(_nss_db_set,ENTNAME) (int stayopen)
119 enum nss_status status;
121 __libc_lock_lock (lock);
123 status = internal_setent (stayopen);
125 /* Reset the sequential index. */
126 entidx = 0;
128 __libc_lock_unlock (lock);
130 return status;
134 /* Close the database file. */
135 static void
136 internal_endent (void)
138 if (db != NULL)
140 db->close (db, 0);
141 db = NULL;
146 /* Thread-safe, exported version of that. */
147 enum nss_status
148 CONCAT(_nss_db_end,ENTNAME) (void)
150 __libc_lock_lock (lock);
152 internal_endent ();
154 /* Reset STAYOPEN flag. */
155 keep_db = 0;
157 __libc_lock_unlock (lock);
159 return NSS_STATUS_SUCCESS;
162 /* Do a database lookup for KEY. */
163 static enum nss_status
164 lookup (DBT *key, struct STRUCTURE *result,
165 void *buffer, size_t buflen, int *errnop H_ERRNO_PROTO EXTRA_ARGS_DECL)
167 char *p;
168 enum nss_status status;
169 int err;
170 DBT value;
172 /* Open the database. */
173 status = internal_setent (keep_db);
174 if (status != NSS_STATUS_SUCCESS)
176 *errnop = errno;
177 H_ERRNO_SET (NETDB_INTERNAL);
178 return status;
181 /* Succeed iff it matches a value that parses correctly. */
182 value.flags = 0;
183 err = db->get (db, NULL, key, &value, 0);
184 if (err != 0)
186 if (err == DB_NOTFOUND)
188 H_ERRNO_SET (HOST_NOT_FOUND);
189 status = NSS_STATUS_NOTFOUND;
191 else
193 *errnop = err;
194 H_ERRNO_SET (NETDB_INTERNAL);
195 status = NSS_STATUS_UNAVAIL;
198 else if (buflen < value.size)
200 /* No room to copy the data to. */
201 *errnop = ERANGE;
202 H_ERRNO_SET (NETDB_INTERNAL);
203 status = NSS_STATUS_TRYAGAIN;
205 else
207 /* Copy the result to a safe place. */
208 p = (char *) memcpy (buffer, value.data, value.size);
210 /* Skip leading blanks. */
211 while (isspace (*p))
212 ++p;
214 err = parse_line (p, result, buffer, buflen, errnop EXTRA_ARGS);
216 if (err == 0)
218 /* If the key begins with '0' we are trying to get the next
219 entry. We want to ignore unparsable lines in this case. */
220 if (((char *) key->data)[0] == '0')
222 /* Super magical return value. We need to tell our caller
223 that it should continue looping. This value cannot
224 happen in other cases. */
225 status = NSS_STATUS_RETURN;
227 else
229 H_ERRNO_SET (HOST_NOT_FOUND);
230 status = NSS_STATUS_NOTFOUND;
233 else if (err < 0)
235 H_ERRNO_SET (NETDB_INTERNAL);
236 status = NSS_STATUS_TRYAGAIN;
238 else
239 status = NSS_STATUS_SUCCESS;
242 if (! keep_db)
243 internal_endent ();
245 return status;
249 /* Macro for defining lookup functions for this DB-based database.
251 NAME is the name of the lookup; e.g. `pwnam'.
253 KEYPATTERN gives `printf' args to construct a key string;
254 e.g. `(".%s", name)'.
256 KEYSIZE gives the allocation size of a buffer to construct it in;
257 e.g. `1 + strlen (name)'.
259 PROTO describes the arguments for the lookup key;
260 e.g. `const char *name'.
262 BREAK_IF_MATCH is ignored, but used by ../nss_files/files-XXX.c. */
264 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
265 enum nss_status \
266 _nss_db_get##name##_r (proto, \
267 struct STRUCTURE *result, \
268 char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO)\
270 DBT key; \
271 enum nss_status status; \
272 const size_t size = (keysize) + 1; \
273 key.data = __alloca (size); \
274 key.size = KEYPRINTF keypattern; \
275 key.flags = 0; \
276 __libc_lock_lock (lock); \
277 status = lookup (&key, result, buffer, buflen, errnop H_ERRNO_ARG \
278 EXTRA_ARGS_VALUE); \
279 __libc_lock_unlock (lock); \
280 return status; \
283 #define KEYPRINTF(pattern, args...) snprintf (key.data, size, pattern ,##args)
288 /* Return the next entry from the database file, doing locking. */
289 enum nss_status
290 CONCAT(_nss_db_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer,
291 size_t buflen, int *errnop H_ERRNO_PROTO)
293 /* Return next entry in host file. */
294 enum nss_status status;
295 char buf[20];
296 DBT key;
298 __libc_lock_lock (lock);
300 /* Loop until we find a valid entry or hit EOF. See above for the
301 special meaning of the status value. */
304 key.size = snprintf (key.data = buf, sizeof buf, "0%u", entidx++);
305 key.flags = 0;
306 status = lookup (&key, result, buffer, buflen, errnop H_ERRNO_ARG
307 EXTRA_ARGS_VALUE);
308 if (status == NSS_STATUS_TRYAGAIN
309 #ifdef NEED_H_ERRNO
310 && *herrnop == NETDB_INTERNAL
311 #endif
312 && *errnop == ERANGE)
313 /* Give the user a chance to get the same entry with a larger
314 buffer. */
315 --entidx;
317 while (status == NSS_STATUS_RETURN);
319 __libc_lock_unlock (lock);
321 return status;