xref: /netbsd-src/lib/libc/yp/yp_first.c (revision c41a4eebefede43f6950f838a387dc18c6a431bf)
1 /*	$NetBSD: yp_first.c,v 1.8 1997/07/21 14:09:26 jtc 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.8 1997/07/21 14:09:26 jtc 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 || outval == NULL)
70 		return YPERR_BADARGS;
71 	*outkey = *outval = NULL;
72 	*outkeylen = *outvallen = 0;
73 	if (_yp_invalid_domain(indomain))
74 		return YPERR_BADARGS;
75 	if (inmap == NULL || *inmap == '\0'
76 	    || strlen(inmap) > YPMAXMAP)
77 		return YPERR_BADARGS;
78 
79 again:
80 	if (_yp_dobind(indomain, &ysd) != 0)
81 		return YPERR_DOMAIN;
82 
83 	yprnk.domain = indomain;
84 	yprnk.map = inmap;
85 	(void)memset(&yprkv, 0, sizeof yprkv);
86 
87 	r = clnt_call(ysd->dom_client, YPPROC_FIRST,
88 		   xdr_ypreq_nokey, &yprnk, xdr_ypresp_key_val, &yprkv,
89 		   _yplib_timeout);
90 	if (r != RPC_SUCCESS) {
91 		if (++nerrs == _yplib_nerrs) {
92 			clnt_perror(ysd->dom_client, "yp_first: clnt_call");
93 			nerrs = 0;
94 		}
95 		ysd->dom_vers = -1;
96 		goto again;
97 	}
98 	if (!(r = ypprot_err(yprkv.status))) {
99 		*outkeylen = yprkv.keydat.dsize;
100 		if ((*outkey = malloc(*outkeylen + 1)) == NULL)
101 			r = YPERR_RESRC;
102 		else {
103 			(void)memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
104 			(*outkey)[*outkeylen] = '\0';
105 		}
106 		*outvallen = yprkv.valdat.dsize;
107 		if ((*outval = malloc(*outvallen + 1)) == NULL)
108 			r = YPERR_RESRC;
109 		else {
110 			(void)memcpy(*outval, yprkv.valdat.dptr, *outvallen);
111 			(*outval)[*outvallen] = '\0';
112 		}
113 	}
114 	xdr_free(xdr_ypresp_key_val, (char *) &yprkv);
115 	__yp_unbind(ysd);
116 	if (r != 0) {
117 		if (*outkey) {
118 			free(*outkey);
119 			*outkey = NULL;
120 		}
121 		if (*outval) {
122 			free(*outval);
123 			*outval = NULL;
124 		}
125 	}
126 	return r;
127 }
128 
129 int
130 yp_next(indomain, inmap, inkey, inkeylen, outkey, outkeylen, outval, outvallen)
131 	const char     *indomain;
132 	const char     *inmap;
133 	const char     *inkey;
134 	int             inkeylen;
135 	char          **outkey;
136 	int            *outkeylen;
137 	char          **outval;
138 	int            *outvallen;
139 {
140 	struct ypresp_key_val yprkv;
141 	struct ypreq_key yprk;
142 	struct dom_binding *ysd;
143 	int r, nerrs = 0;
144 
145 	if (outkey == NULL || outval == NULL)
146 		return YPERR_BADARGS;
147 	*outkey = *outval = NULL;
148 	*outkeylen = *outvallen = 0;
149 
150 	if (_yp_invalid_domain(indomain))
151 		return YPERR_BADARGS;
152 	if (inmap == NULL || *inmap == '\0'
153 	    || strlen(inmap) > YPMAXMAP)
154 		return YPERR_BADARGS;
155 
156 again:
157 	if (_yp_dobind(indomain, &ysd) != 0)
158 		return YPERR_DOMAIN;
159 
160 	yprk.domain = indomain;
161 	yprk.map = inmap;
162 	yprk.keydat.dptr = inkey;
163 	yprk.keydat.dsize = inkeylen;
164 	(void)memset(&yprkv, 0, sizeof yprkv);
165 
166 	r = clnt_call(ysd->dom_client, YPPROC_NEXT,
167 		      xdr_ypreq_key, &yprk, xdr_ypresp_key_val, &yprkv,
168 		      _yplib_timeout);
169 	if (r != RPC_SUCCESS) {
170 		if (++nerrs == _yplib_nerrs) {
171 			clnt_perror(ysd->dom_client, "yp_next: clnt_call");
172 			nerrs = 0;
173 		}
174 		ysd->dom_vers = -1;
175 		goto again;
176 	}
177 	if (!(r = ypprot_err(yprkv.status))) {
178 		*outkeylen = yprkv.keydat.dsize;
179 		if ((*outkey = malloc(*outkeylen + 1)) == NULL)
180 			r = YPERR_RESRC;
181 		else {
182 			(void)memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
183 			(*outkey)[*outkeylen] = '\0';
184 		}
185 		*outvallen = yprkv.valdat.dsize;
186 		if ((*outval = malloc(*outvallen + 1)) == NULL)
187 			r = YPERR_RESRC;
188 		else {
189 			(void)memcpy(*outval, yprkv.valdat.dptr, *outvallen);
190 			(*outval)[*outvallen] = '\0';
191 		}
192 	}
193 	xdr_free(xdr_ypresp_key_val, (char *) &yprkv);
194 	__yp_unbind(ysd);
195 	if (r != 0) {
196 		if (*outkey) {
197 			free(*outkey);
198 			*outkey = NULL;
199 		}
200 		if (*outval) {
201 			free(*outval);
202 			*outval = NULL;
203 		}
204 	}
205 	return r;
206 }
207