s4-samba-tool: started on gpo subcommands in python
[kai/samba.git] / source4 / scripting / python / samba / netcmd / gpo.py
1 #!/usr/bin/env python
2 #
3 # implement samba_tool gpo commands
4 #
5 # Copyright Andrew Tridgell 2010
6 #
7 # based on C implementation by Guenther Deschner and Wilco Baan Hofman
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22
23 import samba.getopt as options
24 import ldb
25
26 from samba.auth import system_session
27 from samba.netcmd import (
28     Command,
29     CommandError,
30     Option,
31     SuperCommand,
32     )
33 from samba.samdb import SamDB
34 from samba import drs_utils, nttime2string, dsdb
35 from samba.dcerpc import misc
36
37
38 def samdb_connect(ctx):
39     '''make a ldap connection to the server'''
40     try:
41         ctx.samdb = SamDB(url=ctx.url,
42                           session_info=system_session(),
43                           credentials=ctx.creds, lp=ctx.lp)
44     except Exception, estr:
45         raise CommandError("LDAP connection to %s failed - %s" % (ctx.url, estr))
46
47
48 def attr_default(msg, attrname, default):
49     '''get an attribute from a ldap msg with a default'''
50     if attrname in msg:
51         return msg[attrname][0]
52     return default
53
54
55 def flags_string(flags, value):
56     '''return a set of flags as a string'''
57     if value == 0:
58         return 'NONE'
59     ret = ''
60     for (str, val) in flags:
61         if val & value:
62             ret += str + ' '
63             value &= ~val
64     if value != 0:
65         ret += '0x%08x' % value
66     return ret.rstrip()
67
68
69 class cmd_listall(Command):
70     """list all GPOs"""
71
72     synopsis = "%prog gpo listall"
73
74     takes_optiongroups = {
75         "sambaopts": options.SambaOptions,
76         "versionopts": options.VersionOptions,
77         "credopts": options.CredentialsOptions,
78     }
79
80     takes_options = [
81         Option("-H", help="LDB URL for database or target server", type=str)
82         ]
83
84     def run(self, H=None, sambaopts=None,
85             credopts=None, versionopts=None, server=None):
86
87         self.url = H
88         self.lp = sambaopts.get_loadparm()
89
90         self.creds = credopts.get_credentials(self.lp)
91         if not self.creds.authentication_requested():
92             self.creds.set_machine_account(self.lp)
93
94         samdb_connect(self)
95
96         policies_dn = self.samdb.get_default_basedn()
97         policies_dn.add_child(ldb.Dn(self.samdb, "CN=Policies,CN=System"))
98
99         gpo_flags = [
100             ("GPO_FLAG_USER_DISABLE", dsdb.GPO_FLAG_USER_DISABLE ),
101             ( "GPO_FLAG_MACHINE_DISABLE", dsdb.GPO_FLAG_MACHINE_DISABLE ) ]
102
103         msg = self.samdb.search(base=policies_dn, scope=ldb.SCOPE_ONELEVEL,
104                                 expression="(objectClass=groupPolicyContainer)",
105                                 attrs=['nTSecurityDescriptor', 'versionNumber', 'flags', 'name', 'displayName', 'gPCFileSysPath'])
106         for m in msg:
107             print("GPO          : %s" % m['name'][0])
108             print("display name : %s" % m['displayName'][0])
109             print("path         : %s" % m['gPCFileSysPath'][0])
110             print("dn           : %s" % m.dn)
111             print("version      : %s" % attr_default(m, 'version', '0'))
112             print("flags        : %s" % flags_string(gpo_flags, int(attr_default(m, 'flags', 0))))
113             print("")
114
115
116 class cmd_gpo(SuperCommand):
117     """GPO commands"""
118
119     subcommands = {}
120     subcommands["listall"] = cmd_listall()