vtable.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #include "ldaputili.h"
  39. #include <ldap.h>
  40. #ifdef USE_LDAP_SSL
  41. #include <ldap_ssl.h>
  42. #endif
  43. #if defined( _WINDOWS ) && ! defined( _WIN32 )
  44. /* On 16-bit WINDOWS platforms, it's erroneous to call LDAP API functions
  45. * via a function pointer, since they are not declared LDAP_CALLBACK.
  46. * So, we define the following functions, which are LDAP_CALLBACK, and
  47. * simply delegate to their counterparts in the LDAP API.
  48. */
  49. #ifdef USE_LDAP_SSL
  50. static LDAP_CALL LDAP_CALLBACK LDAP*
  51. ldapuVd_ssl_init( const char *host, int port, int encrypted )
  52. {
  53. return ldapssl_init (host, port, encrypted);
  54. }
  55. #else
  56. static LDAP_CALL LDAP_CALLBACK LDAP*
  57. ldapuVd_init ( const char *host, int port )
  58. {
  59. return ldap_init (host, port);
  60. }
  61. #endif
  62. static LDAP_CALL LDAP_CALLBACK int
  63. ldapuVd_set_option( LDAP *ld, int opt, void *val )
  64. {
  65. return ldap_set_option (ld, opt, val);
  66. }
  67. static LDAP_CALL LDAP_CALLBACK int
  68. ldapuVd_simple_bind_s( LDAP* ld, const char *username, const char *passwd )
  69. {
  70. return ldap_simple_bind_s (ld, username, passwd);
  71. }
  72. static LDAP_CALL LDAP_CALLBACK int
  73. ldapuVd_unbind( LDAP *ld )
  74. {
  75. return ldap_unbind (ld);
  76. }
  77. static LDAP_CALL LDAP_CALLBACK int
  78. ldapuVd_search_s( LDAP* ld, const char* baseDN, int scope, const char* filter,
  79. char** attrs, int attrsonly, LDAPMessage** result )
  80. {
  81. return ldap_search_s (ld, baseDN, scope, filter, attrs, attrsonly, result);
  82. }
  83. static LDAP_CALL LDAP_CALLBACK int
  84. ldapuVd_count_entries( LDAP* ld, LDAPMessage* msg )
  85. {
  86. return ldap_count_entries (ld, msg);
  87. }
  88. static LDAP_CALL LDAP_CALLBACK LDAPMessage*
  89. ldapuVd_first_entry( LDAP* ld, LDAPMessage* msg )
  90. {
  91. return ldap_first_entry (ld, msg);
  92. }
  93. static LDAP_CALL LDAP_CALLBACK LDAPMessage*
  94. ldapuVd_next_entry( LDAP* ld, LDAPMessage* entry )
  95. {
  96. return ldap_next_entry(ld, entry);
  97. }
  98. static LDAP_CALL LDAP_CALLBACK char*
  99. ldapuVd_get_dn( LDAP* ld, LDAPMessage* entry )
  100. {
  101. return ldap_get_dn (ld, entry);
  102. }
  103. static LDAP_CALL LDAP_CALLBACK char*
  104. ldapuVd_first_attribute( LDAP* ld, LDAPMessage* entry, BerElement** iter )
  105. {
  106. return ldap_first_attribute (ld, entry, iter);
  107. }
  108. static LDAP_CALL LDAP_CALLBACK char*
  109. ldapuVd_next_attribute( LDAP* ld, LDAPMessage* entry, BerElement* iter)
  110. {
  111. return ldap_next_attribute (ld, entry, iter);
  112. }
  113. static LDAP_CALL LDAP_CALLBACK char**
  114. ldapuVd_get_values( LDAP *ld, LDAPMessage *entry, const char *desc )
  115. {
  116. return ldap_get_values (ld, entry, desc);
  117. }
  118. static LDAP_CALL LDAP_CALLBACK struct berval**
  119. ldapuVd_get_values_len( LDAP *ld, LDAPMessage *entry, const char *desc )
  120. {
  121. return ldap_get_values_len (ld, entry, desc);
  122. }
  123. #else
  124. /* On other platforms, an LDAP API function can be called via a pointer. */
  125. #ifdef USE_LDAP_SSL
  126. #define ldapuVd_ssl_init ldapssl_init
  127. #else
  128. #define ldapuVd_init ldap_init
  129. #endif
  130. #define ldapuVd_set_option ldap_set_option
  131. #define ldapuVd_simple_bind_s ldap_simple_bind_s
  132. #define ldapuVd_unbind ldap_unbind
  133. #define ldapuVd_set_option ldap_set_option
  134. #define ldapuVd_simple_bind_s ldap_simple_bind_s
  135. #define ldapuVd_unbind ldap_unbind
  136. #define ldapuVd_search_s ldap_search_s
  137. #define ldapuVd_count_entries ldap_count_entries
  138. #define ldapuVd_first_entry ldap_first_entry
  139. #define ldapuVd_next_entry ldap_next_entry
  140. #define ldapuVd_get_dn ldap_get_dn
  141. #define ldapuVd_first_attribute ldap_first_attribute
  142. #define ldapuVd_next_attribute ldap_next_attribute
  143. #define ldapuVd_get_values ldap_get_values
  144. #define ldapuVd_get_values_len ldap_get_values_len
  145. #endif
  146. /* Several functions in the standard LDAP API have no LDAP* parameter,
  147. but all the VTable functions do. Here are some little functions that
  148. make up the difference, by ignoring their LDAP* parameter:
  149. */
  150. static int LDAP_CALL LDAP_CALLBACK
  151. ldapuVd_msgfree( LDAP *ld, LDAPMessage *chain )
  152. {
  153. return ldap_msgfree (chain);
  154. }
  155. static void LDAP_CALL LDAP_CALLBACK
  156. ldapuVd_memfree( LDAP *ld, void *dn )
  157. {
  158. ldap_memfree (dn);
  159. }
  160. static void LDAP_CALL LDAP_CALLBACK
  161. ldapuVd_ber_free( LDAP *ld, BerElement *ber, int freebuf )
  162. {
  163. ldap_ber_free (ber, freebuf);
  164. }
  165. static void LDAP_CALL LDAP_CALLBACK
  166. ldapuVd_value_free( LDAP *ld, char **vals )
  167. {
  168. ldap_value_free (vals);
  169. }
  170. static void LDAP_CALL LDAP_CALLBACK
  171. ldapuVd_value_free_len( LDAP *ld, struct berval **vals )
  172. {
  173. ldap_value_free_len (vals);
  174. }
  175. static LDAPUVTable_t ldapu_VTable = {
  176. /* By default, the VTable points to the standard LDAP API. */
  177. #ifdef USE_LDAP_SSL
  178. ldapuVd_ssl_init,
  179. #else
  180. ldapuVd_init,
  181. #endif
  182. ldapuVd_set_option,
  183. ldapuVd_simple_bind_s,
  184. ldapuVd_unbind,
  185. ldapuVd_search_s,
  186. ldapuVd_count_entries,
  187. ldapuVd_first_entry,
  188. ldapuVd_next_entry,
  189. ldapuVd_msgfree,
  190. ldapuVd_get_dn,
  191. ldapuVd_memfree,
  192. ldapuVd_first_attribute,
  193. ldapuVd_next_attribute,
  194. ldapuVd_ber_free,
  195. ldapuVd_get_values,
  196. ldapuVd_value_free,
  197. ldapuVd_get_values_len,
  198. ldapuVd_value_free_len
  199. };
  200. /* Replace ldapu_VTable. Subsequently, ldaputil will call the
  201. functions in 'from' (not the LDAP API) to access the directory.
  202. */
  203. void
  204. ldapu_VTable_set (LDAPUVTable_t* from)
  205. {
  206. if (from) {
  207. memcpy (&ldapu_VTable, from, sizeof(LDAPUVTable_t));
  208. }
  209. }
  210. #ifdef USE_LDAP_SSL
  211. LDAP*
  212. ldapu_ssl_init( const char *defhost, int defport, int defsecure )
  213. {
  214. if (ldapu_VTable.ldapuV_ssl_init) {
  215. return ldapu_VTable.ldapuV_ssl_init (defhost, defport, defsecure);
  216. }
  217. return NULL;
  218. }
  219. #else
  220. LDAP*
  221. ldapu_init( const char *defhost, int defport )
  222. {
  223. if (ldapu_VTable.ldapuV_init) {
  224. return ldapu_VTable.ldapuV_init (defhost, defport);
  225. }
  226. return NULL;
  227. }
  228. #endif
  229. int
  230. ldapu_set_option( LDAP *ld, int option, void *optdata )
  231. {
  232. if (ldapu_VTable.ldapuV_set_option) {
  233. return ldapu_VTable.ldapuV_set_option (ld, option, optdata);
  234. }
  235. return LDAP_LOCAL_ERROR;
  236. }
  237. int
  238. ldapu_simple_bind_s( LDAP *ld, const char *who, const char *passwd )
  239. {
  240. if (ldapu_VTable.ldapuV_simple_bind_s) {
  241. return ldapu_VTable.ldapuV_simple_bind_s (ld, who, passwd);
  242. }
  243. return LDAP_LOCAL_ERROR;
  244. }
  245. int
  246. ldapu_unbind( LDAP *ld )
  247. {
  248. if (ldapu_VTable.ldapuV_unbind) {
  249. return ldapu_VTable.ldapuV_unbind (ld);
  250. }
  251. return LDAP_LOCAL_ERROR;
  252. }
  253. int
  254. ldapu_search_s( LDAP *ld, const char *base, int scope,
  255. const char *filter, char **attrs, int attrsonly, LDAPMessage **res )
  256. {
  257. if (ldapu_VTable.ldapuV_search_s) {
  258. return ldapu_VTable.ldapuV_search_s (ld, base, scope, filter, attrs, attrsonly, res);
  259. }
  260. return LDAP_LOCAL_ERROR;
  261. }
  262. int
  263. ldapu_count_entries( LDAP *ld, LDAPMessage *chain )
  264. {
  265. if (ldapu_VTable.ldapuV_count_entries) {
  266. return ldapu_VTable.ldapuV_count_entries (ld, chain);
  267. }
  268. return 0;
  269. }
  270. LDAPMessage*
  271. ldapu_first_entry( LDAP *ld, LDAPMessage *chain )
  272. {
  273. if (ldapu_VTable.ldapuV_first_entry) {
  274. return ldapu_VTable.ldapuV_first_entry (ld, chain);
  275. }
  276. return NULL;
  277. }
  278. LDAPMessage*
  279. ldapu_next_entry( LDAP *ld, LDAPMessage *entry )
  280. {
  281. if (ldapu_VTable.ldapuV_next_entry) {
  282. return ldapu_VTable.ldapuV_next_entry (ld, entry);
  283. }
  284. return NULL;
  285. }
  286. int
  287. ldapu_msgfree( LDAP* ld, LDAPMessage *chain )
  288. {
  289. if (ldapu_VTable.ldapuV_msgfree) {
  290. return ldapu_VTable.ldapuV_msgfree (ld, chain);
  291. }
  292. return LDAP_SUCCESS;
  293. }
  294. char*
  295. ldapu_get_dn( LDAP *ld, LDAPMessage *entry )
  296. {
  297. if (ldapu_VTable.ldapuV_get_dn) {
  298. return ldapu_VTable.ldapuV_get_dn (ld, entry);
  299. }
  300. return NULL;
  301. }
  302. void
  303. ldapu_memfree( LDAP* ld, void *p )
  304. {
  305. if (ldapu_VTable.ldapuV_memfree) {
  306. ldapu_VTable.ldapuV_memfree (ld, p);
  307. }
  308. }
  309. char*
  310. ldapu_first_attribute( LDAP *ld, LDAPMessage *entry, BerElement **ber )
  311. {
  312. if (ldapu_VTable.ldapuV_first_attribute) {
  313. return ldapu_VTable.ldapuV_first_attribute (ld, entry, ber);
  314. }
  315. return NULL;
  316. }
  317. char*
  318. ldapu_next_attribute( LDAP *ld, LDAPMessage *entry, BerElement *ber )
  319. {
  320. if (ldapu_VTable.ldapuV_next_attribute) {
  321. return ldapu_VTable.ldapuV_next_attribute (ld, entry, ber);
  322. }
  323. return NULL;
  324. }
  325. void
  326. ldapu_ber_free( LDAP* ld, BerElement *ber, int freebuf )
  327. {
  328. if (ldapu_VTable.ldapuV_ber_free) {
  329. ldapu_VTable.ldapuV_ber_free (ld, ber, freebuf);
  330. }
  331. }
  332. char**
  333. ldapu_get_values( LDAP *ld, LDAPMessage *entry, const char *desc )
  334. {
  335. if (ldapu_VTable.ldapuV_get_values) {
  336. return ldapu_VTable.ldapuV_get_values (ld, entry, desc);
  337. } else if (!ldapu_VTable.ldapuV_value_free
  338. && ldapu_VTable.ldapuV_get_values_len) {
  339. auto struct berval** bvals =
  340. ldapu_VTable.ldapuV_get_values_len (ld, entry, desc);
  341. if (bvals) {
  342. auto char** vals = (char**)
  343. ldapu_malloc ((ldap_count_values_len (bvals) + 1)
  344. * sizeof(char*));
  345. if (vals) {
  346. auto char** val;
  347. auto struct berval** bval;
  348. for (val = vals, bval = bvals; *bval; ++val, ++bval) {
  349. auto const size_t len = (*bval)->bv_len;
  350. *val = (char*) ldapu_malloc (len + 1);
  351. memcpy (*val, (*bval)->bv_val, len);
  352. (*val)[len] = '\0';
  353. }
  354. *val = NULL;
  355. ldapu_value_free_len(ld, bvals);
  356. return vals;
  357. }
  358. }
  359. ldapu_value_free_len(ld, bvals);
  360. }
  361. return NULL;
  362. }
  363. void
  364. ldapu_value_free( LDAP *ld, char **vals )
  365. {
  366. if (ldapu_VTable.ldapuV_value_free) {
  367. ldapu_VTable.ldapuV_value_free (ld, vals);
  368. } else if (!ldapu_VTable.ldapuV_get_values && vals) {
  369. auto char** val;
  370. for (val = vals; *val; ++val) {
  371. free (*val);
  372. }
  373. free (vals);
  374. }
  375. }
  376. struct berval**
  377. ldapu_get_values_len( LDAP *ld, LDAPMessage *entry, const char *desc )
  378. {
  379. if (ldapu_VTable.ldapuV_get_values_len) {
  380. return ldapu_VTable.ldapuV_get_values_len (ld, entry, desc);
  381. } else if (!ldapu_VTable.ldapuV_value_free_len
  382. && ldapu_VTable.ldapuV_get_values) {
  383. auto char** vals =
  384. ldapu_VTable.ldapuV_get_values (ld, entry, desc);
  385. if (vals) {
  386. auto struct berval** bvals = (struct berval**)
  387. ldapu_malloc ((ldap_count_values (vals) + 1)
  388. * sizeof(struct berval*));
  389. if (bvals) {
  390. auto char** val;
  391. auto struct berval** bval;
  392. for (val = vals, bval = bvals; *val; ++val, ++bval) {
  393. auto const size_t len = strlen(*val);
  394. *bval = (struct berval*) ldapu_malloc (sizeof(struct berval) + len);
  395. (*bval)->bv_len = len;
  396. (*bval)->bv_val = ((char*)(*bval)) + sizeof(struct berval);
  397. memcpy ((*bval)->bv_val, *val, len);
  398. }
  399. *bval = NULL;
  400. return bvals;
  401. }
  402. }
  403. }
  404. return NULL;
  405. }
  406. void
  407. ldapu_value_free_len( LDAP *ld, struct berval **vals )
  408. {
  409. if (ldapu_VTable.ldapuV_value_free_len) {
  410. ldapu_VTable.ldapuV_value_free_len (ld, vals);
  411. } else if (!ldapu_VTable.ldapuV_get_values_len && vals) {
  412. auto struct berval** val;
  413. for (val = vals; *val; ++val) {
  414. free (*val);
  415. }
  416. free (vals);
  417. }
  418. }