f0bea5175d94412a9ab841b2284a7929bd991a92
[samba.git] / source / python / py_spoolss_ports.c
1 /* 
2    Python wrappers for DCERPC/SMB client routines.
3
4    Copyright (C) Tim Potter, 2002
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "python/py_spoolss.h"
22
23 /* Enumerate ports */
24
25 PyObject *spoolss_enumports(PyObject *self, PyObject *args, PyObject *kw)
26 {
27         WERROR werror;
28         PyObject *result = NULL, *creds = NULL;
29         int level = 1;
30         uint32 i, needed, num_ports;
31         static char *kwlist[] = {"server", "level", "creds", NULL};
32         TALLOC_CTX *mem_ctx = NULL;
33         struct cli_state *cli = NULL;
34         char *server, *errstr;
35         PORT_INFO_CTR ctr;
36
37         /* Parse parameters */
38
39         if (!PyArg_ParseTupleAndKeywords(
40                     args, kw, "s|iO!", kwlist, &server, &level,
41                     &PyDict_Type, &creds))
42                 return NULL;
43         
44         if (server[0] == '\\' && server[1] == '\\')
45                 server += 2;
46
47         if (!(cli = open_pipe_creds(server, creds, PIPE_SPOOLSS, &errstr))) {
48                 PyErr_SetString(spoolss_error, errstr);
49                 free(errstr);
50                 goto done;
51         }
52
53         if (!(mem_ctx = talloc_init())) {
54                 PyErr_SetString(
55                         spoolss_error, "unable to init talloc context\n");
56                 goto done;
57         }
58
59         /* Call rpc function */
60         
61         werror = cli_spoolss_enum_ports(
62                 cli, mem_ctx, 0, &needed, level, &num_ports, &ctr);
63
64         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
65                 werror = cli_spoolss_enum_ports(
66                         cli, mem_ctx, needed, NULL, level,
67                         &num_ports, &ctr);
68
69         if (!W_ERROR_IS_OK(werror)) {
70                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
71                 goto done;
72         }
73
74         /* Return value */
75         
76         switch (level) {
77         case 1: 
78                 result = PyDict_New();
79
80                 for (i = 0; i < num_ports; i++) {
81                         PyObject *value;
82                         fstring name;
83
84                         rpcstr_pull(name, ctr.port.info_1[i].port_name.buffer,
85                                     sizeof(fstring), -1, STR_TERMINATE);
86
87                         py_from_PORT_INFO_1(&value, &ctr.port.info_1[i]);
88
89                         PyDict_SetItemString(
90                                 value, "level", PyInt_FromLong(1));
91
92                         PyDict_SetItemString(result, name, value);
93                 }
94
95                 break;
96         case 2:
97                 result = PyDict_New();
98
99                 for(i = 0; i < num_ports; i++) {
100                         PyObject *value;
101                         fstring name;
102
103                         rpcstr_pull(name, ctr.port.info_2[i].port_name.buffer,
104                                     sizeof(fstring), -1, STR_TERMINATE);
105
106                         py_from_PORT_INFO_2(&value, &ctr.port.info_2[i]);
107
108                         PyDict_SetItemString(
109                                 value, "level", PyInt_FromLong(2));
110
111                         PyDict_SetItemString(result, name, value);
112                 }
113                 
114                 break;
115         default:
116                 PyErr_SetString(spoolss_error, "unknown info level");
117                 goto done;
118         }
119
120  done:
121         if (cli)
122                 cli_shutdown(cli);
123         
124         if (mem_ctx)
125                 talloc_destroy(mem_ctx);
126
127         return result;
128 }