94f4271674420665b658322add61ce4b97f7b01a
[samba.git] / source / python / py_srvsvc.c
1 /* 
2    Python wrappers for DCERPC/SMB client routines.
3
4    Copyright (C) Tim Potter, 2003
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_srvsvc.h"
21
22 /* Exceptions this module can raise */
23
24 PyObject *srvsvc_error, *srvsvc_werror;
25
26 static struct const_vals {
27         char *name;
28         uint32 value;
29 } module_const_vals[] = {
30         { "SV_TYPE_WORKSTATION", SV_TYPE_WORKSTATION },
31         { "SV_TYPE_SERVER", SV_TYPE_SERVER },
32         { "SV_TYPE_SQLSERVER", SV_TYPE_SQLSERVER },
33         { "SV_TYPE_DOMAIN_CTRL", SV_TYPE_DOMAIN_CTRL },
34         { "SV_TYPE_DOMAIN_BAKCTRL", SV_TYPE_DOMAIN_BAKCTRL },
35         { "SV_TYPE_TIME_SOURCE", SV_TYPE_TIME_SOURCE },
36         { "SV_TYPE_AFP", SV_TYPE_AFP },
37         { "SV_TYPE_NOVELL", SV_TYPE_NOVELL },
38         { "SV_TYPE_DOMAIN_MEMBER", SV_TYPE_DOMAIN_MEMBER },
39         { "SV_TYPE_PRINTQ_SERVER", SV_TYPE_PRINTQ_SERVER },
40         { "SV_TYPE_DIALIN_SERVER", SV_TYPE_DIALIN_SERVER },
41         { "SV_TYPE_SERVER_UNIX", SV_TYPE_SERVER_UNIX },
42         { "SV_TYPE_NT", SV_TYPE_NT },
43         { "SV_TYPE_WFW", SV_TYPE_WFW },
44         { "SV_TYPE_SERVER_MFPN", SV_TYPE_SERVER_MFPN },
45         { "SV_TYPE_SERVER_NT", SV_TYPE_SERVER_NT },
46         { "SV_TYPE_POTENTIAL_BROWSER", SV_TYPE_POTENTIAL_BROWSER },
47         { "SV_TYPE_BACKUP_BROWSER", SV_TYPE_BACKUP_BROWSER },
48         { "SV_TYPE_MASTER_BROWSER", SV_TYPE_MASTER_BROWSER },
49         { "SV_TYPE_DOMAIN_MASTER", SV_TYPE_DOMAIN_MASTER },
50         { "SV_TYPE_SERVER_OSF", SV_TYPE_SERVER_OSF },
51         { "SV_TYPE_SERVER_VMS", SV_TYPE_SERVER_VMS },
52         { "SV_TYPE_WIN95_PLUS", SV_TYPE_WIN95_PLUS },
53         { "SV_TYPE_DFS_SERVER", SV_TYPE_DFS_SERVER },
54         { "SV_TYPE_ALTERNATE_XPORT", SV_TYPE_ALTERNATE_XPORT },
55         { "SV_TYPE_LOCAL_LIST_ONLY", SV_TYPE_LOCAL_LIST_ONLY },
56         { "SV_TYPE_DOMAIN_ENUM", SV_TYPE_DOMAIN_ENUM },
57         { NULL },
58 };
59
60 static void const_init(PyObject *dict)
61 {
62         struct const_vals *tmp;
63         PyObject *obj;
64
65         for (tmp = module_const_vals; tmp->name; tmp++) {
66                 obj = PyInt_FromLong(tmp->value);
67                 PyDict_SetItemString(dict, tmp->name, obj);
68                 Py_DECREF(obj);
69         }
70 }
71
72 /* NetServerGetInfo */
73
74 PyObject *srvsvc_netservergetinfo(PyObject *self, PyObject *args,
75                                   PyObject *kw)
76 {
77         static char *kwlist[] = { "server", "level", "creds", NULL };
78         char *unc_name, *server, *errstr;
79         PyObject *creds = NULL, *result = NULL;
80         struct cli_state *cli;
81         TALLOC_CTX *mem_ctx = NULL;
82         uint32 level;
83         SRV_INFO_CTR ctr;
84         WERROR status;
85
86         if (!PyArg_ParseTupleAndKeywords(
87                     args, kw, "si|O", kwlist, &unc_name, &level, &creds))
88                 return NULL;
89
90         if (unc_name[0] != '\\' || unc_name[1] != '\\') {
91                 PyErr_SetString(PyExc_ValueError, "UNC name required");
92                 return NULL;
93         }
94
95         server = SMB_STRDUP(unc_name + 2);
96
97         if (strchr(server, '\\')) {
98                 char *c = strchr(server, '\\');
99                 *c = 0;
100         }
101
102         if (creds && creds != Py_None && !PyDict_Check(creds)) {
103                 PyErr_SetString(PyExc_TypeError, 
104                                 "credentials must be dictionary or None");
105                 return NULL;
106         }
107
108         if (!(cli = open_pipe_creds(server, creds, PI_SRVSVC, &errstr))) {
109                 PyErr_SetString(srvsvc_error, errstr);
110                 free(errstr);
111                 goto done;
112         }
113
114         if (!(mem_ctx = talloc_init("srvsvc_netservergetinfo"))) {
115                 PyErr_SetString(srvsvc_error, 
116                                 "unable to init talloc context\n");
117                 goto done;
118         }
119
120         ZERO_STRUCT(ctr);
121
122         status = rpccli_srvsvc_net_srv_get_info(cli->pipe_list, mem_ctx, level, &ctr);
123
124         if (!NT_STATUS_IS_OK(status)) {
125                 PyErr_SetObject(srvsvc_error, py_werror_tuple(status));
126                 goto done;
127         }
128
129         if (level != ctr.switch_value) {
130                 PyErr_SetString(srvsvc_error, "container level value wrong");
131                 goto done;
132         }
133
134         switch(level) {
135         case 101:
136                 py_from_SRV_INFO_101(&result, &ctr.srv.sv101);
137                 break;
138         }
139
140         Py_INCREF(result);
141
142 done:
143         if (mem_ctx)
144                 talloc_destroy(mem_ctx);
145
146         return result;
147 }
148
149 /*
150  * Module initialisation 
151  */
152
153 static PyMethodDef srvsvc_methods[] = {
154         { "netservergetinfo", (PyCFunction)srvsvc_netservergetinfo,
155           METH_VARARGS | METH_KEYWORDS,
156           "Retrieve information about a particular server." },
157
158         { "setup_logging", (PyCFunction)py_setup_logging, 
159           METH_VARARGS | METH_KEYWORDS, 
160           "Set up debug logging.\n"
161 "\n"
162 "Initialises Samba's debug logging system.  One argument is expected which\n"
163 "is a boolean specifying whether debugging is interactive and sent to stdout\n"
164 "or logged to a file.\n"
165 "\n"
166 "Example:\n"
167 "\n"
168 ">>> srvsvc.setup_logging(interactive = 1)" },
169
170         { "get_debuglevel", (PyCFunction)get_debuglevel, 
171           METH_VARARGS, 
172           "Set the current debug level.\n"
173 "\n"
174 "Example:\n"
175 "\n"
176 ">>> srvsvc.get_debuglevel()\n"
177 "0" },
178
179         { "set_debuglevel", (PyCFunction)set_debuglevel, 
180           METH_VARARGS, 
181           "Get the current debug level.\n"
182 "\n"
183 "Example:\n"
184 "\n"
185 ">>> srvsvc.set_debuglevel(10)" },
186
187         { NULL }
188 };
189
190 void initsrvsvc(void)
191 {
192         PyObject *module, *dict;
193
194         /* Initialise module */
195
196         module = Py_InitModule("srvsvc", srvsvc_methods);
197         dict = PyModule_GetDict(module);
198
199         /* Exceptions we can raise */
200
201         srvsvc_error = PyErr_NewException("srvsvc.error", NULL, NULL);
202         PyDict_SetItemString(dict, "error", srvsvc_error);
203
204         srvsvc_werror = PyErr_NewException("srvsvc.werror", NULL, NULL);
205         PyDict_SetItemString(dict, "werror", srvsvc_werror);
206
207         /* Initialise constants */
208
209         const_init(dict);
210
211         /* Do samba initialisation */
212
213         py_samba_init();
214 }