1 /* $OpenBSD: ypset.c,v 1.21 2024/08/27 06:04:03 florian 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 addrinfo hints, *res; 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 memset(&hints, 0, sizeof(hints)); 70 hints.ai_family = AF_INET; 71 72 if (getaddrinfo(server, NULL, &hints, &res) != 0) 73 errx(1, "can't find address for %s", server); 74 iaddr = ((struct sockaddr_in *)res->ai_addr)->sin_addr; 75 freeaddrinfo(res); 76 77 ypsd.ypsetdom_domain = dom; 78 bcopy(&iaddr.s_addr, &ypsd.ypsetdom_binding.ypbind_binding_addr, 79 sizeof(ypsd.ypsetdom_binding.ypbind_binding_addr)); 80 bcopy(&port, &ypsd.ypsetdom_binding.ypbind_binding_port, 81 sizeof(ypsd.ypsetdom_binding.ypbind_binding_port)); 82 ypsd.ypsetdom_vers = YPVERS; 83 84 tv.tv_sec = 15; 85 tv.tv_usec = 0; 86 sock = RPC_ANYSOCK; 87 client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock); 88 if (client == NULL) { 89 warnx("can't yp_bind: reason: %s", yperr_string(YPERR_YPBIND)); 90 return YPERR_YPBIND; 91 } 92 client->cl_auth = authunix_create_default(); 93 94 r = clnt_call(client, YPBINDPROC_SETDOM, 95 xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv); 96 if (r) { 97 warnx("Cannot ypset for domain %s on host %s: %s", dom, 98 server, clnt_sperrno(r)); 99 clnt_destroy(client); 100 return YPERR_YPBIND; 101 } 102 clnt_destroy(client); 103 return 0; 104 } 105 106 int 107 main(int argc, char *argv[]) 108 { 109 struct sockaddr_in sin; 110 struct addrinfo hints, *res; 111 extern char *optarg; 112 extern int optind; 113 char *domainname; 114 int c; 115 116 yp_get_default_domain(&domainname); 117 118 bzero(&sin, sizeof sin); 119 sin.sin_family = AF_INET; 120 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 121 122 while ((c = getopt(argc, argv, "h:d:")) != -1) 123 switch(c) { 124 case 'd': 125 domainname = optarg; 126 break; 127 case 'h': 128 memset(&hints, 0, sizeof(hints)); 129 hints.ai_family = AF_INET; 130 131 if (getaddrinfo(optarg, NULL, &hints, &res) != 0) 132 errx(1, "host %s unknown\n", optarg); 133 134 sin.sin_addr = 135 ((struct sockaddr_in *)res->ai_addr)->sin_addr; 136 freeaddrinfo(res); 137 break; 138 default: 139 usage(); 140 } 141 142 if (optind + 1 != argc) 143 usage(); 144 145 if (bind_tohost(&sin, domainname, argv[optind])) 146 exit(1); 147 exit(0); 148 } 149