home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / n / tcpip / netkit-a.06 / netkit-a / NetKit-A-0.06 / nfs-server-2.0 / mountd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-13  |  5.4 KB  |  258 lines

  1. /*
  2.  * mountd    This program handles RPC "NFS" mount requests.
  3.  *
  4.  * Usage:    [rpc.]mountd [-dhnpv] [-f authfile]
  5.  *
  6.  * Authors:    Mark A. Shand, May 1988
  7.  *        Donald J. Becker, <becker@super.org>
  8.  *        Rick Sladkey, <jrs@world.std.com>
  9.  *        Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10.  *
  11.  *        Copyright 1988 Mark A. Shand
  12.  *        This software maybe be used for any purpose provided
  13.  *        the above copyright notice is retained.  It is supplied
  14.  *        as is, with no warranty expressed or implied.
  15.  */
  16.  
  17. #include "nfsd.h"
  18. #include "getopt.h"
  19.  
  20. #ifndef HAVE_RPCGEN_C
  21. #define mountproc_null_1_svc        mountproc_null_1
  22. #define mountproc_mnt_1_svc        mountproc_mnt_1
  23. #define mountproc_dump_1_svc        mountproc_dump_1
  24. #define mountproc_umnt_1_svc        mountproc_umnt_1
  25. #define mountproc_umntall_1_svc        mountproc_umntall_1
  26. #define mountproc_export_1_svc        mountproc_export_1
  27. #define mountproc_exportall_1_svc    mountproc_exportall_1
  28. #endif
  29.  
  30. char argbuf[MNTPATHLEN + 1];
  31. char *auth_file = NULL;
  32. #ifndef HAVE_RPCGEN_I
  33. int _rpcpmstart = 0;
  34. #endif
  35.  
  36. static _PRO(void usage, (FILE *, int));
  37. extern _PRO(int mountd_main, (int, char **));
  38.  
  39. extern char version[];
  40. static char *program_name;
  41. static struct option longopts[] =
  42. {
  43.     { "debug", 0, 0, 'd' },
  44.     { "exports-file", 1, 0, 'f' },
  45.     { "help", 0, 0, 'h' },
  46.     { "allow-non-root", 0, 0, 'n' },
  47.     { "promiscous", 0, 0, 'p' },
  48.     { "re-export", 0, 0, 'r', },
  49.     { "version", 0, 0, 'v' },
  50.     { NULL, 0, 0, 0 }
  51. };
  52.  
  53. /* In mount_svc.c: */
  54. extern _PRO(void mount_run, (void));
  55.  
  56. /* The NULL request handler. */
  57. void *mountproc_null_1_svc(argp, rqstp)
  58. void *argp;
  59. struct svc_req *rqstp;
  60. {
  61.     static char res;
  62.  
  63.     memset(&res, '\0', sizeof(res));
  64.     log_call(rqstp, "mountproc_null_1", "");
  65.     return ((void *) &res);
  66. }
  67.  
  68. fhstatus *mountproc_mnt_1_svc(argp, rqstp)
  69. dirpath *argp;
  70. struct svc_req *rqstp;
  71. {
  72.     static fhstatus res;
  73.     struct stat stbuf;
  74.     clnt_param *cp;
  75.     char nargbuf[MNTPATHLEN + 1];
  76.  
  77.     memset(&res, '\0', sizeof(res));
  78.     sprintf(argbuf, "%s", *argp);
  79.     log_call(rqstp, "mountproc_mnt_1", argbuf);
  80.  
  81.     /* It is important to resolve symlinks before checking permissions. */
  82.     if (realpath(argbuf, nargbuf) == NULL) {
  83.         res.fhs_status = nfs_errno();
  84.         return (&res);
  85.     }
  86.     strcpy(argbuf, nargbuf);
  87.     if (stat(argbuf, &stbuf) < 0) {
  88.         res.fhs_status = nfs_errno();
  89.         return (&res);
  90.     }
  91.  
  92.     /* Now authenticate the intruder... */
  93.     if (((cp = auth_clnt(rqstp, argbuf)) == NULL))
  94.         res.fhs_status = NFSERR_ACCES;
  95.     else if (!S_ISDIR(stbuf.st_mode) && !S_ISREG(stbuf.st_mode))
  96.         res.fhs_status = NFSERR_NOTDIR;
  97.     else
  98.         res.fhs_status = fh_create((nfs_fh *)
  99.             &res.fhstatus_u.fhs_fhandle, argbuf);
  100.     return (&res);
  101. }
  102.  
  103. mountlist *mountproc_dump_1_svc(argp, rqstp)
  104. void *argp;
  105. struct svc_req *rqstp;
  106. {
  107.     static mountlist res;
  108.  
  109.     memset(&res, '\0', sizeof(res));
  110.     log_call(rqstp, "mountproc_dump_1", "");
  111.     return (&res);
  112. }
  113.  
  114. void *mountproc_umnt_1_svc(argp, rqstp)
  115. dirpath *argp;
  116. struct svc_req *rqstp;
  117. {
  118.     static char res;
  119.  
  120.     memset(&res, '\0', sizeof(res));
  121.     sprintf(argbuf, "%s", *argp);
  122.     log_call(rqstp, "mountproc_umnt_1", argbuf);
  123.     return ((void *) &res);
  124. }
  125.  
  126. void *mountproc_umntall_1_svc(argp, rqstp)
  127. void *argp;
  128. struct svc_req *rqstp;
  129. {
  130.     static char res;
  131.  
  132.     memset(&res, '\0', sizeof(res));
  133.     log_call(rqstp, "mountproc_umntall_1", "");
  134.     return ((void *) &res);
  135. }
  136.  
  137. exports *mountproc_export_1_svc(argp, rqstp)
  138. void *argp;
  139. struct svc_req *rqstp;
  140. {
  141.     static exports res;
  142.  
  143.     memset(&res, '\0', sizeof(res));
  144.     log_call(rqstp, "mountproc_export_1", "");
  145.     res = export_list;
  146.     return (&res);
  147. }
  148.  
  149. exports *mountproc_exportall_1_svc(argp, rqstp)
  150. void *argp;
  151. struct svc_req *rqstp;
  152. {
  153.     log_call(rqstp, "mountproc_exportall_1", "");
  154.     return (mountproc_export_1_svc(argp, rqstp));
  155. }
  156.  
  157. static void mountd_init(argc, argv)
  158. int argc;
  159. char *argv[];
  160. {
  161.     int c;
  162.  
  163.     program_name = argv[0];
  164.  
  165.     /* Parse the command line options and arguments. */
  166.     opterr = 0;
  167.     while ((c = getopt_long(argc, argv, "df:hnprv", longopts, NULL)) != EOF)
  168.         switch (c) {
  169.         case 'h':
  170.             usage(stdout, 0);
  171.             break;
  172.         case 'd':
  173.             toggle_logging(0);
  174.             break;
  175.         case 'f':
  176.             auth_file = optarg;
  177.             break;
  178.         case 'n':
  179.             allow_non_root = 1;
  180.             break;
  181.         case 'p':
  182.             promiscuous = 1;
  183.             break;
  184.         case 'r':
  185.             re_export = 1;
  186.             break;
  187.         case 'v':
  188.             printf("%s\n", version);
  189.             exit(0);
  190.         case 0:
  191.             break;
  192.         case '?':
  193.         default:
  194.             usage(stderr, 1);
  195.         }
  196.  
  197.     /* No more arguments allowed. */
  198.     if (optind != argc)
  199.         usage(stderr, 1);
  200.  
  201. #ifndef HAVE_RPCGEN_I
  202. #ifndef RPC_SVC_FG
  203.     /* We first fork off a child. */
  204.     if ((c = fork()) > 0)
  205.         exit(0);
  206.     if (c < 0) {
  207.         fprintf(stderr, "mountd: cannot fork: %s\n", strerror(errno));
  208.         exit(-1);
  209.     }
  210.     /* Now we remove ourselves from the foreground. */
  211.     (void) close(0);
  212.     (void) close(1);
  213.     (void) close(2);
  214. #ifdef TIOCNOTTY
  215.     if ((c = open("/dev/tty", O_RDWR)) >= 0) {
  216.         (void) ioctl(c, TIOCNOTTY, (char *) NULL);
  217.         (void) close(c);
  218.     }
  219. #else
  220.     setsid();
  221. #endif
  222. #endif /* not RPC_SVC_FG */
  223. #endif /* not HAVE_RPCGEN_I */
  224.  
  225.     /* Initialize logging. */
  226.     log_open("mountd");
  227.  
  228.     /* Initialize the FH module. */
  229.     fh_init();
  230.  
  231.     /* Initialize the AUTH module. */
  232.     auth_init(auth_file);
  233.  
  234.     /* Enable the LOG toggle with a signal. */
  235.     (void) signal(SIGUSR1, toggle_logging);
  236. }
  237.  
  238. int main(argc, argv)
  239. int argc;
  240. char *argv[];
  241. {
  242.     mountd_init(argc, argv);
  243.     mountd_main(argc, argv);
  244.     exit(0);
  245. }
  246.  
  247. static void usage(fp, n)
  248. FILE *fp;
  249. int n;
  250. {
  251.     fprintf(fp, "Usage: %s [-dhnpv] [-f exports-file]\n", program_name);
  252.     fprintf(fp, "       [--debug] [--help] [--allow-non-root]\n");
  253.     fprintf(fp, "       [--promiscuous] [--version]\n");
  254.     fprintf(fp, "       [--exports-file=file]\n");
  255.     exit(n);
  256. }
  257.  
  258.