-
Notifications
You must be signed in to change notification settings - Fork 1
/
dnsupdate.c
1608 lines (1412 loc) · 42.4 KB
/
dnsupdate.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
/* (c) 2008, Quest Software, Inc. All rights reserved. */
#include "common.h"
#include <arpa/inet.h>
#include <vas.h>
#include <vas_gss.h>
#include "err.h"
#include "dns.h"
#include "dnsdebug.h"
#include "dnstcp.h"
#include "dnstkey.h"
#include "dnstsig.h"
#include "conf.h"
#include "list.h"
#include "resconf.h"
/*
* Dnyamic update of a VAS host's A entry in Active Directory DNS using
* GSS TSIG for authentication.
*
* Useful for when DHCP is not provided by Active Directory, or the
* host's DHCP client does not send option 81.
*
* This is the equivalent to Window's "ipconfig /registerdns" command.
*
* References:
* RFC 1034 Domain Names - Concepts and Facilities, 1987
* RFC 1035 Domain Names - Implementation and Specification, 1987
* RFC 1750 Randomness recommendations for security, 1994
* RFC 1995 Incremental Zone Transfer in DNS, 1996
* RFC 2136 Dynamic updates in the DNS (DNS UPDATE), 1997
* RFC 2535 DNS Security Extensions, 1999
* RFC 2845 Secret key transaction authentication for DNS (TSIG), 2000
* RFC 2930 Secret key establishment for DNS (TKEY), 2000
* RFC 3596 DNS Extensions to Support IP Version 6, 2003
* RFC 3645 GSS algorithm for TSIG for DNS (GSS-TSIG), 2003
*
* See also:
* http://tools.ietf.org/wg/dhc/draft-ietf-dhc-fqdn-option
* http://technet2.microsoft.com/windowsserver/en/technologies/featured/dns/default.mspx
*/
/* TSIG algorithm names */
#define GSS_MICROSOFT_COM "gss.microsoft.com"
#define GSS_TSIG "gss-tsig"
/* Values for UpdateSecurityLevel configuration */
#define SECURITY_ONLY_SECURE 256
#define SECURITY_ONLY_UNSECURE 16
#define SECURITY_UNSECURE_THEN_SECURE 0
/* Values for RegisterReverseLookup */
#define REGISTER_PTR_NEVER 0
#define REGISTER_PTR_ALWAYS 1
#define REGISTER_PTR_ONLY_IF_A_SUCCEEDS 2
/* Default cache TTL for updated records */
#define DEFAULT_TTL (15 * 60) /* 15 minutes */
/* An authentication context structure for convenience */
struct verify_context {
vas_ctx_t *vasctx; /* VAS context */
gss_ctx_id_t gssctx; /* Our security context */
const char *key_name; /* The shared name of the context */
};
/* Prototypes */
static uint16_t unique_id(void);
static int name_eq(const char *a, const char *b);
static void make_key_name(const char *fqdn, char *buf, size_t bufsz);
static void print_gss_error(const char *msg, struct verify_context *ctx,
OM_uint32 major, OM_uint32 minor);
static int verify(const void *buf, size_t buflen, const char *key_name,
const struct dns_tsig *tsig, void *context);
static void *sign(struct dns_tsig *tsig, void *data, size_t datalen,
void *context);
static int update(int s, struct verify_context *vctx, const char *fqdn,
uint16_t utype, uint16_t uclass, uint32_t uttl,
const void *udata, size_t udatalen,
const char *auth_domain);
static int gss_update(vas_ctx_t *ctx, vas_id_t *id, int s,
const char *server, const char *fqdn,
const char *serverspn, uint16_t utype, uint16_t uclass,
uint32_t uttl, const void *udata, size_t udatalen,
const char *auth_domain);
static int my_inet_aton(const char *s, unsigned char *ipaddr,
size_t ipaddrsz);
static uint16_t next_id; /* used by unique_id() */
int verbose; /* Verbose, higher value means more verbose */
int ietf_compliant; /* IETF-compliance flag */
const char *tsig_name = GSS_MICROSOFT_COM; /* Signature standard */
static int random_fd = -1;
/* Initialises the unique ID stream */
void
init_unique_id()
{
if ((random_fd = open("/dev/urandom", O_RDONLY)) < 0) {
srandom(time(0) * getpid());
next_id = random();
}
}
/* Returns a unique message ID for this session */
static uint16_t
unique_id()
{
uint16_t data;
int len;
if (random_fd == -1)
return next_id++;
if ((len = read(random_fd, &data, sizeof data)) < 0)
err(1, "urandom");
if (len != sizeof data)
errx(1, "urandom short read");
return data;
}
/* Returns true if two DNS names are the same */
static int
name_eq(const char *a, const char *b)
{
return strcmp(a, b) == 0;
}
/* Constructs a random key name for TKEY */
static void
make_key_name(const char *fqdn, char *buf, size_t bufsz)
{
int i;
static const char domainchars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789-";
/* Choose a random key for the TKEY */
assert(bufsz > 31);
for (i = 0; i < 31; i++)
buf[i] = domainchars[random() % (sizeof domainchars - 1)];
if (ietf_compliant)
snprintf(buf + 31, bufsz- 31, ".%s", fqdn);
else
buf[i] = 0;
if (verbose)
fprintf(stderr, "using TKEY %s\n", buf);
}
/* Prints a GSS error message to standard error */
static void
print_gss_error(const char *msg, struct verify_context *ctx,
OM_uint32 major, OM_uint32 minor)
{
OM_uint32 eminor, emajor, ectx;
gss_buffer_desc ebuf;
fprintf(stderr, "gss_verify_mic: failed");
ectx = 0;
do {
emajor = gss_display_status(&eminor, major, GSS_C_GSS_CODE,
GSS_C_NO_OID, &ectx, &ebuf);
if (GSS_ERROR(emajor)) errx(1, "gss_display_status: %d", emajor);
fprintf(stderr, "\n %.*s", (int)ebuf.length, (char *)ebuf.value);
(void)gss_release_buffer(&eminor, &ebuf);
} while (ectx);
do {
emajor = gss_display_status(&eminor, minor, GSS_C_MECH_CODE,
GSS_C_NO_OID, &ectx, &ebuf);
if (GSS_ERROR(emajor)) errx(1, "gss_display_status: %d", emajor);
fprintf(stderr, "\n %.*s", (int)ebuf.length, (char *)ebuf.value);
(void)gss_release_buffer(&eminor, &ebuf);
} while (ectx);
fprintf(stderr, "\n");
}
/* Verifies a buffer using a TSIG-GSS MAC. Returns true if verified OK */
static int
verify(const void *buf, size_t buflen, const char *key_name,
const struct dns_tsig *tsig, void *context)
{
struct verify_context *ctx = (struct verify_context *)context;
OM_uint32 minor, major;
gss_buffer_desc msgbuf, tokbuf;
gss_qop_t qop;
if (!name_eq(key_name, ctx->key_name))
return 0;
msgbuf.value = (void *)buf;
msgbuf.length = buflen;
tokbuf.value = (void *)tsig->mac;
tokbuf.length = tsig->maclen;
major = gss_verify_mic(&minor, ctx->gssctx, &msgbuf, &tokbuf, &qop);
if (GSS_ERROR(major)) {
print_gss_error("gss_verify_mic: failed", ctx, major, minor);
if (verbose > 1) {
fprintf(stderr, "mac used was:\n");
dumphex(tokbuf.value, tokbuf.length);
fprintf(stderr, "msg used was:\n");
dumphex(msgbuf.value, msgbuf.length);
}
return 0;
}
return 1;
}
/* Signs a buffer using a TSIG-GSS record. Returns the MAC data */
static void *
sign(struct dns_tsig *tsig, void *data, size_t datalen, void *context)
{
struct verify_context *ctx = (struct verify_context *)context;
gss_buffer_desc msgbuf, tokbuf;
OM_uint32 minor, major;
void *mac;
msgbuf.value = data;
msgbuf.length = datalen;
major = gss_get_mic(&minor, ctx->gssctx, 0, &msgbuf, &tokbuf);
if (GSS_ERROR(major)) {
print_gss_error("gss_get_mic: cannot sign", ctx, major, minor);
errx(1, "gss_get_mic");
}
if (verbose > 1)
fprintf(stderr, "sign: signed %d bytes of data -> %d byte mic\n",
(int)msgbuf.length, (int)tokbuf.length);
mac = malloc(tokbuf.length);
memcpy(mac, tokbuf.value, tokbuf.length);
tsig->maclen = tokbuf.length;
gss_release_buffer(&minor, &tokbuf);
tsig->mac = mac;
return mac;
}
/*
* Queries the authoritative SOA record for a domain, and returns the domain
* and primary nameserver from the SOA record in the authority section.
* Expects an NXDOMAIN result. Returns 0 on success.
*/
static int
query_soa(int s, const char *fqdn, char *domain, size_t domainsz,
char *primary, size_t primarysz)
{
struct dns_msg *msg = NULL;
struct dns_header header, rheader;
struct dns_rr zonerr, rr;
char buffer[32768];
int len;
int rcode = -1;
memset(&header, 0, sizeof header);
header.id = unique_id();
header.opcode = DNS_OP_QUERY;
header.authoritative = 1; /* We want an authoritative answer */
header.recurse_desired = 1; /* Don't care how we get it */
/* Questions */
header.qdcount++;
memset(&zonerr, 0, sizeof zonerr);
dns_rr_set_name(&zonerr, fqdn);
zonerr.type = DNS_TYPE_SOA;
zonerr.class_ = DNS_CLASS_IN;
msg = dns_msg_new();
dns_msg_setbuf(msg, buffer, sizeof buffer);
dns_wr_header(msg, &header);
dns_wr_question(msg, &zonerr);
dns_wr_finish(msg);
if (verbose)
fprintf(stderr, "sending SOA query for %s\n", fqdn);
dnstcp_sendmsg(s, msg);
if (verbose > 2) {
dumpmsg(msg);
fprintf(stderr, "\n");
}
if (verbose > 1)
fprintf(stderr, "waiting for query reply\n");
len = dnstcp_recv(s, buffer, sizeof buffer);
if (len <= 0) {
fprintf(stderr, "no reply to query?\n");
goto fail;
}
dns_msg_setbuf(msg, buffer, len);
if (verbose > 2) {
dumpmsg(msg);
fprintf(stderr, "\n");
}
dns_rd_header(msg, &rheader);
/* Expect an NXDOMAIN QUERY response */
if (rheader.id != header.id || rheader.opcode != DNS_OP_QUERY) {
fprintf(stderr, "bad reply to query\n");
goto fail;
}
if (verbose > 1 || (verbose && rheader.rcode))
fprintf(stderr, "server response: %s\n",
dns_rcode_name(rheader.rcode));
if (rheader.rcode != DNS_NXDOMAIN && rheader.rcode != DNS_NOERROR)
goto fail;
/* Skip question RRs */
while (rheader.qdcount--)
dns_rd_question(msg, &rr);
if (rheader.ancount) {
fprintf(stderr, "unexpected answer record in reply\n");
goto fail;
}
if (!rheader.nscount) {
if (verbose)
fprintf(stderr, "no authority records returned for %s\n", fqdn);
goto fail;
}
dns_rd_rr_head(msg, &rr);
if (rr.type != DNS_TYPE_SOA) {
fprintf(stderr, "expected an SOA record in reply\n");
goto fail;
}
dns_rd_begin(msg);
/* Copy the domain name from the RR header */
if (domainsz + 1 < strlen(rr.name))
errx(1, "domainsz too small");
strcpy(domain, rr.name);
/* The next part of the resource record is the primary nameserver */
dns_rd_name(msg, primary, primarysz);
if (verbose) {
fprintf(stderr, "domain: %s\n", domain);
fprintf(stderr, "primary: %s\n", primary);
}
/* (We ignore the rest of the reply packet) */
rcode = 0;
/* Fallthrough to return success */
fail:
if (msg)
dns_msg_free(msg);
return rcode;
}
/*
* Queries the list of nameservers for the given domain.
* Copies the nameserver entries out of all the NS records in the
* answer section.
*/
static int
query_ns(int s, const char *domain, char ***list_ret)
{
struct dns_msg *msg = NULL;
struct dns_header header, rheader;
struct dns_rr zonerr, rr;
char buffer[32768];
char name[DNS_MAXNAME];
int len;
int rcode = -1;
char **list;
list = list_new();
if (!list)
errx(1, "query_ns: cannot allocate list");
memset(&header, 0, sizeof header);
header.id = unique_id();
header.opcode = DNS_OP_QUERY;
header.authoritative = 1; /* We want an authoritative answer */
header.recurse_desired = 1; /* Don't care how we get it */
/* Questions */
header.qdcount++;
memset(&zonerr, 0, sizeof zonerr);
dns_rr_set_name(&zonerr, domain);
zonerr.type = DNS_TYPE_NS;
zonerr.class_ = DNS_CLASS_IN;
msg = dns_msg_new();
dns_msg_setbuf(msg, buffer, sizeof buffer);
dns_wr_header(msg, &header);
dns_wr_question(msg, &zonerr);
dns_wr_finish(msg);
if (verbose)
fprintf(stderr, "sending NS query for %s...\n", domain);
dnstcp_sendmsg(s, msg);
if (verbose > 2) {
dumpmsg(msg);
fprintf(stderr, "\n");
}
if (verbose > 1)
fprintf(stderr, "waiting for query reply\n");
len = dnstcp_recv(s, buffer, sizeof buffer);
if (len <= 0) {
fprintf(stderr, "no reply to query?\n");
goto fail;
}
dns_msg_setbuf(msg, buffer, len);
if (verbose > 2) {
dumpmsg(msg);
fprintf(stderr, "\n");
}
dns_rd_header(msg, &rheader);
/* Expect an NXDOMAIN QUERY response */
if (rheader.id != header.id || rheader.opcode != DNS_OP_QUERY) {
fprintf(stderr, "bad reply to query\n");
goto fail;
}
if (verbose > 1 || (verbose && rheader.rcode))
fprintf(stderr, "server response: %s\n",
dns_rcode_name(rheader.rcode));
if (rheader.rcode != DNS_NOERROR)
goto fail;
/* Skip question RRs */
while (rheader.qdcount--)
dns_rd_question(msg, &rr);
if (!rheader.ancount) {
if (verbose)
fprintf(stderr, "no answers returned for %s\n", domain);
goto fail;
}
/* Add the answer records to a list */
while (rheader.ancount--) {
dns_rd_rr_head(msg, &rr);
dns_rd_begin(msg);
if (rr.type == DNS_TYPE_NS) {
dns_rd_name(msg, name, sizeof name);
if (verbose > 1)
fprintf(stderr, " NS %s\n", name);
if (list_append(&list, name) < 0)
goto fail;
} else
fprintf(stderr, "non-NS record in reply\n");
dns_rd_end(msg);
}
/* The rest of the packet is A addresses but we ignore those */
rcode = 0;
/* Fallthrough to return success */
fail:
if (msg)
dns_msg_free(msg);
if (rcode == 0)
*list_ret = list;
else
list_free(list);
return rcode;
}
/*
* Perform a DNS update
* udata is treated as binary, unless udatalen is -1, in which case
* udata is treated as a domain name.
* Returns 0 on success, -1 on general error, otherwise a DNS rcode
*/
static int
update(int s, struct verify_context *vctx,
const char *fqdn, uint16_t utype, uint16_t uclass,
uint32_t uttl, const void *udata, size_t udatalen,
const char *auth_domain)
{
struct dns_msg *msg;
struct dns_header header, rheader;
struct dns_rr rr;
char buffer[32768];
int len;
int rcode = -1;
int deleting = (uttl == 0);
memset(&header, 0, sizeof header);
header.id = unique_id();
header.opcode = DNS_OP_UPDATE;
msg = dns_msg_new();
dns_msg_setbuf(msg, buffer, sizeof buffer);
dns_wr_header(msg, &header);
/* Questions [=Zones affected] */
memset(&rr, 0, sizeof rr);
dns_rr_set_name(&rr, auth_domain);
rr.type = DNS_TYPE_SOA;
rr.class_ = DNS_CLASS_IN;
dns_wr_question(msg, &rr);
dns_wr_inc_qdcount(msg);
/* Answers [=Prerequisites] */
if (1) {
/* Require that there is no CNAME entry that trumps the A */
memset(&rr, 0, sizeof rr);
dns_rr_set_name(&rr, fqdn);
rr.type = DNS_TYPE_CNAME;
rr.class_ = DNS_CLASS_NONE;
dns_wr_rr_head(msg, &rr);
dns_wr_data(msg, NULL, 0);
dns_wr_inc_ancount(msg);
}
/* Authoritatives [=Updates] */
memset(&rr, 0, sizeof rr);
dns_rr_set_name(&rr, fqdn);
rr.type = utype;
rr.class_ = DNS_CLASS_ANY; /* Delete existing entry */
dns_wr_rr_head(msg, &rr);
dns_wr_data(msg, NULL, 0);
dns_wr_inc_nscount(msg);
if (!deleting) {
/* Adding the new entry */
memset(&rr, 0, sizeof rr);
dns_rr_set_name(&rr, fqdn);
rr.type = utype;
rr.class_ = uclass;
rr.ttl = uttl;
dns_wr_rr_head(msg, &rr);
if (udatalen == -1) { /* If udatalen == -1, then its a domain name */
uint16_t mark;
dns_wr_begin(msg, &mark);
dns_wr_name(msg, udata);
dns_wr_end(msg, &mark);
} else
dns_wr_data(msg, udata, udatalen);
dns_wr_inc_nscount(msg);
} else if (verbose > 1)
fprintf(stderr, "update is a delete request: data ignored\n");
if (vctx)
dns_tsig_sign(msg, vctx->key_name, tsig_name, 36000, NULL, 0,
sign, vctx);
dns_wr_finish(msg);
if (verbose > 1)
fprintf(stderr, "sending %s update...\n",
vctx ? "secure" : "unsecure");
dnstcp_sendmsg(s, msg);
if (verbose > 2) {
dumpmsg(msg);
fprintf(stderr, "\n");
}
if (verbose > 1)
fprintf(stderr, "waiting for update reply\n");
len = dnstcp_recv(s, buffer, sizeof buffer);
if (len <= 0) {
fprintf(stderr, "no reply to update?\n");
goto fail;
}
dns_msg_setbuf(msg, buffer, len);
if (verbose > 2) {
dumpmsg(msg);
fprintf(stderr, "\n");
}
dns_rd_header(msg, &rheader);
if (rheader.id != header.id || rheader.opcode != DNS_OP_UPDATE) {
fprintf(stderr, "bad reply to update request\n");
goto fail;
}
if (verbose > 1)
fprintf(stderr, "server response: %s\n",
dns_rcode_name(rheader.rcode));
if (rheader.rcode != DNS_NOERROR) {
rcode = rheader.rcode;
goto fail;
}
#if 0 /* XXX always GSS_S_BAD_MIC? */
/* Verify response */
if (vctx) {
dns_msg_setbuf(msg, buffer, len);
dns_tsig_verify(msg, verify, vctx);
if (verbose > 1 || (verbose && rheader.rcode))
fprintf(stderr, "server response verified\n");
}
#endif
dns_msg_free(msg);
return 0;
fail:
dns_msg_free(msg);
return rcode;
}
static void macos_krb5_ccache_hack(void) {
#ifdef __APPLE__
/* Work around a bug in QAS < 4.x (well, Heimdal) where we'd
always get KRB5_FCC_INTERNAL from the API ccache code
when being executed by launchd.
RC bug #663. */
setenv("KRB5CCNAME", "MEMORY:", 0); /* Does not override if set */
#endif /* __APPLE__ */
}
/*
* Negotiate a GSS TKEY, and then call update()
* Returns 0 on success, -1 on internal failure, otherwise a DNS rcode.
*/
static int
gss_update(vas_ctx_t *ctx, vas_id_t *id, int s,
const char *server, const char *fqdn, const char *server_spn,
uint16_t utype, uint16_t uclass, uint32_t uttl,
const void *udata, size_t udatalen, const char *auth_domain)
{
char buffer[32768];
struct dns_rr rr, question;
struct dns_header header;
int bufferlen;
int rcode = -1;
char key_name[DNS_MAXNAME];
char server_principal[2048];
gss_ctx_id_t gssctx;
gss_buffer_desc intok, outtok;
OM_uint32 major, minor;
struct dns_tkey tkey;
struct verify_context vctx;
make_key_name(fqdn, key_name, sizeof key_name);
/* The domain server's principal name */
if (!server_spn) {
krb5_context krb5_ctx;
char **realms;
/* Workaround for vas_gss which uses the default realm instead
* of detecting the host realm for a service */
if (vas_krb5_get_context(ctx, &krb5_ctx)) {
warnx("vas_krb5_get_context: %s", vas_err_get_string(ctx, 1));
goto fail;
}
if (krb5_get_host_realm(krb5_ctx, server, &realms)) {
warnx("krb5_get_host_realm: %s", krb5_get_error_string(krb5_ctx));
goto fail;
}
snprintf(server_principal, sizeof server_principal,
"dns/%s@%s", server, realms[0]);
(void)krb5_free_host_realm(krb5_ctx, realms);
server_spn = server_principal;
}
if (verbose)
fprintf(stderr, "target service: %s\n", server_spn);
macos_krb5_ccache_hack();
/* Perform the GSS rounds */
gssctx = GSS_C_NO_CONTEXT;
intok.length = 0;
intok.value = NULL;
outtok.length = 0;
outtok.value = NULL;
for (;;) {
major = vas_gss_spnego_initiate(ctx, id, NULL, &gssctx,
server_spn,
GSS_C_REPLAY_FLAG | GSS_C_MUTUAL_FLAG | GSS_C_DELEG_FLAG |
GSS_C_SEQUENCE_FLAG | GSS_C_INTEG_FLAG,
VAS_GSS_SPNEGO_ENCODING_DER, intok.length ? &intok : NULL,
&outtok);
if (GSS_ERROR(major)) {
warnx("vas_gss_spnego_initiate: %s", vas_err_get_string(ctx, 1));
goto fail;
}
assert(major == GSS_S_CONTINUE_NEEDED || major == GSS_S_COMPLETE);
if (outtok.value && outtok.length) {
struct dns_msg *msg = dns_msg_new();
dns_msg_setbuf(msg, buffer, sizeof buffer);
memset(&header, 0, sizeof header);
header.id = unique_id();
header.opcode = DNS_OP_QUERY;
/* Questions */
header.qdcount++;
memset(&question, 0, sizeof question);
dns_rr_set_name(&question, key_name);
question.type = DNS_TYPE_TKEY;
question.class_ = DNS_CLASS_IN;
/* Answers */
header.ancount++;
memset(&rr, 0, sizeof rr);
dns_rr_set_name(&rr, key_name);
rr.type = DNS_TYPE_TKEY;
rr.class_ = DNS_CLASS_ANY;
rr.ttl = 0;
memset(&tkey, 0, sizeof tkey);
snprintf(tkey.algorithm, sizeof tkey.algorithm, "%s",
tsig_name);
tkey.inception = time(0);
tkey.expiration = tkey.inception + 2*60*60;
tkey.mode = DNS_TKEY_MODE_GSSAPI;
tkey.key = outtok.value;
tkey.keysz = outtok.length;
/* Build DNS packet */
dns_wr_header(msg, &header);
dns_wr_question(msg, &question);
dns_wr_rr_head(msg, &rr);
dns_tkey_wr(msg, &tkey);
dns_wr_finish(msg);
if (verbose)
fprintf(stderr, "sending tkey query\n");
bufferlen = dnstcp_sendmsg(s, msg);
if (bufferlen == -1)
goto fail;
if (verbose > 1) {
dumpmsg(msg);
fprintf(stderr, "\n");
}
dns_msg_free(msg);
(void)gss_release_buffer(&minor, &outtok);
} else {
if (verbose > 1)
fprintf(stderr, "no output token needed after this round\n");
}
if (major == GSS_S_CONTINUE_NEEDED) {
struct dns_msg *msg = dns_msg_new();
struct dns_header recv_header;
if (verbose)
fprintf(stderr, "waiting for tkey reply\n");
bufferlen = dnstcp_recv(s, buffer, sizeof buffer);
if (bufferlen <= 0)
goto fail;
dns_msg_setbuf(msg, buffer, bufferlen);
if (verbose > 1) {
dumpmsg(msg);
fprintf(stderr, "\n");
}
dns_rd_header(msg, &recv_header);
assert(recv_header.id == header.id);
assert(recv_header.response);
assert(recv_header.opcode == DNS_OP_QUERY);
assert(!recv_header.truncated);
if (recv_header.rcode != 0) {
fprintf(stderr, "could not negotiate GSS context: %s\n",
dns_rcode_name(recv_header.rcode));
rcode = recv_header.rcode;
goto fail;
}
/* Check the question back is the same */
assert(recv_header.qdcount == 1);
dns_rd_question(msg, &question);
assert(name_eq(question.name, key_name));
assert(question.type == DNS_TYPE_TKEY);
assert(question.class_ == DNS_CLASS_IN);
/* Check that the answer is a TKEY */
assert(recv_header.ancount == 1);
dns_rd_rr_head(msg, &rr);
assert(name_eq(rr.name, key_name));
assert(rr.type == DNS_TYPE_TKEY);
assert(rr.class_ == DNS_CLASS_IN ||
rr.class_ == DNS_CLASS_ANY);
dns_tkey_rd(msg, &tkey);
assert(name_eq(tkey.algorithm, tsig_name));
assert(tkey.expiration > time(0));
assert(tkey.mode == DNS_TKEY_MODE_GSSAPI);
assert(tkey.error == DNS_NOERROR);
intok.value = tkey.key;
intok.length = tkey.keysz;
dns_msg_free(msg);
} else
break;
}
if (verbose)
fprintf(stderr, "gss context established\n");
vctx.vasctx = ctx;
vctx.gssctx = gssctx;
vctx.key_name = key_name;
/* Verify the final TSIG */
if (bufferlen) {
struct dns_msg *msg = dns_msg_new();
dns_msg_setbuf(msg, buffer, bufferlen);
dns_tsig_verify(msg, verify, &vctx);
dns_msg_free(msg);
if (verbose)
fprintf(stderr, "TSIG verified\n");
} else
errx(1, "final TSIG from server was not signed");
return update(s, &vctx, fqdn, utype, uclass, uttl, udata,
udatalen, auth_domain);
fail:
return rcode;
}
/*
* Convert a string containing an IP address e.g "12.34.56.67"
* into a unsigned char[4]. Returns true if the conversion
* completed successfully.
*/
static int
my_inet_aton(const char *s, unsigned char *ipaddr, size_t ipaddrsz)
{
unsigned int octet[4];
assert(ipaddrsz == 4 * sizeof (unsigned char));
if (sscanf(s, "%u.%u.%u.%u", octet, octet+1, octet+2, octet+3) != 4 ||
octet[0] > 255 || octet[1] > 255 ||
octet[2] > 255 || octet[3] > 255)
return 0;
ipaddr[0] = octet[0];
ipaddr[1] = octet[1];
ipaddr[2] = octet[2];
ipaddr[3] = octet[3];
return 1;
}
/* Initialise GSS credentials. Returns true on success */
static int
gss_auth_init(vas_ctx_t **vas_ctx_p, vas_id_t **local_id_p, const char *spn)
{
vas_ctx_t *vas_ctx = NULL;
vas_id_t *local_id = NULL;
vas_err_t error;
/* Initialise VAS */
error = vas_ctx_alloc(&vas_ctx);
if (error != VAS_ERR_SUCCESS) {
warnx("vas_ctx_alloc");
goto fail;
}
error = vas_id_alloc(vas_ctx, spn, &local_id);
if (error) {
warnx("vas_id_alloc: %s", vas_err_get_string(vas_ctx, 1));
goto fail;
}
error = vas_id_establish_cred_keytab(vas_ctx, local_id,
VAS_ID_FLAG_USE_MEMORY_CCACHE, NULL);
if (error) {
warnx("vas_id_establish_cred_keytab: %s",
vas_err_get_string(vas_ctx, 1));
goto fail;
}
error = vas_gss_initialize(vas_ctx, local_id);
if (error) {
warnx("vas_gss_initialize: %s",
vas_err_get_string(vas_ctx, 1));
goto fail;
}
*vas_ctx_p = vas_ctx;
*local_id_p = local_id;
return 1;
fail:
if (vas_ctx)
vas_ctx_free(vas_ctx);
return 0;
}
/* Determine the fully qualified domain name of the VAS-joined host */
static int
gss_auth_init_fqdn(vas_ctx_t *vas_ctx, vas_id_t *local_id, char *spn,
char **fqdn_p)
{
vas_computer_t *local_computer;
char *fqdn = NULL;
vas_err_t error;
error = vas_computer_init(vas_ctx, local_id, spn,
VAS_NAME_FLAG_NO_IMPLICIT, &local_computer);
if (error) {
warnx("vas_computer_init: %s", vas_err_get_string(vas_ctx, 1));
return 0;
}
error = vas_computer_get_dns_hostname(vas_ctx, local_id, local_computer,
&fqdn);
if (error) {
warnx("vas_computer_get_dns_hostname: %s",
vas_err_get_string(vas_ctx, 1));
return 0;
}
*fqdn_p = fqdn;
return 1;
}
/* Returns the number of dots ('.') in the string s */
static int
count_dots(const char *s)
{
int ndots = 0;
while (*s)
if (*s++ == '.')
ndots++;
return ndots;
}
/* Returns pointer to the parent domain part of a name */
static const char *
parent_domain(const char *d)
{
for (; *d; d++)
if (*d == '.') {
d++;
break;
}
return d;
}
/* Load default configuration, once */
static void
config_init_once()
{
static int config_loaded;
if (!config_loaded) {
config_loaded = 1;
config_load(PATH_SYSCONFDIR "/dnsupdate.conf");
resconf_init();
}
}
/* Sets an option of the form "KEY=VALUE" */
static int
config_opt(char *arg)
{
char *eq;
eq = strchr(arg, '=');
if (!eq)
return 0;
*eq++ = '\0';
config_add(arg, eq);
return 1;
}
/**
* Since we can't rely on getaddrinfo() existing everywhere, our own
* address-tracking structure (since we only care about the address
* component and family).
*/
struct addr {
int family;
union {
struct in_addr inaddr;
struct in6_addr in6addr;
} u;
};
/**
* Parses a string into an address structure, if possible.
*
* @return 0 on success, -1 on failure with errno set to indicate the error.
*/
static int addr_from_str(const char *str, struct addr *addr)
{
assert(str != NULL);
assert(addr != NULL);
/* Default error to EINVAL for inet_pton which does not set errno on
* invalid network address (only on EAFNOSUPPORT). */
errno = EINVAL;
if (inet_pton(AF_INET6, str, &addr->u.in6addr) == 1) {
addr->family = AF_INET6;
return 0;
}