Updated to fedora-glibc-20070814T0725
[glibc.git] / nss / nss_files / files-XXX.c
blob2149d1c401e7a1e58d784f7ec6c2f24c828d7da4
1 /* Common code for file-based databases in nss_files module.
2 Copyright (C) 1996-1999,2001,2002,2004,2007 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 <stdio.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <bits/libc-lock.h>
25 #include "nsswitch.h"
27 #include <kernel-features.h>
29 /* These symbols are defined by the including source file:
31 ENTNAME -- database name of the structure and functions (hostent, pwent).
32 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
33 DATABASE -- string of the database file's name ("hosts", "passwd").
35 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
37 Also see files-parse.c.
40 #define ENTNAME_r CONCAT(ENTNAME,_r)
42 #define DATAFILE "/etc/" DATABASE
44 #ifdef NEED_H_ERRNO
45 # include <netdb.h>
46 # define H_ERRNO_PROTO , int *herrnop
47 # define H_ERRNO_ARG , herrnop
48 # define H_ERRNO_SET(val) (*herrnop = (val))
49 #else
50 # define H_ERRNO_PROTO
51 # define H_ERRNO_ARG
52 # define H_ERRNO_SET(val) ((void) 0)
53 #endif
55 #ifndef EXTRA_ARGS
56 # define EXTRA_ARGS
57 # define EXTRA_ARGS_DECL
58 # define EXTRA_ARGS_VALUE
59 #endif
61 /* Locks the static variables in this file. */
62 __libc_lock_define_initialized (static, lock)
64 /* Maintenance of the shared stream open on the database file. */
66 static FILE *stream;
67 static fpos_t position;
68 static enum { nouse, getent, getby } last_use;
69 static int keep_stream;
71 /* Open database file if not already opened. */
72 static enum nss_status
73 internal_setent (int stayopen)
75 enum nss_status status = NSS_STATUS_SUCCESS;
77 if (stream == NULL)
79 stream = fopen (DATAFILE, "re");
81 if (stream == NULL)
82 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
83 else
85 #if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
86 # ifdef O_CLOEXEC
87 if (__have_o_cloexec <= 0)
88 # endif
90 /* We have to make sure the file is `closed on exec'. */
91 int result;
92 int flags;
94 result = flags = fcntl (fileno (stream), F_GETFD, 0);
95 if (result >= 0)
97 # ifdef O_CLOEXEC
98 if (__have_o_cloexec == 0)
99 __have_o_cloexec = (flags & FD_CLOEXEC) == 0 ? -1 : 1;
100 if (__have_o_cloexec < 0)
101 # endif
103 flags |= FD_CLOEXEC;
104 result = fcntl (fileno (stream), F_SETFD, flags);
107 if (result < 0)
109 /* Something went wrong. Close the stream and return a
110 failure. */
111 fclose (stream);
112 stream = NULL;
113 status = NSS_STATUS_UNAVAIL;
116 #endif
119 else
120 rewind (stream);
122 /* Remember STAYOPEN flag. */
123 if (stream != NULL)
124 keep_stream |= stayopen;
126 return status;
130 /* Thread-safe, exported version of that. */
131 enum nss_status
132 CONCAT(_nss_files_set,ENTNAME) (int stayopen)
134 enum nss_status status;
136 __libc_lock_lock (lock);
138 status = internal_setent (stayopen);
140 if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
142 fclose (stream);
143 stream = NULL;
144 status = NSS_STATUS_UNAVAIL;
147 last_use = getent;
149 __libc_lock_unlock (lock);
151 return status;
155 /* Close the database file. */
156 static void
157 internal_endent (void)
159 if (stream != NULL)
161 fclose (stream);
162 stream = NULL;
167 /* Thread-safe, exported version of that. */
168 enum nss_status
169 CONCAT(_nss_files_end,ENTNAME) (void)
171 __libc_lock_lock (lock);
173 internal_endent ();
175 /* Reset STAYOPEN flag. */
176 keep_stream = 0;
178 __libc_lock_unlock (lock);
180 return NSS_STATUS_SUCCESS;
183 /* Parsing the database file into `struct STRUCTURE' data structures. */
185 static enum nss_status
186 internal_getent (struct STRUCTURE *result,
187 char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO
188 EXTRA_ARGS_DECL)
190 char *p;
191 struct parser_data *data = (void *) buffer;
192 int linebuflen = buffer + buflen - data->linebuffer;
193 int parse_result;
195 if (buflen < sizeof *data + 2)
197 *errnop = ERANGE;
198 H_ERRNO_SET (NETDB_INTERNAL);
199 return NSS_STATUS_TRYAGAIN;
204 /* Terminate the line so that we can test for overflow. */
205 ((unsigned char *) data->linebuffer)[linebuflen - 1] = '\xff';
207 p = fgets_unlocked (data->linebuffer, linebuflen, stream);
208 if (p == NULL)
210 /* End of file or read error. */
211 H_ERRNO_SET (HOST_NOT_FOUND);
212 return NSS_STATUS_NOTFOUND;
214 else if (((unsigned char *) data->linebuffer)[linebuflen - 1] != 0xff)
216 /* The line is too long. Give the user the opportunity to
217 enlarge the buffer. */
218 *errnop = ERANGE;
219 H_ERRNO_SET (NETDB_INTERNAL);
220 return NSS_STATUS_TRYAGAIN;
223 /* Skip leading blanks. */
224 while (isspace (*p))
225 ++p;
227 while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */
228 /* Parse the line. If it is invalid, loop to get the next
229 line of the file to parse. */
230 || ! (parse_result = parse_line (p, result, data, buflen, errnop
231 EXTRA_ARGS)));
233 if (__builtin_expect (parse_result == -1, 0))
235 H_ERRNO_SET (NETDB_INTERNAL);
236 return NSS_STATUS_TRYAGAIN;
239 /* Filled in RESULT with the next entry from the database file. */
240 return NSS_STATUS_SUCCESS;
244 /* Return the next entry from the database file, doing locking. */
245 enum nss_status
246 CONCAT(_nss_files_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer,
247 size_t buflen, int *errnop H_ERRNO_PROTO)
249 /* Return next entry in host file. */
250 enum nss_status status = NSS_STATUS_SUCCESS;
252 __libc_lock_lock (lock);
254 /* Be prepared that the set*ent function was not called before. */
255 if (stream == NULL)
257 status = internal_setent (0);
259 if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
261 fclose (stream);
262 stream = NULL;
263 status = NSS_STATUS_UNAVAIL;
267 if (status == NSS_STATUS_SUCCESS)
269 /* If the last use was not by the getent function we need the
270 position the stream. */
271 if (last_use != getent)
273 if (fsetpos (stream, &position) < 0)
274 status = NSS_STATUS_UNAVAIL;
275 else
276 last_use = getent;
279 if (status == NSS_STATUS_SUCCESS)
281 status = internal_getent (result, buffer, buflen, errnop
282 H_ERRNO_ARG EXTRA_ARGS_VALUE);
284 /* Remember this position if we were successful. If the
285 operation failed we give the user a chance to repeat the
286 operation (perhaps the buffer was too small). */
287 if (status == NSS_STATUS_SUCCESS)
288 fgetpos (stream, &position);
289 else
290 /* We must make sure we reposition the stream the next call. */
291 last_use = nouse;
295 __libc_lock_unlock (lock);
297 return status;
300 /* Macro for defining lookup functions for this file-based database.
302 NAME is the name of the lookup; e.g. `hostbyname'.
304 KEYSIZE and KEYPATTERN are ignored here but used by ../nss_db/db-XXX.c.
306 PROTO describes the arguments for the lookup key;
307 e.g. `const char *hostname'.
309 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
310 to the lookup key arguments and does `break;' if they match. */
312 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
313 enum nss_status \
314 _nss_files_get##name##_r (proto, \
315 struct STRUCTURE *result, char *buffer, \
316 size_t buflen, int *errnop H_ERRNO_PROTO) \
318 enum nss_status status; \
320 __libc_lock_lock (lock); \
322 /* Reset file pointer to beginning or open file. */ \
323 status = internal_setent (keep_stream); \
325 if (status == NSS_STATUS_SUCCESS) \
327 /* Tell getent function that we have repositioned the file pointer. */ \
328 last_use = getby; \
330 while ((status = internal_getent (result, buffer, buflen, errnop \
331 H_ERRNO_ARG EXTRA_ARGS_VALUE)) \
332 == NSS_STATUS_SUCCESS) \
333 { break_if_match } \
335 if (! keep_stream) \
336 internal_endent (); \
339 __libc_lock_unlock (lock); \
341 return status; \