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