1 # gp_sudoers_ext samba gpo policy
2 # Copyright (C) David Mulder <dmulder@suse.com> 2020
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
.gpclass
import gp_pol_ext
19 from base64
import b64encode
20 from tempfile
import NamedTemporaryFile
21 from subprocess
import Popen
, PIPE
23 def find_executable(executable
, path
):
24 paths
= path
.split(os
.pathsep
)
26 f
= os
.path
.join(p
, executable
)
32 ### autogenerated by samba
34 # This file is generated by the gp_sudoers_ext Group Policy
35 # Client Side Extension. To modify the contents of this file,
36 # modify the appropriate Group Policy objects which apply
37 # to this machine. DO NOT MODIFY THIS FILE DIRECTLY.
41 visudo
= find_executable('visudo',
42 path
='%s:%s' % (os
.environ
['PATH'], '/usr/sbin'))
44 class gp_sudoers_ext(gp_pol_ext
):
46 return 'Unix Settings/Sudo Rights'
48 def process_group_policy(self
, deleted_gpo_list
, changed_gpo_list
,
49 sdir
='/etc/sudoers.d'):
50 for guid
, settings
in deleted_gpo_list
:
51 self
.gp_db
.set_guid(guid
)
52 if str(self
) in settings
:
53 for attribute
, sudoers
in settings
[str(self
)].items():
54 if os
.path
.exists(sudoers
):
56 self
.gp_db
.delete(str(self
), attribute
)
59 for gpo
in changed_gpo_list
:
61 section
= 'Software\\Policies\\Samba\\Unix Settings\\Sudo Rights'
62 self
.gp_db
.set_guid(gpo
.name
)
63 pol_file
= 'MACHINE/Registry.pol'
64 path
= os
.path
.join(gpo
.file_sys_path
, pol_file
)
65 pol_conf
= self
.parse(path
)
68 for e
in pol_conf
.entries
:
69 if e
.keyname
== section
and e
.data
.strip():
70 attribute
= b64encode(e
.data
.encode()).decode()
71 old_val
= self
.gp_db
.retrieve(str(self
), attribute
)
74 contents
+= '%s\n' % e
.data
75 with
NamedTemporaryFile() as f
:
76 with
open(f
.name
, 'w') as w
:
79 Popen([visudo
, '-c', '-f', f
.name
],
80 stdout
=PIPE
, stderr
=PIPE
).wait()
81 if sudo_validation
== 0:
82 with
NamedTemporaryFile(prefix
='gp_',
85 with
open(f
.name
, 'w') as w
:
87 self
.gp_db
.store(str(self
),
91 self
.logger
.warn('Sudoers apply "%s" failed'
97 pol_file
= 'MACHINE/Registry.pol'
99 path
= os
.path
.join(gpo
.file_sys_path
, pol_file
)
100 pol_conf
= self
.parse(path
)
103 for e
in pol_conf
.entries
:
104 key
= e
.keyname
.split('\\')[-1]
105 if key
.endswith('Sudo Rights') and e
.data
.strip():
106 if key
not in output
.keys():
108 output
[key
].append(e
.data
)