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