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