]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/sasl.c
fix previous commit
[openldap] / servers / slapd / sasl.c
index 1dcd8bbe95930ceb5f7283ad83f9bb332b095151..87fce497a5e848222180adb68ecc90b43bb3a21c 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2004 The OpenLDAP Foundation.
+ * Copyright 1998-2005 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 #include "portable.h"
 
 #include <stdio.h>
+#ifdef HAVE_LIMITS_H
+#include <limits.h>
+#endif
+
 #include <ac/stdlib.h>
 #include <ac/string.h>
 
@@ -24,8 +28,6 @@
 
 #include "slap.h"
 
-#include <limits.h>
-
 #ifdef HAVE_CYRUS_SASL
 # ifdef HAVE_SASL_SASL_H
 #  include <sasl/sasl.h>
@@ -64,116 +66,6 @@ typedef struct sasl_ctx {
 
 static struct berval ext_bv = BER_BVC( "EXTERNAL" );
 
-int slap_sasl_config( int cargc, char **cargv, char *line,
-       const char *fname, int lineno )
-{
-               /* set SASL proxy authorization policy */
-               if ( !strcasecmp( cargv[0], "authz-policy" ) ||
-                       !strcasecmp( cargv[0], "sasl-authz-policy" ))
-               {
-                       if ( cargc != 2 ) {
-                               Debug( LDAP_DEBUG_ANY,
-                                       "%s: line %d: missing policy in"
-                                       " \"%s <policy>\" line\n",
-                                   cargv[0], fname, lineno );
-
-                               return( 1 );
-                       }
-                       if ( slap_sasl_setpolicy( cargv[1] ) ) {
-                               Debug( LDAP_DEBUG_ANY, "%s: line %d: "
-                                       "unable to parse value \"%s\" in \"authz-policy "
-                                       "<policy>\" line.\n",
-                                       fname, lineno, cargv[1] );
-                               return( 1 );
-                       }
-
-               } else if ( !strcasecmp( cargv[0], "authz-regexp" ) || 
-                       !strcasecmp( cargv[0], "sasl-regexp" ) ||
-                       !strcasecmp( cargv[0], "saslregexp" ) )
-               {
-                       int rc;
-                       if ( cargc != 3 ) {
-                               Debug( LDAP_DEBUG_ANY, 
-                                       "%s: line %d: need 2 args in "
-                                       "\"authz-regexp <match> <replace>\"\n",
-                                       fname, lineno, 0 );
-
-                               return( 1 );
-                       }
-                       rc = slap_sasl_regexp_config( cargv[1], cargv[2] );
-                       if ( rc ) {
-                               return rc;
-                       }
-
-#ifdef HAVE_CYRUS_SASL
-               /* set SASL host */
-               } else if ( strcasecmp( cargv[0], "sasl-host" ) == 0 ) {
-                       if ( cargc < 2 ) {
-                               Debug( LDAP_DEBUG_ANY,
-                               "%s: line %d: missing host in \"sasl-host <host>\" line\n",
-                                   fname, lineno, 0 );
-
-                               return( 1 );
-                       }
-
-                       if ( global_host != NULL ) {
-                               Debug( LDAP_DEBUG_ANY,
-                                       "%s: line %d: already set sasl-host!\n",
-                                       fname, lineno, 0 );
-
-                               return 1;
-
-                       } else {
-                               global_host = ch_strdup( cargv[1] );
-                       }
-
-               /* set SASL realm */
-               } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
-                       if ( cargc < 2 ) {
-                               Debug( LDAP_DEBUG_ANY, "%s: line %d: "
-                                       "missing realm in \"sasl-realm <realm>\" line.\n",
-                                   fname, lineno, 0 );
-
-                               return( 1 );
-                       }
-
-                       if ( global_realm != NULL ) {
-                               Debug( LDAP_DEBUG_ANY,
-                                       "%s: line %d: already set sasl-realm!\n",
-                                       fname, lineno, 0 );
-
-                               return 1;
-
-                       } else {
-                               global_realm = ch_strdup( cargv[1] );
-                       }
-
-               /* SASL security properties */
-               } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
-                       char *txt;
-
-                       if ( cargc < 2 ) {
-                               Debug( LDAP_DEBUG_ANY, "%s: line %d: "
-                                       "missing flags in \"sasl-secprops <properties>\" line\n",
-                                   fname, lineno, 0 );
-
-                               return 1;
-                       }
-
-                       txt = slap_sasl_secprops( cargv[1] );
-                       if ( txt != NULL ) {
-                               Debug( LDAP_DEBUG_ANY,
-                                       "%s: line %d: sasl-secprops: %s\n",
-                                   fname, lineno, txt );
-
-                               return 1;
-                       }
-#endif /* HAVE_CYRUS_SASL */
-           }
-
-           return LDAP_SUCCESS;
-}
-
 #ifdef HAVE_CYRUS_SASL
 
 int
@@ -491,28 +383,33 @@ slap_auxprop_store(
        }
        *modtail = NULL;
 
-       rc = slap_mods_check( modlist, 0, &text, textbuf, textlen, NULL );
-
-       if ( rc == LDAP_SUCCESS ) {
-               rc = slap_mods_opattrs( &op, modlist, modtail, &text, textbuf,
-                       textlen, 1 );
-       }
+       rc = slap_mods_check( modlist, &text, textbuf, textlen, NULL );
 
        if ( rc == LDAP_SUCCESS ) {
-               op.o_hdr = conn->c_sasl_bindop->o_hdr;
-               op.o_tag = LDAP_REQ_MODIFY;
-               op.o_ndn = op.o_req_ndn;
-               op.o_callback = &cb;
-               op.o_time = slap_get_time();
-               op.o_do_not_cache = 1;
-               op.o_is_auth_check = 1;
-               op.o_req_dn = op.o_req_ndn;
-               op.orm_modlist = modlist;
-
-               rc = op.o_bd->be_modify( &op, &rs );
+               rc = slap_mods_no_user_mod_check( &op, modlist,
+                       &text, textbuf, textlen );
+
+               if ( rc == LDAP_SUCCESS ) {
+                       rc = slap_mods_opattrs( &op, modlist, modtail,
+                                       &text, textbuf, textlen, 1 );
+
+                       if ( rc == LDAP_SUCCESS ) {
+                               op.o_hdr = conn->c_sasl_bindop->o_hdr;
+                               op.o_tag = LDAP_REQ_MODIFY;
+                               op.o_ndn = op.o_req_ndn;
+                               op.o_callback = &cb;
+                               op.o_time = slap_get_time();
+                               op.o_do_not_cache = 1;
+                               op.o_is_auth_check = 1;
+                               op.o_req_dn = op.o_req_ndn;
+                               op.orm_modlist = modlist;
+
+                               rc = op.o_bd->be_modify( &op, &rs );
+                       }
+               }
        }
        slap_mods_free( modlist );
-       return rc ? SASL_FAIL : SASL_OK;
+       return rc != LDAP_SUCCESS ? SASL_FAIL : SASL_OK;
 }
 #endif /* SASL_VERSION_FULL >= 2.1.16 */
 
@@ -681,7 +578,7 @@ slap_sasl_authorize(
 {
        Connection *conn = (Connection *)context;
        struct propval auxvals[3];
-       struct berval authcDN, authzDN;
+       struct berval authcDN, authzDN=BER_BVNULL;
        int rc;
 
        /* Simple Binds don't support proxy authorization, ignore it */
@@ -706,17 +603,16 @@ slap_sasl_authorize(
        }
 
        AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
+       conn->c_sasl_dn = authcDN;
 
        /* Nothing to do if no authzID was given */
        if ( !auxvals[1].name || !auxvals[1].values ) {
-               conn->c_sasl_dn = authcDN;
                goto ok;
        }
        
        AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
 
        rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
-       ch_free( authcDN.bv_val );
        if ( rc != LDAP_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
                        "proxy authorization disallowed (%d)\n",
@@ -727,18 +623,19 @@ slap_sasl_authorize(
                return SASL_NOAUTHZ;
        }
 
-       conn->c_sasl_dn = authzDN;
+       conn->c_sasl_authz_dn = authzDN;
 ok:
        if (conn->c_sasl_bindop) {
                Statslog( LDAP_DEBUG_STATS,
-                       "conn=%lu op=%lu BIND authcid=\"%s\"\n",
+                       "conn=%lu op=%lu BIND authcid=\"%s\" authzid=\"%s\"\n",
                        conn->c_connid, conn->c_sasl_bindop->o_opid, 
-                       auth_identity, 0, 0);
+                       auth_identity, requested_user, 0);
        }
 
        Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
-               " proxy authorization allowed\n",
-               (long) (conn ? conn->c_connid : -1), 0, 0 );
+               " proxy authorization allowed authzDN=\"%s\"\n",
+               (long) (conn ? conn->c_connid : -1), 
+               authzDN.bv_val ? authzDN.bv_val : "", 0 );
        return SASL_OK;
 } 
 #else
@@ -787,11 +684,11 @@ slap_sasl_authorize(
                *errstr = ldap_err2string( rc );
                return SASL_NOAUTHZ;
        }
+       conn->c_sasl_dn = authcDN;
        if( ( authzid == NULL ) || !strcmp( authcid, authzid ) ) {
                Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
                 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
 
-               conn->c_sasl_dn = authcDN;
                goto ok;
        }
 
@@ -800,13 +697,11 @@ slap_sasl_authorize(
        rc = slap_sasl_getdn( conn, NULL, &bvauthzid, realm,
                &authzDN, SLAP_GETDN_AUTHZID );
        if( rc != LDAP_SUCCESS ) {
-               ch_free( authcDN.bv_val );
                *errstr = ldap_err2string( rc );
                return SASL_NOAUTHZ;
        }
 
        rc = slap_sasl_authorized(conn->c_sasl_bindop, &authcDN, &authzDN );
-       ch_free( authcDN.bv_val );
        if( rc ) {
                Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
                        "proxy authorization disallowed (%d)\n",
@@ -816,18 +711,19 @@ slap_sasl_authorize(
                ch_free( authzDN.bv_val );
                return SASL_NOAUTHZ;
        }
-       conn->c_sasl_dn = authzDN;
+       conn->c_sasl_authz_dn = authzDN;
 
 ok:
        Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
-               " authorization allowed\n",
-               (long) (conn ? conn->c_connid : -1), 0, 0 );
+               " authorization allowed authzDN=\"%s\"\n",
+               (long) (conn ? conn->c_connid : -1),
+               authzDN.bv_val ? authzDN.bv_val : "", 0 );
 
        if (conn->c_sasl_bindop) {
                Statslog( LDAP_DEBUG_STATS,
-                       "conn=%lu op=%lu BIND authcid=\"%s\"\n",
+                       "conn=%lu op=%lu BIND authcid=\"%s\" authzid=\"%s\"\n",
                        conn->c_connid, conn->c_sasl_bindop->o_opid, 
-                       authcid, 0, 0);
+                       authcid, authzid ? authzid : "", 0);
        }
 
        *errstr = NULL;
@@ -873,7 +769,66 @@ slap_sasl_err2ldap( int saslerr )
 
        return rc;
 }
-#endif
+
+#ifdef SLAPD_SPASSWD
+
+static struct berval sasl_pwscheme = BER_BVC("{SASL}");
+
+static int chk_sasl(
+       const struct berval *sc,
+       const struct berval * passwd,
+       const struct berval * cred,
+       const char **text )
+{
+       unsigned int i;
+       int rtn;
+       void *ctx, *sconn = NULL;
+
+       for( i=0; i<cred->bv_len; i++) {
+               if(cred->bv_val[i] == '\0') {
+                       return LUTIL_PASSWD_ERR;        /* NUL character in password */
+               }
+       }
+
+       if( cred->bv_val[i] != '\0' ) {
+               return LUTIL_PASSWD_ERR;        /* cred must behave like a string */
+       }
+
+       for( i=0; i<passwd->bv_len; i++) {
+               if(passwd->bv_val[i] == '\0') {
+                       return LUTIL_PASSWD_ERR;        /* NUL character in password */
+               }
+       }
+
+       if( passwd->bv_val[i] != '\0' ) {
+               return LUTIL_PASSWD_ERR;        /* passwd must behave like a string */
+       }
+
+       rtn = LUTIL_PASSWD_ERR;
+
+       ctx = ldap_pvt_thread_pool_context();
+       ldap_pvt_thread_pool_getkey( ctx, slap_sasl_bind, &sconn, NULL );
+
+       if( sconn != NULL ) {
+               int sc;
+# if SASL_VERSION_MAJOR < 2
+               sc = sasl_checkpass( sconn,
+                       passwd->bv_val, passwd->bv_len,
+                       cred->bv_val, cred->bv_len,
+                       text );
+# else
+               sc = sasl_checkpass( sconn,
+                       passwd->bv_val, passwd->bv_len,
+                       cred->bv_val, cred->bv_len );
+# endif
+               rtn = ( sc != SASL_OK ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
+       }
+
+       return rtn;
+}
+#endif /* SLAPD_SPASSWD */
+
+#endif /* HAVE_CYRUS_SASL */
 
 int slap_sasl_init( void )
 {
@@ -945,6 +900,10 @@ int slap_sasl_init( void )
                return -1;
        }
 
+#ifdef SLAPD_SPASSWD
+       lutil_passwd_add( &sasl_pwscheme, chk_sasl, NULL );
+#endif
+
        Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
                0, 0, 0 );
 
@@ -1436,6 +1395,13 @@ char* slap_sasl_secprops( const char *in )
 #endif
 }
 
+void slap_sasl_secprops_unparse( struct berval *bv )
+{
+#ifdef HAVE_CYRUS_SASL
+       ldap_pvt_sasl_secprops_unparse( &sasl_secprops, bv );
+#endif
+}
+
 #ifdef HAVE_CYRUS_SASL
 int
 slap_sasl_setpass( Operation *op, SlapReply *rs )
@@ -1609,7 +1575,7 @@ int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
                irdn = 0;
                DN[ irdn ] = RDNs[ irdn ];
                RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
-               BER_BVSTR( &AVAs[ irdn ].la_attr, "uid" );
+               AVAs[ irdn ].la_attr = slap_schema.si_ad_uid->ad_cname;
                AVAs[ irdn ].la_value = *dn;
                AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
                AVAs[ irdn ].la_private = NULL;
@@ -1619,7 +1585,7 @@ int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
                        irdn++;
                        DN[ irdn ] = RDNs[ irdn ];
                        RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
-                       BER_BVSTR( &AVAs[ irdn ].la_attr, "cn" );
+                       AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
                        ber_str2bv( user_realm, 0, 0, &AVAs[ irdn ].la_value );
                        AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
                        AVAs[ irdn ].la_private = NULL;
@@ -1630,7 +1596,7 @@ int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
                        irdn++;
                        DN[ irdn ] = RDNs[ irdn ];
                        RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
-                       BER_BVSTR( &AVAs[ irdn ].la_attr, "cn" );
+                       AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
                        AVAs[ irdn ].la_value = *mech;
                        AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
                        AVAs[ irdn ].la_private = NULL;
@@ -1640,7 +1606,7 @@ int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
                irdn++;
                DN[ irdn ] = RDNs[ irdn ];
                RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
-               BER_BVSTR( &AVAs[ irdn ].la_attr, "cn" );
+               AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
                BER_BVSTR( &AVAs[ irdn ].la_value, "auth" );
                AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
                AVAs[ irdn ].la_private = NULL;