2 * Copyright (c) 2009, Sun Microsystems, Inc.
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.
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.
28 * @(#)pmap_svc.c 1.14 93/07/05 SMI; 1.23 89/04/05 Copyr 1984 Sun Micro
29 * $NetBSD: pmap_svc.c,v 1.2 2000/10/20 11:49:40 fvdl Exp $
30 * $FreeBSD: src/usr.sbin/rpcbind/pmap_svc.c,v 1.5 2007/11/07 10:53:39 kevlo Exp $
33 * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
38 * The server procedure for the version 2 portmaper.
39 * All the portmapper related interface from the portmap side.
43 #include <sys/types.h>
44 #include <sys/socket.h>
47 #include <rpc/pmap_prot.h>
48 #include <rpc/rpcb_prot.h>
54 static struct pmaplist
*find_service_pmap(rpcprog_t
, rpcvers_t
, rpcprot_t
);
55 static bool_t
pmapproc_change(struct svc_req
*, SVCXPRT
*, u_long
);
56 static bool_t
pmapproc_getport(struct svc_req
*, SVCXPRT
*);
57 static bool_t
pmapproc_dump(struct svc_req
*, SVCXPRT
*);
60 * Called for all the version 2 inquiries.
63 pmap_service(struct svc_req
*rqstp
, SVCXPRT
*xprt
)
65 rpcbs_procinfo(RPCBVERS_2_STAT
, rqstp
->rq_proc
);
66 switch (rqstp
->rq_proc
) {
73 fprintf(stderr
, "PMAPPROC_NULL\n");
75 check_access(xprt
, rqstp
->rq_proc
, NULL
, PMAPVERS
);
76 if ((!svc_sendreply(xprt
, (xdrproc_t
) xdr_void
, NULL
)) &&
86 * Set a program, version to port mapping
88 pmapproc_change(rqstp
, xprt
, rqstp
->rq_proc
);
93 * Remove a program, version to port mapping.
95 pmapproc_change(rqstp
, xprt
, rqstp
->rq_proc
);
98 case PMAPPROC_GETPORT
:
100 * Lookup the mapping for a program, version and return its
103 pmapproc_getport(rqstp
, xprt
);
108 * Return the current set of mapped program, version
112 fprintf(stderr
, "PMAPPROC_DUMP\n");
114 pmapproc_dump(rqstp
, xprt
);
117 case PMAPPROC_CALLIT
:
119 * Calls a procedure on the local machine. If the requested
120 * procedure is not registered this procedure does not return
121 * error information!!
122 * This procedure is only supported on rpc/udp and calls via
123 * rpc/udp. It passes null authentication parameters.
125 rpcbproc_callit_com(rqstp
, xprt
, PMAPPROC_CALLIT
, PMAPVERS
);
135 * returns the item with the given program, version number. If that version
136 * number is not found, it returns the item with that program number, so that
137 * the port number is now returned to the caller. The caller when makes a
138 * call to this program, version number, the call will fail and it will
139 * return with PROGVERS_MISMATCH. The user can then determine the highest
140 * and the lowest version number for this program using clnt_geterr() and
141 * use those program version numbers.
143 static struct pmaplist
*
144 find_service_pmap(rpcprog_t prog
, rpcvers_t vers
, rpcprot_t prot
)
146 struct pmaplist
*hit
= NULL
;
147 struct pmaplist
*pml
;
149 for (pml
= list_pml
; pml
!= NULL
; pml
= pml
->pml_next
) {
150 if ((pml
->pml_map
.pm_prog
!= prog
) ||
151 (pml
->pml_map
.pm_prot
!= prot
))
154 if (pml
->pml_map
.pm_vers
== vers
)
161 pmapproc_change(struct svc_req
*rqstp __unused
, SVCXPRT
*xprt
, unsigned long op
)
166 struct sockaddr_in
*who
;
172 fprintf(stderr
, "%s request for (%lu, %lu) : ",
173 op
== PMAPPROC_SET
? "PMAP_SET" : "PMAP_UNSET",
174 reg
.pm_prog
, reg
.pm_vers
);
177 if (!svc_getargs(xprt
, (xdrproc_t
) xdr_pmap
, (char *)®
)) {
182 if (!check_access(xprt
, op
, ®
, PMAPVERS
)) {
183 svcerr_weakauth(xprt
);
187 who
= svc_getcaller(xprt
);
190 * Can't use getpwnam here. We might end up calling ourselves
193 if (__rpc_get_local_uid(xprt
, &uid
) < 0)
194 rpcbreg
.r_owner
= "unknown";
196 rpcbreg
.r_owner
= "superuser";
198 /* r_owner will be strdup-ed later */
199 snprintf(uidbuf
, sizeof uidbuf
, "%d", uid
);
200 rpcbreg
.r_owner
= uidbuf
;
203 rpcbreg
.r_prog
= reg
.pm_prog
;
204 rpcbreg
.r_vers
= reg
.pm_vers
;
206 if (op
== PMAPPROC_SET
) {
209 snprintf(buf
, sizeof buf
, "0.0.0.0.%d.%d",
210 (int)((reg
.pm_port
>> 8) & 0xff),
211 (int)(reg
.pm_port
& 0xff));
212 rpcbreg
.r_addr
= buf
;
213 if (reg
.pm_prot
== IPPROTO_UDP
) {
214 rpcbreg
.r_netid
= udptrans
;
215 } else if (reg
.pm_prot
== IPPROTO_TCP
) {
216 rpcbreg
.r_netid
= tcptrans
;
221 ans
= map_set(&rpcbreg
, rpcbreg
.r_owner
);
222 } else if (op
== PMAPPROC_UNSET
) {
225 rpcbreg
.r_addr
= NULL
;
226 rpcbreg
.r_netid
= tcptrans
;
227 ans1
= map_unset(&rpcbreg
, rpcbreg
.r_owner
);
228 rpcbreg
.r_netid
= udptrans
;
229 ans2
= map_unset(&rpcbreg
, rpcbreg
.r_owner
);
235 if ((!svc_sendreply(xprt
, (xdrproc_t
) xdr_long
, (caddr_t
) &ans
)) &&
237 fprintf(stderr
, "portmap: svc_sendreply\n");
244 fprintf(stderr
, "%s\n", ans
== TRUE
? "succeeded" : "failed");
246 if (op
== PMAPPROC_SET
)
247 rpcbs_set(RPCBVERS_2_STAT
, ans
);
249 rpcbs_unset(RPCBVERS_2_STAT
, ans
);
255 pmapproc_getport(struct svc_req
*rqstp __unused
, SVCXPRT
*xprt
)
260 struct pmaplist
*fnd
;
265 if (!svc_getargs(xprt
, (xdrproc_t
) xdr_pmap
, (char *)®
)) {
270 if (!check_access(xprt
, PMAPPROC_GETPORT
, ®
, PMAPVERS
)) {
271 svcerr_weakauth(xprt
);
277 uaddr
= taddr2uaddr(rpcbind_get_conf(xprt
->xp_netid
),
278 svc_getrpccaller(xprt
));
279 fprintf(stderr
, "PMAP_GETPORT req for (%lu, %lu, %s) from %s :",
280 reg
.pm_prog
, reg
.pm_vers
,
281 reg
.pm_prot
== IPPROTO_UDP
? "udp" : "tcp", uaddr
);
285 fnd
= find_service_pmap(reg
.pm_prog
, reg
.pm_vers
, reg
.pm_prot
);
287 char serveuaddr
[32], *ua
;
288 int h1
, h2
, h3
, h4
, p1
, p2
;
291 if (reg
.pm_prot
== IPPROTO_UDP
) {
295 ua
= tcp_uaddr
; /* To get the len */
301 if (sscanf(ua
, "%d.%d.%d.%d.%d.%d", &h1
, &h2
, &h3
,
302 &h4
, &p1
, &p2
) == 6) {
303 p1
= (fnd
->pml_map
.pm_port
>> 8) & 0xff;
304 p2
= (fnd
->pml_map
.pm_port
) & 0xff;
305 snprintf(serveuaddr
, sizeof serveuaddr
,
306 "%d.%d.%d.%d.%d.%d", h1
, h2
, h3
, h4
, p1
, p2
);
307 if (is_bound(netid
, serveuaddr
)) {
308 port
= fnd
->pml_map
.pm_port
;
309 } else { /* this service is dead; delete it */
310 delete_prog(reg
.pm_prog
);
316 if ((!svc_sendreply(xprt
, (xdrproc_t
) xdr_long
, (caddr_t
)&lport
)) &&
318 fprintf(stderr
, "portmap: svc_sendreply\n");
325 fprintf(stderr
, "port = %d\n", port
);
327 rpcbs_getaddr(RPCBVERS_2_STAT
, reg
.pm_prog
, reg
.pm_vers
,
328 reg
.pm_prot
== IPPROTO_UDP
? udptrans
: tcptrans
,
329 port
? udptrans
: "");
336 pmapproc_dump(struct svc_req
*rqstp __unused
, SVCXPRT
*xprt
)
338 if (!svc_getargs(xprt
, (xdrproc_t
)xdr_void
, NULL
)) {
343 if (!check_access(xprt
, PMAPPROC_DUMP
, NULL
, PMAPVERS
)) {
344 svcerr_weakauth(xprt
);
348 if ((!svc_sendreply(xprt
, (xdrproc_t
) xdr_pmaplist_ptr
,
349 (caddr_t
)&list_pml
)) && debugging
) {
351 fprintf(stderr
, "portmap: svc_sendreply\n");