options.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* See http://www.python-ldap.org/ for details.
  2. * $Id: options.c,v 1.35 2009/11/13 08:45:50 stroeder Exp $ */
  3. #include "common.h"
  4. #include "errors.h"
  5. #include "LDAPObject.h"
  6. #include "ldapcontrol.h"
  7. #include "options.h"
  8. void
  9. set_timeval_from_double( struct timeval *tv, double d ) {
  10. tv->tv_usec = (long) ( fmod(d, 1.0) * 1000000.0 );
  11. tv->tv_sec = (long) floor(d);
  12. }
  13. /**
  14. * Converts a return code from ldap_set_option() or ldap_get_option()
  15. * into a python error, and returns NULL.
  16. */
  17. static PyObject *
  18. option_error(int res, const char *fn)
  19. {
  20. if (res == LDAP_OPT_ERROR)
  21. PyErr_SetString(PyExc_ValueError, "option error");
  22. else if (res == LDAP_PARAM_ERROR)
  23. PyErr_SetString(PyExc_ValueError, "parameter error");
  24. else if (res == LDAP_NO_MEMORY)
  25. PyErr_NoMemory();
  26. else
  27. PyErr_Format(PyExc_SystemError, "error %d from %s", res, fn);
  28. return NULL;
  29. }
  30. /**
  31. * Sets an LDAP option.
  32. * Returns 0 on failure, 1 on success
  33. */
  34. int
  35. LDAP_set_option(LDAPObject *self, int option, PyObject *value)
  36. {
  37. int res;
  38. int intval;
  39. double doubleval;
  40. char *strval;
  41. struct timeval tv;
  42. void *ptr;
  43. LDAP *ld;
  44. LDAPControl **controls = NULL;
  45. ld = self ? self->ldap : NULL;
  46. switch(option) {
  47. case LDAP_OPT_API_INFO:
  48. case LDAP_OPT_API_FEATURE_INFO:
  49. #ifdef HAVE_SASL
  50. case LDAP_OPT_X_SASL_SSF:
  51. #endif
  52. /* Read-only options */
  53. PyErr_SetString(PyExc_ValueError, "read-only option");
  54. return 0;
  55. case LDAP_OPT_REFERRALS:
  56. case LDAP_OPT_RESTART:
  57. #ifdef LDAP_OPT_X_SASL_NOCANON
  58. case LDAP_OPT_X_SASL_NOCANON:
  59. #endif
  60. #ifdef LDAP_OPT_CONNECT_ASYNC
  61. case LDAP_OPT_CONNECT_ASYNC:
  62. #endif
  63. /* Truth-value options */
  64. ptr = PyObject_IsTrue(value) ? LDAP_OPT_ON : LDAP_OPT_OFF;
  65. break;
  66. case LDAP_OPT_DEREF:
  67. case LDAP_OPT_SIZELIMIT:
  68. case LDAP_OPT_TIMELIMIT:
  69. case LDAP_OPT_PROTOCOL_VERSION:
  70. case LDAP_OPT_ERROR_NUMBER:
  71. case LDAP_OPT_DEBUG_LEVEL:
  72. #ifdef HAVE_TLS
  73. case LDAP_OPT_X_TLS:
  74. case LDAP_OPT_X_TLS_REQUIRE_CERT:
  75. #ifdef LDAP_OPT_X_TLS_NEWCTX
  76. case LDAP_OPT_X_TLS_NEWCTX:
  77. #endif
  78. #ifdef LDAP_OPT_X_TLS_PROTOCOL_MIN
  79. case LDAP_OPT_X_TLS_PROTOCOL_MIN:
  80. #endif
  81. #endif
  82. #ifdef HAVE_SASL
  83. case LDAP_OPT_X_SASL_SSF_MIN:
  84. case LDAP_OPT_X_SASL_SSF_MAX:
  85. #endif
  86. /* integer value options */
  87. if (!PyArg_Parse(value, "i:set_option", &intval))
  88. return 0;
  89. ptr = &intval;
  90. break;
  91. case LDAP_OPT_HOST_NAME:
  92. case LDAP_OPT_URI:
  93. case LDAP_OPT_ERROR_STRING:
  94. case LDAP_OPT_MATCHED_DN:
  95. #ifdef HAVE_TLS
  96. case LDAP_OPT_X_TLS_CACERTFILE:
  97. case LDAP_OPT_X_TLS_CACERTDIR:
  98. case LDAP_OPT_X_TLS_CERTFILE:
  99. case LDAP_OPT_X_TLS_KEYFILE:
  100. case LDAP_OPT_X_TLS_CIPHER_SUITE:
  101. case LDAP_OPT_X_TLS_RANDOM_FILE:
  102. case LDAP_OPT_X_TLS_DHFILE:
  103. #endif
  104. #ifdef HAVE_SASL
  105. case LDAP_OPT_X_SASL_SECPROPS:
  106. #endif
  107. /* String valued options */
  108. if (!PyArg_Parse(value, "s:set_option", &strval))
  109. return 0;
  110. ptr = strval;
  111. break;
  112. case LDAP_OPT_TIMEOUT:
  113. case LDAP_OPT_NETWORK_TIMEOUT:
  114. /* Float valued timeval options */
  115. if (!PyArg_Parse(value, "d:set_option", &doubleval))
  116. return 0;
  117. if (doubleval >= 0) {
  118. set_timeval_from_double( &tv, doubleval );
  119. ptr = &tv;
  120. } else {
  121. ptr = NULL;
  122. }
  123. break;
  124. case LDAP_OPT_SERVER_CONTROLS:
  125. case LDAP_OPT_CLIENT_CONTROLS:
  126. if (!LDAPControls_from_object(value, &controls))
  127. return 0;
  128. ptr = controls;
  129. break;
  130. default:
  131. PyErr_Format(PyExc_ValueError, "unknown option %d", option);
  132. return 0;
  133. }
  134. if (self) LDAP_BEGIN_ALLOW_THREADS(self);
  135. res = ldap_set_option(ld, option, ptr);
  136. if (self) LDAP_END_ALLOW_THREADS(self);
  137. if ((option == LDAP_OPT_SERVER_CONTROLS) || (option == LDAP_OPT_CLIENT_CONTROLS))
  138. LDAPControl_List_DEL(controls);
  139. if (res != LDAP_OPT_SUCCESS) {
  140. option_error(res, "ldap_set_option");
  141. return 0;
  142. }
  143. return 1;
  144. }
  145. PyObject *
  146. LDAP_get_option(LDAPObject *self, int option)
  147. {
  148. int res;
  149. int intval;
  150. struct timeval *tv;
  151. LDAPAPIInfo apiinfo;
  152. LDAPControl **lcs;
  153. LDAPControl *lc;
  154. char *strval;
  155. PyObject *extensions, *v, *tup;
  156. Py_ssize_t i, num_extensions, num_controls;
  157. LDAP *ld;
  158. ld = self ? self->ldap : NULL;
  159. switch(option) {
  160. case LDAP_OPT_API_INFO:
  161. apiinfo.ldapai_info_version = LDAP_API_INFO_VERSION;
  162. if (self) LDAP_BEGIN_ALLOW_THREADS(self);
  163. res = ldap_get_option( ld, option, &apiinfo );
  164. if (self) LDAP_END_ALLOW_THREADS(self);
  165. if (res != LDAP_OPT_SUCCESS)
  166. return option_error(res, "ldap_get_option");
  167. /* put the extensions into tuple form */
  168. num_extensions = 0;
  169. while (apiinfo.ldapai_extensions[num_extensions])
  170. num_extensions++;
  171. extensions = PyTuple_New(num_extensions);
  172. for (i = 0; i < num_extensions; i++)
  173. PyTuple_SET_ITEM(extensions, i,
  174. PyString_FromString(apiinfo.ldapai_extensions[i]));
  175. /* return api info as a dictionary */
  176. v = Py_BuildValue("{s:i, s:i, s:i, s:s, s:i, s:O}",
  177. "info_version", apiinfo.ldapai_info_version,
  178. "api_version", apiinfo.ldapai_api_version,
  179. "protocol_version", apiinfo.ldapai_protocol_version,
  180. "vendor_name", apiinfo.ldapai_vendor_name,
  181. "vendor_version", apiinfo.ldapai_vendor_version,
  182. "extensions", extensions);
  183. if (apiinfo.ldapai_vendor_name)
  184. ldap_memfree(apiinfo.ldapai_vendor_name);
  185. for (i = 0; i < num_extensions; i++)
  186. ldap_memfree(apiinfo.ldapai_extensions[i]);
  187. ldap_memfree(apiinfo.ldapai_extensions);
  188. Py_DECREF(extensions);
  189. return v;
  190. #ifdef HAVE_SASL
  191. case LDAP_OPT_X_SASL_SSF:
  192. #endif
  193. case LDAP_OPT_REFERRALS:
  194. case LDAP_OPT_RESTART:
  195. case LDAP_OPT_DEREF:
  196. case LDAP_OPT_SIZELIMIT:
  197. case LDAP_OPT_TIMELIMIT:
  198. case LDAP_OPT_PROTOCOL_VERSION:
  199. case LDAP_OPT_ERROR_NUMBER:
  200. case LDAP_OPT_DEBUG_LEVEL:
  201. #ifdef HAVE_TLS
  202. case LDAP_OPT_X_TLS:
  203. case LDAP_OPT_X_TLS_REQUIRE_CERT:
  204. #ifdef LDAP_OPT_X_TLS_PROTOCOL_MIN
  205. case LDAP_OPT_X_TLS_PROTOCOL_MIN:
  206. #endif
  207. #endif
  208. #ifdef HAVE_SASL
  209. case LDAP_OPT_X_SASL_SSF_MIN:
  210. case LDAP_OPT_X_SASL_SSF_MAX:
  211. #endif
  212. #ifdef LDAP_OPT_X_SASL_NOCANON
  213. case LDAP_OPT_X_SASL_NOCANON:
  214. #endif
  215. #ifdef LDAP_OPT_CONNECT_ASYNC
  216. case LDAP_OPT_CONNECT_ASYNC:
  217. #endif
  218. /* Integer-valued options */
  219. if (self) LDAP_BEGIN_ALLOW_THREADS(self);
  220. res = ldap_get_option(ld, option, &intval);
  221. if (self) LDAP_END_ALLOW_THREADS(self);
  222. if (res != LDAP_OPT_SUCCESS)
  223. return option_error(res, "ldap_get_option");
  224. return PyInt_FromLong(intval);
  225. case LDAP_OPT_HOST_NAME:
  226. case LDAP_OPT_URI:
  227. case LDAP_OPT_ERROR_STRING:
  228. case LDAP_OPT_MATCHED_DN:
  229. #ifdef HAVE_TLS
  230. case LDAP_OPT_X_TLS_CACERTFILE:
  231. case LDAP_OPT_X_TLS_CACERTDIR:
  232. case LDAP_OPT_X_TLS_CERTFILE:
  233. case LDAP_OPT_X_TLS_KEYFILE:
  234. case LDAP_OPT_X_TLS_CIPHER_SUITE:
  235. case LDAP_OPT_X_TLS_RANDOM_FILE:
  236. case LDAP_OPT_X_TLS_DHFILE:
  237. #endif
  238. #ifdef HAVE_SASL
  239. case LDAP_OPT_X_SASL_SECPROPS:
  240. case LDAP_OPT_X_SASL_MECH:
  241. case LDAP_OPT_X_SASL_REALM:
  242. case LDAP_OPT_X_SASL_AUTHCID:
  243. case LDAP_OPT_X_SASL_AUTHZID:
  244. #ifdef LDAP_OPT_X_SASL_USERNAME
  245. case LDAP_OPT_X_SASL_USERNAME:
  246. #endif
  247. #endif
  248. /* String-valued options */
  249. if (self) LDAP_BEGIN_ALLOW_THREADS(self);
  250. res = ldap_get_option(ld, option, &strval);
  251. if (self) LDAP_END_ALLOW_THREADS(self);
  252. if (res != LDAP_OPT_SUCCESS)
  253. return option_error(res, "ldap_get_option");
  254. if (strval == NULL) {
  255. Py_INCREF(Py_None);
  256. return Py_None;
  257. }
  258. v = PyString_FromString(strval);
  259. ldap_memfree(strval);
  260. return v;
  261. case LDAP_OPT_TIMEOUT:
  262. case LDAP_OPT_NETWORK_TIMEOUT:
  263. /* Double-valued timeval options */
  264. if (self) LDAP_BEGIN_ALLOW_THREADS(self);
  265. res = ldap_get_option(ld, option, &tv);
  266. if (self) LDAP_END_ALLOW_THREADS(self);
  267. if (res != LDAP_OPT_SUCCESS)
  268. return option_error(res, "ldap_get_option");
  269. if (tv == NULL) {
  270. Py_INCREF(Py_None);
  271. return Py_None;
  272. }
  273. v = PyFloat_FromDouble(
  274. (double) tv->tv_sec + ( (double) tv->tv_usec / 1000000.0 )
  275. );
  276. ldap_memfree(tv);
  277. return v;
  278. case LDAP_OPT_SERVER_CONTROLS:
  279. case LDAP_OPT_CLIENT_CONTROLS:
  280. if (self) LDAP_BEGIN_ALLOW_THREADS(self);
  281. res = ldap_get_option(ld, option, &lcs);
  282. if (self) LDAP_END_ALLOW_THREADS(self);
  283. if (res != LDAP_OPT_SUCCESS)
  284. return option_error(res, "ldap_get_option");
  285. if (lcs == NULL)
  286. return PyList_New(0);
  287. /* Get the number of controls */
  288. num_controls = 0;
  289. while (lcs[num_controls])
  290. num_controls++;
  291. /* We'll build a list of controls, with each control a tuple */
  292. v = PyList_New(num_controls);
  293. for (i = 0; i < num_controls; i++) {
  294. lc = lcs[i];
  295. tup = Py_BuildValue("(sbs)",
  296. lc->ldctl_oid,
  297. lc->ldctl_iscritical,
  298. lc->ldctl_value.bv_val);
  299. PyList_SET_ITEM(v, i, tup);
  300. }
  301. ldap_controls_free(lcs);
  302. return v;
  303. default:
  304. PyErr_Format(PyExc_ValueError, "unknown option %d", option);
  305. return NULL;
  306. }
  307. }