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