]> git.sur5r.net Git - openldap/blob - libraries/libldap/cyrus.c
Merge remote-tracking branch 'origin/mdb.master'
[openldap] / libraries / libldap / cyrus.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2013 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         if( lc->lconn_sasl_cbind ) {
373                 ldap_memfree( lc->lconn_sasl_cbind );
374                 lc->lconn_sasl_cbind = NULL;
375         }
376
377         return LDAP_SUCCESS;
378 }
379
380 int
381 ldap_int_sasl_bind(
382         LDAP                    *ld,
383         const char              *dn,
384         const char              *mechs,
385         LDAPControl             **sctrls,
386         LDAPControl             **cctrls,
387         unsigned                flags,
388         LDAP_SASL_INTERACT_PROC *interact,
389         void                    *defaults,
390         LDAPMessage             *result,
391         const char              **rmech,
392         int                             *msgid )
393 {
394         const char              *mech;
395         sasl_ssf_t              *ssf;
396         sasl_conn_t             *ctx;
397         sasl_interact_t *prompts = NULL;
398         struct berval   ccred = BER_BVNULL;
399         int saslrc, rc;
400         unsigned credlen;
401
402         Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
403                 mechs ? mechs : "<null>", 0, 0 );
404
405         /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
406         if (ld->ld_version < LDAP_VERSION3) {
407                 ld->ld_errno = LDAP_NOT_SUPPORTED;
408                 return ld->ld_errno;
409         }
410
411         /* Starting a Bind */
412         if ( !result ) {
413                 const char *pmech = NULL;
414                 sasl_conn_t     *oldctx;
415                 ber_socket_t            sd;
416                 void    *ssl;
417
418                 rc = 0;
419                 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
420                 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
421
422                 if ( sd == AC_SOCKET_INVALID || !ld->ld_defconn ) {
423                         /* not connected yet */
424
425                         rc = ldap_open_defconn( ld );
426
427                         if ( rc == 0 ) {
428                                 ber_sockbuf_ctrl( ld->ld_defconn->lconn_sb,
429                                         LBER_SB_OPT_GET_FD, &sd );
430
431                                 if( sd == AC_SOCKET_INVALID ) {
432                                         ld->ld_errno = LDAP_LOCAL_ERROR;
433                                         rc = ld->ld_errno;
434                                 }
435                         }
436                 }
437                 if ( rc == 0 && ld->ld_defconn &&
438                         ld->ld_defconn->lconn_status == LDAP_CONNST_CONNECTING ) {
439                         rc = ldap_int_check_async_open( ld, sd );
440                 }
441                 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
442                 if( rc != 0 ) return ld->ld_errno;
443
444                 oldctx = ld->ld_defconn->lconn_sasl_authctx;
445
446                 /* If we already have an authentication context, clear it out */
447                 if( oldctx ) {
448                         if ( oldctx != ld->ld_defconn->lconn_sasl_sockctx ) {
449                                 sasl_dispose( &oldctx );
450                         }
451                         ld->ld_defconn->lconn_sasl_authctx = NULL;
452                 }
453
454                 {
455                         char *saslhost;
456                         int nocanon = (int)LDAP_BOOL_GET( &ld->ld_options,
457                                 LDAP_BOOL_SASL_NOCANON );
458
459                         /* If we don't need to canonicalize just use the host
460                          * from the LDAP URI.
461                          */
462                         if ( nocanon )
463                                 saslhost = ld->ld_defconn->lconn_server->lud_host;
464                         else 
465                                 saslhost = ldap_host_connected_to( ld->ld_defconn->lconn_sb,
466                                 "localhost" );
467                         rc = ldap_int_sasl_open( ld, ld->ld_defconn, saslhost );
468                         if ( !nocanon )
469                                 LDAP_FREE( saslhost );
470                 }
471
472                 if ( rc != LDAP_SUCCESS ) return rc;
473
474                 ctx = ld->ld_defconn->lconn_sasl_authctx;
475
476 #ifdef HAVE_TLS
477                 /* Check for TLS */
478                 ssl = ldap_pvt_tls_sb_ctx( ld->ld_defconn->lconn_sb );
479                 if ( ssl ) {
480                         struct berval authid = BER_BVNULL;
481                         ber_len_t fac;
482
483                         fac = ldap_pvt_tls_get_strength( ssl );
484                         /* failure is OK, we just can't use SASL EXTERNAL */
485                         (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
486
487                         (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
488                         LDAP_FREE( authid.bv_val );
489 #ifdef SASL_CHANNEL_BINDING     /* 2.1.25+ */
490                         {
491                                 char cbinding[64];
492                                 struct berval cbv = { sizeof(cbinding), cbinding };
493                                 if ( ldap_pvt_tls_get_unique( ssl, &cbv, 0 )) {
494                                         sasl_channel_binding_t *cb = ldap_memalloc( sizeof(*cb) +
495                                                 cbv.bv_len);
496                                         cb->name = "ldap";
497                                         cb->critical = 0;
498                                         cb->data = (char *)(cb+1);
499                                         cb->len = cbv.bv_len;
500                                         memcpy( cb->data, cbv.bv_val, cbv.bv_len );
501                                         sasl_setprop( ld->ld_defconn->lconn_sasl_authctx,
502                                                 SASL_CHANNEL_BINDING, cb );
503                                         ld->ld_defconn->lconn_sasl_cbind = cb;
504                                 }
505                         }
506 #endif
507                 }
508 #endif
509
510 #if !defined(_WIN32)
511                 /* Check for local */
512                 if ( ldap_pvt_url_scheme2proto(
513                         ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC )
514                 {
515                         char authid[sizeof("gidNumber=4294967295+uidNumber=4294967295,"
516                                 "cn=peercred,cn=external,cn=auth")];
517                         sprintf( authid, "gidNumber=%u+uidNumber=%u,"
518                                 "cn=peercred,cn=external,cn=auth",
519                                 getegid(), geteuid() );
520                         (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid,
521                                 LDAP_PVT_SASL_LOCAL_SSF );
522                 }
523 #endif
524
525                 /* (re)set security properties */
526                 sasl_setprop( ctx, SASL_SEC_PROPS,
527                         &ld->ld_options.ldo_sasl_secprops );
528
529                 mech = NULL;
530
531                 do {
532                         saslrc = sasl_client_start( ctx,
533                                 mechs,
534 #if SASL_VERSION_MAJOR < 2
535                                 NULL,
536 #endif
537                                 &prompts,
538                                 (SASL_CONST char **)&ccred.bv_val,
539                                 &credlen,
540                                 &mech );
541
542                         if( pmech == NULL && mech != NULL ) {
543                                 pmech = mech;
544                                 *rmech = mech;
545
546                                 if( flags != LDAP_SASL_QUIET ) {
547                                         fprintf(stderr,
548                                                 "SASL/%s authentication started\n",
549                                                 pmech );
550                                 }
551                         }
552
553                         if( saslrc == SASL_INTERACT ) {
554                                 int res;
555                                 if( !interact ) break;
556                                 res = (interact)( ld, flags, defaults, prompts );
557
558                                 if( res != LDAP_SUCCESS ) break;
559                         }
560                 } while ( saslrc == SASL_INTERACT );
561                 rc = LDAP_SASL_BIND_IN_PROGRESS;
562
563         } else {
564                 /* continuing an in-progress Bind */
565                 struct berval *scred = NULL;
566
567                 ctx = ld->ld_defconn->lconn_sasl_authctx;
568
569                 rc = ldap_parse_sasl_bind_result( ld, result, &scred, 0 );
570                 if ( rc != LDAP_SUCCESS )
571                         goto done;
572
573                 rc = ldap_result2error( ld, result, 0 );
574                 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
575                         if( scred ) {
576                                 /* and server provided us with data? */
577                                 Debug( LDAP_DEBUG_TRACE,
578                                         "ldap_int_sasl_bind: rc=%d len=%ld\n",
579                                         rc, scred ? (long) scred->bv_len : -1L, 0 );
580                                 ber_bvfree( scred );
581                                 scred = NULL;
582                         }
583                         goto done;
584                 }
585
586                 mech = *rmech;
587                 if ( rc == LDAP_SUCCESS && mech == NULL )
588                         goto success;
589
590                 do {
591                         if( ! scred ) {
592                                 /* no data! */
593                                 Debug( LDAP_DEBUG_TRACE,
594                                         "ldap_int_sasl_bind: no data in step!\n",
595                                         0, 0, 0 );
596                         }
597
598                         saslrc = sasl_client_step( ctx,
599                                 (scred == NULL) ? NULL : scred->bv_val,
600                                 (scred == NULL) ? 0 : scred->bv_len,
601                                 &prompts,
602                                 (SASL_CONST char **)&ccred.bv_val,
603                                 &credlen );
604
605                         Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
606                                 saslrc, 0, 0 );
607
608                         if( saslrc == SASL_INTERACT ) {
609                                 int res;
610                                 if( !interact ) break;
611                                 res = (interact)( ld, flags, defaults, prompts );
612                                 if( res != LDAP_SUCCESS ) break;
613                         }
614                 } while ( saslrc == SASL_INTERACT );
615
616                 ber_bvfree( scred );
617         }
618
619         if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
620                 rc = ld->ld_errno = sasl_err2ldap( saslrc );
621 #if SASL_VERSION_MAJOR >= 2
622                 if ( ld->ld_error ) {
623                         LDAP_FREE( ld->ld_error );
624                 }
625                 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
626 #endif
627                 goto done;
628         }
629
630         if ( saslrc == SASL_OK )
631                 *rmech = NULL;
632
633         ccred.bv_len = credlen;
634
635         if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
636                 rc = ldap_sasl_bind( ld, dn, mech, &ccred, sctrls, cctrls, msgid );
637
638                 if ( ccred.bv_val != NULL ) {
639 #if SASL_VERSION_MAJOR < 2
640                         LDAP_FREE( ccred.bv_val );
641 #endif
642                         ccred.bv_val = NULL;
643                 }
644                 if ( rc == LDAP_SUCCESS )
645                         rc = LDAP_SASL_BIND_IN_PROGRESS;
646                 goto done;
647         }
648
649 success:
650         /* Conversation was completed successfully by now */
651         if( flags != LDAP_SASL_QUIET ) {
652                 char *data;
653                 saslrc = sasl_getprop( ctx, SASL_USERNAME,
654                         (SASL_CONST void **)(char *) &data );
655                 if( saslrc == SASL_OK && data && *data ) {
656                         fprintf( stderr, "SASL username: %s\n", data );
657                 }
658
659 #if SASL_VERSION_MAJOR < 2
660                 saslrc = sasl_getprop( ctx, SASL_REALM,
661                         (SASL_CONST void **) &data );
662                 if( saslrc == SASL_OK && data && *data ) {
663                         fprintf( stderr, "SASL realm: %s\n", data );
664                 }
665 #endif
666         }
667
668         ssf = NULL;
669         saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **)(char *) &ssf );
670         if( saslrc == SASL_OK ) {
671                 if( flags != LDAP_SASL_QUIET ) {
672                         fprintf( stderr, "SASL SSF: %lu\n",
673                                 (unsigned long) *ssf );
674                 }
675
676                 if( ssf && *ssf ) {
677                         if ( ld->ld_defconn->lconn_sasl_sockctx ) {
678                                 sasl_conn_t     *oldctx = ld->ld_defconn->lconn_sasl_sockctx;
679                                 sasl_dispose( &oldctx );
680                                 ldap_pvt_sasl_remove( ld->ld_defconn->lconn_sb );
681                         }
682                         ldap_pvt_sasl_install( ld->ld_defconn->lconn_sb, ctx );
683                         ld->ld_defconn->lconn_sasl_sockctx = ctx;
684
685                         if( flags != LDAP_SASL_QUIET ) {
686                                 fprintf( stderr, "SASL data security layer installed.\n" );
687                         }
688                 }
689         }
690         ld->ld_defconn->lconn_sasl_authctx = ctx;
691
692 done:
693         return rc;
694 }
695
696 int
697 ldap_int_sasl_external(
698         LDAP *ld,
699         LDAPConn *conn,
700         const char * authid,
701         ber_len_t ssf )
702 {
703         int sc;
704         sasl_conn_t *ctx;
705 #if SASL_VERSION_MAJOR < 2
706         sasl_external_properties_t extprops;
707 #else
708         sasl_ssf_t sasl_ssf = ssf;
709 #endif
710
711         ctx = conn->lconn_sasl_authctx;
712
713         if ( ctx == NULL ) {
714                 return LDAP_LOCAL_ERROR;
715         }
716    
717 #if SASL_VERSION_MAJOR >= 2
718         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf );
719         if ( sc == SASL_OK )
720                 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
721 #else
722         memset( &extprops, '\0', sizeof(extprops) );
723         extprops.ssf = ssf;
724         extprops.auth_id = (char *) authid;
725
726         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
727                 (void *) &extprops );
728 #endif
729
730         if ( sc != SASL_OK ) {
731                 return LDAP_LOCAL_ERROR;
732         }
733
734         return LDAP_SUCCESS;
735 }
736
737
738 #define GOT_MINSSF      1
739 #define GOT_MAXSSF      2
740 #define GOT_MAXBUF      4
741
742 static struct {
743         struct berval key;
744         int sflag;
745         int ival;
746         int idef;
747 } sprops[] = {
748         { BER_BVC("none"), 0, 0, 0 },
749         { BER_BVC("nodict"), SASL_SEC_NODICTIONARY, 0, 0 },
750         { BER_BVC("noplain"), SASL_SEC_NOPLAINTEXT, 0, 0 },
751         { BER_BVC("noactive"), SASL_SEC_NOACTIVE, 0, 0 },
752         { BER_BVC("passcred"), SASL_SEC_PASS_CREDENTIALS, 0, 0 },
753         { BER_BVC("forwardsec"), SASL_SEC_FORWARD_SECRECY, 0, 0 },
754         { BER_BVC("noanonymous"), SASL_SEC_NOANONYMOUS, 0, 0 },
755         { BER_BVC("minssf="), 0, GOT_MINSSF, 0 },
756         { BER_BVC("maxssf="), 0, GOT_MAXSSF, INT_MAX },
757         { BER_BVC("maxbufsize="), 0, GOT_MAXBUF, 65536 },
758         { BER_BVNULL, 0, 0, 0 }
759 };
760
761 void ldap_pvt_sasl_secprops_unparse(
762         sasl_security_properties_t *secprops,
763         struct berval *out )
764 {
765         int i, l = 0;
766         int comma;
767         char *ptr;
768
769         if ( secprops == NULL || out == NULL ) {
770                 return;
771         }
772
773         comma = 0;
774         for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
775                 if ( sprops[i].ival ) {
776                         int v = 0;
777
778                         switch( sprops[i].ival ) {
779                         case GOT_MINSSF: v = secprops->min_ssf; break;
780                         case GOT_MAXSSF: v = secprops->max_ssf; break;
781                         case GOT_MAXBUF: v = secprops->maxbufsize; break;
782                         }
783                         /* It is the default, ignore it */
784                         if ( v == sprops[i].idef ) continue;
785
786                         l += sprops[i].key.bv_len + 24;
787                 } else if ( sprops[i].sflag ) {
788                         if ( sprops[i].sflag & secprops->security_flags ) {
789                                 l += sprops[i].key.bv_len;
790                         }
791                 } else if ( secprops->security_flags == 0 ) {
792                         l += sprops[i].key.bv_len;
793                 }
794                 if ( comma ) l++;
795                 comma = 1;
796         }
797         l++;
798
799         out->bv_val = LDAP_MALLOC( l );
800         if ( out->bv_val == NULL ) {
801                 out->bv_len = 0;
802                 return;
803         }
804
805         ptr = out->bv_val;
806         comma = 0;
807         for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
808                 if ( sprops[i].ival ) {
809                         int v = 0;
810
811                         switch( sprops[i].ival ) {
812                         case GOT_MINSSF: v = secprops->min_ssf; break;
813                         case GOT_MAXSSF: v = secprops->max_ssf; break;
814                         case GOT_MAXBUF: v = secprops->maxbufsize; break;
815                         }
816                         /* It is the default, ignore it */
817                         if ( v == sprops[i].idef ) continue;
818
819                         if ( comma ) *ptr++ = ',';
820                         ptr += sprintf(ptr, "%s%d", sprops[i].key.bv_val, v );
821                         comma = 1;
822                 } else if ( sprops[i].sflag ) {
823                         if ( sprops[i].sflag & secprops->security_flags ) {
824                                 if ( comma ) *ptr++ = ',';
825                                 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
826                                 comma = 1;
827                         }
828                 } else if ( secprops->security_flags == 0 ) {
829                         if ( comma ) *ptr++ = ',';
830                         ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
831                         comma = 1;
832                 }
833         }
834         out->bv_len = ptr - out->bv_val;
835 }
836
837 int ldap_pvt_sasl_secprops(
838         const char *in,
839         sasl_security_properties_t *secprops )
840 {
841         unsigned i, j, l;
842         char **props;
843         unsigned sflags = 0;
844         int got_sflags = 0;
845         sasl_ssf_t max_ssf = 0;
846         int got_max_ssf = 0;
847         sasl_ssf_t min_ssf = 0;
848         int got_min_ssf = 0;
849         unsigned maxbufsize = 0;
850         int got_maxbufsize = 0;
851
852         if( secprops == NULL ) {
853                 return LDAP_PARAM_ERROR;
854         }
855         props = ldap_str2charray( in, "," );
856         if( props == NULL ) {
857                 return LDAP_PARAM_ERROR;
858         }
859
860         for( i=0; props[i]; i++ ) {
861                 l = strlen( props[i] );
862                 for ( j=0; !BER_BVISNULL( &sprops[j].key ); j++ ) {
863                         if ( l < sprops[j].key.bv_len ) continue;
864                         if ( strncasecmp( props[i], sprops[j].key.bv_val,
865                                 sprops[j].key.bv_len )) continue;
866                         if ( sprops[j].ival ) {
867                                 unsigned v;
868                                 char *next = NULL;
869                                 if ( !isdigit( (unsigned char)props[i][sprops[j].key.bv_len] ))
870                                         continue;
871                                 v = strtoul( &props[i][sprops[j].key.bv_len], &next, 10 );
872                                 if ( next == &props[i][sprops[j].key.bv_len] || next[0] != '\0' ) continue;
873                                 switch( sprops[j].ival ) {
874                                 case GOT_MINSSF:
875                                         min_ssf = v; got_min_ssf++; break;
876                                 case GOT_MAXSSF:
877                                         max_ssf = v; got_max_ssf++; break;
878                                 case GOT_MAXBUF:
879                                         maxbufsize = v; got_maxbufsize++; break;
880                                 }
881                         } else {
882                                 if ( props[i][sprops[j].key.bv_len] ) continue;
883                                 if ( sprops[j].sflag )
884                                         sflags |= sprops[j].sflag;
885                                 else
886                                         sflags = 0;
887                                 got_sflags++;
888                         }
889                         break;
890                 }
891                 if ( BER_BVISNULL( &sprops[j].key )) {
892                         ldap_charray_free( props );
893                         return LDAP_NOT_SUPPORTED;
894                 }
895         }
896
897         if(got_sflags) {
898                 secprops->security_flags = sflags;
899         }
900         if(got_min_ssf) {
901                 secprops->min_ssf = min_ssf;
902         }
903         if(got_max_ssf) {
904                 secprops->max_ssf = max_ssf;
905         }
906         if(got_maxbufsize) {
907                 secprops->maxbufsize = maxbufsize;
908         }
909
910         ldap_charray_free( props );
911         return LDAP_SUCCESS;
912 }
913
914 int
915 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
916 {
917         int rc;
918
919         switch( option ) {
920         case LDAP_OPT_X_SASL_SECPROPS:
921                 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
922                 if( rc == LDAP_SUCCESS ) return 0;
923         }
924
925         return -1;
926 }
927
928 int
929 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
930 {
931         if ( option == LDAP_OPT_X_SASL_MECHLIST ) {
932                 if ( ldap_int_sasl_init() )
933                         return -1;
934                 *(char ***)arg = (char **)sasl_global_listmech();
935                 return 0;
936         }
937
938         if ( ld == NULL )
939                 return -1;
940
941         switch ( option ) {
942                 case LDAP_OPT_X_SASL_MECH: {
943                         *(char **)arg = ld->ld_options.ldo_def_sasl_mech
944                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
945                 } break;
946                 case LDAP_OPT_X_SASL_REALM: {
947                         *(char **)arg = ld->ld_options.ldo_def_sasl_realm
948                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
949                 } break;
950                 case LDAP_OPT_X_SASL_AUTHCID: {
951                         *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
952                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
953                 } break;
954                 case LDAP_OPT_X_SASL_AUTHZID: {
955                         *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
956                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
957                 } break;
958
959                 case LDAP_OPT_X_SASL_SSF: {
960                         int sc;
961                         sasl_ssf_t      *ssf;
962                         sasl_conn_t *ctx;
963
964                         if( ld->ld_defconn == NULL ) {
965                                 return -1;
966                         }
967
968                         ctx = ld->ld_defconn->lconn_sasl_sockctx;
969
970                         if ( ctx == NULL ) {
971                                 return -1;
972                         }
973
974                         sc = sasl_getprop( ctx, SASL_SSF,
975                                 (SASL_CONST void **)(char *) &ssf );
976
977                         if ( sc != SASL_OK ) {
978                                 return -1;
979                         }
980
981                         *(ber_len_t *)arg = *ssf;
982                 } break;
983
984                 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
985                         /* this option is write only */
986                         return -1;
987
988                 case LDAP_OPT_X_SASL_SSF_MIN:
989                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
990                         break;
991                 case LDAP_OPT_X_SASL_SSF_MAX:
992                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
993                         break;
994                 case LDAP_OPT_X_SASL_MAXBUFSIZE:
995                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
996                         break;
997                 case LDAP_OPT_X_SASL_NOCANON:
998                         *(int *)arg = (int) LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
999                         break;
1000
1001                 case LDAP_OPT_X_SASL_USERNAME: {
1002                         int sc;
1003                         char *username;
1004                         sasl_conn_t *ctx;
1005
1006                         if( ld->ld_defconn == NULL ) {
1007                                 return -1;
1008                         }
1009
1010                         ctx = ld->ld_defconn->lconn_sasl_authctx;
1011
1012                         if ( ctx == NULL ) {
1013                                 return -1;
1014                         }
1015
1016                         sc = sasl_getprop( ctx, SASL_USERNAME,
1017                                 (SASL_CONST void **)(char **) &username );
1018
1019                         if ( sc != SASL_OK ) {
1020                                 return -1;
1021                         }
1022
1023                         *(char **)arg = username ? LDAP_STRDUP( username ) : NULL;
1024                 } break;
1025
1026                 case LDAP_OPT_X_SASL_SECPROPS:
1027                         /* this option is write only */
1028                         return -1;
1029
1030 #ifdef SASL_GSS_CREDS
1031                 case LDAP_OPT_X_SASL_GSS_CREDS: {
1032                         sasl_conn_t *ctx;
1033                         int sc;
1034
1035                         if ( ld->ld_defconn == NULL )
1036                                 return -1;
1037
1038                         ctx = ld->ld_defconn->lconn_sasl_authctx;
1039                         if ( ctx == NULL )
1040                                 return -1;
1041
1042                         sc = sasl_getprop( ctx, SASL_GSS_CREDS, arg );
1043                         if ( sc != SASL_OK )
1044                                 return -1;
1045                         }
1046                         break;
1047 #endif
1048
1049                 default:
1050                         return -1;
1051         }
1052         return 0;
1053 }
1054
1055 int
1056 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1057 {
1058         if ( ld == NULL )
1059                 return -1;
1060
1061         if ( arg == NULL && option != LDAP_OPT_X_SASL_NOCANON )
1062                 return -1;
1063
1064         switch ( option ) {
1065         case LDAP_OPT_X_SASL_SSF:
1066         case LDAP_OPT_X_SASL_USERNAME:
1067                 /* This option is read-only */
1068                 return -1;
1069
1070         case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1071                 int sc;
1072 #if SASL_VERSION_MAJOR < 2
1073                 sasl_external_properties_t extprops;
1074 #else
1075                 sasl_ssf_t sasl_ssf;
1076 #endif
1077                 sasl_conn_t *ctx;
1078
1079                 if( ld->ld_defconn == NULL ) {
1080                         return -1;
1081                 }
1082
1083                 ctx = ld->ld_defconn->lconn_sasl_authctx;
1084
1085                 if ( ctx == NULL ) {
1086                         return -1;
1087                 }
1088
1089 #if SASL_VERSION_MAJOR >= 2
1090                 sasl_ssf = * (ber_len_t *)arg;
1091                 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf);
1092 #else
1093                 memset(&extprops, 0L, sizeof(extprops));
1094
1095                 extprops.ssf = * (ber_len_t *) arg;
1096
1097                 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1098                         (void *) &extprops );
1099 #endif
1100
1101                 if ( sc != SASL_OK ) {
1102                         return -1;
1103                 }
1104                 } break;
1105
1106         case LDAP_OPT_X_SASL_SSF_MIN:
1107                 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1108                 break;
1109         case LDAP_OPT_X_SASL_SSF_MAX:
1110                 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1111                 break;
1112         case LDAP_OPT_X_SASL_MAXBUFSIZE:
1113                 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1114                 break;
1115         case LDAP_OPT_X_SASL_NOCANON:
1116                 if ( arg == LDAP_OPT_OFF ) {
1117                         LDAP_BOOL_CLR(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1118                 } else {
1119                         LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1120                 }
1121                 break;
1122
1123         case LDAP_OPT_X_SASL_SECPROPS: {
1124                 int sc;
1125                 sc = ldap_pvt_sasl_secprops( (char *) arg,
1126                         &ld->ld_options.ldo_sasl_secprops );
1127
1128                 return sc == LDAP_SUCCESS ? 0 : -1;
1129                 }
1130
1131 #ifdef SASL_GSS_CREDS
1132         case LDAP_OPT_X_SASL_GSS_CREDS: {
1133                 sasl_conn_t *ctx;
1134                 int sc;
1135
1136                 if ( ld->ld_defconn == NULL )
1137                         return -1;
1138
1139                 ctx = ld->ld_defconn->lconn_sasl_authctx;
1140                 if ( ctx == NULL )
1141                         return -1;
1142
1143                 sc = sasl_setprop( ctx, SASL_GSS_CREDS, arg );
1144                 if ( sc != SASL_OK )
1145                         return -1;
1146                 }
1147                 break;
1148 #endif
1149
1150         default:
1151                 return -1;
1152         }
1153         return 0;
1154 }
1155
1156 #ifdef LDAP_R_COMPILE
1157 #define LDAP_DEBUG_R_SASL
1158 void *ldap_pvt_sasl_mutex_new(void)
1159 {
1160         ldap_pvt_thread_mutex_t *mutex;
1161
1162         mutex = (ldap_pvt_thread_mutex_t *) LDAP_CALLOC( 1,
1163                 sizeof(ldap_pvt_thread_mutex_t) );
1164
1165         if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1166                 return mutex;
1167         }
1168 #ifndef LDAP_DEBUG_R_SASL
1169         assert( 0 );
1170 #endif /* !LDAP_DEBUG_R_SASL */
1171         return NULL;
1172 }
1173
1174 int ldap_pvt_sasl_mutex_lock(void *mutex)
1175 {
1176 #ifdef LDAP_DEBUG_R_SASL
1177         if ( mutex == NULL ) {
1178                 return SASL_OK;
1179         }
1180 #else /* !LDAP_DEBUG_R_SASL */
1181         assert( mutex != NULL );
1182 #endif /* !LDAP_DEBUG_R_SASL */
1183         return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1184                 ? SASL_FAIL : SASL_OK;
1185 }
1186
1187 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1188 {
1189 #ifdef LDAP_DEBUG_R_SASL
1190         if ( mutex == NULL ) {
1191                 return SASL_OK;
1192         }
1193 #else /* !LDAP_DEBUG_R_SASL */
1194         assert( mutex != NULL );
1195 #endif /* !LDAP_DEBUG_R_SASL */
1196         return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1197                 ? SASL_FAIL : SASL_OK;
1198 }
1199
1200 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1201 {
1202 #ifdef LDAP_DEBUG_R_SASL
1203         if ( mutex == NULL ) {
1204                 return;
1205         }
1206 #else /* !LDAP_DEBUG_R_SASL */
1207         assert( mutex != NULL );
1208 #endif /* !LDAP_DEBUG_R_SASL */
1209         (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1210         LDAP_FREE( mutex );
1211 }
1212 #endif
1213
1214 #else
1215 int ldap_int_sasl_init( void )
1216 { return LDAP_SUCCESS; }
1217
1218 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1219 { return LDAP_SUCCESS; }
1220
1221 int
1222 ldap_int_sasl_bind(
1223         LDAP                    *ld,
1224         const char              *dn,
1225         const char              *mechs,
1226         LDAPControl             **sctrls,
1227         LDAPControl             **cctrls,
1228         unsigned                flags,
1229         LDAP_SASL_INTERACT_PROC *interact,
1230         void                    *defaults,
1231         LDAPMessage             *result,
1232         const char              **rmech,
1233         int                             *msgid )
1234 { return LDAP_NOT_SUPPORTED; }
1235
1236 int
1237 ldap_int_sasl_external(
1238         LDAP *ld,
1239         LDAPConn *conn,
1240         const char * authid,
1241         ber_len_t ssf )
1242 { return LDAP_SUCCESS; }
1243
1244 #endif /* HAVE_CYRUS_SASL */