From 3fb382036d67d67236b4205505c9bdbf9c1535bd Mon Sep 17 00:00:00 2001 From: Sam Hartman Date: Wed, 28 Sep 2011 20:54:41 +0000 Subject: [PATCH] miscellaneous leash32.dll corrections do not build leash32.dll with MessageBox() calls. Doing so produces a library that on error must display a dialog box to the end user. If this library is called from a service (via gssapi32.dll) the service will block forever while awaiting the dialog box to clear. LeashKRB5GetTickets() treats krbv5Context as an in/out variable. If the caller does not provide a krb5_context, one will be allocated. It is up to the caller to ensure that the context is eventually freed. A context can be returned even if the function returns an error. Make sure that 'ctx' and 'cache' are properly initialized so that it is possible to tell the difference. Do not free the context if it was locally allocated. In acquire_tkt_no_princ() do not set the KRB5CCNAME environment variable if 'ccname' is an empty string. Combination of 2 patches by Jeffrey Altman From: Alexey Melnikov git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@25248 dc483132-0cff-0310-8789-dd5450dbe970 --- src/windows/leashdll/AFSroutines.c | 4 +++ src/windows/leashdll/krb5routines.c | 55 +++++++++++++++++------------ src/windows/leashdll/lshfunc.c | 11 ++++-- src/windows/leashdll/timesync.c | 5 ++- src/windows/leashdll/winerr.c | 4 +++ 5 files changed, 53 insertions(+), 26 deletions(-) diff --git a/src/windows/leashdll/AFSroutines.c b/src/windows/leashdll/AFSroutines.c index cdf751d50..fec099d4e 100644 --- a/src/windows/leashdll/AFSroutines.c +++ b/src/windows/leashdll/AFSroutines.c @@ -197,7 +197,9 @@ not_an_API_LeashAFSGetToken( list->theTicket = (char*) calloc(1, sizeof(Buffer)); if (!list->theTicket) { +#ifdef USE_MESSAGE_BOX MessageBox(NULL, "Memory Error", "Error", MB_OK); +#endif /* USE_MESSAGE_BOX */ return ENOMEM; } @@ -742,6 +744,7 @@ Leash_afs_error(LONG rc, LPCSTR FailedFunctionName) #ifdef NO_AFS return; #else +#ifdef USE_MESSAGE_BOX char message[256]; const char *errText; @@ -769,6 +772,7 @@ Leash_afs_error(LONG rc, LPCSTR FailedFunctionName) sprintf(message, "%s\n(%s failed)", errText, FailedFunctionName); MessageBox(NULL, message, "AFS", MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND); +#endif /* USE_MESSAGE_BOX */ return; #endif diff --git a/src/windows/leashdll/krb5routines.c b/src/windows/leashdll/krb5routines.c index 0bb30ca15..09d564c0a 100644 --- a/src/windows/leashdll/krb5routines.c +++ b/src/windows/leashdll/krb5routines.c @@ -315,6 +315,13 @@ one_addr(krb5_address *a) return(retstr); } +/* + * LeashKRB5GetTickets() treats krbv5Context as an in/out variable. + * If the caller does not provide a krb5_context, one will be allocated. + * It is up to the caller to ensure that the context is eventually freed. + * A context can be returned even if the function returns an error. + */ + long not_an_API_LeashKRB5GetTickets( TICKETINFO * ticketinfo, @@ -325,8 +332,8 @@ not_an_API_LeashKRB5GetTickets( #ifdef NO_KRB5 return(0); #else - krb5_context ctx; - krb5_ccache cache; + krb5_context ctx = 0; + krb5_ccache cache = 0; krb5_error_code code; krb5_principal KRBv5Principal; krb5_flags flags = 0; @@ -339,7 +346,6 @@ not_an_API_LeashKRB5GetTickets( int StartDay; int EndDay; int RenewDay; - int freeContextFlag; char StartTimeString[256]; char EndTimeString[256]; char RenewTimeString[256]; @@ -356,11 +362,8 @@ not_an_API_LeashKRB5GetTickets( char *sPtr; char *ticketFlag; LPCSTR functionName; + TicketList *list = NULL; - TicketList* list = NULL; - - ctx = NULL; - cache = NULL; if ( ticketinfo ) { ticketinfo->btickets = NO_TICKETS; ticketinfo->principal[0] = '\0'; @@ -379,7 +382,7 @@ not_an_API_LeashKRB5GetTickets( if (code != KRB5_FCC_NOFILE && code != KRB5_CC_NOTFOUND) Leash_krb5_error(code, "krb5_cc_set_flags()", 0, &ctx, &cache); - else if ((code == KRB5_FCC_NOFILE || code == KRB5_CC_NOTFOUND) && ctx != NULL) + else if ((code == KRB5_FCC_NOFILE || code == KRB5_CC_NOTFOUND)) { if (cache != NULL) pkrb5_cc_close(ctx, cache); @@ -391,7 +394,7 @@ not_an_API_LeashKRB5GetTickets( { if (code != KRB5_FCC_NOFILE && code != KRB5_CC_NOTFOUND) Leash_krb5_error(code, "krb5_cc_get_principal()", 0, &ctx, &cache); - else if ((code == KRB5_FCC_NOFILE || code == KRB5_CC_NOTFOUND) && ctx != NULL) + else if ((code == KRB5_FCC_NOFILE || code == KRB5_CC_NOTFOUND)) { if (cache != NULL) pkrb5_cc_close(ctx, cache); @@ -440,7 +443,6 @@ not_an_API_LeashKRB5GetTickets( if ((code = pkrb5_cc_start_seq_get(ctx, cache, &KRBv5Cursor))) { functionName = "krb5_cc_start_seq_get()"; - freeContextFlag = 1; goto on_error; } @@ -583,7 +585,9 @@ not_an_API_LeashKRB5GetTickets( list->theTicket = (char*) calloc(1, strlen(Buffer)+1); if (!list->theTicket) { +#ifdef USE_MESSAGE_BOX MessageBox(NULL, "Memory Error", "Error", MB_OK); +#endif /* USE_MESSAGE_BOX */ return ENOMEM; } strcpy(list->theTicket, Buffer); @@ -596,7 +600,9 @@ not_an_API_LeashKRB5GetTickets( list->tktEncType = (char*) calloc(1, strlen(Buffer)+1); if (!list->tktEncType) { +#ifdef USE_MESSAGE_BOX MessageBox(NULL, "Memory Error", "Error", MB_OK); +#endif /* USE_MESSAGE_BOX */ return ENOMEM; } strcpy(list->tktEncType, Buffer); @@ -611,7 +617,9 @@ not_an_API_LeashKRB5GetTickets( list->keyEncType = (char*) calloc(1, strlen(Buffer)+1); if (!list->keyEncType) { +#ifdef USE_MESSAGE_BOX MessageBox(NULL, "Memory Error", "Error", MB_OK); +#endif /* USE_MESSAGE_BOX */ return ENOMEM; } strcpy(list->keyEncType, Buffer); @@ -622,7 +630,9 @@ not_an_API_LeashKRB5GetTickets( n++; list->addrList = calloc(1, n * sizeof(char *)); if (!list->addrList) { +#ifdef USE_MESSAGE_BOX MessageBox(NULL, "Memory Error", "Error", MB_OK); +#endif /* USE_MESSAGE_BOX */ return ENOMEM; } list->addrCount = n; @@ -631,7 +641,9 @@ not_an_API_LeashKRB5GetTickets( list->addrList[n] = (char*) calloc(1, strlen(Buffer)+1); if (!list->addrList[n]) { +#ifdef USE_MESSAGE_BOX MessageBox(NULL, "Memory Error", "Error", MB_OK); +#endif /* USE_MESSAGE_BOX */ return ENOMEM; } strcpy(list->addrList[n], Buffer); @@ -673,7 +685,6 @@ not_an_API_LeashKRB5GetTickets( if ((code = pkrb5_cc_end_seq_get(ctx, cache, &KRBv5Cursor))) { functionName = "krb5_cc_end_seq_get()"; - freeContextFlag = 1; goto on_error; } @@ -684,14 +695,12 @@ not_an_API_LeashKRB5GetTickets( if ((code = pkrb5_cc_set_flags(ctx, cache, flags))) { functionName = "krb5_cc_set_flags()"; - freeContextFlag = 1; goto on_error; } } else { functionName = "krb5_cc_next_cred()"; - freeContextFlag = 1; goto on_error; } @@ -704,8 +713,7 @@ not_an_API_LeashKRB5GetTickets( return(code); on_error: - - Leash_krb5_error(code, functionName, freeContextFlag, &(*krbv5Context), &cache); + Leash_krb5_error(code, functionName, 0, &(*krbv5Context), &cache); return(code); #endif //!NO_KER5 } @@ -1011,25 +1019,26 @@ int Leash_krb5_initialize(krb5_context *ctx, krb5_ccache *cache) return(0); #else - LPCSTR functionName; - int freeContextFlag; + LPCSTR functionName = NULL; + int freeContextFlag = 0; krb5_error_code rc; krb5_flags flags; if (pkrb5_init_context == NULL) return 1; - if (*ctx == 0 && (rc = (*pkrb5_init_context)(ctx))) + if (*ctx == 0) { + if (rc = (*pkrb5_init_context)(ctx)) { functionName = "krb5_init_context()"; - freeContextFlag = 0; goto on_error; } + freeContextFlag = 1; + } if (*cache == 0 && (rc = pkrb5_cc_default(*ctx, cache))) { functionName = "krb5_cc_default()"; - freeContextFlag = 1; goto on_error; } #ifdef KRB5_TC_NOTICKET @@ -1066,6 +1075,7 @@ Leash_krb5_error(krb5_error_code rc, LPCSTR FailedFunctionName, #ifdef NO_KRB5 return 0; #else +#ifdef USE_MESSAGE_BOX char message[256]; const char *errText; int krb5Error = ((int)(rc & 255)); @@ -1090,8 +1100,8 @@ Leash_krb5_error(krb5_error_code rc, LPCSTR FailedFunctionName, MessageBox(NULL, message, "Kerberos Five", MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND); - if (FreeContextFlag == 1) - { +#endif /* USE_MESSAGE_BOX */ + if (*ctx != NULL) { if (*cache != NULL) { @@ -1099,6 +1109,7 @@ Leash_krb5_error(krb5_error_code rc, LPCSTR FailedFunctionName, *cache = NULL; } + if (FreeContextFlag) { pkrb5_free_context(*ctx); *ctx = NULL; } diff --git a/src/windows/leashdll/lshfunc.c b/src/windows/leashdll/lshfunc.c index c2b6d9216..26a0261b2 100644 --- a/src/windows/leashdll/lshfunc.c +++ b/src/windows/leashdll/lshfunc.c @@ -135,10 +135,11 @@ leash_error_message( p += n; size -= n; } +#ifdef USE_MESSAGE_BOX if ( displayMB ) MessageBox(NULL, message, "Leash", MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND); - +#endif /* USE_MESSAGE_BOX */ if (rc5) return rc5; if (rc4) return rc4; if (rcL) return rcL; @@ -1270,7 +1271,9 @@ not_an_API_LeashKRB4GetTickets(TICKETINFO FAR* ticketinfo, list->theTicket = (char*) calloc(1, sizeof(buf)); if (!list->theTicket) { +#ifdef USE_MESSAGE_BOX MessageBox(NULL, "Memory Error", "Error", MB_OK); +#endif /* USE_MESSAGE_BOX */ return ENOMEM; } @@ -1306,6 +1309,7 @@ cleanup: ticketinfo->btickets = newtickets; +#ifdef USE_MESSAGE_BOX if (k_errno) { CHAR message[256]; @@ -1321,6 +1325,7 @@ cleanup: MessageBox(NULL, message, "Kerberos Four", MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND); } +#endif /* USE_MESSAGE_BOX */ return k_errno; #endif } @@ -3562,7 +3567,7 @@ acquire_tkt_no_princ(krb5_context context, char * ccname, int cclen) ccname[cclen-1] = '\0'; } - if ( ccname && strcmp(ccachename,ccname) ) { + if ( ccname && ccname[0] && strcmp(ccachename,ccname) ) { SetEnvironmentVariable("KRB5CCNAME",ccname); } @@ -3659,7 +3664,7 @@ acquire_tkt_for_princ(krb5_context context, krb5_principal desiredPrincipal, } } - if ( ccname && strcmp(ccachename,ccname) ) { + if ( ccname && ccname[0] && strcmp(ccachename,ccname) ) { SetEnvironmentVariable("KRB5CCNAME",ccname); } diff --git a/src/windows/leashdll/timesync.c b/src/windows/leashdll/timesync.c index 1c0bbbed2..32ab5e628 100644 --- a/src/windows/leashdll/timesync.c +++ b/src/windows/leashdll/timesync.c @@ -159,7 +159,6 @@ not_an_API_LeashGetTimeServerName( LONG Leash_timesync(int MessageP) { char tmpstr[2048]; - char tmpstr1[2048]; char hostname[128]; int Port; int rc; @@ -197,6 +196,7 @@ LONG Leash_timesync(int MessageP) rc = ProcessTimeSync(hostname, Port, tmpstr); +#ifdef USE_MESSAGE_BOX if(MessageP != 0) { if (rc && !*tmpstr) @@ -204,6 +204,8 @@ LONG Leash_timesync(int MessageP) strcpy(tmpstr, "Unable to syncronize time!\n\n"); if (*hostname) { + char tmpstr1[2048]; + memset(tmpstr1, '\0', sizeof(tmpstr1)); sprintf(tmpstr1, "Unreachable server: %s\n", hostname); strcat(tmpstr, tmpstr1); @@ -213,6 +215,7 @@ LONG Leash_timesync(int MessageP) MessageBox(NULL, tmpstr, "Time Server", MB_ICONERROR | MB_OK); } +#endif /* USE_MESSAGE_BOX */ WSACleanup(); return(rc); } diff --git a/src/windows/leashdll/winerr.c b/src/windows/leashdll/winerr.c index 2d487b449..e624fc085 100644 --- a/src/windows/leashdll/winerr.c +++ b/src/windows/leashdll/winerr.c @@ -81,6 +81,7 @@ LPSTR err_describe(LPSTR buf, long code) int _export lsh_com_err_proc (LPSTR whoami, long code, LPSTR fmt, va_list args) { +#ifdef USE_MESSAGE_BOX int retval; HWND hOldFocus; char buf[1024], *cp; /* changed to 512 by jms 8/23/93 */ @@ -118,4 +119,7 @@ int _export lsh_com_err_proc (LPSTR whoami, long code, mbformat | MB_ICONHAND | MB_TASKMODAL); SetFocus(hOldFocus); return retval; +#else + return IDOK; +#endif /* USE_MESSAGE_BOX */ } -- 2.26.2