]> git.sur5r.net Git - openldap/blob - libraries/liblutil/passwd.c
8cd6b1af484d93224541522b11533b1a9b12804e
[openldap] / libraries / liblutil / passwd.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * int lutil_passwd(
8  *      const struct berval *passwd,
9  *      const struct berval *cred,
10  *      const char **schemes )
11  *
12  * Returns true if user supplied credentials (cred) matches
13  * the stored password (passwd). 
14  *
15  * Due to the use of the crypt(3) function 
16  * this routine is NOT thread-safe.
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22 #include <ac/stdlib.h>
23 #include <ac/string.h>
24 #include <ac/unistd.h>
25
26 #ifdef SLAPD_SPASSWD
27 #       ifdef HAVE_SASL_SASL_H
28 #               include <sasl/sasl.h>
29 #       else
30 #               include <sasl.h>
31 #       endif
32 #endif
33
34 #ifdef SLAPD_KPASSWD
35 #       include <ac/krb.h>
36 #       include <ac/krb5.h>
37 #endif
38
39 /* KPASSWD/krb.h brings in a conflicting des.h so don't use both.
40  * configure currently requires OpenSSL to enable LMHASH. Obviously
41  * this requirement can be fulfilled by the KRB DES library as well.
42  */
43 #if defined(SLAPD_LMHASH) && !defined(DES_ENCRYPT)
44 #       include <openssl/des.h>
45 #endif /* SLAPD_LMHASH */
46
47 #include <ac/param.h>
48
49 #ifdef SLAPD_CRYPT
50 # include <ac/crypt.h>
51
52 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
53 #  ifdef HAVE_SHADOW_H
54 #       include <shadow.h>
55 #  endif
56 #  ifdef HAVE_PWD_H
57 #       include <pwd.h>
58 #  endif
59 #  ifdef HAVE_AIX_SECURITY
60 #       include <userpw.h>
61 #  endif
62 # endif
63 #endif
64
65 #include <lber.h>
66
67 #include "ldap_pvt.h"
68 #include "lber_pvt.h"
69
70 #include "lutil_md5.h"
71 #include "lutil_sha1.h"
72 #include "lutil.h"
73
74 static const unsigned char crypt64[] =
75         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
76
77 #ifdef SLAPD_CRYPT
78 static char *salt_format = NULL;
79 #endif
80
81 struct pw_scheme {
82         struct berval name;
83         LUTIL_PASSWD_CHK_FUNC *chk_fn;
84         LUTIL_PASSWD_HASH_FUNC *hash_fn;
85 };
86
87 struct pw_slist {
88         struct pw_slist *next;
89         struct pw_scheme s;
90 };
91
92 /* password check routines */
93
94 static LUTIL_PASSWD_CHK_FUNC chk_md5;
95 static LUTIL_PASSWD_CHK_FUNC chk_smd5;
96 static LUTIL_PASSWD_HASH_FUNC hash_smd5;
97 static LUTIL_PASSWD_HASH_FUNC hash_md5;
98
99
100 #ifdef LUTIL_SHA1_BYTES
101 static LUTIL_PASSWD_CHK_FUNC chk_ssha1;
102 static LUTIL_PASSWD_CHK_FUNC chk_sha1;
103 static LUTIL_PASSWD_HASH_FUNC hash_sha1;
104 static LUTIL_PASSWD_HASH_FUNC hash_ssha1;
105 #endif
106
107 #ifdef SLAPD_LMHASH
108 static LUTIL_PASSWD_CHK_FUNC chk_lanman;
109 static LUTIL_PASSWD_HASH_FUNC hash_lanman;
110 #endif
111
112 #ifdef SLAPD_NS_MTA_MD5
113 static LUTIL_PASSWD_CHK_FUNC chk_ns_mta_md5;
114 #endif
115
116 #ifdef SLAPD_SPASSWD
117 static LUTIL_PASSWD_CHK_FUNC chk_sasl;
118 #endif
119
120 #ifdef SLAPD_KPASSWD
121 static LUTIL_PASSWD_CHK_FUNC chk_kerberos;
122 #endif
123
124 #ifdef SLAPD_CRYPT
125 static LUTIL_PASSWD_CHK_FUNC chk_crypt;
126 static LUTIL_PASSWD_HASH_FUNC hash_crypt;
127
128 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
129 static LUTIL_PASSWD_CHK_FUNC chk_unix;
130 #endif
131 #endif
132
133 /* password hash routines */
134
135 #ifdef SLAPD_CLEARTEXT
136 static LUTIL_PASSWD_HASH_FUNC hash_clear;
137 #endif
138
139 static struct pw_slist *pw_schemes;
140
141 static const struct pw_scheme pw_schemes_default[] =
142 {
143 #ifdef LUTIL_SHA1_BYTES
144         { BER_BVC("{SSHA}"),            chk_ssha1, hash_ssha1 },
145         { BER_BVC("{SHA}"),                     chk_sha1, hash_sha1 },
146 #endif
147
148         { BER_BVC("{SMD5}"),            chk_smd5, hash_smd5 },
149         { BER_BVC("{MD5}"),                     chk_md5, hash_md5 },
150
151 #ifdef SLAPD_LMHASH
152         { BER_BVC("{LANMAN}"),          chk_lanman, hash_lanman },
153 #endif /* SLAPD_LMHASH */
154
155 #ifdef SLAPD_NS_MTA_MD5
156         { BER_BVC("{NS-MTA-MD5}"),      chk_ns_mta_md5, NULL },
157 #endif /* SLAPD_NS_MTA_MD5 */
158
159 #ifdef SLAPD_SPASSWD
160         { BER_BVC("{SASL}"),            chk_sasl, NULL },
161 #endif
162
163 #ifdef SLAPD_KPASSWD
164         { BER_BVC("{KERBEROS}"),        chk_kerberos, NULL },
165 #endif
166
167 #ifdef SLAPD_CRYPT
168         { BER_BVC("{CRYPT}"),           chk_crypt, hash_crypt },
169 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
170         { BER_BVC("{UNIX}"),            chk_unix, NULL },
171 # endif
172 #endif
173
174 #ifdef SLAPD_CLEARTEXT
175         /* pseudo scheme */
176         { {0, "{CLEARTEXT}"},           NULL, hash_clear },
177 #endif
178
179         { BER_BVNULL, NULL, NULL }
180 };
181
182 int lutil_passwd_add(
183         struct berval *scheme,
184         LUTIL_PASSWD_CHK_FUNC *chk,
185         LUTIL_PASSWD_HASH_FUNC *hash )
186 {
187         struct pw_slist *ptr;
188
189         ptr = ber_memalloc( sizeof( struct pw_slist ));
190         if (!ptr) return -1;
191         ptr->next = pw_schemes;
192         ptr->s.name = *scheme;
193         ptr->s.chk_fn = chk;
194         ptr->s.hash_fn = hash;
195         pw_schemes = ptr;
196         return 0;
197 }
198
199 void lutil_passwd_init()
200 {
201         struct pw_slist *ptr;
202         struct pw_scheme *s;
203
204         for( s=(struct pw_scheme *)pw_schemes_default; s->name.bv_val; s++) {
205                 if ( lutil_passwd_add( &s->name, s->chk_fn, s->hash_fn )) break;
206         }
207 }
208
209 void lutil_passwd_destroy()
210 {
211         struct pw_slist *ptr, *next;
212
213         for( ptr=pw_schemes; ptr; ptr=next ) {
214                 next = ptr->next;
215                 ber_memfree( ptr );
216         }
217 }
218
219 static const struct pw_scheme *get_scheme(
220         const char* scheme )
221 {
222         struct pw_slist *pws;
223
224         if (!pw_schemes) lutil_passwd_init();
225
226         for( pws=pw_schemes; pws; pws=pws->next ) {
227                 if( strcasecmp(scheme, pws->s.name.bv_val ) == 0 ) {
228                         return &(pws->s);
229                 }
230         }
231
232         return NULL;
233 }
234
235 int lutil_passwd_scheme(
236         const char* scheme )
237 {
238         if( scheme == NULL ) {
239                 return 0;
240         }
241
242         return get_scheme(scheme) != NULL;
243 }
244
245
246 static int is_allowed_scheme( 
247         const char* scheme,
248         const char** schemes )
249 {
250         int i;
251
252         if( schemes == NULL ) return 1;
253
254         for( i=0; schemes[i] != NULL; i++ ) {
255                 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
256                         return 1;
257                 }
258         }
259         return 0;
260 }
261
262 static struct berval *passwd_scheme(
263         const struct pw_scheme *scheme,
264         const struct berval * passwd,
265         struct berval *bv,
266         const char** allowed )
267 {
268         if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
269                 return NULL;
270         }
271
272         if( passwd->bv_len >= scheme->name.bv_len ) {
273                 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
274                         bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
275                         bv->bv_len = passwd->bv_len - scheme->name.bv_len;
276
277                         return bv;
278                 }
279         }
280
281         return NULL;
282 }
283
284 /*
285  * Return 0 if creds are good.
286  */
287 int
288 lutil_passwd(
289         const struct berval *passwd,    /* stored passwd */
290         const struct berval *cred,              /* user cred */
291         const char **schemes )
292 {
293         struct pw_slist *pws;
294
295         if (cred == NULL || cred->bv_len == 0 ||
296                 passwd == NULL || passwd->bv_len == 0 )
297         {
298                 return -1;
299         }
300
301         if (!pw_schemes) lutil_passwd_init();
302
303         for( pws=pw_schemes; pws; pws=pws->next ) {
304                 if( pws->s.chk_fn ) {
305                         struct berval x;
306                         struct berval *p = passwd_scheme( &(pws->s),
307                                 passwd, &x, schemes );
308
309                         if( p != NULL ) {
310                                 return (pws->s.chk_fn)( &(pws->s.name), p, cred );
311                         }
312                 }
313         }
314
315 #ifdef SLAPD_CLEARTEXT
316         if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
317                 return (( passwd->bv_len == cred->bv_len ) &&
318                                 ( passwd->bv_val[0] != '{' /*'}'*/ ))
319                         ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
320                         : 1;
321         }
322 #endif
323         return 1;
324 }
325
326 struct berval * lutil_passwd_generate( ber_len_t len )
327 {
328         struct berval *pw;
329
330         if( len < 1 ) return NULL;
331
332         pw = ber_memalloc( sizeof( struct berval ) );
333         if( pw == NULL ) return NULL;
334
335         pw->bv_len = len;
336         pw->bv_val = ber_memalloc( len + 1 );
337
338         if( pw->bv_val == NULL ) {
339                 ber_memfree( pw );
340                 return NULL;
341         }
342
343         if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
344                 ber_bvfree( pw );
345                 return NULL; 
346         }
347
348         for( len = 0; len < pw->bv_len; len++ ) {
349                 pw->bv_val[len] = crypt64[
350                         pw->bv_val[len] % (sizeof(crypt64)-1) ];
351         }
352
353         pw->bv_val[len] = '\0';
354         
355         return pw;
356 }
357
358 struct berval * lutil_passwd_hash(
359         const struct berval * passwd,
360         const char * method )
361 {
362         const struct pw_scheme *sc = get_scheme( method );
363
364         if( sc == NULL ) return NULL;
365         if( ! sc->hash_fn ) return NULL;
366
367         return (sc->hash_fn)( &sc->name, passwd );
368 }
369
370 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
371 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
372 static struct berval * pw_string(
373         const struct berval *sc,
374         const struct berval *passwd )
375 {
376         struct berval *pw = ber_memalloc( sizeof( struct berval ) );
377         if( pw == NULL ) return NULL;
378
379         pw->bv_len = sc->bv_len + passwd->bv_len;
380         pw->bv_val = ber_memalloc( pw->bv_len + 1 );
381
382         if( pw->bv_val == NULL ) {
383                 ber_memfree( pw );
384                 return NULL;
385         }
386
387         AC_MEMCPY( pw->bv_val, sc->bv_val, sc->bv_len );
388         AC_MEMCPY( &pw->bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
389
390         pw->bv_val[pw->bv_len] = '\0';
391         return pw;
392 }
393 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
394
395 static struct berval * pw_string64(
396         const struct berval *sc,
397         const struct berval *hash,
398         const struct berval *salt )
399 {
400         int rc;
401         struct berval string;
402         struct berval *b64 = ber_memalloc( sizeof(struct berval) );
403         size_t b64len;
404
405         if( b64 == NULL ) return NULL;
406
407         if( salt ) {
408                 /* need to base64 combined string */
409                 string.bv_len = hash->bv_len + salt->bv_len;
410                 string.bv_val = ber_memalloc( string.bv_len + 1 );
411
412                 if( string.bv_val == NULL ) {
413                         ber_memfree( b64 );
414                         return NULL;
415                 }
416
417                 AC_MEMCPY( string.bv_val, hash->bv_val,
418                         hash->bv_len );
419                 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
420                         salt->bv_len );
421                 string.bv_val[string.bv_len] = '\0';
422
423         } else {
424                 string = *hash;
425         }
426
427         b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
428         b64->bv_len = b64len + sc->bv_len;
429         b64->bv_val = ber_memalloc( b64->bv_len + 1 );
430
431         if( b64->bv_val == NULL ) {
432                 if( salt ) ber_memfree( string.bv_val );
433                 ber_memfree( b64 );
434                 return NULL;
435         }
436
437         AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
438
439         rc = lutil_b64_ntop(
440                 string.bv_val, string.bv_len,
441                 &b64->bv_val[sc->bv_len], b64len );
442
443         if( salt ) ber_memfree( string.bv_val );
444         
445         if( rc < 0 ) {
446                 ber_bvfree( b64 );
447                 return NULL;
448         }
449
450         /* recompute length */
451         b64->bv_len = sc->bv_len + rc;
452         assert( strlen(b64->bv_val) == b64->bv_len );
453         return b64;
454 }
455
456 /* PASSWORD CHECK ROUTINES */
457
458 #ifdef LUTIL_SHA1_BYTES
459 static int chk_ssha1(
460         const struct berval *sc,
461         const struct berval * passwd,
462         const struct berval * cred )
463 {
464         lutil_SHA1_CTX SHA1context;
465         unsigned char SHA1digest[LUTIL_SHA1_BYTES];
466         int rc;
467         unsigned char *orig_pass = NULL;
468
469         /* safety check */
470         if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(SHA1digest)) {
471                 return -1;
472         }
473
474         /* decode base64 password */
475         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
476                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
477
478         if( orig_pass == NULL ) return -1;
479
480         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
481
482         if (rc <= sizeof(SHA1digest)) {
483                 ber_memfree(orig_pass);
484                 return -1;
485         }
486  
487         /* hash credentials with salt */
488         lutil_SHA1Init(&SHA1context);
489         lutil_SHA1Update(&SHA1context,
490                 (const unsigned char *) cred->bv_val, cred->bv_len);
491         lutil_SHA1Update(&SHA1context,
492                 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
493                 rc - sizeof(SHA1digest));
494         lutil_SHA1Final(SHA1digest, &SHA1context);
495  
496         /* compare */
497         rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
498         ber_memfree(orig_pass);
499         return rc ? 1 : 0;
500 }
501
502 static int chk_sha1(
503         const struct berval *sc,
504         const struct berval * passwd,
505         const struct berval * cred )
506 {
507         lutil_SHA1_CTX SHA1context;
508         unsigned char SHA1digest[LUTIL_SHA1_BYTES];
509         int rc;
510         unsigned char *orig_pass = NULL;
511  
512         /* base64 un-encode password */
513         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
514                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
515
516         if( orig_pass == NULL ) return -1;
517
518         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
519
520         if( rc != sizeof(SHA1digest) ) {
521                 ber_memfree(orig_pass);
522                 return -1;
523         }
524  
525         /* hash credentials with salt */
526         lutil_SHA1Init(&SHA1context);
527         lutil_SHA1Update(&SHA1context,
528                 (const unsigned char *) cred->bv_val, cred->bv_len);
529         lutil_SHA1Final(SHA1digest, &SHA1context);
530  
531         /* compare */
532         rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
533         ber_memfree(orig_pass);
534         return rc ? 1 : 0;
535 }
536 #endif
537
538 static int chk_smd5(
539         const struct berval *sc,
540         const struct berval * passwd,
541         const struct berval * cred )
542 {
543         lutil_MD5_CTX MD5context;
544         unsigned char MD5digest[LUTIL_MD5_BYTES];
545         int rc;
546         unsigned char *orig_pass = NULL;
547
548         /* safety check */
549         if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(MD5digest)) {
550                 return -1;
551         }
552
553         /* base64 un-encode password */
554         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
555                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
556
557         if( orig_pass == NULL ) return -1;
558
559         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
560
561         if (rc <= sizeof(MD5digest)) {
562                 ber_memfree(orig_pass);
563                 return -1;
564         }
565
566         /* hash credentials with salt */
567         lutil_MD5Init(&MD5context);
568         lutil_MD5Update(&MD5context,
569                 (const unsigned char *) cred->bv_val,
570                 cred->bv_len );
571         lutil_MD5Update(&MD5context,
572                 &orig_pass[sizeof(MD5digest)],
573                 rc - sizeof(MD5digest));
574         lutil_MD5Final(MD5digest, &MD5context);
575
576         /* compare */
577         rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
578         ber_memfree(orig_pass);
579         return rc ? 1 : 0;
580 }
581
582 static int chk_md5(
583         const struct berval *sc,
584         const struct berval * passwd,
585         const struct berval * cred )
586 {
587         lutil_MD5_CTX MD5context;
588         unsigned char MD5digest[LUTIL_MD5_BYTES];
589         int rc;
590         unsigned char *orig_pass = NULL;
591
592         /* base64 un-encode password */
593         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
594                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
595
596         if( orig_pass == NULL ) return -1;
597
598         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
599         if ( rc != sizeof(MD5digest) ) {
600                 ber_memfree(orig_pass);
601                 return -1;
602         }
603
604         /* hash credentials with salt */
605         lutil_MD5Init(&MD5context);
606         lutil_MD5Update(&MD5context,
607                 (const unsigned char *) cred->bv_val,
608                 cred->bv_len );
609         lutil_MD5Final(MD5digest, &MD5context);
610
611         /* compare */
612         rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
613         ber_memfree(orig_pass);
614         return rc ? 1 : 0;
615 }
616
617 #ifdef SLAPD_LMHASH
618 static int chk_lanman(
619         const struct berval *scheme,
620         const struct berval *passwd,
621         const struct berval *cred )
622 {
623         struct berval *hash;
624
625         hash = hash_lanman( scheme, cred );
626         return memcmp( &hash->bv_val[scheme->bv_len], passwd->bv_val, 32);
627 }
628 #endif /* SLAPD_LMHASH */
629
630 #ifdef SLAPD_NS_MTA_MD5
631 static int chk_ns_mta_md5(
632         const struct berval *scheme,
633         const struct berval *passwd,
634         const struct berval *cred )
635 {
636         lutil_MD5_CTX MD5context;
637         unsigned char MD5digest[LUTIL_MD5_BYTES], c;
638         char buffer[LUTIL_MD5_BYTES + LUTIL_MD5_BYTES + 1];
639         int i;
640
641         /* hash credentials with salt */
642         lutil_MD5Init(&MD5context);
643         lutil_MD5Update(&MD5context,
644                 (const unsigned char *) &passwd->bv_val[32],
645                 32 );
646
647         c = 0x59;
648         lutil_MD5Update(&MD5context,
649                 (const unsigned char *) &c,
650                 1 );
651
652         lutil_MD5Update(&MD5context,
653                 (const unsigned char *) cred->bv_val,
654                 cred->bv_len );
655
656         c = 0xF7;
657         lutil_MD5Update(&MD5context,
658                 (const unsigned char *) &c,
659                 1 );
660
661         lutil_MD5Update(&MD5context,
662                 (const unsigned char *) &passwd->bv_val[32],
663                 32 );
664
665         lutil_MD5Final(MD5digest, &MD5context);
666
667         for( i=0; i < sizeof( MD5digest ); i++ ) {
668                 buffer[i+i]   = "0123456789abcdef"[(MD5digest[i]>>4) & 0x0F]; 
669                 buffer[i+i+1] = "0123456789abcdef"[ MD5digest[i] & 0x0F]; 
670         }
671
672         /* compare */
673         return memcmp((char *)passwd->bv_val, (char *)buffer, sizeof(buffer))
674                 ? 1 : 0;
675 }
676 #endif
677
678 #ifdef SLAPD_SPASSWD
679 #ifdef HAVE_CYRUS_SASL
680 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
681 #endif
682
683 static int chk_sasl(
684         const struct berval *sc,
685         const struct berval * passwd,
686         const struct berval * cred )
687 {
688         unsigned int i;
689         int rtn;
690
691         for( i=0; i<cred->bv_len; i++) {
692                 if(cred->bv_val[i] == '\0') {
693                         return 1;       /* NUL character in password */
694                 }
695         }
696
697         if( cred->bv_val[i] != '\0' ) {
698                 return 1;       /* cred must behave like a string */
699         }
700
701         for( i=0; i<passwd->bv_len; i++) {
702                 if(passwd->bv_val[i] == '\0') {
703                         return 1;       /* NUL character in password */
704                 }
705         }
706
707         if( passwd->bv_val[i] != '\0' ) {
708                 return 1;       /* passwd must behave like a string */
709         }
710
711         rtn = 1;
712
713 #ifdef HAVE_CYRUS_SASL
714         if( lutil_passwd_sasl_conn != NULL ) {
715                 int sc;
716 # if SASL_VERSION_MAJOR < 2
717                 const char *errstr = NULL;
718                 sc = sasl_checkpass( lutil_passwd_sasl_conn,
719                         passwd->bv_val, passwd->bv_len,
720                         cred->bv_val, cred->bv_len,
721                         &errstr );
722 # else
723                 sc = sasl_checkpass( lutil_passwd_sasl_conn,
724                         passwd->bv_val, passwd->bv_len,
725                         cred->bv_val, cred->bv_len );
726 # endif
727                 rtn = ( sc != SASL_OK );
728         }
729 #endif
730
731         return rtn;
732 }
733 #endif
734
735 #ifdef SLAPD_KPASSWD
736 static int chk_kerberos(
737         const struct berval *sc,
738         const struct berval * passwd,
739         const struct berval * cred )
740 {
741         unsigned int i;
742         int rtn;
743
744         for( i=0; i<cred->bv_len; i++) {
745                 if(cred->bv_val[i] == '\0') {
746                         return 1;       /* NUL character in password */
747                 }
748         }
749
750         if( cred->bv_val[i] != '\0' ) {
751                 return 1;       /* cred must behave like a string */
752         }
753
754         for( i=0; i<passwd->bv_len; i++) {
755                 if(passwd->bv_val[i] == '\0') {
756                         return 1;       /* NUL character in password */
757                 }
758         }
759
760         if( passwd->bv_val[i] != '\0' ) {
761                 return 1;       /* passwd must behave like a string */
762         }
763
764         rtn = 1;
765
766 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
767         {
768 /* Portions:
769  * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
770  * (Royal Institute of Technology, Stockholm, Sweden).
771  * All rights reserved.
772  *
773  * Redistribution and use in source and binary forms, with or without
774  * modification, are permitted provided that the following conditions
775  * are met:
776  *
777  * 1. Redistributions of source code must retain the above copyright
778  *    notice, this list of conditions and the following disclaimer.
779  *
780  * 2. Redistributions in binary form must reproduce the above copyright
781  *    notice, this list of conditions and the following disclaimer in the
782  *    documentation and/or other materials provided with the distribution.
783  *
784  * 3. Neither the name of the Institute nor the names of its contributors
785  *    may be used to endorse or promote products derived from this software
786  *    without specific prior written permission.
787  *
788  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
789  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
790  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
791  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
792  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
793  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
794  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
795  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
796  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
797  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
798  * SUCH DAMAGE.
799  */
800
801                 krb5_context context;
802                 krb5_error_code ret;
803                 krb5_creds creds;
804                 krb5_get_init_creds_opt get_options;
805                 krb5_verify_init_creds_opt verify_options;
806                 krb5_principal client, server;
807 #ifdef notdef
808                 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
809 #endif
810
811                 ret = krb5_init_context( &context );
812                 if (ret) {
813                         return 1;
814                 }
815
816 #ifdef notdef
817                 krb5_get_init_creds_opt_set_preauth_list(&get_options,
818                         pre_auth_types, 1);
819 #endif
820
821                 krb5_get_init_creds_opt_init( &get_options );
822
823                 krb5_verify_init_creds_opt_init( &verify_options );
824         
825                 ret = krb5_parse_name( context, passwd->bv_val, &client );
826
827                 if (ret) {
828                         krb5_free_context( context );
829                         return 1;
830                 }
831
832                 ret = krb5_get_init_creds_password( context,
833                         &creds, client, cred->bv_val, NULL,
834                         NULL, 0, NULL, &get_options );
835
836                 if (ret) {
837                         krb5_free_principal( context, client );
838                         krb5_free_context( context );
839                         return 1;
840                 }
841
842                 {
843                         char *host = ldap_pvt_get_fqdn( NULL );
844
845                         if( host == NULL ) {
846                                 krb5_free_principal( context, client );
847                                 krb5_free_context( context );
848                                 return 1;
849                         }
850
851                         ret = krb5_sname_to_principal( context,
852                                 host, "ldap", KRB5_NT_SRV_HST, &server );
853
854                         ber_memfree( host );
855                 }
856
857                 if (ret) {
858                         krb5_free_principal( context, client );
859                         krb5_free_context( context );
860                         return 1;
861                 }
862
863                 ret = krb5_verify_init_creds( context,
864                         &creds, server, NULL, NULL, &verify_options );
865
866                 krb5_free_principal( context, client );
867                 krb5_free_principal( context, server );
868                 krb5_free_cred_contents( context, &creds );
869                 krb5_free_context( context );
870
871                 rtn = !!ret;
872         }
873 #elif   defined(HAVE_KRB4)
874         {
875                 /* Borrowed from Heimdal kpopper */
876 /* Portions:
877  * Copyright (c) 1989 Regents of the University of California.
878  * All rights reserved.  The Berkeley software License Agreement
879  * specifies the terms and conditions for redistribution.
880  */
881
882                 int status;
883                 char lrealm[REALM_SZ];
884                 char tkt[MAXHOSTNAMELEN];
885
886                 status = krb_get_lrealm(lrealm,1);
887                 if (status == KFAILURE) {
888                         return 1;
889                 }
890
891                 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
892                         TKT_ROOT, (unsigned)getpid());
893                 krb_set_tkt_string (tkt);
894
895                 status = krb_verify_user( passwd->bv_val, "", lrealm,
896                         cred->bv_val, 1, "ldap");
897
898                 dest_tkt(); /* no point in keeping the tickets */
899
900                 return status == KFAILURE;
901         }
902 #endif
903
904         return rtn;
905 }
906 #endif /* SLAPD_KPASSWD */
907
908 #ifdef SLAPD_CRYPT
909 static int chk_crypt(
910         const struct berval *sc,
911         const struct berval * passwd,
912         const struct berval * cred )
913 {
914         char *cr;
915         unsigned int i;
916
917         for( i=0; i<cred->bv_len; i++) {
918                 if(cred->bv_val[i] == '\0') {
919                         return 1;       /* NUL character in password */
920                 }
921         }
922
923         if( cred->bv_val[i] != '\0' ) {
924                 return -1;      /* cred must behave like a string */
925         }
926
927         if( passwd->bv_len < 2 ) {
928                 return -1;      /* passwd must be at least two characters long */
929         }
930
931         for( i=0; i<passwd->bv_len; i++) {
932                 if(passwd->bv_val[i] == '\0') {
933                         return -1;      /* NUL character in password */
934                 }
935         }
936
937         if( passwd->bv_val[i] != '\0' ) {
938                 return -1;      /* passwd must behave like a string */
939         }
940
941         cr = crypt( cred->bv_val, passwd->bv_val );
942
943         if( cr == NULL || cr[0] == '\0' ) {
944                 /* salt must have been invalid */
945                 return -1;
946         }
947
948         return strcmp( passwd->bv_val, cr ) ? 1 : 0;
949 }
950
951 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
952 static int chk_unix(
953         const struct berval *sc,
954         const struct berval * passwd,
955         const struct berval * cred )
956 {
957         unsigned int i;
958         char *pw,*cr;
959
960         for( i=0; i<cred->bv_len; i++) {
961                 if(cred->bv_val[i] == '\0') {
962                         return -1;      /* NUL character in password */
963                 }
964         }
965         if( cred->bv_val[i] != '\0' ) {
966                 return -1;      /* cred must behave like a string */
967         }
968
969         for( i=0; i<passwd->bv_len; i++) {
970                 if(passwd->bv_val[i] == '\0') {
971                         return -1;      /* NUL character in password */
972                 }
973         }
974
975         if( passwd->bv_val[i] != '\0' ) {
976                 return -1;      /* passwd must behave like a string */
977         }
978
979         {
980                 struct passwd *pwd = getpwnam(passwd->bv_val);
981
982                 if(pwd == NULL) {
983                         return -1;      /* not found */
984                 }
985
986                 pw = pwd->pw_passwd;
987         }
988 #  ifdef HAVE_GETSPNAM
989         {
990                 struct spwd *spwd = getspnam(passwd->bv_val);
991
992                 if(spwd != NULL) {
993                         pw = spwd->sp_pwdp;
994                 }
995         }
996 #  endif
997 #  ifdef HAVE_AIX_SECURITY
998         {
999                 struct userpw *upw = getuserpw(passwd->bv_val);
1000
1001                 if (upw != NULL) {
1002                         pw = upw->upw_passwd;
1003                 }
1004         }
1005 #  endif
1006
1007         if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
1008                 /* password must must be at least two characters long */
1009                 return -1;
1010         }
1011
1012         cr = crypt(cred->bv_val, pw);
1013
1014         if( cr == NULL || cr[0] == '\0' ) {
1015                 /* salt must have been invalid */
1016                 return -1;
1017         }
1018
1019         return strcmp(pw, cr) ? 1 : 0;
1020
1021 }
1022 # endif
1023 #endif
1024
1025 /* PASSWORD GENERATION ROUTINES */
1026
1027 #ifdef LUTIL_SHA1_BYTES
1028 static struct berval *hash_ssha1(
1029         const struct berval *scheme,
1030         const struct berval  *passwd )
1031 {
1032         lutil_SHA1_CTX  SHA1context;
1033         unsigned char   SHA1digest[LUTIL_SHA1_BYTES];
1034         unsigned char   saltdata[4];
1035         struct berval digest;
1036         struct berval salt;
1037
1038         digest.bv_val = SHA1digest;
1039         digest.bv_len = sizeof(SHA1digest);
1040         salt.bv_val = saltdata;
1041         salt.bv_len = sizeof(saltdata);
1042
1043         if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1044                 return NULL; 
1045         }
1046
1047         lutil_SHA1Init( &SHA1context );
1048         lutil_SHA1Update( &SHA1context,
1049                 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1050         lutil_SHA1Update( &SHA1context,
1051                 (const unsigned char *)salt.bv_val, salt.bv_len );
1052         lutil_SHA1Final( SHA1digest, &SHA1context );
1053
1054         return pw_string64( scheme, &digest, &salt);
1055 }
1056
1057 static struct berval *hash_sha1(
1058         const struct berval *scheme,
1059         const struct berval  *passwd )
1060 {
1061         lutil_SHA1_CTX  SHA1context;
1062         unsigned char   SHA1digest[LUTIL_SHA1_BYTES];
1063         struct berval digest;
1064         digest.bv_val = SHA1digest;
1065         digest.bv_len = sizeof(SHA1digest);
1066      
1067         lutil_SHA1Init( &SHA1context );
1068         lutil_SHA1Update( &SHA1context,
1069                 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1070         lutil_SHA1Final( SHA1digest, &SHA1context );
1071             
1072         return pw_string64( scheme, &digest, NULL);
1073 }
1074 #endif
1075
1076 static struct berval *hash_smd5(
1077         const struct berval *scheme,
1078         const struct berval  *passwd )
1079 {
1080         lutil_MD5_CTX   MD5context;
1081         unsigned char   MD5digest[LUTIL_MD5_BYTES];
1082         unsigned char   saltdata[4];
1083         struct berval digest;
1084         struct berval salt;
1085
1086         digest.bv_val = MD5digest;
1087         digest.bv_len = sizeof(MD5digest);
1088         salt.bv_val = saltdata;
1089         salt.bv_len = sizeof(saltdata);
1090
1091         if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1092                 return NULL; 
1093         }
1094
1095         lutil_MD5Init( &MD5context );
1096         lutil_MD5Update( &MD5context,
1097                 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1098         lutil_MD5Update( &MD5context,
1099                 (const unsigned char *) salt.bv_val, salt.bv_len );
1100         lutil_MD5Final( MD5digest, &MD5context );
1101
1102         return pw_string64( scheme, &digest, &salt );
1103 }
1104
1105 static struct berval *hash_md5(
1106         const struct berval *scheme,
1107         const struct berval  *passwd )
1108 {
1109         lutil_MD5_CTX   MD5context;
1110         unsigned char   MD5digest[LUTIL_MD5_BYTES];
1111
1112         struct berval digest;
1113
1114         digest.bv_val = MD5digest;
1115         digest.bv_len = sizeof(MD5digest);
1116
1117         lutil_MD5Init( &MD5context );
1118         lutil_MD5Update( &MD5context,
1119                 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1120         lutil_MD5Final( MD5digest, &MD5context );
1121
1122         return pw_string64( scheme, &digest, NULL );
1123 ;
1124 }
1125
1126 #ifdef SLAPD_LMHASH 
1127 /* pseudocode from RFC2433
1128  * A.2 LmPasswordHash()
1129  * 
1130  *    LmPasswordHash(
1131  *    IN  0-to-14-oem-char Password,
1132  *    OUT 16-octet         PasswordHash )
1133  *    {
1134  *       Set UcasePassword to the uppercased Password
1135  *       Zero pad UcasePassword to 14 characters
1136  * 
1137  *       DesHash( 1st 7-octets of UcasePassword,
1138  *                giving 1st 8-octets of PasswordHash )
1139  * 
1140  *       DesHash( 2nd 7-octets of UcasePassword,
1141  *                giving 2nd 8-octets of PasswordHash )
1142  *    }
1143  * 
1144  * 
1145  * A.3 DesHash()
1146  * 
1147  *    DesHash(
1148  *    IN  7-octet Clear,
1149  *    OUT 8-octet Cypher )
1150  *    {
1151  *        *
1152  *        * Make Cypher an irreversibly encrypted form of Clear by
1153  *        * encrypting known text using Clear as the secret key.
1154  *        * The known text consists of the string
1155  *        *
1156  *        *              KGS!@#$%
1157  *        *
1158  * 
1159  *       Set StdText to "KGS!@#$%"
1160  *       DesEncrypt( StdText, Clear, giving Cypher )
1161  *    }
1162  * 
1163  * 
1164  * A.4 DesEncrypt()
1165  * 
1166  *    DesEncrypt(
1167  *    IN  8-octet Clear,
1168  *    IN  7-octet Key,
1169  *    OUT 8-octet Cypher )
1170  *    {
1171  *        *
1172  *        * Use the DES encryption algorithm [4] in ECB mode [9]
1173  *        * to encrypt Clear into Cypher such that Cypher can
1174  *        * only be decrypted back to Clear by providing Key.
1175  *        * Note that the DES algorithm takes as input a 64-bit
1176  *        * stream where the 8th, 16th, 24th, etc.  bits are
1177  *        * parity bits ignored by the encrypting algorithm.
1178  *        * Unless you write your own DES to accept 56-bit input
1179  *        * without parity, you will need to insert the parity bits
1180  *        * yourself.
1181  *        *
1182  *    }
1183  */
1184
1185 static void lmPasswd_to_key(
1186         const unsigned char *lmPasswd,
1187         des_cblock *key)
1188 {
1189         /* make room for parity bits */
1190         ((char *)key)[0] = lmPasswd[0];
1191         ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
1192         ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
1193         ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
1194         ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
1195         ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
1196         ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
1197         ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
1198                 
1199         des_set_odd_parity( key );
1200 }       
1201
1202 static struct berval *hash_lanman(
1203         const struct berval *scheme,
1204         const struct berval *passwd )
1205 {
1206
1207         int i;
1208         char UcasePassword[15];
1209         des_cblock key;
1210         des_key_schedule schedule;
1211         des_cblock StdText = "KGS!@#$%";
1212         des_cblock hash1, hash2;
1213         char lmhash[33];
1214         struct berval hash;
1215         
1216         for( i=0; i<passwd->bv_len; i++) {
1217                 if(passwd->bv_val[i] == '\0') {
1218                         return NULL;    /* NUL character in password */
1219                 }
1220         }
1221         
1222         if( passwd->bv_val[i] != '\0' ) {
1223                 return NULL;    /* passwd must behave like a string */
1224         }
1225         
1226         strncpy( UcasePassword, passwd->bv_val, 14 );
1227         UcasePassword[14] = '\0';
1228         ldap_pvt_str2upper( UcasePassword );
1229         
1230         lmPasswd_to_key( UcasePassword, &key );
1231         des_set_key_unchecked( &key, schedule );
1232         des_ecb_encrypt( &StdText, &hash1, schedule , DES_ENCRYPT );
1233         
1234         lmPasswd_to_key( &UcasePassword[7], &key );
1235         des_set_key_unchecked( &key, schedule );
1236         des_ecb_encrypt( &StdText, &hash2, schedule , DES_ENCRYPT );
1237         
1238         sprintf( lmhash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", 
1239                 hash1[0],hash1[1],hash1[2],hash1[3],hash1[4],hash1[5],hash1[6],hash1[7],
1240                 hash2[0],hash2[1],hash2[2],hash2[3],hash2[4],hash2[5],hash2[6],hash2[7] );
1241         
1242         hash.bv_val = lmhash;
1243         hash.bv_len = 32;
1244         
1245         return pw_string( scheme, &hash );
1246 }
1247 #endif /* SLAPD_LMHASH */
1248
1249 #ifdef SLAPD_CRYPT
1250 static struct berval *hash_crypt(
1251         const struct berval *scheme,
1252         const struct berval *passwd )
1253 {
1254         struct berval hash;
1255         unsigned char salt[32]; /* salt suitable for most anything */
1256         unsigned int i;
1257
1258         for( i=0; i<passwd->bv_len; i++) {
1259                 if(passwd->bv_val[i] == '\0') {
1260                         return NULL;    /* NUL character in password */
1261                 }
1262         }
1263
1264         if( passwd->bv_val[i] != '\0' ) {
1265                 return NULL;    /* passwd must behave like a string */
1266         }
1267
1268         if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1269                 return NULL; 
1270         }
1271
1272         for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1273                 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1274         }
1275         salt[sizeof( salt ) - 1 ] = '\0';
1276
1277         if( salt_format != NULL ) {
1278                 /* copy the salt we made into entropy before snprintfing
1279                    it back into the salt */
1280                 char entropy[sizeof(salt)];
1281                 strcpy( entropy, salt );
1282                 snprintf( salt, sizeof(entropy), salt_format, entropy );
1283         }
1284
1285         hash.bv_val = crypt( passwd->bv_val, salt );
1286
1287         if( hash.bv_val == NULL ) return NULL;
1288
1289         hash.bv_len = strlen( hash.bv_val );
1290
1291         if( hash.bv_len == 0 ) {
1292                 return NULL;
1293         }
1294
1295         return pw_string( scheme, &hash );
1296 }
1297 #endif
1298
1299 int lutil_salt_format(const char *format)
1300 {
1301 #ifdef SLAPD_CRYPT
1302         free( salt_format );
1303
1304         salt_format = format != NULL ? strdup( format ) : NULL;
1305 #endif
1306
1307         return 0;
1308 }
1309
1310 #ifdef SLAPD_CLEARTEXT
1311 static struct berval *hash_clear(
1312         const struct berval *scheme,
1313         const struct berval  *passwd )
1314 {
1315         return ber_bvdup( (struct berval *) passwd );
1316 }
1317 #endif
1318