summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>2002-02-13 19:22:41 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>2002-02-13 19:22:41 +0000
commit269ccd5ec2a6e2307ae99d898c6899b78b0ba0da (patch)
treee74a5e5bc26469c6ea7f8e537d2ab0ca7cbcd40b
parenta7e74053f365de72e8d1cd756cdf7601ba3d7428 (diff)
the same resolv.c needs to be in both krb and krb5, for static arch like vax
-rw-r--r--kerberosIV/src/lib/roken/resolve.c242
-rw-r--r--kerberosIV/src/lib/roken/resolve.h48
2 files changed, 268 insertions, 22 deletions
diff --git a/kerberosIV/src/lib/roken/resolve.c b/kerberosIV/src/lib/roken/resolve.c
index 933218f5d4b..d2f5fd5ff99 100644
--- a/kerberosIV/src/lib/roken/resolve.c
+++ b/kerberosIV/src/lib/roken/resolve.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska Högskolan
+ * Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
@@ -43,7 +43,9 @@
#endif
#include "resolve.h"
-RCSID("$KTH: resolve.c,v 1.22 1999/12/02 16:58:52 joda Exp $");
+#include <assert.h>
+
+RCSID("$KTH: resolve.c,v 1.28 2001/08/07 21:57:01 assar Exp $");
#if defined(HAVE_RES_SEARCH) && defined(HAVE_DN_EXPAND)
@@ -56,18 +58,22 @@ static struct stot{
DECL(A),
DECL(NS),
DECL(CNAME),
+ DECL(SOA),
DECL(PTR),
DECL(MX),
DECL(TXT),
DECL(AFSDB),
+ DECL(SIG),
+ DECL(KEY),
DECL(SRV),
+ DECL(NAPTR),
{NULL, 0}
};
-int _resolve_debug;
+int _resolve_debug = 0;
-static int
-string_to_type(const char *name)
+int
+dns_string_to_type(const char *name)
{
struct stot *p = stot;
for(p = stot; p->name; p++)
@@ -76,8 +82,8 @@ string_to_type(const char *name)
return -1;
}
-static const char *
-type_to_string(int type)
+const char *
+dns_type_to_string(int type)
{
struct stot *p = stot;
for(p = stot; p->name; p++)
@@ -235,7 +241,72 @@ parse_reply(unsigned char *data, int len)
(*rr)->u.txt[*p] = 0;
break;
}
-
+ case T_KEY : {
+ size_t key_len;
+
+ key_len = size - 4;
+ (*rr)->u.key = malloc (sizeof(*(*rr)->u.key) + key_len - 1);
+ if ((*rr)->u.key == NULL) {
+ dns_free_data (r);
+ return NULL;
+ }
+
+ (*rr)->u.key->flags = (p[0] << 8) | p[1];
+ (*rr)->u.key->protocol = p[2];
+ (*rr)->u.key->algorithm = p[3];
+ (*rr)->u.key->key_len = key_len;
+ memcpy ((*rr)->u.key->key_data, p + 4, key_len);
+ break;
+ }
+ case T_SIG : {
+ size_t sig_len;
+
+ status = dn_expand (data, data + len, p + 18, host, sizeof(host));
+ if (status < 0) {
+ dns_free_data (r);
+ return NULL;
+ }
+ sig_len = len - 18 - status;
+ (*rr)->u.sig = malloc(sizeof(*(*rr)->u.sig)
+ + strlen(host) + sig_len);
+ if ((*rr)->u.sig == NULL) {
+ dns_free_data (r);
+ return NULL;
+ }
+ (*rr)->u.sig->type = (p[0] << 8) | p[1];
+ (*rr)->u.sig->algorithm = p[2];
+ (*rr)->u.sig->labels = p[3];
+ (*rr)->u.sig->orig_ttl = (p[4] << 24) | (p[5] << 16)
+ | (p[6] << 8) | p[7];
+ (*rr)->u.sig->sig_expiration = (p[8] << 24) | (p[9] << 16)
+ | (p[10] << 8) | p[11];
+ (*rr)->u.sig->sig_inception = (p[12] << 24) | (p[13] << 16)
+ | (p[14] << 8) | p[15];
+ (*rr)->u.sig->key_tag = (p[16] << 8) | p[17];
+ (*rr)->u.sig->sig_len = sig_len;
+ memcpy ((*rr)->u.sig->sig_data, p + 18 + status, sig_len);
+ (*rr)->u.sig->signer = &(*rr)->u.sig->sig_data[sig_len];
+ strcpy((*rr)->u.sig->signer, host);
+ break;
+ }
+
+ case T_CERT : {
+ size_t cert_len;
+
+ cert_len = size - 5;
+ (*rr)->u.cert = malloc (sizeof(*(*rr)->u.cert) + cert_len - 1);
+ if ((*rr)->u.cert == NULL) {
+ dns_free_data (r);
+ return NULL;
+ }
+
+ (*rr)->u.cert->type = (p[0] << 8) | p[1];
+ (*rr)->u.cert->tag = (p[2] << 8) | p[3];
+ (*rr)->u.cert->algorithm = p[4];
+ (*rr)->u.cert->cert_len = cert_len;
+ memcpy ((*rr)->u.cert->cert_data, p + 5, cert_len);
+ break;
+ }
default:
(*rr)->u.data = (unsigned char*)malloc(size);
if(size != 0 && (*rr)->u.data == NULL) {
@@ -263,13 +334,13 @@ dns_lookup_int(const char *domain, int rr_class, int rr_type)
old_options = _res.options;
_res.options |= RES_DEBUG;
fprintf(stderr, "dns_lookup(%s, %d, %s)\n", domain,
- rr_class, type_to_string(rr_type));
+ rr_class, dns_type_to_string(rr_type));
}
len = res_search(domain, rr_class, rr_type, reply, sizeof(reply));
if (_resolve_debug) {
_res.options = old_options;
fprintf(stderr, "dns_lookup(%s, %d, %s) --> %d\n",
- domain, rr_class, type_to_string(rr_type), len);
+ domain, rr_class, dns_type_to_string(rr_type), len);
}
if (len >= 0)
r = parse_reply(reply, len);
@@ -281,7 +352,7 @@ dns_lookup(const char *domain, const char *type_name)
{
int type;
- type = string_to_type(type_name);
+ type = dns_string_to_type(type_name);
if(type == -1) {
if(_resolve_debug)
fprintf(stderr, "dns_lookup: unknown resource type: `%s'\n",
@@ -291,6 +362,109 @@ dns_lookup(const char *domain, const char *type_name)
return dns_lookup_int(domain, C_IN, type);
}
+static int
+compare_srv(const void *a, const void *b)
+{
+ const struct resource_record *const* aa = a, *const* bb = b;
+
+ if((*aa)->u.srv->priority == (*bb)->u.srv->priority)
+ return ((*aa)->u.srv->weight - (*bb)->u.srv->weight);
+ return ((*aa)->u.srv->priority - (*bb)->u.srv->priority);
+}
+
+#ifndef HAVE_RANDOM
+#define random() rand()
+#endif
+
+/* try to rearrange the srv-records by the algorithm in RFC2782 */
+void
+dns_srv_order(struct dns_reply *r)
+{
+ struct resource_record **srvs, **ss, **headp;
+ struct resource_record *rr;
+ int num_srv = 0;
+
+#if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE)
+ char state[256], *oldstate;
+#endif
+
+ for(rr = r->head; rr; rr = rr->next)
+ if(rr->type == T_SRV)
+ num_srv++;
+
+ if(num_srv == 0)
+ return;
+
+ srvs = malloc(num_srv * sizeof(*srvs));
+ if(srvs == NULL)
+ return; /* XXX not much to do here */
+
+ /* unlink all srv-records from the linked list and put them in
+ a vector */
+ for(ss = srvs, headp = &r->head; *headp; )
+ if((*headp)->type == T_SRV) {
+ *ss = *headp;
+ *headp = (*headp)->next;
+ (*ss)->next = NULL;
+ ss++;
+ } else
+ headp = &(*headp)->next;
+
+ /* sort them by priority and weight */
+ qsort(srvs, num_srv, sizeof(*srvs), compare_srv);
+
+#if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE)
+ oldstate = initstate(time(NULL), state, sizeof(state));
+#endif
+
+ headp = &r->head;
+
+ for(ss = srvs; ss < srvs + num_srv; ) {
+ int sum, rnd, count;
+ struct resource_record **ee, **tt;
+ /* find the last record with the same priority and count the
+ sum of all weights */
+ for(sum = 0, tt = ss; tt < srvs + num_srv; tt++) {
+ if(*tt == NULL)
+ continue;
+ if((*tt)->u.srv->priority != (*ss)->u.srv->priority)
+ break;
+ sum += (*tt)->u.srv->weight;
+ }
+ ee = tt;
+ /* ss is now the first record of this priority and ee is the
+ first of the next */
+ while(ss < ee) {
+ rnd = random() % (sum + 1);
+ for(count = 0, tt = ss; ; tt++) {
+ if(*tt == NULL)
+ continue;
+ count += (*tt)->u.srv->weight;
+ if(count >= rnd)
+ break;
+ }
+
+ assert(tt < ee);
+
+ /* insert the selected record at the tail (of the head) of
+ the list */
+ (*tt)->next = *headp;
+ *headp = *tt;
+ headp = &(*tt)->next;
+ sum -= (*tt)->u.srv->weight;
+ *tt = NULL;
+ while(*ss == NULL)
+ ss++;
+ }
+ }
+
+#if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE)
+ setstate(oldstate);
+#endif
+ free(srvs);
+ return;
+}
+
#else /* NOT defined(HAVE_RES_SEARCH) && defined(HAVE_DN_EXPAND) */
struct dns_reply *
@@ -304,6 +478,12 @@ dns_free_data(struct dns_reply *r)
{
}
+void
+dns_srv_order(struct dns_reply *r)
+{
+ return 0;
+}
+
#endif
#ifdef TEST
@@ -317,31 +497,53 @@ main(int argc, char **argv)
printf("No reply.\n");
return 1;
}
+ if(r->q.type == T_SRV)
+ dns_srv_order(r);
+
for(rr = r->head; rr;rr=rr->next){
- printf("%s %s %d ", rr->domain, type_to_string(rr->type), rr->ttl);
+ printf("%s %s %d ", rr->domain, dns_type_to_string(rr->type), rr->ttl);
switch(rr->type){
case T_NS:
+ case T_CNAME:
+ case T_PTR:
printf("%s\n", (char*)rr->u.data);
break;
case T_A:
- printf("%d.%d.%d.%d\n",
- ((unsigned char*)rr->u.data)[0],
- ((unsigned char*)rr->u.data)[1],
- ((unsigned char*)rr->u.data)[2],
- ((unsigned char*)rr->u.data)[3]);
+ printf("%s\n", inet_ntoa(*rr->u.a));
break;
case T_MX:
case T_AFSDB:{
- struct mx_record *mx = (struct mx_record*)rr->u.data;
- printf("%d %s\n", mx->preference, mx->domain);
+ printf("%d %s\n", rr->u.mx->preference, rr->u.mx->domain);
break;
}
case T_SRV:{
- struct srv_record *srv = (struct srv_record*)rr->u.data;
+ struct srv_record *srv = rr->u.srv;
printf("%d %d %d %s\n", srv->priority, srv->weight,
srv->port, srv->target);
break;
}
+ case T_TXT: {
+ printf("%s\n", rr->u.txt);
+ break;
+ }
+ case T_SIG : {
+ struct sig_record *sig = rr->u.sig;
+ const char *type_string = dns_type_to_string (sig->type);
+
+ printf ("type %u (%s), algorithm %u, labels %u, orig_ttl %u, sig_expiration %u, sig_inception %u, key_tag %u, signer %s\n",
+ sig->type, type_string ? type_string : "",
+ sig->algorithm, sig->labels, sig->orig_ttl,
+ sig->sig_expiration, sig->sig_inception, sig->key_tag,
+ sig->signer);
+ break;
+ }
+ case T_KEY : {
+ struct key_record *key = rr->u.key;
+
+ printf ("flags %u, protocol %u, algorithm %u\n",
+ key->flags, key->protocol, key->algorithm);
+ break;
+ }
default:
printf("\n");
break;
diff --git a/kerberosIV/src/lib/roken/resolve.h b/kerberosIV/src/lib/roken/resolve.h
index ef93e81c253..08d8f361aec 100644
--- a/kerberosIV/src/lib/roken/resolve.h
+++ b/kerberosIV/src/lib/roken/resolve.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1995, 1996, 1997, 1998 Kungliga Tekniska Högskolan
+ * Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
@@ -31,7 +31,7 @@
* SUCH DAMAGE.
*/
-/* $KTH: resolve.h,v 1.8 1999/12/02 16:58:52 joda Exp $ */
+/* $KTH: resolve.h,v 1.13 2001/06/09 01:35:04 joda Exp $ */
#ifndef __RESOLVE_H__
#define __RESOLVE_H__
@@ -44,12 +44,21 @@
#ifndef T_AFSDB
#define T_AFSDB 18
#endif
+#ifndef T_SIG
+#define T_SIG 24
+#endif
+#ifndef T_KEY
+#define T_KEY 25
+#endif
#ifndef T_SRV
#define T_SRV 33
#endif
#ifndef T_NAPTR
#define T_NAPTR 35
#endif
+#ifndef T_CERT
+#define T_CERT 37
+#endif
struct dns_query{
char *domain;
@@ -69,6 +78,35 @@ struct srv_record{
char target[1];
};
+struct key_record {
+ unsigned flags;
+ unsigned protocol;
+ unsigned algorithm;
+ size_t key_len;
+ u_char key_data[1];
+};
+
+struct sig_record {
+ unsigned type;
+ unsigned algorithm;
+ unsigned labels;
+ unsigned orig_ttl;
+ unsigned sig_expiration;
+ unsigned sig_inception;
+ unsigned key_tag;
+ char *signer;
+ unsigned sig_len;
+ char sig_data[1]; /* also includes signer */
+};
+
+struct cert_record {
+ unsigned type;
+ unsigned tag;
+ unsigned algorithm;
+ size_t cert_len;
+ u_char cert_data[1];
+};
+
struct resource_record{
char *domain;
unsigned type;
@@ -82,6 +120,9 @@ struct resource_record{
struct srv_record *srv;
struct in_addr *a;
char *txt;
+ struct key_record *key;
+ struct cert_record *cert;
+ struct sig_record *sig;
}u;
struct resource_record *next;
};
@@ -99,5 +140,8 @@ struct dns_reply{
struct dns_reply* dns_lookup(const char *, const char *);
void dns_free_data(struct dns_reply *);
+int dns_string_to_type(const char *name);
+const char *dns_type_to_string(int type);
+void dns_srv_order(struct dns_reply*);
#endif /* __RESOLVE_H__ */