init.c 6.9 KB

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