-
Notifications
You must be signed in to change notification settings - Fork 1
/
phreebird.c
executable file
·1414 lines (1103 loc) · 42 KB
/
phreebird.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
/* sockets */
#include <sys/types.h>
#include <sys/socket.h>
/* for non blocking */
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
/* for libevent */
#include <event.h>
/* for ldns */
#include <ldns/ldns.h>
/* for libghthash */
#include "ght_hash_table.h"
#include <sys/time.h>
#include <time.h>
/* for http */
#include <sys/queue.h>
#include <err.h>
#include <evhttp.h>
#include <pwd.h>
/* for OpenSSL, specifically so we can +1/-1 NSEC3 White Lies */
#include <openssl/bn.h>
// SECTION 1: STRUCTS
struct phreebird_opts_struct
{
char *dnskey_fname;
bool gen_key;
ldns_key *key; // one key to rule them all
ldns_key_list *keylist;
int stubsock;
int backsock;
struct sockaddr_in back_addr;
ght_hash_table_t *correlator;
unsigned long long pcount;
ldns_rdf *owner_rdf;
ldns_rr *dnskey;
ldns_rr *ds;
uint16_t *keytag;
struct evhttp *httpd;
unsigned short http_port;
// for TCP
int listensock;
// Reduced-privilege additions, JS 2011-05-20:
char *chroot;
uid_t uid;
};
typedef struct phreebird_opts_struct phreebird_opts;
struct request_cache_struct
{
phreebird_opts *opts; // TCP needed it
unsigned short id;
bool edns_do;
unsigned short edns_size;
int method;
bool free_buf;
// UDP
struct sockaddr_in addr;
// HTTP
struct evhttp_request *req;
// TCP
struct event *clientevent;
int clientfd;
};
typedef struct request_cache_struct request_cache;
struct nsec3_rate_limiter_struct
{
unsigned int sec;
unsigned int max;
unsigned int count;
};
typedef struct nsec3_rate_limiter_struct nsec3_rate_limiter;
// SECTION 2: GLOBALS
#define METHOD_UDP 0
#define METHOD_HTTP 1
#define METHOD_TCP 2
static bool debug;
static ght_hash_table_t *rrsig_cache; // XXX There are better things I could be using here than GHT
// XXX not using GHT's max cache size, which means we can have our memory drained (problem!)
static nsec3_rate_limiter nsec3_rater;
//premature optimization is the root of all evil but I am not going to re-initialize this static rdf on every packet
ldns_rdf *time_rdf;
#define LOOKING_FOR_ARGSET 0
#define LOOKING_FOR_ARG 1
#define FOUND_VERSION_ARG 2
#define FOUND_QUERY_ARG 3
#define FAIL_PARSE 4
// SECTION 3: FUNCTION DECLARATIONS
int main(int argc, char **argv);
int create_key(char *fname);
int setup_key(ldns_key *key);
int init_udp_socket(unsigned short port);
int init_sockets(phreebird_opts *opts);
int init_tcp_socket(unsigned short port);
int execute_event_listener(phreebird_opts *opts);
void init_key(phreebird_opts *opts);
void set_defaults(phreebird_opts *opts);
void stub_handler_UDP(int fd, short event, void *arg);
void stub_handler_TCP(int fd, short event, void *arg);
void stub_handler_HTTP(struct evhttp_request *req, void *arg);
void clientcallback(int clientfd, short event, void *arg) ;
void listen_handler(int listenfd, short event, void *arg) ;
void stub_handle_request(phreebird_opts *opts, char *buf, size_t len, request_cache *store_cache);
void backend_handler_UDP(int fd, short event, void *arg);
void backend_handle_response(phreebird_opts *opts, char *buf, size_t len, struct sockaddr_in* cAddr);
void send_response_pkt(ldns_pkt *response, request_cache *store_cache, phreebird_opts *opts);
void http_reply(int rcode, unsigned char *buf, int len, struct evhttp_request *req);
void response_fixup(ldns_pkt *response, int edns);
int do_sign(ldns_rr_list *dest, ldns_rr_list *src, ldns_key_list *keylist);
void do_help();
ldns_pkt *build_response(ldns_rdf *orig_q, ldns_rr *rr, ldns_key_list *keylist, bool sign);
ldns_rr_list *build_nsec3_response(ldns_rdf *name, ldns_rdf *shortname, char *mask, bool do_bangbang);
void pb_abort(char *str);
bool validate_name(char *str);
unsigned long listen_addr=INADDR_ANY;
char *listen_name=NULL;
// SECTION 4: MAIN
phreebird_opts opts={0};
int main(int argc, char **argv){
int c;
char *p;
//extern char *optarg;
//extern int optind, opterr, optopt;
listen_name=strdup("0.0.0.0");
if (!listen_name) exit(255);
set_defaults(&opts);
while ((c = getopt(argc, argv, "c:u:k:gdb:?m:l:")) != -1) {
switch (c){
case 'c':
if (!(opts.chroot=strdup(optarg)))
exit(255);
break;
case 'u':
{
struct passwd *pw;
pw=getpwnam(optarg);
if (!pw) {
perror("Lookup user ID to run as");
exit(255);
}
opts.uid=pw->pw_uid;
}
break;
case 'k':
LDNS_FREE(opts.dnskey_fname);
opts.dnskey_fname = strdup(optarg);
break;
case 'g':
opts.gen_key=1;
break;
case 'd':
debug=1;
break;
case 'b':
p=strchr(optarg, ':');
if(p) *p=0;
//LDNS_FREE(opts->backend_ip);
inet_aton(optarg, &opts.back_addr.sin_addr);
if(p){
opts.back_addr.sin_port = htons(atoi(p+1));
}
//opts->backend_ip = strdup(optarg);
//if(p){opts->backend_port = atoi(p+1);}
break;
case 'm':
nsec3_rater.max = atoi(optarg);
break;
case 'l':
inet_aton(optarg,&listen_addr);
free(listen_name);
listen_name=strdup(optarg);
if (!listen_name) exit(255);
break;
case '?':
do_help();
exit(255);
break;
default:
break;
}
}
init_key(&opts);
init_sockets(&opts);
execute_event_listener(&opts);
//never reached
return 0;
}
// SECTION 5: INITIALIZATION
int create_key(char *fname){
FILE *f;
ldns_key *key;
f = fopen(fname, "w");
if(f==NULL) { return -1; }
key = ldns_key_new_frm_algorithm(LDNS_RSASHA1_NSEC3, 1024);
if(key==NULL) { pb_abort("couldn't make algo"); }
ldns_key_print(f, key);
fclose(f);
fprintf(stdout, "Generated key: %s. Restart without -g.\n", fname);
exit(0);
return(1);
}
void init_key(phreebird_opts *opts){
int r;
FILE *f;
bool status;
if(opts->gen_key){
r=create_key(opts->dnskey_fname);
if(r<0) {
fprintf(stderr, "Unable to create key: %s\n", opts->dnskey_fname);
exit(255);
}
}
f = fopen(opts->dnskey_fname, "r");
if(f==NULL){
fprintf(stderr, "Unable to open key: %s\n", opts->dnskey_fname);
fprintf(stderr, "Execute with -g flag to generate a key.\n\n");
do_help();
exit(255);
}
ldns_key_new_frm_fp(&opts->key, f);
if(opts->key == NULL) { pb_abort("couldn't read key"); }
if(debug) { ldns_key_print(stdout, opts->key); }
opts->keylist = ldns_key_list_new();
if(opts->keylist == NULL) { pb_abort("couldn't make keylist\n"); }
status = ldns_key_list_push_key(opts->keylist, opts->key);
if(status == 0) { pb_abort("couldn't push key onto keylist\n"); }
fclose(f);
}
int setup_key(ldns_key *key){
struct timeval now;
gettimeofday(&now, NULL);
ldns_key_set_flags(key, LDNS_KEY_SEP_KEY | LDNS_KEY_ZONE_KEY);
ldns_key_set_expiration(key, now.tv_sec + (1024 * 1024 * 256)); //now.tv_sec + 1024 );
ldns_key_set_inception(key, 1);
ldns_key_set_keytag(key, 2013);
return(1);
}
void cleanup(void) {
ldns_key *key;
while(opts.keylist && (key=ldns_key_list_pop_key(opts.keylist))) {
ldns_key_deep_free(key);
}
if (opts.key) ldns_key_deep_free(opts.key);
if (opts.dnskey_fname) free(opts.dnskey_fname);
if (listen_name) free(listen_name);
if (opts.keylist) ldns_key_list_free(opts.keylist);
ght_finalize(opts.correlator);
if(rrsig_cache) ght_finalize(rrsig_cache);
ldns_rdf_deep_free(time_rdf);
CRYPTO_cleanup_all_ex_data();
}
void set_defaults(phreebird_opts *opts){
debug=0;
rrsig_cache = NULL;
nsec3_rater.count=0;
nsec3_rater.sec=0;
nsec3_rater.max=200;
opts->dnskey_fname = strdup("dns.key");
opts->gen_key=0;
opts->back_addr.sin_family = AF_INET;
opts->back_addr.sin_port = htons(50053);
inet_aton("127.0.0.1", &opts->back_addr.sin_addr);
bzero(&(opts->back_addr.sin_zero), 8);
opts->correlator = ght_create(1024*1024);
opts->pcount=0;
opts->http_port = 80;
time_rdf = ldns_dname_new_frm_str("_dns._time.");
atexit(cleanup);
}
int init_udp_socket(unsigned short port){
int sock;
int yes = 1;
int bsize = 65536*1; // arbitrary
int len = sizeof(struct sockaddr);
struct sockaddr_in addr;
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
fprintf(stderr, "Unable to make UDP socket %u\n", port);
exit(255);
}
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
fprintf(stderr, "Error setting socket option (reuseaddr).\n");
exit(255);
}
if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &bsize, sizeof(int)) < 0) {
fprintf(stderr, "Error setting socket option (rcvbuf %u).\n", bsize);
exit(255);
}
if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &bsize, sizeof(int)) < 0) {
fprintf(stderr, "Error setting socket option. (sndbuf %u).\n", bsize);
exit(255);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = listen_addr;
bzero(&(addr.sin_zero), 8);
if (bind(sock, (struct sockaddr*)&addr, len) < 0) {
fprintf(stderr, "Unable to bind to port: %u.\n", port);
exit(255);
}
return sock;
}
int init_sockets(phreebird_opts *opts){
opts->stubsock = init_udp_socket(53);
opts->backsock = init_udp_socket(0);
opts->listensock = init_tcp_socket(53);
return 1;
}
int init_tcp_socket(unsigned short port){
int sock;
int optval=1;
struct sockaddr_in addr;
int status;
sock = socket(PF_INET, SOCK_STREAM, 0);
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(optval));
fcntl(sock, F_SETFL, O_NONBLOCK);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = listen_addr;
addr.sin_port = htons(port);
status = bind(sock, (void *)&addr, sizeof(struct sockaddr_in));
if(status<0) { pb_abort("couldn't bind to port\n"); }
status = listen(sock, 128);
if(status<0) { pb_abort("couldn't listen\n"); }
return sock;
}
static void dropprivs(phreebird_opts *opts) {
if (opts->chroot && chroot(opts->chroot)==-1) {
perror("chroot");
exit(255);
}
if (opts->uid && setuid(opts->uid)==-1) {
perror("setuid");
exit(255);
}
}
// SECTION 6: STUB HANDLERS
static void termhandler(int t)
{
event_loopbreak();
}
int execute_event_listener(phreebird_opts *opts){
struct event rec_from_stub, rec_from_backend, rec_from_tcp;
struct evhttp *httpd;
struct event_base *eb=event_init();
signal(SIGTERM,termhandler);
event_set(&rec_from_stub, opts->stubsock, EV_READ | EV_PERSIST, stub_handler_UDP, opts);
event_set(&rec_from_backend, opts->backsock, EV_READ | EV_PERSIST, backend_handler_UDP, opts);
event_set(&rec_from_tcp, opts->listensock, EV_READ | EV_PERSIST, stub_handler_TCP, opts);
event_add(&rec_from_stub, NULL);
event_add(&rec_from_backend, NULL);
event_add(&rec_from_tcp, NULL);
httpd = evhttp_start(listen_name, opts->http_port);
if(httpd != NULL) {
//pb_abort("couldn't start web server\n"); }
evhttp_set_timeout(httpd, 3);
evhttp_set_cb(httpd, "/.well-known/dns-http", stub_handler_HTTP, opts);
//evhttp_set_cb(httpd, "/stop", exit, opts);
//evhttp_set_gencb(httpd, stub_handler_HTTP, opts);
opts->httpd = httpd;
}
dropprivs(opts);
event_dispatch();
event_base_free(eb);
return(0);
}
void stub_handler_UDP(int fd, short event, void *arg){
char buf[2048];
size_t len;
struct sockaddr_in cAddr;
phreebird_opts *opts = arg;
request_cache store_cache={0};
unsigned int l = sizeof(struct sockaddr);
len = recvfrom(fd, buf, 2048, 0, (struct sockaddr*)&(store_cache.addr), &l);
if(debug) { fprintf(stderr, "Received %u bytes from %s\n", len, inet_ntoa(store_cache.addr.sin_addr));}
store_cache.method = METHOD_UDP;
store_cache.free_buf = 0;
stub_handle_request(opts, buf, len, &store_cache);
}
void stub_handler_TCP(int listenfd, short event, void *arg) {
int clientfd;
struct event * clientevent;
/* supress unused variable warnings */
(void) event;
phreebird_opts *opts = arg;
request_cache *store_cache;
store_cache = calloc(sizeof(struct request_cache_struct), 1); //ALLOC1 (variant)
if(store_cache == NULL) { pb_abort("couldn't allocate request_cache\n"); }
clientfd = accept(listenfd, NULL, NULL);
clientevent = (struct event *) malloc(sizeof(struct event)); //ALLOC2
store_cache->opts =opts;
store_cache->method=METHOD_TCP;
store_cache->clientevent=clientevent;
event_set(clientevent, clientfd, EV_READ | EV_PERSIST, clientcallback, store_cache);
event_add(clientevent, NULL);
}
void clientcallback(int clientfd, short event, void *arg) {
char *buffer;
int amount_read;
//int amount_sent;
//struct event * clientevent;
/* supress unused variable warnings */
(void) event;
request_cache *store_cache;
store_cache = (request_cache *)arg;
unsigned short to_read;
amount_read = read(clientfd, &to_read, 2);
if (amount_read < 2) {
close(clientfd);
event_free(store_cache->clientevent); // FREE2
return; // MIDRET, ok because no alloc before here
}
buffer = calloc(to_read, 1); //ALLOC3
amount_read = read(clientfd, buffer, to_read);
store_cache->clientfd = clientfd;
// XXX we aren't even bothering to handle split reads right now
// this is rather lazy.
stub_handle_request(store_cache->opts, buffer, amount_read, store_cache);
LDNS_FREE(buffer); //FREE3
}
void stub_handle_request(phreebird_opts *opts, char *buf, size_t len, request_cache *store_cache){
ldns_pkt *request, *response;
ldns_status status;
ldns_rr *q;
int slen = sizeof(struct sockaddr);
char hashkey[512];
ght_hash_table_t *reqlist;
//int offset;
//ldns_rdf *shortname;
ldns_rr *dnskey, *ds;
//char nsbuf[512];
ldns_rdf *shortname = NULL;
ldns_rdf *tmp = NULL;
request=response=NULL;
memset(hashkey, 0, sizeof(hashkey));
status = ldns_wire2pkt(&request, (uint8_t *)buf, len);
if(status != LDNS_STATUS_OK){
if(debug) { fprintf(stderr, "Bad packet received from %s\n", inet_ntoa(store_cache->addr.sin_addr)); }
if(store_cache->method == METHOD_HTTP){
http_reply(500, "fail", 4, store_cache->req);
evhttp_request_free(store_cache->req);
}
goto out;
}
if(debug) { ldns_pkt_print(stderr, request);}
// insert handler for instant replies
q = ldns_pkt_question(request)->_rrs[0];
// needed for dns time
shortname = ldns_rdf_clone(ldns_rr_owner(q));
if(shortname == NULL) { pb_abort( "couldn't create shortname\n"); }
while(ldns_dname_label_count(shortname) > 2) { // XXX yes, I know, this needs to be much more mature. Walk before run.
ldns_rdf *tmp=NULL;
tmp=ldns_dname_left_chop(shortname);
if(tmp==NULL) { pb_abort("couldn't create tmpname 1\n"); }
ldns_rdf_free(shortname);
shortname=tmp;
}
//CASE 1: The stub wants a DS or DNSKEY record. We don't go back to the backend for this.
if((ldns_rr_get_type(q) == LDNS_RR_TYPE_DNSKEY ||
ldns_rr_get_type(q) == LDNS_RR_TYPE_DS)
&& (ldns_dname_label_count(ldns_rr_owner(q))==2)){
// XXX OK. There are two bugs here:
// 1) There might actually be DS or DNSKEY records behind this NS! We really only should be
// interposing when the response comes back blank. We're not doing this now because that
// requires actually asking the backend. In other words, have to re-engineer for a special case.
// 2) I'm being tremendously presumptuous, not even letting the user configure the number of labels
// that separate the closed and open namespaces -- for example, I assume foo.com and bar.to,
// but foo.co.uk is right out. This is actually a fairly tricky problem; I'll fix it but only once I can
// actually find a three-label DNSSEC name I can register somewhere
ldns_key_set_pubkey_owner(opts->key, ldns_rr_owner(q));
dnskey = ldns_key2rr(opts->key);
if(dnskey == NULL) { pb_abort("couldn't set DNSKEY\n"); }
if(ldns_rr_get_type(q) == LDNS_RR_TYPE_DS) {
ds = ldns_key_rr2ds(dnskey, LDNS_SHA1);
if(ds == NULL) { pb_abort("couldn't set DS\n"); }
}
buf[sizeof(buf)]=0;
// response will never be NULL, we'll pb_abort first
if(q->_rr_type == LDNS_RR_TYPE_DNSKEY){
response = build_response(ldns_rr_owner(q), dnskey, opts->keylist, true);
}
if(q->_rr_type == LDNS_RR_TYPE_DS){
response = build_response(ldns_rr_owner(q), ds, opts->keylist, true);
}
ldns_pkt_set_id(response, ldns_pkt_id(request));
send_response_pkt(response, store_cache, opts); // XXX we actually need to set >512 if edns missing
ldns_pkt_free(response);
// ldns_pkt_free deep freed its rrs. So a DS response left the DNSKEY
// We don't generate the DS on a DNSKEY request
if(ldns_rr_get_type(q) == LDNS_RR_TYPE_DS) ldns_rr_free(dnskey);
}
// CASE 2: The stub is requesting a magic record that we supply (in this case, dnstime)
else if(ldns_rdf_compare(time_rdf, shortname)==0 && ldns_rr_get_type(q)==LDNS_RR_TYPE_TXT){
// This is DNS Time support, intended for somewhere high in the DNS heirarchy.
// The idea is you should be able to get a signed expression of time, enough to bootstrap time.
// This _intentionally_ works for both _dns._time and $RANDOM._dns._time -- in fact, that's _required_
// for scalability
char rrbuf[256];
char tbuf[32];
const char *tformat = "%Y%m%d%H%M%S";
struct timeval now;
//struct tm* ptm;
ldns_buffer *lb;
ldns_rr *time_rr = NULL;
bool do_sign;
lb = ldns_buffer_new(256);
if(lb==NULL) { pb_abort("couldn't create buffer\n"); }
ldns_rdf2buffer_str_dname(lb, time_rdf);
do_sign = ldns_pkt_edns_do(request);
gettimeofday(&now, NULL);
strftime(tbuf, sizeof(tbuf), tformat, gmtime(&now.tv_sec));
snprintf(rrbuf, sizeof(rrbuf), "%s IN TXT \"v=dtm1 t=%s\"", lb->_data, tbuf);
ldns_rr_new_frm_str(&time_rr, rrbuf, 1, NULL, NULL);
if(time_rr == NULL) { pb_abort("couldn't create time_rr\n"); }
response = build_response(ldns_rr_owner(q), time_rr, opts->keylist, do_sign);
ldns_pkt_set_id(response, ldns_pkt_id(request));
send_response_pkt(response, store_cache, opts); // XXX we actually need to set >512 if edns missing
ldns_buffer_free(lb);
ldns_pkt_free(response);
}
// CASE 3: We're signing something that the backend has to provide us the answers for.
// WE DO NOT CACHE ANSWERS -- this is an intentional design decision, which makes sure we can always
// reply correctly. There are complex rules for what answers to return when.
else{
// run to the backend
int status;
ldns_buffer *name;
name = ldns_buffer_new(512);
ldns_rdf2buffer_str_dname(name, ldns_rr_owner(q));
snprintf(hashkey, sizeof(hashkey), "%s/%u/%u/%u", name->_data, ldns_rr_get_type(q) , ldns_rr_get_class(q), ldns_pkt_id(request));
ldns_buffer_free(name);
// We may have multiple hosts asking for the same name/type/class/txid. Yes, even TXID. It's only a 65K range!
reqlist = ght_get(opts->correlator, sizeof(hashkey), hashkey);
if(reqlist == NULL){
reqlist = ght_create(128); // XXX could be better?
if(reqlist == NULL) { pb_abort("couldn't create GHT hash\n"); }
ght_insert(opts->correlator, reqlist, sizeof(hashkey), hashkey);
}
store_cache->id = ldns_pkt_id(request);
store_cache->edns_do = ldns_pkt_edns_do(request);
store_cache->edns_size = ldns_pkt_edns_udp_size(request);
status = ght_insert(reqlist, store_cache, sizeof(opts->pcount), &opts->pcount);
if(status<0) {
ght_remove(opts->correlator, sizeof(hashkey), hashkey);
//LDNS_FREE(store_cache);
ght_finalize(reqlist);
goto out;
}
// don't think I need to care about this failing
sendto(opts->backsock, buf, len, 0, (void *)&(opts->back_addr), slen);
}
out:
if(request) ldns_pkt_free(request);
if(shortname) ldns_rdf_free(shortname);
if(store_cache->free_buf==1) { LDNS_FREE(buf);}; //FREE1(variant)
}
void stub_handler_HTTP(struct evhttp_request *req, void *arg)
{
phreebird_opts *opts;
request_cache *store_cache;
char *offset, *decoded;
int orig_len, decoded_len;
char *version_start = NULL;
int version;
char *query_b64 = NULL;
int ustate = LOOKING_FOR_ARGSET;
int arglen = strlen(req->uri);
char *u = req->uri;
int i;
/* In theory, evhttp should have a URL cracker. In reality, this is a basic state machine.
* It should handle things well -- state machines are nice like that -- but obviously it requires
* pen testing.
*/
for(i=0; i<arglen; i++){
if(ustate == LOOKING_FOR_ARGSET){
if(u[i] == '?'){
ustate = LOOKING_FOR_ARG;
continue;
}
}
if(ustate == LOOKING_FOR_ARG){
if(u[i] == 'v') {
ustate = FOUND_VERSION_ARG;
if(i+2 < arglen && u[i+1]=='=') { version_start = u+i+2; }
else { ustate = FAIL_PARSE; }
continue;
}
if(req->uri[i] == 'q') {
ustate = FOUND_QUERY_ARG;
if(i+2 < arglen && u[i+1]=='=') { query_b64 = u+i+2; }
else { ustate = FAIL_PARSE; }
continue;
}
}
if(ustate == FOUND_VERSION_ARG || ustate == FOUND_QUERY_ARG){
if(u[i] == '&') {
u[i] = 0;
ustate = LOOKING_FOR_ARG;
continue;
}
}
}
if(ustate == FAIL_PARSE || version_start == NULL || query_b64 == NULL){
http_reply(500, "fail", 4, req);
return; //MIDRET
}
version = atoi(version_start);
if(version<1) {
// Assuming the TLS model, where you can ask for whatever version you want. We'll
// add a flag on return saying what version was supported.
http_reply(500, "fail", 4, req);
return; // MIDRET
}
opts = (phreebird_opts *)arg;
orig_len = strlen(query_b64);
decoded = calloc(orig_len, 1); //ALLOC4
decoded_len=ldns_b64_pton(query_b64, decoded, orig_len);
if(decoded_len<0){ decoded_len = 0; }
store_cache = calloc(sizeof(struct request_cache_struct), 1);
if(store_cache == NULL) { pb_abort("couldn't decode b64 from HTTP\n"); }
store_cache->req = req;
store_cache->method = METHOD_HTTP;
store_cache->free_buf = true;
stub_handle_request(opts, decoded, decoded_len, store_cache);
LDNS_FREE(decoded); //FREE4
}
// SECTION 7: BACKEND COMMS
void backend_handler_UDP(int fd, short event, void *arg){
unsigned char buf[2048];
int len; // NOT size_t, as I discovered.
struct sockaddr_in cAddr;
phreebird_opts *opts = arg;
int l = sizeof(struct sockaddr);
len = recvfrom(fd, buf, 2048, 0, (struct sockaddr*)&cAddr, &l);
if(len<0) return;
if(debug) { fprintf(stderr, "Received %u bytes from %s\n", len, inet_ntoa(cAddr.sin_addr));}
backend_handle_response(opts, buf, len, &cAddr);
}
void backend_handle_response(phreebird_opts *opts, char *buf, size_t len, struct sockaddr_in* cAddr){
ldns_pkt *response = NULL;
ldns_pkt *response_signed = NULL;
ldns_status status;
ldns_rr *rr = NULL;
int slen = sizeof(struct sockaddr);
char hashkey[512];
ght_hash_table_t *reqlist;
ght_iterator_t iterator;
request_cache *store_cache;
const void *p_key;
uint8_t *newbuf;
size_t newlen;
ldns_rr_list *signatures = NULL;
unsigned short an, ar, ns;
ldns_rr_list *ans_orig = NULL;
ldns_rr_list *ans_sign = NULL;
ldns_rdf *shortname = NULL;
ldns_rdf *zonename = NULL;
ldns_rr *nsec3_rr, *nsec3_rr_enclosing, *nsec3_rr_wildcard = NULL;
ldns_rr_list *nsec3_rr_list = NULL;
ldns_rr_list *nsec3_rr_enclosing_list = NULL;
ldns_rr_list *nsec3_rr_wildcard_list = NULL;
ldns_pkt *silly = NULL;
bool nsec3_signed_it = false;
bool servfail = false;
ldns_rr_list *empty_answer = NULL;
ldns_rr_list *empty_authority = NULL;
ldns_rr_list *empty_additional = NULL;
// NSEC3
char *type;
ldns_rdf *wildname = NULL;
ldns_buffer *shortname_buf = NULL;
char wildbuf[512];
ldns_rdf *parent_zone = NULL;
char *c;
BIGNUM *left, *right;
char *lbuf, *rbuf;
char lhash[64], rhash[64];
char nsec_descrip[256];
int llen, rlen;
char salt[2];
char mask[2048];
bool b_status = false;
int sign_status;
//
ldns_buffer *name;
ans_sign = NULL;
memset(hashkey, 0, sizeof(hashkey));
status = ldns_wire2pkt(&response, buf, len);
if(status != LDNS_STATUS_OK){
if(debug) { fprintf(stderr, "Bad packet received from %s\n", inet_ntoa(cAddr->sin_addr)); }
return; //MIDRET
}
if(debug) { ldns_pkt_print(stderr, response);}
rr = ldns_pkt_question(response)->_rrs[0];
// XXX OK. This entire approach has a flaw: The backend server loses context of the original IP making the request.
// That's fine for an initial release but it's a huge problem for the very GeoIP backends that an online signer can support.
// There are three fixes, one or more of which will be implemented in a future release:
// 1) Mangle Table. In this case, we alter the packets coming in and out of the host.
// This has the advantage of fairly ridiculous performance, because the kernel becomes our event engine.
// 2) EDNS0 Backend Declaration. This is the OpenDNS/Google (I'm not going to try to tease apart that history)
// approach where the forwarded query says who made it. It works well, but it does depend on the backend
// implementing support.
// 3) L2 forwarding. In this approach, we basically force there to be two hosts, and statically set ourselves as the
// IP gateway for the host we're supplementing. For various reasons I'm not the biggest fan of this approach.
// Anyway, you can see why I'm not going down this particular rabbit hole right now.
shortname = ldns_rdf_clone(ldns_rr_owner(rr));
if(shortname == NULL) { pb_abort( "couldn't create shortname\n"); }
while(ldns_dname_label_count(shortname) > 2) { // XXX yes, I know, this needs to be much more mature. Walk before run.
//if(ldns_dname_label_count(shortname) != 2) { // this strips one(1) label iff label count != 2
ldns_rdf *tmp=NULL;
tmp=ldns_dname_left_chop(shortname);
if(tmp==NULL) { pb_abort("couldn't create tmpname 2\n"); }
LDNS_FREE(shortname);
shortname=tmp;
}
ldns_key_set_pubkey_owner(opts->key, shortname);
/* Add NSEC3 records */
if(ldns_pkt_ancount(response)==0){
// WHEW. OK, so NSEC3 records are Base32 Extended (with a period at the end)
// OpenSSL accepts Hex.
// We need to turn them from B32e to Binary, to Hex, to Bignum.
// Then we need to add and subtract 1.
// Then we need to convert both Bignums to Binary, which we then B32e.
// Fun!
// 0: First, sign what's already in there
do_sign(ldns_pkt_authority(response), ldns_pkt_authority(response), opts->keylist);
if(ldns_pkt_get_rcode(response) == LDNS_RCODE_NXDOMAIN){
// 1: Do the qname white lie
type=ldns_rr_type2str(ldns_rr_get_type(rr));
snprintf(mask, sizeof(mask), "%s RRSIG", type);
// will always succeed (or pb_abort)
nsec3_rr_list = build_nsec3_response(ldns_rr_owner(rr), shortname, mask, true);
sign_status = do_sign(nsec3_rr_list, nsec3_rr_list, opts->keylist);
if(sign_status < 0) { servfail = true; goto out; }
// 2: Do the closest encloser white lie
// XXX yes this is ridiculous, re: the bitmask. Apparently it's needed?
snprintf(mask, sizeof(mask), "%s CERT NAPTR A NS CNAME SOA NULL WKS PTR HINFO MX TXT AAAA LOC SRV DS SSHFP IPSECKEY RRSIG NSEC NSEC3 DNSKEY DHCID NSEC3PARAM SPF TKEY RRSIG CNAME", type);
nsec3_rr_enclosing_list = build_nsec3_response(shortname, shortname, mask, false);
sign_status = do_sign(nsec3_rr_enclosing_list, nsec3_rr_enclosing_list, opts->keylist);
if(sign_status < 0) { servfail = true; goto out; }
// 3: Do the wildcard white lie
shortname_buf = ldns_buffer_new(512);
if(shortname_buf == NULL) { pb_abort("couldn't create shortname_buf\n"); }
ldns_rdf2buffer_str_dname(shortname_buf, shortname);
snprintf(wildbuf, sizeof(wildbuf), "*.%s", shortname_buf->_data);
if(validate_name(shortname_buf->_data) != true){ servfail=true; goto out; }
wildname = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME, wildbuf);
if(wildname == NULL) { pb_abort("couldn't create wildname\n"); }
snprintf(mask, sizeof(mask), "%s RRSIG", type);
nsec3_rr_wildcard_list = build_nsec3_response(wildname, shortname, mask, true);
do_sign(nsec3_rr_wildcard_list, nsec3_rr_wildcard_list, opts->keylist);
ldns_buffer_free(shortname_buf);
ldns_rdf_free(wildname);
if(sign_status < 0) { servfail = true; goto out; }
}
else {
type=ldns_rr_type2str(ldns_rr_get_type(rr));
snprintf(mask, sizeof(mask), "CERT NAPTR A NS SOA NULL WKS PTR HINFO MX TXT LOC SRV DS SSHFP IPSECKEY RRSIG NSEC NSEC3 DNSKEY DHCID NSEC3PARAM SPF TKEY RRSIG");
// will always succeed (or pb_abort)
nsec3_rr_list = build_nsec3_response(ldns_rr_owner(rr), shortname, mask, false);
sign_status = do_sign(nsec3_rr_list, nsec3_rr_list, opts->keylist);
if(sign_status < 0) { servfail = true; goto out; }
}
// 4: Push to list. Note this only works if all signatures worked.
if(nsec3_rr_list!=NULL){
b_status = ldns_rr_list_push_rr_list(ldns_pkt_authority(response), nsec3_rr_list);
if(b_status == false) { pb_abort("couldn't push to list\n"); }
ldns_rr_list_free(nsec3_rr_list);
}
if(nsec3_rr_enclosing_list!=NULL){
b_status = ldns_rr_list_push_rr_list(ldns_pkt_authority(response), nsec3_rr_enclosing_list);
if(b_status == false) { pb_abort("couldn't push to list\n"); }
ldns_rr_list_free(nsec3_rr_enclosing_list);
}
if(nsec3_rr_wildcard_list!=NULL){
b_status = ldns_rr_list_push_rr_list(ldns_pkt_authority(response), nsec3_rr_wildcard_list);
if(b_status == false) { pb_abort("couldn't push to list\n"); }
ldns_rr_list_free(nsec3_rr_wildcard_list);
}
nsec3_signed_it = true;
}
out:
name = ldns_buffer_new(512);