py: Fix initialisation of subtypes, fix segfaults.
[Samba/ekacnet.git] / source4 / scripting / python / samba / idmap.py
blobf8eeb1892569a4b68b3b85577467f96c156cd604
1 #!/usr/bin/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 samba
26 import glue
27 import ldb
29 class IDmapDB(samba.Ldb):
30 """The IDmap database."""
32 # Mappings for ID_TYPE_UID, ID_TYPE_GID and ID_TYPE_BOTH
33 TYPE_UID = 1
34 TYPE_GID = 2
35 TYPE_BOTH = 3
37 def __init__(self, url=None, session_info=None, credentials=None,
38 modules_dir=None, lp=None):
39 """Open the IDmap Database.
41 :param url: URL of the database.
42 """
43 self.lp = lp
45 super(IDmapDB, self).__init__(session_info=session_info, credentials=credentials,
46 modules_dir=modules_dir, lp=lp)
47 if url:
48 self.connect(url)
49 else:
50 self.connect(lp.get("idmap database"))
52 def connect(self, url):
53 super(IDmapDB, self).connect(self.lp.private_path(url))
55 def setup_name_mapping(self, sid, type, unixid):
56 """Setup a mapping between a sam name and a unix name.
58 :param sid: SID of the NT-side of the mapping.
59 :param unixname: Unix name to map to.
60 """
61 type_string = ""
62 if type == self.TYPE_UID:
63 type_string = "ID_TYPE_UID"
64 elif type == self.TYPE_GID:
65 type_string = "ID_TYPE_GID"
66 elif type == self.TYPE_BOTH:
67 type_string = "ID_TYPE_BOTH"
68 else:
69 return
71 mod = """
72 dn: CN=%s
73 xidNumber: %s
74 objectSid: %s
75 objectClass: sidMap
76 type: %s
77 cn: %s
79 """ % (sid, unixid, sid, type_string, sid)
80 self.add(self.parse_ldif(mod).next()[1])