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
23 from samba.auth import system_session
24 from samba.netcmd import Command, CommandError
27 class cmd_machinepw(Command):
28 """Get 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)
43 url = lp.get("secrets database")
44 secretsdb = Ldb(url=url, session_info=system_session(),
45 credentials=creds, lp=lp)
47 result = secretsdb.search(attrs=["secret"],
48 expression="(&(objectclass=primaryDomain)(samaccountname=%s))" % secret)
51 raise CommandError("search returned %d records, expected 1" % len(result))
53 self.outf.write("%s\n" % result[0]["secret"])