Debian Codegraph @

src/_bonsai/ldapentry.c

raw symbols SCIP
1 #include "utils.h"
2 #include "ldapentry.h"
3
4 /* Clear all object in the LDAPEntry. */
5 static int
6 ldapentry_clear(LDAPEntry *self) {
7 DEBUG("ldapentry_clear (self:%p)", self);
8 Py_CLEAR(self->conn);
9 Py_CLEAR(self->deleted);
10 Py_CLEAR(self->dn);
11 PyDict_Type.tp_clear((PyObject*)self);
12
13 return 0;
14 }
15
16 /* Deallocate the LDAPEntry. */
17 static void
18 ldapentry_dealloc(LDAPEntry *self) {
19 DEBUG("ldapentry_dealloc (self:%p)", self);
20 PyObject_GC_UnTrack(self);
21 ldapentry_clear(self);
22 PyDict_Type.tp_dealloc((PyObject*)self);
23 }
24
25 static int
26 ldapentry_traverse(LDAPEntry *self, visitproc visit, void *arg) {
27 Py_VISIT(self->dn);
28 Py_VISIT(self->deleted);
29 Py_VISIT(self->conn);
30 return 0;
31 }
32
33 /* Create a new LDAPEntry object. */
34 static PyObject *
35 ldapentry_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
36 LDAPEntry *self;
37
38 self = (LDAPEntry *)PyDict_Type.tp_new(type, args, kwds);
39 if (self != NULL) {
40 /* Set DN for an empty string. */
41 self->dn = PyUnicode_FromString("");
42 if (self->dn == NULL) {
43 Py_DECREF(self);
44 return NULL;
45 }
46 /* Set an empty list for deleted attributes. */
47 self->deleted = PyList_New(0);
48 if (self->deleted == NULL) {
49 Py_DECREF(self);
50 return NULL;
51 }
52 }
53 DEBUG("ldapentry_new [self:%p]", self);
54 return (PyObject *)self;
55 }
56
57 /* Initialising LDAPEntry. */
58 static int
59 ldapentry_init(LDAPEntry *self, PyObject *args, PyObject *kwds) {
60 PyObject *dnobj = NULL;
61 PyObject *conn = NULL;
62 PyObject *tmp;
63 static char *kwlist[] = {"dn", "conn", NULL};
64
65 DEBUG("ldapentry_init (self:%p)", self);
66 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, &dnobj, &conn)) {
67 return -1;
68 }
69
70 if (LDAPEntry_SetDN(self, dnobj) != 0) return -1;
71
72 if (conn != NULL && conn != Py_None && PyObject_IsInstance(conn, (PyObject *)&LDAPConnectionType) != 1) {
73 PyErr_SetString(PyExc_TypeError, "Connection must be an LDAPConnection type.");
74 return -1;
75 }
76
77 /* Just like in the Python doc example. */
78 if (conn && conn != Py_None) {
79 tmp = (PyObject *)self->conn;
80 Py_INCREF(conn);
81 self->conn = (LDAPConnection *)conn;
82 Py_XDECREF(tmp);
83 }
84
85 return 0;
86 }
87
88 /* Returns a NULL-delimitered LDAPMod list for adding new or modifing existing LDAP entries.
89 It uses only those LDAPValueList, whose status is 1 - add or delete, or 2 - replace, and
90 the deleted keys listed in LDAPEntry's deleted list.
91 */
92 LDAPModList *
93 LDAPEntry_CreateLDAPMods(LDAPEntry *self) {
94 int status = -1;
95 Py_ssize_t i;
96 char *strkey = NULL;
97 PyObject *keys = PyMapping_Keys((PyObject *)self);
98 PyObject *iter = NULL, *key = NULL;
99 LDAPModList *mods = NULL;
100 PyObject *value = NULL;
101 PyObject *added = NULL, *deleted = NULL;
102 PyObject *tmp = NULL;
103
104 if (keys == NULL) return NULL;
105 /* Create an LDAPModList for the LDAPEntry values and deleted attributes. */
106 mods = LDAPModList_New((PyObject *)self, Py_SIZE(self) * 2
107 + Py_SIZE(self->deleted));
108 if (mods == NULL) {
109 Py_DECREF(keys);
110 return NULL;
111 }
112
113 iter = PyObject_GetIter(keys);
114 Py_DECREF(keys);
115 if (iter == NULL) goto error;
116
117 DEBUG("LDAPEntry_CreateLDAPMods (self:%p)", self);
118 for (key = PyIter_Next(iter); key != NULL; key = PyIter_Next(iter)) {
119 strkey = lowercase(PyObject2char(key));
120 if (strkey == NULL) goto error;
121
122 /* Skip DN key. */
123 if (strcmp(strkey, "dn") == 0) {
124 free(strkey);
125 continue;
126 }
127 free(strkey);
128
129 /* Return value: Borrowed reference. */
130 value = LDAPEntry_GetItem(self, key);
131 if (value == NULL) goto error;
132
133 /* Get LDAPValueList's status. */
134 status = get_ldapvaluelist_status(value);
135 if (status == -1) goto error;
136
137 /* Get LDAPValueList's __added list. */
138 added = PyObject_GetAttrString(value, "added");
139 if (added == NULL) goto error;
140
141 /* Get LDAPValueList's __deleted list. */
142 deleted = PyObject_GetAttrString(value, "deleted");
143 if (deleted == NULL) goto error;
144
145 if (status == 1) {
146 /* LDAPMod for newly added attributes and values. */
147 if (Py_SIZE(added) > 0) {
148 if (LDAPModList_Add(mods, LDAP_MOD_ADD | LDAP_MOD_BVALUES,
149 key, added) != 0) {
150 goto error;
151 }
152 }
153 /* LDAPMod for deleted values. */
154 if (Py_SIZE(deleted) > 0) {
155 if (LDAPModList_Add(mods, LDAP_MOD_DELETE | LDAP_MOD_BVALUES,
156 key, deleted) != 0) {
157 goto error;
158 }
159 }
160 } else if (status == 2) {
161 /* LDAPMod for replaced attributes. */
162 if (LDAPModList_Add(mods, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES,
163 key, (PyObject *)value) != 0){
164 goto error;
165 }
166 }
167 /* Change attributes' status to "not changed" (0), and clear lists. */
168 if (set_ldapvaluelist_status(value, 0) != 0) goto error;
169
170 tmp = PyObject_CallMethod(added, "clear", NULL);
171 if (tmp == NULL) goto error;
172 Py_DECREF(tmp);
173
174 tmp = PyObject_CallMethod(deleted, "clear", NULL);
175 if (tmp == NULL) goto error;
176 Py_DECREF(tmp);
177
178 Py_DECREF(added);
179 Py_DECREF(deleted);
180 Py_DECREF(key);
181 }
182 Py_DECREF(iter);
183
184 /* LDAPMod for deleted attributes. */
185 for (i = 0; i < Py_SIZE(self->deleted); i++) {
186 if (LDAPModList_Add(mods, LDAP_MOD_DELETE | LDAP_MOD_BVALUES,
187 PyList_GET_ITEM(self->deleted, i), NULL) != 0) {
188 Py_DECREF(mods);
189 return NULL;
190 }
191 }
192
193 /* Delete the list. */
194 Py_DECREF(self->deleted);
195 self->deleted = PyList_New(0);
196
197 return mods;
198 error:
199 Py_XDECREF(added);
200 Py_XDECREF(deleted);
201 Py_XDECREF(iter);
202 Py_XDECREF(key);
203 Py_DECREF(mods);
204 return NULL;
205 }
206
207 /* Create a LDAPEntry from a LDAPMessage. */
208 LDAPEntry *
209 LDAPEntry_FromLDAPMessage(LDAPMessage *entrymsg, LDAPConnection *conn) {
210 int i;
211 int contain = -1;
212 char *dn;
213 char *attr;
214 struct berval **values;
215 BerElement *ber;
216 PyObject *rawval_list = NULL;
217 PyObject *val = NULL, *attrobj = NULL;
218 PyObject *args = NULL;
219 PyObject *lvl = NULL, *tmp = NULL;
220 LDAPEntry *self;
221
222 /* Create an attribute list for LDAPEntry (which is implemented in Python). */
223 dn = ldap_get_dn(conn->ld, entrymsg);
224 DEBUG("LDAPEntry_FromLDAPMessage (entrymsg:%p, conn:%p)[dn:%s]",
225 entrymsg, conn, dn);
226 if (dn == NULL) {
227 set_exception(conn->ld, 0);
228 return NULL;
229 }
230 args = Py_BuildValue("sO", dn, (PyObject *)conn);
231 ldap_memfree(dn);
232 if (args == NULL) return NULL;
233
234 if (LDAPEntryObj == NULL) {
235 /* Load Python-based LDAPEntry, if it's not already loaded. */
236 LDAPEntryObj = load_python_object("bonsai.ldapentry", "LDAPEntry");
237 if (LDAPEntryObj == NULL) return NULL;
238 }
239 /* Create a new LDAPEntry. */
240 self = (LDAPEntry *)PyObject_CallObject(LDAPEntryObj, args);
241 Py_DECREF(args);
242 if (self == NULL) return NULL;
243
244 /* Get list of attribute's names, whose values have to be kept in bytearray.*/
245 rawval_list = PyObject_GetAttrString(conn->client, "raw_attributes");
246 if (rawval_list == NULL) {
247 Py_DECREF(self);
248 return NULL;
249 }
250
251 /* Iterate over the LDAP attributes. */
252 for (attr = ldap_first_attribute(conn->ld, entrymsg, &ber);
253 attr != NULL; attr = ldap_next_attribute(conn->ld, entrymsg, ber)) {
254 /* Create a string of attribute's name and add to the attributes list. */
255 attrobj = PyUnicode_FromString(attr);
256 if (attrobj == NULL) goto error;
257 values = ldap_get_values_len(conn->ld, entrymsg, attr);
258 ldap_memfree(attr);
259
260 lvl = PyObject_CallFunctionObjArgs(LDAPValueListObj, NULL);
261 if (lvl == NULL) goto error;
262 if (values != NULL) {
263 /* Check attribute is in the raw_list. */
264 tmp = unique_contains(rawval_list, attrobj);
265 if (tmp == NULL) goto error;
266 contain = PyObject_IsTrue(PyTuple_GET_ITEM(tmp, 0));
267 Py_DECREF(tmp);
268 for (i = 0; values[i] != NULL; i++) {
269 /* Convert berval to PyObject*, if it's failed skip it. */
270 val = berval2PyObject(values[i], contain);
271 if (val == NULL) continue;
272 /* If the attribute has more value, then append to the list. */
273 if (PyList_Append(lvl, val) != 0) {
274 Py_DECREF(lvl);
275 goto error;
276 }
277 Py_DECREF(val);
278 }
279 }
280 ldap_value_free_len(values);
281 if (PyDict_SetItem((PyObject *)self, attrobj, lvl) != 0) {
282 Py_DECREF(lvl);
283 goto error;
284 }
285 Py_DECREF(attrobj);
286 Py_DECREF(lvl);
287 }
288 /* Cleaning the mess. */
289 Py_DECREF(rawval_list);
290 if (ber != NULL) {
291 ber_free(ber, 0);
292 }
293 return self;
294
295 error:
296 Py_XDECREF(attrobj);
297 Py_DECREF(self);
298 Py_DECREF(rawval_list);
299 ldap_memfree(attr);
300 if (ber != NULL) {
301 ber_free(ber, 0);
302 }
303 return (LDAPEntry *)PyErr_NoMemory();
304 }
305
306 /* Preform a LDAP add or modify operation depend on the `mod` parameter.
307 If `mod` is 0 then add new entry, otherwise modify it. */
308 PyObject *
309 LDAPEntry_AddOrModify(LDAPEntry *self, int mod) {
310 int rc = -1;
311 int msgid = -1;
312 char *dnstr = NULL;
313 unsigned short num_of_ctrls = 0;
314 struct berval ctrl_null_value = {0, NULL};
315 LDAPModList *mods = NULL;
316 LDAPControl **server_ctrls = NULL;
317 LDAPControl *ppolicy_ctrl = NULL;
318 LDAPControl *mdi_ctrl = NULL;
319
320 DEBUG("LDAPEntry_AddOrModify (self:%p, mod:%d)", self, mod);
321 /* Get DN string. */
322 dnstr = PyObject2char(self->dn);
323 if (dnstr == NULL || strlen(dnstr) == 0) {
324 PyErr_SetString(PyExc_ValueError, "Missing distinguished name.");
325 free(dnstr);
326 return NULL;
327 }
328
329 mods = LDAPEntry_CreateLDAPMods(self);
330 if (mods == NULL) {
331 PyErr_SetString(PyExc_MemoryError, "Create LDAPModList is failed.");
332 free(dnstr);
333 return NULL;
334 }
335
336 if (self->conn->ppolicy == 1) num_of_ctrls++;
337 if (self->conn->managedsait == 1) num_of_ctrls++;
338 if (num_of_ctrls > 0) {
339 server_ctrls = (LDAPControl **)malloc(sizeof(LDAPControl *) *
340 (num_of_ctrls + 1));
341 if (server_ctrls == NULL) {
342 Py_DECREF(mods);
343 free(dnstr);
344 return PyErr_NoMemory();
345 }
346 num_of_ctrls = 0;
347 }
348
349 if (self->conn->ppolicy == 1) {
350 /* Create password policy control if it is set. */
351 rc = ldap_create_passwordpolicy_control(self->conn->ld, &ppolicy_ctrl);
352 if (rc != LDAP_SUCCESS) {
353 PyErr_BadInternalCall();
354 Py_DECREF(mods);
355 free(dnstr);
356 return NULL;
357 }
358 server_ctrls[num_of_ctrls++] = ppolicy_ctrl;
359 server_ctrls[num_of_ctrls] = NULL;
360 }
361
362 if (self->conn->managedsait == 1) {
363 /* Create ManageDsaIT dcontrol. */
364 rc = ldap_control_create(LDAP_CONTROL_MANAGEDSAIT, 0, &ctrl_null_value,
365 1, &mdi_ctrl);
366 if (rc != LDAP_SUCCESS) {
367 PyErr_BadInternalCall();
368 Py_DECREF(mods);
369 free(dnstr);
370 return NULL;
371 }
372 server_ctrls[num_of_ctrls++] = mdi_ctrl;
373 server_ctrls[num_of_ctrls] = NULL;
374 }
375
376 if (mod == 0) {
377 rc = ldap_add_ext(self->conn->ld, dnstr, mods->mod_list, server_ctrls,
378 NULL, &msgid);
379 } else {
380 rc = ldap_modify_ext(self->conn->ld, dnstr, mods->mod_list, server_ctrls,
381 NULL, &msgid);
382 }
383
384 /* Clear the mess. */
385 free(dnstr);
386 if (ppolicy_ctrl != NULL) ldap_control_free(ppolicy_ctrl);
387 if (mdi_ctrl != NULL) _ldap_control_free(mdi_ctrl);
388 free(server_ctrls);
389
390 if (rc != LDAP_SUCCESS) {
391 set_exception(self->conn->ld, rc);
392 Py_DECREF(mods);
393 return NULL;
394 }
395 /* Add new add or modify operation to the pending_ops with mod_dict. */
396 if (add_to_pending_ops(self->conn->pending_ops, msgid,
397 (PyObject *)mods) != 0) {
398 Py_DECREF(mods);
399 return NULL;
400 }
401
402
403 return PyLong_FromLong((long int)msgid);
404 }
405
406 /* Rollback the status of an ldapentry after a failed operation. The
407 LDAPModList will be freed on success. */
408 int
409 LDAPEntry_Rollback(LDAPEntry *self, LDAPModList* mods) {
410 int mod_op = -1;
411 int status = -1;
412 PyObject *key = NULL;
413 PyObject *res_tuple = NULL;
414 PyObject *values = NULL;
415 PyObject *iter = NULL, *item = NULL;
416 PyObject *attr = NULL;
417 PyObject *added = NULL, *deleted = NULL;
418
419 DEBUG("LDAPEntry_Rollback (self:%p, mods:%p)", self, mods);
420 while (!LDAPModList_Empty(mods)) {
421 /* Get every item for the LDAPModList. */
422 res_tuple = LDAPModList_Pop(mods);
423 if (res_tuple == NULL) return -1;
424
425 if (!PyArg_ParseTuple(res_tuple, "OiO:rollback",
426 &key, &mod_op, &values)) return -1;
427
428 attr = LDAPEntry_GetItem(self, key); /* Borrowed ref. */
429
430 if (attr == NULL) {
431 /* If the attribute is remove from the LDAPEntry and deleted
432 with the previous modifications, then prepare for resending. */
433 if (values == Py_None) {
434 if (PyList_Append(self->deleted, key) != 0) return -1;
435 }
436 } else {
437 /* Get LDAPValueList's status. */
438 status = get_ldapvaluelist_status(attr);
439 if (status == -1) return -1;
440
441 /* Get LDAPValueList's __added list. */
442 added = PyObject_GetAttrString(attr, "added");
443 if (added == NULL) goto error;
444
445 /* Get LDAPValueList's __deleted list. */
446 deleted = PyObject_GetAttrString(attr, "deleted");
447 if (deleted == NULL) goto error;
448
449 /* When status is `replaced`, then drop the previous changes. */
450 if (status != 2) {
451 iter = PyObject_GetIter(values);
452 if (iter == NULL) goto error;
453 /* Check every item in the LDAPMod value list,
454 and append to the corresponding list for the attribute. */
455 for (item = PyIter_Next(iter); item != NULL;
456 item = PyIter_Next(iter)) {
457 switch (mod_op) {
458 case LDAP_MOD_ADD:
459 /* Check that the item is not in the list already
460 to avoid errors. */
461 if (uniqueness_check(attr, item) == 1 &&
462 uniqueness_check(added, item) == 0) {
463 if (PyList_Append(added, item) != 0) {
464 goto error;
465 }
466 }
467 if (set_ldapvaluelist_status(attr, 1) != 0) goto error;
468 break;
469 case LDAP_MOD_DELETE:
470 if (uniqueness_check(attr, item) == 0 &&
471 uniqueness_check(deleted, item) == 0) {
472 if (PyList_Append(deleted, item) != 0) goto error;
473 }
474 if (set_ldapvaluelist_status(attr, 1) != 0) goto error;
475 break;
476 case LDAP_MOD_REPLACE:
477 /* Nothing to do when the attribute's status is replaced. */
478 if (set_ldapvaluelist_status(attr, 2) != 0) goto error;
479 break;
480 }
481 Py_DECREF(item);
482 }
483 Py_DECREF(iter);
484 }
485 Py_DECREF(added);
486 Py_DECREF(deleted);
487 }
488 Py_DECREF(res_tuple);
489 }
490 return 0;
491
492 error:
493 Py_XDECREF(item);
494 Py_XDECREF(iter);
495 Py_XDECREF(added);
496 Py_XDECREF(deleted);
497 Py_DECREF(res_tuple);
498 return -1;
499 }
500
501 /* Sends the modifications of the entry to the directory server. */
502 static PyObject *
503 ldapentry_modify(LDAPEntry *self) {
504 /* Connection must be open. */
505 DEBUG("ldapentry_modify (self:%p)", self);
506 if (LDAPConnection_IsClosed(self->conn) != 0) return NULL;
507
508 return LDAPEntry_AddOrModify(self, 1);
509 }
510
511 /* Set distinguished name for a LDAPEntry. */
512 static int
513 ldapentry_setdn(LDAPEntry *self, PyObject *value, void *closure) {
514 return LDAPEntry_SetDN(self, value);
515 }
516
517 /* Returns the DN of the LDAPEntry. */
518 static PyObject *
519 ldapentry_getdn(LDAPEntry *self, void *closure) {
520 Py_INCREF(self->dn);
521 return self->dn;
522 }
523
524 /* Disables modifying deleted keys. */
525 static int
526 ldapentry_setdeletedkeys(LDAPEntry *self, PyObject *value, void *closure) {
527 PyErr_SetString(PyExc_ValueError, "Cannot change deleted_keys.");
528 return -1;
529 }
530
531 /* Returns the copy of the deleted keys in the LDAPEntry. */
532 static PyObject *
533 ldapentry_getdeletedkeys(LDAPEntry *self, void *closure) {
534 return PyObject_CallMethod(self->deleted, "copy", NULL);
535 }
536
537
538 /* Converts a Python string or LDAPDN object into an LDAPDN object. */
539 static PyObject *
540 convert_to_ldapdn(PyObject *obj) {
541 PyObject *dn = NULL;
542
543 if (PyObject_IsInstance(obj, LDAPDNObj)) {
544 Py_INCREF(obj);
545 return obj;
546 } else if (PyUnicode_Check(obj)) {
547 /* Call LDAPDN __init__ with the dn string. */
548 dn = PyObject_CallFunctionObjArgs(LDAPDNObj, obj, NULL);
549 if (dn == NULL) return NULL;
550 return dn;
551 } else {
552 PyErr_SetString(PyExc_TypeError, "The DN attribute value must"
553 " be an LDAPDN or a string.");
554 return NULL;
555 }
556 }
557
558 /* Renames the entry object on the directory server, which means changing
559 the DN of the entry. */
560 static PyObject *
561 ldapentry_rename(LDAPEntry *self, PyObject *args, PyObject *kwds) {
562 int rc;
563 int msgid = -1;
564 char *newparent_str, *newrdn_str, *olddn_str;
565 PyObject *newdn, *newparent, *newrdn, *deleteold;
566 PyObject *tmp, *new_ldapdn = NULL;
567 char *kwlist[] = {"newdn", "delete_old_rdn", NULL};
568
569 /* Connection must be open. */
570 if (LDAPConnection_IsClosed(self->conn) != 0) return NULL;
571
572 DEBUG("ldapentry_rename (self:%p)", self);
573 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO!", kwlist, &newdn,
574 &PyBool_Type, &deleteold)) {
575 return NULL;
576 }
577
578 /* Save old dn string. */
579 tmp = PyObject_Str(self->dn);
580 olddn_str = PyObject2char(tmp);
581 Py_DECREF(tmp);
582 if (olddn_str == NULL) return NULL;
583
584 /* Convert the newdn object to an LDAPDN object. */
585 new_ldapdn = convert_to_ldapdn(newdn);
586 if (new_ldapdn == NULL) {
587 free(olddn_str);
588 return NULL;
589 }
590
591 /* Get rdn and parent strings. */
592 newrdn = PySequence_GetItem(new_ldapdn, 0);
593 newparent = PySequence_GetSlice(new_ldapdn, 1, PyObject_Size(self->dn));
594 if (newrdn == NULL || newparent == NULL) {
595 free(olddn_str);
596 Py_DECREF(tmp);
597 return NULL;
598 }
599
600 newrdn_str = PyObject2char(newrdn);
601 newparent_str = PyObject2char(newparent);
602 Py_DECREF(newrdn);
603 Py_DECREF(newparent);
604
605 rc = ldap_rename(self->conn->ld, olddn_str, newrdn_str, newparent_str,
606 PyObject_IsTrue(deleteold), NULL, NULL, &msgid);
607 /* Clean up strings. */
608 free(olddn_str);
609 free(newrdn_str);
610 free(newparent_str);
611 if (rc != LDAP_SUCCESS) {
612 set_exception(self->conn->ld, rc);
613 return NULL;
614 }
615
616 /* Add new rename operation to the pending_ops,
617 with a tuple of the entry and the new DN. */
618 tmp = Py_BuildValue("(O,O)", (PyObject *)self, new_ldapdn);
619 Py_DECREF(new_ldapdn);
620 if (tmp == NULL) return NULL;
621 if (add_to_pending_ops(self->conn->pending_ops, msgid, tmp) != 0) {
622 Py_DECREF(tmp);
623 return NULL;
624 }
625
626 return PyLong_FromLong((long int)msgid);
627 }
628
629 static PyMethodDef ldapentry_methods[] = {
630 {"modify", (PyCFunction)ldapentry_modify, METH_NOARGS,
631 "Send LDAPEntry's modification to the LDAP server."},
632 {"rename", (PyCFunction)ldapentry_rename, METH_VARARGS | METH_KEYWORDS,
633 "Rename or remove LDAPEntry on the LDAP server."},
634 {NULL, NULL, 0, NULL} /* Sentinel */
635 };
636
637 /* Searches among lower-cased keystrings to find a match with the key.
638 if `del` set to 1, then also searches among the deleted keys.
639 Returns a new reference of the case-insensitive key if it's presented,
640 otherwise returns NULL. */
641 static PyObject *
642 searchLowerCaseKeyMatch(LDAPEntry *self, PyObject *key, int del) {
643 PyObject *keys = PyDict_Keys((PyObject *)self);
644 PyObject *iter = PyObject_GetIter(keys);
645 PyObject *item = NULL, *cikey = NULL;
646
647 if (iter == NULL) {
648 Py_DECREF(keys);
649 return NULL;
650 }
651
652 /* Searching for same lowercase key among the other keys. */
653 for (item = PyIter_Next(iter); item != NULL; item = PyIter_Next(iter)) {
654 if (lower_case_match(item, key) == 1) {
655 cikey = item;
656 break;
657 }
658 Py_DECREF(item);
659 }
660 Py_DECREF(iter);
661 Py_DECREF(keys);
662 /* Searching among the deleted keys. */
663 if (cikey == NULL && del == 1) {
664 iter = PyObject_GetIter((PyObject *)self->deleted);
665 if (iter == NULL) return NULL;
666 for (item = PyIter_Next(iter); item != NULL; item = PyIter_Next(iter)) {
667 if (lower_case_match(item, key) == 1) {
668 cikey = item;
669 break;
670 }
671 Py_DECREF(item);
672 }
673 Py_DECREF(iter);
674 }
675 return cikey;
676 }
677
678 /* Returns the object (with borrowed reference) from the LDAPEntry,
679 which has a case-insensitive match. */
680 PyObject *
681 LDAPEntry_GetItem(LDAPEntry *self, PyObject *key) {
682 PyObject *match = NULL, *res = NULL;
683
684 DEBUG("LDAPEntry_GetItem (self:%p, key:%p)", self, key);
685
686 match = searchLowerCaseKeyMatch(self, key, 0);
687 if (match == NULL) {
688 if (PyErr_Occurred()) return NULL;
689 match = key;
690 Py_INCREF(match);
691 }
692
693 res = PyDict_GetItem((PyObject *)self, match);
694 Py_DECREF(match);
695 return res;
696 }
697
698 /* Set item to LDAPEntry with a case-insensitive key. */
699 int
700 LDAPEntry_SetItem(LDAPEntry *self, PyObject *key, PyObject *value) {
701 int rc = 0;
702 int status = 1;
703 char *newkey = lowercase(PyObject2char(key));
704 PyObject *list = NULL;
705 PyObject *tmp = NULL;
706 PyObject *cikey = NULL; /* The actual (case-insenstive) key in the entry */
707
708 if (newkey == NULL) {
709 PyErr_BadInternalCall();
710 return -1;
711 }
712 DEBUG("LDAPEntry_SetItem (self:%p)[key:%s]", self, newkey);
713
714 /* Search for a match. */
715 cikey = searchLowerCaseKeyMatch(self, key, 1);
716 if (cikey == NULL) {
717 if (PyErr_Occurred()) return -1;
718 cikey = key;
719 status = 1;
720 Py_INCREF(cikey);
721 } else {
722 status = 2;
723 }
724
725 if (value != NULL) {
726 /* If theres an item with a `dn` key, and with a string value set to the dn attribute. */
727 if (strcmp(newkey, "dn") == 0) {
728 free(newkey);
729 if (LDAPEntry_SetDN(self, value) != 0) {
730 Py_DECREF(cikey);
731 return -1;
732 }
733 } else {
734 free(newkey);
735 /* Set the new value to the item. */
736 if (PyObject_IsInstance(value, LDAPValueListObj) == 0) {
737 /* Convert value to LDAPValueList object. */
738 list = PyObject_CallFunctionObjArgs(LDAPValueListObj, NULL);
739 if (PyList_Check(value) || PyTuple_Check(value)) {
740 tmp = PyObject_CallMethod(list, "extend", "(O)", value);
741 if (tmp == NULL) {
742 Py_DECREF(list);
743 Py_DECREF(cikey);
744 return -1;
745 }
746 } else {
747 tmp = PyObject_CallMethod(list, "append", "(O)", value);
748 if (tmp == NULL) {
749 Py_DECREF(list);
750 Py_DECREF(cikey);
751 return -1;
752 }
753 }
754 Py_DECREF(tmp);
755 rc = PyDict_SetItem((PyObject *)self, cikey, (PyObject *)list);
756 if (set_ldapvaluelist_status(list, status) != 0) {
757 Py_DECREF(cikey);
758 return -1;
759 }
760 Py_DECREF(list);
761 } else {
762 rc = PyDict_SetItem((PyObject *)self, cikey, value);
763 if (set_ldapvaluelist_status(value, status) != 0) {
764 Py_DECREF(cikey);
765 return -1;
766 }
767 }
768 /* Avoid inconsistency. (same key in the added and the deleted list) */
769 if (PySequence_Contains(self->deleted, cikey)) {
770 if (uniqueness_remove(self->deleted, cikey) != 1) {
771 Py_DECREF(cikey);
772 return -1;
773 }
774 }
775 if (rc != 0) {
776 Py_DECREF(cikey);
777 return rc;
778 }
779 }
780 } else {
781 if (strcmp(newkey, "dn") == 0) {
782 free(newkey);
783 PyErr_SetString(PyExc_TypeError, "Cannot delete the DN key");
784 return -1;
785 }
786 free(newkey);
787 /* This means, the item has to be removed. */
788 if (PyList_Append(self->deleted, cikey) != 0) {
789 Py_DECREF(cikey);
790 return -1;
791 }
792 if (PyDict_DelItem((PyObject *)self, cikey) != 0) {
793 Py_DECREF(cikey);
794 return -1;
795 }
796 }
797 Py_DECREF(cikey);
798 return 0;
799 }
800
801 /* Checks that `key` is in the LDAPEntry. */
802 static int
803 ldapentry_contains(PyObject *op, PyObject *key) {
804 PyObject *obj = NULL;
805 LDAPEntry *self = (LDAPEntry *)op;
806
807 DEBUG("ldapentry_contains (self:%p, key:%p)", self, key);
808 obj = searchLowerCaseKeyMatch(self, key, 0);
809 if (obj == NULL) {
810 if (PyErr_Occurred()) return -1;
811 else return 0;
812 }
813
814 Py_DECREF(obj);
815 return 1;
816 }
817
818 static PySequenceMethods ldapentry_as_sequence = {
819 0, /* sq_length */
820 0, /* sq_concat */
821 0, /* sq_repeat */
822 0, /* sq_item */
823 0, /* sq_slice */
824 0, /* sq_ass_item */
825 0, /* sq_ass_slice */
826 ldapentry_contains, /* sq_contains */
827 0, /* sq_inplace_concat */
828 0, /* sq_inplace_repeat */
829 };
830
831 static PyObject *
832 ldapentry_subscript(LDAPEntry *self, PyObject *key) {
833 PyObject *val = LDAPEntry_GetItem(self, key);
834 if (val == NULL) {
835 PyErr_Format(PyExc_KeyError, "Key %R is not in the LDAPEntry.", key);
836 return NULL;
837 }
838 Py_INCREF(val);
839 return val;
840 }
841
842 static int
843 ldapentry_ass_sub(LDAPEntry *self, PyObject *key, PyObject *value) {
844 if (key == NULL) return PyDict_DelItem((PyObject *)self, key);
845 else return LDAPEntry_SetItem(self, key, value);
846 }
847
848 static PyMappingMethods ldapentry_mapping_meths = {
849 0, /* mp_length */
850 (binaryfunc)ldapentry_subscript, /* mp_subscript */
851 (objobjargproc)ldapentry_ass_sub, /* mp_ass_subscript */
852 };
853
854 /* Set LDAPConnection for a LDAPEntry. */
855 int
856 LDAPEntry_SetConnection(LDAPEntry *self, LDAPConnection *conn) {
857 PyObject *tmp;
858
859 DEBUG("LDAPEntry_SetConnection (self:%p, conn:%p)", self, conn);
860 if (conn) {
861 tmp = (PyObject *)self->conn;
862 Py_INCREF(conn);
863 self->conn = conn;
864 Py_XDECREF(tmp);
865 } else {
866 return -1;
867 }
868 return 0;
869 }
870
871 /* Setter for connection attribute. */
872 static int
873 ldapentry_setconnection(LDAPEntry *self, PyObject *value, void *closure) {
874 if (value == NULL) {
875 PyErr_SetString(PyExc_TypeError, "Cannot delete the connection attribute.");
876 return -1;
877 }
878
879 if (!PyObject_IsInstance(value, (PyObject *)&LDAPConnectionType)) {
880 PyErr_SetString(PyExc_TypeError, "The connection attribute value must be an LDAPConnection.");
881 return -1;
882 }
883
884 if (LDAPEntry_SetConnection(self, (LDAPConnection *)value) != 0) return -1;
885
886 return 0;
887 }
888
889 /* Getter for connection attribute. */
890 static PyObject *
891 ldapentry_getconnection(LDAPEntry *self, void *closure) {
892 if (self->conn == NULL) {
893 PyErr_SetString(PyExc_ValueError, "LDAPConnection is not set.");
894 return NULL;
895 }
896 Py_INCREF(self->conn);
897 return (PyObject *)self->conn;
898 }
899
900 /* Set a `value` Python object as a DN for an LDAP entry. */
901 int
902 LDAPEntry_SetDN(LDAPEntry *self, PyObject *value) {
903 PyObject *dn = NULL;
904
905 DEBUG("LDAPEntry_SetDN (self:%p, value:%p)", self, value);
906 if (value == NULL) {
907 PyErr_SetString(PyExc_TypeError, "Cannot delete the DN attribute.");
908 return -1;
909 }
910
911 dn = convert_to_ldapdn(value);
912 if (dn == NULL) return -1;
913
914 Py_DECREF(self->dn);
915 self->dn = dn;
916 if (PyDict_SetItemString((PyObject *)self, "dn", dn) != 0) return -1;
917
918 return 0;
919 }
920
921 static PyGetSetDef ldapentry_getsetters[] = {
922 {"connection", (getter)ldapentry_getconnection,
923 (setter)ldapentry_setconnection,
924 "LDAP connection.", NULL},
925 {"dn", (getter)ldapentry_getdn,
926 (setter)ldapentry_setdn,
927 "Distinguished name", NULL},
928 {"deleted_keys", (getter)ldapentry_getdeletedkeys,
929 (setter)ldapentry_setdeletedkeys,
930 "Deleted keys", NULL},
931 {NULL} /* Sentinel */
932 };
933
934 PyTypeObject LDAPEntryType = {
935 PyVarObject_HEAD_INIT(NULL, 0)
936 "_bonsai.ldapentry", /* tp_name */
937 sizeof(LDAPEntry), /* tp_basicsize */
938 0, /* tp_itemsize */
939 (destructor)ldapentry_dealloc, /* tp_dealloc */
940 0, /* tp_print */
941 0, /* tp_getattr */
942 0, /* tp_setattr */
943 0, /* tp_reserved */
944 0, /* tp_repr */
945 0, /* tp_as_number */
946 &ldapentry_as_sequence, /* tp_as_sequence */
947 &ldapentry_mapping_meths,/* tp_as_mapping */
948 0, /* tp_hash */
949 0, /* tp_call */
950 0, /* tp_str */
951 0, /* tp_getattro */
952 0, /* tp_setattro */
953 0, /* tp_as_buffer */
954 Py_TPFLAGS_DEFAULT |
955 Py_TPFLAGS_BASETYPE |
956 Py_TPFLAGS_HAVE_GC, /* tp_flags */
957 0, /* tp_doc */
958 (traverseproc)ldapentry_traverse,/* tp_traverse */
959 (inquiry)ldapentry_clear, /* tp_clear */
960 0, /* tp_richcompare */
961 0, /* tp_weaklistoffset */
962 0, /* tp_iter */
963 0, /* tp_iternext */
964 ldapentry_methods, /* tp_methods */
965 0, /* tp_members */
966 ldapentry_getsetters, /* tp_getset */
967 0, /* tp_base */
968 0, /* tp_dict */
969 0, /* tp_descr_get */
970 0, /* tp_descr_set */
971 0, /* tp_dictoffset */
972 (initproc)ldapentry_init,/* tp_init */
973 0, /* tp_alloc */
974 ldapentry_new, /* tp_new */
975 };