]> git.sur5r.net Git - openldap/blob - servers/slapd/sasl.c
cffc17423c305465c73c6c4c83d8540cf981250f
[openldap] / servers / slapd / sasl.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19 #include <ac/stdlib.h>
20 #include <ac/string.h>
21
22 #include <lber.h>
23 #include <ldap_log.h>
24
25 #include "slap.h"
26
27 #include <limits.h>
28
29 #ifdef HAVE_CYRUS_SASL
30 # ifdef HAVE_SASL_SASL_H
31 #  include <sasl/sasl.h>
32 # else
33 #  include <sasl.h>
34 # endif
35
36 # if SASL_VERSION_MAJOR >= 2
37 # ifdef HAVE_SASL_SASL_H
38 #  include <sasl/saslplug.h>
39 # else
40 #  include <saslplug.h>
41 # endif
42 #  define       SASL_CONST const
43 # else
44 #  define       SASL_CONST
45 # endif
46
47 #define SASL_VERSION_FULL       ((SASL_VERSION_MAJOR << 16) |\
48         (SASL_VERSION_MINOR << 8) | SASL_VERSION_STEP)
49
50 static sasl_security_properties_t sasl_secprops;
51 #elif defined( SLAP_BUILTIN_SASL )
52 /*
53  * built-in SASL implementation
54  *      only supports EXTERNAL
55  */
56 typedef struct sasl_ctx {
57         slap_ssf_t sc_external_ssf;
58         struct berval sc_external_id;
59 } SASL_CTX;
60
61 #endif
62
63 #include "ldap_pvt.h"
64 #include "lber_pvt.h"
65 #include <lutil.h>
66
67 static struct berval ext_bv = BER_BVC( "EXTERNAL" );
68
69 int slap_sasl_config( int cargc, char **cargv, char *line,
70         const char *fname, int lineno )
71 {
72                 /* set SASL proxy authorization policy */
73                 if ( strcasecmp( cargv[0], "sasl-authz-policy" ) == 0 ) {
74                         if ( cargc != 2 ) {
75 #ifdef NEW_LOGGING
76                                 LDAP_LOG( CONFIG, CRIT,
77                                         "%s: line %d: missing policy in"
78                                         " \"sasl-authz-policy <policy>\" line\n",
79                                         fname, lineno, 0 );
80 #else
81                                 Debug( LDAP_DEBUG_ANY,
82                                         "%s: line %d: missing policy in"
83                                         " \"sasl-authz-policy <policy>\" line\n",
84                                     fname, lineno, 0 );
85 #endif
86
87                                 return( 1 );
88                         }
89                         if ( slap_sasl_setpolicy( cargv[1] ) ) {
90 #ifdef NEW_LOGGING
91                                 LDAP_LOG( CONFIG, CRIT,
92                                            "%s: line %d: unable "
93                                            "to parse value \"%s\" "
94                                            "in \"sasl-authz-policy "
95                                            "<policy>\" line.\n",
96                                            fname, lineno, cargv[1] );
97 #else
98                                 Debug( LDAP_DEBUG_ANY,
99                                         "%s: line %d: unable "
100                                         "to parse value \"%s\" "
101                                         "in \"sasl-authz-policy "
102                                         "<policy>\" line\n",
103                                         fname, lineno, cargv[1] );
104 #endif
105                                 return( 1 );
106                         }
107
108                 } else if ( !strcasecmp( cargv[0], "sasl-regexp" ) 
109                         || !strcasecmp( cargv[0], "saslregexp" ) )
110                 {
111                         int rc;
112                         if ( cargc != 3 ) {
113 #ifdef NEW_LOGGING
114                                 LDAP_LOG( CONFIG, CRIT,
115                                         "%s: line %d: need 2 args in "
116                                         "\"saslregexp <match> <replace>\"\n",
117                                         fname, lineno, 0 );
118 #else
119                                 Debug( LDAP_DEBUG_ANY, 
120                                         "%s: line %d: need 2 args in "
121                                         "\"saslregexp <match> <replace>\"\n",
122                                         fname, lineno, 0 );
123 #endif
124
125                                 return( 1 );
126                         }
127                         rc = slap_sasl_regexp_config( cargv[1], cargv[2] );
128                         if ( rc ) {
129                                 return rc;
130                         }
131
132 #ifdef HAVE_CYRUS_SASL
133                 /* set SASL host */
134                 } else if ( strcasecmp( cargv[0], "sasl-host" ) == 0 ) {
135                         if ( cargc < 2 ) {
136 #ifdef NEW_LOGGING
137                                 LDAP_LOG( CONFIG, CRIT,
138                                         "%s: line %d: missing host in \"sasl-host <host>\" line\n",
139                                         fname, lineno, 0 );
140 #else
141                                 Debug( LDAP_DEBUG_ANY,
142                                 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
143                                     fname, lineno, 0 );
144 #endif
145
146                                 return( 1 );
147                         }
148
149                         if ( global_host != NULL ) {
150 #ifdef NEW_LOGGING
151                                 LDAP_LOG( CONFIG, CRIT,
152                                         "%s: line %d: already set sasl-host!\n",
153                                         fname, lineno, 0 );
154 #else
155                                 Debug( LDAP_DEBUG_ANY,
156                                         "%s: line %d: already set sasl-host!\n",
157                                         fname, lineno, 0 );
158 #endif
159
160                                 return 1;
161
162                         } else {
163                                 global_host = ch_strdup( cargv[1] );
164                         }
165
166                 /* set SASL realm */
167                 } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
168                         if ( cargc < 2 ) {
169 #ifdef NEW_LOGGING
170                                 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
171                                         "missing realm in \"sasl-realm <realm>\" line.\n",
172                                         fname, lineno, 0 );
173 #else
174                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
175                                         "missing realm in \"sasl-realm <realm>\" line.\n",
176                                     fname, lineno, 0 );
177 #endif
178
179                                 return( 1 );
180                         }
181
182                         if ( global_realm != NULL ) {
183 #ifdef NEW_LOGGING
184                                 LDAP_LOG( CONFIG, CRIT,
185                                         "%s: line %d: already set sasl-realm!\n",
186                                         fname, lineno, 0 );
187 #else
188                                 Debug( LDAP_DEBUG_ANY,
189                                         "%s: line %d: already set sasl-realm!\n",
190                                         fname, lineno, 0 );
191 #endif
192
193                                 return 1;
194
195                         } else {
196                                 global_realm = ch_strdup( cargv[1] );
197                         }
198
199                 /* SASL security properties */
200                 } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
201                         char *txt;
202
203                         if ( cargc < 2 ) {
204 #ifdef NEW_LOGGING
205                                 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
206                                         "missing flags in \"sasl-secprops <properties>\" line\n",
207                                         fname, lineno, 0 );
208 #else
209                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
210                                         "missing flags in \"sasl-secprops <properties>\" line\n",
211                                     fname, lineno, 0 );
212 #endif
213
214                                 return 1;
215                         }
216
217                         txt = slap_sasl_secprops( cargv[1] );
218                         if ( txt != NULL ) {
219 #ifdef NEW_LOGGING
220                                 LDAP_LOG( CONFIG, CRIT,
221                                         "%s: line %d sasl-secprops: %s\n",
222                                         fname, lineno, txt );
223 #else
224                                 Debug( LDAP_DEBUG_ANY,
225                                         "%s: line %d: sasl-secprops: %s\n",
226                                     fname, lineno, txt );
227 #endif
228
229                                 return 1;
230                         }
231 #endif /* HAVE_CYRUS_SASL */
232             }
233
234             return LDAP_SUCCESS;
235 }
236
237 #ifdef HAVE_CYRUS_SASL
238
239 int
240 slap_sasl_log(
241         void *context,
242         int priority,
243         const char *message) 
244 {
245         Connection *conn = context;
246         int level;
247         const char * label;
248
249         if ( message == NULL ) {
250                 return SASL_BADPARAM;
251         }
252
253         switch (priority) {
254 #if SASL_VERSION_MAJOR >= 2
255         case SASL_LOG_NONE:
256                 level = LDAP_DEBUG_NONE;
257                 label = "None";
258                 break;
259         case SASL_LOG_ERR:
260                 level = LDAP_DEBUG_ANY;
261                 label = "Error";
262                 break;
263         case SASL_LOG_FAIL:
264                 level = LDAP_DEBUG_ANY;
265                 label = "Failure";
266                 break;
267         case SASL_LOG_WARN:
268                 level = LDAP_DEBUG_TRACE;
269                 label = "Warning";
270                 break;
271         case SASL_LOG_NOTE:
272                 level = LDAP_DEBUG_TRACE;
273                 label = "Notice";
274                 break;
275         case SASL_LOG_DEBUG:
276                 level = LDAP_DEBUG_TRACE;
277                 label = "Debug";
278                 break;
279         case SASL_LOG_TRACE:
280                 level = LDAP_DEBUG_TRACE;
281                 label = "Trace";
282                 break;
283         case SASL_LOG_PASS:
284                 level = LDAP_DEBUG_TRACE;
285                 label = "Password Trace";
286                 break;
287 #else
288         case SASL_LOG_ERR:
289                 level = LDAP_DEBUG_ANY;
290                 label = "Error";
291                 break;
292         case SASL_LOG_WARNING:
293                 level = LDAP_DEBUG_TRACE;
294                 label = "Warning";
295                 break;
296         case SASL_LOG_INFO:
297                 level = LDAP_DEBUG_TRACE;
298                 label = "Info";
299                 break;
300 #endif
301         default:
302                 return SASL_BADPARAM;
303         }
304
305 #ifdef NEW_LOGGING
306         LDAP_LOG( TRANSPORT, ENTRY, 
307                 "SASL [conn=%ld] %s: %s\n", conn ? conn->c_connid : -1, label, message);
308 #else
309         Debug( level, "SASL [conn=%ld] %s: %s\n",
310                 conn ? conn->c_connid: -1,
311                 label, message );
312 #endif
313
314
315         return SASL_OK;
316 }
317
318
319 #if SASL_VERSION_MAJOR >= 2
320 static const char *slap_propnames[] = {
321         "*slapConn", "*authcDN", "*authzDN", NULL };
322
323 static Filter generic_filter = { LDAP_FILTER_PRESENT };
324 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
325
326 #define PROP_CONN       0
327 #define PROP_AUTHC      1
328 #define PROP_AUTHZ      2
329
330 typedef struct lookup_info {
331         int last;
332         int flags;
333         const struct propval *list;
334         sasl_server_params_t *sparams;
335 } lookup_info;
336
337 static slap_response sasl_ap_lookup;
338
339 static int
340 sasl_ap_lookup( Operation *op, SlapReply *rs )
341 {
342         BerVarray bv;
343         AttributeDescription *ad;
344         Attribute *a;
345         const char *text;
346         int rc, i;
347         slap_callback *tmp = op->o_callback;
348         lookup_info *sl = tmp->sc_private;
349
350         if (rs->sr_type != REP_SEARCH) return 0;
351
352         for( i = 0; i < sl->last; i++ ) {
353                 const char *name = sl->list[i].name;
354
355                 if ( name[0] == '*' ) {
356                         if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
357                         name++;
358                 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
359                         continue;
360
361                 if ( sl->list[i].values ) {
362                         if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
363                 }
364                 ad = NULL;
365                 rc = slap_str2ad( name, &ad, &text );
366                 if ( rc != LDAP_SUCCESS ) {
367 #ifdef NEW_LOGGING
368                         LDAP_LOG( TRANSPORT, DETAIL1, 
369                                 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
370 #else
371                         Debug( LDAP_DEBUG_TRACE,
372                                 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
373 #endif
374                         continue;
375                 }
376                 a = attr_find( rs->sr_entry->e_attrs, ad );
377                 if ( !a ) continue;
378                 if ( ! access_allowed( op, rs->sr_entry, ad, NULL, ACL_AUTH, NULL ) ) {
379                         continue;
380                 }
381                 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
382                         sl->sparams->utils->prop_erase( sl->sparams->propctx,
383                         sl->list[i].name );
384                 }
385                 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
386                         sl->sparams->utils->prop_set( sl->sparams->propctx,
387                                 sl->list[i].name, bv->bv_val, bv->bv_len );
388                 }
389         }
390         return LDAP_SUCCESS;
391 }
392
393 static void
394 slap_auxprop_lookup(
395         void *glob_context,
396         sasl_server_params_t *sparams,
397         unsigned flags,
398         const char *user,
399         unsigned ulen)
400 {
401         Operation op = {0};
402         int i, doit = 0;
403         Connection *conn = NULL;
404         lookup_info sl;
405
406         sl.list = sparams->utils->prop_get( sparams->propctx );
407         sl.sparams = sparams;
408         sl.flags = flags;
409
410         /* Find our DN and conn first */
411         for( i = 0, sl.last = 0; sl.list[i].name; i++ ) {
412                 if ( sl.list[i].name[0] == '*' ) {
413                         if ( !strcmp( sl.list[i].name, slap_propnames[PROP_CONN] ) ) {
414                                 if ( sl.list[i].values && sl.list[i].values[0] )
415                                         AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
416                                 if ( !sl.last ) sl.last = i;
417                         }
418                         if ( (flags & SASL_AUXPROP_AUTHZID) &&
419                                 !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHZ] ) ) {
420
421                                 if ( sl.list[i].values && sl.list[i].values[0] )
422                                         AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
423                                 if ( !sl.last ) sl.last = i;
424                                 break;
425                         }
426                         if ( !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHC] ) ) {
427                                 if ( !sl.last ) sl.last = i;
428                                 if ( sl.list[i].values && sl.list[i].values[0] ) {
429                                         AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
430                                         if ( !(flags & SASL_AUXPROP_AUTHZID) )
431                                                 break;
432                                 }
433                         }
434                 }
435         }
436
437         /* Now see what else needs to be fetched */
438         for( i = 0; i < sl.last; i++ ) {
439                 const char *name = sl.list[i].name;
440
441                 if ( name[0] == '*' ) {
442                         if ( flags & SASL_AUXPROP_AUTHZID ) continue;
443                         name++;
444                 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
445                         continue;
446
447                 if ( sl.list[i].values ) {
448                         if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
449                 }
450                 doit = 1;
451         }
452
453         if (doit) {
454                 slap_callback cb = { NULL, sasl_ap_lookup, NULL, NULL };
455
456                 cb.sc_private = &sl;
457
458                 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
459
460                 if ( op.o_bd && op.o_bd->be_search ) {
461                         SlapReply rs = {REP_RESULT};
462                         op.o_tag = LDAP_REQ_SEARCH;
463                         op.o_protocol = LDAP_VERSION3;
464                         op.o_ndn = conn->c_ndn;
465                         op.o_callback = &cb;
466                         op.o_time = slap_get_time();
467                         op.o_do_not_cache = 1;
468                         op.o_is_auth_check = 1;
469                         op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
470                         op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
471                         op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
472                         op.o_conn = conn;
473                         op.o_connid = conn->c_connid;
474                         op.o_req_dn = op.o_req_ndn;
475                         op.ors_scope = LDAP_SCOPE_BASE;
476                         op.ors_deref = LDAP_DEREF_NEVER;
477                         op.ors_slimit = 1;
478                         op.ors_filter = &generic_filter;
479                         op.ors_filterstr = generic_filterstr;
480
481                         op.o_bd->be_search( &op, &rs );
482                 }
483         }
484 }
485
486 #if SASL_VERSION_FULL >= 0x020110
487 static int
488 slap_auxprop_store(
489         void *glob_context,
490         sasl_server_params_t *sparams,
491         struct propctx *prctx,
492         const char *user,
493         unsigned ulen)
494 {
495         Operation op = {0};
496         SlapReply rs = {REP_RESULT};
497         int rc, i, j;
498         Connection *conn = NULL;
499         const struct propval *pr;
500         Modifications *modlist = NULL, **modtail = &modlist, *mod;
501         slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
502         char textbuf[SLAP_TEXT_BUFLEN];
503         const char *text;
504         size_t textlen = sizeof(textbuf);
505
506         /* just checking if we are enabled */
507         if (!prctx) return SASL_OK;
508
509         if (!sparams || !user) return SASL_BADPARAM;
510
511         pr = sparams->utils->prop_get( sparams->propctx );
512
513         /* Find our DN and conn first */
514         for( i = 0; pr[i].name; i++ ) {
515                 if ( pr[i].name[0] == '*' ) {
516                         if ( !strcmp( pr[i].name, slap_propnames[PROP_CONN] ) ) {
517                                 if ( pr[i].values && pr[i].values[0] )
518                                         AC_MEMCPY( &conn, pr[i].values[0], sizeof( conn ) );
519                                 continue;
520                         }
521                         if ( !strcmp( pr[i].name, slap_propnames[PROP_AUTHC] ) ) {
522                                 if ( pr[i].values && pr[i].values[0] ) {
523                                         AC_MEMCPY( &op.o_req_ndn, pr[i].values[0], sizeof( struct berval ) );
524                                 }
525                         }
526                 }
527         }
528         if (!conn || !op.o_req_ndn.bv_val) return SASL_BADPARAM;
529
530         op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
531
532         if ( !op.o_bd || !op.o_bd->be_modify ) return SASL_FAIL;
533                 
534         pr = sparams->utils->prop_get( prctx );
535         if (!pr) return SASL_BADPARAM;
536
537         for (i=0; pr[i].name; i++);
538         if (!i) return SASL_BADPARAM;
539
540         for (i=0; pr[i].name; i++) {
541                 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
542                 mod->sml_op = LDAP_MOD_REPLACE;
543                 ber_str2bv( pr[i].name, 0, 0, &mod->sml_type );
544                 mod->sml_values = (struct berval *)ch_malloc( (pr[i].nvalues + 1) *
545                         sizeof(struct berval));
546                 for (j=0; j<pr[i].nvalues; j++) {
547                         ber_str2bv( pr[i].values[j], 0, 1, &mod->sml_values[j]);
548                 }
549                 mod->sml_values[j].bv_val = NULL;
550                 mod->sml_values[j].bv_len = 0;
551                 mod->sml_nvalues = NULL;
552                 mod->sml_desc = NULL;
553                 *modtail = mod;
554                 modtail = &mod->sml_next;
555         }
556         *modtail = NULL;
557
558         rc = slap_mods_check( modlist, 0, &text, textbuf, textlen, NULL );
559
560         if ( rc == LDAP_SUCCESS ) {
561                 rc = slap_mods_opattrs( &op, modlist, modtail, &text, textbuf,
562                         textlen );
563         }
564
565         if ( rc == LDAP_SUCCESS ) {
566                 op.o_tag = LDAP_REQ_MODIFY;
567                 op.o_protocol = LDAP_VERSION3;
568                 op.o_ndn = op.o_req_ndn;
569                 op.o_callback = &cb;
570                 op.o_time = slap_get_time();
571                 op.o_do_not_cache = 1;
572                 op.o_is_auth_check = 1;
573                 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
574                 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
575                 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
576                 op.o_conn = conn;
577                 op.o_connid = conn->c_connid;
578                 op.o_req_dn = op.o_req_ndn;
579                 op.orm_modlist = modlist;
580
581                 rc = op.o_bd->be_modify( &op, &rs );
582         }
583         slap_mods_free( modlist );
584         return rc ? SASL_FAIL : SASL_OK;
585 }
586 #endif /* SASL_VERSION_FULL >= 2.1.16 */
587
588 static sasl_auxprop_plug_t slap_auxprop_plugin = {
589         0,      /* Features */
590         0,      /* spare */
591         NULL,   /* glob_context */
592         NULL,   /* auxprop_free */
593         slap_auxprop_lookup,
594         "slapd",        /* name */
595 #if SASL_VERSION_FULL >= 0x020110
596         slap_auxprop_store      /* the declaration of this member changed
597                                  * in cyrus SASL from 2.1.15 to 2.1.16 */
598 #else
599         NULL
600 #endif
601 };
602
603 static int
604 slap_auxprop_init(
605         const sasl_utils_t *utils,
606         int max_version,
607         int *out_version,
608         sasl_auxprop_plug_t **plug,
609         const char *plugname)
610 {
611         if ( !out_version || !plug ) return SASL_BADPARAM;
612
613         if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
614
615         *out_version = SASL_AUXPROP_PLUG_VERSION;
616         *plug = &slap_auxprop_plugin;
617         return SASL_OK;
618 }
619
620 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
621  * auxiliary property, so that we can refer to it in sasl_authorize
622  * without interfering with anything else. Also, the SASL username
623  * buffer is constrained to 256 characters, and our DNs could be
624  * much longer (SLAP_LDAPDN_MAXLEN, currently set to 8192)
625  */
626 static int
627 slap_sasl_canonicalize(
628         sasl_conn_t *sconn,
629         void *context,
630         const char *in,
631         unsigned inlen,
632         unsigned flags,
633         const char *user_realm,
634         char *out,
635         unsigned out_max,
636         unsigned *out_len)
637 {
638         Connection *conn = (Connection *)context;
639         struct propctx *props = sasl_auxprop_getctx( sconn );
640         struct propval auxvals[3];
641         struct berval dn;
642         int rc, which;
643         const char *names[2];
644
645         *out_len = 0;
646
647 #ifdef NEW_LOGGING
648         LDAP_LOG( TRANSPORT, ENTRY, 
649                 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
650                 conn ? conn->c_connid : -1,
651                 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
652                 in ? in : "<empty>");
653 #else
654         Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
655                 conn ? conn->c_connid : -1,
656                 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
657                 in ? in : "<empty>");
658 #endif
659
660         /* If name is too big, just truncate. We don't care, we're
661          * using DNs, not the usernames.
662          */
663         if ( inlen > out_max )
664                 inlen = out_max-1;
665
666         /* This is a Simple Bind using SPASSWD. That means the in-directory
667          * userPassword of the Binding user already points at SASL, so it
668          * cannot be used to actually satisfy a password comparison. Just
669          * ignore it, some other mech will process it.
670          */
671         if ( !conn->c_sasl_bindop ||
672                 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) goto done;
673
674         /* See if we need to add request, can only do it once */
675         prop_getnames( props, slap_propnames, auxvals );
676         if ( !auxvals[0].name )
677                 prop_request( props, slap_propnames );
678
679         if ( flags & SASL_CU_AUTHID )
680                 which = PROP_AUTHC;
681         else
682                 which = PROP_AUTHZ;
683
684         /* Need to store the Connection for auxprop_lookup */
685         if ( !auxvals[PROP_CONN].values ) {
686                 names[0] = slap_propnames[PROP_CONN];
687                 names[1] = NULL;
688                 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
689         }
690                 
691         /* Already been here? */
692         if ( auxvals[which].values )
693                 goto done;
694
695         /* Normally we require an authzID to have a u: or dn: prefix.
696          * However, SASL frequently gives us an authzID that is just
697          * an exact copy of the authcID, without a prefix. We need to
698          * detect and allow this condition. If SASL calls canonicalize
699          * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
700          * But if it's broken into two calls, we need to remember the
701          * authcID so that we can compare the authzID later. We store
702          * the authcID temporarily in conn->c_sasl_dn. We necessarily
703          * finish Canonicalizing before Authorizing, so there is no
704          * conflict with slap_sasl_authorize's use of this temp var.
705          *
706          * The SASL EXTERNAL mech is backwards from all the other mechs,
707          * it does authzID before the authcID. If we see that authzID
708          * has already been done, don't do anything special with authcID.
709          */
710         if ( flags == SASL_CU_AUTHID && !auxvals[PROP_AUTHZ].values ) {
711                 conn->c_sasl_dn.bv_val = (char *) in;
712         } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
713                 rc = strcmp( in, conn->c_sasl_dn.bv_val );
714                 conn->c_sasl_dn.bv_val = NULL;
715                 /* They were equal, no work needed */
716                 if ( !rc ) goto done;
717         }
718
719         rc = slap_sasl_getdn( conn, NULL, (char *)in, inlen, (char *)user_realm, &dn,
720                 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
721         if ( rc != LDAP_SUCCESS ) {
722                 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
723                 return SASL_NOAUTHZ;
724         }               
725
726         names[0] = slap_propnames[which];
727         names[1] = NULL;
728
729         prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
730                 
731 #ifdef NEW_LOGGING
732         LDAP_LOG( TRANSPORT, ENTRY, 
733                 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
734                 conn ? conn->c_connid : -1, names[0]+1,
735                 dn.bv_val ? dn.bv_val : "<EMPTY>" );
736 #else
737         Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
738                 conn ? conn->c_connid : -1, names[0]+1,
739                 dn.bv_val ? dn.bv_val : "<EMPTY>" );
740 #endif
741
742 done:
743         AC_MEMCPY( out, in, inlen );
744         out[inlen] = '\0';
745
746         *out_len = inlen;
747
748         return SASL_OK;
749 }
750
751 static int
752 slap_sasl_authorize(
753         sasl_conn_t *sconn,
754         void *context,
755         char *requested_user,
756         unsigned rlen,
757         char *auth_identity,
758         unsigned alen,
759         const char *def_realm,
760         unsigned urlen,
761         struct propctx *props)
762 {
763         Connection *conn = (Connection *)context;
764         struct propval auxvals[3];
765         struct berval authcDN, authzDN;
766         int rc;
767
768         /* Simple Binds don't support proxy authorization, ignore it */
769         if ( !conn->c_sasl_bindop ||
770                 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) return SASL_OK;
771
772 #ifdef NEW_LOGGING
773         LDAP_LOG( TRANSPORT, ENTRY, 
774                 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
775                 conn ? conn->c_connid : -1, auth_identity, requested_user);
776 #else
777         Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
778                 "authcid=\"%s\" authzid=\"%s\"\n",
779                 conn ? conn->c_connid : -1, auth_identity, requested_user );
780 #endif
781         if ( conn->c_sasl_dn.bv_val ) {
782                 ch_free( conn->c_sasl_dn.bv_val );
783                 conn->c_sasl_dn.bv_val = NULL;
784                 conn->c_sasl_dn.bv_len = 0;
785         }
786
787         /* Skip PROP_CONN */
788         prop_getnames( props, slap_propnames+1, auxvals );
789         
790         /* Should not happen */
791         if ( !auxvals[0].values ) {
792                 sasl_seterror( sconn, 0, "invalid authcid" );
793                 return SASL_NOAUTHZ;
794         }
795
796         AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
797
798         /* Nothing to do if no authzID was given */
799         if ( !auxvals[1].name || !auxvals[1].values ) {
800                 conn->c_sasl_dn = authcDN;
801                 goto ok;
802         }
803         
804         AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
805
806         rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
807         ch_free( authcDN.bv_val );
808         if ( rc != LDAP_SUCCESS ) {
809 #ifdef NEW_LOGGING
810                 LDAP_LOG( TRANSPORT, INFO, 
811                         "slap_sasl_authorize: conn %ld "
812                         "proxy authorization disallowed (%d)\n",
813                         (long)(conn ? conn->c_connid : -1), rc, 0 );
814 #else
815                 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
816                         "proxy authorization disallowed (%d)\n",
817                         (long) (conn ? conn->c_connid : -1), rc, 0 );
818 #endif
819
820                 sasl_seterror( sconn, 0, "not authorized" );
821                 ch_free( authzDN.bv_val );
822                 return SASL_NOAUTHZ;
823         }
824
825         conn->c_sasl_dn = authzDN;
826 ok:
827         if (conn->c_sasl_bindop) {
828                 Statslog( LDAP_DEBUG_STATS,
829                         "conn=%lu op=%lu BIND authcid=\"%s\"\n",
830                         conn->c_connid, conn->c_sasl_bindop->o_opid, 
831                         auth_identity, 0, 0);
832         }
833
834 #ifdef NEW_LOGGING
835         LDAP_LOG( TRANSPORT, ENTRY, 
836                 "slap_sasl_authorize: conn %d proxy authorization allowed\n",
837                 (long)(conn ? conn->c_connid : -1), 0, 0 );
838 #else
839         Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
840                 " proxy authorization allowed\n",
841                 (long) (conn ? conn->c_connid : -1), 0, 0 );
842 #endif
843         return SASL_OK;
844
845 #else
846 static int
847 slap_sasl_authorize(
848         void *context,
849         char *authcid,
850         char *authzid,
851         const char **user,
852         const char **errstr)
853 {
854         struct berval authcDN, authzDN;
855         int rc;
856         Connection *conn = context;
857         char *realm;
858
859         *user = NULL;
860         if ( conn->c_sasl_dn.bv_val ) {
861                 ch_free( conn->c_sasl_dn.bv_val );
862                 conn->c_sasl_dn.bv_val = NULL;
863                 conn->c_sasl_dn.bv_len = 0;
864         }
865
866 #ifdef NEW_LOGGING
867         LDAP_LOG( TRANSPORT, ENTRY, 
868                 "slap_sasl_authorize: conn %d    authcid=\"%s\" authzid=\"%s\"\n",
869                 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
870                 authzid ? authzid : "<empty>" );
871 #else
872         Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
873                 "authcid=\"%s\" authzid=\"%s\"\n",
874                 (long) (conn ? conn->c_connid : -1),
875                 authcid ? authcid : "<empty>",
876                 authzid ? authzid : "<empty>" );
877 #endif
878
879         /* Figure out how much data we have for the dn */
880         rc = sasl_getprop( conn->c_sasl_authctx, SASL_REALM, (void **)&realm );
881         if( rc != SASL_OK && rc != SASL_NOTDONE ) {
882 #ifdef NEW_LOGGING
883                 LDAP_LOG( TRANSPORT, ERR,
884                         "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
885 #else
886                 Debug(LDAP_DEBUG_TRACE,
887                         "authorize: getprop(REALM) failed!\n", 0,0,0);
888 #endif
889                 *errstr = "Could not extract realm";
890                 return SASL_NOAUTHZ;
891         }
892
893         /* Convert the identities to DN's. If no authzid was given, client will
894            be bound as the DN matching their username */
895         rc = slap_sasl_getdn( conn, NULL, (char *)authcid, 0, realm,
896                 &authcDN, SLAP_GETDN_AUTHCID );
897         if( rc != LDAP_SUCCESS ) {
898                 *errstr = ldap_err2string( rc );
899                 return SASL_NOAUTHZ;
900         }
901         if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
902 #ifdef NEW_LOGGING
903                 LDAP_LOG( TRANSPORT, ENTRY, 
904                         "slap_sasl_authorize: conn %d  Using authcDN=%s\n",
905                         conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
906 #else
907                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
908                  "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
909 #endif
910
911                 conn->c_sasl_dn = authcDN;
912                 goto ok;
913         }
914         rc = slap_sasl_getdn( conn, NULL, (char *)authzid, 0, realm,
915                 &authzDN, SLAP_GETDN_AUTHZID );
916         if( rc != LDAP_SUCCESS ) {
917                 ch_free( authcDN.bv_val );
918                 *errstr = ldap_err2string( rc );
919                 return SASL_NOAUTHZ;
920         }
921
922         rc = slap_sasl_authorized(conn->c_sasl_bindop, &authcDN, &authzDN );
923         ch_free( authcDN.bv_val );
924         if( rc ) {
925 #ifdef NEW_LOGGING
926                 LDAP_LOG( TRANSPORT, INFO, 
927                         "slap_sasl_authorize: conn %ld "
928                         "proxy authorization disallowed (%d)\n",
929                         (long)(conn ? conn->c_connid : -1), rc, 0 );
930 #else
931                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
932                         "proxy authorization disallowed (%d)\n",
933                         (long) (conn ? conn->c_connid : -1), rc, 0 );
934 #endif
935
936                 *errstr = "not authorized";
937                 ch_free( authzDN.bv_val );
938                 return SASL_NOAUTHZ;
939         }
940         conn->c_sasl_dn = authzDN;
941
942 ok:
943 #ifdef NEW_LOGGING
944         LDAP_LOG( TRANSPORT, RESULTS, 
945                 "slap_sasl_authorize: conn %d proxy authorization allowed\n",
946            (long)(conn ? conn->c_connid : -1 ), 0, 0 );
947 #else
948         Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
949                 " authorization allowed\n",
950                 (long) (conn ? conn->c_connid : -1), 0, 0 );
951 #endif
952
953         if (conn->c_sasl_bindop) {
954                 Statslog( LDAP_DEBUG_STATS,
955                         "conn=%lu op=%lu BIND authcid=\"%s\"\n",
956                         conn->c_connid, conn->c_sasl_bindop->o_opid, 
957                         authcid, 0, 0);
958         }
959
960         *errstr = NULL;
961         return SASL_OK;
962 }
963 #endif /* SASL_VERSION_MAJOR >= 2 */
964
965 static int
966 slap_sasl_err2ldap( int saslerr )
967 {
968         int rc;
969
970         switch (saslerr) {
971                 case SASL_OK:
972                         rc = LDAP_SUCCESS;
973                         break;
974                 case SASL_CONTINUE:
975                         rc = LDAP_SASL_BIND_IN_PROGRESS;
976                         break;
977                 case SASL_FAIL:
978                         rc = LDAP_OTHER;
979                         break;
980                 case SASL_NOMEM:
981                         rc = LDAP_OTHER;
982                         break;
983                 case SASL_NOMECH:
984                         rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
985                         break;
986                 case SASL_BADAUTH:
987                         rc = LDAP_INVALID_CREDENTIALS;
988                         break;
989                 case SASL_NOAUTHZ:
990                         rc = LDAP_INSUFFICIENT_ACCESS;
991                         break;
992                 case SASL_TOOWEAK:
993                 case SASL_ENCRYPT:
994                         rc = LDAP_INAPPROPRIATE_AUTH;
995                         break;
996                 default:
997                         rc = LDAP_OTHER;
998                         break;
999         }
1000
1001         return rc;
1002 }
1003 #endif
1004
1005 int slap_sasl_init( void )
1006 {
1007 #ifdef HAVE_CYRUS_SASL
1008         int rc;
1009         static sasl_callback_t server_callbacks[] = {
1010                 { SASL_CB_LOG, &slap_sasl_log, NULL },
1011                 { SASL_CB_LIST_END, NULL, NULL }
1012         };
1013
1014 #ifdef HAVE_SASL_VERSION
1015         /* stringify the version number, sasl.h doesn't do it for us */
1016 #define VSTR0(maj, min, pat)    #maj "." #min "." #pat
1017 #define VSTR(maj, min, pat)     VSTR0(maj, min, pat)
1018 #define SASL_VERSION_STRING     VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
1019                                 SASL_VERSION_STEP)
1020
1021         sasl_version( NULL, &rc );
1022         if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
1023                 (rc & 0xffff) < SASL_VERSION_STEP)
1024         {
1025                 char version[sizeof("xxx.xxx.xxxxx")];
1026                 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
1027                         rc & 0xffff );
1028 #ifdef NEW_LOGGING
1029                 LDAP_LOG( TRANSPORT, INFO,
1030                 "slap_sasl_init: SASL library version mismatch:"
1031                 " expected " SASL_VERSION_STRING ","
1032                 " got %s\n", version, 0, 0 );
1033 #else
1034                 Debug( LDAP_DEBUG_ANY,
1035                 "slap_sasl_init: SASL library version mismatch:"
1036                 " expected " SASL_VERSION_STRING ","
1037                 " got %s\n", version, 0, 0 );
1038 #endif
1039                 return -1;
1040         }
1041 #endif
1042
1043         /* SASL 2 does its own memory management internally */
1044 #if SASL_VERSION_MAJOR < 2
1045         sasl_set_alloc(
1046                 ber_memalloc,
1047                 ber_memcalloc,
1048                 ber_memrealloc,
1049                 ber_memfree ); 
1050 #endif
1051
1052         sasl_set_mutex(
1053                 ldap_pvt_sasl_mutex_new,
1054                 ldap_pvt_sasl_mutex_lock,
1055                 ldap_pvt_sasl_mutex_unlock,
1056                 ldap_pvt_sasl_mutex_dispose );
1057
1058 #if SASL_VERSION_MAJOR >= 2
1059         generic_filter.f_desc = slap_schema.si_ad_objectClass;
1060
1061         sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1062 #endif
1063         /* should provide callbacks for logging */
1064         /* server name should be configurable */
1065         rc = sasl_server_init( server_callbacks, "slapd" );
1066
1067         if( rc != SASL_OK ) {
1068 #ifdef NEW_LOGGING
1069                 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
1070 #else
1071                 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1072                         0, 0, 0 );
1073 #endif
1074 #if SASL_VERSION_MAJOR < 2
1075                 /* A no-op used to make sure we linked with Cyrus 1.5 */
1076                 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1077 #endif
1078
1079                 return -1;
1080         }
1081
1082 #ifdef NEW_LOGGING
1083         LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1084 #else
1085         Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1086                 0, 0, 0 );
1087 #endif
1088
1089         /* default security properties */
1090         memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1091         sasl_secprops.max_ssf = INT_MAX;
1092         sasl_secprops.maxbufsize = 65536;
1093         sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1094 #endif
1095
1096         return 0;
1097 }
1098
1099 int slap_sasl_destroy( void )
1100 {
1101 #ifdef HAVE_CYRUS_SASL
1102         sasl_done();
1103 #endif
1104         free( global_host );
1105         global_host = NULL;
1106
1107         return 0;
1108 }
1109
1110 int slap_sasl_open( Connection *conn, int reopen )
1111 {
1112         int sc = LDAP_SUCCESS;
1113 #ifdef HAVE_CYRUS_SASL
1114         int cb;
1115
1116         sasl_conn_t *ctx = NULL;
1117         sasl_callback_t *session_callbacks;
1118
1119 #if SASL_VERSION_MAJOR >= 2
1120         char *ipremoteport = NULL, *iplocalport = NULL;
1121 #endif
1122
1123         assert( conn->c_sasl_authctx == NULL );
1124
1125         if ( !reopen ) {
1126                 assert( conn->c_sasl_extra == NULL );
1127
1128                 session_callbacks =
1129 #if SASL_VERSION_MAJOR >= 2
1130                         SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1131 #else
1132                         SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1133 #endif
1134                 if( session_callbacks == NULL ) {
1135 #ifdef NEW_LOGGING
1136                         LDAP_LOG( TRANSPORT, ERR, 
1137                                 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1138 #else
1139                         Debug( LDAP_DEBUG_ANY, 
1140                                 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1141 #endif
1142                         return -1;
1143                 }
1144                 conn->c_sasl_extra = session_callbacks;
1145
1146                 session_callbacks[cb=0].id = SASL_CB_LOG;
1147                 session_callbacks[cb].proc = &slap_sasl_log;
1148                 session_callbacks[cb++].context = conn;
1149
1150                 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1151                 session_callbacks[cb].proc = &slap_sasl_authorize;
1152                 session_callbacks[cb++].context = conn;
1153
1154 #if SASL_VERSION_MAJOR >= 2
1155                 session_callbacks[cb].id = SASL_CB_CANON_USER;
1156                 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1157                 session_callbacks[cb++].context = conn;
1158 #endif
1159
1160                 session_callbacks[cb].id = SASL_CB_LIST_END;
1161                 session_callbacks[cb].proc = NULL;
1162                 session_callbacks[cb++].context = NULL;
1163         } else {
1164                 session_callbacks = conn->c_sasl_extra;
1165         }
1166
1167         conn->c_sasl_layers = 0;
1168
1169         if( global_host == NULL ) {
1170                 global_host = ldap_pvt_get_fqdn( NULL );
1171         }
1172
1173         /* create new SASL context */
1174 #if SASL_VERSION_MAJOR >= 2
1175         if ( conn->c_sock_name.bv_len != 0 &&
1176              strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1177                 char *p;
1178
1179                 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1180                 /* Convert IPv6 addresses to address;port syntax. */
1181                 p = strrchr( iplocalport, ' ' );
1182                 /* Convert IPv4 addresses to address;port syntax. */
1183                 if ( p == NULL ) p = strchr( iplocalport, ':' );
1184                 if ( p != NULL ) {
1185                         *p = ';';
1186                 }
1187         }
1188         if ( conn->c_peer_name.bv_len != 0 &&
1189              strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1190                 char *p;
1191
1192                 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1193                 /* Convert IPv6 addresses to address;port syntax. */
1194                 p = strrchr( ipremoteport, ' ' );
1195                 /* Convert IPv4 addresses to address;port syntax. */
1196                 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1197                 if ( p != NULL ) {
1198                         *p = ';';
1199                 }
1200         }
1201         sc = sasl_server_new( "ldap", global_host, global_realm,
1202                 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1203         if ( iplocalport != NULL ) {
1204                 ch_free( iplocalport );
1205         }
1206         if ( ipremoteport != NULL ) {
1207                 ch_free( ipremoteport );
1208         }
1209 #else
1210         sc = sasl_server_new( "ldap", global_host, global_realm,
1211                 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1212 #endif
1213
1214         if( sc != SASL_OK ) {
1215 #ifdef NEW_LOGGING
1216                 LDAP_LOG( TRANSPORT, ERR, 
1217                         "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1218 #else
1219                 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1220                         sc, 0, 0 );
1221 #endif
1222
1223                 return -1;
1224         }
1225
1226         conn->c_sasl_authctx = ctx;
1227
1228         if( sc == SASL_OK ) {
1229                 sc = sasl_setprop( ctx,
1230                         SASL_SEC_PROPS, &sasl_secprops );
1231
1232                 if( sc != SASL_OK ) {
1233 #ifdef NEW_LOGGING
1234                         LDAP_LOG( TRANSPORT, ERR, 
1235                                 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1236 #else
1237                         Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1238                                 sc, 0, 0 );
1239 #endif
1240
1241                         slap_sasl_close( conn );
1242                         return -1;
1243                 }
1244         }
1245
1246         sc = slap_sasl_err2ldap( sc );
1247
1248 #elif defined(SLAP_BUILTIN_SASL)
1249         /* built-in SASL implementation */
1250         SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1251         if( ctx == NULL ) return -1;
1252
1253         ctx->sc_external_ssf = 0;
1254         ctx->sc_external_id.bv_len = 0;
1255         ctx->sc_external_id.bv_val = NULL;
1256
1257         conn->c_sasl_authctx = ctx;
1258 #endif
1259
1260         return sc;
1261 }
1262
1263 int slap_sasl_external(
1264         Connection *conn,
1265         slap_ssf_t ssf,
1266         struct berval *auth_id )
1267 {
1268 #if SASL_VERSION_MAJOR >= 2
1269         int sc;
1270         sasl_conn_t *ctx = conn->c_sasl_authctx;
1271
1272         if ( ctx == NULL ) {
1273                 return LDAP_UNAVAILABLE;
1274         }
1275
1276         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1277
1278         if ( sc != SASL_OK ) {
1279                 return LDAP_OTHER;
1280         }
1281
1282         sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1283                 auth_id ? auth_id->bv_val : NULL );
1284
1285         if ( sc != SASL_OK ) {
1286                 return LDAP_OTHER;
1287         }
1288
1289 #elif defined(HAVE_CYRUS_SASL)
1290         int sc;
1291         sasl_conn_t *ctx = conn->c_sasl_authctx;
1292         sasl_external_properties_t extprops;
1293
1294         if ( ctx == NULL ) {
1295                 return LDAP_UNAVAILABLE;
1296         }
1297
1298         memset( &extprops, '\0', sizeof(extprops) );
1299         extprops.ssf = ssf;
1300         extprops.auth_id = auth_id ? auth_id->bv_val : NULL;
1301
1302         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1303                 (void *) &extprops );
1304
1305         if ( sc != SASL_OK ) {
1306                 return LDAP_OTHER;
1307         }
1308 #elif defined(SLAP_BUILTIN_SASL)
1309         /* built-in SASL implementation */
1310         SASL_CTX *ctx = conn->c_sasl_authctx;
1311         if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1312
1313         ctx->sc_external_ssf = ssf;
1314         if( auth_id ) {
1315                 ctx->sc_external_id = *auth_id;
1316                 auth_id->bv_len = 0;
1317                 auth_id->bv_val = NULL;
1318         } else {
1319                 ctx->sc_external_id.bv_len = 0;
1320                 ctx->sc_external_id.bv_val = NULL;
1321         }
1322 #endif
1323
1324         return LDAP_SUCCESS;
1325 }
1326
1327 int slap_sasl_reset( Connection *conn )
1328 {
1329         return LDAP_SUCCESS;
1330 }
1331
1332 char ** slap_sasl_mechs( Connection *conn )
1333 {
1334         char **mechs = NULL;
1335
1336 #ifdef HAVE_CYRUS_SASL
1337         sasl_conn_t *ctx = conn->c_sasl_authctx;
1338
1339         if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1340
1341         if( ctx != NULL ) {
1342                 int sc;
1343                 SASL_CONST char *mechstr;
1344
1345                 sc = sasl_listmech( ctx,
1346                         NULL, NULL, ",", NULL,
1347                         &mechstr, NULL, NULL );
1348
1349                 if( sc != SASL_OK ) {
1350 #ifdef NEW_LOGGING
1351                         LDAP_LOG( TRANSPORT, ERR, 
1352                                 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1353 #else
1354                         Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1355                                 sc, 0, 0 );
1356 #endif
1357
1358                         return NULL;
1359                 }
1360
1361                 mechs = ldap_str2charray( mechstr, "," );
1362
1363 #if SASL_VERSION_MAJOR < 2
1364                 ch_free( mechstr );
1365 #endif
1366         }
1367 #elif defined(SLAP_BUILTIN_SASL)
1368         /* builtin SASL implementation */
1369         SASL_CTX *ctx = conn->c_sasl_authctx;
1370         if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1371                 /* should check ssf */
1372                 mechs = ldap_str2charray( "EXTERNAL", "," );
1373         }
1374 #endif
1375
1376         return mechs;
1377 }
1378
1379 int slap_sasl_close( Connection *conn )
1380 {
1381 #ifdef HAVE_CYRUS_SASL
1382         sasl_conn_t *ctx = conn->c_sasl_authctx;
1383
1384         if( ctx != NULL ) {
1385                 sasl_dispose( &ctx );
1386         }
1387         if ( conn->c_sasl_sockctx &&
1388                 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1389         {
1390                 ctx = conn->c_sasl_sockctx;
1391                 sasl_dispose( &ctx );
1392         }
1393
1394         conn->c_sasl_authctx = NULL;
1395         conn->c_sasl_sockctx = NULL;
1396         conn->c_sasl_done = 0;
1397
1398         free( conn->c_sasl_extra );
1399         conn->c_sasl_extra = NULL;
1400
1401 #elif defined(SLAP_BUILTIN_SASL)
1402         SASL_CTX *ctx = conn->c_sasl_authctx;
1403         if( ctx ) {
1404                 if( ctx->sc_external_id.bv_val ) {
1405                         free( ctx->sc_external_id.bv_val );
1406                         ctx->sc_external_id.bv_val = NULL;
1407                 }
1408                 free( ctx );
1409                 conn->c_sasl_authctx = NULL;
1410         }
1411 #endif
1412
1413         return LDAP_SUCCESS;
1414 }
1415
1416 int slap_sasl_bind( Operation *op, SlapReply *rs )
1417 {
1418 #ifdef HAVE_CYRUS_SASL
1419         sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1420         struct berval response;
1421         unsigned reslen = 0;
1422         int sc;
1423
1424 #ifdef NEW_LOGGING
1425         LDAP_LOG( TRANSPORT, ENTRY, 
1426                 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1427                 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1428                 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" : 
1429                 op->o_conn->c_sasl_bind_mech.bv_val,
1430                 op->orb_cred.bv_len );
1431 #else
1432         Debug(LDAP_DEBUG_ARGS,
1433                 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1434                 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1435                 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" : 
1436                 op->o_conn->c_sasl_bind_mech.bv_val,
1437                 op->orb_cred.bv_len );
1438 #endif
1439
1440         if( ctx == NULL ) {
1441                 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1442                         "SASL unavailable on this session" );
1443                 return rs->sr_err;
1444         }
1445
1446 #if SASL_VERSION_MAJOR >= 2
1447 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1448         sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1449 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1450         sasl_server_step( ctx, cred, clen, resp, rlen )
1451 #else
1452 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1453         sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1454 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1455         sasl_server_step( ctx, cred, clen, resp, rlen, err )
1456 #endif
1457
1458         if ( !op->o_conn->c_sasl_bind_in_progress ) {
1459                 /* If we already authenticated once, must use a new context */
1460                 if ( op->o_conn->c_sasl_done ) {
1461                         slap_ssf_t ssf = 0;
1462                         const char *authid = NULL;
1463 #if SASL_VERSION_MAJOR >= 2
1464                         sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1465                         sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1466                         if ( authid ) authid = ch_strdup( authid );
1467 #endif
1468                         if ( ctx != op->o_conn->c_sasl_sockctx ) {
1469                                 sasl_dispose( &ctx );
1470                         }
1471                         op->o_conn->c_sasl_authctx = NULL;
1472                                 
1473                         slap_sasl_open( op->o_conn, 1 );
1474                         ctx = op->o_conn->c_sasl_authctx;
1475 #if SASL_VERSION_MAJOR >= 2
1476                         if ( authid ) {
1477                                 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1478                                 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1479                                 ch_free( (char *)authid );
1480                         }
1481 #endif
1482                 }
1483                 sc = START( ctx,
1484                         op->o_conn->c_sasl_bind_mech.bv_val,
1485                         op->orb_cred.bv_val, op->orb_cred.bv_len,
1486                         (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1487
1488         } else {
1489                 sc = STEP( ctx,
1490                         op->orb_cred.bv_val, op->orb_cred.bv_len,
1491                         (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1492         }
1493
1494         response.bv_len = reslen;
1495
1496         if ( sc == SASL_OK ) {
1497                 sasl_ssf_t *ssf = NULL;
1498
1499                 op->orb_edn = op->o_conn->c_sasl_dn;
1500                 op->o_conn->c_sasl_dn.bv_val = NULL;
1501                 op->o_conn->c_sasl_dn.bv_len = 0;
1502                 op->o_conn->c_sasl_done = 1;
1503
1504                 rs->sr_err = LDAP_SUCCESS;
1505
1506                 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1507                 op->orb_ssf = ssf ? *ssf : 0;
1508
1509                 ctx = NULL;
1510                 if( op->orb_ssf ) {
1511                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1512                         op->o_conn->c_sasl_layers++;
1513
1514                         /* If there's an old layer, set sockctx to NULL to
1515                          * tell connection_read() to wait for us to finish.
1516                          * Otherwise there is a race condition: we have to
1517                          * send the Bind response using the old security
1518                          * context and then remove it before reading any
1519                          * new messages.
1520                          */
1521                         if ( op->o_conn->c_sasl_sockctx ) {
1522                                 ctx = op->o_conn->c_sasl_sockctx;
1523                                 op->o_conn->c_sasl_sockctx = NULL;
1524                         } else {
1525                                 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1526                         }
1527                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1528                 }
1529
1530                 /* Must send response using old security layer */
1531                 if (response.bv_len) rs->sr_sasldata = &response;
1532                 send_ldap_sasl( op, rs );
1533                 
1534                 /* Now dispose of the old security layer.
1535                  */
1536                 if ( ctx ) {
1537                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1538                         ldap_pvt_sasl_remove( op->o_conn->c_sb );
1539                         op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1540                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1541                         sasl_dispose( &ctx );
1542                 }
1543         } else if ( sc == SASL_CONTINUE ) {
1544                 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1545                 rs->sr_sasldata = &response;
1546                 send_ldap_sasl( op, rs );
1547
1548         } else {
1549 #if SASL_VERSION_MAJOR >= 2
1550                 rs->sr_text = sasl_errdetail( ctx );
1551 #endif
1552                 rs->sr_err = slap_sasl_err2ldap( sc ),
1553                 send_ldap_result( op, rs );
1554         }
1555
1556 #if SASL_VERSION_MAJOR < 2
1557         if( response.bv_len ) {
1558                 ch_free( response.bv_val );
1559         }
1560 #endif
1561
1562 #ifdef NEW_LOGGING
1563         LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0 );
1564 #else
1565         Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1566 #endif
1567
1568 #elif defined(SLAP_BUILTIN_SASL)
1569         /* built-in SASL implementation */
1570         SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1571
1572         if ( ctx == NULL ) {
1573                 send_ldap_error( op, rs, LDAP_OTHER,
1574                         "Internal SASL Error" );
1575
1576         } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1577                 /* EXTERNAL */
1578
1579                 if( op->orb_cred.bv_len ) {
1580                         rs->sr_text = "proxy authorization not support";
1581                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1582                         send_ldap_result( op, rs );
1583
1584                 } else {
1585                         op->orb_edn = ctx->sc_external_id;
1586                         rs->sr_err = LDAP_SUCCESS;
1587                         rs->sr_sasldata = NULL;
1588                         send_ldap_sasl( op, rs );
1589                 }
1590
1591         } else {
1592                 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1593                         "requested SASL mechanism not supported" );
1594         }
1595 #else
1596         send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1597                 "SASL not supported" );
1598 #endif
1599
1600         return rs->sr_err;
1601 }
1602
1603 char* slap_sasl_secprops( const char *in )
1604 {
1605 #ifdef HAVE_CYRUS_SASL
1606         int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1607
1608         return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1609 #else
1610         return "SASL not supported";
1611 #endif
1612 }
1613
1614 #ifdef HAVE_CYRUS_SASL
1615 int
1616 slap_sasl_setpass( Operation *op, SlapReply *rs )
1617 {
1618         struct berval id = { 0, NULL }; /* needs to come from connection */
1619         struct berval new = { 0, NULL };
1620         struct berval old = { 0, NULL };
1621
1622         assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1623
1624         rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1625                 (SASL_CONST void **)&id.bv_val );
1626
1627         if( rs->sr_err != SASL_OK ) {
1628                 rs->sr_text = "unable to retrieve SASL username";
1629                 rs->sr_err = LDAP_OTHER;
1630                 goto done;
1631         }
1632
1633 #ifdef NEW_LOGGING
1634         LDAP_LOG( BACKEND, ENTRY,
1635                 "slap_sasl_setpass: \"%s\"\n",
1636                 id.bv_val ? id.bv_val : "", 0, 0);
1637 #else
1638         Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1639                 id.bv_val ? id.bv_val : "", 0, 0 );
1640 #endif
1641
1642         rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1643                 NULL, &old, &new, &rs->sr_text );
1644
1645         if( rs->sr_err != LDAP_SUCCESS ) {
1646                 goto done;
1647         }
1648
1649         if( new.bv_len == 0 ) {
1650                 slap_passwd_generate(&new);
1651
1652                 if( new.bv_len == 0 ) {
1653                         rs->sr_text = "password generation failed.";
1654                         rs->sr_err = LDAP_OTHER;
1655                         goto done;
1656                 }
1657                 
1658                 rs->sr_rspdata = slap_passwd_return( &new );
1659         }
1660
1661 #if SASL_VERSION_MAJOR < 2
1662         rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx,
1663                 id.bv_val, new.bv_val, new.bv_len, 0, &rs->sr_text );
1664 #else
1665         rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1666                 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1667         if( rs->sr_err != SASL_OK ) {
1668                 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1669         }
1670 #endif
1671         switch(rs->sr_err) {
1672                 case SASL_OK:
1673                         rs->sr_err = LDAP_SUCCESS;
1674                         break;
1675
1676                 case SASL_NOCHANGE:
1677                 case SASL_NOMECH:
1678                 case SASL_DISABLED:
1679                 case SASL_PWLOCK:
1680                 case SASL_FAIL:
1681                 case SASL_BADPARAM:
1682                 default:
1683                         rs->sr_err = LDAP_OTHER;
1684         }
1685
1686 done:
1687         return rs->sr_err;
1688 }
1689 #endif /* HAVE_CYRUS_SASL */
1690
1691 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1692  * string returned in *dn is in its own allocated memory, and must be free'd 
1693  * by the calling process.  -Mark Adamson, Carnegie Mellon
1694  *
1695  * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1696  * on strings passed in directly from SASL.  -Howard Chu, Symas Corp.
1697  */
1698
1699 #define SET_NONE        0
1700 #define SET_DN          1
1701 #define SET_U           2
1702
1703 int slap_sasl_getdn( Connection *conn, Operation *op, char *id, int len,
1704         char *user_realm, struct berval *dn, int flags )
1705 {
1706         int rc, is_dn = SET_NONE, do_norm = 1;
1707         struct berval dn2, *mech;
1708
1709         assert( conn );
1710
1711 #ifdef NEW_LOGGING
1712         LDAP_LOG( TRANSPORT, ENTRY, 
1713                 "slap_sasl_getdn: conn %d id=%s [len=%d]\n",
1714                 conn->c_connid, id ? (*id ? id : "<empty>") : "NULL", len );
1715 #else
1716         Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s [len=%d]\n", 
1717                 id ? ( *id ? id : "<empty>" ) : "NULL", len, 0 );
1718 #endif
1719
1720         if ( !op ) {
1721                 op = conn->c_sasl_bindop;
1722         }
1723
1724         dn->bv_val = NULL;
1725         dn->bv_len = 0;
1726
1727         if ( id ) {
1728                 if ( len == 0 ) len = strlen( id );
1729
1730                 /* Blatantly anonymous ID */
1731                 if ( len == sizeof("anonymous") - 1 &&
1732                         !strcasecmp( id, "anonymous" ) ) {
1733                         return( LDAP_SUCCESS );
1734                 }
1735         } else {
1736                 len = 0;
1737         }
1738
1739         if ( conn->c_sasl_bind_mech.bv_len ) {
1740                 mech = &conn->c_sasl_bind_mech;
1741         } else {
1742                 mech = &conn->c_authmech;
1743         }
1744
1745         /* An authcID needs to be converted to authzID form. Set the
1746          * values directly into *dn; they will be normalized later. (and
1747          * normalizing always makes a new copy.) An ID from a TLS certificate
1748          * is already normalized, so copy it and skip normalization.
1749          */
1750         if( flags & SLAP_GETDN_AUTHCID ) {
1751                 if( bvmatch( mech, &ext_bv )) {
1752                         /* EXTERNAL DNs are already normalized */
1753                         do_norm = 0;
1754                         is_dn = SET_DN;
1755                         ber_str2bv_x( id, len, 1, dn, op->o_tmpmemctx );
1756
1757                 } else {
1758                         /* convert to u:<username> form */
1759                         is_dn = SET_U;
1760                         dn->bv_val = id;
1761                         dn->bv_len = len;
1762                 }
1763         }
1764         if( is_dn == SET_NONE ) {
1765                 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
1766                         is_dn = SET_U;
1767                         dn->bv_val = id+2;
1768                         dn->bv_len = len-2;
1769                 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
1770                         is_dn = SET_DN;
1771                         dn->bv_val = id+3;
1772                         dn->bv_len = len-3;
1773                 }
1774         }
1775
1776         /* No other possibilities from here */
1777         if( is_dn == SET_NONE ) {
1778                 dn->bv_val = NULL;
1779                 dn->bv_len = 0;
1780                 return( LDAP_INAPPROPRIATE_AUTH );
1781         }
1782
1783         /* Username strings */
1784         if( is_dn == SET_U ) {
1785                 char            *p;
1786                 struct berval   realm = { 0, NULL }, c1 = *dn;
1787
1788                 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
1789
1790                 if( user_realm && *user_realm ) {
1791                         realm.bv_val = user_realm;
1792                         realm.bv_len = strlen( user_realm );
1793                         len += realm.bv_len + sizeof(",cn=") - 1;
1794                 }
1795
1796                 if( mech->bv_len ) {
1797                         len += mech->bv_len + sizeof(",cn=")-1;
1798                 }
1799
1800                 /* Build the new dn */
1801                 dn->bv_val = sl_malloc( len+1, op->o_tmpmemctx );
1802                 if( dn->bv_val == NULL ) {
1803 #ifdef NEW_LOGGING
1804                         LDAP_LOG( TRANSPORT, ERR, 
1805                                 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1806 #else
1807                         Debug( LDAP_DEBUG_ANY, 
1808                                 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1809 #endif
1810                         return LDAP_OTHER;
1811                 }
1812                 p = lutil_strcopy( dn->bv_val, "uid=" );
1813                 p = lutil_strncopy( p, c1.bv_val, c1.bv_len );
1814
1815                 if( realm.bv_len ) {
1816                         p = lutil_strcopy( p, ",cn=" );
1817                         p = lutil_strncopy( p, realm.bv_val, realm.bv_len );
1818                 }
1819
1820                 if( mech->bv_len ) {
1821                         p = lutil_strcopy( p, ",cn=" );
1822                         p = lutil_strcopy( p, mech->bv_val );
1823                 }
1824                 p = lutil_strcopy( p, ",cn=auth" );
1825                 dn->bv_len = p - dn->bv_val;
1826
1827 #ifdef NEW_LOGGING
1828                 LDAP_LOG( TRANSPORT, ENTRY, 
1829                         "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val, 0, 0 );
1830 #else
1831                 Debug( LDAP_DEBUG_TRACE, "slap_sasl_getdn: u:id converted to %s\n", dn->bv_val,0,0 );
1832 #endif
1833         } else {
1834                 
1835                 /* Dup the DN in any case, so we don't risk 
1836                  * leaks or dangling pointers later,
1837                  * and the DN value is '\0' terminated */
1838                 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1839                 dn->bv_val = dn2.bv_val;
1840         }
1841
1842         /* All strings are in DN form now. Normalize if needed. */
1843         if ( do_norm ) {
1844                 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1845
1846                 /* User DNs were constructed above and must be freed now */
1847                 sl_free( dn->bv_val, op->o_tmpmemctx );
1848
1849                 if ( rc != LDAP_SUCCESS ) {
1850                         dn->bv_val = NULL;
1851                         dn->bv_len = 0;
1852                         return rc;
1853                 }
1854                 *dn = dn2;
1855         }
1856
1857         /* Run thru regexp */
1858         slap_sasl2dn( op, dn, &dn2, flags );
1859         if( dn2.bv_val ) {
1860                 sl_free( dn->bv_val, op->o_tmpmemctx );
1861                 *dn = dn2;
1862 #ifdef NEW_LOGGING
1863                 LDAP_LOG( TRANSPORT, ENTRY, 
1864                         "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val, 0, 0 );
1865 #else
1866                 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
1867                         dn->bv_val, 0, 0 );
1868 #endif
1869         }
1870
1871         return( LDAP_SUCCESS );
1872 }