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