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