1 /* $NetBSD: rpcb_svc_4.c,v 1.2 2001/04/30 00:36:07 fvdl Exp $ */ 2 3 /* 4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 5 * unrestricted use provided that this legend is included on all tape 6 * media and as a part of the software program in whole or part. Users 7 * may copy or modify Sun RPC without charge, but are not authorized 8 * to license or distribute it to anyone else except as part of a product or 9 * program developed by the user. 10 * 11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 14 * 15 * Sun RPC is provided with no support and without any obligation on the 16 * part of Sun Microsystems, Inc. to assist in its use, correction, 17 * modification or enhancement. 18 * 19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 21 * OR ANY PART THEREOF. 22 * 23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 24 * or profits or other special, indirect and consequential damages, even if 25 * Sun has been advised of the possibility of such damages. 26 * 27 * Sun Microsystems, Inc. 28 * 2550 Garcia Avenue 29 * Mountain View, California 94043 30 */ 31 /* 32 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc. 33 */ 34 35 /* #ident "@(#)rpcb_svc_4.c 1.8 93/07/05 SMI" */ 36 37 /* 38 * rpcb_svc_4.c 39 * The server procedure for the version 4 rpcbind. 40 * 41 */ 42 43 #include <sys/types.h> 44 #include <sys/stat.h> 45 #include <rpc/rpc.h> 46 #include <stdio.h> 47 #include <unistd.h> 48 #include <netconfig.h> 49 #include <syslog.h> 50 #include <string.h> 51 #include <stdlib.h> 52 #include "rpcbind.h" 53 54 static void *rpcbproc_getaddr_4_local __P((void *, struct svc_req *, SVCXPRT *, 55 rpcvers_t)); 56 static void *rpcbproc_getversaddr_4_local __P((void *, struct svc_req *, SVCXPRT *, rpcvers_t)); 57 static void *rpcbproc_getaddrlist_4_local 58 __P((void *, struct svc_req *, SVCXPRT *, rpcvers_t)); 59 static void free_rpcb_entry_list __P((rpcb_entry_list_ptr *)); 60 static void *rpcbproc_dump_4_local __P((void *, struct svc_req *, SVCXPRT *, rpcvers_t)); 61 62 /* 63 * Called by svc_getreqset. There is a separate server handle for 64 * every transport that it waits on. 65 */ 66 void 67 rpcb_service_4(struct svc_req *rqstp, SVCXPRT *transp) 68 { 69 union { 70 rpcb rpcbproc_set_4_arg; 71 rpcb rpcbproc_unset_4_arg; 72 rpcb rpcbproc_getaddr_4_local_arg; 73 char *rpcbproc_uaddr2taddr_4_arg; 74 struct netbuf rpcbproc_taddr2uaddr_4_arg; 75 } argument; 76 char *result; 77 xdrproc_t xdr_argument, xdr_result; 78 void *(*local) __P((void *, struct svc_req *, SVCXPRT *, rpcvers_t)); 79 80 rpcbs_procinfo(RPCBVERS_4_STAT, rqstp->rq_proc); 81 82 switch (rqstp->rq_proc) { 83 case NULLPROC: 84 /* 85 * Null proc call 86 */ 87 #ifdef RPCBIND_DEBUG 88 if (debugging) 89 fprintf(stderr, "RPCBPROC_NULL\n"); 90 #endif 91 check_access(transp, rqstp->rq_proc, NULL, RPCBVERS4); 92 (void) svc_sendreply(transp, (xdrproc_t) xdr_void, 93 (char *)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 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) 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) 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, SVCXPRT *transp, 320 rpcvers_t versnum) 321 { 322 RPCB *regp = (RPCB *)arg; 323 static rpcb_entry_list_ptr rlist; 324 register 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 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", rbl->rpcb_map.r_addr); 363 #endif 364 if ((maddr = mergeaddr(transp, rbl->rpcb_map.r_netid, 365 rbl->rpcb_map.r_addr, saddr)) == NULL) { 366 #ifdef RPCBIND_DEBUG 367 if (debugging) 368 fprintf(stderr, " FAILED\n"); 369 #endif 370 continue; 371 } else if (!maddr[0]) { 372 #ifdef RPCBIND_DEBUG 373 if (debugging) 374 fprintf(stderr, " SUCCEEDED, but port died - maddr: nullstring\n"); 375 #endif 376 /* The server died. Unset this combination */ 377 delete_prog(regp->r_prog); 378 continue; 379 } 380 #ifdef RPCBIND_DEBUG 381 if (debugging) 382 fprintf(stderr, " SUCCEEDED maddr: %s\n", maddr); 383 #endif 384 /* 385 * Add it to rlist. 386 */ 387 rp = (rpcb_entry_list_ptr) 388 malloc((u_int)sizeof (rpcb_entry_list)); 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 { 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 (void *)&rlist; 422 423 fail: free_rpcb_entry_list(&rlist); 424 return (NULL); 425 } 426 427 /* 428 * Free only the allocated structure, rest is all a pointer to some 429 * other data somewhere else. 430 */ 431 static void 432 free_rpcb_entry_list(rpcb_entry_list_ptr *rlistp) 433 { 434 register rpcb_entry_list_ptr rbl, tmp; 435 436 for (rbl = *rlistp; rbl != NULL; ) { 437 tmp = rbl; 438 rbl = rbl->rpcb_entry_next; 439 free((char *)tmp->rpcb_entry_map.r_maddr); 440 free((char *)tmp); 441 } 442 *rlistp = NULL; 443 } 444 445 /* ARGSUSED */ 446 static void * 447 rpcbproc_dump_4_local(void *arg, struct svc_req *req, SVCXPRT *xprt, 448 rpcvers_t versnum) 449 { 450 return ((void *)&list_rbl); 451 } 452