]> git.sur5r.net Git - openldap/blob - servers/slapd/sasl.c
syntax and mr plugins
[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 #include <lutil.h>
28 #if SASL_VERSION_MAJOR >= 2
29 #include <sasl/saslplug.h>
30 #define SASL_CONST const
31 #else
32 #define SASL_CONST
33 #endif
34
35 #include <ldap_pvt.h>
36
37 /* Flags for telling slap_sasl_getdn() what type of identity is being passed */
38 #define FLAG_GETDN_AUTHCID 2
39 #define FLAG_GETDN_AUTHZID 4
40
41 static sasl_security_properties_t sasl_secprops;
42
43 int slap_sasl_config( int cargc, char **cargv, char *line,
44         const char *fname, int lineno )
45 {
46                 /* set SASL proxy authorization policy */
47                 if ( strcasecmp( cargv[0], "sasl-authz-policy" ) == 0 ) {
48                         if ( cargc != 2 ) {
49 #ifdef NEW_LOGGING
50                                 LDAP_LOG( CONFIG, CRIT,
51                                            "%s: line %d: missing policy in \"sasl-authz-policy <policy>\" line\n",
52                                            fname, lineno, 0 );
53 #else
54                                 Debug( LDAP_DEBUG_ANY,
55             "%s: line %d: missing policy in \"sasl-authz-policy <policy>\" line\n",
56                                     fname, lineno, 0 );
57 #endif
58
59                                 return( 1 );
60                         }
61                         if ( slap_sasl_setpolicy( cargv[1] ) ) {
62 #ifdef NEW_LOGGING
63                                 LDAP_LOG( CONFIG, CRIT,
64                                            "%s: line %d: unable "
65                                            "to parse value \"%s\" "
66                                            "in \"sasl-authz-policy "
67                                            "<policy>\" line.\n",
68                                            fname, lineno, cargv[1] );
69 #else
70                                 Debug( LDAP_DEBUG_ANY,
71                                         "%s: line %d: unable "
72                                         "to parse value \"%s\" "
73                                         "in \"sasl-authz-policy "
74                                         "<policy>\" line\n",
75                                         fname, lineno, cargv[1] );
76 #endif
77                                 return( 1 );
78                         }
79                         
80
81                 /* set SASL host */
82                 } else if ( strcasecmp( cargv[0], "sasl-host" ) == 0 ) {
83                         if ( cargc < 2 ) {
84 #ifdef NEW_LOGGING
85                                 LDAP_LOG( CONFIG, CRIT,
86                                            "%s: line %d: missing host in \"sasl-host <host>\" line\n",
87                                            fname, lineno, 0 );
88 #else
89                                 Debug( LDAP_DEBUG_ANY,
90             "%s: line %d: missing host in \"sasl-host <host>\" line\n",
91                                     fname, lineno, 0 );
92 #endif
93
94                                 return( 1 );
95                         }
96
97                         if ( global_host != NULL ) {
98 #ifdef NEW_LOGGING
99                                 LDAP_LOG( CONFIG, CRIT,
100                                            "%s: line %d: already set sasl-host!\n",
101                                            fname, lineno, 0 );
102 #else
103                                 Debug( LDAP_DEBUG_ANY,
104                                         "%s: line %d: already set sasl-host!\n",
105                                         fname, lineno, 0 );
106 #endif
107
108                                 return 1;
109
110                         } else {
111                                 global_host = ch_strdup( cargv[1] );
112                         }
113
114                 /* set SASL realm */
115                 } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
116                         if ( cargc < 2 ) {
117 #ifdef NEW_LOGGING
118                                 LDAP_LOG( CONFIG, CRIT,
119                                            "%s: line %d: missing realm in \"sasl-realm <realm>\" line.\n",
120                                            fname, lineno, 0 );
121 #else
122                                 Debug( LDAP_DEBUG_ANY,
123             "%s: line %d: missing realm in \"sasl-realm <realm>\" line\n",
124                                     fname, lineno, 0 );
125 #endif
126
127                                 return( 1 );
128                         }
129
130                         if ( global_realm != NULL ) {
131 #ifdef NEW_LOGGING
132                                 LDAP_LOG( CONFIG, CRIT,
133                                            "%s: line %d: already set sasl-realm!\n",
134                                            fname, lineno, 0 );
135 #else
136                                 Debug( LDAP_DEBUG_ANY,
137                                         "%s: line %d: already set sasl-realm!\n",
138                                         fname, lineno, 0 );
139 #endif
140
141                                 return 1;
142
143                         } else {
144                                 global_realm = ch_strdup( cargv[1] );
145                         }
146
147                 } else if ( !strcasecmp( cargv[0], "sasl-regexp" ) 
148                         || !strcasecmp( cargv[0], "saslregexp" ) )
149                 {
150                         int rc;
151                         if ( cargc != 3 ) {
152 #ifdef NEW_LOGGING
153                                 LDAP_LOG( CONFIG, CRIT,
154                                            "%s: line %d: need 2 args in "
155                                            "\"saslregexp <match> <replace>\"\n",
156                                            fname, lineno, 0 );
157 #else
158                                 Debug( LDAP_DEBUG_ANY, 
159                                 "%s: line %d: need 2 args in \"saslregexp <match> <replace>\"\n",
160                                     fname, lineno, 0 );
161 #endif
162
163                                 return( 1 );
164                         }
165                         rc = slap_sasl_regexp_config( cargv[1], cargv[2] );
166                         if ( rc ) {
167                                 return rc;
168                         }
169
170                 /* SASL security properties */
171                 } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
172                         char *txt;
173
174                         if ( cargc < 2 ) {
175 #ifdef NEW_LOGGING
176                                 LDAP_LOG( CONFIG, CRIT,
177                                            "%s: line %d: missing flags in "
178                                            "\"sasl-secprops <properties>\" line\n",
179                                            fname, lineno, 0 );
180 #else
181                                 Debug( LDAP_DEBUG_ANY,
182             "%s: line %d: missing flags in \"sasl-secprops <properties>\" line\n",
183                                     fname, lineno, 0 );
184 #endif
185
186                                 return 1;
187                         }
188
189                         txt = slap_sasl_secprops( cargv[1] );
190                         if ( txt != NULL ) {
191 #ifdef NEW_LOGGING
192                                 LDAP_LOG( CONFIG, CRIT,
193                                            "%s: line %d sasl-secprops: %s\n",
194                                            fname, lineno, txt );
195 #else
196                                 Debug( LDAP_DEBUG_ANY,
197             "%s: line %d: sasl-secprops: %s\n",
198                                     fname, lineno, txt );
199 #endif
200
201                                 return 1;
202                         }
203             }
204
205             return LDAP_SUCCESS;
206 }
207
208 static int
209 slap_sasl_log(
210         void *context,
211         int priority,
212         const char *message) 
213 {
214         Connection *conn = context;
215         int level;
216         const char * label;
217
218         if ( message == NULL ) {
219                 return SASL_BADPARAM;
220         }
221
222         switch (priority) {
223 #if SASL_VERSION_MAJOR >= 2
224         case SASL_LOG_NONE:
225                 level = LDAP_DEBUG_NONE;
226                 label = "None";
227                 break;
228         case SASL_LOG_ERR:
229                 level = LDAP_DEBUG_ANY;
230                 label = "Error";
231                 break;
232         case SASL_LOG_FAIL:
233                 level = LDAP_DEBUG_ANY;
234                 label = "Failure";
235                 break;
236         case SASL_LOG_WARN:
237                 level = LDAP_DEBUG_TRACE;
238                 label = "Warning";
239                 break;
240         case SASL_LOG_NOTE:
241                 level = LDAP_DEBUG_TRACE;
242                 label = "Notice";
243                 break;
244         case SASL_LOG_DEBUG:
245                 level = LDAP_DEBUG_TRACE;
246                 label = "Debug";
247                 break;
248         case SASL_LOG_TRACE:
249                 level = LDAP_DEBUG_TRACE;
250                 label = "Trace";
251                 break;
252         case SASL_LOG_PASS:
253                 level = LDAP_DEBUG_TRACE;
254                 label = "Password Trace";
255                 break;
256 #else
257         case SASL_LOG_ERR:
258                 level = LDAP_DEBUG_ANY;
259                 label = "Error";
260                 break;
261         case SASL_LOG_WARNING:
262                 level = LDAP_DEBUG_TRACE;
263                 label = "Warning";
264                 break;
265         case SASL_LOG_INFO:
266                 level = LDAP_DEBUG_TRACE;
267                 label = "Info";
268                 break;
269 #endif
270         default:
271                 return SASL_BADPARAM;
272         }
273
274 #ifdef NEW_LOGGING
275         LDAP_LOG( TRANSPORT, ENTRY, 
276                 "SASL [conn=%ld] %s: %s\n", conn ? conn->c_connid : -1, label, message);
277 #else
278         Debug( level, "SASL [conn=%ld] %s: %s\n",
279                 conn ? conn->c_connid: -1,
280                 label, message );
281 #endif
282
283
284         return SASL_OK;
285 }
286
287
288 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
289    string returned in *dn is in its own allocated memory, and must be free'd 
290    by the calling process.
291    -Mark Adamson, Carnegie Mellon
292
293    The "dn:" prefix is no longer used anywhere inside slapd. It is only used
294    on strings passed in directly from SASL.
295    -Howard Chu, Symas Corp.
296 */
297
298 #define SET_DN  1
299 #define SET_U   2
300
301 static struct berval ext_bv = { sizeof("EXTERNAL")-1, "EXTERNAL" };
302
303 int slap_sasl_getdn( Connection *conn, char *id, int len,
304         char *user_realm, struct berval *dn, int flags )
305 {
306         char *c1;
307         int rc, is_dn = 0, do_norm = 1;
308         sasl_conn_t *ctx;
309         struct berval dn2;
310
311 #ifdef NEW_LOGGING
312         LDAP_LOG( TRANSPORT, ENTRY, 
313                 "slap_sasl_getdn: conn %d id=%s\n",
314                 conn ? conn->c_connid : -1, id ? (*id ? id : "<empty>") : "NULL", 0 );
315 #else
316         Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s\n", 
317       id?(*id?id:"<empty>"):"NULL",0,0 );
318 #endif
319
320         dn->bv_val = NULL;
321         dn->bv_len = 0;
322
323         if ( id ) {
324                 if ( len == 0 ) len = strlen( id );
325
326                 /* Blatantly anonymous ID */
327                 if ( len == sizeof("anonymous") - 1 &&
328                         !strcasecmp( id, "anonymous" ) ) {
329                         return( LDAP_SUCCESS );
330                 }
331         } else {
332                 len = 0;
333         }
334
335         ctx = conn->c_sasl_context;
336
337         /* An authcID needs to be converted to authzID form. Set the
338          * values directly into *dn; they will be normalized later. (and
339          * normalizing always makes a new copy.) An ID from a TLS certificate
340          * is already normalized, so copy it and skip normalization.
341          */
342         if( flags & FLAG_GETDN_AUTHCID ) {
343 #ifdef HAVE_TLS
344                 if( conn->c_is_tls && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
345                         && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) ) {
346                         /* X.509 DN is already normalized */
347                         do_norm = 0;
348                         is_dn = SET_DN;
349                         ber_str2bv( id, len, 1, dn );
350
351                 } else
352 #endif
353                 {
354                         /* convert to u:<username> form */
355                         is_dn = SET_U;
356                         dn->bv_val = id;
357                         dn->bv_len = len;
358                 }
359         }
360         if( !is_dn ) {
361                 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
362                         is_dn = SET_U;
363                         dn->bv_val = id+2;
364                         dn->bv_len = len-2;
365                 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
366                         is_dn = SET_DN;
367                         dn->bv_val = id+3;
368                         dn->bv_len = len-3;
369                 }
370         }
371
372         /* No other possibilities from here */
373         if( !is_dn ) {
374                 dn->bv_val = NULL;
375                 dn->bv_len = 0;
376                 return( LDAP_INAPPROPRIATE_AUTH );
377         }
378
379         /* Username strings */
380         if( is_dn == SET_U ) {
381                 char *p, *realm;
382                 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
383
384                 /* username may have embedded realm name */
385                 if( ( realm = strchr( dn->bv_val, '@') ) ) {
386                         *realm++ = '\0';
387                         len += sizeof(",cn=")-2;
388                 } else if( user_realm && *user_realm ) {
389                         len += strlen( user_realm ) + sizeof(",cn=")-1;
390                 }
391
392                 if( conn->c_sasl_bind_mech.bv_len ) {
393                         len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
394                 }
395
396                 /* Build the new dn */
397                 c1 = dn->bv_val;
398                 dn->bv_val = ch_malloc( len+1 );
399                 p = lutil_strcopy( dn->bv_val, "uid=" );
400                 p = lutil_strncopy( p, c1, dn->bv_len );
401
402                 if( realm ) {
403                         int rlen = dn->bv_len - ( realm - c1 );
404                         p = lutil_strcopy( p, ",cn=" );
405                         p = lutil_strncopy( p, realm, rlen );
406                         realm[-1] = '@';
407                 } else if( user_realm && *user_realm ) {
408                         p = lutil_strcopy( p, ",cn=" );
409                         p = lutil_strcopy( p, user_realm );
410                 }
411
412                 if( conn->c_sasl_bind_mech.bv_len ) {
413                         p = lutil_strcopy( p, ",cn=" );
414                         p = lutil_strcopy( p, conn->c_sasl_bind_mech.bv_val );
415                 }
416                 p = lutil_strcopy( p, ",cn=auth" );
417                 dn->bv_len = p - dn->bv_val;
418
419 #ifdef NEW_LOGGING
420                 LDAP_LOG( TRANSPORT, ENTRY, 
421                         "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val, 0, 0 );
422 #else
423                 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
424 #endif
425         }
426
427         /* All strings are in DN form now. Normalize if needed. */
428         if ( do_norm ) {
429                 rc = dnNormalize2( NULL, dn, &dn2 );
430
431                 /* User DNs were constructed above and must be freed now */
432                 if ( is_dn == SET_U )
433                         ch_free( dn->bv_val );
434
435                 if ( rc != LDAP_SUCCESS ) {
436                         dn->bv_val = NULL;
437                         dn->bv_len = 0;
438                         return rc;
439                 }
440                 *dn = dn2;
441         }
442
443         /* Run thru regexp */
444         slap_sasl2dn( conn, dn, &dn2 );
445         if( dn2.bv_val ) {
446                 ch_free( dn->bv_val );
447                 *dn = dn2;
448 #ifdef NEW_LOGGING
449                 LDAP_LOG( TRANSPORT, ENTRY, 
450                         "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val, 0, 0 );
451 #else
452                 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
453                         dn->bv_val, 0, 0 );
454 #endif
455         }
456
457         return( LDAP_SUCCESS );
458 }
459
460 #if SASL_VERSION_MAJOR >= 2
461 static const char *slap_propnames[] = { "*authcDN", "*authzDN", NULL };
462
463 static void
464 slap_auxprop_lookup(
465         void *glob_context,
466         sasl_server_params_t *sparams,
467         unsigned flags,
468         const char *user,
469         unsigned ulen)
470 {
471         int rc, i, last;
472         struct berval dn;
473         const struct propval *list;
474         BerVarray vals, bv;
475         AttributeDescription *ad;
476         const char *text;
477
478         list = sparams->utils->prop_get( sparams->propctx );
479
480         /* Find our DN first */
481         for( i = 0, last = 0; list[i].name; i++ ) {
482                 if ( list[i].name[0] == '*' ) {
483                         if ( (flags & SASL_AUXPROP_AUTHZID) &&
484                                 !strcmp( list[i].name, slap_propnames[1] ) ) {
485                                 if ( list[i].values && list[i].values[0] )
486                                         AC_MEMCPY( &dn, list[i].values[0], sizeof( dn ) );
487                                 if ( !last ) last = i;
488                                 break;
489                         }
490                         if ( !strcmp( list[i].name, slap_propnames[0] ) ) {
491                                 if ( !last ) last = i;
492                                 if ( list[i].values && list[i].values[0] ) {
493                                         AC_MEMCPY( &dn, list[i].values[0], sizeof( dn ) );
494                                         if ( !(flags & SASL_AUXPROP_AUTHZID) )
495                                                 break;
496                                 }
497                         }
498                 }
499         }
500
501         /* Now fetch the rest */
502         for( i = 0; i < last; i++ ) {
503                 const char *name = list[i].name;
504
505                 if ( name[0] == '*' ) {
506                         if ( flags & SASL_AUXPROP_AUTHZID ) continue;
507                         name++;
508                 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
509                         continue;
510
511                 if ( list[i].values ) {
512                         if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
513                         sparams->utils->prop_erase( sparams->propctx, list[i].name );
514                 }
515                 ad = NULL;
516                 rc = slap_str2ad( name, &ad, &text );
517                 if ( rc != LDAP_SUCCESS ) {
518 #ifdef NEW_LOGGING
519                         LDAP_LOG( TRANSPORT, DETAIL1, 
520                                 "slap_auxprop: str2ad(%s): %s\n",  name, text, 0 );
521 #else
522                         Debug( LDAP_DEBUG_TRACE,
523                                 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
524 #endif
525                         rc = slap_str2undef_ad( name, &ad, &text );
526                         if ( rc != LDAP_SUCCESS ) continue;
527                 }
528                 rc = backend_attribute( NULL,NULL,NULL,NULL, &dn, ad, &vals );
529                 if ( rc != LDAP_SUCCESS ) continue;
530                 for ( bv = vals; bv->bv_val; bv++ ) {
531                         sparams->utils->prop_set( sparams->propctx, list[i].name,
532                                 bv->bv_val, bv->bv_len );
533                 }
534                 ber_bvarray_free( vals );
535         }
536 }
537
538 static sasl_auxprop_plug_t slap_auxprop_plugin = {
539         0,      /* Features */
540         0,      /* spare */
541         NULL,   /* glob_context */
542         NULL,   /* auxprop_free */
543         slap_auxprop_lookup,
544         "slapd",        /* name */
545         NULL    /* spare */
546 };
547
548 static int
549 slap_auxprop_init(
550         const sasl_utils_t *utils,
551         int max_version,
552         int *out_version,
553         sasl_auxprop_plug_t **plug,
554         const char *plugname)
555 {
556         if ( !out_version | !plug ) return SASL_BADPARAM;
557
558         if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
559
560         *out_version = SASL_AUXPROP_PLUG_VERSION;
561         *plug = &slap_auxprop_plugin;
562         return SASL_OK;
563 }
564
565 static int
566 slap_sasl_checkpass(
567         sasl_conn_t *sconn,
568         void *context,
569         const char *username,
570         const char *pass,
571         unsigned passlen,
572         struct propctx *propctx)
573 {
574         Connection *conn = (Connection *)context;
575         struct berval dn, cred;
576         int rc;
577         BerVarray vals, bv;
578
579         cred.bv_val = (char *)pass;
580         cred.bv_len = passlen;
581
582         /* SASL will fallback to its own mechanisms if we don't
583          * find an answer here.
584          */
585
586         rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &dn,
587                 FLAG_GETDN_AUTHCID );
588         if ( rc != LDAP_SUCCESS ) {
589                 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
590                 return SASL_NOUSER;
591         }
592
593         if ( dn.bv_len == 0 ) {
594                 sasl_seterror( sconn, 0,
595                         "No password is associated with the Root DSE" );
596                 if ( dn.bv_val != NULL ) {
597                         ch_free( dn.bv_val );
598                 }
599                 return SASL_NOUSER;
600         }
601
602         rc = backend_attribute( NULL, NULL, NULL, NULL, &dn,
603                 slap_schema.si_ad_userPassword, &vals);
604         if ( rc != LDAP_SUCCESS ) {
605                 ch_free( dn.bv_val );
606                 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
607                 return SASL_NOVERIFY;
608         }
609
610         rc = SASL_NOVERIFY;
611
612         if ( vals != NULL ) {
613                 for ( bv = vals; bv->bv_val != NULL; bv++ ) {
614                         if ( !lutil_passwd( bv, &cred, NULL ) ) {
615                                 rc = SASL_OK;
616                                 break;
617                         }
618                 }
619                 ber_bvarray_free( vals );
620         }
621
622         if ( rc != SASL_OK ) {
623                 sasl_seterror( sconn, 0,
624                         ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
625         }
626
627         ch_free( dn.bv_val );
628
629         return rc;
630 }
631
632 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
633  * auxiliary property, so that we can refer to it in sasl_authorize
634  * without interfering with anything else. Also, the SASL username
635  * buffer is constrained to 256 characters, and our DNs could be
636  * much longer (totally arbitrary length)...
637  */
638 static int
639 slap_sasl_canonicalize(
640         sasl_conn_t *sconn,
641         void *context,
642         const char *in,
643         unsigned inlen,
644         unsigned flags,
645         const char *user_realm,
646         char *out,
647         unsigned out_max,
648         unsigned *out_len)
649 {
650         Connection *conn = (Connection *)context;
651         struct propctx *props = sasl_auxprop_getctx( sconn );
652         struct propval auxvals[3];
653         struct berval dn;
654         int rc, which;
655         const char *names[2];
656
657         *out_len = 0;
658
659 #ifdef NEW_LOGGING
660         LDAP_LOG( TRANSPORT, ENTRY, 
661                 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
662                 conn ? conn->c_connid : -1,
663                 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid", in ? in : "<empty>");
664 #else
665         Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
666                 "%s=\"%s\"\n",
667                         conn ? conn->c_connid : -1,
668                         (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
669                         in ? in : "<empty>" );
670 #endif
671
672         /* If name is too big, just truncate. We don't care, we're
673          * using DNs, not the usernames.
674          */
675         if ( inlen > out_max )
676                 inlen = out_max-1;
677
678         /* See if we need to add request, can only do it once */
679         prop_getnames( props, slap_propnames, auxvals );
680         if ( !auxvals[0].name )
681                 prop_request( props, slap_propnames );
682
683         if ( flags & SASL_CU_AUTHID )
684                 which = 0;
685         else
686                 which = 1;
687
688         /* Already been here? */
689         if ( auxvals[which].values )
690                 goto done;
691
692         if ( flags == SASL_CU_AUTHZID ) {
693         /* If we got unqualified authzid's, they probably came from SASL
694          * itself just passing the authcid to us. Look inside the oparams
695          * structure to see if that's true. (HACK: the out_len pointer is
696          * the address of a member of a sasl_out_params_t structure...)
697          */
698                 sasl_out_params_t dummy;
699                 int offset = (void *)&dummy.ulen - (void *)&dummy.authid;
700                 char **authid = (void *)out_len - offset;
701                 if ( *authid && !strcmp( in, *authid ) )
702                         goto done;
703         }
704
705         rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
706                 (flags & SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
707         if ( rc != LDAP_SUCCESS ) {
708                 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
709                 return SASL_NOAUTHZ;
710         }               
711
712         names[0] = slap_propnames[which];
713         names[1] = NULL;
714
715         prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
716                 
717 #ifdef NEW_LOGGING
718         LDAP_LOG( TRANSPORT, ENTRY, 
719                 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
720                 conn ? conn->c_connid : -1, names[0]+1, dn.bv_val );
721 #else
722         Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
723                 "%s=\"%s\"\n",
724                         conn ? conn->c_connid : -1,
725                         names[0]+1, dn.bv_val );
726 #endif
727 done:   AC_MEMCPY( out, in, inlen );
728         out[inlen] = '\0';
729
730         *out_len = inlen;
731
732         return SASL_OK;
733 }
734
735 static int
736 slap_sasl_authorize(
737         sasl_conn_t *sconn,
738         void *context,
739         char *requested_user,
740         unsigned rlen,
741         char *auth_identity,
742         unsigned alen,
743         const char *def_realm,
744         unsigned urlen,
745         struct propctx *props)
746 {
747         Connection *conn = (Connection *)context;
748         struct propval auxvals[3];
749         struct berval authcDN, authzDN;
750         int rc;
751
752 #ifdef NEW_LOGGING
753         LDAP_LOG( TRANSPORT, ENTRY, 
754                 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
755                 conn ? conn->c_connid : -1, auth_identity, requested_user);
756 #else
757         Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
758                 "authcid=\"%s\" authzid=\"%s\"\n",
759                 conn ? conn->c_connid : -1, auth_identity, requested_user );
760 #endif
761         if ( conn->c_sasl_dn.bv_val ) {
762                 ch_free( conn->c_sasl_dn.bv_val );
763                 conn->c_sasl_dn.bv_val = NULL;
764                 conn->c_sasl_dn.bv_len = 0;
765         }
766
767         prop_getnames( props, slap_propnames, auxvals );
768         
769         AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
770
771         /* Nothing to do if no authzID was given */
772         if ( !auxvals[1].name || !auxvals[1].values ) {
773                 conn->c_sasl_dn = authcDN;
774                 return SASL_OK;
775         }
776         
777         AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
778
779         rc = slap_sasl_authorized( conn, &authcDN, &authzDN );
780         ch_free( authcDN.bv_val );
781         if ( rc != LDAP_SUCCESS ) {
782 #ifdef NEW_LOGGING
783                 LDAP_LOG( TRANSPORT, INFO, 
784                         "slap_sasl_authorize: conn %ld  authorization disallowed (%d)\n",
785                         (long)(conn ? conn->c_connid : -1), rc, 0 );
786 #else
787                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
788                         " authorization disallowed (%d)\n",
789                         (long) (conn ? conn->c_connid : -1), rc, 0 );
790 #endif
791
792                 sasl_seterror( sconn, 0, "not authorized" );
793                 ch_free( authzDN.bv_val );
794                 return SASL_NOAUTHZ;
795         }
796
797         conn->c_sasl_dn = authzDN;
798
799 #ifdef NEW_LOGGING
800         LDAP_LOG( TRANSPORT, ENTRY, 
801                 "slap_sasl_authorize: conn %d authorization allowed\n",
802                 (long)(conn ? conn->c_connid : -1), 0, 0 );
803 #else
804         Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
805                 " authorization allowed\n",
806                 (long) (conn ? conn->c_connid : -1), 0, 0 );
807 #endif
808         return SASL_OK;
809
810 #else
811 static int
812 slap_sasl_authorize(
813         void *context,
814         char *authcid,
815         char *authzid,
816         const char **user,
817         const char **errstr)
818 {
819         struct berval authcDN, authzDN;
820         int rc;
821         Connection *conn = context;
822         char *realm;
823
824         *user = NULL;
825         if ( conn->c_sasl_dn.bv_val ) {
826                 ch_free( conn->c_sasl_dn.bv_val );
827                 conn->c_sasl_dn.bv_val = NULL;
828                 conn->c_sasl_dn.bv_len = 0;
829         }
830
831 #ifdef NEW_LOGGING
832         LDAP_LOG( TRANSPORT, ENTRY, 
833                 "slap_sasl_authorize: conn %d    authcid=\"%s\" authzid=\"%s\"\n",
834                 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
835                 authzid ? authzid : "<empty>" );
836 #else
837         Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
838                 "authcid=\"%s\" authzid=\"%s\"\n",
839                 (long) (conn ? conn->c_connid : -1),
840                 authcid ? authcid : "<empty>",
841                 authzid ? authzid : "<empty>" );
842 #endif
843
844         /* Figure out how much data we have for the dn */
845         rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
846         if( rc != SASL_OK && rc != SASL_NOTDONE ) {
847 #ifdef NEW_LOGGING
848                 LDAP_LOG( TRANSPORT, ERR,
849                         "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
850 #else
851                 Debug(LDAP_DEBUG_TRACE,
852                         "authorize: getprop(REALM) failed!\n", 0,0,0);
853 #endif
854                 *errstr = "Could not extract realm";
855                 return SASL_NOAUTHZ;
856         }
857
858         /* Convert the identities to DN's. If no authzid was given, client will
859            be bound as the DN matching their username */
860         rc = slap_sasl_getdn( conn, (char *)authcid, 0, realm, &authcDN, FLAG_GETDN_AUTHCID );
861         if( rc != LDAP_SUCCESS ) {
862                 *errstr = ldap_err2string( rc );
863                 return SASL_NOAUTHZ;
864         }
865         if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
866 #ifdef NEW_LOGGING
867                 LDAP_LOG( TRANSPORT, ENTRY, 
868                         "slap_sasl_authorize: conn %d  Using authcDN=%s\n",
869                         conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
870 #else
871                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
872                  "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
873 #endif
874
875                 conn->c_sasl_dn = authcDN;
876                 *errstr = NULL;
877                 return SASL_OK;
878         }
879         rc = slap_sasl_getdn( conn, (char *)authzid, 0, realm, &authzDN, FLAG_GETDN_AUTHZID );
880         if( rc != LDAP_SUCCESS ) {
881                 ch_free( authcDN.bv_val );
882                 *errstr = ldap_err2string( rc );
883                 return SASL_NOAUTHZ;
884         }
885
886         rc = slap_sasl_authorized(conn, &authcDN, &authzDN );
887         ch_free( authcDN.bv_val );
888         if( rc ) {
889 #ifdef NEW_LOGGING
890                 LDAP_LOG( TRANSPORT, INFO, 
891                         "slap_sasl_authorize: conn %ld  authorization disallowed (%d)\n",
892                         (long)(conn ? conn->c_connid : -1), rc, 0 );
893 #else
894                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
895                         " authorization disallowed (%d)\n",
896                         (long) (conn ? conn->c_connid : -1), rc, 0 );
897 #endif
898
899                 *errstr = "not authorized";
900                 ch_free( authzDN.bv_val );
901                 return SASL_NOAUTHZ;
902         }
903
904 #ifdef NEW_LOGGING
905         LDAP_LOG( TRANSPORT, RESULTS, 
906                 "slap_sasl_authorize: conn %d authorization allowed\n",
907            (long)(conn ? conn->c_connid : -1 ), 0, 0 );
908 #else
909         Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
910                 " authorization allowed\n",
911                 (long) (conn ? conn->c_connid : -1), 0, 0 );
912 #endif
913
914         conn->c_sasl_dn = authzDN;
915         *errstr = NULL;
916         return SASL_OK;
917 }
918 #endif /* SASL_VERSION_MAJOR >= 2 */
919
920 static int
921 slap_sasl_err2ldap( int saslerr )
922 {
923         int rc;
924
925         switch (saslerr) {
926                 case SASL_CONTINUE:
927                         rc = LDAP_SASL_BIND_IN_PROGRESS;
928                         break;
929                 case SASL_FAIL:
930                         rc = LDAP_OTHER;
931                         break;
932                 case SASL_NOMEM:
933                         rc = LDAP_OTHER;
934                         break;
935                 case SASL_NOMECH:
936                         rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
937                         break;
938                 case SASL_BADAUTH:
939                         rc = LDAP_INVALID_CREDENTIALS;
940                         break;
941                 case SASL_NOAUTHZ:
942                         rc = LDAP_INSUFFICIENT_ACCESS;
943                         break;
944                 case SASL_TOOWEAK:
945                 case SASL_ENCRYPT:
946                         rc = LDAP_INAPPROPRIATE_AUTH;
947                         break;
948                 default:
949                         rc = LDAP_OTHER;
950                         break;
951         }
952
953         return rc;
954 }
955 #endif
956
957
958 int slap_sasl_init( void )
959 {
960 #ifdef HAVE_CYRUS_SASL
961         int rc;
962         static sasl_callback_t server_callbacks[] = {
963                 { SASL_CB_LOG, &slap_sasl_log, NULL },
964                 { SASL_CB_LIST_END, NULL, NULL }
965         };
966
967         sasl_set_alloc(
968                 ch_malloc,
969                 ch_calloc,
970                 ch_realloc,
971                 ch_free ); 
972
973         sasl_set_mutex(
974                 ldap_pvt_sasl_mutex_new,
975                 ldap_pvt_sasl_mutex_lock,
976                 ldap_pvt_sasl_mutex_unlock,
977                 ldap_pvt_sasl_mutex_dispose );
978
979 #if SASL_VERSION_MAJOR >= 2
980         sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
981 #endif
982         /* should provide callbacks for logging */
983         /* server name should be configurable */
984         rc = sasl_server_init( server_callbacks, "slapd" );
985
986         if( rc != SASL_OK ) {
987 #ifdef NEW_LOGGING
988                 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
989 #else
990                 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
991                         0, 0, 0 );
992 #endif
993
994                 return -1;
995         }
996
997 #ifdef NEW_LOGGING
998         LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
999 #else
1000         Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1001                 0, 0, 0 );
1002 #endif
1003
1004
1005         /* default security properties */
1006         memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1007         sasl_secprops.max_ssf = INT_MAX;
1008         sasl_secprops.maxbufsize = 65536;
1009         sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1010 #endif
1011
1012         return 0;
1013 }
1014
1015 int slap_sasl_destroy( void )
1016 {
1017 #ifdef HAVE_CYRUS_SASL
1018         sasl_done();
1019 #endif
1020         free( global_host );
1021         global_host = NULL;
1022
1023         return 0;
1024 }
1025
1026 int slap_sasl_open( Connection *conn )
1027 {
1028         int cb, sc = LDAP_SUCCESS;
1029 #if SASL_VERSION_MAJOR >= 2
1030         char *ipremoteport = NULL, *iplocalport = NULL;
1031 #endif
1032
1033 #ifdef HAVE_CYRUS_SASL
1034         sasl_conn_t *ctx = NULL;
1035         sasl_callback_t *session_callbacks;
1036
1037         assert( conn->c_sasl_context == NULL );
1038         assert( conn->c_sasl_extra == NULL );
1039
1040         conn->c_sasl_layers = 0;
1041
1042         session_callbacks =
1043 #if SASL_VERSION_MAJOR >= 2
1044                 ch_calloc( 5, sizeof(sasl_callback_t));
1045 #else
1046                 ch_calloc( 3, sizeof(sasl_callback_t));
1047 #endif
1048         conn->c_sasl_extra = session_callbacks;
1049
1050         session_callbacks[cb=0].id = SASL_CB_LOG;
1051         session_callbacks[cb].proc = &slap_sasl_log;
1052         session_callbacks[cb++].context = conn;
1053
1054         session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1055         session_callbacks[cb].proc = &slap_sasl_authorize;
1056         session_callbacks[cb++].context = conn;
1057
1058 #if SASL_VERSION_MAJOR >= 2
1059         session_callbacks[cb].id = SASL_CB_CANON_USER;
1060         session_callbacks[cb].proc = &slap_sasl_canonicalize;
1061         session_callbacks[cb++].context = conn;
1062
1063         /* XXXX: this should be conditional */
1064         session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1065         session_callbacks[cb].proc = &slap_sasl_checkpass;
1066         session_callbacks[cb++].context = conn;
1067 #endif
1068
1069         session_callbacks[cb].id = SASL_CB_LIST_END;
1070         session_callbacks[cb].proc = NULL;
1071         session_callbacks[cb++].context = NULL;
1072
1073         if( global_host == NULL ) {
1074                 global_host = ldap_pvt_get_fqdn( NULL );
1075         }
1076
1077         /* create new SASL context */
1078 #if SASL_VERSION_MAJOR >= 2
1079         if ( conn->c_sock_name.bv_len != 0 &&
1080              strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1081                 char *p;
1082
1083                 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1084                 /* Convert IPv6 addresses to address;port syntax. */
1085                 p = strrchr( iplocalport, ' ' );
1086                 /* Convert IPv4 addresses to address;port syntax. */
1087                 if ( p == NULL ) p = strchr( iplocalport, ':' );
1088                 if ( p != NULL ) {
1089                         *p = ';';
1090                 }
1091         }
1092         if ( conn->c_peer_name.bv_len != 0 &&
1093              strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1094                 char *p;
1095
1096                 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1097                 /* Convert IPv6 addresses to address;port syntax. */
1098                 p = strrchr( ipremoteport, ' ' );
1099                 /* Convert IPv4 addresses to address;port syntax. */
1100                 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1101                 if ( p != NULL ) {
1102                         *p = ';';
1103                 }
1104         }
1105         sc = sasl_server_new( "ldap", global_host, global_realm,
1106                 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
1107         if ( iplocalport != NULL ) {
1108                 ch_free( iplocalport );
1109         }
1110         if ( ipremoteport != NULL ) {
1111                 ch_free( ipremoteport );
1112         }
1113 #else
1114         sc = sasl_server_new( "ldap", global_host, global_realm,
1115                 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1116 #endif
1117
1118         if( sc != SASL_OK ) {
1119 #ifdef NEW_LOGGING
1120                 LDAP_LOG( TRANSPORT, ERR, 
1121                         "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1122 #else
1123                 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1124                         sc, 0, 0 );
1125 #endif
1126
1127                 return -1;
1128         }
1129
1130         conn->c_sasl_context = ctx;
1131
1132         if( sc == SASL_OK ) {
1133                 sc = sasl_setprop( ctx,
1134                         SASL_SEC_PROPS, &sasl_secprops );
1135
1136                 if( sc != SASL_OK ) {
1137 #ifdef NEW_LOGGING
1138                         LDAP_LOG( TRANSPORT, ERR, 
1139                                 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1140 #else
1141                         Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1142                                 sc, 0, 0 );
1143 #endif
1144
1145                         slap_sasl_close( conn );
1146                         return -1;
1147                 }
1148         }
1149
1150         sc = slap_sasl_err2ldap( sc );
1151 #endif
1152         return sc;
1153 }
1154
1155 int slap_sasl_external(
1156         Connection *conn,
1157         slap_ssf_t ssf,
1158         const char *auth_id )
1159 {
1160 #if SASL_VERSION_MAJOR >= 2
1161         int sc;
1162         sasl_conn_t *ctx = conn->c_sasl_context;
1163
1164         if ( ctx == NULL ) {
1165                 return LDAP_UNAVAILABLE;
1166         }
1167
1168         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1169
1170         if ( sc != SASL_OK ) {
1171                 return LDAP_OTHER;
1172         }
1173
1174         sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1175
1176         if ( sc != SASL_OK ) {
1177                 return LDAP_OTHER;
1178         }
1179
1180 #elif defined(HAVE_CYRUS_SASL)
1181         int sc;
1182         sasl_conn_t *ctx = conn->c_sasl_context;
1183         sasl_external_properties_t extprops;
1184
1185         if ( ctx == NULL ) {
1186                 return LDAP_UNAVAILABLE;
1187         }
1188
1189         memset( &extprops, '\0', sizeof(extprops) );
1190         extprops.ssf = ssf;
1191         extprops.auth_id = (char *) auth_id;
1192
1193         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1194                 (void *) &extprops );
1195
1196         if ( sc != SASL_OK ) {
1197                 return LDAP_OTHER;
1198         }
1199 #endif
1200
1201         return LDAP_SUCCESS;
1202 }
1203
1204 int slap_sasl_reset( Connection *conn )
1205 {
1206 #ifdef HAVE_CYRUS_SASL
1207         sasl_conn_t *ctx = conn->c_sasl_context;
1208
1209         if( ctx != NULL ) {
1210         }
1211 #endif
1212         /* must return "anonymous" */
1213         return LDAP_SUCCESS;
1214 }
1215
1216 char ** slap_sasl_mechs( Connection *conn )
1217 {
1218         char **mechs = NULL;
1219
1220 #ifdef HAVE_CYRUS_SASL
1221         sasl_conn_t *ctx = conn->c_sasl_context;
1222
1223         if( ctx != NULL ) {
1224                 int sc;
1225                 SASL_CONST char *mechstr;
1226
1227                 sc = sasl_listmech( ctx,
1228                         NULL, NULL, ",", NULL,
1229                         &mechstr, NULL, NULL );
1230
1231                 if( sc != SASL_OK ) {
1232 #ifdef NEW_LOGGING
1233                         LDAP_LOG( TRANSPORT, ERR, 
1234                                 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1235 #else
1236                         Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1237                                 sc, 0, 0 );
1238 #endif
1239
1240                         return NULL;
1241                 }
1242
1243                 mechs = str2charray( mechstr, "," );
1244
1245 #if SASL_VERSION_MAJOR < 2
1246                 ch_free( mechstr );
1247 #endif
1248         }
1249 #endif
1250
1251         return mechs;
1252 }
1253
1254 int slap_sasl_close( Connection *conn )
1255 {
1256 #ifdef HAVE_CYRUS_SASL
1257         sasl_conn_t *ctx = conn->c_sasl_context;
1258
1259         if( ctx != NULL ) {
1260                 sasl_dispose( &ctx );
1261         }
1262
1263         conn->c_sasl_context = NULL;
1264
1265         free( conn->c_sasl_extra );
1266         conn->c_sasl_extra = NULL;
1267 #endif
1268
1269         return LDAP_SUCCESS;
1270 }
1271
1272 int slap_sasl_bind(
1273     Connection          *conn,
1274     Operation           *op,  
1275     struct berval       *dn,  
1276     struct berval       *ndn,
1277     struct berval       *cred,
1278         struct berval                   *edn,
1279         slap_ssf_t              *ssfp )
1280 {
1281         int rc = 1;
1282
1283 #ifdef HAVE_CYRUS_SASL
1284         sasl_conn_t *ctx = conn->c_sasl_context;
1285         struct berval response;
1286         unsigned reslen = 0;
1287         const char *errstr = NULL;
1288         int sc;
1289
1290 #ifdef NEW_LOGGING
1291         LDAP_LOG( TRANSPORT, ENTRY, 
1292                 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1293                 dn->bv_len ? dn->bv_val : "",
1294                 conn->c_sasl_bind_in_progress ? "<continuing>" : 
1295                 conn->c_sasl_bind_mech.bv_val,
1296                 cred ? cred->bv_len : 0 );
1297 #else
1298         Debug(LDAP_DEBUG_ARGS,
1299                 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1300                 dn->bv_len ? dn->bv_val : "",
1301                 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1302                 cred ? cred->bv_len : 0 );
1303 #endif
1304
1305
1306         if( ctx == NULL ) {
1307                 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1308                         NULL, "SASL unavailable on this session", NULL, NULL );
1309                 return rc;
1310         }
1311
1312 #if SASL_VERSION_MAJOR >= 2
1313 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1314         sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1315 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1316         sasl_server_step( ctx, cred, clen, resp, rlen )
1317 #else
1318 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1319         sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1320 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1321         sasl_server_step( ctx, cred, clen, resp, rlen, err )
1322 #endif
1323
1324         if ( !conn->c_sasl_bind_in_progress ) {
1325                 sc = START( ctx,
1326                         conn->c_sasl_bind_mech.bv_val,
1327                         cred->bv_len ? cred->bv_val : "",
1328                         cred->bv_len,
1329                         (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1330
1331         } else {
1332                 sc = STEP( ctx,
1333                         cred->bv_val, cred->bv_len,
1334                         (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1335         }
1336
1337         response.bv_len = reslen;
1338
1339         if ( sc == SASL_OK ) {
1340                 sasl_ssf_t *ssf = NULL;
1341
1342                 *edn = conn->c_sasl_dn;
1343                 conn->c_sasl_dn.bv_val = NULL;
1344                 conn->c_sasl_dn.bv_len = 0;
1345
1346                 rc = LDAP_SUCCESS;
1347
1348                 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1349                 *ssfp = ssf ? *ssf : 0;
1350
1351                 if( *ssfp ) {
1352                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1353                         conn->c_sasl_layers++;
1354                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1355                 }
1356
1357                 send_ldap_sasl( conn, op, rc,
1358                         NULL, NULL, NULL, NULL,
1359                         response.bv_len ? &response : NULL );
1360
1361         } else if ( sc == SASL_CONTINUE ) {
1362                 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1363                         NULL, NULL, NULL, NULL, &response );
1364
1365         } else {
1366 #if SASL_VERSION_MAJOR >= 2
1367                 errstr = sasl_errdetail( ctx );
1368 #endif
1369                 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1370                         NULL, errstr, NULL, NULL );
1371         }
1372
1373 #if SASL_VERSION_MAJOR < 2
1374         if( response.bv_len ) {
1375                 ch_free( response.bv_val );
1376         }
1377 #endif
1378
1379 #ifdef NEW_LOGGING
1380         LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rc, 0, 0 );
1381 #else
1382         Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1383 #endif
1384
1385
1386 #else
1387         send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1388                 NULL, "SASL not supported", NULL, NULL );
1389 #endif
1390
1391         return rc;
1392 }
1393
1394 char* slap_sasl_secprops( const char *in )
1395 {
1396 #ifdef HAVE_CYRUS_SASL
1397         int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1398
1399         return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1400 #else
1401         return "SASL not supported";
1402 #endif
1403 }
1404
1405 #ifdef HAVE_CYRUS_SASL
1406 int
1407 slap_sasl_setpass(
1408         Connection      *conn,
1409         Operation       *op,
1410         const char      *reqoid,
1411         struct berval   *reqdata,
1412         char            **rspoid,
1413         struct berval   **rspdata,
1414         LDAPControl     *** rspctrls,
1415         const char      **text )
1416 {
1417         int rc;
1418         struct berval id = { 0, NULL }; /* needs to come from connection */
1419         struct berval new = { 0, NULL };
1420         struct berval old = { 0, NULL };
1421
1422         assert( reqoid != NULL );
1423         assert( strcmp( LDAP_EXOP_MODIFY_PASSWD, reqoid ) == 0 );
1424
1425         rc = sasl_getprop( conn->c_sasl_context, SASL_USERNAME,
1426                 (SASL_CONST void **)&id.bv_val );
1427
1428         if( rc != SASL_OK ) {
1429                 *text = "unable to retrieve SASL username";
1430                 rc = LDAP_OTHER;
1431                 goto done;
1432         }
1433
1434 #ifdef NEW_LOGGING
1435         LDAP_LOG( BACKEND, ENTRY,
1436                 "slap_sasl_setpass: \"%s\"\n",
1437                 id.bv_val ? id.bv_val : "", 0, 0);
1438 #else
1439         Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1440                 id.bv_val ? id.bv_val : "", 0, 0 );
1441 #endif
1442
1443         rc = slap_passwd_parse( reqdata,
1444                 NULL, &old, &new, text );
1445
1446         if( rc != LDAP_SUCCESS ) {
1447                 goto done;
1448         }
1449
1450         if( new.bv_len == 0 ) {
1451                 slap_passwd_generate(&new);
1452
1453                 if( new.bv_len == 0 ) {
1454                         *text = "password generation failed.";
1455                         rc = LDAP_OTHER;
1456                         goto done;
1457                 }
1458                 
1459                 *rspdata = slap_passwd_return( &new );
1460         }
1461
1462 #if SASL_VERSION_MAJOR < 2
1463         rc = sasl_setpass( conn->c_sasl_context,
1464                 id.bv_val, new.bv_val, new.bv_len, 0, text );
1465 #else
1466         rc = sasl_setpass( conn->c_sasl_context, id.bv_val,
1467                 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1468         if( rc != SASL_OK ) {
1469                 *text = sasl_errdetail( conn->c_sasl_context );
1470         }
1471 #endif
1472         switch(rc) {
1473                 case SASL_OK:
1474                         rc = LDAP_SUCCESS;
1475                         break;
1476
1477                 case SASL_NOCHANGE:
1478                 case SASL_NOMECH:
1479                 case SASL_DISABLED:
1480                 case SASL_PWLOCK:
1481                 case SASL_FAIL:
1482                 case SASL_BADPARAM:
1483                 default:
1484                         rc = LDAP_OTHER;
1485         }
1486
1487 done:
1488         return rc;
1489 }
1490 #endif