1 /* Common code for file-based databases in nss_files module.
2 Copyright (C) 1996,1997,1998,1999,2001,2002 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
24 #include <bits/libc-lock.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
44 # define H_ERRNO_PROTO , int *herrnop
45 # define H_ERRNO_ARG , herrnop
46 # define H_ERRNO_SET(val) (*herrnop = (val))
48 # define H_ERRNO_PROTO
50 # define H_ERRNO_SET(val) ((void) 0)
55 # define EXTRA_ARGS_DECL
56 # define EXTRA_ARGS_VALUE
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. */
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
;
77 stream
= fopen (DATAFILE
, "r");
80 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
83 /* We have to make sure the file is `closed on exec'. */
86 result
= flags
= fcntl (fileno (stream
), F_GETFD
, 0);
90 result
= fcntl (fileno (stream
), F_SETFD
, flags
);
94 /* Something went wrong. Close the stream and return a
98 status
= NSS_STATUS_UNAVAIL
;
105 /* Remember STAYOPEN flag. */
107 keep_stream
|= stayopen
;
113 /* Thread-safe, exported version of that. */
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)
127 status
= NSS_STATUS_UNAVAIL
;
132 __libc_lock_unlock (lock
);
138 /* Close the database file. */
140 internal_endent (void)
150 /* Thread-safe, exported version of that. */
152 CONCAT(_nss_files_end
,ENTNAME
) (void)
154 __libc_lock_lock (lock
);
158 /* Reset STAYOPEN flag. */
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
174 struct parser_data
*data
= (void *) buffer
;
175 int linebuflen
= buffer
+ buflen
- data
->linebuffer
;
178 if (buflen
< sizeof *data
+ 2)
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
);
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. */
202 H_ERRNO_SET (NETDB_INTERNAL
);
203 return NSS_STATUS_TRYAGAIN
;
206 /* Skip leading blanks. */
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
216 /* Filled in RESULT with the next entry from the database file. */
217 return parse_result
== -1 ? NSS_STATUS_TRYAGAIN
: NSS_STATUS_SUCCESS
;
221 /* Return the next entry from the database file, doing locking. */
223 CONCAT(_nss_files_get
,ENTNAME_r
) (struct STRUCTURE
*result
, char *buffer
,
224 size_t buflen
, int *errnop H_ERRNO_PROTO
)
226 /* Return next entry in host file. */
227 enum nss_status status
= NSS_STATUS_SUCCESS
;
229 __libc_lock_lock (lock
);
231 /* Be prepared that the set*ent function was not called before. */
234 status
= internal_setent (0);
236 if (status
== NSS_STATUS_SUCCESS
&& fgetpos (stream
, &position
) < 0)
240 status
= NSS_STATUS_UNAVAIL
;
244 if (status
== NSS_STATUS_SUCCESS
)
246 /* If the last use was not by the getent function we need the
247 position the stream. */
248 if (last_use
!= getent
)
250 if (fsetpos (stream
, &position
) < 0)
251 status
= NSS_STATUS_UNAVAIL
;
256 if (status
== NSS_STATUS_SUCCESS
)
258 status
= internal_getent (result
, buffer
, buflen
, errnop
259 H_ERRNO_ARG EXTRA_ARGS_VALUE
);
261 /* Remember this position if we were successful. If the
262 operation failed we give the user a chance to repeat the
263 operation (perhaps the buffer was too small). */
264 if (status
== NSS_STATUS_SUCCESS
)
265 fgetpos (stream
, &position
);
267 /* We must make sure we reposition the stream the next call. */
272 __libc_lock_unlock (lock
);
277 /* Macro for defining lookup functions for this file-based database.
279 NAME is the name of the lookup; e.g. `hostbyname'.
281 KEYSIZE and KEYPATTERN are ignored here but used by ../nss_db/db-XXX.c.
283 PROTO describes the arguments for the lookup key;
284 e.g. `const char *hostname'.
286 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
287 to the lookup key arguments and does `break;' if they match. */
289 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
291 _nss_files_get##name##_r (proto, \
292 struct STRUCTURE *result, char *buffer, \
293 size_t buflen, int *errnop H_ERRNO_PROTO) \
295 enum nss_status status; \
297 __libc_lock_lock (lock); \
299 /* Reset file pointer to beginning or open file. */ \
300 status = internal_setent (keep_stream); \
302 if (status == NSS_STATUS_SUCCESS) \
304 /* Tell getent function that we have repositioned the file pointer. */ \
307 while ((status = internal_getent (result, buffer, buflen, errnop \
308 H_ERRNO_ARG EXTRA_ARGS_VALUE)) \
309 == NSS_STATUS_SUCCESS) \
313 internal_endent (); \
316 __libc_lock_unlock (lock); \