File: ldapcontrol.c

package info (click to toggle)
python-ldap 3.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,756 kB
  • sloc: python: 9,558; ansic: 3,052; makefile: 139; sh: 79
file content (394 lines) | stat: -rw-r--r-- 9,332 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/* See https://www.python-ldap.org/ for details. */

#include "common.h"
#include "LDAPObject.h"
#include "ldapcontrol.h"
#include "berval.h"
#include "constants.h"

/* Prints to stdout the contents of an array of LDAPControl objects */

/* XXX: This is a debugging tool, and the printf generates some warnings
 * about pointer types. I left it here in case something breaks and we
 * need to inspect an LDAPControl structure.

static void
LDAPControl_DumpList( LDAPControl** lcs ) {
    LDAPControl** lcp;
    LDAPControl* lc;
        for ( lcp = lcs; *lcp; lcp++ ) {
        lc = *lcp;
        printf("OID: %s\nCriticality: %d\nBER length: %d\nBER value: %x\n",
            lc->ldctl_oid, lc->ldctl_iscritical, lc->ldctl_value.bv_len,
            lc->ldctl_value.bv_val);
    }
} */

/* Free a single LDAPControl object created by Tuple_to_LDAPControl */

static void
LDAPControl_DEL(LDAPControl *lc)
{
    if (lc == NULL)
        return;

    if (lc->ldctl_oid)
        PyMem_DEL(lc->ldctl_oid);
    PyMem_DEL(lc);
}

/* Free an array of LDAPControl objects created by LDAPControls_from_object */

void
LDAPControl_List_DEL(LDAPControl **lcs)
{
    LDAPControl **lcp;

    if (lcs == NULL)
        return;

    for (lcp = lcs; *lcp; lcp++)
        LDAPControl_DEL(*lcp);

    PyMem_DEL(lcs);
}

/* Takes a tuple of the form:
 * (OID: string, Criticality: int/boolean, Value: string/None)
 * and converts it into an LDAPControl structure.
 *
 * The Value string should represent an ASN.1 encoded structure.
 */

static LDAPControl *
Tuple_to_LDAPControl(PyObject *tup)
{
    char *oid;
    char iscritical;
    struct berval berbytes;
    PyObject *bytes;
    LDAPControl *lc = NULL;
    Py_ssize_t len;

    if (!PyTuple_Check(tup)) {
        LDAPerror_TypeError("Tuple_to_LDAPControl(): expected a tuple", tup);
        return NULL;
    }

    if (!PyArg_ParseTuple
        (tup, "sbO:Tuple_to_LDAPControl", &oid, &iscritical, &bytes))
        return NULL;

    lc = PyMem_NEW(LDAPControl, 1);

    if (lc == NULL) {
        PyErr_NoMemory();
        return NULL;
    }

    lc->ldctl_iscritical = iscritical;

    len = strlen(oid);
    lc->ldctl_oid = PyMem_NEW(char, len + 1);

    if (lc->ldctl_oid == NULL) {
        PyErr_NoMemory();
        LDAPControl_DEL(lc);
        return NULL;
    }
    memcpy(lc->ldctl_oid, oid, len + 1);

    /* The berval can either be None or a String */
    if (PyNone_Check(bytes)) {
        berbytes.bv_len = 0;
        berbytes.bv_val = NULL;
    }
    else if (PyBytes_Check(bytes)) {
        berbytes.bv_len = PyBytes_Size(bytes);
        berbytes.bv_val = PyBytes_AsString(bytes);
    }
    else {
        LDAPerror_TypeError("Tuple_to_LDAPControl(): expected bytes", bytes);
        LDAPControl_DEL(lc);
        return NULL;
    }

    lc->ldctl_value = berbytes;

    return lc;
}

/* Convert a list of tuples (of a format acceptable to the Tuple_to_LDAPControl
 * function) into an array of LDAPControl objects. */

int
LDAPControls_from_object(PyObject *list, LDAPControl ***controls_ret)
{
    Py_ssize_t len, i;
    LDAPControl **ldcs;
    LDAPControl *ldc;
    PyObject *item;

    if (!PySequence_Check(list)) {
        LDAPerror_TypeError("LDAPControls_from_object(): expected a list",
                            list);
        return 0;
    }

    len = PySequence_Length(list);
    ldcs = PyMem_NEW(LDAPControl *, len + 1);

    if (ldcs == NULL) {
        PyErr_NoMemory();
        return 0;
    }

    for (i = 0; i < len; i++) {
        item = PySequence_GetItem(list, i);
        if (item == NULL) {
            PyMem_DEL(ldcs);
            return 0;
        }

        ldc = Tuple_to_LDAPControl(item);
        if (ldc == NULL) {
            Py_DECREF(item);
            PyMem_DEL(ldcs);
            return 0;
        }

        ldcs[i] = ldc;
        Py_DECREF(item);
    }

    ldcs[len] = NULL;
    *controls_ret = ldcs;
    return 1;
}

PyObject *
LDAPControls_to_List(LDAPControl **ldcs)
{
    PyObject *res = 0, *pyctrl;
    LDAPControl **tmp = ldcs;
    Py_ssize_t num_ctrls = 0, i;

    if (tmp)
        while (*tmp++)
            num_ctrls++;

    if ((res = PyList_New(num_ctrls)) == NULL) {
        return NULL;
    }

    for (i = 0; i < num_ctrls; i++) {
        pyctrl = Py_BuildValue("sbO&",
                               ldcs[i]->ldctl_oid,
                               ldcs[i]->ldctl_iscritical,
                               LDAPberval_to_object, &ldcs[i]->ldctl_value);
        if (pyctrl == NULL) {
            Py_DECREF(res);
            return NULL;
        }
        PyList_SET_ITEM(res, i, pyctrl);
    }
    return res;
}

/* --------------- en-/decoders ------------- */

/* Matched Values, aka, Values Return Filter */
static PyObject *
encode_rfc3876(PyObject *self, PyObject *args)
{
    PyObject *res = 0;
    int err;
    BerElement *vrber = 0;
    char *vrFilter;
    struct berval *ctrl_val;

    if (!PyArg_ParseTuple
        (args, "s:encode_valuesreturnfilter_control", &vrFilter)) {
        goto endlbl;
    }

    if (!(vrber = ber_alloc_t(LBER_USE_DER))) {
        LDAPerr(LDAP_NO_MEMORY);
        goto endlbl;
    }

    err = ldap_put_vrFilter(vrber, vrFilter);
    if (err == -1) {
        LDAPerr(LDAP_FILTER_ERROR);
        goto endlbl;
    }

    err = ber_flatten(vrber, &ctrl_val);
    if (err == -1) {
        LDAPerr(LDAP_NO_MEMORY);
        goto endlbl;
    }

    res = LDAPberval_to_object(ctrl_val);
    ber_bvfree(ctrl_val);

  endlbl:
    if (vrber)
        ber_free(vrber, 1);

    return res;
}

static PyObject *
encode_rfc2696(PyObject *self, PyObject *args)
{
    PyObject *res = 0;
    BerElement *ber = 0;
    struct berval cookie, *ctrl_val;
    Py_ssize_t cookie_len;
    int size = 0;               /* ber_int_t is int */
    ber_tag_t tag;

    if (!PyArg_ParseTuple(args, "is#:encode_page_control", &size,
                          &cookie.bv_val, &cookie_len)) {
        goto endlbl;
    }
    cookie.bv_len = (ber_len_t) cookie_len;

    if (!(ber = ber_alloc_t(LBER_USE_DER))) {
        LDAPerr(LDAP_NO_MEMORY);
        goto endlbl;
    }

    tag = ber_printf(ber, "{i", size);
    if (tag == LBER_ERROR) {
        LDAPerr(LDAP_ENCODING_ERROR);
        goto endlbl;
    }

    if (!cookie.bv_len)
        tag = ber_printf(ber, "o", "", 0);
    else
        tag = ber_printf(ber, "O", &cookie);
    if (tag == LBER_ERROR) {
        LDAPerr(LDAP_ENCODING_ERROR);
        goto endlbl;
    }

    tag = ber_printf(ber, /*{ */ "N}");
    if (tag == LBER_ERROR) {
        LDAPerr(LDAP_ENCODING_ERROR);
        goto endlbl;
    }

    if (-1 == ber_flatten(ber, &ctrl_val)) {
        LDAPerr(LDAP_NO_MEMORY);
        goto endlbl;
    }

    res = LDAPberval_to_object(ctrl_val);
    ber_bvfree(ctrl_val);

  endlbl:
    if (ber)
        ber_free(ber, 1);
    return res;
}

static PyObject *
decode_rfc2696(PyObject *self, PyObject *args)
{
    PyObject *res = 0;
    BerElement *ber = 0;
    struct berval ldctl_value;
    ber_tag_t tag;
    struct berval *cookiep;
    int count = 0;              /* ber_int_t is int */
    Py_ssize_t ldctl_value_len;

    if (!PyArg_ParseTuple(args, "s#:decode_page_control",
                          &ldctl_value.bv_val, &ldctl_value_len)) {
        goto endlbl;
    }
    ldctl_value.bv_len = (ber_len_t) ldctl_value_len;

    if (!(ber = ber_init(&ldctl_value))) {
        LDAPerr(LDAP_NO_MEMORY);
        goto endlbl;
    }

    tag = ber_scanf(ber, "{iO", &count, &cookiep);
    if (tag == LBER_ERROR) {
        LDAPerr(LDAP_DECODING_ERROR);
        goto endlbl;
    }

    res = Py_BuildValue("(iO&)", count, LDAPberval_to_object, cookiep);
    ber_bvfree(cookiep);

  endlbl:
    if (ber)
        ber_free(ber, 1);
    return res;
}

static PyObject *
encode_assertion_control(PyObject *self, PyObject *args)
{
    int err;
    PyObject *res = 0;
    char *assertion_filterstr;
    struct berval ctrl_val;
    LDAP *ld = NULL;
    PyThreadState *save;

    if (!PyArg_ParseTuple(args, "s:encode_assertion_control",
                          &assertion_filterstr)) {
        goto endlbl;
    }

    /* XXX: ldap_create() is a nasty and slow hack. It's creating a full blown
     * LDAP object just to encode assertion controls.
     */
    save = PyEval_SaveThread();
    err = ldap_create(&ld);
    PyEval_RestoreThread(save);
    if (err != LDAP_SUCCESS)
        return LDAPerror(ld);

    err = ldap_create_assertion_control_value(ld, assertion_filterstr,
                                              &ctrl_val);

    if (err != LDAP_SUCCESS) {
        LDAPerror(ld);
        save = PyEval_SaveThread();
        ldap_unbind_ext(ld, NULL, NULL);
        PyEval_RestoreThread(save);
        return NULL;
    }
    save = PyEval_SaveThread();
    ldap_unbind_ext(ld, NULL, NULL);
    PyEval_RestoreThread(save);
    res = LDAPberval_to_object(&ctrl_val);

    if (ctrl_val.bv_val != NULL) {
        ber_memfree(ctrl_val.bv_val);
    }
  endlbl:

    return res;
}

static PyMethodDef methods[] = {
    {"encode_page_control", encode_rfc2696, METH_VARARGS},
    {"decode_page_control", decode_rfc2696, METH_VARARGS},
    {"encode_valuesreturnfilter_control", encode_rfc3876, METH_VARARGS},
    {"encode_assertion_control", encode_assertion_control, METH_VARARGS},
    {NULL, NULL}
};

void
LDAPinit_control(PyObject *d)
{
    LDAPadd_methods(d, methods);
}