/* * 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 . */ using System; using System.Collections.Generic; using ProcessHacker.Common; using ProcessHacker.Native.Api; using ProcessHacker.Native.Security; namespace ProcessHacker.Native.Objects { /// /// Represents a handle to a SAM server. /// public sealed class SamServerHandle : SamHandle { private static WeakReference _connectServerHandle; private static int _connectServerHandleMisses = 0; public static SamServerHandle ConnectServerHandle { get { WeakReference 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(connectHandle); } return connectHandle; } } public static int ConnectServerHandleMisses { get { return _connectServerHandleMisses; } } public delegate bool EnumDomainsDelegate(string name); /// /// Opens the local SAM server. /// /// The desired access to the server. public SamServerHandle(SamServerAccess access) : this(null, access) { } /// /// Opens a SAM server. /// /// The name of the server. /// The desired access to the server. 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(i); if (!callback(data.Name.Read())) return; } } } } public string[] GetDomains() { List domains = new List(); 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); } } }