r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[samba.git] / source / python / py_spoolss_printerdata.c
index 0c62baab7258e2aef97f350cb5fd4de9f01bddf1..5faac0e3919e5b1c866b8e772d5a5d26063190f9 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_spoolss.h"
+#include "python/py_conv.h"
 
 static BOOL py_from_printerdata(PyObject **dict, char *key, char *value,
-                               uint32 data_type, char *data, 
+                               uint16 data_type, uint8 *data, 
                                uint32 data_size) 
 {
        *dict = PyDict_New();
@@ -36,8 +36,8 @@ static BOOL py_from_printerdata(PyObject **dict, char *key, char *value,
        return True;
 }
 
-static BOOL py_to_printerdata(char **key, char **value, uint32 *data_type, 
-                             char **data, uint32 *data_size, 
+static BOOL py_to_printerdata(char **key, char **value, uint16 *data_type, 
+                             uint8 **data, uint32 *data_size, 
                              PyObject *dict)
 {
        PyObject *obj;
@@ -50,10 +50,12 @@ static BOOL py_to_printerdata(char **key, char **value, uint32 *data_type,
                        return False;
                }
 
-               *key = PyString_AsString(obj);
+               if (key) {
+                       *key = PyString_AsString(obj);
 
-               if (!key[0])
-                       *key = NULL;
+                       if (!key[0])
+                               *key = NULL;
+               }
        } else
                *key = NULL;
 
@@ -107,34 +109,30 @@ PyObject *spoolss_hnd_getprinterdata(PyObject *self, PyObject *args, PyObject *k
 {
        spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
        static char *kwlist[] = { "value", NULL };
-       char *value;
+       char *valuename;
        WERROR werror;
-       uint32 needed, data_type, data_size;
-       char *data;
        PyObject *result;
+       REGISTRY_VALUE value;
 
        /* Parse parameters */
 
-       if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &value))
+       if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &valuename))
                return NULL;
 
        /* Call rpc function */
 
-       werror = cli_spoolss_getprinterdata(
-               hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, value,
-               &data_type, &data, &data_size);
-
-       if (W_ERROR_V(werror) == ERRmoredata) 
-               werror = cli_spoolss_getprinterdata(
-                       hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol, value,
-                       &data_type, &data, &data_size);
+       werror = rpccli_spoolss_getprinterdata(
+               hnd->cli, hnd->mem_ctx, &hnd->pol, valuename,
+               &value);
 
        if (!W_ERROR_IS_OK(werror)) {
                PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
                return NULL;
        }
 
-       py_from_printerdata(&result, NULL, value, data_type, data, needed);
+       py_from_printerdata(
+               &result, NULL, valuename, value.type, value.data_p, 
+               value.size);
 
        return result;
 }
@@ -144,22 +142,25 @@ PyObject *spoolss_hnd_setprinterdata(PyObject *self, PyObject *args, PyObject *k
        spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
        static char *kwlist[] = { "data", NULL };
        PyObject *py_data;
-       char *key, *value, *data;
-       uint32 data_size, data_type;
+       char *valuename;
        WERROR werror;
+       REGISTRY_VALUE value;
 
        if (!PyArg_ParseTupleAndKeywords(
                    args, kw, "O!", kwlist, &PyDict_Type, &py_data))
                return NULL;
        
-       if (!py_to_printerdata(&key, &value, &data_type, &data, &data_size, py_data))
+       if (!py_to_printerdata(
+                   NULL, &valuename, &value.type, &value.data_p, 
+                   &value.size, py_data))
                return NULL;
 
+       fstrcpy(value.valuename, valuename);
+       
        /* Call rpc function */
 
-       werror = cli_spoolss_setprinterdata(
-               hnd->cli, hnd->mem_ctx, &hnd->pol, value, data_type,
-               data, data_size);
+       werror = rpccli_spoolss_setprinterdata(
+               hnd->cli, hnd->mem_ctx, &hnd->pol, &value);
 
        if (!W_ERROR_IS_OK(werror)) {
                PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
@@ -174,19 +175,19 @@ PyObject *spoolss_hnd_enumprinterdata(PyObject *self, PyObject *args, PyObject *
 {
        spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
        static char *kwlist[] = { NULL };
-       uint32 data_needed, value_needed, ndx = 0, data_size, data_type;
-       char *value, *data;
+       uint32 data_needed, value_needed, ndx = 0;
        WERROR werror;
        PyObject *result;
+       REGISTRY_VALUE value;
 
        if (!PyArg_ParseTupleAndKeywords(args, kw, "", kwlist))
                return NULL;
 
        /* Get max buffer sizes for value and data */
 
-       werror = cli_spoolss_enumprinterdata(
+       werror = rpccli_spoolss_enumprinterdata(
                hnd->cli, hnd->mem_ctx, &hnd->pol, ndx, 0, 0,
-               &value_needed, &data_needed, NULL, NULL, NULL, NULL);
+               &value_needed, &data_needed, NULL);
 
        if (!W_ERROR_IS_OK(werror)) {
                PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
@@ -200,14 +201,14 @@ PyObject *spoolss_hnd_enumprinterdata(PyObject *self, PyObject *args, PyObject *
        while (W_ERROR_IS_OK(werror)) {
                PyObject *obj;
 
-               werror = cli_spoolss_enumprinterdata(
+               werror = rpccli_spoolss_enumprinterdata(
                        hnd->cli, hnd->mem_ctx, &hnd->pol, ndx,
-                       value_needed, data_needed, NULL, NULL,
-                       &value, &data_type, &data, &data_size); 
+                       value_needed, data_needed, NULL, NULL, &value);
 
                if (py_from_printerdata(
-                           &obj, NULL, value, data_type, data, data_size))
-                       PyDict_SetItemString(result, value, obj);
+                           &obj, NULL, value.valuename, value.type, 
+                           value.data_p, value.size))
+                       PyDict_SetItemString(result, value.valuename, obj);
 
                ndx++;
        }
@@ -229,7 +230,7 @@ PyObject *spoolss_hnd_deleteprinterdata(PyObject *self, PyObject *args, PyObject
 
        /* Call rpc function */
 
-       werror = cli_spoolss_deleteprinterdata(
+       werror = rpccli_spoolss_deleteprinterdata(
                hnd->cli, hnd->mem_ctx, &hnd->pol, value);
 
        if (!W_ERROR_IS_OK(werror)) {
@@ -245,34 +246,29 @@ PyObject *spoolss_hnd_getprinterdataex(PyObject *self, PyObject *args, PyObject
 {
        spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
        static char *kwlist[] = { "key", "value", NULL };
-       char *key, *value;
+       char *key, *valuename;
        WERROR werror;
-       uint32 needed, data_type, data_size;
-       char *data;
        PyObject *result;
+       REGISTRY_VALUE value;
 
        /* Parse parameters */
 
-       if (!PyArg_ParseTupleAndKeywords(args, kw, "ss", kwlist, &key, &value))
+       if (!PyArg_ParseTupleAndKeywords(args, kw, "ss", kwlist, &key, &valuename))
                return NULL;
 
        /* Call rpc function */
 
-       werror = cli_spoolss_getprinterdataex(
-               hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, key,
-               value, &data_type, &data, &data_size);
-
-       if (W_ERROR_V(werror) == ERRmoredata) 
-               werror = cli_spoolss_getprinterdataex(
-                       hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol, key,
-                       value, &data_type, &data, &data_size);
+       werror = rpccli_spoolss_getprinterdataex(
+               hnd->cli, hnd->mem_ctx, &hnd->pol, key,
+               valuename, &value);
 
        if (!W_ERROR_IS_OK(werror)) {
                PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
                return NULL;
        }
 
-       py_from_printerdata(&result, key, value, data_type, data, needed);
+       py_from_printerdata(
+               &result, key, valuename, value.type, value.data_p, value.size);
 
        return result;
 }
@@ -282,22 +278,24 @@ PyObject *spoolss_hnd_setprinterdataex(PyObject *self, PyObject *args, PyObject
        spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
        static char *kwlist[] = { "data", NULL };
        PyObject *py_data;
-       char *key, *value, *data;
-       uint32 data_size, data_type;
+       char *keyname, *valuename;
        WERROR werror;
+       REGISTRY_VALUE value;
 
        if (!PyArg_ParseTupleAndKeywords(
                    args, kw, "O!", kwlist, &PyDict_Type, &py_data))
                return NULL;
        
-       if (!py_to_printerdata(&key, &value, &data_type, &data, &data_size, py_data))
+       if (!py_to_printerdata(
+                   &keyname, &valuename, &value.type, &value.data_p, &value.size, py_data))
                return NULL;
 
+       fstrcpy(value.valuename,  valuename);
+
        /* Call rpc function */
 
-       werror = cli_spoolss_setprinterdataex(
-               hnd->cli, hnd->mem_ctx, &hnd->pol, key, value, data_type,
-               data, data_size);
+       werror = rpccli_spoolss_setprinterdataex(
+               hnd->cli, hnd->mem_ctx, &hnd->pol, keyname, &value);
 
        if (!W_ERROR_IS_OK(werror)) {
                PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
@@ -311,26 +309,25 @@ PyObject *spoolss_hnd_setprinterdataex(PyObject *self, PyObject *args, PyObject
 PyObject *spoolss_hnd_enumprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
 {
        spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
-       static char *kwlist[] = { NULL };
-       uint32 needed, returned, i;
+       static char *kwlist[] = { "key", NULL };
+       uint32 i;
        char *key;
        WERROR werror;
        PyObject *result;
-       PRINTER_ENUM_VALUES *values;
+       REGVAL_CTR *ctr;
 
        if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &key))
                return NULL;
 
-       /* Get max buffer sizes for value and data */
+       if (!(ctr = TALLOC_ZERO_P(hnd->mem_ctx, REGVAL_CTR))) {
+               PyErr_SetString(spoolss_error, "talloc failed");
+               return NULL;
+       }
 
-       werror = cli_spoolss_enumprinterdataex(
-               hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, key,
-               &returned, &values);
+       /* Get max buffer sizes for value and data */
 
-       if (W_ERROR_V(werror) == ERRmoredata) 
-               werror = cli_spoolss_enumprinterdataex(
-                       hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol, key,
-                       &returned, &values);
+       werror = rpccli_spoolss_enumprinterdataex(
+               hnd->cli, hnd->mem_ctx, &hnd->pol, key, &ctr);
 
        if (!W_ERROR_IS_OK(werror)) {
                PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
@@ -341,16 +338,17 @@ PyObject *spoolss_hnd_enumprinterdataex(PyObject *self, PyObject *args, PyObject
 
        result = PyDict_New();
 
-       for (i = 0; i < returned; i++) {
+       for (i = 0; i < regval_ctr_numvals(&ctr); i++) {
+               REGISTRY_VALUE *value;
                PyObject *item;
-               fstring value = "";
 
-               rpcstr_pull(value, values[i].valuename.buffer, sizeof(value), -1, STR_TERMINATE);
                item = PyDict_New();
-               py_from_printerdata(&item, key, value, values[i].type, values[i].data, 
-                                   values[i].data_len);
+               value = regval_ctr_specific_value(&ctr, i);
 
-               PyDict_SetItemString(result, value, item);
+               if (py_from_printerdata(
+                           &item, key, value->valuename, value->type, 
+                           value->data_p, value->size))
+                       PyDict_SetItemString(result, value->valuename, item);
        }
        
        return result;
@@ -370,7 +368,7 @@ PyObject *spoolss_hnd_deleteprinterdataex(PyObject *self, PyObject *args, PyObje
 
        /* Call rpc function */
 
-       werror = cli_spoolss_deleteprinterdataex(
+       werror = rpccli_spoolss_deleteprinterdataex(
                hnd->cli, hnd->mem_ctx, &hnd->pol, key, value);
 
        if (!W_ERROR_IS_OK(werror)) {
@@ -381,3 +379,54 @@ PyObject *spoolss_hnd_deleteprinterdataex(PyObject *self, PyObject *args, PyObje
        Py_INCREF(Py_None);
        return Py_None;
 }
+
+PyObject *spoolss_hnd_enumprinterkey(PyObject *self, PyObject *args,
+                                    PyObject *kw)
+{
+       spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
+       static char *kwlist[] = { "key", NULL };
+       char *keyname;
+       WERROR werror;
+       uint32 keylist_len;
+       uint16 *keylist;
+       PyObject *result;
+
+       /* Parse parameters */
+
+       if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &keyname))
+               return NULL;
+
+       /* Call rpc function */
+
+       werror = rpccli_spoolss_enumprinterkey(
+               hnd->cli, hnd->mem_ctx, &hnd->pol, keyname, &keylist, 
+               &keylist_len);
+
+       if (!W_ERROR_IS_OK(werror)) {
+               PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
+               return NULL;
+       }
+
+       result = from_unistr_list(keylist);
+
+       return result;
+}
+
+#if 0
+
+PyObject *spoolss_hnd_deleteprinterkey(PyObject *self, PyObject *args,
+                                      PyObject *kw)
+{
+       spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
+       static char *kwlist[] = { "key", NULL };
+       char *keyname;
+       WERROR werror;
+
+       if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &keyname))
+               return NULL;
+
+       Py_INCREF(Py_None);
+       return Py_None;
+}
+
+#endif