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