Blame


1 c45084ea 2005-11-29 pbug /*
2 48603f7d 2014-04-13 pjp * Copyright (c) 2002-2014 Peter J. Philipp
3 c45084ea 2005-11-29 pbug * All rights reserved.
4 c45084ea 2005-11-29 pbug *
5 c45084ea 2005-11-29 pbug * Redistribution and use in source and binary forms, with or without
6 c45084ea 2005-11-29 pbug * modification, are permitted provided that the following conditions
7 c45084ea 2005-11-29 pbug * are met:
8 c45084ea 2005-11-29 pbug * 1. Redistributions of source code must retain the above copyright
9 c45084ea 2005-11-29 pbug * notice, this list of conditions and the following disclaimer.
10 c45084ea 2005-11-29 pbug * 2. Redistributions in binary form must reproduce the above copyright
11 c45084ea 2005-11-29 pbug * notice, this list of conditions and the following disclaimer in the
12 c45084ea 2005-11-29 pbug * documentation and/or other materials provided with the distribution.
13 c45084ea 2005-11-29 pbug * 3. The name of the author may not be used to endorse or promote products
14 c45084ea 2005-11-29 pbug * derived from this software without specific prior written permission
15 c45084ea 2005-11-29 pbug *
16 c45084ea 2005-11-29 pbug * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 c45084ea 2005-11-29 pbug * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 c45084ea 2005-11-29 pbug * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 c45084ea 2005-11-29 pbug * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 c45084ea 2005-11-29 pbug * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 c45084ea 2005-11-29 pbug * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 c45084ea 2005-11-29 pbug * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 c45084ea 2005-11-29 pbug * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 c45084ea 2005-11-29 pbug * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 c45084ea 2005-11-29 pbug * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 c45084ea 2005-11-29 pbug *
27 c45084ea 2005-11-29 pbug */
28 c45084ea 2005-11-29 pbug #include "include.h"
29 c45084ea 2005-11-29 pbug #include "dns.h"
30 c45084ea 2005-11-29 pbug #include "db.h"
31 187cc902 2011-04-22 pbug #include "config.h"
32 c45084ea 2005-11-29 pbug
33 c45084ea 2005-11-29 pbug /* prototypes */
34 c45084ea 2005-11-29 pbug
35 0e7d94ac 2014-05-18 pjp extern void add_rrlimit(int, u_int16_t *, int, char *);
36 0e7d94ac 2014-05-18 pjp extern void axfrloop(int *, int, char **, DB *);
37 0e7d94ac 2014-05-18 pjp extern int check_rrlimit(int, u_int16_t *, int, char *);
38 0e7d94ac 2014-05-18 pjp extern void collects_init(void);
39 0e7d94ac 2014-05-18 pjp extern void dolog(int, char *, ...);
40 0e7d94ac 2014-05-18 pjp extern int find_filter(struct sockaddr_storage *, int);
41 0e7d94ac 2014-05-18 pjp extern int find_recurse(struct sockaddr_storage *, int);
42 457912e1 2014-05-18 pjp extern u_int8_t find_region(struct sockaddr_storage *, int);
43 6f8d6a57 2014-05-18 pjp extern int find_whitelist(struct sockaddr_storage *, int);
44 0e7d94ac 2014-05-18 pjp extern int find_wildcard(struct sockaddr_storage *, int);
45 0e7d94ac 2014-05-18 pjp extern void init_wildcard(void);
46 0e7d94ac 2014-05-18 pjp extern void init_recurse(void);
47 0e7d94ac 2014-05-18 pjp extern void init_region(void);
48 0e7d94ac 2014-05-18 pjp extern void init_filter(void);
49 0e7d94ac 2014-05-18 pjp extern void init_notifyslave(void);
50 6f8d6a57 2014-05-18 pjp extern void init_whitelist(void);
51 0e7d94ac 2014-05-18 pjp extern void recurseloop(int sp, int *, DB *);
52 0e7d94ac 2014-05-18 pjp extern void receivelog(char *, int);
53 a2b189d4 2014-10-08 pjp extern int reply_a(struct sreply *, DB *);
54 a2b189d4 2014-10-08 pjp extern int reply_aaaa(struct sreply *, DB *);
55 a2b189d4 2014-10-08 pjp extern int reply_any(struct sreply *);
56 a2b189d4 2014-10-08 pjp extern int reply_cname(struct sreply *);
57 a2b189d4 2014-10-08 pjp extern int reply_fmterror(struct sreply *);
58 a2b189d4 2014-10-08 pjp extern int reply_notimpl(struct sreply *);
59 a2b189d4 2014-10-08 pjp extern int reply_nxdomain(struct sreply *);
60 a2b189d4 2014-10-08 pjp extern int reply_noerror(struct sreply *);
61 a2b189d4 2014-10-08 pjp extern int reply_soa(struct sreply *);
62 a2b189d4 2014-10-08 pjp extern int reply_mx(struct sreply *, DB *);
63 a2b189d4 2014-10-08 pjp extern int reply_naptr(struct sreply *, DB *);
64 a2b189d4 2014-10-08 pjp extern int reply_ns(struct sreply *, DB *);
65 a2b189d4 2014-10-08 pjp extern int reply_ptr(struct sreply *);
66 a2b189d4 2014-10-08 pjp extern int reply_refused(struct sreply *);
67 a2b189d4 2014-10-08 pjp extern int reply_spf(struct sreply *);
68 a2b189d4 2014-10-08 pjp extern int reply_srv(struct sreply *, DB *);
69 a2b189d4 2014-10-08 pjp extern int reply_sshfp(struct sreply *);
70 a2b189d4 2014-10-08 pjp extern int reply_txt(struct sreply *);
71 0e7d94ac 2014-05-18 pjp extern int remotelog(int, char *, ...);
72 0e7d94ac 2014-05-18 pjp extern char *rrlimit_setup(int);
73 a042435d 2010-03-19 pbug
74 0e7d94ac 2014-05-18 pjp struct question *build_fake_question(char *, int, u_int16_t);
75 0e7d94ac 2014-05-18 pjp struct question *build_question(char *, int, int);
76 0e7d94ac 2014-05-18 pjp void build_reply(struct sreply *, int, char *, int, struct question *, struct sockaddr *, socklen_t, struct domain *, struct domain *, u_int8_t, int, int, struct recurses *, char *);
77 0e7d94ac 2014-05-18 pjp int compress_label(u_char *, u_int16_t, int);
78 0e7d94ac 2014-05-18 pjp u_int16_t check_qtype(struct domain *, u_int16_t, int, int *);
79 0e7d94ac 2014-05-18 pjp char *dns_label(char *, int *);
80 0e7d94ac 2014-05-18 pjp int free_question(struct question *);
81 0e7d94ac 2014-05-18 pjp char *get_dns_type(int dnstype);
82 0e7d94ac 2014-05-18 pjp int get_soa(DB *, struct question *, struct domain *, int);
83 0e7d94ac 2014-05-18 pjp int lookup_zone(DB *, struct question *, struct domain *, int *, char *, int);
84 0e7d94ac 2014-05-18 pjp void mainloop(struct cfg *);
85 0e7d94ac 2014-05-18 pjp void master_reload(int);
86 0e7d94ac 2014-05-18 pjp void master_shutdown(int);
87 0e7d94ac 2014-05-18 pjp int memcasecmp(u_char *, u_char *, int);
88 0e7d94ac 2014-05-18 pjp void recurseheader(struct srecurseheader *, int, struct sockaddr_storage *, struct sockaddr_storage *, int);
89 c3e0e8b7 2014-09-29 pjp void setup_master(DB *, DB_ENV *, char **);
90 0e7d94ac 2014-05-18 pjp void slave_signal(int);
91 0e7d94ac 2014-05-18 pjp void slave_shutdown(void);
92 06aa4e57 2011-04-12 pbug
93 eea748bc 2008-07-10 pbug /* aliases */
94 eea748bc 2008-07-10 pbug
95 187cc902 2011-04-22 pbug #ifndef DEFAULT_PRIVILEGE
96 187cc902 2011-04-22 pbug #define DEFAULT_PRIVILEGE "wdnsd"
97 187cc902 2011-04-22 pbug #endif
98 187cc902 2011-04-22 pbug
99 0aefb92a 2011-04-12 pbug #define PIDFILE "/var/run/wildcarddnsd.pid"
100 f98bb34d 2011-09-19 pbug #define MYDB_PATH "/var/db/wdns"
101 c45084ea 2005-11-29 pbug
102 c37b91b6 2009-03-10 pbug
103 c37b91b6 2009-03-10 pbug struct typetable {
104 c37b91b6 2009-03-10 pbug char *type;
105 c37b91b6 2009-03-10 pbug int number;
106 c37b91b6 2009-03-10 pbug } TT[] = {
107 c37b91b6 2009-03-10 pbug { "A", DNS_TYPE_A},
108 c37b91b6 2009-03-10 pbug { "NS", DNS_TYPE_NS},
109 c37b91b6 2009-03-10 pbug { "CNAME", DNS_TYPE_CNAME},
110 c37b91b6 2009-03-10 pbug { "SOA", DNS_TYPE_SOA},
111 c37b91b6 2009-03-10 pbug { "PTR", DNS_TYPE_PTR},
112 c37b91b6 2009-03-10 pbug { "MX", DNS_TYPE_MX},
113 c37b91b6 2009-03-10 pbug { "TXT", DNS_TYPE_TXT},
114 c37b91b6 2009-03-10 pbug { "AAAA", DNS_TYPE_AAAA},
115 0d95ac68 2010-12-27 pbug { "ANY", DNS_TYPE_ANY },
116 6f8190d3 2012-04-30 pbug { "SRV", DNS_TYPE_SRV },
117 f5989689 2014-04-21 pjp { "SPF", DNS_TYPE_SPF },
118 df34d218 2014-04-21 pjp { "SSHFP", DNS_TYPE_SSHFP },
119 1f1faf13 2014-05-11 pjp { "NAPTR", DNS_TYPE_NAPTR },
120 c37b91b6 2009-03-10 pbug { NULL, 0}
121 c37b91b6 2009-03-10 pbug };
122 eea748bc 2008-07-10 pbug
123 eea748bc 2008-07-10 pbug
124 eea748bc 2008-07-10 pbug /* global variables */
125 eea748bc 2008-07-10 pbug
126 c45084ea 2005-11-29 pbug extern char *__progname;
127 c25e4088 2011-06-28 pbug extern struct logging logging;
128 f98bb34d 2011-09-19 pbug extern int axfrport;
129 4667e76d 2014-05-05 pjp extern int ratelimit;
130 4667e76d 2014-05-05 pjp extern int ratelimit_packets_per_second;
131 6f8d6a57 2014-05-18 pjp extern int whitelist;
132 dd869383 2013-02-16 pjp
133 06aa4e57 2011-04-12 pbug static int *ptr = NULL;
134 99ed55ed 2011-04-12 pbug static int reload = 0;
135 f98bb34d 2011-09-19 pbug static int mshutdown = 0;
136 f98bb34d 2011-09-19 pbug static int msig;
137 f98bb34d 2011-09-19 pbug static char *database;
138 f98bb34d 2011-09-19 pbug static char mydatabase[512];
139 4667e76d 2014-05-05 pjp static char *rptr;
140 d64396b7 2014-05-09 pjp static int ratelimit_backlog;
141 57ffa3f9 2008-04-15 pbug
142 0f80aa4d 2014-04-15 pjp int debug = 0;
143 0f80aa4d 2014-04-15 pjp int verbose = 0;
144 0f80aa4d 2014-04-15 pjp int bflag = 0;
145 0f80aa4d 2014-04-15 pjp int iflag = 0;
146 0f80aa4d 2014-04-15 pjp int lflag = 0;
147 0f80aa4d 2014-04-15 pjp int nflag = 0;
148 0f80aa4d 2014-04-15 pjp int rflag = 0;
149 0f80aa4d 2014-04-15 pjp int bcount = 0;
150 0f80aa4d 2014-04-15 pjp int icount = 0;
151 0f80aa4d 2014-04-15 pjp u_int16_t port = 53;
152 0f80aa4d 2014-04-15 pjp u_int32_t cachesize = 0;
153 0f80aa4d 2014-04-15 pjp char *bind_list[255];
154 0f80aa4d 2014-04-15 pjp char *interface_list[255];
155 dd869383 2013-02-16 pjp
156 f1e3cfca 2010-03-12 pbug /* singly linked list for tcp operations */
157 f1e3cfca 2010-03-12 pbug SLIST_HEAD(listhead, tcps) tcpshead;
158 c45084ea 2005-11-29 pbug
159 c0963faf 2014-05-01 pjp static struct tcps {
160 f1e3cfca 2010-03-12 pbug char *input;
161 f1e3cfca 2010-03-12 pbug char *ident;
162 f1e3cfca 2010-03-12 pbug char *address;
163 f1e3cfca 2010-03-12 pbug int offset;
164 f1e3cfca 2010-03-12 pbug int length;
165 f1e3cfca 2010-03-12 pbug int maxlen;
166 f1e3cfca 2010-03-12 pbug int so;
167 f1e3cfca 2010-03-12 pbug int isv6;
168 959d1769 2010-04-05 pbug u_int8_t region;
169 959d1769 2010-04-05 pbug int wildcard;
170 f1e3cfca 2010-03-12 pbug time_t time;
171 c0963faf 2014-05-01 pjp SLIST_ENTRY(tcps) tcps_entry;
172 ec3f221d 2014-05-01 pjp } *tn1, *tnp, *tntmp;
173 f1e3cfca 2010-03-12 pbug
174 f1e3cfca 2010-03-12 pbug
175 27110135 2014-11-07 pjp static const char rcsid[] = "$Id: main.c,v 1.103 2014/11/07 10:33:12 pjp Exp $";
176 c25e4088 2011-06-28 pbug
177 c45084ea 2005-11-29 pbug /*
178 c45084ea 2005-11-29 pbug * MAIN - set up arguments, set up database, set up sockets, call mainloop
179 c45084ea 2005-11-29 pbug *
180 c45084ea 2005-11-29 pbug */
181 c45084ea 2005-11-29 pbug
182 c45084ea 2005-11-29 pbug int
183 c45084ea 2005-11-29 pbug main(int argc, char *argv[])
184 c45084ea 2005-11-29 pbug {
185 c25e4088 2011-06-28 pbug static int udp[DEFAULT_SOCKET];
186 c25e4088 2011-06-28 pbug static int tcp[DEFAULT_SOCKET];
187 f98bb34d 2011-09-19 pbug static int afd[DEFAULT_SOCKET];
188 f98bb34d 2011-09-19 pbug static int uafd[DEFAULT_SOCKET];
189 b6dc64dc 2010-04-15 pbug int raw[2];
190 f98bb34d 2011-09-19 pbug int lfd = -1;
191 f98bb34d 2011-09-19 pbug int fd, n;
192 c45084ea 2005-11-29 pbug
193 c45084ea 2005-11-29 pbug int ch, i, j;
194 c45084ea 2005-11-29 pbug int gai_error;
195 f98bb34d 2011-09-19 pbug int salen, ret;
196 c45084ea 2005-11-29 pbug int found = 0;
197 9525851c 2009-08-31 pbug int on = 1;
198 b6dc64dc 2010-04-15 pbug int sp[2];
199 b6dc64dc 2010-04-15 pbug
200 b6dc64dc 2010-04-15 pbug pid_t pid;
201 c45084ea 2005-11-29 pbug
202 eea748bc 2008-07-10 pbug static char *ident[DEFAULT_SOCKET];
203 eea748bc 2008-07-10 pbug char *conffile = CONFFILE;
204 eea748bc 2008-07-10 pbug char buf[512];
205 99ed55ed 2011-04-12 pbug char **av = NULL;
206 c45084ea 2005-11-29 pbug
207 c45084ea 2005-11-29 pbug struct passwd *pw;
208 c45084ea 2005-11-29 pbug struct addrinfo hints, *res0, *res;
209 c45084ea 2005-11-29 pbug struct ifaddrs *ifap, *pifap;
210 c45084ea 2005-11-29 pbug struct sockaddr_in *sin;
211 c45084ea 2005-11-29 pbug struct sockaddr_in6 *sin6;
212 c25e4088 2011-06-28 pbug struct cfg *cfg;
213 c45084ea 2005-11-29 pbug
214 f98bb34d 2011-09-19 pbug static DB_ENV *dbenv;
215 c45084ea 2005-11-29 pbug static DB *db;
216 f98bb34d 2011-09-19 pbug
217 f98bb34d 2011-09-19 pbug key_t key;
218 c45084ea 2005-11-29 pbug
219 c45084ea 2005-11-29 pbug if (geteuid() != 0) {
220 c45084ea 2005-11-29 pbug fprintf(stderr, "must be started as root\n"); /* .. dolt */
221 c45084ea 2005-11-29 pbug exit(1);
222 c45084ea 2005-11-29 pbug }
223 c45084ea 2005-11-29 pbug
224 99ed55ed 2011-04-12 pbug av = argv;
225 99ed55ed 2011-04-12 pbug
226 dd869383 2013-02-16 pjp while ((ch = getopt(argc, argv, "b:c:df:i:ln:p:rv")) != -1) {
227 c45084ea 2005-11-29 pbug switch (ch) {
228 c45084ea 2005-11-29 pbug case 'b':
229 c45084ea 2005-11-29 pbug bflag = 1;
230 c45084ea 2005-11-29 pbug if (bcount > 253) {
231 0f80aa4d 2014-04-15 pjp fprintf(stderr, "too many -b flags\n");
232 c45084ea 2005-11-29 pbug exit(1);
233 c45084ea 2005-11-29 pbug }
234 c45084ea 2005-11-29 pbug bind_list[bcount++] = optarg;
235 f98bb34d 2011-09-19 pbug break;
236 f98bb34d 2011-09-19 pbug case 'c':
237 27110135 2014-11-07 pjp #if !defined __OpenBSD__
238 f98bb34d 2011-09-19 pbug cachesize = atoi(optarg);
239 f98bb34d 2011-09-19 pbug #else
240 f98bb34d 2011-09-19 pbug cachesize = strtonum(optarg, 1, 0xffffffff, NULL);
241 f98bb34d 2011-09-19 pbug #endif
242 dd869383 2013-02-16 pjp break;
243 dd869383 2013-02-16 pjp case 'd':
244 dd869383 2013-02-16 pjp debug = 1;
245 c45084ea 2005-11-29 pbug break;
246 87f793be 2009-02-16 pbug case 'f':
247 87f793be 2009-02-16 pbug conffile = optarg;
248 87f793be 2009-02-16 pbug break;
249 c45084ea 2005-11-29 pbug case 'i':
250 c45084ea 2005-11-29 pbug iflag = 1;
251 c45084ea 2005-11-29 pbug if (icount > 254) {
252 c45084ea 2005-11-29 pbug fprintf(stderr, "too many -i flags\n");
253 c45084ea 2005-11-29 pbug exit(1);
254 c45084ea 2005-11-29 pbug }
255 c45084ea 2005-11-29 pbug interface_list[icount++] = optarg;
256 c45084ea 2005-11-29 pbug break;
257 87f793be 2009-02-16 pbug case 'l':
258 87f793be 2009-02-16 pbug lflag = 1;
259 c45084ea 2005-11-29 pbug break;
260 f98bb34d 2011-09-19 pbug case 'n':
261 f98bb34d 2011-09-19 pbug nflag = atoi(optarg);
262 f98bb34d 2011-09-19 pbug break;
263 c45084ea 2005-11-29 pbug case 'p':
264 c45084ea 2005-11-29 pbug port = atoi(optarg) & 0xffff;
265 eea748bc 2008-07-10 pbug break;
266 b6dc64dc 2010-04-15 pbug case 'r':
267 b6dc64dc 2010-04-15 pbug rflag = 1;
268 b6dc64dc 2010-04-15 pbug break;
269 dd869383 2013-02-16 pjp case 'v':
270 dd869383 2013-02-16 pjp verbose++;
271 dd869383 2013-02-16 pjp break;
272 c45084ea 2005-11-29 pbug default:
273 dd869383 2013-02-16 pjp fprintf(stderr, "usage: wildcarddnsd [-i interface] [-b bindaddress] [-f configfile] [-p portnumber] [-drv]\n");
274 c45084ea 2005-11-29 pbug exit (1);
275 c45084ea 2005-11-29 pbug }
276 c45084ea 2005-11-29 pbug }
277 c45084ea 2005-11-29 pbug
278 c45084ea 2005-11-29 pbug if (bflag && iflag) {
279 c45084ea 2005-11-29 pbug fprintf(stderr, "you may specify -i or -b but not both\n");
280 c45084ea 2005-11-29 pbug exit(1);
281 c45084ea 2005-11-29 pbug }
282 9c4921a5 2008-04-10 pbug
283 70879a99 2009-03-01 pbug /*
284 70879a99 2009-03-01 pbug * calling daemon before a sleuth of configurations ala rwhod.c
285 70879a99 2009-03-01 pbug */
286 dd869383 2013-02-16 pjp
287 dd869383 2013-02-16 pjp if (! debug)
288 dd869383 2013-02-16 pjp daemon(0,0);
289 70879a99 2009-03-01 pbug
290 c25e4088 2011-06-28 pbug
291 4583838f 2009-02-28 pbug openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
292 dd869383 2013-02-16 pjp dolog(LOG_INFO, "starting up\n");
293 06aa4e57 2011-04-12 pbug
294 c25e4088 2011-06-28 pbug /* cfg struct */
295 c25e4088 2011-06-28 pbug cfg = calloc(1, sizeof(struct cfg));
296 c25e4088 2011-06-28 pbug if (cfg == NULL) {
297 dd869383 2013-02-16 pjp dolog(LOG_ERR, "calloc: %s\n", strerror(errno));
298 c25e4088 2011-06-28 pbug exit(1);
299 c25e4088 2011-06-28 pbug }
300 c25e4088 2011-06-28 pbug
301 06aa4e57 2011-04-12 pbug /*
302 06aa4e57 2011-04-12 pbug * make a shared memory segment for signaling kills between
303 06aa4e57 2011-04-12 pbug * processes...
304 06aa4e57 2011-04-12 pbug */
305 06aa4e57 2011-04-12 pbug
306 06aa4e57 2011-04-12 pbug
307 06aa4e57 2011-04-12 pbug ptr = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED |\
308 06aa4e57 2011-04-12 pbug MAP_ANON, -1, 0);
309 06aa4e57 2011-04-12 pbug
310 06aa4e57 2011-04-12 pbug if (ptr == MAP_FAILED) {
311 dd869383 2013-02-16 pjp dolog(LOG_ERR, "failed to setup mmap segment, exit\n");
312 06aa4e57 2011-04-12 pbug exit(1);
313 06aa4e57 2011-04-12 pbug }
314 0b505b33 2009-02-26 pbug
315 06aa4e57 2011-04-12 pbug *ptr = 0;
316 06aa4e57 2011-04-12 pbug
317 f98bb34d 2011-09-19 pbug if ((ret = db_env_create(&dbenv, 0)) != 0) {
318 dd869383 2013-02-16 pjp dolog(LOG_INFO, "db_env_create: %s\n", db_strerror(ret));
319 f98bb34d 2011-09-19 pbug slave_shutdown();
320 0aefb92a 2011-04-12 pbug exit(1);
321 0aefb92a 2011-04-12 pbug }
322 0aefb92a 2011-04-12 pbug
323 f98bb34d 2011-09-19 pbug key = ftok("/usr/local/sbin/wildcarddnsd", 1);
324 b3d7d68a 2012-06-21 pbug if (key == (key_t)-1) {
325 dd869383 2013-02-16 pjp dolog(LOG_INFO, "ftok failed, does /usr/local/sbin/wildcarddnsd exist?\n");
326 b3d7d68a 2012-06-21 pbug slave_shutdown();
327 b3d7d68a 2012-06-21 pbug exit(1);
328 b3d7d68a 2012-06-21 pbug }
329 b3d7d68a 2012-06-21 pbug
330 9c4921a5 2008-04-10 pbug
331 f98bb34d 2011-09-19 pbug if ((ret = dbenv->set_shm_key(dbenv, key)) != 0) {
332 dd869383 2013-02-16 pjp dolog(LOG_INFO, "dbenv->set_shm_key failed\n");
333 f98bb34d 2011-09-19 pbug slave_shutdown();
334 f98bb34d 2011-09-19 pbug exit(1);
335 f98bb34d 2011-09-19 pbug }
336 f98bb34d 2011-09-19 pbug
337 f98bb34d 2011-09-19 pbug /* set cache size , if requested */
338 f98bb34d 2011-09-19 pbug
339 f98bb34d 2011-09-19 pbug if (cachesize) {
340 f98bb34d 2011-09-19 pbug if ((ret = dbenv->set_cachesize(dbenv, 0, cachesize, 0)) != 0) {
341 dd869383 2013-02-16 pjp dolog(LOG_INFO, "dbenv->set_cachesize: %s\n",
342 f98bb34d 2011-09-19 pbug db_strerror(ret));
343 f98bb34d 2011-09-19 pbug slave_shutdown();
344 f98bb34d 2011-09-19 pbug exit(1);
345 f98bb34d 2011-09-19 pbug }
346 f98bb34d 2011-09-19 pbug }
347 f98bb34d 2011-09-19 pbug
348 f98bb34d 2011-09-19 pbug (void)mkdir(MYDB_PATH, 0700);
349 cd18f393 2014-05-10 pjp snprintf(mydatabase, sizeof(mydatabase), "%s/%ld",
350 cd18f393 2014-05-10 pjp MYDB_PATH, (long)getpid());
351 f98bb34d 2011-09-19 pbug
352 cd18f393 2014-05-10 pjp if (mkdir(mydatabase, 0750) < 0) {
353 cd18f393 2014-05-10 pjp if (errno != EEXIST) {
354 cd18f393 2014-05-10 pjp dolog(LOG_ERR, "mkdir: %s\n", strerror(errno));
355 cd18f393 2014-05-10 pjp exit(1);
356 cd18f393 2014-05-10 pjp }
357 cd18f393 2014-05-10 pjp }
358 cd18f393 2014-05-10 pjp
359 cd18f393 2014-05-10 pjp if ((ret = dbenv->open(dbenv, mydatabase, DB_CREATE | \
360 f98bb34d 2011-09-19 pbug DB_INIT_LOCK | DB_INIT_MPOOL | DB_SYSTEM_MEM, \
361 f98bb34d 2011-09-19 pbug S_IRUSR | S_IWUSR)) != 0) {
362 dd869383 2013-02-16 pjp dolog(LOG_INFO, "dbenv->open failed: %s\n", db_strerror(ret));
363 f98bb34d 2011-09-19 pbug slave_shutdown();
364 f98bb34d 2011-09-19 pbug exit(1);
365 f98bb34d 2011-09-19 pbug }
366 f98bb34d 2011-09-19 pbug
367 f98bb34d 2011-09-19 pbug if (db_create((DB **)&db, (DB_ENV *)dbenv, 0) != 0) {
368 dd869383 2013-02-16 pjp dolog(LOG_INFO, "db_create: %s\n", strerror(errno));
369 06aa4e57 2011-04-12 pbug slave_shutdown();
370 9c4921a5 2008-04-10 pbug exit(1);
371 9c4921a5 2008-04-10 pbug }
372 f98bb34d 2011-09-19 pbug
373 f98bb34d 2011-09-19 pbug /*
374 f98bb34d 2011-09-19 pbug * we want to run multiple instances of different versions so we'll
375 f98bb34d 2011-09-19 pbug * make a temporary database...
376 f98bb34d 2011-09-19 pbug */
377 f98bb34d 2011-09-19 pbug
378 cd18f393 2014-05-10 pjp
379 cd18f393 2014-05-10 pjp snprintf(mydatabase, sizeof(mydatabase), "%s/%ld/wdns.db",
380 f98bb34d 2011-09-19 pbug MYDB_PATH, (long)getpid());
381 cd18f393 2014-05-10 pjp
382 cd18f393 2014-05-10 pjp (void)unlink(mydatabase);
383 cd18f393 2014-05-10 pjp
384 f98bb34d 2011-09-19 pbug database = mydatabase;
385 f98bb34d 2011-09-19 pbug
386 cd18f393 2014-05-10 pjp
387 f98bb34d 2011-09-19 pbug fd = open(database, O_WRONLY | O_CREAT, 0600);
388 f98bb34d 2011-09-19 pbug if (fd < 0) {
389 dd869383 2013-02-16 pjp dolog(LOG_INFO, "open: %s\n", strerror(errno));
390 f98bb34d 2011-09-19 pbug }
391 f98bb34d 2011-09-19 pbug close(fd);
392 f98bb34d 2011-09-19 pbug
393 f98bb34d 2011-09-19 pbug if (db->open(db, NULL, database, NULL, DB_BTREE, DB_CREATE, 0600) != 0) {
394 dd869383 2013-02-16 pjp dolog(LOG_INFO, "db->open: %s\n", strerror(errno));
395 72f7b570 2008-04-21 pbug db->close(db, DB_NOSYNC);
396 06aa4e57 2011-04-12 pbug slave_shutdown();
397 72f7b570 2008-04-21 pbug exit(1);
398 72f7b570 2008-04-21 pbug }
399 c45084ea 2005-11-29 pbug
400 f98bb34d 2011-09-19 pbug /* make a master program that holds the pidfile, boss of ... eek */
401 f98bb34d 2011-09-19 pbug
402 f98bb34d 2011-09-19 pbug pid = fork();
403 f98bb34d 2011-09-19 pbug switch (pid) {
404 f98bb34d 2011-09-19 pbug case -1:
405 dd869383 2013-02-16 pjp dolog(LOG_ERR, "fork(): %s\n", strerror(errno));
406 f98bb34d 2011-09-19 pbug exit(1);
407 f98bb34d 2011-09-19 pbug case 0:
408 f98bb34d 2011-09-19 pbug break;
409 f98bb34d 2011-09-19 pbug default:
410 c3e0e8b7 2014-09-29 pjp setup_master(db, dbenv, av);
411 f98bb34d 2011-09-19 pbug /* NOTREACHED */
412 f98bb34d 2011-09-19 pbug exit(1);
413 f98bb34d 2011-09-19 pbug }
414 f98bb34d 2011-09-19 pbug
415 f98bb34d 2011-09-19 pbug /* end of setup_master code */
416 f98bb34d 2011-09-19 pbug
417 959d1769 2010-04-05 pbug init_wildcard();
418 b6dc64dc 2010-04-15 pbug init_recurse();
419 ed595a91 2014-04-13 pjp init_region();
420 c0963faf 2014-05-01 pjp init_filter();
421 6f8d6a57 2014-05-18 pjp init_whitelist();
422 58eafea4 2014-05-17 pjp init_notifyslave();
423 c45084ea 2005-11-29 pbug
424 d45a63b0 2009-02-16 pbug if (parse_file(db, conffile) < 0) {
425 dd869383 2013-02-16 pjp dolog(LOG_INFO, "parsing config file failed\n");
426 06aa4e57 2011-04-12 pbug slave_shutdown();
427 d45a63b0 2009-02-16 pbug exit(1);
428 4667e76d 2014-05-05 pjp }
429 4667e76d 2014-05-05 pjp
430 4667e76d 2014-05-05 pjp /* ratelimiting setup */
431 4667e76d 2014-05-05 pjp if (ratelimit) {
432 4667e76d 2014-05-05 pjp ratelimit_backlog = ratelimit_packets_per_second * 2;
433 4667e76d 2014-05-05 pjp rptr = rrlimit_setup(ratelimit_backlog);
434 4667e76d 2014-05-05 pjp if (rptr == NULL) {
435 4667e76d 2014-05-05 pjp dolog(LOG_INFO, "ratelimiting error\n");
436 4667e76d 2014-05-05 pjp slave_shutdown();
437 4667e76d 2014-05-05 pjp exit(1);
438 4667e76d 2014-05-05 pjp }
439 d45a63b0 2009-02-16 pbug }
440 4667e76d 2014-05-05 pjp
441 c45084ea 2005-11-29 pbug
442 c45084ea 2005-11-29 pbug pw = getpwnam(DEFAULT_PRIVILEGE);
443 c45084ea 2005-11-29 pbug if (pw == NULL) {
444 dd869383 2013-02-16 pjp dolog(LOG_INFO, "getpwnam: %s\n", strerror(errno));
445 06aa4e57 2011-04-12 pbug slave_shutdown();
446 c45084ea 2005-11-29 pbug exit(1);
447 c45084ea 2005-11-29 pbug }
448 c45084ea 2005-11-29 pbug
449 c45084ea 2005-11-29 pbug if (bcount > DEFAULT_SOCKET) {
450 dd869383 2013-02-16 pjp dolog(LOG_INFO, "not enough sockets available\n");
451 06aa4e57 2011-04-12 pbug slave_shutdown();
452 c45084ea 2005-11-29 pbug exit(1);
453 c45084ea 2005-11-29 pbug }
454 c45084ea 2005-11-29 pbug
455 c45084ea 2005-11-29 pbug if (bflag) {
456 c45084ea 2005-11-29 pbug for (i = 0; i < bcount; i++) {
457 c45084ea 2005-11-29 pbug memset(&hints, 0, sizeof(hints));
458 c45084ea 2005-11-29 pbug
459 c45084ea 2005-11-29 pbug if (strchr(bind_list[i], ':') != NULL) {
460 c45084ea 2005-11-29 pbug hints.ai_family = AF_INET6;
461 c45084ea 2005-11-29 pbug } else {
462 c45084ea 2005-11-29 pbug hints.ai_family = AF_INET;
463 c45084ea 2005-11-29 pbug }
464 c45084ea 2005-11-29 pbug
465 c45084ea 2005-11-29 pbug hints.ai_socktype = SOCK_DGRAM;
466 c45084ea 2005-11-29 pbug hints.ai_protocol = IPPROTO_UDP;
467 c45084ea 2005-11-29 pbug hints.ai_flags = AI_NUMERICHOST;
468 c45084ea 2005-11-29 pbug
469 c45084ea 2005-11-29 pbug snprintf(buf, sizeof(buf) - 1, "%u", port);
470 c45084ea 2005-11-29 pbug
471 c45084ea 2005-11-29 pbug if ((gai_error = getaddrinfo(bind_list[i], buf, &hints, &res0)) != 0) {
472 dd869383 2013-02-16 pjp dolog(LOG_INFO, "getaddrinfo: %s\n", gai_strerror(gai_error));
473 06aa4e57 2011-04-12 pbug slave_shutdown();
474 c45084ea 2005-11-29 pbug exit (1);
475 c45084ea 2005-11-29 pbug }
476 c45084ea 2005-11-29 pbug
477 c45084ea 2005-11-29 pbug res = res0;
478 c45084ea 2005-11-29 pbug
479 c45084ea 2005-11-29 pbug if ((udp[i] = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
480 dd869383 2013-02-16 pjp dolog(LOG_INFO, "socket: %s\n", strerror(errno));
481 06aa4e57 2011-04-12 pbug slave_shutdown();
482 c45084ea 2005-11-29 pbug exit(1);
483 c45084ea 2005-11-29 pbug }
484 c45084ea 2005-11-29 pbug
485 c45084ea 2005-11-29 pbug if (bind(udp[i], res->ai_addr, res->ai_addrlen) < 0) {
486 dd869383 2013-02-16 pjp dolog(LOG_INFO, "bind: %s\n", strerror(errno));
487 06aa4e57 2011-04-12 pbug slave_shutdown();
488 c45084ea 2005-11-29 pbug exit(1);
489 c45084ea 2005-11-29 pbug }
490 c45084ea 2005-11-29 pbug
491 9525851c 2009-08-31 pbug if (res->ai_family == AF_INET) {
492 1e8325ae 2010-03-28 pbug #ifdef __NetBSD__
493 1e8325ae 2010-03-28 pbug if (setsockopt(udp[i], IPPROTO_IP, IP_TTL,
494 1e8325ae 2010-03-28 pbug &on, sizeof(on)) < 0) {
495 1e8325ae 2010-03-28 pbug #else
496 9525851c 2009-08-31 pbug if (setsockopt(udp[i], IPPROTO_IP, IP_RECVTTL,
497 9525851c 2009-08-31 pbug &on, sizeof(on)) < 0) {
498 1e8325ae 2010-03-28 pbug #endif
499 dd869383 2013-02-16 pjp dolog(LOG_INFO, "setsockopt: %s\n", strerror(errno));
500 562d28f3 2012-06-11 pbug }
501 562d28f3 2012-06-11 pbug } else if (res->ai_family == AF_INET6) {
502 562d28f3 2012-06-11 pbug /* RFC 3542 page 30 */
503 562d28f3 2012-06-11 pbug on = 1;
504 562d28f3 2012-06-11 pbug if (setsockopt(udp[i], IPPROTO_IPV6,
505 562d28f3 2012-06-11 pbug IPV6_RECVHOPLIMIT, &on, sizeof(on)) < 0) {
506 dd869383 2013-02-16 pjp dolog(LOG_INFO, "setsockopt: %s\n", strerror(errno));
507 9525851c 2009-08-31 pbug }
508 9525851c 2009-08-31 pbug }
509 562d28f3 2012-06-11 pbug
510 c45084ea 2005-11-29 pbug ident[i] = bind_list[i];
511 f1e3cfca 2010-03-12 pbug
512 f1e3cfca 2010-03-12 pbug /* tcp below */
513 f1e3cfca 2010-03-12 pbug hints.ai_socktype = SOCK_STREAM;
514 f1e3cfca 2010-03-12 pbug hints.ai_protocol = IPPROTO_TCP;
515 f1e3cfca 2010-03-12 pbug hints.ai_flags = AI_NUMERICHOST;
516 f1e3cfca 2010-03-12 pbug
517 f1e3cfca 2010-03-12 pbug snprintf(buf, sizeof(buf) - 1, "%u", port);
518 f1e3cfca 2010-03-12 pbug
519 f1e3cfca 2010-03-12 pbug if ((gai_error = getaddrinfo(bind_list[i], buf, &hints, &res0)) != 0) {
520 dd869383 2013-02-16 pjp dolog(LOG_INFO, "getaddrinfo: %s\n", gai_strerror(gai_error));
521 06aa4e57 2011-04-12 pbug slave_shutdown();
522 f1e3cfca 2010-03-12 pbug exit (1);
523 f1e3cfca 2010-03-12 pbug }
524 f1e3cfca 2010-03-12 pbug
525 f1e3cfca 2010-03-12 pbug res = res0;
526 c45084ea 2005-11-29 pbug
527 f1e3cfca 2010-03-12 pbug if ((tcp[i] = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
528 dd869383 2013-02-16 pjp dolog(LOG_INFO, "tcp socket: %s\n", strerror(errno));
529 06aa4e57 2011-04-12 pbug slave_shutdown();
530 f1e3cfca 2010-03-12 pbug exit(1);
531 f1e3cfca 2010-03-12 pbug }
532 f1e3cfca 2010-03-12 pbug if (setsockopt(tcp[i], SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
533 dd869383 2013-02-16 pjp dolog(LOG_INFO, "setsockopt: %s\n", strerror(errno));
534 06aa4e57 2011-04-12 pbug slave_shutdown();
535 f1e3cfca 2010-03-12 pbug exit(1);
536 f1e3cfca 2010-03-12 pbug }
537 f1e3cfca 2010-03-12 pbug if (bind(tcp[i], res->ai_addr, res->ai_addrlen) < 0) {
538 dd869383 2013-02-16 pjp dolog(LOG_INFO, "tcp bind: %s\n", strerror(errno));
539 06aa4e57 2011-04-12 pbug slave_shutdown();
540 f1e3cfca 2010-03-12 pbug exit(1);
541 f1e3cfca 2010-03-12 pbug }
542 f98bb34d 2011-09-19 pbug
543 f98bb34d 2011-09-19 pbug if (axfrport) {
544 f98bb34d 2011-09-19 pbug /* axfr port below */
545 f98bb34d 2011-09-19 pbug hints.ai_socktype = SOCK_STREAM;
546 f98bb34d 2011-09-19 pbug hints.ai_protocol = IPPROTO_TCP;
547 f98bb34d 2011-09-19 pbug hints.ai_flags = AI_NUMERICHOST;
548 f98bb34d 2011-09-19 pbug
549 f98bb34d 2011-09-19 pbug snprintf(buf, sizeof(buf) - 1, "%u", axfrport);
550 f98bb34d 2011-09-19 pbug
551 f98bb34d 2011-09-19 pbug if ((gai_error = getaddrinfo(bind_list[i], buf, &hints, &res0)) != 0) {
552 dd869383 2013-02-16 pjp dolog(LOG_INFO, "getaddrinfo: %s\n", gai_strerror(gai_error));
553 f98bb34d 2011-09-19 pbug slave_shutdown();
554 f98bb34d 2011-09-19 pbug exit (1);
555 f98bb34d 2011-09-19 pbug }
556 f98bb34d 2011-09-19 pbug
557 f98bb34d 2011-09-19 pbug res = res0;
558 f98bb34d 2011-09-19 pbug
559 f98bb34d 2011-09-19 pbug if ((afd[i] = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
560 dd869383 2013-02-16 pjp dolog(LOG_INFO, "tcp socket: %s\n", strerror(errno));
561 f98bb34d 2011-09-19 pbug slave_shutdown();
562 f98bb34d 2011-09-19 pbug exit(1);
563 f98bb34d 2011-09-19 pbug }
564 f98bb34d 2011-09-19 pbug if (setsockopt(afd[i], SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
565 dd869383 2013-02-16 pjp dolog(LOG_INFO, "setsockopt: %s\n", strerror(errno));
566 f98bb34d 2011-09-19 pbug slave_shutdown();
567 f98bb34d 2011-09-19 pbug exit(1);
568 f98bb34d 2011-09-19 pbug }
569 f98bb34d 2011-09-19 pbug if (bind(afd[i], res->ai_addr, res->ai_addrlen) < 0) {
570 dd869383 2013-02-16 pjp dolog(LOG_INFO, "tcp bind: %s\n", strerror(errno));
571 f98bb34d 2011-09-19 pbug slave_shutdown();
572 f98bb34d 2011-09-19 pbug exit(1);
573 f98bb34d 2011-09-19 pbug }
574 f98bb34d 2011-09-19 pbug
575 ba97ed10 2014-04-16 pjp if ((uafd[i] = socket(res->ai_family, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
576 dd869383 2013-02-16 pjp dolog(LOG_INFO, "axfr udp socket: %s\n", strerror(errno));
577 f98bb34d 2011-09-19 pbug slave_shutdown();
578 f98bb34d 2011-09-19 pbug exit(1);
579 f98bb34d 2011-09-19 pbug }
580 f98bb34d 2011-09-19 pbug if (bind(uafd[i], res->ai_addr, res->ai_addrlen) < 0) {
581 dd869383 2013-02-16 pjp dolog(LOG_INFO, "axfr udp socket bind: %s\n", strerror(errno));
582 f98bb34d 2011-09-19 pbug slave_shutdown();
583 f98bb34d 2011-09-19 pbug exit(1);
584 f98bb34d 2011-09-19 pbug }
585 f98bb34d 2011-09-19 pbug } /* axfrport */
586 f1e3cfca 2010-03-12 pbug
587 f1e3cfca 2010-03-12 pbug } /* for .. bcount */
588 f1e3cfca 2010-03-12 pbug
589 c45084ea 2005-11-29 pbug } else {
590 c45084ea 2005-11-29 pbug if (getifaddrs(&ifap) < 0) {
591 dd869383 2013-02-16 pjp dolog(LOG_INFO, "getifaddrs\n");
592 06aa4e57 2011-04-12 pbug slave_shutdown();
593 c45084ea 2005-11-29 pbug exit(1);
594 c45084ea 2005-11-29 pbug }
595 c45084ea 2005-11-29 pbug
596 c45084ea 2005-11-29 pbug for (pifap = ifap, i = 0; i < DEFAULT_SOCKET && pifap; pifap = pifap->ifa_next, i++) {
597 c45084ea 2005-11-29 pbug
598 c45084ea 2005-11-29 pbug found = 0;
599 c45084ea 2005-11-29 pbug
600 c45084ea 2005-11-29 pbug /* we want only one interface not the rest */
601 c45084ea 2005-11-29 pbug if (icount > 0) {
602 c45084ea 2005-11-29 pbug for (j = 0; j < icount; j++) {
603 c45084ea 2005-11-29 pbug if (strcmp(pifap->ifa_name, interface_list[j]) == 0) {
604 c45084ea 2005-11-29 pbug found = 1;
605 c45084ea 2005-11-29 pbug }
606 c45084ea 2005-11-29 pbug }
607 c45084ea 2005-11-29 pbug
608 c45084ea 2005-11-29 pbug if (! found) {
609 c45084ea 2005-11-29 pbug i--;
610 c45084ea 2005-11-29 pbug continue;
611 c45084ea 2005-11-29 pbug }
612 c45084ea 2005-11-29 pbug
613 c45084ea 2005-11-29 pbug }
614 c45084ea 2005-11-29 pbug if ((pifap->ifa_flags & IFF_UP) != IFF_UP) {
615 dd869383 2013-02-16 pjp dolog(LOG_INFO, "skipping interface %s\n", pifap->ifa_name);
616 c45084ea 2005-11-29 pbug i--;
617 c45084ea 2005-11-29 pbug continue;
618 c45084ea 2005-11-29 pbug }
619 c45084ea 2005-11-29 pbug
620 c45084ea 2005-11-29 pbug if (pifap->ifa_addr->sa_family == AF_INET) {
621 c45084ea 2005-11-29 pbug sin = (struct sockaddr_in *)pifap->ifa_addr;
622 c45084ea 2005-11-29 pbug sin->sin_port = htons(port);
623 c45084ea 2005-11-29 pbug salen = sizeof(struct sockaddr_in);
624 c45084ea 2005-11-29 pbug /* no address bound to this interface */
625 c45084ea 2005-11-29 pbug if (sin->sin_addr.s_addr == INADDR_ANY) {
626 c45084ea 2005-11-29 pbug i--;
627 c45084ea 2005-11-29 pbug continue;
628 c45084ea 2005-11-29 pbug }
629 c45084ea 2005-11-29 pbug } else if (pifap->ifa_addr->sa_family == AF_INET6) {
630 c45084ea 2005-11-29 pbug sin6 = (struct sockaddr_in6 *)pifap->ifa_addr;
631 ed247332 2010-03-09 pbug sin6->sin6_port = htons(port);
632 c45084ea 2005-11-29 pbug /* no address bound to this interface */
633 c45084ea 2005-11-29 pbug salen = sizeof(struct sockaddr_in6);
634 b6dc64dc 2010-04-15 pbug
635 c45084ea 2005-11-29 pbug } else {
636 dd869383 2013-02-16 pjp dolog(LOG_DEBUG, "unknown address family %d\n", pifap->ifa_addr->sa_family);
637 c45084ea 2005-11-29 pbug i--;
638 c45084ea 2005-11-29 pbug continue;
639 c45084ea 2005-11-29 pbug }
640 c45084ea 2005-11-29 pbug
641 c45084ea 2005-11-29 pbug
642 c45084ea 2005-11-29 pbug if ((udp[i] = socket(pifap->ifa_addr->sa_family, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
643 dd869383 2013-02-16 pjp dolog(LOG_INFO, "socket: %s\n", strerror(errno));
644 06aa4e57 2011-04-12 pbug slave_shutdown();
645 c45084ea 2005-11-29 pbug exit(1);
646 c45084ea 2005-11-29 pbug }
647 c45084ea 2005-11-29 pbug
648 c45084ea 2005-11-29 pbug if (bind(udp[i], (struct sockaddr *)pifap->ifa_addr, salen) < 0) {
649 dd869383 2013-02-16 pjp dolog(LOG_INFO, "bind: %s\n", strerror(errno));
650 06aa4e57 2011-04-12 pbug slave_shutdown();
651 c45084ea 2005-11-29 pbug exit(1);
652 9525851c 2009-08-31 pbug }
653 9525851c 2009-08-31 pbug
654 9525851c 2009-08-31 pbug if (pifap->ifa_addr->sa_family == AF_INET) {
655 1e8325ae 2010-03-28 pbug #ifdef __NetBSD__
656 1e8325ae 2010-03-28 pbug if (setsockopt(udp[i], IPPROTO_IP, IP_TTL,
657 1e8325ae 2010-03-28 pbug &on, sizeof(on)) < 0) {
658 1e8325ae 2010-03-28 pbug #else
659 9525851c 2009-08-31 pbug if (setsockopt(udp[i], IPPROTO_IP, IP_RECVTTL,
660 9525851c 2009-08-31 pbug &on, sizeof(on)) < 0) {
661 1e8325ae 2010-03-28 pbug #endif
662 dd869383 2013-02-16 pjp dolog(LOG_INFO, "setsockopt: %s\n", strerror(errno));
663 9525851c 2009-08-31 pbug }
664 562d28f3 2012-06-11 pbug } else if (pifap->ifa_addr->sa_family == AF_INET6) {
665 562d28f3 2012-06-11 pbug /* RFC 3542 page 30 */
666 562d28f3 2012-06-11 pbug on = 1;
667 562d28f3 2012-06-11 pbug if (setsockopt(udp[i], IPPROTO_IPV6,
668 562d28f3 2012-06-11 pbug IPV6_RECVHOPLIMIT, &on, sizeof(on)) < 0) {
669 dd869383 2013-02-16 pjp dolog(LOG_INFO, "setsockopt: %s\n", strerror(errno));
670 562d28f3 2012-06-11 pbug }
671 c45084ea 2005-11-29 pbug }
672 c45084ea 2005-11-29 pbug
673 562d28f3 2012-06-11 pbug
674 c45084ea 2005-11-29 pbug ident[i] = pifap->ifa_name;
675 c45084ea 2005-11-29 pbug
676 f1e3cfca 2010-03-12 pbug if ((tcp[i] = socket(pifap->ifa_addr->sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0) {
677 dd869383 2013-02-16 pjp dolog(LOG_INFO, "tcp socket: %s\n", strerror(errno));
678 06aa4e57 2011-04-12 pbug slave_shutdown();
679 f1e3cfca 2010-03-12 pbug exit(1);
680 f1e3cfca 2010-03-12 pbug }
681 f1e3cfca 2010-03-12 pbug if (setsockopt(tcp[i], SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
682 dd869383 2013-02-16 pjp dolog(LOG_INFO, "setsockopt: %s\n", strerror(errno));
683 06aa4e57 2011-04-12 pbug slave_shutdown();
684 f1e3cfca 2010-03-12 pbug exit(1);
685 f1e3cfca 2010-03-12 pbug }
686 f1e3cfca 2010-03-12 pbug
687 f1e3cfca 2010-03-12 pbug if (bind(tcp[i], (struct sockaddr *)pifap->ifa_addr, salen) < 0) {
688 dd869383 2013-02-16 pjp dolog(LOG_INFO, "tcp bind: %s\n", strerror(errno));
689 06aa4e57 2011-04-12 pbug slave_shutdown();
690 f1e3cfca 2010-03-12 pbug exit(1);
691 06aa4e57 2011-04-12 pbug }
692 f1e3cfca 2010-03-12 pbug
693 f98bb34d 2011-09-19 pbug
694 f98bb34d 2011-09-19 pbug /* axfr socket */
695 f98bb34d 2011-09-19 pbug if (axfrport) {
696 f98bb34d 2011-09-19 pbug if ((afd[i] = socket(pifap->ifa_addr->sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0) {
697 dd869383 2013-02-16 pjp dolog(LOG_INFO, "tcp socket: %s\n", strerror(errno));
698 f98bb34d 2011-09-19 pbug slave_shutdown();
699 f98bb34d 2011-09-19 pbug exit(1);
700 f98bb34d 2011-09-19 pbug }
701 f98bb34d 2011-09-19 pbug if (setsockopt(afd[i], SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
702 dd869383 2013-02-16 pjp dolog(LOG_INFO, "setsockopt: %s\n", strerror(errno));
703 f98bb34d 2011-09-19 pbug slave_shutdown();
704 f98bb34d 2011-09-19 pbug exit(1);
705 f98bb34d 2011-09-19 pbug }
706 f98bb34d 2011-09-19 pbug
707 f98bb34d 2011-09-19 pbug ((struct sockaddr_in *)pifap->ifa_addr)->sin_port = htons(axfrport);
708 f98bb34d 2011-09-19 pbug
709 f98bb34d 2011-09-19 pbug if (bind(afd[i], (struct sockaddr *)pifap->ifa_addr, salen) < 0) {
710 dd869383 2013-02-16 pjp dolog(LOG_INFO, "tcp bind: %s\n", strerror(errno));
711 f98bb34d 2011-09-19 pbug slave_shutdown();
712 f98bb34d 2011-09-19 pbug exit(1);
713 f98bb34d 2011-09-19 pbug }
714 f98bb34d 2011-09-19 pbug if ((uafd[i] = socket(pifap->ifa_addr->sa_family, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
715 dd869383 2013-02-16 pjp dolog(LOG_INFO, "axfr udp socket: %s\n", strerror(errno));
716 f98bb34d 2011-09-19 pbug slave_shutdown();
717 f98bb34d 2011-09-19 pbug exit(1);
718 f98bb34d 2011-09-19 pbug }
719 f98bb34d 2011-09-19 pbug if (bind(uafd[i], (struct sockaddr *)pifap->ifa_addr, salen) < 0) {
720 dd869383 2013-02-16 pjp dolog(LOG_INFO, "udp axfr bind: %s\n", strerror(errno));
721 f98bb34d 2011-09-19 pbug slave_shutdown();
722 f98bb34d 2011-09-19 pbug exit(1);
723 f98bb34d 2011-09-19 pbug }
724 f98bb34d 2011-09-19 pbug } /* axfrport */
725 f98bb34d 2011-09-19 pbug
726 b6dc64dc 2010-04-15 pbug } /* AF_INET */
727 c45084ea 2005-11-29 pbug
728 c45084ea 2005-11-29 pbug if (i >= DEFAULT_SOCKET) {
729 dd869383 2013-02-16 pjp dolog(LOG_INFO, "not enough sockets available\n");
730 06aa4e57 2011-04-12 pbug slave_shutdown();
731 c45084ea 2005-11-29 pbug exit(1);
732 c45084ea 2005-11-29 pbug }
733 c25e4088 2011-06-28 pbug } /* if bflag? */
734 c45084ea 2005-11-29 pbug
735 b6dc64dc 2010-04-15 pbug if (rflag == 1) {
736 b6dc64dc 2010-04-15 pbug if ((raw[0] = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
737 dd869383 2013-02-16 pjp dolog(LOG_INFO, "raw socket: %s\n", strerror(errno));
738 06aa4e57 2011-04-12 pbug slave_shutdown();
739 b6dc64dc 2010-04-15 pbug exit(1);
740 b6dc64dc 2010-04-15 pbug }
741 b6dc64dc 2010-04-15 pbug
742 b6dc64dc 2010-04-15 pbug if (setsockopt(raw[0], IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0) {
743 dd869383 2013-02-16 pjp dolog(LOG_INFO, "raw setsockopt: %s\n", strerror(errno));
744 06aa4e57 2011-04-12 pbug slave_shutdown();
745 b6dc64dc 2010-04-15 pbug exit(1);
746 b6dc64dc 2010-04-15 pbug }
747 b6dc64dc 2010-04-15 pbug
748 518aa971 2010-09-30 pbug if ((raw[1] = socket(AF_INET6, SOCK_RAW, IPPROTO_UDP)) < 0) {
749 dd869383 2013-02-16 pjp dolog(LOG_INFO, "raw socket[1]: %s\n", strerror(errno));
750 06aa4e57 2011-04-12 pbug slave_shutdown();
751 b6dc64dc 2010-04-15 pbug exit(1);
752 b6dc64dc 2010-04-15 pbug }
753 b6dc64dc 2010-04-15 pbug
754 b6dc64dc 2010-04-15 pbug } /* rflag */
755 c25e4088 2011-06-28 pbug
756 c25e4088 2011-06-28 pbug
757 c25e4088 2011-06-28 pbug /* if we are binding a log socket do it now */
758 c25e4088 2011-06-28 pbug if (logging.bind == 1 || logging.active == 1) {
759 c25e4088 2011-06-28 pbug switch (logging.loghost2.ss_family) {
760 c25e4088 2011-06-28 pbug case AF_INET:
761 c25e4088 2011-06-28 pbug lfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
762 c25e4088 2011-06-28 pbug if (lfd < 0) {
763 dd869383 2013-02-16 pjp dolog(LOG_INFO, "logging socket: %s\n", strerror(errno));
764 c25e4088 2011-06-28 pbug slave_shutdown();
765 c25e4088 2011-06-28 pbug exit(1);
766 c25e4088 2011-06-28 pbug }
767 c25e4088 2011-06-28 pbug sin = (struct sockaddr_in *)&logging.loghost2;
768 c25e4088 2011-06-28 pbug sin->sin_port = htons(logging.logport2);
769 c25e4088 2011-06-28 pbug break;
770 c25e4088 2011-06-28 pbug case AF_INET6:
771 c25e4088 2011-06-28 pbug lfd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
772 c25e4088 2011-06-28 pbug if (lfd < 0) {
773 dd869383 2013-02-16 pjp dolog(LOG_INFO, "logging socket: %s\n", strerror(errno));
774 c25e4088 2011-06-28 pbug slave_shutdown();
775 c25e4088 2011-06-28 pbug exit(1);
776 c25e4088 2011-06-28 pbug }
777 c25e4088 2011-06-28 pbug sin6 = (struct sockaddr_in6 *)&logging.loghost2;
778 c25e4088 2011-06-28 pbug sin6->sin6_port = htons(logging.logport2);
779 c25e4088 2011-06-28 pbug break;
780 c25e4088 2011-06-28 pbug }
781 c25e4088 2011-06-28 pbug
782 c25e4088 2011-06-28 pbug if (logging.bind == 1) {
783 c25e4088 2011-06-28 pbug if (bind(lfd, (struct sockaddr *)&logging.loghost2,
784 02c349d2 2011-06-28 pbug ((logging.loghost2.ss_family == AF_INET6) ?
785 02c349d2 2011-06-28 pbug sizeof(struct sockaddr_in6) :
786 02c349d2 2011-06-28 pbug sizeof(struct sockaddr_in))
787 02c349d2 2011-06-28 pbug ) < 0) {
788 dd869383 2013-02-16 pjp dolog(LOG_INFO, "binding log socket: %s\n", strerror(errno));
789 c25e4088 2011-06-28 pbug slave_shutdown();
790 c25e4088 2011-06-28 pbug exit(1);
791 c25e4088 2011-06-28 pbug }
792 c25e4088 2011-06-28 pbug
793 02c349d2 2011-06-28 pbug #ifndef __linux__
794 c25e4088 2011-06-28 pbug if (shutdown(lfd, SHUT_WR) < 0) {
795 dd869383 2013-02-16 pjp dolog(LOG_INFO, "shutdown log socket: %s\n", strerror(errno));
796 c25e4088 2011-06-28 pbug slave_shutdown();
797 c25e4088 2011-06-28 pbug exit(1);
798 c25e4088 2011-06-28 pbug }
799 02c349d2 2011-06-28 pbug #endif
800 c25e4088 2011-06-28 pbug
801 c25e4088 2011-06-28 pbug } else {
802 c25e4088 2011-06-28 pbug if (connect(lfd, (struct sockaddr *)&logging.loghost2,
803 02c349d2 2011-06-28 pbug ((logging.loghost2.ss_family == AF_INET6) ?
804 02c349d2 2011-06-28 pbug sizeof(struct sockaddr_in6) :
805 02c349d2 2011-06-28 pbug sizeof(struct sockaddr_in))) < 0) {
806 dd869383 2013-02-16 pjp dolog(LOG_INFO, "connecting log socket: %s\n", strerror(errno));
807 c25e4088 2011-06-28 pbug slave_shutdown();
808 c25e4088 2011-06-28 pbug exit(1);
809 c25e4088 2011-06-28 pbug }
810 c25e4088 2011-06-28 pbug
811 c25e4088 2011-06-28 pbug if (shutdown(lfd, SHUT_RD) < 0) {
812 dd869383 2013-02-16 pjp dolog(LOG_INFO, "shutdown log socket: %s\n", strerror(errno));
813 c25e4088 2011-06-28 pbug slave_shutdown();
814 c25e4088 2011-06-28 pbug exit(1);
815 c25e4088 2011-06-28 pbug }
816 c25e4088 2011-06-28 pbug
817 c25e4088 2011-06-28 pbug } /* if logging.bind */
818 c25e4088 2011-06-28 pbug
819 c25e4088 2011-06-28 pbug } /* if logging.bind */
820 c25e4088 2011-06-28 pbug
821 c45084ea 2005-11-29 pbug /* chroot to the drop priv user home directory */
822 0b505b33 2009-02-26 pbug if (chroot(pw->pw_dir) < 0) {
823 dd869383 2013-02-16 pjp dolog(LOG_INFO, "chroot: %s\n", strerror(errno));
824 06aa4e57 2011-04-12 pbug slave_shutdown();
825 0b505b33 2009-02-26 pbug exit(1);
826 0b505b33 2009-02-26 pbug }
827 ef5c8735 2009-04-07 pbug
828 ef5c8735 2009-04-07 pbug if (chdir("/") < 0) {
829 dd869383 2013-02-16 pjp dolog(LOG_INFO, "chdir: %s\n", strerror(errno));
830 06aa4e57 2011-04-12 pbug slave_shutdown();
831 ef5c8735 2009-04-07 pbug exit(1);
832 ef5c8735 2009-04-07 pbug }
833 b6b96cfd 2010-03-22 pbug
834 b6b96cfd 2010-03-22 pbug /*
835 06aa4e57 2011-04-12 pbug * add signals
836 b6b96cfd 2010-03-22 pbug */
837 c45084ea 2005-11-29 pbug
838 b6b96cfd 2010-03-22 pbug signal(SIGPIPE, SIG_IGN);
839 b6b96cfd 2010-03-22 pbug
840 06aa4e57 2011-04-12 pbug signal(SIGTERM, slave_signal);
841 06aa4e57 2011-04-12 pbug signal(SIGINT, slave_signal);
842 06aa4e57 2011-04-12 pbug signal(SIGQUIT, slave_signal);
843 06aa4e57 2011-04-12 pbug
844 0b505b33 2009-02-26 pbug /*
845 0b505b33 2009-02-26 pbug * I open the log again after the chroot just in case I can't
846 0b505b33 2009-02-26 pbug * reach the old /dev/log anymore.
847 0b505b33 2009-02-26 pbug */
848 0b505b33 2009-02-26 pbug
849 70879a99 2009-03-01 pbug closelog();
850 0b505b33 2009-02-26 pbug openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
851 0b505b33 2009-02-26 pbug
852 c45084ea 2005-11-29 pbug /* set groups */
853 c45084ea 2005-11-29 pbug
854 c45084ea 2005-11-29 pbug if (setgroups(1, &pw->pw_gid) < 0) {
855 dd869383 2013-02-16 pjp dolog(LOG_INFO, "setgroups: %s\n", strerror(errno));
856 06aa4e57 2011-04-12 pbug slave_shutdown();
857 c45084ea 2005-11-29 pbug exit(1);
858 c45084ea 2005-11-29 pbug }
859 c45084ea 2005-11-29 pbug
860 f98bb34d 2011-09-19 pbug #if defined __OpenBSD__ || defined __FreeBSD__
861 c45084ea 2005-11-29 pbug if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) < 0) {
862 dd869383 2013-02-16 pjp dolog(LOG_INFO, "setresgid: %s\n", strerror(errno));
863 06aa4e57 2011-04-12 pbug slave_shutdown();
864 c45084ea 2005-11-29 pbug exit(1);
865 c45084ea 2005-11-29 pbug }
866 c45084ea 2005-11-29 pbug
867 c45084ea 2005-11-29 pbug if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) < 0) {
868 dd869383 2013-02-16 pjp dolog(LOG_INFO, "setresuid: %s\n", strerror(errno));
869 06aa4e57 2011-04-12 pbug slave_shutdown();
870 9c4921a5 2008-04-10 pbug exit(1);
871 9c4921a5 2008-04-10 pbug }
872 9c4921a5 2008-04-10 pbug
873 9c4921a5 2008-04-10 pbug #else
874 9c4921a5 2008-04-10 pbug if (setgid(pw->pw_gid) < 0) {
875 dd869383 2013-02-16 pjp dolog(LOG_INFO, "setgid: %s\n", strerror(errno));
876 06aa4e57 2011-04-12 pbug slave_shutdown();
877 9c4921a5 2008-04-10 pbug exit(1);
878 9c4921a5 2008-04-10 pbug }
879 9c4921a5 2008-04-10 pbug if (setuid(pw->pw_uid) < 0) {
880 dd869383 2013-02-16 pjp dolog(LOG_INFO, "setuid: %s\n", strerror(errno));
881 06aa4e57 2011-04-12 pbug slave_shutdown();
882 c45084ea 2005-11-29 pbug exit(1);
883 c45084ea 2005-11-29 pbug }
884 f98bb34d 2011-09-19 pbug #endif
885 f98bb34d 2011-09-19 pbug
886 f98bb34d 2011-09-19 pbug /*
887 f98bb34d 2011-09-19 pbug * start our axfr process
888 f98bb34d 2011-09-19 pbug */
889 f98bb34d 2011-09-19 pbug
890 f98bb34d 2011-09-19 pbug if (axfrport) {
891 f98bb34d 2011-09-19 pbug switch (pid = fork()) {
892 f98bb34d 2011-09-19 pbug case 0:
893 f98bb34d 2011-09-19 pbug /* close descriptors that we don't need */
894 f98bb34d 2011-09-19 pbug for (j = 0; j < i; j++) {
895 f98bb34d 2011-09-19 pbug close(tcp[j]);
896 f98bb34d 2011-09-19 pbug close(udp[j]);
897 9e918e62 2011-09-22 pbug close(uafd[j]);
898 f98bb34d 2011-09-19 pbug }
899 c7a6a5d7 2011-09-22 pbug
900 c7a6a5d7 2011-09-22 pbug if (rflag) {
901 c7a6a5d7 2011-09-22 pbug close(raw[0]);
902 c7a6a5d7 2011-09-22 pbug close(raw[1]);
903 c7a6a5d7 2011-09-22 pbug }
904 f98bb34d 2011-09-19 pbug
905 50190f1e 2013-02-16 pjp #if !defined __linux__ && !defined __APPLE__
906 f98bb34d 2011-09-19 pbug setproctitle("AXFR engine on port %d", axfrport);
907 9c4921a5 2008-04-10 pbug #endif
908 f98bb34d 2011-09-19 pbug
909 f98bb34d 2011-09-19 pbug axfrloop(afd, i, ident, db);
910 f98bb34d 2011-09-19 pbug /* NOTREACHED */
911 f98bb34d 2011-09-19 pbug exit(1);
912 f98bb34d 2011-09-19 pbug default:
913 f98bb34d 2011-09-19 pbug /* close afd descriptors, they aren't needed here */
914 f98bb34d 2011-09-19 pbug for (j = 0; j < i; j++) {
915 395f7f0c 2011-09-22 pbug close(afd[j]);
916 f98bb34d 2011-09-19 pbug }
917 f98bb34d 2011-09-19 pbug
918 f98bb34d 2011-09-19 pbug break;
919 f98bb34d 2011-09-19 pbug }
920 f98bb34d 2011-09-19 pbug
921 f98bb34d 2011-09-19 pbug } /* axfrport */
922 f98bb34d 2011-09-19 pbug
923 f98bb34d 2011-09-19 pbug /* what follows is a bit mangled code, we set up nflag + 1 amount of
924 f98bb34d 2011-09-19 pbug * server instances (1 per cpu?) and if we're recursive we also set up
925 f98bb34d 2011-09-19 pbug * the same amount of recursive instances all connected through a
926 f98bb34d 2011-09-19 pbug * socketpair() so that it looks somewhat like this (with 4 instances):
927 f98bb34d 2011-09-19 pbug *
928 f98bb34d 2011-09-19 pbug * replies <--- [] ---- [] recursive end
929 f98bb34d 2011-09-19 pbug * |
930 f98bb34d 2011-09-19 pbug * replies <--- [] ---- []
931 f98bb34d 2011-09-19 pbug * request * ---> |
932 f98bb34d 2011-09-19 pbug * replies <--- [] ---- []
933 f98bb34d 2011-09-19 pbug * |
934 f98bb34d 2011-09-19 pbug * replies <--- [] ---- []
935 f98bb34d 2011-09-19 pbug *
936 f98bb34d 2011-09-19 pbug */
937 f98bb34d 2011-09-19 pbug
938 f98bb34d 2011-09-19 pbug for (n = 0; n < nflag; n++) {
939 f98bb34d 2011-09-19 pbug switch (pid = fork()) {
940 f98bb34d 2011-09-19 pbug case 0:
941 f98bb34d 2011-09-19 pbug if (rflag) {
942 f98bb34d 2011-09-19 pbug /*
943 f98bb34d 2011-09-19 pbug * set up socket pair
944 f98bb34d 2011-09-19 pbug */
945 f98bb34d 2011-09-19 pbug
946 f98bb34d 2011-09-19 pbug if (socketpair(AF_UNIX, SOCK_DGRAM, 0, (int *)&sp) < 0) {
947 dd869383 2013-02-16 pjp dolog(LOG_INFO, "socketpair: %s\n", strerror(errno));
948 f98bb34d 2011-09-19 pbug slave_shutdown();
949 f98bb34d 2011-09-19 pbug exit(1);
950 f98bb34d 2011-09-19 pbug }
951 f98bb34d 2011-09-19 pbug
952 f98bb34d 2011-09-19 pbug switch (pid = fork()) {
953 f98bb34d 2011-09-19 pbug case -1:
954 dd869383 2013-02-16 pjp dolog(LOG_INFO, "fork: %s\n", strerror(errno));
955 f98bb34d 2011-09-19 pbug slave_shutdown();
956 f98bb34d 2011-09-19 pbug exit(1);
957 f98bb34d 2011-09-19 pbug
958 f98bb34d 2011-09-19 pbug case 0:
959 f98bb34d 2011-09-19 pbug for (j = 0; j < i; j++) {
960 f98bb34d 2011-09-19 pbug close(tcp[j]);
961 f98bb34d 2011-09-19 pbug close(udp[j]);
962 f98bb34d 2011-09-19 pbug }
963 f98bb34d 2011-09-19 pbug close (sp[1]);
964 f98bb34d 2011-09-19 pbug
965 f98bb34d 2011-09-19 pbug recurseloop(sp[0], (int *)&raw, db);
966 f98bb34d 2011-09-19 pbug /* NOTREACHED */
967 f98bb34d 2011-09-19 pbug break;
968 f98bb34d 2011-09-19 pbug
969 f98bb34d 2011-09-19 pbug default:
970 f98bb34d 2011-09-19 pbug close(raw[0]);
971 f98bb34d 2011-09-19 pbug close(raw[1]);
972 f98bb34d 2011-09-19 pbug close (sp[0]);
973 f98bb34d 2011-09-19 pbug break;
974 f98bb34d 2011-09-19 pbug } /* switch */
975 f98bb34d 2011-09-19 pbug } /* rflag */
976 f98bb34d 2011-09-19 pbug
977 f98bb34d 2011-09-19 pbug
978 f98bb34d 2011-09-19 pbug cfg->sockcount = i;
979 f98bb34d 2011-09-19 pbug cfg->db = db;
980 f98bb34d 2011-09-19 pbug for (i = 0; i < cfg->sockcount; i++) {
981 f98bb34d 2011-09-19 pbug cfg->udp[i] = udp[i];
982 f98bb34d 2011-09-19 pbug cfg->tcp[i] = tcp[i];
983 f98bb34d 2011-09-19 pbug
984 f98bb34d 2011-09-19 pbug if (axfrport)
985 f98bb34d 2011-09-19 pbug cfg->axfr[i] = uafd[i];
986 f98bb34d 2011-09-19 pbug
987 f98bb34d 2011-09-19 pbug cfg->ident[i] = strdup(ident[i]);
988 f98bb34d 2011-09-19 pbug }
989 f98bb34d 2011-09-19 pbug cfg->recurse = (rflag ? sp[1] : -1);
990 f98bb34d 2011-09-19 pbug cfg->log = lfd;
991 f98bb34d 2011-09-19 pbug
992 f98bb34d 2011-09-19 pbug
993 f98bb34d 2011-09-19 pbug (void)mainloop(cfg);
994 18fa5f8a 2010-03-14 pbug
995 f98bb34d 2011-09-19 pbug /* NOTREACHED */
996 f98bb34d 2011-09-19 pbug default:
997 f98bb34d 2011-09-19 pbug break;
998 f98bb34d 2011-09-19 pbug } /* switch pid= fork */
999 f98bb34d 2011-09-19 pbug } /* for (.. nflag */
1000 f98bb34d 2011-09-19 pbug
1001 b6dc64dc 2010-04-15 pbug if (rflag) {
1002 b6dc64dc 2010-04-15 pbug /*
1003 b6dc64dc 2010-04-15 pbug * set up socket pair
1004 b6dc64dc 2010-04-15 pbug */
1005 b6dc64dc 2010-04-15 pbug
1006 b6dc64dc 2010-04-15 pbug if (socketpair(AF_UNIX, SOCK_DGRAM, 0, (int *)&sp) < 0) {
1007 dd869383 2013-02-16 pjp dolog(LOG_INFO, "socketpair: %s\n", strerror(errno));
1008 06aa4e57 2011-04-12 pbug slave_shutdown();
1009 b6dc64dc 2010-04-15 pbug exit(1);
1010 b6dc64dc 2010-04-15 pbug }
1011 b6dc64dc 2010-04-15 pbug
1012 b6dc64dc 2010-04-15 pbug switch (pid = fork()) {
1013 b6dc64dc 2010-04-15 pbug case -1:
1014 dd869383 2013-02-16 pjp dolog(LOG_INFO, "fork: %s\n", strerror(errno));
1015 06aa4e57 2011-04-12 pbug slave_shutdown();
1016 b6dc64dc 2010-04-15 pbug exit(1);
1017 c45084ea 2005-11-29 pbug
1018 b6dc64dc 2010-04-15 pbug case 0:
1019 b6dc64dc 2010-04-15 pbug for (j = 0; j < i; j++) {
1020 b6dc64dc 2010-04-15 pbug close(tcp[j]);
1021 b6dc64dc 2010-04-15 pbug close(udp[j]);
1022 c7a6a5d7 2011-09-22 pbug close(uafd[j]);
1023 b6dc64dc 2010-04-15 pbug }
1024 b6dc64dc 2010-04-15 pbug close (sp[1]);
1025 c45084ea 2005-11-29 pbug
1026 b6dc64dc 2010-04-15 pbug recurseloop(sp[0], (int *)&raw, db);
1027 b6dc64dc 2010-04-15 pbug /* NOTREACHED */
1028 b6dc64dc 2010-04-15 pbug break;
1029 b6dc64dc 2010-04-15 pbug
1030 b6dc64dc 2010-04-15 pbug default:
1031 b6dc64dc 2010-04-15 pbug close(raw[0]);
1032 b6dc64dc 2010-04-15 pbug close(raw[1]);
1033 b6dc64dc 2010-04-15 pbug close (sp[0]);
1034 b6dc64dc 2010-04-15 pbug break;
1035 b6dc64dc 2010-04-15 pbug } /* switch */
1036 f98bb34d 2011-09-19 pbug
1037 f98bb34d 2011-09-19 pbug } /* rflag */
1038 b6dc64dc 2010-04-15 pbug
1039 c25e4088 2011-06-28 pbug
1040 c25e4088 2011-06-28 pbug cfg->sockcount = i;
1041 c25e4088 2011-06-28 pbug cfg->db = db;
1042 c25e4088 2011-06-28 pbug for (i = 0; i < cfg->sockcount; i++) {
1043 c25e4088 2011-06-28 pbug cfg->udp[i] = udp[i];
1044 c25e4088 2011-06-28 pbug cfg->tcp[i] = tcp[i];
1045 f98bb34d 2011-09-19 pbug
1046 f98bb34d 2011-09-19 pbug if (axfrport)
1047 f98bb34d 2011-09-19 pbug cfg->axfr[i] = uafd[i];
1048 f98bb34d 2011-09-19 pbug
1049 c25e4088 2011-06-28 pbug cfg->ident[i] = strdup(ident[i]);
1050 c25e4088 2011-06-28 pbug }
1051 c25e4088 2011-06-28 pbug cfg->recurse = (rflag ? sp[1] : -1);
1052 c25e4088 2011-06-28 pbug cfg->log = lfd;
1053 b6dc64dc 2010-04-15 pbug
1054 f98bb34d 2011-09-19 pbug
1055 c25e4088 2011-06-28 pbug (void)mainloop(cfg);
1056 b6dc64dc 2010-04-15 pbug
1057 c45084ea 2005-11-29 pbug /* NOTREACHED */
1058 771d101c 2010-05-29 pbug return (0);
1059 c45084ea 2005-11-29 pbug }
1060 c45084ea 2005-11-29 pbug
1061 c45084ea 2005-11-29 pbug
1062 c45084ea 2005-11-29 pbug /*
1063 c45084ea 2005-11-29 pbug * BUILD_QUESTION - fill the question structure with the DNS query.
1064 c45084ea 2005-11-29 pbug */
1065 c45084ea 2005-11-29 pbug
1066 c45084ea 2005-11-29 pbug struct question *
1067 cd18f393 2014-05-10 pjp build_question(char *buf, int len, int additional)
1068 c45084ea 2005-11-29 pbug {
1069 c45084ea 2005-11-29 pbug u_int i;
1070 c45084ea 2005-11-29 pbug u_int namelen = 0;
1071 eea748bc 2008-07-10 pbug u_int16_t *qtype, *qclass;
1072 c45084ea 2005-11-29 pbug int num_label;
1073 eea748bc 2008-07-10 pbug
1074 c45084ea 2005-11-29 pbug char *p, *end_name = NULL;
1075 c45084ea 2005-11-29 pbug
1076 d64396b7 2014-05-09 pjp struct dns_optrr *opt = NULL;
1077 d64396b7 2014-05-09 pjp struct question *q = NULL;
1078 c45084ea 2005-11-29 pbug
1079 c45084ea 2005-11-29 pbug /* find the end of name */
1080 c45084ea 2005-11-29 pbug for (i = sizeof(struct dns_header); i < len; i++) {
1081 9c4921a5 2008-04-10 pbug /* XXX */
1082 9c4921a5 2008-04-10 pbug if (buf[i] == 0) {
1083 c45084ea 2005-11-29 pbug end_name = &buf[i];
1084 c45084ea 2005-11-29 pbug break;
1085 c45084ea 2005-11-29 pbug }
1086 c45084ea 2005-11-29 pbug }
1087 c45084ea 2005-11-29 pbug
1088 8b9017e4 2009-02-17 pbug /*
1089 8b9017e4 2009-02-17 pbug * implies i >= len , because end_name still points to NULL and not
1090 8b9017e4 2009-02-17 pbug * &buf[i]
1091 8b9017e4 2009-02-17 pbug */
1092 8b9017e4 2009-02-17 pbug
1093 c45084ea 2005-11-29 pbug if (end_name == NULL) {
1094 dd869383 2013-02-16 pjp dolog(LOG_INFO, "query name is not null terminated\n");
1095 c45084ea 2005-11-29 pbug return NULL;
1096 c45084ea 2005-11-29 pbug }
1097 c45084ea 2005-11-29 pbug
1098 c45084ea 2005-11-29 pbug /* parse the size of the name */
1099 c45084ea 2005-11-29 pbug for (i = sizeof(struct dns_header), num_label = 0; i < len && &buf[i] < end_name;) {
1100 c45084ea 2005-11-29 pbug u_int labellen;
1101 c45084ea 2005-11-29 pbug
1102 c45084ea 2005-11-29 pbug ++num_label;
1103 c45084ea 2005-11-29 pbug
1104 c45084ea 2005-11-29 pbug labellen = (u_int)buf[i];
1105 c45084ea 2005-11-29 pbug
1106 c45084ea 2005-11-29 pbug /*
1107 c45084ea 2005-11-29 pbug * do some checks on the label, if it's 0 or over 63 it's
1108 c45084ea 2005-11-29 pbug * illegal, also if it reaches beyond the entire name it's
1109 c45084ea 2005-11-29 pbug * also illegal.
1110 c45084ea 2005-11-29 pbug */
1111 c45084ea 2005-11-29 pbug if (labellen == 0) {
1112 dd869383 2013-02-16 pjp dolog(LOG_INFO, "illegal label len (0)\n");
1113 c45084ea 2005-11-29 pbug return NULL;
1114 c45084ea 2005-11-29 pbug }
1115 c45084ea 2005-11-29 pbug if (labellen > DNS_MAXLABEL) {
1116 dd869383 2013-02-16 pjp dolog(LOG_INFO, "illegal label len (> 63)\n");
1117 c45084ea 2005-11-29 pbug return NULL;
1118 c45084ea 2005-11-29 pbug }
1119 c45084ea 2005-11-29 pbug if (labellen > (end_name - &buf[i])) {
1120 dd869383 2013-02-16 pjp dolog(LOG_INFO, "label len extends beyond name\n");
1121 c45084ea 2005-11-29 pbug return NULL;
1122 c45084ea 2005-11-29 pbug }
1123 c45084ea 2005-11-29 pbug
1124 c45084ea 2005-11-29 pbug i += (labellen + 1);
1125 c45084ea 2005-11-29 pbug namelen += labellen;
1126 c45084ea 2005-11-29 pbug }
1127 c45084ea 2005-11-29 pbug
1128 c45084ea 2005-11-29 pbug if (&buf[i] != end_name || i >= len) {
1129 dd869383 2013-02-16 pjp dolog(LOG_INFO, "query name is maliciously malformed\n");
1130 c45084ea 2005-11-29 pbug return NULL;
1131 c45084ea 2005-11-29 pbug }
1132 c45084ea 2005-11-29 pbug
1133 c45084ea 2005-11-29 pbug if (i > DNS_MAXNAME) {
1134 dd869383 2013-02-16 pjp dolog(LOG_INFO, "query name is too long (%u)\n", i);
1135 c45084ea 2005-11-29 pbug return NULL;
1136 c45084ea 2005-11-29 pbug }
1137 c45084ea 2005-11-29 pbug
1138 c45084ea 2005-11-29 pbug
1139 c45084ea 2005-11-29 pbug /* check if there is space for qtype and qclass */
1140 c45084ea 2005-11-29 pbug if (len < ((end_name - &buf[0]) + (2 * sizeof(u_int16_t)))) {
1141 dd869383 2013-02-16 pjp dolog(LOG_INFO, "question rr is truncated\n");
1142 c45084ea 2005-11-29 pbug return NULL;
1143 c45084ea 2005-11-29 pbug }
1144 c45084ea 2005-11-29 pbug
1145 c45084ea 2005-11-29 pbug
1146 c45084ea 2005-11-29 pbug q = (void *)calloc(1, sizeof(struct question));
1147 c45084ea 2005-11-29 pbug if (q == NULL) {
1148 dd869383 2013-02-16 pjp dolog(LOG_INFO, "calloc: %s\n", strerror(errno));
1149 c45084ea 2005-11-29 pbug return NULL;
1150 c45084ea 2005-11-29 pbug }
1151 c45084ea 2005-11-29 pbug q->hdr = (void *)calloc(1, sizeof(struct dns_question_hdr));
1152 c45084ea 2005-11-29 pbug if (q->hdr == NULL) {
1153 dd869383 2013-02-16 pjp dolog(LOG_INFO, "calloc: %s\n", strerror(errno));
1154 c45084ea 2005-11-29 pbug free(q);
1155 c45084ea 2005-11-29 pbug return NULL;
1156 c45084ea 2005-11-29 pbug }
1157 c45084ea 2005-11-29 pbug q->hdr->namelen = (end_name - &buf[sizeof(struct dns_header)]) + 1; /* XXX */
1158 c45084ea 2005-11-29 pbug q->hdr->name = (void *) calloc(1, q->hdr->namelen);
1159 c45084ea 2005-11-29 pbug if (q->hdr->name == NULL) {
1160 dd869383 2013-02-16 pjp dolog(LOG_INFO, "calloc: %s\n", strerror(errno));
1161 c45084ea 2005-11-29 pbug free(q->hdr);
1162 c45084ea 2005-11-29 pbug free(q);
1163 c45084ea 2005-11-29 pbug return NULL;
1164 c45084ea 2005-11-29 pbug }
1165 c45084ea 2005-11-29 pbug q->converted_name = (void *)calloc(1, namelen + num_label + 2);
1166 c45084ea 2005-11-29 pbug if (q->converted_name == NULL) {
1167 dd869383 2013-02-16 pjp dolog(LOG_INFO, "calloc: %s\n", strerror(errno));
1168 c45084ea 2005-11-29 pbug free(q->hdr->name);
1169 c45084ea 2005-11-29 pbug free(q->hdr);
1170 c45084ea 2005-11-29 pbug free(q);
1171 c45084ea 2005-11-29 pbug return NULL;
1172 c45084ea 2005-11-29 pbug }
1173 c45084ea 2005-11-29 pbug
1174 c45084ea 2005-11-29 pbug p = q->converted_name;
1175 c45084ea 2005-11-29 pbug
1176 c45084ea 2005-11-29 pbug /*
1177 c45084ea 2005-11-29 pbug * parse the name again this time filling the labels
1178 c45084ea 2005-11-29 pbug * XXX this is expensive going over the buffer twice
1179 c45084ea 2005-11-29 pbug */
1180 c45084ea 2005-11-29 pbug for (i = sizeof(struct dns_header); i < len && &buf[i] < end_name;) {
1181 c45084ea 2005-11-29 pbug u_int labelend;
1182 c45084ea 2005-11-29 pbug
1183 d64396b7 2014-05-09 pjp
1184 d64396b7 2014-05-09 pjp /* check for compression */
1185 d64396b7 2014-05-09 pjp if ((buf[i] & 0xc0) == 0xc0) {
1186 d64396b7 2014-05-09 pjp dolog(LOG_INFO, "question has compressed name, drop\n");
1187 d64396b7 2014-05-09 pjp free_question(q);
1188 d64396b7 2014-05-09 pjp return NULL; /* XXX should say error */
1189 d64396b7 2014-05-09 pjp }
1190 d64396b7 2014-05-09 pjp
1191 c45084ea 2005-11-29 pbug labelend = (u_int)buf[i] + 1 + i; /* i = offset, plus contents of buf[i], + 1 */
1192 c45084ea 2005-11-29 pbug
1193 8b9017e4 2009-02-17 pbug /*
1194 8b9017e4 2009-02-17 pbug * i is reused here to count every character, this is not
1195 8b9017e4 2009-02-17 pbug * a bug!
1196 8b9017e4 2009-02-17 pbug */
1197 8b9017e4 2009-02-17 pbug
1198 c45084ea 2005-11-29 pbug for (i++; i < labelend; i++) {
1199 9c4921a5 2008-04-10 pbug int c0;
1200 9c4921a5 2008-04-10 pbug
1201 9c4921a5 2008-04-10 pbug c0 = buf[i];
1202 9c4921a5 2008-04-10 pbug *p++ = tolower(c0);
1203 c45084ea 2005-11-29 pbug }
1204 c45084ea 2005-11-29 pbug
1205 c45084ea 2005-11-29 pbug *p++ = '.';
1206 c45084ea 2005-11-29 pbug }
1207 c45084ea 2005-11-29 pbug
1208 c45084ea 2005-11-29 pbug /* XXX */
1209 c45084ea 2005-11-29 pbug if (&buf[sizeof(struct dns_header)] == end_name)
1210 c45084ea 2005-11-29 pbug *p++ = '.';
1211 c45084ea 2005-11-29 pbug
1212 c45084ea 2005-11-29 pbug *p = '\0';
1213 c45084ea 2005-11-29 pbug
1214 d64396b7 2014-05-09 pjp /* check for edns0 opt rr */
1215 d64396b7 2014-05-09 pjp do {
1216 d64396b7 2014-05-09 pjp /* if we don't have an additional section, break */
1217 d64396b7 2014-05-09 pjp if (additional != 1)
1218 d64396b7 2014-05-09 pjp break;
1219 d64396b7 2014-05-09 pjp
1220 cd18f393 2014-05-10 pjp i += (2 * sizeof(u_int16_t)) + 1;
1221 d64396b7 2014-05-09 pjp
1222 d64396b7 2014-05-09 pjp /* check that the minimum optrr fits */
1223 cd18f393 2014-05-10 pjp /* 10 */
1224 cd18f393 2014-05-10 pjp if (i + sizeof(struct dns_optrr) > len)
1225 d64396b7 2014-05-09 pjp break;
1226 d64396b7 2014-05-09 pjp
1227 d64396b7 2014-05-09 pjp opt = (struct dns_optrr *)&buf[i];
1228 cd18f393 2014-05-10 pjp if (opt->name[0] != 0)
1229 d64396b7 2014-05-09 pjp break;
1230 d64396b7 2014-05-09 pjp
1231 d64396b7 2014-05-09 pjp if (ntohs(opt->type) != DNS_TYPE_OPT)
1232 d64396b7 2014-05-09 pjp break;
1233 d64396b7 2014-05-09 pjp
1234 d64396b7 2014-05-09 pjp /* if we got options here I don't want to know about them */
1235 d64396b7 2014-05-09 pjp if (ntohs(opt->rdlen) > 0)
1236 d29dc98c 2014-05-09 pjp break;
1237 d29dc98c 2014-05-09 pjp
1238 d29dc98c 2014-05-09 pjp /* RFC 3225 */
1239 d29dc98c 2014-05-09 pjp if (ntohl(opt->ttl) & DNSSEC_OK)
1240 d29dc98c 2014-05-09 pjp q->dnssecok = 1;
1241 d29dc98c 2014-05-09 pjp else if (ntohl(opt->ttl) != 0)
1242 d64396b7 2014-05-09 pjp break;
1243 d64396b7 2014-05-09 pjp
1244 d64396b7 2014-05-09 pjp q->edns0len = ntohs(opt->class);
1245 8061892e 2014-05-10 pjp if (q->edns0len < 512)
1246 8061892e 2014-05-10 pjp q->edns0len = 512; /* RFC 6891 - page 10 */
1247 d64396b7 2014-05-09 pjp
1248 d64396b7 2014-05-09 pjp } while (0);
1249 d64396b7 2014-05-09 pjp
1250 c45084ea 2005-11-29 pbug /* fill our name into the dns header struct */
1251 d64396b7 2014-05-09 pjp
1252 c45084ea 2005-11-29 pbug memcpy(q->hdr->name, &buf[sizeof(struct dns_header)], q->hdr->namelen);
1253 c45084ea 2005-11-29 pbug
1254 c45084ea 2005-11-29 pbug /* make it lower case */
1255 c45084ea 2005-11-29 pbug
1256 c45084ea 2005-11-29 pbug for (i = 0; i < q->hdr->namelen; i++) {
1257 9c4921a5 2008-04-10 pbug int c0;
1258 9c4921a5 2008-04-10 pbug
1259 9c4921a5 2008-04-10 pbug c0 = q->hdr->name[i];
1260 9c4921a5 2008-04-10 pbug if (isalpha(c0)) {
1261 9c4921a5 2008-04-10 pbug q->hdr->name[i] = tolower(c0);
1262 c45084ea 2005-11-29 pbug }
1263 c45084ea 2005-11-29 pbug }
1264 c45084ea 2005-11-29 pbug
1265 c45084ea 2005-11-29 pbug /* parse type and class from the question */
1266 c45084ea 2005-11-29 pbug
1267 c45084ea 2005-11-29 pbug qtype = (u_int16_t *)(end_name + 1);
1268 c45084ea 2005-11-29 pbug qclass = (u_int16_t *)(end_name + sizeof(u_int16_t) + 1);
1269 c45084ea 2005-11-29 pbug
1270 c45084ea 2005-11-29 pbug memcpy((char *)&q->hdr->qtype, (char *)qtype, sizeof(u_int16_t));
1271 c45084ea 2005-11-29 pbug memcpy((char *)&q->hdr->qclass, (char *)qclass, sizeof(u_int16_t));
1272 d64396b7 2014-05-09 pjp
1273 c45084ea 2005-11-29 pbug
1274 c45084ea 2005-11-29 pbug return (q);
1275 c45084ea 2005-11-29 pbug }
1276 c45084ea 2005-11-29 pbug
1277 c45084ea 2005-11-29 pbug /*
1278 c45084ea 2005-11-29 pbug * FREE_QUESTION - free a question struct
1279 c45084ea 2005-11-29 pbug *
1280 c45084ea 2005-11-29 pbug */
1281 c45084ea 2005-11-29 pbug
1282 c45084ea 2005-11-29 pbug int
1283 c45084ea 2005-11-29 pbug free_question(struct question *q)
1284 c45084ea 2005-11-29 pbug {
1285 c45084ea 2005-11-29 pbug free(q->hdr->name);
1286 c45084ea 2005-11-29 pbug free(q->hdr);
1287 c45084ea 2005-11-29 pbug free(q->converted_name);
1288 c45084ea 2005-11-29 pbug free(q);
1289 c45084ea 2005-11-29 pbug
1290 c45084ea 2005-11-29 pbug return 0;
1291 c45084ea 2005-11-29 pbug }
1292 c45084ea 2005-11-29 pbug
1293 c45084ea 2005-11-29 pbug /*
1294 c45084ea 2005-11-29 pbug * DNS_LABEL - build a DNS NAME (with labels) from a canonical name
1295 c45084ea 2005-11-29 pbug *
1296 c45084ea 2005-11-29 pbug */
1297 c45084ea 2005-11-29 pbug
1298 c45084ea 2005-11-29 pbug char *
1299 c45084ea 2005-11-29 pbug dns_label(char *name, int *returnlen)
1300 c45084ea 2005-11-29 pbug {
1301 c45084ea 2005-11-29 pbug int len, newlen = 0;
1302 c45084ea 2005-11-29 pbug int i, lc = 0; /* lc = label count */
1303 eea748bc 2008-07-10 pbug
1304 c45084ea 2005-11-29 pbug char *dnslabel, *p;
1305 c45084ea 2005-11-29 pbug char *labels[255];
1306 c45084ea 2005-11-29 pbug char **pl;
1307 eea748bc 2008-07-10 pbug char tname[DNS_MAXNAME + 1]; /* 255 bytes + 1*/
1308 eea748bc 2008-07-10 pbug char *pt = &tname[0];
1309 c45084ea 2005-11-29 pbug
1310 c45084ea 2005-11-29 pbug
1311 c45084ea 2005-11-29 pbug if (name == NULL)
1312 c45084ea 2005-11-29 pbug return NULL;
1313 c45084ea 2005-11-29 pbug
1314 9c4921a5 2008-04-10 pbug #if __linux__
1315 9c4921a5 2008-04-10 pbug strncpy(tname, name, sizeof(tname));
1316 9c4921a5 2008-04-10 pbug tname[sizeof(tname) - 1] = 0;
1317 9c4921a5 2008-04-10 pbug #else
1318 c45084ea 2005-11-29 pbug strlcpy(tname, name, sizeof(tname));
1319 9c4921a5 2008-04-10 pbug #endif
1320 c45084ea 2005-11-29 pbug
1321 c45084ea 2005-11-29 pbug len = strlen(tname);
1322 c45084ea 2005-11-29 pbug if (tname[len - 1] == '.')
1323 c45084ea 2005-11-29 pbug tname[len - 1] = '\0';
1324 c45084ea 2005-11-29 pbug
1325 c45084ea 2005-11-29 pbug for (pl=labels;pl<&labels[254]&&(*pl=strsep(&pt,"."))!= NULL;pl++,lc++)
1326 c45084ea 2005-11-29 pbug newlen += strlen(*pl);
1327 c45084ea 2005-11-29 pbug
1328 c45084ea 2005-11-29 pbug newlen += lc; /* add label count to length */
1329 c45084ea 2005-11-29 pbug
1330 c45084ea 2005-11-29 pbug
1331 c45084ea 2005-11-29 pbug /* make the buffer space, add 1 for trailing NULL */
1332 c45084ea 2005-11-29 pbug if ((dnslabel = malloc(newlen + 1)) == NULL) {
1333 c45084ea 2005-11-29 pbug return NULL;
1334 c45084ea 2005-11-29 pbug }
1335 c45084ea 2005-11-29 pbug
1336 c45084ea 2005-11-29 pbug *returnlen = newlen + 1;
1337 c45084ea 2005-11-29 pbug dnslabel[newlen] = '\0'; /* trailing NULL */
1338 c45084ea 2005-11-29 pbug
1339 c45084ea 2005-11-29 pbug for (i = 0, p = dnslabel; i < lc; i++) {
1340 c45084ea 2005-11-29 pbug len = strlen(labels[i]);
1341 c45084ea 2005-11-29 pbug *p++ = len;
1342 9c4921a5 2008-04-10 pbug #if __linux__
1343 9c4921a5 2008-04-10 pbug /* XXX */
1344 9c4921a5 2008-04-10 pbug strncpy(p, labels[i], newlen - (p - dnslabel) + 1);
1345 9c4921a5 2008-04-10 pbug p[newlen - (p - dnslabel)] = 0;
1346 9c4921a5 2008-04-10 pbug #else
1347 c45084ea 2005-11-29 pbug strlcpy(p, labels[i], newlen - (p - dnslabel) + 1);
1348 9c4921a5 2008-04-10 pbug #endif
1349 c45084ea 2005-11-29 pbug p += len;
1350 c45084ea 2005-11-29 pbug }
1351 c45084ea 2005-11-29 pbug
1352 c45084ea 2005-11-29 pbug /*
1353 c45084ea 2005-11-29 pbug * XXX hack to make all DNS names lower case, we only preserve
1354 c45084ea 2005-11-29 pbug * case on compressed answers..
1355 c45084ea 2005-11-29 pbug */
1356 c45084ea 2005-11-29 pbug
1357 c45084ea 2005-11-29 pbug for (i = 0, p = dnslabel; i < *returnlen; i++) {
1358 9c4921a5 2008-04-10 pbug int c;
1359 9c4921a5 2008-04-10 pbug
1360 9c4921a5 2008-04-10 pbug c = *p;
1361 9c4921a5 2008-04-10 pbug if (isalpha(c))
1362 9c4921a5 2008-04-10 pbug *p = tolower(c);
1363 c45084ea 2005-11-29 pbug p++;
1364 c45084ea 2005-11-29 pbug }
1365 c45084ea 2005-11-29 pbug
1366 dd869383 2013-02-16 pjp dolog(LOG_DEBUG, "converting name= %s\n", name);
1367 c45084ea 2005-11-29 pbug
1368 c45084ea 2005-11-29 pbug return dnslabel;
1369 c45084ea 2005-11-29 pbug }
1370 c45084ea 2005-11-29 pbug
1371 c45084ea 2005-11-29 pbug /*
1372 c45084ea 2005-11-29 pbug * COMPRESS_LABEL - compress a DNS name, must be passed an entire reply
1373 c45084ea 2005-11-29 pbug * with the to be compressed name before the offset of
1374 c45084ea 2005-11-29 pbug * that reply.
1375 c45084ea 2005-11-29 pbug */
1376 c45084ea 2005-11-29 pbug
1377 b6dc64dc 2010-04-15 pbug int
1378 b6dc64dc 2010-04-15 pbug compress_label(u_char *buf, u_int16_t offset, int labellen)
1379 c45084ea 2005-11-29 pbug {
1380 b6dc64dc 2010-04-15 pbug u_char *label[256]; /* should be enough */
1381 b6dc64dc 2010-04-15 pbug u_char *end = &buf[offset];
1382 b6dc64dc 2010-04-15 pbug struct question {
1383 b6dc64dc 2010-04-15 pbug u_int16_t type;
1384 b6dc64dc 2010-04-15 pbug u_int16_t class;
1385 b6dc64dc 2010-04-15 pbug } __attribute__((packed));
1386 b6dc64dc 2010-04-15 pbug struct answer {
1387 b6dc64dc 2010-04-15 pbug u_int16_t type;
1388 b6dc64dc 2010-04-15 pbug u_int16_t class;
1389 b6dc64dc 2010-04-15 pbug u_int32_t ttl;
1390 b6dc64dc 2010-04-15 pbug u_int16_t rdlength;
1391 b6dc64dc 2010-04-15 pbug } __attribute__((packed));
1392 b6dc64dc 2010-04-15 pbug struct soa {
1393 b6dc64dc 2010-04-15 pbug u_int32_t serial;
1394 b6dc64dc 2010-04-15 pbug u_int32_t refresh;
1395 b6dc64dc 2010-04-15 pbug u_int32_t retry;
1396 b6dc64dc 2010-04-15 pbug u_int32_t expire;
1397 b6dc64dc 2010-04-15 pbug u_int32_t minttl;
1398 b6dc64dc 2010-04-15 pbug } __attribute__((packed));
1399 c45084ea 2005-11-29 pbug
1400 b6dc64dc 2010-04-15 pbug struct answer *a;
1401 eea748bc 2008-07-10 pbug
1402 b6dc64dc 2010-04-15 pbug u_int i, j;
1403 b6dc64dc 2010-04-15 pbug u_int checklen;
1404 b6dc64dc 2010-04-15 pbug u_int16_t *compressor;
1405 c45084ea 2005-11-29 pbug
1406 b6dc64dc 2010-04-15 pbug u_char *p, *e;
1407 b6dc64dc 2010-04-15 pbug u_char *compressmark;
1408 c45084ea 2005-11-29 pbug
1409 c45084ea 2005-11-29 pbug
1410 b6dc64dc 2010-04-15 pbug p = &buf[sizeof(struct dns_header)];
1411 b6dc64dc 2010-04-15 pbug label[0] = p;
1412 b6dc64dc 2010-04-15 pbug
1413 b6dc64dc 2010-04-15 pbug while (p <= end && *p) {
1414 b6dc64dc 2010-04-15 pbug p += *p;
1415 b6dc64dc 2010-04-15 pbug p++;
1416 b6dc64dc 2010-04-15 pbug }
1417 b6dc64dc 2010-04-15 pbug
1418 b6dc64dc 2010-04-15 pbug /*
1419 b6dc64dc 2010-04-15 pbug * the question label was bogus, we'll just get out of there, return 0
1420 b6dc64dc 2010-04-15 pbug */
1421 c45084ea 2005-11-29 pbug
1422 b6dc64dc 2010-04-15 pbug if (p >= end)
1423 b6dc64dc 2010-04-15 pbug return (0);
1424 c45084ea 2005-11-29 pbug
1425 b6dc64dc 2010-04-15 pbug p += sizeof(struct question);
1426 b6dc64dc 2010-04-15 pbug p++; /* one more */
1427 b6dc64dc 2010-04-15 pbug /* start of answer/additional/authoritative */
1428 c45084ea 2005-11-29 pbug
1429 b6dc64dc 2010-04-15 pbug for (i = 1; i < 100; i++) {
1430 b6dc64dc 2010-04-15 pbug label[i] = p;
1431 c45084ea 2005-11-29 pbug
1432 b6dc64dc 2010-04-15 pbug while (p <= end && *p) {
1433 b6dc64dc 2010-04-15 pbug if ((*p & 0xc0) == 0xc0) {
1434 b6dc64dc 2010-04-15 pbug p++;
1435 b6dc64dc 2010-04-15 pbug break;
1436 b6dc64dc 2010-04-15 pbug }
1437 b6dc64dc 2010-04-15 pbug p += *p;
1438 b6dc64dc 2010-04-15 pbug p++;
1439 c45084ea 2005-11-29 pbug
1440 b6dc64dc 2010-04-15 pbug if (p >= end)
1441 b6dc64dc 2010-04-15 pbug goto end;
1442 b6dc64dc 2010-04-15 pbug }
1443 b6dc64dc 2010-04-15 pbug
1444 b6dc64dc 2010-04-15 pbug p++; /* one more */
1445 b6dc64dc 2010-04-15 pbug
1446 b6dc64dc 2010-04-15 pbug
1447 b6dc64dc 2010-04-15 pbug a = (struct answer *)p;
1448 b6dc64dc 2010-04-15 pbug p += sizeof(struct answer);
1449 d9c1cf34 2011-02-27 pbug
1450 d9c1cf34 2011-02-27 pbug /* Thanks FreeLogic! */
1451 d9c1cf34 2011-02-27 pbug if (p >= end)
1452 d9c1cf34 2011-02-27 pbug goto end;
1453 b6dc64dc 2010-04-15 pbug
1454 b6dc64dc 2010-04-15 pbug switch (ntohs(a->type)) {
1455 b6dc64dc 2010-04-15 pbug case DNS_TYPE_A:
1456 b6dc64dc 2010-04-15 pbug p += sizeof(in_addr_t);
1457 b6dc64dc 2010-04-15 pbug break;
1458 b6dc64dc 2010-04-15 pbug case DNS_TYPE_AAAA:
1459 b6dc64dc 2010-04-15 pbug p += 16; /* sizeof 4 * 32 bit */
1460 b6dc64dc 2010-04-15 pbug break;
1461 b6dc64dc 2010-04-15 pbug case DNS_TYPE_TXT:
1462 f5989689 2014-04-21 pjp case DNS_TYPE_SPF:
1463 b6dc64dc 2010-04-15 pbug p += *p;
1464 b6dc64dc 2010-04-15 pbug p++;
1465 b6dc64dc 2010-04-15 pbug break;
1466 df34d218 2014-04-21 pjp case DNS_TYPE_SSHFP:
1467 df34d218 2014-04-21 pjp p++;
1468 df34d218 2014-04-21 pjp switch (*p) {
1469 df34d218 2014-04-21 pjp case 1:
1470 df34d218 2014-04-21 pjp p += DNS_SSHFP_SIZE_SHA1 + 1;
1471 df34d218 2014-04-21 pjp break;
1472 df34d218 2014-04-21 pjp case 2:
1473 df34d218 2014-04-21 pjp p += DNS_SSHFP_SIZE_SHA256 + 1;
1474 df34d218 2014-04-21 pjp break;
1475 df34d218 2014-04-21 pjp default:
1476 df34d218 2014-04-21 pjp /* XXX */
1477 df34d218 2014-04-21 pjp goto end;
1478 df34d218 2014-04-21 pjp }
1479 df34d218 2014-04-21 pjp
1480 df34d218 2014-04-21 pjp break;
1481 6f8190d3 2012-04-30 pbug case DNS_TYPE_SRV:
1482 6f8190d3 2012-04-30 pbug p += (2 * sizeof(u_int16_t)); /* priority, weight */
1483 6f8190d3 2012-04-30 pbug /* the port will be assumed in the fall through for
1484 6f8190d3 2012-04-30 pbug mx_priority..
1485 6f8190d3 2012-04-30 pbug */
1486 6f8190d3 2012-04-30 pbug /* FALLTHROUGH */
1487 b6dc64dc 2010-04-15 pbug case DNS_TYPE_MX:
1488 b6dc64dc 2010-04-15 pbug p += sizeof(u_int16_t); /* mx_priority */
1489 b6dc64dc 2010-04-15 pbug /* FALLTHROUGH */
1490 b6dc64dc 2010-04-15 pbug case DNS_TYPE_NS:
1491 b6dc64dc 2010-04-15 pbug case DNS_TYPE_PTR:
1492 b6dc64dc 2010-04-15 pbug case DNS_TYPE_CNAME:
1493 b6dc64dc 2010-04-15 pbug label[++i] = p;
1494 b6dc64dc 2010-04-15 pbug while (p <= end && *p) {
1495 b6dc64dc 2010-04-15 pbug if ((*p & 0xc0) == 0xc0) {
1496 b6dc64dc 2010-04-15 pbug p++;
1497 b6dc64dc 2010-04-15 pbug break;
1498 c45084ea 2005-11-29 pbug }
1499 b6dc64dc 2010-04-15 pbug p += *p;
1500 b6dc64dc 2010-04-15 pbug p++;
1501 c45084ea 2005-11-29 pbug
1502 b6dc64dc 2010-04-15 pbug if (p >= end)
1503 b6dc64dc 2010-04-15 pbug goto end;
1504 b6dc64dc 2010-04-15 pbug }
1505 c45084ea 2005-11-29 pbug
1506 b6dc64dc 2010-04-15 pbug p++; /* one more */
1507 b6dc64dc 2010-04-15 pbug break;
1508 b6dc64dc 2010-04-15 pbug case DNS_TYPE_SOA:
1509 b6dc64dc 2010-04-15 pbug /* nsserver */
1510 b6dc64dc 2010-04-15 pbug label[++i] = p;
1511 b6dc64dc 2010-04-15 pbug while (p <= end && *p) {
1512 b6dc64dc 2010-04-15 pbug if ((*p & 0xc0) == 0xc0) {
1513 b6dc64dc 2010-04-15 pbug p++;
1514 b6dc64dc 2010-04-15 pbug break;
1515 b6dc64dc 2010-04-15 pbug }
1516 b6dc64dc 2010-04-15 pbug p += *p;
1517 b6dc64dc 2010-04-15 pbug p++;
1518 b6dc64dc 2010-04-15 pbug if (p >= end)
1519 b6dc64dc 2010-04-15 pbug goto end;
1520 b6dc64dc 2010-04-15 pbug }
1521 c45084ea 2005-11-29 pbug
1522 b6dc64dc 2010-04-15 pbug p++; /* one more */
1523 b6dc64dc 2010-04-15 pbug
1524 b6dc64dc 2010-04-15 pbug if (p >= end)
1525 b6dc64dc 2010-04-15 pbug break;
1526 b6dc64dc 2010-04-15 pbug
1527 b6dc64dc 2010-04-15 pbug /* responsible person */
1528 b6dc64dc 2010-04-15 pbug label[++i] = p;
1529 b6dc64dc 2010-04-15 pbug while (p <= end && *p) {
1530 b6dc64dc 2010-04-15 pbug if ((*p & 0xc0) == 0xc0) {
1531 b6dc64dc 2010-04-15 pbug p++;
1532 b6dc64dc 2010-04-15 pbug break;
1533 b6dc64dc 2010-04-15 pbug }
1534 b6dc64dc 2010-04-15 pbug p += *p;
1535 b6dc64dc 2010-04-15 pbug p++;
1536 b6dc64dc 2010-04-15 pbug }
1537 b6dc64dc 2010-04-15 pbug
1538 b6dc64dc 2010-04-15 pbug p++; /* one more */
1539 b6dc64dc 2010-04-15 pbug
1540 b6dc64dc 2010-04-15 pbug if (p >= end)
1541 b6dc64dc 2010-04-15 pbug break;
1542 b6dc64dc 2010-04-15 pbug
1543 b6dc64dc 2010-04-15 pbug p += sizeof(struct soa); /* advance struct soa */
1544 b6dc64dc 2010-04-15 pbug
1545 b6dc64dc 2010-04-15 pbug break;
1546 1f1faf13 2014-05-11 pjp case DNS_TYPE_NAPTR:
1547 1f1faf13 2014-05-11 pjp p += (2 * sizeof(u_int16_t)); /* order and preference */
1548 1f1faf13 2014-05-11 pjp p += *p; /* flags */
1549 1f1faf13 2014-05-11 pjp p++;
1550 1f1faf13 2014-05-11 pjp p += *p; /* services */
1551 1f1faf13 2014-05-11 pjp p++;
1552 1f1faf13 2014-05-11 pjp p += *p; /* regexp */
1553 1f1faf13 2014-05-11 pjp p++;
1554 1f1faf13 2014-05-11 pjp
1555 1f1faf13 2014-05-11 pjp label[++i] = p;
1556 1f1faf13 2014-05-11 pjp while (p <= end && *p) {
1557 1f1faf13 2014-05-11 pjp if ((*p & 0xc0) == 0xc0) {
1558 1f1faf13 2014-05-11 pjp p++;
1559 1f1faf13 2014-05-11 pjp break;
1560 1f1faf13 2014-05-11 pjp }
1561 1f1faf13 2014-05-11 pjp p += *p;
1562 1f1faf13 2014-05-11 pjp p++;
1563 1f1faf13 2014-05-11 pjp
1564 1f1faf13 2014-05-11 pjp if (p >= end)
1565 1f1faf13 2014-05-11 pjp goto end;
1566 1f1faf13 2014-05-11 pjp }
1567 1f1faf13 2014-05-11 pjp
1568 1f1faf13 2014-05-11 pjp p++; /* one more */
1569 1f1faf13 2014-05-11 pjp break;
1570 1f1faf13 2014-05-11 pjp
1571 b6dc64dc 2010-04-15 pbug default:
1572 b6dc64dc 2010-04-15 pbug break;
1573 b6dc64dc 2010-04-15 pbug /* XXX */
1574 b6dc64dc 2010-04-15 pbug } /* switch */
1575 b6dc64dc 2010-04-15 pbug
1576 b6dc64dc 2010-04-15 pbug if (p >= end)
1577 b6dc64dc 2010-04-15 pbug break;
1578 b6dc64dc 2010-04-15 pbug } /* for (i *) */
1579 b6dc64dc 2010-04-15 pbug
1580 b6dc64dc 2010-04-15 pbug end:
1581 b6dc64dc 2010-04-15 pbug
1582 b6dc64dc 2010-04-15 pbug p = &buf[offset - labellen];
1583 b6dc64dc 2010-04-15 pbug checklen = labellen;
1584 b6dc64dc 2010-04-15 pbug
1585 b6dc64dc 2010-04-15 pbug for (;*p != 0;) {
1586 b6dc64dc 2010-04-15 pbug for (j = 0; j < i; j++) {
1587 b6dc64dc 2010-04-15 pbug for (e = label[j]; *e; e += *e, e++) {
1588 b6dc64dc 2010-04-15 pbug if ((*e & 0xc0) == 0xc0)
1589 b6dc64dc 2010-04-15 pbug break;
1590 b6dc64dc 2010-04-15 pbug
1591 b6dc64dc 2010-04-15 pbug if (memcasecmp(e, p, checklen) == 0) {
1592 b6dc64dc 2010-04-15 pbug /* e is now our compress offset */
1593 b6dc64dc 2010-04-15 pbug compressmark = e;
1594 b6dc64dc 2010-04-15 pbug goto out; /* found one */
1595 b6dc64dc 2010-04-15 pbug }
1596 b6dc64dc 2010-04-15 pbug } /* for (e .. */
1597 b6dc64dc 2010-04-15 pbug
1598 b6dc64dc 2010-04-15 pbug } /* for (j .. */
1599 b6dc64dc 2010-04-15 pbug
1600 c45084ea 2005-11-29 pbug if (*p > DNS_MAXLABEL)
1601 c45084ea 2005-11-29 pbug return 0; /* totally bogus label */
1602 c45084ea 2005-11-29 pbug
1603 c45084ea 2005-11-29 pbug checklen -= *p;
1604 c45084ea 2005-11-29 pbug p += *p;
1605 c45084ea 2005-11-29 pbug checklen--;
1606 c45084ea 2005-11-29 pbug p++;
1607 c45084ea 2005-11-29 pbug }
1608 c45084ea 2005-11-29 pbug
1609 b6dc64dc 2010-04-15 pbug return (0); /* no compression possible */
1610 c45084ea 2005-11-29 pbug
1611 c45084ea 2005-11-29 pbug out:
1612 c45084ea 2005-11-29 pbug /* take off our compress length */
1613 c45084ea 2005-11-29 pbug offset -= checklen;
1614 c45084ea 2005-11-29 pbug /* write compressed label */
1615 c45084ea 2005-11-29 pbug compressor = (u_int16_t *)&buf[offset];
1616 c45084ea 2005-11-29 pbug
1617 c45084ea 2005-11-29 pbug *compressor = (compressmark - &buf[0]);
1618 c45084ea 2005-11-29 pbug *compressor |= 0xc000;
1619 c45084ea 2005-11-29 pbug
1620 c45084ea 2005-11-29 pbug /* network byte order */
1621 1a591502 2010-03-28 pbug HTONS(*compressor);
1622 c45084ea 2005-11-29 pbug
1623 c45084ea 2005-11-29 pbug offset += sizeof(u_int16_t);
1624 c45084ea 2005-11-29 pbug
1625 c45084ea 2005-11-29 pbug return (offset);
1626 c45084ea 2005-11-29 pbug }
1627 c45084ea 2005-11-29 pbug
1628 c45084ea 2005-11-29 pbug /*
1629 c45084ea 2005-11-29 pbug * MEMCASECMP - check if buffer is identical to another buffer with
1630 c45084ea 2005-11-29 pbug * one exception if a character is alphabetic it's
1631 c45084ea 2005-11-29 pbug * compared to it's lower case value so that heLLo is
1632 c45084ea 2005-11-29 pbug * the same as hello
1633 c45084ea 2005-11-29 pbug */
1634 c45084ea 2005-11-29 pbug
1635 c45084ea 2005-11-29 pbug int
1636 c5bcfb5c 2010-07-27 pbug memcasecmp(u_char *b1, u_char *b2, int len)
1637 c45084ea 2005-11-29 pbug {
1638 c45084ea 2005-11-29 pbug int i;
1639 c45084ea 2005-11-29 pbug int identical = 1;
1640 c45084ea 2005-11-29 pbug
1641 c45084ea 2005-11-29 pbug for (i = 0; i < len; i++) {
1642 9c4921a5 2008-04-10 pbug int c0, c1;
1643 9c4921a5 2008-04-10 pbug
1644 9c4921a5 2008-04-10 pbug c0 = b1[i];
1645 9c4921a5 2008-04-10 pbug c1 = b2[i];
1646 9c4921a5 2008-04-10 pbug
1647 9c4921a5 2008-04-10 pbug if ((isalpha(c0) ? tolower(c0) : c0) !=
1648 9c4921a5 2008-04-10 pbug (isalpha(c1) ? tolower(c1) : c1)) {
1649 c45084ea 2005-11-29 pbug identical = 0;
1650 c45084ea 2005-11-29 pbug break;
1651 c45084ea 2005-11-29 pbug }
1652 c45084ea 2005-11-29 pbug }
1653 c45084ea 2005-11-29 pbug
1654 c45084ea 2005-11-29 pbug if (identical)
1655 c45084ea 2005-11-29 pbug return 0;
1656 c45084ea 2005-11-29 pbug
1657 c45084ea 2005-11-29 pbug return 1; /* XXX */
1658 c45084ea 2005-11-29 pbug }
1659 c45084ea 2005-11-29 pbug
1660 c45084ea 2005-11-29 pbug
1661 c45084ea 2005-11-29 pbug /*
1662 c45084ea 2005-11-29 pbug * LOOKUP_ZONE - look up a zone filling sd and returning RR TYPE, if error
1663 c45084ea 2005-11-29 pbug * occurs returns -1, and sets errno on what type of error.
1664 c45084ea 2005-11-29 pbug */
1665 c45084ea 2005-11-29 pbug
1666 c45084ea 2005-11-29 pbug
1667 c45084ea 2005-11-29 pbug int
1668 959d1769 2010-04-05 pbug lookup_zone(DB *db, struct question *question, struct domain *sd, int *lzerrno, char *replystring, int wildcard)
1669 c45084ea 2005-11-29 pbug {
1670 c45084ea 2005-11-29 pbug
1671 c45084ea 2005-11-29 pbug int plen, onemore = 0;
1672 c45084ea 2005-11-29 pbug int ret = 0;
1673 457912e1 2014-05-18 pjp int returnval, error;
1674 959d1769 2010-04-05 pbug int w = 0;
1675 9c4921a5 2008-04-10 pbug
1676 c45084ea 2005-11-29 pbug char *wildlookup = "*";
1677 9c4921a5 2008-04-10 pbug char *p;
1678 c45084ea 2005-11-29 pbug
1679 c45084ea 2005-11-29 pbug DBT key, data;
1680 c45084ea 2005-11-29 pbug
1681 c45084ea 2005-11-29 pbug /*
1682 c45084ea 2005-11-29 pbug * if the asked domain name is foo.bar.baz.org then
1683 c45084ea 2005-11-29 pbug * lookup foo.bar.baz.org, bar.baz.org, baz.org,
1684 c45084ea 2005-11-29 pbug * org and if there is a match return that.
1685 c45084ea 2005-11-29 pbug */
1686 c45084ea 2005-11-29 pbug
1687 c45084ea 2005-11-29 pbug p = question->hdr->name;
1688 c45084ea 2005-11-29 pbug plen = question->hdr->namelen;
1689 c45084ea 2005-11-29 pbug onemore = 0;
1690 c45084ea 2005-11-29 pbug
1691 c45084ea 2005-11-29 pbug returnval = 0;
1692 c45084ea 2005-11-29 pbug
1693 c45084ea 2005-11-29 pbug do {
1694 c45084ea 2005-11-29 pbug
1695 9c4921a5 2008-04-10 pbug memset(&key, 0, sizeof(key));
1696 9c4921a5 2008-04-10 pbug memset(&data, 0, sizeof(data));
1697 9c4921a5 2008-04-10 pbug
1698 c45084ea 2005-11-29 pbug key.data = (char *)p;
1699 c45084ea 2005-11-29 pbug key.size = plen;
1700 c45084ea 2005-11-29 pbug
1701 c45084ea 2005-11-29 pbug data.data = NULL;
1702 c45084ea 2005-11-29 pbug data.size = 0;
1703 9c4921a5 2008-04-10 pbug
1704 9c4921a5 2008-04-10 pbug ret = db->get(db, NULL, &key, &data, 0);
1705 c45084ea 2005-11-29 pbug
1706 c45084ea 2005-11-29 pbug if (ret != 0) {
1707 959d1769 2010-04-05 pbug if (! wildcard)
1708 959d1769 2010-04-05 pbug w = 1;
1709 c45084ea 2005-11-29 pbug /* next label */
1710 9c4921a5 2008-04-10 pbug if (*p != 0) {
1711 c45084ea 2005-11-29 pbug plen -= (*p + 1);
1712 c45084ea 2005-11-29 pbug p = (p + (*p + 1));
1713 9c4921a5 2008-04-10 pbug } else if (*p == 0 && ! onemore) {
1714 c45084ea 2005-11-29 pbug plen = 1;
1715 c45084ea 2005-11-29 pbug onemore = 1;
1716 c45084ea 2005-11-29 pbug continue;
1717 c45084ea 2005-11-29 pbug }
1718 c45084ea 2005-11-29 pbug } else {
1719 c45084ea 2005-11-29 pbug /* we have a match check if the type has an answer, if not we leave */
1720 c45084ea 2005-11-29 pbug if (data.size != sizeof(struct domain)) {
1721 dd869383 2013-02-16 pjp dolog(LOG_INFO, "btree db is damaged, drop\n");
1722 c45084ea 2005-11-29 pbug *lzerrno = ERR_DROP;
1723 c45084ea 2005-11-29 pbug return -1;
1724 c45084ea 2005-11-29 pbug }
1725 c45084ea 2005-11-29 pbug
1726 c45084ea 2005-11-29 pbug memcpy((char *)sd, (char *)data.data, data.size);
1727 c45084ea 2005-11-29 pbug snprintf(replystring, DNS_MAXNAME, "%s", sd->zonename);
1728 4cc84fc5 2010-03-19 pbug
1729 959d1769 2010-04-05 pbug /*
1730 959d1769 2010-04-05 pbug * If we're not wildcarding and ns_type is 0, NXDOMAIN
1731 959d1769 2010-04-05 pbug */
1732 959d1769 2010-04-05 pbug if (! wildcard)
1733 959d1769 2010-04-05 pbug if (w && sd->ns_type == 0) {
1734 4cc84fc5 2010-03-19 pbug *lzerrno = ERR_NXDOMAIN;
1735 4cc84fc5 2010-03-19 pbug return -1;
1736 4cc84fc5 2010-03-19 pbug }
1737 4cc84fc5 2010-03-19 pbug
1738 959d1769 2010-04-05 pbug /*
1739 959d1769 2010-04-05 pbug * we're of ns_type > 0, return an NS record
1740 959d1769 2010-04-05 pbug */
1741 959d1769 2010-04-05 pbug
1742 b0a72376 2010-04-01 pbug if (sd->ns_type > 0) {
1743 4cc84fc5 2010-03-19 pbug returnval = DNS_TYPE_NS;
1744 4cc84fc5 2010-03-19 pbug *lzerrno = ERR_NOERROR;
1745 4cc84fc5 2010-03-19 pbug goto out;
1746 4cc84fc5 2010-03-19 pbug }
1747 f98bb34d 2011-09-19 pbug
1748 f98bb34d 2011-09-19 pbug /*
1749 f98bb34d 2011-09-19 pbug * check if our record is dynamic (non-static)
1750 f98bb34d 2011-09-19 pbug * if so, we'll hand it down to the recurse
1751 f98bb34d 2011-09-19 pbug * process later on...
1752 f98bb34d 2011-09-19 pbug */
1753 c45084ea 2005-11-29 pbug
1754 f98bb34d 2011-09-19 pbug if (! (sd->flags & DOMAIN_STATIC_ZONE)) {
1755 dd869383 2013-02-16 pjp dolog(LOG_INFO, "non-static zone %s passed to recurse process\n", sd->zonename);
1756 f98bb34d 2011-09-19 pbug *lzerrno = ERR_NOERROR;
1757 f98bb34d 2011-09-19 pbug return (-1);
1758 f98bb34d 2011-09-19 pbug }
1759 c45084ea 2005-11-29 pbug
1760 c45084ea 2005-11-29 pbug
1761 457912e1 2014-05-18 pjp returnval = check_qtype(sd, ntohs(question->hdr->qtype), 0, &error);
1762 457912e1 2014-05-18 pjp if (returnval == 0) {
1763 8c7bdc7c 2009-03-07 pbug *lzerrno = ERR_NOERROR;
1764 457912e1 2014-05-18 pjp return (-1);
1765 c45084ea 2005-11-29 pbug }
1766 c45084ea 2005-11-29 pbug
1767 c45084ea 2005-11-29 pbug break;
1768 c45084ea 2005-11-29 pbug }
1769 c45084ea 2005-11-29 pbug
1770 c45084ea 2005-11-29 pbug
1771 9c4921a5 2008-04-10 pbug } while (*p != 0 && ret != 0);
1772 c45084ea 2005-11-29 pbug
1773 c45084ea 2005-11-29 pbug if (ret != 0) {
1774 959d1769 2010-04-05 pbug /*
1775 959d1769 2010-04-05 pbug * somehow we managed to get here and wildcardding is off
1776 959d1769 2010-04-05 pbug * return with NXDOMAIN
1777 959d1769 2010-04-05 pbug */
1778 959d1769 2010-04-05 pbug if (! wildcard) {
1779 959d1769 2010-04-05 pbug *lzerrno = ERR_NXDOMAIN;
1780 959d1769 2010-04-05 pbug return -1;
1781 959d1769 2010-04-05 pbug }
1782 959d1769 2010-04-05 pbug
1783 9c4921a5 2008-04-10 pbug memset(&key, 0, sizeof(key));
1784 9c4921a5 2008-04-10 pbug memset(&data, 0, sizeof(data));
1785 9c4921a5 2008-04-10 pbug
1786 c45084ea 2005-11-29 pbug key.data = wildlookup;
1787 c45084ea 2005-11-29 pbug key.size = 1;
1788 c45084ea 2005-11-29 pbug
1789 9c4921a5 2008-04-10 pbug if ((ret = db->get(db, NULL, &key, &data, 0)) != 0) {
1790 9c4921a5 2008-04-10 pbug db->err(db, ret, "db->get");
1791 dd869383 2013-02-16 pjp dolog(LOG_INFO, "don't have wildcard answer\n");
1792 c45084ea 2005-11-29 pbug *lzerrno = ERR_NXDOMAIN;
1793 c45084ea 2005-11-29 pbug return -1;
1794 c45084ea 2005-11-29 pbug
1795 c45084ea 2005-11-29 pbug }
1796 c45084ea 2005-11-29 pbug if (data.size != sizeof(struct domain)) {
1797 dd869383 2013-02-16 pjp dolog(LOG_INFO, "btree db is damaged, drop\n");
1798 c45084ea 2005-11-29 pbug *lzerrno = ERR_DROP;
1799 c45084ea 2005-11-29 pbug return -1;
1800 c45084ea 2005-11-29 pbug }
1801 c45084ea 2005-11-29 pbug
1802 c45084ea 2005-11-29 pbug memcpy((char *)sd, (char *)data.data, data.size);
1803 c45084ea 2005-11-29 pbug
1804 b0a72376 2010-04-01 pbug if (sd->ns_type > 0) {
1805 4cc84fc5 2010-03-19 pbug returnval = DNS_TYPE_NS;
1806 4cc84fc5 2010-03-19 pbug goto out;
1807 4cc84fc5 2010-03-19 pbug }
1808 c45084ea 2005-11-29 pbug
1809 457912e1 2014-05-18 pjp returnval = check_qtype(sd, ntohs(question->hdr->qtype), 1, &error);
1810 457912e1 2014-05-18 pjp if (returnval == 0) {
1811 457912e1 2014-05-18 pjp switch (error) {
1812 457912e1 2014-05-18 pjp case -2:
1813 c45084ea 2005-11-29 pbug *lzerrno = ERR_NXDOMAIN;
1814 1f1faf13 2014-05-11 pjp break;
1815 457912e1 2014-05-18 pjp case -1:
1816 457912e1 2014-05-18 pjp *lzerrno = ERR_NOERROR;
1817 f5989689 2014-04-21 pjp break;
1818 f5989689 2014-04-21 pjp }
1819 457912e1 2014-05-18 pjp
1820 457912e1 2014-05-18 pjp return (-1);
1821 c45084ea 2005-11-29 pbug }
1822 c45084ea 2005-11-29 pbug
1823 c45084ea 2005-11-29 pbug snprintf(replystring, DNS_MAXNAME, "*");
1824 c45084ea 2005-11-29 pbug }
1825 c45084ea 2005-11-29 pbug
1826 4cc84fc5 2010-03-19 pbug out:
1827 c45084ea 2005-11-29 pbug return returnval;
1828 c45084ea 2005-11-29 pbug }
1829 c45084ea 2005-11-29 pbug
1830 c45084ea 2005-11-29 pbug /*
1831 c45084ea 2005-11-29 pbug * BUILD_FAKE_QUESTION - fill the fake question structure with the DNS query.
1832 c45084ea 2005-11-29 pbug */
1833 c45084ea 2005-11-29 pbug
1834 c45084ea 2005-11-29 pbug struct question *
1835 c45084ea 2005-11-29 pbug build_fake_question(char *name, int namelen, u_int16_t type)
1836 c45084ea 2005-11-29 pbug {
1837 c45084ea 2005-11-29 pbug struct question *q;
1838 c45084ea 2005-11-29 pbug
1839 c45084ea 2005-11-29 pbug q = (void *)calloc(1, sizeof(struct question));
1840 c45084ea 2005-11-29 pbug if (q == NULL) {
1841 dd869383 2013-02-16 pjp dolog(LOG_INFO, "calloc: %s\n", strerror(errno));
1842 c45084ea 2005-11-29 pbug return NULL;
1843 c45084ea 2005-11-29 pbug }
1844 c45084ea 2005-11-29 pbug
1845 c45084ea 2005-11-29 pbug q->hdr = (void *)calloc(1, sizeof(struct dns_question_hdr));
1846 c45084ea 2005-11-29 pbug if (q->hdr == NULL) {
1847 dd869383 2013-02-16 pjp dolog(LOG_INFO, "calloc: %s\n", strerror(errno));
1848 c45084ea 2005-11-29 pbug free(q);
1849 c45084ea 2005-11-29 pbug return NULL;
1850 c45084ea 2005-11-29 pbug }
1851 c45084ea 2005-11-29 pbug q->hdr->namelen = namelen;
1852 c45084ea 2005-11-29 pbug q->hdr->name = (void *) calloc(1, q->hdr->namelen);
1853 c45084ea 2005-11-29 pbug if (q->hdr->name == NULL) {
1854 dd869383 2013-02-16 pjp dolog(LOG_INFO, "calloc: %s\n", strerror(errno));
1855 c45084ea 2005-11-29 pbug free(q->hdr);
1856 c45084ea 2005-11-29 pbug free(q);
1857 c45084ea 2005-11-29 pbug return NULL;
1858 c45084ea 2005-11-29 pbug }
1859 c45084ea 2005-11-29 pbug q->converted_name = NULL;
1860 c45084ea 2005-11-29 pbug
1861 c45084ea 2005-11-29 pbug /* fill our name into the dns header struct */
1862 c45084ea 2005-11-29 pbug
1863 c45084ea 2005-11-29 pbug memcpy(q->hdr->name, name, q->hdr->namelen);
1864 c45084ea 2005-11-29 pbug
1865 c45084ea 2005-11-29 pbug q->hdr->qtype = type;
1866 c45084ea 2005-11-29 pbug q->hdr->qclass = htons(DNS_CLASS_IN);
1867 c45084ea 2005-11-29 pbug
1868 c45084ea 2005-11-29 pbug return (q);
1869 c45084ea 2005-11-29 pbug }
1870 d6a24f1e 2008-07-24 pbug
1871 d6a24f1e 2008-07-24 pbug /*
1872 d6a24f1e 2008-07-24 pbug * GET_SOA - get authoritative soa for a particular domain
1873 d6a24f1e 2008-07-24 pbug */
1874 d6a24f1e 2008-07-24 pbug
1875 d6a24f1e 2008-07-24 pbug int
1876 959d1769 2010-04-05 pbug get_soa(DB *db, struct question *question, struct domain *sd, int wildcard)
1877 d6a24f1e 2008-07-24 pbug {
1878 d6a24f1e 2008-07-24 pbug int plen;
1879 d6a24f1e 2008-07-24 pbug int ret = 0;
1880 d6a24f1e 2008-07-24 pbug
1881 d6a24f1e 2008-07-24 pbug DBT key, data;
1882 d6a24f1e 2008-07-24 pbug
1883 d6a24f1e 2008-07-24 pbug char *p;
1884 d6a24f1e 2008-07-24 pbug
1885 d6a24f1e 2008-07-24 pbug p = question->hdr->name;
1886 d6a24f1e 2008-07-24 pbug plen = question->hdr->namelen;
1887 d6a24f1e 2008-07-24 pbug
1888 d6a24f1e 2008-07-24 pbug do {
1889 d6a24f1e 2008-07-24 pbug
1890 d6a24f1e 2008-07-24 pbug memset(&key, 0, sizeof(key));
1891 d6a24f1e 2008-07-24 pbug memset(&data, 0, sizeof(data));
1892 d6a24f1e 2008-07-24 pbug
1893 d6a24f1e 2008-07-24 pbug key.data = (char *)p;
1894 d6a24f1e 2008-07-24 pbug key.size = plen;
1895 d6a24f1e 2008-07-24 pbug
1896 d6a24f1e 2008-07-24 pbug data.data = NULL;
1897 d6a24f1e 2008-07-24 pbug data.size = 0;
1898 d6a24f1e 2008-07-24 pbug
1899 d6a24f1e 2008-07-24 pbug ret = db->get(db, NULL, &key, &data, 0);
1900 d6a24f1e 2008-07-24 pbug if (ret != 0) {
1901 959d1769 2010-04-05 pbug /*
1902 959d1769 2010-04-05 pbug * If we're not wildcarding end the search here and
1903 959d1769 2010-04-05 pbug * return with -1
1904 959d1769 2010-04-05 pbug */
1905 959d1769 2010-04-05 pbug if (! wildcard)
1906 555d3dda 2009-11-08 pbug return -1;
1907 555d3dda 2009-11-08 pbug
1908 d6a24f1e 2008-07-24 pbug plen -= (*p + 1);
1909 d6a24f1e 2008-07-24 pbug p = (p + (*p + 1));
1910 d6a24f1e 2008-07-24 pbug continue;
1911 d6a24f1e 2008-07-24 pbug }
1912 d6a24f1e 2008-07-24 pbug
1913 d6a24f1e 2008-07-24 pbug if (data.size != sizeof(struct domain)) {
1914 dd869383 2013-02-16 pjp dolog(LOG_INFO, "btree db is damaged, drop\n");
1915 d6a24f1e 2008-07-24 pbug return -1;
1916 d6a24f1e 2008-07-24 pbug }
1917 d6a24f1e 2008-07-24 pbug
1918 d6a24f1e 2008-07-24 pbug memcpy((char *)sd, (char *)data.data, data.size);
1919 d6a24f1e 2008-07-24 pbug
1920 d6a24f1e 2008-07-24 pbug if ((sd->flags & DOMAIN_HAVE_SOA) == DOMAIN_HAVE_SOA) {
1921 d6a24f1e 2008-07-24 pbug /* we'll take this one */
1922 d6a24f1e 2008-07-24 pbug return 0;
1923 d6a24f1e 2008-07-24 pbug } else {
1924 d6a24f1e 2008-07-24 pbug plen -= (*p + 1);
1925 d6a24f1e 2008-07-24 pbug p = (p + (*p + 1));
1926 d6a24f1e 2008-07-24 pbug }
1927 d6a24f1e 2008-07-24 pbug
1928 d6a24f1e 2008-07-24 pbug } while (*p);
1929 d6a24f1e 2008-07-24 pbug
1930 d6a24f1e 2008-07-24 pbug return -1;
1931 d6a24f1e 2008-07-24 pbug }
1932 c37b91b6 2009-03-10 pbug
1933 c37b91b6 2009-03-10 pbug /*
1934 c37b91b6 2009-03-10 pbug * GET_DNS_TYPE - take integer and compare to table, then spit back a static
1935 c37b91b6 2009-03-10 pbug * string with the result. This function can't fail.
1936 c37b91b6 2009-03-10 pbug */
1937 c37b91b6 2009-03-10 pbug
1938 c37b91b6 2009-03-10 pbug char *
1939 c37b91b6 2009-03-10 pbug get_dns_type(int dnstype)
1940 c37b91b6 2009-03-10 pbug {
1941 c37b91b6 2009-03-10 pbug static char type[128];
1942 c37b91b6 2009-03-10 pbug struct typetable *t;
1943 c37b91b6 2009-03-10 pbug
1944 c37b91b6 2009-03-10 pbug t = TT;
1945 c37b91b6 2009-03-10 pbug
1946 c37b91b6 2009-03-10 pbug while (t->type != NULL) {
1947 c37b91b6 2009-03-10 pbug if (dnstype == t->number)
1948 c37b91b6 2009-03-10 pbug break;
1949 c37b91b6 2009-03-10 pbug
1950 c37b91b6 2009-03-10 pbug t = (t + 1);
1951 c37b91b6 2009-03-10 pbug }
1952 c37b91b6 2009-03-10 pbug
1953 c37b91b6 2009-03-10 pbug if (t->type == NULL) {
1954 c37b91b6 2009-03-10 pbug snprintf(type, sizeof(type) - 1, "%u", dnstype);
1955 c37b91b6 2009-03-10 pbug } else
1956 75d81e0d 2009-03-13 pbug snprintf(type, sizeof(type) - 1, "%s(%u)", t->type, dnstype);
1957 c37b91b6 2009-03-10 pbug
1958 c37b91b6 2009-03-10 pbug return (type);
1959 c37b91b6 2009-03-10 pbug }
1960 f1e3cfca 2010-03-12 pbug
1961 f1e3cfca 2010-03-12 pbug /*
1962 18fa5f8a 2010-03-14 pbug * MAINLOOP - does the polling of tcp & udp descriptors and if ready receives the
1963 f1e3cfca 2010-03-12 pbug * requests, builds the question and calls for replies, loops
1964 f1e3cfca 2010-03-12 pbug *
1965 f1e3cfca 2010-03-12 pbug */
1966 f1e3cfca 2010-03-12 pbug
1967 f1e3cfca 2010-03-12 pbug void
1968 c25e4088 2011-06-28 pbug mainloop(struct cfg *cfg)
1969 f1e3cfca 2010-03-12 pbug {
1970 f1e3cfca 2010-03-12 pbug fd_set rset;
1971 f1e3cfca 2010-03-12 pbug int sel;
1972 a2b189d4 2014-10-08 pjp int len, slen;
1973 f1e3cfca 2010-03-12 pbug int is_ipv6;
1974 f1e3cfca 2010-03-12 pbug int i;
1975 f1e3cfca 2010-03-12 pbug int istcp = 1;
1976 f1e3cfca 2010-03-12 pbug int maxso;
1977 f1e3cfca 2010-03-12 pbug int so;
1978 f1e3cfca 2010-03-12 pbug int type0, type1;
1979 f1e3cfca 2010-03-12 pbug int lzerrno;
1980 959d1769 2010-04-05 pbug int wildcard = 0;
1981 c0963faf 2014-05-01 pjp int filter = 0;
1982 4667e76d 2014-05-05 pjp int rcheck = 0;
1983 6f8d6a57 2014-05-18 pjp int blacklist = 1;
1984 c25e4088 2011-06-28 pbug int sp;
1985 c25e4088 2011-06-28 pbug int lfd;
1986 18fa5f8a 2010-03-14 pbug
1987 18fa5f8a 2010-03-14 pbug u_int32_t received_ttl;
1988 bd1eb3c6 2011-02-13 pbug #if defined __FreeBSD__ || defined __OpenBSD__
1989 18fa5f8a 2010-03-14 pbug u_char *ttlptr;
1990 18fa5f8a 2010-03-14 pbug #else
1991 18fa5f8a 2010-03-14 pbug int *ttlptr;
1992 18fa5f8a 2010-03-14 pbug #endif
1993 f1e3cfca 2010-03-12 pbug
1994 f1e3cfca 2010-03-12 pbug u_int8_t aregion; /* region where the address comes from */
1995 f1e3cfca 2010-03-12 pbug
1996 f1e3cfca 2010-03-12 pbug char *pbuf;
1997 18fa5f8a 2010-03-14 pbug char buf[4096];
1998 df34d218 2014-04-21 pjp char *replybuf = NULL;
1999 f1e3cfca 2010-03-12 pbug char address[INET6_ADDRSTRLEN];
2000 f1e3cfca 2010-03-12 pbug char replystring[DNS_MAXNAME + 1];
2001 f1e3cfca 2010-03-12 pbug char fakereplystring[DNS_MAXNAME + 1];
2002 18fa5f8a 2010-03-14 pbug char controlbuf[64];
2003 f1e3cfca 2010-03-12 pbug
2004 f1e3cfca 2010-03-12 pbug union {
2005 f1e3cfca 2010-03-12 pbug struct sockaddr sa;
2006 f1e3cfca 2010-03-12 pbug struct sockaddr_in sin;
2007 f1e3cfca 2010-03-12 pbug struct sockaddr_in6 sin6;
2008 f1e3cfca 2010-03-12 pbug } sockaddr_large;
2009 f1e3cfca 2010-03-12 pbug
2010 1e8325ae 2010-03-28 pbug socklen_t fromlen = sizeof(sockaddr_large);
2011 b6dc64dc 2010-04-15 pbug socklen_t namelen = sizeof(struct sockaddr_storage);
2012 c25e4088 2011-06-28 pbug socklen_t logfromlen = sizeof(struct sockaddr_storage);
2013 f1e3cfca 2010-03-12 pbug
2014 f1e3cfca 2010-03-12 pbug struct sockaddr *from = (void *)&sockaddr_large;
2015 f1e3cfca 2010-03-12 pbug struct sockaddr_in *sin;
2016 f1e3cfca 2010-03-12 pbug struct sockaddr_in6 *sin6;
2017 b6dc64dc 2010-04-15 pbug struct sockaddr_storage sto;
2018 c25e4088 2011-06-28 pbug struct sockaddr_storage logfrom;
2019 f1e3cfca 2010-03-12 pbug
2020 f1e3cfca 2010-03-12 pbug struct dns_header *dh;
2021 f1e3cfca 2010-03-12 pbug struct question *question, *fakequestion;
2022 f1e3cfca 2010-03-12 pbug struct domain sd0, sd1;
2023 f1e3cfca 2010-03-12 pbug
2024 f1e3cfca 2010-03-12 pbug struct sreply sreply;
2025 b6dc64dc 2010-04-15 pbug struct srecurseheader rh;
2026 f1e3cfca 2010-03-12 pbug struct timeval tv = { 10, 0};
2027 18fa5f8a 2010-03-14 pbug
2028 18fa5f8a 2010-03-14 pbug struct msghdr msgh;
2029 18fa5f8a 2010-03-14 pbug struct cmsghdr *cmsg;
2030 18fa5f8a 2010-03-14 pbug struct iovec iov;
2031 f1e3cfca 2010-03-12 pbug
2032 f1e3cfca 2010-03-12 pbug int flag;
2033 b6dc64dc 2010-04-15 pbug int recursion = 0;
2034 f1e3cfca 2010-03-12 pbug
2035 c25e4088 2011-06-28 pbug
2036 f1e3cfca 2010-03-12 pbug SLIST_INIT(&tcpshead);
2037 a042435d 2010-03-19 pbug collects_init();
2038 f1e3cfca 2010-03-12 pbug
2039 df34d218 2014-04-21 pjp replybuf = calloc(1, 65536);
2040 df34d218 2014-04-21 pjp if (replybuf == NULL) {
2041 df34d218 2014-04-21 pjp dolog(LOG_ERR, "calloc: %s\n", strerror(errno));
2042 df34d218 2014-04-21 pjp slave_shutdown();
2043 df34d218 2014-04-21 pjp exit(1);
2044 df34d218 2014-04-21 pjp }
2045 df34d218 2014-04-21 pjp
2046 df34d218 2014-04-21 pjp
2047 c25e4088 2011-06-28 pbug sp = cfg->recurse;
2048 c25e4088 2011-06-28 pbug lfd = cfg->log;
2049 c25e4088 2011-06-28 pbug
2050 f1e3cfca 2010-03-12 pbug /*
2051 f1e3cfca 2010-03-12 pbug * set descriptors nonblocking, and listen on them
2052 f1e3cfca 2010-03-12 pbug */
2053 f1e3cfca 2010-03-12 pbug
2054 c25e4088 2011-06-28 pbug for (i = 0; i < cfg->sockcount; i++) {
2055 c25e4088 2011-06-28 pbug listen(cfg->tcp[i], 5);
2056 f1e3cfca 2010-03-12 pbug }
2057 f1e3cfca 2010-03-12 pbug
2058 f1e3cfca 2010-03-12 pbug for (;;) {
2059 f1e3cfca 2010-03-12 pbug is_ipv6 = 0;
2060 f1e3cfca 2010-03-12 pbug maxso = 0;
2061 f1e3cfca 2010-03-12 pbug /*
2062 f1e3cfca 2010-03-12 pbug * check for timeouts
2063 f1e3cfca 2010-03-12 pbug */
2064 f1e3cfca 2010-03-12 pbug
2065 ec3f221d 2014-05-01 pjp #ifdef __linux__
2066 c0963faf 2014-05-01 pjp SLIST_FOREACH(tnp, &tcpshead, tcps_entry) {
2067 ec3f221d 2014-05-01 pjp #else
2068 ec3f221d 2014-05-01 pjp SLIST_FOREACH_SAFE(tnp, &tcpshead, tcps_entry, tntmp) {
2069 ec3f221d 2014-05-01 pjp #endif
2070 f1e3cfca 2010-03-12 pbug if ((tnp->time + 10) < time(NULL)) {
2071 f1e3cfca 2010-03-12 pbug free(tnp->input);
2072 f1e3cfca 2010-03-12 pbug free(tnp->ident);
2073 f1e3cfca 2010-03-12 pbug free(tnp->address);
2074 f1e3cfca 2010-03-12 pbug close(tnp->so);
2075 c0963faf 2014-05-01 pjp SLIST_REMOVE(&tcpshead, tnp, tcps, tcps_entry);
2076 f1e3cfca 2010-03-12 pbug free(tnp);
2077 f1e3cfca 2010-03-12 pbug }
2078 f1e3cfca 2010-03-12 pbug }
2079 f1e3cfca 2010-03-12 pbug
2080 f1e3cfca 2010-03-12 pbug FD_ZERO(&rset);
2081 c25e4088 2011-06-28 pbug for (i = 0; i < cfg->sockcount; i++) {
2082 c25e4088 2011-06-28 pbug if (maxso < cfg->tcp[i])
2083 c25e4088 2011-06-28 pbug maxso = cfg->tcp[i];
2084 18fa5f8a 2010-03-14 pbug
2085 c25e4088 2011-06-28 pbug if (maxso < cfg->udp[i])
2086 c25e4088 2011-06-28 pbug maxso = cfg->udp[i];
2087 f98bb34d 2011-09-19 pbug
2088 f98bb34d 2011-09-19 pbug if (axfrport && maxso < cfg->axfr[i])
2089 f98bb34d 2011-09-19 pbug maxso = cfg->axfr[i];
2090 f1e3cfca 2010-03-12 pbug
2091 c25e4088 2011-06-28 pbug FD_SET(cfg->tcp[i], &rset);
2092 c25e4088 2011-06-28 pbug FD_SET(cfg->udp[i], &rset);
2093 f98bb34d 2011-09-19 pbug
2094 f98bb34d 2011-09-19 pbug if (axfrport)
2095 f98bb34d 2011-09-19 pbug FD_SET(cfg->axfr[i], &rset);
2096 f1e3cfca 2010-03-12 pbug }
2097 f1e3cfca 2010-03-12 pbug
2098 c0963faf 2014-05-01 pjp SLIST_FOREACH(tnp, &tcpshead, tcps_entry) {
2099 f1e3cfca 2010-03-12 pbug if (maxso < tnp->so)
2100 f1e3cfca 2010-03-12 pbug maxso = tnp->so;
2101 f1e3cfca 2010-03-12 pbug
2102 f1e3cfca 2010-03-12 pbug FD_SET(tnp->so, &rset);
2103 f1e3cfca 2010-03-12 pbug }
2104 e40d4d5e 2010-03-13 pbug
2105 c25e4088 2011-06-28 pbug if (logging.bind == 1) {
2106 c25e4088 2011-06-28 pbug if (maxso < lfd)
2107 c25e4088 2011-06-28 pbug maxso = lfd;
2108 c25e4088 2011-06-28 pbug FD_SET(lfd, &rset);
2109 c25e4088 2011-06-28 pbug }
2110 c25e4088 2011-06-28 pbug
2111 e40d4d5e 2010-03-13 pbug tv.tv_sec = 10;
2112 e40d4d5e 2010-03-13 pbug tv.tv_usec = 0;
2113 f1e3cfca 2010-03-12 pbug
2114 f1e3cfca 2010-03-12 pbug sel = select(maxso + 1, &rset, NULL, NULL, &tv);
2115 f1e3cfca 2010-03-12 pbug
2116 f1e3cfca 2010-03-12 pbug if (sel < 0) {
2117 dd869383 2013-02-16 pjp dolog(LOG_INFO, "select: %s\n", strerror(errno));
2118 f1e3cfca 2010-03-12 pbug continue;
2119 f1e3cfca 2010-03-12 pbug }
2120 f1e3cfca 2010-03-12 pbug
2121 f1e3cfca 2010-03-12 pbug if (sel == 0) {
2122 ec3f221d 2014-05-01 pjp #ifdef __linux__
2123 c0963faf 2014-05-01 pjp SLIST_FOREACH(tnp, &tcpshead, tcps_entry) {
2124 ec3f221d 2014-05-01 pjp #else
2125 ec3f221d 2014-05-01 pjp SLIST_FOREACH_SAFE(tnp, &tcpshead, tcps_entry, tntmp) {
2126 ec3f221d 2014-05-01 pjp #endif
2127 f1e3cfca 2010-03-12 pbug if ((tnp->time + 10) < time(NULL)) {
2128 f1e3cfca 2010-03-12 pbug free(tnp->input);
2129 f1e3cfca 2010-03-12 pbug free(tnp->ident);
2130 f1e3cfca 2010-03-12 pbug free(tnp->address);
2131 f1e3cfca 2010-03-12 pbug close(tnp->so);
2132 c0963faf 2014-05-01 pjp SLIST_REMOVE(&tcpshead, tnp, tcps, tcps_entry);
2133 f1e3cfca 2010-03-12 pbug free(tnp);
2134 f1e3cfca 2010-03-12 pbug }
2135 f1e3cfca 2010-03-12 pbug }
2136 f1e3cfca 2010-03-12 pbug continue;
2137 f1e3cfca 2010-03-12 pbug }
2138 f1e3cfca 2010-03-12 pbug
2139 c25e4088 2011-06-28 pbug for (i = 0; i < cfg->sockcount; i++) {
2140 c25e4088 2011-06-28 pbug if (FD_ISSET(cfg->tcp[i], &rset)) {
2141 f1e3cfca 2010-03-12 pbug fromlen = sizeof(sockaddr_large);
2142 f1e3cfca 2010-03-12 pbug
2143 c25e4088 2011-06-28 pbug so = accept(cfg->tcp[i], (struct sockaddr*)from, &fromlen);
2144 f1e3cfca 2010-03-12 pbug
2145 f1e3cfca 2010-03-12 pbug if (so < 0) {
2146 dd869383 2013-02-16 pjp dolog(LOG_INFO, "tcp accept: %s\n", strerror(errno));
2147 f1e3cfca 2010-03-12 pbug continue;
2148 f1e3cfca 2010-03-12 pbug }
2149 f1e3cfca 2010-03-12 pbug
2150 f1e3cfca 2010-03-12 pbug if (from->sa_family == AF_INET6) {
2151 f1e3cfca 2010-03-12 pbug is_ipv6 = 1;
2152 f1e3cfca 2010-03-12 pbug
2153 f1e3cfca 2010-03-12 pbug fromlen = sizeof(struct sockaddr_in6);
2154 f1e3cfca 2010-03-12 pbug sin6 = (struct sockaddr_in6 *)from;
2155 f1e3cfca 2010-03-12 pbug inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, (char *)&address, sizeof(address));
2156 10133795 2010-03-27 pbug aregion = find_region((struct sockaddr_storage *)sin6, AF_INET6);
2157 959d1769 2010-04-05 pbug wildcard = find_wildcard((struct sockaddr_storage *)sin6, AF_INET6);
2158 6f8d6a57 2014-05-18 pjp filter = find_filter((struct sockaddr_storage *)sin6, AF_INET6);
2159 6f8d6a57 2014-05-18 pjp if (whitelist) {
2160 6f8d6a57 2014-05-18 pjp blacklist = find_whitelist((struct sockaddr_storage *)sin6, AF_INET6);
2161 6f8d6a57 2014-05-18 pjp }
2162 f1e3cfca 2010-03-12 pbug } else if (from->sa_family == AF_INET) {
2163 f1e3cfca 2010-03-12 pbug is_ipv6 = 0;
2164 f1e3cfca 2010-03-12 pbug
2165 f1e3cfca 2010-03-12 pbug fromlen = sizeof(struct sockaddr_in);
2166 f1e3cfca 2010-03-12 pbug sin = (struct sockaddr_in *)from;
2167 f1e3cfca 2010-03-12 pbug inet_ntop(AF_INET, (void *)&sin->sin_addr, (char *)&address, sizeof(address));
2168 959d1769 2010-04-05 pbug wildcard = find_wildcard((struct sockaddr_storage *)sin, AF_INET);
2169 10133795 2010-03-27 pbug aregion = find_region((struct sockaddr_storage *)sin, AF_INET);
2170 c0963faf 2014-05-01 pjp filter = find_filter((struct sockaddr_storage *)sin, AF_INET);
2171 6f8d6a57 2014-05-18 pjp if (whitelist) {
2172 6f8d6a57 2014-05-18 pjp blacklist = find_whitelist((struct sockaddr_storage *)sin, AF_INET);
2173 6f8d6a57 2014-05-18 pjp }
2174 f1e3cfca 2010-03-12 pbug } else {
2175 dd869383 2013-02-16 pjp dolog(LOG_INFO, "TCP packet received on descriptor %u interface \"%s\" had weird address family (%u), drop\n", so, cfg->ident[i], from->sa_family);
2176 c0963faf 2014-05-01 pjp close(so);
2177 c0963faf 2014-05-01 pjp continue;
2178 c0963faf 2014-05-01 pjp }
2179 c0963faf 2014-05-01 pjp
2180 c0963faf 2014-05-01 pjp
2181 c0963faf 2014-05-01 pjp if (filter) {
2182 c0963faf 2014-05-01 pjp dolog(LOG_INFO, "TCP connection refused on descriptor %u interface \"%s\" from %s, filter policy\n", so, cfg->ident[i], address);
2183 6f8d6a57 2014-05-18 pjp close(so);
2184 6f8d6a57 2014-05-18 pjp continue;
2185 6f8d6a57 2014-05-18 pjp }
2186 6f8d6a57 2014-05-18 pjp
2187 6f8d6a57 2014-05-18 pjp if (whitelist && blacklist == 0) {
2188 6f8d6a57 2014-05-18 pjp dolog(LOG_INFO, "TCP connection refused on descriptor %u interface \"%s\" from %s, whitelist policy\n", so, cfg->ident[i], address);
2189 f1e3cfca 2010-03-12 pbug close(so);
2190 f1e3cfca 2010-03-12 pbug continue;
2191 f1e3cfca 2010-03-12 pbug }
2192 f1e3cfca 2010-03-12 pbug
2193 c0963faf 2014-05-01 pjp
2194 6f8d6a57 2014-05-18 pjp
2195 6f8d6a57 2014-05-18 pjp
2196 e40d4d5e 2010-03-13 pbug /*
2197 e40d4d5e 2010-03-13 pbug * make this socket nonblocking
2198 e40d4d5e 2010-03-13 pbug */
2199 e40d4d5e 2010-03-13 pbug
2200 e40d4d5e 2010-03-13 pbug if ((flag = fcntl(so, F_GETFL)) < 0) {
2201 dd869383 2013-02-16 pjp dolog(LOG_INFO, "fcntl: %s\n", strerror(errno));
2202 e40d4d5e 2010-03-13 pbug }
2203 e40d4d5e 2010-03-13 pbug flag |= O_NONBLOCK;
2204 e40d4d5e 2010-03-13 pbug if (fcntl(so, F_SETFL, flag) < 0) {
2205 dd869383 2013-02-16 pjp dolog(LOG_INFO, "fcntl 2: %s\n", strerror(errno));
2206 e40d4d5e 2010-03-13 pbug }
2207 e40d4d5e 2010-03-13 pbug
2208 e40d4d5e 2010-03-13 pbug
2209 f1e3cfca 2010-03-12 pbug /* fill the tcps struct */
2210 f1e3cfca 2010-03-12 pbug
2211 f1e3cfca 2010-03-12 pbug tn1 = malloc(sizeof(struct tcps));
2212 f1e3cfca 2010-03-12 pbug if (tn1 == NULL) {
2213 dd869383 2013-02-16 pjp dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
2214 f1e3cfca 2010-03-12 pbug close(so);
2215 f1e3cfca 2010-03-12 pbug continue;
2216 f1e3cfca 2010-03-12 pbug }
2217 f1e3cfca 2010-03-12 pbug
2218 f1e3cfca 2010-03-12 pbug tn1->input = (char *)malloc(0xffff + 2);
2219 f1e3cfca 2010-03-12 pbug if (tn1->input == NULL) {
2220 dd869383 2013-02-16 pjp dolog(LOG_INFO, "tcp malloc 2: %s\n", strerror(errno));
2221 f1e3cfca 2010-03-12 pbug close(so);
2222 f1e3cfca 2010-03-12 pbug continue;
2223 f1e3cfca 2010-03-12 pbug }
2224 f1e3cfca 2010-03-12 pbug
2225 f1e3cfca 2010-03-12 pbug tn1->offset = 0;
2226 f1e3cfca 2010-03-12 pbug tn1->length = 0;
2227 f1e3cfca 2010-03-12 pbug tn1->maxlen = 0xffff + 2;
2228 f1e3cfca 2010-03-12 pbug tn1->so = so;
2229 f1e3cfca 2010-03-12 pbug tn1->isv6 = is_ipv6;
2230 c25e4088 2011-06-28 pbug tn1->ident = strdup(cfg->ident[i]);
2231 f1e3cfca 2010-03-12 pbug tn1->address = strdup(address);
2232 f1e3cfca 2010-03-12 pbug tn1->region = aregion;
2233 959d1769 2010-04-05 pbug tn1->wildcard = wildcard;
2234 f1e3cfca 2010-03-12 pbug tn1->time = time(NULL);
2235 c37b91b6 2009-03-10 pbug
2236 c0963faf 2014-05-01 pjp SLIST_INSERT_HEAD(&tcpshead, tn1, tcps_entry);
2237 f1e3cfca 2010-03-12 pbug
2238 f1e3cfca 2010-03-12 pbug } /* FD_ISSET(); */
2239 f1e3cfca 2010-03-12 pbug } /* if sockcount */
2240 f1e3cfca 2010-03-12 pbug
2241 ec3f221d 2014-05-01 pjp #ifdef __linux__
2242 c0963faf 2014-05-01 pjp SLIST_FOREACH(tnp, &tcpshead, tcps_entry) {
2243 ec3f221d 2014-05-01 pjp #else
2244 ec3f221d 2014-05-01 pjp SLIST_FOREACH_SAFE(tnp, &tcpshead, tcps_entry, tntmp) {
2245 ec3f221d 2014-05-01 pjp #endif
2246 f1e3cfca 2010-03-12 pbug if (FD_ISSET(tnp->so, &rset)) {
2247 f1e3cfca 2010-03-12 pbug
2248 18fa5f8a 2010-03-14 pbug istcp = 1;
2249 f1e3cfca 2010-03-12 pbug len = recv(tnp->so, tnp->input + tnp->offset, tnp->maxlen - tnp->offset, 0);
2250 f1e3cfca 2010-03-12 pbug if (len < 0) {
2251 f1e3cfca 2010-03-12 pbug if (errno == EWOULDBLOCK)
2252 f1e3cfca 2010-03-12 pbug continue;
2253 f1e3cfca 2010-03-12 pbug else {
2254 f1e3cfca 2010-03-12 pbug free(tnp->input);
2255 f1e3cfca 2010-03-12 pbug free(tnp->ident);
2256 f1e3cfca 2010-03-12 pbug free(tnp->address);
2257 f1e3cfca 2010-03-12 pbug close(tnp->so);
2258 c0963faf 2014-05-01 pjp SLIST_REMOVE(&tcpshead, tnp, tcps, tcps_entry);
2259 f1e3cfca 2010-03-12 pbug free(tnp);
2260 f1e3cfca 2010-03-12 pbug continue;
2261 f1e3cfca 2010-03-12 pbug }
2262 f1e3cfca 2010-03-12 pbug } /* if len */
2263 f1e3cfca 2010-03-12 pbug
2264 f1e3cfca 2010-03-12 pbug if (len == 0) {
2265 f1e3cfca 2010-03-12 pbug free(tnp->input);
2266 f1e3cfca 2010-03-12 pbug free(tnp->ident);
2267 f1e3cfca 2010-03-12 pbug free(tnp->address);
2268 f1e3cfca 2010-03-12 pbug close(tnp->so);
2269 c0963faf 2014-05-01 pjp SLIST_REMOVE(&tcpshead, tnp, tcps, tcps_entry);
2270 f1e3cfca 2010-03-12 pbug free(tnp);
2271 f1e3cfca 2010-03-12 pbug continue;
2272 f1e3cfca 2010-03-12 pbug }
2273 f1e3cfca 2010-03-12 pbug
2274 f1e3cfca 2010-03-12 pbug tnp->offset += len;
2275 f1e3cfca 2010-03-12 pbug tnp->time = time(NULL);
2276 f1e3cfca 2010-03-12 pbug
2277 f1e3cfca 2010-03-12 pbug if (tnp->offset >= 2) {
2278 f1e3cfca 2010-03-12 pbug tnp->length = ntohs(*((u_int16_t *) tnp->input));
2279 f1e3cfca 2010-03-12 pbug }
2280 f1e3cfca 2010-03-12 pbug
2281 f1e3cfca 2010-03-12 pbug /*
2282 f1e3cfca 2010-03-12 pbug * only go on if the full packet was written
2283 f1e3cfca 2010-03-12 pbug */
2284 f1e3cfca 2010-03-12 pbug
2285 f1e3cfca 2010-03-12 pbug if (tnp->length + 2 != tnp->offset)
2286 f1e3cfca 2010-03-12 pbug continue;
2287 f1e3cfca 2010-03-12 pbug
2288 f1e3cfca 2010-03-12 pbug len = tnp->length;
2289 f1e3cfca 2010-03-12 pbug pbuf = tnp->input + 2;
2290 f1e3cfca 2010-03-12 pbug
2291 f1e3cfca 2010-03-12 pbug /* if UDP packet check length for minimum / maximum */
2292 f1e3cfca 2010-03-12 pbug if (len > DNS_MAXUDP || len < sizeof(struct dns_header)){
2293 dd869383 2013-02-16 pjp dolog(LOG_INFO, "TCP packet on descriptor %u interface \"%s\" illegal dns packet length from %s, drop\n", tnp->so, tnp->ident, tnp->address);
2294 f1e3cfca 2010-03-12 pbug goto drop;
2295 f1e3cfca 2010-03-12 pbug }
2296 f1e3cfca 2010-03-12 pbug
2297 f1e3cfca 2010-03-12 pbug dh = (struct dns_header *)&pbuf[0];
2298 f1e3cfca 2010-03-12 pbug
2299 f1e3cfca 2010-03-12 pbug /* check if we're a question or reply, drop replies */
2300 f1e3cfca 2010-03-12 pbug if ((ntohs(dh->query) & DNS_REPLY)) {
2301 dd869383 2013-02-16 pjp dolog(LOG_INFO, "TCP packet on descriptor %u interface \"%s\" dns header from %s is not a question, drop\n", tnp->so, tnp->ident, tnp->address);
2302 f1e3cfca 2010-03-12 pbug goto drop;
2303 f1e3cfca 2010-03-12 pbug }
2304 f1e3cfca 2010-03-12 pbug
2305 f1e3cfca 2010-03-12 pbug /*
2306 f1e3cfca 2010-03-12 pbug * if questions aren't exactly 1 then drop
2307 f1e3cfca 2010-03-12 pbug */
2308 f1e3cfca 2010-03-12 pbug
2309 f1e3cfca 2010-03-12 pbug if (ntohs(dh->question) != 1) {
2310 dd869383 2013-02-16 pjp dolog(LOG_INFO, "TCP packet on descriptor %u interface \"%s\" header from %s has no question, drop\n", tnp->so, tnp->ident, tnp->address);
2311 f1e3cfca 2010-03-12 pbug
2312 f1e3cfca 2010-03-12 pbug /* format error */
2313 e87eb338 2010-08-27 pbug build_reply( &sreply, tnp->so, pbuf, len, NULL,
2314 959d1769 2010-04-05 pbug from, fromlen, NULL, NULL, tnp->region,
2315 df34d218 2014-04-21 pjp istcp, tnp->wildcard, NULL, replybuf);
2316 959d1769 2010-04-05 pbug
2317 a2b189d4 2014-10-08 pjp slen = reply_fmterror(&sreply);
2318 dd869383 2013-02-16 pjp dolog(LOG_INFO, "TCP question on descriptor %d interface \"%s\" from %s, did not have question of 1 replying format error\n", tnp->so, tnp->ident, tnp->address);
2319 f1e3cfca 2010-03-12 pbug goto drop;
2320 f1e3cfca 2010-03-12 pbug }
2321 f1e3cfca 2010-03-12 pbug
2322 f1e3cfca 2010-03-12 pbug
2323 d64396b7 2014-05-09 pjp if ((question = build_question(pbuf, len, 0)) == NULL) {
2324 dd869383 2013-02-16 pjp dolog(LOG_INFO, "TCP packet on descriptor %u interface \"%s\" malformed question from %s, drop\n", tnp->so, tnp->ident, tnp->address);
2325 f1e3cfca 2010-03-12 pbug goto drop;
2326 f1e3cfca 2010-03-12 pbug }
2327 f1e3cfca 2010-03-12 pbug
2328 f1e3cfca 2010-03-12 pbug /* goto drop beyond this point should goto out instead */
2329 f1e3cfca 2010-03-12 pbug fakequestion = NULL;
2330 f1e3cfca 2010-03-12 pbug
2331 c25e4088 2011-06-28 pbug if ((type0 = lookup_zone(cfg->db, question, &sd0, &lzerrno, (char *)&replystring, wildcard)) < 0) {
2332 4cc84fc5 2010-03-19 pbug
2333 f1e3cfca 2010-03-12 pbug switch (lzerrno) {
2334 f1e3cfca 2010-03-12 pbug default:
2335 dd869383 2013-02-16 pjp dolog(LOG_INFO, "invalid lzerrno! dropping\n");
2336 f1e3cfca 2010-03-12 pbug /* FALLTHROUGH */
2337 f1e3cfca 2010-03-12 pbug case ERR_DROP:
2338 f1e3cfca 2010-03-12 pbug snprintf(replystring, DNS_MAXNAME, "DROP");
2339 18fa5f8a 2010-03-14 pbug goto tcpout;
2340 f1e3cfca 2010-03-12 pbug
2341 f1e3cfca 2010-03-12 pbug case ERR_NXDOMAIN:
2342 18fa5f8a 2010-03-14 pbug goto tcpnxdomain;
2343 f1e3cfca 2010-03-12 pbug case ERR_NOERROR:
2344 f1e3cfca 2010-03-12 pbug /*
2345 f1e3cfca 2010-03-12 pbug * this is hackish not sure if this should be here
2346 f1e3cfca 2010-03-12 pbug */
2347 f1e3cfca 2010-03-12 pbug
2348 f1e3cfca 2010-03-12 pbug snprintf(replystring, DNS_MAXNAME, "NOERROR");
2349 f1e3cfca 2010-03-12 pbug
2350 f1e3cfca 2010-03-12 pbug /*
2351 f1e3cfca 2010-03-12 pbug * lookup an authoritative soa
2352 f1e3cfca 2010-03-12 pbug */
2353 f1e3cfca 2010-03-12 pbug
2354 f1e3cfca 2010-03-12 pbug memset(&sd0, 0, sizeof(sd0));
2355 c25e4088 2011-06-28 pbug (void)get_soa(cfg->db, question, &sd0, wildcard);
2356 f1e3cfca 2010-03-12 pbug
2357 e87eb338 2010-08-27 pbug build_reply( &sreply, tnp->so, pbuf, len,
2358 959d1769 2010-04-05 pbug question, from, fromlen,
2359 959d1769 2010-04-05 pbug &sd0, NULL, tnp->region, istcp,
2360 df34d218 2014-04-21 pjp tnp->wildcard, NULL, replybuf);
2361 f1e3cfca 2010-03-12 pbug
2362 a2b189d4 2014-10-08 pjp slen = reply_noerror(&sreply);
2363 18fa5f8a 2010-03-14 pbug goto tcpout;
2364 f1e3cfca 2010-03-12 pbug
2365 f1e3cfca 2010-03-12 pbug }
2366 f1e3cfca 2010-03-12 pbug }
2367 f1e3cfca 2010-03-12 pbug
2368 f1e3cfca 2010-03-12 pbug switch (type0) {
2369 f1e3cfca 2010-03-12 pbug case 0:
2370 f1e3cfca 2010-03-12 pbug /*
2371 f1e3cfca 2010-03-12 pbug * lookup_zone could not find an RR for the
2372 f1e3cfca 2010-03-12 pbug * question at all -> nxdomain
2373 f1e3cfca 2010-03-12 pbug */
2374 18fa5f8a 2010-03-14 pbug tcpnxdomain:
2375 f1e3cfca 2010-03-12 pbug snprintf(replystring, DNS_MAXNAME, "NXDOMAIN");
2376 f1e3cfca 2010-03-12 pbug
2377 f1e3cfca 2010-03-12 pbug /*
2378 f1e3cfca 2010-03-12 pbug * lookup an authoritative soa
2379 f1e3cfca 2010-03-12 pbug */
2380 f1e3cfca 2010-03-12 pbug
2381 f1e3cfca 2010-03-12 pbug memset(&sd0, 0, sizeof(sd0));
2382 c25e4088 2011-06-28 pbug (void)get_soa(cfg->db, question, &sd0, wildcard);
2383 f1e3cfca 2010-03-12 pbug
2384 e87eb338 2010-08-27 pbug build_reply( &sreply, tnp->so, pbuf, len, question,
2385 959d1769 2010-04-05 pbug from, fromlen, &sd0, NULL,
2386 df34d218 2014-04-21 pjp tnp->region, istcp, tnp->wildcard, NULL,
2387 df34d218 2014-04-21 pjp replybuf);
2388 959d1769 2010-04-05 pbug
2389 a2b189d4 2014-10-08 pjp slen = reply_nxdomain(&sreply);
2390 18fa5f8a 2010-03-14 pbug goto tcpout;
2391 f1e3cfca 2010-03-12 pbug case DNS_TYPE_CNAME:
2392 f1e3cfca 2010-03-12 pbug fakequestion = build_fake_question(sd0.cname, sd0.cnamelen, question->hdr->qtype);
2393 f1e3cfca 2010-03-12 pbug if (fakequestion == NULL) {
2394 dd869383 2013-02-16 pjp dolog(LOG_INFO, "fakequestion failed\n");
2395 f1e3cfca 2010-03-12 pbug break;
2396 f1e3cfca 2010-03-12 pbug }
2397 f1e3cfca 2010-03-12 pbug
2398 c25e4088 2011-06-28 pbug type1 = lookup_zone(cfg->db, fakequestion, &sd1, &lzerrno, (char *)&fakereplystring, wildcard);
2399 f1e3cfca 2010-03-12 pbug /* break CNAMES pointing to CNAMES */
2400 f1e3cfca 2010-03-12 pbug if (type1 == DNS_TYPE_CNAME)
2401 f1e3cfca 2010-03-12 pbug type1 = 0;
2402 f1e3cfca 2010-03-12 pbug
2403 f1e3cfca 2010-03-12 pbug break;
2404 f1e3cfca 2010-03-12 pbug default:
2405 f1e3cfca 2010-03-12 pbug
2406 f1e3cfca 2010-03-12 pbug break;
2407 f1e3cfca 2010-03-12 pbug }
2408 f1e3cfca 2010-03-12 pbug
2409 f1e3cfca 2010-03-12 pbug /*
2410 f1e3cfca 2010-03-12 pbug * Allow CLASS IN, CHAOS and others are
2411 f1e3cfca 2010-03-12 pbug * not implemented and so we build a reply for
2412 f1e3cfca 2010-03-12 pbug * that and go out.
2413 f1e3cfca 2010-03-12 pbug */
2414 f1e3cfca 2010-03-12 pbug
2415 f1e3cfca 2010-03-12 pbug switch (ntohs(question->hdr->qclass)) {
2416 f1e3cfca 2010-03-12 pbug case DNS_CLASS_IN:
2417 f1e3cfca 2010-03-12 pbug break;
2418 f1e3cfca 2010-03-12 pbug default:
2419 e87eb338 2010-08-27 pbug build_reply( &sreply, tnp->so, pbuf, len, question,
2420 959d1769 2010-04-05 pbug from, fromlen, NULL, NULL, tnp->region,
2421 df34d218 2014-04-21 pjp istcp, tnp->wildcard, NULL, replybuf);
2422 959d1769 2010-04-05 pbug
2423 a2b189d4 2014-10-08 pjp slen = reply_notimpl(&sreply);
2424 f1e3cfca 2010-03-12 pbug snprintf(replystring, DNS_MAXNAME, "NOTIMPL");
2425 18fa5f8a 2010-03-14 pbug goto tcpout;
2426 f1e3cfca 2010-03-12 pbug }
2427 f1e3cfca 2010-03-12 pbug
2428 f1e3cfca 2010-03-12 pbug switch (ntohs(question->hdr->qtype)) {
2429 f1e3cfca 2010-03-12 pbug case DNS_TYPE_A:
2430 f1e3cfca 2010-03-12 pbug if (type0 == DNS_TYPE_CNAME) {
2431 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2432 f1e3cfca 2010-03-12 pbug fromlen, &sd0, ((type1 > 0) ? &sd1 : NULL), \
2433 df34d218 2014-04-21 pjp tnp->region, istcp, tnp->wildcard, NULL, replybuf);
2434 a2b189d4 2014-10-08 pjp slen = reply_cname(&sreply);
2435 4cc84fc5 2010-03-19 pbug } else if (type0 == DNS_TYPE_NS) {
2436 959d1769 2010-04-05 pbug
2437 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question,
2438 959d1769 2010-04-05 pbug from, fromlen, &sd0, NULL,
2439 df34d218 2014-04-21 pjp tnp->region, istcp, tnp->wildcard, NULL,
2440 df34d218 2014-04-21 pjp replybuf);
2441 959d1769 2010-04-05 pbug
2442 a2b189d4 2014-10-08 pjp slen = reply_ns(&sreply, cfg->db);
2443 4cc84fc5 2010-03-19 pbug break;
2444 f1e3cfca 2010-03-12 pbug } else if (type0 == DNS_TYPE_A) {
2445 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2446 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, tnp->region, istcp, tnp->wildcard,
2447 df34d218 2014-04-21 pjp NULL, replybuf);
2448 a2b189d4 2014-10-08 pjp slen = reply_a(&sreply, cfg->db);
2449 f1e3cfca 2010-03-12 pbug break; /* must break here */
2450 f1e3cfca 2010-03-12 pbug }
2451 f1e3cfca 2010-03-12 pbug
2452 f1e3cfca 2010-03-12 pbug break;
2453 0d95ac68 2010-12-27 pbug
2454 0d95ac68 2010-12-27 pbug case DNS_TYPE_ANY:
2455 0d95ac68 2010-12-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2456 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, tnp->region, istcp, tnp->wildcard,
2457 df34d218 2014-04-21 pjp NULL, replybuf);
2458 df34d218 2014-04-21 pjp
2459 a2b189d4 2014-10-08 pjp slen = reply_any(&sreply);
2460 0d95ac68 2010-12-27 pbug break; /* must break here */
2461 0d95ac68 2010-12-27 pbug
2462 f1e3cfca 2010-03-12 pbug case DNS_TYPE_AAAA:
2463 f1e3cfca 2010-03-12 pbug
2464 f1e3cfca 2010-03-12 pbug if (type0 == DNS_TYPE_CNAME) {
2465 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2466 f1e3cfca 2010-03-12 pbug fromlen, &sd0, ((type1 > 0) ? &sd1 : NULL), \
2467 df34d218 2014-04-21 pjp tnp->region, istcp, tnp->wildcard, NULL, replybuf);
2468 a2b189d4 2014-10-08 pjp slen = reply_cname(&sreply);
2469 4cc84fc5 2010-03-19 pbug } else if (type0 == DNS_TYPE_NS) {
2470 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2471 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, tnp->region, istcp,
2472 df34d218 2014-04-21 pjp tnp->wildcard, NULL, replybuf);
2473 df34d218 2014-04-21 pjp
2474 a2b189d4 2014-10-08 pjp slen = reply_ns(&sreply, cfg->db);
2475 4cc84fc5 2010-03-19 pbug break;
2476 f1e3cfca 2010-03-12 pbug } else if (type0 == DNS_TYPE_AAAA) {
2477 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from,
2478 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, tnp->region, istcp,
2479 df34d218 2014-04-21 pjp tnp->wildcard, NULL, replybuf);
2480 df34d218 2014-04-21 pjp
2481 a2b189d4 2014-10-08 pjp slen = reply_aaaa(&sreply, cfg->db);
2482 f1e3cfca 2010-03-12 pbug break; /* must break here */
2483 f1e3cfca 2010-03-12 pbug }
2484 f1e3cfca 2010-03-12 pbug
2485 f1e3cfca 2010-03-12 pbug break;
2486 f1e3cfca 2010-03-12 pbug case DNS_TYPE_MX:
2487 f1e3cfca 2010-03-12 pbug
2488 f1e3cfca 2010-03-12 pbug if (type0 == DNS_TYPE_CNAME) {
2489 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2490 f1e3cfca 2010-03-12 pbug fromlen, &sd0, ((type1 > 0) ? &sd1 : NULL), \
2491 df34d218 2014-04-21 pjp tnp->region, istcp, tnp->wildcard, NULL, replybuf);
2492 df34d218 2014-04-21 pjp
2493 a2b189d4 2014-10-08 pjp slen = reply_cname(&sreply);
2494 df34d218 2014-04-21 pjp
2495 4cc84fc5 2010-03-19 pbug } else if (type0 == DNS_TYPE_NS) {
2496 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2497 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, tnp->region, istcp,
2498 df34d218 2014-04-21 pjp tnp->wildcard, NULL, replybuf);
2499 df34d218 2014-04-21 pjp
2500 a2b189d4 2014-10-08 pjp slen = reply_ns(&sreply, cfg->db);
2501 df34d218 2014-04-21 pjp
2502 4cc84fc5 2010-03-19 pbug break;
2503 f1e3cfca 2010-03-12 pbug } else if (type0 == DNS_TYPE_MX) {
2504 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2505 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, tnp->region, istcp,
2506 df34d218 2014-04-21 pjp tnp->wildcard, NULL, replybuf);
2507 df34d218 2014-04-21 pjp
2508 a2b189d4 2014-10-08 pjp slen = reply_mx(&sreply, cfg->db);
2509 f1e3cfca 2010-03-12 pbug break; /* must break here */
2510 f1e3cfca 2010-03-12 pbug }
2511 f1e3cfca 2010-03-12 pbug
2512 f1e3cfca 2010-03-12 pbug break;
2513 f1e3cfca 2010-03-12 pbug case DNS_TYPE_SOA:
2514 f1e3cfca 2010-03-12 pbug if (type0 == DNS_TYPE_SOA) {
2515 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2516 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, tnp->region, istcp,
2517 df34d218 2014-04-21 pjp tnp->wildcard, NULL, replybuf);
2518 df34d218 2014-04-21 pjp
2519 a2b189d4 2014-10-08 pjp slen = reply_soa(&sreply);
2520 4cc84fc5 2010-03-19 pbug } else if (type0 == DNS_TYPE_NS) {
2521 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2522 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, tnp->region, istcp,
2523 df34d218 2014-04-21 pjp tnp->wildcard, NULL, replybuf);
2524 df34d218 2014-04-21 pjp
2525 a2b189d4 2014-10-08 pjp slen = reply_ns(&sreply, cfg->db);
2526 4cc84fc5 2010-03-19 pbug break;
2527 f1e3cfca 2010-03-12 pbug }
2528 f1e3cfca 2010-03-12 pbug break;
2529 f1e3cfca 2010-03-12 pbug case DNS_TYPE_NS:
2530 f1e3cfca 2010-03-12 pbug if (type0 == DNS_TYPE_NS) {
2531 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2532 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, tnp->region, istcp,
2533 df34d218 2014-04-21 pjp tnp->wildcard, NULL, replybuf);
2534 df34d218 2014-04-21 pjp
2535 a2b189d4 2014-10-08 pjp slen = reply_ns(&sreply, cfg->db);
2536 6f8190d3 2012-04-30 pbug }
2537 6f8190d3 2012-04-30 pbug break;
2538 6f8190d3 2012-04-30 pbug
2539 df34d218 2014-04-21 pjp case DNS_TYPE_SSHFP:
2540 df34d218 2014-04-21 pjp if (type0 == DNS_TYPE_SSHFP) {
2541 df34d218 2014-04-21 pjp build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2542 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, tnp->region, istcp,
2543 df34d218 2014-04-21 pjp tnp->wildcard, NULL, replybuf);
2544 df34d218 2014-04-21 pjp
2545 a2b189d4 2014-10-08 pjp slen = reply_sshfp(&sreply);
2546 df34d218 2014-04-21 pjp }
2547 df34d218 2014-04-21 pjp break;
2548 df34d218 2014-04-21 pjp
2549 df34d218 2014-04-21 pjp
2550 6f8190d3 2012-04-30 pbug case DNS_TYPE_SRV:
2551 6f8190d3 2012-04-30 pbug if (type0 == DNS_TYPE_SRV) {
2552 6f8190d3 2012-04-30 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2553 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, tnp->region, istcp,
2554 df34d218 2014-04-21 pjp tnp->wildcard, NULL, replybuf);
2555 df34d218 2014-04-21 pjp
2556 a2b189d4 2014-10-08 pjp slen = reply_srv(&sreply, cfg->db);
2557 f1e3cfca 2010-03-12 pbug }
2558 f1e3cfca 2010-03-12 pbug break;
2559 f1e3cfca 2010-03-12 pbug
2560 1f1faf13 2014-05-11 pjp case DNS_TYPE_NAPTR:
2561 1f1faf13 2014-05-11 pjp if (type0 == DNS_TYPE_NAPTR) {
2562 1f1faf13 2014-05-11 pjp build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2563 1f1faf13 2014-05-11 pjp fromlen, &sd0, NULL, tnp->region, istcp,
2564 1f1faf13 2014-05-11 pjp tnp->wildcard, NULL, replybuf);
2565 f1e3cfca 2010-03-12 pbug
2566 a2b189d4 2014-10-08 pjp slen = reply_naptr(&sreply, cfg->db);
2567 1f1faf13 2014-05-11 pjp }
2568 1f1faf13 2014-05-11 pjp break;
2569 6f8190d3 2012-04-30 pbug
2570 f1e3cfca 2010-03-12 pbug case DNS_TYPE_CNAME:
2571 f1e3cfca 2010-03-12 pbug if (type0 == DNS_TYPE_CNAME) {
2572 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2573 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, tnp->region, istcp,
2574 df34d218 2014-04-21 pjp tnp->wildcard, NULL, replybuf);
2575 df34d218 2014-04-21 pjp
2576 a2b189d4 2014-10-08 pjp slen = reply_cname(&sreply);
2577 4cc84fc5 2010-03-19 pbug } else if (type0 == DNS_TYPE_NS) {
2578 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2579 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, tnp->region, istcp,
2580 df34d218 2014-04-21 pjp tnp->wildcard, NULL, replybuf);
2581 df34d218 2014-04-21 pjp
2582 a2b189d4 2014-10-08 pjp slen = reply_ns(&sreply, cfg->db);
2583 4cc84fc5 2010-03-19 pbug break;
2584 f1e3cfca 2010-03-12 pbug }
2585 f1e3cfca 2010-03-12 pbug break;
2586 f1e3cfca 2010-03-12 pbug
2587 f1e3cfca 2010-03-12 pbug case DNS_TYPE_PTR:
2588 f1e3cfca 2010-03-12 pbug if (type0 == DNS_TYPE_CNAME) {
2589 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2590 f1e3cfca 2010-03-12 pbug fromlen, &sd0, ((type1 > 0) ? &sd1 : NULL) \
2591 df34d218 2014-04-21 pjp , tnp->region, istcp, tnp->wildcard, NULL,
2592 df34d218 2014-04-21 pjp replybuf);
2593 df34d218 2014-04-21 pjp
2594 a2b189d4 2014-10-08 pjp slen = reply_cname(&sreply);
2595 df34d218 2014-04-21 pjp
2596 4cc84fc5 2010-03-19 pbug } else if (type0 == DNS_TYPE_NS) {
2597 df34d218 2014-04-21 pjp
2598 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2599 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, tnp->region, istcp,
2600 df34d218 2014-04-21 pjp tnp->wildcard, NULL, replybuf);
2601 df34d218 2014-04-21 pjp
2602 a2b189d4 2014-10-08 pjp slen = reply_ns(&sreply, cfg->db);
2603 df34d218 2014-04-21 pjp
2604 4cc84fc5 2010-03-19 pbug break;
2605 f1e3cfca 2010-03-12 pbug } else if (type0 == DNS_TYPE_PTR) {
2606 df34d218 2014-04-21 pjp
2607 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from,
2608 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, tnp->region, istcp,
2609 df34d218 2014-04-21 pjp tnp->wildcard, NULL, replybuf);
2610 df34d218 2014-04-21 pjp
2611 a2b189d4 2014-10-08 pjp slen = reply_ptr(&sreply);
2612 f1e3cfca 2010-03-12 pbug break; /* must break here */
2613 f1e3cfca 2010-03-12 pbug }
2614 f1e3cfca 2010-03-12 pbug break;
2615 f1e3cfca 2010-03-12 pbug
2616 fe42904f 2010-03-27 pbug case DNS_TYPE_TXT:
2617 fe42904f 2010-03-27 pbug if (type0 == DNS_TYPE_TXT) {
2618 df34d218 2014-04-21 pjp
2619 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2620 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, tnp->region, istcp,
2621 df34d218 2014-04-21 pjp tnp->wildcard, NULL, replybuf);
2622 df34d218 2014-04-21 pjp
2623 a2b189d4 2014-10-08 pjp slen = reply_txt(&sreply);
2624 f5989689 2014-04-21 pjp }
2625 f5989689 2014-04-21 pjp break;
2626 f5989689 2014-04-21 pjp
2627 f5989689 2014-04-21 pjp case DNS_TYPE_SPF:
2628 f5989689 2014-04-21 pjp if (type0 == DNS_TYPE_SPF) {
2629 df34d218 2014-04-21 pjp
2630 f5989689 2014-04-21 pjp build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2631 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, tnp->region, istcp,
2632 df34d218 2014-04-21 pjp tnp->wildcard, NULL, replybuf);
2633 df34d218 2014-04-21 pjp
2634 a2b189d4 2014-10-08 pjp slen = reply_spf(&sreply);
2635 fe42904f 2010-03-27 pbug }
2636 fe42904f 2010-03-27 pbug break;
2637 f5989689 2014-04-21 pjp
2638 fe42904f 2010-03-27 pbug
2639 f1e3cfca 2010-03-12 pbug default:
2640 f1e3cfca 2010-03-12 pbug
2641 f1e3cfca 2010-03-12 pbug /*
2642 4cc84fc5 2010-03-19 pbug * ANY unknown RR TYPE gets a NOTIMPL
2643 f1e3cfca 2010-03-12 pbug */
2644 f1e3cfca 2010-03-12 pbug
2645 4cc84fc5 2010-03-19 pbug /*
2646 4cc84fc5 2010-03-19 pbug * except for delegations
2647 4cc84fc5 2010-03-19 pbug */
2648 4cc84fc5 2010-03-19 pbug
2649 4cc84fc5 2010-03-19 pbug if (type0 == DNS_TYPE_NS) {
2650 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2651 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, aregion, istcp,
2652 df34d218 2014-04-21 pjp tnp->wildcard, NULL, replybuf);
2653 df34d218 2014-04-21 pjp
2654 a2b189d4 2014-10-08 pjp slen = reply_ns(&sreply, cfg->db);
2655 df34d218 2014-04-21 pjp
2656 4cc84fc5 2010-03-19 pbug } else {
2657 4cc84fc5 2010-03-19 pbug
2658 e87eb338 2010-08-27 pbug build_reply(&sreply, tnp->so, pbuf, len, question, from, \
2659 df34d218 2014-04-21 pjp fromlen, NULL, NULL, tnp->region, istcp,
2660 df34d218 2014-04-21 pjp tnp->wildcard, NULL, replybuf);
2661 4cc84fc5 2010-03-19 pbug
2662 a2b189d4 2014-10-08 pjp slen = reply_notimpl(&sreply);
2663 4cc84fc5 2010-03-19 pbug snprintf(replystring, DNS_MAXNAME, "NOTIMPL");
2664 4cc84fc5 2010-03-19 pbug }
2665 f1e3cfca 2010-03-12 pbug break;
2666 f1e3cfca 2010-03-12 pbug }
2667 f1e3cfca 2010-03-12 pbug
2668 18fa5f8a 2010-03-14 pbug tcpout:
2669 f1e3cfca 2010-03-12 pbug if (lflag)
2670 f3d5c2ab 2014-10-08 pjp dolog(LOG_INFO, "request on descriptor %u interface \"%s\" from %s (ttl=TCP, region=%d) for \"%s\" type=%s class=%u, answering \"%s\" (%d/%d)\n", tnp->so, tnp->ident, tnp->address, tnp->region, question->converted_name, get_dns_type(ntohs(question->hdr->qtype)), ntohs(question->hdr->qclass), replystring, len, slen);
2671 f1e3cfca 2010-03-12 pbug
2672 f1e3cfca 2010-03-12 pbug
2673 f1e3cfca 2010-03-12 pbug if (fakequestion != NULL) {
2674 f1e3cfca 2010-03-12 pbug free_question(fakequestion);
2675 f1e3cfca 2010-03-12 pbug }
2676 f1e3cfca 2010-03-12 pbug
2677 f1e3cfca 2010-03-12 pbug free_question(question);
2678 f1e3cfca 2010-03-12 pbug
2679 f1e3cfca 2010-03-12 pbug } /* END ISSET */
2680 f1e3cfca 2010-03-12 pbug
2681 62fd6633 2010-03-13 pbug memset(tnp->input, 0, tnp->maxlen);
2682 62fd6633 2010-03-13 pbug tnp->offset = 0;
2683 f1e3cfca 2010-03-12 pbug
2684 62fd6633 2010-03-13 pbug } /* SLIST_FOREACH */
2685 18fa5f8a 2010-03-14 pbug
2686 18fa5f8a 2010-03-14 pbug /* UDP marriage */
2687 c25e4088 2011-06-28 pbug for (i = 0; i < cfg->sockcount; i++) {
2688 f98bb34d 2011-09-19 pbug if (axfrport && FD_ISSET(cfg->axfr[i], &rset)) {
2689 f98bb34d 2011-09-19 pbug istcp = 0;
2690 f98bb34d 2011-09-19 pbug so = cfg->axfr[i];
2691 f98bb34d 2011-09-19 pbug
2692 f98bb34d 2011-09-19 pbug goto axfrentry;
2693 f98bb34d 2011-09-19 pbug }
2694 f98bb34d 2011-09-19 pbug
2695 c25e4088 2011-06-28 pbug if (FD_ISSET(cfg->udp[i], &rset)) {
2696 18fa5f8a 2010-03-14 pbug istcp = 0;
2697 c25e4088 2011-06-28 pbug so = cfg->udp[i];
2698 f98bb34d 2011-09-19 pbug axfrentry:
2699 18fa5f8a 2010-03-14 pbug fromlen = sizeof(sockaddr_large);
2700 18fa5f8a 2010-03-14 pbug
2701 18fa5f8a 2010-03-14 pbug memset(&msgh, 0, sizeof(msgh));
2702 18fa5f8a 2010-03-14 pbug iov.iov_base = buf;
2703 18fa5f8a 2010-03-14 pbug iov.iov_len = sizeof(buf);
2704 18fa5f8a 2010-03-14 pbug msgh.msg_name = from;
2705 18fa5f8a 2010-03-14 pbug msgh.msg_namelen = fromlen;
2706 18fa5f8a 2010-03-14 pbug msgh.msg_iov = &iov;
2707 18fa5f8a 2010-03-14 pbug msgh.msg_iovlen = 1;
2708 18fa5f8a 2010-03-14 pbug msgh.msg_control = (struct cmsghdr*)&controlbuf;
2709 18fa5f8a 2010-03-14 pbug msgh.msg_controllen = sizeof(controlbuf);
2710 18fa5f8a 2010-03-14 pbug
2711 18fa5f8a 2010-03-14 pbug len = recvmsg(so, &msgh, 0);
2712 18fa5f8a 2010-03-14 pbug if (len < 0) {
2713 dd869383 2013-02-16 pjp dolog(LOG_INFO, "recvmsg: on descriptor %u interface \"%s\" %s\n", so, cfg->ident[i], strerror(errno));
2714 18fa5f8a 2010-03-14 pbug continue;
2715 18fa5f8a 2010-03-14 pbug }
2716 18fa5f8a 2010-03-14 pbug
2717 18fa5f8a 2010-03-14 pbug received_ttl = 0;
2718 18fa5f8a 2010-03-14 pbug
2719 18fa5f8a 2010-03-14 pbug for (cmsg = CMSG_FIRSTHDR(&msgh);
2720 18fa5f8a 2010-03-14 pbug cmsg != NULL;
2721 18fa5f8a 2010-03-14 pbug cmsg = CMSG_NXTHDR(&msgh,cmsg)) {
2722 18fa5f8a 2010-03-14 pbug if (cmsg->cmsg_level == IPPROTO_IP
2723 1e8325ae 2010-03-28 pbug #ifdef __linux__
2724 18fa5f8a 2010-03-14 pbug && cmsg->cmsg_type == IP_TTL) {
2725 9b8955b7 2010-03-28 pbug #elif defined __NetBSD__
2726 1e8325ae 2010-03-28 pbug && cmsg->cmsg_type == IP_TTL) {
2727 1e8325ae 2010-03-28 pbug
2728 18fa5f8a 2010-03-14 pbug #else
2729 1e8325ae 2010-03-28 pbug
2730 18fa5f8a 2010-03-14 pbug && cmsg->cmsg_type == IP_RECVTTL) {
2731 18fa5f8a 2010-03-14 pbug #endif
2732 18fa5f8a 2010-03-14 pbug
2733 bd1eb3c6 2011-02-13 pbug #if defined __FreeBSD__ || defined __OpenBSD__
2734 18fa5f8a 2010-03-14 pbug
2735 18fa5f8a 2010-03-14 pbug ttlptr = (u_char *) CMSG_DATA(cmsg);
2736 18fa5f8a 2010-03-14 pbug received_ttl = (u_int)*ttlptr;
2737 18fa5f8a 2010-03-14 pbug #else
2738 18fa5f8a 2010-03-14 pbug
2739 18fa5f8a 2010-03-14 pbug ttlptr = (int *) CMSG_DATA(cmsg);
2740 18fa5f8a 2010-03-14 pbug received_ttl = (u_int)*ttlptr;
2741 18fa5f8a 2010-03-14 pbug #endif
2742 18fa5f8a 2010-03-14 pbug }
2743 562d28f3 2012-06-11 pbug
2744 562d28f3 2012-06-11 pbug if (cmsg->cmsg_level == IPPROTO_IPV6 &&
2745 562d28f3 2012-06-11 pbug cmsg->cmsg_type == IPV6_HOPLIMIT) {
2746 562d28f3 2012-06-11 pbug
2747 562d28f3 2012-06-11 pbug if (cmsg->cmsg_len !=
2748 562d28f3 2012-06-11 pbug CMSG_LEN(sizeof(int))) {
2749 dd869383 2013-02-16 pjp dolog(LOG_INFO, "cmsg->cmsg_len == %d\n", cmsg->cmsg_len);
2750 562d28f3 2012-06-11 pbug continue;
2751 562d28f3 2012-06-11 pbug }
2752 562d28f3 2012-06-11 pbug
2753 27110135 2014-11-07 pjp #ifdef __NetBSD__
2754 27110135 2014-11-07 pjp ttlptr = (int *) CMSG_DATA(cmsg);
2755 27110135 2014-11-07 pjp #else
2756 562d28f3 2012-06-11 pbug ttlptr = (u_char *) CMSG_DATA(cmsg);
2757 27110135 2014-11-07 pjp #endif
2758 27110135 2014-11-07 pjp
2759 27110135 2014-11-07 pjp
2760 562d28f3 2012-06-11 pbug received_ttl = (u_int)*ttlptr;
2761 562d28f3 2012-06-11 pbug }
2762 18fa5f8a 2010-03-14 pbug }
2763 b6dc64dc 2010-04-15 pbug
2764 b6dc64dc 2010-04-15 pbug if (rflag) {
2765 b6dc64dc 2010-04-15 pbug if (getsockname(so, (struct sockaddr*)&sto, &namelen) < 0) {
2766 dd869383 2013-02-16 pjp dolog(LOG_INFO, "getsockname failed: %s\n", strerror(errno));
2767 b6dc64dc 2010-04-15 pbug }
2768 b6dc64dc 2010-04-15 pbug
2769 b6dc64dc 2010-04-15 pbug memset(&rh, 0, sizeof(rh));
2770 b6dc64dc 2010-04-15 pbug }
2771 18fa5f8a 2010-03-14 pbug
2772 18fa5f8a 2010-03-14 pbug if (from->sa_family == AF_INET6) {
2773 18fa5f8a 2010-03-14 pbug is_ipv6 = 1;
2774 18fa5f8a 2010-03-14 pbug
2775 18fa5f8a 2010-03-14 pbug fromlen = sizeof(struct sockaddr_in6);
2776 18fa5f8a 2010-03-14 pbug sin6 = (struct sockaddr_in6 *)from;
2777 18fa5f8a 2010-03-14 pbug inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, (char *)&address, sizeof(address));
2778 4667e76d 2014-05-05 pjp if (ratelimit) {
2779 4667e76d 2014-05-05 pjp add_rrlimit(ratelimit_backlog, (u_int16_t *)&sin6->sin6_addr, sizeof(sin6->sin6_addr), rptr);
2780 4667e76d 2014-05-05 pjp
2781 4667e76d 2014-05-05 pjp rcheck = check_rrlimit(ratelimit_backlog, (u_int16_t *)&sin6->sin6_addr, sizeof(sin6->sin6_addr), rptr);
2782 4667e76d 2014-05-05 pjp }
2783 4667e76d 2014-05-05 pjp
2784 10133795 2010-03-27 pbug aregion = find_region((struct sockaddr_storage *)sin6, AF_INET6);
2785 959d1769 2010-04-05 pbug wildcard = find_wildcard((struct sockaddr_storage *)sin6, AF_INET6);
2786 c0963faf 2014-05-01 pjp filter = find_filter((struct sockaddr_storage *)sin6, AF_INET6);
2787 6f8d6a57 2014-05-18 pjp if (whitelist) {
2788 6f8d6a57 2014-05-18 pjp blacklist = find_whitelist((struct sockaddr_storage *)sin6, AF_INET6);
2789 6f8d6a57 2014-05-18 pjp }
2790 b6dc64dc 2010-04-15 pbug if (rflag) {
2791 b6dc64dc 2010-04-15 pbug recursion = find_recurse((struct sockaddr_storage *)sin6, AF_INET6);
2792 b6dc64dc 2010-04-15 pbug recurseheader(&rh, IPPROTO_UDP, (struct sockaddr_storage*)sin6, &sto, AF_INET6);
2793 b6dc64dc 2010-04-15 pbug }
2794 18fa5f8a 2010-03-14 pbug } else if (from->sa_family == AF_INET) {
2795 18fa5f8a 2010-03-14 pbug is_ipv6 = 0;
2796 18fa5f8a 2010-03-14 pbug
2797 18fa5f8a 2010-03-14 pbug fromlen = sizeof(struct sockaddr_in);
2798 18fa5f8a 2010-03-14 pbug sin = (struct sockaddr_in *)from;
2799 18fa5f8a 2010-03-14 pbug inet_ntop(AF_INET, (void *)&sin->sin_addr, (char *)&address, sizeof(address));
2800 4667e76d 2014-05-05 pjp if (ratelimit) {
2801 4667e76d 2014-05-05 pjp add_rrlimit(ratelimit_backlog, (u_int16_t *)&sin->sin_addr.s_addr, sizeof(sin->sin_addr.s_addr), rptr);
2802 4667e76d 2014-05-05 pjp
2803 4667e76d 2014-05-05 pjp rcheck = check_rrlimit(ratelimit_backlog, (u_int16_t *)&sin->sin_addr.s_addr, sizeof(sin->sin_addr.s_addr), rptr);
2804 4667e76d 2014-05-05 pjp }
2805 4667e76d 2014-05-05 pjp
2806 10133795 2010-03-27 pbug aregion = find_region((struct sockaddr_storage *)sin, AF_INET);
2807 959d1769 2010-04-05 pbug wildcard = find_wildcard((struct sockaddr_storage *)sin, AF_INET);
2808 c0963faf 2014-05-01 pjp filter = find_filter((struct sockaddr_storage *)sin, AF_INET);
2809 6f8d6a57 2014-05-18 pjp if (whitelist) {
2810 6f8d6a57 2014-05-18 pjp blacklist = find_whitelist((struct sockaddr_storage *)sin, AF_INET);
2811 6f8d6a57 2014-05-18 pjp }
2812 6f8d6a57 2014-05-18 pjp
2813 b6dc64dc 2010-04-15 pbug if (rflag) {
2814 b6dc64dc 2010-04-15 pbug recursion = find_recurse((struct sockaddr_storage *)sin, AF_INET);
2815 b6dc64dc 2010-04-15 pbug recurseheader(&rh, IPPROTO_UDP, (struct sockaddr_storage*)sin, &sto, AF_INET);
2816 b6dc64dc 2010-04-15 pbug }
2817 18fa5f8a 2010-03-14 pbug } else {
2818 dd869383 2013-02-16 pjp dolog(LOG_INFO, "packet received on descriptor %u interface \"%s\" had weird address family (%u), drop\n", so, cfg->ident[i], from->sa_family);
2819 18fa5f8a 2010-03-14 pbug goto drop;
2820 18fa5f8a 2010-03-14 pbug }
2821 18fa5f8a 2010-03-14 pbug
2822 18fa5f8a 2010-03-14 pbug /* if UDP packet check length for minimum / maximum */
2823 18fa5f8a 2010-03-14 pbug if (len > DNS_MAXUDP || len < sizeof(struct dns_header)){
2824 dd869383 2013-02-16 pjp dolog(LOG_INFO, "on descriptor %u interface \"%s\" illegal dns packet length from %s, drop\n", so, cfg->ident[i], address);
2825 18fa5f8a 2010-03-14 pbug goto drop;
2826 18fa5f8a 2010-03-14 pbug }
2827 18fa5f8a 2010-03-14 pbug
2828 18fa5f8a 2010-03-14 pbug dh = (struct dns_header *)&buf[0];
2829 18fa5f8a 2010-03-14 pbug
2830 18fa5f8a 2010-03-14 pbug /* check if we're a question or reply, drop replies */
2831 18fa5f8a 2010-03-14 pbug if ((ntohs(dh->query) & DNS_REPLY)) {
2832 dd869383 2013-02-16 pjp dolog(LOG_INFO, "on descriptor %u interface \"%s\" dns header from %s is not a question, drop\n", so, cfg->ident[i], address);
2833 18fa5f8a 2010-03-14 pbug goto drop;
2834 18fa5f8a 2010-03-14 pbug }
2835 18fa5f8a 2010-03-14 pbug
2836 18fa5f8a 2010-03-14 pbug /*
2837 18fa5f8a 2010-03-14 pbug * if questions aren't exactly 1 then drop
2838 18fa5f8a 2010-03-14 pbug */
2839 18fa5f8a 2010-03-14 pbug
2840 18fa5f8a 2010-03-14 pbug if (ntohs(dh->question) != 1) {
2841 dd869383 2013-02-16 pjp dolog(LOG_INFO, "on descriptor %u interface \"%s\" header from %s has no question, drop\n", so, cfg->ident[i], address);
2842 18fa5f8a 2010-03-14 pbug
2843 18fa5f8a 2010-03-14 pbug /* format error */
2844 df34d218 2014-04-21 pjp build_reply(&sreply, so, buf, len, NULL, from, fromlen, NULL, NULL, aregion, istcp, wildcard, NULL, replybuf);
2845 df34d218 2014-04-21 pjp
2846 a2b189d4 2014-10-08 pjp slen = reply_fmterror(&sreply);
2847 dd869383 2013-02-16 pjp dolog(LOG_INFO, "question on descriptor %d interface \"%s\" from %s, did not have question of 1 replying format error\n", so, cfg->ident[i], address);
2848 c0963faf 2014-05-01 pjp goto drop;
2849 c0963faf 2014-05-01 pjp }
2850 c0963faf 2014-05-01 pjp
2851 c0963faf 2014-05-01 pjp if (filter) {
2852 c0963faf 2014-05-01 pjp
2853 c0963faf 2014-05-01 pjp build_reply(&sreply, so, buf, len, NULL, from, fromlen, NULL, NULL, aregion, istcp, wildcard, NULL, replybuf);
2854 a2b189d4 2014-10-08 pjp slen = reply_refused(&sreply);
2855 c0963faf 2014-05-01 pjp
2856 c0963faf 2014-05-01 pjp dolog(LOG_INFO, "UDP connection refused on descriptor %u interface \"%s\" from %s (ttl=%d, region=%d) replying REFUSED, filter policy\n", so, cfg->ident[i], address, received_ttl, aregion);
2857 18fa5f8a 2010-03-14 pbug goto drop;
2858 18fa5f8a 2010-03-14 pbug }
2859 c0963faf 2014-05-01 pjp
2860 6f8d6a57 2014-05-18 pjp if (whitelist && blacklist == 0) {
2861 6f8d6a57 2014-05-18 pjp
2862 6f8d6a57 2014-05-18 pjp build_reply(&sreply, so, buf, len, NULL, from, fromlen, NULL, NULL, aregion, istcp, wildcard, NULL, replybuf);
2863 a2b189d4 2014-10-08 pjp slen = reply_refused(&sreply);
2864 6f8d6a57 2014-05-18 pjp
2865 6f8d6a57 2014-05-18 pjp dolog(LOG_INFO, "UDP connection refused on descriptor %u interface \"%s\" from %s (ttl=%d, region=%d) replying REFUSED, whitelist policy\n", so, cfg->ident[i], address, received_ttl, aregion);
2866 6f8d6a57 2014-05-18 pjp goto drop;
2867 6f8d6a57 2014-05-18 pjp }
2868 6f8d6a57 2014-05-18 pjp
2869 4667e76d 2014-05-05 pjp if (ratelimit && rcheck) {
2870 4667e76d 2014-05-05 pjp dolog(LOG_INFO, "UDP connection refused on descriptor %u interface \"%s\" from %s (ttl=%d, region=%d) ratelimit policy dropping packet\n", so, cfg->ident[i], address, received_ttl, aregion);
2871 4667e76d 2014-05-05 pjp goto drop;
2872 4667e76d 2014-05-05 pjp }
2873 18fa5f8a 2010-03-14 pbug
2874 b6dc64dc 2010-04-15 pbug if (rflag && recursion) {
2875 b6dc64dc 2010-04-15 pbug memcpy(&rh.buf, buf, len);
2876 b6dc64dc 2010-04-15 pbug rh.len = len;
2877 b6dc64dc 2010-04-15 pbug }
2878 18fa5f8a 2010-03-14 pbug
2879 d64396b7 2014-05-09 pjp if ((question = build_question(buf, len, ntohs(dh->additional))) == NULL) {
2880 dd869383 2013-02-16 pjp dolog(LOG_INFO, "on descriptor %u interface \"%s\" malformed question from %s, drop\n", so, cfg->ident[i], address);
2881 18fa5f8a 2010-03-14 pbug goto drop;
2882 18fa5f8a 2010-03-14 pbug }
2883 18fa5f8a 2010-03-14 pbug
2884 18fa5f8a 2010-03-14 pbug /* goto drop beyond this point should goto out instead */
2885 18fa5f8a 2010-03-14 pbug fakequestion = NULL;
2886 18fa5f8a 2010-03-14 pbug
2887 c25e4088 2011-06-28 pbug if ((type0 = lookup_zone(cfg->db, question, &sd0, &lzerrno, (char *)&replystring, wildcard)) < 0) {
2888 18fa5f8a 2010-03-14 pbug switch (lzerrno) {
2889 18fa5f8a 2010-03-14 pbug default:
2890 dd869383 2013-02-16 pjp dolog(LOG_INFO, "invalid lzerrno! dropping\n");
2891 18fa5f8a 2010-03-14 pbug /* FALLTHROUGH */
2892 18fa5f8a 2010-03-14 pbug case ERR_DROP:
2893 18fa5f8a 2010-03-14 pbug snprintf(replystring, DNS_MAXNAME, "DROP");
2894 18fa5f8a 2010-03-14 pbug goto udpout;
2895 18fa5f8a 2010-03-14 pbug
2896 18fa5f8a 2010-03-14 pbug case ERR_NXDOMAIN:
2897 18fa5f8a 2010-03-14 pbug goto udpnxdomain;
2898 18fa5f8a 2010-03-14 pbug case ERR_NOERROR:
2899 b6dc64dc 2010-04-15 pbug if (rflag && recursion) {
2900 b6dc64dc 2010-04-15 pbug snprintf(replystring, DNS_MAXNAME, "RECURSE");
2901 b6dc64dc 2010-04-15 pbug if (send(sp, (char *)&rh, sizeof(rh), 0) < 0) {
2902 dd869383 2013-02-16 pjp dolog(LOG_INFO, "send sp: %s\n", strerror(errno));
2903 b6dc64dc 2010-04-15 pbug }
2904 62fd6633 2010-03-13 pbug
2905 b6dc64dc 2010-04-15 pbug goto udpout;
2906 b6dc64dc 2010-04-15 pbug } else {
2907 b6dc64dc 2010-04-15 pbug /*
2908 b6dc64dc 2010-04-15 pbug * this is hackish not sure if this should be here
2909 b6dc64dc 2010-04-15 pbug */
2910 18fa5f8a 2010-03-14 pbug
2911 b6dc64dc 2010-04-15 pbug snprintf(replystring, DNS_MAXNAME, "NOERROR");
2912 18fa5f8a 2010-03-14 pbug
2913 b6dc64dc 2010-04-15 pbug /*
2914 b6dc64dc 2010-04-15 pbug * lookup an authoritative soa
2915 b6dc64dc 2010-04-15 pbug */
2916 18fa5f8a 2010-03-14 pbug
2917 b6dc64dc 2010-04-15 pbug memset(&sd0, 0, sizeof(sd0));
2918 c25e4088 2011-06-28 pbug (void)get_soa(cfg->db, question, &sd0, wildcard);
2919 18fa5f8a 2010-03-14 pbug
2920 b6dc64dc 2010-04-15 pbug build_reply(&sreply, so, buf, len, question, from, \
2921 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, aregion, istcp, wildcard,
2922 df34d218 2014-04-21 pjp NULL, replybuf);
2923 18fa5f8a 2010-03-14 pbug
2924 a2b189d4 2014-10-08 pjp slen = reply_noerror(&sreply);
2925 b6dc64dc 2010-04-15 pbug goto udpout;
2926 b6dc64dc 2010-04-15 pbug } /* else rflag */
2927 18fa5f8a 2010-03-14 pbug }
2928 18fa5f8a 2010-03-14 pbug }
2929 18fa5f8a 2010-03-14 pbug
2930 18fa5f8a 2010-03-14 pbug switch (type0) {
2931 18fa5f8a 2010-03-14 pbug case 0:
2932 18fa5f8a 2010-03-14 pbug udpnxdomain:
2933 b6dc64dc 2010-04-15 pbug if (rflag && recursion) {
2934 b6dc64dc 2010-04-15 pbug snprintf(replystring, DNS_MAXNAME, "RECURSE");
2935 b6dc64dc 2010-04-15 pbug if (send(sp, (char *)&rh, sizeof(rh), 0) < 0) {
2936 dd869383 2013-02-16 pjp dolog(LOG_INFO, "send sp: %s\n", strerror(errno));
2937 b6dc64dc 2010-04-15 pbug }
2938 18fa5f8a 2010-03-14 pbug
2939 b6dc64dc 2010-04-15 pbug goto udpout;
2940 b6dc64dc 2010-04-15 pbug } else {
2941 b6dc64dc 2010-04-15 pbug
2942 b6dc64dc 2010-04-15 pbug /*
2943 b6dc64dc 2010-04-15 pbug * lookup_zone could not find an RR for the
2944 b6dc64dc 2010-04-15 pbug * question at all -> nxdomain
2945 b6dc64dc 2010-04-15 pbug */
2946 b6dc64dc 2010-04-15 pbug snprintf(replystring, DNS_MAXNAME, "NXDOMAIN");
2947 b6dc64dc 2010-04-15 pbug
2948 b6dc64dc 2010-04-15 pbug /*
2949 b6dc64dc 2010-04-15 pbug * lookup an authoritative soa
2950 b6dc64dc 2010-04-15 pbug */
2951 18fa5f8a 2010-03-14 pbug
2952 b6dc64dc 2010-04-15 pbug memset(&sd0, 0, sizeof(sd0));
2953 c25e4088 2011-06-28 pbug (void)get_soa(cfg->db, question, &sd0, wildcard);
2954 b6dc64dc 2010-04-15 pbug
2955 b6dc64dc 2010-04-15 pbug build_reply(&sreply, so, buf, len, question, from, \
2956 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, aregion, istcp, \
2957 df34d218 2014-04-21 pjp wildcard, NULL, replybuf);
2958 df34d218 2014-04-21 pjp
2959 a2b189d4 2014-10-08 pjp slen = reply_nxdomain(&sreply);
2960 b6dc64dc 2010-04-15 pbug goto udpout;
2961 b6dc64dc 2010-04-15 pbug } /* else rflag */
2962 18fa5f8a 2010-03-14 pbug case DNS_TYPE_CNAME:
2963 18fa5f8a 2010-03-14 pbug fakequestion = build_fake_question(sd0.cname, sd0.cnamelen, question->hdr->qtype);
2964 18fa5f8a 2010-03-14 pbug if (fakequestion == NULL) {
2965 dd869383 2013-02-16 pjp dolog(LOG_INFO, "fakequestion failed\n");
2966 18fa5f8a 2010-03-14 pbug break;
2967 18fa5f8a 2010-03-14 pbug }
2968 18fa5f8a 2010-03-14 pbug
2969 c25e4088 2011-06-28 pbug type1 = lookup_zone(cfg->db, fakequestion, &sd1, &lzerrno, (char *)&fakereplystring, wildcard);
2970 18fa5f8a 2010-03-14 pbug /* break CNAMES pointing to CNAMES */
2971 18fa5f8a 2010-03-14 pbug if (type1 == DNS_TYPE_CNAME)
2972 18fa5f8a 2010-03-14 pbug type1 = 0;
2973 18fa5f8a 2010-03-14 pbug
2974 18fa5f8a 2010-03-14 pbug break;
2975 18fa5f8a 2010-03-14 pbug default:
2976 18fa5f8a 2010-03-14 pbug
2977 18fa5f8a 2010-03-14 pbug break;
2978 18fa5f8a 2010-03-14 pbug }
2979 18fa5f8a 2010-03-14 pbug
2980 18fa5f8a 2010-03-14 pbug /*
2981 18fa5f8a 2010-03-14 pbug * Allow CLASS IN, CHAOS and others are
2982 18fa5f8a 2010-03-14 pbug * not implemented and so we build a reply for
2983 18fa5f8a 2010-03-14 pbug * that and go out.
2984 18fa5f8a 2010-03-14 pbug */
2985 18fa5f8a 2010-03-14 pbug
2986 18fa5f8a 2010-03-14 pbug switch (ntohs(question->hdr->qclass)) {
2987 18fa5f8a 2010-03-14 pbug case DNS_CLASS_IN:
2988 18fa5f8a 2010-03-14 pbug break;
2989 18fa5f8a 2010-03-14 pbug default:
2990 959d1769 2010-04-05 pbug build_reply(&sreply, so, buf, len, question, from, \
2991 df34d218 2014-04-21 pjp fromlen, NULL, NULL, aregion, istcp, wildcard, \
2992 df34d218 2014-04-21 pjp NULL, replybuf);
2993 df34d218 2014-04-21 pjp
2994 a2b189d4 2014-10-08 pjp slen = reply_notimpl(&sreply);
2995 18fa5f8a 2010-03-14 pbug snprintf(replystring, DNS_MAXNAME, "NOTIMPL");
2996 18fa5f8a 2010-03-14 pbug goto udpout;
2997 18fa5f8a 2010-03-14 pbug }
2998 18fa5f8a 2010-03-14 pbug
2999 18fa5f8a 2010-03-14 pbug switch (ntohs(question->hdr->qtype)) {
3000 18fa5f8a 2010-03-14 pbug case DNS_TYPE_A:
3001 18fa5f8a 2010-03-14 pbug if (type0 == DNS_TYPE_CNAME) {
3002 df34d218 2014-04-21 pjp
3003 959d1769 2010-04-05 pbug build_reply(&sreply, so, buf, len, question, from, \
3004 18fa5f8a 2010-03-14 pbug fromlen, &sd0, ((type1 > 0) ? &sd1 : NULL), \
3005 df34d218 2014-04-21 pjp aregion, istcp, wildcard, NULL, replybuf);
3006 df34d218 2014-04-21 pjp
3007 a2b189d4 2014-10-08 pjp slen = reply_cname(&sreply);
3008 4cc84fc5 2010-03-19 pbug } else if (type0 == DNS_TYPE_NS) {
3009 df34d218 2014-04-21 pjp
3010 959d1769 2010-04-05 pbug build_reply(&sreply, so, buf, len, question, from, \
3011 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, aregion, istcp, wildcard, \
3012 df34d218 2014-04-21 pjp NULL, replybuf);
3013 df34d218 2014-04-21 pjp
3014 a2b189d4 2014-10-08 pjp slen = reply_ns(&sreply, cfg->db);
3015 4cc84fc5 2010-03-19 pbug break;
3016 18fa5f8a 2010-03-14 pbug } else if (type0 == DNS_TYPE_A) {
3017 df34d218 2014-04-21 pjp
3018 959d1769 2010-04-05 pbug build_reply(&sreply, so, buf, len, question, from, \
3019 df34d218 2014-04-21 pjp fromlen, &sd0, NULL, aregion, istcp, wildcard,
3020 df34d218 2014-04-21 pjp NULL, replybuf);
3021 df34d218 2014-04-21 pjp
3022 a2b189d4 2014-10-08 pjp slen = reply_a(&sreply, cfg->db);
3023 18fa5f8a 2010-03-14 pbug break; /* must break here */
3024 18fa5f8a 2010-03-14 pbug }
3025 18fa5f8a 2010-03-14 pbug
3026 18fa5f8a 2010-03-14 pbug break;
3027 0d95ac68 2010-12-27 pbug
3028 0d95ac68 2010-12-27 pbug case DNS_TYPE_ANY:
3029