s4:net utility - make outprinted description comments more consistent
[ira/wip.git] / source4 / scripting / python / samba / netcmd / machinepw.py
1 #!/usr/bin/python
2 #
3 # Machine passwords
4 # Copyright Jelmer Vernooij 2010
5 #
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.
10 #
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.
15 #
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/>.
18 #
19
20 import samba.getopt as options
21
22 from samba import Ldb
23 from samba.auth import system_session
24 from samba.netcmd import Command, CommandError
25
26
27 class cmd_machinepw(Command):
28     """Gets a machine password out of our SAM"""
29
30     synopsis = "%prog machinepw <accountname>"
31
32     takes_optiongroups = {
33         "sambaopts": options.SambaOptions,
34         "versionopts": options.VersionOptions,
35         "credopts": options.CredentialsOptions,
36     }
37
38     takes_args = ["secret"]
39
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)
46         
47         result = secretsdb.search(attrs=["secret"], 
48             expression="(&(objectclass=primaryDomain)(samaccountname=%s))" % secret)
49
50         if len(result) != 1:
51             raise CommandError("search returned %d records, expected 1" % len(result))
52
53         self.outf.write("%s\n" % result[0]["secret"])