]>
Commit | Line | Data |
---|---|---|
a68b0d31 | 1 | /* Netgroup file parser in nss_files modules. |
ff3cacc5 | 2 | Copyright (C) 1996, 1997, 2000, 2004, 2005 Free Software Foundation, Inc. |
2303f5fd UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. | |
a68b0d31 | 5 | |
2303f5fd | 6 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
a68b0d31 | 10 | |
2303f5fd UD |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
a68b0d31 | 15 | |
41bdb6e2 AJ |
16 | You should have received a copy of the GNU Lesser General Public |
17 | License along with the GNU C Library; if not, write to the Free | |
18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
19 | 02111-1307 USA. */ | |
a68b0d31 UD |
20 | |
21 | #include <ctype.h> | |
22 | #include <errno.h> | |
a68b0d31 UD |
23 | #include <netdb.h> |
24 | #include <stdio.h> | |
ff3cacc5 | 25 | #include <stdio_ext.h> |
a68b0d31 UD |
26 | #include <stdlib.h> |
27 | #include <string.h> | |
28 | #include "nsswitch.h" | |
29 | #include "netgroup.h" | |
30 | ||
31 | #define DATAFILE "/etc/netgroup" | |
32 | ||
ff3cacc5 | 33 | libnss_files_hidden_proto (_nss_files_endnetgrent) |
a68b0d31 | 34 | |
a68b0d31 UD |
35 | #define EXPAND(needed) \ |
36 | do \ | |
37 | { \ | |
2303f5fd | 38 | size_t old_cursor = result->cursor - result->data; \ |
9be31a51 | 39 | void *old_data = result->data; \ |
a68b0d31 | 40 | \ |
2303f5fd UD |
41 | result->data_size += 512 > 2 * needed ? 512 : 2 * needed; \ |
42 | result->data = realloc (result->data, result->data_size); \ | |
a68b0d31 | 43 | \ |
2303f5fd | 44 | if (result->data == NULL) \ |
a68b0d31 | 45 | { \ |
9be31a51 | 46 | free (old_data); \ |
a68b0d31 UD |
47 | status = NSS_STATUS_UNAVAIL; \ |
48 | goto the_end; \ | |
49 | } \ | |
50 | \ | |
2303f5fd | 51 | result->cursor = result->data + old_cursor; \ |
a68b0d31 UD |
52 | } \ |
53 | while (0) | |
54 | ||
55 | ||
56 | enum nss_status | |
2303f5fd | 57 | _nss_files_setnetgrent (const char *group, struct __netgrent *result) |
a68b0d31 UD |
58 | { |
59 | FILE *fp; | |
60 | enum nss_status status; | |
61 | ||
62 | if (group[0] == '\0') | |
63 | return NSS_STATUS_UNAVAIL; | |
64 | ||
a68b0d31 UD |
65 | /* Find the netgroups file and open it. */ |
66 | fp = fopen (DATAFILE, "r"); | |
67 | if (fp == NULL) | |
68 | status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL; | |
69 | else | |
70 | { | |
71 | /* Read the file line by line and try to find the description | |
72 | GROUP. We must take care for long lines. */ | |
73 | char *line = NULL; | |
74 | size_t line_len = 0; | |
75 | const ssize_t group_len = strlen (group); | |
76 | ||
77 | status = NSS_STATUS_NOTFOUND; | |
2303f5fd | 78 | result->cursor = result->data; |
a68b0d31 | 79 | |
ff3cacc5 UD |
80 | __fsetlocking (fp, FSETLOCKING_BYCALLER); |
81 | ||
82 | while (!feof_unlocked (fp)) | |
a68b0d31 UD |
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); | |
2303f5fd UD |
102 | memcpy (result->cursor, &line[group_len + 1], |
103 | curlen - group_len); | |
104 | result->cursor += (curlen - group_len) - 1; | |
a68b0d31 UD |
105 | } |
106 | ||
107 | while (line[curlen - 1] == '\n' && line[curlen - 2] == '\\') | |
108 | { | |
109 | /* Yes, we have a continuation line. */ | |
110 | if (found) | |
111 | /* Remove these characters from the stored line. */ | |
2303f5fd | 112 | result->cursor -= 2; |
a68b0d31 | 113 | |
01c901a5 | 114 | /* Get next line. */ |
a68b0d31 UD |
115 | curlen = getline (&line, &line_len, fp); |
116 | if (curlen <= 0) | |
117 | break; | |
118 | ||
119 | if (found) | |
120 | { | |
121 | /* Make sure we have enough room. */ | |
122 | EXPAND (1 + curlen + 1); | |
123 | ||
124 | /* Add separator in case next line starts immediately. */ | |
2303f5fd | 125 | *result->cursor++ = ' '; |
a68b0d31 UD |
126 | |
127 | /* Copy new line. */ | |
2303f5fd UD |
128 | memcpy (result->cursor, line, curlen + 1); |
129 | result->cursor += curlen; | |
a68b0d31 UD |
130 | } |
131 | } | |
132 | ||
133 | if (found) | |
134 | { | |
135 | /* Now we have read the line. */ | |
136 | status = NSS_STATUS_SUCCESS; | |
2303f5fd UD |
137 | result->cursor = result->data; |
138 | result->first = 1; | |
a68b0d31 UD |
139 | break; |
140 | } | |
141 | } | |
142 | ||
143 | the_end: | |
144 | /* We don't need the file and the line buffer anymore. */ | |
145 | free (line); | |
146 | fclose (fp); | |
ff3cacc5 UD |
147 | |
148 | if (status != NSS_STATUS_SUCCESS) | |
149 | _nss_files_endnetgrent (result); | |
a68b0d31 UD |
150 | } |
151 | ||
a68b0d31 UD |
152 | return status; |
153 | } | |
154 | ||
155 | ||
34da0098 | 156 | enum nss_status |
2303f5fd | 157 | _nss_files_endnetgrent (struct __netgrent *result) |
a68b0d31 | 158 | { |
a68b0d31 | 159 | /* Free allocated memory for data if some is present. */ |
ff3cacc5 UD |
160 | free (result->data); |
161 | result->data = NULL; | |
162 | result->data_size = 0; | |
163 | result->cursor = NULL; | |
a68b0d31 UD |
164 | return NSS_STATUS_SUCCESS; |
165 | } | |
ff3cacc5 | 166 | libnss_files_hidden_def (_nss_files_endnetgrent) |
a68b0d31 | 167 | |
3452aba8 UD |
168 | static char * |
169 | strip_whitespace (char *str) | |
170 | { | |
171 | char *cp = str; | |
172 | ||
173 | /* Skip leading spaces. */ | |
174 | while (isspace (*cp)) | |
175 | cp++; | |
176 | ||
177 | str = cp; | |
178 | while (*cp != '\0' && ! isspace(*cp)) | |
179 | cp++; | |
180 | ||
181 | /* Null-terminate, stripping off any trailing spaces. */ | |
182 | *cp = '\0'; | |
183 | ||
184 | return *str == '\0' ? NULL : str; | |
185 | } | |
a68b0d31 | 186 | |
d4a089cf | 187 | enum nss_status |
a68b0d31 | 188 | _nss_netgroup_parseline (char **cursor, struct __netgrent *result, |
d71b808a | 189 | char *buffer, size_t buflen, int *errnop) |
a68b0d31 UD |
190 | { |
191 | enum nss_status status; | |
192 | const char *host, *user, *domain; | |
193 | char *cp = *cursor; | |
194 | ||
1f64ac13 UD |
195 | /* Some sanity checks. */ |
196 | if (cp == NULL) | |
9c2322bc | 197 | return NSS_STATUS_NOTFOUND; |
1f64ac13 | 198 | |
a68b0d31 UD |
199 | /* First skip leading spaces. */ |
200 | while (isspace (*cp)) | |
201 | ++cp; | |
202 | ||
203 | if (*cp != '(') | |
11336c16 UD |
204 | { |
205 | /* We have a list of other netgroups. */ | |
206 | char *name = cp; | |
207 | ||
208 | while (*cp != '\0' && ! isspace (*cp)) | |
209 | ++cp; | |
210 | ||
211 | if (name != cp) | |
212 | { | |
213 | /* It is another netgroup name. */ | |
214 | int last = *cp == '\0'; | |
215 | ||
216 | result->type = group_val; | |
217 | result->val.group = name; | |
218 | *cp = '\0'; | |
219 | if (! last) | |
220 | ++cp; | |
221 | *cursor = cp; | |
2303f5fd | 222 | result->first = 0; |
11336c16 UD |
223 | |
224 | return NSS_STATUS_SUCCESS; | |
225 | } | |
226 | ||
2303f5fd | 227 | return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN; |
11336c16 | 228 | } |
a68b0d31 UD |
229 | |
230 | /* Match host name. */ | |
231 | host = ++cp; | |
232 | while (*cp != ',') | |
233 | if (*cp++ == '\0') | |
2303f5fd | 234 | return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN; |
a68b0d31 UD |
235 | |
236 | /* Match user name. */ | |
237 | user = ++cp; | |
238 | while (*cp != ',') | |
239 | if (*cp++ == '\0') | |
2303f5fd | 240 | return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN; |
a68b0d31 UD |
241 | |
242 | /* Match domain name. */ | |
243 | domain = ++cp; | |
244 | while (*cp != ')') | |
245 | if (*cp++ == '\0') | |
2303f5fd | 246 | return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN; |
a68b0d31 UD |
247 | ++cp; |
248 | ||
249 | ||
250 | /* When we got here we have found an entry. Before we can copy it | |
251 | to the private buffer we have to make sure it is big enough. */ | |
252 | if (cp - host > buflen) | |
253 | { | |
d71b808a | 254 | *errnop = ERANGE; |
a68b0d31 UD |
255 | status = NSS_STATUS_UNAVAIL; |
256 | } | |
257 | else | |
258 | { | |
259 | memcpy (buffer, host, cp - host); | |
11336c16 | 260 | result->type = triple_val; |
a68b0d31 | 261 | |
3452aba8 UD |
262 | buffer[(user - host) - 1] = '\0'; /* Replace ',' with '\0'. */ |
263 | result->val.triple.host = strip_whitespace (buffer); | |
a68b0d31 | 264 | |
3452aba8 UD |
265 | buffer[(domain - host) - 1] = '\0'; /* Replace ',' with '\0'. */ |
266 | result->val.triple.user = strip_whitespace (buffer + (user - host)); | |
a68b0d31 | 267 | |
3452aba8 UD |
268 | buffer[(cp - host) - 1] = '\0'; /* Replace ')' with '\0'. */ |
269 | result->val.triple.domain = strip_whitespace (buffer + (domain - host)); | |
a68b0d31 UD |
270 | |
271 | status = NSS_STATUS_SUCCESS; | |
272 | ||
6d52618b | 273 | /* Remember where we stopped reading. */ |
a68b0d31 UD |
274 | *cursor = cp; |
275 | ||
2303f5fd | 276 | result->first = 0; |
a68b0d31 UD |
277 | } |
278 | ||
279 | return status; | |
280 | } | |
8cf26632 | 281 | libnss_files_hidden_def (_nss_netgroup_parseline) |
a68b0d31 UD |
282 | |
283 | ||
284 | enum nss_status | |
d71b808a UD |
285 | _nss_files_getnetgrent_r (struct __netgrent *result, char *buffer, |
286 | size_t buflen, int *errnop) | |
a68b0d31 UD |
287 | { |
288 | enum nss_status status; | |
289 | ||
d71b808a UD |
290 | status = _nss_netgroup_parseline (&result->cursor, result, buffer, buflen, |
291 | errnop); | |
a68b0d31 UD |
292 | |
293 | return status; | |
294 | } |