.
[glibc/pb-stable.git] / nis / nss_nisplus / nisplus-service.c
blob607ce80b018e3eefc7da069b362509c1420c8694
1 /* Copyright (C) 1997, 1998, 1999, 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@suse.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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <atomic.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <netdb.h>
25 #include <nss.h>
26 #include <string.h>
27 #include <rpcsvc/nis.h>
28 #include <bits/libc-lock.h>
30 #include "nss-nisplus.h"
32 __libc_lock_define_initialized (static, lock);
34 static nis_result *result;
35 static nis_name tablename_val;
36 static u_long tablename_len;
38 #define NISENTRYVAL(idx, col, res) \
39 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
41 #define NISENTRYLEN(idx, col, res) \
42 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
45 static int
46 _nss_nisplus_parse_servent (nis_result *result, struct servent *serv,
47 char *buffer, size_t buflen, int *errnop)
49 char *first_unused = buffer;
50 size_t room_left = buflen;
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, "services_tbl") != 0
58 || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 4)
59 return 0;
61 if (NISENTRYLEN (0, 0, result) >= room_left)
63 no_more_room:
64 *errnop = ERANGE;
65 return -1;
67 strncpy (first_unused, NISENTRYVAL (0, 0, result),
68 NISENTRYLEN (0, 0, result));
69 first_unused[NISENTRYLEN (0, 0, result)] = '\0';
70 serv->s_name = first_unused;
71 size_t len = strlen (first_unused) + 1;
72 room_left -= len;
73 first_unused += len;
75 if (NISENTRYLEN (0, 2, result) >= room_left)
76 goto no_more_room;
77 strncpy (first_unused, NISENTRYVAL (0, 2, result),
78 NISENTRYLEN (0, 2, result));
79 first_unused[NISENTRYLEN (0, 2, result)] = '\0';
80 serv->s_proto = first_unused;
81 len = strlen (first_unused) + 1;
82 room_left -= len;
83 first_unused += len;
85 serv->s_port = htons (atoi (NISENTRYVAL (0, 3, result)));
87 /* XXX Rewrite at some point to allocate the array first and then
88 copy the strings. It wasteful to first concatenate the strings
89 to just split them again later. */
90 char *line = first_unused;
91 for (unsigned int i = 0; i < NIS_RES_NUMOBJ (result); ++i)
93 if (strcmp (NISENTRYVAL (i, 1, result), serv->s_name) != 0)
95 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
96 goto no_more_room;
97 *first_unused++ = ' ';
98 first_unused = __stpncpy (first_unused, NISENTRYVAL (i, 1, result),
99 NISENTRYLEN (i, 1, result));
100 room_left -= NISENTRYLEN (i, 1, result) + 1;
103 *first_unused++ = '\0';
105 /* Adjust the pointer so it is aligned for
106 storing pointers. */
107 size_t adjust = ((__alignof__ (char *)
108 - (first_unused - (char *) 0) % __alignof__ (char *))
109 % __alignof__ (char *));
110 if (room_left < adjust + sizeof (char *))
111 goto no_more_room;
112 first_unused += adjust;
113 room_left -= adjust;
114 serv->s_aliases = (char **) first_unused;
116 /* For the terminating NULL pointer. */
117 room_left -= (sizeof (char *));
119 unsigned int i = 0;
120 while (*line != '\0')
122 /* Skip leading blanks. */
123 while (isspace (*line))
124 ++line;
126 if (*line == '\0')
127 break;
129 if (room_left < sizeof (char *))
130 goto no_more_room;
132 room_left -= sizeof (char *);
133 serv->s_aliases[i++] = line;
135 while (*line != '\0' && *line != ' ')
136 ++line;
138 if (*line == ' ')
139 *line++ = '\0';
141 serv->s_aliases[i] = NULL;
143 return 1;
147 static enum nss_status
148 _nss_create_tablename (int *errnop)
150 if (tablename_val == NULL)
152 const char *local_dir = nis_local_directory ();
153 size_t local_dir_len = strlen (local_dir);
154 static const char prefix[] = "services.org_dir.";
156 char *p = malloc (sizeof (prefix) + local_dir_len);
157 if (p == NULL)
159 *errnop = errno;
160 return NSS_STATUS_TRYAGAIN;
163 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
165 tablename_len = sizeof (prefix) - 1 + local_dir_len;
167 atomic_write_barrier ();
169 tablename_val = p;
172 return NSS_STATUS_SUCCESS;
176 enum nss_status
177 _nss_nisplus_setservent (int stayopen)
179 enum nss_status status = NSS_STATUS_SUCCESS;
180 int err;
182 __libc_lock_lock (lock);
184 if (result != NULL)
186 nis_freeresult (result);
187 result = NULL;
190 if (tablename_val == NULL)
191 status = _nss_create_tablename (&err);
193 __libc_lock_unlock (lock);
195 return status;
198 enum nss_status
199 _nss_nisplus_endservent (void)
201 __libc_lock_lock (lock);
203 if (result != NULL)
205 nis_freeresult (result);
206 result = NULL;
209 __libc_lock_unlock (lock);
211 return NSS_STATUS_SUCCESS;
214 static enum nss_status
215 internal_nisplus_getservent_r (struct servent *serv, char *buffer,
216 size_t buflen, int *errnop)
218 int parse_res;
220 /* Get the next entry until we found a correct one. */
223 nis_result *saved_res;
225 if (result == NULL)
227 saved_res = NULL;
228 if (tablename_val == NULL)
230 enum nss_status status = _nss_create_tablename (errnop);
232 if (status != NSS_STATUS_SUCCESS)
233 return status;
236 result = nis_first_entry (tablename_val);
237 if (result == NULL)
239 *errnop = errno;
240 return NSS_STATUS_TRYAGAIN;
242 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
243 return niserr2nss (result->status);
245 else
247 saved_res = result;
248 result = nis_next_entry (tablename_val, &result->cookie);
249 if (result == NULL)
251 *errnop = errno;
252 return NSS_STATUS_TRYAGAIN;
254 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
256 nis_freeresult (saved_res);
257 return niserr2nss (result->status);
261 parse_res = _nss_nisplus_parse_servent (result, serv, buffer,
262 buflen, errnop);
263 if (__builtin_expect (parse_res == -1, 0))
265 nis_freeresult (result);
266 result = saved_res;
267 *errnop = ERANGE;
268 return NSS_STATUS_TRYAGAIN;
270 else
272 if (saved_res)
273 nis_freeresult (saved_res);
276 while (!parse_res);
278 return NSS_STATUS_SUCCESS;
281 enum nss_status
282 _nss_nisplus_getservent_r (struct servent *result, char *buffer,
283 size_t buflen, int *errnop)
285 __libc_lock_lock (lock);
287 int status = internal_nisplus_getservent_r (result, buffer, buflen, errnop);
289 __libc_lock_unlock (lock);
291 return status;
294 enum nss_status
295 _nss_nisplus_getservbyname_r (const char *name, const char *protocol,
296 struct servent *serv,
297 char *buffer, size_t buflen, int *errnop)
299 if (tablename_val == NULL)
301 __libc_lock_lock (lock);
303 enum nss_status status = _nss_create_tablename (errnop);
305 __libc_lock_unlock (lock);
307 if (status != NSS_STATUS_SUCCESS)
308 return status;
311 if (name == NULL || protocol == NULL)
313 *errnop = EINVAL;
314 return NSS_STATUS_NOTFOUND;
317 size_t protocol_len = strlen (protocol);
318 char buf[strlen (name) + protocol_len + 17 + tablename_len];
319 int olderr = errno;
321 /* Search at first in the alias list, and use the correct name
322 for the next search */
323 snprintf (buf, sizeof (buf), "[name=%s,proto=%s],%s", name, protocol,
324 tablename_val);
325 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM,
326 NULL, NULL);
328 if (result != NULL)
330 char *bufptr = buf;
332 /* If we did not find it, try it as original name. But if the
333 database is correct, we should find it in the first case, too */
334 if ((result->status != NIS_SUCCESS
335 && result->status != NIS_S_SUCCESS)
336 || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
337 || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type,
338 "services_tbl") != 0
339 || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 4)
340 snprintf (buf, sizeof (buf), "[cname=%s,proto=%s],%s", name, protocol,
341 tablename_val);
342 else
344 /* We need to allocate a new buffer since there is no
345 guarantee the returned name has a length limit. */
346 const char *entryval = NISENTRYVAL(0, 0, result);
347 size_t buflen = (strlen (entryval) + protocol_len + 17
348 + tablename_len);
349 bufptr = alloca (buflen);
350 snprintf (bufptr, buflen, "[cname=%s,proto=%s],%s",
351 entryval, protocol, tablename_val);
354 nis_freeresult (result);
355 result = nis_list (bufptr, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM,
356 NULL, NULL);
359 if (result == NULL)
361 *errnop = ENOMEM;
362 return NSS_STATUS_TRYAGAIN;
365 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
367 enum nss_status status = niserr2nss (result->status);
369 __set_errno (olderr);
371 nis_freeresult (result);
372 return status;
375 int parse_res = _nss_nisplus_parse_servent (result, serv, buffer, buflen,
376 errnop);
377 nis_freeresult (result);
379 if (__builtin_expect (parse_res < 1, 0))
381 if (parse_res == -1)
383 *errnop = ERANGE;
384 return NSS_STATUS_TRYAGAIN;
386 else
388 __set_errno (olderr);
389 return NSS_STATUS_NOTFOUND;
393 return NSS_STATUS_SUCCESS;
396 enum nss_status
397 _nss_nisplus_getservbyport_r (const int number, const char *protocol,
398 struct servent *serv,
399 char *buffer, size_t buflen, int *errnop)
401 if (tablename_val == NULL)
403 __libc_lock_lock (lock);
405 enum nss_status status = _nss_create_tablename (errnop);
407 __libc_lock_unlock (lock);
409 if (status != NSS_STATUS_SUCCESS)
410 return status;
413 if (protocol == NULL)
415 *errnop = EINVAL;
416 return NSS_STATUS_NOTFOUND;
419 char buf[17 + 3 * sizeof (int) + strlen (protocol) + tablename_len];
420 int olderr = errno;
422 snprintf (buf, sizeof (buf), "[port=%d,proto=%s],%s",
423 number, protocol, tablename_val);
425 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM,
426 NULL, NULL);
428 if (result == NULL)
430 *errnop = ENOMEM;
431 return NSS_STATUS_TRYAGAIN;
434 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
436 enum nss_status status = niserr2nss (result->status);
438 __set_errno (olderr);
440 nis_freeresult (result);
441 return status;
444 int parse_res = _nss_nisplus_parse_servent (result, serv, buffer, buflen,
445 errnop);
446 nis_freeresult (result);
448 if (__builtin_expect (parse_res < 1, 0))
450 if (parse_res == -1)
452 *errnop = ERANGE;
453 return NSS_STATUS_TRYAGAIN;
455 else
457 __set_errno (olderr);
458 return NSS_STATUS_NOTFOUND;
462 return NSS_STATUS_SUCCESS;