Added a net acl ds command for modification of ACLs on directory objects
[kamenim/samba.git] / source4 / scripting / python / samba / netcmd / ntacl.py
1 #!/usr/bin/python
2 #
3 # Manipulate file NT ACLs
4 #
5 # Copyright Matthieu Patou 2010 <mat@matws.net>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 from samba.credentials import DONT_USE_KERBEROS
22 import samba.getopt as options
23 from samba.dcerpc import security
24 from samba.ntacls import setntacl, getntacl
25 from samba import Ldb
26 from samba.ndr import ndr_unpack
27
28 from ldb import SCOPE_BASE
29 import os
30
31 from samba.auth import system_session
32 from samba.netcmd import (
33     Command,
34     CommandError,
35     SuperCommand,
36     Option,
37     )
38
39 class cmd_acl_set(Command):
40     """Set ACLs on a file"""
41     synopsis = "%prog set <acl> <file> [--xattr-backend=native|tdb] [--eadb-file=file] [options]"
42
43     takes_optiongroups = {
44         "sambaopts": options.SambaOptions,
45         "credopts": options.CredentialsOptions,
46         "versionopts": options.VersionOptions,
47         }
48
49     takes_options = [
50         Option("--quiet", help="Be quiet", action="store_true"),
51         Option("--xattr-backend", type="choice", help="xattr backend type (native fs or tdb)",
52                choices=["native","tdb"]),
53         Option("--eadb-file", help="Name of the tdb file where attributes are stored", type="string"),
54         ]
55
56     takes_args = ["acl","file"]
57
58     def run(self, acl, file, quiet=False,xattr_backend=None,eadb_file=None,
59             credopts=None, sambaopts=None, versionopts=None):
60         lp = sambaopts.get_loadparm()
61         creds = credopts.get_credentials(lp)
62         path = os.path.join(lp.get("private dir"), lp.get("secrets database") or "secrets.ldb")
63         creds = credopts.get_credentials(lp)
64         creds.set_kerberos_state(DONT_USE_KERBEROS)
65         try:
66             ldb = Ldb(path, session_info=system_session(), credentials=creds,lp=lp)
67         except:
68             # XXX: Should catch a particular exception type
69             raise CommandError("Unable to read domain SID from configuration files")
70         attrs = ["objectSid"]
71         print lp.get("realm")
72         res = ldb.search(expression="(objectClass=*)",
73             base="flatname=%s,cn=Primary Domains" % lp.get("workgroup"),
74             scope=SCOPE_BASE, attrs=attrs)
75         if len(res) !=0:
76             domainsid = ndr_unpack(security.dom_sid, res[0]["objectSid"][0])
77             setntacl(lp, file, acl, str(domainsid), xattr_backend, eadb_file)
78         else:
79             raise CommandError("Unable to read domain SID from configuration files")
80
81
82 class cmd_acl_get(Command):
83     """Set ACLs on a file"""
84     synopsis = "%prog get <file> [--as-sddl] [--xattr-backend=native|tdb] [--eadb-file=file] [options]"
85
86     takes_optiongroups = {
87         "sambaopts": options.SambaOptions,
88         "credopts": options.CredentialsOptions,
89         "versionopts": options.VersionOptions,
90         }
91
92     takes_options = [
93         Option("--as-sddl", help="Output ACL in the SDDL format", action="store_true"),
94         Option("--xattr-backend", type="choice", help="xattr backend type (native fs or tdb)",
95                choices=["native","tdb"]),
96         Option("--eadb-file", help="Name of the tdb file where attributes are stored", type="string"),
97         ]
98
99     takes_args = ["file"]
100
101     def run(self, file, as_sddl=False, xattr_backend=None, eadb_file=None,
102             credopts=None, sambaopts=None, versionopts=None):
103         lp = sambaopts.get_loadparm()
104         creds = credopts.get_credentials(lp)
105         acl = getntacl(lp, file, xattr_backend, eadb_file)
106         if as_sddl:
107             anysid = security.dom_sid(security.SID_NT_SELF)
108             print acl.info.as_sddl(anysid)
109         else:
110             acl.dump()
111
112
113 class cmd_nt_acl(SuperCommand):
114     """NT ACLs manipulation"""
115
116     subcommands = {}
117     subcommands["set"] = cmd_acl_set()
118     subcommands["get"] = cmd_acl_get()
119