2 Unix SMB/CIFS implementation.
5 Copyright (C) Tim Potter 2001,2002
6 Copyright (C) Jelmer Vernooij 2002,2003
7 Copyright (C) James Peach 2006
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
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 set_logfile(poptContext con, const char * arg)
48 /* Find out basename of current program */
49 pname = strrchr_m(poptGetInvocationName(con),'/');
52 pname = poptGetInvocationName(con);
56 pstr_sprintf(logfile, "%s/log.%s", arg, pname);
57 lp_set_logfile(logfile);
60 static BOOL PrintSambaVersionString;
62 static void popt_common_callback(poptContext con,
63 enum poptCallbackReason reason,
64 const struct poptOption *opt,
65 const char *arg, const void *data)
68 if (reason == POPT_CALLBACK_REASON_PRE) {
69 set_logfile(con, dyn_LOGFILEBASE);
73 if (reason == POPT_CALLBACK_REASON_POST) {
74 if (!PrintSambaVersionString) return;
76 printf( "Version %s\n", SAMBA_VERSION_STRING);
84 debug_parse_levels(arg);
85 AllowDebugChange = False;
90 PrintSambaVersionString = True;
95 pstrcpy(user_socket_options,arg);
101 pstrcpy(dyn_CONFIGFILE, arg);
107 set_global_myname(arg);
113 set_logfile(con, arg);
114 override_logfile = True;
115 pstr_sprintf(dyn_LOGFILEBASE, "%s", arg);
121 set_global_scope(arg);
127 set_global_myworkgroup(arg);
133 struct poptOption popt_common_connection[] = {
134 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
135 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
137 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
138 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
139 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
144 struct poptOption popt_common_samba[] = {
145 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
146 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
147 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
148 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Base name for log files", "LOGFILEBASE" },
149 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
153 struct poptOption popt_common_version[] = {
154 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
155 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
160 /* Handle command line options:
187 static void popt_dynconfig_callback(poptContext con,
188 enum poptCallbackReason reason,
189 const struct poptOption *opt,
190 const char *arg, const void *data)
196 dyn_SBINDIR = SMB_STRDUP(arg);
202 dyn_BINDIR = SMB_STRDUP(arg);
208 dyn_SWATDIR = SMB_STRDUP(arg);
212 case DYN_LMHOSTSFILE:
214 pstrcpy(dyn_LMHOSTSFILE, arg);
220 pstrcpy(dyn_LIBDIR, arg);
226 fstrcpy(dyn_SHLIBEXT, arg);
232 pstrcpy(dyn_LOCKDIR, arg);
238 pstrcpy(dyn_PIDDIR, arg);
242 case DYN_SMB_PASSWD_FILE:
244 pstrcpy(dyn_SMB_PASSWD_FILE, arg);
248 case DYN_PRIVATE_DIR:
250 pstrcpy(dyn_PRIVATE_DIR, arg);
257 const struct poptOption popt_common_dynconfig[] = {
259 { NULL, '\0', POPT_ARG_CALLBACK, (void *)popt_dynconfig_callback },
261 { "sbindir", '\0' , POPT_ARG_STRING, NULL, DYN_SBINDIR,
262 "Path to sbin directory", "SBINDIR" },
263 { "bindir", '\0' , POPT_ARG_STRING, NULL, DYN_BINDIR,
264 "Path to bin directory", "BINDIR" },
265 { "swatdir", '\0' , POPT_ARG_STRING, NULL, DYN_SWATDIR,
266 "Path to SWAT installation directory", "SWATDIR" },
267 { "lmhostsfile", '\0' , POPT_ARG_STRING, NULL, DYN_LMHOSTSFILE,
268 "Path to lmhosts file", "LMHOSTSFILE" },
269 { "libdir", '\0' , POPT_ARG_STRING, NULL, DYN_LIBDIR,
270 "Path to shared library directory", "LIBDIR" },
271 { "shlibext", '\0' , POPT_ARG_STRING, NULL, DYN_SHLIBEXT,
272 "Shared library extension", "SHLIBEXT" },
273 { "lockdir", '\0' , POPT_ARG_STRING, NULL, DYN_LOCKDIR,
274 "Path to lock file directory", "LOCKDIR" },
275 { "piddir", '\0' , POPT_ARG_STRING, NULL, DYN_PIDDIR,
276 "Path to PID file directory", "PIDDIR" },
277 { "smb-passwd-file", '\0' , POPT_ARG_STRING, NULL, DYN_SMB_PASSWD_FILE,
278 "Path to smbpasswd file", "SMB_PASSWD_FILE" },
279 { "private-dir", '\0' , POPT_ARG_STRING, NULL, DYN_PRIVATE_DIR,
280 "Path to private data directory", "PRIVATE_DIR" },
285 /****************************************************************************
286 * get a password from a a file or file descriptor
288 * ****************************************************************************/
289 static void get_password_file(struct user_auth_info *a)
293 BOOL close_it = False;
297 if ((p = getenv("PASSWD_FD")) != NULL) {
298 pstrcpy(spec, "descriptor ");
300 sscanf(p, "%d", &fd);
302 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
303 fd = sys_open(p, O_RDONLY, 0);
306 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
307 spec, strerror(errno));
313 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
314 p && p - pass < sizeof(pass);) {
315 switch (read(fd, p, 1)) {
317 if (*p != '\n' && *p != '\0') {
318 *++p = '\0'; /* advance p, and null-terminate pass */
323 *p = '\0'; /* null-terminate it, just in case... */
324 p = NULL; /* then force the loop condition to become false */
327 fprintf(stderr, "Error reading password from file %s: %s\n",
328 spec, "empty password\n");
333 fprintf(stderr, "Error reading password from file %s: %s\n",
334 spec, strerror(errno));
338 pstrcpy(a->password, pass);
343 static void get_credentials_file(const char *file, struct user_auth_info *info)
348 char *ptr, *val, *param;
350 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
352 /* fail if we can't open the credentials file */
353 d_printf("ERROR: Unable to open credentials file!\n");
357 while (!x_feof(auth))
359 /* get a line from the file */
360 if (!x_fgets(buf, sizeof(buf), auth))
364 if ((len) && (buf[len-1]=='\n'))
372 /* break up the line into parameter & value.
373 * will need to eat a little whitespace possibly */
375 if (!(ptr = strchr_m (buf, '=')))
381 /* eat leading white space */
382 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
385 if (strwicmp("password", param) == 0)
387 pstrcpy(info->password, val);
388 info->got_pass = True;
390 else if (strwicmp("username", param) == 0)
391 pstrcpy(info->username, val);
392 else if (strwicmp("domain", param) == 0)
393 set_global_myworkgroup(val);
394 memset(buf, 0, sizeof(buf));
399 /* Handle command line options:
401 * -A,--authentication-file
409 static void popt_common_credentials_callback(poptContext con,
410 enum poptCallbackReason reason,
411 const struct poptOption *opt,
412 const char *arg, const void *data)
416 if (reason == POPT_CALLBACK_REASON_PRE) {
417 cmdline_auth_info.use_kerberos = False;
418 cmdline_auth_info.got_pass = False;
419 cmdline_auth_info.signing_state = Undefined;
420 pstrcpy(cmdline_auth_info.username, "GUEST");
422 if (getenv("LOGNAME"))pstrcpy(cmdline_auth_info.username,getenv("LOGNAME"));
424 if (getenv("USER")) {
425 pstrcpy(cmdline_auth_info.username,getenv("USER"));
427 if ((p = strchr_m(cmdline_auth_info.username,'%'))) {
429 pstrcpy(cmdline_auth_info.password,p+1);
430 cmdline_auth_info.got_pass = True;
431 memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(cmdline_auth_info.password));
435 if (getenv("PASSWD")) {
436 pstrcpy(cmdline_auth_info.password,getenv("PASSWD"));
437 cmdline_auth_info.got_pass = True;
440 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
441 get_password_file(&cmdline_auth_info);
442 cmdline_auth_info.got_pass = True;
453 pstrcpy(cmdline_auth_info.username,arg);
454 if ((lp=strchr_m(cmdline_auth_info.username,'%'))) {
456 pstrcpy(cmdline_auth_info.password,lp+1);
457 cmdline_auth_info.got_pass = True;
458 memset(strchr_m(arg,'%')+1,'X',strlen(cmdline_auth_info.password));
464 get_credentials_file(arg, &cmdline_auth_info);
469 d_printf("No kerberos support compiled in\n");
472 cmdline_auth_info.use_kerberos = True;
473 cmdline_auth_info.got_pass = True;
479 cmdline_auth_info.signing_state = -1;
480 if (strequal(arg, "off") || strequal(arg, "no") || strequal(arg, "false"))
481 cmdline_auth_info.signing_state = False;
482 else if (strequal(arg, "on") || strequal(arg, "yes") || strequal(arg, "true") ||
483 strequal(arg, "auto") )
484 cmdline_auth_info.signing_state = True;
485 else if (strequal(arg, "force") || strequal(arg, "required") || strequal(arg, "forced"))
486 cmdline_auth_info.signing_state = Required;
488 fprintf(stderr, "Unknown signing option %s\n", arg );
495 char *opt_password = NULL;
496 /* it is very useful to be able to make ads queries as the
497 machine account for testing purposes and for domain leave */
499 if (!secrets_init()) {
500 d_printf("ERROR: Unable to open secrets database\n");
504 opt_password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
507 d_printf("ERROR: Unable to fetch machine password\n");
510 pstr_sprintf(cmdline_auth_info.username, "%s$",
512 pstrcpy(cmdline_auth_info.password,opt_password);
513 SAFE_FREE(opt_password);
515 /* machine accounts only work with kerberos */
516 cmdline_auth_info.use_kerberos = True;
517 cmdline_auth_info.got_pass = True;
525 struct poptOption popt_common_credentials[] = {
526 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, (void *)popt_common_credentials_callback },
527 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
528 { "no-pass", 'N', POPT_ARG_NONE, &cmdline_auth_info.got_pass, 0, "Don't ask for a password" },
529 { "kerberos", 'k', POPT_ARG_NONE, &cmdline_auth_info.use_kerberos, 'k', "Use kerberos (active directory) authentication" },
530 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
531 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
532 {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },