]> git.sur5r.net Git - openldap/blob - servers/slapd/sasl.c
Minor typedef and other clean ups
[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 slap_sasl_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
80         *user = NULL;
81
82         if ( authcid == NULL || *authcid == '\0' ) {
83                 *errstr = "empty authentication identity";
84
85                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
86                         "empty authentication identity\n",
87                         (long) (conn ? conn->c_connid : -1),
88                         0, 0 );
89                 return SASL_BADAUTH;
90         }
91
92         Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
93                 "authcid=\"%s\" authzid=\"%s\"\n",
94                 (long) (conn ? conn->c_connid : -1),
95                 authcid ? authcid : "<empty>",
96                 authcid ? authcid : "<empty>" );
97
98         if ( authzid == NULL || *authzid == '\0' ||
99                 strcmp( authcid, authzid ) == 0 )
100         {
101                 char* cuser;
102                 size_t len = sizeof("u:") + strlen( authcid );
103
104                 cuser = ch_malloc( len );
105                 strcpy( cuser, "u:" );
106                 strcpy( &cuser[sizeof("u:")-1], authcid );
107
108                 *user = cuser;
109
110                 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
111                         "\"%s\" as \"%s\"\n", 
112                         (long) (conn ? conn->c_connid : -1),
113                         authcid, cuser );
114
115                 return SASL_OK;
116         }
117
118         Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
119                 "\"%s\" as \"%s\" disallowed. No policy.\n", 
120                 (long) (conn ? conn->c_connid : -1),
121                 authcid, authzid );
122
123         *errstr = "no proxy policy";
124     return SASL_NOAUTHZ;
125 }
126
127
128 static int
129 slap_sasl_err2ldap( int saslerr )
130 {
131         int rc;
132
133         switch (saslerr) {
134                 case SASL_CONTINUE:
135                         rc = LDAP_SASL_BIND_IN_PROGRESS;
136                         break;
137                 case SASL_FAIL:
138                         rc = LDAP_OTHER;
139                         break;
140                 case SASL_NOMEM:
141                         rc = LDAP_OTHER;
142                         break;
143                 case SASL_NOMECH:
144                         rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
145                         break;
146                 case SASL_BADAUTH:
147                         rc = LDAP_INVALID_CREDENTIALS;
148                         break;
149                 case SASL_NOAUTHZ:
150                         rc = LDAP_INSUFFICIENT_ACCESS;
151                         break;
152                 case SASL_TOOWEAK:
153                 case SASL_ENCRYPT:
154                         rc = LDAP_INAPPROPRIATE_AUTH;
155                         break;
156                 default:
157                         rc = LDAP_OTHER;
158                         break;
159         }
160
161         return rc;
162 }
163 #endif
164
165
166 int slap_sasl_init( void )
167 {
168 #ifdef HAVE_CYRUS_SASL
169         int rc;
170         sasl_conn_t *server = NULL;
171         static sasl_callback_t server_callbacks[] = {
172                 { SASL_CB_LOG, &slap_sasl_log, NULL },
173                 { SASL_CB_LIST_END, NULL, NULL }
174         };
175
176         sasl_set_alloc(
177                 ch_malloc,
178                 ch_calloc,
179                 ch_realloc,
180                 ch_free ); 
181
182         sasl_set_mutex(
183                 ldap_pvt_sasl_mutex_new,
184                 ldap_pvt_sasl_mutex_lock,
185                 ldap_pvt_sasl_mutex_unlock,
186                 ldap_pvt_sasl_mutex_dispose );
187
188         /* should provide callbacks for logging */
189         /* server name should be configurable */
190         rc = sasl_server_init( server_callbacks, "slapd" );
191
192         if( rc != SASL_OK ) {
193                 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
194                         0, 0, 0 );
195                 return -1;
196         }
197
198         if( sasl_host == NULL ) {
199                 sasl_host = ldap_pvt_get_fqdn( NULL );
200         }
201
202         Debug( LDAP_DEBUG_TRACE,
203                 "slap_sasl_init: %s initialized!\n",
204                 sasl_host, 0, 0 );
205
206         /* default security properties */
207         memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
208     sasl_secprops.max_ssf = INT_MAX;
209     sasl_secprops.maxbufsize = 65536;
210     sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
211
212 #ifdef SLAPD_SPASSWD
213         lutil_passwd_sasl_conn = server;
214 #else
215         sasl_dispose( &server );
216 #endif
217
218 #endif
219         return 0;
220 }
221
222 int slap_sasl_destroy( void )
223 {
224 #ifdef HAVE_CYRUS_SASL
225 #ifdef SLAPD_SPASSWD
226         sasl_dispose( &lutil_passwd_sasl_conn );
227 #endif
228         sasl_done();
229 #endif
230         return 0;
231 }
232
233 int slap_sasl_open( Connection *conn )
234 {
235         int sc = LDAP_SUCCESS;
236
237 #ifdef HAVE_CYRUS_SASL
238         sasl_conn_t *ctx = NULL;
239         sasl_callback_t *session_callbacks;
240
241         assert( conn->c_sasl_context == NULL );
242         assert( conn->c_sasl_extra == NULL );
243
244         conn->c_sasl_layers = 0;
245
246         session_callbacks =
247                 ch_calloc( 3, sizeof(sasl_callback_t));
248         conn->c_sasl_extra = session_callbacks;
249
250         session_callbacks[0].id = SASL_CB_LOG;
251         session_callbacks[0].proc = &slap_sasl_log;
252         session_callbacks[0].context = conn;
253
254         session_callbacks[1].id = SASL_CB_PROXY_POLICY;
255         session_callbacks[1].proc = &slap_sasl_authorize;
256         session_callbacks[1].context = conn;
257
258         session_callbacks[2].id = SASL_CB_LIST_END;
259         session_callbacks[2].proc = NULL;
260         session_callbacks[2].context = NULL;
261
262         /* create new SASL context */
263         sc = sasl_server_new( "ldap", sasl_host, global_realm,
264                 session_callbacks, SASL_SECURITY_LAYER, &ctx );
265
266         if( sc != SASL_OK ) {
267                 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
268                         sc, 0, 0 );
269                 return -1;
270         }
271
272         conn->c_sasl_context = ctx;
273
274         if( sc == SASL_OK ) {
275                 sc = sasl_setprop( ctx,
276                         SASL_SEC_PROPS, &sasl_secprops );
277
278                 if( sc != SASL_OK ) {
279                         Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
280                                 sc, 0, 0 );
281                         slap_sasl_close( conn );
282                         return -1;
283                 }
284         }
285
286         sc = slap_sasl_err2ldap( sc );
287 #endif
288         return sc;
289 }
290
291 int slap_sasl_external(
292         Connection *conn,
293         slap_ssf_t ssf,
294         char *auth_id )
295 {
296 #ifdef HAVE_CYRUS_SASL
297         int sc;
298         sasl_conn_t *ctx = conn->c_sasl_context;
299         sasl_external_properties_t extprops;
300
301         if ( ctx == NULL ) {
302                 return LDAP_UNAVAILABLE;
303         }
304
305         memset( &extprops, '\0', sizeof(extprops) );
306         extprops.ssf = ssf;
307         extprops.auth_id = auth_id;
308
309         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
310                 (void *) &extprops );
311
312         if ( sc != SASL_OK ) {
313                 return LDAP_OTHER;
314         }
315 #endif
316
317         return LDAP_SUCCESS;
318 }
319
320 int slap_sasl_reset( Connection *conn )
321 {
322 #ifdef HAVE_CYRUS_SASL
323         sasl_conn_t *ctx = conn->c_sasl_context;
324
325         if( ctx != NULL ) {
326         }
327 #endif
328         /* must return "anonymous" */
329         return LDAP_SUCCESS;
330 }
331
332 char ** slap_sasl_mechs( Connection *conn )
333 {
334         char **mechs = NULL;
335
336 #ifdef HAVE_CYRUS_SASL
337         sasl_conn_t *ctx = conn->c_sasl_context;
338
339         if( ctx != NULL ) {
340                 int sc;
341                 char *mechstr;
342
343                 sc = sasl_listmech( ctx,
344                         NULL, NULL, ",", NULL,
345                         &mechstr, NULL, NULL );
346
347                 if( sc != SASL_OK ) {
348                         Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
349                                 sc, 0, 0 );
350                         return NULL;
351                 }
352
353                 mechs = str2charray( mechstr, "," );
354
355                 ch_free( mechstr );
356         }
357 #endif
358
359         return mechs;
360 }
361
362 int slap_sasl_close( Connection *conn )
363 {
364 #ifdef HAVE_CYRUS_SASL
365         sasl_conn_t *ctx = conn->c_sasl_context;
366
367         if( ctx != NULL ) {
368                 sasl_dispose( &ctx );
369         }
370
371         conn->c_sasl_context = NULL;
372
373         free( conn->c_sasl_extra );
374         conn->c_sasl_extra = NULL;
375 #endif
376
377         return LDAP_SUCCESS;
378 }
379
380 int slap_sasl_bind(
381     Connection          *conn,
382     Operation           *op,  
383     const char          *dn,  
384     const char          *ndn,
385     const char          *mech,
386     struct berval       *cred,
387         char                            **edn,
388         slap_ssf_t                      *ssfp )
389 {
390         int rc = 1;
391
392 #ifdef HAVE_CYRUS_SASL
393         sasl_conn_t *ctx = conn->c_sasl_context;
394         struct berval response;
395         unsigned reslen;
396         const char *errstr;
397         int sc;
398
399         Debug(LDAP_DEBUG_ARGS,
400                 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%d\n",
401                 dn, mech ? mech : "<continuing>", cred ? cred->bv_len : 0 );
402
403         if( ctx == NULL ) {
404                 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
405                         NULL, "SASL unavailable on this session", NULL, NULL );
406                 return rc;
407         }
408
409         if ( mech != NULL ) {
410                 sc = sasl_server_start( ctx,
411                         mech,
412                         cred->bv_val, cred->bv_len,
413                         (char **)&response.bv_val, &reslen, &errstr );
414
415         } else {
416                 sc = sasl_server_step( ctx,
417                         cred->bv_val, cred->bv_len,
418                         (char **)&response.bv_val, &reslen, &errstr );
419         }
420
421         response.bv_len = reslen;
422
423         if ( sc == SASL_OK ) {
424                 char *username = NULL;
425
426                 sc = sasl_getprop( ctx,
427                         SASL_USERNAME, (void **)&username );
428
429                 if ( sc != SASL_OK ) {
430                         Debug(LDAP_DEBUG_TRACE,
431                                 "slap_sasl_bind: getprop(USERNAME) failed!\n",
432                                 0, 0, 0);
433
434                         send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
435                                 NULL, "no SASL username", NULL, NULL );
436
437                 } else if ( username == NULL || *username == '\0' ) {
438                         Debug(LDAP_DEBUG_TRACE,
439                                 "slap_sasl_bind: getprop(USERNAME) returned NULL!\n",
440                                 0, 0, 0);
441
442                         send_ldap_result( conn, op, rc = LDAP_INSUFFICIENT_ACCESS,
443                                 NULL, "no SASL username", NULL, NULL );
444
445                 } else {
446                         char *realm = NULL;
447                         sasl_ssf_t *ssf = NULL;
448
449                         (void) sasl_getprop( ctx,
450                                 SASL_REALM, (void **)&realm );
451
452                         (void) sasl_getprop( ctx,
453                                 SASL_SSF, (void *)&ssf );
454
455                         Debug(LDAP_DEBUG_TRACE,
456                                 "slap_sasl_bind: username=\"%s\" realm=\"%s\" ssf=%lu\n",
457                                 username ? username : "",
458                                 realm ? realm : "",
459                                 (unsigned long) ( ssf ? *ssf : 0 ) );
460
461                         *ssfp = ssf ? *ssf : 0;
462
463                         rc = LDAP_SUCCESS;
464
465                         if( username == NULL || (
466                                 !strncasecmp( username, "anonymous", sizeof("anonymous")-1 ) &&
467                                 ( username[sizeof("anonymous")-1] == '\0' ||
468                                   username[sizeof("anonymous")-1] == '@' ) ) )
469                         {
470                                 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: anonymous\n",
471                                         0, 0, 0);
472
473                         } else if ( username[0] == 'u' && username[1] == ':'
474                                 && username[2] != '\0'
475                                 && strpbrk( &username[2], "+=,;\"\\ \t") == NULL )
476                         {
477                                 *edn = ch_malloc( sizeof( "uid= + realm=" )
478                                         + strlen( &username[2] )
479                                         + ( realm ? strlen( realm ) : 0 ) );
480
481                                 strcpy( *edn, "uid=" );
482                                 strcat( *edn, &username[2] );
483
484                                 if( realm && *realm ) {
485                                         strcat( *edn, " + realm=" );
486                                         strcat( *edn, realm );
487                                 }
488
489                                 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: authzdn: \"%s\"\n",
490                                         *edn, 0, 0);
491
492                         } else {
493                                 rc = LDAP_INAPPROPRIATE_AUTH;
494                                 errstr = "authorization disallowed";
495                                 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: %s\n",
496                                         errstr, 0, 0);
497                         }
498
499                         if( rc == LDAP_SUCCESS ) {
500                                 send_ldap_sasl( conn, op, rc,
501                                         NULL, NULL, NULL, NULL,
502                                         response.bv_len ? &response : NULL );
503
504                         } else {
505                                 send_ldap_result( conn, op, rc,
506                                         NULL, errstr, NULL, NULL );
507                         }
508                 }
509
510         } else if ( sc == SASL_CONTINUE ) {
511                 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
512                         NULL, NULL, NULL, NULL, &response );
513
514         } else {
515                 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
516                         NULL, errstr, NULL, NULL );
517         }
518
519         Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
520
521 #else
522         send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
523                 NULL, "SASL not supported", NULL, NULL );
524 #endif
525
526         return rc;
527 }
528
529 char* slap_sasl_secprops( const char *in )
530 {
531 #ifdef HAVE_CYRUS_SASL
532         int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
533
534         return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
535 #else
536         return "SASL not supported";
537 #endif
538 }