init.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #include <string.h>
  13. #include <prlink.h>
  14. #include <prio.h>
  15. #include <prprf.h>
  16. /*#include "base/file.h"*/
  17. #include "ldaputil/certmap.h"
  18. /*#include "ldaputil/ldapdb.h"*/
  19. #include "ldaputil/ldaputil.h"
  20. #include "ldaputil/cert.h"
  21. #include "ldaputil/errors.h"
  22. #include "ldaputil/init.h"
  23. #include "slapi-plugin.h"
  24. #ifndef FILE_PATHSEP
  25. #define FILE_PATHSEP '/'
  26. #endif
  27. #ifdef HPUX
  28. #ifdef __ia64
  29. #define DLL_SUFFIX ".so"
  30. #else
  31. #define DLL_SUFFIX ".sl"
  32. #endif
  33. #else
  34. #define DLL_SUFFIX ".so"
  35. #endif
  36. static int load_server_libs (const char *dir)
  37. {
  38. int rv = LDAPU_SUCCESS;
  39. PRDir* ds;
  40. int suffix_len = strlen(DLL_SUFFIX);
  41. if ((ds = PR_OpenDir(dir)) != NULL) {
  42. PRDirEntry *d;
  43. /* Dir exists */
  44. while( (d = PR_ReadDir(ds, PR_SKIP_BOTH)) ) {
  45. PRLibrary *lib = 0;
  46. const char *libname = d->name;
  47. int len = strlen(libname);
  48. int is_lib;
  49. is_lib = (len > suffix_len && !strcmp(libname+len-suffix_len,
  50. DLL_SUFFIX));
  51. if(is_lib) {
  52. char path[1024];
  53. PR_snprintf(path, sizeof(path), "%s%c%s", dir, FILE_PATHSEP, libname);
  54. lib = PR_LoadLibrary(path);
  55. if (!lib) rv = LDAPU_ERR_UNABLE_TO_LOAD_PLUGIN;
  56. }
  57. }
  58. }
  59. else {
  60. /* It's ok if dir doesn't exists */
  61. }
  62. return rv;
  63. }
  64. NSAPI_PUBLIC int ldaputil_init (const char *config_file,
  65. const char *dllname,
  66. const char *serv_root,
  67. const char *serv_type,
  68. const char *serv_id)
  69. {
  70. int rv = LDAPU_SUCCESS;
  71. static int initialized = 0;
  72. /* If already initialized, cleanup the old structures */
  73. if (initialized) ldaputil_exit();
  74. if (config_file && *config_file) {
  75. char dir[1024];
  76. LDAPUCertMapListInfo_t *certmap_list;
  77. LDAPUCertMapInfo_t *certmap_default;
  78. if (serv_root && *serv_root) {
  79. /* Load common libraries */
  80. PR_snprintf(dir, sizeof(dir), "%s%clib%c%s", serv_root, FILE_PATHSEP,
  81. FILE_PATHSEP, "common");
  82. rv = load_server_libs(dir);
  83. if (rv != LDAPU_SUCCESS) return rv;
  84. if (serv_type && *serv_type) {
  85. /* Load server type specific libraries */
  86. sprintf(dir, "%s%clib%c%s", serv_root, FILE_PATHSEP,
  87. FILE_PATHSEP, serv_type);
  88. rv = load_server_libs(dir);
  89. if (rv != LDAPU_SUCCESS) return rv;
  90. if (serv_id && *serv_id) {
  91. /* Load server instance specific libraries */
  92. sprintf(dir, "%s%clib%c%s", serv_root, FILE_PATHSEP,
  93. FILE_PATHSEP, serv_id);
  94. rv = load_server_libs(dir);
  95. if (rv != LDAPU_SUCCESS) return rv;
  96. }
  97. }
  98. }
  99. rv = ldapu_certmap_init (config_file, dllname, &certmap_list,
  100. &certmap_default);
  101. }
  102. initialized = 1;
  103. if (rv != LDAPU_SUCCESS) return rv;
  104. return rv;
  105. }
  106. static LDAPUDispatchVector_t __ldapu_vector = {
  107. ldapu_cert_to_ldap_entry,
  108. ldapu_set_cert_mapfn,
  109. ldapu_get_cert_mapfn,
  110. ldapu_set_cert_searchfn,
  111. ldapu_get_cert_searchfn,
  112. ldapu_set_cert_verifyfn,
  113. ldapu_get_cert_verifyfn,
  114. ldapu_get_cert_subject_dn,
  115. ldapu_get_cert_issuer_dn,
  116. ldapu_get_cert_ava_val,
  117. ldapu_free_cert_ava_val,
  118. ldapu_get_cert_der,
  119. ldapu_issuer_certinfo,
  120. ldapu_certmap_info_attrval,
  121. ldapu_err2string,
  122. ldapu_free_old,
  123. ldapu_malloc,
  124. ldapu_strdup,
  125. ldapu_free
  126. };
  127. LDAPUDispatchVector_t *__ldapu_table = &__ldapu_vector;
  128. #if 0
  129. NSAPI_PUBLIC int CertMapDLLInitFn(LDAPUDispatchVector_t **table)
  130. {
  131. *table = &__ldapu_vector;
  132. }
  133. #endif
  134. NSAPI_PUBLIC int CertMapDLLInitFn(LDAPUDispatchVector_t **table)
  135. {
  136. *table = (LDAPUDispatchVector_t *)slapi_ch_malloc(sizeof(LDAPUDispatchVector_t));
  137. if (!*table) return LDAPU_ERR_OUT_OF_MEMORY;
  138. (*table)->f_ldapu_cert_to_ldap_entry = ldapu_cert_to_ldap_entry;
  139. (*table)->f_ldapu_set_cert_mapfn = ldapu_set_cert_mapfn;
  140. (*table)->f_ldapu_get_cert_mapfn = ldapu_get_cert_mapfn;
  141. (*table)->f_ldapu_set_cert_searchfn = ldapu_set_cert_searchfn;
  142. (*table)->f_ldapu_get_cert_searchfn = ldapu_get_cert_searchfn;
  143. (*table)->f_ldapu_set_cert_verifyfn = ldapu_set_cert_verifyfn;
  144. (*table)->f_ldapu_get_cert_verifyfn = ldapu_get_cert_verifyfn;
  145. (*table)->f_ldapu_get_cert_subject_dn = ldapu_get_cert_subject_dn;
  146. (*table)->f_ldapu_get_cert_issuer_dn = ldapu_get_cert_issuer_dn;
  147. (*table)->f_ldapu_get_cert_ava_val = ldapu_get_cert_ava_val;
  148. (*table)->f_ldapu_free_cert_ava_val = ldapu_free_cert_ava_val;
  149. (*table)->f_ldapu_get_cert_der = ldapu_get_cert_der;
  150. (*table)->f_ldapu_issuer_certinfo = ldapu_issuer_certinfo;
  151. (*table)->f_ldapu_certmap_info_attrval = ldapu_certmap_info_attrval;
  152. (*table)->f_ldapu_err2string = ldapu_err2string;
  153. (*table)->f_ldapu_free_old = ldapu_free_old;
  154. (*table)->f_ldapu_malloc = ldapu_malloc;
  155. (*table)->f_ldapu_strdup = ldapu_strdup;
  156. (*table)->f_ldapu_free = ldapu_free;
  157. return LDAPU_SUCCESS;
  158. }