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 AllowDebugChange;
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. */
102 if (!lp_set_option(arg)) {
103 fprintf(stderr, "Error setting option '%s'\n", arg);
110 debug_parse_levels(arg);
111 AllowDebugChange = False;
116 PrintSambaVersionString = True;
121 lp_do_parameter(-1, "socket options", arg);
127 set_dyn_CONFIGFILE(arg);
133 set_global_myname(arg);
139 set_logfile(con, arg);
140 override_logfile = True;
141 set_dyn_LOGFILEBASE(arg);
147 set_global_scope(arg);
153 set_global_myworkgroup(arg);
159 struct poptOption popt_common_connection[] = {
160 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
161 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
163 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
164 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
165 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
170 struct poptOption popt_common_samba[] = {
171 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
172 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
173 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
174 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Base name for log files", "LOGFILEBASE" },
175 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
176 { "option", 0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
180 struct poptOption popt_common_configfile[] = {
181 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
182 { "configfile", 0, POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
186 struct poptOption popt_common_version[] = {
187 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_POST, (void *)popt_common_callback },
188 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
192 struct poptOption popt_common_debuglevel[] = {
193 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
194 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
199 /* Handle command line options:
232 static void popt_dynconfig_callback(poptContext con,
233 enum poptCallbackReason reason,
234 const struct poptOption *opt,
235 const char *arg, const void *data)
241 set_dyn_SBINDIR(arg);
253 set_dyn_SWATDIR(arg);
257 case DYN_LMHOSTSFILE:
259 set_dyn_LMHOSTSFILE(arg);
271 set_dyn_MODULESDIR(arg);
277 set_dyn_SHLIBEXT(arg);
283 set_dyn_LOCKDIR(arg);
289 set_dyn_STATEDIR(arg);
295 set_dyn_CACHEDIR(arg);
305 case DYN_SMB_PASSWD_FILE:
307 set_dyn_SMB_PASSWD_FILE(arg);
311 case DYN_PRIVATE_DIR:
313 set_dyn_PRIVATE_DIR(arg);
320 const struct poptOption popt_common_dynconfig[] = {
322 { NULL, '\0', POPT_ARG_CALLBACK, (void *)popt_dynconfig_callback },
324 { "sbindir", '\0' , POPT_ARG_STRING, NULL, DYN_SBINDIR,
325 "Path to sbin directory", "SBINDIR" },
326 { "bindir", '\0' , POPT_ARG_STRING, NULL, DYN_BINDIR,
327 "Path to bin directory", "BINDIR" },
328 { "swatdir", '\0' , POPT_ARG_STRING, NULL, DYN_SWATDIR,
329 "Path to SWAT installation directory", "SWATDIR" },
330 { "lmhostsfile", '\0' , POPT_ARG_STRING, NULL, DYN_LMHOSTSFILE,
331 "Path to lmhosts file", "LMHOSTSFILE" },
332 { "libdir", '\0' , POPT_ARG_STRING, NULL, DYN_LIBDIR,
333 "Path to shared library directory", "LIBDIR" },
334 { "modulesdir", '\0' , POPT_ARG_STRING, NULL, DYN_MODULESDIR,
335 "Path to shared modules directory", "MODULESDIR" },
336 { "shlibext", '\0' , POPT_ARG_STRING, NULL, DYN_SHLIBEXT,
337 "Shared library extension", "SHLIBEXT" },
338 { "lockdir", '\0' , POPT_ARG_STRING, NULL, DYN_LOCKDIR,
339 "Path to lock file directory", "LOCKDIR" },
340 { "statedir", '\0' , POPT_ARG_STRING, NULL, DYN_STATEDIR,
341 "Path to persistent state file directory", "STATEDIR" },
342 { "cachedir", '\0' , POPT_ARG_STRING, NULL, DYN_CACHEDIR,
343 "Path to temporary cache file directory", "CACHEDIR" },
344 { "piddir", '\0' , POPT_ARG_STRING, NULL, DYN_PIDDIR,
345 "Path to PID file directory", "PIDDIR" },
346 { "smb-passwd-file", '\0' , POPT_ARG_STRING, NULL, DYN_SMB_PASSWD_FILE,
347 "Path to smbpasswd file", "SMB_PASSWD_FILE" },
348 { "private-dir", '\0' , POPT_ARG_STRING, NULL, DYN_PRIVATE_DIR,
349 "Path to private data directory", "PRIVATE_DIR" },
354 /****************************************************************************
355 * get a password from a a file or file descriptor
357 * ****************************************************************************/
359 static void get_password_file(struct user_auth_info *auth_info)
363 bool close_it = False;
367 if ((p = getenv("PASSWD_FD")) != NULL) {
368 if (asprintf(&spec, "descriptor %s", p) < 0) {
371 sscanf(p, "%d", &fd);
373 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
374 fd = sys_open(p, O_RDONLY, 0);
375 spec = SMB_STRDUP(p);
377 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
378 spec, strerror(errno));
385 fprintf(stderr, "fd = %d, < 0\n", fd);
389 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
390 p && p - pass < sizeof(pass);) {
391 switch (read(fd, p, 1)) {
393 if (*p != '\n' && *p != '\0') {
394 *++p = '\0'; /* advance p, and null-terminate pass */
399 *p = '\0'; /* null-terminate it, just in case... */
400 p = NULL; /* then force the loop condition to become false */
403 fprintf(stderr, "Error reading password from file %s: %s\n",
404 spec, "empty password\n");
410 fprintf(stderr, "Error reading password from file %s: %s\n",
411 spec, strerror(errno));
418 set_cmdline_auth_info_password(auth_info, pass);
424 static void get_credentials_file(struct user_auth_info *auth_info,
430 char *ptr, *val, *param;
432 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
434 /* fail if we can't open the credentials file */
435 d_printf("ERROR: Unable to open credentials file!\n");
439 while (!x_feof(auth))
441 /* get a line from the file */
442 if (!x_fgets(buf, sizeof(buf), auth))
446 if ((len) && (buf[len-1]=='\n'))
454 /* break up the line into parameter & value.
455 * will need to eat a little whitespace possibly */
457 if (!(ptr = strchr_m (buf, '=')))
463 /* eat leading white space */
464 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
467 if (strwicmp("password", param) == 0) {
468 set_cmdline_auth_info_password(auth_info, val);
469 } else if (strwicmp("username", param) == 0) {
470 set_cmdline_auth_info_username(auth_info, val);
471 } else if (strwicmp("domain", param) == 0) {
472 set_global_myworkgroup(val);
474 memset(buf, 0, sizeof(buf));
479 /* Handle command line options:
481 * -A,--authentication-file
491 static void popt_common_credentials_callback(poptContext con,
492 enum poptCallbackReason reason,
493 const struct poptOption *opt,
494 const char *arg, const void *data)
496 struct user_auth_info *auth_info = talloc_get_type_abort(
497 *((const char **)data), struct user_auth_info);
500 if (reason == POPT_CALLBACK_REASON_PRE) {
501 set_cmdline_auth_info_username(auth_info, "GUEST");
503 if (getenv("LOGNAME")) {
504 set_cmdline_auth_info_username(auth_info,
508 if (getenv("USER")) {
509 char *puser = SMB_STRDUP(getenv("USER"));
513 set_cmdline_auth_info_username(auth_info, puser);
515 if ((p = strchr_m(puser,'%'))) {
519 set_cmdline_auth_info_password(auth_info, p+1);
520 memset(strchr_m(getenv("USER"),'%')+1,'X',len);
525 if (getenv("PASSWD")) {
526 set_cmdline_auth_info_password(auth_info,
530 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
531 get_password_file(auth_info);
541 char *puser = SMB_STRDUP(arg);
543 if ((lp=strchr_m(puser,'%'))) {
546 set_cmdline_auth_info_username(auth_info,
548 set_cmdline_auth_info_password(auth_info,
551 memset(strchr_m(arg,'%')+1,'X',len);
553 set_cmdline_auth_info_username(auth_info,
561 get_credentials_file(auth_info, arg);
566 d_printf("No kerberos support compiled in\n");
569 set_cmdline_auth_info_use_krb5_ticket(auth_info);
574 if (!set_cmdline_auth_info_signing_state(auth_info, arg)) {
575 fprintf(stderr, "Unknown signing option %s\n", arg );
580 set_cmdline_auth_info_use_machine_account(auth_info);
583 set_cmdline_auth_info_password(auth_info, "");
586 set_cmdline_auth_info_smb_encrypt(auth_info);
589 set_cmdline_auth_info_use_ccache(auth_info, true);
594 static struct user_auth_info *global_auth_info;
596 void popt_common_set_auth_info(struct user_auth_info *auth_info)
598 global_auth_info = auth_info;
601 struct poptOption popt_common_credentials[] = {
602 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE,
603 (void *)popt_common_credentials_callback, 0,
604 (const char *)&global_auth_info },
605 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
606 { "no-pass", 'N', POPT_ARG_NONE, NULL, 'N', "Don't ask for a password" },
607 { "kerberos", 'k', POPT_ARG_NONE, NULL, 'k', "Use kerberos (active directory) authentication" },
608 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
609 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
610 {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
611 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },
612 {"use-ccache", 'C', POPT_ARG_NONE, NULL, 'C',
613 "Use the winbind ccache for authentication" },