ypset.c revision 1.9
11.9Sthorpej/* $NetBSD: ypset.c,v 1.9 1996/05/29 20:12:03 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.9Sthorpejstatic char rcsid[] = "$NetBSD: ypset.c,v 1.9 1996/05/29 20:12:03 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.1Sderaadtusage() 511.1Sderaadt{ 521.1Sderaadt fprintf(stderr, "Usage:\n"); 531.1Sderaadt fprintf(stderr, "\typset [-h host ] [-d domain] server\n"); 541.1Sderaadt exit(1); 551.1Sderaadt} 561.1Sderaadt 571.1Sderaadtbind_tohost(sin, dom, server) 581.1Sderaadtstruct sockaddr_in *sin; 591.1Sderaadtchar *dom, *server; 601.1Sderaadt{ 611.1Sderaadt struct ypbind_setdom ypsd; 621.1Sderaadt struct timeval tv; 631.4Sbrezak struct hostent *hp; 641.1Sderaadt CLIENT *client; 651.1Sderaadt int sock, port; 661.1Sderaadt int r; 671.1Sderaadt 681.1Sderaadt if( (port=htons(getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP))) == 0) { 691.1Sderaadt fprintf(stderr, "%s not running ypserv.\n", server); 701.1Sderaadt exit(1); 711.1Sderaadt } 721.1Sderaadt 731.1Sderaadt bzero(&ypsd, sizeof ypsd); 741.4Sbrezak 751.6Smycroft if (inet_aton(server, &ypsd.ypsetdom_addr) == 0) { 761.6Smycroft hp = gethostbyname(server); 771.6Smycroft if (hp == NULL) { 781.6Smycroft fprintf(stderr, "ypset: can't find address for %s\n", server); 791.6Smycroft exit(1); 801.6Smycroft } 811.7Scgd bcopy(hp->h_addr, &ypsd.ypsetdom_addr, sizeof(ypsd.ypsetdom_addr)); 821.6Smycroft } 831.4Sbrezak 841.1Sderaadt strncpy(ypsd.ypsetdom_domain, dom, sizeof ypsd.ypsetdom_domain); 851.4Sbrezak ypsd.ypsetdom_port = port; 861.1Sderaadt ypsd.ypsetdom_vers = YPVERS; 871.1Sderaadt 881.1Sderaadt tv.tv_sec = 15; 891.1Sderaadt tv.tv_usec = 0; 901.1Sderaadt sock = RPC_ANYSOCK; 911.3Sderaadt client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock); 921.1Sderaadt if (client==NULL) { 931.1Sderaadt fprintf(stderr, "can't yp_bind: Reason: %s\n", 941.1Sderaadt yperr_string(YPERR_YPBIND)); 951.1Sderaadt return YPERR_YPBIND; 961.1Sderaadt } 971.1Sderaadt client->cl_auth = authunix_create_default(); 981.1Sderaadt 991.1Sderaadt r = clnt_call(client, YPBINDPROC_SETDOM, 1001.1Sderaadt xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv); 1011.1Sderaadt if(r) { 1021.1Sderaadt fprintf(stderr, "Sorry, cannot ypset for domain %s on host.\n", dom); 1031.1Sderaadt clnt_destroy(client); 1041.1Sderaadt return YPERR_YPBIND; 1051.1Sderaadt } 1061.1Sderaadt clnt_destroy(client); 1071.1Sderaadt return 0; 1081.1Sderaadt} 1091.1Sderaadt 1101.1Sderaadtint 1111.1Sderaadtmain(argc, argv) 1121.1Sderaadtchar **argv; 1131.1Sderaadt{ 1141.1Sderaadt struct sockaddr_in sin; 1151.1Sderaadt struct hostent *hent; 1161.1Sderaadt extern char *optarg; 1171.1Sderaadt extern int optind; 1181.1Sderaadt char *domainname; 1191.1Sderaadt int c; 1201.1Sderaadt 1211.1Sderaadt yp_get_default_domain(&domainname); 1221.1Sderaadt 1231.1Sderaadt bzero(&sin, sizeof sin); 1241.1Sderaadt sin.sin_family = AF_INET; 1251.1Sderaadt sin.sin_addr.s_addr = htonl(0x7f000001); 1261.1Sderaadt 1271.1Sderaadt while( (c=getopt(argc, argv, "h:d:")) != -1) 1281.1Sderaadt switch(c) { 1291.1Sderaadt case 'd': 1301.1Sderaadt domainname = optarg; 1311.1Sderaadt break; 1321.1Sderaadt case 'h': 1331.6Smycroft if (inet_aton(optarg, &sin.sin_addr) == 0) { 1341.1Sderaadt hent = gethostbyname(optarg); 1351.6Smycroft if (hent == NULL) { 1361.1Sderaadt fprintf(stderr, "ypset: host %s unknown\n", 1371.6Smycroft optarg); 1381.1Sderaadt exit(1); 1391.1Sderaadt } 1401.6Smycroft bcopy(&hent->h_addr, &sin.sin_addr, 1411.6Smycroft sizeof(sin.sin_addr)); 1421.1Sderaadt } 1431.1Sderaadt break; 1441.1Sderaadt default: 1451.1Sderaadt usage(); 1461.1Sderaadt } 1471.1Sderaadt 1481.1Sderaadt if(optind + 1 != argc ) 1491.1Sderaadt usage(); 1501.1Sderaadt 1511.1Sderaadt if (bind_tohost(&sin, domainname, argv[optind])) 1521.1Sderaadt exit(1); 1531.1Sderaadt exit(0); 1541.1Sderaadt} 155