|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | hash: hashing library for IBM DB2 | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | Copyright (c) 2007-2012 Helmut K. C. Tessarek | |
| 6 | + +----------------------------------------------------------------------+ |
| 7 | + | Licensed under the Apache License, Version 2.0 (the "License"); you | |
| 8 | + | may not use this file except in compliance with the License. You may | |
| 9 | + | obtain a copy of the License at | |
| 10 | + | http://www.apache.org/licenses/LICENSE-2.0 | |
| 11 | + | | |
| 12 | + | Unless required by applicable law or agreed to in writing, software | |
| 13 | + | distributed under the License is distributed on an "AS IS" BASIS, | |
| 14 | + | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | |
| 15 | + | implied. See the License for the specific language governing | |
| 16 | + | permissions and limitations under the License. | |
| 17 | + +----------------------------------------------------------------------+ |
| 18 | + | Author: Helmut K. C. Tessarek | |
| 19 | + +----------------------------------------------------------------------+ |
| 20 | + | Website: http://mod-auth-ibmdb2.sourceforge.net | |
| 21 | + +----------------------------------------------------------------------+ |
| 22 | +*/ |
| 23 | + |
| 24 | +/* $Id: hash.c,v 1.2 2012/11/25 07:59:12 tessus Exp $ */ |
| 25 | + |
| 26 | +#include <stdio.h> |
| 27 | +#include <sqludf.h> |
| 28 | +#include <sqlca.h> |
| 29 | +#include <sqlda.h> |
| 30 | +#include "hash.h" |
| 31 | + |
| 32 | +/*--------------------------------------------------*/ |
| 33 | +/* function md5: MD5 Hashing */ |
| 34 | +/* */ |
| 35 | +/* input : varchar */ |
| 36 | +/* output: varchar */ |
| 37 | +/*--------------------------------------------------*/ |
| 38 | + |
| 39 | +#ifdef __cplusplus |
| 40 | +extern "C" |
| 41 | +#endif |
| 42 | +void SQL_API_FN md5( SQLUDF_CHAR *in, |
| 43 | + SQLUDF_CHAR out[33], |
| 44 | + SQLUDF_SMALLINT *innull, |
| 45 | + SQLUDF_SMALLINT *outnull, |
| 46 | + SQLUDF_TRAIL_ARGS) |
| 47 | +{ |
| 48 | + char *t; |
| 49 | + |
| 50 | + if( *innull != 0 ) |
| 51 | + { |
| 52 | + *outnull = -1; |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + t = mk_hash( in, ALG_MD5 ); |
| 57 | + strcpy( out, t ); |
| 58 | + free( t ); |
| 59 | + |
| 60 | + *outnull = 0; |
| 61 | + return; |
| 62 | +} |
| 63 | + |
| 64 | +/*--------------------------------------------------*/ |
| 65 | +/* function apr_md5: MD5 Hashing as in the htpasswd */ |
| 66 | +/* program from Apache */ |
| 67 | +/* */ |
| 68 | +/* input : varchar */ |
| 69 | +/* output: varchar */ |
| 70 | +/*--------------------------------------------------*/ |
| 71 | + |
| 72 | +#ifdef __cplusplus |
| 73 | +extern "C" |
| 74 | +#endif |
| 75 | +void SQL_API_FN aprmd5( SQLUDF_CHAR *in, |
| 76 | + SQLUDF_CHAR out[38], |
| 77 | + SQLUDF_SMALLINT *innull, |
| 78 | + SQLUDF_SMALLINT *outnull, |
| 79 | + SQLUDF_TRAIL_ARGS) |
| 80 | +{ |
| 81 | + char *t; |
| 82 | + |
| 83 | + if( *innull != 0 ) |
| 84 | + { |
| 85 | + *outnull = -1; |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + t = mk_hash( in, ALG_APMD5 ); |
| 90 | + strcpy( out, t ); |
| 91 | + free( t ); |
| 92 | + |
| 93 | + *outnull = 0; |
| 94 | + return; |
| 95 | +} |
| 96 | + |
| 97 | +/*--------------------------------------------------*/ |
| 98 | +/* function apr_crypt: Crypt fuction as in the */ |
| 99 | +/* htpasswd program from Apache */ |
| 100 | +/* */ |
| 101 | +/* input : varchar */ |
| 102 | +/* output: varchar */ |
| 103 | +/*--------------------------------------------------*/ |
| 104 | + |
| 105 | +#ifdef __cplusplus |
| 106 | +extern "C" |
| 107 | +#endif |
| 108 | +void SQL_API_FN aprcrypt( SQLUDF_CHAR *in, |
| 109 | + SQLUDF_CHAR out[14], |
| 110 | + SQLUDF_SMALLINT *innull, |
| 111 | + SQLUDF_SMALLINT *outnull, |
| 112 | + SQLUDF_TRAIL_ARGS) |
| 113 | +{ |
| 114 | + char *t; |
| 115 | + |
| 116 | + if( *innull != 0 ) |
| 117 | + { |
| 118 | + *outnull = -1; |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + t = mk_hash( in, ALG_CRYPT ); |
| 123 | + strcpy( out, t ); |
| 124 | + free( t ); |
| 125 | + |
| 126 | + *outnull = 0; |
| 127 | + return; |
| 128 | +} |
| 129 | + |
| 130 | +/*--------------------------------------------------*/ |
| 131 | +/* function apr_sha1: SHA1 fuction as in the */ |
| 132 | +/* htpasswd program from Apache */ |
| 133 | +/* */ |
| 134 | +/* input : varchar */ |
| 135 | +/* output: varchar */ |
| 136 | +/*--------------------------------------------------*/ |
| 137 | + |
| 138 | +#ifdef __cplusplus |
| 139 | +extern "C" |
| 140 | +#endif |
| 141 | +void SQL_API_FN aprsha1( SQLUDF_CHAR *in, |
| 142 | + SQLUDF_CHAR out[34], |
| 143 | + SQLUDF_SMALLINT *innull, |
| 144 | + SQLUDF_SMALLINT *outnull, |
| 145 | + SQLUDF_TRAIL_ARGS) |
| 146 | +{ |
| 147 | + char *t; |
| 148 | + |
| 149 | + if( *innull != 0 ) |
| 150 | + { |
| 151 | + *outnull = -1; |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + t = mk_hash( in, ALG_APSHA ); |
| 156 | + strcpy( out, t ); |
| 157 | + free( t ); |
| 158 | + |
| 159 | + *outnull = 0; |
| 160 | + return; |
| 161 | +} |
| 162 | + |
| 163 | +/*--------------------------------------------------*/ |
| 164 | +/* function validate : validates the hash */ |
| 165 | +/* */ |
| 166 | +/* input1: varchar */ |
| 167 | +/* input2: varchar */ |
| 168 | +/* output: integer */ |
| 169 | +/*--------------------------------------------------*/ |
| 170 | + |
| 171 | +#ifdef __cplusplus |
| 172 | +extern "C" |
| 173 | +#endif |
| 174 | +SQL_API_RC SQL_API_FN validate( SQLUDF_CHAR *password, |
| 175 | + SQLUDF_CHAR *hash, |
| 176 | + SQLUDF_INTEGER *out, |
| 177 | + SQLUDF_SMALLINT *passwordNullInd, |
| 178 | + SQLUDF_SMALLINT *hashNullInd, |
| 179 | + SQLUDF_SMALLINT *outNullInd, |
| 180 | + SQLUDF_TRAIL_ARGS) |
| 181 | +{ |
| 182 | + apr_status_t status; |
| 183 | + char *md5, *result; |
| 184 | + |
| 185 | + *out = -1; |
| 186 | + *outNullInd = -1; |
| 187 | + |
| 188 | + if( *passwordNullInd != 0 || *hashNullInd != 0 ) |
| 189 | + { |
| 190 | + *outNullInd = -1; |
| 191 | + return(0); |
| 192 | + } |
| 193 | + |
| 194 | + if( strlen(hash) == 0 ) |
| 195 | + { |
| 196 | + strcpy(SQLUDF_STATE, "39701"); |
| 197 | + strcpy(SQLUDF_MSGTX, "The second parameter (hash) must not be empty."); |
| 198 | + *outNullInd = 0; |
| 199 | + return(0); |
| 200 | + } |
| 201 | + |
| 202 | + if( strlen(hash) == 32 ) |
| 203 | + { |
| 204 | + md5 = mk_hash( password, ALG_MD5 ); |
| 205 | + |
| 206 | + if( apr_strnatcmp( hash, md5 ) == 0 ) |
| 207 | + { |
| 208 | + *out = 1; |
| 209 | + } |
| 210 | + else |
| 211 | + { |
| 212 | + *out = 0; |
| 213 | + } |
| 214 | + |
| 215 | + free(md5); |
| 216 | + |
| 217 | + *outNullInd = 0; |
| 218 | + return(0); |
| 219 | + } |
| 220 | + |
| 221 | + status = apr_password_validate( password, hash ); |
| 222 | + |
| 223 | + if( status == APR_SUCCESS ) |
| 224 | + { |
| 225 | + *out = 1; |
| 226 | + } |
| 227 | + else |
| 228 | + { |
| 229 | + // maybe a different encrypted password (glibc2 crypt)? |
| 230 | + result = crypt( password, hash ); |
| 231 | + if( strcmp( hash, result ) == 0 ) |
| 232 | + { |
| 233 | + *out = 1; |
| 234 | + } |
| 235 | + else |
| 236 | + { |
| 237 | + *out = 0; |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + *outNullInd = 0; |
| 242 | + return(0); |
| 243 | +} |
0 commit comments