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