Skip to content

Commit 0c7a1dd

Browse files
[GPO][MWB]Add policy to disable service mode (#37366)
* [MWB]Add policy to disable service mode * Add restart note * Tweak settings to disable setting * Tweak infobars * Policy should be machine only
1 parent e0cb401 commit 0c7a1dd

File tree

13 files changed

+109
-12
lines changed

13 files changed

+109
-12
lines changed

src/common/GPOWrapper/GPOWrapper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ namespace winrt::PowerToys::GPOWrapper::implementation
200200
{
201201
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredMwbDisallowBlockingScreensaverValue());
202202
}
203+
GpoRuleConfigured GPOWrapper::GetConfiguredMwbAllowServiceModeValue()
204+
{
205+
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredMwbAllowServiceModeValue());
206+
}
203207
GpoRuleConfigured GPOWrapper::GetConfiguredMwbSameSubnetOnlyValue()
204208
{
205209
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredMwbSameSubnetOnlyValue());

src/common/GPOWrapper/GPOWrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ namespace winrt::PowerToys::GPOWrapper::implementation
5656
static GpoRuleConfigured GetConfiguredMwbFileTransferEnabledValue();
5757
static GpoRuleConfigured GetConfiguredMwbUseOriginalUserInterfaceValue();
5858
static GpoRuleConfigured GetConfiguredMwbDisallowBlockingScreensaverValue();
59+
static GpoRuleConfigured GetConfiguredMwbAllowServiceModeValue();
5960
static GpoRuleConfigured GetConfiguredMwbSameSubnetOnlyValue();
6061
static GpoRuleConfigured GetConfiguredMwbValidateRemoteIpValue();
6162
static GpoRuleConfigured GetConfiguredMwbDisableUserDefinedIpMappingRulesValue();

src/common/GPOWrapper/GPOWrapper.idl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ namespace PowerToys
6060
static GpoRuleConfigured GetConfiguredMwbFileTransferEnabledValue();
6161
static GpoRuleConfigured GetConfiguredMwbUseOriginalUserInterfaceValue();
6262
static GpoRuleConfigured GetConfiguredMwbDisallowBlockingScreensaverValue();
63+
static GpoRuleConfigured GetConfiguredMwbAllowServiceModeValue();
6364
static GpoRuleConfigured GetConfiguredMwbSameSubnetOnlyValue();
6465
static GpoRuleConfigured GetConfiguredMwbValidateRemoteIpValue();
6566
static GpoRuleConfigured GetConfiguredMwbDisableUserDefinedIpMappingRulesValue();

src/common/utils/gpo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ namespace powertoys_gpo {
7979
const std::wstring POLICY_MWB_FILE_TRANSFER_ENABLED = L"MwbFileTransferEnabled";
8080
const std::wstring POLICY_MWB_USE_ORIGINAL_USER_INTERFACE = L"MwbUseOriginalUserInterface";
8181
const std::wstring POLICY_MWB_DISALLOW_BLOCKING_SCREENSAVER = L"MwbDisallowBlockingScreensaver";
82+
const std::wstring POLICY_MWB_ALLOW_SERVICE_MODE = L"MwbAllowServiceMode";
8283
const std::wstring POLICY_MWB_SAME_SUBNET_ONLY = L"MwbSameSubnetOnly";
8384
const std::wstring POLICY_MWB_VALIDATE_REMOTE_IP = L"MwbValidateRemoteIp";
8485
const std::wstring POLICY_MWB_DISABLE_USER_DEFINED_IP_MAPPING_RULES = L"MwbDisableUserDefinedIpMappingRules";
@@ -558,6 +559,11 @@ namespace powertoys_gpo {
558559
return getConfiguredValue(POLICY_MWB_DISALLOW_BLOCKING_SCREENSAVER);
559560
}
560561

562+
inline gpo_rule_configured_t getConfiguredMwbAllowServiceModeValue()
563+
{
564+
return getConfiguredValue(POLICY_MWB_ALLOW_SERVICE_MODE);
565+
}
566+
561567
inline gpo_rule_configured_t getConfiguredMwbSameSubnetOnlyValue()
562568
{
563569
return getConfiguredValue(POLICY_MWB_SAME_SUBNET_ONLY);

src/gpo/assets/PowerToys.admx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!-- Copyright (c) Microsoft Corporation.
33
Licensed under the MIT License. -->
4-
<policyDefinitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" revision="1.15" schemaVersion="1.0" xmlns="http://schemas.microsoft.com/GroupPolicy/2006/07/PolicyDefinitions">
4+
<policyDefinitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" revision="1.16" schemaVersion="1.0" xmlns="http://schemas.microsoft.com/GroupPolicy/2006/07/PolicyDefinitions">
55
<policyNamespaces>
66
<target prefix="powertoys" namespace="Microsoft.Policies.PowerToys" />
77
</policyNamespaces>
8-
<resources minRequiredRevision="1.15"/><!-- Last changed with PowerToys v0.88.0 -->
8+
<resources minRequiredRevision="1.16"/><!-- Last changed with PowerToys v0.89.0 -->
99
<supportedOn>
1010
<definitions>
1111
<definition name="SUPPORTED_POWERTOYS_0_64_0" displayName="$(string.SUPPORTED_POWERTOYS_0_64_0)"/>
@@ -24,6 +24,7 @@
2424
<definition name="SUPPORTED_POWERTOYS_0_85_0" displayName="$(string.SUPPORTED_POWERTOYS_0_85_0)"/>
2525
<definition name="SUPPORTED_POWERTOYS_0_86_0" displayName="$(string.SUPPORTED_POWERTOYS_0_86_0)"/>
2626
<definition name="SUPPORTED_POWERTOYS_0_88_0" displayName="$(string.SUPPORTED_POWERTOYS_0_88_0)"/>
27+
<definition name="SUPPORTED_POWERTOYS_0_89_0" displayName="$(string.SUPPORTED_POWERTOYS_0_89_0)"/>
2728
<definition name="SUPPORTED_POWERTOYS_0_64_0_TO_0_87_1" displayName="$(string.SUPPORTED_POWERTOYS_0_64_0_TO_0_87_1)"/>
2829
</definitions>
2930
</supportedOn>
@@ -602,6 +603,16 @@
602603
<decimal value="0" />
603604
</disabledValue>
604605
</policy>
606+
<policy name="MwbAllowServiceMode" class="Machine" displayName="$(string.MwbAllowServiceMode)" explainText="$(string.MwbAllowServiceModeDescription)" key="Software\Policies\PowerToys" valueName="MwbAllowServiceMode">
607+
<parentCategory ref="MouseWithoutBorders" />
608+
<supportedOn ref="SUPPORTED_POWERTOYS_0_89_0" />
609+
<enabledValue>
610+
<decimal value="1" />
611+
</enabledValue>
612+
<disabledValue>
613+
<decimal value="0" />
614+
</disabledValue>
615+
</policy>
605616
<policy name="MwbSameSubnetOnly" class="Both" displayName="$(string.MwbSameSubnetOnly)" explainText="$(string.MwbSameSubnetOnlyDescription)" key="Software\Policies\PowerToys" valueName="MwbSameSubnetOnly">
606617
<parentCategory ref="MouseWithoutBorders" />
607618
<supportedOn ref="SUPPORTED_POWERTOYS_0_83_0" />

src/gpo/assets/en-US/PowerToys.adml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!-- Copyright (c) Microsoft Corporation.
33
Licensed under the MIT License. -->
4-
<policyDefinitionResources xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" revision="1.15" schemaVersion="1.0" xmlns="http://schemas.microsoft.com/GroupPolicy/2006/07/PolicyDefinitions">
4+
<policyDefinitionResources xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" revision="1.16" schemaVersion="1.0" xmlns="http://schemas.microsoft.com/GroupPolicy/2006/07/PolicyDefinitions">
55
<displayName>PowerToys</displayName>
66
<description>PowerToys</description>
77
<resources>
@@ -31,6 +31,7 @@
3131
<string id="SUPPORTED_POWERTOYS_0_85_0">PowerToys version 0.85.0 or later</string>
3232
<string id="SUPPORTED_POWERTOYS_0_86_0">PowerToys version 0.86.0 or later</string>
3333
<string id="SUPPORTED_POWERTOYS_0_88_0">PowerToys version 0.88.0 or later</string>
34+
<string id="SUPPORTED_POWERTOYS_0_89_0">PowerToys version 0.89.0 or later</string>
3435
<string id="SUPPORTED_POWERTOYS_0_64_0_TO_0_87_1">From PowerToys version 0.64.0 until PowerToys version 0.87.1</string>
3536

3637
<string id="ConfigureAllUtilityGlobalEnabledStateDescription">This policy configures the enabled state for all PowerToys utilities.
@@ -169,7 +170,14 @@ If you enable this policy, the user won't be able to enable the "block screensav
169170

170171
If you disable or don't configure this policy, the user takes control over the setting and can block the screensaver.
171172
</string>
173+
<string id="MwbAllowServiceModeDescription">This policy configures if the user is allowed to use Mouse Without Borders in Service Mode.
172174

175+
If this setting is enabled or not configured, the user can enable and use Mouse Without Borders in Service Mode.
176+
177+
If this setting is disabled, the user won't be able to enable or use Mouse Without Borders in Service Mode.
178+
179+
Note: As most other PowerToys policies, a restart of PowerToys is required for a change in this policy to take full effect.
180+
</string>
173181
<string id="MwbSameSubnetOnlyDescription">This policy configures if connections are only allowed in the same subnet.
174182

175183
If you enable this policy, the setting is enabled and only connections in the same subnet are allowed.
@@ -264,6 +272,7 @@ If you don't configure this policy, the user takes control over the setting and
264272
<string id="MwbFileTransferEnabled">File transfer enabled</string>
265273
<string id="MwbUseOriginalUserInterface">Original user interface is available</string>
266274
<string id="MwbDisallowBlockingScreensaver">Disallow blocking screensaver on other machines</string>
275+
<string id="MwbAllowServiceMode">Allow Service Mode</string>
267276
<string id="MwbSameSubnetOnly">Connect only in same subnet</string>
268277
<string id="MwbValidateRemoteIp">Validate remote machine IP Address</string>
269278
<string id="MwbDisableUserDefinedIpMappingRules">Disable user defined IP Address mapping rules</string>

src/modules/MouseWithoutBorders/App/Class/Program.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ private static void Main()
9292

9393
bool serviceMode = firstArg == ServiceModeArg;
9494

95+
if (PowerToys.GPOWrapper.GPOWrapper.GetConfiguredMwbAllowServiceModeValue() == PowerToys.GPOWrapper.GpoRuleConfigured.Disabled)
96+
{
97+
if (runningAsSystem)
98+
{
99+
Logger.Log("Can't run as a service. It's not allowed according to GPO policy. Please contact your systems administrator.");
100+
return;
101+
}
102+
103+
serviceMode = false;
104+
}
105+
95106
// If we're started from the .dll module or from the service process, we should
96107
// assume the service mode.
97108
if (serviceMode && !runningAsSystem)

src/modules/MouseWithoutBorders/App/Class/Setting.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,11 @@ internal bool UseService
10901090
{
10911091
get
10921092
{
1093+
if (GPOWrapper.GetConfiguredMwbAllowServiceModeValue() == GpoRuleConfigured.Disabled)
1094+
{
1095+
return false;
1096+
}
1097+
10931098
lock (_loadingSettingsLock)
10941099
{
10951100
return _properties.UseService;
@@ -1098,6 +1103,11 @@ internal bool UseService
10981103

10991104
set
11001105
{
1106+
if (AllowServiceModeIsGpoConfigured)
1107+
{
1108+
return;
1109+
}
1110+
11011111
lock (_loadingSettingsLock)
11021112
{
11031113
_properties.UseService = value;
@@ -1109,6 +1119,10 @@ internal bool UseService
11091119
}
11101120
}
11111121

1122+
[CmdConfigureIgnore]
1123+
[JsonIgnore]
1124+
internal bool AllowServiceModeIsGpoConfigured => GPOWrapper.GetConfiguredMwbAllowServiceModeValue() == GpoRuleConfigured.Disabled;
1125+
11121126
// Note(@htcfreek): Settings UI CheckBox is disabled in frmMatrix.cs > FrmMatrix_Load()
11131127
internal bool SendErrorLogV2
11141128
{

src/modules/MouseWithoutBorders/App/Service/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ internal sealed class Program
2828
[STAThread]
2929
private static void Main()
3030
{
31-
if (PowerToys.GPOWrapper.GPOWrapper.GetConfiguredMouseWithoutBordersEnabledValue() == PowerToys.GPOWrapper.GpoRuleConfigured.Disabled)
31+
if (PowerToys.GPOWrapper.GPOWrapper.GetConfiguredMouseWithoutBordersEnabledValue() == PowerToys.GPOWrapper.GpoRuleConfigured.Disabled
32+
|| PowerToys.GPOWrapper.GPOWrapper.GetConfiguredMwbAllowServiceModeValue() == PowerToys.GPOWrapper.GpoRuleConfigured.Disabled )
3233
{
3334
// TODO: Add logging.
3435
// Logger.LogWarning("Tried to start with a GPO policy setting the utility to always be disabled. Please contact your systems administrator.");

src/modules/MouseWithoutBorders/ModuleInterface/dllmain.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,11 @@ class MouseWithoutBorders : public PowertoyModuleIface
363363

364364
void update_state_from_settings(const PowerToysSettings::PowerToyValues& values)
365365
{
366-
const bool new_run_in_service_mode = values.get_bool_value(USE_SERVICE_PROPERTY_NAME).value_or(false);
366+
bool new_run_in_service_mode = values.get_bool_value(USE_SERVICE_PROPERTY_NAME).value_or(false);
367+
if (powertoys_gpo::getConfiguredMwbAllowServiceModeValue() == powertoys_gpo::gpo_rule_configured_disabled)
368+
{
369+
new_run_in_service_mode = false;
370+
}
367371

368372
if (new_run_in_service_mode != run_in_service_mode)
369373
{

src/settings-ui/Settings.UI/SettingsXAML/Views/MouseWithoutBordersPage.xaml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,19 @@
186186
</tkcontrols:SettingsCard>
187187
</controls:SettingsGroup>
188188
<controls:SettingsGroup x:Uid="MouseWithoutBorders_ServiceSettings" IsEnabled="{x:Bind ViewModel.CanToggleUseService, Mode=OneWay}">
189-
<tkcontrols:SettingsCard x:Uid="MouseWithoutBorders_UseService">
190-
<ToggleSwitch
191-
x:Uid="MouseWithoutBorders_UseService_ToggleSwitch"
192-
IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}"
193-
IsOn="{x:Bind ViewModel.UseService, Mode=TwoWay}" />
189+
<tkcontrols:SettingsCard x:Uid="MouseWithoutBorders_UseService" IsEnabled="{x:Bind ViewModel.UseServiceSettingIsEnabled, Mode=OneWay}">
190+
<ToggleSwitch x:Uid="MouseWithoutBorders_UseService_ToggleSwitch" IsOn="{x:Bind ViewModel.UseService, Mode=TwoWay}" />
194191
</tkcontrols:SettingsCard>
192+
<InfoBar
193+
x:Uid="GPO_SettingIsManaged"
194+
IsClosable="False"
195+
IsOpen="{x:Bind ViewModel.ShowPolicyConfiguredInfoForServiceSettings, Mode=OneWay}"
196+
IsTabStop="{x:Bind ViewModel.ShowPolicyConfiguredInfoForServiceSettings, Mode=OneWay}"
197+
Severity="Informational">
198+
<InfoBar.IconSource>
199+
<FontIconSource FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE72E;" />
200+
</InfoBar.IconSource>
201+
</InfoBar>
195202
<InfoBar
196203
x:Uid="MouseWithoutBorders_RunAsAdminText"
197204
IsClosable="False"

src/settings-ui/Settings.UI/ViewModels/MouseWithoutBordersViewModel.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,23 @@ public bool ShowOriginalUI
9393

9494
public bool UseService
9595
{
96-
get => Settings.Properties.UseService;
96+
get
97+
{
98+
if (_allowServiceModeGpoConfiguration == GpoRuleConfigured.Disabled)
99+
{
100+
return false;
101+
}
102+
103+
return Settings.Properties.UseService;
104+
}
97105

98106
set
99107
{
108+
if (_allowServiceModeIsGPOConfigured)
109+
{
110+
return;
111+
}
112+
100113
var valueChanged = Settings.Properties.UseService != value;
101114

102115
// Set the UI property itself instantly
@@ -122,6 +135,8 @@ public bool UseService
122135
}
123136
}
124137

138+
public bool UseServiceSettingIsEnabled => _allowServiceModeIsGPOConfigured == false;
139+
125140
public bool ConnectFieldsVisible
126141
{
127142
get => _connectFieldsVisible;
@@ -185,6 +200,8 @@ public string ConnectPCName
185200
private bool _useOriginalUserInterfaceIsGPOConfigured;
186201
private GpoRuleConfigured _disallowBlockingScreensaverGpoConfiguration;
187202
private bool _disallowBlockingScreensaverIsGPOConfigured;
203+
private GpoRuleConfigured _allowServiceModeGpoConfiguration;
204+
private bool _allowServiceModeIsGPOConfigured;
188205
private GpoRuleConfigured _sameSubnetOnlyGpoConfiguration;
189206
private bool _sameSubnetOnlyIsGPOConfigured;
190207
private GpoRuleConfigured _validateRemoteIpGpoConfiguration;
@@ -507,6 +524,8 @@ private void InitializePolicyValues()
507524
_disableUserDefinedIpMappingRulesIsGPOConfigured = _disableUserDefinedIpMappingRulesGpoConfiguration == GpoRuleConfigured.Enabled;
508525

509526
// Policies supporting only disabled state
527+
_allowServiceModeGpoConfiguration = GPOWrapper.GetConfiguredMwbAllowServiceModeValue();
528+
_allowServiceModeIsGPOConfigured = _allowServiceModeGpoConfiguration == GpoRuleConfigured.Disabled;
510529
_clipboardSharingEnabledGpoConfiguration = GPOWrapper.GetConfiguredMwbClipboardSharingEnabledValue();
511530
_clipboardSharingEnabledIsGPOConfigured = _clipboardSharingEnabledGpoConfiguration == GpoRuleConfigured.Disabled;
512531
_fileTransferEnabledGpoConfiguration = GPOWrapper.GetConfiguredMwbFileTransferEnabledValue();
@@ -1231,6 +1250,14 @@ internal void UninstallService()
12311250
SendCustomAction("uninstall_service");
12321251
}
12331252

1253+
public bool ShowPolicyConfiguredInfoForServiceSettings
1254+
{
1255+
get
1256+
{
1257+
return IsEnabled && _allowServiceModeIsGPOConfigured;
1258+
}
1259+
}
1260+
12341261
public bool ShowPolicyConfiguredInfoForBehaviorSettings
12351262
{
12361263
get
@@ -1248,7 +1275,7 @@ public bool ShowInfobarCannotDragDropAsAdmin
12481275

12491276
public bool ShowInfobarRunAsAdminText
12501277
{
1251-
get { return !CanToggleUseService && IsEnabled; }
1278+
get { return !CanToggleUseService && IsEnabled && !ShowPolicyConfiguredInfoForServiceSettings; }
12521279
}
12531280
}
12541281
}

tools/BugReportTool/BugReportTool/ReportGPOValues.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ void ReportGPOValues(const std::filesystem::path &tmpDir)
8787
report << "getConfiguredMwbFileTransferEnabledValue: " << gpo_rule_configured_to_string(powertoys_gpo::getConfiguredMwbFileTransferEnabledValue()) << std::endl;
8888
report << "getConfiguredMwbUseOriginalUserInterfaceValue: " << gpo_rule_configured_to_string(powertoys_gpo::getConfiguredMwbUseOriginalUserInterfaceValue()) << std::endl;
8989
report << "getConfiguredMwbDisallowBlockingScreensaverValue: " << gpo_rule_configured_to_string(powertoys_gpo::getConfiguredMwbDisallowBlockingScreensaverValue()) << std::endl;
90+
report << "getConfiguredMwbAllowServiceModeValue: " << gpo_rule_configured_to_string(powertoys_gpo::getConfiguredMwbAllowServiceModeValue()) << std::endl;
9091
report << "getConfiguredMwbSameSubnetOnlyValue: " << gpo_rule_configured_to_string(powertoys_gpo::getConfiguredMwbSameSubnetOnlyValue()) << std::endl;
9192
report << "getConfiguredMwbValidateRemoteIpValue: " << gpo_rule_configured_to_string(powertoys_gpo::getConfiguredMwbValidateRemoteIpValue()) << std::endl;
9293
report << "getConfiguredMwbDisableUserDefinedIpMappingRulesValue: " << gpo_rule_configured_to_string(powertoys_gpo::getConfiguredMwbDisableUserDefinedIpMappingRulesValue()) << std::endl;

0 commit comments

Comments
 (0)