Skip to content

Commit 6477a9c

Browse files
committed
move validate function to hash.c, db2hash.c is the DB2 wrapper
1 parent 8a157de commit 6477a9c

File tree

3 files changed

+101
-97
lines changed

3 files changed

+101
-97
lines changed

db2hash.c

Lines changed: 2 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -495,9 +495,6 @@ SQL_API_RC SQL_API_FN validate( SQLUDF_CHAR *password,
495495
SQLUDF_SMALLINT *outNullInd,
496496
SQLUDF_TRAIL_ARGS)
497497
{
498-
apr_status_t status;
499-
char *tmphash, *result;
500-
501498
*out = -1;
502499
*outNullInd = -1;
503500

@@ -515,91 +512,14 @@ SQL_API_RC SQL_API_FN validate( SQLUDF_CHAR *password,
515512
return(0);
516513
}
517514

518-
if( !strncmp( hash, APR_SHA256PW_ID, APR_SHA256PW_IDLEN ) )
519-
{
520-
tmphash = mk_hash( ALG_APSHA256, password, NULL );
521-
522-
if( apr_strnatcmp( hash, tmphash ) == 0 )
523-
{
524-
*out = 1;
525-
}
526-
else
527-
{
528-
*out = 0;
529-
}
530-
531-
free(tmphash);
532-
533-
*outNullInd = 0;
534-
return(0);
535-
}
536-
537-
if( strlen(hash) == 32 && (hash[0] != '$') )
538-
{
539-
tmphash = mk_hash( ALG_PHPMD5, password, NULL );
540-
541-
if( apr_strnatcmp( hash, tmphash ) == 0 )
542-
{
543-
*out = 1;
544-
}
545-
else
546-
{
547-
*out = 0;
548-
}
549-
550-
free(tmphash);
551-
552-
*outNullInd = 0;
553-
return(0);
554-
}
555-
556-
if( strlen(hash) == 64 && (hash[0] != '$') )
557-
{
558-
tmphash = mk_hash( ALG_SHA256HEX, password, NULL );
559-
560-
if( apr_strnatcmp( hash, tmphash ) == 0 )
561-
{
562-
*out = 1;
563-
}
564-
else
565-
{
566-
*out = 0;
567-
}
568-
569-
free(tmphash);
570-
571-
*outNullInd = 0;
572-
return(0);
573-
}
574-
575-
status = apr_password_validate( password, hash );
576-
577-
if( status == APR_SUCCESS )
515+
if( validate_hash(password, hash) )
578516
{
579517
*out = 1;
580518
}
581-
#ifndef WIN32
582519
else
583520
{
584-
// maybe a different encrypted password (glibc2 crypt)?
585-
result = crypt( password, hash );
586-
if( result != NULL )
587-
{
588-
if( strcmp( hash, result ) == 0 )
589-
{
590-
*out = 1;
591-
}
592-
else
593-
{
594-
*out = 0;
595-
}
596-
}
597-
else
598-
{
599-
*out = 0;
600-
}
521+
*out = 0;
601522
}
602-
#endif
603523

604524
*outNullInd = 0;
605525
return(0);

hash.c

Lines changed: 98 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ void sha256_base64(const char *clear, int len, char *out)
107107
SHA256_CTX context;
108108
apr_byte_t digest[SHA256_DIGEST_LENGTH];
109109

110-
apr__SHA256_Init( &context );
111-
apr__SHA256_Update( &context, (const unsigned char *)clear, len );
112-
apr__SHA256_Final( digest, &context );
110+
apr__SHA256_Init(&context);
111+
apr__SHA256_Update(&context, (const unsigned char *)clear, len);
112+
apr__SHA256_Final(digest, &context);
113113

114-
apr_cpystrn( out, APR_SHA256PW_ID, APR_SHA256PW_IDLEN + 1 );
114+
apr_cpystrn(out, APR_SHA256PW_ID, APR_SHA256PW_IDLEN + 1);
115115

116-
l = apr_base64_encode_binary( out + APR_SHA256PW_IDLEN, digest, sizeof(digest) );
116+
l = apr_base64_encode_binary(out + APR_SHA256PW_IDLEN, digest, sizeof(digest));
117117
out[l + APR_SHA256PW_IDLEN] = '\0';
118118
}
119119

@@ -218,12 +218,12 @@ char* mk_hash(int alg, const char *passwd, const char *mysalt)
218218
case ALG_PHPMD5:
219219
md5str[0] = '\0';
220220

221-
apr_md5_init( &context );
222-
apr_md5_update( &context, passwd, strlen(passwd) );
223-
apr_md5_final( digest, &context );
224-
for( i = 0, r = md5str; i < APR_MD5_DIGESTSIZE; i++, r += 2 )
221+
apr_md5_init(&context);
222+
apr_md5_update(&context, passwd, strlen(passwd));
223+
apr_md5_final(digest, &context);
224+
for (i = 0, r = md5str; i < APR_MD5_DIGESTSIZE; i++, r += 2)
225225
{
226-
sprintf( r, "%02x", digest[i] );
226+
sprintf(r, "%02x", digest[i]);
227227
}
228228
*r = '\0';
229229

@@ -234,12 +234,12 @@ char* mk_hash(int alg, const char *passwd, const char *mysalt)
234234
case ALG_SHA256HEX:
235235
sha256str[0] = '\0';
236236

237-
apr__SHA256_Init( &context256 );
238-
apr__SHA256_Update( &context256, passwd, strlen(passwd) );
239-
apr__SHA256_Final( digest256, &context256 );
240-
for( i = 0, r = sha256str; i < SHA256_DIGEST_LENGTH; i++, r += 2 )
237+
apr__SHA256_Init(&context256);
238+
apr__SHA256_Update(&context256, passwd, strlen(passwd));
239+
apr__SHA256_Final(digest256, &context256);
240+
for (i = 0, r = sha256str; i < SHA256_DIGEST_LENGTH; i++, r += 2)
241241
{
242-
sprintf( r, "%02x", digest256[i] );
242+
sprintf(r, "%02x", digest256[i]);
243243
}
244244
*r = '\0';
245245

@@ -254,3 +254,86 @@ char* mk_hash(int alg, const char *passwd, const char *mysalt)
254254

255255
return result;
256256
}
257+
258+
int validate_hash(const char *password, const char *hash)
259+
{
260+
apr_status_t status;
261+
char *tmphash, *result;
262+
263+
if (!strncmp(hash, APR_SHA256PW_ID, APR_SHA256PW_IDLEN))
264+
{
265+
tmphash = mk_hash(ALG_APSHA256, password, NULL);
266+
267+
if (apr_strnatcmp(hash, tmphash) == 0)
268+
{
269+
free(tmphash);
270+
return TRUE;
271+
}
272+
else
273+
{
274+
free(tmphash);
275+
return FALSE;
276+
}
277+
}
278+
279+
if (strlen(hash) == 32 && (hash[0] != '$'))
280+
{
281+
tmphash = mk_hash(ALG_PHPMD5, password, NULL);
282+
283+
if (apr_strnatcmp(hash, tmphash) == 0)
284+
{
285+
free(tmphash);
286+
return TRUE;
287+
}
288+
else
289+
{
290+
free(tmphash);
291+
return FALSE;
292+
}
293+
}
294+
295+
if (strlen(hash) == 64 && (hash[0] != '$'))
296+
{
297+
tmphash = mk_hash(ALG_SHA256HEX, password, NULL);
298+
299+
if (apr_strnatcmp(hash, tmphash) == 0)
300+
{
301+
free(tmphash);
302+
return TRUE;
303+
}
304+
else
305+
{
306+
free(tmphash);
307+
return FALSE;
308+
}
309+
}
310+
311+
status = apr_password_validate(password, hash);
312+
313+
if (status == APR_SUCCESS)
314+
{
315+
return TRUE;
316+
}
317+
#ifndef WIN32
318+
else
319+
{
320+
// maybe a different encrypted password (glibc2 crypt)?
321+
result = crypt(password, hash);
322+
if (result != NULL)
323+
{
324+
if (strcmp(hash, result) == 0)
325+
{
326+
return TRUE;
327+
}
328+
else
329+
{
330+
return FALSE;
331+
}
332+
}
333+
else
334+
{
335+
return FALSE;
336+
}
337+
}
338+
#endif
339+
}

hash.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,6 @@ int is_valid_salt(const char *salt);
7575
int supported(int alg);
7676
void sha256_base64(const char *clear, int len, char *out);
7777
char* mk_hash(int alg, const char *passwd, const char *mysalt);
78+
int validate_hash(const char *password, const char *hash);
7879

7980
#endif

0 commit comments

Comments
 (0)