MAN: Adding entry for net ads lookup
[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.auth import system_session
33 try:
34     from samba.samdb import SamDB
35 except:
36     SamDB = None
37 from samba.gpclass import apply_gp, unapply_gp, GPOStorage
38 from samba.gp_sec_ext import gp_sec_ext
39 import logging
40
41 if __name__ == "__main__":
42     parser = optparse.OptionParser('samba-gpupdate [options]')
43     sambaopts = options.SambaOptions(parser)
44
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',
51                       action='store_true')
52     parser.add_option('-M', '--machine', help='Apply machine policy',
53                       action='store_true', default=False)
54     parser.add_option_group(credopts)
55
56     # Set the options and the arguments
57     (opts, args) = parser.parse_args()
58
59     # Set the loadparm context
60     lp = sambaopts.get_loadparm()
61     if not opts.url:
62         url = lp.samdb_url()
63     else:
64         url = opts.url
65
66     # Initialize the session
67     creds = credopts.get_credentials(lp, fallback_machine=True)
68     session = system_session()
69
70     # Set up logging
71     logger = logging.getLogger('samba-gpupdate')
72     logger.addHandler(logging.StreamHandler(sys.stdout))
73     logger.setLevel(logging.CRITICAL)
74     log_level = lp.log_level()
75     if log_level == 1:
76         logger.setLevel(logging.ERROR)
77     elif log_level == 2:
78         logger.setLevel(logging.WARNING)
79     elif log_level == 3:
80         logger.setLevel(logging.INFO)
81     elif log_level >= 4:
82         logger.setLevel(logging.DEBUG)
83
84     cache_dir = lp.get('cache directory')
85     store = GPOStorage(os.path.join(cache_dir, 'gpo.tdb'))
86
87     gp_extensions = []
88     if opts.machine:
89         if lp.get('server role') == 'active directory domain controller':
90             gp_extensions.append(gp_sec_ext(logger))
91     else:
92         pass # User extensions
93
94     # Get a live instance of Samba
95     if SamDB:
96         test_ldb = SamDB(url, session_info=session, credentials=creds, lp=lp)
97     else:
98         test_ldb = None
99
100     if not opts.unapply:
101         apply_gp(lp, creds, test_ldb, logger, store, gp_extensions)
102     else:
103         unapply_gp(lp, creds, test_ldb, logger, store, gp_extensions)
104