1 /* Copyright (C) 1996, 1997, 1998 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 <bits/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
, int *errnop
);
37 __libc_lock_define_initialized (static, lock
)
42 struct response_t
*next
;
47 struct response_t
*start
;
48 struct response_t
*next
;
50 typedef struct intern_t intern_t
;
52 static intern_t intern
= { NULL
, NULL
};
55 saveit (int instatus
, char *inkey
, int inkeylen
, char *inval
,
56 int invallen
, char *indata
)
58 intern_t
*intern
= (intern_t
*) indata
;
60 if (instatus
!= YP_TRUE
)
63 if (inkey
&& inkeylen
> 0 && inval
&& invallen
> 0)
65 if (intern
->start
== NULL
)
67 intern
->start
= malloc (sizeof (struct response_t
));
68 intern
->next
= intern
->start
;
72 intern
->next
->next
= malloc (sizeof (struct response_t
));
73 intern
->next
= intern
->next
->next
;
75 intern
->next
->next
= NULL
;
76 intern
->next
->val
= malloc (invallen
+ 1);
77 strncpy (intern
->next
->val
, inval
, invallen
);
78 intern
->next
->val
[invallen
] = '\0';
84 static enum nss_status
85 internal_nis_setservent (intern_t
*intern
)
88 struct ypall_callback ypcb
;
89 enum nss_status status
;
91 if (yp_get_default_domain (&domainname
))
92 return NSS_STATUS_UNAVAIL
;
94 while (intern
->start
!= NULL
)
96 if (intern
->start
->val
!= NULL
)
97 free (intern
->start
->val
);
98 intern
->next
= intern
->start
;
99 intern
->start
= intern
->start
->next
;
102 intern
->start
= NULL
;
104 ypcb
.foreach
= saveit
;
105 ypcb
.data
= (char *) intern
;
106 status
= yperr2nss (yp_all (domainname
, "services.byname", &ypcb
));
107 intern
->next
= intern
->start
;
112 _nss_nis_setservent (void)
114 enum nss_status status
;
116 __libc_lock_lock (lock
);
118 status
= internal_nis_setservent (&intern
);
120 __libc_lock_unlock (lock
);
125 static enum nss_status
126 internal_nis_endservent (intern_t
* intern
)
128 while (intern
->start
!= NULL
)
130 if (intern
->start
->val
!= NULL
)
131 free (intern
->start
->val
);
132 intern
->next
= intern
->start
;
133 intern
->start
= intern
->start
->next
;
136 intern
->start
= NULL
;
138 return NSS_STATUS_SUCCESS
;
142 _nss_nis_endservent (void)
144 enum nss_status status
;
146 __libc_lock_lock (lock
);
148 status
= internal_nis_endservent (&intern
);
150 __libc_lock_unlock (lock
);
155 static enum nss_status
156 internal_nis_getservent_r (struct servent
*serv
, char *buffer
,
157 size_t buflen
, int *errnop
, intern_t
*data
)
162 if (data
->start
== NULL
)
163 internal_nis_setservent (data
);
165 /* Get the next entry until we found a correct one. */
168 if (data
->next
== NULL
)
171 return NSS_STATUS_NOTFOUND
;
173 p
= strncpy (buffer
, data
->next
->val
, buflen
);
177 parse_res
= _nss_files_parse_servent (p
, serv
, buffer
, buflen
, errnop
);
179 return NSS_STATUS_TRYAGAIN
;
180 data
->next
= data
->next
->next
;
184 return NSS_STATUS_SUCCESS
;
188 _nss_nis_getservent_r (struct servent
*serv
, char *buffer
, size_t buflen
,
191 enum nss_status status
;
193 __libc_lock_lock (lock
);
195 status
= internal_nis_getservent_r (serv
, buffer
, buflen
, errnop
, &intern
);
197 __libc_lock_unlock (lock
);
203 _nss_nis_getservbyname_r (const char *name
, char *protocol
,
204 struct servent
*serv
, char *buffer
, size_t buflen
,
207 intern_t data
= { NULL
, NULL
};
208 enum nss_status status
;
214 return NSS_STATUS_UNAVAIL
;
217 status
= internal_nis_setservent (&data
);
218 if (status
!= NSS_STATUS_SUCCESS
)
223 ((status
= internal_nis_getservent_r (serv
, buffer
, buflen
, errnop
,
224 &data
)) == NSS_STATUS_SUCCESS
))
226 if (protocol
== NULL
|| strcmp (serv
->s_proto
, protocol
) == 0)
230 if (strcmp (serv
->s_name
, name
) == 0)
233 for (cp
= serv
->s_aliases
; *cp
; cp
++)
234 if (strcmp (name
, *cp
) == 0)
239 internal_nis_endservent (&data
);
241 if (!found
&& status
== NSS_STATUS_SUCCESS
)
244 return NSS_STATUS_NOTFOUND
;
251 _nss_nis_getservbyport_r (int port
, char *protocol
, struct servent
*serv
,
252 char *buffer
, size_t buflen
, int *errnop
)
254 intern_t data
= { NULL
, NULL
};
255 enum nss_status status
;
258 if (protocol
== NULL
)
261 return NSS_STATUS_UNAVAIL
;
264 status
= internal_nis_setservent (&data
);
265 if (status
!= NSS_STATUS_SUCCESS
)
270 ((status
= internal_nis_getservent_r (serv
, buffer
, buflen
, errnop
,
271 &data
)) == NSS_STATUS_SUCCESS
))
272 if (htons (serv
->s_port
) == port
273 && strcmp (serv
->s_proto
, protocol
) == 0)
276 internal_nis_endservent (&data
);
278 if (!found
&& status
== NSS_STATUS_SUCCESS
)
281 return NSS_STATUS_NOTFOUND
;