r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[samba.git] / source / python / py_spoolss_printers_conv.c
index b20382922cc4f2f0a0491f7c283383624f903c33..7c3f04011d1c1d8a9cbce9ffaa1a723bce2f4152 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"
 
 struct pyconv py_PRINTER_INFO_0[] = {
-       { "printer_name", PY_UNISTR, offsetof(PRINTER_INFO_0, printername) },
+       { "name", PY_UNISTR, offsetof(PRINTER_INFO_0, printername) },
        { "server_name", PY_UNISTR, offsetof(PRINTER_INFO_0, servername) },
 
        { "cjobs", PY_UINT32, offsetof(PRINTER_INFO_0, cjobs) },
@@ -72,7 +71,7 @@ struct pyconv py_PRINTER_INFO_0[] = {
 };     
 
 struct pyconv py_PRINTER_INFO_1[] = {
-       { "printer_name", PY_UNISTR, offsetof(PRINTER_INFO_1, name) },
+       { "name", PY_UNISTR, offsetof(PRINTER_INFO_1, name) },
        { "description", PY_UNISTR, offsetof(PRINTER_INFO_1, description) },
        { "comment", PY_UNISTR, offsetof(PRINTER_INFO_1, comment) },
        { "flags", PY_UINT32, offsetof(PRINTER_INFO_1, flags) },
@@ -81,7 +80,7 @@ struct pyconv py_PRINTER_INFO_1[] = {
 
 struct pyconv py_PRINTER_INFO_2[] = {
        { "server_name", PY_UNISTR, offsetof(PRINTER_INFO_2, servername) },
-       { "printer_name", PY_UNISTR, offsetof(PRINTER_INFO_2, printername) },
+       { "name", PY_UNISTR, offsetof(PRINTER_INFO_2, printername) },
        { "share_name", PY_UNISTR, offsetof(PRINTER_INFO_2, sharename) },
        { "port_name", PY_UNISTR, offsetof(PRINTER_INFO_2, portname) },
        { "driver_name", PY_UNISTR, offsetof(PRINTER_INFO_2, drivername) },
@@ -154,24 +153,35 @@ BOOL py_from_DEVICEMODE(PyObject **dict, DEVICEMODE *devmode)
 
        PyDict_SetItemString(*dict, "private",
                             PyString_FromStringAndSize(
-                                    devmode->private, devmode->driverextra));
+                                    devmode->dev_private, devmode->driverextra));
 
        return True;
 }
 
 BOOL py_to_DEVICEMODE(DEVICEMODE *devmode, PyObject *dict)
 {
-       PyObject *obj;
+       PyObject *obj, *dict_copy = PyDict_Copy(dict);
+       BOOL result = False;
 
-       to_struct(devmode, dict, py_DEVICEMODE);
+       if (!(obj = PyDict_GetItemString(dict_copy, "private")))
+               goto done;
 
-       if (!(obj = PyDict_GetItemString(dict, "private")))
-               return False;
+       if (!PyString_Check(obj))
+               goto done;
 
-       devmode->private = PyString_AsString(obj);
+       devmode->dev_private = PyString_AsString(obj);
        devmode->driverextra = PyString_Size(obj);
 
-       return True;
+       PyDict_DelItemString(dict_copy, "private");
+
+       if (!to_struct(devmode, dict_copy, py_DEVICEMODE))
+               goto done;
+
+       result = True;
+
+done:
+       Py_DECREF(dict_copy);
+       return result;
 }
 
 /*
@@ -203,7 +213,23 @@ BOOL py_from_PRINTER_INFO_1(PyObject **dict, PRINTER_INFO_1 *info)
 
 BOOL py_to_PRINTER_INFO_1(PRINTER_INFO_1 *info, PyObject *dict)
 {
-       return False;
+       PyObject *obj, *dict_copy = PyDict_Copy(dict);
+       BOOL result = False;
+
+       if (!(obj = PyDict_GetItemString(dict_copy, "level")) ||
+           !PyInt_Check(obj))
+               goto done;
+
+       PyDict_DelItemString(dict_copy, "level");
+
+       if (!to_struct(info, dict_copy, py_PRINTER_INFO_1))
+               goto done;
+
+       result = True;
+
+done:
+       Py_DECREF(dict_copy);
+       return result;
 }
 
 /*
@@ -216,11 +242,21 @@ BOOL py_from_PRINTER_INFO_2(PyObject **dict, PRINTER_INFO_2 *info)
 
        *dict = from_struct(info, py_PRINTER_INFO_2);
 
-       if (py_from_SECDESC(&obj, info->secdesc))
-               PyDict_SetItemString(*dict, "security_descriptor", obj);
+       /* The security descriptor could be NULL */
+
+       if (info->secdesc) {
+               if (py_from_SECDESC(&obj, info->secdesc))
+                       PyDict_SetItemString(*dict, "security_descriptor", obj);
+       }
 
-       if (py_from_DEVICEMODE(&obj, info->devmode))
-               PyDict_SetItemString(*dict, "device_mode", obj);
+       /* Bong!  The devmode could be NULL */
+
+       if (info->devmode)
+               py_from_DEVICEMODE(&obj, info->devmode);
+       else
+               obj = PyDict_New();
+
+       PyDict_SetItemString(*dict, "device_mode", obj);
 
        PyDict_SetItemString(*dict, "level", PyInt_FromLong(2));
 
@@ -230,25 +266,55 @@ BOOL py_from_PRINTER_INFO_2(PyObject **dict, PRINTER_INFO_2 *info)
 BOOL py_to_PRINTER_INFO_2(PRINTER_INFO_2 *info, PyObject *dict,
                          TALLOC_CTX *mem_ctx)
 {
-       PyObject *obj;
+       PyObject *obj, *dict_copy = PyDict_Copy(dict);
+       BOOL result = False;
 
-       to_struct(info, dict, py_PRINTER_INFO_2);
+       /* Convert security descriptor - may be NULL */
 
-       if (!(obj = PyDict_GetItemString(dict, "security_descriptor")))
-               return False;
+       info->secdesc = NULL;
 
-       if (!py_to_SECDESC(&info->secdesc, obj, mem_ctx))
-               return False;
+       if ((obj = PyDict_GetItemString(dict_copy, "security_descriptor"))) {
 
-       if (!(obj = PyDict_GetItemString(dict, "device_mode")))
-               return False;
+               if (!PyDict_Check(obj))
+                       goto done;
+
+               if (!py_to_SECDESC(&info->secdesc, obj, mem_ctx))
+                       goto done;
 
-       info->devmode = talloc(mem_ctx, sizeof(DEVICEMODE));
+               PyDict_DelItemString(dict_copy, "security_descriptor");
+       }
+
+       /* Convert device mode */
+
+       if (!(obj = PyDict_GetItemString(dict_copy, "device_mode"))
+           || !PyDict_Check(obj))
+               goto done;
+
+       info->devmode = _talloc(mem_ctx, sizeof(DEVICEMODE));
 
        if (!py_to_DEVICEMODE(info->devmode, obj))
-               return False;
+               goto done;
 
-       return True;
+       PyDict_DelItemString(dict_copy, "device_mode");
+
+       /* Check info level */
+
+       if (!(obj = PyDict_GetItemString(dict_copy, "level")) ||
+           !PyInt_Check(obj))
+               goto done;
+
+       PyDict_DelItemString(dict_copy, "level");
+
+       /* Convert remaining elements of dictionary */
+
+       if (!to_struct(info, dict_copy, py_PRINTER_INFO_2))
+               goto done;
+
+       result = True;
+
+done:
+       Py_DECREF(dict_copy);
+       return result;
 }
 
 /*
@@ -274,7 +340,8 @@ BOOL py_to_PRINTER_INFO_3(PRINTER_INFO_3 *info, PyObject *dict,
 {
        PyObject *obj;
 
-       to_struct(info, dict, py_PRINTER_INFO_3);
+       if (!to_struct(info, dict, py_PRINTER_INFO_3))
+               return False;
 
        if (!(obj = PyDict_GetItemString(dict, "security_descriptor")))
                return False;