Replace FSF snail mail address with URLs.
[glibc.git] / nss / nss_files / files-initgroups.c
blobc8ce0656afc3131b94930e25cd5984d123cd6b26
1 /* Initgroups handling in nss_files module.
2 Copyright (C) 2011 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <alloca.h>
20 #include <errno.h>
21 #include <grp.h>
22 #include <nss.h>
23 #include <stdio_ext.h>
24 #include <string.h>
25 #include <sys/param.h>
27 enum nss_status
28 _nss_files_initgroups_dyn (const char *user, gid_t group, long int *start,
29 long int *size, gid_t **groupsp, long int limit,
30 int *errnop)
32 FILE *stream = fopen ("/etc/group", "rce");
33 if (stream == NULL)
35 *errnop = errno;
36 return *errnop == ENOMEM ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
39 /* No other thread using this stream. */
40 __fsetlocking (stream, FSETLOCKING_BYCALLER);
42 char *line = NULL;
43 size_t linelen = 0;
44 enum nss_status status = NSS_STATUS_SUCCESS;
45 bool any = false;
47 size_t buflen = 1024;
48 void *buffer = alloca (buflen);
49 bool buffer_use_malloc = false;
51 gid_t *groups = *groupsp;
53 /* We have to iterate over the entire file. */
54 while (1)
56 fpos_t pos;
57 fgetpos (stream, &pos);
58 ssize_t n = getline (&line, &linelen, stream);
59 if (n < 0)
61 if (! feof_unlocked (stream))
62 status = ((*errnop = errno) == ENOMEM
63 ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL);
64 break;
67 struct group grp;
68 int res = _nss_files_parse_grent (line, &grp, buffer, buflen, errnop);
69 if (res == -1)
71 size_t newbuflen = 2 * buflen;
72 if (buffer_use_malloc || ! __libc_use_alloca (buflen + newbuflen))
74 void *newbuf = realloc (buffer_use_malloc ? buffer : NULL,
75 newbuflen);
76 if (newbuf == NULL)
78 *errnop = ENOMEM;
79 status = NSS_STATUS_TRYAGAIN;
80 goto out;
82 buffer = newbuf;
83 buflen = newbuflen;
84 buffer_use_malloc = true;
86 else
87 buffer = extend_alloca (buffer, buflen, newbuflen);
88 /* Reread current line, the parser has clobbered it. */
89 fsetpos (stream, &pos);
90 continue;
93 if (res > 0 && grp.gr_gid != group)
94 for (char **m = grp.gr_mem; *m != NULL; ++m)
95 if (strcmp (*m, user) == 0)
97 /* Matches user. Insert this group. */
98 if (*start == *size)
100 /* Need a bigger buffer. */
101 if (limit > 0 && *size == limit)
102 /* We reached the maximum. */
103 goto out;
105 long int newsize;
106 if (limit <= 0)
107 newsize = 2 * *size;
108 else
109 newsize = MIN (limit, 2 * *size);
111 gid_t *newgroups = realloc (groups,
112 newsize * sizeof (*groups));
113 if (newgroups == NULL)
115 *errnop = ENOMEM;
116 status = NSS_STATUS_TRYAGAIN;
117 goto out;
119 *groupsp = groups = newgroups;
120 *size = newsize;
123 groups[*start] = grp.gr_gid;
124 *start += 1;
125 any = true;
127 break;
131 out:
132 /* Free memory. */
133 if (buffer_use_malloc)
134 free (buffer);
135 free (line);
137 fclose (stream);
139 return status == NSS_STATUS_SUCCESS && !any ? NSS_STATUS_NOTFOUND : status;