]> git.sur5r.net Git - openldap/blob - libraries/liblutil/passwd.c
remove lint
[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 "ldap_pvt.h"
52
53 #include "lutil_md5.h"
54 #include "lutil_sha1.h"
55 #include "lutil.h"
56
57 static const unsigned char crypt64[] =
58         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
59
60 struct pw_scheme;
61
62 typedef int (*PASSWD_CHK_FUNC)(
63         const struct pw_scheme *scheme,
64         const struct berval *passwd,
65         const struct berval *cred );
66
67 typedef struct berval * (*PASSWD_HASH_FUNC) (
68         const struct pw_scheme *scheme,
69         const struct berval *passwd );
70
71 struct pw_scheme {
72         struct berval name;
73         PASSWD_CHK_FUNC chk_fn;
74         PASSWD_HASH_FUNC hash_fn;
75 };
76
77 /* password check routines */
78 static int chk_md5(
79         const struct pw_scheme *scheme,
80         const struct berval *passwd,
81         const struct berval *cred );
82
83 static int chk_smd5(
84         const struct pw_scheme *scheme,
85         const struct berval *passwd,
86         const struct berval *cred );
87
88 static int chk_ssha1(
89         const struct pw_scheme *scheme,
90         const struct berval *passwd,
91         const struct berval *cred );
92
93 static int chk_sha1(
94         const struct pw_scheme *scheme,
95         const struct berval *passwd,
96         const struct berval *cred );
97
98 #ifdef SLAPD_SPASSWD
99 static int chk_sasl(
100         const struct pw_scheme *scheme,
101         const struct berval *passwd,
102         const struct berval *cred );
103 #endif
104
105 #ifdef SLAPD_KPASSWD
106 static int chk_kerberos(
107         const struct pw_scheme *scheme,
108         const struct berval *passwd,
109         const struct berval *cred );
110 #endif
111
112 #ifdef SLAPD_CRYPT
113 static int chk_crypt(
114         const struct pw_scheme *scheme,
115         const struct berval *passwd,
116         const struct berval *cred );
117
118 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
119 static int chk_unix(
120         const struct pw_scheme *scheme,
121         const struct berval *passwd,
122         const struct berval *cred );
123 #endif
124 #endif
125
126
127 /* password hash routines */
128 static struct berval *hash_sha1(
129         const struct pw_scheme *scheme,
130         const struct berval *passwd );
131
132 static struct berval *hash_ssha1(
133         const struct pw_scheme *scheme,
134         const struct berval *passwd );
135
136 static struct berval *hash_smd5(
137         const struct pw_scheme *scheme,
138         const struct berval *passwd );
139
140 static struct berval *hash_md5(
141         const struct pw_scheme *scheme,
142         const struct berval *passwd );
143
144 #ifdef SLAPD_CRYPT
145 static struct berval *hash_crypt(
146         const struct pw_scheme *scheme,
147         const struct berval *passwd );
148 #endif
149
150
151 static const struct pw_scheme pw_schemes[] =
152 {
153         { {sizeof("{SSHA}")-1, "{SSHA}"},       chk_ssha1, hash_ssha1 },
154         { {sizeof("{SHA}")-1, "{SHA}"},         chk_sha1, hash_sha1 },
155
156         { {sizeof("{SMD5}")-1, "{SMD5}"},       chk_smd5, hash_smd5 },
157         { {sizeof("{MD5}")-1, "{MD5}"},         chk_md5, hash_md5 },
158
159 #ifdef SLAPD_SPASSWD
160         { {sizeof("{SASL}")-1, "{SASL}"}, chk_sasl, NULL },
161 #endif
162
163 #ifdef SLAPD_KPASSWD
164         { {sizeof("{KERBEROS}")-1, "{KERBEROS}"}, chk_kerberos, NULL },
165 #endif
166
167 #ifdef SLAPD_CRYPT
168         { {sizeof("{CRYPT}")-1, "{CRYPT}"},     chk_crypt, hash_crypt },
169 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
170         { {sizeof("{UNIX}")-1, "{UNIX}"},       chk_unix, NULL },
171 # endif
172 #endif
173
174 #ifdef SLAPD_CLEARTEXT
175         /* psuedo scheme */
176         { {0, "{CLEARTEXT}"}, NULL, NULL },
177 #endif
178
179         { {0, NULL}, NULL, NULL }
180 };
181
182 static const struct pw_scheme *get_scheme(
183         const char* scheme )
184 {
185         int i;
186
187         for( i=0; pw_schemes[i].name.bv_val; i++) {
188                 if( pw_schemes[i].name.bv_len == 0 ) continue;
189
190                 if( strncasecmp(scheme, pw_schemes[i].name.bv_val,
191                         pw_schemes[i].name.bv_len) == 0 )
192                 {
193                         return &pw_schemes[i];
194                 }
195         }
196
197         return NULL;
198 }
199
200 int lutil_passwd_scheme(
201         const char* scheme )
202 {
203         if( scheme == NULL ) {
204                 return 0;
205         }
206
207         return get_scheme(scheme) != NULL;
208 }
209
210
211 static int is_allowed_scheme( 
212         const char* scheme,
213         const char** schemes )
214 {
215         int i;
216
217         if( schemes == NULL ) return 1;
218
219         for( i=0; schemes[i] != NULL; i++ ) {
220                 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
221                         return 1;
222                 }
223         }
224         return 0;
225 }
226
227 static struct berval *passwd_scheme(
228         const struct pw_scheme *scheme,
229         const struct berval * passwd,
230         const char** allowed )
231 {
232         if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
233                 return NULL;
234         }
235
236         if( passwd->bv_len >= scheme->name.bv_len ) {
237                 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
238                         struct berval *bv = ber_memalloc( sizeof(struct berval) );
239
240                         if( bv == NULL ) return NULL;
241
242                         bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
243                         bv->bv_len = passwd->bv_len - scheme->name.bv_len;
244
245                         return bv;
246                 }
247         }
248
249         return NULL;
250 }
251
252 /*
253  * Return 0 if creds are good.
254  */
255 int
256 lutil_passwd(
257         const struct berval *passwd,    /* stored passwd */
258         const struct berval *cred,              /* user cred */
259         const char **schemes )
260 {
261         int i;
262
263         if (cred == NULL || cred->bv_len == 0 ||
264                 passwd == NULL || passwd->bv_len == 0 )
265         {
266                 return -1;
267         }
268
269         for( i=0; pw_schemes[i].name.bv_val != NULL; i++ ) {
270                 if( pw_schemes[i].chk_fn ) {
271                         struct berval *p = passwd_scheme( &pw_schemes[i],
272                                 passwd, schemes );
273
274                         if( p != NULL ) {
275                                 int rc = (pw_schemes[i].chk_fn)( &pw_schemes[i], p, cred );
276
277                                 /* only free the berval structure as the bv_val points
278                                  * into passwd->bv_val
279                                  */
280                                 ber_memfree( p );
281                                 
282                                 return rc;
283                         }
284                 }
285         }
286
287 #ifdef SLAPD_CLEARTEXT
288         if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
289                 return passwd->bv_len == cred->bv_len
290                         ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
291                         : 1;
292         }
293 #endif
294         return 1;
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         AC_MEMCPY( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
357         AC_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                 AC_MEMCPY( string.bv_val, hash->bv_val,
386                         hash->bv_len );
387                 AC_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         AC_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,
525                 cred->bv_len );
526         lutil_MD5Update(&MD5context,
527                 &orig_pass[sizeof(MD5digest)],
528                 rc - sizeof(MD5digest));
529         lutil_MD5Final(MD5digest, &MD5context);
530
531         /* compare */
532         rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
533         ber_memfree(orig_pass);
534         return rc ? 1 : 0;
535 }
536
537 static int chk_md5(
538         const struct pw_scheme *sc,
539         const struct berval * passwd,
540         const struct berval * cred )
541 {
542         lutil_MD5_CTX MD5context;
543         unsigned char MD5digest[LUTIL_MD5_BYTES];
544         int rc;
545         unsigned char *orig_pass = NULL;
546
547         /* base64 un-encode password */
548         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
549                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
550
551         if( orig_pass == NULL ) return -1;
552
553         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
554         if ( rc != sizeof(MD5digest) ) {
555                 ber_memfree(orig_pass);
556                 return -1;
557         }
558
559         /* hash credentials with salt */
560         lutil_MD5Init(&MD5context);
561         lutil_MD5Update(&MD5context,
562                 (const unsigned char *) cred->bv_val,
563                 cred->bv_len );
564         lutil_MD5Final(MD5digest, &MD5context);
565
566         /* compare */
567         rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
568         ber_memfree(orig_pass);
569         return rc ? 1 : 0;
570 }
571
572 #ifdef SLAPD_SPASSWD
573 #ifdef HAVE_CYRUS_SASL
574 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
575 #endif
576
577 static int chk_sasl(
578         const struct pw_scheme *sc,
579         const struct berval * passwd,
580         const struct berval * cred )
581 {
582         int i;
583         int rtn;
584
585         for( i=0; i<cred->bv_len; i++) {
586                 if(cred->bv_val[i] == '\0') {
587                         return 1;       /* NUL character in password */
588                 }
589         }
590
591         if( cred->bv_val[i] != '\0' ) {
592                 return 1;       /* cred must behave like a string */
593         }
594
595         for( i=0; i<passwd->bv_len; i++) {
596                 if(passwd->bv_val[i] == '\0') {
597                         return 1;       /* NUL character in password */
598                 }
599         }
600
601         if( passwd->bv_val[i] != '\0' ) {
602                 return 1;       /* passwd must behave like a string */
603         }
604
605         rtn = 1;
606
607 #ifdef HAVE_CYRUS_SASL
608         if( lutil_passwd_sasl_conn != NULL ) {
609                 const char *errstr = NULL;
610                 int sc;
611
612                 sc = sasl_checkpass( lutil_passwd_sasl_conn,
613                         passwd->bv_val, passwd->bv_len,
614                         cred->bv_val, cred->bv_len,
615                         &errstr );
616
617                 rtn = ( sc != SASL_OK );
618         }
619 #endif
620
621         return rtn;
622 }
623 #endif
624
625 #ifdef SLAPD_KPASSWD
626 static int chk_kerberos(
627         const struct pw_scheme *sc,
628         const struct berval * passwd,
629         const struct berval * cred )
630 {
631         int i;
632         int rtn;
633
634         for( i=0; i<cred->bv_len; i++) {
635                 if(cred->bv_val[i] == '\0') {
636                         return 1;       /* NUL character in password */
637                 }
638         }
639
640         if( cred->bv_val[i] != '\0' ) {
641                 return 1;       /* cred must behave like a string */
642         }
643
644         for( i=0; i<passwd->bv_len; i++) {
645                 if(passwd->bv_val[i] == '\0') {
646                         return 1;       /* NUL character in password */
647                 }
648         }
649
650         if( passwd->bv_val[i] != '\0' ) {
651                 return 1;       /* passwd must behave like a string */
652         }
653
654         rtn = 1;
655
656 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
657         {
658 /* Portions:
659  * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
660  * (Royal Institute of Technology, Stockholm, Sweden).
661  * All rights reserved.
662  *
663  * Redistribution and use in source and binary forms, with or without
664  * modification, are permitted provided that the following conditions
665  * are met:
666  *
667  * 1. Redistributions of source code must retain the above copyright
668  *    notice, this list of conditions and the following disclaimer.
669  *
670  * 2. Redistributions in binary form must reproduce the above copyright
671  *    notice, this list of conditions and the following disclaimer in the
672  *    documentation and/or other materials provided with the distribution.
673  *
674  * 3. Neither the name of the Institute nor the names of its contributors
675  *    may be used to endorse or promote products derived from this software
676  *    without specific prior written permission.
677  *
678  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
679  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
680  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
681  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
682  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
683  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
684  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
685  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
686  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
687  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
688  * SUCH DAMAGE.
689  */
690
691                 krb5_context context;
692                 krb5_error_code ret;
693                 krb5_creds creds;
694                 krb5_get_init_creds_opt get_options;
695                 krb5_verify_init_creds_opt verify_options;
696                 krb5_principal client, server;
697 #ifdef notdef
698                 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
699 #endif
700
701                 ret = krb5_init_context( &context );
702                 if (ret) {
703                         return 1;
704                 }
705
706 #ifdef notdef
707                 krb5_get_init_creds_opt_set_preauth_list(&get_options,
708                         pre_auth_types, 1);
709 #endif
710
711                 krb5_get_init_creds_opt_init( &get_options );
712
713                 krb5_verify_init_creds_opt_init( &verify_options );
714         
715                 ret = krb5_parse_name( context, passwd->bv_val, &client );
716
717                 if (ret) {
718                         krb5_free_context( context );
719                         return 1;
720                 }
721
722                 ret = krb5_get_init_creds_password( context,
723                         &creds, client, cred->bv_val, NULL,
724                         NULL, 0, NULL, &get_options );
725
726                 if (ret) {
727                         krb5_free_principal( context, client );
728                         krb5_free_context( context );
729                         return 1;
730                 }
731
732                 {
733                         char *host = ldap_pvt_get_fqdn( NULL );
734
735                         if( host == NULL ) {
736                                 krb5_free_principal( context, client );
737                                 krb5_free_context( context );
738                                 return 1;
739                         }
740
741                         ret = krb5_sname_to_principal( context,
742                                 host, "ldap", KRB5_NT_SRV_HST, &server );
743
744                         ber_memfree( host );
745                 }
746
747                 if (ret) {
748                         krb5_free_principal( context, client );
749                         krb5_free_context( context );
750                         return 1;
751                 }
752
753                 ret = krb5_verify_init_creds( context,
754                         &creds, server, NULL, NULL, &verify_options );
755
756                 krb5_free_principal( context, client );
757                 krb5_free_principal( context, server );
758                 krb5_free_cred_contents( context, &creds );
759                 krb5_free_context( context );
760
761                 rtn = !!ret;
762         }
763 #elif   defined(HAVE_KRB4)
764         {
765                 /* Borrowed from Heimdal kpopper */
766 /* Portions:
767  * Copyright (c) 1989 Regents of the University of California.
768  * All rights reserved.  The Berkeley software License Agreement
769  * specifies the terms and conditions for redistribution.
770  */
771
772                 int status;
773                 char lrealm[REALM_SZ];
774                 char tkt[MAXHOSTNAMELEN];
775
776                 status = krb_get_lrealm(lrealm,1);
777                 if (status == KFAILURE) {
778                         return 1;
779                 }
780
781                 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
782                         TKT_ROOT, (unsigned)getpid());
783                 krb_set_tkt_string (tkt);
784
785                 status = krb_verify_user( passwd->bv_val, "", lrealm,
786                         cred->bv_val, 1, "ldap");
787
788                 dest_tkt(); /* no point in keeping the tickets */
789
790                 return status == KFAILURE;
791         }
792 #endif
793
794         return rtn;
795 }
796 #endif /* SLAPD_KPASSWD */
797
798 #ifdef SLAPD_CRYPT
799 static int chk_crypt(
800         const struct pw_scheme *sc,
801         const struct berval * passwd,
802         const struct berval * cred )
803 {
804         char *cr;
805         int i;
806
807         for( i=0; i<cred->bv_len; i++) {
808                 if(cred->bv_val[i] == '\0') {
809                         return 1;       /* NUL character in password */
810                 }
811         }
812
813         if( cred->bv_val[i] != '\0' ) {
814                 return -1;      /* cred must behave like a string */
815         }
816
817         if( passwd->bv_len < 2 ) {
818                 return -1;      /* passwd must be at least two characters long */
819         }
820
821         for( i=0; i<passwd->bv_len; i++) {
822                 if(passwd->bv_val[i] == '\0') {
823                         return -1;      /* NUL character in password */
824                 }
825         }
826
827         if( passwd->bv_val[i] != '\0' ) {
828                 return -1;      /* passwd must behave like a string */
829         }
830
831         cr = crypt( cred->bv_val, passwd->bv_val );
832
833         if( cr == NULL || cr[0] == '\0' ) {
834                 /* salt must have been invalid */
835                 return -1;
836         }
837
838         return strcmp( passwd->bv_val, cr ) ? 1 : 0;
839 }
840
841 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
842 static int chk_unix(
843         const struct pw_scheme *sc,
844         const struct berval * passwd,
845         const struct berval * cred )
846 {
847         int i;
848         char *pw,*cr;
849
850         for( i=0; i<cred->bv_len; i++) {
851                 if(cred->bv_val[i] == '\0') {
852                         return -1;      /* NUL character in password */
853                 }
854         }
855         if( cred->bv_val[i] != '\0' ) {
856                 return -1;      /* cred must behave like a string */
857         }
858
859         for( i=0; i<passwd->bv_len; i++) {
860                 if(passwd->bv_val[i] == '\0') {
861                         return -1;      /* NUL character in password */
862                 }
863         }
864
865         if( passwd->bv_val[i] != '\0' ) {
866                 return -1;      /* passwd must behave like a string */
867         }
868
869         {
870                 struct passwd *pwd = getpwnam(passwd->bv_val);
871
872                 if(pwd == NULL) {
873                         return -1;      /* not found */
874                 }
875
876                 pw = pwd->pw_passwd;
877         }
878 #  ifdef HAVE_GETSPNAM
879         {
880                 struct spwd *spwd = getspnam(passwd->bv_val);
881
882                 if(spwd != NULL) {
883                         pw = spwd->sp_pwdp;
884                 }
885         }
886 #  endif
887 #  ifdef HAVE_AIX_SECURITY
888         {
889                 struct userpw *upw = getuserpw(passwd->bv_val);
890
891                 if (upw != NULL) {
892                         pw = upw->upw_passwd;
893                 }
894         }
895 #  endif
896
897         if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
898                 /* password must must be at least two characters long */
899                 return -1;
900         }
901
902         cr = crypt(cred->bv_val, pw);
903
904         if( cr == NULL || cr[0] == '\0' ) {
905                 /* salt must have been invalid */
906                 return -1;
907         }
908
909         return strcmp(pw, cr) ? 1 : 0;
910
911 }
912 # endif
913 #endif
914
915 /* PASSWORD GENERATION ROUTINES */
916
917 static struct berval *hash_ssha1(
918         const struct pw_scheme *scheme,
919         const struct berval  *passwd )
920 {
921         lutil_SHA1_CTX  SHA1context;
922         unsigned char   SHA1digest[LUTIL_SHA1_BYTES];
923         unsigned char   saltdata[4];
924         struct berval digest;
925         struct berval salt;
926
927         digest.bv_val = SHA1digest;
928         digest.bv_len = sizeof(SHA1digest);
929         salt.bv_val = saltdata;
930         salt.bv_len = sizeof(saltdata);
931
932         if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
933                 return NULL; 
934         }
935
936         lutil_SHA1Init( &SHA1context );
937         lutil_SHA1Update( &SHA1context,
938                 (const unsigned char *)passwd->bv_val, passwd->bv_len );
939         lutil_SHA1Update( &SHA1context,
940                 (const unsigned char *)salt.bv_val, salt.bv_len );
941         lutil_SHA1Final( SHA1digest, &SHA1context );
942
943         return pw_string64( scheme, &digest, &salt);
944 }
945
946 static struct berval *hash_sha1(
947         const struct pw_scheme *scheme,
948         const struct berval  *passwd )
949 {
950         lutil_SHA1_CTX  SHA1context;
951         unsigned char   SHA1digest[LUTIL_SHA1_BYTES];
952         struct berval digest;
953         digest.bv_val = SHA1digest;
954         digest.bv_len = sizeof(SHA1digest);
955      
956         lutil_SHA1Init( &SHA1context );
957         lutil_SHA1Update( &SHA1context,
958                 (const unsigned char *)passwd->bv_val, passwd->bv_len );
959         lutil_SHA1Final( SHA1digest, &SHA1context );
960             
961         return pw_string64( scheme, &digest, NULL);
962 }
963
964 static struct berval *hash_smd5(
965         const struct pw_scheme *scheme,
966         const struct berval  *passwd )
967 {
968         lutil_MD5_CTX   MD5context;
969         unsigned char   MD5digest[LUTIL_MD5_BYTES];
970         unsigned char   saltdata[4];
971         struct berval digest;
972         struct berval salt;
973
974         digest.bv_val = MD5digest;
975         digest.bv_len = sizeof(MD5digest);
976         salt.bv_val = saltdata;
977         salt.bv_len = sizeof(saltdata);
978
979         if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
980                 return NULL; 
981         }
982
983         lutil_MD5Init( &MD5context );
984         lutil_MD5Update( &MD5context,
985                 (const unsigned char *) passwd->bv_val, passwd->bv_len );
986         lutil_MD5Update( &MD5context,
987                 (const unsigned char *) salt.bv_val, salt.bv_len );
988         lutil_MD5Final( MD5digest, &MD5context );
989
990         return pw_string64( scheme, &digest, &salt );
991 }
992
993 static struct berval *hash_md5(
994         const struct pw_scheme *scheme,
995         const struct berval  *passwd )
996 {
997         lutil_MD5_CTX   MD5context;
998         unsigned char   MD5digest[LUTIL_MD5_BYTES];
999
1000         struct berval digest;
1001
1002         digest.bv_val = MD5digest;
1003         digest.bv_len = sizeof(MD5digest);
1004
1005         lutil_MD5Init( &MD5context );
1006         lutil_MD5Update( &MD5context,
1007                 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1008         lutil_MD5Final( MD5digest, &MD5context );
1009
1010         return pw_string64( scheme, &digest, NULL );
1011 ;
1012 }
1013
1014 #ifdef SLAPD_CRYPT
1015 static struct berval *hash_crypt(
1016         const struct pw_scheme *scheme,
1017         const struct berval *passwd )
1018 {
1019         struct berval hash;
1020         unsigned char salt[3];
1021         int i;
1022
1023         for( i=0; i<passwd->bv_len; i++) {
1024                 if(passwd->bv_val[i] == '\0') {
1025                         return NULL;    /* NUL character in password */
1026                 }
1027         }
1028
1029         if( passwd->bv_val[i] != '\0' ) {
1030                 return NULL;    /* passwd must behave like a string */
1031         }
1032
1033         if( lutil_entropy( salt, sizeof(salt)) < 0 ) {
1034                 return NULL; 
1035         }
1036
1037         salt[0] = crypt64[ salt[0] % (sizeof(crypt64)-1) ];
1038         salt[1] = crypt64[ salt[1] % (sizeof(crypt64)-1) ];
1039         salt[2] = '\0';
1040
1041         hash.bv_val = crypt( passwd->bv_val, salt );
1042
1043         if( hash.bv_val == NULL ) return NULL;
1044
1045         hash.bv_len = strlen( hash.bv_val );
1046
1047         if( hash.bv_len == 0 ) {
1048                 return NULL;
1049         }
1050
1051         return pw_string( scheme, &hash );
1052 }
1053 #endif