* nis/nis_domain_of_r.c (nis_domain_of_r): Add missing buffer
[glibc.git] / nss / nss_files / files-XXX.c
blobfb13fbe2b60391159fec89c4c0c206e8b0375f0f
1 /* Common code for file-based databases in nss_files module.
2 Copyright (C) 1996-1999,2001,2002,2004 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 /* These symbols are defined by the including source file:
29 ENTNAME -- database name of the structure and functions (hostent, pwent).
30 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
31 DATABASE -- string of the database file's name ("hosts", "passwd").
33 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
35 Also see files-parse.c.
38 #define ENTNAME_r CONCAT(ENTNAME,_r)
40 #define DATAFILE "/etc/" DATABASE
42 #ifdef NEED_H_ERRNO
43 # include <netdb.h>
44 # define H_ERRNO_PROTO , int *herrnop
45 # define H_ERRNO_ARG , herrnop
46 # define H_ERRNO_SET(val) (*herrnop = (val))
47 #else
48 # define H_ERRNO_PROTO
49 # define H_ERRNO_ARG
50 # define H_ERRNO_SET(val) ((void) 0)
51 #endif
53 #ifndef EXTRA_ARGS
54 # define EXTRA_ARGS
55 # define EXTRA_ARGS_DECL
56 # define EXTRA_ARGS_VALUE
57 #endif
59 /* Locks the static variables in this file. */
60 __libc_lock_define_initialized (static, lock)
62 /* Maintenance of the shared stream open on the database file. */
64 static FILE *stream;
65 static fpos_t position;
66 static enum { nouse, getent, getby } last_use;
67 static int keep_stream;
69 /* Open database file if not already opened. */
70 static enum nss_status
71 internal_setent (int stayopen)
73 enum nss_status status = NSS_STATUS_SUCCESS;
75 if (stream == NULL)
77 stream = fopen (DATAFILE, "r");
79 if (stream == NULL)
80 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
81 else
83 /* We have to make sure the file is `closed on exec'. */
84 int result, flags;
86 result = flags = fcntl (fileno (stream), F_GETFD, 0);
87 if (result >= 0)
89 flags |= FD_CLOEXEC;
90 result = fcntl (fileno (stream), F_SETFD, flags);
92 if (result < 0)
94 /* Something went wrong. Close the stream and return a
95 failure. */
96 fclose (stream);
97 stream = NULL;
98 status = NSS_STATUS_UNAVAIL;
102 else
103 rewind (stream);
105 /* Remember STAYOPEN flag. */
106 if (stream != NULL)
107 keep_stream |= stayopen;
109 return status;
113 /* Thread-safe, exported version of that. */
114 enum nss_status
115 CONCAT(_nss_files_set,ENTNAME) (int stayopen)
117 enum nss_status status;
119 __libc_lock_lock (lock);
121 status = internal_setent (stayopen);
123 if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
125 fclose (stream);
126 stream = NULL;
127 status = NSS_STATUS_UNAVAIL;
130 last_use = getent;
132 __libc_lock_unlock (lock);
134 return status;
138 /* Close the database file. */
139 static void
140 internal_endent (void)
142 if (stream != NULL)
144 fclose (stream);
145 stream = NULL;
150 /* Thread-safe, exported version of that. */
151 enum nss_status
152 CONCAT(_nss_files_end,ENTNAME) (void)
154 __libc_lock_lock (lock);
156 internal_endent ();
158 /* Reset STAYOPEN flag. */
159 keep_stream = 0;
161 __libc_lock_unlock (lock);
163 return NSS_STATUS_SUCCESS;
166 /* Parsing the database file into `struct STRUCTURE' data structures. */
168 static enum nss_status
169 internal_getent (struct STRUCTURE *result,
170 char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO
171 EXTRA_ARGS_DECL)
173 char *p;
174 struct parser_data *data = (void *) buffer;
175 int linebuflen = buffer + buflen - data->linebuffer;
176 int parse_result;
178 if (buflen < sizeof *data + 2)
180 *errnop = ERANGE;
181 H_ERRNO_SET (NETDB_INTERNAL);
182 return NSS_STATUS_TRYAGAIN;
187 /* Terminate the line so that we can test for overflow. */
188 ((unsigned char *) data->linebuffer)[linebuflen - 1] = '\xff';
190 p = fgets_unlocked (data->linebuffer, linebuflen, stream);
191 if (p == NULL)
193 /* End of file or read error. */
194 H_ERRNO_SET (HOST_NOT_FOUND);
195 return NSS_STATUS_NOTFOUND;
197 else if (((unsigned char *) data->linebuffer)[linebuflen - 1] != 0xff)
199 /* The line is too long. Give the user the opportunity to
200 enlarge the buffer. */
201 *errnop = ERANGE;
202 H_ERRNO_SET (NETDB_INTERNAL);
203 return NSS_STATUS_TRYAGAIN;
206 /* Skip leading blanks. */
207 while (isspace (*p))
208 ++p;
210 while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */
211 /* Parse the line. If it is invalid, loop to get the next
212 line of the file to parse. */
213 || ! (parse_result = parse_line (p, result, data, buflen, errnop
214 EXTRA_ARGS)));
216 if (__builtin_expect (parse_result == -1, 0))
218 H_ERRNO_SET (NETDB_INTERNAL);
219 return NSS_STATUS_TRYAGAIN;
222 /* Filled in RESULT with the next entry from the database file. */
223 return NSS_STATUS_SUCCESS;
227 /* Return the next entry from the database file, doing locking. */
228 enum nss_status
229 CONCAT(_nss_files_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 = NSS_STATUS_SUCCESS;
235 __libc_lock_lock (lock);
237 /* Be prepared that the set*ent function was not called before. */
238 if (stream == NULL)
240 status = internal_setent (0);
242 if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
244 fclose (stream);
245 stream = NULL;
246 status = NSS_STATUS_UNAVAIL;
250 if (status == NSS_STATUS_SUCCESS)
252 /* If the last use was not by the getent function we need the
253 position the stream. */
254 if (last_use != getent)
256 if (fsetpos (stream, &position) < 0)
257 status = NSS_STATUS_UNAVAIL;
258 else
259 last_use = getent;
262 if (status == NSS_STATUS_SUCCESS)
264 status = internal_getent (result, buffer, buflen, errnop
265 H_ERRNO_ARG EXTRA_ARGS_VALUE);
267 /* Remember this position if we were successful. If the
268 operation failed we give the user a chance to repeat the
269 operation (perhaps the buffer was too small). */
270 if (status == NSS_STATUS_SUCCESS)
271 fgetpos (stream, &position);
272 else
273 /* We must make sure we reposition the stream the next call. */
274 last_use = nouse;
278 __libc_lock_unlock (lock);
280 return status;
283 /* Macro for defining lookup functions for this file-based database.
285 NAME is the name of the lookup; e.g. `hostbyname'.
287 KEYSIZE and KEYPATTERN are ignored here but used by ../nss_db/db-XXX.c.
289 PROTO describes the arguments for the lookup key;
290 e.g. `const char *hostname'.
292 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
293 to the lookup key arguments and does `break;' if they match. */
295 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
296 enum nss_status \
297 _nss_files_get##name##_r (proto, \
298 struct STRUCTURE *result, char *buffer, \
299 size_t buflen, int *errnop H_ERRNO_PROTO) \
301 enum nss_status status; \
303 __libc_lock_lock (lock); \
305 /* Reset file pointer to beginning or open file. */ \
306 status = internal_setent (keep_stream); \
308 if (status == NSS_STATUS_SUCCESS) \
310 /* Tell getent function that we have repositioned the file pointer. */ \
311 last_use = getby; \
313 while ((status = internal_getent (result, buffer, buflen, errnop \
314 H_ERRNO_ARG EXTRA_ARGS_VALUE)) \
315 == NSS_STATUS_SUCCESS) \
316 { break_if_match } \
318 if (! keep_stream) \
319 internal_endent (); \
322 __libc_lock_unlock (lock); \
324 return status; \