1286610Saraujo/*	$OpenBSD: ypset.c,v 1.20 2015/01/16 06:40:23 deraadt Exp $ */
2286610Saraujo/*	$NetBSD: ypset.c,v 1.8 1996/05/13 02:46:33 thorpej Exp $	*/
3286610Saraujo
41929Swollman/*
5286610Saraujo * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com>
61929Swollman * All rights reserved.
71929Swollman *
81929Swollman * Redistribution and use in source and binary forms, with or without
91929Swollman * modification, are permitted provided that the following conditions
101929Swollman * are met:
111929Swollman * 1. Redistributions of source code must retain the above copyright
121929Swollman *    notice, this list of conditions and the following disclaimer.
131929Swollman * 2. Redistributions in binary form must reproduce the above copyright
141929Swollman *    notice, this list of conditions and the following disclaimer in the
151929Swollman *    documentation and/or other materials provided with the distribution.
161929Swollman *
171929Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
181929Swollman * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
191929Swollman * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201929Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
211929Swollman * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221929Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231929Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241929Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251929Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261929Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271929Swollman * SUCH DAMAGE.
281929Swollman */
291929Swollman
30114601Sobrien#include <sys/cdefs.h>
31114601Sobrien__FBSDID("$FreeBSD: releng/11.0/usr.sbin/ypset/ypset.c 286611 2015-08-11 01:45:17Z araujo $");
321929Swollman
33286610Saraujo#include <sys/param.h>
34286610Saraujo#include <sys/types.h>
35286610Saraujo#include <sys/socket.h>
36286610Saraujo
3730828Scharnier#include <err.h>
3830828Scharnier#include <netdb.h>
3930828Scharnier#include <stdio.h>
40116010Simp#include <stdlib.h>
4130828Scharnier#include <string.h>
4230828Scharnier#include <unistd.h>
43286610Saraujo
441929Swollman#include <rpc/rpc.h>
451929Swollman#include <rpc/xdr.h>
4612862Swpaul#include <rpcsvc/yp.h>
471929Swollman#include <rpcsvc/ypclnt.h>
481929Swollman#include <arpa/inet.h>
491929Swollman
5030828Scharnierstatic void
5190298Sdesusage(void)
521929Swollman{
53286610Saraujo	fprintf(stderr, "usage: ypset [-d domain] [-h host] server\n");
541929Swollman	exit(1);
551929Swollman}
561929Swollman
57286610Saraujostatic int
5890298Sdesbind_tohost(struct sockaddr_in *sin, char *dom, char *server)
591929Swollman{
601929Swollman	struct ypbind_setdom ypsd;
61286610Saraujo	struct in_addr iaddr;
62286610Saraujo	struct hostent *hp;
631929Swollman	struct timeval tv;
641929Swollman	CLIENT *client;
65286610Saraujo	int sock, port, r;
668857Srgrimes
67286610Saraujo	port = getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP);
68286610Saraujo	if (port == 0)
6930828Scharnier		errx(1, "%s not running ypserv", server);
70286610Saraujo	port = htons(port);
711929Swollman
72286610Saraujo	memset(&ypsd, 0, sizeof ypsd);
731929Swollman
74286610Saraujo	if (inet_aton(server, &iaddr) == 0) {
75286610Saraujo		hp = gethostbyname(server);
76286610Saraujo		if (hp == NULL)
77286610Saraujo			errx(1, "can't find address for %s", server);
78286610Saraujo		memmove(&iaddr.s_addr, hp->h_addr, sizeof(iaddr.s_addr));
79286610Saraujo	}
8012862Swpaul	ypsd.ypsetdom_domain = dom;
81286610Saraujo	bcopy(&iaddr.s_addr, &ypsd.ypsetdom_binding.ypbind_binding_addr,
82286610Saraujo	    sizeof(ypsd.ypsetdom_binding.ypbind_binding_addr));
83286610Saraujo	bcopy(&port, &ypsd.ypsetdom_binding.ypbind_binding_port,
84286610Saraujo	    sizeof(ypsd.ypsetdom_binding.ypbind_binding_port));
851929Swollman	ypsd.ypsetdom_vers = YPVERS;
868857Srgrimes
871929Swollman	tv.tv_sec = 15;
881929Swollman	tv.tv_usec = 0;
891929Swollman	sock = RPC_ANYSOCK;
901929Swollman	client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
9190297Sdes	if (client == NULL) {
9230828Scharnier		warnx("can't yp_bind, reason: %s", yperr_string(YPERR_YPBIND));
9390297Sdes		return (YPERR_YPBIND);
941929Swollman	}
951929Swollman	client->cl_auth = authunix_create_default();
961929Swollman
971929Swollman	r = clnt_call(client, YPBINDPROC_SETDOM,
9895658Sdes		(xdrproc_t)xdr_ypbind_setdom, &ypsd,
9995658Sdes		(xdrproc_t)xdr_void, NULL, tv);
10090297Sdes	if (r) {
101286610Saraujo		warnx("cannot ypset for domain %s on host %s: %s"
102286610Saraujo                " - make sure ypbind was started with -ypset or -ypsetme", dom,
103286610Saraujo		    server, clnt_sperrno(r));
1041929Swollman		clnt_destroy(client);
10590297Sdes		return (YPERR_YPBIND);
1061929Swollman	}
1071929Swollman	clnt_destroy(client);
10890297Sdes	return (0);
1091929Swollman}
1101929Swollman
1111929Swollmanint
11290298Sdesmain(int argc, char *argv[])
1131929Swollman{
1141929Swollman	struct sockaddr_in sin;
1151929Swollman	struct hostent *hent;
1161929Swollman	char *domainname;
1171929Swollman	int c;
1181929Swollman
1191929Swollman	yp_get_default_domain(&domainname);
1201929Swollman
1211929Swollman	bzero(&sin, sizeof sin);
1221929Swollman	sin.sin_family = AF_INET;
12374347Salfred	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1241929Swollman
12590297Sdes	while ((c = getopt(argc, argv, "h:d:")) != -1)
126286611Saraujo		switch (c) {
1271929Swollman		case 'd':
1281929Swollman			domainname = optarg;
1291929Swollman			break;
1301929Swollman		case 'h':
131286610Saraujo			if (inet_aton(optarg, &sin.sin_addr) == 0) {
1321929Swollman				hent = gethostbyname(optarg);
13390297Sdes				if (hent == NULL)
134286610Saraujo					errx(1, "host %s unknown\n", optarg);
135286610Saraujo				bcopy(hent->h_addr, &sin.sin_addr,
136286610Saraujo				    sizeof(sin.sin_addr));
1371929Swollman			}
1381929Swollman			break;
1391929Swollman		default:
1401929Swollman			usage();
1411929Swollman		}
1421929Swollman
14390297Sdes	if (optind + 1 != argc)
1441929Swollman		usage();
1451929Swollman
1461929Swollman	if (bind_tohost(&sin, domainname, argv[optind]))
1471929Swollman		exit(1);
1481929Swollman	exit(0);
1491929Swollman}
150