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 pstring user_socket_options;
37 extern BOOL AllowDebugChange;
38 extern BOOL override_logfile;
40 struct user_auth_info cmdline_auth_info;
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 pstr_sprintf(logfile, "%s/log.%s", arg, pname);
57 lp_set_logfile(logfile);
60 static void popt_common_callback(poptContext con,
61 enum poptCallbackReason reason,
62 const struct poptOption *opt,
63 const char *arg, const void *data)
66 if (reason == POPT_CALLBACK_REASON_PRE) {
67 set_logfile(con, dyn_LOGFILEBASE);
74 debug_parse_levels(arg);
75 AllowDebugChange = False;
80 printf( "Version %s\n", SAMBA_VERSION_STRING);
86 pstrcpy(user_socket_options,arg);
92 pstrcpy(dyn_CONFIGFILE, arg);
98 set_global_myname(arg);
104 set_logfile(con, arg);
105 override_logfile = True;
106 pstr_sprintf(dyn_LOGFILEBASE, "%s", arg);
112 set_global_scope(arg);
118 set_global_myworkgroup(arg);
124 struct poptOption popt_common_connection[] = {
125 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
126 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
128 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
129 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
130 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
135 struct poptOption popt_common_samba[] = {
136 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, (void *)popt_common_callback },
137 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
138 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
139 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Base name for log files", "LOGFILEBASE" },
140 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
144 struct poptOption popt_common_version[] = {
145 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
146 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
151 /* Handle command line options:
178 static void popt_dynconfig_callback(poptContext con,
179 enum poptCallbackReason reason,
180 const struct poptOption *opt,
181 const char *arg, const void *data)
187 dyn_SBINDIR = SMB_STRDUP(arg);
193 dyn_BINDIR = SMB_STRDUP(arg);
199 dyn_SWATDIR = SMB_STRDUP(arg);
203 case DYN_LMHOSTSFILE:
205 pstrcpy(dyn_LMHOSTSFILE, arg);
211 pstrcpy(dyn_LIBDIR, arg);
217 fstrcpy(dyn_SHLIBEXT, arg);
223 pstrcpy(dyn_LOCKDIR, arg);
229 pstrcpy(dyn_PIDDIR, arg);
233 case DYN_SMB_PASSWD_FILE:
235 pstrcpy(dyn_SMB_PASSWD_FILE, arg);
239 case DYN_PRIVATE_DIR:
241 pstrcpy(dyn_PRIVATE_DIR, arg);
248 const struct poptOption popt_common_dynconfig[] = {
250 { NULL, '\0', POPT_ARG_CALLBACK, (void *)popt_dynconfig_callback },
252 { "sbindir", '\0' , POPT_ARG_STRING, NULL, DYN_SBINDIR,
253 "Path to sbin directory", "SBINDIR" },
254 { "bindir", '\0' , POPT_ARG_STRING, NULL, DYN_BINDIR,
255 "Path to bin directory", "BINDIR" },
256 { "swatdir", '\0' , POPT_ARG_STRING, NULL, DYN_SWATDIR,
257 "Path to SWAT installation directory", "SWATDIR" },
258 { "lmhostsfile", '\0' , POPT_ARG_STRING, NULL, DYN_LMHOSTSFILE,
259 "Path to lmhosts file", "LMHOSTSFILE" },
260 { "libdir", '\0' , POPT_ARG_STRING, NULL, DYN_LIBDIR,
261 "Path to shared library directory", "LIBDIR" },
262 { "shlibext", '\0' , POPT_ARG_STRING, NULL, DYN_SHLIBEXT,
263 "Shared library extension", "SHLIBEXT" },
264 { "lockdir", '\0' , POPT_ARG_STRING, NULL, DYN_LOCKDIR,
265 "Path to lock file directory", "LOCKDIR" },
266 { "piddir", '\0' , POPT_ARG_STRING, NULL, DYN_PIDDIR,
267 "Path to PID file directory", "PIDDIR" },
268 { "smb-passwd-file", '\0' , POPT_ARG_STRING, NULL, DYN_SMB_PASSWD_FILE,
269 "Path to smbpasswd file", "SMB_PASSWD_FILE" },
270 { "private-dir", '\0' , POPT_ARG_STRING, NULL, DYN_PRIVATE_DIR,
271 "Path to private data directory", "PRIVATE_DIR" },
276 /****************************************************************************
277 * get a password from a a file or file descriptor
279 * ****************************************************************************/
280 static void get_password_file(struct user_auth_info *a)
284 BOOL close_it = False;
288 if ((p = getenv("PASSWD_FD")) != NULL) {
289 pstrcpy(spec, "descriptor ");
291 sscanf(p, "%d", &fd);
293 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
294 fd = sys_open(p, O_RDONLY, 0);
297 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
298 spec, strerror(errno));
304 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
305 p && p - pass < sizeof(pass);) {
306 switch (read(fd, p, 1)) {
308 if (*p != '\n' && *p != '\0') {
309 *++p = '\0'; /* advance p, and null-terminate pass */
314 *p = '\0'; /* null-terminate it, just in case... */
315 p = NULL; /* then force the loop condition to become false */
318 fprintf(stderr, "Error reading password from file %s: %s\n",
319 spec, "empty password\n");
324 fprintf(stderr, "Error reading password from file %s: %s\n",
325 spec, strerror(errno));
329 pstrcpy(a->password, pass);
334 static void get_credentials_file(const char *file, struct user_auth_info *info)
339 char *ptr, *val, *param;
341 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
343 /* fail if we can't open the credentials file */
344 d_printf("ERROR: Unable to open credentials file!\n");
348 while (!x_feof(auth))
350 /* get a line from the file */
351 if (!x_fgets(buf, sizeof(buf), auth))
355 if ((len) && (buf[len-1]=='\n'))
363 /* break up the line into parameter & value.
364 * will need to eat a little whitespace possibly */
366 if (!(ptr = strchr_m (buf, '=')))
372 /* eat leading white space */
373 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
376 if (strwicmp("password", param) == 0)
378 pstrcpy(info->password, val);
379 info->got_pass = True;
381 else if (strwicmp("username", param) == 0)
382 pstrcpy(info->username, val);
383 else if (strwicmp("domain", param) == 0)
384 set_global_myworkgroup(val);
385 memset(buf, 0, sizeof(buf));
390 /* Handle command line options:
392 * -A,--authentication-file
400 static void popt_common_credentials_callback(poptContext con,
401 enum poptCallbackReason reason,
402 const struct poptOption *opt,
403 const char *arg, const void *data)
407 if (reason == POPT_CALLBACK_REASON_PRE) {
408 cmdline_auth_info.use_kerberos = False;
409 cmdline_auth_info.got_pass = False;
410 cmdline_auth_info.signing_state = Undefined;
411 pstrcpy(cmdline_auth_info.username, "GUEST");
413 if (getenv("LOGNAME"))pstrcpy(cmdline_auth_info.username,getenv("LOGNAME"));
415 if (getenv("USER")) {
416 pstrcpy(cmdline_auth_info.username,getenv("USER"));
418 if ((p = strchr_m(cmdline_auth_info.username,'%'))) {
420 pstrcpy(cmdline_auth_info.password,p+1);
421 cmdline_auth_info.got_pass = True;
422 memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(cmdline_auth_info.password));
426 if (getenv("PASSWD")) {
427 pstrcpy(cmdline_auth_info.password,getenv("PASSWD"));
428 cmdline_auth_info.got_pass = True;
431 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
432 get_password_file(&cmdline_auth_info);
433 cmdline_auth_info.got_pass = True;
444 pstrcpy(cmdline_auth_info.username,arg);
445 if ((lp=strchr_m(cmdline_auth_info.username,'%'))) {
447 pstrcpy(cmdline_auth_info.password,lp+1);
448 cmdline_auth_info.got_pass = True;
449 memset(strchr_m(arg,'%')+1,'X',strlen(cmdline_auth_info.password));
455 get_credentials_file(arg, &cmdline_auth_info);
460 d_printf("No kerberos support compiled in\n");
463 cmdline_auth_info.use_kerberos = True;
464 cmdline_auth_info.got_pass = True;
470 cmdline_auth_info.signing_state = -1;
471 if (strequal(arg, "off") || strequal(arg, "no") || strequal(arg, "false"))
472 cmdline_auth_info.signing_state = False;
473 else if (strequal(arg, "on") || strequal(arg, "yes") || strequal(arg, "true") ||
474 strequal(arg, "auto") )
475 cmdline_auth_info.signing_state = True;
476 else if (strequal(arg, "force") || strequal(arg, "required") || strequal(arg, "forced"))
477 cmdline_auth_info.signing_state = Required;
479 fprintf(stderr, "Unknown signing option %s\n", arg );
486 char *opt_password = NULL;
487 /* it is very useful to be able to make ads queries as the
488 machine account for testing purposes and for domain leave */
490 if (!secrets_init()) {
491 d_printf("ERROR: Unable to open secrets database\n");
495 opt_password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
498 d_printf("ERROR: Unable to fetch machine password\n");
501 pstr_sprintf(cmdline_auth_info.username, "%s$",
503 pstrcpy(cmdline_auth_info.password,opt_password);
504 SAFE_FREE(opt_password);
506 /* machine accounts only work with kerberos */
507 cmdline_auth_info.use_kerberos = True;
508 cmdline_auth_info.got_pass = True;
516 struct poptOption popt_common_credentials[] = {
517 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, (void *)popt_common_credentials_callback },
518 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
519 { "no-pass", 'N', POPT_ARG_NONE, &cmdline_auth_info.got_pass, 0, "Don't ask for a password" },
520 { "kerberos", 'k', POPT_ARG_NONE, &cmdline_auth_info.use_kerberos, 'k', "Use kerberos (active directory) authentication" },
521 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
522 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
523 {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },