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