gpo: Make restore with entities more robust
authorGarming Sam <garming@catalyst.net.nz>
Tue, 12 Jun 2018 04:19:41 +0000 (16:19 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 16 Aug 2018 21:42:22 +0000 (23:42 +0200)
Sometimes the restore fails for unknown reasons, but rearranging the XML
such that the DTD is after the xml header appears to fix it. This might
be the case in certain files where no entities are used perhaps.

This could probably be made more tolerant using regex, but for the most
part we expect the fixed output from the minidom pretty-printed XML.

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

index 92526965ccc96d442fc603ba79d0948bb47e3cf4..1fc25bd6f1b9c146439d2c4b1aa90cbc839496b3 100644 (file)
@@ -1310,8 +1310,17 @@ class cmd_restore(cmd_create):
                         try:
                             with open(l_name, 'r') as ltemp:
                                 data = ltemp.read()
-                                # Load the XML file with the DTD (entity) header
-                                parser.load_xml(ET.fromstring(dtd_header + data))
+                                xml_head = '<?xml version="1.0" encoding="utf-8"?>'
+
+                                if data.startswith(xml_head):
+                                    # It appears that sometimes the DTD rejects
+                                    # the xml header being after it.
+                                    data = data[len(xml_head):]
+
+                                    # Load the XML file with the DTD (entity) header
+                                    parser.load_xml(ET.fromstring(xml_head + dtd_header + data))
+                                else:
+                                    parser.load_xml(ET.fromstring(dtd_header + data))
 
                                 # Write out the substituted files in the output
                                 # location, ready to copy over.