1 /* Copyright (C) 1996-1998,2000,2002,2003,2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
31 /* Get the declaration of the parser function. */
32 #define ENTNAME rpcent
34 #include <nss/nss_files/files-parse.c>
36 __libc_lock_define_initialized (static, lock
)
40 struct response_t
*next
;
46 struct response_t
*start
;
47 struct response_t
*next
;
49 typedef struct intern_t intern_t
;
51 static intern_t intern
= {NULL
, NULL
};
54 saveit (int instatus
, char *inkey
, int inkeylen
, char *inval
,
55 int invallen
, char *indata
)
57 intern_t
*intern
= (intern_t
*)indata
;
59 if (instatus
!= YP_TRUE
)
62 if (inkey
&& inkeylen
> 0 && inval
&& invallen
> 0)
64 struct response_t
*newp
= malloc (sizeof (struct response_t
)
67 return 1; /* We have no error code for out of memory */
69 if (intern
->start
== NULL
)
72 intern
->next
->next
= newp
;
76 *((char *) mempcpy (newp
->val
, inval
, invallen
)) = '\0';
83 internal_nis_endrpcent (intern_t
*intern
)
85 while (intern
->start
!= NULL
)
87 intern
->next
= intern
->start
;
88 intern
->start
= intern
->start
->next
;
93 static enum nss_status
94 internal_nis_setrpcent (intern_t
*intern
)
97 struct ypall_callback ypcb
;
98 enum nss_status status
;
100 if (yp_get_default_domain (&domainname
))
101 return NSS_STATUS_UNAVAIL
;
103 internal_nis_endrpcent (intern
);
105 ypcb
.foreach
= saveit
;
106 ypcb
.data
= (char *)intern
;
107 status
= yperr2nss (yp_all(domainname
, "rpc.bynumber", &ypcb
));
108 intern
->next
= intern
->start
;
114 _nss_nis_setrpcent (int stayopen
)
116 enum nss_status status
;
118 __libc_lock_lock (lock
);
120 status
= internal_nis_setrpcent (&intern
);
122 __libc_lock_unlock (lock
);
128 _nss_nis_endrpcent (void)
130 __libc_lock_lock (lock
);
132 internal_nis_endrpcent (&intern
);
134 __libc_lock_unlock (lock
);
136 return NSS_STATUS_SUCCESS
;
139 static enum nss_status
140 internal_nis_getrpcent_r (struct rpcent
*rpc
, char *buffer
, size_t buflen
,
141 int *errnop
, intern_t
*data
)
143 struct parser_data
*pdata
= (void *) buffer
;
147 if (data
->start
== NULL
)
148 internal_nis_setrpcent (data
);
150 /* Get the next entry until we found a correct one. */
153 if (data
->next
== NULL
)
154 return NSS_STATUS_NOTFOUND
;
156 p
= strncpy (buffer
, data
->next
->val
, buflen
);
160 parse_res
= _nss_files_parse_rpcent (p
, rpc
, pdata
, buflen
, errnop
);
162 return NSS_STATUS_TRYAGAIN
;
163 data
->next
= data
->next
->next
;
167 return NSS_STATUS_SUCCESS
;
171 _nss_nis_getrpcent_r (struct rpcent
*rpc
, char *buffer
, size_t buflen
,
174 enum nss_status status
;
176 __libc_lock_lock (lock
);
178 status
= internal_nis_getrpcent_r (rpc
, buffer
, buflen
, errnop
, &intern
);
180 __libc_lock_unlock (lock
);
186 _nss_nis_getrpcbyname_r (const char *name
, struct rpcent
*rpc
,
187 char *buffer
, size_t buflen
, int *errnop
)
189 intern_t data
= {NULL
, NULL
};
190 enum nss_status status
;
196 return NSS_STATUS_UNAVAIL
;
199 status
= internal_nis_setrpcent (&data
);
200 if (status
!= NSS_STATUS_SUCCESS
)
205 ((status
= internal_nis_getrpcent_r (rpc
, buffer
, buflen
, errnop
,
206 &data
)) == NSS_STATUS_SUCCESS
))
208 if (strcmp (rpc
->r_name
, name
) == 0)
214 while (rpc
->r_aliases
[i
] != NULL
)
216 if (strcmp (rpc
->r_aliases
[i
], name
) == 0)
227 internal_nis_endrpcent (&data
);
229 if (!found
&& status
== NSS_STATUS_SUCCESS
)
230 return NSS_STATUS_NOTFOUND
;
236 _nss_nis_getrpcbynumber_r (int number
, struct rpcent
*rpc
,
237 char *buffer
, size_t buflen
, int *errnop
)
239 struct parser_data
*data
= (void *) buffer
;
240 enum nss_status retval
;
241 char *domain
, *result
, *p
;
242 int len
, nlen
, parse_res
;
245 if (yp_get_default_domain (&domain
))
246 return NSS_STATUS_UNAVAIL
;
248 nlen
= sprintf (buf
, "%d", number
);
250 retval
= yperr2nss (yp_match (domain
, "rpc.bynumber", buf
,
251 nlen
, &result
, &len
));
253 if (retval
!= NSS_STATUS_SUCCESS
)
255 if (retval
== NSS_STATUS_TRYAGAIN
)
260 if ((size_t) (len
+ 1) > buflen
)
264 return NSS_STATUS_TRYAGAIN
;
267 p
= strncpy (buffer
, result
, len
);
273 parse_res
= _nss_files_parse_rpcent (p
, rpc
, data
, buflen
, errnop
);
278 return NSS_STATUS_TRYAGAIN
;
280 return NSS_STATUS_NOTFOUND
;
283 return NSS_STATUS_SUCCESS
;