1 /* 2 * Copyright (c) 2009, Sun Microsystems, Inc. 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 are met: 7 * - Redistributions of source code must retain the above copyright notice, 8 * this list of conditions and the following disclaimer. 9 * - Redistributions in binary form must reproduce the above copyright notice, 10 * this list of conditions and the following disclaimer in the documentation 11 * and/or other materials provided with the distribution. 12 * - Neither the name of Sun Microsystems, Inc. nor the names of its 13 * contributors may be used to endorse or promote products derived 14 * from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 * 28 * @(#)rpcb_svc_4.c 1.8 93/07/05 SMI 29 * $NetBSD: rpcb_svc_4.c,v 1.1 2000/06/02 23:15:41 fvdl Exp $ 30 * $FreeBSD: src/usr.sbin/rpcbind/rpcb_svc_4.c,v 1.5 2007/11/07 10:53:39 kevlo Exp $ 31 */ 32 /* 33 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc. 34 */ 35 36 /* 37 * rpcb_svc_4.c 38 * The server procedure for the version 4 rpcbind. 39 * 40 */ 41 42 #include <sys/types.h> 43 #include <sys/stat.h> 44 #include <rpc/rpc.h> 45 #include <stdio.h> 46 #include <unistd.h> 47 #include <netconfig.h> 48 #include <syslog.h> 49 #include <string.h> 50 #include <stdlib.h> 51 #include "rpcbind.h" 52 53 static void *rpcbproc_getaddr_4_local(void *, struct svc_req *, SVCXPRT *, 54 rpcvers_t); 55 static void *rpcbproc_getversaddr_4_local(void *, struct svc_req *, 56 SVCXPRT *, rpcvers_t); 57 static void *rpcbproc_getaddrlist_4_local(void *, struct svc_req *, 58 SVCXPRT *, rpcvers_t); 59 static void free_rpcb_entry_list(rpcb_entry_list_ptr *); 60 static void *rpcbproc_dump_4_local(void *, struct svc_req *, SVCXPRT *, 61 rpcvers_t); 62 63 /* 64 * Called by svc_getreqset. There is a separate server handle for 65 * every transport that it waits on. 66 */ 67 void 68 rpcb_service_4(struct svc_req *rqstp, SVCXPRT *transp) 69 { 70 union { 71 rpcb rpcbproc_set_4_arg; 72 rpcb rpcbproc_unset_4_arg; 73 rpcb rpcbproc_getaddr_4_local_arg; 74 char *rpcbproc_uaddr2taddr_4_arg; 75 struct netbuf rpcbproc_taddr2uaddr_4_arg; 76 } argument; 77 char *result; 78 xdrproc_t xdr_argument, xdr_result; 79 void *(*local)(void *, struct svc_req *, SVCXPRT *, rpcvers_t); 80 81 rpcbs_procinfo(RPCBVERS_4_STAT, rqstp->rq_proc); 82 83 switch (rqstp->rq_proc) { 84 case NULLPROC: 85 /* 86 * Null proc call 87 */ 88 #ifdef RPCBIND_DEBUG 89 if (debugging) 90 fprintf(stderr, "RPCBPROC_NULL\n"); 91 #endif 92 check_access(transp, rqstp->rq_proc, NULL, RPCBVERS4); 93 svc_sendreply(transp, (xdrproc_t) xdr_void, NULL); 94 return; 95 96 case RPCBPROC_SET: 97 /* 98 * Check to see whether the message came from 99 * loopback transports (for security reasons) 100 */ 101 xdr_argument = (xdrproc_t)xdr_rpcb; 102 xdr_result = (xdrproc_t)xdr_bool; 103 local = rpcbproc_set_com; 104 break; 105 106 case RPCBPROC_UNSET: 107 /* 108 * Check to see whether the message came from 109 * loopback transports (for security reasons) 110 */ 111 xdr_argument = (xdrproc_t)xdr_rpcb; 112 xdr_result = (xdrproc_t)xdr_bool; 113 local = rpcbproc_unset_com; 114 break; 115 116 case RPCBPROC_GETADDR: 117 xdr_argument = (xdrproc_t)xdr_rpcb; 118 xdr_result = (xdrproc_t)xdr_wrapstring; 119 local = rpcbproc_getaddr_4_local; 120 break; 121 122 case RPCBPROC_GETVERSADDR: 123 #ifdef RPCBIND_DEBUG 124 if (debugging) 125 fprintf(stderr, "RPCBPROC_GETVERSADDR\n"); 126 #endif 127 xdr_argument = (xdrproc_t)xdr_rpcb; 128 xdr_result = (xdrproc_t)xdr_wrapstring; 129 local = rpcbproc_getversaddr_4_local; 130 break; 131 132 case RPCBPROC_DUMP: 133 #ifdef RPCBIND_DEBUG 134 if (debugging) 135 fprintf(stderr, "RPCBPROC_DUMP\n"); 136 #endif 137 xdr_argument = (xdrproc_t)xdr_void; 138 xdr_result = (xdrproc_t)xdr_rpcblist_ptr; 139 local = rpcbproc_dump_4_local; 140 break; 141 142 case RPCBPROC_INDIRECT: 143 #ifdef RPCBIND_DEBUG 144 if (debugging) 145 fprintf(stderr, "RPCBPROC_INDIRECT\n"); 146 #endif 147 rpcbproc_callit_com(rqstp, transp, rqstp->rq_proc, RPCBVERS4); 148 return; 149 150 /* case RPCBPROC_CALLIT: */ 151 case RPCBPROC_BCAST: 152 #ifdef RPCBIND_DEBUG 153 if (debugging) 154 fprintf(stderr, "RPCBPROC_BCAST\n"); 155 #endif 156 rpcbproc_callit_com(rqstp, transp, rqstp->rq_proc, RPCBVERS4); 157 return; 158 159 case RPCBPROC_GETTIME: 160 #ifdef RPCBIND_DEBUG 161 if (debugging) 162 fprintf(stderr, "RPCBPROC_GETTIME\n"); 163 #endif 164 xdr_argument = (xdrproc_t)xdr_void; 165 xdr_result = (xdrproc_t)xdr_u_long; 166 local = rpcbproc_gettime_com; 167 break; 168 169 case RPCBPROC_UADDR2TADDR: 170 #ifdef RPCBIND_DEBUG 171 if (debugging) 172 fprintf(stderr, "RPCBPROC_UADDR2TADDR\n"); 173 #endif 174 xdr_argument = (xdrproc_t)xdr_wrapstring; 175 xdr_result = (xdrproc_t)xdr_netbuf; 176 local = rpcbproc_uaddr2taddr_com; 177 break; 178 179 case RPCBPROC_TADDR2UADDR: 180 #ifdef RPCBIND_DEBUG 181 if (debugging) 182 fprintf(stderr, "RPCBPROC_TADDR2UADDR\n"); 183 #endif 184 xdr_argument = (xdrproc_t)xdr_netbuf; 185 xdr_result = (xdrproc_t)xdr_wrapstring; 186 local = rpcbproc_taddr2uaddr_com; 187 break; 188 189 case RPCBPROC_GETADDRLIST: 190 #ifdef RPCBIND_DEBUG 191 if (debugging) 192 fprintf(stderr, "RPCBPROC_GETADDRLIST\n"); 193 #endif 194 xdr_argument = (xdrproc_t)xdr_rpcb; 195 xdr_result = (xdrproc_t)xdr_rpcb_entry_list_ptr; 196 local = rpcbproc_getaddrlist_4_local; 197 break; 198 199 case RPCBPROC_GETSTAT: 200 #ifdef RPCBIND_DEBUG 201 if (debugging) 202 fprintf(stderr, "RPCBPROC_GETSTAT\n"); 203 #endif 204 xdr_argument = (xdrproc_t)xdr_void; 205 xdr_result = (xdrproc_t)xdr_rpcb_stat_byvers; 206 local = rpcbproc_getstat; 207 break; 208 209 default: 210 svcerr_noproc(transp); 211 return; 212 } 213 memset((char *)&argument, 0, sizeof (argument)); 214 if (!svc_getargs(transp, (xdrproc_t) xdr_argument, 215 (char *)&argument)) { 216 svcerr_decode(transp); 217 if (debugging) 218 fprintf(stderr, "rpcbind: could not decode\n"); 219 return; 220 } 221 if (!check_access(transp, rqstp->rq_proc, &argument, RPCBVERS4)) { 222 svcerr_weakauth(transp); 223 goto done; 224 } 225 result = (*local)(&argument, rqstp, transp, RPCBVERS4); 226 if (result != NULL && !svc_sendreply(transp, (xdrproc_t) xdr_result, 227 result)) { 228 svcerr_systemerr(transp); 229 if (debugging) { 230 fprintf(stderr, "rpcbind: svc_sendreply\n"); 231 if (doabort) { 232 rpcbind_abort(); 233 } 234 } 235 } 236 done: 237 if (!svc_freeargs(transp, (xdrproc_t) xdr_argument, 238 (char *)&argument)) { 239 if (debugging) { 240 fprintf(stderr, "unable to free arguments\n"); 241 if (doabort) { 242 rpcbind_abort(); 243 } 244 } 245 } 246 return; 247 } 248 249 /* 250 * Lookup the mapping for a program, version and return its 251 * address. Assuming that the caller wants the address of the 252 * server running on the transport on which the request came. 253 * Even if a service with a different version number is available, 254 * it will return that address. The client should check with an 255 * clnt_call to verify whether the service is the one that is desired. 256 * We also try to resolve the universal address in terms of 257 * address of the caller. 258 */ 259 /* ARGSUSED */ 260 static void * 261 rpcbproc_getaddr_4_local(void *arg, struct svc_req *rqstp, SVCXPRT *transp, 262 rpcvers_t rpcbversnum __unused) 263 { 264 RPCB *regp = (RPCB *)arg; 265 #ifdef RPCBIND_DEBUG 266 if (debugging) { 267 char *uaddr; 268 269 uaddr = taddr2uaddr(rpcbind_get_conf(transp->xp_netid), 270 svc_getrpccaller(transp)); 271 fprintf(stderr, "RPCB_GETADDR req for (%lu, %lu, %s) from %s: ", 272 (unsigned long)regp->r_prog, (unsigned long)regp->r_vers, 273 regp->r_netid, uaddr); 274 free(uaddr); 275 } 276 #endif 277 return (rpcbproc_getaddr_com(regp, rqstp, transp, RPCBVERS4, 278 RPCB_ALLVERS)); 279 } 280 281 /* 282 * Lookup the mapping for a program, version and return its 283 * address. Assuming that the caller wants the address of the 284 * server running on the transport on which the request came. 285 * 286 * We also try to resolve the universal address in terms of 287 * address of the caller. 288 */ 289 /* ARGSUSED */ 290 static void * 291 rpcbproc_getversaddr_4_local(void *arg, struct svc_req *rqstp, SVCXPRT *transp, 292 rpcvers_t versnum __unused) 293 { 294 RPCB *regp = (RPCB *)arg; 295 #ifdef RPCBIND_DEBUG 296 if (debugging) { 297 char *uaddr; 298 299 uaddr = taddr2uaddr(rpcbind_get_conf(transp->xp_netid), 300 svc_getrpccaller(transp)); 301 fprintf(stderr, "RPCB_GETVERSADDR rqst for (%lu, %lu, %s)" 302 " from %s : ", 303 (unsigned long)regp->r_prog, (unsigned long)regp->r_vers, 304 regp->r_netid, uaddr); 305 free(uaddr); 306 } 307 #endif 308 return (rpcbproc_getaddr_com(regp, rqstp, transp, RPCBVERS4, 309 RPCB_ONEVERS)); 310 } 311 312 /* 313 * Lookup the mapping for a program, version and return the 314 * addresses for all transports in the current transport family. 315 * We return a merged address. 316 */ 317 /* ARGSUSED */ 318 static void * 319 rpcbproc_getaddrlist_4_local(void *arg, struct svc_req *rqstp __unused, 320 SVCXPRT *transp, rpcvers_t versnum __unused) 321 { 322 RPCB *regp = (RPCB *)arg; 323 static rpcb_entry_list_ptr rlist; 324 rpcblist_ptr rbl; 325 rpcb_entry_list_ptr rp, tail; 326 rpcprog_t prog; 327 rpcvers_t vers; 328 rpcb_entry *a; 329 struct netconfig *nconf; 330 struct netconfig *reg_nconf; 331 char *saddr, *maddr = NULL; 332 333 free_rpcb_entry_list(&rlist); 334 tail = NULL; 335 prog = regp->r_prog; 336 vers = regp->r_vers; 337 reg_nconf = rpcbind_get_conf(transp->xp_netid); 338 if (reg_nconf == NULL) 339 return (NULL); 340 if (*(regp->r_addr) != '\0') { 341 saddr = regp->r_addr; 342 } else { 343 saddr = NULL; 344 } 345 #ifdef RPCBIND_DEBUG 346 if (debugging) { 347 fprintf(stderr, "r_addr: %s r_netid: %s nc_protofmly: %s\n", 348 regp->r_addr, regp->r_netid, reg_nconf->nc_protofmly); 349 } 350 #endif 351 for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) { 352 if ((rbl->rpcb_map.r_prog == prog) && 353 (rbl->rpcb_map.r_vers == vers)) { 354 nconf = rpcbind_get_conf(rbl->rpcb_map.r_netid); 355 if (nconf == NULL) 356 goto fail; 357 if (strcmp(nconf->nc_protofmly, reg_nconf->nc_protofmly) 358 != 0) { 359 continue; /* not same proto family */ 360 } 361 #ifdef RPCBIND_DEBUG 362 if (debugging) 363 fprintf(stderr, "\tmerge with: %s\n", 364 rbl->rpcb_map.r_addr); 365 #endif 366 if ((maddr = mergeaddr(transp, rbl->rpcb_map.r_netid, 367 rbl->rpcb_map.r_addr, saddr)) == NULL) { 368 #ifdef RPCBIND_DEBUG 369 if (debugging) 370 fprintf(stderr, " FAILED\n"); 371 #endif 372 continue; 373 } else if (!maddr[0]) { 374 #ifdef RPCBIND_DEBUG 375 if (debugging) 376 fprintf(stderr, " SUCCEEDED, but port died - maddr: nullstring\n"); 377 #endif 378 /* The server died. Unset this combination */ 379 delete_prog(regp->r_prog); 380 continue; 381 } 382 #ifdef RPCBIND_DEBUG 383 if (debugging) 384 fprintf(stderr, " SUCCEEDED maddr: %s\n", maddr); 385 #endif 386 /* 387 * Add it to rlist. 388 */ 389 rp = malloc(sizeof (rpcb_entry_list)); 390 if (rp == NULL) 391 goto fail; 392 a = &rp->rpcb_entry_map; 393 a->r_maddr = maddr; 394 a->r_nc_netid = nconf->nc_netid; 395 a->r_nc_semantics = nconf->nc_semantics; 396 a->r_nc_protofmly = nconf->nc_protofmly; 397 a->r_nc_proto = nconf->nc_proto; 398 rp->rpcb_entry_next = NULL; 399 if (rlist == NULL) { 400 rlist = rp; 401 tail = rp; 402 } else { 403 tail->rpcb_entry_next = rp; 404 tail = rp; 405 } 406 rp = NULL; 407 } 408 } 409 #ifdef RPCBIND_DEBUG 410 if (debugging) { 411 for (rp = rlist; rp; rp = rp->rpcb_entry_next) { 412 fprintf(stderr, "\t%s %s\n", rp->rpcb_entry_map.r_maddr, 413 rp->rpcb_entry_map.r_nc_proto); 414 } 415 } 416 #endif 417 /* 418 * XXX: getaddrlist info is also being stuffed into getaddr. 419 * Perhaps wrong, but better than it not getting counted at all. 420 */ 421 rpcbs_getaddr(RPCBVERS4 - 2, prog, vers, transp->xp_netid, maddr); 422 return (void *)&rlist; 423 424 fail: free_rpcb_entry_list(&rlist); 425 return (NULL); 426 } 427 428 /* 429 * Free only the allocated structure, rest is all a pointer to some 430 * other data somewhere else. 431 */ 432 static void 433 free_rpcb_entry_list(rpcb_entry_list_ptr *rlistp) 434 { 435 rpcb_entry_list_ptr rbl, tmp; 436 437 for (rbl = *rlistp; rbl != NULL; ) { 438 tmp = rbl; 439 rbl = rbl->rpcb_entry_next; 440 free((char *)tmp->rpcb_entry_map.r_maddr); 441 free((char *)tmp); 442 } 443 *rlistp = NULL; 444 } 445 446 /* ARGSUSED */ 447 static void * 448 rpcbproc_dump_4_local(void *arg __unused, struct svc_req *req __unused, 449 SVCXPRT *xprt __unused, rpcvers_t versnum __unused) 450 { 451 return ((void *)&list_rbl); 452 } 453