Back to index

openldap  2.4.31
Classes | Defines | Functions | Variables
passwd.c File Reference
#include "portable.h"
#include <stdio.h>
#include <ac/stdlib.h>
#include <ac/string.h>
#include <ac/unistd.h>
#include <ac/param.h>
#include <lber.h>
#include "ldap_pvt.h"
#include "lber_pvt.h"
#include "lutil_md5.h"
#include "lutil_sha1.h"
#include "lutil.h"

Go to the source code of this file.

Classes

struct  pw_scheme
struct  pw_slist

Defines

#define SALT_SIZE   4

Functions

int lutil_passwd_add (struct berval *scheme, LUTIL_PASSWD_CHK_FUNC *chk, LUTIL_PASSWD_HASH_FUNC *hash)
void lutil_passwd_init ()
void lutil_passwd_destroy ()
static struct pw_schemeget_scheme (const char *scheme)
int lutil_passwd_scheme (const char *scheme)
static int is_allowed_scheme (const char *scheme, const char **schemes)
static struct bervalpasswd_scheme (const struct pw_scheme *scheme, const struct berval *passwd, struct berval *bv, const char **allowed)
int lutil_passwd (const struct berval *passwd, const struct berval *cred, const char **schemes, const char **text)
int lutil_passwd_generate (struct berval *pw, ber_len_t len)
int lutil_passwd_hash (const struct berval *passwd, const char *method, struct berval *hash, const char **text)
int lutil_passwd_string64 (const struct berval *sc, const struct berval *hash, struct berval *b64, const struct berval *salt)
static int chk_smd5 (const struct berval *sc, const struct berval *passwd, const struct berval *cred, const char **text)
static int chk_md5 (const struct berval *sc, const struct berval *passwd, const struct berval *cred, const char **text)
static int hash_smd5 (const struct berval *scheme, const struct berval *passwd, struct berval *hash, const char **text)
static int hash_md5 (const struct berval *scheme, const struct berval *passwd, struct berval *hash, const char **text)
int lutil_salt_format (const char *format)

Variables

static const unsigned char crypt64 [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./"
static LUTIL_PASSWD_CHK_FUNC chk_md5
static LUTIL_PASSWD_CHK_FUNC chk_smd5
static LUTIL_PASSWD_HASH_FUNC hash_smd5
static LUTIL_PASSWD_HASH_FUNC hash_md5
static struct pw_slistpw_schemes
static int pw_inited
static struct pw_scheme []

Class Documentation

struct pw_scheme

Definition at line 105 of file passwd.c.

Class Members
LUTIL_PASSWD_CHK_FUNC * chk_fn
LUTIL_PASSWD_HASH_FUNC * hash_fn
struct pw_slist

Definition at line 111 of file passwd.c.

Collaboration diagram for pw_slist:
Class Members
struct pw_slist * next

Define Documentation

#define SALT_SIZE   4

Definition at line 118 of file passwd.c.


Function Documentation

static int chk_md5 ( const struct berval sc,
const struct berval passwd,
const struct berval cred,
const char **  text 
) [static]

Definition at line 618 of file passwd.c.

{
       lutil_MD5_CTX MD5context;
       unsigned char MD5digest[LUTIL_MD5_BYTES];
       int rc;
       unsigned char *orig_pass = NULL;

       /* safety check */
       if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(MD5digest)) {
              return LUTIL_PASSWD_ERR;
       }

       /* base64 un-encode password */
       orig_pass = (unsigned char *) ber_memalloc( (size_t) (
              LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );

       if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;

       rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
       if ( rc != sizeof(MD5digest) ) {
              ber_memfree(orig_pass);
              return LUTIL_PASSWD_ERR;
       }

       /* hash credentials with salt */
       lutil_MD5Init(&MD5context);
       lutil_MD5Update(&MD5context,
              (const unsigned char *) cred->bv_val,
              cred->bv_len );
       lutil_MD5Final(MD5digest, &MD5context);

       /* compare */
       rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
       ber_memfree(orig_pass);
       return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
}

Here is the call graph for this function:

static int chk_smd5 ( const struct berval sc,
const struct berval passwd,
const struct berval cred,
const char **  text 
) [static]

Definition at line 573 of file passwd.c.

{
       lutil_MD5_CTX MD5context;
       unsigned char MD5digest[LUTIL_MD5_BYTES];
       int rc;
       unsigned char *orig_pass = NULL;

       /* safety check */
       if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(MD5digest)) {
              return LUTIL_PASSWD_ERR;
       }

       /* base64 un-encode password */
       orig_pass = (unsigned char *) ber_memalloc( (size_t) (
              LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );

       if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;

       rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);

       if (rc <= (int)(sizeof(MD5digest))) {
              ber_memfree(orig_pass);
              return LUTIL_PASSWD_ERR;
       }

       /* hash credentials with salt */
       lutil_MD5Init(&MD5context);
       lutil_MD5Update(&MD5context,
              (const unsigned char *) cred->bv_val,
              cred->bv_len );
       lutil_MD5Update(&MD5context,
              &orig_pass[sizeof(MD5digest)],
              rc - sizeof(MD5digest));
       lutil_MD5Final(MD5digest, &MD5context);

       /* compare */
       rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
       ber_memfree(orig_pass);
       return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
}

Here is the call graph for this function:

static struct pw_scheme* get_scheme ( const char *  scheme) [static, read]

Definition at line 225 of file passwd.c.

{
       struct pw_slist *pws;
       struct berval bv;

       if (!pw_inited) lutil_passwd_init();

       bv.bv_val = strchr( scheme, '}' );
       if ( !bv.bv_val )
              return NULL;

       bv.bv_len = bv.bv_val - scheme + 1;
       bv.bv_val = (char *) scheme;

       for( pws=pw_schemes; pws; pws=pws->next ) {
              if ( ber_bvstrcasecmp(&bv, &pws->s.name ) == 0 ) {
                     return &(pws->s);
              }
       }

       return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int hash_md5 ( const struct berval scheme,
const struct berval passwd,
struct berval hash,
const char **  text 
) [static]

Definition at line 1109 of file passwd.c.

{
       lutil_MD5_CTX   MD5context;
       unsigned char   MD5digest[LUTIL_MD5_BYTES];

       struct berval digest;

       digest.bv_val = (char *) MD5digest;
       digest.bv_len = sizeof(MD5digest);

       lutil_MD5Init( &MD5context );
       lutil_MD5Update( &MD5context,
              (const unsigned char *) passwd->bv_val, passwd->bv_len );
       lutil_MD5Final( MD5digest, &MD5context );

       return lutil_passwd_string64( scheme, &digest, hash, NULL );
;
}

Here is the call graph for this function:

static int hash_smd5 ( const struct berval scheme,
const struct berval passwd,
struct berval hash,
const char **  text 
) [static]

Definition at line 1078 of file passwd.c.

{
       lutil_MD5_CTX   MD5context;
       unsigned char   MD5digest[LUTIL_MD5_BYTES];
       char            saltdata[SALT_SIZE];
       struct berval digest;
       struct berval salt;

       digest.bv_val = (char *) MD5digest;
       digest.bv_len = sizeof(MD5digest);
       salt.bv_val = saltdata;
       salt.bv_len = sizeof(saltdata);

       if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
              return LUTIL_PASSWD_ERR; 
       }

       lutil_MD5Init( &MD5context );
       lutil_MD5Update( &MD5context,
              (const unsigned char *) passwd->bv_val, passwd->bv_len );
       lutil_MD5Update( &MD5context,
              (const unsigned char *) salt.bv_val, salt.bv_len );
       lutil_MD5Final( MD5digest, &MD5context );

       return lutil_passwd_string64( scheme, &digest, hash, &salt );
}

Here is the call graph for this function:

static int is_allowed_scheme ( const char *  scheme,
const char **  schemes 
) [static]

Definition at line 260 of file passwd.c.

{
       int i;

       if( schemes == NULL ) return 1;

       for( i=0; schemes[i] != NULL; i++ ) {
              if( strcasecmp( scheme, schemes[i] ) == 0 ) {
                     return 1;
              }
       }
       return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int lutil_passwd ( const struct berval passwd,
const struct berval cred,
const char **  schemes,
const char **  text 
)

Definition at line 302 of file passwd.c.

{
       struct pw_slist *pws;

       if ( text ) *text = NULL;

       if (cred == NULL || cred->bv_len == 0 ||
              passwd == NULL || passwd->bv_len == 0 )
       {
              return -1;
       }

       if (!pw_inited) lutil_passwd_init();

       for( pws=pw_schemes; pws; pws=pws->next ) {
              if( pws->s.chk_fn ) {
                     struct berval x;
                     struct berval *p = passwd_scheme( &(pws->s),
                            passwd, &x, schemes );

                     if( p != NULL ) {
                            return (pws->s.chk_fn)( &(pws->s.name), p, cred, text );
                     }
              }
       }

#ifdef SLAPD_CLEARTEXT
       /* Do we think there is a scheme specifier here that we
        * didn't recognize? Assume a scheme name is at least 1 character.
        */
       if (( passwd->bv_val[0] == '{' ) &&
              ( ber_bvchr( passwd, '}' ) > passwd->bv_val+1 ))
       {
              return 1;
       }
       if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
              return ( passwd->bv_len == cred->bv_len ) ?
                     memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
                     : 1;
       }
#endif
       return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int lutil_passwd_add ( struct berval scheme,
LUTIL_PASSWD_CHK_FUNC chk,
LUTIL_PASSWD_HASH_FUNC hash 
)

Definition at line 185 of file passwd.c.

{
       struct pw_slist *ptr;

       if (!pw_inited) lutil_passwd_init();

       ptr = ber_memalloc( sizeof( struct pw_slist ));
       if (!ptr) return -1;
       ptr->next = pw_schemes;
       ptr->s.name = *scheme;
       ptr->s.chk_fn = chk;
       ptr->s.hash_fn = hash;
       pw_schemes = ptr;
       return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 215 of file passwd.c.

{
       struct pw_slist *ptr, *next;

       for( ptr=pw_schemes; ptr; ptr=next ) {
              next = ptr->next;
              ber_memfree( ptr );
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

int lutil_passwd_generate ( struct berval pw,
ber_len_t  len 
)

Definition at line 350 of file passwd.c.

{

       if( len < 1 ) return -1;

       pw->bv_len = len;
       pw->bv_val = ber_memalloc( len + 1 );

       if( pw->bv_val == NULL ) {
              return -1;
       }

       if( lutil_entropy( (unsigned char *) pw->bv_val, pw->bv_len) < 0 ) {
              return -1; 
       }

       for( len = 0; len < pw->bv_len; len++ ) {
              pw->bv_val[len] = crypt64[
                     pw->bv_val[len] % (sizeof(crypt64)-1) ];
       }

       pw->bv_val[len] = '\0';
       
       return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int lutil_passwd_hash ( const struct berval passwd,
const char *  method,
struct berval hash,
const char **  text 
)

Definition at line 376 of file passwd.c.

{
       const struct pw_scheme *sc = get_scheme( method );

       hash->bv_val = NULL;
       hash->bv_len = 0;

       if( sc == NULL ) {
              if( text ) *text = "scheme not recognized";
              return -1;
       }

       if( ! sc->hash_fn ) {
              if( text ) *text = "scheme provided no hash function";
              return -1;
       }

       if( text ) *text = NULL;

       return (sc->hash_fn)( &sc->name, passwd, hash, text );
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 204 of file passwd.c.

{
       struct pw_scheme *s;

       pw_inited = 1;

       for( s=(struct pw_scheme *)pw_schemes_default; s->name.bv_val; s++) {
              if ( lutil_passwd_add( &s->name, s->chk_fn, s->hash_fn ) ) break;
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

int lutil_passwd_scheme ( const char *  scheme)

Definition at line 249 of file passwd.c.

{
       if( scheme == NULL ) {
              return 0;
       }

       return get_scheme(scheme) != NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int lutil_passwd_string64 ( const struct berval sc,
const struct berval hash,
struct berval b64,
const struct berval salt 
)

Definition at line 427 of file passwd.c.

{
       int rc;
       struct berval string;
       size_t b64len;

       if( salt ) {
              /* need to base64 combined string */
              string.bv_len = hash->bv_len + salt->bv_len;
              string.bv_val = ber_memalloc( string.bv_len + 1 );

              if( string.bv_val == NULL ) {
                     return LUTIL_PASSWD_ERR;
              }

              AC_MEMCPY( string.bv_val, hash->bv_val,
                     hash->bv_len );
              AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
                     salt->bv_len );
              string.bv_val[string.bv_len] = '\0';

       } else {
              string = *hash;
       }

       b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
       b64->bv_len = b64len + sc->bv_len;
       b64->bv_val = ber_memalloc( b64->bv_len + 1 );

       if( b64->bv_val == NULL ) {
              if( salt ) ber_memfree( string.bv_val );
              return LUTIL_PASSWD_ERR;
       }

       AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);

       rc = lutil_b64_ntop(
              (unsigned char *) string.bv_val, string.bv_len,
              &b64->bv_val[sc->bv_len], b64len );

       if( salt ) ber_memfree( string.bv_val );
       
       if( rc < 0 ) {
              return LUTIL_PASSWD_ERR;
       }

       /* recompute length */
       b64->bv_len = sc->bv_len + rc;
       assert( strlen(b64->bv_val) == b64->bv_len );
       return LUTIL_PASSWD_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int lutil_salt_format ( const char *  format)

Definition at line 1241 of file passwd.c.

{
#ifdef SLAPD_CRYPT
       ber_memfree( salt_format );

       salt_format = format != NULL ? ber_strdup( format ) : NULL;
#endif

       return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static struct berval* passwd_scheme ( const struct pw_scheme scheme,
const struct berval passwd,
struct berval bv,
const char **  allowed 
) [static, read]

Definition at line 276 of file passwd.c.

{
       if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
              return NULL;
       }

       if( passwd->bv_len >= scheme->name.bv_len ) {
              if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
                     bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
                     bv->bv_len = passwd->bv_len - scheme->name.bv_len;

                     return bv;
              }
       }

       return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 120 of file passwd.c.

Definition at line 121 of file passwd.c.

const unsigned char crypt64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./" [static]

Definition at line 92 of file passwd.c.

Definition at line 123 of file passwd.c.

Definition at line 122 of file passwd.c.

int pw_inited [static]

Definition at line 154 of file passwd.c.

struct pw_scheme[] [static]
Initial value:
{





       { BER_BVC("{SMD5}"),        chk_smd5, hash_smd5 },
       { BER_BVC("{MD5}"),                chk_md5, hash_md5 },



 













       { BER_BVNULL, NULL, NULL }
}

Definition at line 156 of file passwd.c.

struct pw_slist* pw_schemes [static]

Definition at line 153 of file passwd.c.