2 * SPDX-License-Identifier: BSD-4-Clause
5 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
44 #include <rpcsvc/yp.h>
45 #include <rpcsvc/ypclnt.h>
46 #include "ypxfr_extern.h"
49 ypxfrerr_string(ypxfrstat code)
53 return ("Map successfully transferred");
56 return ("Master's version not newer");
59 return ("No such map in server's domain");
62 return ("Domain not supported by server");
65 return ("Local resource allocation failure");
68 return ("RPC failure talking to server");
71 return ("Could not get master server address");
74 return ("NIS server/map database error");
77 return ("Request arguments bad");
80 return ("Local database operation failed");
83 return ("Local file I/O operation failed");
86 return ("Map version skew during transfer");
89 return ("Couldn't send \"clear\" request to local ypserv");
92 return ("No local order number in map -- use -f flag");
95 return ("General ypxfr error");
98 return ("Transfer request refused by ypserv");
101 return ("Unknown error code");
107 * These are wrappers for the usual yp_master() and yp_order() functions.
108 * They can use either local yplib functions (the real yp_master() and
109 * yp_order()) or do direct RPCs to a specified server. The latter is
110 * necessary if ypxfr is run on a machine that isn't configured as an
111 * NIS client (this can happen very easily: a given machine need not be
112 * an NIS client in order to be an NIS server).
116 * Careful: yp_master() returns a pointer to a dynamically allocated
117 * buffer. Calling ypproc_master_2() ourselves also returns a pointer
118 * to dynamically allocated memory, though this time it's memory
119 * allocated by the XDR routines. We have to rememver to free() or
120 * xdr_free() the memory as required to avoid leaking memory.
123 ypxfr_get_master(char *domain, char *map, char *source, const int yplib)
125 static char mastername[MAXPATHLEN + 2];
127 bzero((char *)&mastername, sizeof(mastername));
132 if ((res = yp_master(domain, map, &master))) {
135 yp_errno = (enum ypstat)YPXFR_NODOM;
138 yp_errno = (enum ypstat)YPXFR_NOMAP;
142 yp_errno = (enum ypstat)YPXFR_YPERR;
147 snprintf(mastername, sizeof(mastername), "%s", master);
149 return((char *)&mastername);
156 if ((clnt = clnt_create(source,YPPROG,YPVERS,"udp")) == NULL) {
157 yp_error("%s",clnt_spcreateerror("failed to \
158 create udp handle to ypserv"));
159 yp_errno = (enum ypstat)YPXFR_RPC;
165 if ((resp = ypproc_master_2(&req, clnt)) == NULL) {
166 yp_error("%s",clnt_sperror(clnt,"YPPROC_MASTER \
169 yp_errno = (enum ypstat)YPXFR_RPC;
173 if (resp->stat != YP_TRUE) {
174 switch (resp->stat) {
176 yp_errno = (enum ypstat)YPXFR_NODOM;
179 yp_errno = (enum ypstat)YPXFR_NOMAP;
183 yp_errno = (enum ypstat)YPXFR_YPERR;
188 snprintf(mastername, sizeof(mastername), "%s", resp->peer);
189 /* xdr_free(xdr_ypresp_master, (char *)&resp); */
190 return((char *)&mastername);
195 ypxfr_get_order(char *domain, char *map, char *source, const int yplib)
200 if ((res = yp_order(domain, map, &order))) {
203 yp_errno = (enum ypstat)YPXFR_NODOM;
206 yp_errno = (enum ypstat)YPXFR_NOMAP;
210 yp_errno = (enum ypstat)YPXFR_YPERR;
221 if ((clnt = clnt_create(source,YPPROG,YPVERS,"udp")) == NULL) {
222 yp_error("%s",clnt_spcreateerror("couldn't create \
223 udp handle to ypserv"));
224 yp_errno = (enum ypstat)YPXFR_RPC;
229 if ((resp = ypproc_order_2(&req, clnt)) == NULL) {
230 yp_error("%s", clnt_sperror(clnt, "YPPROC_ORDER \
233 yp_errno = (enum ypstat)YPXFR_RPC;
237 if (resp->stat != YP_TRUE) {
238 switch (resp->stat) {
240 yp_errno = (enum ypstat)YPXFR_NODOM;
243 yp_errno = (enum ypstat)YPXFR_NOMAP;
247 yp_errno = (enum ypstat)YPXFR_YPERR;
252 return(resp->ordernum);
257 ypxfr_match(char *server, char *domain, char *map, char *key,
258 unsigned long keylen)
263 static char buf[YPMAXRECORD + 2];
265 bzero(buf, sizeof(buf));
267 if ((clnt = clnt_create(server, YPPROG,YPVERS,"udp")) == NULL) {
268 yp_error("failed to create UDP handle: %s",
269 clnt_spcreateerror(server));
273 ypkey.domain = domain;
275 ypkey.key.keydat_len = keylen;
276 ypkey.key.keydat_val = key;
278 if ((ypval = ypproc_match_2(&ypkey, clnt)) == NULL) {
280 yp_error("%s: %s", server,
281 clnt_sperror(clnt,"YPPROC_MATCH failed"));
287 if (ypval->stat != YP_TRUE) {
288 xdr_free((xdrproc_t)xdr_ypresp_val, ypval);
292 xdr_free((xdrproc_t)xdr_ypresp_val, ypval);