etc hosts handling.

git-svn-id: file:///svn/unbound/trunk@944 be551aaa-1e26-0410-a405-d3ace91eadb9
This commit is contained in:
Wouter Wijngaards 2008-02-11 10:27:37 +00:00
parent 1dd130be61
commit 49d73f5f56
7 changed files with 141 additions and 20 deletions

View file

@ -1,6 +1,7 @@
11 February 2008: Wouter 11 February 2008: Wouter
- changed library to use ub_ instead of ub_val_ as prefix. - changed library to use ub_ instead of ub_val_ as prefix.
- statistics output text nice. - statistics output text nice.
- etc/hosts handling.
8 February 2008: Wouter 8 February 2008: Wouter
- test program for multiple queries over a TCP channel. - test program for multiple queries over a TCP channel.

View file

@ -19,6 +19,7 @@
.B ub_ctx_config, .B ub_ctx_config,
.B ub_ctx_set_fwd, .B ub_ctx_set_fwd,
.B ub_ctx_resolvconf, .B ub_ctx_resolvconf,
.B ub_ctx_hosts,
.B ub_ctx_add_ta, .B ub_ctx_add_ta,
.B ub_ctx_add_ta_file, .B ub_ctx_add_ta_file,
.B ub_ctx_trustedkeys, .B ub_ctx_trustedkeys,
@ -54,6 +55,9 @@
\fBub_ctx_resolvconf\fR(\fIstruct ub_ctx*\fR ctx, \fIchar*\fR fname); \fBub_ctx_resolvconf\fR(\fIstruct ub_ctx*\fR ctx, \fIchar*\fR fname);
.LP .LP
\fIint\fR \fIint\fR
\fBub_ctx_hosts\fR(\fIstruct ub_ctx*\fR ctx, \fIchar*\fR fname);
.LP
\fIint\fR
\fBub_ctx_add_ta\fR(\fIstruct ub_ctx*\fR ctx, \fIchar*\fR ta); \fBub_ctx_add_ta\fR(\fIstruct ub_ctx*\fR ctx, \fIchar*\fR ta);
.LP .LP
\fIint\fR \fIint\fR
@ -131,6 +135,12 @@ The functions are discussed in turn below.
.TP .TP
.B ub_ctx_create .B ub_ctx_create
Create a new context, initialised with defaults. Create a new context, initialised with defaults.
The information from /etc/resolv.conf and /etc/hosts is not utilised
by default. Use
.B ub_ctx_resolvconf
and
.B ub_ctx_hosts
to read them.
.TP .TP
.B ub_ctx_delete .B ub_ctx_delete
Delete validation context and free associated resources. Delete validation context and free associated resources.
@ -161,6 +171,13 @@ If fname NULL is passed, "/etc/resolv.conf" is used.
At this time it is only possible to set configuration before the At this time it is only possible to set configuration before the
first resolve is done. first resolve is done.
.TP .TP
.B ub_ctx_hosts
Read list of hosts from the filename given.
Usually "/etc/hosts". When queried for, these addresses are not marked
DNSSEC secure. If fname NULL is passed, "/etc/hosts" is used.
At this time it is only possible to set configuration before the
first resolve is done.
.TP
.B .B
ub_ctx_add_ta ub_ctx_add_ta
Add a trust anchor to the given context. Add a trust anchor to the given context.

View file

@ -819,3 +819,79 @@ ub_ctx_resolvconf(struct ub_ctx* ctx, char* fname)
} }
return UB_NOERROR; return UB_NOERROR;
} }
int
ub_ctx_hosts(struct ub_ctx* ctx, char* fname)
{
FILE* in;
char buf[1024], ldata[1024];
char* parse, *addr, *name, *ins;
lock_basic_lock(&ctx->cfglock);
if(ctx->finalized) {
lock_basic_unlock(&ctx->cfglock);
errno=EINVAL;
return UB_AFTERFINAL;
}
lock_basic_unlock(&ctx->cfglock);
if(fname == NULL)
fname = "/etc/hosts";
in = fopen(fname, "r");
if(!in) {
/* error in errno! perror(fname) */
return UB_READFILE;
}
while(fgets(buf, (int)sizeof(buf), in)) {
buf[sizeof(buf)-1] = 0;
parse=buf;
while(*parse == ' ' || *parse == '\t')
parse++;
if(*parse == '#')
continue; /* skip comment */
/* format: <addr> spaces <name> spaces <name> ... */
addr = parse;
/* skip addr */
while(isxdigit(*parse) || *parse == '.' || *parse == ':')
parse++;
if(*parse != ' ' && *parse != '\t') {
/* must have whitespace after address */
fclose(in);
errno=EINVAL;
return UB_SYNTAX;
}
*parse++ = 0; /* end delimiter for addr ... */
/* go to names and add them */
while(*parse) {
while(*parse == ' ' || *parse == '\t' || *parse=='\n')
parse++;
if(*parse == 0 || *parse == '#')
break;
/* skip name, allows (too) many printable characters */
name = parse;
while('!' <= *parse && *parse <= '~')
parse++;
if(*parse)
*parse++ = 0; /* end delimiter for name */
snprintf(ldata, sizeof(ldata), "%s %s %s",
name, str_is_ip6(addr)?"AAAA":"A", addr);
ins = strdup(ldata);
if(!ins) {
/* out of memory */
fclose(in);
errno=ENOMEM;
return UB_NOMEM;
}
lock_basic_lock(&ctx->cfglock);
if(!cfg_strlist_insert(&ctx->env->cfg->local_data,
ins)) {
lock_basic_unlock(&ctx->cfglock);
fclose(in);
free(ins);
errno=ENOMEM;
return UB_NOMEM;
}
lock_basic_unlock(&ctx->cfglock);
}
}
fclose(in);
return UB_NOERROR;
}

View file

@ -1,19 +1,20 @@
ub_val_ctx_create ub_ctx_create
ub_val_ctx_delete ub_ctx_delete
ub_val_ctx_config ub_ctx_config
ub_val_ctx_set_fwd ub_ctx_set_fwd
ub_val_ctx_resolvconf ub_ctx_resolvconf
ub_val_ctx_add_ta ub_ctx_hosts
ub_val_ctx_add_ta_file ub_ctx_add_ta
ub_val_ctx_trustedkeys ub_ctx_add_ta_file
ub_val_ctx_debuglevel ub_ctx_trustedkeys
ub_val_ctx_async ub_ctx_debuglevel
ub_val_poll ub_ctx_async
ub_val_wait ub_poll
ub_val_fd ub_wait
ub_val_process ub_fd
ub_val_resolve ub_process
ub_val_resolve_async ub_resolve
ub_val_cancel ub_resolve_async
ub_val_resolve_free ub_cancel
ub_val_strerror ub_resolve_free
ub_strerror

View file

@ -193,6 +193,8 @@ typedef void (*ub_callback_t)(void*, int, struct ub_result*);
/** /**
* Create a resolving and validation context. * Create a resolving and validation context.
* The information from /etc/resolv.conf and /etc/hosts is not utilised by
* default. Use ub_ctx_resolvconf and ub_ctx_hosts to read them.
* @return a new context. default initialisation. * @return a new context. default initialisation.
* returns NULL on error. * returns NULL on error.
*/ */
@ -252,6 +254,19 @@ int ub_ctx_set_fwd(struct ub_ctx* ctx, char* addr);
*/ */
int ub_ctx_resolvconf(struct ub_ctx* ctx, char* fname); int ub_ctx_resolvconf(struct ub_ctx* ctx, char* fname);
/**
* Read list of hosts from the filename given.
* Usually "/etc/hosts".
* These addresses are not flagged as DNSSEC secure when queried for.
*
* @param ctx: context.
* At this time it is only possible to set configuration before the
* first resolve is done.
* @param fname: file name string. If NULL "/etc/hosts" is used.
* @return 0 if OK, else error.
*/
int ub_ctx_hosts(struct ub_ctx* ctx, char* fname);
/** /**
* Add a trust anchor to the given context. * Add a trust anchor to the given context.
* The trust anchor is a string, on one line, that holds a valid DNSKEY or * The trust anchor is a string, on one line, that holds a valid DNSKEY or

View file

@ -72,6 +72,7 @@ void usage(char* argv[])
printf(" -d : enable debug output\n"); printf(" -d : enable debug output\n");
printf(" -f addr : use addr, forward to that server\n"); printf(" -f addr : use addr, forward to that server\n");
printf(" -h : this help message\n"); printf(" -h : this help message\n");
printf(" -H fname : read hosts from fname\n");
printf(" -r fname : read resolv.conf from fname\n"); printf(" -r fname : read resolv.conf from fname\n");
printf(" -t : use a resolver thread instead of forking a process\n"); printf(" -t : use a resolver thread instead of forking a process\n");
printf(" -x : perform extended threaded test\n"); printf(" -x : perform extended threaded test\n");
@ -351,7 +352,7 @@ int main(int argc, char** argv)
if(argc == 1) { if(argc == 1) {
usage(argv); usage(argv);
} }
while( (c=getopt(argc, argv, "bcdf:hr:tx")) != -1) { while( (c=getopt(argc, argv, "bcdf:hH:r:tx")) != -1) {
switch(c) { switch(c) {
case 'd': case 'd':
r = ub_ctx_debuglevel(ctx, 3); r = ub_ctx_debuglevel(ctx, 3);
@ -377,6 +378,16 @@ int main(int argc, char** argv)
return 1; return 1;
} }
break; break;
case 'H':
r = ub_ctx_hosts(ctx, optarg);
if(r != 0) {
printf("ub_ctx_hosts "
"error: %s : %s\n",
ub_strerror(r),
strerror(errno));
return 1;
}
break;
case 'f': case 'f':
r = ub_ctx_set_fwd(ctx, optarg); r = ub_ctx_set_fwd(ctx, optarg);
checkerr("ub_ctx_set_fwd", r); checkerr("ub_ctx_set_fwd", r);

Binary file not shown.