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'''
28 sys.path.insert(0, "bin/python")
31 from samba import getopt as options
32 from samba.auth import system_session
34 from samba.samdb import SamDB
37 from samba.gpclass import apply_gp, unapply_gp, GPOStorage
38 from samba.gp_sec_ext import gp_sec_ext
41 if __name__ == "__main__":
42 parser = optparse.OptionParser('samba_gpoupdate [options]')
43 sambaopts = options.SambaOptions(parser)
45 # Get the command line options
46 parser.add_option_group(sambaopts)
47 parser.add_option_group(options.VersionOptions(parser))
48 credopts = options.CredentialsOptions(parser)
49 parser.add_option('-H', '--url', dest='url', help='URL for the samdb')
50 parser.add_option('-X', '--unapply', help='Unapply Group Policy',
52 parser.add_option('-M', '--machine', help='Apply machine policy',
53 action='store_true', default=False)
54 parser.add_option_group(credopts)
56 # Set the options and the arguments
57 (opts, args) = parser.parse_args()
59 # Set the loadparm context
60 lp = sambaopts.get_loadparm()
66 # Initialize the session
67 creds = credopts.get_credentials(lp, fallback_machine=True)
68 session = system_session()
71 logger = logging.getLogger('samba_gpoupdate')
72 logger.addHandler(logging.StreamHandler(sys.stdout))
73 logger.setLevel(logging.CRITICAL)
74 log_level = lp.log_level()
76 logger.setLevel(logging.ERROR)
78 logger.setLevel(logging.WARNING)
80 logger.setLevel(logging.INFO)
82 logger.setLevel(logging.DEBUG)
84 cache_dir = lp.get('cache directory')
85 store = GPOStorage(os.path.join(cache_dir, 'gpo.tdb'))
89 if lp.get('server role') == 'active directory domain controller':
90 gp_extensions.append(gp_sec_ext(logger))
92 pass # User extensions
94 # Get a live instance of Samba
96 test_ldb = SamDB(url, session_info=session, credentials=creds, lp=lp)
101 apply_gp(lp, creds, test_ldb, logger, store, gp_extensions)
103 unapply_gp(lp, creds, test_ldb, logger, store, gp_extensions)