/// <summary> /// Creates a local user on the host. /// </summary> /// <param name="name"></param> public NTLocalUser CreateLocalUser(string name, string password) { var newUser = new WinAPI.NETAPI32.USER_INFO_1 { usri1_name = name, usri1_password = password, usri1_priv = (int)UserPrivilege.User, usri1_home_dir = null, comment = null, usri1_script_path = null }; // Create an new instance of the USER_INFO_1 struct var result = WinAPI.NETAPI32.NetUserAdd(this.NTCompatibleHostName, 1, ref newUser, 0); if (result != 0) { throw new NetApiException( result, "Unable to create local user '{0}' on host '{1}'", name, Host ); } var user = new NTLocalUser { Host = Host, Name = name }; user.Refresh(); return(user); }
/// <summary> /// Returns the local users on the host. /// </summary> /// <returns></returns> public NTLocalUser[] GetLocalUsers() { var bufPtr = IntPtr.Zero; var localUsers = new List <NTLocalUser>(); try { int entriesRead; int totalEntries; int resumeHandle; var result = WinAPI.NETAPI32.NetUserEnum( this.NTCompatibleHostName, 3, 2, out bufPtr, -1, out entriesRead, out totalEntries, out resumeHandle ); if (result != 0) { throw new NetApiException( result, "Failed to enumerate local users on host '{0}'", Host ); } var structSize = Marshal.SizeOf(typeof(WinAPI.NETAPI32.USER_INFO_3)); var startAddr = bufPtr.ToInt64(); var endAddr = startAddr + entriesRead * structSize; for (var offset = startAddr; offset < endAddr; offset += structSize) { var userInfo = (WinAPI.NETAPI32.USER_INFO_3)Marshal.PtrToStructure( new IntPtr(offset), typeof(WinAPI.NETAPI32.USER_INFO_3) ); var user = new NTLocalUser { Host = Host, Name = !string.IsNullOrEmpty(userInfo.usri3_name) ? userInfo.usri3_name : string.Empty }; user.Refresh(); localUsers.Add(user); } } catch (Exception error) { #warning Need to fix this //throw new ApplicationException( } finally { if (bufPtr != IntPtr.Zero) { WinAPI.NETAPI32.NetApiBufferFree(bufPtr); } } return(localUsers.ToArray()); }
/// <summary> /// Gets all the members belonging to this group. /// </summary> /// <returns>List of members.</returns> public NTObject[] GetMembers() { var members = new List <NTObject>(); var memberInfoPtr = IntPtr.Zero; try { var resumeHandle = 0; const uint prefmaxlen1 = 0xffffffff; uint entriesread1, totalentries1; var result = WinAPI.NETAPI32.NetLocalGroupGetMembers(this.NTCompatibleHostName, this.Name, 2, out memberInfoPtr, prefmaxlen1, out entriesread1, out totalentries1, out resumeHandle); if (result != 0) { throw new NetApiException( result, "Unable to get members of group '{0}' on host '{1}'", Name, Host ); } var structSize = Marshal.SizeOf(typeof(WinAPI.NETAPI32.LOCALGROUP_MEMBERS_INFO_2)); var startAddr = memberInfoPtr.ToInt64(); var endAddr = startAddr + (int)totalentries1 * structSize; for (var offset = startAddr; offset < endAddr; offset += structSize) { var memberInfo = (WinAPI.NETAPI32.LOCALGROUP_MEMBERS_INFO_2)Marshal.PtrToStructure( new IntPtr(offset), typeof(WinAPI.NETAPI32.LOCALGROUP_MEMBERS_INFO_2) ); var memberDomainName = Marshal.PtrToStringAuto(memberInfo.lgrmi2_domainandname); var memberDomain = SecurityTool.GetDomainFromDomainUserString(memberDomainName); var memberName = SecurityTool.GetUserFromDomainUserString(memberDomainName); var memberSid = new SecurityIdentifier(memberInfo.lgrmi2_sid); var memberSidUsage = (WinAPI.ADVAPI32.SidNameUse)memberInfo.lgrmi2_sidusage; if (memberSidUsage == WinAPI.ADVAPI32.SidNameUse.DeletedAccount || memberSidUsage == WinAPI.ADVAPI32.SidNameUse.Unknown) { #region It's Dangling object members.Add( new NTDanglingObject( Host, memberSid, memberSidUsage ) ); #endregion } else if (memberDomain.ToUpper() != this.Host.ToUpper()) { #region It's a remote object members.Add( new NTRemoteObject( Host, memberDomain, memberName, memberSid, memberSidUsage ) ); #endregion } else { #region It's a local object switch (memberSidUsage) { case WinAPI.ADVAPI32.SidNameUse.User: var user = new NTLocalUser { Host = Host, Name = memberName, SID = memberSid }; user.Refresh(); members.Add(user); break; case WinAPI.ADVAPI32.SidNameUse.Group: case WinAPI.ADVAPI32.SidNameUse.WellKnownGroup: var group = new NTLocalGroup(); group.Host = Host; group.Name = memberName; group.Refresh(); members.Add(group); break; default: members.Add( new NTDanglingObject( Host, memberSid, memberSidUsage ) ); break; } #endregion } } } finally { if (memberInfoPtr != IntPtr.Zero) { WinAPI.NETAPI32.NetApiBufferFree(memberInfoPtr); } } return(members.ToArray()); }