source: rtems/c/src/librpc/src/rpc/rtems_portmapper.c @ 8fe36e1

Last change on this file since 8fe36e1 was 8fe36e1, checked in by Joel Sherrill <joel.sherrill@…>, on 05/24/00 at 14:21:27

Removed some warnings.

  • Property mode set to 100644
File size: 11.7 KB
Line 
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
30#include <rpc/rpc.h>
31#include <rpc/pmap_prot.h>
32#include <stdio.h>
33#include <netdb.h>
34#include <sys/socket.h>
35#include <sys/ioctl.h>
36#include <sys/wait.h>
37#include <sys/signal.h>
38
39int reg_service();
40static struct pmaplist *pmaplist;
41static int debugging = 0;
42
43#include <rtems.h>
44#define fork()  (-1)
45
46
47static rtems_task rtems_portmapper (rtems_task_argument unused)
48{
49        SVCXPRT *xprt;
50        int sock, pid, t;
51        struct sockaddr_in addr;
52        int len = sizeof(struct sockaddr_in);
53        register struct pmaplist *pml;
54
55        rtems_rpc_task_init ();
56        if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
57                perror("portmap cannot create socket");
58                rtems_task_delete (RTEMS_SELF);
59        }
60
61        addr.sin_addr.s_addr = 0;
62        addr.sin_family = AF_INET;
63        addr.sin_port = htons(PMAPPORT);
64        if (bind(sock, (struct sockaddr *)&addr, len) != 0) {
65                perror("portmap cannot bind");
66                close (sock);
67                rtems_task_delete (RTEMS_SELF);
68        }
69
70        if ((xprt = svcudp_create(sock)) == (SVCXPRT *)NULL) {
71                fprintf(stderr, "couldn't do udp_create\n");
72                close (sock);
73                rtems_task_delete (RTEMS_SELF);
74        }
75        /* make an entry for ourself */
76        pml = (struct pmaplist *)malloc((u_int)sizeof(struct pmaplist));
77        pml->pml_next = 0;
78        pml->pml_map.pm_prog = PMAPPROG;
79        pml->pml_map.pm_vers = PMAPVERS;
80        pml->pml_map.pm_prot = IPPROTO_UDP;
81        pml->pml_map.pm_port = PMAPPORT;
82        pmaplist = pml;
83
84        if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
85                perror("portmap cannot create socket");
86                close (sock);
87                rtems_task_delete (RTEMS_SELF);
88        }
89        if (bind(sock, (struct sockaddr *)&addr, len) != 0) {
90                perror("portmap cannot bind");
91                close (sock);
92                rtems_task_delete (RTEMS_SELF);
93        }
94        if ((xprt = svctcp_create(sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE))
95            == (SVCXPRT *)NULL) {
96                fprintf(stderr, "couldn't do tcp_create\n");
97                close (sock);
98                rtems_task_delete (RTEMS_SELF);
99        }
100        /* make an entry for ourself */
101        pml = (struct pmaplist *)malloc((u_int)sizeof(struct pmaplist));
102        pml->pml_map.pm_prog = PMAPPROG;
103        pml->pml_map.pm_vers = PMAPVERS;
104        pml->pml_map.pm_prot = IPPROTO_TCP;
105        pml->pml_map.pm_port = PMAPPORT;
106        pml->pml_next = pmaplist;
107        pmaplist = pml;
108
109        (void)svc_register(xprt, PMAPPROG, PMAPVERS, reg_service, FALSE);
110
111        svc_run();
112        fprintf(stderr, "run_svc returned unexpectedly\n");
113        close (sock);
114        rtems_task_delete (RTEMS_SELF);
115}
116
117static struct pmaplist *
118find_service(prog, vers, prot)
119u_long prog;
120u_long vers;
121{
122register struct pmaplist *hit = NULL;
123register struct pmaplist *pml;
124
125for (pml = pmaplist; pml != NULL; pml = pml->pml_next) {
126        if ((pml->pml_map.pm_prog != prog) ||
127                (pml->pml_map.pm_prot != prot))
128                continue;
129        hit = pml;
130        if (pml->pml_map.pm_vers == vers)
131            break;
132}
133        return (hit);
134}
135
136/*
137 * 1 OK, 0 not
138 */
139static reg_service(rqstp, xprt)
140        struct svc_req *rqstp;
141        SVCXPRT *xprt;
142{
143        struct pmap reg;
144        struct pmaplist *pml, *prevpml, *fnd;
145        int ans, port;
146        caddr_t t;
147       
148#ifdef DEBUG
149        fprintf(stderr, "server: about do a switch\n");
150#endif
151        switch (rqstp->rq_proc) {
152
153        case PMAPPROC_NULL:
154                /*
155                 * Null proc call
156                 */
157                if ((!svc_sendreply(xprt, xdr_void, NULL)) && debugging) {
158                        abort();
159                }
160                break;
161
162        case PMAPPROC_SET:
163                /*
164                 * Set a program,version to port mapping
165                 */
166                if (!svc_getargs(xprt, xdr_pmap, (caddr_t) &reg))
167                        svcerr_decode(xprt);
168                else {
169                        /*
170                         * check to see if already used
171                         * find_service returns a hit even if
172                         * the versions don't match, so check for it
173                         */
174                        fnd = find_service(reg.pm_prog, reg.pm_vers, reg.pm_prot);
175                        if (fnd && fnd->pml_map.pm_vers == reg.pm_vers) {
176                                if (fnd->pml_map.pm_port == reg.pm_port) {
177                                        ans = 1;
178                                        goto done;
179                                }
180                                else {
181                                        ans = 0;
182                                        goto done;
183                                }
184                        } else {
185                                /*
186                                 * add to END of list
187                                 */
188                                pml = (struct pmaplist *)
189                                    malloc((u_int)sizeof(struct pmaplist));
190                                pml->pml_map = reg;
191                                pml->pml_next = 0;
192                                if (pmaplist == 0) {
193                                        pmaplist = pml;
194                                } else {
195                                        for (fnd= pmaplist; fnd->pml_next != 0;
196                                            fnd = fnd->pml_next);
197                                        fnd->pml_next = pml;
198                                }
199                                ans = 1;
200                        }
201                done:
202                        if ((!svc_sendreply(xprt, xdr_long, (caddr_t)&ans)) &&
203                            debugging) {
204                                fprintf(stderr, "svc_sendreply\n");
205                                abort();
206                        }
207                }
208                break;
209
210        case PMAPPROC_UNSET:
211                /*
212                 * Remove a program,version to port mapping.
213                 */
214                if (!svc_getargs(xprt, xdr_pmap, (caddr_t) &reg))
215                        svcerr_decode(xprt);
216                else {
217                        ans = 0;
218                        for (prevpml = NULL, pml = pmaplist; pml != NULL; ) {
219                                if ((pml->pml_map.pm_prog != reg.pm_prog) ||
220                                        (pml->pml_map.pm_vers != reg.pm_vers)) {
221                                        /* both pml & prevpml move forwards */
222                                        prevpml = pml;
223                                        pml = pml->pml_next;
224                                        continue;
225                                }
226                                /* found it; pml moves forward, prevpml stays */
227                                ans = 1;
228                                t = (caddr_t)pml;
229                                pml = pml->pml_next;
230                                if (prevpml == NULL)
231                                        pmaplist = pml;
232                                else
233                                        prevpml->pml_next = pml;
234                                free(t);
235                        }
236                        if ((!svc_sendreply(xprt, xdr_long, (caddr_t)&ans)) &&
237                            debugging) {
238                                fprintf(stderr, "svc_sendreply\n");
239                                abort();
240                        }
241                }
242                break;
243
244        case PMAPPROC_GETPORT:
245                /*
246                 * Lookup the mapping for a program,version and return its port
247                 */
248                if (!svc_getargs(xprt, xdr_pmap, (caddr_t) &reg))
249                        svcerr_decode(xprt);
250                else {
251                        fnd = find_service(reg.pm_prog, reg.pm_vers, reg.pm_prot);
252                        if (fnd)
253                                port = fnd->pml_map.pm_port;
254                        else
255                                port = 0;
256                        if ((!svc_sendreply(xprt, xdr_long, (caddr_t)&port)) &&
257                            debugging) {
258                                fprintf(stderr, "svc_sendreply\n");
259                                abort();
260                        }
261                }
262                break;
263
264        case PMAPPROC_DUMP:
265                /*
266                 * Return the current set of mapped program,version
267                 */
268                if (!svc_getargs(xprt, xdr_void, NULL))
269                        svcerr_decode(xprt);
270                else {
271                        if ((!svc_sendreply(xprt, xdr_pmaplist,
272                            (caddr_t)&pmaplist)) && debugging) {
273                                fprintf(stderr, "svc_sendreply\n");
274                                abort();
275                        }
276                }
277                break;
278
279        case PMAPPROC_CALLIT:
280                /*
281                 * Calls a procedure on the local machine.  If the requested
282                 * procedure is not registered this procedure does not return
283                 * error information!!
284                 * This procedure is only supported on rpc/udp and calls via
285                 * rpc/udp.  It passes null authentication parameters.
286                 */
287                callit(rqstp, xprt);
288                break;
289
290        default:
291                svcerr_noproc(xprt);
292                break;
293        }
294}
295
296
297/*
298 * Stuff for the rmtcall service
299 */
300#define ARGSIZE 9000
301
302/* typedef */ struct encap_parms {
303        u_long arglen;
304        char *args;
305};
306
307static bool_t
308xdr_encap_parms(xdrs, epp)
309        XDR *xdrs;
310        struct encap_parms *epp;
311{
312
313        return (xdr_bytes(xdrs, &(epp->args), &(epp->arglen), ARGSIZE));
314}
315
316typedef struct rmtcallargs {
317        u_long  rmt_prog;
318        u_long  rmt_vers;
319        u_long  rmt_port;
320        u_long  rmt_proc;
321        struct encap_parms rmt_args;
322};
323
324static bool_t
325xdr_rmtcall_args(xdrs, cap)
326        register XDR *xdrs;
327        register struct rmtcallargs *cap;
328{
329
330        /* does not get a port number */
331        if (xdr_u_long(xdrs, &(cap->rmt_prog)) &&
332            xdr_u_long(xdrs, &(cap->rmt_vers)) &&
333            xdr_u_long(xdrs, &(cap->rmt_proc))) {
334                return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
335        }
336        return (FALSE);
337}
338
339static bool_t
340xdr_rmtcall_result(xdrs, cap)
341        register XDR *xdrs;
342        register struct rmtcallargs *cap;
343{
344        if (xdr_u_long(xdrs, &(cap->rmt_port)))
345                return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
346        return (FALSE);
347}
348
349/*
350 * only worries about the struct encap_parms part of struct rmtcallargs.
351 * The arglen must already be set!!
352 */
353static bool_t
354xdr_opaque_parms(xdrs, cap)
355        XDR *xdrs;
356        struct rmtcallargs *cap;
357{
358
359        return (xdr_opaque(xdrs, cap->rmt_args.args, cap->rmt_args.arglen));
360}
361
362/*
363 * This routine finds and sets the length of incoming opaque paraters
364 * and then calls xdr_opaque_parms.
365 */
366static bool_t
367xdr_len_opaque_parms(xdrs, cap)
368        register XDR *xdrs;
369        struct rmtcallargs *cap;
370{
371        register u_int beginpos, lowpos, highpos, currpos, pos;
372
373        beginpos = lowpos = pos = xdr_getpos(xdrs);
374        highpos = lowpos + ARGSIZE;
375        while ((int)(highpos - lowpos) >= 0) {
376                currpos = (lowpos + highpos) / 2;
377                if (xdr_setpos(xdrs, currpos)) {
378                        pos = currpos;
379                        lowpos = currpos + 1;
380                } else {
381                        highpos = currpos - 1;
382                }
383        }
384        xdr_setpos(xdrs, beginpos);
385        cap->rmt_args.arglen = pos - beginpos;
386        return (xdr_opaque_parms(xdrs, cap));
387}
388
389/*
390 * Call a remote procedure service
391 * This procedure is very quiet when things go wrong.
392 * The proc is written to support broadcast rpc.  In the broadcast case,
393 * a machine should shut-up instead of complain, less the requestor be
394 * overrun with complaints at the expense of not hearing a valid reply ...
395 *
396 * This now forks so that the program & process that it calls can call
397 * back to the portmapper.
398 */
399static
400callit(rqstp, xprt)
401        struct svc_req *rqstp;
402        SVCXPRT *xprt;
403{
404        struct rmtcallargs a;
405        struct pmaplist *pml;
406        u_short port;
407        struct sockaddr_in me;
408        int pid, socket = -1;
409        CLIENT *client;
410        struct authunix_parms *au = (struct authunix_parms *)rqstp->rq_clntcred;
411        struct timeval timeout;
412        char buf[ARGSIZE];
413
414        timeout.tv_sec = 5;
415        timeout.tv_usec = 0;
416        a.rmt_args.args = buf;
417        if (!svc_getargs(xprt, xdr_rmtcall_args, (caddr_t) &a))
418            return;
419        if ((pml = find_service(a.rmt_prog, a.rmt_vers, IPPROTO_UDP)) == NULL)
420            return;
421        /*
422         * fork a child to do the work.  Parent immediately returns.
423         * Child exits upon completion.
424         */
425        if ((pid = fork()) != 0) {
426                if (debugging && (pid < 0)) {
427                        fprintf(stderr, "portmap CALLIT: cannot fork.\n");
428                }
429                return;
430        }
431        port = pml->pml_map.pm_port;
432        get_myaddress(&me);
433        me.sin_port = htons(port);
434        client = clntudp_create(&me, a.rmt_prog, a.rmt_vers, timeout, &socket);
435        if (client != (CLIENT *)NULL) {
436                if (rqstp->rq_cred.oa_flavor == AUTH_UNIX) {
437                        client->cl_auth = authunix_create(au->aup_machname,
438                           au->aup_uid, au->aup_gid, au->aup_len, au->aup_gids);
439                }
440                a.rmt_port = (u_long)port;
441                if (clnt_call(client, a.rmt_proc, xdr_opaque_parms, &a,
442                    xdr_len_opaque_parms, &a, timeout) == RPC_SUCCESS) {
443                        svc_sendreply(xprt, xdr_rmtcall_result, &a);
444                }
445                AUTH_DESTROY(client->cl_auth);
446                clnt_destroy(client);
447        }
448        (void)close(socket);
449        exit(0);
450}
451
452/*
453 * Start the RPC portmapper
454 */
455int rtems_rpc_start_portmapper (int priority)
456{
457        rtems_mode mode;
458        rtems_status_code sc;
459        rtems_id tid;
460        static int started;
461
462        rtems_task_mode (RTEMS_NO_PREEMPT, RTEMS_PREEMPT_MASK, &mode);
463        if (started) {
464                rtems_task_mode (mode, RTEMS_PREEMPT_MASK, &mode);
465                return RTEMS_SUCCESSFUL;
466        }
467        sc = rtems_task_create (rtems_build_name('P', 'M', 'A', 'P'),
468                priority,
469                8000,
470                RTEMS_PREEMPT|RTEMS_NO_TIMESLICE|RTEMS_NO_ASR|RTEMS_INTERRUPT_LEVEL(0),
471                RTEMS_NO_FLOATING_POINT|RTEMS_LOCAL,
472                &tid);
473        if (sc != RTEMS_SUCCESSFUL) {
474                rtems_task_mode (mode, RTEMS_PREEMPT_MASK, &mode);
475                return sc;
476        }
477        sc = rtems_task_start (tid, rtems_portmapper, 0);
478        if (sc != RTEMS_SUCCESSFUL) {
479                rtems_task_mode (mode, RTEMS_PREEMPT_MASK, &mode);
480                return sc;
481        }
482        started = 1;
483        rtems_task_mode (mode, RTEMS_PREEMPT_MASK, &mode);
484        return RTEMS_SUCCESSFUL;
485}
Note: See TracBrowser for help on using the repository browser.