gpo: Restore gPCMachineExtensionNames and gPCUserExtensionNames
authorGarming Sam <garming@catalyst.net.nz>
Wed, 20 Feb 2019 03:51:04 +0000 (16:51 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 12 Mar 2019 00:42:20 +0000 (00:42 +0000)
After creating a backup and calling 'gpo restore', this makes it so that
restoring a GPO will instantly enable it for use.

There might be some cases where we might not want to do this, but for now just do it.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=13627

Signed-off-by: Garming Sam <garming@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/netcmd/gpo.py

index ae06594e719da289d0659d73806d992fa43cc5ee..b082b34302de9cc4923c6fdcd8f51e9ca0a07c77 100644 (file)
@@ -171,7 +171,9 @@ def get_gpo_info(samdb, gpo=None, displayname=None, dn=None,
                                   'flags',
                                   'name',
                                   'displayName',
-                                  'gPCFileSysPath'],
+                                  'gPCFileSysPath',
+                                  'gPCMachineExtensionNames',
+                                  'gPCUserExtensionNames'],
                            controls=['sd_flags:1:%d' % sd_flags])
     except Exception as e:
         if gpo is not None:
@@ -1076,6 +1078,12 @@ class cmd_backup(GPOCommand):
                 self.outf.write('\nEntities:\n')
                 self.outf.write(ents)
 
+        # Backup the enabled GPO extension names
+        for ext in ('gPCMachineExtensionNames', 'gPCUserExtensionNames'):
+            if ext in msg:
+                with open(os.path.join(gpodir, ext + '.SAMBAEXT'), 'wb') as f:
+                    f.write(msg[ext][0])
+
     @staticmethod
     def generalize_xml_entities(outf, sourcedir, targetdir):
         entities = {}
@@ -1402,6 +1410,22 @@ class cmd_restore(cmd_create):
                                            self.sharepath,
                                            ignore_existing=True)
 
+            gpo_dn = get_gpo_dn(self.samdb, self.gpo_name)
+
+            # Restore the enabled extensions
+            for ext in ('gPCMachineExtensionNames', 'gPCUserExtensionNames'):
+                ext_file = os.path.join(backup, ext + '.SAMBAEXT')
+                if os.path.exists(ext_file):
+                    with open(ext_file, 'rb') as f:
+                        data = f.read()
+
+                    m = ldb.Message()
+                    m.dn = gpo_dn
+                    m[ext] = ldb.MessageElement(data, ldb.FLAG_MOD_REPLACE,
+                                                ext)
+
+                    self.samdb.modify(m)
+
         except Exception as e:
             import traceback
             traceback.print_exc()