7f53eb5a528e0b9398453ae8416cc321b76662de
[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 #include "param/loadparm.h"
41
42
43 /***********************************************
44  Here we do a set of 'hard coded' checks for bad
45  configuration settings.
46 ************************************************/
47
48 static int do_global_checks(struct loadparm_context *lp_ctx)
49 {
50         int ret = 0;
51
52         if (!directory_exist(lp_lockdir(lp_ctx))) {
53                 fprintf(stderr, "ERROR: lock directory %s does not exist\n",
54                        lp_lockdir(lp_ctx));
55                 ret = 1;
56         }
57
58         if (!directory_exist(lp_piddir(lp_ctx))) {
59                 fprintf(stderr, "ERROR: pid directory %s does not exist\n",
60                        lp_piddir(lp_ctx));
61                 ret = 1;
62         }
63
64         if (strlen(lp_winbind_separator(lp_ctx)) != 1) {
65                 fprintf(stderr,"ERROR: the 'winbind separator' parameter must be a single character.\n");
66                 ret = 1;
67         }
68
69         if (*lp_winbind_separator(lp_ctx) == '+') {
70                 fprintf(stderr,"'winbind separator = +' might cause problems with group membership.\n");
71         }
72
73         return ret;
74 }   
75
76
77 static int do_share_checks(struct loadparm_context *lp_ctx, const char *cname, const char *caddr, bool silent_mode,
78                            bool show_defaults, const char *section_name, const char *parameter_name)
79 {
80         int ret = 0;
81         int s;
82
83         for (s=0;s<lp_numservices(lp_ctx);s++) {
84                 struct loadparm_service *service = lp_servicebynum(lp_ctx, s);
85                 if (service != NULL)
86                         if (strlen(lp_servicename(lp_servicebynum(lp_ctx, s))) > 12) {
87                                 fprintf(stderr, "WARNING: You have some share names that are longer than 12 characters.\n" );
88                                 fprintf(stderr, "These may not be accessible to some older clients.\n" );
89                                 fprintf(stderr, "(Eg. Windows9x, WindowsMe, and not listed in smbclient in Samba 3.0.)\n" );
90                                 break;
91                         }
92         }
93
94         for (s=0;s<lp_numservices(lp_ctx);s++) {
95                 struct loadparm_service *service = lp_servicebynum(lp_ctx, s);
96                 if (service != NULL) {
97                         const char **deny_list = lp_hostsdeny(service, lp_default_service(lp_ctx));
98                         const char **allow_list = lp_hostsallow(service, lp_default_service(lp_ctx));
99                         int i;
100                         if(deny_list) {
101                                 for (i=0; deny_list[i]; i++) {
102                                         char *hasstar = strchr_m(deny_list[i], '*');
103                                         char *hasquery = strchr_m(deny_list[i], '?');
104                                         if(hasstar || hasquery) {
105                                                 fprintf(stderr,"Invalid character %c in hosts deny list (%s) for service %s.\n",
106                                                            hasstar ? *hasstar : *hasquery, deny_list[i], lp_servicename(service) );
107                                         }
108                                 }
109                         }
110
111                         if(allow_list) {
112                                 for (i=0; allow_list[i]; i++) {
113                                         char *hasstar = strchr_m(allow_list[i], '*');
114                                         char *hasquery = strchr_m(allow_list[i], '?');
115                                         if(hasstar || hasquery) {
116                                                 fprintf(stderr,"Invalid character %c in hosts allow list (%s) for service %s.\n",
117                                                            hasstar ? *hasstar : *hasquery, allow_list[i], lp_servicename(service) );
118                                         }
119                                 }
120                         }
121                 }
122         }
123
124
125         if (!cname) {
126                 if (!silent_mode) {
127                         fprintf(stderr,"Press enter to see a dump of your service definitions\n");
128                         fflush(stdout);
129                         getc(stdin);
130                 }
131                 if (section_name != NULL || parameter_name != NULL) {
132                         struct loadparm_service *service = NULL;
133                         if (!section_name) {
134                                 section_name = GLOBAL_NAME;
135                                 service = NULL;
136                         } else if ((!strwicmp(section_name, GLOBAL_NAME)) == 0 &&
137                                  (service=lp_service(lp_ctx, section_name)) == NULL) {
138                                         fprintf(stderr,"Unknown section %s\n",
139                                                 section_name);
140                                         return(1);
141                         }
142                         if (!parameter_name) {
143                                 lp_dump_one(stdout, show_defaults, service, lp_default_service(lp_ctx));
144                         } else {
145                                 ret = !lp_dump_a_parameter(lp_ctx, service, parameter_name, stdout);
146                         }
147                 } else {
148                         lp_dump(lp_ctx, stdout, show_defaults, lp_numservices(lp_ctx));
149                 }
150                 return(ret);
151         }
152
153         if(cname && caddr){
154                 /* this is totally ugly, a real `quick' hack */
155                 for (s=0;s<lp_numservices(lp_ctx);s++) {
156                         struct loadparm_service *service = lp_servicebynum(lp_ctx, s);
157                         if (service != NULL) {
158                                 if (allow_access(NULL, lp_hostsdeny(NULL, lp_default_service(lp_ctx)), lp_hostsallow(NULL, lp_default_service(lp_ctx)), cname, caddr)
159                                     && allow_access(NULL, lp_hostsdeny(service, lp_default_service(lp_ctx)), lp_hostsallow(service, lp_default_service(lp_ctx)), cname, caddr)) {
160                                         fprintf(stderr,"Allow connection from %s (%s) to %s\n",
161                                                    cname,caddr,lp_servicename(service));
162                                 } else {
163                                         fprintf(stderr,"Deny connection from %s (%s) to %s\n",
164                                                    cname,caddr,lp_servicename(service));
165                                 }
166                         }
167                 }
168         }
169
170         return ret;
171 }
172
173
174  int main(int argc, const char *argv[])
175 {
176         static bool silent_mode = false;
177         int ret = 0;
178         poptContext pc;
179 /*
180         static int show_all_parameters = 0;
181         static char *new_local_machine = NULL;
182 */
183         static const char *section_name = NULL;
184         static char *parameter_name = NULL;
185         static const char *cname;
186         static const char *caddr;
187         static bool show_defaults = false;
188         struct loadparm_context *lp_ctx;
189
190         struct poptOption long_options[] = {
191                 POPT_AUTOHELP
192                 {"suppress-prompt", 0, POPT_ARG_NONE, &silent_mode, true, "Suppress prompt for enter"},
193                 {"verbose", 'v', POPT_ARG_NONE, &show_defaults, true, "Show default options too"},
194 /*
195   We need support for smb.conf macros before this will work again 
196                 {"server", 'L',POPT_ARG_STRING, &new_local_machine, 0, "Set %%L macro to servername\n"},
197 */
198 /*
199   These are harder to do with the new code structure
200                 {"show-all-parameters", '\0', POPT_ARG_NONE, &show_all_parameters, 1, "Show the parameters, type, possible values" },
201 */
202                 {"section-name", '\0', POPT_ARG_STRING, &section_name, 0, "Limit testparm to a named section" },
203                 {"parameter-name", '\0', POPT_ARG_STRING, &parameter_name, 0, "Limit testparm to a named parameter" },
204                 {"client-name", '\0', POPT_ARG_STRING, &cname, 0, "Client DNS name for 'hosts allow' checking (should match reverse lookup)"},
205                 {"client-ip", '\0', POPT_ARG_STRING, &caddr, 0, "Client IP address for 'hosts allow' checking"},
206                 POPT_COMMON_SAMBA
207                 POPT_COMMON_VERSION
208                 { NULL }
209         };
210
211         setup_logging(NULL, DEBUG_STDERR);
212
213         pc = poptGetContext(NULL, argc, argv, long_options, 
214                             POPT_CONTEXT_KEEP_FIRST);
215         poptSetOtherOptionHelp(pc, "[OPTION...] [host-name] [host-ip]");
216
217         while(poptGetNextOpt(pc) != -1);
218
219 /* 
220         if (show_all_parameters) {
221                 show_parameter_list();
222                 exit(0);
223         }
224 */
225
226         if ( cname && ! caddr ) {
227                 printf ( "ERROR: For 'hosts allow' check you must specify both a DNS name and an IP address.\n" );
228                 return(1);
229         }
230 /*
231   We need support for smb.conf macros before this will work again 
232
233         if (new_local_machine) {
234                 set_local_machine_name(new_local_machine, True);
235         }
236 */
237
238         lp_ctx = cmdline_lp_ctx;
239         
240         /* We need this to force the output */
241         lp_set_cmdline(lp_ctx, "log level", "2");
242
243         fprintf(stderr, "Loaded smb config files from %s\n", lp_configfile(lp_ctx));
244
245         if (!lp_load(lp_ctx, lp_configfile(lp_ctx))) {
246                 fprintf(stderr,"Error loading services.\n");
247                 return(1);
248         }
249
250         fprintf(stderr,"Loaded services file OK.\n");
251
252         ret = do_global_checks(lp_ctx);
253         ret |= do_share_checks(lp_ctx, cname, caddr, silent_mode, show_defaults, section_name, parameter_name);
254
255         return(ret);
256 }
257