functions.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* See http://www.python-ldap.org/ for details.
  2. * $Id: functions.c,v 1.27 2009/08/17 05:00:57 leonard Exp $ */
  3. #include "common.h"
  4. #include "functions.h"
  5. #include "LDAPObject.h"
  6. #include "berval.h"
  7. #include "errors.h"
  8. #include "options.h"
  9. /* ldap_initialize */
  10. static PyObject*
  11. l_ldap_initialize(PyObject* unused, PyObject *args)
  12. {
  13. char *uri;
  14. LDAP *ld = NULL;
  15. int ret;
  16. if (!PyArg_ParseTuple(args, "s", &uri))
  17. return NULL;
  18. Py_BEGIN_ALLOW_THREADS
  19. ret = ldap_initialize(&ld, uri);
  20. Py_END_ALLOW_THREADS
  21. if (ret != LDAP_SUCCESS)
  22. return LDAPerror(ld, "ldap_initialize");
  23. return (PyObject*)newLDAPObject(ld);
  24. }
  25. /* ldap_str2dn */
  26. static PyObject*
  27. l_ldap_str2dn( PyObject* unused, PyObject *args )
  28. {
  29. struct berval str;
  30. LDAPDN dn;
  31. int flags = 0;
  32. PyObject *result = NULL, *tmp;
  33. int res, i, j;
  34. Py_ssize_t str_len;
  35. /*
  36. * From a DN string such as "a=b,c=d;e=f", build
  37. * a list-equivalent of AVA structures; namely:
  38. * ((('a','b',1),('c','d',1)),(('e','f',1),))
  39. * The integers are a bit combination of the AVA_* flags
  40. */
  41. if (!PyArg_ParseTuple( args, "z#|i:str2dn",
  42. &str.bv_val, &str_len, &flags ))
  43. return NULL;
  44. str.bv_len = (ber_len_t) str_len;
  45. res = ldap_bv2dn(&str, &dn, flags);
  46. if (res != LDAP_SUCCESS)
  47. return LDAPerr(res);
  48. tmp = PyList_New(0);
  49. if (!tmp)
  50. goto failed;
  51. for (i = 0; dn[i]; i++) {
  52. LDAPRDN rdn;
  53. PyObject *rdnlist;
  54. rdn = dn[i];
  55. rdnlist = PyList_New(0);
  56. if (!rdnlist)
  57. goto failed;
  58. if (PyList_Append(tmp, rdnlist) == -1) {
  59. Py_DECREF(rdnlist);
  60. goto failed;
  61. }
  62. for (j = 0; rdn[j]; j++) {
  63. LDAPAVA *ava = rdn[j];
  64. PyObject *tuple;
  65. tuple = Py_BuildValue("(O&O&i)",
  66. LDAPberval_to_object, &ava->la_attr,
  67. LDAPberval_to_object, &ava->la_value,
  68. ava->la_flags & ~(LDAP_AVA_FREE_ATTR|LDAP_AVA_FREE_VALUE));
  69. if (!tuple) {
  70. Py_DECREF(rdnlist);
  71. goto failed;
  72. }
  73. if (PyList_Append(rdnlist, tuple) == -1) {
  74. Py_DECREF(tuple);
  75. goto failed;
  76. }
  77. Py_DECREF(tuple);
  78. }
  79. Py_DECREF(rdnlist);
  80. }
  81. result = tmp;
  82. tmp = NULL;
  83. failed:
  84. Py_XDECREF(tmp);
  85. ldap_dnfree(dn);
  86. return result;
  87. }
  88. /* ldap_set_option (global options) */
  89. static PyObject*
  90. l_ldap_set_option(PyObject* self, PyObject *args)
  91. {
  92. PyObject *value;
  93. int option;
  94. if (!PyArg_ParseTuple(args, "iO:set_option", &option, &value))
  95. return NULL;
  96. if (!LDAP_set_option(NULL, option, value))
  97. return NULL;
  98. Py_INCREF(Py_None);
  99. return Py_None;
  100. }
  101. /* ldap_get_option (global options) */
  102. static PyObject*
  103. l_ldap_get_option(PyObject* self, PyObject *args)
  104. {
  105. int option;
  106. if (!PyArg_ParseTuple(args, "i:get_option", &option))
  107. return NULL;
  108. return LDAP_get_option(NULL, option);
  109. }
  110. /* methods */
  111. static PyMethodDef methods[] = {
  112. { "initialize", (PyCFunction)l_ldap_initialize, METH_VARARGS },
  113. { "str2dn", (PyCFunction)l_ldap_str2dn, METH_VARARGS },
  114. { "set_option", (PyCFunction)l_ldap_set_option, METH_VARARGS },
  115. { "get_option", (PyCFunction)l_ldap_get_option, METH_VARARGS },
  116. { NULL, NULL }
  117. };
  118. /* initialisation */
  119. void
  120. LDAPinit_functions( PyObject* d ) {
  121. LDAPadd_methods( d, methods );
  122. }