home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / src / gopher / gopher1.01 / gopherd / openers.c < prev    next >
C/C++ Source or Header  |  1992-04-08  |  3KB  |  171 lines

  1. /*
  2.  * Routines that implement safe "openers" so that we can do without
  3.  * the chroot().  This is an advantage because then you can have
  4.  * symbolic links from your gopher server directory to other files
  5.  * that are elsewhere on your system, without (if we've done this right)
  6.  * compromising your security, or allowing access to any files that
  7.  * you don't want made available.
  8.  *
  9.  * The "r" in the names is meant to indicate "restricted".
  10.  * The "u" in the names is meant to indicate "unrestricted".
  11.  */
  12.  
  13. #include "gopherd.h"
  14. #include <sys/param.h>    /* for MAXPATHLEN */
  15.  
  16. /* and restore our real names */
  17. #undef open
  18. #undef fopen
  19. #undef stat
  20. #undef opendir
  21. #undef chdir
  22.  
  23.  
  24. char    *fixfile();
  25.  
  26.  
  27. int
  28. ropen( path, flags, mode )
  29. char *path;
  30. int flags, mode;
  31. {
  32.      char *p;
  33.      p = fixfile(path);
  34.      if (p != NULL)
  35.       return( open( p, flags, mode ) );
  36.      return(-1);    /* failed */
  37. }
  38.  
  39.  
  40. FILE *
  41. rfopen( filename, type )
  42. char *filename, *type;
  43. {
  44.      char *p;
  45.      p = fixfile(filename);
  46.      if (p != NULL)
  47.       return( fopen( p, type ) );
  48.      return(NULL);    /* failed */
  49. }
  50.  
  51.  
  52. int
  53. rstat( path, buf )
  54. char *path;
  55. struct stat *buf;
  56. {
  57.      char *p;
  58.      p = fixfile(path);
  59.      if (p != NULL)
  60.       return( stat( p, buf ) );
  61.      return(-1);    /* failed */
  62. }
  63.  
  64.  
  65. DIR *
  66. ropendir( dirname )
  67. char *dirname;
  68. {
  69.      char *p;
  70.      p = fixfile(dirname);
  71.      if (p != NULL)
  72.       return( opendir( p ) );
  73.      return(NULL);    /* failed */
  74. }
  75.  
  76.  
  77. /*
  78.  * Restricted chdir.
  79.  * 
  80.  * Change to Data_Dir first if it's an absolute path, 
  81.  * then do a relative chdir from there....
  82.  */
  83.  
  84. int
  85. rchdir( path )
  86. char *path;
  87. {
  88.      char *p;
  89.      p = fixfile(path);
  90.      if (p != NULL) {
  91.       if (*p == '/') {
  92.            chdir(Data_Dir);
  93.            return(chdir(p+1));
  94.       } 
  95.       else
  96.            return( chdir( p ) );
  97.      }
  98.      else
  99.       return(-1);    /* failed */
  100. }
  101.  
  102.  
  103. int
  104. uopen( path, flags, mode )
  105. char *path;
  106. int flags, mode;
  107. {
  108.      return( open( path, flags, mode ) );
  109. }
  110.  
  111.  
  112. FILE *
  113. ufopen( filename, type )
  114. char *filename, *type;
  115. {
  116.      return( fopen( filename, type ) );
  117. }
  118.  
  119.  
  120. int
  121. ustat( path, buf )
  122. char *path;
  123. struct stat *buf;
  124. {
  125.      return( stat( path, buf ) );
  126. }
  127.  
  128.  
  129. DIR *
  130. uopendir( dirname )
  131. char *dirname;
  132. {
  133.      return( opendir( dirname ) );
  134. }
  135.  
  136.  
  137. int
  138. uchdir( path )
  139. char *path;
  140. {
  141.      return( chdir( path ) );
  142. }
  143.  
  144.  
  145. /* Make sure the pathname they gave us is safe and secure for use */
  146.  
  147. char *
  148. fixfile(name)
  149. char *name;
  150. {
  151.      static char newpathbuf[MAXPATHLEN];
  152.      char *newpath;
  153.  
  154.      newpath = &newpathbuf[0];
  155.  
  156.      /* set errno to EPERM in case we reject the request */
  157.      errno = EPERM;
  158.  
  159.      /*
  160.      ** rip any .. or . entries out, so they can't sneak up out of
  161.      ** the gopher directory.  Need to use dedot2() so we don't clobber
  162.      ** the string they sent us originally.
  163.      */
  164.      dedot2(name,newpath);
  165.      while ( *newpath == '/' )    /* make it relative path */
  166.       newpath++;
  167.      if ( *newpath == '\0' )    /* nothing left - it was "/" */
  168.       newpath = ".";
  169.      return( newpath );
  170. }
  171.