summaryrefslogtreecommitdiff
path: root/source4/dns_server/dns_update.c
blob: f789f2d6258115b31132f1556834da0995bee3b5 (plain)
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
/*
   Unix SMB/CIFS implementation.

   DNS server handler for update requests

   Copyright (C) 2010 Kai Blin  <kai@samba.org>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "libcli/util/ntstatus.h"
#include "librpc/ndr/libndr.h"
#include "librpc/gen_ndr/ndr_dns.h"
#include "librpc/gen_ndr/ndr_dnsp.h"
#include <ldb.h>
#include "dsdb/samdb/samdb.h"
#include "dsdb/common/util.h"
#include "dns_server/dns_server.h"

static WERROR check_prerequsites(struct dns_server *dns,
				 TALLOC_CTX *mem_ctx,
				 const struct dns_name_packet *in,
				 const struct dns_res_rec *prereqs, uint16_t count)
{
	const struct dns_name_question *zone;
	size_t host_part_len = 0;
	uint16_t i;

	zone = in->questions;

	for (i = 0; i < count; i++) {
		const struct dns_res_rec *r = &prereqs[i];
		bool match;

		if (r->ttl != 0) {
			return DNS_ERR(FORMAT_ERROR);
		}
		match = dns_name_match(zone->name, r->name, &host_part_len);
		if (!match) {
			/* TODO: check if we need to echo all prereqs if the
			 * first fails */
			return DNS_ERR(NOTZONE);
		}
		if (r->rr_class == DNS_QCLASS_ANY) {
			if (r->length != 0) {
				return DNS_ERR(FORMAT_ERROR);
			}
			if (r->rr_type == DNS_QTYPE_ALL) {
				/* TODO: Check if zone has at least one RR */
				return DNS_ERR(NAME_ERROR);
			} else {
				/* TODO: Check if RR exists of the specified type */
				return DNS_ERR(NXRRSET);
			}
			continue;
		}
		if (r->rr_class == DNS_QCLASS_NONE) {
			if (r->length != 0) {
				return DNS_ERR(FORMAT_ERROR);
			}
			if (r->rr_type == DNS_QTYPE_ALL) {
				/* TODO: Return this error if the given name exits in this zone */
				return DNS_ERR(YXDOMAIN);
			} else {
				/* TODO: Return error if there's an RRset of this type in the zone */
				return DNS_ERR(YXRRSET);
			}
			continue;
		}
		if (r->rr_class == zone->question_class) {
			/* Check if there's a RR with this */
			return DNS_ERR(NOT_IMPLEMENTED);
		} else {
			return DNS_ERR(FORMAT_ERROR);
		}
	}


	return WERR_OK;
}

static WERROR update_prescan(const struct dns_name_question *zone,
			     const struct dns_res_rec *updates, uint16_t count)
{
	const struct dns_res_rec *r;
	uint16_t i;
	size_t host_part_len;
	bool match;

	for (i = 0; i < count; i++) {
		r = &updates[i];
		match = dns_name_match(zone->name, r->name, &host_part_len);
		if (!match) {
			return DNS_ERR(NOTZONE);
		}
		if (zone->question_class == r->rr_class) {
			/*TODO: also check for AXFR,MAILA,MAILB  */
			if (r->rr_type == DNS_QTYPE_ALL) {
				return DNS_ERR(FORMAT_ERROR);
			}
		} else if (r->rr_class == DNS_QCLASS_ANY) {
			if (r->ttl != 0 || r->length != 0) {
				return DNS_ERR(FORMAT_ERROR);
			}
		} else if (r->rr_class == DNS_QCLASS_NONE) {
			if (r->ttl != 0 || r->rr_type == DNS_QTYPE_ALL) {
				return DNS_ERR(FORMAT_ERROR);
			}
		} else {
			return DNS_ERR(FORMAT_ERROR);
		}
	}
	return WERR_OK;
}

WERROR dns_server_process_update(struct dns_server *dns,
				 TALLOC_CTX *mem_ctx,
				 struct dns_name_packet *in,
				 const struct dns_res_rec *prereqs, uint16_t prereq_count,
				 struct dns_res_rec **updates,      uint16_t *update_count,
				 struct dns_res_rec **additional,   uint16_t *arcount)
{
	struct dns_name_question *zone;
	const struct dns_server_zone *z;
	size_t host_part_len = 0;
	WERROR werror = DNS_ERR(NOT_IMPLEMENTED);
	bool update_allowed = false;

	if (in->qdcount != 1) {
		return DNS_ERR(FORMAT_ERROR);
	}

	zone = in->questions;

	if (zone->question_type != DNS_QTYPE_SOA) {
		return DNS_ERR(FORMAT_ERROR);
	}

	DEBUG(0, ("Got a dns update request.\n"));

	for (z = dns->zones; z != NULL; z = z->next) {
		bool match;

		match = dns_name_match(z->name, zone->name, &host_part_len);
		if (match) {
			break;
		}
	}

	if (z == NULL) {
		return DNS_ERR(NOTAUTH);
	}

	if (host_part_len != 0) {
		/* TODO: We need to delegate this one */
		return DNS_ERR(NOT_IMPLEMENTED);
	}

	werror = check_prerequsites(dns, mem_ctx, in, prereqs, prereq_count);
	W_ERROR_NOT_OK_RETURN(werror);

	/* TODO: Check if update is allowed, we probably want "always",
	 * key-based GSSAPI, key-based bind-style TSIG and "never" as
	 * smb.conf options. */
	if (!update_allowed) {
		return DNS_ERR(REFUSED);
	}

	werror = update_prescan(in->questions, *updates, *update_count);
	W_ERROR_NOT_OK_RETURN(werror);

	return werror;
}