2
Usage: python share.py <root-dir> <owner> <cmd-json>
4
This utility can be used in conjunction with an Apache-based
5
WebDAV server to create .htaccess files with readability
6
permissions that are set by a privileged remote client.
8
<root-dir> is the local root directory where per-user
11
<owner> is the name of the user who is sharing a
12
directory. Their user directory is assumed to be in
15
<cmd-json> is JSON that contains information about the
16
share. It must have the following keys:
18
version: This must be 1.
20
directory: This is the directory to be shared, relative to
21
the owner's user directory.
23
share_to_users: This is a list of the users who should be
24
given access to read the directory. If it is
25
a list with "all" as its only element, then
26
the directory is readable by anyone.
28
If successful, the script displays nothing and exits with a
29
return code of 0. Otherwise, it displays an error and exits
30
with a nonzero return code.
37
def make_read_share_htaccess(owner, users):
39
read_require_perms = "valid-user"
41
users = set(users + [owner])
44
read_require_perms = "user %s" % " ".join(users)
47
"<Limit GET PROPFIND>",
48
"Require %s" % read_require_perms,
50
"<LimitExcept GET PROPFIND>",
51
"Require user %s" % owner,
55
return "\n".join(lines)
57
def write_htaccess(root_dir, owner, cmd, file_open = open):
58
htaccess = make_read_share_htaccess(owner,
59
cmd["share_to_users"])
60
user_root_dir = os.path.join(root_dir, owner)
61
path = os.path.join(user_root_dir, cmd["directory"], ".htaccess")
62
path = os.path.normpath(path)
63
if not path.startswith(user_root_dir):
64
raise Exception("Path doesn't start with user root dir: %s" % path)
65
file_open(path, "w").write(htaccess)
67
def write_htaccess_from_json(root_dir, owner, cmd_json,
68
write_htaccess = write_htaccess):
69
write_htaccess(root_dir, owner, json.read(cmd_json))
71
if __name__ == "__main__":
74
__main__ = __import__("__main__")
75
print __main__.__doc__
77
root_dir, owner, cmd_json = args
78
write_htaccess_from_json(root_dir, owner, cmd_json)