]> git.sur5r.net Git - openldap/blob - servers/slapd/sasl.c
be7b1bb51368184bfe5dc8c897e53c0fa3d2ff77
[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, CRIT,
55                                            "%s: line %d: missing policy in \"sasl-authz-policy <policy>\" line\n",
56                                            fname, lineno, 0 );
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, 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, CRIT,
90                                            "%s: line %d: missing host in \"sasl-host <host>\" line\n",
91                                            fname, lineno, 0 );
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, CRIT,
104                                            "%s: line %d: already set sasl-host!\n",
105                                            fname, lineno, 0 );
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, CRIT,
123                                            "%s: line %d: missing realm in \"sasl-realm <realm>\" line.\n",
124                                            fname, lineno, 0 );
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, CRIT,
137                                            "%s: line %d: already set sasl-realm!\n",
138                                            fname, lineno, 0 );
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, CRIT,
158                                            "%s: line %d: need 2 args in "
159                                            "\"saslregexp <match> <replace>\"\n",
160                                            fname, lineno, 0 );
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, CRIT,
181                                            "%s: line %d: missing flags in "
182                                            "\"sasl-secprops <properties>\" line\n",
183                                            fname, lineno, 0 );
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, 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", 0 );
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 = lutil_strcopy( dn->bv_val, "uid=" );
404                 p = lutil_strncopy( p, c1, dn->bv_len );
405
406                 if( realm ) {
407                         int rlen = dn->bv_len - ( realm - c1 );
408                         p = lutil_strcopy( p, ",cn=" );
409                         p = lutil_strncopy( p, realm, rlen );
410                         realm[-1] = '@';
411                 } else if( user_realm && *user_realm ) {
412                         p = lutil_strcopy( p, ",cn=" );
413                         p = lutil_strcopy( p, user_realm );
414                 }
415
416                 if( conn->c_sasl_bind_mech.bv_len ) {
417                         p = lutil_strcopy( p, ",cn=" );
418                         p = lutil_strcopy( p, conn->c_sasl_bind_mech.bv_val );
419                 }
420                 p = lutil_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 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
637  * auxiliary property, so that we can refer to it in sasl_authorize
638  * without interfering with anything else. Also, the SASL username
639  * buffer is constrained to 256 characters, and our DNs could be
640  * much longer (totally arbitrary length)...
641  */
642 static int
643 slap_sasl_canonicalize(
644         sasl_conn_t *sconn,
645         void *context,
646         const char *in,
647         unsigned inlen,
648         unsigned flags,
649         const char *user_realm,
650         char *out,
651         unsigned out_max,
652         unsigned *out_len)
653 {
654         Connection *conn = (Connection *)context;
655         struct propctx *props = sasl_auxprop_getctx( sconn );
656         struct propval auxvals[3];
657         struct berval dn;
658         int rc, which;
659         const char *names[2];
660
661         *out_len = 0;
662
663 #ifdef NEW_LOGGING
664         LDAP_LOG( TRANSPORT, ENTRY, 
665                 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
666                 conn ? conn->c_connid : -1,
667                 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid", in ? in : "<empty>");
668 #else
669         Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
670                 "%s=\"%s\"\n",
671                         conn ? conn->c_connid : -1,
672                         (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
673                         in ? in : "<empty>" );
674 #endif
675
676         /* If name is too big, just truncate. We don't care, we're
677          * using DNs, not the usernames.
678          */
679         if ( inlen > out_max )
680                 inlen = out_max-1;
681
682         /* See if we need to add request, can only do it once */
683         prop_getnames( props, slap_propnames, auxvals );
684         if ( !auxvals[0].name )
685                 prop_request( props, slap_propnames );
686
687         if ( flags & SASL_CU_AUTHID )
688                 which = 0;
689         else
690                 which = 1;
691
692         /* Already been here? */
693         if ( auxvals[which].values )
694                 goto done;
695
696         if ( flags == SASL_CU_AUTHZID ) {
697         /* If we got unqualified authzid's, they probably came from SASL
698          * itself just passing the authcid to us. Look inside the oparams
699          * structure to see if that's true. (HACK: the out_len pointer is
700          * the address of a member of a sasl_out_params_t structure...)
701          */
702                 sasl_out_params_t dummy;
703                 int offset = (void *)&dummy.ulen - (void *)&dummy.authid;
704                 char **authid = (void *)out_len - offset;
705                 if ( *authid && !strcmp( in, *authid ) )
706                         goto done;
707         }
708
709         rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
710                 (flags & SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
711         if ( rc != LDAP_SUCCESS ) {
712                 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
713                 return SASL_NOAUTHZ;
714         }               
715
716         names[0] = slap_propnames[which];
717         names[1] = NULL;
718
719         prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
720                 
721 #ifdef NEW_LOGGING
722         LDAP_LOG( TRANSPORT, ENTRY, 
723                 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
724                 conn ? conn->c_connid : -1, names[0]+1, dn.bv_val );
725 #else
726         Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
727                 "%s=\"%s\"\n",
728                         conn ? conn->c_connid : -1,
729                         names[0]+1, dn.bv_val );
730 #endif
731 done:   AC_MEMCPY( out, in, inlen );
732         out[inlen] = '\0';
733
734         *out_len = inlen;
735
736         return SASL_OK;
737 }
738
739 static int
740 slap_sasl_authorize(
741         sasl_conn_t *sconn,
742         void *context,
743         char *requested_user,
744         unsigned rlen,
745         char *auth_identity,
746         unsigned alen,
747         const char *def_realm,
748         unsigned urlen,
749         struct propctx *props)
750 {
751         Connection *conn = (Connection *)context;
752         struct propval auxvals[3];
753         struct berval authcDN, authzDN;
754         int rc;
755
756 #ifdef NEW_LOGGING
757         LDAP_LOG( TRANSPORT, ENTRY, 
758                 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
759                 conn ? conn->c_connid : -1, auth_identity, requested_user);
760 #else
761         Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
762                 "authcid=\"%s\" authzid=\"%s\"\n",
763                 conn ? conn->c_connid : -1, auth_identity, requested_user );
764 #endif
765         if ( conn->c_sasl_dn.bv_val ) {
766                 ch_free( conn->c_sasl_dn.bv_val );
767                 conn->c_sasl_dn.bv_val = NULL;
768                 conn->c_sasl_dn.bv_len = 0;
769         }
770
771         prop_getnames( props, slap_propnames, auxvals );
772         
773         AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
774
775         /* Nothing to do if no authzID was given */
776         if ( !auxvals[1].name || !auxvals[1].values ) {
777                 conn->c_sasl_dn = authcDN;
778                 return SASL_OK;
779         }
780         
781         AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
782
783         rc = slap_sasl_authorized( conn, &authcDN, &authzDN );
784         ch_free( authcDN.bv_val );
785         if ( rc != LDAP_SUCCESS ) {
786 #ifdef NEW_LOGGING
787                 LDAP_LOG( TRANSPORT, INFO, 
788                         "slap_sasl_authorize: conn %ld  authorization disallowed (%d)\n",
789                         (long)(conn ? conn->c_connid : -1), rc, 0 );
790 #else
791                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
792                         " authorization disallowed (%d)\n",
793                         (long) (conn ? conn->c_connid : -1), rc, 0 );
794 #endif
795
796                 sasl_seterror( sconn, 0, "not authorized" );
797                 ch_free( authzDN.bv_val );
798                 return SASL_NOAUTHZ;
799         }
800
801         conn->c_sasl_dn = authzDN;
802
803 #ifdef NEW_LOGGING
804         LDAP_LOG( TRANSPORT, ENTRY, 
805                 "slap_sasl_authorize: conn %d authorization allowed\n",
806                 (long)(conn ? conn->c_connid : -1), 0, 0 );
807 #else
808         Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
809                 " authorization allowed\n",
810                 (long) (conn ? conn->c_connid : -1), 0, 0 );
811 #endif
812         return SASL_OK;
813
814 #else
815 static int
816 slap_sasl_authorize(
817         void *context,
818         char *authcid,
819         char *authzid,
820         const char **user,
821         const char **errstr)
822 {
823         struct berval authcDN, authzDN;
824         int rc;
825         Connection *conn = context;
826         char *realm;
827
828         *user = NULL;
829         if ( conn->c_sasl_dn.bv_val ) {
830                 ch_free( conn->c_sasl_dn.bv_val );
831                 conn->c_sasl_dn.bv_val = NULL;
832                 conn->c_sasl_dn.bv_len = 0;
833         }
834
835 #ifdef NEW_LOGGING
836         LDAP_LOG( TRANSPORT, ENTRY, 
837                 "slap_sasl_authorize: conn %d    authcid=\"%s\" authzid=\"%s\"\n",
838                 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
839                 authzid ? authzid : "<empty>" );
840 #else
841         Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
842                 "authcid=\"%s\" authzid=\"%s\"\n",
843                 (long) (conn ? conn->c_connid : -1),
844                 authcid ? authcid : "<empty>",
845                 authzid ? authzid : "<empty>" );
846 #endif
847
848         /* Figure out how much data we have for the dn */
849         rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
850         if( rc != SASL_OK && rc != SASL_NOTDONE ) {
851 #ifdef NEW_LOGGING
852                 LDAP_LOG( TRANSPORT, ERR,
853                         "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
854 #else
855                 Debug(LDAP_DEBUG_TRACE,
856                         "authorize: getprop(REALM) failed!\n", 0,0,0);
857 #endif
858                 *errstr = "Could not extract realm";
859                 return SASL_NOAUTHZ;
860         }
861
862         /* Convert the identities to DN's. If no authzid was given, client will
863            be bound as the DN matching their username */
864         rc = slap_sasl_getdn( conn, (char *)authcid, 0, realm, &authcDN, FLAG_GETDN_AUTHCID );
865         if( rc != LDAP_SUCCESS ) {
866                 *errstr = ldap_err2string( rc );
867                 return SASL_NOAUTHZ;
868         }
869         if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
870 #ifdef NEW_LOGGING
871                 LDAP_LOG( TRANSPORT, ENTRY, 
872                         "slap_sasl_authorize: conn %d  Using authcDN=%s\n",
873                         conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
874 #else
875                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
876                  "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
877 #endif
878
879                 conn->c_sasl_dn = authcDN;
880                 *errstr = NULL;
881                 return SASL_OK;
882         }
883         rc = slap_sasl_getdn( conn, (char *)authzid, 0, realm, &authzDN, FLAG_GETDN_AUTHZID );
884         if( rc != LDAP_SUCCESS ) {
885                 ch_free( authcDN.bv_val );
886                 *errstr = ldap_err2string( rc );
887                 return SASL_NOAUTHZ;
888         }
889
890         rc = slap_sasl_authorized(conn, &authcDN, &authzDN );
891         ch_free( authcDN.bv_val );
892         if( rc ) {
893 #ifdef NEW_LOGGING
894                 LDAP_LOG( TRANSPORT, INFO, 
895                         "slap_sasl_authorize: conn %ld  authorization disallowed (%d)\n",
896                         (long)(conn ? conn->c_connid : -1), rc, 0 );
897 #else
898                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
899                         " authorization disallowed (%d)\n",
900                         (long) (conn ? conn->c_connid : -1), rc, 0 );
901 #endif
902
903                 *errstr = "not authorized";
904                 ch_free( authzDN.bv_val );
905                 return SASL_NOAUTHZ;
906         }
907
908 #ifdef NEW_LOGGING
909         LDAP_LOG( TRANSPORT, RESULTS, 
910                 "slap_sasl_authorize: conn %d authorization allowed\n",
911            (long)(conn ? conn->c_connid : -1 ), 0, 0 );
912 #else
913         Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
914                 " authorization allowed\n",
915                 (long) (conn ? conn->c_connid : -1), 0, 0 );
916 #endif
917
918         conn->c_sasl_dn = authzDN;
919         *errstr = NULL;
920         return SASL_OK;
921 }
922 #endif /* SASL_VERSION_MAJOR >= 2 */
923
924 static int
925 slap_sasl_err2ldap( int saslerr )
926 {
927         int rc;
928
929         switch (saslerr) {
930                 case SASL_CONTINUE:
931                         rc = LDAP_SASL_BIND_IN_PROGRESS;
932                         break;
933                 case SASL_FAIL:
934                         rc = LDAP_OTHER;
935                         break;
936                 case SASL_NOMEM:
937                         rc = LDAP_OTHER;
938                         break;
939                 case SASL_NOMECH:
940                         rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
941                         break;
942                 case SASL_BADAUTH:
943                         rc = LDAP_INVALID_CREDENTIALS;
944                         break;
945                 case SASL_NOAUTHZ:
946                         rc = LDAP_INSUFFICIENT_ACCESS;
947                         break;
948                 case SASL_TOOWEAK:
949                 case SASL_ENCRYPT:
950                         rc = LDAP_INAPPROPRIATE_AUTH;
951                         break;
952                 default:
953                         rc = LDAP_OTHER;
954                         break;
955         }
956
957         return rc;
958 }
959 #endif
960
961
962 int slap_sasl_init( void )
963 {
964 #ifdef HAVE_CYRUS_SASL
965         int rc;
966         static sasl_callback_t server_callbacks[] = {
967                 { SASL_CB_LOG, &slap_sasl_log, NULL },
968                 { SASL_CB_LIST_END, NULL, NULL }
969         };
970
971         sasl_set_alloc(
972                 ch_malloc,
973                 ch_calloc,
974                 ch_realloc,
975                 ch_free ); 
976
977         sasl_set_mutex(
978                 ldap_pvt_sasl_mutex_new,
979                 ldap_pvt_sasl_mutex_lock,
980                 ldap_pvt_sasl_mutex_unlock,
981                 ldap_pvt_sasl_mutex_dispose );
982
983 #if SASL_VERSION_MAJOR >= 2
984         sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
985 #endif
986         /* should provide callbacks for logging */
987         /* server name should be configurable */
988         rc = sasl_server_init( server_callbacks, "slapd" );
989
990         if( rc != SASL_OK ) {
991 #ifdef NEW_LOGGING
992                 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
993 #else
994                 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
995                         0, 0, 0 );
996 #endif
997
998                 return -1;
999         }
1000
1001 #ifdef NEW_LOGGING
1002         LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1003 #else
1004         Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1005                 0, 0, 0 );
1006 #endif
1007
1008
1009         /* default security properties */
1010         memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1011         sasl_secprops.max_ssf = INT_MAX;
1012         sasl_secprops.maxbufsize = 65536;
1013         sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1014 #endif
1015
1016         return 0;
1017 }
1018
1019 int slap_sasl_destroy( void )
1020 {
1021 #ifdef HAVE_CYRUS_SASL
1022         sasl_done();
1023 #endif
1024         free( global_host );
1025         global_host = NULL;
1026
1027         return 0;
1028 }
1029
1030 int slap_sasl_open( Connection *conn )
1031 {
1032         int cb, sc = LDAP_SUCCESS;
1033 #if SASL_VERSION_MAJOR >= 2
1034         char *ipremoteport = NULL, *iplocalport = NULL;
1035 #endif
1036
1037 #ifdef HAVE_CYRUS_SASL
1038         sasl_conn_t *ctx = NULL;
1039         sasl_callback_t *session_callbacks;
1040
1041         assert( conn->c_sasl_context == NULL );
1042         assert( conn->c_sasl_extra == NULL );
1043
1044         conn->c_sasl_layers = 0;
1045
1046         session_callbacks =
1047 #if SASL_VERSION_MAJOR >= 2
1048                 ch_calloc( 5, sizeof(sasl_callback_t));
1049 #else
1050                 ch_calloc( 3, sizeof(sasl_callback_t));
1051 #endif
1052         conn->c_sasl_extra = session_callbacks;
1053
1054         session_callbacks[cb=0].id = SASL_CB_LOG;
1055         session_callbacks[cb].proc = &slap_sasl_log;
1056         session_callbacks[cb++].context = conn;
1057
1058         session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1059         session_callbacks[cb].proc = &slap_sasl_authorize;
1060         session_callbacks[cb++].context = conn;
1061
1062 #if SASL_VERSION_MAJOR >= 2
1063         session_callbacks[cb].id = SASL_CB_CANON_USER;
1064         session_callbacks[cb].proc = &slap_sasl_canonicalize;
1065         session_callbacks[cb++].context = conn;
1066
1067         /* XXXX: this should be conditional */
1068         session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1069         session_callbacks[cb].proc = &slap_sasl_checkpass;
1070         session_callbacks[cb++].context = conn;
1071 #endif
1072
1073         session_callbacks[cb].id = SASL_CB_LIST_END;
1074         session_callbacks[cb].proc = NULL;
1075         session_callbacks[cb++].context = NULL;
1076
1077         if( global_host == NULL ) {
1078                 global_host = ldap_pvt_get_fqdn( NULL );
1079         }
1080
1081         /* create new SASL context */
1082 #if SASL_VERSION_MAJOR >= 2
1083         if ( conn->c_sock_name.bv_len != 0 &&
1084              strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1085                 char *p;
1086
1087                 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1088                 /* Convert IPv6 addresses to address;port syntax. */
1089                 p = strrchr( iplocalport, ' ' );
1090                 /* Convert IPv4 addresses to address;port syntax. */
1091                 if ( p == NULL ) p = strchr( iplocalport, ':' );
1092                 if ( p != NULL ) {
1093                         *p = ';';
1094                 }
1095         }
1096         if ( conn->c_peer_name.bv_len != 0 &&
1097              strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1098                 char *p;
1099
1100                 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1101                 /* Convert IPv6 addresses to address;port syntax. */
1102                 p = strrchr( ipremoteport, ' ' );
1103                 /* Convert IPv4 addresses to address;port syntax. */
1104                 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1105                 if ( p != NULL ) {
1106                         *p = ';';
1107                 }
1108         }
1109         sc = sasl_server_new( "ldap", global_host, global_realm,
1110                 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
1111         if ( iplocalport != NULL ) {
1112                 ch_free( iplocalport );
1113         }
1114         if ( ipremoteport != NULL ) {
1115                 ch_free( ipremoteport );
1116         }
1117 #else
1118         sc = sasl_server_new( "ldap", global_host, global_realm,
1119                 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1120 #endif
1121
1122         if( sc != SASL_OK ) {
1123 #ifdef NEW_LOGGING
1124                 LDAP_LOG( TRANSPORT, ERR, 
1125                         "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1126 #else
1127                 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1128                         sc, 0, 0 );
1129 #endif
1130
1131                 return -1;
1132         }
1133
1134         conn->c_sasl_context = ctx;
1135
1136         if( sc == SASL_OK ) {
1137                 sc = sasl_setprop( ctx,
1138                         SASL_SEC_PROPS, &sasl_secprops );
1139
1140                 if( sc != SASL_OK ) {
1141 #ifdef NEW_LOGGING
1142                         LDAP_LOG( TRANSPORT, ERR, 
1143                                 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1144 #else
1145                         Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1146                                 sc, 0, 0 );
1147 #endif
1148
1149                         slap_sasl_close( conn );
1150                         return -1;
1151                 }
1152         }
1153
1154         sc = slap_sasl_err2ldap( sc );
1155 #endif
1156         return sc;
1157 }
1158
1159 int slap_sasl_external(
1160         Connection *conn,
1161         slap_ssf_t ssf,
1162         const char *auth_id )
1163 {
1164 #if SASL_VERSION_MAJOR >= 2
1165         int sc;
1166         sasl_conn_t *ctx = conn->c_sasl_context;
1167
1168         if ( ctx == NULL ) {
1169                 return LDAP_UNAVAILABLE;
1170         }
1171
1172         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1173
1174         if ( sc != SASL_OK ) {
1175                 return LDAP_OTHER;
1176         }
1177
1178         sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1179
1180         if ( sc != SASL_OK ) {
1181                 return LDAP_OTHER;
1182         }
1183
1184 #elif defined(HAVE_CYRUS_SASL)
1185         int sc;
1186         sasl_conn_t *ctx = conn->c_sasl_context;
1187         sasl_external_properties_t extprops;
1188
1189         if ( ctx == NULL ) {
1190                 return LDAP_UNAVAILABLE;
1191         }
1192
1193         memset( &extprops, '\0', sizeof(extprops) );
1194         extprops.ssf = ssf;
1195         extprops.auth_id = (char *) auth_id;
1196
1197         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1198                 (void *) &extprops );
1199
1200         if ( sc != SASL_OK ) {
1201                 return LDAP_OTHER;
1202         }
1203 #endif
1204
1205         return LDAP_SUCCESS;
1206 }
1207
1208 int slap_sasl_reset( Connection *conn )
1209 {
1210 #ifdef HAVE_CYRUS_SASL
1211         sasl_conn_t *ctx = conn->c_sasl_context;
1212
1213         if( ctx != NULL ) {
1214         }
1215 #endif
1216         /* must return "anonymous" */
1217         return LDAP_SUCCESS;
1218 }
1219
1220 char ** slap_sasl_mechs( Connection *conn )
1221 {
1222         char **mechs = NULL;
1223
1224 #ifdef HAVE_CYRUS_SASL
1225         sasl_conn_t *ctx = conn->c_sasl_context;
1226
1227         if( ctx != NULL ) {
1228                 int sc;
1229                 SASL_CONST char *mechstr;
1230
1231                 sc = sasl_listmech( ctx,
1232                         NULL, NULL, ",", NULL,
1233                         &mechstr, NULL, NULL );
1234
1235                 if( sc != SASL_OK ) {
1236 #ifdef NEW_LOGGING
1237                         LDAP_LOG( TRANSPORT, ERR, 
1238                                 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1239 #else
1240                         Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1241                                 sc, 0, 0 );
1242 #endif
1243
1244                         return NULL;
1245                 }
1246
1247                 mechs = str2charray( mechstr, "," );
1248
1249 #if SASL_VERSION_MAJOR < 2
1250                 ch_free( mechstr );
1251 #endif
1252         }
1253 #endif
1254
1255         return mechs;
1256 }
1257
1258 int slap_sasl_close( Connection *conn )
1259 {
1260 #ifdef HAVE_CYRUS_SASL
1261         sasl_conn_t *ctx = conn->c_sasl_context;
1262
1263         if( ctx != NULL ) {
1264                 sasl_dispose( &ctx );
1265         }
1266
1267         conn->c_sasl_context = NULL;
1268
1269         free( conn->c_sasl_extra );
1270         conn->c_sasl_extra = NULL;
1271 #endif
1272
1273         return LDAP_SUCCESS;
1274 }
1275
1276 int slap_sasl_bind(
1277     Connection          *conn,
1278     Operation           *op,  
1279     struct berval       *dn,  
1280     struct berval       *ndn,
1281     struct berval       *cred,
1282         struct berval                   *edn,
1283         slap_ssf_t              *ssfp )
1284 {
1285         int rc = 1;
1286
1287 #ifdef HAVE_CYRUS_SASL
1288         sasl_conn_t *ctx = conn->c_sasl_context;
1289         struct berval response;
1290         unsigned reslen = 0;
1291         const char *errstr = NULL;
1292         int sc;
1293
1294 #ifdef NEW_LOGGING
1295         LDAP_LOG( TRANSPORT, ENTRY, 
1296                 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1297                 dn->bv_len ? dn->bv_val : "",
1298                 conn->c_sasl_bind_in_progress ? "<continuing>" : 
1299                 conn->c_sasl_bind_mech.bv_val,
1300                 cred ? cred->bv_len : 0 );
1301 #else
1302         Debug(LDAP_DEBUG_ARGS,
1303                 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1304                 dn->bv_len ? dn->bv_val : "",
1305                 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1306                 cred ? cred->bv_len : 0 );
1307 #endif
1308
1309
1310         if( ctx == NULL ) {
1311                 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1312                         NULL, "SASL unavailable on this session", NULL, NULL );
1313                 return rc;
1314         }
1315
1316 #if SASL_VERSION_MAJOR >= 2
1317 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1318         sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1319 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1320         sasl_server_step( ctx, cred, clen, resp, rlen )
1321 #else
1322 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1323         sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1324 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1325         sasl_server_step( ctx, cred, clen, resp, rlen, err )
1326 #endif
1327
1328         if ( !conn->c_sasl_bind_in_progress ) {
1329                 sc = START( ctx,
1330                         conn->c_sasl_bind_mech.bv_val,
1331                         cred->bv_len ? cred->bv_val : "",
1332                         cred->bv_len,
1333                         (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1334
1335         } else {
1336                 sc = STEP( ctx,
1337                         cred->bv_val, cred->bv_len,
1338                         (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1339         }
1340
1341         response.bv_len = reslen;
1342
1343         if ( sc == SASL_OK ) {
1344                 sasl_ssf_t *ssf = NULL;
1345
1346                 *edn = conn->c_sasl_dn;
1347                 conn->c_sasl_dn.bv_val = NULL;
1348                 conn->c_sasl_dn.bv_len = 0;
1349
1350                 rc = LDAP_SUCCESS;
1351
1352                 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1353                 *ssfp = ssf ? *ssf : 0;
1354
1355                 if( *ssfp ) {
1356                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1357                         conn->c_sasl_layers++;
1358                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1359                 }
1360
1361                 send_ldap_sasl( conn, op, rc,
1362                         NULL, NULL, NULL, NULL,
1363                         response.bv_len ? &response : NULL );
1364
1365         } else if ( sc == SASL_CONTINUE ) {
1366                 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1367                         NULL, NULL, NULL, NULL, &response );
1368
1369         } else {
1370 #if SASL_VERSION_MAJOR >= 2
1371                 errstr = sasl_errdetail( ctx );
1372 #endif
1373                 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1374                         NULL, errstr, NULL, NULL );
1375         }
1376
1377 #if SASL_VERSION_MAJOR < 2
1378         if( response.bv_len ) {
1379                 ch_free( response.bv_val );
1380         }
1381 #endif
1382
1383 #ifdef NEW_LOGGING
1384         LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rc, 0, 0 );
1385 #else
1386         Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1387 #endif
1388
1389
1390 #else
1391         send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1392                 NULL, "SASL not supported", NULL, NULL );
1393 #endif
1394
1395         return rc;
1396 }
1397
1398 char* slap_sasl_secprops( const char *in )
1399 {
1400 #ifdef HAVE_CYRUS_SASL
1401         int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1402
1403         return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1404 #else
1405         return "SASL not supported";
1406 #endif
1407 }
1408
1409 #ifdef HAVE_CYRUS_SASL
1410 int
1411 slap_sasl_setpass(
1412         Connection      *conn,
1413         Operation       *op,
1414         const char      *reqoid,
1415         struct berval   *reqdata,
1416         char            **rspoid,
1417         struct berval   **rspdata,
1418         LDAPControl     *** rspctrls,
1419         const char      **text )
1420 {
1421         int rc;
1422         struct berval id = { 0, NULL }; /* needs to come from connection */
1423         struct berval new = { 0, NULL };
1424         struct berval old = { 0, NULL };
1425
1426         assert( reqoid != NULL );
1427         assert( strcmp( LDAP_EXOP_MODIFY_PASSWD, reqoid ) == 0 );
1428
1429         rc = sasl_getprop( conn->c_sasl_context, SASL_USERNAME,
1430                 (SASL_CONST void **)&id.bv_val );
1431
1432         if( rc != SASL_OK ) {
1433                 *text = "unable to retrieve SASL username";
1434                 rc = LDAP_OTHER;
1435                 goto done;
1436         }
1437
1438 #ifdef NEW_LOGGING
1439         LDAP_LOG( BACKEND, ENTRY,
1440                 "slap_sasl_setpass: \"%s\"\n",
1441                 id.bv_val ? id.bv_val : "", 0, 0);
1442 #else
1443         Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1444                 id.bv_val ? id.bv_val : "", 0, 0 );
1445 #endif
1446
1447         rc = slap_passwd_parse( reqdata,
1448                 NULL, &old, &new, text );
1449
1450         if( rc != LDAP_SUCCESS ) {
1451                 goto done;
1452         }
1453
1454         if( new.bv_len == 0 ) {
1455                 slap_passwd_generate(&new);
1456
1457                 if( new.bv_len == 0 ) {
1458                         *text = "password generation failed.";
1459                         rc = LDAP_OTHER;
1460                         goto done;
1461                 }
1462                 
1463                 *rspdata = slap_passwd_return( &new );
1464         }
1465
1466 #if SASL_VERSION_MAJOR < 2
1467         rc = sasl_setpass( conn->c_sasl_context,
1468                 id.bv_val, new.bv_val, new.bv_len, 0, text );
1469 #else
1470         rc = sasl_setpass( conn->c_sasl_context, id.bv_val,
1471                 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1472         if( rc != SASL_OK ) {
1473                 *text = sasl_errdetail( conn->c_sasl_context );
1474         }
1475 #endif
1476         switch(rc) {
1477                 case SASL_OK:
1478                         rc = LDAP_SUCCESS;
1479                         break;
1480
1481                 case SASL_NOCHANGE:
1482                 case SASL_NOMECH:
1483                 case SASL_DISABLED:
1484                 case SASL_PWLOCK:
1485                 case SASL_FAIL:
1486                 case SASL_BADPARAM:
1487                 default:
1488                         rc = LDAP_OTHER;
1489         }
1490
1491 done:
1492         return rc;
1493 }
1494 #endif