Replace FSF snail mail address with URLs.
[glibc.git] / nis / nss_nisplus / nisplus-proto.c
blob031d555f3734803d17cfda157a9d188ccdfddfc5
1 /* Copyright (C) 1997, 1998, 2001, 2002, 2003, 2005, 2006
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 <netdb.h>
24 #include <nss.h>
25 #include <string.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_protoent (nis_result *result, struct protoent *proto,
46 char *buffer, size_t buflen, int *errnop)
48 char *first_unused = buffer;
49 size_t room_left = buflen;
50 unsigned int i;
52 if (result == NULL)
53 return 0;
55 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
56 || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
57 || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type,
58 "protocols_tbl") != 0
59 || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 3)
60 return 0;
62 /* Generate the protocols entry format and use the normal parser */
63 if (NISENTRYLEN (0, 0, result) + 1 > 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 proto->p_name = first_unused;
73 size_t len = strlen (first_unused) + 1;
74 room_left -= len;
75 first_unused += len;
78 proto->p_proto = atoi (NISENTRYVAL (0, 2, result));
80 /* XXX Rewrite at some point to allocate the array first and then
81 copy the strings. It wasteful to first concatenate the strings
82 to just split them again later. */
83 char *line = first_unused;
84 for (i = 0; i < NIS_RES_NUMOBJ (result); ++i)
86 if (strcmp (NISENTRYVAL (i, 1, result), proto->p_name) != 0)
88 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
89 goto no_more_room;
90 *first_unused++ = ' ';
91 first_unused = __stpncpy (first_unused, NISENTRYVAL (i, 1, result),
92 NISENTRYLEN (i, 1, result));
93 room_left -= NISENTRYLEN (i, 1, result) + 1;
96 *first_unused++ = '\0';
98 /* Adjust the pointer so it is aligned for
99 storing pointers. */
100 size_t adjust = ((__alignof__ (char *)
101 - (first_unused - (char *) 0) % __alignof__ (char *))
102 % __alignof__ (char *));
103 if (room_left < adjust + sizeof (char *))
104 goto no_more_room;
105 first_unused += adjust;
106 room_left -= adjust;
107 proto->p_aliases = (char **) first_unused;
109 /* For the terminating NULL pointer. */
110 room_left -= sizeof (char *);
112 i = 0;
113 while (*line != '\0')
115 /* Skip leading blanks. */
116 while (isspace (*line))
117 line++;
118 if (*line == '\0')
119 break;
121 if (room_left < sizeof (char *))
122 goto no_more_room;
124 room_left -= sizeof (char *);
125 proto->p_aliases[i++] = line;
127 while (*line != '\0' && *line != ' ')
128 ++line;
130 if (*line == ' ')
131 *line++ = '\0';
133 proto->p_aliases[i] = NULL;
135 return 1;
138 static enum nss_status
139 _nss_create_tablename (int *errnop)
141 if (tablename_val == NULL)
143 const char *local_dir = nis_local_directory ();
144 size_t local_dir_len = strlen (local_dir);
145 static const char prefix[] = "protocols.org_dir.";
147 char *p = malloc (sizeof (prefix) + local_dir_len);
148 if (p == NULL)
150 *errnop = errno;
151 return NSS_STATUS_TRYAGAIN;
154 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
156 tablename_len = sizeof (prefix) - 1 + local_dir_len;
158 atomic_write_barrier ();
160 tablename_val = p;
163 return NSS_STATUS_SUCCESS;
166 enum nss_status
167 _nss_nisplus_setprotoent (int stayopen)
169 enum nss_status status = NSS_STATUS_SUCCESS;
171 __libc_lock_lock (lock);
173 if (result != NULL)
175 nis_freeresult (result);
176 result = NULL;
179 if (tablename_val == NULL)
181 int err;
182 status = _nss_create_tablename (&err);
185 __libc_lock_unlock (lock);
187 return status;
190 enum nss_status
191 _nss_nisplus_endprotoent (void)
193 __libc_lock_lock (lock);
195 if (result != NULL)
197 nis_freeresult (result);
198 result = NULL;
201 __libc_lock_unlock (lock);
203 return NSS_STATUS_SUCCESS;
206 static enum nss_status
207 internal_nisplus_getprotoent_r (struct protoent *proto, char *buffer,
208 size_t buflen, int *errnop)
210 int parse_res;
212 /* Get the next entry until we found a correct one. */
215 nis_result *saved_res;
217 if (result == NULL)
219 saved_res = NULL;
220 if (tablename_val == NULL)
222 enum nss_status status = _nss_create_tablename (errnop);
224 if (status != NSS_STATUS_SUCCESS)
225 return status;
228 result = nis_first_entry (tablename_val);
229 if (result == NULL)
231 *errnop = errno;
232 return NSS_STATUS_TRYAGAIN;
234 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
235 return niserr2nss (result->status);
237 else
239 saved_res = result;
240 result = nis_next_entry (tablename_val, &result->cookie);
241 if (result == NULL)
243 *errnop = errno;
244 return NSS_STATUS_TRYAGAIN;
246 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
248 nis_freeresult (saved_res);
249 return niserr2nss (result->status);
253 parse_res = _nss_nisplus_parse_protoent (result, proto, buffer,
254 buflen, errnop);
255 if (parse_res == -1)
257 nis_freeresult (result);
258 result = saved_res;
259 *errnop = ERANGE;
260 return NSS_STATUS_TRYAGAIN;
262 else
264 if (saved_res)
265 nis_freeresult (saved_res);
268 while (!parse_res);
270 return NSS_STATUS_SUCCESS;
273 enum nss_status
274 _nss_nisplus_getprotoent_r (struct protoent *result, char *buffer,
275 size_t buflen, int *errnop)
277 int status;
279 __libc_lock_lock (lock);
281 status = internal_nisplus_getprotoent_r (result, buffer, buflen, errnop);
283 __libc_lock_unlock (lock);
285 return status;
288 enum nss_status
289 _nss_nisplus_getprotobyname_r (const char *name, struct protoent *proto,
290 char *buffer, size_t buflen, int *errnop)
292 int parse_res;
294 if (tablename_val == NULL)
296 __libc_lock_lock (lock);
298 enum nss_status status = _nss_create_tablename (errnop);
300 __libc_lock_unlock (lock);
302 if (status != NSS_STATUS_SUCCESS)
303 return status;
306 if (name == NULL)
307 return NSS_STATUS_NOTFOUND;
309 char buf[strlen (name) + 10 + tablename_len];
310 int olderr = errno;
312 /* Search at first in the alias list, and use the correct name
313 for the next search */
314 snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
315 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
317 if (result != NULL)
319 char *bufptr = buf;
321 /* If we did not find it, try it as original name. But if the
322 database is correct, we should find it in the first case, too */
323 if ((result->status != NIS_SUCCESS
324 && result->status != NIS_S_SUCCESS)
325 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
326 || strcmp (result->objects.objects_val->EN_data.en_type,
327 "protocols_tbl") != 0
328 || result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
329 snprintf (buf, sizeof (buf), "[cname=%s],%s", name, tablename_val);
330 else
332 /* We need to allocate a new buffer since there is no
333 guarantee the returned name has a length limit. */
334 const char *entryval = NISENTRYVAL (0, 0, result);
335 size_t buflen = strlen (entryval) + 10 + tablename_len;
336 bufptr = alloca (buflen);
337 snprintf (bufptr, buflen, "[cname=%s],%s",
338 entryval, tablename_val);
341 nis_freeresult (result);
342 result = nis_list (bufptr, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
345 if (result == NULL)
347 __set_errno (ENOMEM);
348 return NSS_STATUS_TRYAGAIN;
351 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
353 enum nss_status status = niserr2nss (result->status);
355 __set_errno (olderr);
357 nis_freeresult (result);
358 return status;
361 parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen,
362 errnop);
364 nis_freeresult (result);
366 if (parse_res < 1)
368 if (parse_res == -1)
370 *errnop = ERANGE;
371 return NSS_STATUS_TRYAGAIN;
373 else
375 __set_errno (olderr);
376 return NSS_STATUS_NOTFOUND;
380 return NSS_STATUS_SUCCESS;
383 enum nss_status
384 _nss_nisplus_getprotobynumber_r (const int number, struct protoent *proto,
385 char *buffer, size_t buflen, int *errnop)
387 if (tablename_val == NULL)
389 __libc_lock_lock (lock);
391 enum nss_status status = _nss_create_tablename (errnop);
393 __libc_lock_unlock (lock);
395 if (status != NSS_STATUS_SUCCESS)
396 return status;
399 char buf[12 + 3 * sizeof (number) + tablename_len];
400 int olderr = errno;
402 snprintf (buf, sizeof (buf), "[number=%d],%s", number, tablename_val);
404 nis_result *result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
406 if (result == NULL)
408 __set_errno (ENOMEM);
409 return NSS_STATUS_TRYAGAIN;
412 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
414 enum nss_status status = niserr2nss (result->status);
416 __set_errno (olderr);
418 nis_freeresult (result);
419 return status;
422 int parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen,
423 errnop);
425 nis_freeresult (result);
427 if (parse_res < 1)
429 if (parse_res == -1)
431 *errnop = ERANGE;
432 return NSS_STATUS_TRYAGAIN;
434 else
436 __set_errno (olderr);
437 return NSS_STATUS_NOTFOUND;
441 return NSS_STATUS_SUCCESS;