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
2016-07-06
pjp
/*
0002
2021-01-04
pjp
* Copyright (c) 2002-2021 Peter J. Philipp
0003
2016-07-06
pjp
* All rights reserved.
0004
2016-07-06
pjp
*
0005
2016-07-06
pjp
* Redistribution and use in source and binary forms, with or without
0006
2016-07-06
pjp
* modification, are permitted provided that the following conditions
0007
2016-07-06
pjp
* are met:
0008
2016-07-06
pjp
* 1. Redistributions of source code must retain the above copyright
0009
2016-07-06
pjp
* notice, this list of conditions and the following disclaimer.
0010
2016-07-06
pjp
* 2. Redistributions in binary form must reproduce the above copyright
0011
2016-07-06
pjp
* notice, this list of conditions and the following disclaimer in the
0012
2016-07-06
pjp
* documentation and/or other materials provided with the distribution.
0013
2016-07-06
pjp
* 3. The name of the author may not be used to endorse or promote products
0014
2016-07-06
pjp
* derived from this software without specific prior written permission
0015
2016-07-06
pjp
*
0016
2016-07-06
pjp
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017
2016-07-06
pjp
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018
2016-07-06
pjp
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019
2016-07-06
pjp
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020
2016-07-06
pjp
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021
2016-07-06
pjp
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022
2016-07-06
pjp
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023
2016-07-06
pjp
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024
2016-07-06
pjp
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025
2016-07-06
pjp
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026
2016-07-06
pjp
*
0027
2016-07-06
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
2017-10-26
pjp
0032
2019-06-06
pjp
#include <netinet/in.h>
0033
2020-07-21
pjp
#include <netinet/ip.h>
0034
2020-07-21
pjp
#include <netinet/ip6.h>
0035
2020-07-21
pjp
#include <netinet/udp.h>
0036
2019-06-06
pjp
#include <arpa/inet.h>
0037
2019-06-06
pjp
#include <netdb.h>
0038
2019-02-24
pjp
0039
2019-06-06
pjp
#include <stdio.h>
0040
2019-06-06
pjp
#include <stdlib.h>
0041
2019-06-06
pjp
#include <stdint.h>
0042
2019-06-06
pjp
#include <stdarg.h>
0043
2019-06-06
pjp
#include <string.h>
0044
2019-06-06
pjp
#include <errno.h>
0045
2019-06-06
pjp
#include <syslog.h>
0046
2019-06-06
pjp
#include <unistd.h>
0047
2019-06-06
pjp
#include <ctype.h>
0048
2019-06-06
pjp
0049
2019-06-06
pjp
#ifdef __linux__
0050
2019-06-06
pjp
#include <grp.h>
0051
2019-06-06
pjp
#define __USE_BSD 1
0052
2019-06-06
pjp
#include <endian.h>
0053
2019-06-06
pjp
#include <bsd/stdlib.h>
0054
2019-06-06
pjp
#include <bsd/string.h>
0055
2019-06-06
pjp
#include <bsd/unistd.h>
0056
2019-06-06
pjp
#include <bsd/sys/queue.h>
0057
2019-06-06
pjp
#define __unused
0058
2019-06-06
pjp
#include <bsd/sys/tree.h>
0059
2019-06-06
pjp
#include <bsd/sys/endian.h>
0060
2019-06-06
pjp
#include "imsg.h"
0061
2019-06-06
pjp
#else /* not linux */
0062
2019-06-06
pjp
#include <sys/queue.h>
0063
2019-06-06
pjp
#include <sys/tree.h>
0064
2019-06-06
pjp
#ifdef __FreeBSD__
0065
2019-06-07
pjp
#include <sys/endian.h>
0066
2019-06-06
pjp
#include "imsg.h"
0067
2019-06-06
pjp
#else
0068
2019-06-06
pjp
#include <imsg.h>
0069
2019-06-06
pjp
#endif /* __FreeBSD__ */
0070
2019-06-06
pjp
#endif /* __linux__ */
0071
2019-06-06
pjp
0072
2019-06-06
pjp
#ifndef NTOHS
0073
2019-06-06
pjp
#include "endian.h"
0074
2019-06-06
pjp
#endif
0075
2019-06-06
pjp
0076
2019-02-24
pjp
#include <openssl/evp.h>
0077
2019-02-24
pjp
#include <openssl/hmac.h>
0078
2019-02-24
pjp
0079
2019-06-06
pjp
#include "ddd-dns.h"
0080
2019-06-06
pjp
#include "ddd-db.h"
0081
2019-06-06
pjp
#include "ddd-config.h"
0082
2019-06-06
pjp
0083
2016-07-06
pjp
/* prototypes */
0084
2016-07-06
pjp
0085
2019-12-03
pjp
void pack(char *, char *, int);
0086
2019-12-03
pjp
void pack32(char *, u_int32_t);
0087
2019-12-03
pjp
void pack16(char *, u_int16_t);
0088
2019-12-03
pjp
void pack8(char *, u_int8_t);
0089
2019-12-03
pjp
uint32_t unpack32(char *);
0090
2019-12-03
pjp
uint16_t unpack16(char *);
0091
2019-12-03
pjp
void unpack(char *, char *, int);
0092
2020-07-16
pjp
int lower_dnsname(char *, int);
0093
2020-07-16
pjp
int randomize_dnsname(char *, int);
0094
2019-12-03
pjp
0095
2016-08-28
pjp
int label_count(char *);
0096
2016-07-06
pjp
char * dns_label(char *, int *);
0097
2020-06-25
pjp
void ddd_shutdown(void);
0098
2017-06-26
pjp
int get_record_size(ddDB *, char *, int);
0099
2019-11-11
pjp
struct rbtree * get_soa(ddDB *, struct question *);
0100
2019-11-11
pjp
struct rbtree * get_ns(ddDB *, struct rbtree *, int *);
0101
2020-01-16
pjp
struct rbtree * lookup_zone(ddDB *, struct question *, int *, int *, char *, int);
0102
2019-10-31
pjp
struct rbtree * Lookup_zone(ddDB *, char *, u_int16_t, u_int16_t, int);
0103
2019-02-15
pjp
u_int16_t check_qtype(struct rbtree *, u_int16_t, int, int *);
0104
2019-02-26
pjp
struct question *build_fake_question(char *, int, u_int16_t, char *, int);
0105
2016-07-06
pjp
0106
2016-08-30
pjp
char *get_dns_type(int, int);
0107
2017-01-11
pjp
int memcasecmp(u_char *, u_char *, int);
0108
2021-01-04
pjp
int compress_label(u_char *, u_int16_t, int);
0109
2019-02-26
pjp
struct question *build_question(char *, int, int, char *);
0110
2019-02-07
pjp
int free_question(struct question *);
0111
2019-02-07
pjp
struct rrtab *rrlookup(char *);
0112
2019-02-24
pjp
char * expand_compression(u_char *, u_char *, u_char *, u_char *, int *, int);
0113
2019-02-24
pjp
void log_diff(char *sha256, char *mac, int len);
0114
2019-10-15
pjp
int tsig_pseudoheader(char *, uint16_t, time_t, HMAC_CTX *);
0115
2019-11-01
pjp
char * bin2hex(char *, int);
0116
2019-11-01
pjp
u_int64_t timethuman(time_t);
0117
2019-11-01
pjp
char * bitmap2human(char *, int);
0118
2020-09-24
pjp
int lookup_axfr(FILE *, int, char *, struct soa *, u_int32_t, char *, char *, int *, int *, int *, struct soa_constraints *, uint32_t);
0119
2019-11-12
pjp
int dn_contains(char *name, int len, char *anchorname, int alen);
0120
2020-07-21
pjp
uint16_t udp_cksum(u_int16_t *, uint16_t, struct ip *, struct udphdr *);
0121
2020-07-21
pjp
uint16_t udp_cksum6(u_int16_t *, uint16_t, struct ip6_hdr *, struct udphdr *);
0122
2016-07-06
pjp
0123
2020-04-11
pjp
0124
2019-11-05
pjp
int bytes_received;
0125
2019-11-02
pjp
0126
2019-11-02
pjp
/* externs */
0127
2016-07-06
pjp
extern int debug;
0128
2016-07-06
pjp
extern int *ptr;
0129
2019-02-24
pjp
extern int tsig;
0130
2020-06-30
pjp
extern int forward;
0131
2020-07-03
pjp
extern int zonecount;
0132
2016-07-06
pjp
0133
2019-02-15
pjp
extern void dolog(int, char *, ...);
0134
2019-02-15
pjp
0135
2019-02-15
pjp
extern struct rbtree * find_rrset(ddDB *db, char *name, int len);
0136
2019-02-15
pjp
extern struct rrset * find_rr(struct rbtree *rbt, u_int16_t rrtype);
0137
2019-02-15
pjp
extern int add_rr(struct rbtree *rbt, char *name, int len, u_int16_t rrtype, void *rdata);
0138
2019-02-15
pjp
extern int display_rr(struct rrset *rrset);
0139
2019-02-18
pjp
extern int check_ent(char *, int);
0140
2019-02-24
pjp
extern int find_tsig_key(char *, int, char *, int);
0141
2019-11-02
pjp
extern int mybase64_decode(char const *, u_char *, size_t);
0142
2019-02-15
pjp
0143
2020-07-08
pjp
extern int raxfr_a(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0144
2020-07-08
pjp
extern int raxfr_tlsa(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0145
2020-07-08
pjp
extern int raxfr_srv(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0146
2020-07-08
pjp
extern int raxfr_naptr(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0147
2020-07-08
pjp
extern int raxfr_aaaa(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0148
2020-07-08
pjp
extern int raxfr_cname(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0149
2020-07-08
pjp
extern int raxfr_ns(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0150
2020-07-08
pjp
extern int raxfr_ptr(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0151
2020-07-08
pjp
extern int raxfr_mx(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0152
2020-07-08
pjp
extern int raxfr_txt(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0153
2020-07-08
pjp
extern int raxfr_dnskey(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0154
2020-07-08
pjp
extern int raxfr_rrsig(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0155
2020-07-08
pjp
extern int raxfr_nsec3param(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0156
2020-07-08
pjp
extern int raxfr_nsec3(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0157
2020-07-08
pjp
extern int raxfr_ds(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0158
2020-07-23
pjp
extern int raxfr_rp(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0159
2020-07-23
pjp
extern int raxfr_caa(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0160
2020-07-23
pjp
extern int raxfr_hinfo(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0161
2020-07-08
pjp
extern int raxfr_sshfp(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
0162
2019-11-02
pjp
extern u_int16_t raxfr_skip(FILE *, u_char *, u_char *);
0163
2020-07-27
pjp
extern int raxfr_soa(FILE *, u_char *, u_char *, u_char *, struct soa *, int, u_int32_t, u_int16_t, HMAC_CTX *, struct soa_constraints *);
0164
2020-07-27
pjp
extern int raxfr_peek(FILE *, u_char *, u_char *, u_char *, int *, int, u_int16_t *, u_int32_t, HMAC_CTX *, char *, int, int);
0165
2019-11-05
pjp
extern int raxfr_tsig(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *, char *, int);
0166
2020-07-16
pjp
extern char *convert_name(char *, int);
0167
2019-02-15
pjp
0168
2019-11-02
pjp
0169
2016-08-30
pjp
/* internals */
0170
2016-08-30
pjp
struct typetable {
0171
2016-08-30
pjp
char *type;
0172
2016-08-30
pjp
int number;
0173
2016-08-30
pjp
} TT[] = {
0174
2016-08-30
pjp
{ "A", DNS_TYPE_A},
0175
2016-08-30
pjp
{ "NS", DNS_TYPE_NS},
0176
2016-08-30
pjp
{ "CNAME", DNS_TYPE_CNAME},
0177
2016-08-30
pjp
{ "SOA", DNS_TYPE_SOA},
0178
2016-08-30
pjp
{ "PTR", DNS_TYPE_PTR},
0179
2016-08-30
pjp
{ "MX", DNS_TYPE_MX},
0180
2016-08-30
pjp
{ "TXT", DNS_TYPE_TXT},
0181
2016-08-30
pjp
{ "AAAA", DNS_TYPE_AAAA},
0182
2016-08-30
pjp
{ "ANY", DNS_TYPE_ANY },
0183
2016-08-30
pjp
{ "SRV", DNS_TYPE_SRV },
0184
2016-08-30
pjp
{ "SSHFP", DNS_TYPE_SSHFP },
0185
2016-08-30
pjp
{ "NAPTR", DNS_TYPE_NAPTR },
0186
2016-08-30
pjp
{ "RRSIG", DNS_TYPE_RRSIG },
0187
2016-08-30
pjp
{ "DNSKEY", DNS_TYPE_DNSKEY },
0188
2016-08-30
pjp
{ "NSEC", DNS_TYPE_NSEC },
0189
2016-08-30
pjp
{ "DS", DNS_TYPE_DS },
0190
2016-08-30
pjp
{ "NSEC3", DNS_TYPE_NSEC3 },
0191
2016-08-30
pjp
{ "NSEC3PARAM", DNS_TYPE_NSEC3PARAM },
0192
2016-08-30
pjp
{ "TLSA", DNS_TYPE_TLSA },
0193
2020-07-23
pjp
{ "RP", DNS_TYPE_RP },
0194
2020-07-23
pjp
{ "HINFO", DNS_TYPE_HINFO },
0195
2020-07-23
pjp
{ "CAA", DNS_TYPE_CAA },
0196
2016-08-30
pjp
{ NULL, 0}
0197
2016-08-30
pjp
};
0198
2016-08-30
pjp
0199
2019-02-15
pjp
static struct rrtab myrrtab[] = {
0200
2019-02-15
pjp
{ "a", DNS_TYPE_A, DNS_TYPE_A } ,
0201
2019-02-15
pjp
{ "aaaa", DNS_TYPE_AAAA, DNS_TYPE_AAAA },
0202
2020-07-23
pjp
{ "caa", DNS_TYPE_CAA, DNS_TYPE_CAA },
0203
2019-02-15
pjp
{ "cname", DNS_TYPE_CNAME, DNS_TYPE_CNAME },
0204
2019-02-15
pjp
{ "delegate", DNS_TYPE_NS, DNS_TYPE_NS },
0205
2019-02-15
pjp
{ "dnskey", DNS_TYPE_DNSKEY, DNS_TYPE_DNSKEY },
0206
2019-02-15
pjp
{ "ds", DNS_TYPE_DS, DNS_TYPE_DS },
0207
2020-07-23
pjp
{ "hinfo", DNS_TYPE_HINFO, DNS_TYPE_HINFO },
0208
2019-02-15
pjp
{ "hint", DNS_TYPE_HINT, DNS_TYPE_NS },
0209
2019-02-15
pjp
{ "mx", DNS_TYPE_MX, DNS_TYPE_MX },
0210
2019-02-15
pjp
{ "naptr", DNS_TYPE_NAPTR, DNS_TYPE_NAPTR },
0211
2019-02-15
pjp
{ "ns", DNS_TYPE_NS, DNS_TYPE_NS },
0212
2019-02-15
pjp
{ "nsec", DNS_TYPE_NSEC, DNS_TYPE_NSEC },
0213
2019-02-15
pjp
{ "nsec3", DNS_TYPE_NSEC3, DNS_TYPE_NSEC3 },
0214
2019-02-15
pjp
{ "nsec3param", DNS_TYPE_NSEC3PARAM, DNS_TYPE_NSEC3PARAM },
0215
2019-02-15
pjp
{ "ptr", DNS_TYPE_PTR, DNS_TYPE_PTR },
0216
2020-07-23
pjp
{ "rp", DNS_TYPE_RP, DNS_TYPE_RP },
0217
2019-02-18
pjp
{ "rrsig", DNS_TYPE_RRSIG, DNS_TYPE_RRSIG },
0218
2019-02-15
pjp
{ "soa", DNS_TYPE_SOA, DNS_TYPE_SOA },
0219
2019-02-15
pjp
{ "srv", DNS_TYPE_SRV, DNS_TYPE_SRV },
0220
2019-02-15
pjp
{ "sshfp", DNS_TYPE_SSHFP, DNS_TYPE_SSHFP },
0221
2019-02-15
pjp
{ "tlsa", DNS_TYPE_TLSA, DNS_TYPE_TLSA },
0222
2019-02-15
pjp
{ "txt", DNS_TYPE_TXT, DNS_TYPE_TXT },
0223
2019-02-07
pjp
};
0224
2019-02-07
pjp
0225
2019-11-02
pjp
0226
2019-11-02
pjp
0227
2019-11-02
pjp
static struct raxfr_logic supported[] = {
0228
2019-11-02
pjp
{ DNS_TYPE_A, 0, raxfr_a },
0229
2019-11-02
pjp
{ DNS_TYPE_NS, 0, raxfr_ns },
0230
2019-11-02
pjp
{ DNS_TYPE_MX, 0, raxfr_mx },
0231
2019-11-02
pjp
{ DNS_TYPE_PTR, 0, raxfr_ptr },
0232
2019-11-02
pjp
{ DNS_TYPE_AAAA, 0, raxfr_aaaa },
0233
2019-11-02
pjp
{ DNS_TYPE_CNAME, 0, raxfr_cname },
0234
2019-11-02
pjp
{ DNS_TYPE_TXT, 0, raxfr_txt },
0235
2019-11-02
pjp
{ DNS_TYPE_DNSKEY, 1, raxfr_dnskey },
0236
2019-11-02
pjp
{ DNS_TYPE_RRSIG, 1, raxfr_rrsig },
0237
2019-11-02
pjp
{ DNS_TYPE_NSEC3PARAM, 1, raxfr_nsec3param },
0238
2019-11-02
pjp
{ DNS_TYPE_NSEC3, 1, raxfr_nsec3 },
0239
2019-11-02
pjp
{ DNS_TYPE_DS, 1, raxfr_ds },
0240
2019-11-02
pjp
{ DNS_TYPE_SSHFP, 0, raxfr_sshfp },
0241
2019-11-02
pjp
{ DNS_TYPE_TLSA, 0, raxfr_tlsa },
0242
2019-11-02
pjp
{ DNS_TYPE_SRV, 0, raxfr_srv },
0243
2019-11-02
pjp
{ DNS_TYPE_NAPTR, 0, raxfr_naptr },
0244
2020-07-23
pjp
{ DNS_TYPE_RP, 0, raxfr_rp },
0245
2020-07-23
pjp
{ DNS_TYPE_HINFO, 0, raxfr_hinfo },
0246
2020-07-23
pjp
{ DNS_TYPE_CAA, 0, raxfr_caa },
0247
2019-11-02
pjp
{ 0, 0, NULL }
0248
2019-11-02
pjp
};
0249
2019-11-02
pjp
0250
2016-07-06
pjp
/*
0251
2016-12-29
pjp
* LABEL_COUNT - count the labels and return that number
0252
2016-08-28
pjp
*/
0253
2016-08-28
pjp
0254
2016-08-28
pjp
int
0255
2016-08-28
pjp
label_count(char *name)
0256
2016-08-28
pjp
{
0257
2016-08-28
pjp
int lc = 0;
0258
2016-12-29
pjp
char *p;
0259
2016-08-28
pjp
0260
2016-08-28
pjp
if (name == NULL)
0261
2016-08-28
pjp
return -1;
0262
2016-08-28
pjp
0263
2016-12-29
pjp
p = name;
0264
2016-12-29
pjp
while (*p != '\0') {
0265
2016-12-29
pjp
lc++;
0266
2016-12-29
pjp
p += (*p + 1);
0267
2016-12-29
pjp
}
0268
2016-08-28
pjp
0269
2016-08-28
pjp
return (lc);
0270
2016-08-28
pjp
}
0271
2016-08-28
pjp
0272
2016-08-28
pjp
/*
0273
2016-07-06
pjp
* DNS_LABEL - build a DNS NAME (with labels) from a canonical name
0274
2016-07-06
pjp
*
0275
2016-07-06
pjp
*/
0276
2016-07-06
pjp
0277
2016-07-06
pjp
char *
0278
2016-07-06
pjp
dns_label(char *name, int *returnlen)
0279
2016-07-06
pjp
{
0280
2016-07-06
pjp
int len, newlen = 0;
0281
2016-07-06
pjp
int i, lc = 0; /* lc = label count */
0282
2016-07-06
pjp
0283
2016-07-06
pjp
char *dnslabel, *p;
0284
2016-07-06
pjp
char *labels[255];
0285
2016-07-06
pjp
char **pl;
0286
2016-12-29
pjp
static char tname[DNS_MAXNAME + 1]; /* 255 bytes + 1*/
0287
2016-07-06
pjp
char *pt = &tname[0];
0288
2016-07-06
pjp
0289
2016-07-06
pjp
0290
2016-07-06
pjp
if (name == NULL)
0291
2016-07-06
pjp
return NULL;
0292
2016-07-06
pjp
0293
2016-07-06
pjp
strlcpy(tname, name, sizeof(tname));
0294
2016-07-06
pjp
0295
2016-07-06
pjp
len = strlen(tname);
0296
2016-07-06
pjp
if (tname[len - 1] == '.')
0297
2016-07-06
pjp
tname[len - 1] = '\0';
0298
2016-07-06
pjp
0299
2016-07-06
pjp
for (pl=labels;pl<&labels[254]&&(*pl=strsep(&pt,"."))!= NULL;pl++,lc++)
0300
2016-07-06
pjp
newlen += strlen(*pl);
0301
2016-07-06
pjp
0302
2016-07-06
pjp
newlen += lc; /* add label count to length */
0303
2016-07-06
pjp
0304
2016-07-06
pjp
0305
2016-07-06
pjp
/* make the buffer space, add 1 for trailing NULL */
0306
2016-07-06
pjp
if ((dnslabel = malloc(newlen + 1)) == NULL) {
0307
2016-07-06
pjp
return NULL;
0308
2016-07-06
pjp
}
0309
2016-07-06
pjp
0310
2019-12-04
pjp
pack32((char *)returnlen, (newlen + 1));
0311
2016-07-06
pjp
dnslabel[newlen] = '\0'; /* trailing NULL */
0312
2016-07-06
pjp
0313
2016-07-06
pjp
for (i = 0, p = dnslabel; i < lc; i++) {
0314
2016-07-06
pjp
len = strlen(labels[i]);
0315
2016-07-06
pjp
*p++ = len;
0316
2016-07-06
pjp
strlcpy(p, labels[i], newlen - (p - dnslabel) + 1);
0317
2016-07-06
pjp
p += len;
0318
2016-07-06
pjp
}
0319
2016-07-06
pjp
0320
2016-07-06
pjp
/*
0321
2016-07-06
pjp
* XXX hack to make all DNS names lower case, we only preserve
0322
2016-07-06
pjp
* case on compressed answers..
0323
2016-07-06
pjp
*/
0324
2016-07-06
pjp
0325
2016-07-06
pjp
for (i = 0, p = dnslabel; i < *returnlen; i++) {
0326
2016-07-06
pjp
int c;
0327
2016-07-06
pjp
0328
2016-07-06
pjp
c = *p;
0329
2016-07-06
pjp
if (isalpha(c))
0330
2016-07-06
pjp
*p = tolower(c);
0331
2016-07-06
pjp
p++;
0332
2016-07-06
pjp
}
0333
2016-07-06
pjp
0334
2019-11-04
pjp
#if DEBUG
0335
2016-07-06
pjp
if (debug)
0336
2016-07-06
pjp
dolog(LOG_DEBUG, "converting name= %s\n", name);
0337
2019-11-04
pjp
#endif
0338
2016-07-06
pjp
0339
2016-07-06
pjp
return dnslabel;
0340
2016-07-06
pjp
}
0341
2016-07-06
pjp
/*
0342
2020-06-25
pjp
* ddd_shutdown - delphinusdnsd wishes to shutdown, enter its pid into the
0343
2016-07-06
pjp
* shutdown shared memory and return.
0344
2016-07-06
pjp
*/
0345
2016-07-06
pjp
0346
2016-07-06
pjp
void
0347
2020-06-25
pjp
ddd_shutdown(void)
0348
2016-07-06
pjp
{
0349
2016-07-06
pjp
pid_t pid;
0350
2016-07-06
pjp
0351
2016-07-06
pjp
pid = getpid();
0352
2016-07-06
pjp
0353
2016-07-06
pjp
*ptr = pid;
0354
2016-07-06
pjp
}
0355
2016-07-06
pjp
0356
2016-07-06
pjp
0357
2016-07-06
pjp
/*
0358
2019-02-15
pjp
* LOOKUP_ZONE - look up a zone filling rbtree and returning RR TYPE, if error
0359
2019-02-15
pjp
* occurs returns -1, and sets errno on what type of error.
0360
2016-07-06
pjp
*/
0361
2016-07-06
pjp
0362
2019-02-15
pjp
0363
2019-02-15
pjp
struct rbtree *
0364
2020-01-16
pjp
lookup_zone(ddDB *db, struct question *question, int *returnval, int *lzerrno, char *replystring, int replystringsize)
0365
2016-07-06
pjp
{
0366
2016-07-06
pjp
0367
2019-02-15
pjp
struct rbtree *rbt = NULL;
0368
2019-11-11
pjp
struct rbtree *rbt0 = NULL;
0369
2019-11-12
pjp
struct rrset *rrset = NULL;
0370
2019-11-11
pjp
int plen, splen, error;
0371
2016-07-06
pjp
0372
2019-11-11
pjp
char *p, *sp;
0373
2019-02-15
pjp
0374
2019-02-15
pjp
p = question->hdr->name;
0375
2019-02-15
pjp
plen = question->hdr->namelen;
0376
2016-07-06
pjp
0377
2019-02-15
pjp
*returnval = 0;
0378
2020-06-30
pjp
0379
2020-06-30
pjp
if (forward) {
0380
2020-07-03
pjp
/* short circuit when we have no zones loaded */
0381
2020-07-03
pjp
if (zonecount == 0) {
0382
2020-06-30
pjp
*lzerrno = ERR_FORWARD;
0383
2020-06-30
pjp
*returnval = -1;
0384
2020-06-30
pjp
0385
2020-06-30
pjp
return NULL;
0386
2020-06-30
pjp
}
0387
2020-06-30
pjp
}
0388
2019-10-30
pjp
/* if the find_rrset fails, the find_rr will not get questioned */
0389
2019-02-19
pjp
if ((rbt = find_rrset(db, p, plen)) == NULL ||
0390
2019-11-11
pjp
((ntohs(question->hdr->qtype) != DNS_TYPE_DS) &&
0391
2019-11-11
pjp
(rbt->flags & RBT_GLUE)) ||
0392
2019-11-11
pjp
((rbt->flags & RBT_DNSSEC) && (rrset = find_rr(rbt, DNS_TYPE_NSEC3)) != NULL)) {
0393
2019-11-11
pjp
if (rbt == NULL) {
0394
2019-11-11
pjp
splen = plen;
0395
2019-11-11
pjp
sp = p;
0396
2019-11-11
pjp
0397
2019-11-11
pjp
while ((rbt0 = find_rrset(db, sp, splen)) == NULL) {
0398
2019-11-11
pjp
if (*sp == 0 && splen == 1)
0399
2019-11-11
pjp
break;
0400
2019-11-11
pjp
splen -= (*sp + 1);
0401
2019-11-11
pjp
sp += (*sp + 1);
0402
2019-11-11
pjp
}
0403
2019-11-11
pjp
0404
2019-11-11
pjp
if (rbt0 && rbt0->flags & RBT_GLUE)
0405
2019-11-11
pjp
rbt = rbt0;
0406
2019-11-11
pjp
}
0407
2019-11-11
pjp
/* check our delegations */
0408
2019-11-11
pjp
if (rbt && rbt->flags & RBT_GLUE) {
0409
2019-11-11
pjp
while (rbt && (rbt->flags & RBT_GLUE)) {
0410
2019-11-11
pjp
plen -= (*p + 1);
0411
2019-11-11
pjp
p += (*p + 1);
0412
2019-11-11
pjp
0413
2019-11-11
pjp
while ((rbt0 = find_rrset(db, p, plen)) == NULL) {
0414
2019-11-11
pjp
plen -= (*p + 1);
0415
2019-11-11
pjp
p += (*p + 1);
0416
2019-11-11
pjp
}
0417
2019-11-11
pjp
0418
2019-11-11
pjp
if (rbt0->flags & RBT_GLUE) {
0419
2019-11-11
pjp
rbt = rbt0;
0420
2019-11-11
pjp
} else {
0421
2019-11-11
pjp
/* answer the delegation */
0422
2020-01-16
pjp
snprintf(replystring, replystringsize, "%s", rbt->humanname);
0423
2019-11-11
pjp
*lzerrno = ERR_DELEGATE;
0424
2019-11-11
pjp
*returnval = -1;
0425
2019-11-11
pjp
return (rbt);
0426
2019-11-11
pjp
}
0427
2019-11-11
pjp
}
0428
2019-11-11
pjp
}
0429
2019-11-11
pjp
0430
2019-02-18
pjp
if (check_ent(p, plen) == 1) {
0431
2019-02-18
pjp
*lzerrno = ERR_NODATA;
0432
2019-02-18
pjp
*returnval = -1;
0433
2019-02-24
pjp
0434
2019-02-18
pjp
return NULL;
0435
2019-02-18
pjp
}
0436
2019-10-30
pjp
0437
2019-02-15
pjp
/*
0438
2019-02-15
pjp
* We have a condition where a record does not exist but we
0439
2019-02-15
pjp
* move toward the apex of the record, and there may be
0440
2019-02-15
pjp
* something. We return NXDOMAIN if there is an apex with
0441
2019-02-15
pjp
* SOA if not then we return REFUSED
0442
2019-02-15
pjp
*/
0443
2019-02-15
pjp
while (*p != 0) {
0444
2019-02-15
pjp
plen -= (*p + 1);
0445
2019-02-15
pjp
p = (p + (*p + 1));
0446
2019-02-15
pjp
0447
2019-02-24
pjp
/* rbt was NULL */
0448
2019-02-15
pjp
if ((rbt = find_rrset(db, p, plen)) != NULL) {
0449
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_SOA) != NULL) {
0450
2019-02-15
pjp
*lzerrno = ERR_NXDOMAIN;
0451
2019-02-15
pjp
*returnval = -1;
0452
2019-02-15
pjp
return (rbt);
0453
2019-02-15
pjp
}
0454
2019-10-30
pjp
0455
2019-10-30
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_NS)) != NULL) {
0456
2020-01-16
pjp
snprintf(replystring, replystringsize, "%s", rbt->humanname);
0457
2019-10-30
pjp
*lzerrno = ERR_DELEGATE;
0458
2019-10-30
pjp
*returnval = -1;
0459
2019-10-30
pjp
return (rbt);
0460
2019-10-30
pjp
}
0461
2019-02-24
pjp
0462
2019-02-15
pjp
}
0463
2016-07-21
pjp
}
0464
2020-06-30
pjp
if (forward)
0465
2020-06-30
pjp
*lzerrno = ERR_FORWARD;
0466
2020-06-30
pjp
else
0467
2020-06-30
pjp
*lzerrno = ERR_REFUSED;
0468
2019-02-15
pjp
*returnval = -1;
0469
2019-02-24
pjp
return (NULL);
0470
2016-07-21
pjp
}
0471
2019-02-15
pjp
0472
2020-01-16
pjp
snprintf(replystring, replystringsize, "%s", rbt->humanname);
0473
2016-07-21
pjp
0474
2019-11-12
pjp
if ((ntohs(question->hdr->qtype) != DNS_TYPE_DS) &&
0475
2019-11-12
pjp
(rrset = find_rr(rbt, DNS_TYPE_NS)) != NULL &&
0476
2019-11-12
pjp
! (rbt->flags & RBT_APEX)) {
0477
2019-10-30
pjp
*returnval = -1;
0478
2019-10-30
pjp
*lzerrno = ERR_DELEGATE;
0479
2019-10-30
pjp
return (rbt);
0480
2019-02-15
pjp
}
0481
2016-07-21
pjp
0482
2019-11-12
pjp
0483
2019-02-15
pjp
*returnval = check_qtype(rbt, ntohs(question->hdr->qtype), 0, &error);
0484
2019-02-15
pjp
if (*returnval == 0) {
0485
2019-02-15
pjp
*lzerrno = ERR_NOERROR;
0486
2019-02-15
pjp
*returnval = -1;
0487
2019-02-15
pjp
return (rbt);
0488
2019-02-15
pjp
}
0489
2019-02-15
pjp
0490
2019-02-15
pjp
return(rbt);
0491
2019-02-15
pjp
}
0492
2019-02-15
pjp
0493
2019-11-11
pjp
/*
0494
2019-11-11
pjp
* GET_SOA - get authoritative soa for a particular domain
0495
2019-11-11
pjp
*/
0496
2019-11-11
pjp
0497
2019-11-11
pjp
struct rbtree *
0498
2019-11-11
pjp
get_soa(ddDB *db, struct question *question)
0499
2019-11-11
pjp
{
0500
2019-11-11
pjp
struct rbtree *rbt = NULL;
0501
2019-11-11
pjp
0502
2019-11-11
pjp
int plen;
0503
2019-11-11
pjp
char *p;
0504
2019-11-11
pjp
0505
2019-11-11
pjp
p = question->hdr->name;
0506
2019-11-11
pjp
plen = question->hdr->namelen;
0507
2019-11-11
pjp
0508
2019-11-11
pjp
do {
0509
2019-11-11
pjp
struct rrset *rrset;
0510
2019-11-11
pjp
0511
2019-11-11
pjp
rbt = find_rrset(db, p, plen);
0512
2019-11-11
pjp
if (rbt == NULL) {
0513
2019-11-11
pjp
if (*p == '\0')
0514
2019-11-11
pjp
return (NULL);
0515
2019-11-11
pjp
0516
2019-11-11
pjp
plen -= (*p + 1);
0517
2019-11-11
pjp
p = (p + (*p + 1));
0518
2019-11-11
pjp
continue;
0519
2019-11-11
pjp
}
0520
2019-11-11
pjp
0521
2019-11-11
pjp
rrset = find_rr(rbt, DNS_TYPE_SOA);
0522
2019-11-11
pjp
if (rrset != NULL) {
0523
2019-11-11
pjp
/* we'll take this one */
0524
2019-11-11
pjp
return (rbt);
0525
2019-11-11
pjp
} else {
0526
2019-11-11
pjp
plen -= (*p + 1);
0527
2019-11-11
pjp
p = (p + (*p + 1));
0528
2019-11-11
pjp
}
0529
2019-11-11
pjp
0530
2019-11-11
pjp
} while (*p);
0531
2019-11-11
pjp
0532
2019-11-11
pjp
return (NULL);
0533
2019-11-11
pjp
}
0534
2019-11-11
pjp
0535
2019-11-11
pjp
/*
0536
2019-11-11
pjp
* GET_NS - walk to delegation name
0537
2019-11-11
pjp
*/
0538
2019-11-11
pjp
0539
2019-11-11
pjp
struct rbtree *
0540
2019-11-11
pjp
get_ns(ddDB *db, struct rbtree *rbt, int *delegation)
0541
2019-11-11
pjp
{
0542
2019-11-11
pjp
struct rrset *rrset = NULL;
0543
2019-11-11
pjp
struct rbtree *rbt0;
0544
2019-11-11
pjp
char *p;
0545
2019-11-11
pjp
int len;
0546
2019-11-11
pjp
0547
2019-11-11
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_SOA)) == NULL) {
0548
2019-12-04
pjp
pack32((char *)delegation, 1);
0549
2019-11-11
pjp
} else {
0550
2019-12-04
pjp
pack32((char *)delegation, 0);
0551
2019-11-11
pjp
return (rbt);
0552
2019-11-11
pjp
}
0553
2019-11-11
pjp
0554
2019-11-11
pjp
p = rbt->zone;
0555
2019-11-11
pjp
len = rbt->zonelen;
0556
2019-11-11
pjp
0557
2019-11-11
pjp
while (*p && len > 0) {
0558
2019-11-11
pjp
rbt0 = Lookup_zone(db, p, len, DNS_TYPE_NS, 0);
0559
2019-11-11
pjp
if (rbt0 == NULL) {
0560
2019-11-11
pjp
p += (*p + 1);
0561
2019-11-11
pjp
len -= (*p + 1);
0562
2019-11-11
pjp
0563
2019-11-11
pjp
continue;
0564
2019-11-11
pjp
} else
0565
2019-11-11
pjp
break;
0566
2019-11-11
pjp
}
0567
2019-11-11
pjp
0568
2019-11-11
pjp
if ((rrset = find_rr(rbt0, DNS_TYPE_SOA)) != NULL) {
0569
2019-12-04
pjp
pack32((char *)delegation, 0);
0570
2019-11-11
pjp
return (rbt);
0571
2019-11-11
pjp
}
0572
2019-11-11
pjp
0573
2019-11-11
pjp
return (rbt0);
0574
2019-11-11
pjp
}
0575
2019-11-11
pjp
0576
2019-11-11
pjp
0577
2019-11-11
pjp
0578
2019-11-02
pjp
/*
0579
2019-11-02
pjp
* Lookup_zone: wrapper for lookup_zone() et al. type must be htons()'ed!
0580
2019-11-02
pjp
*/
0581
2019-11-02
pjp
0582
2019-11-02
pjp
struct rbtree *
0583
2019-11-02
pjp
Lookup_zone(ddDB *db, char *name, u_int16_t namelen, u_int16_t type, int wildcard)
0584
2019-11-02
pjp
{
0585
2019-11-02
pjp
struct rbtree *rbt;
0586
2019-11-11
pjp
struct rrset *rrset = NULL;
0587
2019-11-02
pjp
0588
2019-11-11
pjp
rbt = find_rrset(db, name, namelen);
0589
2019-11-11
pjp
if (rbt != NULL) {
0590
2019-11-11
pjp
rrset = find_rr(rbt, type);
0591
2020-07-06
pjp
if (rrset != NULL) {
0592
2019-11-11
pjp
return (rbt);
0593
2020-07-08
pjp
}
0594
2019-11-02
pjp
}
0595
2019-11-02
pjp
0596
2019-11-11
pjp
return NULL;
0597
2019-11-02
pjp
}
0598
2019-11-02
pjp
0599
2019-10-31
pjp
/*
0600
2016-07-21
pjp
* CHECK_QTYPE - check the query type and return appropriately if we have
0601
2016-07-21
pjp
* such a record in our DB..
0602
2016-07-21
pjp
* returns 0 on error, or the DNS TYPE from 1 through 65535
0603
2016-07-21
pjp
* when the return is 0 the error variable is set with the error
0604
2016-07-21
pjp
* code (-1 or -2)
0605
2016-07-06
pjp
*/
0606
2016-07-06
pjp
0607
2016-07-21
pjp
u_int16_t
0608
2019-02-15
pjp
check_qtype(struct rbtree *rbt, u_int16_t type, int nxdomain, int *error)
0609
2016-07-06
pjp
{
0610
2019-10-30
pjp
u_int16_t returnval = -1;
0611
2016-07-06
pjp
0612
2016-07-06
pjp
switch (type) {
0613
2016-07-21
pjp
0614
2017-08-09
pjp
case DNS_TYPE_IXFR:
0615
2017-08-09
pjp
returnval = DNS_TYPE_IXFR;
0616
2017-08-09
pjp
break;
0617
2017-08-09
pjp
case DNS_TYPE_AXFR:
0618
2017-08-09
pjp
returnval = DNS_TYPE_AXFR;
0619
2017-08-09
pjp
break;
0620
2016-07-21
pjp
case DNS_TYPE_ANY:
0621
2016-07-21
pjp
returnval = DNS_TYPE_ANY;
0622
2016-07-21
pjp
break;
0623
2016-07-21
pjp
0624
2016-07-21
pjp
case DNS_TYPE_A:
0625
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_A) != NULL) {
0626
2016-07-21
pjp
returnval = DNS_TYPE_A;
0627
2016-07-21
pjp
break;
0628
2019-02-15
pjp
} else if (find_rr(rbt, DNS_TYPE_CNAME) != NULL) {
0629
2016-07-21
pjp
returnval = DNS_TYPE_CNAME;
0630
2016-07-21
pjp
break;
0631
2016-07-21
pjp
}
0632
2016-07-21
pjp
0633
2016-07-21
pjp
*error = -1;
0634
2016-07-21
pjp
return 0;
0635
2016-07-21
pjp
case DNS_TYPE_AAAA:
0636
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_AAAA) != NULL) {
0637
2016-07-21
pjp
returnval = DNS_TYPE_AAAA;
0638
2016-07-21
pjp
break;
0639
2019-02-15
pjp
} else if (find_rr(rbt, DNS_TYPE_CNAME) != NULL) {
0640
2016-07-21
pjp
returnval = DNS_TYPE_CNAME;
0641
2016-07-21
pjp
break;
0642
2016-07-21
pjp
}
0643
2016-07-21
pjp
0644
2016-07-21
pjp
*error = -1;
0645
2016-07-21
pjp
return 0;
0646
2016-07-21
pjp
case DNS_TYPE_MX:
0647
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_MX) != NULL) {
0648
2016-07-21
pjp
returnval = DNS_TYPE_MX;
0649
2016-07-21
pjp
break;
0650
2019-02-15
pjp
} else if (find_rr(rbt, DNS_TYPE_CNAME) != NULL) {
0651
2016-07-21
pjp
returnval = DNS_TYPE_CNAME;
0652
2016-07-21
pjp
break;
0653
2016-07-21
pjp
}
0654
2016-07-21
pjp
0655
2016-07-21
pjp
*error = -1;
0656
2016-07-21
pjp
return 0;
0657
2016-07-21
pjp
case DNS_TYPE_PTR:
0658
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_PTR) != NULL) {
0659
2016-07-21
pjp
returnval = DNS_TYPE_PTR;
0660
2016-07-21
pjp
break;
0661
2019-02-15
pjp
} else if (find_rr(rbt, DNS_TYPE_CNAME) != NULL) {
0662
2016-07-21
pjp
returnval = DNS_TYPE_CNAME;
0663
2016-07-21
pjp
break;
0664
2016-07-21
pjp
}
0665
2016-07-21
pjp
0666
2016-07-21
pjp
*error = -1;
0667
2016-07-21
pjp
return 0;
0668
2016-07-21
pjp
0669
2016-07-21
pjp
case DNS_TYPE_SOA:
0670
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_SOA) != NULL) {
0671
2016-07-21
pjp
returnval = DNS_TYPE_SOA;
0672
2016-07-21
pjp
break;
0673
2016-07-21
pjp
}
0674
2016-07-21
pjp
0675
2016-07-21
pjp
if (nxdomain)
0676
2016-07-21
pjp
*error = -2;
0677
2016-07-21
pjp
else
0678
2016-07-21
pjp
*error = -1;
0679
2016-07-21
pjp
0680
2016-07-21
pjp
return 0;
0681
2016-07-21
pjp
0682
2016-07-21
pjp
case DNS_TYPE_TLSA:
0683
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_TLSA) != NULL) {
0684
2016-07-21
pjp
returnval = DNS_TYPE_TLSA;
0685
2016-07-21
pjp
break;
0686
2016-07-21
pjp
}
0687
2016-07-21
pjp
0688
2016-07-21
pjp
*error = -1;
0689
2016-07-21
pjp
return 0;
0690
2016-07-21
pjp
0691
2020-07-23
pjp
case DNS_TYPE_CAA:
0692
2020-07-23
pjp
if (find_rr(rbt, DNS_TYPE_CAA) != NULL) {
0693
2020-07-23
pjp
returnval = DNS_TYPE_CAA;
0694
2020-07-23
pjp
break;
0695
2020-07-23
pjp
}
0696
2020-07-23
pjp
0697
2020-07-23
pjp
*error = -1;
0698
2020-07-23
pjp
return 0;
0699
2020-07-23
pjp
0700
2020-07-23
pjp
case DNS_TYPE_RP:
0701
2020-07-23
pjp
if (find_rr(rbt, DNS_TYPE_RP) != NULL) {
0702
2020-07-23
pjp
returnval = DNS_TYPE_RP;
0703
2020-07-23
pjp
break;
0704
2020-07-23
pjp
}
0705
2020-07-23
pjp
0706
2020-07-23
pjp
*error = -1;
0707
2020-07-23
pjp
return 0;
0708
2020-07-23
pjp
0709
2020-07-23
pjp
case DNS_TYPE_HINFO:
0710
2020-07-23
pjp
if (find_rr(rbt, DNS_TYPE_HINFO) != NULL) {
0711
2020-07-23
pjp
returnval = DNS_TYPE_HINFO;
0712
2020-07-23
pjp
break;
0713
2020-07-23
pjp
}
0714
2020-07-23
pjp
0715
2020-07-23
pjp
*error = -1;
0716
2020-07-23
pjp
return 0;
0717
2020-07-23
pjp
0718
2016-07-21
pjp
case DNS_TYPE_SSHFP:
0719
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_SSHFP) != NULL) {
0720
2016-07-21
pjp
returnval = DNS_TYPE_SSHFP;
0721
2016-07-21
pjp
break;
0722
2016-07-21
pjp
}
0723
2016-07-21
pjp
0724
2016-07-21
pjp
*error = -1;
0725
2016-07-21
pjp
return 0;
0726
2016-07-21
pjp
0727
2016-07-21
pjp
case DNS_TYPE_SRV:
0728
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_SRV) != NULL) {
0729
2016-07-21
pjp
returnval = DNS_TYPE_SRV;
0730
2016-07-21
pjp
break;
0731
2016-07-21
pjp
}
0732
2016-07-21
pjp
0733
2016-07-21
pjp
*error = -1;
0734
2016-07-21
pjp
return 0;
0735
2016-07-21
pjp
0736
2016-07-21
pjp
case DNS_TYPE_NAPTR:
0737
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_NAPTR) != NULL) {
0738
2016-07-21
pjp
returnval = DNS_TYPE_NAPTR;
0739
2016-07-21
pjp
break;
0740
2016-07-21
pjp
}
0741
2016-07-21
pjp
0742
2016-07-21
pjp
*error = -1;
0743
2016-07-21
pjp
return 0;
0744
2016-07-21
pjp
case DNS_TYPE_CNAME:
0745
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_CNAME) != NULL) {
0746
2016-07-21
pjp
returnval = DNS_TYPE_CNAME;
0747
2016-07-21
pjp
break;
0748
2016-07-21
pjp
}
0749
2016-07-21
pjp
0750
2016-07-21
pjp
*error = -1;
0751
2016-07-21
pjp
return 0;
0752
2016-07-21
pjp
0753
2016-07-21
pjp
case DNS_TYPE_NS:
0754
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_NS) != NULL) {
0755
2016-07-21
pjp
returnval = DNS_TYPE_NS;
0756
2016-07-21
pjp
break;
0757
2016-07-21
pjp
}
0758
2016-07-21
pjp
0759
2016-07-21
pjp
*error = -1;
0760
2016-07-21
pjp
return 0;
0761
2016-07-21
pjp
case DNS_TYPE_TXT:
0762
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_TXT) != NULL) {
0763
2016-07-21
pjp
returnval = DNS_TYPE_TXT;
0764
2016-07-21
pjp
break;
0765
2016-07-21
pjp
}
0766
2016-07-21
pjp
0767
2016-07-21
pjp
*error = -1;
0768
2016-07-21
pjp
return 0;
0769
2016-07-21
pjp
case DNS_TYPE_RRSIG:
0770
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_RRSIG) != NULL) {
0771
2016-07-21
pjp
returnval = DNS_TYPE_RRSIG;
0772
2016-07-21
pjp
break;
0773
2016-07-21
pjp
}
0774
2016-07-21
pjp
0775
2016-07-21
pjp
*error = -1;
0776
2016-07-21
pjp
return 0;
0777
2016-07-21
pjp
case DNS_TYPE_NSEC3PARAM:
0778
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_NSEC3PARAM) != NULL) {
0779
2016-07-21
pjp
returnval = DNS_TYPE_NSEC3PARAM;
0780
2016-07-21
pjp
break;
0781
2016-07-21
pjp
}
0782
2016-07-21
pjp
0783
2016-07-21
pjp
*error = -1;
0784
2016-07-21
pjp
return 0;
0785
2016-07-21
pjp
case DNS_TYPE_NSEC3:
0786
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_NSEC3) != NULL) {
0787
2016-07-21
pjp
returnval = DNS_TYPE_NSEC3;
0788
2016-07-21
pjp
break;
0789
2016-07-21
pjp
}
0790
2016-07-21
pjp
0791
2016-07-21
pjp
*error = -1;
0792
2016-07-21
pjp
return 0;
0793
2016-07-21
pjp
case DNS_TYPE_NSEC:
0794
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_NSEC) != NULL) {
0795
2016-07-21
pjp
returnval = DNS_TYPE_NSEC;
0796
2016-07-21
pjp
break;
0797
2016-07-21
pjp
}
0798
2016-07-21
pjp
0799
2016-07-21
pjp
*error = -1;
0800
2016-07-21
pjp
return 0;
0801
2016-07-21
pjp
case DNS_TYPE_DS:
0802
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_DS) != NULL) {
0803
2016-07-21
pjp
returnval = DNS_TYPE_DS;
0804
2016-07-21
pjp
break;
0805
2016-07-21
pjp
}
0806
2016-07-21
pjp
0807
2016-07-21
pjp
*error = -1;
0808
2016-07-21
pjp
return 0;
0809
2016-07-21
pjp
case DNS_TYPE_DNSKEY:
0810
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_DNSKEY) != NULL) {
0811
2016-07-21
pjp
returnval = DNS_TYPE_DNSKEY;
0812
2016-07-21
pjp
break;
0813
2016-07-21
pjp
}
0814
2016-07-21
pjp
0815
2016-07-21
pjp
*error = -1;
0816
2016-07-21
pjp
return 0;
0817
2016-07-21
pjp
default: /* RR's that we don't support, but have a zone for */
0818
2016-07-21
pjp
0819
2016-07-21
pjp
*error = -1;
0820
2016-07-21
pjp
return 0;
0821
2016-07-06
pjp
break;
0822
2016-07-21
pjp
}
0823
2016-07-21
pjp
0824
2016-07-21
pjp
return (returnval);
0825
2016-07-21
pjp
}
0826
2016-07-21
pjp
0827
2016-07-21
pjp
/*
0828
2016-07-21
pjp
* BUILD_FAKE_QUESTION - fill the fake question structure with the DNS query.
0829
2016-07-21
pjp
*/
0830
2016-07-21
pjp
0831
2016-07-21
pjp
struct question *
0832
2019-02-26
pjp
build_fake_question(char *name, int namelen, u_int16_t type, char *tsigkey, int tsigkeylen)
0833
2016-07-21
pjp
{
0834
2016-07-21
pjp
struct question *q;
0835
2016-07-21
pjp
0836
2016-07-21
pjp
q = (void *)calloc(1, sizeof(struct question));
0837
2016-07-21
pjp
if (q == NULL) {
0838
2016-07-21
pjp
dolog(LOG_INFO, "calloc: %s\n", strerror(errno));
0839
2016-07-21
pjp
return NULL;
0840
2016-07-21
pjp
}
0841
2016-07-21
pjp
0842
2016-07-21
pjp
q->hdr = (void *)calloc(1, sizeof(struct dns_question_hdr));
0843
2016-07-21
pjp
if (q->hdr == NULL) {
0844
2016-07-21
pjp
dolog(LOG_INFO, "calloc: %s\n", strerror(errno));
0845
2016-07-21
pjp
free(q);
0846
2016-07-21
pjp
return NULL;
0847
2016-07-21
pjp
}
0848
2016-07-21
pjp
q->hdr->namelen = namelen;
0849
2016-07-21
pjp
q->hdr->name = (void *) calloc(1, q->hdr->namelen);
0850
2016-07-21
pjp
if (q->hdr->name == NULL) {
0851
2016-07-21
pjp
dolog(LOG_INFO, "calloc: %s\n", strerror(errno));
0852
2016-07-21
pjp
free(q->hdr);
0853
2016-07-21
pjp
free(q);
0854
2016-07-21
pjp
return NULL;
0855
2016-07-21
pjp
}
0856
2020-07-16
pjp
q->hdr->original_name = (void *) calloc(1, q->hdr->namelen);
0857
2020-07-16
pjp
if (q->hdr->original_name == NULL) {
0858
2020-07-16
pjp
dolog(LOG_INFO, "calloc: %s\n", strerror(errno));
0859
2020-07-16
pjp
free(q->hdr->name);
0860
2020-07-16
pjp
free(q->hdr);
0861
2020-07-16
pjp
free(q);
0862
2020-07-16
pjp
return NULL;
0863
2020-07-16
pjp
}
0864
2016-07-21
pjp
q->converted_name = NULL;
0865
2016-07-21
pjp
0866
2016-07-21
pjp
/* fill our name into the dns header struct */
0867
2016-07-21
pjp
0868
2020-07-16
pjp
memcpy(q->hdr->original_name, name, q->hdr->namelen);
0869
2020-07-16
pjp
memcpy(q->hdr->name, name, q->hdr->namelen);
0870
2020-07-16
pjp
0871
2020-07-16
pjp
if (lower_dnsname(q->hdr->name, q->hdr->namelen) == -1) {
0872
2020-07-16
pjp
free(q->hdr->original_name);
0873
2020-07-16
pjp
free(q->hdr->name);
0874
2020-07-16
pjp
free(q->hdr);
0875
2020-07-16
pjp
free(q);
0876
2020-07-16
pjp
return NULL;
0877
2020-07-16
pjp
}
0878
2020-07-16
pjp
0879
2016-07-21
pjp
0880
2016-07-21
pjp
q->hdr->qtype = type;
0881
2016-07-21
pjp
q->hdr->qclass = htons(DNS_CLASS_IN);
0882
2016-07-21
pjp
0883
2019-02-26
pjp
if (tsig) {
0884
2019-02-26
pjp
char *alg;
0885
2019-02-26
pjp
int alglen;
0886
2019-02-26
pjp
0887
2019-02-26
pjp
if (tsigkeylen > sizeof(q->tsig.tsigkey)) {
0888
2020-07-16
pjp
free(q->hdr->original_name);
0889
2020-07-16
pjp
free(q->hdr->name);
0890
2019-02-26
pjp
free(q->hdr);
0891
2019-02-26
pjp
free(q);
0892
2019-02-26
pjp
return NULL;
0893
2019-02-26
pjp
}
0894
2019-02-26
pjp
0895
2019-02-26
pjp
memcpy(&q->tsig.tsigkey, tsigkey, tsigkeylen);
0896
2019-02-26
pjp
q->tsig.tsigkeylen = tsigkeylen;
0897
2019-02-26
pjp
0898
2019-02-26
pjp
alg = dns_label("hmac-sha256.", &alglen);
0899
2019-02-26
pjp
0900
2019-02-26
pjp
if (alg != NULL) {
0901
2019-02-26
pjp
memcpy (&q->tsig.tsigalg, alg, alglen);
0902
2019-02-26
pjp
q->tsig.tsigalglen = alglen;
0903
2019-02-26
pjp
0904
2019-02-26
pjp
free(alg);
0905
2019-02-26
pjp
0906
2019-02-26
pjp
q->tsig.tsigmaclen = 32;
0907
2019-02-26
pjp
}
0908
2019-02-26
pjp
}
0909
2019-02-26
pjp
0910
2016-07-21
pjp
return (q);
0911
2019-02-26
pjp
0912
2016-08-30
pjp
}
0913
2016-08-30
pjp
0914
2016-08-30
pjp
/*
0915
2016-08-30
pjp
* GET_DNS_TYPE - take integer and compare to table, then spit back a static
0916
2016-08-30
pjp
* string with the result. This function can't fail.
0917
2016-08-30
pjp
*/
0918
2016-08-30
pjp
0919
2016-08-30
pjp
char *
0920
2016-08-30
pjp
get_dns_type(int dnstype, int withbracket)
0921
2016-08-30
pjp
{
0922
2016-08-30
pjp
static char type[128];
0923
2016-08-30
pjp
struct typetable *t;
0924
2016-08-30
pjp
0925
2016-08-30
pjp
t = TT;
0926
2016-08-30
pjp
0927
2016-08-30
pjp
while (t->type != NULL) {
0928
2016-08-30
pjp
if (dnstype == t->number)
0929
2016-08-30
pjp
break;
0930
2016-08-30
pjp
0931
2016-08-30
pjp
t = (t + 1);
0932
2016-08-30
pjp
}
0933
2016-08-30
pjp
0934
2016-08-30
pjp
if (t->type == NULL) {
0935
2016-08-30
pjp
snprintf(type, sizeof(type) - 1, "%u", dnstype);
0936
2016-08-30
pjp
} else {
0937
2016-08-30
pjp
if (withbracket)
0938
2016-08-30
pjp
snprintf(type, sizeof(type) - 1, "%s(%u)", t->type, dnstype);
0939
2016-08-30
pjp
else
0940
2016-08-30
pjp
snprintf(type, sizeof(type) - 1, "%s", t->type);
0941
2016-08-30
pjp
}
0942
2016-08-30
pjp
0943
2016-08-30
pjp
return (type);
0944
2016-08-30
pjp
}
0945
2016-08-30
pjp
0946
2017-01-11
pjp
/*
0947
2017-01-11
pjp
* MEMCASECMP - check if buffer is identical to another buffer with
0948
2017-01-11
pjp
* one exception if a character is alphabetic it's
0949
2017-01-11
pjp
* compared to it's lower case value so that heLLo is
0950
2017-01-11
pjp
* the same as hello
0951
2017-01-11
pjp
*/
0952
2017-01-11
pjp
0953
2017-01-11
pjp
int
0954
2017-01-11
pjp
memcasecmp(u_char *b1, u_char *b2, int len)
0955
2017-01-11
pjp
{
0956
2017-01-11
pjp
int i;
0957
2017-01-11
pjp
int identical = 1;
0958
2017-01-11
pjp
0959
2017-01-11
pjp
for (i = 0; i < len; i++) {
0960
2017-01-11
pjp
int c0, c1;
0961
2017-01-11
pjp
0962
2017-01-11
pjp
c0 = b1[i];
0963
2017-01-11
pjp
c1 = b2[i];
0964
2017-01-11
pjp
0965
2017-01-11
pjp
if ((isalpha(c0) ? tolower(c0) : c0) !=
0966
2017-01-11
pjp
(isalpha(c1) ? tolower(c1) : c1)) {
0967
2017-01-11
pjp
identical = 0;
0968
2017-01-11
pjp
break;
0969
2017-01-11
pjp
}
0970
2017-01-11
pjp
}
0971
2017-01-11
pjp
0972
2017-01-11
pjp
if (identical)
0973
2017-01-11
pjp
return 0;
0974
2017-01-11
pjp
0975
2017-01-11
pjp
return 1; /* XXX */
0976
2019-02-07
pjp
}
0977
2019-02-07
pjp
0978
2019-02-07
pjp
/*
0979
2019-02-07
pjp
* BUILD_QUESTION - fill the question structure with the DNS query.
0980
2019-02-07
pjp
*/
0981
2019-02-07
pjp
0982
2019-02-07
pjp
struct question *
0983
2019-02-26
pjp
build_question(char *buf, int len, int additional, char *mac)
0984
2019-02-07
pjp
{
0985
2019-02-24
pjp
char pseudo_packet[4096]; /* for tsig */
0986
2019-02-24
pjp
u_int rollback, i;
0987
2019-12-03
pjp
u_int16_t qtype, qclass;
0988
2019-02-07
pjp
u_int32_t ttl;
0989
2019-02-28
pjp
u_int64_t timefudge;
0990
2020-07-16
pjp
int elen = 0;
0991
2019-02-07
pjp
0992
2020-07-16
pjp
char *end_name = NULL;
0993
2020-07-16
pjp
char *pb = NULL;
0994
2019-12-03
pjp
char *o;
0995
2020-07-16
pjp
char expand[DNS_MAXNAME + 1];
0996
2019-02-07
pjp
0997
2019-02-24
pjp
struct dns_tsigrr *tsigrr = NULL;
0998
2019-02-07
pjp
struct dns_optrr *opt = NULL;
0999
2019-02-07
pjp
struct question *q = NULL;
1000
2019-02-07
pjp
struct dns_header *hdr = (struct dns_header *)buf;
1001
2019-02-07
pjp
1002
2019-02-07
pjp
/* find the end of name */
1003
2020-07-16
pjp
elen = 0;
1004
2020-07-16
pjp
memset(&expand, 0, sizeof(expand));
1005
2020-07-16
pjp
end_name = expand_compression((u_char *)&buf[sizeof(struct dns_header)], (u_char *)buf, (u_char *)&buf[len], (u_char *)&expand, &elen, sizeof(expand));
1006
2019-02-07
pjp
if (end_name == NULL) {
1007
2020-07-16
pjp
dolog(LOG_ERR, "expand_compression() failed, bad formatted question name\n");
1008
2019-02-07
pjp
return NULL;
1009
2019-02-07
pjp
}
1010
2019-02-07
pjp
1011
2020-07-16
pjp
if ((end_name - buf) < elen) {
1012
2020-07-16
pjp
dolog(LOG_ERR, "compression in question #1\n");
1013
2019-02-07
pjp
return NULL;
1014
2019-02-07
pjp
}
1015
2019-02-07
pjp
1016
2020-07-16
pjp
i = (end_name - &buf[0]);
1017
2019-02-07
pjp
1018
2019-02-07
pjp
1019
2019-02-07
pjp
/* check if there is space for qtype and qclass */
1020
2019-02-07
pjp
if (len < ((end_name - &buf[0]) + (2 * sizeof(u_int16_t)))) {
1021
2019-02-07
pjp
dolog(LOG_INFO, "question rr is truncated\n");
1022
2019-02-07
pjp
return NULL;
1023
2019-02-07
pjp
}
1024
2020-07-19
pjp
/* check the class type so that $IP is erroring earlier */
1025
2020-07-19
pjp
1026
2020-07-19
pjp
o = (end_name + sizeof(uint16_t));
1027
2020-07-19
pjp
qclass = ntohs(unpack16(o));
1028
2020-07-19
pjp
1029
2020-07-19
pjp
switch (qclass) {
1030
2020-07-19
pjp
case DNS_CLASS_ANY:
1031
2020-07-19
pjp
case DNS_CLASS_NONE:
1032
2020-07-19
pjp
case DNS_CLASS_HS:
1033
2020-07-19
pjp
case DNS_CLASS_CH:
1034
2020-07-19
pjp
case DNS_CLASS_IN:
1035
2020-07-19
pjp
break;
1036
2020-07-19
pjp
default:
1037
2020-07-19
pjp
dolog(LOG_INFO, "unsupported class %d\n", qclass);
1038
2020-07-19
pjp
return NULL;
1039
2020-07-19
pjp
break;
1040
2020-07-19
pjp
}
1041
2019-02-07
pjp
1042
2019-02-07
pjp
q = (void *)calloc(1, sizeof(struct question));
1043
2019-02-07
pjp
if (q == NULL) {
1044
2019-02-07
pjp
dolog(LOG_INFO, "calloc: %s\n", strerror(errno));
1045
2019-02-07
pjp
return NULL;
1046
2019-02-07
pjp
}
1047
2019-02-07
pjp
q->hdr = (void *)calloc(1, sizeof(struct dns_question_hdr));
1048
2019-02-07
pjp
if (q->hdr == NULL) {
1049
2019-02-07
pjp
dolog(LOG_INFO, "calloc: %s\n", strerror(errno));
1050
2019-02-07
pjp
free(q);
1051
2019-02-07
pjp
return NULL;
1052
2019-02-07
pjp
}
1053
2020-07-16
pjp
q->hdr->namelen = (end_name - &buf[sizeof(struct dns_header)]);
1054
2019-02-07
pjp
q->hdr->name = (void *) calloc(1, q->hdr->namelen);
1055
2019-02-07
pjp
if (q->hdr->name == NULL) {
1056
2019-02-07
pjp
dolog(LOG_INFO, "calloc: %s\n", strerror(errno));
1057
2019-02-07
pjp
free(q->hdr);
1058
2019-02-07
pjp
free(q);
1059
2019-02-07
pjp
return NULL;
1060
2019-02-07
pjp
}
1061
2020-07-16
pjp
q->hdr->original_name = (void *)calloc(1, q->hdr->namelen);
1062
2020-07-16
pjp
if (q->hdr->original_name == NULL) {
1063
2020-07-16
pjp
dolog(LOG_INFO, "calloc: %s\n", strerror(errno));
1064
2019-02-07
pjp
free(q->hdr->name);
1065
2019-02-07
pjp
free(q->hdr);
1066
2019-02-07
pjp
free(q);
1067
2019-02-07
pjp
return NULL;
1068
2019-02-07
pjp
}
1069
2020-07-16
pjp
1070
2020-07-16
pjp
/* XXX the below line can fail */
1071
2020-07-16
pjp
(void)lower_dnsname(expand, elen);
1072
2020-07-16
pjp
1073
2020-07-16
pjp
if ((q->converted_name = convert_name(expand, elen)) == NULL) {
1074
2020-07-16
pjp
dolog(LOG_INFO, "error in convert_name()\n");
1075
2020-07-16
pjp
free(q->hdr->name);
1076
2020-07-16
pjp
free(q->hdr->original_name);
1077
2020-07-16
pjp
free(q->hdr);
1078
2020-07-16
pjp
free(q);
1079
2020-07-16
pjp
return NULL;
1080
2019-02-27
pjp
}
1081
2019-02-07
pjp
1082
2020-07-16
pjp
i += (2 * sizeof(u_int16_t)); /* type,class*/
1083
2019-02-07
pjp
1084
2019-02-27
pjp
/* in IXFR an additional SOA entry is tacked on, we want to skip this */
1085
2019-02-27
pjp
do {
1086
2019-12-03
pjp
u_int16_t val16;
1087
2019-02-07
pjp
1088
2019-02-27
pjp
rollback = i;
1089
2019-02-07
pjp
1090
2019-02-27
pjp
elen = 0;
1091
2019-02-27
pjp
memset(&expand, 0, sizeof(expand));
1092
2019-02-27
pjp
pb = expand_compression((u_char *)&buf[i], (u_char *)buf, (u_char *)&buf[len], (u_char *)&expand, &elen, sizeof(expand));
1093
2019-02-27
pjp
if (pb == NULL) {
1094
2019-02-27
pjp
i = rollback;
1095
2019-02-27
pjp
break;
1096
2019-02-27
pjp
}
1097
2019-02-27
pjp
i = (pb - buf);
1098
2019-02-07
pjp
1099
2019-02-27
pjp
if (i + 10 > len) { /* type + class + ttl + rdlen == 10 */
1100
2019-02-27
pjp
i = rollback;
1101
2019-02-27
pjp
break;
1102
2019-02-27
pjp
}
1103
2019-02-07
pjp
1104
2019-02-27
pjp
/* type */
1105
2019-12-03
pjp
o = &buf[i];
1106
2019-12-03
pjp
val16 = unpack16(o);
1107
2019-12-03
pjp
if (ntohs(val16) != DNS_TYPE_SOA) {
1108
2019-02-27
pjp
i = rollback;
1109
2019-02-27
pjp
break;
1110
2019-02-07
pjp
}
1111
2019-02-27
pjp
i += 2;
1112
2019-12-03
pjp
o += 2;
1113
2019-02-27
pjp
/* class */
1114
2019-12-03
pjp
val16 = unpack16(o);
1115
2019-12-03
pjp
if (ntohs(val16) != DNS_CLASS_IN) {
1116
2019-02-27
pjp
i = rollback;
1117
2019-02-27
pjp
break;
1118
2019-02-27
pjp
}
1119
2019-02-27
pjp
i += 2;
1120
2019-12-03
pjp
o += 2;
1121
2019-02-27
pjp
/* ttl */
1122
2020-08-08
pjp
#if 0
1123
2019-12-03
pjp
val32 = unpack32(o);
1124
2020-08-08
pjp
#endif
1125
2019-02-27
pjp
i += 4;
1126
2019-12-03
pjp
o += 4;
1127
2019-12-03
pjp
val16 = unpack16(o);
1128
2019-02-27
pjp
i += 2;
1129
2019-02-07
pjp
1130
2019-12-03
pjp
if (i + ntohs(val16) > len) { /* rdlen of SOA */
1131
2019-02-27
pjp
i = rollback;
1132
2019-02-27
pjp
break;
1133
2019-02-27
pjp
}
1134
2019-02-07
pjp
1135
2019-12-03
pjp
i += ntohs(val16);
1136
2019-12-03
pjp
o += ntohs(val16);
1137
2019-02-27
pjp
} while (0);
1138
2019-02-07
pjp
1139
2019-02-07
pjp
/* check for edns0 opt rr */
1140
2019-02-07
pjp
do {
1141
2019-02-07
pjp
/* if we don't have an additional section, break */
1142
2019-02-24
pjp
if (additional < 1)
1143
2019-02-07
pjp
break;
1144
2019-02-07
pjp
1145
2019-02-24
pjp
rollback = i;
1146
2019-02-07
pjp
1147
2019-02-07
pjp
/* check that the minimum optrr fits */
1148
2019-02-07
pjp
/* 10 */
1149
2019-02-24
pjp
if (i + sizeof(struct dns_optrr) > len) {
1150
2019-02-24
pjp
i = rollback;
1151
2019-02-07
pjp
break;
1152
2019-02-24
pjp
}
1153
2019-02-07
pjp
1154
2019-02-07
pjp
opt = (struct dns_optrr *)&buf[i];
1155
2019-02-24
pjp
if (opt->name[0] != 0) {
1156
2019-02-24
pjp
i = rollback;
1157
2019-02-07
pjp
break;
1158
2019-02-24
pjp
}
1159
2019-02-07
pjp
1160
2019-02-24
pjp
if (ntohs(opt->type) != DNS_TYPE_OPT) {
1161
2019-02-24
pjp
i = rollback;
1162
2019-02-07
pjp
break;
1163
2019-02-24
pjp
}
1164
2019-02-07
pjp
1165
2019-02-07
pjp
/* RFC 3225 */
1166
2019-02-07
pjp
ttl = ntohl(opt->ttl);
1167
2019-02-07
pjp
if (((ttl >> 16) & 0xff) != 0)
1168
2019-02-07
pjp
q->ednsversion = (ttl >> 16) & 0xff;
1169
2019-02-07
pjp
1170
2019-02-07
pjp
q->edns0len = ntohs(opt->class);
1171
2019-02-07
pjp
if (q->edns0len < 512)
1172
2019-02-07
pjp
q->edns0len = 512; /* RFC 6891 - page 10 */
1173
2019-02-07
pjp
1174
2019-02-07
pjp
if (ttl & DNSSEC_OK)
1175
2019-02-07
pjp
q->dnssecok = 1;
1176
2019-02-24
pjp
1177
2019-02-24
pjp
i += 11 + ntohs(opt->rdlen);
1178
2019-02-24
pjp
additional--;
1179
2019-02-07
pjp
} while (0);
1180
2019-02-24
pjp
/* check for TSIG rr */
1181
2019-02-24
pjp
do {
1182
2019-12-03
pjp
u_int16_t val16, tsigerror, tsigotherlen;
1183
2019-02-24
pjp
u_int16_t fudge;
1184
2019-12-03
pjp
u_int32_t val32;
1185
2019-02-24
pjp
int elen, tsignamelen;
1186
2019-02-24
pjp
char *pb;
1187
2019-02-24
pjp
char expand[DNS_MAXNAME + 1];
1188
2019-02-24
pjp
char tsigkey[512];
1189
2019-02-24
pjp
u_char sha256[32];
1190
2019-02-24
pjp
u_int shasize = sizeof(sha256);
1191
2019-02-24
pjp
time_t now, tsigtime;
1192
2019-02-24
pjp
int pseudolen1, pseudolen2, ppoffset = 0;
1193
2019-02-24
pjp
int pseudolen3 , pseudolen4;
1194
2019-02-07
pjp
1195
2019-02-24
pjp
q->tsig.have_tsig = 0;
1196
2019-02-24
pjp
q->tsig.tsigerrorcode = 1;
1197
2019-02-24
pjp
1198
2019-02-24
pjp
/* if we don't have an additional section, break */
1199
2019-02-24
pjp
if (additional < 1) {
1200
2019-02-24
pjp
break;
1201
2019-02-24
pjp
}
1202
2019-02-24
pjp
1203
2019-02-24
pjp
memset(q->tsig.tsigkey, 0, sizeof(q->tsig.tsigkey));
1204
2019-02-24
pjp
memset(q->tsig.tsigalg, 0, sizeof(q->tsig.tsigalg));
1205
2019-02-24
pjp
memset(q->tsig.tsigmac, 0, sizeof(q->tsig.tsigmac));
1206
2019-02-24
pjp
q->tsig.tsigkeylen = q->tsig.tsigalglen = q->tsig.tsigmaclen = 0;
1207
2019-02-24
pjp
1208
2019-02-24
pjp
/* the key name is parsed here */
1209
2019-02-24
pjp
rollback = i;
1210
2019-02-24
pjp
elen = 0;
1211
2019-02-24
pjp
memset(&expand, 0, sizeof(expand));
1212
2019-02-24
pjp
pb = expand_compression((u_char *)&buf[i], (u_char *)buf, (u_char *)&buf[len], (u_char *)&expand, &elen, sizeof(expand));
1213
2019-02-24
pjp
if (pb == NULL) {
1214
2019-02-24
pjp
free_question(q);
1215
2020-07-19
pjp
dolog(LOG_INFO, "expand_compression() failed, tsig keyname\n");
1216
2019-02-24
pjp
return NULL;
1217
2019-02-24
pjp
}
1218
2019-02-24
pjp
i = (pb - buf);
1219
2019-02-24
pjp
pseudolen1 = i;
1220
2019-02-24
pjp
1221
2019-02-24
pjp
memcpy(q->tsig.tsigkey, expand, elen);
1222
2019-02-24
pjp
q->tsig.tsigkeylen = elen;
1223
2019-02-24
pjp
1224
2019-02-24
pjp
1225
2019-02-24
pjp
if (i + 10 > len) { /* type + class + ttl + rdlen == 10 */
1226
2019-02-24
pjp
i = rollback;
1227
2019-02-24
pjp
break;
1228
2019-02-24
pjp
}
1229
2019-02-24
pjp
1230
2019-02-24
pjp
/* type */
1231
2019-12-03
pjp
o = &buf[i];
1232
2019-12-03
pjp
val16 = unpack16(o);
1233
2019-12-03
pjp
if (ntohs(val16) != DNS_TYPE_TSIG) {
1234
2019-02-24
pjp
i = rollback;
1235
2019-02-24
pjp
break;
1236
2019-02-24
pjp
}
1237
2019-02-24
pjp
i += 2;
1238
2019-12-03
pjp
o += 2;
1239
2019-02-24
pjp
pseudolen2 = i;
1240
2019-02-24
pjp
1241
2019-02-24
pjp
q->tsig.have_tsig = 1;
1242
2019-02-24
pjp
1243
2019-02-24
pjp
/* we don't have any tsig keys configured, no auth done */
1244
2019-02-24
pjp
if (tsig == 0) {
1245
2019-02-24
pjp
i = rollback;
1246
2019-11-05
pjp
#if 0
1247
2019-11-03
pjp
dolog(LOG_INFO, "build_question(): received a TSIG request, but tsig is not turned on for this IP range, this could result in a '1' error reply\n");
1248
2019-11-05
pjp
#endif
1249
2019-02-24
pjp
break;
1250
2019-02-24
pjp
}
1251
2019-02-24
pjp
1252
2019-02-24
pjp
q->tsig.tsigerrorcode = DNS_BADKEY;
1253
2019-02-24
pjp
1254
2019-02-24
pjp
/* class */
1255
2019-12-03
pjp
val16 = unpack16(o);
1256
2019-12-03
pjp
if (ntohs(val16) != DNS_CLASS_ANY) {
1257
2019-02-24
pjp
i = rollback;
1258
2019-02-24
pjp
break;
1259
2019-02-24
pjp
}
1260
2019-02-24
pjp
i += 2;
1261
2019-12-03
pjp
o += 2;
1262
2019-02-24
pjp
1263
2019-02-24
pjp
/* ttl */
1264
2019-12-03
pjp
val32 = unpack32(o);
1265
2019-12-03
pjp
if (ntohl(val32) != 0) {
1266
2019-02-24
pjp
i = rollback;
1267
2019-02-24
pjp
break;
1268
2019-02-24
pjp
}
1269
2019-02-24
pjp
i += 4;
1270
2019-12-03
pjp
o += 4;
1271
2019-02-24
pjp
1272
2019-02-24
pjp
/* rdlen */
1273
2019-12-03
pjp
val16 = unpack16(o);
1274
2019-12-03
pjp
if (ntohs(val16) != (len - (i + 2))) {
1275
2019-02-24
pjp
i = rollback;
1276
2019-02-24
pjp
break;
1277
2019-02-24
pjp
}
1278
2019-02-24
pjp
i += 2;
1279
2019-12-03
pjp
o += 2;
1280
2019-02-24
pjp
pseudolen3 = i;
1281
2019-02-24
pjp
1282
2019-02-24
pjp
/* the algorithm name is parsed here */
1283
2019-02-24
pjp
elen = 0;
1284
2019-02-24
pjp
memset(&expand, 0, sizeof(expand));
1285
2019-02-24
pjp
pb = expand_compression((u_char *)&buf[i], (u_char *)buf, (u_char *)&buf[len], (u_char *)&expand, &elen, sizeof(expand));
1286
2019-02-24
pjp
if (pb == NULL) {
1287
2019-02-24
pjp
free_question(q);
1288
2020-07-19
pjp
dolog(LOG_INFO, "expand_compression() failed, tsig algorithm name\n");
1289
2019-02-24
pjp
return NULL;
1290
2019-02-24
pjp
}
1291
2019-02-24
pjp
i = (pb - buf);
1292
2019-02-24
pjp
pseudolen4 = i;
1293
2019-02-24
pjp
1294
2019-02-24
pjp
memcpy(q->tsig.tsigalg, expand, elen);
1295
2019-02-24
pjp
q->tsig.tsigalglen = elen;
1296
2019-02-24
pjp
1297
2019-02-24
pjp
/* now check for MAC type, since it's given once again */
1298
2019-02-24
pjp
if (elen == 11) {
1299
2019-02-24
pjp
if (expand[0] != 9 ||
1300
2019-02-24
pjp
memcasecmp(&expand[1], "hmac-sha1", 9) != 0) {
1301
2019-02-24
pjp
break;
1302
2019-02-24
pjp
}
1303
2019-02-24
pjp
} else if (elen == 13) {
1304
2019-02-24
pjp
if (expand[0] != 11 ||
1305
2019-02-24
pjp
memcasecmp(&expand[1], "hmac-sha256", 11) != 0) {
1306
2019-02-24
pjp
break;
1307
2019-02-24
pjp
}
1308
2019-02-24
pjp
} else if (elen == 26) {
1309
2019-02-24
pjp
if (expand[0] != 8 ||
1310
2019-02-24
pjp
memcasecmp(&expand[1], "hmac-md5", 8) != 0) {
1311
2019-02-24
pjp
break;
1312
2019-02-24
pjp
}
1313
2019-02-24
pjp
} else {
1314
2019-02-24
pjp
break;
1315
2019-02-24
pjp
}
1316
2019-02-24
pjp
1317
2019-02-24
pjp
/*
1318
2019-02-24
pjp
* this is a delayed (moved down) check of the key, we don't
1319
2019-02-24
pjp
* know if this is a TSIG packet until we've chekced the TSIG
1320
2019-02-24
pjp
* type, that's why it's delayed...
1321
2019-02-24
pjp
*/
1322
2019-02-24
pjp
1323
2019-02-24
pjp
if ((tsignamelen = find_tsig_key(q->tsig.tsigkey, q->tsig.tsigkeylen, (char *)&tsigkey, sizeof(tsigkey))) < 0) {
1324
2019-02-24
pjp
/* we don't have the name configured, let it pass */
1325
2019-02-24
pjp
i = rollback;
1326
2019-02-24
pjp
break;
1327
2019-02-24
pjp
}
1328
2019-02-07
pjp
1329
2019-02-24
pjp
if (i + sizeof(struct dns_tsigrr) > len) {
1330
2019-02-24
pjp
i = rollback;
1331
2019-02-24
pjp
break;
1332
2019-02-24
pjp
}
1333
2019-02-24
pjp
1334
2019-02-24
pjp
tsigrr = (struct dns_tsigrr *)&buf[i];
1335
2019-02-28
pjp
/* XXX */
1336
2019-06-12
pjp
#ifndef __OpenBSD__
1337
2019-04-25
pjp
timefudge = be64toh(tsigrr->timefudge);
1338
2019-04-25
pjp
#else
1339
2019-02-28
pjp
timefudge = betoh64(tsigrr->timefudge);
1340
2019-04-25
pjp
#endif
1341
2019-02-28
pjp
fudge = (u_int16_t)(timefudge & 0xffff);
1342
2019-02-28
pjp
tsigtime = (u_int64_t)(timefudge >> 16);
1343
2019-02-24
pjp
1344
2019-02-24
pjp
q->tsig.tsig_timefudge = tsigrr->timefudge;
1345
2019-02-24
pjp
1346
2019-02-24
pjp
i += (8 + 2); /* timefudge + macsize */
1347
2019-02-24
pjp
1348
2019-02-24
pjp
if (ntohs(tsigrr->macsize) != 32) {
1349
2019-02-24
pjp
q->tsig.tsigerrorcode = DNS_BADSIG;
1350
2019-02-24
pjp
break;
1351
2019-02-24
pjp
}
1352
2019-02-24
pjp
1353
2019-02-24
pjp
i += ntohs(tsigrr->macsize);
1354
2019-02-07
pjp
1355
2019-02-07
pjp
1356
2019-02-24
pjp
/* now get the MAC from packet with length rollback */
1357
2019-02-24
pjp
NTOHS(hdr->additional);
1358
2019-02-24
pjp
hdr->additional--;
1359
2019-02-24
pjp
HTONS(hdr->additional);
1360
2019-02-07
pjp
1361
2019-02-24
pjp
/* origid */
1362
2019-12-03
pjp
o = &buf[i];
1363
2019-12-03
pjp
val16 = unpack16(o);
1364
2019-02-24
pjp
i += 2;
1365
2019-12-03
pjp
o += 2;
1366
2019-12-03
pjp
if (hdr->id != val16)
1367
2019-12-03
pjp
hdr->id = val16;
1368
2019-12-03
pjp
q->tsig.tsigorigid = val16;
1369
2019-02-24
pjp
1370
2019-02-24
pjp
/* error */
1371
2019-12-03
pjp
tsigerror = unpack16(o);
1372
2019-02-24
pjp
i += 2;
1373
2019-12-03
pjp
o += 2;
1374
2019-02-24
pjp
1375
2019-02-24
pjp
/* other len */
1376
2019-12-03
pjp
tsigotherlen = unpack16(o);
1377
2019-02-24
pjp
i += 2;
1378
2019-12-03
pjp
o += 2;
1379
2019-02-24
pjp
1380
2019-02-26
pjp
ppoffset = 0;
1381
2019-02-26
pjp
1382
2019-02-26
pjp
/* check if we have a request mac, this means it's an answer */
1383
2019-02-26
pjp
if (mac) {
1384
2019-12-03
pjp
o = &pseudo_packet[ppoffset];
1385
2019-12-03
pjp
pack16(o, htons(32));
1386
2019-02-26
pjp
ppoffset += 2;
1387
2019-02-26
pjp
1388
2019-02-26
pjp
memcpy(&pseudo_packet[ppoffset], mac, 32);
1389
2019-02-26
pjp
ppoffset += 32;
1390
2019-02-26
pjp
}
1391
2019-02-26
pjp
1392
2019-02-26
pjp
memcpy(&pseudo_packet[ppoffset], buf, pseudolen1);
1393
2019-02-26
pjp
ppoffset += pseudolen1;
1394
2019-02-24
pjp
memcpy((char *)&pseudo_packet[ppoffset], &buf[pseudolen2], 6);
1395
2019-02-24
pjp
ppoffset += 6;
1396
2019-02-24
pjp
1397
2019-02-24
pjp
memcpy((char *)&pseudo_packet[ppoffset], &buf[pseudolen3], pseudolen4 - pseudolen3);
1398
2019-02-24
pjp
ppoffset += (pseudolen4 - pseudolen3);
1399
2019-02-24
pjp
1400
2019-02-24
pjp
memcpy((char *)&pseudo_packet[ppoffset], (char *)&tsigrr->timefudge, 8);
1401
2019-02-24
pjp
ppoffset += 8;
1402
2019-02-24
pjp
1403
2019-12-03
pjp
o = &pseudo_packet[ppoffset];
1404
2019-12-03
pjp
pack16(o, tsigerror);
1405
2019-02-24
pjp
ppoffset += 2;
1406
2019-12-03
pjp
o += 2;
1407
2019-02-24
pjp
1408
2019-12-03
pjp
o = &pseudo_packet[ppoffset];
1409
2019-12-03
pjp
pack16(o, tsigotherlen);
1410
2019-02-24
pjp
ppoffset += 2;
1411
2019-12-03
pjp
o += 2;
1412
2019-02-24
pjp
1413
2019-02-24
pjp
memcpy(&pseudo_packet[ppoffset], &buf[i], len - i);
1414
2019-02-24
pjp
ppoffset += (len - i);
1415
2019-02-24
pjp
1416
2019-03-01
pjp
/* check for BADTIME before the HMAC memcmp as per RFC 2845 */
1417
2019-03-01
pjp
now = time(NULL);
1418
2019-03-01
pjp
/* outside our fudge window */
1419
2019-03-01
pjp
if (tsigtime < (now - fudge) || tsigtime > (now + fudge)) {
1420
2019-03-01
pjp
q->tsig.tsigerrorcode = DNS_BADTIME;
1421
2019-03-01
pjp
break;
1422
2019-03-01
pjp
}
1423
2019-02-26
pjp
1424
2019-02-24
pjp
HMAC(EVP_sha256(), tsigkey, tsignamelen, (unsigned char *)pseudo_packet,
1425
2019-02-24
pjp
ppoffset, (unsigned char *)&sha256, &shasize);
1426
2019-02-24
pjp
1427
2019-02-24
pjp
1428
2019-02-24
pjp
1429
2019-02-26
pjp
#if __OpenBSD__
1430
2019-02-26
pjp
if (timingsafe_memcmp(sha256, tsigrr->mac, sizeof(sha256)) != 0) {
1431
2019-02-26
pjp
#else
1432
2019-02-24
pjp
if (memcmp(sha256, tsigrr->mac, sizeof(sha256)) != 0) {
1433
2019-02-26
pjp
#endif
1434
2019-02-24
pjp
#if DEBUG
1435
2019-02-24
pjp
dolog(LOG_INFO, "HMAC did not verify\n");
1436
2019-02-24
pjp
#endif
1437
2019-02-24
pjp
q->tsig.tsigerrorcode = DNS_BADSIG;
1438
2019-02-24
pjp
break;
1439
2019-02-24
pjp
}
1440
2019-02-24
pjp
1441
2019-02-24
pjp
/* copy the mac for error coding */
1442
2019-02-24
pjp
memcpy(q->tsig.tsigmac, tsigrr->mac, sizeof(q->tsig.tsigmac));
1443
2019-02-24
pjp
q->tsig.tsigmaclen = 32;
1444
2019-02-24
pjp
1445
2019-02-24
pjp
/* we're now authenticated */
1446
2019-02-24
pjp
q->tsig.tsigerrorcode = 0;
1447
2019-02-24
pjp
q->tsig.tsigverified = 1;
1448
2019-02-24
pjp
1449
2019-02-24
pjp
} while (0);
1450
2019-02-24
pjp
1451
2019-02-24
pjp
/* fill our name into the dns header struct */
1452
2019-02-24
pjp
1453
2019-02-24
pjp
memcpy(q->hdr->name, &buf[sizeof(struct dns_header)], q->hdr->namelen);
1454
2020-07-16
pjp
memcpy(q->hdr->original_name, &buf[sizeof(struct dns_header)], q->hdr->namelen);
1455
2019-02-24
pjp
1456
2020-07-16
pjp
/* make hdr->name lower case */
1457
2019-02-24
pjp
1458
2020-07-16
pjp
if (lower_dnsname(q->hdr->name, q->hdr->namelen) == -1) {
1459
2020-07-16
pjp
dolog(LOG_INFO, "lower_dnsname failed\n");
1460
2020-07-16
pjp
free(q->hdr->name);
1461
2020-07-16
pjp
free(q->hdr->original_name);
1462
2020-07-16
pjp
free(q->hdr);
1463
2020-07-16
pjp
free(q);
1464
2020-07-16
pjp
return NULL;
1465
2019-02-07
pjp
}
1466
2019-02-07
pjp
1467
2019-02-07
pjp
/* parse type and class from the question */
1468
2019-02-07
pjp
1469
2020-07-16
pjp
o = (end_name);
1470
2019-12-03
pjp
qtype = unpack16(o);
1471
2020-07-16
pjp
o = (end_name + sizeof(uint16_t));
1472
2019-12-03
pjp
qclass = unpack16(o);
1473
2019-02-07
pjp
1474
2019-12-03
pjp
memcpy((char *)&q->hdr->qtype, (char *)&qtype, sizeof(u_int16_t));
1475
2019-12-03
pjp
memcpy((char *)&q->hdr->qclass, (char *)&qclass, sizeof(u_int16_t));
1476
2019-02-07
pjp
1477
2019-02-07
pjp
/* make note of whether recursion is desired */
1478
2019-02-07
pjp
q->rd = ((ntohs(hdr->query) & DNS_RECURSE) == DNS_RECURSE);
1479
2019-02-07
pjp
1480
2019-10-25
pjp
/* are we a notify packet? */
1481
2019-12-03
pjp
if ((ntohs(qtype) == DNS_TYPE_SOA) && (ntohs(qclass) == DNS_CLASS_IN))
1482
2019-10-25
pjp
q->notify = ((ntohs(hdr->query) & (DNS_NOTIFY | DNS_AUTH)) \
1483
2019-10-25
pjp
== (DNS_NOTIFY | DNS_AUTH));
1484
2019-10-25
pjp
else
1485
2019-10-25
pjp
q->notify = 0;
1486
2019-10-25
pjp
1487
2019-02-07
pjp
return (q);
1488
2019-02-07
pjp
}
1489
2019-02-07
pjp
1490
2019-02-07
pjp
/*
1491
2019-02-07
pjp
* FREE_QUESTION - free a question struct
1492
2019-02-07
pjp
*
1493
2019-02-07
pjp
*/
1494
2019-02-07
pjp
1495
2019-02-07
pjp
int
1496
2019-02-07
pjp
free_question(struct question *q)
1497
2019-02-07
pjp
{
1498
2019-02-07
pjp
free(q->hdr->name);
1499
2020-07-16
pjp
free(q->hdr->original_name);
1500
2019-02-07
pjp
free(q->hdr);
1501
2019-02-07
pjp
free(q->converted_name);
1502
2019-02-07
pjp
free(q);
1503
2019-02-07
pjp
1504
2019-02-07
pjp
return 0;
1505
2019-02-07
pjp
}
1506
2019-02-07
pjp
1507
2019-02-07
pjp
/* probably Copyright 2012 Kenneth R Westerback <krw@openbsd.org> */
1508
2019-02-07
pjp
1509
2019-02-07
pjp
static int
1510
2019-02-07
pjp
kw_cmp(const void *k, const void *e)
1511
2019-02-07
pjp
{
1512
2019-02-07
pjp
return (strcasecmp(k, ((const struct rrtab *)e)->name));
1513
2019-02-07
pjp
}
1514
2019-02-07
pjp
1515
2019-02-07
pjp
1516
2019-02-07
pjp
struct rrtab *
1517
2019-02-07
pjp
rrlookup(char *keyword)
1518
2019-02-07
pjp
{
1519
2019-02-07
pjp
static struct rrtab *p;
1520
2019-02-07
pjp
1521
2020-04-27
pjp
/* safety */
1522
2020-04-27
pjp
if (keyword == NULL)
1523
2020-04-27
pjp
return NULL;
1524
2020-04-27
pjp
1525
2019-02-07
pjp
p = bsearch(keyword, myrrtab, sizeof(myrrtab)/sizeof(myrrtab[0]),
1526
2019-02-07
pjp
sizeof(myrrtab[0]), kw_cmp);
1527
2019-02-07
pjp
1528
2019-02-07
pjp
return (p);
1529
2019-02-07
pjp
}
1530
2019-02-24
pjp
1531
2019-02-24
pjp
/*
1532
2019-02-24
pjp
* parse a domain name through a compression scheme and stay inside the bounds
1533
2019-02-24
pjp
* returns NULL on error and pointer to the next object;
1534
2019-02-24
pjp
*/
1535
2019-02-24
pjp
1536
2019-02-24
pjp
char *
1537
2019-02-24
pjp
expand_compression(u_char *p, u_char *estart, u_char *end, u_char *expand, int *elen, int max)
1538
2019-02-24
pjp
{
1539
2019-02-24
pjp
u_short tlen;
1540
2019-02-24
pjp
u_char *save = NULL;
1541
2019-12-03
pjp
u_int16_t offset;
1542
2019-02-24
pjp
1543
2019-02-24
pjp
/* expand name */
1544
2019-02-24
pjp
while ((u_char)*p && p <= end) {
1545
2019-02-24
pjp
/* test for compression */
1546
2019-02-24
pjp
if ((*p & 0xc0) == 0xc0) {
1547
2019-02-24
pjp
/* do not allow recursive compress pointers */
1548
2019-02-24
pjp
if (! save) {
1549
2019-02-24
pjp
save = p + 2;
1550
2019-02-24
pjp
}
1551
2019-12-03
pjp
offset = unpack16(p);
1552
2020-07-16
pjp
/* offsets into the dns header are a nono */
1553
2020-07-16
pjp
if ((ntohs(offset) & (~0xc000)) < sizeof(struct dns_header))
1554
2020-07-16
pjp
return NULL;
1555
2020-07-16
pjp
1556
2019-02-24
pjp
/* do not allow forwards jumping */
1557
2019-12-03
pjp
if ((p - estart) <= (ntohs(offset) & (~0xc000))) {
1558
2019-02-24
pjp
return NULL;
1559
2019-02-24
pjp
}
1560
2019-02-24
pjp
1561
2019-12-03
pjp
p = (estart + (ntohs(offset) & (~0xc000)));
1562
2019-02-24
pjp
} else {
1563
2019-02-24
pjp
if (*elen + 1 >= max) {
1564
2019-02-24
pjp
return NULL;
1565
2019-02-24
pjp
}
1566
2019-02-24
pjp
expand[(*elen)] = *p;
1567
2019-02-24
pjp
(*elen)++;
1568
2019-02-24
pjp
tlen = *p;
1569
2019-02-24
pjp
p++;
1570
2019-02-24
pjp
memcpy(&expand[*elen], p, tlen);
1571
2019-02-24
pjp
p += tlen;
1572
2019-02-24
pjp
if (*elen + tlen >= max) {
1573
2019-02-24
pjp
return NULL;
1574
2019-02-24
pjp
}
1575
2019-02-24
pjp
*elen += tlen;
1576
2019-02-24
pjp
}
1577
2019-02-24
pjp
}
1578
2019-02-24
pjp
1579
2019-02-24
pjp
if (p > end) {
1580
2019-02-24
pjp
return NULL;
1581
2019-02-24
pjp
}
1582
2019-02-24
pjp
1583
2019-02-24
pjp
if (save == NULL) {
1584
2019-02-24
pjp
p++;
1585
2019-02-24
pjp
(*elen)++;
1586
2019-02-24
pjp
return (p);
1587
2019-02-24
pjp
} else {
1588
2019-02-24
pjp
(*elen)++;
1589
2019-02-24
pjp
return (save);
1590
2019-02-24
pjp
}
1591
2019-02-24
pjp
}
1592
2019-02-24
pjp
1593
2019-02-24
pjp
void
1594
2019-02-24
pjp
log_diff(char *sha256, char *mac, int len)
1595
2019-02-24
pjp
{
1596
2019-02-24
pjp
char buf[512];
1597
2019-02-24
pjp
char tbuf[16];
1598
2019-02-24
pjp
int i;
1599
2019-02-24
pjp
1600
2019-02-24
pjp
memset(&buf, 0, sizeof(buf));
1601
2019-02-24
pjp
for (i = 0; i < 32; i++) {
1602
2019-02-24
pjp
snprintf(tbuf, sizeof(tbuf), "%02x", sha256[i] & 0xff);
1603
2019-02-24
pjp
strlcat(buf, tbuf, sizeof(buf));
1604
2019-02-24
pjp
}
1605
2019-02-24
pjp
1606
2019-02-24
pjp
strlcat(buf, "\n", sizeof(buf));
1607
2019-02-24
pjp
1608
2019-02-24
pjp
dolog(LOG_INFO, "our HMAC = %s\n", buf);
1609
2019-02-24
pjp
1610
2019-02-24
pjp
memset(&buf, 0, sizeof(buf));
1611
2019-02-24
pjp
for (i = 0; i < 32; i++) {
1612
2019-02-24
pjp
snprintf(tbuf, sizeof(tbuf), "%02x", mac[i] & 0xff);
1613
2019-02-24
pjp
strlcat(buf, tbuf, sizeof(buf));
1614
2019-02-24
pjp
}
1615
2019-02-24
pjp
1616
2019-02-24
pjp
strlcat(buf, "\n", sizeof(buf));
1617
2019-02-24
pjp
1618
2019-02-24
pjp
dolog(LOG_INFO, "given HMAC = %s\n", buf);
1619
2019-02-24
pjp
1620
2019-10-15
pjp
}
1621
2019-10-15
pjp
1622
2019-10-15
pjp
/*
1623
2019-10-15
pjp
* TSIG_PSEUDOHEADER - assemble a pseudoheader and with a HMAC_CTX * and
1624
2019-10-15
pjp
* update it within this function...
1625
2019-10-15
pjp
*/
1626
2019-10-15
pjp
1627
2019-10-15
pjp
int
1628
2019-10-15
pjp
tsig_pseudoheader(char *tsigkeyname, uint16_t fudge, time_t now, HMAC_CTX *ctx)
1629
2019-10-15
pjp
{
1630
2019-10-15
pjp
char pseudo_packet[512];
1631
2019-10-15
pjp
char *keyname = NULL;
1632
2019-10-15
pjp
1633
2019-12-11
pjp
int ppoffset = 0;
1634
2019-10-15
pjp
int len;
1635
2019-10-15
pjp
1636
2019-12-03
pjp
char *p;
1637
2019-10-15
pjp
1638
2019-10-15
pjp
keyname = dns_label(tsigkeyname, &len);
1639
2019-10-15
pjp
if (keyname == NULL) {
1640
2019-10-15
pjp
return -1;
1641
2019-10-15
pjp
}
1642
2019-10-15
pjp
1643
2019-10-15
pjp
/* name of key */
1644
2019-10-15
pjp
memcpy(&pseudo_packet, keyname, len);
1645
2019-10-15
pjp
ppoffset += len;
1646
2019-12-03
pjp
p = &pseudo_packet[len];
1647
2019-10-15
pjp
1648
2019-10-15
pjp
free(keyname);
1649
2019-10-15
pjp
1650
2019-10-15
pjp
/* class */
1651
2019-12-03
pjp
pack16(p, htons(DNS_CLASS_ANY));
1652
2019-10-15
pjp
ppoffset += 2;
1653
2019-12-03
pjp
p += 2;
1654
2019-10-15
pjp
1655
2019-10-15
pjp
/* TTL */
1656
2019-12-03
pjp
pack32(p, 0);
1657
2019-10-15
pjp
ppoffset += 4;
1658
2019-12-03
pjp
p += 4;
1659
2019-10-15
pjp
1660
2019-10-15
pjp
keyname = dns_label("hmac-sha256", &len);
1661
2019-10-15
pjp
if (keyname == NULL) {
1662
2019-10-15
pjp
return -1;
1663
2019-10-15
pjp
}
1664
2019-10-15
pjp
1665
2019-10-15
pjp
/* alg name */
1666
2019-10-15
pjp
memcpy(&pseudo_packet[ppoffset], keyname, len);
1667
2019-10-15
pjp
ppoffset += len;
1668
2019-12-03
pjp
p += len;
1669
2019-10-15
pjp
1670
2019-10-15
pjp
free(keyname);
1671
2019-10-15
pjp
1672
2019-10-15
pjp
/* time 1 and 2 */
1673
2019-10-15
pjp
now = time(NULL);
1674
2019-12-06
pjp
if (sizeof(time_t) == 4) /* 32-bit time_t */
1675
2019-12-06
pjp
pack16(p, 0);
1676
2019-12-06
pjp
else
1677
2019-12-06
pjp
pack16(p, htons((now >> 32) & 0xffff));
1678
2019-10-15
pjp
ppoffset += 2;
1679
2019-12-03
pjp
p += 2;
1680
2019-10-15
pjp
1681
2019-12-03
pjp
pack32(p, htonl((now & 0xffffffff)));
1682
2019-10-15
pjp
ppoffset += 4;
1683
2019-12-03
pjp
p += 4;
1684
2019-10-15
pjp
1685
2019-10-15
pjp
/* fudge */
1686
2019-12-03
pjp
pack16(p, htons(fudge));
1687
2019-10-15
pjp
ppoffset += 2;
1688
2019-12-03
pjp
p += 2;
1689
2019-10-15
pjp
1690
2019-10-15
pjp
/* error */
1691
2019-10-15
pjp
1692
2019-12-03
pjp
pack16(p, 0);
1693
2019-10-15
pjp
ppoffset += 2;
1694
2019-12-03
pjp
p += 2;
1695
2019-10-15
pjp
1696
2019-10-15
pjp
/* other len */
1697
2019-10-15
pjp
1698
2019-12-03
pjp
pack16(p, 0);
1699
2019-10-15
pjp
ppoffset += 2;
1700
2019-12-03
pjp
p += 2;
1701
2019-10-15
pjp
1702
2019-10-15
pjp
HMAC_Update(ctx, pseudo_packet, ppoffset);
1703
2019-10-15
pjp
1704
2019-10-15
pjp
return 0;
1705
2019-11-01
pjp
}
1706
2019-11-01
pjp
1707
2019-11-01
pjp
1708
2019-11-01
pjp
char *
1709
2019-11-01
pjp
bin2hex(char *bin, int len)
1710
2019-11-01
pjp
{
1711
2019-11-01
pjp
static char hex[4096];
1712
2019-11-01
pjp
char *p;
1713
2019-11-01
pjp
int i;
1714
2019-11-01
pjp
1715
2019-11-01
pjp
memset(&hex, 0, sizeof(hex));
1716
2019-11-01
pjp
p = &hex[0];
1717
2019-11-01
pjp
1718
2019-11-01
pjp
for (i = 0; i < len; i++) {
1719
2019-11-01
pjp
snprintf(p, sizeof(hex), "%02x", bin[i] & 0xff);
1720
2019-11-01
pjp
p += 2;
1721
2019-11-01
pjp
}
1722
2019-11-01
pjp
1723
2019-11-01
pjp
return ((char *)&hex);
1724
2019-11-01
pjp
}
1725
2019-11-01
pjp
1726
2019-11-01
pjp
u_int64_t
1727
2019-11-01
pjp
timethuman(time_t timet)
1728
2019-11-01
pjp
{
1729
2019-11-01
pjp
char timebuf[512];
1730
2019-11-01
pjp
struct tm *tm;
1731
2019-11-01
pjp
u_int64_t retbuf;
1732
2019-11-01
pjp
1733
2019-11-01
pjp
tm = gmtime((time_t *)&timet);
1734
2019-11-01
pjp
strftime(timebuf, sizeof(timebuf), "%Y%m%d%H%M%S", tm);
1735
2019-11-01
pjp
retbuf = atoll(timebuf);
1736
2019-11-01
pjp
1737
2019-11-01
pjp
return(retbuf);
1738
2019-11-01
pjp
}
1739
2019-11-01
pjp
1740
2019-11-01
pjp
1741
2019-11-01
pjp
char *
1742
2019-11-01
pjp
bitmap2human(char *bitmap, int len)
1743
2019-11-01
pjp
{
1744
2019-11-01
pjp
static char human[4096];
1745
2019-11-01
pjp
char expanded_bitmap[32];
1746
2019-11-01
pjp
u_int16_t bit;
1747
2019-11-01
pjp
int i, j, block, bitlen;
1748
2019-11-01
pjp
int x;
1749
2019-11-01
pjp
char *p;
1750
2019-11-01
pjp
1751
2019-11-01
pjp
memset(&human, 0, sizeof(human));
1752
2019-11-01
pjp
1753
2019-11-01
pjp
for (i = 0, p = bitmap; i < len;) {
1754
2019-11-01
pjp
block = *p;
1755
2019-11-01
pjp
p++;
1756
2019-11-01
pjp
i++;
1757
2019-11-01
pjp
memset(&expanded_bitmap, 0, sizeof(expanded_bitmap));
1758
2019-11-01
pjp
bitlen = *p;
1759
2019-11-01
pjp
p++;
1760
2019-11-01
pjp
i++;
1761
2019-11-01
pjp
memcpy(&expanded_bitmap, p, bitlen);
1762
2019-11-01
pjp
p += bitlen;
1763
2019-11-01
pjp
i += bitlen;
1764
2019-11-01
pjp
for (j = 0; j < 32; j++) {
1765
2019-11-01
pjp
if (expanded_bitmap[j] & 0x80) {
1766
2019-11-01
pjp
x = 0;
1767
2020-07-23
pjp
bit = (block * 256) + ((j * 8) + x);
1768
2019-11-01
pjp
strlcat(human, get_dns_type(bit, 0), sizeof(human));
1769
2019-11-01
pjp
strlcat(human, " ", sizeof(human));
1770
2019-11-01
pjp
}
1771
2019-11-01
pjp
if (expanded_bitmap[j] & 0x40) {
1772
2019-11-01
pjp
x = 1;
1773
2020-07-23
pjp
bit = (block * 256) + ((j * 8) + x);
1774
2019-11-01
pjp
strlcat(human, get_dns_type(bit, 0), sizeof(human));
1775
2019-11-01
pjp
strlcat(human, " ", sizeof(human));
1776
2019-11-01
pjp
}
1777
2019-11-01
pjp
if (expanded_bitmap[j] & 0x20) {
1778
2019-11-01
pjp
x = 2;
1779
2020-07-23
pjp
bit = (block * 256) + ((j * 8) + x);
1780
2019-11-01
pjp
strlcat(human, get_dns_type(bit, 0), sizeof(human));
1781
2019-11-01
pjp
strlcat(human, " ", sizeof(human));
1782
2019-11-01
pjp
}
1783
2019-11-01
pjp
if (expanded_bitmap[j] & 0x10) {
1784
2019-11-01
pjp
x = 3;
1785
2020-07-23
pjp
bit = (block * 256) + ((j * 8) + x);
1786
2019-11-01
pjp
strlcat(human, get_dns_type(bit, 0), sizeof(human));
1787
2019-11-01
pjp
strlcat(human, " ", sizeof(human));
1788
2019-11-01
pjp
}
1789
2019-11-01
pjp
if (expanded_bitmap[j] & 0x8) {
1790
2019-11-01
pjp
x = 4;
1791
2020-07-23
pjp
bit = (block * 256) + ((j * 8) + x);
1792
2019-11-01
pjp
strlcat(human, get_dns_type(bit, 0), sizeof(human));
1793
2019-11-01
pjp
strlcat(human, " ", sizeof(human));
1794
2019-11-01
pjp
}
1795
2019-11-01
pjp
if (expanded_bitmap[j] & 0x4) {
1796
2019-11-01
pjp
x = 5;
1797
2020-07-23
pjp
bit = (block * 256) + ((j * 8) + x);
1798
2019-11-01
pjp
strlcat(human, get_dns_type(bit, 0), sizeof(human));
1799
2019-11-01
pjp
strlcat(human, " ", sizeof(human));
1800
2019-11-01
pjp
}
1801
2019-11-01
pjp
if (expanded_bitmap[j] & 0x2) {
1802
2019-11-01
pjp
x = 6;
1803
2020-07-23
pjp
bit = (block * 256) + ((j * 8) + x);
1804
2019-11-01
pjp
strlcat(human, get_dns_type(bit, 0), sizeof(human));
1805
2019-11-01
pjp
strlcat(human, " ", sizeof(human));
1806
2019-11-01
pjp
}
1807
2019-11-01
pjp
if (expanded_bitmap[j] & 0x1) {
1808
2019-11-01
pjp
x = 7;
1809
2020-07-23
pjp
bit = (block * 256) + ((j * 8) + x);
1810
2019-11-01
pjp
strlcat(human, get_dns_type(bit, 0), sizeof(human));
1811
2019-11-01
pjp
strlcat(human, " ", sizeof(human));
1812
2019-11-01
pjp
}
1813
2019-11-01
pjp
1814
2019-11-01
pjp
}
1815
2019-11-01
pjp
}
1816
2019-11-01
pjp
1817
2019-11-01
pjp
if (human[strlen(human) - 1] == ' ')
1818
2019-11-01
pjp
human[strlen(human) - 1] = '\0';
1819
2019-11-01
pjp
1820
2019-11-01
pjp
return ((char *)&human);
1821
2019-11-02
pjp
}
1822
2019-11-02
pjp
1823
2019-11-02
pjp
1824
2019-11-02
pjp
int
1825
2020-09-24
pjp
lookup_axfr(FILE *f, int so, char *zonename, struct soa *mysoa, u_int32_t format, char *tsigkey, char *tsigpass, int *segment, int *answers, int *additionalcount, struct soa_constraints *constraints, uint32_t bytelimit)
1826
2019-11-02
pjp
{
1827
2019-11-02
pjp
char query[512];
1828
2019-11-02
pjp
char pseudo_packet[512];
1829
2019-11-02
pjp
char shabuf[32];
1830
2019-11-02
pjp
char *reply;
1831
2019-11-02
pjp
struct timeval tv, savetv;
1832
2019-11-02
pjp
struct question *q;
1833
2019-11-02
pjp
struct whole_header {
1834
2019-11-02
pjp
u_int16_t len;
1835
2019-11-02
pjp
struct dns_header dh;
1836
2019-11-02
pjp
} *wh, *rwh;
1837
2019-11-02
pjp
struct raxfr_logic *sr;
1838
2019-11-02
pjp
1839
2019-11-02
pjp
u_char *p, *name, *keyname;
1840
2019-11-02
pjp
1841
2019-11-02
pjp
u_char *end, *estart;
1842
2019-11-02
pjp
int len, totallen, zonelen, rrlen, rrtype;
1843
2019-11-02
pjp
int soacount = 0;
1844
2019-11-02
pjp
int segmentcount = 0;
1845
2019-11-02
pjp
int count = 0;
1846
2019-12-03
pjp
u_int16_t rdlen, *plen;
1847
2019-11-02
pjp
u_int16_t tcplen;
1848
2019-11-02
pjp
1849
2019-11-02
pjp
HMAC_CTX *ctx;
1850
2019-11-02
pjp
time_t now = 0;
1851
2019-11-02
pjp
socklen_t sizetv;
1852
2019-11-05
pjp
int sacount = 0;
1853
2019-11-02
pjp
1854
2019-11-02
pjp
if (!(format & TCP_FORMAT))
1855
2019-11-02
pjp
return -1;
1856
2019-11-02
pjp
1857
2019-11-02
pjp
memset(&query, 0, sizeof(query));
1858
2019-11-02
pjp
1859
2019-11-02
pjp
wh = (struct whole_header *)&query[0];
1860
2019-11-02
pjp
1861
2019-11-02
pjp
wh->dh.id = htons(arc4random() & 0xffff);
1862
2019-11-02
pjp
wh->dh.query = 0;
1863
2019-11-02
pjp
wh->dh.question = htons(1);
1864
2019-11-02
pjp
wh->dh.answer = 0;
1865
2019-11-02
pjp
wh->dh.nsrr = 0;
1866
2019-11-02
pjp
wh->dh.additional = htons(0);
1867
2019-11-02
pjp
1868
2019-11-02
pjp
1869
2019-11-02
pjp
SET_DNS_QUERY(&wh->dh);
1870
2019-11-02
pjp
SET_DNS_RECURSION(&wh->dh);
1871
2019-11-02
pjp
HTONS(wh->dh.query);
1872
2019-11-02
pjp
1873
2019-11-02
pjp
totallen = sizeof(struct whole_header);
1874
2019-11-02
pjp
1875
2019-11-02
pjp
name = dns_label(zonename, &len);
1876
2019-11-02
pjp
if (name == NULL) {
1877
2019-11-02
pjp
return -1;
1878
2019-11-02
pjp
}
1879
2019-11-02
pjp
1880
2019-11-02
pjp
zonelen = len;
1881
2019-11-02
pjp
1882
2019-11-02
pjp
p = (char *)&wh[1];
1883
2019-11-02
pjp
1884
2019-11-02
pjp
memcpy(p, name, len);
1885
2019-11-02
pjp
totallen += len;
1886
2019-12-03
pjp
p += len;
1887
2019-11-02
pjp
1888
2019-12-03
pjp
pack16(p, htons(DNS_TYPE_AXFR));
1889
2019-11-02
pjp
totallen += sizeof(u_int16_t);
1890
2019-12-03
pjp
p += sizeof(u_int16_t);
1891
2019-11-02
pjp
1892
2019-12-03
pjp
pack16(p, htons(DNS_CLASS_IN));
1893
2019-11-02
pjp
totallen += sizeof(u_int16_t);
1894
2019-12-03
pjp
p += sizeof(u_int16_t);
1895
2019-11-02
pjp
1896
2019-11-02
pjp
/* we have a key, attach a TSIG payload */
1897
2019-11-02
pjp
if (tsigkey) {
1898
2019-11-02
pjp
1899
2019-11-02
pjp
if ((len = mybase64_decode(tsigpass, (u_char *)&pseudo_packet, sizeof(pseudo_packet))) < 0) {
1900
2019-11-02
pjp
fprintf(stderr, "bad base64 password\n");
1901
2019-11-02
pjp
return -1;
1902
2019-11-02
pjp
}
1903
2019-11-02
pjp
1904
2019-11-02
pjp
ctx = HMAC_CTX_new();
1905
2019-11-02
pjp
HMAC_Init_ex(ctx, pseudo_packet, len, EVP_sha256(), NULL);
1906
2019-11-02
pjp
HMAC_Update(ctx, &query[2], totallen - 2);
1907
2019-11-02
pjp
1908
2019-11-02
pjp
now = time(NULL);
1909
2019-12-11
pjp
if (tsig_pseudoheader(tsigkey, DEFAULT_TSIG_FUDGE, now, ctx) < 0) {
1910
2019-11-02
pjp
fprintf(stderr, "tsig_pseudoheader failed\n");
1911
2019-11-02
pjp
return -1;
1912
2019-11-02
pjp
}
1913
2019-11-02
pjp
1914
2019-11-02
pjp
HMAC_Final(ctx, shabuf, &len);
1915
2019-11-02
pjp
1916
2019-11-02
pjp
if (len != 32) {
1917
2019-11-02
pjp
fprintf(stderr, "not expected len != 32\n");
1918
2019-11-02
pjp
return -1;
1919
2019-11-02
pjp
}
1920
2019-11-02
pjp
1921
2019-11-02
pjp
HMAC_CTX_free(ctx);
1922
2019-11-02
pjp
1923
2019-11-02
pjp
keyname = dns_label(tsigkey, &len);
1924
2019-11-02
pjp
if (keyname == NULL) {
1925
2019-11-02
pjp
return -1;
1926
2019-11-02
pjp
}
1927
2019-11-02
pjp
1928
2019-11-02
pjp
memcpy(&query[totallen], keyname, len);
1929
2019-11-02
pjp
totallen += len;
1930
2019-11-02
pjp
1931
2019-12-03
pjp
p = &query[totallen];
1932
2019-12-03
pjp
pack16(p, htons(DNS_TYPE_TSIG));
1933
2019-11-02
pjp
totallen += 2;
1934
2019-12-03
pjp
p += 2;
1935
2019-11-02
pjp
1936
2019-12-03
pjp
pack16(p, htons(DNS_CLASS_ANY));
1937
2019-11-02
pjp
totallen += 2;
1938
2019-12-03
pjp
p += 2;
1939
2019-11-02
pjp
1940
2019-12-03
pjp
pack32(p, htonl(0));
1941
2019-11-02
pjp
totallen += 4;
1942
2019-12-03
pjp
p += 4;
1943
2019-11-02
pjp
1944
2019-11-02
pjp
keyname = dns_label("hmac-sha256", &len);
1945
2019-11-02
pjp
if (keyname == NULL) {
1946
2019-11-02
pjp
return -1;
1947
2019-11-02
pjp
}
1948
2019-11-02
pjp
1949
2019-11-02
pjp
/* rdlen */
1950
2019-12-03
pjp
pack16(p, htons(len + 2 + 4 + 2 + 2 + 32 + 2 + 2 + 2));
1951
2019-11-02
pjp
totallen += 2;
1952
2019-12-03
pjp
p += 2;
1953
2019-11-02
pjp
1954
2019-11-02
pjp
/* algorithm name */
1955
2019-11-02
pjp
memcpy(&query[totallen], keyname, len);
1956
2019-11-02
pjp
totallen += len;
1957
2019-12-03
pjp
p += len;
1958
2019-11-02
pjp
1959
2019-11-02
pjp
/* time 1 */
1960
2019-12-06
pjp
if (sizeof(time_t) == 4) /* 32-bit time-t */
1961
2019-12-06
pjp
pack16(p, 0);
1962
2019-12-06
pjp
else
1963
2019-12-06
pjp
pack16(p, htons((now >> 32) & 0xffff));
1964
2019-11-02
pjp
totallen += 2;
1965
2019-12-03
pjp
p += 2;
1966
2019-11-02
pjp
1967
2019-11-02
pjp
/* time 2 */
1968
2019-12-03
pjp
pack32(p, htonl(now & 0xffffffff));
1969
2019-11-02
pjp
totallen += 4;
1970
2019-12-03
pjp
p += 4;
1971
2019-11-02
pjp
1972
2019-11-02
pjp
/* fudge */
1973
2019-12-11
pjp
pack16(p, htons(DEFAULT_TSIG_FUDGE));
1974
2019-11-02
pjp
totallen += 2;
1975
2019-12-03
pjp
p += 2;
1976
2019-11-02
pjp
1977
2019-11-02
pjp
/* hmac size */
1978
2019-12-03
pjp
pack16(p, htons(sizeof(shabuf)));
1979
2019-11-02
pjp
totallen += 2;
1980
2019-12-03
pjp
p += 2;
1981
2019-11-02
pjp
1982
2019-11-02
pjp
/* hmac */
1983
2019-11-02
pjp
memcpy(&query[totallen], shabuf, sizeof(shabuf));
1984
2019-11-02
pjp
totallen += sizeof(shabuf);
1985
2019-12-03
pjp
p += sizeof(shabuf);
1986
2019-11-02
pjp
1987
2019-11-02
pjp
/* original id */
1988
2019-12-03
pjp
pack16(p, wh->dh.id);
1989
2019-11-02
pjp
totallen += 2;
1990
2019-12-03
pjp
p += 2;
1991
2019-11-02
pjp
1992
2019-11-02
pjp
/* error */
1993
2019-12-03
pjp
pack16(p, 0);
1994
2019-11-02
pjp
totallen += 2;
1995
2019-12-03
pjp
p += 2;
1996
2019-11-02
pjp
1997
2019-11-02
pjp
/* other len */
1998
2019-12-03
pjp
pack16(p, 0);
1999
2019-11-02
pjp
totallen += 2;
2000
2019-12-03
pjp
p += 2;
2001
2019-11-02
pjp
2002
2019-11-02
pjp
wh->dh.additional = htons(1);
2003
2019-11-02
pjp
}
2004
2019-11-02
pjp
2005
2019-11-02
pjp
2006
2019-11-02
pjp
wh->len = htons(totallen - 2);
2007
2019-11-02
pjp
2008
2019-11-02
pjp
if (send(so, query, totallen, 0) < 0) {
2009
2019-11-02
pjp
perror("send");
2010
2019-11-02
pjp
return -1;
2011
2019-11-02
pjp
}
2012
2019-11-02
pjp
2013
2020-09-24
pjp
/* catch reply, totallen is reused here */
2014
2020-09-24
pjp
totallen = 0;
2015
2019-11-02
pjp
2016
2019-11-02
pjp
reply = calloc(1, 0xffff + 2);
2017
2019-11-02
pjp
if (reply == NULL) {
2018
2019-11-02
pjp
perror("calloc");
2019
2019-11-02
pjp
return -1;
2020
2019-11-02
pjp
}
2021
2019-11-02
pjp
2022
2019-11-02
pjp
if (tsigkey) {
2023
2019-11-02
pjp
uint16_t maclen;
2024
2019-11-02
pjp
2025
2019-11-02
pjp
if ((len = mybase64_decode(tsigpass, (u_char *)&pseudo_packet, sizeof(pseudo_packet))) < 0) {
2026
2019-11-02
pjp
fprintf(stderr, "bad base64 password\n");
2027
2019-11-02
pjp
return -1;
2028
2019-11-02
pjp
}
2029
2019-11-02
pjp
2030
2019-11-02
pjp
ctx = HMAC_CTX_new();
2031
2019-11-02
pjp
HMAC_Init_ex(ctx, pseudo_packet, len, EVP_sha256(), NULL);
2032
2019-11-02
pjp
maclen = htons(32);
2033
2019-11-05
pjp
HMAC_Update(ctx, (char *)&maclen, sizeof(maclen));
2034
2019-11-02
pjp
HMAC_Update(ctx, shabuf, sizeof(shabuf));
2035
2019-11-02
pjp
} else
2036
2019-11-02
pjp
ctx = NULL;
2037
2019-11-02
pjp
2038
2019-11-02
pjp
q = build_question((char *)&wh->dh, wh->len, wh->dh.additional, (tsigkey == NULL) ? NULL : shabuf);
2039
2019-11-02
pjp
if (q == NULL) {
2040
2019-11-02
pjp
fprintf(stderr, "failed to build_question\n");
2041
2019-11-02
pjp
return -1;
2042
2019-11-02
pjp
}
2043
2019-11-02
pjp
2044
2019-11-02
pjp
for (;;) {
2045
2019-11-06
pjp
sizetv = sizeof(struct timeval);
2046
2019-11-02
pjp
if (getsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &savetv, &sizetv) < 0) {
2047
2019-11-02
pjp
perror("getsockopt");
2048
2019-11-02
pjp
}
2049
2019-11-02
pjp
2050
2019-11-02
pjp
tv.tv_sec = 1;
2051
2019-11-02
pjp
tv.tv_usec = 0;
2052
2019-11-02
pjp
2053
2019-11-02
pjp
if (setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
2054
2019-11-06
pjp
dolog(LOG_DEBUG, "setsockopt failed with sec 1, usec 0: %s\n", strerror(errno));
2055
2019-11-02
pjp
}
2056
2019-11-06
pjp
2057
2019-11-02
pjp
len = recv(so, reply, 2, MSG_PEEK | MSG_WAITALL);
2058
2019-11-02
pjp
if (len <= 0)
2059
2019-11-02
pjp
break;
2060
2019-11-02
pjp
2061
2019-11-02
pjp
plen = (u_int16_t *)reply;
2062
2019-11-02
pjp
tcplen = ntohs(*plen) + 2;
2063
2019-11-02
pjp
2064
2019-11-06
pjp
/* restore original timeout values */
2065
2019-11-02
pjp
if (setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &savetv, sizeof(savetv)) < 0) {
2066
2019-11-02
pjp
perror("setsockopt");
2067
2019-11-02
pjp
}
2068
2019-11-02
pjp
2069
2019-11-02
pjp
len = recv(so, reply, tcplen, MSG_WAITALL);
2070
2019-11-02
pjp
if (len < 0) {
2071
2019-11-02
pjp
perror("recv");
2072
2019-11-02
pjp
return -1;
2073
2019-11-02
pjp
}
2074
2020-09-24
pjp
2075
2020-09-24
pjp
totallen += len;
2076
2020-09-24
pjp
2077
2020-09-24
pjp
if (totallen >= bytelimit) {
2078
2020-09-24
pjp
fprintf(stderr, "download exceeded byte limit\n");
2079
2020-09-24
pjp
return -1;
2080
2020-09-24
pjp
}
2081
2020-09-24
pjp
2082
2019-11-02
pjp
rwh = (struct whole_header *)&reply[0];
2083
2019-11-02
pjp
bytes_received += ntohs(rwh->len);
2084
2019-11-02
pjp
2085
2019-11-02
pjp
end = &reply[len];
2086
2019-11-02
pjp
len = rwh->len;
2087
2019-11-02
pjp
2088
2019-11-02
pjp
if (rwh->dh.id != wh->dh.id) {
2089
2019-11-02
pjp
fprintf(stderr, "DNS ID mismatch\n");
2090
2019-11-02
pjp
return -1;
2091
2019-11-02
pjp
}
2092
2019-11-02
pjp
2093
2019-11-02
pjp
if (!(htons(rwh->dh.query) & DNS_REPLY)) {
2094
2019-11-02
pjp
fprintf(stderr, "NOT a DNS reply\n");
2095
2019-11-02
pjp
return -1;
2096
2019-11-02
pjp
}
2097
2019-11-02
pjp
2098
2019-11-02
pjp
if (ntohs(rwh->dh.answer) < 1) {
2099
2019-11-02
pjp
fprintf(stderr, "NO ANSWER provided\n");
2100
2019-11-02
pjp
return -1;
2101
2019-11-02
pjp
}
2102
2019-11-02
pjp
2103
2019-11-02
pjp
segmentcount = ntohs(rwh->dh.answer);
2104
2019-11-02
pjp
if (tsigkey) {
2105
2019-11-02
pjp
segmentcount += ntohs(rwh->dh.additional);
2106
2019-11-05
pjp
*additionalcount += ntohs(rwh->dh.additional);
2107
2019-11-05
pjp
#if 0
2108
2019-11-05
pjp
printf("additional = %d\n", ntohs(rwh->dh.additional));
2109
2019-11-05
pjp
// rwh->dh.additional = 0;
2110
2019-11-05
pjp
#endif
2111
2019-11-02
pjp
}
2112
2019-11-05
pjp
*answers += segmentcount;
2113
2019-11-02
pjp
2114
2019-11-02
pjp
2115
2019-11-02
pjp
if (memcmp(q->hdr->name, name, q->hdr->namelen) != 0) {
2116
2019-11-02
pjp
fprintf(stderr, "question name not for what we asked\n");
2117
2019-11-02
pjp
return -1;
2118
2019-11-02
pjp
}
2119
2019-11-02
pjp
2120
2019-11-02
pjp
if (q->hdr->qclass != htons(DNS_CLASS_IN) || q->hdr->qtype != htons(DNS_TYPE_AXFR)) {
2121
2019-11-02
pjp
fprintf(stderr, "wrong class or type\n");
2122
2019-11-02
pjp
return -1;
2123
2019-11-02
pjp
}
2124
2019-11-02
pjp
2125
2019-11-02
pjp
p = (char *)&rwh[1];
2126
2019-11-02
pjp
p += q->hdr->namelen;
2127
2019-11-02
pjp
p += sizeof(u_int16_t); /* type */
2128
2019-11-02
pjp
p += sizeof(u_int16_t); /* class */
2129
2019-11-02
pjp
/* end of question */
2130
2019-11-02
pjp
2131
2019-11-02
pjp
estart = (u_char *)&rwh->dh;
2132
2019-11-02
pjp
2133
2019-11-02
pjp
if (tsigkey) {
2134
2019-11-05
pjp
uint16_t saveadd;
2135
2019-11-05
pjp
2136
2019-11-05
pjp
saveadd = rwh->dh.additional;
2137
2019-11-05
pjp
NTOHS(rwh->dh.additional);
2138
2019-11-19
pjp
if (rwh->dh.additional)
2139
2019-11-19
pjp
rwh->dh.additional--;
2140
2019-11-05
pjp
HTONS(rwh->dh.additional);
2141
2019-11-02
pjp
HMAC_Update(ctx, estart, (p - estart));
2142
2019-11-05
pjp
rwh->dh.additional = saveadd;
2143
2019-11-02
pjp
}
2144
2019-11-02
pjp
2145
2019-11-04
pjp
(*segment)++;
2146
2019-11-02
pjp
2147
2019-11-02
pjp
for (count = 0; count < segmentcount; count++) {
2148
2019-11-02
pjp
char mac[32];
2149
2019-11-02
pjp
2150
2020-07-27
pjp
if ((rrlen = raxfr_peek(f, p, estart, end, &rrtype, soacount, &rdlen, format, ctx, name, zonelen, 1)) < 0) {
2151
2019-11-02
pjp
fprintf(stderr, "not a SOA reply, or ERROR\n");
2152
2019-11-02
pjp
return -1;
2153
2019-11-02
pjp
}
2154
2019-11-02
pjp
2155
2019-11-02
pjp
if (tsigkey && (rrtype == DNS_TYPE_TSIG)) {
2156
2019-11-02
pjp
uint16_t maclen;
2157
2019-11-02
pjp
2158
2019-11-02
pjp
/* do tsig checks here */
2159
2019-11-05
pjp
if ((len = raxfr_tsig(f,p,estart,end,mysoa,rdlen,ctx, (char *)&mac, (sacount++ == 0) ? 1 : 0)) < 0) {
2160
2019-11-02
pjp
fprintf(stderr, "error with TSIG record\n");
2161
2019-11-02
pjp
return -1;
2162
2019-11-02
pjp
}
2163
2019-11-02
pjp
2164
2019-11-02
pjp
p = (estart + len);
2165
2019-11-02
pjp
2166
2019-11-02
pjp
if ((len = mybase64_decode(tsigpass, (u_char *)&pseudo_packet, sizeof(pseudo_packet))) < 0) {
2167
2019-11-02
pjp
fprintf(stderr, "bad base64 password\n");
2168
2019-11-02
pjp
return -1;
2169
2019-11-02
pjp
}
2170
2019-11-02
pjp
2171
2019-11-19
pjp
if (HMAC_CTX_reset(ctx) != 1) {
2172
2019-11-19
pjp
fprintf(stderr, "HMAC_CTX_reset failed!\n");
2173
2019-11-19
pjp
return -1;
2174
2019-11-19
pjp
}
2175
2019-11-19
pjp
if (HMAC_Init_ex(ctx, pseudo_packet, len, EVP_sha256(), NULL) != 1) {
2176
2019-11-19
pjp
fprintf(stderr, "HMAC_Init_ex failed!\n");
2177
2019-11-19
pjp
return -1;
2178
2019-11-19
pjp
}
2179
2019-11-02
pjp
maclen = htons(32);
2180
2019-11-05
pjp
HMAC_Update(ctx, (char *)&maclen, sizeof(maclen));
2181
2019-11-05
pjp
HMAC_Update(ctx, mac, sizeof(mac));
2182
2019-11-02
pjp
2183
2019-11-02
pjp
if (soacount > 1)
2184
2019-11-02
pjp
goto out;
2185
2019-11-02
pjp
} else
2186
2019-11-02
pjp
p = (estart + rrlen);
2187
2019-11-02
pjp
2188
2019-11-02
pjp
if (rrtype == DNS_TYPE_SOA) {
2189
2020-07-27
pjp
if ((len = raxfr_soa(f, p, estart, end, mysoa, soacount, format, rdlen, ctx, constraints)) < 0) {
2190
2019-11-02
pjp
fprintf(stderr, "raxfr_soa failed\n");
2191
2019-11-02
pjp
return -1;
2192
2019-11-02
pjp
}
2193
2019-11-02
pjp
p = (estart + len);
2194
2019-11-02
pjp
soacount++;
2195
2019-11-02
pjp
2196
2019-11-02
pjp
/*
2197
2019-11-02
pjp
* the envelopes are done because we have
2198
2019-11-02
pjp
* two SOA's, continue here to catch the
2199
2019-11-02
pjp
* TSIG.
2200
2019-11-02
pjp
*/
2201
2019-11-02
pjp
if (soacount > 1)
2202
2019-11-02
pjp
continue;
2203
2019-11-02
pjp
} else {
2204
2019-11-02
pjp
for (sr = supported; sr->rrtype != 0; sr++) {
2205
2019-11-02
pjp
if (rrtype == sr->rrtype) {
2206
2020-07-08
pjp
if ((len = (*sr->raxfr)(f, p, estart, end, mysoa, rdlen, ctx)) < 0) {
2207
2019-11-02
pjp
fprintf(stderr, "error with rrtype %d\n", sr->rrtype);
2208
2019-11-02
pjp
return -1;
2209
2019-11-02
pjp
}
2210
2019-11-02
pjp
p = (estart + len);
2211
2019-11-02
pjp
break;
2212
2019-11-02
pjp
}
2213
2019-11-02
pjp
}
2214
2019-11-02
pjp
2215
2019-11-02
pjp
if (sr->rrtype == 0) {
2216
2019-11-02
pjp
if (rrtype != DNS_TYPE_TSIG) {
2217
2019-11-02
pjp
fprintf(stderr, "unsupported RRTYPE %d\n", rrtype);
2218
2019-11-02
pjp
return -1;
2219
2019-11-02
pjp
}
2220
2019-11-02
pjp
}
2221
2019-11-02
pjp
}
2222
2019-11-02
pjp
}
2223
2019-11-02
pjp
}
2224
2019-11-02
pjp
2225
2019-11-02
pjp
if ((len = recv(so, reply, 0xffff, 0)) > 0) {
2226
2019-11-02
pjp
fprintf(stderr, ";; WARN: received %d more bytes.\n", len);
2227
2019-11-02
pjp
}
2228
2019-11-02
pjp
2229
2019-11-02
pjp
out:
2230
2019-11-02
pjp
2231
2019-11-02
pjp
if (tsigkey) {
2232
2019-11-02
pjp
HMAC_CTX_free(ctx);
2233
2019-11-02
pjp
}
2234
2019-11-02
pjp
2235
2020-07-26
pjp
#if 0
2236
2019-11-02
pjp
if (f != NULL) {
2237
2019-11-02
pjp
if ((format & ZONE_FORMAT))
2238
2019-11-02
pjp
fprintf(f, "}\n");
2239
2019-11-02
pjp
}
2240
2020-07-26
pjp
#endif
2241
2019-11-02
pjp
2242
2019-11-02
pjp
free_question(q);
2243
2019-11-02
pjp
2244
2019-11-02
pjp
return 0;
2245
2019-11-02
pjp
2246
2019-11-02
pjp
}
2247
2019-11-02
pjp
2248
2019-11-12
pjp
/*
2249
2019-11-12
pjp
* DN_CONTAINS - is anchorname contained in name?
2250
2019-11-12
pjp
*/
2251
2019-11-02
pjp
2252
2019-11-12
pjp
int
2253
2019-11-12
pjp
dn_contains(char *name, int len, char *anchorname, int alen)
2254
2019-11-12
pjp
{
2255
2019-11-12
pjp
char *p = name;
2256
2019-11-12
pjp
int plen = len;
2257
2019-11-12
pjp
2258
2019-11-12
pjp
while (plen >= alen) {
2259
2019-11-12
pjp
if (plen == alen &&
2260
2019-11-12
pjp
memcasecmp(p, anchorname, alen) == 0) {
2261
2019-11-12
pjp
return 1;
2262
2019-11-12
pjp
}
2263
2019-11-12
pjp
2264
2019-11-12
pjp
plen -= (*p + 1);
2265
2019-11-12
pjp
p += (*p + 1);
2266
2019-11-12
pjp
}
2267
2019-11-12
pjp
2268
2019-11-12
pjp
return 0;
2269
2019-12-03
pjp
}
2270
2019-12-03
pjp
2271
2019-12-03
pjp
/* pack functions */
2272
2019-12-03
pjp
2273
2019-12-03
pjp
void
2274
2019-12-03
pjp
pack32(char *buf, u_int32_t value)
2275
2019-12-03
pjp
{
2276
2019-12-03
pjp
pack(buf, (char *)&value, sizeof(uint32_t));
2277
2019-12-03
pjp
}
2278
2019-12-03
pjp
2279
2019-12-03
pjp
void
2280
2019-12-03
pjp
pack16(char *buf, u_int16_t value)
2281
2019-12-03
pjp
{
2282
2019-12-03
pjp
pack(buf, (char *)&value, sizeof(uint16_t));
2283
2019-12-03
pjp
}
2284
2019-12-03
pjp
2285
2019-12-03
pjp
void
2286
2019-12-03
pjp
pack8(char *buf, u_int8_t value)
2287
2019-12-03
pjp
{
2288
2019-12-03
pjp
u_int8_t *p;
2289
2019-12-03
pjp
2290
2019-12-03
pjp
p = (u_int8_t *)buf;
2291
2019-12-03
pjp
*p = value;
2292
2019-12-03
pjp
}
2293
2019-12-03
pjp
2294
2019-12-03
pjp
void
2295
2019-12-03
pjp
pack(char *buf, char *input, int len)
2296
2019-12-03
pjp
{
2297
2019-12-03
pjp
memcpy(buf, input, len);
2298
2019-12-03
pjp
}
2299
2019-12-03
pjp
2300
2019-12-03
pjp
uint32_t
2301
2019-12-03
pjp
unpack32(char *buf)
2302
2019-12-03
pjp
{
2303
2019-12-03
pjp
uint32_t ret = 0;
2304
2019-12-03
pjp
2305
2019-12-03
pjp
unpack((char *)&ret, buf, sizeof(uint32_t));
2306
2019-12-03
pjp
2307
2019-12-03
pjp
return (ret);
2308
2019-12-03
pjp
}
2309
2019-12-03
pjp
2310
2019-12-03
pjp
uint16_t
2311
2019-12-03
pjp
unpack16(char *buf)
2312
2019-12-03
pjp
{
2313
2019-12-03
pjp
uint16_t ret = 0;
2314
2019-12-03
pjp
2315
2019-12-03
pjp
unpack((char *)&ret, buf, sizeof(uint16_t));
2316
2019-12-03
pjp
2317
2019-12-03
pjp
return (ret);
2318
2019-12-03
pjp
}
2319
2019-12-03
pjp
2320
2019-12-03
pjp
void
2321
2019-12-03
pjp
unpack(char *buf, char *input, int len)
2322
2019-12-03
pjp
{
2323
2019-12-03
pjp
memcpy(buf, input, len);
2324
2020-07-16
pjp
}
2325
2020-07-16
pjp
2326
2020-07-16
pjp
/* https://tools.ietf.org/html/draft-vixie-dnsext-dns0x20-00 */
2327
2020-07-16
pjp
int
2328
2020-07-16
pjp
randomize_dnsname(char *buf, int len)
2329
2020-07-16
pjp
{
2330
2020-07-16
pjp
char save[DNS_MAXNAME];
2331
2020-07-16
pjp
char randompad[DNS_MAXNAME];
2332
2020-07-16
pjp
char *p, *q;
2333
2020-07-16
pjp
uint offset, labellen;
2334
2020-07-16
pjp
int i;
2335
2020-07-16
pjp
char ch;
2336
2020-07-16
pjp
2337
2020-07-16
pjp
if (len > sizeof(save))
2338
2020-07-16
pjp
return (-1);
2339
2020-07-16
pjp
2340
2020-07-16
pjp
memcpy(save, buf, len);
2341
2020-07-16
pjp
arc4random_buf(randompad, sizeof(randompad));
2342
2020-07-16
pjp
2343
2020-07-16
pjp
q = &buf[0];
2344
2020-07-16
pjp
for (p = q, offset = 0; offset <= len && *p != 0; offset += (*p + 1), p += (*p + 1)) {
2345
2020-07-16
pjp
labellen = *p;
2346
2020-07-16
pjp
2347
2020-07-16
pjp
if (labellen > DNS_MAXLABEL)
2348
2020-07-16
pjp
goto err;
2349
2020-07-16
pjp
2350
2020-07-16
pjp
for (i = 1; i < (1 + labellen); i++) {
2351
2020-07-16
pjp
ch = q[offset + i];
2352
2020-07-16
pjp
q[offset + i] = (randompad[offset + i] & 1) ? toupper(ch) : ch;
2353
2020-07-16
pjp
}
2354
2020-07-16
pjp
}
2355
2020-07-16
pjp
2356
2020-07-16
pjp
if (offset > len)
2357
2020-07-16
pjp
goto err;
2358
2020-07-16
pjp
2359
2020-07-16
pjp
return (0);
2360
2020-07-16
pjp
2361
2020-07-16
pjp
err:
2362
2020-07-16
pjp
/* error condition, restore original buf */
2363
2020-07-16
pjp
memcpy(buf, save, len);
2364
2020-07-16
pjp
return (-1);
2365
2020-07-16
pjp
}
2366
2020-07-16
pjp
2367
2020-07-16
pjp
int
2368
2020-07-16
pjp
lower_dnsname(char *buf, int len)
2369
2020-07-16
pjp
{
2370
2020-07-16
pjp
char *p, *q;
2371
2020-07-16
pjp
char save[DNS_MAXNAME];
2372
2020-07-16
pjp
uint offset, labellen;
2373
2020-07-16
pjp
int i;
2374
2020-07-16
pjp
char ch;
2375
2020-07-16
pjp
2376
2020-07-16
pjp
if (len > sizeof(save))
2377
2020-07-16
pjp
return (-1);
2378
2020-07-16
pjp
2379
2020-07-16
pjp
memcpy(save, buf, len);
2380
2020-07-16
pjp
2381
2020-07-16
pjp
q = &buf[0];
2382
2020-07-16
pjp
for (p = q, offset = 0; offset <= len && *p != 0; offset += (*p + 1), p += (*p + 1)) {
2383
2020-07-16
pjp
labellen = *p;
2384
2020-07-16
pjp
if (labellen > DNS_MAXLABEL)
2385
2020-07-16
pjp
goto err;
2386
2020-07-16
pjp
2387
2020-07-16
pjp
for (i = 1; i < (1 + labellen); i++) {
2388
2020-07-16
pjp
ch = tolower(q[offset + i]);
2389
2020-07-16
pjp
q[offset + i] = ch;
2390
2020-07-16
pjp
}
2391
2020-07-16
pjp
}
2392
2020-07-16
pjp
2393
2020-07-16
pjp
if (offset > len)
2394
2020-07-16
pjp
goto err;
2395
2020-07-16
pjp
2396
2020-07-16
pjp
return (0);
2397
2020-07-16
pjp
2398
2020-07-16
pjp
err:
2399
2020-07-16
pjp
/* restore the old */
2400
2020-07-16
pjp
2401
2020-07-16
pjp
memcpy(buf, save, len);
2402
2020-07-16
pjp
return (-1);
2403
2020-07-21
pjp
}
2404
2020-07-21
pjp
2405
2021-01-04
pjp
2406
2021-01-04
pjp
/*
2407
2021-01-04
pjp
* COMPRESS_LABEL - compress a DNS name, must be passed an entire reply
2408
2021-01-04
pjp
* with the to be compressed name before the offset of
2409
2021-01-04
pjp
* that reply.
2410
2021-01-04
pjp
*/
2411
2021-01-04
pjp
2412
2021-01-04
pjp
int
2413
2021-01-04
pjp
compress_label(u_char *buf, u_int16_t offset, int labellen)
2414
2021-01-04
pjp
{
2415
2021-01-04
pjp
u_char *label[10000];
2416
2021-01-04
pjp
u_char *end = &buf[offset];
2417
2021-01-04
pjp
struct question {
2418
2021-01-04
pjp
u_int16_t type;
2419
2021-01-04
pjp
u_int16_t class;
2420
2021-01-04
pjp
} __attribute__((packed));
2421
2021-01-04
pjp
struct answer {
2422
2021-01-04
pjp
u_int16_t type;
2423
2021-01-04
pjp
u_int16_t class;
2424
2021-01-04
pjp
u_int32_t ttl;
2425
2021-01-04
pjp
u_int16_t rdlength;
2426
2021-01-04
pjp
} __attribute__((packed));
2427
2021-01-04
pjp
struct soa {
2428
2021-01-04
pjp
u_int32_t serial;
2429
2021-01-04
pjp
u_int32_t refresh;
2430
2021-01-04
pjp
u_int32_t retry;
2431
2021-01-04
pjp
u_int32_t expire;
2432
2021-01-04
pjp
u_int32_t minttl;
2433
2021-01-04
pjp
} __attribute__((packed));
2434
2021-01-04
pjp
2435
2021-01-04
pjp
struct answer *a;
2436
2021-01-04
pjp
2437
2021-01-04
pjp
u_int i, j;
2438
2021-01-04
pjp
u_int checklen;
2439
2021-01-04
pjp
2440
2021-01-04
pjp
u_char *p, *e;
2441
2021-01-04
pjp
u_char *compressmark;
2442
2021-01-04
pjp
2443
2021-01-04
pjp
int elen;
2444
2021-01-04
pjp
char expand[DNS_MAXNAME + 1];
2445
2021-01-04
pjp
char *end_name = NULL;
2446
2021-01-04
pjp
2447
2021-01-04
pjp
2448
2021-01-04
pjp
p = &buf[sizeof(struct dns_header)];
2449
2021-01-04
pjp
label[0] = p;
2450
2021-01-04
pjp
2451
2021-01-04
pjp
elen = 0;
2452
2021-01-04
pjp
memset(&expand, 0, sizeof(expand));
2453
2021-01-04
pjp
end_name = expand_compression((u_char *)&buf[sizeof(struct dns_header)],(u_char *)buf, (u_char *)&buf[offset], (u_char *)&expand, &elen, sizeof(expand));
2454
2021-01-04
pjp
if (end_name == NULL) {
2455
2021-01-04
pjp
dolog(LOG_ERR, "expand_compression() failed, bad formatted question name\n");
2456
2021-01-04
pjp
return(0);
2457
2021-01-04
pjp
}
2458
2021-01-04
pjp
2459
2021-01-04
pjp
if (((char *)end_name - (char *)buf) < elen) {
2460
2021-01-04
pjp
dolog(LOG_ERR, "compression in question compress_label #1\n");
2461
2021-01-04
pjp
return(0);
2462
2021-01-04
pjp
}
2463
2021-01-04
pjp
2464
2021-01-04
pjp
p = end_name;
2465
2021-01-04
pjp
2466
2021-01-04
pjp
p += sizeof(struct question);
2467
2021-01-04
pjp
p++; /* one more */
2468
2021-01-04
pjp
/* start of answer/additional/authoritative */
2469
2021-01-04
pjp
/* XXX 10000 in case of AXFR should satisfy a envelope size 64K */
2470
2021-01-04
pjp
for (i = 1; i < 10000; i++) {
2471
2021-01-04
pjp
label[i] = p;
2472
2021-01-04
pjp
2473
2021-01-04
pjp
while (p <= end && *p) {
2474
2021-01-04
pjp
if ((*p & 0xc0) == 0xc0) {
2475
2021-01-04
pjp
p++;
2476
2021-01-04
pjp
break;
2477
2021-01-04
pjp
}
2478
2021-01-04
pjp
p += *p;
2479
2021-01-04
pjp
p++;
2480
2021-01-04
pjp
2481
2021-01-04
pjp
if (p >= end)
2482
2021-01-04
pjp
goto end;
2483
2021-01-04
pjp
}
2484
2021-01-04
pjp
2485
2021-01-04
pjp
p++; /* one more */
2486
2021-01-04
pjp
2487
2021-01-04
pjp
2488
2021-01-04
pjp
a = (struct answer *)p;
2489
2021-01-04
pjp
p += sizeof(struct answer);
2490
2021-01-04
pjp
2491
2021-01-04
pjp
/* Thanks FreeLogic! */
2492
2021-01-04
pjp
if (p >= end)
2493
2021-01-04
pjp
goto end;
2494
2021-01-04
pjp
2495
2021-01-04
pjp
switch (ntohs(a->type)) {
2496
2021-01-04
pjp
case DNS_TYPE_A:
2497
2021-01-04
pjp
p += sizeof(in_addr_t);
2498
2021-01-04
pjp
break;
2499
2021-01-04
pjp
case DNS_TYPE_AAAA:
2500
2021-01-04
pjp
p += 16; /* sizeof 4 * 32 bit */
2501
2021-01-04
pjp
break;
2502
2021-01-04
pjp
case DNS_TYPE_TXT:
2503
2021-01-04
pjp
p += *p;
2504
2021-01-04
pjp
p++;
2505
2021-01-04
pjp
break;
2506
2021-01-04
pjp
case DNS_TYPE_TLSA:
2507
2021-01-04
pjp
p += 2;
2508
2021-01-04
pjp
switch (*p) {
2509
2021-01-04
pjp
case 1:
2510
2021-01-04
pjp
p += DNS_TLSA_SIZE_SHA256 + 1;
2511
2021-01-04
pjp
break;
2512
2021-01-04
pjp
case 2:
2513
2021-01-04
pjp
p += DNS_TLSA_SIZE_SHA512 + 1;
2514
2021-01-04
pjp
break;
2515
2021-01-04
pjp
default:
2516
2021-01-04
pjp
/* XXX */
2517
2021-01-04
pjp
goto end;
2518
2021-01-04
pjp
}
2519
2021-01-04
pjp
2520
2021-01-04
pjp
break;
2521
2021-01-04
pjp
case DNS_TYPE_SSHFP:
2522
2021-01-04
pjp
p++;
2523
2021-01-04
pjp
switch (*p) {
2524
2021-01-04
pjp
case 1:
2525
2021-01-04
pjp
p += DNS_SSHFP_SIZE_SHA1 + 1;
2526
2021-01-04
pjp
break;
2527
2021-01-04
pjp
case 2:
2528
2021-01-04
pjp
p += DNS_SSHFP_SIZE_SHA256 + 1;
2529
2021-01-04
pjp
break;
2530
2021-01-04
pjp
default:
2531
2021-01-04
pjp
/* XXX */
2532
2021-01-04
pjp
goto end;
2533
2021-01-04
pjp
}
2534
2021-01-04
pjp
2535
2021-01-04
pjp
break;
2536
2021-01-04
pjp
case DNS_TYPE_SRV:
2537
2021-01-04
pjp
p += (2 * sizeof(u_int16_t)); /* priority, weight */
2538
2021-01-04
pjp
/* the port will be assumed in the fall through for
2539
2021-01-04
pjp
mx_priority..
2540
2021-01-04
pjp
*/
2541
2021-01-04
pjp
/* FALLTHROUGH */
2542
2021-01-04
pjp
case DNS_TYPE_MX:
2543
2021-01-04
pjp
p += sizeof(u_int16_t); /* mx_priority */
2544
2021-01-04
pjp
/* FALLTHROUGH */
2545
2021-01-04
pjp
case DNS_TYPE_NS:
2546
2021-01-04
pjp
case DNS_TYPE_PTR:
2547
2021-01-04
pjp
case DNS_TYPE_CNAME:
2548
2021-01-04
pjp
label[++i] = p;
2549
2021-01-04
pjp
while (p <= end && *p) {
2550
2021-01-04
pjp
if ((*p & 0xc0) == 0xc0) {
2551
2021-01-04
pjp
p++;
2552
2021-01-04
pjp
break;
2553
2021-01-04
pjp
}
2554
2021-01-04
pjp
p += *p;
2555
2021-01-04
pjp
p++;
2556
2021-01-04
pjp
2557
2021-01-04
pjp
if (p >= end)
2558
2021-01-04
pjp
goto end;
2559
2021-01-04
pjp
}
2560
2021-01-04
pjp
2561
2021-01-04
pjp
p++; /* one more */
2562
2021-01-04
pjp
break;
2563
2021-01-04
pjp
case DNS_TYPE_RP:
2564
2021-01-04
pjp
label[++i] = p;
2565
2021-01-04
pjp
while (p <= end && *p) {
2566
2021-01-04
pjp
if ((*p & 0xc0) == 0xc0) {
2567
2021-01-04
pjp
p++;
2568
2021-01-04
pjp
break;
2569
2021-01-04
pjp
}
2570
2021-01-04
pjp
p += *p;
2571
2021-01-04
pjp
p++;
2572
2021-01-04
pjp
2573
2021-01-04
pjp
if (p >= end)
2574
2021-01-04
pjp
goto end;
2575
2021-01-04
pjp
}
2576
2021-01-04
pjp
2577
2021-01-04
pjp
p++; /* one more */
2578
2021-01-04
pjp
2579
2021-01-04
pjp
if (p >= end)
2580
2021-01-04
pjp
goto end;
2581
2021-01-04
pjp
2582
2021-01-04
pjp
label[++i] = p;
2583
2021-01-04
pjp
while (p <= end && *p) {
2584
2021-01-04
pjp
if ((*p & 0xc0) == 0xc0) {
2585
2021-01-04
pjp
p++;
2586
2021-01-04
pjp
break;
2587
2021-01-04
pjp
}
2588
2021-01-04
pjp
p += *p;
2589
2021-01-04
pjp
p++;
2590
2021-01-04
pjp
2591
2021-01-04
pjp
if (p >= end)
2592
2021-01-04
pjp
goto end;
2593
2021-01-04
pjp
}
2594
2021-01-04
pjp
2595
2021-01-04
pjp
p++; /* one more */
2596
2021-01-04
pjp
2597
2021-01-04
pjp
if (p >= end)
2598
2021-01-04
pjp
goto end;
2599
2021-01-04
pjp
2600
2021-01-04
pjp
break;
2601
2021-01-04
pjp
case DNS_TYPE_SOA:
2602
2021-01-04
pjp
/* nsserver */
2603
2021-01-04
pjp
label[++i] = p;
2604
2021-01-04
pjp
while (p <= end && *p) {
2605
2021-01-04
pjp
if ((*p & 0xc0) == 0xc0) {
2606
2021-01-04
pjp
p++;
2607
2021-01-04
pjp
break;
2608
2021-01-04
pjp
}
2609
2021-01-04
pjp
p += *p;
2610
2021-01-04
pjp
p++;
2611
2021-01-04
pjp
if (p >= end)
2612
2021-01-04
pjp
goto end;
2613
2021-01-04
pjp
}
2614
2021-01-04
pjp
2615
2021-01-04
pjp
p++; /* one more */
2616
2021-01-04
pjp
2617
2021-01-04
pjp
if (p >= end)
2618
2021-01-04
pjp
break;
2619
2021-01-04
pjp
2620
2021-01-04
pjp
/* responsible person */
2621
2021-01-04
pjp
label[++i] = p;
2622
2021-01-04
pjp
while (p <= end && *p) {
2623
2021-01-04
pjp
if ((*p & 0xc0) == 0xc0) {
2624
2021-01-04
pjp
p++;
2625
2021-01-04
pjp
break;
2626
2021-01-04
pjp
}
2627
2021-01-04
pjp
p += *p;
2628
2021-01-04
pjp
p++;
2629
2021-01-04
pjp
}
2630
2021-01-04
pjp
2631
2021-01-04
pjp
p++; /* one more */
2632
2021-01-04
pjp
2633
2021-01-04
pjp
if (p >= end)
2634
2021-01-04
pjp
break;
2635
2021-01-04
pjp
2636
2021-01-04
pjp
p += sizeof(struct soa); /* advance struct soa */
2637
2021-01-04
pjp
2638
2021-01-04
pjp
break;
2639
2021-01-04
pjp
case DNS_TYPE_NAPTR:
2640
2021-01-04
pjp
p += (2 * sizeof(u_int16_t)); /* order and preference */
2641
2021-01-04
pjp
p += *p; /* flags */
2642
2021-01-04
pjp
p++;
2643
2021-01-04
pjp
p += *p; /* services */
2644
2021-01-04
pjp
p++;
2645
2021-01-04
pjp
p += *p; /* regexp */
2646
2021-01-04
pjp
p++;
2647
2021-01-04
pjp
2648
2021-01-04
pjp
label[++i] = p;
2649
2021-01-04
pjp
while (p <= end && *p) {
2650
2021-01-04
pjp
if ((*p & 0xc0) == 0xc0) {
2651
2021-01-04
pjp
p++;
2652
2021-01-04
pjp
break;
2653
2021-01-04
pjp
}
2654
2021-01-04
pjp
p += *p;
2655
2021-01-04
pjp
p++;
2656
2021-01-04
pjp
2657
2021-01-04
pjp
if (p >= end)
2658
2021-01-04
pjp
goto end;
2659
2021-01-04
pjp
}
2660
2021-01-04
pjp
2661
2021-01-04
pjp
p++; /* one more */
2662
2021-01-04
pjp
break;
2663
2021-01-04
pjp
2664
2021-01-04
pjp
case DNS_TYPE_DNSKEY:
2665
2021-01-04
pjp
case DNS_TYPE_NSEC3:
2666
2021-01-04
pjp
case DNS_TYPE_NSEC3PARAM:
2667
2021-01-04
pjp
case DNS_TYPE_RRSIG:
2668
2021-01-04
pjp
case DNS_TYPE_CAA:
2669
2021-01-04
pjp
case DNS_TYPE_HINFO:
2670
2021-01-04
pjp
/* above six FALLTHROUGH */
2671
2021-01-04
pjp
default:
2672
2021-01-04
pjp
p += a->rdlength;
2673
2021-01-04
pjp
break;
2674
2021-01-04
pjp
} /* switch */
2675
2021-01-04
pjp
2676
2021-01-04
pjp
if (p >= end)
2677
2021-01-04
pjp
break;
2678
2021-01-04
pjp
} /* for (i *) */
2679
2021-01-04
pjp
2680
2021-01-04
pjp
end:
2681
2021-01-04
pjp
2682
2021-01-04
pjp
p = &buf[offset - labellen];
2683
2021-01-04
pjp
checklen = labellen;
2684
2021-01-04
pjp
2685
2021-01-04
pjp
for (;*p != 0;) {
2686
2021-01-04
pjp
for (j = 0; j < i; j++) {
2687
2021-01-04
pjp
for (e = label[j]; *e; e += *e, e++) {
2688
2021-01-04
pjp
if ((*e & 0xc0) == 0xc0)
2689
2021-01-04
pjp
break;
2690
2021-01-04
pjp
2691
2021-01-04
pjp
if (memcasecmp(e, p, checklen) == 0) {
2692
2021-01-04
pjp
/* e is now our compress offset */
2693
2021-01-04
pjp
compressmark = e;
2694
2021-01-04
pjp
goto out; /* found one */
2695
2021-01-04
pjp
}
2696
2021-01-04
pjp
} /* for (e .. */
2697
2021-01-04
pjp
2698
2021-01-04
pjp
} /* for (j .. */
2699
2021-01-04
pjp
2700
2021-01-04
pjp
if (*p > DNS_MAXLABEL)
2701
2021-01-04
pjp
return 0; /* totally bogus label */
2702
2021-01-04
pjp
2703
2021-01-04
pjp
checklen -= *p;
2704
2021-01-04
pjp
p += *p;
2705
2021-01-04
pjp
checklen--;
2706
2021-01-04
pjp
p++;
2707
2021-01-04
pjp
}
2708
2021-01-04
pjp
2709
2021-01-04
pjp
return (0); /* no compression possible */
2710
2021-01-04
pjp
2711
2021-01-04
pjp
out:
2712
2021-01-04
pjp
/* take off our compress length */
2713
2021-01-04
pjp
offset -= checklen;
2714
2021-01-04
pjp
/* write compressed label */
2715
2021-01-04
pjp
pack16(&buf[offset], htons((compressmark - &buf[0]) | 0xc000));
2716
2021-01-04
pjp
2717
2021-01-04
pjp
offset += sizeof(u_int16_t);
2718
2021-01-04
pjp
2719
2021-01-04
pjp
return (offset);
2720
2021-01-04
pjp
}
2721
2020-07-21
pjp
2722
2020-07-21
pjp
/*
2723
2020-07-21
pjp
* Copyright (c) 1988, 1992, 1993
2724
2020-07-21
pjp
* The Regents of the University of California. All rights reserved.
2725
2020-07-21
pjp
*
2726
2020-07-21
pjp
* Redistribution and use in source and binary forms, with or without
2727
2020-07-21
pjp
* modification, are permitted provided that the following conditions
2728
2020-07-21
pjp
* are met:
2729
2020-07-21
pjp
* 1. Redistributions of source code must retain the above copyright
2730
2020-07-21
pjp
* notice, this list of conditions and the following disclaimer.
2731
2020-07-21
pjp
* 2. Redistributions in binary form must reproduce the above copyright
2732
2020-07-21
pjp
* notice, this list of conditions and the following disclaimer in the
2733
2020-07-21
pjp
* documentation and/or other materials provided with the distribution.
2734
2020-07-21
pjp
* 3. Neither the name of the University nor the names of its contributors
2735
2020-07-21
pjp
* may be used to endorse or promote products derived from this software
2736
2020-07-21
pjp
* without specific prior written permission.
2737
2020-07-21
pjp
*
2738
2020-07-21
pjp
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2739
2020-07-21
pjp
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2740
2020-07-21
pjp
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2741
2020-07-21
pjp
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2742
2020-07-21
pjp
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2743
2020-07-21
pjp
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2744
2020-07-21
pjp
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2745
2020-07-21
pjp
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2746
2020-07-21
pjp
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2747
2020-07-21
pjp
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2748
2020-07-21
pjp
* SUCH DAMAGE.
2749
2020-07-21
pjp
*
2750
2020-07-21
pjp
* @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
2751
2020-07-21
pjp
*/
2752
2020-07-21
pjp
2753
2020-07-21
pjp
/*
2754
2020-07-21
pjp
* UDP_CKSUM - compute the ones complement sum of the ones complement of 16 bit
2755
2020-07-21
pjp
* numbers
2756
2020-07-21
pjp
*/
2757
2020-07-21
pjp
2758
2020-07-21
pjp
2759
2020-07-21
pjp
2760
2020-07-21
pjp
/*
2761
2020-07-21
pjp
* UDP_CKSUM - compute the checksum with a pseudo header of the UDP packet
2762
2020-07-21
pjp
*
2763
2020-07-21
pjp
*/
2764
2020-07-21
pjp
2765
2020-07-21
pjp
uint16_t
2766
2020-07-21
pjp
udp_cksum(u_int16_t *addr, uint16_t len, struct ip *ip, struct udphdr *uh)
2767
2020-07-21
pjp
{
2768
2020-07-21
pjp
union {
2769
2020-07-21
pjp
struct ph {
2770
2020-07-21
pjp
in_addr_t src;
2771
2020-07-21
pjp
in_addr_t dst;
2772
2020-07-21
pjp
u_int8_t pad;
2773
2020-07-21
pjp
u_int8_t proto;
2774
2020-07-21
pjp
u_int16_t len;
2775
2020-07-21
pjp
} s __attribute__((packed));
2776
2020-07-21
pjp
2777
2020-07-21
pjp
u_int16_t i[6];
2778
2020-07-21
pjp
} ph;
2779
2020-07-21
pjp
2780
2020-07-21
pjp
int nleft = len - sizeof(struct udphdr); /* we pass the udp header */
2781
2020-07-21
pjp
int sum = 0;
2782
2020-07-21
pjp
u_int16_t *w = &ph.i[0];
2783
2020-07-21
pjp
u_int16_t *u = (u_int16_t *)uh;
2784
2020-07-21
pjp
uint16_t answer;
2785
2020-07-21
pjp
2786
2020-07-21
pjp
memset(&ph, 0, sizeof(ph));
2787
2020-07-21
pjp
memcpy(&ph.s.src, &ip->ip_src.s_addr, sizeof(in_addr_t));
2788
2020-07-21
pjp
memcpy(&ph.s.dst, &ip->ip_dst.s_addr, sizeof(in_addr_t));
2789
2020-07-21
pjp
ph.s.pad = 0;
2790
2020-07-21
pjp
ph.s.proto = ip->ip_p;
2791
2020-07-21
pjp
ph.s.len = uh->uh_ulen;
2792
2020-07-21
pjp
sum = w[0] + w[1] + w[2] + w[3] + w[4] + w[5] + u[0] + u[1] + u[2];
2793
2020-07-21
pjp
w = addr;
2794
2020-07-21
pjp
2795
2020-07-21
pjp
while (nleft > 1) {
2796
2020-07-21
pjp
sum += *w++;
2797
2020-07-21
pjp
nleft -= 2;
2798
2020-07-21
pjp
}
2799
2020-07-21
pjp
if (nleft == 1) {
2800
2020-07-21
pjp
sum += htons(*(u_char *)w << 8);
2801
2020-07-21
pjp
}
2802
2020-07-21
pjp
2803
2020-07-21
pjp
sum = (sum >> 16) + (sum & 0xffff);
2804
2020-07-21
pjp
sum += (sum >> 16);
2805
2020-07-21
pjp
answer = ~sum;
2806
2020-07-21
pjp
return (answer);
2807
2020-07-21
pjp
}
2808
2020-07-21
pjp
2809
2020-07-21
pjp
/*
2810
2020-07-21
pjp
* UDP_CKSUM6 - compute the checksum with a pseudo header of the UDP6 packet
2811
2020-07-21
pjp
* RFC 8200 section 8.1
2812
2020-07-21
pjp
*/
2813
2020-07-21
pjp
2814
2020-07-21
pjp
uint16_t
2815
2020-07-21
pjp
udp_cksum6(u_int16_t *addr, uint16_t len, struct ip6_hdr *ip6, struct udphdr *uh)
2816
2020-07-21
pjp
{
2817
2020-07-21
pjp
union {
2818
2020-07-21
pjp
struct ph {
2819
2020-07-21
pjp
struct in6_addr src;
2820
2020-07-21
pjp
struct in6_addr dst;
2821
2020-07-21
pjp
u_int32_t len;
2822
2020-07-21
pjp
u_int8_t pad[3];
2823
2020-07-21
pjp
u_int8_t nxt;
2824
2020-07-21
pjp
} s __attribute__((packed));
2825
2020-07-21
pjp
2826
2020-07-21
pjp
u_int16_t i[20];
2827
2020-07-21
pjp
} ph;
2828
2020-07-21
pjp
2829
2020-07-21
pjp
int nleft = len - sizeof(struct udphdr); /* we pass the udp header */
2830
2020-07-21
pjp
int sum;
2831
2020-07-21
pjp
u_int16_t *w = &ph.i[0];
2832
2020-07-21
pjp
u_int16_t *u = (u_int16_t *)uh;
2833
2020-07-21
pjp
uint16_t answer;
2834
2020-07-21
pjp
2835
2020-07-21
pjp
memset(&ph, 0, sizeof(ph));
2836
2020-07-21
pjp
memcpy(&ph.s.src, &ip6->ip6_src, sizeof(struct in6_addr));
2837
2020-07-21
pjp
memcpy(&ph.s.dst, &ip6->ip6_dst, sizeof(struct in6_addr));
2838
2020-07-21
pjp
ph.s.len = htonl(len);
2839
2020-07-21
pjp
ph.s.nxt = ip6->ip6_nxt;
2840
2020-07-21
pjp
2841
2020-07-21
pjp
sum = w[0] + w[1] + w[2] + w[3] + w[4] + w[5] + \
2842
2020-07-21
pjp
w[6] + w[7] + w[8] + w[9] + w[10] + \
2843
2020-07-21
pjp
w[11] + w[12] + w[13] + w[14] + w[15] + \
2844
2020-07-21
pjp
w[16] + w[17] + w[18] + w[19] + u[0] + u[1] + u[2];
2845
2020-07-21
pjp
2846
2020-07-21
pjp
w = addr;
2847
2020-07-21
pjp
2848
2020-07-21
pjp
while (nleft > 1) {
2849
2020-07-21
pjp
sum += *w++;
2850
2020-07-21
pjp
nleft -= 2;
2851
2020-07-21
pjp
}
2852
2020-07-21
pjp
if (nleft == 1) {
2853
2020-07-21
pjp
sum += htons(*(u_char *)w << 8);
2854
2020-07-21
pjp
}
2855
2020-07-21
pjp
2856
2020-07-21
pjp
sum = (sum >> 16) + (sum & 0xffff);
2857
2020-07-21
pjp
sum += (sum >> 16);
2858
2020-07-21
pjp
answer = ~sum;
2859
2020-07-21
pjp
return (answer);
2860
2019-11-12
pjp
}
repomaster@centroid.eu