r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[samba.git] / source / python / py_lsa.c
index 0584cf716bfb162cb99689d972aa3c1fff5f3562..6095fdfc677f0fa1ef0561ce59621521647cba4e 100644 (file)
@@ -5,7 +5,7 @@
    
    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
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "python/py_lsa.h"
 
-PyObject *new_lsa_policy_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+PyObject *new_lsa_policy_hnd_object(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
                                    POLICY_HND *pol)
 {
        lsa_policy_hnd_object *o;
@@ -55,7 +54,7 @@ static PyObject *lsa_open_policy(PyObject *self, PyObject *args,
        static char *kwlist[] = { "servername", "creds", "access", NULL };
        char *server, *errstr;
        PyObject *creds = NULL, *result = NULL;
-       uint32 desired_access = MAXIMUM_ALLOWED_ACCESS;
+       uint32 desired_access = GENERIC_EXECUTE_ACCESS;
        struct cli_state *cli = NULL;
        NTSTATUS ntstatus;
        TALLOC_CTX *mem_ctx = NULL;
@@ -78,34 +77,33 @@ static PyObject *lsa_open_policy(PyObject *self, PyObject *args,
 
        server += 2;
 
-       if (!(cli = open_pipe_creds(server, creds, PIPE_LSARPC, &errstr))) {
+       if (!(cli = open_pipe_creds(server, creds, PI_LSARPC, &errstr))) {
                PyErr_SetString(lsa_error, errstr);
                free(errstr);
                return NULL;
        }
 
-       if (!(mem_ctx = talloc_init())) {
+       if (!(mem_ctx = talloc_init("lsa_open_policy"))) {
                PyErr_SetString(lsa_error, "unable to init talloc context\n");
                goto done;
        }
 
-       ntstatus = cli_lsa_open_policy(cli, mem_ctx, True,
-                                      SEC_RIGHTS_MAXIMUM_ALLOWED, &hnd);
+       ntstatus = rpccli_lsa_open_policy(
+               cli->pipe_list, mem_ctx, True, desired_access, &hnd);
 
        if (!NT_STATUS_IS_OK(ntstatus)) {
                PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
                goto done;
        }
 
-       result = new_lsa_policy_hnd_object(cli, mem_ctx, &hnd);
+       result = new_lsa_policy_hnd_object(cli->pipe_list, mem_ctx, &hnd);
 
 done:
        if (!result) {
                if (cli)
                        cli_shutdown(cli);
 
-               if (mem_ctx)
-                       talloc_destroy(mem_ctx);
+               talloc_destroy(mem_ctx);
        }
 
        return result;
@@ -126,7 +124,7 @@ static PyObject *lsa_close(PyObject *self, PyObject *args, PyObject *kw)
 
        /* Call rpc function */
 
-       result = cli_lsa_close(hnd->cli, hnd->mem_ctx, &hnd->pol);
+       result = rpccli_lsa_Close(hnd->cli, hnd->mem_ctx, &hnd->pol);
 
        /* Cleanup samba stuff */
 
@@ -141,13 +139,14 @@ static PyObject *lsa_close(PyObject *self, PyObject *args, PyObject *kw)
 
 static PyObject *lsa_lookup_names(PyObject *self, PyObject *args)
 {
-       PyObject *py_names, *result;
+       PyObject *py_names, *result = NULL;
        NTSTATUS ntstatus;
        lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
        int num_names, i;
        const char **names;
        DOM_SID *sids;
-       uint32 *name_types;
+       TALLOC_CTX *mem_ctx = NULL;
+       enum lsa_SidType *name_types;
 
        if (!PyArg_ParseTuple(args, "O", &py_names))
                return NULL;
@@ -157,18 +156,22 @@ static PyObject *lsa_lookup_names(PyObject *self, PyObject *args)
                return NULL;
        }
 
+       if (!(mem_ctx = talloc_init("lsa_lookup_names"))) {
+               PyErr_SetString(lsa_error, "unable to init talloc context\n");
+               goto done;
+       }
+
        if (PyList_Check(py_names)) {
 
                /* Convert list to char ** array */
 
                num_names = PyList_Size(py_names);
-               names = (const char **)talloc(
-                       hnd->mem_ctx, num_names * sizeof(char *));
+               names = (const char **)_talloc(mem_ctx, num_names * sizeof(char *));
                
                for (i = 0; i < num_names; i++) {
                        PyObject *obj = PyList_GetItem(py_names, i);
                        
-                       names[i] = talloc_strdup(hnd->mem_ctx, PyString_AsString(obj));
+                       names[i] = talloc_strdup(mem_ctx, PyString_AsString(obj));
                }
 
        } else {
@@ -176,17 +179,18 @@ static PyObject *lsa_lookup_names(PyObject *self, PyObject *args)
                /* Just a single element */
 
                num_names = 1;
-               names = (const char **)talloc(hnd->mem_ctx, sizeof(char *));
+               names = (const char **)_talloc(mem_ctx, sizeof(char *));
 
                names[0] = PyString_AsString(py_names);
        }
 
-       ntstatus = cli_lsa_lookup_names(hnd->cli, hnd->mem_ctx, &hnd->pol,
-                                       num_names, names, &sids, &name_types);
+       ntstatus = rpccli_lsa_lookup_names(
+               hnd->cli, mem_ctx, &hnd->pol, num_names, names, 
+               NULL, 1, &sids, &name_types);
 
        if (!NT_STATUS_IS_OK(ntstatus) && NT_STATUS_V(ntstatus) != 0x107) {
                PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
-               return NULL;
+               goto done;
        }
 
        result = PyList_New(num_names);
@@ -196,10 +200,13 @@ static PyObject *lsa_lookup_names(PyObject *self, PyObject *args)
 
                py_from_SID(&sid_obj, &sids[i]);
 
-               obj = Py_BuildValue("(Oi)", sid_obj, name_types[i]);
+               obj = Py_BuildValue("(Ni)", sid_obj, name_types[i]);
 
                PyList_SetItem(result, i, obj);
        }
+
+ done:
+       talloc_destroy(mem_ctx);
        
        return result;
 }
@@ -207,12 +214,13 @@ static PyObject *lsa_lookup_names(PyObject *self, PyObject *args)
 static PyObject *lsa_lookup_sids(PyObject *self, PyObject *args, 
                                 PyObject *kw) 
 {
-       PyObject *py_sids, *result;
+       PyObject *py_sids, *result = NULL;
        NTSTATUS ntstatus;
        int num_sids, i;
        char **domains, **names;
        uint32 *types;
        lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
+       TALLOC_CTX *mem_ctx = NULL;
        DOM_SID *sids;
 
        if (!PyArg_ParseTuple(args, "O", &py_sids))
@@ -223,19 +231,27 @@ static PyObject *lsa_lookup_sids(PyObject *self, PyObject *args,
                return NULL;
        }
 
+       if (!(mem_ctx = talloc_init("lsa_lookup_sids"))) {
+               PyErr_SetString(lsa_error, "unable to init talloc context\n");
+               goto done;
+       }
+
        if (PyList_Check(py_sids)) {
 
                /* Convert dictionary to char ** array */
                
                num_sids = PyList_Size(py_sids);
-               sids = (DOM_SID *)talloc(hnd->mem_ctx, num_sids * sizeof(DOM_SID));
+               sids = (DOM_SID *)_talloc(mem_ctx, num_sids * sizeof(DOM_SID));
                
                memset(sids, 0, num_sids * sizeof(DOM_SID));
                
                for (i = 0; i < num_sids; i++) {
                        PyObject *obj = PyList_GetItem(py_sids, i);
                        
-                       string_to_sid(&sids[i], PyString_AsString(obj));
+                       if (!string_to_sid(&sids[i], PyString_AsString(obj))) {
+                               PyErr_SetString(PyExc_ValueError, "string_to_sid failed");
+                               goto done;
+                       }
                }
 
        } else {
@@ -243,18 +259,21 @@ static PyObject *lsa_lookup_sids(PyObject *self, PyObject *args,
                /* Just a single element */
 
                num_sids = 1;
-               sids = (DOM_SID *)talloc(hnd->mem_ctx, sizeof(DOM_SID));
+               sids = (DOM_SID *)_talloc(mem_ctx, sizeof(DOM_SID));
 
-               string_to_sid(&sids[0], PyString_AsString(py_sids));
+               if (!string_to_sid(&sids[0], PyString_AsString(py_sids))) {
+                       PyErr_SetString(PyExc_ValueError, "string_to_sid failed");
+                       goto done;
+               }
        }
 
-       ntstatus = cli_lsa_lookup_sids(hnd->cli, hnd->mem_ctx, &hnd->pol,
-                                      num_sids, sids, &domains, &names, 
-                                      &types);
+       ntstatus = rpccli_lsa_lookup_sids(
+               hnd->cli, mem_ctx, &hnd->pol, num_sids, sids, &domains, 
+               &names, &types);
 
        if (!NT_STATUS_IS_OK(ntstatus)) {
                PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
-               return NULL;
+               goto done;
        }
 
        result = PyList_New(num_sids);
@@ -268,7 +287,10 @@ static PyObject *lsa_lookup_sids(PyObject *self, PyObject *args,
 
                PyList_SetItem(result, i, obj);
        }
-       
+
+ done:
+       talloc_destroy(mem_ctx);
+
        return result;
 }
 
@@ -284,7 +306,7 @@ static PyObject *lsa_enum_trust_dom(PyObject *self, PyObject *args)
        if (!PyArg_ParseTuple(args, ""))
                return NULL;
        
-       ntstatus = cli_lsa_enum_trust_dom(
+       ntstatus = rpccli_lsa_enum_trust_dom(
                hnd->cli, hnd->mem_ctx, &hnd->pol, &enum_ctx,
                &num_domains, &domain_names, &domain_sids);
 
@@ -377,32 +399,32 @@ static PyMethodDef lsa_methods[] = {
 
        { "setup_logging", (PyCFunction)py_setup_logging, 
          METH_VARARGS | METH_KEYWORDS, 
-         "Set up debug logging.
-
-Initialises Samba's debug logging system.  One argument is expected which
-is a boolean specifying whether debugging is interactive and sent to stdout
-or logged to a file.
-
-Example:
-
->>> spoolss.setup_logging(interactive = 1)" },
+         "Set up debug logging.\n"
+"\n"
+"Initialises Samba's debug logging system.  One argument is expected which\n"
+"is a boolean specifying whether debugging is interactive and sent to stdout\n"
+"or logged to a file.\n"
+"\n"
+"Example:\n"
+"\n"
+">>> lsa.setup_logging(interactive = 1)" },
 
        { "get_debuglevel", (PyCFunction)get_debuglevel, 
          METH_VARARGS, 
-         "Set the current debug level.
-
-Example:
-
->>> spoolss.get_debuglevel()
-0" },
+         "Set the current debug level.\n"
+"\n"
+"Example:\n"
+"\n"
+">>> lsa.get_debuglevel()\n"
+"0" },
 
        { "set_debuglevel", (PyCFunction)set_debuglevel, 
          METH_VARARGS, 
-         "Get the current debug level.
-
-Example:
-
->>> spoolss.set_debuglevel(10)" },
+         "Get the current debug level.\n"
+"\n"
+"Example:\n"
+"\n"
+">>> lsa.set_debuglevel(10)" },
 
        { NULL }
 };