Skip to content

Commit

Permalink
Add DNSDBQ_CONFIG_FILE environment variable which can specify a diffe…
Browse files Browse the repository at this point in the history
…rent configuration file to use (#6)

* Add DNSDBQ_CONFIG_FILE environment variable which can specify a different configuration file to use.
  • Loading branch information
djw1149 authored Apr 11, 2021
1 parent e6d05b3 commit 5adb982
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 33 deletions.
51 changes: 32 additions & 19 deletions dnsdbflex.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014-2020 by Farsight Security, Inc.
* Copyright (c) 2014-2021 by Farsight Security, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -446,23 +446,23 @@ help(void) {
printf("usage: %s [-cdFhjqsTUv46] \n",
program_name);
#if 0 /* disable output limit feature */
puts("\t[-l QUERY-LIMIT] [-L OUTPUT-LIMIT] [-A after] [-B before]\n"
puts("\t[-l QUERY-LIMIT] [-L OUTPUT-LIMIT] [-A AFTER] [-B BEFORE]\n"
#else
puts("\t[-l QUERY-LIMIT] [-A after] [-B before]\n"
puts("\t[-l QUERY-LIMIT] [-A AFTER] [-B BEFORE]\n"
#endif
"\t[-u system] [-O offset]\n"
"\t[-u SYSTEM] [-O OFFSET]\n"
"\t{\n"
"\t\t[--regex regex] |\n"
"\t\t[--glob glob]\n"
"\t\t[--regex REGEX] |\n"
"\t\t[--glob GLOB]\n"
"\t}\n"
"\t[--exclude glob|regex]\n"
"\t[--exclude GLOB|REGEX]\n"
#ifdef DETAILS_SUPPORTED
"\t[--mode terse|t|details|d]\n"
#else
"\t[--mode terse|t]\n"
#endif
"\t[-s rrnames|n|rdata|d]\n"
"\t[-t rrtype]\n"
"\t[-t RRTYPE]\n"
"for -A and -B, use absolute format YYYY-MM-DD[ HH:MM:SS],\n"
"\tor relative format %dw%dd%dh%dm%ds.\n"
"use -c to get complete (strict) time matching for -A and -B.\n"
Expand Down Expand Up @@ -589,21 +589,34 @@ parse_long(const char *in, long *out) {
*/
static void
read_configs(void) {
char *value;
const char * const *conf;
char *cf = NULL;

for (conf = conf_files; *conf != NULL; conf++) {
wordexp_t we;

wordexp(*conf, &we, WRDE_NOCMD);
cf = strdup(we.we_wordv[0]);
assert(cf != NULL);
wordfree(&we);
if (access(cf, R_OK) == 0) {
DEBUG(1, true, "conf found: '%s'\n", cf);
break;
value = getenv(env_config_file);
if (value != NULL) {
if (access(value, R_OK) == 0) {
DEBUG(1, true, "conf found via env variable: '%s'\n", value);
cf = strdup(value);
} else {
fprintf(stderr, "%s: Cannot read configuration file '%s' named in env variable: %s\n",
program_name, value, strerror(errno));
my_exit(1);
}
} else {
for (conf = conf_files; *conf != NULL; conf++) {
wordexp_t we;

wordexp(*conf, &we, WRDE_NOCMD);
cf = strdup(we.we_wordv[0]);
assert(cf != NULL);
wordfree(&we);
if (access(cf, R_OK) == 0) {
DEBUG(1, true, "conf found: '%s'\n", cf);
break;
}
DESTROY(cf);
}
DESTROY(cf);
}
if (cf != NULL) {
char *cmd, *line;
Expand Down
36 changes: 23 additions & 13 deletions dnsdbflex.man
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,24 @@ $ dnsdbflex... -B 2013-01-22
.Ic ~/.dnsdb-query.conf
or
.Ic /etc/dnsdb-query.conf :
configuration file which should contain:
configuration file which can specify the API key, etc. variables. The
first of these which is readable will be used, alone, in its
entirety. See the
.Ic DNSDBQ_CONFIG_FILE
environment variable which can specify a different configuration
file to use.
.Pp
For backwards compability,
.Ic ~/.isc-dnsdb-query.conf
and
.Ic /etc/isc-dnsdb-query.conf
are also valid, but deprecated.
.Pp
The variables which can be set in the configuration file are as
follows:
.Bl -tag -width ".Ev DNSDB_API_KEY , APIKEY"
.It Ev APIKEY
contains the user's API key (no default).
.It Ev DNSDB_API_KEY , APIKEY
contains the user's DNSDB apikey (no default).
.It Ev DNSDB_SERVER
contains the URL of the DNSDB API server (default is <\fI\%https://api.dnsdb.info\fP>),
and optionally the URI prefix for the database.
Expand All @@ -330,19 +344,15 @@ option described above. Can only be "dnsdb2". If unset,
.Nm dnsdbflex
will probe for any configured system.
.El
.Pp
For backwards compability,
.Ic ~/.isc-dnsdb-query.conf
and
.Ic /etc/isc-dnsdb-query.conf
are also valid, but deprecated.
.Sh ENVIRONMENT
The following environment variables affect the execution of
.Nm :
.Bl -tag -width ".Ev DNSDB_API_KEY , APIKEY"
.It Ev DNSDB_API_KEY , APIKEY
contains the user's API key. If DNSDB_API_KEY is not present, then APIKEY will
be used. If neither variable is present, the configuration file is consulted.
.Bl -tag -width ".Ev DNSDBQ_CONFIG_FILE"
.It Ev DNSDBQ_CONFIG_FILE
specifies the configuration file to use, overriding the internal search list.
.It Ev DNSDB_API_KEY
contains the user's apikey. The older APIKEY environment variable has
been retired, though it can still be used in the configuration file.
.It Ev DNSDB_SERVER
contains the URL of the DNSDB API server, and optionally a URI prefix to be
used. If not set, the configuration file is consulted.
Expand Down
3 changes: 2 additions & 1 deletion globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
#endif

EXTERN const char id_swclient[] INIT("dnsdbflex");
EXTERN const char id_version[] INIT("1.0.5");
EXTERN const char id_version[] INIT("1.0.6");
EXTERN const char *program_name INIT(NULL);
EXTERN const char jsonl_header[] INIT("Accept: application/x-ndjson");
EXTERN const char env_config_file[] INIT("DNSDBQ_CONFIG_FILE");
EXTERN const char status_noerror[] INIT("NOERROR");
EXTERN const char status_error[] INIT("ERROR");
EXTERN pdns_system_ct psys INIT(NULL);
Expand Down

0 comments on commit 5adb982

Please sign in to comment.