1 /* $NetBSD: pmap_svc.c,v 1.5 2007/05/13 20:03:47 christos Exp $ */
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.
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.
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.
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.
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.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
35 /* #ident "@(#)pmap_svc.c 1.14 93/07/05 SMI" */
39 static char sccsid
[] = "@(#)pmap_svc.c 1.23 89/04/05 Copyr 1984 Sun Micro";
45 * The server procedure for the version 2 portmapper.
46 * All the portmapper related interface from the portmap side.
50 #include <sys/types.h>
51 #include <sys/socket.h>
54 #include <rpc/pmap_prot.h>
55 #include <rpc/rpcb_prot.h>
61 static struct pmaplist
*find_service_pmap(rpcprog_t
, rpcvers_t
, rpcprot_t
);
62 static bool_t
pmapproc_change(struct svc_req
*, SVCXPRT
*, u_long
);
63 static bool_t
pmapproc_getport(struct svc_req
*, SVCXPRT
*);
64 static bool_t
pmapproc_dump(struct svc_req
*, SVCXPRT
*);
67 * Called for all the version 2 inquiries.
70 pmap_service(struct svc_req
*rqstp
, SVCXPRT
*xprt
)
72 rpcbs_procinfo(RPCBVERS_2_STAT
, rqstp
->rq_proc
);
73 switch (rqstp
->rq_proc
) {
80 fprintf(stderr
, "PMAPPROC_NULL\n");
82 check_access(xprt
, rqstp
->rq_proc
, NULL
, PMAPVERS
);
83 if ((!svc_sendreply(xprt
, (xdrproc_t
) xdr_void
, NULL
)) &&
93 * Set a program, version to port mapping
95 pmapproc_change(rqstp
, xprt
, rqstp
->rq_proc
);
100 * Remove a program, version to port mapping.
102 pmapproc_change(rqstp
, xprt
, rqstp
->rq_proc
);
105 case PMAPPROC_GETPORT
:
107 * Lookup the mapping for a program, version and return its
110 pmapproc_getport(rqstp
, xprt
);
115 * Return the current set of mapped program, version
119 fprintf(stderr
, "PMAPPROC_DUMP\n");
121 pmapproc_dump(rqstp
, xprt
);
124 case PMAPPROC_CALLIT
:
126 * Calls a procedure on the local machine. If the requested
127 * procedure is not registered this procedure does not return
128 * error information!!
129 * This procedure is only supported on rpc/udp and calls via
130 * rpc/udp. It passes null authentication parameters.
132 rpcbproc_callit_com(rqstp
, xprt
, PMAPPROC_CALLIT
, PMAPVERS
);
142 * returns the item with the given program, version number. If that version
143 * number is not found, it returns the item with that program number, so that
144 * the port number is now returned to the caller. The caller when makes a
145 * call to this program, version number, the call will fail and it will
146 * return with PROGVERS_MISMATCH. The user can then determine the highest
147 * and the lowest version number for this program using clnt_geterr() and
148 * use those program version numbers.
150 static struct pmaplist
*
151 find_service_pmap(rpcprog_t prog
, rpcvers_t vers
, rpcprot_t prot
)
153 register struct pmaplist
*hit
= NULL
;
154 register struct pmaplist
*pml
;
156 for (pml
= list_pml
; pml
!= NULL
; pml
= pml
->pml_next
) {
157 if ((pml
->pml_map
.pm_prog
!= prog
) ||
158 (pml
->pml_map
.pm_prot
!= prot
))
161 if (pml
->pml_map
.pm_vers
== vers
)
168 pmapproc_change(struct svc_req
*rqstp
, SVCXPRT
*xprt
, unsigned long op
)
173 struct sockaddr_in
*who
;
179 fprintf(stderr
, "%s request for (%lu, %lu) : ",
180 op
== PMAPPROC_SET
? "PMAP_SET" : "PMAP_UNSET",
181 reg
.pm_prog
, reg
.pm_vers
);
184 if (!svc_getargs(xprt
, (xdrproc_t
) xdr_pmap
, (char *)®
)) {
189 if (!check_access(xprt
, op
, ®
, PMAPVERS
)) {
190 svcerr_weakauth(xprt
);
194 who
= svc_getcaller(xprt
);
195 sc
= __svc_getcallercreds(xprt
);
198 * Can't use getpwnam here. We might end up calling ourselves
202 rpcbreg
.r_owner
= __UNCONST(rpcbind_unknown
);
203 else if (sc
->sc_uid
== 0)
204 rpcbreg
.r_owner
= __UNCONST(rpcbind_superuser
);
206 /* r_owner will be strdup-ed later */
207 snprintf(uidbuf
, sizeof uidbuf
, "%d", sc
->sc_uid
);
208 rpcbreg
.r_owner
= uidbuf
;
211 rpcbreg
.r_prog
= reg
.pm_prog
;
212 rpcbreg
.r_vers
= reg
.pm_vers
;
214 if (op
== PMAPPROC_SET
) {
217 snprintf(buf
, sizeof(buf
), "0.0.0.0.%d.%d",
218 (int)((reg
.pm_port
>> 8) & 0xff),
219 (int)(reg
.pm_port
& 0xff));
220 rpcbreg
.r_addr
= buf
;
221 if (reg
.pm_prot
== IPPROTO_UDP
) {
222 rpcbreg
.r_netid
= __UNCONST(udptrans
);
223 } else if (reg
.pm_prot
== IPPROTO_TCP
) {
224 rpcbreg
.r_netid
= __UNCONST(tcptrans
);
229 ans
= map_set(&rpcbreg
, rpcbreg
.r_owner
);
230 } else if (op
== PMAPPROC_UNSET
) {
233 rpcbreg
.r_addr
= NULL
;
234 rpcbreg
.r_netid
= __UNCONST(tcptrans
);
235 ans1
= map_unset(&rpcbreg
, rpcbreg
.r_owner
);
236 rpcbreg
.r_netid
= __UNCONST(udptrans
);
237 ans2
= map_unset(&rpcbreg
, rpcbreg
.r_owner
);
243 if ((!svc_sendreply(xprt
, (xdrproc_t
) xdr_long
, (caddr_t
) &ans
)) &&
245 fprintf(stderr
, "portmap: svc_sendreply\n");
252 fprintf(stderr
, "%s\n", ans
== TRUE
? "succeeded" : "failed");
254 if (op
== PMAPPROC_SET
)
255 rpcbs_set(RPCBVERS_2_STAT
, ans
);
257 rpcbs_unset(RPCBVERS_2_STAT
, ans
);
263 pmapproc_getport(struct svc_req
*rqstp
, SVCXPRT
*xprt
)
268 struct pmaplist
*fnd
;
273 if (!svc_getargs(xprt
, (xdrproc_t
) xdr_pmap
, (char *)®
)) {
278 if (!check_access(xprt
, PMAPPROC_GETPORT
, ®
, PMAPVERS
)) {
279 svcerr_weakauth(xprt
);
285 uaddr
= taddr2uaddr(rpcbind_get_conf(xprt
->xp_netid
),
286 svc_getrpccaller(xprt
));
287 fprintf(stderr
, "PMAP_GETPORT req for (%lu, %lu, %s) from %s :",
288 reg
.pm_prog
, reg
.pm_vers
,
289 reg
.pm_prot
== IPPROTO_UDP
? "udp" : "tcp", uaddr
);
293 fnd
= find_service_pmap(reg
.pm_prog
, reg
.pm_vers
, reg
.pm_prot
);
296 int h1
, h2
, h3
, h4
, p1
, p2
;
297 const char *netid
, *ua
;
299 if (reg
.pm_prot
== IPPROTO_UDP
) {
303 ua
= tcp_uaddr
; /* To get the len */
309 if (sscanf(ua
, "%d.%d.%d.%d.%d.%d", &h1
, &h2
, &h3
,
310 &h4
, &p1
, &p2
) == 6) {
311 p1
= (fnd
->pml_map
.pm_port
>> 8) & 0xff;
312 p2
= (fnd
->pml_map
.pm_port
) & 0xff;
313 snprintf(serveuaddr
, sizeof(serveuaddr
),
314 "%d.%d.%d.%d.%d.%d", h1
, h2
, h3
, h4
, p1
, p2
);
315 if (is_bound(netid
, serveuaddr
)) {
316 port
= fnd
->pml_map
.pm_port
;
317 } else { /* this service is dead; delete it */
318 delete_prog(reg
.pm_prog
);
324 if ((!svc_sendreply(xprt
, (xdrproc_t
) xdr_long
, (caddr_t
)&lport
)) &&
326 (void) fprintf(stderr
, "portmap: svc_sendreply\n");
333 fprintf(stderr
, "port = %d\n", port
);
335 rpcbs_getaddr(RPCBVERS_2_STAT
, reg
.pm_prog
, reg
.pm_vers
,
336 reg
.pm_prot
== IPPROTO_UDP
? udptrans
: tcptrans
,
337 port
? udptrans
: "");
344 pmapproc_dump(struct svc_req
*rqstp
, SVCXPRT
*xprt
)
346 if (!svc_getargs(xprt
, (xdrproc_t
)xdr_void
, NULL
)) {
351 if (!check_access(xprt
, PMAPPROC_DUMP
, NULL
, PMAPVERS
)) {
352 svcerr_weakauth(xprt
);
356 if ((!svc_sendreply(xprt
, (xdrproc_t
) xdr_pmaplist_ptr
,
357 (caddr_t
)&list_pml
)) && debugging
) {
359 (void) fprintf(stderr
, "portmap: svc_sendreply\n");