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/* Add a form */
24
25PyObject *spoolss_hnd_addform(PyObject *self, PyObject *args, PyObject *kw)
26{
27	spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
28	WERROR werror;
29	PyObject *info;
30	FORM form;
31	int level;
32	static char *kwlist[] = {"form", NULL};
33
34	/* Parse parameters */
35
36	if (!PyArg_ParseTupleAndKeywords(
37		    args, kw, "O!", kwlist, &PyDict_Type, &info))
38		return NULL;
39
40	/* Call rpc function */
41
42	if (!py_to_FORM(&form, info)) {
43		PyErr_SetString(spoolss_error, "invalid form");
44		return NULL;
45	}
46
47	if (!get_level_value(info, &level)) {
48		PyErr_SetString(spoolss_error, "invalid info level");
49		return NULL;
50	}
51
52	if (level != 1) {
53		PyErr_SetString(spoolss_error, "unsupported info level");
54		return NULL;
55	}
56
57	switch (level) {
58	case 1: {
59		PyObject *obj = PyDict_GetItemString(info, "name");
60		char *form_name = PyString_AsString(obj);
61
62		init_unistr2(&form.name, form_name, UNI_STR_TERMINATE);
63		break;
64	}
65	default:
66		PyErr_SetString(spoolss_error, "unsupported info level");
67		return NULL;
68	}
69
70	werror = cli_spoolss_addform(hnd->cli, hnd->mem_ctx, &hnd->pol,
71				     level, &form);
72
73
74	if (!W_ERROR_IS_OK(werror)) {
75		PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
76		return NULL;
77	}
78
79	Py_INCREF(Py_None);
80	return Py_None;
81}
82
83/* Get form properties */
84
85PyObject *spoolss_hnd_getform(PyObject *self, PyObject *args, PyObject *kw)
86{
87	spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
88	WERROR werror;
89	PyObject *result;
90	char *form_name;
91	int level = 1;
92	static char *kwlist[] = {"form_name", "level", NULL};
93	uint32 needed;
94	FORM_1 form;
95
96	/* Parse parameters */
97
98	if (!PyArg_ParseTupleAndKeywords(
99		    args, kw, "s|i", kwlist, &form_name, &level))
100		return NULL;
101
102	/* Call rpc function */
103
104	werror = cli_spoolss_getform(hnd->cli, hnd->mem_ctx, 0, &needed,
105				     &hnd->pol, form_name, level, &form);
106
107	if (W_ERROR_V(werror) == ERRinsufficientbuffer)
108		werror = cli_spoolss_getform(
109			hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
110			form_name, 1, &form);
111
112	if (!W_ERROR_IS_OK(werror)) {
113		PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
114		return NULL;
115	}
116
117	result = Py_None;
118
119	switch(level) {
120	case 1:
121		py_from_FORM_1(&result, &form);
122		break;
123	}
124
125	Py_INCREF(result);
126	return result;
127}
128
129/* Set form properties */
130
131PyObject *spoolss_hnd_setform(PyObject *self, PyObject *args, PyObject *kw)
132{
133	spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
134	WERROR werror;
135	PyObject *info, *form_name;
136	int level;
137	static char *kwlist[] = { "form", NULL};
138	FORM form;
139
140	/* Parse parameters */
141
142	if (!PyArg_ParseTupleAndKeywords(
143		    args, kw, "O!", kwlist, &PyDict_Type, &info))
144		return NULL;
145
146	if (!get_level_value(info, &level)) {
147		PyErr_SetString(spoolss_error, "invalid info level");
148		return NULL;
149	}
150
151	if (level != 1) {
152		PyErr_SetString(spoolss_error, "unsupported info level");
153		return NULL;
154	}
155
156	/* Call rpc function */
157
158	if (!py_to_FORM(&form, info)) {
159		PyErr_SetString(spoolss_error, "invalid form");
160		return NULL;
161	}
162
163	form_name = PyDict_GetItemString(info, "name");
164
165	werror = cli_spoolss_setform(
166		hnd->cli, hnd->mem_ctx, &hnd->pol, level,
167		PyString_AsString(form_name), &form);
168
169	if (!W_ERROR_IS_OK(werror)) {
170		PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
171		return NULL;
172	}
173
174	Py_INCREF(Py_None);
175	return Py_None;
176}
177
178/* Delete a form */
179
180PyObject *spoolss_hnd_deleteform(PyObject *self, PyObject *args, PyObject *kw)
181{
182	spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
183	WERROR werror;
184	static char *kwlist[] = {"form_name", NULL};
185	char *form_name;
186
187	/* Parse parameters */
188
189	if (!PyArg_ParseTupleAndKeywords(
190		    args, kw, "s", kwlist, &form_name))
191		return NULL;
192
193	/* Call rpc function */
194
195	werror = cli_spoolss_deleteform(
196		hnd->cli, hnd->mem_ctx, &hnd->pol, form_name);
197
198	if (!W_ERROR_IS_OK(werror)) {
199		PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
200		return NULL;
201	}
202
203	Py_INCREF(Py_None);
204	return Py_None;
205}
206
207/* Enumerate forms */
208
209PyObject *spoolss_hnd_enumforms(PyObject *self, PyObject *args, PyObject *kw)
210{
211	PyObject *result;
212	spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
213	WERROR werror;
214	uint32 level = 1, num_forms, needed, i;
215	static char *kwlist[] = {"level", NULL};
216	FORM_1 *forms;
217
218	/* Parse parameters */
219
220	if (!PyArg_ParseTupleAndKeywords(
221		    args, kw, "|i", kwlist, &level))
222		return NULL;
223
224	/* Call rpc function */
225
226	werror = cli_spoolss_enumforms(
227		hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, level,
228		&num_forms, &forms);
229
230	if (W_ERROR_V(werror) == ERRinsufficientbuffer)
231		werror = cli_spoolss_enumforms(
232			hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol, level,
233			&num_forms, &forms);
234
235	if (!W_ERROR_IS_OK(werror)) {
236		PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
237		return NULL;
238	}
239
240	switch(level) {
241	case 1:
242		result = PyDict_New();
243
244		for (i = 0; i < num_forms; i++) {
245			PyObject *value;
246			fstring name;
247
248			rpcstr_pull(name, forms[i].name.buffer,
249				    sizeof(fstring), -1, STR_TERMINATE);
250
251			py_from_FORM_1(&value, &forms[i]);
252
253			PyDict_SetItemString(
254				value, "level", PyInt_FromLong(1));
255
256			PyDict_SetItemString(result, name, value);
257		}
258
259		break;
260	default:
261		PyErr_SetString(spoolss_error, "unknown info level");
262		return NULL;
263	}
264
265	return result;
266}
267