1 /* $NetBSD: pmap_svc.c,v 1.2 2000/10/20 11:49:40 fvdl Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 * Copyright (c) 2009, Sun Microsystems, Inc. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions are met: 11 * - Redistributions of source code must retain the above copyright notice, 12 * this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright notice, 14 * this list of conditions and the following disclaimer in the documentation 15 * and/or other materials provided with the distribution. 16 * - Neither the name of Sun Microsystems, Inc. nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 /* 33 * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc. 34 */ 35 36 /* #ident "@(#)pmap_svc.c 1.14 93/07/05 SMI" */ 37 38 #if 0 39 #ifndef lint 40 static char sccsid[] = "@(#)pmap_svc.c 1.23 89/04/05 Copyr 1984 Sun Micro"; 41 #endif 42 #endif 43 44 /* 45 * pmap_svc.c 46 * The server procedure for the version 2 portmaper. 47 * All the portmapper related interface from the portmap side. 48 */ 49 50 #ifdef PORTMAP 51 #include <sys/types.h> 52 #include <sys/socket.h> 53 #include <stdio.h> 54 #include <rpc/rpc.h> 55 #include <rpc/pmap_prot.h> 56 #include <rpc/rpcb_prot.h> 57 #ifdef RPCBIND_DEBUG 58 #include <stdlib.h> 59 #endif 60 #include "rpcbind.h" 61 62 static struct pmaplist *find_service_pmap(rpcprog_t, rpcvers_t, 63 rpcprot_t); 64 static bool_t pmapproc_change(struct svc_req *, SVCXPRT *, u_long); 65 static bool_t pmapproc_getport(struct svc_req *, SVCXPRT *); 66 static bool_t pmapproc_dump(struct svc_req *, SVCXPRT *); 67 68 /* 69 * Called for all the version 2 inquiries. 70 */ 71 void 72 pmap_service(struct svc_req *rqstp, SVCXPRT *xprt) 73 { 74 rpcbs_procinfo(RPCBVERS_2_STAT, rqstp->rq_proc); 75 switch (rqstp->rq_proc) { 76 case PMAPPROC_NULL: 77 /* 78 * Null proc call 79 */ 80 #ifdef RPCBIND_DEBUG 81 if (debugging) 82 fprintf(stderr, "PMAPPROC_NULL\n"); 83 #endif 84 check_access(xprt, rqstp->rq_proc, NULL, PMAPVERS); 85 if ((!svc_sendreply(xprt, (xdrproc_t) xdr_void, NULL)) && 86 debugging) { 87 if (doabort) { 88 rpcbind_abort(); 89 } 90 } 91 break; 92 93 case PMAPPROC_SET: 94 /* 95 * Set a program, version to port mapping 96 */ 97 pmapproc_change(rqstp, xprt, rqstp->rq_proc); 98 break; 99 100 case PMAPPROC_UNSET: 101 /* 102 * Remove a program, version to port mapping. 103 */ 104 pmapproc_change(rqstp, xprt, rqstp->rq_proc); 105 break; 106 107 case PMAPPROC_GETPORT: 108 /* 109 * Lookup the mapping for a program, version and return its 110 * port number. 111 */ 112 pmapproc_getport(rqstp, xprt); 113 break; 114 115 case PMAPPROC_DUMP: 116 /* 117 * Return the current set of mapped program, version 118 */ 119 #ifdef RPCBIND_DEBUG 120 if (debugging) 121 fprintf(stderr, "PMAPPROC_DUMP\n"); 122 #endif 123 pmapproc_dump(rqstp, xprt); 124 break; 125 126 case PMAPPROC_CALLIT: 127 /* 128 * Calls a procedure on the local machine. If the requested 129 * procedure is not registered this procedure does not return 130 * error information!! 131 * This procedure is only supported on rpc/udp and calls via 132 * rpc/udp. It passes null authentication parameters. 133 */ 134 rpcbproc_callit_com(rqstp, xprt, PMAPPROC_CALLIT, PMAPVERS); 135 break; 136 137 default: 138 svcerr_noproc(xprt); 139 break; 140 } 141 } 142 143 /* 144 * returns the item with the given program, version number. If that version 145 * number is not found, it returns the item with that program number, so that 146 * the port number is now returned to the caller. The caller when makes a 147 * call to this program, version number, the call will fail and it will 148 * return with PROGVERS_MISMATCH. The user can then determine the highest 149 * and the lowest version number for this program using clnt_geterr() and 150 * use those program version numbers. 151 */ 152 static struct pmaplist * 153 find_service_pmap(rpcprog_t prog, rpcvers_t vers, rpcprot_t prot) 154 { 155 register struct pmaplist *hit = NULL; 156 register struct pmaplist *pml; 157 158 for (pml = list_pml; pml != NULL; pml = pml->pml_next) { 159 if ((pml->pml_map.pm_prog != prog) || 160 (pml->pml_map.pm_prot != prot)) 161 continue; 162 hit = pml; 163 if (pml->pml_map.pm_vers == vers) 164 break; 165 } 166 return (hit); 167 } 168 169 static bool_t 170 pmapproc_change(struct svc_req *rqstp __unused, SVCXPRT *xprt, unsigned long op) 171 { 172 struct pmap reg; 173 RPCB rpcbreg; 174 long ans; 175 struct sockaddr_in *who; 176 uid_t uid; 177 char uidbuf[32]; 178 179 #ifdef RPCBIND_DEBUG 180 if (debugging) 181 fprintf(stderr, "%s request for (%lu, %lu) : ", 182 op == PMAPPROC_SET ? "PMAP_SET" : "PMAP_UNSET", 183 reg.pm_prog, reg.pm_vers); 184 #endif 185 186 if (!svc_getargs(xprt, (xdrproc_t) xdr_pmap, (char *)®)) { 187 svcerr_decode(xprt); 188 return (FALSE); 189 } 190 191 if (!check_access(xprt, op, ®, PMAPVERS)) { 192 svcerr_weakauth(xprt); 193 return FALSE; 194 } 195 196 who = svc_getcaller(xprt); 197 198 /* 199 * Can't use getpwnam here. We might end up calling ourselves 200 * and looping. 201 */ 202 if (__rpc_get_local_uid(xprt, &uid) < 0) 203 rpcbreg.r_owner = "unknown"; 204 else if (uid == 0) 205 rpcbreg.r_owner = "superuser"; 206 else { 207 /* r_owner will be strdup-ed later */ 208 snprintf(uidbuf, sizeof uidbuf, "%d", uid); 209 rpcbreg.r_owner = uidbuf; 210 } 211 212 rpcbreg.r_prog = reg.pm_prog; 213 rpcbreg.r_vers = reg.pm_vers; 214 215 if (op == PMAPPROC_SET) { 216 char buf[32]; 217 218 snprintf(buf, sizeof buf, "0.0.0.0.%d.%d", 219 (int)((reg.pm_port >> 8) & 0xff), 220 (int)(reg.pm_port & 0xff)); 221 rpcbreg.r_addr = buf; 222 if (reg.pm_prot == IPPROTO_UDP) { 223 rpcbreg.r_netid = udptrans; 224 } else if (reg.pm_prot == IPPROTO_TCP) { 225 rpcbreg.r_netid = tcptrans; 226 } else { 227 ans = FALSE; 228 goto done_change; 229 } 230 ans = map_set(&rpcbreg, rpcbreg.r_owner); 231 } else if (op == PMAPPROC_UNSET) { 232 bool_t ans1, ans2; 233 234 rpcbreg.r_addr = NULL; 235 rpcbreg.r_netid = tcptrans; 236 ans1 = map_unset(&rpcbreg, rpcbreg.r_owner); 237 rpcbreg.r_netid = udptrans; 238 ans2 = map_unset(&rpcbreg, rpcbreg.r_owner); 239 ans = ans1 || ans2; 240 } else { 241 ans = FALSE; 242 } 243 done_change: 244 if ((!svc_sendreply(xprt, (xdrproc_t) xdr_long, (caddr_t) &ans)) && 245 debugging) { 246 fprintf(stderr, "portmap: svc_sendreply\n"); 247 if (doabort) { 248 rpcbind_abort(); 249 } 250 } 251 #ifdef RPCBIND_DEBUG 252 if (debugging) 253 fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed"); 254 #endif 255 if (op == PMAPPROC_SET) 256 rpcbs_set(RPCBVERS_2_STAT, ans); 257 else 258 rpcbs_unset(RPCBVERS_2_STAT, ans); 259 return (TRUE); 260 } 261 262 /* ARGSUSED */ 263 static bool_t 264 pmapproc_getport(struct svc_req *rqstp __unused, SVCXPRT *xprt) 265 { 266 struct pmap reg; 267 long lport; 268 int port = 0; 269 struct pmaplist *fnd; 270 #ifdef RPCBIND_DEBUG 271 char *uaddr; 272 #endif 273 274 if (!svc_getargs(xprt, (xdrproc_t) xdr_pmap, (char *)®)) { 275 svcerr_decode(xprt); 276 return (FALSE); 277 } 278 279 if (!check_access(xprt, PMAPPROC_GETPORT, ®, PMAPVERS)) { 280 svcerr_weakauth(xprt); 281 return FALSE; 282 } 283 284 #ifdef RPCBIND_DEBUG 285 if (debugging) { 286 uaddr = taddr2uaddr(rpcbind_get_conf(xprt->xp_netid), 287 svc_getrpccaller(xprt)); 288 fprintf(stderr, "PMAP_GETPORT req for (%lu, %lu, %s) from %s :", 289 reg.pm_prog, reg.pm_vers, 290 reg.pm_prot == IPPROTO_UDP ? "udp" : "tcp", uaddr); 291 free(uaddr); 292 } 293 #endif 294 fnd = find_service_pmap(reg.pm_prog, reg.pm_vers, reg.pm_prot); 295 if (fnd) { 296 char serveuaddr[32], *ua; 297 int h1, h2, h3, h4, p1, p2; 298 char *netid; 299 300 if (reg.pm_prot == IPPROTO_UDP) { 301 ua = udp_uaddr; 302 netid = udptrans; 303 } else { 304 ua = tcp_uaddr; /* To get the len */ 305 netid = tcptrans; 306 } 307 if (ua == NULL) { 308 goto sendreply; 309 } 310 if (sscanf(ua, "%d.%d.%d.%d.%d.%d", &h1, &h2, &h3, 311 &h4, &p1, &p2) == 6) { 312 p1 = (fnd->pml_map.pm_port >> 8) & 0xff; 313 p2 = (fnd->pml_map.pm_port) & 0xff; 314 snprintf(serveuaddr, sizeof serveuaddr, 315 "%d.%d.%d.%d.%d.%d", h1, h2, h3, h4, p1, p2); 316 if (is_bound(netid, serveuaddr)) { 317 port = fnd->pml_map.pm_port; 318 } else { /* this service is dead; delete it */ 319 delete_prog(reg.pm_prog); 320 } 321 } 322 } 323 sendreply: 324 lport = port; 325 if ((!svc_sendreply(xprt, (xdrproc_t) xdr_long, (caddr_t)&lport)) && 326 debugging) { 327 (void) fprintf(stderr, "portmap: svc_sendreply\n"); 328 if (doabort) { 329 rpcbind_abort(); 330 } 331 } 332 #ifdef RPCBIND_DEBUG 333 if (debugging) 334 fprintf(stderr, "port = %d\n", port); 335 #endif 336 rpcbs_getaddr(RPCBVERS_2_STAT, reg.pm_prog, reg.pm_vers, 337 reg.pm_prot == IPPROTO_UDP ? udptrans : tcptrans, 338 port ? udptrans : ""); 339 340 return (TRUE); 341 } 342 343 /* ARGSUSED */ 344 static bool_t 345 pmapproc_dump(struct svc_req *rqstp __unused, SVCXPRT *xprt) 346 { 347 if (!svc_getargs(xprt, (xdrproc_t)xdr_void, NULL)) { 348 svcerr_decode(xprt); 349 return (FALSE); 350 } 351 352 if (!check_access(xprt, PMAPPROC_DUMP, NULL, PMAPVERS)) { 353 svcerr_weakauth(xprt); 354 return FALSE; 355 } 356 357 if ((!svc_sendreply(xprt, (xdrproc_t) xdr_pmaplist_ptr, 358 (caddr_t)&list_pml)) && debugging) { 359 if (debugging) 360 (void) fprintf(stderr, "portmap: svc_sendreply\n"); 361 if (doabort) { 362 rpcbind_abort(); 363 } 364 } 365 return (TRUE); 366 } 367 368 #endif /* PORTMAP */ 369