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