4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
26 * Copyright (c) 2016 by Delphix. All rights reserved.
29 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
30 /* All Rights Reserved */
33 * Portions of this source code were derived from Berkeley
34 * under license from the Regents of the University of
38 #pragma ident "%Z%%M% %I% %E% SMI"
44 #include <sys/types.h>
46 #include <rpcsvc/yp_prot.h>
47 #include <rpcsvc/ypclnt.h>
50 extern int __yp_dobind_cflookup(char *, struct dom_binding
**, int);
52 static int dofirst(char *, char *, struct dom_binding
*, struct timeval
,
53 char **, int *, char **, int *);
55 static int donext(char *, char *, char *, int, struct dom_binding
*,
56 struct timeval
, char **, int *, char **val
, int *);
59 * This requests the yp server associated with a given domain to return the
60 * first key/value pair from the map data base. The returned key should be
61 * used as an input to the call to ypclnt_next. This part does the parameter
62 * checking, and the do-until-success loop if 'hardlookup' is set.
68 char **key
, /* return: key array */
69 int *keylen
, /* return: bytes in key */
70 char **val
, /* return: value array */
71 int *vallen
, /* return: bytes in val */
76 struct dom_binding
*pdomb
;
79 if ((map
== NULL
) || (domain
== NULL
))
80 return (YPERR_BADARGS
);
82 domlen
= strlen(domain
);
85 if ((domlen
== 0) || (domlen
> YPMAXDOMAIN
) ||
86 (maplen
== 0) || (maplen
> YPMAXMAP
))
87 return (YPERR_BADARGS
);
91 if (reason
= __yp_dobind_cflookup(domain
, &pdomb
, hardlookup
))
94 if (pdomb
->dom_binding
->ypbind_hi_vers
== YPVERS
) {
96 reason
= dofirst(domain
, map
, pdomb
, _ypserv_timeout
,
97 key
, keylen
, val
, vallen
);
99 __yp_rel_binding(pdomb
);
100 if (reason
== YPERR_RPC
|| reason
== YPERR_YPSERV
||
101 reason
== YPERR_BUSY
/* as if */) {
104 (void) sleep(_ypsleeptime
); /* retry */
110 __yp_rel_binding(pdomb
);
121 char **key
, /* return: key array */
122 int *keylen
, /* return: bytes in key */
123 char **val
, /* return: value array */
124 int *vallen
) /* return: bytes in val */
126 /* traditional yp_firs loops forever until success */
127 return (__yp_first_cflookup(domain
, map
, key
, keylen
, val
, vallen
, 1));
131 * This part of the "get first" interface talks to ypserv.
135 dofirst(domain
, map
, pdomb
, timeout
, key
, keylen
, val
, vallen
)
138 struct dom_binding
*pdomb
;
139 struct timeval timeout
;
146 struct ypreq_nokey req
;
147 struct ypresp_key_val resp
;
148 unsigned int retval
= 0;
152 resp
.keydat
.dptr
= resp
.valdat
.dptr
= NULL
;
153 resp
.keydat
.dsize
= resp
.valdat
.dsize
= 0;
156 * Do the get first request. If the rpc call failed, return with status
160 (void) memset((char *)&resp
, 0, sizeof (struct ypresp_key_val
));
162 switch (clnt_call(pdomb
->dom_client
, YPPROC_FIRST
,
163 (xdrproc_t
)xdr_ypreq_nokey
,
164 (char *)&req
, (xdrproc_t
)xdr_ypresp_key_val
,
165 (char *)&resp
, timeout
)) {
169 return (YPERR_YPSERV
);
174 /* See if the request succeeded */
176 if (resp
.status
!= YP_TRUE
) {
177 retval
= ypprot_err(resp
.status
);
180 /* Get some memory which the user can get rid of as they like */
184 if ((*key
= malloc((size_t)resp
.keydat
.dsize
+ 2)) != NULL
) {
187 (size_t)resp
.valdat
.dsize
+ 2)) == NULL
) {
189 retval
= YPERR_RESRC
;
193 retval
= YPERR_RESRC
;
197 /* Copy the returned key and value byte strings into the new memory */
200 *keylen
= (int)resp
.keydat
.dsize
;
201 (void) memcpy(*key
, resp
.keydat
.dptr
,
202 (size_t)resp
.keydat
.dsize
);
203 (*key
)[resp
.keydat
.dsize
] = '\n';
204 (*key
)[resp
.keydat
.dsize
+ 1] = '\0';
206 *vallen
= (int)resp
.valdat
.dsize
;
207 (void) memcpy(*val
, resp
.valdat
.dptr
,
208 (size_t)resp
.valdat
.dsize
);
209 (*val
)[resp
.valdat
.dsize
] = '\n';
210 (*val
)[resp
.valdat
.dsize
+ 1] = '\0';
213 CLNT_FREERES(pdomb
->dom_client
,
214 (xdrproc_t
)xdr_ypresp_key_val
, (char *)&resp
);
219 * This requests the yp server associated with a given domain to return the
220 * "next" key/value pair from the map data base. The input key should be
221 * one returned by ypclnt_first or a previous call to ypclnt_next. The
222 * returned key should be used as an input to the next call to ypclnt_next.
223 * This part does the parameter checking, and the do-until-success loop.
224 * if 'hardlookup' is set.
232 char **outkey
, /* return: key array associated with val */
233 int *outkeylen
, /* return: bytes in key */
234 char **val
, /* return: value array associated with outkey */
235 int *vallen
, /* return: bytes in val */
240 struct dom_binding
*pdomb
;
244 if ((map
== NULL
) || (domain
== NULL
) || (inkey
== NULL
))
245 return (YPERR_BADARGS
);
247 domlen
= strlen(domain
);
248 maplen
= strlen(map
);
250 if ((domlen
== 0) || (domlen
> YPMAXDOMAIN
) ||
251 (maplen
== 0) || (maplen
> YPMAXMAP
))
252 return (YPERR_BADARGS
);
255 if (reason
= __yp_dobind_cflookup(domain
, &pdomb
, hardlookup
))
258 if (pdomb
->dom_binding
->ypbind_hi_vers
== YPVERS
) {
260 reason
= donext(domain
, map
, inkey
, inkeylen
, pdomb
,
261 _ypserv_timeout
, outkey
, outkeylen
, val
, vallen
);
263 __yp_rel_binding(pdomb
);
265 if (reason
== YPERR_RPC
|| reason
== YPERR_YPSERV
||
266 reason
== YPERR_BUSY
/* as if */) {
269 (void) sleep(_ypsleeptime
); /* retry */
275 __yp_rel_binding(pdomb
);
289 char **outkey
, /* return: key array associated with val */
290 int *outkeylen
, /* return: bytes in key */
291 char **val
, /* return: value array associated with outkey */
292 int *vallen
) /* return: bytes in val */
294 /* traditional yp_next loops forever until success */
295 return (__yp_next_cflookup(domain
, map
, inkey
, inkeylen
, outkey
,
296 outkeylen
, val
, vallen
, 1));
301 * This part of the "get next" interface talks to ypserv.
304 donext(domain
, map
, inkey
, inkeylen
, pdomb
, timeout
, outkey
, outkeylen
,
310 struct dom_binding
*pdomb
;
311 struct timeval timeout
;
312 char **outkey
; /* return: key array associated with val */
313 int *outkeylen
; /* return: bytes in key */
314 char **val
; /* return: value array associated with outkey */
315 int *vallen
; /* return: bytes in val */
318 struct ypreq_key req
;
319 struct ypresp_key_val resp
;
320 unsigned int retval
= 0;
324 req
.keydat
.dptr
= inkey
;
325 req
.keydat
.dsize
= inkeylen
;
327 resp
.keydat
.dptr
= resp
.valdat
.dptr
= NULL
;
328 resp
.keydat
.dsize
= resp
.valdat
.dsize
= 0;
331 * Do the get next request. If the rpc call failed, return with status
335 switch (clnt_call(pdomb
->dom_client
,
336 YPPROC_NEXT
, (xdrproc_t
)xdr_ypreq_key
, (char *)&req
,
337 (xdrproc_t
)xdr_ypresp_key_val
, (char *)&resp
,
342 return (YPERR_YPSERV
);
347 /* See if the request succeeded */
349 if (resp
.status
!= YP_TRUE
) {
350 retval
= ypprot_err(resp
.status
);
353 /* Get some memory which the user can get rid of as they like */
356 if ((*outkey
= malloc((size_t)
357 resp
.keydat
.dsize
+ 2)) != NULL
) {
359 if ((*val
= malloc((size_t)
360 resp
.valdat
.dsize
+ 2)) == NULL
) {
362 retval
= YPERR_RESRC
;
366 retval
= YPERR_RESRC
;
370 /* Copy the returned key and value byte strings into the new memory */
373 *outkeylen
= (int)resp
.keydat
.dsize
;
374 (void) memcpy(*outkey
, resp
.keydat
.dptr
,
375 (size_t)resp
.keydat
.dsize
);
376 (*outkey
)[resp
.keydat
.dsize
] = '\n';
377 (*outkey
)[resp
.keydat
.dsize
+ 1] = '\0';
379 *vallen
= (int)resp
.valdat
.dsize
;
380 (void) memcpy(*val
, resp
.valdat
.dptr
,
381 (size_t)resp
.valdat
.dsize
);
382 (*val
)[resp
.valdat
.dsize
] = '\n';
383 (*val
)[resp
.valdat
.dsize
+ 1] = '\0';
386 CLNT_FREERES(pdomb
->dom_client
, (xdrproc_t
)xdr_ypresp_key_val
,