summaryrefslogtreecommitdiff
path: root/source4/scripting/python/samba/__init__.py
blob: c3446146a7a3d4a3597eb2d1e5ac2b69e92b9dcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/python

# Unix SMB/CIFS implementation.
# Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
#
# Based on the original in EJS:
# Copyright (C) Andrew Tridgell <tridge@samba.org> 2005
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

"""Samba 4."""

__docformat__ = "restructuredText"

import os

def _in_source_tree():
    """Check whether the script is being run from the source dir. """
    return os.path.exists("%s/../../../selftest/skip" % os.path.dirname(__file__))


# When running, in-tree, make sure bin/python is in the PYTHONPATH
if _in_source_tree():
    import sys
    srcdir = "%s/../../.." % os.path.dirname(__file__)
    sys.path.append("%s/bin/python" % srcdir)
    default_ldb_modules_dir = "%s/bin/modules/ldb" % srcdir
else:
    default_ldb_modules_dir = None


import ldb
import glue

class Ldb(ldb.Ldb):
    """Simple Samba-specific LDB subclass that takes care
    of setting up the modules dir, credentials pointers, etc.

    Please note that this is intended to be for all Samba LDB files,
    not necessarily the Sam database. For Sam-specific helper
    functions see samdb.py.
    """
    def __init__(self, url=None, lp=None, modules_dir=None, session_info=None,
                 credentials=None, flags=0, options=None):
        """Opens a Samba Ldb file.

        :param url: Optional LDB URL to open
        :param lp: Optional loadparm object
        :param modules_dir: Optional modules directory
        :param session_info: Optional session information
        :param credentials: Optional credentials, defaults to anonymous.
        :param flags: Optional LDB flags
        :param options: Additional options (optional)

        This is different from a regular Ldb file in that the Samba-specific
        modules-dir is used by default and that credentials and session_info
        can be passed through (required by some modules).
        """

        if modules_dir is not None:
            self.set_modules_dir(modules_dir)
        elif default_ldb_modules_dir is not None:
            self.set_modules_dir(default_ldb_modules_dir)
        elif lp is not None:
            self.set_modules_dir(os.path.join(lp.get("modules dir"), "ldb"))

        if session_info is not None:
            self.set_session_info(session_info)

        if credentials is not None:
            self.set_credentials(credentials)

        if lp is not None:
            self.set_loadparm(lp)

        # This must be done before we load the schema, as these handlers for
        # objectSid and objectGUID etc must take precedence over the 'binary
        # attribute' declaration in the schema
        glue.ldb_register_samba_handlers(self)

        # TODO set debug
        def msg(l,text):
            print text
        #self.set_debug(msg)

        glue.ldb_set_utf8_casefold(self)

        # Allow admins to force non-sync ldb for all databases
        if lp is not None:
            nosync_p = lp.get("nosync", "ldb")
            if nosync_p is not None and nosync_p == True:
                flags |= FLG_NOSYNC

        self.set_create_perms()

        if url is not None:
            self.connect(url, flags, options)

    def set_session_info(self, session_info):
        glue.ldb_set_session_info(self, session_info)

    def set_credentials(self, credentials):
        glue.ldb_set_credentials(self, credentials)

    def set_loadparm(self, lp_ctx):
        glue.ldb_set_loadparm(self, lp_ctx)

    def set_create_perms(self, perms=0600):
        # we usually want Samba databases to be private. If we later find we
        # need one public, we will have to change this here
        super(Ldb, self).set_create_perms(perms)

    def searchone(self, attribute, basedn=None, expression=None,
                  scope=ldb.SCOPE_BASE):
        """Search for one attribute as a string.

        :param basedn: BaseDN for the search.
        :param attribute: Name of the attribute
        :param expression: Optional search expression.
        :param scope: Search scope (defaults to base).
        :return: Value of attribute as a string or None if it wasn't found.
        """
        res = self.search(basedn, scope, expression, [attribute])
        if len(res) != 1 or res[0][attribute] is None:
            return None
        values = set(res[0][attribute])
        assert len(values) == 1
        return self.schema_format_value(attribute, values.pop())

    def erase_users_computers(self, dn):
        """Erases user and computer objects from our AD. This is needed since the 'samldb' module denies the deletion of primary groups. Therefore all groups shouldn't be primary somewhere anymore."""

        try:
            res = self.search(base=dn, scope=ldb.SCOPE_SUBTREE, attrs=[],
                      expression="(|(objectclass=user)(objectclass=computer))")
        except ldb.LdbError, (errno, _):
            if errno == ldb.ERR_NO_SUCH_OBJECT:
                # Ignore no such object errors
                return
            else:
                raise

        try:
            for msg in res:
                self.delete(msg.dn)
        except ldb.LdbError, (errno, _):
            if errno != ldb.ERR_NO_SUCH_OBJECT:
                # Ignore no such object errors
                raise

    def erase_except_schema_controlled(self):
        """Erase this ldb, removing all records, except those that are controlled by Samba4's schema."""

        basedn = ""

        # Try to delete user/computer accounts to allow deletion of groups
        self.erase_users_computers(basedn)

        # Delete the 'visible' records, and the invisble 'deleted' records (if this DB supports it)
        for msg in self.search(basedn, ldb.SCOPE_SUBTREE,
                               "(&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))",
                               [], controls=["show_deleted:0"]):
            try:
                self.delete(msg.dn)
            except ldb.LdbError, (errno, _):
                if errno != ldb.ERR_NO_SUCH_OBJECT:
                    # Ignore no such object errors
                    raise

        res = self.search(basedn, ldb.SCOPE_SUBTREE,
                          "(&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))",
                          [], controls=["show_deleted:0"])
        assert len(res) == 0

        # delete the specials
        for attr in ["@SUBCLASSES", "@MODULES",
                     "@OPTIONS", "@PARTITION", "@KLUDGEACL"]:
            try:
                self.delete(attr)
            except ldb.LdbError, (errno, _):
                if errno != ldb.ERR_NO_SUCH_OBJECT:
                    # Ignore missing dn errors
                    raise

    def erase(self):
        """Erase this ldb, removing all records."""

        self.erase_except_schema_controlled()

        # delete the specials
        for attr in ["@INDEXLIST", "@ATTRIBUTES"]:
            try:
                self.delete(attr)
            except ldb.LdbError, (errno, _):
                if errno != ldb.ERR_NO_SUCH_OBJECT
                    # Ignore missing dn errors
                    raise

    def erase_partitions(self):
        """Erase an ldb, removing all records."""

        def erase_recursive(self, dn):
            try:
                res = self.search(base=dn, scope=ldb.SCOPE_ONELEVEL, attrs=[],
                                  controls=["show_deleted:0"])
            except ldb.LdbError, (errno, _):
                if errno == ldb.ERR_NO_SUCH_OBJECT:
                    # Ignore no such object errors
                    return

            for msg in res:
                erase_recursive(self, msg.dn)

            try:
                self.delete(dn)
            except ldb.LdbError, (errno, _):
                if errno != ldb.ERR_NO_SUCH_OBJECT:
                    # Ignore no such object errors
                    raise

        res = self.search("", ldb.SCOPE_BASE, "(objectClass=*)",
                         ["namingContexts"])
        assert len(res) == 1
        if not "namingContexts" in res[0]:
            return
        for basedn in res[0]["namingContexts"]:
            # Try to delete user/computer accounts to allow deletion of groups
            self.erase_users_computers(basedn)
            # Try and erase from the bottom-up in the tree
            erase_recursive(self, basedn)

    def load_ldif_file_add(self, ldif_path):
        """Load a LDIF file.

        :param ldif_path: Path to LDIF file.
        """
        self.add_ldif(open(ldif_path, 'r').read())

    def add_ldif(self, ldif, controls=None):
        """Add data based on a LDIF string.

        :param ldif: LDIF text.
        """
        for changetype, msg in self.parse_ldif(ldif):
            assert changetype == ldb.CHANGETYPE_NONE
            self.add(msg,controls)

    def modify_ldif(self, ldif, controls=None):
        """Modify database based on a LDIF string.

        :param ldif: LDIF text.
        """
        for changetype, msg in self.parse_ldif(ldif):
            if (changetype == ldb.CHANGETYPE_ADD):
                self.add(msg, controls)
            else:
                self.modify(msg, controls)

    def set_domain_sid(self, sid):
        """Change the domain SID used by this LDB.

        :param sid: The new domain sid to use.
        """
        glue.samdb_set_domain_sid(self, sid)

    def domain_sid(self):
        """Read the domain SID used by this LDB.

        """
        glue.samdb_get_domain_sid(self)

    def set_schema_from_ldif(self, pf, df):
        glue.dsdb_set_schema_from_ldif(self, pf, df)

    def set_schema_from_ldb(self, ldb):
        glue.dsdb_set_schema_from_ldb(self, ldb)

    def write_prefixes_from_schema(self):
        glue.dsdb_write_prefixes_from_schema_to_ldb(self)

    def convert_schema_to_openldap(self, target, mapping):
        return glue.dsdb_convert_schema_to_openldap(self, target, mapping)

    def set_invocation_id(self, invocation_id):
        """Set the invocation id for this SamDB handle.

        :param invocation_id: GUID of the invocation id.
        """
        glue.dsdb_set_ntds_invocation_id(self, invocation_id)

    def get_invocation_id(self):
        "Get the invocation_id id"
        return glue.samdb_ntds_invocation_id(self)

    def get_ntds_GUID(self):
        "Get the NTDS objectGUID"
        return glue.samdb_ntds_objectGUID(self)

    def server_site_name(self):
        "Get the server site name"
        return glue.samdb_server_site_name(self)

    def set_opaque_integer(self, name, value):
        """Set an integer as an opaque (a flag or other value) value on the database

        :param name: The name for the opaque value
        :param value: The integer value
        """
        glue.dsdb_set_opaque_integer(self, name, value)


def substitute_var(text, values):
    """substitute strings of the form ${NAME} in str, replacing
    with substitutions from subobj.

    :param text: Text in which to subsitute.
    :param values: Dictionary with keys and values.
    """

    for (name, value) in values.items():
        assert isinstance(name, str), "%r is not a string" % name
        assert isinstance(value, str), "Value %r for %s is not a string" % (value, name)
        text = text.replace("${%s}" % name, value)

    return text


def check_all_substituted(text):
    """Make sure that all substitution variables in a string have been replaced.
    If not, raise an exception.

    :param text: The text to search for substitution variables
    """
    if not "${" in text:
        return

    var_start = text.find("${")
    var_end = text.find("}", var_start)

    raise Exception("Not all variables substituted: %s" % text[var_start:var_end+1])


def read_and_sub_file(file, subst_vars):
    """Read a file and sub in variables found in it

    :param file: File to be read (typically from setup directory)
     param subst_vars: Optional variables to subsitute in the file.
    """
    data = open(file, 'r').read()
    if subst_vars is not None:
        data = substitute_var(data, subst_vars)
        check_all_substituted(data)
    return data


def setup_file(template, fname, subst_vars=None):
    """Setup a file in the private dir.

    :param template: Path of the template file.
    :param fname: Path of the file to create.
    :param subst_vars: Substitution variables.
    """
    f = fname

    if os.path.exists(f):
        os.unlink(f)

    data = read_and_sub_file(template, subst_vars)
    open(f, 'w').write(data)


def valid_netbios_name(name):
    """Check whether a name is valid as a NetBIOS name. """
    # See crh's book (1.4.1.1)
    if len(name) > 15:
        return False
    for x in name:
        if not x.isalnum() and not x in " !#$%&'()-.@^_{}~":
            return False
    return True


version = glue.version

# "userAccountControl" flags
UF_NORMAL_ACCOUNT = glue.UF_NORMAL_ACCOUNT
UF_TEMP_DUPLICATE_ACCOUNT = glue.UF_TEMP_DUPLICATE_ACCOUNT
UF_SERVER_TRUST_ACCOUNT = glue.UF_SERVER_TRUST_ACCOUNT
UF_WORKSTATION_TRUST_ACCOUNT = glue.UF_WORKSTATION_TRUST_ACCOUNT
UF_INTERDOMAIN_TRUST_ACCOUNT = glue.UF_INTERDOMAIN_TRUST_ACCOUNT
UF_PASSWD_NOTREQD = glue.UF_PASSWD_NOTREQD
UF_ACCOUNTDISABLE = glue.UF_ACCOUNTDISABLE

# "groupType" flags
GTYPE_SECURITY_BUILTIN_LOCAL_GROUP = glue.GTYPE_SECURITY_BUILTIN_LOCAL_GROUP
GTYPE_SECURITY_GLOBAL_GROUP = glue.GTYPE_SECURITY_GLOBAL_GROUP
GTYPE_SECURITY_DOMAIN_LOCAL_GROUP = glue.GTYPE_SECURITY_DOMAIN_LOCAL_GROUP
GTYPE_SECURITY_UNIVERSAL_GROUP = glue.GTYPE_SECURITY_UNIVERSAL_GROUP
GTYPE_DISTRIBUTION_GLOBAL_GROUP = glue.GTYPE_DISTRIBUTION_GLOBAL_GROUP
GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP = glue.GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP
GTYPE_DISTRIBUTION_UNIVERSAL_GROUP = glue.GTYPE_DISTRIBUTION_UNIVERSAL_GROUP

# "sAMAccountType" flags
ATYPE_NORMAL_ACCOUNT = glue.ATYPE_NORMAL_ACCOUNT
ATYPE_WORKSTATION_TRUST = glue.ATYPE_WORKSTATION_TRUST
ATYPE_INTERDOMAIN_TRUST = glue.ATYPE_INTERDOMAIN_TRUST
ATYPE_SECURITY_GLOBAL_GROUP = glue.ATYPE_SECURITY_GLOBAL_GROUP
ATYPE_SECURITY_LOCAL_GROUP = glue.ATYPE_SECURITY_LOCAL_GROUP
ATYPE_SECURITY_UNIVERSAL_GROUP = glue.ATYPE_SECURITY_UNIVERSAL_GROUP
ATYPE_DISTRIBUTION_GLOBAL_GROUP = glue.ATYPE_DISTRIBUTION_GLOBAL_GROUP
ATYPE_DISTRIBUTION_LOCAL_GROUP = glue.ATYPE_DISTRIBUTION_LOCAL_GROUP
ATYPE_DISTRIBUTION_UNIVERSAL_GROUP = glue.ATYPE_DISTRIBUTION_UNIVERSAL_GROUP

# "domainFunctionality", "forestFunctionality" flags in the rootDSE */
DS_DOMAIN_FUNCTION_2000 = glue.DS_DOMAIN_FUNCTION_2000
DS_DOMAIN_FUNCTION_2003_MIXED = glue.DS_DOMAIN_FUNCTION_2003_MIXED
DS_DOMAIN_FUNCTION_2003 = glue.DS_DOMAIN_FUNCTION_2003
DS_DOMAIN_FUNCTION_2008 = glue.DS_DOMAIN_FUNCTION_2008
DS_DOMAIN_FUNCTION_2008_R2 = glue.DS_DOMAIN_FUNCTION_2008_R2

# "domainControllerFunctionality" flags in the rootDSE */
DS_DC_FUNCTION_2000 = glue.DS_DC_FUNCTION_2000
DS_DC_FUNCTION_2003 = glue.DS_DC_FUNCTION_2003
DS_DC_FUNCTION_2008 = glue.DS_DC_FUNCTION_2008
DS_DC_FUNCTION_2008_R2 = glue.DS_DC_FUNCTION_2008_R2

#LDAP_SERVER_SD_FLAGS_OID flags
SECINFO_OWNER = glue.SECINFO_OWNER
SECINFO_GROUP = glue.SECINFO_GROUP
SECINFO_DACL  = glue.SECINFO_DACL
SECINFO_SACL  = glue.SECINFO_SACL