]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ypset/ypset.c
sys/{x86,amd64}: remove one of doubled ;s
[FreeBSD/FreeBSD.git] / usr.sbin / ypset / ypset.c
1 /*      $OpenBSD: ypset.c,v 1.20 2015/01/16 06:40:23 deraadt Exp $ */
2 /*      $NetBSD: ypset.c,v 1.8 1996/05/13 02:46:33 thorpej Exp $        */
3
4 /*-
5  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6  *
7  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38
39 #include <err.h>
40 #include <netdb.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45
46 #include <rpc/rpc.h>
47 #include <rpc/xdr.h>
48 #include <rpcsvc/yp.h>
49 #include <rpcsvc/ypclnt.h>
50 #include <arpa/inet.h>
51
52 static void
53 usage(void)
54 {
55         fprintf(stderr, "usage: ypset [-d domain] [-h host] server\n");
56         exit(1);
57 }
58
59 static int
60 bind_tohost(struct sockaddr_in *sin, char *dom, char *server)
61 {
62         struct ypbind_setdom ypsd;
63         struct in_addr iaddr;
64         struct hostent *hp;
65         struct timeval tv;
66         CLIENT *client;
67         int sock, port, r;
68
69         port = getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP);
70         if (port == 0)
71                 errx(1, "%s not running ypserv", server);
72         port = htons(port);
73
74         memset(&ypsd, 0, sizeof ypsd);
75
76         if (inet_aton(server, &iaddr) == 0) {
77                 hp = gethostbyname(server);
78                 if (hp == NULL)
79                         errx(1, "can't find address for %s", server);
80                 memmove(&iaddr.s_addr, hp->h_addr, sizeof(iaddr.s_addr));
81         }
82         ypsd.ypsetdom_domain = dom;
83         bcopy(&iaddr.s_addr, &ypsd.ypsetdom_binding.ypbind_binding_addr,
84             sizeof(ypsd.ypsetdom_binding.ypbind_binding_addr));
85         bcopy(&port, &ypsd.ypsetdom_binding.ypbind_binding_port,
86             sizeof(ypsd.ypsetdom_binding.ypbind_binding_port));
87         ypsd.ypsetdom_vers = YPVERS;
88
89         tv.tv_sec = 15;
90         tv.tv_usec = 0;
91         sock = RPC_ANYSOCK;
92         client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
93         if (client == NULL) {
94                 warnx("can't yp_bind, reason: %s", 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                 (xdrproc_t)xdr_ypbind_setdom, &ypsd,
101                 (xdrproc_t)xdr_void, NULL, tv);
102         if (r) {
103                 warnx("cannot ypset for domain %s on host %s: %s"
104                 " - make sure ypbind was started with -ypset or -ypsetme", dom,
105                     server, clnt_sperrno(r));
106                 clnt_destroy(client);
107                 return (YPERR_YPBIND);
108         }
109         clnt_destroy(client);
110         return (0);
111 }
112
113 int
114 main(int argc, char *argv[])
115 {
116         struct sockaddr_in sin;
117         struct hostent *hent;
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(INADDR_LOOPBACK);
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 (inet_aton(optarg, &sin.sin_addr) == 0) {
134                                 hent = gethostbyname(optarg);
135                                 if (hent == NULL)
136                                         errx(1, "host %s unknown\n", optarg);
137                                 bcopy(hent->h_addr, &sin.sin_addr,
138                                     sizeof(sin.sin_addr));
139                         }
140                         break;
141                 default:
142                         usage();
143                 }
144
145         if (optind + 1 != argc)
146                 usage();
147
148         if (bind_tohost(&sin, domainname, argv[optind]))
149                 exit(1);
150         exit(0);
151 }