source4/lib: Fix prototypes for all functions.
[bbaumbach/samba-autobuild/.git] / source4 / lib / registry / pyregistry.c
index a3ba6c535116761daf0dd3145ced286218a3974d..5719bbd11516b53882f90b9dadcc267e82538a1f 100644 (file)
@@ -2,6 +2,7 @@
    Unix SMB/CIFS implementation.
    Samba utility functions
    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
+   Copyright (C) Wilco Baan Hofman <wilco@baanhofman.nl> 2010
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 #include <Python.h>
 #include "includes.h"
-#include <tevent.h>
 #include "libcli/util/pyerrors.h"
 #include "lib/registry/registry.h"
 #include "lib/talloc/pytalloc.h"
+#include "lib/events/events.h"
 #include "auth/credentials/pycredentials.h"
 #include "param/pyparam.h"
 
-PyAPI_DATA(PyTypeObject) PyRegistryKey;
-PyAPI_DATA(PyTypeObject) PyRegistry;
-PyAPI_DATA(PyTypeObject) PyHiveKey;
+extern PyTypeObject PyRegistryKey;
+extern PyTypeObject PyRegistry;
+extern PyTypeObject PyHiveKey;
+
+void initregistry(void);
 
 /*#define PyRegistryKey_AsRegistryKey(obj) py_talloc_get_type(obj, struct registry_key)*/
 #define PyRegistry_AsRegistryContext(obj) ((struct registry_context *)py_talloc_get_ptr(obj))
@@ -158,7 +161,6 @@ PyTypeObject PyRegistry = {
        .tp_methods = registry_methods,
        .tp_new = registry_new,
        .tp_basicsize = sizeof(py_talloc_Object),
-       .tp_dealloc = py_talloc_dealloc,
        .tp_flags = Py_TPFLAGS_DEFAULT,
 };
 
@@ -238,25 +240,70 @@ static PyMethodDef hive_key_methods[] = {
        { NULL }
 };
 
-static PyObject *hive_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
-{
-       /* reg_open_hive */
+static PyObject *hive_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) {
        Py_RETURN_NONE;
 }
 
+static PyObject *py_open_hive(PyTypeObject *type, PyObject *args, PyObject *kwargs)
+{
+       const char *kwnames[] = { "location", "lp_ctx", "session_info", "credentials", NULL };
+       WERROR result;
+       struct loadparm_context *lp_ctx;
+       PyObject *py_lp_ctx, *py_session_info, *py_credentials;
+       struct auth_session_info *session_info;
+       struct cli_credentials *credentials;
+       char *location;
+       struct hive_key *hive_key;
+       TALLOC_CTX *mem_ctx;
+
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO",
+                                        discard_const_p(char *, kwnames),
+                                        &location,
+                                        &py_lp_ctx, &py_session_info,
+                                        &py_credentials))
+               return NULL;
+
+       mem_ctx = talloc_new(NULL);
+       if (mem_ctx == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
+
+       lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
+       if (lp_ctx == NULL) {
+               PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
+               talloc_free(mem_ctx);
+               return NULL;
+       }
+
+       credentials = cli_credentials_from_py_object(py_credentials);
+       if (credentials == NULL) {
+               PyErr_SetString(PyExc_TypeError, "Expected credentials");
+               talloc_free(mem_ctx);
+               return NULL;
+       }
+       session_info = NULL;
+
+       result = reg_open_hive(NULL, location, session_info, credentials,
+                              tevent_context_init(NULL),
+                              lp_ctx, &hive_key);
+       talloc_free(mem_ctx);
+       PyErr_WERROR_IS_ERR_RAISE(result);
+
+       return py_talloc_steal(&PyHiveKey, hive_key);
+}
+
 PyTypeObject PyHiveKey = {
        .tp_name = "HiveKey",
        .tp_methods = hive_key_methods,
-       .tp_new = hive_open,
+       .tp_new = hive_new,
        .tp_basicsize = sizeof(py_talloc_Object),
-       .tp_dealloc = py_talloc_dealloc,
        .tp_flags = Py_TPFLAGS_DEFAULT,
 };
 
 PyTypeObject PyRegistryKey = {
        .tp_name = "RegistryKey",
        .tp_basicsize = sizeof(py_talloc_Object),
-       .tp_dealloc = py_talloc_dealloc,
        .tp_flags = Py_TPFLAGS_DEFAULT,
 };
 
@@ -269,21 +316,31 @@ static PyObject *py_open_samba(PyObject *self, PyObject *args, PyObject *kwargs)
        PyObject *py_lp_ctx, *py_session_info, *py_credentials;
        struct auth_session_info *session_info;
        struct cli_credentials *credentials;
+       TALLOC_CTX *mem_ctx;
+
        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOO",
                                         discard_const_p(char *, kwnames),
                                         &py_lp_ctx, &py_session_info,
                                         &py_credentials))
                return NULL;
 
-       lp_ctx = lpcfg_from_py_object(NULL, py_lp_ctx); /* FIXME: leaky */
+       mem_ctx = talloc_new(NULL);
+       if (mem_ctx == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
+
+       lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
        if (lp_ctx == NULL) {
                PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
+               talloc_free(mem_ctx);
                return NULL;
        }
 
        credentials = cli_credentials_from_py_object(py_credentials);
        if (credentials == NULL) {
                PyErr_SetString(PyExc_TypeError, "Expected credentials");
+               talloc_free(mem_ctx);
                return NULL;
        }
 
@@ -291,11 +348,12 @@ static PyObject *py_open_samba(PyObject *self, PyObject *args, PyObject *kwargs)
 
        result = reg_open_samba(NULL, &reg_ctx, NULL, 
                                lp_ctx, session_info, credentials);
+       talloc_free(mem_ctx);
        if (!W_ERROR_IS_OK(result)) {
                PyErr_SetWERROR(result);
                return NULL;
        }
-       
+
        return py_talloc_steal(&PyRegistry, reg_ctx);
 }
 
@@ -339,6 +397,7 @@ static PyObject *py_open_ldb_file(PyObject *self, PyObject *args, PyObject *kwar
        struct cli_credentials *credentials;
        struct hive_key *key;
        struct auth_session_info *session_info;
+       TALLOC_CTX *mem_ctx;
 
        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO", 
                                         discard_const_p(char *, kwnames),
@@ -346,22 +405,31 @@ static PyObject *py_open_ldb_file(PyObject *self, PyObject *args, PyObject *kwar
                                         &py_credentials, &py_lp_ctx))
                return NULL;
 
-       lp_ctx = lpcfg_from_py_object(NULL, py_lp_ctx); /* FIXME: leaky */
+       mem_ctx = talloc_new(NULL);
+       if (mem_ctx == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
+
+       lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
        if (lp_ctx == NULL) {
                PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
+               talloc_free(mem_ctx);
                return NULL;
        }
 
        credentials = cli_credentials_from_py_object(py_credentials);
        if (credentials == NULL) {
                PyErr_SetString(PyExc_TypeError, "Expected credentials");
+               talloc_free(mem_ctx);
                return NULL;
        }
 
        session_info = NULL; /* FIXME */
 
        result = reg_open_ldb_file(NULL, location, session_info, credentials,
-                                  tevent_context_init(NULL), lp_ctx, &key);
+                                  s4_event_context_init(NULL), lp_ctx, &key);
+       talloc_free(mem_ctx);
        PyErr_WERROR_IS_ERR_RAISE(result);
 
        return py_talloc_steal(&PyHiveKey, key);
@@ -396,6 +464,7 @@ static PyMethodDef py_registry_methods[] = {
        { "open_directory", py_open_directory, METH_VARARGS, "open_dir(location) -> key" },
        { "create_directory", py_create_directory, METH_VARARGS, "create_dir(location) -> key" },
        { "open_ldb", (PyCFunction)py_open_ldb_file, METH_VARARGS|METH_KEYWORDS, "open_ldb(location, session_info=None, credentials=None, loadparm_context=None) -> key" },
+       { "open_hive", (PyCFunction)py_open_hive, METH_VARARGS|METH_KEYWORDS, "open_hive(location, session_info=None, credentials=None, loadparm_context=None) -> key" },
        { "str_regtype", py_str_regtype, METH_VARARGS, "str_regtype(int) -> str" },
        { "get_predef_name", py_get_predef_name, METH_VARARGS, "get_predef_name(hkey) -> str" },
        { NULL }
@@ -404,6 +473,14 @@ static PyMethodDef py_registry_methods[] = {
 void initregistry(void)
 {
        PyObject *m;
+       PyTypeObject *talloc_type = PyTalloc_GetObjectType();
+
+       if (talloc_type == NULL)
+               return;
+
+       PyHiveKey.tp_base = talloc_type;
+       PyRegistry.tp_base = talloc_type;
+       PyRegistryKey.tp_base = talloc_type;
 
        if (PyType_Ready(&PyHiveKey) < 0)
                return;