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