xref: /netbsd/lib/libc/yp/yp_first.c (revision bf9ec67e)
1 /*	$NetBSD: yp_first.c,v 1.13 2000/07/06 03:14:05 christos Exp $	 */
2 
3 /*
4  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Theo de Raadt.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
36 __RCSID("$NetBSD: yp_first.c,v 1.13 2000/07/06 03:14:05 christos Exp $");
37 #endif
38 
39 #include "namespace.h"
40 #include <stdlib.h>
41 #include <string.h>
42 #include <rpc/rpc.h>
43 #include <rpcsvc/yp_prot.h>
44 #include <rpcsvc/ypclnt.h>
45 #include "local.h"
46 
47 extern struct timeval _yplib_timeout;
48 extern int _yplib_nerrs;
49 
50 #ifdef __weak_alias
51 __weak_alias(yp_first,_yp_first)
52 __weak_alias(yp_next,_yp_next)
53 #endif
54 
55 int
56 yp_first(indomain, inmap, outkey, outkeylen, outval, outvallen)
57 	const char     *indomain;
58 	const char     *inmap;
59 	char          **outkey;
60 	int            *outkeylen;
61 	char          **outval;
62 	int            *outvallen;
63 {
64 	struct ypresp_key_val yprkv;
65 	struct ypreq_nokey yprnk;
66 	struct dom_binding *ysd;
67 	int r, nerrs = 0;
68 
69 	if (outkey == NULL || outkeylen == NULL || \
70 	    outval == NULL || outvallen == NULL)
71 		return YPERR_BADARGS;
72 	*outkey = *outval = NULL;
73 	*outkeylen = *outvallen = 0;
74 	if (_yp_invalid_domain(indomain))
75 		return YPERR_BADARGS;
76 	if (inmap == NULL || *inmap == '\0'
77 	    || strlen(inmap) > YPMAXMAP)
78 		return YPERR_BADARGS;
79 
80 again:
81 	if (_yp_dobind(indomain, &ysd) != 0)
82 		return YPERR_DOMAIN;
83 
84 	yprnk.domain = indomain;
85 	yprnk.map = inmap;
86 	(void)memset(&yprkv, 0, sizeof yprkv);
87 
88 	r = clnt_call(ysd->dom_client, (rpcproc_t)YPPROC_FIRST,
89 	    (xdrproc_t)xdr_ypreq_nokey,
90 	    &yprnk, (xdrproc_t)xdr_ypresp_key_val, &yprkv, _yplib_timeout);
91 	if (r != RPC_SUCCESS) {
92 		if (++nerrs == _yplib_nerrs) {
93 			clnt_perror(ysd->dom_client, "yp_first: clnt_call");
94 			nerrs = 0;
95 		}
96 		ysd->dom_vers = -1;
97 		goto again;
98 	}
99 	if (!(r = ypprot_err(yprkv.status))) {
100 		*outkeylen = yprkv.keydat.dsize;
101 		if ((*outkey = malloc((size_t)(*outkeylen + 1))) == NULL)
102 			r = YPERR_RESRC;
103 		else {
104 			(void)memcpy(*outkey, yprkv.keydat.dptr,
105 			    (size_t)*outkeylen);
106 			(*outkey)[*outkeylen] = '\0';
107 		}
108 		*outvallen = yprkv.valdat.dsize;
109 		if ((*outval = malloc((size_t)(*outvallen + 1))) == NULL)
110 			r = YPERR_RESRC;
111 		else {
112 			(void)memcpy(*outval, yprkv.valdat.dptr,
113 			    (size_t)*outvallen);
114 			(*outval)[*outvallen] = '\0';
115 		}
116 	}
117 	xdr_free((xdrproc_t)xdr_ypresp_key_val, (char *)(void *)&yprkv);
118 	__yp_unbind(ysd);
119 	if (r != 0) {
120 		if (*outkey) {
121 			free(*outkey);
122 			*outkey = NULL;
123 		}
124 		if (*outval) {
125 			free(*outval);
126 			*outval = NULL;
127 		}
128 	}
129 	return r;
130 }
131 
132 int
133 yp_next(indomain, inmap, inkey, inkeylen, outkey, outkeylen, outval, outvallen)
134 	const char     *indomain;
135 	const char     *inmap;
136 	const char     *inkey;
137 	int             inkeylen;
138 	char          **outkey;
139 	int            *outkeylen;
140 	char          **outval;
141 	int            *outvallen;
142 {
143 	struct ypresp_key_val yprkv;
144 	struct ypreq_key yprk;
145 	struct dom_binding *ysd;
146 	int r, nerrs = 0;
147 
148 	if (outkey == NULL || outkeylen == NULL || \
149 	    outval == NULL || outvallen == NULL || \
150 	    inkey == NULL)
151 		return YPERR_BADARGS;
152 	*outkey = *outval = NULL;
153 	*outkeylen = *outvallen = 0;
154 
155 	if (_yp_invalid_domain(indomain))
156 		return YPERR_BADARGS;
157 	if (inmap == NULL || *inmap == '\0'
158 	    || strlen(inmap) > YPMAXMAP)
159 		return YPERR_BADARGS;
160 
161 again:
162 	if (_yp_dobind(indomain, &ysd) != 0)
163 		return YPERR_DOMAIN;
164 
165 	yprk.domain = indomain;
166 	yprk.map = inmap;
167 	yprk.keydat.dptr = inkey;
168 	yprk.keydat.dsize = inkeylen;
169 	(void)memset(&yprkv, 0, sizeof yprkv);
170 
171 	r = clnt_call(ysd->dom_client, (rpcproc_t)YPPROC_NEXT,
172 	    (xdrproc_t)xdr_ypreq_key,
173 	    &yprk, (xdrproc_t)xdr_ypresp_key_val, &yprkv, _yplib_timeout);
174 	if (r != RPC_SUCCESS) {
175 		if (++nerrs == _yplib_nerrs) {
176 			clnt_perror(ysd->dom_client, "yp_next: clnt_call");
177 			nerrs = 0;
178 		}
179 		ysd->dom_vers = -1;
180 		goto again;
181 	}
182 	if (!(r = ypprot_err(yprkv.status))) {
183 		*outkeylen = yprkv.keydat.dsize;
184 		if ((*outkey = malloc((size_t)(*outkeylen + 1))) == NULL)
185 			r = YPERR_RESRC;
186 		else {
187 			(void)memcpy(*outkey, yprkv.keydat.dptr,
188 			    (size_t)*outkeylen);
189 			(*outkey)[*outkeylen] = '\0';
190 		}
191 		*outvallen = yprkv.valdat.dsize;
192 		if ((*outval = malloc((size_t)(*outvallen + 1))) == NULL)
193 			r = YPERR_RESRC;
194 		else {
195 			(void)memcpy(*outval, yprkv.valdat.dptr,
196 			    (size_t)*outvallen);
197 			(*outval)[*outvallen] = '\0';
198 		}
199 	}
200 	xdr_free((xdrproc_t)xdr_ypresp_key_val, (char *)(void *)&yprkv);
201 	__yp_unbind(ysd);
202 	if (r != 0) {
203 		if (*outkey) {
204 			free(*outkey);
205 			*outkey = NULL;
206 		}
207 		if (*outval) {
208 			free(*outval);
209 			*outval = NULL;
210 		}
211 	}
212 	return r;
213 }
214