2 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
4 schsh = "/usr/local/bin/schsh"
6 chroots = "/var/lib/schsh"
9 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
10 # DO NOT TOUCH ANYTHING BELOW THIS LINE
12 import os, sys, subprocess, pwd, grp, shutil
15 print("Run this a root, please.")
20 chroot = os.path.join(chroots, name)
21 if os.path.exists(chroot):
22 raise Exception(chroot+" already exists, please remove it first")
23 userpw = pwd.getpwnam(name)
24 data = "/home/{0}/data".format(name)
26 # schroot configuration
27 with open("/etc/schroot/chroot.d/schsh-"+name, "w") as f:
33 setup.fstab=schsh/{0}.fstab
34 """.format(name, chroot), file=f)
35 with open("/etc/schroot/schsh/"+name+".fstab", "w") as f:
36 # no spaces, schroot does not like them
37 print("# <file system> <mount point> <type> <options> <dump> <pass>", file=f)
39 for folder in ("/lib", "/lib64", "/usr/bin", "/usr/lib", "/usr/lib64", "/usr/share/", "/usr/local/bin"):
40 if os.path.exists(folder):
41 print("{0}\t{0}\tnone\trw,bind\t0\t0".format(folder), file=f)
43 print("{0}\t/data\tnone\trw,bind\t0\t0".format(data), file=f)
45 # setup the schroot directory
47 for folder in ["etc", "dev", "data"]:
48 os.mkdir(os.path.join(chroot, folder))
50 # setup /etc/passwd and /etc/group
51 with open(os.path.join(chroot, "etc", "passwd"), "w") as f:
52 print("root:x:0:0:root:/root:/bin/bash", file=f)
53 print("{0}:x:{1}:{2}:,,,:/data:/bin/false".format(name, userpw.pw_uid, userpw.pw_gid), file=f)
54 with open(os.path.join(chroot, "etc", "group"), "w") as f:
55 print("root:x:0:", file=f)
56 usergrp = grp.getgrgid(userpw.pw_gid)
57 print("{0}:x:{1}:".format(usergrp.gr_name, usergrp.gr_gid), file=f)
59 groupgrp = grp.getgrnam(group)
60 assert usergrp.gr_gid != groupgrp.gr_gid
61 print("{0}:x:{1}:{2}".format(groupgrp.gr_name, groupgrp.gr_gid, name), file=f)
63 # Make sure ~/data (part of the fstab above) exists.
64 if not os.path.exists(data):
66 shutil.chown(data, name, name)
70 if userpw.pw_shell != schsh:
71 subprocess.check_output(["usermod", "--shell", schsh, name])
73 subprocess.check_output(["adduser", name, "schsh"])
77 if len(sys.argv) <= 1:
78 print("Usage: %s <usernames>" % sys.argv[0])
80 for name in sys.argv[1:]:
81 print("Setting up",name)