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