home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-B.05 / NETKIT-B / NetKit-B-0.05 / portmap / portmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-23  |  13.7 KB  |  545 lines

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. /*static char sccsid[] = "from: @(#)portmap.c    5.4 (Berkeley) 4/19/91";*/
  42. static char rcsid[] = "$Id: portmap.c,v 1.1 1994/05/23 09:07:19 rzsfl Exp rzsfl $";
  43. #endif /* not lint */
  44.  
  45. /*
  46. @(#)portmap.c    2.3 88/08/11 4.0 RPCSRC
  47. static char sccsid[] = "@(#)portmap.c 1.32 87/08/06 Copyr 1984 Sun Micro";
  48. */
  49.  
  50. /*
  51.  * portmap.c, Implements the program,version to port number mapping for
  52.  * rpc.
  53.  */
  54.  
  55. /*
  56.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  57.  * unrestricted use provided that this legend is included on all tape
  58.  * media and as a part of the software program in whole or part.  Users
  59.  * may copy or modify Sun RPC without charge, but are not authorized
  60.  * to license or distribute it to anyone else except as part of a product or
  61.  * program developed by the user.
  62.  * 
  63.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  64.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  65.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  66.  * 
  67.  * Sun RPC is provided with no support and without any obligation on the
  68.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  69.  * modification or enhancement.
  70.  * 
  71.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  72.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  73.  * OR ANY PART THEREOF.
  74.  * 
  75.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  76.  * or profits or other special, indirect and consequential damages, even if
  77.  * Sun has been advised of the possibility of such damages.
  78.  * 
  79.  * Sun Microsystems, Inc.
  80.  * 2550 Garcia Avenue
  81.  * Mountain View, California  94043
  82.  */
  83.  
  84. #include <rpc/rpc.h>
  85. #include <rpc/pmap_clnt.h>
  86. #include <rpc/pmap_prot.h>
  87. #include <stdio.h>
  88. #include <stdlib.h>
  89. #include <string.h>
  90. #include <syslog.h>
  91. #include <unistd.h>
  92. #include <netdb.h>
  93. #include <sys/socket.h>
  94. #include <sys/ioctl.h>
  95. #include <sys/wait.h>
  96. #include <sys/signal.h>
  97. #include <sys/resource.h>
  98.  
  99. void reg_service();
  100. void reap();
  101. static void callit();
  102. struct pmaplist *pmaplist;
  103. int debugging = 0;
  104. extern int errno;
  105.  
  106. main(argc, argv)
  107.     int argc;
  108.     char **argv;
  109. {
  110.     SVCXPRT *xprt;
  111.     int sock, c;
  112.     struct sockaddr_in addr;
  113.     int len = sizeof(struct sockaddr_in);
  114.     register struct pmaplist *pml;
  115.  
  116.     while ((c = getopt(argc, argv, "d")) != EOF) {
  117.         switch (c) {
  118.  
  119.         case 'd':
  120.             debugging = 1;
  121.             break;
  122.  
  123.         default:
  124.             (void) fprintf(stderr, "usage: %s [-d]\n", argv[0]);
  125.             exit(1);
  126.         }
  127.     }
  128.  
  129.     if (!debugging && daemon(0, 0)) {
  130.         (void) fprintf(stderr, "portmap: fork: %s", strerror(errno));
  131.         exit(1);
  132.     }
  133.  
  134.     openlog("portmap", debugging ? LOG_PID | LOG_PERROR : LOG_PID,
  135.         LOG_DAEMON);
  136.  
  137.     if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  138.         syslog(LOG_ERR, "cannot create udp socket: %m");
  139.         exit(1);
  140.     }
  141.  
  142.     bzero((char *)&addr, sizeof addr);
  143.     addr.sin_addr.s_addr = 0;
  144.     addr.sin_family = AF_INET;
  145.     addr.sin_port = htons(PMAPPORT);
  146.     if (bind(sock, (struct sockaddr *)&addr, len) != 0) {
  147.         syslog(LOG_ERR, "cannot bind udp: %m");
  148.         exit(1);
  149.     }
  150.  
  151.     if ((xprt = svcudp_create(sock)) == (SVCXPRT *)NULL) {
  152.         syslog(LOG_ERR, "couldn't do udp_create");
  153.         exit(1);
  154.     }
  155.     /* make an entry for ourself */
  156.     pml = (struct pmaplist *)malloc((u_int)sizeof(struct pmaplist));
  157.     pml->pml_next = 0;
  158.     pml->pml_map.pm_prog = PMAPPROG;
  159.     pml->pml_map.pm_vers = PMAPVERS;
  160.     pml->pml_map.pm_prot = IPPROTO_UDP;
  161.     pml->pml_map.pm_port = PMAPPORT;
  162.     pmaplist = pml;
  163.  
  164.     if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
  165.         syslog(LOG_ERR, "cannot create tcp socket: %m");
  166.         exit(1);
  167.     }
  168.     if (bind(sock, (struct sockaddr *)&addr, len) != 0) {
  169.         syslog(LOG_ERR, "cannot bind udp: %m");
  170.         exit(1);
  171.     }
  172.     if ((xprt = svctcp_create(sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE))
  173.         == (SVCXPRT *)NULL) {
  174.         syslog(LOG_ERR, "couldn't do tcp_create");
  175.         exit(1);
  176.     }
  177.     /* make an entry for ourself */
  178.     pml = (struct pmaplist *)malloc((u_int)sizeof(struct pmaplist));
  179.     pml->pml_map.pm_prog = PMAPPROG;
  180.     pml->pml_map.pm_vers = PMAPVERS;
  181.     pml->pml_map.pm_prot = IPPROTO_TCP;
  182.     pml->pml_map.pm_port = PMAPPORT;
  183.     pml->pml_next = pmaplist;
  184.     pmaplist = pml;
  185.  
  186.     (void)svc_register(xprt, PMAPPROG, PMAPVERS, reg_service, FALSE);
  187.  
  188.     (void)signal(SIGCHLD, reap);
  189.     svc_run();
  190.     syslog(LOG_ERR, "run_svc returned unexpectedly");
  191.     abort();
  192. }
  193.  
  194. #ifndef lint
  195. /* need to override perror calls in rpc library */
  196. void
  197. perror(what)
  198.     const char *what;
  199. {
  200.  
  201.     syslog(LOG_ERR, "%s: %m", what);
  202. }
  203. #endif
  204.  
  205. static struct pmaplist *
  206. find_service(prog, vers, prot)
  207.     u_long prog, vers, prot;
  208. {
  209.     register struct pmaplist *hit = NULL;
  210.     register struct pmaplist *pml;
  211.  
  212.     for (pml = pmaplist; pml != NULL; pml = pml->pml_next) {
  213.         if ((pml->pml_map.pm_prog != prog) ||
  214.             (pml->pml_map.pm_prot != prot))
  215.             continue;
  216.         hit = pml;
  217.         if (pml->pml_map.pm_vers == vers)
  218.             break;
  219.     }
  220.     return (hit);
  221. }
  222.  
  223. /* 
  224.  * 1 OK, 0 not
  225.  */
  226. void
  227. reg_service(rqstp, xprt)
  228.     struct svc_req *rqstp;
  229.     SVCXPRT *xprt;
  230. {
  231.     struct pmap reg;
  232.     struct pmaplist *pml, *prevpml, *fnd;
  233.     int ans, port;
  234.     caddr_t t;
  235.     
  236.     if (debugging)
  237.         (void) fprintf(stderr, "server: about do a switch\n");
  238.     switch (rqstp->rq_proc) {
  239.  
  240.     case PMAPPROC_NULL:
  241.         /*
  242.          * Null proc call
  243.          */
  244.         if (!svc_sendreply(xprt, xdr_void, (caddr_t)0) && debugging) {
  245.             abort();
  246.         }
  247.         break;
  248.  
  249.     case PMAPPROC_SET:
  250.         /*
  251.          * Set a program,version to port mapping
  252.          */
  253.         if (!svc_getargs(xprt, xdr_pmap, ®))
  254.             svcerr_decode(xprt);
  255.         else {
  256.             /*
  257.              * check to see if already used
  258.              * find_service returns a hit even if
  259.              * the versions don't match, so check for it
  260.              */
  261.             fnd = find_service(reg.pm_prog, reg.pm_vers, reg.pm_prot);
  262.             if (fnd && fnd->pml_map.pm_vers == reg.pm_vers) {
  263.                 if (fnd->pml_map.pm_port == reg.pm_port) {
  264.                     ans = 1;
  265.                     goto done;
  266.                 }
  267.                 else {
  268.                     ans = 0;
  269.                     goto done;
  270.                 }
  271.             } else {
  272.                 /* 
  273.                  * add to END of list
  274.                  */
  275.                 pml = (struct pmaplist *)
  276.                     malloc((u_int)sizeof(struct pmaplist));
  277.                 pml->pml_map = reg;
  278.                 pml->pml_next = 0;
  279.                 if (pmaplist == 0) {
  280.                     pmaplist = pml;
  281.                 } else {
  282.                     for (fnd= pmaplist; fnd->pml_next != 0;
  283.                         fnd = fnd->pml_next);
  284.                     fnd->pml_next = pml;
  285.                 }
  286.                 ans = 1;
  287.             }
  288.         done:
  289.             if ((!svc_sendreply(xprt, xdr_long, (caddr_t)&ans)) &&
  290.                 debugging) {
  291.                 (void) fprintf(stderr, "svc_sendreply\n");
  292.                 abort();
  293.             }
  294.         }
  295.         break;
  296.  
  297.     case PMAPPROC_UNSET:
  298.         /*
  299.          * Remove a program,version to port mapping.
  300.          */
  301.         if (!svc_getargs(xprt, xdr_pmap, ®))
  302.             svcerr_decode(xprt);
  303.         else {
  304.             ans = 0;
  305.             for (prevpml = NULL, pml = pmaplist; pml != NULL; ) {
  306.                 if ((pml->pml_map.pm_prog != reg.pm_prog) ||
  307.                     (pml->pml_map.pm_vers != reg.pm_vers)) {
  308.                     /* both pml & prevpml move forwards */
  309.                     prevpml = pml;
  310.                     pml = pml->pml_next;
  311.                     continue;
  312.                 }
  313.                 /* found it; pml moves forward, prevpml stays */
  314.                 ans = 1;
  315.                 t = (caddr_t)pml;
  316.                 pml = pml->pml_next;
  317.                 if (prevpml == NULL)
  318.                     pmaplist = pml;
  319.                 else
  320.                     prevpml->pml_next = pml;
  321.                 free(t);
  322.             }
  323.             if ((!svc_sendreply(xprt, xdr_long, (caddr_t)&ans)) &&
  324.                 debugging) {
  325.                 (void) fprintf(stderr, "svc_sendreply\n");
  326.                 abort();
  327.             }
  328.         }
  329.         break;
  330.  
  331.     case PMAPPROC_GETPORT:
  332.         /*
  333.          * Lookup the mapping for a program,version and return its port
  334.          */
  335.         if (!svc_getargs(xprt, xdr_pmap, ®))
  336.             svcerr_decode(xprt);
  337.         else {
  338.             fnd = find_service(reg.pm_prog, reg.pm_vers, reg.pm_prot);
  339.             if (fnd)
  340.                 port = fnd->pml_map.pm_port;
  341.             else
  342.                 port = 0;
  343.             if ((!svc_sendreply(xprt, xdr_long, (caddr_t)&port)) &&
  344.                 debugging) {
  345.                 (void) fprintf(stderr, "svc_sendreply\n");
  346.                 abort();
  347.             }
  348.         }
  349.         break;
  350.  
  351.     case PMAPPROC_DUMP:
  352.         /*
  353.          * Return the current set of mapped program,version
  354.          */
  355.         if (!svc_getargs(xprt, xdr_void, NULL))
  356.             svcerr_decode(xprt);
  357.         else {
  358.             if ((!svc_sendreply(xprt, xdr_pmaplist,
  359.                 (caddr_t)&pmaplist)) && debugging) {
  360.                 (void) fprintf(stderr, "svc_sendreply\n");
  361.                 abort();
  362.             }
  363.         }
  364.         break;
  365.  
  366.     case PMAPPROC_CALLIT:
  367.         /*
  368.          * Calls a procedure on the local machine.  If the requested
  369.          * procedure is not registered this procedure does not return
  370.          * error information!!
  371.          * This procedure is only supported on rpc/udp and calls via 
  372.          * rpc/udp.  It passes null authentication parameters.
  373.          */
  374.         callit(rqstp, xprt);
  375.         break;
  376.  
  377.     default:
  378.         svcerr_noproc(xprt);
  379.         break;
  380.     }
  381. }
  382.  
  383.  
  384. /*
  385.  * Stuff for the rmtcall service
  386.  */
  387. #define ARGSIZE 9000
  388.  
  389. struct encap_parms {
  390.     u_int arglen;
  391.     char *args;
  392. };
  393.  
  394. static bool_t
  395. xdr_encap_parms(xdrs, epp)
  396.     XDR *xdrs;
  397.     struct encap_parms *epp;
  398. {
  399.  
  400.     return (xdr_bytes(xdrs, &(epp->args), &(epp->arglen), ARGSIZE));
  401. }
  402.  
  403. struct rmtcallargs {
  404.     u_long    rmt_prog;
  405.     u_long    rmt_vers;
  406.     u_long    rmt_port;
  407.     u_long    rmt_proc;
  408.     struct encap_parms rmt_args;
  409. };
  410.  
  411. static bool_t
  412. xdr_rmtcall_args(xdrs, cap)
  413.     register XDR *xdrs;
  414.     register struct rmtcallargs *cap;
  415. {
  416.  
  417.     /* does not get a port number */
  418.     if (xdr_u_long(xdrs, &(cap->rmt_prog)) &&
  419.         xdr_u_long(xdrs, &(cap->rmt_vers)) &&
  420.         xdr_u_long(xdrs, &(cap->rmt_proc))) {
  421.         return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
  422.     }
  423.     return (FALSE);
  424. }
  425.  
  426. static bool_t
  427. xdr_rmtcall_result(xdrs, cap)
  428.     register XDR *xdrs;
  429.     register struct rmtcallargs *cap;
  430. {
  431.     if (xdr_u_long(xdrs, &(cap->rmt_port)))
  432.         return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
  433.     return (FALSE);
  434. }
  435.  
  436. /*
  437.  * only worries about the struct encap_parms part of struct rmtcallargs.
  438.  * The arglen must already be set!!
  439.  */
  440. static bool_t
  441. xdr_opaque_parms(xdrs, cap)
  442.     XDR *xdrs;
  443.     struct rmtcallargs *cap;
  444. {
  445.  
  446.     return (xdr_opaque(xdrs, cap->rmt_args.args, cap->rmt_args.arglen));
  447. }
  448.  
  449. /*
  450.  * This routine finds and sets the length of incoming opaque paraters
  451.  * and then calls xdr_opaque_parms.
  452.  */
  453. static bool_t
  454. xdr_len_opaque_parms(xdrs, cap)
  455.     register XDR *xdrs;
  456.     struct rmtcallargs *cap;
  457. {
  458.     register u_int beginpos, lowpos, highpos, currpos, pos;
  459.  
  460.     beginpos = lowpos = pos = xdr_getpos(xdrs);
  461.     highpos = lowpos + ARGSIZE;
  462.     while ((int)(highpos - lowpos) >= 0) {
  463.         currpos = (lowpos + highpos) / 2;
  464.         if (xdr_setpos(xdrs, currpos)) {
  465.             pos = currpos;
  466.             lowpos = currpos + 1;
  467.         } else {
  468.             highpos = currpos - 1;
  469.         }
  470.     }
  471.     xdr_setpos(xdrs, beginpos);
  472.     cap->rmt_args.arglen = pos - beginpos;
  473.     return (xdr_opaque_parms(xdrs, cap));
  474. }
  475.  
  476. /*
  477.  * Call a remote procedure service
  478.  * This procedure is very quiet when things go wrong.
  479.  * The proc is written to support broadcast rpc.  In the broadcast case,
  480.  * a machine should shut-up instead of complain, less the requestor be
  481.  * overrun with complaints at the expense of not hearing a valid reply ...
  482.  *
  483.  * This now forks so that the program & process that it calls can call 
  484.  * back to the portmapper.
  485.  */
  486. static void
  487. callit(rqstp, xprt)
  488.     struct svc_req *rqstp;
  489.     SVCXPRT *xprt;
  490. {
  491.     struct rmtcallargs a;
  492.     struct pmaplist *pml;
  493.     u_short port;
  494.     struct sockaddr_in me;
  495.     int pid, so = -1;
  496.     CLIENT *client;
  497.     struct authunix_parms *au = (struct authunix_parms *)rqstp->rq_clntcred;
  498.     struct timeval timeout;
  499.     char buf[ARGSIZE];
  500.  
  501.     timeout.tv_sec = 5;
  502.     timeout.tv_usec = 0;
  503.     a.rmt_args.args = buf;
  504.     if (!svc_getargs(xprt, xdr_rmtcall_args, &a))
  505.         return;
  506.     if ((pml = find_service(a.rmt_prog, a.rmt_vers,
  507.         (u_long)IPPROTO_UDP)) == NULL)
  508.         return;
  509.     /*
  510.      * fork a child to do the work.  Parent immediately returns.
  511.      * Child exits upon completion.
  512.      */
  513.     if ((pid = fork()) != 0) {
  514.         if (pid < 0)
  515.             syslog(LOG_ERR, "CALLIT (prog %lu): fork: %m",
  516.                 a.rmt_prog);
  517.         return;
  518.     }
  519.     port = pml->pml_map.pm_port;
  520.     get_myaddress(&me);
  521.     me.sin_port = htons(port);
  522.     client = clntudp_create(&me, a.rmt_prog, a.rmt_vers, timeout, &so);
  523.     if (client != (CLIENT *)NULL) {
  524.         if (rqstp->rq_cred.oa_flavor == AUTH_UNIX) {
  525.             client->cl_auth = authunix_create(au->aup_machname,
  526.                au->aup_uid, au->aup_gid, au->aup_len, au->aup_gids);
  527.         }
  528.         a.rmt_port = (u_long)port;
  529.         if (clnt_call(client, a.rmt_proc, xdr_opaque_parms, &a,
  530.             xdr_len_opaque_parms, &a, timeout) == RPC_SUCCESS) {
  531.             svc_sendreply(xprt, xdr_rmtcall_result, (caddr_t)&a);
  532.         }
  533.         AUTH_DESTROY(client->cl_auth);
  534.         clnt_destroy(client);
  535.     }
  536.     (void)close(so);
  537.     exit(0);
  538. }
  539.  
  540. void
  541. reap()
  542. {
  543.     while (wait3((int *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
  544. }
  545.