]> git.ipfire.org Git - thirdparty/glibc.git/blob - nss/nss_files/files-alias.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / nss / nss_files / files-alias.c
1 /* Mail alias file parser in nss_files module.
2 Copyright (C) 1996-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5
6 The GNU C Library is free software; you can redistribute it and/or
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.
10
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
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20 #include <aliases.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <bits/libc-lock.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include <kernel-features.h>
30
31 #include "nsswitch.h"
32
33 /* Locks the static variables in this file. */
34 __libc_lock_define_initialized (static, lock)
35 \f
36 /* Maintenance of the shared stream open on the database file. */
37
38 static FILE *stream;
39 static fpos_t position;
40 static enum { nouse, getent, getby } last_use;
41
42
43 static enum nss_status
44 internal_setent (void)
45 {
46 enum nss_status status = NSS_STATUS_SUCCESS;
47
48 if (stream == NULL)
49 {
50 stream = fopen ("/etc/aliases", "rce");
51
52 if (stream == NULL)
53 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
54 else
55 {
56 #if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
57 # ifdef O_CLOEXEC
58 if (__have_o_cloexec <= 0)
59 # endif
60 {
61 /* We have to make sure the file is `closed on exec'. */
62 int result;
63 int flags;
64
65 result = flags = fcntl (fileno (stream), F_GETFD, 0);
66 if (result >= 0)
67 {
68 # ifdef O_CLOEXEC
69 if (__have_o_cloexec == 0)
70 __have_o_cloexec = (flags & FD_CLOEXEC) == 0 ? -1 : 1;
71 if (__have_o_cloexec < 0)
72 # endif
73 {
74 flags |= FD_CLOEXEC;
75 result = fcntl (fileno (stream), F_SETFD, flags);
76 }
77 }
78 if (result < 0)
79 {
80 /* Something went wrong. Close the stream and return a
81 failure. */
82 fclose (stream);
83 stream = NULL;
84 status = NSS_STATUS_UNAVAIL;
85 }
86 }
87 #endif
88 }
89 }
90 else
91 rewind (stream);
92
93 return status;
94 }
95
96
97 /* Thread-safe, exported version of that. */
98 enum nss_status
99 _nss_files_setaliasent (void)
100 {
101 enum nss_status status;
102
103 __libc_lock_lock (lock);
104
105 status = internal_setent ();
106
107 if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
108 {
109 fclose (stream);
110 stream = NULL;
111 status = NSS_STATUS_UNAVAIL;
112 }
113
114 last_use = getent;
115
116 __libc_lock_unlock (lock);
117
118 return status;
119 }
120
121
122 /* Close the database file. */
123 static void
124 internal_endent (void)
125 {
126 if (stream != NULL)
127 {
128 fclose (stream);
129 stream = NULL;
130 }
131 }
132
133
134 /* Thread-safe, exported version of that. */
135 enum nss_status
136 _nss_files_endaliasent (void)
137 {
138 __libc_lock_lock (lock);
139
140 internal_endent ();
141
142 __libc_lock_unlock (lock);
143
144 return NSS_STATUS_SUCCESS;
145 }
146 \f
147 /* Parsing the database file into `struct aliasent' data structures. */
148 static enum nss_status
149 get_next_alias (const char *match, struct aliasent *result,
150 char *buffer, size_t buflen, int *errnop)
151 {
152 enum nss_status status = NSS_STATUS_NOTFOUND;
153 int ignore = 0;
154
155 result->alias_members_len = 0;
156
157 while (1)
158 {
159 /* Now we are ready to process the input. We have to read a
160 line and all its continuations and construct the array of
161 string pointers. This pointers and the names itself have to
162 be placed in BUFFER. */
163 char *first_unused = buffer;
164 size_t room_left = buflen - (buflen % __alignof__ (char *));
165 char *line;
166
167 /* Check whether the buffer is large enough for even trying to
168 read something. */
169 if (room_left < 2)
170 goto no_more_room;
171
172 /* Read the first line. It must contain the alias name and
173 possibly some alias names. */
174 first_unused[room_left - 1] = '\xff';
175 line = fgets_unlocked (first_unused, room_left, stream);
176 if (line == NULL)
177 /* Nothing to read. */
178 break;
179 else if (first_unused[room_left - 1] != '\xff')
180 {
181 /* The line is too long for our buffer. */
182 no_more_room:
183 *errnop = ERANGE;
184 status = NSS_STATUS_TRYAGAIN;
185 break;
186 }
187 else
188 {
189 char *cp;
190
191 /* If we are in IGNORE mode and the first character in the
192 line is a white space we ignore the line and start
193 reading the next. */
194 if (ignore && isspace (*first_unused))
195 continue;
196
197 /* Terminate the line for any case. */
198 cp = strpbrk (first_unused, "#\n");
199 if (cp != NULL)
200 *cp = '\0';
201
202 /* Skip leading blanks. */
203 while (isspace (*line))
204 ++line;
205
206 result->alias_name = first_unused;
207 while (*line != '\0' && *line != ':')
208 *first_unused++ = *line++;
209 if (*line == '\0' || result->alias_name == first_unused)
210 /* No valid name. Ignore the line. */
211 continue;
212
213 *first_unused++ = '\0';
214 if (room_left < (size_t) (first_unused - result->alias_name))
215 goto no_more_room;
216 room_left -= first_unused - result->alias_name;
217 ++line;
218
219 /* When we search for a specific alias we can avoid all the
220 difficult parts and compare now with the name we are
221 looking for. If it does not match we simply ignore all
222 lines until the next line containing the start of a new
223 alias is found. */
224 ignore = (match != NULL
225 && __strcasecmp (result->alias_name, match) != 0);
226
227 while (! ignore)
228 {
229 while (isspace (*line))
230 ++line;
231
232 cp = first_unused;
233 while (*line != '\0' && *line != ',')
234 *first_unused++ = *line++;
235
236 if (first_unused != cp)
237 {
238 /* OK, we can have a regular entry or an include
239 request. */
240 if (*line != '\0')
241 ++line;
242 *first_unused++ = '\0';
243
244 if (strncmp (cp, ":include:", 9) != 0)
245 {
246 if (room_left < (first_unused - cp) + sizeof (char *))
247 goto no_more_room;
248 room_left -= (first_unused - cp) + sizeof (char *);
249
250 ++result->alias_members_len;
251 }
252 else
253 {
254 /* Oh well, we have to read the addressed file. */
255 FILE *listfile;
256 char *old_line = NULL;
257
258 first_unused = cp;
259
260 listfile = fopen (&cp[9], "rce");
261 /* If the file does not exist we simply ignore
262 the statement. */
263 if (listfile != NULL
264 && (old_line = strdup (line)) != NULL)
265 {
266 while (! feof_unlocked (listfile))
267 {
268 first_unused[room_left - 1] = '\xff';
269 line = fgets_unlocked (first_unused, room_left,
270 listfile);
271 if (line == NULL)
272 break;
273 if (first_unused[room_left - 1] != '\xff')
274 {
275 free (old_line);
276 goto no_more_room;
277 }
278
279 /* Parse the line. */
280 cp = strpbrk (line, "#\n");
281 if (cp != NULL)
282 *cp = '\0';
283
284 do
285 {
286 while (isspace (*line))
287 ++line;
288
289 cp = first_unused;
290 while (*line != '\0' && *line != ',')
291 *first_unused++ = *line++;
292
293 if (*line != '\0')
294 ++line;
295
296 if (first_unused != cp)
297 {
298 *first_unused++ = '\0';
299 if (room_left < ((first_unused - cp)
300 + __alignof__ (char *)))
301 {
302 free (old_line);
303 goto no_more_room;
304 }
305 room_left -= ((first_unused - cp)
306 + __alignof__ (char *));
307 ++result->alias_members_len;
308 }
309 }
310 while (*line != '\0');
311 }
312 fclose (listfile);
313
314 first_unused[room_left - 1] = '\0';
315 strncpy (first_unused, old_line, room_left);
316
317 free (old_line);
318 line = first_unused;
319
320 if (first_unused[room_left - 1] != '\0')
321 goto no_more_room;
322 }
323 }
324 }
325
326 if (*line == '\0')
327 {
328 /* Get the next line. But we must be careful. We
329 must not read the whole line at once since it
330 might belong to the current alias. Simply read
331 the first character. If it is a white space we
332 have a continuation line. Otherwise it is the
333 beginning of a new alias and we can push back the
334 just read character. */
335 int ch;
336
337 ch = fgetc_unlocked (stream);
338 if (ch == EOF || ch == '\n' || !isspace (ch))
339 {
340 size_t cnt;
341
342 /* Now prepare the return. Provide string
343 pointers for the currently selected aliases. */
344 if (ch != EOF)
345 ungetc (ch, stream);
346
347 /* Adjust the pointer so it is aligned for
348 storing pointers. */
349 first_unused += __alignof__ (char *) - 1;
350 first_unused -= ((first_unused - (char *) 0)
351 % __alignof__ (char *));
352 result->alias_members = (char **) first_unused;
353
354 /* Compute addresses of alias entry strings. */
355 cp = result->alias_name;
356 for (cnt = 0; cnt < result->alias_members_len; ++cnt)
357 {
358 cp = strchr (cp, '\0') + 1;
359 result->alias_members[cnt] = cp;
360 }
361
362 status = (result->alias_members_len == 0
363 ? NSS_STATUS_RETURN : NSS_STATUS_SUCCESS);
364 break;
365 }
366
367 /* The just read character is a white space and so
368 can be ignored. */
369 first_unused[room_left - 1] = '\xff';
370 line = fgets_unlocked (first_unused, room_left, stream);
371 if (first_unused[room_left - 1] != '\xff')
372 goto no_more_room;
373 cp = strpbrk (line, "#\n");
374 if (cp != NULL)
375 *cp = '\0';
376 }
377 }
378 }
379
380 if (status != NSS_STATUS_NOTFOUND)
381 /* We read something. In any case break here. */
382 break;
383 }
384
385 return status;
386 }
387
388
389 enum nss_status
390 _nss_files_getaliasent_r (struct aliasent *result, char *buffer, size_t buflen,
391 int *errnop)
392 {
393 /* Return next entry in host file. */
394 enum nss_status status = NSS_STATUS_SUCCESS;
395
396 __libc_lock_lock (lock);
397
398 /* Be prepared that the set*ent function was not called before. */
399 if (stream == NULL)
400 status = internal_setent ();
401
402 if (status == NSS_STATUS_SUCCESS)
403 {
404 /* If the last use was not by the getent function we need the
405 position the stream. */
406 if (last_use != getent)
407 {
408 if (fsetpos (stream, &position) < 0)
409 status = NSS_STATUS_UNAVAIL;
410 else
411 last_use = getent;
412 }
413
414 if (status == NSS_STATUS_SUCCESS)
415 {
416 result->alias_local = 1;
417
418 /* Read lines until we get a definite result. */
419 do
420 status = get_next_alias (NULL, result, buffer, buflen, errnop);
421 while (status == NSS_STATUS_RETURN);
422
423 /* If we successfully read an entry remember this position. */
424 if (status == NSS_STATUS_SUCCESS)
425 fgetpos (stream, &position);
426 else
427 last_use = nouse;
428 }
429 }
430
431 __libc_lock_unlock (lock);
432
433 return status;
434 }
435
436
437 enum nss_status
438 _nss_files_getaliasbyname_r (const char *name, struct aliasent *result,
439 char *buffer, size_t buflen, int *errnop)
440 {
441 /* Return next entry in host file. */
442 enum nss_status status = NSS_STATUS_SUCCESS;
443
444 if (name == NULL)
445 {
446 __set_errno (EINVAL);
447 return NSS_STATUS_UNAVAIL;
448 }
449
450 __libc_lock_lock (lock);
451
452 /* Open the stream or rest it. */
453 status = internal_setent ();
454 last_use = getby;
455
456 if (status == NSS_STATUS_SUCCESS)
457 {
458 result->alias_local = 1;
459
460 /* Read lines until we get a definite result. */
461 do
462 status = get_next_alias (name, result, buffer, buflen, errnop);
463 while (status == NSS_STATUS_RETURN);
464 }
465
466 internal_endent ();
467
468 __libc_lock_unlock (lock);
469
470 return status;
471 }