Added SRVSVC pipe support. I've started implementing NetServerGetInfo()
[sfrench/samba-autobuild/.git] / source3 / 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         { NULL },
32 };
33
34 static void const_init(PyObject *dict)
35 {
36         struct const_vals *tmp;
37         PyObject *obj;
38
39         for (tmp = module_const_vals; tmp->name; tmp++) {
40                 obj = PyInt_FromLong(tmp->value);
41                 PyDict_SetItemString(dict, tmp->name, obj);
42                 Py_DECREF(obj);
43         }
44 }
45
46 /* NetServerGetInfo */
47
48 PyObject *srvsvc_netservergetinfo(PyObject *self, PyObject *args,
49                                   PyObject *kw)
50 {
51         static char *kwlist[] = { "server", "level", "creds", NULL };
52         char *unc_name, *server, *errstr;
53         PyObject *creds = NULL, *result = NULL;
54         struct cli_state *cli;
55         TALLOC_CTX *mem_ctx = NULL;
56         uint32 level;
57         SRV_INFO_CTR ctr;
58         WERROR status;
59
60         if (!PyArg_ParseTupleAndKeywords(
61                     args, kw, "si|O", kwlist, &unc_name, &level, &creds))
62                 return NULL;
63
64         if (unc_name[0] != '\\' || unc_name[1] != '\\') {
65                 PyErr_SetString(PyExc_ValueError, "UNC name required");
66                 return NULL;
67         }
68
69         server = strdup(unc_name + 2);
70
71         if (strchr(server, '\\')) {
72                 char *c = strchr(server, '\\');
73                 *c = 0;
74         }
75
76         if (creds && creds != Py_None && !PyDict_Check(creds)) {
77                 PyErr_SetString(PyExc_TypeError, 
78                                 "credentials must be dictionary or None");
79                 return NULL;
80         }
81
82         if (!(cli = open_pipe_creds(server, creds, PI_SRVSVC, &errstr))) {
83                 PyErr_SetString(srvsvc_error, errstr);
84                 free(errstr);
85                 goto done;
86         }
87
88         if (!(mem_ctx = talloc_init("srvsvc_netservergetinfo"))) {
89                 PyErr_SetString(srvsvc_error, 
90                                 "unable to init talloc context\n");
91                 goto done;
92         }
93
94         ZERO_STRUCT(ctr);
95
96         status = cli_srvsvc_net_srv_get_info(cli, mem_ctx, level, &ctr);
97
98         if (!NT_STATUS_IS_OK(status)) {
99                 PyErr_SetObject(srvsvc_error, py_werror_tuple(status));
100                 goto done;
101         }
102
103         result = Py_None;
104         Py_INCREF(Py_None);
105
106 done:
107         if (mem_ctx)
108                 talloc_destroy(mem_ctx);
109
110         return result;
111 }
112
113 /*
114  * Module initialisation 
115  */
116
117 static PyMethodDef srvsvc_methods[] = {
118         { "netservergetinfo", (PyCFunction)srvsvc_netservergetinfo,
119           METH_VARARGS | METH_KEYWORDS,
120           "Retrieve information about a particular server." },
121
122         { "setup_logging", (PyCFunction)py_setup_logging, 
123           METH_VARARGS | METH_KEYWORDS, 
124           "Set up debug logging.
125
126 Initialises Samba's debug logging system.  One argument is expected which
127 is a boolean specifying whether debugging is interactive and sent to stdout
128 or logged to a file.
129
130 Example:
131
132 >>> srvsvc.setup_logging(interactive = 1)" },
133
134         { "get_debuglevel", (PyCFunction)get_debuglevel, 
135           METH_VARARGS, 
136           "Set the current debug level.
137
138 Example:
139
140 >>> srvsvc.get_debuglevel()
141 0" },
142
143         { "set_debuglevel", (PyCFunction)set_debuglevel, 
144           METH_VARARGS, 
145           "Get the current debug level.
146
147 Example:
148
149 >>> srvsvc.set_debuglevel(10)" },
150
151         { NULL }
152 };
153
154 void initsrvsvc(void)
155 {
156         PyObject *module, *dict;
157
158         /* Initialise module */
159
160         module = Py_InitModule("srvsvc", srvsvc_methods);
161         dict = PyModule_GetDict(module);
162
163         /* Exceptions we can raise */
164
165         srvsvc_error = PyErr_NewException("srvsvc.error", NULL, NULL);
166         PyDict_SetItemString(dict, "error", srvsvc_error);
167
168         srvsvc_werror = PyErr_NewException("srvsvc.werror", NULL, NULL);
169         PyDict_SetItemString(dict, "werror", srvsvc_werror);
170
171         /* Initialise constants */
172
173         const_init(dict);
174
175         /* Do samba initialisation */
176
177         py_samba_init();
178 }