ypset.c revision 1.8
1/*	$NetBSD: ypset.c,v 1.8 1996/05/13 02:46:33 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#ifndef LINT
36static char rcsid[] = "$NetBSD: ypset.c,v 1.8 1996/05/13 02:46:33 thorpej 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 <netdb.h>
44#include <rpc/rpc.h>
45#include <rpc/xdr.h>
46#include <rpcsvc/yp_prot.h>
47#include <rpcsvc/ypclnt.h>
48#include <arpa/inet.h>
49
50extern bool_t xdr_domainname();
51
52usage()
53{
54	fprintf(stderr, "Usage:\n");
55	fprintf(stderr, "\typset [-h host ] [-d domain] server\n");
56	exit(1);
57}
58
59bind_tohost(sin, dom, server)
60struct sockaddr_in *sin;
61char *dom, *server;
62{
63	struct ypbind_setdom ypsd;
64	struct timeval tv;
65	struct hostent *hp;
66	CLIENT *client;
67	int sock, port;
68	int r;
69
70	if( (port=htons(getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP))) == 0) {
71		fprintf(stderr, "%s not running ypserv.\n", server);
72		exit(1);
73	}
74
75	bzero(&ypsd, sizeof ypsd);
76
77	if (inet_aton(server, &ypsd.ypsetdom_addr) == 0) {
78		hp = gethostbyname(server);
79		if (hp == NULL) {
80			fprintf(stderr, "ypset: can't find address for %s\n", server);
81			exit(1);
82		}
83		bcopy(hp->h_addr, &ypsd.ypsetdom_addr, sizeof(ypsd.ypsetdom_addr));
84	}
85
86	strncpy(ypsd.ypsetdom_domain, dom, sizeof ypsd.ypsetdom_domain);
87	ypsd.ypsetdom_port = port;
88	ypsd.ypsetdom_vers = YPVERS;
89
90	tv.tv_sec = 15;
91	tv.tv_usec = 0;
92	sock = RPC_ANYSOCK;
93	client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
94	if (client==NULL) {
95		fprintf(stderr, "can't yp_bind: Reason: %s\n",
96			yperr_string(YPERR_YPBIND));
97		return YPERR_YPBIND;
98	}
99	client->cl_auth = authunix_create_default();
100
101	r = clnt_call(client, YPBINDPROC_SETDOM,
102		xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv);
103	if(r) {
104		fprintf(stderr, "Sorry, cannot ypset for domain %s on host.\n", dom);
105		clnt_destroy(client);
106		return YPERR_YPBIND;
107	}
108	clnt_destroy(client);
109	return 0;
110}
111
112int
113main(argc, argv)
114char **argv;
115{
116	struct sockaddr_in sin;
117	struct hostent *hent;
118	extern char *optarg;
119	extern int optind;
120	char *domainname;
121	int c;
122
123	yp_get_default_domain(&domainname);
124
125	bzero(&sin, sizeof sin);
126	sin.sin_family = AF_INET;
127	sin.sin_addr.s_addr = htonl(0x7f000001);
128
129	while( (c=getopt(argc, argv, "h:d:")) != -1)
130		switch(c) {
131		case 'd':
132			domainname = optarg;
133			break;
134		case 'h':
135			if (inet_aton(optarg, &sin.sin_addr) == 0) {
136				hent = gethostbyname(optarg);
137				if (hent == NULL) {
138					fprintf(stderr, "ypset: host %s unknown\n",
139					    optarg);
140					exit(1);
141				}
142				bcopy(&hent->h_addr, &sin.sin_addr,
143				    sizeof(sin.sin_addr));
144			}
145			break;
146		default:
147			usage();
148		}
149
150	if(optind + 1 != argc )
151		usage();
152
153	if (bind_tohost(&sin, domainname, argv[optind]))
154		exit(1);
155	exit(0);
156}
157