2 Unix SMB/CIFS implementation.
4 Python DNS server wrapper
6 Copyright (C) 2015 Andrew Bartlett
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "python/py3compat.h"
27 #include "dns_server/dnsserver_common.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "dsdb/common/util.h"
30 #include "librpc/gen_ndr/ndr_dnsp.h"
31 #include "librpc/rpc/pyrpc_util.h"
33 /* FIXME: These should be in a header file somewhere */
34 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
35 if (!py_check_dcerpc_type(py_ldb, "ldb", "Ldb")) { \
36 PyErr_SetString(PyExc_TypeError, "Ldb connection object required"); \
39 ldb = pyldb_Ldb_AsLdbContext(py_ldb);
41 #define PyErr_LDB_DN_OR_RAISE(py_ldb_dn, dn) \
42 if (!py_check_dcerpc_type(py_ldb_dn, "ldb", "Dn")) { \
43 PyErr_SetString(PyExc_TypeError, "ldb Dn object required"); \
46 dn = pyldb_Dn_AsDn(py_ldb_dn);
48 static PyObject *py_dnsp_DnssrvRpcRecord_get_list(struct dnsp_DnssrvRpcRecord *records,
51 PyObject *py_dns_list;
53 py_dns_list = PyList_New(num_records);
54 if (py_dns_list == NULL) {
57 for (i = 0; i < num_records; i++) {
58 PyObject *py_dns_record;
59 py_dns_record = py_return_ndr_struct("samba.dcerpc.dnsp", "DnssrvRpcRecord", records, &records[i]);
60 PyList_SetItem(py_dns_list, i, py_dns_record);
65 static int py_dnsp_DnssrvRpcRecord_get_array(PyObject *value,
67 struct dnsp_DnssrvRpcRecord **records,
68 uint16_t *num_records)
71 struct dnsp_DnssrvRpcRecord *recs;
72 PY_CHECK_TYPE(&PyList_Type, value, return -1;);
73 recs = talloc_array(mem_ctx, struct dnsp_DnssrvRpcRecord,
74 PyList_GET_SIZE(value));
79 for (i = 0; i < PyList_GET_SIZE(value); i++) {
81 PyObject *item = PyList_GET_ITEM(value, i);
82 type_correct = py_check_dcerpc_type(item, "samba.dcerpc.dnsp", "DnssrvRpcRecord");
83 if (type_correct == false) {
86 if (talloc_reference(mem_ctx, pytalloc_get_mem_ctx(item)) == NULL) {
90 recs[i] = *(struct dnsp_DnssrvRpcRecord *)pytalloc_get_ptr(item);
93 *num_records = PyList_GET_SIZE(value);
97 static PyObject *py_dsdb_dns_lookup(PyObject *self,
98 PyObject *args, PyObject *kwargs)
100 struct ldb_context *samdb;
101 PyObject *py_ldb, *ret, *pydn;
102 PyObject *py_dns_partition = NULL;
107 struct dns_server_zone *zones_list;
108 struct ldb_dn *dn, *dns_partition = NULL;
109 struct dnsp_DnssrvRpcRecord *records;
110 uint16_t num_records;
111 const char * const kwnames[] = { "ldb", "dns_name",
112 "dns_partition", NULL };
114 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os|O",
115 discard_const_p(char *, kwnames),
117 &py_dns_partition)) {
120 PyErr_LDB_OR_RAISE(py_ldb, samdb);
122 if (py_dns_partition) {
123 PyErr_LDB_DN_OR_RAISE(py_dns_partition,
127 frame = talloc_stackframe();
129 status = dns_common_zones(samdb, frame, dns_partition,
131 if (!NT_STATUS_IS_OK(status)) {
133 PyErr_SetNTSTATUS(status);
137 werr = dns_common_name2dn(samdb, zones_list, frame, dns_name, &dn);
138 if (!W_ERROR_IS_OK(werr)) {
140 PyErr_SetWERROR(werr);
144 werr = dns_common_lookup(samdb,
150 if (!W_ERROR_IS_OK(werr)) {
152 PyErr_SetWERROR(werr);
156 ret = py_dnsp_DnssrvRpcRecord_get_list(records, num_records);
157 pydn = pyldb_Dn_FromDn(dn);
159 return Py_BuildValue("(OO)", pydn, ret);
162 static PyObject *py_dsdb_dns_extract(PyObject *self, PyObject *args)
164 struct ldb_context *samdb;
165 PyObject *py_dns_el, *ret;
166 PyObject *py_ldb = NULL;
169 struct ldb_message_element *dns_el;
170 struct dnsp_DnssrvRpcRecord *records;
171 uint16_t num_records;
173 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_dns_el)) {
177 PyErr_LDB_OR_RAISE(py_ldb, samdb);
179 if (!py_check_dcerpc_type(py_dns_el, "ldb", "MessageElement")) {
180 PyErr_SetString(PyExc_TypeError,
181 "ldb MessageElement object required");
184 dns_el = pyldb_MessageElement_AsMessageElement(py_dns_el);
186 frame = talloc_stackframe();
188 werr = dns_common_extract(samdb, dns_el,
192 if (!W_ERROR_IS_OK(werr)) {
194 PyErr_SetWERROR(werr);
198 ret = py_dnsp_DnssrvRpcRecord_get_list(records, num_records);
203 static PyObject *py_dsdb_dns_replace(PyObject *self, PyObject *args)
205 struct ldb_context *samdb;
206 PyObject *py_ldb, *py_dns_records;
212 struct dns_server_zone *zones_list;
214 struct dnsp_DnssrvRpcRecord *records;
215 uint16_t num_records;
218 * TODO: This is a shocking abuse, but matches what the
219 * internal DNS server does, it should be pushed into
220 * dns_common_replace()
222 static const int serial = 110;
224 if (!PyArg_ParseTuple(args, "OsO", &py_ldb, &dns_name, &py_dns_records)) {
227 PyErr_LDB_OR_RAISE(py_ldb, samdb);
229 frame = talloc_stackframe();
231 status = dns_common_zones(samdb, frame, NULL, &zones_list);
232 if (!NT_STATUS_IS_OK(status)) {
233 PyErr_SetNTSTATUS(status);
238 werr = dns_common_name2dn(samdb, zones_list, frame, dns_name, &dn);
239 if (!W_ERROR_IS_OK(werr)) {
240 PyErr_SetWERROR(werr);
245 ret = py_dnsp_DnssrvRpcRecord_get_array(py_dns_records,
247 &records, &num_records);
253 werr = dns_common_replace(samdb,
256 false, /* Not adding a record */
260 if (!W_ERROR_IS_OK(werr)) {
261 PyErr_SetWERROR(werr);
270 static PyObject *py_dsdb_dns_replace_by_dn(PyObject *self, PyObject *args)
272 struct ldb_context *samdb;
273 PyObject *py_ldb, *py_dn, *py_dns_records;
278 struct dnsp_DnssrvRpcRecord *records;
279 uint16_t num_records;
282 * TODO: This is a shocking abuse, but matches what the
283 * internal DNS server does, it should be pushed into
284 * dns_common_replace()
286 static const int serial = 110;
288 if (!PyArg_ParseTuple(args, "OOO", &py_ldb, &py_dn, &py_dns_records)) {
291 PyErr_LDB_OR_RAISE(py_ldb, samdb);
293 PyErr_LDB_DN_OR_RAISE(py_dn, dn);
295 frame = talloc_stackframe();
297 ret = py_dnsp_DnssrvRpcRecord_get_array(py_dns_records,
299 &records, &num_records);
305 werr = dns_common_replace(samdb,
308 false, /* Not adding a record */
312 if (!W_ERROR_IS_OK(werr)) {
313 PyErr_SetWERROR(werr);
323 static PyMethodDef py_dsdb_dns_methods[] = {
325 { "lookup", (PyCFunction)py_dsdb_dns_lookup,
326 METH_VARARGS|METH_KEYWORDS,
327 "Get the DNS database entries for a DNS name"},
328 { "replace", (PyCFunction)py_dsdb_dns_replace,
329 METH_VARARGS, "Replace the DNS database entries for a DNS name"},
330 { "replace_by_dn", (PyCFunction)py_dsdb_dns_replace_by_dn,
331 METH_VARARGS, "Replace the DNS database entries for a LDB DN"},
332 { "extract", (PyCFunction)py_dsdb_dns_extract,
333 METH_VARARGS, "Return the DNS database entry as a python structure from an Ldb.MessageElement of type dnsRecord"},
337 static struct PyModuleDef moduledef = {
338 PyModuleDef_HEAD_INIT,
339 .m_name = "dsdb_dns",
340 .m_doc = "Python bindings for the DNS objects in the directory service databases.",
342 .m_methods = py_dsdb_dns_methods,
345 MODULE_INIT_FUNC(dsdb_dns)
349 m = PyModule_Create(&moduledef);