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 "system/filesys.h"
25 #include "popt_common.h"
26 #include "lib/param/param.h"
28 /* Handle command line options:
41 extern bool override_logfile;
43 static void set_logfile(poptContext con, const char * arg)
49 /* Find out basename of current program */
50 pname = strrchr_m(poptGetInvocationName(con),'/');
53 pname = poptGetInvocationName(con);
57 if (asprintf(&lfile, "%s/log.%s", arg, pname) < 0) {
60 lp_set_logfile(lfile);
64 static bool PrintSambaVersionString;
66 static void popt_s3_talloc_log_fn(const char *message)
68 DEBUG(0,("%s", message));
71 static void popt_common_callback(poptContext con,
72 enum poptCallbackReason reason,
73 const struct poptOption *opt,
74 const char *arg, const void *data)
77 if (reason == POPT_CALLBACK_REASON_PRE) {
78 set_logfile(con, get_dyn_LOGFILEBASE());
79 talloc_set_log_fn(popt_s3_talloc_log_fn);
80 talloc_set_abort_fn(smb_panic);
84 if (reason == POPT_CALLBACK_REASON_POST) {
86 if (PrintSambaVersionString) {
87 printf( "Version %s\n", samba_version_string());
91 if (is_default_dyn_CONFIGFILE()) {
92 if(getenv("SMB_CONF_PATH")) {
93 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
97 /* Further 'every Samba program must do this' hooks here. */
104 struct loadparm_context *lp_ctx;
106 lp_ctx = loadparm_init_s3(talloc_tos(), loadparm_s3_helpers());
107 if (lp_ctx == NULL) {
108 fprintf(stderr, "loadparm_init_s3() failed!\n");
112 if (!lpcfg_set_option(lp_ctx, arg)) {
113 fprintf(stderr, "Error setting option '%s'\n", arg);
121 lp_set_cmdline("log level", arg);
126 PrintSambaVersionString = True;
131 lp_do_parameter(-1, "socket options", arg);
137 set_dyn_CONFIGFILE(arg);
143 lp_set_cmdline("netbios name", arg);
149 set_logfile(con, arg);
150 override_logfile = True;
151 set_dyn_LOGFILEBASE(arg);
157 lp_set_cmdline("netbios scope", arg);
163 lp_set_cmdline("workgroup", arg);
169 struct poptOption popt_common_connection[] = {
170 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
171 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
173 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
174 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
175 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
180 struct poptOption popt_common_samba[] = {
181 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
182 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
183 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
184 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Base name for log files", "LOGFILEBASE" },
185 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
186 { "option", 0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
190 struct poptOption popt_common_configfile[] = {
191 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
192 { "configfile", 0, POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
196 struct poptOption popt_common_version[] = {
197 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_POST, (void *)popt_common_callback },
198 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
202 struct poptOption popt_common_debuglevel[] = {
203 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
204 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
208 struct poptOption popt_common_option[] = {
209 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_POST, (void *)popt_common_callback },
210 { "option", 0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
214 /****************************************************************************
215 * get a password from a a file or file descriptor
217 * ****************************************************************************/
219 static void get_password_file(struct user_auth_info *auth_info)
223 bool close_it = False;
227 if ((p = getenv("PASSWD_FD")) != NULL) {
228 if (asprintf(&spec, "descriptor %s", p) < 0) {
231 sscanf(p, "%d", &fd);
233 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
234 fd = open(p, O_RDONLY, 0);
235 spec = SMB_STRDUP(p);
237 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
238 spec, strerror(errno));
245 fprintf(stderr, "fd = %d, < 0\n", fd);
249 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
250 p && p - pass < sizeof(pass);) {
251 switch (read(fd, p, 1)) {
253 if (*p != '\n' && *p != '\0') {
254 *++p = '\0'; /* advance p, and null-terminate pass */
259 *p = '\0'; /* null-terminate it, just in case... */
260 p = NULL; /* then force the loop condition to become false */
263 fprintf(stderr, "Error reading password from file %s: %s\n",
264 spec, "empty password\n");
270 fprintf(stderr, "Error reading password from file %s: %s\n",
271 spec, strerror(errno));
278 set_cmdline_auth_info_password(auth_info, pass);
284 static void get_credentials_file(struct user_auth_info *auth_info,
290 char *ptr, *val, *param;
292 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
294 /* fail if we can't open the credentials file */
295 d_printf("ERROR: Unable to open credentials file!\n");
299 while (!x_feof(auth))
301 /* get a line from the file */
302 if (!x_fgets(buf, sizeof(buf), auth))
306 if ((len) && (buf[len-1]=='\n'))
314 /* break up the line into parameter & value.
315 * will need to eat a little whitespace possibly */
317 if (!(ptr = strchr_m (buf, '=')))
323 /* eat leading white space */
324 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
327 if (strwicmp("password", param) == 0) {
328 set_cmdline_auth_info_password(auth_info, val);
329 } else if (strwicmp("username", param) == 0) {
330 set_cmdline_auth_info_username(auth_info, val);
331 } else if (strwicmp("domain", param) == 0) {
332 set_cmdline_auth_info_domain(auth_info, val);
334 memset(buf, 0, sizeof(buf));
339 /* Handle command line options:
341 * -A,--authentication-file
351 static void popt_common_credentials_callback(poptContext con,
352 enum poptCallbackReason reason,
353 const struct poptOption *opt,
354 const char *arg, const void *data)
356 struct user_auth_info *auth_info = talloc_get_type_abort(
357 *((const char **)data), struct user_auth_info);
359 if (reason == POPT_CALLBACK_REASON_PRE) {
360 set_cmdline_auth_info_username(auth_info, "GUEST");
362 if (getenv("LOGNAME")) {
363 set_cmdline_auth_info_username(auth_info,
367 if (getenv("USER")) {
368 char *puser = SMB_STRDUP(getenv("USER"));
372 set_cmdline_auth_info_username(auth_info, puser);
375 if (getenv("PASSWD")) {
376 set_cmdline_auth_info_password(auth_info,
380 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
381 get_password_file(auth_info);
391 char *puser = SMB_STRDUP(arg);
393 if ((lp=strchr_m(puser,'%'))) {
396 set_cmdline_auth_info_username(auth_info,
398 set_cmdline_auth_info_password(auth_info,
401 memset(lp + 1, '\0', len);
403 set_cmdline_auth_info_username(auth_info,
411 get_credentials_file(auth_info, arg);
416 d_printf("No kerberos support compiled in\n");
419 set_cmdline_auth_info_use_krb5_ticket(auth_info);
424 if (!set_cmdline_auth_info_signing_state(auth_info, arg)) {
425 fprintf(stderr, "Unknown signing option %s\n", arg );
430 set_cmdline_auth_info_use_machine_account(auth_info);
433 set_cmdline_auth_info_password(auth_info, "");
436 set_cmdline_auth_info_smb_encrypt(auth_info);
439 set_cmdline_auth_info_use_ccache(auth_info, true);
442 set_cmdline_auth_info_use_pw_nt_hash(auth_info, true);
447 static struct user_auth_info *global_auth_info;
449 void popt_common_set_auth_info(struct user_auth_info *auth_info)
451 global_auth_info = auth_info;
455 * @brief Burn the commandline password.
457 * This function removes the password from the command line so we
458 * don't leak the password e.g. in 'ps aux'.
460 * It should be called after processing the options and you should pass down
463 * @param[in] argc The number of arguments.
465 * @param[in] argv[] The argument array we will find the array.
467 void popt_burn_cmdline_password(int argc, char *argv[])
473 for (i = 0; i < argc; i++) {
475 if (strncmp(p, "-U", 2) == 0) {
478 } else if (strncmp(p, "--user", 6) == 0) {
488 if (strlen(p) == ulen) {
492 p = strchr_m(p, '%');
494 memset(p, '\0', strlen(p));
501 struct poptOption popt_common_credentials[] = {
502 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE,
503 (void *)popt_common_credentials_callback, 0,
504 (const char *)&global_auth_info },
505 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
506 { "no-pass", 'N', POPT_ARG_NONE, NULL, 'N', "Don't ask for a password" },
507 { "kerberos", 'k', POPT_ARG_NONE, NULL, 'k', "Use kerberos (active directory) authentication" },
508 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
509 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
510 {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
511 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },
512 {"use-ccache", 'C', POPT_ARG_NONE, NULL, 'C',
513 "Use the winbind ccache for authentication" },
514 {"pw-nt-hash", '\0', POPT_ARG_NONE, NULL, 'H',
515 "The supplied password is the NT hash" },