s3:ctdb_conn: add ctdbd_conn_get_fd() to get the fd out of the ctdb connection
[Samba.git] / source4 / utils / testparm.c
blobc8e07973bc40538550f5d7c44ca09e34819b6e18
1 /*
2 Unix SMB/CIFS implementation.
3 Test validity of smb.conf
4 Copyright (C) Karl Auer 1993, 1994-1998
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
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.
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.
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/>.
25 * Testbed for loadparm.c/params.c
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.
31 * Useful for a quick 'syntax check' of a configuration file.
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 #include "param/loadparm.h"
43 /***********************************************
44 Here we do a set of 'hard coded' checks for bad
45 configuration settings.
46 ************************************************/
48 static int do_global_checks(struct loadparm_context *lp_ctx)
50 int ret = 0;
52 if (!directory_exist(lp_lockdir(lp_ctx))) {
53 fprintf(stderr, "ERROR: lock directory %s does not exist\n",
54 lp_lockdir(lp_ctx));
55 ret = 1;
58 if (!directory_exist(lp_piddir(lp_ctx))) {
59 fprintf(stderr, "ERROR: pid directory %s does not exist\n",
60 lp_piddir(lp_ctx));
61 ret = 1;
64 if (strlen(lp_winbind_separator(lp_ctx)) != 1) {
65 fprintf(stderr,"ERROR: the 'winbind separator' parameter must be a single character.\n");
66 ret = 1;
69 if (*lp_winbind_separator(lp_ctx) == '+') {
70 fprintf(stderr,"'winbind separator = +' might cause problems with group membership.\n");
73 return ret;
77 static int do_share_checks(struct loadparm_context *lp_ctx, const char *cname, const char *caddr, bool silent_mode,
78 bool show_defaults, const char *section_name, const char *parameter_name)
80 int ret = 0;
81 int s;
83 for (s=0;s<lp_numservices(lp_ctx);s++) {
84 struct loadparm_service *service = lp_servicebynum(lp_ctx, s);
85 if (service != NULL)
86 if (strlen(lp_servicename(lp_servicebynum(lp_ctx, s))) > 12) {
87 fprintf(stderr, "WARNING: You have some share names that are longer than 12 characters.\n" );
88 fprintf(stderr, "These may not be accessible to some older clients.\n" );
89 fprintf(stderr, "(Eg. Windows9x, WindowsMe, and not listed in smbclient in Samba 3.0.)\n" );
90 break;
94 for (s=0;s<lp_numservices(lp_ctx);s++) {
95 struct loadparm_service *service = lp_servicebynum(lp_ctx, s);
96 if (service != NULL) {
97 const char **deny_list = lp_hostsdeny(service, lp_default_service(lp_ctx));
98 const char **allow_list = lp_hostsallow(service, lp_default_service(lp_ctx));
99 int i;
100 if(deny_list) {
101 for (i=0; deny_list[i]; i++) {
102 char *hasstar = strchr_m(deny_list[i], '*');
103 char *hasquery = strchr_m(deny_list[i], '?');
104 if(hasstar || hasquery) {
105 fprintf(stderr,"Invalid character %c in hosts deny list (%s) for service %s.\n",
106 hasstar ? *hasstar : *hasquery, deny_list[i], lp_servicename(service) );
111 if(allow_list) {
112 for (i=0; allow_list[i]; i++) {
113 char *hasstar = strchr_m(allow_list[i], '*');
114 char *hasquery = strchr_m(allow_list[i], '?');
115 if(hasstar || hasquery) {
116 fprintf(stderr,"Invalid character %c in hosts allow list (%s) for service %s.\n",
117 hasstar ? *hasstar : *hasquery, allow_list[i], lp_servicename(service) );
125 if (!cname) {
126 if (!silent_mode) {
127 fprintf(stderr,"Press enter to see a dump of your service definitions\n");
128 fflush(stdout);
129 getc(stdin);
131 if (section_name != NULL || parameter_name != NULL) {
132 struct loadparm_service *service = NULL;
133 if (!section_name) {
134 section_name = GLOBAL_NAME;
135 service = NULL;
136 } else if ((!strwicmp(section_name, GLOBAL_NAME)) == 0 &&
137 (service=lp_service(lp_ctx, section_name)) == NULL) {
138 fprintf(stderr,"Unknown section %s\n",
139 section_name);
140 return(1);
142 if (!parameter_name) {
143 lp_dump_one(stdout, show_defaults, service, lp_default_service(lp_ctx));
144 } else {
145 ret = !lp_dump_a_parameter(lp_ctx, service, parameter_name, stdout);
147 } else {
148 lp_dump(lp_ctx, stdout, show_defaults, lp_numservices(lp_ctx));
150 return(ret);
153 if(cname && caddr){
154 /* this is totally ugly, a real `quick' hack */
155 for (s=0;s<lp_numservices(lp_ctx);s++) {
156 struct loadparm_service *service = lp_servicebynum(lp_ctx, s);
157 if (service != NULL) {
158 if (allow_access(NULL, lp_hostsdeny(NULL, lp_default_service(lp_ctx)), lp_hostsallow(NULL, lp_default_service(lp_ctx)), cname, caddr)
159 && allow_access(NULL, lp_hostsdeny(service, lp_default_service(lp_ctx)), lp_hostsallow(service, lp_default_service(lp_ctx)), cname, caddr)) {
160 fprintf(stderr,"Allow connection from %s (%s) to %s\n",
161 cname,caddr,lp_servicename(service));
162 } else {
163 fprintf(stderr,"Deny connection from %s (%s) to %s\n",
164 cname,caddr,lp_servicename(service));
170 return ret;
174 int main(int argc, const char *argv[])
176 int ret = 0;
177 poptContext pc;
179 static int show_all_parameters = 0;
180 static char *new_local_machine = NULL;
182 static const char *section_name = NULL;
183 static char *parameter_name = NULL;
184 static const char *cname;
185 static const char *caddr;
186 static int silent_mode = false;
187 static int show_defaults = false; /* This must be an 'int',
188 * as we take it as we pass
189 * it's address as an int
190 * pointer */
191 struct loadparm_context *lp_ctx;
193 struct poptOption long_options[] = {
194 POPT_AUTOHELP
195 {"suppress-prompt", 0, POPT_ARG_NONE, &silent_mode, true, "Suppress prompt for enter"},
196 {"verbose", 'v', POPT_ARG_NONE, &show_defaults, true, "Show default options too"},
198 We need support for smb.conf macros before this will work again
199 {"server", 'L',POPT_ARG_STRING, &new_local_machine, 0, "Set %%L macro to servername\n"},
202 These are harder to do with the new code structure
203 {"show-all-parameters", '\0', POPT_ARG_NONE, &show_all_parameters, 1, "Show the parameters, type, possible values" },
205 {"section-name", '\0', POPT_ARG_STRING, &section_name, 0, "Limit testparm to a named section" },
206 {"parameter-name", '\0', POPT_ARG_STRING, &parameter_name, 0, "Limit testparm to a named parameter" },
207 {"client-name", '\0', POPT_ARG_STRING, &cname, 0, "Client DNS name for 'hosts allow' checking (should match reverse lookup)"},
208 {"client-ip", '\0', POPT_ARG_STRING, &caddr, 0, "Client IP address for 'hosts allow' checking"},
209 POPT_COMMON_SAMBA
210 POPT_COMMON_VERSION
211 { NULL }
214 setup_logging(NULL, DEBUG_STDERR);
216 pc = poptGetContext(NULL, argc, argv, long_options,
217 POPT_CONTEXT_KEEP_FIRST);
218 poptSetOtherOptionHelp(pc, "[OPTION...] [host-name] [host-ip]");
220 while(poptGetNextOpt(pc) != -1);
223 if (show_all_parameters) {
224 show_parameter_list();
225 exit(0);
229 if ( cname && ! caddr ) {
230 printf ( "ERROR: For 'hosts allow' check you must specify both a DNS name and an IP address.\n" );
231 return(1);
234 We need support for smb.conf macros before this will work again
236 if (new_local_machine) {
237 set_local_machine_name(new_local_machine, True);
241 lp_ctx = cmdline_lp_ctx;
243 /* We need this to force the output */
244 lp_set_cmdline(lp_ctx, "log level", "2");
246 fprintf(stderr, "Loaded smb config files from %s\n", lp_configfile(lp_ctx));
248 if (!lp_load(lp_ctx, lp_configfile(lp_ctx))) {
249 fprintf(stderr,"Error loading services.\n");
250 return(1);
253 fprintf(stderr,"Loaded services file OK.\n");
255 ret = do_global_checks(lp_ctx);
256 ret |= do_share_checks(lp_ctx, cname, caddr, silent_mode, show_defaults, section_name, parameter_name);
258 return(ret);