summaryrefslogtreecommitdiffstats
path: root/pykolab/utils.py
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2012-06-13 11:37:19 +0100
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2012-06-13 11:37:19 +0100
commit1df64e63556db23be0d701ca10c4fd35f87f9aec (patch)
treed5677e36d70d14f01b7154bbec76259e9ac9d156 /pykolab/utils.py
parentd310f8a8ba453cc1030a6a83cbdd8332a5fb64ff (diff)
downloadpykolab-1df64e63556db23be0d701ca10c4fd35f87f9aec.tar.gz
Add helper function to ensure a directory exists, and with the correct permissions
Diffstat (limited to 'pykolab/utils.py')
-rw-r--r--pykolab/utils.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/pykolab/utils.py b/pykolab/utils.py
index f0fa30a..f766983 100644
--- a/pykolab/utils.py
+++ b/pykolab/utils.py
@@ -121,6 +121,79 @@ def ask_confirmation(question, default="y", all_inclusive_no=True):
else:
return True
+def ensure_directory(_dir, _user='root', _group='root'):
+ os.makedirs(_dir)
+ try:
+ try:
+ (ruid, euid, suid) = os.getresuid()
+ (rgid, egid, sgid) = os.getresgid()
+ except AttributeError, errmsg:
+ ruid = os.getuid()
+ rgid = os.getgid()
+
+ if ruid == 0:
+ # Means we can setreuid() / setregid() / setgroups()
+ if rgid == 0:
+ # Get group entry details
+ try:
+ (
+ group_name,
+ group_password,
+ group_gid,
+ group_members
+ ) = grp.getgrnam(_group)
+
+ except KeyError:
+ print >> sys.stderr, _("Group %s does not exist") % (
+ _group
+ )
+
+ sys.exit(1)
+
+ # Set real and effective group if not the same as current.
+ if not group_gid == rgid:
+ log.debug(
+ _("Switching real and effective group id to %d") % (
+ group_gid
+ ),
+ level=8
+ )
+
+ os.chown(_dir, -1, group_gid)
+
+ if ruid == 0:
+ # Means we haven't switched yet.
+ try:
+ (
+ user_name,
+ user_password,
+ user_uid,
+ user_gid,
+ user_gecos,
+ user_homedir,
+ user_shell
+ ) = pwd.getpwnam(_user)
+
+ except KeyError:
+ print >> sys.stderr, _("User %s does not exist") % (_user)
+
+ sys.exit(1)
+
+
+ # Set real and effective user if not the same as current.
+ if not user_uid == ruid:
+ log.debug(
+ _("Switching real and effective user id to %d") % (
+ user_uid
+ ),
+ level=8
+ )
+
+ os.chown(_dir, user_uid, -1)
+
+ except:
+ log.error(_("Could not change the permissions on %s") % (_dir))
+
def generate_password():
import subprocess