home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher+1.2b4 / gopherd / openers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-09  |  4.1 KB  |  220 lines

  1. /********************************************************************
  2.  * lindner
  3.  * 3.3
  4.  * 1993/04/09 16:23:12
  5.  * /home/mudhoney/GopherSrc/CVS/gopher+/gopherd/openers.c,v
  6.  * Exp
  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.  * openers.c,v
  18.  * Revision 3.3  1993/04/09  16:23:12  lindner
  19.  * Additional debug stuff
  20.  *
  21.  * Revision 3.2  1993/02/19  21:22:05  lindner
  22.  * Fixed problems with non-chroot() use
  23.  *
  24.  * Revision 3.1.1.1  1993/02/11  18:02:52  lindner
  25.  * Gopher+1.2beta release
  26.  *
  27.  * Revision 1.2  1993/01/30  23:57:44  lindner
  28.  * Fixes so that opening a file doesn't depend on what the current
  29.  * directory is.
  30.  *
  31.  * Revision 1.1  1992/12/10  23:13:27  lindner
  32.  * gopher 1.1 release
  33.  *
  34.  *
  35.  *********************************************************************/
  36.  
  37.  
  38.  
  39. /*
  40.  * Routines that implement safe "openers" so that we can do without
  41.  * the chroot().  This is an advantage because then you can have
  42.  * symbolic links from your gopher server directory to other files
  43.  * that are elsewhere on your system, without (if we've done this right)
  44.  * compromising your security, or allowing access to any files that
  45.  * you don't want made available.
  46.  *
  47.  * The "r" in the names is meant to indicate "restricted".
  48.  * The "u" in the names is meant to indicate "unrestricted".
  49.  */
  50.  
  51. #include "gopherd.h"
  52. #include <sys/param.h>    /* for MAXPATHLEN */
  53.  
  54. /* and restore our real names */
  55. #undef open
  56. #undef fopen
  57. #undef stat
  58. #undef opendir
  59. #undef chdir
  60.  
  61.  
  62. char    *fixfile();
  63.  
  64. int
  65. ropen( path, flags, mode )
  66. char *path;
  67. int flags, mode;
  68. {
  69.      char *p;
  70.      p = fixfile(path);
  71.      if (p != NULL)
  72.       return( open( p, flags, mode ) );
  73.      return(-1);    /* failed */
  74. }
  75.  
  76.  
  77. FILE *
  78. rfopen( filename, type )
  79. char *filename, *type;
  80. {
  81.      char *p;
  82.      p = fixfile(filename);
  83.      if (p != NULL)
  84.       return( fopen( p, type ) );
  85.      return(NULL);    /* failed */
  86. }
  87.  
  88.  
  89. int
  90. rstat( path, buf )
  91. char *path;
  92. struct stat *buf;
  93. {
  94.      char *p;
  95.      p = fixfile(path);
  96.      if (p != NULL)
  97.       return( stat( p, buf ) );
  98.      return(-1);    /* failed */
  99. }
  100.  
  101.  
  102. DIR *
  103. ropendir( dirname )
  104. char *dirname;
  105. {
  106.      char *p;
  107.      p = fixfile(dirname);
  108.      if (p != NULL)
  109.       return( opendir( p ) );
  110.      return(NULL);    /* failed */
  111. }
  112.  
  113.  
  114. /*
  115.  * Restricted chdir.
  116.  * 
  117.  * Change to Data_Dir first if it's an absolute path, 
  118.  * then do a relative chdir from there....
  119.  */
  120.  
  121. int
  122. rchdir( path )
  123. char *path;
  124. {
  125.      char *p;
  126.      p = fixfile(path);
  127.      if (DEBUG) {
  128.       char tmpstr[128];
  129.       getwd(tmpstr);
  130.       
  131.       printf("Changing Dir to %s, from %s\n", p, tmpstr);
  132.      }
  133.  
  134.      return( chdir( p ) );
  135. }
  136.  
  137.  
  138. int
  139. uopen( path, flags, mode )
  140. char *path;
  141. int flags, mode;
  142. {
  143.      return( open( path, flags, mode ) );
  144. }
  145.  
  146.  
  147. FILE *
  148. ufopen( filename, type )
  149. char *filename, *type;
  150. {
  151.      return( fopen( filename, type ) );
  152. }
  153.  
  154.  
  155. int
  156. ustat( path, buf )
  157. char *path;
  158. struct stat *buf;
  159. {
  160.      return( stat( path, buf ) );
  161. }
  162.  
  163.  
  164. DIR *
  165. uopendir( dirname )
  166. char *dirname;
  167. {
  168.      return( opendir( dirname ) );
  169. }
  170.  
  171.  
  172. int
  173. uchdir( path )
  174. char *path;
  175. {
  176.      if (DEBUG) {
  177.       char tmpstr[128];
  178.       getwd(tmpstr);
  179.       
  180.       printf("Changing Dir to %s, from %s\n", path, tmpstr);
  181.      }
  182.  
  183.      return( chdir( path ) );
  184. }
  185.  
  186.  
  187. /* Make sure the pathname they gave us is safe and secure for use */
  188.  
  189. char *
  190. fixfile(name)
  191. char *name;
  192. {
  193.      static char newpathbuf[MAXPATHLEN];
  194.      char *newpath;
  195.  
  196.      newpath = &newpathbuf[0];
  197.  
  198.      if (!dochroot) {
  199.       strcpy(newpath, Data_Dir);
  200.  
  201.       newpath += strlen(Data_Dir);
  202.      }
  203.      else {
  204.       strcpy(newpath, "/");
  205.      }
  206.      /* set errno to EPERM in case we reject the request */
  207.      errno = EPERM;
  208.  
  209.      /*
  210.      ** rip any .. or . entries out, so they can't sneak up out of
  211.      ** the gopher directory.  Need to use dedot2() so we don't clobber
  212.      ** the string they sent us originally.
  213.      */
  214.      dedot2(name,newpath);
  215.      if (*newpath == '/')
  216.       return(newpathbuf);
  217.      else 
  218.       return(newpath);
  219. }
  220.