2006-03-05 Roland McGrath <roland@frob.com>
[glibc/history.git] / nis / nss_nisplus / nisplus-service.c
blob848e5f4dd812b25aec31079ac826335aaedd824e
1 /* Copyright (C) 1997-1999,2001,2002,2003,2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
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 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
40 #define NISENTRYLEN(idx,col,res) \
41 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
43 static int
44 _nss_nisplus_parse_servent (nis_result *result, struct servent *serv,
45 char *buffer, size_t buflen, int *errnop)
47 char *first_unused = buffer;
48 size_t room_left = buflen;
50 if (result == NULL)
51 return 0;
53 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
54 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
55 || strcmp (result->objects.objects_val->EN_data.en_type,
56 "services_tbl") != 0
57 || result->objects.objects_val->EN_data.en_cols.en_cols_len < 4)
58 return 0;
60 if (NISENTRYLEN (0, 0, result) >= room_left)
62 no_more_room:
63 *errnop = ERANGE;
64 return -1;
66 strncpy (first_unused, NISENTRYVAL (0, 0, result),
67 NISENTRYLEN (0, 0, result));
68 first_unused[NISENTRYLEN (0, 0, result)] = '\0';
69 serv->s_name = first_unused;
70 room_left -= (strlen (first_unused) +1);
71 first_unused += strlen (first_unused) +1;
73 if (NISENTRYLEN (0, 2, result) >= room_left)
74 goto no_more_room;
75 strncpy (first_unused, NISENTRYVAL (0, 2, result),
76 NISENTRYLEN (0, 2, result));
77 first_unused[NISENTRYLEN (0, 2, result)] = '\0';
78 serv->s_proto = first_unused;
79 room_left -= strlen (first_unused) + 1;
80 first_unused += strlen (first_unused) + 1;
82 serv->s_port = htons (atoi (NISENTRYVAL (0, 3, result)));
83 char *p = first_unused;
85 char *line = p;
86 for (unsigned int i = 0; i < result->objects.objects_len; ++i)
88 if (strcmp (NISENTRYVAL (i, 1, result), serv->s_name) != 0)
90 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
91 goto no_more_room;
92 *p++ = ' ';
93 p = __stpncpy (p, NISENTRYVAL (i, 1, result),
94 NISENTRYLEN (i, 1, result));
95 *p = '\0';
96 room_left -= (NISENTRYLEN (i, 1, result) + 1);
99 *p++ = '\0';
100 first_unused = p;
102 /* Adjust the pointer so it is aligned for
103 storing pointers. */
104 first_unused += __alignof__ (char *) - 1;
105 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
106 serv->s_aliases = (char **) first_unused;
107 if (room_left < sizeof (char *))
108 goto no_more_room;
109 room_left -= (sizeof (char *));
110 serv->s_aliases[0] = NULL;
112 unsigned int i = 0;
113 while (*line != '\0')
115 /* Skip leading blanks. */
116 while (isspace (*line))
117 ++line;
119 if (*line == '\0')
120 break;
122 if (room_left < sizeof (char *))
123 goto no_more_room;
125 room_left -= sizeof (char *);
126 serv->s_aliases[i] = line;
128 while (*line != '\0' && *line != ' ')
129 ++line;
131 if (*line == ' ')
133 *line = '\0';
134 ++line;
135 ++i;
137 else
138 serv->s_aliases[i+1] = NULL;
141 return 1;
144 static enum nss_status
145 _nss_create_tablename (int *errnop)
147 if (tablename_val == NULL)
149 const char *local_dir = nis_local_directory ();
150 size_t local_dir_len = strlen (local_dir);
151 static const char prefix[] = "services.org_dir.";
153 char *p = malloc (sizeof (prefix) + local_dir_len);
154 if (tablename_val == NULL)
156 *errnop = errno;
157 return NSS_STATUS_TRYAGAIN;
160 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
162 tablename_len = sizeof (prefix) - 1 + local_dir_len;
164 atomic_write_barrier ();
166 tablename_val = p;
169 return NSS_STATUS_SUCCESS;
173 enum nss_status
174 _nss_nisplus_setservent (int stayopen)
176 enum nss_status status = NSS_STATUS_SUCCESS;
177 int err;
179 __libc_lock_lock (lock);
181 if (result != NULL)
183 nis_freeresult (result);
184 result = NULL;
187 if (tablename_val == NULL)
188 status = _nss_create_tablename (&err);
190 __libc_lock_unlock (lock);
192 return status;
195 enum nss_status
196 _nss_nisplus_endservent (void)
198 __libc_lock_lock (lock);
200 if (result != NULL)
202 nis_freeresult (result);
203 result = NULL;
206 __libc_lock_unlock (lock);
208 return NSS_STATUS_SUCCESS;
211 static enum nss_status
212 internal_nisplus_getservent_r (struct servent *serv, char *buffer,
213 size_t buflen, int *errnop)
215 int parse_res;
217 /* Get the next entry until we found a correct one. */
220 nis_result *saved_res;
222 if (result == NULL)
224 saved_res = NULL;
225 if (tablename_val == NULL)
227 enum nss_status status = _nss_create_tablename (errnop);
229 if (status != NSS_STATUS_SUCCESS)
230 return status;
233 result = nis_first_entry (tablename_val);
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 (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
243 nis_freeresult (saved_res);
244 return niserr2nss (result->status);
248 parse_res = _nss_nisplus_parse_servent (result, serv, buffer,
249 buflen, errnop);
250 if (__builtin_expect (parse_res == -1, 0))
252 nis_freeresult (result);
253 result = saved_res;
254 *errnop = ERANGE;
255 return NSS_STATUS_TRYAGAIN;
257 else
259 if (saved_res)
260 nis_freeresult (saved_res);
263 while (!parse_res);
265 return NSS_STATUS_SUCCESS;
268 enum nss_status
269 _nss_nisplus_getservent_r (struct servent *result, char *buffer,
270 size_t buflen, int *errnop)
272 __libc_lock_lock (lock);
274 int status = internal_nisplus_getservent_r (result, buffer, buflen, errnop);
276 __libc_lock_unlock (lock);
278 return status;
281 enum nss_status
282 _nss_nisplus_getservbyname_r (const char *name, const char *protocol,
283 struct servent *serv,
284 char *buffer, size_t buflen, int *errnop)
286 if (tablename_val == NULL)
288 __libc_lock_lock (lock);
290 enum nss_status status = _nss_create_tablename (errnop);
292 __libc_lock_unlock (lock);
294 if (status != NSS_STATUS_SUCCESS)
295 return status;
298 if (name == NULL || protocol == NULL)
300 *errnop = EINVAL;
301 return NSS_STATUS_NOTFOUND;
304 size_t protocol_len = strlen (protocol);
305 char buf[strlen (name) + protocol_len + 17 + tablename_len];
306 int olderr = errno;
308 /* Search at first in the alias list, and use the correct name
309 for the next search */
310 snprintf (buf, sizeof (buf), "[name=%s,proto=%s],%s", name, protocol,
311 tablename_val);
312 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
314 if (result != NULL)
316 char *bufptr = buf;
318 /* If we did not find it, try it as original name. But if the
319 database is correct, we should find it in the first case, too */
320 if ((result->status != NIS_SUCCESS
321 && result->status != NIS_S_SUCCESS)
322 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
323 || strcmp (result->objects.objects_val->EN_data.en_type,
324 "services_tbl") != 0
325 || result->objects.objects_val->EN_data.en_cols.en_cols_len < 4)
326 snprintf (buf, sizeof (buf), "[cname=%s,proto=%s],%s", name, protocol,
327 tablename_val);
328 else
330 /* We need to allocate a new buffer since there is no
331 guarantee the returned name has a length limit. */
332 const char *entryval = NISENTRYVAL(0, 0, result);
333 size_t buflen = (strlen (entryval) + protocol_len + 17
334 + tablename_len);
335 bufptr = alloca (buflen);
336 snprintf (bufptr, buflen, "[cname=%s,proto=%s],%s",
337 entryval, protocol, tablename_val);
340 nis_freeresult (result);
341 result = nis_list (bufptr, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
344 if (result == NULL)
346 *errnop = ENOMEM;
347 return NSS_STATUS_TRYAGAIN;
350 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
352 enum nss_status status = niserr2nss (result->status);
354 __set_errno (olderr);
356 nis_freeresult (result);
357 return status;
360 int parse_res = _nss_nisplus_parse_servent (result, serv, buffer, buflen,
361 errnop);
362 nis_freeresult (result);
364 if (__builtin_expect (parse_res < 1, 0))
366 if (parse_res == -1)
368 *errnop = ERANGE;
369 return NSS_STATUS_TRYAGAIN;
371 else
373 __set_errno (olderr);
374 return NSS_STATUS_NOTFOUND;
378 return NSS_STATUS_SUCCESS;
381 enum nss_status
382 _nss_nisplus_getservbyport_r (const int number, const char *protocol,
383 struct servent *serv,
384 char *buffer, size_t buflen, int *errnop)
386 if (tablename_val == NULL)
388 __libc_lock_lock (lock);
390 enum nss_status status = _nss_create_tablename (errnop);
392 __libc_lock_unlock (lock);
394 if (status != NSS_STATUS_SUCCESS)
395 return status;
398 if (protocol == NULL)
400 *errnop = EINVAL;
401 return NSS_STATUS_NOTFOUND;
404 char buf[17 + 3 * sizeof (int) + strlen (protocol) + tablename_len];
405 int olderr = errno;
407 snprintf (buf, sizeof (buf), "[port=%d,proto=%s],%s",
408 number, protocol, tablename_val);
410 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
412 if (result == NULL)
414 *errnop = ENOMEM;
415 return NSS_STATUS_TRYAGAIN;
418 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
420 enum nss_status status = niserr2nss (result->status);
422 __set_errno (olderr);
424 nis_freeresult (result);
425 return status;
428 int parse_res = _nss_nisplus_parse_servent (result, serv, buffer, buflen,
429 errnop);
430 nis_freeresult (result);
432 if (__builtin_expect (parse_res < 1, 0))
434 if (parse_res == -1)
436 *errnop = ERANGE;
437 return NSS_STATUS_TRYAGAIN;
439 else
441 __set_errno (olderr);
442 return NSS_STATUS_NOTFOUND;
446 return NSS_STATUS_SUCCESS;