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/>.
25 /* Handle command line options:
36 extern bool AllowDebugChange
;
37 extern bool override_logfile
;
39 static void set_logfile(poptContext con
, const char * arg
)
45 /* Find out basename of current program */
46 pname
= strrchr_m(poptGetInvocationName(con
),'/');
49 pname
= poptGetInvocationName(con
);
53 if (asprintf(&lfile
, "%s/log.%s", arg
, pname
) < 0) {
56 lp_set_logfile(lfile
);
60 static bool PrintSambaVersionString
;
62 static void popt_s3_talloc_log_fn(const char *message
)
64 DEBUG(0,("%s", message
));
67 static void popt_common_callback(poptContext con
,
68 enum poptCallbackReason reason
,
69 const struct poptOption
*opt
,
70 const char *arg
, const void *data
)
73 if (reason
== POPT_CALLBACK_REASON_PRE
) {
74 set_logfile(con
, get_dyn_LOGFILEBASE());
75 talloc_set_log_fn(popt_s3_talloc_log_fn
);
76 talloc_set_abort_fn(smb_panic
);
80 if (reason
== POPT_CALLBACK_REASON_POST
) {
82 if (PrintSambaVersionString
) {
83 printf( "Version %s\n", samba_version_string());
87 if (is_default_dyn_CONFIGFILE()) {
88 if(getenv("SMB_CONF_PATH")) {
89 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
93 /* Further 'every Samba program must do this' hooks here. */
100 debug_parse_levels(arg
);
101 AllowDebugChange
= False
;
106 PrintSambaVersionString
= True
;
111 lp_do_parameter(-1, "socket options", arg
);
117 set_dyn_CONFIGFILE(arg
);
123 set_global_myname(arg
);
129 set_logfile(con
, arg
);
130 override_logfile
= True
;
131 set_dyn_LOGFILEBASE(arg
);
137 set_global_scope(arg
);
143 set_global_myworkgroup(arg
);
149 struct poptOption popt_common_connection
[] = {
150 { NULL
, 0, POPT_ARG_CALLBACK
, (void *)popt_common_callback
},
151 { "socket-options", 'O', POPT_ARG_STRING
, NULL
, 'O', "socket options to use",
153 { "netbiosname", 'n', POPT_ARG_STRING
, NULL
, 'n', "Primary netbios name", "NETBIOSNAME" },
154 { "workgroup", 'W', POPT_ARG_STRING
, NULL
, 'W', "Set the workgroup name", "WORKGROUP" },
155 { "scope", 'i', POPT_ARG_STRING
, NULL
, 'i', "Use this Netbios scope", "SCOPE" },
160 struct poptOption popt_common_samba
[] = {
161 { NULL
, 0, POPT_ARG_CALLBACK
|POPT_CBFLAG_PRE
|POPT_CBFLAG_POST
, (void *)popt_common_callback
},
162 { "debuglevel", 'd', POPT_ARG_STRING
, NULL
, 'd', "Set debug level", "DEBUGLEVEL" },
163 { "configfile", 's', POPT_ARG_STRING
, NULL
, 's', "Use alternate configuration file", "CONFIGFILE" },
164 { "log-basename", 'l', POPT_ARG_STRING
, NULL
, 'l', "Base name for log files", "LOGFILEBASE" },
165 { "version", 'V', POPT_ARG_NONE
, NULL
, 'V', "Print version" },
169 struct poptOption popt_common_configfile
[] = {
170 { NULL
, 0, POPT_ARG_CALLBACK
|POPT_CBFLAG_PRE
|POPT_CBFLAG_POST
, (void *)popt_common_callback
},
171 { "configfile", 0, POPT_ARG_STRING
, NULL
, 's', "Use alternate configuration file", "CONFIGFILE" },
175 struct poptOption popt_common_version
[] = {
176 { NULL
, 0, POPT_ARG_CALLBACK
|POPT_CBFLAG_POST
, (void *)popt_common_callback
},
177 { "version", 'V', POPT_ARG_NONE
, NULL
, 'V', "Print version" },
181 struct poptOption popt_common_debuglevel
[] = {
182 { NULL
, 0, POPT_ARG_CALLBACK
, (void *)popt_common_callback
},
183 { "debuglevel", 'd', POPT_ARG_STRING
, NULL
, 'd', "Set debug level", "DEBUGLEVEL" },
188 /* Handle command line options:
221 static void popt_dynconfig_callback(poptContext con
,
222 enum poptCallbackReason reason
,
223 const struct poptOption
*opt
,
224 const char *arg
, const void *data
)
230 set_dyn_SBINDIR(arg
);
242 set_dyn_SWATDIR(arg
);
246 case DYN_LMHOSTSFILE
:
248 set_dyn_LMHOSTSFILE(arg
);
260 set_dyn_MODULESDIR(arg
);
266 set_dyn_SHLIBEXT(arg
);
272 set_dyn_LOCKDIR(arg
);
278 set_dyn_STATEDIR(arg
);
284 set_dyn_CACHEDIR(arg
);
294 case DYN_SMB_PASSWD_FILE
:
296 set_dyn_SMB_PASSWD_FILE(arg
);
300 case DYN_PRIVATE_DIR
:
302 set_dyn_PRIVATE_DIR(arg
);
309 const struct poptOption popt_common_dynconfig
[] = {
311 { NULL
, '\0', POPT_ARG_CALLBACK
, (void *)popt_dynconfig_callback
},
313 { "sbindir", '\0' , POPT_ARG_STRING
, NULL
, DYN_SBINDIR
,
314 "Path to sbin directory", "SBINDIR" },
315 { "bindir", '\0' , POPT_ARG_STRING
, NULL
, DYN_BINDIR
,
316 "Path to bin directory", "BINDIR" },
317 { "swatdir", '\0' , POPT_ARG_STRING
, NULL
, DYN_SWATDIR
,
318 "Path to SWAT installation directory", "SWATDIR" },
319 { "lmhostsfile", '\0' , POPT_ARG_STRING
, NULL
, DYN_LMHOSTSFILE
,
320 "Path to lmhosts file", "LMHOSTSFILE" },
321 { "libdir", '\0' , POPT_ARG_STRING
, NULL
, DYN_LIBDIR
,
322 "Path to shared library directory", "LIBDIR" },
323 { "modulesdir", '\0' , POPT_ARG_STRING
, NULL
, DYN_MODULESDIR
,
324 "Path to shared modules directory", "MODULESDIR" },
325 { "shlibext", '\0' , POPT_ARG_STRING
, NULL
, DYN_SHLIBEXT
,
326 "Shared library extension", "SHLIBEXT" },
327 { "lockdir", '\0' , POPT_ARG_STRING
, NULL
, DYN_LOCKDIR
,
328 "Path to lock file directory", "LOCKDIR" },
329 { "statedir", '\0' , POPT_ARG_STRING
, NULL
, DYN_STATEDIR
,
330 "Path to persistent state file directory", "STATEDIR" },
331 { "cachedir", '\0' , POPT_ARG_STRING
, NULL
, DYN_CACHEDIR
,
332 "Path to temporary cache file directory", "CACHEDIR" },
333 { "piddir", '\0' , POPT_ARG_STRING
, NULL
, DYN_PIDDIR
,
334 "Path to PID file directory", "PIDDIR" },
335 { "smb-passwd-file", '\0' , POPT_ARG_STRING
, NULL
, DYN_SMB_PASSWD_FILE
,
336 "Path to smbpasswd file", "SMB_PASSWD_FILE" },
337 { "private-dir", '\0' , POPT_ARG_STRING
, NULL
, DYN_PRIVATE_DIR
,
338 "Path to private data directory", "PRIVATE_DIR" },
343 /****************************************************************************
344 * get a password from a a file or file descriptor
346 * ****************************************************************************/
348 static void get_password_file(struct user_auth_info
*auth_info
)
352 bool close_it
= False
;
356 if ((p
= getenv("PASSWD_FD")) != NULL
) {
357 if (asprintf(&spec
, "descriptor %s", p
) < 0) {
360 sscanf(p
, "%d", &fd
);
362 } else if ((p
= getenv("PASSWD_FILE")) != NULL
) {
363 fd
= sys_open(p
, O_RDONLY
, 0);
364 spec
= SMB_STRDUP(p
);
366 fprintf(stderr
, "Error opening PASSWD_FILE %s: %s\n",
367 spec
, strerror(errno
));
374 fprintf(stderr
, "fd = %d, < 0\n", fd
);
378 for(p
= pass
, *p
= '\0'; /* ensure that pass is null-terminated */
379 p
&& p
- pass
< sizeof(pass
);) {
380 switch (read(fd
, p
, 1)) {
382 if (*p
!= '\n' && *p
!= '\0') {
383 *++p
= '\0'; /* advance p, and null-terminate pass */
388 *p
= '\0'; /* null-terminate it, just in case... */
389 p
= NULL
; /* then force the loop condition to become false */
392 fprintf(stderr
, "Error reading password from file %s: %s\n",
393 spec
, "empty password\n");
399 fprintf(stderr
, "Error reading password from file %s: %s\n",
400 spec
, strerror(errno
));
407 set_cmdline_auth_info_password(auth_info
, pass
);
413 static void get_credentials_file(struct user_auth_info
*auth_info
,
419 char *ptr
, *val
, *param
;
421 if ((auth
=x_fopen(file
, O_RDONLY
, 0)) == NULL
)
423 /* fail if we can't open the credentials file */
424 d_printf("ERROR: Unable to open credentials file!\n");
428 while (!x_feof(auth
))
430 /* get a line from the file */
431 if (!x_fgets(buf
, sizeof(buf
), auth
))
435 if ((len
) && (buf
[len
-1]=='\n'))
443 /* break up the line into parameter & value.
444 * will need to eat a little whitespace possibly */
446 if (!(ptr
= strchr_m (buf
, '=')))
452 /* eat leading white space */
453 while ((*val
!='\0') && ((*val
==' ') || (*val
=='\t')))
456 if (strwicmp("password", param
) == 0) {
457 set_cmdline_auth_info_password(auth_info
, val
);
458 } else if (strwicmp("username", param
) == 0) {
459 set_cmdline_auth_info_username(auth_info
, val
);
460 } else if (strwicmp("domain", param
) == 0) {
461 set_global_myworkgroup(val
);
463 memset(buf
, 0, sizeof(buf
));
468 /* Handle command line options:
470 * -A,--authentication-file
480 static void popt_common_credentials_callback(poptContext con
,
481 enum poptCallbackReason reason
,
482 const struct poptOption
*opt
,
483 const char *arg
, const void *data
)
485 struct user_auth_info
*auth_info
= talloc_get_type_abort(
486 *((const char **)data
), struct user_auth_info
);
489 if (reason
== POPT_CALLBACK_REASON_PRE
) {
490 set_cmdline_auth_info_username(auth_info
, "GUEST");
492 if (getenv("LOGNAME")) {
493 set_cmdline_auth_info_username(auth_info
,
497 if (getenv("USER")) {
498 char *puser
= SMB_STRDUP(getenv("USER"));
502 set_cmdline_auth_info_username(auth_info
, puser
);
504 if ((p
= strchr_m(puser
,'%'))) {
508 set_cmdline_auth_info_password(auth_info
, p
+1);
509 memset(strchr_m(getenv("USER"),'%')+1,'X',len
);
514 if (getenv("PASSWD")) {
515 set_cmdline_auth_info_password(auth_info
,
519 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
520 get_password_file(auth_info
);
530 char *puser
= SMB_STRDUP(arg
);
532 if ((lp
=strchr_m(puser
,'%'))) {
535 set_cmdline_auth_info_username(auth_info
,
537 set_cmdline_auth_info_password(auth_info
,
540 memset(strchr_m(arg
,'%')+1,'X',len
);
542 set_cmdline_auth_info_username(auth_info
,
550 get_credentials_file(auth_info
, arg
);
555 d_printf("No kerberos support compiled in\n");
558 set_cmdline_auth_info_use_krb5_ticket(auth_info
);
563 if (!set_cmdline_auth_info_signing_state(auth_info
, arg
)) {
564 fprintf(stderr
, "Unknown signing option %s\n", arg
);
569 set_cmdline_auth_info_use_machine_account(auth_info
);
572 set_cmdline_auth_info_password(auth_info
, "");
575 set_cmdline_auth_info_smb_encrypt(auth_info
);
578 set_cmdline_auth_info_use_ccache(auth_info
, true);
583 static struct user_auth_info
*global_auth_info
;
585 void popt_common_set_auth_info(struct user_auth_info
*auth_info
)
587 global_auth_info
= auth_info
;
590 struct poptOption popt_common_credentials
[] = {
591 { NULL
, 0, POPT_ARG_CALLBACK
|POPT_CBFLAG_PRE
,
592 (void *)popt_common_credentials_callback
, 0,
593 (const char *)&global_auth_info
},
594 { "user", 'U', POPT_ARG_STRING
, NULL
, 'U', "Set the network username", "USERNAME" },
595 { "no-pass", 'N', POPT_ARG_NONE
, NULL
, 'N', "Don't ask for a password" },
596 { "kerberos", 'k', POPT_ARG_NONE
, NULL
, 'k', "Use kerberos (active directory) authentication" },
597 { "authentication-file", 'A', POPT_ARG_STRING
, NULL
, 'A', "Get the credentials from a file", "FILE" },
598 { "signing", 'S', POPT_ARG_STRING
, NULL
, 'S', "Set the client signing state", "on|off|required" },
599 {"machine-pass", 'P', POPT_ARG_NONE
, NULL
, 'P', "Use stored machine account password" },
600 {"encrypt", 'e', POPT_ARG_NONE
, NULL
, 'e', "Encrypt SMB transport (UNIX extended servers only)" },
601 {"use-ccache", 'C', POPT_ARG_NONE
, NULL
, 'C',
602 "Use the winbind ccache for authentication" },