1/* Initgroups handling in nss_files module.
2 Copyright (C) 2011-2019 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 <errno.h>
20#include <grp.h>
21#include <nss.h>
22#include <stdio_ext.h>
23#include <string.h>
24#include <sys/param.h>
25#include <stdbool.h>
26#include <stdlib.h>
27#include <scratch_buffer.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 struct scratch_buffer tmpbuf;
50 scratch_buffer_init (&tmpbuf);
51
52 gid_t *groups = *groupsp;
53
54 /* We have to iterate over the entire file. */
55 while (1)
56 {
57 fpos_t pos;
58 fgetpos (stream, &pos);
59 ssize_t n = getline (&line, &linelen, stream);
60 if (n < 0)
61 {
62 if (! feof_unlocked (stream))
63 status = ((*errnop = errno) == ENOMEM
64 ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL);
65 break;
66 }
67
68 struct group grp;
69 int res = _nss_files_parse_grent (line, &grp,
70 tmpbuf.data, tmpbuf.length, errnop);
71 if (res == -1)
72 {
73 if (!scratch_buffer_grow (&tmpbuf))
74 {
75 *errnop = ENOMEM;
76 status = NSS_STATUS_TRYAGAIN;
77 goto out;
78 }
79 /* Reread current line, the parser has clobbered it. */
80 fsetpos (stream, &pos);
81 continue;
82 }
83
84 if (res > 0 && grp.gr_gid != group)
85 for (char **m = grp.gr_mem; *m != NULL; ++m)
86 if (strcmp (*m, user) == 0)
87 {
88 /* Matches user. Insert this group. */
89 if (*start == *size)
90 {
91 /* Need a bigger buffer. */
92 if (limit > 0 && *size == limit)
93 /* We reached the maximum. */
94 goto out;
95
96 long int newsize;
97 if (limit <= 0)
98 newsize = 2 * *size;
99 else
100 newsize = MIN (limit, 2 * *size);
101
102 gid_t *newgroups = realloc (groups,
103 newsize * sizeof (*groups));
104 if (newgroups == NULL)
105 {
106 *errnop = ENOMEM;
107 status = NSS_STATUS_TRYAGAIN;
108 goto out;
109 }
110 *groupsp = groups = newgroups;
111 *size = newsize;
112 }
113
114 groups[*start] = grp.gr_gid;
115 *start += 1;
116 any = true;
117
118 break;
119 }
120 }
121
122 out:
123 /* Free memory. */
124 scratch_buffer_free (&tmpbuf);
125 free (line);
126
127 fclose (stream);
128
129 return status == NSS_STATUS_SUCCESS && !any ? NSS_STATUS_NOTFOUND : status;
130}
131