1e5763f00SMarcelo Araujo /* $OpenBSD: ypset.c,v 1.20 2015/01/16 06:40:23 deraadt Exp $ */
2e5763f00SMarcelo Araujo /* $NetBSD: ypset.c,v 1.8 1996/05/13 02:46:33 thorpej Exp $ */
3e5763f00SMarcelo Araujo
41de7b4b8SPedro F. Giffuni /*-
5*b61a5730SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
61de7b4b8SPedro F. Giffuni *
7e5763f00SMarcelo Araujo * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com>
8d832d691SGarrett Wollman * All rights reserved.
9d832d691SGarrett Wollman *
10d832d691SGarrett Wollman * Redistribution and use in source and binary forms, with or without
11d832d691SGarrett Wollman * modification, are permitted provided that the following conditions
12d832d691SGarrett Wollman * are met:
13d832d691SGarrett Wollman * 1. Redistributions of source code must retain the above copyright
14d832d691SGarrett Wollman * notice, this list of conditions and the following disclaimer.
15d832d691SGarrett Wollman * 2. Redistributions in binary form must reproduce the above copyright
16d832d691SGarrett Wollman * notice, this list of conditions and the following disclaimer in the
17d832d691SGarrett Wollman * documentation and/or other materials provided with the distribution.
18d832d691SGarrett Wollman *
19d832d691SGarrett Wollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20d832d691SGarrett Wollman * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21d832d691SGarrett Wollman * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22d832d691SGarrett Wollman * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23d832d691SGarrett Wollman * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24d832d691SGarrett Wollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25d832d691SGarrett Wollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26d832d691SGarrett Wollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27d832d691SGarrett Wollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28d832d691SGarrett Wollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29d832d691SGarrett Wollman * SUCH DAMAGE.
30d832d691SGarrett Wollman */
31d832d691SGarrett Wollman
32e5763f00SMarcelo Araujo #include <sys/param.h>
33e5763f00SMarcelo Araujo #include <sys/types.h>
34e5763f00SMarcelo Araujo #include <sys/socket.h>
35e5763f00SMarcelo Araujo
360da8bf4aSPhilippe Charnier #include <err.h>
370da8bf4aSPhilippe Charnier #include <netdb.h>
380da8bf4aSPhilippe Charnier #include <stdio.h>
398aacd912SWarner Losh #include <stdlib.h>
400da8bf4aSPhilippe Charnier #include <string.h>
410da8bf4aSPhilippe Charnier #include <unistd.h>
42e5763f00SMarcelo Araujo
43d832d691SGarrett Wollman #include <rpc/rpc.h>
44d832d691SGarrett Wollman #include <rpc/xdr.h>
457db881e1SBill Paul #include <rpcsvc/yp.h>
46d832d691SGarrett Wollman #include <rpcsvc/ypclnt.h>
47d832d691SGarrett Wollman #include <arpa/inet.h>
48d832d691SGarrett Wollman
490da8bf4aSPhilippe Charnier static void
usage(void)50dc584ddbSDag-Erling Smørgrav usage(void)
51d832d691SGarrett Wollman {
52e5763f00SMarcelo Araujo fprintf(stderr, "usage: ypset [-d domain] [-h host] server\n");
53d832d691SGarrett Wollman exit(1);
54d832d691SGarrett Wollman }
55d832d691SGarrett Wollman
56e5763f00SMarcelo Araujo static int
bind_tohost(struct sockaddr_in * sin,char * dom,char * server)57dc584ddbSDag-Erling Smørgrav bind_tohost(struct sockaddr_in *sin, char *dom, char *server)
58d832d691SGarrett Wollman {
59d832d691SGarrett Wollman struct ypbind_setdom ypsd;
60e5763f00SMarcelo Araujo struct in_addr iaddr;
61d832d691SGarrett Wollman struct hostent *hp;
62e5763f00SMarcelo Araujo struct timeval tv;
63d832d691SGarrett Wollman CLIENT *client;
64e5763f00SMarcelo Araujo int sock, port, r;
65d832d691SGarrett Wollman
66e5763f00SMarcelo Araujo port = getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP);
67e5763f00SMarcelo Araujo if (port == 0)
680da8bf4aSPhilippe Charnier errx(1, "%s not running ypserv", server);
69e5763f00SMarcelo Araujo port = htons(port);
70d832d691SGarrett Wollman
71e5763f00SMarcelo Araujo memset(&ypsd, 0, sizeof ypsd);
72d832d691SGarrett Wollman
73e5763f00SMarcelo Araujo if (inet_aton(server, &iaddr) == 0) {
74e5763f00SMarcelo Araujo hp = gethostbyname(server);
75e5763f00SMarcelo Araujo if (hp == NULL)
760da8bf4aSPhilippe Charnier errx(1, "can't find address for %s", server);
77e5763f00SMarcelo Araujo memmove(&iaddr.s_addr, hp->h_addr, sizeof(iaddr.s_addr));
78e5763f00SMarcelo Araujo }
797db881e1SBill Paul ypsd.ypsetdom_domain = dom;
80e5763f00SMarcelo Araujo bcopy(&iaddr.s_addr, &ypsd.ypsetdom_binding.ypbind_binding_addr,
81e5763f00SMarcelo Araujo sizeof(ypsd.ypsetdom_binding.ypbind_binding_addr));
82e5763f00SMarcelo Araujo bcopy(&port, &ypsd.ypsetdom_binding.ypbind_binding_port,
83e5763f00SMarcelo Araujo sizeof(ypsd.ypsetdom_binding.ypbind_binding_port));
84d832d691SGarrett Wollman ypsd.ypsetdom_vers = YPVERS;
85d832d691SGarrett Wollman
86d832d691SGarrett Wollman tv.tv_sec = 15;
87d832d691SGarrett Wollman tv.tv_usec = 0;
88d832d691SGarrett Wollman sock = RPC_ANYSOCK;
89d832d691SGarrett Wollman client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
90d832d691SGarrett Wollman if (client == NULL) {
910da8bf4aSPhilippe Charnier warnx("can't yp_bind, reason: %s", yperr_string(YPERR_YPBIND));
92ed4d1c46SDag-Erling Smørgrav return (YPERR_YPBIND);
93d832d691SGarrett Wollman }
94d832d691SGarrett Wollman client->cl_auth = authunix_create_default();
95d832d691SGarrett Wollman
96d832d691SGarrett Wollman r = clnt_call(client, YPBINDPROC_SETDOM,
97f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_ypbind_setdom, &ypsd,
98f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_void, NULL, tv);
99d832d691SGarrett Wollman if (r) {
100e5763f00SMarcelo Araujo warnx("cannot ypset for domain %s on host %s: %s"
101e5763f00SMarcelo Araujo " - make sure ypbind was started with -ypset or -ypsetme", dom,
102e5763f00SMarcelo Araujo server, clnt_sperrno(r));
103d832d691SGarrett Wollman clnt_destroy(client);
104ed4d1c46SDag-Erling Smørgrav return (YPERR_YPBIND);
105d832d691SGarrett Wollman }
106d832d691SGarrett Wollman clnt_destroy(client);
107ed4d1c46SDag-Erling Smørgrav return (0);
108d832d691SGarrett Wollman }
109d832d691SGarrett Wollman
110d832d691SGarrett Wollman int
main(int argc,char * argv[])111dc584ddbSDag-Erling Smørgrav main(int argc, char *argv[])
112d832d691SGarrett Wollman {
113d832d691SGarrett Wollman struct sockaddr_in sin;
114d832d691SGarrett Wollman struct hostent *hent;
115d832d691SGarrett Wollman char *domainname;
116d832d691SGarrett Wollman int c;
117d832d691SGarrett Wollman
118d832d691SGarrett Wollman yp_get_default_domain(&domainname);
119d832d691SGarrett Wollman
120d832d691SGarrett Wollman bzero(&sin, sizeof sin);
121d832d691SGarrett Wollman sin.sin_family = AF_INET;
122e8f6dea1SAlfred Perlstein sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
123d832d691SGarrett Wollman
124d832d691SGarrett Wollman while ((c = getopt(argc, argv, "h:d:")) != -1)
125d832d691SGarrett Wollman switch (c) {
126d832d691SGarrett Wollman case 'd':
127d832d691SGarrett Wollman domainname = optarg;
128d832d691SGarrett Wollman break;
129d832d691SGarrett Wollman case 'h':
130e5763f00SMarcelo Araujo if (inet_aton(optarg, &sin.sin_addr) == 0) {
131d832d691SGarrett Wollman hent = gethostbyname(optarg);
1320da8bf4aSPhilippe Charnier if (hent == NULL)
133e5763f00SMarcelo Araujo errx(1, "host %s unknown\n", optarg);
134e5763f00SMarcelo Araujo bcopy(hent->h_addr, &sin.sin_addr,
135e5763f00SMarcelo Araujo sizeof(sin.sin_addr));
136d832d691SGarrett Wollman }
137d832d691SGarrett Wollman break;
138d832d691SGarrett Wollman default:
139d832d691SGarrett Wollman usage();
140d832d691SGarrett Wollman }
141d832d691SGarrett Wollman
142d832d691SGarrett Wollman if (optind + 1 != argc)
143d832d691SGarrett Wollman usage();
144d832d691SGarrett Wollman
145d832d691SGarrett Wollman if (bind_tohost(&sin, domainname, argv[optind]))
146d832d691SGarrett Wollman exit(1);
147d832d691SGarrett Wollman exit(0);
148d832d691SGarrett Wollman }
149