2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2012 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
17 * BindRequest ::= SEQUENCE {
19 * name DistinguishedName, -- who
20 * authentication CHOICE {
21 * simple [0] OCTET STRING -- passwd
22 * krbv42ldap [1] OCTET STRING -- OBSOLETE
23 * krbv42dsa [2] OCTET STRING -- OBSOLETE
24 * sasl [3] SaslCredentials -- LDAPv3
28 * BindResponse ::= SEQUENCE {
29 * COMPONENTS OF LDAPResult,
30 * serverSaslCreds OCTET STRING OPTIONAL -- LDAPv3
39 #include <ac/socket.h>
40 #include <ac/stdlib.h>
41 #include <ac/string.h>
48 * ldap_sasl_bind - bind to the ldap server (and X.500).
49 * The dn (usually NULL), mechanism, and credentials are provided.
50 * The message id of the request initiated is provided upon successful
51 * (LDAP_SUCCESS) return.
54 * ldap_sasl_bind( ld, NULL, "mechanism",
55 * cred, NULL, NULL, &msgid )
62 LDAP_CONST char *mechanism,
72 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind\n", 0, 0, 0 );
75 assert( LDAP_VALID( ld ) );
76 assert( msgidp != NULL );
78 /* check client controls */
79 rc = ldap_int_client_controls( ld, cctrls );
80 if( rc != LDAP_SUCCESS ) return rc;
82 if( mechanism == LDAP_SASL_SIMPLE ) {
83 if( dn == NULL && cred != NULL && cred->bv_len ) {
84 /* use default binddn */
85 dn = ld->ld_defbinddn;
88 } else if( ld->ld_version < LDAP_VERSION3 ) {
89 ld->ld_errno = LDAP_NOT_SUPPORTED;
97 /* create a message to send */
98 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
99 ld->ld_errno = LDAP_NO_MEMORY;
103 assert( LBER_VALID( ber ) );
105 LDAP_NEXT_MSGID( ld, id );
106 if( mechanism == LDAP_SASL_SIMPLE ) {
108 rc = ber_printf( ber, "{it{istON}" /*}*/,
110 ld->ld_version, dn, LDAP_AUTH_SIMPLE,
113 } else if ( cred == NULL || cred->bv_val == NULL ) {
114 /* SASL bind w/o credentials */
115 rc = ber_printf( ber, "{it{ist{sN}N}" /*}*/,
117 ld->ld_version, dn, LDAP_AUTH_SASL,
121 /* SASL bind w/ credentials */
122 rc = ber_printf( ber, "{it{ist{sON}N}" /*}*/,
124 ld->ld_version, dn, LDAP_AUTH_SASL,
129 ld->ld_errno = LDAP_ENCODING_ERROR;
134 /* Put Server Controls */
135 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
140 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
141 ld->ld_errno = LDAP_ENCODING_ERROR;
147 /* send the message */
148 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber, id );
161 LDAP_CONST char *mechanism,
163 LDAPControl **sctrls,
164 LDAPControl **cctrls,
165 struct berval **servercredp )
169 struct berval *scredp = NULL;
171 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind_s\n", 0, 0, 0 );
173 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
174 if( servercredp != NULL ) {
175 if (ld->ld_version < LDAP_VERSION3) {
176 ld->ld_errno = LDAP_NOT_SUPPORTED;
182 rc = ldap_sasl_bind( ld, dn, mechanism, cred, sctrls, cctrls, &msgid );
184 if ( rc != LDAP_SUCCESS ) {
188 #ifdef LDAP_CONNECTIONLESS
189 if (LDAP_IS_UDP(ld)) {
194 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 || !result ) {
195 return( ld->ld_errno ); /* ldap_result sets ld_errno */
198 /* parse the results */
200 if( servercredp != NULL ) {
201 rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 );
204 if ( rc != LDAP_SUCCESS ) {
205 ldap_msgfree( result );
209 rc = ldap_result2error( ld, result, 1 );
211 if ( rc == LDAP_SUCCESS || rc == LDAP_SASL_BIND_IN_PROGRESS ) {
212 if( servercredp != NULL ) {
213 *servercredp = scredp;
218 if ( scredp != NULL ) {
227 * Parse BindResponse:
229 * BindResponse ::= [APPLICATION 1] SEQUENCE {
230 * COMPONENTS OF LDAPResult,
231 * serverSaslCreds [7] OCTET STRING OPTIONAL }
233 * LDAPResult ::= SEQUENCE {
234 * resultCode ENUMERATED,
236 * errorMessage LDAPString,
237 * referral [3] Referral OPTIONAL }
241 ldap_parse_sasl_bind_result(
244 struct berval **servercredp,
248 struct berval* scred;
253 Debug( LDAP_DEBUG_TRACE, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
255 assert( ld != NULL );
256 assert( LDAP_VALID( ld ) );
257 assert( res != NULL );
259 if( servercredp != NULL ) {
260 if( ld->ld_version < LDAP_VERSION2 ) {
261 return LDAP_NOT_SUPPORTED;
266 if( res->lm_msgtype != LDAP_RES_BIND ) {
267 ld->ld_errno = LDAP_PARAM_ERROR;
273 if ( ld->ld_error ) {
274 LDAP_FREE( ld->ld_error );
277 if ( ld->ld_matched ) {
278 LDAP_FREE( ld->ld_matched );
279 ld->ld_matched = NULL;
284 ber = ber_dup( res->lm_ber );
287 ld->ld_errno = LDAP_NO_MEMORY;
291 if ( ld->ld_version < LDAP_VERSION2 ) {
292 tag = ber_scanf( ber, "{iA}",
293 &errcode, &ld->ld_error );
295 if( tag == LBER_ERROR ) {
297 ld->ld_errno = LDAP_DECODING_ERROR;
304 tag = ber_scanf( ber, "{eAA" /*}*/,
305 &errcode, &ld->ld_matched, &ld->ld_error );
307 if( tag == LBER_ERROR ) {
309 ld->ld_errno = LDAP_DECODING_ERROR;
313 tag = ber_peek_tag(ber, &len);
315 if( tag == LDAP_TAG_REFERRAL ) {
317 if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
319 ld->ld_errno = LDAP_DECODING_ERROR;
323 tag = ber_peek_tag(ber, &len);
326 if( tag == LDAP_TAG_SASL_RES_CREDS ) {
327 if( ber_scanf( ber, "O", &scred ) == LBER_ERROR ) {
329 ld->ld_errno = LDAP_DECODING_ERROR;
337 if ( servercredp != NULL ) {
338 *servercredp = scred;
340 } else if ( scred != NULL ) {
344 ld->ld_errno = errcode;
350 return( LDAP_SUCCESS );
354 ldap_pvt_sasl_getmechs ( LDAP *ld, char **pmechlist )
356 /* we need to query the server for supported mechs anyway */
357 LDAPMessage *res, *e;
358 char *attrs[] = { "supportedSASLMechanisms", NULL };
359 char **values, *mechlist;
362 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
364 rc = ldap_search_s( ld, "", LDAP_SCOPE_BASE,
365 NULL, attrs, 0, &res );
367 if ( rc != LDAP_SUCCESS ) {
371 e = ldap_first_entry( ld, res );
374 if ( ld->ld_errno == LDAP_SUCCESS ) {
375 ld->ld_errno = LDAP_NO_SUCH_OBJECT;
380 values = ldap_get_values( ld, e, "supportedSASLMechanisms" );
381 if ( values == NULL ) {
383 ld->ld_errno = LDAP_NO_SUCH_ATTRIBUTE;
387 mechlist = ldap_charray2str( values, " " );
388 if ( mechlist == NULL ) {
389 LDAP_VFREE( values );
391 ld->ld_errno = LDAP_NO_MEMORY;
395 LDAP_VFREE( values );
398 *pmechlist = mechlist;
404 * ldap_sasl_interactive_bind - interactive SASL authentication
406 * This routine uses interactive callbacks.
408 * LDAP_SUCCESS is returned upon success, the ldap error code
409 * otherwise. LDAP_SASL_BIND_IN_PROGRESS is returned if further
413 ldap_sasl_interactive_bind(
415 LDAP_CONST char *dn, /* usually NULL */
416 LDAP_CONST char *mechs,
417 LDAPControl **serverControls,
418 LDAPControl **clientControls,
420 LDAP_SASL_INTERACT_PROC *interact,
429 #if defined( HAVE_CYRUS_SASL )
430 LDAP_MUTEX_LOCK( &ldap_int_sasl_mutex );
432 #ifdef LDAP_CONNECTIONLESS
433 if( LDAP_IS_UDP(ld) ) {
434 /* Just force it to simple bind, silly to make the user
435 * ask all the time. No, we don't ever actually bind, but I'll
436 * let the final bind handler take care of saving the cdn.
438 rc = ldap_simple_bind( ld, dn, NULL );
439 rc = rc < 0 ? rc : 0;
447 #ifdef HAVE_CYRUS_SASL
448 if( mechs == NULL || *mechs == '\0' ) {
449 mechs = ld->ld_options.ldo_def_sasl_mech;
453 if( mechs == NULL || *mechs == '\0' ) {
454 /* FIXME: this needs to be asynchronous too;
455 * perhaps NULL should be disallowed for async usage?
457 rc = ldap_pvt_sasl_getmechs( ld, &smechs );
458 if( rc != LDAP_SUCCESS ) {
462 Debug( LDAP_DEBUG_TRACE,
463 "ldap_sasl_interactive_bind: server supports: %s\n",
469 Debug( LDAP_DEBUG_TRACE,
470 "ldap_sasl_interactive_bind: user selected: %s\n",
474 rc = ldap_int_sasl_bind( ld, dn, mechs,
475 serverControls, clientControls,
476 flags, interact, defaults, result, rmech, msgid );
479 #if defined( HAVE_CYRUS_SASL )
480 LDAP_MUTEX_UNLOCK( &ldap_int_sasl_mutex );
482 if ( smechs ) LDAP_FREE( smechs );
488 * ldap_sasl_interactive_bind_s - interactive SASL authentication
490 * This routine uses interactive callbacks.
492 * LDAP_SUCCESS is returned upon success, the ldap error code
496 ldap_sasl_interactive_bind_s(
498 LDAP_CONST char *dn, /* usually NULL */
499 LDAP_CONST char *mechs,
500 LDAPControl **serverControls,
501 LDAPControl **clientControls,
503 LDAP_SASL_INTERACT_PROC *interact,
506 const char *rmech = NULL;
507 LDAPMessage *result = NULL;
511 rc = ldap_sasl_interactive_bind( ld, dn, mechs,
512 serverControls, clientControls,
513 flags, interact, defaults, result, &rmech, &msgid );
515 ldap_msgfree( result );
517 if ( rc != LDAP_SASL_BIND_IN_PROGRESS )
520 #ifdef LDAP_CONNECTIONLESS
521 if (LDAP_IS_UDP(ld)) {
526 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 || !result ) {
527 return( ld->ld_errno ); /* ldap_result sets ld_errno */
529 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
534 #ifdef HAVE_CYRUS_SASL
536 #ifdef HAVE_SASL_SASL_H
537 #include <sasl/sasl.h>
542 #endif /* HAVE_CYRUS_SASL */
545 sb_sasl_generic_remove( Sockbuf_IO_Desc *sbiod );
548 sb_sasl_generic_setup( Sockbuf_IO_Desc *sbiod, void *arg )
550 struct sb_sasl_generic_data *p;
551 struct sb_sasl_generic_install *i;
553 assert( sbiod != NULL );
555 i = (struct sb_sasl_generic_install *)arg;
557 p = LBER_MALLOC( sizeof( *p ) );
561 p->ops_private = i->ops_private;
564 ber_pvt_sb_buf_init( &p->sec_buf_in );
565 ber_pvt_sb_buf_init( &p->buf_in );
566 ber_pvt_sb_buf_init( &p->buf_out );
568 sbiod->sbiod_pvt = p;
570 p->ops->init( p, &p->min_send, &p->max_send, &p->max_recv );
572 if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, p->min_send ) < 0 ) {
573 sb_sasl_generic_remove( sbiod );
582 sb_sasl_generic_remove( Sockbuf_IO_Desc *sbiod )
584 struct sb_sasl_generic_data *p;
586 assert( sbiod != NULL );
588 p = (struct sb_sasl_generic_data *)sbiod->sbiod_pvt;
592 ber_pvt_sb_buf_destroy( &p->sec_buf_in );
593 ber_pvt_sb_buf_destroy( &p->buf_in );
594 ber_pvt_sb_buf_destroy( &p->buf_out );
596 sbiod->sbiod_pvt = NULL;
601 sb_sasl_generic_pkt_length(
602 struct sb_sasl_generic_data *p,
603 const unsigned char *buf,
608 assert( buf != NULL );
615 if ( size > p->max_recv ) {
616 /* somebody is trying to mess me up. */
617 ber_log_printf( LDAP_DEBUG_ANY, debuglevel,
618 "sb_sasl_generic_pkt_length: "
619 "received illegal packet length of %lu bytes\n",
620 (unsigned long)size );
621 size = 16; /* this should lead to an error. */
624 return size + 4; /* include the size !!! */
627 /* Drop a processed packet from the input buffer */
629 sb_sasl_generic_drop_packet (
630 struct sb_sasl_generic_data *p,
635 len = p->sec_buf_in.buf_ptr - p->sec_buf_in.buf_end;
637 AC_MEMCPY( p->sec_buf_in.buf_base, p->sec_buf_in.buf_base +
638 p->sec_buf_in.buf_end, len );
641 p->sec_buf_in.buf_end = sb_sasl_generic_pkt_length(p,
642 (unsigned char *) p->sec_buf_in.buf_base, debuglevel);
645 p->sec_buf_in.buf_end = 0;
647 p->sec_buf_in.buf_ptr = len;
651 sb_sasl_generic_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
653 struct sb_sasl_generic_data *p;
654 ber_slen_t ret, bufptr;
656 assert( sbiod != NULL );
657 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
659 p = (struct sb_sasl_generic_data *)sbiod->sbiod_pvt;
661 /* Are there anything left in the buffer? */
662 ret = ber_pvt_sb_copy_out( &p->buf_in, buf, len );
669 p->ops->reset_buf( p, &p->buf_in );
671 /* Read the length of the packet */
672 while ( p->sec_buf_in.buf_ptr < 4 ) {
673 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
674 p->sec_buf_in.buf_ptr,
675 4 - p->sec_buf_in.buf_ptr );
677 if ( ( ret < 0 ) && ( errno == EINTR ) )
681 return bufptr ? bufptr : ret;
683 p->sec_buf_in.buf_ptr += ret;
686 /* The new packet always starts at p->sec_buf_in.buf_base */
687 ret = sb_sasl_generic_pkt_length(p, (unsigned char *) p->sec_buf_in.buf_base,
688 sbiod->sbiod_sb->sb_debug );
690 /* Grow the packet buffer if neccessary */
691 if ( ( p->sec_buf_in.buf_size < (ber_len_t) ret ) &&
692 ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 )
697 p->sec_buf_in.buf_end = ret;
699 /* Did we read the whole encrypted packet? */
700 while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) {
701 /* No, we have got only a part of it */
702 ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr;
704 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
705 p->sec_buf_in.buf_ptr, ret );
707 if ( ( ret < 0 ) && ( errno == EINTR ) )
711 return bufptr ? bufptr : ret;
713 p->sec_buf_in.buf_ptr += ret;
716 /* Decode the packet */
717 ret = p->ops->decode( p, &p->sec_buf_in, &p->buf_in );
719 /* Drop the packet from the input buffer */
720 sb_sasl_generic_drop_packet( p, sbiod->sbiod_sb->sb_debug );
723 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
724 "sb_sasl_generic_read: failed to decode packet\n" );
729 bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
735 sb_sasl_generic_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
737 struct sb_sasl_generic_data *p;
741 assert( sbiod != NULL );
742 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
744 p = (struct sb_sasl_generic_data *)sbiod->sbiod_pvt;
746 /* Is there anything left in the buffer? */
747 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
748 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
749 if ( ret < 0 ) return ret;
751 /* Still have something left?? */
752 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
758 len2 = p->max_send - 100; /* For safety margin */
759 len2 = len > len2 ? len2 : len;
761 /* If we're just retrying a partial write, tell the
762 * caller it's done. Let them call again if there's
763 * still more left to write.
765 if ( p->flags & LDAP_PVT_SASL_PARTIAL_WRITE ) {
766 p->flags ^= LDAP_PVT_SASL_PARTIAL_WRITE;
770 /* now encode the next packet. */
771 p->ops->reset_buf( p, &p->buf_out );
773 ret = p->ops->encode( p, buf, len2, &p->buf_out );
776 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
777 "sb_sasl_generic_write: failed to encode packet\n" );
782 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
786 int err = sock_errno();
787 /* caller can retry this */
788 if ( err == EAGAIN || err == EWOULDBLOCK || err == EINTR )
789 p->flags |= LDAP_PVT_SASL_PARTIAL_WRITE;
791 } else if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
792 /* partial write? pretend nothing got written */
793 p->flags |= LDAP_PVT_SASL_PARTIAL_WRITE;
798 /* return number of bytes encoded, not written, to ensure
799 * no byte is encoded twice (even if only sent once).
805 sb_sasl_generic_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
807 struct sb_sasl_generic_data *p;
809 p = (struct sb_sasl_generic_data *)sbiod->sbiod_pvt;
811 if ( opt == LBER_SB_OPT_DATA_READY ) {
812 if ( p->buf_in.buf_ptr != p->buf_in.buf_end ) return 1;
815 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
818 Sockbuf_IO ldap_pvt_sockbuf_io_sasl_generic = {
819 sb_sasl_generic_setup, /* sbi_setup */
820 sb_sasl_generic_remove, /* sbi_remove */
821 sb_sasl_generic_ctrl, /* sbi_ctrl */
822 sb_sasl_generic_read, /* sbi_read */
823 sb_sasl_generic_write, /* sbi_write */
827 int ldap_pvt_sasl_generic_install(
829 struct sb_sasl_generic_install *install_arg )
831 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_generic_install\n",
834 /* don't install the stuff unless security has been negotiated */
836 if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
837 &ldap_pvt_sockbuf_io_sasl_generic ) )
840 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
841 LBER_SBIOD_LEVEL_APPLICATION, (void *)"sasl_generic_" );
843 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl_generic,
844 LBER_SBIOD_LEVEL_APPLICATION, install_arg );
850 void ldap_pvt_sasl_generic_remove( Sockbuf *sb )
852 ber_sockbuf_remove_io( sb, &ldap_pvt_sockbuf_io_sasl_generic,
853 LBER_SBIOD_LEVEL_APPLICATION );
855 ber_sockbuf_remove_io( sb, &ber_sockbuf_io_debug,
856 LBER_SBIOD_LEVEL_APPLICATION );