1 /* $OpenBSD: ypset.c,v 1.20 2015/01/16 06:40:23 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/types.h> 31 #include <sys/socket.h> 32 #include <err.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <unistd.h> 37 #include <netdb.h> 38 39 #include <rpc/rpc.h> 40 #include <rpc/xdr.h> 41 #include <rpcsvc/yp.h> 42 #include <rpcsvc/ypclnt.h> 43 #include <arpa/inet.h> 44 45 static void 46 usage(void) 47 { 48 fprintf(stderr, "usage: ypset [-d domain] [-h host] server\n"); 49 exit(1); 50 } 51 52 static int 53 bind_tohost(struct sockaddr_in *sin, char *dom, char *server) 54 { 55 struct ypbind_setdom ypsd; 56 struct in_addr iaddr; 57 struct hostent *hp; 58 struct timeval tv; 59 CLIENT *client; 60 int sock, port, r; 61 62 port = getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP); 63 if (port == 0) 64 errx(1, "%s not running ypserv", server); 65 port = htons(port); 66 67 memset(&ypsd, 0, sizeof ypsd); 68 69 if (inet_aton(server, &iaddr) == 0) { 70 hp = gethostbyname(server); 71 if (hp == NULL) 72 errx(1, "can't find address for %s", server); 73 memmove(&iaddr.s_addr, hp->h_addr, sizeof(iaddr.s_addr)); 74 } 75 ypsd.ypsetdom_domain = dom; 76 bcopy(&iaddr.s_addr, &ypsd.ypsetdom_binding.ypbind_binding_addr, 77 sizeof(ypsd.ypsetdom_binding.ypbind_binding_addr)); 78 bcopy(&port, &ypsd.ypsetdom_binding.ypbind_binding_port, 79 sizeof(ypsd.ypsetdom_binding.ypbind_binding_port)); 80 ypsd.ypsetdom_vers = YPVERS; 81 82 tv.tv_sec = 15; 83 tv.tv_usec = 0; 84 sock = RPC_ANYSOCK; 85 client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock); 86 if (client == NULL) { 87 warnx("can't yp_bind: reason: %s", yperr_string(YPERR_YPBIND)); 88 return YPERR_YPBIND; 89 } 90 client->cl_auth = authunix_create_default(); 91 92 r = clnt_call(client, YPBINDPROC_SETDOM, 93 xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv); 94 if (r) { 95 warnx("Cannot ypset for domain %s on host %s: %s", dom, 96 server, clnt_sperrno(r)); 97 clnt_destroy(client); 98 return YPERR_YPBIND; 99 } 100 clnt_destroy(client); 101 return 0; 102 } 103 104 int 105 main(int argc, char *argv[]) 106 { 107 struct sockaddr_in sin; 108 struct hostent *hent; 109 extern char *optarg; 110 extern int optind; 111 char *domainname; 112 int c; 113 114 yp_get_default_domain(&domainname); 115 116 bzero(&sin, sizeof sin); 117 sin.sin_family = AF_INET; 118 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 119 120 while ((c = getopt(argc, argv, "h:d:")) != -1) 121 switch(c) { 122 case 'd': 123 domainname = optarg; 124 break; 125 case 'h': 126 if (inet_aton(optarg, &sin.sin_addr) == 0) { 127 hent = gethostbyname(optarg); 128 if (hent == NULL) 129 errx(1, "host %s unknown\n", optarg); 130 bcopy(hent->h_addr, &sin.sin_addr, 131 sizeof(sin.sin_addr)); 132 } 133 break; 134 default: 135 usage(); 136 } 137 138 if (optind + 1 != argc) 139 usage(); 140 141 if (bind_tohost(&sin, domainname, argv[optind])) 142 exit(1); 143 exit(0); 144 } 145