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