2 * lutil_password(credentials, password)
4 * Returns true if user supplied credentials matches
7 * Due to the use of the crypt(3) function
8 * this routine is NOT thread-safe.
13 #include <ac/string.h>
14 #include <ac/unistd.h>
16 #include "lutil_md5.h"
17 #include "lutil_sha1.h"
29 if (cred == NULL || passwd == NULL) {
33 if (strncasecmp(passwd, "{CRYPT}", sizeof("{CRYPT}") - 1) == 0 ) {
34 const char *p = passwd + (sizeof("{CRYPT}") - 1);
36 return( strcmp(p, crypt(cred, p)) != 0 );
38 } else if (strncasecmp(passwd, "{MD5}", sizeof("{MD5}") - 1) == 0 ) {
39 ldap_MD5_CTX MD5context;
40 unsigned char MD5digest[20];
41 char base64digest[29]; /* ceiling(sizeof(input)/3) * 4 + 1 */
43 const char *p = passwd + (sizeof("{MD5}") - 1);
45 ldap_MD5Init(&MD5context);
46 ldap_MD5Update(&MD5context,
47 (const unsigned char *)cred, strlen(cred));
48 ldap_MD5Final(MD5digest, &MD5context);
50 if ( b64_ntop(MD5digest, sizeof(MD5digest),
51 base64digest, sizeof(base64digest)) < 0)
56 return (strcmp(p, base64digest) != 0);
58 } else if (strncasecmp(passwd, "{SHA}",sizeof("{SHA}") - 1) == 0 ) {
59 ldap_SHA1_CTX SHA1context;
60 unsigned char SHA1digest[20];
61 char base64digest[29]; /* ceiling(sizeof(input)/3) * 4 + 1 */
62 const char *p = passwd + (sizeof("{SHA}") - 1);
64 ldap_SHA1Init(&SHA1context);
65 ldap_SHA1Update(&SHA1context,
66 (const unsigned char *) cred, strlen(cred));
67 ldap_SHA1Final(SHA1digest, &SHA1context);
69 if (b64_ntop(SHA1digest, sizeof(SHA1digest),
70 base64digest, sizeof(base64digest)) < 0)
75 return( strcmp(p, base64digest) != 0 );
78 return( strcmp(passwd, cred) != 0 );