]> git.sur5r.net Git - openldap/blob - servers/slapd/sasl.c
a877f63d4000114d487cfe9fc33855669ca7e17a
[openldap] / servers / slapd / sasl.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <ac/stdlib.h>
10 #include <stdio.h>
11
12 #include "slap.h"
13 #include "proto-slap.h"
14
15 #include <lber.h>
16 #include <ldap_log.h>
17
18 #ifdef HAVE_CYRUS_SASL
19 #include <limits.h>
20 #include <sasl.h>
21
22 #include <ldap_pvt.h>
23
24 #ifdef SLAPD_SPASSWD
25 #include <lutil.h>
26 #endif
27
28 static char *sasl_host = NULL;
29 static sasl_security_properties_t sasl_secprops;
30
31
32 static int
33 sasl_cb_log(
34         void *context,
35         int priority,
36         const char *message) 
37 {
38         Connection *conn = context;
39         int level;
40         const char * label;
41
42         if ( message == NULL ) {
43                 return SASL_BADPARAM;
44         }
45
46         switch (priority) {
47         case SASL_LOG_ERR:
48                 level = LDAP_DEBUG_ANY;
49                 label = "Error";
50                 break;
51         case SASL_LOG_WARNING:
52                 level = LDAP_DEBUG_TRACE;
53                 label = "Warning";
54                 break;
55         case SASL_LOG_INFO:
56                 level = LDAP_DEBUG_TRACE;
57                 label = "Info";
58                 break;
59         default:
60                 return SASL_BADPARAM;
61         }
62
63         Debug( level, "SASL [conn=%d] %s: %s\n",
64                 conn ? conn->c_connid: -1,
65                 label, message );
66
67         return SASL_OK;
68 }
69
70 static int
71 slap_sasl_authorize(
72         void *context,
73         const char *authcid,
74         const char *authzid,
75         const char **user,
76         const char **errstr)
77 {
78         Connection *conn = context;
79         char *canon = NULL;
80
81         if ( authcid == NULL || *authcid == '\0' ) {
82                 *errstr = "empty authentication identity";
83
84                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
85                         "empty authentication identity\n",
86                         (long) (conn ? conn->c_connid : -1),
87                         0, 0 );
88                 return SASL_BADAUTH;
89         }
90
91         if ( authzid == NULL || *authzid == '\0' ||
92                 strcmp( authcid, authzid ) == 0 )
93         {
94                 size_t len = sizeof("u:") + strlen( authcid );
95                 canon = ch_malloc( len );
96                 strcpy( canon, "u:" );
97                 strcpy( &canon[sizeof("u:")-1], authcid );
98
99                 *user = canon;
100
101                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
102                         "\"%s\" as \"%s\"\n", 
103                         (long) (conn ? conn->c_connid : -1),
104                         authcid, canon );
105                 return SASL_OK;
106         }
107
108         Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
109                 "\"%s\" as \"%s\" disallowed. No policy.\n", 
110                 (long) (conn ? conn->c_connid : -1),
111                 authcid, authzid );
112
113         *errstr = "no proxy policy";
114     return SASL_BADAUTH;
115 }
116
117
118 static int
119 slap_sasl_err2ldap( int saslerr )
120 {
121         int rc;
122
123         switch (saslerr) {
124                 case SASL_CONTINUE:
125                         rc = LDAP_SASL_BIND_IN_PROGRESS;
126                         break;
127                 case SASL_FAIL:
128                         rc = LDAP_OTHER;
129                         break;
130                 case SASL_NOMEM:
131                         rc = LDAP_OTHER;
132                         break;
133                 case SASL_NOMECH:
134                         rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
135                         break;
136                 case SASL_BADAUTH:
137                         rc = LDAP_INVALID_CREDENTIALS;
138                         break;
139                 case SASL_NOAUTHZ:
140                         rc = LDAP_INSUFFICIENT_ACCESS;
141                         break;
142                 case SASL_TOOWEAK:
143                 case SASL_ENCRYPT:
144                         rc = LDAP_INAPPROPRIATE_AUTH;
145                         break;
146                 default:
147                         rc = LDAP_OTHER;
148                         break;
149         }
150
151         return rc;
152 }
153 #endif
154
155
156 int slap_sasl_init( void )
157 {
158 #ifdef HAVE_CYRUS_SASL
159         int rc;
160         sasl_conn_t *server = NULL;
161         static sasl_callback_t server_callbacks[] = {
162                 { SASL_CB_LOG, &sasl_cb_log, NULL },
163                 { SASL_CB_LIST_END, NULL, NULL }
164         };
165
166         sasl_set_alloc(
167                 ch_malloc,
168                 ch_calloc,
169                 ch_realloc,
170                 ch_free ); 
171
172         sasl_set_mutex(
173                 ldap_pvt_sasl_mutex_new,
174                 ldap_pvt_sasl_mutex_lock,
175                 ldap_pvt_sasl_mutex_unlock,
176                 ldap_pvt_sasl_mutex_dispose );
177
178         /* should provide callbacks for logging */
179         /* server name should be configurable */
180         rc = sasl_server_init( server_callbacks, "slapd" );
181
182         if( rc != SASL_OK ) {
183                 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
184                         0, 0, 0 );
185                 return -1;
186         }
187
188         if( sasl_host == NULL ) {
189                 static char hostname[MAXHOSTNAMELEN+1];
190
191                 if( gethostname( hostname, MAXHOSTNAMELEN ) == 0 ) {
192                         hostname[MAXHOSTNAMELEN] = '\0';
193                         sasl_host = hostname;
194                 }
195         }
196
197         Debug( LDAP_DEBUG_TRACE,
198                 "slap_sasl_init: %s initialized!\n",
199                 sasl_host, 0, 0 );
200
201         /* default security properties */
202         memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
203     sasl_secprops.max_ssf = UINT_MAX;
204     sasl_secprops.maxbufsize = 65536;
205     sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
206
207 #ifdef SLAPD_SPASSWD
208         lutil_passwd_sasl_conn = server;
209 #else
210         sasl_dispose( &server );
211 #endif
212
213 #endif
214         return 0;
215 }
216
217 int slap_sasl_destroy( void )
218 {
219 #ifdef HAVE_CYRUS_SASL
220 #ifdef SLAPD_SPASSWD
221         sasl_dispose( &lutil_passwd_sasl_conn );
222 #endif
223         sasl_done();
224 #endif
225         return 0;
226 }
227
228 int slap_sasl_open( Connection *conn )
229 {
230         int sc = LDAP_SUCCESS;
231
232 #ifdef HAVE_CYRUS_SASL
233         sasl_conn_t *ctx = NULL;
234         sasl_callback_t *session_callbacks =
235                 ch_calloc( 3, sizeof(sasl_callback_t));
236
237         session_callbacks[0].id = SASL_CB_LOG;
238         session_callbacks[0].proc = &sasl_cb_log;
239         session_callbacks[0].context = conn;
240
241         session_callbacks[1].id = SASL_CB_PROXY_POLICY;
242         session_callbacks[1].proc = &slap_sasl_authorize;
243         session_callbacks[1].context = conn;
244
245         session_callbacks[2].id = SASL_CB_LIST_END;
246         session_callbacks[2].proc = NULL;
247         session_callbacks[2].context = NULL;
248
249         /* create new SASL context */
250         sc = sasl_server_new( "ldap", sasl_host, global_realm,
251                 session_callbacks,
252 #ifdef LDAP_SASL_SECURITY_LAYER
253                 SASL_SECURITY_LAYER,
254 #else
255                 0,
256 #endif
257                 &ctx );
258
259
260         if( sc != SASL_OK ) {
261                 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
262                         sc, 0, 0 );
263                 return -1;
264         }
265
266         conn->c_sasl_context = ctx;
267
268         if( sc == SASL_OK ) {
269                 sc = sasl_setprop( ctx,
270                         SASL_SEC_PROPS, &sasl_secprops );
271
272                 if( sc != SASL_OK ) {
273                         Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
274                                 sc, 0, 0 );
275                         slap_sasl_close( conn );
276                         return -1;
277                 }
278         }
279
280         sc = slap_sasl_err2ldap( sc );
281 #endif
282         return sc;
283 }
284
285 int slap_sasl_external(
286         Connection *conn,
287         unsigned ssf,
288         char *auth_id )
289 {
290 #ifdef HAVE_CYRUS_SASL
291         int sc;
292         sasl_conn_t *ctx = conn->c_sasl_context;
293         sasl_external_properties_t extprops;
294
295         if ( ctx == NULL ) {
296                 return LDAP_UNAVAILABLE;
297         }
298
299         memset( &extprops, 0L, sizeof(extprops) );
300         extprops.ssf = ssf;
301         extprops.auth_id = auth_id;
302
303         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
304                 (void *) &extprops );
305
306         if ( sc != SASL_OK ) {
307                 return LDAP_OTHER;
308         }
309 #endif
310
311         return LDAP_SUCCESS;
312 }
313
314 int slap_sasl_reset( Connection *conn )
315 {
316 #ifdef HAVE_CYRUS_SASL
317         sasl_conn_t *ctx = conn->c_sasl_context;
318
319         if( ctx != NULL ) {
320         }
321 #endif
322         /* must return "anonymous" */
323         return LDAP_SUCCESS;
324 }
325
326 char ** slap_sasl_mechs( Connection *conn )
327 {
328         char **mechs = NULL;
329
330 #ifdef HAVE_CYRUS_SASL
331         sasl_conn_t *ctx = conn->c_sasl_context;
332
333         if( ctx != NULL ) {
334                 int sc;
335                 char *mechstr;
336
337                 sc = sasl_listmech( ctx,
338                         NULL, NULL, ",", NULL,
339                         &mechstr, NULL, NULL );
340
341                 if( sc != SASL_OK ) {
342                         Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
343                                 sc, 0, 0 );
344                         return NULL;
345                 }
346
347                 mechs = str2charray( mechstr, "," );
348
349                 ch_free( mechstr );
350         }
351 #endif
352
353         return mechs;
354 }
355
356 int slap_sasl_close( Connection *conn )
357 {
358 #ifdef HAVE_CYRUS_SASL
359         sasl_conn_t *ctx = conn->c_sasl_context;
360
361         if( ctx != NULL ) {
362                 sasl_dispose( &ctx );
363         }
364
365         conn->c_sasl_context = NULL;
366 #endif
367         return LDAP_SUCCESS;
368 }
369
370 int slap_sasl_bind(
371     Connection          *conn,
372     Operation           *op,  
373     const char          *dn,  
374     const char          *ndn,
375     const char          *mech,
376     struct berval       *cred,
377         char                            **edn )
378 {
379         int rc = 1;
380
381 #ifdef HAVE_CYRUS_SASL
382         sasl_conn_t *ctx = conn->c_sasl_context;
383         struct berval response;
384         unsigned reslen;
385         const char *errstr;
386         int sc;
387
388         Debug(LDAP_DEBUG_ARGS,
389                 "==> sasl_bind: dn=\"%s\" mech=%s cred->bv_len=%d\n",
390                 dn, mech, cred ? cred->bv_len : 0 );
391
392         if( ctx == NULL ) {
393                 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
394                         NULL, "SASL unavailable on this session", NULL, NULL );
395                 return rc;
396         }
397
398         if ( mech != NULL ) {
399                 sc = sasl_server_start( ctx,
400                         mech,
401                         cred->bv_val, cred->bv_len,
402                         (char **)&response.bv_val, &reslen, &errstr );
403
404         } else {
405                 sc = sasl_server_step( ctx,
406                         cred->bv_val, cred->bv_len,
407                         (char **)&response.bv_val, &reslen, &errstr );
408         }
409
410         response.bv_len = reslen;
411
412         if ( sc == SASL_OK ) {
413                 char *username = NULL;
414
415                 sc = sasl_getprop( ctx,
416                         SASL_USERNAME, (void **)&username );
417
418                 if ( sc != SASL_OK ) {
419                         Debug(LDAP_DEBUG_TRACE,
420                                 "slap_sasl_bind: getprop(USERNAME) failed!\n",
421                                 0, 0, 0);
422
423                         send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
424                                 NULL, "no SASL username", NULL, NULL );
425
426                 } else if ( username == NULL || *username == '\0' ) {
427                         Debug(LDAP_DEBUG_TRACE,
428                                 "slap_sasl_bind: getprop(USERNAME) returned NULL!\n",
429                                 0, 0, 0);
430
431                         send_ldap_result( conn, op, rc = LDAP_INSUFFICIENT_ACCESS,
432                                 NULL, "no SASL username", NULL, NULL );
433
434                 } else {
435                         char *realm = NULL;
436                         sasl_ssf_t *ssf = NULL;
437
438                         (void) sasl_getprop( ctx,
439                                 SASL_REALM, (void **)&realm );
440
441                         (void) sasl_getprop( ctx,
442                                 SASL_SSF, (void *)&ssf );
443
444                         Debug(LDAP_DEBUG_TRACE,
445                                 "slap_sasl_bind: username=\"%s\" realm=\"%s\" ssf=%lu\n",
446                                 username ? username : "",
447                                 realm ? realm : "",
448                                 (unsigned long) ( ssf ? *ssf : 0 ) );
449
450                         if( !strncasecmp( username, "anonymous", sizeof("anonyous")-1 ) &&
451                                 ( ( username[sizeof("anonymous")] == '\0' ) ||
452                                   ( username[sizeof("anonymous")] == '@' ) ) )
453                         {
454                                 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: anonymous\n",
455                                         0, 0, 0);
456
457                         } else {
458                                 *edn = ch_malloc( sizeof( "uid= + realm=" )
459                                         + ( username ? strlen( username ) : 0 )
460                                         + ( realm ? strlen( realm ) : 0 ) );
461
462                                 strcpy( *edn, "uid=" );
463                                 strcat( *edn, username );
464
465                                 if( realm && *realm ) {
466                                         strcat( *edn, " + realm=" );
467                                         strcat( *edn, realm );
468                                 }
469
470                                 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: authzdn: \"%s\"\n",
471                                         *edn, 0, 0);
472                         }
473
474                         send_ldap_sasl( conn, op, rc = LDAP_SUCCESS,
475                                 NULL, NULL, NULL, NULL, &response );
476                 }
477
478         } else if ( sc == SASL_CONTINUE ) {
479                 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
480                         NULL, NULL, NULL, NULL,  &response );
481
482         } else {
483                 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
484                         NULL, errstr, NULL, NULL );
485         }
486
487         Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
488
489 #else
490         send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
491                 NULL, "SASL not supported", NULL, NULL );
492 #endif
493
494         return rc;
495 }
496
497 char* slap_sasl_secprops( const char *in )
498 {
499 #ifdef HAVE_CYRUS_SASL
500         int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
501
502         return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
503 #else
504         return "SASL not supported";
505 #endif
506 }
507