ypset.c revision 1.13
11.13Scgd/* $NetBSD: ypset.c,v 1.13 2001/02/19 23:22:53 cgd Exp $ */ 21.8Sthorpej 31.2Sderaadt/* 41.5Sderaadt * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca> 51.2Sderaadt * All rights reserved. 61.2Sderaadt * 71.2Sderaadt * Redistribution and use in source and binary forms, with or without 81.2Sderaadt * modification, are permitted provided that the following conditions 91.2Sderaadt * are met: 101.2Sderaadt * 1. Redistributions of source code must retain the above copyright 111.2Sderaadt * notice, this list of conditions and the following disclaimer. 121.2Sderaadt * 2. Redistributions in binary form must reproduce the above copyright 131.2Sderaadt * notice, this list of conditions and the following disclaimer in the 141.2Sderaadt * documentation and/or other materials provided with the distribution. 151.5Sderaadt * 3. All advertising materials mentioning features or use of this software 161.5Sderaadt * must display the following acknowledgement: 171.5Sderaadt * This product includes software developed by Theo de Raadt. 181.5Sderaadt * 4. The name of the author may not be used to endorse or promote 191.2Sderaadt * products derived from this software without specific prior written 201.2Sderaadt * permission. 211.2Sderaadt * 221.2Sderaadt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 231.2Sderaadt * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 241.2Sderaadt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 251.2Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 261.2Sderaadt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 271.2Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 281.2Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 291.2Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 301.2Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 311.2Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 321.2Sderaadt * SUCH DAMAGE. 331.2Sderaadt */ 341.2Sderaadt 351.11Sthorpej#include <sys/cdefs.h> 361.11Sthorpej#ifndef lint 371.13Scgd__RCSID("$NetBSD: ypset.c,v 1.13 2001/02/19 23:22:53 cgd Exp $"); 381.2Sderaadt#endif 391.2Sderaadt 401.1Sderaadt#include <sys/param.h> 411.1Sderaadt#include <sys/types.h> 421.1Sderaadt#include <sys/socket.h> 431.1Sderaadt#include <stdio.h> 441.10Schristos#include <string.h> 451.10Schristos#include <stdlib.h> 461.10Schristos#include <unistd.h> 471.10Schristos#include <err.h> 481.1Sderaadt#include <netdb.h> 491.11Sthorpej 501.1Sderaadt#include <rpc/rpc.h> 511.1Sderaadt#include <rpc/xdr.h> 521.1Sderaadt#include <rpcsvc/yp_prot.h> 531.1Sderaadt#include <rpcsvc/ypclnt.h> 541.4Sbrezak#include <arpa/inet.h> 551.1Sderaadt 561.11Sthorpejint main __P((int, char *[])); 571.10Schristosstatic void usage __P((void)); 581.10Schristosstatic void gethostaddr __P((const char *, struct in_addr *)); 591.10Schristosstatic int bind_tohost __P((struct sockaddr_in *, char *, char *)); 601.10Schristos 611.11Sthorpejint 621.11Sthorpejmain(argc, argv) 631.11Sthorpej int argc; 641.11Sthorpej char *argv[]; 651.1Sderaadt{ 661.11Sthorpej struct sockaddr_in sin; 671.11Sthorpej char *domainname; 681.11Sthorpej int c; 691.11Sthorpej 701.11Sthorpej yp_get_default_domain(&domainname); 711.11Sthorpej 721.11Sthorpej (void) memset(&sin, 0, sizeof sin); 731.11Sthorpej sin.sin_family = AF_INET; 741.11Sthorpej sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 751.11Sthorpej 761.11Sthorpej while ((c = getopt(argc, argv, "h:d:")) != -1) { 771.11Sthorpej switch(c) { 781.11Sthorpej case 'd': 791.11Sthorpej domainname = optarg; 801.11Sthorpej break; 811.11Sthorpej 821.11Sthorpej case 'h': 831.11Sthorpej gethostaddr(optarg, &sin.sin_addr); 841.11Sthorpej break; 851.11Sthorpej 861.11Sthorpej default: 871.11Sthorpej usage(); 881.11Sthorpej } 891.11Sthorpej } 901.11Sthorpej 911.11Sthorpej if (domainname == NULL) 921.11Sthorpej errx(1, "YP domain name not set"); 931.11Sthorpej 941.11Sthorpej argc -= optind; 951.11Sthorpej argv += optind; 961.11Sthorpej 971.11Sthorpej if (argc != 1) 981.11Sthorpej usage(); 991.11Sthorpej 1001.11Sthorpej return bind_tohost(&sin, domainname, argv[0]) != 0; 1011.1Sderaadt} 1021.1Sderaadt 1031.10Schristosstatic void 1041.10Schristosgethostaddr(host, ia) 1051.10Schristos const char *host; 1061.10Schristos struct in_addr *ia; 1071.10Schristos{ 1081.10Schristos struct hostent *hp; 1091.10Schristos 1101.10Schristos if (inet_aton(host, ia) != 0) 1111.10Schristos return; 1121.10Schristos 1131.10Schristos hp = gethostbyname(host); 1141.10Schristos if (hp == NULL) 1151.10Schristos errx(1, "Cannot get host address for %s: %s", host, 1161.10Schristos hstrerror(h_errno)); 1171.10Schristos (void) memcpy(ia, hp->h_addr, sizeof(*ia)); 1181.10Schristos} 1191.10Schristos 1201.10Schristosstatic int 1211.1Sderaadtbind_tohost(sin, dom, server) 1221.10Schristos struct sockaddr_in *sin; 1231.10Schristos char *dom, *server; 1241.1Sderaadt{ 1251.1Sderaadt struct ypbind_setdom ypsd; 1261.1Sderaadt struct timeval tv; 1271.1Sderaadt CLIENT *client; 1281.1Sderaadt int sock, port; 1291.1Sderaadt int r; 1301.1Sderaadt 1311.10Schristos port = htons(getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP)); 1321.10Schristos if (port == 0) 1331.10Schristos errx(1, "%s not running ypserv.", server); 1341.1Sderaadt 1351.10Schristos (void) memset(&ypsd, 0, sizeof ypsd); 1361.4Sbrezak 1371.10Schristos gethostaddr(server, &ypsd.ypsetdom_addr); 1381.4Sbrezak 1391.10Schristos (void) strncpy(ypsd.ypsetdom_domain, dom, sizeof ypsd.ypsetdom_domain); 1401.10Schristos ypsd.ypsetdom_domain[sizeof(ypsd.ypsetdom_domain) - 1] = '\0'; 1411.4Sbrezak ypsd.ypsetdom_port = port; 1421.1Sderaadt ypsd.ypsetdom_vers = YPVERS; 1431.1Sderaadt 1441.1Sderaadt tv.tv_sec = 15; 1451.1Sderaadt tv.tv_usec = 0; 1461.1Sderaadt sock = RPC_ANYSOCK; 1471.10Schristos 1481.3Sderaadt client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock); 1491.10Schristos if (client == NULL) { 1501.10Schristos warnx("Can't yp_bind: Reason: %s", yperr_string(YPERR_YPBIND)); 1511.1Sderaadt return YPERR_YPBIND; 1521.1Sderaadt } 1531.1Sderaadt client->cl_auth = authunix_create_default(); 1541.1Sderaadt 1551.1Sderaadt r = clnt_call(client, YPBINDPROC_SETDOM, 1561.10Schristos xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv); 1571.10Schristos if (r) { 1581.10Schristos warnx("Cannot ypset for domain %s on host %s: %s.\n", 1591.10Schristos dom, server, clnt_sperrno(r)); 1601.1Sderaadt clnt_destroy(client); 1611.1Sderaadt return YPERR_YPBIND; 1621.1Sderaadt } 1631.1Sderaadt clnt_destroy(client); 1641.1Sderaadt return 0; 1651.1Sderaadt} 1661.1Sderaadt 1671.11Sthorpejstatic void 1681.11Sthorpejusage() 1691.1Sderaadt{ 1701.11Sthorpej (void) fprintf(stderr, "usage: %s [-h host ] [-d domain] server\n", 1711.13Scgd getprogname()); 1721.11Sthorpej exit(1); 1731.1Sderaadt} 174