]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_nisplus/nisplus-rpc.c
Add script to update copyright notices and reformat some to facilitate its use.
[thirdparty/glibc.git] / nis / nss_nisplus / nisplus-rpc.c
1 /* Copyright (C) 1997, 1998, 2001, 2002, 2003, 2005, 2006, 2007
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
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 <http://www.gnu.org/licenses/>. */
19
20 #include <atomic.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <nss.h>
24 #include <string.h>
25 #include <rpc/netdb.h>
26 #include <rpcsvc/nis.h>
27 #include <bits/libc-lock.h>
28
29 #include "nss-nisplus.h"
30
31 __libc_lock_define_initialized (static, lock)
32
33 static nis_result *result;
34 static nis_name tablename_val;
35 static u_long tablename_len;
36
37 #define NISENTRYVAL(idx, col, res) \
38 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
39
40 #define NISENTRYLEN(idx, col, res) \
41 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
42
43
44 static int
45 _nss_nisplus_parse_rpcent (nis_result *result, struct rpcent *rpc,
46 char *buffer, size_t buflen, int *errnop)
47 {
48 char *first_unused = buffer;
49 size_t room_left = buflen;
50 unsigned int i;
51 char *line;
52
53
54 if (result == NULL)
55 return 0;
56
57 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
58 || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
59 || strcmp (NIS_RES_OBJECT (result)[0].EN_data.en_type, "rpc_tbl") != 0
60 || NIS_RES_OBJECT (result)[0].EN_data.en_cols.en_cols_len < 3)
61 return 0;
62
63 if (NISENTRYLEN (0, 0, result) >= room_left)
64 {
65 no_more_room:
66 *errnop = ERANGE;
67 return -1;
68 }
69 strncpy (first_unused, NISENTRYVAL (0, 0, result),
70 NISENTRYLEN (0, 0, result));
71 first_unused[NISENTRYLEN (0, 0, result)] = '\0';
72 rpc->r_name = first_unused;
73 size_t len = strlen (first_unused) + 1;
74 room_left -= len;
75 first_unused += len;
76
77 rpc->r_number = atoi (NISENTRYVAL (0, 2, result));
78
79 /* XXX Rewrite at some point to allocate the array first and then
80 copy the strings. It wasteful to first concatenate the strings
81 to just split them again later. */
82 line = first_unused;
83 for (i = 0; i < NIS_RES_NUMOBJ (result); ++i)
84 {
85 if (strcmp (NISENTRYVAL (i, 1, result), rpc->r_name) != 0)
86 {
87 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
88 goto no_more_room;
89 *first_unused++ = ' ';
90 first_unused = __stpncpy (first_unused, NISENTRYVAL (i, 1, result),
91 NISENTRYLEN (i, 1, result));
92 room_left -= NISENTRYLEN (i, 1, result) + 1;
93 }
94 }
95 *first_unused++ = '\0';
96
97 /* Adjust the pointer so it is aligned for
98 storing pointers. */
99 size_t adjust = ((__alignof__ (char *)
100 - (first_unused - (char *) 0) % __alignof__ (char *))
101 % __alignof__ (char *));
102 if (room_left < adjust + sizeof (char *))
103 goto no_more_room;
104 first_unused += adjust;
105 room_left -= adjust;
106 rpc->r_aliases = (char **) first_unused;
107
108 /* For the terminating NULL pointer. */
109 room_left -= sizeof (char *);
110
111 i = 0;
112 while (*line != '\0')
113 {
114 /* Skip leading blanks. */
115 while (isspace (*line))
116 ++line;
117
118 if (*line == '\0')
119 break;
120
121 if (room_left < sizeof (char *))
122 goto no_more_room;
123
124 room_left -= sizeof (char *);
125 rpc->r_aliases[i++] = line;
126
127 while (*line != '\0' && *line != ' ')
128 ++line;
129
130 if (*line == ' ')
131 *line++ = '\0';
132 }
133 rpc->r_aliases[i] = NULL;
134
135 return 1;
136 }
137
138
139 static enum nss_status
140 _nss_create_tablename (int *errnop)
141 {
142 if (tablename_val == NULL)
143 {
144 const char *local_dir = nis_local_directory ();
145 size_t local_dir_len = strlen (local_dir);
146 static const char prefix[] = "rpc.org_dir.";
147
148 char *p = malloc (sizeof (prefix) + local_dir_len);
149 if (p == NULL)
150 {
151 *errnop = errno;
152 return NSS_STATUS_TRYAGAIN;
153 }
154
155 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
156
157 tablename_len = sizeof (prefix) - 1 + local_dir_len;
158
159 atomic_write_barrier ();
160
161 tablename_val = p;
162 }
163
164 return NSS_STATUS_SUCCESS;
165 }
166
167
168 enum nss_status
169 _nss_nisplus_setrpcent (int stayopen)
170 {
171 enum nss_status status = NSS_STATUS_SUCCESS;
172
173 __libc_lock_lock (lock);
174
175 if (result != NULL)
176 {
177 nis_freeresult (result);
178 result = NULL;
179 }
180
181 if (tablename_val == NULL)
182 {
183 int err;
184 status = _nss_create_tablename (&err);
185 }
186
187 __libc_lock_unlock (lock);
188
189 return status;
190 }
191
192 enum nss_status
193 _nss_nisplus_endrpcent (void)
194 {
195 __libc_lock_lock (lock);
196
197 if (result != NULL)
198 {
199 nis_freeresult (result);
200 result = NULL;
201 }
202
203 __libc_lock_unlock (lock);
204
205 return NSS_STATUS_SUCCESS;
206 }
207
208 static enum nss_status
209 internal_nisplus_getrpcent_r (struct rpcent *rpc, char *buffer,
210 size_t buflen, int *errnop)
211 {
212 int parse_res;
213
214 /* Get the next entry until we found a correct one. */
215 do
216 {
217 nis_result *saved_res;
218
219 if (result == NULL)
220 {
221 saved_res = NULL;
222 if (tablename_val == NULL)
223 {
224 enum nss_status status = _nss_create_tablename (errnop);
225
226 if (status != NSS_STATUS_SUCCESS)
227 return status;
228 }
229
230 result = nis_first_entry (tablename_val);
231 if (result == NULL)
232 {
233 *errnop = errno;
234 return NSS_STATUS_TRYAGAIN;
235 }
236 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
237 return niserr2nss (result->status);
238 }
239 else
240 {
241 saved_res = result;
242 result = nis_next_entry (tablename_val, &result->cookie);
243 if (result == NULL)
244 {
245 *errnop = errno;
246 return NSS_STATUS_TRYAGAIN;
247 }
248 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
249 {
250 nis_freeresult (saved_res);
251 return niserr2nss (result->status);
252 }
253 }
254
255 parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer,
256 buflen, errnop);
257 if (parse_res == -1)
258 {
259 nis_freeresult (result);
260 result = saved_res;
261 *errnop = ERANGE;
262 return NSS_STATUS_TRYAGAIN;
263 }
264 else
265 {
266 if (saved_res)
267 nis_freeresult (saved_res);
268 }
269 }
270 while (!parse_res);
271
272 return NSS_STATUS_SUCCESS;
273 }
274
275 enum nss_status
276 _nss_nisplus_getrpcent_r (struct rpcent *result, char *buffer,
277 size_t buflen, int *errnop)
278 {
279 int status;
280
281 __libc_lock_lock (lock);
282
283 status = internal_nisplus_getrpcent_r (result, buffer, buflen, errnop);
284
285 __libc_lock_unlock (lock);
286
287 return status;
288 }
289
290 enum nss_status
291 _nss_nisplus_getrpcbyname_r (const char *name, struct rpcent *rpc,
292 char *buffer, size_t buflen, int *errnop)
293 {
294 int parse_res;
295
296 if (tablename_val == NULL)
297 {
298 __libc_lock_lock (lock);
299
300 enum nss_status status = _nss_create_tablename (errnop);
301
302 __libc_lock_unlock (lock);
303
304 if (status != NSS_STATUS_SUCCESS)
305 return status;
306 }
307
308 if (name == NULL)
309 return NSS_STATUS_NOTFOUND;
310
311 char buf[strlen (name) + 10 + tablename_len];
312 int olderr = errno;
313
314 /* Search at first in the alias list, and use the correct name
315 for the next search */
316 snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
317 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM,
318 NULL, NULL);
319
320 if (result != NULL)
321 {
322 char *bufptr = buf;
323
324 /* If we did not find it, try it as original name. But if the
325 database is correct, we should find it in the first case, too */
326 if ((result->status != NIS_SUCCESS
327 && result->status != NIS_S_SUCCESS)
328 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
329 || strcmp (result->objects.objects_val->EN_data.en_type,
330 "rpc_tbl") != 0
331 || result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
332 snprintf (buf, sizeof (buf), "[cname=%s],%s", name, tablename_val);
333 else
334 {
335 /* We need to allocate a new buffer since there is no
336 guarantee the returned name has a length limit. */
337 const char *entryval = NISENTRYVAL (0, 0, result);
338 size_t buflen = strlen (entryval) + 10 + tablename_len;
339 bufptr = alloca (buflen);
340 snprintf (bufptr, buflen, "[cname=%s],%s",
341 entryval, tablename_val);
342 }
343
344 nis_freeresult (result);
345 result = nis_list (bufptr, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM,
346 NULL, NULL);
347 }
348
349 if (result == NULL)
350 {
351 *errnop = ENOMEM;
352 return NSS_STATUS_TRYAGAIN;
353 }
354
355 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
356 {
357 enum nss_status status = niserr2nss (result->status);
358
359 __set_errno (olderr);
360
361 nis_freeresult (result);
362 return status;
363 }
364
365 parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer, buflen,
366 errnop);
367
368 nis_freeresult (result);
369
370 if (parse_res < 1)
371 {
372 if (parse_res == -1)
373 {
374 *errnop = ERANGE;
375 return NSS_STATUS_TRYAGAIN;
376 }
377
378 __set_errno (olderr);
379 return NSS_STATUS_NOTFOUND;
380 }
381
382 return NSS_STATUS_SUCCESS;
383 }
384
385 enum nss_status
386 _nss_nisplus_getrpcbynumber_r (const int number, struct rpcent *rpc,
387 char *buffer, size_t buflen, int *errnop)
388 {
389 if (tablename_val == NULL)
390 {
391 __libc_lock_lock (lock);
392
393 enum nss_status status = _nss_create_tablename (errnop);
394
395 __libc_lock_unlock (lock);
396
397 if (status != NSS_STATUS_SUCCESS)
398 return status;
399 }
400
401 char buf[12 + 3 * sizeof (number) + tablename_len];
402 int olderr = errno;
403
404 snprintf (buf, sizeof (buf), "[number=%d],%s", number, tablename_val);
405
406 nis_result *result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH | USE_DGRAM,
407 NULL, NULL);
408
409 if (result == NULL)
410 {
411 *errnop = ENOMEM;
412 return NSS_STATUS_TRYAGAIN;
413 }
414
415 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
416 {
417 enum nss_status status = niserr2nss (result->status);
418
419 __set_errno (olderr);
420
421 nis_freeresult (result);
422 return status;
423 }
424
425 int parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer, buflen,
426 errnop);
427
428 nis_freeresult (result);
429
430 if (parse_res < 1)
431 {
432 if (parse_res == -1)
433 {
434 *errnop = ERANGE;
435 return NSS_STATUS_TRYAGAIN;
436 }
437 else
438 {
439 __set_errno (olderr);
440 return NSS_STATUS_NOTFOUND;
441 }
442 }
443
444 return NSS_STATUS_SUCCESS;
445 }