1 | /* Netgroup file parser in nss_files modules. |
2 | Copyright (C) 1996-2020 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 | <https://www.gnu.org/licenses/>. */ |
19 | |
20 | #include <ctype.h> |
21 | #include <errno.h> |
22 | #include <netdb.h> |
23 | #include <stdio.h> |
24 | #include <stdio_ext.h> |
25 | #include <stdlib.h> |
26 | #include <string.h> |
27 | #include "nsswitch.h" |
28 | #include "netgroup.h" |
29 | #include <nss_files.h> |
30 | |
31 | NSS_DECLARE_MODULE_FUNCTIONS (files) |
32 | |
33 | #define DATAFILE "/etc/netgroup" |
34 | |
35 | libnss_files_hidden_proto (_nss_files_endnetgrent) |
36 | |
37 | #define EXPAND(needed) \ |
38 | do \ |
39 | { \ |
40 | size_t old_cursor = result->cursor - result->data; \ |
41 | void *old_data = result->data; \ |
42 | \ |
43 | result->data_size += 512 > 2 * needed ? 512 : 2 * needed; \ |
44 | result->data = realloc (result->data, result->data_size); \ |
45 | \ |
46 | if (result->data == NULL) \ |
47 | { \ |
48 | free (old_data); \ |
49 | status = NSS_STATUS_UNAVAIL; \ |
50 | goto the_end; \ |
51 | } \ |
52 | \ |
53 | result->cursor = result->data + old_cursor; \ |
54 | } \ |
55 | while (0) |
56 | |
57 | |
58 | enum nss_status |
59 | _nss_files_setnetgrent (const char *group, struct __netgrent *result) |
60 | { |
61 | FILE *fp; |
62 | enum nss_status status; |
63 | |
64 | if (group[0] == '\0') |
65 | return NSS_STATUS_UNAVAIL; |
66 | |
67 | /* Find the netgroups file and open it. */ |
68 | fp = __nss_files_fopen (DATAFILE); |
69 | if (fp == NULL) |
70 | status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL; |
71 | else |
72 | { |
73 | /* Read the file line by line and try to find the description |
74 | GROUP. We must take care for long lines. */ |
75 | char *line = NULL; |
76 | size_t line_len = 0; |
77 | const ssize_t group_len = strlen (group); |
78 | |
79 | status = NSS_STATUS_NOTFOUND; |
80 | result->cursor = result->data; |
81 | |
82 | while (!feof_unlocked (fp)) |
83 | { |
84 | ssize_t curlen = getline (&line, &line_len, fp); |
85 | int found; |
86 | |
87 | if (curlen < 0) |
88 | { |
89 | status = NSS_STATUS_NOTFOUND; |
90 | break; |
91 | } |
92 | |
93 | found = (curlen > group_len && strncmp (line, group, group_len) == 0 |
94 | && isspace (line[group_len])); |
95 | |
96 | /* Read the whole line (including continuation) and store it |
97 | if FOUND in nonzero. Otherwise we don't need it. */ |
98 | if (found) |
99 | { |
100 | /* Store the data from the first line. */ |
101 | EXPAND (curlen - group_len); |
102 | memcpy (result->cursor, &line[group_len + 1], |
103 | curlen - group_len); |
104 | result->cursor += (curlen - group_len) - 1; |
105 | } |
106 | |
107 | while (curlen > 1 && line[curlen - 1] == '\n' |
108 | && line[curlen - 2] == '\\') |
109 | { |
110 | /* Yes, we have a continuation line. */ |
111 | if (found) |
112 | /* Remove these characters from the stored line. */ |
113 | result->cursor -= 2; |
114 | |
115 | /* Get next line. */ |
116 | curlen = getline (&line, &line_len, fp); |
117 | if (curlen <= 0) |
118 | break; |
119 | |
120 | if (found) |
121 | { |
122 | /* Make sure we have enough room. */ |
123 | EXPAND (1 + curlen + 1); |
124 | |
125 | /* Add separator in case next line starts immediately. */ |
126 | *result->cursor++ = ' '; |
127 | |
128 | /* Copy new line. */ |
129 | memcpy (result->cursor, line, curlen + 1); |
130 | result->cursor += curlen; |
131 | } |
132 | } |
133 | |
134 | if (found) |
135 | { |
136 | /* Now we have read the line. */ |
137 | status = NSS_STATUS_SUCCESS; |
138 | result->cursor = result->data; |
139 | result->first = 1; |
140 | break; |
141 | } |
142 | } |
143 | |
144 | the_end: |
145 | /* We don't need the file and the line buffer anymore. */ |
146 | free (line); |
147 | fclose (fp); |
148 | |
149 | if (status != NSS_STATUS_SUCCESS) |
150 | _nss_files_endnetgrent (result); |
151 | } |
152 | |
153 | return status; |
154 | } |
155 | |
156 | |
157 | enum nss_status |
158 | _nss_files_endnetgrent (struct __netgrent *result) |
159 | { |
160 | /* Free allocated memory for data if some is present. */ |
161 | free (result->data); |
162 | result->data = NULL; |
163 | result->data_size = 0; |
164 | result->cursor = NULL; |
165 | return NSS_STATUS_SUCCESS; |
166 | } |
167 | libnss_files_hidden_def (_nss_files_endnetgrent) |
168 | |
169 | static char * |
170 | strip_whitespace (char *str) |
171 | { |
172 | char *cp = str; |
173 | |
174 | /* Skip leading spaces. */ |
175 | while (isspace (*cp)) |
176 | cp++; |
177 | |
178 | str = cp; |
179 | while (*cp != '\0' && ! isspace(*cp)) |
180 | cp++; |
181 | |
182 | /* Null-terminate, stripping off any trailing spaces. */ |
183 | *cp = '\0'; |
184 | |
185 | return *str == '\0' ? NULL : str; |
186 | } |
187 | |
188 | enum nss_status |
189 | _nss_netgroup_parseline (char **cursor, struct __netgrent *result, |
190 | char *buffer, size_t buflen, int *errnop) |
191 | { |
192 | enum nss_status status; |
193 | const char *host, *user, *domain; |
194 | char *cp = *cursor; |
195 | |
196 | /* Some sanity checks. */ |
197 | if (cp == NULL) |
198 | return NSS_STATUS_NOTFOUND; |
199 | |
200 | /* First skip leading spaces. */ |
201 | while (isspace (*cp)) |
202 | ++cp; |
203 | |
204 | if (*cp != '(') |
205 | { |
206 | /* We have a list of other netgroups. */ |
207 | char *name = cp; |
208 | |
209 | while (*cp != '\0' && ! isspace (*cp)) |
210 | ++cp; |
211 | |
212 | if (name != cp) |
213 | { |
214 | /* It is another netgroup name. */ |
215 | int last = *cp == '\0'; |
216 | |
217 | result->type = group_val; |
218 | result->val.group = name; |
219 | *cp = '\0'; |
220 | if (! last) |
221 | ++cp; |
222 | *cursor = cp; |
223 | result->first = 0; |
224 | |
225 | return NSS_STATUS_SUCCESS; |
226 | } |
227 | |
228 | return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN; |
229 | } |
230 | |
231 | /* Match host name. */ |
232 | host = ++cp; |
233 | while (*cp != ',') |
234 | if (*cp++ == '\0') |
235 | return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN; |
236 | |
237 | /* Match user name. */ |
238 | user = ++cp; |
239 | while (*cp != ',') |
240 | if (*cp++ == '\0') |
241 | return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN; |
242 | |
243 | /* Match domain name. */ |
244 | domain = ++cp; |
245 | while (*cp != ')') |
246 | if (*cp++ == '\0') |
247 | return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN; |
248 | ++cp; |
249 | |
250 | |
251 | /* When we got here we have found an entry. Before we can copy it |
252 | to the private buffer we have to make sure it is big enough. */ |
253 | if (cp - host > buflen) |
254 | { |
255 | *errnop = ERANGE; |
256 | status = NSS_STATUS_TRYAGAIN; |
257 | } |
258 | else |
259 | { |
260 | memcpy (buffer, host, cp - host); |
261 | result->type = triple_val; |
262 | |
263 | buffer[(user - host) - 1] = '\0'; /* Replace ',' with '\0'. */ |
264 | result->val.triple.host = strip_whitespace (buffer); |
265 | |
266 | buffer[(domain - host) - 1] = '\0'; /* Replace ',' with '\0'. */ |
267 | result->val.triple.user = strip_whitespace (buffer + (user - host)); |
268 | |
269 | buffer[(cp - host) - 1] = '\0'; /* Replace ')' with '\0'. */ |
270 | result->val.triple.domain = strip_whitespace (buffer + (domain - host)); |
271 | |
272 | status = NSS_STATUS_SUCCESS; |
273 | |
274 | /* Remember where we stopped reading. */ |
275 | *cursor = cp; |
276 | |
277 | result->first = 0; |
278 | } |
279 | |
280 | return status; |
281 | } |
282 | libnss_files_hidden_def (_nss_netgroup_parseline) |
283 | |
284 | |
285 | enum nss_status |
286 | _nss_files_getnetgrent_r (struct __netgrent *result, char *buffer, |
287 | size_t buflen, int *errnop) |
288 | { |
289 | enum nss_status status; |
290 | |
291 | status = _nss_netgroup_parseline (&result->cursor, result, buffer, buflen, |
292 | errnop); |
293 | |
294 | return status; |
295 | } |
296 | |