1 /* $OpenBSD: ypset.c,v 1.19 2009/10/27 23:59:58 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 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/param.h> 31 #include <sys/types.h> 32 #include <sys/socket.h> 33 #include <err.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <string.h> 37 #include <unistd.h> 38 #include <netdb.h> 39 40 #include <rpc/rpc.h> 41 #include <rpc/xdr.h> 42 #include <rpcsvc/yp.h> 43 #include <rpcsvc/ypclnt.h> 44 #include <arpa/inet.h> 45 46 static void 47 usage(void) 48 { 49 fprintf(stderr, "usage: ypset [-d domain] [-h host] server\n"); 50 exit(1); 51 } 52 53 static int 54 bind_tohost(struct sockaddr_in *sin, char *dom, char *server) 55 { 56 struct ypbind_setdom ypsd; 57 struct in_addr iaddr; 58 struct hostent *hp; 59 struct timeval tv; 60 CLIENT *client; 61 int sock, port, r; 62 63 port = getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP); 64 if (port == 0) 65 errx(1, "%s not running ypserv", server); 66 port = htons(port); 67 68 memset(&ypsd, 0, sizeof ypsd); 69 70 if (inet_aton(server, &iaddr) == 0) { 71 hp = gethostbyname(server); 72 if (hp == NULL) 73 errx(1, "can't find address for %s", server); 74 memmove(&iaddr.s_addr, hp->h_addr, sizeof(iaddr.s_addr)); 75 } 76 ypsd.ypsetdom_domain = dom; 77 bcopy(&iaddr.s_addr, &ypsd.ypsetdom_binding.ypbind_binding_addr, 78 sizeof(ypsd.ypsetdom_binding.ypbind_binding_addr)); 79 bcopy(&port, &ypsd.ypsetdom_binding.ypbind_binding_port, 80 sizeof(ypsd.ypsetdom_binding.ypbind_binding_port)); 81 ypsd.ypsetdom_vers = YPVERS; 82 83 tv.tv_sec = 15; 84 tv.tv_usec = 0; 85 sock = RPC_ANYSOCK; 86 client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock); 87 if (client == NULL) { 88 warnx("can't yp_bind: reason: %s", yperr_string(YPERR_YPBIND)); 89 return YPERR_YPBIND; 90 } 91 client->cl_auth = authunix_create_default(); 92 93 r = clnt_call(client, YPBINDPROC_SETDOM, 94 xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv); 95 if (r) { 96 warnx("Cannot ypset for domain %s on host %s: %s", dom, 97 server, clnt_sperrno(r)); 98 clnt_destroy(client); 99 return YPERR_YPBIND; 100 } 101 clnt_destroy(client); 102 return 0; 103 } 104 105 int 106 main(int argc, char *argv[]) 107 { 108 struct sockaddr_in sin; 109 struct hostent *hent; 110 extern char *optarg; 111 extern int optind; 112 char *domainname; 113 int c; 114 115 yp_get_default_domain(&domainname); 116 117 bzero(&sin, sizeof sin); 118 sin.sin_family = AF_INET; 119 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 120 121 while ((c = getopt(argc, argv, "h:d:")) != -1) 122 switch(c) { 123 case 'd': 124 domainname = optarg; 125 break; 126 case 'h': 127 if (inet_aton(optarg, &sin.sin_addr) == 0) { 128 hent = gethostbyname(optarg); 129 if (hent == NULL) 130 errx(1, "host %s unknown\n", optarg); 131 bcopy(hent->h_addr, &sin.sin_addr, 132 sizeof(sin.sin_addr)); 133 } 134 break; 135 default: 136 usage(); 137 } 138 139 if (optind + 1 != argc) 140 usage(); 141 142 if (bind_tohost(&sin, domainname, argv[optind])) 143 exit(1); 144 exit(0); 145 } 146