s4 dns: Check more of the returned values for the A query
[Samba/gebeck_regimport.git] / source4 / scripting / python / samba / idmap.py
blob9d61ec8e5ec4876e9fad81f6c7966aec3b4931f6
1 #!/usr/bin/env python
3 # Unix SMB/CIFS implementation.
4 # Copyright (C) 2008 Kai Blin <kai@samba.org>
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.
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.
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/>.
21 """Convenience functions for using the idmap database."""
23 __docformat__ = "restructuredText"
25 import ldb
26 import samba
28 class IDmapDB(samba.Ldb):
29 """The IDmap database."""
31 # Mappings for ID_TYPE_UID, ID_TYPE_GID and ID_TYPE_BOTH
32 TYPE_UID = 1
33 TYPE_GID = 2
34 TYPE_BOTH = 3
36 def __init__(self, url=None, lp=None, modules_dir=None, session_info=None,
37 credentials=None, flags=0, options=None):
38 """Opens the IDMap Database.
40 For parameter meanings see the super class (samba.Ldb)
41 """
42 self.lp = lp
43 if url is None:
44 url = lp.private_path("idmap.ldb")
46 super(IDmapDB, self).__init__(url=url, lp=lp, modules_dir=modules_dir,
47 session_info=session_info, credentials=credentials, flags=flags,
48 options=options)
50 def connect(self, url=None, flags=0, options=None):
51 super(IDmapDB, self).connect(url=self.lp.private_path(url), flags=flags,
52 options=options)
54 def increment_xid(self):
55 """Increment xidNumber, if not present it create and assign it to the lowerBound
57 :return xid can that be used for SID/unixid mapping
58 """
59 res = self.search(expression="dn=CN=CONFIG", base="",
60 scope=ldb.SCOPE_SUBTREE)
61 id = res[0].get("xidNumber")
62 flag = ldb.FLAG_MOD_REPLACE
63 if id is None:
64 id = res[0].get("lowerBound")
65 flag = ldb.FLAG_MOD_ADD
66 newid = int(str(id)) + 1
67 msg = ldb.Message()
68 msg.dn = ldb.Dn(self, "CN=CONFIG")
69 msg["xidNumber"] = ldb.MessageElement(str(newid), flag, "xidNumber")
70 self.modify(msg)
71 return id
73 def setup_name_mapping(self, sid, type, unixid=None):
74 """Setup a mapping between a sam name and a unix name.
76 :param sid: SID of the NT-side of the mapping.
77 :param unixname: Unix id to map to, if none supplied the next one will be selected
78 """
79 if unixid is None:
80 unixid = self.increment_xid()
81 type_string = ""
82 if type == self.TYPE_UID:
83 type_string = "ID_TYPE_UID"
84 elif type == self.TYPE_GID:
85 type_string = "ID_TYPE_GID"
86 elif type == self.TYPE_BOTH:
87 type_string = "ID_TYPE_BOTH"
88 else:
89 return
91 mod = """
92 dn: CN=%s
93 xidNumber: %s
94 objectSid: %s
95 objectClass: sidMap
96 type: %s
97 cn: %s
99 """ % (sid, unixid, sid, type_string, sid)
100 self.add(self.parse_ldif(mod).next()[1])