Merge branch 'v4-0-test' of git://git.samba.org/samba into 4-0-local
[kai/samba.git] / source4 / utils / testparm.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Test validity of smb.conf
4    Copyright (C) Karl Auer 1993, 1994-1998
5
6    Extensively modified by Andrew Tridgell, 1995
7    Converted to popt by Jelmer Vernooij (jelmer@nl.linux.org), 2002
8    Updated for Samba4 by Andrew Bartlett <abartlet@samba.org> 2006
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25  * Testbed for loadparm.c/params.c
26  *
27  * This module simply loads a specified configuration file and
28  * if successful, dumps it's contents to stdout. Note that the
29  * operation is performed with DEBUGLEVEL at 3.
30  *
31  * Useful for a quick 'syntax check' of a configuration file.
32  *
33  */
34
35 #include "includes.h"
36 #include "system/filesys.h"
37 #include "lib/cmdline/popt_common.h"
38 #include "lib/socket/socket.h"
39 #include "param/param.h"
40
41
42 /***********************************************
43  Here we do a set of 'hard coded' checks for bad
44  configuration settings.
45 ************************************************/
46
47 static int do_global_checks(struct loadparm_context *lp_ctx)
48 {
49         int ret = 0;
50
51         if (!directory_exist(lp_lockdir(lp_ctx))) {
52                 fprintf(stderr, "ERROR: lock directory %s does not exist\n",
53                        lp_lockdir(lp_ctx));
54                 ret = 1;
55         }
56
57         if (!directory_exist(lp_piddir(lp_ctx))) {
58                 fprintf(stderr, "ERROR: pid directory %s does not exist\n",
59                        lp_piddir(lp_ctx));
60                 ret = 1;
61         }
62
63         if (strlen(lp_winbind_separator(lp_ctx)) != 1) {
64                 fprintf(stderr,"ERROR: the 'winbind separator' parameter must be a single character.\n");
65                 ret = 1;
66         }
67
68         if (*lp_winbind_separator(lp_ctx) == '+') {
69                 fprintf(stderr,"'winbind separator = +' might cause problems with group membership.\n");
70         }
71
72         return ret;
73 }   
74
75
76 static int do_share_checks(struct loadparm_context *lp_ctx, const char *cname, const char *caddr, bool silent_mode,
77                            bool show_defaults, const char *section_name, const char *parameter_name)
78 {
79         int ret = 0;
80         int s;
81
82         for (s=0;s<lp_numservices(lp_ctx);s++) {
83                 struct loadparm_service *service = lp_servicebynum(lp_ctx, s);
84                 if (service != NULL)
85                         if (strlen(lp_servicename(lp_servicebynum(lp_ctx, s))) > 12) {
86                                 fprintf(stderr, "WARNING: You have some share names that are longer than 12 characters.\n" );
87                                 fprintf(stderr, "These may not be accessible to some older clients.\n" );
88                                 fprintf(stderr, "(Eg. Windows9x, WindowsMe, and not listed in smbclient in Samba 3.0.)\n" );
89                                 break;
90                         }
91         }
92
93         for (s=0;s<lp_numservices(lp_ctx);s++) {
94                 struct loadparm_service *service = lp_servicebynum(lp_ctx, s);
95                 if (service != NULL) {
96                         const char **deny_list = lp_hostsdeny(service, lp_default_service(lp_ctx));
97                         const char **allow_list = lp_hostsallow(service, lp_default_service(lp_ctx));
98                         int i;
99                         if(deny_list) {
100                                 for (i=0; deny_list[i]; i++) {
101                                         char *hasstar = strchr_m(deny_list[i], '*');
102                                         char *hasquery = strchr_m(deny_list[i], '?');
103                                         if(hasstar || hasquery) {
104                                                 fprintf(stderr,"Invalid character %c in hosts deny list (%s) for service %s.\n",
105                                                            hasstar ? *hasstar : *hasquery, deny_list[i], lp_servicename(service) );
106                                         }
107                                 }
108                         }
109
110                         if(allow_list) {
111                                 for (i=0; allow_list[i]; i++) {
112                                         char *hasstar = strchr_m(allow_list[i], '*');
113                                         char *hasquery = strchr_m(allow_list[i], '?');
114                                         if(hasstar || hasquery) {
115                                                 fprintf(stderr,"Invalid character %c in hosts allow list (%s) for service %s.\n",
116                                                            hasstar ? *hasstar : *hasquery, allow_list[i], lp_servicename(service) );
117                                         }
118                                 }
119                         }
120                 }
121         }
122
123
124         if (!cname) {
125                 if (!silent_mode) {
126                         fprintf(stderr,"Press enter to see a dump of your service definitions\n");
127                         fflush(stdout);
128                         getc(stdin);
129                 }
130                 if (section_name != NULL || parameter_name != NULL) {
131                         struct loadparm_service *service = NULL;
132                         if (!section_name) {
133                                 section_name = GLOBAL_NAME;
134                                 service = NULL;
135                         } else if ((!strwicmp(section_name, GLOBAL_NAME)) == 0 &&
136                                  (service=lp_service(lp_ctx, section_name)) == NULL) {
137                                         fprintf(stderr,"Unknown section %s\n",
138                                                 section_name);
139                                         return(1);
140                         }
141                         if (!parameter_name) {
142                                 lp_dump_one(stdout, show_defaults, service, lp_default_service(lp_ctx));
143                         } else {
144                                 ret = !lp_dump_a_parameter(lp_ctx, service, parameter_name, stdout);
145                         }
146                 } else {
147                         lp_dump(lp_ctx, stdout, show_defaults, lp_numservices(lp_ctx));
148                 }
149                 return(ret);
150         }
151
152         if(cname && caddr){
153                 /* this is totally ugly, a real `quick' hack */
154                 for (s=0;s<lp_numservices(lp_ctx);s++) {
155                         struct loadparm_service *service = lp_servicebynum(lp_ctx, s);
156                         if (service != NULL) {
157                                 if (allow_access(NULL, lp_hostsdeny(NULL, lp_default_service(lp_ctx)), lp_hostsallow(NULL, lp_default_service(lp_ctx)), cname, caddr)
158                                     && allow_access(NULL, lp_hostsdeny(service, lp_default_service(lp_ctx)), lp_hostsallow(service, lp_default_service(lp_ctx)), cname, caddr)) {
159                                         fprintf(stderr,"Allow connection from %s (%s) to %s\n",
160                                                    cname,caddr,lp_servicename(service));
161                                 } else {
162                                         fprintf(stderr,"Deny connection from %s (%s) to %s\n",
163                                                    cname,caddr,lp_servicename(service));
164                                 }
165                         }
166                 }
167         }
168
169         return ret;
170 }
171
172
173  int main(int argc, const char *argv[])
174 {
175         static bool silent_mode = false;
176         int ret = 0;
177         poptContext pc;
178 /*
179         static int show_all_parameters = 0;
180         static char *new_local_machine = NULL;
181 */
182         static const char *section_name = NULL;
183         static char *parameter_name = NULL;
184         static const char *cname;
185         static const char *caddr;
186         static bool show_defaults = false;
187         struct loadparm_context *lp_ctx;
188
189         struct poptOption long_options[] = {
190                 POPT_AUTOHELP
191                 {"suppress-prompt", 0, POPT_ARG_NONE, &silent_mode, true, "Suppress prompt for enter"},
192                 {"verbose", 'v', POPT_ARG_NONE, &show_defaults, true, "Show default options too"},
193 /*
194   We need support for smb.conf macros before this will work again 
195                 {"server", 'L',POPT_ARG_STRING, &new_local_machine, 0, "Set %%L macro to servername\n"},
196 */
197 /*
198   These are harder to do with the new code structure
199                 {"show-all-parameters", '\0', POPT_ARG_NONE, &show_all_parameters, 1, "Show the parameters, type, possible values" },
200 */
201                 {"section-name", '\0', POPT_ARG_STRING, &section_name, 0, "Limit testparm to a named section" },
202                 {"parameter-name", '\0', POPT_ARG_STRING, &parameter_name, 0, "Limit testparm to a named parameter" },
203                 {"client-name", '\0', POPT_ARG_STRING, &cname, 0, "Client DNS name for 'hosts allow' checking (should match reverse lookup)"},
204                 {"client-ip", '\0', POPT_ARG_STRING, &caddr, 0, "Client IP address for 'hosts allow' checking"},
205                 POPT_COMMON_SAMBA
206                 POPT_COMMON_VERSION
207                 { NULL }
208         };
209
210         setup_logging(NULL, DEBUG_STDERR);
211
212         pc = poptGetContext(NULL, argc, argv, long_options, 
213                             POPT_CONTEXT_KEEP_FIRST);
214         poptSetOtherOptionHelp(pc, "[OPTION...] [host-name] [host-ip]");
215
216         while(poptGetNextOpt(pc) != -1);
217
218 /* 
219         if (show_all_parameters) {
220                 show_parameter_list();
221                 exit(0);
222         }
223 */
224
225         if ( cname && ! caddr ) {
226                 printf ( "ERROR: For 'hosts allow' check you must specify both a DNS name and an IP address.\n" );
227                 return(1);
228         }
229 /*
230   We need support for smb.conf macros before this will work again 
231
232         if (new_local_machine) {
233                 set_local_machine_name(new_local_machine, True);
234         }
235 */
236
237         lp_ctx = cmdline_lp_ctx;
238         
239         /* We need this to force the output */
240         lp_set_cmdline(lp_ctx, "log level", "2");
241
242         fprintf(stderr, "Loaded smb config files from %s\n", lp_configfile(lp_ctx));
243
244         if (!lp_load(lp_ctx, lp_configfile(lp_ctx))) {
245                 fprintf(stderr,"Error loading services.\n");
246                 return(1);
247         }
248
249         fprintf(stderr,"Loaded services file OK.\n");
250
251         ret = do_global_checks(lp_ctx);
252         ret |= do_share_checks(lp_ctx, cname, caddr, silent_mode, show_defaults, section_name, parameter_name);
253
254         return(ret);
255 }
256