s3:smbd: fix NULL dereference in case of readlink failure
[Samba.git] / python / samba / netcmd / main.py
blobcce1f291f3412f45a5af8b33a8902d5ebf386799
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2011
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 """The main samba-tool command implementation."""
20 from samba import getopt as options
22 from samba.netcmd import SuperCommand
25 class cache_loader(dict):
26 """
27 We only load subcommand tools if they are actually used.
28 This significantly reduces the amount of time spent starting up
29 samba-tool
30 """
31 def __getitem__(self, attr):
32 item = dict.__getitem__(self, attr)
33 if item is None:
34 cmd = 'cmd_%s' % attr.replace('-', '_')
35 package = 'nettime' if attr == 'time' else attr
36 package = package.replace('-', '_')
37 self[attr] = getattr(__import__('samba.netcmd.%s' % package,
38 fromlist=[cmd]), cmd)()
39 return dict.__getitem__(self, attr)
41 def get(self, attr, default=None):
42 try:
43 return self[attr]
44 except KeyError:
45 return default
47 def items(self):
48 for key in self:
49 yield (key, self[key])
52 class cmd_sambatool(SuperCommand):
53 """Main samba administration tool."""
55 takes_optiongroups = {
56 "versionopts": options.VersionOptions,
59 subcommands = cache_loader()
61 subcommands["computer"] = None
62 subcommands["contact"] = None
63 subcommands["dbcheck"] = None
64 subcommands["delegation"] = None
65 subcommands["dns"] = None
66 subcommands["domain"] = None
67 subcommands["drs"] = None
68 subcommands["dsacl"] = None
69 subcommands["forest"] = None
70 subcommands["fsmo"] = None
71 subcommands["gpo"] = None
72 subcommands["group"] = None
73 subcommands["ldapcmp"] = None
74 subcommands["ntacl"] = None
75 subcommands["rodc"] = None
76 subcommands["schema"] = None
77 subcommands["shell"] = None
78 subcommands["sites"] = None
79 subcommands["spn"] = None
80 subcommands["testparm"] = None
81 subcommands["time"] = None
82 subcommands["user"] = None
83 subcommands["ou"] = None
84 subcommands["processes"] = None
85 subcommands["service-account"] = None
86 subcommands["visualize"] = None
89 def samba_tool(*args, **kwargs):
90 """A single function that runs samba-tool, returning an error code on
91 error, and None on success."""
92 try:
93 cmd, argv = cmd_sambatool()._resolve("samba-tool", *args, **kwargs)
94 ret = cmd._run(*argv)
95 except SystemExit as e:
96 ret = e.code
97 except Exception as e:
98 cmd.show_command_error(e)
99 ret = 1
100 return ret