Line data Source code
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 "lib/replace/system/python.h"
24 : #include "includes.h"
25 : #include "python/py3compat.h"
26 : #include "python/modules.h"
27 : #include "py_net.h"
28 : #include "libnet_export_keytab.h"
29 : #include "pyldb.h"
30 : #include "libcli/util/pyerrors.h"
31 :
32 : void initdckeytab(void);
33 :
34 56 : static PyObject *py_net_export_keytab(py_net_Object *self, PyObject *args, PyObject *kwargs)
35 : {
36 0 : struct libnet_export_keytab r;
37 56 : PyObject *py_samdb = NULL;
38 0 : TALLOC_CTX *mem_ctx;
39 56 : const char *kwnames[] = { "keytab",
40 : "samdb",
41 : "principal",
42 : "keep_stale_entries",
43 : NULL };
44 0 : NTSTATUS status;
45 : /*
46 : * int, with values true or false, to match expectation of
47 : * PyArg_ParseTupleAndKeywords()
48 : */
49 56 : int keep_stale_entries = false;
50 :
51 56 : r.in.principal = NULL;
52 :
53 56 : if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|Ozp:export_keytab", discard_const_p(char *, kwnames),
54 : &r.in.keytab_name,
55 : &py_samdb,
56 : &r.in.principal,
57 : &keep_stale_entries)) {
58 0 : return NULL;
59 : }
60 :
61 56 : r.in.keep_stale_entries = keep_stale_entries;
62 :
63 56 : if (py_samdb == NULL) {
64 36 : r.in.samdb = NULL;
65 : } else {
66 20 : PyErr_LDB_OR_RAISE(py_samdb, r.in.samdb);
67 : }
68 :
69 56 : mem_ctx = talloc_new(self->mem_ctx);
70 56 : if (mem_ctx == NULL) {
71 0 : PyErr_NoMemory();
72 0 : return NULL;
73 : }
74 :
75 56 : status = libnet_export_keytab(self->libnet_ctx, mem_ctx, &r);
76 :
77 56 : if (!NT_STATUS_IS_OK(status)) {
78 4 : PyErr_SetNTSTATUS_and_string(status,
79 : r.out.error_string
80 : ? r.out.error_string
81 : : nt_errstr(status));
82 4 : talloc_free(mem_ctx);
83 4 : return NULL;
84 : }
85 :
86 52 : talloc_free(mem_ctx);
87 :
88 52 : Py_RETURN_NONE;
89 : }
90 :
91 : static const char py_net_export_keytab_doc[] = "export_keytab(keytab, name)\n\n"
92 : "Export the DC keytab to a keytab file.";
93 :
94 : static PyMethodDef export_keytab_method_table[] = {
95 : {"export_keytab", PY_DISCARD_FUNC_SIG(PyCFunction,
96 : py_net_export_keytab),
97 : METH_VARARGS|METH_KEYWORDS, py_net_export_keytab_doc},
98 : { NULL, NULL, 0, NULL }
99 : };
100 :
101 : /*
102 : * A fake Python module to inject export_keytab() method into existing samba.net.Net class.
103 : * Python enforces that every loaded module actually creates Python module record in
104 : * the global module table even if we don't really need that record. Thus, we initialize
105 : * dckeytab module but never use it.
106 : * */
107 : void initdckeytab(void);
108 : static struct PyModuleDef moduledef = {
109 : PyModuleDef_HEAD_INIT,
110 : .m_name = "dckeytab",
111 : .m_doc = "dckeytab",
112 : .m_size = -1,
113 : .m_methods = NULL
114 : };
115 :
116 744 : MODULE_INIT_FUNC(dckeytab)
117 : {
118 744 : PyObject *m = NULL;
119 34 : PyObject *Net;
120 34 : PyObject *descr;
121 34 : int ret;
122 :
123 744 : m = PyModule_Create(&moduledef);
124 744 : if (m == NULL)
125 0 : return m;
126 :
127 744 : m = PyImport_ImportModule("samba.net");
128 744 : if (m == NULL)
129 0 : return m;
130 :
131 744 : Net = (PyObject *)PyObject_GetAttrString(m, "Net");
132 744 : if (Net == NULL)
133 0 : return m;
134 :
135 744 : descr = PyDescr_NewMethod((PyTypeObject*)Net, &export_keytab_method_table[0]);
136 744 : if (descr == NULL)
137 0 : return m;
138 :
139 744 : ret = PyDict_SetItemString(((PyTypeObject*)Net)->tp_dict,
140 : export_keytab_method_table[0].ml_name,
141 : descr);
142 744 : if (ret != -1) {
143 548 : Py_DECREF(descr);
144 : }
145 :
146 710 : return m;
147 : }
|