]> git.sur5r.net Git - openldap/blob - servers/slapd/sasl.c
6ef2d1e278347b7cb9e92ef72691e79be37c03e3
[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
71 static int
72 slap_sasl_err2ldap( int saslerr )
73 {
74         int rc;
75
76         switch (saslerr) {
77                 case SASL_CONTINUE:
78                         rc = LDAP_SASL_BIND_IN_PROGRESS;
79                         break;
80                 case SASL_FAIL:
81                         rc = LDAP_OTHER;
82                         break;
83                 case SASL_NOMEM:
84                         rc = LDAP_OTHER;
85                         break;
86                 case SASL_NOMECH:
87                         rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
88                         break;
89                 case SASL_BADAUTH:
90                         rc = LDAP_INVALID_CREDENTIALS;
91                         break;
92                 case SASL_NOAUTHZ:
93                         rc = LDAP_INSUFFICIENT_ACCESS;
94                         break;
95                 case SASL_TOOWEAK:
96                 case SASL_ENCRYPT:
97                         rc = LDAP_INAPPROPRIATE_AUTH;
98                         break;
99                 default:
100                         rc = LDAP_OTHER;
101                         break;
102         }
103
104         return rc;
105 }
106 #endif
107
108
109 int slap_sasl_init( void )
110 {
111 #ifdef HAVE_CYRUS_SASL
112         int rc;
113         sasl_conn_t *server = NULL;
114         sasl_callback_t server_callbacks[] = {
115                 { SASL_CB_LOG, &sasl_cb_log, NULL },
116                 { SASL_CB_LIST_END, NULL, NULL }
117         };
118
119         sasl_set_alloc(
120                 ch_malloc,
121                 ch_calloc,
122                 ch_realloc,
123                 ch_free ); 
124
125         sasl_set_mutex(
126                 ldap_pvt_sasl_mutex_new,
127                 ldap_pvt_sasl_mutex_lock,
128                 ldap_pvt_sasl_mutex_unlock,
129                 ldap_pvt_sasl_mutex_dispose );
130
131         /* should provide callbacks for logging */
132         /* server name should be configurable */
133         rc = sasl_server_init( server_callbacks, "slapd" );
134
135         if( rc != SASL_OK ) {
136                 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
137                         0, 0, 0 );
138                 return -1;
139         }
140
141         if( sasl_host == NULL ) {
142                 static char hostname[MAXHOSTNAMELEN+1];
143
144                 if( gethostname( hostname, MAXHOSTNAMELEN ) == 0 ) {
145                         hostname[MAXHOSTNAMELEN] = '\0';
146                         sasl_host = hostname;
147                 }
148         }
149
150         Debug( LDAP_DEBUG_TRACE,
151                 "slap_sasl_init: %s initialized!\n",
152                 sasl_host, 0, 0 );
153
154         /* default security properties */
155         memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
156     sasl_secprops.max_ssf = UINT_MAX;
157     sasl_secprops.maxbufsize = 65536;
158     sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
159
160 #ifdef SLAPD_SPASSWD
161         lutil_passwd_sasl_conn = server;
162 #else
163         sasl_dispose( &server );
164 #endif
165
166 #endif
167         return 0;
168 }
169
170 int slap_sasl_destroy( void )
171 {
172 #ifdef HAVE_CYRUS_SASL
173 #ifdef SLAPD_SPASSWD
174         sasl_dispose( &lutil_passwd_sasl_conn );
175 #endif
176         sasl_done();
177 #endif
178         return 0;
179 }
180
181 int slap_sasl_open( Connection *conn )
182 {
183         int sc = LDAP_SUCCESS;
184
185 #ifdef HAVE_CYRUS_SASL
186         sasl_conn_t *ctx = NULL;
187         sasl_callback_t session_callbacks[] = {
188                 { SASL_CB_LOG, &sasl_cb_log, conn },
189                 { SASL_CB_LIST_END, NULL, NULL }
190         };
191
192         /* create new SASL context */
193         sc = sasl_server_new( "ldap", sasl_host, global_realm,
194                 session_callbacks,
195 #ifdef LDAP_SASL_SECURITY_LAYER
196                 SASL_SECURITY_LAYER,
197 #else
198                 0,
199 #endif
200                 &ctx );
201
202
203         if( sc != SASL_OK ) {
204                 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
205                         sc, 0, 0 );
206                 return -1;
207         }
208
209         conn->c_sasl_context = ctx;
210
211         if( sc == SASL_OK ) {
212                 sc = sasl_setprop( ctx,
213                         SASL_SEC_PROPS, &sasl_secprops );
214
215                 if( sc != SASL_OK ) {
216                         Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
217                                 sc, 0, 0 );
218                         slap_sasl_close( conn );
219                         return -1;
220                 }
221         }
222
223         sc = slap_sasl_err2ldap( sc );
224 #endif
225         return sc;
226 }
227
228 int slap_sasl_external(
229         Connection *conn,
230         unsigned ssf,
231         char *auth_id )
232 {
233 #ifdef HAVE_CYRUS_SASL
234         int sc;
235         sasl_conn_t *ctx = conn->c_sasl_context;
236         sasl_external_properties_t extprops;
237
238         if ( ctx == NULL ) {
239                 return LDAP_UNAVAILABLE;
240         }
241
242         memset( &extprops, 0L, sizeof(extprops) );
243         extprops.ssf = ssf;
244         extprops.auth_id = auth_id;
245
246         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
247                 (void *) &extprops );
248
249         if ( sc != SASL_OK ) {
250                 return LDAP_OTHER;
251         }
252 #endif
253
254         return LDAP_SUCCESS;
255 }
256
257 int slap_sasl_reset( Connection *conn )
258 {
259 #ifdef HAVE_CYRUS_SASL
260         sasl_conn_t *ctx = conn->c_sasl_context;
261
262         if( ctx != NULL ) {
263         }
264 #endif
265         /* must return "anonymous" */
266         return LDAP_SUCCESS;
267 }
268
269 char ** slap_sasl_mechs( Connection *conn )
270 {
271         char **mechs = NULL;
272
273 #ifdef HAVE_CYRUS_SASL
274         sasl_conn_t *ctx = conn->c_sasl_context;
275
276         if( ctx != NULL ) {
277                 int sc;
278                 char *mechstr;
279
280                 sc = sasl_listmech( ctx,
281                         NULL, NULL, ",", NULL,
282                         &mechstr, NULL, NULL );
283
284                 if( sc != SASL_OK ) {
285                         Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
286                                 sc, 0, 0 );
287                         return NULL;
288                 }
289
290                 mechs = str2charray( mechstr, "," );
291
292                 ch_free( mechstr );
293         }
294 #endif
295
296         return mechs;
297 }
298
299 int slap_sasl_close( Connection *conn )
300 {
301 #ifdef HAVE_CYRUS_SASL
302         sasl_conn_t *ctx = conn->c_sasl_context;
303
304         if( ctx != NULL ) {
305                 sasl_dispose( &ctx );
306         }
307
308         conn->c_sasl_context = NULL;
309 #endif
310         return LDAP_SUCCESS;
311 }
312
313 int slap_sasl_bind(
314     Connection          *conn,
315     Operation           *op,  
316     const char          *dn,  
317     const char          *ndn,
318     const char          *mech,
319     struct berval       *cred,
320         char                            **edn )
321 {
322         int rc = 1;
323
324 #ifdef HAVE_CYRUS_SASL
325         sasl_conn_t *ctx = conn->c_sasl_context;
326         struct berval response;
327         unsigned reslen;
328         const char *errstr;
329         int sc;
330
331         Debug(LDAP_DEBUG_ARGS,
332                 "==> sasl_bind: dn=\"%s\" mech=%s cred->bv_len=%d\n",
333                 dn, mech, cred ? cred->bv_len : 0 );
334
335         if( ctx == NULL ) {
336                 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
337                         NULL, "SASL unavailable on this session", NULL, NULL );
338                 return rc;
339         }
340
341         if ( mech != NULL ) {
342                 sc = sasl_server_start( ctx,
343                         mech,
344                         cred->bv_val, cred->bv_len,
345                         (char **)&response.bv_val, &reslen, &errstr );
346
347         } else {
348                 sc = sasl_server_step( ctx,
349                         cred->bv_val, cred->bv_len,
350                         (char **)&response.bv_val, &reslen, &errstr );
351         }
352
353         response.bv_len = reslen;
354
355         if ( sc == SASL_OK ) {
356                 char *username = NULL;
357
358                 sc = sasl_getprop( ctx,
359                         SASL_USERNAME, (void **)&username );
360
361                 if ( sc != SASL_OK ) {
362                         Debug(LDAP_DEBUG_TRACE,
363                                 "slap_sasl_bind: getprop(USERNAME) failed!\n",
364                                 0, 0, 0);
365
366                         send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
367                                 NULL, "no SASL username", NULL, NULL );
368
369                 } else if ( username == NULL || *username == '\0' ) {
370                         Debug(LDAP_DEBUG_TRACE,
371                                 "slap_sasl_bind: getprop(USERNAME) returned NULL!\n",
372                                 0, 0, 0);
373
374                         send_ldap_result( conn, op, rc = LDAP_INSUFFICIENT_ACCESS,
375                                 NULL, "no SASL username", NULL, NULL );
376
377                 } else {
378                         char *realm = NULL;
379                         sasl_ssf_t ssf = 0;
380
381                         (void) sasl_getprop( ctx,
382                                 SASL_REALM, (void **)&realm );
383
384                         (void) sasl_getprop( ctx,
385                                 SASL_SSF, (void *)&ssf );
386
387                         Debug(LDAP_DEBUG_TRACE,
388                                 "slap_sasl_bind: username=\"%s\" realm=\"%s\" ssf=%lu\n",
389                                 username ? username : "",
390                                 realm ? realm : "",
391                                 (unsigned long) ssf );
392
393                         if( !strncasecmp( username, "anonymous", sizeof("anonyous")-1 ) &&
394                                 ( ( username[sizeof("anonymous")] == '\0' ) ||
395                                   ( username[sizeof("anonymous")] == '@' ) ) )
396                         {
397                                 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: anonymous\n",
398                                         0, 0, 0);
399
400                         } else {
401                                 *edn = ch_malloc( sizeof( "uid= + realm=" )
402                                         + ( username ? strlen( username ) : 0 )
403                                         + ( realm ? strlen( realm ) : 0 ) );
404
405                                 strcpy( *edn, "uid=" );
406                                 strcat( *edn, username );
407
408                                 if( realm && *realm ) {
409                                         strcat( *edn, " + realm=" );
410                                         strcat( *edn, realm );
411                                 }
412
413                                 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: authzdn: \"%s\"\n",
414                                         *edn, 0, 0);
415                         }
416
417                         send_ldap_sasl( conn, op, rc = LDAP_SUCCESS,
418                                 NULL, NULL, NULL, NULL, &response );
419                 }
420
421         } else if ( sc == SASL_CONTINUE ) {
422                 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
423                         NULL, NULL, NULL, NULL,  &response );
424
425         } else {
426                 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
427                         NULL, errstr, NULL, NULL );
428         }
429
430         Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
431
432 #else
433         send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
434                 NULL, "SASL not supported", NULL, NULL );
435 #endif
436
437         return rc;
438 }
439
440 char* slap_sasl_secprops( const char *in )
441 {
442 #ifdef HAVE_CYRUS_SASL
443         int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
444
445         return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
446 #else
447         return "SASL not supported";
448 #endif
449 }
450