]> git.sur5r.net Git - openldap/blob - libraries/liblutil/passwd.c
Add char* ldap_pvt_get_fqdn(char*) which returns the FQDN of the
[openldap] / libraries / liblutil / passwd.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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 #endif
292         return 1;
293 }
294
295 struct berval * lutil_passwd_generate( ber_len_t len )
296 {
297         struct berval *pw;
298
299         if( len < 1 ) return NULL;
300
301         pw = ber_memalloc( sizeof( struct berval ) );
302         if( pw == NULL ) return NULL;
303
304         pw->bv_len = len;
305         pw->bv_val = ber_memalloc( len + 1 );
306
307         if( pw->bv_val == NULL ) {
308                 ber_memfree( pw );
309                 return NULL;
310         }
311
312         if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
313                 ber_bvfree( pw );
314                 return NULL; 
315         }
316
317         for( len = 0; len < pw->bv_len; len++ ) {
318                 pw->bv_val[len] = crypt64[
319                         pw->bv_val[len] % (sizeof(crypt64)-1) ];
320         }
321
322         pw->bv_val[len] = '\0';
323         
324         return pw;
325 }
326
327 struct berval * lutil_passwd_hash(
328         const struct berval * passwd,
329         const char * method )
330 {
331         const struct pw_scheme *sc = get_scheme( method );
332
333         if( sc == NULL ) return NULL;
334         if( ! sc->hash_fn ) return NULL;
335
336         return (sc->hash_fn)( sc, passwd );
337 }
338
339 static struct berval * pw_string(
340         const struct pw_scheme *sc,
341         const struct berval *passwd )
342 {
343         struct berval *pw = ber_memalloc( sizeof( struct berval ) );
344         if( pw == NULL ) return NULL;
345
346         pw->bv_len = sc->name.bv_len + passwd->bv_len;
347         pw->bv_val = ber_memalloc( pw->bv_len + 1 );
348
349         if( pw->bv_val == NULL ) {
350                 ber_memfree( pw );
351                 return NULL;
352         }
353
354         AC_MEMCPY( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
355         AC_MEMCPY( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
356
357         pw->bv_val[pw->bv_len] = '\0';
358         return pw;
359 }
360
361 static struct berval * pw_string64(
362         const struct pw_scheme *sc,
363         const struct berval *hash,
364         const struct berval *salt )
365 {
366         int rc;
367         struct berval string;
368         struct berval *b64 = ber_memalloc( sizeof(struct berval) );
369         size_t b64len;
370
371         if( b64 == NULL ) return NULL;
372
373         if( salt ) {
374                 /* need to base64 combined string */
375                 string.bv_len = hash->bv_len + salt->bv_len;
376                 string.bv_val = ber_memalloc( string.bv_len + 1 );
377
378                 if( string.bv_val == NULL ) {
379                         ber_memfree( b64 );
380                         return NULL;
381                 }
382
383                 AC_MEMCPY( string.bv_val, hash->bv_val,
384                         hash->bv_len );
385                 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
386                         salt->bv_len );
387                 string.bv_val[string.bv_len] = '\0';
388
389         } else {
390                 string = *hash;
391         }
392
393         b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
394         b64->bv_len = b64len + sc->name.bv_len;
395         b64->bv_val = ber_memalloc( b64->bv_len + 1 );
396
397         if( b64->bv_val == NULL ) {
398                 if( salt ) ber_memfree( string.bv_val );
399                 ber_memfree( b64 );
400                 return NULL;
401         }
402
403         AC_MEMCPY(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
404
405         rc = lutil_b64_ntop(
406                 string.bv_val, string.bv_len,
407                 &b64->bv_val[sc->name.bv_len], b64len );
408
409         if( salt ) ber_memfree( string.bv_val );
410         
411         if( rc < 0 ) {
412                 ber_bvfree( b64 );
413                 return NULL;
414         }
415
416         /* recompute length */
417         b64->bv_len = sc->name.bv_len + rc;
418         assert( strlen(b64->bv_val) == b64->bv_len );
419         return b64;
420 }
421
422 /* PASSWORD CHECK ROUTINES */
423
424 static int chk_ssha1(
425         const struct pw_scheme *sc,
426         const struct berval * passwd,
427         const struct berval * cred )
428 {
429         lutil_SHA1_CTX SHA1context;
430         unsigned char SHA1digest[LUTIL_SHA1_BYTES];
431         int rc;
432         unsigned char *orig_pass = NULL;
433  
434         /* decode base64 password */
435         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
436                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
437
438         if( orig_pass == NULL ) return -1;
439
440         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
441
442         if(rc < 0) {
443                 ber_memfree(orig_pass);
444                 return -1;
445         }
446  
447         /* hash credentials with salt */
448         lutil_SHA1Init(&SHA1context);
449         lutil_SHA1Update(&SHA1context,
450                 (const unsigned char *) cred->bv_val, cred->bv_len);
451         lutil_SHA1Update(&SHA1context,
452                 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
453                 rc - sizeof(SHA1digest));
454         lutil_SHA1Final(SHA1digest, &SHA1context);
455  
456         /* compare */
457         rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
458         ber_memfree(orig_pass);
459         return rc ? 1 : 0;
460 }
461
462 static int chk_sha1(
463         const struct pw_scheme *sc,
464         const struct berval * passwd,
465         const struct berval * cred )
466 {
467         lutil_SHA1_CTX SHA1context;
468         unsigned char SHA1digest[LUTIL_SHA1_BYTES];
469         int rc;
470         unsigned char *orig_pass = NULL;
471  
472         /* base64 un-encode password */
473         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
474                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
475
476         if( orig_pass == NULL ) return -1;
477
478         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
479
480         if( rc != sizeof(SHA1digest) ) {
481                 ber_memfree(orig_pass);
482                 return -1;
483         }
484  
485         /* hash credentials with salt */
486         lutil_SHA1Init(&SHA1context);
487         lutil_SHA1Update(&SHA1context,
488                 (const unsigned char *) cred->bv_val, cred->bv_len);
489         lutil_SHA1Final(SHA1digest, &SHA1context);
490  
491         /* compare */
492         rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
493         ber_memfree(orig_pass);
494         return rc ? 1 : 0;
495 }
496
497 static int chk_smd5(
498         const struct pw_scheme *sc,
499         const struct berval * passwd,
500         const struct berval * cred )
501 {
502         lutil_MD5_CTX MD5context;
503         unsigned char MD5digest[LUTIL_MD5_BYTES];
504         int rc;
505         unsigned char *orig_pass = NULL;
506
507         /* base64 un-encode password */
508         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
509                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
510
511         if( orig_pass == NULL ) return -1;
512
513         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
514         if ( rc < 0 ) {
515                 ber_memfree(orig_pass);
516                 return -1;
517         }
518
519         /* hash credentials with salt */
520         lutil_MD5Init(&MD5context);
521         lutil_MD5Update(&MD5context,
522                 (const unsigned char *) cred->bv_val, cred->bv_len );
523         lutil_MD5Update(&MD5context,
524                 (const unsigned char *) &orig_pass[sizeof(MD5digest)],
525                 rc - sizeof(MD5digest));
526         lutil_MD5Final(MD5digest, &MD5context);
527
528         /* compare */
529         rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
530         ber_memfree(orig_pass);
531         return rc ? 1 : 0;
532 }
533
534 static int chk_md5(
535         const struct pw_scheme *sc,
536         const struct berval * passwd,
537         const struct berval * cred )
538 {
539         lutil_MD5_CTX MD5context;
540         unsigned char MD5digest[LUTIL_MD5_BYTES];
541         int rc;
542         unsigned char *orig_pass = NULL;
543
544         /* base64 un-encode password */
545         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
546                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
547
548         if( orig_pass == NULL ) return -1;
549
550         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
551         if ( rc != sizeof(MD5digest) ) {
552                 ber_memfree(orig_pass);
553                 return -1;
554         }
555
556         /* hash credentials with salt */
557         lutil_MD5Init(&MD5context);
558         lutil_MD5Update(&MD5context,
559                 (const unsigned char *) cred->bv_val, cred->bv_len );
560         lutil_MD5Final(MD5digest, &MD5context);
561
562         /* compare */
563         rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
564         ber_memfree(orig_pass);
565         return rc ? 1 : 0;
566 }
567
568 #ifdef SLAPD_SPASSWD
569 #ifdef HAVE_CYRUS_SASL
570 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
571 #endif
572
573 static int chk_sasl(
574         const struct pw_scheme *sc,
575         const struct berval * passwd,
576         const struct berval * cred )
577 {
578         int i;
579         int rtn;
580
581         for( i=0; i<cred->bv_len; i++) {
582                 if(cred->bv_val[i] == '\0') {
583                         return 1;       /* NUL character in password */
584                 }
585         }
586
587         if( cred->bv_val[i] != '\0' ) {
588                 return 1;       /* cred must behave like a string */
589         }
590
591         for( i=0; i<passwd->bv_len; i++) {
592                 if(passwd->bv_val[i] == '\0') {
593                         return 1;       /* NUL character in password */
594                 }
595         }
596
597         if( passwd->bv_val[i] != '\0' ) {
598                 return 1;       /* passwd must behave like a string */
599         }
600
601         rtn = 1;
602
603 #ifdef HAVE_CYRUS_SASL
604         if( lutil_passwd_sasl_conn != NULL ) {
605                 const char *errstr = NULL;
606                 int sc;
607
608                 sc = sasl_checkpass( lutil_passwd_sasl_conn,
609                         passwd->bv_val, passwd->bv_len,
610                         cred->bv_val, cred->bv_len,
611                         &errstr );
612
613                 rtn = ( sc != SASL_OK );
614         }
615 #endif
616
617         return rtn;
618 }
619 #endif
620
621 #ifdef SLAPD_KPASSWD
622 static int chk_kerberos(
623         const struct pw_scheme *sc,
624         const struct berval * passwd,
625         const struct berval * cred )
626 {
627         int i;
628         int rtn;
629
630         for( i=0; i<cred->bv_len; i++) {
631                 if(cred->bv_val[i] == '\0') {
632                         return 1;       /* NUL character in password */
633                 }
634         }
635
636         if( cred->bv_val[i] != '\0' ) {
637                 return 1;       /* cred must behave like a string */
638         }
639
640         for( i=0; i<passwd->bv_len; i++) {
641                 if(passwd->bv_val[i] == '\0') {
642                         return 1;       /* NUL character in password */
643                 }
644         }
645
646         if( passwd->bv_val[i] != '\0' ) {
647                 return 1;       /* passwd must behave like a string */
648         }
649
650         rtn = 1;
651
652 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
653         {
654 /* Portions:
655  * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
656  * (Royal Institute of Technology, Stockholm, Sweden).
657  * All rights reserved.
658  *
659  * Redistribution and use in source and binary forms, with or without
660  * modification, are permitted provided that the following conditions
661  * are met:
662  *
663  * 1. Redistributions of source code must retain the above copyright
664  *    notice, this list of conditions and the following disclaimer.
665  *
666  * 2. Redistributions in binary form must reproduce the above copyright
667  *    notice, this list of conditions and the following disclaimer in the
668  *    documentation and/or other materials provided with the distribution.
669  *
670  * 3. Neither the name of the Institute nor the names of its contributors
671  *    may be used to endorse or promote products derived from this software
672  *    without specific prior written permission.
673  *
674  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
675  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
676  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
677  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
678  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
679  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
680  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
681  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
682  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
683  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
684  * SUCH DAMAGE.
685  */
686
687                 krb5_context context;
688                 krb5_error_code ret;
689                 krb5_creds creds;
690                 krb5_get_init_creds_opt get_options;
691                 krb5_verify_init_creds_opt verify_options;
692                 krb5_principal client, server;
693 #ifdef notdef
694                 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
695 #endif
696
697                 ret = krb5_init_context( &context );
698                 if (ret) {
699                         return 1;
700                 }
701
702 #ifdef notdef
703                 krb5_get_init_creds_opt_set_preauth_list(&get_options,
704                         pre_auth_types, 1);
705 #endif
706
707                 krb5_get_init_creds_opt_init( &get_options );
708
709                 krb5_verify_init_creds_opt_init( &verify_options );
710         
711                 ret = krb5_parse_name( context, passwd->bv_val, &client );
712
713                 if (ret) {
714                         krb5_free_context( context );
715                         return 1;
716                 }
717
718                 ret = krb5_get_init_creds_password( context,
719                         &creds, client, cred->bv_val, NULL,
720                         NULL, 0, NULL, &get_options );
721
722                 if (ret) {
723                         krb5_free_principal( context, client );
724                         krb5_free_context( context );
725                         return 1;
726                 }
727
728                 {
729                         char *host = ldap_pvt_get_fqdn( NULL );
730
731                         if( host == NULL ) {
732                                 krb5_free_principal( context, client );
733                                 krb5_free_context( context );
734                                 return 1;
735                         }
736
737                         ret = krb5_sname_to_principal( context,
738                                 host, "ldap", KRB5_NT_SRV_HST, &server );
739
740                         ber_memfree( host );
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[LUTIL_SHA1_BYTES];
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[LUTIL_MD5_BYTES];
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[LUTIL_MD5_BYTES];
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