~lefteris-nikoltsios/+junk/samba-lp1016895

« back to all changes in this revision

Viewing changes to source4/param/pyparam.c

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2011-12-21 13:18:04 UTC
  • mfrom: (0.39.21 sid)
  • Revision ID: package-import@ubuntu.com-20111221131804-xtlr39wx6njehxxr
Tags: 2:3.6.1-3ubuntu1
* Merge from Debian testing.  Remaining changes:
  + debian/patches/VERSION.patch:
    - set SAMBA_VERSION_SUFFIX to Ubuntu.
  + debian/patches/error-trans.fix-276472:
    - Add the translation of Unix Error code -ENOTSUP to NT Error Code
    - NT_STATUS_NOT_SUPPORTED to prevent the Permission denied error.
  + debian/smb.conf:
    - add "(Samba, Ubuntu)" to server string.
    - comment out the default [homes] share, and add a comment about
      "valid users = %S" to show users how to restrict access to
      \\server\username to only username.
    - Set 'usershare allow guests', so that usershare admins are 
      allowed to create public shares in addition to authenticated
      ones.
    - add map to guest = Bad user, maps bad username to guest access.
  + debian/samba-common.config:
    - Do not change priority to high if dhclient3 is installed.
    - Use priority medium instead of high for the workgroup question.
  + debian/control:
    - Don't build against or suggest ctdb.
    - Add dependency on samba-common-bin to samba.
  + Add ufw integration:
    - Created debian/samba.ufw.profile
    - debian/rules, debian/samba.dirs, debian/samba.files: install
      profile
    - debian/control: have samba suggest ufw
  + Add apport hook:
    - Created debian/source_samba.py.
    - debian/rules, debian/samba.dirs, debian/samba-common-bin.files: install
  + Switch to upstart:
    - Add debian/samba.{nmbd,smbd}.upstart.
  + debian/samba.logrotate, debian/samba-common.dhcp, debian/samba.if-up:
    - Make them upstart compatible
  + debian/samba.postinst: 
    - Avoid scary pdbedit warnings on first import.
  + debian/samba-common.postinst: Add more informative error message for
    the case where smb.conf was manually deleted
  + debian/patches/fix-debuglevel-name-conflict.patch: don't use 'debug_level'
    as a global variable name in an NSS module 
  + Dropped:
    - debian/patches/error-trans.fix-276472
    - debian/patches/fix-debuglevel-name-conflict.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
18
*/
19
19
 
20
 
#include <stdint.h>
21
 
#include <stdbool.h>
22
 
 
 
20
#include <Python.h>
23
21
#include "includes.h"
24
22
#include "param/param.h"
25
23
#include "param/loadparm.h"
26
 
#include <Python.h>
27
 
#include "pytalloc.h"
 
24
#include "lib/talloc/pytalloc.h"
 
25
#include "dynconfig/dynconfig.h"
28
26
 
29
27
/* There's no Py_ssize_t in 2.4, apparently */
30
28
#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
32
30
typedef inquiry lenfunc;
33
31
#endif
34
32
 
35
 
#ifndef Py_RETURN_NONE
36
 
#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
37
 
#endif
38
 
 
39
33
#define PyLoadparmContext_AsLoadparmContext(obj) py_talloc_get_type(obj, struct loadparm_context)
 
34
#define PyLoadparmService_AsLoadparmService(obj) py_talloc_get_type(obj, struct loadparm_service)
40
35
 
41
 
PyAPI_DATA(PyTypeObject) PyLoadparmContext;
42
 
PyAPI_DATA(PyTypeObject) PyLoadparmService;
 
36
extern PyTypeObject PyLoadparmContext;
 
37
extern PyTypeObject PyLoadparmService;
43
38
 
44
39
PyObject *PyLoadparmService_FromService(struct loadparm_service *service)
45
40
{
48
43
 
49
44
static PyObject *py_lp_ctx_get_helper(struct loadparm_context *lp_ctx, const char *service_name, const char *param_name)
50
45
{
51
 
    struct parm_struct *parm = NULL;
52
 
    void *parm_ptr = NULL;
53
 
    int i;
54
 
 
55
 
    if (service_name != NULL) {
56
 
        struct loadparm_service *service;
57
 
        /* its a share parameter */
58
 
        service = lp_service(lp_ctx, service_name);
59
 
        if (service == NULL) {
60
 
            return NULL;
61
 
        }
62
 
        if (strchr(param_name, ':')) {
63
 
            /* its a parametric option on a share */
64
 
            const char *type = talloc_strndup(lp_ctx, 
65
 
                              param_name, 
66
 
                              strcspn(param_name, ":"));
67
 
            const char *option = strchr(param_name, ':') + 1;
68
 
            const char *value;
69
 
            if (type == NULL || option == NULL) {
70
 
                return NULL;
71
 
            }
72
 
            value = lp_get_parametric(lp_ctx, service, type, option);
73
 
            if (value == NULL) {
74
 
                return NULL;
75
 
            }
76
 
            return PyString_FromString(value);
77
 
        }
78
 
 
79
 
        parm = lp_parm_struct(param_name);
80
 
        if (parm == NULL || parm->pclass == P_GLOBAL) {
81
 
            return NULL;
82
 
        }
83
 
        parm_ptr = lp_parm_ptr(lp_ctx, service, parm);
 
46
        struct parm_struct *parm = NULL;
 
47
        void *parm_ptr = NULL;
 
48
        int i;
 
49
 
 
50
        if (service_name != NULL && strwicmp(service_name, GLOBAL_NAME) && 
 
51
                strwicmp(service_name, GLOBAL_NAME2)) {
 
52
                struct loadparm_service *service;
 
53
                /* its a share parameter */
 
54
                service = lpcfg_service(lp_ctx, service_name);
 
55
                if (service == NULL) {
 
56
                        return NULL;
 
57
                }
 
58
                if (strchr(param_name, ':')) {
 
59
                        /* its a parametric option on a share */
 
60
                        const char *type = talloc_strndup(lp_ctx, param_name,
 
61
                                                                                          strcspn(param_name, ":"));
 
62
                        const char *option = strchr(param_name, ':') + 1;
 
63
                        const char *value;
 
64
                        if (type == NULL || option == NULL) {
 
65
                        return NULL;
 
66
                        }
 
67
                        value = lpcfg_get_parametric(lp_ctx, service, type, option);
 
68
                        if (value == NULL) {
 
69
                        return NULL;
 
70
                        }
 
71
                        return PyString_FromString(value);
 
72
                }
 
73
 
 
74
                parm = lpcfg_parm_struct(param_name);
 
75
                if (parm == NULL || parm->pclass == P_GLOBAL) {
 
76
                        return NULL;
 
77
                }
 
78
                parm_ptr = lpcfg_parm_ptr(lp_ctx, service, parm);
84
79
    } else if (strchr(param_name, ':')) {
85
 
        /* its a global parametric option */
86
 
        const char *type = talloc_strndup(lp_ctx, 
87
 
                          param_name, strcspn(param_name, ":"));
88
 
        const char *option = strchr(param_name, ':') + 1;
89
 
        const char *value;
90
 
        if (type == NULL || option == NULL) {
91
 
            return NULL;
92
 
        }
93
 
        value = lp_get_parametric(lp_ctx, NULL, type, option);
94
 
        if (value == NULL)
95
 
            return NULL;
96
 
        return PyString_FromString(value);
97
 
    } else {
98
 
        /* its a global parameter */
99
 
        parm = lp_parm_struct(param_name);
100
 
        if (parm == NULL) {
101
 
            return NULL;
102
 
        }
103
 
        parm_ptr = lp_parm_ptr(lp_ctx, NULL, parm);
104
 
    }
 
80
                /* its a global parametric option */
 
81
                const char *type = talloc_strndup(lp_ctx,
 
82
                                  param_name, strcspn(param_name, ":"));
 
83
                const char *option = strchr(param_name, ':') + 1;
 
84
                const char *value;
 
85
                if (type == NULL || option == NULL) {
 
86
                        return NULL;
 
87
                }
 
88
                value = lpcfg_get_parametric(lp_ctx, NULL, type, option);
 
89
                if (value == NULL)
 
90
                        return NULL;
 
91
                return PyString_FromString(value);
 
92
        } else {
 
93
                /* its a global parameter */
 
94
                parm = lpcfg_parm_struct(param_name);
 
95
                if (parm == NULL) {
 
96
                        return NULL;
 
97
                }
 
98
                parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, parm);
 
99
        }
105
100
 
106
 
    if (parm == NULL || parm_ptr == NULL) {
107
 
        return NULL;
 
101
        if (parm == NULL || parm_ptr == NULL) {
 
102
                return NULL;
108
103
    }
109
104
 
110
105
    /* construct and return the right type of python object */
125
120
            }
126
121
        }
127
122
        return NULL;
 
123
    case P_CMDLIST:
128
124
    case P_LIST: 
129
125
        {
130
126
            int j;
155
151
        if (!PyArg_ParseTuple(args, "s", &filename))
156
152
                return NULL;
157
153
 
158
 
        ret = lp_load(PyLoadparmContext_AsLoadparmContext(self), filename);
 
154
        ret = lpcfg_load(PyLoadparmContext_AsLoadparmContext(self), filename);
159
155
 
160
156
        if (!ret) {
161
157
                PyErr_Format(PyExc_RuntimeError, "Unable to load file %s", filename);
167
163
static PyObject *py_lp_ctx_load_default(py_talloc_Object *self)
168
164
{
169
165
        bool ret;
170
 
        ret = lp_load_default(PyLoadparmContext_AsLoadparmContext(self));
 
166
        ret = lpcfg_load_default(PyLoadparmContext_AsLoadparmContext(self));
171
167
 
172
168
        if (!ret) {
173
169
                PyErr_SetString(PyExc_RuntimeError, "Unable to load default file");
181
177
        char *param_name;
182
178
        char *section_name = NULL;
183
179
        PyObject *ret;
184
 
        if (!PyArg_ParseTuple(args, "s|s", &param_name, &section_name))
 
180
        if (!PyArg_ParseTuple(args, "s|z", &param_name, &section_name))
185
181
                return NULL;
186
182
 
187
183
        ret = py_lp_ctx_get_helper(PyLoadparmContext_AsLoadparmContext(self), section_name, param_name);
196
192
        if (!PyArg_ParseTuple(args, "s", &name))
197
193
                return NULL;
198
194
 
199
 
        return PyBool_FromLong(lp_is_myname(PyLoadparmContext_AsLoadparmContext(self), name));
 
195
        return PyBool_FromLong(lpcfg_is_myname(PyLoadparmContext_AsLoadparmContext(self), name));
200
196
}
201
197
 
202
198
static PyObject *py_lp_ctx_is_mydomain(py_talloc_Object *self, PyObject *args)
205
201
        if (!PyArg_ParseTuple(args, "s", &name))
206
202
                return NULL;
207
203
 
208
 
        return PyBool_FromLong(lp_is_mydomain(PyLoadparmContext_AsLoadparmContext(self), name));
 
204
        return PyBool_FromLong(lpcfg_is_mydomain(PyLoadparmContext_AsLoadparmContext(self), name));
209
205
}
210
206
 
211
207
static PyObject *py_lp_ctx_set(py_talloc_Object *self, PyObject *args)
215
211
        if (!PyArg_ParseTuple(args, "ss", &name, &value))
216
212
                return NULL;
217
213
 
218
 
        ret = lp_set_cmdline(PyLoadparmContext_AsLoadparmContext(self), name, value);
 
214
        ret = lpcfg_set_cmdline(PyLoadparmContext_AsLoadparmContext(self), name, value);
219
215
        if (!ret) {
220
216
                PyErr_SetString(PyExc_RuntimeError, "Unable to set parameter");
221
217
                return NULL;
243
239
        struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
244
240
        PyObject *ret;
245
241
        int i;
246
 
        ret = PyList_New(lp_numservices(lp_ctx));
247
 
        for (i = 0; i < lp_numservices(lp_ctx); i++) {
248
 
                struct loadparm_service *service = lp_servicebynum(lp_ctx, i);
 
242
        ret = PyList_New(lpcfg_numservices(lp_ctx));
 
243
        for (i = 0; i < lpcfg_numservices(lp_ctx); i++) {
 
244
                struct loadparm_service *service = lpcfg_servicebynum(lp_ctx, i);
249
245
                if (service != NULL) {
250
 
                        PyList_SetItem(ret, i, PyString_FromString(lp_servicename(service)));
 
246
                        PyList_SetItem(ret, i, PyString_FromString(lpcfg_servicename(service)));
251
247
                }
252
248
        }
253
249
        return ret;
254
250
}
255
251
 
 
252
static PyObject *py_lp_dump(PyObject *self, PyObject *args)
 
253
{
 
254
        PyObject *py_stream;
 
255
        bool show_defaults = false;
 
256
        FILE *f;
 
257
        struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
 
258
 
 
259
        if (!PyArg_ParseTuple(args, "O|b", &py_stream, &show_defaults))
 
260
                return NULL;
 
261
 
 
262
        f = PyFile_AsFile(py_stream);
 
263
        if (f == NULL) {
 
264
                PyErr_SetString(PyExc_TypeError, "Not a file stream");
 
265
                return NULL;
 
266
        }
 
267
 
 
268
        lpcfg_dump(lp_ctx, f, show_defaults, lpcfg_numservices(lp_ctx));
 
269
 
 
270
        Py_RETURN_NONE;
 
271
}
 
272
 
 
273
 
256
274
static PyMethodDef py_lp_ctx_methods[] = {
257
275
        { "load", (PyCFunction)py_lp_ctx_load, METH_VARARGS, 
258
276
                "S.load(filename) -> None\n"
276
294
                "S.private_path(name) -> path\n" },
277
295
        { "services", (PyCFunction)py_lp_ctx_services, METH_NOARGS,
278
296
                "S.services() -> list" },
 
297
        { "dump", (PyCFunction)py_lp_dump, METH_VARARGS, 
 
298
                "S.dump(stream, show_defaults=False)" },
279
299
        { NULL }
280
300
};
281
301
 
282
302
static PyObject *py_lp_ctx_default_service(py_talloc_Object *self, void *closure)
283
303
{
284
 
        return PyLoadparmService_FromService(lp_default_service(PyLoadparmContext_AsLoadparmContext(self)));
 
304
        return PyLoadparmService_FromService(lpcfg_default_service(PyLoadparmContext_AsLoadparmContext(self)));
285
305
}
286
306
 
287
307
static PyObject *py_lp_ctx_config_file(py_talloc_Object *self, void *closure)
288
308
{
289
 
        const char *configfile = lp_configfile(PyLoadparmContext_AsLoadparmContext(self));
 
309
        const char *configfile = lpcfg_configfile(PyLoadparmContext_AsLoadparmContext(self));
290
310
        if (configfile == NULL)
291
311
                Py_RETURN_NONE;
292
312
        else
312
332
                PyErr_NoMemory();
313
333
                return NULL;
314
334
        }
315
 
        ret->ptr = loadparm_init(ret->talloc_ctx);
 
335
        ret->ptr = loadparm_init_global(false);
316
336
        return (PyObject *)ret;
317
337
}
318
338
 
319
339
static Py_ssize_t py_lp_ctx_len(py_talloc_Object *self)
320
340
{
321
 
        return lp_numservices(PyLoadparmContext_AsLoadparmContext(self));
 
341
        return lpcfg_numservices(PyLoadparmContext_AsLoadparmContext(self));
322
342
}
323
343
 
324
344
static PyObject *py_lp_ctx_getitem(py_talloc_Object *self, PyObject *name)
328
348
                PyErr_SetString(PyExc_TypeError, "Only string subscripts are supported");
329
349
                return NULL;
330
350
        }
331
 
        service = lp_service(PyLoadparmContext_AsLoadparmContext(self), PyString_AsString(name));
 
351
        service = lpcfg_service(PyLoadparmContext_AsLoadparmContext(self), PyString_AsString(name));
332
352
        if (service == NULL) {
333
353
                PyErr_SetString(PyExc_KeyError, "No such section");
334
354
                return NULL;
344
364
PyTypeObject PyLoadparmContext = {
345
365
        .tp_name = "LoadParm",
346
366
        .tp_basicsize = sizeof(py_talloc_Object),
347
 
        .tp_dealloc = py_talloc_dealloc,
348
367
        .tp_getset = py_lp_ctx_getset,
349
368
        .tp_methods = py_lp_ctx_methods,
350
369
        .tp_new = py_lp_ctx_new,
352
371
        .tp_flags = Py_TPFLAGS_DEFAULT,
353
372
};
354
373
 
 
374
static PyObject *py_lp_service_dump(PyObject *self, PyObject *args)
 
375
{
 
376
        PyObject *py_stream;
 
377
        bool show_defaults = false;
 
378
        FILE *f;
 
379
        struct loadparm_service *service = PyLoadparmService_AsLoadparmService(self);
 
380
        struct loadparm_service *default_service;
 
381
        PyObject *py_default_service;
 
382
 
 
383
        if (!PyArg_ParseTuple(args, "OO|b", &py_stream, &py_default_service,
 
384
                                                  &show_defaults))
 
385
                return NULL;
 
386
 
 
387
        f = PyFile_AsFile(py_stream);
 
388
        if (f == NULL) {
 
389
                PyErr_SetString(PyExc_TypeError, "Not a file stream");
 
390
                return NULL;
 
391
        }
 
392
 
 
393
        if (!PyObject_TypeCheck(py_default_service, &PyLoadparmService)) {
 
394
                PyErr_SetNone(PyExc_TypeError);
 
395
                return NULL;
 
396
        }
 
397
 
 
398
        default_service = PyLoadparmService_AsLoadparmService(py_default_service);
 
399
 
 
400
        lpcfg_dump_one(f, show_defaults, service, default_service);
 
401
 
 
402
        Py_RETURN_NONE;
 
403
}
 
404
 
 
405
static PyMethodDef py_lp_service_methods[] = {
 
406
        { "dump", (PyCFunction)py_lp_service_dump, METH_VARARGS, 
 
407
                "S.dump(f, default_service, show_defaults=False)" },
 
408
        { NULL }
 
409
};
 
410
 
355
411
PyTypeObject PyLoadparmService = {
356
412
        .tp_name = "LoadparmService",
357
 
        .tp_dealloc = py_talloc_dealloc,
358
413
        .tp_basicsize = sizeof(py_talloc_Object),
 
414
        .tp_methods = py_lp_service_methods,
359
415
        .tp_flags = Py_TPFLAGS_DEFAULT,
360
416
};
361
417
 
364
420
    return PyString_FromString(lp_default_path());
365
421
}
366
422
 
 
423
static PyObject *py_setup_dir(PyObject *self)
 
424
{
 
425
    return PyString_FromString(dyn_SETUPDIR);
 
426
}
 
427
 
367
428
static PyMethodDef pyparam_methods[] = {
368
429
    { "default_path", (PyCFunction)py_default_path, METH_NOARGS, 
369
430
        "Returns the default smb.conf path." },
 
431
    { "setup_dir", (PyCFunction)py_setup_dir, METH_NOARGS,
 
432
        "Returns the compiled in location of provision tempates." },
370
433
    { NULL }
371
434
};
372
435
 
373
436
void initparam(void)
374
437
{
375
438
        PyObject *m;
 
439
        PyTypeObject *talloc_type = PyTalloc_GetObjectType();
 
440
        if (talloc_type == NULL)
 
441
                return;
 
442
 
 
443
        PyLoadparmContext.tp_base = talloc_type;
 
444
        PyLoadparmService.tp_base = talloc_type;
376
445
 
377
446
        if (PyType_Ready(&PyLoadparmContext) < 0)
378
447
                return;
379
448
 
 
449
        if (PyType_Ready(&PyLoadparmService) < 0)
 
450
                return;
 
451
 
380
452
        m = Py_InitModule3("param", pyparam_methods, "Parsing and writing Samba configuration files.");
381
453
        if (m == NULL)
382
454
                return;