From: Jeffrey Altman Date: Fri, 7 Oct 2011 22:45:08 +0000 (-0400) Subject: Do not call krb5_get_error_message with NULL context X-Git-Tag: upstream/1.6.1.pre1^2~197 X-Git-Url: https://git.michaelhowe.org/gitweb/?a=commitdiff_plain;h=855d3fbcde486640ba60fcce283cebb4bbdafe98;p=packages%2Fo%2Fopenafs.git Do not call krb5_get_error_message with NULL context MIT's krb5_get_error_message() ignores the context and can be called with a NULL context. Heimdal's version does not. Derived from http://gerrit.openafs.org/5508 aka commit 9dd9cfa0e1536e0e75628c84605b3d5b8486d69c The 1.6 branch is sufficiently different from master. Change-Id: Ie325705a78b155a04a6a9d44800037b72f5b045c Reviewed-on: http://gerrit.openafs.org/5569 Tested-by: BuildBot Reviewed-by: Derrick Brashear --- diff --git a/src/WINNT/aklog/aklog.c b/src/WINNT/aklog/aklog.c index cb9bb414f..d9b4fc10d 100644 --- a/src/WINNT/aklog/aklog.c +++ b/src/WINNT/aklog/aklog.c @@ -197,6 +197,8 @@ static int use524 = FALSE; /* use krb524? */ static krb5_context context = 0; static krb5_ccache _krb425_ccache = 0; +static krb5_error_code (KRB5_CALLCONV *pkrb5_init_context)(krb5_context *context) = NULL; +static krb5_error_code (KRB5_CALLCONV *pkrb5_free_context)(krb5_context context) = NULL; static char * (KRB5_CALLCONV *pkrb5_get_error_message)(krb5_context context, krb5_error_code code)=NULL; static void (KRB5_CALLCONV *pkrb5_free_error_message)(krb5_context context, char *s) = NULL; @@ -217,6 +219,8 @@ void akexit(int exit_code) void redirect_errors(const char *who, afs_int32 code, const char *fmt, va_list ap) { + krb5_context context; + if (who) { fputs(who, stderr); fputs(": ", stderr); @@ -225,16 +229,22 @@ redirect_errors(const char *who, afs_int32 code, const char *fmt, va_list ap) int freestr = 0; char *str = (char *)afs_error_message(code); if (strncmp(str, "unknown", strlen(str)) == 0) { - if (pkrb5_get_error_message) { + if (pkrb5_init_context && + pkrb5_get_error_message && + !pkrb5_init_context(&context)) + { str = pkrb5_get_error_message(NULL, code); freestr = 1; - } else + } else { str = (char *)error_message(code); + } } fputs(str, stderr); fputs(" ", stderr); - if (freestr) + if (freestr) { pkrb5_free_error_message(NULL, str); + pkrb5_free_context(context); + } } if (fmt) { vfprintf(stderr, fmt, ap); @@ -1416,6 +1426,8 @@ load_krb5_error_message_funcs(void) { HINSTANCE h = LoadLibrary(KRB5LIB); if (h) { + (FARPROC)pkrb5_init_context = GetProcAddress(h, "krb5_init_context"); + (FARPROC)pkrb5_free_context = GetProcAddress(h, "krb5_free_context"); (FARPROC)pkrb5_get_error_message = GetProcAddress(h, "krb5_get_error_message"); (FARPROC)pkrb5_free_error_message = GetProcAddress(h, "krb5_free_error_message"); } diff --git a/src/WINNT/netidmgr_plugin/krb5common.c b/src/WINNT/netidmgr_plugin/krb5common.c index 3c294e453..8212c802f 100644 --- a/src/WINNT/netidmgr_plugin/krb5common.c +++ b/src/WINNT/netidmgr_plugin/krb5common.c @@ -53,8 +53,8 @@ khm_krb5_error(krb5_error_code rc, LPCSTR FailedFunctionName, const char *errText; int krb5Error = ((int)(rc & 255)); - if (pkrb5_get_error_message) - errText = pkrb5_get_error_message(rc); + if (*ctx && pkrb5_get_error_message) + errText = pkrb5_get_error_message(*ctx, rc); else errText = perror_message(rc); _snprintf(message, sizeof(message), @@ -62,8 +62,8 @@ khm_krb5_error(krb5_error_code rc, LPCSTR FailedFunctionName, errText, krb5Error, FailedFunctionName); - if (pkrb5_free_error_message) - pkrb5_free_error_message(errText); + if (*ctx && pkrb5_free_error_message) + pkrb5_free_error_message(*ctx, errText); MessageBoxA(NULL, message, "Kerberos Five", MB_OK | MB_ICONERROR | MB_TASKMODAL | diff --git a/src/util/krb5_nt.c b/src/util/krb5_nt.c index 61d6b39be..093c324be 100644 --- a/src/util/krb5_nt.c +++ b/src/util/krb5_nt.c @@ -30,6 +30,8 @@ #include #include "krb5_nt.h" +static krb5_error_code (KRB5_CALLCONV *pkrb5_init_context)(krb5_context *context) = NULL; +static krb5_error_code (KRB5_CALLCONV *pkrb5_free_context)(krb5_context context) = NULL; static char * (KRB5_CALLCONV *pkrb5_get_error_message)(krb5_context context, krb5_error_code code) = NULL; static void (KRB5_CALLCONV *pkrb5_free_error_message)(krb5_context context, char *s) = NULL; @@ -57,6 +59,8 @@ initialize_krb5(void) */ HINSTANCE h = LoadLibrary(KRB5LIB); if (h) { + (FARPROC)pkrb5_init_context = GetProcAddress(h, "krb5_init_context"); + (FARPROC)pkrb5_free_context = GetProcAddress(h, "krb5_free_context"); (FARPROC)pkrb5_get_error_message = GetProcAddress(h, "krb5_get_error_message"); (FARPROC)pkrb5_free_error_message = GetProcAddress(h, "krb5_free_error_message"); } @@ -66,15 +70,28 @@ const char * fetch_krb5_error_message(krb5_context context, krb5_error_code code) { static char errorText[1024]; + char *msg = NULL; + int free_context = 0; - if (pkrb5_get_error_message) { - char *msg = pkrb5_get_error_message(context, code); + if (pkrb5_init_context && pkrb5_get_error_message) { + + if (context == NULL) { + if (krb5_init_context(&context) != 0) + goto done; + free_context = 1; + } + + msg = pkrb5_get_error_message(context, code); if (msg) { strncpy(errorText, msg, sizeof(errorText)); errorText[sizeof(errorText)-1]='\0'; pkrb5_free_error_message(context, msg); - return errorText; + msg = errorText; } + + if (free_context) + pkrb5_free_context(context); } - return NULL; + done: + return msg; }