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.
24 #include "lib/cmdline/popt_common.h"
26 /* Handle command line options:
38 enum {OPT_OPTION=1,OPT_LEAK_REPORT,OPT_LEAK_REPORT_FULL};
40 static struct cmdline_auth_info cmdline_auth_info;
42 static void popt_common_callback(poptContext con,
43 enum poptCallbackReason reason,
44 const struct poptOption *opt,
45 const char *arg, const void *data)
49 /* Find out basename of current program */
50 pname = strrchr_m(poptGetInvocationName(con),'/');
53 pname = poptGetInvocationName(con);
57 if (reason == POPT_CALLBACK_REASON_PRE) {
58 char *logfile = talloc_asprintf(NULL, "%s/log.%s", dyn_LOGFILEBASE, pname);
59 lp_set_cmdline("log file", logfile);
66 lp_set_cmdline("log level", arg);
70 printf( "Version %s\n", SAMBA_VERSION_STRING );
76 lp_set_cmdline("socket options", arg);
82 pstrcpy(dyn_CONFIGFILE, arg);
88 char *logfile = talloc_asprintf(NULL, "%s/log.%s", arg, pname);
89 lp_set_cmdline("log file", logfile);
95 lp_set_cmdline("workgroup", arg);
99 lp_set_cmdline("netbios name", arg);
103 lp_set_cmdline("netbios scope", arg);
107 lp_set_cmdline("max protocol", arg);
111 lp_set_cmdline("name resolve order", arg);
115 if (!lp_set_option(arg)) {
116 fprintf(stderr, "Error setting option '%s'\n", arg);
121 case OPT_LEAK_REPORT:
122 talloc_enable_leak_report();
125 case OPT_LEAK_REPORT_FULL:
126 talloc_enable_leak_report_full();
131 struct poptOption popt_common_connection[] = {
132 { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
133 { "name-resolve", 'R', POPT_ARG_STRING, NULL, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
134 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use", "SOCKETOPTIONS" },
135 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
136 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
137 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
138 { "maxprotocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set max protocol level", "MAXPROTOCOL" },
142 struct poptOption popt_common_samba[] = {
143 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_callback },
144 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
145 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternative configuration file", "CONFIGFILE" },
146 { "option", 0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
147 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Basename for log/debug files", "LOGFILEBASE" },
148 { "leak-report", 0, POPT_ARG_NONE, NULL, OPT_LEAK_REPORT, "enable talloc leak reporting on exit", NULL },
149 { "leak-report-full",0, POPT_ARG_NONE, NULL, OPT_LEAK_REPORT_FULL, "enable full talloc leak reporting on exit", NULL },
153 struct poptOption popt_common_version[] = {
154 { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
155 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
161 /****************************************************************************
162 * get a password from a a file or file descriptor
164 * ****************************************************************************/
165 static void get_password_file(struct cmdline_auth_info *a)
169 BOOL close_it = False;
173 if ((p = getenv("PASSWD_FD")) != NULL) {
174 pstrcpy(spec, "descriptor ");
176 sscanf(p, "%d", &fd);
178 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
179 fd = open(p, O_RDONLY, 0);
182 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
183 spec, strerror(errno));
189 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
190 p && p - pass < sizeof(pass);) {
191 switch (read(fd, p, 1)) {
193 if (*p != '\n' && *p != '\0') {
194 *++p = '\0'; /* advance p, and null-terminate pass */
199 *p = '\0'; /* null-terminate it, just in case... */
200 p = NULL; /* then force the loop condition to become false */
203 fprintf(stderr, "Error reading password from file %s: %s\n",
204 spec, "empty password\n");
209 fprintf(stderr, "Error reading password from file %s: %s\n",
210 spec, strerror(errno));
214 pstrcpy(a->password, pass);
219 static void get_credentials_file(const char *file, struct cmdline_auth_info *info)
224 char *ptr, *val, *param;
226 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
228 /* fail if we can't open the credentials file */
229 d_printf("ERROR: Unable to open credentials file!\n");
233 while (!x_feof(auth))
235 /* get a line from the file */
236 if (!x_fgets(buf, sizeof(buf), auth))
240 if ((len) && (buf[len-1]=='\n'))
248 /* break up the line into parameter & value.
249 * will need to eat a little whitespace possibly */
251 if (!(ptr = strchr_m (buf, '=')))
257 /* eat leading white space */
258 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
261 if (strwicmp("password", param) == 0) {
262 pstrcpy(info->password, val);
263 info->got_pass = True;
264 } else if (strwicmp("username", param) == 0) {
265 pstrcpy(info->username, val);
266 } else if (strwicmp("domain", param) == 0) {
267 pstrcpy(info->domain,val);
268 info->got_domain = True;
270 memset(buf, 0, sizeof(buf));
275 /* Handle command line options:
277 * -A,--authentication-file
285 static void popt_common_credentials_callback(poptContext con,
286 enum poptCallbackReason reason,
287 const struct poptOption *opt,
288 const char *arg, const void *data)
292 if (reason == POPT_CALLBACK_REASON_PRE) {
293 cmdline_auth_info.use_kerberos = False;
294 cmdline_auth_info.got_pass = False;
295 pstrcpy(cmdline_auth_info.username, "GUEST");
297 if (getenv("LOGNAME"))pstrcpy(cmdline_auth_info.username,getenv("LOGNAME"));
299 if (getenv("USER")) {
302 pstrcpy(cmdline_auth_info.username,getenv("USER"));
304 pstrcpy(tmp,cmdline_auth_info.username);
305 if ((p = strchr_m(tmp,'\\'))) {
307 pstrcpy(cmdline_auth_info.domain,tmp);
308 cmdline_auth_info.got_domain = True;
309 pstrcpy(cmdline_auth_info.username,p+1);
312 if ((p = strchr_m(cmdline_auth_info.username,'%'))) {
314 pstrcpy(cmdline_auth_info.password,p+1);
315 cmdline_auth_info.got_pass = True;
316 memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(cmdline_auth_info.password));
320 if (getenv("DOMAIN")) {
321 pstrcpy(cmdline_auth_info.domain,getenv("DOMAIN"));
322 cmdline_auth_info.got_domain = True;
325 if (getenv("PASSWD")) {
326 pstrcpy(cmdline_auth_info.password,getenv("PASSWD"));
327 cmdline_auth_info.got_pass = True;
330 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
331 get_password_file(&cmdline_auth_info);
332 cmdline_auth_info.got_pass = True;
344 pstrcpy(cmdline_auth_info.username,arg);
346 pstrcpy(tmp,cmdline_auth_info.username);
347 if ((p = strchr_m(tmp,'\\'))) {
349 pstrcpy(cmdline_auth_info.domain,tmp);
350 cmdline_auth_info.got_domain = True;
351 pstrcpy(cmdline_auth_info.username,p+1);
354 if ((lp=strchr_m(cmdline_auth_info.username,'%'))) {
356 pstrcpy(cmdline_auth_info.password,lp+1);
357 cmdline_auth_info.got_pass = True;
358 memset(strchr_m(arg,'%')+1,'X',strlen(cmdline_auth_info.password));
364 get_credentials_file(arg, &cmdline_auth_info);
369 d_printf("No kerberos support compiled in\n");
372 cmdline_auth_info.use_kerberos = True;
373 cmdline_auth_info.got_pass = True;
378 lp_set_cmdline("client signing", arg);
383 char *opt_password = NULL;
384 /* it is very useful to be able to make ads queries as the
385 machine account for testing purposes and for domain leave */
387 if (!secrets_init()) {
388 d_printf("ERROR: Unable to open secrets database\n");
392 opt_password = secrets_fetch_machine_password(lp_workgroup());
395 d_printf("ERROR: Unable to fetch machine password\n");
398 snprintf(cmdline_auth_info.username, sizeof(cmdline_auth_info.username),
399 "%s$", lp_netbios_name());
400 pstrcpy(cmdline_auth_info.password,opt_password);
401 SAFE_FREE(opt_password);
402 cmdline_auth_info.got_pass = True;
404 pstrcpy(cmdline_auth_info.domain, lp_workgroup());
405 cmdline_auth_info.got_domain = True;
407 /* machine accounts only work with kerberos */
408 cmdline_auth_info.use_kerberos = True;
416 struct poptOption popt_common_credentials[] = {
417 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_credentials_callback },
418 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
419 { "no-pass", 'N', POPT_ARG_NONE, &cmdline_auth_info.got_pass, 0, "Don't ask for a password" },
420 { "kerberos", 'k', POPT_ARG_NONE, &cmdline_auth_info.use_kerberos, 'k', "Use kerberos (active directory) authentication" },
421 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
422 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
423 { "machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
427 void cmdline_set_username(const char *name)
429 pstrcpy(cmdline_auth_info.username, name);
432 const char *cmdline_get_username(void)
434 return cmdline_auth_info.username;
437 void cmdline_set_userdomain(const char *domain)
439 cmdline_auth_info.got_domain = True;
440 pstrcpy(cmdline_auth_info.domain, domain);
443 const char *cmdline_get_userdomain(void)
445 if (cmdline_auth_info.got_domain) {
446 return cmdline_auth_info.domain;
449 /* I think this should be lp_netbios_name()
450 * instead of lp_workgroup(), because if you're logged in
451 * as domain user the getenv("USER") contains the domain
452 * and this code path isn't used
455 return lp_netbios_name();
458 const char *cmdline_get_userpassword(void)
463 if (cmdline_auth_info.got_pass) {
464 return cmdline_auth_info.password;
467 prompt = talloc_asprintf(NULL, "Password for [%s\\%s]:",
468 cmdline_get_userdomain(),
469 cmdline_get_username());
471 ret = getpass(prompt);
477 void cmdline_set_userpassword(const char *pass)
479 cmdline_auth_info.got_pass = True;
480 pstrcpy(cmdline_auth_info.password, pass);
483 void cmdline_set_use_kerberos(BOOL use_kerberos)
485 cmdline_auth_info.use_kerberos = use_kerberos;
488 BOOL cmdline_get_use_kerberos(void)
490 return cmdline_auth_info.use_kerberos;