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_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
) {
75 if (PrintSambaVersionString
) {
76 printf( "Version %s\n", samba_version_string());
80 if (is_default_dyn_CONFIGFILE()) {
81 if(getenv("SMB_CONF_PATH")) {
82 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
86 /* Further 'every Samba program must do this' hooks here. */
93 debug_parse_levels(arg
);
94 AllowDebugChange
= False
;
99 PrintSambaVersionString
= True
;
104 lp_do_parameter(-1, "socket options", arg
);
110 set_dyn_CONFIGFILE(arg
);
116 set_global_myname(arg
);
122 set_logfile(con
, arg
);
123 override_logfile
= True
;
124 set_dyn_LOGFILEBASE(arg
);
130 set_global_scope(arg
);
136 set_global_myworkgroup(arg
);
142 struct poptOption popt_common_connection
[] = {
143 { NULL
, 0, POPT_ARG_CALLBACK
, (void *)popt_common_callback
},
144 { "socket-options", 'O', POPT_ARG_STRING
, NULL
, 'O', "socket options to use",
146 { "netbiosname", 'n', POPT_ARG_STRING
, NULL
, 'n', "Primary netbios name", "NETBIOSNAME" },
147 { "workgroup", 'W', POPT_ARG_STRING
, NULL
, 'W', "Set the workgroup name", "WORKGROUP" },
148 { "scope", 'i', POPT_ARG_STRING
, NULL
, 'i', "Use this Netbios scope", "SCOPE" },
153 struct poptOption popt_common_samba
[] = {
154 { NULL
, 0, POPT_ARG_CALLBACK
|POPT_CBFLAG_PRE
|POPT_CBFLAG_POST
, (void *)popt_common_callback
},
155 { "debuglevel", 'd', POPT_ARG_STRING
, NULL
, 'd', "Set debug level", "DEBUGLEVEL" },
156 { "configfile", 's', POPT_ARG_STRING
, NULL
, 's', "Use alternate configuration file", "CONFIGFILE" },
157 { "log-basename", 'l', POPT_ARG_STRING
, NULL
, 'l', "Base name for log files", "LOGFILEBASE" },
158 { "version", 'V', POPT_ARG_NONE
, NULL
, 'V', "Print version" },
162 struct poptOption popt_common_configfile
[] = {
163 { NULL
, 0, POPT_ARG_CALLBACK
|POPT_CBFLAG_PRE
|POPT_CBFLAG_POST
, (void *)popt_common_callback
},
164 { "configfile", 0, POPT_ARG_STRING
, NULL
, 's', "Use alternate configuration file", "CONFIGFILE" },
168 struct poptOption popt_common_version
[] = {
169 { NULL
, 0, POPT_ARG_CALLBACK
|POPT_CBFLAG_POST
, (void *)popt_common_callback
},
170 { "version", 'V', POPT_ARG_NONE
, NULL
, 'V', "Print version" },
174 struct poptOption popt_common_debuglevel
[] = {
175 { NULL
, 0, POPT_ARG_CALLBACK
, (void *)popt_common_callback
},
176 { "debuglevel", 'd', POPT_ARG_STRING
, NULL
, 'd', "Set debug level", "DEBUGLEVEL" },
181 /* Handle command line options:
214 static void popt_dynconfig_callback(poptContext con
,
215 enum poptCallbackReason reason
,
216 const struct poptOption
*opt
,
217 const char *arg
, const void *data
)
223 set_dyn_SBINDIR(arg
);
235 set_dyn_SWATDIR(arg
);
239 case DYN_LMHOSTSFILE
:
241 set_dyn_LMHOSTSFILE(arg
);
253 set_dyn_MODULESDIR(arg
);
259 set_dyn_SHLIBEXT(arg
);
265 set_dyn_LOCKDIR(arg
);
271 set_dyn_STATEDIR(arg
);
277 set_dyn_CACHEDIR(arg
);
287 case DYN_SMB_PASSWD_FILE
:
289 set_dyn_SMB_PASSWD_FILE(arg
);
293 case DYN_PRIVATE_DIR
:
295 set_dyn_PRIVATE_DIR(arg
);
302 const struct poptOption popt_common_dynconfig
[] = {
304 { NULL
, '\0', POPT_ARG_CALLBACK
, (void *)popt_dynconfig_callback
},
306 { "sbindir", '\0' , POPT_ARG_STRING
, NULL
, DYN_SBINDIR
,
307 "Path to sbin directory", "SBINDIR" },
308 { "bindir", '\0' , POPT_ARG_STRING
, NULL
, DYN_BINDIR
,
309 "Path to bin directory", "BINDIR" },
310 { "swatdir", '\0' , POPT_ARG_STRING
, NULL
, DYN_SWATDIR
,
311 "Path to SWAT installation directory", "SWATDIR" },
312 { "lmhostsfile", '\0' , POPT_ARG_STRING
, NULL
, DYN_LMHOSTSFILE
,
313 "Path to lmhosts file", "LMHOSTSFILE" },
314 { "libdir", '\0' , POPT_ARG_STRING
, NULL
, DYN_LIBDIR
,
315 "Path to shared library directory", "LIBDIR" },
316 { "modulesdir", '\0' , POPT_ARG_STRING
, NULL
, DYN_MODULESDIR
,
317 "Path to shared modules directory", "MODULESDIR" },
318 { "shlibext", '\0' , POPT_ARG_STRING
, NULL
, DYN_SHLIBEXT
,
319 "Shared library extension", "SHLIBEXT" },
320 { "lockdir", '\0' , POPT_ARG_STRING
, NULL
, DYN_LOCKDIR
,
321 "Path to lock file directory", "LOCKDIR" },
322 { "statedir", '\0' , POPT_ARG_STRING
, NULL
, DYN_STATEDIR
,
323 "Path to persistent state file directory", "STATEDIR" },
324 { "cachedir", '\0' , POPT_ARG_STRING
, NULL
, DYN_CACHEDIR
,
325 "Path to temporary cache file directory", "CACHEDIR" },
326 { "piddir", '\0' , POPT_ARG_STRING
, NULL
, DYN_PIDDIR
,
327 "Path to PID file directory", "PIDDIR" },
328 { "smb-passwd-file", '\0' , POPT_ARG_STRING
, NULL
, DYN_SMB_PASSWD_FILE
,
329 "Path to smbpasswd file", "SMB_PASSWD_FILE" },
330 { "private-dir", '\0' , POPT_ARG_STRING
, NULL
, DYN_PRIVATE_DIR
,
331 "Path to private data directory", "PRIVATE_DIR" },
336 /****************************************************************************
337 * get a password from a a file or file descriptor
339 * ****************************************************************************/
341 static void get_password_file(struct user_auth_info
*auth_info
)
345 bool close_it
= False
;
349 if ((p
= getenv("PASSWD_FD")) != NULL
) {
350 if (asprintf(&spec
, "descriptor %s", p
) < 0) {
353 sscanf(p
, "%d", &fd
);
355 } else if ((p
= getenv("PASSWD_FILE")) != NULL
) {
356 fd
= sys_open(p
, O_RDONLY
, 0);
357 spec
= SMB_STRDUP(p
);
359 fprintf(stderr
, "Error opening PASSWD_FILE %s: %s\n",
360 spec
, strerror(errno
));
367 fprintf(stderr
, "fd = %d, < 0\n", fd
);
371 for(p
= pass
, *p
= '\0'; /* ensure that pass is null-terminated */
372 p
&& p
- pass
< sizeof(pass
);) {
373 switch (read(fd
, p
, 1)) {
375 if (*p
!= '\n' && *p
!= '\0') {
376 *++p
= '\0'; /* advance p, and null-terminate pass */
381 *p
= '\0'; /* null-terminate it, just in case... */
382 p
= NULL
; /* then force the loop condition to become false */
385 fprintf(stderr
, "Error reading password from file %s: %s\n",
386 spec
, "empty password\n");
392 fprintf(stderr
, "Error reading password from file %s: %s\n",
393 spec
, strerror(errno
));
400 set_cmdline_auth_info_password(auth_info
, pass
);
406 static void get_credentials_file(struct user_auth_info
*auth_info
,
412 char *ptr
, *val
, *param
;
414 if ((auth
=x_fopen(file
, O_RDONLY
, 0)) == NULL
)
416 /* fail if we can't open the credentials file */
417 d_printf("ERROR: Unable to open credentials file!\n");
421 while (!x_feof(auth
))
423 /* get a line from the file */
424 if (!x_fgets(buf
, sizeof(buf
), auth
))
428 if ((len
) && (buf
[len
-1]=='\n'))
436 /* break up the line into parameter & value.
437 * will need to eat a little whitespace possibly */
439 if (!(ptr
= strchr_m (buf
, '=')))
445 /* eat leading white space */
446 while ((*val
!='\0') && ((*val
==' ') || (*val
=='\t')))
449 if (strwicmp("password", param
) == 0) {
450 set_cmdline_auth_info_password(auth_info
, val
);
451 } else if (strwicmp("username", param
) == 0) {
452 set_cmdline_auth_info_username(auth_info
, val
);
453 } else if (strwicmp("domain", param
) == 0) {
454 set_global_myworkgroup(val
);
456 memset(buf
, 0, sizeof(buf
));
461 /* Handle command line options:
463 * -A,--authentication-file
472 static void popt_common_credentials_callback(poptContext con
,
473 enum poptCallbackReason reason
,
474 const struct poptOption
*opt
,
475 const char *arg
, const void *data
)
477 struct user_auth_info
*auth_info
= talloc_get_type_abort(
478 *((const char **)data
), struct user_auth_info
);
481 if (reason
== POPT_CALLBACK_REASON_PRE
) {
482 set_cmdline_auth_info_username(auth_info
, "GUEST");
484 if (getenv("LOGNAME")) {
485 set_cmdline_auth_info_username(auth_info
,
489 if (getenv("USER")) {
490 char *puser
= SMB_STRDUP(getenv("USER"));
494 set_cmdline_auth_info_username(auth_info
, puser
);
496 if ((p
= strchr_m(puser
,'%'))) {
500 set_cmdline_auth_info_password(auth_info
, p
+1);
501 memset(strchr_m(getenv("USER"),'%')+1,'X',len
);
506 if (getenv("PASSWD")) {
507 set_cmdline_auth_info_password(auth_info
,
511 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
512 get_password_file(auth_info
);
522 char *puser
= SMB_STRDUP(arg
);
524 if ((lp
=strchr_m(puser
,'%'))) {
527 set_cmdline_auth_info_username(auth_info
,
529 set_cmdline_auth_info_password(auth_info
,
532 memset(strchr_m(arg
,'%')+1,'X',len
);
534 set_cmdline_auth_info_username(auth_info
,
542 get_credentials_file(auth_info
, arg
);
547 d_printf("No kerberos support compiled in\n");
550 set_cmdline_auth_info_use_krb5_ticket(auth_info
);
555 if (!set_cmdline_auth_info_signing_state(auth_info
, arg
)) {
556 fprintf(stderr
, "Unknown signing option %s\n", arg
);
561 set_cmdline_auth_info_use_machine_account(auth_info
);
564 set_cmdline_auth_info_password(auth_info
, "");
567 set_cmdline_auth_info_smb_encrypt(auth_info
);
573 static struct user_auth_info
*global_auth_info
;
575 void popt_common_set_auth_info(struct user_auth_info
*auth_info
)
577 global_auth_info
= auth_info
;
580 struct poptOption popt_common_credentials
[] = {
581 { NULL
, 0, POPT_ARG_CALLBACK
|POPT_CBFLAG_PRE
,
582 (void *)popt_common_credentials_callback
, 0,
583 (const char *)&global_auth_info
},
584 { "user", 'U', POPT_ARG_STRING
, NULL
, 'U', "Set the network username", "USERNAME" },
585 { "no-pass", 'N', POPT_ARG_NONE
, NULL
, 'N', "Don't ask for a password" },
586 { "kerberos", 'k', POPT_ARG_NONE
, NULL
, 'k', "Use kerberos (active directory) authentication" },
587 { "authentication-file", 'A', POPT_ARG_STRING
, NULL
, 'A', "Get the credentials from a file", "FILE" },
588 { "signing", 'S', POPT_ARG_STRING
, NULL
, 'S', "Set the client signing state", "on|off|required" },
589 {"machine-pass", 'P', POPT_ARG_NONE
, NULL
, 'P', "Use stored machine account password" },
590 {"encrypt", 'e', POPT_ARG_NONE
, NULL
, 'e', "Encrypt SMB transport (UNIX extended servers only)" },