Path: blob/main/crypto/krb5/src/windows/leash/LeashUICommandHandler.cpp
34889 views
// -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-1// leash/LeashUICommandHandler.cpp - implements IUICommandHandler interfaces2//3// Copyright (C) 2014 by the Massachusetts Institute of Technology.4// All rights reserved.5//6// Redistribution and use in source and binary forms, with or without7// modification, are permitted provided that the following conditions8// are met:9//10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12//13// * Redistributions in binary form must reproduce the above copyright14// notice, this list of conditions and the following disclaimer in15// the documentation and/or other materials provided with the16// distribution.17//18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE22// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,23// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES24// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR25// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,27// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED29// OF THE POSSIBILITY OF SUCH DAMAGE.3031// This file contains the class implementation of the leash implementation32// of the UICommandHandler interface. Its primary responsibility is33// to accept UI events (i.e., button presses) and perform the34// corresponding actions to the leash data structures and display35// presentation.3637#include <UIRibbon.h>38#include <UIRibbonPropertyHelpers.h>39#include "kfwribbon.h"40#include "LeashUICommandHandler.h"41#include "resource.h"4243#include <loadfuncs-leash.h>4445// Allowing mixed-case realms has both a machine and user-specific knob,46// and thus needs a function to manage it.47extern DWORD Leash_get_default_uppercaserealm();48extern DECL_FUNC_PTR(Leash_get_default_uppercaserealm);4950HRESULT51LeashUICommandHandler::CreateInstance(IUICommandHandler **out, HWND hwnd)52{53LeashUICommandHandler *handler;5455if (out == NULL)56return E_POINTER;5758handler = new LeashUICommandHandler();59handler->mainwin = hwnd;60*out = static_cast<IUICommandHandler *>(handler);61return S_OK;62}6364ULONG65LeashUICommandHandler::AddRef()66{67return InterlockedIncrement(&refcnt);68}6970ULONG71LeashUICommandHandler::Release()72{73LONG tmp;7475tmp = InterlockedDecrement(&refcnt);76if (tmp == 0)77delete this;78return tmp;79}8081HRESULT82LeashUICommandHandler::QueryInterface(REFIID iid, void **ppv)83{84if (ppv == NULL)85return E_POINTER;8687if (iid == __uuidof(IUnknown)) {88*ppv = static_cast<IUnknown*>(this);89} else if (iid == __uuidof(IUICommandHandler)) {90*ppv = static_cast<IUICommandHandler*>(this);91} else {92*ppv = NULL;93return E_NOINTERFACE;94}9596AddRef();97return S_OK;98}99100// Called by the framework when a control is activated that may require101// an action to be taken, such as a button being pressed or a checkbox102// state flipped. (It is not called when the user changes tabs on the103// ribbon.) Just proxy these commands through to the existing MFC104// handlers by sendding the appropriate message to the main window.105// Action only needs to be taken on the EXECUTE verb, so we can106// ignore the additional properties surrounding the action, which would107// be relevant for other verbs.108//109// The commandIds are taken from the XML ribbon description.110HRESULT111LeashUICommandHandler::Execute(UINT32 commandId, UI_EXECUTIONVERB verb,112const PROPERTYKEY *key,113const PROPVARIANT *currentValue,114IUISimplePropertySet *commandExecutionProperties)115{116if (verb != UI_EXECUTIONVERB_EXECUTE)117return E_NOTIMPL;118119switch(commandId) {120case cmdGetTicketButton:121SendMessage(mainwin, WM_COMMAND, MAKEWPARAM(ID_INIT_TICKET, 1), 0);122break;123case cmdRenewTicketButton:124SendMessage(mainwin, WM_COMMAND, MAKEWPARAM(ID_RENEW_TICKET, 1), 0);125break;126case cmdDestroyTicketButton:127SendMessage(mainwin, WM_COMMAND, MAKEWPARAM(ID_DESTROY_TICKET, 1),1280);129break;130case cmdMakeDefaultButton:131SendMessage(mainwin, WM_COMMAND, MAKEWPARAM(ID_MAKE_DEFAULT, 1),1320);133break;134case cmdChangePasswordButton:135SendMessage(mainwin, WM_COMMAND, MAKEWPARAM(ID_CHANGE_PASSWORD, 1),1360);137break;138case cmdIssuedCheckBox:139SendMessage(mainwin, WM_COMMAND, MAKEWPARAM(ID_TIME_ISSUED, 1), 0);140break;141case cmdRenewUntilCheckBox:142SendMessage(mainwin, WM_COMMAND, MAKEWPARAM(ID_RENEWABLE_UNTIL, 1),1430);144break;145case cmdValidUntilCheckBox:146SendMessage(mainwin, WM_COMMAND, MAKEWPARAM(ID_VALID_UNTIL, 1), 0);147break;148case cmdEncTypeCheckBox:149SendMessage(mainwin, WM_COMMAND, MAKEWPARAM(ID_ENCRYPTION_TYPE, 1),1500);151break;152case cmdFlagsCheckBox:153SendMessage(mainwin, WM_COMMAND, MAKEWPARAM(ID_SHOW_TICKET_FLAGS,1541), 0);155break;156case cmdCcacheNameCheckBox:157SendMessage(mainwin, WM_COMMAND, MAKEWPARAM(ID_CCACHE_NAME, 1), 0);158break;159case cmdAutoRenewCheckBox:160SendMessage(mainwin, WM_COMMAND, MAKEWPARAM(ID_AUTO_RENEW, 1), 0);161break;162case cmdExpireAlarmCheckBox:163SendMessage(mainwin, WM_COMMAND, MAKEWPARAM(ID_LOW_TICKET_ALARM,1641), 0);165break;166case cmdDestroyOnExitCheckBox:167SendMessage(mainwin, WM_COMMAND, MAKEWPARAM(ID_KILL_TIX_ONEXIT, 1),1680);169break;170case cmdMixedCaseCheckBox:171SendMessage(mainwin, WM_COMMAND, MAKEWPARAM(ID_UPPERCASE_REALM, 1),1720);173break;174case cmdHelp:175SendMessage(mainwin, WM_COMMAND, MAKEWPARAM(ID_HELP_LEASH32, 1), 0);176break;177case cmdAbout:178// ID_APP_ABOUT (0xe140) is defined in afxres.h, an MFC header179SendMessage(mainwin, WM_COMMAND, MAKEWPARAM(0xe140, 1), 0);180break;181case cmdExit:182// Save Ribbon customizations here, since this is the only183// path to a clean exit from the application.184if (app != NULL)185app->SaveRibbonState();186// ID_APP_EXIT (0xe141) is defined in afxres.h, an MFC header187SendMessage(mainwin, WM_COMMAND, MAKEWPARAM(0xe141, 1), 0);188break;189default:190// Lots of commands we don't need to pass on191return S_OK;192}193return S_OK;194}195196// Looks up a given registry key in this application's Settings space197// (analogous to CWinApp::GetProfileInt()), converting it to a198// (boolean) PROPVARIANT which is returned in *out. Uses the given199// default value if the registry key cannot be loaded.200static HRESULT201RegKeyToProperty(const char *regkey, bool default, PROPVARIANT *out)202{203DWORD bsize = sizeof(DWORD), enabled;204LONG code;205206code = RegGetValue(HKEY_CURRENT_USER,207"Software\\MIT\\MIT Kerberos\\Settings",208regkey, RRF_RT_DWORD, NULL, &enabled,209&bsize);210if (code == ERROR_FILE_NOT_FOUND) {211code = ERROR_SUCCESS;212enabled = default ? 1 : 0;213}214if (FAILED(code) || bsize != sizeof(enabled))215return E_FAIL;216return UIInitPropertyFromBoolean(UI_PKEY_BooleanValue, enabled, out);217}218219// Called by the framework when the value of a property needs to be220// re-evaluated, e.g., if it has been explicitly invalidated, or at221// program startup. This is the way to specify the initial/default222// state for ribbon elements which have state, such as checkboxes.223// The registry values which are modified by the MFC checkbox224// action handlers can be read directly from here in order to present225// a consistent visual interface. The MFC handlers only write to the226// registry when a value is changed, though, so we must duplicate227// the default values which are hardcoded in CLeashView::sm_viewColumns[]228// and elsewhere in LeashView.cpp.229HRESULT230LeashUICommandHandler::UpdateProperty(UINT32 commandId, REFPROPERTYKEY key,231const PROPVARIANT *currentValue,232PROPVARIANT *newValue)233{234if (key != UI_PKEY_BooleanValue)235return E_NOTIMPL;236237// These default values duplicate those hardcoded in238// CLeashView::sm_viewColumns[] and elsewhere in LeashView.cpp.239switch(commandId) {240case cmdIssuedCheckBox:241return RegKeyToProperty("Issued", false, newValue);242case cmdRenewUntilCheckBox:243return RegKeyToProperty("Renewable Until", false, newValue);244case cmdValidUntilCheckBox:245return RegKeyToProperty("Valid Until", true, newValue);246case cmdEncTypeCheckBox:247return RegKeyToProperty("Encryption Type", false, newValue);248case cmdFlagsCheckBox:249return RegKeyToProperty("Flags", false, newValue);250case cmdCcacheNameCheckBox:251return RegKeyToProperty("Credential Cache", false, newValue);252case cmdAutoRenewCheckBox:253return RegKeyToProperty("AutoRenewTickets", true, newValue);254case cmdExpireAlarmCheckBox:255return RegKeyToProperty("LowTicketAlarm", true, newValue);256case cmdDestroyOnExitCheckBox:257return RegKeyToProperty("DestroyTicketsOnExit", false, newValue);258case cmdMixedCaseCheckBox:259return UIInitPropertyFromBoolean(UI_PKEY_BooleanValue,260pLeash_get_default_uppercaserealm(), newValue);261default:262return E_NOTIMPL;263}264265return E_NOTIMPL;266}267268269