init.c 6.9 KB

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