swat: Remove swat.
[Samba/bjacke.git] / source3 / lib / popt_common.c
blobeb3e2a455c49a34a838b830b6dd857c37c8c50f2
1 /*
2 Unix SMB/CIFS implementation.
3 Common popt routines
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/>.
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "popt_common.h"
27 /* Handle command line options:
28 * -d,--debuglevel
29 * -s,--configfile
30 * -O,--socket-options
31 * -V,--version
32 * -l,--log-base
33 * -n,--netbios-name
34 * -W,--workgroup
35 * -i,--scope
38 enum {OPT_OPTION=1};
40 extern bool override_logfile;
42 static void set_logfile(poptContext con, const char * arg)
45 char *lfile = NULL;
46 const char *pname;
48 /* Find out basename of current program */
49 pname = strrchr_m(poptGetInvocationName(con),'/');
51 if (!pname)
52 pname = poptGetInvocationName(con);
53 else
54 pname++;
56 if (asprintf(&lfile, "%s/log.%s", arg, pname) < 0) {
57 return;
59 lp_set_logfile(lfile);
60 SAFE_FREE(lfile);
63 static bool PrintSambaVersionString;
65 static void popt_s3_talloc_log_fn(const char *message)
67 DEBUG(0,("%s", message));
70 static void popt_common_callback(poptContext con,
71 enum poptCallbackReason reason,
72 const struct poptOption *opt,
73 const char *arg, const void *data)
76 if (reason == POPT_CALLBACK_REASON_PRE) {
77 set_logfile(con, get_dyn_LOGFILEBASE());
78 talloc_set_log_fn(popt_s3_talloc_log_fn);
79 talloc_set_abort_fn(smb_panic);
80 return;
83 if (reason == POPT_CALLBACK_REASON_POST) {
85 if (PrintSambaVersionString) {
86 printf( "Version %s\n", samba_version_string());
87 exit(0);
90 if (is_default_dyn_CONFIGFILE()) {
91 if(getenv("SMB_CONF_PATH")) {
92 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
96 /* Further 'every Samba program must do this' hooks here. */
97 return;
100 switch(opt->val) {
101 case OPT_OPTION:
102 if (!lp_set_option(arg)) {
103 fprintf(stderr, "Error setting option '%s'\n", arg);
104 exit(1);
106 break;
108 case 'd':
109 if (arg) {
110 lp_set_cmdline("log level", arg);
112 break;
114 case 'V':
115 PrintSambaVersionString = True;
116 break;
118 case 'O':
119 if (arg) {
120 lp_do_parameter(-1, "socket options", arg);
122 break;
124 case 's':
125 if (arg) {
126 set_dyn_CONFIGFILE(arg);
128 break;
130 case 'n':
131 if (arg) {
132 lp_set_cmdline("netbios name", arg);
134 break;
136 case 'l':
137 if (arg) {
138 set_logfile(con, arg);
139 override_logfile = True;
140 set_dyn_LOGFILEBASE(arg);
142 break;
144 case 'i':
145 if (arg) {
146 lp_set_cmdline("netbios scope", arg);
148 break;
150 case 'W':
151 if (arg) {
152 lp_set_cmdline("workgroup", arg);
154 break;
158 struct poptOption popt_common_connection[] = {
159 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
160 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
161 "SOCKETOPTIONS" },
162 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
163 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
164 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
166 POPT_TABLEEND
169 struct poptOption popt_common_samba[] = {
170 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
171 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
172 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
173 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Base name for log files", "LOGFILEBASE" },
174 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
175 { "option", 0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
176 POPT_TABLEEND
179 struct poptOption popt_common_configfile[] = {
180 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
181 { "configfile", 0, POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
182 POPT_TABLEEND
185 struct poptOption popt_common_version[] = {
186 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_POST, (void *)popt_common_callback },
187 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
188 POPT_TABLEEND
191 struct poptOption popt_common_debuglevel[] = {
192 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
193 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
194 POPT_TABLEEND
197 struct poptOption popt_common_option[] = {
198 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_POST, (void *)popt_common_callback },
199 { "option", 0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
200 POPT_TABLEEND
203 /* Handle command line options:
204 * --sbindir
205 * --bindir
206 * --lmhostsfile
207 * --libdir
208 * --modulesdir
209 * --shlibext
210 * --lockdir
211 * --statedir
212 * --cachedir
213 * --piddir
214 * --smb-passwd-file
215 * --private-dir
218 enum dyn_item{
219 DYN_SBINDIR = 1,
220 DYN_BINDIR,
221 DYN_LMHOSTSFILE,
222 DYN_LIBDIR,
223 DYN_MODULESDIR,
224 DYN_SHLIBEXT,
225 DYN_LOCKDIR,
226 DYN_STATEDIR,
227 DYN_CACHEDIR,
228 DYN_PIDDIR,
229 DYN_SMB_PASSWD_FILE,
230 DYN_PRIVATE_DIR,
234 static void popt_dynconfig_callback(poptContext con,
235 enum poptCallbackReason reason,
236 const struct poptOption *opt,
237 const char *arg, const void *data)
240 switch (opt->val) {
241 case DYN_SBINDIR:
242 if (arg) {
243 set_dyn_SBINDIR(arg);
245 break;
247 case DYN_BINDIR:
248 if (arg) {
249 set_dyn_BINDIR(arg);
251 break;
253 case DYN_LMHOSTSFILE:
254 if (arg) {
255 set_dyn_LMHOSTSFILE(arg);
257 break;
259 case DYN_LIBDIR:
260 if (arg) {
261 set_dyn_LIBDIR(arg);
263 break;
265 case DYN_MODULESDIR:
266 if (arg) {
267 set_dyn_MODULESDIR(arg);
269 break;
271 case DYN_SHLIBEXT:
272 if (arg) {
273 set_dyn_SHLIBEXT(arg);
275 break;
277 case DYN_LOCKDIR:
278 if (arg) {
279 set_dyn_LOCKDIR(arg);
281 break;
283 case DYN_STATEDIR:
284 if (arg) {
285 set_dyn_STATEDIR(arg);
287 break;
289 case DYN_CACHEDIR:
290 if (arg) {
291 set_dyn_CACHEDIR(arg);
293 break;
295 case DYN_PIDDIR:
296 if (arg) {
297 set_dyn_PIDDIR(arg);
299 break;
301 case DYN_SMB_PASSWD_FILE:
302 if (arg) {
303 set_dyn_SMB_PASSWD_FILE(arg);
305 break;
307 case DYN_PRIVATE_DIR:
308 if (arg) {
309 set_dyn_PRIVATE_DIR(arg);
311 break;
316 const struct poptOption popt_common_dynconfig[] = {
318 { NULL, '\0', POPT_ARG_CALLBACK, (void *)popt_dynconfig_callback },
320 { "sbindir", '\0' , POPT_ARG_STRING, NULL, DYN_SBINDIR,
321 "Path to sbin directory", "SBINDIR" },
322 { "bindir", '\0' , POPT_ARG_STRING, NULL, DYN_BINDIR,
323 "Path to bin directory", "BINDIR" },
324 { "lmhostsfile", '\0' , POPT_ARG_STRING, NULL, DYN_LMHOSTSFILE,
325 "Path to lmhosts file", "LMHOSTSFILE" },
326 { "libdir", '\0' , POPT_ARG_STRING, NULL, DYN_LIBDIR,
327 "Path to shared library directory", "LIBDIR" },
328 { "modulesdir", '\0' , POPT_ARG_STRING, NULL, DYN_MODULESDIR,
329 "Path to shared modules directory", "MODULESDIR" },
330 { "shlibext", '\0' , POPT_ARG_STRING, NULL, DYN_SHLIBEXT,
331 "Shared library extension", "SHLIBEXT" },
332 { "lockdir", '\0' , POPT_ARG_STRING, NULL, DYN_LOCKDIR,
333 "Path to lock file directory", "LOCKDIR" },
334 { "statedir", '\0' , POPT_ARG_STRING, NULL, DYN_STATEDIR,
335 "Path to persistent state file directory", "STATEDIR" },
336 { "cachedir", '\0' , POPT_ARG_STRING, NULL, DYN_CACHEDIR,
337 "Path to temporary cache file directory", "CACHEDIR" },
338 { "piddir", '\0' , POPT_ARG_STRING, NULL, DYN_PIDDIR,
339 "Path to PID file directory", "PIDDIR" },
340 { "smb-passwd-file", '\0' , POPT_ARG_STRING, NULL, DYN_SMB_PASSWD_FILE,
341 "Path to smbpasswd file", "SMB_PASSWD_FILE" },
342 { "private-dir", '\0' , POPT_ARG_STRING, NULL, DYN_PRIVATE_DIR,
343 "Path to private data directory", "PRIVATE_DIR" },
345 POPT_TABLEEND
348 /****************************************************************************
349 * get a password from a a file or file descriptor
350 * exit on failure
351 * ****************************************************************************/
353 static void get_password_file(struct user_auth_info *auth_info)
355 int fd = -1;
356 char *p;
357 bool close_it = False;
358 char *spec = NULL;
359 char pass[128];
361 if ((p = getenv("PASSWD_FD")) != NULL) {
362 if (asprintf(&spec, "descriptor %s", p) < 0) {
363 return;
365 sscanf(p, "%d", &fd);
366 close_it = false;
367 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
368 fd = open(p, O_RDONLY, 0);
369 spec = SMB_STRDUP(p);
370 if (fd < 0) {
371 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
372 spec, strerror(errno));
373 exit(1);
375 close_it = True;
378 if (fd < 0) {
379 fprintf(stderr, "fd = %d, < 0\n", fd);
380 exit(1);
383 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
384 p && p - pass < sizeof(pass);) {
385 switch (read(fd, p, 1)) {
386 case 1:
387 if (*p != '\n' && *p != '\0') {
388 *++p = '\0'; /* advance p, and null-terminate pass */
389 break;
391 case 0:
392 if (p - pass) {
393 *p = '\0'; /* null-terminate it, just in case... */
394 p = NULL; /* then force the loop condition to become false */
395 break;
396 } else {
397 fprintf(stderr, "Error reading password from file %s: %s\n",
398 spec, "empty password\n");
399 SAFE_FREE(spec);
400 exit(1);
403 default:
404 fprintf(stderr, "Error reading password from file %s: %s\n",
405 spec, strerror(errno));
406 SAFE_FREE(spec);
407 exit(1);
410 SAFE_FREE(spec);
412 set_cmdline_auth_info_password(auth_info, pass);
413 if (close_it) {
414 close(fd);
418 static void get_credentials_file(struct user_auth_info *auth_info,
419 const char *file)
421 XFILE *auth;
422 fstring buf;
423 uint16 len = 0;
424 char *ptr, *val, *param;
426 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
428 /* fail if we can't open the credentials file */
429 d_printf("ERROR: Unable to open credentials file!\n");
430 exit(-1);
433 while (!x_feof(auth))
435 /* get a line from the file */
436 if (!x_fgets(buf, sizeof(buf), auth))
437 continue;
438 len = strlen(buf);
440 if ((len) && (buf[len-1]=='\n'))
442 buf[len-1] = '\0';
443 len--;
445 if (len == 0)
446 continue;
448 /* break up the line into parameter & value.
449 * will need to eat a little whitespace possibly */
450 param = buf;
451 if (!(ptr = strchr_m (buf, '=')))
452 continue;
454 val = ptr+1;
455 *ptr = '\0';
457 /* eat leading white space */
458 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
459 val++;
461 if (strwicmp("password", param) == 0) {
462 set_cmdline_auth_info_password(auth_info, val);
463 } else if (strwicmp("username", param) == 0) {
464 set_cmdline_auth_info_username(auth_info, val);
465 } else if (strwicmp("domain", param) == 0) {
466 set_cmdline_auth_info_domain(auth_info, val);
468 memset(buf, 0, sizeof(buf));
470 x_fclose(auth);
473 /* Handle command line options:
474 * -U,--user
475 * -A,--authentication-file
476 * -k,--use-kerberos
477 * -N,--no-pass
478 * -S,--signing
479 * -P --machine-pass
480 * -e --encrypt
481 * -C --use-ccache
485 static void popt_common_credentials_callback(poptContext con,
486 enum poptCallbackReason reason,
487 const struct poptOption *opt,
488 const char *arg, const void *data)
490 struct user_auth_info *auth_info = talloc_get_type_abort(
491 *((const char **)data), struct user_auth_info);
492 char *p;
494 if (reason == POPT_CALLBACK_REASON_PRE) {
495 set_cmdline_auth_info_username(auth_info, "GUEST");
497 if (getenv("LOGNAME")) {
498 set_cmdline_auth_info_username(auth_info,
499 getenv("LOGNAME"));
502 if (getenv("USER")) {
503 char *puser = SMB_STRDUP(getenv("USER"));
504 if (!puser) {
505 exit(ENOMEM);
507 set_cmdline_auth_info_username(auth_info, puser);
510 if (getenv("PASSWD")) {
511 set_cmdline_auth_info_password(auth_info,
512 getenv("PASSWD"));
515 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
516 get_password_file(auth_info);
519 return;
522 switch(opt->val) {
523 case 'U':
525 char *lp;
526 char *puser = SMB_STRDUP(arg);
528 if ((lp=strchr_m(puser,'%'))) {
529 size_t len;
530 *lp = '\0';
531 set_cmdline_auth_info_username(auth_info,
532 puser);
533 set_cmdline_auth_info_password(auth_info,
534 lp+1);
535 len = strlen(lp+1);
536 memset(lp + 1, '\0', len);
537 } else {
538 set_cmdline_auth_info_username(auth_info,
539 puser);
541 SAFE_FREE(puser);
543 break;
545 case 'A':
546 get_credentials_file(auth_info, arg);
547 break;
549 case 'k':
550 #ifndef HAVE_KRB5
551 d_printf("No kerberos support compiled in\n");
552 exit(1);
553 #else
554 set_cmdline_auth_info_use_krb5_ticket(auth_info);
555 #endif
556 break;
558 case 'S':
559 if (!set_cmdline_auth_info_signing_state(auth_info, arg)) {
560 fprintf(stderr, "Unknown signing option %s\n", arg );
561 exit(1);
563 break;
564 case 'P':
565 set_cmdline_auth_info_use_machine_account(auth_info);
566 break;
567 case 'N':
568 set_cmdline_auth_info_password(auth_info, "");
569 break;
570 case 'e':
571 set_cmdline_auth_info_smb_encrypt(auth_info);
572 break;
573 case 'C':
574 set_cmdline_auth_info_use_ccache(auth_info, true);
575 break;
576 case 'H':
577 set_cmdline_auth_info_use_pw_nt_hash(auth_info, true);
578 break;
582 static struct user_auth_info *global_auth_info;
584 void popt_common_set_auth_info(struct user_auth_info *auth_info)
586 global_auth_info = auth_info;
590 * @brief Burn the commandline password.
592 * This function removes the password from the command line so we
593 * don't leak the password e.g. in 'ps aux'.
595 * It should be called after processing the options and you should pass down
596 * argv from main().
598 * @param[in] argc The number of arguments.
600 * @param[in] argv[] The argument array we will find the array.
602 void popt_burn_cmdline_password(int argc, char *argv[])
604 bool found = false;
605 char *p = NULL;
606 int i, ulen = 0;
608 for (i = 0; i < argc; i++) {
609 p = argv[i];
610 if (strncmp(p, "-U", 2) == 0) {
611 ulen = 2;
612 found = true;
613 } else if (strncmp(p, "--user", 6) == 0) {
614 ulen = 6;
615 found = true;
618 if (found) {
619 if (p == NULL) {
620 return;
623 if (strlen(p) == ulen) {
624 continue;
627 p = strchr_m(p, '%');
628 if (p != NULL) {
629 memset(p, '\0', strlen(p));
631 found = false;
636 struct poptOption popt_common_credentials[] = {
637 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE,
638 (void *)popt_common_credentials_callback, 0,
639 (const char *)&global_auth_info },
640 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
641 { "no-pass", 'N', POPT_ARG_NONE, NULL, 'N', "Don't ask for a password" },
642 { "kerberos", 'k', POPT_ARG_NONE, NULL, 'k', "Use kerberos (active directory) authentication" },
643 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
644 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
645 {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
646 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },
647 {"use-ccache", 'C', POPT_ARG_NONE, NULL, 'C',
648 "Use the winbind ccache for authentication" },
649 {"pw-nt-hash", '\0', POPT_ARG_NONE, NULL, 'H',
650 "The supplied password is the NT hash" },
651 POPT_TABLEEND