libnet: Use dom_sid_str_buf
[samba.git] / source4 / libnet / py_net_dckeytab.c
1 /*
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4
5    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008-2010
6    Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2009
7    Copyright (C) Alexander Bokovoy <ab@samba.org> 2012
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include <Python.h>
24 #include "includes.h"
25 #include "python/py3compat.h"
26 #include "py_net.h"
27 #include "libnet_export_keytab.h"
28
29 void initdckeytab(void);
30
31 static PyObject *py_net_export_keytab(py_net_Object *self, PyObject *args, PyObject *kwargs)
32 {
33         struct libnet_export_keytab r;
34         TALLOC_CTX *mem_ctx;
35         const char *kwnames[] = { "keytab", "principal", NULL };
36         NTSTATUS status;
37         r.in.principal = NULL;
38
39         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|z:export_keytab", discard_const_p(char *, kwnames),
40                                          &r.in.keytab_name,
41                                          &r.in.principal)) {
42                 return NULL;
43         }
44
45         mem_ctx = talloc_new(self->mem_ctx);
46         if (mem_ctx == NULL) {
47                 PyErr_NoMemory();
48                 return NULL;
49         }
50
51         status = libnet_export_keytab(self->libnet_ctx, mem_ctx, &r);
52         if (NT_STATUS_IS_ERR(status)) {
53                 PyErr_SetString(PyExc_RuntimeError,
54                                 r.out.error_string?r.out.error_string:nt_errstr(status));
55                 talloc_free(mem_ctx);
56                 return NULL;
57         }
58
59         talloc_free(mem_ctx);
60
61         Py_RETURN_NONE;
62 }
63
64 static const char py_net_export_keytab_doc[] = "export_keytab(keytab, name)\n\n"
65 "Export the DC keytab to a keytab file.";
66
67 static PyMethodDef export_keytab_method_table[] = {
68         {"export_keytab", (PyCFunction)py_net_export_keytab, METH_VARARGS|METH_KEYWORDS, py_net_export_keytab_doc},
69         { NULL, NULL, 0, NULL }
70 };
71
72 /*
73  * A fake Python module to inject export_keytab() method into existing samba.net.Net class.
74  * Python enforces that every loaded module actually creates Python module record in
75  * the global module table even if we don't really need that record. Thus, we initialize
76  * dckeytab module but never use it.
77  * */
78 void initdckeytab(void);
79 static struct PyModuleDef moduledef = {
80     PyModuleDef_HEAD_INIT,
81     .m_name = "dckeytab",
82     .m_doc = "dckeytab",
83     .m_size = -1,
84     .m_methods = NULL
85 };
86
87 MODULE_INIT_FUNC(dckeytab)
88 {
89         PyObject *m = NULL;
90         PyObject *Net;
91         PyObject *descr;
92         int ret;
93
94         m = PyModule_Create(&moduledef);
95         if (m == NULL)
96                 return m;
97
98         m = PyImport_ImportModule("samba.net");
99         if (m == NULL)
100                 return m;
101
102         Net = (PyObject *)PyObject_GetAttrString(m, "Net");
103         if (Net == NULL)
104                 return m;
105
106         descr = PyDescr_NewMethod((PyTypeObject*)Net, &export_keytab_method_table[0]);
107         if (descr == NULL)
108                 return m;
109
110         ret = PyDict_SetItemString(((PyTypeObject*)Net)->tp_dict,
111                                      export_keytab_method_table[0].ml_name,
112                                      descr);
113         if (ret != -1) {
114                 Py_DECREF(descr);
115         }
116
117         return m;
118 }