r26477: Allow setting loadparm context for a ldb context in python, plus some other...
[ira/wip.git] / source / scripting / python / samba / __init__.py
1 #!/usr/bin/python
2
3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Andrew Tridgell <tridge@samba.org> 2005
5 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
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 import os
22
23 def _in_source_tree():
24     """Check whether the script is being run from the source dir. """
25     return os.path.exists("%s/../../../samba4-skip" % os.path.dirname(__file__))
26
27
28 # When running, in-tree, make sure bin/python is in the PYTHONPATH
29 if _in_source_tree():
30     import sys
31     srcdir = "%s/../../.." % os.path.dirname(__file__)
32     sys.path.append("%s/bin/python" % srcdir)
33     default_ldb_modules_dir = "%s/bin/modules/ldb" % srcdir
34
35
36 import misc
37 import ldb
38 ldb.ldb.set_credentials = misc.ldb_set_credentials
39 #FIXME: ldb.ldb.set_session_info = misc.ldb_set_session_info
40 ldb.ldb.set_loadparm = misc.ldb_set_loadparm
41
42 def Ldb(url, session_info=None, credentials=None, modules_dir=None, lp=None):
43     """Open a Samba Ldb file. 
44
45     :param url: LDB Url to open
46     :param session_info: Optional session information
47     :param credentials: Optional credentials, defaults to anonymous.
48     :param modules_dir: Modules directory, automatically set if not specified.
49     :param lp: Loadparm object, optional.
50
51     This is different from a regular Ldb file in that the Samba-specific
52     modules-dir is used by default and that credentials and session_info 
53     can be passed through (required by some modules).
54     """
55     import ldb
56     ret = ldb.Ldb()
57     if modules_dir is None:
58         modules_dir = default_ldb_modules_dir
59     if modules_dir is not None:
60         ret.set_modules_dir(modules_dir)
61     def samba_debug(level,text):
62         print "%d %s" % (level, text)
63     if credentials is not None:
64         ldb.set_credentials(credentials)
65     if session_info is not None:
66         ldb.set_session_info(session_info)
67     if lp is not None:
68         ldb.set_loadparm(lp)
69     #ret.set_debug(samba_debug)
70     ret.connect(url)
71     return ret
72
73
74 def substitute_var(text, values):
75     """substitute strings of the form ${NAME} in str, replacing
76     with substitutions from subobj.
77     
78     :param text: Text in which to subsitute.
79     :param values: Dictionary with keys and values.
80     """
81
82     for (name, value) in values.items():
83         text = text.replace("${%s}" % name, value)
84
85     return text
86
87
88 def valid_netbios_name(name):
89     """Check whether a name is valid as a NetBIOS name. """
90     # FIXME: There are probably more constraints here. 
91     # crh has a paragraph on this in his book (1.4.1.1)
92     if len(name) > 13:
93         return False
94     return True
95