Replace FSF snail mail address with URLs.
[glibc.git] / nis / nss_nisplus / nisplus-rpc.c
blob4f65fb244d446a7a30b031ca527422441b73cbc8
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.
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.
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.
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/>. */
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>
29 #include "nss-nisplus.h"
31 __libc_lock_define_initialized (static, lock)
33 static nis_result *result;
34 static nis_name tablename_val;
35 static u_long tablename_len;
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)
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)
44 static int
45 _nss_nisplus_parse_rpcent (nis_result *result, struct rpcent *rpc,
46 char *buffer, size_t buflen, int *errnop)
48 char *first_unused = buffer;
49 size_t room_left = buflen;
50 unsigned int i;
51 char *line;
54 if (result == NULL)
55 return 0;
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;
63 if (NISENTRYLEN (0, 0, result) >= room_left)
65 no_more_room:
66 *errnop = ERANGE;
67 return -1;
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;
77 rpc->r_number = atoi (NISENTRYVAL (0, 2, result));
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)
85 if (strcmp (NISENTRYVAL (i, 1, result), rpc->r_name) != 0)
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;
95 *first_unused++ = '\0';
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;
108 /* For the terminating NULL pointer. */
109 room_left -= sizeof (char *);
111 i = 0;
112 while (*line != '\0')
114 /* Skip leading blanks. */
115 while (isspace (*line))
116 ++line;
118 if (*line == '\0')
119 break;
121 if (room_left < sizeof (char *))
122 goto no_more_room;
124 room_left -= sizeof (char *);
125 rpc->r_aliases[i++] = line;
127 while (*line != '\0' && *line != ' ')
128 ++line;
130 if (*line == ' ')
131 *line++ = '\0';
133 rpc->r_aliases[i] = NULL;
135 return 1;
139 static enum nss_status
140 _nss_create_tablename (int *errnop)
142 if (tablename_val == NULL)
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.";
148 char *p = malloc (sizeof (prefix) + local_dir_len);
149 if (p == NULL)
151 *errnop = errno;
152 return NSS_STATUS_TRYAGAIN;
155 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
157 tablename_len = sizeof (prefix) - 1 + local_dir_len;
159 atomic_write_barrier ();
161 tablename_val = p;
164 return NSS_STATUS_SUCCESS;
168 enum nss_status
169 _nss_nisplus_setrpcent (int stayopen)
171 enum nss_status status = NSS_STATUS_SUCCESS;
173 __libc_lock_lock (lock);
175 if (result != NULL)
177 nis_freeresult (result);
178 result = NULL;
181 if (tablename_val == NULL)
183 int err;
184 status = _nss_create_tablename (&err);
187 __libc_lock_unlock (lock);
189 return status;
192 enum nss_status
193 _nss_nisplus_endrpcent (void)
195 __libc_lock_lock (lock);
197 if (result != NULL)
199 nis_freeresult (result);
200 result = NULL;
203 __libc_lock_unlock (lock);
205 return NSS_STATUS_SUCCESS;
208 static enum nss_status
209 internal_nisplus_getrpcent_r (struct rpcent *rpc, char *buffer,
210 size_t buflen, int *errnop)
212 int parse_res;
214 /* Get the next entry until we found a correct one. */
217 nis_result *saved_res;
219 if (result == NULL)
221 saved_res = NULL;
222 if (tablename_val == NULL)
224 enum nss_status status = _nss_create_tablename (errnop);
226 if (status != NSS_STATUS_SUCCESS)
227 return status;
230 result = nis_first_entry (tablename_val);
231 if (result == NULL)
233 *errnop = errno;
234 return NSS_STATUS_TRYAGAIN;
236 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
237 return niserr2nss (result->status);
239 else
241 saved_res = result;
242 result = nis_next_entry (tablename_val, &result->cookie);
243 if (result == NULL)
245 *errnop = errno;
246 return NSS_STATUS_TRYAGAIN;
248 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
250 nis_freeresult (saved_res);
251 return niserr2nss (result->status);
255 parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer,
256 buflen, errnop);
257 if (parse_res == -1)
259 nis_freeresult (result);
260 result = saved_res;
261 *errnop = ERANGE;
262 return NSS_STATUS_TRYAGAIN;
264 else
266 if (saved_res)
267 nis_freeresult (saved_res);
270 while (!parse_res);
272 return NSS_STATUS_SUCCESS;
275 enum nss_status
276 _nss_nisplus_getrpcent_r (struct rpcent *result, char *buffer,
277 size_t buflen, int *errnop)
279 int status;
281 __libc_lock_lock (lock);
283 status = internal_nisplus_getrpcent_r (result, buffer, buflen, errnop);
285 __libc_lock_unlock (lock);
287 return status;
290 enum nss_status
291 _nss_nisplus_getrpcbyname_r (const char *name, struct rpcent *rpc,
292 char *buffer, size_t buflen, int *errnop)
294 int parse_res;
296 if (tablename_val == NULL)
298 __libc_lock_lock (lock);
300 enum nss_status status = _nss_create_tablename (errnop);
302 __libc_lock_unlock (lock);
304 if (status != NSS_STATUS_SUCCESS)
305 return status;
308 if (name == NULL)
309 return NSS_STATUS_NOTFOUND;
311 char buf[strlen (name) + 10 + tablename_len];
312 int olderr = errno;
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);
320 if (result != NULL)
322 char *bufptr = buf;
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
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);
344 nis_freeresult (result);
345 result = nis_list (bufptr, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM,
346 NULL, NULL);
349 if (result == NULL)
351 *errnop = ENOMEM;
352 return NSS_STATUS_TRYAGAIN;
355 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
357 enum nss_status status = niserr2nss (result->status);
359 __set_errno (olderr);
361 nis_freeresult (result);
362 return status;
365 parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer, buflen,
366 errnop);
368 nis_freeresult (result);
370 if (parse_res < 1)
372 if (parse_res == -1)
374 *errnop = ERANGE;
375 return NSS_STATUS_TRYAGAIN;
378 __set_errno (olderr);
379 return NSS_STATUS_NOTFOUND;
382 return NSS_STATUS_SUCCESS;
385 enum nss_status
386 _nss_nisplus_getrpcbynumber_r (const int number, struct rpcent *rpc,
387 char *buffer, size_t buflen, int *errnop)
389 if (tablename_val == NULL)
391 __libc_lock_lock (lock);
393 enum nss_status status = _nss_create_tablename (errnop);
395 __libc_lock_unlock (lock);
397 if (status != NSS_STATUS_SUCCESS)
398 return status;
401 char buf[12 + 3 * sizeof (number) + tablename_len];
402 int olderr = errno;
404 snprintf (buf, sizeof (buf), "[number=%d],%s", number, tablename_val);
406 nis_result *result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH | USE_DGRAM,
407 NULL, NULL);
409 if (result == NULL)
411 *errnop = ENOMEM;
412 return NSS_STATUS_TRYAGAIN;
415 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
417 enum nss_status status = niserr2nss (result->status);
419 __set_errno (olderr);
421 nis_freeresult (result);
422 return status;
425 int parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer, buflen,
426 errnop);
428 nis_freeresult (result);
430 if (parse_res < 1)
432 if (parse_res == -1)
434 *errnop = ERANGE;
435 return NSS_STATUS_TRYAGAIN;
437 else
439 __set_errno (olderr);
440 return NSS_STATUS_NOTFOUND;
444 return NSS_STATUS_SUCCESS;