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