]> git.sur5r.net Git - openldap/blob - servers/slapd/sasl.c
6e46d65fe3572308064423f935ce550b87409485
[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                 static char hostname[MAXHOSTNAMELEN+1];
200
201                 if( gethostname( hostname, MAXHOSTNAMELEN ) == 0 ) {
202                         hostname[MAXHOSTNAMELEN] = '\0';
203                         sasl_host = hostname;
204                 }
205         }
206
207         Debug( LDAP_DEBUG_TRACE,
208                 "slap_sasl_init: %s initialized!\n",
209                 sasl_host, 0, 0 );
210
211         /* default security properties */
212         memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
213     sasl_secprops.max_ssf = UINT_MAX;
214     sasl_secprops.maxbufsize = 65536;
215     sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
216
217 #ifdef SLAPD_SPASSWD
218         lutil_passwd_sasl_conn = server;
219 #else
220         sasl_dispose( &server );
221 #endif
222
223 #endif
224         return 0;
225 }
226
227 int slap_sasl_destroy( void )
228 {
229 #ifdef HAVE_CYRUS_SASL
230 #ifdef SLAPD_SPASSWD
231         sasl_dispose( &lutil_passwd_sasl_conn );
232 #endif
233         sasl_done();
234 #endif
235         return 0;
236 }
237
238 int slap_sasl_open( Connection *conn )
239 {
240         int sc = LDAP_SUCCESS;
241
242 #ifdef HAVE_CYRUS_SASL
243         sasl_conn_t *ctx = NULL;
244         sasl_callback_t *session_callbacks;
245
246         assert( conn->c_sasl_context == NULL );
247         assert( conn->c_sasl_extra == NULL );
248
249         conn->c_sasl_layers = 0;
250
251         session_callbacks =
252                 ch_calloc( 3, sizeof(sasl_callback_t));
253         conn->c_sasl_extra = session_callbacks;
254
255         session_callbacks[0].id = SASL_CB_LOG;
256         session_callbacks[0].proc = &slap_sasl_log;
257         session_callbacks[0].context = conn;
258
259         session_callbacks[1].id = SASL_CB_PROXY_POLICY;
260         session_callbacks[1].proc = &slap_sasl_authorize;
261         session_callbacks[1].context = conn;
262
263         session_callbacks[2].id = SASL_CB_LIST_END;
264         session_callbacks[2].proc = NULL;
265         session_callbacks[2].context = NULL;
266
267         /* create new SASL context */
268         sc = sasl_server_new( "ldap", sasl_host, global_realm,
269                 session_callbacks, SASL_SECURITY_LAYER, &ctx );
270
271         if( sc != SASL_OK ) {
272                 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
273                         sc, 0, 0 );
274                 return -1;
275         }
276
277         conn->c_sasl_context = ctx;
278
279         if( sc == SASL_OK ) {
280                 sc = sasl_setprop( ctx,
281                         SASL_SEC_PROPS, &sasl_secprops );
282
283                 if( sc != SASL_OK ) {
284                         Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
285                                 sc, 0, 0 );
286                         slap_sasl_close( conn );
287                         return -1;
288                 }
289         }
290
291         sc = slap_sasl_err2ldap( sc );
292 #endif
293         return sc;
294 }
295
296 int slap_sasl_external(
297         Connection *conn,
298         unsigned ssf,
299         char *auth_id )
300 {
301 #ifdef HAVE_CYRUS_SASL
302         int sc;
303         sasl_conn_t *ctx = conn->c_sasl_context;
304         sasl_external_properties_t extprops;
305
306         if ( ctx == NULL ) {
307                 return LDAP_UNAVAILABLE;
308         }
309
310         memset( &extprops, 0L, sizeof(extprops) );
311         extprops.ssf = ssf;
312         extprops.auth_id = auth_id;
313
314         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
315                 (void *) &extprops );
316
317         if ( sc != SASL_OK ) {
318                 return LDAP_OTHER;
319         }
320 #endif
321
322         return LDAP_SUCCESS;
323 }
324
325 int slap_sasl_reset( Connection *conn )
326 {
327 #ifdef HAVE_CYRUS_SASL
328         sasl_conn_t *ctx = conn->c_sasl_context;
329
330         if( ctx != NULL ) {
331         }
332 #endif
333         /* must return "anonymous" */
334         return LDAP_SUCCESS;
335 }
336
337 char ** slap_sasl_mechs( Connection *conn )
338 {
339         char **mechs = NULL;
340
341 #ifdef HAVE_CYRUS_SASL
342         sasl_conn_t *ctx = conn->c_sasl_context;
343
344         if( ctx != NULL ) {
345                 int sc;
346                 char *mechstr;
347
348                 sc = sasl_listmech( ctx,
349                         NULL, NULL, ",", NULL,
350                         &mechstr, NULL, NULL );
351
352                 if( sc != SASL_OK ) {
353                         Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
354                                 sc, 0, 0 );
355                         return NULL;
356                 }
357
358                 mechs = str2charray( mechstr, "," );
359
360                 ch_free( mechstr );
361         }
362 #endif
363
364         return mechs;
365 }
366
367 int slap_sasl_close( Connection *conn )
368 {
369 #ifdef HAVE_CYRUS_SASL
370         sasl_conn_t *ctx = conn->c_sasl_context;
371
372         if( ctx != NULL ) {
373                 sasl_dispose( &ctx );
374         }
375
376         conn->c_sasl_context = NULL;
377
378         free( conn->c_sasl_extra );
379         conn->c_sasl_extra = NULL;
380 #endif
381
382         return LDAP_SUCCESS;
383 }
384
385 int slap_sasl_bind(
386     Connection          *conn,
387     Operation           *op,  
388     const char          *dn,  
389     const char          *ndn,
390     const char          *mech,
391     struct berval       *cred,
392         char                            **edn,
393         unsigned long           *ssfp )
394 {
395         int rc = 1;
396
397 #ifdef HAVE_CYRUS_SASL
398         sasl_conn_t *ctx = conn->c_sasl_context;
399         struct berval response;
400         unsigned reslen;
401         const char *errstr;
402         int sc;
403
404         Debug(LDAP_DEBUG_ARGS,
405                 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%d\n",
406                 dn, mech ? mech : "<continuing>", cred ? cred->bv_len : 0 );
407
408         if( ctx == NULL ) {
409                 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
410                         NULL, "SASL unavailable on this session", NULL, NULL );
411                 return rc;
412         }
413
414         if ( mech != NULL ) {
415                 sc = sasl_server_start( ctx,
416                         mech,
417                         cred->bv_val, cred->bv_len,
418                         (char **)&response.bv_val, &reslen, &errstr );
419
420         } else {
421                 sc = sasl_server_step( ctx,
422                         cred->bv_val, cred->bv_len,
423                         (char **)&response.bv_val, &reslen, &errstr );
424         }
425
426         response.bv_len = reslen;
427
428         if ( sc == SASL_OK ) {
429                 char *username = NULL;
430
431                 sc = sasl_getprop( ctx,
432                         SASL_USERNAME, (void **)&username );
433
434                 if ( sc != SASL_OK ) {
435                         Debug(LDAP_DEBUG_TRACE,
436                                 "slap_sasl_bind: getprop(USERNAME) failed!\n",
437                                 0, 0, 0);
438
439                         send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
440                                 NULL, "no SASL username", NULL, NULL );
441
442                 } else if ( username == NULL || *username == '\0' ) {
443                         Debug(LDAP_DEBUG_TRACE,
444                                 "slap_sasl_bind: getprop(USERNAME) returned NULL!\n",
445                                 0, 0, 0);
446
447                         send_ldap_result( conn, op, rc = LDAP_INSUFFICIENT_ACCESS,
448                                 NULL, "no SASL username", NULL, NULL );
449
450                 } else {
451                         char *realm = NULL;
452                         sasl_ssf_t *ssf = NULL;
453
454                         (void) sasl_getprop( ctx,
455                                 SASL_REALM, (void **)&realm );
456
457                         (void) sasl_getprop( ctx,
458                                 SASL_SSF, (void *)&ssf );
459
460                         Debug(LDAP_DEBUG_TRACE,
461                                 "slap_sasl_bind: username=\"%s\" realm=\"%s\" ssf=%lu\n",
462                                 username ? username : "",
463                                 realm ? realm : "",
464                                 (unsigned long) ( ssf ? *ssf : 0 ) );
465
466                         *ssfp = ssf ? *ssf : 0;
467
468                         rc = LDAP_SUCCESS;
469
470                         if( username == NULL || (
471                                 !strncasecmp( username, "anonymous", sizeof("anonyous")-1 ) &&
472                                 ( ( username[sizeof("anonymous")] == '\0' ) ||
473                                   ( username[sizeof("anonymous")] == '@' ) ) ) )
474                         {
475                                 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: anonymous\n",
476                                         0, 0, 0);
477
478                         } else if ( username[0] == 'u' && username[1] == ':'
479                                 && username[2] != '\0'
480                                 && strpbrk( &username[2], "=,;\"\\") == NULL )
481                         {
482                                 *edn = ch_malloc( sizeof( "uid= + realm=" )
483                                         + strlen( &username[2] )
484                                         + ( realm ? strlen( realm ) : 0 ) );
485
486                                 strcpy( *edn, "uid=" );
487                                 strcat( *edn, &username[2] );
488
489                                 if( realm && *realm ) {
490                                         strcat( *edn, " + realm=" );
491                                         strcat( *edn, realm );
492                                 }
493
494                                 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: authzdn: \"%s\"\n",
495                                         *edn, 0, 0);
496
497                         } else {
498                                 rc = LDAP_INAPPROPRIATE_AUTH;
499                                 errstr = "authorization disallowed";
500                                 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: %s\n",
501                                         errstr, 0, 0);
502                         }
503
504                         if( rc == LDAP_SUCCESS ) {
505                                 send_ldap_sasl( conn, op, rc,
506                                         NULL, NULL, NULL, NULL,
507                                         response.bv_len ? &response : NULL );
508
509                         } else {
510                                 send_ldap_result( conn, op, rc,
511                                         NULL, errstr, NULL, NULL );
512                         }
513                 }
514
515         } else if ( sc == SASL_CONTINUE ) {
516                 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
517                         NULL, NULL, NULL, NULL, &response );
518
519         } else {
520                 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
521                         NULL, errstr, NULL, NULL );
522         }
523
524         Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
525
526 #else
527         send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
528                 NULL, "SASL not supported", NULL, NULL );
529 #endif
530
531         return rc;
532 }
533
534 char* slap_sasl_secprops( const char *in )
535 {
536 #ifdef HAVE_CYRUS_SASL
537         int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
538
539         return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
540 #else
541         return "SASL not supported";
542 #endif
543 }