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_CHECK=2};
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);
121 talloc_enable_leak_check();
126 struct poptOption popt_common_connection[] = {
127 { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
128 { "name-resolve", 'R', POPT_ARG_STRING, NULL, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
129 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use", "SOCKETOPTIONS" },
130 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
131 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
132 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
133 { "maxprotocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set max protocol level", "MAXPROTOCOL" },
137 struct poptOption popt_common_samba[] = {
138 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_callback },
139 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
140 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternative configuration file", "CONFIGFILE" },
141 { "option", 0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
142 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Basename for log/debug files", "LOGFILEBASE" },
143 { "leak-check", 0, POPT_ARG_NONE, NULL, OPT_LEAK_CHECK, "enable talloc leak checking", NULL },
147 struct poptOption popt_common_version[] = {
148 { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
149 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
155 /****************************************************************************
156 * get a password from a a file or file descriptor
158 * ****************************************************************************/
159 static void get_password_file(struct cmdline_auth_info *a)
163 BOOL close_it = False;
167 if ((p = getenv("PASSWD_FD")) != NULL) {
168 pstrcpy(spec, "descriptor ");
170 sscanf(p, "%d", &fd);
172 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
173 fd = sys_open(p, O_RDONLY, 0);
176 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
177 spec, strerror(errno));
183 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
184 p && p - pass < sizeof(pass);) {
185 switch (read(fd, p, 1)) {
187 if (*p != '\n' && *p != '\0') {
188 *++p = '\0'; /* advance p, and null-terminate pass */
193 *p = '\0'; /* null-terminate it, just in case... */
194 p = NULL; /* then force the loop condition to become false */
197 fprintf(stderr, "Error reading password from file %s: %s\n",
198 spec, "empty password\n");
203 fprintf(stderr, "Error reading password from file %s: %s\n",
204 spec, strerror(errno));
208 pstrcpy(a->password, pass);
213 static void get_credentials_file(const char *file, struct cmdline_auth_info *info)
218 char *ptr, *val, *param;
220 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
222 /* fail if we can't open the credentials file */
223 d_printf("ERROR: Unable to open credentials file!\n");
227 while (!x_feof(auth))
229 /* get a line from the file */
230 if (!x_fgets(buf, sizeof(buf), auth))
234 if ((len) && (buf[len-1]=='\n'))
242 /* break up the line into parameter & value.
243 * will need to eat a little whitespace possibly */
245 if (!(ptr = strchr_m (buf, '=')))
251 /* eat leading white space */
252 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
255 if (strwicmp("password", param) == 0)
257 pstrcpy(info->password, val);
258 info->got_pass = True;
260 else if (strwicmp("username", param) == 0)
261 pstrcpy(info->username, val);
262 else if (strwicmp("domain", param) == 0)
263 pstrcpy(info->domain,val);
264 memset(buf, 0, sizeof(buf));
269 /* Handle command line options:
271 * -A,--authentication-file
279 static void popt_common_credentials_callback(poptContext con,
280 enum poptCallbackReason reason,
281 const struct poptOption *opt,
282 const char *arg, const void *data)
286 if (reason == POPT_CALLBACK_REASON_PRE) {
287 cmdline_auth_info.use_kerberos = False;
288 cmdline_auth_info.got_pass = False;
289 pstrcpy(cmdline_auth_info.username, "GUEST");
291 if (getenv("LOGNAME"))pstrcpy(cmdline_auth_info.username,getenv("LOGNAME"));
293 if (getenv("USER")) {
296 pstrcpy(cmdline_auth_info.username,getenv("USER"));
298 pstrcpy(tmp,cmdline_auth_info.username);
299 if ((p = strchr_m(tmp,'\\'))) {
301 pstrcpy(cmdline_auth_info.domain,tmp);
302 pstrcpy(cmdline_auth_info.username,p+1);
305 if ((p = strchr_m(cmdline_auth_info.username,'%'))) {
307 pstrcpy(cmdline_auth_info.password,p+1);
308 cmdline_auth_info.got_pass = True;
309 memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(cmdline_auth_info.password));
313 if (getenv("DOMAIN")) {
314 pstrcpy(cmdline_auth_info.domain,getenv("DOMAIN"));
317 if (getenv("PASSWD")) {
318 pstrcpy(cmdline_auth_info.password,getenv("PASSWD"));
319 cmdline_auth_info.got_pass = True;
322 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
323 get_password_file(&cmdline_auth_info);
324 cmdline_auth_info.got_pass = True;
336 pstrcpy(cmdline_auth_info.username,arg);
338 pstrcpy(tmp,cmdline_auth_info.username);
339 if ((p = strchr_m(tmp,'\\'))) {
341 pstrcpy(cmdline_auth_info.domain,tmp);
342 pstrcpy(cmdline_auth_info.username,p+1);
345 if ((lp=strchr_m(cmdline_auth_info.username,'%'))) {
347 pstrcpy(cmdline_auth_info.password,lp+1);
348 cmdline_auth_info.got_pass = True;
349 memset(strchr_m(arg,'%')+1,'X',strlen(cmdline_auth_info.password));
355 get_credentials_file(arg, &cmdline_auth_info);
360 d_printf("No kerberos support compiled in\n");
363 cmdline_auth_info.use_kerberos = True;
364 cmdline_auth_info.got_pass = True;
369 lp_set_cmdline("client signing", arg);
374 char *opt_password = NULL;
375 /* it is very useful to be able to make ads queries as the
376 machine account for testing purposes and for domain leave */
378 if (!secrets_init()) {
379 d_printf("ERROR: Unable to open secrets database\n");
383 opt_password = secrets_fetch_machine_password(lp_workgroup());
386 d_printf("ERROR: Unable to fetch machine password\n");
389 pstr_sprintf(cmdline_auth_info.username, "%s$",
391 pstrcpy(cmdline_auth_info.password,opt_password);
392 SAFE_FREE(opt_password);
394 pstrcpy(cmdline_auth_info.password, lp_workgroup());
396 /* machine accounts only work with kerberos */
397 cmdline_auth_info.use_kerberos = True;
398 cmdline_auth_info.got_pass = True;
406 struct poptOption popt_common_credentials[] = {
407 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_credentials_callback },
408 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
409 { "no-pass", 'N', POPT_ARG_NONE, &cmdline_auth_info.got_pass, 0, "Don't ask for a password" },
410 { "kerberos", 'k', POPT_ARG_NONE, &cmdline_auth_info.use_kerberos, 'k', "Use kerberos (active directory) authentication" },
411 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
412 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
413 { "machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
417 void cmdline_set_username(const char *name)
419 pstrcpy(cmdline_auth_info.username, name);
422 const char *cmdline_get_username(void)
424 return cmdline_auth_info.username;
427 void cmdline_set_userdomain(const char *domain)
429 pstrcpy(cmdline_auth_info.domain, domain);
432 const char *cmdline_get_userdomain(void)
434 if (cmdline_auth_info.domain[0]) {
435 return cmdline_auth_info.domain;
438 /* I think this should be lp_netbios_name()
439 * instead of lp_workgroup(), because if you're logged in
440 * as domain user the getenv("USER") contains the domain
441 * and this code path isn't used
444 return lp_netbios_name();
447 const char *cmdline_get_userpassword(void)
451 if (cmdline_auth_info.got_pass) {
452 return cmdline_auth_info.password;
455 pstr_sprintf(prompt, "Password for [%s\\%s]:",
456 cmdline_get_userdomain(),
457 cmdline_get_username());
459 return getpass(prompt);
462 void cmdline_set_userpassword(const char *pass)
464 cmdline_auth_info.got_pass = True;
465 pstrcpy(cmdline_auth_info.password, pass);
468 void cmdline_set_use_kerberos(BOOL use_kerberos)
470 cmdline_auth_info.use_kerberos = use_kerberos;
473 BOOL cmdline_get_use_kerberos(void)
475 return cmdline_auth_info.use_kerberos;