]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
etc hosts handling.
authorWouter Wijngaards <wouter@nlnetlabs.nl>
Mon, 11 Feb 2008 10:27:37 +0000 (10:27 +0000)
committerWouter Wijngaards <wouter@nlnetlabs.nl>
Mon, 11 Feb 2008 10:27:37 +0000 (10:27 +0000)
git-svn-id: file:///svn/unbound/trunk@944 be551aaa-1e26-0410-a405-d3ace91eadb9

doc/Changelog
doc/libunbound.3
libunbound/libunbound.c
libunbound/ubsyms.def
libunbound/unbound.h
testcode/asynclook.c
testdata/05-asynclook.tpkg

index 422eed29056fae9b5a18ee661785c49fd6d5950c..b3078769744fcc8c14d358c57418a381b19fb97d 100644 (file)
@@ -1,6 +1,7 @@
 11 February 2008: Wouter
        - changed library to use ub_ instead of ub_val_ as prefix.
        - statistics output text nice.
+       - etc/hosts handling.
 
 8 February 2008: Wouter
        - test program for multiple queries over a TCP channel.
index ce34100e58b54b1a05d455efc2794ae2bab420e9..dd9f97cacec4c249a5bbe071b76275514aa9c3a9 100644 (file)
@@ -19,6 +19,7 @@
 .B ub_ctx_config,
 .B ub_ctx_set_fwd,
 .B ub_ctx_resolvconf,
+.B ub_ctx_hosts,
 .B ub_ctx_add_ta,
 .B ub_ctx_add_ta_file,
 .B ub_ctx_trustedkeys,
@@ -54,6 +55,9 @@
 \fBub_ctx_resolvconf\fR(\fIstruct ub_ctx*\fR ctx, \fIchar*\fR fname);
 .LP
 \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);
 .LP
 \fIint\fR
@@ -131,6 +135,12 @@ The functions are discussed in turn below.
 .TP 
 .B ub_ctx_create
 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
 .B ub_ctx_delete
 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
 first resolve is done.
 .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
 ub_ctx_add_ta
 Add a trust anchor to the given context.
index 295b5288438d5db76f0bc9a17061f9d82d0dc2a2..0533fca5bd062def0f4be4202638ccf9fe10ba17 100644 (file)
@@ -819,3 +819,79 @@ ub_ctx_resolvconf(struct ub_ctx* ctx, char* fname)
        }
        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;
+}
index 94776a02e131ab9ada40cafa40df9a23f4e6f854..2b7b174fc910eb6657ff67b81dbb36609503d9fb 100644 (file)
@@ -1,19 +1,20 @@
-ub_val_ctx_create
-ub_val_ctx_delete
-ub_val_ctx_config
-ub_val_ctx_set_fwd
-ub_val_ctx_resolvconf
-ub_val_ctx_add_ta
-ub_val_ctx_add_ta_file
-ub_val_ctx_trustedkeys
-ub_val_ctx_debuglevel
-ub_val_ctx_async
-ub_val_poll
-ub_val_wait
-ub_val_fd
-ub_val_process
-ub_val_resolve
-ub_val_resolve_async
-ub_val_cancel
-ub_val_resolve_free
-ub_val_strerror
+ub_ctx_create
+ub_ctx_delete
+ub_ctx_config
+ub_ctx_set_fwd
+ub_ctx_resolvconf
+ub_ctx_hosts
+ub_ctx_add_ta
+ub_ctx_add_ta_file
+ub_ctx_trustedkeys
+ub_ctx_debuglevel
+ub_ctx_async
+ub_poll
+ub_wait
+ub_fd
+ub_process
+ub_resolve
+ub_resolve_async
+ub_cancel
+ub_resolve_free
+ub_strerror
index e1303eeaee5070b4d6ca3dbe24ceb7bd97c7d878..f5f8248ff9b1846c0b06ebd772af069d780a696a 100644 (file)
@@ -193,6 +193,8 @@ typedef void (*ub_callback_t)(void*, int, struct ub_result*);
 
 /**
  * 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.
  *     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);
 
+/**
+ * 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.
  * The trust anchor is a string, on one line, that holds a valid DNSKEY or
index b7925900a151b9c98c4493be2d2432cd79060f86..d119192dff636a822ad61d5cec439e96f238fd88 100644 (file)
@@ -72,6 +72,7 @@ void usage(char* argv[])
        printf("        -d : enable debug output\n");
        printf("        -f addr : use addr, forward to that server\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("        -t : use a resolver thread instead of forking a process\n");
        printf("        -x : perform extended threaded test\n");
@@ -351,7 +352,7 @@ int main(int argc, char** argv)
        if(argc == 1) {
                usage(argv);
        }
-       while( (c=getopt(argc, argv, "bcdf:hr:tx")) != -1) {
+       while( (c=getopt(argc, argv, "bcdf:hH:r:tx")) != -1) {
                switch(c) {
                        case 'd':
                                r = ub_ctx_debuglevel(ctx, 3);
@@ -377,6 +378,16 @@ int main(int argc, char** argv)
                                        return 1;
                                }
                                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':
                                r = ub_ctx_set_fwd(ctx, optarg);
                                checkerr("ub_ctx_set_fwd", r);
index 1fd0e332f66be0ff6d493a4c4d286e831e5a07e1..128328d657f4a945bcdce7aa26b11e4f75a39561 100644 (file)
Binary files a/testdata/05-asynclook.tpkg and b/testdata/05-asynclook.tpkg differ