xref: /freebsd/libexec/ypxfr/ypxfr_getmap.c (revision 1d386b48)
1df57947fSPedro F. Giffuni /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
4665823d0SBill Paul  * Copyright (c) 1995
5665823d0SBill Paul  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
6665823d0SBill Paul  *
7665823d0SBill Paul  * Redistribution and use in source and binary forms, with or without
8665823d0SBill Paul  * modification, are permitted provided that the following conditions
9665823d0SBill Paul  * are met:
10665823d0SBill Paul  * 1. Redistributions of source code must retain the above copyright
11665823d0SBill Paul  *    notice, this list of conditions and the following disclaimer.
12665823d0SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
13665823d0SBill Paul  *    notice, this list of conditions and the following disclaimer in the
14665823d0SBill Paul  *    documentation and/or other materials provided with the distribution.
15665823d0SBill Paul  * 3. All advertising materials mentioning features or use of this software
16665823d0SBill Paul  *    must display the following acknowledgement:
17665823d0SBill Paul  *	This product includes software developed by Bill Paul.
18665823d0SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
19665823d0SBill Paul  *    may be used to endorse or promote products derived from this software
20665823d0SBill Paul  *    without specific prior written permission.
21665823d0SBill Paul  *
22665823d0SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23665823d0SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24665823d0SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25665823d0SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26665823d0SBill Paul  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27665823d0SBill Paul  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28665823d0SBill Paul  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29665823d0SBill Paul  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30665823d0SBill Paul  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31665823d0SBill Paul  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32665823d0SBill Paul  * SUCH DAMAGE.
33665823d0SBill Paul  */
3427eed7e3SPhilippe Charnier 
3522e9bc15SDavid E. O'Brien #include <sys/cdefs.h>
36665823d0SBill Paul #include <stdio.h>
37665823d0SBill Paul #include <time.h>
3827eed7e3SPhilippe Charnier #include <sys/types.h>
39665823d0SBill Paul #include <rpc/rpc.h>
40665823d0SBill Paul #include <rpc/xdr.h>
41665823d0SBill Paul #include <rpcsvc/yp.h>
42665823d0SBill Paul #include "ypxfr_extern.h"
43665823d0SBill Paul 
44ed4d1c46SDag-Erling Smørgrav extern bool_t xdr_ypresp_all_seq(XDR *, unsigned long *);
45665823d0SBill Paul 
461d08a87aSMark Johnston extern int (*ypresp_allfn)();
471d08a87aSMark Johnston extern void *ypresp_data;
48665823d0SBill Paul extern DB *specdbp;
49db83a391SDimitry Andric extern enum ypstat yp_errno;
50665823d0SBill Paul 
51665823d0SBill Paul /*
52665823d0SBill Paul  * This is largely the same as yp_all() except we do the transfer
53665823d0SBill Paul  * from a specific server without the aid of ypbind(8). We need to
54665823d0SBill Paul  * be able to specify the source host explicitly since ypxfr may
55665823d0SBill Paul  * only transfer maps from the NIS master server for any given domain.
56665823d0SBill Paul  * However, if we use the libc version of yp_all(), we could end up
57665823d0SBill Paul  * talking to one of the slaves instead. We do need to dig into libc
58665823d0SBill Paul  * a little though, since it contains the magic XDR function we need.
59665823d0SBill Paul  */
60dc584ddbSDag-Erling Smørgrav int
ypxfr_get_map(char * map,char * domain,char * host,int (* callback)(int,char *,int,char *,int,char *))61dc584ddbSDag-Erling Smørgrav ypxfr_get_map(char *map, char *domain, char *host,
62dc584ddbSDag-Erling Smørgrav     int (*callback)(int, char *, int, char *, int, char*))
63665823d0SBill Paul {
64665823d0SBill Paul 	CLIENT *clnt;
65665823d0SBill Paul 	ypreq_nokey req;
66665823d0SBill Paul 	unsigned long status;
67665823d0SBill Paul 	struct timeval timeout;
68665823d0SBill Paul 
69665823d0SBill Paul 	timeout.tv_usec = 0;
70665823d0SBill Paul 	timeout.tv_sec = 10;
71665823d0SBill Paul 
72665823d0SBill Paul 	/* YPPROC_ALL is a TCP service */
73665823d0SBill Paul 	if ((clnt = clnt_create(host, YPPROG, YPVERS, "tcp")) == NULL) {
7416deb43aSBill Paul 		yp_error("%s", clnt_spcreateerror("failed to \
7516deb43aSBill Paul create tcp handle"));
763eb9425cSDimitry Andric 		yp_errno = (enum ypstat)YPXFR_YPERR;
77665823d0SBill Paul 		return(1);
78665823d0SBill Paul 	}
79665823d0SBill Paul 
80665823d0SBill Paul 	req.domain = domain;
81665823d0SBill Paul 	req.map = map;
82665823d0SBill Paul 	ypresp_allfn = callback;
83665823d0SBill Paul 	ypresp_data = NULL;
84665823d0SBill Paul 
85f249dbccSDag-Erling Smørgrav 	(void)clnt_call(clnt, YPPROC_ALL, (xdrproc_t)xdr_ypreq_nokey, &req,
86f249dbccSDag-Erling Smørgrav 	    (xdrproc_t)xdr_ypresp_all_seq, &status, timeout);
87665823d0SBill Paul 
88665823d0SBill Paul 	clnt_destroy(clnt);
89665823d0SBill Paul 
90bc66df81SBill Paul 	if (status == YP_NOMORE)
91bc66df81SBill Paul 		return(0);
92bc66df81SBill Paul 
93665823d0SBill Paul 	if (status != YP_TRUE) {
943eb9425cSDimitry Andric 		yp_errno = (enum ypstat)YPXFR_YPERR;
95665823d0SBill Paul 		return(1);
96665823d0SBill Paul 	}
97665823d0SBill Paul 
98665823d0SBill Paul 	return(0);
99665823d0SBill Paul }
100