init.c 6.8 KB

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