home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / lan / soss.arj / SRC / MOUNTD.C < prev    next >
C/C++ Source or Header  |  1991-02-22  |  5KB  |  190 lines

  1. /*
  2.  *  mountd.c --
  3.  *      Mount daemon program for PC NFS.  Runs on top of the net daemon
  4.  *      (netd.c).  This is a partial implementation of the UNIX mount
  5.  *      daemon mountd(8c).
  6.  *
  7.  *  Author:
  8.  *      See-Mong Tan, 6/12/88]
  9.  */
  10.  
  11. #include "common.h"
  12.  
  13. static void expreply (struct svc_req *, SVCXPRT *);
  14.  
  15. /*
  16.  *  bool_t mountd_init() --
  17.  *      Mount daemon initialization.  Creates and registers transport
  18.  *      handle, and sets exports in the file EXPORTS.
  19.  */
  20. bool_t mountd_init()
  21. {
  22.     int sock;
  23.     struct sockaddr_in addr;
  24.     SVCXPRT *transp;
  25.  
  26.     addr.sin_family = AF_INET;
  27.     addr.sin_port = htons(MOUNTPORT);
  28.     addr.sin_addr.s_addr = INADDR_ANY;
  29.     if ((sock = sock_create(SOCK_DGRAM, IPPROTO_UDP, &addr)) < 0) {
  30.             (void) fprintf (stderr, "mountd: cannot get mount socket");
  31.         sock_close(sock);
  32.         return FALSE;
  33.     }
  34.     if ((transp = svcudp_create(sock, 0)) == (SVCXPRT *) NULL) {
  35.         (void) fprintf (stderr, "mountd: cannot create udp handle");
  36.         sock_close(sock);
  37.         return FALSE;
  38.     }
  39.     if (! svc_register(transp, MOUNTPROG, MOUNTVERS, mountd_dispatch, 
  40.         IPPROTO_UDP)) {
  41.         (void) fprintf(stderr, "mountd: cannot register transport\n");
  42.         sock_close(sock);
  43.         return FALSE;
  44.     }
  45.     if (! exps_parse()) {
  46.         (void) fprintf(stderr, "mountd: cannot parse exports file\n");
  47.         sock_close(sock);
  48.         return FALSE;
  49.     }
  50.     
  51.     return TRUE;
  52. }
  53.  
  54. /*
  55.  *  void mountd_dispatch(struct svc_req *req, SVCXPRT *xprt) --
  56.  *      Dispatch routine for the mount daemon.
  57.  */
  58. void mountd_dispatch(req, xprt)
  59.     struct svc_req *req;
  60.     SVCXPRT *xprt;
  61. {
  62. #if DEBUG
  63. static char *names[] = {"NULL", "MNT", "DUMP", "UMNT", "UMNTALL",
  64.             "EXPORT", "EXPORTALL", "<invalid>"};
  65.     DBGPRT1 (mountd, ">>> MOUNTPROC_%s", names[(req->rq_proc >
  66.         MOUNTPROC_EXPORTALL) ? MOUNTPROC_EXPORTALL+1 : req->rq_proc]);
  67. #endif
  68.  
  69.     switch((int) req->rq_proc) {
  70.  
  71.       case NULLPROC:            /* "ping" the mount daemon */
  72.     if (! svc_sendreply(xprt, xdr_void, 0)) {
  73.         (void) fprintf(stderr, "mountd: cannot send reply\n");
  74.         return;
  75.     }
  76.     break;
  77.     
  78.       case MOUNTPROC_MNT:    mount(req, xprt);    break;
  79.       case MOUNTPROC_UMNT:    unmount(req, xprt);    break;
  80.       case MOUNTPROC_EXPORT:    expreply(req, xprt);    break;
  81.       case MOUNTPROC_DUMP:
  82.       case MOUNTPROC_UMNTALL:
  83.       case MOUNTPROC_EXPORTALL:
  84.  
  85.       default:
  86.     svcerr_noproc(xprt);
  87.     }
  88. }        
  89.  
  90. /*
  91.  *  void mount(struct svc_req *req, SVCXPRT *xprt) --
  92.  *      Services a mount request
  93.  */
  94. void mount(req, xprt)
  95.     struct svc_req *req;
  96.     SVCXPRT *xprt;
  97. {
  98.     char *path = NULL, *hostname;
  99.     struct fhstatus fhs;        /* file handle status */
  100.     struct sockaddr_in addr;
  101.  
  102.     if (! svc_getargs(xprt, xdr_path, &path)) {
  103.             DBGPRT0 (mountd, "cannot decode mount path");
  104.         svcerr_decode(xprt);
  105.         return;
  106.     }
  107.     DBGPRT1 (mountd, "path: %s", path);
  108.     addr = *(svc_getcaller(xprt));
  109.     if (! exps_isclnt(path, addr.sin_addr.s_addr)) {
  110.             DBGPRT0 (mountd, "access denied");
  111.         fhs.fhs_status = NFSERR_ACCES;
  112.     }
  113.     else if (! mntpntofh(path, &(fhs.fhs_fh))) {
  114.             DBGPRT0 (mountd, "illegal path");
  115.         fhs.fhs_status = (int) NFSERR_NOENT;
  116.     }
  117.     else 
  118.         fhs.fhs_status = (int) NFS_OK;
  119.  
  120.     /* reply to caller */
  121.     if (! svc_sendreply(xprt, xdr_fhstatus, &fhs))
  122.         (void) fprintf(stderr, "mount: cannot reply to mount req\n");
  123.  
  124.     svc_freeargs(xprt, xdr_path, &path);
  125. }
  126.  
  127. /*
  128.  *  void unmount(struct svc_req *req, SVCXPRT *xprt) --
  129.  *      Unmount a filesystem.
  130.  */
  131. void unmount(req, xprt)
  132.     struct svc_req *req;
  133.     SVCXPRT *xprt;
  134. {
  135.     char *path = NULL;
  136.     fhandle_t fh;
  137.     
  138.     if (! svc_getargs(xprt, xdr_path, &path)) {
  139.             DBGPRT0 (mountd, "unmount: cannot decode");
  140.         svcerr_decode(xprt);
  141.         return;
  142.     }
  143.     DBGPRT1 (mountd, "unmounting %s", path);
  144.     if (! mntpntofh(path, &fh))
  145.         DBGPRT0 (mountd, "unmount: not in mount list");
  146.  
  147.     if (! svc_sendreply(xprt, xdr_void, NULL)) {
  148.         (void) fprintf(stderr, "unmount: cannot send reply\n");
  149.     }
  150.     svc_freeargs(xprt, xdr_path, &path);
  151. }
  152.         
  153. /*
  154.  *  void expreply(struct svc_req *req, SVCXPRT *xprt) --
  155.  *      Services an exports request
  156.  */
  157. void expreply(req, xprt)
  158.      struct svc_req *req;
  159.      SVCXPRT *xprt;
  160. {
  161.     struct exports *exp_list;
  162.  
  163.     exp_list = exps_get ();
  164.  
  165.     /* reply to caller */
  166.     if (! svc_sendreply(xprt, xdr_exports, &exp_list))
  167.       (void) fprintf(stderr, "mount: cannot reply to export req\n");
  168. }
  169.  
  170. /*
  171.  *  bool_t mntpntofh(char *path, fhandle_t *fhp) --
  172.  *      Checks that path matches something in the export list and
  173.  *      converts the path name to file status handle.
  174.  */
  175. bool_t mntpntofh(path, fhp)
  176.     char *path;
  177.     fhandle_t *fhp;
  178. {
  179.     Exports *ptr;
  180.  
  181.     if ((ptr = exps_isexport(path)) == NULL) {    /* not in list */
  182.             DBGPRT1 (mountd, "path \"%s\" not in export list", path);
  183.         return FALSE;
  184.     }
  185.     /* anything in export list should already be in directory tree cache */
  186.     *fhp = pntofh (path);
  187.     return TRUE;
  188. }
  189.  
  190.