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