open_pipe_creds() now takes a pipe index instead of a pipe name.
[samba.git] / source / python / py_spoolss_ports.c
index fb3f47c746a0236dd110d9e1ad355ae634016082..75c8c4aa28b113ceb5f026983cbda3abd0df9609 100644 (file)
@@ -25,7 +25,7 @@
 PyObject *spoolss_enumports(PyObject *self, PyObject *args, PyObject *kw)
 {
        WERROR werror;
-       PyObject *result, *creds = NULL;
+       PyObject *result = NULL, *creds = NULL;
        int level = 1;
        uint32 i, needed, num_ports;
        static char *kwlist[] = {"server", "level", "creds", NULL};
@@ -37,24 +37,32 @@ PyObject *spoolss_enumports(PyObject *self, PyObject *args, PyObject *kw)
        /* Parse parameters */
 
        if (!PyArg_ParseTupleAndKeywords(
-                   args, kw, "s|iO!", kwlist, &server, &creds, &level,
-                   &PyDict_Type))
+                   args, kw, "s|iO", kwlist, &server, &level, &creds))
                return NULL;
        
-       if (server[0] == '\\' && server[1] == '\\')
-               server += 2;
+       if (server[0] != '\\' || server[1] != '\\') {
+               PyErr_SetString(PyExc_ValueError, "UNC name required");
+               return NULL;
+       }
 
-       if (!(cli = open_pipe_creds(
-                     server, creds, cli_spoolss_initialise, &errstr))) {
+       server += 2;
+
+       if (creds && creds != Py_None && !PyDict_Check(creds)) {
+               PyErr_SetString(PyExc_TypeError, 
+                               "credentials must be dictionary or None");
+               return NULL;
+       }
+
+       if (!(cli = open_pipe_creds(server, creds, PI_SPOOLSS, &errstr))) {
                PyErr_SetString(spoolss_error, errstr);
                free(errstr);
-               return NULL;
+               goto done;
        }
 
        if (!(mem_ctx = talloc_init())) {
                PyErr_SetString(
-                       spoolss_error, "unable to initialise talloc context\n");
-               return NULL;
+                       spoolss_error, "unable to init talloc context\n");
+               goto done;
        }
 
        /* Call rpc function */
@@ -67,39 +75,63 @@ PyObject *spoolss_enumports(PyObject *self, PyObject *args, PyObject *kw)
                        cli, mem_ctx, needed, NULL, level,
                        &num_ports, &ctr);
 
-       /* Return value */
-       
-       result = Py_None;
-
-       if (!W_ERROR_IS_OK(werror))
+       if (!W_ERROR_IS_OK(werror)) {
+               PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
                goto done;
+       }
 
-       result = PyList_New(num_ports);
-
+       /* Return value */
+       
        switch (level) {
        case 1: 
+               result = PyDict_New();
+
                for (i = 0; i < num_ports; i++) {
                        PyObject *value;
+                       fstring name;
+
+                       rpcstr_pull(name, ctr.port.info_1[i].port_name.buffer,
+                                   sizeof(fstring), -1, STR_TERMINATE);
 
                        py_from_PORT_INFO_1(&value, &ctr.port.info_1[i]);
 
-                       PyList_SetItem(result, i, value);
+                       PyDict_SetItemString(
+                               value, "level", PyInt_FromLong(1));
+
+                       PyDict_SetItemString(result, name, value);
                }
 
                break;
        case 2:
+               result = PyDict_New();
+
                for(i = 0; i < num_ports; i++) {
                        PyObject *value;
+                       fstring name;
+
+                       rpcstr_pull(name, ctr.port.info_2[i].port_name.buffer,
+                                   sizeof(fstring), -1, STR_TERMINATE);
 
                        py_from_PORT_INFO_2(&value, &ctr.port.info_2[i]);
 
-                       PyList_SetItem(result, i, value);
+                       PyDict_SetItemString(
+                               value, "level", PyInt_FromLong(2));
+
+                       PyDict_SetItemString(result, name, value);
                }
                
                break;
+       default:
+               PyErr_SetString(spoolss_error, "unknown info level");
+               goto done;
        }
 
  done:
-       Py_INCREF(result);
+       if (cli)
+               cli_shutdown(cli);
+       
+       if (mem_ctx)
+               talloc_destroy(mem_ctx);
+
        return result;
 }