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 servent
37 #define DATABASE "services"
38 #define TRAILING_LIST_MEMBER s_aliases
39 #define TRAILING_LIST_SEPARATOR_P isspace
40 #include "../../nss/nss_files/files-parse.c"
41 #define ISSLASH(c) ((c) == '/')
44 STRING_FIELD (result
->s_name
, isspace
, 1);
45 INT_FIELD (result
->s_port
, ISSLASH
, 10, 0, htons
);
46 STRING_FIELD (result
->s_proto
, isspace
, 1);
50 #define NISENTRYVAL(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_val)
53 #define NISENTRYLEN(idx,col,res) \
54 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
57 _nss_nisplus_parse_servent (nis_result
*result
, struct servent
*serv
,
58 char *buffer
, size_t buflen
)
61 size_t room_left
= buflen
;
63 struct parser_data
*data
= (void *) buffer
;
68 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
) ||
69 result
->objects
.objects_val
[0].zo_data
.zo_type
!= ENTRY_OBJ
||
70 strcmp (result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_type
,
71 "services_tbl") != 0 ||
72 result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_cols
.en_cols_len
< 4)
75 memset (p
, '\0', room_left
);
77 /* Generate the services entry format and use the normal parser */
78 if (NISENTRYLEN (0, 0, result
) + 1 > room_left
)
83 strncpy (p
, NISENTRYVAL (0, 0, result
), NISENTRYLEN (0, 0, result
));
84 room_left
-= (NISENTRYLEN (0, 0, result
) + 1);
86 if (NISENTRYLEN (0, 3, result
) + 1 > room_left
)
92 strncat (p
, NISENTRYVAL (0, 3, result
), NISENTRYLEN (0, 3, result
));
93 room_left
-= (NISENTRYLEN (0, 3, result
) + 1);
94 if (NISENTRYLEN (0, 2, result
) + 1 > room_left
)
100 strncat (p
, NISENTRYVAL (0, 2, result
), NISENTRYLEN (0, 2, result
));
101 room_left
-= (NISENTRYLEN (0, 2, result
) + 1);
103 for (i
= 1; i
< result
->objects
.objects_len
; i
++)
105 if (NISENTRYLEN (i
, 1, result
) + 1 > room_left
)
107 __set_errno (ERANGE
);
111 strcat (p
, NISENTRYVAL (i
, 1, result
));
112 room_left
-= (NISENTRYLEN (i
, 1, result
) + 1);
115 return _nss_files_parse_servent (p
, serv
, data
, buflen
);
119 _nss_nisplus_setservent (void)
121 __libc_lock_lock (lock
);
124 nis_freeresult (result
);
128 nis_freenames (names
);
132 __libc_lock_unlock (lock
);
134 return NSS_STATUS_SUCCESS
;
138 _nss_nisplus_endservent (void)
140 __libc_lock_lock (lock
);
143 nis_freeresult (result
);
147 nis_freenames (names
);
151 __libc_lock_unlock (lock
);
153 return NSS_STATUS_SUCCESS
;
156 static enum nss_status
157 internal_nisplus_getservent_r (struct servent
*serv
, char *buffer
,
162 /* Get the next entry until we found a correct one. */
167 names
= nis_getnames ("services.org_dir");
168 if (names
== NULL
|| names
[0] == NULL
)
169 return NSS_STATUS_UNAVAIL
;
171 result
= nis_first_entry (names
[0]);
172 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
173 return niserr2nss (result
->status
);
179 res
= nis_next_entry (names
[0], &result
->cookie
);
180 nis_freeresult (result
);
182 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
183 return niserr2nss (result
->status
);
186 parse_res
= _nss_nisplus_parse_servent (result
, serv
, buffer
, buflen
);
190 return NSS_STATUS_SUCCESS
;
194 _nss_nisplus_getservent_r (struct servent
*result
, char *buffer
,
199 __libc_lock_lock (lock
);
201 status
= internal_nisplus_getservent_r (result
, buffer
, buflen
);
203 __libc_lock_unlock (lock
);
209 _nss_nisplus_getservbyname_r (const char *name
, const char *protocol
,
210 struct servent
*serv
,
211 char *buffer
, size_t buflen
)
215 if (name
== NULL
|| protocol
== NULL
)
217 __set_errno (EINVAL
);
218 return NSS_STATUS_NOTFOUND
;
223 char buf
[strlen (name
) + 255];
225 /* Search at first in the alias list, and use the correct name
226 for the next search */
227 sprintf (buf
, "[name=%s,proto=%s],services.org_dir", name
,
229 result
= nis_list (buf
, EXPAND_NAME
, NULL
, NULL
);
231 /* If we do not find it, try it as original name. But if the
232 database is correct, we should find it in the first case, too */
233 if ((result
->status
!= NIS_SUCCESS
&&
234 result
->status
!= NIS_S_SUCCESS
) ||
235 result
->objects
.objects_val
[0].zo_data
.zo_type
!= ENTRY_OBJ
||
236 strcmp (result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_type
,
237 "services_tbl") != 0 ||
238 result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_cols
.en_cols_len
< 4)
239 sprintf (buf
, "[cname=%s,proto=%s],services.org_dir", name
, protocol
);
241 sprintf (buf
, "[cname=%s,proto=%s],services.org_dir",
242 NISENTRYVAL (0, 0, result
), protocol
);
244 nis_freeresult (result
);
245 result
= nis_list (buf
, EXPAND_NAME
, NULL
, NULL
);
247 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
249 enum nss_status status
= niserr2nss (result
->status
);
251 nis_freeresult (result
);
255 parse_res
= _nss_nisplus_parse_servent (result
, serv
, buffer
, buflen
);
257 nis_freeresult (result
);
260 return NSS_STATUS_SUCCESS
;
262 if (!parse_res
&& errno
== ERANGE
)
263 return NSS_STATUS_TRYAGAIN
;
265 return NSS_STATUS_NOTFOUND
;
270 _nss_nisplus_getservbynumber_r (const int number
, const char *protocol
,
271 struct servent
*serv
,
272 char *buffer
, size_t buflen
)
276 char buf
[60 + strlen (protocol
)];
278 if (protocol
== NULL
)
280 __set_errno (EINVAL
);
281 return NSS_STATUS_NOTFOUND
;
284 snprintf (buf
, sizeof (buf
), "[number=%d,proto=%s],services.org_dir",
287 result
= nis_list (buf
, EXPAND_NAME
, NULL
, NULL
);
289 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
291 enum nss_status status
= niserr2nss (result
->status
);
293 nis_freeresult (result
);
297 parse_res
= _nss_nisplus_parse_servent (result
, serv
, buffer
, buflen
);
299 nis_freeresult (result
);
302 return NSS_STATUS_SUCCESS
;
304 if (!parse_res
&& errno
== ERANGE
)
305 return NSS_STATUS_TRYAGAIN
;
307 return NSS_STATUS_NOTFOUND
;