vtable.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include "ldaputili.h"
  42. #include <ldap.h>
  43. static LDAPUVTable_t ldapu_VTable = {0};
  44. /* Replace ldapu_VTable. Subsequently, ldaputil will call the
  45. functions in 'from' (not the LDAP API) to access the directory.
  46. */
  47. void
  48. ldapu_VTable_set (LDAPUVTable_t* from)
  49. {
  50. if (from) {
  51. memcpy (&ldapu_VTable, from, sizeof(LDAPUVTable_t));
  52. }
  53. }
  54. int
  55. ldapu_set_option( LDAP *ld, int option, void *optdata )
  56. {
  57. if (ldapu_VTable.ldapuV_set_option) {
  58. return ldapu_VTable.ldapuV_set_option (ld, option, optdata);
  59. }
  60. return LDAP_LOCAL_ERROR;
  61. }
  62. int
  63. ldapu_simple_bind_s( LDAP *ld, const char *who, const char *passwd )
  64. {
  65. if (ldapu_VTable.ldapuV_simple_bind_s) {
  66. return ldapu_VTable.ldapuV_simple_bind_s (ld, who, passwd);
  67. }
  68. return LDAP_LOCAL_ERROR;
  69. }
  70. int
  71. ldapu_unbind( LDAP *ld )
  72. {
  73. if (ldapu_VTable.ldapuV_unbind) {
  74. return ldapu_VTable.ldapuV_unbind (ld);
  75. }
  76. return LDAP_LOCAL_ERROR;
  77. }
  78. int
  79. ldapu_search_s( LDAP *ld, const char *base, int scope,
  80. const char *filter, char **attrs, int attrsonly, LDAPMessage **res )
  81. {
  82. if (ldapu_VTable.ldapuV_search_s) {
  83. return ldapu_VTable.ldapuV_search_s (ld, base, scope, filter, attrs, attrsonly, res);
  84. }
  85. return LDAP_LOCAL_ERROR;
  86. }
  87. int
  88. ldapu_count_entries( LDAP *ld, LDAPMessage *chain )
  89. {
  90. if (ldapu_VTable.ldapuV_count_entries) {
  91. return ldapu_VTable.ldapuV_count_entries (ld, chain);
  92. }
  93. return 0;
  94. }
  95. LDAPMessage*
  96. ldapu_first_entry( LDAP *ld, LDAPMessage *chain )
  97. {
  98. if (ldapu_VTable.ldapuV_first_entry) {
  99. return ldapu_VTable.ldapuV_first_entry (ld, chain);
  100. }
  101. return NULL;
  102. }
  103. LDAPMessage*
  104. ldapu_next_entry( LDAP *ld, LDAPMessage *entry )
  105. {
  106. if (ldapu_VTable.ldapuV_next_entry) {
  107. return ldapu_VTable.ldapuV_next_entry (ld, entry);
  108. }
  109. return NULL;
  110. }
  111. int
  112. ldapu_msgfree( LDAP* ld, LDAPMessage *chain )
  113. {
  114. if (ldapu_VTable.ldapuV_msgfree) {
  115. return ldapu_VTable.ldapuV_msgfree (ld, chain);
  116. }
  117. return LDAP_SUCCESS;
  118. }
  119. char*
  120. ldapu_get_dn( LDAP *ld, LDAPMessage *entry )
  121. {
  122. if (ldapu_VTable.ldapuV_get_dn) {
  123. return ldapu_VTable.ldapuV_get_dn (ld, entry);
  124. }
  125. return NULL;
  126. }
  127. void
  128. ldapu_memfree( LDAP* ld, void *p )
  129. {
  130. if (ldapu_VTable.ldapuV_memfree) {
  131. ldapu_VTable.ldapuV_memfree (ld, p);
  132. }
  133. }
  134. char*
  135. ldapu_first_attribute( LDAP *ld, LDAPMessage *entry, BerElement **ber )
  136. {
  137. if (ldapu_VTable.ldapuV_first_attribute) {
  138. return ldapu_VTable.ldapuV_first_attribute (ld, entry, ber);
  139. }
  140. return NULL;
  141. }
  142. char*
  143. ldapu_next_attribute( LDAP *ld, LDAPMessage *entry, BerElement *ber )
  144. {
  145. if (ldapu_VTable.ldapuV_next_attribute) {
  146. return ldapu_VTable.ldapuV_next_attribute (ld, entry, ber);
  147. }
  148. return NULL;
  149. }
  150. void
  151. ldapu_ber_free( LDAP* ld, BerElement *ber, int freebuf )
  152. {
  153. if (ldapu_VTable.ldapuV_ber_free) {
  154. ldapu_VTable.ldapuV_ber_free (ld, ber, freebuf);
  155. }
  156. }
  157. char**
  158. ldapu_get_values( LDAP *ld, LDAPMessage *entry, const char *desc )
  159. {
  160. if (ldapu_VTable.ldapuV_get_values) {
  161. return ldapu_VTable.ldapuV_get_values (ld, entry, desc);
  162. } else if (!ldapu_VTable.ldapuV_value_free
  163. && ldapu_VTable.ldapuV_get_values_len) {
  164. auto struct berval** bvals =
  165. ldapu_VTable.ldapuV_get_values_len (ld, entry, desc);
  166. if (bvals) {
  167. auto char** vals = (char**)
  168. ldapu_malloc ((ldap_count_values_len (bvals) + 1)
  169. * sizeof(char*));
  170. if (vals) {
  171. auto char** val;
  172. auto struct berval** bval;
  173. for (val = vals, bval = bvals; *bval; ++val, ++bval) {
  174. auto const size_t len = (*bval)->bv_len;
  175. *val = (char*) ldapu_malloc (len + 1);
  176. memcpy (*val, (*bval)->bv_val, len);
  177. (*val)[len] = '\0';
  178. }
  179. *val = NULL;
  180. ldapu_value_free_len(ld, bvals);
  181. return vals;
  182. }
  183. }
  184. ldapu_value_free_len(ld, bvals);
  185. }
  186. return NULL;
  187. }
  188. void
  189. ldapu_value_free( LDAP *ld, char **vals )
  190. {
  191. if (ldapu_VTable.ldapuV_value_free) {
  192. ldapu_VTable.ldapuV_value_free (ld, vals);
  193. } else if (!ldapu_VTable.ldapuV_get_values && vals) {
  194. auto char** val;
  195. for (val = vals; *val; ++val) {
  196. free (*val);
  197. }
  198. free (vals);
  199. }
  200. }
  201. struct berval**
  202. ldapu_get_values_len( LDAP *ld, LDAPMessage *entry, const char *desc )
  203. {
  204. if (ldapu_VTable.ldapuV_get_values_len) {
  205. return ldapu_VTable.ldapuV_get_values_len (ld, entry, desc);
  206. }
  207. return NULL;
  208. }
  209. void
  210. ldapu_value_free_len( LDAP *ld, struct berval **vals )
  211. {
  212. if (ldapu_VTable.ldapuV_value_free_len) {
  213. ldapu_VTable.ldapuV_value_free_len (ld, vals);
  214. } else if (!ldapu_VTable.ldapuV_get_values_len && vals) {
  215. auto struct berval** val;
  216. for (val = vals; *val; ++val) {
  217. free (*val);
  218. }
  219. free (vals);
  220. }
  221. }