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