ypset.c revision 74347
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 3030828Scharnier#ifndef lint 3130828Scharnierstatic const char rcsid[] = 3250479Speter "$FreeBSD: head/usr.sbin/ypset/ypset.c 74347 2001-03-16 13:10:26Z alfred $"; 3330828Scharnier#endif /* not lint */ 341929Swollman 3530828Scharnier#include <err.h> 3630828Scharnier#include <netdb.h> 3730828Scharnier#include <stdio.h> 3830828Scharnier#include <string.h> 3930828Scharnier#include <unistd.h> 401929Swollman#include <sys/param.h> 411929Swollman#include <sys/types.h> 421929Swollman#include <sys/socket.h> 431929Swollman#include <rpc/rpc.h> 441929Swollman#include <rpc/xdr.h> 4512862Swpaul#include <rpcsvc/yp.h> 4612862Swpaulstruct dom_binding{}; 471929Swollman#include <rpcsvc/ypclnt.h> 481929Swollman#include <arpa/inet.h> 491929Swollman 501929Swollmanextern bool_t xdr_domainname(); 511929Swollman 5230828Scharnierstatic void 531929Swollmanusage() 541929Swollman{ 5530828Scharnier fprintf(stderr, "usage: ypset [-h host] [-d domain] server\n"); 561929Swollman exit(1); 571929Swollman} 581929Swollman 5930828Scharnierint 601929Swollmanbind_tohost(sin, dom, server) 611929Swollmanstruct sockaddr_in *sin; 621929Swollmanchar *dom, *server; 631929Swollman{ 641929Swollman struct ypbind_setdom ypsd; 651929Swollman struct timeval tv; 661929Swollman struct hostent *hp; 671929Swollman CLIENT *client; 681929Swollman int sock, port; 691929Swollman int r; 701929Swollman unsigned long server_addr; 718857Srgrimes 7230828Scharnier if( (port=htons(getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP))) == 0) 7330828Scharnier errx(1, "%s not running ypserv", server); 741929Swollman 751929Swollman bzero(&ypsd, sizeof ypsd); 761929Swollman 771929Swollman if( (hp = gethostbyname (server)) != NULL ) { 781929Swollman /* is this the most compatible way?? */ 7912862Swpaul bcopy (hp->h_addr_list[0], 8012862Swpaul (u_long *)&ypsd.ypsetdom_binding.ypbind_binding_addr, 8112862Swpaul sizeof (unsigned long)); 821929Swollman } else if( (long)(server_addr = inet_addr (server)) == -1) { 8330828Scharnier errx(1, "can't find address for %s", server); 841929Swollman } else 8512862Swpaul bcopy (&server_addr, 8672955Swpaul (u_long *)&ypsd.ypsetdom_binding.ypbind_binding_addr, 871929Swollman sizeof (server_addr)); 881929Swollman 8912862Swpaul/* strncpy(ypsd.ypsetdom_domain, dom, sizeof ypsd.ypsetdom_domain); */ 9012862Swpaul ypsd.ypsetdom_domain = dom; 9112862Swpaul *(u_long *)&ypsd.ypsetdom_binding.ypbind_binding_port = port; 921929Swollman ypsd.ypsetdom_vers = YPVERS; 938857Srgrimes 941929Swollman tv.tv_sec = 15; 951929Swollman tv.tv_usec = 0; 961929Swollman sock = RPC_ANYSOCK; 971929Swollman client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock); 981929Swollman if (client==NULL) { 9930828Scharnier warnx("can't yp_bind, reason: %s", yperr_string(YPERR_YPBIND)); 1001929Swollman return YPERR_YPBIND; 1011929Swollman } 1021929Swollman client->cl_auth = authunix_create_default(); 1031929Swollman 1041929Swollman r = clnt_call(client, YPBINDPROC_SETDOM, 1051929Swollman xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv); 1061929Swollman if(r) { 10730828Scharnier warnx("sorry, cannot ypset for domain %s on host", dom); 1081929Swollman clnt_destroy(client); 1091929Swollman return YPERR_YPBIND; 1101929Swollman } 1111929Swollman clnt_destroy(client); 1121929Swollman return 0; 1131929Swollman} 1141929Swollman 1151929Swollmanint 1161929Swollmanmain(argc, argv) 1171929Swollmanchar **argv; 1181929Swollman{ 1191929Swollman struct sockaddr_in sin; 1201929Swollman struct hostent *hent; 1211929Swollman char *domainname; 1221929Swollman int c; 1231929Swollman 1241929Swollman yp_get_default_domain(&domainname); 1251929Swollman 1261929Swollman bzero(&sin, sizeof sin); 1271929Swollman sin.sin_family = AF_INET; 12874347Salfred sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 1291929Swollman 1301929Swollman while( (c=getopt(argc, argv, "h:d:")) != -1) 1311929Swollman switch(c) { 1321929Swollman case 'd': 1331929Swollman domainname = optarg; 1341929Swollman break; 1351929Swollman case 'h': 1361929Swollman if( (sin.sin_addr.s_addr=inet_addr(optarg)) == -1) { 1371929Swollman hent = gethostbyname(optarg); 13830828Scharnier if(hent==NULL) 13930828Scharnier errx(1, "host %s unknown", optarg); 1401929Swollman bcopy(&hent->h_addr_list[0], &sin.sin_addr, 1411929Swollman sizeof sin.sin_addr); 1421929Swollman } 1431929Swollman break; 1441929Swollman default: 1451929Swollman usage(); 1461929Swollman } 1471929Swollman 1481929Swollman if(optind + 1 != argc ) 1491929Swollman usage(); 1501929Swollman 1511929Swollman if (bind_tohost(&sin, domainname, argv[optind])) 1521929Swollman exit(1); 1531929Swollman exit(0); 1541929Swollman} 155