]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Checking group membership in simulation mode
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 9 Aug 2017 15:10:52 +0000 (17:10 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 9 Aug 2017 15:10:52 +0000 (17:10 +0200)
pp_lib/config_named_app.py

index 42812cea8ac2947beed6542ae1df40ab19668004..2cba07947cc4516b72a62a0b1aa2f83662fd7978 100644 (file)
@@ -39,7 +39,7 @@ from .cfg_app import PpCfgAppError, PpConfigApplication
 
 from .pidfile import PidFileError, InvalidPidFileError, PidFileInUseError, PidFile
 
-__version__ = '0.6.2'
+__version__ = '0.6.3'
 LOG = logging.getLogger(__name__)
 
 
@@ -786,10 +786,28 @@ class PpConfigNamedApp(PpConfigApplication):
     # -------------------------------------------------------------------------
     def _run(self):
 
-        if os.geteuid():
+        my_uid = os.geteuid()
+        if my_uid:
             if self.simulate:
                 LOG.warn("You must be root to execute this script.")
-                LOG.warn("But hey - this is simulation mode ...")
+                group_ids = {}
+                for group in grp.getgrall():
+                    for g_username in group.gr_mem:
+                        g_uid = pwd.getpwnam(g_username).pw_uid
+                        if g_uid == my_uid:
+                            group_ids[group.gr_name] = group.gr_gid
+                if self.verbose > 2:
+                    LOG.debug("You are a member of the groups:\n{}".format(group_ids))
+                if self.named_group in group_ids:
+                    LOG.warn((
+                        "But hey - this is simulation mode, "
+                        "and you are a member of group {!r} ...").format(
+                        self.named_group))
+                else:
+                    LOG.error((
+                        "But also in simulation mode you has to be a member "
+                        "of group {!r}!").format(self.named_group))
+                    self.exit(1)
                 time.sleep(1)
             else:
                 LOG.error("You must be root to execute this script.")