certmap.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #pragma once
  13. /* What was extcmap.h begins ... */
  14. #include <ldap.h>
  15. #include <cert.h>
  16. #ifndef NSAPI_PUBLIC
  17. #define NSAPI_PUBLIC
  18. #endif
  19. #define LDAPU_ATTR_INITFN "InitFn"
  20. #define LDAPU_ATTR_LIBRARY "library"
  21. #define LDAPU_ATTR_DNCOMPS "DNComps"
  22. #define LDAPU_ATTR_FILTERCOMPS "FilterComps"
  23. #define LDAPU_ATTR_VERIFYCERT "VerifyCert"
  24. #define LDAPU_ATTR_CERTMAP_LDAP_ATTR "CmapLdapAttr"
  25. /*
  26. * CertMapFn_t -
  27. * This is a typedef for cert mapping function. The mapping function is
  28. * called by the function ldapu_cert_to_ldap_entry.
  29. * Parameters:
  30. * cert - cert to be mapped. You can pass this to
  31. * functions ldapu_get_cert_XYZ.
  32. * ld - Handle to the connection to the directory server.
  33. * certmap_info - This structure contains information about the
  34. * configuration parameters for the cert's issuer (CA).
  35. * This structure can be passed to the function
  36. * ldapu_certmap_info_attrval to get value for a particular
  37. * configuration attribute (or a property).
  38. * ldapdn - The mapping function should allocate memory for ldapdn
  39. * using malloc and set this variable using the 'cert' and
  40. * 'certmap_info'. This DN will be used for ldap lookup.
  41. * filter - The mapping function should allocate memory for filter
  42. * using malloc and set this variable using the 'cert' and
  43. * 'certmap_info'. This will be used as ldap filter for ldap
  44. * lookup of the ldapdn.
  45. *
  46. * Return Value:
  47. * return LDAPU_SUCCESS upon successful completion (cert is mapped)
  48. * return LDAPU_FAILED there is no unexpected error but cert could not
  49. * mapped (probably because ldap entry doesn't exist).
  50. * otherwise return LDAPU_CERT_MAP_FUNCTION_FAILED.
  51. */
  52. typedef int (*CertMapFn_t)(void *cert, LDAP *ld, void *certmap_info, char **ldapdn, char **filter);
  53. /*
  54. * CertSearchFn_t -
  55. * This is a typedef for cert search function. The search function is
  56. * called by the function ldapu_cert_to_ldap_entry after calling the mapping
  57. * function. The candidate 'dn' and 'filter' returned by the mapping
  58. * function is passed to this function.
  59. * The default search function works as follows:
  60. * 1. If the 'filter' is NULL, default it to 'objectclass=*'.
  61. * 2. If the 'dn' is non-NULL, do a base level search with the 'dn' and
  62. * 'filter'. If it succeeds, we are done. If there is no serious
  63. * error (LDAP_NO_SUCH_OBJECT is not serious error yet), continue.
  64. * 3. If the 'dn' is NULL, default it to 'basedn'.
  65. * 4. Perform a 'subtree' search in LDAP for the 'dn' and the 'filter'.
  66. * 5. Return the results of the last search.
  67. * Parameters:
  68. * cert - cert to be mapped. You can pass this to
  69. * functions ldapu_get_cert_XYZ.
  70. * ld - Handle to the connection to the directory server.
  71. * certmap_info - This structure contains information about the
  72. * configuration parameters for the cert's issuer (CA).
  73. * This structure can be passed to the function
  74. * ldapu_certmap_info_attrval to get value for a particular
  75. * configuration attribute (or a property).
  76. * suffix - If the ldapdn is empty then use this DN to begin the
  77. * search. This is the DN of the root object in LDAP
  78. * Directory.
  79. * ldapdn - candidate 'dn' returned by the mapping function.
  80. * filter - returned by the mapping function.
  81. * attrs - list of attributes to return from the search. If this is
  82. * NULL, all attributes are returned.
  83. * res - result of the search which is passed to the verify
  84. * function.
  85. *
  86. * Return Value:
  87. * return LDAPU_SUCCESS upon successful completion
  88. * return LDAPU_FAILED there is no unexpected error but entries matching the
  89. * 'dn' and 'filter' doesn't exist.
  90. * otherwise return LDAPU_CERT_SEARCH_FUNCTION_FAILED.
  91. */
  92. typedef int (*CertSearchFn_t)(void *cert, LDAP *ld, void *certmap_info, const char *suffix, const char *ldapdn, const char *filter, const char **attrs, LDAPMessage ***res);
  93. /*
  94. * CertVerifyFn_t -
  95. * This is a typedef for cert verify function. The verify function is
  96. * called by the function ldapu_cert_to_ldap_entry after the cert is
  97. * successfully mapped to ldapdn and filter, and an entry matching that
  98. * exists in the directory server. The verify fn may get called for
  99. * multiple matched entries. This function must go through all the entries
  100. * and check which one is appropriate. The pointer to that entry must be
  101. * passed back in the 'LDAPMessage **entry' parameter.
  102. * Parameters:
  103. * cert - Original cert to be mapped. You can pass this to
  104. * functions ldapu_get_cert_XYZ.
  105. * ld - Handle to the connection to the directory server.
  106. * certmap_info - This structure contains information about the
  107. * configuration parameters for the cert's issuer (CA).
  108. * This structure can be passed to the function
  109. * ldapu_certmap_info_attrval to get value for a particular
  110. * configuration attribute (or a property).
  111. * res - cert is first mapped to ldapdn and filter. 'res' is the
  112. * result of ldap search using the ldapdn and filter.
  113. * 'ld' and 'res' can be used in the calls to ldapsdk API.
  114. * entry - pointer to the entry from 'res' which is the correct match
  115. * according to the verify function.
  116. *
  117. * Return Values:
  118. * return LDAPU_SUCCESS upon successful completion (cert is verified)
  119. * return LDAPU_FAILED there is no unexpected error but cert could not
  120. * verified (probably because it was revoked).
  121. * otherwise return LDAPU_CERT_VERIFY_FUNCTION_FAILED.
  122. */
  123. typedef int (*CertVerifyFn_t)(void *cert, LDAP *ld, void *certmap_info, LDAPMessage *res, LDAPMessage **entry);
  124. /*
  125. * CertmapInitFn_t -
  126. * This is a typedef for user defined init function. An init function can be
  127. * specified in the config file (<ServerRoot>/userdb/certmap.conf) per issuer
  128. * of a certificate. This init function must from the user's library, also
  129. * loaded from the config file using the 'library' property. The init
  130. * function is specified in the config file using the 'InitFn' property.
  131. * When the config file is loaded, any user defined init functions will be
  132. * called with the certmap_info pertaining to the issuer (CA).
  133. * Parameters:
  134. * certmap_info - This structure contains information about the
  135. * configuration parameters for the cert's issuer (CA).
  136. * This structure can be passed to the function
  137. * ldapu_certmap_info_attrval to get value for a particular
  138. * configuration attribute (or a property).
  139. *
  140. * Return Value:
  141. * return LDAPU_SUCCESS upon successful completion
  142. * otherwise return LDAPU_CERT_MAP_INITFN_FAILED. The server startup will be
  143. * aborted if the return value is not LDAPU_SUCCESS.
  144. */
  145. typedef int (*CertMapInitFn_t)(void *certmap_info, const char *issuerName, const CERTName *issuerDN, const char *libname);
  146. /*
  147. * Refer to the description of the function ldapu_get_cert_ava_val
  148. */
  149. enum
  150. {
  151. LDAPU_SUBJECT_DN,
  152. LDAPU_ISSUER_DN
  153. };
  154. /* end extcmap */
  155. enum
  156. {
  157. LDAPU_STR_FILTER_DEFAULT,
  158. LDAPU_STR_FILTER_USER,
  159. LDAPU_STR_FILTER_GROUP,
  160. LDAPU_STR_FILTER_MEMBER,
  161. LDAPU_STR_FILTER_MEMBER_RECURSE,
  162. LDAPU_STR_ATTR_USER,
  163. LDAPU_STR_ATTR_CERT,
  164. LDAPU_STR_ATTR_CERT_NOSUBTYPE,
  165. LDAPU_STR_MAX_INDEX
  166. };
  167. #ifdef DEFINE_LDAPU_STRINGS
  168. /* used only in certmap.c and ldaputil.c */
  169. static char *ldapu_strings[] = {
  170. "objectclass=*", /* LDAPU_STR_DEFAULT */
  171. "uid=%s", /* LDAPU_STR_FILTER_USER */
  172. "(& (cn=%s) (| (objectclass=groupofuniquenames) (objectclass=groupofnames)))", /* LDAPU_STR_FILTER_GROUP */
  173. "(| (uniquemember=%s) (member=%s))", /* LDAPU_STR_FILTER_MEMBER */
  174. "(& %s (| (objectclass=groupofuniquenames) (objectclass=groupofnames))", /* LDAPU_STR_FILTER_MEMBER_RECURSE */
  175. "uid", /* LDAPU_STR_ATTR_USER */
  176. "userCertificate;binary", /* LDAPU_STR_ATTR_CERT */
  177. "userCertificate" /* LDAPU_STR_ATTR_CERT_NOSUBTYPE */
  178. };
  179. #endif /* DEFINE_LDAPU_STRINGS */
  180. typedef struct
  181. {
  182. char *str;
  183. int size;
  184. int len;
  185. } LDAPUStr_t;
  186. #ifdef __cplusplus
  187. extern "C" {
  188. #endif
  189. NSAPI_PUBLIC int ldapu_cert_to_ldap_entry(void *cert, LDAP *ld, const char *basedn, LDAPMessage **res);
  190. NSAPI_PUBLIC int ldapu_set_cert_mapfn(const CERTName *issuerDN,
  191. CertMapFn_t mapfn);
  192. NSAPI_PUBLIC CertMapFn_t ldapu_get_cert_mapfn(const CERTName *issuerDN);
  193. NSAPI_PUBLIC int ldapu_set_cert_searchfn(const CERTName *issuerDN,
  194. CertSearchFn_t searchfn);
  195. NSAPI_PUBLIC CertSearchFn_t ldapu_get_cert_searchfn(const CERTName *issuerDN);
  196. NSAPI_PUBLIC int ldapu_set_cert_verifyfn(const CERTName *issuerDN,
  197. CertVerifyFn_t verifyFn);
  198. NSAPI_PUBLIC CertVerifyFn_t ldapu_get_cert_verifyfn(const CERTName *issuerDN);
  199. NSAPI_PUBLIC int ldapu_get_cert_subject_dn(void *cert, char **subjectDN);
  200. NSAPI_PUBLIC CERTName *ldapu_get_cert_issuer_dn_as_CERTName(CERTCertificate *cert);
  201. NSAPI_PUBLIC int ldapu_get_cert_issuer_dn(void *cert, char **issuerDN);
  202. NSAPI_PUBLIC int ldapu_get_cert_ava_val(void *cert, int which_dn, const char *attr, char ***val);
  203. NSAPI_PUBLIC int ldapu_free_cert_ava_val(char **val);
  204. NSAPI_PUBLIC int ldapu_get_cert_der(void *cert, unsigned char **derCert, unsigned int *len);
  205. NSAPI_PUBLIC int ldapu_issuer_certinfo(const CERTName *issuerDN,
  206. void **certmap_info);
  207. NSAPI_PUBLIC int ldapu_certmap_info_attrval(void *certmap_info,
  208. const char *attr,
  209. char **val);
  210. NSAPI_PUBLIC char *ldapu_err2string(int err);
  211. /* Keep the old fn for backward compatibility */
  212. NSAPI_PUBLIC void ldapu_free_old(char *ptr);
  213. NSAPI_PUBLIC void *ldapu_malloc(int size);
  214. NSAPI_PUBLIC char *ldapu_strdup(const char *ptr);
  215. NSAPI_PUBLIC void *ldapu_realloc(void *ptr, int size);
  216. NSAPI_PUBLIC void ldapu_free(void *ptr);
  217. NSAPI_PUBLIC int ldaputil_exit(void);
  218. #ifdef __cplusplus
  219. }
  220. #endif