2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2015 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>
51 LDAP_CONST char *mechanism,
60 if( mechanism == LDAP_SASL_SIMPLE ) {
61 if( dn == NULL && cred != NULL && cred->bv_len ) {
62 /* use default binddn */
63 dn = ld->ld_defbinddn;
66 } else if( ld->ld_version < LDAP_VERSION3 ) {
67 ld->ld_errno = LDAP_NOT_SUPPORTED;
75 /* create a message to send */
76 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
80 LDAP_NEXT_MSGID( ld, *msgidp );
81 if( mechanism == LDAP_SASL_SIMPLE ) {
83 rc = ber_printf( ber, "{it{istON}" /*}*/,
84 *msgidp, LDAP_REQ_BIND,
85 ld->ld_version, dn, LDAP_AUTH_SIMPLE,
88 } else if ( cred == NULL || cred->bv_val == NULL ) {
89 /* SASL bind w/o credentials */
90 rc = ber_printf( ber, "{it{ist{sN}N}" /*}*/,
91 *msgidp, LDAP_REQ_BIND,
92 ld->ld_version, dn, LDAP_AUTH_SASL,
96 /* SASL bind w/ credentials */
97 rc = ber_printf( ber, "{it{ist{sON}N}" /*}*/,
98 *msgidp, LDAP_REQ_BIND,
99 ld->ld_version, dn, LDAP_AUTH_SASL,
104 ld->ld_errno = LDAP_ENCODING_ERROR;
109 /* Put Server Controls */
110 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
115 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
116 ld->ld_errno = LDAP_ENCODING_ERROR;
125 * ldap_sasl_bind - bind to the ldap server (and X.500).
126 * The dn (usually NULL), mechanism, and credentials are provided.
127 * The message id of the request initiated is provided upon successful
128 * (LDAP_SUCCESS) return.
131 * ldap_sasl_bind( ld, NULL, "mechanism",
132 * cred, NULL, NULL, &msgid )
139 LDAP_CONST char *mechanism,
141 LDAPControl **sctrls,
142 LDAPControl **cctrls,
149 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind\n", 0, 0, 0 );
151 assert( ld != NULL );
152 assert( LDAP_VALID( ld ) );
153 assert( msgidp != NULL );
155 /* check client controls */
156 rc = ldap_int_client_controls( ld, cctrls );
157 if( rc != LDAP_SUCCESS ) return rc;
159 ber = ldap_build_bind_req( ld, dn, mechanism, cred, sctrls, cctrls, &id );
163 /* send the message */
164 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber, id );
177 LDAP_CONST char *mechanism,
179 LDAPControl **sctrls,
180 LDAPControl **cctrls,
181 struct berval **servercredp )
185 struct berval *scredp = NULL;
187 Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind_s\n", 0, 0, 0 );
189 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
190 if( servercredp != NULL ) {
191 if (ld->ld_version < LDAP_VERSION3) {
192 ld->ld_errno = LDAP_NOT_SUPPORTED;
198 rc = ldap_sasl_bind( ld, dn, mechanism, cred, sctrls, cctrls, &msgid );
200 if ( rc != LDAP_SUCCESS ) {
204 #ifdef LDAP_CONNECTIONLESS
205 if (LDAP_IS_UDP(ld)) {
210 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 || !result ) {
211 return( ld->ld_errno ); /* ldap_result sets ld_errno */
214 /* parse the results */
216 if( servercredp != NULL ) {
217 rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 );
220 if ( rc != LDAP_SUCCESS ) {
221 ldap_msgfree( result );
225 rc = ldap_result2error( ld, result, 1 );
227 if ( rc == LDAP_SUCCESS || rc == LDAP_SASL_BIND_IN_PROGRESS ) {
228 if( servercredp != NULL ) {
229 *servercredp = scredp;
234 if ( scredp != NULL ) {
243 * Parse BindResponse:
245 * BindResponse ::= [APPLICATION 1] SEQUENCE {
246 * COMPONENTS OF LDAPResult,
247 * serverSaslCreds [7] OCTET STRING OPTIONAL }
249 * LDAPResult ::= SEQUENCE {
250 * resultCode ENUMERATED,
252 * errorMessage LDAPString,
253 * referral [3] Referral OPTIONAL }
257 ldap_parse_sasl_bind_result(
260 struct berval **servercredp,
264 struct berval* scred;
269 Debug( LDAP_DEBUG_TRACE, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
271 assert( ld != NULL );
272 assert( LDAP_VALID( ld ) );
273 assert( res != NULL );
275 if( servercredp != NULL ) {
276 if( ld->ld_version < LDAP_VERSION2 ) {
277 return LDAP_NOT_SUPPORTED;
282 if( res->lm_msgtype != LDAP_RES_BIND ) {
283 ld->ld_errno = LDAP_PARAM_ERROR;
289 if ( ld->ld_error ) {
290 LDAP_FREE( ld->ld_error );
293 if ( ld->ld_matched ) {
294 LDAP_FREE( ld->ld_matched );
295 ld->ld_matched = NULL;
300 ber = ber_dup( res->lm_ber );
303 ld->ld_errno = LDAP_NO_MEMORY;
307 if ( ld->ld_version < LDAP_VERSION2 ) {
308 tag = ber_scanf( ber, "{iA}",
309 &errcode, &ld->ld_error );
311 if( tag == LBER_ERROR ) {
313 ld->ld_errno = LDAP_DECODING_ERROR;
320 tag = ber_scanf( ber, "{eAA" /*}*/,
321 &errcode, &ld->ld_matched, &ld->ld_error );
323 if( tag == LBER_ERROR ) {
325 ld->ld_errno = LDAP_DECODING_ERROR;
329 tag = ber_peek_tag(ber, &len);
331 if( tag == LDAP_TAG_REFERRAL ) {
333 if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
335 ld->ld_errno = LDAP_DECODING_ERROR;
339 tag = ber_peek_tag(ber, &len);
342 if( tag == LDAP_TAG_SASL_RES_CREDS ) {
343 if( ber_scanf( ber, "O", &scred ) == LBER_ERROR ) {
345 ld->ld_errno = LDAP_DECODING_ERROR;
353 if ( servercredp != NULL ) {
354 *servercredp = scred;
356 } else if ( scred != NULL ) {
360 ld->ld_errno = errcode;
366 return( LDAP_SUCCESS );
370 ldap_pvt_sasl_getmechs ( LDAP *ld, char **pmechlist )
372 /* we need to query the server for supported mechs anyway */
373 LDAPMessage *res, *e;
374 char *attrs[] = { "supportedSASLMechanisms", NULL };
375 char **values, *mechlist;
378 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
380 rc = ldap_search_s( ld, "", LDAP_SCOPE_BASE,
381 NULL, attrs, 0, &res );
383 if ( rc != LDAP_SUCCESS ) {
387 e = ldap_first_entry( ld, res );
390 if ( ld->ld_errno == LDAP_SUCCESS ) {
391 ld->ld_errno = LDAP_NO_SUCH_OBJECT;
396 values = ldap_get_values( ld, e, "supportedSASLMechanisms" );
397 if ( values == NULL ) {
399 ld->ld_errno = LDAP_NO_SUCH_ATTRIBUTE;
403 mechlist = ldap_charray2str( values, " " );
404 if ( mechlist == NULL ) {
405 LDAP_VFREE( values );
407 ld->ld_errno = LDAP_NO_MEMORY;
411 LDAP_VFREE( values );
414 *pmechlist = mechlist;
420 * ldap_sasl_interactive_bind - interactive SASL authentication
422 * This routine uses interactive callbacks.
424 * LDAP_SUCCESS is returned upon success, the ldap error code
425 * otherwise. LDAP_SASL_BIND_IN_PROGRESS is returned if further
429 ldap_sasl_interactive_bind(
431 LDAP_CONST char *dn, /* usually NULL */
432 LDAP_CONST char *mechs,
433 LDAPControl **serverControls,
434 LDAPControl **clientControls,
436 LDAP_SASL_INTERACT_PROC *interact,
445 #ifdef LDAP_CONNECTIONLESS
446 if( LDAP_IS_UDP(ld) ) {
447 /* Just force it to simple bind, silly to make the user
448 * ask all the time. No, we don't ever actually bind, but I'll
449 * let the final bind handler take care of saving the cdn.
451 rc = ldap_simple_bind( ld, dn, NULL );
452 rc = rc < 0 ? rc : 0;
460 #ifdef HAVE_CYRUS_SASL
461 if( mechs == NULL || *mechs == '\0' ) {
462 mechs = ld->ld_options.ldo_def_sasl_mech;
466 if( mechs == NULL || *mechs == '\0' ) {
467 /* FIXME: this needs to be asynchronous too;
468 * perhaps NULL should be disallowed for async usage?
470 rc = ldap_pvt_sasl_getmechs( ld, &smechs );
471 if( rc != LDAP_SUCCESS ) {
475 Debug( LDAP_DEBUG_TRACE,
476 "ldap_sasl_interactive_bind: server supports: %s\n",
482 Debug( LDAP_DEBUG_TRACE,
483 "ldap_sasl_interactive_bind: user selected: %s\n",
487 rc = ldap_int_sasl_bind( ld, dn, mechs,
488 serverControls, clientControls,
489 flags, interact, defaults, result, rmech, msgid );
492 if ( smechs ) LDAP_FREE( smechs );
498 * ldap_sasl_interactive_bind_s - interactive SASL authentication
500 * This routine uses interactive callbacks.
502 * LDAP_SUCCESS is returned upon success, the ldap error code
506 ldap_sasl_interactive_bind_s(
508 LDAP_CONST char *dn, /* usually NULL */
509 LDAP_CONST char *mechs,
510 LDAPControl **serverControls,
511 LDAPControl **clientControls,
513 LDAP_SASL_INTERACT_PROC *interact,
516 const char *rmech = NULL;
517 LDAPMessage *result = NULL;
521 rc = ldap_sasl_interactive_bind( ld, dn, mechs,
522 serverControls, clientControls,
523 flags, interact, defaults, result, &rmech, &msgid );
525 ldap_msgfree( result );
527 if ( rc != LDAP_SASL_BIND_IN_PROGRESS )
530 #ifdef LDAP_CONNECTIONLESS
531 if (LDAP_IS_UDP(ld)) {
536 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 || !result ) {
537 return( ld->ld_errno ); /* ldap_result sets ld_errno */
539 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
544 #ifdef HAVE_CYRUS_SASL
546 #ifdef HAVE_SASL_SASL_H
547 #include <sasl/sasl.h>
552 #endif /* HAVE_CYRUS_SASL */
555 sb_sasl_generic_remove( Sockbuf_IO_Desc *sbiod );
558 sb_sasl_generic_setup( Sockbuf_IO_Desc *sbiod, void *arg )
560 struct sb_sasl_generic_data *p;
561 struct sb_sasl_generic_install *i;
563 assert( sbiod != NULL );
565 i = (struct sb_sasl_generic_install *)arg;
567 p = LBER_MALLOC( sizeof( *p ) );
571 p->ops_private = i->ops_private;
574 ber_pvt_sb_buf_init( &p->sec_buf_in );
575 ber_pvt_sb_buf_init( &p->buf_in );
576 ber_pvt_sb_buf_init( &p->buf_out );
578 sbiod->sbiod_pvt = p;
580 p->ops->init( p, &p->min_send, &p->max_send, &p->max_recv );
582 if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, p->min_send ) < 0 ) {
583 sb_sasl_generic_remove( sbiod );
592 sb_sasl_generic_remove( Sockbuf_IO_Desc *sbiod )
594 struct sb_sasl_generic_data *p;
596 assert( sbiod != NULL );
598 p = (struct sb_sasl_generic_data *)sbiod->sbiod_pvt;
602 ber_pvt_sb_buf_destroy( &p->sec_buf_in );
603 ber_pvt_sb_buf_destroy( &p->buf_in );
604 ber_pvt_sb_buf_destroy( &p->buf_out );
606 sbiod->sbiod_pvt = NULL;
611 sb_sasl_generic_pkt_length(
612 struct sb_sasl_generic_data *p,
613 const unsigned char *buf,
618 assert( buf != NULL );
625 if ( size > p->max_recv ) {
626 /* somebody is trying to mess me up. */
627 ber_log_printf( LDAP_DEBUG_ANY, debuglevel,
628 "sb_sasl_generic_pkt_length: "
629 "received illegal packet length of %lu bytes\n",
630 (unsigned long)size );
631 size = 16; /* this should lead to an error. */
634 return size + 4; /* include the size !!! */
637 /* Drop a processed packet from the input buffer */
639 sb_sasl_generic_drop_packet (
640 struct sb_sasl_generic_data *p,
645 len = p->sec_buf_in.buf_ptr - p->sec_buf_in.buf_end;
647 AC_MEMCPY( p->sec_buf_in.buf_base, p->sec_buf_in.buf_base +
648 p->sec_buf_in.buf_end, len );
651 p->sec_buf_in.buf_end = sb_sasl_generic_pkt_length(p,
652 (unsigned char *) p->sec_buf_in.buf_base, debuglevel);
655 p->sec_buf_in.buf_end = 0;
657 p->sec_buf_in.buf_ptr = len;
661 sb_sasl_generic_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
663 struct sb_sasl_generic_data *p;
664 ber_slen_t ret, bufptr;
666 assert( sbiod != NULL );
667 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
669 p = (struct sb_sasl_generic_data *)sbiod->sbiod_pvt;
671 /* Are there anything left in the buffer? */
672 ret = ber_pvt_sb_copy_out( &p->buf_in, buf, len );
679 p->ops->reset_buf( p, &p->buf_in );
681 /* Read the length of the packet */
682 while ( p->sec_buf_in.buf_ptr < 4 ) {
683 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
684 p->sec_buf_in.buf_ptr,
685 4 - p->sec_buf_in.buf_ptr );
687 if ( ( ret < 0 ) && ( errno == EINTR ) )
691 return bufptr ? bufptr : ret;
693 p->sec_buf_in.buf_ptr += ret;
696 /* The new packet always starts at p->sec_buf_in.buf_base */
697 ret = sb_sasl_generic_pkt_length(p, (unsigned char *) p->sec_buf_in.buf_base,
698 sbiod->sbiod_sb->sb_debug );
700 /* Grow the packet buffer if neccessary */
701 if ( ( p->sec_buf_in.buf_size < (ber_len_t) ret ) &&
702 ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 )
707 p->sec_buf_in.buf_end = ret;
709 /* Did we read the whole encrypted packet? */
710 while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) {
711 /* No, we have got only a part of it */
712 ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr;
714 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
715 p->sec_buf_in.buf_ptr, ret );
717 if ( ( ret < 0 ) && ( errno == EINTR ) )
721 return bufptr ? bufptr : ret;
723 p->sec_buf_in.buf_ptr += ret;
726 /* Decode the packet */
727 ret = p->ops->decode( p, &p->sec_buf_in, &p->buf_in );
729 /* Drop the packet from the input buffer */
730 sb_sasl_generic_drop_packet( p, sbiod->sbiod_sb->sb_debug );
733 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
734 "sb_sasl_generic_read: failed to decode packet\n" );
739 bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
745 sb_sasl_generic_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
747 struct sb_sasl_generic_data *p;
751 assert( sbiod != NULL );
752 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
754 p = (struct sb_sasl_generic_data *)sbiod->sbiod_pvt;
756 /* Is there anything left in the buffer? */
757 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
758 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
759 if ( ret < 0 ) return ret;
761 /* Still have something left?? */
762 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
768 len2 = p->max_send - 100; /* For safety margin */
769 len2 = len > len2 ? len2 : len;
771 /* If we're just retrying a partial write, tell the
772 * caller it's done. Let them call again if there's
773 * still more left to write.
775 if ( p->flags & LDAP_PVT_SASL_PARTIAL_WRITE ) {
776 p->flags ^= LDAP_PVT_SASL_PARTIAL_WRITE;
780 /* now encode the next packet. */
781 p->ops->reset_buf( p, &p->buf_out );
783 ret = p->ops->encode( p, buf, len2, &p->buf_out );
786 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
787 "sb_sasl_generic_write: failed to encode packet\n" );
792 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
796 int err = sock_errno();
797 /* caller can retry this */
798 if ( err == EAGAIN || err == EWOULDBLOCK || err == EINTR )
799 p->flags |= LDAP_PVT_SASL_PARTIAL_WRITE;
801 } else if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
802 /* partial write? pretend nothing got written */
803 p->flags |= LDAP_PVT_SASL_PARTIAL_WRITE;
808 /* return number of bytes encoded, not written, to ensure
809 * no byte is encoded twice (even if only sent once).
815 sb_sasl_generic_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
817 struct sb_sasl_generic_data *p;
819 p = (struct sb_sasl_generic_data *)sbiod->sbiod_pvt;
821 if ( opt == LBER_SB_OPT_DATA_READY ) {
822 if ( p->buf_in.buf_ptr != p->buf_in.buf_end ) return 1;
825 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
828 Sockbuf_IO ldap_pvt_sockbuf_io_sasl_generic = {
829 sb_sasl_generic_setup, /* sbi_setup */
830 sb_sasl_generic_remove, /* sbi_remove */
831 sb_sasl_generic_ctrl, /* sbi_ctrl */
832 sb_sasl_generic_read, /* sbi_read */
833 sb_sasl_generic_write, /* sbi_write */
837 int ldap_pvt_sasl_generic_install(
839 struct sb_sasl_generic_install *install_arg )
841 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_generic_install\n",
844 /* don't install the stuff unless security has been negotiated */
846 if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
847 &ldap_pvt_sockbuf_io_sasl_generic ) )
850 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
851 LBER_SBIOD_LEVEL_APPLICATION, (void *)"sasl_generic_" );
853 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl_generic,
854 LBER_SBIOD_LEVEL_APPLICATION, install_arg );
860 void ldap_pvt_sasl_generic_remove( Sockbuf *sb )
862 ber_sockbuf_remove_io( sb, &ldap_pvt_sockbuf_io_sasl_generic,
863 LBER_SBIOD_LEVEL_APPLICATION );
865 ber_sockbuf_remove_io( sb, &ber_sockbuf_io_debug,
866 LBER_SBIOD_LEVEL_APPLICATION );