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