mirror of
https://github.com/mirror/processhacker
synced 2026-06-08 16:03:24 +00:00
b02427417c
git-svn-id: svn://svn.code.sf.net/p/processhacker/code@5614 21ef857c-d57f-4fe0-8362-d861dc6d29cd
195 lines
5.8 KiB
C#
195 lines
5.8 KiB
C#
/*
|
|
* Process Hacker -
|
|
* SAM server handle
|
|
*
|
|
* Copyright (C) 2009 wj32
|
|
*
|
|
* This file is part of Process Hacker.
|
|
*
|
|
* Process Hacker is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Process Hacker is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with Process Hacker. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using ProcessHacker.Common;
|
|
using ProcessHacker.Native.Api;
|
|
using ProcessHacker.Native.Security;
|
|
|
|
namespace ProcessHacker.Native.Objects
|
|
{
|
|
/// <summary>
|
|
/// Represents a handle to a SAM server.
|
|
/// </summary>
|
|
public sealed class SamServerHandle : SamHandle<SamServerAccess>
|
|
{
|
|
private static WeakReference<SamServerHandle> _connectServerHandle;
|
|
private static int _connectServerHandleMisses = 0;
|
|
|
|
public static SamServerHandle ConnectServerHandle
|
|
{
|
|
get
|
|
{
|
|
WeakReference<SamServerHandle> weakRef = _connectServerHandle;
|
|
SamServerHandle connectHandle = null;
|
|
|
|
if (weakRef != null)
|
|
{
|
|
connectHandle = weakRef.Target;
|
|
}
|
|
|
|
if (connectHandle == null)
|
|
{
|
|
System.Threading.Interlocked.Increment(ref _connectServerHandleMisses);
|
|
|
|
connectHandle = new SamServerHandle(SamServerAccess.GenericRead | SamServerAccess.GenericExecute);
|
|
|
|
if (connectHandle != null)
|
|
_connectServerHandle = new WeakReference<SamServerHandle>(connectHandle);
|
|
}
|
|
|
|
return connectHandle;
|
|
}
|
|
}
|
|
|
|
public static int ConnectServerHandleMisses
|
|
{
|
|
get { return _connectServerHandleMisses; }
|
|
}
|
|
|
|
public delegate bool EnumDomainsDelegate(string name);
|
|
|
|
/// <summary>
|
|
/// Opens the local SAM server.
|
|
/// </summary>
|
|
/// <param name="access">The desired access to the server.</param>
|
|
public SamServerHandle(SamServerAccess access)
|
|
: this(null, access)
|
|
{ }
|
|
|
|
/// <summary>
|
|
/// Opens a SAM server.
|
|
/// </summary>
|
|
/// <param name="serverName">The name of the server.</param>
|
|
/// <param name="access">The desired access to the server.</param>
|
|
public SamServerHandle(string serverName, SamServerAccess access)
|
|
{
|
|
NtStatus status;
|
|
ObjectAttributes oa = new ObjectAttributes();
|
|
UnicodeString serverNameStr;
|
|
IntPtr handle;
|
|
|
|
serverNameStr = new UnicodeString(serverName);
|
|
|
|
try
|
|
{
|
|
if ((status = Win32.SamConnect(
|
|
ref serverNameStr,
|
|
out handle,
|
|
access,
|
|
ref oa
|
|
)) >= NtStatus.Error)
|
|
Win32.Throw(status);
|
|
}
|
|
finally
|
|
{
|
|
serverNameStr.Dispose();
|
|
}
|
|
|
|
this.Handle = handle;
|
|
}
|
|
|
|
public void EnumDomains(EnumDomainsDelegate callback)
|
|
{
|
|
NtStatus status;
|
|
int enumerationContext = 0;
|
|
IntPtr buffer;
|
|
int count;
|
|
|
|
while (true)
|
|
{
|
|
status = Win32.SamEnumerateDomainsInSamServer(
|
|
this,
|
|
ref enumerationContext,
|
|
out buffer,
|
|
0x100,
|
|
out count
|
|
);
|
|
|
|
if (status >= NtStatus.Error)
|
|
Win32.Throw(status);
|
|
if (count == 0)
|
|
break;
|
|
|
|
using (var bufferAlloc = new SamMemoryAlloc(buffer))
|
|
{
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
var data = bufferAlloc.ReadStruct<SamSidEnumeration>(i);
|
|
|
|
if (!callback(data.Name.Read()))
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public string[] GetDomains()
|
|
{
|
|
List<string> domains = new List<string>();
|
|
|
|
this.EnumDomains((name) =>
|
|
{
|
|
domains.Add(name);
|
|
return true;
|
|
});
|
|
|
|
return domains.ToArray();
|
|
}
|
|
|
|
public Sid LookupDomain(string name)
|
|
{
|
|
NtStatus status;
|
|
UnicodeString nameStr;
|
|
IntPtr domainId;
|
|
|
|
nameStr = new UnicodeString(name);
|
|
|
|
try
|
|
{
|
|
if ((status = Win32.SamLookupDomainInSamServer(
|
|
this,
|
|
ref nameStr,
|
|
out domainId
|
|
)) >= NtStatus.Error)
|
|
Win32.Throw(status);
|
|
}
|
|
finally
|
|
{
|
|
nameStr.Dispose();
|
|
}
|
|
|
|
using (var domainIdAlloc = new SamMemoryAlloc(domainId))
|
|
return new Sid(domainIdAlloc);
|
|
}
|
|
|
|
public void Shutdown()
|
|
{
|
|
NtStatus status;
|
|
|
|
if ((status = Win32.SamShutdownSamServer(this)) >= NtStatus.Error)
|
|
Win32.Throw(status);
|
|
}
|
|
}
|
|
}
|