]> git.ipfire.org Git - thirdparty/glibc.git/blame - nss/nss_files/files-XXX.c
Use <> for include of kernel-features.h.
[thirdparty/glibc.git] / nss / nss_files / files-XXX.c
CommitLineData
5f0e6fc7 1/* Common code for file-based databases in nss_files module.
2666d441 2 Copyright (C) 1996-1999,2001,2002,2004,2007,2008,2011
0facd3df 3 Free Software Foundation, Inc.
2303f5fd 4 This file is part of the GNU C Library.
5f0e6fc7 5
2303f5fd 6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
5f0e6fc7 10
2303f5fd
UD
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 14 Lesser General Public License for more details.
5f0e6fc7 15
41bdb6e2
AJ
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
5f0e6fc7
RM
20
21#include <stdio.h>
22#include <ctype.h>
26761c28 23#include <errno.h>
3996f34b 24#include <fcntl.h>
5107cf1d 25#include <bits/libc-lock.h>
5f0e6fc7
RM
26#include "nsswitch.h"
27
aa132749
UD
28#include <kernel-features.h>
29
5f0e6fc7
RM
30/* These symbols are defined by the including source file:
31
32 ENTNAME -- database name of the structure and functions (hostent, pwent).
33 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
96bda0ea 34 DATABASE -- string of the database file's name ("hosts", "passwd").
5f0e6fc7
RM
35
36 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
adc6ff7f
RM
37
38 Also see files-parse.c.
5f0e6fc7
RM
39*/
40
96bda0ea
RM
41#define ENTNAME_r CONCAT(ENTNAME,_r)
42
43#define DATAFILE "/etc/" DATABASE
5f0e6fc7
RM
44
45#ifdef NEED_H_ERRNO
47707456 46# include <netdb.h>
26761c28
UD
47# define H_ERRNO_PROTO , int *herrnop
48# define H_ERRNO_ARG , herrnop
49# define H_ERRNO_SET(val) (*herrnop = (val))
5f0e6fc7 50#else
26761c28
UD
51# define H_ERRNO_PROTO
52# define H_ERRNO_ARG
53# define H_ERRNO_SET(val) ((void) 0)
5f0e6fc7
RM
54#endif
55
71d3bda9
UD
56#ifndef EXTRA_ARGS
57# define EXTRA_ARGS
58# define EXTRA_ARGS_DECL
59# define EXTRA_ARGS_VALUE
60#endif
61
5f0e6fc7 62/* Locks the static variables in this file. */
1e16111c 63__libc_lock_define_initialized (static, lock)
5f0e6fc7
RM
64\f
65/* Maintenance of the shared stream open on the database file. */
66
67static FILE *stream;
2303f5fd 68static fpos_t position;
0f283ffc 69static enum { nouse, getent, getby } last_use;
5f0e6fc7
RM
70static int keep_stream;
71
72/* Open database file if not already opened. */
26761c28 73static enum nss_status
5f0e6fc7
RM
74internal_setent (int stayopen)
75{
26761c28 76 enum nss_status status = NSS_STATUS_SUCCESS;
5f0e6fc7
RM
77
78 if (stream == NULL)
79 {
312be3f9 80 stream = fopen (DATAFILE, "rce");
5f0e6fc7
RM
81
82 if (stream == NULL)
0413b54c 83 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
3996f34b
UD
84 else
85 {
aa132749
UD
86#if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
87# ifdef O_CLOEXEC
88 if (__have_o_cloexec <= 0)
89# endif
3996f34b 90 {
aa132749
UD
91 /* We have to make sure the file is `closed on exec'. */
92 int result;
93 int flags;
94
95 result = flags = fcntl (fileno (stream), F_GETFD, 0);
96 if (result >= 0)
97 {
98# ifdef O_CLOEXEC
99 if (__have_o_cloexec == 0)
100 __have_o_cloexec = (flags & FD_CLOEXEC) == 0 ? -1 : 1;
101 if (__have_o_cloexec < 0)
102# endif
103 {
104 flags |= FD_CLOEXEC;
105 result = fcntl (fileno (stream), F_SETFD, flags);
106 }
107 }
108 if (result < 0)
109 {
110 /* Something went wrong. Close the stream and return a
111 failure. */
112 fclose (stream);
113 stream = NULL;
114 status = NSS_STATUS_UNAVAIL;
115 }
3996f34b 116 }
aa132749 117#endif
3996f34b 118 }
5f0e6fc7
RM
119 }
120 else
121 rewind (stream);
122
123 /* Remember STAYOPEN flag. */
124 if (stream != NULL)
125 keep_stream |= stayopen;
126
127 return status;
128}
129
130
131/* Thread-safe, exported version of that. */
26761c28 132enum nss_status
5f0e6fc7
RM
133CONCAT(_nss_files_set,ENTNAME) (int stayopen)
134{
26761c28 135 enum nss_status status;
5f0e6fc7
RM
136
137 __libc_lock_lock (lock);
138
139 status = internal_setent (stayopen);
140
2303f5fd
UD
141 if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
142 {
143 fclose (stream);
144 stream = NULL;
145 status = NSS_STATUS_UNAVAIL;
146 }
147
148 last_use = getent;
149
5f0e6fc7
RM
150 __libc_lock_unlock (lock);
151
152 return status;
153}
154
155
156/* Close the database file. */
157static void
158internal_endent (void)
159{
160 if (stream != NULL)
161 {
162 fclose (stream);
163 stream = NULL;
164 }
5f0e6fc7
RM
165}
166
167
168/* Thread-safe, exported version of that. */
26761c28 169enum nss_status
5f0e6fc7
RM
170CONCAT(_nss_files_end,ENTNAME) (void)
171{
172 __libc_lock_lock (lock);
173
174 internal_endent ();
175
6dbe2837
RM
176 /* Reset STAYOPEN flag. */
177 keep_stream = 0;
178
5f0e6fc7
RM
179 __libc_lock_unlock (lock);
180
181 return NSS_STATUS_SUCCESS;
182}
183\f
184/* Parsing the database file into `struct STRUCTURE' data structures. */
185
186static enum nss_status
187internal_getent (struct STRUCTURE *result,
71d3bda9
UD
188 char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO
189 EXTRA_ARGS_DECL)
5f0e6fc7
RM
190{
191 char *p;
192 struct parser_data *data = (void *) buffer;
193 int linebuflen = buffer + buflen - data->linebuffer;
22d57dd3 194 int parse_result;
5f0e6fc7 195
4caef86c 196 if (buflen < sizeof *data + 2)
5f0e6fc7 197 {
d71b808a 198 *errnop = ERANGE;
47707456 199 H_ERRNO_SET (NETDB_INTERNAL);
ffee1316 200 return NSS_STATUS_TRYAGAIN;
5f0e6fc7
RM
201 }
202
203 do
204 {
26761c28 205 /* Terminate the line so that we can test for overflow. */
3fd13f9e 206 ((unsigned char *) data->linebuffer)[linebuflen - 1] = '\xff';
26761c28 207
71412a8c 208 p = fgets_unlocked (data->linebuffer, linebuflen, stream);
5f0e6fc7
RM
209 if (p == NULL)
210 {
211 /* End of file or read error. */
212 H_ERRNO_SET (HOST_NOT_FOUND);
213 return NSS_STATUS_NOTFOUND;
214 }
3fd13f9e 215 else if (((unsigned char *) data->linebuffer)[linebuflen - 1] != 0xff)
26761c28
UD
216 {
217 /* The line is too long. Give the user the opportunity to
218 enlarge the buffer. */
d71b808a 219 *errnop = ERANGE;
26761c28
UD
220 H_ERRNO_SET (NETDB_INTERNAL);
221 return NSS_STATUS_TRYAGAIN;
222 }
6dbe2837 223
5f0e6fc7
RM
224 /* Skip leading blanks. */
225 while (isspace (*p))
226 ++p;
26761c28
UD
227 }
228 while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */
229 /* Parse the line. If it is invalid, loop to get the next
230 line of the file to parse. */
71d3bda9
UD
231 || ! (parse_result = parse_line (p, result, data, buflen, errnop
232 EXTRA_ARGS)));
5f0e6fc7 233
405521b5
UD
234 if (__builtin_expect (parse_result == -1, 0))
235 {
236 H_ERRNO_SET (NETDB_INTERNAL);
237 return NSS_STATUS_TRYAGAIN;
238 }
239
5f0e6fc7 240 /* Filled in RESULT with the next entry from the database file. */
405521b5 241 return NSS_STATUS_SUCCESS;
5f0e6fc7
RM
242}
243
244
245/* Return the next entry from the database file, doing locking. */
26761c28 246enum nss_status
d71b808a
UD
247CONCAT(_nss_files_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer,
248 size_t buflen, int *errnop H_ERRNO_PROTO)
5f0e6fc7
RM
249{
250 /* Return next entry in host file. */
26761c28 251 enum nss_status status = NSS_STATUS_SUCCESS;
5f0e6fc7
RM
252
253 __libc_lock_lock (lock);
254
26761c28
UD
255 /* Be prepared that the set*ent function was not called before. */
256 if (stream == NULL)
d47aac39 257 {
0facd3df
UD
258 int save_errno = errno;
259
d47aac39
UD
260 status = internal_setent (0);
261
9aef35a5 262 __set_errno (save_errno);
0facd3df 263
d47aac39
UD
264 if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
265 {
266 fclose (stream);
267 stream = NULL;
268 status = NSS_STATUS_UNAVAIL;
269 }
270 }
2303f5fd 271
54d79e99 272 if (status == NSS_STATUS_SUCCESS)
2303f5fd 273 {
26761c28
UD
274 /* If the last use was not by the getent function we need the
275 position the stream. */
276 if (last_use != getent)
04795ad9
UD
277 {
278 if (fsetpos (stream, &position) < 0)
279 status = NSS_STATUS_UNAVAIL;
280 else
281 last_use = getent;
282 }
26761c28
UD
283
284 if (status == NSS_STATUS_SUCCESS)
285 {
d71b808a 286 status = internal_getent (result, buffer, buflen, errnop
71d3bda9 287 H_ERRNO_ARG EXTRA_ARGS_VALUE);
26761c28
UD
288
289 /* Remember this position if we were successful. If the
290 operation failed we give the user a chance to repeat the
291 operation (perhaps the buffer was too small). */
292 if (status == NSS_STATUS_SUCCESS)
293 fgetpos (stream, &position);
294 else
295 /* We must make sure we reposition the stream the next call. */
0f283ffc 296 last_use = nouse;
26761c28 297 }
2303f5fd 298 }
5f0e6fc7
RM
299
300 __libc_lock_unlock (lock);
301
302 return status;
303}
304\f
305/* Macro for defining lookup functions for this file-based database.
306
307 NAME is the name of the lookup; e.g. `hostbyname'.
308
2666d441
UD
309 DB_CHAR, KEYPATTERN, KEYSIZE are ignored here but used by db-XXX.c
310 e.g. `1 + sizeof (id) * 4'.
96bda0ea 311
2666d441 312 PROTO is the potentially empty list of other parameters.
5f0e6fc7
RM
313
314 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
315 to the lookup key arguments and does `break;' if they match. */
316
2666d441 317#define DB_LOOKUP(name, db_char, keysize, keypattern, break_if_match, proto...)\
5f0e6fc7
RM
318enum nss_status \
319_nss_files_get##name##_r (proto, \
d71b808a
UD
320 struct STRUCTURE *result, char *buffer, \
321 size_t buflen, int *errnop H_ERRNO_PROTO) \
5f0e6fc7
RM
322{ \
323 enum nss_status status; \
324 \
325 __libc_lock_lock (lock); \
326 \
327 /* Reset file pointer to beginning or open file. */ \
26761c28 328 status = internal_setent (keep_stream); \
5f0e6fc7 329 \
26761c28
UD
330 if (status == NSS_STATUS_SUCCESS) \
331 { \
332 /* Tell getent function that we have repositioned the file pointer. */ \
333 last_use = getby; \
2303f5fd 334 \
d71b808a 335 while ((status = internal_getent (result, buffer, buflen, errnop \
71d3bda9 336 H_ERRNO_ARG EXTRA_ARGS_VALUE)) \
26761c28
UD
337 == NSS_STATUS_SUCCESS) \
338 { break_if_match } \
5f0e6fc7 339 \
26761c28
UD
340 if (! keep_stream) \
341 internal_endent (); \
342 } \
5f0e6fc7
RM
343 \
344 __libc_lock_unlock (lock); \
345 \
346 return status; \
347}