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