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