b9979770e60ccbccbe0fbe7075cdc04bfe9b78fe
[obnox/samba/samba-obnox.git] / source4 / scripting / python / samba / netcmd / testparm.py
1 #!/usr/bin/env python
2 # vim: expandtab ft=python
3 #
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>
8 #
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
14 #
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.
19 #
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.
24 #
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/>.
27 #
28 # Testbed for loadparm.c/params.c
29 #
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.
33 #
34 # Useful for a quick 'syntax check' of a configuration file.
35 #
36
37 import os
38 import sys
39
40 import samba
41 import samba.getopt as options
42 from samba.netcmd import Command, CommandError, Option
43
44 class cmd_testparm(Command):
45     """Syntax check the configuration file"""
46
47     synopsis = "%prog testparm [options]"
48
49     takes_optiongroups = {
50         "sambaopts" : options.SambaOptions,
51         "versionopts": options.VersionOptions
52     }
53
54     takes_options = [
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")
73         ]
74
75     takes_args = []
76
77     def run(self, sambaopts, versionopts, 
78             section_name=None, parameter_name=None,
79             client_ip=None, client_name=None, verbose=False,
80             suppress_prompt=None,
81             show_all_parameters=False, server=None):
82         if server:
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")
89
90         lp = sambaopts.get_loadparm()
91
92         # We need this to force the output
93         samba.set_debug_level(2)
94
95         logger = self.get_logger("testparm")
96
97         logger.info("Loaded smb config files from %s", lp.configfile)
98         logger.info("Loaded services file OK.")
99
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)
104         else:
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)
108                 else:
109                     print lp.get(parameter_name, section_name)
110             else:
111                 if not suppress_prompt:
112                     print "Press enter to see a dump of your service definitions"
113                     sys.stdin.readline()
114                 lp.dump(sys.stdout, verbose)
115         if valid:
116             return
117         else:
118             raise CommandError("Invalid smb.conf")
119
120     def do_global_checks(self, lp, logger):
121         valid = True
122
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",
126                          netbios_name)
127             valid = False
128
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",
132                          workgroup)
133             valid = False
134
135         lockdir = lp.get("lockdir")
136
137         if not os.path.isdir(lockdir):
138             logger.error("lock directory %s does not exist", lockdir)
139             valid = False
140
141         piddir = lp.get("pid directory")
142
143         if not os.path.isdir(piddir):
144             logger.error("pid directory %s does not exist", piddir)
145             valid = False
146
147         winbind_separator = lp.get("winbind separator")
148
149         if len(winbind_separator) != 1:
150             logger.error("the 'winbind separator' parameter must be a single "
151                          "character.")
152             valid = False
153
154         if winbind_separator == '+':
155             logger.error("'winbind separator = +' might cause problems with group "
156                          "membership.")
157             valid = False
158
159         return valid
160
161     def allow_access(self, deny_list, allow_list, cname, caddr):
162         raise NotImplementedError(self.allow_access)
163
164     def do_share_checks(self, lp, logger):
165         valid = True
166         for s in lp.services():
167             if len(s) > 12:
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.)")
172                 break
173
174         for s in lp.services():
175             deny_list = lp.get("hosts deny", s)
176             allow_list = lp.get("hosts allow", s)
177             if deny_list:
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)
182                         valid = False
183
184             if allow_list:
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)
189                         valid = False
190         return valid
191
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,
196                              caddr) and
197                 self.allow_access(lp.get("hosts deny", s), lp.get("hosts allow", s),
198                              cname, caddr)):
199                 logger.info("Allow connection from %s (%s) to %s", cname, caddr, s)
200             else:
201                 logger.info("Deny connection from %s (%s) to %s", cname, caddr, s)
202
203 ##   FIXME: We need support for smb.conf macros before this will work again
204 ##
205 ##    if (new_local_machine) {
206 ##        set_local_machine_name(new_local_machine, True)
207 ##    }
208 #