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 <libc-lock.h>
26 #include <rpcsvc/nis.h>
27 #include <rpcsvc/nislib.h>
29 #include "nss-nisplus.h"
31 __libc_lock_define_initialized (static, lock
)
33 static nis_result
*result
= NULL
;
34 static nis_name
*names
= NULL
;
36 #define ENTNAME protoent
37 #define DATABASE "protocols"
38 #define TRAILING_LIST_MEMBER p_aliases
39 #define TRAILING_LIST_SEPARATOR_P isspace
40 #include "../../nss/nss_files/files-parse.c"
43 STRING_FIELD (result
->p_name
, isspace
, 1);
44 INT_FIELD (result
->p_proto
, isspace
, 1, 10,);
47 #define NISENTRYVAL(idx,col,res) \
48 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
50 #define NISENTRYLEN(idx,col,res) \
51 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
54 _nss_nisplus_parse_protoent (nis_result
* result
, struct protoent
*proto
,
55 char *buffer
, size_t buflen
)
58 size_t room_left
= buflen
;
60 struct parser_data
*data
= (void *) buffer
;
65 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
) ||
66 result
->objects
.objects_val
[0].zo_data
.zo_type
!= ENTRY_OBJ
||
67 strcmp (result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_type
,
68 "protocols_tbl") != 0 ||
69 result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_cols
.en_cols_len
< 3)
72 memset (p
, '\0', room_left
);
74 /* Generate the protocols entry format and use the normal parser */
75 if (NISENTRYLEN (0, 0, result
) + 1 > room_left
)
80 strncpy (p
, NISENTRYVAL (0, 0, result
), NISENTRYLEN (0, 0, result
));
81 room_left
-= (NISENTRYLEN (0, 0, result
) + 1);
83 if (NISENTRYLEN (0, 2, result
) + 1 > room_left
)
89 strncat (p
, NISENTRYVAL (0, 2, result
), NISENTRYLEN (0, 2, result
));
90 room_left
-= (NISENTRYLEN (0, 2, result
) + 1);
91 /* + 1: We overwrite the last \0 */
93 for (i
= 1; i
< result
->objects
.objects_len
; i
++)
95 if (NISENTRYLEN (i
, 1, result
) + 1 > room_left
)
101 strncat (p
, NISENTRYVAL (i
, 1, result
), NISENTRYLEN (i
, 1, result
));
102 room_left
-= (NISENTRYLEN (i
, 1, result
) + 1);
105 return _nss_files_parse_protoent (p
, proto
, data
, buflen
);
109 _nss_nisplus_setprotoent (void)
111 __libc_lock_lock (lock
);
114 nis_freeresult (result
);
118 nis_freenames (names
);
122 __libc_lock_unlock (lock
);
124 return NSS_STATUS_SUCCESS
;
128 _nss_nisplus_endprotoent (void)
130 __libc_lock_lock (lock
);
133 nis_freeresult (result
);
137 nis_freenames (names
);
141 __libc_lock_unlock (lock
);
143 return NSS_STATUS_SUCCESS
;
146 static enum nss_status
147 internal_nisplus_getprotoent_r (struct protoent
*proto
, char *buffer
,
152 /* Get the next entry until we found a correct one. */
157 names
= nis_getnames ("protocols.org_dir");
158 if (names
== NULL
|| names
[0] == NULL
)
159 return NSS_STATUS_UNAVAIL
;
161 result
= nis_first_entry (names
[0]);
162 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
163 return niserr2nss (result
->status
);
169 res
= nis_next_entry (names
[0], &result
->cookie
);
170 nis_freeresult (result
);
173 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
174 return niserr2nss (result
->status
);
177 parse_res
= _nss_nisplus_parse_protoent (result
, proto
, buffer
, buflen
);
181 return NSS_STATUS_SUCCESS
;
185 _nss_nisplus_getprotoent_r (struct protoent
*result
, char *buffer
,
190 __libc_lock_lock (lock
);
192 status
= internal_nisplus_getprotoent_r (result
, buffer
, buflen
);
194 __libc_lock_unlock (lock
);
200 _nss_nisplus_getprotobyname_r (const char *name
, struct protoent
*proto
,
201 char *buffer
, size_t buflen
)
206 return NSS_STATUS_NOTFOUND
;
210 char buf
[strlen (name
) + 255];
212 /* Search at first in the alias list, and use the correct name
213 for the next search */
214 sprintf (buf
, "[name=%s],protocols.org_dir", name
);
215 result
= nis_list (buf
, EXPAND_NAME
, NULL
, NULL
);
217 /* If we do not find it, try it as original name. But if the
218 database is correct, we should find it in the first case, too */
219 if ((result
->status
!= NIS_SUCCESS
&&
220 result
->status
!= NIS_S_SUCCESS
) ||
221 result
->objects
.objects_val
[0].zo_data
.zo_type
!= ENTRY_OBJ
||
222 strcmp (result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_type
,
223 "protocols_tbl") != 0 ||
224 result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_cols
.en_cols_len
< 3)
225 sprintf (buf
, "[cname=%s],protocols.org_dir", name
);
227 sprintf (buf
, "[cname=%s],protocols.org_dir", NISENTRYVAL (0, 0, result
));
229 nis_freeresult (result
);
230 result
= nis_list (buf
, EXPAND_NAME
, NULL
, NULL
);
232 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
234 enum nss_status status
= niserr2nss (result
->status
);
236 nis_freeresult (result
);
240 parse_res
= _nss_nisplus_parse_protoent (result
, proto
, buffer
, buflen
);
242 nis_freeresult (result
);
245 return NSS_STATUS_SUCCESS
;
247 if (!parse_res
&& errno
== ERANGE
)
248 return NSS_STATUS_TRYAGAIN
;
250 return NSS_STATUS_NOTFOUND
;
255 _nss_nisplus_getprotobynumber_r (const int number
, struct protoent
*proto
,
256 char *buffer
, size_t buflen
)
262 snprintf (buf
, sizeof (buf
), "[number=%d],protocols.org_dir", number
);
264 result
= nis_list (buf
, EXPAND_NAME
, NULL
, NULL
);
266 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
268 enum nss_status status
= niserr2nss (result
->status
);
270 nis_freeresult (result
);
274 parse_res
= _nss_nisplus_parse_protoent (result
, proto
, buffer
, buflen
);
276 nis_freeresult (result
);
278 return NSS_STATUS_SUCCESS
;
280 if (!parse_res
&& errno
== ERANGE
)
281 return NSS_STATUS_TRYAGAIN
;
283 return NSS_STATUS_NOTFOUND
;