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