Browse Source

[1508] Move the dropping root into the socket creator

And removed the setuid component
Michal 'vorner' Vaner 13 years ago
parent
commit
9e43c8d755

+ 5 - 29
src/lib/python/isc/bind10/special_component.py

@@ -36,6 +36,7 @@ class SockCreator(BaseComponent):
     def __init__(self, process, boss, kind, address=None, params=None):
         BaseComponent.__init__(self, boss, kind)
         self.__creator = None
+        self.__uid = boss.uid
 
     def _start_internal(self):
         self._boss.curproc = 'b10-sockcreator'
@@ -44,6 +45,9 @@ class SockCreator(BaseComponent):
         self._boss.register_process(self.pid(), self)
         self._boss.set_creator(self.__creator)
         self._boss.log_started(self.pid())
+        if self.__uid is not None:
+            logger.info(BIND10_SETUID, self.__uid)
+            posix.setuid(self.__uid)
 
     def _stop_internal(self):
         self.__creator.terminate()
@@ -108,32 +112,6 @@ class CmdCtl(Component):
     def __init__(self, process, boss, kind, address=None, params=None):
         Component.__init__(self, process, boss, kind, 'Cmdctl', None,
                            boss.start_cmdctl)
-
-class SetUID(BaseComponent):
-    """
-    This is a pseudo-component which drops root privileges when started
-    and sets the uid stored in boss.
-
-    This component does nothing when stopped.
-    """
-    def __init__(self, process, boss, kind, address=None, params=None):
-        BaseComponent.__init__(self, boss, kind)
-        self.uid = boss.uid
-
-    def _start_internal(self):
-        if self.uid is not None:
-            logger.info(BIND10_SETUID, self.uid)
-            posix.setuid(self.uid)
-
-    def _stop_internal(self): pass
-    def kill(self, forceful=False): pass
-
-    def name(self):
-        return "Set UID"
-
-    def pid(self):
-        return None
-
 def get_specials():
     """
     List of specially started components. Each one should be the class than can
@@ -147,7 +125,5 @@ def get_specials():
         # They should not have any parameters anyway
         'auth': Auth,
         'resolver': Resolver,
-        'cmdctl': CmdCtl,
-        # TODO: Remove when not needed, workaround before sockcreator works
-        'setuid': SetUID
+        'cmdctl': CmdCtl
     }

+ 34 - 8
src/lib/python/isc/bind10/tests/component_test.py

@@ -507,8 +507,7 @@ class ComponentTests(BossUtils, unittest.TestCase):
                                isc.bind10.special_component.CfgMgr,
                                isc.bind10.special_component.Auth,
                                isc.bind10.special_component.Resolver,
-                               isc.bind10.special_component.CmdCtl,
-                               isc.bind10.special_component.SetUID]:
+                               isc.bind10.special_component.CmdCtl]:
             component = component_type('none', self, 'needed')
             self.assertIsNone(component.pid())
 
@@ -611,14 +610,38 @@ class ComponentTests(BossUtils, unittest.TestCase):
     def setuid(self, uid):
         self.__uid_set = uid
 
-    def test_setuid(self):
+    class FakeCreator:
+        def pid(self):
+            return 42
+        def terminate(self): pass
+        def kill(self): pass
+
+    def set_creator(self, creator):
+        """
+        Part of faking being the boss. Check the creator (faked as well)
+        is passed here.
+        """
+        self.assertTrue(isinstance(creator, self.FakeCreator))
+
+    def log_started(self, pid):
+        """
+        Part of faking the boss. Check the pid is the one of the fake creator.
+        """
+        self.assertEqual(42, pid)
+
+    def test_creator(self):
         """
-        Some tests around the SetUID pseudo-component.
+        Some tests around the SockCreator component.
         """
-        component = isc.bind10.special_component.SetUID(None, self, 'needed',
-                                                        None)
+        component = isc.bind10.special_component.SockCreator(None, self, 'needed',
+                                                             None)
         orig_setuid = isc.bind10.special_component.posix.setuid
         isc.bind10.special_component.posix.setuid = self.setuid
+        orig_creator = \
+            isc.bind10.special_component.isc.bind10.sockcreator.Creator
+        # Just ignore the creator call
+        isc.bind10.special_component.isc.bind10.sockcreator.Creator = \
+            lambda path: self.FakeCreator()
         component.start()
         # No uid set in boss, nothing called.
         self.assertIsNone(self.__uid_set)
@@ -627,11 +650,14 @@ class ComponentTests(BossUtils, unittest.TestCase):
         component.kill()
         component.kill(True)
         self.uid = 42
-        component = isc.bind10.special_component.SetUID(None, self, 'needed',
-                                                        None)
+        component = isc.bind10.special_component.SockCreator(None, self, 'needed',
+                                                             None)
         component.start()
         # This time, it get's called
         self.assertEqual(42, self.__uid_set)
+        isc.bind10.special_component.posix.setuid = orig_setuid
+        isc.bind10.special_component.isc.bind10.sockcreator.Creator = \
+            orig_creator
 
 class TestComponent(BaseComponent):
     """