1 /* Copyright (C) 1996, 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>, 1996.
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/yp.h>
27 #include <rpcsvc/ypclnt.h>
32 /* The parser is defined in a different module. */
33 extern int _nss_files_parse_servent (char *line
, struct servent
*result
,
34 char *data
, size_t datalen
);
38 __libc_lock_define_initialized (static, lock
)
43 struct response_t
*next
;
48 struct response_t
*start
;
49 struct response_t
*next
;
51 typedef struct intern_t intern_t
;
53 static intern_t intern
= { NULL
, NULL
};
56 saveit (int instatus
, char *inkey
, int inkeylen
, char *inval
,
57 int invallen
, char *indata
)
59 intern_t
*intern
= (intern_t
*) indata
;
61 if (instatus
!= YP_TRUE
)
64 if (inkey
&& inkeylen
> 0 && inval
&& invallen
> 0)
66 if (intern
->start
== NULL
)
68 intern
->start
= malloc (sizeof (struct response_t
));
69 intern
->next
= intern
->start
;
73 intern
->next
->next
= malloc (sizeof (struct response_t
));
74 intern
->next
= intern
->next
->next
;
76 intern
->next
->next
= NULL
;
77 intern
->next
->val
= malloc (invallen
+ 1);
78 strncpy (intern
->next
->val
, inval
, invallen
);
79 intern
->next
->val
[invallen
] = '\0';
85 static enum nss_status
86 internal_nis_setservent (intern_t
*intern
)
89 struct ypall_callback ypcb
;
90 enum nss_status status
;
92 if (yp_get_default_domain (&domainname
))
93 return NSS_STATUS_UNAVAIL
;
95 while (intern
->start
!= NULL
)
97 if (intern
->start
->val
!= NULL
)
98 free (intern
->start
->val
);
99 intern
->next
= intern
->start
;
100 intern
->start
= intern
->start
->next
;
103 intern
->start
= NULL
;
105 ypcb
.foreach
= saveit
;
106 ypcb
.data
= (char *) intern
;
107 status
= yperr2nss (yp_all (domainname
, "services.byname", &ypcb
));
108 intern
->next
= intern
->start
;
113 _nss_nis_setservent (void)
115 enum nss_status status
;
117 __libc_lock_lock (lock
);
119 status
= internal_nis_setservent (&intern
);
121 __libc_lock_unlock (lock
);
126 static enum nss_status
127 internal_nis_endservent (intern_t
* intern
)
129 while (intern
->start
!= NULL
)
131 if (intern
->start
->val
!= NULL
)
132 free (intern
->start
->val
);
133 intern
->next
= intern
->start
;
134 intern
->start
= intern
->start
->next
;
137 intern
->start
= NULL
;
139 return NSS_STATUS_SUCCESS
;
143 _nss_nis_endservent (void)
145 enum nss_status status
;
147 __libc_lock_lock (lock
);
149 status
= internal_nis_endservent (&intern
);
151 __libc_lock_unlock (lock
);
156 static enum nss_status
157 internal_nis_getservent_r (struct servent
*serv
, char *buffer
,
158 size_t buflen
, intern_t
*data
)
163 if (data
->start
== NULL
)
164 internal_nis_setservent (data
);
166 /* Get the next entry until we found a correct one. */
169 if (data
->next
== NULL
)
170 return NSS_STATUS_NOTFOUND
;
171 p
= strncpy (buffer
, data
->next
->val
, buflen
);
172 data
->next
= data
->next
->next
;
176 parse_res
= _nss_files_parse_servent (p
, serv
, buffer
, buflen
);
177 if (!parse_res
&& errno
== ERANGE
)
178 return NSS_STATUS_TRYAGAIN
;
182 return NSS_STATUS_SUCCESS
;
186 _nss_nis_getservent_r (struct servent
*serv
, char *buffer
, size_t buflen
)
188 enum nss_status status
;
190 __libc_lock_lock (lock
);
192 status
= internal_nis_getservent_r (serv
, buffer
, buflen
, &intern
);
194 __libc_lock_unlock (lock
);
200 _nss_nis_getservbyname_r (const char *name
, char *protocol
,
201 struct servent
*serv
, char *buffer
, size_t buflen
)
203 intern_t data
= { NULL
, NULL
};
204 enum nss_status status
;
209 __set_errno (EINVAL
);
210 return NSS_STATUS_UNAVAIL
;
213 status
= internal_nis_setservent (&data
);
214 if (status
!= NSS_STATUS_SUCCESS
)
219 ((status
= internal_nis_getservent_r (serv
, buffer
, buflen
, &data
))
220 == NSS_STATUS_SUCCESS
))
222 if (protocol
== NULL
|| strcmp (serv
->s_proto
, protocol
) == 0)
226 if (strcmp (serv
->s_name
, name
) == 0)
229 for (cp
= serv
->s_aliases
; *cp
; cp
++)
230 if (strcmp (name
, *cp
) == 0)
235 internal_nis_endservent (&data
);
237 if (!found
&& status
== NSS_STATUS_SUCCESS
)
238 return NSS_STATUS_NOTFOUND
;
244 _nss_nis_getservbyport_r (int port
, char *protocol
, struct servent
*serv
,
245 char *buffer
, size_t buflen
)
247 intern_t data
= { NULL
, NULL
};
248 enum nss_status status
;
251 if (protocol
== NULL
)
253 __set_errno (EINVAL
);
254 return NSS_STATUS_UNAVAIL
;
257 status
= internal_nis_setservent (&data
);
258 if (status
!= NSS_STATUS_SUCCESS
)
263 ((status
= internal_nis_getservent_r (serv
, buffer
, buflen
, &data
))
264 == NSS_STATUS_SUCCESS
))
266 if (htons (serv
->s_port
) == port
)
268 if (strcmp (serv
->s_proto
, protocol
) == 0)
275 internal_nis_endservent (&data
);
277 if (!found
&& status
== NSS_STATUS_SUCCESS
)
278 return NSS_STATUS_NOTFOUND
;