1 | /* Initgroups handling in nss_files module. |
2 | Copyright (C) 2011-2020 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 | <https://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 | #include <nss.h> |
29 | #include <nss_files.h> |
30 | |
31 | NSS_DECLARE_MODULE_FUNCTIONS (files) |
32 | |
33 | enum nss_status |
34 | _nss_files_initgroups_dyn (const char *user, gid_t group, long int *start, |
35 | long int *size, gid_t **groupsp, long int limit, |
36 | int *errnop) |
37 | { |
38 | FILE *stream = __nss_files_fopen ("/etc/group" ); |
39 | if (stream == NULL) |
40 | { |
41 | *errnop = errno; |
42 | return *errnop == ENOMEM ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL; |
43 | } |
44 | |
45 | char *line = NULL; |
46 | size_t linelen = 0; |
47 | enum nss_status status = NSS_STATUS_SUCCESS; |
48 | bool any = false; |
49 | |
50 | struct scratch_buffer tmpbuf; |
51 | scratch_buffer_init (&tmpbuf); |
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, |
71 | tmpbuf.data, tmpbuf.length, errnop); |
72 | if (res == -1) |
73 | { |
74 | if (!scratch_buffer_grow (&tmpbuf)) |
75 | { |
76 | *errnop = ENOMEM; |
77 | status = NSS_STATUS_TRYAGAIN; |
78 | goto out; |
79 | } |
80 | /* Reread current line, the parser has clobbered it. */ |
81 | fsetpos (stream, &pos); |
82 | continue; |
83 | } |
84 | |
85 | if (res > 0 && grp.gr_gid != group) |
86 | for (char **m = grp.gr_mem; *m != NULL; ++m) |
87 | if (strcmp (*m, user) == 0) |
88 | { |
89 | /* Matches user. Insert this group. */ |
90 | if (*start == *size) |
91 | { |
92 | /* Need a bigger buffer. */ |
93 | if (limit > 0 && *size == limit) |
94 | /* We reached the maximum. */ |
95 | goto out; |
96 | |
97 | long int newsize; |
98 | if (limit <= 0) |
99 | newsize = 2 * *size; |
100 | else |
101 | newsize = MIN (limit, 2 * *size); |
102 | |
103 | gid_t *newgroups = realloc (groups, |
104 | newsize * sizeof (*groups)); |
105 | if (newgroups == NULL) |
106 | { |
107 | *errnop = ENOMEM; |
108 | status = NSS_STATUS_TRYAGAIN; |
109 | goto out; |
110 | } |
111 | *groupsp = groups = newgroups; |
112 | *size = newsize; |
113 | } |
114 | |
115 | groups[*start] = grp.gr_gid; |
116 | *start += 1; |
117 | any = true; |
118 | |
119 | break; |
120 | } |
121 | } |
122 | |
123 | out: |
124 | /* Free memory. */ |
125 | scratch_buffer_free (&tmpbuf); |
126 | free (line); |
127 | |
128 | fclose (stream); |
129 | |
130 | return status == NSS_STATUS_SUCCESS && !any ? NSS_STATUS_NOTFOUND : status; |
131 | } |
132 | |