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