1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 """Support for reading Samba 3 data files."""
20 __docformat__
= "restructuredText"
22 REGISTRY_VALUE_PREFIX
= "SAMBA_REGVAL"
23 REGISTRY_DB_VERSION
= 1
31 import param
as s3param
34 def fetch_uint32(db
, key
):
40 return struct
.unpack("<L", data
)[0]
43 def fetch_int32(db
, key
):
49 return struct
.unpack("<l", data
)[0]
52 class DbDatabase(object):
53 """Simple Samba 3 TDB/NTDB database reader."""
54 def __init__(self
, file):
57 :param file: Path of the file to open, appending .tdb or .ntdb.
59 if os
.path
.exists(file + ".ntdb"):
60 self
.db
= ntdb
.Ntdb(file + ".ntdb", flags
=os
.O_RDONLY
)
62 self
.db
= tdb
.Tdb(file + ".tdb", flags
=os
.O_RDONLY
)
65 def _check_version(self
):
69 """Close resources associated with this object."""
73 class Registry(DbDatabase
):
74 """Simple read-only support for reading the Samba3 registry.
76 :note: This object uses the same syntax for registry key paths as
77 Samba 3. This particular format uses forward slashes for key path
78 separators and abbreviations for the predefined key names.
79 e.g.: HKLM/Software/Bar.
82 """Return the number of keys."""
83 return len(self
.keys())
86 """Return list with all the keys."""
87 return [k
.rstrip("\x00") for k
in self
.db
.iterkeys() if not k
.startswith(REGISTRY_VALUE_PREFIX
)]
89 def subkeys(self
, key
):
90 """Retrieve the subkeys for the specified key.
93 :return: list with key names
95 data
= self
.db
.get("%s\x00" % key
)
98 (num
, ) = struct
.unpack("<L", data
[0:4])
99 keys
= data
[4:].split("\0")
100 assert keys
[-1] == ""
102 assert len(keys
) == num
105 def values(self
, key
):
106 """Return a dictionary with the values set for a specific key.
108 :param key: Key to retrieve values for.
109 :return: Dictionary with value names as key, tuple with type and
111 data
= self
.db
.get("%s/%s\x00" % (REGISTRY_VALUE_PREFIX
, key
))
115 (num
, ) = struct
.unpack("<L", data
[0:4])
119 (name
, data
) = data
.split("\0", 1)
121 (type, ) = struct
.unpack("<L", data
[0:4])
123 (value_len
, ) = struct
.unpack("<L", data
[0:4])
126 ret
[name
] = (type, data
[:value_len
])
127 data
= data
[value_len
:]
132 # High water mark keys
133 IDMAP_HWM_GROUP
= "GROUP HWM\0"
134 IDMAP_HWM_USER
= "USER HWM\0"
136 IDMAP_GROUP_PREFIX
= "GID "
137 IDMAP_USER_PREFIX
= "UID "
139 # idmap version determines auto-conversion
142 class IdmapDatabase(DbDatabase
):
143 """Samba 3 ID map database reader."""
145 def _check_version(self
):
146 assert fetch_int32(self
.db
, "IDMAP_VERSION\0") == IDMAP_VERSION_V2
149 """Retrieve a list of all ids in this database."""
150 for k
in self
.db
.iterkeys():
151 if k
.startswith(IDMAP_USER_PREFIX
):
152 yield k
.rstrip("\0").split(" ")
153 if k
.startswith(IDMAP_GROUP_PREFIX
):
154 yield k
.rstrip("\0").split(" ")
157 """Retrieve a list of all uids in this database."""
158 for k
in self
.db
.iterkeys():
159 if k
.startswith(IDMAP_USER_PREFIX
):
160 yield int(k
[len(IDMAP_USER_PREFIX
):].rstrip("\0"))
163 """Retrieve a list of all gids in this database."""
164 for k
in self
.db
.iterkeys():
165 if k
.startswith(IDMAP_GROUP_PREFIX
):
166 yield int(k
[len(IDMAP_GROUP_PREFIX
):].rstrip("\0"))
168 def get_sid(self
, xid
, id_type
):
169 """Retrive SID associated with a particular id and type.
171 :param xid: UID or GID to retrive SID for.
172 :param id_type: Type of id specified - 'UID' or 'GID'
174 data
= self
.db
.get("%s %s\0" % (id_type
, str(xid
)))
177 return data
.rstrip("\0")
179 def get_user_sid(self
, uid
):
180 """Retrieve the SID associated with a particular uid.
182 :param uid: UID to retrieve SID for.
183 :return: A SID or None if no mapping was found.
185 data
= self
.db
.get("%s%d\0" % (IDMAP_USER_PREFIX
, uid
))
188 return data
.rstrip("\0")
190 def get_group_sid(self
, gid
):
191 data
= self
.db
.get("%s%d\0" % (IDMAP_GROUP_PREFIX
, gid
))
194 return data
.rstrip("\0")
196 def get_user_hwm(self
):
197 """Obtain the user high-water mark."""
198 return fetch_uint32(self
.db
, IDMAP_HWM_USER
)
200 def get_group_hwm(self
):
201 """Obtain the group high-water mark."""
202 return fetch_uint32(self
.db
, IDMAP_HWM_GROUP
)
205 class SecretsDatabase(DbDatabase
):
206 """Samba 3 Secrets database reader."""
208 def get_auth_password(self
):
209 return self
.db
.get("SECRETS/AUTH_PASSWORD")
211 def get_auth_domain(self
):
212 return self
.db
.get("SECRETS/AUTH_DOMAIN")
214 def get_auth_user(self
):
215 return self
.db
.get("SECRETS/AUTH_USER")
217 def get_domain_guid(self
, host
):
218 return self
.db
.get("SECRETS/DOMGUID/%s" % host
)
221 for k
in self
.db
.iterkeys():
222 if k
.startswith("SECRETS/LDAP_BIND_PW/"):
223 yield k
[len("SECRETS/LDAP_BIND_PW/"):].rstrip("\0")
226 """Iterate over domains in this database.
228 :return: Iterator over the names of domains in this database.
230 for k
in self
.db
.iterkeys():
231 if k
.startswith("SECRETS/SID/"):
232 yield k
[len("SECRETS/SID/"):].rstrip("\0")
234 def get_ldap_bind_pw(self
, host
):
235 return self
.db
.get("SECRETS/LDAP_BIND_PW/%s" % host
)
237 def get_afs_keyfile(self
, host
):
238 return self
.db
.get("SECRETS/AFS_KEYFILE/%s" % host
)
240 def get_machine_sec_channel_type(self
, host
):
241 return fetch_uint32(self
.db
, "SECRETS/MACHINE_SEC_CHANNEL_TYPE/%s" % host
)
243 def get_machine_last_change_time(self
, host
):
244 return fetch_uint32(self
.db
, "SECRETS/MACHINE_LAST_CHANGE_TIME/%s" % host
)
246 def get_machine_password(self
, host
):
247 return self
.db
.get("SECRETS/MACHINE_PASSWORD/%s" % host
)
249 def get_machine_acc(self
, host
):
250 return self
.db
.get("SECRETS/$MACHINE.ACC/%s" % host
)
252 def get_domtrust_acc(self
, host
):
253 return self
.db
.get("SECRETS/$DOMTRUST.ACC/%s" % host
)
255 def trusted_domains(self
):
256 for k
in self
.db
.iterkeys():
257 if k
.startswith("SECRETS/$DOMTRUST.ACC/"):
258 yield k
[len("SECRETS/$DOMTRUST.ACC/"):].rstrip("\0")
260 def get_random_seed(self
):
261 return self
.db
.get("INFO/random_seed")
263 def get_sid(self
, host
):
264 return self
.db
.get("SECRETS/SID/%s" % host
.upper())
267 SHARE_DATABASE_VERSION_V1
= 1
268 SHARE_DATABASE_VERSION_V2
= 2
271 class ShareInfoDatabase(DbDatabase
):
272 """Samba 3 Share Info database reader."""
274 def _check_version(self
):
275 assert fetch_int32(self
.db
, "INFO/version\0") in (SHARE_DATABASE_VERSION_V1
, SHARE_DATABASE_VERSION_V2
)
277 def get_secdesc(self
, name
):
278 """Obtain the security descriptor on a particular share.
280 :param name: Name of the share
282 secdesc
= self
.db
.get("SECDESC/%s" % name
)
283 # FIXME: Run ndr_pull_security_descriptor
287 class Shares(object):
288 """Container for share objects."""
289 def __init__(self
, lp
, shareinfo
):
291 self
.shareinfo
= shareinfo
294 """Number of shares."""
295 return len(self
.lp
) - 1
298 """Iterate over the share names."""
299 return self
.lp
.__iter
__()
302 def shellsplit(text
):
303 """Very simple shell-like line splitting.
305 :param text: Text to split.
306 :return: List with parts of the line as strings.
313 inquotes
= not inquotes
314 elif c
in ("\t", "\n", " ") and not inquotes
:
325 class WinsDatabase(object):
326 """Samba 3 WINS database reader."""
327 def __init__(self
, file):
330 assert f
.readline().rstrip("\n") == "VERSION 1 0"
331 for l
in f
.readlines():
332 if l
[0] == "#": # skip comments
334 entries
= shellsplit(l
.rstrip("\n"))
336 ttl
= int(entries
[1])
339 while "." in entries
[i
]:
340 ips
.append(entries
[i
])
342 nb_flags
= int(entries
[i
][:-1], 16)
343 assert not name
in self
.entries
, "Name %s exists twice" % name
344 self
.entries
[name
] = (ttl
, ips
, nb_flags
)
347 def __getitem__(self
, name
):
348 return self
.entries
[name
]
351 return len(self
.entries
)
354 return iter(self
.entries
)
357 """Return the entries in this WINS database."""
358 return self
.entries
.items()
360 def close(self
): # for consistency
364 class Samba3(object):
365 """Samba 3 configuration and state data reader."""
367 def __init__(self
, smbconfpath
, s3_lp_ctx
=None):
368 """Open the configuration and data for a Samba 3 installation.
370 :param smbconfpath: Path to the smb.conf file.
371 :param s3_lp_ctx: Samba3 Loadparm context
373 self
.smbconfpath
= smbconfpath
377 self
.lp
= s3param
.get_context()
378 self
.lp
.load(smbconfpath
)
380 def statedir_path(self
, path
):
381 if path
[0] == "/" or path
[0] == ".":
383 return os
.path
.join(self
.lp
.get("state directory"), path
)
385 def privatedir_path(self
, path
):
386 if path
[0] == "/" or path
[0] == ".":
388 return os
.path
.join(self
.lp
.get("private dir"), path
)
393 def get_sam_db(self
):
394 return passdb
.PDB(self
.lp
.get('passdb backend'))
396 def get_registry(self
):
397 return Registry(self
.statedir_path("registry"))
399 def get_secrets_db(self
):
400 return SecretsDatabase(self
.privatedir_path("secrets"))
402 def get_shareinfo_db(self
):
403 return ShareInfoDatabase(self
.statedir_path("share_info"))
405 def get_idmap_db(self
):
406 return IdmapDatabase(self
.statedir_path("winbindd_idmap"))
408 def get_wins_db(self
):
409 return WinsDatabase(self
.statedir_path("wins.dat"))
411 def get_shares(self
):
412 return Shares(self
.get_conf(), self
.get_shareinfo_db())