1 /* $NetBSD: yppoll.c,v 1.11 2002/12/06 15:24:08 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca> 5 * Copyright (c) 1992, 1993 John Brezak 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 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Theo de Raadt and 19 * John Brezak. 20 * 4. The name of the author may not be used to endorse or promote 21 * products derived from this software without specific prior written 22 * permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 25 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 28 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #include <sys/cdefs.h> 38 #ifndef lint 39 __RCSID("$NetBSD: yppoll.c,v 1.11 2002/12/06 15:24:08 thorpej Exp $"); 40 #endif /* not lint */ 41 42 #include <sys/param.h> 43 #include <sys/types.h> 44 #include <sys/socket.h> 45 #include <err.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <time.h> 49 #include <netdb.h> 50 #include <unistd.h> 51 #include <string.h> 52 #include <netinet/in.h> 53 #include <arpa/inet.h> 54 55 #include <rpc/rpc.h> 56 #include <rpc/xdr.h> 57 #include <rpcsvc/yp_prot.h> 58 #include <rpcsvc/ypclnt.h> 59 60 int main __P((int, char *[])); 61 int get_remote_info __P((char *, char *, char *, int *, char **)); 62 void usage __P((void)); 63 64 int 65 main(argc, argv) 66 int argc; 67 char **argv; 68 { 69 char *domainname; 70 char *hostname = NULL; 71 char *inmap, *master; 72 int order; 73 int c, r; 74 75 yp_get_default_domain(&domainname); 76 77 while ((c = getopt(argc, argv, "h:d:")) != -1) { 78 switch (c) { 79 case 'd': 80 domainname = optarg; 81 break; 82 83 case 'h': 84 hostname = optarg; 85 break; 86 87 default: 88 usage(); 89 /*NOTREACHED*/ 90 } 91 } 92 93 if (domainname == NULL) 94 errx(1, "YP domain name not set"); 95 96 argc -= optind; 97 argv += optind; 98 99 if (argc != 1) 100 usage(); 101 102 inmap = argv[0]; 103 104 if (hostname != NULL) 105 r = get_remote_info(domainname, inmap, hostname, 106 &order, &master); 107 else { 108 r = yp_order(domainname, inmap, &order); 109 if (r == 0) 110 r = yp_master(domainname, inmap, &master); 111 } 112 113 if (r != 0) 114 errx(1, "no such map %s. Reason: %s", 115 inmap, yperr_string(r)); 116 117 printf("Map %s has order number %d. %s", inmap, order, 118 ctime((void *)&order)); 119 printf("The master server is %s.\n", master); 120 exit(0); 121 } 122 123 int 124 get_remote_info(indomain, inmap, server, outorder, outname) 125 char *indomain; 126 char *inmap; 127 char *server; 128 int *outorder; 129 char **outname; 130 { 131 struct ypresp_order ypro; 132 struct ypresp_master yprm; 133 struct ypreq_nokey yprnk; 134 struct timeval tv; 135 int r; 136 struct sockaddr_in rsrv_sin; 137 int rsrv_sock; 138 CLIENT *client; 139 struct hostent *h; 140 141 memset(&rsrv_sin, 0, sizeof(rsrv_sin)); 142 rsrv_sin.sin_len = sizeof rsrv_sin; 143 rsrv_sin.sin_family = AF_INET; 144 rsrv_sock = RPC_ANYSOCK; 145 146 h = gethostbyname(server); 147 if (h == NULL) { 148 if (inet_aton(server, &rsrv_sin.sin_addr) == 0) 149 errx(1, "unknown host %s", server); 150 } else 151 memcpy(&rsrv_sin.sin_addr.s_addr, h->h_addr, h->h_length); 152 153 tv.tv_sec = 10; 154 tv.tv_usec = 0; 155 156 client = clntudp_create(&rsrv_sin, YPPROG, YPVERS, tv, &rsrv_sock); 157 if (client == NULL) 158 errx(1, "clntudp_create: no contact with host %s.", server); 159 160 yprnk.domain = indomain; 161 yprnk.map = inmap; 162 163 memset(&ypro, 0, sizeof(ypro)); 164 165 r = clnt_call(client, YPPROC_ORDER, xdr_ypreq_nokey, &yprnk, 166 xdr_ypresp_order, &ypro, tv); 167 if (r != RPC_SUCCESS) 168 clnt_perror(client, "yp_order: clnt_call"); 169 170 *outorder = ypro.ordernum; 171 xdr_free(xdr_ypresp_order, (char *)&ypro); 172 173 r = ypprot_err(ypro.status); 174 if (r == RPC_SUCCESS) { 175 memset(&yprm, 0, sizeof(yprm)); 176 177 r = clnt_call(client, YPPROC_MASTER, xdr_ypreq_nokey, 178 &yprnk, xdr_ypresp_master, &yprm, tv); 179 if (r != RPC_SUCCESS) 180 clnt_perror(client, "yp_master: clnt_call"); 181 r = ypprot_err(yprm.status); 182 if (r == 0) 183 *outname = (char *)strdup(yprm.master); 184 xdr_free(xdr_ypresp_master, (char *)&yprm); 185 } 186 clnt_destroy(client); 187 return r; 188 } 189 190 void 191 usage() 192 { 193 194 fprintf(stderr, "usage: %s [-h host] [-d domainname] mapname\n", 195 getprogname()); 196 exit(1); 197 } 198