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 22and sorts them by container. Then, it applies the ones that haven't been 23applied, have changed, or is in the right container''' 24 25import os 26import sys 27 28sys.path.insert(0, "bin/python") 29 30import optparse 31from samba import getopt as options 32from samba.gpclass import apply_gp, unapply_gp, GPOStorage 33from samba.gp_sec_ext import gp_sec_ext 34from samba.gp_ext_loader import get_gp_client_side_extensions 35import logging 36 37if __name__ == "__main__": 38 parser = optparse.OptionParser('samba-gpupdate [options]') 39 sambaopts = options.SambaOptions(parser) 40 41 # Get the command line options 42 parser.add_option_group(sambaopts) 43 parser.add_option_group(options.VersionOptions(parser)) 44 credopts = options.CredentialsOptions(parser) 45 parser.add_option('-X', '--unapply', help='Unapply Group Policy', 46 action='store_true') 47 parser.add_option('--target', default='Computer', help='{Computer | User}', 48 choices=['Computer', 'User']) 49 parser.add_option('--force', help='Reapplies all policy settings', 50 action='store_true') 51 parser.add_option_group(credopts) 52 53 # Set the options and the arguments 54 (opts, args) = parser.parse_args() 55 56 # Set the loadparm context 57 lp = sambaopts.get_loadparm() 58 59 creds = credopts.get_credentials(lp, fallback_machine=True) 60 61 # Set up logging 62 logger = logging.getLogger('samba-gpupdate') 63 logger.addHandler(logging.StreamHandler(sys.stdout)) 64 logger.setLevel(logging.CRITICAL) 65 log_level = lp.log_level() 66 if log_level == 1: 67 logger.setLevel(logging.ERROR) 68 elif log_level == 2: 69 logger.setLevel(logging.WARNING) 70 elif log_level == 3: 71 logger.setLevel(logging.INFO) 72 elif log_level >= 4: 73 logger.setLevel(logging.DEBUG) 74 75 cache_dir = lp.get('cache directory') 76 store = GPOStorage(os.path.join(cache_dir, 'gpo.tdb')) 77 78 machine_exts, user_exts = get_gp_client_side_extensions(logger, 79 lp.configfile) 80 gp_extensions = [] 81 if opts.target == 'Computer': 82 gp_extensions.append(gp_sec_ext(logger, lp, creds, store)) 83 for ext in machine_exts: 84 gp_extensions.append(ext(logger, lp, creds, store)) 85 elif opts.target == 'User': 86 for ext in user_exts: 87 gp_extensions.append(ext(logger, lp, creds, store)) 88 89 if not opts.unapply: 90 apply_gp(lp, creds, logger, store, gp_extensions, opts.force) 91 else: 92 unapply_gp(lp, creds, logger, store, gp_extensions) 93 94