Initial import
[samba] / 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         uint32 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, &creds))
41                 return NULL;
42         
43         if (server[0] != '\\' || server[1] != '\\') {
44                 PyErr_SetString(PyExc_ValueError, "UNC name required");
45                 return NULL;
46         }
47
48         server += 2;
49
50         if (creds && creds != Py_None && !PyDict_Check(creds)) {
51                 PyErr_SetString(PyExc_TypeError, 
52                                 "credentials must be dictionary or None");
53                 return NULL;
54         }
55
56         if (!(cli = open_pipe_creds(server, creds, PI_SPOOLSS, &errstr))) {
57                 PyErr_SetString(spoolss_error, errstr);
58                 free(errstr);
59                 goto done;
60         }
61
62         if (!(mem_ctx = talloc_init("spoolss_enumports"))) {
63                 PyErr_SetString(
64                         spoolss_error, "unable to init talloc context\n");
65                 goto done;
66         }
67
68         /* Call rpc function */
69         
70         werror = rpccli_spoolss_enum_ports( 
71                 cli, mem_ctx, level, &num_ports, &ctr);
72
73         if (!W_ERROR_IS_OK(werror)) {
74                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
75                 goto done;
76         }
77
78         /* Return value */
79         
80         switch (level) {
81         case 1: 
82                 result = PyDict_New();
83
84                 for (i = 0; i < num_ports; i++) {
85                         PyObject *value;
86                         fstring name;
87
88                         rpcstr_pull(name, ctr.port.info_1[i].port_name.buffer,
89                                     sizeof(fstring), -1, STR_TERMINATE);
90
91                         py_from_PORT_INFO_1(&value, &ctr.port.info_1[i]);
92
93                         PyDict_SetItemString(
94                                 value, "level", PyInt_FromLong(1));
95
96                         PyDict_SetItemString(result, name, value);
97                 }
98
99                 break;
100         case 2:
101                 result = PyDict_New();
102
103                 for(i = 0; i < num_ports; i++) {
104                         PyObject *value;
105                         fstring name;
106
107                         rpcstr_pull(name, ctr.port.info_2[i].port_name.buffer,
108                                     sizeof(fstring), -1, STR_TERMINATE);
109
110                         py_from_PORT_INFO_2(&value, &ctr.port.info_2[i]);
111
112                         PyDict_SetItemString(
113                                 value, "level", PyInt_FromLong(2));
114
115                         PyDict_SetItemString(result, name, value);
116                 }
117                 
118                 break;
119         default:
120                 PyErr_SetString(spoolss_error, "unknown info level");
121                 goto done;
122         }
123
124  done:
125         if (cli)
126                 cli_shutdown(cli);
127         
128         if (mem_ctx)
129                 talloc_destroy(mem_ctx);
130
131         return result;
132 }