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