]> git.sur5r.net Git - openldap/blob - servers/slapd/sasl.c
Update Cyrus SASL detection to always look for <sasl.h> regardless
[openldap] / servers / slapd / sasl.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10 #include <ac/stdlib.h>
11 #include <ac/string.h>
12
13 #include <lber.h>
14 #include <ldap_log.h>
15
16 #include "slap.h"
17
18 #ifdef HAVE_CYRUS_SASL
19 #include <limits.h>
20
21 #include <sasl.h>
22
23 #if SASL_VERSION_MAJOR >= 2
24 #include <lutil.h>
25 #define SASL_CONST const
26 #else
27 #define SASL_CONST
28 #endif
29
30 #include <ldap_pvt.h>
31
32 #ifdef SLAPD_SPASSWD
33 #include <lutil.h>
34 #endif
35
36 /* Flags for telling slap_sasl_getdn() what type of identity is being passed */
37 #define FLAG_GETDN_FINAL   1
38 #define FLAG_GETDN_AUTHCID 2
39 #define FLAG_GETDN_AUTHZID 4
40
41 static sasl_security_properties_t sasl_secprops;
42
43 static int
44 slap_sasl_log(
45         void *context,
46         int priority,
47         const char *message) 
48 {
49         Connection *conn = context;
50         int level;
51         const char * label;
52
53         if ( message == NULL ) {
54                 return SASL_BADPARAM;
55         }
56
57         switch (priority) {
58 #if SASL_VERSION_MAJOR >= 2
59         case SASL_LOG_NONE:
60                 level = LDAP_DEBUG_NONE;
61                 label = "None";
62                 break;
63         case SASL_LOG_ERR:
64                 level = LDAP_DEBUG_ANY;
65                 label = "Error";
66                 break;
67         case SASL_LOG_FAIL:
68                 level = LDAP_DEBUG_ANY;
69                 label = "Failure";
70                 break;
71         case SASL_LOG_WARN:
72                 level = LDAP_DEBUG_TRACE;
73                 label = "Warning";
74                 break;
75         case SASL_LOG_NOTE:
76                 level = LDAP_DEBUG_TRACE;
77                 label = "Notice";
78                 break;
79         case SASL_LOG_DEBUG:
80                 level = LDAP_DEBUG_TRACE;
81                 label = "Debug";
82                 break;
83         case SASL_LOG_TRACE:
84                 level = LDAP_DEBUG_TRACE;
85                 label = "Trace";
86                 break;
87         case SASL_LOG_PASS:
88                 level = LDAP_DEBUG_TRACE;
89                 label = "Password Trace";
90                 break;
91 #else
92         case SASL_LOG_ERR:
93                 level = LDAP_DEBUG_ANY;
94                 label = "Error";
95                 break;
96         case SASL_LOG_WARNING:
97                 level = LDAP_DEBUG_TRACE;
98                 label = "Warning";
99                 break;
100         case SASL_LOG_INFO:
101                 level = LDAP_DEBUG_TRACE;
102                 label = "Info";
103                 break;
104 #endif
105         default:
106                 return SASL_BADPARAM;
107         }
108
109 #ifdef NEW_LOGGING
110         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
111                 "SASL [conn=%ld] %s: %s\n",
112                 conn ? conn->c_connid : -1,
113                 label, message ));
114 #else
115         Debug( level, "SASL [conn=%ld] %s: %s\n",
116                 conn ? conn->c_connid: -1,
117                 label, message );
118 #endif
119
120
121         return SASL_OK;
122 }
123
124
125 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
126    string returned in *dn is in its own allocated memory, and must be free'd 
127    by the calling process.
128    -Mark Adamson, Carnegie Mellon
129 */
130
131 #define SET_DN  1
132 #define SET_U   2
133
134 static struct berval ext_bv = { sizeof("EXTERNAL")-1, "EXTERNAL" };
135
136 int slap_sasl_getdn( Connection *conn, char *id, char *user_realm, struct berval *dn, int flags )
137 {
138         char *c1;
139         int rc, len, is_dn = 0;
140         sasl_conn_t *ctx;
141         struct berval dn2;
142
143 #ifdef NEW_LOGGING
144         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
145                 "slap_sasl_getdn: conn %d id=%s\n",
146                 conn ? conn->c_connid : -1,
147                 id ? (*id ? id : "<empty>") : "NULL" ));
148 #else
149         Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s\n", 
150       id?(*id?id:"<empty>"):"NULL",0,0 );
151 #endif
152
153         dn->bv_val = NULL;
154         dn->bv_len = 0;
155
156         /* Blatantly anonymous ID */
157         if( id &&
158                 ( id[sizeof( "anonymous" )-1] == '\0'
159                         || id[sizeof( "anonymous" )-1] == '@' ) &&
160                 !strncasecmp( id, "anonymous", sizeof( "anonymous" )-1) ) {
161                 return( LDAP_SUCCESS );
162         }
163         ctx = conn->c_sasl_context;
164         len = strlen( id );
165
166         /* An authcID needs to be converted to authzID form */
167         if( flags & FLAG_GETDN_AUTHCID ) {
168                 if( sasl_external_x509dn_convert
169                         && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
170                         && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) 
171                         && id[0] == '/' )
172                 {
173                         /* check SASL external for X.509 style DN and */
174                         /* convert to dn:<dn> form */
175                         dn->bv_val = ldap_dcedn2dn( id );
176                         dn->bv_len = strlen(dn->bv_val);
177                         is_dn = SET_DN;
178
179                 } else {
180                         /* convert to u:<username> form */
181                         ber_str2bv( id, len, 1, dn );
182                         is_dn = SET_U;
183                 }
184         }
185         if( !is_dn ) {
186                 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
187                         is_dn = SET_U;
188                         ber_str2bv( id+2, len-2, 1, dn );
189                 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
190                         is_dn = SET_DN;
191                         ber_str2bv( id+3, len-3, 1, dn );
192                 }
193         }
194
195         /* An authzID must be properly prefixed */
196         if( (flags & FLAG_GETDN_AUTHZID) && !is_dn ) {
197                 free( dn->bv_val );
198                 dn->bv_val = NULL;
199                 dn->bv_len = 0;
200                 return( LDAP_INAPPROPRIATE_AUTH );
201         }
202
203         /* Username strings */
204         if( is_dn == SET_U ) {
205                 char *p;
206                 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
207
208                 if( user_realm && *user_realm ) {
209                         len += strlen( user_realm ) + sizeof(",cn=")-1;
210                 }
211
212                 if( conn->c_sasl_bind_mech.bv_len ) {
213                         len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
214                 }
215
216                 /* Build the new dn */
217                 c1 = dn->bv_val;
218                 dn->bv_val = ch_malloc( len );
219                 p = slap_strcopy( dn->bv_val, "uid=" );
220                 p = slap_strcopy( p, c1 );
221                 ch_free( c1 );
222
223                 if( user_realm ) {
224                         p = slap_strcopy( p, ",cn=" );
225                         p = slap_strcopy( p, user_realm );
226                 }
227                 if( conn->c_sasl_bind_mech.bv_len ) {
228                         p = slap_strcopy( p, ",cn=" );
229                         p = slap_strcopy( p, conn->c_sasl_bind_mech.bv_val );
230                 }
231                 p = slap_strcopy( p, ",cn=auth" );
232                 dn->bv_len = p - dn->bv_val;
233                 is_dn = SET_DN;
234
235 #ifdef NEW_LOGGING
236                 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
237                         "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val ));
238 #else
239                 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
240 #endif
241         }
242
243         /* DN strings that are a cn=auth identity to run through regexp */
244         if( is_dn == SET_DN && ( ( flags & FLAG_GETDN_FINAL ) == 0 ) )
245         {
246                 slap_sasl2dn( dn, &dn2 );
247                 if( dn2.bv_val ) {
248                         ch_free( dn->bv_val );
249                         *dn = dn2;
250 #ifdef NEW_LOGGING
251                         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
252                                 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val ));
253 #else
254                         Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
255                                 dn->bv_val, 0, 0 );
256 #endif
257                 }
258         }
259
260         if( flags & FLAG_GETDN_FINAL ) {
261                 /* omit "dn:" prefix */
262                 is_dn = 0;
263         } else {
264                 rc = dnNormalize2( NULL, dn, &dn2 );
265                 free(dn->bv_val);
266                 if ( rc != LDAP_SUCCESS ) {
267                         *dn = slap_empty_bv;
268                         return rc;
269                 }
270                 *dn = dn2;
271         }
272
273         /* Attach the "dn:" prefix if needed */
274         if ( is_dn == SET_DN ) {
275                 c1 = ch_malloc( dn->bv_len + sizeof("dn:") );
276                 strcpy( c1, "dn:" );
277                 strcpy( c1 + 3, dn->bv_val );
278                 free( dn->bv_val );
279                 dn->bv_val = c1;
280                 dn->bv_len += 3;
281         }
282
283         return( LDAP_SUCCESS );
284 }
285
286 #if SASL_VERSION_MAJOR >= 2
287 static int
288 slap_sasl_checkpass(
289         sasl_conn_t *sconn,
290         void *context,
291         const char *username,
292         const char *pass,
293         unsigned passlen,
294         struct propctx *propctx)
295 {
296         Connection *conn = (Connection *)context;
297         struct berval dn, cred;
298         int rc;
299         BerVarray vals, bv;
300
301         cred.bv_val = (char *)pass;
302         cred.bv_len = passlen;
303
304         /* XXX can we do both steps at once? */
305         rc = slap_sasl_getdn( conn, (char *)username, NULL, &dn,
306                 FLAG_GETDN_AUTHCID | FLAG_GETDN_FINAL );
307         if ( rc != LDAP_SUCCESS ) {
308                 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
309                 return SASL_NOUSER;
310         }
311
312         if ( dn.bv_len == 0 ) {
313                 sasl_seterror( sconn, 0, "No password is associated with the Root DSE" );
314                 if ( dn.bv_val != NULL ) {
315                         ch_free( dn.bv_val );
316                 }
317                 return SASL_NOUSER;
318         }
319
320         rc = backend_attribute( NULL, NULL, NULL, NULL, &dn,
321                 slap_schema.si_ad_userPassword, &vals);
322         if ( rc != LDAP_SUCCESS ) {
323                 ch_free( dn.bv_val );
324                 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
325                 return SASL_NOVERIFY;
326         }
327
328         rc = SASL_NOVERIFY;
329
330         if ( vals != NULL ) {
331                 for ( bv = vals; bv->bv_val != NULL; bv++ ) {
332                         if ( !lutil_passwd( bv, &cred, NULL ) ) {
333                                 rc = SASL_OK;
334                                 break;
335                         }
336                 }
337                 ber_bvarray_free( vals );
338         }
339
340         if ( rc != SASL_OK ) {
341                 sasl_seterror( sconn, 0,
342                         ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
343         }
344
345         ch_free( dn.bv_val );
346
347         return rc;
348 }
349
350 static int
351 slap_sasl_canonicalize(
352         sasl_conn_t *sconn,
353         void *context,
354         const char *in,
355         unsigned inlen,
356         unsigned flags,
357         const char *user_realm,
358         char *out,
359         unsigned out_max,
360         unsigned *out_len)
361 {
362         Connection *conn = (Connection *)context;
363         struct berval dn;
364         int rc;
365
366         *out_len = 0;
367
368 #ifdef NEW_LOGGING
369         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
370                 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
371                         conn ? conn->c_connid : -1,
372                         (flags == SASL_CU_AUTHID) ? "authcid" : "authzid",
373                         in ? in : "<empty>" ));
374 #else
375         Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
376                 "%s=\"%s\"\n",
377                         conn ? conn->c_connid : -1,
378                         (flags == SASL_CU_AUTHID) ? "authcid" : "authzid",
379                         in ? in : "<empty>" );
380 #endif
381
382         rc = slap_sasl_getdn( conn, (char *)in, (char *)user_realm, &dn,
383                 (flags == SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
384         if ( rc != LDAP_SUCCESS ) {
385                 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
386                 return SASL_NOAUTHZ;
387         }               
388
389         if ( out_max < dn.bv_len ) {
390                 return SASL_BUFOVER;
391         }
392
393         AC_MEMCPY( out, dn.bv_val, dn.bv_len );
394         out[dn.bv_len] = '\0';
395
396         *out_len = dn.bv_len;
397
398         ch_free( dn.bv_val );
399
400 #ifdef NEW_LOGGING
401         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
402                 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
403                         conn ? conn->c_connid : -1,
404                         (flags == SASL_CU_AUTHID) ? "authcDN" : "authzDN",
405                         out ));
406 #else
407         Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
408                 "%s=\"%s\"\n",
409                         conn ? conn->c_connid : -1,
410                         (flags == SASL_CU_AUTHID) ? "authcDN" : "authzDN",
411                         out );
412 #endif
413
414         return SASL_OK;
415 }
416
417 static int
418 slap_sasl_authorize(
419         sasl_conn_t *sconn,
420         void *context,
421         const char *requested_user,
422         unsigned rlen,
423         const char *auth_identity,
424         unsigned alen,
425         const char *def_realm,
426         unsigned urlen,
427         struct propctx *propctx)
428 {
429         Connection *conn = (Connection *)context;
430         struct berval authcDN, authzDN;
431         int rc;
432
433         authcDN.bv_val = (char *)auth_identity;
434         authcDN.bv_len = alen;
435
436         authzDN.bv_val = (char *)requested_user;
437         authzDN.bv_len = rlen;
438
439 #ifdef NEW_LOGGING
440         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
441                 "slap_sasl_authorize: conn %d authcDN=\"%s\" authzDN=\"%s\"\n",
442                         conn ? conn->c_connid : -1, authcDN.bv_val, authzDN.bv_val));
443 #else
444         Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
445                 "authcDN=\"%s\" authzDN=\"%s\"\n",
446                 conn ? conn->c_connid : -1, authcDN.bv_val, authzDN.bv_val );
447 #endif
448
449         rc = slap_sasl_authorized( &authcDN, &authzDN );
450         if ( rc != LDAP_SUCCESS ) {
451 #ifdef NEW_LOGGING
452                 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
453                            "slap_sasl_authorize: conn %ld  authorization disallowed (%d)\n",
454                            (long)(conn ? conn->c_connid : -1), rc ));
455 #else
456                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
457                         " authorization disallowed (%d)\n",
458                         (long) (conn ? conn->c_connid : -1), rc, 0 );
459 #endif
460
461                 sasl_seterror( sconn, 0, "not authorized" );
462                 return SASL_NOAUTHZ;
463         }
464
465 #ifdef NEW_LOGGING
466         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
467                    "slap_sasl_authorize: conn %d authorization allowed\n",
468                    (long)(conn ? conn->c_connid : -1 ) ));
469 #else
470         Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
471                 " authorization allowed\n",
472                 (long) (conn ? conn->c_connid : -1), 0, 0 );
473 #endif
474
475         return SASL_OK;
476
477 #else
478 static int
479 slap_sasl_authorize(
480         void *context,
481         const char *authcid,
482         const char *authzid,
483         const char **user,
484         const char **errstr)
485 {
486         struct berval authcDN, authzDN;
487         int rc;
488         Connection *conn = context;
489         char *realm;
490
491         *user = NULL;
492
493 #ifdef NEW_LOGGING
494         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
495                    "slap_sasl_authorize: conn %d         authcid=\"%s\" authzid=\"%s\"\n",
496                    conn ? conn->c_connid : -1,
497                    authcid ? authcid : "<empty>",
498                    authzid ? authzid : "<empty>" ));
499 #else
500         Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
501                 "authcid=\"%s\" authzid=\"%s\"\n",
502                 (long) (conn ? conn->c_connid : -1),
503                 authcid ? authcid : "<empty>",
504                 authzid ? authzid : "<empty>" );
505 #endif
506
507         /* Convert the identities to DN's. If no authzid was given, client will
508            be bound as the DN matching their username */
509         rc = slap_sasl_getdn( conn, (char *)authcid, realm, &authcDN, FLAG_GETDN_AUTHCID );
510         if( rc != LDAP_SUCCESS ) {
511                 *errstr = ldap_err2string( rc );
512                 return SASL_NOAUTHZ;
513         }
514         if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
515 #ifdef NEW_LOGGING
516                 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
517                            "slap_sasl_authorize: conn %d  Using authcDN=%s\n",
518                            conn ? conn->c_connid : -1, authcDN.bv_val ));
519 #else
520                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
521                  "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
522 #endif
523
524                 *user = authcDN.bv_val;
525                 *errstr = NULL;
526                 return SASL_OK;
527         }
528         rc = slap_sasl_getdn( conn, (char *)authzid, realm, &authzDN, FLAG_GETDN_AUTHZID );
529         if( rc != LDAP_SUCCESS ) {
530                 ch_free( authcDN.bv_val );
531                 *errstr = ldap_err2string( rc );
532                 return SASL_NOAUTHZ;
533         }
534
535         rc = slap_sasl_authorized( &authcDN, &authzDN );
536         if( rc ) {
537 #ifdef NEW_LOGGING
538                 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
539                            "slap_sasl_authorize: conn %ld  authorization disallowed (%d)\n",
540                            (long)(conn ? conn->c_connid : -1), rc ));
541 #else
542                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
543                         " authorization disallowed (%d)\n",
544                         (long) (conn ? conn->c_connid : -1), rc, 0 );
545 #endif
546
547                 *errstr = "not authorized";
548                 ch_free( authcDN.bv_val );
549                 ch_free( authzDN.bv_val );
550                 return SASL_NOAUTHZ;
551         }
552
553 #ifdef NEW_LOGGING
554         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
555                    "slap_sasl_authorize: conn %d authorization allowed\n",
556                    (long)(conn ? conn->c_connid : -1 ) ));
557 #else
558         Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
559                 " authorization allowed\n",
560                 (long) (conn ? conn->c_connid : -1), 0, 0 );
561 #endif
562
563
564         ch_free( authcDN.bv_val );
565         *user = authzDN.bv_val;
566         *errstr = NULL;
567         return SASL_OK;
568 }
569 #endif /* SASL_VERSION_MAJOR >= 2 */
570
571 static int
572 slap_sasl_err2ldap( int saslerr )
573 {
574         int rc;
575
576         switch (saslerr) {
577                 case SASL_CONTINUE:
578                         rc = LDAP_SASL_BIND_IN_PROGRESS;
579                         break;
580                 case SASL_FAIL:
581                         rc = LDAP_OTHER;
582                         break;
583                 case SASL_NOMEM:
584                         rc = LDAP_OTHER;
585                         break;
586                 case SASL_NOMECH:
587                         rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
588                         break;
589                 case SASL_BADAUTH:
590                         rc = LDAP_INVALID_CREDENTIALS;
591                         break;
592                 case SASL_NOAUTHZ:
593                         rc = LDAP_INSUFFICIENT_ACCESS;
594                         break;
595                 case SASL_TOOWEAK:
596                 case SASL_ENCRYPT:
597                         rc = LDAP_INAPPROPRIATE_AUTH;
598                         break;
599                 default:
600                         rc = LDAP_OTHER;
601                         break;
602         }
603
604         return rc;
605 }
606 #endif
607
608
609 int slap_sasl_init( void )
610 {
611 #ifdef HAVE_CYRUS_SASL
612         int rc;
613         static sasl_callback_t server_callbacks[] = {
614                 { SASL_CB_LOG, &slap_sasl_log, NULL },
615                 { SASL_CB_LIST_END, NULL, NULL }
616         };
617
618         sasl_set_alloc(
619                 ch_malloc,
620                 ch_calloc,
621                 ch_realloc,
622                 ch_free ); 
623
624         sasl_set_mutex(
625                 ldap_pvt_sasl_mutex_new,
626                 ldap_pvt_sasl_mutex_lock,
627                 ldap_pvt_sasl_mutex_unlock,
628                 ldap_pvt_sasl_mutex_dispose );
629
630         /* should provide callbacks for logging */
631         /* server name should be configurable */
632         rc = sasl_server_init( server_callbacks, "slapd" );
633
634         if( rc != SASL_OK ) {
635 #ifdef NEW_LOGGING
636                 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
637                            "slap_sasl_init: init failed.\n" ));
638 #else
639                 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
640                         0, 0, 0 );
641 #endif
642
643                 return -1;
644         }
645
646 #ifdef NEW_LOGGING
647         LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
648                    "slap_sasl_init: initialized!\n"));
649 #else
650         Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
651                 0, 0, 0 );
652 #endif
653
654
655         /* default security properties */
656         memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
657         sasl_secprops.max_ssf = INT_MAX;
658         sasl_secprops.maxbufsize = 65536;
659         sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
660 #endif
661
662         return 0;
663 }
664
665 int slap_sasl_destroy( void )
666 {
667 #ifdef HAVE_CYRUS_SASL
668         sasl_done();
669 #endif
670         free( global_host );
671         global_host = NULL;
672
673         return 0;
674 }
675
676 int slap_sasl_open( Connection *conn )
677 {
678         int sc = LDAP_SUCCESS;
679 #if SASL_VERSION_MAJOR >= 2
680         char *ipremoteport = NULL, *iplocalport = NULL;
681 #endif
682
683 #ifdef HAVE_CYRUS_SASL
684         sasl_conn_t *ctx = NULL;
685         sasl_callback_t *session_callbacks;
686
687         assert( conn->c_sasl_context == NULL );
688         assert( conn->c_sasl_extra == NULL );
689
690         conn->c_sasl_layers = 0;
691
692         session_callbacks =
693 #if SASL_VERSION_MAJOR >= 2
694                 ch_calloc( 5, sizeof(sasl_callback_t));
695 #else
696                 ch_calloc( 3, sizeof(sasl_callback_t));
697 #endif
698         conn->c_sasl_extra = session_callbacks;
699
700         session_callbacks[0].id = SASL_CB_LOG;
701         session_callbacks[0].proc = &slap_sasl_log;
702         session_callbacks[0].context = conn;
703
704         session_callbacks[1].id = SASL_CB_PROXY_POLICY;
705         session_callbacks[1].proc = &slap_sasl_authorize;
706         session_callbacks[1].context = conn;
707
708 #if SASL_VERSION_MAJOR >= 2
709         session_callbacks[2].id = SASL_CB_CANON_USER;
710         session_callbacks[2].proc = &slap_sasl_canonicalize;
711         session_callbacks[2].context = conn;
712
713         session_callbacks[3].id = SASL_CB_SERVER_USERDB_CHECKPASS;
714         session_callbacks[3].proc = &slap_sasl_checkpass;
715         session_callbacks[3].context = conn;
716
717         session_callbacks[4].id = SASL_CB_LIST_END;
718         session_callbacks[4].proc = NULL;
719         session_callbacks[4].context = NULL;
720 #else
721         session_callbacks[2].id = SASL_CB_LIST_END;
722         session_callbacks[2].proc = NULL;
723         session_callbacks[2].context = NULL;
724 #endif
725
726         if( global_host == NULL ) {
727                 global_host = ldap_pvt_get_fqdn( NULL );
728         }
729
730         /* create new SASL context */
731 #if SASL_VERSION_MAJOR >= 2
732         if ( conn->c_sock_name.bv_len != 0 &&
733              strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
734                 char *p;
735
736                 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
737                 /* Convert IPv6 addresses to address;port syntax. */
738                 p = strrchr( iplocalport, ' ' );
739                 if ( p != NULL ) {
740                         *p = ';';
741                 }
742         }
743         if ( conn->c_peer_name.bv_len != 0 &&
744              strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
745                 char *p;
746
747                 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
748                 /* Convert IPv6 addresses to address;port syntax. */
749                 p = strrchr( ipremoteport, ' ' );
750                 if ( p != NULL ) {
751                         *p = ';';
752                 }
753         }
754         sc = sasl_server_new( "ldap", global_host, global_realm,
755                 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
756         if ( iplocalport != NULL ) {
757                 ch_free( iplocalport );
758         }
759         if ( ipremoteport != NULL ) {
760                 ch_free( ipremoteport );
761         }
762 #else
763         sc = sasl_server_new( "ldap", global_host, global_realm,
764                 session_callbacks, SASL_SECURITY_LAYER, &ctx );
765 #endif
766
767         if( sc != SASL_OK ) {
768 #ifdef NEW_LOGGING
769                 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
770                            "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
771 #else
772                 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
773                         sc, 0, 0 );
774 #endif
775
776                 return -1;
777         }
778
779         conn->c_sasl_context = ctx;
780
781         if( sc == SASL_OK ) {
782                 sc = sasl_setprop( ctx,
783                         SASL_SEC_PROPS, &sasl_secprops );
784
785                 if( sc != SASL_OK ) {
786 #ifdef NEW_LOGGING
787                         LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
788                                    "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
789 #else
790                         Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
791                                 sc, 0, 0 );
792 #endif
793
794                         slap_sasl_close( conn );
795                         return -1;
796                 }
797         }
798
799         sc = slap_sasl_err2ldap( sc );
800 #endif
801         return sc;
802 }
803
804 int slap_sasl_external(
805         Connection *conn,
806         slap_ssf_t ssf,
807         const char *auth_id )
808 {
809 #if SASL_VERSION_MAJOR >= 2
810         int sc;
811         sasl_conn_t *ctx = conn->c_sasl_context;
812
813         if ( ctx == NULL ) {
814                 return LDAP_UNAVAILABLE;
815         }
816
817         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
818
819         if ( sc != SASL_OK ) {
820                 return LDAP_OTHER;
821         }
822
823         sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
824
825         if ( sc != SASL_OK ) {
826                 return LDAP_OTHER;
827         }
828 #elif defined(HAVE_CYRUS_SASL)
829         int sc;
830         sasl_conn_t *ctx = conn->c_sasl_context;
831         sasl_external_properties_t extprops;
832
833         if ( ctx == NULL ) {
834                 return LDAP_UNAVAILABLE;
835         }
836
837         memset( &extprops, '\0', sizeof(extprops) );
838         extprops.ssf = ssf;
839         extprops.auth_id = (char *) auth_id;
840
841         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
842                 (void *) &extprops );
843
844         if ( sc != SASL_OK ) {
845                 return LDAP_OTHER;
846         }
847 #endif
848
849         return LDAP_SUCCESS;
850 }
851
852 int slap_sasl_reset( Connection *conn )
853 {
854 #ifdef HAVE_CYRUS_SASL
855         sasl_conn_t *ctx = conn->c_sasl_context;
856
857         if( ctx != NULL ) {
858         }
859 #endif
860         /* must return "anonymous" */
861         return LDAP_SUCCESS;
862 }
863
864 char ** slap_sasl_mechs( Connection *conn )
865 {
866         char **mechs = NULL;
867
868 #ifdef HAVE_CYRUS_SASL
869         sasl_conn_t *ctx = conn->c_sasl_context;
870
871         if( ctx != NULL ) {
872                 int sc;
873                 SASL_CONST char *mechstr;
874
875                 sc = sasl_listmech( ctx,
876                         NULL, NULL, ",", NULL,
877                         &mechstr, NULL, NULL );
878
879                 if( sc != SASL_OK ) {
880 #ifdef NEW_LOGGING
881                         LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
882                                 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
883 #else
884                         Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
885                                 sc, 0, 0 );
886 #endif
887
888                         return NULL;
889                 }
890
891                 mechs = str2charray( mechstr, "," );
892
893 #if SASL_VERSION_MAJOR < 2
894                 ch_free( mechstr );
895 #endif
896         }
897 #endif
898
899         return mechs;
900 }
901
902 int slap_sasl_close( Connection *conn )
903 {
904 #ifdef HAVE_CYRUS_SASL
905         sasl_conn_t *ctx = conn->c_sasl_context;
906
907         if( ctx != NULL ) {
908                 sasl_dispose( &ctx );
909         }
910
911         conn->c_sasl_context = NULL;
912
913         free( conn->c_sasl_extra );
914         conn->c_sasl_extra = NULL;
915 #endif
916
917         return LDAP_SUCCESS;
918 }
919
920 int slap_sasl_bind(
921     Connection          *conn,
922     Operation           *op,  
923     struct berval       *dn,  
924     struct berval       *ndn,
925     struct berval       *cred,
926         struct berval                   *edn,
927         slap_ssf_t              *ssfp )
928 {
929         int rc = 1;
930
931 #ifdef HAVE_CYRUS_SASL
932         sasl_conn_t *ctx = conn->c_sasl_context;
933         struct berval response;
934         unsigned reslen = 0;
935         const char *errstr = NULL;
936         int sc;
937
938 #ifdef NEW_LOGGING
939         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
940                 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
941                 conn->c_connid,
942                 dn->bv_len ? dn->bv_val : "",
943                 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech.bv_val,
944                 cred ? cred->bv_len : 0 ));
945 #else
946         Debug(LDAP_DEBUG_ARGS,
947                 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
948                 dn->bv_len ? dn->bv_val : "",
949                 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
950                 cred ? cred->bv_len : 0 );
951 #endif
952
953
954         if( ctx == NULL ) {
955                 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
956                         NULL, "SASL unavailable on this session", NULL, NULL );
957                 return rc;
958         }
959
960 #if SASL_VERSION_MAJOR >= 2
961 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
962         sasl_server_start( ctx, mech, cred, clen, resp, rlen )
963 #define STEP( ctx, cred, clen, resp, rlen, err ) \
964         sasl_server_step( ctx, cred, clen, resp, rlen )
965 #else
966 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
967         sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
968 #define STEP( ctx, cred, clen, resp, rlen, err ) \
969         sasl_server_step( ctx, cred, clen, resp, rlen, err )
970 #endif
971
972         if ( !conn->c_sasl_bind_in_progress ) {
973                 sc = START( ctx,
974                         conn->c_sasl_bind_mech.bv_val,
975                         cred->bv_len ? cred->bv_val : "",
976                         cred->bv_len,
977                         (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
978
979         } else {
980                 sc = STEP( ctx,
981                         cred->bv_val, cred->bv_len,
982                         (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
983         }
984
985         response.bv_len = reslen;
986
987         if ( sc == SASL_OK ) {
988                 char *username = NULL;
989                 char *realm = NULL;
990
991 #if SASL_VERSION_MAJOR >= 2
992                 sc = sasl_getprop( ctx, SASL_DEFUSERREALM, (const void **)&realm );
993 #else
994                 sc = sasl_getprop( ctx, SASL_REALM, (void **)&realm );
995 #endif
996                 sc = sasl_getprop( ctx,
997                         SASL_USERNAME, (SASL_CONST void **)&username );
998
999                 if ( sc != SASL_OK ) {
1000 #ifdef NEW_LOGGING
1001                         LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1002                                 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
1003 #else
1004                         Debug(LDAP_DEBUG_TRACE,
1005                                 "slap_sasl_bind: getprop(USERNAME) failed!\n",
1006                                 0, 0, 0);
1007 #endif
1008
1009
1010                         send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1011                                 NULL, "no SASL username", NULL, NULL );
1012
1013                 } else {
1014                         rc = slap_sasl_getdn( conn, username, realm, edn, FLAG_GETDN_FINAL );
1015
1016                         if( rc == LDAP_SUCCESS ) {
1017                                 sasl_ssf_t *ssf = NULL;
1018                                 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1019                                 *ssfp = ssf ? *ssf : 0;
1020
1021                                 if( *ssfp ) {
1022                                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1023                                         conn->c_sasl_layers++;
1024                                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1025                                 }
1026
1027                                 send_ldap_sasl( conn, op, rc,
1028                                         NULL, NULL, NULL, NULL,
1029                                         response.bv_len ? &response : NULL );
1030
1031                         } else {
1032                                 send_ldap_result( conn, op, rc,
1033                                         NULL, errstr, NULL, NULL );
1034                         }
1035                 }
1036
1037         } else if ( sc == SASL_CONTINUE ) {
1038                 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1039                         NULL, NULL, NULL, NULL, &response );
1040
1041         } else {
1042                 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1043                         NULL, errstr, NULL, NULL );
1044         }
1045
1046 #if SASL_VERSION_MAJOR < 2
1047         if( response.bv_len ) {
1048                 ch_free( response.bv_val );
1049         }
1050 #endif
1051
1052 #ifdef NEW_LOGGING
1053         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1054                 "slap_sasl_bind: rc=%d\n", rc ));
1055 #else
1056         Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1057 #endif
1058
1059
1060 #else
1061         send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1062                 NULL, "SASL not supported", NULL, NULL );
1063 #endif
1064
1065         return rc;
1066 }
1067
1068 char* slap_sasl_secprops( const char *in )
1069 {
1070 #ifdef HAVE_CYRUS_SASL
1071         int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1072
1073         return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1074 #else
1075         return "SASL not supported";
1076 #endif
1077 }