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