home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / utils / sossntr3 / src / mountd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  5.2 KB  |  205 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.     SOCKET sock;
  23.     SOCKADDR_IN local_sin;
  24.     SVCXPRT *transp;
  25.  
  26.     sock = socket( PF_INET, SOCK_DGRAM, 0);
  27.     if (sock == INVALID_SOCKET) {
  28.         (void) fprintf(stderr, "cannot create mountd socket\n");
  29.         return FALSE;
  30.     }
  31.  
  32.     local_sin.sin_family = AF_INET;
  33.     local_sin.sin_port = htons(MOUNTPORT);
  34.     local_sin.sin_addr.s_addr = INADDR_ANY;
  35.  
  36.     if (bind( sock,(SOCKADDR *)(&local_sin), sizeof(local_sin)) == SOCKET_ERROR) {
  37.         fprintf(stderr,"bind() failed");
  38.         return FALSE;
  39.     }
  40.  
  41.     if ((transp = svcudp_create(sock, 0, local_sin.sin_port)) == (SVCXPRT *) NULL) {
  42.         (void) fprintf (stderr, "mountd: cannot create udp handle");
  43.         closesocket(sock);
  44.         return FALSE;
  45.     }
  46.     if (! svc_register(transp, MOUNTPROG, MOUNTVERS, mountd_dispatch, 
  47.         IPPROTO_UDP)) {
  48.         (void) fprintf(stderr, "mountd: cannot register transport\n");
  49.         closesocket(sock);
  50.         return FALSE;
  51.     }
  52.     if (! svc_register(transp, MOUNTPROG, MOUNTVERS_OLD, mountd_dispatch, 
  53.         IPPROTO_UDP)) {
  54.         (void) fprintf(stderr, "mountd: warning: cannot register old version\n");
  55.     }
  56.     if (! exps_parse()) {
  57.         (void) fprintf(stderr, "mountd: cannot parse exports file\n");
  58.         closesocket(sock);
  59.         return FALSE;
  60.     }
  61.     
  62.     return TRUE;
  63. }
  64.  
  65. /*
  66.  *  void mountd_dispatch(struct svc_req *req, SVCXPRT *xprt) --
  67.  *      Dispatch routine for the mount daemon.
  68.  */
  69. void mountd_dispatch(req, xprt)
  70.     struct svc_req *req;
  71.     SVCXPRT *xprt;
  72. {
  73. #if DEBUG
  74. static char *names[] = {"NULL", "MNT", "DUMP", "UMNT", "UMNTALL",
  75.             "EXPORT", "EXPORTALL", "<invalid>"};
  76.     DBGPRT1 (mountd, ">>> MOUNTPROC_%s", names[(req->rq_proc >
  77.         MOUNTPROC_EXPORTALL) ? MOUNTPROC_EXPORTALL+1 : req->rq_proc]);
  78. #endif
  79.  
  80.     switch((int) req->rq_proc) {
  81.  
  82.       case NULLPROC:            /* "ping" the mount daemon */
  83.     if (! svc_sendreply(xprt, xdr_void, 0)) {
  84.         (void) fprintf(stderr, "mountd: cannot send reply\n");
  85.         return;
  86.     }
  87.     break;
  88.     
  89.       case MOUNTPROC_MNT:   mount(req, xprt);   break;
  90.       case MOUNTPROC_UMNT:  unmount(req, xprt); break;
  91.       case MOUNTPROC_EXPORT:    expreply(req, xprt);    break;
  92.       case MOUNTPROC_DUMP:
  93.       case MOUNTPROC_UMNTALL:
  94.       case MOUNTPROC_EXPORTALL:
  95.  
  96.       default:
  97.     svcerr_noproc(xprt);
  98.     }
  99. }       
  100.  
  101. /*
  102.  *  void mount(struct svc_req *req, SVCXPRT *xprt) --
  103.  *      Services a mount request
  104.  */
  105. void mount(req, xprt)
  106.     struct svc_req *req;
  107.     SVCXPRT *xprt;
  108. {
  109.     char *path = NULL;
  110.     struct fhstatus fhs;        /* file handle status */
  111.     SOCKADDR_IN local_sin;
  112.     unsigned long   address;
  113.  
  114.     if (! svc_getargs(xprt, xdr_path, &path)) {
  115.             DBGPRT0 (mountd, "cannot decode mount path");
  116.         svcerr_decode(xprt);
  117.         return;
  118.     }
  119.     DBGPRT1 (mountd, "path: %s", path);
  120.     memcpy( &address, &xprt->xp_raddr.sa_data[2], 4);
  121. //    local_sin = *(svc_getcaller(xprt));
  122. //    if (! exps_isclnt(path, local_sin.sin_addr.s_addr)) {
  123.     if (! exps_isclnt(path, address)) {
  124.  
  125.             DBGPRT0 (mountd, "access denied");
  126.         fhs.fhs_status = NFSERR_ACCES;
  127.     }
  128.     else if (! mntpntofh(path, &(fhs.fhs_fh))) {
  129.             DBGPRT0 (mountd, "illegal path");
  130.         fhs.fhs_status = (int) NFSERR_NOENT;
  131.     }
  132.     else 
  133.         fhs.fhs_status = (int) NFS_OK;
  134.  
  135.     /* reply to caller */
  136.     if (! svc_sendreply(xprt, xdr_fhstatus, &fhs))
  137.         (void) fprintf(stderr, "mount: cannot reply to mount req\n");
  138.  
  139.     svc_freeargs(xprt, xdr_path, &path);
  140. }
  141.  
  142. /*
  143.  *  void unmount(struct svc_req *req, SVCXPRT *xprt) --
  144.  *      Unmount a filesystem.
  145.  */
  146. void unmount(req, xprt)
  147.     struct svc_req *req;
  148.     SVCXPRT *xprt;
  149. {
  150.     char *path = NULL;
  151.     fhandle_t fh;
  152.     
  153.     if (! svc_getargs(xprt, xdr_path, &path)) {
  154.             DBGPRT0 (mountd, "unmount: cannot decode");
  155.         svcerr_decode(xprt);
  156.         return;
  157.     }
  158.     DBGPRT1 (mountd, "unmounting %s", path);
  159.     if (! mntpntofh(path, &fh))
  160.         DBGPRT0 (mountd, "unmount: not in mount list");
  161.  
  162.     if (! svc_sendreply(xprt, xdr_void, NULL)) {
  163.         (void) fprintf(stderr, "unmount: cannot send reply\n");
  164.     }
  165.     svc_freeargs(xprt, xdr_path, &path);
  166. }
  167.         
  168. /*
  169.  *  void expreply(struct svc_req *req, SVCXPRT *xprt) --
  170.  *      Services an exports request
  171.  */
  172. void expreply(req, xprt)
  173.      struct svc_req *req;
  174.      SVCXPRT *xprt;
  175. {
  176.     struct exports *exp_list;
  177.  
  178.     exp_list = exps_get ();
  179.  
  180.     /* reply to caller */
  181.     if (! svc_sendreply(xprt, xdr_exports, &exp_list))
  182.       (void) fprintf(stderr, "mount: cannot reply to export req\n");
  183. }
  184.  
  185. /*
  186.  *  bool_t mntpntofh(char *path, fhandle_t *fhp) --
  187.  *      Checks that path matches something in the export list and
  188.  *      converts the path name to file status handle.
  189.  */
  190. bool_t mntpntofh(path, fhp)
  191.     char *path;
  192.     fhandle_t *fhp;
  193. {
  194.     Exports *ptr;
  195.  
  196.     if ((ptr = exps_isexport(path)) == NULL) {  /* not in list */
  197.             DBGPRT1 (mountd, "path \"%s\" not in export list", path);
  198.         return FALSE;
  199.     }
  200.     /* anything in export list should already be in directory tree cache */
  201.     *fhp = pntofh (path);
  202.     return TRUE;
  203. }
  204.  
  205.