2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)pmap_svc.c 1.14 93/07/05 SMI; 1.23 89/04/05 Copyr 1984 Sun Micro
30 * $NetBSD: pmap_svc.c,v 1.2 2000/10/20 11:49:40 fvdl Exp $
31 * $FreeBSD: src/usr.sbin/rpcbind/pmap_svc.c,v 1.5 2007/11/07 10:53:39 kevlo Exp $
34 * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
39 * The server procedure for the version 2 portmaper.
40 * All the portmapper related interface from the portmap side.
44 #include <sys/types.h>
45 #include <sys/socket.h>
48 #include <rpc/pmap_prot.h>
49 #include <rpc/rpcb_prot.h>
55 static struct pmaplist *find_service_pmap(rpcprog_t, rpcvers_t, rpcprot_t);
56 static bool_t pmapproc_change(struct svc_req *, SVCXPRT *, u_long);
57 static bool_t pmapproc_getport(struct svc_req *, SVCXPRT *);
58 static bool_t pmapproc_dump(struct svc_req *, SVCXPRT *);
61 * Called for all the version 2 inquiries.
64 pmap_service(struct svc_req *rqstp, SVCXPRT *xprt)
66 rpcbs_procinfo(RPCBVERS_2_STAT, rqstp->rq_proc);
67 switch (rqstp->rq_proc) {
74 fprintf(stderr, "PMAPPROC_NULL\n");
76 check_access(xprt, rqstp->rq_proc, NULL, PMAPVERS);
77 if ((!svc_sendreply(xprt, (xdrproc_t) xdr_void, NULL)) &&
87 * Set a program, version to port mapping
89 pmapproc_change(rqstp, xprt, rqstp->rq_proc);
94 * Remove a program, version to port mapping.
96 pmapproc_change(rqstp, xprt, rqstp->rq_proc);
99 case PMAPPROC_GETPORT:
101 * Lookup the mapping for a program, version and return its
104 pmapproc_getport(rqstp, xprt);
109 * Return the current set of mapped program, version
113 fprintf(stderr, "PMAPPROC_DUMP\n");
115 pmapproc_dump(rqstp, xprt);
118 case PMAPPROC_CALLIT:
120 * Calls a procedure on the local machine. If the requested
121 * procedure is not registered this procedure does not return
122 * error information!!
123 * This procedure is only supported on rpc/udp and calls via
124 * rpc/udp. It passes null authentication parameters.
126 rpcbproc_callit_com(rqstp, xprt, PMAPPROC_CALLIT, PMAPVERS);
136 * returns the item with the given program, version number. If that version
137 * number is not found, it returns the item with that program number, so that
138 * the port number is now returned to the caller. The caller when makes a
139 * call to this program, version number, the call will fail and it will
140 * return with PROGVERS_MISMATCH. The user can then determine the highest
141 * and the lowest version number for this program using clnt_geterr() and
142 * use those program version numbers.
144 static struct pmaplist *
145 find_service_pmap(rpcprog_t prog, rpcvers_t vers, rpcprot_t prot)
147 struct pmaplist *hit = NULL;
148 struct pmaplist *pml;
150 for (pml = list_pml; pml != NULL; pml = pml->pml_next) {
151 if ((pml->pml_map.pm_prog != prog) ||
152 (pml->pml_map.pm_prot != prot))
155 if (pml->pml_map.pm_vers == vers)
162 pmapproc_change(struct svc_req *rqstp __unused, SVCXPRT *xprt, unsigned long op)
167 struct sockaddr_in *who;
173 fprintf(stderr, "%s request for (%lu, %lu) : ",
174 op == PMAPPROC_SET ? "PMAP_SET" : "PMAP_UNSET",
175 reg.pm_prog, reg.pm_vers);
178 if (!svc_getargs(xprt, (xdrproc_t) xdr_pmap, (char *)®)) {
183 if (!check_access(xprt, op, ®, PMAPVERS)) {
184 svcerr_weakauth(xprt);
188 who = svc_getcaller(xprt);
191 * Can't use getpwnam here. We might end up calling ourselves
194 if (__rpc_get_local_uid(xprt, &uid) < 0)
195 rpcbreg.r_owner = "unknown";
197 rpcbreg.r_owner = "superuser";
199 /* r_owner will be strdup-ed later */
200 snprintf(uidbuf, sizeof uidbuf, "%d", uid);
201 rpcbreg.r_owner = uidbuf;
204 rpcbreg.r_prog = reg.pm_prog;
205 rpcbreg.r_vers = reg.pm_vers;
207 if (op == PMAPPROC_SET) {
210 snprintf(buf, sizeof buf, "0.0.0.0.%d.%d",
211 (int)((reg.pm_port >> 8) & 0xff),
212 (int)(reg.pm_port & 0xff));
213 rpcbreg.r_addr = buf;
214 if (reg.pm_prot == IPPROTO_UDP) {
215 rpcbreg.r_netid = udptrans;
216 } else if (reg.pm_prot == IPPROTO_TCP) {
217 rpcbreg.r_netid = tcptrans;
222 ans = map_set(&rpcbreg, rpcbreg.r_owner);
223 } else if (op == PMAPPROC_UNSET) {
226 rpcbreg.r_addr = NULL;
227 rpcbreg.r_netid = tcptrans;
228 ans1 = map_unset(&rpcbreg, rpcbreg.r_owner);
229 rpcbreg.r_netid = udptrans;
230 ans2 = map_unset(&rpcbreg, rpcbreg.r_owner);
236 if ((!svc_sendreply(xprt, (xdrproc_t) xdr_long, (caddr_t) &ans)) &&
238 fprintf(stderr, "portmap: svc_sendreply\n");
245 fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed");
247 if (op == PMAPPROC_SET)
248 rpcbs_set(RPCBVERS_2_STAT, ans);
250 rpcbs_unset(RPCBVERS_2_STAT, ans);
256 pmapproc_getport(struct svc_req *rqstp __unused, SVCXPRT *xprt)
261 struct pmaplist *fnd;
266 if (!svc_getargs(xprt, (xdrproc_t) xdr_pmap, (char *)®)) {
271 if (!check_access(xprt, PMAPPROC_GETPORT, ®, PMAPVERS)) {
272 svcerr_weakauth(xprt);
278 uaddr = taddr2uaddr(rpcbind_get_conf(xprt->xp_netid),
279 svc_getrpccaller(xprt));
280 fprintf(stderr, "PMAP_GETPORT req for (%lu, %lu, %s) from %s :",
281 reg.pm_prog, reg.pm_vers,
282 reg.pm_prot == IPPROTO_UDP ? "udp" : "tcp", uaddr);
286 fnd = find_service_pmap(reg.pm_prog, reg.pm_vers, reg.pm_prot);
288 char serveuaddr[32], *ua;
289 int h1, h2, h3, h4, p1, p2;
292 if (reg.pm_prot == IPPROTO_UDP) {
296 ua = tcp_uaddr; /* To get the len */
302 if (sscanf(ua, "%d.%d.%d.%d.%d.%d", &h1, &h2, &h3,
303 &h4, &p1, &p2) == 6) {
304 p1 = (fnd->pml_map.pm_port >> 8) & 0xff;
305 p2 = (fnd->pml_map.pm_port) & 0xff;
306 snprintf(serveuaddr, sizeof serveuaddr,
307 "%d.%d.%d.%d.%d.%d", h1, h2, h3, h4, p1, p2);
308 if (is_bound(netid, serveuaddr)) {
309 port = fnd->pml_map.pm_port;
310 } else { /* this service is dead; delete it */
311 delete_prog(reg.pm_prog);
317 if ((!svc_sendreply(xprt, (xdrproc_t) xdr_long, (caddr_t)&lport)) &&
319 fprintf(stderr, "portmap: svc_sendreply\n");
326 fprintf(stderr, "port = %d\n", port);
328 rpcbs_getaddr(RPCBVERS_2_STAT, reg.pm_prog, reg.pm_vers,
329 reg.pm_prot == IPPROTO_UDP ? udptrans : tcptrans,
330 port ? udptrans : "");
337 pmapproc_dump(struct svc_req *rqstp __unused, SVCXPRT *xprt)
339 if (!svc_getargs(xprt, (xdrproc_t)xdr_void, NULL)) {
344 if (!check_access(xprt, PMAPPROC_DUMP, NULL, PMAPVERS)) {
345 svcerr_weakauth(xprt);
349 if ((!svc_sendreply(xprt, (xdrproc_t) xdr_pmaplist_ptr,
350 (caddr_t)&list_pml)) && debugging) {
352 fprintf(stderr, "portmap: svc_sendreply\n");