gpupdate: Remove the unnecessary url parameter
[samba.git] / source4 / scripting / bin / samba-gpupdate
1 #!/usr/bin/env python
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
33 from samba.gp_sec_ext import gp_sec_ext
34 from samba.gp_ext_loader import get_gp_client_side_extensions
35 import logging
36
37 if __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_group(credopts)
50
51     # Set the options and the arguments
52     (opts, args) = parser.parse_args()
53
54     # Set the loadparm context
55     lp = sambaopts.get_loadparm()
56
57     creds = credopts.get_credentials(lp, fallback_machine=True)
58
59     # Set up logging
60     logger = logging.getLogger('samba-gpupdate')
61     logger.addHandler(logging.StreamHandler(sys.stdout))
62     logger.setLevel(logging.CRITICAL)
63     log_level = lp.log_level()
64     if log_level == 1:
65         logger.setLevel(logging.ERROR)
66     elif log_level == 2:
67         logger.setLevel(logging.WARNING)
68     elif log_level == 3:
69         logger.setLevel(logging.INFO)
70     elif log_level >= 4:
71         logger.setLevel(logging.DEBUG)
72
73     cache_dir = lp.get('cache directory')
74     store = GPOStorage(os.path.join(cache_dir, 'gpo.tdb'))
75
76     machine_exts, user_exts = get_gp_client_side_extensions(logger,
77                                                             lp.configfile)
78     gp_extensions = []
79     if opts.target == 'Computer':
80         gp_extensions.append(gp_sec_ext(logger))
81         for ext in machine_exts:
82             gp_extensions.append(ext(logger))
83     elif opts.target == 'User':
84         for ext in user_exts:
85             gp_extensions.append(ext(logger))
86
87     if not opts.unapply:
88         apply_gp(lp, creds, logger, store, gp_extensions)
89     else:
90         unapply_gp(lp, creds, logger, store, gp_extensions)
91