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/>.
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
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.
34 class vgp_startup_scripts_ext(gp_xml_ext
, gp_file_applier
):
36 return 'VGP/Unix Settings/Startup Scripts'
38 def process_group_policy(self
, deleted_gpo_list
, changed_gpo_list
,
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
:
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
)
52 policy
= xml_conf
.find('policysetting')
53 data
= policy
.find('data')
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(),
61 parameters
= listelement
.find('parameters')
62 if parameters
is not None:
63 parameters
= parameters
.text
66 value_hash
= listelement
.find('hash').text
67 attribute
= self
.generate_attribute(script_file
,
69 attributes
.append(attribute
)
70 run_as
= listelement
.find('run_as')
71 if run_as
is not None:
75 run_once
= listelement
.find('run_once') is not None
77 def applier_func(script_file
, parameters
):
78 Popen(['/bin/sh %s %s' % (script_file
, parameters
)],
80 # Run once scripts don't create a file to unapply,
81 # so their is nothing to return.
83 self
.apply(gpo
.name
, attribute
, value_hash
, applier_func
,
84 script_file
, parameters
)
86 def applier_func(run_as
, script_file
, parameters
):
87 entry
= '@reboot %s %s %s' % (run_as
, script_file
,
89 with
NamedTemporaryFile(prefix
='gp_', dir=cdir
,
92 f
.write(get_bytes(entry
))
93 os
.chmod(f
.name
, 0o700)
95 self
.apply(gpo
.name
, attribute
, value_hash
, applier_func
,
96 run_as
, script_file
, parameters
)
98 self
.clean(gpo
.name
, keep
=attributes
)
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
)
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(),
116 parameters
= listelement
.find('parameters')
117 if parameters
is not None:
118 parameters
= parameters
.text
121 run_as
= listelement
.find('run_as')
122 if run_as
is not None:
126 run_once
= listelement
.find('run_once') is not None
128 entry
= 'Run once as: %s `%s %s`' % (run_as
, script_file
,
131 entry
= '@reboot %s %s %s' % (run_as
, script_file
,
133 if str(self
) not in output
.keys():
134 output
[str(self
)] = []
135 output
[str(self
)].append(entry
)