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