Merge commit 'crater/master'
[dragonfly.git] / usr.sbin / ypset / ypset.c
blobcfc0e0effff9e0e74de281a6dc384fa04a59548c
1 /*
2 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
3 * All rights reserved.
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.
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.
29 * $FreeBSD: src/usr.sbin/ypset/ypset.c,v 1.5.2.2 2002/02/15 00:47:00 des Exp $
30 * $DragonFly: src/usr.sbin/ypset/ypset.c,v 1.4 2007/11/25 01:28:24 swildner Exp $
33 #include <err.h>
34 #include <netdb.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <rpc/rpc.h>
43 #include <rpc/xdr.h>
44 #include <rpcsvc/yp.h>
45 struct dom_binding{};
46 #include <rpcsvc/ypclnt.h>
47 #include <arpa/inet.h>
49 extern bool_t xdr_domainname();
51 static void
52 usage(void)
54 fprintf(stderr, "usage: ypset [-h host] [-d domain] server\n");
55 exit(1);
58 int
59 bind_tohost(struct sockaddr_in *sin, char *dom, char *server)
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;
69 if ((port = htons(getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP))) == 0)
70 errx(1, "%s not running ypserv", server);
72 bzero(&ypsd, sizeof ypsd);
74 if ((hp = gethostbyname (server)) != NULL) {
75 /* is this the most compatible way?? */
76 bcopy (hp->h_addr_list[0],
77 (u_long *)&ypsd.ypsetdom_binding.ypbind_binding_addr,
78 sizeof (unsigned long));
79 } else if ((long)(server_addr = inet_addr (server)) == -1) {
80 errx(1, "can't find address for %s", server);
81 } else
82 bcopy (&server_addr,
83 (u_long *)&ypsd.ypsetdom_binding.ypbind_binding_addr,
84 sizeof (server_addr));
86 /* strncpy(ypsd.ypsetdom_domain, dom, sizeof ypsd.ypsetdom_domain); */
87 ypsd.ypsetdom_domain = dom;
88 *(u_long *)&ypsd.ypsetdom_binding.ypbind_binding_port = port;
89 ypsd.ypsetdom_vers = YPVERS;
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 warnx("can't yp_bind, reason: %s", yperr_string(YPERR_YPBIND));
97 return (YPERR_YPBIND);
99 client->cl_auth = authunix_create_default();
101 r = clnt_call(client, YPBINDPROC_SETDOM,
102 (xdrproc_t)xdr_ypbind_setdom, &ypsd,
103 (xdrproc_t)xdr_void, NULL, tv);
104 if (r) {
105 warnx("sorry, cannot ypset for domain %s on host", dom);
106 clnt_destroy(client);
107 return (YPERR_YPBIND);
109 clnt_destroy(client);
110 return (0);
114 main(int argc, char **argv)
116 struct sockaddr_in sin;
117 struct hostent *hent;
118 extern char *optarg;
119 extern int optind;
120 char *domainname;
121 int c;
123 yp_get_default_domain(&domainname);
125 bzero(&sin, sizeof sin);
126 sin.sin_family = AF_INET;
127 sin.sin_addr.s_addr = htonl(0x7f000001);
129 while ((c = getopt(argc, argv, "h:d:")) != -1)
130 switch (c) {
131 case 'd':
132 domainname = optarg;
133 break;
134 case 'h':
135 if ((sin.sin_addr.s_addr = inet_addr(optarg)) == -1) {
136 hent = gethostbyname(optarg);
137 if (hent == NULL)
138 errx(1, "host %s unknown", optarg);
139 bcopy(&hent->h_addr_list[0], &sin.sin_addr,
140 sizeof sin.sin_addr);
142 break;
143 default:
144 usage();
147 if (optind + 1 != argc)
148 usage();
150 if (bind_tohost(&sin, domainname, argv[optind]))
151 exit(1);
152 exit(0);