]> git.sur5r.net Git - openldap/blob - servers/slapd/sasl.c
Fix previous commit, == instead of !=
[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 #define SASL_CONST const
30 #else
31 #define SASL_CONST
32 #endif
33
34 #include <ldap_pvt.h>
35
36 #ifdef SLAPD_SPASSWD
37 #include <lutil.h>
38 #endif
39
40 /* Flags for telling slap_sasl_getdn() what type of identity is being passed */
41 #define FLAG_GETDN_FINAL   1
42 #define FLAG_GETDN_AUTHCID 2
43 #define FLAG_GETDN_AUTHZID 4
44
45 static sasl_security_properties_t sasl_secprops;
46
47 static int
48 slap_sasl_log(
49         void *context,
50         int priority,
51         const char *message) 
52 {
53         Connection *conn = context;
54         int level;
55         const char * label;
56
57         if ( message == NULL ) {
58                 return SASL_BADPARAM;
59         }
60
61         switch (priority) {
62 #if SASL_VERSION_MAJOR >= 2
63         case SASL_LOG_NONE:
64                 level = LDAP_DEBUG_NONE;
65                 label = "None";
66                 break;
67         case SASL_LOG_ERR:
68                 level = LDAP_DEBUG_ANY;
69                 label = "Error";
70                 break;
71         case SASL_LOG_FAIL:
72                 level = LDAP_DEBUG_ANY;
73                 label = "Failure";
74                 break;
75         case SASL_LOG_WARN:
76                 level = LDAP_DEBUG_TRACE;
77                 label = "Warning";
78                 break;
79         case SASL_LOG_NOTE:
80                 level = LDAP_DEBUG_TRACE;
81                 label = "Notice";
82                 break;
83         case SASL_LOG_DEBUG:
84                 level = LDAP_DEBUG_TRACE;
85                 label = "Debug";
86                 break;
87         case SASL_LOG_TRACE:
88                 level = LDAP_DEBUG_TRACE;
89                 label = "Trace";
90                 break;
91         case SASL_LOG_PASS:
92                 level = LDAP_DEBUG_TRACE;
93                 label = "Password Trace";
94                 break;
95 #else
96         case SASL_LOG_ERR:
97                 level = LDAP_DEBUG_ANY;
98                 label = "Error";
99                 break;
100         case SASL_LOG_WARNING:
101                 level = LDAP_DEBUG_TRACE;
102                 label = "Warning";
103                 break;
104         case SASL_LOG_INFO:
105                 level = LDAP_DEBUG_TRACE;
106                 label = "Info";
107                 break;
108 #endif
109         default:
110                 return SASL_BADPARAM;
111         }
112
113 #ifdef NEW_LOGGING
114         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
115                 "SASL [conn=%ld] %s: %s\n",
116                 conn ? conn->c_connid : -1,
117                 label, message ));
118 #else
119         Debug( level, "SASL [conn=%ld] %s: %s\n",
120                 conn ? conn->c_connid: -1,
121                 label, message );
122 #endif
123
124
125         return SASL_OK;
126 }
127
128
129 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
130    string returned in *dn is in its own allocated memory, and must be free'd 
131    by the calling process.
132    -Mark Adamson, Carnegie Mellon
133 */
134
135 #define SET_DN  1
136 #define SET_U   2
137
138 static struct berval ext_bv = { sizeof("EXTERNAL")-1, "EXTERNAL" };
139
140 int slap_sasl_getdn( Connection *conn, char *id,
141         char *user_realm, struct berval *dn, int flags )
142 {
143         char *c1;
144         int rc, len, is_dn = 0;
145         sasl_conn_t *ctx;
146         struct berval dn2;
147
148 #ifdef NEW_LOGGING
149         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
150                 "slap_sasl_getdn: conn %d id=%s\n",
151                 conn ? conn->c_connid : -1,
152                 id ? (*id ? id : "<empty>") : "NULL" ));
153 #else
154         Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s\n", 
155       id?(*id?id:"<empty>"):"NULL",0,0 );
156 #endif
157
158         dn->bv_val = NULL;
159         dn->bv_len = 0;
160
161         /* Blatantly anonymous ID */
162         if( id &&
163                 ( id[sizeof( "anonymous" )-1] == '\0'
164                         || id[sizeof( "anonymous" )-1] == '@' ) &&
165                 !strncasecmp( id, "anonymous", sizeof( "anonymous" )-1) ) {
166                 return( LDAP_SUCCESS );
167         }
168         ctx = conn->c_sasl_context;
169         len = strlen( id );
170
171         /* An authcID needs to be converted to authzID form */
172         if( flags & FLAG_GETDN_AUTHCID ) {
173                 if( sasl_external_x509dn_convert
174                         && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
175                         && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) 
176                         && id[0] == '/' )
177                 {
178                         /* check SASL external for X.509 style DN and */
179                         /* convert to dn:<dn> form */
180                         dn->bv_val = ldap_dcedn2dn( id );
181                         dn->bv_len = strlen(dn->bv_val);
182                         is_dn = SET_DN;
183
184                 } else {
185                         /* convert to u:<username> form */
186                         ber_str2bv( id, len, 1, dn );
187                         is_dn = SET_U;
188                 }
189         }
190         if( !is_dn ) {
191                 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
192                         is_dn = SET_U;
193                         ber_str2bv( id+2, len-2, 1, dn );
194                 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
195                         is_dn = SET_DN;
196                         ber_str2bv( id+3, len-3, 1, dn );
197                 }
198         }
199
200         /* An authzID must be properly prefixed */
201         if( (flags & FLAG_GETDN_AUTHZID) && !is_dn ) {
202                 free( dn->bv_val );
203                 dn->bv_val = NULL;
204                 dn->bv_len = 0;
205                 return( LDAP_INAPPROPRIATE_AUTH );
206         }
207
208         /* Username strings */
209         if( is_dn == SET_U ) {
210                 char *p;
211                 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
212
213                 if( user_realm && *user_realm ) {
214                         len += strlen( user_realm ) + sizeof(",cn=")-1;
215                 }
216
217                 if( conn->c_sasl_bind_mech.bv_len ) {
218                         len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
219                 }
220
221                 /* Build the new dn */
222                 c1 = dn->bv_val;
223                 dn->bv_val = ch_malloc( len+1 );
224                 p = slap_strcopy( dn->bv_val, "uid=" );
225                 p = slap_strcopy( p, c1 );
226                 ch_free( c1 );
227
228                 if( user_realm && *user_realm ) {
229                         p = slap_strcopy( p, ",cn=" );
230                         p = slap_strcopy( p, user_realm );
231                 }
232                 if( conn->c_sasl_bind_mech.bv_len ) {
233                         p = slap_strcopy( p, ",cn=" );
234                         p = slap_strcopy( p, conn->c_sasl_bind_mech.bv_val );
235                 }
236                 p = slap_strcopy( p, ",cn=auth" );
237                 dn->bv_len = p - dn->bv_val;
238                 is_dn = SET_DN;
239
240 #ifdef NEW_LOGGING
241                 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
242                         "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val ));
243 #else
244                 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
245 #endif
246         }
247
248         /* DN strings that are a cn=auth identity to run through regexp */
249         if( is_dn == SET_DN && ( ( flags & FLAG_GETDN_FINAL ) == 0 ) )
250         {
251                 slap_sasl2dn( dn, &dn2 );
252                 if( dn2.bv_val ) {
253                         ch_free( dn->bv_val );
254                         *dn = dn2;
255 #ifdef NEW_LOGGING
256                         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
257                                 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val ));
258 #else
259                         Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
260                                 dn->bv_val, 0, 0 );
261 #endif
262                 }
263         }
264
265         if( flags & FLAG_GETDN_FINAL ) {
266                 /* omit "dn:" prefix */
267                 is_dn = 0;
268         } else {
269                 rc = dnNormalize2( NULL, dn, &dn2 );
270                 free(dn->bv_val);
271                 if ( rc != LDAP_SUCCESS ) {
272                         *dn = slap_empty_bv;
273                         return rc;
274                 }
275                 *dn = dn2;
276         }
277
278         /* Attach the "dn:" prefix if needed */
279         if ( is_dn == SET_DN ) {
280                 c1 = ch_malloc( dn->bv_len + sizeof("dn:") );
281                 strcpy( c1, "dn:" );
282                 strcpy( c1 + 3, dn->bv_val );
283                 free( dn->bv_val );
284                 dn->bv_val = c1;
285                 dn->bv_len += 3;
286         }
287
288         return( LDAP_SUCCESS );
289 }
290
291 #if SASL_VERSION_MAJOR >= 2
292 static int
293 slap_sasl_checkpass(
294         sasl_conn_t *sconn,
295         void *context,
296         const char *username,
297         const char *pass,
298         unsigned passlen,
299         struct propctx *propctx)
300 {
301         Connection *conn = (Connection *)context;
302         struct berval dn, cred;
303         int rc;
304         BerVarray vals, bv;
305
306         cred.bv_val = (char *)pass;
307         cred.bv_len = passlen;
308
309         /* XXX do we need to check sasldb as well? */
310
311         /* XXX can we do both steps at once? */
312         rc = slap_sasl_getdn( conn, (char *)username, NULL, &dn,
313                 FLAG_GETDN_AUTHCID | FLAG_GETDN_FINAL );
314         if ( rc != LDAP_SUCCESS ) {
315                 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
316                 return SASL_NOUSER;
317         }
318
319         if ( dn.bv_len == 0 ) {
320                 sasl_seterror( sconn, 0,
321                         "No password is associated with the Root DSE" );
322                 if ( dn.bv_val != NULL ) {
323                         ch_free( dn.bv_val );
324                 }
325                 return SASL_NOUSER;
326         }
327
328         rc = backend_attribute( NULL, NULL, NULL, NULL, &dn,
329                 slap_schema.si_ad_userPassword, &vals);
330         if ( rc != LDAP_SUCCESS ) {
331                 ch_free( dn.bv_val );
332                 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
333                 return SASL_NOVERIFY;
334         }
335
336         rc = SASL_NOVERIFY;
337
338         if ( vals != NULL ) {
339                 for ( bv = vals; bv->bv_val != NULL; bv++ ) {
340                         if ( !lutil_passwd( bv, &cred, NULL ) ) {
341                                 rc = SASL_OK;
342                                 break;
343                         }
344                 }
345                 ber_bvarray_free( vals );
346         }
347
348         if ( rc != SASL_OK ) {
349                 sasl_seterror( sconn, 0,
350                         ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
351         }
352
353         ch_free( dn.bv_val );
354
355         return rc;
356 }
357
358 static int
359 slap_sasl_canonicalize(
360         sasl_conn_t *sconn,
361         void *context,
362         const char *in,
363         unsigned inlen,
364         unsigned flags,
365         const char *user_realm,
366         char *out,
367         unsigned out_max,
368         unsigned *out_len)
369 {
370         Connection *conn = (Connection *)context;
371         struct berval dn;
372         int rc;
373
374         *out_len = 0;
375
376 #ifdef NEW_LOGGING
377         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
378                 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
379                         conn ? conn->c_connid : -1,
380                         (flags == SASL_CU_AUTHID) ? "authcid" : "authzid",
381                         in ? in : "<empty>" ));
382 #else
383         Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
384                 "%s=\"%s\"\n",
385                         conn ? conn->c_connid : -1,
386                         (flags == SASL_CU_AUTHID) ? "authcid" : "authzid",
387                         in ? in : "<empty>" );
388 #endif
389
390         if (flags == SASL_CU_AUTHID) {
391                 rc = slap_sasl_getdn( conn, (char *)in, (char *)user_realm,
392                         &dn, FLAG_GETDN_AUTHCID);
393                 if ( rc != LDAP_SUCCESS ) {
394                         sasl_seterror( sconn, 0, ldap_err2string( rc ) );
395                         return SASL_NOAUTHZ;
396                 }               
397
398                 if ( out_max < dn.bv_len ) {
399                         return SASL_BUFOVER;
400                 }
401
402                 AC_MEMCPY( out, dn.bv_val, dn.bv_len );
403                 out[dn.bv_len] = '\0';
404
405                 *out_len = dn.bv_len;
406
407                 ch_free( dn.bv_val );
408
409         } else {
410                 strcpy( out, in );
411
412                 *out_len = strlen( in );
413         }
414
415 #ifdef NEW_LOGGING
416         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
417                 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
418                         conn ? conn->c_connid : -1,
419                         (flags == SASL_CU_AUTHID) ? "authcDN" : "authzDN",
420                         out ));
421 #else
422         Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
423                 "%s=\"%s\"\n",
424                         conn ? conn->c_connid : -1,
425                         (flags == SASL_CU_AUTHID) ? "authcDN" : "authzDN",
426                         out );
427 #endif
428
429         return SASL_OK;
430 }
431
432 static int
433 slap_sasl_authorize(
434         sasl_conn_t *sconn,
435         void *context,
436         const char *requested_user,
437         unsigned rlen,
438         const char *auth_identity,
439         unsigned alen,
440         const char *def_realm,
441         unsigned urlen,
442         struct propctx *propctx)
443 {
444         Connection *conn = (Connection *)context;
445         struct berval authcDN, authzDN;
446         int rc;
447
448         authcDN.bv_val = (char *)auth_identity;
449         authcDN.bv_len = alen;
450
451         authzDN.bv_val = (char *)requested_user;
452         authzDN.bv_len = rlen;
453
454 #ifdef NEW_LOGGING
455         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
456                 "slap_sasl_authorize: conn %d authcDN=\"%s\" authzDN=\"%s\"\n",
457                         conn ? conn->c_connid : -1, authcDN.bv_val, authzDN.bv_val));
458 #else
459         Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
460                 "authcDN=\"%s\" authzDN=\"%s\"\n",
461                 conn ? conn->c_connid : -1, authcDN.bv_val, authzDN.bv_val );
462 #endif
463
464         rc = slap_sasl_authorized( &authcDN, &authzDN );
465         if ( rc != LDAP_SUCCESS ) {
466 #ifdef NEW_LOGGING
467                 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
468                            "slap_sasl_authorize: conn %ld  authorization disallowed (%d)\n",
469                            (long)(conn ? conn->c_connid : -1), rc ));
470 #else
471                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
472                         " authorization disallowed (%d)\n",
473                         (long) (conn ? conn->c_connid : -1), rc, 0 );
474 #endif
475
476                 sasl_seterror( sconn, 0, "not authorized" );
477                 return SASL_NOAUTHZ;
478         }
479
480 #ifdef NEW_LOGGING
481         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
482                    "slap_sasl_authorize: conn %d authorization allowed\n",
483                    (long)(conn ? conn->c_connid : -1 ) ));
484 #else
485         Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
486                 " authorization allowed\n",
487                 (long) (conn ? conn->c_connid : -1), 0, 0 );
488 #endif
489
490         return SASL_OK;
491
492 #else
493 static int
494 slap_sasl_authorize(
495         void *context,
496         const char *authcid,
497         const char *authzid,
498         const char **user,
499         const char **errstr)
500 {
501         struct berval authcDN, authzDN;
502         int rc;
503         Connection *conn = context;
504         char *realm;
505
506         *user = NULL;
507
508 #ifdef NEW_LOGGING
509         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
510                    "slap_sasl_authorize: conn %d         authcid=\"%s\" authzid=\"%s\"\n",
511                    conn ? conn->c_connid : -1,
512                    authcid ? authcid : "<empty>",
513                    authzid ? authzid : "<empty>" ));
514 #else
515         Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
516                 "authcid=\"%s\" authzid=\"%s\"\n",
517                 (long) (conn ? conn->c_connid : -1),
518                 authcid ? authcid : "<empty>",
519                 authzid ? authzid : "<empty>" );
520 #endif
521
522         /* Figure out how much data we have for the dn */
523         rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
524         if( rc != SASL_OK && rc != SASL_NOTDONE ) {
525 #ifdef NEW_LOGGING
526                 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
527                         "slap_sasl_authorize: getprop(REALM) failed.\n" ));
528 #else
529                 Debug(LDAP_DEBUG_TRACE,
530                         "authorize: getprop(REALM) failed!\n", 0,0,0);
531 #endif
532                 *errstr = "Could not extract realm";
533                 return SASL_NOAUTHZ;
534         }
535
536         /* Convert the identities to DN's. If no authzid was given, client will
537            be bound as the DN matching their username */
538         rc = slap_sasl_getdn( conn, (char *)authcid, realm, &authcDN, FLAG_GETDN_AUTHCID );
539         if( rc != LDAP_SUCCESS ) {
540                 *errstr = ldap_err2string( rc );
541                 return SASL_NOAUTHZ;
542         }
543         if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
544 #ifdef NEW_LOGGING
545                 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
546                            "slap_sasl_authorize: conn %d  Using authcDN=%s\n",
547                            conn ? conn->c_connid : -1, authcDN.bv_val ));
548 #else
549                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
550                  "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
551 #endif
552
553                 *user = authcDN.bv_val;
554                 *errstr = NULL;
555                 return SASL_OK;
556         }
557         rc = slap_sasl_getdn( conn, (char *)authzid, realm, &authzDN, FLAG_GETDN_AUTHZID );
558         if( rc != LDAP_SUCCESS ) {
559                 ch_free( authcDN.bv_val );
560                 *errstr = ldap_err2string( rc );
561                 return SASL_NOAUTHZ;
562         }
563
564         rc = slap_sasl_authorized( &authcDN, &authzDN );
565         if( rc ) {
566 #ifdef NEW_LOGGING
567                 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
568                            "slap_sasl_authorize: conn %ld  authorization disallowed (%d)\n",
569                            (long)(conn ? conn->c_connid : -1), rc ));
570 #else
571                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
572                         " authorization disallowed (%d)\n",
573                         (long) (conn ? conn->c_connid : -1), rc, 0 );
574 #endif
575
576                 *errstr = "not authorized";
577                 ch_free( authcDN.bv_val );
578                 ch_free( authzDN.bv_val );
579                 return SASL_NOAUTHZ;
580         }
581
582 #ifdef NEW_LOGGING
583         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
584                    "slap_sasl_authorize: conn %d authorization allowed\n",
585                    (long)(conn ? conn->c_connid : -1 ) ));
586 #else
587         Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
588                 " authorization allowed\n",
589                 (long) (conn ? conn->c_connid : -1), 0, 0 );
590 #endif
591
592
593         ch_free( authcDN.bv_val );
594         *user = authzDN.bv_val;
595         *errstr = NULL;
596         return SASL_OK;
597 }
598 #endif /* SASL_VERSION_MAJOR >= 2 */
599
600 static int
601 slap_sasl_err2ldap( int saslerr )
602 {
603         int rc;
604
605         switch (saslerr) {
606                 case SASL_CONTINUE:
607                         rc = LDAP_SASL_BIND_IN_PROGRESS;
608                         break;
609                 case SASL_FAIL:
610                         rc = LDAP_OTHER;
611                         break;
612                 case SASL_NOMEM:
613                         rc = LDAP_OTHER;
614                         break;
615                 case SASL_NOMECH:
616                         rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
617                         break;
618                 case SASL_BADAUTH:
619                         rc = LDAP_INVALID_CREDENTIALS;
620                         break;
621                 case SASL_NOAUTHZ:
622                         rc = LDAP_INSUFFICIENT_ACCESS;
623                         break;
624                 case SASL_TOOWEAK:
625                 case SASL_ENCRYPT:
626                         rc = LDAP_INAPPROPRIATE_AUTH;
627                         break;
628                 default:
629                         rc = LDAP_OTHER;
630                         break;
631         }
632
633         return rc;
634 }
635 #endif
636
637
638 int slap_sasl_init( void )
639 {
640 #ifdef HAVE_CYRUS_SASL
641         int rc;
642         static sasl_callback_t server_callbacks[] = {
643                 { SASL_CB_LOG, &slap_sasl_log, NULL },
644                 { SASL_CB_LIST_END, NULL, NULL }
645         };
646
647         sasl_set_alloc(
648                 ch_malloc,
649                 ch_calloc,
650                 ch_realloc,
651                 ch_free ); 
652
653         sasl_set_mutex(
654                 ldap_pvt_sasl_mutex_new,
655                 ldap_pvt_sasl_mutex_lock,
656                 ldap_pvt_sasl_mutex_unlock,
657                 ldap_pvt_sasl_mutex_dispose );
658
659         /* should provide callbacks for logging */
660         /* server name should be configurable */
661         rc = sasl_server_init( server_callbacks, "slapd" );
662
663         if( rc != SASL_OK ) {
664 #ifdef NEW_LOGGING
665                 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
666                            "slap_sasl_init: init failed.\n" ));
667 #else
668                 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
669                         0, 0, 0 );
670 #endif
671
672                 return -1;
673         }
674
675 #ifdef NEW_LOGGING
676         LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
677                    "slap_sasl_init: initialized!\n"));
678 #else
679         Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
680                 0, 0, 0 );
681 #endif
682
683
684         /* default security properties */
685         memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
686         sasl_secprops.max_ssf = INT_MAX;
687         sasl_secprops.maxbufsize = 65536;
688         sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
689 #endif
690
691         return 0;
692 }
693
694 int slap_sasl_destroy( void )
695 {
696 #ifdef HAVE_CYRUS_SASL
697         sasl_done();
698 #endif
699         free( global_host );
700         global_host = NULL;
701
702         return 0;
703 }
704
705 int slap_sasl_open( Connection *conn )
706 {
707         int cb, sc = LDAP_SUCCESS;
708 #if SASL_VERSION_MAJOR >= 2
709         char *ipremoteport = NULL, *iplocalport = NULL;
710 #endif
711
712 #ifdef HAVE_CYRUS_SASL
713         sasl_conn_t *ctx = NULL;
714         sasl_callback_t *session_callbacks;
715
716         assert( conn->c_sasl_context == NULL );
717         assert( conn->c_sasl_extra == NULL );
718
719         conn->c_sasl_layers = 0;
720
721         session_callbacks =
722 #if SASL_VERSION_MAJOR >= 2
723                 ch_calloc( 5, sizeof(sasl_callback_t));
724 #else
725                 ch_calloc( 3, sizeof(sasl_callback_t));
726 #endif
727         conn->c_sasl_extra = session_callbacks;
728
729         session_callbacks[cb=0].id = SASL_CB_LOG;
730         session_callbacks[cb].proc = &slap_sasl_log;
731         session_callbacks[cb++].context = conn;
732
733         session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
734         session_callbacks[cb].proc = &slap_sasl_authorize;
735         session_callbacks[cb++].context = conn;
736
737 #if SASL_VERSION_MAJOR >= 2
738         session_callbacks[cb].id = SASL_CB_CANON_USER;
739         session_callbacks[cb].proc = &slap_sasl_canonicalize;
740         session_callbacks[cb++].context = conn;
741
742         /* XXXX: this should be conditional */
743         session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
744         session_callbacks[cb].proc = &slap_sasl_checkpass;
745         session_callbacks[cb++].context = conn;
746 #endif
747
748         session_callbacks[cb].id = SASL_CB_LIST_END;
749         session_callbacks[cb].proc = NULL;
750         session_callbacks[cb++].context = NULL;
751
752         if( global_host == NULL ) {
753                 global_host = ldap_pvt_get_fqdn( NULL );
754         }
755
756         /* create new SASL context */
757 #if SASL_VERSION_MAJOR >= 2
758         if ( conn->c_sock_name.bv_len != 0 &&
759              strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
760                 char *p;
761
762                 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
763                 /* Convert IPv6 addresses to address;port syntax. */
764                 p = strrchr( iplocalport, ' ' );
765                 /* Convert IPv4 addresses to address;port syntax. */
766                 if ( p == NULL ) p = strchr( iplocalport, ':' );
767                 if ( p != NULL ) {
768                         *p = ';';
769                 }
770         }
771         if ( conn->c_peer_name.bv_len != 0 &&
772              strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
773                 char *p;
774
775                 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
776                 /* Convert IPv6 addresses to address;port syntax. */
777                 p = strrchr( ipremoteport, ' ' );
778                 /* Convert IPv4 addresses to address;port syntax. */
779                 if ( p == NULL ) p = strchr( ipremoteport, ':' );
780                 if ( p != NULL ) {
781                         *p = ';';
782                 }
783         }
784         sc = sasl_server_new( "ldap", global_host, global_realm,
785                 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
786         if ( iplocalport != NULL ) {
787                 ch_free( iplocalport );
788         }
789         if ( ipremoteport != NULL ) {
790                 ch_free( ipremoteport );
791         }
792 #else
793         sc = sasl_server_new( "ldap", global_host, global_realm,
794                 session_callbacks, SASL_SECURITY_LAYER, &ctx );
795 #endif
796
797         if( sc != SASL_OK ) {
798 #ifdef NEW_LOGGING
799                 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
800                            "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
801 #else
802                 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
803                         sc, 0, 0 );
804 #endif
805
806                 return -1;
807         }
808
809         conn->c_sasl_context = ctx;
810
811         if( sc == SASL_OK ) {
812                 sc = sasl_setprop( ctx,
813                         SASL_SEC_PROPS, &sasl_secprops );
814
815                 if( sc != SASL_OK ) {
816 #ifdef NEW_LOGGING
817                         LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
818                                    "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
819 #else
820                         Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
821                                 sc, 0, 0 );
822 #endif
823
824                         slap_sasl_close( conn );
825                         return -1;
826                 }
827         }
828
829         sc = slap_sasl_err2ldap( sc );
830 #endif
831         return sc;
832 }
833
834 int slap_sasl_external(
835         Connection *conn,
836         slap_ssf_t ssf,
837         const char *auth_id )
838 {
839 #if SASL_VERSION_MAJOR >= 2
840         int sc;
841         sasl_conn_t *ctx = conn->c_sasl_context;
842
843         if ( ctx == NULL ) {
844                 return LDAP_UNAVAILABLE;
845         }
846
847         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
848
849         if ( sc != SASL_OK ) {
850                 return LDAP_OTHER;
851         }
852
853         sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
854
855         if ( sc != SASL_OK ) {
856                 return LDAP_OTHER;
857         }
858
859 #elif defined(HAVE_CYRUS_SASL)
860         int sc;
861         sasl_conn_t *ctx = conn->c_sasl_context;
862         sasl_external_properties_t extprops;
863
864         if ( ctx == NULL ) {
865                 return LDAP_UNAVAILABLE;
866         }
867
868         memset( &extprops, '\0', sizeof(extprops) );
869         extprops.ssf = ssf;
870         extprops.auth_id = (char *) auth_id;
871
872         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
873                 (void *) &extprops );
874
875         if ( sc != SASL_OK ) {
876                 return LDAP_OTHER;
877         }
878 #endif
879
880         return LDAP_SUCCESS;
881 }
882
883 int slap_sasl_reset( Connection *conn )
884 {
885 #ifdef HAVE_CYRUS_SASL
886         sasl_conn_t *ctx = conn->c_sasl_context;
887
888         if( ctx != NULL ) {
889         }
890 #endif
891         /* must return "anonymous" */
892         return LDAP_SUCCESS;
893 }
894
895 char ** slap_sasl_mechs( Connection *conn )
896 {
897         char **mechs = NULL;
898
899 #ifdef HAVE_CYRUS_SASL
900         sasl_conn_t *ctx = conn->c_sasl_context;
901
902         if( ctx != NULL ) {
903                 int sc;
904                 SASL_CONST char *mechstr;
905
906                 sc = sasl_listmech( ctx,
907                         NULL, NULL, ",", NULL,
908                         &mechstr, NULL, NULL );
909
910                 if( sc != SASL_OK ) {
911 #ifdef NEW_LOGGING
912                         LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
913                                 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
914 #else
915                         Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
916                                 sc, 0, 0 );
917 #endif
918
919                         return NULL;
920                 }
921
922                 mechs = str2charray( mechstr, "," );
923
924 #if SASL_VERSION_MAJOR < 2
925                 ch_free( mechstr );
926 #endif
927         }
928 #endif
929
930         return mechs;
931 }
932
933 int slap_sasl_close( Connection *conn )
934 {
935 #ifdef HAVE_CYRUS_SASL
936         sasl_conn_t *ctx = conn->c_sasl_context;
937
938         if( ctx != NULL ) {
939                 sasl_dispose( &ctx );
940         }
941
942         conn->c_sasl_context = NULL;
943
944         free( conn->c_sasl_extra );
945         conn->c_sasl_extra = NULL;
946 #endif
947
948         return LDAP_SUCCESS;
949 }
950
951 int slap_sasl_bind(
952     Connection          *conn,
953     Operation           *op,  
954     struct berval       *dn,  
955     struct berval       *ndn,
956     struct berval       *cred,
957         struct berval                   *edn,
958         slap_ssf_t              *ssfp )
959 {
960         int rc = 1;
961
962 #ifdef HAVE_CYRUS_SASL
963         sasl_conn_t *ctx = conn->c_sasl_context;
964         struct berval response;
965         unsigned reslen = 0;
966         const char *errstr = NULL;
967         int sc;
968
969 #ifdef NEW_LOGGING
970         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
971                 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
972                 conn->c_connid,
973                 dn->bv_len ? dn->bv_val : "",
974                 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech.bv_val,
975                 cred ? cred->bv_len : 0 ));
976 #else
977         Debug(LDAP_DEBUG_ARGS,
978                 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
979                 dn->bv_len ? dn->bv_val : "",
980                 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
981                 cred ? cred->bv_len : 0 );
982 #endif
983
984
985         if( ctx == NULL ) {
986                 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
987                         NULL, "SASL unavailable on this session", NULL, NULL );
988                 return rc;
989         }
990
991 #if SASL_VERSION_MAJOR >= 2
992 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
993         sasl_server_start( ctx, mech, cred, clen, resp, rlen )
994 #define STEP( ctx, cred, clen, resp, rlen, err ) \
995         sasl_server_step( ctx, cred, clen, resp, rlen )
996 #else
997 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
998         sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
999 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1000         sasl_server_step( ctx, cred, clen, resp, rlen, err )
1001 #endif
1002
1003         if ( !conn->c_sasl_bind_in_progress ) {
1004                 sc = START( ctx,
1005                         conn->c_sasl_bind_mech.bv_val,
1006                         cred->bv_len ? cred->bv_val : "",
1007                         cred->bv_len,
1008                         (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1009
1010         } else {
1011                 sc = STEP( ctx,
1012                         cred->bv_val, cred->bv_len,
1013                         (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1014         }
1015
1016         response.bv_len = reslen;
1017
1018         if ( sc == SASL_OK ) {
1019                 char *username = NULL;
1020                 char *realm = NULL;
1021
1022 #if SASL_VERSION_MAJOR >= 2
1023                 sc = sasl_getprop( ctx, SASL_DEFUSERREALM, (const void **)&realm );
1024 #else
1025                 sc = sasl_getprop( ctx, SASL_REALM, (void **)&realm );
1026 #endif
1027                 sc = sasl_getprop( ctx,
1028                         SASL_USERNAME, (SASL_CONST void **)&username );
1029
1030                 if ( sc != SASL_OK ) {
1031 #ifdef NEW_LOGGING
1032                         LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1033                                 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
1034 #else
1035                         Debug(LDAP_DEBUG_TRACE,
1036                                 "slap_sasl_bind: getprop(USERNAME) failed!\n",
1037                                 0, 0, 0);
1038 #endif
1039
1040
1041                         send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1042                                 NULL, "no SASL username", NULL, NULL );
1043
1044                 } else {
1045                         rc = slap_sasl_getdn( conn, username, realm, edn, FLAG_GETDN_FINAL );
1046
1047                         if( rc == LDAP_SUCCESS ) {
1048                                 sasl_ssf_t *ssf = NULL;
1049                                 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1050                                 *ssfp = ssf ? *ssf : 0;
1051
1052                                 if( *ssfp ) {
1053                                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1054                                         conn->c_sasl_layers++;
1055                                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1056                                 }
1057
1058                                 send_ldap_sasl( conn, op, rc,
1059                                         NULL, NULL, NULL, NULL,
1060                                         response.bv_len ? &response : NULL );
1061
1062                         } else {
1063                                 send_ldap_result( conn, op, rc,
1064                                         NULL, errstr, NULL, NULL );
1065                         }
1066                 }
1067
1068         } else if ( sc == SASL_CONTINUE ) {
1069                 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1070                         NULL, NULL, NULL, NULL, &response );
1071
1072         } else {
1073                 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1074                         NULL, errstr, NULL, NULL );
1075         }
1076
1077 #if SASL_VERSION_MAJOR < 2
1078         if( response.bv_len ) {
1079                 ch_free( response.bv_val );
1080         }
1081 #endif
1082
1083 #ifdef NEW_LOGGING
1084         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1085                 "slap_sasl_bind: rc=%d\n", rc ));
1086 #else
1087         Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1088 #endif
1089
1090
1091 #else
1092         send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1093                 NULL, "SASL not supported", NULL, NULL );
1094 #endif
1095
1096         return rc;
1097 }
1098
1099 char* slap_sasl_secprops( const char *in )
1100 {
1101 #ifdef HAVE_CYRUS_SASL
1102         int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1103
1104         return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1105 #else
1106         return "SASL not supported";
1107 #endif
1108 }