2 # vim: expandtab ft=python
4 # Unix SMB/CIFS implementation.
5 # Test validity of smb.conf
6 # Copyright (C) 2010-2011 Jelmer Vernooij <jelmer@samba.org>
7 # Copyright (C) Giampaolo Lauria 2011 <lauria2@yahoo.com>
9 # Based on the original in C:
10 # Copyright (C) Karl Auer 1993, 1994-1998
11 # Extensively modified by Andrew Tridgell, 1995
12 # Converted to popt by Jelmer Vernooij (jelmer@nl.linux.org), 2002
13 # Updated for Samba4 by Andrew Bartlett <abartlet@samba.org> 2006
15 # This program is free software; you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation; either version 3 of the License, or
18 # (at your option) any later version.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program. If not, see <http://www.gnu.org/licenses/>.
28 # Testbed for loadparm.c/params.c
30 # This module simply loads a specified configuration file and
31 # if successful, dumps it's contents to stdout. Note that the
32 # operation is performed with DEBUGLEVEL at 3.
34 # Useful for a quick 'syntax check' of a configuration file.
41 import samba.getopt as options
42 from samba.netcmd import Command, CommandError, Option
44 class cmd_testparm(Command):
45 """Syntax check the configuration file"""
47 synopsis = "%prog testparm [options]"
49 takes_optiongroups = {
50 "sambaopts" : options.SambaOptions,
51 "versionopts": options.VersionOptions
55 Option("--section-name", type=str,
56 help="Limit testparm to a named section"),
57 Option("--parameter-name", type=str,
58 help="Limit testparm to a named parameter"),
59 Option("--client-name", type=str,
60 help="Client DNS name for 'hosts allow' checking "
61 "(should match reverse lookup)"),
62 Option("--client-ip", type=str,
63 help="Client IP address for 'hosts allow' checking"),
64 Option("--suppress-prompt", action="store_true", default=False,
65 help="Suppress prompt for enter"),
66 Option("-v", "--verbose", action="store_true",
67 default=False, help="Show default options too"),
68 # We need support for smb.conf macros before this will work again
69 Option("--server", type=str, help="Set %%L macro to servername"),
70 # These are harder to do with the new code structure
71 Option("--show-all-parameters", action="store_true", default=False,
72 help="Show the parameters, type, possible values")
77 def run(self, sambaopts, versionopts,
78 section_name=None, parameter_name=None,
79 client_ip=None, client_name=None, verbose=False,
81 show_all_parameters=False, server=None):
83 raise NotImplementedError("--server not yet implemented")
84 if show_all_parameters:
85 raise NotImplementedError("--show-all-parameters not yet implemented")
86 if client_name is not None and client_ip is None:
87 raise CommandError("Both a DNS name and an IP address are "
88 "required for the host access check")
90 lp = sambaopts.get_loadparm()
92 # We need this to force the output
93 samba.set_debug_level(2)
95 logger = self.get_logger("testparm")
97 logger.info("Loaded smb config files from %s", lp.configfile)
98 logger.info("Loaded services file OK.")
100 valid = self.do_global_checks(lp, logger)
101 valid = valid and self.do_share_checks(lp, logger)
102 if client_name is not None and client_ip is not None:
103 self.check_client_access(lp, logger, client_name, client_ip)
105 if section_name is not None or parameter_name is not None:
106 if parameter_name is None:
107 lp[section_name].dump(sys.stdout, lp.default_service, verbose)
109 print lp.get(parameter_name, section_name)
111 if not suppress_prompt:
112 print "Press enter to see a dump of your service definitions"
114 lp.dump(sys.stdout, verbose)
118 raise CommandError("Invalid smb.conf")
120 def do_global_checks(self, lp, logger):
123 netbios_name = lp.get("netbios name")
124 if not samba.valid_netbios_name(netbios_name):
125 logger.error("netbios name %s is not a valid netbios name",
129 workgroup = lp.get("workgroup")
130 if not samba.valid_netbios_name(workgroup):
131 logger.error("workgroup name %s is not a valid netbios name",
135 lockdir = lp.get("lockdir")
137 if not os.path.isdir(lockdir):
138 logger.error("lock directory %s does not exist", lockdir)
141 piddir = lp.get("pid directory")
143 if not os.path.isdir(piddir):
144 logger.error("pid directory %s does not exist", piddir)
147 winbind_separator = lp.get("winbind separator")
149 if len(winbind_separator) != 1:
150 logger.error("the 'winbind separator' parameter must be a single "
154 if winbind_separator == '+':
155 logger.error("'winbind separator = +' might cause problems with group "
161 def allow_access(self, deny_list, allow_list, cname, caddr):
162 raise NotImplementedError(self.allow_access)
164 def do_share_checks(self, lp, logger):
166 for s in lp.services():
168 logger.warning("You have some share names that are longer than 12 "
169 "characters. These may not be accessible to some older "
170 "clients. (Eg. Windows9x, WindowsMe, and not listed in "
171 "smbclient in Samba 3.0.)")
174 for s in lp.services():
175 deny_list = lp.get("hosts deny", s)
176 allow_list = lp.get("hosts allow", s)
178 for entry in deny_list:
179 if "*" in entry or "?" in entry:
180 logger.error("Invalid character (* or ?) in hosts deny "
181 "list (%s) for service %s.", entry, s)
185 for entry in allow_list:
186 if "*" in entry or "?" in entry:
187 logger.error("Invalid character (* or ?) in hosts allow "
188 "list (%s) for service %s.", entry, s)
192 def check_client_access(self, lp, logger, cname, caddr):
193 # this is totally ugly, a real `quick' hack
194 for s in lp.services():
195 if (self.allow_access(lp.get("hosts deny"), lp.get("hosts allow"), cname,
197 self.allow_access(lp.get("hosts deny", s), lp.get("hosts allow", s),
199 logger.info("Allow connection from %s (%s) to %s", cname, caddr, s)
201 logger.info("Deny connection from %s (%s) to %s", cname, caddr, s)
203 ## FIXME: We need support for smb.conf macros before this will work again
205 ## if (new_local_machine) {
206 ## set_local_machine_name(new_local_machine, True)