s3: smbd: Ensure srvstr_pull_req_talloc() always NULLs out *dest.
[Samba.git] / python / samba / gp / vgp_startup_scripts_ext.py
blob02f22bab78d4dc55d9b45992014f2be51fdf7400
1 # vgp_startup_scripts_ext samba gpo policy
2 # Copyright (C) David Mulder <dmulder@suse.com> 2021
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 import os
18 from samba.gp.gpclass import gp_xml_ext, check_safe_path, gp_file_applier
19 from tempfile import NamedTemporaryFile
20 from samba.common import get_bytes
21 from subprocess import Popen, PIPE
23 intro = b'''
24 ### autogenerated by samba
26 # This file is generated by the vgp_startup_scripts_ext Group Policy
27 # Client Side Extension. To modify the contents of this file,
28 # modify the appropriate Group Policy objects which apply
29 # to this machine. DO NOT MODIFY THIS FILE DIRECTLY.
32 '''
34 class vgp_startup_scripts_ext(gp_xml_ext, gp_file_applier):
35 def __str__(self):
36 return 'VGP/Unix Settings/Startup Scripts'
38 def process_group_policy(self, deleted_gpo_list, changed_gpo_list,
39 cdir='/etc/cron.d'):
40 for guid, settings in deleted_gpo_list:
41 if str(self) in settings:
42 for attribute, script in settings[str(self)].items():
43 self.unapply(guid, attribute, script)
45 for gpo in changed_gpo_list:
46 if gpo.file_sys_path:
47 xml = 'MACHINE/VGP/VTLA/Unix/Scripts/Startup/manifest.xml'
48 path = os.path.join(gpo.file_sys_path, xml)
49 xml_conf = self.parse(path)
50 if not xml_conf:
51 continue
52 policy = xml_conf.find('policysetting')
53 data = policy.find('data')
54 attributes = []
55 for listelement in data.findall('listelement'):
56 local_path = self.lp.cache_path('gpo_cache')
57 script = listelement.find('script').text
58 script_file = os.path.join(local_path,
59 os.path.dirname(check_safe_path(path)).upper(),
60 script.upper())
61 parameters = listelement.find('parameters')
62 if parameters is not None:
63 parameters = parameters.text
64 else:
65 parameters = ''
66 value_hash = listelement.find('hash').text
67 attribute = self.generate_attribute(script_file,
68 parameters)
69 attributes.append(attribute)
70 run_as = listelement.find('run_as')
71 if run_as is not None:
72 run_as = run_as.text
73 else:
74 run_as = 'root'
75 run_once = listelement.find('run_once') is not None
76 if run_once:
77 def applier_func(script_file, parameters):
78 Popen(['/bin/sh %s %s' % (script_file, parameters)],
79 shell=True).wait()
80 # Run once scripts don't create a file to unapply,
81 # so their is nothing to return.
82 return []
83 self.apply(gpo.name, attribute, value_hash, applier_func,
84 script_file, parameters)
85 else:
86 def applier_func(run_as, script_file, parameters):
87 entry = '@reboot %s %s %s' % (run_as, script_file,
88 parameters)
89 with NamedTemporaryFile(prefix='gp_', dir=cdir,
90 delete=False) as f:
91 f.write(intro)
92 f.write(get_bytes(entry))
93 os.chmod(f.name, 0o700)
94 return [f.name]
95 self.apply(gpo.name, attribute, value_hash, applier_func,
96 run_as, script_file, parameters)
98 self.clean(gpo.name, keep=attributes)
100 def rsop(self, gpo):
101 output = {}
102 xml = 'MACHINE/VGP/VTLA/Unix/Scripts/Startup/manifest.xml'
103 if gpo.file_sys_path:
104 path = os.path.join(gpo.file_sys_path, xml)
105 xml_conf = self.parse(path)
106 if not xml_conf:
107 return output
108 policy = xml_conf.find('policysetting')
109 data = policy.find('data')
110 for listelement in data.findall('listelement'):
111 local_path = self.lp.cache_path('gpo_cache')
112 script = listelement.find('script').text
113 script_file = os.path.join(local_path,
114 os.path.dirname(check_safe_path(path)).upper(),
115 script.upper())
116 parameters = listelement.find('parameters')
117 if parameters is not None:
118 parameters = parameters.text
119 else:
120 parameters = ''
121 run_as = listelement.find('run_as')
122 if run_as is not None:
123 run_as = run_as.text
124 else:
125 run_as = 'root'
126 run_once = listelement.find('run_once') is not None
127 if run_once:
128 entry = 'Run once as: %s `%s %s`' % (run_as, script_file,
129 parameters)
130 else:
131 entry = '@reboot %s %s %s' % (run_as, script_file,
132 parameters)
133 if str(self) not in output.keys():
134 output[str(self)] = []
135 output[str(self)].append(entry)
136 return output