Merge branches 'master' and 'suser_to_priv'
[dragonfly.git] / lib / libc / rpc / pmap_rmt.c
1 /*
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.
8  *
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.
12  *
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.
16  *
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.
20  *
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.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  *
29  * @(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro
30  * @(#)pmap_rmt.c       2.2 88/08/01 4.0 RPCSRC
31  * $NetBSD: pmap_rmt.c,v 1.29 2000/07/06 03:10:34 christos Exp $
32  * $FreeBSD: src/lib/libc/rpc/pmap_rmt.c,v 1.20 2004/10/16 06:11:35 obrien Exp $
33  * $DragonFly: src/lib/libc/rpc/pmap_rmt.c,v 1.5 2005/11/13 12:27:04 swildner Exp $
34  */
35
36 /*
37  * pmap_rmt.c
38  * Client interface to pmap rpc service.
39  * remote call and broadcast service
40  *
41  * Copyright (C) 1984, Sun Microsystems, Inc.
42  */
43
44 #include "namespace.h"
45 #include <sys/types.h>
46 #include <sys/ioctl.h>
47 #include <sys/poll.h>
48 #include <sys/socket.h>
49
50 #include <net/if.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
53
54 #include <assert.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <stdio.h>
58 #include <string.h>
59 #include <unistd.h>
60
61 #include <rpc/rpc.h>
62 #include <rpc/pmap_prot.h>
63 #include <rpc/pmap_clnt.h>
64 #include <rpc/pmap_rmt.h>
65 #include "un-namespace.h"
66
67 static const struct timeval timeout = { 3, 0 };
68
69 /*
70  * pmapper remote-call-service interface.
71  * This routine is used to call the pmapper remote call service
72  * which will look up a service program in the port maps, and then
73  * remotely call that routine with the given parameters.  This allows
74  * programs to do a lookup and call in one step.
75 */
76 enum clnt_stat
77 pmap_rmtcall(struct sockaddr_in *addr, u_long prog, u_long vers, u_long proc,
78              xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres, caddr_t resp,
79              struct timeval tout, u_long *port_ptr)
80 {
81         int sock = -1;
82         CLIENT *client;
83         struct rmtcallargs a;
84         struct rmtcallres r;
85         enum clnt_stat stat;
86
87         assert(addr != NULL);
88         assert(port_ptr != NULL);
89
90         addr->sin_port = htons(PMAPPORT);
91         client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
92         if (client != NULL) {
93                 a.prog = prog;
94                 a.vers = vers;
95                 a.proc = proc;
96                 a.args_ptr = argsp;
97                 a.xdr_args = xdrargs;
98                 r.port_ptr = port_ptr;
99                 r.results_ptr = resp;
100                 r.xdr_results = xdrres;
101                 stat = CLNT_CALL(client, (rpcproc_t)PMAPPROC_CALLIT,
102                     (xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres,
103                     &r, tout);
104                 CLNT_DESTROY(client);
105         } else {
106                 stat = RPC_FAILED;
107         }
108         addr->sin_port = 0;
109         return (stat);
110 }
111
112
113 /*
114  * XDR remote call arguments
115  * written for XDR_ENCODE direction only
116  */
117 bool_t
118 xdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap)
119 {
120         u_int lenposition, argposition, position;
121
122         assert(xdrs != NULL);
123         assert(cap != NULL);
124
125         if (xdr_u_long(xdrs, &(cap->prog)) &&
126             xdr_u_long(xdrs, &(cap->vers)) &&
127             xdr_u_long(xdrs, &(cap->proc))) {
128                 lenposition = XDR_GETPOS(xdrs);
129                 if (! xdr_u_long(xdrs, &(cap->arglen)))
130                     return (FALSE);
131                 argposition = XDR_GETPOS(xdrs);
132                 if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
133                     return (FALSE);
134                 position = XDR_GETPOS(xdrs);
135                 cap->arglen = (u_long)position - (u_long)argposition;
136                 XDR_SETPOS(xdrs, lenposition);
137                 if (! xdr_u_long(xdrs, &(cap->arglen)))
138                     return (FALSE);
139                 XDR_SETPOS(xdrs, position);
140                 return (TRUE);
141         }
142         return (FALSE);
143 }
144
145 /*
146  * XDR remote call results
147  * written for XDR_DECODE direction only
148  */
149 bool_t
150 xdr_rmtcallres(XDR *xdrs, struct rmtcallres *crp)
151 {
152         caddr_t port_ptr;
153
154         assert(xdrs != NULL);
155         assert(crp != NULL);
156
157         port_ptr = (caddr_t)(void *)crp->port_ptr;
158         if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
159             (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
160                 crp->port_ptr = (u_long *)(void *)port_ptr;
161                 return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
162         }
163         return (FALSE);
164 }