From e4a885074bdb75bc545e3dbf80190c03e4da09ec Mon Sep 17 00:00:00 2001 From: "Roland C. Dowdeswell" Date: Tue, 19 May 2026 00:59:14 +0100 Subject: [PATCH] krb5: make KRB5_TRACE default to tracing not just debugging --- lib/base/log.c | 37 +++++++++++++----- lib/base/test_base.c | 78 +++++++++++++++++++++++++++++++++++++ lib/base/version-script.map | 1 + lib/krb5/context.c | 2 +- 4 files changed, 108 insertions(+), 10 deletions(-) diff --git a/lib/base/log.c b/lib/base/log.c index c3170e140..7487391f4 100644 --- a/lib/base/log.c +++ b/lib/base/log.c @@ -355,11 +355,12 @@ open_file(heim_context context, heim_log_facility *fac, int min, int max, return ret; } -heim_error_code -heim_addlog_dest(heim_context context, heim_log_facility *f, const char *orig) +static heim_error_code +addlog_dest(heim_context context, heim_log_facility *f, + const char *orig, int default_min, int default_max) { heim_error_code ret = 0; - int min = 0, max = 3, n; + int min = default_min, max = default_max, n; char c; const char *p = orig; #ifdef _WIN32 @@ -393,8 +394,6 @@ heim_addlog_dest(heim_context context, heim_log_facility *f, const char *orig) return HEIM_ERR_LOG_PARSE; } p++; - } else { - max = 5; } if (strcmp(p, "STDERR") == 0) { ret = open_file(context, f, min, max, NULL, "a", stderr, @@ -445,6 +444,12 @@ heim_addlog_dest(heim_context context, heim_log_facility *f, const char *orig) return ret; } +heim_error_code +heim_addlog_dest(heim_context context, heim_log_facility *f, const char *orig) +{ + return addlog_dest(context, f, orig, 0, 5); +} + heim_error_code heim_openlog(heim_context context, const char *program, @@ -648,9 +653,9 @@ heim_add_warn_dest(heim_context context, const char *program, return 0; } -heim_error_code -heim_add_debug_dest(heim_context context, const char *program, - const char *log_spec) +static heim_error_code +add_debug_dest(heim_context context, const char *program, + const char *log_spec, int default_min, int default_max) { heim_log_facility *fac; heim_error_code ret; @@ -662,12 +667,26 @@ heim_add_debug_dest(heim_context context, const char *program, heim_set_debug_dest(context, fac); } - ret = heim_addlog_dest(context, fac, log_spec); + ret = addlog_dest(context, fac, log_spec, default_min, default_max); if (ret) return ret; return 0; } +heim_error_code +heim_add_debug_dest(heim_context context, const char *program, + const char *log_spec) +{ + return add_debug_dest(context, program, log_spec, 0, 5); +} + +heim_error_code +heim_add_trace_dest(heim_context context, const char *program, + const char *log_spec) +{ + return add_debug_dest(context, program, log_spec, 0, -1); +} + struct heim_audit_kv_tuple { heim_string_t key; heim_object_t value; diff --git a/lib/base/test_base.c b/lib/base/test_base.c index cc875d7f8..fd4133b41 100644 --- a/lib/base/test_base.c +++ b/lib/base/test_base.c @@ -142,6 +142,83 @@ test_rwlock(void) return 0; } +static int +file_contains(const char *filename, const char *needle) +{ + char buf[1024]; + FILE *f; + int found = 0; + + f = fopen(filename, "r"); + if (f == NULL) + return 0; + + while (fgets(buf, sizeof(buf), f) != NULL) { + if (strstr(buf, needle) != NULL) { + found = 1; + break; + } + } + fclose(f); + return found; +} + +static int +test_log_dest(void) +{ + const char *normal = "test-log-dest.log"; + const char *trace = "test-trace-dest.log"; + const char *explicit = "test-trace-explicit-dest.log"; + heim_log_facility *fac = NULL; + heim_context context; + int ret; + + unlink(normal); + unlink(trace); + unlink(explicit); + + context = heim_context_init(); + heim_assert(context != NULL, "heim_context_init failed"); + + ret = heim_initlog(context, "test_base", &fac); + heim_assert(ret == 0, "heim_initlog failed"); + ret = heim_addlog_dest(context, fac, normal); + heim_assert(ret == 0, "heim_addlog_dest failed"); + + heim_log(context, fac, 10, "normal level 10"); + heim_log(context, fac, 5, "normal level 5"); + heim_closelog(context, fac); + + heim_assert(!file_contains(normal, "normal level 10"), + "normal log destination traced level 10 by default"); + heim_assert(file_contains(normal, "normal level 5"), + "normal log destination did not log level 5 by default"); + + ret = heim_add_trace_dest(context, "test_base", trace); + heim_assert(ret == 0, "heim_add_trace_dest failed"); + ret = heim_add_trace_dest(context, "test_base", + "5/FILE:test-trace-explicit-dest.log"); + heim_assert(ret == 0, "heim_add_trace_dest with explicit level failed"); + + heim_debug(context, 10, "trace level 10"); + heim_debug(context, 5, "trace level 5"); + + heim_assert(file_contains(trace, "trace level 10"), + "trace log destination did not trace level 10 by default"); + heim_assert(!file_contains(explicit, "trace level 10"), + "explicit trace destination ignored its level filter"); + heim_assert(file_contains(explicit, "trace level 5"), + "explicit trace destination did not log matching level"); + + heim_context_free(&context); + + unlink(normal); + unlink(trace); + unlink(explicit); + + return 0; +} + static int test_dict(void) { @@ -1365,6 +1442,7 @@ main(int argc, char **argv) res |= test_memory(); res |= test_mutex(); res |= test_rwlock(); + res |= test_log_dest(); res |= test_dict(); res |= test_auto_release(); res |= test_string(); diff --git a/lib/base/version-script.map b/lib/base/version-script.map index 1ba889929..0ab0c4040 100644 --- a/lib/base/version-script.map +++ b/lib/base/version-script.map @@ -10,6 +10,7 @@ HEIMDAL_BASE_1.0 { heim_abort; heim_abortv; heim_add_debug_dest; + heim_add_trace_dest; heim_add_et_list; heim_addlog_dest; heim_addlog_func; diff --git a/lib/krb5/context.c b/lib/krb5/context.c index dc03283b4..847cd3602 100644 --- a/lib/krb5/context.c +++ b/lib/krb5/context.c @@ -489,7 +489,7 @@ init_context_from_config_file(krb5_context context) tmp = secure_getenv("KRB5_TRACE"); if (tmp) - heim_add_debug_dest(context->hcontext, "libkrb5", tmp); + heim_add_trace_dest(context->hcontext, "libkrb5", tmp); s = krb5_config_get_strings(context, NULL, "logging", "krb5", NULL); if (s) { char **p;