3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * lutil_authpassword(authpasswd, cred)
9 * Returns true if user supplied credentials (cred) matches
10 * the stored authentication password (authpasswd).
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 *salt,
54 const struct berval *cred );
56 typedef int (*PASSWD_HASH_FUNC) (
57 const struct pw_scheme *scheme,
58 const struct berval *cred,
59 const struct berval *salt,
60 struct berval **passwd_out,
61 struct berval **salt_out );
63 /* password check routines */
65 const struct pw_scheme *scheme,
66 const struct berval *passwd,
67 const struct berval *salt,
68 const struct berval *cred );
71 const struct pw_scheme *scheme,
72 const struct berval *passwd,
73 const struct berval *salt,
74 const struct berval *cred );
77 const struct pw_scheme *scheme,
78 const struct berval *passwd,
79 const struct berval *salt,
80 const struct berval *cred );
83 const struct pw_scheme *scheme,
84 const struct berval *passwd,
85 const struct berval *salt,
86 const struct berval *cred );
88 static int chk_ext_kerberos(
89 const struct pw_scheme *scheme,
90 const struct berval *passwd,
91 const struct berval *cred );
93 static int chk_ext_unix(
94 const struct pw_scheme *scheme,
95 const struct berval *passwd,
96 const struct berval *cred );
99 /* password hash routines */
100 static int *hash_sha1(
101 const struct pw_scheme *scheme,
102 const struct berval *cred,
103 const struct berval *salt,
104 struct berval **passwd_out,
105 struct berval **salt_out );
107 static int *hash_md5(
108 const struct pw_scheme *scheme,
109 const struct berval *cred,
110 const struct berval *salt,
111 struct berval **passwd_out,
112 struct berval **salt_out );
114 static int *hash_crypt(
115 const struct pw_scheme *scheme,
116 const struct berval *cred,
117 const struct berval *salt,
118 struct berval **passwd_out,
119 struct berval **salt_out );
124 PASSWD_CHK_FUNC chk_fn;
125 PASSWD_HASH_FUNC hash_fn;
129 static const struct pw_scheme pw_schemes[] =
131 { {sizeof("SHA1")-1, "SHA1"}, chk_sha1, 0 /* hash_sha1 */, 4 },
132 { {sizeof("MD5")-1, "MD5"}, chk_md5, 0 /* hash_md5 */, 4 },
135 { {sizeof("CRYPT")-1, "CRYPT"}, chk_crypt, hash_crypt, 2 },
139 { {sizeof("EXTERNAL")-1, "EXTERNAL"}, chk_ext, NULL, 0 },
142 { {0, NULL}, NULL, NULL, 0 }
151 static const struct ext_scheme ext_schemes[] =
153 { {0, NULL}, NULL, NULL, 0 }
157 static const struct pw_scheme *get_scheme(
162 if( scheme == NULL || *scheme == '\0' ) return NULL;
164 for( i=0; pw_schemes[i].name.bv_val; i++) {
165 if( pw_schemes[i].name.bv_len == 0 ) continue;
167 if( strncasecmp( scheme,
168 pw_schemes[i].name.bv_val,
169 pw_schemes[i].name.bv_len) == 0 )
171 return &pw_schemes[i];
178 int lutil_authpasswd_scheme(
181 return get_scheme( scheme ) != NULL;
185 static int is_allowed_scheme(
187 const char** schemes )
191 if( scheme == NULL || *scheme == '\0' ) return 1;
193 for( i=0; schemes[i] != NULL; i++ ) {
194 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
201 static int parse_authpasswd(
204 struct berval *passwd )
211 * Return 0 if creds are good.
215 const struct berval *value, /* stored authpasswd */
216 const struct berval *cred, /* user cred */
217 const char **schemes )
220 struct berval salt, passwd;
221 const struct pw_scheme *pws;
224 if (cred == NULL || cred->bv_len == 0 ||
225 value == NULL || value->bv_len == 0 )
230 rc = parse_authpasswd( &scheme, &salt, &passwd );
232 if( rc != 0 ) return -1;
234 if( !is_allowed_scheme( scheme, schemes ) ) {
238 pws = get_scheme( scheme );
240 if( pws == NULL || !pws->chk_fn ) {
244 rc = (pws->chk_fn)( pws, &salt, &passwd, cred );
247 if( scheme != NULL ) {
248 ber_memfree( scheme );
249 ber_memfree( salt.bv_val );
250 ber_memfree( passwd.bv_val );
256 struct berval * lutil_authpasswd_generate( ber_len_t len )
260 if( len < 1 ) return NULL;
262 pw = ber_memalloc( sizeof( struct berval ) );
263 if( pw == NULL ) return NULL;
266 pw->bv_val = ber_memalloc( len + 1 );
268 if( pw->bv_val == NULL ) {
273 if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
278 for( len = 0; len < pw->bv_len; len++ ) {
279 pw->bv_val[len] = crypt64[
280 pw->bv_val[len] % (sizeof(crypt64)-1) ];
283 pw->bv_val[len] = '\0';
288 int lutil_authpasswd_hash(
289 const struct berval * cred,
290 struct berval ** passwd_out,
291 struct berval ** salt_out,
292 const char * method )
294 const struct pw_scheme *sc;
297 if( passwd_out == NULL ) return -1;
299 sc = get_scheme( method );
300 if( sc == NULL || !sc->hash_fn ) return -1;
302 if( sc->saltbytes && salt_out != NULL ) {
304 salt.bv_val = ber_memalloc( sc->saltbytes );
306 if( salt.bv_val == NULL ) {
309 salt.bv_len = sc->saltbytes;
311 if( lutil_entropy( salt.bv_val, salt.bv_len ) < 0 ) {
312 ber_memfree( salt.bv_val );
316 rc = (sc->hash_fn)( sc, cred, &salt, passwd_out, NULL );
317 ber_memfree( salt.bv_val );
319 } else if ( sc->saltbytes ) {
320 /* wants salt, disallow */
324 rc = (sc->hash_fn)( sc, cred, NULL, passwd_out, salt_out );
330 static struct berval * base64(
331 const struct berval *value )
336 assert( value != NULL );
338 if( value == NULL || value->bv_len == 0 ) return NULL;
340 b64 = ber_memalloc( sizeof(struct berval) );
341 if( b64 == NULL ) return NULL;
343 b64->bv_len = LUTIL_BASE64_ENCODE_LEN( value->bv_len );
344 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
346 if( b64->bv_val == NULL ) {
352 value->bv_val, value->bv_len,
353 b64->bv_val, b64->bv_len );
355 b64->bv_val[b64->bv_len] = '\0';
365 /* PASSWORD CHECK ROUTINES */
368 const struct pw_scheme *sc,
369 const struct berval * passwd,
370 const struct berval * salt,
371 const struct berval * cred )
373 lutil_SHA1_CTX SHA1context;
374 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
376 unsigned char *orig_pass = NULL;
377 unsigned char *orig_salt = NULL;
380 if( passwd == NULL || passwd->bv_len == 0 ) {
384 /* decode base64 password */
385 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
386 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
388 if( orig_pass == NULL ) {
393 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
399 /* decode base64 salt */
400 if( salt != NULL && salt->bv_len > 0 ) {
401 orig_salt = (unsigned char *) ber_memalloc( (size_t) (
402 LUTIL_BASE64_DECODE_LEN(salt->bv_len) + 1) );
404 if( orig_salt == NULL ) {
409 saltlen = lutil_b64_pton(passwd->bv_val, orig_salt, passwd->bv_len);
416 /* hash credentials with salt */
417 lutil_SHA1Init(&SHA1context);
418 lutil_SHA1Update(&SHA1context,
419 (const unsigned char *) cred->bv_val, cred->bv_len);
420 if( orig_salt != NULL ) {
421 lutil_SHA1Update(&SHA1context,
422 orig_salt, saltlen );
424 lutil_SHA1Final(SHA1digest, &SHA1context);
427 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
430 ber_memfree(orig_pass);
431 ber_memfree(orig_salt);
436 const struct pw_scheme *sc,
437 const struct berval * passwd,
438 const struct berval * salt,
439 const struct berval * cred )
441 lutil_MD5_CTX MD5context;
442 unsigned char MD5digest[LUTIL_MD5_BYTES];
444 unsigned char *orig_pass = NULL;
445 unsigned char *orig_salt = NULL;
448 if( passwd == NULL || passwd->bv_len == 0 ) {
452 /* decode base64 password */
453 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
454 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
456 if( orig_pass == NULL ) {
461 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
467 /* decode base64 salt */
468 if( salt != NULL && salt->bv_len > 0 ) {
469 orig_salt = (unsigned char *) ber_memalloc( (size_t) (
470 LUTIL_BASE64_DECODE_LEN(salt->bv_len) + 1) );
472 if( orig_salt == NULL ) {
477 saltlen = lutil_b64_pton(passwd->bv_val, orig_salt, passwd->bv_len);
484 /* hash credentials with salt */
485 lutil_MD5Init(&MD5context);
486 lutil_MD5Update(&MD5context,
487 (const unsigned char *) cred->bv_val, cred->bv_len);
488 if( orig_salt != NULL ) {
489 lutil_MD5Update(&MD5context,
490 orig_salt, saltlen );
492 lutil_MD5Final(MD5digest, &MD5context);
495 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
498 ber_memfree(orig_pass);
499 ber_memfree(orig_salt);
504 static int chk_kerberos(
505 const struct pw_scheme *sc,
506 const struct berval * passwd,
507 const struct berval * cred,
508 const struct berval * salt )
513 for( i=0; i<cred->bv_len; i++) {
514 if(cred->bv_val[i] == '\0') {
515 return 1; /* NUL character in password */
519 if( cred->bv_val[i] != '\0' ) {
520 return 1; /* cred must behave like a string */
523 for( i=0; i<passwd->bv_len; i++) {
524 if(passwd->bv_val[i] == '\0') {
525 return 1; /* NUL character in password */
529 if( passwd->bv_val[i] != '\0' ) {
530 return 1; /* passwd must behave like a string */
535 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
538 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
539 * (Royal Institute of Technology, Stockholm, Sweden).
540 * All rights reserved.
542 * Redistribution and use in source and binary forms, with or without
543 * modification, are permitted provided that the following conditions
546 * 1. Redistributions of source code must retain the above copyright
547 * notice, this list of conditions and the following disclaimer.
549 * 2. Redistributions in binary form must reproduce the above copyright
550 * notice, this list of conditions and the following disclaimer in the
551 * documentation and/or other materials provided with the distribution.
553 * 3. Neither the name of the Institute nor the names of its contributors
554 * may be used to endorse or promote products derived from this software
555 * without specific prior written permission.
557 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
558 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
559 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
560 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
561 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
562 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
563 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
564 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
565 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
566 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
570 krb5_context context;
573 krb5_get_init_creds_opt get_options;
574 krb5_verify_init_creds_opt verify_options;
575 krb5_principal client, server;
577 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
580 ret = krb5_init_context( &context );
586 krb5_get_init_creds_opt_set_preauth_list(&get_options,
590 krb5_get_init_creds_opt_init( &get_options );
592 krb5_verify_init_creds_opt_init( &verify_options );
594 ret = krb5_parse_name( context, passwd->bv_val, &client );
597 krb5_free_context( context );
601 ret = krb5_get_init_creds_password( context,
602 &creds, client, cred->bv_val, NULL,
603 NULL, 0, NULL, &get_options );
606 krb5_free_principal( context, client );
607 krb5_free_context( context );
612 char host[MAXHOSTNAMELEN];
614 if( gethostname( host, MAXHOSTNAMELEN ) != 0 ) {
615 krb5_free_principal( context, client );
616 krb5_free_context( context );
620 ret = krb5_sname_to_principal( context,
621 host, "ldap", KRB5_NT_SRV_HST, &server );
625 krb5_free_principal( context, client );
626 krb5_free_context( context );
630 ret = krb5_verify_init_creds( context,
631 &creds, server, NULL, NULL, &verify_options );
633 krb5_free_principal( context, client );
634 krb5_free_principal( context, server );
635 krb5_free_creds_contents( context, &creds );
636 krb5_free_context( context );
640 #elif defined(HAVE_KRB4)
642 /* Borrowed from Heimdal kpopper */
644 * Copyright (c) 1989 Regents of the University of California.
645 * All rights reserved. The Berkeley software License Agreement
646 * specifies the terms and conditions for redistribution.
650 char lrealm[REALM_SZ];
651 char tkt[MAXHOSTNAMELEN];
653 status = krb_get_lrealm(lrealm,1);
654 if (status == KFAILURE) {
658 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
659 TKT_ROOT, (unsigned)getpid());
660 krb_set_tkt_string (tkt);
662 status = krb_verify_user( passwd->bv_val, "", lrealm,
663 cred->bv_val, 1, "ldap");
665 dest_tkt(); /* no point in keeping the tickets */
667 return status == KFAILURE;
673 #endif /* SLAPD_KPASSWD */
676 static int chk_crypt(
677 const struct pw_scheme *sc,
678 const struct berval * passwd,
679 const struct berval * cred )
684 for( i=0; i<cred->bv_len; i++) {
685 if(cred->bv_val[i] == '\0') {
686 return 1; /* NUL character in password */
690 if( cred->bv_val[i] != '\0' ) {
691 return 1; /* cred must behave like a string */
694 if( passwd->bv_len < 2 ) {
695 return 1; /* passwd must be at least two characters long */
698 for( i=0; i<passwd->bv_len; i++) {
699 if(passwd->bv_val[i] == '\0') {
700 return 1; /* NUL character in password */
704 if( passwd->bv_val[i] != '\0' ) {
705 return 1; /* passwd must behave like a string */
708 cr = crypt( cred->bv_val, passwd->bv_val );
710 if( cr == NULL || cr[0] == '\0' ) {
711 /* salt must have been invalid */
715 return strcmp( passwd->bv_val, cr );
718 # if defined( HAVE_GETSPNAM ) \
719 || ( defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD ) )
721 const struct pw_scheme *sc,
722 const struct berval * passwd,
723 const struct berval * cred )
728 for( i=0; i<cred->bv_len; i++) {
729 if(cred->bv_val[i] == '\0') {
730 return 1; /* NUL character in password */
733 if( cred->bv_val[i] != '\0' ) {
734 return 1; /* cred must behave like a string */
737 for( i=0; i<passwd->bv_len; i++) {
738 if(passwd->bv_val[i] == '\0') {
739 return 1; /* NUL character in password */
743 if( passwd->bv_val[i] != '\0' ) {
744 return 1; /* passwd must behave like a string */
747 # ifdef HAVE_GETSPNAM
749 struct spwd *spwd = getspnam(passwd->bv_val);
752 return 1; /* not found */
760 struct passwd *pwd = getpwnam(passwd->bv_val);
763 return 1; /* not found */
770 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
771 /* password must must be at least two characters long */
775 cr = crypt(cred->bv_val, pw);
777 if( cr == NULL || cr[0] == '\0' ) {
778 /* salt must have been invalid */
782 return strcmp(pw, cr);
788 /* PASSWORD GENERATION ROUTINES */
790 #ifdef SLAPD_GENERATE
792 static struct berval *hash_ssha1(
793 const struct pw_scheme *scheme,
794 const struct berval *passwd )
796 lutil_SHA1_CTX SHA1context;
797 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
798 unsigned char saltdata[4];
799 struct berval digest;
802 digest.bv_val = SHA1digest;
803 digest.bv_len = sizeof(SHA1digest);
804 salt.bv_val = saltdata;
805 salt.bv_len = sizeof(saltdata);
807 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
811 lutil_SHA1Init( &SHA1context );
812 lutil_SHA1Update( &SHA1context,
813 (const unsigned char *)passwd->bv_val, passwd->bv_len );
814 lutil_SHA1Update( &SHA1context,
815 (const unsigned char *)salt.bv_val, salt.bv_len );
816 lutil_SHA1Final( SHA1digest, &SHA1context );
818 return pw_string64( scheme, &digest, &salt);
821 static struct berval *hash_sha1(
822 const struct pw_scheme *scheme,
823 const struct berval *passwd )
825 lutil_SHA1_CTX SHA1context;
826 unsigned char SHA1digest[20];
827 struct berval digest;
828 digest.bv_val = SHA1digest;
829 digest.bv_len = sizeof(SHA1digest);
831 lutil_SHA1Init( &SHA1context );
832 lutil_SHA1Update( &SHA1context,
833 (const unsigned char *)passwd->bv_val, passwd->bv_len );
834 lutil_SHA1Final( SHA1digest, &SHA1context );
836 return pw_string64( scheme, &digest, NULL);
839 static struct berval *hash_smd5(
840 const struct pw_scheme *scheme,
841 const struct berval *passwd )
843 lutil_MD5_CTX MD5context;
844 unsigned char MD5digest[16];
845 unsigned char saltdata[4];
846 struct berval digest;
849 digest.bv_val = MD5digest;
850 digest.bv_len = sizeof(MD5digest);
851 salt.bv_val = saltdata;
852 salt.bv_len = sizeof(saltdata);
854 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
858 lutil_MD5Init( &MD5context );
859 lutil_MD5Update( &MD5context,
860 (const unsigned char *) passwd->bv_val, passwd->bv_len );
861 lutil_MD5Update( &MD5context,
862 (const unsigned char *) salt.bv_val, salt.bv_len );
863 lutil_MD5Final( MD5digest, &MD5context );
865 return pw_string64( scheme, &digest, &salt );
868 static struct berval *hash_md5(
869 const struct pw_scheme *scheme,
870 const struct berval *passwd )
872 lutil_MD5_CTX MD5context;
873 unsigned char MD5digest[16];
875 struct berval digest;
877 digest.bv_val = MD5digest;
878 digest.bv_len = sizeof(MD5digest);
880 lutil_MD5Init( &MD5context );
881 lutil_MD5Update( &MD5context,
882 (const unsigned char *) passwd->bv_val, passwd->bv_len );
883 lutil_MD5Final( MD5digest, &MD5context );
885 return pw_string64( scheme, &digest, NULL );
890 static struct berval *hash_crypt(
891 const struct pw_scheme *scheme,
892 const struct berval *passwd )
895 unsigned char salt[3];
898 for( i=0; i<passwd->bv_len; i++) {
899 if(passwd->bv_val[i] == '\0') {
900 return NULL; /* NUL character in password */
904 if( passwd->bv_val[i] != '\0' ) {
905 return NULL; /* passwd must behave like a string */
908 if( lutil_entropy( salt, sizeof(salt)) < 0 ) {
912 salt[0] = crypt64[ salt[0] % (sizeof(crypt64)-1) ];
913 salt[1] = crypt64[ salt[1] % (sizeof(crypt64)-1) ];
916 hash.bv_val = crypt( passwd->bv_val, salt );
918 if( hash.bv_val == NULL ) return NULL;
920 hash.bv_len = strlen( hash.bv_val );
922 if( hash.bv_len == 0 ) {
926 return pw_string( scheme, &hash );