ARM: Rewrite sysdeps/arm/tls-macros.h
[glibc.git] / nss / nss_files / files-alias.c
blobd5c5089e2efc02ed6b4a358a936f77e659f2ba40
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.
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.
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.
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/>. */
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>
29 #include <kernel-features.h>
31 #include "nsswitch.h"
33 /* Locks the static variables in this file. */
34 __libc_lock_define_initialized (static, lock)
36 /* Maintenance of the shared stream open on the database file. */
38 static FILE *stream;
39 static fpos_t position;
40 static enum { nouse, getent, getby } last_use;
43 static enum nss_status
44 internal_setent (void)
46 enum nss_status status = NSS_STATUS_SUCCESS;
48 if (stream == NULL)
50 stream = fopen ("/etc/aliases", "rce");
52 if (stream == NULL)
53 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
54 else
56 #if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
57 # ifdef O_CLOEXEC
58 if (__have_o_cloexec <= 0)
59 # endif
61 /* We have to make sure the file is `closed on exec'. */
62 int result;
63 int flags;
65 result = flags = fcntl (fileno (stream), F_GETFD, 0);
66 if (result >= 0)
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
74 flags |= FD_CLOEXEC;
75 result = fcntl (fileno (stream), F_SETFD, flags);
78 if (result < 0)
80 /* Something went wrong. Close the stream and return a
81 failure. */
82 fclose (stream);
83 stream = NULL;
84 status = NSS_STATUS_UNAVAIL;
87 #endif
90 else
91 rewind (stream);
93 return status;
97 /* Thread-safe, exported version of that. */
98 enum nss_status
99 _nss_files_setaliasent (void)
101 enum nss_status status;
103 __libc_lock_lock (lock);
105 status = internal_setent ();
107 if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
109 fclose (stream);
110 stream = NULL;
111 status = NSS_STATUS_UNAVAIL;
114 last_use = getent;
116 __libc_lock_unlock (lock);
118 return status;
122 /* Close the database file. */
123 static void
124 internal_endent (void)
126 if (stream != NULL)
128 fclose (stream);
129 stream = NULL;
134 /* Thread-safe, exported version of that. */
135 enum nss_status
136 _nss_files_endaliasent (void)
138 __libc_lock_lock (lock);
140 internal_endent ();
142 __libc_lock_unlock (lock);
144 return NSS_STATUS_SUCCESS;
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)
152 enum nss_status status = NSS_STATUS_NOTFOUND;
153 int ignore = 0;
155 result->alias_members_len = 0;
157 while (1)
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;
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;
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')
181 /* The line is too long for our buffer. */
182 no_more_room:
183 *errnop = ERANGE;
184 status = NSS_STATUS_TRYAGAIN;
185 break;
187 else
189 char *cp;
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;
197 /* Terminate the line for any case. */
198 cp = strpbrk (first_unused, "#\n");
199 if (cp != NULL)
200 *cp = '\0';
202 /* Skip leading blanks. */
203 while (isspace (*line))
204 ++line;
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;
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;
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);
227 while (! ignore)
229 while (isspace (*line))
230 ++line;
232 cp = first_unused;
233 while (*line != '\0' && *line != ',')
234 *first_unused++ = *line++;
236 if (first_unused != cp)
238 /* OK, we can have a regular entry or an include
239 request. */
240 if (*line != '\0')
241 ++line;
242 *first_unused++ = '\0';
244 if (strncmp (cp, ":include:", 9) != 0)
246 if (room_left < (first_unused - cp) + sizeof (char *))
247 goto no_more_room;
248 room_left -= (first_unused - cp) + sizeof (char *);
250 ++result->alias_members_len;
252 else
254 /* Oh well, we have to read the addressed file. */
255 FILE *listfile;
256 char *old_line = NULL;
258 first_unused = cp;
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)
266 while (! feof_unlocked (listfile))
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')
275 free (old_line);
276 goto no_more_room;
279 /* Parse the line. */
280 cp = strpbrk (line, "#\n");
281 if (cp != NULL)
282 *cp = '\0';
286 while (isspace (*line))
287 ++line;
289 cp = first_unused;
290 while (*line != '\0' && *line != ',')
291 *first_unused++ = *line++;
293 if (*line != '\0')
294 ++line;
296 if (first_unused != cp)
298 *first_unused++ = '\0';
299 if (room_left < ((first_unused - cp)
300 + __alignof__ (char *)))
302 free (old_line);
303 goto no_more_room;
305 room_left -= ((first_unused - cp)
306 + __alignof__ (char *));
307 ++result->alias_members_len;
310 while (*line != '\0');
312 fclose (listfile);
314 first_unused[room_left - 1] = '\0';
315 strncpy (first_unused, old_line, room_left);
317 free (old_line);
318 line = first_unused;
320 if (first_unused[room_left - 1] != '\0')
321 goto no_more_room;
326 if (*line == '\0')
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;
337 ch = fgetc_unlocked (stream);
338 if (ch == EOF || ch == '\n' || !isspace (ch))
340 size_t cnt;
342 /* Now prepare the return. Provide string
343 pointers for the currently selected aliases. */
344 if (ch != EOF)
345 ungetc (ch, stream);
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;
354 /* Compute addresses of alias entry strings. */
355 cp = result->alias_name;
356 for (cnt = 0; cnt < result->alias_members_len; ++cnt)
358 cp = strchr (cp, '\0') + 1;
359 result->alias_members[cnt] = cp;
362 status = (result->alias_members_len == 0
363 ? NSS_STATUS_RETURN : NSS_STATUS_SUCCESS);
364 break;
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';
380 if (status != NSS_STATUS_NOTFOUND)
381 /* We read something. In any case break here. */
382 break;
385 return status;
389 enum nss_status
390 _nss_files_getaliasent_r (struct aliasent *result, char *buffer, size_t buflen,
391 int *errnop)
393 /* Return next entry in host file. */
394 enum nss_status status = NSS_STATUS_SUCCESS;
396 __libc_lock_lock (lock);
398 /* Be prepared that the set*ent function was not called before. */
399 if (stream == NULL)
400 status = internal_setent ();
402 if (status == NSS_STATUS_SUCCESS)
404 /* If the last use was not by the getent function we need the
405 position the stream. */
406 if (last_use != getent)
408 if (fsetpos (stream, &position) < 0)
409 status = NSS_STATUS_UNAVAIL;
410 else
411 last_use = getent;
414 if (status == NSS_STATUS_SUCCESS)
416 result->alias_local = 1;
418 /* Read lines until we get a definite result. */
420 status = get_next_alias (NULL, result, buffer, buflen, errnop);
421 while (status == NSS_STATUS_RETURN);
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;
431 __libc_lock_unlock (lock);
433 return status;
437 enum nss_status
438 _nss_files_getaliasbyname_r (const char *name, struct aliasent *result,
439 char *buffer, size_t buflen, int *errnop)
441 /* Return next entry in host file. */
442 enum nss_status status = NSS_STATUS_SUCCESS;
444 if (name == NULL)
446 __set_errno (EINVAL);
447 return NSS_STATUS_UNAVAIL;
450 __libc_lock_lock (lock);
452 /* Open the stream or rest it. */
453 status = internal_setent ();
454 last_use = getby;
456 if (status == NSS_STATUS_SUCCESS)
458 result->alias_local = 1;
460 /* Read lines until we get a definite result. */
462 status = get_next_alias (name, result, buffer, buflen, errnop);
463 while (status == NSS_STATUS_RETURN);
466 internal_endent ();
468 __libc_lock_unlock (lock);
470 return status;