ypset.c revision 12862
1/*
2 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 *    products derived from this software without specific prior written
15 *    permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef LINT
31static char rcsid[] = "ypset.c,v 1.3 1993/06/12 00:02:37 deraadt Exp";
32#endif
33
34#include <sys/param.h>
35#include <sys/types.h>
36#include <sys/socket.h>
37#include <stdio.h>
38#include <netdb.h>
39#include <rpc/rpc.h>
40#include <rpc/xdr.h>
41#include <rpcsvc/yp.h>
42struct dom_binding{};
43#include <rpcsvc/ypclnt.h>
44#include <arpa/inet.h>
45
46extern bool_t xdr_domainname();
47
48usage()
49{
50	fprintf(stderr, "Usage:\n");
51	fprintf(stderr, "\typset [-h host ] [-d domain] server\n");
52	exit(1);
53}
54
55bind_tohost(sin, dom, server)
56struct sockaddr_in *sin;
57char *dom, *server;
58{
59	struct ypbind_setdom ypsd;
60	struct timeval tv;
61	struct hostent *hp;
62	CLIENT *client;
63	int sock, port;
64	int r;
65	unsigned long server_addr;
66
67	if( (port=htons(getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP))) == 0) {
68		fprintf(stderr, "%s not running ypserv.\n", server);
69		exit(1);
70	}
71
72	bzero(&ypsd, sizeof ypsd);
73
74	if( (hp = gethostbyname (server)) != NULL ) {
75		/* is this the most compatible way?? */
76		bcopy (hp->h_addr_list[0],
77		       (u_long *)&ypsd.ypsetdom_binding.ypbind_binding_addr,
78		       sizeof (unsigned long));
79	} else if( (long)(server_addr = inet_addr (server)) == -1) {
80		fprintf(stderr, "can't find address for %s\n", server);
81		exit(1);
82	} else
83		bcopy (&server_addr,
84		       *(u_long *)&ypsd.ypsetdom_binding.ypbind_binding_addr,
85		       sizeof (server_addr));
86
87/*	strncpy(ypsd.ypsetdom_domain, dom, sizeof ypsd.ypsetdom_domain); */
88	ypsd.ypsetdom_domain = dom;
89	*(u_long *)&ypsd.ypsetdom_binding.ypbind_binding_port = port;
90	ypsd.ypsetdom_vers = YPVERS;
91
92	tv.tv_sec = 15;
93	tv.tv_usec = 0;
94	sock = RPC_ANYSOCK;
95	client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
96	if (client==NULL) {
97		fprintf(stderr, "can't yp_bind: Reason: %s\n",
98			yperr_string(YPERR_YPBIND));
99		return YPERR_YPBIND;
100	}
101	client->cl_auth = authunix_create_default();
102
103	r = clnt_call(client, YPBINDPROC_SETDOM,
104		xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv);
105	if(r) {
106		fprintf(stderr, "Sorry, cannot ypset for domain %s on host.\n", dom);
107		clnt_destroy(client);
108		return YPERR_YPBIND;
109	}
110	clnt_destroy(client);
111	return 0;
112}
113
114int
115main(argc, argv)
116char **argv;
117{
118	struct sockaddr_in sin;
119	struct hostent *hent;
120	extern char *optarg;
121	extern int optind;
122	char *domainname;
123	int c;
124
125	yp_get_default_domain(&domainname);
126
127	bzero(&sin, sizeof sin);
128	sin.sin_family = AF_INET;
129	sin.sin_addr.s_addr = htonl(0x7f000001);
130
131	while( (c=getopt(argc, argv, "h:d:")) != -1)
132		switch(c) {
133		case 'd':
134			domainname = optarg;
135			break;
136		case 'h':
137			if( (sin.sin_addr.s_addr=inet_addr(optarg)) == -1) {
138				hent = gethostbyname(optarg);
139				if(hent==NULL) {
140					fprintf(stderr, "ypset: host %s unknown\n",
141						optarg);
142					exit(1);
143				}
144				bcopy(&hent->h_addr_list[0], &sin.sin_addr,
145					sizeof sin.sin_addr);
146			}
147			break;
148		default:
149			usage();
150		}
151
152	if(optind + 1 != argc )
153		usage();
154
155	if (bind_tohost(&sin, domainname, argv[optind]))
156		exit(1);
157	exit(0);
158}
159