home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher1.12 / gopherd / openers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-21  |  3.7 KB  |  199 lines

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