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