28 lines
2.0 KiB
C#
28 lines
2.0 KiB
C#
|
|
// Loaded only by explicit elevated install/uninstall scripts. Never by the agent.
|
||
|
|
using System;
|
||
|
|
using System.ComponentModel;
|
||
|
|
using System.Runtime.InteropServices;
|
||
|
|
using System.Security.Principal;
|
||
|
|
|
||
|
|
public static class HomeVisionServiceRights {
|
||
|
|
[StructLayout(LayoutKind.Sequential)] struct LsaObjectAttributes { public uint Length; public IntPtr Root, ObjectName; public uint Attributes; public IntPtr Descriptor, Quality; }
|
||
|
|
[StructLayout(LayoutKind.Sequential)] struct LsaString { public ushort Length, MaximumLength; public IntPtr Buffer; }
|
||
|
|
[DllImport("advapi32.dll")] static extern uint LsaOpenPolicy(IntPtr system, ref LsaObjectAttributes attributes, uint access, out IntPtr policy);
|
||
|
|
[DllImport("advapi32.dll")] static extern uint LsaAddAccountRights(IntPtr policy, byte[] sid, LsaString[] rights, uint count);
|
||
|
|
[DllImport("advapi32.dll")] static extern uint LsaRemoveAccountRights(IntPtr policy, byte[] sid, [MarshalAs(UnmanagedType.Bool)] bool all, LsaString[] rights, uint count);
|
||
|
|
[DllImport("advapi32.dll")] static extern uint LsaClose(IntPtr policy);
|
||
|
|
[DllImport("advapi32.dll")] static extern int LsaNtStatusToWinError(uint status);
|
||
|
|
static void Check(uint result) { if (result != 0) throw new Win32Exception(LsaNtStatusToWinError(result)); }
|
||
|
|
public static void SetShutdownRight(SecurityIdentifier identity, bool grant) {
|
||
|
|
var attributes = new LsaObjectAttributes { Length = (uint)Marshal.SizeOf(typeof(LsaObjectAttributes)) };
|
||
|
|
IntPtr policy;
|
||
|
|
Check(LsaOpenPolicy(IntPtr.Zero, ref attributes, 0x00000810, out policy)); // lookup names + create account
|
||
|
|
IntPtr text = Marshal.StringToHGlobalUni("SeShutdownPrivilege");
|
||
|
|
try {
|
||
|
|
byte[] sid = new byte[identity.BinaryLength]; identity.GetBinaryForm(sid, 0);
|
||
|
|
var rights = new[] { new LsaString { Buffer = text, Length = 38, MaximumLength = 40 } };
|
||
|
|
Check(grant ? LsaAddAccountRights(policy, sid, rights, 1) : LsaRemoveAccountRights(policy, sid, false, rights, 1));
|
||
|
|
} finally { Marshal.FreeHGlobal(text); LsaClose(policy); }
|
||
|
|
}
|
||
|
|
}
|