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