#!/usr/bin/env python3 # Copyright Luke Morrison July 2013 # Co-Edited by Matthieu Pattou July 2013 from original August 2013 # Edited by Garming Sam Feb. 2014 # Edited by Luke Morrison April 2014 # Edited by David Mulder May 2017 # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . '''This script reads a log file of previous GPO, gets all GPO from sysvol and sorts them by container. Then, it applies the ones that haven't been applied, have changed, or is in the right container''' import os import sys sys.path.insert(0, "bin/python") import optparse from samba import getopt as options from samba.gpclass import apply_gp, unapply_gp, GPOStorage, rsop from samba.gp_sec_ext import gp_krb_ext, gp_access_ext from samba.gp_ext_loader import get_gp_client_side_extensions from samba.gp_scripts_ext import gp_scripts_ext from samba.gp_sudoers_ext import gp_sudoers_ext from samba.vgp_sudoers_ext import vgp_sudoers_ext from samba.gp_smb_conf_ext import gp_smb_conf_ext from samba.gp_msgs_ext import gp_msgs_ext import logging if __name__ == "__main__": parser = optparse.OptionParser('samba-gpupdate [options]') sambaopts = options.SambaOptions(parser) # Get the command line options parser.add_option_group(sambaopts) parser.add_option_group(options.VersionOptions(parser)) credopts = options.CredentialsOptions(parser) parser.add_option('-X', '--unapply', help='Unapply Group Policy', action='store_true') parser.add_option('--target', default='Computer', help='{Computer | User}', choices=['Computer', 'User']) parser.add_option('--force', help='Reapplies all policy settings', action='store_true') parser.add_option('--rsop', help='Print the Resultant Set of Policy', action='store_true') parser.add_option_group(credopts) # Set the options and the arguments (opts, args) = parser.parse_args() # Set the loadparm context lp = sambaopts.get_loadparm() creds = credopts.get_credentials(lp, fallback_machine=True) # Set up logging logger = logging.getLogger('samba-gpupdate') logger.addHandler(logging.StreamHandler(sys.stdout)) logger.setLevel(logging.CRITICAL) log_level = lp.log_level() if log_level == 1: logger.setLevel(logging.ERROR) elif log_level == 2: logger.setLevel(logging.WARNING) elif log_level == 3: logger.setLevel(logging.INFO) elif log_level >= 4: logger.setLevel(logging.DEBUG) cache_dir = lp.get('cache directory') store = GPOStorage(os.path.join(cache_dir, 'gpo.tdb')) machine_exts, user_exts = get_gp_client_side_extensions(logger, lp.configfile) gp_extensions = [] if opts.target == 'Computer': gp_extensions.append(gp_access_ext) gp_extensions.append(gp_krb_ext) gp_extensions.append(gp_scripts_ext) gp_extensions.append(gp_sudoers_ext) gp_extensions.append(vgp_sudoers_ext) gp_extensions.append(gp_smb_conf_ext) gp_extensions.append(gp_msgs_ext) gp_extensions.extend(machine_exts) elif opts.target == 'User': gp_extensions.extend(user_exts) if opts.rsop: rsop(lp, creds, logger, store, gp_extensions, opts.target) elif not opts.unapply: apply_gp(lp, creds, logger, store, gp_extensions, opts.force) else: unapply_gp(lp, creds, logger, store, gp_extensions)