Browse Source

[213] Component to change uid

It is a workaround before we have socket creator.
Michal 'vorner' Vaner 13 years ago
parent
commit
e6d7624e50

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

@@ -17,6 +17,7 @@ from isc.bind10.component import Component, BaseComponent
 import isc.bind10.sockcreator
 import isc.bind10.sockcreator
 from bind10_config import LIBEXECDIR
 from bind10_config import LIBEXECDIR
 import os
 import os
+import posix
 
 
 class SockCreator(BaseComponent):
 class SockCreator(BaseComponent):
     """
     """
@@ -108,6 +109,31 @@ class XfrIn(Component):
         Component.__init__(self, process, boss, kind, 'Xfrin', None,
         Component.__init__(self, process, boss, kind, 'Xfrin', None,
                            boss.start_xfrin)
                            boss.start_xfrin)
 
 
+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:
+            # TODO: log
+            posix.setuid(self.uid)
+
+    def _stop_internal(self): pass
+    def kill(self, forefull=False): pass
+
+    def name(self):
+        return "Set UID"
+
+    def pid(self):
+        return None
+
 def get_specials():
 def get_specials():
     """
     """
     List of specially started components. Each one should be the class than can
     List of specially started components. Each one should be the class than can
@@ -123,5 +149,7 @@ def get_specials():
         'resolver': Resolver,
         'resolver': Resolver,
         'cmdctl': CmdCtl,
         'cmdctl': CmdCtl,
         # FIXME: Temporary workaround before #1292 is done
         # FIXME: Temporary workaround before #1292 is done
-        'xfrin': XfrIn
+        'xfrin': XfrIn,
+        # TODO: Remove when not needed, workaround before sockcreator works
+        'setuid': SetUID
     }
     }

+ 30 - 1
src/lib/python/isc/bind10/tests/component_test.py

@@ -106,6 +106,9 @@ class ComponentTests(BossUtils, unittest.TestCase):
         self.__registered_processes = {}
         self.__registered_processes = {}
         self.__stop_process_params = None
         self.__stop_process_params = None
         self.__start_simple_params = None
         self.__start_simple_params = None
+        # Pretending to be boss
+        self.uid = None
+        self.__uid_set = None
 
 
     def __start(self):
     def __start(self):
         """
         """
@@ -427,7 +430,8 @@ class ComponentTests(BossUtils, unittest.TestCase):
                                isc.bind10.special_component.Auth,
                                isc.bind10.special_component.Auth,
                                isc.bind10.special_component.Resolver,
                                isc.bind10.special_component.Resolver,
                                isc.bind10.special_component.CmdCtl,
                                isc.bind10.special_component.CmdCtl,
-                               isc.bind10.special_component.XfrIn]:
+                               isc.bind10.special_component.XfrIn,
+                               isc.bind10.special_component.SetUID]:
             component = component_type('none', self, 'needed')
             component = component_type('none', self, 'needed')
             self.assertIsNone(component.pid())
             self.assertIsNone(component.pid())
 
 
@@ -527,6 +531,31 @@ class ComponentTests(BossUtils, unittest.TestCase):
         self.assertTrue(process.killed)
         self.assertTrue(process.killed)
         self.assertFalse(process.terminated)
         self.assertFalse(process.terminated)
 
 
+    def setuid(self, uid):
+        self.__uid_set = uid
+
+    def test_setuid(self):
+        """
+        Some tests around the SetUID pseudo-component.
+        """
+        component = isc.bind10.special_component.SetUID(None, self, 'needed',
+                                                        None)
+        orig_setuid = isc.bind10.special_component.posix.setuid
+        isc.bind10.special_component.posix.setuid = self.setuid
+        component.start()
+        # No uid set in boss, nothing called.
+        self.assertIsNone(self.__uid_set)
+        # Doesn't do anything, but doesn't crash
+        component.stop()
+        component.kill()
+        component.kill(True)
+        self.uid = 42
+        component = isc.bind10.special_component.SetUID(None, self, 'needed',
+                                                        None)
+        component.start()
+        # This time, it get's called
+        self.assertEqual(42, self.__uid_set)
+
 class TestComponent(BaseComponent):
 class TestComponent(BaseComponent):
     """
     """
     A test component. It does not start any processes or so, it just logs
     A test component. It does not start any processes or so, it just logs