gpo: Apply Group Policy Login Prompt Message
[gd/samba-autobuild/.git] / python / samba / gp_msgs_ext.py
1 # gp_msgs_ext samba gpo policy
2 # Copyright (C) David Mulder <dmulder@suse.com> 2020
3 #
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.
8 #
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.
13 #
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/>.
16
17 import os
18 from samba.gpclass import gp_pol_ext
19
20 class gp_msgs_ext(gp_pol_ext):
21     def __str__(self):
22         return 'Unix Settings/Messages'
23
24     def process_group_policy(self, deleted_gpo_list, changed_gpo_list,
25                              cdir='/etc'):
26         for guid, settings in deleted_gpo_list:
27             self.gp_db.set_guid(guid)
28             if str(self) in settings:
29                 for attribute, msg in settings[str(self)].items():
30                     if attribute == 'motd':
31                         mfile = os.path.join(cdir, 'motd')
32                     elif attribute == 'issue':
33                         mfile = os.path.join(cdir, 'issue')
34                     else:
35                         continue
36                     with open(mfile, 'w') as w:
37                         if msg:
38                             w.write(msg)
39                         else:
40                             w.truncate()
41                     self.gp_db.delete(str(self), attribute)
42             self.gp_db.commit()
43
44         for gpo in changed_gpo_list:
45             if gpo.file_sys_path:
46                 section_name = 'Software\\Policies\\Samba\\Unix Settings\\Messages'
47                 self.gp_db.set_guid(gpo.name)
48                 pol_file = 'MACHINE/Registry.pol'
49                 path = os.path.join(gpo.file_sys_path, pol_file)
50                 pol_conf = self.parse(path)
51                 if not pol_conf:
52                     continue
53                 for e in pol_conf.entries:
54                     if e.keyname == section_name and e.data.strip():
55                         if e.valuename == 'motd':
56                             mfile = os.path.join(cdir, 'motd')
57                         elif e.valuename == 'issue':
58                             mfile = os.path.join(cdir, 'issue')
59                         else:
60                             continue
61                         if os.path.exists(mfile):
62                             old_val = open(mfile, 'r').read()
63                         else:
64                             old_val = ''
65                         with open(mfile, 'w') as w:
66                             w.write(e.data)
67                             self.gp_db.store(str(self), e.valuename, old_val)
68                         self.gp_db.commit()