ypset.c revision 1.12
1/*	$NetBSD: ypset.c,v 1.12 2000/04/14 06:26:55 simonb 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#include <sys/cdefs.h>
36#ifndef lint
37__RCSID("$NetBSD: ypset.c,v 1.12 2000/04/14 06:26:55 simonb Exp $");
38#endif
39
40#include <sys/param.h>
41#include <sys/types.h>
42#include <sys/socket.h>
43#include <stdio.h>
44#include <string.h>
45#include <stdlib.h>
46#include <unistd.h>
47#include <err.h>
48#include <netdb.h>
49
50#include <rpc/rpc.h>
51#include <rpc/xdr.h>
52#include <rpcsvc/yp_prot.h>
53#include <rpcsvc/ypclnt.h>
54#include <arpa/inet.h>
55
56extern char *__progname;
57
58int	main __P((int, char *[]));
59static void usage __P((void));
60static void gethostaddr __P((const char *, struct in_addr *));
61static int bind_tohost __P((struct sockaddr_in *, char *, char *));
62
63int
64main(argc, argv)
65	int argc;
66	char *argv[];
67{
68	struct sockaddr_in sin;
69	char *domainname;
70	int c;
71
72	yp_get_default_domain(&domainname);
73
74	(void) memset(&sin, 0, sizeof sin);
75	sin.sin_family = AF_INET;
76	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
77
78	while ((c = getopt(argc, argv, "h:d:")) != -1) {
79		switch(c) {
80		case 'd':
81			domainname = optarg;
82			break;
83
84		case 'h':
85			gethostaddr(optarg, &sin.sin_addr);
86			break;
87
88		default:
89			usage();
90		}
91	}
92
93	if (domainname == NULL)
94		errx(1, "YP domain name not set");
95
96	argc -= optind;
97	argv += optind;
98
99	if (argc != 1)
100		usage();
101
102	return bind_tohost(&sin, domainname, argv[0]) != 0;
103}
104
105static void
106gethostaddr(host, ia)
107	const char *host;
108	struct in_addr *ia;
109{
110	struct hostent *hp;
111
112	if (inet_aton(host, ia) != 0)
113		return;
114
115	hp = gethostbyname(host);
116	if (hp == NULL)
117		errx(1, "Cannot get host address for %s: %s", host,
118		    hstrerror(h_errno));
119	(void) memcpy(ia, hp->h_addr, sizeof(*ia));
120}
121
122static int
123bind_tohost(sin, dom, server)
124	struct sockaddr_in *sin;
125	char *dom, *server;
126{
127	struct ypbind_setdom ypsd;
128	struct timeval tv;
129	CLIENT *client;
130	int sock, port;
131	int r;
132
133	port = htons(getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP));
134	if (port == 0)
135		errx(1, "%s not running ypserv.", server);
136
137	(void) memset(&ypsd, 0, sizeof ypsd);
138
139	gethostaddr(server, &ypsd.ypsetdom_addr);
140
141	(void) strncpy(ypsd.ypsetdom_domain, dom, sizeof ypsd.ypsetdom_domain);
142	ypsd.ypsetdom_domain[sizeof(ypsd.ypsetdom_domain) - 1] = '\0';
143	ypsd.ypsetdom_port = port;
144	ypsd.ypsetdom_vers = YPVERS;
145
146	tv.tv_sec = 15;
147	tv.tv_usec = 0;
148	sock = RPC_ANYSOCK;
149
150	client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
151	if (client == NULL) {
152		warnx("Can't yp_bind: Reason: %s", yperr_string(YPERR_YPBIND));
153		return YPERR_YPBIND;
154	}
155	client->cl_auth = authunix_create_default();
156
157	r = clnt_call(client, YPBINDPROC_SETDOM,
158	    xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv);
159	if (r) {
160		warnx("Cannot ypset for domain %s on host %s: %s.\n",
161		    dom, server, clnt_sperrno(r));
162		clnt_destroy(client);
163		return YPERR_YPBIND;
164	}
165	clnt_destroy(client);
166	return 0;
167}
168
169static void
170usage()
171{
172	(void) fprintf(stderr, "usage: %s [-h host ] [-d domain] server\n",
173	    __progname);
174	exit(1);
175}
176