]> git.sur5r.net Git - openldap/blobdiff - libraries/liblutil/passwd.c
Framework for back-bdb
[openldap] / libraries / liblutil / passwd.c
index 85511c0875e8712a05a42385bcd26db249022772..427af165a02780c2529efda6dd0abc084ab22bb2 100644 (file)
@@ -1,13 +1,16 @@
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 /*
- * lutil_password(credentials, password)
+ * int lutil_passwd(
+ *     const struct berval *passwd,
+ *     const struct berval *cred,
+ *     const char **schemes )
  *
- * Returns true if user supplied credentials matches
- * the stored password. 
+ * Returns true if user supplied credentials (cred) matches
+ * the stored password (passwd)
  *
  * Due to the use of the crypt(3) function 
  * this routine is NOT thread-safe.
 #include <ac/stdlib.h>
 #include <ac/string.h>
 
+#ifdef SLAPD_SPASSWD
+#      include <sasl.h>
+#endif
+
 #ifdef SLAPD_KPASSWD
 #      include <ac/krb.h>
 #      include <ac/krb5.h>
 #ifdef HAVE_PWD_H
 #      include <pwd.h>
 #endif
+#ifdef HAVE_AIX_SECURITY
+#      include <userpw.h>
+#endif
 
 #include <lber.h>
 
+#include "ldap_pvt.h"
+
 #include "lutil_md5.h"
 #include "lutil_sha1.h"
 #include "lutil.h"
@@ -83,20 +95,33 @@ static int chk_sha1(
        const struct berval *passwd,
        const struct berval *cred );
 
+#ifdef SLAPD_SPASSWD
+static int chk_sasl(
+       const struct pw_scheme *scheme,
+       const struct berval *passwd,
+       const struct berval *cred );
+#endif
+
+#ifdef SLAPD_KPASSWD
 static int chk_kerberos(
        const struct pw_scheme *scheme,
        const struct berval *passwd,
        const struct berval *cred );
+#endif
 
+#ifdef SLAPD_CRYPT
 static int chk_crypt(
        const struct pw_scheme *scheme,
        const struct berval *passwd,
        const struct berval *cred );
 
+#if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
 static int chk_unix(
        const struct pw_scheme *scheme,
        const struct berval *passwd,
        const struct berval *cred );
+#endif
+#endif
 
 
 /* password hash routines */
@@ -116,9 +141,11 @@ static struct berval *hash_md5(
        const struct pw_scheme *scheme,
        const struct berval *passwd );
 
+#ifdef SLAPD_CRYPT
 static struct berval *hash_crypt(
        const struct pw_scheme *scheme,
        const struct berval *passwd );
+#endif
 
 
 static const struct pw_scheme pw_schemes[] =
@@ -129,16 +156,19 @@ static const struct pw_scheme pw_schemes[] =
        { {sizeof("{SMD5}")-1, "{SMD5}"},       chk_smd5, hash_smd5 },
        { {sizeof("{MD5}")-1, "{MD5}"},         chk_md5, hash_md5 },
 
+#ifdef SLAPD_SPASSWD
+       { {sizeof("{SASL}")-1, "{SASL}"}, chk_sasl, NULL },
+#endif
+
 #ifdef SLAPD_KPASSWD
        { {sizeof("{KERBEROS}")-1, "{KERBEROS}"}, chk_kerberos, NULL },
 #endif
 
 #ifdef SLAPD_CRYPT
        { {sizeof("{CRYPT}")-1, "{CRYPT}"},     chk_crypt, hash_crypt },
-#endif
-# if defined( HAVE_GETSPNAM ) \
-  || ( defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD ) )
+# if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
        { {sizeof("{UNIX}")-1, "{UNIX}"},       chk_unix, NULL },
+# endif
 #endif
 
 #ifdef SLAPD_CLEARTEXT
@@ -260,10 +290,8 @@ lutil_passwd(
                        ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
                        : 1;
        }
-#else
-       return 1;
 #endif
-
+       return 1;
 }
 
 struct berval * lutil_passwd_generate( ber_len_t len )
@@ -325,8 +353,8 @@ static struct berval * pw_string(
                return NULL;
        }
 
-       memcpy( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
-       memcpy( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
+       AC_MEMCPY( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
+       AC_MEMCPY( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
 
        pw->bv_val[pw->bv_len] = '\0';
        return pw;
@@ -354,9 +382,9 @@ static struct berval * pw_string64(
                        return NULL;
                }
 
-               memcpy( string.bv_val, hash->bv_val,
+               AC_MEMCPY( string.bv_val, hash->bv_val,
                        hash->bv_len );
-               memcpy( &string.bv_val[hash->bv_len], salt->bv_val,
+               AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
                        salt->bv_len );
                string.bv_val[string.bv_len] = '\0';
 
@@ -374,21 +402,22 @@ static struct berval * pw_string64(
                return NULL;
        }
 
-       memcpy(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
+       AC_MEMCPY(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
 
        rc = lutil_b64_ntop(
                string.bv_val, string.bv_len,
                &b64->bv_val[sc->name.bv_len], b64len );
 
-       b64->bv_val[b64->bv_len] = '\0';
-
        if( salt ) ber_memfree( string.bv_val );
-
+       
        if( rc < 0 ) {
                ber_bvfree( b64 );
                return NULL;
        }
 
+       /* recompute length */
+       b64->bv_len = sc->name.bv_len + rc;
+       assert( strlen(b64->bv_val) == b64->bv_len );
        return b64;
 }
 
@@ -404,7 +433,7 @@ static int chk_ssha1(
        int rc;
        unsigned char *orig_pass = NULL;
  
-       /* base64 un-encode password */
+       /* decode base64 password */
        orig_pass = (unsigned char *) ber_memalloc( (size_t) (
                LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
 
@@ -414,7 +443,7 @@ static int chk_ssha1(
 
        if(rc < 0) {
                ber_memfree(orig_pass);
-               return 1;
+               return -1;
        }
  
        /* hash credentials with salt */
@@ -429,7 +458,7 @@ static int chk_ssha1(
        /* compare */
        rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
        ber_memfree(orig_pass);
-       return rc;
+       return rc ? 1 : 0;
 }
 
 static int chk_sha1(
@@ -452,7 +481,7 @@ static int chk_sha1(
 
        if( rc != sizeof(SHA1digest) ) {
                ber_memfree(orig_pass);
-               return 1;
+               return -1;
        }
  
        /* hash credentials with salt */
@@ -464,7 +493,7 @@ static int chk_sha1(
        /* compare */
        rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
        ber_memfree(orig_pass);
-       return rc;
+       return rc ? 1 : 0;
 }
 
 static int chk_smd5(
@@ -486,7 +515,7 @@ static int chk_smd5(
        rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
        if ( rc < 0 ) {
                ber_memfree(orig_pass);
-               return 1;
+               return -1;
        }
 
        /* hash credentials with salt */
@@ -501,7 +530,7 @@ static int chk_smd5(
        /* compare */
        rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
        ber_memfree(orig_pass);
-       return rc;
+       return rc ? 1 : 0;
 }
 
 static int chk_md5(
@@ -523,7 +552,7 @@ static int chk_md5(
        rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
        if ( rc != sizeof(MD5digest) ) {
                ber_memfree(orig_pass);
-               return 1;
+               return -1;
        }
 
        /* hash credentials with salt */
@@ -535,8 +564,61 @@ static int chk_md5(
        /* compare */
        rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
        ber_memfree(orig_pass);
-       return rc;
+       return rc ? 1 : 0;
+}
+
+#ifdef SLAPD_SPASSWD
+#ifdef HAVE_CYRUS_SASL
+sasl_conn_t *lutil_passwd_sasl_conn = NULL;
+#endif
+
+static int chk_sasl(
+       const struct pw_scheme *sc,
+       const struct berval * passwd,
+       const struct berval * cred )
+{
+       int i;
+       int rtn;
+
+       for( i=0; i<cred->bv_len; i++) {
+               if(cred->bv_val[i] == '\0') {
+                       return 1;       /* NUL character in password */
+               }
+       }
+
+       if( cred->bv_val[i] != '\0' ) {
+               return 1;       /* cred must behave like a string */
+       }
+
+       for( i=0; i<passwd->bv_len; i++) {
+               if(passwd->bv_val[i] == '\0') {
+                       return 1;       /* NUL character in password */
+               }
+       }
+
+       if( passwd->bv_val[i] != '\0' ) {
+               return 1;       /* passwd must behave like a string */
+       }
+
+       rtn = 1;
+
+#ifdef HAVE_CYRUS_SASL
+       if( lutil_passwd_sasl_conn != NULL ) {
+               const char *errstr = NULL;
+               int sc;
+
+               sc = sasl_checkpass( lutil_passwd_sasl_conn,
+                       passwd->bv_val, passwd->bv_len,
+                       cred->bv_val, cred->bv_len,
+                       &errstr );
+
+               rtn = ( sc != SASL_OK );
+       }
+#endif
+
+       return rtn;
 }
+#endif
 
 #ifdef SLAPD_KPASSWD
 static int chk_kerberos(
@@ -646,9 +728,9 @@ static int chk_kerberos(
                }
 
                {
-                       char host[MAXHOSTNAMELEN];
+                       char *host = ldap_pvt_get_fqdn( NULL );
 
-                       if( gethostname( host, MAXHOSTNAMELEN ) != 0 ) {
+                       if( host == NULL ) {
                                krb5_free_principal( context, client );
                                krb5_free_context( context );
                                return 1;
@@ -656,6 +738,8 @@ static int chk_kerberos(
 
                        ret = krb5_sname_to_principal( context,
                                host, "ldap", KRB5_NT_SRV_HST, &server );
+
+                       ber_memfree( host );
                }
 
                if (ret) {
@@ -669,7 +753,7 @@ static int chk_kerberos(
 
                krb5_free_principal( context, client );
                krb5_free_principal( context, server );
-               krb5_free_creds_contents( context, &creds );
+               krb5_free_cred_contents( context, &creds );
                krb5_free_context( context );
 
                rtn = !!ret;
@@ -725,35 +809,34 @@ static int chk_crypt(
        }
 
        if( cred->bv_val[i] != '\0' ) {
-               return 1;       /* cred must behave like a string */
+               return -1;      /* cred must behave like a string */
        }
 
        if( passwd->bv_len < 2 ) {
-               return 1;       /* passwd must be at least two characters long */
+               return -1;      /* passwd must be at least two characters long */
        }
 
        for( i=0; i<passwd->bv_len; i++) {
                if(passwd->bv_val[i] == '\0') {
-                       return 1;       /* NUL character in password */
+                       return -1;      /* NUL character in password */
                }
        }
 
        if( passwd->bv_val[i] != '\0' ) {
-               return 1;       /* passwd must behave like a string */
+               return -1;      /* passwd must behave like a string */
        }
 
        cr = crypt( cred->bv_val, passwd->bv_val );
 
        if( cr == NULL || cr[0] == '\0' ) {
                /* salt must have been invalid */
-               return 1;
+               return -1;
        }
 
-       return strcmp( passwd->bv_val, cr );
+       return strcmp( passwd->bv_val, cr ) ? 1 : 0;
 }
 
-# if defined( HAVE_GETSPNAM ) \
-  || ( defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD ) )
+# if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
 static int chk_unix(
        const struct pw_scheme *sc,
        const struct berval * passwd,
@@ -764,59 +847,64 @@ static int chk_unix(
 
        for( i=0; i<cred->bv_len; i++) {
                if(cred->bv_val[i] == '\0') {
-                       return 1;       /* NUL character in password */
+                       return -1;      /* NUL character in password */
                }
        }
        if( cred->bv_val[i] != '\0' ) {
-               return 1;       /* cred must behave like a string */
+               return -1;      /* cred must behave like a string */
        }
 
        for( i=0; i<passwd->bv_len; i++) {
                if(passwd->bv_val[i] == '\0') {
-                       return 1;       /* NUL character in password */
+                       return -1;      /* NUL character in password */
                }
        }
 
        if( passwd->bv_val[i] != '\0' ) {
-               return 1;       /* passwd must behave like a string */
+               return -1;      /* passwd must behave like a string */
        }
 
-#  ifdef HAVE_GETSPNAM
        {
-               struct spwd *spwd = getspnam(passwd->bv_val);
+               struct passwd *pwd = getpwnam(passwd->bv_val);
 
-               if(spwd == NULL) {
-                       return 1;       /* not found */
+               if(pwd == NULL) {
+                       return -1;      /* not found */
                }
 
-               pw = spwd->sp_pwdp;
+               pw = pwd->pw_passwd;
        }
-
-#  else
+#  ifdef HAVE_GETSPNAM
        {
-               struct passwd *pwd = getpwnam(passwd->bv_val);
+               struct spwd *spwd = getspnam(passwd->bv_val);
 
-               if(pwd == NULL) {
-                       return 1;       /* not found */
+               if(spwd != NULL) {
+                       pw = spwd->sp_pwdp;
                }
+       }
+#  endif
+#  ifdef HAVE_AIX_SECURITY
+       {
+               struct userpw *upw = getuserpw(passwd->bv_val);
 
-               pw = pwd->pw_passwd;
+               if (upw != NULL) {
+                       pw = upw->upw_passwd;
+               }
        }
 #  endif
 
        if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
                /* password must must be at least two characters long */
-               return 1;
+               return -1;
        }
 
        cr = crypt(cred->bv_val, pw);
 
        if( cr == NULL || cr[0] == '\0' ) {
                /* salt must have been invalid */
-               return 1;
+               return -1;
        }
 
-       return strcmp(pw, cr);
+       return strcmp(pw, cr) ? 1 : 0;
 
 }
 # endif
@@ -858,7 +946,7 @@ static struct berval *hash_sha1(
        const struct berval  *passwd )
 {
        lutil_SHA1_CTX  SHA1context;
-       unsigned char   SHA1digest[20];
+       unsigned char   SHA1digest[LUTIL_SHA1_BYTES];
        struct berval digest;
        digest.bv_val = SHA1digest;
        digest.bv_len = sizeof(SHA1digest);
@@ -876,7 +964,7 @@ static struct berval *hash_smd5(
        const struct berval  *passwd )
 {
        lutil_MD5_CTX   MD5context;
-       unsigned char   MD5digest[16];
+       unsigned char   MD5digest[LUTIL_MD5_BYTES];
        unsigned char   saltdata[4];
        struct berval digest;
        struct berval salt;
@@ -905,7 +993,7 @@ static struct berval *hash_md5(
        const struct berval  *passwd )
 {
        lutil_MD5_CTX   MD5context;
-       unsigned char   MD5digest[16];
+       unsigned char   MD5digest[LUTIL_MD5_BYTES];
 
        struct berval digest;