Add context pointer to secrets functions.
[jelmer/samba4-debian.git] / source / param / param.i
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 %module(package="samba.param") param
20
21 %{
22 #include <stdint.h>
23 #include <stdbool.h>
24
25 #include "includes.h"
26 #include "param/param.h"
27 #include "param/loadparm.h"
28
29 typedef struct param_context param;
30 typedef struct loadparm_context loadparm_context;
31 typedef struct loadparm_service loadparm_service;
32 typedef struct param_section param_section;
33 typedef struct param_opt param_opt;
34 %}
35
36 %import "stdint.i"
37 %import "carrays.i"
38 %import "typemaps.i"
39 %import "../lib/talloc/talloc.i"
40
41 %typemap(default,noblock=1) struct loadparm_context * {
42     $1 = loadparm_init(NULL);
43 }
44
45 %rename(LoadParm) loadparm_context;
46
47 %talloctype(loadparm_context);
48
49 typedef struct loadparm_context {
50     %extend {
51         loadparm_context(TALLOC_CTX *mem_ctx) { return loadparm_init(mem_ctx); }
52         struct loadparm_service *default_service() { return lp_default_service($self); }
53         bool load(const char *filename) { return lp_load($self, filename); }
54         bool load_default() { return lp_load_default($self); }
55 #ifdef SWIGPYTHON
56         int __len__() { return lp_numservices($self); }
57         struct loadparm_service *__getitem__(const char *name) { return lp_service($self, name); }
58 #endif
59         const char *configfile() { return lp_configfile($self); }
60         bool is_mydomain(const char *domain) { return lp_is_mydomain($self, domain); }
61         bool is_myname(const char *name) { return lp_is_myname($self, name); }
62         int use(struct param_context *param_ctx) { return param_use($self, param_ctx); }
63         bool set(const char *parm_name, const char *parm_value) {
64             if (parm_value == NULL)
65                 return false;
66             return lp_set_cmdline($self, parm_name, parm_value);
67         }
68
69         PyObject *get(const char *param_name, const char *service_name)
70         {
71             struct parm_struct *parm = NULL;
72             void *parm_ptr = NULL;
73             int i;
74
75             if (service_name != NULL) {
76                 struct loadparm_service *service;
77                 /* its a share parameter */
78                 service = lp_service($self, service_name);
79                 if (service == NULL) {
80                     return Py_None;
81                 }
82                 if (strchr(param_name, ':')) {
83                     /* its a parametric option on a share */
84                     const char *type = talloc_strndup($self, 
85                                       param_name, 
86                                       strcspn(param_name, ":"));
87                     const char *option = strchr(param_name, ':') + 1;
88                     const char *value;
89                     if (type == NULL || option == NULL) {
90                         return Py_None;
91                     }
92                     value = lp_get_parametric($self, service, type, option);
93                     if (value == NULL) {
94                         return Py_None;
95                     }
96                     return PyString_FromString(value);
97                 }
98
99                 parm = lp_parm_struct(param_name);
100                 if (parm == NULL || parm->class == P_GLOBAL) {
101                     return Py_None;
102                 }
103                 parm_ptr = lp_parm_ptr($self, service, parm);
104             } else if (strchr(param_name, ':')) {
105                 /* its a global parametric option */
106                 const char *type = talloc_strndup($self, 
107                                   param_name, strcspn(param_name, ":"));
108                 const char *option = strchr(param_name, ':') + 1;
109                 const char *value;
110                 if (type == NULL || option == NULL) {
111                     return Py_None;
112                 }
113                 value = lp_get_parametric($self, NULL, type, option);
114                 if (value == NULL)
115                     return Py_None;
116                 return PyString_FromString(value);
117             } else {
118                 /* its a global parameter */
119                 parm = lp_parm_struct(param_name);
120                 if (parm == NULL) {
121                     return Py_None;
122                 }
123                 parm_ptr = lp_parm_ptr($self, NULL, parm);
124             }
125
126             if (parm == NULL || parm_ptr == NULL) {
127                 return Py_None;
128             }
129
130             /* construct and return the right type of python object */
131             switch (parm->type) {
132             case P_STRING:
133             case P_USTRING:
134                 return PyString_FromString(*(char **)parm_ptr);
135             case P_BOOL:
136                 return PyBool_FromLong(*(bool *)parm_ptr);
137             case P_INTEGER:
138             case P_OCTAL:
139             case P_BYTES:
140                 return PyLong_FromLong(*(int *)parm_ptr);
141             case P_ENUM:
142                 for (i=0; parm->enum_list[i].name; i++) {
143                     if (*(int *)parm_ptr == parm->enum_list[i].value) {
144                         return PyString_FromString(parm->enum_list[i].name);
145                     }
146                 }
147                 return Py_None;
148             case P_LIST: 
149                 {
150                     int j;
151                     const char **strlist = *(const char ***)parm_ptr;
152                     PyObject *pylist = PyList_New(str_list_length(strlist));
153                     for (j = 0; strlist[j]; j++) 
154                         PyList_SetItem(pylist, j, 
155                                        PyString_FromString(strlist[j]));
156                     return pylist;
157                 }
158
159                 break;
160             }
161             return Py_None;
162         }
163     }
164 } loadparm_context;
165
166 %nodefaultctor loadparm_service;
167 %nodefaultdtor loadparm_service;
168
169 typedef struct loadparm_service {
170     %extend { 
171         const char *volume_label(struct loadparm_service *sDefault) { return volume_label($self, sDefault); }
172         const char *printername(struct loadparm_service *sDefault) { return lp_printername($self, sDefault); }
173         int maxprintjobs(struct loadparm_service *sDefault) { return lp_maxprintjobs($self, sDefault); } 
174     }
175 } loadparm_service;
176
177 %rename(ParamFile) param_context;
178
179 %talloctype(param_context);
180 typedef struct param_context {
181     %extend { 
182         param(TALLOC_CTX *mem_ctx) { return param_init(mem_ctx); }
183         struct param_section *get_section(const char *name);
184         struct param_section *add_section(const char *name);
185         struct param_opt *get(const char *name, const char *section_name="global");
186         const char *get_string(const char *name, const char *section_name="global");
187         int set_string(const char *param, const char *value, const char *section="global");
188 #ifdef SWIGPYTHON
189         int set(const char *parameter, PyObject *ob, const char *section_name="global")
190         {
191             struct param_opt *opt = param_get_add($self, parameter, section_name);
192
193             talloc_free(opt->value);
194             opt->value = talloc_strdup(opt, PyString_AsString(PyObject_Str(ob)));
195
196             return 0;
197         }
198         
199 #endif
200
201         struct param_section *first_section() { return $self->sections; }
202         struct param_section *next_section(struct param_section *s) { return s->next; }
203
204         int read(const char *fn);
205         int write(const char *fn);
206     }
207     %pythoncode {
208         def __getitem__(self, name):
209             ret = self.get_section(name)
210             if ret is None:
211                 raise KeyError("No such section %s" % name)
212             return ret
213
214         class SectionIterator:
215             def __init__(self, param):
216                 self.param = param
217                 self.key = None
218
219             def __iter__(self):
220                 return self
221                 
222             def next(self):
223                 if self.key is None:
224                     self.key = self.param.first_section()
225                     if self.key is None:
226                         raise StopIteration
227                     return self.key
228                 else:
229                     self.key = self.param.next_section(self.key)
230                     if self.key is None:
231                         raise StopIteration
232                     return self.key
233
234         def __iter__(self):
235             return self.SectionIterator(self)
236     }
237 } param;
238
239 %talloctype(param_opt);
240
241 typedef struct param_opt {
242     %immutable key;
243     %immutable value;
244     const char *key, *value;
245     %extend {
246 #ifdef SWIGPYTHON
247         const char *__str__() { return $self->value; }
248 #endif
249     }
250 } param_opt;
251
252 %talloctype(param);
253 typedef struct param_section {
254     %immutable name;
255     const char *name;
256     %extend {
257         struct param_opt *get(const char *name);
258         struct param_opt *first_parameter() { return $self->parameters; }
259         struct param_opt *next_parameter(struct param_opt *s) { return s->next; }
260     }
261     %pythoncode {
262         def __getitem__(self, name):
263             ret = self.get(name)
264             if ret is None:
265                 raise KeyError("No such option %s" % name)
266             return ret
267
268         class ParamIterator:
269             def __init__(self, section):
270                 self.section = section
271                 self.key = None
272
273             def __iter__(self):
274                 return self
275                 
276             def next(self):
277                 if self.key is None:
278                     self.key = self.section.first_parameter()
279                     if self.key is None:
280                         raise StopIteration
281                     return self.key
282                 else:
283                     self.key = self.section.next_parameter(self.key)
284                     if self.key is None:
285                         raise StopIteration
286                     return self.key
287
288         def __iter__(self):
289             return self.ParamIterator(self)
290     }
291 } param_section;
292
293 %rename(default_config) global_loadparm;
294 struct loadparm_context *global_loadparm;
295
296 %{
297
298 struct loadparm_context *lp_from_py_object(PyObject *py_obj)
299 {
300     struct loadparm_context *lp_ctx;
301     if (PyString_Check(py_obj)) {
302         lp_ctx = loadparm_init(NULL);
303         if (!lp_load(lp_ctx, PyString_AsString(py_obj))) {
304             talloc_free(lp_ctx);
305             return NULL;
306         }
307         return lp_ctx;
308     }
309
310     if (SWIG_ConvertPtr(py_obj, (void *)&lp_ctx, SWIGTYPE_p_loadparm_context, 0 |  0 ) < 0)
311         return NULL;
312     return lp_ctx;
313 }
314
315 %}