9397ea252016f5af9eec516f37a9f54adc93102f
[vlendec/samba-autobuild/.git] / source3 / utils / testparm.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Test validity of smb.conf
5    Copyright (C) Karl Auer 1993, 1994-1998
6
7    Extensively modified by Andrew Tridgell, 1995
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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 "smb.h"
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                 printf("ERROR: in 'security=domain' mode the 'encrypt passwords' parameter must also be set to 'true'.\n");
50                 ret = 1;
51         }
52
53         if (lp_wins_support() && wins_srv_count()) {
54                 printf("ERROR: both 'wins support = true' and 'wins server = <server>' \
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                 printf("ERROR: lock directory %s does not exist\n",
61                        lp_lockdir());
62                 ret = 1;
63         } else if ((st.st_mode & 0777) != 0755) {
64                 printf("WARNING: lock directory %s should have permissions 0755 for browsing to work\n",
65                        lp_lockdir());
66                 ret = 1;
67         }
68
69         /*
70          * Password server sanity checks.
71          */
72
73         if((lp_security() == SEC_SERVER || lp_security() == SEC_DOMAIN) && !lp_passwordserver()) {
74                 pstring sec_setting;
75                 if(lp_security() == SEC_SERVER)
76                         pstrcpy(sec_setting, "server");
77                 else if(lp_security() == SEC_DOMAIN)
78                         pstrcpy(sec_setting, "domain");
79
80                 printf("ERROR: The setting 'security=%s' requires the 'password server' parameter be set \
81 to a valid password server.\n", sec_setting );
82                 ret = 1;
83         }
84
85         
86         /*
87          * Check 'hosts equiv' and 'use rhosts' compatability with 'hostname lookup' value.
88          */
89
90         if(*lp_hosts_equiv() && !lp_hostname_lookups()) {
91                 printf("ERROR: The setting 'hosts equiv = %s' requires that 'hostname lookups = yes'.\n", lp_hosts_equiv());
92                 ret = 1;
93         }
94
95         if(lp_use_rhosts() && !lp_hostname_lookups()) {
96                 printf("ERROR: The setting 'use rhosts = yes' requires the 'hostname lookups = yes'.\n");
97                 ret = 1;
98         }
99
100
101         /*
102          * Password chat sanity checks.
103          */
104
105         if(lp_security() == SEC_USER && lp_unix_password_sync()) {
106
107                 /*
108                  * Check that we have a valid lp_passwd_program() if not using pam.
109                  */
110
111 #ifdef WITH_PAM
112                 if (!lp_pam_password_change()) {
113 #endif
114
115                         if(lp_passwd_program() == NULL) {
116                                 printf("ERROR: the 'unix password sync' parameter is set and there is no valid 'passwd program' \
117 parameter.\n" );
118                                 ret = 1;
119                         } else {
120                                 pstring passwd_prog;
121                                 pstring truncated_prog;
122                                 char *p;
123
124                                 pstrcpy( passwd_prog, lp_passwd_program());
125                                 p = passwd_prog;
126                                 *truncated_prog = '\0';
127                                 next_token(&p, truncated_prog, NULL, sizeof(pstring));
128
129                                 if(access(truncated_prog, F_OK) == -1) {
130                                         printf("ERROR: the 'unix password sync' parameter is set and the 'passwd program' (%s) \
131 cannot be executed (error was %s).\n", truncated_prog, strerror(errno) );
132                                         ret = 1;
133                                 }
134                         }
135
136 #ifdef WITH_PAM
137                 }
138 #endif
139
140                 if(lp_passwd_chat() == NULL) {
141                         printf("ERROR: the 'unix password sync' parameter is set and there is no valid 'passwd chat' \
142 parameter.\n");
143                         ret = 1;
144                 }
145
146                 /*
147                  * Check that we have a valid script and that it hasn't
148                  * been written to expect the old password.
149                  */
150
151                 if(lp_encrypted_passwords()) {
152                         if(strstr( lp_passwd_chat(), "%o")!=NULL) {
153                                 printf("ERROR: the 'passwd chat' script [%s] expects to use the old plaintext password \
154 via the %%o substitution. With encrypted passwords this is not possible.\n", lp_passwd_chat() );
155                                 ret = 1;
156                         }
157                 }
158         }
159
160         if (lp_status(-1) && lp_max_smbd_processes()) {
161                 printf("ERROR: the 'max smbd processes' parameter is set and the 'status' parameter is set to 'no'.\n");
162                 ret = 1;
163         }
164
165         return ret;
166 }   
167
168 static void usage(char *pname)
169 {
170         printf("Usage: %s [-sh] [-L servername] [configfilename] [hostname hostIP]\n", pname);
171         printf("\t-s                  Suppress prompt for enter\n");
172         printf("\t-h                  Print usage\n");
173         printf("\t-L servername       Set %%L macro to servername\n");
174         printf("\t-t encoding         Print parameters with encoding\n");
175         printf("\tconfigfilename      Configuration file to test\n");
176         printf("\thostname hostIP.    Hostname and Host IP address to test\n");
177         printf("\t                    against \"host allow\" and \"host deny\"\n");
178         printf("\n");
179 }
180
181
182 int main(int argc, char *argv[])
183 {
184   extern char *optarg;
185   extern int optind;
186   extern fstring local_machine;
187   pstring configfile;
188   int opt;
189   int s;
190   BOOL silent_mode = False;
191   int ret = 0;
192   pstring term_code;
193
194   *term_code = 0;
195
196   TimeInit();
197
198   setup_logging(argv[0],True);
199   
200   while ((opt = getopt(argc, argv,"shL:t:")) != EOF) {
201   switch (opt) {
202     case 's':
203       silent_mode = True;
204       break;
205     case 'L':
206       fstrcpy(local_machine,optarg);
207       break;
208     case 'h':
209       usage(argv[0]);
210       exit(0);
211       break;
212     case 't':
213       pstrcpy(term_code,optarg);
214       break;
215     default:
216       printf("Incorrect program usage\n");
217       usage(argv[0]);
218       exit(1);
219       break;
220     }
221   }
222
223   argc += (1 - optind);
224
225   if ((argc == 1) || (argc == 3))
226     pstrcpy(configfile,CONFIGFILE);
227   else if ((argc == 2) || (argc == 4))
228     pstrcpy(configfile,argv[optind]);
229
230   dbf = x_stdout;
231   DEBUGLEVEL = 2;
232
233   printf("Load smb config files from %s\n",configfile);
234
235   if (!lp_load(configfile,False,True,False)) {
236       printf("Error loading services.\n");
237       return(1);
238   }
239
240   printf("Loaded services file OK.\n");
241
242   ret = do_global_checks();
243
244   for (s=0;s<1000;s++) {
245     if (VALID_SNUM(s))
246       if (strlen(lp_servicename(s)) > 8) {
247         printf("WARNING: You have some share names that are longer than 8 chars\n");
248         printf("These may give errors while browsing or may not be accessible\nto some older clients\n");
249         break;
250       }
251   }
252
253   for (s=0;s<1000;s++) {
254     if (VALID_SNUM(s)) {
255       char **deny_list = lp_hostsdeny(s);
256       char **allow_list = lp_hostsallow(s);
257       int i;
258       if(deny_list) {
259         for (i=0; deny_list[i]; i++) {
260           char *hasstar = strchr_m(deny_list[i], '*');
261           char *hasquery = strchr_m(deny_list[i], '?');
262           if(hasstar || hasquery) {
263             printf("Invalid character %c in hosts deny list (%s) for service %s.\n",
264                  hasstar ? *hasstar : *hasquery, deny_list[i], lp_servicename(s) );
265           }
266         }
267       }
268
269       if(allow_list) {
270         for (i=0; allow_list[i]; i++) {
271           char *hasstar = strchr_m(allow_list[i], '*');
272           char *hasquery = strchr_m(allow_list[i], '?');
273           if(hasstar || hasquery) {
274             printf("Invalid character %c in hosts allow list (%s) for service %s.\n",
275                  hasstar ? *hasstar : *hasquery, allow_list[i], lp_servicename(s) );
276           }
277         }
278       }
279
280       if(lp_level2_oplocks(s) && !lp_oplocks(s)) {
281         printf("Invalid combination of parameters for service %s. \
282 Level II oplocks can only be set if oplocks are also set.\n",
283                lp_servicename(s) );
284       }
285     }
286   }
287
288   if (argc < 3) {
289     if (!silent_mode) {
290       printf("Press enter to see a dump of your service definitions\n");
291       fflush(stdout);
292       getc(stdin);
293     }
294     lp_dump(stdout,True, lp_numservices());
295   }
296   
297   if (argc >= 3) {
298     char *cname;
299     char *caddr;
300       
301     if (argc == 3) {
302       cname = argv[optind];
303       caddr = argv[optind+1];
304     } else {
305       cname = argv[optind+1];
306       caddr = argv[optind+2];
307     }
308
309     /* this is totally ugly, a real `quick' hack */
310     for (s=0;s<1000;s++) {
311       if (VALID_SNUM(s)) {               
312         if (allow_access(lp_hostsdeny(s),lp_hostsallow(s),cname,caddr)) {
313           printf("Allow connection from %s (%s) to %s\n",
314                  cname,caddr,lp_servicename(s));
315         } else {
316           printf("Deny connection from %s (%s) to %s\n",
317                  cname,caddr,lp_servicename(s));
318         }
319       }
320     }
321   }
322   return(ret);
323 }