make sure data dir exists
[schsh.git] / makeschsh
1 #!/usr/bin/python3
2 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
3 # Configuration
4 schsh = "/usr/local/bin/schsh"
5 group = "schsh"
6 chroots = "/var/lib/schsh"
7
8 # END of Configuration
9 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
10 # DO NOT TOUCH ANYTHING BELOW THIS LINE
11
12 import os, sys, subprocess, pwd, grp, shutil
13
14 if os.getuid() != 0:
15         print("Run this a root, please.")
16         sys.exit(1)
17
18
19 def setup(name):
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)
25         
26         # schroot configuration
27         with open("/etc/schroot/chroot.d/schsh-"+name, "w") as f:
28                 print("""[schsh-{0}]
29 type=directory
30 directory={1}
31 users={0}
32 profile=schsh
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)
38                 # system folders
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)
42                 # user folder
43                 print("{0}\t/data\tnone\trw,bind\t0\t0".format(data), file=f)
44         
45         # setup the schroot directory
46         os.mkdir(chroot)
47         for folder in ["etc", "dev", "data"]:
48                 os.mkdir(os.path.join(chroot, folder))
49         
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)
58                 if group:
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)
62
63                 # Make sure ~/data (part of the fstab above) exists.
64                 if not os.path.exists(data):
65                         os.mkdir(data)
66                 shutil.chown(data, user, user)
67                 os.chmod(data, 0o640)
68
69         # user configuration
70         if userpw.pw_shell != schsh:
71                 subprocess.check_output(["usermod", "--shell", schsh, name])
72         if group:
73                 subprocess.check_output(["adduser", name, "schsh"])
74         
75         # done!
76
77 if len(sys.argv) <= 1:
78         print("Usage: %s <usernames>" % sys.argv[0])
79 else:
80         for name in sys.argv[1:]:
81                 print("Setting up",name)
82                 setup(name)