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(&logfile, "%s/log.%s", arg, pname) < 0) {
56 lp_set_logfile(logfile);
60 static bool PrintSambaVersionString;
62 static void popt_common_callback(poptContext con,
63 enum poptCallbackReason reason,
64 const struct poptOption *opt,
65 const char *arg, const void *data)
68 if (reason == POPT_CALLBACK_REASON_PRE) {
69 set_logfile(con, get_dyn_LOGFILEBASE());
73 if (reason == POPT_CALLBACK_REASON_POST) {
74 if (!PrintSambaVersionString) return;
76 printf( "Version %s\n", SAMBA_VERSION_STRING);
84 debug_parse_levels(arg);
85 AllowDebugChange = False;
90 PrintSambaVersionString = True;
95 lp_do_parameter(-1, "socket options", arg);
101 set_dyn_CONFIGFILE(arg);
107 set_global_myname(arg);
113 set_logfile(con, arg);
114 override_logfile = True;
115 set_dyn_LOGFILEBASE(arg);
121 set_global_scope(arg);
127 set_global_myworkgroup(arg);
133 struct poptOption popt_common_connection[] = {
134 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
135 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
137 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
138 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
139 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
144 struct poptOption popt_common_samba[] = {
145 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
146 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
147 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
148 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Base name for log files", "LOGFILEBASE" },
149 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
153 struct poptOption popt_common_configfile[] = {
154 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
155 { "configfile", 0, POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
159 struct poptOption popt_common_version[] = {
160 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
161 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
166 /* Handle command line options:
193 static void popt_dynconfig_callback(poptContext con,
194 enum poptCallbackReason reason,
195 const struct poptOption *opt,
196 const char *arg, const void *data)
202 set_dyn_SBINDIR(arg);
214 set_dyn_SWATDIR(arg);
218 case DYN_LMHOSTSFILE:
220 set_dyn_LMHOSTSFILE(arg);
232 set_dyn_SHLIBEXT(arg);
238 set_dyn_LOCKDIR(arg);
248 case DYN_SMB_PASSWD_FILE:
250 set_dyn_SMB_PASSWD_FILE(arg);
254 case DYN_PRIVATE_DIR:
256 set_dyn_PRIVATE_DIR(arg);
263 const struct poptOption popt_common_dynconfig[] = {
265 { NULL, '\0', POPT_ARG_CALLBACK, (void *)popt_dynconfig_callback },
267 { "sbindir", '\0' , POPT_ARG_STRING, NULL, DYN_SBINDIR,
268 "Path to sbin directory", "SBINDIR" },
269 { "bindir", '\0' , POPT_ARG_STRING, NULL, DYN_BINDIR,
270 "Path to bin directory", "BINDIR" },
271 { "swatdir", '\0' , POPT_ARG_STRING, NULL, DYN_SWATDIR,
272 "Path to SWAT installation directory", "SWATDIR" },
273 { "lmhostsfile", '\0' , POPT_ARG_STRING, NULL, DYN_LMHOSTSFILE,
274 "Path to lmhosts file", "LMHOSTSFILE" },
275 { "libdir", '\0' , POPT_ARG_STRING, NULL, DYN_LIBDIR,
276 "Path to shared library directory", "LIBDIR" },
277 { "shlibext", '\0' , POPT_ARG_STRING, NULL, DYN_SHLIBEXT,
278 "Shared library extension", "SHLIBEXT" },
279 { "lockdir", '\0' , POPT_ARG_STRING, NULL, DYN_LOCKDIR,
280 "Path to lock file directory", "LOCKDIR" },
281 { "piddir", '\0' , POPT_ARG_STRING, NULL, DYN_PIDDIR,
282 "Path to PID file directory", "PIDDIR" },
283 { "smb-passwd-file", '\0' , POPT_ARG_STRING, NULL, DYN_SMB_PASSWD_FILE,
284 "Path to smbpasswd file", "SMB_PASSWD_FILE" },
285 { "private-dir", '\0' , POPT_ARG_STRING, NULL, DYN_PRIVATE_DIR,
286 "Path to private data directory", "PRIVATE_DIR" },
291 /****************************************************************************
292 * get a password from a a file or file descriptor
294 * ****************************************************************************/
296 static void get_password_file(void)
300 bool close_it = False;
304 if ((p = getenv("PASSWD_FD")) != NULL) {
305 if (asprintf(&spec, "descriptor %s", p) < 0) {
308 sscanf(p, "%d", &fd);
310 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
311 fd = sys_open(p, O_RDONLY, 0);
312 spec = SMB_STRDUP(p);
314 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
315 spec, strerror(errno));
321 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
322 p && p - pass < sizeof(pass);) {
323 switch (read(fd, p, 1)) {
325 if (*p != '\n' && *p != '\0') {
326 *++p = '\0'; /* advance p, and null-terminate pass */
331 *p = '\0'; /* null-terminate it, just in case... */
332 p = NULL; /* then force the loop condition to become false */
335 fprintf(stderr, "Error reading password from file %s: %s\n",
336 spec, "empty password\n");
342 fprintf(stderr, "Error reading password from file %s: %s\n",
343 spec, strerror(errno));
350 set_cmdline_auth_info_password(pass);
356 static void get_credentials_file(const char *file)
361 char *ptr, *val, *param;
363 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
365 /* fail if we can't open the credentials file */
366 d_printf("ERROR: Unable to open credentials file!\n");
370 while (!x_feof(auth))
372 /* get a line from the file */
373 if (!x_fgets(buf, sizeof(buf), auth))
377 if ((len) && (buf[len-1]=='\n'))
385 /* break up the line into parameter & value.
386 * will need to eat a little whitespace possibly */
388 if (!(ptr = strchr_m (buf, '=')))
394 /* eat leading white space */
395 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
398 if (strwicmp("password", param) == 0) {
399 set_cmdline_auth_info_password(val);
400 } else if (strwicmp("username", param) == 0) {
401 set_cmdline_auth_info_username(val);
402 } else if (strwicmp("domain", param) == 0) {
403 set_global_myworkgroup(val);
405 memset(buf, 0, sizeof(buf));
410 /* Handle command line options:
412 * -A,--authentication-file
421 static void popt_common_credentials_callback(poptContext con,
422 enum poptCallbackReason reason,
423 const struct poptOption *opt,
424 const char *arg, const void *data)
428 if (reason == POPT_CALLBACK_REASON_PRE) {
429 set_cmdline_auth_info_username("GUEST");
431 if (getenv("LOGNAME")) {
432 set_cmdline_auth_info_username(getenv("LOGNAME"));
435 if (getenv("USER")) {
436 char *puser = SMB_STRDUP(getenv("USER"));
440 set_cmdline_auth_info_username(puser);
442 if ((p = strchr_m(puser,'%'))) {
446 set_cmdline_auth_info_password(p+1);
447 memset(strchr_m(getenv("USER"),'%')+1,'X',len);
452 if (getenv("PASSWD")) {
453 set_cmdline_auth_info_password(getenv("PASSWD"));
456 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
467 char *puser = SMB_STRDUP(arg);
469 if ((lp=strchr_m(puser,'%'))) {
472 set_cmdline_auth_info_username(puser);
473 set_cmdline_auth_info_password(lp+1);
475 memset(strchr_m(arg,'%')+1,'X',len);
477 set_cmdline_auth_info_username(puser);
484 get_credentials_file(arg);
489 d_printf("No kerberos support compiled in\n");
492 set_cmdline_auth_info_use_krb5_ticket();
497 if (!set_cmdline_auth_info_signing_state(arg)) {
498 fprintf(stderr, "Unknown signing option %s\n", arg );
504 char *opt_password = NULL;
507 /* it is very useful to be able to make ads queries as the
508 machine account for testing purposes and for domain leave */
510 if (!secrets_init()) {
511 d_printf("ERROR: Unable to open secrets database\n");
515 opt_password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
518 d_printf("ERROR: Unable to fetch machine password\n");
521 if (asprintf(&pwd, "%s$", global_myname()) < 0) {
524 set_cmdline_auth_info_username(pwd);
525 set_cmdline_auth_info_password(opt_password);
527 SAFE_FREE(opt_password);
529 /* machine accounts only work with kerberos */
530 set_cmdline_auth_info_use_krb5_ticket();
534 set_cmdline_auth_info_password("");
537 set_cmdline_auth_info_smb_encrypt();
543 struct poptOption popt_common_credentials[] = {
544 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, (void *)popt_common_credentials_callback },
545 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
546 { "no-pass", 'N', POPT_ARG_NONE, NULL, 'N', "Don't ask for a password" },
547 { "kerberos", 'k', POPT_ARG_NONE, NULL, 'k', "Use kerberos (active directory) authentication" },
548 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
549 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
550 {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
551 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },