tevent: Add lib/tevent as include directory.
[Samba.git] / source3 / lib / popt_common.c
blob8ff2b8023bf2c405455648fc308e3c0aacc5ab7b
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 "popt_common.h"
26 /* Handle command line options:
27 * -d,--debuglevel
28 * -s,--configfile
29 * -O,--socket-options
30 * -V,--version
31 * -l,--log-base
32 * -n,--netbios-name
33 * -W,--workgroup
34 * -i,--scope
37 enum {OPT_OPTION=1};
39 extern bool override_logfile;
41 static void set_logfile(poptContext con, const char * arg)
44 char *lfile = NULL;
45 const char *pname;
47 /* Find out basename of current program */
48 pname = strrchr_m(poptGetInvocationName(con),'/');
50 if (!pname)
51 pname = poptGetInvocationName(con);
52 else
53 pname++;
55 if (asprintf(&lfile, "%s/log.%s", arg, pname) < 0) {
56 return;
58 lp_set_logfile(lfile);
59 SAFE_FREE(lfile);
62 static bool PrintSambaVersionString;
64 static void popt_s3_talloc_log_fn(const char *message)
66 DEBUG(0,("%s", message));
69 static void popt_common_callback(poptContext con,
70 enum poptCallbackReason reason,
71 const struct poptOption *opt,
72 const char *arg, const void *data)
75 if (reason == POPT_CALLBACK_REASON_PRE) {
76 set_logfile(con, get_dyn_LOGFILEBASE());
77 talloc_set_log_fn(popt_s3_talloc_log_fn);
78 talloc_set_abort_fn(smb_panic);
79 return;
82 if (reason == POPT_CALLBACK_REASON_POST) {
84 if (PrintSambaVersionString) {
85 printf( "Version %s\n", samba_version_string());
86 exit(0);
89 if (is_default_dyn_CONFIGFILE()) {
90 if(getenv("SMB_CONF_PATH")) {
91 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
95 /* Further 'every Samba program must do this' hooks here. */
96 return;
99 switch(opt->val) {
100 case OPT_OPTION:
101 if (!lp_set_option(arg)) {
102 fprintf(stderr, "Error setting option '%s'\n", arg);
103 exit(1);
105 break;
107 case 'd':
108 if (arg) {
109 lp_set_cmdline("log level", arg);
111 break;
113 case 'V':
114 PrintSambaVersionString = True;
115 break;
117 case 'O':
118 if (arg) {
119 lp_do_parameter(-1, "socket options", arg);
121 break;
123 case 's':
124 if (arg) {
125 set_dyn_CONFIGFILE(arg);
127 break;
129 case 'n':
130 if (arg) {
131 set_global_myname(arg);
133 break;
135 case 'l':
136 if (arg) {
137 set_logfile(con, arg);
138 override_logfile = True;
139 set_dyn_LOGFILEBASE(arg);
141 break;
143 case 'i':
144 if (arg) {
145 set_global_scope(arg);
147 break;
149 case 'W':
150 if (arg) {
151 set_global_myworkgroup(arg);
153 break;
157 struct poptOption popt_common_connection[] = {
158 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
159 { "socket-options", 'O', POPT_ARG_STRING, NULL, 'O', "socket options to use",
160 "SOCKETOPTIONS" },
161 { "netbiosname", 'n', POPT_ARG_STRING, NULL, 'n', "Primary netbios name", "NETBIOSNAME" },
162 { "workgroup", 'W', POPT_ARG_STRING, NULL, 'W', "Set the workgroup name", "WORKGROUP" },
163 { "scope", 'i', POPT_ARG_STRING, NULL, 'i', "Use this Netbios scope", "SCOPE" },
165 POPT_TABLEEND
168 struct poptOption popt_common_samba[] = {
169 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
170 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
171 { "configfile", 's', POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
172 { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Base name for log files", "LOGFILEBASE" },
173 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
174 { "option", 0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
175 POPT_TABLEEND
178 struct poptOption popt_common_configfile[] = {
179 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, (void *)popt_common_callback },
180 { "configfile", 0, POPT_ARG_STRING, NULL, 's', "Use alternate configuration file", "CONFIGFILE" },
181 POPT_TABLEEND
184 struct poptOption popt_common_version[] = {
185 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_POST, (void *)popt_common_callback },
186 { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
187 POPT_TABLEEND
190 struct poptOption popt_common_debuglevel[] = {
191 { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
192 { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
193 POPT_TABLEEND
196 struct poptOption popt_common_option[] = {
197 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_POST, (void *)popt_common_callback },
198 { "option", 0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
199 POPT_TABLEEND
202 /* Handle command line options:
203 * --sbindir
204 * --bindir
205 * --swatdir
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_SWATDIR,
222 DYN_LMHOSTSFILE,
223 DYN_LIBDIR,
224 DYN_MODULESDIR,
225 DYN_SHLIBEXT,
226 DYN_LOCKDIR,
227 DYN_STATEDIR,
228 DYN_CACHEDIR,
229 DYN_PIDDIR,
230 DYN_SMB_PASSWD_FILE,
231 DYN_PRIVATE_DIR,
235 static void popt_dynconfig_callback(poptContext con,
236 enum poptCallbackReason reason,
237 const struct poptOption *opt,
238 const char *arg, const void *data)
241 switch (opt->val) {
242 case DYN_SBINDIR:
243 if (arg) {
244 set_dyn_SBINDIR(arg);
246 break;
248 case DYN_BINDIR:
249 if (arg) {
250 set_dyn_BINDIR(arg);
252 break;
254 case DYN_SWATDIR:
255 if (arg) {
256 set_dyn_SWATDIR(arg);
258 break;
260 case DYN_LMHOSTSFILE:
261 if (arg) {
262 set_dyn_LMHOSTSFILE(arg);
264 break;
266 case DYN_LIBDIR:
267 if (arg) {
268 set_dyn_LIBDIR(arg);
270 break;
272 case DYN_MODULESDIR:
273 if (arg) {
274 set_dyn_MODULESDIR(arg);
276 break;
278 case DYN_SHLIBEXT:
279 if (arg) {
280 set_dyn_SHLIBEXT(arg);
282 break;
284 case DYN_LOCKDIR:
285 if (arg) {
286 set_dyn_LOCKDIR(arg);
288 break;
290 case DYN_STATEDIR:
291 if (arg) {
292 set_dyn_STATEDIR(arg);
294 break;
296 case DYN_CACHEDIR:
297 if (arg) {
298 set_dyn_CACHEDIR(arg);
300 break;
302 case DYN_PIDDIR:
303 if (arg) {
304 set_dyn_PIDDIR(arg);
306 break;
308 case DYN_SMB_PASSWD_FILE:
309 if (arg) {
310 set_dyn_SMB_PASSWD_FILE(arg);
312 break;
314 case DYN_PRIVATE_DIR:
315 if (arg) {
316 set_dyn_PRIVATE_DIR(arg);
318 break;
323 const struct poptOption popt_common_dynconfig[] = {
325 { NULL, '\0', POPT_ARG_CALLBACK, (void *)popt_dynconfig_callback },
327 { "sbindir", '\0' , POPT_ARG_STRING, NULL, DYN_SBINDIR,
328 "Path to sbin directory", "SBINDIR" },
329 { "bindir", '\0' , POPT_ARG_STRING, NULL, DYN_BINDIR,
330 "Path to bin directory", "BINDIR" },
331 { "swatdir", '\0' , POPT_ARG_STRING, NULL, DYN_SWATDIR,
332 "Path to SWAT installation directory", "SWATDIR" },
333 { "lmhostsfile", '\0' , POPT_ARG_STRING, NULL, DYN_LMHOSTSFILE,
334 "Path to lmhosts file", "LMHOSTSFILE" },
335 { "libdir", '\0' , POPT_ARG_STRING, NULL, DYN_LIBDIR,
336 "Path to shared library directory", "LIBDIR" },
337 { "modulesdir", '\0' , POPT_ARG_STRING, NULL, DYN_MODULESDIR,
338 "Path to shared modules directory", "MODULESDIR" },
339 { "shlibext", '\0' , POPT_ARG_STRING, NULL, DYN_SHLIBEXT,
340 "Shared library extension", "SHLIBEXT" },
341 { "lockdir", '\0' , POPT_ARG_STRING, NULL, DYN_LOCKDIR,
342 "Path to lock file directory", "LOCKDIR" },
343 { "statedir", '\0' , POPT_ARG_STRING, NULL, DYN_STATEDIR,
344 "Path to persistent state file directory", "STATEDIR" },
345 { "cachedir", '\0' , POPT_ARG_STRING, NULL, DYN_CACHEDIR,
346 "Path to temporary cache file directory", "CACHEDIR" },
347 { "piddir", '\0' , POPT_ARG_STRING, NULL, DYN_PIDDIR,
348 "Path to PID file directory", "PIDDIR" },
349 { "smb-passwd-file", '\0' , POPT_ARG_STRING, NULL, DYN_SMB_PASSWD_FILE,
350 "Path to smbpasswd file", "SMB_PASSWD_FILE" },
351 { "private-dir", '\0' , POPT_ARG_STRING, NULL, DYN_PRIVATE_DIR,
352 "Path to private data directory", "PRIVATE_DIR" },
354 POPT_TABLEEND
357 /****************************************************************************
358 * get a password from a a file or file descriptor
359 * exit on failure
360 * ****************************************************************************/
362 static void get_password_file(struct user_auth_info *auth_info)
364 int fd = -1;
365 char *p;
366 bool close_it = False;
367 char *spec = NULL;
368 char pass[128];
370 if ((p = getenv("PASSWD_FD")) != NULL) {
371 if (asprintf(&spec, "descriptor %s", p) < 0) {
372 return;
374 sscanf(p, "%d", &fd);
375 close_it = false;
376 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
377 fd = sys_open(p, O_RDONLY, 0);
378 spec = SMB_STRDUP(p);
379 if (fd < 0) {
380 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
381 spec, strerror(errno));
382 exit(1);
384 close_it = True;
387 if (fd < 0) {
388 fprintf(stderr, "fd = %d, < 0\n", fd);
389 exit(1);
392 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
393 p && p - pass < sizeof(pass);) {
394 switch (read(fd, p, 1)) {
395 case 1:
396 if (*p != '\n' && *p != '\0') {
397 *++p = '\0'; /* advance p, and null-terminate pass */
398 break;
400 case 0:
401 if (p - pass) {
402 *p = '\0'; /* null-terminate it, just in case... */
403 p = NULL; /* then force the loop condition to become false */
404 break;
405 } else {
406 fprintf(stderr, "Error reading password from file %s: %s\n",
407 spec, "empty password\n");
408 SAFE_FREE(spec);
409 exit(1);
412 default:
413 fprintf(stderr, "Error reading password from file %s: %s\n",
414 spec, strerror(errno));
415 SAFE_FREE(spec);
416 exit(1);
419 SAFE_FREE(spec);
421 set_cmdline_auth_info_password(auth_info, pass);
422 if (close_it) {
423 close(fd);
427 static void get_credentials_file(struct user_auth_info *auth_info,
428 const char *file)
430 XFILE *auth;
431 fstring buf;
432 uint16 len = 0;
433 char *ptr, *val, *param;
435 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
437 /* fail if we can't open the credentials file */
438 d_printf("ERROR: Unable to open credentials file!\n");
439 exit(-1);
442 while (!x_feof(auth))
444 /* get a line from the file */
445 if (!x_fgets(buf, sizeof(buf), auth))
446 continue;
447 len = strlen(buf);
449 if ((len) && (buf[len-1]=='\n'))
451 buf[len-1] = '\0';
452 len--;
454 if (len == 0)
455 continue;
457 /* break up the line into parameter & value.
458 * will need to eat a little whitespace possibly */
459 param = buf;
460 if (!(ptr = strchr_m (buf, '=')))
461 continue;
463 val = ptr+1;
464 *ptr = '\0';
466 /* eat leading white space */
467 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
468 val++;
470 if (strwicmp("password", param) == 0) {
471 set_cmdline_auth_info_password(auth_info, val);
472 } else if (strwicmp("username", param) == 0) {
473 set_cmdline_auth_info_username(auth_info, val);
474 } else if (strwicmp("domain", param) == 0) {
475 set_global_myworkgroup(val);
477 memset(buf, 0, sizeof(buf));
479 x_fclose(auth);
482 /* Handle command line options:
483 * -U,--user
484 * -A,--authentication-file
485 * -k,--use-kerberos
486 * -N,--no-pass
487 * -S,--signing
488 * -P --machine-pass
489 * -e --encrypt
490 * -C --use-ccache
494 static void popt_common_credentials_callback(poptContext con,
495 enum poptCallbackReason reason,
496 const struct poptOption *opt,
497 const char *arg, const void *data)
499 struct user_auth_info *auth_info = talloc_get_type_abort(
500 *((const char **)data), struct user_auth_info);
501 char *p;
503 if (reason == POPT_CALLBACK_REASON_PRE) {
504 set_cmdline_auth_info_username(auth_info, "GUEST");
506 if (getenv("LOGNAME")) {
507 set_cmdline_auth_info_username(auth_info,
508 getenv("LOGNAME"));
511 if (getenv("USER")) {
512 char *puser = SMB_STRDUP(getenv("USER"));
513 if (!puser) {
514 exit(ENOMEM);
516 set_cmdline_auth_info_username(auth_info, puser);
518 if ((p = strchr_m(puser,'%'))) {
519 size_t len;
520 *p = 0;
521 len = strlen(p+1);
522 set_cmdline_auth_info_password(auth_info, p+1);
523 memset(strchr_m(getenv("USER"),'%')+1,'X',len);
525 SAFE_FREE(puser);
528 if (getenv("PASSWD")) {
529 set_cmdline_auth_info_password(auth_info,
530 getenv("PASSWD"));
533 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
534 get_password_file(auth_info);
537 return;
540 switch(opt->val) {
541 case 'U':
543 char *lp;
544 char *puser = SMB_STRDUP(arg);
546 if ((lp=strchr_m(puser,'%'))) {
547 size_t len;
548 *lp = 0;
549 set_cmdline_auth_info_username(auth_info,
550 puser);
551 set_cmdline_auth_info_password(auth_info,
552 lp+1);
553 len = strlen(lp+1);
554 memset(strchr_m(arg,'%')+1,'X',len);
555 } else {
556 set_cmdline_auth_info_username(auth_info,
557 puser);
559 SAFE_FREE(puser);
561 break;
563 case 'A':
564 get_credentials_file(auth_info, arg);
565 break;
567 case 'k':
568 #ifndef HAVE_KRB5
569 d_printf("No kerberos support compiled in\n");
570 exit(1);
571 #else
572 set_cmdline_auth_info_use_krb5_ticket(auth_info);
573 #endif
574 break;
576 case 'S':
577 if (!set_cmdline_auth_info_signing_state(auth_info, arg)) {
578 fprintf(stderr, "Unknown signing option %s\n", arg );
579 exit(1);
581 break;
582 case 'P':
583 set_cmdline_auth_info_use_machine_account(auth_info);
584 break;
585 case 'N':
586 set_cmdline_auth_info_password(auth_info, "");
587 break;
588 case 'e':
589 set_cmdline_auth_info_smb_encrypt(auth_info);
590 break;
591 case 'C':
592 set_cmdline_auth_info_use_ccache(auth_info, true);
593 break;
597 static struct user_auth_info *global_auth_info;
599 void popt_common_set_auth_info(struct user_auth_info *auth_info)
601 global_auth_info = auth_info;
604 struct poptOption popt_common_credentials[] = {
605 { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE,
606 (void *)popt_common_credentials_callback, 0,
607 (const char *)&global_auth_info },
608 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
609 { "no-pass", 'N', POPT_ARG_NONE, NULL, 'N', "Don't ask for a password" },
610 { "kerberos", 'k', POPT_ARG_NONE, NULL, 'k', "Use kerberos (active directory) authentication" },
611 { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" },
612 { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" },
613 {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" },
614 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },
615 {"use-ccache", 'C', POPT_ARG_NONE, NULL, 'C',
616 "Use the winbind ccache for authentication" },
617 POPT_TABLEEND