Use bzip instead of gzip, rename the tar file to make it more obvious that
[dragonfly/vkernel-mp.git] / usr.sbin / ypset / ypset.c
blobd439ece910a640bd06466e68af1272f0a2609929
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.3 2005/11/24 23:23:03 swildner Exp $
33 #include <err.h>
34 #include <netdb.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <rpc/rpc.h>
42 #include <rpc/xdr.h>
43 #include <rpcsvc/yp.h>
44 struct dom_binding{};
45 #include <rpcsvc/ypclnt.h>
46 #include <arpa/inet.h>
48 extern bool_t xdr_domainname();
50 static void
51 usage(void)
53 fprintf(stderr, "usage: ypset [-h host] [-d domain] server\n");
54 exit(1);
57 int
58 bind_tohost(struct sockaddr_in *sin, char *dom, char *server)
60 struct ypbind_setdom ypsd;
61 struct timeval tv;
62 struct hostent *hp;
63 CLIENT *client;
64 int sock, port;
65 int r;
66 unsigned long server_addr;
68 if ((port = htons(getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP))) == 0)
69 errx(1, "%s not running ypserv", server);
71 bzero(&ypsd, sizeof ypsd);
73 if ((hp = gethostbyname (server)) != NULL) {
74 /* is this the most compatible way?? */
75 bcopy (hp->h_addr_list[0],
76 (u_long *)&ypsd.ypsetdom_binding.ypbind_binding_addr,
77 sizeof (unsigned long));
78 } else if ((long)(server_addr = inet_addr (server)) == -1) {
79 errx(1, "can't find address for %s", server);
80 } else
81 bcopy (&server_addr,
82 (u_long *)&ypsd.ypsetdom_binding.ypbind_binding_addr,
83 sizeof (server_addr));
85 /* strncpy(ypsd.ypsetdom_domain, dom, sizeof ypsd.ypsetdom_domain); */
86 ypsd.ypsetdom_domain = dom;
87 *(u_long *)&ypsd.ypsetdom_binding.ypbind_binding_port = port;
88 ypsd.ypsetdom_vers = YPVERS;
90 tv.tv_sec = 15;
91 tv.tv_usec = 0;
92 sock = RPC_ANYSOCK;
93 client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
94 if (client == NULL) {
95 warnx("can't yp_bind, reason: %s", yperr_string(YPERR_YPBIND));
96 return (YPERR_YPBIND);
98 client->cl_auth = authunix_create_default();
100 r = clnt_call(client, YPBINDPROC_SETDOM,
101 xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv);
102 if (r) {
103 warnx("sorry, cannot ypset for domain %s on host", dom);
104 clnt_destroy(client);
105 return (YPERR_YPBIND);
107 clnt_destroy(client);
108 return (0);
112 main(int argc, char **argv)
114 struct sockaddr_in sin;
115 struct hostent *hent;
116 extern char *optarg;
117 extern int optind;
118 char *domainname;
119 int c;
121 yp_get_default_domain(&domainname);
123 bzero(&sin, sizeof sin);
124 sin.sin_family = AF_INET;
125 sin.sin_addr.s_addr = htonl(0x7f000001);
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 errx(1, "host %s unknown", optarg);
137 bcopy(&hent->h_addr_list[0], &sin.sin_addr,
138 sizeof sin.sin_addr);
140 break;
141 default:
142 usage();
145 if (optind + 1 != argc)
146 usage();
148 if (bind_tohost(&sin, domainname, argv[optind]))
149 exit(1);
150 exit(0);