5153225c536370c9b9259459104a58824a847737
[samba.git] / source4 / scripting / bin / samba-gpupdate
1 #!/usr/bin/env python3
2 # Copyright Luke Morrison <luc785@.hotmail.com> July 2013
3 # Co-Edited by Matthieu Pattou July 2013 from original August 2013
4 # Edited by Garming Sam Feb. 2014
5 # Edited by Luke Morrison April 2014
6 # Edited by David Mulder May 2017
7
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 '''This script reads a log file of previous GPO, gets all GPO from sysvol
22 and sorts them by container. Then, it applies the ones that haven't been
23 applied, have changed, or is in the right container'''
24
25 import os
26 import sys
27
28 sys.path.insert(0, "bin/python")
29
30 import optparse
31 from samba import getopt as options
32 from samba.gpclass import apply_gp, unapply_gp, GPOStorage, rsop
33 from samba.gp_sec_ext import gp_krb_ext, gp_access_ext
34 from samba.gp_ext_loader import get_gp_client_side_extensions
35 from samba.gp_scripts_ext import gp_scripts_ext, gp_user_scripts_ext
36 from samba.gp_sudoers_ext import gp_sudoers_ext
37 from samba.vgp_sudoers_ext import vgp_sudoers_ext
38 from samba.gp_smb_conf_ext import gp_smb_conf_ext
39 from samba.gp_msgs_ext import gp_msgs_ext
40 from samba.vgp_symlink_ext import vgp_symlink_ext
41 from samba.vgp_files_ext import vgp_files_ext
42 from samba.vgp_openssh_ext import vgp_openssh_ext
43 from samba.vgp_motd_ext import vgp_motd_ext
44 from samba.vgp_issue_ext import vgp_issue_ext
45 from samba.vgp_startup_scripts_ext import vgp_startup_scripts_ext
46 from samba.vgp_access_ext import vgp_access_ext
47 from samba.gp_gnome_settings_ext import gp_gnome_settings_ext
48 from samba.gp_cert_auto_enroll_ext import gp_cert_auto_enroll_ext
49 from samba.gp_firefox_ext import gp_firefox_ext
50 from samba.gp_chromium_ext import gp_chromium_ext, gp_chrome_ext
51 from samba.gp_firewalld_ext import gp_firewalld_ext
52 from samba.credentials import Credentials
53 import logging
54
55 if __name__ == "__main__":
56     parser = optparse.OptionParser('samba-gpupdate [options]')
57     sambaopts = options.SambaOptions(parser)
58
59     # Get the command line options
60     parser.add_option_group(sambaopts)
61     parser.add_option_group(options.VersionOptions(parser))
62     credopts = options.CredentialsOptions(parser)
63     parser.add_option('-X', '--unapply', help='Unapply Group Policy',
64                       action='store_true')
65     parser.add_option('--target', default='Computer', help='{Computer | User}',
66                       choices=['Computer', 'User'])
67     parser.add_option('--force', help='Reapplies all policy settings',
68                       action='store_true')
69     parser.add_option('--rsop', help='Print the Resultant Set of Policy',
70                       action='store_true')
71     parser.add_option_group(credopts)
72
73     # Set the options and the arguments
74     (opts, args) = parser.parse_args()
75
76     # Set the loadparm context
77     lp = sambaopts.get_loadparm()
78
79     creds = credopts.get_credentials(lp, fallback_machine=True)
80     # Apply policy to the command line specified user
81     if opts.target == 'Computer':
82         username = creds.get_username()
83     elif opts.target == 'User':
84         username = '%s\\%s' % (creds.get_domain(), creds.get_username())
85     # Always supply the machine creds for fetching the gpo list
86     creds = Credentials()
87     creds.guess(lp)
88     creds.set_machine_account(lp)
89
90     # Set up logging
91     logger = logging.getLogger('samba-gpupdate')
92     logger.addHandler(logging.StreamHandler(sys.stdout))
93     logger.setLevel(logging.CRITICAL)
94     log_level = lp.log_level()
95     if log_level == 1:
96         logger.setLevel(logging.ERROR)
97     elif log_level == 2:
98         logger.setLevel(logging.WARNING)
99     elif log_level == 3:
100         logger.setLevel(logging.INFO)
101     elif log_level >= 4:
102         logger.setLevel(logging.DEBUG)
103
104     cache_dir = lp.get('cache directory')
105     store = GPOStorage(os.path.join(cache_dir, 'gpo.tdb'))
106
107     machine_exts, user_exts = get_gp_client_side_extensions(logger,
108                                                             lp.configfile)
109     gp_extensions = []
110     if opts.target == 'Computer':
111         gp_extensions.append(gp_access_ext)
112         gp_extensions.append(gp_krb_ext)
113         gp_extensions.append(gp_scripts_ext)
114         gp_extensions.append(gp_sudoers_ext)
115         gp_extensions.append(vgp_sudoers_ext)
116         gp_extensions.append(gp_smb_conf_ext)
117         gp_extensions.append(gp_msgs_ext)
118         gp_extensions.append(vgp_symlink_ext)
119         gp_extensions.append(vgp_files_ext)
120         gp_extensions.append(vgp_openssh_ext)
121         gp_extensions.append(vgp_motd_ext)
122         gp_extensions.append(vgp_issue_ext)
123         gp_extensions.append(vgp_startup_scripts_ext)
124         gp_extensions.append(vgp_access_ext)
125         gp_extensions.append(gp_gnome_settings_ext)
126         gp_extensions.append(gp_cert_auto_enroll_ext)
127         gp_extensions.append(gp_firefox_ext)
128         gp_extensions.append(gp_chromium_ext)
129         gp_extensions.append(gp_chrome_ext)
130         gp_extensions.append(gp_firewalld_ext)
131         gp_extensions.extend(machine_exts)
132     elif opts.target == 'User':
133         gp_extensions.append(gp_user_scripts_ext)
134         gp_extensions.extend(user_exts)
135
136     if opts.rsop:
137         rsop(lp, creds, logger, store, gp_extensions, username, opts.target)
138     elif not opts.unapply:
139         apply_gp(lp, creds, logger, store, gp_extensions, username,
140                  opts.target, opts.force)
141     else:
142         unapply_gp(lp, creds, logger, store, gp_extensions, username,
143                    opts.target)
144