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