]> git.ipfire.org Git - thirdparty/glibc.git/blob - nss/nss_files/files-alias.c
35b0bfc5d2479ab62dfefaa52c7b640c937428a8
[thirdparty/glibc.git] / nss / nss_files / files-alias.c
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.
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 <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 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. */
39
40 static FILE *stream;
41
42
43 static enum nss_status
44 internal_setent (FILE **stream)
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 }
55 else
56 rewind (*stream);
57
58 return status;
59 }
60
61
62 /* Thread-safe, exported version of that. */
63 enum nss_status
64 _nss_files_setaliasent (void)
65 {
66 enum nss_status status;
67
68 __libc_lock_lock (lock);
69
70 status = internal_setent (&stream);
71
72 __libc_lock_unlock (lock);
73
74 return status;
75 }
76
77
78 /* Close the database file. */
79 static void
80 internal_endent (FILE **stream)
81 {
82 if (*stream != NULL)
83 {
84 fclose (*stream);
85 *stream = NULL;
86 }
87 }
88
89
90 /* Thread-safe, exported version of that. */
91 enum nss_status
92 _nss_files_endaliasent (void)
93 {
94 __libc_lock_lock (lock);
95
96 internal_endent (&stream);
97
98 __libc_lock_unlock (lock);
99
100 return NSS_STATUS_SUCCESS;
101 }
102 \f
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)
107 {
108 enum nss_status status = NSS_STATUS_NOTFOUND;
109 int ignore = 0;
110
111 result->alias_members_len = 0;
112
113 while (1)
114 {
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 *));
121 char *line;
122
123 /* Check whether the buffer is large enough for even trying to
124 read something. */
125 if (room_left < 2)
126 goto no_more_room;
127
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);
132 if (line == NULL)
133 /* Nothing to read. */
134 break;
135 else if (first_unused[room_left - 1] != '\xff')
136 {
137 /* The line is too long for our buffer. */
138 no_more_room:
139 *errnop = ERANGE;
140 status = NSS_STATUS_TRYAGAIN;
141 break;
142 }
143 else
144 {
145 char *cp;
146
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
149 reading the next. */
150 if (ignore && isspace (*first_unused))
151 continue;
152
153 /* Terminate the line for any case. */
154 cp = strpbrk (first_unused, "#\n");
155 if (cp != NULL)
156 *cp = '\0';
157
158 /* Skip leading blanks. */
159 while (isspace (*line))
160 ++line;
161
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. */
167 continue;
168
169 *first_unused++ = '\0';
170 if (room_left < (size_t) (first_unused - result->alias_name))
171 goto no_more_room;
172 room_left -= first_unused - result->alias_name;
173 ++line;
174
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
179 alias is found. */
180 ignore = (match != NULL
181 && __strcasecmp (result->alias_name, match) != 0);
182
183 while (! ignore)
184 {
185 while (isspace (*line))
186 ++line;
187
188 cp = first_unused;
189 while (*line != '\0' && *line != ',')
190 *first_unused++ = *line++;
191
192 if (first_unused != cp)
193 {
194 /* OK, we can have a regular entry or an include
195 request. */
196 if (*line != '\0')
197 ++line;
198 *first_unused++ = '\0';
199
200 if (strncmp (cp, ":include:", 9) != 0)
201 {
202 if (room_left < (first_unused - cp) + sizeof (char *))
203 goto no_more_room;
204 room_left -= (first_unused - cp) + sizeof (char *);
205
206 ++result->alias_members_len;
207 }
208 else
209 {
210 /* Oh well, we have to read the addressed file. */
211 FILE *listfile;
212 char *old_line = NULL;
213
214 first_unused = cp;
215
216 listfile = fopen (&cp[9], "rce");
217 /* If the file does not exist we simply ignore
218 the statement. */
219 if (listfile != NULL
220 && (old_line = strdup (line)) != NULL)
221 {
222 while (! feof_unlocked (listfile))
223 {
224 if (room_left < 2)
225 {
226 free (old_line);
227 fclose (listfile);
228 goto no_more_room;
229 }
230
231 first_unused[room_left - 1] = '\xff';
232 line = fgets_unlocked (first_unused, room_left,
233 listfile);
234 if (line == NULL)
235 break;
236 if (first_unused[room_left - 1] != '\xff')
237 {
238 free (old_line);
239 fclose (listfile);
240 goto no_more_room;
241 }
242
243 /* Parse the line. */
244 cp = strpbrk (line, "#\n");
245 if (cp != NULL)
246 *cp = '\0';
247
248 do
249 {
250 while (isspace (*line))
251 ++line;
252
253 cp = first_unused;
254 while (*line != '\0' && *line != ',')
255 *first_unused++ = *line++;
256
257 if (*line != '\0')
258 ++line;
259
260 if (first_unused != cp)
261 {
262 *first_unused++ = '\0';
263 if (room_left < ((first_unused - cp)
264 + __alignof__ (char *)))
265 {
266 free (old_line);
267 fclose (listfile);
268 goto no_more_room;
269 }
270 room_left -= ((first_unused - cp)
271 + __alignof__ (char *));
272 ++result->alias_members_len;
273 }
274 }
275 while (*line != '\0');
276 }
277 fclose (listfile);
278
279 first_unused[room_left - 1] = '\0';
280 strncpy (first_unused, old_line, room_left);
281
282 free (old_line);
283 line = first_unused;
284
285 if (first_unused[room_left - 1] != '\0')
286 goto no_more_room;
287 }
288 }
289 }
290
291 if (*line == '\0')
292 {
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. */
300 int ch;
301
302 ch = fgetc_unlocked (stream);
303 if (ch == EOF || ch == '\n' || !isspace (ch))
304 {
305 size_t cnt;
306
307 /* Now prepare the return. Provide string
308 pointers for the currently selected aliases. */
309 if (ch != EOF)
310 ungetc (ch, stream);
311
312 /* Adjust the pointer so it is aligned for
313 storing pointers. */
314 first_unused += __alignof__ (char *) - 1;
315 first_unused -= ((first_unused - (char *) 0)
316 % __alignof__ (char *));
317 result->alias_members = (char **) first_unused;
318
319 /* Compute addresses of alias entry strings. */
320 cp = result->alias_name;
321 for (cnt = 0; cnt < result->alias_members_len; ++cnt)
322 {
323 cp = strchr (cp, '\0') + 1;
324 result->alias_members[cnt] = cp;
325 }
326
327 status = (result->alias_members_len == 0
328 ? NSS_STATUS_RETURN : NSS_STATUS_SUCCESS);
329 break;
330 }
331
332 /* The just read character is a white space and so
333 can be ignored. */
334 first_unused[room_left - 1] = '\xff';
335 line = fgets_unlocked (first_unused, room_left, stream);
336 if (first_unused[room_left - 1] != '\xff')
337 goto no_more_room;
338 cp = strpbrk (line, "#\n");
339 if (cp != NULL)
340 *cp = '\0';
341 }
342 }
343 }
344
345 if (status != NSS_STATUS_NOTFOUND)
346 /* We read something. In any case break here. */
347 break;
348 }
349
350 return status;
351 }
352
353
354 enum nss_status
355 _nss_files_getaliasent_r (struct aliasent *result, char *buffer, size_t buflen,
356 int *errnop)
357 {
358 /* Return next entry in host file. */
359 enum nss_status status = NSS_STATUS_SUCCESS;
360
361 __libc_lock_lock (lock);
362
363 /* Be prepared that the set*ent function was not called before. */
364 if (stream == NULL)
365 status = internal_setent (&stream);
366
367 if (status == NSS_STATUS_SUCCESS)
368 {
369 result->alias_local = 1;
370
371 /* Read lines until we get a definite result. */
372 do
373 status = get_next_alias (stream, NULL, result, buffer, buflen, errnop);
374 while (status == NSS_STATUS_RETURN);
375 }
376
377 __libc_lock_unlock (lock);
378
379 return status;
380 }
381
382
383 enum nss_status
384 _nss_files_getaliasbyname_r (const char *name, struct aliasent *result,
385 char *buffer, size_t buflen, int *errnop)
386 {
387 /* Return next entry in host file. */
388 enum nss_status status = NSS_STATUS_SUCCESS;
389 FILE *stream = NULL;
390
391 if (name == NULL)
392 {
393 __set_errno (EINVAL);
394 return NSS_STATUS_UNAVAIL;
395 }
396
397 /* Open the stream. */
398 status = internal_setent (&stream);
399
400 if (status == NSS_STATUS_SUCCESS)
401 {
402 result->alias_local = 1;
403
404 /* Read lines until we get a definite result. */
405 do
406 status = get_next_alias (stream, name, result, buffer, buflen, errnop);
407 while (status == NSS_STATUS_RETURN);
408 }
409
410 internal_endent (&stream);
411
412 return status;
413 }