ypset.c revision 1.11
1/*	$NetBSD: ypset.c,v 1.11 1997/07/18 08:16:58 thorpej 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.11 1997/07/18 08:16:58 thorpej 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	extern char *optarg;
70	extern int optind;
71	char *domainname;
72	int c;
73
74	yp_get_default_domain(&domainname);
75
76	(void) memset(&sin, 0, sizeof sin);
77	sin.sin_family = AF_INET;
78	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
79
80	while ((c = getopt(argc, argv, "h:d:")) != -1) {
81		switch(c) {
82		case 'd':
83			domainname = optarg;
84			break;
85
86		case 'h':
87			gethostaddr(optarg, &sin.sin_addr);
88			break;
89
90		default:
91			usage();
92		}
93	}
94
95	if (domainname == NULL)
96		errx(1, "YP domain name not set");
97
98	argc -= optind;
99	argv += optind;
100
101	if (argc != 1)
102		usage();
103
104	return bind_tohost(&sin, domainname, argv[0]) != 0;
105}
106
107static void
108gethostaddr(host, ia)
109	const char *host;
110	struct in_addr *ia;
111{
112	struct hostent *hp;
113
114	if (inet_aton(host, ia) != 0)
115		return;
116
117	hp = gethostbyname(host);
118	if (hp == NULL)
119		errx(1, "Cannot get host address for %s: %s", host,
120		    hstrerror(h_errno));
121	(void) memcpy(ia, hp->h_addr, sizeof(*ia));
122}
123
124static int
125bind_tohost(sin, dom, server)
126	struct sockaddr_in *sin;
127	char *dom, *server;
128{
129	struct ypbind_setdom ypsd;
130	struct timeval tv;
131	CLIENT *client;
132	int sock, port;
133	int r;
134
135	port = htons(getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP));
136	if (port == 0)
137		errx(1, "%s not running ypserv.", server);
138
139	(void) memset(&ypsd, 0, sizeof ypsd);
140
141	gethostaddr(server, &ypsd.ypsetdom_addr);
142
143	(void) strncpy(ypsd.ypsetdom_domain, dom, sizeof ypsd.ypsetdom_domain);
144	ypsd.ypsetdom_domain[sizeof(ypsd.ypsetdom_domain) - 1] = '\0';
145	ypsd.ypsetdom_port = port;
146	ypsd.ypsetdom_vers = YPVERS;
147
148	tv.tv_sec = 15;
149	tv.tv_usec = 0;
150	sock = RPC_ANYSOCK;
151
152	client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
153	if (client == NULL) {
154		warnx("Can't yp_bind: Reason: %s", yperr_string(YPERR_YPBIND));
155		return YPERR_YPBIND;
156	}
157	client->cl_auth = authunix_create_default();
158
159	r = clnt_call(client, YPBINDPROC_SETDOM,
160	    xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv);
161	if (r) {
162		warnx("Cannot ypset for domain %s on host %s: %s.\n",
163		    dom, server, clnt_sperrno(r));
164		clnt_destroy(client);
165		return YPERR_YPBIND;
166	}
167	clnt_destroy(client);
168	return 0;
169}
170
171static void
172usage()
173{
174	(void) fprintf(stderr, "usage: %s [-h host ] [-d domain] server\n",
175	    __progname);
176	exit(1);
177}
178