11929Swollman/* 21929Swollman * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca> 31929Swollman * All rights reserved. 41929Swollman * 51929Swollman * Redistribution and use in source and binary forms, with or without 61929Swollman * modification, are permitted provided that the following conditions 71929Swollman * are met: 81929Swollman * 1. Redistributions of source code must retain the above copyright 91929Swollman * notice, this list of conditions and the following disclaimer. 101929Swollman * 2. Redistributions in binary form must reproduce the above copyright 111929Swollman * notice, this list of conditions and the following disclaimer in the 121929Swollman * documentation and/or other materials provided with the distribution. 131929Swollman * 3. The name of the author may not be used to endorse or promote 141929Swollman * products derived from this software without specific prior written 151929Swollman * permission. 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$"); 321929Swollman 3330828Scharnier#include <err.h> 3430828Scharnier#include <netdb.h> 3530828Scharnier#include <stdio.h> 36116010Simp#include <stdlib.h> 3730828Scharnier#include <string.h> 3830828Scharnier#include <unistd.h> 391929Swollman#include <sys/param.h> 401929Swollman#include <sys/types.h> 411929Swollman#include <sys/socket.h> 421929Swollman#include <rpc/rpc.h> 431929Swollman#include <rpc/xdr.h> 4412862Swpaul#include <rpcsvc/yp.h> 451929Swollman#include <rpcsvc/ypclnt.h> 461929Swollman#include <arpa/inet.h> 471929Swollman 481929Swollmanextern bool_t xdr_domainname(); 491929Swollman 5030828Scharnierstatic void 5190298Sdesusage(void) 521929Swollman{ 5330828Scharnier fprintf(stderr, "usage: ypset [-h host] [-d domain] server\n"); 541929Swollman exit(1); 551929Swollman} 561929Swollman 5730828Scharnierint 5890298Sdesbind_tohost(struct sockaddr_in *sin, char *dom, char *server) 591929Swollman{ 601929Swollman struct ypbind_setdom ypsd; 611929Swollman struct timeval tv; 621929Swollman struct hostent *hp; 631929Swollman CLIENT *client; 641929Swollman int sock, port; 651929Swollman int r; 661929Swollman unsigned long server_addr; 678857Srgrimes 6890297Sdes if ((port = htons(getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP))) == 0) 6930828Scharnier errx(1, "%s not running ypserv", server); 701929Swollman 711929Swollman bzero(&ypsd, sizeof ypsd); 721929Swollman 7390297Sdes if ((hp = gethostbyname (server)) != NULL) { 741929Swollman /* is this the most compatible way?? */ 7512862Swpaul bcopy (hp->h_addr_list[0], 7612862Swpaul (u_long *)&ypsd.ypsetdom_binding.ypbind_binding_addr, 7712862Swpaul sizeof (unsigned long)); 7890297Sdes } else if ((long)(server_addr = inet_addr (server)) == -1) { 7930828Scharnier errx(1, "can't find address for %s", server); 801929Swollman } else 8112862Swpaul bcopy (&server_addr, 8272955Swpaul (u_long *)&ypsd.ypsetdom_binding.ypbind_binding_addr, 831929Swollman sizeof (server_addr)); 841929Swollman 8512862Swpaul/* strncpy(ypsd.ypsetdom_domain, dom, sizeof ypsd.ypsetdom_domain); */ 8612862Swpaul ypsd.ypsetdom_domain = dom; 8712862Swpaul *(u_long *)&ypsd.ypsetdom_binding.ypbind_binding_port = port; 881929Swollman ypsd.ypsetdom_vers = YPVERS; 898857Srgrimes 901929Swollman tv.tv_sec = 15; 911929Swollman tv.tv_usec = 0; 921929Swollman sock = RPC_ANYSOCK; 931929Swollman client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock); 9490297Sdes if (client == NULL) { 9530828Scharnier warnx("can't yp_bind, reason: %s", yperr_string(YPERR_YPBIND)); 9690297Sdes return (YPERR_YPBIND); 971929Swollman } 981929Swollman client->cl_auth = authunix_create_default(); 991929Swollman 1001929Swollman r = clnt_call(client, YPBINDPROC_SETDOM, 10195658Sdes (xdrproc_t)xdr_ypbind_setdom, &ypsd, 10295658Sdes (xdrproc_t)xdr_void, NULL, tv); 10390297Sdes if (r) { 104167128Sjmallett warnx("sorry, cannot ypset for domain %s on host - make sure ypbind was started with -ypset or -ypsetme", dom); 1051929Swollman clnt_destroy(client); 10690297Sdes return (YPERR_YPBIND); 1071929Swollman } 1081929Swollman clnt_destroy(client); 10990297Sdes return (0); 1101929Swollman} 1111929Swollman 1121929Swollmanint 11390298Sdesmain(int argc, char *argv[]) 1141929Swollman{ 1151929Swollman struct sockaddr_in sin; 1161929Swollman struct hostent *hent; 1171929Swollman char *domainname; 1181929Swollman int c; 1191929Swollman 1201929Swollman yp_get_default_domain(&domainname); 1211929Swollman 1221929Swollman bzero(&sin, sizeof sin); 1231929Swollman sin.sin_family = AF_INET; 12474347Salfred sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 1251929Swollman 12690297Sdes while ((c = getopt(argc, argv, "h:d:")) != -1) 12790297Sdes switch (c) { 1281929Swollman case 'd': 1291929Swollman domainname = optarg; 1301929Swollman break; 1311929Swollman case 'h': 13290297Sdes if ((sin.sin_addr.s_addr = inet_addr(optarg)) == -1) { 1331929Swollman hent = gethostbyname(optarg); 13490297Sdes if (hent == NULL) 13530828Scharnier errx(1, "host %s unknown", optarg); 136125613Siedowse bcopy(hent->h_addr_list[0], &sin.sin_addr, 1371929Swollman sizeof sin.sin_addr); 1381929Swollman } 1391929Swollman break; 1401929Swollman default: 1411929Swollman usage(); 1421929Swollman } 1431929Swollman 14490297Sdes if (optind + 1 != argc) 1451929Swollman usage(); 1461929Swollman 1471929Swollman if (bind_tohost(&sin, domainname, argv[optind])) 1481929Swollman exit(1); 1491929Swollman exit(0); 1501929Swollman} 151