s3: libsmbclient: Fix smbc_stat() to return ENOENT on a non-existent file.
[Samba.git] / source4 / scripting / bin / samba-gpupdate
blob4b3f057f534b7c1bb1e15a8a423af90ea485bc95
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
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.
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.
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/>.
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'''
25 import os
26 import sys
28 sys.path.insert(0, "bin/python")
30 import optparse
31 from samba import getopt as options
32 from samba.gp.gpclass import apply_gp, unapply_gp, GPOStorage, rsop
33 from samba.gp.gp_sec_ext import gp_krb_ext, gp_access_ext
34 from samba.gp.gp_ext_loader import get_gp_client_side_extensions
35 from samba.gp.gp_scripts_ext import gp_scripts_ext, gp_user_scripts_ext
36 from samba.gp.gp_sudoers_ext import gp_sudoers_ext
37 from samba.gp.vgp_sudoers_ext import vgp_sudoers_ext
38 from samba.gp.gp_smb_conf_ext import gp_smb_conf_ext
39 from samba.gp.gp_msgs_ext import gp_msgs_ext
40 from samba.gp.vgp_symlink_ext import vgp_symlink_ext
41 from samba.gp.vgp_files_ext import vgp_files_ext
42 from samba.gp.vgp_openssh_ext import vgp_openssh_ext
43 from samba.gp.vgp_motd_ext import vgp_motd_ext
44 from samba.gp.vgp_issue_ext import vgp_issue_ext
45 from samba.gp.vgp_startup_scripts_ext import vgp_startup_scripts_ext
46 from samba.gp.vgp_access_ext import vgp_access_ext
47 from samba.gp.gp_gnome_settings_ext import gp_gnome_settings_ext
48 from samba.gp.gp_cert_auto_enroll_ext import gp_cert_auto_enroll_ext
49 from samba.gp.gp_firefox_ext import gp_firefox_ext
50 from samba.gp.gp_chromium_ext import gp_chromium_ext, gp_chrome_ext
51 from samba.gp.gp_firewalld_ext import gp_firewalld_ext
52 from samba.gp.gp_centrify_sudoers_ext import gp_centrify_sudoers_ext
53 from samba.gp.gp_centrify_crontab_ext import gp_centrify_crontab_ext, \
54                                              gp_user_centrify_crontab_ext
55 from samba.credentials import Credentials
56 from samba.gp.util.logging import logger_init
58 if __name__ == "__main__":
59     parser = optparse.OptionParser('samba-gpupdate [options]')
60     sambaopts = options.Samba3Options(parser)
62     # Get the command line options
63     parser.add_option_group(sambaopts)
64     parser.add_option_group(options.VersionOptions(parser))
65     credopts = options.CredentialsOptions(parser)
66     parser.add_option('-X', '--unapply', help='Unapply Group Policy',
67                       action='store_true')
68     parser.add_option('--target', default='Computer', help='{Computer | User}',
69                       choices=['Computer', 'User'])
70     parser.add_option('--force', help='Reapplies all policy settings',
71                       action='store_true')
72     parser.add_option('--rsop', help='Print the Resultant Set of Policy',
73                       action='store_true')
74     parser.add_option_group(credopts)
76     # Set the options and the arguments
77     (opts, args) = parser.parse_args()
79     # Set the loadparm context
80     lp = sambaopts.get_loadparm()
82     creds = credopts.get_credentials(lp, fallback_machine=True)
83     # Apply policy to the command line specified user
84     if opts.target == 'Computer':
85         username = creds.get_username()
86     elif opts.target == 'User':
87         username = '%s\\%s' % (creds.get_domain(), creds.get_username())
88     # Always supply the machine creds for fetching the gpo list
89     creds = Credentials()
90     creds.guess(lp)
91     creds.set_machine_account(lp)
93     # Set up logging
94     logger_init('samba-gpupdate', lp.log_level())
96     cache_dir = lp.get('cache directory')
97     store = GPOStorage(os.path.join(cache_dir, 'gpo.tdb'))
99     machine_exts, user_exts = get_gp_client_side_extensions(lp.configfile)
100     gp_extensions = []
101     if opts.target == 'Computer':
102         gp_extensions.append(gp_access_ext)
103         gp_extensions.append(gp_krb_ext)
104         gp_extensions.append(gp_scripts_ext)
105         gp_extensions.append(gp_sudoers_ext)
106         gp_extensions.append(vgp_sudoers_ext)
107         gp_extensions.append(gp_centrify_sudoers_ext)
108         gp_extensions.append(gp_centrify_crontab_ext)
109         gp_extensions.append(gp_smb_conf_ext)
110         gp_extensions.append(gp_msgs_ext)
111         gp_extensions.append(vgp_symlink_ext)
112         gp_extensions.append(vgp_files_ext)
113         gp_extensions.append(vgp_openssh_ext)
114         gp_extensions.append(vgp_motd_ext)
115         gp_extensions.append(vgp_issue_ext)
116         gp_extensions.append(vgp_startup_scripts_ext)
117         gp_extensions.append(vgp_access_ext)
118         gp_extensions.append(gp_gnome_settings_ext)
119         gp_extensions.append(gp_cert_auto_enroll_ext)
120         gp_extensions.append(gp_firefox_ext)
121         gp_extensions.append(gp_chromium_ext)
122         gp_extensions.append(gp_chrome_ext)
123         gp_extensions.append(gp_firewalld_ext)
124         gp_extensions.extend(machine_exts)
125     elif opts.target == 'User':
126         gp_extensions.append(gp_user_scripts_ext)
127         gp_extensions.append(gp_user_centrify_crontab_ext)
128         gp_extensions.extend(user_exts)
130     if opts.rsop:
131         rsop(lp, creds, store, gp_extensions, username, opts.target)
132     elif not opts.unapply:
133         apply_gp(lp, creds, store, gp_extensions, username,
134                  opts.target, opts.force)
135     else:
136         unapply_gp(lp, creds, store, gp_extensions, username,
137                    opts.target)