[s3]testparm: give testparm one common exit point and call gfree_loadparm().
[kai/samba.git] / source3 / 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    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 /*
24  * Testbed for loadparm.c/params.c
25  *
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.
29  *
30  * Useful for a quick 'syntax check' of a configuration file.
31  *
32  */
33
34 #include "includes.h"
35
36 extern bool AllowDebugChange;
37
38 /***********************************************
39  Here we do a set of 'hard coded' checks for bad
40  configuration settings.
41 ************************************************/
42
43 static int do_global_checks(void)
44 {
45         int ret = 0;
46         SMB_STRUCT_STAT st;
47
48         if (lp_security() >= SEC_DOMAIN && !lp_encrypted_passwords()) {
49                 fprintf(stderr, "ERROR: in 'security=domain' mode the 'encrypt passwords' parameter must always be set to 'true'.\n");
50                 ret = 1;
51         }
52
53         if (lp_wins_support() && lp_wins_server_list()) {
54                 fprintf(stderr, "ERROR: both 'wins support = true' and 'wins server = <server list>' \
55 cannot be set in the smb.conf file. nmbd will abort with this setting.\n");
56                 ret = 1;
57         }
58
59         if (!directory_exist(lp_lockdir(), &st)) {
60                 fprintf(stderr, "ERROR: lock directory %s does not exist\n",
61                        lp_lockdir());
62                 ret = 1;
63         } else if ((st.st_mode & 0777) != 0755) {
64                 fprintf(stderr, "WARNING: lock directory %s should have permissions 0755 for browsing to work\n",
65                        lp_lockdir());
66                 ret = 1;
67         }
68
69         if (!directory_exist(lp_piddir(), &st)) {
70                 fprintf(stderr, "ERROR: pid directory %s does not exist\n",
71                        lp_piddir());
72                 ret = 1;
73         }
74
75         if (lp_passdb_expand_explicit()) {
76                 fprintf(stderr, "WARNING: passdb expand explicit = yes is "
77                         "deprecated\n");
78         }
79
80         /*
81          * Password server sanity checks.
82          */
83
84         if((lp_security() == SEC_SERVER || lp_security() >= SEC_DOMAIN) && !lp_passwordserver()) {
85                 const char *sec_setting;
86                 if(lp_security() == SEC_SERVER)
87                         sec_setting = "server";
88                 else if(lp_security() == SEC_DOMAIN)
89                         sec_setting = "domain";
90                 else
91                         sec_setting = "";
92
93                 fprintf(stderr, "ERROR: The setting 'security=%s' requires the 'password server' parameter be set \
94 to a valid password server.\n", sec_setting );
95                 ret = 1;
96         }
97
98         /*
99          * Password chat sanity checks.
100          */
101
102         if(lp_security() == SEC_USER && lp_unix_password_sync()) {
103
104                 /*
105                  * Check that we have a valid lp_passwd_program() if not using pam.
106                  */
107
108 #ifdef WITH_PAM
109                 if (!lp_pam_password_change()) {
110 #endif
111
112                         if((lp_passwd_program() == NULL) ||
113                            (strlen(lp_passwd_program()) == 0))
114                         {
115                                 fprintf( stderr, "ERROR: the 'unix password sync' parameter is set and there is no valid 'passwd program' \
116 parameter.\n" );
117                                 ret = 1;
118                         } else {
119                                 const char *passwd_prog;
120                                 char *truncated_prog = NULL;
121                                 const char *p;
122
123                                 passwd_prog = lp_passwd_program();
124                                 p = passwd_prog;
125                                 next_token_talloc(talloc_tos(),
126                                                 &p,
127                                                 &truncated_prog, NULL);
128                                 if (truncated_prog && access(truncated_prog, F_OK) == -1) {
129                                         fprintf(stderr, "ERROR: the 'unix password sync' parameter is set and the 'passwd program' (%s) \
130 cannot be executed (error was %s).\n", truncated_prog, strerror(errno) );
131                                         ret = 1;
132                                 }
133                         }
134
135 #ifdef WITH_PAM
136                 }
137 #endif
138
139                 if(lp_passwd_chat() == NULL) {
140                         fprintf(stderr, "ERROR: the 'unix password sync' parameter is set and there is no valid 'passwd chat' \
141 parameter.\n");
142                         ret = 1;
143                 }
144
145                 if ((lp_passwd_program() != NULL) &&
146                     (strlen(lp_passwd_program()) > 0))
147                 {
148                         /* check if there's a %u parameter present */
149                         if(strstr_m(lp_passwd_program(), "%u") == NULL) {
150                                 fprintf(stderr, "ERROR: the 'passwd program' (%s) requires a '%%u' parameter.\n", lp_passwd_program());
151                                 ret = 1;
152                         }
153                 }
154
155                 /*
156                  * Check that we have a valid script and that it hasn't
157                  * been written to expect the old password.
158                  */
159
160                 if(lp_encrypted_passwords()) {
161                         if(strstr_m( lp_passwd_chat(), "%o")!=NULL) {
162                                 fprintf(stderr, "ERROR: the 'passwd chat' script [%s] expects to use the old plaintext password \
163 via the %%o substitution. With encrypted passwords this is not possible.\n", lp_passwd_chat() );
164                                 ret = 1;
165                         }
166                 }
167         }
168
169         if (strlen(lp_winbind_separator()) != 1) {
170                 fprintf(stderr,"ERROR: the 'winbind separator' parameter must be a single character.\n");
171                 ret = 1;
172         }
173
174         if (*lp_winbind_separator() == '+') {
175                 fprintf(stderr,"'winbind separator = +' might cause problems with group membership.\n");
176         }
177
178         if (lp_algorithmic_rid_base() < BASE_RID) {
179                 /* Try to prevent admin foot-shooting, we can't put algorithmic
180                    rids below 1000, that's the 'well known RIDs' on NT */
181                 fprintf(stderr,"'algorithmic rid base' must be equal to or above %lu\n", BASE_RID);
182         }
183
184         if (lp_algorithmic_rid_base() & 1) {
185                 fprintf(stderr,"'algorithmic rid base' must be even.\n");
186         }
187
188 #ifndef HAVE_DLOPEN
189         if (lp_preload_modules()) {
190                 fprintf(stderr,"WARNING: 'preload modules = ' set while loading plugins not supported.\n");
191         }
192 #endif
193
194         if (!lp_passdb_backend()) {
195                 fprintf(stderr,"ERROR: passdb backend must have a value or be left out\n");
196         }
197         
198         if (lp_os_level() > 255) {
199                 fprintf(stderr,"WARNING: Maximum value for 'os level' is 255!\n");      
200         }
201
202         return ret;
203 }   
204
205 /**
206  * per-share logic tests
207  */
208 static void do_per_share_checks(int s)
209 {
210         const char **deny_list = lp_hostsdeny(s);
211         const char **allow_list = lp_hostsallow(s);
212         int i;
213
214         if(deny_list) {
215                 for (i=0; deny_list[i]; i++) {
216                         char *hasstar = strchr_m(deny_list[i], '*');
217                         char *hasquery = strchr_m(deny_list[i], '?');
218                         if(hasstar || hasquery) {
219                                 fprintf(stderr,"Invalid character %c in hosts deny list (%s) for service %s.\n",
220                                            hasstar ? *hasstar : *hasquery, deny_list[i], lp_servicename(s) );
221                         }
222                 }
223         }
224
225         if(allow_list) {
226                 for (i=0; allow_list[i]; i++) {
227                         char *hasstar = strchr_m(allow_list[i], '*');
228                         char *hasquery = strchr_m(allow_list[i], '?');
229                         if(hasstar || hasquery) {
230                                 fprintf(stderr,"Invalid character %c in hosts allow list (%s) for service %s.\n",
231                                            hasstar ? *hasstar : *hasquery, allow_list[i], lp_servicename(s) );
232                         }
233                 }
234         }
235
236         if(lp_level2_oplocks(s) && !lp_oplocks(s)) {
237                 fprintf(stderr,"Invalid combination of parameters for service %s. \
238                            Level II oplocks can only be set if oplocks are also set.\n",
239                            lp_servicename(s) );
240         }
241
242         if (lp_map_hidden(s) && !(lp_create_mask(s) & S_IXOTH)) {
243                 fprintf(stderr,"Invalid combination of parameters for service %s. \
244                            Map hidden can only work if create mask includes octal 01 (S_IXOTH).\n",
245                            lp_servicename(s) );
246         }
247         if (lp_map_hidden(s) && (lp_force_create_mode(s) & S_IXOTH)) {
248                 fprintf(stderr,"Invalid combination of parameters for service %s. \
249                            Map hidden can only work if force create mode excludes octal 01 (S_IXOTH).\n",
250                            lp_servicename(s) );
251         }
252         if (lp_map_system(s) && !(lp_create_mask(s) & S_IXGRP)) {
253                 fprintf(stderr,"Invalid combination of parameters for service %s. \
254                            Map system can only work if create mask includes octal 010 (S_IXGRP).\n",
255                            lp_servicename(s) );
256         }
257         if (lp_map_system(s) && (lp_force_create_mode(s) & S_IXGRP)) {
258                 fprintf(stderr,"Invalid combination of parameters for service %s. \
259                            Map system can only work if force create mode excludes octal 010 (S_IXGRP).\n",
260                            lp_servicename(s) );
261         }
262 #ifdef HAVE_CUPS
263         if (lp_printing(s) == PRINT_CUPS && *(lp_printcommand(s)) != '\0') {
264                  fprintf(stderr,"Warning: Service %s defines a print command, but \
265 rameter is ignored when using CUPS libraries.\n",
266                            lp_servicename(s) );
267         }
268 #endif
269 }
270
271  int main(int argc, const char *argv[])
272 {
273         const char *config_file = get_dyn_CONFIGFILE();
274         int s;
275         static int silent_mode = False;
276         static int show_all_parameters = False;
277         int ret = 0;
278         poptContext pc;
279         static const char *term_code = "";
280         static char *parameter_name = NULL;
281         static const char *section_name = NULL;
282         static char *new_local_machine = NULL;
283         const char *cname;
284         const char *caddr;
285         static int show_defaults;
286         static int skip_logic_checks = 0;
287
288         struct poptOption long_options[] = {
289                 POPT_AUTOHELP
290                 {"suppress-prompt", 's', POPT_ARG_VAL, &silent_mode, 1, "Suppress prompt for enter"},
291                 {"verbose", 'v', POPT_ARG_NONE, &show_defaults, 1, "Show default options too"},
292                 {"server", 'L',POPT_ARG_STRING, &new_local_machine, 0, "Set %%L macro to servername\n"},
293                 {"encoding", 't', POPT_ARG_STRING, &term_code, 0, "Print parameters with encoding"},
294                 {"skip-logic-checks", 'l', POPT_ARG_NONE, &skip_logic_checks, 1, "Skip the global checks"},
295                 {"show-all-parameters", '\0', POPT_ARG_VAL, &show_all_parameters, True, "Show the parameters, type, possible values" },
296                 {"parameter-name", '\0', POPT_ARG_STRING, &parameter_name, 0, "Limit testparm to a named parameter" },
297                 {"section-name", '\0', POPT_ARG_STRING, &section_name, 0, "Limit testparm to a named section" },
298                 POPT_COMMON_VERSION
299                 POPT_COMMON_DEBUGLEVEL
300                 POPT_TABLEEND
301         };
302
303         TALLOC_CTX *frame = talloc_stackframe();
304
305         load_case_tables();
306         /*
307          * Set the default debug level to 2.
308          * Allow it to be overridden by the command line,
309          * not by smb.conf.
310          */
311         DEBUGLEVEL_CLASS[DBGC_ALL] = 2;
312
313         pc = poptGetContext(NULL, argc, argv, long_options, 
314                             POPT_CONTEXT_KEEP_FIRST);
315         poptSetOtherOptionHelp(pc, "[OPTION...] <config-file> [host-name] [host-ip]");
316
317         while(poptGetNextOpt(pc) != -1);
318
319         if (show_all_parameters) {
320                 show_parameter_list();
321                 exit(0);
322         }
323
324         setup_logging(poptGetArg(pc), True);
325
326         if (poptPeekArg(pc)) 
327                 config_file = poptGetArg(pc);
328
329         cname = poptGetArg(pc);
330         caddr = poptGetArg(pc);
331
332         poptFreeContext(pc);
333
334         if ( cname && ! caddr ) {
335                 printf ( "ERROR: You must specify both a machine name and an IP address.\n" );
336                 ret = 1;
337                 goto done;
338         }
339
340         if (new_local_machine) {
341                 set_local_machine_name(new_local_machine, True);
342         }
343
344         dbf = x_stderr;
345         /* Don't let the debuglevel be changed by smb.conf. */
346         AllowDebugChange = False;
347
348         fprintf(stderr,"Load smb config files from %s\n",config_file);
349
350         if (!lp_load_with_registry_shares(config_file,False,True,False,True)) {
351                 fprintf(stderr,"Error loading services.\n");
352                 ret = 1;
353                 goto done;
354         }
355
356         fprintf(stderr,"Loaded services file OK.\n");
357
358         if (skip_logic_checks == 0) {
359                 ret = do_global_checks();
360         }
361
362         for (s=0;s<1000;s++) {
363                 if (VALID_SNUM(s))
364                         if (strlen(lp_servicename(s)) > 12) {
365                                 fprintf(stderr, "WARNING: You have some share names that are longer than 12 characters.\n" );
366                                 fprintf(stderr, "These may not be accessible to some older clients.\n" );
367                                 fprintf(stderr, "(Eg. Windows9x, WindowsMe, and smbclient prior to Samba 3.0.)\n" );
368                                 break;
369                         }
370         }
371
372         for (s=0;s<1000;s++) {
373                 if (VALID_SNUM(s) && (skip_logic_checks == 0)) {
374                         do_per_share_checks(s);
375                 }
376         }
377
378
379         if (!section_name && !parameter_name) {
380                 fprintf(stderr,"Server role: %s\n", server_role_str(lp_server_role()));
381         }
382
383         if (!cname) {
384                 if (!silent_mode) {
385                         fprintf(stderr,"Press enter to see a dump of your service definitions\n");
386                         fflush(stdout);
387                         getc(stdin);
388                 }
389                 if (parameter_name || section_name) {
390                         bool isGlobal = False;
391                         s = GLOBAL_SECTION_SNUM;
392
393                         if (!section_name) {
394                                 section_name = GLOBAL_NAME;
395                                 isGlobal = True;
396                         } else if ((isGlobal=!strwicmp(section_name, GLOBAL_NAME)) == 0 &&
397                                  (s=lp_servicenumber(section_name)) == -1) {
398                                         fprintf(stderr,"Unknown section %s\n",
399                                                 section_name);
400                                         ret = 1;
401                                         goto done;
402                         }
403                         if (parameter_name) {
404                                 if (!dump_a_parameter( s, parameter_name, stdout, isGlobal)) {
405                                         fprintf(stderr,"Parameter %s unknown for section %s\n",
406                                                 parameter_name, section_name);
407                                         ret = 1;
408                                         goto done;
409                                 }
410                         } else {
411                                 if (isGlobal == True)
412                                         lp_dump(stdout, show_defaults, 0);
413                                 else
414                                         lp_dump_one(stdout, show_defaults, s);
415                         }
416                         goto done;
417                 }
418
419                 lp_dump(stdout, show_defaults, lp_numservices());
420         }
421
422         if(cname && caddr){
423                 /* this is totally ugly, a real `quick' hack */
424                 for (s=0;s<1000;s++) {
425                         if (VALID_SNUM(s)) {
426                                 if (allow_access(lp_hostsdeny(-1), lp_hostsallow(-1), cname, caddr)
427                                     && allow_access(lp_hostsdeny(s), lp_hostsallow(s), cname, caddr)) {
428                                         fprintf(stderr,"Allow connection from %s (%s) to %s\n",
429                                                    cname,caddr,lp_servicename(s));
430                                 } else {
431                                         fprintf(stderr,"Deny connection from %s (%s) to %s\n",
432                                                    cname,caddr,lp_servicename(s));
433                                 }
434                         }
435                 }
436         }
437
438 done:
439         TALLOC_FREE(frame);
440         gfree_loadparm();
441         return ret;
442 }
443