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 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 popt_common_callback(poptContext con,
43 enum poptCallbackReason reason,
44 const struct poptOption *opt,
45 const char *arg, const void *data)
50 /* Find out basename of current program */
51 pname = strrchr_m(poptGetInvocationName(con),'/');
54 pname = poptGetInvocationName(con);
58 if (reason == POPT_CALLBACK_REASON_PRE) {
59 pstr_sprintf(logfile, "%s/log.%s", dyn_LOGFILEBASE, pname);
60 lp_set_logfile(logfile);
67 debug_parse_levels(arg);
68 AllowDebugChange = False;
73 printf( "Version %s\n", SAMBA_VERSION_STRING);
79 pstrcpy(user_socket_options,arg);
85 pstrcpy(dyn_CONFIGFILE, arg);
91 set_global_myname(arg);
97 pstr_sprintf(logfile, "%s/log.%s", arg, pname);
98 lp_set_logfile(logfile);
99 override_logfile = True;
105 set_global_scope(arg);
111 set_global_myworkgroup(arg);
117 struct poptOption popt_common_connection[] = {
118 { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
119 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
121 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
122 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
123 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
128 struct poptOption popt_common_samba[] = {
129 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_callback },
130 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
131 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternative configuration file", "CONFIGFILE" },
132 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Basename for log/debug files", "LOGFILEBASE" },
133 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
137 struct poptOption popt_common_version[] = {
138 { NULL, 0, POPT_ARG_CALLBACK, popt_common_callback },
139 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
145 /****************************************************************************
146 * get a password from a a file or file descriptor
148 * ****************************************************************************/
149 static void get_password_file(struct user_auth_info *a)
153 BOOL close_it = False;
157 if ((p = getenv("PASSWD_FD")) != NULL) {
158 pstrcpy(spec, "descriptor ");
160 sscanf(p, "%d", &fd);
162 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
163 fd = sys_open(p, O_RDONLY, 0);
166 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
167 spec, strerror(errno));
173 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
174 p && p - pass < sizeof(pass);) {
175 switch (read(fd, p, 1)) {
177 if (*p != '\n' && *p != '\0') {
178 *++p = '\0'; /* advance p, and null-terminate pass */
183 *p = '\0'; /* null-terminate it, just in case... */
184 p = NULL; /* then force the loop condition to become false */
187 fprintf(stderr, "Error reading password from file %s: %s\n",
188 spec, "empty password\n");
193 fprintf(stderr, "Error reading password from file %s: %s\n",
194 spec, strerror(errno));
198 pstrcpy(a->password, pass);
203 static void get_credentials_file(const char *file, struct user_auth_info *info)
208 char *ptr, *val, *param;
210 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
212 /* fail if we can't open the credentials file */
213 d_printf("ERROR: Unable to open credentials file!\n");
217 while (!x_feof(auth))
219 /* get a line from the file */
220 if (!x_fgets(buf, sizeof(buf), auth))
224 if ((len) && (buf[len-1]=='\n'))
232 /* break up the line into parameter & value.
233 * will need to eat a little whitespace possibly */
235 if (!(ptr = strchr_m (buf, '=')))
241 /* eat leading white space */
242 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
245 if (strwicmp("password", param) == 0)
247 pstrcpy(info->password, val);
248 info->got_pass = True;
250 else if (strwicmp("username", param) == 0)
251 pstrcpy(info->username, val);
252 else if (strwicmp("domain", param) == 0)
253 set_global_myworkgroup(val);
254 memset(buf, 0, sizeof(buf));
259 /* Handle command line options:
261 * -A,--authentication-file
269 static void popt_common_credentials_callback(poptContext con,
270 enum poptCallbackReason reason,
271 const struct poptOption *opt,
272 const char *arg, const void *data)
276 if (reason == POPT_CALLBACK_REASON_PRE) {
277 cmdline_auth_info.use_kerberos = False;
278 cmdline_auth_info.got_pass = False;
279 cmdline_auth_info.signing_state = Undefined;
280 pstrcpy(cmdline_auth_info.username, "GUEST");
282 if (getenv("LOGNAME"))pstrcpy(cmdline_auth_info.username,getenv("LOGNAME"));
284 if (getenv("USER")) {
285 pstrcpy(cmdline_auth_info.username,getenv("USER"));
287 if ((p = strchr_m(cmdline_auth_info.username,'%'))) {
289 pstrcpy(cmdline_auth_info.password,p+1);
290 cmdline_auth_info.got_pass = True;
291 memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(cmdline_auth_info.password));
295 if (getenv("PASSWD")) {
296 pstrcpy(cmdline_auth_info.password,getenv("PASSWD"));
297 cmdline_auth_info.got_pass = True;
300 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
301 get_password_file(&cmdline_auth_info);
302 cmdline_auth_info.got_pass = True;
313 pstrcpy(cmdline_auth_info.username,arg);
314 if ((lp=strchr_m(cmdline_auth_info.username,'%'))) {
316 pstrcpy(cmdline_auth_info.password,lp+1);
317 cmdline_auth_info.got_pass = True;
318 memset(strchr_m(arg,'%')+1,'X',strlen(cmdline_auth_info.password));
324 get_credentials_file(arg, &cmdline_auth_info);
329 d_printf("No kerberos support compiled in\n");
332 cmdline_auth_info.use_kerberos = True;
333 cmdline_auth_info.got_pass = True;
339 cmdline_auth_info.signing_state = -1;
340 if (strequal(arg, "off") || strequal(arg, "no") || strequal(arg, "false"))
341 cmdline_auth_info.signing_state = False;
342 else if (strequal(arg, "on") || strequal(arg, "yes") || strequal(arg, "true") ||
343 strequal(arg, "auto") )
344 cmdline_auth_info.signing_state = True;
345 else if (strequal(arg, "force") || strequal(arg, "required") || strequal(arg, "forced"))
346 cmdline_auth_info.signing_state = Required;
348 fprintf(stderr, "Unknown signing option %s\n", arg );
355 char *opt_password = NULL;
356 /* it is very useful to be able to make ads queries as the
357 machine account for testing purposes and for domain leave */
359 if (!secrets_init()) {
360 d_printf("ERROR: Unable to open secrets database\n");
364 opt_password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
367 d_printf("ERROR: Unable to fetch machine password\n");
370 pstr_sprintf(cmdline_auth_info.username, "%s$",
372 pstrcpy(cmdline_auth_info.password,opt_password);
373 SAFE_FREE(opt_password);
375 /* machine accounts only work with kerberos */
376 cmdline_auth_info.use_kerberos = True;
377 cmdline_auth_info.got_pass = True;
385 struct poptOption popt_common_credentials[] = {
386 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_credentials_callback },
387 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
388 { "no-pass", 'N', POPT_ARG_NONE, &cmdline_auth_info.got_pass, 0, "Don't ask for a password" },
389 { "kerberos", 'k', POPT_ARG_NONE, &cmdline_auth_info.use_kerberos, 'k', "Use kerberos (active directory) authentication" },
390 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
391 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
392 {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },