xref: /netbsd/usr.sbin/ypset/ypset.c (revision 697a41c3)
1 /*	$NetBSD: ypset.c,v 1.17 2011/08/27 22:41:35 joerg 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #ifndef lint
31 __RCSID("$NetBSD: ypset.c,v 1.17 2011/08/27 22:41:35 joerg 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 <string.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <err.h>
42 #include <netdb.h>
43 
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 
50 __dead static void usage(void);
51 static void gethostaddr(const char *, struct in_addr *);
52 static int bind_tohost(struct sockaddr_in *, char *, char *);
53 
54 int
main(int argc,char * argv[])55 main(int argc, char *argv[])
56 {
57 	struct sockaddr_in sin;
58 	char *domainname;
59 	int c;
60 
61 	yp_get_default_domain(&domainname);
62 
63 	(void) memset(&sin, 0, sizeof sin);
64 	sin.sin_family = AF_INET;
65 	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
66 
67 	while ((c = getopt(argc, argv, "h:d:")) != -1) {
68 		switch(c) {
69 		case 'd':
70 			domainname = optarg;
71 			break;
72 
73 		case 'h':
74 			gethostaddr(optarg, &sin.sin_addr);
75 			break;
76 
77 		default:
78 			usage();
79 		}
80 	}
81 
82 	if (domainname == NULL)
83 		errx(1, "YP domain name not set");
84 
85 	argc -= optind;
86 	argv += optind;
87 
88 	if (argc != 1)
89 		usage();
90 
91 	return bind_tohost(&sin, domainname, argv[0]) != 0;
92 }
93 
94 static void
gethostaddr(const char * host,struct in_addr * ia)95 gethostaddr(const char *host, struct in_addr *ia)
96 {
97 	struct hostent *hp;
98 
99 	if (inet_aton(host, ia) != 0)
100 		return;
101 
102 	hp = gethostbyname(host);
103 	if (hp == NULL)
104 		errx(1, "Cannot get host address for %s: %s", host,
105 		    hstrerror(h_errno));
106 	(void) memcpy(ia, hp->h_addr, sizeof(*ia));
107 }
108 
109 static int
bind_tohost(struct sockaddr_in * sin,char * dom,char * server)110 bind_tohost(struct sockaddr_in *sin, char *dom, char *server)
111 {
112 	struct ypbind_setdom ypsd;
113 	struct timeval tv;
114 	CLIENT *client;
115 	int sock, port;
116 	int r;
117 
118 	port = htons(getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP));
119 	if (port == 0)
120 		errx(1, "%s not running ypserv.", server);
121 
122 	(void) memset(&ypsd, 0, sizeof ypsd);
123 
124 	gethostaddr(server, &ypsd.ypsetdom_addr);
125 
126 	(void) strlcpy(ypsd.ypsetdom_domain, dom, sizeof ypsd.ypsetdom_domain);
127 	ypsd.ypsetdom_port = port;
128 	ypsd.ypsetdom_vers = YPVERS;
129 
130 	tv.tv_sec = 15;
131 	tv.tv_usec = 0;
132 	sock = RPC_ANYSOCK;
133 
134 	client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
135 	if (client == NULL) {
136 		warnx("Can't yp_bind: Reason: %s", yperr_string(YPERR_YPBIND));
137 		return YPERR_YPBIND;
138 	}
139 	client->cl_auth = authunix_create_default();
140 
141 	r = clnt_call(client, YPBINDPROC_SETDOM,
142 	    xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv);
143 	if (r) {
144 		warnx("Cannot ypset for domain %s on host %s: %s.",
145 		    dom, server, clnt_sperrno(r));
146 		clnt_destroy(client);
147 		return YPERR_YPBIND;
148 	}
149 	clnt_destroy(client);
150 	return 0;
151 }
152 
153 static void
usage(void)154 usage(void)
155 {
156 	(void) fprintf(stderr, "usage: %s [-h host ] [-d domain] server\n",
157 	    getprogname());
158 	exit(1);
159 }
160