private void WalkMachineGroups() { IntPtr enumbuffer = IntPtr.Zero; uint entriesread = 0; uint totalentries = 0; uint rc = NetApi32.NetLocalGroupEnum(null, 0, ref enumbuffer, 0xFFFFFFFF, out entriesread, out totalentries, IntPtr.Zero); this.Success = (rc == 0); if (this.Success) { for (int cnt = 0; cnt < totalentries; cnt++) { LOCALGROUP_INFO_0 ginfo = (LOCALGROUP_INFO_0)Marshal.PtrToStructure(enumbuffer, typeof(LOCALGROUP_INFO_0)); HandleLocalGroup(ginfo); try { enumbuffer = (IntPtr)((Int64)enumbuffer + Marshal.SizeOf(typeof(LOCALGROUP_INFO_0))); } catch (Exception) { break; } } NetApi32.NetApiBufferFree(enumbuffer); } }
public static DCInfo DsGetDCName(string domainDNS, string siteName, NetApi32.DS_FLAGS flags) { DCInfo ret = new DCInfo() { Success = false }; int iret = -1; IntPtr ipinfo = IntPtr.Zero; NetApi32.DOMAIN_CONTROLLER_INFO stcinfo = default(NetApi32.DOMAIN_CONTROLLER_INFO); if (domainDNS == null) { domainDNS = String.Empty; } try { iret = NetApi32.DsGetDcName(null, domainDNS, new Guid(), siteName, flags, ref ipinfo); } catch (Win32Exception Win32Ex) { ret.ErrorString = Win32Ex.ErrorCode + " (Win32: " + Win32Ex.Message + ")"; } catch (Exception ex) { ret.ErrorString = ex.Message; } if (iret == 0) { ret.Success = true; stcinfo = (NetApi32.DOMAIN_CONTROLLER_INFO)Marshal.PtrToStructure(ipinfo, typeof(NetApi32.DOMAIN_CONTROLLER_INFO)); NetApi32.NetApiBufferFree(ipinfo); ReturnDCINFO(stcinfo, ref ret); } else { HandleError(iret, ref ret, domainDNS, siteName, flags.ToString()); } return(ret); }
private void HandleLocalGroup(LOCALGROUP_INFO_0 ginfo) { IntPtr enumbuffer = IntPtr.Zero; uint entriesread = 0; uint totalentries = 0; string sid = GetAccountSid(ginfo.lgrpi0_name); GroupInfo group = new GroupInfo(sid, ginfo.lgrpi0_name); uint rc = NetApi32.NetLocalGroupGetMembers(null, group.NTName, 1, ref enumbuffer, 0xFFFFFFFF, out entriesread, out totalentries, IntPtr.Zero); this.Success = (rc == 0); if (this.Success) { for (int cnt = 0; cnt < totalentries; cnt++) { LOCALGROUP_MEMBERS_INFO_1 member = (LOCALGROUP_MEMBERS_INFO_1)Marshal.PtrToStructure(enumbuffer, typeof(LOCALGROUP_MEMBERS_INFO_1)); string msid = GetAccountSid(member.lgrmi1_sid); SidInfo sinfo = new SidInfo(msid, member.lgrmi1_name, member.lgrmi1_sidusage); group.Members.Add(sinfo); group.SidList.Add(msid); try { enumbuffer = (IntPtr)((Int64)enumbuffer + Marshal.SizeOf(typeof(LOCALGROUP_MEMBERS_INFO_1))); } catch (Exception) { break; } } this.Groups.AddSafe(group); NetApi32.NetApiBufferFree(enumbuffer); } }
public static List <DomainControllerHelper> DnsGetDCs(string domainDNS, string siteName, string forestName, NetApi32.DS_OPEN_FLAGS flags, NetApi32.DS_OPTION_FLAGS siteOptions, bool doUdpPing = false) { List <DomainControllerHelper> ret = new List <DomainControllerHelper> { }; DomainControllerHelper errdc = new DomainControllerHelper() { Success = false }; int iret = -1; IntPtr iphandle = IntPtr.Zero; try { if (domainDNS == null) { domainDNS = Environment.ExpandEnvironmentVariables("%USERDNSDOMAIN%"); } iret = NetApi32.DsGetDcOpen(domainDNS, siteOptions, siteName, IntPtr.Zero, forestName, flags, out iphandle); } catch (Win32Exception Win32Ex) { errdc.ErrorString = Win32Ex.ErrorCode + " (Win32: " + Win32Ex.Message + ")"; } catch (Exception ex) { errdc.ErrorString = ex.Message; } if (iret == 0) { string dnsName = null; IntPtr socketcnt = IntPtr.Zero; IntPtr sockets = IntPtr.Zero; while ((iret == (int)NetApi32.ERROR_LIST.ERROR_SUCCESS) || (iret == (int)NetApi32.ERROR_LIST.ERROR_FILEMARK_DETECTED)) { errdc = new DomainControllerHelper() { Success = false }; try { iret = NetApi32.DsGetDcNext(iphandle, ref socketcnt, out sockets, out dnsName); } catch (Win32Exception Win32Ex) { errdc.ErrorString = Win32Ex.ErrorCode + " (Win32: " + Win32Ex.Message + ")"; } catch (Exception ex) { errdc.ErrorString = ex.Message; } if ((iret == (int)NetApi32.ERROR_LIST.ERROR_SUCCESS) || (iret == (int)NetApi32.ERROR_LIST.ERROR_FILEMARK_DETECTED)) { if (doUdpPing) { DateTime start = DateTime.Now; GlobalEventHandler.RaiseMessageOccured(String.Format("UDPPing against {0} ({1})", dnsName, domainDNS)); ret.Add(DomainControllerHelper.UDPPing(dnsName)); double dur = DateTime.Now.Subtract(start).TotalMilliseconds; GlobalEventHandler.RaiseMessageOccured(String.Format("UDPPing {0} in {1} ms", dnsName, dur.ToString())); } else { ret.Add(new DomainControllerHelper(domainDNS, dnsName)); } } else { if (ret.Count == 0) { HandleError(iret, ref errdc, domainDNS, siteName, flags.ToString()); ret.Add(errdc); } } } } else { if (ret.Count == 0) { HandleError(iret, ref errdc, domainDNS, siteName, flags.ToString()); ret.Add(errdc); } } if (iphandle != IntPtr.Zero) { NetApi32.DsGetDcClose(iphandle); } return(ret); }