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