2 Unix SMB/CIFS implementation.
3 Samba utility functions
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
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.
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.
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/>.
23 #include "lib/replace/system/python.h"
25 #include "python/py3compat.h"
26 #include "python/modules.h"
28 #include "libnet_export_keytab.h"
30 #include "libcli/util/pyerrors.h"
32 static PyObject
*py_net_export_keytab(py_net_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
34 struct libnet_export_keytab r
= { .in
= { .principal
= NULL
, }};
35 PyObject
*py_samdb
= NULL
;
37 const char *kwnames
[] = { "keytab",
46 * int, with values true or false, to match expectation of
47 * PyArg_ParseTupleAndKeywords()
49 int keep_stale_entries
= false;
50 int only_current_keys
= false;
51 int as_for_AS_REQ
= false;
53 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|Ozppp:export_keytab", discard_const_p(char *, kwnames
),
63 r
.in
.keep_stale_entries
= keep_stale_entries
;
64 r
.in
.only_current_keys
= only_current_keys
;
65 r
.in
.as_for_AS_REQ
= as_for_AS_REQ
;
67 if (py_samdb
== NULL
) {
70 PyErr_LDB_OR_RAISE(py_samdb
, r
.in
.samdb
);
73 mem_ctx
= talloc_new(self
->mem_ctx
);
74 if (mem_ctx
== NULL
) {
79 status
= libnet_export_keytab(self
->libnet_ctx
, mem_ctx
, &r
);
81 if (!NT_STATUS_IS_OK(status
)) {
82 PyErr_SetNTSTATUS_and_string(status
,
95 static const char py_net_export_keytab_doc
[] =
96 "export_keytab(keytab, samdb=None, principal=None, "
97 "keep_stale_entries=False, only_current_keys=False, "
98 "as_for_AS_REQ=False)\n\n"
99 "Export the DC keytab to a keytab file.\n\n"
100 "Pass as_for_AS_REQ=True to simulate the combination of flags normally "
101 "utilized for an AS‐REQ. Samba’s testsuite uses this to verify which "
102 "keys the KDC would see — some combination of previous and current "
103 "keys — for a Group Managed Service Account performing an AS‐REQ.";
105 static PyMethodDef export_keytab_method_table
[] = {
106 {"export_keytab", PY_DISCARD_FUNC_SIG(PyCFunction
,
107 py_net_export_keytab
),
108 METH_VARARGS
|METH_KEYWORDS
, py_net_export_keytab_doc
},
109 { NULL
, NULL
, 0, NULL
}
113 * A fake Python module to inject export_keytab() method into existing samba.net.Net class.
114 * Python enforces that every loaded module actually creates Python module record in
115 * the global module table even if we don't really need that record. Thus, we initialize
116 * dckeytab module but never use it.
118 static struct PyModuleDef moduledef
= {
119 PyModuleDef_HEAD_INIT
,
120 .m_name
= "dckeytab",
126 MODULE_INIT_FUNC(dckeytab
)
133 m
= PyModule_Create(&moduledef
);
137 m
= PyImport_ImportModule("samba.net");
141 Net
= (PyObject
*)PyObject_GetAttrString(m
, "Net");
145 descr
= PyDescr_NewMethod((PyTypeObject
*)Net
, &export_keytab_method_table
[0]);
149 ret
= PyDict_SetItemString(((PyTypeObject
*)Net
)->tp_dict
,
150 export_keytab_method_table
[0].ml_name
,