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" },
197 /* Handle command line options:
230 static void popt_dynconfig_callback(poptContext con
,
231 enum poptCallbackReason reason
,
232 const struct poptOption
*opt
,
233 const char *arg
, const void *data
)
239 set_dyn_SBINDIR(arg
);
251 set_dyn_SWATDIR(arg
);
255 case DYN_LMHOSTSFILE
:
257 set_dyn_LMHOSTSFILE(arg
);
269 set_dyn_MODULESDIR(arg
);
275 set_dyn_SHLIBEXT(arg
);
281 set_dyn_LOCKDIR(arg
);
287 set_dyn_STATEDIR(arg
);
293 set_dyn_CACHEDIR(arg
);
303 case DYN_SMB_PASSWD_FILE
:
305 set_dyn_SMB_PASSWD_FILE(arg
);
309 case DYN_PRIVATE_DIR
:
311 set_dyn_PRIVATE_DIR(arg
);
318 const struct poptOption popt_common_dynconfig
[] = {
320 { NULL
, '\0', POPT_ARG_CALLBACK
, (void *)popt_dynconfig_callback
},
322 { "sbindir", '\0' , POPT_ARG_STRING
, NULL
, DYN_SBINDIR
,
323 "Path to sbin directory", "SBINDIR" },
324 { "bindir", '\0' , POPT_ARG_STRING
, NULL
, DYN_BINDIR
,
325 "Path to bin directory", "BINDIR" },
326 { "swatdir", '\0' , POPT_ARG_STRING
, NULL
, DYN_SWATDIR
,
327 "Path to SWAT installation directory", "SWATDIR" },
328 { "lmhostsfile", '\0' , POPT_ARG_STRING
, NULL
, DYN_LMHOSTSFILE
,
329 "Path to lmhosts file", "LMHOSTSFILE" },
330 { "libdir", '\0' , POPT_ARG_STRING
, NULL
, DYN_LIBDIR
,
331 "Path to shared library directory", "LIBDIR" },
332 { "modulesdir", '\0' , POPT_ARG_STRING
, NULL
, DYN_MODULESDIR
,
333 "Path to shared modules directory", "MODULESDIR" },
334 { "shlibext", '\0' , POPT_ARG_STRING
, NULL
, DYN_SHLIBEXT
,
335 "Shared library extension", "SHLIBEXT" },
336 { "lockdir", '\0' , POPT_ARG_STRING
, NULL
, DYN_LOCKDIR
,
337 "Path to lock file directory", "LOCKDIR" },
338 { "statedir", '\0' , POPT_ARG_STRING
, NULL
, DYN_STATEDIR
,
339 "Path to persistent state file directory", "STATEDIR" },
340 { "cachedir", '\0' , POPT_ARG_STRING
, NULL
, DYN_CACHEDIR
,
341 "Path to temporary cache file directory", "CACHEDIR" },
342 { "piddir", '\0' , POPT_ARG_STRING
, NULL
, DYN_PIDDIR
,
343 "Path to PID file directory", "PIDDIR" },
344 { "smb-passwd-file", '\0' , POPT_ARG_STRING
, NULL
, DYN_SMB_PASSWD_FILE
,
345 "Path to smbpasswd file", "SMB_PASSWD_FILE" },
346 { "private-dir", '\0' , POPT_ARG_STRING
, NULL
, DYN_PRIVATE_DIR
,
347 "Path to private data directory", "PRIVATE_DIR" },
352 /****************************************************************************
353 * get a password from a a file or file descriptor
355 * ****************************************************************************/
357 static void get_password_file(struct user_auth_info
*auth_info
)
361 bool close_it
= False
;
365 if ((p
= getenv("PASSWD_FD")) != NULL
) {
366 if (asprintf(&spec
, "descriptor %s", p
) < 0) {
369 sscanf(p
, "%d", &fd
);
371 } else if ((p
= getenv("PASSWD_FILE")) != NULL
) {
372 fd
= sys_open(p
, O_RDONLY
, 0);
373 spec
= SMB_STRDUP(p
);
375 fprintf(stderr
, "Error opening PASSWD_FILE %s: %s\n",
376 spec
, strerror(errno
));
383 fprintf(stderr
, "fd = %d, < 0\n", fd
);
387 for(p
= pass
, *p
= '\0'; /* ensure that pass is null-terminated */
388 p
&& p
- pass
< sizeof(pass
);) {
389 switch (read(fd
, p
, 1)) {
391 if (*p
!= '\n' && *p
!= '\0') {
392 *++p
= '\0'; /* advance p, and null-terminate pass */
397 *p
= '\0'; /* null-terminate it, just in case... */
398 p
= NULL
; /* then force the loop condition to become false */
401 fprintf(stderr
, "Error reading password from file %s: %s\n",
402 spec
, "empty password\n");
408 fprintf(stderr
, "Error reading password from file %s: %s\n",
409 spec
, strerror(errno
));
416 set_cmdline_auth_info_password(auth_info
, pass
);
422 static void get_credentials_file(struct user_auth_info
*auth_info
,
428 char *ptr
, *val
, *param
;
430 if ((auth
=x_fopen(file
, O_RDONLY
, 0)) == NULL
)
432 /* fail if we can't open the credentials file */
433 d_printf("ERROR: Unable to open credentials file!\n");
437 while (!x_feof(auth
))
439 /* get a line from the file */
440 if (!x_fgets(buf
, sizeof(buf
), auth
))
444 if ((len
) && (buf
[len
-1]=='\n'))
452 /* break up the line into parameter & value.
453 * will need to eat a little whitespace possibly */
455 if (!(ptr
= strchr_m (buf
, '=')))
461 /* eat leading white space */
462 while ((*val
!='\0') && ((*val
==' ') || (*val
=='\t')))
465 if (strwicmp("password", param
) == 0) {
466 set_cmdline_auth_info_password(auth_info
, val
);
467 } else if (strwicmp("username", param
) == 0) {
468 set_cmdline_auth_info_username(auth_info
, val
);
469 } else if (strwicmp("domain", param
) == 0) {
470 set_global_myworkgroup(val
);
472 memset(buf
, 0, sizeof(buf
));
477 /* Handle command line options:
479 * -A,--authentication-file
489 static void popt_common_credentials_callback(poptContext con
,
490 enum poptCallbackReason reason
,
491 const struct poptOption
*opt
,
492 const char *arg
, const void *data
)
494 struct user_auth_info
*auth_info
= talloc_get_type_abort(
495 *((const char **)data
), struct user_auth_info
);
498 if (reason
== POPT_CALLBACK_REASON_PRE
) {
499 set_cmdline_auth_info_username(auth_info
, "GUEST");
501 if (getenv("LOGNAME")) {
502 set_cmdline_auth_info_username(auth_info
,
506 if (getenv("USER")) {
507 char *puser
= SMB_STRDUP(getenv("USER"));
511 set_cmdline_auth_info_username(auth_info
, puser
);
513 if ((p
= strchr_m(puser
,'%'))) {
517 set_cmdline_auth_info_password(auth_info
, p
+1);
518 memset(strchr_m(getenv("USER"),'%')+1,'X',len
);
523 if (getenv("PASSWD")) {
524 set_cmdline_auth_info_password(auth_info
,
528 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
529 get_password_file(auth_info
);
539 char *puser
= SMB_STRDUP(arg
);
541 if ((lp
=strchr_m(puser
,'%'))) {
544 set_cmdline_auth_info_username(auth_info
,
546 set_cmdline_auth_info_password(auth_info
,
549 memset(strchr_m(arg
,'%')+1,'X',len
);
551 set_cmdline_auth_info_username(auth_info
,
559 get_credentials_file(auth_info
, arg
);
564 d_printf("No kerberos support compiled in\n");
567 set_cmdline_auth_info_use_krb5_ticket(auth_info
);
572 if (!set_cmdline_auth_info_signing_state(auth_info
, arg
)) {
573 fprintf(stderr
, "Unknown signing option %s\n", arg
);
578 set_cmdline_auth_info_use_machine_account(auth_info
);
581 set_cmdline_auth_info_password(auth_info
, "");
584 set_cmdline_auth_info_smb_encrypt(auth_info
);
587 set_cmdline_auth_info_use_ccache(auth_info
, true);
592 static struct user_auth_info
*global_auth_info
;
594 void popt_common_set_auth_info(struct user_auth_info
*auth_info
)
596 global_auth_info
= auth_info
;
599 struct poptOption popt_common_credentials
[] = {
600 { NULL
, 0, POPT_ARG_CALLBACK
|POPT_CBFLAG_PRE
,
601 (void *)popt_common_credentials_callback
, 0,
602 (const char *)&global_auth_info
},
603 { "user", 'U', POPT_ARG_STRING
, NULL
, 'U', "Set the network username", "USERNAME" },
604 { "no-pass", 'N', POPT_ARG_NONE
, NULL
, 'N', "Don't ask for a password" },
605 { "kerberos", 'k', POPT_ARG_NONE
, NULL
, 'k', "Use kerberos (active directory) authentication" },
606 { "authentication-file", 'A', POPT_ARG_STRING
, NULL
, 'A', "Get the credentials from a file", "FILE" },
607 { "signing", 'S', POPT_ARG_STRING
, NULL
, 'S', "Set the client signing state", "on|off|required" },
608 {"machine-pass", 'P', POPT_ARG_NONE
, NULL
, 'P', "Use stored machine account password" },
609 {"encrypt", 'e', POPT_ARG_NONE
, NULL
, 'e', "Encrypt SMB transport (UNIX extended servers only)" },
610 {"use-ccache", 'C', POPT_ARG_NONE
, NULL
, 'C',
611 "Use the winbind ccache for authentication" },