1/* Initgroups handling in nss_files module.
2 Copyright (C) 2011-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
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>
26#include <stdbool.h>
27#include <stdlib.h>
28
29enum nss_status
30_nss_files_initgroups_dyn (const char *user, gid_t group, long int *start,
31 long int *size, gid_t **groupsp, long int limit,
32 int *errnop)
33{
34 FILE *stream = fopen ("/etc/group", "rce");
35 if (stream == NULL)
36 {
37 *errnop = errno;
38 return *errnop == ENOMEM ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
39 }
40
41 /* No other thread using this stream. */
42 __fsetlocking (stream, FSETLOCKING_BYCALLER);
43
44 char *line = NULL;
45 size_t linelen = 0;
46 enum nss_status status = NSS_STATUS_SUCCESS;
47 bool any = false;
48
49 size_t buflen = 1024;
50 void *buffer = alloca (buflen);
51 bool buffer_use_malloc = false;
52
53 gid_t *groups = *groupsp;
54
55 /* We have to iterate over the entire file. */
56 while (1)
57 {
58 fpos_t pos;
59 fgetpos (stream, &pos);
60 ssize_t n = getline (&line, &linelen, stream);
61 if (n < 0)
62 {
63 if (! feof_unlocked (stream))
64 status = ((*errnop = errno) == ENOMEM
65 ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL);
66 break;
67 }
68
69 struct group grp;
70 int res = _nss_files_parse_grent (line, &grp, buffer, buflen, errnop);
71 if (res == -1)
72 {
73 size_t newbuflen = 2 * buflen;
74 if (buffer_use_malloc || ! __libc_use_alloca (buflen + newbuflen))
75 {
76 void *newbuf = realloc (buffer_use_malloc ? buffer : NULL,
77 newbuflen);
78 if (newbuf == NULL)
79 {
80 *errnop = ENOMEM;
81 status = NSS_STATUS_TRYAGAIN;
82 goto out;
83 }
84 buffer = newbuf;
85 buflen = newbuflen;
86 buffer_use_malloc = true;
87 }
88 else
89 buffer = extend_alloca (buffer, buflen, newbuflen);
90 /* Reread current line, the parser has clobbered it. */
91 fsetpos (stream, &pos);
92 continue;
93 }
94
95 if (res > 0 && grp.gr_gid != group)
96 for (char **m = grp.gr_mem; *m != NULL; ++m)
97 if (strcmp (*m, user) == 0)
98 {
99 /* Matches user. Insert this group. */
100 if (*start == *size)
101 {
102 /* Need a bigger buffer. */
103 if (limit > 0 && *size == limit)
104 /* We reached the maximum. */
105 goto out;
106
107 long int newsize;
108 if (limit <= 0)
109 newsize = 2 * *size;
110 else
111 newsize = MIN (limit, 2 * *size);
112
113 gid_t *newgroups = realloc (groups,
114 newsize * sizeof (*groups));
115 if (newgroups == NULL)
116 {
117 *errnop = ENOMEM;
118 status = NSS_STATUS_TRYAGAIN;
119 goto out;
120 }
121 *groupsp = groups = newgroups;
122 *size = newsize;
123 }
124
125 groups[*start] = grp.gr_gid;
126 *start += 1;
127 any = true;
128
129 break;
130 }
131 }
132
133 out:
134 /* Free memory. */
135 if (buffer_use_malloc)
136 free (buffer);
137 free (line);
138
139 fclose (stream);
140
141 return status == NSS_STATUS_SUCCESS && !any ? NSS_STATUS_NOTFOUND : status;
142}
143