ypset.c revision 1.8
11.8Sthorpej/* $NetBSD: ypset.c,v 1.8 1996/05/13 02:46:33 thorpej 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.2Sderaadt#ifndef LINT 361.8Sthorpejstatic char rcsid[] = "$NetBSD: ypset.c,v 1.8 1996/05/13 02:46:33 thorpej Exp $"; 371.2Sderaadt#endif 381.2Sderaadt 391.1Sderaadt#include <sys/param.h> 401.1Sderaadt#include <sys/types.h> 411.1Sderaadt#include <sys/socket.h> 421.1Sderaadt#include <stdio.h> 431.1Sderaadt#include <netdb.h> 441.1Sderaadt#include <rpc/rpc.h> 451.1Sderaadt#include <rpc/xdr.h> 461.1Sderaadt#include <rpcsvc/yp_prot.h> 471.1Sderaadt#include <rpcsvc/ypclnt.h> 481.4Sbrezak#include <arpa/inet.h> 491.1Sderaadt 501.1Sderaadtextern bool_t xdr_domainname(); 511.1Sderaadt 521.1Sderaadtusage() 531.1Sderaadt{ 541.1Sderaadt fprintf(stderr, "Usage:\n"); 551.1Sderaadt fprintf(stderr, "\typset [-h host ] [-d domain] server\n"); 561.1Sderaadt exit(1); 571.1Sderaadt} 581.1Sderaadt 591.1Sderaadtbind_tohost(sin, dom, server) 601.1Sderaadtstruct sockaddr_in *sin; 611.1Sderaadtchar *dom, *server; 621.1Sderaadt{ 631.1Sderaadt struct ypbind_setdom ypsd; 641.1Sderaadt struct timeval tv; 651.4Sbrezak struct hostent *hp; 661.1Sderaadt CLIENT *client; 671.1Sderaadt int sock, port; 681.1Sderaadt int r; 691.1Sderaadt 701.1Sderaadt if( (port=htons(getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP))) == 0) { 711.1Sderaadt fprintf(stderr, "%s not running ypserv.\n", server); 721.1Sderaadt exit(1); 731.1Sderaadt } 741.1Sderaadt 751.1Sderaadt bzero(&ypsd, sizeof ypsd); 761.4Sbrezak 771.6Smycroft if (inet_aton(server, &ypsd.ypsetdom_addr) == 0) { 781.6Smycroft hp = gethostbyname(server); 791.6Smycroft if (hp == NULL) { 801.6Smycroft fprintf(stderr, "ypset: can't find address for %s\n", server); 811.6Smycroft exit(1); 821.6Smycroft } 831.7Scgd bcopy(hp->h_addr, &ypsd.ypsetdom_addr, sizeof(ypsd.ypsetdom_addr)); 841.6Smycroft } 851.4Sbrezak 861.1Sderaadt strncpy(ypsd.ypsetdom_domain, dom, sizeof ypsd.ypsetdom_domain); 871.4Sbrezak ypsd.ypsetdom_port = port; 881.1Sderaadt ypsd.ypsetdom_vers = YPVERS; 891.1Sderaadt 901.1Sderaadt tv.tv_sec = 15; 911.1Sderaadt tv.tv_usec = 0; 921.1Sderaadt sock = RPC_ANYSOCK; 931.3Sderaadt client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock); 941.1Sderaadt if (client==NULL) { 951.1Sderaadt fprintf(stderr, "can't yp_bind: Reason: %s\n", 961.1Sderaadt yperr_string(YPERR_YPBIND)); 971.1Sderaadt return YPERR_YPBIND; 981.1Sderaadt } 991.1Sderaadt client->cl_auth = authunix_create_default(); 1001.1Sderaadt 1011.1Sderaadt r = clnt_call(client, YPBINDPROC_SETDOM, 1021.1Sderaadt xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv); 1031.1Sderaadt if(r) { 1041.1Sderaadt fprintf(stderr, "Sorry, cannot ypset for domain %s on host.\n", dom); 1051.1Sderaadt clnt_destroy(client); 1061.1Sderaadt return YPERR_YPBIND; 1071.1Sderaadt } 1081.1Sderaadt clnt_destroy(client); 1091.1Sderaadt return 0; 1101.1Sderaadt} 1111.1Sderaadt 1121.1Sderaadtint 1131.1Sderaadtmain(argc, argv) 1141.1Sderaadtchar **argv; 1151.1Sderaadt{ 1161.1Sderaadt struct sockaddr_in sin; 1171.1Sderaadt struct hostent *hent; 1181.1Sderaadt extern char *optarg; 1191.1Sderaadt extern int optind; 1201.1Sderaadt char *domainname; 1211.1Sderaadt int c; 1221.1Sderaadt 1231.1Sderaadt yp_get_default_domain(&domainname); 1241.1Sderaadt 1251.1Sderaadt bzero(&sin, sizeof sin); 1261.1Sderaadt sin.sin_family = AF_INET; 1271.1Sderaadt sin.sin_addr.s_addr = htonl(0x7f000001); 1281.1Sderaadt 1291.1Sderaadt while( (c=getopt(argc, argv, "h:d:")) != -1) 1301.1Sderaadt switch(c) { 1311.1Sderaadt case 'd': 1321.1Sderaadt domainname = optarg; 1331.1Sderaadt break; 1341.1Sderaadt case 'h': 1351.6Smycroft if (inet_aton(optarg, &sin.sin_addr) == 0) { 1361.1Sderaadt hent = gethostbyname(optarg); 1371.6Smycroft if (hent == NULL) { 1381.1Sderaadt fprintf(stderr, "ypset: host %s unknown\n", 1391.6Smycroft optarg); 1401.1Sderaadt exit(1); 1411.1Sderaadt } 1421.6Smycroft bcopy(&hent->h_addr, &sin.sin_addr, 1431.6Smycroft sizeof(sin.sin_addr)); 1441.1Sderaadt } 1451.1Sderaadt break; 1461.1Sderaadt default: 1471.1Sderaadt usage(); 1481.1Sderaadt } 1491.1Sderaadt 1501.1Sderaadt if(optind + 1 != argc ) 1511.1Sderaadt usage(); 1521.1Sderaadt 1531.1Sderaadt if (bind_tohost(&sin, domainname, argv[optind])) 1541.1Sderaadt exit(1); 1551.1Sderaadt exit(0); 1561.1Sderaadt} 157