]> git.sur5r.net Git - openldap/blob - libraries/liblutil/passwd.c
Fix ldaps / TLS processing...
[openldap] / libraries / liblutil / passwd.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 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
25 #ifdef SLAPD_SPASSWD
26 #       include <sasl.h>
27 #endif
28
29 #ifdef SLAPD_KPASSWD
30 #       include <ac/krb.h>
31 #       include <ac/krb5.h>
32 #endif
33
34 #include <ac/param.h>
35
36 #include <ac/unistd.h>
37 #include <ac/crypt.h>
38
39 #ifdef HAVE_SHADOW_H
40 #       include <shadow.h>
41 #endif
42 #ifdef HAVE_PWD_H
43 #       include <pwd.h>
44 #endif
45 #ifdef HAVE_AIX_SECURITY
46 #       include <userpw.h>
47 #endif
48
49 #include <lber.h>
50
51 #include "lutil_md5.h"
52 #include "lutil_sha1.h"
53 #include "lutil.h"
54
55 static const unsigned char crypt64[] =
56         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
57
58 struct pw_scheme;
59
60 typedef int (*PASSWD_CHK_FUNC)(
61         const struct pw_scheme *scheme,
62         const struct berval *passwd,
63         const struct berval *cred );
64
65 typedef struct berval * (*PASSWD_HASH_FUNC) (
66         const struct pw_scheme *scheme,
67         const struct berval *passwd );
68
69 struct pw_scheme {
70         struct berval name;
71         PASSWD_CHK_FUNC chk_fn;
72         PASSWD_HASH_FUNC hash_fn;
73 };
74
75 /* password check routines */
76 static int chk_md5(
77         const struct pw_scheme *scheme,
78         const struct berval *passwd,
79         const struct berval *cred );
80
81 static int chk_smd5(
82         const struct pw_scheme *scheme,
83         const struct berval *passwd,
84         const struct berval *cred );
85
86 static int chk_ssha1(
87         const struct pw_scheme *scheme,
88         const struct berval *passwd,
89         const struct berval *cred );
90
91 static int chk_sha1(
92         const struct pw_scheme *scheme,
93         const struct berval *passwd,
94         const struct berval *cred );
95
96 #ifdef SLAPD_SPASSWD
97 static int chk_sasl(
98         const struct pw_scheme *scheme,
99         const struct berval *passwd,
100         const struct berval *cred );
101 #endif
102
103 #ifdef SLAPD_KPASSWD
104 static int chk_kerberos(
105         const struct pw_scheme *scheme,
106         const struct berval *passwd,
107         const struct berval *cred );
108 #endif
109
110 #ifdef SLAPD_CRYPT
111 static int chk_crypt(
112         const struct pw_scheme *scheme,
113         const struct berval *passwd,
114         const struct berval *cred );
115
116 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
117 static int chk_unix(
118         const struct pw_scheme *scheme,
119         const struct berval *passwd,
120         const struct berval *cred );
121 #endif
122 #endif
123
124
125 /* password hash routines */
126 static struct berval *hash_sha1(
127         const struct pw_scheme *scheme,
128         const struct berval *passwd );
129
130 static struct berval *hash_ssha1(
131         const struct pw_scheme *scheme,
132         const struct berval *passwd );
133
134 static struct berval *hash_smd5(
135         const struct pw_scheme *scheme,
136         const struct berval *passwd );
137
138 static struct berval *hash_md5(
139         const struct pw_scheme *scheme,
140         const struct berval *passwd );
141
142 #ifdef SLAPD_CRYPT
143 static struct berval *hash_crypt(
144         const struct pw_scheme *scheme,
145         const struct berval *passwd );
146 #endif
147
148
149 static const struct pw_scheme pw_schemes[] =
150 {
151         { {sizeof("{SSHA}")-1, "{SSHA}"},       chk_ssha1, hash_ssha1 },
152         { {sizeof("{SHA}")-1, "{SHA}"},         chk_sha1, hash_sha1 },
153
154         { {sizeof("{SMD5}")-1, "{SMD5}"},       chk_smd5, hash_smd5 },
155         { {sizeof("{MD5}")-1, "{MD5}"},         chk_md5, hash_md5 },
156
157 #ifdef SLAPD_SPASSWD
158         { {sizeof("{SASL}")-1, "{SASL}"}, chk_sasl, NULL },
159 #endif
160
161 #ifdef SLAPD_KPASSWD
162         { {sizeof("{KERBEROS}")-1, "{KERBEROS}"}, chk_kerberos, NULL },
163 #endif
164
165 #ifdef SLAPD_CRYPT
166         { {sizeof("{CRYPT}")-1, "{CRYPT}"},     chk_crypt, hash_crypt },
167 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
168         { {sizeof("{UNIX}")-1, "{UNIX}"},       chk_unix, NULL },
169 # endif
170 #endif
171
172 #ifdef SLAPD_CLEARTEXT
173         /* psuedo scheme */
174         { {0, "{CLEARTEXT}"}, NULL, NULL },
175 #endif
176
177         { {0, NULL}, NULL, NULL }
178 };
179
180 static const struct pw_scheme *get_scheme(
181         const char* scheme )
182 {
183         int i;
184
185         for( i=0; pw_schemes[i].name.bv_val; i++) {
186                 if( pw_schemes[i].name.bv_len == 0 ) continue;
187
188                 if( strncasecmp(scheme, pw_schemes[i].name.bv_val,
189                         pw_schemes[i].name.bv_len) == 0 )
190                 {
191                         return &pw_schemes[i];
192                 }
193         }
194
195         return NULL;
196 }
197
198 int lutil_passwd_scheme(
199         const char* scheme )
200 {
201         if( scheme == NULL ) {
202                 return 0;
203         }
204
205         return get_scheme(scheme) != NULL;
206 }
207
208
209 static int is_allowed_scheme( 
210         const char* scheme,
211         const char** schemes )
212 {
213         int i;
214
215         if( schemes == NULL ) return 1;
216
217         for( i=0; schemes[i] != NULL; i++ ) {
218                 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
219                         return 1;
220                 }
221         }
222         return 0;
223 }
224
225 static struct berval *passwd_scheme(
226         const struct pw_scheme *scheme,
227         const struct berval * passwd,
228         const char** allowed )
229 {
230         if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
231                 return NULL;
232         }
233
234         if( passwd->bv_len >= scheme->name.bv_len ) {
235                 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
236                         struct berval *bv = ber_memalloc( sizeof(struct berval) );
237
238                         if( bv == NULL ) return NULL;
239
240                         bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
241                         bv->bv_len = passwd->bv_len - scheme->name.bv_len;
242
243                         return bv;
244                 }
245         }
246
247         return NULL;
248 }
249
250 /*
251  * Return 0 if creds are good.
252  */
253 int
254 lutil_passwd(
255         const struct berval *passwd,    /* stored passwd */
256         const struct berval *cred,              /* user cred */
257         const char **schemes )
258 {
259         int i;
260
261         if (cred == NULL || cred->bv_len == 0 ||
262                 passwd == NULL || passwd->bv_len == 0 )
263         {
264                 return -1;
265         }
266
267         for( i=0; pw_schemes[i].name.bv_val != NULL; i++ ) {
268                 if( pw_schemes[i].chk_fn ) {
269                         struct berval *p = passwd_scheme( &pw_schemes[i],
270                                 passwd, schemes );
271
272                         if( p != NULL ) {
273                                 int rc = (pw_schemes[i].chk_fn)( &pw_schemes[i], p, cred );
274
275                                 /* only free the berval structure as the bv_val points
276                                  * into passwd->bv_val
277                                  */
278                                 ber_memfree( p );
279                                 
280                                 return rc;
281                         }
282                 }
283         }
284
285 #ifdef SLAPD_CLEARTEXT
286         if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
287                 return passwd->bv_len == cred->bv_len
288                         ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
289                         : 1;
290         }
291 #else
292         return 1;
293 #endif
294
295 }
296
297 struct berval * lutil_passwd_generate( ber_len_t len )
298 {
299         struct berval *pw;
300
301         if( len < 1 ) return NULL;
302
303         pw = ber_memalloc( sizeof( struct berval ) );
304         if( pw == NULL ) return NULL;
305
306         pw->bv_len = len;
307         pw->bv_val = ber_memalloc( len + 1 );
308
309         if( pw->bv_val == NULL ) {
310                 ber_memfree( pw );
311                 return NULL;
312         }
313
314         if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
315                 ber_bvfree( pw );
316                 return NULL; 
317         }
318
319         for( len = 0; len < pw->bv_len; len++ ) {
320                 pw->bv_val[len] = crypt64[
321                         pw->bv_val[len] % (sizeof(crypt64)-1) ];
322         }
323
324         pw->bv_val[len] = '\0';
325         
326         return pw;
327 }
328
329 struct berval * lutil_passwd_hash(
330         const struct berval * passwd,
331         const char * method )
332 {
333         const struct pw_scheme *sc = get_scheme( method );
334
335         if( sc == NULL ) return NULL;
336         if( ! sc->hash_fn ) return NULL;
337
338         return (sc->hash_fn)( sc, passwd );
339 }
340
341 static struct berval * pw_string(
342         const struct pw_scheme *sc,
343         const struct berval *passwd )
344 {
345         struct berval *pw = ber_memalloc( sizeof( struct berval ) );
346         if( pw == NULL ) return NULL;
347
348         pw->bv_len = sc->name.bv_len + passwd->bv_len;
349         pw->bv_val = ber_memalloc( pw->bv_len + 1 );
350
351         if( pw->bv_val == NULL ) {
352                 ber_memfree( pw );
353                 return NULL;
354         }
355
356         memcpy( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
357         memcpy( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
358
359         pw->bv_val[pw->bv_len] = '\0';
360         return pw;
361 }
362
363 static struct berval * pw_string64(
364         const struct pw_scheme *sc,
365         const struct berval *hash,
366         const struct berval *salt )
367 {
368         int rc;
369         struct berval string;
370         struct berval *b64 = ber_memalloc( sizeof(struct berval) );
371         size_t b64len;
372
373         if( b64 == NULL ) return NULL;
374
375         if( salt ) {
376                 /* need to base64 combined string */
377                 string.bv_len = hash->bv_len + salt->bv_len;
378                 string.bv_val = ber_memalloc( string.bv_len + 1 );
379
380                 if( string.bv_val == NULL ) {
381                         ber_memfree( b64 );
382                         return NULL;
383                 }
384
385                 memcpy( string.bv_val, hash->bv_val,
386                         hash->bv_len );
387                 memcpy( &string.bv_val[hash->bv_len], salt->bv_val,
388                         salt->bv_len );
389                 string.bv_val[string.bv_len] = '\0';
390
391         } else {
392                 string = *hash;
393         }
394
395         b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
396         b64->bv_len = b64len + sc->name.bv_len;
397         b64->bv_val = ber_memalloc( b64->bv_len + 1 );
398
399         if( b64->bv_val == NULL ) {
400                 if( salt ) ber_memfree( string.bv_val );
401                 ber_memfree( b64 );
402                 return NULL;
403         }
404
405         memcpy(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
406
407         rc = lutil_b64_ntop(
408                 string.bv_val, string.bv_len,
409                 &b64->bv_val[sc->name.bv_len], b64len );
410
411         if( salt ) ber_memfree( string.bv_val );
412         
413         if( rc < 0 ) {
414                 ber_bvfree( b64 );
415                 return NULL;
416         }
417
418         /* recompute length */
419         b64->bv_len = sc->name.bv_len + rc;
420         assert( strlen(b64->bv_val) == b64->bv_len );
421         return b64;
422 }
423
424 /* PASSWORD CHECK ROUTINES */
425
426 static int chk_ssha1(
427         const struct pw_scheme *sc,
428         const struct berval * passwd,
429         const struct berval * cred )
430 {
431         lutil_SHA1_CTX SHA1context;
432         unsigned char SHA1digest[LUTIL_SHA1_BYTES];
433         int rc;
434         unsigned char *orig_pass = NULL;
435  
436         /* decode base64 password */
437         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
438                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
439
440         if( orig_pass == NULL ) return -1;
441
442         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
443
444         if(rc < 0) {
445                 ber_memfree(orig_pass);
446                 return -1;
447         }
448  
449         /* hash credentials with salt */
450         lutil_SHA1Init(&SHA1context);
451         lutil_SHA1Update(&SHA1context,
452                 (const unsigned char *) cred->bv_val, cred->bv_len);
453         lutil_SHA1Update(&SHA1context,
454                 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
455                 rc - sizeof(SHA1digest));
456         lutil_SHA1Final(SHA1digest, &SHA1context);
457  
458         /* compare */
459         rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
460         ber_memfree(orig_pass);
461         return rc ? 1 : 0;
462 }
463
464 static int chk_sha1(
465         const struct pw_scheme *sc,
466         const struct berval * passwd,
467         const struct berval * cred )
468 {
469         lutil_SHA1_CTX SHA1context;
470         unsigned char SHA1digest[LUTIL_SHA1_BYTES];
471         int rc;
472         unsigned char *orig_pass = NULL;
473  
474         /* base64 un-encode password */
475         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
476                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
477
478         if( orig_pass == NULL ) return -1;
479
480         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
481
482         if( rc != sizeof(SHA1digest) ) {
483                 ber_memfree(orig_pass);
484                 return -1;
485         }
486  
487         /* hash credentials with salt */
488         lutil_SHA1Init(&SHA1context);
489         lutil_SHA1Update(&SHA1context,
490                 (const unsigned char *) cred->bv_val, cred->bv_len);
491         lutil_SHA1Final(SHA1digest, &SHA1context);
492  
493         /* compare */
494         rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
495         ber_memfree(orig_pass);
496         return rc ? 1 : 0;
497 }
498
499 static int chk_smd5(
500         const struct pw_scheme *sc,
501         const struct berval * passwd,
502         const struct berval * cred )
503 {
504         lutil_MD5_CTX MD5context;
505         unsigned char MD5digest[LUTIL_MD5_BYTES];
506         int rc;
507         unsigned char *orig_pass = NULL;
508
509         /* base64 un-encode password */
510         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
511                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
512
513         if( orig_pass == NULL ) return -1;
514
515         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
516         if ( rc < 0 ) {
517                 ber_memfree(orig_pass);
518                 return -1;
519         }
520
521         /* hash credentials with salt */
522         lutil_MD5Init(&MD5context);
523         lutil_MD5Update(&MD5context,
524                 (const unsigned char *) cred->bv_val, cred->bv_len );
525         lutil_MD5Update(&MD5context,
526                 (const unsigned char *) &orig_pass[sizeof(MD5digest)],
527                 rc - sizeof(MD5digest));
528         lutil_MD5Final(MD5digest, &MD5context);
529
530         /* compare */
531         rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
532         ber_memfree(orig_pass);
533         return rc ? 1 : 0;
534 }
535
536 static int chk_md5(
537         const struct pw_scheme *sc,
538         const struct berval * passwd,
539         const struct berval * cred )
540 {
541         lutil_MD5_CTX MD5context;
542         unsigned char MD5digest[LUTIL_MD5_BYTES];
543         int rc;
544         unsigned char *orig_pass = NULL;
545
546         /* base64 un-encode password */
547         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
548                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
549
550         if( orig_pass == NULL ) return -1;
551
552         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
553         if ( rc != sizeof(MD5digest) ) {
554                 ber_memfree(orig_pass);
555                 return -1;
556         }
557
558         /* hash credentials with salt */
559         lutil_MD5Init(&MD5context);
560         lutil_MD5Update(&MD5context,
561                 (const unsigned char *) cred->bv_val, cred->bv_len );
562         lutil_MD5Final(MD5digest, &MD5context);
563
564         /* compare */
565         rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
566         ber_memfree(orig_pass);
567         return rc ? 1 : 0;
568 }
569
570 #ifdef SLAPD_SPASSWD
571 #ifdef HAVE_CYRUS_SASL
572 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
573 #endif
574
575 static int chk_sasl(
576         const struct pw_scheme *sc,
577         const struct berval * passwd,
578         const struct berval * cred )
579 {
580         int i;
581         int rtn;
582
583         for( i=0; i<cred->bv_len; i++) {
584                 if(cred->bv_val[i] == '\0') {
585                         return 1;       /* NUL character in password */
586                 }
587         }
588
589         if( cred->bv_val[i] != '\0' ) {
590                 return 1;       /* cred must behave like a string */
591         }
592
593         for( i=0; i<passwd->bv_len; i++) {
594                 if(passwd->bv_val[i] == '\0') {
595                         return 1;       /* NUL character in password */
596                 }
597         }
598
599         if( passwd->bv_val[i] != '\0' ) {
600                 return 1;       /* passwd must behave like a string */
601         }
602
603         rtn = 1;
604
605 #ifdef HAVE_CYRUS_SASL
606         if( lutil_passwd_sasl_conn != NULL ) {
607                 const char *errstr = NULL;
608                 int sc;
609
610                 sc = sasl_checkpass( lutil_passwd_sasl_conn,
611                         passwd->bv_val, passwd->bv_len,
612                         cred->bv_val, cred->bv_len,
613                         &errstr );
614
615                 rtn = ( sc != SASL_OK );
616         }
617 #endif
618
619         return rtn;
620 }
621 #endif
622
623 #ifdef SLAPD_KPASSWD
624 static int chk_kerberos(
625         const struct pw_scheme *sc,
626         const struct berval * passwd,
627         const struct berval * cred )
628 {
629         int i;
630         int rtn;
631
632         for( i=0; i<cred->bv_len; i++) {
633                 if(cred->bv_val[i] == '\0') {
634                         return 1;       /* NUL character in password */
635                 }
636         }
637
638         if( cred->bv_val[i] != '\0' ) {
639                 return 1;       /* cred must behave like a string */
640         }
641
642         for( i=0; i<passwd->bv_len; i++) {
643                 if(passwd->bv_val[i] == '\0') {
644                         return 1;       /* NUL character in password */
645                 }
646         }
647
648         if( passwd->bv_val[i] != '\0' ) {
649                 return 1;       /* passwd must behave like a string */
650         }
651
652         rtn = 1;
653
654 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
655         {
656 /* Portions:
657  * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
658  * (Royal Institute of Technology, Stockholm, Sweden).
659  * All rights reserved.
660  *
661  * Redistribution and use in source and binary forms, with or without
662  * modification, are permitted provided that the following conditions
663  * are met:
664  *
665  * 1. Redistributions of source code must retain the above copyright
666  *    notice, this list of conditions and the following disclaimer.
667  *
668  * 2. Redistributions in binary form must reproduce the above copyright
669  *    notice, this list of conditions and the following disclaimer in the
670  *    documentation and/or other materials provided with the distribution.
671  *
672  * 3. Neither the name of the Institute nor the names of its contributors
673  *    may be used to endorse or promote products derived from this software
674  *    without specific prior written permission.
675  *
676  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
677  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
678  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
679  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
680  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
681  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
682  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
683  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
684  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
685  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
686  * SUCH DAMAGE.
687  */
688
689                 krb5_context context;
690                 krb5_error_code ret;
691                 krb5_creds creds;
692                 krb5_get_init_creds_opt get_options;
693                 krb5_verify_init_creds_opt verify_options;
694                 krb5_principal client, server;
695 #ifdef notdef
696                 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
697 #endif
698
699                 ret = krb5_init_context( &context );
700                 if (ret) {
701                         return 1;
702                 }
703
704 #ifdef notdef
705                 krb5_get_init_creds_opt_set_preauth_list(&get_options,
706                         pre_auth_types, 1);
707 #endif
708
709                 krb5_get_init_creds_opt_init( &get_options );
710
711                 krb5_verify_init_creds_opt_init( &verify_options );
712         
713                 ret = krb5_parse_name( context, passwd->bv_val, &client );
714
715                 if (ret) {
716                         krb5_free_context( context );
717                         return 1;
718                 }
719
720                 ret = krb5_get_init_creds_password( context,
721                         &creds, client, cred->bv_val, NULL,
722                         NULL, 0, NULL, &get_options );
723
724                 if (ret) {
725                         krb5_free_principal( context, client );
726                         krb5_free_context( context );
727                         return 1;
728                 }
729
730                 {
731                         char host[MAXHOSTNAMELEN];
732
733                         if( gethostname( host, MAXHOSTNAMELEN ) != 0 ) {
734                                 krb5_free_principal( context, client );
735                                 krb5_free_context( context );
736                                 return 1;
737                         }
738
739                         ret = krb5_sname_to_principal( context,
740                                 host, "ldap", KRB5_NT_SRV_HST, &server );
741                 }
742
743                 if (ret) {
744                         krb5_free_principal( context, client );
745                         krb5_free_context( context );
746                         return 1;
747                 }
748
749                 ret = krb5_verify_init_creds( context,
750                         &creds, server, NULL, NULL, &verify_options );
751
752                 krb5_free_principal( context, client );
753                 krb5_free_principal( context, server );
754                 krb5_free_creds_contents( context, &creds );
755                 krb5_free_context( context );
756
757                 rtn = !!ret;
758         }
759 #elif   defined(HAVE_KRB4)
760         {
761                 /* Borrowed from Heimdal kpopper */
762 /* Portions:
763  * Copyright (c) 1989 Regents of the University of California.
764  * All rights reserved.  The Berkeley software License Agreement
765  * specifies the terms and conditions for redistribution.
766  */
767
768                 int status;
769                 char lrealm[REALM_SZ];
770                 char tkt[MAXHOSTNAMELEN];
771
772                 status = krb_get_lrealm(lrealm,1);
773                 if (status == KFAILURE) {
774                         return 1;
775                 }
776
777                 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
778                         TKT_ROOT, (unsigned)getpid());
779                 krb_set_tkt_string (tkt);
780
781                 status = krb_verify_user( passwd->bv_val, "", lrealm,
782                         cred->bv_val, 1, "ldap");
783
784                 dest_tkt(); /* no point in keeping the tickets */
785
786                 return status == KFAILURE;
787         }
788 #endif
789
790         return rtn;
791 }
792 #endif /* SLAPD_KPASSWD */
793
794 #ifdef SLAPD_CRYPT
795 static int chk_crypt(
796         const struct pw_scheme *sc,
797         const struct berval * passwd,
798         const struct berval * cred )
799 {
800         char *cr;
801         int i;
802
803         for( i=0; i<cred->bv_len; i++) {
804                 if(cred->bv_val[i] == '\0') {
805                         return 1;       /* NUL character in password */
806                 }
807         }
808
809         if( cred->bv_val[i] != '\0' ) {
810                 return -1;      /* cred must behave like a string */
811         }
812
813         if( passwd->bv_len < 2 ) {
814                 return -1;      /* passwd must be at least two characters long */
815         }
816
817         for( i=0; i<passwd->bv_len; i++) {
818                 if(passwd->bv_val[i] == '\0') {
819                         return -1;      /* NUL character in password */
820                 }
821         }
822
823         if( passwd->bv_val[i] != '\0' ) {
824                 return -1;      /* passwd must behave like a string */
825         }
826
827         cr = crypt( cred->bv_val, passwd->bv_val );
828
829         if( cr == NULL || cr[0] == '\0' ) {
830                 /* salt must have been invalid */
831                 return -1;
832         }
833
834         return strcmp( passwd->bv_val, cr ) ? 1 : 0;
835 }
836
837 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
838 static int chk_unix(
839         const struct pw_scheme *sc,
840         const struct berval * passwd,
841         const struct berval * cred )
842 {
843         int i;
844         char *pw,*cr;
845
846         for( i=0; i<cred->bv_len; i++) {
847                 if(cred->bv_val[i] == '\0') {
848                         return -1;      /* NUL character in password */
849                 }
850         }
851         if( cred->bv_val[i] != '\0' ) {
852                 return -1;      /* cred must behave like a string */
853         }
854
855         for( i=0; i<passwd->bv_len; i++) {
856                 if(passwd->bv_val[i] == '\0') {
857                         return -1;      /* NUL character in password */
858                 }
859         }
860
861         if( passwd->bv_val[i] != '\0' ) {
862                 return -1;      /* passwd must behave like a string */
863         }
864
865         {
866                 struct passwd *pwd = getpwnam(passwd->bv_val);
867
868                 if(pwd == NULL) {
869                         return -1;      /* not found */
870                 }
871
872                 pw = pwd->pw_passwd;
873         }
874 #  ifdef HAVE_GETSPNAM
875         {
876                 struct spwd *spwd = getspnam(passwd->bv_val);
877
878                 if(spwd != NULL) {
879                         pw = spwd->sp_pwdp;
880                 }
881         }
882 #  endif
883 #  ifdef HAVE_AIX_SECURITY
884         {
885                 struct userpw *upw = getuserpw(passwd->bv_val);
886
887                 if (upw != NULL) {
888                         pw = upw->upw_passwd;
889                 }
890         }
891 #  endif
892
893         if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
894                 /* password must must be at least two characters long */
895                 return -1;
896         }
897
898         cr = crypt(cred->bv_val, pw);
899
900         if( cr == NULL || cr[0] == '\0' ) {
901                 /* salt must have been invalid */
902                 return -1;
903         }
904
905         return strcmp(pw, cr) ? 1 : 0;
906
907 }
908 # endif
909 #endif
910
911 /* PASSWORD GENERATION ROUTINES */
912
913 static struct berval *hash_ssha1(
914         const struct pw_scheme *scheme,
915         const struct berval  *passwd )
916 {
917         lutil_SHA1_CTX  SHA1context;
918         unsigned char   SHA1digest[LUTIL_SHA1_BYTES];
919         unsigned char   saltdata[4];
920         struct berval digest;
921         struct berval salt;
922
923         digest.bv_val = SHA1digest;
924         digest.bv_len = sizeof(SHA1digest);
925         salt.bv_val = saltdata;
926         salt.bv_len = sizeof(saltdata);
927
928         if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
929                 return NULL; 
930         }
931
932         lutil_SHA1Init( &SHA1context );
933         lutil_SHA1Update( &SHA1context,
934                 (const unsigned char *)passwd->bv_val, passwd->bv_len );
935         lutil_SHA1Update( &SHA1context,
936                 (const unsigned char *)salt.bv_val, salt.bv_len );
937         lutil_SHA1Final( SHA1digest, &SHA1context );
938
939         return pw_string64( scheme, &digest, &salt);
940 }
941
942 static struct berval *hash_sha1(
943         const struct pw_scheme *scheme,
944         const struct berval  *passwd )
945 {
946         lutil_SHA1_CTX  SHA1context;
947         unsigned char   SHA1digest[20];
948         struct berval digest;
949         digest.bv_val = SHA1digest;
950         digest.bv_len = sizeof(SHA1digest);
951      
952         lutil_SHA1Init( &SHA1context );
953         lutil_SHA1Update( &SHA1context,
954                 (const unsigned char *)passwd->bv_val, passwd->bv_len );
955         lutil_SHA1Final( SHA1digest, &SHA1context );
956             
957         return pw_string64( scheme, &digest, NULL);
958 }
959
960 static struct berval *hash_smd5(
961         const struct pw_scheme *scheme,
962         const struct berval  *passwd )
963 {
964         lutil_MD5_CTX   MD5context;
965         unsigned char   MD5digest[16];
966         unsigned char   saltdata[4];
967         struct berval digest;
968         struct berval salt;
969
970         digest.bv_val = MD5digest;
971         digest.bv_len = sizeof(MD5digest);
972         salt.bv_val = saltdata;
973         salt.bv_len = sizeof(saltdata);
974
975         if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
976                 return NULL; 
977         }
978
979         lutil_MD5Init( &MD5context );
980         lutil_MD5Update( &MD5context,
981                 (const unsigned char *) passwd->bv_val, passwd->bv_len );
982         lutil_MD5Update( &MD5context,
983                 (const unsigned char *) salt.bv_val, salt.bv_len );
984         lutil_MD5Final( MD5digest, &MD5context );
985
986         return pw_string64( scheme, &digest, &salt );
987 }
988
989 static struct berval *hash_md5(
990         const struct pw_scheme *scheme,
991         const struct berval  *passwd )
992 {
993         lutil_MD5_CTX   MD5context;
994         unsigned char   MD5digest[16];
995
996         struct berval digest;
997
998         digest.bv_val = MD5digest;
999         digest.bv_len = sizeof(MD5digest);
1000
1001         lutil_MD5Init( &MD5context );
1002         lutil_MD5Update( &MD5context,
1003                 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1004         lutil_MD5Final( MD5digest, &MD5context );
1005
1006         return pw_string64( scheme, &digest, NULL );
1007 ;
1008 }
1009
1010 #ifdef SLAPD_CRYPT
1011 static struct berval *hash_crypt(
1012         const struct pw_scheme *scheme,
1013         const struct berval *passwd )
1014 {
1015         struct berval hash;
1016         unsigned char salt[3];
1017         int i;
1018
1019         for( i=0; i<passwd->bv_len; i++) {
1020                 if(passwd->bv_val[i] == '\0') {
1021                         return NULL;    /* NUL character in password */
1022                 }
1023         }
1024
1025         if( passwd->bv_val[i] != '\0' ) {
1026                 return NULL;    /* passwd must behave like a string */
1027         }
1028
1029         if( lutil_entropy( salt, sizeof(salt)) < 0 ) {
1030                 return NULL; 
1031         }
1032
1033         salt[0] = crypt64[ salt[0] % (sizeof(crypt64)-1) ];
1034         salt[1] = crypt64[ salt[1] % (sizeof(crypt64)-1) ];
1035         salt[2] = '\0';
1036
1037         hash.bv_val = crypt( passwd->bv_val, salt );
1038
1039         if( hash.bv_val == NULL ) return NULL;
1040
1041         hash.bv_len = strlen( hash.bv_val );
1042
1043         if( hash.bv_len == 0 ) {
1044                 return NULL;
1045         }
1046
1047         return pw_string( scheme, &hash );
1048 }
1049 #endif