summaryrefslogtreecommitdiffstats
path: root/src/lib/dns/python/tsigkey_python.cc
blob: 906dfc0177abbaa624be3438bcce56b3fa006e98 (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
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
// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include <new>

#include <dns/tsigkey.h>

using namespace isc::dns;
using namespace isc::dns::rdata;

//
// Definition of the classes
//

// For each class, we need a struct, a helper functions (init, destroy,
// and static wrappers around the methods we export), a list of methods,
// and a type description

namespace {
//
// TSIGKey
//

// The s_* Class simply covers one instantiation of the object

class s_TSIGKey : public PyObject {
public:
    s_TSIGKey() : tsigkey(NULL) {}
    TSIGKey* tsigkey;
};

//
// We declare the functions here, the definitions are below
// the type definition of the object, since both can use the other
//

// General creation and destruction
int TSIGKey_init(s_TSIGKey* self, PyObject* args);
void TSIGKey_destroy(s_TSIGKey* self);

// These are the functions we export
// This is a second version of toText, we need one where the argument
// is a PyObject*, for the str() function in python.
PyObject* TSIGKey_getKeyName(const s_TSIGKey* self);
PyObject* TSIGKey_getAlgorithmName(const s_TSIGKey* self);
PyObject* TSIGKey_getSecret(const s_TSIGKey* self);
PyObject* TSIGKey_toText(const s_TSIGKey* self);

// This list contains the actual set of functions we have in
// python. Each entry has
// 1. Python method name
// 2. Our static function here
// 3. Argument type
// 4. Documentation
PyMethodDef TSIGKey_methods[] = {
    { "get_key_name",
      reinterpret_cast<PyCFunction>(TSIGKey_getKeyName), METH_NOARGS,
      "Return the key name." },
    { "get_algorithm_name",
      reinterpret_cast<PyCFunction>(TSIGKey_getAlgorithmName), METH_NOARGS,
      "Return the algorithm name." },
    { "get_secret",
      reinterpret_cast<PyCFunction>(TSIGKey_getSecret), METH_NOARGS,
      "Return the value of the TSIG secret." },
    { "to_text", reinterpret_cast<PyCFunction>(TSIGKey_toText), METH_NOARGS,
      "Returns the string representation (name:secret:algorithm)" },
    { NULL, NULL, 0, NULL }
};

// This defines the complete type for reflection in python and
// parsing of PyObject* to s_EDNS
// Most of the functions are not actually implemented and NULL here.
PyTypeObject tsigkey_type = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "libdns_python.TSIGKey",
    sizeof(s_TSIGKey),                  // tp_basicsize
    0,                                  // tp_itemsize
    (destructor)TSIGKey_destroy,        // tp_dealloc
    NULL,                               // tp_print
    NULL,                               // tp_getattr
    NULL,                               // tp_setattr
    NULL,                               // tp_reserved
    NULL,                               // tp_repr
    NULL,                               // tp_as_number
    NULL,                               // tp_as_sequence
    NULL,                               // tp_as_mapping
    NULL,                               // tp_hash 
    NULL,                               // tp_call
    NULL,                               // tp_str
    NULL,                               // tp_getattro
    NULL,                               // tp_setattro
    NULL,                               // tp_as_buffer
    Py_TPFLAGS_DEFAULT,                 // tp_flags
    "The TSIGKey class holds a TSIG key along with some related attributes as "
    "defined in RFC2845.",
    NULL,                               // tp_traverse
    NULL,                               // tp_clear
    NULL,                               // tp_richcompare
    0,                                  // tp_weaklistoffset
    NULL,                               // tp_iter
    NULL,                               // tp_iternext
    TSIGKey_methods,                    // tp_methods
    NULL,                               // tp_members
    NULL,                               // tp_getset
    NULL,                               // tp_base
    NULL,                               // tp_dict
    NULL,                               // tp_descr_get
    NULL,                               // tp_descr_set
    0,                                  // tp_dictoffset
    (initproc)TSIGKey_init,             // tp_init
    NULL,                               // tp_alloc
    PyType_GenericNew,                  // tp_new
    NULL,                               // tp_free
    NULL,                               // tp_is_gc
    NULL,                               // tp_bases
    NULL,                               // tp_mro
    NULL,                               // tp_cache
    NULL,                               // tp_subclasses
    NULL,                               // tp_weaklist
    NULL,                               // tp_del
    0                                   // tp_version_tag
};

// A helper function to build a python "Name" object with error handling
// encapsulated.
s_Name*
createNameObject(const Name& source) {
    s_Name* name = PyObject_New(s_Name, &name_type);
    if (name == NULL) {
        return (NULL);
    }
    name->name = new(nothrow) Name(source);
    if (name->name == NULL) {
        Py_DECREF(name);
        PyErr_SetString(po_IscException, "Allocating Name object failed");
        return (NULL);
    }
    return (name);
}

int
TSIGKey_init(s_TSIGKey* self, PyObject* args) {
    const char* str;

    const s_Name* key_name;
    const s_Name* algorithm_name;
    PyObject* bytes_obj;
    const char* secret;
    Py_ssize_t secret_len;


    try {
        if (PyArg_ParseTuple(args, "s", &str)) {
            self->tsigkey = new TSIGKey(str);
            return (0);
        } else if (PyArg_ParseTuple(args, "O!O!O", &name_type, &key_name,
                         &name_type, &algorithm_name, &bytes_obj) &&
            PyObject_AsCharBuffer(bytes_obj, &secret, &secret_len) != -1) {
                self->tsigkey = new TSIGKey(*key_name->name,
                                            *algorithm_name->name,
                                            secret, secret_len);
            return (0);
        }
    } catch (const isc::InvalidParameter& ex) {
        PyErr_SetString(po_InvalidParameter, ex.what());
        return (-1);
    } catch (...) {
        PyErr_SetString(po_IscException, "Unexpected exception");
        return (-1);
    }

    PyErr_Clear();
    PyErr_SetString(PyExc_TypeError,
                    "Invalid arguments to TSIGKey constructor");

    return (-1);
}

void
TSIGKey_destroy(s_TSIGKey* const self) {
    delete self->tsigkey;
    self->tsigkey = NULL;
    Py_TYPE(self)->tp_free(self);
}

PyObject*
TSIGKey_getKeyName(const s_TSIGKey* const self) {
    return (createNameObject(self->tsigkey->getKeyName()));
}

PyObject*
TSIGKey_getAlgorithmName(const s_TSIGKey* const self) {
    return (createNameObject(self->tsigkey->getAlgorithmName()));
}

PyObject*
TSIGKey_getSecret(const s_TSIGKey* const self) {
    return (Py_BuildValue("y#", self->tsigkey->getSecret(),
                          self->tsigkey->getSecretLength()));
}

PyObject*
TSIGKey_toText(const s_TSIGKey* self) {
    return (Py_BuildValue("s", self->tsigkey->toText().c_str()));
}

// Module Initialization, all statics are initialized here
bool
initModulePart_TSIGKey(PyObject* mod) {
    // We initialize the static description object with PyType_Ready(),
    // then add it to the module. This is not just a check! (leaving
    // this out results in segmentation faults)
    if (PyType_Ready(&tsigkey_type) < 0) {
        return (false);
    }
    Py_INCREF(&tsigkey_type);
    void* p = &tsigkey_type;
    if (PyModule_AddObject(mod, "TSIGKey", static_cast<PyObject*>(p)) != 0) {
        Py_DECREF(&tsigkey_type);
        return (false);
    }

    s_Name* name;
    if ((name = createNameObject(TSIGKey::HMACMD5_NAME())) == NULL) {
        goto cleanup;
    }
    addClassVariable(tsigkey_type, "HMACMD5_NAME", name);
    if ((name = createNameObject(TSIGKey::HMACSHA1_NAME())) == NULL) {
        goto cleanup;
    }
    addClassVariable(tsigkey_type, "HMACSHA1_NAME", name);
    if ((name = createNameObject(TSIGKey::HMACSHA256_NAME())) == NULL) {
        goto cleanup;
    }
    addClassVariable(tsigkey_type, "HMACSHA256_NAME", name);

    return (true);

  cleanup:
    Py_DECREF(&tsigkey_type);
    return (false);
}
//
// End of TSIGKey
//

//
// TSIGKeyRing
//

// The s_* Class simply covers one instantiation of the object

// The s_* Class simply covers one instantiation of the object

class s_TSIGKeyRing : public PyObject {
public:
    s_TSIGKeyRing() : keyring(NULL) {}
    TSIGKeyRing* keyring;
};

//
// We declare the functions here, the definitions are below
// the type definition of the object, since both can use the other
//

int TSIGKeyRing_init(s_TSIGKeyRing* self, PyObject* args);
void TSIGKeyRing_destroy(s_TSIGKeyRing* self);

PyObject* TSIGKeyRing_size(const s_TSIGKeyRing* self);
PyObject* TSIGKeyRing_add(const s_TSIGKeyRing* self, PyObject* args);
PyObject* TSIGKeyRing_remove(const s_TSIGKeyRing* self, PyObject* args);
PyObject* TSIGKeyRing_find(const s_TSIGKeyRing* self, PyObject* args);

PyMethodDef TSIGKeyRing_methods[] = {
    { "size", reinterpret_cast<PyCFunction>(TSIGKeyRing_size), METH_NOARGS,
      "Return the number of keys stored in the TSIGKeyRing." },
    { "add", reinterpret_cast<PyCFunction>(TSIGKeyRing_add), METH_VARARGS,
      "Add a TSIGKey to the TSIGKeyRing." },
    { "remove", reinterpret_cast<PyCFunction>(TSIGKeyRing_remove),
      METH_VARARGS,
      "Remove a TSIGKey for the given name from the TSIGKeyRing." },
    { "find", reinterpret_cast<PyCFunction>(TSIGKeyRing_find), METH_VARARGS,
      "Find a TSIGKey for the given name in the TSIGKeyRing. "
      "It returns a tuple of (result_code, key)." },
    { NULL, NULL, 0, NULL }
};

PyTypeObject tsigkeyring_type = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "libdns_python.TSIGKeyRing",
    sizeof(s_TSIGKeyRing),              // tp_basicsize
    0,                                  // tp_itemsize
    (destructor)TSIGKeyRing_destroy,    // tp_dealloc
    NULL,                               // tp_print
    NULL,                               // tp_getattr
    NULL,                               // tp_setattr
    NULL,                               // tp_reserved
    NULL,                               // tp_repr
    NULL,                               // tp_as_number
    NULL,                               // tp_as_sequence
    NULL,                               // tp_as_mapping
    NULL,                               // tp_hash 
    NULL,                               // tp_call
    NULL,                               // tp_str
    NULL,                               // tp_getattro
    NULL,                               // tp_setattro
    NULL,                               // tp_as_buffer
    Py_TPFLAGS_DEFAULT,                 // tp_flags
    "A simple repository of a set of TSIGKey objects.",
    NULL,                               // tp_traverse
    NULL,                               // tp_clear
    NULL,                               // tp_richcompare
    0,                                  // tp_weaklistoffset
    NULL,                               // tp_iter
    NULL,                               // tp_iternext
    TSIGKeyRing_methods,                // tp_methods
    NULL,                               // tp_members
    NULL,                               // tp_getset
    NULL,                               // tp_base
    NULL,                               // tp_dict
    NULL,                               // tp_descr_get
    NULL,                               // tp_descr_set
    0,                                  // tp_dictoffset
    (initproc)TSIGKeyRing_init,         // tp_init
    NULL,                               // tp_alloc
    PyType_GenericNew,                  // tp_new
    NULL,                               // tp_free
    NULL,                               // tp_is_gc
    NULL,                               // tp_bases
    NULL,                               // tp_mro
    NULL,                               // tp_cache
    NULL,                               // tp_subclasses
    NULL,                               // tp_weaklist
    NULL,                               // tp_del
    0                                   // tp_version_tag
};

int
TSIGKeyRing_init(s_TSIGKeyRing* self, PyObject* args) {
    if (!PyArg_ParseTuple(args, "")) {
        PyErr_Clear();
        PyErr_SetString(PyExc_TypeError,
                        "Invalid arguments to TSIGKeyRing constructor");
        return (-1);
    }
    
    self->keyring = new(nothrow) TSIGKeyRing();
    if (self->keyring == NULL) {
        PyErr_SetString(po_IscException, "Allocating TSIGKeyRing failed");
        return (-1);
    }

    return (0);
}

void
TSIGKeyRing_destroy(s_TSIGKeyRing* self) {
    delete self->keyring;
    self->keyring = NULL;
    Py_TYPE(self)->tp_free(self);
}

PyObject*
TSIGKeyRing_size(const s_TSIGKeyRing* const self) {
    return (Py_BuildValue("I", self->keyring->size()));
}

PyObject*
TSIGKeyRing_add(const s_TSIGKeyRing* const self, PyObject* args) {
    s_TSIGKey* tsigkey;
    
    if (PyArg_ParseTuple(args, "O!", &tsigkey_type, &tsigkey)) {
        try {
            const TSIGKeyRing::Result result =
                self->keyring->add(*tsigkey->tsigkey);
            return (Py_BuildValue("I", result));
        } catch (...) {
            PyErr_SetString(po_IscException, "Unexpected exception");
            return (NULL);
        }
    }

    PyErr_Clear();
    PyErr_SetString(PyExc_TypeError, "Invalid arguments to TSIGKeyRing.add");

    return (NULL);
}

PyObject*
TSIGKeyRing_remove(const s_TSIGKeyRing* self, PyObject* args) {
    s_Name* key_name;

    if (PyArg_ParseTuple(args, "O!", &name_type, &key_name)) {
        const TSIGKeyRing::Result result =
            self->keyring->remove(*key_name->name);
        return (Py_BuildValue("I", result));
    }

    PyErr_Clear();
    PyErr_SetString(PyExc_TypeError, "Invalid arguments to TSIGKeyRing.add");

    return (NULL);
}

PyObject*
TSIGKeyRing_find(const s_TSIGKeyRing* self, PyObject* args) {
    s_Name* key_name;

    if (PyArg_ParseTuple(args, "O!", &name_type, &key_name)) {
        const TSIGKeyRing::FindResult result =
            self->keyring->find(*key_name->name);
        if (result.key != NULL) {
            s_TSIGKey* key = PyObject_New(s_TSIGKey, &tsigkey_type);
            if (key == NULL) {
                return (NULL);
            }
            key->tsigkey = new(nothrow) TSIGKey(*result.key);
            if (key->tsigkey == NULL) {
                Py_DECREF(key);
                PyErr_SetString(po_IscException,
                                "Allocating TSIGKey object failed");
                return (NULL);
            }
            return (Py_BuildValue("IN", result.code, key));
        } else {
            return (Py_BuildValue("Is", result.code, NULL));
        }
    }

    return (NULL);
}

bool
initModulePart_TSIGKeyRing(PyObject* mod) {
    if (PyType_Ready(&tsigkeyring_type) < 0) {
        return (false);
    }
    Py_INCREF(&tsigkeyring_type);
    void* p = &tsigkeyring_type;
    if (PyModule_AddObject(mod, "TSIGKeyRing",
                           static_cast<PyObject*>(p)) != 0) {
        Py_DECREF(&tsigkeyring_type);
        return (false);
    }

    addClassVariable(tsigkeyring_type, "SUCCESS",
                     Py_BuildValue("I", TSIGKeyRing::SUCCESS));
    addClassVariable(tsigkeyring_type, "EXIST",
                     Py_BuildValue("I", TSIGKeyRing::EXIST));
    addClassVariable(tsigkeyring_type, "NOTFOUND",
                     Py_BuildValue("I", TSIGKeyRing::NOTFOUND));

    return (true);
}

} // end of unnamed namespace