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"
26 /* Handle command line options:
39 extern bool override_logfile;
41 static void set_logfile(poptContext con, const char * arg)
47 /* Find out basename of current program */
48 pname = strrchr_m(poptGetInvocationName(con),'/');
51 pname = poptGetInvocationName(con);
55 if (asprintf(&lfile, "%s/log.%s", arg, pname) < 0) {
58 lp_set_logfile(lfile);
62 static bool PrintSambaVersionString;
64 static void popt_s3_talloc_log_fn(const char *message)
66 DEBUG(0,("%s", message));
69 static void popt_common_callback(poptContext con,
70 enum poptCallbackReason reason,
71 const struct poptOption *opt,
72 const char *arg, const void *data)
75 if (reason == POPT_CALLBACK_REASON_PRE) {
76 set_logfile(con, get_dyn_LOGFILEBASE());
77 talloc_set_log_fn(popt_s3_talloc_log_fn);
78 talloc_set_abort_fn(smb_panic);
82 if (reason == POPT_CALLBACK_REASON_POST) {
84 if (PrintSambaVersionString) {
85 printf( "Version %s\n", samba_version_string());
89 if (is_default_dyn_CONFIGFILE()) {
90 if(getenv("SMB_CONF_PATH")) {
91 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
95 /* Further 'every Samba program must do this' hooks here. */
101 if (!lp_set_option(arg)) {
102 fprintf(stderr, "Error setting option '%s'\n", arg);
109 lp_set_cmdline("log level", arg);
114 PrintSambaVersionString = True;
119 lp_do_parameter(-1, "socket options", arg);
125 set_dyn_CONFIGFILE(arg);
131 set_global_myname(arg);
137 set_logfile(con, arg);
138 override_logfile = True;
139 set_dyn_LOGFILEBASE(arg);
145 set_global_scope(arg);
151 set_global_myworkgroup(arg);
157 struct poptOption popt_common_connection[] = {
158 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
159 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
161 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
162 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
163 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
168 struct poptOption popt_common_samba[] = {
169 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
170 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
171 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
172 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Base name for log files", "LOGFILEBASE" },
173 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
174 { "option", 0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
178 struct poptOption popt_common_configfile[] = {
179 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
180 { "configfile", 0, POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
184 struct poptOption popt_common_version[] = {
185 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_POST, (void *)popt_common_callback },
186 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
190 struct poptOption popt_common_debuglevel[] = {
191 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
192 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
196 struct poptOption popt_common_option[] = {
197 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_POST, (void *)popt_common_callback },
198 { "option", 0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
202 /* Handle command line options:
235 static void popt_dynconfig_callback(poptContext con,
236 enum poptCallbackReason reason,
237 const struct poptOption *opt,
238 const char *arg, const void *data)
244 set_dyn_SBINDIR(arg);
256 set_dyn_SWATDIR(arg);
260 case DYN_LMHOSTSFILE:
262 set_dyn_LMHOSTSFILE(arg);
274 set_dyn_MODULESDIR(arg);
280 set_dyn_SHLIBEXT(arg);
286 set_dyn_LOCKDIR(arg);
292 set_dyn_STATEDIR(arg);
298 set_dyn_CACHEDIR(arg);
308 case DYN_SMB_PASSWD_FILE:
310 set_dyn_SMB_PASSWD_FILE(arg);
314 case DYN_PRIVATE_DIR:
316 set_dyn_PRIVATE_DIR(arg);
323 const struct poptOption popt_common_dynconfig[] = {
325 { NULL, '\0', POPT_ARG_CALLBACK, (void *)popt_dynconfig_callback },
327 { "sbindir", '\0' , POPT_ARG_STRING, NULL, DYN_SBINDIR,
328 "Path to sbin directory", "SBINDIR" },
329 { "bindir", '\0' , POPT_ARG_STRING, NULL, DYN_BINDIR,
330 "Path to bin directory", "BINDIR" },
331 { "swatdir", '\0' , POPT_ARG_STRING, NULL, DYN_SWATDIR,
332 "Path to SWAT installation directory", "SWATDIR" },
333 { "lmhostsfile", '\0' , POPT_ARG_STRING, NULL, DYN_LMHOSTSFILE,
334 "Path to lmhosts file", "LMHOSTSFILE" },
335 { "libdir", '\0' , POPT_ARG_STRING, NULL, DYN_LIBDIR,
336 "Path to shared library directory", "LIBDIR" },
337 { "modulesdir", '\0' , POPT_ARG_STRING, NULL, DYN_MODULESDIR,
338 "Path to shared modules directory", "MODULESDIR" },
339 { "shlibext", '\0' , POPT_ARG_STRING, NULL, DYN_SHLIBEXT,
340 "Shared library extension", "SHLIBEXT" },
341 { "lockdir", '\0' , POPT_ARG_STRING, NULL, DYN_LOCKDIR,
342 "Path to lock file directory", "LOCKDIR" },
343 { "statedir", '\0' , POPT_ARG_STRING, NULL, DYN_STATEDIR,
344 "Path to persistent state file directory", "STATEDIR" },
345 { "cachedir", '\0' , POPT_ARG_STRING, NULL, DYN_CACHEDIR,
346 "Path to temporary cache file directory", "CACHEDIR" },
347 { "piddir", '\0' , POPT_ARG_STRING, NULL, DYN_PIDDIR,
348 "Path to PID file directory", "PIDDIR" },
349 { "smb-passwd-file", '\0' , POPT_ARG_STRING, NULL, DYN_SMB_PASSWD_FILE,
350 "Path to smbpasswd file", "SMB_PASSWD_FILE" },
351 { "private-dir", '\0' , POPT_ARG_STRING, NULL, DYN_PRIVATE_DIR,
352 "Path to private data directory", "PRIVATE_DIR" },
357 /****************************************************************************
358 * get a password from a a file or file descriptor
360 * ****************************************************************************/
362 static void get_password_file(struct user_auth_info *auth_info)
366 bool close_it = False;
370 if ((p = getenv("PASSWD_FD")) != NULL) {
371 if (asprintf(&spec, "descriptor %s", p) < 0) {
374 sscanf(p, "%d", &fd);
376 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
377 fd = sys_open(p, O_RDONLY, 0);
378 spec = SMB_STRDUP(p);
380 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
381 spec, strerror(errno));
388 fprintf(stderr, "fd = %d, < 0\n", fd);
392 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
393 p && p - pass < sizeof(pass);) {
394 switch (read(fd, p, 1)) {
396 if (*p != '\n' && *p != '\0') {
397 *++p = '\0'; /* advance p, and null-terminate pass */
402 *p = '\0'; /* null-terminate it, just in case... */
403 p = NULL; /* then force the loop condition to become false */
406 fprintf(stderr, "Error reading password from file %s: %s\n",
407 spec, "empty password\n");
413 fprintf(stderr, "Error reading password from file %s: %s\n",
414 spec, strerror(errno));
421 set_cmdline_auth_info_password(auth_info, pass);
427 static void get_credentials_file(struct user_auth_info *auth_info,
433 char *ptr, *val, *param;
435 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
437 /* fail if we can't open the credentials file */
438 d_printf("ERROR: Unable to open credentials file!\n");
442 while (!x_feof(auth))
444 /* get a line from the file */
445 if (!x_fgets(buf, sizeof(buf), auth))
449 if ((len) && (buf[len-1]=='\n'))
457 /* break up the line into parameter & value.
458 * will need to eat a little whitespace possibly */
460 if (!(ptr = strchr_m (buf, '=')))
466 /* eat leading white space */
467 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
470 if (strwicmp("password", param) == 0) {
471 set_cmdline_auth_info_password(auth_info, val);
472 } else if (strwicmp("username", param) == 0) {
473 set_cmdline_auth_info_username(auth_info, val);
474 } else if (strwicmp("domain", param) == 0) {
475 set_global_myworkgroup(val);
477 memset(buf, 0, sizeof(buf));
482 /* Handle command line options:
484 * -A,--authentication-file
494 static void popt_common_credentials_callback(poptContext con,
495 enum poptCallbackReason reason,
496 const struct poptOption *opt,
497 const char *arg, const void *data)
499 struct user_auth_info *auth_info = talloc_get_type_abort(
500 *((const char **)data), struct user_auth_info);
503 if (reason == POPT_CALLBACK_REASON_PRE) {
504 set_cmdline_auth_info_username(auth_info, "GUEST");
506 if (getenv("LOGNAME")) {
507 set_cmdline_auth_info_username(auth_info,
511 if (getenv("USER")) {
512 char *puser = SMB_STRDUP(getenv("USER"));
516 set_cmdline_auth_info_username(auth_info, puser);
518 if ((p = strchr_m(puser,'%'))) {
522 set_cmdline_auth_info_password(auth_info, p+1);
523 memset(strchr_m(getenv("USER"),'%')+1,'X',len);
528 if (getenv("PASSWD")) {
529 set_cmdline_auth_info_password(auth_info,
533 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
534 get_password_file(auth_info);
544 char *puser = SMB_STRDUP(arg);
546 if ((lp=strchr_m(puser,'%'))) {
549 set_cmdline_auth_info_username(auth_info,
551 set_cmdline_auth_info_password(auth_info,
554 memset(strchr_m(arg,'%')+1,'X',len);
556 set_cmdline_auth_info_username(auth_info,
564 get_credentials_file(auth_info, arg);
569 d_printf("No kerberos support compiled in\n");
572 set_cmdline_auth_info_use_krb5_ticket(auth_info);
577 if (!set_cmdline_auth_info_signing_state(auth_info, arg)) {
578 fprintf(stderr, "Unknown signing option %s\n", arg );
583 set_cmdline_auth_info_use_machine_account(auth_info);
586 set_cmdline_auth_info_password(auth_info, "");
589 set_cmdline_auth_info_smb_encrypt(auth_info);
592 set_cmdline_auth_info_use_ccache(auth_info, true);
597 static struct user_auth_info *global_auth_info;
599 void popt_common_set_auth_info(struct user_auth_info *auth_info)
601 global_auth_info = auth_info;
604 struct poptOption popt_common_credentials[] = {
605 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE,
606 (void *)popt_common_credentials_callback, 0,
607 (const char *)&global_auth_info },
608 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
609 { "no-pass", 'N', POPT_ARG_NONE, NULL, 'N', "Don't ask for a password" },
610 { "kerberos", 'k', POPT_ARG_NONE, NULL, 'k', "Use kerberos (active directory) authentication" },
611 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
612 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
613 {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
614 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },
615 {"use-ccache", 'C', POPT_ARG_NONE, NULL, 'C',
616 "Use the winbind ccache for authentication" },