1 /* 2 * $NetBSD: rpcb_svc_4.c,v 1.1 2000/06/02 23:15:41 fvdl Exp $ 3 */ 4 5 /*- 6 * SPDX-License-Identifier: BSD-3-Clause 7 * 8 * Copyright (c) 2009, Sun Microsystems, Inc. 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions are met: 13 * - Redistributions of source code must retain the above copyright notice, 14 * this list of conditions and the following disclaimer. 15 * - Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * - Neither the name of Sun Microsystems, Inc. nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 /* 35 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc. 36 */ 37 38 /* 39 * rpcb_svc_4.c 40 * The server procedure for the version 4 rpcbind. 41 * 42 */ 43 44 #include <sys/types.h> 45 #include <sys/stat.h> 46 #include <rpc/rpc.h> 47 #include <stdio.h> 48 #include <netconfig.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 *, SVCXPRT *, 56 rpcvers_t); 57 static void *rpcbproc_getaddrlist_4_local(void *, struct svc_req *, SVCXPRT *, 58 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 (void) 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 (void) 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 (void) 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 (void) fprintf(stderr, "unable to free arguments\n"); 241 if (doabort) { 242 rpcbind_abort(); 243 } 244 } 245 } 246 } 247 248 /* 249 * Lookup the mapping for a program, version and return its 250 * address. Assuming that the caller wants the address of the 251 * server running on the transport on which the request came. 252 * Even if a service with a different version number is available, 253 * it will return that address. The client should check with an 254 * clnt_call to verify whether the service is the one that is desired. 255 * We also try to resolve the universal address in terms of 256 * address of the caller. 257 */ 258 /* ARGSUSED */ 259 static void * 260 rpcbproc_getaddr_4_local(void *arg, struct svc_req *rqstp, SVCXPRT *transp, 261 rpcvers_t rpcbversnum __unused) 262 { 263 RPCB *regp = (RPCB *)arg; 264 #ifdef RPCBIND_DEBUG 265 if (debugging) { 266 char *uaddr; 267 268 uaddr = taddr2uaddr(rpcbind_get_conf(transp->xp_netid), 269 svc_getrpccaller(transp)); 270 fprintf(stderr, "RPCB_GETADDR req for (%lu, %lu, %s) from %s: ", 271 (unsigned long)regp->r_prog, (unsigned long)regp->r_vers, 272 regp->r_netid, uaddr); 273 free(uaddr); 274 } 275 #endif 276 return (rpcbproc_getaddr_com(regp, rqstp, transp, RPCBVERS4, 277 RPCB_ALLVERS)); 278 } 279 280 /* 281 * Lookup the mapping for a program, version and return its 282 * address. Assuming that the caller wants the address of the 283 * server running on the transport on which the request came. 284 * 285 * We also try to resolve the universal address in terms of 286 * address of the caller. 287 */ 288 /* ARGSUSED */ 289 static void * 290 rpcbproc_getversaddr_4_local(void *arg, struct svc_req *rqstp, SVCXPRT *transp, 291 rpcvers_t versnum __unused) 292 { 293 RPCB *regp = (RPCB *)arg; 294 #ifdef RPCBIND_DEBUG 295 if (debugging) { 296 char *uaddr; 297 298 uaddr = taddr2uaddr(rpcbind_get_conf(transp->xp_netid), 299 svc_getrpccaller(transp)); 300 fprintf(stderr, "RPCB_GETVERSADDR rqst for (%lu, %lu, %s)" 301 " from %s : ", 302 (unsigned long)regp->r_prog, (unsigned long)regp->r_vers, 303 regp->r_netid, uaddr); 304 free(uaddr); 305 } 306 #endif 307 return (rpcbproc_getaddr_com(regp, rqstp, transp, RPCBVERS4, 308 RPCB_ONEVERS)); 309 } 310 311 /* 312 * Lookup the mapping for a program, version and return the 313 * addresses for all transports in the current transport family. 314 * We return a merged address. 315 */ 316 /* ARGSUSED */ 317 static void * 318 rpcbproc_getaddrlist_4_local(void *arg, struct svc_req *rqstp __unused, 319 SVCXPRT *transp, rpcvers_t versnum __unused) 320 { 321 RPCB *regp = (RPCB *)arg; 322 static rpcb_entry_list_ptr rlist; 323 register rpcblist_ptr rbl; 324 rpcb_entry_list_ptr rp, tail; 325 rpcprog_t prog; 326 rpcvers_t vers; 327 rpcb_entry *a; 328 struct netconfig *nconf; 329 struct netconfig *reg_nconf; 330 char *saddr, *maddr = NULL; 331 332 free_rpcb_entry_list(&rlist); 333 tail = NULL; 334 prog = regp->r_prog; 335 vers = regp->r_vers; 336 reg_nconf = rpcbind_get_conf(transp->xp_netid); 337 if (reg_nconf == NULL) 338 return (NULL); 339 if (*(regp->r_addr) != '\0') { 340 saddr = regp->r_addr; 341 } else { 342 saddr = NULL; 343 } 344 #ifdef RPCBIND_DEBUG 345 if (debugging) { 346 fprintf(stderr, "r_addr: %s r_netid: %s nc_protofmly: %s\n", 347 regp->r_addr, regp->r_netid, reg_nconf->nc_protofmly); 348 } 349 #endif 350 for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) { 351 if ((rbl->rpcb_map.r_prog == prog) && 352 (rbl->rpcb_map.r_vers == vers)) { 353 nconf = rpcbind_get_conf(rbl->rpcb_map.r_netid); 354 if (nconf == NULL) 355 goto fail; 356 if (strcmp(nconf->nc_protofmly, reg_nconf->nc_protofmly) 357 != 0) { 358 continue; /* not same proto family */ 359 } 360 #ifdef RPCBIND_DEBUG 361 if (debugging) 362 fprintf(stderr, "\tmerge with: %s\n", 363 rbl->rpcb_map.r_addr); 364 #endif 365 if ((maddr = mergeaddr(transp, rbl->rpcb_map.r_netid, 366 rbl->rpcb_map.r_addr, saddr)) == NULL) { 367 #ifdef RPCBIND_DEBUG 368 if (debugging) 369 fprintf(stderr, " FAILED\n"); 370 #endif 371 continue; 372 } else if (!maddr[0]) { 373 #ifdef RPCBIND_DEBUG 374 if (debugging) 375 fprintf(stderr, " SUCCEEDED, but port died - maddr: nullstring\n"); 376 #endif 377 /* The server died. Unset this combination */ 378 delete_prog(regp->r_prog); 379 continue; 380 } 381 #ifdef RPCBIND_DEBUG 382 if (debugging) 383 fprintf(stderr, " SUCCEEDED maddr: %s\n", maddr); 384 #endif 385 /* 386 * Add it to rlist. 387 */ 388 rp = malloc(sizeof(*rp)); 389 if (rp == NULL) 390 goto fail; 391 a = &rp->rpcb_entry_map; 392 a->r_maddr = maddr; 393 a->r_nc_netid = nconf->nc_netid; 394 a->r_nc_semantics = nconf->nc_semantics; 395 a->r_nc_protofmly = nconf->nc_protofmly; 396 a->r_nc_proto = nconf->nc_proto; 397 rp->rpcb_entry_next = NULL; 398 if (rlist == NULL) { 399 rlist = rp; 400 tail = rp; 401 } else if (tail != NULL) { 402 tail->rpcb_entry_next = rp; 403 tail = rp; 404 } 405 rp = NULL; 406 } 407 } 408 #ifdef RPCBIND_DEBUG 409 if (debugging) { 410 for (rp = rlist; rp; rp = rp->rpcb_entry_next) { 411 fprintf(stderr, "\t%s %s\n", rp->rpcb_entry_map.r_maddr, 412 rp->rpcb_entry_map.r_nc_proto); 413 } 414 } 415 #endif 416 /* 417 * XXX: getaddrlist info is also being stuffed into getaddr. 418 * Perhaps wrong, but better than it not getting counted at all. 419 */ 420 rpcbs_getaddr(RPCBVERS4 - 2, prog, vers, transp->xp_netid, maddr); 421 return (&rlist); 422 423 fail: 424 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 register 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