3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * lutil_password(credentials, password)
9 * Returns true if user supplied credentials matches
10 * the stored password.
12 * Due to the use of the crypt(3) function
13 * this routine is NOT thread-safe.
19 #include <ac/stdlib.h>
20 #include <ac/string.h>
29 #include <ac/unistd.h>
41 #include "lutil_md5.h"
42 #include "lutil_sha1.h"
45 static const unsigned char crypt64[] =
46 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
50 typedef int (*PASSWD_CHK_FUNC)(
51 const struct pw_scheme *scheme,
52 const struct berval *passwd,
53 const struct berval *cred );
55 typedef struct berval * (*PASSWD_HASH_FUNC) (
56 const struct pw_scheme *scheme,
57 const struct berval *passwd );
61 PASSWD_CHK_FUNC chk_fn;
62 PASSWD_HASH_FUNC hash_fn;
65 /* password check routines */
67 const struct pw_scheme *scheme,
68 const struct berval *passwd,
69 const struct berval *cred );
72 const struct pw_scheme *scheme,
73 const struct berval *passwd,
74 const struct berval *cred );
77 const struct pw_scheme *scheme,
78 const struct berval *passwd,
79 const struct berval *cred );
82 const struct pw_scheme *scheme,
83 const struct berval *passwd,
84 const struct berval *cred );
86 static int chk_kerberos(
87 const struct pw_scheme *scheme,
88 const struct berval *passwd,
89 const struct berval *cred );
92 const struct pw_scheme *scheme,
93 const struct berval *passwd,
94 const struct berval *cred );
97 const struct pw_scheme *scheme,
98 const struct berval *passwd,
99 const struct berval *cred );
102 /* password hash routines */
103 static struct berval *hash_sha1(
104 const struct pw_scheme *scheme,
105 const struct berval *passwd );
107 static struct berval *hash_ssha1(
108 const struct pw_scheme *scheme,
109 const struct berval *passwd );
111 static struct berval *hash_smd5(
112 const struct pw_scheme *scheme,
113 const struct berval *passwd );
115 static struct berval *hash_md5(
116 const struct pw_scheme *scheme,
117 const struct berval *passwd );
119 static struct berval *hash_crypt(
120 const struct pw_scheme *scheme,
121 const struct berval *passwd );
124 static const struct pw_scheme pw_schemes[] =
126 { {sizeof("{SSHA}")-1, "{SSHA}"}, chk_ssha1, hash_ssha1 },
127 { {sizeof("{SHA}")-1, "{SHA}"}, chk_sha1, hash_sha1 },
129 { {sizeof("{SMD5}")-1, "{SMD5}"}, chk_smd5, hash_smd5 },
130 { {sizeof("{MD5}")-1, "{MD5}"}, chk_md5, hash_md5 },
133 { {sizeof("{KERBEROS}")-1, "{KERBEROS}"}, chk_kerberos, NULL },
137 { {sizeof("{CRYPT}")-1, "{CRYPT}"}, chk_crypt, hash_crypt },
139 # if defined( HAVE_GETSPNAM ) \
140 || ( defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD ) )
141 { {sizeof("{UNIX}")-1, "{UNIX}"}, chk_unix, NULL },
144 #ifdef SLAPD_CLEARTEXT
146 { {0, "{CLEARTEXT}"}, NULL, NULL },
149 { {0, NULL}, NULL, NULL }
152 static const struct pw_scheme *get_scheme(
157 for( i=0; pw_schemes[i].name.bv_val; i++) {
158 if( pw_schemes[i].name.bv_len == 0 ) continue;
160 if( strncasecmp(scheme, pw_schemes[i].name.bv_val,
161 pw_schemes[i].name.bv_len) == 0 )
163 return &pw_schemes[i];
170 int lutil_passwd_scheme(
173 if( scheme == NULL ) {
177 return get_scheme(scheme) != NULL;
181 static int is_allowed_scheme(
183 const char** schemes )
187 if( schemes == NULL ) return 1;
189 for( i=0; schemes[i] != NULL; i++ ) {
190 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
197 static struct berval *passwd_scheme(
198 const struct pw_scheme *scheme,
199 const struct berval * passwd,
200 const char** allowed )
202 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
206 if( passwd->bv_len >= scheme->name.bv_len ) {
207 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
208 struct berval *bv = ber_memalloc( sizeof(struct berval) );
210 if( bv == NULL ) return NULL;
212 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
213 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
223 * Return 0 if creds are good.
227 const struct berval *passwd, /* stored passwd */
228 const struct berval *cred, /* user cred */
229 const char **schemes )
233 if (cred == NULL || cred->bv_len == 0 ||
234 passwd == NULL || passwd->bv_len == 0 )
239 for( i=0; pw_schemes[i].name.bv_val != NULL; i++ ) {
240 if( pw_schemes[i].chk_fn ) {
241 struct berval *p = passwd_scheme( &pw_schemes[i],
245 int rc = (pw_schemes[i].chk_fn)( &pw_schemes[i], p, cred );
247 /* only free the berval structure as the bv_val points
248 * into passwd->bv_val
257 #ifdef SLAPD_CLEARTEXT
258 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
259 return passwd->bv_len == cred->bv_len
260 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
269 struct berval * lutil_passwd_generate( ber_len_t len )
273 if( len < 1 ) return NULL;
275 pw = ber_memalloc( sizeof( struct berval ) );
276 if( pw == NULL ) return NULL;
279 pw->bv_val = ber_memalloc( len + 1 );
281 if( pw->bv_val == NULL ) {
286 if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
291 for( len = 0; len < pw->bv_len; len++ ) {
292 pw->bv_val[len] = crypt64[
293 pw->bv_val[len] % (sizeof(crypt64)-1) ];
296 pw->bv_val[len] = '\0';
301 struct berval * lutil_passwd_hash(
302 const struct berval * passwd,
303 const char * method )
305 const struct pw_scheme *sc = get_scheme( method );
307 if( sc == NULL ) return NULL;
308 if( ! sc->hash_fn ) return NULL;
310 return (sc->hash_fn)( sc, passwd );
313 static struct berval * pw_string(
314 const struct pw_scheme *sc,
315 const struct berval *passwd )
317 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
318 if( pw == NULL ) return NULL;
320 pw->bv_len = sc->name.bv_len + passwd->bv_len;
321 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
323 if( pw->bv_val == NULL ) {
328 memcpy( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
329 memcpy( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
331 pw->bv_val[pw->bv_len] = '\0';
335 static struct berval * pw_string64(
336 const struct pw_scheme *sc,
337 const struct berval *hash,
338 const struct berval *salt )
341 struct berval string;
342 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
345 if( b64 == NULL ) return NULL;
348 /* need to base64 combined string */
349 string.bv_len = hash->bv_len + salt->bv_len;
350 string.bv_val = ber_memalloc( string.bv_len + 1 );
352 if( string.bv_val == NULL ) {
357 memcpy( string.bv_val, hash->bv_val,
359 memcpy( &string.bv_val[hash->bv_len], salt->bv_val,
361 string.bv_val[string.bv_len] = '\0';
367 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
368 b64->bv_len = b64len + sc->name.bv_len;
369 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
371 if( b64->bv_val == NULL ) {
372 if( salt ) ber_memfree( string.bv_val );
377 memcpy(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
380 string.bv_val, string.bv_len,
381 &b64->bv_val[sc->name.bv_len], b64len );
383 b64->bv_val[b64->bv_len] = '\0';
385 if( salt ) ber_memfree( string.bv_val );
395 /* PASSWORD CHECK ROUTINES */
397 static int chk_ssha1(
398 const struct pw_scheme *sc,
399 const struct berval * passwd,
400 const struct berval * cred )
402 lutil_SHA1_CTX SHA1context;
403 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
405 unsigned char *orig_pass = NULL;
407 /* base64 un-encode password */
408 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
409 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
411 if( orig_pass == NULL ) return -1;
413 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
416 ber_memfree(orig_pass);
420 /* hash credentials with salt */
421 lutil_SHA1Init(&SHA1context);
422 lutil_SHA1Update(&SHA1context,
423 (const unsigned char *) cred->bv_val, cred->bv_len);
424 lutil_SHA1Update(&SHA1context,
425 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
426 rc - sizeof(SHA1digest));
427 lutil_SHA1Final(SHA1digest, &SHA1context);
430 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
431 ber_memfree(orig_pass);
436 const struct pw_scheme *sc,
437 const struct berval * passwd,
438 const struct berval * cred )
440 lutil_SHA1_CTX SHA1context;
441 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
443 unsigned char *orig_pass = NULL;
445 /* base64 un-encode password */
446 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
447 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
449 if( orig_pass == NULL ) return -1;
451 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
453 if( rc != sizeof(SHA1digest) ) {
454 ber_memfree(orig_pass);
458 /* hash credentials with salt */
459 lutil_SHA1Init(&SHA1context);
460 lutil_SHA1Update(&SHA1context,
461 (const unsigned char *) cred->bv_val, cred->bv_len);
462 lutil_SHA1Final(SHA1digest, &SHA1context);
465 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
466 ber_memfree(orig_pass);
471 const struct pw_scheme *sc,
472 const struct berval * passwd,
473 const struct berval * cred )
475 lutil_MD5_CTX MD5context;
476 unsigned char MD5digest[LUTIL_MD5_BYTES];
478 unsigned char *orig_pass = NULL;
480 /* base64 un-encode password */
481 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
482 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
484 if( orig_pass == NULL ) return -1;
486 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
488 ber_memfree(orig_pass);
492 /* hash credentials with salt */
493 lutil_MD5Init(&MD5context);
494 lutil_MD5Update(&MD5context,
495 (const unsigned char *) cred->bv_val, cred->bv_len );
496 lutil_MD5Update(&MD5context,
497 (const unsigned char *) &orig_pass[sizeof(MD5digest)],
498 rc - sizeof(MD5digest));
499 lutil_MD5Final(MD5digest, &MD5context);
502 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
503 ber_memfree(orig_pass);
508 const struct pw_scheme *sc,
509 const struct berval * passwd,
510 const struct berval * cred )
512 lutil_MD5_CTX MD5context;
513 unsigned char MD5digest[LUTIL_MD5_BYTES];
515 unsigned char *orig_pass = NULL;
517 /* base64 un-encode password */
518 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
519 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
521 if( orig_pass == NULL ) return -1;
523 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
524 if ( rc != sizeof(MD5digest) ) {
525 ber_memfree(orig_pass);
529 /* hash credentials with salt */
530 lutil_MD5Init(&MD5context);
531 lutil_MD5Update(&MD5context,
532 (const unsigned char *) cred->bv_val, cred->bv_len );
533 lutil_MD5Final(MD5digest, &MD5context);
536 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
537 ber_memfree(orig_pass);
542 static int chk_kerberos(
543 const struct pw_scheme *sc,
544 const struct berval * passwd,
545 const struct berval * cred )
550 for( i=0; i<cred->bv_len; i++) {
551 if(cred->bv_val[i] == '\0') {
552 return 1; /* NUL character in password */
556 if( cred->bv_val[i] != '\0' ) {
557 return 1; /* cred must behave like a string */
560 for( i=0; i<passwd->bv_len; i++) {
561 if(passwd->bv_val[i] == '\0') {
562 return 1; /* NUL character in password */
566 if( passwd->bv_val[i] != '\0' ) {
567 return 1; /* passwd must behave like a string */
572 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
575 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
576 * (Royal Institute of Technology, Stockholm, Sweden).
577 * All rights reserved.
579 * Redistribution and use in source and binary forms, with or without
580 * modification, are permitted provided that the following conditions
583 * 1. Redistributions of source code must retain the above copyright
584 * notice, this list of conditions and the following disclaimer.
586 * 2. Redistributions in binary form must reproduce the above copyright
587 * notice, this list of conditions and the following disclaimer in the
588 * documentation and/or other materials provided with the distribution.
590 * 3. Neither the name of the Institute nor the names of its contributors
591 * may be used to endorse or promote products derived from this software
592 * without specific prior written permission.
594 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
595 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
596 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
597 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
598 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
599 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
600 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
601 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
602 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
603 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
607 krb5_context context;
610 krb5_get_init_creds_opt get_options;
611 krb5_verify_init_creds_opt verify_options;
612 krb5_principal client, server;
614 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
617 ret = krb5_init_context( &context );
623 krb5_get_init_creds_opt_set_preauth_list(&get_options,
627 krb5_get_init_creds_opt_init( &get_options );
629 krb5_verify_init_creds_opt_init( &verify_options );
631 ret = krb5_parse_name( context, passwd->bv_val, &client );
634 krb5_free_context( context );
638 ret = krb5_get_init_creds_password( context,
639 &creds, client, cred->bv_val, NULL,
640 NULL, 0, NULL, &get_options );
643 krb5_free_principal( context, client );
644 krb5_free_context( context );
649 char host[MAXHOSTNAMELEN];
651 if( gethostname( host, MAXHOSTNAMELEN ) != 0 ) {
652 krb5_free_principal( context, client );
653 krb5_free_context( context );
657 ret = krb5_sname_to_principal( context,
658 host, "ldap", KRB5_NT_SRV_HST, &server );
662 krb5_free_principal( context, client );
663 krb5_free_context( context );
667 ret = krb5_verify_init_creds( context,
668 &creds, server, NULL, NULL, &verify_options );
670 krb5_free_principal( context, client );
671 krb5_free_principal( context, server );
672 krb5_free_creds_contents( context, &creds );
673 krb5_free_context( context );
677 #elif defined(HAVE_KRB4)
679 /* Borrowed from Heimdal kpopper */
681 * Copyright (c) 1989 Regents of the University of California.
682 * All rights reserved. The Berkeley software License Agreement
683 * specifies the terms and conditions for redistribution.
687 char lrealm[REALM_SZ];
688 char tkt[MAXHOSTNAMELEN];
690 status = krb_get_lrealm(lrealm,1);
691 if (status == KFAILURE) {
695 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
696 TKT_ROOT, (unsigned)getpid());
697 krb_set_tkt_string (tkt);
699 status = krb_verify_user( passwd->bv_val, "", lrealm,
700 cred->bv_val, 1, "ldap");
702 dest_tkt(); /* no point in keeping the tickets */
704 return status == KFAILURE;
710 #endif /* SLAPD_KPASSWD */
713 static int chk_crypt(
714 const struct pw_scheme *sc,
715 const struct berval * passwd,
716 const struct berval * cred )
720 for( i=0; i<cred->bv_len; i++) {
721 if(cred->bv_val[i] == '\0') {
722 return 1; /* NUL character in password */
726 if( cred->bv_val[i] != '\0' ) {
727 return 1; /* cred must behave like a string */
730 for( i=0; i<passwd->bv_len; i++) {
731 if(passwd->bv_val[i] == '\0') {
732 return 1; /* NUL character in password */
736 if( passwd->bv_val[i] != '\0' ) {
737 return 1; /* passwd must behave like a string */
740 return strcmp(passwd->bv_val, crypt(cred->bv_val, passwd->bv_val));
743 # if defined( HAVE_GETSPNAM ) \
744 || ( defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD ) )
746 const struct pw_scheme *sc,
747 const struct berval * passwd,
748 const struct berval * cred )
753 for( i=0; i<cred->bv_len; i++) {
754 if(cred->bv_val[i] == '\0') {
755 return 1; /* NUL character in password */
758 if( cred->bv_val[i] != '\0' ) {
759 return 1; /* cred must behave like a string */
762 for( i=0; i<passwd->bv_len; i++) {
763 if(passwd->bv_val[i] == '\0') {
764 return 1; /* NUL character in password */
768 if( passwd->bv_val[i] != '\0' ) {
769 return 1; /* passwd must behave like a string */
772 # ifdef HAVE_GETSPNAM
774 struct spwd *spwd = getspnam(passwd->bv_val);
777 return 1; /* not found */
785 struct passwd *pwd = getpwnam(passwd->bv_val);
788 return 1; /* not found */
795 if( pw == NULL || *pw == '\0' ) return 1;
797 return strcmp(pw, crypt(cred->bv_val, pw));
803 /* PASSWORD CHECK ROUTINES */
804 static struct berval *hash_ssha1(
805 const struct pw_scheme *scheme,
806 const struct berval *passwd )
808 lutil_SHA1_CTX SHA1context;
809 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
810 unsigned char saltdata[4];
811 struct berval digest;
814 digest.bv_val = SHA1digest;
815 digest.bv_len = sizeof(SHA1digest);
816 salt.bv_val = saltdata;
817 salt.bv_len = sizeof(saltdata);
819 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
823 lutil_SHA1Init( &SHA1context );
824 lutil_SHA1Update( &SHA1context,
825 (const unsigned char *)passwd->bv_val, passwd->bv_len );
826 lutil_SHA1Update( &SHA1context,
827 (const unsigned char *)salt.bv_val, salt.bv_len );
828 lutil_SHA1Final( SHA1digest, &SHA1context );
830 return pw_string64( scheme, &digest, &salt);
833 static struct berval *hash_sha1(
834 const struct pw_scheme *scheme,
835 const struct berval *passwd )
837 lutil_SHA1_CTX SHA1context;
838 unsigned char SHA1digest[20];
839 struct berval digest;
840 digest.bv_val = SHA1digest;
841 digest.bv_len = sizeof(SHA1digest);
843 lutil_SHA1Init( &SHA1context );
844 lutil_SHA1Update( &SHA1context,
845 (const unsigned char *)passwd->bv_val, passwd->bv_len );
846 lutil_SHA1Final( SHA1digest, &SHA1context );
848 return pw_string64( scheme, &digest, NULL);
851 static struct berval *hash_smd5(
852 const struct pw_scheme *scheme,
853 const struct berval *passwd )
855 lutil_MD5_CTX MD5context;
856 unsigned char MD5digest[16];
857 unsigned char saltdata[4];
858 struct berval digest;
861 digest.bv_val = MD5digest;
862 digest.bv_len = sizeof(MD5digest);
863 salt.bv_val = saltdata;
864 salt.bv_len = sizeof(saltdata);
866 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
870 lutil_MD5Init( &MD5context );
871 lutil_MD5Update( &MD5context,
872 (const unsigned char *) passwd->bv_val, passwd->bv_len );
873 lutil_MD5Update( &MD5context,
874 (const unsigned char *) salt.bv_val, salt.bv_len );
875 lutil_MD5Final( MD5digest, &MD5context );
877 return pw_string64( scheme, &digest, &salt );
880 static struct berval *hash_md5(
881 const struct pw_scheme *scheme,
882 const struct berval *passwd )
884 lutil_MD5_CTX MD5context;
885 unsigned char MD5digest[16];
887 struct berval digest;
889 digest.bv_val = MD5digest;
890 digest.bv_len = sizeof(MD5digest);
892 lutil_MD5Init( &MD5context );
893 lutil_MD5Update( &MD5context,
894 (const unsigned char *) passwd->bv_val, passwd->bv_len );
895 lutil_MD5Final( MD5digest, &MD5context );
897 return pw_string64( scheme, &digest, NULL );
902 static struct berval *hash_crypt(
903 const struct pw_scheme *scheme,
904 const struct berval *passwd )
907 unsigned char salt[3];
910 for( i=0; i<passwd->bv_len; i++) {
911 if(passwd->bv_val[i] == '\0') {
912 return NULL; /* NUL character in password */
916 if( passwd->bv_val[i] != '\0' ) {
917 return NULL; /* passwd must behave like a string */
920 if( lutil_entropy( salt, sizeof(salt)) < 0 ) {
924 salt[0] = crypt64[ salt[0] % (sizeof(crypt64)-1) ];
925 salt[1] = crypt64[ salt[1] % (sizeof(crypt64)-1) ];
928 hash.bv_val = crypt( passwd->bv_val, salt );
930 if( hash.bv_val == NULL ) return NULL;
931 hash.bv_len = strlen( hash.bv_val );
933 return pw_string( scheme, &hash );