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