| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#include "conf.h" |
| 30 |
|
| 31 |
#define MOD_DIGEST_VERSION "mod_digest/2.0.1" |
| 32 |
|
| 33 |
|
| 34 |
#ifndef C_HASH |
| 35 |
# define C_HASH "HASH" |
| 36 |
#endif |
| 37 |
#ifndef C_MD5 |
| 38 |
# define C_MD5 "MD5" |
| 39 |
#endif |
| 40 |
#ifndef C_XCRC |
| 41 |
# define C_XCRC "XCRC" |
| 42 |
#endif |
| 43 |
#ifndef C_XMD5 |
| 44 |
# define C_XMD5 "XMD5" |
| 45 |
#endif |
| 46 |
#ifndef C_XSHA |
| 47 |
# define C_XSHA "XSHA" |
| 48 |
#endif |
| 49 |
#ifndef C_XSHA1 |
| 50 |
# define C_XSHA1 "XSHA1" |
| 51 |
#endif |
| 52 |
#ifndef C_XSHA256 |
| 53 |
# define C_XSHA256 "XSHA256" |
| 54 |
#endif |
| 55 |
#ifndef C_XSHA512 |
| 56 |
# define C_XSHA512 "XSHA512" |
| 57 |
#endif |
| 58 |
|
| 59 |
|
| 60 |
#ifndef R_251 |
| 61 |
# define R_251 "251" |
| 62 |
#endif |
| 63 |
#ifndef R_556 |
| 64 |
# define R_556 "556" |
| 65 |
#endif |
| 66 |
|
| 67 |
|
| 68 |
#if PROFTPD_VERSION_NUMBER < 0x0001030602 |
| 69 |
# error "ProFTPD 1.3.6rc2 or later required" |
| 70 |
#endif |
| 71 |
|
| 72 |
#if !defined(HAVE_OPENSSL) && !defined(PR_USE_OPENSSL) |
| 73 |
# error "OpenSSL support required (--enable-openssl)" |
| 74 |
#else |
| 75 |
# include <openssl/bio.h> |
| 76 |
# include <openssl/evp.h> |
| 77 |
# include <openssl/err.h> |
| 78 |
#endif |
| 79 |
|
| 80 |
|
| 81 |
#if defined(LIBRESSL_VERSION_NUMBER) |
| 82 |
# define HAVE_LIBRESSL 1 |
| 83 |
#endif |
| 84 |
|
| 85 |
#if OPENSSL_VERSION_NUMBER >= 0x40000000L && !defined(HAVE_LIBRESSL) |
| 86 |
# include <openssl/core.h> |
| 87 |
# include <openssl/core_dispatch.h> |
| 88 |
# include <openssl/core_names.h> |
| 89 |
# include <openssl/params.h> |
| 90 |
# include <openssl/provider.h> |
| 91 |
#endif |
| 92 |
|
| 93 |
module digest_module; |
| 94 |
|
| 95 |
static int digest_caching = TRUE; |
| 96 |
|
| 97 |
#ifndef DIGEST_CACHE_DEFAULT_SIZE |
| 98 |
# define DIGEST_CACHE_DEFAULT_SIZE 10000 |
| 99 |
#endif |
| 100 |
#ifndef DIGEST_CACHE_DEFAULT_MAX_AGE |
| 101 |
# define DIGEST_CACHE_DEFAULT_MAX_AGE 30 |
| 102 |
#endif |
| 103 |
static unsigned int digest_cache_max_size = DIGEST_CACHE_DEFAULT_SIZE; |
| 104 |
static unsigned int digest_cache_max_age = DIGEST_CACHE_DEFAULT_MAX_AGE; |
| 105 |
|
| 106 |
static EVP_MD_CTX *digest_cache_xfer_ctx = NULL; |
| 107 |
|
| 108 |
static int digest_engine = TRUE; |
| 109 |
static pool *digest_pool = NULL; |
| 110 |
|
| 111 |
#define DIGEST_OPT_NO_TRANSFER_CACHE 0x0001 |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
#if PROFTPD_VERSION_NUMBER < 0x0001030602 |
| 118 |
# define DIGEST_DEFAULT_OPTS DIGEST_OPT_NO_TRANSFER |
| 119 |
#else |
| 120 |
# define DIGEST_DEFAULT_OPTS 0UL |
| 121 |
#endif |
| 122 |
|
| 123 |
static unsigned long digest_opts = DIGEST_DEFAULT_OPTS; |
| 124 |
|
| 125 |
|
| 126 |
static pr_table_t *digest_crc32_tab = NULL; |
| 127 |
static pr_table_t *digest_md5_tab = NULL; |
| 128 |
static pr_table_t *digest_sha1_tab = NULL; |
| 129 |
static pr_table_t *digest_sha256_tab = NULL; |
| 130 |
static pr_table_t *digest_sha512_tab = NULL; |
| 131 |
|
| 132 |
|
| 133 |
struct digest_cache_key { |
| 134 |
struct digest_cache_key *next, *prev; |
| 135 |
pool *pool; |
| 136 |
unsigned long algo; |
| 137 |
const char *path; |
| 138 |
time_t mtime; |
| 139 |
off_t start; |
| 140 |
off_t len; |
| 141 |
const char *key; |
| 142 |
const char *hex_digest; |
| 143 |
}; |
| 144 |
|
| 145 |
static xaset_t *digest_cache_keys = NULL; |
| 146 |
|
| 147 |
|
| 148 |
#define DIGEST_CACHE_EXPIRY_INTVL 5 |
| 149 |
|
| 150 |
|
| 151 |
#define DIGEST_ALGO_CRC32 0x0001 |
| 152 |
#ifndef OPENSSL_NO_MD5 |
| 153 |
# define DIGEST_ALGO_MD5 0x0002 |
| 154 |
#else |
| 155 |
# define DIGEST_ALGO_MD5 0x0000 |
| 156 |
#endif |
| 157 |
#ifndef OPENSSL_NO_SHA |
| 158 |
# define DIGEST_ALGO_SHA1 0x0004 |
| 159 |
#else |
| 160 |
# define DIGEST_ALGO_SHA1 0x0000 |
| 161 |
#endif |
| 162 |
#ifndef OPENSSL_NO_SHA256 |
| 163 |
# define DIGEST_ALGO_SHA256 0x0008 |
| 164 |
#else |
| 165 |
# define DIGEST_ALGO_SHA256 0x0000 |
| 166 |
#endif |
| 167 |
#ifndef OPENSSL_NO_SHA512 |
| 168 |
# define DIGEST_ALGO_SHA512 0x0010 |
| 169 |
#else |
| 170 |
# define DIGEST_ALGO_SHA512 0x0000 |
| 171 |
#endif |
| 172 |
|
| 173 |
#define DIGEST_DEFAULT_ALGOS \ |
| 174 |
(DIGEST_ALGO_CRC32|DIGEST_ALGO_MD5|DIGEST_ALGO_SHA1|DIGEST_ALGO_SHA256|DIGEST_ALGO_SHA512) |
| 175 |
|
| 176 |
static unsigned long digest_algos = DIGEST_DEFAULT_ALGOS; |
| 177 |
|
| 178 |
static const EVP_MD *digest_hash_md = NULL; |
| 179 |
static unsigned long digest_hash_algo = DIGEST_ALGO_SHA1; |
| 180 |
static int digest_hash_free_md = FALSE; |
| 181 |
|
| 182 |
|
| 183 |
#define DIGEST_ALGO_FL_IANA_STYLE 0x0001 |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
#ifndef DIGEST_PROGRESS_NTH_ITER |
| 189 |
# define DIGEST_PROGRESS_NTH_ITER 40000 |
| 190 |
#endif |
| 191 |
|
| 192 |
#if OPENSSL_VERSION_NUMBER >= 0x40000000L && !defined(HAVE_LIBRESSL) |
| 193 |
static OSSL_PROVIDER *crc32_provider = NULL; |
| 194 |
#endif |
| 195 |
|
| 196 |
static const char *trace_channel = "digest"; |
| 197 |
|
| 198 |
|
| 199 |
static void digest_data_xfer_ev(const void *event_data, void *user_data); |
| 200 |
static int digest_sess_init(void); |
| 201 |
static const char *get_algo_name(unsigned long algo, int flags); |
| 202 |
|
| 203 |
#if PROFTPD_VERSION_NUMBER < 0x0001030602 |
| 204 |
# define PR_STR_FL_HEX_USE_UC 0x0001 |
| 205 |
# define PR_STR_FL_HEX_USE_LC 0x0002 |
| 206 |
# define pr_str_bin2hex digest_bin2hex |
| 207 |
|
| 208 |
static char *digest_bin2hex(pool *p, const unsigned char *buf, size_len, |
| 209 |
int flags) { |
| 210 |
static const char *hex_lc = "0123456789abcdef", *hex_uc = "0123456789ABCDEF"; |
| 211 |
register unsigned int i; |
| 212 |
const char *hex_vals; |
| 213 |
char *hex, *ptr; |
| 214 |
size_t hex_len; |
| 215 |
|
| 216 |
if (p == NULL || |
| 217 |
buf == NULL) { |
| 218 |
errno = EINVAL; |
| 219 |
return NULL; |
| 220 |
} |
| 221 |
|
| 222 |
if (len == 0) { |
| 223 |
return pstrdup(p, ""); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
hex_vals = hex_lc; |
| 228 |
if (flags & PR_STR_FL_HEX_USE_UC) { |
| 229 |
hex_vals = hex_uc; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
hex_len = (len * 2) + 1; |
| 234 |
hex = palloc(p, hex_len); |
| 235 |
|
| 236 |
ptr = hex; |
| 237 |
for (i = 0; i < len; i++) { |
| 238 |
*ptr++ = hex_vals[buf[i] >> 4]; |
| 239 |
*ptr++ = hex_vals[buf[i] % 16]; |
| 240 |
} |
| 241 |
*ptr = '\0'; |
| 242 |
|
| 243 |
return hex; |
| 244 |
} |
| 245 |
#endif |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
#define CRC32_BLOCK 4 |
| 255 |
#define CRC32_DIGEST_LENGTH 4 |
| 256 |
#define CRC32_TABLE_SIZE 256 |
| 257 |
|
| 258 |
typedef struct crc32_ctx_st { |
| 259 |
uint32_t *crc32_table; |
| 260 |
uint32_t data; |
| 261 |
} CRC32_CTX; |
| 262 |
|
| 263 |
static int CRC32_Init(CRC32_CTX *ctx) { |
| 264 |
register unsigned int i; |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
ctx->crc32_table = malloc(sizeof(uint32_t) * CRC32_TABLE_SIZE); |
| 271 |
if (ctx->crc32_table == NULL) { |
| 272 |
errno = ENOMEM; |
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 276 |
for (i = 0; i < CRC32_TABLE_SIZE; i++) { |
| 277 |
register unsigned int j; |
| 278 |
uint32_t crc; |
| 279 |
|
| 280 |
crc = i; |
| 281 |
for (j = 8; j > 0; j--) { |
| 282 |
if (crc & 1) { |
| 283 |
crc = (crc >> 1) ^ 0xEDB88320; |
| 284 |
} else { |
| 285 |
crc >>= 1; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
ctx->crc32_table[i] = crc; |
| 290 |
} |
| 291 |
|
| 292 |
ctx->data = 0xffffffff; |
| 293 |
return 1; |
| 294 |
} |
| 295 |
|
| 296 |
#define CRC32(ctx, c, b) (ctx->crc32_table[((int)(c) ^ (b)) & 0xff] ^ ((c) >> 8)) |
| 297 |
#define DOCRC(ctx, c, d) c = CRC32(ctx, c, *d++) |
| 298 |
|
| 299 |
static int CRC32_Update(CRC32_CTX *ctx, const unsigned char *data, |
| 300 |
size_t datasz) { |
| 301 |
|
| 302 |
if (datasz == 0) { |
| 303 |
return 1; |
| 304 |
} |
| 305 |
|
| 306 |
while (datasz > 0) { |
| 307 |
DOCRC(ctx, ctx->data, data); |
| 308 |
datasz--; |
| 309 |
} |
| 310 |
|
| 311 |
return 1; |
| 312 |
} |
| 313 |
|
| 314 |
static int CRC32_Final(unsigned char *md, CRC32_CTX *ctx) { |
| 315 |
uint32_t crc; |
| 316 |
|
| 317 |
crc = ctx->data; |
| 318 |
crc ^= 0xffffffff; |
| 319 |
crc = htonl(crc); |
| 320 |
|
| 321 |
memcpy(md, &crc, sizeof(crc)); |
| 322 |
return 1; |
| 323 |
} |
| 324 |
|
| 325 |
static int CRC32_Free(CRC32_CTX *ctx) { |
| 326 |
if (ctx->crc32_table != NULL) { |
| 327 |
free(ctx->crc32_table); |
| 328 |
ctx->crc32_table = NULL; |
| 329 |
} |
| 330 |
|
| 331 |
return 1; |
| 332 |
} |
| 333 |
|
| 334 |
#if OPENSSL_VERSION_NUMBER >= 0x40000000L && !defined(HAVE_LIBRESSL) |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
static void *crc32_ctx_new(void *vctx) { |
| 339 |
CRC32_CTX *ctx; |
| 340 |
|
| 341 |
ctx = OPENSSL_zalloc(sizeof(CRC32_CTX)); |
| 342 |
return ctx; |
| 343 |
} |
| 344 |
|
| 345 |
static void crc32_ctx_free(void *vctx) { |
| 346 |
CRC32_CTX *ctx; |
| 347 |
|
| 348 |
ctx = vctx; |
| 349 |
CRC32_Free(ctx); |
| 350 |
OPENSSL_free(ctx); |
| 351 |
} |
| 352 |
|
| 353 |
static int crc32_md_init(void *vctx) { |
| 354 |
CRC32_CTX *ctx; |
| 355 |
|
| 356 |
ctx = vctx; |
| 357 |
return CRC32_Init(ctx); |
| 358 |
} |
| 359 |
|
| 360 |
static int crc32_md_update(void *vctx, const unsigned char *data, size_t len) { |
| 361 |
CRC32_CTX *ctx; |
| 362 |
|
| 363 |
ctx = vctx; |
| 364 |
return CRC32_Update(ctx, data, len); |
| 365 |
} |
| 366 |
|
| 367 |
static int crc32_md_final(void *vctx, unsigned char *out, size_t *out_len, |
| 368 |
size_t outsz) { |
| 369 |
CRC32_CTX *ctx; |
| 370 |
|
| 371 |
ctx = vctx; |
| 372 |
|
| 373 |
*out_len = outsz; |
| 374 |
|
| 375 |
if (outsz != 0) { |
| 376 |
return CRC32_Final(out, ctx); |
| 377 |
} |
| 378 |
|
| 379 |
return 1; |
| 380 |
} |
| 381 |
|
| 382 |
static const OSSL_PARAM crc32_params[] = { |
| 383 |
OSSL_PARAM_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, NULL), |
| 384 |
OSSL_PARAM_size_t(OSSL_DIGEST_PARAM_SIZE, NULL), |
| 385 |
OSSL_PARAM_END |
| 386 |
}; |
| 387 |
|
| 388 |
static int crc32_get_params(void *provctx, OSSL_PARAM params[]) { |
| 389 |
OSSL_PARAM *p; |
| 390 |
int ok = 1; |
| 391 |
|
| 392 |
p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_BLOCK_SIZE); |
| 393 |
if (p != NULL) { |
| 394 |
if (OSSL_PARAM_set_size_t(p, CRC32_BLOCK) != 1) { |
| 395 |
ok = 0; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
if (ok == 1) { |
| 400 |
p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); |
| 401 |
if (p != NULL) { |
| 402 |
if (OSSL_PARAM_set_size_t(p, CRC32_DIGEST_LENGTH) != 1) { |
| 403 |
ok = 0; |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
return ok; |
| 409 |
} |
| 410 |
|
| 411 |
static const OSSL_PARAM *crc32_gettable_params(void) { |
| 412 |
return crc32_params; |
| 413 |
} |
| 414 |
|
| 415 |
static const OSSL_DISPATCH crc32_md_functions[] = { |
| 416 |
{ OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void)) crc32_ctx_new }, |
| 417 |
{ OSSL_FUNC_DIGEST_FREECTX, (void (*)(void)) crc32_ctx_free }, |
| 418 |
{ OSSL_FUNC_DIGEST_INIT, (void (*)(void)) crc32_md_init }, |
| 419 |
{ OSSL_FUNC_DIGEST_UPDATE, (void (*)(void)) crc32_md_update }, |
| 420 |
{ OSSL_FUNC_DIGEST_FINAL, (void (*)(void)) crc32_md_final }, |
| 421 |
{ OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void)) crc32_get_params }, |
| 422 |
{ OSSL_FUNC_DIGEST_GETTABLE_PARAMS, (void (*)(void)) crc32_gettable_params }, |
| 423 |
|
| 424 |
{ 0, NULL } |
| 425 |
}; |
| 426 |
|
| 427 |
static const OSSL_ALGORITHM crc32_digests[] = { |
| 428 |
{ "crc32", NULL, crc32_md_functions }, |
| 429 |
|
| 430 |
{ NULL, NULL, NULL } |
| 431 |
}; |
| 432 |
|
| 433 |
static const OSSL_ALGORITHM *crc32_provider_operation(void *provctx, |
| 434 |
int operation_id, int *no_cache) { |
| 435 |
*no_cache = 0; |
| 436 |
|
| 437 |
if (operation_id == OSSL_OP_DIGEST) { |
| 438 |
return crc32_digests; |
| 439 |
} |
| 440 |
|
| 441 |
return NULL; |
| 442 |
} |
| 443 |
|
| 444 |
static const OSSL_DISPATCH crc32_provider_functions[] = { |
| 445 |
{ OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void)) crc32_provider_operation }, |
| 446 |
|
| 447 |
{ 0, NULL } |
| 448 |
}; |
| 449 |
|
| 450 |
static int crc32_provider_init(const OSSL_CORE_HANDLE *core, |
| 451 |
const OSSL_DISPATCH *in, const OSSL_DISPATCH **out, void **provctx) { |
| 452 |
*out = crc32_provider_functions; |
| 453 |
*provctx = (void *) core; |
| 454 |
|
| 455 |
return 1; |
| 456 |
} |
| 457 |
|
| 458 |
#else |
| 459 |
static int crc32_init(EVP_MD_CTX *ctx) { |
| 460 |
void *md_data; |
| 461 |
|
| 462 |
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \ |
| 463 |
(defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L) |
| 464 |
md_data = EVP_MD_CTX_md_data(ctx); |
| 465 |
#else |
| 466 |
md_data = ctx->md_data; |
| 467 |
#endif |
| 468 |
|
| 469 |
return CRC32_Init(md_data); |
| 470 |
} |
| 471 |
|
| 472 |
static int crc32_update(EVP_MD_CTX *ctx, const void *data, size_t datasz) { |
| 473 |
void *md_data; |
| 474 |
|
| 475 |
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \ |
| 476 |
(defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L) |
| 477 |
md_data = EVP_MD_CTX_md_data(ctx); |
| 478 |
#else |
| 479 |
md_data = ctx->md_data; |
| 480 |
#endif |
| 481 |
|
| 482 |
return CRC32_Update(md_data, data, datasz); |
| 483 |
} |
| 484 |
|
| 485 |
static int crc32_final(EVP_MD_CTX *ctx, unsigned char *md) { |
| 486 |
void *md_data; |
| 487 |
|
| 488 |
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \ |
| 489 |
(defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L) |
| 490 |
md_data = EVP_MD_CTX_md_data(ctx); |
| 491 |
#else |
| 492 |
md_data = ctx->md_data; |
| 493 |
#endif |
| 494 |
|
| 495 |
return CRC32_Final(md, md_data); |
| 496 |
} |
| 497 |
|
| 498 |
static int crc32_free(EVP_MD_CTX *ctx) { |
| 499 |
void *md_data; |
| 500 |
|
| 501 |
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \ |
| 502 |
(defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L) |
| 503 |
md_data = EVP_MD_CTX_md_data(ctx); |
| 504 |
#else |
| 505 |
md_data = ctx->md_data; |
| 506 |
#endif |
| 507 |
|
| 508 |
return CRC32_Free(md_data); |
| 509 |
} |
| 510 |
|
| 511 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
| 512 |
(defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L) |
| 513 |
static const EVP_MD crc32_md = { |
| 514 |
NID_undef, |
| 515 |
NID_undef, |
| 516 |
CRC32_DIGEST_LENGTH, |
| 517 |
0, |
| 518 |
crc32_init, |
| 519 |
crc32_update, |
| 520 |
crc32_final, |
| 521 |
NULL, |
| 522 |
crc32_free, |
| 523 |
EVP_PKEY_NULL_method, |
| 524 |
CRC32_BLOCK, |
| 525 |
sizeof(EVP_MD *) + sizeof(CRC32_CTX) |
| 526 |
}; |
| 527 |
#endif |
| 528 |
|
| 529 |
static const EVP_MD *EVP_crc32(void) { |
| 530 |
EVP_MD *md; |
| 531 |
|
| 532 |
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \ |
| 533 |
(defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L) |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
md = EVP_MD_meth_new(NID_undef, NID_undef); |
| 538 |
EVP_MD_meth_set_input_blocksize(md, CRC32_BLOCK); |
| 539 |
EVP_MD_meth_set_result_size(md, CRC32_DIGEST_LENGTH); |
| 540 |
EVP_MD_meth_set_app_datasize(md, sizeof(EVP_MD *) + sizeof(CRC32_CTX)); |
| 541 |
EVP_MD_meth_set_init(md, crc32_init); |
| 542 |
EVP_MD_meth_set_update(md, crc32_update); |
| 543 |
EVP_MD_meth_set_final(md, crc32_final); |
| 544 |
EVP_MD_meth_set_cleanup(md, crc32_free); |
| 545 |
EVP_MD_meth_set_flags(md, 0); |
| 546 |
#else |
| 547 |
md = &crc32_md; |
| 548 |
#endif |
| 549 |
|
| 550 |
return md; |
| 551 |
} |
| 552 |
#endif |
| 553 |
|
| 554 |
static const char *get_errors(void) { |
| 555 |
unsigned int count = 0; |
| 556 |
unsigned long error_code; |
| 557 |
BIO *bio = NULL; |
| 558 |
char *data = NULL; |
| 559 |
long datalen; |
| 560 |
const char *error_data = NULL, *str = "(unknown)"; |
| 561 |
int error_flags = 0; |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
error_code = ERR_get_error_line_data(NULL, NULL, &error_data, &error_flags); |
| 568 |
if (error_code) { |
| 569 |
bio = BIO_new(BIO_s_mem()); |
| 570 |
} |
| 571 |
|
| 572 |
while (error_code) { |
| 573 |
pr_signals_handle(); |
| 574 |
|
| 575 |
if (error_flags & ERR_TXT_STRING) { |
| 576 |
BIO_printf(bio, "\n (%u) %s [%s]", ++count, |
| 577 |
ERR_error_string(error_code, NULL), error_data); |
| 578 |
|
| 579 |
} else { |
| 580 |
BIO_printf(bio, "\n (%u) %s", ++count, |
| 581 |
ERR_error_string(error_code, NULL)); |
| 582 |
} |
| 583 |
|
| 584 |
error_data = NULL; |
| 585 |
error_flags = 0; |
| 586 |
error_code = ERR_get_error_line_data(NULL, NULL, &error_data, &error_flags); |
| 587 |
} |
| 588 |
|
| 589 |
datalen = BIO_get_mem_data(bio, &data); |
| 590 |
if (data) { |
| 591 |
data[datalen] = '\0'; |
| 592 |
str = pstrdup(session.pool, data); |
| 593 |
} |
| 594 |
if (bio != NULL) { |
| 595 |
BIO_free(bio); |
| 596 |
} |
| 597 |
|
| 598 |
return str; |
| 599 |
} |
| 600 |
|
| 601 |
static void digest_hash_feat_add(pool *p) { |
| 602 |
char *feat_str = ""; |
| 603 |
int flags; |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
flags = DIGEST_ALGO_FL_IANA_STYLE; |
| 609 |
|
| 610 |
if (digest_algos & DIGEST_ALGO_CRC32) { |
| 611 |
int current_hash; |
| 612 |
|
| 613 |
current_hash = (digest_hash_algo == DIGEST_ALGO_CRC32); |
| 614 |
feat_str = pstrcat(p, *feat_str ? feat_str : "", |
| 615 |
get_algo_name(DIGEST_ALGO_CRC32, flags), current_hash ? "*" : "", ";", |
| 616 |
NULL); |
| 617 |
} |
| 618 |
|
| 619 |
if (digest_algos & DIGEST_ALGO_MD5) { |
| 620 |
int current_hash; |
| 621 |
|
| 622 |
current_hash = (digest_hash_algo == DIGEST_ALGO_MD5); |
| 623 |
feat_str = pstrcat(p, *feat_str ? feat_str : "", |
| 624 |
get_algo_name(DIGEST_ALGO_MD5, flags), current_hash ? "*" : "", ";", |
| 625 |
NULL); |
| 626 |
} |
| 627 |
|
| 628 |
if (digest_algos & DIGEST_ALGO_SHA1) { |
| 629 |
int current_hash; |
| 630 |
|
| 631 |
current_hash = (digest_hash_algo == DIGEST_ALGO_SHA1); |
| 632 |
feat_str = pstrcat(p, *feat_str ? feat_str : "", |
| 633 |
get_algo_name(DIGEST_ALGO_SHA1, flags), current_hash ? "*" : "", ";", |
| 634 |
NULL); |
| 635 |
} |
| 636 |
|
| 637 |
if (digest_algos & DIGEST_ALGO_SHA256) { |
| 638 |
int current_hash; |
| 639 |
|
| 640 |
current_hash = (digest_hash_algo == DIGEST_ALGO_SHA256); |
| 641 |
feat_str = pstrcat(p, *feat_str ? feat_str : "", |
| 642 |
get_algo_name(DIGEST_ALGO_SHA256, flags), current_hash ? "*" : "", ";", |
| 643 |
NULL); |
| 644 |
} |
| 645 |
|
| 646 |
if (digest_algos & DIGEST_ALGO_SHA512) { |
| 647 |
int current_hash; |
| 648 |
|
| 649 |
current_hash = (digest_hash_algo == DIGEST_ALGO_SHA512); |
| 650 |
feat_str = pstrcat(p, *feat_str ? feat_str : "", |
| 651 |
get_algo_name(DIGEST_ALGO_SHA512, flags), current_hash ? "*" : "", ";", |
| 652 |
NULL); |
| 653 |
} |
| 654 |
|
| 655 |
feat_str = pstrcat(p, "HASH ", feat_str, NULL); |
| 656 |
pr_feat_add(feat_str); |
| 657 |
} |
| 658 |
|
| 659 |
static void digest_hash_feat_remove(void) { |
| 660 |
const char *feat, *hash_feat = NULL; |
| 661 |
|
| 662 |
feat = pr_feat_get(); |
| 663 |
while (feat != NULL) { |
| 664 |
pr_signals_handle(); |
| 665 |
|
| 666 |
if (strncmp(feat, C_HASH, 4) == 0) { |
| 667 |
hash_feat = feat; |
| 668 |
break; |
| 669 |
} |
| 670 |
|
| 671 |
feat = pr_feat_get_next(); |
| 672 |
} |
| 673 |
|
| 674 |
if (hash_feat != NULL) { |
| 675 |
pr_feat_remove(hash_feat); |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
static void digest_x_feat_add(pool *p) { |
| 680 |
if (digest_algos & DIGEST_ALGO_CRC32) { |
| 681 |
pr_feat_add(C_XCRC); |
| 682 |
} |
| 683 |
|
| 684 |
if (digest_algos & DIGEST_ALGO_MD5) { |
| 685 |
pr_feat_add(C_MD5); |
| 686 |
pr_feat_add(C_XMD5); |
| 687 |
} |
| 688 |
|
| 689 |
if (digest_algos & DIGEST_ALGO_SHA1) { |
| 690 |
pr_feat_add(C_XSHA); |
| 691 |
pr_feat_add(C_XSHA1); |
| 692 |
} |
| 693 |
|
| 694 |
if (digest_algos & DIGEST_ALGO_SHA256) { |
| 695 |
pr_feat_add(C_XSHA256); |
| 696 |
} |
| 697 |
|
| 698 |
if (digest_algos & DIGEST_ALGO_SHA512) { |
| 699 |
pr_feat_add(C_XSHA512); |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
static void digest_x_help_add(pool *p) { |
| 704 |
if (digest_algos & DIGEST_ALGO_CRC32) { |
| 705 |
pr_help_add(C_XCRC, _("<sp> pathname [<sp> start <sp> end]"), TRUE); |
| 706 |
} |
| 707 |
|
| 708 |
if (digest_algos & DIGEST_ALGO_MD5) { |
| 709 |
pr_help_add(C_MD5, _("<sp> pathname"), TRUE); |
| 710 |
pr_help_add(C_XMD5, _("<sp> pathname [<sp> start <sp> end]"), TRUE); |
| 711 |
} |
| 712 |
|
| 713 |
if (digest_algos & DIGEST_ALGO_SHA1) { |
| 714 |
pr_help_add(C_XSHA, _("<sp> pathname [<sp> start <sp> end]"), TRUE); |
| 715 |
pr_help_add(C_XSHA1, _("<sp> pathname [<sp> start <sp> end]"), TRUE); |
| 716 |
} |
| 717 |
|
| 718 |
if (digest_algos & DIGEST_ALGO_SHA256) { |
| 719 |
pr_help_add(C_XSHA256, _("<sp> pathname [<sp> start <sp> end]"), TRUE); |
| 720 |
} |
| 721 |
|
| 722 |
if (digest_algos & DIGEST_ALGO_SHA512) { |
| 723 |
pr_help_add(C_XSHA512, _("<sp> pathname [<sp> start <sp> end]"), TRUE); |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
MODRET set_digestalgorithms(cmd_rec *cmd) { |
| 732 |
config_rec *c; |
| 733 |
unsigned long algos = 0UL; |
| 734 |
|
| 735 |
CHECK_CONF(cmd, CONF_ROOT|CONF_GLOBAL|CONF_VIRTUAL|CONF_ANON); |
| 736 |
|
| 737 |
|
| 738 |
if (cmd->argc < 2) { |
| 739 |
CONF_ERROR(cmd, "wrong number of parameters"); |
| 740 |
} |
| 741 |
|
| 742 |
if (strcasecmp(cmd->argv[1], "all") == 0) { |
| 743 |
algos = DIGEST_DEFAULT_ALGOS; |
| 744 |
|
| 745 |
} else { |
| 746 |
register unsigned int i; |
| 747 |
|
| 748 |
for (i = 1; i < cmd->argc; i++) { |
| 749 |
if (strcasecmp(cmd->argv[i], "crc32") == 0) { |
| 750 |
algos |= DIGEST_ALGO_CRC32; |
| 751 |
|
| 752 |
} else if (strcasecmp(cmd->argv[i], "md5") == 0) { |
| 753 |
#if !defined(OPENSSL_NO_MD5) |
| 754 |
algos |= DIGEST_ALGO_MD5; |
| 755 |
#else |
| 756 |
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "installed OpenSSL does not support the '", cmd->argv[i], "' DigestAlgorithm", NULL)); |
| 757 |
#endif |
| 758 |
|
| 759 |
} else if (strcasecmp(cmd->argv[i], "sha1") == 0) { |
| 760 |
#if !defined(OPENSSL_NO_SHA) |
| 761 |
algos |= DIGEST_ALGO_SHA1; |
| 762 |
#else |
| 763 |
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "installed OpenSSL does not support the '", cmd->argv[i], "' DigestAlgorithm", NULL)); |
| 764 |
#endif |
| 765 |
|
| 766 |
} else if (strcasecmp(cmd->argv[i], "sha256") == 0) { |
| 767 |
#if !defined(OPENSSL_NO_SHA256) |
| 768 |
algos |= DIGEST_ALGO_SHA256; |
| 769 |
#else |
| 770 |
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "installed OpenSSL does not support the '", cmd->argv[i], "' DigestAlgorithm", NULL)); |
| 771 |
#endif |
| 772 |
|
| 773 |
} else if (strcasecmp(cmd->argv[i], "sha512") == 0) { |
| 774 |
#if !defined(OPENSSL_NO_SHA512) |
| 775 |
algos |= DIGEST_ALGO_SHA512; |
| 776 |
#else |
| 777 |
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "installed OpenSSL does not support the '", cmd->argv[i], "' DigestAlgorithm", NULL)); |
| 778 |
#endif |
| 779 |
|
| 780 |
} else { |
| 781 |
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, |
| 782 |
"unknown/unsupported DigestAlgorithm: ", cmd->argv[i], NULL)); |
| 783 |
} |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
c = add_config_param(cmd->argv[0], 1, NULL); |
| 788 |
c->argv[0] = palloc(c->pool, sizeof(unsigned long)); |
| 789 |
*((unsigned long *) c->argv[0]) = algos; |
| 790 |
c->flags |= CF_MERGEDOWN; |
| 791 |
|
| 792 |
return PR_HANDLED(cmd); |
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
MODRET set_digestcache(cmd_rec *cmd) { |
| 797 |
register unsigned int i; |
| 798 |
config_rec *c; |
| 799 |
|
| 800 |
if (cmd->argc < 2 || |
| 801 |
cmd->argc > 5) { |
| 802 |
CONF_ERROR(cmd, "wrong number of parameters"); |
| 803 |
} |
| 804 |
|
| 805 |
CHECK_CONF(cmd, CONF_ROOT|CONF_GLOBAL|CONF_VIRTUAL|CONF_ANON); |
| 806 |
|
| 807 |
if (cmd->argc == 2) { |
| 808 |
int caching = -1; |
| 809 |
|
| 810 |
caching = get_boolean(cmd, 1); |
| 811 |
if (caching == -1) { |
| 812 |
CONF_ERROR(cmd, "expected Boolean parameter"); |
| 813 |
} |
| 814 |
|
| 815 |
c = add_config_param(cmd->argv[0], 3, NULL, NULL, NULL); |
| 816 |
c->argv[0] = palloc(c->pool, sizeof(int)); |
| 817 |
*((int *) c->argv[0]) = caching; |
| 818 |
c->argv[1] = palloc(c->pool, sizeof(unsigned int)); |
| 819 |
*((unsigned int *) c->argv[1]) = DIGEST_CACHE_DEFAULT_SIZE; |
| 820 |
c->argv[2] = palloc(c->pool, sizeof(unsigned int)); |
| 821 |
*((unsigned int *) c->argv[2]) = DIGEST_CACHE_DEFAULT_MAX_AGE; |
| 822 |
c->flags |= CF_MERGEDOWN; |
| 823 |
|
| 824 |
return PR_HANDLED(cmd); |
| 825 |
} |
| 826 |
|
| 827 |
c = add_config_param(cmd->argv[0], 3, NULL, NULL); |
| 828 |
c->argv[0] = palloc(c->pool, sizeof(int)); |
| 829 |
*((int *) c->argv[0]) = TRUE; |
| 830 |
c->argv[1] = palloc(c->pool, sizeof(unsigned int)); |
| 831 |
*((unsigned int *) c->argv[1]) = DIGEST_CACHE_DEFAULT_SIZE; |
| 832 |
c->argv[2] = palloc(c->pool, sizeof(unsigned int)); |
| 833 |
*((unsigned int *) c->argv[2]) = DIGEST_CACHE_DEFAULT_MAX_AGE; |
| 834 |
c->flags |= CF_MERGEDOWN; |
| 835 |
|
| 836 |
for (i = 1; i < cmd->argc; i++) { |
| 837 |
if (strncasecmp(cmd->argv[i], "size", 5) == 0) { |
| 838 |
long size; |
| 839 |
char *ptr = NULL; |
| 840 |
|
| 841 |
if (i+1 == cmd->argc) { |
| 842 |
CONF_ERROR(cmd, "wrong number of parameters"); |
| 843 |
} |
| 844 |
|
| 845 |
size = strtol(cmd->argv[i+1], &ptr, 10); |
| 846 |
if (ptr && *ptr) { |
| 847 |
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "invalid cache size: ", |
| 848 |
cmd->argv[i+1], NULL)); |
| 849 |
} |
| 850 |
|
| 851 |
if (size < 1) { |
| 852 |
CONF_ERROR(cmd, "cache size must be greater than 0"); |
| 853 |
} |
| 854 |
|
| 855 |
*((unsigned int *) c->argv[1]) = (unsigned int) size; |
| 856 |
i++; |
| 857 |
|
| 858 |
} else if (strncasecmp(cmd->argv[i], "maxAge", 7) == 0) { |
| 859 |
int max_age; |
| 860 |
|
| 861 |
if (i+1 == cmd->argc) { |
| 862 |
CONF_ERROR(cmd, "wrong number of parameters"); |
| 863 |
} |
| 864 |
|
| 865 |
if (pr_str_get_duration(cmd->argv[i+1], &max_age) < 0) { |
| 866 |
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "invalid max age: ", |
| 867 |
cmd->argv[i+1], NULL)); |
| 868 |
} |
| 869 |
|
| 870 |
if (max_age < 1) { |
| 871 |
CONF_ERROR(cmd, "maxAge parameter must be greater than 1"); |
| 872 |
} |
| 873 |
|
| 874 |
*((unsigned int *) c->argv[2]) = max_age; |
| 875 |
i++; |
| 876 |
|
| 877 |
} else { |
| 878 |
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unknown DigestCache parameter: ", |
| 879 |
cmd->argv[i], NULL)); |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
return PR_HANDLED(cmd); |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
MODRET set_digestdefaultalgo(cmd_rec *cmd) { |
| 888 |
config_rec *c; |
| 889 |
const char *algo_name; |
| 890 |
unsigned long algo = 0UL; |
| 891 |
|
| 892 |
CHECK_CONF(cmd, CONF_ROOT|CONF_GLOBAL|CONF_VIRTUAL); |
| 893 |
CHECK_ARGS(cmd, 1); |
| 894 |
|
| 895 |
algo_name = cmd->argv[1]; |
| 896 |
|
| 897 |
if (strcasecmp(algo_name, "crc32") == 0) { |
| 898 |
algo = DIGEST_ALGO_CRC32; |
| 899 |
|
| 900 |
} else if (strcasecmp(algo_name, "md5") == 0) { |
| 901 |
#ifndef OPENSSL_NO_MD5 |
| 902 |
algo = DIGEST_ALGO_MD5; |
| 903 |
#else |
| 904 |
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "installed OpenSSL does not support the '", algo_name, "' DigestAlgorithm", NULL)); |
| 905 |
#endif |
| 906 |
|
| 907 |
} else if (strcasecmp(algo_name, "sha1") == 0) { |
| 908 |
#ifndef OPENSSL_NO_SHA |
| 909 |
algo = DIGEST_ALGO_SHA1; |
| 910 |
#else |
| 911 |
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "installed OpenSSL does not support the '", algo_name, "' DigestAlgorithm", NULL)); |
| 912 |
#endif |
| 913 |
|
| 914 |
} else if (strcasecmp(algo_name, "sha256") == 0) { |
| 915 |
#ifndef OPENSSL_NO_SHA256 |
| 916 |
algo = DIGEST_ALGO_SHA256; |
| 917 |
#else |
| 918 |
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "installed OpenSSL does not support the '", algo_name, "' DigestAlgorithm", NULL)); |
| 919 |
#endif |
| 920 |
|
| 921 |
} else if (strcasecmp(algo_name, "sha512") == 0) { |
| 922 |
#ifndef OPENSSL_NO_SHA512 |
| 923 |
algo = DIGEST_ALGO_SHA512; |
| 924 |
#else |
| 925 |
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "installed OpenSSL does not support the '", algo_name, "' DigestAlgorithm", NULL)); |
| 926 |
#endif |
| 927 |
|
| 928 |
} else { |
| 929 |
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, |
| 930 |
"unknown/unsupported DigestAlgorithm: ", algo_name, NULL)); |
| 931 |
} |
| 932 |
|
| 933 |
c = add_config_param(cmd->argv[0], 1, NULL); |
| 934 |
c->argv[0] = palloc(c->pool, sizeof(unsigned long)); |
| 935 |
*((unsigned long *) c->argv[0]) = algo; |
| 936 |
|
| 937 |
return PR_HANDLED(cmd); |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
MODRET set_digestenable(cmd_rec *cmd) { |
| 942 |
int enable = -1; |
| 943 |
config_rec *c; |
| 944 |
|
| 945 |
CHECK_ARGS(cmd, 1); |
| 946 |
CHECK_CONF(cmd, CONF_DIR|CONF_DYNDIR); |
| 947 |
|
| 948 |
enable = get_boolean(cmd, 1); |
| 949 |
if (enable == -1) { |
| 950 |
CONF_ERROR(cmd, "expected Boolean parameter"); |
| 951 |
} |
| 952 |
|
| 953 |
c = add_config_param(cmd->argv[0], 1, NULL); |
| 954 |
c->argv[0] = palloc(c->pool, sizeof(int)); |
| 955 |
*((int *) c->argv[0]) = enable; |
| 956 |
|
| 957 |
return PR_HANDLED(cmd); |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
MODRET set_digestengine(cmd_rec *cmd) { |
| 962 |
int engine = -1; |
| 963 |
config_rec *c; |
| 964 |
|
| 965 |
CHECK_ARGS(cmd, 1); |
| 966 |
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON); |
| 967 |
|
| 968 |
engine = get_boolean(cmd, 1); |
| 969 |
if (engine == -1) { |
| 970 |
CONF_ERROR(cmd, "expected Boolean parameter"); |
| 971 |
} |
| 972 |
|
| 973 |
c = add_config_param(cmd->argv[0], 1, NULL); |
| 974 |
c->argv[0] = palloc(c->pool, sizeof(int)); |
| 975 |
*((int *) c->argv[0]) = engine; |
| 976 |
|
| 977 |
c->flags |= CF_MERGEDOWN; |
| 978 |
return PR_HANDLED(cmd); |
| 979 |
} |
| 980 |
|
| 981 |
|
| 982 |
MODRET set_digestmaxsize(cmd_rec *cmd) { |
| 983 |
config_rec *c = NULL; |
| 984 |
const char *num, *units = ""; |
| 985 |
off_t max_size; |
| 986 |
|
| 987 |
if (cmd->argc < 2 || |
| 988 |
cmd->argc > 3) { |
| 989 |
CONF_ERROR(cmd, "wrong number of parameters"); |
| 990 |
} |
| 991 |
|
| 992 |
CHECK_CONF(cmd, CONF_ROOT|CONF_GLOBAL|CONF_VIRTUAL|CONF_ANON); |
| 993 |
|
| 994 |
|
| 995 |
if (cmd->argc == 2 && |
| 996 |
get_boolean(cmd, 1) == FALSE) { |
| 997 |
c = add_config_param(cmd->argv[0], 1, NULL); |
| 998 |
c->argv[0] = pcalloc(c->pool, sizeof(off_t)); |
| 999 |
c->flags |= CF_MERGEDOWN; |
| 1000 |
return PR_HANDLED(cmd); |
| 1001 |
} |
| 1002 |
|
| 1003 |
num = cmd->argv[1]; |
| 1004 |
if (cmd->argc == 3) { |
| 1005 |
units = cmd->argv[2]; |
| 1006 |
} |
| 1007 |
|
| 1008 |
if (pr_str_get_nbytes(num, units, &max_size) < 0) { |
| 1009 |
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "badly formatted size value: ", |
| 1010 |
num, units, NULL)); |
| 1011 |
} |
| 1012 |
|
| 1013 |
if (max_size == 0) { |
| 1014 |
CONF_ERROR(cmd, "requires a value greater than zero"); |
| 1015 |
} |
| 1016 |
|
| 1017 |
c = add_config_param(cmd->argv[0], 1, NULL); |
| 1018 |
c->argv[0] = pcalloc(c->pool, sizeof(off_t)); |
| 1019 |
*((off_t *) c->argv[0]) = max_size; |
| 1020 |
c->flags |= CF_MERGEDOWN; |
| 1021 |
|
| 1022 |
return PR_HANDLED(cmd); |
| 1023 |
} |
| 1024 |
|
| 1025 |
|
| 1026 |
MODRET set_digestoptions(cmd_rec *cmd) { |
| 1027 |
config_rec *c = NULL; |
| 1028 |
register unsigned int i = 0; |
| 1029 |
unsigned long opts = 0UL; |
| 1030 |
|
| 1031 |
if (cmd->argc-1 == 0) { |
| 1032 |
CONF_ERROR(cmd, "wrong number of parameters"); |
| 1033 |
} |
| 1034 |
|
| 1035 |
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); |
| 1036 |
|
| 1037 |
c = add_config_param(cmd->argv[0], 1, NULL); |
| 1038 |
|
| 1039 |
for (i = 1; i < cmd->argc; i++) { |
| 1040 |
if (strcmp(cmd->argv[i], "NoTransferCache") == 0) { |
| 1041 |
opts |= DIGEST_OPT_NO_TRANSFER_CACHE; |
| 1042 |
|
| 1043 |
} else { |
| 1044 |
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown DigestOption '", |
| 1045 |
cmd->argv[i], "'", NULL)); |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
c->argv[0] = pcalloc(c->pool, sizeof(unsigned long)); |
| 1050 |
*((unsigned long *) c->argv[0]) = opts; |
| 1051 |
|
| 1052 |
if (pr_module_exists("mod_ifsession.c")) { |
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
c->flags |= CF_MULTI; |
| 1057 |
} |
| 1058 |
|
| 1059 |
return PR_HANDLED(cmd); |
| 1060 |
} |
| 1061 |
|
| 1062 |
static int check_digest_max_size(off_t len) { |
| 1063 |
config_rec *c; |
| 1064 |
off_t max_size; |
| 1065 |
|
| 1066 |
c = find_config(CURRENT_CONF, CONF_PARAM, "DigestMaxSize", FALSE); |
| 1067 |
if (c == NULL) { |
| 1068 |
return 0; |
| 1069 |
} |
| 1070 |
|
| 1071 |
max_size = *((off_t *) c->argv[0]); |
| 1072 |
if (max_size == 0) { |
| 1073 |
|
| 1074 |
return 0; |
| 1075 |
} |
| 1076 |
|
| 1077 |
if (len > max_size) { |
| 1078 |
pr_log_debug(DEBUG5, MOD_DIGEST_VERSION |
| 1079 |
": %s requested len (%" PR_LU ") exceeds DigestMaxSize %" PR_LU |
| 1080 |
", rejecting", session.curr_cmd, (pr_off_t) len, (pr_off_t) max_size); |
| 1081 |
errno = EPERM; |
| 1082 |
return -1; |
| 1083 |
} |
| 1084 |
|
| 1085 |
return 0; |
| 1086 |
} |
| 1087 |
|
| 1088 |
static int can_digest_file(pool *p, const char *path, off_t start, size_t len, |
| 1089 |
struct stat *st) { |
| 1090 |
config_rec *d; |
| 1091 |
char *dir_path, *ptr; |
| 1092 |
|
| 1093 |
if (!S_ISREG(st->st_mode)) { |
| 1094 |
pr_trace_msg(trace_channel, 2, "path '%s' is not a regular file", path); |
| 1095 |
errno = EISDIR; |
| 1096 |
return -1; |
| 1097 |
} |
| 1098 |
|
| 1099 |
if (start > 0) { |
| 1100 |
if (start > st->st_size) { |
| 1101 |
pr_log_debug(DEBUG3, MOD_DIGEST_VERSION |
| 1102 |
": requested offset (%" PR_LU " bytes) for path '%s' exceeds file size " |
| 1103 |
"(%lu bytes)", (pr_off_t) start, path, (unsigned long) st->st_size); |
| 1104 |
errno = EINVAL; |
| 1105 |
return -1; |
| 1106 |
} |
| 1107 |
} |
| 1108 |
|
| 1109 |
if (len > 0) { |
| 1110 |
if (((off_t) (start + len)) > st->st_size) { |
| 1111 |
pr_log_debug(DEBUG3, MOD_DIGEST_VERSION |
| 1112 |
": requested offset/length (offset %" PR_LU " bytes, length %lu bytes) " |
| 1113 |
"for path '%s' exceeds file size (%lu bytes)", (pr_off_t) start, |
| 1114 |
(unsigned long) len, path, (unsigned long) st->st_size); |
| 1115 |
errno = EINVAL; |
| 1116 |
return -1; |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
ptr = strrchr(path, '/'); |
| 1125 |
if (ptr == NULL || |
| 1126 |
ptr == path) { |
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
return 0; |
| 1135 |
} |
| 1136 |
|
| 1137 |
dir_path = pstrndup(p, path, (ptr - path)); |
| 1138 |
|
| 1139 |
pr_trace_msg(trace_channel, 1, "checking for DigestEnable in '%s'", dir_path); |
| 1140 |
d = dir_match_path(p, dir_path); |
| 1141 |
if (d != NULL) { |
| 1142 |
config_rec *c; |
| 1143 |
|
| 1144 |
c = find_config(d->subset, CONF_PARAM, "DigestEnable", FALSE); |
| 1145 |
if (c != NULL) { |
| 1146 |
int digest_enable; |
| 1147 |
|
| 1148 |
digest_enable = *((int *) c->argv[0]); |
| 1149 |
if (digest_enable == FALSE) { |
| 1150 |
pr_log_debug(DEBUG8, MOD_DIGEST_VERSION |
| 1151 |
": digest of '%s' denied by DigestEnable configuration", path); |
| 1152 |
errno = EPERM; |
| 1153 |
return -1; |
| 1154 |
} |
| 1155 |
} |
| 1156 |
} |
| 1157 |
|
| 1158 |
return 0; |
| 1159 |
} |
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
static int blacklisted_file(const char *path) { |
| 1166 |
int res = FALSE; |
| 1167 |
|
| 1168 |
if (strcasecmp("/dev/full", path) == 0 || |
| 1169 |
strcasecmp("/dev/null", path) == 0 || |
| 1170 |
strcasecmp("/dev/random", path) == 0 || |
| 1171 |
strcasecmp("/dev/urandom", path) == 0 || |
| 1172 |
strcasecmp("/dev/zero", path) == 0) { |
| 1173 |
res = TRUE; |
| 1174 |
} |
| 1175 |
|
| 1176 |
return res; |
| 1177 |
} |
| 1178 |
|
| 1179 |
static int compute_digest(pool *p, const char *path, off_t start, off_t len, |
| 1180 |
const EVP_MD *md, unsigned char *digest, unsigned int *digest_len, |
| 1181 |
time_t *mtime) { |
| 1182 |
int res, xerrno = 0; |
| 1183 |
pr_fh_t *fh; |
| 1184 |
struct stat st; |
| 1185 |
unsigned char *buf; |
| 1186 |
size_t bufsz, readsz; |
| 1187 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
| 1188 |
(defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L) |
| 1189 |
EVP_MD_CTX ctx; |
| 1190 |
#endif |
| 1191 |
EVP_MD_CTX *pctx; |
| 1192 |
|
| 1193 |
fh = pr_fsio_open(path, O_RDONLY); |
| 1194 |
if (fh == NULL) { |
| 1195 |
xerrno = errno; |
| 1196 |
|
| 1197 |
pr_trace_msg(trace_channel, 1, "unable to read '%s': %s", path, |
| 1198 |
strerror(xerrno)); |
| 1199 |
|
| 1200 |
errno = xerrno; |
| 1201 |
return -1; |
| 1202 |
} |
| 1203 |
|
| 1204 |
res = pr_fsio_fstat(fh, &st); |
| 1205 |
if (res < 0) { |
| 1206 |
xerrno = errno; |
| 1207 |
|
| 1208 |
pr_trace_msg(trace_channel, 1, "unable to stat '%s': %s", path, |
| 1209 |
strerror(xerrno)); |
| 1210 |
(void) pr_fsio_close(fh); |
| 1211 |
|
| 1212 |
errno = xerrno; |
| 1213 |
return -1; |
| 1214 |
} |
| 1215 |
|
| 1216 |
res = can_digest_file(p, path, start, len, &st); |
| 1217 |
if (res < 0) { |
| 1218 |
xerrno = errno; |
| 1219 |
(void) pr_fsio_close(fh); |
| 1220 |
errno = xerrno; |
| 1221 |
return -1; |
| 1222 |
} |
| 1223 |
|
| 1224 |
if (mtime != NULL) { |
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
*mtime = st.st_mtime; |
| 1229 |
} |
| 1230 |
|
| 1231 |
|
| 1232 |
fh->fh_iosz = bufsz = st.st_blksize; |
| 1233 |
|
| 1234 |
if (pr_fsio_lseek(fh, start, SEEK_SET) == (off_t) -1) { |
| 1235 |
xerrno = errno; |
| 1236 |
|
| 1237 |
pr_trace_msg(trace_channel, 1, "error seeking to offset %" PR_LU |
| 1238 |
" in '%s': %s", (pr_off_t) start, path, strerror(xerrno)); |
| 1239 |
|
| 1240 |
(void) pr_fsio_close(fh); |
| 1241 |
errno = xerrno; |
| 1242 |
return -1; |
| 1243 |
} |
| 1244 |
|
| 1245 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
| 1246 |
(defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L) |
| 1247 |
pctx = &ctx; |
| 1248 |
#else |
| 1249 |
pctx = EVP_MD_CTX_new(); |
| 1250 |
#endif |
| 1251 |
|
| 1252 |
EVP_MD_CTX_init(pctx); |
| 1253 |
if (EVP_DigestInit_ex(pctx, md, NULL) != 1) { |
| 1254 |
pr_log_debug(DEBUG1, MOD_DIGEST_VERSION |
| 1255 |
": error preparing digest context: %s", get_errors()); |
| 1256 |
(void) pr_fsio_close(fh); |
| 1257 |
# if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \ |
| 1258 |
(defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L) |
| 1259 |
EVP_MD_CTX_free(pctx); |
| 1260 |
# endif |
| 1261 |
errno = EPERM; |
| 1262 |
return -1; |
| 1263 |
} |
| 1264 |
|
| 1265 |
buf = palloc(p, bufsz); |
| 1266 |
|
| 1267 |
readsz = bufsz; |
| 1268 |
if ((off_t) readsz > len) { |
| 1269 |
readsz = len; |
| 1270 |
} |
| 1271 |
|
| 1272 |
res = pr_fsio_read(fh, (char *) buf, readsz); |
| 1273 |
xerrno = errno; |
| 1274 |
|
| 1275 |
while (len > 0) { |
| 1276 |
if (res < 0 && |
| 1277 |
errno == EAGAIN) { |
| 1278 |
|
| 1279 |
errno = xerrno = EINTR; |
| 1280 |
} |
| 1281 |
|
| 1282 |
pr_signals_handle(); |
| 1283 |
|
| 1284 |
if (res < 0 && |
| 1285 |
xerrno == EINTR) { |
| 1286 |
|
| 1287 |
res = pr_fsio_read(fh, (char *) buf, readsz); |
| 1288 |
continue; |
| 1289 |
} |
| 1290 |
|
| 1291 |
if (EVP_DigestUpdate(pctx, buf, res) != 1) { |
| 1292 |
pr_log_debug(DEBUG1, MOD_DIGEST_VERSION |
| 1293 |
": error updating digest: %s", get_errors()); |
| 1294 |
} |
| 1295 |
|
| 1296 |
len -= res; |
| 1297 |
|
| 1298 |
readsz = bufsz; |
| 1299 |
if ((off_t) readsz > len) { |
| 1300 |
readsz = len; |
| 1301 |
} |
| 1302 |
|
| 1303 |
res = pr_fsio_read(fh, (char *) buf, readsz); |
| 1304 |
xerrno = errno; |
| 1305 |
} |
| 1306 |
|
| 1307 |
(void) pr_fsio_close(fh); |
| 1308 |
|
| 1309 |
if (len != 0) { |
| 1310 |
# if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \ |
| 1311 |
(defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L) |
| 1312 |
EVP_MD_CTX_free(pctx); |
| 1313 |
# endif |
| 1314 |
pr_log_debug(DEBUG3, MOD_DIGEST_VERSION |
| 1315 |
": failed to read all %" PR_LU " bytes of '%s' (premature EOF?)", |
| 1316 |
(pr_off_t) len, path); |
| 1317 |
errno = EIO; |
| 1318 |
return -1; |
| 1319 |
} |
| 1320 |
|
| 1321 |
if (EVP_DigestFinal_ex(pctx, digest, digest_len) != 1) { |
| 1322 |
pr_log_debug(DEBUG1, MOD_DIGEST_VERSION |
| 1323 |
": error finishing digest: %s", get_errors()); |
| 1324 |
# if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \ |
| 1325 |
(defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L) |
| 1326 |
EVP_MD_CTX_free(pctx); |
| 1327 |
# endif |
| 1328 |
errno = EPERM; |
| 1329 |
return -1; |
| 1330 |
} |
| 1331 |
|
| 1332 |
# if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \ |
| 1333 |
(defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L) |
| 1334 |
EVP_MD_CTX_free(pctx); |
| 1335 |
# endif |
| 1336 |
|
| 1337 |
return 0; |
| 1338 |
} |
| 1339 |
|
| 1340 |
static void free_algo_md(const EVP_MD *md) { |
| 1341 |
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined(HAVE_LIBRESSL)) || \ |
| 1342 |
(defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3080000L) |
| 1343 |
EVP_MD_free((EVP_MD *) md); |
| 1344 |
#else |
| 1345 |
|
| 1346 |
(void) md; |
| 1347 |
#endif |
| 1348 |
} |
| 1349 |
|
| 1350 |
static const EVP_MD *get_algo_md(unsigned long algo, int *free_md) { |
| 1351 |
const EVP_MD *md = NULL; |
| 1352 |
|
| 1353 |
*free_md = FALSE; |
| 1354 |
|
| 1355 |
switch (algo) { |
| 1356 |
case DIGEST_ALGO_CRC32: |
| 1357 |
#if OPENSSL_VERSION_NUMBER >= 0x40000000L && !defined(HAVE_LIBRESSL) |
| 1358 |
md = EVP_MD_fetch(NULL, "crc32", NULL); |
| 1359 |
if (md == NULL) { |
| 1360 |
pr_trace_msg(trace_channel, 3, "error obtaining CRC32 EVP_MD: %s", |
| 1361 |
get_errors()); |
| 1362 |
|
| 1363 |
} else { |
| 1364 |
*free_md = TRUE; |
| 1365 |
} |
| 1366 |
#else |
| 1367 |
md = EVP_crc32(); |
| 1368 |
#endif |
| 1369 |
break; |
| 1370 |
|
| 1371 |
#if !defined(OPENSSL_NO_MD5) |
| 1372 |
case DIGEST_ALGO_MD5: |
| 1373 |
md = EVP_md5(); |
| 1374 |
break; |
| 1375 |
#endif |
| 1376 |
|
| 1377 |
#if !defined(OPENSSL_NO_SHA1) |
| 1378 |
case DIGEST_ALGO_SHA1: |
| 1379 |
md = EVP_sha1(); |
| 1380 |
break; |
| 1381 |
#endif |
| 1382 |
|
| 1383 |
#if !defined(OPENSSL_NO_SHA256) |
| 1384 |
case DIGEST_ALGO_SHA256: |
| 1385 |
md = EVP_sha256(); |
| 1386 |
break; |
| 1387 |
#endif |
| 1388 |
|
| 1389 |
#if !defined(OPENSSL_NO_SHA512) |
| 1390 |
case DIGEST_ALGO_SHA512: |
| 1391 |
md = EVP_sha512(); |
| 1392 |
break; |
| 1393 |
#endif |
| 1394 |
|
| 1395 |
default: |
| 1396 |
errno = ENOENT; |
| 1397 |
break; |
| 1398 |
} |
| 1399 |
|
| 1400 |
return md; |
| 1401 |
} |
| 1402 |
|
| 1403 |
static const char *get_algo_name(unsigned long algo, int flags) { |
| 1404 |
const char *algo_name = "(unknown)"; |
| 1405 |
|
| 1406 |
switch (algo) { |
| 1407 |
case DIGEST_ALGO_CRC32: |
| 1408 |
algo_name = "CRC32"; |
| 1409 |
break; |
| 1410 |
|
| 1411 |
case DIGEST_ALGO_MD5: |
| 1412 |
algo_name = "MD5"; |
| 1413 |
break; |
| 1414 |
|
| 1415 |
case DIGEST_ALGO_SHA1: |
| 1416 |
if (flags & DIGEST_ALGO_FL_IANA_STYLE) { |
| 1417 |
algo_name = "SHA-1"; |
| 1418 |
|
| 1419 |
} else { |
| 1420 |
algo_name = "SHA1"; |
| 1421 |
} |
| 1422 |
break; |
| 1423 |
|
| 1424 |
case DIGEST_ALGO_SHA256: |
| 1425 |
if (flags & DIGEST_ALGO_FL_IANA_STYLE) { |
| 1426 |
algo_name = "SHA-256"; |
| 1427 |
|
| 1428 |
} else { |
| 1429 |
algo_name = "SHA256"; |
| 1430 |
} |
| 1431 |
break; |
| 1432 |
|
| 1433 |
case DIGEST_ALGO_SHA512: |
| 1434 |
if (flags & DIGEST_ALGO_FL_IANA_STYLE) { |
| 1435 |
algo_name = "SHA-512"; |
| 1436 |
|
| 1437 |
} else { |
| 1438 |
algo_name = "SHA512"; |
| 1439 |
} |
| 1440 |
break; |
| 1441 |
|
| 1442 |
default: |
| 1443 |
errno = ENOENT; |
| 1444 |
break; |
| 1445 |
} |
| 1446 |
|
| 1447 |
return algo_name; |
| 1448 |
} |
| 1449 |
|
| 1450 |
static pr_table_t *get_cache(unsigned long algo) { |
| 1451 |
pr_table_t *cache = NULL; |
| 1452 |
|
| 1453 |
switch (algo) { |
| 1454 |
case DIGEST_ALGO_CRC32: |
| 1455 |
cache = digest_crc32_tab; |
| 1456 |
break; |
| 1457 |
|
| 1458 |
case DIGEST_ALGO_MD5: |
| 1459 |
cache = digest_md5_tab; |
| 1460 |
break; |
| 1461 |
|
| 1462 |
case DIGEST_ALGO_SHA1: |
| 1463 |
cache = digest_sha1_tab; |
| 1464 |
break; |
| 1465 |
|
| 1466 |
case DIGEST_ALGO_SHA256: |
| 1467 |
cache = digest_sha256_tab; |
| 1468 |
break; |
| 1469 |
|
| 1470 |
case DIGEST_ALGO_SHA512: |
| 1471 |
cache = digest_sha512_tab; |
| 1472 |
break; |
| 1473 |
|
| 1474 |
default: |
| 1475 |
pr_trace_msg(trace_channel, 4, |
| 1476 |
"unable to determine cache for %s digest", get_algo_name(algo, 0)); |
| 1477 |
errno = EINVAL; |
| 1478 |
return NULL; |
| 1479 |
} |
| 1480 |
|
| 1481 |
if (cache == NULL) { |
| 1482 |
errno = ENOENT; |
| 1483 |
} |
| 1484 |
|
| 1485 |
return cache; |
| 1486 |
} |
| 1487 |
|
| 1488 |
static unsigned int get_cache_size(void) { |
| 1489 |
int res; |
| 1490 |
unsigned int cache_size = 0; |
| 1491 |
|
| 1492 |
if (digest_caching == FALSE) { |
| 1493 |
return 0; |
| 1494 |
} |
| 1495 |
|
| 1496 |
res = pr_table_count(digest_crc32_tab); |
| 1497 |
if (res >= 0) { |
| 1498 |
cache_size += res; |
| 1499 |
} |
| 1500 |
|
| 1501 |
res = pr_table_count(digest_md5_tab); |
| 1502 |
if (res >= 0) { |
| 1503 |
cache_size += res; |
| 1504 |
} |
| 1505 |
|
| 1506 |
res = pr_table_count(digest_sha1_tab); |
| 1507 |
if (res >= 0) { |
| 1508 |
cache_size += res; |
| 1509 |
} |
| 1510 |
|
| 1511 |
res = pr_table_count(digest_sha256_tab); |
| 1512 |
if (res >= 0) { |
| 1513 |
cache_size += res; |
| 1514 |
} |
| 1515 |
|
| 1516 |
res = pr_table_count(digest_sha512_tab); |
| 1517 |
if (res >= 0) { |
| 1518 |
cache_size += res; |
| 1519 |
} |
| 1520 |
|
| 1521 |
return cache_size; |
| 1522 |
} |
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
static const char *get_key_for_cache(pool *p, const char *path, time_t mtime, |
| 1528 |
off_t start, size_t len) { |
| 1529 |
const char *key; |
| 1530 |
char mtime_str[256], start_str[256], len_str[256]; |
| 1531 |
|
| 1532 |
memset(mtime_str, '\0', sizeof(mtime_str)); |
| 1533 |
pr_snprintf(mtime_str, sizeof(mtime_str)-1, "%llu", |
| 1534 |
(unsigned long long) mtime); |
| 1535 |
|
| 1536 |
memset(start_str, '\0', sizeof(start_str)); |
| 1537 |
pr_snprintf(start_str, sizeof(start_str)-1, "%" PR_LU, (pr_off_t) start); |
| 1538 |
|
| 1539 |
memset(len_str, '\0', sizeof(len_str)); |
| 1540 |
pr_snprintf(len_str, sizeof(len_str)-1, "%llu", (unsigned long long) len); |
| 1541 |
|
| 1542 |
key = pstrcat(p, path, "@", mtime_str, ",", start_str, "+", len_str, NULL); |
| 1543 |
return key; |
| 1544 |
} |
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
static int cache_key_cmp(xasetmember_t *a, xasetmember_t *b) { |
| 1550 |
struct digest_cache_key *k1, *k2; |
| 1551 |
|
| 1552 |
k1 = (struct digest_cache_key *) a; |
| 1553 |
k2 = (struct digest_cache_key *) b; |
| 1554 |
|
| 1555 |
if (k1->mtime < k2->mtime) { |
| 1556 |
return -1; |
| 1557 |
} |
| 1558 |
|
| 1559 |
if (k1->mtime > k2->mtime) { |
| 1560 |
return 1; |
| 1561 |
} |
| 1562 |
|
| 1563 |
return 0; |
| 1564 |
} |
| 1565 |
|
| 1566 |
static struct digest_cache_key *create_cache_key(pool *p, unsigned long algo, |
| 1567 |
const char *path, time_t mtime, off_t start, size_t len, |
| 1568 |
const char *hex_digest) { |
| 1569 |
int res; |
| 1570 |
struct digest_cache_key *cache_key; |
| 1571 |
pool *sub_pool; |
| 1572 |
|
| 1573 |
sub_pool = make_sub_pool(digest_pool); |
| 1574 |
pr_pool_tag(sub_pool, "DigestCache entry"); |
| 1575 |
|
| 1576 |
cache_key = pcalloc(sub_pool, sizeof(struct digest_cache_key)); |
| 1577 |
cache_key->pool = sub_pool; |
| 1578 |
cache_key->path = pstrdup(cache_key->pool, path); |
| 1579 |
cache_key->mtime = mtime; |
| 1580 |
cache_key->start = start; |
| 1581 |
cache_key->len = len; |
| 1582 |
cache_key->algo = algo; |
| 1583 |
cache_key->key = get_key_for_cache(cache_key->pool, path, mtime, start, len); |
| 1584 |
cache_key->hex_digest = pstrdup(cache_key->pool, hex_digest); |
| 1585 |
|
| 1586 |
if (digest_cache_keys == NULL) { |
| 1587 |
digest_cache_keys = xaset_create(digest_pool, cache_key_cmp); |
| 1588 |
} |
| 1589 |
|
| 1590 |
res = xaset_insert_sort(digest_cache_keys, (xasetmember_t *) cache_key, TRUE); |
| 1591 |
if (res < 0) { |
| 1592 |
pr_trace_msg(trace_channel, 12, |
| 1593 |
"error adding cache key '%s' to set: %s", cache_key->key, |
| 1594 |
strerror(errno)); |
| 1595 |
} |
| 1596 |
|
| 1597 |
return cache_key; |
| 1598 |
} |
| 1599 |
|
| 1600 |
static struct digest_cache_key *find_cache_key(pool *p, unsigned long algo, |
| 1601 |
const char *path, time_t mtime, off_t start, off_t len) { |
| 1602 |
struct digest_cache_key *cache_key = NULL; |
| 1603 |
|
| 1604 |
for (cache_key = (struct digest_cache_key *) digest_cache_keys->xas_list; |
| 1605 |
cache_key != NULL; |
| 1606 |
cache_key = cache_key->next) { |
| 1607 |
if (cache_key->algo != algo) { |
| 1608 |
continue; |
| 1609 |
} |
| 1610 |
|
| 1611 |
if (cache_key->mtime != mtime) { |
| 1612 |
continue; |
| 1613 |
} |
| 1614 |
|
| 1615 |
if (cache_key->start != start) { |
| 1616 |
continue; |
| 1617 |
} |
| 1618 |
|
| 1619 |
if (cache_key->len != len) { |
| 1620 |
continue; |
| 1621 |
} |
| 1622 |
|
| 1623 |
if (strcmp(cache_key->path, path) == 0) { |
| 1624 |
return cache_key; |
| 1625 |
} |
| 1626 |
} |
| 1627 |
|
| 1628 |
errno = ENOENT; |
| 1629 |
return NULL; |
| 1630 |
} |
| 1631 |
|
| 1632 |
static int destroy_cache_key(pool *p, unsigned long algo, const char *path, |
| 1633 |
time_t mtime, off_t start, off_t len) { |
| 1634 |
struct digest_cache_key *cache_key; |
| 1635 |
int res; |
| 1636 |
|
| 1637 |
cache_key = find_cache_key(p, algo, path, mtime, start, len); |
| 1638 |
if (cache_key == NULL) { |
| 1639 |
return -1; |
| 1640 |
} |
| 1641 |
|
| 1642 |
res = xaset_remove(digest_cache_keys, (xasetmember_t *) cache_key); |
| 1643 |
if (res < 0) { |
| 1644 |
pr_trace_msg(trace_channel, 12, |
| 1645 |
"error removing cache key '%s' from set: %s", cache_key->key, |
| 1646 |
strerror(errno)); |
| 1647 |
} |
| 1648 |
|
| 1649 |
destroy_pool(cache_key->pool); |
| 1650 |
return 0; |
| 1651 |
} |
| 1652 |
|
| 1653 |
static int remove_cached_digest(pool *p, unsigned long algo, const char *path, |
| 1654 |
time_t mtime, off_t start, size_t len) { |
| 1655 |
const char *key; |
| 1656 |
pr_table_t *cache; |
| 1657 |
|
| 1658 |
cache = get_cache(algo); |
| 1659 |
if (cache == NULL) { |
| 1660 |
return -1; |
| 1661 |
} |
| 1662 |
|
| 1663 |
key = get_key_for_cache(p, path, mtime, start, len); |
| 1664 |
if (key == NULL) { |
| 1665 |
return -1; |
| 1666 |
} |
| 1667 |
|
| 1668 |
if (pr_table_remove(cache, key, NULL) == NULL) { |
| 1669 |
return -1; |
| 1670 |
} |
| 1671 |
|
| 1672 |
destroy_cache_key(p, algo, path, mtime, start, len); |
| 1673 |
return 0; |
| 1674 |
} |
| 1675 |
|
| 1676 |
static int add_cached_digest(pool *p, cmd_rec *cmd, unsigned long algo, |
| 1677 |
const char *path, time_t mtime, off_t start, size_t len, |
| 1678 |
const char *hex_digest) { |
| 1679 |
int res; |
| 1680 |
struct digest_cache_key *cache_key; |
| 1681 |
pr_table_t *cache; |
| 1682 |
const char *algo_name; |
| 1683 |
|
| 1684 |
if (digest_caching == FALSE) { |
| 1685 |
return 0; |
| 1686 |
} |
| 1687 |
|
| 1688 |
cache = get_cache(algo); |
| 1689 |
if (cache == NULL) { |
| 1690 |
return -1; |
| 1691 |
} |
| 1692 |
|
| 1693 |
cache_key = create_cache_key(p, algo, path, mtime, start, len, hex_digest); |
| 1694 |
|
| 1695 |
|
| 1696 |
algo_name = get_algo_name(algo, 0); |
| 1697 |
if (pr_table_add(cmd->notes, "mod_digest.algo", |
| 1698 |
pstrdup(cmd->pool, algo_name), 0) < 0) { |
| 1699 |
if (errno != EEXIST) { |
| 1700 |
pr_trace_msg(trace_channel, 3, |
| 1701 |
"error adding 'mod_digest.algo' note: %s", strerror(errno)); |
| 1702 |
} |
| 1703 |
} |
| 1704 |
|
| 1705 |
if (pr_table_add(cmd->notes, "mod_digest.digest", |
| 1706 |
pstrdup(cmd->pool, hex_digest), 0) < 0) { |
| 1707 |
if (errno != EEXIST) { |
| 1708 |
pr_trace_msg(trace_channel, 3, |
| 1709 |
"error adding 'mod_digest.digest' note: %s", strerror(errno)); |
| 1710 |
} |
| 1711 |
} |
| 1712 |
|
| 1713 |
res = pr_table_add(cache, cache_key->key, (void *) cache_key->hex_digest, 0); |
| 1714 |
if (res == 0) { |
| 1715 |
pr_trace_msg(trace_channel, 12, |
| 1716 |
"cached digest '%s' for %s digest, key '%s'", hex_digest, |
| 1717 |
get_algo_name(algo, 0), cache_key->key); |
| 1718 |
} |
| 1719 |
|
| 1720 |
return res; |
| 1721 |
} |
| 1722 |
|
| 1723 |
static char *get_cached_digest(pool *p, unsigned long algo, const char *path, |
| 1724 |
time_t mtime, off_t start, size_t len) { |
| 1725 |
const char *algo_name, *key; |
| 1726 |
pr_table_t *cache; |
| 1727 |
const void *val; |
| 1728 |
|
| 1729 |
if (digest_caching == FALSE) { |
| 1730 |
errno = ENOENT; |
| 1731 |
return NULL; |
| 1732 |
} |
| 1733 |
|
| 1734 |
cache = get_cache(algo); |
| 1735 |
if (cache == NULL) { |
| 1736 |
return NULL; |
| 1737 |
} |
| 1738 |
|
| 1739 |
key = get_key_for_cache(p, path, mtime, start, len); |
| 1740 |
if (key == NULL) { |
| 1741 |
return NULL; |
| 1742 |
} |
| 1743 |
|
| 1744 |
algo_name = get_algo_name(algo, 0); |
| 1745 |
|
| 1746 |
pr_trace_msg(trace_channel, 19, |
| 1747 |
"checking for cached %s digest using key '%s'", algo_name, key); |
| 1748 |
|
| 1749 |
val = pr_table_get(cache, key, NULL); |
| 1750 |
if (val != NULL) { |
| 1751 |
char *hex_digest; |
| 1752 |
time_t now; |
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
time(&now); |
| 1758 |
|
| 1759 |
if (now > (mtime + digest_cache_max_age)) { |
| 1760 |
pr_trace_msg(trace_channel, 12, |
| 1761 |
"cached digest for %s digest using key '%s' has expired, evicting", |
| 1762 |
algo_name, key); |
| 1763 |
|
| 1764 |
if (remove_cached_digest(p, algo, path, mtime, start, len) < 0) { |
| 1765 |
pr_trace_msg(trace_channel, 15, |
| 1766 |
"error removing key '%s' from %s cache: %s", key, algo_name, |
| 1767 |
strerror(errno)); |
| 1768 |
} |
| 1769 |
|
| 1770 |
errno = ENOENT; |
| 1771 |
return NULL; |
| 1772 |
} |
| 1773 |
|
| 1774 |
hex_digest = pstrdup(p, val); |
| 1775 |
pr_trace_msg(trace_channel, 12, |
| 1776 |
"using cached digest '%s' for %s digest, key '%s'", hex_digest, |
| 1777 |
algo_name, key); |
| 1778 |
return hex_digest; |
| 1779 |
} |
| 1780 |
|
| 1781 |
errno = ENOENT; |
| 1782 |
return NULL; |
| 1783 |
} |
| 1784 |
|
| 1785 |
static int digest_cache_expiry_cb(CALLBACK_FRAME) { |
| 1786 |
struct digest_cache_key *cache_key; |
| 1787 |
time_t now; |
| 1788 |
pool *tmp_pool; |
| 1789 |
|
| 1790 |
if (digest_cache_keys == NULL || |
| 1791 |
digest_cache_keys->xas_list == NULL) { |
| 1792 |
|
| 1793 |
return 1; |
| 1794 |
} |
| 1795 |
|
| 1796 |
time(&now); |
| 1797 |
tmp_pool = make_sub_pool(digest_pool); |
| 1798 |
pr_pool_tag(tmp_pool, "Digest cache expiry pool"); |
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
for (cache_key = (struct digest_cache_key *) digest_cache_keys->xas_list; |
| 1806 |
cache_key != NULL; |
| 1807 |
) { |
| 1808 |
struct digest_cache_key *next_key; |
| 1809 |
|
| 1810 |
next_key = cache_key->next; |
| 1811 |
|
| 1812 |
if (now > (cache_key->mtime + digest_cache_max_age)) { |
| 1813 |
char *key_text; |
| 1814 |
|
| 1815 |
key_text = pstrdup(tmp_pool, cache_key->key); |
| 1816 |
if (remove_cached_digest(tmp_pool, cache_key->algo, cache_key->path, |
| 1817 |
cache_key->mtime, cache_key->start, cache_key->len) < 0) { |
| 1818 |
pr_trace_msg(trace_channel, 12, |
| 1819 |
"error removing cache key '%s' from set: %s", key_text, |
| 1820 |
strerror(errno)); |
| 1821 |
|
| 1822 |
} else { |
| 1823 |
pr_trace_msg(trace_channel, 15, |
| 1824 |
"removed expired cache key '%s' from set", key_text); |
| 1825 |
} |
| 1826 |
|
| 1827 |
} else { |
| 1828 |
break; |
| 1829 |
} |
| 1830 |
|
| 1831 |
cache_key = next_key; |
| 1832 |
} |
| 1833 |
|
| 1834 |
destroy_pool(tmp_pool); |
| 1835 |
|
| 1836 |
|
| 1837 |
return 1; |
| 1838 |
} |
| 1839 |
|
| 1840 |
static int check_cache_size(cmd_rec *cmd) { |
| 1841 |
unsigned int cache_size; |
| 1842 |
|
| 1843 |
|
| 1844 |
cache_size = get_cache_size(); |
| 1845 |
if (cache_size >= digest_cache_max_size) { |
| 1846 |
int xerrno = EAGAIN; |
| 1847 |
|
| 1848 |
#if defined(EBUSY) |
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
xerrno = EBUSY; |
| 1853 |
#endif |
| 1854 |
|
| 1855 |
pr_log_debug(DEBUG5, MOD_DIGEST_VERSION |
| 1856 |
": cache size (%u) meets/exceeds max cache size (%u), " |
| 1857 |
"refusing %s command", cache_size, digest_cache_max_size, |
| 1858 |
(char *) cmd->argv[0]); |
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
pr_event_generate("mod_digest.max-cache-size", NULL); |
| 1864 |
|
| 1865 |
errno = xerrno; |
| 1866 |
return -1; |
| 1867 |
} |
| 1868 |
|
| 1869 |
return 0; |
| 1870 |
} |
| 1871 |
|
| 1872 |
static char *get_digest(cmd_rec *cmd, unsigned long algo, const char *path, |
| 1873 |
time_t mtime, off_t start, size_t len, int flags) { |
| 1874 |
int res, free_md = FALSE; |
| 1875 |
const EVP_MD *md; |
| 1876 |
unsigned char *digest = NULL; |
| 1877 |
unsigned int digest_len; |
| 1878 |
char *hex_digest; |
| 1879 |
const char *algo_name; |
| 1880 |
|
| 1881 |
hex_digest = get_cached_digest(cmd->tmp_pool, algo, path, mtime, start, len); |
| 1882 |
|
| 1883 |
|
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
if (check_cache_size(cmd) < 0) { |
| 1888 |
return NULL; |
| 1889 |
} |
| 1890 |
|
| 1891 |
if (hex_digest != NULL) { |
| 1892 |
|
| 1893 |
algo_name = get_algo_name(algo, 0); |
| 1894 |
if (pr_table_add(cmd->notes, "mod_digest.algo", |
| 1895 |
pstrdup(cmd->pool, algo_name), 0) < 0) { |
| 1896 |
pr_trace_msg(trace_channel, 3, |
| 1897 |
"error adding 'mod_digest.algo' note: %s", strerror(errno)); |
| 1898 |
} |
| 1899 |
|
| 1900 |
if (pr_table_add(cmd->notes, "mod_digest.digest", |
| 1901 |
pstrdup(cmd->pool, hex_digest), 0) < 0) { |
| 1902 |
pr_trace_msg(trace_channel, 3, |
| 1903 |
"error adding 'mod_digest.digest' note: %s", strerror(errno)); |
| 1904 |
} |
| 1905 |
|
| 1906 |
if (flags & PR_STR_FL_HEX_USE_UC) { |
| 1907 |
register unsigned int i; |
| 1908 |
|
| 1909 |
for (i = 0; hex_digest[i]; i++) { |
| 1910 |
hex_digest[i] = toupper((int) hex_digest[i]); |
| 1911 |
} |
| 1912 |
} |
| 1913 |
|
| 1914 |
return hex_digest; |
| 1915 |
} |
| 1916 |
|
| 1917 |
md = get_algo_md(algo, &free_md); |
| 1918 |
digest_len = EVP_MD_size(md); |
| 1919 |
digest = palloc(cmd->tmp_pool, digest_len); |
| 1920 |
|
| 1921 |
res = compute_digest(cmd->tmp_pool, path, start, len, md, digest, |
| 1922 |
&digest_len, &mtime); |
| 1923 |
if (res < 0) { |
| 1924 |
if (free_md == TRUE) { |
| 1925 |
free_algo_md(md); |
| 1926 |
} |
| 1927 |
|
| 1928 |
return NULL; |
| 1929 |
} |
| 1930 |
|
| 1931 |
hex_digest = pr_str_bin2hex(cmd->tmp_pool, digest, digest_len, |
| 1932 |
PR_STR_FL_HEX_USE_LC); |
| 1933 |
|
| 1934 |
if (add_cached_digest(cmd->pool, cmd, algo, path, mtime, start, len, |
| 1935 |
hex_digest) < 0) { |
| 1936 |
pr_trace_msg(trace_channel, 8, |
| 1937 |
"error caching %s digest for path '%s': %s", get_algo_name(algo, 0), |
| 1938 |
path, strerror(errno)); |
| 1939 |
} |
| 1940 |
|
| 1941 |
|
| 1942 |
algo_name = get_algo_name(algo, 0); |
| 1943 |
if (pr_table_add(cmd->notes, "mod_digest.algo", |
| 1944 |
pstrdup(cmd->pool, algo_name), 0) < 0) { |
| 1945 |
pr_trace_msg(trace_channel, 3, |
| 1946 |
"error adding 'mod_digest.algo' note: %s", strerror(errno)); |
| 1947 |
} |
| 1948 |
|
| 1949 |
if (pr_table_add(cmd->notes, "mod_digest.digest", |
| 1950 |
pstrdup(cmd->pool, hex_digest), 0) < 0) { |
| 1951 |
pr_trace_msg(trace_channel, 3, |
| 1952 |
"error adding 'mod_digest.digest' note: %s", strerror(errno)); |
| 1953 |
} |
| 1954 |
|
| 1955 |
if (flags & PR_STR_FL_HEX_USE_UC) { |
| 1956 |
register unsigned int i; |
| 1957 |
|
| 1958 |
for (i = 0; hex_digest[i]; i++) { |
| 1959 |
hex_digest[i] = toupper((int) hex_digest[i]); |
| 1960 |
} |
| 1961 |
} |
| 1962 |
|
| 1963 |
if (free_md == TRUE) { |
| 1964 |
free_algo_md(md); |
| 1965 |
} |
| 1966 |
|
| 1967 |
return hex_digest; |
| 1968 |
} |
| 1969 |
|
| 1970 |
static modret_t *digest_xcmd(cmd_rec *cmd, unsigned long algo) { |
| 1971 |
char *orig_path, *path; |
| 1972 |
struct stat st; |
| 1973 |
|
| 1974 |
CHECK_CMD_MIN_ARGS(cmd, 2); |
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
if (cmd->argc == 3) { |
| 1981 |
pr_response_add_err(R_501, _("Invalid number of parameters")); |
| 1982 |
return PR_ERROR((cmd)); |
| 1983 |
} |
| 1984 |
|
| 1985 |
|
| 1986 |
path = orig_path = cmd->argv[1]; |
| 1987 |
|
| 1988 |
if (pr_fsio_lstat(path, &st) == 0) { |
| 1989 |
if (S_ISLNK(st.st_mode)) { |
| 1990 |
char link_path[PR_TUNABLE_PATH_MAX]; |
| 1991 |
int link_len; |
| 1992 |
|
| 1993 |
memset(link_path, '\0', sizeof(link_path)); |
| 1994 |
link_len = dir_readlink(cmd->tmp_pool, path, link_path, |
| 1995 |
sizeof(link_path)-1, PR_DIR_READLINK_FL_HANDLE_REL_PATH); |
| 1996 |
if (link_len > 0) { |
| 1997 |
link_path[link_len] = '\0'; |
| 1998 |
path = pstrdup(cmd->tmp_pool, link_path); |
| 1999 |
} |
| 2000 |
} |
| 2001 |
} |
| 2002 |
|
| 2003 |
path = dir_realpath(cmd->tmp_pool, path); |
| 2004 |
if (path == NULL) { |
| 2005 |
int xerrno = errno; |
| 2006 |
|
| 2007 |
pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno)); |
| 2008 |
|
| 2009 |
pr_cmd_set_errno(cmd, xerrno); |
| 2010 |
errno = xerrno; |
| 2011 |
return PR_ERROR(cmd); |
| 2012 |
} |
| 2013 |
|
| 2014 |
if (blacklisted_file(path) == TRUE) { |
| 2015 |
pr_log_debug(DEBUG8, MOD_DIGEST_VERSION |
| 2016 |
": rejecting request to checksum blacklisted special file '%s'", path); |
| 2017 |
pr_response_add_err(R_550, "%s: %s", (char *) cmd->arg, strerror(EPERM)); |
| 2018 |
pr_cmd_set_errno(cmd, EPERM); |
| 2019 |
errno = EPERM; |
| 2020 |
return PR_ERROR(cmd); |
| 2021 |
} |
| 2022 |
|
| 2023 |
if (!dir_check(cmd->tmp_pool, cmd, cmd->group, path, NULL)) { |
| 2024 |
int xerrno = EPERM; |
| 2025 |
|
| 2026 |
pr_log_debug(DEBUG8, MOD_DIGEST_VERSION |
| 2027 |
": %s denied by <Limit> configuration", (char *) cmd->argv[0]); |
| 2028 |
pr_response_add_err(R_550, "%s: %s", orig_path, strerror(xerrno)); |
| 2029 |
|
| 2030 |
pr_cmd_set_errno(cmd, xerrno); |
| 2031 |
errno = xerrno; |
| 2032 |
return PR_ERROR(cmd); |
| 2033 |
} |
| 2034 |
|
| 2035 |
pr_fs_clear_cache2(path); |
| 2036 |
if (pr_fsio_stat(path, &st) < 0) { |
| 2037 |
int xerrno = errno; |
| 2038 |
|
| 2039 |
pr_log_debug(DEBUG8, MOD_DIGEST_VERSION |
| 2040 |
": error checking %s: %s", path, strerror(xerrno)); |
| 2041 |
pr_response_add_err(R_550, "%s: %s", orig_path, strerror(xerrno)); |
| 2042 |
|
| 2043 |
pr_cmd_set_errno(cmd, xerrno); |
| 2044 |
errno = xerrno; |
| 2045 |
return PR_ERROR(cmd); |
| 2046 |
} |
| 2047 |
|
| 2048 |
if (!S_ISREG(st.st_mode)) { |
| 2049 |
pr_log_debug(DEBUG5, MOD_DIGEST_VERSION |
| 2050 |
": unable to handle %s for non-file path '%s'", (char *) cmd->argv[0], |
| 2051 |
path); |
| 2052 |
pr_response_add_err(R_550, _("%s: Not a regular file"), orig_path); |
| 2053 |
return PR_ERROR(cmd); |
| 2054 |
|
| 2055 |
} else { |
| 2056 |
off_t len, start_pos, end_pos; |
| 2057 |
const EVP_MD *md = NULL; |
| 2058 |
int free_md = FALSE; |
| 2059 |
|
| 2060 |
if (cmd->argc > 3) { |
| 2061 |
char *ptr = NULL; |
| 2062 |
|
| 2063 |
#if defined(HAVE_STRTOULL) |
| 2064 |
start_pos = strtoull(cmd->argv[2], &ptr, 10); |
| 2065 |
#else |
| 2066 |
start_pos = strtoul(cmd->argv[2], &ptr, 10); |
| 2067 |
#endif |
| 2068 |
|
| 2069 |
if (ptr && *ptr) { |
| 2070 |
pr_response_add_err(R_501, |
| 2071 |
_("%s requires a start greater than or equal to 0"), |
| 2072 |
(char *) cmd->argv[0]); |
| 2073 |
return PR_ERROR(cmd); |
| 2074 |
} |
| 2075 |
|
| 2076 |
ptr = NULL; |
| 2077 |
#if defined(HAVE_STRTOULL) |
| 2078 |
end_pos = strtoull(cmd->argv[3], &ptr, 10); |
| 2079 |
#else |
| 2080 |
end_pos = strtoul(cmd->argv[3], &ptr, 10); |
| 2081 |
#endif |
| 2082 |
|
| 2083 |
if (ptr && *ptr) { |
| 2084 |
pr_response_add_err(R_501, |
| 2085 |
_("%s requires an end greater than 0"), (char *) cmd->argv[0]); |
| 2086 |
return PR_ERROR(cmd); |
| 2087 |
} |
| 2088 |
|
| 2089 |
} else { |
| 2090 |
start_pos = 0; |
| 2091 |
end_pos = st.st_size; |
| 2092 |
} |
| 2093 |
|
| 2094 |
if (end_pos > st.st_size) { |
| 2095 |
pr_response_add_err(R_501, |
| 2096 |
_("%s: end exceeds file size"), (char *) cmd->argv[0]); |
| 2097 |
return PR_ERROR(cmd); |
| 2098 |
} |
| 2099 |
|
| 2100 |
len = end_pos - start_pos; |
| 2101 |
|
| 2102 |
if (start_pos >= end_pos) { |
| 2103 |
pr_response_add_err(R_501, |
| 2104 |
_("%s requires end (%" PR_LU ") greater than start (%" PR_LU ")"), |
| 2105 |
(char *) cmd->argv[0], (pr_off_t) end_pos, (pr_off_t) start_pos); |
| 2106 |
return PR_ERROR(cmd); |
| 2107 |
} |
| 2108 |
|
| 2109 |
if (check_digest_max_size(len) < 0) { |
| 2110 |
pr_response_add_err(R_550, "%s: %s", orig_path, strerror(EPERM)); |
| 2111 |
pr_cmd_set_errno(cmd, EPERM); |
| 2112 |
errno = EPERM; |
| 2113 |
return PR_ERROR(cmd); |
| 2114 |
} |
| 2115 |
|
| 2116 |
md = get_algo_md(algo, &free_md); |
| 2117 |
if (md != NULL) { |
| 2118 |
char *hex_digest; |
| 2119 |
|
| 2120 |
if (free_md == TRUE) { |
| 2121 |
free_algo_md(md); |
| 2122 |
} |
| 2123 |
|
| 2124 |
hex_digest = get_digest(cmd, algo, path, st.st_mtime, start_pos, len, |
| 2125 |
PR_STR_FL_HEX_USE_UC); |
| 2126 |
if (hex_digest != NULL) { |
| 2127 |
pr_response_add(R_213, "%s", hex_digest); |
| 2128 |
return PR_HANDLED(cmd); |
| 2129 |
} |
| 2130 |
|
| 2131 |
|
| 2132 |
pr_response_add_err(R_550, "%s: %s", orig_path, strerror(errno)); |
| 2133 |
|
| 2134 |
} else { |
| 2135 |
pr_response_add_err(R_550, _("%s: Hash algorithm not available"), |
| 2136 |
(char *) cmd->argv[0]); |
| 2137 |
} |
| 2138 |
} |
| 2139 |
|
| 2140 |
return PR_ERROR(cmd); |
| 2141 |
} |
| 2142 |
|
| 2143 |
|
| 2144 |
|
| 2145 |
|
| 2146 |
MODRET digest_hash(cmd_rec *cmd) { |
| 2147 |
int xerrno = 0; |
| 2148 |
char *error_code = NULL, *orig_path = NULL, *path = NULL, *hex_digest = NULL; |
| 2149 |
struct stat st; |
| 2150 |
off_t len, start_pos, end_pos; |
| 2151 |
|
| 2152 |
if (digest_engine == FALSE) { |
| 2153 |
return PR_DECLINED(cmd); |
| 2154 |
} |
| 2155 |
|
| 2156 |
CHECK_CMD_MIN_ARGS(cmd, 2); |
| 2157 |
|
| 2158 |
path = orig_path = pr_fs_decode_path(cmd->tmp_pool, cmd->arg); |
| 2159 |
|
| 2160 |
if (pr_fsio_lstat(path, &st) == 0) { |
| 2161 |
if (S_ISLNK(st.st_mode)) { |
| 2162 |
char link_path[PR_TUNABLE_PATH_MAX]; |
| 2163 |
int link_len; |
| 2164 |
|
| 2165 |
memset(link_path, '\0', sizeof(link_path)); |
| 2166 |
link_len = dir_readlink(cmd->tmp_pool, path, link_path, |
| 2167 |
sizeof(link_path)-1, PR_DIR_READLINK_FL_HANDLE_REL_PATH); |
| 2168 |
if (link_len > 0) { |
| 2169 |
link_path[link_len] = '\0'; |
| 2170 |
path = pstrdup(cmd->tmp_pool, link_path); |
| 2171 |
} |
| 2172 |
} |
| 2173 |
} |
| 2174 |
|
| 2175 |
path = dir_realpath(cmd->tmp_pool, path); |
| 2176 |
if (path == NULL) { |
| 2177 |
xerrno = errno; |
| 2178 |
|
| 2179 |
pr_response_add_err(R_550, "%s: %s", orig_path, strerror(xerrno)); |
| 2180 |
|
| 2181 |
pr_cmd_set_errno(cmd, xerrno); |
| 2182 |
errno = xerrno; |
| 2183 |
return PR_ERROR(cmd); |
| 2184 |
} |
| 2185 |
|
| 2186 |
if (blacklisted_file(path) == TRUE) { |
| 2187 |
pr_log_debug(DEBUG8, MOD_DIGEST_VERSION |
| 2188 |
": rejecting request to checksum blacklisted special file '%s'", path); |
| 2189 |
pr_response_add_err(R_556, "%s: %s", (char *) cmd->arg, strerror(EPERM)); |
| 2190 |
pr_cmd_set_errno(cmd, EPERM); |
| 2191 |
errno = EPERM; |
| 2192 |
return PR_ERROR(cmd); |
| 2193 |
} |
| 2194 |
|
| 2195 |
if (!dir_check(cmd->tmp_pool, cmd, cmd->group, path, NULL)) { |
| 2196 |
xerrno = EPERM; |
| 2197 |
|
| 2198 |
pr_log_debug(DEBUG8, MOD_DIGEST_VERSION |
| 2199 |
": %s denied by <Limit> configuration", (char *) cmd->argv[0]); |
| 2200 |
pr_response_add_err(R_552, "%s: %s", orig_path, strerror(xerrno)); |
| 2201 |
|
| 2202 |
pr_cmd_set_errno(cmd, xerrno); |
| 2203 |
errno = xerrno; |
| 2204 |
return PR_ERROR(cmd); |
| 2205 |
} |
| 2206 |
|
| 2207 |
pr_fs_clear_cache2(path); |
| 2208 |
if (pr_fsio_stat(path, &st) < 0) { |
| 2209 |
xerrno = errno; |
| 2210 |
|
| 2211 |
pr_log_debug(DEBUG8, MOD_DIGEST_VERSION |
| 2212 |
": error checking %s: %s", path, strerror(xerrno)); |
| 2213 |
pr_response_add_err(R_550, "%s: %s", orig_path, strerror(xerrno)); |
| 2214 |
|
| 2215 |
pr_cmd_set_errno(cmd, xerrno); |
| 2216 |
errno = xerrno; |
| 2217 |
return PR_ERROR(cmd); |
| 2218 |
} |
| 2219 |
|
| 2220 |
if (!S_ISREG(st.st_mode)) { |
| 2221 |
pr_log_debug(DEBUG5, MOD_DIGEST_VERSION |
| 2222 |
": unable to handle %s for non-file path '%s'", (char *) cmd->argv[0], |
| 2223 |
path); |
| 2224 |
pr_response_add_err(R_553, _("%s: Not a regular file"), orig_path); |
| 2225 |
return PR_ERROR(cmd); |
| 2226 |
} |
| 2227 |
|
| 2228 |
start_pos = 0; |
| 2229 |
end_pos = st.st_size; |
| 2230 |
len = end_pos - start_pos; |
| 2231 |
|
| 2232 |
if (check_digest_max_size(len) < 0) { |
| 2233 |
pr_response_add_err(R_556, "%s: %s", orig_path, strerror(EPERM)); |
| 2234 |
pr_cmd_set_errno(cmd, EPERM); |
| 2235 |
errno = EPERM; |
| 2236 |
return PR_ERROR(cmd); |
| 2237 |
} |
| 2238 |
|
| 2239 |
pr_trace_msg(trace_channel, 14, "%s: using %s algorithm on path '%s'", |
| 2240 |
(char *) cmd->argv[0], get_algo_name(digest_hash_algo, 0), path); |
| 2241 |
|
| 2242 |
hex_digest = get_digest(cmd, digest_hash_algo, path, st.st_mtime, start_pos, |
| 2243 |
len, PR_STR_FL_HEX_USE_LC); |
| 2244 |
xerrno = errno; |
| 2245 |
|
| 2246 |
if (hex_digest != NULL) { |
| 2247 |
pr_response_add(R_213, "%s %" PR_LU "-%" PR_LU " %s %s", |
| 2248 |
get_algo_name(digest_hash_algo, DIGEST_ALGO_FL_IANA_STYLE), |
| 2249 |
(pr_off_t) start_pos, (pr_off_t) end_pos, hex_digest, orig_path); |
| 2250 |
return PR_HANDLED(cmd); |
| 2251 |
} |
| 2252 |
|
| 2253 |
switch (xerrno) { |
| 2254 |
#if defined(EBUSY) |
| 2255 |
case EBUSY: |
| 2256 |
#endif |
| 2257 |
case EAGAIN: |
| 2258 |
|
| 2259 |
error_code = R_450; |
| 2260 |
break; |
| 2261 |
|
| 2262 |
case EISDIR: |
| 2263 |
|
| 2264 |
error_code = R_553; |
| 2265 |
break; |
| 2266 |
|
| 2267 |
case EPERM: |
| 2268 |
|
| 2269 |
error_code = R_552; |
| 2270 |
break; |
| 2271 |
|
| 2272 |
default: |
| 2273 |
error_code = R_550; |
| 2274 |
break; |
| 2275 |
} |
| 2276 |
|
| 2277 |
|
| 2278 |
pr_response_add_err(error_code, "%s: %s", orig_path, strerror(xerrno)); |
| 2279 |
|
| 2280 |
pr_cmd_set_errno(cmd, xerrno); |
| 2281 |
errno = xerrno; |
| 2282 |
return PR_ERROR(cmd); |
| 2283 |
} |
| 2284 |
|
| 2285 |
MODRET digest_opts_hash(cmd_rec *cmd) { |
| 2286 |
char *algo_name; |
| 2287 |
|
| 2288 |
if (digest_engine == FALSE) { |
| 2289 |
return PR_DECLINED(cmd); |
| 2290 |
} |
| 2291 |
|
| 2292 |
if (cmd->argc > 2) { |
| 2293 |
pr_response_add_err(R_501, _("OPTS HASH: Wrong number of parameters")); |
| 2294 |
return PR_ERROR(cmd); |
| 2295 |
} |
| 2296 |
|
| 2297 |
if (cmd->argc == 1) { |
| 2298 |
int flags = DIGEST_ALGO_FL_IANA_STYLE; |
| 2299 |
|
| 2300 |
|
| 2301 |
pr_response_add(R_200, "%s", get_algo_name(digest_hash_algo, flags)); |
| 2302 |
return PR_HANDLED(cmd); |
| 2303 |
} |
| 2304 |
|
| 2305 |
|
| 2306 |
|
| 2307 |
algo_name = cmd->argv[1]; |
| 2308 |
|
| 2309 |
if (strcasecmp(algo_name, "CRC32") == 0) { |
| 2310 |
if (digest_algos & DIGEST_ALGO_CRC32) { |
| 2311 |
if (digest_hash_free_md == TRUE) { |
| 2312 |
free_algo_md(digest_hash_md); |
| 2313 |
} |
| 2314 |
|
| 2315 |
digest_hash_algo = DIGEST_ALGO_CRC32; |
| 2316 |
digest_hash_md = get_algo_md(digest_hash_algo, &digest_hash_free_md); |
| 2317 |
|
| 2318 |
} else { |
| 2319 |
pr_response_add_err(R_501, _("%s: Unsupported algorithm"), algo_name); |
| 2320 |
return PR_ERROR(cmd); |
| 2321 |
} |
| 2322 |
|
| 2323 |
#if !defined(OPENSSL_NO_MD5) |
| 2324 |
} else if (strcasecmp(algo_name, "MD5") == 0) { |
| 2325 |
if (digest_algos & DIGEST_ALGO_MD5) { |
| 2326 |
if (digest_hash_free_md == TRUE) { |
| 2327 |
free_algo_md(digest_hash_md); |
| 2328 |
} |
| 2329 |
|
| 2330 |
digest_hash_algo = DIGEST_ALGO_MD5; |
| 2331 |
digest_hash_md = get_algo_md(digest_hash_algo, &digest_hash_free_md); |
| 2332 |
|
| 2333 |
} else { |
| 2334 |
pr_response_add_err(R_501, _("%s: Unsupported algorithm"), algo_name); |
| 2335 |
return PR_ERROR(cmd); |
| 2336 |
} |
| 2337 |
#endif |
| 2338 |
|
| 2339 |
#if !defined(OPENSSL_NO_SHA1) |
| 2340 |
} else if (strcasecmp(algo_name, "SHA-1") == 0) { |
| 2341 |
if (digest_algos & DIGEST_ALGO_SHA1) { |
| 2342 |
if (digest_hash_free_md == TRUE) { |
| 2343 |
free_algo_md(digest_hash_md); |
| 2344 |
} |
| 2345 |
|
| 2346 |
digest_hash_algo = DIGEST_ALGO_SHA1; |
| 2347 |
digest_hash_md = get_algo_md(digest_hash_algo, &digest_hash_free_md); |
| 2348 |
|
| 2349 |
} else { |
| 2350 |
pr_response_add_err(R_501, _("%s: Unsupported algorithm"), algo_name); |
| 2351 |
return PR_ERROR(cmd); |
| 2352 |
} |
| 2353 |
#endif |
| 2354 |
|
| 2355 |
#if !defined(OPENSSL_NO_SHA256) |
| 2356 |
} else if (strcasecmp(algo_name, "SHA-256") == 0) { |
| 2357 |
if (digest_algos & DIGEST_ALGO_SHA256) { |
| 2358 |
if (digest_hash_free_md == TRUE) { |
| 2359 |
free_algo_md(digest_hash_md); |
| 2360 |
} |
| 2361 |
|
| 2362 |
digest_hash_algo = DIGEST_ALGO_SHA256; |
| 2363 |
digest_hash_md = get_algo_md(digest_hash_algo, &digest_hash_free_md); |
| 2364 |
|
| 2365 |
} else { |
| 2366 |
pr_response_add_err(R_501, _("%s: Unsupported algorithm"), algo_name); |
| 2367 |
return PR_ERROR(cmd); |
| 2368 |
} |
| 2369 |
#endif |
| 2370 |
|
| 2371 |
#if !defined(OPENSSL_NO_SHA512) |
| 2372 |
} else if (strcasecmp(algo_name, "SHA-512") == 0) { |
| 2373 |
if (digest_algos & DIGEST_ALGO_SHA512) { |
| 2374 |
if (digest_hash_free_md == TRUE) { |
| 2375 |
free_algo_md(digest_hash_md); |
| 2376 |
} |
| 2377 |
|
| 2378 |
digest_hash_algo = DIGEST_ALGO_SHA512; |
| 2379 |
digest_hash_md = get_algo_md(digest_hash_algo, &digest_hash_free_md); |
| 2380 |
|
| 2381 |
} else { |
| 2382 |
pr_response_add_err(R_501, _("%s: Unsupported algorithm"), algo_name); |
| 2383 |
return PR_ERROR(cmd); |
| 2384 |
} |
| 2385 |
#endif |
| 2386 |
|
| 2387 |
} else { |
| 2388 |
pr_response_add_err(R_501, _("%s: Unsupported algorithm"), algo_name); |
| 2389 |
return PR_ERROR(cmd); |
| 2390 |
} |
| 2391 |
|
| 2392 |
digest_hash_feat_remove(); |
| 2393 |
digest_hash_feat_add(cmd->tmp_pool); |
| 2394 |
|
| 2395 |
pr_response_add(R_200, "%s", algo_name); |
| 2396 |
return PR_HANDLED(cmd); |
| 2397 |
} |
| 2398 |
|
| 2399 |
MODRET digest_pre_retr(cmd_rec *cmd) { |
| 2400 |
config_rec *c; |
| 2401 |
const char *proto; |
| 2402 |
|
| 2403 |
if (digest_engine == FALSE) { |
| 2404 |
return PR_DECLINED(cmd); |
| 2405 |
} |
| 2406 |
|
| 2407 |
if (digest_caching == FALSE) { |
| 2408 |
return PR_DECLINED(cmd); |
| 2409 |
} |
| 2410 |
|
| 2411 |
if (session.sf_flags & SF_ASCII) { |
| 2412 |
pr_trace_msg(trace_channel, 19, |
| 2413 |
"%s: ASCII mode transfer (TYPE A) in effect, not computing/caching " |
| 2414 |
"opportunistic digest for download", (char *) cmd->argv[0]); |
| 2415 |
return PR_DECLINED(cmd); |
| 2416 |
} |
| 2417 |
|
| 2418 |
if (digest_opts & DIGEST_OPT_NO_TRANSFER_CACHE) { |
| 2419 |
pr_trace_msg(trace_channel, 19, |
| 2420 |
"%s: NoTransferCache DigestOption in effect, not computing/caching " |
| 2421 |
"opportunistic digest for download", (char *) cmd->argv[0]); |
| 2422 |
return PR_DECLINED(cmd); |
| 2423 |
} |
| 2424 |
|
| 2425 |
if (session.restart_pos > 0) { |
| 2426 |
pr_trace_msg(trace_channel, 12, |
| 2427 |
"REST %" PR_LU " sent before %s, declining to compute transfer digest", |
| 2428 |
(pr_off_t) session.restart_pos, (char *) cmd->argv[0]); |
| 2429 |
return PR_DECLINED(cmd); |
| 2430 |
} |
| 2431 |
|
| 2432 |
proto = pr_session_get_protocol(0); |
| 2433 |
if (strcasecmp(proto, "ftp") == 0 || |
| 2434 |
strcasecmp(proto, "ftps") == 0) { |
| 2435 |
unsigned char use_sendfile = TRUE; |
| 2436 |
|
| 2437 |
|
| 2438 |
|
| 2439 |
|
| 2440 |
c = find_config(CURRENT_CONF, CONF_PARAM, "UseSendfile", FALSE); |
| 2441 |
if (c != NULL) { |
| 2442 |
use_sendfile = *((unsigned char *) c->argv[0]); |
| 2443 |
} |
| 2444 |
|
| 2445 |
if (use_sendfile) { |
| 2446 |
pr_trace_msg(trace_channel, 12, |
| 2447 |
"UseSendfile in effect, declining to compute digest for %s transfer", |
| 2448 |
(char *) cmd->argv[0]); |
| 2449 |
return PR_DECLINED(cmd); |
| 2450 |
} |
| 2451 |
} |
| 2452 |
|
| 2453 |
digest_cache_xfer_ctx = EVP_MD_CTX_create(); |
| 2454 |
if (EVP_DigestInit_ex(digest_cache_xfer_ctx, digest_hash_md, NULL) != 1) { |
| 2455 |
pr_trace_msg(trace_channel, 3, |
| 2456 |
"error preparing %s digest: %s", get_algo_name(digest_hash_algo, 0), |
| 2457 |
get_errors()); |
| 2458 |
EVP_MD_CTX_destroy(digest_cache_xfer_ctx); |
| 2459 |
digest_cache_xfer_ctx = NULL; |
| 2460 |
|
| 2461 |
} else { |
| 2462 |
pr_event_register(&digest_module, "core.data-write", digest_data_xfer_ev, |
| 2463 |
digest_cache_xfer_ctx); |
| 2464 |
pr_event_register(&digest_module, "mod_sftp.sftp.data-write", |
| 2465 |
digest_data_xfer_ev, digest_cache_xfer_ctx); |
| 2466 |
} |
| 2467 |
|
| 2468 |
return PR_DECLINED(cmd); |
| 2469 |
} |
| 2470 |
|
| 2471 |
MODRET digest_log(cmd_rec *cmd) { |
| 2472 |
const char *algo_name; |
| 2473 |
unsigned char *digest; |
| 2474 |
unsigned int digest_len; |
| 2475 |
|
| 2476 |
if (digest_engine == FALSE) { |
| 2477 |
return PR_DECLINED(cmd); |
| 2478 |
} |
| 2479 |
|
| 2480 |
if (pr_cmd_cmp(cmd, PR_CMD_RETR_ID) == 0) { |
| 2481 |
pr_event_unregister(&digest_module, "core.data-write", NULL); |
| 2482 |
pr_event_unregister(&digest_module, "mod_sftp.sftp.data-write", NULL); |
| 2483 |
|
| 2484 |
} else if (pr_cmd_cmp(cmd, PR_CMD_APPE_ID) == 0 || |
| 2485 |
pr_cmd_cmp(cmd, PR_CMD_STOR_ID) == 0) { |
| 2486 |
pr_event_unregister(&digest_module, "core.data-read", NULL); |
| 2487 |
pr_event_unregister(&digest_module, "mod_sftp.sftp.data-read", NULL); |
| 2488 |
|
| 2489 |
} else { |
| 2490 |
|
| 2491 |
return PR_DECLINED(cmd); |
| 2492 |
} |
| 2493 |
|
| 2494 |
if (digest_caching == FALSE || |
| 2495 |
(digest_opts & DIGEST_OPT_NO_TRANSFER_CACHE)) { |
| 2496 |
return PR_DECLINED(cmd); |
| 2497 |
} |
| 2498 |
|
| 2499 |
if (digest_cache_xfer_ctx == NULL) { |
| 2500 |
return PR_DECLINED(cmd); |
| 2501 |
} |
| 2502 |
|
| 2503 |
algo_name = get_algo_name(digest_hash_algo, 0); |
| 2504 |
digest_len = EVP_MD_size(digest_hash_md); |
| 2505 |
digest = palloc(cmd->tmp_pool, digest_len); |
| 2506 |
|
| 2507 |
if (EVP_DigestFinal_ex(digest_cache_xfer_ctx, digest, &digest_len) != 1) { |
| 2508 |
pr_trace_msg(trace_channel, 1, |
| 2509 |
"error finishing %s digest for %s: %s", algo_name, |
| 2510 |
(char *) cmd->argv[0], get_errors()); |
| 2511 |
|
| 2512 |
} else { |
| 2513 |
int res; |
| 2514 |
struct stat st; |
| 2515 |
const char *path; |
| 2516 |
|
| 2517 |
path = session.xfer.path; |
| 2518 |
pr_fs_clear_cache2(path); |
| 2519 |
res = pr_fsio_stat(path, &st); |
| 2520 |
if (res == 0) { |
| 2521 |
char *hex_digest; |
| 2522 |
off_t start, len; |
| 2523 |
time_t mtime; |
| 2524 |
|
| 2525 |
hex_digest = pr_str_bin2hex(cmd->tmp_pool, digest, digest_len, |
| 2526 |
PR_STR_FL_HEX_USE_LC); |
| 2527 |
|
| 2528 |
mtime = st.st_mtime; |
| 2529 |
start = 0; |
| 2530 |
len = st.st_size; |
| 2531 |
|
| 2532 |
if (add_cached_digest(cmd->pool, cmd, digest_hash_algo, path, mtime, |
| 2533 |
start, len, hex_digest) < 0) { |
| 2534 |
pr_trace_msg(trace_channel, 8, |
| 2535 |
"error caching %s digest for path '%s': %s", algo_name, path, |
| 2536 |
strerror(errno)); |
| 2537 |
} |
| 2538 |
|
| 2539 |
} else { |
| 2540 |
pr_trace_msg(trace_channel, 7, |
| 2541 |
"error checking '%s' post-%s: %s", path, (char *) cmd->argv[0], |
| 2542 |
strerror(errno)); |
| 2543 |
} |
| 2544 |
} |
| 2545 |
|
| 2546 |
EVP_MD_CTX_destroy(digest_cache_xfer_ctx); |
| 2547 |
digest_cache_xfer_ctx = NULL; |
| 2548 |
|
| 2549 |
return PR_DECLINED(cmd); |
| 2550 |
} |
| 2551 |
|
| 2552 |
MODRET digest_log_err(cmd_rec *cmd) { |
| 2553 |
if (digest_engine == FALSE) { |
| 2554 |
return PR_DECLINED(cmd); |
| 2555 |
} |
| 2556 |
|
| 2557 |
if (pr_cmd_cmp(cmd, PR_CMD_RETR_ID) == 0) { |
| 2558 |
pr_event_unregister(&digest_module, "core.data-write", NULL); |
| 2559 |
pr_event_unregister(&digest_module, "mod_sftp.sftp.data-write", NULL); |
| 2560 |
|
| 2561 |
} else if (pr_cmd_cmp(cmd, PR_CMD_APPE_ID) == 0 || |
| 2562 |
pr_cmd_cmp(cmd, PR_CMD_STOR_ID) == 0) { |
| 2563 |
pr_event_unregister(&digest_module, "core.data-read", NULL); |
| 2564 |
pr_event_unregister(&digest_module, "mod_sftp.sftp.data-read", NULL); |
| 2565 |
|
| 2566 |
} else { |
| 2567 |
|
| 2568 |
return PR_DECLINED(cmd); |
| 2569 |
} |
| 2570 |
|
| 2571 |
if (digest_caching == FALSE || |
| 2572 |
(digest_opts & DIGEST_OPT_NO_TRANSFER_CACHE)) { |
| 2573 |
return PR_DECLINED(cmd); |
| 2574 |
} |
| 2575 |
|
| 2576 |
if (digest_cache_xfer_ctx != NULL) { |
| 2577 |
EVP_MD_CTX_destroy(digest_cache_xfer_ctx); |
| 2578 |
digest_cache_xfer_ctx = NULL; |
| 2579 |
} |
| 2580 |
|
| 2581 |
return PR_DECLINED(cmd); |
| 2582 |
} |
| 2583 |
|
| 2584 |
MODRET digest_pre_appe(cmd_rec *cmd) { |
| 2585 |
int res; |
| 2586 |
struct stat st; |
| 2587 |
char *path; |
| 2588 |
|
| 2589 |
if (digest_engine == FALSE) { |
| 2590 |
return PR_DECLINED(cmd); |
| 2591 |
} |
| 2592 |
|
| 2593 |
if (digest_caching == FALSE) { |
| 2594 |
return PR_DECLINED(cmd); |
| 2595 |
} |
| 2596 |
|
| 2597 |
|
| 2598 |
|
| 2599 |
|
| 2600 |
|
| 2601 |
|
| 2602 |
path = pr_fs_decode_path(cmd->tmp_pool, cmd->arg); |
| 2603 |
if (path == NULL) { |
| 2604 |
return PR_DECLINED(cmd); |
| 2605 |
} |
| 2606 |
|
| 2607 |
pr_fs_clear_cache2(path); |
| 2608 |
res = pr_fsio_stat(path, &st); |
| 2609 |
if (res == 0) { |
| 2610 |
if (!S_ISREG(st.st_mode)) { |
| 2611 |
|
| 2612 |
return PR_DECLINED(cmd); |
| 2613 |
} |
| 2614 |
|
| 2615 |
if (st.st_size > 0) { |
| 2616 |
|
| 2617 |
return PR_DECLINED(cmd); |
| 2618 |
} |
| 2619 |
} |
| 2620 |
|
| 2621 |
if (session.sf_flags & SF_ASCII) { |
| 2622 |
pr_trace_msg(trace_channel, 19, |
| 2623 |
"%s: ASCII mode transfer (TYPE A) in effect, not computing/caching " |
| 2624 |
"opportunistic digest for upload", (char *) cmd->argv[0]); |
| 2625 |
return PR_DECLINED(cmd); |
| 2626 |
} |
| 2627 |
|
| 2628 |
if (digest_opts & DIGEST_OPT_NO_TRANSFER_CACHE) { |
| 2629 |
pr_trace_msg(trace_channel, 19, |
| 2630 |
"%s: NoTransferCache DigestOption in effect, not computing/caching " |
| 2631 |
"opportunistic digest for upload", (char *) cmd->argv[0]); |
| 2632 |
return PR_DECLINED(cmd); |
| 2633 |
} |
| 2634 |
|
| 2635 |
|
| 2636 |
if (session.restart_pos > 0) { |
| 2637 |
pr_trace_msg(trace_channel, 12, |
| 2638 |
"REST %" PR_LU " sent before %s, declining to compute transfer digest", |
| 2639 |
(pr_off_t) session.restart_pos, (char *) cmd->argv[0]); |
| 2640 |
return PR_DECLINED(cmd); |
| 2641 |
} |
| 2642 |
|
| 2643 |
digest_cache_xfer_ctx = EVP_MD_CTX_create(); |
| 2644 |
if (EVP_DigestInit_ex(digest_cache_xfer_ctx, digest_hash_md, NULL) != 1) { |
| 2645 |
pr_trace_msg(trace_channel, 3, |
| 2646 |
"error preparing %s digest: %s", get_algo_name(digest_hash_algo, 0), |
| 2647 |
get_errors()); |
| 2648 |
EVP_MD_CTX_destroy(digest_cache_xfer_ctx); |
| 2649 |
digest_cache_xfer_ctx = NULL; |
| 2650 |
|
| 2651 |
} else { |
| 2652 |
pr_event_register(&digest_module, "core.data-read", digest_data_xfer_ev, |
| 2653 |
digest_cache_xfer_ctx); |
| 2654 |
pr_event_register(&digest_module, "mod_sftp.sftp.data-read", |
| 2655 |
digest_data_xfer_ev, digest_cache_xfer_ctx); |
| 2656 |
} |
| 2657 |
|
| 2658 |
return PR_DECLINED(cmd); |
| 2659 |
} |
| 2660 |
|
| 2661 |
MODRET digest_pre_stor(cmd_rec *cmd) { |
| 2662 |
if (digest_engine == FALSE) { |
| 2663 |
return PR_DECLINED(cmd); |
| 2664 |
} |
| 2665 |
|
| 2666 |
if (digest_caching == FALSE) { |
| 2667 |
return PR_DECLINED(cmd); |
| 2668 |
} |
| 2669 |
|
| 2670 |
if (session.sf_flags & SF_ASCII) { |
| 2671 |
pr_trace_msg(trace_channel, 19, |
| 2672 |
"%s: ASCII mode transfer (TYPE A) in effect, not computing/caching " |
| 2673 |
"opportunistic digest for upload", (char *) cmd->argv[0]); |
| 2674 |
return PR_DECLINED(cmd); |
| 2675 |
} |
| 2676 |
|
| 2677 |
if (digest_opts & DIGEST_OPT_NO_TRANSFER_CACHE) { |
| 2678 |
pr_trace_msg(trace_channel, 19, |
| 2679 |
"%s: NoTransferCache DigestOption in effect, not computing/caching " |
| 2680 |
"opportunistic digest for upload", (char *) cmd->argv[0]); |
| 2681 |
return PR_DECLINED(cmd); |
| 2682 |
} |
| 2683 |
|
| 2684 |
if (session.restart_pos > 0) { |
| 2685 |
pr_trace_msg(trace_channel, 12, |
| 2686 |
"REST %" PR_LU " sent before %s, declining to compute transfer digest", |
| 2687 |
(pr_off_t) session.restart_pos, (char *) cmd->argv[0]); |
| 2688 |
return PR_DECLINED(cmd); |
| 2689 |
} |
| 2690 |
|
| 2691 |
digest_cache_xfer_ctx = EVP_MD_CTX_create(); |
| 2692 |
if (EVP_DigestInit_ex(digest_cache_xfer_ctx, digest_hash_md, NULL) != 1) { |
| 2693 |
pr_trace_msg(trace_channel, 3, |
| 2694 |
"error preparing %s digest: %s", get_algo_name(digest_hash_algo, 0), |
| 2695 |
get_errors()); |
| 2696 |
EVP_MD_CTX_destroy(digest_cache_xfer_ctx); |
| 2697 |
digest_cache_xfer_ctx = NULL; |
| 2698 |
|
| 2699 |
} else { |
| 2700 |
pr_event_register(&digest_module, "core.data-read", digest_data_xfer_ev, |
| 2701 |
digest_cache_xfer_ctx); |
| 2702 |
pr_event_register(&digest_module, "mod_sftp.sftp.data-read", |
| 2703 |
digest_data_xfer_ev, digest_cache_xfer_ctx); |
| 2704 |
} |
| 2705 |
|
| 2706 |
return PR_DECLINED(cmd); |
| 2707 |
} |
| 2708 |
|
| 2709 |
MODRET digest_post_pass(cmd_rec *cmd) { |
| 2710 |
config_rec *c; |
| 2711 |
|
| 2712 |
if (digest_engine == FALSE) { |
| 2713 |
return PR_DECLINED(cmd); |
| 2714 |
} |
| 2715 |
|
| 2716 |
c = find_config(CURRENT_CONF, CONF_PARAM, "DigestEngine", FALSE); |
| 2717 |
if (c != NULL) { |
| 2718 |
digest_engine = *((int *) c->argv[0]); |
| 2719 |
} |
| 2720 |
|
| 2721 |
if (digest_engine == FALSE) { |
| 2722 |
return PR_DECLINED(cmd); |
| 2723 |
} |
| 2724 |
|
| 2725 |
c = find_config(CURRENT_CONF, CONF_PARAM, "DigestAlgorithms", FALSE); |
| 2726 |
if (c != NULL) { |
| 2727 |
digest_algos = *((unsigned long *) c->argv[0]); |
| 2728 |
} |
| 2729 |
|
| 2730 |
c = find_config(CURRENT_CONF, CONF_PARAM, "DigestCache", FALSE); |
| 2731 |
if (c != NULL) { |
| 2732 |
digest_caching = *((int *) c->argv[0]); |
| 2733 |
if (digest_caching == TRUE) { |
| 2734 |
digest_cache_max_size = *((unsigned int *) c->argv[1]); |
| 2735 |
digest_cache_max_age = *((unsigned int *) c->argv[2]); |
| 2736 |
} |
| 2737 |
} |
| 2738 |
|
| 2739 |
if (digest_caching == TRUE) { |
| 2740 |
int timerno; |
| 2741 |
|
| 2742 |
|
| 2743 |
|
| 2744 |
|
| 2745 |
timerno = pr_timer_add(DIGEST_CACHE_EXPIRY_INTVL, -1, &digest_module, |
| 2746 |
digest_cache_expiry_cb, "DigestCache expiry"); |
| 2747 |
if (timerno < 0) { |
| 2748 |
pr_log_debug(DEBUG5, MOD_DIGEST_VERSION |
| 2749 |
": error adding timer for DigestCache expiration: %s", strerror(errno)); |
| 2750 |
} |
| 2751 |
} |
| 2752 |
|
| 2753 |
return PR_DECLINED(cmd); |
| 2754 |
} |
| 2755 |
|
| 2756 |
MODRET digest_md5(cmd_rec *cmd) { |
| 2757 |
int xerrno = 0; |
| 2758 |
char *error_code = NULL, *orig_path = NULL, *path = NULL, *hex_digest = NULL; |
| 2759 |
struct stat st; |
| 2760 |
off_t len, start_pos, end_pos; |
| 2761 |
unsigned long algo = DIGEST_ALGO_MD5; |
| 2762 |
|
| 2763 |
if (digest_engine == FALSE) { |
| 2764 |
return PR_DECLINED(cmd); |
| 2765 |
} |
| 2766 |
|
| 2767 |
if (!(digest_algos & algo)) { |
| 2768 |
pr_log_debug(DEBUG9, MOD_DIGEST_VERSION |
| 2769 |
": unable to handle %s command: MD5 disabled by DigestAlgorithms", |
| 2770 |
(char *) cmd->argv[0]); |
| 2771 |
return PR_DECLINED(cmd); |
| 2772 |
} |
| 2773 |
|
| 2774 |
CHECK_CMD_MIN_ARGS(cmd, 2); |
| 2775 |
|
| 2776 |
orig_path = pr_fs_decode_path(cmd->tmp_pool, cmd->arg); |
| 2777 |
path = dir_realpath(cmd->tmp_pool, orig_path); |
| 2778 |
if (path == NULL) { |
| 2779 |
xerrno = errno; |
| 2780 |
|
| 2781 |
pr_response_add_err(R_550, "%s: %s", orig_path, strerror(xerrno)); |
| 2782 |
|
| 2783 |
pr_cmd_set_errno(cmd, xerrno); |
| 2784 |
errno = xerrno; |
| 2785 |
return PR_ERROR(cmd); |
| 2786 |
} |
| 2787 |
|
| 2788 |
if (blacklisted_file(path) == TRUE) { |
| 2789 |
pr_log_debug(DEBUG8, MOD_DIGEST_VERSION |
| 2790 |
": rejecting request to checksum blacklisted special file '%s'", path); |
| 2791 |
pr_response_add_err(R_550, "%s: %s", (char *) cmd->arg, strerror(EPERM)); |
| 2792 |
pr_cmd_set_errno(cmd, EPERM); |
| 2793 |
errno = EPERM; |
| 2794 |
return PR_ERROR(cmd); |
| 2795 |
} |
| 2796 |
|
| 2797 |
if (!dir_check(cmd->tmp_pool, cmd, cmd->group, path, NULL)) { |
| 2798 |
xerrno = EPERM; |
| 2799 |
|
| 2800 |
pr_log_debug(DEBUG8, MOD_DIGEST_VERSION |
| 2801 |
": %s denied by <Limit> configuration", (char *) cmd->argv[0]); |
| 2802 |
pr_response_add_err(R_550, "%s: %s", orig_path, strerror(xerrno)); |
| 2803 |
|
| 2804 |
pr_cmd_set_errno(cmd, xerrno); |
| 2805 |
errno = xerrno; |
| 2806 |
return PR_ERROR(cmd); |
| 2807 |
} |
| 2808 |
|
| 2809 |
pr_fs_clear_cache2(path); |
| 2810 |
if (pr_fsio_stat(path, &st) < 0) { |
| 2811 |
xerrno = errno; |
| 2812 |
|
| 2813 |
pr_log_debug(DEBUG8, MOD_DIGEST_VERSION |
| 2814 |
": error checking %s: %s", path, strerror(xerrno)); |
| 2815 |
pr_response_add_err(R_550, "%s: %s", orig_path, strerror(xerrno)); |
| 2816 |
|
| 2817 |
pr_cmd_set_errno(cmd, xerrno); |
| 2818 |
errno = xerrno; |
| 2819 |
return PR_ERROR(cmd); |
| 2820 |
} |
| 2821 |
|
| 2822 |
if (!S_ISREG(st.st_mode)) { |
| 2823 |
pr_log_debug(DEBUG5, MOD_DIGEST_VERSION |
| 2824 |
": unable to handle %s for non-file path '%s'", (char *) cmd->argv[0], |
| 2825 |
path); |
| 2826 |
pr_response_add_err(R_504, _("%s: Not a regular file"), orig_path); |
| 2827 |
return PR_ERROR(cmd); |
| 2828 |
} |
| 2829 |
|
| 2830 |
start_pos = 0; |
| 2831 |
end_pos = st.st_size; |
| 2832 |
len = end_pos - start_pos; |
| 2833 |
|
| 2834 |
if (check_digest_max_size(len) < 0) { |
| 2835 |
pr_response_add_err(R_550, "%s: %s", orig_path, strerror(EPERM)); |
| 2836 |
pr_cmd_set_errno(cmd, EPERM); |
| 2837 |
errno = EPERM; |
| 2838 |
return PR_ERROR(cmd); |
| 2839 |
} |
| 2840 |
|
| 2841 |
pr_trace_msg(trace_channel, 14, "%s: using %s algorithm on path '%s'", |
| 2842 |
(char *) cmd->argv[0], get_algo_name(algo, 0), path); |
| 2843 |
|
| 2844 |
hex_digest = get_digest(cmd, algo, path, st.st_mtime, start_pos, |
| 2845 |
len, PR_STR_FL_HEX_USE_UC); |
| 2846 |
xerrno = errno; |
| 2847 |
|
| 2848 |
if (hex_digest != NULL) { |
| 2849 |
|
| 2850 |
pr_response_add(R_251, "%s %s", orig_path, hex_digest); |
| 2851 |
return PR_HANDLED(cmd); |
| 2852 |
} |
| 2853 |
|
| 2854 |
switch (xerrno) { |
| 2855 |
case EISDIR: |
| 2856 |
|
| 2857 |
error_code = R_504; |
| 2858 |
break; |
| 2859 |
|
| 2860 |
default: |
| 2861 |
error_code = R_550; |
| 2862 |
break; |
| 2863 |
} |
| 2864 |
|
| 2865 |
|
| 2866 |
pr_response_add_err(error_code, "%s: %s", orig_path, strerror(xerrno)); |
| 2867 |
|
| 2868 |
pr_cmd_set_errno(cmd, xerrno); |
| 2869 |
errno = xerrno; |
| 2870 |
return PR_ERROR(cmd); |
| 2871 |
} |
| 2872 |
|
| 2873 |
MODRET digest_xcrc(cmd_rec *cmd) { |
| 2874 |
unsigned long algo = DIGEST_ALGO_CRC32; |
| 2875 |
|
| 2876 |
if (digest_engine == FALSE) { |
| 2877 |
return PR_DECLINED(cmd); |
| 2878 |
} |
| 2879 |
|
| 2880 |
if (!(digest_algos & algo)) { |
| 2881 |
pr_log_debug(DEBUG9, MOD_DIGEST_VERSION |
| 2882 |
": unable to handle %s command: CRC32 disabled by DigestAlgorithms", |
| 2883 |
(char *) cmd->argv[0]); |
| 2884 |
return PR_DECLINED(cmd); |
| 2885 |
} |
| 2886 |
|
| 2887 |
return digest_xcmd(cmd, algo); |
| 2888 |
} |
| 2889 |
|
| 2890 |
MODRET digest_xmd5(cmd_rec *cmd) { |
| 2891 |
unsigned long algo = DIGEST_ALGO_MD5; |
| 2892 |
|
| 2893 |
if (digest_engine == FALSE) { |
| 2894 |
return PR_DECLINED(cmd); |
| 2895 |
} |
| 2896 |
|
| 2897 |
if (!(digest_algos & algo)) { |
| 2898 |
pr_log_debug(DEBUG9, MOD_DIGEST_VERSION |
| 2899 |
": unable to handle %s command: MD5 disabled by DigestAlgorithms", |
| 2900 |
(char *) cmd->argv[0]); |
| 2901 |
return PR_DECLINED(cmd); |
| 2902 |
} |
| 2903 |
|
| 2904 |
return digest_xcmd(cmd, algo); |
| 2905 |
} |
| 2906 |
|
| 2907 |
MODRET digest_xsha1(cmd_rec *cmd) { |
| 2908 |
unsigned long algo = DIGEST_ALGO_SHA1; |
| 2909 |
|
| 2910 |
if (digest_engine == FALSE) { |
| 2911 |
return PR_DECLINED(cmd); |
| 2912 |
} |
| 2913 |
|
| 2914 |
if (!(digest_algos & algo)) { |
| 2915 |
pr_log_debug(DEBUG9, MOD_DIGEST_VERSION |
| 2916 |
": unable to handle %s command: SHA1 disabled by DigestAlgorithms", |
| 2917 |
(char *) cmd->argv[0]); |
| 2918 |
return PR_DECLINED(cmd); |
| 2919 |
} |
| 2920 |
|
| 2921 |
return digest_xcmd(cmd, algo); |
| 2922 |
} |
| 2923 |
|
| 2924 |
MODRET digest_xsha256(cmd_rec *cmd) { |
| 2925 |
unsigned long algo = DIGEST_ALGO_SHA256; |
| 2926 |
|
| 2927 |
if (digest_engine == FALSE) { |
| 2928 |
return PR_DECLINED(cmd); |
| 2929 |
} |
| 2930 |
|
| 2931 |
if (!(digest_algos & algo)) { |
| 2932 |
pr_log_debug(DEBUG9, MOD_DIGEST_VERSION |
| 2933 |
": unable to handle %s command: SHA256 disabled by DigestAlgorithms", |
| 2934 |
(char *) cmd->argv[0]); |
| 2935 |
return PR_DECLINED(cmd); |
| 2936 |
} |
| 2937 |
|
| 2938 |
return digest_xcmd(cmd, algo); |
| 2939 |
} |
| 2940 |
|
| 2941 |
MODRET digest_xsha512(cmd_rec *cmd) { |
| 2942 |
unsigned long algo = DIGEST_ALGO_SHA512; |
| 2943 |
|
| 2944 |
if (digest_engine == FALSE) { |
| 2945 |
return PR_DECLINED(cmd); |
| 2946 |
} |
| 2947 |
|
| 2948 |
if (!(digest_algos & algo)) { |
| 2949 |
pr_log_debug(DEBUG9, MOD_DIGEST_VERSION |
| 2950 |
": unable to handle %s command: SHA512 disabled by DigestAlgorithms", |
| 2951 |
(char *) cmd->argv[0]); |
| 2952 |
return PR_DECLINED(cmd); |
| 2953 |
} |
| 2954 |
|
| 2955 |
return digest_xcmd(cmd, algo); |
| 2956 |
} |
| 2957 |
|
| 2958 |
|
| 2959 |
|
| 2960 |
|
| 2961 |
static void digest_data_xfer_ev(const void *event_data, void *user_data) { |
| 2962 |
const pr_buffer_t *pbuf; |
| 2963 |
EVP_MD_CTX *md_ctx; |
| 2964 |
|
| 2965 |
md_ctx = user_data; |
| 2966 |
pbuf = event_data; |
| 2967 |
|
| 2968 |
if (EVP_DigestUpdate(md_ctx, pbuf->buf, pbuf->buflen) != 1) { |
| 2969 |
pr_trace_msg(trace_channel, 3, |
| 2970 |
"error updating %s digest: %s", get_algo_name(digest_hash_algo, 0), |
| 2971 |
get_errors()); |
| 2972 |
|
| 2973 |
} else { |
| 2974 |
pr_trace_msg(trace_channel, 19, |
| 2975 |
"updated %s digest with %lu bytes", get_algo_name(digest_hash_algo, 0), |
| 2976 |
(unsigned long) pbuf->buflen); |
| 2977 |
} |
| 2978 |
} |
| 2979 |
|
| 2980 |
#if defined(PR_SHARED_MODULE) |
| 2981 |
static void digest_mod_unload_ev(const void *event_data, void *user_data) { |
| 2982 |
if (strcmp((char *) event_data, "mod_digest.c") != 0) { |
| 2983 |
return; |
| 2984 |
} |
| 2985 |
|
| 2986 |
pr_event_unregister(&digest_module, NULL, NULL); |
| 2987 |
|
| 2988 |
# if OPENSSL_VERSION_NUMBER >= 0x40000000L && !defined(HAVE_LIBRESSL) |
| 2989 |
if (crc32_provider != NULL) { |
| 2990 |
OSSL_PROVIDER_unload(crc32_provider); |
| 2991 |
crc32_provider = NULL; |
| 2992 |
} |
| 2993 |
# endif |
| 2994 |
} |
| 2995 |
#endif |
| 2996 |
|
| 2997 |
static void digest_sess_reinit_ev(const void *event_data, void *user_data) { |
| 2998 |
int res; |
| 2999 |
|
| 3000 |
|
| 3001 |
|
| 3002 |
pr_event_unregister(&digest_module, "core.session-reinit", |
| 3003 |
digest_sess_reinit_ev); |
| 3004 |
|
| 3005 |
digest_engine = TRUE; |
| 3006 |
digest_caching = TRUE; |
| 3007 |
digest_cache_max_size = DIGEST_CACHE_DEFAULT_SIZE; |
| 3008 |
digest_cache_max_age = DIGEST_CACHE_DEFAULT_MAX_AGE; |
| 3009 |
digest_opts = DIGEST_DEFAULT_OPTS; |
| 3010 |
digest_algos = DIGEST_DEFAULT_ALGOS; |
| 3011 |
digest_hash_algo = DIGEST_ALGO_SHA1; |
| 3012 |
digest_hash_md = NULL; |
| 3013 |
|
| 3014 |
res = digest_sess_init(); |
| 3015 |
if (res < 0) { |
| 3016 |
pr_session_disconnect(&digest_module, |
| 3017 |
PR_SESS_DISCONNECT_SESSION_INIT_FAILED, NULL); |
| 3018 |
} |
| 3019 |
} |
| 3020 |
|
| 3021 |
static void digest_shutdown_ev(const void *event_data, void *user_data) { |
| 3022 |
#if defined(HAVE_OSSL_PROVIDER_LOAD_OPENSSL) |
| 3023 |
if (crc32_provider != NULL) { |
| 3024 |
OSSL_PROVIDER_unload(crc32_provider); |
| 3025 |
crc32_provider = NULL; |
| 3026 |
} |
| 3027 |
#endif |
| 3028 |
} |
| 3029 |
|
| 3030 |
|
| 3031 |
|
| 3032 |
|
| 3033 |
static int digest_init(void) { |
| 3034 |
digest_pool = make_sub_pool(permanent_pool); |
| 3035 |
pr_pool_tag(digest_pool, MOD_DIGEST_VERSION); |
| 3036 |
|
| 3037 |
#if defined(PR_SHARED_MODULE) |
| 3038 |
pr_event_register(&digest_module, "core.module-unload", digest_mod_unload_ev, |
| 3039 |
NULL); |
| 3040 |
#endif |
| 3041 |
pr_event_register(&digest_module, "core.shutdown", digest_shutdown_ev, NULL); |
| 3042 |
|
| 3043 |
#if OPENSSL_VERSION_NUMBER >= 0x40000000L && !defined(HAVE_LIBRESSL) |
| 3044 |
if (OSSL_PROVIDER_add_builtin(NULL, "crc32", crc32_provider_init) != 1) { |
| 3045 |
pr_log_debug(DEBUG1, MOD_DIGEST_VERSION |
| 3046 |
": error registering 'crc32' OpenSSL provider: %s", get_errors()); |
| 3047 |
|
| 3048 |
} else { |
| 3049 |
pr_trace_msg(trace_channel, 9, "%s", "registered 'crc32' OpenSSL provider"); |
| 3050 |
} |
| 3051 |
|
| 3052 |
|
| 3053 |
crc32_provider = OSSL_PROVIDER_load(NULL, "crc32"); |
| 3054 |
if (crc32_provider == NULL) { |
| 3055 |
pr_log_pri(PR_LOG_NOTICE, MOD_DIGEST_VERSION |
| 3056 |
": error loading 'crc32' OpenSSL provider: %s", get_errors()); |
| 3057 |
|
| 3058 |
} else { |
| 3059 |
pr_trace_msg(trace_channel, 9, "%s", "loaded 'crc32' OpenSSL provider"); |
| 3060 |
} |
| 3061 |
#endif |
| 3062 |
|
| 3063 |
return 0; |
| 3064 |
} |
| 3065 |
|
| 3066 |
static int digest_sess_init(void) { |
| 3067 |
config_rec *c; |
| 3068 |
|
| 3069 |
pr_event_register(&digest_module, "core.session-reinit", |
| 3070 |
digest_sess_reinit_ev, NULL); |
| 3071 |
|
| 3072 |
c = find_config(main_server->conf, CONF_PARAM, "DigestEngine", FALSE); |
| 3073 |
if (c != NULL) { |
| 3074 |
digest_engine = *((int *) c->argv[0]); |
| 3075 |
} |
| 3076 |
|
| 3077 |
if (digest_engine == FALSE) { |
| 3078 |
return 0; |
| 3079 |
} |
| 3080 |
|
| 3081 |
c = find_config(main_server->conf, CONF_PARAM, "DigestAlgorithms", FALSE); |
| 3082 |
if (c != NULL) { |
| 3083 |
digest_algos = *((unsigned long *) c->argv[0]); |
| 3084 |
} |
| 3085 |
|
| 3086 |
|
| 3087 |
|
| 3088 |
|
| 3089 |
|
| 3090 |
|
| 3091 |
|
| 3092 |
|
| 3093 |
|
| 3094 |
if (digest_algos & DIGEST_ALGO_SHA1) { |
| 3095 |
digest_hash_algo = DIGEST_ALGO_SHA1; |
| 3096 |
|
| 3097 |
} else if (digest_algos & DIGEST_ALGO_SHA256) { |
| 3098 |
digest_hash_algo = DIGEST_ALGO_SHA256; |
| 3099 |
|
| 3100 |
} else if (digest_algos & DIGEST_ALGO_SHA512) { |
| 3101 |
digest_hash_algo = DIGEST_ALGO_SHA512; |
| 3102 |
|
| 3103 |
} else if (digest_algos & DIGEST_ALGO_MD5) { |
| 3104 |
digest_hash_algo = DIGEST_ALGO_MD5; |
| 3105 |
|
| 3106 |
} else { |
| 3107 |
|
| 3108 |
digest_hash_algo = DIGEST_ALGO_CRC32; |
| 3109 |
} |
| 3110 |
|
| 3111 |
c = find_config(main_server->conf, CONF_PARAM, "DigestDefaultAlgorithm", |
| 3112 |
FALSE); |
| 3113 |
if (c != NULL) { |
| 3114 |
unsigned long algo; |
| 3115 |
|
| 3116 |
algo = *((unsigned long *) c->argv[0]); |
| 3117 |
|
| 3118 |
|
| 3119 |
|
| 3120 |
|
| 3121 |
|
| 3122 |
|
| 3123 |
if (digest_algos & algo) { |
| 3124 |
digest_hash_algo = algo; |
| 3125 |
|
| 3126 |
} else { |
| 3127 |
pr_log_debug(DEBUG5, MOD_DIGEST_VERSION |
| 3128 |
": DigestDefaultAlgorithm %s not allowed by DigestAlgorithms, ignoring", |
| 3129 |
get_algo_name(algo, 0)); |
| 3130 |
} |
| 3131 |
} |
| 3132 |
|
| 3133 |
digest_hash_md = get_algo_md(digest_hash_algo, &digest_hash_free_md); |
| 3134 |
|
| 3135 |
c = find_config(main_server->conf, CONF_PARAM, "DigestCache", FALSE); |
| 3136 |
if (c != NULL) { |
| 3137 |
digest_caching = *((int *) c->argv[0]); |
| 3138 |
if (digest_caching == TRUE) { |
| 3139 |
digest_cache_max_size = *((unsigned int *) c->argv[1]); |
| 3140 |
digest_cache_max_age = *((unsigned int *) c->argv[2]); |
| 3141 |
} |
| 3142 |
} |
| 3143 |
|
| 3144 |
c = find_config(main_server->conf, CONF_PARAM, "DigestOptions", FALSE); |
| 3145 |
while (c != NULL) { |
| 3146 |
unsigned long opts = 0; |
| 3147 |
|
| 3148 |
pr_signals_handle(); |
| 3149 |
|
| 3150 |
opts = *((unsigned long *) c->argv[0]); |
| 3151 |
digest_opts |= opts; |
| 3152 |
|
| 3153 |
c = find_config_next(c, c->next, CONF_PARAM, "DigestOptions", FALSE); |
| 3154 |
} |
| 3155 |
|
| 3156 |
if (digest_caching == TRUE) { |
| 3157 |
digest_crc32_tab = pr_table_alloc(digest_pool, 0); |
| 3158 |
digest_md5_tab = pr_table_alloc(digest_pool, 0); |
| 3159 |
digest_sha1_tab = pr_table_alloc(digest_pool, 0); |
| 3160 |
digest_sha256_tab = pr_table_alloc(digest_pool, 0); |
| 3161 |
digest_sha512_tab = pr_table_alloc(digest_pool, 0); |
| 3162 |
} |
| 3163 |
|
| 3164 |
digest_hash_feat_add(session.pool); |
| 3165 |
pr_help_add(C_HASH, _("<sp> pathname"), TRUE); |
| 3166 |
|
| 3167 |
digest_x_feat_add(session.pool); |
| 3168 |
digest_x_help_add(session.pool); |
| 3169 |
|
| 3170 |
return 0; |
| 3171 |
} |
| 3172 |
|
| 3173 |
|
| 3174 |
|
| 3175 |
|
| 3176 |
static cmdtable digest_cmdtab[] = { |
| 3177 |
{ CMD, C_HASH, G_READ, digest_hash, TRUE, FALSE, CL_READ|CL_INFO }, |
| 3178 |
{ CMD, C_OPTS"_HASH", G_NONE, digest_opts_hash,FALSE,FALSE }, |
| 3179 |
|
| 3180 |
{ CMD, C_MD5, G_READ, digest_md5, TRUE, FALSE, CL_READ|CL_INFO }, |
| 3181 |
{ CMD, C_XCRC, G_READ, digest_xcrc, TRUE, FALSE, CL_READ|CL_INFO }, |
| 3182 |
{ CMD, C_XMD5, G_READ, digest_xmd5, TRUE, FALSE, CL_READ|CL_INFO }, |
| 3183 |
{ CMD, C_XSHA, G_READ, digest_xsha1, TRUE, FALSE, CL_READ|CL_INFO }, |
| 3184 |
{ CMD, C_XSHA1, G_READ, digest_xsha1, TRUE, FALSE, CL_READ|CL_INFO }, |
| 3185 |
{ CMD, C_XSHA256, G_READ, digest_xsha256, TRUE, FALSE, CL_READ|CL_INFO }, |
| 3186 |
{ CMD, C_XSHA512, G_READ, digest_xsha512, TRUE, FALSE, CL_READ|CL_INFO }, |
| 3187 |
|
| 3188 |
{ POST_CMD, C_PASS, G_NONE, digest_post_pass, FALSE, FALSE }, |
| 3189 |
|
| 3190 |
|
| 3191 |
|
| 3192 |
|
| 3193 |
|
| 3194 |
{ PRE_CMD, C_APPE, G_NONE, digest_pre_appe, TRUE, FALSE }, |
| 3195 |
{ PRE_CMD, C_RETR, G_NONE, digest_pre_retr, TRUE, FALSE }, |
| 3196 |
{ PRE_CMD, C_STOR, G_NONE, digest_pre_stor, TRUE, FALSE }, |
| 3197 |
{ LOG_CMD, C_ANY, G_NONE, digest_log, FALSE, FALSE }, |
| 3198 |
{ LOG_CMD_ERR,C_ANY, G_NONE, digest_log_err, FALSE, FALSE }, |
| 3199 |
|
| 3200 |
{ 0, NULL } |
| 3201 |
}; |
| 3202 |
|
| 3203 |
static conftable digest_conftab[] = { |
| 3204 |
{ "DigestAlgorithms", set_digestalgorithms, NULL }, |
| 3205 |
{ "DigestCache", set_digestcache, NULL }, |
| 3206 |
{ "DigestDefaultAlgorithm", set_digestdefaultalgo, NULL }, |
| 3207 |
{ "DigestEnable", set_digestenable, NULL }, |
| 3208 |
{ "DigestEngine", set_digestengine, NULL }, |
| 3209 |
{ "DigestMaxSize", set_digestmaxsize, NULL }, |
| 3210 |
{ "DigestOptions", set_digestoptions, NULL }, |
| 3211 |
|
| 3212 |
{ NULL } |
| 3213 |
}; |
| 3214 |
|
| 3215 |
module digest_module = { |
| 3216 |
NULL, NULL, |
| 3217 |
|
| 3218 |
|
| 3219 |
0x20, |
| 3220 |
|
| 3221 |
|
| 3222 |
"digest", |
| 3223 |
|
| 3224 |
|
| 3225 |
digest_conftab, |
| 3226 |
|
| 3227 |
|
| 3228 |
digest_cmdtab, |
| 3229 |
|
| 3230 |
|
| 3231 |
NULL, |
| 3232 |
|
| 3233 |
|
| 3234 |
digest_init, |
| 3235 |
|
| 3236 |
|
| 3237 |
digest_sess_init, |
| 3238 |
|
| 3239 |
|
| 3240 |
MOD_DIGEST_VERSION |
| 3241 |
}; |