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