]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
samba-tool: Add a gpo command for adding Sudoers Group Policy
authorDavid Mulder <dmulder@suse.com>
Fri, 6 Nov 2020 16:30:35 +0000 (09:30 -0700)
committerDavid Mulder <dmulder@samba.org>
Wed, 9 Dec 2020 17:38:28 +0000 (17:38 +0000)
Signed-off-by: David Mulder <dmulder@suse.com>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
python/samba/netcmd/gpo.py
selftest/knownfail.d/gpo [deleted file]

index cf763af1c23d1ca545ca5a532804dea06f9db3c2..28887081bdab730c1f761f0de3f1aa574499bfdb 100644 (file)
@@ -65,6 +65,7 @@ from samba.gp_parse.gp_aas import GPAasParser
 from samba import param
 from samba.credentials import SMB_SIGNING_REQUIRED
 from samba.netcmd.common import attr_default
+from samba.common import get_bytes
 
 
 def gpo_flags_string(value):
@@ -1667,6 +1668,11 @@ class cmd_admxload(Command):
 
 class cmd_add_sudoers(Command):
     """Adds a Samba Sudoers Group Policy to the sysvol
+
+This command adds a sudo rule to the sysvol for applying to winbind clients.
+
+Example:
+samba-tool gpo manage sudoers add {31B2F340-016D-11D2-945F-00C04FB984F9} 'fakeu ALL=(ALL) NOPASSWD: ALL'
     """
 
     synopsis = "%prog <gpo> <entry> [options]"
@@ -1685,7 +1691,56 @@ class cmd_add_sudoers(Command):
     takes_args = ["gpo", "entry"]
 
     def run(self, gpo, entry, H=None, sambaopts=None, credopts=None, versionopts=None):
-        pass
+        self.lp = sambaopts.get_loadparm()
+        self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
+
+        # We need to know writable DC to setup SMB connection
+        if H and H.startswith('ldap://'):
+            dc_hostname = H[7:]
+            self.url = H
+        else:
+            dc_hostname = netcmd_finddc(self.lp, self.creds)
+            self.url = dc_url(self.lp, self.creds, dc=dc_hostname)
+
+        # SMB connect to DC
+        conn = smb_connection(dc_hostname,
+                              'sysvol',
+                              lp=self.lp,
+                              creds=self.creds)
+
+        realm = self.lp.get('realm')
+        pol_dir = '\\'.join([realm.lower(), 'Policies', gpo, 'MACHINE'])
+        pol_file = '\\'.join([pol_dir, 'Registry.pol'])
+        try:
+            pol_data = ndr_unpack(preg.file, conn.loadfile(pol_file))
+        except NTSTATUSError as e:
+            # STATUS_OBJECT_NAME_INVALID, STATUS_OBJECT_NAME_NOT_FOUND
+            if e.args[0] in [0xC0000033, 0xC0000034]:
+                pol_data = preg.file() # The file doesn't exist
+            elif e.args[0] == 0xC0000022: # STATUS_ACCESS_DENIED
+                raise CommandError("The authenticated user does "
+                                   "not have sufficient privileges")
+            else:
+                raise
+
+        e = preg.entry()
+        e.keyname = b'Software\\Policies\\Samba\\Unix Settings\\Sudo Rights'
+        e.valuename = b'Software\\Policies\\Samba\\Unix Settings'
+        e.type = 1
+        e.data = get_bytes(entry)
+        entries = list(pol_data.entries)
+        entries.append(e)
+        pol_data.entries = entries
+        pol_data.num_entries = len(entries)
+
+        try:
+            create_directory_hier(conn, pol_dir)
+            conn.savefile(pol_file, ndr_pack(pol_data))
+        except NTSTATUSError as e:
+            if e.args[0] == 0xC0000022: # STATUS_ACCESS_DENIED
+                raise CommandError("The authenticated user does "
+                                   "not have sufficient privileges")
+            raise
 
 class cmd_list_sudoers(Command):
     """List Samba Sudoers Group Policy from the sysvol
diff --git a/selftest/knownfail.d/gpo b/selftest/knownfail.d/gpo
deleted file mode 100644 (file)
index 05e3d4e..0000000
+++ /dev/null
@@ -1 +0,0 @@
-^samba.tests.samba_tool.gpo.samba.tests.samba_tool.gpo.GpoCmdTestCase.test_sudoers_add