r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "python/py_spoolss.h"
21
22 /* Enumerate ports */
23
24 PyObject *spoolss_enumports(PyObject *self, PyObject *args, PyObject *kw)
25 {
26         WERROR werror;
27         PyObject *result = NULL, *creds = NULL;
28         uint32 level = 1;
29         uint32 i, needed, num_ports;
30         static char *kwlist[] = {"server", "level", "creds", NULL};
31         TALLOC_CTX *mem_ctx = NULL;
32         struct cli_state *cli = NULL;
33         char *server, *errstr;
34         PORT_INFO_CTR ctr;
35
36         /* Parse parameters */
37
38         if (!PyArg_ParseTupleAndKeywords(
39                     args, kw, "s|iO", kwlist, &server, &level, &creds))
40                 return NULL;
41         
42         if (server[0] != '\\' || server[1] != '\\') {
43                 PyErr_SetString(PyExc_ValueError, "UNC name required");
44                 return NULL;
45         }
46
47         server += 2;
48
49         if (creds && creds != Py_None && !PyDict_Check(creds)) {
50                 PyErr_SetString(PyExc_TypeError, 
51                                 "credentials must be dictionary or None");
52                 return NULL;
53         }
54
55         if (!(cli = open_pipe_creds(server, creds, PI_SPOOLSS, &errstr))) {
56                 PyErr_SetString(spoolss_error, errstr);
57                 free(errstr);
58                 goto done;
59         }
60
61         if (!(mem_ctx = talloc_init("spoolss_enumports"))) {
62                 PyErr_SetString(
63                         spoolss_error, "unable to init talloc context\n");
64                 goto done;
65         }
66
67         /* Call rpc function */
68         
69         werror = rpccli_spoolss_enum_ports( 
70                 cli->pipe_list, mem_ctx, level, &num_ports, &ctr);
71
72         if (!W_ERROR_IS_OK(werror)) {
73                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
74                 goto done;
75         }
76
77         /* Return value */
78         
79         switch (level) {
80         case 1: 
81                 result = PyDict_New();
82
83                 for (i = 0; i < num_ports; i++) {
84                         PyObject *value;
85                         fstring name;
86
87                         rpcstr_pull(name, ctr.port.info_1[i].port_name.buffer,
88                                     sizeof(fstring), -1, STR_TERMINATE);
89
90                         py_from_PORT_INFO_1(&value, &ctr.port.info_1[i]);
91
92                         PyDict_SetItemString(
93                                 value, "level", PyInt_FromLong(1));
94
95                         PyDict_SetItemString(result, name, value);
96                 }
97
98                 break;
99         case 2:
100                 result = PyDict_New();
101
102                 for(i = 0; i < num_ports; i++) {
103                         PyObject *value;
104                         fstring name;
105
106                         rpcstr_pull(name, ctr.port.info_2[i].port_name.buffer,
107                                     sizeof(fstring), -1, STR_TERMINATE);
108
109                         py_from_PORT_INFO_2(&value, &ctr.port.info_2[i]);
110
111                         PyDict_SetItemString(
112                                 value, "level", PyInt_FromLong(2));
113
114                         PyDict_SetItemString(result, name, value);
115                 }
116                 
117                 break;
118         default:
119                 PyErr_SetString(spoolss_error, "unknown info level");
120                 goto done;
121         }
122
123  done:
124         if (cli)
125                 cli_shutdown(cli);
126         
127         if (mem_ctx)
128                 talloc_destroy(mem_ctx);
129
130         return result;
131 }