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