1 /* 2 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 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 * $FreeBSD: src/usr.sbin/ypset/ypset.c,v 1.5.2.2 2002/02/15 00:47:00 des Exp $ 30 * $DragonFly: src/usr.sbin/ypset/ypset.c,v 1.2 2003/06/17 04:30:05 dillon Exp $ 31 */ 32 33 #include <err.h> 34 #include <netdb.h> 35 #include <stdio.h> 36 #include <string.h> 37 #include <unistd.h> 38 #include <sys/param.h> 39 #include <sys/types.h> 40 #include <sys/socket.h> 41 #include <rpc/rpc.h> 42 #include <rpc/xdr.h> 43 #include <rpcsvc/yp.h> 44 struct dom_binding{}; 45 #include <rpcsvc/ypclnt.h> 46 #include <arpa/inet.h> 47 48 extern bool_t xdr_domainname(); 49 50 static void 51 usage() 52 { 53 fprintf(stderr, "usage: ypset [-h host] [-d domain] server\n"); 54 exit(1); 55 } 56 57 int 58 bind_tohost(sin, dom, server) 59 struct sockaddr_in *sin; 60 char *dom, *server; 61 { 62 struct ypbind_setdom ypsd; 63 struct timeval tv; 64 struct hostent *hp; 65 CLIENT *client; 66 int sock, port; 67 int r; 68 unsigned long server_addr; 69 70 if ((port = htons(getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP))) == 0) 71 errx(1, "%s not running ypserv", server); 72 73 bzero(&ypsd, sizeof ypsd); 74 75 if ((hp = gethostbyname (server)) != NULL) { 76 /* is this the most compatible way?? */ 77 bcopy (hp->h_addr_list[0], 78 (u_long *)&ypsd.ypsetdom_binding.ypbind_binding_addr, 79 sizeof (unsigned long)); 80 } else if ((long)(server_addr = inet_addr (server)) == -1) { 81 errx(1, "can't find address for %s", server); 82 } else 83 bcopy (&server_addr, 84 (u_long *)&ypsd.ypsetdom_binding.ypbind_binding_addr, 85 sizeof (server_addr)); 86 87 /* strncpy(ypsd.ypsetdom_domain, dom, sizeof ypsd.ypsetdom_domain); */ 88 ypsd.ypsetdom_domain = dom; 89 *(u_long *)&ypsd.ypsetdom_binding.ypbind_binding_port = port; 90 ypsd.ypsetdom_vers = YPVERS; 91 92 tv.tv_sec = 15; 93 tv.tv_usec = 0; 94 sock = RPC_ANYSOCK; 95 client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock); 96 if (client == NULL) { 97 warnx("can't yp_bind, reason: %s", yperr_string(YPERR_YPBIND)); 98 return (YPERR_YPBIND); 99 } 100 client->cl_auth = authunix_create_default(); 101 102 r = clnt_call(client, YPBINDPROC_SETDOM, 103 xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv); 104 if (r) { 105 warnx("sorry, cannot ypset for domain %s on host", dom); 106 clnt_destroy(client); 107 return (YPERR_YPBIND); 108 } 109 clnt_destroy(client); 110 return (0); 111 } 112 113 int 114 main(argc, argv) 115 char **argv; 116 { 117 struct sockaddr_in sin; 118 struct hostent *hent; 119 extern char *optarg; 120 extern int optind; 121 char *domainname; 122 int c; 123 124 yp_get_default_domain(&domainname); 125 126 bzero(&sin, sizeof sin); 127 sin.sin_family = AF_INET; 128 sin.sin_addr.s_addr = htonl(0x7f000001); 129 130 while ((c = getopt(argc, argv, "h:d:")) != -1) 131 switch (c) { 132 case 'd': 133 domainname = optarg; 134 break; 135 case 'h': 136 if ((sin.sin_addr.s_addr = inet_addr(optarg)) == -1) { 137 hent = gethostbyname(optarg); 138 if (hent == NULL) 139 errx(1, "host %s unknown", optarg); 140 bcopy(&hent->h_addr_list[0], &sin.sin_addr, 141 sizeof sin.sin_addr); 142 } 143 break; 144 default: 145 usage(); 146 } 147 148 if (optind + 1 != argc) 149 usage(); 150 151 if (bind_tohost(&sin, domainname, argv[optind])) 152 exit(1); 153 exit(0); 154 } 155