From 3914c4b1bd9a94dc9998e7e1a7105a9835da84e0 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Sat, 9 Aug 2014 16:17:48 -0400 Subject: Add debug helpers to be used to trace gss-ntlmssp If the GSSNTLMSSP_DEBUG environment variable is set to a file that can be opened for writing, then trace information will be written to that file whenever DEBUG macros are called in the code. --- Makefile.am | 2 ++ src/debug.c | 33 +++++++++++++++++++++++++++++++++ src/debug.h | 37 +++++++++++++++++++++++++++++++++++++ src/gss_ntlmssp.h | 1 + tests/ntlmssptest.c | 3 +++ 5 files changed, 76 insertions(+) create mode 100644 src/debug.c create mode 100644 src/debug.h diff --git a/Makefile.am b/Makefile.am index 047f0d7..5df5d89 100644 --- a/Makefile.am +++ b/Makefile.am @@ -72,6 +72,7 @@ GN_MECHGLUE_OBJ = \ src/crypto.c \ src/ntlm_crypto.c \ src/ntlm.c \ + src/debug.c \ src/gss_err.c \ src/gss_spi.c \ src/gss_names.c \ @@ -88,6 +89,7 @@ dist_noinst_HEADERS = \ src/crypto.h \ src/ntlm_common.h \ src/ntlm.h \ + src/debug.h \ src/gss_ntlmssp.h \ src/gss_ntlmssp_winbind.h diff --git a/src/debug.c b/src/debug.c new file mode 100644 index 0000000..e6185b4 --- /dev/null +++ b/src/debug.c @@ -0,0 +1,33 @@ +/* Copyright (C) 2014 GSS-NTLMSSP contributors, see COPYING for license */ + +#define _GNU_SOURCE +#include +#include +#include +#include + +bool gssntlm_debug_initialized = false; +bool gssntlm_debug_enabled = false; +static FILE *debug_fd = NULL; + +void gssntlm_debug_init(void) +{ + char *env; + + env = secure_getenv("GSSNTLMSSP_DEBUG"); + if (env) { + debug_fd = fopen(env, "a"); + if (debug_fd) gssntlm_debug_enabled = true; + } + gssntlm_debug_initialized = true; +} + +void gssntlm_debug_printf(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(debug_fd, fmt, ap); + va_end(ap); + fflush(debug_fd); +} diff --git a/src/debug.h b/src/debug.h new file mode 100644 index 0000000..81ed247 --- /dev/null +++ b/src/debug.h @@ -0,0 +1,37 @@ +/* Copyright (C) 2014 GSS-NTLMSSP contributors, see COPYING for license */ + +#ifndef _GSSNTLMSSP_DEBUG_H_ +#define _GSSNTLMSSP_DEBUG_H_ + +#include +#include + +extern bool gssntlm_debug_initialized; +extern bool gssntlm_debug_enabled; + +void gssntlm_debug_init(void); +void gssntlm_debug_printf(const char *fmt, ...); + +#define unlikely(x) __builtin_expect(!!(x), 0) + +static inline int debug_gss_errors(const char *function, + const char *file, + unsigned int line, + unsigned int maj, + unsigned int min) +{ + if (unlikely(gssntlm_debug_initialized == false)) { + gssntlm_debug_init(); + } + if (unlikely(gssntlm_debug_enabled == true)) { + gssntlm_debug_printf("[%ld] %s: %s() @ %s:%u [%u:%u]\n", + (long)time(NULL), + GSS_ERROR(maj) ? "ERROR" : "ALLOK", + function, file, line, maj, min); + } + return 0; +} +#define DEBUG_GSS_ERRORS(maj, min) \ + debug_gss_errors(__FUNCTION__, __FILE__, __LINE__, maj, min) + +#endif /* _GSSNTLMSSP_DEBUG_H_ */ diff --git a/src/gss_ntlmssp.h b/src/gss_ntlmssp.h index 68dde38..33b3279 100644 --- a/src/gss_ntlmssp.h +++ b/src/gss_ntlmssp.h @@ -21,6 +21,7 @@ #include "ntlm.h" #include "crypto.h" #include "gssapi_ntlmssp.h" +#include "debug.h" #define MAX_CHALRESP_LIFETIME 36 * 60 * 60 /* 36 hours in seconds */ diff --git a/tests/ntlmssptest.c b/tests/ntlmssptest.c index 8a61a2f..ddc9d75 100644 --- a/tests/ntlmssptest.c +++ b/tests/ntlmssptest.c @@ -1975,6 +1975,9 @@ int main(int argc, const char *argv[]) struct ntlm_ctx *ctx; int ret; + /* enable trace debugging by dfault in tests */ + setenv("GSSNTLMSSP_DEBUG", "tests-trace.log", 0); + ret = ntlm_init_ctx(&ctx); if (ret) goto done; -- cgit