1 /* Copyright (C) 1997, 1998, 2001, 2002, 2003 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 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
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
;
33 static nis_name tablename_val
;
34 static u_long tablename_len
;
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 (int stayopen
)
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
];
294 /* Search at first in the alias list, and use the correct name
295 for the next search */
296 sprintf (buf
, "[name=%s],%s", name
, tablename_val
);
297 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
301 /* If we do not find it, try it as original name. But if the
302 database is correct, we should find it in the first case, too */
303 if ((result
->status
!= NIS_SUCCESS
304 && result
->status
!= NIS_S_SUCCESS
)
305 || __type_of (result
->objects
.objects_val
) != NIS_ENTRY_OBJ
306 || strcmp (result
->objects
.objects_val
->EN_data
.en_type
,
307 "protocols_tbl") != 0
308 || result
->objects
.objects_val
->EN_data
.en_cols
.en_cols_len
< 3)
309 sprintf (buf
, "[cname=%s],%s", name
, tablename_val
);
311 sprintf (buf
, "[cname=%s],%s", NISENTRYVAL (0, 0, result
),
314 nis_freeresult (result
);
315 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
320 __set_errno (ENOMEM
);
321 return NSS_STATUS_TRYAGAIN
;
323 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
325 enum nss_status status
= niserr2nss (result
->status
);
327 __set_errno (olderr
);
329 nis_freeresult (result
);
333 parse_res
= _nss_nisplus_parse_protoent (result
, proto
, buffer
, buflen
,
336 nis_freeresult (result
);
343 return NSS_STATUS_TRYAGAIN
;
347 __set_errno (olderr
);
348 return NSS_STATUS_NOTFOUND
;
351 return NSS_STATUS_SUCCESS
;
356 _nss_nisplus_getprotobynumber_r (const int number
, struct protoent
*proto
,
357 char *buffer
, size_t buflen
, int *errnop
)
359 if (tablename_val
== NULL
)
361 enum nss_status status
= _nss_create_tablename (errnop
);
363 if (status
!= NSS_STATUS_SUCCESS
)
370 char buf
[46 + tablename_len
];
373 sprintf (buf
, "[number=%d],%s", number
, tablename_val
);
375 result
= nis_list (buf
, FOLLOW_LINKS
| FOLLOW_PATH
, NULL
, NULL
);
379 __set_errno (ENOMEM
);
380 return NSS_STATUS_TRYAGAIN
;
382 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
384 enum nss_status status
= niserr2nss (result
->status
);
386 __set_errno (olderr
);
388 nis_freeresult (result
);
392 parse_res
= _nss_nisplus_parse_protoent (result
, proto
, buffer
, buflen
,
395 nis_freeresult (result
);
402 return NSS_STATUS_TRYAGAIN
;
406 __set_errno (olderr
);
407 return NSS_STATUS_NOTFOUND
;
410 return NSS_STATUS_SUCCESS
;