1 /* Mail alias file parser in nss_files module.
2 Copyright (C) 1996-2018 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/>. */
24 #include <libc-lock.h>
29 #include <kernel-features.h>
33 /* Locks the static variables in this file. */
34 __libc_lock_define_initialized (static, lock
)
36 /* Maintenance of the stream open on the database file. For getXXent
37 operations the stream needs to be held open across calls, the other
38 getXXbyYY operations all use their own stream. */
43 static enum nss_status
44 internal_setent (FILE **stream
)
46 enum nss_status status
= NSS_STATUS_SUCCESS
;
50 *stream
= fopen ("/etc/aliases", "rce");
53 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
62 /* Thread-safe, exported version of that. */
64 _nss_files_setaliasent (void)
66 enum nss_status status
;
68 __libc_lock_lock (lock
);
70 status
= internal_setent (&stream
);
72 __libc_lock_unlock (lock
);
78 /* Close the database file. */
80 internal_endent (FILE **stream
)
90 /* Thread-safe, exported version of that. */
92 _nss_files_endaliasent (void)
94 __libc_lock_lock (lock
);
96 internal_endent (&stream
);
98 __libc_lock_unlock (lock
);
100 return NSS_STATUS_SUCCESS
;
103 /* Parsing the database file into `struct aliasent' data structures. */
104 static enum nss_status
105 get_next_alias (FILE *stream
, const char *match
, struct aliasent
*result
,
106 char *buffer
, size_t buflen
, int *errnop
)
108 enum nss_status status
= NSS_STATUS_NOTFOUND
;
111 result
->alias_members_len
= 0;
115 /* Now we are ready to process the input. We have to read a
116 line and all its continuations and construct the array of
117 string pointers. This pointers and the names itself have to
118 be placed in BUFFER. */
119 char *first_unused
= buffer
;
120 size_t room_left
= buflen
- (buflen
% __alignof__ (char *));
123 /* Check whether the buffer is large enough for even trying to
128 /* Read the first line. It must contain the alias name and
129 possibly some alias names. */
130 first_unused
[room_left
- 1] = '\xff';
131 line
= fgets_unlocked (first_unused
, room_left
, stream
);
133 /* Nothing to read. */
135 else if (first_unused
[room_left
- 1] != '\xff')
137 /* The line is too long for our buffer. */
140 status
= NSS_STATUS_TRYAGAIN
;
147 /* If we are in IGNORE mode and the first character in the
148 line is a white space we ignore the line and start
150 if (ignore
&& isspace (*first_unused
))
153 /* Terminate the line for any case. */
154 cp
= strpbrk (first_unused
, "#\n");
158 /* Skip leading blanks. */
159 while (isspace (*line
))
162 result
->alias_name
= first_unused
;
163 while (*line
!= '\0' && *line
!= ':')
164 *first_unused
++ = *line
++;
165 if (*line
== '\0' || result
->alias_name
== first_unused
)
166 /* No valid name. Ignore the line. */
169 *first_unused
++ = '\0';
170 if (room_left
< (size_t) (first_unused
- result
->alias_name
))
172 room_left
-= first_unused
- result
->alias_name
;
175 /* When we search for a specific alias we can avoid all the
176 difficult parts and compare now with the name we are
177 looking for. If it does not match we simply ignore all
178 lines until the next line containing the start of a new
180 ignore
= (match
!= NULL
181 && __strcasecmp (result
->alias_name
, match
) != 0);
185 while (isspace (*line
))
189 while (*line
!= '\0' && *line
!= ',')
190 *first_unused
++ = *line
++;
192 if (first_unused
!= cp
)
194 /* OK, we can have a regular entry or an include
198 *first_unused
++ = '\0';
200 if (strncmp (cp
, ":include:", 9) != 0)
202 if (room_left
< (first_unused
- cp
) + sizeof (char *))
204 room_left
-= (first_unused
- cp
) + sizeof (char *);
206 ++result
->alias_members_len
;
210 /* Oh well, we have to read the addressed file. */
212 char *old_line
= NULL
;
216 listfile
= fopen (&cp
[9], "rce");
217 /* If the file does not exist we simply ignore
220 && (old_line
= strdup (line
)) != NULL
)
222 while (! feof_unlocked (listfile
))
231 first_unused
[room_left
- 1] = '\xff';
232 line
= fgets_unlocked (first_unused
, room_left
,
236 if (first_unused
[room_left
- 1] != '\xff')
243 /* Parse the line. */
244 cp
= strpbrk (line
, "#\n");
250 while (isspace (*line
))
254 while (*line
!= '\0' && *line
!= ',')
255 *first_unused
++ = *line
++;
260 if (first_unused
!= cp
)
262 *first_unused
++ = '\0';
263 if (room_left
< ((first_unused
- cp
)
264 + __alignof__ (char *)))
270 room_left
-= ((first_unused
- cp
)
271 + __alignof__ (char *));
272 ++result
->alias_members_len
;
275 while (*line
!= '\0');
279 first_unused
[room_left
- 1] = '\0';
280 strncpy (first_unused
, old_line
, room_left
);
285 if (first_unused
[room_left
- 1] != '\0')
293 /* Get the next line. But we must be careful. We
294 must not read the whole line at once since it
295 might belong to the current alias. Simply read
296 the first character. If it is a white space we
297 have a continuation line. Otherwise it is the
298 beginning of a new alias and we can push back the
299 just read character. */
302 ch
= fgetc_unlocked (stream
);
303 if (ch
== EOF
|| ch
== '\n' || !isspace (ch
))
307 /* Now prepare the return. Provide string
308 pointers for the currently selected aliases. */
312 /* Adjust the pointer so it is aligned for
314 first_unused
+= __alignof__ (char *) - 1;
315 first_unused
-= ((first_unused
- (char *) 0)
316 % __alignof__ (char *));
317 result
->alias_members
= (char **) first_unused
;
319 /* Compute addresses of alias entry strings. */
320 cp
= result
->alias_name
;
321 for (cnt
= 0; cnt
< result
->alias_members_len
; ++cnt
)
323 cp
= strchr (cp
, '\0') + 1;
324 result
->alias_members
[cnt
] = cp
;
327 status
= (result
->alias_members_len
== 0
328 ? NSS_STATUS_RETURN
: NSS_STATUS_SUCCESS
);
332 /* The just read character is a white space and so
334 first_unused
[room_left
- 1] = '\xff';
335 line
= fgets_unlocked (first_unused
, room_left
, stream
);
336 if (first_unused
[room_left
- 1] != '\xff')
338 cp
= strpbrk (line
, "#\n");
345 if (status
!= NSS_STATUS_NOTFOUND
)
346 /* We read something. In any case break here. */
355 _nss_files_getaliasent_r (struct aliasent
*result
, char *buffer
, size_t buflen
,
358 /* Return next entry in host file. */
359 enum nss_status status
= NSS_STATUS_SUCCESS
;
361 __libc_lock_lock (lock
);
363 /* Be prepared that the set*ent function was not called before. */
365 status
= internal_setent (&stream
);
367 if (status
== NSS_STATUS_SUCCESS
)
369 result
->alias_local
= 1;
371 /* Read lines until we get a definite result. */
373 status
= get_next_alias (stream
, NULL
, result
, buffer
, buflen
, errnop
);
374 while (status
== NSS_STATUS_RETURN
);
377 __libc_lock_unlock (lock
);
384 _nss_files_getaliasbyname_r (const char *name
, struct aliasent
*result
,
385 char *buffer
, size_t buflen
, int *errnop
)
387 /* Return next entry in host file. */
388 enum nss_status status
= NSS_STATUS_SUCCESS
;
393 __set_errno (EINVAL
);
394 return NSS_STATUS_UNAVAIL
;
397 /* Open the stream. */
398 status
= internal_setent (&stream
);
400 if (status
== NSS_STATUS_SUCCESS
)
402 result
->alias_local
= 1;
404 /* Read lines until we get a definite result. */
406 status
= get_next_alias (stream
, name
, result
, buffer
, buflen
, errnop
);
407 while (status
== NSS_STATUS_RETURN
);
410 internal_endent (&stream
);