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:
36 static struct cmdline_auth_info cmdline_auth_info;
38 static void popt_common_callback(poptContext con,
39 enum poptCallbackReason reason,
40 const struct poptOption *opt,
41 const char *arg, const void *data)
46 /* Find out basename of current program */
47 pname = strrchr_m(poptGetInvocationName(con),'/');
50 pname = poptGetInvocationName(con);
54 if (reason == POPT_CALLBACK_REASON_PRE) {
55 pstr_sprintf(logfile, "%s/log.%s", dyn_LOGFILEBASE, pname);
56 lp_set_cmdline("log file", logfile);
62 lp_set_cmdline("log level", arg);
66 printf( "Version %s\n", SAMBA_VERSION_STRING );
72 lp_set_cmdline("socket options", arg);
78 pstrcpy(dyn_CONFIGFILE, arg);
84 pstr_sprintf(logfile, "%s/log.%s", arg, pname);
85 lp_set_cmdline("log file", logfile);
90 lp_set_cmdline("workgroup", arg);
94 lp_set_cmdline("netbios name", arg);
98 lp_set_cmdline("netbios scope", arg);
102 lp_set_cmdline("max protocol", arg);
106 lp_set_cmdline("name resolve order", arg);
111 struct poptOption popt_common_connection[] = {
112 { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
113 { "name-resolve", 'R', POPT_ARG_STRING, NULL, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
114 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use", "SOCKETOPTIONS" },
115 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
116 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
117 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
118 { "maxprotocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set max protocol level", "MAXPROTOCOL" },
122 struct poptOption popt_common_samba[] = {
123 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_callback },
124 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
125 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternative configuration file", "CONFIGFILE" },
126 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Basename for log/debug files", "LOGFILEBASE" },
130 struct poptOption popt_common_version[] = {
131 { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
132 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
138 /****************************************************************************
139 * get a password from a a file or file descriptor
141 * ****************************************************************************/
142 static void get_password_file(struct cmdline_auth_info *a)
146 BOOL close_it = False;
150 if ((p = getenv("PASSWD_FD")) != NULL) {
151 pstrcpy(spec, "descriptor ");
153 sscanf(p, "%d", &fd);
155 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
156 fd = sys_open(p, O_RDONLY, 0);
159 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
160 spec, strerror(errno));
166 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
167 p && p - pass < sizeof(pass);) {
168 switch (read(fd, p, 1)) {
170 if (*p != '\n' && *p != '\0') {
171 *++p = '\0'; /* advance p, and null-terminate pass */
176 *p = '\0'; /* null-terminate it, just in case... */
177 p = NULL; /* then force the loop condition to become false */
180 fprintf(stderr, "Error reading password from file %s: %s\n",
181 spec, "empty password\n");
186 fprintf(stderr, "Error reading password from file %s: %s\n",
187 spec, strerror(errno));
191 pstrcpy(a->password, pass);
196 static void get_credentials_file(const char *file, struct cmdline_auth_info *info)
201 char *ptr, *val, *param;
203 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
205 /* fail if we can't open the credentials file */
206 d_printf("ERROR: Unable to open credentials file!\n");
210 while (!x_feof(auth))
212 /* get a line from the file */
213 if (!x_fgets(buf, sizeof(buf), auth))
217 if ((len) && (buf[len-1]=='\n'))
225 /* break up the line into parameter & value.
226 * will need to eat a little whitespace possibly */
228 if (!(ptr = strchr_m (buf, '=')))
234 /* eat leading white space */
235 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
238 if (strwicmp("password", param) == 0)
240 pstrcpy(info->password, val);
241 info->got_pass = True;
243 else if (strwicmp("username", param) == 0)
244 pstrcpy(info->username, val);
245 else if (strwicmp("domain", param) == 0)
246 pstrcpy(info->domain,val);
247 memset(buf, 0, sizeof(buf));
252 /* Handle command line options:
254 * -A,--authentication-file
262 static void popt_common_credentials_callback(poptContext con,
263 enum poptCallbackReason reason,
264 const struct poptOption *opt,
265 const char *arg, const void *data)
269 if (reason == POPT_CALLBACK_REASON_PRE) {
270 cmdline_auth_info.use_kerberos = False;
271 cmdline_auth_info.got_pass = False;
272 pstrcpy(cmdline_auth_info.username, "GUEST");
274 if (getenv("LOGNAME"))pstrcpy(cmdline_auth_info.username,getenv("LOGNAME"));
276 if (getenv("USER")) {
279 pstrcpy(cmdline_auth_info.username,getenv("USER"));
281 pstrcpy(tmp,cmdline_auth_info.username);
282 if ((p = strchr_m(tmp,'\\'))) {
284 pstrcpy(cmdline_auth_info.domain,tmp);
285 pstrcpy(cmdline_auth_info.username,p+1);
288 if ((p = strchr_m(cmdline_auth_info.username,'%'))) {
290 pstrcpy(cmdline_auth_info.password,p+1);
291 cmdline_auth_info.got_pass = True;
292 memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(cmdline_auth_info.password));
296 if (getenv("DOMAIN")) {
297 pstrcpy(cmdline_auth_info.domain,getenv("DOMAIN"));
300 if (getenv("PASSWD")) {
301 pstrcpy(cmdline_auth_info.password,getenv("PASSWD"));
302 cmdline_auth_info.got_pass = True;
305 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
306 get_password_file(&cmdline_auth_info);
307 cmdline_auth_info.got_pass = True;
319 pstrcpy(cmdline_auth_info.username,arg);
321 pstrcpy(tmp,cmdline_auth_info.username);
322 if ((p = strchr_m(tmp,'\\'))) {
324 pstrcpy(cmdline_auth_info.domain,tmp);
325 pstrcpy(cmdline_auth_info.username,p+1);
328 if ((lp=strchr_m(cmdline_auth_info.username,'%'))) {
330 pstrcpy(cmdline_auth_info.password,lp+1);
331 cmdline_auth_info.got_pass = True;
332 memset(strchr_m(arg,'%')+1,'X',strlen(cmdline_auth_info.password));
338 get_credentials_file(arg, &cmdline_auth_info);
343 d_printf("No kerberos support compiled in\n");
346 cmdline_auth_info.use_kerberos = True;
347 cmdline_auth_info.got_pass = True;
352 lp_set_cmdline("client signing", arg);
357 char *opt_password = NULL;
358 /* it is very useful to be able to make ads queries as the
359 machine account for testing purposes and for domain leave */
361 if (!secrets_init()) {
362 d_printf("ERROR: Unable to open secrets database\n");
366 opt_password = secrets_fetch_machine_password(lp_workgroup());
369 d_printf("ERROR: Unable to fetch machine password\n");
372 pstr_sprintf(cmdline_auth_info.username, "%s$",
374 pstrcpy(cmdline_auth_info.password,opt_password);
375 SAFE_FREE(opt_password);
377 pstrcpy(cmdline_auth_info.password, lp_workgroup());
379 /* machine accounts only work with kerberos */
380 cmdline_auth_info.use_kerberos = True;
381 cmdline_auth_info.got_pass = True;
389 struct poptOption popt_common_credentials[] = {
390 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_credentials_callback },
391 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
392 { "no-pass", 'N', POPT_ARG_NONE, &cmdline_auth_info.got_pass, True, "Don't ask for a password" },
393 { "kerberos", 'k', POPT_ARG_NONE, &cmdline_auth_info.use_kerberos, True, "Use kerberos (active directory) authentication" },
394 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
395 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
396 { "machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
400 const char *cmdline_get_username(void)
402 return cmdline_auth_info.username;
405 const char *cmdline_get_userdomain(void)
407 if (cmdline_auth_info.domain[0]) {
408 return cmdline_auth_info.domain;
411 /* I think this should be lp_netbios_name()
412 * instead of lp_workgroup(), because if you're logged in
413 * as domain user the getenv("USER") contains the domain
414 * and this code path isn't used
417 return lp_netbios_name();
420 const char *cmdline_get_userpassword(void)
424 if (cmdline_auth_info.got_pass) {
425 return cmdline_auth_info.password;
428 pstr_sprintf(prompt, "Password for [%s\\%s]:",
429 cmdline_get_userdomain(),
430 cmdline_get_username());
432 return getpass(prompt);