1 /* $OpenBSD: ypset.c,v 1.5 1997/07/21 19:18:34 deraadt Exp $ */ 2 /* $NetBSD: ypset.c,v 1.8 1996/05/13 02:46:33 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Theo de Raadt. 19 * 4. The name of the author may not be used to endorse or promote 20 * products derived from this software without specific prior written 21 * permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 24 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 27 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef LINT 37 static char rcsid[] = "$OpenBSD: ypset.c,v 1.5 1997/07/21 19:18:34 deraadt Exp $"; 38 #endif 39 40 #include <sys/param.h> 41 #include <sys/types.h> 42 #include <sys/socket.h> 43 #include <stdio.h> 44 #include <string.h> 45 #include <unistd.h> 46 #include <netdb.h> 47 48 #include <rpc/rpc.h> 49 #include <rpc/xdr.h> 50 #include <rpcsvc/yp.h> 51 #include <rpcsvc/ypclnt.h> 52 #include <arpa/inet.h> 53 54 extern bool_t xdr_domainname(); 55 56 void 57 usage() 58 { 59 fprintf(stderr, "Usage:\n"); 60 fprintf(stderr, "\typset [-h host ] [-d domain] server\n"); 61 exit(1); 62 } 63 64 int 65 bind_tohost(sin, dom, server) 66 struct sockaddr_in *sin; 67 char *dom, *server; 68 { 69 struct ypbind_setdom ypsd; 70 struct timeval tv; 71 struct hostent *hp; 72 CLIENT *client; 73 int sock, port; 74 int r; 75 struct in_addr iaddr; 76 77 if( (port=htons(getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP))) == 0) { 78 fprintf(stderr, "%s not running ypserv.\n", server); 79 exit(1); 80 } 81 82 memset(&ypsd, 0, sizeof ypsd); 83 84 if (inet_aton(server, &iaddr) == 0) { 85 hp = gethostbyname(server); 86 if (hp == NULL) { 87 fprintf(stderr, "ypset: can't find address for %s\n", server); 88 exit(1); 89 } 90 memmove(&iaddr.s_addr, hp->h_addr, sizeof(iaddr.s_addr)); 91 } 92 ypsd.ypsetdom_domain = dom; 93 bcopy(&iaddr.s_addr, &ypsd.ypsetdom_binding.ypbind_binding_addr, 94 sizeof(ypsd.ypsetdom_binding.ypbind_binding_addr)); 95 bcopy(&port, &ypsd.ypsetdom_binding.ypbind_binding_port, 96 sizeof(ypsd.ypsetdom_binding.ypbind_binding_port)); 97 ypsd.ypsetdom_vers = YPVERS; 98 99 tv.tv_sec = 15; 100 tv.tv_usec = 0; 101 sock = RPC_ANYSOCK; 102 client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock); 103 if (client==NULL) { 104 fprintf(stderr, "can't yp_bind: Reason: %s\n", 105 yperr_string(YPERR_YPBIND)); 106 return YPERR_YPBIND; 107 } 108 client->cl_auth = authunix_create_default(); 109 110 r = clnt_call(client, YPBINDPROC_SETDOM, 111 xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv); 112 if(r) { 113 fprintf(stderr, "Sorry, cannot ypset for domain %s on host.\n", dom); 114 clnt_destroy(client); 115 return YPERR_YPBIND; 116 } 117 clnt_destroy(client); 118 return 0; 119 } 120 121 int 122 main(argc, argv) 123 char **argv; 124 { 125 struct sockaddr_in sin; 126 struct hostent *hent; 127 extern char *optarg; 128 extern int optind; 129 char *domainname; 130 int c; 131 132 yp_get_default_domain(&domainname); 133 134 bzero(&sin, sizeof sin); 135 sin.sin_family = AF_INET; 136 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 137 138 while( (c=getopt(argc, argv, "h:d:")) != -1) 139 switch(c) { 140 case 'd': 141 domainname = optarg; 142 break; 143 case 'h': 144 if (inet_aton(optarg, &sin.sin_addr) == 0) { 145 hent = gethostbyname(optarg); 146 if (hent == NULL) { 147 fprintf(stderr, "ypset: host %s unknown\n", 148 optarg); 149 exit(1); 150 } 151 bcopy(&hent->h_addr, &sin.sin_addr, 152 sizeof(sin.sin_addr)); 153 } 154 break; 155 default: 156 usage(); 157 } 158 159 if(optind + 1 != argc ) 160 usage(); 161 162 if (bind_tohost(&sin, domainname, argv[optind])) 163 exit(1); 164 exit(0); 165 } 166