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