make sure data dir exists
[schsh.git] / makeschsh
index 61b37f63d3045846437d19d3aee9907d4e2049bf..e728dc00dd27d46871ce74f4c620a1937fa6bf4b 100755 (executable)
--- a/makeschsh
+++ b/makeschsh
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
 # Configuration
 schsh = "/usr/local/bin/schsh"
 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
 # Configuration
 schsh = "/usr/local/bin/schsh"
@@ -9,10 +9,10 @@ chroots = "/var/lib/schsh"
 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
 # DO NOT TOUCH ANYTHING BELOW THIS LINE
 
 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
 # DO NOT TOUCH ANYTHING BELOW THIS LINE
 
-import os, sys, subprocess, pwd, grp
+import os, sys, subprocess, pwd, grp, shutil
 
 if os.getuid() != 0:
 
 if os.getuid() != 0:
-       print "Run this a root, please."
+       print("Run this a root, please.")
        sys.exit(1)
 
 
        sys.exit(1)
 
 
@@ -21,43 +21,51 @@ def setup(name):
        if os.path.exists(chroot):
                raise Exception(chroot+" already exists, please remove it first")
        userpw = pwd.getpwnam(name)
        if os.path.exists(chroot):
                raise Exception(chroot+" already exists, please remove it first")
        userpw = pwd.getpwnam(name)
+       data = "/home/{0}/data".format(name)
        
        # schroot configuration
        with open("/etc/schroot/chroot.d/schsh-"+name, "w") as f:
        
        # schroot configuration
        with open("/etc/schroot/chroot.d/schsh-"+name, "w") as f:
-               print >>f, """[schsh-{0}]
+               print("""[schsh-{0}]
 type=directory
 directory={1}
 users={0}
 profile=schsh
 setup.fstab=schsh/{0}.fstab
 type=directory
 directory={1}
 users={0}
 profile=schsh
 setup.fstab=schsh/{0}.fstab
-""".format(name, chroot)
+""".format(name, chroot), file=f)
        with open("/etc/schroot/schsh/"+name+".fstab", "w") as f:
        with open("/etc/schroot/schsh/"+name+".fstab", "w") as f:
-               print >>f, """# <file system> <mount point>   <type>  <options>       <dump>  <pass>
-/bin          \t/bin          \tnone  \trw,bind       \t0     \t0
-/lib          \t/lib          \tnone  \trw,bind       \t0     \t0
-/usr/bin      \t/usr/bin      \tnone  \trw,bind       \t0     \t0
-/usr/lib      \t/usr/lib      \tnone  \trw,bind       \t0     \t0
-/home/{0}/data\t/data         \tnone  \trw,bind       \t0     \t0
-""".replace(' ', '').format(name) # need to remove spaces so schroot does not complain
+               # no spaces, schroot does not like them
+               print("# <file system> <mount point>   <type>  <options>       <dump>  <pass>", file=f)
+               # system folders
+               for folder in ("/lib", "/lib64", "/usr/bin", "/usr/lib", "/usr/lib64", "/usr/share/", "/usr/local/bin"):
+                       if os.path.exists(folder):
+                               print("{0}\t{0}\tnone\trw,bind\t0\t0".format(folder), file=f)
+               # user folder
+               print("{0}\t/data\tnone\trw,bind\t0\t0".format(data), file=f)
        
        # setup the schroot directory
        os.mkdir(chroot)
        
        # setup the schroot directory
        os.mkdir(chroot)
-       for folder in ["etc", "dev", "bin", "usr", "data"]:
+       for folder in ["etc", "dev", "data"]:
                os.mkdir(os.path.join(chroot, folder))
        
        # setup /etc/passwd and /etc/group
        with open(os.path.join(chroot, "etc", "passwd"), "w") as f:
                os.mkdir(os.path.join(chroot, folder))
        
        # setup /etc/passwd and /etc/group
        with open(os.path.join(chroot, "etc", "passwd"), "w") as f:
-               print >>f, "root:x:0:0:root:/root:/bin/bash"
-               print >>f, "{0}:x:{1}:{2}:,,,:/data:/bin/false".format(name, userpw.pw_uid, userpw.pw_gid)
+               print("root:x:0:0:root:/root:/bin/bash", file=f)
+               print("{0}:x:{1}:{2}:,,,:/data:/bin/false".format(name, userpw.pw_uid, userpw.pw_gid), file=f)
        with open(os.path.join(chroot, "etc", "group"), "w") as f:
        with open(os.path.join(chroot, "etc", "group"), "w") as f:
-               print >>f, "root:x:0:"
+               print("root:x:0:", file=f)
                usergrp = grp.getgrgid(userpw.pw_gid)
                usergrp = grp.getgrgid(userpw.pw_gid)
-               print >>f, "{0}:x:{1}:".format(usergrp.gr_name, usergrp.gr_gid)
+               print("{0}:x:{1}:".format(usergrp.gr_name, usergrp.gr_gid), file=f)
                if group:
                        groupgrp = grp.getgrnam(group)
                        assert usergrp.gr_gid != groupgrp.gr_gid
                if group:
                        groupgrp = grp.getgrnam(group)
                        assert usergrp.gr_gid != groupgrp.gr_gid
-                       print >>f, "{0}:x:{1}:{2}".format(groupgrp.gr_name, groupgrp.gr_gid, name)
-       
+                       print("{0}:x:{1}:{2}".format(groupgrp.gr_name, groupgrp.gr_gid, name), file=f)
+
+               # Make sure ~/data (part of the fstab above) exists.
+               if not os.path.exists(data):
+                       os.mkdir(data)
+               shutil.chown(data, user, user)
+               os.chmod(data, 0o640)
+
        # user configuration
        if userpw.pw_shell != schsh:
                subprocess.check_output(["usermod", "--shell", schsh, name])
        # user configuration
        if userpw.pw_shell != schsh:
                subprocess.check_output(["usermod", "--shell", schsh, name])
@@ -67,8 +75,8 @@ setup.fstab=schsh/{0}.fstab
        # done!
 
 if len(sys.argv) <= 1:
        # done!
 
 if len(sys.argv) <= 1:
-       print "Usage: %s <usernames>" % sys.argv[0]
+       print("Usage: %s <usernames>" % sys.argv[0])
 else:
        for name in sys.argv[1:]:
 else:
        for name in sys.argv[1:]:
-               print "Setting up",name
+               print("Setting up",name)
                setup(name)
                setup(name)