[BZ #6004]
[glibc.git] / nss / nss_db / db-XXX.c
blobaa8cfd0746db6041f9d7add572fbef98bc1df0d7
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 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <dlfcn.h>
21 #include <fcntl.h>
22 #include <bits/libc-lock.h>
23 #include "nsswitch.h"
24 #include "nss_db.h"
26 /* These symbols are defined by the including source file:
28 ENTNAME -- database name of the structure and functions (hostent, pwent).
29 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
30 DATABASE -- database file name, ("hosts", "passwd")
32 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
35 #define ENTNAME_r CONCAT(ENTNAME,_r)
37 #include <paths.h>
38 #define DBFILE _PATH_VARDB DATABASE ".db"
40 #ifdef NEED_H_ERRNO
41 #define H_ERRNO_PROTO , int *herrnop
42 #define H_ERRNO_ARG , herrnop
43 #define H_ERRNO_SET(val) (*herrnop = (val))
44 #else
45 #define H_ERRNO_PROTO
46 #define H_ERRNO_ARG
47 #define H_ERRNO_SET(val) ((void) 0)
48 #endif
50 /* Locks the static variables in this file. */
51 __libc_lock_define_initialized (static, lock)
53 /* Maintenance of the shared handle open on the database. */
55 static NSS_DB *db;
56 static int keep_db;
57 static int entidx;
60 /* Open the database. */
61 enum nss_status
62 CONCAT(_nss_db_set,ENTNAME) (int stayopen)
64 enum nss_status status;
66 __libc_lock_lock (lock);
68 status = internal_setent (DBFILE, &db);
70 /* Remember STAYOPEN flag. */
71 if (db != NULL)
72 keep_db |= stayopen;
73 /* Reset the sequential index. */
74 entidx = 0;
76 __libc_lock_unlock (lock);
78 return status;
82 /* Close it again. */
83 enum nss_status
84 CONCAT(_nss_db_end,ENTNAME) (void)
86 __libc_lock_lock (lock);
88 internal_endent (&db);
90 /* Reset STAYOPEN flag. */
91 keep_db = 0;
93 __libc_lock_unlock (lock);
95 return NSS_STATUS_SUCCESS;
98 /* Do a database lookup for KEY. */
99 static enum nss_status
100 lookup (DBT *key, struct STRUCTURE *result,
101 void *buffer, size_t buflen, int *errnop H_ERRNO_PROTO EXTRA_ARGS_DECL)
103 char *p;
104 enum nss_status status;
105 int err;
106 DBT value;
108 /* Open the database. */
109 if (db == NULL)
111 status = internal_setent (DBFILE, &db);
112 if (status != NSS_STATUS_SUCCESS)
114 *errnop = errno;
115 H_ERRNO_SET (NETDB_INTERNAL);
116 return status;
120 /* Succeed iff it matches a value that parses correctly. */
121 value.flags = 0;
122 err = DL_CALL_FCT (db->get, (db->db, NULL, key, &value, 0));
123 if (err != 0)
125 if (err == db_notfound)
127 H_ERRNO_SET (HOST_NOT_FOUND);
128 status = NSS_STATUS_NOTFOUND;
130 else
132 *errnop = err;
133 H_ERRNO_SET (NETDB_INTERNAL);
134 status = NSS_STATUS_UNAVAIL;
137 else if (buflen < value.size)
139 /* No room to copy the data to. */
140 *errnop = ERANGE;
141 H_ERRNO_SET (NETDB_INTERNAL);
142 status = NSS_STATUS_TRYAGAIN;
144 else
146 /* Copy the result to a safe place. */
147 p = (char *) memcpy (buffer, value.data, value.size);
149 /* Skip leading blanks. */
150 while (isspace (*p))
151 ++p;
153 err = parse_line (p, result, buffer, buflen, errnop EXTRA_ARGS);
155 if (err == 0)
157 /* If the key begins with '0' we are trying to get the next
158 entry. We want to ignore unparsable lines in this case. */
159 if (((char *) key->data)[0] == '0')
161 /* Super magical return value. We need to tell our caller
162 that it should continue looping. This value cannot
163 happen in other cases. */
164 status = NSS_STATUS_RETURN;
166 else
168 H_ERRNO_SET (HOST_NOT_FOUND);
169 status = NSS_STATUS_NOTFOUND;
172 else if (err < 0)
174 H_ERRNO_SET (NETDB_INTERNAL);
175 status = NSS_STATUS_TRYAGAIN;
177 else
178 status = NSS_STATUS_SUCCESS;
181 if (! keep_db)
182 internal_endent (&db);
184 return status;
188 /* Macro for defining lookup functions for this DB-based database.
190 NAME is the name of the lookup; e.g. `pwnam'.
192 KEYPATTERN gives `printf' args to construct a key string;
193 e.g. `(".%s", name)'.
195 KEYSIZE gives the allocation size of a buffer to construct it in;
196 e.g. `1 + strlen (name)'.
198 PROTO describes the arguments for the lookup key;
199 e.g. `const char *name'.
201 BREAK_IF_MATCH is ignored, but used by ../nss_files/files-XXX.c. */
203 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
204 enum nss_status \
205 _nss_db_get##name##_r (proto, \
206 struct STRUCTURE *result, \
207 char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO)\
209 DBT key; \
210 enum nss_status status; \
211 const size_t size = (keysize) + 1; \
212 key.data = __alloca (size); \
213 key.size = KEYPRINTF keypattern; \
214 key.flags = 0; \
215 __libc_lock_lock (lock); \
216 status = lookup (&key, result, buffer, buflen, errnop H_ERRNO_ARG \
217 EXTRA_ARGS_VALUE); \
218 __libc_lock_unlock (lock); \
219 return status; \
222 #define KEYPRINTF(pattern, args...) snprintf (key.data, size, pattern ,##args)
227 /* Return the next entry from the database file, doing locking. */
228 enum nss_status
229 CONCAT(_nss_db_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer,
230 size_t buflen, int *errnop H_ERRNO_PROTO)
232 /* Return next entry in host file. */
233 enum nss_status status;
234 char buf[20];
235 DBT key;
237 __libc_lock_lock (lock);
239 /* Loop until we find a valid entry or hit EOF. See above for the
240 special meaning of the status value. */
243 key.size = snprintf (key.data = buf, sizeof buf, "0%u", entidx++);
244 key.flags = 0;
245 status = lookup (&key, result, buffer, buflen, errnop H_ERRNO_ARG
246 EXTRA_ARGS_VALUE);
247 if (status == NSS_STATUS_TRYAGAIN
248 #ifdef NEED_H_ERRNO
249 && *herrnop == NETDB_INTERNAL
250 #endif
251 && *errnop == ERANGE)
252 /* Give the user a chance to get the same entry with a larger
253 buffer. */
254 --entidx;
256 while (status == NSS_STATUS_RETURN);
258 __libc_lock_unlock (lock);
260 return status;