From 65695aa1d0fbc0a4aa5e7b1bb28c66fbb4879e01 Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Sun, 25 Jun 2017 16:05:44 +0200 Subject: Create pidfile directory in pynslcd This ensures that /var/run/nslcd is created (when it does not exist) when starting pynslcd. --- pynslcd/cfg.py | 12 ++++++++++++ pynslcd/mypidfile.py | 10 +++++++++- pynslcd/pynslcd.py | 10 ++-------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/pynslcd/cfg.py b/pynslcd/cfg.py index 2406cc2..dcbc8f7 100644 --- a/pynslcd/cfg.py +++ b/pynslcd/cfg.py @@ -334,3 +334,15 @@ def read(filename): for k, v in globals().items(): if not k.startswith('_'): logging.debug('%s=%r', k, v) + + +def get_usergid(): + """Return user info and group id.""" + import pwd + import grp + u = pwd.getpwnam(uid) + if gid is None: + g = u.pw_gid + else: + g = grp.getgrnam(gid).gr_gid + return u, g diff --git a/pynslcd/mypidfile.py b/pynslcd/mypidfile.py index e386f3e..2bf158f 100644 --- a/pynslcd/mypidfile.py +++ b/pynslcd/mypidfile.py @@ -1,7 +1,7 @@ # mypidfile.py - functions for properly locking a PIDFile # -# Copyright (C) 2010, 2011, 2012 Arthur de Jong +# Copyright (C) 2010-2017 Arthur de Jong # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -22,6 +22,8 @@ import errno import fcntl import os +import cfg + class MyPIDLockFile(object): """Implementation of a PIDFile fit for use with the daemon module @@ -32,6 +34,12 @@ class MyPIDLockFile(object): def __enter__(self): """Lock the PID file and write the process ID to the file.""" + # create the directory for the pidfile if needed + piddir = os.path.dirname(self.path) + if not os.path.isdir(piddir): + os.mkdir(piddir) + u, gid = cfg.get_usergid() + os.chown(piddir, u.u.pw_uid, gid) fd = os.open(self.path, os.O_RDWR | os.O_CREAT, 0644) try: fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) diff --git a/pynslcd/pynslcd.py b/pynslcd/pynslcd.py index 565b0aa..b7ff7f4 100755 --- a/pynslcd/pynslcd.py +++ b/pynslcd/pynslcd.py @@ -2,7 +2,7 @@ # pynslcd.py - main daemon module # -# Copyright (C) 2010-2016 Arthur de Jong +# Copyright (C) 2010-2017 Arthur de Jong # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -339,13 +339,7 @@ if __name__ == '__main__': nslcd_serversocket = create_socket() # load supplementary groups if cfg.uid is not None: - import pwd - import grp - u = pwd.getpwnam(cfg.uid) - if cfg.gid is None: - gid = u.pw_gid - else: - gid = grp.getgrnam(cfg.gid).gr_gid + u, gid = cfg.get_usergid() # set supplementary groups, gid and uid os.initgroups(u.pw_name, gid) os.setgid(gid) -- cgit v1.2.3