56016af312718705645bd587076f96c43078654a
[samba.git] / source4 / auth / gensec / pygensec.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2009
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <Python.h>
20 #include "includes.h"
21 #include "param/pyparam.h"
22 #include "auth/gensec/gensec.h"
23 #include "auth/gensec/gensec_internal.h" /* TODO: remove this */
24 #include "auth/credentials/pycredentials.h"
25 #include "libcli/util/pyerrors.h"
26 #include "python/modules.h"
27 #include <pytalloc.h>
28 #include <tevent.h>
29 #include "librpc/rpc/pyrpc_util.h"
30
31 static PyObject *py_get_name_by_authtype(PyObject *self, PyObject *args)
32 {
33         int type;
34         const char *name;
35         struct gensec_security *security;
36
37         if (!PyArg_ParseTuple(args, "i", &type))
38                 return NULL;
39
40         security = pytalloc_get_type(self, struct gensec_security);
41
42         name = gensec_get_name_by_authtype(security, type);
43         if (name == NULL)
44                 Py_RETURN_NONE;
45
46         return PyString_FromString(name);
47 }
48
49 static struct gensec_settings *settings_from_object(TALLOC_CTX *mem_ctx, PyObject *object)
50 {
51         struct gensec_settings *s;
52         PyObject *py_hostname, *py_lp_ctx;
53
54         if (!PyDict_Check(object)) {
55                 PyErr_SetString(PyExc_ValueError, "settings should be a dictionary");
56                 return NULL;
57         }
58
59         s = talloc_zero(mem_ctx, struct gensec_settings);
60         if (!s) return NULL;
61
62         py_hostname = PyDict_GetItemString(object, "target_hostname");
63         if (!py_hostname) {
64                 PyErr_SetString(PyExc_ValueError, "settings.target_hostname not found");
65                 return NULL;
66         }
67
68         py_lp_ctx = PyDict_GetItemString(object, "lp_ctx");
69         if (!py_lp_ctx) {
70                 PyErr_SetString(PyExc_ValueError, "settings.lp_ctx not found");
71                 return NULL;
72         }
73
74         s->target_hostname = PyString_AsString(py_hostname);
75         s->lp_ctx = lpcfg_from_py_object(s, py_lp_ctx);
76         return s;
77 }
78
79 static PyObject *py_gensec_start_client(PyTypeObject *type, PyObject *args, PyObject *kwargs)
80 {
81         NTSTATUS status;
82         PyObject *self;
83         struct gensec_settings *settings;
84         const char *kwnames[] = { "settings", NULL };
85         PyObject *py_settings = Py_None;
86         struct gensec_security *gensec;
87         TALLOC_CTX *frame;
88
89         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", discard_const_p(char *, kwnames), &py_settings))
90                 return NULL;
91
92         frame = talloc_stackframe();
93
94         if (py_settings != Py_None) {
95                 settings = settings_from_object(frame, py_settings);
96                 if (settings == NULL) {
97                         PyErr_NoMemory();
98                         TALLOC_FREE(frame);
99                         return NULL;
100                 }
101         } else {
102                 settings = talloc_zero(frame, struct gensec_settings);
103                 if (settings == NULL) {
104                         PyErr_NoMemory();
105                         TALLOC_FREE(frame);
106                         return NULL;
107                 }
108
109                 settings->lp_ctx = loadparm_init_global(true);
110                 if (settings->lp_ctx == NULL) {
111                         PyErr_NoMemory();
112                         TALLOC_FREE(frame);
113                         return NULL;
114                 }
115         }
116
117         status = gensec_init();
118         if (!NT_STATUS_IS_OK(status)) {
119                 PyErr_SetNTSTATUS(status);
120                 TALLOC_FREE(frame);
121                 return NULL;
122         }
123
124         status = gensec_client_start(frame, &gensec, settings);
125         if (!NT_STATUS_IS_OK(status)) {
126                 PyErr_SetNTSTATUS(status);
127                 TALLOC_FREE(frame);
128                 return NULL;
129         }
130
131         self = pytalloc_steal(type, gensec);
132         TALLOC_FREE(frame);
133
134         return (PyObject *)self;
135 }
136
137 static PyObject *py_gensec_start_server(PyTypeObject *type, PyObject *args, PyObject *kwargs)
138 {
139         NTSTATUS status;
140         PyObject *self;
141         struct gensec_settings *settings = NULL;
142         const char *kwnames[] = { "settings", "auth_context", NULL };
143         PyObject *py_settings = Py_None;
144         PyObject *py_auth_context = Py_None;
145         struct gensec_security *gensec;
146         struct auth4_context *auth_context = NULL;
147         TALLOC_CTX *frame;
148
149         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", discard_const_p(char *, kwnames), &py_settings, &py_auth_context))
150                 return NULL;
151
152         frame = talloc_stackframe();
153
154         if (py_settings != Py_None) {
155                 settings = settings_from_object(frame, py_settings);
156                 if (settings == NULL) {
157                         PyErr_NoMemory();
158                         TALLOC_FREE(frame);
159                         return NULL;
160                 }
161         } else {
162                 settings = talloc_zero(frame, struct gensec_settings);
163                 if (settings == NULL) {
164                         PyErr_NoMemory();
165                         TALLOC_FREE(frame);
166                         return NULL;
167                 }
168
169                 settings->lp_ctx = loadparm_init_global(true);
170                 if (settings->lp_ctx == NULL) {
171                         PyErr_NoMemory();
172                         TALLOC_FREE(frame);
173                         return NULL;
174                 }
175         }
176
177         if (py_auth_context != Py_None) {
178                 auth_context = pytalloc_get_type(py_auth_context, struct auth4_context);
179                 if (!auth_context) {
180                         PyErr_Format(PyExc_TypeError,
181                                      "Expected auth.AuthContext for auth_context argument, got %s",
182                                      talloc_get_name(pytalloc_get_ptr(py_auth_context)));
183                         return NULL;
184                 }
185         }
186
187         status = gensec_init();
188         if (!NT_STATUS_IS_OK(status)) {
189                 PyErr_SetNTSTATUS(status);
190                 TALLOC_FREE(frame);
191                 return NULL;
192         }
193
194         status = gensec_server_start(frame, settings, auth_context, &gensec);
195         if (!NT_STATUS_IS_OK(status)) {
196                 PyErr_SetNTSTATUS(status);
197                 TALLOC_FREE(frame);
198                 return NULL;
199         }
200
201         self = pytalloc_steal(type, gensec);
202         TALLOC_FREE(frame);
203
204         return self;
205 }
206
207 static PyObject *py_gensec_set_target_hostname(PyObject *self, PyObject *args)
208 {
209         struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
210         char *target_hostname;
211         NTSTATUS status;
212
213         if (!PyArg_ParseTuple(args, "s", &target_hostname))
214                 return NULL;
215
216         status = gensec_set_target_hostname(security, target_hostname);
217         if (!NT_STATUS_IS_OK(status)) {
218                 PyErr_SetNTSTATUS(status);
219                 return NULL;
220         }
221         
222         Py_RETURN_NONE;
223 }
224
225 static PyObject *py_gensec_set_target_service(PyObject *self, PyObject *args)
226 {
227         struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
228         char *target_service;
229         NTSTATUS status;
230
231         if (!PyArg_ParseTuple(args, "s", &target_service))
232                 return NULL;
233
234         status = gensec_set_target_service(security, target_service);
235         if (!NT_STATUS_IS_OK(status)) {
236                 PyErr_SetNTSTATUS(status);
237                 return NULL;
238         }
239         
240         Py_RETURN_NONE;
241 }
242
243 static PyObject *py_gensec_set_credentials(PyObject *self, PyObject *args)
244 {
245         PyObject *py_creds = Py_None;
246         struct cli_credentials *creds;
247         struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
248         NTSTATUS status;
249
250         if (!PyArg_ParseTuple(args, "O", &py_creds))
251                 return NULL;
252
253         creds = PyCredentials_AsCliCredentials(py_creds);
254         if (!creds) {
255                 PyErr_Format(PyExc_TypeError,
256                              "Expected samba.credentaials for credentials argument got  %s",
257                              talloc_get_name(pytalloc_get_ptr(py_creds)));
258                 return NULL;
259         }
260
261         status = gensec_set_credentials(security, creds);
262         if (!NT_STATUS_IS_OK(status)) {
263                 PyErr_SetNTSTATUS(status);
264                 return NULL;
265         }
266
267         Py_RETURN_NONE;
268 }
269
270 static PyObject *py_gensec_session_info(PyObject *self)
271 {
272         TALLOC_CTX *mem_ctx;
273         NTSTATUS status;
274         PyObject *py_session_info;
275         struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
276         struct auth_session_info *info;
277         if (security->ops == NULL) {
278                 PyErr_SetString(PyExc_RuntimeError, "no mechanism selected");
279                 return NULL;
280         }
281         mem_ctx = talloc_new(NULL);
282
283         status = gensec_session_info(security, mem_ctx, &info);
284         if (NT_STATUS_IS_ERR(status)) {
285                 PyErr_SetNTSTATUS(status);
286                 return NULL;
287         }
288
289         py_session_info = py_return_ndr_struct("samba.dcerpc.auth", "session_info",
290                                                  info, info);
291         talloc_free(mem_ctx);
292         return py_session_info;
293 }
294
295 static PyObject *py_gensec_session_key(PyObject *self)
296 {
297         TALLOC_CTX *mem_ctx;
298         NTSTATUS status;
299         struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
300         DATA_BLOB session_key = data_blob_null;
301         static PyObject *session_key_obj = NULL;
302
303         if (security->ops == NULL) {
304                 PyErr_SetString(PyExc_RuntimeError, "no mechanism selected");
305                 return NULL;
306         }
307         mem_ctx = talloc_new(NULL);
308
309         status = gensec_session_key(security, mem_ctx, &session_key);
310         if (!NT_STATUS_IS_OK(status)) {
311                 talloc_free(mem_ctx);
312                 PyErr_SetNTSTATUS(status);
313                 return NULL;
314         }
315
316         session_key_obj = PyString_FromStringAndSize((const char *)session_key.data,
317                                                      session_key.length);
318         talloc_free(mem_ctx);
319         return session_key_obj;
320 }
321
322 static PyObject *py_gensec_start_mech_by_name(PyObject *self, PyObject *args)
323 {
324         char *name;
325         struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
326         NTSTATUS status;
327
328         if (!PyArg_ParseTuple(args, "s", &name))
329                 return NULL;
330
331         status = gensec_start_mech_by_name(security, name);
332         if (!NT_STATUS_IS_OK(status)) {
333                 PyErr_SetNTSTATUS(status);
334                 return NULL;
335         }
336
337         Py_RETURN_NONE;
338 }
339
340 static PyObject *py_gensec_start_mech_by_sasl_name(PyObject *self, PyObject *args)
341 {
342         char *sasl_name;
343         struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
344         NTSTATUS status;
345
346         if (!PyArg_ParseTuple(args, "s", &sasl_name))
347                 return NULL;
348
349         status = gensec_start_mech_by_sasl_name(security, sasl_name);
350         if (!NT_STATUS_IS_OK(status)) {
351                 PyErr_SetNTSTATUS(status);
352                 return NULL;
353         }
354
355         Py_RETURN_NONE;
356 }
357
358 static PyObject *py_gensec_start_mech_by_authtype(PyObject *self, PyObject *args)
359 {
360         int authtype, level;
361         struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
362         NTSTATUS status;
363         if (!PyArg_ParseTuple(args, "ii", &authtype, &level))
364                 return NULL;
365
366         status = gensec_start_mech_by_authtype(security, authtype, level);
367         if (!NT_STATUS_IS_OK(status)) {
368                 PyErr_SetNTSTATUS(status);
369                 return NULL;
370         }
371
372         Py_RETURN_NONE;
373 }
374
375 static PyObject *py_gensec_want_feature(PyObject *self, PyObject *args)
376 {
377         int feature;
378         struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
379         /* This is i (and declared as an int above) by design, as they are handled as an integer in python */
380         if (!PyArg_ParseTuple(args, "i", &feature))
381                 return NULL;
382
383         gensec_want_feature(security, feature);
384
385         Py_RETURN_NONE;
386 }
387
388 static PyObject *py_gensec_have_feature(PyObject *self, PyObject *args)
389 {
390         int feature;
391         struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
392         /* This is i (and declared as an int above) by design, as they are handled as an integer in python */
393         if (!PyArg_ParseTuple(args, "i", &feature))
394                 return NULL;
395
396         if (gensec_have_feature(security, feature)) {
397                 return Py_True;
398         } 
399         return Py_False;
400 }
401
402 static PyObject *py_gensec_set_max_update_size(PyObject *self, PyObject *args)
403 {
404         struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
405         unsigned int max_update_size = 0;
406
407         if (!PyArg_ParseTuple(args, "I", &max_update_size))
408                 return NULL;
409
410         gensec_set_max_update_size(security, max_update_size);
411
412         Py_RETURN_NONE;
413 }
414
415 static PyObject *py_gensec_max_update_size(PyObject *self)
416 {
417         struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
418         unsigned int max_update_size = gensec_max_update_size(security);
419
420         return PyInt_FromLong(max_update_size);
421 }
422
423 static PyObject *py_gensec_update(PyObject *self, PyObject *args)
424 {
425         NTSTATUS status;
426         TALLOC_CTX *mem_ctx;
427         DATA_BLOB in, out;
428         PyObject *ret, *py_in;
429         struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
430         PyObject *finished_processing;
431
432         if (!PyArg_ParseTuple(args, "O", &py_in))
433                 return NULL;
434
435         mem_ctx = talloc_new(NULL);
436
437         if (!PyString_Check(py_in)) {
438                 PyErr_Format(PyExc_TypeError, "expected a string");
439                 return NULL;
440         }
441
442         in.data = (uint8_t *)PyString_AsString(py_in);
443         in.length = PyString_Size(py_in);
444
445         status = gensec_update(security, mem_ctx, in, &out);
446
447         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)
448             && !NT_STATUS_IS_OK(status)) {
449                 PyErr_SetNTSTATUS(status);
450                 talloc_free(mem_ctx);
451                 return NULL;
452         }
453         ret = PyString_FromStringAndSize((const char *)out.data, out.length);
454         talloc_free(mem_ctx);
455
456         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
457                 finished_processing = Py_False;
458         } else {
459                 finished_processing = Py_True;
460         }
461
462         return PyTuple_Pack(2, finished_processing, ret);
463 }
464
465 static PyObject *py_gensec_wrap(PyObject *self, PyObject *args)
466 {
467         NTSTATUS status;
468
469         TALLOC_CTX *mem_ctx;
470         DATA_BLOB in, out;
471         PyObject *ret, *py_in;
472         struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
473
474         if (!PyArg_ParseTuple(args, "O", &py_in))
475                 return NULL;
476
477         mem_ctx = talloc_new(NULL);
478
479         if (!PyString_Check(py_in)) {
480                 PyErr_Format(PyExc_TypeError, "expected a string");
481                 return NULL;
482         }
483         in.data = (uint8_t *)PyString_AsString(py_in);
484         in.length = PyString_Size(py_in);
485
486         status = gensec_wrap(security, mem_ctx, &in, &out);
487
488         if (!NT_STATUS_IS_OK(status)) {
489                 PyErr_SetNTSTATUS(status);
490                 talloc_free(mem_ctx);
491                 return NULL;
492         }
493
494         ret = PyString_FromStringAndSize((const char *)out.data, out.length);
495         talloc_free(mem_ctx);
496         return ret;
497 }
498
499 static PyObject *py_gensec_unwrap(PyObject *self, PyObject *args)
500 {
501         NTSTATUS status;
502
503         TALLOC_CTX *mem_ctx;
504         DATA_BLOB in, out;
505         PyObject *ret, *py_in;
506         struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
507
508         if (!PyArg_ParseTuple(args, "O", &py_in))
509                 return NULL;
510
511         mem_ctx = talloc_new(NULL);
512
513         if (!PyString_Check(py_in)) {
514                 PyErr_Format(PyExc_TypeError, "expected a string");
515                 return NULL;
516         }
517
518         in.data = (uint8_t *)PyString_AsString(py_in);
519         in.length = PyString_Size(py_in);
520
521         status = gensec_unwrap(security, mem_ctx, &in, &out);
522
523         if (!NT_STATUS_IS_OK(status)) {
524                 PyErr_SetNTSTATUS(status);
525                 talloc_free(mem_ctx);
526                 return NULL;
527         }
528
529         ret = PyString_FromStringAndSize((const char *)out.data, out.length);
530         talloc_free(mem_ctx);
531         return ret;
532 }
533
534 static PyObject *py_gensec_sig_size(PyObject *self, PyObject *args)
535 {
536         struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
537         Py_ssize_t data_size = 0;
538         size_t sig_size = 0;
539
540         if (!PyArg_ParseTuple(args, "n", &data_size)) {
541                 return NULL;
542         }
543
544         sig_size = gensec_sig_size(security, data_size);
545
546         return PyLong_FromSize_t(sig_size);
547 }
548
549 static PyObject *py_gensec_sign_packet(PyObject *self, PyObject *args)
550 {
551         NTSTATUS status;
552         TALLOC_CTX *mem_ctx = NULL;
553         Py_ssize_t data_length = 0;
554         Py_ssize_t pdu_length = 0;
555         DATA_BLOB data, pdu, sig;
556         PyObject *py_sig;
557         struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
558
559         if (!PyArg_ParseTuple(args, "z#z#", &data.data, &data_length, &pdu.data, &pdu_length)) {
560                 return NULL;
561         }
562         data.length = data_length;
563         pdu.length = pdu_length;
564
565         mem_ctx = talloc_new(NULL);
566
567         status = gensec_sign_packet(security, mem_ctx,
568                                     data.data, data.length,
569                                     pdu.data, pdu.length, &sig);
570         if (!NT_STATUS_IS_OK(status)) {
571                 PyErr_SetNTSTATUS(status);
572                 talloc_free(mem_ctx);
573                 return NULL;
574         }
575
576         py_sig = PyBytes_FromStringAndSize((const char *)sig.data, sig.length);
577         talloc_free(mem_ctx);
578         return py_sig;
579 }
580
581 static PyObject *py_gensec_check_packet(PyObject *self, PyObject *args)
582 {
583         NTSTATUS status;
584         Py_ssize_t data_length = 0;
585         Py_ssize_t pdu_length = 0;
586         Py_ssize_t sig_length = 0;
587         DATA_BLOB data, pdu, sig;
588         struct gensec_security *security = pytalloc_get_type(self, struct gensec_security);
589
590         if (!PyArg_ParseTuple(args, "z#z#z#",
591                               &data.data, &data_length,
592                               &pdu.data, &pdu_length,
593                               &sig.data, &sig_length)) {
594                 return NULL;
595         }
596         data.length = data_length;
597         pdu.length = pdu_length;
598         sig.length = sig_length;
599
600         status = gensec_check_packet(security,
601                                      data.data, data.length,
602                                      pdu.data, pdu.length, &sig);
603         if (!NT_STATUS_IS_OK(status)) {
604                 PyErr_SetNTSTATUS(status);
605                 return NULL;
606         }
607
608         Py_RETURN_NONE;
609 }
610
611 static PyMethodDef py_gensec_security_methods[] = {
612         { "start_client", (PyCFunction)py_gensec_start_client, METH_VARARGS|METH_KEYWORDS|METH_CLASS, 
613                 "S.start_client(settings) -> gensec" },
614         { "start_server", (PyCFunction)py_gensec_start_server, METH_VARARGS|METH_KEYWORDS|METH_CLASS,
615                 "S.start_server(auth_ctx, settings) -> gensec" },
616         { "set_credentials", (PyCFunction)py_gensec_set_credentials, METH_VARARGS, 
617                 "S.start_client(credentials)" },
618         { "set_target_hostname", (PyCFunction)py_gensec_set_target_hostname, METH_VARARGS, 
619                 "S.start_target_hostname(target_hostname)" },
620         { "set_target_service", (PyCFunction)py_gensec_set_target_service, METH_VARARGS, 
621                 "S.start_target_service(target_service)" },
622         { "session_info", (PyCFunction)py_gensec_session_info, METH_NOARGS,
623                 "S.session_info() -> info" },
624         { "session_key", (PyCFunction)py_gensec_session_key, METH_NOARGS,
625                 "S.session_key() -> key" },
626         { "start_mech_by_name", (PyCFunction)py_gensec_start_mech_by_name, METH_VARARGS,
627                 "S.start_mech_by_name(name)" },
628         { "start_mech_by_sasl_name", (PyCFunction)py_gensec_start_mech_by_sasl_name, METH_VARARGS,
629                 "S.start_mech_by_sasl_name(name)" },
630         { "start_mech_by_authtype", (PyCFunction)py_gensec_start_mech_by_authtype, METH_VARARGS,
631                 "S.start_mech_by_authtype(authtype, level)" },
632         { "get_name_by_authtype", (PyCFunction)py_get_name_by_authtype, METH_VARARGS,
633                 "S.get_name_by_authtype(authtype) -> name\nLookup an auth type." },
634         { "want_feature", (PyCFunction)py_gensec_want_feature, METH_VARARGS,
635                 "S.want_feature(feature)\n Request that GENSEC negotiate a particular feature." },
636         { "have_feature", (PyCFunction)py_gensec_have_feature, METH_VARARGS,
637                 "S.have_feature()\n Return True if GENSEC negotiated a particular feature." },
638         { "set_max_update_size",  (PyCFunction)py_gensec_set_max_update_size, METH_VARARGS,
639                 "S.set_max_update_size(max_size) \n Some mechs can fragment update packets, needs to be use before the mech is started." },
640         { "max_update_size",  (PyCFunction)py_gensec_max_update_size, 0,
641                 "S.max_update_size() \n Return the current max_update_size." },
642         { "update",  (PyCFunction)py_gensec_update, METH_VARARGS,
643                 "S.update(blob_in) -> (finished, blob_out)\nPerform one step in a GENSEC dance.  Repeat with new packets until finished is true or exception." },
644         { "wrap",  (PyCFunction)py_gensec_wrap, METH_VARARGS,
645                 "S.wrap(blob_in) -> blob_out\nPackage one clear packet into a wrapped GENSEC packet." },
646         { "unwrap",  (PyCFunction)py_gensec_unwrap, METH_VARARGS,
647                 "S.unwrap(blob_in) -> blob_out\nPerform one wrapped GENSEC packet into a clear packet." },
648         { "sig_size",  (PyCFunction)py_gensec_sig_size, METH_VARARGS,
649                 "S.sig_size(data_size) -> sig_size\nSize of the DCERPC packet signature" },
650         { "sign_packet",  (PyCFunction)py_gensec_sign_packet, METH_VARARGS,
651                 "S.sign_packet(data, whole_pdu) -> sig\nSign a DCERPC packet." },
652         { "check_packet",  (PyCFunction)py_gensec_check_packet, METH_VARARGS,
653                 "S.check_packet(data, whole_pdu, sig)\nCheck a DCERPC packet." },
654         { NULL }
655 };
656
657 static PyTypeObject Py_Security = {
658         .tp_name = "gensec.Security",
659         .tp_flags = Py_TPFLAGS_DEFAULT,
660         .tp_methods = py_gensec_security_methods,
661 };
662
663 void initgensec(void);
664 void initgensec(void)
665 {
666         PyObject *m;
667
668         if (pytalloc_BaseObject_PyType_Ready(&Py_Security) < 0)
669                 return;
670
671         m = Py_InitModule3("gensec", NULL, "Generic Security Interface.");
672         if (m == NULL)
673                 return;
674
675         PyModule_AddObject(m, "FEATURE_SESSION_KEY",     PyInt_FromLong(GENSEC_FEATURE_SESSION_KEY));
676         PyModule_AddObject(m, "FEATURE_SIGN",            PyInt_FromLong(GENSEC_FEATURE_SIGN));
677         PyModule_AddObject(m, "FEATURE_SEAL",            PyInt_FromLong(GENSEC_FEATURE_SEAL));
678         PyModule_AddObject(m, "FEATURE_DCE_STYLE",       PyInt_FromLong(GENSEC_FEATURE_DCE_STYLE));
679         PyModule_AddObject(m, "FEATURE_ASYNC_REPLIES",   PyInt_FromLong(GENSEC_FEATURE_ASYNC_REPLIES));
680         PyModule_AddObject(m, "FEATURE_DATAGRAM_MODE",   PyInt_FromLong(GENSEC_FEATURE_DATAGRAM_MODE));
681         PyModule_AddObject(m, "FEATURE_SIGN_PKT_HEADER", PyInt_FromLong(GENSEC_FEATURE_SIGN_PKT_HEADER));
682         PyModule_AddObject(m, "FEATURE_NEW_SPNEGO",      PyInt_FromLong(GENSEC_FEATURE_NEW_SPNEGO));
683
684         Py_INCREF(&Py_Security);
685         PyModule_AddObject(m, "Security", (PyObject *)&Py_Security);
686 }