vfs_ceph: use consistent code style when setting errno
[Samba.git] / source4 / libnet / py_net_dckeytab.c
blobd3770e1ec5a93de8034556f3aeee1df4e57aaf2e
1 /*
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"
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"
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;
36 TALLOC_CTX *mem_ctx;
37 const char *kwnames[] = { "keytab",
38 "samdb",
39 "principal",
40 "keep_stale_entries",
41 "only_current_keys",
42 "as_for_AS_REQ",
43 NULL };
44 NTSTATUS status;
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),
54 &r.in.keytab_name,
55 &py_samdb,
56 &r.in.principal,
57 &keep_stale_entries,
58 &only_current_keys,
59 &as_for_AS_REQ)) {
60 return NULL;
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) {
68 r.in.samdb = NULL;
69 } else {
70 PyErr_LDB_OR_RAISE(py_samdb, r.in.samdb);
73 mem_ctx = talloc_new(self->mem_ctx);
74 if (mem_ctx == NULL) {
75 PyErr_NoMemory();
76 return 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,
83 r.out.error_string
84 ? r.out.error_string
85 : nt_errstr(status));
86 talloc_free(mem_ctx);
87 return NULL;
90 talloc_free(mem_ctx);
92 Py_RETURN_NONE;
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.
117 * */
118 static struct PyModuleDef moduledef = {
119 PyModuleDef_HEAD_INIT,
120 .m_name = "dckeytab",
121 .m_doc = "dckeytab",
122 .m_size = -1,
123 .m_methods = NULL
126 MODULE_INIT_FUNC(dckeytab)
128 PyObject *m = NULL;
129 PyObject *Net;
130 PyObject *descr;
131 int ret;
133 m = PyModule_Create(&moduledef);
134 if (m == NULL)
135 return m;
137 m = PyImport_ImportModule("samba.net");
138 if (m == NULL)
139 return m;
141 Net = (PyObject *)PyObject_GetAttrString(m, "Net");
142 if (Net == NULL)
143 return m;
145 descr = PyDescr_NewMethod((PyTypeObject*)Net, &export_keytab_method_table[0]);
146 if (descr == NULL)
147 return m;
149 ret = PyDict_SetItemString(((PyTypeObject*)Net)->tp_dict,
150 export_keytab_method_table[0].ml_name,
151 descr);
152 if (ret != -1) {
153 Py_DECREF(descr);
156 return m;