init.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 <string.h>
  42. #include <prlink.h>
  43. #include <prio.h>
  44. #include <prprf.h>
  45. /*#include "base/file.h"*/
  46. #include "ldaputil/certmap.h"
  47. /*#include "ldaputil/ldapdb.h"*/
  48. #include "ldaputil/ldaputil.h"
  49. #include "ldaputil/cert.h"
  50. #include "ldaputil/errors.h"
  51. #include "ldaputil/init.h"
  52. #include "slapi-plugin.h"
  53. #ifdef XP_WIN32
  54. #define DLL_SUFFIX ".dll"
  55. #ifndef FILE_PATHSEP
  56. #define FILE_PATHSEP '\\'
  57. #endif
  58. #else
  59. #ifndef FILE_PATHSEP
  60. #define FILE_PATHSEP '/'
  61. #endif
  62. #ifdef HPUX
  63. #ifdef __ia64
  64. #define DLL_SUFFIX ".so"
  65. #else
  66. #define DLL_SUFFIX ".sl"
  67. #endif
  68. #else
  69. #define DLL_SUFFIX ".so"
  70. #endif
  71. #endif
  72. static int load_server_libs (const char *dir)
  73. {
  74. int rv = LDAPU_SUCCESS;
  75. PRDir* ds;
  76. int suffix_len = strlen(DLL_SUFFIX);
  77. if ((ds = PR_OpenDir(dir)) != NULL) {
  78. PRDirEntry *d;
  79. /* Dir exists */
  80. while( (d = PR_ReadDir(ds, PR_SKIP_BOTH)) ) {
  81. PRLibrary *lib = 0;
  82. const char *libname = d->name;
  83. int len = strlen(libname);
  84. int is_lib;
  85. is_lib = (len > suffix_len && !strcmp(libname+len-suffix_len,
  86. DLL_SUFFIX));
  87. if(is_lib) {
  88. char path[1024];
  89. PR_snprintf(path, sizeof(path), "%s%c%s", dir, FILE_PATHSEP, libname);
  90. lib = PR_LoadLibrary(path);
  91. if (!lib) rv = LDAPU_ERR_UNABLE_TO_LOAD_PLUGIN;
  92. }
  93. }
  94. }
  95. else {
  96. /* It's ok if dir doesn't exists */
  97. }
  98. return rv;
  99. }
  100. NSAPI_PUBLIC int ldaputil_init (const char *config_file,
  101. const char *dllname,
  102. const char *serv_root,
  103. const char *serv_type,
  104. const char *serv_id)
  105. {
  106. int rv = LDAPU_SUCCESS;
  107. static int initialized = 0;
  108. /* If already initialized, cleanup the old structures */
  109. if (initialized) ldaputil_exit();
  110. if (config_file && *config_file) {
  111. char dir[1024];
  112. LDAPUCertMapListInfo_t *certmap_list;
  113. LDAPUCertMapInfo_t *certmap_default;
  114. if (serv_root && *serv_root) {
  115. /* Load common libraries */
  116. PR_snprintf(dir, sizeof(dir), "%s%clib%c%s", serv_root, FILE_PATHSEP,
  117. FILE_PATHSEP, "common");
  118. rv = load_server_libs(dir);
  119. if (rv != LDAPU_SUCCESS) return rv;
  120. if (serv_type && *serv_type) {
  121. /* Load server type specific libraries */
  122. sprintf(dir, "%s%clib%c%s", serv_root, FILE_PATHSEP,
  123. FILE_PATHSEP, serv_type);
  124. rv = load_server_libs(dir);
  125. if (rv != LDAPU_SUCCESS) return rv;
  126. if (serv_id && *serv_id) {
  127. /* Load server instance specific libraries */
  128. sprintf(dir, "%s%clib%c%s", serv_root, FILE_PATHSEP,
  129. FILE_PATHSEP, serv_id);
  130. rv = load_server_libs(dir);
  131. if (rv != LDAPU_SUCCESS) return rv;
  132. }
  133. }
  134. }
  135. rv = ldapu_certmap_init (config_file, dllname, &certmap_list,
  136. &certmap_default);
  137. }
  138. initialized = 1;
  139. if (rv != LDAPU_SUCCESS) return rv;
  140. return rv;
  141. }
  142. static LDAPUDispatchVector_t __ldapu_vector = {
  143. ldapu_cert_to_ldap_entry,
  144. ldapu_set_cert_mapfn,
  145. ldapu_get_cert_mapfn,
  146. ldapu_set_cert_searchfn,
  147. ldapu_get_cert_searchfn,
  148. ldapu_set_cert_verifyfn,
  149. ldapu_get_cert_verifyfn,
  150. ldapu_get_cert_subject_dn,
  151. ldapu_get_cert_issuer_dn,
  152. ldapu_get_cert_ava_val,
  153. ldapu_free_cert_ava_val,
  154. ldapu_get_cert_der,
  155. ldapu_issuer_certinfo,
  156. ldapu_certmap_info_attrval,
  157. ldapu_err2string,
  158. ldapu_free_old,
  159. ldapu_malloc,
  160. ldapu_strdup,
  161. ldapu_free
  162. };
  163. #ifdef XP_UNIX
  164. LDAPUDispatchVector_t *__ldapu_table = &__ldapu_vector;
  165. #endif
  166. #if 0
  167. NSAPI_PUBLIC int CertMapDLLInitFn(LDAPUDispatchVector_t **table)
  168. {
  169. *table = &__ldapu_vector;
  170. }
  171. #endif
  172. NSAPI_PUBLIC int CertMapDLLInitFn(LDAPUDispatchVector_t **table)
  173. {
  174. *table = (LDAPUDispatchVector_t *)slapi_ch_malloc(sizeof(LDAPUDispatchVector_t));
  175. if (!*table) return LDAPU_ERR_OUT_OF_MEMORY;
  176. (*table)->f_ldapu_cert_to_ldap_entry = ldapu_cert_to_ldap_entry;
  177. (*table)->f_ldapu_set_cert_mapfn = ldapu_set_cert_mapfn;
  178. (*table)->f_ldapu_get_cert_mapfn = ldapu_get_cert_mapfn;
  179. (*table)->f_ldapu_set_cert_searchfn = ldapu_set_cert_searchfn;
  180. (*table)->f_ldapu_get_cert_searchfn = ldapu_get_cert_searchfn;
  181. (*table)->f_ldapu_set_cert_verifyfn = ldapu_set_cert_verifyfn;
  182. (*table)->f_ldapu_get_cert_verifyfn = ldapu_get_cert_verifyfn;
  183. (*table)->f_ldapu_get_cert_subject_dn = ldapu_get_cert_subject_dn;
  184. (*table)->f_ldapu_get_cert_issuer_dn = ldapu_get_cert_issuer_dn;
  185. (*table)->f_ldapu_get_cert_ava_val = ldapu_get_cert_ava_val;
  186. (*table)->f_ldapu_free_cert_ava_val = ldapu_free_cert_ava_val;
  187. (*table)->f_ldapu_get_cert_der = ldapu_get_cert_der;
  188. (*table)->f_ldapu_issuer_certinfo = ldapu_issuer_certinfo;
  189. (*table)->f_ldapu_certmap_info_attrval = ldapu_certmap_info_attrval;
  190. (*table)->f_ldapu_err2string = ldapu_err2string;
  191. (*table)->f_ldapu_free_old = ldapu_free_old;
  192. (*table)->f_ldapu_malloc = ldapu_malloc;
  193. (*table)->f_ldapu_strdup = ldapu_strdup;
  194. (*table)->f_ldapu_free = ldapu_free;
  195. return LDAPU_SUCCESS;
  196. }