update from main archive 961203
[platform/upstream/glibc.git] / nss / nss_files / files-XXX.c
1 /* Common code for file-based databases in nss_files module.
2    Copyright (C) 1996 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 Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    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    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <libc-lock.h>
25 #include "nsswitch.h"
26
27 /* These symbols are defined by the including source file:
28
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").
32
33    NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
34
35    Also see files-parse.c.
36 */
37
38 #define ENTNAME_r       CONCAT(ENTNAME,_r)
39
40 #define DATAFILE        "/etc/" DATABASE
41
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
52
53 /* Locks the static variables in this file.  */
54 __libc_lock_define_initialized (static, lock)
55 \f
56 /* Maintenance of the shared stream open on the database file.  */
57
58 static FILE *stream;
59 static fpos_t position;
60 static enum { none, getent, getby } last_use;
61 static int keep_stream;
62
63 /* Open database file if not already opened.  */
64 static enum nss_status
65 internal_setent (int stayopen)
66 {
67   enum nss_status status = NSS_STATUS_SUCCESS;
68
69   if (stream == NULL)
70     {
71       stream = fopen (DATAFILE, "r");
72
73       if (stream == NULL)
74         status = NSS_STATUS_UNAVAIL;
75     }
76   else
77     rewind (stream);
78
79   /* Remember STAYOPEN flag.  */
80   if (stream != NULL)
81     keep_stream |= stayopen;
82
83   return status;
84 }
85
86
87 /* Thread-safe, exported version of that.  */
88 enum nss_status
89 CONCAT(_nss_files_set,ENTNAME) (int stayopen)
90 {
91   enum nss_status status;
92
93   __libc_lock_lock (lock);
94
95   status = internal_setent (stayopen);
96
97   if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
98     {
99       fclose (stream);
100       stream = NULL;
101       status = NSS_STATUS_UNAVAIL;
102     }
103
104   last_use = getent;
105
106   __libc_lock_unlock (lock);
107
108   return status;
109 }
110
111
112 /* Close the database file.  */
113 static void
114 internal_endent (void)
115 {
116   if (stream != NULL)
117     {
118       fclose (stream);
119       stream = NULL;
120     }
121 }
122
123
124 /* Thread-safe, exported version of that.  */
125 enum nss_status
126 CONCAT(_nss_files_end,ENTNAME) (void)
127 {
128   __libc_lock_lock (lock);
129
130   internal_endent ();
131
132   /* Reset STAYOPEN flag.  */
133   keep_stream = 0;
134
135   __libc_lock_unlock (lock);
136
137   return NSS_STATUS_SUCCESS;
138 }
139 \f
140 /* Parsing the database file into `struct STRUCTURE' data structures.  */
141
142 static enum nss_status
143 internal_getent (struct STRUCTURE *result,
144                  char *buffer, int buflen H_ERRNO_PROTO)
145 {
146   char *p;
147   struct parser_data *data = (void *) buffer;
148   int linebuflen = buffer + buflen - data->linebuffer;
149
150   if (buflen < (int) sizeof *data + 1)
151     {
152       __set_errno (ERANGE);
153       H_ERRNO_SET (NETDB_INTERNAL);
154       return NSS_STATUS_TRYAGAIN;
155     }
156
157   do
158     {
159       /* Terminate the line so that we can test for overflow.  */
160       data->linebuffer[linebuflen - 1] = '\0';
161
162       p = fgets (data->linebuffer, linebuflen, stream);
163       if (p == NULL)
164         {
165           /* End of file or read error.  */
166           H_ERRNO_SET (HOST_NOT_FOUND);
167           return NSS_STATUS_NOTFOUND;
168         }
169       else if (data->linebuffer[linebuflen - 1] != '\0')
170         {
171           /* The line is too long.  Give the user the opportunity to
172              enlarge the buffer.  */
173           __set_errno (ERANGE);
174           H_ERRNO_SET (NETDB_INTERNAL);
175           return NSS_STATUS_TRYAGAIN;
176         }
177
178       /* Skip leading blanks.  */
179       while (isspace (*p))
180         ++p;
181     }
182   while (*p == '\0' || *p == '#' /* Ignore empty and comment lines.  */
183          /* Parse the line.  If it is invalid, loop to get the next
184             line of the file to parse.  */
185          || ! parse_line (p, result, data, buflen));
186
187   /* Filled in RESULT with the next entry from the database file.  */
188   return NSS_STATUS_SUCCESS;
189 }
190
191
192 /* Return the next entry from the database file, doing locking.  */
193 enum nss_status
194 CONCAT(_nss_files_get,ENTNAME_r) (struct STRUCTURE *result,
195                                   char *buffer, size_t buflen H_ERRNO_PROTO)
196 {
197   /* Return next entry in host file.  */
198   enum nss_status status = NSS_STATUS_SUCCESS;
199
200   __libc_lock_lock (lock);
201
202   /* Be prepared that the set*ent function was not called before.  */
203   if (stream == NULL)
204     status = internal_setent (0);
205
206   if (status == NSS_STATUS_SUCCESS)
207     {
208       /* If the last use was not by the getent function we need the
209          position the stream.  */
210       if (last_use != getent)
211         if (fsetpos (stream, &position) < 0)
212           status = NSS_STATUS_UNAVAIL;
213         else
214           last_use = getent;
215
216       if (status == NSS_STATUS_SUCCESS)
217         {
218           status = internal_getent (result, buffer, buflen H_ERRNO_ARG);
219
220           /* Remember this position if we were successful.  If the
221              operation failed we give the user a chance to repeat the
222              operation (perhaps the buffer was too small).  */
223           if (status == NSS_STATUS_SUCCESS)
224             fgetpos (stream, &position);
225           else
226             /* We must make sure we reposition the stream the next call.  */
227             last_use = none;
228         }
229     }
230
231   __libc_lock_unlock (lock);
232
233   return status;
234 }
235 \f
236 /* Macro for defining lookup functions for this file-based database.
237
238    NAME is the name of the lookup; e.g. `hostbyname'.
239
240    KEYSIZE and KEYPATTERN are ignored here but used by ../nss_db/db-XXX.c.
241
242    PROTO describes the arguments for the lookup key;
243    e.g. `const char *hostname'.
244
245    BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
246    to the lookup key arguments and does `break;' if they match.  */
247
248 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...)        \
249 enum nss_status                                                               \
250 _nss_files_get##name##_r (proto,                                              \
251                           struct STRUCTURE *result,                           \
252                           char *buffer, size_t buflen H_ERRNO_PROTO)          \
253 {                                                                             \
254   enum nss_status status;                                                     \
255                                                                               \
256   __libc_lock_lock (lock);                                                    \
257                                                                               \
258   /* Reset file pointer to beginning or open file.  */                        \
259   status = internal_setent (keep_stream);                                     \
260                                                                               \
261   if (status == NSS_STATUS_SUCCESS)                                           \
262     {                                                                         \
263       /* Tell getent function that we have repositioned the file pointer.  */ \
264       last_use = getby;                                                       \
265                                                                               \
266       while ((status = internal_getent (result, buffer, buflen H_ERRNO_ARG))  \
267              == NSS_STATUS_SUCCESS)                                           \
268         { break_if_match }                                                    \
269                                                                               \
270       if (! keep_stream)                                                      \
271         internal_endent ();                                                   \
272     }                                                                         \
273                                                                               \
274   __libc_lock_unlock (lock);                                                  \
275                                                                               \
276   return status;                                                              \
277 }