r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[sfrench/samba-autobuild/.git] / source3 / python / py_spoolss_drivers.c
index 19fe5800aa7e73b488991df51ddc508ac909a002..7e84aa1a4a00be523b91c6bed4349e6e3afc6598 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,
@@ -14,8 +14,7 @@
    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_spoolss.h"
@@ -29,7 +28,7 @@ PyObject *spoolss_enumprinterdrivers(PyObject *self, PyObject *args,
        PyObject *result = NULL, *creds = NULL;
        PRINTER_DRIVER_CTR ctr;
        int level = 1, i;
-       uint32 needed, num_drivers;
+       uint32 num_drivers;
        char *arch = "Windows NT x86", *server, *errstr;
        static char *kwlist[] = {"server", "level", "creds", "arch", NULL};
        struct cli_state *cli = NULL;
@@ -43,7 +42,7 @@ PyObject *spoolss_enumprinterdrivers(PyObject *self, PyObject *args,
                return NULL;
        
        if (server[0] != '\\' || server[1] != '\\') {
-               PyErr_SetString(spoolss_error, "bad server name");
+               PyErr_SetString(PyExc_ValueError, "UNC name required");
                return NULL;
        }
 
@@ -57,27 +56,22 @@ PyObject *spoolss_enumprinterdrivers(PyObject *self, PyObject *args,
 
        /* Call rpc function */
        
-       if (!(cli = open_pipe_creds(server, creds, PIPE_SPOOLSS, &errstr))) {
+       if (!(cli = open_pipe_creds(server, creds, PI_SPOOLSS, &errstr))) {
                PyErr_SetString(spoolss_error, errstr);
                free(errstr);
                goto done;
        }
 
-       if (!(mem_ctx = talloc_init())) {
+       if (!(mem_ctx = talloc_init("spoolss_enumprinterdrivers"))) {
                PyErr_SetString(
                        spoolss_error, "unable to init talloc context\n");
                goto done;
        }       
 
-       werror = cli_spoolss_enumprinterdrivers(
-               cli, mem_ctx, 0, &needed, level, arch,
+       werror = rpccli_spoolss_enumprinterdrivers(
+               cli->pipe_list, mem_ctx, level, arch,
                &num_drivers, &ctr);
 
-       if (W_ERROR_V(werror) == ERRinsufficientbuffer)
-               werror = cli_spoolss_enumprinterdrivers(
-                       cli, mem_ctx, needed, NULL, level, arch, 
-                       &num_drivers, &ctr);
-
        if (!W_ERROR_IS_OK(werror)) {
                PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
                goto done;
@@ -98,9 +92,6 @@ PyObject *spoolss_enumprinterdrivers(PyObject *self, PyObject *args,
 
                        py_from_DRIVER_INFO_1(&value, &ctr.info1[i]);
 
-                       PyDict_SetItemString(
-                               value, "level", PyInt_FromLong(1));
-
                        PyDict_SetItemString(result, name, value);
                }
                
@@ -117,9 +108,6 @@ PyObject *spoolss_enumprinterdrivers(PyObject *self, PyObject *args,
 
                        py_from_DRIVER_INFO_2(&value, &ctr.info2[i]);
 
-                       PyDict_SetItemString(
-                               value, "level", PyInt_FromLong(2));
-
                        PyDict_SetItemString(result, name, value);
                }
 
@@ -136,9 +124,6 @@ PyObject *spoolss_enumprinterdrivers(PyObject *self, PyObject *args,
 
                        py_from_DRIVER_INFO_3(&value, &ctr.info3[i]);
 
-                       PyDict_SetItemString(
-                               value, "level", PyInt_FromLong(3));
-
                        PyDict_SetItemString(result, name, value);
                }
 
@@ -155,9 +140,6 @@ PyObject *spoolss_enumprinterdrivers(PyObject *self, PyObject *args,
 
                        py_from_DRIVER_INFO_6(&value, &ctr.info6[i]);
 
-                       PyDict_SetItemString(
-                               value, "level", PyInt_FromLong(6));
-
                        PyList_SetItem(result, i, value);
                }
 
@@ -187,8 +169,8 @@ PyObject *spoolss_hnd_getprinterdriver(PyObject *self, PyObject *args,
        PyObject *result = Py_None;
        PRINTER_DRIVER_CTR ctr;
        int level = 1;
-       uint32 needed;
        char *arch = "Windows NT x86";
+       int version = 2;
        static char *kwlist[] = {"level", "arch", NULL};
 
        /* Parse parameters */
@@ -199,14 +181,8 @@ PyObject *spoolss_hnd_getprinterdriver(PyObject *self, PyObject *args,
 
        /* Call rpc function */
 
-       werror = cli_spoolss_getprinterdriver(
-               hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, level,
-               arch, &ctr);
-
-       if (W_ERROR_V(werror) == ERRinsufficientbuffer)
-               werror = cli_spoolss_getprinterdriver(
-                       hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
-                       level, arch, &ctr);
+       werror = rpccli_spoolss_getprinterdriver(
+               hnd->cli, hnd->mem_ctx, &hnd->pol, level, arch, version, &ctr);
 
        if (!W_ERROR_IS_OK(werror)) {
                PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
@@ -226,7 +202,7 @@ PyObject *spoolss_hnd_getprinterdriver(PyObject *self, PyObject *args,
                py_from_DRIVER_INFO_3(&result, ctr.info3);
                break;
        case 6:
-               py_from_DRIVER_INFO_6(&result,  ctr.info6);
+               py_from_DRIVER_INFO_6(&result, ctr.info6);
                break;
        default:
                PyErr_SetString(spoolss_error, "unsupported info level");
@@ -245,7 +221,7 @@ PyObject *spoolss_getprinterdriverdir(PyObject *self, PyObject *args,
        WERROR werror;
        PyObject *result = NULL, *creds = NULL;
        DRIVER_DIRECTORY_CTR ctr;
-       uint32 needed, level = 1;
+       uint32 level = 1;
        char *arch = "Windows NT x86", *server, *errstr;
        static char *kwlist[] = {"server", "level", "arch", "creds", NULL};
        struct cli_state *cli = NULL;
@@ -259,7 +235,7 @@ PyObject *spoolss_getprinterdriverdir(PyObject *self, PyObject *args,
                return NULL;
 
        if (server[0] != '\\' || server[1] != '\\') {
-               PyErr_SetString(spoolss_error, "bad server name");
+               PyErr_SetString(PyExc_ValueError, "UNC name required");
                return NULL;
        }
 
@@ -273,24 +249,20 @@ PyObject *spoolss_getprinterdriverdir(PyObject *self, PyObject *args,
 
        /* Call rpc function */
 
-       if (!(cli = open_pipe_creds(server, creds, PIPE_SPOOLSS, &errstr))) {
+       if (!(cli = open_pipe_creds(server, creds, PI_SPOOLSS, &errstr))) {
                PyErr_SetString(spoolss_error, errstr);
                free(errstr);
                goto done;
        }
        
-       if (!(mem_ctx = talloc_init())) {
+       if (!(mem_ctx = talloc_init("spoolss_getprinterdriverdir"))) {
                PyErr_SetString(
                        spoolss_error, "unable to init talloc context\n");
                goto done;
        }       
 
-       werror = cli_spoolss_getprinterdriverdir(
-               cli, mem_ctx, 0, &needed, level, arch, &ctr);
-
-       if (W_ERROR_V(werror) == ERRinsufficientbuffer)
-               werror = cli_spoolss_getprinterdriverdir(
-                       cli, mem_ctx, needed, NULL, level, arch, &ctr);
+       werror = rpccli_spoolss_getprinterdriverdir(
+               cli->pipe_list, mem_ctx, level, arch, &ctr);
 
        if (!W_ERROR_IS_OK(werror)) {
                PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
@@ -302,8 +274,6 @@ PyObject *spoolss_getprinterdriverdir(PyObject *self, PyObject *args,
        switch (level) {
        case 1:
                py_from_DRIVER_DIRECTORY_1(&result, ctr.info1);
-               PyDict_SetItemString(
-                       result, "level", PyInt_FromLong(1));
                break;
        default:
                PyErr_SetString(spoolss_error, "unknown info level");
@@ -340,7 +310,7 @@ PyObject *spoolss_addprinterdriver(PyObject *self, PyObject *args,
                    &info, &creds))
                return NULL;
        
-       if (server[0] == '\\' && server[1] == '\\')
+       if (server[0] == '\\' || server[1] == '\\')
                server += 2;
 
        if (creds && creds != Py_None && !PyDict_Check(creds)) {
@@ -349,13 +319,13 @@ PyObject *spoolss_addprinterdriver(PyObject *self, PyObject *args,
                return NULL;
        }
 
-       if (!(mem_ctx = talloc_init())) {
+       if (!(mem_ctx = talloc_init("spoolss_addprinterdriver"))) {
                PyErr_SetString(
                        spoolss_error, "unable to init talloc context\n");
                return NULL;
        }
 
-       if (!(cli = open_pipe_creds(server, creds, PIPE_SPOOLSS, &errstr))) {
+       if (!(cli = open_pipe_creds(server, creds, PI_SPOOLSS, &errstr))) {
                PyErr_SetString(spoolss_error, errstr);
                free(errstr);
                goto done;
@@ -372,12 +342,13 @@ PyObject *spoolss_addprinterdriver(PyObject *self, PyObject *args,
        }
 
        ZERO_STRUCT(ctr);
-       
+       ZERO_STRUCT(dinfo);
+
        switch(level) {
        case 3:
                ctr.info3 = &dinfo.driver_3;
 
-               if (!py_to_DRIVER_INFO_3(&dinfo.driver_3, info)) {
+               if (!py_to_DRIVER_INFO_3(&dinfo.driver_3, info, mem_ctx)) {
                        PyErr_SetString(spoolss_error,
                                        "error converting to driver info 3");
                        goto done;
@@ -389,7 +360,7 @@ PyObject *spoolss_addprinterdriver(PyObject *self, PyObject *args,
                goto done;
        }
 
-       werror = cli_spoolss_addprinterdriver(cli, mem_ctx, level, &ctr);
+       werror = rpccli_spoolss_addprinterdriver(cli->pipe_list, mem_ctx, level, &ctr);
 
        if (!W_ERROR_IS_OK(werror)) {
                PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));