s4:kdc: also provide cross-realm keys via samba_kdc_seq()
[Samba.git] / libgpo / pygpo.c
blob1db83603fba2b38cf0f47d2942015f0a8210c18c
1 /*
2 Unix SMB/CIFS implementation.
3 Copyright (C) Luke Morrison <luc785@hotmail.com> 2013
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.
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.
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/>.
19 #include "lib/replace/system/python.h"
20 #include "includes.h"
21 #include "version.h"
22 #include "param/pyparam.h"
23 #include "gpo.h"
24 #include "ads.h"
25 #include "secrets.h"
26 #include "../libds/common/flags.h"
27 #include "librpc/rpc/pyrpc_util.h"
28 #include "auth/credentials/pycredentials.h"
29 #include "libcli/util/pyerrors.h"
30 #include "python/py3compat.h"
31 #include "python/modules.h"
32 #include <pytalloc.h>
33 #include "../libcli/security/security.h"
35 /* A Python C API module to use LIBGPO */
37 #define GPO_getter(ATTR) \
38 static PyObject* GPO_get_##ATTR(PyObject *self, void *closure) \
39 { \
40 struct GROUP_POLICY_OBJECT *gpo_ptr \
41 = pytalloc_get_ptr(self); \
43 if (gpo_ptr->ATTR) \
44 return PyUnicode_FromString(gpo_ptr->ATTR); \
45 else \
46 Py_RETURN_NONE; \
48 GPO_getter(ds_path)
49 GPO_getter(file_sys_path)
50 GPO_getter(display_name)
51 GPO_getter(name)
52 GPO_getter(link)
53 GPO_getter(user_extensions)
54 GPO_getter(machine_extensions)
55 #define GPO_setter(ATTR) \
56 static int GPO_set_##ATTR(PyObject *self, PyObject *val, void *closure) \
57 { \
58 struct GROUP_POLICY_OBJECT *gpo_ptr \
59 = pytalloc_get_ptr(self); \
61 if (!PyUnicode_Check(val)) { \
62 PyErr_Format(PyExc_TypeError, \
63 "Cannot convert input to string"); \
64 return -1; \
65 } \
66 if (val != Py_None) { \
67 gpo_ptr->ATTR = talloc_strdup(gpo_ptr, \
68 _PyUnicode_AsString(val)); \
69 } else { \
70 gpo_ptr->ATTR = NULL; \
71 } \
72 return 0; \
74 GPO_setter(ds_path)
75 GPO_setter(file_sys_path)
76 GPO_setter(display_name)
77 GPO_setter(name)
78 GPO_setter(link)
79 GPO_setter(user_extensions)
80 GPO_setter(machine_extensions)
81 #define GPO_int_getter(ATTR) \
82 static PyObject* GPO_get_##ATTR(PyObject *self, void *closure) \
83 { \
84 struct GROUP_POLICY_OBJECT *gpo_ptr \
85 = pytalloc_get_ptr(self); \
87 return PyLong_FromLong(gpo_ptr->ATTR); \
89 GPO_int_getter(options)
90 GPO_int_getter(version)
91 GPO_int_getter(link_type)
92 #define GPO_int_setter(ATTR) \
93 static int GPO_set_##ATTR(PyObject *self, PyObject *val, void *closure) \
94 { \
95 struct GROUP_POLICY_OBJECT *gpo_ptr \
96 = pytalloc_get_ptr(self); \
98 if (!PyLong_Check(val)) { \
99 PyErr_Format(PyExc_TypeError, \
100 "Cannot convert input to int"); \
101 return -1; \
102 } else { \
103 gpo_ptr->ATTR = PyLong_AsLong(val); \
105 return 0; \
107 GPO_int_setter(options)
108 GPO_int_setter(version)
109 GPO_int_setter(link_type)
111 static PyObject *GPO_marshall_get_sec_desc_buf(PyObject *self, PyObject *args,
112 PyObject *kwds)
114 struct GROUP_POLICY_OBJECT *gpo_ptr = pytalloc_get_ptr(self);
115 NTSTATUS status;
116 uint8_t *data = NULL;
117 size_t len = 0;
119 if (gpo_ptr->security_descriptor == NULL) {
120 PyErr_SetString(PyExc_RuntimeError, "Uninitialized");
121 return NULL;
124 status = marshall_sec_desc(gpo_ptr, gpo_ptr->security_descriptor,
125 &data, &len);
126 if (!NT_STATUS_IS_OK(status)) {
127 PyErr_Format(PyExc_BufferError,
128 "marshall_sec_desc_buf failed: %s",
129 nt_errstr(status));
130 return NULL;
133 return PyBytes_FromStringAndSize((char *)data, len);
136 static PyObject *GPO_unmarshall_set_sec_desc(PyObject *self, PyObject *args,
137 PyObject *kwds)
139 struct GROUP_POLICY_OBJECT *gpo_ptr = pytalloc_get_ptr(self);
140 char *bytes = NULL;
141 size_t length = 0;
142 NTSTATUS status;
144 if (!PyArg_ParseTuple(args, "s#", &bytes, &length)) {
145 PyErr_Format(PyExc_TypeError,
146 "Cannot convert input to bytes");
147 return NULL;
150 gpo_ptr->security_descriptor = talloc_zero(gpo_ptr,
151 struct security_descriptor);
152 status = unmarshall_sec_desc(gpo_ptr, (uint8_t *)bytes, length,
153 &gpo_ptr->security_descriptor);
154 if (!NT_STATUS_IS_OK(status)) {
155 PyErr_Format(PyExc_BufferError,
156 "unmarshall_sec_desc failed: %s",
157 nt_errstr(status));
158 return NULL;
161 return Py_None;
164 static PyGetSetDef GPO_setters[] = {
165 {discard_const_p(char, "options"), (getter)GPO_get_options,
166 (setter)GPO_set_options, NULL, NULL},
167 {discard_const_p(char, "version"), (getter)GPO_get_version,
168 (setter)GPO_set_version, NULL, NULL},
169 {discard_const_p(char, "ds_path"), (getter)GPO_get_ds_path,
170 (setter)GPO_set_ds_path, NULL, NULL},
171 {discard_const_p(char, "file_sys_path"), (getter)GPO_get_file_sys_path,
172 (setter)GPO_set_file_sys_path, NULL, NULL},
173 {discard_const_p(char, "display_name"), (getter)GPO_get_display_name,
174 (setter)GPO_set_display_name, NULL, NULL},
175 {discard_const_p(char, "name"), (getter)GPO_get_name,
176 (setter)GPO_set_name, NULL, NULL},
177 {discard_const_p(char, "link"), (getter)GPO_get_link,
178 (setter)GPO_set_link, NULL, NULL},
179 {discard_const_p(char, "link_type"), (getter)GPO_get_link_type,
180 (setter)GPO_set_link_type, NULL, NULL},
181 {discard_const_p(char, "user_extensions"),
182 (getter)GPO_get_user_extensions,
183 (setter)GPO_set_user_extensions, NULL, NULL},
184 {discard_const_p(char, "machine_extensions"),
185 (getter)GPO_get_machine_extensions,
186 (setter)GPO_set_machine_extensions, NULL, NULL},
190 static PyObject *py_gpo_get_unix_path(PyObject *self, PyObject *args,
191 PyObject *kwds)
193 NTSTATUS status;
194 const char *cache_dir = NULL;
195 PyObject *ret = NULL;
196 char *unix_path = NULL;
197 TALLOC_CTX *frame = NULL;
198 static const char *kwlist[] = {"cache_dir", NULL};
199 struct GROUP_POLICY_OBJECT *gpo_ptr \
200 = (struct GROUP_POLICY_OBJECT *)pytalloc_get_ptr(self);
202 frame = talloc_stackframe();
204 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s",
205 discard_const_p(char *, kwlist),
206 &cache_dir)) {
207 goto out;
210 if (!cache_dir) {
211 cache_dir = cache_path(talloc_tos(), GPO_CACHE_DIR);
212 if (!cache_dir) {
213 PyErr_SetString(PyExc_MemoryError,
214 "Failed to determine gpo cache dir");
215 goto out;
219 status = gpo_get_unix_path(frame, cache_dir, gpo_ptr, &unix_path);
221 if (!NT_STATUS_IS_OK(status)) {
222 PyErr_Format(PyExc_RuntimeError,
223 "Failed to determine gpo unix path: %s",
224 get_friendly_nt_error_msg(status));
225 goto out;
228 ret = PyUnicode_FromString(unix_path);
230 out:
231 TALLOC_FREE(frame);
232 return ret;
235 static PyMethodDef GPO_methods[] = {
236 {"get_unix_path", PY_DISCARD_FUNC_SIG(PyCFunction,
237 py_gpo_get_unix_path),
238 METH_VARARGS | METH_KEYWORDS,
239 NULL },
240 {"set_sec_desc", PY_DISCARD_FUNC_SIG(PyCFunction,
241 GPO_unmarshall_set_sec_desc),
242 METH_VARARGS, NULL },
243 {"get_sec_desc_buf", PY_DISCARD_FUNC_SIG(PyCFunction,
244 GPO_marshall_get_sec_desc_buf),
245 METH_NOARGS, NULL },
249 static int py_gpo_init(PyObject *self, PyObject *args, PyObject *kwds)
251 struct GROUP_POLICY_OBJECT *gpo_ptr = pytalloc_get_ptr(self);
252 const char *name = NULL;
253 const char *display_name = NULL;
254 enum GPO_LINK_TYPE link_type = GP_LINK_UNKOWN;
255 const char *file_sys_path = NULL;
257 static const char *kwlist[] = {
258 "name", "display_name", "link_type", "file_sys_path", NULL
260 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ssIs",
261 discard_const_p(char *, kwlist),
262 &name, &display_name, &link_type,
263 &file_sys_path)) {
264 return -1;
267 if (name) {
268 gpo_ptr->name = talloc_strdup(gpo_ptr, name);
270 if (display_name) {
271 gpo_ptr->display_name = talloc_strdup(gpo_ptr, display_name);
273 gpo_ptr->link_type = link_type;
274 if (file_sys_path) {
275 gpo_ptr->file_sys_path = talloc_strdup(gpo_ptr, file_sys_path);
278 return 0;
281 static PyObject *py_gpo_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
283 return pytalloc_new(struct GROUP_POLICY_OBJECT, type);
286 static PyTypeObject GPOType = {
287 PyVarObject_HEAD_INIT(NULL, 0)
288 .tp_name = "gpo.GROUP_POLICY_OBJECT",
289 .tp_doc = "GROUP_POLICY_OBJECT",
290 .tp_getset = GPO_setters,
291 .tp_methods = GPO_methods,
292 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
293 .tp_new = py_gpo_new,
294 .tp_init = (initproc)py_gpo_init,
297 typedef struct {
298 PyObject_HEAD
299 ADS_STRUCT *ads_ptr;
300 PyObject *py_creds;
301 struct cli_credentials *cli_creds;
302 } ADS;
304 static void py_ads_dealloc(ADS* self)
306 TALLOC_FREE(self->ads_ptr);
307 Py_CLEAR(self->py_creds);
308 Py_TYPE(self)->tp_free((PyObject*)self);
311 static PyObject* py_ads_connect(ADS *self, PyObject *Py_UNUSED(ignored));
312 static int py_ads_init(ADS *self, PyObject *args, PyObject *kwds)
314 const char *realm = NULL;
315 const char *workgroup = NULL;
316 const char *ldap_server = NULL;
317 PyObject *lp_obj = NULL;
318 PyObject *py_creds = NULL;
319 struct loadparm_context *lp_ctx = NULL;
320 bool ok = false;
322 static const char *kwlist[] = {
323 "ldap_server", "loadparm_context", "credentials", NULL
325 if (!PyArg_ParseTupleAndKeywords(args, kwds, "sO|O",
326 discard_const_p(char *, kwlist),
327 &ldap_server, &lp_obj, &py_creds)) {
328 return -1;
330 /* keep reference to the credentials. Clear any earlier ones */
331 Py_CLEAR(self->py_creds);
332 self->cli_creds = NULL;
333 self->py_creds = py_creds;
334 Py_XINCREF(self->py_creds);
336 if (self->py_creds) {
337 ok = py_check_dcerpc_type(self->py_creds, "samba.credentials",
338 "Credentials");
339 if (!ok) {
340 return -1;
342 self->cli_creds
343 = PyCredentials_AsCliCredentials(self->py_creds);
346 ok = py_check_dcerpc_type(lp_obj, "samba.param", "LoadParm");
347 if (!ok) {
348 return -1;
350 lp_ctx = pytalloc_get_type(lp_obj, struct loadparm_context);
351 if (lp_ctx == NULL) {
352 return -1;
354 ok = lp_load_initial_only(lp_ctx->szConfigFile);
355 if (!ok) {
356 PyErr_Format(PyExc_RuntimeError, "Could not load config file '%s'",
357 lp_ctx->szConfigFile);
358 return -1;
361 if (self->cli_creds) {
362 realm = cli_credentials_get_realm(self->cli_creds);
363 workgroup = cli_credentials_get_domain(self->cli_creds);
364 } else {
365 realm = lp_realm();
366 workgroup = lp_workgroup();
369 /* in case __init__ is called more than once */
370 if (self->ads_ptr) {
371 TALLOC_FREE(self->ads_ptr);
373 /* always succeeds or crashes */
374 self->ads_ptr = ads_init(pytalloc_get_mem_ctx(args),
375 realm,
376 workgroup,
377 ldap_server,
378 ADS_SASL_PLAIN);
380 return 0;
383 /* connect. Failure to connect results in an Exception */
384 static PyObject* py_ads_connect(ADS *self,
385 PyObject *Py_UNUSED(ignored))
387 ADS_STATUS status;
388 TALLOC_CTX *frame = talloc_stackframe();
389 if (!self->ads_ptr) {
390 PyErr_SetString(PyExc_RuntimeError, "Uninitialized");
391 return NULL;
393 if (self->cli_creds) {
394 status = ads_connect_creds(self->ads_ptr, self->cli_creds);
395 if (!ADS_ERR_OK(status)) {
396 PyErr_Format(PyExc_RuntimeError,
397 "ads_connect_creds() failed: %s",
398 ads_errstr(status));
399 goto err;
401 } else {
402 status = ads_connect_machine(self->ads_ptr);
403 if (!ADS_ERR_OK(status)) {
404 PyErr_Format(PyExc_RuntimeError,
405 "ads_connect_machine() failed: %s",
406 ads_errstr(status));
407 goto err;
411 TALLOC_FREE(frame);
412 Py_RETURN_TRUE;
414 err:
415 TALLOC_FREE(frame);
416 return NULL;
419 /* Parameter mapping and functions for the GP_EXT struct */
420 void initgpo(void);
422 /* Global methods aka do not need a special pyobject type */
423 static PyObject *py_gpo_get_sysvol_gpt_version(PyObject * self,
424 PyObject * args)
426 TALLOC_CTX *tmp_ctx = NULL;
427 char *unix_path;
428 char *display_name = NULL;
429 uint32_t sysvol_version = 0;
430 PyObject *result;
431 NTSTATUS status;
433 if (!PyArg_ParseTuple(args, "s", &unix_path)) {
434 return NULL;
436 tmp_ctx = talloc_new(NULL);
437 if (!tmp_ctx) {
438 return PyErr_NoMemory();
440 status = gpo_get_sysvol_gpt_version(tmp_ctx, unix_path,
441 &sysvol_version,
442 &display_name);
443 if (!NT_STATUS_IS_OK(status)) {
444 PyErr_SetNTSTATUS(status);
445 TALLOC_FREE(tmp_ctx);
446 return NULL;
449 result = Py_BuildValue("[s,i]", display_name, sysvol_version);
450 talloc_free(tmp_ctx);
451 return result;
454 #ifdef HAVE_ADS
455 static ADS_STATUS find_samaccount(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
456 const char *samaccountname,
457 uint32_t *uac_ret, const char **dn_ret)
459 ADS_STATUS status;
460 const char *attrs[] = { "userAccountControl", NULL };
461 const char *filter;
462 LDAPMessage *res = NULL;
463 char *dn = NULL;
464 uint32_t uac = 0;
466 filter = talloc_asprintf(mem_ctx, "(sAMAccountName=%s)",
467 samaccountname);
468 if (filter == NULL) {
469 status = ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
470 goto out;
473 status = ads_do_search_all(ads, ads->config.bind_path,
474 LDAP_SCOPE_SUBTREE, filter, attrs, &res);
476 if (!ADS_ERR_OK(status)) {
477 goto out;
480 if (ads_count_replies(ads, res) != 1) {
481 status = ADS_ERROR(LDAP_NO_RESULTS_RETURNED);
482 goto out;
485 dn = ads_get_dn(ads, talloc_tos(), res);
486 if (dn == NULL) {
487 status = ADS_ERROR(LDAP_NO_MEMORY);
488 goto out;
491 if (!ads_pull_uint32(ads, res, "userAccountControl", &uac)) {
492 status = ADS_ERROR(LDAP_NO_SUCH_ATTRIBUTE);
493 goto out;
496 if (uac_ret) {
497 *uac_ret = uac;
500 if (dn_ret) {
501 *dn_ret = talloc_strdup(mem_ctx, dn);
502 if (*dn_ret == NULL) {
503 status = ADS_ERROR(LDAP_NO_MEMORY);
504 goto out;
507 out:
508 TALLOC_FREE(dn);
509 ads_msgfree(ads, res);
511 return status;
514 static PyObject *py_ads_get_gpo_list(ADS *self, PyObject *args, PyObject *kwds)
516 TALLOC_CTX *frame = NULL;
517 struct GROUP_POLICY_OBJECT *gpo = NULL, *gpo_list = NULL;
518 ADS_STATUS status;
519 const char *samaccountname = NULL;
520 const char *dn = NULL;
521 uint32_t uac = 0;
522 uint32_t flags = 0;
523 struct security_token *token = NULL;
524 PyObject *ret = NULL;
525 TALLOC_CTX *gpo_ctx = NULL;
526 size_t list_size;
527 size_t i;
529 static const char *kwlist[] = {"samaccountname", NULL};
531 PyErr_WarnEx(PyExc_DeprecationWarning, "The get_gpo_list function"
532 " is deprecated as of Samba 4.19. Please use "
533 "the samba.gp module instead.", 2);
535 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s",
536 discard_const_p(char *, kwlist),
537 &samaccountname)) {
538 return NULL;
540 if (!self->ads_ptr) {
541 PyErr_SetString(PyExc_RuntimeError, "Uninitialized");
542 return NULL;
545 frame = talloc_stackframe();
547 status = find_samaccount(self->ads_ptr, frame,
548 samaccountname, &uac, &dn);
549 if (!ADS_ERR_OK(status)) {
550 PyErr_Format(PyExc_RuntimeError,
551 "Failed to find samAccountName '%s': %s",
552 samaccountname, ads_errstr(status));
553 goto out;
556 if (uac & UF_WORKSTATION_TRUST_ACCOUNT ||
557 uac & UF_SERVER_TRUST_ACCOUNT) {
558 flags |= GPO_LIST_FLAG_MACHINE;
559 status = gp_get_machine_token(self->ads_ptr, frame, dn,
560 &token);
561 if (!ADS_ERR_OK(status)) {
562 PyErr_Format(PyExc_RuntimeError,
563 "Failed to get machine token for '%s'(%s): %s",
564 samaccountname, dn, ads_errstr(status));
565 goto out;
567 } else {
568 status = ads_get_sid_token(self->ads_ptr, frame, dn, &token);
569 if (!ADS_ERR_OK(status)) {
570 PyErr_Format(PyExc_RuntimeError,
571 "Failed to get sid token for '%s'(%s): %s",
572 samaccountname, dn, ads_errstr(status));
573 goto out;
577 gpo_ctx = talloc_new(frame);
578 if (!gpo_ctx) {
579 PyErr_NoMemory();
580 goto out;
582 status = ads_get_gpo_list(self->ads_ptr, gpo_ctx, dn, flags, token,
583 &gpo_list);
584 if (!ADS_ERR_OK(status)) {
585 PyErr_Format(PyExc_RuntimeError,
586 "Failed to fetch GPO list: %s",
587 ads_errstr(status));
588 goto out;
591 /* Convert the C linked list into a python list */
592 list_size = 0;
593 for (gpo = gpo_list; gpo != NULL; gpo = gpo->next) {
594 list_size++;
597 i = 0;
598 ret = PyList_New(list_size);
599 if (ret == NULL) {
600 goto out;
603 for (gpo = gpo_list; gpo != NULL; gpo = gpo->next) {
604 PyObject *obj = pytalloc_reference_ex(&GPOType,
605 gpo_ctx, gpo);
606 if (obj == NULL) {
607 Py_CLEAR(ret);
608 goto out;
611 PyList_SetItem(ret, i, obj);
612 i++;
615 out:
616 TALLOC_FREE(frame);
617 return ret;
620 #endif
622 static PyMethodDef ADS_methods[] = {
623 { "connect", (PyCFunction)py_ads_connect, METH_NOARGS,
624 "Connect to the LDAP server" },
625 #ifdef HAVE_ADS
626 { "get_gpo_list", PY_DISCARD_FUNC_SIG(PyCFunction, py_ads_get_gpo_list),
627 METH_VARARGS | METH_KEYWORDS,
628 NULL },
629 #endif
633 static PyTypeObject ads_ADSType = {
634 .tp_name = "gpo.ADS_STRUCT",
635 .tp_basicsize = sizeof(ADS),
636 .tp_new = PyType_GenericNew,
637 .tp_dealloc = (destructor)py_ads_dealloc,
638 .tp_flags = Py_TPFLAGS_DEFAULT,
639 .tp_doc = "ADS struct",
640 .tp_methods = ADS_methods,
641 .tp_init = (initproc)py_ads_init,
644 static PyMethodDef py_gpo_methods[] = {
645 {"gpo_get_sysvol_gpt_version",
646 (PyCFunction)py_gpo_get_sysvol_gpt_version,
647 METH_VARARGS, NULL},
651 static struct PyModuleDef moduledef = {
652 PyModuleDef_HEAD_INIT,
653 .m_name = "gpo",
654 .m_doc = "libgpo python bindings",
655 .m_size = -1,
656 .m_methods = py_gpo_methods,
659 /* Will be called by python when loading this module */
660 void initgpo(void);
662 MODULE_INIT_FUNC(gpo)
664 PyObject *m;
666 debug_setup_talloc_log();
668 /* Instantiate the types */
669 m = PyModule_Create(&moduledef);
670 if (m == NULL) {
671 goto err;
674 if (PyModule_AddObject(m, "version",
675 PyUnicode_FromString(SAMBA_VERSION_STRING)) ) {
676 goto err;
679 if (pytalloc_BaseObject_PyType_Ready(&ads_ADSType) < 0) {
680 goto err;
683 Py_INCREF(&ads_ADSType);
684 if (PyModule_AddObject(m, "ADS_STRUCT", (PyObject *)&ads_ADSType)) {
685 goto err;
688 if (pytalloc_BaseObject_PyType_Ready(&GPOType) < 0) {
689 goto err;
692 Py_INCREF((PyObject *)(void *)&GPOType);
693 if (PyModule_AddObject(m, "GROUP_POLICY_OBJECT",
694 (PyObject *)&GPOType)) {
695 goto err;
698 #define ADD_FLAGS(val) PyModule_AddObject(m, #val, PyLong_FromLong(val))
700 ADD_FLAGS(GP_LINK_UNKOWN);
701 ADD_FLAGS(GP_LINK_MACHINE);
702 ADD_FLAGS(GP_LINK_SITE);
703 ADD_FLAGS(GP_LINK_DOMAIN);
704 ADD_FLAGS(GP_LINK_OU);
705 ADD_FLAGS(GP_LINK_LOCAL);
707 return m;
709 err:
710 Py_CLEAR(m);
711 return NULL;