ldaputil.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
  3. * Copyright (C) 2003-2006 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 2 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, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  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. #define SYLDAP_TEST_FILTER "(objectclass=*)"
  33. #define SYLDAP_SEARCHBASE_V2 "cn=config"
  34. #define SYLDAP_SEARCHBASE_V3 ""
  35. #define SYLDAP_V2_TEST_ATTR "database"
  36. #define SYLDAP_V3_TEST_ATTR "namingcontexts"
  37. /**
  38. * Attempt to discover the base DN for a server using LDAP version 3.
  39. * \param ld LDAP handle for a connected server.
  40. * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
  41. * \return List of Base DN's, or NULL if could not read. List should be
  42. * g_free() when done.
  43. */
  44. static GList *ldaputil_test_v3( LDAP *ld, gint tov, gint *errcode ) {
  45. GList *baseDN = NULL;
  46. gint rc, i;
  47. LDAPMessage *result = NULL, *e;
  48. gchar *attribs[2];
  49. BerElement *ber;
  50. gchar *attribute;
  51. gchar **vals;
  52. struct timeval timeout;
  53. /* Set timeout */
  54. timeout.tv_usec = 0L;
  55. if( tov > 0 ) {
  56. timeout.tv_sec = tov;
  57. }
  58. else {
  59. timeout.tv_sec = 30L;
  60. }
  61. /* Test for LDAP version 3 */
  62. attribs[0] = SYLDAP_V3_TEST_ATTR;
  63. attribs[1] = NULL;
  64. rc = ldap_search_ext_s(
  65. ld, SYLDAP_SEARCHBASE_V3, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER,
  66. attribs, 0, NULL, NULL, &timeout, 0, &result );
  67. if( rc == LDAP_SUCCESS ) {
  68. /* Process entries */
  69. for( e = ldap_first_entry( ld, result );
  70. e != NULL;
  71. e = ldap_next_entry( ld, e ) )
  72. {
  73. /* Process attributes */
  74. for( attribute = ldap_first_attribute( ld, e, &ber );
  75. attribute != NULL;
  76. attribute = ldap_next_attribute( ld, e, ber ) )
  77. {
  78. if( strcasecmp(
  79. attribute, SYLDAP_V3_TEST_ATTR ) == 0 )
  80. {
  81. vals = ldap_get_values( ld, e, attribute );
  82. if( vals != NULL ) {
  83. for( i = 0; vals[i] != NULL; i++ ) {
  84. baseDN = g_list_append(
  85. baseDN, g_strdup( vals[i] ) );
  86. }
  87. }
  88. ldap_value_free( vals );
  89. }
  90. ldap_memfree( attribute );
  91. }
  92. if( ber != NULL ) {
  93. ber_free( ber, 0 );
  94. }
  95. ber = NULL;
  96. }
  97. }
  98. if (errcode)
  99. *errcode = rc;
  100. if (result)
  101. ldap_msgfree( result );
  102. return baseDN;
  103. }
  104. /**
  105. * Attempt to discover the base DN for a server using LDAP version 2.
  106. * \param ld LDAP handle for a connected server.
  107. * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
  108. * \return List of Base DN's, or NULL if could not read. List should be
  109. * g_free() when done.
  110. */
  111. static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
  112. GList *baseDN = NULL;
  113. gint rc, i;
  114. LDAPMessage *result = NULL, *e;
  115. gchar *attribs[1];
  116. BerElement *ber;
  117. gchar *attribute;
  118. gchar **vals;
  119. struct timeval timeout;
  120. /* Set timeout */
  121. timeout.tv_usec = 0L;
  122. if( tov > 0 ) {
  123. timeout.tv_sec = tov;
  124. }
  125. else {
  126. timeout.tv_sec = 30L;
  127. }
  128. attribs[0] = NULL;
  129. rc = ldap_search_ext_s(
  130. ld, SYLDAP_SEARCHBASE_V2, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER,
  131. attribs, 0, NULL, NULL, &timeout, 0, &result );
  132. if( rc == LDAP_SUCCESS ) {
  133. /* Process entries */
  134. for( e = ldap_first_entry( ld, result );
  135. e != NULL;
  136. e = ldap_next_entry( ld, e ) )
  137. {
  138. /* Process attributes */
  139. for( attribute = ldap_first_attribute( ld, e, &ber );
  140. attribute != NULL;
  141. attribute = ldap_next_attribute( ld, e, ber ) )
  142. {
  143. if( strcasecmp(
  144. attribute,
  145. SYLDAP_V2_TEST_ATTR ) == 0 ) {
  146. vals = ldap_get_values( ld, e, attribute );
  147. if( vals != NULL ) {
  148. for( i = 0; vals[i] != NULL; i++ ) {
  149. char *ch;
  150. /*
  151. * Strip the 'ldb:' from the
  152. * front of the value.
  153. */
  154. ch = ( char * ) strchr( vals[i], ':' );
  155. if( ch ) {
  156. gchar *bn = g_strdup( ++ch );
  157. g_strchomp( bn );
  158. g_strchug( bn );
  159. baseDN = g_list_append(
  160. baseDN, g_strdup( bn ) );
  161. g_free( bn );
  162. }
  163. }
  164. }
  165. ldap_value_free( vals );
  166. }
  167. ldap_memfree( attribute );
  168. }
  169. if( ber != NULL ) {
  170. ber_free( ber, 0 );
  171. }
  172. ber = NULL;
  173. }
  174. }
  175. if (result)
  176. ldap_msgfree( result );
  177. return baseDN;
  178. }
  179. /**
  180. * Attempt to discover the base DN for the server.
  181. * \param host Host name.
  182. * \param port Port number.
  183. * \param bindDN Bind DN (optional).
  184. * \param bindPW Bind PW (optional).
  185. * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
  186. * \return List of Base DN's, or NULL if could not read. This list should be
  187. * g_free() when done.
  188. */
  189. GList *ldaputil_read_basedn(
  190. const gchar *host, const gint port, const gchar *bindDN,
  191. const gchar *bindPW, const gint tov, int ssl, int tls )
  192. {
  193. GList *baseDN = NULL;
  194. LDAP *ld = NULL;
  195. gint rc;
  196. #ifdef USE_LDAP_TLS
  197. gint version;
  198. #endif
  199. if( host == NULL ) return baseDN;
  200. if( port < 1 ) return baseDN;
  201. /* Connect to server. */
  202. if (!ssl) {
  203. ld = ldap_init( host, port );
  204. } else {
  205. gchar *uri = g_strdup_printf("ldaps://%s:%d",
  206. host, port);
  207. rc = ldap_initialize(&ld, uri);
  208. g_free(uri);
  209. }
  210. if( ld == NULL ) {
  211. return baseDN;
  212. }
  213. #ifdef USE_LDAP_TLS
  214. if( tls && !ssl ) {
  215. /* Handle TLS */
  216. version = LDAP_VERSION3;
  217. rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
  218. if( rc != LDAP_OPT_SUCCESS ) {
  219. ldap_unbind( ld );
  220. return baseDN;
  221. }
  222. rc = ldap_start_tls_s( ld, NULL, NULL );
  223. if (rc != 0) {
  224. ldap_unbind( ld );
  225. return baseDN;
  226. }
  227. }
  228. #endif
  229. /* Bind to the server, if required */
  230. if( bindDN ) {
  231. if( *bindDN != '\0' ) {
  232. rc = ldap_simple_bind_s( ld, bindDN, bindPW );
  233. if( rc != LDAP_SUCCESS ) {
  234. ldap_unbind( ld );
  235. return baseDN;
  236. }
  237. }
  238. }
  239. /* Test for LDAP version 3 */
  240. baseDN = ldaputil_test_v3( ld, tov, &rc );
  241. if( baseDN == NULL && !LDAP_API_ERROR(rc) ) {
  242. baseDN = ldaputil_test_v2( ld, tov );
  243. }
  244. if (ld && !LDAP_API_ERROR(rc))
  245. ldap_unbind( ld );
  246. return baseDN;
  247. }
  248. /**
  249. * Attempt to connect to the server.
  250. * Enter:
  251. * \param host Host name.
  252. * \param port Port number.
  253. * \return <i>TRUE</i> if connected successfully.
  254. */
  255. gboolean ldaputil_test_connect( const gchar *host, const gint port, int ssl, int tls ) {
  256. gboolean retVal = FALSE;
  257. LDAP *ld;
  258. #ifdef USE_LDAP_TLS
  259. gint rc;
  260. gint version;
  261. #endif
  262. if( host == NULL ) return retVal;
  263. if( port < 1 ) return retVal;
  264. if (!ssl) {
  265. ld = ldap_open( host, port );
  266. } else {
  267. gchar *uri = g_strdup_printf("ldaps://%s:%d",
  268. host, port);
  269. ldap_initialize(&ld, uri);
  270. g_free(uri);
  271. }
  272. if (ld == NULL)
  273. return FALSE;
  274. #ifdef USE_LDAP_TLS
  275. if (ssl) {
  276. GList *dummy = ldaputil_test_v3( ld, 10, &rc );
  277. if (dummy)
  278. g_list_free(dummy);
  279. if (LDAP_API_ERROR(rc))
  280. return FALSE;
  281. }
  282. if( tls && !ssl ) {
  283. /* Handle TLS */
  284. version = LDAP_VERSION3;
  285. rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
  286. if( rc != LDAP_OPT_SUCCESS ) {
  287. ldap_unbind( ld );
  288. return FALSE;
  289. }
  290. rc = ldap_start_tls_s( ld, NULL, NULL );
  291. if (rc != 0) {
  292. ldap_unbind( ld );
  293. return FALSE;
  294. }
  295. }
  296. #endif
  297. if( ld != NULL ) {
  298. ldap_unbind( ld );
  299. retVal = TRUE;
  300. }
  301. return retVal;
  302. }
  303. /**
  304. * Test whether LDAP libraries installed.
  305. * Return: TRUE if library available.
  306. */
  307. gboolean ldaputil_test_ldap_lib( void ) {
  308. return TRUE;
  309. }
  310. #endif /* USE_LDAP */
  311. /*
  312. * End of Source.
  313. */