ldaputil.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
  3. * Copyright (C) 2003-2009 Match Grun and the Claws Mail team
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. /*
  20. * Some utility functions to access LDAP servers.
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. # include "config.h"
  24. #endif
  25. #ifdef USE_LDAP
  26. #include <glib.h>
  27. #include <string.h>
  28. #include <sys/time.h>
  29. #include <ldap.h>
  30. #include <lber.h>
  31. #include <errno.h>
  32. #include "common/utils.h"
  33. #define SYLDAP_TEST_FILTER "(objectclass=*)"
  34. #define SYLDAP_SEARCHBASE_V2 "cn=config"
  35. #define SYLDAP_SEARCHBASE_V3 ""
  36. #define SYLDAP_V2_TEST_ATTR "database"
  37. #define SYLDAP_V3_TEST_ATTR "namingcontexts"
  38. /**
  39. * Attempt to discover the base DN for a server using LDAP version 3.
  40. * \param ld LDAP handle for a connected server.
  41. * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
  42. * \return List of Base DN's, or NULL if could not read. List should be
  43. * g_free() when done.
  44. */
  45. static GList *ldaputil_test_v3( LDAP *ld, gint tov, gint *errcode ) {
  46. GList *baseDN = NULL;
  47. gint rc, i;
  48. LDAPMessage *result = NULL, *e;
  49. gchar *attribs[2];
  50. BerElement *ber;
  51. gchar *attribute;
  52. struct berval **vals;
  53. struct timeval timeout;
  54. /* Set timeout */
  55. timeout.tv_usec = 0L;
  56. if( tov > 0 ) {
  57. timeout.tv_sec = tov;
  58. }
  59. else {
  60. timeout.tv_sec = 30L;
  61. }
  62. /* Test for LDAP version 3 */
  63. attribs[0] = SYLDAP_V3_TEST_ATTR;
  64. attribs[1] = NULL;
  65. rc = ldap_search_ext_s(
  66. ld, SYLDAP_SEARCHBASE_V3, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER,
  67. attribs, 0, NULL, NULL, &timeout, 0, &result );
  68. if( rc == LDAP_SUCCESS ) {
  69. /* Process entries */
  70. for( e = ldap_first_entry( ld, result );
  71. e != NULL;
  72. e = ldap_next_entry( ld, e ) )
  73. {
  74. /* Process attributes */
  75. for( attribute = ldap_first_attribute( ld, e, &ber );
  76. attribute != NULL;
  77. attribute = ldap_next_attribute( ld, e, ber ) )
  78. {
  79. if( strcasecmp(
  80. attribute, SYLDAP_V3_TEST_ATTR ) == 0 )
  81. {
  82. vals = ldap_get_values_len( ld, e, attribute );
  83. if( vals != NULL ) {
  84. for( i = 0; vals[i] != NULL; i++ ) {
  85. baseDN = g_list_append(
  86. baseDN, g_strndup( vals[i]->bv_val, vals[i]->bv_len ) );
  87. }
  88. }
  89. ldap_value_free_len( vals );
  90. }
  91. ldap_memfree( attribute );
  92. }
  93. if( ber != NULL ) {
  94. ber_free( ber, 0 );
  95. }
  96. ber = NULL;
  97. }
  98. }
  99. if (errcode)
  100. *errcode = rc;
  101. if (result)
  102. ldap_msgfree( result );
  103. return baseDN;
  104. }
  105. /**
  106. * Attempt to discover the base DN for a server using LDAP version 2.
  107. * \param ld LDAP handle for a connected server.
  108. * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
  109. * \return List of Base DN's, or NULL if could not read. List should be
  110. * g_free() when done.
  111. */
  112. static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
  113. GList *baseDN = NULL;
  114. gint rc, i;
  115. LDAPMessage *result = NULL, *e;
  116. gchar *attribs[1];
  117. BerElement *ber;
  118. gchar *attribute;
  119. struct berval **vals;
  120. struct timeval timeout;
  121. /* Set timeout */
  122. timeout.tv_usec = 0L;
  123. if( tov > 0 ) {
  124. timeout.tv_sec = tov;
  125. }
  126. else {
  127. timeout.tv_sec = 30L;
  128. }
  129. attribs[0] = NULL;
  130. rc = ldap_search_ext_s(
  131. ld, SYLDAP_SEARCHBASE_V2, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER,
  132. attribs, 0, NULL, NULL, &timeout, 0, &result );
  133. if( rc == LDAP_SUCCESS ) {
  134. /* Process entries */
  135. for( e = ldap_first_entry( ld, result );
  136. e != NULL;
  137. e = ldap_next_entry( ld, e ) )
  138. {
  139. /* Process attributes */
  140. for( attribute = ldap_first_attribute( ld, e, &ber );
  141. attribute != NULL;
  142. attribute = ldap_next_attribute( ld, e, ber ) )
  143. {
  144. if( strcasecmp(
  145. attribute,
  146. SYLDAP_V2_TEST_ATTR ) == 0 ) {
  147. vals = ldap_get_values_len( ld, e, attribute );
  148. if( vals != NULL ) {
  149. for( i = 0; vals[i] != NULL; i++ ) {
  150. char *ch, *tmp;
  151. /*
  152. * Strip the 'ldb:' from the
  153. * front of the value.
  154. */
  155. tmp = g_strndup( vals[i]->bv_val, vals[i]->bv_len);
  156. ch = ( char * ) strchr( tmp, ':' );
  157. if( ch ) {
  158. gchar *bn = g_strdup( ++ch );
  159. g_strchomp( bn );
  160. g_strchug( bn );
  161. baseDN = g_list_append(
  162. baseDN, g_strdup( bn ) );
  163. g_free( bn );
  164. }
  165. g_free(tmp);
  166. }
  167. }
  168. ldap_value_free_len( vals );
  169. }
  170. ldap_memfree( attribute );
  171. }
  172. if( ber != NULL ) {
  173. ber_free( ber, 0 );
  174. }
  175. ber = NULL;
  176. }
  177. }
  178. if (result)
  179. ldap_msgfree( result );
  180. return baseDN;
  181. }
  182. int claws_ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
  183. {
  184. struct berval cred;
  185. if ( passwd != NULL ) {
  186. cred.bv_val = (char *) passwd;
  187. cred.bv_len = strlen( passwd );
  188. } else {
  189. cred.bv_val = "";
  190. cred.bv_len = 0;
  191. }
  192. debug_print("binding: DN->%s\n", dn?dn:"null");
  193. return ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, &cred,
  194. NULL, NULL, NULL );
  195. }
  196. /* from ldapsrc.c */
  197. void ldapsrv_set_options (gint secs, LDAP *ld);
  198. /**
  199. * Attempt to discover the base DN for the server.
  200. * \param host Host name.
  201. * \param port Port number.
  202. * \param bindDN Bind DN (optional).
  203. * \param bindPW Bind PW (optional).
  204. * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
  205. * \return List of Base DN's, or NULL if could not read. This list should be
  206. * g_free() when done.
  207. */
  208. GList *ldaputil_read_basedn(
  209. const gchar *host, const gint port, const gchar *bindDN,
  210. const gchar *bindPW, const gint tov, int ssl, int tls )
  211. {
  212. GList *baseDN = NULL;
  213. LDAP *ld = NULL;
  214. gint rc;
  215. gchar *uri = NULL;
  216. gint version;
  217. if( host == NULL ) return baseDN;
  218. if( port < 1 ) return baseDN;
  219. /* Connect to server. */
  220. ldapsrv_set_options (tov, NULL);
  221. uri = g_strdup_printf("ldap%s://%s:%d",
  222. ssl?"s":"",
  223. host, port);
  224. debug_print("URI: %s\n", uri);
  225. rc = ldap_initialize(&ld, uri);
  226. g_free(uri);
  227. if( ld == NULL ) {
  228. return baseDN;
  229. }
  230. if ((bindDN && *bindDN)
  231. #ifdef USE_LDAP_TLS
  232. || (tls && !ssl)
  233. #endif
  234. ) {
  235. version = LDAP_VERSION3;
  236. rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
  237. }
  238. #ifdef USE_LDAP_TLS
  239. if( tls && !ssl ) {
  240. /* Handle TLS */
  241. if( rc != LDAP_OPT_SUCCESS ) {
  242. ldap_unbind_ext( ld, NULL, NULL );
  243. return baseDN;
  244. }
  245. rc = ldap_start_tls_s( ld, NULL, NULL );
  246. if (rc != 0) {
  247. ldap_unbind_ext( ld, NULL, NULL );
  248. return baseDN;
  249. }
  250. }
  251. #endif
  252. /* Bind to the server, if required */
  253. if( bindDN ) {
  254. if( *bindDN != '\0' ) {
  255. rc = claws_ldap_simple_bind_s( ld, bindDN, bindPW );
  256. if( rc != LDAP_SUCCESS ) {
  257. g_printerr("LDAP: %s\n", ldap_err2string(rc));
  258. ldap_unbind_ext( ld, NULL, NULL );
  259. return baseDN;
  260. }
  261. }
  262. }
  263. /* Test for LDAP version 3 */
  264. baseDN = ldaputil_test_v3( ld, tov, &rc );
  265. if (baseDN) {
  266. debug_print("Using LDAP v3\n");
  267. }
  268. if( baseDN == NULL && !LDAP_API_ERROR(rc) ) {
  269. baseDN = ldaputil_test_v2( ld, tov );
  270. if (baseDN) {
  271. debug_print("Using LDAP v2\n");
  272. }
  273. }
  274. if (ld && !LDAP_API_ERROR(rc))
  275. ldap_unbind_ext( ld, NULL, NULL );
  276. return baseDN;
  277. }
  278. /**
  279. * Attempt to connect to the server.
  280. * Enter:
  281. * \param host Host name.
  282. * \param port Port number.
  283. * \return <i>TRUE</i> if connected successfully.
  284. */
  285. gboolean ldaputil_test_connect( const gchar *host, const gint port, int ssl, int tls, int secs ) {
  286. gboolean retVal = FALSE;
  287. LDAP *ld;
  288. #ifdef USE_LDAP_TLS
  289. gint rc;
  290. gint version;
  291. #endif
  292. gchar *uri = NULL;
  293. if( host == NULL ) return retVal;
  294. if( port < 1 ) return retVal;
  295. ldapsrv_set_options (secs, NULL);
  296. uri = g_strdup_printf("ldap%s://%s:%d",
  297. ssl?"s":"",
  298. host, port);
  299. debug_print("URI: %s\n", uri);
  300. ldap_initialize(&ld, uri);
  301. g_free(uri);
  302. if (ld == NULL)
  303. return FALSE;
  304. #ifdef USE_LDAP_TLS
  305. if (ssl) {
  306. GList *dummy = ldaputil_test_v3( ld, secs, &rc );
  307. if (dummy)
  308. g_list_free(dummy);
  309. if (LDAP_API_ERROR(rc))
  310. return FALSE;
  311. }
  312. if( tls && !ssl ) {
  313. /* Handle TLS */
  314. version = LDAP_VERSION3;
  315. rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
  316. if( rc != LDAP_OPT_SUCCESS ) {
  317. ldap_unbind_ext( ld, NULL, NULL );
  318. return FALSE;
  319. }
  320. rc = ldap_start_tls_s( ld, NULL, NULL );
  321. if (rc != 0) {
  322. ldap_unbind_ext( ld, NULL, NULL );
  323. return FALSE;
  324. }
  325. }
  326. #endif
  327. if( ld != NULL ) {
  328. ldap_unbind_ext( ld, NULL, NULL );
  329. debug_print("ld != NULL\n");
  330. retVal = TRUE;
  331. }
  332. return retVal;
  333. }
  334. /**
  335. * Test whether LDAP libraries installed.
  336. * Return: TRUE if library available.
  337. */
  338. gboolean ldaputil_test_ldap_lib( void ) {
  339. return TRUE;
  340. }
  341. #endif /* USE_LDAP */
  342. /*
  343. * End of Source.
  344. */