#ifdef __NMAKE__ # NMAKE portion. # Build with : nmake /f custom.cpp # Clean with : nmake /f custom.cpp clean # Builds custom.dll OUTPATH = . # program name macros CC = cl /nologo LINK = link /nologo RM = del DLLFILE = $(OUTPATH)\custom.dll DLLEXPORTS =\ -EXPORT:EnableAllowTgtSessionKey \ -EXPORT:RevertAllowTgtSessionKey \ -EXPORT:AbortMsiImmediate \ -EXPORT:UninstallNsisInstallation \ -EXPORT:KillRunningProcesses \ -EXPORT:ListRunningProcesses \ -EXPORT:InstallNetProvider \ -EXPORT:UninstallNetProvider $(DLLFILE): $(OUTPATH)\custom.obj $(LINK) /OUT:$@ /DLL $** $(DLLEXPORTS) $(OUTPATH)\custom.obj: custom.cpp custom.h $(CC) /c /Fo$@ custom.cpp all: $(DLLFILE) clean: $(RM) $(DLLFILE) $(RM) $(OUTPATH)\custom.obj $(RM) $(OUTPATH)\custom.exp !IFDEF __C_TEXT__ #else /* Copyright 2004,2005 by the Massachusetts Institute of Technology All rights reserved. Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the name of the Massachusetts Institute of Technology (M.I.T.) not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /************************************************************** * custom.cpp : Dll implementing custom action to install Kerberos for Windows * * The functions in this file are for use as entry points * for calls from MSI only. The specific MSI parameters * are noted in the comments section of each of the * functions. * * rcsid: $Id$ **************************************************************/ #pragma unmanaged // Only works for Win2k and above #define _WIN32_WINNT 0x500 #include "custom.h" #include // linker stuff #pragma comment(lib, "msi") #pragma comment(lib, "advapi32") #pragma comment(lib, "shell32") #pragma comment(lib, "user32") void ShowMsiError( MSIHANDLE hInstall, DWORD errcode, DWORD param ){ MSIHANDLE hRecord; hRecord = MsiCreateRecord(3); MsiRecordClearData(hRecord); MsiRecordSetInteger(hRecord, 1, errcode); MsiRecordSetInteger(hRecord, 2, param); MsiProcessMessage( hInstall, INSTALLMESSAGE_ERROR, hRecord ); MsiCloseHandle( hRecord ); } static void ShowMsiErrorEx(MSIHANDLE hInstall, DWORD errcode, LPTSTR str, DWORD param ) { MSIHANDLE hRecord; hRecord = MsiCreateRecord(3); MsiRecordClearData(hRecord); MsiRecordSetInteger(hRecord, 1, errcode); MsiRecordSetString(hRecord, 2, str); MsiRecordSetInteger(hRecord, 3, param); MsiProcessMessage(hInstall, INSTALLMESSAGE_ERROR, hRecord); MsiCloseHandle(hRecord); } #define LSA_KERBEROS_KEY "SYSTEM\\CurrentControlSet\\Control\\Lsa\\Kerberos" #define LSA_KERBEROS_PARM_KEY "SYSTEM\\CurrentControlSet\\Control\\Lsa\\Kerberos\\Parameters" #define KFW_CLIENT_KEY "SOFTWARE\\MIT\\Kerberos\\Client\\" #define SESSKEY_VALUE_NAME "AllowTGTSessionKey" #define SESSBACKUP_VALUE_NAME "AllowTGTSessionKeyBackup" #define SESSXPBACKUP_VALUE_NAME "AllowTGTSessionKeyBackupXP" /* Set the AllowTGTSessionKey registry keys on install. Called as a deferred custom action. */ MSIDLLEXPORT EnableAllowTgtSessionKey( MSIHANDLE hInstall ) { return SetAllowTgtSessionKey( hInstall, TRUE ); } /* Unset the AllowTGTSessionKey registry keys on uninstall. Called as a deferred custom action. */ MSIDLLEXPORT RevertAllowTgtSessionKey( MSIHANDLE hInstall ) { return SetAllowTgtSessionKey( hInstall, FALSE ); } UINT SetAllowTgtSessionKey( MSIHANDLE hInstall, BOOL pInstall ) { TCHAR tchVersionString[1024]; TCHAR tchVersionKey[2048]; DWORD size; DWORD type; DWORD value; HKEY hkKfwClient = NULL; HKEY hkLsaKerberos = NULL; HKEY hkLsaKerberosParm = NULL; UINT rv; DWORD phase = 0; // construct the backup key path size = sizeof(tchVersionString) / sizeof(TCHAR); rv = MsiGetProperty( hInstall, _T("CustomActionData"), tchVersionString, &size ); if(rv != ERROR_SUCCESS) { if(pInstall) { ShowMsiError( hInstall, ERR_CUSTACTDATA, rv ); return rv; } else { return ERROR_SUCCESS; } } _tcscpy( tchVersionKey, _T( KFW_CLIENT_KEY ) ); _tcscat( tchVersionKey, tchVersionString ); phase = 1; rv = RegOpenKeyEx( HKEY_LOCAL_MACHINE, tchVersionKey, 0, ((pInstall)?KEY_WRITE:KEY_READ), &hkKfwClient ); if(rv != ERROR_SUCCESS) goto cleanup; phase = 2; rv = RegOpenKeyEx( HKEY_LOCAL_MACHINE, _T( LSA_KERBEROS_KEY ), 0, KEY_READ | KEY_WRITE, &hkLsaKerberos ); if(rv != ERROR_SUCCESS) goto cleanup; phase = 3; rv = RegOpenKeyEx( HKEY_LOCAL_MACHINE, _T( LSA_KERBEROS_PARM_KEY ), 0, KEY_READ | KEY_WRITE, &hkLsaKerberosParm ); if(rv != ERROR_SUCCESS) { hkLsaKerberosParm = NULL; } if(pInstall) { // backup the existing values if(hkLsaKerberosParm) { phase = 4; size = sizeof(value); rv = RegQueryValueEx( hkLsaKerberosParm, _T( SESSKEY_VALUE_NAME ), NULL, &type, (LPBYTE) &value, &size ); if(rv != ERROR_SUCCESS) value = 0; phase = 5; rv = RegSetValueEx( hkKfwClient, _T( SESSBACKUP_VALUE_NAME ), 0, REG_DWORD, (LPBYTE) &value, sizeof(value)); if(rv != ERROR_SUCCESS) goto cleanup; } phase = 6; size = sizeof(value); rv = RegQueryValueEx( hkLsaKerberos, _T( SESSKEY_VALUE_NAME ), NULL, &type, (LPBYTE) &value, &size ); if(rv != ERROR_SUCCESS) value = 0; phase = 7; rv = RegSetValueEx( hkKfwClient, _T( SESSXPBACKUP_VALUE_NAME ), 0, REG_DWORD, (LPBYTE) &value, sizeof(value)); if(rv != ERROR_SUCCESS) goto cleanup; // and now write the actual values phase = 8; value = 1; rv = RegSetValueEx( hkLsaKerberos, _T( SESSKEY_VALUE_NAME ), 0, REG_DWORD, (LPBYTE) &value, sizeof(value)); if(rv != ERROR_SUCCESS) goto cleanup; if(hkLsaKerberosParm) { phase = 9; value = 1; rv = RegSetValueEx( hkLsaKerberosParm, _T( SESSKEY_VALUE_NAME ), 0, REG_DWORD, (LPBYTE) &value, sizeof(value)); if(rv != ERROR_SUCCESS) goto cleanup; } } else { // uninstalling // Don't fail no matter what goes wrong. This is also a rollback action. if(hkLsaKerberosParm) { size = sizeof(value); rv = RegQueryValueEx( hkKfwClient, _T( SESSBACKUP_VALUE_NAME ), NULL, &type, (LPBYTE) &value, &size ); if(rv != ERROR_SUCCESS) value = 0; RegSetValueEx( hkLsaKerberosParm, _T( SESSKEY_VALUE_NAME ), 0, REG_DWORD, (LPBYTE) &value, sizeof(value)); } size = sizeof(value); rv = RegQueryValueEx( hkKfwClient, _T( SESSXPBACKUP_VALUE_NAME ), NULL, &type, (LPBYTE) &value, &size ); if(rv != ERROR_SUCCESS) value = 0; RegSetValueEx( hkLsaKerberos, _T( SESSKEY_VALUE_NAME ), 0, REG_DWORD, (LPBYTE) &value, sizeof(value)); RegDeleteValue( hkKfwClient, _T( SESSXPBACKUP_VALUE_NAME ) ); RegDeleteValue( hkKfwClient, _T( SESSBACKUP_VALUE_NAME ) ); } // all done rv = ERROR_SUCCESS; cleanup: if(rv != ERROR_SUCCESS && pInstall) { ShowMsiError(hInstall, 4005, phase); } if(hkKfwClient) RegCloseKey( hkKfwClient ); if(hkLsaKerberos) RegCloseKey( hkLsaKerberos ); if(hkLsaKerberosParm) RegCloseKey( hkLsaKerberosParm ); return rv; } /* Abort the installation (called as an immediate custom action) */ MSIDLLEXPORT AbortMsiImmediate( MSIHANDLE hInstall ) { DWORD rv; DWORD dwSize = 0; LPTSTR sReason = NULL; LPTSTR sFormatted = NULL; MSIHANDLE hRecord = NULL; LPTSTR cAbortReason = _T("ABORTREASON"); rv = MsiGetProperty( hInstall, cAbortReason, _T(""), &dwSize ); if(rv != ERROR_MORE_DATA) goto _cleanup; sReason = new TCHAR[ ++dwSize ]; rv = MsiGetProperty( hInstall, cAbortReason, sReason, &dwSize ); if(rv != ERROR_SUCCESS) goto _cleanup; hRecord = MsiCreateRecord(3); MsiRecordClearData(hRecord); MsiRecordSetString(hRecord, 0, sReason); dwSize = 0; rv = MsiFormatRecord(hInstall, hRecord, "", &dwSize); if(rv != ERROR_MORE_DATA) goto _cleanup; sFormatted = new TCHAR[ ++dwSize ]; rv = MsiFormatRecord(hInstall, hRecord, sFormatted, &dwSize); if(rv != ERROR_SUCCESS) goto _cleanup; MsiCloseHandle(hRecord); hRecord = MsiCreateRecord(3); MsiRecordClearData(hRecord); MsiRecordSetInteger(hRecord, 1, ERR_ABORT); MsiRecordSetString(hRecord,2, sFormatted); MsiProcessMessage(hInstall, INSTALLMESSAGE_ERROR, hRecord); _cleanup: if(sFormatted) delete sFormatted; if(hRecord) MsiCloseHandle( hRecord ); if(sReason) delete sReason; return ~ERROR_SUCCESS; } /* Kill specified processes that are running on the system */ /* Uses the custom table KillProcess. Called as an immediate action. */ #define MAX_KILL_PROCESSES 255 #define FIELD_SIZE 256 struct _KillProc { TCHAR * image; TCHAR * desc; BOOL found; DWORD pid; }; #define RV_BAIL if(rv != ERROR_SUCCESS) goto _cleanup MSIDLLEXPORT KillRunningProcesses( MSIHANDLE hInstall ) { return KillRunningProcessesSlave( hInstall, TRUE ); } /* When listing running processes, we populate the ListBox table with values associated with the property 'KillableProcesses'. If we actually find any processes worth killing, then we also set the 'FoundProcceses' property to '1'. Otherwise we set it to ''. */ MSIDLLEXPORT ListRunningProcesses( MSIHANDLE hInstall ) { return KillRunningProcessesSlave( hInstall, FALSE ); } UINT KillRunningProcessesSlave( MSIHANDLE hInstall, BOOL bKill ) { UINT rv = ERROR_SUCCESS; _KillProc * kpList; int nKpList = 0; int i; int rowNum = 1; DWORD size; BOOL found = FALSE; MSIHANDLE hDatabase = NULL; MSIHANDLE hView = NULL; MSIHANDLE hViewInsert = NULL; MSIHANDLE hRecord = NULL; MSIHANDLE hRecordInsert = NULL; HANDLE hSnapshot = NULL; PROCESSENTRY32 pe; kpList = new _KillProc[MAX_KILL_PROCESSES]; memset(kpList, 0, sizeof(*kpList) * MAX_KILL_PROCESSES); hDatabase = MsiGetActiveDatabase( hInstall ); if( hDatabase == NULL ) { rv = GetLastError(); goto _cleanup; } // If we are only going to list out the processes, delete all the existing // entries first. if(!bKill) { rv = MsiDatabaseOpenView( hDatabase, _T( "DELETE FROM `ListBox` WHERE `ListBox`.`Property` = 'KillableProcesses'" ), &hView); RV_BAIL; rv = MsiViewExecute( hView, NULL ); RV_BAIL; MsiCloseHandle( hView ); hView = NULL; rv = MsiDatabaseOpenView( hDatabase, _T( "SELECT * FROM `ListBox` WHERE `Property` = 'KillableProcesses'" ), &hViewInsert); RV_BAIL; MsiViewExecute(hViewInsert, NULL); hRecordInsert = MsiCreateRecord(4); if(hRecordInsert == NULL) { rv = GetLastError(); goto _cleanup; } } // Open a view rv = MsiDatabaseOpenView( hDatabase, _T( "SELECT `Image`,`Desc` FROM `KillProcess`" ), &hView); RV_BAIL; rv = MsiViewExecute( hView, NULL ); RV_BAIL; do { rv = MsiViewFetch( hView, &hRecord ); if(rv != ERROR_SUCCESS) { if(hRecord) MsiCloseHandle(hRecord); hRecord = NULL; break; } kpList[nKpList].image = new TCHAR[ FIELD_SIZE ]; kpList[nKpList].image[0] = _T('\0'); kpList[nKpList].desc = new TCHAR[ FIELD_SIZE ]; kpList[nKpList].desc[0] = _T('\0'); nKpList++; size = FIELD_SIZE; rv = MsiRecordGetString(hRecord, 1, kpList[nKpList-1].image, &size); RV_BAIL; size = FIELD_SIZE; rv = MsiRecordGetString(hRecord, 2, kpList[nKpList-1].desc, &size); RV_BAIL; MsiCloseHandle(hRecord); } while(nKpList < MAX_KILL_PROCESSES); hRecord = NULL; // now we have all the processes in the array. Check if they are running. hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); if(hSnapshot == INVALID_HANDLE_VALUE) { rv = GetLastError(); goto _cleanup; } pe.dwSize = sizeof( PROCESSENTRY32 ); if(!Process32First( hSnapshot, &pe )) { // technically we should at least find the MSI process, but we let this pass rv = ERROR_SUCCESS; goto _cleanup; } do { for(i=0; i