1 /* $OpenBSD: yppush.c,v 1.27 2009/10/27 23:59:58 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Mats O Jansson <moj@stacken.kth.se> 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/types.h> 30 #include <sys/stat.h> 31 #include <sys/resource.h> 32 #include <sys/wait.h> 33 34 #include <stdio.h> 35 #include <unistd.h> 36 #include <stdlib.h> 37 #include <signal.h> 38 #include <ctype.h> 39 40 #include <rpc/rpc.h> 41 #include <rpc/xdr.h> 42 #include <rpcsvc/yp.h> 43 #include <rpcsvc/ypclnt.h> 44 45 #include <netdb.h> 46 #include <string.h> 47 #include <errno.h> 48 #include <fcntl.h> 49 50 #include "yplib_host.h" 51 #include "ypdef.h" 52 #include "ypdb.h" 53 54 int Verbose = 0; 55 char Domain[MAXHOSTNAMELEN], Map[255]; 56 u_int32_t OrderNum; 57 char *master; 58 59 extern void yppush_xfrrespprog_1(struct svc_req *request, SVCXPRT *xprt); 60 extern bool_t xdr_ypreq_xfr(XDR *, struct ypreq_xfr *); 61 62 static void 63 usage(void) 64 { 65 fprintf(stderr, 66 "usage: yppush [-v] [-d domainname] [-h hostname] mapname\n"); 67 exit(1); 68 } 69 70 static void 71 my_svc_run(void) 72 { 73 struct pollfd *pfd = NULL, *newp; 74 int nready, saved_max_pollfd = 0; 75 76 for (;;) { 77 if (svc_max_pollfd > saved_max_pollfd) { 78 newp = realloc(pfd, sizeof(*pfd) * svc_max_pollfd); 79 if (newp == NULL) { 80 free(pfd); 81 perror("svc_run: - realloc failed"); 82 return; 83 } 84 pfd = newp; 85 saved_max_pollfd = svc_max_pollfd; 86 } 87 memcpy(pfd, svc_pollfd, sizeof(*pfd) * svc_max_pollfd); 88 89 nready = poll(pfd, svc_max_pollfd, 60 * 1000); 90 switch (nready) { 91 case -1: 92 if (errno == EINTR) 93 continue; 94 perror("yppush: my_svc_run: poll failed"); 95 free(pfd); 96 return; 97 case 0: 98 fprintf(stderr, "yppush: Callback timed out.\n"); 99 exit(0); 100 default: 101 svc_getreq_poll(pfd, nready); 102 free(pfd); 103 break; 104 } 105 } 106 } 107 108 static void 109 req_xfr(pid_t pid, u_int prog, SVCXPRT *transp, char *host, CLIENT *client) 110 { 111 struct ypreq_xfr request; 112 struct timeval tv; 113 114 tv.tv_sec = 0; 115 tv.tv_usec = 0; 116 117 request.map_parms.domain=(char *)&Domain; 118 request.map_parms.map=(char *)⤅ 119 request.map_parms.peer=master; 120 request.map_parms.ordernum=OrderNum; 121 request.transid=(u_int)pid; 122 request.prog=prog; 123 request.port=transp->xp_port; 124 125 if (Verbose) 126 printf("%d: %s(%u@%s) -> %s@%s\n", 127 request.transid, request.map_parms.map, 128 request.map_parms.ordernum, host, 129 request.map_parms.peer, request.map_parms.domain); 130 switch (clnt_call(client, YPPROC_XFR, xdr_ypreq_xfr, &request, 131 xdr_void, NULL, tv)) { 132 case RPC_SUCCESS: 133 case RPC_TIMEDOUT: 134 break; 135 default: 136 clnt_perror(client, "yppush: Cannot call YPPROC_XFR"); 137 kill(pid, SIGTERM); 138 break; 139 } 140 } 141 142 static void 143 push(int inlen, char *indata) 144 { 145 char host[MAXHOSTNAMELEN]; 146 CLIENT *client; 147 SVCXPRT *transp; 148 int sock = RPC_ANYSOCK, status; 149 u_int prog; 150 bool_t sts = 0; 151 pid_t pid; 152 struct rusage res; 153 154 snprintf(host, sizeof host, "%*.*s", inlen, inlen, indata); 155 156 client = clnt_create(host, YPPROG, YPVERS, "tcp"); 157 if (client == NULL) { 158 if (Verbose) 159 fprintf(stderr, "Target Host: %s\n", host); 160 clnt_pcreateerror("yppush: Cannot create client"); 161 return; 162 } 163 164 transp = svcudp_create(sock); 165 if (transp == NULL) { 166 fprintf(stderr, "yppush: Cannot create callback transport.\n"); 167 return; 168 } 169 if (transp->xp_port >= IPPORT_RESERVED) { 170 SVC_DESTROY(transp); 171 fprintf(stderr, "yppush: Cannot allocate reserved port.\n"); 172 return; 173 } 174 175 for (prog=0x40000000; prog<0x5fffffff; prog++) { 176 if ((sts = svc_register(transp, prog, 1, 177 yppush_xfrrespprog_1, IPPROTO_UDP))) 178 break; 179 } 180 181 if (!sts) { 182 fprintf(stderr, "yppush: Cannot register callback.\n"); 183 return; 184 } 185 186 switch (pid=fork()) { 187 case -1: 188 fprintf(stderr, "yppush: Cannot fork.\n"); 189 exit(1); 190 case 0: 191 my_svc_run(); 192 exit(0); 193 default: 194 close(transp->xp_sock); 195 transp->xp_sock = -1; 196 req_xfr(pid, prog, transp, host, client); 197 wait4(pid, &status, 0, &res); 198 svc_unregister(prog, 1); 199 if (client != NULL) 200 clnt_destroy(client); 201 /* XXX transp leak? */ 202 } 203 204 } 205 206 static int 207 pushit(u_long instatus, char *inkey, int inkeylen, char *inval, int invallen, 208 void *indata) 209 { 210 if (instatus != YP_TRUE) 211 return instatus; 212 push(invallen, inval); 213 return 0; 214 } 215 216 int 217 main(int argc, char *argv[]) 218 { 219 struct ypall_callback ypcb; 220 extern char *optarg; 221 extern int optind; 222 char *domain, *map, *hostname; 223 int c, r, i; 224 char *ypmap = "ypservers"; 225 CLIENT *client; 226 static char map_path[MAXPATHLEN]; 227 struct stat finfo; 228 DBM *yp_databas; 229 char order_key[YP_LAST_LEN] = YP_LAST_KEY; 230 datum o; 231 232 yp_get_default_domain(&domain); 233 hostname = NULL; 234 while ((c=getopt(argc, argv, "d:h:v")) != -1) 235 switch (c) { 236 case 'd': 237 domain = optarg; 238 break; 239 case 'h': 240 hostname = optarg; 241 break; 242 case 'v': 243 Verbose = 1; 244 break; 245 default: 246 usage(); 247 /*NOTREACHED*/ 248 } 249 250 if (optind + 1 != argc ) 251 usage(); 252 253 map = argv[optind]; 254 255 strncpy(Domain, domain, sizeof(Domain)-1); 256 Domain[sizeof(Domain)-1] = '\0'; 257 strncpy(Map, map, sizeof(Map)-1); 258 Map[sizeof(Map)-1] = '\0'; 259 260 /* Check domain */ 261 snprintf(map_path, sizeof map_path, "%s/%s", YP_DB_PATH, domain); 262 if (!((stat(map_path, &finfo) == 0) && S_ISDIR(finfo.st_mode))) { 263 fprintf(stderr, "yppush: Map does not exist.\n"); 264 exit(1); 265 } 266 267 /* Check map */ 268 snprintf(map_path, sizeof map_path, "%s/%s/%s%s", 269 YP_DB_PATH, domain, Map, YPDB_SUFFIX); 270 if (!(stat(map_path, &finfo) == 0)) { 271 fprintf(stderr, "yppush: Map does not exist.\n"); 272 exit(1); 273 } 274 275 snprintf(map_path, sizeof map_path, "%s/%s/%s", 276 YP_DB_PATH, domain, Map); 277 yp_databas = ypdb_open(map_path, 0, O_RDONLY); 278 OrderNum=0xffffffff; 279 if (yp_databas == 0) { 280 fprintf(stderr, "yppush: %s%s: Cannot open database\n", 281 map_path, YPDB_SUFFIX); 282 } else { 283 o.dptr = (char *) &order_key; 284 o.dsize = YP_LAST_LEN; 285 o = ypdb_fetch(yp_databas, o); 286 if (o.dptr == NULL) { 287 fprintf(stderr, 288 "yppush: %s: Cannot determine order number\n", 289 Map); 290 } else { 291 OrderNum=0; 292 for (i=0; i<o.dsize-1; i++) { 293 if (!isdigit(o.dptr[i])) 294 OrderNum=0xffffffff; 295 } 296 if (OrderNum != 0) { 297 fprintf(stderr, 298 "yppush: %s: Invalid order number '%s'\n", 299 Map, o.dptr); 300 } else { 301 OrderNum = atoi(o.dptr); 302 } 303 } 304 } 305 306 yp_bind(Domain); 307 308 r = yp_master(Domain, ypmap, &master); 309 if (r != 0) { 310 fprintf(stderr, "yppush: could not get ypservers map\n"); 311 exit(1); 312 } 313 314 if (hostname != NULL) { 315 push(strlen(hostname), hostname); 316 } else { 317 if (Verbose) { 318 printf("Contacting master for ypservers (%s).\n", 319 master); 320 } 321 322 client = yp_bind_host(master, YPPROG, YPVERS, 0, 1); 323 324 ypcb.foreach = pushit; 325 ypcb.data = NULL; 326 r = yp_all_host(client, Domain, ypmap, &ypcb); 327 } 328 329 exit(0); 330 } 331