samba-tool: fix machinepw command so that we use the full path to the secret database...
[Samba/gebeck_regimport.git] / source4 / scripting / python / samba / netcmd / machinepw.py
blob75338d95ef5eaf38f6bceb8c55252b8fa2f1a4bb
1 #!/usr/bin/env python
3 # Machine passwords
4 # Copyright Jelmer Vernooij 2010
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 import samba.getopt as options
21 import os
22 from samba import Ldb
23 from samba.auth import system_session
24 from samba.netcmd import Command, CommandError
27 class cmd_machinepw(Command):
28 """Gets a machine password out of our SAM"""
30 synopsis = "%prog machinepw <accountname>"
32 takes_optiongroups = {
33 "sambaopts": options.SambaOptions,
34 "versionopts": options.VersionOptions,
35 "credopts": options.CredentialsOptions,
38 takes_args = ["secret"]
40 def run(self, secret, sambaopts=None, credopts=None, versionopts=None):
41 lp = sambaopts.get_loadparm()
42 creds = credopts.get_credentials(lp, fallback_machine=True)
43 name = lp.get("secrets database")
44 path = lp.get("private dir")
45 url = os.path.join(path, name)
46 if not os.path.exists(url):
47 raise CommandError("secret database not found at %s " % url)
48 secretsdb = Ldb(url=url, session_info=system_session(),
49 credentials=creds, lp=lp)
50 result = secretsdb.search(attrs=["secret"],
51 expression="(&(objectclass=primaryDomain)(samaccountname=%s))" % secret)
53 if len(result) != 1:
54 raise CommandError("search returned %d records, expected 1" % len(result))
56 self.outf.write("%s\n" % result[0]["secret"])