]> git.sur5r.net Git - openldap/blob - libraries/libldap/cyrus.c
Prep for release
[openldap] / libraries / libldap / cyrus.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2015 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19
20 #include <ac/socket.h>
21 #include <ac/stdlib.h>
22 #include <ac/string.h>
23 #include <ac/time.h>
24 #include <ac/errno.h>
25 #include <ac/ctype.h>
26 #include <ac/unistd.h>
27
28 #ifdef HAVE_LIMITS_H
29 #include <limits.h>
30 #endif
31
32 #include "ldap-int.h"
33
34 #ifdef HAVE_CYRUS_SASL
35
36 #ifdef HAVE_LIMITS_H
37 #include <limits.h>
38 #endif
39
40 #ifndef INT_MAX
41 #define INT_MAX 2147483647      /* 32 bit signed max */
42 #endif
43
44 #ifdef HAVE_SASL_SASL_H
45 #include <sasl/sasl.h>
46 #else
47 #include <sasl.h>
48 #endif
49
50 #if SASL_VERSION_MAJOR >= 2
51 #define SASL_CONST const
52 #else
53 #define SASL_CONST
54 #endif
55
56 /*
57 * Various Cyrus SASL related stuff.
58 */
59
60 static const sasl_callback_t client_callbacks[] = {
61 #ifdef SASL_CB_GETREALM
62         { SASL_CB_GETREALM, NULL, NULL },
63 #endif
64         { SASL_CB_USER, NULL, NULL },
65         { SASL_CB_AUTHNAME, NULL, NULL },
66         { SASL_CB_PASS, NULL, NULL },
67         { SASL_CB_ECHOPROMPT, NULL, NULL },
68         { SASL_CB_NOECHOPROMPT, NULL, NULL },
69         { SASL_CB_LIST_END, NULL, NULL }
70 };
71
72 int ldap_int_sasl_init( void )
73 {
74         /* XXX not threadsafe */
75         static int sasl_initialized = 0;
76
77 #ifdef HAVE_SASL_VERSION
78         /* stringify the version number, sasl.h doesn't do it for us */
79 #define VSTR0(maj, min, pat)    #maj "." #min "." #pat
80 #define VSTR(maj, min, pat)     VSTR0(maj, min, pat)
81 #define SASL_VERSION_STRING     VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
82                                 SASL_VERSION_STEP)
83         { int rc;
84         sasl_version( NULL, &rc );
85         if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
86                 (rc & 0xffff) < SASL_VERSION_STEP) {
87                 char version[sizeof("xxx.xxx.xxxxx")];
88                 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
89                         rc & 0xffff );
90
91                 Debug( LDAP_DEBUG_ANY,
92                 "ldap_int_sasl_init: SASL library version mismatch:"
93                 " expected " SASL_VERSION_STRING ","
94                 " got %s\n", version, 0, 0 );
95                 return -1;
96         }
97         }
98 #endif
99         if ( sasl_initialized ) {
100                 return 0;
101         }
102
103 /* SASL 2 takes care of its own memory completely internally */
104 #if SASL_VERSION_MAJOR < 2 && !defined(CSRIMALLOC)
105         sasl_set_alloc(
106                 ber_memalloc,
107                 ber_memcalloc,
108                 ber_memrealloc,
109                 ber_memfree );
110 #endif /* CSRIMALLOC */
111
112 #ifdef LDAP_R_COMPILE
113         sasl_set_mutex(
114                 ldap_pvt_sasl_mutex_new,
115                 ldap_pvt_sasl_mutex_lock,
116                 ldap_pvt_sasl_mutex_unlock,    
117                 ldap_pvt_sasl_mutex_dispose );    
118 #endif
119
120         if ( sasl_client_init( NULL ) == SASL_OK ) {
121                 sasl_initialized = 1;
122                 return 0;
123         }
124
125 #if SASL_VERSION_MAJOR < 2
126         /* A no-op to make sure we link with Cyrus 1.5 */
127         sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
128 #endif
129         return -1;
130 }
131
132 static void
133 sb_sasl_cyrus_init(
134         struct sb_sasl_generic_data *p,
135         ber_len_t *min_send,
136         ber_len_t *max_send,
137         ber_len_t *max_recv)
138 {
139         sasl_conn_t *sasl_context = (sasl_conn_t *)p->ops_private;
140         ber_len_t maxbuf;
141
142         sasl_getprop( sasl_context, SASL_MAXOUTBUF,
143                       (SASL_CONST void **)(char *) &maxbuf );
144
145         *min_send = SASL_MIN_BUFF_SIZE;
146         *max_send = maxbuf;
147         *max_recv = SASL_MAX_BUFF_SIZE;
148 }
149
150 static ber_int_t
151 sb_sasl_cyrus_encode(
152         struct sb_sasl_generic_data *p,
153         unsigned char *buf,
154         ber_len_t len,
155         Sockbuf_Buf *dst)
156 {
157         sasl_conn_t *sasl_context = (sasl_conn_t *)p->ops_private;
158         ber_int_t ret;
159         unsigned tmpsize = dst->buf_size;
160
161         ret = sasl_encode( sasl_context, (char *)buf, len,
162                            (SASL_CONST char **)&dst->buf_base,
163                            &tmpsize );
164
165         dst->buf_size = tmpsize;
166         dst->buf_end = dst->buf_size;
167
168         if ( ret != SASL_OK ) {
169                 ber_log_printf( LDAP_DEBUG_ANY, p->sbiod->sbiod_sb->sb_debug,
170                                 "sb_sasl_cyrus_encode: failed to encode packet: %s\n",
171                                 sasl_errstring( ret, NULL, NULL ) );
172                 return -1;
173         }
174
175         return 0;
176 }
177
178 static ber_int_t
179 sb_sasl_cyrus_decode(
180         struct sb_sasl_generic_data *p,
181         const Sockbuf_Buf *src,
182         Sockbuf_Buf *dst)
183 {
184         sasl_conn_t *sasl_context = (sasl_conn_t *)p->ops_private;
185         ber_int_t ret;
186         unsigned tmpsize = dst->buf_size;
187
188         ret = sasl_decode( sasl_context,
189                            src->buf_base, src->buf_end,
190                            (SASL_CONST char **)&dst->buf_base,
191                            (unsigned *)&tmpsize );
192
193
194         dst->buf_size = tmpsize;
195         dst->buf_end = dst->buf_size;
196
197         if ( ret != SASL_OK ) {
198                 ber_log_printf( LDAP_DEBUG_ANY, p->sbiod->sbiod_sb->sb_debug,
199                                 "sb_sasl_cyrus_decode: failed to decode packet: %s\n",
200                                 sasl_errstring( ret, NULL, NULL ) );
201                 return -1;
202         }
203
204         return 0;
205 }
206
207 static void
208 sb_sasl_cyrus_reset_buf(
209         struct sb_sasl_generic_data *p,
210         Sockbuf_Buf *buf)
211 {
212 #if SASL_VERSION_MAJOR >= 2
213         ber_pvt_sb_buf_init( buf );
214 #else
215         ber_pvt_sb_buf_destroy( buf );
216 #endif
217 }
218
219 static void
220 sb_sasl_cyrus_fini(
221         struct sb_sasl_generic_data *p)
222 {
223 #if SASL_VERSION_MAJOR >= 2
224         /*
225          * SASLv2 encode/decode buffers are managed by
226          * libsasl2. Ensure they are not freed by liblber.
227          */
228         p->buf_in.buf_base = NULL;
229         p->buf_out.buf_base = NULL;
230 #endif
231 }
232
233 static const struct sb_sasl_generic_ops sb_sasl_cyrus_ops = {
234         sb_sasl_cyrus_init,
235         sb_sasl_cyrus_encode,
236         sb_sasl_cyrus_decode,
237         sb_sasl_cyrus_reset_buf,
238         sb_sasl_cyrus_fini
239  };
240
241 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
242 {
243         struct sb_sasl_generic_install install_arg;
244
245         install_arg.ops         = &sb_sasl_cyrus_ops;
246         install_arg.ops_private = ctx_arg;
247
248         return ldap_pvt_sasl_generic_install( sb, &install_arg );
249 }
250
251 void ldap_pvt_sasl_remove( Sockbuf *sb )
252 {
253         ldap_pvt_sasl_generic_remove( sb );
254 }
255
256 static int
257 sasl_err2ldap( int saslerr )
258 {
259         int rc;
260
261         /* map SASL errors to LDAP API errors returned by:
262          *      sasl_client_new()
263          *              SASL_OK, SASL_NOMECH, SASL_NOMEM
264          *      sasl_client_start()
265          *              SASL_OK, SASL_NOMECH, SASL_NOMEM, SASL_INTERACT
266          *      sasl_client_step()
267          *              SASL_OK, SASL_INTERACT, SASL_BADPROT, SASL_BADSERV
268          */
269
270         switch (saslerr) {
271                 case SASL_CONTINUE:
272                         rc = LDAP_MORE_RESULTS_TO_RETURN;
273                         break;
274                 case SASL_INTERACT:
275                         rc = LDAP_LOCAL_ERROR;
276                         break;
277                 case SASL_OK:
278                         rc = LDAP_SUCCESS;
279                         break;
280                 case SASL_NOMEM:
281                         rc = LDAP_NO_MEMORY;
282                         break;
283                 case SASL_NOMECH:
284                         rc = LDAP_AUTH_UNKNOWN;
285                         break;
286                 case SASL_BADPROT:
287                         rc = LDAP_DECODING_ERROR;
288                         break;
289                 case SASL_BADSERV:
290                         rc = LDAP_AUTH_UNKNOWN;
291                         break;
292
293                 /* other codes */
294                 case SASL_BADAUTH:
295                         rc = LDAP_AUTH_UNKNOWN;
296                         break;
297                 case SASL_NOAUTHZ:
298                         rc = LDAP_PARAM_ERROR;
299                         break;
300                 case SASL_FAIL:
301                         rc = LDAP_LOCAL_ERROR;
302                         break;
303                 case SASL_TOOWEAK:
304                 case SASL_ENCRYPT:
305                         rc = LDAP_AUTH_UNKNOWN;
306                         break;
307                 default:
308                         rc = LDAP_LOCAL_ERROR;
309                         break;
310         }
311
312         assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
313         return rc;
314 }
315
316 int
317 ldap_int_sasl_open(
318         LDAP *ld, 
319         LDAPConn *lc,
320         const char * host )
321 {
322         int rc;
323         sasl_conn_t *ctx;
324
325         assert( lc->lconn_sasl_authctx == NULL );
326
327         if ( host == NULL ) {
328                 ld->ld_errno = LDAP_LOCAL_ERROR;
329                 return ld->ld_errno;
330         }
331
332         if ( ldap_int_sasl_init() ) {
333                 ld->ld_errno = LDAP_LOCAL_ERROR;
334                 return ld->ld_errno;
335         }
336
337 #if SASL_VERSION_MAJOR >= 2
338         rc = sasl_client_new( "ldap", host, NULL, NULL,
339                 client_callbacks, 0, &ctx );
340 #else
341         rc = sasl_client_new( "ldap", host, client_callbacks,
342                 SASL_SECURITY_LAYER, &ctx );
343 #endif
344
345         if ( rc != SASL_OK ) {
346                 ld->ld_errno = sasl_err2ldap( rc );
347                 return ld->ld_errno;
348         }
349
350         Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: host=%s\n",
351                 host, 0, 0 );
352
353         lc->lconn_sasl_authctx = ctx;
354
355         return LDAP_SUCCESS;
356 }
357
358 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
359 {
360         sasl_conn_t *ctx = lc->lconn_sasl_authctx;
361
362         if( ctx != NULL ) {
363                 sasl_dispose( &ctx );
364                 if ( lc->lconn_sasl_sockctx &&
365                         lc->lconn_sasl_authctx != lc->lconn_sasl_sockctx ) {
366                         ctx = lc->lconn_sasl_sockctx;
367                         sasl_dispose( &ctx );
368                 }
369                 lc->lconn_sasl_sockctx = NULL;
370                 lc->lconn_sasl_authctx = NULL;
371         }
372
373         return LDAP_SUCCESS;
374 }
375
376 int
377 ldap_int_sasl_bind(
378         LDAP                    *ld,
379         const char              *dn,
380         const char              *mechs,
381         LDAPControl             **sctrls,
382         LDAPControl             **cctrls,
383         unsigned                flags,
384         LDAP_SASL_INTERACT_PROC *interact,
385         void                    *defaults,
386         LDAPMessage             *result,
387         const char              **rmech,
388         int                             *msgid )
389 {
390         const char              *mech;
391         sasl_ssf_t              *ssf;
392         sasl_conn_t             *ctx;
393         sasl_interact_t *prompts = NULL;
394         struct berval   ccred = BER_BVNULL;
395         int saslrc, rc;
396         unsigned credlen;
397
398         Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
399                 mechs ? mechs : "<null>", 0, 0 );
400
401         /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
402         if (ld->ld_version < LDAP_VERSION3) {
403                 ld->ld_errno = LDAP_NOT_SUPPORTED;
404                 return ld->ld_errno;
405         }
406
407         /* Starting a Bind */
408         if ( !result ) {
409                 const char *pmech = NULL;
410                 sasl_conn_t     *oldctx;
411                 ber_socket_t            sd;
412                 void    *ssl;
413
414                 rc = 0;
415                 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
416                 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
417
418                 if ( sd == AC_SOCKET_INVALID || !ld->ld_defconn ) {
419                         /* not connected yet */
420
421                         rc = ldap_open_defconn( ld );
422
423                         if ( rc == 0 ) {
424                                 ber_sockbuf_ctrl( ld->ld_defconn->lconn_sb,
425                                         LBER_SB_OPT_GET_FD, &sd );
426
427                                 if( sd == AC_SOCKET_INVALID ) {
428                                         ld->ld_errno = LDAP_LOCAL_ERROR;
429                                         rc = ld->ld_errno;
430                                 }
431                         }
432                 }
433                 if ( rc == 0 && ld->ld_defconn &&
434                         ld->ld_defconn->lconn_status == LDAP_CONNST_CONNECTING ) {
435                         rc = ldap_int_check_async_open( ld, sd );
436                 }
437                 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
438                 if( rc != 0 ) return ld->ld_errno;
439
440                 oldctx = ld->ld_defconn->lconn_sasl_authctx;
441
442                 /* If we already have an authentication context, clear it out */
443                 if( oldctx ) {
444                         if ( oldctx != ld->ld_defconn->lconn_sasl_sockctx ) {
445                                 sasl_dispose( &oldctx );
446                         }
447                         ld->ld_defconn->lconn_sasl_authctx = NULL;
448                 }
449
450                 {
451                         char *saslhost;
452                         int nocanon = (int)LDAP_BOOL_GET( &ld->ld_options,
453                                 LDAP_BOOL_SASL_NOCANON );
454
455                         /* If we don't need to canonicalize just use the host
456                          * from the LDAP URI.
457                          */
458                         if ( nocanon )
459                                 saslhost = ld->ld_defconn->lconn_server->lud_host;
460                         else 
461                                 saslhost = ldap_host_connected_to( ld->ld_defconn->lconn_sb,
462                                 "localhost" );
463                         rc = ldap_int_sasl_open( ld, ld->ld_defconn, saslhost );
464                         if ( !nocanon )
465                                 LDAP_FREE( saslhost );
466                 }
467
468                 if ( rc != LDAP_SUCCESS ) return rc;
469
470                 ctx = ld->ld_defconn->lconn_sasl_authctx;
471
472 #ifdef HAVE_TLS
473                 /* Check for TLS */
474                 ssl = ldap_pvt_tls_sb_ctx( ld->ld_defconn->lconn_sb );
475                 if ( ssl ) {
476                         struct berval authid = BER_BVNULL;
477                         ber_len_t fac;
478
479                         fac = ldap_pvt_tls_get_strength( ssl );
480                         /* failure is OK, we just can't use SASL EXTERNAL */
481                         (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
482
483                         (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
484                         LDAP_FREE( authid.bv_val );
485                 }
486 #endif
487
488 #if !defined(_WIN32)
489                 /* Check for local */
490                 if ( ldap_pvt_url_scheme2proto(
491                         ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC )
492                 {
493                         char authid[sizeof("gidNumber=4294967295+uidNumber=4294967295,"
494                                 "cn=peercred,cn=external,cn=auth")];
495                         sprintf( authid, "gidNumber=%u+uidNumber=%u,"
496                                 "cn=peercred,cn=external,cn=auth",
497                                 getegid(), geteuid() );
498                         (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid,
499                                 LDAP_PVT_SASL_LOCAL_SSF );
500                 }
501 #endif
502
503                 /* (re)set security properties */
504                 sasl_setprop( ctx, SASL_SEC_PROPS,
505                         &ld->ld_options.ldo_sasl_secprops );
506
507                 mech = NULL;
508
509                 do {
510                         saslrc = sasl_client_start( ctx,
511                                 mechs,
512 #if SASL_VERSION_MAJOR < 2
513                                 NULL,
514 #endif
515                                 &prompts,
516                                 (SASL_CONST char **)&ccred.bv_val,
517                                 &credlen,
518                                 &mech );
519
520                         if( pmech == NULL && mech != NULL ) {
521                                 pmech = mech;
522                                 *rmech = mech;
523
524                                 if( flags != LDAP_SASL_QUIET ) {
525                                         fprintf(stderr,
526                                                 "SASL/%s authentication started\n",
527                                                 pmech );
528                                 }
529                         }
530
531                         if( saslrc == SASL_INTERACT ) {
532                                 int res;
533                                 if( !interact ) break;
534                                 res = (interact)( ld, flags, defaults, prompts );
535
536                                 if( res != LDAP_SUCCESS ) break;
537                         }
538                 } while ( saslrc == SASL_INTERACT );
539                 rc = LDAP_SASL_BIND_IN_PROGRESS;
540
541         } else {
542                 /* continuing an in-progress Bind */
543                 struct berval *scred = NULL;
544
545                 ctx = ld->ld_defconn->lconn_sasl_authctx;
546
547                 rc = ldap_parse_sasl_bind_result( ld, result, &scred, 0 );
548                 if ( rc != LDAP_SUCCESS ) {
549                         if ( scred )
550                                 ber_bvfree( scred );
551                         goto done;
552                 }
553
554                 rc = ldap_result2error( ld, result, 0 );
555                 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
556                         if( scred ) {
557                                 /* and server provided us with data? */
558                                 Debug( LDAP_DEBUG_TRACE,
559                                         "ldap_int_sasl_bind: rc=%d len=%ld\n",
560                                         rc, scred ? (long) scred->bv_len : -1L, 0 );
561                                 ber_bvfree( scred );
562                                 scred = NULL;
563                         }
564                         goto done;
565                 }
566
567                 mech = *rmech;
568                 if ( rc == LDAP_SUCCESS && mech == NULL ) {
569                         if ( scred )
570                                 ber_bvfree( scred );
571                         goto success;
572                 }
573
574                 do {
575                         if( ! scred ) {
576                                 /* no data! */
577                                 Debug( LDAP_DEBUG_TRACE,
578                                         "ldap_int_sasl_bind: no data in step!\n",
579                                         0, 0, 0 );
580                         }
581
582                         saslrc = sasl_client_step( ctx,
583                                 (scred == NULL) ? NULL : scred->bv_val,
584                                 (scred == NULL) ? 0 : scred->bv_len,
585                                 &prompts,
586                                 (SASL_CONST char **)&ccred.bv_val,
587                                 &credlen );
588
589                         Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
590                                 saslrc, 0, 0 );
591
592                         if( saslrc == SASL_INTERACT ) {
593                                 int res;
594                                 if( !interact ) break;
595                                 res = (interact)( ld, flags, defaults, prompts );
596                                 if( res != LDAP_SUCCESS ) break;
597                         }
598                 } while ( saslrc == SASL_INTERACT );
599
600                 ber_bvfree( scred );
601         }
602
603         if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
604                 rc = ld->ld_errno = sasl_err2ldap( saslrc );
605 #if SASL_VERSION_MAJOR >= 2
606                 if ( ld->ld_error ) {
607                         LDAP_FREE( ld->ld_error );
608                 }
609                 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
610 #endif
611                 goto done;
612         }
613
614         if ( saslrc == SASL_OK )
615                 *rmech = NULL;
616
617         ccred.bv_len = credlen;
618
619         if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
620                 rc = ldap_sasl_bind( ld, dn, mech, &ccred, sctrls, cctrls, msgid );
621
622                 if ( ccred.bv_val != NULL ) {
623 #if SASL_VERSION_MAJOR < 2
624                         LDAP_FREE( ccred.bv_val );
625 #endif
626                         ccred.bv_val = NULL;
627                 }
628                 if ( rc == LDAP_SUCCESS )
629                         rc = LDAP_SASL_BIND_IN_PROGRESS;
630                 goto done;
631         }
632
633 success:
634         /* Conversation was completed successfully by now */
635         if( flags != LDAP_SASL_QUIET ) {
636                 char *data;
637                 saslrc = sasl_getprop( ctx, SASL_USERNAME,
638                         (SASL_CONST void **)(char *) &data );
639                 if( saslrc == SASL_OK && data && *data ) {
640                         fprintf( stderr, "SASL username: %s\n", data );
641                 }
642
643 #if SASL_VERSION_MAJOR < 2
644                 saslrc = sasl_getprop( ctx, SASL_REALM,
645                         (SASL_CONST void **) &data );
646                 if( saslrc == SASL_OK && data && *data ) {
647                         fprintf( stderr, "SASL realm: %s\n", data );
648                 }
649 #endif
650         }
651
652         ssf = NULL;
653         saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **)(char *) &ssf );
654         if( saslrc == SASL_OK ) {
655                 if( flags != LDAP_SASL_QUIET ) {
656                         fprintf( stderr, "SASL SSF: %lu\n",
657                                 (unsigned long) *ssf );
658                 }
659
660                 if( ssf && *ssf ) {
661                         if ( ld->ld_defconn->lconn_sasl_sockctx ) {
662                                 sasl_conn_t     *oldctx = ld->ld_defconn->lconn_sasl_sockctx;
663                                 sasl_dispose( &oldctx );
664                                 ldap_pvt_sasl_remove( ld->ld_defconn->lconn_sb );
665                         }
666                         ldap_pvt_sasl_install( ld->ld_defconn->lconn_sb, ctx );
667                         ld->ld_defconn->lconn_sasl_sockctx = ctx;
668
669                         if( flags != LDAP_SASL_QUIET ) {
670                                 fprintf( stderr, "SASL data security layer installed.\n" );
671                         }
672                 }
673         }
674         ld->ld_defconn->lconn_sasl_authctx = ctx;
675
676 done:
677         return rc;
678 }
679
680 int
681 ldap_int_sasl_external(
682         LDAP *ld,
683         LDAPConn *conn,
684         const char * authid,
685         ber_len_t ssf )
686 {
687         int sc;
688         sasl_conn_t *ctx;
689 #if SASL_VERSION_MAJOR < 2
690         sasl_external_properties_t extprops;
691 #else
692         sasl_ssf_t sasl_ssf = ssf;
693 #endif
694
695         ctx = conn->lconn_sasl_authctx;
696
697         if ( ctx == NULL ) {
698                 return LDAP_LOCAL_ERROR;
699         }
700    
701 #if SASL_VERSION_MAJOR >= 2
702         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf );
703         if ( sc == SASL_OK )
704                 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
705 #else
706         memset( &extprops, '\0', sizeof(extprops) );
707         extprops.ssf = ssf;
708         extprops.auth_id = (char *) authid;
709
710         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
711                 (void *) &extprops );
712 #endif
713
714         if ( sc != SASL_OK ) {
715                 return LDAP_LOCAL_ERROR;
716         }
717
718         return LDAP_SUCCESS;
719 }
720
721
722 #define GOT_MINSSF      1
723 #define GOT_MAXSSF      2
724 #define GOT_MAXBUF      4
725
726 static struct {
727         struct berval key;
728         int sflag;
729         int ival;
730         int idef;
731 } sprops[] = {
732         { BER_BVC("none"), 0, 0, 0 },
733         { BER_BVC("nodict"), SASL_SEC_NODICTIONARY, 0, 0 },
734         { BER_BVC("noplain"), SASL_SEC_NOPLAINTEXT, 0, 0 },
735         { BER_BVC("noactive"), SASL_SEC_NOACTIVE, 0, 0 },
736         { BER_BVC("passcred"), SASL_SEC_PASS_CREDENTIALS, 0, 0 },
737         { BER_BVC("forwardsec"), SASL_SEC_FORWARD_SECRECY, 0, 0 },
738         { BER_BVC("noanonymous"), SASL_SEC_NOANONYMOUS, 0, 0 },
739         { BER_BVC("minssf="), 0, GOT_MINSSF, 0 },
740         { BER_BVC("maxssf="), 0, GOT_MAXSSF, INT_MAX },
741         { BER_BVC("maxbufsize="), 0, GOT_MAXBUF, 65536 },
742         { BER_BVNULL, 0, 0, 0 }
743 };
744
745 void ldap_pvt_sasl_secprops_unparse(
746         sasl_security_properties_t *secprops,
747         struct berval *out )
748 {
749         int i, l = 0;
750         int comma;
751         char *ptr;
752
753         if ( secprops == NULL || out == NULL ) {
754                 return;
755         }
756
757         comma = 0;
758         for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
759                 if ( sprops[i].ival ) {
760                         int v = 0;
761
762                         switch( sprops[i].ival ) {
763                         case GOT_MINSSF: v = secprops->min_ssf; break;
764                         case GOT_MAXSSF: v = secprops->max_ssf; break;
765                         case GOT_MAXBUF: v = secprops->maxbufsize; break;
766                         }
767                         /* It is the default, ignore it */
768                         if ( v == sprops[i].idef ) continue;
769
770                         l += sprops[i].key.bv_len + 24;
771                 } else if ( sprops[i].sflag ) {
772                         if ( sprops[i].sflag & secprops->security_flags ) {
773                                 l += sprops[i].key.bv_len;
774                         }
775                 } else if ( secprops->security_flags == 0 ) {
776                         l += sprops[i].key.bv_len;
777                 }
778                 if ( comma ) l++;
779                 comma = 1;
780         }
781         l++;
782
783         out->bv_val = LDAP_MALLOC( l );
784         if ( out->bv_val == NULL ) {
785                 out->bv_len = 0;
786                 return;
787         }
788
789         ptr = out->bv_val;
790         comma = 0;
791         for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
792                 if ( sprops[i].ival ) {
793                         int v = 0;
794
795                         switch( sprops[i].ival ) {
796                         case GOT_MINSSF: v = secprops->min_ssf; break;
797                         case GOT_MAXSSF: v = secprops->max_ssf; break;
798                         case GOT_MAXBUF: v = secprops->maxbufsize; break;
799                         }
800                         /* It is the default, ignore it */
801                         if ( v == sprops[i].idef ) continue;
802
803                         if ( comma ) *ptr++ = ',';
804                         ptr += sprintf(ptr, "%s%d", sprops[i].key.bv_val, v );
805                         comma = 1;
806                 } else if ( sprops[i].sflag ) {
807                         if ( sprops[i].sflag & secprops->security_flags ) {
808                                 if ( comma ) *ptr++ = ',';
809                                 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
810                                 comma = 1;
811                         }
812                 } else if ( secprops->security_flags == 0 ) {
813                         if ( comma ) *ptr++ = ',';
814                         ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
815                         comma = 1;
816                 }
817         }
818         out->bv_len = ptr - out->bv_val;
819 }
820
821 int ldap_pvt_sasl_secprops(
822         const char *in,
823         sasl_security_properties_t *secprops )
824 {
825         unsigned i, j, l;
826         char **props;
827         unsigned sflags = 0;
828         int got_sflags = 0;
829         sasl_ssf_t max_ssf = 0;
830         int got_max_ssf = 0;
831         sasl_ssf_t min_ssf = 0;
832         int got_min_ssf = 0;
833         unsigned maxbufsize = 0;
834         int got_maxbufsize = 0;
835
836         if( secprops == NULL ) {
837                 return LDAP_PARAM_ERROR;
838         }
839         props = ldap_str2charray( in, "," );
840         if( props == NULL ) {
841                 return LDAP_PARAM_ERROR;
842         }
843
844         for( i=0; props[i]; i++ ) {
845                 l = strlen( props[i] );
846                 for ( j=0; !BER_BVISNULL( &sprops[j].key ); j++ ) {
847                         if ( l < sprops[j].key.bv_len ) continue;
848                         if ( strncasecmp( props[i], sprops[j].key.bv_val,
849                                 sprops[j].key.bv_len )) continue;
850                         if ( sprops[j].ival ) {
851                                 unsigned v;
852                                 char *next = NULL;
853                                 if ( !isdigit( (unsigned char)props[i][sprops[j].key.bv_len] ))
854                                         continue;
855                                 v = strtoul( &props[i][sprops[j].key.bv_len], &next, 10 );
856                                 if ( next == &props[i][sprops[j].key.bv_len] || next[0] != '\0' ) continue;
857                                 switch( sprops[j].ival ) {
858                                 case GOT_MINSSF:
859                                         min_ssf = v; got_min_ssf++; break;
860                                 case GOT_MAXSSF:
861                                         max_ssf = v; got_max_ssf++; break;
862                                 case GOT_MAXBUF:
863                                         maxbufsize = v; got_maxbufsize++; break;
864                                 }
865                         } else {
866                                 if ( props[i][sprops[j].key.bv_len] ) continue;
867                                 if ( sprops[j].sflag )
868                                         sflags |= sprops[j].sflag;
869                                 else
870                                         sflags = 0;
871                                 got_sflags++;
872                         }
873                         break;
874                 }
875                 if ( BER_BVISNULL( &sprops[j].key )) {
876                         ldap_charray_free( props );
877                         return LDAP_NOT_SUPPORTED;
878                 }
879         }
880
881         if(got_sflags) {
882                 secprops->security_flags = sflags;
883         }
884         if(got_min_ssf) {
885                 secprops->min_ssf = min_ssf;
886         }
887         if(got_max_ssf) {
888                 secprops->max_ssf = max_ssf;
889         }
890         if(got_maxbufsize) {
891                 secprops->maxbufsize = maxbufsize;
892         }
893
894         ldap_charray_free( props );
895         return LDAP_SUCCESS;
896 }
897
898 int
899 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
900 {
901         int rc;
902
903         switch( option ) {
904         case LDAP_OPT_X_SASL_SECPROPS:
905                 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
906                 if( rc == LDAP_SUCCESS ) return 0;
907         }
908
909         return -1;
910 }
911
912 int
913 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
914 {
915         if ( option == LDAP_OPT_X_SASL_MECHLIST ) {
916                 if ( ldap_int_sasl_init() )
917                         return -1;
918                 *(char ***)arg = (char **)sasl_global_listmech();
919                 return 0;
920         }
921
922         if ( ld == NULL )
923                 return -1;
924
925         switch ( option ) {
926                 case LDAP_OPT_X_SASL_MECH: {
927                         *(char **)arg = ld->ld_options.ldo_def_sasl_mech
928                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
929                 } break;
930                 case LDAP_OPT_X_SASL_REALM: {
931                         *(char **)arg = ld->ld_options.ldo_def_sasl_realm
932                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
933                 } break;
934                 case LDAP_OPT_X_SASL_AUTHCID: {
935                         *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
936                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
937                 } break;
938                 case LDAP_OPT_X_SASL_AUTHZID: {
939                         *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
940                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
941                 } break;
942
943                 case LDAP_OPT_X_SASL_SSF: {
944                         int sc;
945                         sasl_ssf_t      *ssf;
946                         sasl_conn_t *ctx;
947
948                         if( ld->ld_defconn == NULL ) {
949                                 return -1;
950                         }
951
952                         ctx = ld->ld_defconn->lconn_sasl_sockctx;
953
954                         if ( ctx == NULL ) {
955                                 return -1;
956                         }
957
958                         sc = sasl_getprop( ctx, SASL_SSF,
959                                 (SASL_CONST void **)(char *) &ssf );
960
961                         if ( sc != SASL_OK ) {
962                                 return -1;
963                         }
964
965                         *(ber_len_t *)arg = *ssf;
966                 } break;
967
968                 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
969                         /* this option is write only */
970                         return -1;
971
972                 case LDAP_OPT_X_SASL_SSF_MIN:
973                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
974                         break;
975                 case LDAP_OPT_X_SASL_SSF_MAX:
976                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
977                         break;
978                 case LDAP_OPT_X_SASL_MAXBUFSIZE:
979                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
980                         break;
981                 case LDAP_OPT_X_SASL_NOCANON:
982                         *(int *)arg = (int) LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
983                         break;
984
985                 case LDAP_OPT_X_SASL_USERNAME: {
986                         int sc;
987                         char *username;
988                         sasl_conn_t *ctx;
989
990                         if( ld->ld_defconn == NULL ) {
991                                 return -1;
992                         }
993
994                         ctx = ld->ld_defconn->lconn_sasl_authctx;
995
996                         if ( ctx == NULL ) {
997                                 return -1;
998                         }
999
1000                         sc = sasl_getprop( ctx, SASL_USERNAME,
1001                                 (SASL_CONST void **)(char **) &username );
1002
1003                         if ( sc != SASL_OK ) {
1004                                 return -1;
1005                         }
1006
1007                         *(char **)arg = username ? LDAP_STRDUP( username ) : NULL;
1008                 } break;
1009
1010                 case LDAP_OPT_X_SASL_SECPROPS:
1011                         /* this option is write only */
1012                         return -1;
1013
1014 #ifdef SASL_GSS_CREDS
1015                 case LDAP_OPT_X_SASL_GSS_CREDS: {
1016                         sasl_conn_t *ctx;
1017                         int sc;
1018
1019                         if ( ld->ld_defconn == NULL )
1020                                 return -1;
1021
1022                         ctx = ld->ld_defconn->lconn_sasl_authctx;
1023                         if ( ctx == NULL )
1024                                 return -1;
1025
1026                         sc = sasl_getprop( ctx, SASL_GSS_CREDS, arg );
1027                         if ( sc != SASL_OK )
1028                                 return -1;
1029                         }
1030                         break;
1031 #endif
1032
1033                 default:
1034                         return -1;
1035         }
1036         return 0;
1037 }
1038
1039 int
1040 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1041 {
1042         if ( ld == NULL )
1043                 return -1;
1044
1045         if ( arg == NULL && option != LDAP_OPT_X_SASL_NOCANON )
1046                 return -1;
1047
1048         switch ( option ) {
1049         case LDAP_OPT_X_SASL_SSF:
1050         case LDAP_OPT_X_SASL_USERNAME:
1051                 /* This option is read-only */
1052                 return -1;
1053
1054         case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1055                 int sc;
1056 #if SASL_VERSION_MAJOR < 2
1057                 sasl_external_properties_t extprops;
1058 #else
1059                 sasl_ssf_t sasl_ssf;
1060 #endif
1061                 sasl_conn_t *ctx;
1062
1063                 if( ld->ld_defconn == NULL ) {
1064                         return -1;
1065                 }
1066
1067                 ctx = ld->ld_defconn->lconn_sasl_authctx;
1068
1069                 if ( ctx == NULL ) {
1070                         return -1;
1071                 }
1072
1073 #if SASL_VERSION_MAJOR >= 2
1074                 sasl_ssf = * (ber_len_t *)arg;
1075                 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf);
1076 #else
1077                 memset(&extprops, 0L, sizeof(extprops));
1078
1079                 extprops.ssf = * (ber_len_t *) arg;
1080
1081                 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1082                         (void *) &extprops );
1083 #endif
1084
1085                 if ( sc != SASL_OK ) {
1086                         return -1;
1087                 }
1088                 } break;
1089
1090         case LDAP_OPT_X_SASL_SSF_MIN:
1091                 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1092                 break;
1093         case LDAP_OPT_X_SASL_SSF_MAX:
1094                 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1095                 break;
1096         case LDAP_OPT_X_SASL_MAXBUFSIZE:
1097                 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1098                 break;
1099         case LDAP_OPT_X_SASL_NOCANON:
1100                 if ( arg == LDAP_OPT_OFF ) {
1101                         LDAP_BOOL_CLR(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1102                 } else {
1103                         LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1104                 }
1105                 break;
1106
1107         case LDAP_OPT_X_SASL_SECPROPS: {
1108                 int sc;
1109                 sc = ldap_pvt_sasl_secprops( (char *) arg,
1110                         &ld->ld_options.ldo_sasl_secprops );
1111
1112                 return sc == LDAP_SUCCESS ? 0 : -1;
1113                 }
1114
1115 #ifdef SASL_GSS_CREDS
1116         case LDAP_OPT_X_SASL_GSS_CREDS: {
1117                 sasl_conn_t *ctx;
1118                 int sc;
1119
1120                 if ( ld->ld_defconn == NULL )
1121                         return -1;
1122
1123                 ctx = ld->ld_defconn->lconn_sasl_authctx;
1124                 if ( ctx == NULL )
1125                         return -1;
1126
1127                 sc = sasl_setprop( ctx, SASL_GSS_CREDS, arg );
1128                 if ( sc != SASL_OK )
1129                         return -1;
1130                 }
1131                 break;
1132 #endif
1133
1134         default:
1135                 return -1;
1136         }
1137         return 0;
1138 }
1139
1140 #ifdef LDAP_R_COMPILE
1141 #define LDAP_DEBUG_R_SASL
1142 void *ldap_pvt_sasl_mutex_new(void)
1143 {
1144         ldap_pvt_thread_mutex_t *mutex;
1145
1146         mutex = (ldap_pvt_thread_mutex_t *) LDAP_CALLOC( 1,
1147                 sizeof(ldap_pvt_thread_mutex_t) );
1148
1149         if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1150                 return mutex;
1151         }
1152         LDAP_FREE( mutex );
1153 #ifndef LDAP_DEBUG_R_SASL
1154         assert( 0 );
1155 #endif /* !LDAP_DEBUG_R_SASL */
1156         return NULL;
1157 }
1158
1159 int ldap_pvt_sasl_mutex_lock(void *mutex)
1160 {
1161 #ifdef LDAP_DEBUG_R_SASL
1162         if ( mutex == NULL ) {
1163                 return SASL_OK;
1164         }
1165 #else /* !LDAP_DEBUG_R_SASL */
1166         assert( mutex != NULL );
1167 #endif /* !LDAP_DEBUG_R_SASL */
1168         return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1169                 ? SASL_FAIL : SASL_OK;
1170 }
1171
1172 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1173 {
1174 #ifdef LDAP_DEBUG_R_SASL
1175         if ( mutex == NULL ) {
1176                 return SASL_OK;
1177         }
1178 #else /* !LDAP_DEBUG_R_SASL */
1179         assert( mutex != NULL );
1180 #endif /* !LDAP_DEBUG_R_SASL */
1181         return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1182                 ? SASL_FAIL : SASL_OK;
1183 }
1184
1185 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1186 {
1187 #ifdef LDAP_DEBUG_R_SASL
1188         if ( mutex == NULL ) {
1189                 return;
1190         }
1191 #else /* !LDAP_DEBUG_R_SASL */
1192         assert( mutex != NULL );
1193 #endif /* !LDAP_DEBUG_R_SASL */
1194         (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1195         LDAP_FREE( mutex );
1196 }
1197 #endif
1198
1199 #else
1200 int ldap_int_sasl_init( void )
1201 { return LDAP_SUCCESS; }
1202
1203 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1204 { return LDAP_SUCCESS; }
1205
1206 int
1207 ldap_int_sasl_bind(
1208         LDAP                    *ld,
1209         const char              *dn,
1210         const char              *mechs,
1211         LDAPControl             **sctrls,
1212         LDAPControl             **cctrls,
1213         unsigned                flags,
1214         LDAP_SASL_INTERACT_PROC *interact,
1215         void                    *defaults,
1216         LDAPMessage             *result,
1217         const char              **rmech,
1218         int                             *msgid )
1219 { return LDAP_NOT_SUPPORTED; }
1220
1221 int
1222 ldap_int_sasl_external(
1223         LDAP *ld,
1224         LDAPConn *conn,
1225         const char * authid,
1226         ber_len_t ssf )
1227 { return LDAP_SUCCESS; }
1228
1229 #endif /* HAVE_CYRUS_SASL */