]> git.sur5r.net Git - openldap/blob - libraries/liblutil/passwd.c
Added support for AIX security database:
[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_GETSPNAM ) \
117   || ( defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD ) )
118 static int chk_unix(
119         const struct pw_scheme *scheme,
120         const struct berval *passwd,
121         const struct berval *cred );
122 #endif
123 #endif
124
125
126 /* password hash routines */
127 static struct berval *hash_sha1(
128         const struct pw_scheme *scheme,
129         const struct berval *passwd );
130
131 static struct berval *hash_ssha1(
132         const struct pw_scheme *scheme,
133         const struct berval *passwd );
134
135 static struct berval *hash_smd5(
136         const struct pw_scheme *scheme,
137         const struct berval *passwd );
138
139 static struct berval *hash_md5(
140         const struct pw_scheme *scheme,
141         const struct berval *passwd );
142
143 #ifdef SLAPD_CRYPT
144 static struct berval *hash_crypt(
145         const struct pw_scheme *scheme,
146         const struct berval *passwd );
147 #endif
148
149
150 static const struct pw_scheme pw_schemes[] =
151 {
152         { {sizeof("{SSHA}")-1, "{SSHA}"},       chk_ssha1, hash_ssha1 },
153         { {sizeof("{SHA}")-1, "{SHA}"},         chk_sha1, hash_sha1 },
154
155         { {sizeof("{SMD5}")-1, "{SMD5}"},       chk_smd5, hash_smd5 },
156         { {sizeof("{MD5}")-1, "{MD5}"},         chk_md5, hash_md5 },
157
158 #ifdef SLAPD_SPASSWD
159         { {sizeof("{SASL}")-1, "{SASL}"}, chk_sasl, NULL },
160 #endif
161
162 #ifdef SLAPD_KPASSWD
163         { {sizeof("{KERBEROS}")-1, "{KERBEROS}"}, chk_kerberos, NULL },
164 #endif
165
166 #ifdef SLAPD_CRYPT
167         { {sizeof("{CRYPT}")-1, "{CRYPT}"},     chk_crypt, hash_crypt },
168 #endif
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 #else
294         return 1;
295 #endif
296
297 }
298
299 struct berval * lutil_passwd_generate( ber_len_t len )
300 {
301         struct berval *pw;
302
303         if( len < 1 ) return NULL;
304
305         pw = ber_memalloc( sizeof( struct berval ) );
306         if( pw == NULL ) return NULL;
307
308         pw->bv_len = len;
309         pw->bv_val = ber_memalloc( len + 1 );
310
311         if( pw->bv_val == NULL ) {
312                 ber_memfree( pw );
313                 return NULL;
314         }
315
316         if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
317                 ber_bvfree( pw );
318                 return NULL; 
319         }
320
321         for( len = 0; len < pw->bv_len; len++ ) {
322                 pw->bv_val[len] = crypt64[
323                         pw->bv_val[len] % (sizeof(crypt64)-1) ];
324         }
325
326         pw->bv_val[len] = '\0';
327         
328         return pw;
329 }
330
331 struct berval * lutil_passwd_hash(
332         const struct berval * passwd,
333         const char * method )
334 {
335         const struct pw_scheme *sc = get_scheme( method );
336
337         if( sc == NULL ) return NULL;
338         if( ! sc->hash_fn ) return NULL;
339
340         return (sc->hash_fn)( sc, passwd );
341 }
342
343 static struct berval * pw_string(
344         const struct pw_scheme *sc,
345         const struct berval *passwd )
346 {
347         struct berval *pw = ber_memalloc( sizeof( struct berval ) );
348         if( pw == NULL ) return NULL;
349
350         pw->bv_len = sc->name.bv_len + passwd->bv_len;
351         pw->bv_val = ber_memalloc( pw->bv_len + 1 );
352
353         if( pw->bv_val == NULL ) {
354                 ber_memfree( pw );
355                 return NULL;
356         }
357
358         memcpy( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
359         memcpy( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
360
361         pw->bv_val[pw->bv_len] = '\0';
362         return pw;
363 }
364
365 static struct berval * pw_string64(
366         const struct pw_scheme *sc,
367         const struct berval *hash,
368         const struct berval *salt )
369 {
370         int rc;
371         struct berval string;
372         struct berval *b64 = ber_memalloc( sizeof(struct berval) );
373         size_t b64len;
374
375         if( b64 == NULL ) return NULL;
376
377         if( salt ) {
378                 /* need to base64 combined string */
379                 string.bv_len = hash->bv_len + salt->bv_len;
380                 string.bv_val = ber_memalloc( string.bv_len + 1 );
381
382                 if( string.bv_val == NULL ) {
383                         ber_memfree( b64 );
384                         return NULL;
385                 }
386
387                 memcpy( string.bv_val, hash->bv_val,
388                         hash->bv_len );
389                 memcpy( &string.bv_val[hash->bv_len], salt->bv_val,
390                         salt->bv_len );
391                 string.bv_val[string.bv_len] = '\0';
392
393         } else {
394                 string = *hash;
395         }
396
397         b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
398         b64->bv_len = b64len + sc->name.bv_len;
399         b64->bv_val = ber_memalloc( b64->bv_len + 1 );
400
401         if( b64->bv_val == NULL ) {
402                 if( salt ) ber_memfree( string.bv_val );
403                 ber_memfree( b64 );
404                 return NULL;
405         }
406
407         memcpy(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
408
409         rc = lutil_b64_ntop(
410                 string.bv_val, string.bv_len,
411                 &b64->bv_val[sc->name.bv_len], b64len );
412
413         if( salt ) ber_memfree( string.bv_val );
414         
415         if( rc < 0 ) {
416                 ber_bvfree( b64 );
417                 return NULL;
418         }
419
420         /* recompute length */
421         b64->bv_len = sc->name.bv_len + rc;
422         assert( strlen(b64->bv_val) == b64->bv_len );
423         return b64;
424 }
425
426 /* PASSWORD CHECK ROUTINES */
427
428 static int chk_ssha1(
429         const struct pw_scheme *sc,
430         const struct berval * passwd,
431         const struct berval * cred )
432 {
433         lutil_SHA1_CTX SHA1context;
434         unsigned char SHA1digest[LUTIL_SHA1_BYTES];
435         int rc;
436         unsigned char *orig_pass = NULL;
437  
438         /* decode base64 password */
439         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
440                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
441
442         if( orig_pass == NULL ) return -1;
443
444         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
445
446         if(rc < 0) {
447                 ber_memfree(orig_pass);
448                 return -1;
449         }
450  
451         /* hash credentials with salt */
452         lutil_SHA1Init(&SHA1context);
453         lutil_SHA1Update(&SHA1context,
454                 (const unsigned char *) cred->bv_val, cred->bv_len);
455         lutil_SHA1Update(&SHA1context,
456                 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
457                 rc - sizeof(SHA1digest));
458         lutil_SHA1Final(SHA1digest, &SHA1context);
459  
460         /* compare */
461         rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
462         ber_memfree(orig_pass);
463         return rc ? 1 : 0;
464 }
465
466 static int chk_sha1(
467         const struct pw_scheme *sc,
468         const struct berval * passwd,
469         const struct berval * cred )
470 {
471         lutil_SHA1_CTX SHA1context;
472         unsigned char SHA1digest[LUTIL_SHA1_BYTES];
473         int rc;
474         unsigned char *orig_pass = NULL;
475  
476         /* base64 un-encode password */
477         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
478                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
479
480         if( orig_pass == NULL ) return -1;
481
482         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
483
484         if( rc != sizeof(SHA1digest) ) {
485                 ber_memfree(orig_pass);
486                 return -1;
487         }
488  
489         /* hash credentials with salt */
490         lutil_SHA1Init(&SHA1context);
491         lutil_SHA1Update(&SHA1context,
492                 (const unsigned char *) cred->bv_val, cred->bv_len);
493         lutil_SHA1Final(SHA1digest, &SHA1context);
494  
495         /* compare */
496         rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
497         ber_memfree(orig_pass);
498         return rc ? 1 : 0;
499 }
500
501 static int chk_smd5(
502         const struct pw_scheme *sc,
503         const struct berval * passwd,
504         const struct berval * cred )
505 {
506         lutil_MD5_CTX MD5context;
507         unsigned char MD5digest[LUTIL_MD5_BYTES];
508         int rc;
509         unsigned char *orig_pass = NULL;
510
511         /* base64 un-encode password */
512         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
513                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
514
515         if( orig_pass == NULL ) return -1;
516
517         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
518         if ( rc < 0 ) {
519                 ber_memfree(orig_pass);
520                 return -1;
521         }
522
523         /* hash credentials with salt */
524         lutil_MD5Init(&MD5context);
525         lutil_MD5Update(&MD5context,
526                 (const unsigned char *) cred->bv_val, cred->bv_len );
527         lutil_MD5Update(&MD5context,
528                 (const unsigned char *) &orig_pass[sizeof(MD5digest)],
529                 rc - sizeof(MD5digest));
530         lutil_MD5Final(MD5digest, &MD5context);
531
532         /* compare */
533         rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
534         ber_memfree(orig_pass);
535         return rc ? 1 : 0;
536 }
537
538 static int chk_md5(
539         const struct pw_scheme *sc,
540         const struct berval * passwd,
541         const struct berval * cred )
542 {
543         lutil_MD5_CTX MD5context;
544         unsigned char MD5digest[LUTIL_MD5_BYTES];
545         int rc;
546         unsigned char *orig_pass = NULL;
547
548         /* base64 un-encode password */
549         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
550                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
551
552         if( orig_pass == NULL ) return -1;
553
554         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
555         if ( rc != sizeof(MD5digest) ) {
556                 ber_memfree(orig_pass);
557                 return -1;
558         }
559
560         /* hash credentials with salt */
561         lutil_MD5Init(&MD5context);
562         lutil_MD5Update(&MD5context,
563                 (const unsigned char *) cred->bv_val, 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[MAXHOSTNAMELEN];
734
735                         if( gethostname( host, MAXHOSTNAMELEN ) != 0 ) {
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
745                 if (ret) {
746                         krb5_free_principal( context, client );
747                         krb5_free_context( context );
748                         return 1;
749                 }
750
751                 ret = krb5_verify_init_creds( context,
752                         &creds, server, NULL, NULL, &verify_options );
753
754                 krb5_free_principal( context, client );
755                 krb5_free_principal( context, server );
756                 krb5_free_creds_contents( context, &creds );
757                 krb5_free_context( context );
758
759                 rtn = !!ret;
760         }
761 #elif   defined(HAVE_KRB4)
762         {
763                 /* Borrowed from Heimdal kpopper */
764 /* Portions:
765  * Copyright (c) 1989 Regents of the University of California.
766  * All rights reserved.  The Berkeley software License Agreement
767  * specifies the terms and conditions for redistribution.
768  */
769
770                 int status;
771                 char lrealm[REALM_SZ];
772                 char tkt[MAXHOSTNAMELEN];
773
774                 status = krb_get_lrealm(lrealm,1);
775                 if (status == KFAILURE) {
776                         return 1;
777                 }
778
779                 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
780                         TKT_ROOT, (unsigned)getpid());
781                 krb_set_tkt_string (tkt);
782
783                 status = krb_verify_user( passwd->bv_val, "", lrealm,
784                         cred->bv_val, 1, "ldap");
785
786                 dest_tkt(); /* no point in keeping the tickets */
787
788                 return status == KFAILURE;
789         }
790 #endif
791
792         return rtn;
793 }
794 #endif /* SLAPD_KPASSWD */
795
796 #ifdef SLAPD_CRYPT
797 static int chk_crypt(
798         const struct pw_scheme *sc,
799         const struct berval * passwd,
800         const struct berval * cred )
801 {
802         char *cr;
803         int i;
804
805         for( i=0; i<cred->bv_len; i++) {
806                 if(cred->bv_val[i] == '\0') {
807                         return 1;       /* NUL character in password */
808                 }
809         }
810
811         if( cred->bv_val[i] != '\0' ) {
812                 return -1;      /* cred must behave like a string */
813         }
814
815         if( passwd->bv_len < 2 ) {
816                 return -1;      /* passwd must be at least two characters long */
817         }
818
819         for( i=0; i<passwd->bv_len; i++) {
820                 if(passwd->bv_val[i] == '\0') {
821                         return -1;      /* NUL character in password */
822                 }
823         }
824
825         if( passwd->bv_val[i] != '\0' ) {
826                 return -1;      /* passwd must behave like a string */
827         }
828
829         cr = crypt( cred->bv_val, passwd->bv_val );
830
831         if( cr == NULL || cr[0] == '\0' ) {
832                 /* salt must have been invalid */
833                 return -1;
834         }
835
836         return strcmp( passwd->bv_val, cr ) ? 1 : 0;
837 }
838
839 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
840 static int chk_unix(
841         const struct pw_scheme *sc,
842         const struct berval * passwd,
843         const struct berval * cred )
844 {
845         int i;
846         char *pw,*cr;
847
848         for( i=0; i<cred->bv_len; i++) {
849                 if(cred->bv_val[i] == '\0') {
850                         return -1;      /* NUL character in password */
851                 }
852         }
853         if( cred->bv_val[i] != '\0' ) {
854                 return -1;      /* cred must behave like a string */
855         }
856
857         for( i=0; i<passwd->bv_len; i++) {
858                 if(passwd->bv_val[i] == '\0') {
859                         return -1;      /* NUL character in password */
860                 }
861         }
862
863         if( passwd->bv_val[i] != '\0' ) {
864                 return -1;      /* passwd must behave like a string */
865         }
866
867         {
868                 struct passwd *pwd = getpwnam(passwd->bv_val);
869
870                 if(pwd == NULL) {
871                         return -1;      /* not found */
872                 }
873
874                 pw = pwd->pw_passwd;
875         }
876 #  ifdef HAVE_GETSPNAM
877         {
878                 struct spwd *spwd = getspnam(passwd->bv_val);
879
880                 if(spwd != NULL) {
881                         pw = spwd->sp_pwdp;
882                 }
883         }
884 #  endif
885 #  ifdef HAVE_AIX_SECURITY
886         {
887                 struct userpw *upw = getuserpw(passwd->bv_val);
888
889                 if (upw != NULL) {
890                         pw = upw->upw_passwd;
891                 }
892         }
893 #  endif
894
895         if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
896                 /* password must must be at least two characters long */
897                 return -1;
898         }
899
900         cr = crypt(cred->bv_val, pw);
901
902         if( cr == NULL || cr[0] == '\0' ) {
903                 /* salt must have been invalid */
904                 return -1;
905         }
906
907         return strcmp(pw, cr) ? 1 : 0;
908
909 }
910 # endif
911 #endif
912
913 /* PASSWORD GENERATION ROUTINES */
914
915 static struct berval *hash_ssha1(
916         const struct pw_scheme *scheme,
917         const struct berval  *passwd )
918 {
919         lutil_SHA1_CTX  SHA1context;
920         unsigned char   SHA1digest[LUTIL_SHA1_BYTES];
921         unsigned char   saltdata[4];
922         struct berval digest;
923         struct berval salt;
924
925         digest.bv_val = SHA1digest;
926         digest.bv_len = sizeof(SHA1digest);
927         salt.bv_val = saltdata;
928         salt.bv_len = sizeof(saltdata);
929
930         if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
931                 return NULL; 
932         }
933
934         lutil_SHA1Init( &SHA1context );
935         lutil_SHA1Update( &SHA1context,
936                 (const unsigned char *)passwd->bv_val, passwd->bv_len );
937         lutil_SHA1Update( &SHA1context,
938                 (const unsigned char *)salt.bv_val, salt.bv_len );
939         lutil_SHA1Final( SHA1digest, &SHA1context );
940
941         return pw_string64( scheme, &digest, &salt);
942 }
943
944 static struct berval *hash_sha1(
945         const struct pw_scheme *scheme,
946         const struct berval  *passwd )
947 {
948         lutil_SHA1_CTX  SHA1context;
949         unsigned char   SHA1digest[20];
950         struct berval digest;
951         digest.bv_val = SHA1digest;
952         digest.bv_len = sizeof(SHA1digest);
953      
954         lutil_SHA1Init( &SHA1context );
955         lutil_SHA1Update( &SHA1context,
956                 (const unsigned char *)passwd->bv_val, passwd->bv_len );
957         lutil_SHA1Final( SHA1digest, &SHA1context );
958             
959         return pw_string64( scheme, &digest, NULL);
960 }
961
962 static struct berval *hash_smd5(
963         const struct pw_scheme *scheme,
964         const struct berval  *passwd )
965 {
966         lutil_MD5_CTX   MD5context;
967         unsigned char   MD5digest[16];
968         unsigned char   saltdata[4];
969         struct berval digest;
970         struct berval salt;
971
972         digest.bv_val = MD5digest;
973         digest.bv_len = sizeof(MD5digest);
974         salt.bv_val = saltdata;
975         salt.bv_len = sizeof(saltdata);
976
977         if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
978                 return NULL; 
979         }
980
981         lutil_MD5Init( &MD5context );
982         lutil_MD5Update( &MD5context,
983                 (const unsigned char *) passwd->bv_val, passwd->bv_len );
984         lutil_MD5Update( &MD5context,
985                 (const unsigned char *) salt.bv_val, salt.bv_len );
986         lutil_MD5Final( MD5digest, &MD5context );
987
988         return pw_string64( scheme, &digest, &salt );
989 }
990
991 static struct berval *hash_md5(
992         const struct pw_scheme *scheme,
993         const struct berval  *passwd )
994 {
995         lutil_MD5_CTX   MD5context;
996         unsigned char   MD5digest[16];
997
998         struct berval digest;
999
1000         digest.bv_val = MD5digest;
1001         digest.bv_len = sizeof(MD5digest);
1002
1003         lutil_MD5Init( &MD5context );
1004         lutil_MD5Update( &MD5context,
1005                 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1006         lutil_MD5Final( MD5digest, &MD5context );
1007
1008         return pw_string64( scheme, &digest, NULL );
1009 ;
1010 }
1011
1012 #ifdef SLAPD_CRYPT
1013 static struct berval *hash_crypt(
1014         const struct pw_scheme *scheme,
1015         const struct berval *passwd )
1016 {
1017         struct berval hash;
1018         unsigned char salt[3];
1019         int i;
1020
1021         for( i=0; i<passwd->bv_len; i++) {
1022                 if(passwd->bv_val[i] == '\0') {
1023                         return NULL;    /* NUL character in password */
1024                 }
1025         }
1026
1027         if( passwd->bv_val[i] != '\0' ) {
1028                 return NULL;    /* passwd must behave like a string */
1029         }
1030
1031         if( lutil_entropy( salt, sizeof(salt)) < 0 ) {
1032                 return NULL; 
1033         }
1034
1035         salt[0] = crypt64[ salt[0] % (sizeof(crypt64)-1) ];
1036         salt[1] = crypt64[ salt[1] % (sizeof(crypt64)-1) ];
1037         salt[2] = '\0';
1038
1039         hash.bv_val = crypt( passwd->bv_val, salt );
1040
1041         if( hash.bv_val == NULL ) return NULL;
1042
1043         hash.bv_len = strlen( hash.bv_val );
1044
1045         if( hash.bv_len == 0 ) {
1046                 return NULL;
1047         }
1048
1049         return pw_string( scheme, &hash );
1050 }
1051 #endif