tsig_python.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #define PY_SSIZE_T_CLEAN // need for "y#" below
  15. #include <Python.h>
  16. #include <string>
  17. #include <stdexcept>
  18. #include <exceptions/exceptions.h>
  19. #include <util/python/pycppwrapper_util.h>
  20. #include <dns/tsig.h>
  21. #include "pydnspp_common.h"
  22. #include "name_python.h"
  23. #include "tsigkey_python.h"
  24. #include "tsigerror_python.h"
  25. #include "tsigrecord_python.h"
  26. #include "tsig_python.h"
  27. using namespace std;
  28. using namespace isc;
  29. using namespace isc::util::python;
  30. using namespace isc::dns;
  31. using namespace isc::dns::python;
  32. //
  33. // Definition of the classes
  34. //
  35. // For each class, we need a struct, a helper functions (init, destroy,
  36. // and static wrappers around the methods we export), a list of methods,
  37. // and a type description
  38. //
  39. // TSIGContext
  40. //
  41. // Trivial constructor.
  42. s_TSIGContext::s_TSIGContext() : cppobj(NULL) {
  43. }
  44. namespace {
  45. // Shortcut type which would be convenient for adding class variables safely.
  46. typedef CPPPyObjectContainer<s_TSIGContext, TSIGContext> TSIGContextContainer;
  47. //
  48. // We declare the functions here, the definitions are below
  49. // the type definition of the object, since both can use the other
  50. //
  51. // General creation and destruction
  52. int TSIGContext_init(s_TSIGContext* self, PyObject* args);
  53. void TSIGContext_destroy(s_TSIGContext* self);
  54. // Class specific methods
  55. PyObject* TSIGContext_getState(s_TSIGContext* self);
  56. PyObject* TSIGContext_getError(s_TSIGContext* self);
  57. PyObject* TSIGContext_sign(s_TSIGContext* self, PyObject* args);
  58. PyObject* TSIGContext_verify(s_TSIGContext* self, PyObject* args);
  59. // These are the functions we export
  60. // For a minimal support, we don't need them.
  61. // This list contains the actual set of functions we have in
  62. // python. Each entry has
  63. // 1. Python method name
  64. // 2. Our static function here
  65. // 3. Argument type
  66. // 4. Documentation
  67. PyMethodDef TSIGContext_methods[] = {
  68. { "get_state", reinterpret_cast<PyCFunction>(TSIGContext_getState),
  69. METH_NOARGS,
  70. "Return the current state of the context (mainly for tests)" },
  71. { "get_error", reinterpret_cast<PyCFunction>(TSIGContext_getError),
  72. METH_NOARGS,
  73. "Return the TSIG error as a result of the latest verification" },
  74. { "sign",
  75. reinterpret_cast<PyCFunction>(TSIGContext_sign), METH_VARARGS,
  76. "Sign a DNS message." },
  77. { "verify",
  78. reinterpret_cast<PyCFunction>(TSIGContext_verify), METH_VARARGS,
  79. "Verify a DNS message." },
  80. { NULL, NULL, 0, NULL }
  81. };
  82. int
  83. TSIGContext_init(s_TSIGContext* self, PyObject* args) {
  84. try {
  85. // "From key" constructor
  86. const s_TSIGKey* tsigkey_obj;
  87. if (PyArg_ParseTuple(args, "O!", &tsigkey_type, &tsigkey_obj)) {
  88. self->cppobj = new TSIGContext(*tsigkey_obj->cppobj);
  89. return (0);
  90. }
  91. // "From key param + keyring" constructor
  92. PyErr_Clear();
  93. const s_Name* keyname_obj;
  94. const s_Name* algname_obj;
  95. const s_TSIGKeyRing* keyring_obj;
  96. if (PyArg_ParseTuple(args, "O!O!O!", &name_type, &keyname_obj,
  97. &name_type, &algname_obj, &tsigkeyring_type,
  98. &keyring_obj)) {
  99. self->cppobj = new TSIGContext(*keyname_obj->cppobj,
  100. *algname_obj->cppobj,
  101. *keyring_obj->cppobj);
  102. return (0);
  103. }
  104. } catch (const exception& ex) {
  105. const string ex_what = "Failed to construct TSIGContext object: " +
  106. string(ex.what());
  107. PyErr_SetString(po_IscException, ex_what.c_str());
  108. return (-1);
  109. } catch (...) {
  110. PyErr_SetString(po_IscException,
  111. "Unexpected exception in constructing TSIGContext");
  112. return (-1);
  113. }
  114. PyErr_SetString(PyExc_TypeError,
  115. "Invalid arguments to TSIGContext constructor");
  116. return (-1);
  117. }
  118. void
  119. TSIGContext_destroy(s_TSIGContext* const self) {
  120. delete self->cppobj;
  121. self->cppobj = NULL;
  122. Py_TYPE(self)->tp_free(self);
  123. }
  124. PyObject*
  125. TSIGContext_getState(s_TSIGContext* self) {
  126. return (Py_BuildValue("I", self->cppobj->getState()));
  127. }
  128. PyObject*
  129. TSIGContext_getError(s_TSIGContext* self) {
  130. try {
  131. PyObjectContainer container(createTSIGErrorObject(
  132. self->cppobj->getError()));
  133. return (Py_BuildValue("O", container.get()));
  134. } catch (const exception& ex) {
  135. const string ex_what =
  136. "Unexpectedly failed to get TSIGContext error: " +
  137. string(ex.what());
  138. PyErr_SetString(po_IscException, ex_what.c_str());
  139. } catch (...) {
  140. PyErr_SetString(po_IscException,
  141. "Unexpected exception in TSIGContext.get_error");
  142. }
  143. return (NULL);
  144. }
  145. PyObject*
  146. TSIGContext_sign(s_TSIGContext* self, PyObject* args) {
  147. long qid = 0;
  148. const char* mac;
  149. Py_ssize_t mac_size;
  150. if (PyArg_ParseTuple(args, "ly#", &qid, &mac, &mac_size)) {
  151. if (qid < 0 || qid > 0xffff) {
  152. PyErr_SetString(PyExc_ValueError,
  153. "TSIGContext.sign: QID out of range");
  154. return (NULL);
  155. }
  156. try {
  157. ConstTSIGRecordPtr record = self->cppobj->sign(qid, mac, mac_size);
  158. return (createTSIGRecordObject(*record));
  159. } catch (const TSIGContextError& ex) {
  160. PyErr_SetString(po_TSIGContextError, ex.what());
  161. } catch (const exception& ex) {
  162. const string ex_what = "Unexpected failure in TSIG sign: " +
  163. string(ex.what());
  164. PyErr_SetString(po_IscException, ex_what.c_str());
  165. } catch (...) {
  166. PyErr_SetString(PyExc_SystemError,
  167. "Unexpected failure in TSIG sign");
  168. }
  169. } else {
  170. PyErr_SetString(PyExc_TypeError,
  171. "Invalid arguments to TSIGContext.sign");
  172. }
  173. return (NULL);
  174. }
  175. PyObject*
  176. TSIGContext_verify(s_TSIGContext* self, PyObject* args) {
  177. const char* data;
  178. Py_ssize_t data_len;
  179. s_TSIGRecord* py_record;
  180. PyObject* py_maybe_none;
  181. TSIGRecord* record;
  182. if (PyArg_ParseTuple(args, "O!y#", &tsigrecord_type, &py_record,
  183. &data, &data_len)) {
  184. record = py_record->cppobj;
  185. } else if (PyArg_ParseTuple(args, "Oy#", &py_maybe_none, &data,
  186. &data_len)) {
  187. record = NULL;
  188. } else {
  189. PyErr_SetString(PyExc_TypeError,
  190. "Invalid arguments to TSIGContext.verify");
  191. return (NULL);
  192. }
  193. PyErr_Clear();
  194. try {
  195. const TSIGError error = self->cppobj->verify(record, data, data_len);
  196. return (createTSIGErrorObject(error));
  197. } catch (const TSIGContextError& ex) {
  198. PyErr_SetString(po_TSIGContextError, ex.what());
  199. } catch (const InvalidParameter& ex) {
  200. PyErr_SetString(po_InvalidParameter, ex.what());
  201. } catch (const exception& ex) {
  202. const string ex_what = "Unexpected failure in TSIG verify: " +
  203. string(ex.what());
  204. PyErr_SetString(po_IscException, ex_what.c_str());
  205. } catch (...) {
  206. PyErr_SetString(PyExc_SystemError, "Unexpected failure in TSIG verify");
  207. }
  208. return (NULL);
  209. }
  210. } // end of unnamed namespace
  211. namespace isc {
  212. namespace dns {
  213. namespace python {
  214. // Definition of class specific exception(s)
  215. PyObject* po_TSIGContextError;
  216. // This defines the complete type for reflection in python and
  217. // parsing of PyObject* to s_TSIGContext
  218. // Most of the functions are not actually implemented and NULL here.
  219. PyTypeObject tsigcontext_type = {
  220. PyVarObject_HEAD_INIT(NULL, 0)
  221. "pydnspp.TSIGContext",
  222. sizeof(s_TSIGContext), // tp_basicsize
  223. 0, // tp_itemsize
  224. reinterpret_cast<destructor>(TSIGContext_destroy), // tp_dealloc
  225. NULL, // tp_print
  226. NULL, // tp_getattr
  227. NULL, // tp_setattr
  228. NULL, // tp_reserved
  229. NULL, // tp_repr
  230. NULL, // tp_as_number
  231. NULL, // tp_as_sequence
  232. NULL, // tp_as_mapping
  233. NULL, // tp_hash
  234. NULL, // tp_call
  235. NULL, // tp_str
  236. NULL, // tp_getattro
  237. NULL, // tp_setattro
  238. NULL, // tp_as_buffer
  239. Py_TPFLAGS_DEFAULT, // tp_flags
  240. "The TSIGContext class objects is...(COMPLETE THIS)",
  241. NULL, // tp_traverse
  242. NULL, // tp_clear
  243. NULL, // tp_richcompare
  244. 0, // tp_weaklistoffset
  245. NULL, // tp_iter
  246. NULL, // tp_iternext
  247. TSIGContext_methods, // tp_methods
  248. NULL, // tp_members
  249. NULL, // tp_getset
  250. NULL, // tp_base
  251. NULL, // tp_dict
  252. NULL, // tp_descr_get
  253. NULL, // tp_descr_set
  254. 0, // tp_dictoffset
  255. reinterpret_cast<initproc>(TSIGContext_init), // tp_init
  256. NULL, // tp_alloc
  257. PyType_GenericNew, // tp_new
  258. NULL, // tp_free
  259. NULL, // tp_is_gc
  260. NULL, // tp_bases
  261. NULL, // tp_mro
  262. NULL, // tp_cache
  263. NULL, // tp_subclasses
  264. NULL, // tp_weaklist
  265. NULL, // tp_del
  266. 0 // tp_version_tag
  267. };
  268. // Module Initialization, all statics are initialized here
  269. bool
  270. initModulePart_TSIGContext(PyObject* mod) {
  271. // We initialize the static description object with PyType_Ready(),
  272. // then add it to the module. This is not just a check! (leaving
  273. // this out results in segmentation faults)
  274. if (PyType_Ready(&tsigcontext_type) < 0) {
  275. return (false);
  276. }
  277. void* p = &tsigcontext_type;
  278. if (PyModule_AddObject(mod, "TSIGContext",
  279. static_cast<PyObject*>(p)) < 0) {
  280. return (false);
  281. }
  282. Py_INCREF(&tsigcontext_type);
  283. try {
  284. // Class specific exceptions
  285. po_TSIGContextError = PyErr_NewException("pydnspp.TSIGContextError",
  286. po_IscException, NULL);
  287. PyObjectContainer(po_TSIGContextError).installToModule(
  288. mod, "TSIGContextError");
  289. // Constant class variables
  290. installClassVariable(tsigcontext_type, "STATE_INIT",
  291. Py_BuildValue("I", TSIGContext::INIT));
  292. installClassVariable(tsigcontext_type, "STATE_SENT_REQUEST",
  293. Py_BuildValue("I", TSIGContext::SENT_REQUEST));
  294. installClassVariable(tsigcontext_type, "STATE_RECEIVED_REQUEST",
  295. Py_BuildValue("I", TSIGContext::RECEIVED_REQUEST));
  296. installClassVariable(tsigcontext_type, "STATE_SENT_RESPONSE",
  297. Py_BuildValue("I", TSIGContext::SENT_RESPONSE));
  298. installClassVariable(tsigcontext_type, "STATE_VERIFIED_RESPONSE",
  299. Py_BuildValue("I",
  300. TSIGContext::VERIFIED_RESPONSE));
  301. installClassVariable(tsigcontext_type, "DEFAULT_FUDGE",
  302. Py_BuildValue("H", TSIGContext::DEFAULT_FUDGE));
  303. } catch (const exception& ex) {
  304. const string ex_what =
  305. "Unexpected failure in TSIGContext initialization: " +
  306. string(ex.what());
  307. PyErr_SetString(po_IscException, ex_what.c_str());
  308. return (false);
  309. } catch (...) {
  310. PyErr_SetString(PyExc_SystemError,
  311. "Unexpected failure in TSIGContext initialization");
  312. return (false);
  313. }
  314. return (true);
  315. }
  316. } // namespace python
  317. } // namespace dns
  318. } // namespace isc