Blame
Date:
Mon Jan 11 09:00:47 2021 UTC
Message:
a usleep makes it unnecessary slow, I guess it forces a context switch...
0001
2015-09-13
pjp
/*
0002
2018-10-19
pjp
* Copyright (c) 2015-2018 Peter J. Philipp
0003
2015-09-13
pjp
* All rights reserved.
0004
2015-09-13
pjp
*
0005
2015-09-13
pjp
* Redistribution and use in source and binary forms, with or without
0006
2015-09-13
pjp
* modification, are permitted provided that the following conditions
0007
2015-09-13
pjp
* are met:
0008
2015-09-13
pjp
* 1. Redistributions of source code must retain the above copyright
0009
2015-09-13
pjp
* notice, this list of conditions and the following disclaimer.
0010
2015-09-13
pjp
* 2. Redistributions in binary form must reproduce the above copyright
0011
2015-09-13
pjp
* notice, this list of conditions and the following disclaimer in the
0012
2015-09-13
pjp
* documentation and/or other materials provided with the distribution.
0013
2015-09-13
pjp
* 3. The name of the author may not be used to endorse or promote products
0014
2015-09-13
pjp
* derived from this software without specific prior written permission
0015
2015-09-13
pjp
*
0016
2015-09-13
pjp
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017
2015-09-13
pjp
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018
2015-09-13
pjp
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019
2015-09-13
pjp
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020
2015-09-13
pjp
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021
2015-09-13
pjp
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022
2015-09-13
pjp
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023
2015-09-13
pjp
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024
2015-09-13
pjp
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025
2015-09-13
pjp
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026
2015-09-13
pjp
*
0027
2015-09-13
pjp
*/
0028
2017-10-26
pjp
0029
2019-06-06
pjp
#include <sys/types.h>
0030
2019-06-06
pjp
#include <sys/socket.h>
0031
2019-06-06
pjp
#include <sys/select.h>
0032
2019-06-06
pjp
#include <sys/wait.h>
0033
2017-10-26
pjp
0034
2019-06-06
pjp
#include <netinet/in.h>
0035
2019-06-06
pjp
#include <arpa/inet.h>
0036
2019-06-06
pjp
#include <netdb.h>
0037
2019-06-06
pjp
0038
2019-06-06
pjp
#include <stdio.h>
0039
2019-06-06
pjp
#include <stdlib.h>
0040
2019-06-06
pjp
#include <string.h>
0041
2019-06-06
pjp
#include <unistd.h>
0042
2019-06-06
pjp
#include <syslog.h>
0043
2019-06-06
pjp
#include <errno.h>
0044
2019-06-06
pjp
#include <signal.h>
0045
2019-06-06
pjp
#include <ctype.h>
0046
2019-06-06
pjp
0047
2019-06-06
pjp
#ifdef __linux__
0048
2019-06-06
pjp
#include <grp.h>
0049
2019-06-06
pjp
#define __USE_BSD 1
0050
2019-06-06
pjp
#include <endian.h>
0051
2019-06-06
pjp
#include <bsd/stdlib.h>
0052
2019-06-06
pjp
#include <bsd/string.h>
0053
2019-06-06
pjp
#include <bsd/sys/queue.h>
0054
2019-06-06
pjp
#define __unused
0055
2019-06-06
pjp
#include <bsd/sys/tree.h>
0056
2019-06-06
pjp
#include <bsd/sys/endian.h>
0057
2019-06-06
pjp
#else /* not linux */
0058
2019-06-06
pjp
#include <sys/queue.h>
0059
2019-06-06
pjp
#include <sys/tree.h>
0060
2019-06-06
pjp
#ifdef __FreeBSD__
0061
2019-06-06
pjp
#include "imsg.h"
0062
2019-06-06
pjp
#else
0063
2019-06-06
pjp
#include <imsg.h>
0064
2019-06-06
pjp
#endif /* __FreeBSD__ */
0065
2019-06-06
pjp
#endif /* __linux__ */
0066
2019-06-06
pjp
0067
2016-07-06
pjp
#include "ddd-dns.h"
0068
2016-07-06
pjp
#include "ddd-db.h"
0069
2016-07-06
pjp
0070
2015-09-13
pjp
#include <openssl/sha.h>
0071
2015-09-13
pjp
0072
2015-09-13
pjp
/* prototypes */
0073
2015-09-13
pjp
0074
2015-09-13
pjp
void init_dnssec(void);
0075
2015-09-13
pjp
int insert_apex(char *zonename, char *zone, int zonelen);
0076
2015-09-13
pjp
int insert_nsec3(char *zonename, char *domainname, char *dname, int dnamelen);
0077
2015-09-14
pjp
char * find_next_closer_nsec3(char *zonename, int zonelen, char *hashname);
0078
2015-09-14
pjp
char * find_match_nsec3(char *zonename, int zonelen, char *hashname);
0079
2017-01-11
pjp
char * find_match_nsec3_ent(char *zonename, int zonelen, char *hashname);
0080
2019-02-15
pjp
struct rbtree * find_nsec(char *name, int namelen, struct rbtree *rbt, ddDB *db);
0081
2019-02-15
pjp
struct rbtree * find_nsec3_match_qname(char *name, int namelen, struct rbtree *, ddDB *db);
0082
2019-02-15
pjp
struct rbtree * find_nsec3_match_closest(char *name, int namelen, struct rbtree *, ddDB *db);
0083
2019-02-15
pjp
struct rbtree * find_nsec3_wildcard_closest(char *name, int namelen, struct rbtree *, ddDB *db);
0084
2015-09-13
pjp
char * convert_name(char *name, int namelen);
0085
2015-09-13
pjp
int nsec_comp(const void *a, const void *b);
0086
2015-09-13
pjp
int nsec3_comp(const void *a, const void *b);
0087
2015-09-13
pjp
int count_dots(char *name);
0088
2019-02-15
pjp
struct rbtree * find_closest_encloser(ddDB *db, char *name, int namelen);
0089
2016-01-22
pjp
char * find_next_closer_name(char *, int, char *, int, int *);
0090
2015-09-13
pjp
char * hash_name(char *name, int len, struct nsec3param *n3p);
0091
2015-09-13
pjp
char * base32hex_encode(u_char *input, int len);
0092
2015-11-17
pjp
int base32hex_decode(u_char *, u_char *);
0093
2015-11-17
pjp
void mysetbit(u_char *, int);
0094
2015-09-13
pjp
0095
2017-06-26
pjp
extern int get_record_size(ddDB *, char *, int);
0096
2015-09-13
pjp
extern char * dns_label(char *, int *);
0097
2015-09-13
pjp
extern void dolog(int, char *, ...);
0098
2019-02-15
pjp
extern int checklabel(ddDB *, struct rbtree *, struct rbtree *, struct question *);
0099
2015-09-13
pjp
extern int free_question(struct question *);
0100
2017-01-11
pjp
extern int check_ent(char *, int);
0101
2017-01-11
pjp
extern int memcasecmp(u_char *, u_char *, int);
0102
2015-09-13
pjp
0103
2019-02-15
pjp
extern struct rbtree * find_rrset(ddDB *db, char *name, int len);
0104
2019-02-15
pjp
extern struct rrset * find_rr(struct rbtree *rbt, u_int16_t rrtype);
0105
2019-02-15
pjp
extern int add_rr(struct rbtree *rbt, char *name, int len, u_int16_t rrtype, void *rdata);
0106
2019-02-15
pjp
0107
2015-09-13
pjp
SLIST_HEAD(listhead, dnssecentry) dnssechead;
0108
2015-09-13
pjp
0109
2015-09-14
pjp
static struct nsec3entry {
0110
2015-09-14
pjp
char domainname[DNS_MAXNAME + 1];
0111
2015-09-14
pjp
char dname[DNS_MAXNAME];
0112
2015-09-14
pjp
int dnamelen;
0113
2015-09-14
pjp
TAILQ_ENTRY(nsec3entry) nsec3_entries;
0114
2015-09-14
pjp
} *n3, *ns3p;
0115
2015-09-14
pjp
0116
2015-09-13
pjp
static struct dnssecentry {
0117
2015-09-13
pjp
char zonename[DNS_MAXNAME + 1];
0118
2015-09-13
pjp
char zone[DNS_MAXNAME];
0119
2015-09-13
pjp
int zonelen;
0120
2015-09-13
pjp
SLIST_ENTRY(dnssecentry) dnssec_entry;
0121
2019-02-15
pjp
TAILQ_HEAD(aa, nsec3entry) nsec3head;
0122
2015-09-13
pjp
} *dn, *dnp;
0123
2015-09-13
pjp
0124
2015-09-13
pjp
0125
2015-09-13
pjp
void
0126
2015-09-13
pjp
init_dnssec(void)
0127
2015-09-13
pjp
{
0128
2015-09-13
pjp
SLIST_INIT(&dnssechead);
0129
2015-09-13
pjp
return;
0130
2015-09-13
pjp
}
0131
2015-09-13
pjp
0132
2015-09-13
pjp
int
0133
2015-09-13
pjp
insert_apex(char *zonename, char *zone, int zonelen)
0134
2015-09-13
pjp
{
0135
2015-09-13
pjp
dn = calloc(1, sizeof(struct dnssecentry));
0136
2015-09-13
pjp
if (dn == NULL) {
0137
2015-09-13
pjp
return -1;
0138
2015-09-13
pjp
}
0139
2015-09-13
pjp
0140
2015-09-13
pjp
strlcpy(dn->zonename, zonename, DNS_MAXNAME + 1);
0141
2015-09-13
pjp
0142
2015-09-13
pjp
if (zonelen > DNS_MAXNAME) {
0143
2015-09-13
pjp
free (dn);
0144
2015-09-13
pjp
return -1;
0145
2015-09-13
pjp
}
0146
2015-09-13
pjp
0147
2015-09-13
pjp
memcpy(dn->zone, zone, zonelen);
0148
2015-09-13
pjp
dn->zonelen = zonelen;
0149
2015-09-13
pjp
0150
2015-09-14
pjp
TAILQ_INIT(&dn->nsec3head);
0151
2015-09-13
pjp
0152
2015-09-13
pjp
SLIST_INSERT_HEAD(&dnssechead, dn, dnssec_entry);
0153
2015-09-13
pjp
0154
2015-09-13
pjp
return (0);
0155
2015-09-13
pjp
}
0156
2015-09-13
pjp
0157
2015-09-13
pjp
int
0158
2015-09-13
pjp
insert_nsec3(char *zonename, char *domainname, char *dname, int dnamelen)
0159
2015-09-13
pjp
{
0160
2015-09-13
pjp
0161
2015-09-13
pjp
SLIST_FOREACH(dnp, &dnssechead, dnssec_entry) {
0162
2015-09-13
pjp
if (strcasecmp(dnp->zonename, zonename) == 0)
0163
2015-09-13
pjp
break;
0164
2015-09-13
pjp
}
0165
2015-09-13
pjp
0166
2015-09-13
pjp
if (dnp == NULL)
0167
2015-09-13
pjp
return -1;
0168
2015-09-13
pjp
0169
2015-09-14
pjp
n3 = calloc(1, sizeof(struct nsec3entry));
0170
2015-09-13
pjp
if (n3 == NULL)
0171
2015-09-13
pjp
return -1;
0172
2015-09-13
pjp
0173
2015-09-13
pjp
strlcpy(n3->domainname, domainname, DNS_MAXNAME + 1);
0174
2015-09-13
pjp
0175
2015-09-13
pjp
if (dnamelen > DNS_MAXNAME) {
0176
2015-09-13
pjp
free (n3);
0177
2015-09-13
pjp
return -1;
0178
2015-09-13
pjp
}
0179
2015-09-13
pjp
0180
2015-09-13
pjp
memcpy(n3->dname, dname, dnamelen);
0181
2015-09-13
pjp
n3->dnamelen = dnamelen;
0182
2015-09-13
pjp
0183
2015-09-14
pjp
0184
2015-11-07
pjp
/*
0185
2015-11-07
pjp
* sort the tailq here
0186
2015-11-07
pjp
*/
0187
2015-11-07
pjp
0188
2015-11-07
pjp
if (TAILQ_EMPTY(&dn->nsec3head)) {
0189
2015-11-07
pjp
TAILQ_INSERT_TAIL(&dn->nsec3head, n3, nsec3_entries);
0190
2015-11-07
pjp
} else {
0191
2015-11-07
pjp
ns3p = TAILQ_FIRST(&dn->nsec3head);
0192
2015-11-07
pjp
if (strcmp(n3->domainname, ns3p->domainname) < 0) {
0193
2015-11-07
pjp
TAILQ_INSERT_BEFORE(ns3p, n3, nsec3_entries);
0194
2015-11-07
pjp
} else {
0195
2015-11-07
pjp
while ((ns3p = TAILQ_NEXT(ns3p, nsec3_entries)) != NULL) {
0196
2015-11-07
pjp
if (strcmp(n3->domainname, ns3p->domainname) < 0) {
0197
2015-11-07
pjp
TAILQ_INSERT_BEFORE(ns3p, n3, nsec3_entries);
0198
2015-11-07
pjp
break;
0199
2015-11-07
pjp
}
0200
2015-11-07
pjp
}
0201
2015-11-07
pjp
}
0202
2015-11-07
pjp
if (ns3p == NULL) {
0203
2015-11-07
pjp
TAILQ_INSERT_TAIL(&dn->nsec3head, n3, nsec3_entries);
0204
2015-11-07
pjp
}
0205
2015-11-07
pjp
}
0206
2015-11-07
pjp
0207
2015-11-07
pjp
0208
2015-09-14
pjp
return (0);
0209
2015-09-14
pjp
}
0210
2015-09-14
pjp
0211
2015-09-14
pjp
char *
0212
2015-09-14
pjp
find_next_closer_nsec3(char *zonename, int zonelen, char *hashname)
0213
2015-09-14
pjp
{
0214
2015-09-14
pjp
int hashlen;
0215
2015-09-14
pjp
0216
2015-09-14
pjp
hashlen = strlen(hashname);
0217
2015-09-14
pjp
0218
2015-09-14
pjp
SLIST_FOREACH(dnp, &dnssechead, dnssec_entry) {
0219
2015-09-14
pjp
if (zonelen == dnp->zonelen &&
0220
2015-09-14
pjp
(memcmp(dnp->zone, zonename, zonelen) == 0))
0221
2015-09-13
pjp
break;
0222
2015-09-14
pjp
}
0223
2015-09-14
pjp
0224
2015-09-14
pjp
if (dnp == NULL)
0225
2015-09-14
pjp
return NULL;
0226
2015-09-14
pjp
0227
2015-09-14
pjp
/* we have found the zone, now find the next closer hash for nsec3 */
0228
2015-09-14
pjp
0229
2015-09-14
pjp
TAILQ_FOREACH(n3, &dnp->nsec3head, nsec3_entries) {
0230
2015-09-14
pjp
if (strncasecmp(hashname, n3->domainname, hashlen) <= 0) {
0231
2015-09-14
pjp
break;
0232
2015-09-14
pjp
}
0233
2015-09-14
pjp
}
0234
2015-09-13
pjp
0235
2015-09-14
pjp
if (n3 == NULL) {
0236
2017-09-06
pjp
/* returning NULL is not recommended here */
0237
2019-02-15
pjp
ns3p = TAILQ_LAST(&dnp->nsec3head, aa);
0238
2017-09-06
pjp
return (ns3p->domainname);
0239
2015-09-14
pjp
}
0240
2015-09-13
pjp
0241
2016-01-22
pjp
#if DEBUG
0242
2015-09-14
pjp
dolog(LOG_INFO, "resolved at %s\n", n3->domainname);
0243
2015-09-14
pjp
#endif
0244
2015-09-14
pjp
0245
2019-02-15
pjp
if ((ns3p = TAILQ_PREV(n3, aa, nsec3_entries)) != NULL) {
0246
2015-09-14
pjp
return (ns3p->domainname);
0247
2015-09-14
pjp
} else {
0248
2019-02-15
pjp
ns3p = TAILQ_LAST(&dnp->nsec3head, aa);
0249
2015-09-14
pjp
return (ns3p->domainname);
0250
2015-09-14
pjp
}
0251
2015-09-14
pjp
0252
2015-09-14
pjp
/* NOTREACHED */
0253
2015-09-14
pjp
return (NULL);
0254
2015-09-13
pjp
}
0255
2015-09-13
pjp
0256
2019-02-18
pjp
#if 0
0257
2015-09-14
pjp
char *
0258
2017-01-11
pjp
find_match_nsec3_ent(char *zonename, int zonelen, char *hashname)
0259
2017-01-11
pjp
{
0260
2017-01-11
pjp
int hashlen;
0261
2017-01-11
pjp
int count;
0262
2017-01-11
pjp
0263
2017-01-11
pjp
hashlen = strlen(hashname);
0264
2017-01-11
pjp
0265
2017-01-11
pjp
SLIST_FOREACH(dnp, &dnssechead, dnssec_entry) {
0266
2017-01-11
pjp
if (zonelen == dnp->zonelen &&
0267
2017-01-11
pjp
(memcasecmp(dnp->zone, zonename, zonelen) == 0))
0268
2017-01-11
pjp
break;
0269
2017-01-11
pjp
}
0270
2017-01-11
pjp
0271
2017-01-11
pjp
if (dnp == NULL)
0272
2017-01-11
pjp
return NULL;
0273
2017-01-11
pjp
0274
2017-01-11
pjp
/* we have found the zone, now find the next closer hash for nsec3 */
0275
2017-01-11
pjp
0276
2017-01-11
pjp
count = 0;
0277
2017-01-11
pjp
TAILQ_FOREACH(n3, &dnp->nsec3head, nsec3_entries) {
0278
2017-01-11
pjp
if (strncasecmp(hashname, n3->domainname, hashlen) < 0) {
0279
2017-01-11
pjp
if (count == 0)
0280
2019-02-15
pjp
n3 = TAILQ_LAST(&dnp->nsec3head, aa);
0281
2017-01-11
pjp
else
0282
2019-02-15
pjp
n3 = TAILQ_PREV(n3, aa, nsec3_entries);
0283
2017-01-11
pjp
break;
0284
2017-01-11
pjp
}
0285
2017-01-11
pjp
count++;
0286
2017-01-11
pjp
}
0287
2017-01-11
pjp
0288
2017-01-11
pjp
if (n3 == NULL) {
0289
2017-01-11
pjp
return NULL;
0290
2017-01-11
pjp
}
0291
2017-01-11
pjp
0292
2017-01-11
pjp
#ifdef DEBUG
0293
2017-01-11
pjp
dolog(LOG_INFO, "resolved at %s\n", n3->domainname);
0294
2017-01-11
pjp
#endif
0295
2017-01-11
pjp
0296
2017-01-11
pjp
return (n3->domainname);
0297
2017-01-11
pjp
}
0298
2019-02-18
pjp
#endif
0299
2017-01-11
pjp
0300
2017-01-11
pjp
char *
0301
2015-09-14
pjp
find_match_nsec3(char *zonename, int zonelen, char *hashname)
0302
2015-09-14
pjp
{
0303
2015-09-14
pjp
int hashlen;
0304
2015-09-14
pjp
0305
2015-09-14
pjp
hashlen = strlen(hashname);
0306
2015-09-14
pjp
0307
2015-09-14
pjp
SLIST_FOREACH(dnp, &dnssechead, dnssec_entry) {
0308
2015-09-14
pjp
if (zonelen == dnp->zonelen &&
0309
2015-09-14
pjp
(memcmp(dnp->zone, zonename, zonelen) == 0))
0310
2015-09-14
pjp
break;
0311
2015-09-14
pjp
}
0312
2015-09-14
pjp
0313
2015-09-14
pjp
if (dnp == NULL)
0314
2015-09-14
pjp
return NULL;
0315
2015-09-14
pjp
0316
2015-09-14
pjp
/* we have found the zone, now find the next closer hash for nsec3 */
0317
2015-09-14
pjp
0318
2015-09-14
pjp
TAILQ_FOREACH(n3, &dnp->nsec3head, nsec3_entries) {
0319
2015-09-14
pjp
if (strncasecmp(hashname, n3->domainname, hashlen) == 0) {
0320
2015-09-14
pjp
break;
0321
2015-09-14
pjp
}
0322
2015-09-14
pjp
}
0323
2015-09-14
pjp
0324
2015-09-14
pjp
if (n3 == NULL) {
0325
2015-09-14
pjp
return NULL;
0326
2015-09-14
pjp
}
0327
2015-09-14
pjp
0328
2015-09-14
pjp
#ifdef DEBUG
0329
2015-09-14
pjp
dolog(LOG_INFO, "resolved at %s\n", n3->domainname);
0330
2015-09-14
pjp
#endif
0331
2015-09-14
pjp
0332
2015-09-14
pjp
return (n3->domainname);
0333
2015-09-14
pjp
}
0334
2015-09-14
pjp
0335
2015-09-14
pjp
0336
2015-09-13
pjp
/* FIND_NSEC */
0337
2015-09-13
pjp
/* finds the right nsec domainname in a zone */
0338
2019-02-15
pjp
struct rbtree *
0339
2019-02-15
pjp
find_nsec(char *name, int namelen, struct rbtree *rbt, ddDB *db)
0340
2015-09-13
pjp
{
0341
2015-09-13
pjp
char *table, *tmp;
0342
2015-09-13
pjp
char *nsecname;
0343
2015-09-13
pjp
struct domainnames {
0344
2015-09-13
pjp
char name[DNS_MAXNAME + 1];
0345
2015-09-13
pjp
char next[DNS_MAXNAME + 1];
0346
2015-09-13
pjp
} *dn;
0347
2015-09-13
pjp
0348
2019-02-15
pjp
struct rbtree *rbt0;
0349
2019-02-15
pjp
struct rrset *rrset = NULL;
0350
2019-02-15
pjp
struct rr *rrp = NULL;
0351
2015-09-13
pjp
char *humanname;
0352
2015-09-13
pjp
char tmpname[DNS_MAXNAME];
0353
2015-09-13
pjp
int tmplen;
0354
2019-02-15
pjp
char *backname;
0355
2015-09-13
pjp
int backnamelen;
0356
2015-09-13
pjp
int i, names = 100;
0357
2015-09-13
pjp
int j;
0358
2015-09-13
pjp
0359
2015-09-13
pjp
humanname = convert_name(name, namelen);
0360
2015-09-13
pjp
0361
2019-02-15
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_NSEC)) == NULL) {
0362
2015-09-13
pjp
free (humanname);
0363
2015-09-13
pjp
return (NULL);
0364
2015-09-13
pjp
}
0365
2015-09-13
pjp
0366
2019-02-15
pjp
rrp = TAILQ_FIRST(&rrset->rr_head);
0367
2019-02-15
pjp
if (rrp == NULL) {
0368
2019-02-15
pjp
free(humanname);
0369
2019-02-15
pjp
return (NULL);
0370
2019-02-15
pjp
}
0371
2019-02-15
pjp
0372
2015-09-13
pjp
table = calloc(names, sizeof(struct domainnames));
0373
2015-09-13
pjp
if (table == NULL) {
0374
2015-09-13
pjp
free (humanname);
0375
2015-09-13
pjp
return (NULL);
0376
2015-09-13
pjp
}
0377
2015-09-13
pjp
0378
2015-09-13
pjp
dn = (struct domainnames *)table;
0379
2019-02-15
pjp
strlcpy(dn->name, rbt->humanname, DNS_MAXNAME + 1);
0380
2019-02-15
pjp
nsecname = convert_name(((struct nsec *)rrp->rdata)->next_domain_name, ((struct nsec *)rrp->rdata)->ndn_len);
0381
2015-09-13
pjp
strlcpy(dn->next, nsecname, DNS_MAXNAME + 1);
0382
2015-09-13
pjp
0383
2019-02-15
pjp
rbt0 = find_rrset(db, ((struct nsec *)rrp->rdata)->next_domain_name, ((struct nsec *)rrp->rdata)->ndn_len);
0384
2019-02-15
pjp
if (rbt0 == NULL) {
0385
2015-09-13
pjp
free (nsecname);
0386
2015-09-13
pjp
free (humanname);
0387
2015-09-13
pjp
free (table);
0388
2015-09-13
pjp
return (NULL);
0389
2015-09-13
pjp
}
0390
2015-09-13
pjp
0391
2019-02-15
pjp
if ((rrset = find_rr(rbt0, DNS_TYPE_NSEC)) == NULL) {
0392
2015-09-13
pjp
free (nsecname);
0393
2015-09-13
pjp
free (humanname);
0394
2015-09-13
pjp
free (table);
0395
2015-09-13
pjp
return (NULL);
0396
2015-09-13
pjp
}
0397
2015-09-13
pjp
0398
2019-02-15
pjp
rrp = TAILQ_FIRST(&rrset->rr_head);
0399
2019-02-15
pjp
if (rrp == NULL) {
0400
2020-07-18
pjp
free(nsecname);
0401
2019-02-15
pjp
free(humanname);
0402
2015-09-13
pjp
return (NULL);
0403
2015-09-13
pjp
}
0404
2015-09-13
pjp
0405
2015-09-13
pjp
i = 1;
0406
2019-02-15
pjp
while (strcasecmp(nsecname, rbt->humanname) != 0) {
0407
2015-09-13
pjp
/* grow our table */
0408
2015-09-13
pjp
if (i == names - 1) {
0409
2015-09-13
pjp
names += 100;
0410
2015-09-13
pjp
0411
2015-09-13
pjp
tmp = realloc(table, names * sizeof(struct domainnames));
0412
2015-09-13
pjp
if (tmp == NULL) {
0413
2015-09-13
pjp
free (nsecname);
0414
2015-09-13
pjp
free (humanname);
0415
2015-09-13
pjp
free (table);
0416
2015-09-13
pjp
return (NULL);
0417
2015-09-13
pjp
}
0418
2015-09-13
pjp
table = tmp;
0419
2015-09-13
pjp
}
0420
2015-09-13
pjp
0421
2015-09-13
pjp
dn = ((struct domainnames *)table) + i;
0422
2015-09-13
pjp
0423
2015-09-13
pjp
free (nsecname);
0424
2019-02-15
pjp
strlcpy(dn->name, rbt0->humanname, DNS_MAXNAME + 1);
0425
2019-02-15
pjp
nsecname = convert_name(((struct nsec *)rrp->rdata)->next_domain_name, ((struct nsec *)rrp->rdata)->ndn_len);
0426
2015-09-13
pjp
strlcpy(dn->next, nsecname, DNS_MAXNAME + 1);
0427
2015-09-13
pjp
0428
2019-02-15
pjp
memcpy(tmpname, ((struct nsec *)rrp->rdata)->next_domain_name, ((struct nsec *)rrp->rdata)->ndn_len);
0429
2019-02-15
pjp
tmplen = ((struct nsec *)rrp->rdata)->ndn_len;
0430
2015-09-13
pjp
0431
2015-09-13
pjp
0432
2019-02-15
pjp
rbt0 = find_rrset(db, tmpname, tmplen);
0433
2019-02-15
pjp
if (rbt0 == NULL) {
0434
2020-07-18
pjp
free (nsecname);
0435
2015-09-13
pjp
free (humanname);
0436
2015-09-13
pjp
free (table);
0437
2015-09-13
pjp
return (NULL);
0438
2015-09-13
pjp
}
0439
2015-09-13
pjp
0440
2019-02-15
pjp
if ((rrset = find_rr(rbt0, DNS_TYPE_NSEC)) == NULL) {
0441
2020-07-18
pjp
free (nsecname);
0442
2015-09-13
pjp
free (humanname);
0443
2015-09-13
pjp
free (table);
0444
2015-09-13
pjp
return (NULL);
0445
2015-09-13
pjp
}
0446
2015-09-13
pjp
0447
2015-09-13
pjp
i++;
0448
2015-09-13
pjp
}
0449
2015-09-13
pjp
0450
2015-09-13
pjp
free (nsecname);
0451
2015-09-13
pjp
dn = ((struct domainnames *)table) + i;
0452
2015-09-13
pjp
strlcpy(dn->next, ".", DNS_MAXNAME + 1);
0453
2015-09-13
pjp
strlcpy(dn->name, humanname, DNS_MAXNAME + 1);
0454
2015-09-13
pjp
0455
2015-09-13
pjp
i++;
0456
2015-09-13
pjp
0457
2015-09-13
pjp
/* now we sort the shebang */
0458
2015-09-13
pjp
0459
2015-09-13
pjp
qsort(table, i, sizeof(struct domainnames), nsec_comp);
0460
2015-09-13
pjp
0461
2015-09-13
pjp
for (j = 0; j < i; j++) {
0462
2015-09-13
pjp
dn = ((struct domainnames *)table) + j;
0463
2015-09-13
pjp
0464
2015-09-13
pjp
#if DEBUG
0465
2019-02-18
pjp
printf("%s\n", dn->name);
0466
2015-09-13
pjp
#endif
0467
2015-09-13
pjp
0468
2015-09-13
pjp
if (strcmp(dn->next, ".") == 0)
0469
2015-09-13
pjp
break;
0470
2015-09-13
pjp
}
0471
2015-09-13
pjp
0472
2015-09-13
pjp
dn = ((struct domainnames *)table) + (j - 1);
0473
2015-09-13
pjp
0474
2015-09-13
pjp
/* found it, get it via db after converting it */
0475
2015-09-13
pjp
0476
2015-09-13
pjp
/* free what we don't need */
0477
2015-09-13
pjp
free (humanname);
0478
2015-09-13
pjp
0479
2015-09-13
pjp
backname = dns_label(dn->name, &backnamelen);
0480
2015-09-13
pjp
free (table);
0481
2015-09-13
pjp
0482
2015-09-13
pjp
0483
2019-02-15
pjp
rbt0 = find_rrset(db, backname, backnamelen);
0484
2019-02-15
pjp
if (rbt0 == NULL) {
0485
2015-09-13
pjp
free (backname);
0486
2015-09-13
pjp
return (NULL);
0487
2015-09-13
pjp
}
0488
2015-09-13
pjp
0489
2015-09-13
pjp
free (backname);
0490
2019-02-15
pjp
return (rbt0);
0491
2015-09-13
pjp
}
0492
2015-09-13
pjp
0493
2015-09-13
pjp
char *
0494
2015-09-13
pjp
convert_name(char *name, int namelen)
0495
2015-09-13
pjp
{
0496
2015-09-13
pjp
char *ret;
0497
2015-09-13
pjp
char *p, *p0;
0498
2015-09-13
pjp
int plen;
0499
2015-09-13
pjp
int i;
0500
2015-09-13
pjp
0501
2020-07-16
pjp
if (namelen <= 0)
0502
2019-02-05
pjp
return NULL;
0503
2019-02-05
pjp
0504
2015-09-13
pjp
ret = calloc(namelen + 1, 1);
0505
2015-09-13
pjp
if (ret == NULL) {
0506
2015-09-13
pjp
return NULL;
0507
2020-07-19
pjp
}
0508
2020-07-19
pjp
0509
2020-07-19
pjp
/* short circuit root */
0510
2020-07-19
pjp
if (namelen == 1 && name[1] == '\0') {
0511
2020-07-19
pjp
ret[0] = '.';
0512
2020-07-19
pjp
return (ret);
0513
2020-07-16
pjp
}
0514
2020-07-16
pjp
0515
2015-09-13
pjp
memcpy(ret, name + 1, namelen - 1);
0516
2015-09-13
pjp
0517
2015-09-13
pjp
p0 = ret;
0518
2015-09-13
pjp
p = name;
0519
2015-09-13
pjp
plen = namelen;
0520
2015-09-13
pjp
0521
2020-07-16
pjp
while (plen >= 0 && *p != 0) {
0522
2020-07-16
pjp
if (*p > DNS_MAXLABEL) {
0523
2020-07-16
pjp
dolog(LOG_INFO, "compression in dns name\n");
0524
2020-07-16
pjp
free (ret);
0525
2020-07-16
pjp
return NULL;
0526
2020-07-16
pjp
}
0527
2015-09-13
pjp
for (i = 0; i < *p; i++) {
0528
2015-09-13
pjp
*p0++ = p[i + 1];
0529
2015-09-13
pjp
}
0530
2015-09-13
pjp
*p0++ = '.';
0531
2015-09-13
pjp
plen -= (*p + 1);
0532
2020-07-16
pjp
p += (*p + 1);
0533
2015-09-13
pjp
}
0534
2015-09-13
pjp
0535
2015-09-13
pjp
return (ret);
0536
2015-09-13
pjp
}
0537
2015-09-13
pjp
0538
2015-09-13
pjp
/* canonical sort compare */
0539
2015-09-13
pjp
0540
2015-09-13
pjp
int
0541
2015-09-13
pjp
nsec_comp(const void *a, const void *b)
0542
2015-09-13
pjp
{
0543
2015-09-13
pjp
struct domainnames {
0544
2015-09-13
pjp
char name[DNS_MAXNAME + 1];
0545
2015-09-13
pjp
char next[DNS_MAXNAME + 1];
0546
2015-09-13
pjp
};
0547
2015-09-13
pjp
struct domainnames *dn0, *dn1;
0548
2015-09-13
pjp
int dots0, dots1;
0549
2015-09-13
pjp
0550
2015-09-13
pjp
dn0 = (struct domainnames *)a;
0551
2015-09-13
pjp
dn1 = (struct domainnames *)b;
0552
2015-09-13
pjp
0553
2015-09-13
pjp
/* count the dots we need this for canonical compare */
0554
2015-09-13
pjp
0555
2015-09-13
pjp
dots0 = count_dots(dn0->name);
0556
2015-09-13
pjp
dots1 = count_dots(dn1->name);
0557
2015-09-13
pjp
0558
2015-09-13
pjp
if (dots0 > dots1)
0559
2015-09-13
pjp
return 1;
0560
2015-09-13
pjp
else if (dots1 > dots0)
0561
2015-09-13
pjp
return -1;
0562
2015-09-13
pjp
0563
2015-09-13
pjp
0564
2015-09-13
pjp
/* we have a tie, strcmp them */
0565
2015-09-13
pjp
0566
2015-09-13
pjp
return (strcmp(dn0->name, dn1->name));
0567
2015-09-13
pjp
}
0568
2015-09-13
pjp
0569
2015-09-13
pjp
/* much like nsec_comp */
0570
2015-09-13
pjp
0571
2015-09-13
pjp
int
0572
2015-09-13
pjp
nsec3_comp(const void *a, const void *b)
0573
2015-09-13
pjp
{
0574
2015-09-13
pjp
struct domainnames {
0575
2015-09-13
pjp
char name[DNS_MAXNAME + 1];
0576
2015-09-13
pjp
char next[DNS_MAXNAME + 1];
0577
2015-09-13
pjp
};
0578
2015-09-13
pjp
struct domainnames *dn0, *dn1;
0579
2015-09-13
pjp
0580
2015-09-13
pjp
dn0 = (struct domainnames *)a;
0581
2015-09-13
pjp
dn1 = (struct domainnames *)b;
0582
2015-09-13
pjp
0583
2015-09-13
pjp
return (strcmp(dn0->name, dn1->name));
0584
2015-09-13
pjp
}
0585
2015-09-13
pjp
0586
2015-09-13
pjp
int
0587
2015-09-13
pjp
count_dots(char *name)
0588
2015-09-13
pjp
{
0589
2015-09-13
pjp
int i;
0590
2015-09-13
pjp
int ret = 0;
0591
2015-09-13
pjp
0592
2015-09-13
pjp
0593
2015-09-13
pjp
for (i = 0; i < strlen(name); i++) {
0594
2015-09-13
pjp
if (name[i] == '.')
0595
2015-09-13
pjp
ret++;
0596
2015-09-13
pjp
}
0597
2015-09-13
pjp
0598
2015-09-13
pjp
return(ret);
0599
2015-09-13
pjp
}
0600
2015-09-13
pjp
0601
2015-09-13
pjp
/*
0602
2016-01-22
pjp
* FIND_NEXT_CLOSER - find the next closer name
0603
2015-09-13
pjp
*/
0604
2015-09-13
pjp
0605
2016-01-22
pjp
char *
0606
2016-01-22
pjp
find_next_closer_name(char *qname, int qlen, char *closestname, int clen, int *rlen)
0607
2015-09-13
pjp
{
0608
2016-01-22
pjp
static char save[DNS_MAXNAME];
0609
2015-09-13
pjp
0610
2015-09-13
pjp
int plen;
0611
2016-01-22
pjp
int qcount = 0;
0612
2016-01-22
pjp
int ccount = 0;
0613
2016-01-22
pjp
int discard;
0614
2015-09-13
pjp
0615
2016-01-22
pjp
char *p;
0616
2015-09-13
pjp
0617
2016-01-22
pjp
p = qname;
0618
2016-01-22
pjp
plen = qlen;
0619
2016-01-22
pjp
0620
2016-01-22
pjp
do {
0621
2016-01-22
pjp
plen -= (*p + 1);
0622
2016-01-22
pjp
p = (p + (*p + 1));
0623
2016-01-22
pjp
qcount++;
0624
2016-01-22
pjp
} while (*p);
0625
2016-01-22
pjp
0626
2016-01-22
pjp
p = closestname;
0627
2016-01-22
pjp
plen = clen;
0628
2016-01-22
pjp
0629
2016-01-22
pjp
do {
0630
2016-01-22
pjp
plen -= (*p + 1);
0631
2016-01-22
pjp
p = (p + (*p + 1));
0632
2016-01-22
pjp
ccount++;
0633
2016-01-22
pjp
} while (*p);
0634
2016-01-22
pjp
0635
2016-01-22
pjp
0636
2016-01-22
pjp
discard = qcount - (ccount + 1);
0637
2016-01-22
pjp
if (discard < 0)
0638
2016-01-22
pjp
return NULL;
0639
2016-01-22
pjp
0640
2016-01-22
pjp
p = qname;
0641
2016-01-22
pjp
plen = qlen;
0642
2016-01-22
pjp
0643
2016-01-22
pjp
while (*p && discard > 0) {
0644
2016-01-22
pjp
plen -= (*p + 1);
0645
2016-01-22
pjp
p = (p + (*p + 1));
0646
2016-01-22
pjp
discard--;
0647
2016-01-22
pjp
}
0648
2016-01-22
pjp
0649
2016-01-22
pjp
*rlen = plen;
0650
2016-01-22
pjp
memcpy(save, p, plen);
0651
2016-01-22
pjp
0652
2016-01-22
pjp
return ((char *)&save);
0653
2016-01-22
pjp
}
0654
2016-01-22
pjp
0655
2016-01-22
pjp
/*
0656
2016-01-22
pjp
* FIND_CLOSEST_ENCLOSER - find the closest encloser record
0657
2016-01-22
pjp
*/
0658
2016-01-22
pjp
0659
2019-02-15
pjp
struct rbtree *
0660
2017-06-26
pjp
find_closest_encloser(ddDB *db, char *name, int namelen)
0661
2016-01-22
pjp
{
0662
2019-02-15
pjp
struct rbtree *rbt = NULL;
0663
2019-02-15
pjp
struct rrset *rrset = NULL;
0664
2016-01-22
pjp
0665
2016-01-22
pjp
int plen;
0666
2016-01-22
pjp
0667
2016-01-22
pjp
char *p;
0668
2016-01-22
pjp
0669
2016-01-22
pjp
p = name;
0670
2016-01-22
pjp
plen = namelen;
0671
2016-01-22
pjp
0672
2016-01-22
pjp
/* advance one label */
0673
2016-01-22
pjp
plen -= (*p + 1);
0674
2016-01-22
pjp
p = (p + (*p + 1));
0675
2016-01-22
pjp
0676
2016-01-22
pjp
0677
2016-01-22
pjp
do {
0678
2019-02-15
pjp
rbt = find_rrset(db, p, plen);
0679
2019-02-15
pjp
if (rbt == NULL) {
0680
2015-09-13
pjp
plen -= (*p + 1);
0681
2015-09-13
pjp
p = (p + (*p + 1));
0682
2015-09-13
pjp
continue;
0683
2015-09-13
pjp
}
0684
2015-09-13
pjp
0685
2019-02-15
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_NSEC3)) != NULL) {
0686
2015-09-13
pjp
plen -= (*p + 1);
0687
2015-09-13
pjp
p = (p + (*p + 1));
0688
2015-09-13
pjp
continue;
0689
2015-09-13
pjp
}
0690
2015-09-13
pjp
0691
2019-02-15
pjp
return (rbt);
0692
2015-09-13
pjp
} while (*p);
0693
2015-09-13
pjp
0694
2015-09-13
pjp
return NULL;
0695
2015-09-13
pjp
}
0696
2015-09-13
pjp
0697
2015-09-13
pjp
char *
0698
2015-09-13
pjp
hash_name(char *name, int len, struct nsec3param *n3p)
0699
2015-09-13
pjp
{
0700
2015-09-13
pjp
SHA_CTX ctx;
0701
2015-09-13
pjp
u_char md[20];
0702
2015-09-13
pjp
int i;
0703
2015-09-13
pjp
0704
2015-09-13
pjp
if (n3p->algorithm != 1) {
0705
2015-09-13
pjp
dolog(LOG_INFO, "wrong algorithm: %d, expected 1\n", n3p->algorithm);
0706
2015-09-13
pjp
return NULL;
0707
2015-09-13
pjp
}
0708
2015-09-13
pjp
0709
2015-09-13
pjp
SHA1_Init(&ctx);
0710
2015-09-13
pjp
SHA1_Update(&ctx, name, len);
0711
2015-09-13
pjp
SHA1_Update(&ctx, n3p->salt, n3p->saltlen);
0712
2015-09-13
pjp
SHA1_Final(md, &ctx);
0713
2015-09-13
pjp
0714
2015-09-13
pjp
for (i = 0; i < n3p->iterations; i++) {
0715
2015-09-13
pjp
SHA1_Init(&ctx);
0716
2015-09-13
pjp
SHA1_Update(&ctx, md, sizeof(md));
0717
2015-09-13
pjp
SHA1_Update(&ctx, n3p->salt, n3p->saltlen);
0718
2015-09-13
pjp
SHA1_Final(md, &ctx);
0719
2015-09-13
pjp
}
0720
2015-09-13
pjp
0721
2016-10-20
pjp
0722
2016-10-20
pjp
return(base32hex_encode(md, sizeof(md)));
0723
2015-09-13
pjp
}
0724
2015-09-13
pjp
0725
2015-11-17
pjp
void
0726
2015-11-17
pjp
mysetbit(u_char *input, int pos)
0727
2015-11-17
pjp
{
0728
2015-11-17
pjp
int bit;
0729
2015-11-17
pjp
int byte;
0730
2015-11-17
pjp
0731
2015-11-17
pjp
byte = pos / 8;
0732
2015-11-17
pjp
bit = pos % 8;
0733
2015-11-17
pjp
0734
2015-11-17
pjp
switch (bit) {
0735
2015-11-17
pjp
case 0:
0736
2015-11-17
pjp
input[byte] |= 128;
0737
2015-11-17
pjp
break;
0738
2015-11-17
pjp
case 1:
0739
2015-11-17
pjp
input[byte] |= 64;
0740
2015-11-17
pjp
break;
0741
2015-11-17
pjp
case 2:
0742
2015-11-17
pjp
input[byte] |= 32;
0743
2015-11-17
pjp
break;
0744
2015-11-17
pjp
case 3:
0745
2015-11-17
pjp
input[byte] |= 16;
0746
2015-11-17
pjp
break;
0747
2015-11-17
pjp
case 4:
0748
2015-11-17
pjp
input[byte] |= 8;
0749
2015-11-17
pjp
break;
0750
2015-11-17
pjp
case 5:
0751
2015-11-17
pjp
input[byte] |= 4;
0752
2015-11-17
pjp
break;
0753
2015-11-17
pjp
case 6:
0754
2015-11-17
pjp
input[byte] |= 2;
0755
2015-11-17
pjp
break;
0756
2015-11-17
pjp
case 7:
0757
2015-11-17
pjp
input[byte] |= 1;
0758
2015-11-17
pjp
break;
0759
2015-11-17
pjp
}
0760
2015-11-17
pjp
0761
2015-11-17
pjp
return;
0762
2015-11-17
pjp
}
0763
2015-11-17
pjp
0764
2015-11-17
pjp
int
0765
2015-11-17
pjp
base32hex_decode(u_char *input, u_char *output)
0766
2015-11-17
pjp
{
0767
2015-11-17
pjp
u_int8_t tmp;
0768
2015-11-17
pjp
u_char *character = "0123456789abcdefghijklmnopqrstuv=";
0769
2015-11-17
pjp
u_char *start = character, *p = character;
0770
2015-11-17
pjp
int i, j;
0771
2015-11-17
pjp
int len;
0772
2015-11-17
pjp
int bit = 0;
0773
2015-11-17
pjp
0774
2015-11-17
pjp
len = (strlen(input) * 5) / 8;
0775
2015-11-17
pjp
0776
2015-11-17
pjp
memset(output, 0, len);
0777
2015-11-17
pjp
0778
2015-11-17
pjp
for (i = 0; i < strlen(input); i++) {
0779
2015-11-17
pjp
if (input[i] == '=')
0780
2015-11-17
pjp
continue;
0781
2015-11-17
pjp
0782
2015-11-17
pjp
input[i] = tolower(input[i]);
0783
2015-11-17
pjp
for (p = character; *p && *p != input[i]; p++);
0784
2015-12-01
pjp
if (*p == '\0')
0785
2015-11-17
pjp
return 0;
0786
2015-11-17
pjp
0787
2015-11-17
pjp
tmp = (p - start);
0788
2015-11-17
pjp
tmp <<= 3;
0789
2015-11-17
pjp
0790
2015-11-17
pjp
for (j = 0; j < 5; j++) {
0791
2015-11-17
pjp
if (tmp & 128)
0792
2015-11-17
pjp
mysetbit(output, bit);
0793
2015-11-17
pjp
0794
2015-11-17
pjp
bit++;
0795
2015-11-17
pjp
tmp <<= 1;
0796
2015-11-17
pjp
}
0797
2015-11-17
pjp
}
0798
2015-11-17
pjp
0799
2015-11-17
pjp
return (len);
0800
2015-11-17
pjp
}
0801
2015-11-17
pjp
0802
2015-11-17
pjp
0803
2015-11-17
pjp
0804
2015-09-13
pjp
char *
0805
2015-09-13
pjp
base32hex_encode(u_char *input, int len)
0806
2015-09-13
pjp
{
0807
2015-09-13
pjp
u_char *ui;
0808
2015-09-13
pjp
u_int64_t tb = 0;
0809
2015-09-13
pjp
int i;
0810
2015-09-13
pjp
u_char *p;
0811
2019-07-05
pjp
static char ret[64];
0812
2015-09-13
pjp
0813
2015-09-13
pjp
u_char *character = "0123456789abcdefghijklmnopqrstuv=";
0814
2015-09-13
pjp
0815
2015-11-19
pjp
memset(&ret, 0, sizeof(ret));
0816
2015-09-13
pjp
p = &ret[0];
0817
2015-09-13
pjp
ui = input;
0818
2015-09-13
pjp
0819
2015-09-13
pjp
for (i = 0; i < len; i += 5) {
0820
2015-09-13
pjp
tb = (*ui & 0xff);
0821
2015-09-13
pjp
tb <<= 8;
0822
2015-09-13
pjp
0823
2015-09-13
pjp
if (i < len)
0824
2015-09-13
pjp
ui++;
0825
2015-09-13
pjp
else
0826
2015-09-13
pjp
*ui = 0;
0827
2015-09-13
pjp
0828
2015-09-13
pjp
tb |= (*ui & 0xff);
0829
2015-09-13
pjp
tb <<= 8;
0830
2015-09-13
pjp
0831
2015-09-13
pjp
if (i < len)
0832
2015-09-13
pjp
ui++;
0833
2015-09-13
pjp
else
0834
2015-09-13
pjp
*ui = 0;
0835
2015-09-13
pjp
0836
2015-09-13
pjp
tb |= (*ui & 0xff);
0837
2015-09-13
pjp
0838
2015-09-13
pjp
tb <<= 8;
0839
2015-09-13
pjp
0840
2015-09-13
pjp
if (i < len)
0841
2015-09-13
pjp
ui++;
0842
2015-09-13
pjp
else
0843
2015-09-13
pjp
*ui = 0;
0844
2015-09-13
pjp
0845
2015-09-13
pjp
tb |= (*ui & 0xff);
0846
2015-09-13
pjp
0847
2015-09-13
pjp
tb <<= 8;
0848
2015-09-13
pjp
0849
2015-09-13
pjp
if (i < len)
0850
2015-09-13
pjp
ui++;
0851
2015-09-13
pjp
else
0852
2015-09-13
pjp
*ui = 0;
0853
2015-09-13
pjp
0854
2015-09-13
pjp
tb |= (*ui & 0xff);
0855
2015-09-13
pjp
0856
2015-09-13
pjp
if (i < len)
0857
2015-09-13
pjp
ui++;
0858
2015-09-13
pjp
else
0859
2015-09-13
pjp
*ui = 0;
0860
2015-09-13
pjp
0861
2015-09-13
pjp
*(p + 7) = character[(tb & 0x1f)];
0862
2015-09-13
pjp
tb >>= 5;
0863
2015-09-13
pjp
*(p + 6) = character[(tb & 0x1f)];
0864
2015-09-13
pjp
tb >>= 5;
0865
2015-09-13
pjp
*(p + 5) = character[(tb & 0x1f)];
0866
2015-09-13
pjp
tb >>= 5;
0867
2015-09-13
pjp
*(p + 4) = character[(tb & 0x1f)];
0868
2015-09-13
pjp
tb >>= 5;
0869
2015-09-13
pjp
*(p + 3) = character[(tb & 0x1f)];
0870
2015-09-13
pjp
tb >>= 5;
0871
2015-09-13
pjp
*(p + 2) = character[(tb & 0x1f)];
0872
2015-09-13
pjp
tb >>= 5;
0873
2015-09-13
pjp
*(p + 1) = character[(tb & 0x1f)];
0874
2015-09-13
pjp
tb >>= 5;
0875
2015-09-13
pjp
*(p + 0) = character[(tb & 0x1f)];
0876
2015-09-13
pjp
0877
2015-09-13
pjp
p += 8;
0878
2015-09-13
pjp
}
0879
2015-09-13
pjp
0880
2015-09-13
pjp
return (ret);
0881
2015-09-13
pjp
}
0882
2015-09-13
pjp
0883
2015-09-13
pjp
/*
0884
2015-09-13
pjp
* FIND_NSEC3_MATCH_CLOSEST - find the closest matching encloser
0885
2015-09-13
pjp
*
0886
2015-09-13
pjp
*/
0887
2015-09-13
pjp
0888
2019-02-15
pjp
struct rbtree *
0889
2019-02-15
pjp
find_nsec3_match_closest(char *name, int namelen, struct rbtree *rbt, ddDB *db)
0890
2015-09-13
pjp
{
0891
2015-09-13
pjp
char *hashname;
0892
2015-09-13
pjp
char *backname;
0893
2015-09-14
pjp
char *dname;
0894
2015-09-13
pjp
int backnamelen;
0895
2019-02-15
pjp
struct rbtree *rbt0;
0896
2019-02-15
pjp
struct rrset *rrset = NULL;
0897
2019-02-15
pjp
struct rr *rrp = NULL;
0898
2015-09-13
pjp
0899
2019-02-15
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_NSEC3PARAM)) == NULL) {
0900
2015-09-13
pjp
return NULL;
0901
2015-09-13
pjp
}
0902
2019-02-15
pjp
if ((rrp = TAILQ_FIRST(&rrset->rr_head)) == NULL) {
0903
2019-02-15
pjp
return NULL;
0904
2019-02-15
pjp
}
0905
2015-09-13
pjp
0906
2015-09-13
pjp
/* first off find the next closer record */
0907
2019-02-15
pjp
rbt0 = find_closest_encloser(db, name, namelen);
0908
2019-02-15
pjp
if (rbt0 == NULL) {
0909
2015-09-13
pjp
return NULL;
0910
2015-09-13
pjp
}
0911
2015-09-13
pjp
0912
2015-09-13
pjp
#if DEBUG
0913
2019-02-15
pjp
dolog(LOG_INFO, "next closer = %s\n", rbt0->humanname);
0914
2015-09-13
pjp
#endif
0915
2015-09-13
pjp
0916
2019-02-15
pjp
hashname = hash_name(rbt0->zone, rbt0->zonelen, (struct nsec3param *)rrp->rdata);
0917
2015-09-13
pjp
if (hashname == NULL) {
0918
2015-09-13
pjp
dolog(LOG_INFO, "unable to get hashname\n");
0919
2015-09-13
pjp
return NULL;
0920
2015-09-13
pjp
}
0921
2015-09-13
pjp
0922
2015-09-13
pjp
#if DEBUG
0923
2015-09-13
pjp
dolog(LOG_INFO, "hashname = %s\n", hashname);
0924
2015-09-13
pjp
#endif
0925
2019-02-15
pjp
dname = find_match_nsec3(rbt->zone, rbt->zonelen, hashname);
0926
2015-09-13
pjp
0927
2015-09-14
pjp
if (dname == NULL) {
0928
2015-09-13
pjp
return NULL;
0929
2015-09-13
pjp
}
0930
2015-09-13
pjp
0931
2015-09-13
pjp
/* found it, get it via db after converting it */
0932
2015-09-13
pjp
0933
2015-09-14
pjp
#ifdef DEBUG
0934
2015-09-14
pjp
dolog(LOG_INFO, "converting %s\n", dname);
0935
2015-09-14
pjp
#endif
0936
2015-09-14
pjp
backname = dns_label(dname, &backnamelen);
0937
2015-09-13
pjp
0938
2019-02-15
pjp
rbt0 = find_rrset(db, backname, backnamelen);
0939
2019-02-15
pjp
if (rbt0 == NULL) {
0940
2015-09-13
pjp
free (backname);
0941
2015-09-13
pjp
return (NULL);
0942
2015-09-13
pjp
}
0943
2015-09-13
pjp
0944
2015-09-13
pjp
free (backname);
0945
2015-09-13
pjp
0946
2015-09-14
pjp
#ifdef DEBUG
0947
2019-02-15
pjp
dolog(LOG_INFO, "returning %s\n", rbt0->humanname);
0948
2015-09-14
pjp
#endif
0949
2019-02-15
pjp
return (rbt0);
0950
2015-09-13
pjp
}
0951
2015-09-13
pjp
0952
2015-09-13
pjp
/*
0953
2015-09-13
pjp
* FIND_NSEC3_WILDCARD_CLOSEST - finds the right nsec3 domainname in a zone
0954
2015-09-13
pjp
*
0955
2015-09-13
pjp
*/
0956
2019-02-15
pjp
struct rbtree *
0957
2019-02-15
pjp
find_nsec3_wildcard_closest(char *name, int namelen, struct rbtree *rbt, ddDB *db)
0958
2015-09-13
pjp
{
0959
2020-04-11
pjp
struct rbtree *rbt0 = NULL;
0960
2020-04-11
pjp
struct rrset *rrset = NULL;
0961
2020-04-11
pjp
struct rr *rrp = NULL;
0962
2020-04-11
pjp
0963
2015-09-13
pjp
char *hashname;
0964
2015-09-13
pjp
char *backname;
0965
2015-09-14
pjp
char *dname;
0966
2020-04-11
pjp
char *p;
0967
2015-09-13
pjp
char wildcard[DNS_MAXNAME + 1];
0968
2020-04-11
pjp
0969
2015-09-13
pjp
int backnamelen;
0970
2020-04-11
pjp
int ret;
0971
2015-09-13
pjp
0972
2019-02-15
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_NSEC3PARAM)) == NULL) {
0973
2015-09-13
pjp
return NULL;
0974
2015-09-13
pjp
}
0975
2019-02-15
pjp
if ((rrp = TAILQ_FIRST(&rrset->rr_head)) == NULL) {
0976
2019-02-15
pjp
return NULL;
0977
2019-02-15
pjp
}
0978
2015-09-13
pjp
0979
2015-09-13
pjp
/* first off find the next closer record */
0980
2019-02-15
pjp
rbt0 = find_closest_encloser(db, name, namelen);
0981
2019-02-15
pjp
if (rbt0 == NULL) {
0982
2015-09-13
pjp
return NULL;
0983
2015-09-13
pjp
}
0984
2015-09-13
pjp
0985
2015-09-13
pjp
#if DEBUG
0986
2019-02-15
pjp
dolog(LOG_INFO, "next closer = %s\n", rbt0->humanname);
0987
2015-09-13
pjp
#endif
0988
2020-04-11
pjp
p = rbt0->humanname;
0989
2020-04-11
pjp
ret = snprintf(wildcard, sizeof(wildcard), "*.%s", p);
0990
2020-04-11
pjp
if (ret >= sizeof(wildcard)) {
0991
2020-04-11
pjp
dolog(LOG_INFO, "result was truncated\n");
0992
2020-04-11
pjp
return NULL;
0993
2020-04-11
pjp
}
0994
2015-09-13
pjp
0995
2015-09-13
pjp
backname = dns_label(wildcard, &backnamelen);
0996
2015-09-13
pjp
0997
2019-02-15
pjp
hashname = hash_name(backname, backnamelen, (struct nsec3param *)rrp->rdata);
0998
2015-09-13
pjp
if (hashname == NULL) {
0999
2015-09-13
pjp
dolog(LOG_INFO, "unable to get hashname\n");
1000
2015-09-13
pjp
return NULL;
1001
2015-09-13
pjp
}
1002
2015-09-13
pjp
1003
2015-09-13
pjp
#if DEBUG
1004
2015-09-13
pjp
dolog(LOG_INFO, "hashname = %s\n", hashname);
1005
2015-09-13
pjp
#endif
1006
2015-09-13
pjp
1007
2019-02-15
pjp
dname = find_next_closer_nsec3(rbt->zone, rbt->zonelen, hashname);
1008
2015-09-13
pjp
1009
2015-09-13
pjp
/* found it, get it via db after converting it */
1010
2020-07-08
pjp
1011
2015-09-14
pjp
#ifdef DEBUG
1012
2015-09-14
pjp
dolog(LOG_INFO, "converting %s\n", dname);
1013
2015-09-14
pjp
#endif
1014
2015-09-14
pjp
backname = dns_label(dname, &backnamelen);
1015
2015-09-13
pjp
1016
2019-02-15
pjp
rbt0 = find_rrset(db, backname, backnamelen);
1017
2019-02-15
pjp
if (rbt0 == NULL) {
1018
2015-09-13
pjp
free (backname);
1019
2015-09-13
pjp
return (NULL);
1020
2015-09-13
pjp
}
1021
2015-09-13
pjp
1022
2015-09-13
pjp
1023
2015-09-13
pjp
free (backname);
1024
2015-09-13
pjp
1025
2015-09-14
pjp
#ifdef DEBUG
1026
2019-02-15
pjp
dolog(LOG_INFO, "returning %s\n", rbt0->humanname);
1027
2015-09-14
pjp
#endif
1028
2019-02-15
pjp
return (rbt0);
1029
2015-09-13
pjp
}
1030
2015-09-13
pjp
1031
2015-09-13
pjp
/*
1032
2015-09-13
pjp
* FIND_NSEC3_COVER_NEXT_CLOSER - finds the right nsec3 domainname in a zone
1033
2015-09-13
pjp
*
1034
2015-09-13
pjp
*/
1035
2019-02-15
pjp
struct rbtree *
1036
2019-02-15
pjp
find_nsec3_cover_next_closer(char *name, int namelen, struct rbtree *rbt, ddDB *db)
1037
2015-09-13
pjp
{
1038
2015-09-13
pjp
char *hashname;
1039
2015-09-13
pjp
char *backname;
1040
2015-09-14
pjp
char *dname;
1041
2015-09-13
pjp
int backnamelen;
1042
2019-02-15
pjp
struct rrset *rrset = NULL;
1043
2019-02-15
pjp
struct rr *rrp = NULL;
1044
2016-01-22
pjp
char *ncn;
1045
2016-01-22
pjp
int ncnlen;
1046
2019-02-15
pjp
struct rbtree *rbt0;
1047
2015-09-13
pjp
1048
2019-02-15
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_NSEC3PARAM)) == NULL) {
1049
2015-09-13
pjp
return NULL;
1050
2015-09-13
pjp
}
1051
2019-02-15
pjp
if ((rrp = TAILQ_FIRST(&rrset->rr_head)) == NULL) {
1052
2019-02-15
pjp
return NULL;
1053
2019-02-15
pjp
}
1054
2015-09-13
pjp
1055
2015-09-13
pjp
/* first off find the next closer record */
1056
2019-02-15
pjp
rbt0 = find_closest_encloser(db, name, namelen);
1057
2019-02-15
pjp
if (rbt0 == NULL) {
1058
2015-09-13
pjp
return NULL;
1059
2015-09-13
pjp
}
1060
2015-09-13
pjp
1061
2019-02-15
pjp
ncn = find_next_closer_name(name, namelen, rbt0->zone, rbt0->zonelen, &ncnlen);
1062
2017-09-06
pjp
if (ncn == NULL) {
1063
2016-01-22
pjp
return NULL;
1064
2017-09-06
pjp
}
1065
2015-09-13
pjp
1066
2019-02-15
pjp
hashname = hash_name(ncn, ncnlen, (struct nsec3param *)rrp->rdata);
1067
2015-09-13
pjp
if (hashname == NULL) {
1068
2015-09-13
pjp
dolog(LOG_INFO, "unable to get hashname\n");
1069
2015-09-13
pjp
return NULL;
1070
2015-09-13
pjp
}
1071
2015-09-13
pjp
1072
2015-09-13
pjp
#if DEBUG
1073
2015-09-13
pjp
dolog(LOG_INFO, "hashname = %s\n", hashname);
1074
2015-09-13
pjp
#endif
1075
2015-09-13
pjp
1076
2015-09-14
pjp
/* free what we don't need */
1077
2015-09-13
pjp
1078
2019-02-15
pjp
dname = find_next_closer_nsec3(rbt->zone, rbt->zonelen, hashname);
1079
2015-09-14
pjp
if (dname == NULL)
1080
2015-09-13
pjp
return NULL;
1081
2015-09-13
pjp
1082
2015-09-13
pjp
1083
2015-09-14
pjp
#ifdef DEBUG
1084
2015-09-14
pjp
dolog(LOG_INFO, "converting %s\n", dname);
1085
2015-09-14
pjp
#endif
1086
2015-09-13
pjp
1087
2015-09-14
pjp
backname = dns_label(dname, &backnamelen);
1088
2015-09-13
pjp
1089
2019-02-15
pjp
if ((rbt0 = find_rrset(db, backname, backnamelen)) == NULL) {
1090
2015-09-13
pjp
free (backname);
1091
2015-09-13
pjp
return (NULL);
1092
2015-09-13
pjp
}
1093
2015-09-13
pjp
1094
2015-09-13
pjp
1095
2015-09-13
pjp
free (backname);
1096
2015-09-13
pjp
1097
2015-09-14
pjp
#ifdef DEBUG
1098
2019-02-15
pjp
dolog(LOG_INFO, "returning %s\n", rbt0->humanname);
1099
2015-09-14
pjp
#endif
1100
2015-09-14
pjp
1101
2019-02-15
pjp
return (rbt0);
1102
2015-09-13
pjp
}
1103
2015-09-13
pjp
1104
2015-09-13
pjp
/*
1105
2015-09-13
pjp
* FIND_NSEC3_MATCH_QNAME - find the matching QNAME and return NSEC3
1106
2015-09-13
pjp
*
1107
2015-09-13
pjp
*/
1108
2015-09-13
pjp
1109
2019-02-15
pjp
struct rbtree *
1110
2019-02-15
pjp
find_nsec3_match_qname(char *name, int namelen, struct rbtree *rbt, ddDB *db)
1111
2015-09-13
pjp
{
1112
2015-09-13
pjp
char *hashname;
1113
2015-09-13
pjp
char *backname;
1114
2015-09-14
pjp
char *dname;
1115
2015-09-13
pjp
int backnamelen;
1116
2019-02-18
pjp
struct rbtree *rbt0 = NULL;
1117
2019-02-15
pjp
struct rrset *rrset = NULL;
1118
2019-02-15
pjp
struct rr *rrp = NULL;
1119
2015-09-13
pjp
1120
2019-02-18
pjp
1121
2019-02-15
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_NSEC3PARAM)) == NULL) {
1122
2015-09-13
pjp
return NULL;
1123
2015-09-13
pjp
}
1124
2019-02-15
pjp
if ((rrp = TAILQ_FIRST(&rrset->rr_head)) == NULL) {
1125
2019-02-15
pjp
return NULL;
1126
2019-02-15
pjp
}
1127
2015-09-13
pjp
1128
2019-02-15
pjp
hashname = hash_name(name, namelen, (struct nsec3param *)rrp->rdata);
1129
2015-09-13
pjp
if (hashname == NULL) {
1130
2015-09-13
pjp
dolog(LOG_INFO, "unable to get hashname\n");
1131
2015-09-13
pjp
return NULL;
1132
2015-09-13
pjp
}
1133
2015-09-13
pjp
1134
2015-09-13
pjp
#if DEBUG
1135
2015-09-13
pjp
dolog(LOG_INFO, "hashname = %s\n", hashname);
1136
2015-09-13
pjp
#endif
1137
2017-01-11
pjp
1138
2019-02-18
pjp
#if 0
1139
2017-01-11
pjp
if (check_ent(name, namelen))
1140
2019-02-15
pjp
dname = find_match_nsec3_ent(rbt->zone, rbt->zonelen, hashname);
1141
2017-01-11
pjp
else
1142
2019-02-18
pjp
#endif
1143
2019-02-18
pjp
1144
2019-02-18
pjp
dname = find_match_nsec3(rbt->zone, rbt->zonelen, hashname);
1145
2015-09-13
pjp
1146
2019-02-18
pjp
if (dname == NULL) {
1147
2015-09-13
pjp
return NULL;
1148
2019-02-18
pjp
}
1149
2015-09-13
pjp
1150
2015-09-13
pjp
/* found it, get it via db after converting it */
1151
2015-09-13
pjp
1152
2015-09-14
pjp
#if DEBUG
1153
2015-09-14
pjp
dolog(LOG_INFO, "converting %s\n", dname);
1154
2015-09-14
pjp
#endif
1155
2015-09-13
pjp
1156
2015-09-14
pjp
backname = dns_label(dname, &backnamelen);
1157
2019-02-18
pjp
if (backname == NULL) {
1158
2019-02-18
pjp
return NULL;
1159
2019-02-18
pjp
}
1160
2019-02-18
pjp
1161
2019-02-15
pjp
rbt0 = find_rrset(db, backname, backnamelen);
1162
2019-02-15
pjp
if (rbt0 == NULL) {
1163
2015-09-13
pjp
free (backname);
1164
2015-09-13
pjp
return (NULL);
1165
2015-09-13
pjp
}
1166
2015-09-13
pjp
1167
2015-09-13
pjp
1168
2015-09-13
pjp
free (backname);
1169
2015-09-13
pjp
1170
2015-09-14
pjp
#ifdef DEBUG
1171
2019-02-15
pjp
dolog(LOG_INFO, "returning %s\n", rbt0->humanname);
1172
2015-09-14
pjp
#endif
1173
2015-09-14
pjp
1174
2019-02-15
pjp
return (rbt0);
1175
2015-09-13
pjp
}
repomaster@centroid.eu