2 Unix SMB/CIFS implementation.
5 Copyright (C) Tim Potter 2001,2002
6 Copyright (C) Jelmer Vernooij 2002,2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /* Handle command line options:
37 enum {OPT_OPTION=1,OPT_LEAK_REPORT,OPT_LEAK_REPORT_FULL};
39 static struct cmdline_auth_info cmdline_auth_info;
41 static void popt_common_callback(poptContext con,
42 enum poptCallbackReason reason,
43 const struct poptOption *opt,
44 const char *arg, const void *data)
48 /* Find out basename of current program */
49 pname = strrchr_m(poptGetInvocationName(con),'/');
52 pname = poptGetInvocationName(con);
56 if (reason == POPT_CALLBACK_REASON_PRE) {
57 char *logfile = talloc_asprintf(NULL, "%s/log.%s", dyn_LOGFILEBASE, pname);
58 lp_set_cmdline("log file", logfile);
65 lp_set_cmdline("log level", arg);
69 printf( "Version %s\n", SAMBA_VERSION_STRING );
75 lp_set_cmdline("socket options", arg);
81 pstrcpy(dyn_CONFIGFILE, arg);
87 char *logfile = talloc_asprintf(NULL, "%s/log.%s", arg, pname);
88 lp_set_cmdline("log file", logfile);
94 lp_set_cmdline("workgroup", arg);
98 lp_set_cmdline("netbios name", arg);
102 lp_set_cmdline("netbios scope", arg);
106 lp_set_cmdline("max protocol", arg);
110 lp_set_cmdline("name resolve order", arg);
114 if (!lp_set_option(arg)) {
115 fprintf(stderr, "Error setting option '%s'\n", arg);
120 case OPT_LEAK_REPORT:
121 talloc_enable_leak_report();
124 case OPT_LEAK_REPORT_FULL:
125 talloc_enable_leak_report_full();
130 struct poptOption popt_common_connection[] = {
131 { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
132 { "name-resolve", 'R', POPT_ARG_STRING, NULL, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
133 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use", "SOCKETOPTIONS" },
134 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
135 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
136 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
137 { "maxprotocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set max protocol level", "MAXPROTOCOL" },
141 struct poptOption popt_common_samba[] = {
142 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_callback },
143 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
144 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternative configuration file", "CONFIGFILE" },
145 { "option", 0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
146 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Basename for log/debug files", "LOGFILEBASE" },
147 { "leak-report", 0, POPT_ARG_NONE, NULL, OPT_LEAK_REPORT, "enable talloc leak reporting on exit", NULL },
148 { "leak-report-full",0, POPT_ARG_NONE, NULL, OPT_LEAK_REPORT_FULL, "enable full talloc leak reporting on exit", NULL },
152 struct poptOption popt_common_version[] = {
153 { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
154 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
160 /****************************************************************************
161 * get a password from a a file or file descriptor
163 * ****************************************************************************/
164 static void get_password_file(struct cmdline_auth_info *a)
168 BOOL close_it = False;
172 if ((p = getenv("PASSWD_FD")) != NULL) {
173 pstrcpy(spec, "descriptor ");
175 sscanf(p, "%d", &fd);
177 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
178 fd = open(p, O_RDONLY, 0);
181 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
182 spec, strerror(errno));
188 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
189 p && p - pass < sizeof(pass);) {
190 switch (read(fd, p, 1)) {
192 if (*p != '\n' && *p != '\0') {
193 *++p = '\0'; /* advance p, and null-terminate pass */
198 *p = '\0'; /* null-terminate it, just in case... */
199 p = NULL; /* then force the loop condition to become false */
202 fprintf(stderr, "Error reading password from file %s: %s\n",
203 spec, "empty password\n");
208 fprintf(stderr, "Error reading password from file %s: %s\n",
209 spec, strerror(errno));
213 pstrcpy(a->password, pass);
218 static void get_credentials_file(const char *file, struct cmdline_auth_info *info)
223 char *ptr, *val, *param;
225 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
227 /* fail if we can't open the credentials file */
228 d_printf("ERROR: Unable to open credentials file!\n");
232 while (!x_feof(auth))
234 /* get a line from the file */
235 if (!x_fgets(buf, sizeof(buf), auth))
239 if ((len) && (buf[len-1]=='\n'))
247 /* break up the line into parameter & value.
248 * will need to eat a little whitespace possibly */
250 if (!(ptr = strchr_m (buf, '=')))
256 /* eat leading white space */
257 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
260 if (strwicmp("password", param) == 0) {
261 pstrcpy(info->password, val);
262 info->got_pass = True;
263 } else if (strwicmp("username", param) == 0) {
264 pstrcpy(info->username, val);
265 } else if (strwicmp("domain", param) == 0) {
266 pstrcpy(info->domain,val);
267 info->got_domain = True;
269 memset(buf, 0, sizeof(buf));
274 /* Handle command line options:
276 * -A,--authentication-file
284 static void popt_common_credentials_callback(poptContext con,
285 enum poptCallbackReason reason,
286 const struct poptOption *opt,
287 const char *arg, const void *data)
291 if (reason == POPT_CALLBACK_REASON_PRE) {
292 cmdline_auth_info.use_kerberos = False;
293 cmdline_auth_info.got_pass = False;
294 pstrcpy(cmdline_auth_info.username, "GUEST");
296 if (getenv("LOGNAME"))pstrcpy(cmdline_auth_info.username,getenv("LOGNAME"));
298 if (getenv("USER")) {
301 pstrcpy(cmdline_auth_info.username,getenv("USER"));
303 pstrcpy(tmp,cmdline_auth_info.username);
304 if ((p = strchr_m(tmp,'\\'))) {
306 pstrcpy(cmdline_auth_info.domain,tmp);
307 cmdline_auth_info.got_domain = True;
308 pstrcpy(cmdline_auth_info.username,p+1);
311 if ((p = strchr_m(cmdline_auth_info.username,'%'))) {
313 pstrcpy(cmdline_auth_info.password,p+1);
314 cmdline_auth_info.got_pass = True;
315 memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(cmdline_auth_info.password));
319 if (getenv("DOMAIN")) {
320 pstrcpy(cmdline_auth_info.domain,getenv("DOMAIN"));
321 cmdline_auth_info.got_domain = True;
324 if (getenv("PASSWD")) {
325 pstrcpy(cmdline_auth_info.password,getenv("PASSWD"));
326 cmdline_auth_info.got_pass = True;
329 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
330 get_password_file(&cmdline_auth_info);
331 cmdline_auth_info.got_pass = True;
343 pstrcpy(cmdline_auth_info.username,arg);
345 pstrcpy(tmp,cmdline_auth_info.username);
346 if ((p = strchr_m(tmp,'\\'))) {
348 pstrcpy(cmdline_auth_info.domain,tmp);
349 cmdline_auth_info.got_domain = True;
350 pstrcpy(cmdline_auth_info.username,p+1);
353 if ((lp=strchr_m(cmdline_auth_info.username,'%'))) {
355 pstrcpy(cmdline_auth_info.password,lp+1);
356 cmdline_auth_info.got_pass = True;
357 memset(strchr_m(arg,'%')+1,'X',strlen(cmdline_auth_info.password));
363 get_credentials_file(arg, &cmdline_auth_info);
368 d_printf("No kerberos support compiled in\n");
371 cmdline_auth_info.use_kerberos = True;
372 cmdline_auth_info.got_pass = True;
377 lp_set_cmdline("client signing", arg);
382 char *opt_password = NULL;
383 /* it is very useful to be able to make ads queries as the
384 machine account for testing purposes and for domain leave */
386 if (!secrets_init()) {
387 d_printf("ERROR: Unable to open secrets database\n");
391 opt_password = secrets_fetch_machine_password(lp_workgroup());
394 d_printf("ERROR: Unable to fetch machine password\n");
397 snprintf(cmdline_auth_info.username, sizeof(cmdline_auth_info.username),
398 "%s$", lp_netbios_name());
399 pstrcpy(cmdline_auth_info.password,opt_password);
400 SAFE_FREE(opt_password);
401 cmdline_auth_info.got_pass = True;
403 pstrcpy(cmdline_auth_info.domain, lp_workgroup());
404 cmdline_auth_info.got_domain = True;
406 /* machine accounts only work with kerberos */
407 cmdline_auth_info.use_kerberos = True;
415 struct poptOption popt_common_credentials[] = {
416 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_credentials_callback },
417 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
418 { "no-pass", 'N', POPT_ARG_NONE, &cmdline_auth_info.got_pass, 0, "Don't ask for a password" },
419 { "kerberos", 'k', POPT_ARG_NONE, &cmdline_auth_info.use_kerberos, 'k', "Use kerberos (active directory) authentication" },
420 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
421 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
422 { "machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
426 void cmdline_set_username(const char *name)
428 pstrcpy(cmdline_auth_info.username, name);
431 const char *cmdline_get_username(void)
433 return cmdline_auth_info.username;
436 void cmdline_set_userdomain(const char *domain)
438 cmdline_auth_info.got_domain = True;
439 pstrcpy(cmdline_auth_info.domain, domain);
442 const char *cmdline_get_userdomain(void)
444 if (cmdline_auth_info.got_domain) {
445 return cmdline_auth_info.domain;
448 /* I think this should be lp_netbios_name()
449 * instead of lp_workgroup(), because if you're logged in
450 * as domain user the getenv("USER") contains the domain
451 * and this code path isn't used
454 return lp_netbios_name();
457 const char *cmdline_get_userpassword(void)
462 if (cmdline_auth_info.got_pass) {
463 return cmdline_auth_info.password;
466 prompt = talloc_asprintf(NULL, "Password for [%s\\%s]:",
467 cmdline_get_userdomain(),
468 cmdline_get_username());
470 ret = getpass(prompt);
476 void cmdline_set_userpassword(const char *pass)
478 cmdline_auth_info.got_pass = True;
479 pstrcpy(cmdline_auth_info.password, pass);
482 void cmdline_set_use_kerberos(BOOL use_kerberos)
484 cmdline_auth_info.use_kerberos = use_kerberos;
487 BOOL cmdline_get_use_kerberos(void)
489 return cmdline_auth_info.use_kerberos;