1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
|
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* All rights reserved.
*
* License: GPL (version 3 or any later version).
* See LICENSE for details.
* END COPYRIGHT BLOCK **/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h> /* getenv */
#include <string.h> /* memcpy */
#include <ldaputil/errors.h> /* LDAPU_SUCCESS, ldapu_VTable_set */
#include <ldaputil/ldaputil.h> /* ldapu_VTable_set */
#include <ldaputil/init.h> /* ldaputil_init */
#include <ldaputil/certmap.h> /* ldapu_cert_to_ldap_entry */
#include <sys/param.h> /* MAXPATHLEN */
#include "slap.h" /* slapi_ch_malloc */
#include "fe.h"
char *client_auth_config_file = NULL;
/* forward declarations */
static void generate_id(void);
static Slapi_ComponentId *auth_get_component_id(void);
#define internal_ld NULL
static int LDAP_CALL LDAP_CALLBACK
slapu_msgfree(LDAP *ld, LDAPMessage *msg)
{
Slapi_PBlock *pb = (Slapi_PBlock *)msg;
if (ld != internal_ld) {
return ldap_msgfree(msg);
}
if (pb) {
slapi_free_search_results_internal(pb);
slapi_pblock_destroy(pb);
}
return LDAP_SUCCESS;
}
static int LDAP_CALL LDAP_CALLBACK
slapu_search_s(LDAP *ld, const char *rawbaseDN, int scope, const char *filter, char **attrs, int attrsonly, LDAPMessage **result)
{
int err = LDAP_NO_SUCH_OBJECT;
Slapi_PBlock *pb = NULL;
LDAPControl **ctrls;
Slapi_DN *sdn = slapi_sdn_new_dn_byval(rawbaseDN);
const char *baseDN = slapi_sdn_get_dn(sdn);
if (ld != internal_ld) {
err = ldap_search_ext_s(ld, baseDN, scope, filter, attrs, attrsonly,
NULL, NULL, NULL, -1, result);
slapi_sdn_free(&sdn);
return err;
}
slapi_log_err(SLAPI_LOG_TRACE, "slapu_search_s", "=> (\"%s\", %i, %s)\n",
baseDN, scope, filter);
if (filter == NULL)
filter = "objectclass=*";
/* use new internal search API */
pb = slapi_pblock_new();
/* we need to provide managedsait control to avoid returning continuation references */
ctrls = (LDAPControl **)slapi_ch_calloc(2, sizeof(LDAPControl *));
ctrls[0] = (LDAPControl *)slapi_ch_malloc(sizeof(LDAPControl));
ctrls[0]->ldctl_oid = slapi_ch_strdup(LDAP_CONTROL_MANAGEDSAIT);
ctrls[0]->ldctl_value.bv_val = NULL;
ctrls[0]->ldctl_value.bv_len = 0;
ctrls[0]->ldctl_iscritical = '\0';
slapi_search_internal_set_pb(pb, baseDN, scope, (char *)filter, attrs,
attrsonly, ctrls, NULL,
auth_get_component_id(), 0 /* actions */);
slapi_search_internal_pb(pb);
if (pb != NULL) {
if (slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &err)) {
err = LDAP_LOCAL_ERROR;
}
if (err != LDAP_SUCCESS) {
slapu_msgfree(ld, (LDAPMessage *)pb);
pb = NULL;
if (scope == LDAP_SCOPE_SUBTREE) {
char fbuf[BUFSIZ];
slapi_log_err(SLAPI_LOG_ERR, "slapu_search_s", "(\"%s\", subtree, %s) err %i\n",
baseDN, escape_string((char *)filter, fbuf), err);
}
}
} else {
char fbuf[BUFSIZ];
slapi_log_err(SLAPI_LOG_ERR, "slapu_search_s", "(\"%s\", %i, %s) NULL\n",
baseDN, scope, escape_string((char *)filter, fbuf));
}
slapi_sdn_free(&sdn);
*result = (LDAPMessage *)pb;
slapi_log_err(SLAPI_LOG_TRACE, "<= slapu_search_s", "%i\n", err);
return err;
}
static int LDAP_CALL LDAP_CALLBACK
slapu_count_entries(LDAP *ld, LDAPMessage *msg)
{
Slapi_Entry **entry = NULL;
int count = 0;
if (ld != internal_ld) {
return ldap_count_entries(ld, msg);
}
if (!slapi_pblock_get((Slapi_PBlock *)msg, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entry) && entry) {
for (; *entry; ++entry)
++count;
}
return count;
}
/* slapu_search_s() returns a Slapi_PBlock*, but slapu_first_entry() and
* slapu_next_entry() return a Slapi_Entry** pointing into the same array
* as the PBlock. If one of the iteration (Slapi_Entry**) pointers was
* passed to slapu_msgfree(), havoc would ensue. ldaputil never does this.
* But ldap_msgfree() would support it (no?); so a plugin function might.
* Yet another way this doesn't support plugin functions.
*/
static LDAPMessage *LDAP_CALL LDAP_CALLBACK
slapu_first_entry(LDAP *ld, LDAPMessage *msg)
{
Slapi_Entry **entry = NULL;
if (ld != internal_ld) {
return ldap_first_entry(ld, msg);
}
if (!slapi_pblock_get((Slapi_PBlock *)msg, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entry) && entry && *entry) {
return (LDAPMessage *)entry;
}
return NULL;
}
static LDAPMessage *LDAP_CALL LDAP_CALLBACK
slapu_next_entry(LDAP *ld, LDAPMessage *msg)
{
Slapi_Entry **entry = (Slapi_Entry **)msg;
if (ld != internal_ld) {
if (msg) {
return ldap_next_entry(ld, msg);
} else {
return NULL;
}
}
if (entry && *entry && *++entry) {
return (LDAPMessage *)entry;
}
return NULL;
}
static char *LDAP_CALL LDAP_CALLBACK
slapu_get_dn(LDAP *ld, LDAPMessage *entry)
{
if (ld != internal_ld) {
return ldap_get_dn(ld, entry);
}
return slapi_ch_strdup(slapi_entry_get_dn(*(Slapi_Entry **)entry));
}
static void LDAP_CALL LDAP_CALLBACK
slapu_memfree(LDAP *ld, void *dn)
{
if (ld != internal_ld) {
ldap_memfree(dn);
} else {
free(dn);
}
}
static char *
slapu_attr_get_desc(Slapi_Attr *attr)
{
char *desc = NULL;
if (slapi_attr_get_type(attr, &desc) == LDAP_SUCCESS && desc) {
return slapi_ch_strdup(desc);
}
return NULL;
}
/* slapu_first_attribute and slapu_next_attribute use a Slapi_Attr*
* as an iterator. It is malloc'd by first() and free'd by ber_free().
*/
static char *LDAP_CALL LDAP_CALLBACK
slapu_first_attribute(LDAP *ld, LDAPMessage *entry, BerElement **iter)
{
if (ld != internal_ld) {
return ldap_first_attribute(ld, entry, iter);
} else {
Slapi_Attr **attr = (Slapi_Attr **)slapi_ch_malloc(sizeof(Slapi_Attr *));
*iter = (BerElement *)attr;
if (attr && slapi_entry_first_attr(*(Slapi_Entry **)entry, attr) == LDAP_SUCCESS) {
return slapu_attr_get_desc(*attr);
}
}
return NULL;
}
static char *LDAP_CALL LDAP_CALLBACK
slapu_next_attribute(LDAP *ld, LDAPMessage *entry, BerElement *iter)
{
Slapi_Attr **attr = (Slapi_Attr **)iter;
if (ld != internal_ld) {
return ldap_next_attribute(ld, entry, iter);
}
if (attr && slapi_entry_next_attr(*(Slapi_Entry **)entry, *attr, attr) == LDAP_SUCCESS) {
return slapu_attr_get_desc(*attr);
}
return NULL;
}
static void LDAP_CALL LDAP_CALLBACK
slapu_ber_free(LDAP *ld, BerElement *iter, int freebuf)
{
if (ld != internal_ld) {
ber_free(iter, freebuf);
} else {
free((Slapi_Attr **)iter);
}
}
static struct berval **LDAP_CALL LDAP_CALLBACK
slapu_get_values_len(LDAP *ld, LDAPMessage *entry, const char *desc)
{
Slapi_Attr *attr = NULL;
if (ld != internal_ld) {
return ldap_get_values_len(ld, entry, desc);
}
if (slapi_entry_attr_find(*(Slapi_Entry **)entry, desc, &attr) == LDAP_SUCCESS && attr) {
struct berval **values = NULL;
if (slapi_attr_get_bervals_copy(attr, &values) == 0) {
return (values);
}
}
return NULL;
}
static void LDAP_CALL LDAP_CALLBACK
slapu_value_free_len(LDAP *ld, struct berval **values)
{
if (ld != internal_ld) {
ldap_value_free_len(values);
} else {
ber_bvecfree(values);
}
}
void
client_auth_init()
{
int err;
if (client_auth_config_file == NULL) {
char *confdir = config_get_configdir();
if (NULL == confdir) {
slapi_log_err(SLAPI_LOG_ERR, "client_auth_init", "Failed to get configdir\n");
return;
}
client_auth_config_file = PR_smprintf("%s/certmap.conf", confdir);
if (NULL == client_auth_config_file) {
slapi_log_err(SLAPI_LOG_ERR, "client_auth_init", "Failed to duplicate \"%s/certmap\"\n", confdir);
slapi_ch_free_string(&confdir);
return;
}
slapi_ch_free_string(&confdir);
}
err = ldaputil_init(client_auth_config_file, "", NULL, "slapd", NULL);
if (err != LDAPU_SUCCESS) {
slapi_log_err(SLAPI_LOG_TRACE, "client_auth_init", "ldaputil_init(%s,...) %i\n", client_auth_config_file, err);
} else {
LDAPUVTable_t vtable = {
NULL /* ssl_init */,
NULL /* set_option */,
NULL /* simple_bind_s */,
NULL /* unbind */,
slapu_search_s,
slapu_count_entries,
slapu_first_entry,
slapu_next_entry,
slapu_msgfree,
slapu_get_dn,
slapu_memfree,
slapu_first_attribute,
slapu_next_attribute,
slapu_ber_free,
NULL /* get_values */,
NULL /* value_free */,
slapu_get_values_len,
slapu_value_free_len};
ldapu_VTable_set(&vtable);
}
/* Generate a component id for cert-based authentication */
generate_id();
}
#include <ssl.h>
#include "slapi-plugin.h" /* SLAPI_BERVAL_EQ */
#include "slapi-private.h" /* COMPONENT_CERT_AUTH */
static Slapi_ComponentId *auth_component_id = NULL;
static void
generate_id(void)
{
if (auth_component_id == NULL) {
auth_component_id = generate_componentid(NULL /* Not a plugin */, COMPONENT_CERT_AUTH);
}
}
static Slapi_ComponentId *
auth_get_component_id(void)
{
return auth_component_id;
}
static char *
subject_of(CERTCertificate *cert)
{
char *dn = NULL;
if (cert != NULL) {
int err = ldapu_get_cert_subject_dn(cert, &dn);
if (err != LDAPU_SUCCESS) {
slapi_log_err(SLAPI_LOG_ERR, "subject_of", "ldapu_get_cert_subject_dn(%p) %i (%s)\n",
(void *)cert, err, ldapu_err2string(err));
}
}
return dn;
}
static char *
issuer_of(CERTCertificate *cert)
{
char *dn = NULL;
if (cert != NULL) {
int err = ldapu_get_cert_issuer_dn(cert, &dn);
if (err != LDAPU_SUCCESS) {
slapi_log_err(SLAPI_LOG_ERR, "issuer_of", "ldapu_get_cert_issuer_dn(%p) %i (%s)\n",
(void *)cert, err, ldapu_err2string(err));
}
}
return dn;
}
/*
* Log a certificate that was rejected because the client didn't
* authenticate it.
*
* Note: handle_bad_certificate() is called via slapd_ssl_badCertHook().
* A Connection * is passed in client data. That connection must have its
* c_mutex locked.
*/
int
handle_bad_certificate(void *clientData, PRFileDesc *prfd)
{
char sbuf[BUFSIZ], ibuf[BUFSIZ];
Connection *conn = (Connection *)clientData;
CERTCertificate *clientCert = slapd_ssl_peerCertificate(prfd);
PRErrorCode errorCode = PR_GetError();
char *subject = subject_of(clientCert);
char *issuer = issuer_of(clientCert);
slapi_log_access(LDAP_DEBUG_STATS,
"conn=%" PRIu64 " " SLAPI_COMPONENT_NAME_NSPR " error %i (%s); unauthenticated client %s; issuer %s\n",
conn->c_connid, errorCode, slapd_pr_strerror(errorCode),
subject ? escape_string(subject, sbuf) : "NULL",
issuer ? escape_string(issuer, ibuf) : "NULL");
if (issuer)
free(issuer);
if (subject)
free(subject);
if (clientCert)
CERT_DestroyCertificate(clientCert);
return -1; /* non-zero means reject this certificate */
}
/*
* Get an identity from the client's certificate (if any was sent).
*
* Note: handle_handshake_done() is called via slapd_ssl_handshakeCallback().
* A Connection * is passed in client data. That connection must have its
* c_mutex locked.
*/
void
handle_handshake_done(PRFileDesc *prfd, void *clientData)
{
Connection *conn = (Connection *)clientData;
CERTCertificate *clientCert = slapd_ssl_peerCertificate(prfd);
char *clientDN = NULL;
int keySize = 0;
char *cipher = NULL;
char *extraErrorMsg = "";
SSLChannelInfo channelInfo;
SSLCipherSuiteInfo cipherInfo;
char *subject = NULL;
char sslversion[64];
if ((slapd_ssl_getChannelInfo(prfd, &channelInfo, sizeof(channelInfo))) != SECSuccess) {
PRErrorCode errorCode = PR_GetError();
slapi_log_access(LDAP_DEBUG_STATS,
"conn=%" PRIu64 " SSL failed to obtain channel info; " SLAPI_COMPONENT_NAME_NSPR " error %i (%s)\n",
conn->c_connid, errorCode, slapd_pr_strerror(errorCode));
goto done;
}
if ((slapd_ssl_getCipherSuiteInfo(channelInfo.cipherSuite, &cipherInfo, sizeof(cipherInfo))) != SECSuccess) {
PRErrorCode errorCode = PR_GetError();
slapi_log_access(LDAP_DEBUG_STATS,
"conn=%" PRIu64 " SSL failed to obtain cipher info; " SLAPI_COMPONENT_NAME_NSPR " error %i (%s)\n",
conn->c_connid, errorCode, slapd_pr_strerror(errorCode));
goto done;
}
keySize = cipherInfo.effectiveKeyBits;
cipher = slapi_ch_strdup(cipherInfo.symCipherName);
/* If inside an Start TLS operation, perform the privacy level discovery
* and if the security degree achieved after the handshake is not reckoned
* to be enough, close the SSL connection. */
if (conn->c_flags & CONN_FLAG_START_TLS) {
if (cipherInfo.symKeyBits == 0) {
start_tls_graceful_closure(conn, NULL, 1);
goto done;
}
}
if (config_get_SSLclientAuth() == SLAPD_SSLCLIENTAUTH_OFF) {
(void)slapi_getSSLVersion_str(channelInfo.protocolVersion, sslversion, sizeof(sslversion));
slapi_log_access(LDAP_DEBUG_STATS, "conn=%" PRIu64 " %s %i-bit %s\n",
conn->c_connid,
sslversion, keySize, cipher ? cipher : "NULL");
goto done;
}
if (clientCert == NULL) {
(void)slapi_getSSLVersion_str(channelInfo.protocolVersion, sslversion, sizeof(sslversion));
slapi_log_access(LDAP_DEBUG_STATS, "conn=%" PRIu64 " %s %i-bit %s\n",
conn->c_connid,
sslversion, keySize, cipher ? cipher : "NULL");
} else {
subject = subject_of(clientCert);
if (!subject) {
(void)slapi_getSSLVersion_str(channelInfo.protocolVersion,
sslversion, sizeof(sslversion));
slapi_log_access(LDAP_DEBUG_STATS,
"conn=%" PRIu64 " %s %i-bit %s; missing subject\n",
conn->c_connid,
sslversion, keySize, cipher ? cipher : "NULL");
goto done;
}
{
char *issuer = issuer_of(clientCert);
char sbuf[BUFSIZ], ibuf[BUFSIZ];
(void)slapi_getSSLVersion_str(channelInfo.protocolVersion,
sslversion, sizeof(sslversion));
slapi_log_access(LDAP_DEBUG_STATS,
"conn=%" PRIu64 " %s %i-bit %s; client %s; issuer %s\n",
conn->c_connid,
sslversion, keySize,
cipher ? cipher : "NULL",
escape_string(subject, sbuf),
issuer ? escape_string(issuer, ibuf) : "NULL");
if (issuer)
free(issuer);
}
slapi_dn_normalize(subject);
{
LDAPMessage *chain = NULL;
char *basedn = config_get_basedn();
int err;
err = ldapu_cert_to_ldap_entry(clientCert, internal_ld, basedn ? basedn : "" /*baseDN*/, &chain);
if (err == LDAPU_SUCCESS && chain) {
LDAPMessage *entry = slapu_first_entry(internal_ld, chain);
if (entry) {
/* clientDN is duplicated in slapu_get_dn */
clientDN = slapu_get_dn(internal_ld, entry);
} else {
extraErrorMsg = "no entry";
slapi_log_err(SLAPI_LOG_TRACE, "handle_handshake_done", "<= ldapu_cert_to_ldap_entry() %s\n",
extraErrorMsg);
}
} else {
extraErrorMsg = ldapu_err2string(err);
slapi_log_err(SLAPI_LOG_TRACE, "handle_handshake_done", "<= ldapu_cert_to_ldap_entry() %i (%s)%s\n",
err, extraErrorMsg, chain ? "" : " NULL");
}
slapi_ch_free_string(&basedn);
slapu_msgfree(internal_ld, chain);
}
}
if (clientDN != NULL) {
Slapi_DN *sdn = NULL;
sdn = slapi_sdn_new_dn_passin(clientDN);
clientDN = slapi_ch_strdup(slapi_sdn_get_dn(sdn));
slapi_sdn_free(&sdn);
(void)slapi_getSSLVersion_str(channelInfo.protocolVersion,
sslversion, sizeof(sslversion));
slapi_log_access(LDAP_DEBUG_STATS,
"conn=%" PRIu64 " %s client bound as %s\n",
conn->c_connid,
sslversion, clientDN);
} else if (clientCert != NULL) {
(void)slapi_getSSLVersion_str(channelInfo.protocolVersion,
sslversion, sizeof(sslversion));
slapi_log_access(LDAP_DEBUG_STATS,
"conn=%" PRIu64 " %s failed to map client "
"certificate to LDAP DN (%s)\n",
conn->c_connid,
sslversion, extraErrorMsg);
}
/*
* Associate the new credentials with the connection. Note that
* clientDN and clientCert may be NULL.
*/
bind_credentials_set_nolock(conn, SLAPD_AUTH_SSL, clientDN,
SLAPD_AUTH_SSL, clientDN, clientCert, NULL);
done:
slapi_ch_free_string(&subject);
slapi_ch_free_string(&cipher);
/* clientDN and clientCert will be freed later */
}
|