s4:samba-tool: remove unused code in testparm.py
[Samba/gebeck_regimport.git] / source4 / scripting / python / samba / netcmd / testparm.py
blob5cb75e27ae9cf01624b7618e4232a80b4c67a7f2
1 # Unix SMB/CIFS implementation.
2 # Test validity of smb.conf
3 # Copyright (C) 2010-2011 Jelmer Vernooij <jelmer@samba.org>
5 # Based on the original in C:
6 # Copyright (C) Karl Auer 1993, 1994-1998
7 # Extensively modified by Andrew Tridgell, 1995
8 # Converted to popt by Jelmer Vernooij (jelmer@nl.linux.org), 2002
9 # Updated for Samba4 by Andrew Bartlett <abartlet@samba.org> 2006
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 # Testbed for loadparm.c/params.c
26 # This module simply loads a specified configuration file and
27 # if successful, dumps it's contents to stdout. Note that the
28 # operation is performed with DEBUGLEVEL at 3.
30 # Useful for a quick 'syntax check' of a configuration file.
33 import os
34 import sys
36 import samba
37 import samba.getopt as options
38 from samba.netcmd import Command, CommandError, Option
40 class cmd_testparm(Command):
41 """Syntax check the configuration file."""
43 synopsis = "%prog [options]"
45 takes_optiongroups = {
46 "sambaopts": options.SambaOptions,
47 "versionopts": options.VersionOptions
50 takes_options = [
51 Option("--section-name", type=str,
52 help="Limit testparm to a named section"),
53 Option("--parameter-name", type=str,
54 help="Limit testparm to a named parameter"),
55 Option("--client-name", type=str,
56 help="Client DNS name for 'hosts allow' checking "
57 "(should match reverse lookup)"),
58 Option("--client-ip", type=str,
59 help="Client IP address for 'hosts allow' checking"),
60 Option("--suppress-prompt", action="store_true", default=False,
61 help="Suppress prompt for enter"),
62 Option("-v", "--verbose", action="store_true",
63 default=False, help="Show default options too"),
64 # We need support for smb.conf macros before this will work again
65 Option("--server", type=str, help="Set %%L macro to servername"),
66 # These are harder to do with the new code structure
67 Option("--show-all-parameters", action="store_true", default=False,
68 help="Show the parameters, type, possible values")
71 takes_args = []
73 def run(self, sambaopts, versionopts,
74 section_name=None, parameter_name=None,
75 client_ip=None, client_name=None, verbose=False,
76 suppress_prompt=None,
77 show_all_parameters=False, server=None):
78 if server:
79 raise NotImplementedError("--server not yet implemented")
80 if show_all_parameters:
81 raise NotImplementedError("--show-all-parameters not yet implemented")
82 if client_name is not None and client_ip is None:
83 raise CommandError("Both a DNS name and an IP address are "
84 "required for the host access check")
86 lp = sambaopts.get_loadparm()
88 # We need this to force the output
89 samba.set_debug_level(2)
91 logger = self.get_logger("testparm")
93 logger.info("Loaded smb config files from %s", lp.configfile)
94 logger.info("Loaded services file OK.")
96 valid = self.do_global_checks(lp, logger)
97 valid = valid and self.do_share_checks(lp, logger)
98 if client_name is not None and client_ip is not None:
99 self.check_client_access(lp, logger, client_name, client_ip)
100 else:
101 if section_name is not None or parameter_name is not None:
102 if parameter_name is None:
103 lp[section_name].dump(sys.stdout, lp.default_service, verbose)
104 else:
105 self.outf.write(lp.get(parameter_name, section_name)+"\n")
106 else:
107 if not suppress_prompt:
108 self.outf.write("Press enter to see a dump of your service definitions\n")
109 sys.stdin.readline()
110 lp.dump(sys.stdout, verbose)
111 if valid:
112 return
113 else:
114 raise CommandError("Invalid smb.conf")
116 def do_global_checks(self, lp, logger):
117 valid = True
119 netbios_name = lp.get("netbios name")
120 if not samba.valid_netbios_name(netbios_name):
121 logger.error("netbios name %s is not a valid netbios name",
122 netbios_name)
123 valid = False
125 workgroup = lp.get("workgroup")
126 if not samba.valid_netbios_name(workgroup):
127 logger.error("workgroup name %s is not a valid netbios name",
128 workgroup)
129 valid = False
131 lockdir = lp.get("lockdir")
133 if not os.path.isdir(lockdir):
134 logger.error("lock directory %s does not exist", lockdir)
135 valid = False
137 piddir = lp.get("pid directory")
139 if not os.path.isdir(piddir):
140 logger.error("pid directory %s does not exist", piddir)
141 valid = False
143 winbind_separator = lp.get("winbind separator")
145 if len(winbind_separator) != 1:
146 logger.error("the 'winbind separator' parameter must be a single "
147 "character.")
148 valid = False
150 if winbind_separator == '+':
151 logger.error("'winbind separator = +' might cause problems with group "
152 "membership.")
153 valid = False
155 return valid
157 def allow_access(self, deny_list, allow_list, cname, caddr):
158 raise NotImplementedError(self.allow_access)
160 def do_share_checks(self, lp, logger):
161 valid = True
162 for s in lp.services():
163 if len(s) > 12:
164 logger.warning("You have some share names that are longer than 12 "
165 "characters. These may not be accessible to some older "
166 "clients. (Eg. Windows9x, WindowsMe, and not listed in "
167 "smbclient in Samba 3.0.)")
168 break
170 for s in lp.services():
171 deny_list = lp.get("hosts deny", s)
172 allow_list = lp.get("hosts allow", s)
173 if deny_list:
174 for entry in deny_list:
175 if "*" in entry or "?" in entry:
176 logger.error("Invalid character (* or ?) in hosts deny "
177 "list (%s) for service %s.", entry, s)
178 valid = False
180 if allow_list:
181 for entry in allow_list:
182 if "*" in entry or "?" in entry:
183 logger.error("Invalid character (* or ?) in hosts allow "
184 "list (%s) for service %s.", entry, s)
185 valid = False
186 return valid
188 def check_client_access(self, lp, logger, cname, caddr):
189 # this is totally ugly, a real `quick' hack
190 for s in lp.services():
191 if (self.allow_access(lp.get("hosts deny"), lp.get("hosts allow"), cname,
192 caddr) and
193 self.allow_access(lp.get("hosts deny", s), lp.get("hosts allow", s),
194 cname, caddr)):
195 logger.info("Allow connection from %s (%s) to %s", cname, caddr, s)
196 else:
197 logger.info("Deny connection from %s (%s) to %s", cname, caddr, s)
199 ## FIXME: We need support for smb.conf macros before this will work again
201 ## if (new_local_machine) {
202 ## set_local_machine_name(new_local_machine, True)
203 ## }