Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / ypset / ypset.c
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 const char rcsid[] =
32   "$FreeBSD: src/usr.sbin/ypset/ypset.c,v 1.5.2.2 2002/02/15 00:47:00 des Exp $";
33 #endif /* not lint */
34
35 #include <err.h>
36 #include <netdb.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <rpc/rpc.h>
44 #include <rpc/xdr.h>
45 #include <rpcsvc/yp.h>
46 struct dom_binding{};
47 #include <rpcsvc/ypclnt.h>
48 #include <arpa/inet.h>
49
50 extern bool_t xdr_domainname();
51
52 static void
53 usage()
54 {
55         fprintf(stderr, "usage: ypset [-h host] [-d domain] server\n");
56         exit(1);
57 }
58
59 int
60 bind_tohost(sin, dom, server)
61 struct sockaddr_in *sin;
62 char *dom, *server;
63 {
64         struct ypbind_setdom ypsd;
65         struct timeval tv;
66         struct hostent *hp;
67         CLIENT *client;
68         int sock, port;
69         int r;
70         unsigned long server_addr;
71
72         if ((port = htons(getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP))) == 0)
73                 errx(1, "%s not running ypserv", server);
74
75         bzero(&ypsd, sizeof ypsd);
76
77         if ((hp = gethostbyname (server)) != NULL) {
78                 /* is this the most compatible way?? */
79                 bcopy (hp->h_addr_list[0],
80                        (u_long *)&ypsd.ypsetdom_binding.ypbind_binding_addr,
81                        sizeof (unsigned long));
82         } else if ((long)(server_addr = inet_addr (server)) == -1) {
83                 errx(1, "can't find address for %s", server);
84         } else
85                 bcopy (&server_addr,
86                        (u_long *)&ypsd.ypsetdom_binding.ypbind_binding_addr,
87                        sizeof (server_addr));
88
89 /*      strncpy(ypsd.ypsetdom_domain, dom, sizeof ypsd.ypsetdom_domain); */
90         ypsd.ypsetdom_domain = dom;
91         *(u_long *)&ypsd.ypsetdom_binding.ypbind_binding_port = port;
92         ypsd.ypsetdom_vers = YPVERS;
93
94         tv.tv_sec = 15;
95         tv.tv_usec = 0;
96         sock = RPC_ANYSOCK;
97         client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
98         if (client == NULL) {
99                 warnx("can't yp_bind, reason: %s", yperr_string(YPERR_YPBIND));
100                 return (YPERR_YPBIND);
101         }
102         client->cl_auth = authunix_create_default();
103
104         r = clnt_call(client, YPBINDPROC_SETDOM,
105                 xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv);
106         if (r) {
107                 warnx("sorry, cannot ypset for domain %s on host", dom);
108                 clnt_destroy(client);
109                 return (YPERR_YPBIND);
110         }
111         clnt_destroy(client);
112         return (0);
113 }
114
115 int
116 main(argc, argv)
117 char **argv;
118 {
119         struct sockaddr_in sin;
120         struct hostent *hent;
121         extern char *optarg;
122         extern int optind;
123         char *domainname;
124         int c;
125
126         yp_get_default_domain(&domainname);
127
128         bzero(&sin, sizeof sin);
129         sin.sin_family = AF_INET;
130         sin.sin_addr.s_addr = htonl(0x7f000001);
131
132         while ((c = getopt(argc, argv, "h:d:")) != -1)
133                 switch (c) {
134                 case 'd':
135                         domainname = optarg;
136                         break;
137                 case 'h':
138                         if ((sin.sin_addr.s_addr = inet_addr(optarg)) == -1) {
139                                 hent = gethostbyname(optarg);
140                                 if (hent == NULL)
141                                         errx(1, "host %s unknown", optarg);
142                                 bcopy(&hent->h_addr_list[0], &sin.sin_addr,
143                                         sizeof sin.sin_addr);
144                         }
145                         break;
146                 default:
147                         usage();
148                 }
149
150         if (optind + 1 != argc)
151                 usage();
152
153         if (bind_tohost(&sin, domainname, argv[optind]))
154                 exit(1);
155         exit(0);
156 }