File: restrictedfs.pike

package info (click to toggle)
roxen 1.2beta2-2
  • links: PTS
  • area: contrib
  • in suites: hamm
  • size: 16,948 kB
  • ctags: 8,589
  • sloc: ansic: 89,632; asm: 8,431; sh: 2,915; makefile: 1,787; cpp: 377
file content (122 lines) | stat: -rw-r--r-- 2,832 bytes parent folder | download | duplicates (4)
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
/*
 * $Id: restrictedfs.pike,v 1.8 1998/02/10 18:36:13 per Exp $
 *
 * $Author: per $
 *
 * Implements a restricted filesystem.
 * This filesystem only allows accesses to files that are a prefix of
 * id->misc->home (ie the users home-directory).
 * Usable for eg ftp-servers allowing named ftp.
 *
 * Thanks to Zsolt Varga <redax@agria.hu> for the idea.
 */

inherit "filesystem";

constant cvs_version = "$Id: restrictedfs.pike,v 1.8 1998/02/10 18:36:13 per Exp $";

#include <module.h>
#include <roxen.h>

// import Array;

mixed *register_module()
{
  return ({ MODULE_LOCATION, "Restricted filesystem", 
	      "This is a restricted filesystem, use it to make users home "
	      "directories available to them if they login.<br>\n"
	      "Usable for eg ftp-servers."
	      });
}

void create()
{
  ::create();
  defvar("remap_home", 0, "Hide path to the home-directory",
	 TYPE_FLAG, "Hides the path to the homedirectory if enabled.<br>\n"
	 "E.g.<br>\n<ul>\n"
	 "If the user <i>foo</i> has the homedirectory <i>/home/foo</i> and "
	 "this is enabled, he will see his files in <b>/</b>.<br>\n"
	 "If this is not enabled, he would see them in <b>/home/foo</b>\n"
	 "</ul>\n");
}

mixed stat_file(string f, object id)
{
  string home = id->misc->home;
  if (!stringp(home)) {
    // No home-directory
    return(0);
  }
  if (QUERY(remap_home)) {
    if (home[0] == '/') {
      home = home[1..];
    }
    if (home[-1] != '/') {
      home += "/";
    }
    return(::stat_file(home + f, id));
  } else {
    if (search("/" + f, home)) {
      // Not a prefix, or short.
      if ((home[1..sizeof(f)] != f) ||
	  ((home[sizeof(f)] != '/') && (home[sizeof(f)+1] != '/'))) {
	return(0);
      }
      // Short.
    }
    return(::stat_file(f, id));
  }
}

array find_dir(string f, object id)
{
  string home = id->misc->home;
  if (!stringp(home)) {
    // No home-directory
    return(0);
  }
  if (QUERY(remap_home)) {
    if (home[0] == '/') {
      home = home[1..];
    }
    if (home[-1] != '/') {
      home += "/";
    }
    return(::find_dir(home + f, id));
  } else {
    if (search("/" + f, home)) {
      // Not a prefix, or short
      if (home[1..sizeof(f)] == f) {
	// Short - return the next part of the path.
	return(Array.filter(({ ".", "..", (home[sizeof(f)+1..]/"/")[0] }),
		      dir_filter_function));
      }
    }
    return(::find_dir(f, id));
  }
}

mixed find_file(string f, object id)
{
  string home = id->misc->home;
  if (!stringp(home)) {
    // No home-directory
    return(0);
  }
  if (QUERY(remap_home)) {
    if (home[0] == '/') {
      home = home[1..];
    }
    if (home[-1] != '/') {
      home += "/";
    }
    return(::find_file(home + f, id));
  } else {
    if (search("/" + f, home)) {
      // Not a prefix, or short.
      return(0);
    }
    return(::find_file(f, id));
  }
}