2 Unix SMB/CIFS implementation.
5 Copyright (C) Tim Potter 2001,2002
6 Copyright (C) Jelmer Vernooij 2002,2003
7 Copyright (C) James Peach 2006
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 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "popt_common.h"
25 #include "lib/param/param.h"
27 /* Handle command line options:
40 extern bool override_logfile
;
42 static void set_logfile(poptContext con
, const char * arg
)
48 /* Find out basename of current program */
49 pname
= strrchr_m(poptGetInvocationName(con
),'/');
52 pname
= poptGetInvocationName(con
);
56 if (asprintf(&lfile
, "%s/log.%s", arg
, pname
) < 0) {
59 lp_set_logfile(lfile
);
63 static bool PrintSambaVersionString
;
65 static void popt_s3_talloc_log_fn(const char *message
)
67 DEBUG(0,("%s", message
));
70 static void popt_common_callback(poptContext con
,
71 enum poptCallbackReason reason
,
72 const struct poptOption
*opt
,
73 const char *arg
, const void *data
)
76 if (reason
== POPT_CALLBACK_REASON_PRE
) {
77 set_logfile(con
, get_dyn_LOGFILEBASE());
78 talloc_set_log_fn(popt_s3_talloc_log_fn
);
79 talloc_set_abort_fn(smb_panic
);
83 if (reason
== POPT_CALLBACK_REASON_POST
) {
85 if (PrintSambaVersionString
) {
86 printf( "Version %s\n", samba_version_string());
90 if (is_default_dyn_CONFIGFILE()) {
91 if(getenv("SMB_CONF_PATH")) {
92 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
96 /* Further 'every Samba program must do this' hooks here. */
103 struct loadparm_context
*lp_ctx
;
105 lp_ctx
= loadparm_init_s3(talloc_tos(), loadparm_s3_helpers());
106 if (lp_ctx
== NULL
) {
107 fprintf(stderr
, "loadparm_init_s3() failed!\n");
111 if (!lpcfg_set_option(lp_ctx
, arg
)) {
112 fprintf(stderr
, "Error setting option '%s'\n", arg
);
120 lp_set_cmdline("log level", arg
);
125 PrintSambaVersionString
= True
;
130 lp_set_cmdline("socket options", arg
);
136 set_dyn_CONFIGFILE(arg
);
142 lp_set_cmdline("netbios name", arg
);
148 set_logfile(con
, arg
);
149 override_logfile
= True
;
150 set_dyn_LOGFILEBASE(arg
);
156 lp_set_cmdline("netbios scope", arg
);
162 lp_set_cmdline("workgroup", arg
);
168 struct poptOption popt_common_connection
[] = {
169 { NULL
, 0, POPT_ARG_CALLBACK
, (void *)popt_common_callback
},
170 { "socket-options", 'O', POPT_ARG_STRING
, NULL
, 'O', "socket options to use",
172 { "netbiosname", 'n', POPT_ARG_STRING
, NULL
, 'n', "Primary netbios name", "NETBIOSNAME" },
173 { "workgroup", 'W', POPT_ARG_STRING
, NULL
, 'W', "Set the workgroup name", "WORKGROUP" },
174 { "scope", 'i', POPT_ARG_STRING
, NULL
, 'i', "Use this Netbios scope", "SCOPE" },
179 struct poptOption popt_common_samba
[] = {
180 { NULL
, 0, POPT_ARG_CALLBACK
|POPT_CBFLAG_PRE
|POPT_CBFLAG_POST
, (void *)popt_common_callback
},
181 { "debuglevel", 'd', POPT_ARG_STRING
, NULL
, 'd', "Set debug level", "DEBUGLEVEL" },
182 { "configfile", 's', POPT_ARG_STRING
, NULL
, 's', "Use alternate configuration file", "CONFIGFILE" },
183 { "log-basename", 'l', POPT_ARG_STRING
, NULL
, 'l', "Base name for log files", "LOGFILEBASE" },
184 { "version", 'V', POPT_ARG_NONE
, NULL
, 'V', "Print version" },
185 { "option", 0, POPT_ARG_STRING
, NULL
, OPT_OPTION
, "Set smb.conf option from command line", "name=value" },
189 struct poptOption popt_common_configfile
[] = {
190 { NULL
, 0, POPT_ARG_CALLBACK
|POPT_CBFLAG_PRE
|POPT_CBFLAG_POST
, (void *)popt_common_callback
},
191 { "configfile", 0, POPT_ARG_STRING
, NULL
, 's', "Use alternate configuration file", "CONFIGFILE" },
195 struct poptOption popt_common_version
[] = {
196 { NULL
, 0, POPT_ARG_CALLBACK
|POPT_CBFLAG_POST
, (void *)popt_common_callback
},
197 { "version", 'V', POPT_ARG_NONE
, NULL
, 'V', "Print version" },
201 struct poptOption popt_common_debuglevel
[] = {
202 { NULL
, 0, POPT_ARG_CALLBACK
, (void *)popt_common_callback
},
203 { "debuglevel", 'd', POPT_ARG_STRING
, NULL
, 'd', "Set debug level", "DEBUGLEVEL" },
207 struct poptOption popt_common_option
[] = {
208 { NULL
, 0, POPT_ARG_CALLBACK
|POPT_CBFLAG_POST
, (void *)popt_common_callback
},
209 { "option", 0, POPT_ARG_STRING
, NULL
, OPT_OPTION
, "Set smb.conf option from command line", "name=value" },
213 /* Handle command line options:
215 * -A,--authentication-file
224 static struct user_auth_info
*cmdline_auth_info
;
226 struct user_auth_info
*popt_get_cmdline_auth_info(void)
228 return cmdline_auth_info
;
230 void popt_free_cmdline_auth_info(void)
232 TALLOC_FREE(cmdline_auth_info
);
235 static bool popt_common_credentials_ignore_missing_conf
;
236 static bool popt_common_credentials_delay_post
;
238 void popt_common_credentials_set_ignore_missing_conf(void)
240 popt_common_credentials_delay_post
= true;
243 void popt_common_credentials_set_delay_post(void)
245 popt_common_credentials_delay_post
= true;
248 void popt_common_credentials_post(void)
250 if (get_cmdline_auth_info_use_machine_account(cmdline_auth_info
) &&
251 !set_cmdline_auth_info_machine_account_creds(cmdline_auth_info
))
254 "Failed to use machine account credentials\n");
258 set_cmdline_auth_info_getpass(cmdline_auth_info
);
261 * When we set the username during the handling of the options passed to
262 * the binary we haven't loaded the config yet. This means that we
263 * didnn't take the 'winbind separator' into account.
265 * The username might contain the domain name and thus it hasn't been
266 * correctly parsed yet. If we have a username we need to set it again
267 * to run the string parser for the username correctly.
269 reset_cmdline_auth_info_username(cmdline_auth_info
);
272 static void popt_common_credentials_callback(poptContext con
,
273 enum poptCallbackReason reason
,
274 const struct poptOption
*opt
,
275 const char *arg
, const void *data
)
277 if (reason
== POPT_CALLBACK_REASON_PRE
) {
278 struct user_auth_info
*auth_info
=
279 user_auth_info_init(NULL
);
280 if (auth_info
== NULL
) {
281 fprintf(stderr
, "user_auth_info_init() failed\n");
284 cmdline_auth_info
= auth_info
;
288 if (reason
== POPT_CALLBACK_REASON_POST
) {
291 if (override_logfile
) {
292 setup_logging(lp_logfile(talloc_tos()), DEBUG_FILE
);
295 ok
= lp_load_client(get_dyn_CONFIGFILE());
297 const char *pname
= poptGetInvocationName(con
);
299 fprintf(stderr
, "%s: Can't load %s - run testparm to debug it\n",
300 pname
, get_dyn_CONFIGFILE());
301 if (!popt_common_credentials_ignore_missing_conf
) {
308 set_cmdline_auth_info_guess(cmdline_auth_info
);
310 if (popt_common_credentials_delay_post
) {
314 popt_common_credentials_post();
320 set_cmdline_auth_info_username(cmdline_auth_info
, arg
);
324 set_cmdline_auth_info_from_file(cmdline_auth_info
, arg
);
329 d_printf("No kerberos support compiled in\n");
332 set_cmdline_auth_info_use_krb5_ticket(cmdline_auth_info
);
337 if (!set_cmdline_auth_info_signing_state(cmdline_auth_info
,
339 fprintf(stderr
, "Unknown signing option %s\n", arg
);
344 set_cmdline_auth_info_use_machine_account(cmdline_auth_info
);
347 set_cmdline_auth_info_password(cmdline_auth_info
, "");
350 set_cmdline_auth_info_smb_encrypt(cmdline_auth_info
);
353 set_cmdline_auth_info_use_ccache(cmdline_auth_info
, true);
356 set_cmdline_auth_info_use_pw_nt_hash(cmdline_auth_info
, true);
362 * @brief Burn the commandline password.
364 * This function removes the password from the command line so we
365 * don't leak the password e.g. in 'ps aux'.
367 * It should be called after processing the options and you should pass down
370 * @param[in] argc The number of arguments.
372 * @param[in] argv[] The argument array we will find the array.
374 void popt_burn_cmdline_password(int argc
, char *argv
[])
380 for (i
= 0; i
< argc
; i
++) {
382 if (strncmp(p
, "-U", 2) == 0) {
385 } else if (strncmp(p
, "--user", 6) == 0) {
395 if (strlen(p
) == ulen
) {
399 p
= strchr_m(p
, '%');
401 memset(p
, '\0', strlen(p
));
408 struct poptOption popt_common_credentials
[] = {
409 { NULL
, 0, POPT_ARG_CALLBACK
|POPT_CBFLAG_PRE
|POPT_CBFLAG_POST
,
410 (void *)popt_common_credentials_callback
, 0, NULL
},
411 { "user", 'U', POPT_ARG_STRING
, NULL
, 'U', "Set the network username", "USERNAME" },
412 { "no-pass", 'N', POPT_ARG_NONE
, NULL
, 'N', "Don't ask for a password" },
413 { "kerberos", 'k', POPT_ARG_NONE
, NULL
, 'k', "Use kerberos (active directory) authentication" },
414 { "authentication-file", 'A', POPT_ARG_STRING
, NULL
, 'A', "Get the credentials from a file", "FILE" },
415 { "signing", 'S', POPT_ARG_STRING
, NULL
, 'S', "Set the client signing state", "on|off|required" },
416 {"machine-pass", 'P', POPT_ARG_NONE
, NULL
, 'P', "Use stored machine account password" },
417 {"encrypt", 'e', POPT_ARG_NONE
, NULL
, 'e', "Encrypt SMB transport" },
418 {"use-ccache", 'C', POPT_ARG_NONE
, NULL
, 'C',
419 "Use the winbind ccache for authentication" },
420 {"pw-nt-hash", '\0', POPT_ARG_NONE
, NULL
, 'H',
421 "The supplied password is the NT hash" },