xref: /freebsd/libexec/ypxfr/ypxfr_misc.c (revision 1b56bb4ca7afcf0f76feac28ddb97f5b78923913)
1 /*
2  * Copyright (c) 1995
3  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	$Id: ypxfr_misc.c,v 1.9 1996/10/25 15:58:15 wpaul Exp $
33  */
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <sys/param.h>
39 #include <rpc/rpc.h>
40 #include <rpcsvc/yp.h>
41 struct dom_binding {};
42 #include <rpcsvc/ypclnt.h>
43 #include "ypxfr_extern.h"
44 
45 #ifndef lint
46 static const char rcsid[] = "$Id: ypxfr_misc.c,v 1.9 1996/10/25 15:58:15 wpaul Exp $";
47 #endif
48 
49 char *ypxfrerr_string(code)
50 	ypxfrstat code;
51 {
52 	switch(code) {
53 	case YPXFR_SUCC:
54 		return ("Map successfully transfered");
55 		break;
56 	case YPXFR_AGE:
57 		return ("Master's version not newer");
58 		break;
59 	case YPXFR_NOMAP:
60 		return ("No such map in server's domain");
61 		break;
62 	case YPXFR_NODOM:
63 		return ("Domain not supported by server");
64 		break;
65 	case YPXFR_RSRC:
66 		return ("Local resource allocation failure");
67 		break;
68 	case YPXFR_RPC:
69 		return ("RPC failure talking to server");
70 		break;
71 	case YPXFR_MADDR:
72 		return ("Could not get master server address");
73 		break;
74 	case YPXFR_YPERR:
75 		return ("NIS server/map database error");
76 		break;
77 	case YPXFR_BADARGS:
78 		return ("Request arguments bad");
79 		break;
80 	case YPXFR_DBM:
81 		return ("Local database operation failed");
82 		break;
83 	case YPXFR_FILE:
84 		return ("Local file I/O operation failed");
85 		break;
86 	case YPXFR_SKEW:
87 		return ("Map version skew during transfer");
88 		break;
89 	case YPXFR_CLEAR:
90 		return ("Couldn't send \"clear\" request to local ypserv");
91 		break;
92 	case YPXFR_FORCE:
93 		return ("No local order number in map -- use -f flag");
94 		break;
95 	case YPXFR_XFRERR:
96 		return ("General ypxfr error");
97 		break;
98 	case YPXFR_REFUSED:
99 		return ("Transfer request refused by ypserv");
100 		break;
101 	default:
102 		return ("Unknown error code");
103 		break;
104 	}
105 }
106 
107 /*
108  * These are wrappers for the usual yp_master() and yp_order() functions.
109  * They can use either local yplib functions (the real yp_master() and
110  * yp_order()) or do direct RPCs to a specified server. The latter is
111  * necessary if ypxfr is run on a machine that isn't configured as an
112  * NIS client (this can happen very easily: a given machine need not be
113  * an NIS client in order to be an NIS server).
114  */
115 
116 /*
117  * Careful: yp_master() returns a pointer to a dynamically allocated
118  * buffer. Calling ypproc_master_2() ourselves also returns a pointer
119  * to dynamically allocated memory, though this time it's memory
120  * allocated by the XDR routines. We have to rememver to free() or
121  * xdr_free() the memory as required to avoid leaking memory.
122  */
123 char *ypxfr_get_master(domain,map,source,yplib)
124 	char *domain;
125 	char *map;
126 	char *source;
127 	const int yplib;
128 {
129 	static char mastername[MAXPATHLEN + 2];
130 
131 	bzero((char *)&mastername, sizeof(mastername));
132 
133 	if (yplib) {
134 		int res;
135 		char *master;
136 		if ((res = yp_master(domain, map, &master))) {
137 			switch (res) {
138 			case YPERR_DOMAIN:
139 				yp_errno = YPXFR_NODOM;
140 				break;
141 			case YPERR_MAP:
142 				yp_errno = YPXFR_NOMAP;
143 				break;
144 			case YPERR_YPERR:
145 			default:
146 				yp_errno = YPXFR_YPERR;
147 				break;
148 			}
149 			return(NULL);
150 		} else {
151 			snprintf(mastername, sizeof(mastername), "%s", master);
152 			free(master);
153 			return((char *)&mastername);
154 		}
155 	} else {
156 		CLIENT *clnt;
157 		ypresp_master *resp;
158 		ypreq_nokey req;
159 
160 		if ((clnt = clnt_create(source,YPPROG,YPVERS,"udp")) == NULL) {
161 			yp_error("%s",clnt_spcreateerror("failed to \
162 create udp handle to ypserv"));
163 			yp_errno = YPXFR_RPC;
164 			return(NULL);
165 		}
166 
167 		req.map = map;
168 		req.domain = domain;
169 		if ((resp = ypproc_master_2(&req, clnt)) == NULL) {
170 			yp_error("%s",clnt_sperror(clnt,"YPPROC_MASTER \
171 failed"));
172 			clnt_destroy(clnt);
173 			yp_errno = YPXFR_RPC;
174 			return(NULL);
175 		}
176 		clnt_destroy(clnt);
177 		if (resp->stat != YP_TRUE) {
178 			switch (resp->stat) {
179 			case YP_NODOM:
180 				yp_errno = YPXFR_NODOM;
181 				break;
182 			case YP_NOMAP:
183 				yp_errno = YPXFR_NOMAP;
184 				break;
185 			case YP_YPERR:
186 			default:
187 				yp_errno = YPXFR_YPERR;
188 				break;
189 			}
190 			return(NULL);
191 		}
192 		snprintf(mastername, sizeof(mastername), "%s", resp->peer);
193 /*		xdr_free(xdr_ypresp_master, (char *)&resp); */
194 		return((char *)&mastername);
195 	}
196 }
197 
198 unsigned long ypxfr_get_order(domain, map, source, yplib)
199 	char *domain;
200 	char *map;
201 	char *source;
202 	const int yplib;
203 {
204 	if (yplib) {
205 		unsigned long order;
206 		int res;
207 		if ((res = yp_order(domain, map, (int *)&order))) {
208 			switch (res) {
209 			case YPERR_DOMAIN:
210 				yp_errno = YPXFR_NODOM;
211 				break;
212 			case YPERR_MAP:
213 				yp_errno = YPXFR_NOMAP;
214 				break;
215 			case YPERR_YPERR:
216 			default:
217 				yp_errno = YPXFR_YPERR;
218 				break;
219 			}
220 			return(0);
221 		} else
222 			return(order);
223 	} else {
224 		CLIENT *clnt;
225 		ypresp_order *resp;
226 		ypreq_nokey req;
227 
228 		if ((clnt = clnt_create(source,YPPROG,YPVERS,"udp")) == NULL) {
229 			yp_error("%s",clnt_spcreateerror("couldn't create \
230 udp handle to ypserv"));
231 			yp_errno = YPXFR_RPC;
232 			return(0);
233 		}
234 		req.map = map;
235 		req.domain = domain;
236 		if ((resp = ypproc_order_2(&req, clnt)) == NULL) {
237 			yp_error("%s", clnt_sperror(clnt, "YPPROC_ORDER \
238 failed"));
239 			clnt_destroy(clnt);
240 			yp_errno = YPXFR_RPC;
241 			return(0);
242 		}
243 		clnt_destroy(clnt);
244 		if (resp->stat != YP_TRUE) {
245 			switch (resp->stat) {
246 			case YP_NODOM:
247 				yp_errno = YPXFR_NODOM;
248 				break;
249 			case YP_NOMAP:
250 				yp_errno = YPXFR_NOMAP;
251 				break;
252 			case YP_YPERR:
253 			default:
254 				yp_errno = YPXFR_YPERR;
255 				break;
256 			}
257 			return(0);
258 		}
259 		return(resp->ordernum);
260 	}
261 }
262 
263 int ypxfr_match(server, domain, map, key, keylen)
264 	char *server;
265 	char *domain;
266 	char *map;
267 	char *key;
268 	unsigned long keylen;
269 {
270 	ypreq_key ypkey;
271 	ypresp_val *ypval;
272 	CLIENT *clnt;
273 	static char buf[YPMAXRECORD + 2];
274 
275 	bzero((char *)buf, sizeof(buf));
276 
277 	if ((clnt = clnt_create(server, YPPROG,YPVERS,"udp")) == NULL) {
278 		yp_error("failed to create UDP handle: %s",
279 					clnt_spcreateerror(server));
280 		return(0);
281 	}
282 
283 	ypkey.domain = domain;
284 	ypkey.map = map;
285 	ypkey.key.keydat_len = keylen;
286 	ypkey.key.keydat_val = key;
287 
288 	if ((ypval = ypproc_match_2(&ypkey, clnt)) == NULL) {
289 		clnt_destroy(clnt);
290 		yp_error("%s: %s", server,
291 				clnt_sperror(clnt,"YPPROC_MATCH failed"));
292 		return(0);
293 	}
294 
295 	clnt_destroy(clnt);
296 
297 	if (ypval->stat != YP_TRUE) {
298 		xdr_free(xdr_ypresp_val, (char *)ypval);
299 		return(0);
300 	}
301 
302 	xdr_free(xdr_ypresp_val, (char *)ypval);
303 
304 	return(1);
305 }
306