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 4330449Seadler/*- 5330449Seadler * SPDX-License-Identifier: BSD-2-Clause-NetBSD 6330449Seadler * 7286610Saraujo * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com> 81929Swollman * All rights reserved. 91929Swollman * 101929Swollman * Redistribution and use in source and binary forms, with or without 111929Swollman * modification, are permitted provided that the following conditions 121929Swollman * are met: 131929Swollman * 1. Redistributions of source code must retain the above copyright 141929Swollman * notice, this list of conditions and the following disclaimer. 151929Swollman * 2. Redistributions in binary form must reproduce the above copyright 161929Swollman * notice, this list of conditions and the following disclaimer in the 171929Swollman * documentation and/or other materials provided with the distribution. 181929Swollman * 191929Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 201929Swollman * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 211929Swollman * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 221929Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 231929Swollman * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 241929Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 251929Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 261929Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 271929Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 281929Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 291929Swollman * SUCH DAMAGE. 301929Swollman */ 311929Swollman 32114601Sobrien#include <sys/cdefs.h> 33114601Sobrien__FBSDID("$FreeBSD: stable/11/usr.sbin/ypset/ypset.c 330449 2018-03-05 07:26:05Z eadler $"); 341929Swollman 35286610Saraujo#include <sys/param.h> 36286610Saraujo#include <sys/types.h> 37286610Saraujo#include <sys/socket.h> 38286610Saraujo 3930828Scharnier#include <err.h> 4030828Scharnier#include <netdb.h> 4130828Scharnier#include <stdio.h> 42116010Simp#include <stdlib.h> 4330828Scharnier#include <string.h> 4430828Scharnier#include <unistd.h> 45286610Saraujo 461929Swollman#include <rpc/rpc.h> 471929Swollman#include <rpc/xdr.h> 4812862Swpaul#include <rpcsvc/yp.h> 491929Swollman#include <rpcsvc/ypclnt.h> 501929Swollman#include <arpa/inet.h> 511929Swollman 5230828Scharnierstatic void 5390298Sdesusage(void) 541929Swollman{ 55286610Saraujo fprintf(stderr, "usage: ypset [-d domain] [-h host] server\n"); 561929Swollman exit(1); 571929Swollman} 581929Swollman 59286610Saraujostatic int 6090298Sdesbind_tohost(struct sockaddr_in *sin, char *dom, char *server) 611929Swollman{ 621929Swollman struct ypbind_setdom ypsd; 63286610Saraujo struct in_addr iaddr; 64286610Saraujo struct hostent *hp; 651929Swollman struct timeval tv; 661929Swollman CLIENT *client; 67286610Saraujo int sock, port, r; 688857Srgrimes 69286610Saraujo port = getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP); 70286610Saraujo if (port == 0) 7130828Scharnier errx(1, "%s not running ypserv", server); 72286610Saraujo port = htons(port); 731929Swollman 74286610Saraujo memset(&ypsd, 0, sizeof ypsd); 751929Swollman 76286610Saraujo if (inet_aton(server, &iaddr) == 0) { 77286610Saraujo hp = gethostbyname(server); 78286610Saraujo if (hp == NULL) 79286610Saraujo errx(1, "can't find address for %s", server); 80286610Saraujo memmove(&iaddr.s_addr, hp->h_addr, sizeof(iaddr.s_addr)); 81286610Saraujo } 8212862Swpaul ypsd.ypsetdom_domain = dom; 83286610Saraujo bcopy(&iaddr.s_addr, &ypsd.ypsetdom_binding.ypbind_binding_addr, 84286610Saraujo sizeof(ypsd.ypsetdom_binding.ypbind_binding_addr)); 85286610Saraujo bcopy(&port, &ypsd.ypsetdom_binding.ypbind_binding_port, 86286610Saraujo sizeof(ypsd.ypsetdom_binding.ypbind_binding_port)); 871929Swollman ypsd.ypsetdom_vers = YPVERS; 888857Srgrimes 891929Swollman tv.tv_sec = 15; 901929Swollman tv.tv_usec = 0; 911929Swollman sock = RPC_ANYSOCK; 921929Swollman client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock); 9390297Sdes if (client == NULL) { 9430828Scharnier warnx("can't yp_bind, reason: %s", yperr_string(YPERR_YPBIND)); 9590297Sdes return (YPERR_YPBIND); 961929Swollman } 971929Swollman client->cl_auth = authunix_create_default(); 981929Swollman 991929Swollman r = clnt_call(client, YPBINDPROC_SETDOM, 10095658Sdes (xdrproc_t)xdr_ypbind_setdom, &ypsd, 10195658Sdes (xdrproc_t)xdr_void, NULL, tv); 10290297Sdes if (r) { 103286610Saraujo warnx("cannot ypset for domain %s on host %s: %s" 104286610Saraujo " - make sure ypbind was started with -ypset or -ypsetme", dom, 105286610Saraujo server, clnt_sperrno(r)); 1061929Swollman clnt_destroy(client); 10790297Sdes return (YPERR_YPBIND); 1081929Swollman } 1091929Swollman clnt_destroy(client); 11090297Sdes return (0); 1111929Swollman} 1121929Swollman 1131929Swollmanint 11490298Sdesmain(int argc, char *argv[]) 1151929Swollman{ 1161929Swollman struct sockaddr_in sin; 1171929Swollman struct hostent *hent; 1181929Swollman char *domainname; 1191929Swollman int c; 1201929Swollman 1211929Swollman yp_get_default_domain(&domainname); 1221929Swollman 1231929Swollman bzero(&sin, sizeof sin); 1241929Swollman sin.sin_family = AF_INET; 12574347Salfred sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 1261929Swollman 12790297Sdes while ((c = getopt(argc, argv, "h:d:")) != -1) 128286611Saraujo switch (c) { 1291929Swollman case 'd': 1301929Swollman domainname = optarg; 1311929Swollman break; 1321929Swollman case 'h': 133286610Saraujo if (inet_aton(optarg, &sin.sin_addr) == 0) { 1341929Swollman hent = gethostbyname(optarg); 13590297Sdes if (hent == NULL) 136286610Saraujo errx(1, "host %s unknown\n", optarg); 137286610Saraujo bcopy(hent->h_addr, &sin.sin_addr, 138286610Saraujo sizeof(sin.sin_addr)); 1391929Swollman } 1401929Swollman break; 1411929Swollman default: 1421929Swollman usage(); 1431929Swollman } 1441929Swollman 14590297Sdes if (optind + 1 != argc) 1461929Swollman usage(); 1471929Swollman 1481929Swollman if (bind_tohost(&sin, domainname, argv[optind])) 1491929Swollman exit(1); 1501929Swollman exit(0); 1511929Swollman} 152