Import Debian changes 0.69.0-2
[hcoop/debian/courier-authlib.git] / checkpassword.c
index b2d8afa..9f7896a 100644 (file)
@@ -16,7 +16,6 @@
 #include       "auth.h"
 #include       "courierauthdebug.h"
 
-static const char rcsid[]="$Id: checkpassword.c,v 1.17 2008/07/10 02:43:55 mrsam Exp $";
 
 #if HAVE_CRYPT
 #if NEED_CRYPT_PROTOTYPE
@@ -27,8 +26,16 @@ extern char *crypt(const char *, const char *);
 extern int authcheckpasswordmd5(const char *, const char *);
 extern int authcheckpasswordsha1(const char *, const char *);
 
+static int safe_strcmp(const char *a, const char *nullable_b)
+{
+       if (!nullable_b)
+               return -1;
+       return strcmp(a, nullable_b);
+}
+
 static int do_authcheckpassword(const char *password, const char *encrypted_password)
 {
+       char *cpass;
        if (strncmp(encrypted_password, "$1$", 3) == 0
            || strncasecmp(encrypted_password, "{MD5}", 5) == 0
            || strncasecmp(encrypted_password, "{MD5RAW}", 8) == 0
@@ -36,23 +43,28 @@ static int do_authcheckpassword(const char *password, const char *encrypted_pass
                return (authcheckpasswordmd5(password, encrypted_password));
 
        if (strncasecmp(encrypted_password, "{SHA}", 5) == 0 ||
-           strncasecmp(encrypted_password, "{SHA256}", 8) == 0
-               )
+           strncasecmp(encrypted_password, "{SHA256}", 8) == 0 ||
+           strncasecmp(encrypted_password, "{SHA512}", 8) == 0 ||
+           strncasecmp(encrypted_password, "{SSHA}", 6) == 0)
                return (authcheckpasswordsha1(password, encrypted_password));
 
+
 #if    HAVE_CRYPT
        if (strncasecmp(encrypted_password, "{CRYPT}", 7) == 0)
                encrypted_password += 7;
 #endif
 
-       return (
 #if    HAVE_CRYPT
-               strcmp(encrypted_password,
-                       crypt(password, encrypted_password))
+
+       cpass = crypt(password, encrypted_password);
+       if (cpass == NULL) {
+               return 1;
+       } else {
+               return safe_strcmp(encrypted_password, cpass);
+       }
 #else
-               strcmp(encrypted_password, password)
+       return safe_strcmp(encrypted_password, password)
 #endif
-                               );
 }
 
 int authcheckpassword(const char *password, const char *encrypted_password)