]> git.sur5r.net Git - openldap/blob - servers/slapd/sasl.c
f42a13de13586c11b5f7f5bedfe3a8d84dca733f
[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 );
224                 p = slap_strcopy( dn->bv_val, "uid=" );
225                 p = slap_strcopy( p, c1 );
226                 ch_free( c1 );
227
228                 if( 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 can we do both steps at once? */
310         rc = slap_sasl_getdn( conn, (char *)username, NULL, &dn,
311                 FLAG_GETDN_AUTHCID | FLAG_GETDN_FINAL );
312         if ( rc != LDAP_SUCCESS ) {
313                 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
314                 return SASL_NOUSER;
315         }
316
317         if ( dn.bv_len == 0 ) {
318                 sasl_seterror( sconn, 0,
319                         "No password is associated with the Root DSE" );
320                 if ( dn.bv_val != NULL ) {
321                         ch_free( dn.bv_val );
322                 }
323                 return SASL_NOUSER;
324         }
325
326         rc = backend_attribute( NULL, NULL, NULL, NULL, &dn,
327                 slap_schema.si_ad_userPassword, &vals);
328         if ( rc != LDAP_SUCCESS ) {
329                 ch_free( dn.bv_val );
330                 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
331                 return SASL_NOVERIFY;
332         }
333
334         rc = SASL_NOVERIFY;
335
336         if ( vals != NULL ) {
337                 for ( bv = vals; bv->bv_val != NULL; bv++ ) {
338                         if ( !lutil_passwd( bv, &cred, NULL ) ) {
339                                 rc = SASL_OK;
340                                 break;
341                         }
342                 }
343                 ber_bvarray_free( vals );
344         }
345
346         if ( rc != SASL_OK ) {
347                 sasl_seterror( sconn, 0,
348                         ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
349         }
350
351         ch_free( dn.bv_val );
352
353         return rc;
354 }
355
356 static int
357 slap_sasl_canonicalize(
358         sasl_conn_t *sconn,
359         void *context,
360         const char *in,
361         unsigned inlen,
362         unsigned flags,
363         const char *user_realm,
364         char *out,
365         unsigned out_max,
366         unsigned *out_len)
367 {
368         Connection *conn = (Connection *)context;
369         struct berval dn;
370         int rc;
371
372         *out_len = 0;
373
374 #ifdef NEW_LOGGING
375         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
376                 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
377                         conn ? conn->c_connid : -1,
378                         (flags == SASL_CU_AUTHID) ? "authcid" : "authzid",
379                         in ? in : "<empty>" ));
380 #else
381         Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
382                 "%s=\"%s\"\n",
383                         conn ? conn->c_connid : -1,
384                         (flags == SASL_CU_AUTHID) ? "authcid" : "authzid",
385                         in ? in : "<empty>" );
386 #endif
387
388         rc = slap_sasl_getdn( conn, (char *)in, (char *)user_realm, &dn,
389                 (flags == SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
390         if ( rc != LDAP_SUCCESS ) {
391                 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
392                 return SASL_NOAUTHZ;
393         }               
394
395         if ( out_max < dn.bv_len ) {
396                 return SASL_BUFOVER;
397         }
398
399         AC_MEMCPY( out, dn.bv_val, dn.bv_len );
400         out[dn.bv_len] = '\0';
401
402         *out_len = dn.bv_len;
403
404         ch_free( dn.bv_val );
405
406 #ifdef NEW_LOGGING
407         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
408                 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
409                         conn ? conn->c_connid : -1,
410                         (flags == SASL_CU_AUTHID) ? "authcDN" : "authzDN",
411                         out ));
412 #else
413         Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
414                 "%s=\"%s\"\n",
415                         conn ? conn->c_connid : -1,
416                         (flags == SASL_CU_AUTHID) ? "authcDN" : "authzDN",
417                         out );
418 #endif
419
420         return SASL_OK;
421 }
422
423 static int
424 slap_sasl_authorize(
425         sasl_conn_t *sconn,
426         void *context,
427         const char *requested_user,
428         unsigned rlen,
429         const char *auth_identity,
430         unsigned alen,
431         const char *def_realm,
432         unsigned urlen,
433         struct propctx *propctx)
434 {
435         Connection *conn = (Connection *)context;
436         struct berval authcDN, authzDN;
437         int rc;
438
439         authcDN.bv_val = (char *)auth_identity;
440         authcDN.bv_len = alen;
441
442         authzDN.bv_val = (char *)requested_user;
443         authzDN.bv_len = rlen;
444
445 #ifdef NEW_LOGGING
446         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
447                 "slap_sasl_authorize: conn %d authcDN=\"%s\" authzDN=\"%s\"\n",
448                         conn ? conn->c_connid : -1, authcDN.bv_val, authzDN.bv_val));
449 #else
450         Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
451                 "authcDN=\"%s\" authzDN=\"%s\"\n",
452                 conn ? conn->c_connid : -1, authcDN.bv_val, authzDN.bv_val );
453 #endif
454
455         rc = slap_sasl_authorized( &authcDN, &authzDN );
456         if ( rc != LDAP_SUCCESS ) {
457 #ifdef NEW_LOGGING
458                 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
459                            "slap_sasl_authorize: conn %ld  authorization disallowed (%d)\n",
460                            (long)(conn ? conn->c_connid : -1), rc ));
461 #else
462                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
463                         " authorization disallowed (%d)\n",
464                         (long) (conn ? conn->c_connid : -1), rc, 0 );
465 #endif
466
467                 sasl_seterror( sconn, 0, "not authorized" );
468                 return SASL_NOAUTHZ;
469         }
470
471 #ifdef NEW_LOGGING
472         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
473                    "slap_sasl_authorize: conn %d authorization allowed\n",
474                    (long)(conn ? conn->c_connid : -1 ) ));
475 #else
476         Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
477                 " authorization allowed\n",
478                 (long) (conn ? conn->c_connid : -1), 0, 0 );
479 #endif
480
481         return SASL_OK;
482
483 #else
484 static int
485 slap_sasl_authorize(
486         void *context,
487         const char *authcid,
488         const char *authzid,
489         const char **user,
490         const char **errstr)
491 {
492         struct berval authcDN, authzDN;
493         int rc;
494         Connection *conn = context;
495         char *realm;
496
497         *user = NULL;
498
499 #ifdef NEW_LOGGING
500         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
501                    "slap_sasl_authorize: conn %d         authcid=\"%s\" authzid=\"%s\"\n",
502                    conn ? conn->c_connid : -1,
503                    authcid ? authcid : "<empty>",
504                    authzid ? authzid : "<empty>" ));
505 #else
506         Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
507                 "authcid=\"%s\" authzid=\"%s\"\n",
508                 (long) (conn ? conn->c_connid : -1),
509                 authcid ? authcid : "<empty>",
510                 authzid ? authzid : "<empty>" );
511 #endif
512
513         /* Figure out how much data we have for the dn */
514         rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
515         if( rc != SASL_OK && rc != SASL_NOTDONE ) {
516 #ifdef NEW_LOGGING
517                 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
518                         "slap_sasl_authorize: getprop(REALM) failed.\n" ));
519 #else
520                 Debug(LDAP_DEBUG_TRACE,
521                         "authorize: getprop(REALM) failed!\n", 0,0,0);
522 #endif
523                 *errstr = "Could not extract realm";
524                 return SASL_NOAUTHZ;
525         }
526
527         /* Convert the identities to DN's. If no authzid was given, client will
528            be bound as the DN matching their username */
529         rc = slap_sasl_getdn( conn, (char *)authcid, realm, &authcDN, FLAG_GETDN_AUTHCID );
530         if( rc != LDAP_SUCCESS ) {
531                 *errstr = ldap_err2string( rc );
532                 return SASL_NOAUTHZ;
533         }
534         if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
535 #ifdef NEW_LOGGING
536                 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
537                            "slap_sasl_authorize: conn %d  Using authcDN=%s\n",
538                            conn ? conn->c_connid : -1, authcDN.bv_val ));
539 #else
540                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
541                  "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
542 #endif
543
544                 *user = authcDN.bv_val;
545                 *errstr = NULL;
546                 return SASL_OK;
547         }
548         rc = slap_sasl_getdn( conn, (char *)authzid, realm, &authzDN, FLAG_GETDN_AUTHZID );
549         if( rc != LDAP_SUCCESS ) {
550                 ch_free( authcDN.bv_val );
551                 *errstr = ldap_err2string( rc );
552                 return SASL_NOAUTHZ;
553         }
554
555         rc = slap_sasl_authorized( &authcDN, &authzDN );
556         if( rc ) {
557 #ifdef NEW_LOGGING
558                 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
559                            "slap_sasl_authorize: conn %ld  authorization disallowed (%d)\n",
560                            (long)(conn ? conn->c_connid : -1), rc ));
561 #else
562                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
563                         " authorization disallowed (%d)\n",
564                         (long) (conn ? conn->c_connid : -1), rc, 0 );
565 #endif
566
567                 *errstr = "not authorized";
568                 ch_free( authcDN.bv_val );
569                 ch_free( authzDN.bv_val );
570                 return SASL_NOAUTHZ;
571         }
572
573 #ifdef NEW_LOGGING
574         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
575                    "slap_sasl_authorize: conn %d authorization allowed\n",
576                    (long)(conn ? conn->c_connid : -1 ) ));
577 #else
578         Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
579                 " authorization allowed\n",
580                 (long) (conn ? conn->c_connid : -1), 0, 0 );
581 #endif
582
583
584         ch_free( authcDN.bv_val );
585         *user = authzDN.bv_val;
586         *errstr = NULL;
587         return SASL_OK;
588 }
589 #endif /* SASL_VERSION_MAJOR >= 2 */
590
591 static int
592 slap_sasl_err2ldap( int saslerr )
593 {
594         int rc;
595
596         switch (saslerr) {
597                 case SASL_CONTINUE:
598                         rc = LDAP_SASL_BIND_IN_PROGRESS;
599                         break;
600                 case SASL_FAIL:
601                         rc = LDAP_OTHER;
602                         break;
603                 case SASL_NOMEM:
604                         rc = LDAP_OTHER;
605                         break;
606                 case SASL_NOMECH:
607                         rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
608                         break;
609                 case SASL_BADAUTH:
610                         rc = LDAP_INVALID_CREDENTIALS;
611                         break;
612                 case SASL_NOAUTHZ:
613                         rc = LDAP_INSUFFICIENT_ACCESS;
614                         break;
615                 case SASL_TOOWEAK:
616                 case SASL_ENCRYPT:
617                         rc = LDAP_INAPPROPRIATE_AUTH;
618                         break;
619                 default:
620                         rc = LDAP_OTHER;
621                         break;
622         }
623
624         return rc;
625 }
626 #endif
627
628
629 int slap_sasl_init( void )
630 {
631 #ifdef HAVE_CYRUS_SASL
632         int rc;
633         static sasl_callback_t server_callbacks[] = {
634                 { SASL_CB_LOG, &slap_sasl_log, NULL },
635                 { SASL_CB_LIST_END, NULL, NULL }
636         };
637
638         sasl_set_alloc(
639                 ch_malloc,
640                 ch_calloc,
641                 ch_realloc,
642                 ch_free ); 
643
644         sasl_set_mutex(
645                 ldap_pvt_sasl_mutex_new,
646                 ldap_pvt_sasl_mutex_lock,
647                 ldap_pvt_sasl_mutex_unlock,
648                 ldap_pvt_sasl_mutex_dispose );
649
650         /* should provide callbacks for logging */
651         /* server name should be configurable */
652         rc = sasl_server_init( server_callbacks, "slapd" );
653
654         if( rc != SASL_OK ) {
655 #ifdef NEW_LOGGING
656                 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
657                            "slap_sasl_init: init failed.\n" ));
658 #else
659                 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
660                         0, 0, 0 );
661 #endif
662
663                 return -1;
664         }
665
666 #ifdef NEW_LOGGING
667         LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
668                    "slap_sasl_init: initialized!\n"));
669 #else
670         Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
671                 0, 0, 0 );
672 #endif
673
674
675         /* default security properties */
676         memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
677         sasl_secprops.max_ssf = INT_MAX;
678         sasl_secprops.maxbufsize = 65536;
679         sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
680 #endif
681
682         return 0;
683 }
684
685 int slap_sasl_destroy( void )
686 {
687 #ifdef HAVE_CYRUS_SASL
688         sasl_done();
689 #endif
690         free( global_host );
691         global_host = NULL;
692
693         return 0;
694 }
695
696 int slap_sasl_open( Connection *conn )
697 {
698         int cb, sc = LDAP_SUCCESS;
699 #if SASL_VERSION_MAJOR >= 2
700         char *ipremoteport = NULL, *iplocalport = NULL;
701 #endif
702
703 #ifdef HAVE_CYRUS_SASL
704         sasl_conn_t *ctx = NULL;
705         sasl_callback_t *session_callbacks;
706
707         assert( conn->c_sasl_context == NULL );
708         assert( conn->c_sasl_extra == NULL );
709
710         conn->c_sasl_layers = 0;
711
712         session_callbacks =
713 #if SASL_VERSION_MAJOR >= 2
714                 ch_calloc( 5, sizeof(sasl_callback_t));
715 #else
716                 ch_calloc( 3, sizeof(sasl_callback_t));
717 #endif
718         conn->c_sasl_extra = session_callbacks;
719
720         session_callbacks[cb=0].id = SASL_CB_LOG;
721         session_callbacks[cb].proc = &slap_sasl_log;
722         session_callbacks[cb++].context = conn;
723
724         session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
725         session_callbacks[cb].proc = &slap_sasl_authorize;
726         session_callbacks[cb++].context = conn;
727
728 #if SASL_VERSION_MAJOR >= 2
729         session_callbacks[cb].id = SASL_CB_CANON_USER;
730         session_callbacks[cb].proc = &slap_sasl_canonicalize;
731         session_callbacks[cb++].context = conn;
732
733         /* XXXX: this should be conditional */
734         session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
735         session_callbacks[cb].proc = &slap_sasl_checkpass;
736         session_callbacks[cb++].context = conn;
737 #endif
738
739         session_callbacks[cb].id = SASL_CB_LIST_END;
740         session_callbacks[cb].proc = NULL;
741         session_callbacks[cb++].context = NULL;
742
743         if( global_host == NULL ) {
744                 global_host = ldap_pvt_get_fqdn( NULL );
745         }
746
747         /* create new SASL context */
748 #if SASL_VERSION_MAJOR >= 2
749         if ( conn->c_sock_name.bv_len != 0 &&
750              strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
751                 char *p;
752
753                 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
754                 /* Convert IPv6 addresses to address;port syntax. */
755                 p = strrchr( iplocalport, ' ' );
756                 if ( p != NULL ) {
757                         *p = ';';
758                 }
759         }
760         if ( conn->c_peer_name.bv_len != 0 &&
761              strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
762                 char *p;
763
764                 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
765                 /* Convert IPv6 addresses to address;port syntax. */
766                 p = strrchr( ipremoteport, ' ' );
767                 if ( p != NULL ) {
768                         *p = ';';
769                 }
770         }
771         sc = sasl_server_new( "ldap", global_host, global_realm,
772                 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
773         if ( iplocalport != NULL ) {
774                 ch_free( iplocalport );
775         }
776         if ( ipremoteport != NULL ) {
777                 ch_free( ipremoteport );
778         }
779 #else
780         sc = sasl_server_new( "ldap", global_host, global_realm,
781                 session_callbacks, SASL_SECURITY_LAYER, &ctx );
782 #endif
783
784         if( sc != SASL_OK ) {
785 #ifdef NEW_LOGGING
786                 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
787                            "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
788 #else
789                 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
790                         sc, 0, 0 );
791 #endif
792
793                 return -1;
794         }
795
796         conn->c_sasl_context = ctx;
797
798         if( sc == SASL_OK ) {
799                 sc = sasl_setprop( ctx,
800                         SASL_SEC_PROPS, &sasl_secprops );
801
802                 if( sc != SASL_OK ) {
803 #ifdef NEW_LOGGING
804                         LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
805                                    "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
806 #else
807                         Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
808                                 sc, 0, 0 );
809 #endif
810
811                         slap_sasl_close( conn );
812                         return -1;
813                 }
814         }
815
816         sc = slap_sasl_err2ldap( sc );
817 #endif
818         return sc;
819 }
820
821 int slap_sasl_external(
822         Connection *conn,
823         slap_ssf_t ssf,
824         const char *auth_id )
825 {
826 #if SASL_VERSION_MAJOR >= 2
827         int sc;
828         sasl_conn_t *ctx = conn->c_sasl_context;
829
830         if ( ctx == NULL ) {
831                 return LDAP_UNAVAILABLE;
832         }
833
834         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
835
836         if ( sc != SASL_OK ) {
837                 return LDAP_OTHER;
838         }
839
840         sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
841
842         if ( sc != SASL_OK ) {
843                 return LDAP_OTHER;
844         }
845
846 #elif defined(HAVE_CYRUS_SASL)
847         int sc;
848         sasl_conn_t *ctx = conn->c_sasl_context;
849         sasl_external_properties_t extprops;
850
851         if ( ctx == NULL ) {
852                 return LDAP_UNAVAILABLE;
853         }
854
855         memset( &extprops, '\0', sizeof(extprops) );
856         extprops.ssf = ssf;
857         extprops.auth_id = (char *) auth_id;
858
859         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
860                 (void *) &extprops );
861
862         if ( sc != SASL_OK ) {
863                 return LDAP_OTHER;
864         }
865 #endif
866
867         return LDAP_SUCCESS;
868 }
869
870 int slap_sasl_reset( Connection *conn )
871 {
872 #ifdef HAVE_CYRUS_SASL
873         sasl_conn_t *ctx = conn->c_sasl_context;
874
875         if( ctx != NULL ) {
876         }
877 #endif
878         /* must return "anonymous" */
879         return LDAP_SUCCESS;
880 }
881
882 char ** slap_sasl_mechs( Connection *conn )
883 {
884         char **mechs = NULL;
885
886 #ifdef HAVE_CYRUS_SASL
887         sasl_conn_t *ctx = conn->c_sasl_context;
888
889         if( ctx != NULL ) {
890                 int sc;
891                 SASL_CONST char *mechstr;
892
893                 sc = sasl_listmech( ctx,
894                         NULL, NULL, ",", NULL,
895                         &mechstr, NULL, NULL );
896
897                 if( sc != SASL_OK ) {
898 #ifdef NEW_LOGGING
899                         LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
900                                 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
901 #else
902                         Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
903                                 sc, 0, 0 );
904 #endif
905
906                         return NULL;
907                 }
908
909                 mechs = str2charray( mechstr, "," );
910
911 #if SASL_VERSION_MAJOR < 2
912                 ch_free( mechstr );
913 #endif
914         }
915 #endif
916
917         return mechs;
918 }
919
920 int slap_sasl_close( Connection *conn )
921 {
922 #ifdef HAVE_CYRUS_SASL
923         sasl_conn_t *ctx = conn->c_sasl_context;
924
925         if( ctx != NULL ) {
926                 sasl_dispose( &ctx );
927         }
928
929         conn->c_sasl_context = NULL;
930
931         free( conn->c_sasl_extra );
932         conn->c_sasl_extra = NULL;
933 #endif
934
935         return LDAP_SUCCESS;
936 }
937
938 int slap_sasl_bind(
939     Connection          *conn,
940     Operation           *op,  
941     struct berval       *dn,  
942     struct berval       *ndn,
943     struct berval       *cred,
944         struct berval                   *edn,
945         slap_ssf_t              *ssfp )
946 {
947         int rc = 1;
948
949 #ifdef HAVE_CYRUS_SASL
950         sasl_conn_t *ctx = conn->c_sasl_context;
951         struct berval response;
952         unsigned reslen = 0;
953         const char *errstr = NULL;
954         int sc;
955
956 #ifdef NEW_LOGGING
957         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
958                 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
959                 conn->c_connid,
960                 dn->bv_len ? dn->bv_val : "",
961                 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech.bv_val,
962                 cred ? cred->bv_len : 0 ));
963 #else
964         Debug(LDAP_DEBUG_ARGS,
965                 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
966                 dn->bv_len ? dn->bv_val : "",
967                 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
968                 cred ? cred->bv_len : 0 );
969 #endif
970
971
972         if( ctx == NULL ) {
973                 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
974                         NULL, "SASL unavailable on this session", NULL, NULL );
975                 return rc;
976         }
977
978 #if SASL_VERSION_MAJOR >= 2
979 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
980         sasl_server_start( ctx, mech, cred, clen, resp, rlen )
981 #define STEP( ctx, cred, clen, resp, rlen, err ) \
982         sasl_server_step( ctx, cred, clen, resp, rlen )
983 #else
984 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
985         sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
986 #define STEP( ctx, cred, clen, resp, rlen, err ) \
987         sasl_server_step( ctx, cred, clen, resp, rlen, err )
988 #endif
989
990         if ( !conn->c_sasl_bind_in_progress ) {
991                 sc = START( ctx,
992                         conn->c_sasl_bind_mech.bv_val,
993                         cred->bv_len ? cred->bv_val : "",
994                         cred->bv_len,
995                         (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
996
997         } else {
998                 sc = STEP( ctx,
999                         cred->bv_val, cred->bv_len,
1000                         (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1001         }
1002
1003         response.bv_len = reslen;
1004
1005         if ( sc == SASL_OK ) {
1006                 char *username = NULL;
1007                 char *realm = NULL;
1008
1009 #if SASL_VERSION_MAJOR >= 2
1010                 sc = sasl_getprop( ctx, SASL_DEFUSERREALM, (const void **)&realm );
1011 #else
1012                 sc = sasl_getprop( ctx, SASL_REALM, (void **)&realm );
1013 #endif
1014                 sc = sasl_getprop( ctx,
1015                         SASL_USERNAME, (SASL_CONST void **)&username );
1016
1017                 if ( sc != SASL_OK ) {
1018 #ifdef NEW_LOGGING
1019                         LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1020                                 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
1021 #else
1022                         Debug(LDAP_DEBUG_TRACE,
1023                                 "slap_sasl_bind: getprop(USERNAME) failed!\n",
1024                                 0, 0, 0);
1025 #endif
1026
1027
1028                         send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1029                                 NULL, "no SASL username", NULL, NULL );
1030
1031                 } else {
1032                         rc = slap_sasl_getdn( conn, username, realm, edn, FLAG_GETDN_FINAL );
1033
1034                         if( rc == LDAP_SUCCESS ) {
1035                                 sasl_ssf_t *ssf = NULL;
1036                                 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1037                                 *ssfp = ssf ? *ssf : 0;
1038
1039                                 if( *ssfp ) {
1040                                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1041                                         conn->c_sasl_layers++;
1042                                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1043                                 }
1044
1045                                 send_ldap_sasl( conn, op, rc,
1046                                         NULL, NULL, NULL, NULL,
1047                                         response.bv_len ? &response : NULL );
1048
1049                         } else {
1050                                 send_ldap_result( conn, op, rc,
1051                                         NULL, errstr, NULL, NULL );
1052                         }
1053                 }
1054
1055         } else if ( sc == SASL_CONTINUE ) {
1056                 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1057                         NULL, NULL, NULL, NULL, &response );
1058
1059         } else {
1060                 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1061                         NULL, errstr, NULL, NULL );
1062         }
1063
1064 #if SASL_VERSION_MAJOR < 2
1065         if( response.bv_len ) {
1066                 ch_free( response.bv_val );
1067         }
1068 #endif
1069
1070 #ifdef NEW_LOGGING
1071         LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1072                 "slap_sasl_bind: rc=%d\n", rc ));
1073 #else
1074         Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1075 #endif
1076
1077
1078 #else
1079         send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1080                 NULL, "SASL not supported", NULL, NULL );
1081 #endif
1082
1083         return rc;
1084 }
1085
1086 char* slap_sasl_secprops( const char *in )
1087 {
1088 #ifdef HAVE_CYRUS_SASL
1089         int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1090
1091         return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1092 #else
1093         return "SASL not supported";
1094 #endif
1095 }