1 /* vc.c - LDAP Verify Credentials extop (no spec yet) */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2010 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Pierangelo Masarati for inclusion
18 * in OpenLDAP Software.
22 * LDAP Verify Credentials: suggested by Kurt Zeilenga
29 #include "ac/string.h"
31 typedef struct vc_conn_t {
32 struct vc_conn_t *conn;
34 OperationBuffer opbuf;
39 static const struct berval vc_exop_oid_bv = BER_BVC(LDAP_EXOP_VERIFY_CREDENTIALS);
40 static ldap_pvt_thread_mutex_t vc_mutex;
41 static Avlnode *vc_tree;
44 vc_conn_cmp( const void *c1, const void *c2 )
46 const vc_conn_t *vc1 = (const vc_conn_t *)c1;
47 const vc_conn_t *vc2 = (const vc_conn_t *)c2;
49 return SLAP_PTRCMP( vc1->conn, vc2->conn );
53 vc_conn_dup( void *c1, void *c2 )
55 vc_conn_t *vc1 = (vc_conn_t *)c1;
56 vc_conn_t *vc2 = (vc_conn_t *)c2;
58 if ( vc1->conn == vc2->conn ) {
69 const char *diagnosticMessage,
70 struct berval *servercred,
71 struct berval *authzid,
75 BerElementBuffer berbuf;
76 BerElement *ber = (BerElement *)&berbuf;
80 assert( val != NULL );
84 ber_init2( ber, NULL, LBER_USE_DER );
86 (void)ber_printf( ber, "{is" /*}*/ , resultCode, diagnosticMessage ? diagnosticMessage : "" );
91 cookie.bv_len = sizeof( conn );
92 cookie.bv_val = (char *)&conn;
93 (void)ber_printf( ber, "tO", 0, LDAP_TAG_EXOP_VERIFY_CREDENTIALS_COOKIE, &cookie );
97 ber_printf( ber, "tO", LDAP_TAG_EXOP_VERIFY_CREDENTIALS_SCREDS, servercred );
102 ber_printf( ber, "tO", LDAP_TAG_EXOP_VERIFY_CREDENTIALS_AUTHZID, authzid );
109 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_EXOP_VERIFY_CREDENTIALS_CONTROLS );
110 if ( rc == -1 ) goto done;
112 for ( c = 0; ctrls[c] != NULL; c++ ) {
113 rc = ber_printf( ber, "{s" /*}*/, ctrls[c]->ldctl_oid );
115 if ( ctrls[c]->ldctl_iscritical ) {
116 rc = ber_printf( ber, "b", (ber_int_t)ctrls[c]->ldctl_iscritical ) ;
117 if ( rc == -1 ) goto done;
120 if ( ctrls[c]->ldctl_value.bv_val != NULL ) {
121 rc = ber_printf( ber, "O", &ctrls[c]->ldctl_value );
122 if( rc == -1 ) goto done;
125 rc = ber_printf( ber, /*{*/"N}" );
126 if ( rc == -1 ) goto done;
129 rc = ber_printf( ber, /*{*/"N}" );
130 if ( rc == -1 ) goto done;
133 ber_printf( ber, /*{*/ "}" );
135 rc = ber_flatten2( ber, &bv, 0 );
137 *val = ber_bvdup( &bv );
145 typedef struct vc_cb_t {
146 struct berval sasldata;
155 vc_cb_t *vc = (vc_cb_t *)op->o_callback->sc_private;
157 if ( rs->sr_tag == LDAP_RES_BIND ) {
158 if ( rs->sr_sasldata != NULL ) {
159 ber_dupbv( &vc->sasldata, rs->sr_sasldata );
162 if ( rs->sr_ctrls != NULL ) {
163 vc->ctrls = ldap_controls_dup( rs->sr_ctrls );
175 int rc = LDAP_SUCCESS;
178 BerElementBuffer berbuf;
179 BerElement *ber = (BerElement *)&berbuf;
180 struct berval reqdata = BER_BVNULL;
182 struct berval cookie = BER_BVNULL;
183 struct berval bdn = BER_BVNULL;
185 struct berval cred = BER_BVNULL;
186 struct berval ndn = BER_BVNULL;
187 struct berval mechanism = BER_BVNULL;
189 vc_conn_t *conn = NULL;
191 slap_callback sc = { 0 };
192 SlapReply rs2 = { 0 };
194 if ( op->ore_reqdata == NULL || op->ore_reqdata->bv_len == 0 ) {
195 rs->sr_text = "empty request data field in VerifyCredentials exop";
196 return LDAP_PROTOCOL_ERROR;
200 rs->sr_err = LDAP_SUCCESS;
202 ber_dupbv_x( &reqdata, op->ore_reqdata, op->o_tmpmemctx );
204 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
205 ber_init2( ber, &reqdata, 0 );
207 tag = ber_scanf( ber, "{" /*}*/ );
208 if ( tag != LBER_SEQUENCE ) {
209 rs->sr_err = LDAP_PROTOCOL_ERROR;
213 tag = ber_peek_tag( ber, &len );
214 if ( tag == LDAP_TAG_EXOP_VERIFY_CREDENTIALS_COOKIE ) {
216 * cookie: the pointer to the connection
220 ber_scanf( ber, "m", &cookie );
221 if ( cookie.bv_len != sizeof(Connection *) ) {
222 rs->sr_err = LDAP_PROTOCOL_ERROR;
228 tag = ber_scanf( ber, "mt", &bdn, &authtag );
229 if ( tag == LBER_ERROR ) {
230 rs->sr_err = LDAP_PROTOCOL_ERROR;
234 rc = dnNormalize( 0, NULL, NULL, &bdn, &ndn, op->o_tmpmemctx );
235 if ( rc != LDAP_SUCCESS ) {
236 rs->sr_err = LDAP_PROTOCOL_ERROR;
241 case LDAP_AUTH_SIMPLE:
242 /* cookie only makes sense for SASL bind (so far) */
243 if ( !BER_BVISNULL( &cookie ) ) {
244 rs->sr_err = LDAP_PROTOCOL_ERROR;
248 tag = ber_scanf( ber, "m", &cred );
249 if ( tag == LBER_ERROR ) {
250 rs->sr_err = LDAP_PROTOCOL_ERROR;
256 tag = ber_scanf( ber, "{s" /*}*/ , &mechanism );
257 if ( tag == LBER_ERROR ||
258 BER_BVISNULL( &mechanism ) || BER_BVISEMPTY( &mechanism ) )
260 rs->sr_err = LDAP_PROTOCOL_ERROR;
264 tag = ber_peek_tag( ber, &len );
265 if ( tag == LBER_OCTETSTRING ) {
266 ber_scanf( ber, "m", &cred );
269 tag = ber_scanf( ber, /*{*/ "}" );
273 rs->sr_err = LDAP_PROTOCOL_ERROR;
277 if ( !BER_BVISNULL( &cookie ) ) {
278 vc_conn_t tmp = { 0 };
280 AC_MEMCPY( (char *)&tmp.conn, (const char *)cookie.bv_val, cookie.bv_len );
281 ldap_pvt_mutex_lock( &vc_mutex );
282 conn = (vc_conn_t *)avl_find( vc_tree, (caddr_t)&tmp, vc_conn_cmp );
283 if ( conn == NULL || ( conn != NULL && conn->refcnt != 0 ) ) {
285 ldap_pvt_mutex_unlock( &vc_mutex );
286 rs->sr_err = LDAP_PROTOCOL_ERROR;
290 ldap_pvt_mutex_unlock( &vc_mutex );
295 conn = (vc_conn_t *)SLAP_CALLOC( 1, sizeof( vc_conn_t ) );
298 thrctx = ldap_pvt_thread_pool_context();
299 connection_fake_init2( &conn->connbuf, &conn->opbuf, thrctx, 0 );
300 conn->op = &conn->opbuf.ob_op;
301 snprintf( conn->op->o_log_prefix, sizeof( conn->op->o_log_prefix ),
302 "%s VERIFYCREDENTIALS", op->o_log_prefix );
305 conn->op->o_tag = LDAP_REQ_BIND;
306 memset( &conn->op->oq_bind, 0, sizeof( conn->op->oq_bind ) );
307 conn->op->o_req_dn = ndn;
308 conn->op->o_req_ndn = ndn;
309 conn->op->o_protocol = LDAP_VERSION3;
310 conn->op->orb_method = authtag;
311 conn->op->o_callback = ≻
314 tag = ber_peek_tag( ber, &len );
315 if ( tag == LDAP_TAG_EXOP_VERIFY_CREDENTIALS_CONTROLS ) {
316 conn->op->o_ber = ber;
317 rc = get_ctrls2( conn->op, &rs2, 0, LDAP_TAG_EXOP_VERIFY_CREDENTIALS_CONTROLS );
318 if ( rc != LDAP_SUCCESS ) {
319 rs->sr_err = LDAP_PROTOCOL_ERROR;
324 tag = ber_skip_tag( ber, &len );
325 if ( len || tag != LBER_DEFAULT ) {
326 rs->sr_err = LDAP_PROTOCOL_ERROR;
331 case LDAP_AUTH_SIMPLE:
335 conn->op->orb_mech = mechanism;
339 conn->op->orb_cred = cred;
340 sc.sc_response = vc_cb;
343 conn->op->o_bd = frontendDB;
344 rs->sr_err = frontendDB->be_bind( conn->op, &rs2 );
346 if ( conn->op->o_conn->c_sasl_bind_in_progress ) {
347 rc = vc_create_response( conn, rs2.sr_err, rs2.sr_text,
348 !BER_BVISEMPTY( &vc.sasldata ) ? &vc.sasldata : NULL,
350 vc.ctrls, &rs->sr_rspdata );
353 rc = vc_create_response( NULL, rs2.sr_err, rs2.sr_text,
355 &conn->op->o_conn->c_dn,
356 vc.ctrls, &rs->sr_rspdata );
360 rs->sr_err = LDAP_OTHER;
364 if ( !BER_BVISNULL( &conn->op->o_conn->c_dn ) &&
365 conn->op->o_conn->c_dn.bv_val != conn->op->o_conn->c_ndn.bv_val )
366 ber_memfree( conn->op->o_conn->c_dn.bv_val );
367 if ( !BER_BVISNULL( &conn->op->o_conn->c_ndn ) )
368 ber_memfree( conn->op->o_conn->c_ndn.bv_val );
372 if ( conn->op->o_conn->c_sasl_bind_in_progress ) {
373 if ( conn->conn == NULL ) {
376 ldap_pvt_mutex_lock( &vc_mutex );
377 rc = avl_insert( &vc_tree, (caddr_t)conn,
378 vc_conn_cmp, vc_conn_dup );
379 ldap_pvt_mutex_unlock( &vc_mutex );
383 ldap_pvt_mutex_lock( &vc_mutex );
385 ldap_pvt_mutex_unlock( &vc_mutex );
389 if ( conn->conn != NULL ) {
392 ldap_pvt_mutex_lock( &vc_mutex );
393 tmp = avl_delete( &vc_tree, (caddr_t)conn, vc_conn_cmp );
394 ldap_pvt_mutex_unlock( &vc_mutex );
401 ldap_controls_free( vc.ctrls );
405 if ( !BER_BVISNULL( &ndn ) ) {
406 op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
410 op->o_tmpfree( reqdata.bv_val, op->o_tmpmemctx );
411 BER_BVZERO( &reqdata );
417 vc_initialize( void )
421 rc = load_extop2( (struct berval *)&vc_exop_oid_bv,
422 SLAP_EXOP_HIDE, vc_exop, 0 );
423 if ( rc != LDAP_SUCCESS ) {
424 Debug( LDAP_DEBUG_ANY,
425 "vc_initialize: unable to register VerifyCredentials exop: %d.\n",
429 ldap_pvt_thread_mutex_init( &vc_mutex );
435 init_module( int argc, char *argv[] )
437 return vc_initialize();