samba-tool domain join subdomain: Rework sambadns.py to allow setup of DomainDNSZone...
[samba.git] / python / samba / provision / common.py
1
2 # Unix SMB/CIFS implementation.
3 # utility functions for provisioning a Samba4 server
4
5 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2010
6 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2008-2009
7 # Copyright (C) Oliver Liebel <oliver@itc.li> 2008-2009
8 #
9 # Based on the original in EJS:
10 # Copyright (C) Andrew Tridgell <tridge@samba.org> 2005
11 #
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 3 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 #
25
26 """Functions for setting up a Samba configuration."""
27
28 __docformat__ = "restructuredText"
29
30 import os
31 from samba import read_and_sub_file
32 from samba.param import setup_dir
33
34 FILL_FULL = "FULL"
35 FILL_SUBDOMAIN = "SUBDOMAIN"
36 FILL_NT4SYNC = "NT4SYNC"
37 FILL_DRS = "DRS"
38
39
40 def setup_path(file):
41     """Return an absolute path to the provision tempate file specified by file"""
42     return os.path.join(setup_dir(), file)
43
44
45 def setup_add_ldif(ldb, ldif_path, subst_vars=None,controls=["relax:0"]):
46     """Setup a ldb in the private dir.
47
48     :param ldb: LDB file to import data into
49     :param ldif_path: Path of the LDIF file to load
50     :param subst_vars: Optional variables to subsitute in LDIF.
51     :param nocontrols: Optional list of controls, can be None for no controls
52     """
53     assert isinstance(ldif_path, str)
54     data = read_and_sub_file(ldif_path, subst_vars)
55     ldb.add_ldif(data, controls)
56
57
58 def setup_modify_ldif(ldb, ldif_path, subst_vars=None,controls=["relax:0"]):
59     """Modify a ldb in the private dir.
60
61     :param ldb: LDB object.
62     :param ldif_path: LDIF file path.
63     :param subst_vars: Optional dictionary with substitution variables.
64     """
65     data = read_and_sub_file(ldif_path, subst_vars)
66     ldb.modify_ldif(data, controls)
67
68
69 def setup_ldb(ldb, ldif_path, subst_vars):
70     """Import a LDIF a file into a LDB handle, optionally substituting
71     variables.
72
73     :note: Either all LDIF data will be added or none (using transactions).
74
75     :param ldb: LDB file to import into.
76     :param ldif_path: Path to the LDIF file.
77     :param subst_vars: Dictionary with substitution variables.
78     """
79     assert ldb is not None
80     ldb.transaction_start()
81     try:
82         setup_add_ldif(ldb, ldif_path, subst_vars)
83     except:
84         ldb.transaction_cancel()
85         raise
86     else:
87         ldb.transaction_commit()