1430c02000-03-16Martin Nilsson // This is a roxen module. Copyright © 1996 - 2000, Roxen IS.
57f45e1996-11-27Per Hedbor 
b1fca01996-11-12Per Hedbor // This is a virtual "file-system". // It will be located somewhere in the name-space of the server.
57f45e1996-11-27Per Hedbor // Also inherited by some of the other filesystems.
f6d62d1997-03-26Per Hedbor 
1909011998-02-24Henrik Grubbström (Grubba) inherit "module"; inherit "socket";
191ff32001-08-24Per Hedbor constant cvs_version= "$Id: filesystem.pike,v 1.108 2001/08/23 23:00:26 per Exp $";
1909011998-02-24Henrik Grubbström (Grubba) constant thread_safe=1;
6682b91997-08-31Peter Bortas 
b1fca01996-11-12Per Hedbor #include <module.h>
48fa361997-04-05Per Hedbor #include <roxen.h>
14179b1997-01-29Per Hedbor #include <stat.h>
c5e0961999-10-04Per Hedbor #include <request_trace.h>
b1fca01996-11-12Per Hedbor 
79ca872000-11-24Per Hedbor  //<locale-token project="mod_filesystem">LOCALE</locale-token> #define LOCALE(X,Y) _DEF_LOCALE("mod_filesystem",X,Y) // end of the locale related stuff
ac69b51999-12-28Martin Nilsson #ifdef FILESYSTEM_DEBUG # define FILESYSTEM_WERR(X) werror("Filesystem: "+X+"\n") #else # define FILESYSTEM_WERR(X) #endif #ifdef QUOTA_DEBUG # define QUOTA_WERR(X) werror("QUOTA: "+X+"\n") #else # define QUOTA_WERR(X) #endif
c7ec632001-08-15Henrik Grubbström (Grubba) #if constant(system.normalize_path) #define NORMALIZE_PATH(X) system.normalize_path(X) #else /* !constant(system.normalize_path) */ #define NORMALIZE_PATH(X) (X) #endif /* constant(system.normalize_path) */
c5e0961999-10-04Per Hedbor constant module_type = MODULE_LOCATION;
f2e3482001-05-20Martin Nilsson LocaleString module_name = LOCALE(51,"File systems: Normal File system");
f0d6942001-01-29Per Hedbor LocaleString module_doc =
de9ca82000-11-27Per Hedbor LOCALE(2,"This is the basic file system module that makes it possible "
79ca872000-11-24Per Hedbor  "to mount a directory structure in the virtual file system of " "your site.");
c5e0961999-10-04Per Hedbor constant module_unique = 0;
6396111997-02-14Per Hedbor 
b1fca01996-11-12Per Hedbor int redirects, accesses, errors, dirlists;
6cec001998-05-14David Hedbor int puts, deletes, mkdirs, moves, chmods;
b1fca01996-11-12Per Hedbor 
1358d51999-05-06Henrik Grubbström (Grubba) static mapping http_low_answer(int errno, string data, string|void desc) {
7f59662000-05-16Henrik Grubbström (Grubba)  mapping res = Roxen.http_low_answer(errno, data);
1358d51999-05-06Henrik Grubbström (Grubba)  if (desc) { res->rettext = desc; } return res; }
b1fca01996-11-12Per Hedbor static int do_stat = 1; string status() {
de9ca82000-11-27Per Hedbor  return "<h2>"+LOCALE(3,"Accesses to this filesystem")+"</h2>"+ (redirects?"<b>"+LOCALE(4,"Redirects")+"</b>: "+redirects+"<br>":"")+ (accesses?"<b>"+LOCALE(5,"Normal files")+"</b>: "+accesses+"<br>" :LOCALE(6,"No file accesses)")+"<br>")+
ed540b2001-01-13Martin Nilsson  (query("put")&&puts?"<b>"+LOCALE(7,"PUTs")+"</b>: "+puts+"<br>":"")+ (query("put")&&mkdirs?"<b>"+LOCALE(8,"MKDIRs")+"</b>: "+mkdirs+"<br>":"")+ (query("put")&&query("delete")&&moves?
de9ca82000-11-27Per Hedbor  "<b>"+LOCALE(9,"Moved files")+"</b>: "+moves+"<br>":"")+
ed540b2001-01-13Martin Nilsson  (query("put")&&chmods?"<b>"+LOCALE(10,"CHMODs")+"</b>: "+chmods+"<br>":"")+ (query("delete")&&deletes?"<b>"+LOCALE(11,"Deletes")+"</b>: "+deletes+"<br>":"")+
de9ca82000-11-27Per Hedbor  (errors?"<b>"+LOCALE(12,"Permission denied")+"</b>: "+errors +" ("+LOCALE(13,"not counting .htaccess")+")<br>":"")+ (dirlists?"<b>"+LOCALE(14,"Directories")+"</b>:"+dirlists+"<br>":"");
b1fca01996-11-12Per Hedbor } void create() {
b1e8672001-08-01Per Hedbor  defvar("mountpoint", "/", LOCALE(15,"Mount point"), TYPE_LOCATION|VAR_INITIAL|VAR_NO_DEFAULT,
de9ca82000-11-27Per Hedbor  LOCALE(16,"Where the module will be mounted in the site's virtual "
79ca872000-11-24Per Hedbor  "file system."));
b1e8672001-08-01Per Hedbor  defvar("searchpath", "NONE", LOCALE(17,"Search path"), TYPE_DIR|VAR_INITIAL|VAR_NO_DEFAULT,
de9ca82000-11-27Per Hedbor  LOCALE(18,"The directory that contains the files."));
79ca872000-11-24Per Hedbor 
de9ca82000-11-27Per Hedbor  defvar(".files", 0, LOCALE(19,"Show hidden files"), TYPE_FLAG|VAR_MORE, LOCALE(20,"If set, hidden files, ie files that begin with a '.', "
79ca872000-11-24Per Hedbor  "will be shown in directory listings." ));
de9ca82000-11-27Per Hedbor  defvar("dir", 1, LOCALE(21,"Enable directory listings per default"),
79ca872000-11-24Per Hedbor  TYPE_FLAG|VAR_MORE,
de9ca82000-11-27Per Hedbor  LOCALE(22,"If set, it will be possible to get a directory listings "
79ca872000-11-24Per Hedbor  "from directories in this file system. It is possible to " "force a directory to never be browsable by putting a " "<tt>.www_not_browsable</tt> or a <tt>.nodiraccess</tt> file " "in it. Similarly it is possible to let a directory be " "browsable, even if the file system is not, by putting a " "<tt>.www_browsable</tt> file in it.\n"));
b1fca01996-11-12Per Hedbor 
c4908f2000-05-06Martin Nilsson  defvar("nobrowse", ({ ".www_not_browsable", ".nodiraccess" }),
de9ca82000-11-27Per Hedbor  LOCALE(23,"List prevention files"), TYPE_STRING_LIST|VAR_MORE, LOCALE(24,"All directories containing any of these files will not be "
79ca872000-11-24Per Hedbor  "browsable."));
c4908f2000-05-06Martin Nilsson 
de9ca82000-11-27Per Hedbor  defvar("tilde", 0, LOCALE(25,"Show backup files"), TYPE_FLAG|VAR_MORE, LOCALE(26,"If set, files ending with '~', '#' or '.bak' will "+
79ca872000-11-24Per Hedbor  "be shown in directory listings"));
b1fca01996-11-12Per Hedbor 
de9ca82000-11-27Per Hedbor  defvar("put", 0, LOCALE(27,"Handle the PUT method"), TYPE_FLAG, LOCALE(28,"If set, it will be possible to upload files with the HTTP "
79ca872000-11-24Per Hedbor  "method PUT, or through FTP."));
b1fca01996-11-12Per Hedbor 
de9ca82000-11-27Per Hedbor  defvar("delete", 0, LOCALE(29,"Handle the DELETE method"), TYPE_FLAG, LOCALE(30,"If set, it will be possible to delete files with the HTTP "
79ca872000-11-24Per Hedbor  "method DELETE, or through FTP."));
b1fca01996-11-12Per Hedbor 
de9ca82000-11-27Per Hedbor  defvar("check_auth", 1, LOCALE(31,"Require authentication for modification"),
b1fca01996-11-12Per Hedbor  TYPE_FLAG,
de9ca82000-11-27Per Hedbor  LOCALE(32,"Only allow users authenticated by a authentication module "
79ca872000-11-24Per Hedbor  "to use methods that can modify the files, such as PUT or " "DELETE. If this is not set the file system will be a " "<b>very</b> public one since anyone will be able to edit " "files."));
14179b1997-01-29Per Hedbor 
de9ca82000-11-27Per Hedbor  defvar("stat_cache", 0, LOCALE(33,"Cache the results of stat(2)"),
79ca872000-11-24Per Hedbor  TYPE_FLAG|VAR_MORE,
de9ca82000-11-27Per Hedbor  LOCALE(34,"A performace option that can speed up retrieval of files "
79ca872000-11-24Per Hedbor  "from NFS with up to 50%. In turn it uses some memory and the " "file system will not notice that files have changed unless " "it gets a pragma no-cache request (produced e.g. by " "Alt-Ctrl-Reload in Netscape). Therefore this option should " "not be used on file systems that change a lot."));
de9ca82000-11-27Per Hedbor  defvar("access_as_user", 0, LOCALE(35,"Access file as the logged in user"),
9b9f701997-08-12Per Hedbor  TYPE_FLAG|VAR_MORE,
de9ca82000-11-27Per Hedbor  LOCALE(36,"If set, the module will access files as the authenticated "
79ca872000-11-24Per Hedbor  "user. This assumes that a authentication module which imports" " the users from the operating systems, such as the <i>User " "database</i> module is used. This option is very useful for " "named FTP sites, but it will have severe performance impacts " "since all threads will be locked for each access."));
de9ca82000-11-27Per Hedbor  defvar("no_symlinks", 0, LOCALE(37,"Forbid access to symlinks"),
9b9f701997-08-12Per Hedbor  TYPE_FLAG|VAR_MORE,
de9ca82000-11-27Per Hedbor  LOCALE(38,"It set, the file system will not follow symbolic links. "
79ca872000-11-24Per Hedbor  "This option can lower performace by a lot." ));
de9ca82000-11-27Per Hedbor  defvar("charset", "iso-8859-1", LOCALE(39,"File contents charset"),
79ca872000-11-24Per Hedbor  TYPE_STRING,
de9ca82000-11-27Per Hedbor  LOCALE(40,"The charset of the contents of the files on this file "
79ca872000-11-24Per Hedbor  "system. This variable makes it possible for Roxen to use " "any text file, no matter what charset it is written in. If" " necessary, Roxen will convert the file to Unicode before " "processing the file."));
de9ca82000-11-27Per Hedbor  defvar("path_encoding", "iso-8859-1", LOCALE(41,"Filename charset"),
79ca872000-11-24Per Hedbor  TYPE_STRING,
de9ca82000-11-27Per Hedbor  LOCALE(42,"The charset of the file names of the files on this file "
79ca872000-11-24Per Hedbor  "system. Unlike the <i>File contents charset</i> variable, " "this might not work for all charsets simply because not " "all browsers support anything except ISO-8859-1 " "in URLs."));
de9ca82000-11-27Per Hedbor  defvar("internal_files", ({}), LOCALE(43,"Internal files"), TYPE_STRING_LIST, LOCALE(44,"A list of glob patterns that matches files which should be "
79ca872000-11-24Per Hedbor  "considered internal. Internal files cannot be requested " "directly from a browser, won't show up in directory listings " "and can never be uploaded, moved or deleted by a browser." "They can only be accessed internally, e.g. with the RXML tags" " <tt>&lt;insert&gt;</tt> and <tt>&lt;use&gt;</tt>."));
54b4802000-03-07Martin Stjernholm }
c7ec632001-08-15Henrik Grubbström (Grubba) string path, mountpoint, charset, path_encoding, normalized_path;
a2f3c72000-08-13Per Hedbor int stat_cache, dotfiles, access_as_user, no_symlinks, tilde; array(string) internal_files;
b1fca01996-11-12Per Hedbor  void start() {
ed540b2001-01-13Martin Nilsson  tilde = query("tilde"); charset = query("charset"); path_encoding = query("path_encoding"); no_symlinks = query("no_symlinks"); access_as_user = query("access_as_user"); dotfiles = query(".files"); path = query("searchpath"); mountpoint = query("mountpoint"); stat_cache = query("stat_cache"); internal_files = query("internal_files");
c7ec632001-08-15Henrik Grubbström (Grubba) #if constant(system.normalize_path) if (catch { if ((<'/','\\'>)[path[-1]]) { normalized_path = system.normalize_path(path + "."); } else { normalized_path = system.normalize_path(path); } #ifdef __NT__ normalized_path += "\\"; #else /* !__NT__ */ normalized_path += "/"; #endif /* __NT__ */ }) {
191ff32001-08-24Per Hedbor  report_error(LOCALE(1, "Path verification of %s failed.\n"), mountpoint);
c7ec632001-08-15Henrik Grubbström (Grubba)  normalized_path = path; } #else /* !constant(system.normalize_path) */ normalized_path = path; #endif /* constant(system.normalize_path) */
ed540b2001-01-13Martin Nilsson  FILESYSTEM_WERR("Online at "+query("mountpoint")+" (path="+path+")");
8d10df2000-01-06Martin Stjernholm  cache_expire("stat_cache");
b1fca01996-11-12Per Hedbor } string query_location() {
a2f3c72000-08-13Per Hedbor  return mountpoint;
b1fca01996-11-12Per Hedbor }
54b4802000-03-07Martin Stjernholm #define FILTER_INTERNAL_FILE(f, id) \
a2f3c72000-08-13Per Hedbor  (!id->misc->internal_get && sizeof (filter (internal_files, glob, (f/"/")[-1])))
54b4802000-03-07Martin Stjernholm  mixed stat_file( string f, RequestID id )
b1fca01996-11-12Per Hedbor {
1f4a6c2000-08-28Per Hedbor  Stat fs;
54b4802000-03-07Martin Stjernholm 
cdf0532000-06-23Martin Stjernholm  FILESYSTEM_WERR("stat_file for \""+f+"\"" + (id->misc->internal_get ? " (internal)" : ""));
54b4802000-03-07Martin Stjernholm 
2c0a3e2001-08-13Per Hedbor  f = path+f;
ae46202001-06-27Per Hedbor  if (FILTER_INTERNAL_FILE (f, id)) return 0;
54b4802000-03-07Martin Stjernholm 
64c51c1997-10-11Henrik Grubbström (Grubba)  if(stat_cache && !id->pragma["no-cache"] &&
2c0a3e2001-08-13Per Hedbor  (fs=cache_lookup("stat_cache",f)))
1b8f6b1998-02-24Per Hedbor  return fs[0]; object privs;
a2f3c72000-08-13Per Hedbor  if (access_as_user && ((int)id->misc->uid) && ((int)id->misc->gid))
0216e21997-08-12Henrik Grubbström (Grubba)  // NB: Root-access is prevented.
f7d9811997-09-12Per Hedbor  privs=Privs("Statting file", (int)id->misc->uid, (int)id->misc->gid );
8afc811998-02-04Per Hedbor 
ec0dab2000-04-03Per Hedbor  /* No security currently in this function */
2c0a3e2001-08-13Per Hedbor  fs = file_stat(decode_path(f));
64c51c1997-10-11Henrik Grubbström (Grubba)  privs = 0;
a2f3c72000-08-13Per Hedbor  if(!stat_cache) return fs;
2c0a3e2001-08-13Per Hedbor  cache_set("stat_cache", f, ({fs}));
14179b1997-01-29Per Hedbor  return fs;
b1fca01996-11-12Per Hedbor }
54b4802000-03-07Martin Stjernholm string real_file( string f, RequestID id )
b1fca01996-11-12Per Hedbor {
c7ec632001-08-15Henrik Grubbström (Grubba)  if(stat_file( f, id )) { catch { return NORMALIZE_PATH(decode_path(path + f)); }; }
b1fca01996-11-12Per Hedbor }
54b4802000-03-07Martin Stjernholm int dir_filter_function(string f, RequestID id)
b1fca01996-11-12Per Hedbor {
a2f3c72000-08-13Per Hedbor  if(f[0]=='.' && !dotfiles) return 0; if(!tilde && Roxen.backup_extension(f)) return 0;
b1fca01996-11-12Per Hedbor  return 1; }
c4908f2000-05-06Martin Nilsson array(string) list_lock_files() {
ed540b2001-01-13Martin Nilsson  return query("nobrowse");
c4908f2000-05-06Martin Nilsson }
54b4802000-03-07Martin Stjernholm array find_dir( string f, RequestID id )
b1fca01996-11-12Per Hedbor { array dir;
cdf0532000-06-23Martin Stjernholm  FILESYSTEM_WERR("find_dir for \""+f+"\"" + (id->misc->internal_get ? " (internal)" : ""));
54b4802000-03-07Martin Stjernholm 
0216e21997-08-12Henrik Grubbström (Grubba)  object privs;
a2f3c72000-08-13Per Hedbor  if (((int)id->misc->uid) && ((int)id->misc->gid) && access_as_user )
0216e21997-08-12Henrik Grubbström (Grubba)  // NB: Root-access is prevented.
f7d9811997-09-12Per Hedbor  privs=Privs("Getting dir", (int)id->misc->uid, (int)id->misc->gid );
0216e21997-08-12Henrik Grubbström (Grubba) 
c7ec632001-08-15Henrik Grubbström (Grubba)  if (catch { f = NORMALIZE_PATH(decode_path(path + f)); } || !(dir = get_dir(f))) {
64c51c1997-10-11Henrik Grubbström (Grubba)  privs = 0;
b1fca01996-11-12Per Hedbor  return 0;
64c51c1997-10-11Henrik Grubbström (Grubba)  }
0216e21997-08-12Henrik Grubbström (Grubba)  privs = 0;
ed540b2001-01-13Martin Nilsson  if(!query("dir"))
b1fca01996-11-12Per Hedbor  // Access to this dir is allowed.
c4908f2000-05-06Martin Nilsson  if(! has_value(dir, ".www_browsable"))
b1fca01996-11-12Per Hedbor  { errors++; return 0; } // Access to this dir is not allowed.
ed540b2001-01-13Martin Nilsson  if( sizeof(dir & query("nobrowse")) )
b1fca01996-11-12Per Hedbor  { errors++; return 0; } dirlists++; // Pass _all_ files, hide none.
a2f3c72000-08-13Per Hedbor  if(tilde && dotfiles && (!sizeof( internal_files ) || id->misc->internal_get))
b1fca01996-11-12Per Hedbor  return dir;
3bf15e2000-03-08Martin Stjernholm  dir = Array.filter(dir, dir_filter_function, id); if (!id->misc->internal_get)
a2f3c72000-08-13Per Hedbor  foreach (internal_files, string globstr)
3bf15e2000-03-08Martin Stjernholm  dir -= glob (globstr, dir); return dir;
b1fca01996-11-12Per Hedbor } mapping putting = ([]);
2f83081999-05-05Henrik Grubbström (Grubba) void done_with_put( array(object|string) id_arr )
b1fca01996-11-12Per Hedbor {
b3ef2d1999-12-18Martin Nilsson // werror("Done with put.\n");
2f83081999-05-05Henrik Grubbström (Grubba)  object to; object from; object id; string oldf; [to, from, id, oldf] = id_arr;
ac69b51999-12-28Martin Nilsson  FILESYSTEM_WERR(sprintf("done_with_put(%O)\n" "from: %O\n", id_arr, mkmapping(indices(from), values(from))));
1358d51999-05-06Henrik Grubbström (Grubba) 
2f83081999-05-05Henrik Grubbström (Grubba)  to->close(); from->set_blocking(); m_delete(putting, from); if (putting[from] && (putting[from] != 0x7fffffff)) {
0163161999-04-21Henrik Grubbström (Grubba)  // Truncated!
2f83081999-05-05Henrik Grubbström (Grubba)  id->send_result(http_low_answer(400, "<h2>Bad Request - " "Expected more data.</h2>"));
0163161999-04-21Henrik Grubbström (Grubba)  } else {
2f83081999-05-05Henrik Grubbström (Grubba)  id->send_result(http_low_answer(200, "<h2>Transfer Complete.</h2>"));
0163161999-04-21Henrik Grubbström (Grubba)  }
b1fca01996-11-12Per Hedbor }
2f83081999-05-05Henrik Grubbström (Grubba) void got_put_data( array (object|string) id_arr, string data )
b1fca01996-11-12Per Hedbor {
b3ef2d1999-12-18Martin Nilsson // werror(strlen(data)+" .. ");
2f83081999-05-05Henrik Grubbström (Grubba)  object to; object from; object id; string oldf; [to, from, id, oldf] = id_arr;
0163161999-04-21Henrik Grubbström (Grubba)  // Truncate at end.
2f83081999-05-05Henrik Grubbström (Grubba)  data = data[..putting[from]]; if (id->misc->quota_obj && !id->misc->quota_obj->check_quota(oldf, sizeof(data))) { to->close(); from->set_blocking(); m_delete(putting, from);
1358d51999-05-06Henrik Grubbström (Grubba)  id->send_result(http_low_answer(413, "<h2>Out of disk quota.</h2>", "413 Out of disk quota"));
2f83081999-05-05Henrik Grubbström (Grubba)  return; }
0163161999-04-21Henrik Grubbström (Grubba) 
2f83081999-05-05Henrik Grubbström (Grubba)  int bytes = to->write( data );
0163161999-04-21Henrik Grubbström (Grubba)  if (bytes < sizeof(data)) { // Out of disk!
2f83081999-05-05Henrik Grubbström (Grubba)  to->close(); from->set_blocking(); m_delete(putting, from); id->send_result(http_low_answer(413, "<h2>Disk full.</h2>")); return;
0163161999-04-21Henrik Grubbström (Grubba)  } else {
2f83081999-05-05Henrik Grubbström (Grubba)  if (id->misc->quota_obj && !id->misc->quota_obj->allocate(oldf, bytes)) { to->close(); from->set_blocking(); m_delete(putting, from);
1358d51999-05-06Henrik Grubbström (Grubba)  id->send_result(http_low_answer(413, "<h2>Out of disk quota.</h2>", "413 Out of disk quota"));
2f83081999-05-05Henrik Grubbström (Grubba)  return; } if (putting[from] != 0x7fffffff) { putting[from] -= bytes;
32b4ba1999-04-21Henrik Grubbström (Grubba)  }
2f83081999-05-05Henrik Grubbström (Grubba)  if(putting[from] <= 0) { putting[from] = 0; // Paranoia done_with_put( id_arr );
0163161999-04-21Henrik Grubbström (Grubba)  } }
b1fca01996-11-12Per Hedbor }
ec0dab2000-04-03Per Hedbor string decode_path( string p ) {
a2f3c72000-08-13Per Hedbor  if( path_encoding != "iso-8859-1" ) p = Locale.Charset.encoder( path_encoding )->feed( p )->drain();
ec0dab2000-04-03Per Hedbor #ifndef __NT__ if( String.width( p ) != 8 ) p = string_to_utf8( p );
3f286a2000-08-23Per Hedbor #else while( strlen(p) && p[-1] == '/' ) p = p[..strlen(p)-2];
ec0dab2000-04-03Per Hedbor #endif return p; }
c9e0362000-08-28Johan Sundström int _file_size(string X, RequestID id)
14179b1997-01-29Per Hedbor {
1f4a6c2000-08-28Per Hedbor  Stat fs;
a2f3c72000-08-13Per Hedbor  if( stat_cache )
14179b1997-01-29Per Hedbor  {
c9e0362000-08-28Johan Sundström  array(Stat) cached_fs; if(!id->pragma["no-cache"] && (cached_fs = cache_lookup("stat_cache", X)))
a2f3c72000-08-13Per Hedbor  {
c9e0362000-08-28Johan Sundström  id->misc->stat = cached_fs[0]; return cached_fs[0] ? cached_fs[0][ST_SIZE] : -1;
a2f3c72000-08-13Per Hedbor  }
14179b1997-01-29Per Hedbor  }
ec0dab2000-04-03Per Hedbor  if(fs = file_stat(decode_path(X)))
14179b1997-01-29Per Hedbor  { id->misc->stat = fs;
a2f3c72000-08-13Per Hedbor  if( stat_cache ) cache_set("stat_cache",(X),({fs}));
14179b1997-01-29Per Hedbor  return fs[ST_SIZE];
a2f3c72000-08-13Per Hedbor  } else if( stat_cache )
1b8f6b1998-02-24Per Hedbor  cache_set("stat_cache",(X),({0}));
14179b1997-01-29Per Hedbor  return -1; }
afb1581997-07-06Henrik Grubbström (Grubba) int contains_symlinks(string root, string path) { array arr = path/"/";
1f4a6c2000-08-28Per Hedbor  Stat rr;
afb1581997-07-06Henrik Grubbström (Grubba)  foreach(arr - ({ "" }), path) { root += "/" + path;
1f4a6c2000-08-28Per Hedbor  if (rr = file_stat(decode_path(root), 1)) { if (rr[1] == -3) {
afb1581997-07-06Henrik Grubbström (Grubba)  return(1); } } else { return(0); } } return(0); }
54b4802000-03-07Martin Stjernholm mixed find_file( string f, RequestID id )
b1fca01996-11-12Per Hedbor {
8afb501998-04-15Henrik Grubbström (Grubba)  TRACE_ENTER("find_file(\""+f+"\")", 0);
b1fca01996-11-12Per Hedbor  object o; int size; string tmp;
afb1581997-07-06Henrik Grubbström (Grubba)  string oldf = f;
01128d1998-04-27Henrik Grubbström (Grubba) 
cdf0532000-06-23Martin Stjernholm  FILESYSTEM_WERR("Request for \""+f+"\"" + (id->misc->internal_get ? " (internal)" : ""));
01128d1998-04-27Henrik Grubbström (Grubba) 
a2f3c72000-08-13Per Hedbor  /* only used for the quota system, thus rather unessesary to do for each request.... */
c7ec632001-08-15Henrik Grubbström (Grubba) #define URI combine_path(mountpoint + "/" + oldf, ".")
1358d51999-05-06Henrik Grubbström (Grubba) 
c7ec632001-08-15Henrik Grubbström (Grubba)  string norm_f; catch { /* NOTE: NORMALIZE_PATH() may throw errors. */ f = norm_f = NORMALIZE_PATH(f = decode_path(path + f)); #if constant(system.normalize_path)
bd25082001-08-16Henrik Grubbström (Grubba)  if (!has_prefix(norm_f, normalized_path) &&
b0f68e2001-08-16Henrik Grubbström (Grubba) #ifdef __NT__
3282772001-08-16Henrik Grubbström (Grubba)  (norm_f+"\\" != normalized_path)
b0f68e2001-08-16Henrik Grubbström (Grubba) #else /* !__NT__ */ (norm_f+"/" != normalized_path) #endif /* __NT__ */ ) {
c7ec632001-08-15Henrik Grubbström (Grubba)  errors++;
191ff32001-08-24Per Hedbor  report_error(LOCALE(52, "Path verification of %O failed:\n"
b0f68e2001-08-16Henrik Grubbström (Grubba)  "%O is not a prefix of %O\n" ), oldf, normalized_path, norm_f);
c7ec632001-08-15Henrik Grubbström (Grubba)  TRACE_LEAVE(""); TRACE_LEAVE("Permission denied."); return http_low_answer(403, "<h2>File exists, but access forbidden " "by user</h2>"); }
b0f68e2001-08-16Henrik Grubbström (Grubba)  /* Adjust not_query */
c7ec632001-08-15Henrik Grubbström (Grubba)  id->not_query = mountpoint + replace(norm_f[sizeof(normalized_path)..], "\\", "/");
bd25082001-08-16Henrik Grubbström (Grubba)  if (sizeof(oldf) && (oldf[-1] == '/')) { id->not_query += "/"; }
c7ec632001-08-15Henrik Grubbström (Grubba) #endif /* constant(system.normalize_path) */ };
a2f3c72000-08-13Per Hedbor  size = _file_size( f, id );
b1fca01996-11-12Per Hedbor 
465eb61998-08-26Henrik Grubbström (Grubba)  /* * FIXME: Should probably move path-info extraction here. * /grubba 1998-08-26 */
b04ec82000-01-31Per Hedbor 
b1fca01996-11-12Per Hedbor  switch(id->method) { case "GET": case "HEAD": case "POST":
b04ec82000-01-31Per Hedbor 
b1fca01996-11-12Per Hedbor  switch(-size) { case 1:
8afb501998-04-15Henrik Grubbström (Grubba)  case 3: case 4: TRACE_LEAVE("No file");
b1fca01996-11-12Per Hedbor  return 0; /* Is no-file */ case 2:
8afb501998-04-15Henrik Grubbström (Grubba)  TRACE_LEAVE("Is directory");
b1fca01996-11-12Per Hedbor  return -1; /* Is dir */ default:
c7ec632001-08-15Henrik Grubbström (Grubba)  if( oldf[ -1 ] == '/' || /* Trying to access file with '/' appended */ !norm_f) { /* Or a file that is not normalizable. */
b04ec82000-01-31Per Hedbor  return 0;
c7ec632001-08-15Henrik Grubbström (Grubba)  }
b1fca01996-11-12Per Hedbor 
a2f3c72000-08-13Per Hedbor  if(!id->misc->internal_get) { if (!dotfiles
0fb1b12000-03-26Martin Stjernholm  && sizeof (tmp = (id->not_query/"/")[-1])
a2f3c72000-08-13Per Hedbor  && tmp[0] == '.') {
54b4802000-03-07Martin Stjernholm  TRACE_LEAVE("Is .-file"); return 0; }
a2f3c72000-08-13Per Hedbor  if (FILTER_INTERNAL_FILE (f, id)) {
54b4802000-03-07Martin Stjernholm  TRACE_LEAVE ("Is internal file"); return 0; }
8afb501998-04-15Henrik Grubbström (Grubba)  }
5e5fd62000-03-07Martin Stjernholm  TRACE_ENTER("Opening file \"" + f + "\"", 0);
1b8f6b1998-02-24Per Hedbor  object privs;
a2f3c72000-08-13Per Hedbor  if (access_as_user && ((int)id->misc->uid) && ((int)id->misc->gid))
0216e21997-08-12Henrik Grubbström (Grubba)  // NB: Root-access is prevented.
f7d9811997-09-12Per Hedbor  privs=Privs("Getting file", (int)id->misc->uid, (int)id->misc->gid );
a2f3c72000-08-13Per Hedbor  o = Stdio.File( );
c7ec632001-08-15Henrik Grubbström (Grubba)  if(!o->open(norm_f, "r" )) o = 0;
2e7c231997-06-10Henrik Grubbström (Grubba)  privs = 0;
a2f3c72000-08-13Per Hedbor  if(!o || (no_symlinks && (contains_symlinks(path, oldf))))
b1fca01996-11-12Per Hedbor  { errors++;
de9ca82000-11-27Per Hedbor  report_error(LOCALE(45,"Open of %s failed. Permission denied.\n"),f);
b04ec82000-01-31Per Hedbor 
8afb501998-04-15Henrik Grubbström (Grubba)  TRACE_LEAVE(""); TRACE_LEAVE("Permission denied.");
b1fca01996-11-12Per Hedbor  return http_low_answer(403, "<h2>File exists, but access forbidden " "by user</h2>"); }
c7ec632001-08-15Henrik Grubbström (Grubba)  id->realfile = norm_f;
8afb501998-04-15Henrik Grubbström (Grubba)  TRACE_LEAVE("");
b1fca01996-11-12Per Hedbor  accesses++;
8afb501998-04-15Henrik Grubbström (Grubba)  TRACE_LEAVE("Normal return");
a2f3c72000-08-13Per Hedbor  if( charset != "iso-8859-1" ) { if( id->misc->set_output_charset ) id->misc->set_output_charset( charset, 2 ); id->misc->input_charset = charset; }
b1fca01996-11-12Per Hedbor  return o; } break;
b04ec82000-01-31Per Hedbor 
8764c01998-05-01Henrik Grubbström (Grubba)  case "MKDIR":
ed540b2001-01-13Martin Nilsson  if(!query("put"))
8764c01998-05-01Henrik Grubbström (Grubba)  { id->misc->error_code = 405; TRACE_LEAVE("MKDIR disallowed (since PUT is disallowed)"); return 0;
b04ec82000-01-31Per Hedbor  }
8764c01998-05-01Henrik Grubbström (Grubba) 
54b4802000-03-07Martin Stjernholm  if (FILTER_INTERNAL_FILE (f, id)) { id->misc->error_code = 405; TRACE_LEAVE("MKDIR disallowed (since the dir name matches internal file glob)"); return 0; }
c7ec632001-08-15Henrik Grubbström (Grubba)  if (size) { TRACE_LEAVE("MKDIR failed. Directory name already exists. "); return 0; }
0764862001-03-16Per Hedbor  if(query("check_auth") && (!id->conf->authenticate( id ) ) ) {
8764c01998-05-01Henrik Grubbström (Grubba)  TRACE_LEAVE("MKDIR: Permission denied");
f4b8bc2000-05-01Martin Nilsson  return Roxen.http_auth_required("foo",
8764c01998-05-01Henrik Grubbström (Grubba)  "<h1>Permission to 'MKDIR' denied</h1>"); } mkdirs++; object privs; if (((int)id->misc->uid) && ((int)id->misc->gid)) { // NB: Root-access is prevented. privs=Privs("Creating directory", (int)id->misc->uid, (int)id->misc->gid ); }
ed540b2001-01-13Martin Nilsson  if (query("no_symlinks") && (contains_symlinks(path, oldf))) {
8764c01998-05-01Henrik Grubbström (Grubba)  privs = 0; errors++;
c7ec632001-08-15Henrik Grubbström (Grubba)  report_error(LOCALE(46,"Creation of %s failed. Permission denied.\n"), oldf);
8764c01998-05-01Henrik Grubbström (Grubba)  TRACE_LEAVE("MKDIR: Contains symlinks. Permission denied"); return http_low_answer(403, "<h2>Permission denied.</h2>"); }
c7ec632001-08-15Henrik Grubbström (Grubba)  int code = mkdir(f);
8764c01998-05-01Henrik Grubbström (Grubba)  privs = 0;
5e5fd62000-03-07Martin Stjernholm  TRACE_ENTER("MKDIR: Accepted", 0);
8764c01998-05-01Henrik Grubbström (Grubba)  if (code) {
2422401998-06-04David Hedbor  chmod(f, 0777 & ~(id->misc->umask || 022));
8764c01998-05-01Henrik Grubbström (Grubba)  TRACE_LEAVE("MKDIR: Success"); TRACE_LEAVE("Success");
f4b8bc2000-05-01Martin Nilsson  return Roxen.http_string_answer("Ok");
8764c01998-05-01Henrik Grubbström (Grubba)  } else { TRACE_LEAVE("MKDIR: Failed"); TRACE_LEAVE("Failure"); return 0; } break;
b1fca01996-11-12Per Hedbor  case "PUT":
ed540b2001-01-13Martin Nilsson  if(!query("put"))
f6d62d1997-03-26Per Hedbor  { id->misc->error_code = 405;
8afb501998-04-15Henrik Grubbström (Grubba)  TRACE_LEAVE("PUT disallowed");
b1fca01996-11-12Per Hedbor  return 0;
b04ec82000-01-31Per Hedbor  }
f6d62d1997-03-26Per Hedbor 
54b4802000-03-07Martin Stjernholm  if (FILTER_INTERNAL_FILE (f, id)) { id->misc->error_code = 405; TRACE_LEAVE("PUT of internal file is disallowed"); return 0; }
0764862001-03-16Per Hedbor  if(query("check_auth") && (!id->conf->authenticate( id ) ) ) {
8afb501998-04-15Henrik Grubbström (Grubba)  TRACE_LEAVE("PUT: Permission denied");
f4b8bc2000-05-01Martin Nilsson  return Roxen.http_auth_required("foo",
f6d62d1997-03-26Per Hedbor  "<h1>Permission to 'PUT' files denied</h1>");
8afb501998-04-15Henrik Grubbström (Grubba)  }
2f83081999-05-05Henrik Grubbström (Grubba) 
b1fca01996-11-12Per Hedbor  puts++;
2f83081999-05-05Henrik Grubbström (Grubba) 
ac69b51999-12-28Martin Nilsson  QUOTA_WERR("Checking quota.\n");
1358d51999-05-06Henrik Grubbström (Grubba)  if (id->misc->quota_obj && (id->misc->len > 0) &&
3f286a2000-08-23Per Hedbor  !id->misc->quota_obj->check_quota(URI, id->misc->len)) {
2f83081999-05-05Henrik Grubbström (Grubba)  errors++;
de9ca82000-11-27Per Hedbor  report_warning(LOCALE(47,"Creation of %s failed. Out of quota.\n"),f);
2f83081999-05-05Henrik Grubbström (Grubba)  TRACE_LEAVE("PUT: Out of quota.");
1358d51999-05-06Henrik Grubbström (Grubba)  return http_low_answer(413, "<h2>Out of disk quota.</h2>", "413 Out of disk quota");
2f83081999-05-05Henrik Grubbström (Grubba)  }
b04ec82000-01-31Per Hedbor 
f6d62d1997-03-26Per Hedbor 
2e7c231997-06-10Henrik Grubbström (Grubba)  if (((int)id->misc->uid) && ((int)id->misc->gid)) {
0216e21997-08-12Henrik Grubbström (Grubba)  // NB: Root-access is prevented.
f7d9811997-09-12Per Hedbor  privs=Privs("Saving file", (int)id->misc->uid, (int)id->misc->gid );
2e7c231997-06-10Henrik Grubbström (Grubba)  }
f6d62d1997-03-26Per Hedbor 
ed540b2001-01-13Martin Nilsson  if (query("no_symlinks") && (contains_symlinks(path, oldf))) {
64c51c1997-10-11Henrik Grubbström (Grubba)  privs = 0;
afb1581997-07-06Henrik Grubbström (Grubba)  errors++;
de9ca82000-11-27Per Hedbor  report_error(LOCALE(46,"Creation of %s failed. Permission denied.\n"),f);
8afb501998-04-15Henrik Grubbström (Grubba)  TRACE_LEAVE("PUT: Contains symlinks. Permission denied");
afb1581997-07-06Henrik Grubbström (Grubba)  return http_low_answer(403, "<h2>Permission denied.</h2>"); }
c7ec632001-08-15Henrik Grubbström (Grubba)  rm(f); mkdirhier(f);
8afb501998-04-15Henrik Grubbström (Grubba) 
2f83081999-05-05Henrik Grubbström (Grubba)  if (id->misc->quota_obj) {
c7ec632001-08-15Henrik Grubbström (Grubba)  QUOTA_WERR("Checking if the file already existed.");
2f83081999-05-05Henrik Grubbström (Grubba)  if (size > 0) {
ac69b51999-12-28Martin Nilsson  QUOTA_WERR("Deallocating " + size + "bytes.");
3f286a2000-08-23Per Hedbor  id->misc->quota_obj->deallocate(URI, size);
2f83081999-05-05Henrik Grubbström (Grubba)  } }
c7ec632001-08-15Henrik Grubbström (Grubba)  object to = open(f, "wct");
5e5fd62000-03-07Martin Stjernholm  privs = 0; TRACE_ENTER("PUT: Accepted", 0);
8afb501998-04-15Henrik Grubbström (Grubba)  /* Clear the stat-cache for this file */ if (stat_cache) { cache_set("stat_cache", f, 0); }
b1fca01996-11-12Per Hedbor  if(!to)
f6d62d1997-03-26Per Hedbor  { id->misc->error_code = 403;
8afb501998-04-15Henrik Grubbström (Grubba)  TRACE_LEAVE("PUT: Open failed"); TRACE_LEAVE("Failure");
f6d62d1997-03-26Per Hedbor  return 0; }
0163161999-04-21Henrik Grubbström (Grubba)  // FIXME: Race-condition.
c7ec632001-08-15Henrik Grubbström (Grubba)  chmod(f, 0666 & ~(id->misc->umask || 022));
0163161999-04-21Henrik Grubbström (Grubba) 
2f83081999-05-05Henrik Grubbström (Grubba)  putting[id->my_fd] = id->misc->len;
b1fca01996-11-12Per Hedbor  if(id->data && strlen(id->data)) {
2f83081999-05-05Henrik Grubbström (Grubba)  // FIXME: What if sizeof(id->data) > id->misc->len ? if (id->misc->len > 0) { putting[id->my_fd] -= strlen(id->data); } int bytes = to->write( id->data ); if (id->misc->quota_obj) {
ac69b51999-12-28Martin Nilsson  QUOTA_WERR("Allocating " + bytes + "bytes.");
2f83081999-05-05Henrik Grubbström (Grubba)  if (!id->misc->quota_obj->allocate(f, bytes)) { TRACE_LEAVE("PUT: A string"); TRACE_LEAVE("PUT: Out of quota");
1358d51999-05-06Henrik Grubbström (Grubba)  return http_low_answer(413, "<h2>Out of disk quota.</h2>", "413 Out of disk quota");
2f83081999-05-05Henrik Grubbström (Grubba)  } }
b1fca01996-11-12Per Hedbor  }
8afb501998-04-15Henrik Grubbström (Grubba)  if(!putting[id->my_fd]) { TRACE_LEAVE("PUT: Just a string"); TRACE_LEAVE("Put: Success");
f4b8bc2000-05-01Martin Nilsson  return Roxen.http_string_answer("Ok");
8afb501998-04-15Henrik Grubbström (Grubba)  }
b1fca01996-11-12Per Hedbor 
a2f3461997-06-12Henrik Grubbström (Grubba)  if(id->clientprot == "HTTP/1.1") {
b1fca01996-11-12Per Hedbor  id->my_fd->write("HTTP/1.1 100 Continue\r\n");
a2f3461997-06-12Henrik Grubbström (Grubba)  }
3f286a2000-08-23Per Hedbor  id->my_fd->set_id( ({ to, id->my_fd, id, URI }) );
b1fca01996-11-12Per Hedbor  id->my_fd->set_nonblocking(got_put_data, 0, done_with_put);
8afb501998-04-15Henrik Grubbström (Grubba)  TRACE_LEAVE("PUT: Pipe in progress"); TRACE_LEAVE("PUT: Success so far");
f4b8bc2000-05-01Martin Nilsson  return Roxen.http_pipe_in_progress();
b1fca01996-11-12Per Hedbor  break;
6cec001998-05-14David Hedbor  case "CHMOD":
b04ec82000-01-31Per Hedbor  // Change permission of a file.
1358d51999-05-06Henrik Grubbström (Grubba)  // FIXME: !!
b04ec82000-01-31Per Hedbor 
ed540b2001-01-13Martin Nilsson  if(!query("put"))
6cec001998-05-14David Hedbor  { id->misc->error_code = 405; TRACE_LEAVE("CHMOD disallowed (since PUT is disallowed)"); return 0;
b04ec82000-01-31Per Hedbor  }
6cec001998-05-14David Hedbor 
54b4802000-03-07Martin Stjernholm  if (FILTER_INTERNAL_FILE (f, id)) { id->misc->error_code = 405; TRACE_LEAVE("CHMOD of internal file is disallowed"); return 0; }
0764862001-03-16Per Hedbor  if(query("check_auth") && (!id->conf->authenticate( id ) ) ) {
6cec001998-05-14David Hedbor  TRACE_LEAVE("CHMOD: Permission denied");
f4b8bc2000-05-01Martin Nilsson  return Roxen.http_auth_required("foo",
6cec001998-05-14David Hedbor  "<h1>Permission to 'CHMOD' files denied</h1>"); }
b04ec82000-01-31Per Hedbor 
6cec001998-05-14David Hedbor  if (((int)id->misc->uid) && ((int)id->misc->gid)) { // NB: Root-access is prevented. privs=Privs("CHMODing file", (int)id->misc->uid, (int)id->misc->gid ); }
b04ec82000-01-31Per Hedbor 
ed540b2001-01-13Martin Nilsson  if (query("no_symlinks") && (contains_symlinks(path, oldf))) {
6cec001998-05-14David Hedbor  privs = 0; errors++; TRACE_LEAVE("CHMOD: Contains symlinks. Permission denied"); return http_low_answer(403, "<h2>Permission denied.</h2>"); }
c7ec632001-08-15Henrik Grubbström (Grubba)  array err = catch(chmod(f, id->misc->mode & 0777));
5e5fd62000-03-07Martin Stjernholm  privs = 0;
6cec001998-05-14David Hedbor  chmods++; TRACE_ENTER("CHMOD: Accepted", 0); if (stat_cache) { cache_set("stat_cache", f, 0); }
b04ec82000-01-31Per Hedbor 
6cec001998-05-14David Hedbor  if(err) { id->misc->error_code = 403; TRACE_LEAVE("CHMOD: Failure"); TRACE_LEAVE("Failure"); return 0; } TRACE_LEAVE("CHMOD: Success"); TRACE_LEAVE("Success");
f4b8bc2000-05-01Martin Nilsson  return Roxen.http_string_answer("Ok");
b04ec82000-01-31Per Hedbor 
dba2b51998-05-14David Hedbor  case "MV":
b04ec82000-01-31Per Hedbor  // This little kluge is used by ftp2 to move files.
2f83081999-05-05Henrik Grubbström (Grubba)  // FIXME: Support for quota.
ed540b2001-01-13Martin Nilsson  if(!query("put"))
dba2b51998-05-14David Hedbor  { id->misc->error_code = 405; TRACE_LEAVE("MV disallowed (since PUT is disallowed)"); return 0;
b04ec82000-01-31Per Hedbor  }
ed540b2001-01-13Martin Nilsson  if(!query("delete") && size != -1)
dba2b51998-05-14David Hedbor  { id->misc->error_code = 405; TRACE_LEAVE("MV disallowed (DELE disabled, can't overwrite file)"); return 0; }
f37e471998-05-14Henrik Grubbström (Grubba)  if(size < -1)
dba2b51998-05-14David Hedbor  { id->misc->error_code = 405; TRACE_LEAVE("MV: Cannot overwrite directory"); return 0; }
0764862001-03-16Per Hedbor  if(query("check_auth") && (!id->conf->authenticate( id ) ) ) {
dba2b51998-05-14David Hedbor  TRACE_LEAVE("MV: Permission denied");
f4b8bc2000-05-01Martin Nilsson  return Roxen.http_auth_required("foo",
dba2b51998-05-14David Hedbor  "<h1>Permission to 'MV' files denied</h1>"); } string movefrom; if(!id->misc->move_from || !(movefrom = id->conf->real_file(id->misc->move_from, id))) { id->misc->error_code = 405; errors++; TRACE_LEAVE("MV: No source file"); return 0; }
54b4802000-03-07Martin Stjernholm  if (FILTER_INTERNAL_FILE (movefrom, id) || FILTER_INTERNAL_FILE (f, id)) { id->misc->error_code = 405; TRACE_LEAVE("MV to or from internal file is disallowed"); return 0; }
dba2b51998-05-14David Hedbor  if (((int)id->misc->uid) && ((int)id->misc->gid)) { // NB: Root-access is prevented. privs=Privs("Moving file", (int)id->misc->uid, (int)id->misc->gid ); }
b04ec82000-01-31Per Hedbor 
ed540b2001-01-13Martin Nilsson  if (query("no_symlinks") &&
f37e471998-05-14Henrik Grubbström (Grubba)  ((contains_symlinks(path, oldf)) || (contains_symlinks(path, id->misc->move_from)))) {
dba2b51998-05-14David Hedbor  privs = 0; errors++; TRACE_LEAVE("MV: Contains symlinks. Permission denied"); return http_low_answer(403, "<h2>Permission denied.</h2>"); }
c7ec632001-08-15Henrik Grubbström (Grubba)  code = mv(movefrom, f);
5e5fd62000-03-07Martin Stjernholm  privs = 0; moves++;
dba2b51998-05-14David Hedbor  TRACE_ENTER("MV: Accepted", 0); /* Clear the stat-cache for this file */ if (stat_cache) { cache_set("stat_cache", movefrom, 0); cache_set("stat_cache", f, 0); } if(!code) { id->misc->error_code = 403; TRACE_LEAVE("MV: Move failed"); TRACE_LEAVE("Failure"); return 0; } TRACE_LEAVE("MV: Success");
f37e471998-05-14Henrik Grubbström (Grubba)  TRACE_LEAVE("Success");
f4b8bc2000-05-01Martin Nilsson  return Roxen.http_string_answer("Ok");
de14f21998-10-19 Wim Bonis  case "MOVE": // This little kluge is used by NETSCAPE 4.5
2f83081999-05-05Henrik Grubbström (Grubba)  // FIXME: Support for quota.
b04ec82000-01-31Per Hedbor 
ed540b2001-01-13Martin Nilsson  if(!query("put"))
de14f21998-10-19 Wim Bonis  { id->misc->error_code = 405; TRACE_LEAVE("MOVE disallowed (since PUT is disallowed)"); return 0;
b04ec82000-01-31Per Hedbor  }
de14f21998-10-19 Wim Bonis  if(size != -1) { id->misc->error_code = 404; TRACE_LEAVE("MOVE failed (no such file)"); return 0; }
0764862001-03-16Per Hedbor  if(query("check_auth") && (!id->conf->authenticate( id ) ) ) {
de14f21998-10-19 Wim Bonis  TRACE_LEAVE("MOVE: Permission denied");
f4b8bc2000-05-01Martin Nilsson  return Roxen.http_auth_required("foo",
de14f21998-10-19 Wim Bonis  "<h1>Permission to 'MOVE' files denied</h1>"); }
b04ec82000-01-31Per Hedbor  if(!sizeof(id->misc["new-uri"] || "")) {
de14f21998-10-19 Wim Bonis  id->misc->error_code = 405; errors++; TRACE_LEAVE("MOVE: No dest file"); return 0; }
3f286a2000-08-23Per Hedbor  string new_uri = combine_path(URI + "/../",
1358d51999-05-06Henrik Grubbström (Grubba)  id->misc["new-uri"]);
de14f21998-10-19 Wim Bonis 
3a26d12000-12-29Henrik Grubbström (Grubba)  // FIXME: The code below doesn't allow for this module being overloaded.
1358d51999-05-06Henrik Grubbström (Grubba)  if (new_uri[..sizeof(mountpoint)-1] != mountpoint) {
de14f21998-10-19 Wim Bonis  id->misc->error_code = 405; TRACE_LEAVE("MOVE: Dest file on other filesystem."); return(0); }
1358d51999-05-06Henrik Grubbström (Grubba)  string moveto = path + "/" + new_uri[sizeof(mountpoint)..];
de14f21998-10-19 Wim Bonis 
54b4802000-03-07Martin Stjernholm  if (FILTER_INTERNAL_FILE (f, id) || FILTER_INTERNAL_FILE (moveto, id)) { id->misc->error_code = 405; TRACE_LEAVE("MOVE to or from internal file is disallowed"); return 0; }
a2f3c72000-08-13Per Hedbor  size = _file_size(moveto,id);
de14f21998-10-19 Wim Bonis 
ed540b2001-01-13Martin Nilsson  if(!query("delete") && size != -1)
de14f21998-10-19 Wim Bonis  { id->misc->error_code = 405; TRACE_LEAVE("MOVE disallowed (DELE disabled, can't overwrite file)"); return 0; }
b04ec82000-01-31Per Hedbor 
de14f21998-10-19 Wim Bonis  if(size < -1) { id->misc->error_code = 405; TRACE_LEAVE("MOVE: Cannot overwrite directory"); return 0; } if (((int)id->misc->uid) && ((int)id->misc->gid)) { // NB: Root-access is prevented. privs=Privs("Moving file", (int)id->misc->uid, (int)id->misc->gid ); }
ed540b2001-01-13Martin Nilsson  if (query("no_symlinks") &&
de14f21998-10-19 Wim Bonis  ((contains_symlinks(path, f)) || (contains_symlinks(path, moveto)))) { privs = 0; errors++; TRACE_LEAVE("MOVE: Contains symlinks. Permission denied"); return http_low_answer(403, "<h2>Permission denied.</h2>"); }
c7ec632001-08-15Henrik Grubbström (Grubba)  code = mv(f, decode_path(moveto));
5e5fd62000-03-07Martin Stjernholm  privs = 0;
de14f21998-10-19 Wim Bonis  TRACE_ENTER("MOVE: Accepted", 0); moves++; /* Clear the stat-cache for this file */ if (stat_cache) { cache_set("stat_cache", moveto, 0); cache_set("stat_cache", f, 0); } if(!code) { id->misc->error_code = 403; TRACE_LEAVE("MOVE: Move failed"); TRACE_LEAVE("Failure"); return 0; } TRACE_LEAVE("MOVE: Success"); TRACE_LEAVE("Success");
f4b8bc2000-05-01Martin Nilsson  return Roxen.http_string_answer("Ok");
de14f21998-10-19 Wim Bonis 
b04ec82000-01-31Per Hedbor 
b1fca01996-11-12Per Hedbor  case "DELETE":
ed540b2001-01-13Martin Nilsson  if(!query("delete") || size==-1)
f6d62d1997-03-26Per Hedbor  { id->misc->error_code = 405;
8afb501998-04-15Henrik Grubbström (Grubba)  TRACE_LEAVE("DELETE: Disabled");
b1fca01996-11-12Per Hedbor  return 0;
f6d62d1997-03-26Per Hedbor  }
54b4802000-03-07Martin Stjernholm  if (FILTER_INTERNAL_FILE (f, id)) { id->misc->error_code = 405; TRACE_LEAVE("DELETE of internal file is disallowed"); return 0; }
0764862001-03-16Per Hedbor  if(query("check_auth") && (!id->conf->authenticate( id ) ) ) {
8afb501998-04-15Henrik Grubbström (Grubba)  TRACE_LEAVE("DELETE: Permission denied");
b1fca01996-11-12Per Hedbor  return http_low_answer(403, "<h1>Permission to DELETE file denied</h1>");
8afb501998-04-15Henrik Grubbström (Grubba)  }
b1fca01996-11-12Per Hedbor 
ed540b2001-01-13Martin Nilsson  if (query("no_symlinks") && (contains_symlinks(path, oldf))) {
afb1581997-07-06Henrik Grubbström (Grubba)  errors++;
de9ca82000-11-27Per Hedbor  report_error(LOCALE(48,"Deletion of %s failed. Permission denied.\n"),f);
8afb501998-04-15Henrik Grubbström (Grubba)  TRACE_LEAVE("DELETE: Contains symlinks");
afb1581997-07-06Henrik Grubbström (Grubba)  return http_low_answer(403, "<h2>Permission denied.</h2>"); }
de9ca82000-11-27Per Hedbor  report_notice(LOCALE(49,"DELETING the file %s.\n"),f);
b1fca01996-11-12Per Hedbor  accesses++;
f6d62d1997-03-26Per Hedbor 
2e7c231997-06-10Henrik Grubbström (Grubba)  if (((int)id->misc->uid) && ((int)id->misc->gid)) {
0216e21997-08-12Henrik Grubbström (Grubba)  // NB: Root-access is prevented.
f7d9811997-09-12Per Hedbor  privs=Privs("Deleting file", id->misc->uid, id->misc->gid );
2e7c231997-06-10Henrik Grubbström (Grubba)  }
f6d62d1997-03-26Per Hedbor 
8afb501998-04-15Henrik Grubbström (Grubba)  /* Clear the stat-cache for this file */ if (stat_cache) { cache_set("stat_cache", f, 0); }
c7ec632001-08-15Henrik Grubbström (Grubba)  if(!rm(f))
f6d62d1997-03-26Per Hedbor  {
64c51c1997-10-11Henrik Grubbström (Grubba)  privs = 0;
f6d62d1997-03-26Per Hedbor  id->misc->error_code = 405;
8afb501998-04-15Henrik Grubbström (Grubba)  TRACE_LEAVE("DELETE: Failed");
f6d62d1997-03-26Per Hedbor  return 0; }
64c51c1997-10-11Henrik Grubbström (Grubba)  privs = 0;
f6d62d1997-03-26Per Hedbor  deletes++;
2f83081999-05-05Henrik Grubbström (Grubba) 
2ac4001999-05-05Henrik Grubbström (Grubba)  if (id->misc->quota_obj && (size > 0)) {
2f83081999-05-05Henrik Grubbström (Grubba)  id->misc->quota_obj->deallocate(oldf, size); }
8afb501998-04-15Henrik Grubbström (Grubba)  TRACE_LEAVE("DELETE: Success");
b1fca01996-11-12Per Hedbor  return http_low_answer(200,(f+" DELETED from the server")); default:
8afb501998-04-15Henrik Grubbström (Grubba)  TRACE_LEAVE("Not supported");
b1fca01996-11-12Per Hedbor  return 0; }
8afb501998-04-15Henrik Grubbström (Grubba)  TRACE_LEAVE("Not reached");
b1fca01996-11-12Per Hedbor  return 0; } string query_name() {
06225a2001-05-16Martin Nilsson  return sprintf((string)LOCALE(50,"%s from %s"), mountpoint, path);
b1fca01996-11-12Per Hedbor }