1 /* Copyright (C) 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/nis.h>
28 #include "nss-nisplus.h"
30 __libc_lock_define_initialized (static, lock)
32 static nis_result *result = NULL;
33 static nis_name tablename_val = NULL;
34 static u_long tablename_len = 0;
36 #define NISENTRYVAL(idx,col,res) \
37 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
39 #define NISENTRYLEN(idx,col,res) \
40 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
43 _nss_nisplus_parse_protoent (nis_result * result, struct protoent *proto,
44 char *buffer, size_t buflen, int *errnop)
46 char *first_unused = buffer;
47 size_t room_left = buflen;
54 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
55 || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
56 || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type,
58 || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 3)
61 /* Generate the protocols entry format and use the normal parser */
62 if (NISENTRYLEN (0, 0, result) + 1 > room_left)
68 strncpy (first_unused, NISENTRYVAL (0, 0, result),
69 NISENTRYLEN (0, 0, result));
70 first_unused[NISENTRYLEN (0, 0, result)] = '\0';
71 proto->p_name = first_unused;
72 room_left -= (strlen (first_unused) +1);
73 first_unused += strlen (first_unused) +1;
76 if (NISENTRYLEN (0, 2, result) + 1 > room_left)
78 proto->p_proto = atoi (NISENTRYVAL (0, 2, result));
82 for (i = 0; i < result->objects.objects_len; i++)
84 if (strcmp (NISENTRYVAL (i, 1, result), proto->p_name) != 0)
86 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
89 p = __stpncpy (p, NISENTRYVAL (i, 1, result),
90 NISENTRYLEN (i, 1, result));
92 room_left -= (NISENTRYLEN (i, 1, result) + 1);
98 /* Adjust the pointer so it is aligned for
100 first_unused += __alignof__ (char *) - 1;
101 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
102 proto->p_aliases = (char **) first_unused;
103 if (room_left < sizeof (char *))
105 room_left -= sizeof (char *);
106 proto->p_aliases[0] = NULL;
109 while (*line != '\0')
111 /* Skip leading blanks. */
112 while (isspace (*line))
117 if (room_left < sizeof (char *))
120 room_left -= sizeof (char *);
121 proto->p_aliases[i] = line;
123 while (*line != '\0' && *line != ' ')
133 proto->p_aliases[i+1] = NULL;
139 static enum nss_status
140 _nss_create_tablename (int *errnop)
142 if (tablename_val == NULL)
144 char buf [40 + strlen (nis_local_directory ())];
147 p = __stpcpy (buf, "protocols.org_dir.");
148 p = __stpcpy (p, nis_local_directory ());
149 tablename_val = __strdup (buf);
150 if (tablename_val == NULL)
153 return NSS_STATUS_TRYAGAIN;
155 tablename_len = strlen (tablename_val);
157 return NSS_STATUS_SUCCESS;
161 _nss_nisplus_setprotoent (void)
163 enum nss_status status = NSS_STATUS_SUCCESS;
165 __libc_lock_lock (lock);
168 nis_freeresult (result);
171 if (tablename_val == NULL)
174 status = _nss_create_tablename (&err);
177 __libc_lock_unlock (lock);
183 _nss_nisplus_endprotoent (void)
185 __libc_lock_lock (lock);
188 nis_freeresult (result);
191 __libc_lock_unlock (lock);
193 return NSS_STATUS_SUCCESS;
196 static enum nss_status
197 internal_nisplus_getprotoent_r (struct protoent *proto, char *buffer,
198 size_t buflen, int *errnop)
202 /* Get the next entry until we found a correct one. */
205 nis_result *saved_res;
210 if (tablename_val == NULL)
212 enum nss_status status = _nss_create_tablename (errnop);
214 if (status != NSS_STATUS_SUCCESS)
218 result = nis_first_entry (tablename_val);
219 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
220 return niserr2nss (result->status);
227 res = nis_next_entry (tablename_val, &result->cookie);
230 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
232 nis_freeresult (saved_res);
233 return niserr2nss (result->status);
237 parse_res = _nss_nisplus_parse_protoent (result, proto, buffer,
241 nis_freeresult (result);
244 return NSS_STATUS_TRYAGAIN;
249 nis_freeresult (saved_res);
254 return NSS_STATUS_SUCCESS;
258 _nss_nisplus_getprotoent_r (struct protoent *result, char *buffer,
259 size_t buflen, int *errnop)
263 __libc_lock_lock (lock);
265 status = internal_nisplus_getprotoent_r (result, buffer, buflen, errnop);
267 __libc_lock_unlock (lock);
273 _nss_nisplus_getprotobyname_r (const char *name, struct protoent *proto,
274 char *buffer, size_t buflen, int *errnop)
278 if (tablename_val == NULL)
280 enum nss_status status = _nss_create_tablename (errnop);
282 if (status != NSS_STATUS_SUCCESS)
287 return NSS_STATUS_NOTFOUND;
291 char buf[strlen (name) + 255 + tablename_len];
293 /* Search at first in the alias list, and use the correct name
294 for the next search */
295 sprintf (buf, "[name=%s],%s", name, tablename_val);
296 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
298 /* If we do not find it, try it as original name. But if the
299 database is correct, we should find it in the first case, too */
300 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
301 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
302 || strcmp (result->objects.objects_val->EN_data.en_type,
303 "protocols_tbl") != 0
304 || result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
305 sprintf (buf, "[cname=%s],%s", name, tablename_val);
307 sprintf (buf, "[cname=%s],%s", NISENTRYVAL (0, 0, result),
310 nis_freeresult (result);
311 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
313 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
315 enum nss_status status = niserr2nss (result->status);
317 nis_freeresult (result);
321 parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen,
324 nis_freeresult (result);
329 return NSS_STATUS_TRYAGAIN;
332 return NSS_STATUS_SUCCESS;
334 return NSS_STATUS_NOTFOUND;
339 _nss_nisplus_getprotobynumber_r (const int number, struct protoent *proto,
340 char *buffer, size_t buflen, int *errnop)
342 if (tablename_val == NULL)
344 enum nss_status status = _nss_create_tablename (errnop);
346 if (status != NSS_STATUS_SUCCESS)
353 char buf[46 + tablename_len];
355 sprintf (buf, "[number=%d],%s", number, tablename_val);
357 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
359 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
361 enum nss_status status = niserr2nss (result->status);
363 nis_freeresult (result);
367 parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen,
370 nis_freeresult (result);
375 return NSS_STATUS_TRYAGAIN;
379 return NSS_STATUS_SUCCESS;
381 return NSS_STATUS_NOTFOUND;