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 <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
)
169 return NSS_STATUS_NOTFOUND
;
170 p
= strncpy (buffer
, data
->next
->val
, buflen
);
174 parse_res
= _nss_files_parse_servent (p
, serv
, buffer
, buflen
, errnop
);
176 return NSS_STATUS_TRYAGAIN
;
177 data
->next
= data
->next
->next
;
181 return NSS_STATUS_SUCCESS
;
185 _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
, errnop
, &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
,
204 intern_t data
= { NULL
, NULL
};
205 enum nss_status status
;
211 return NSS_STATUS_UNAVAIL
;
214 status
= internal_nis_setservent (&data
);
215 if (status
!= NSS_STATUS_SUCCESS
)
220 ((status
= internal_nis_getservent_r (serv
, buffer
, buflen
, errnop
,
221 &data
)) == NSS_STATUS_SUCCESS
))
223 if (protocol
== NULL
|| strcmp (serv
->s_proto
, protocol
) == 0)
227 if (strcmp (serv
->s_name
, name
) == 0)
230 for (cp
= serv
->s_aliases
; *cp
; cp
++)
231 if (strcmp (name
, *cp
) == 0)
236 internal_nis_endservent (&data
);
238 if (!found
&& status
== NSS_STATUS_SUCCESS
)
239 return NSS_STATUS_NOTFOUND
;
245 _nss_nis_getservbyport_r (int port
, char *protocol
, struct servent
*serv
,
246 char *buffer
, size_t buflen
, int *errnop
)
248 intern_t data
= { NULL
, NULL
};
249 enum nss_status status
;
252 if (protocol
== NULL
)
255 return NSS_STATUS_UNAVAIL
;
258 status
= internal_nis_setservent (&data
);
259 if (status
!= NSS_STATUS_SUCCESS
)
264 ((status
= internal_nis_getservent_r (serv
, buffer
, buflen
, errnop
,
265 &data
)) == NSS_STATUS_SUCCESS
))
266 if (htons (serv
->s_port
) == port
267 && strcmp (serv
->s_proto
, protocol
) == 0)
270 internal_nis_endservent (&data
);
272 if (!found
&& status
== NSS_STATUS_SUCCESS
)
273 return NSS_STATUS_NOTFOUND
;