ypset.c revision 1.10
1/*	$NetBSD: ypset.c,v 1.10 1996/06/18 20:32:25 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
19 *    products derived from this software without specific prior written
20 *    permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
26 * 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
32 * SUCH DAMAGE.
33 */
34
35#ifndef LINT
36static char rcsid[] = "$NetBSD: ypset.c,v 1.10 1996/06/18 20:32:25 christos Exp $";
37#endif
38
39#include <sys/param.h>
40#include <sys/types.h>
41#include <sys/socket.h>
42#include <stdio.h>
43#include <string.h>
44#include <stdlib.h>
45#include <unistd.h>
46#include <err.h>
47#include <netdb.h>
48#include <rpc/rpc.h>
49#include <rpc/xdr.h>
50#include <rpcsvc/yp_prot.h>
51#include <rpcsvc/ypclnt.h>
52#include <arpa/inet.h>
53
54extern char *__progname;
55
56static void usage __P((void));
57static void gethostaddr __P((const char *, struct in_addr *));
58static int bind_tohost __P((struct sockaddr_in *, char *, char *));
59
60static void
61usage()
62{
63	(void) fprintf(stderr, "Usage: %s [-h host ] [-d domain] server\n",
64	    __progname);
65	exit(1);
66}
67
68static void
69gethostaddr(host, ia)
70	const char *host;
71	struct in_addr *ia;
72{
73	struct hostent *hp;
74
75	if (inet_aton(host, ia) != 0)
76		return;
77
78	hp = gethostbyname(host);
79	if (hp == NULL)
80		errx(1, "Cannot get host address for %s: %s", host,
81		    hstrerror(h_errno));
82	(void) memcpy(ia, hp->h_addr, sizeof(*ia));
83}
84
85
86static int
87bind_tohost(sin, dom, server)
88	struct sockaddr_in *sin;
89	char *dom, *server;
90{
91	struct ypbind_setdom ypsd;
92	struct timeval tv;
93	CLIENT *client;
94	int sock, port;
95	int r;
96
97	port = htons(getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP));
98	if (port == 0)
99		errx(1, "%s not running ypserv.", server);
100
101	(void) memset(&ypsd, 0, sizeof ypsd);
102
103	gethostaddr(server, &ypsd.ypsetdom_addr);
104
105	(void) strncpy(ypsd.ypsetdom_domain, dom, sizeof ypsd.ypsetdom_domain);
106	ypsd.ypsetdom_domain[sizeof(ypsd.ypsetdom_domain) - 1] = '\0';
107	ypsd.ypsetdom_port = port;
108	ypsd.ypsetdom_vers = YPVERS;
109
110	tv.tv_sec = 15;
111	tv.tv_usec = 0;
112	sock = RPC_ANYSOCK;
113
114	client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
115	if (client == NULL) {
116		warnx("Can't yp_bind: Reason: %s", yperr_string(YPERR_YPBIND));
117		return YPERR_YPBIND;
118	}
119	client->cl_auth = authunix_create_default();
120
121	r = clnt_call(client, YPBINDPROC_SETDOM,
122	    xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv);
123	if (r) {
124		warnx("Cannot ypset for domain %s on host %s: %s.\n",
125		    dom, server, clnt_sperrno(r));
126		clnt_destroy(client);
127		return YPERR_YPBIND;
128	}
129	clnt_destroy(client);
130	return 0;
131}
132
133int
134main(argc, argv)
135	int argc;
136	char *argv[];
137{
138	struct sockaddr_in sin;
139	extern char *optarg;
140	extern int optind;
141	char *domainname;
142	int c;
143
144	yp_get_default_domain(&domainname);
145
146	(void) memset(&sin, 0, sizeof sin);
147	sin.sin_family = AF_INET;
148	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
149
150	while((c = getopt(argc, argv, "h:d:")) != -1)
151		switch(c) {
152		case 'd':
153			domainname = optarg;
154			break;
155		case 'h':
156			gethostaddr(optarg, &sin.sin_addr);
157			break;
158		default:
159			usage();
160		}
161
162	if(optind + 1 != argc)
163		usage();
164
165	return bind_tohost(&sin, domainname, argv[optind]) != 0;
166}
167