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