Skip to content

Commit

Permalink
Merge pull request #51 from sysadminanywhere/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
sysadminanywhere authored Nov 3, 2023
2 parents b340b26 + b15beae commit baa2e0a
Show file tree
Hide file tree
Showing 16 changed files with 262 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ src/Sysadmin/bin
src/Sysadmin/obj
src/Sysadmin/.sonarqube
src/Sysadmin/sonar.bat
src/GenerateADObjects/bin
src/GenerateADObjects/obj
*.pubxml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FastReport.OpenSource" Version="2023.3.0" />
<PackageReference Include="FastReport.OpenSource" Version="2023.3.9" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup>

Expand Down
18 changes: 18 additions & 0 deletions src/GenerateADObjects/GenerateADObjects.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bogus" Version="34.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SysAdmin.ActiveDirectory\SysAdmin.ActiveDirectory.csproj" />
</ItemGroup>

</Project>
124 changes: 124 additions & 0 deletions src/GenerateADObjects/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using Bogus;
using SysAdmin.ActiveDirectory.Models;
using SysAdmin.ActiveDirectory.Repositories;
using SysAdmin.ActiveDirectory.Services.Ldap;

internal class Program
{

private static IServer Server = null;
private static ICredential Credential = null;


private static async Task Main(string[] args)
{

//Server = new Server("192.168.245.129", 389);
//Credential = new Credential("admin", "Secret2#");

try
{
for (int i = 0; i < 999; i++)
{
await GenerateUser("OU=Users,OU=Test1,DC=example,DC=com");
}

//await GenerateContact("OU=Contacts,OU=Test1,DC=example,DC=com");
//await GenerateComputer("OU=Computers,OU=Test1,DC=example,DC=com");
} catch (Exception ex)
{
Console.WriteLine(ex);
}

Console.WriteLine("Press any key...");
Console.ReadLine();
}

private static async Task GenerateUser(string distinguishedName)
{
var testUsers = new Faker<UserEntry>()

.RuleFor(u => u.FirstName, (f, u) => f.Name.FirstName())
.RuleFor(u => u.LastName, (f, u) => f.Name.LastName())
.RuleFor(u => u.SamAccountName, (f, u) => f.Internet.UserName(u.FirstName, u.LastName))
.RuleFor(u => u.DisplayName, (f, u) => u.FirstName + " " + u.LastName)

.FinishWith((f, u) =>
{
Console.WriteLine(u.DisplayName);
});

using (var ldap = new LdapService(Server, Credential))
{
using (var usersRepository = new UsersRepository(ldap))
{
var user = testUsers.Generate();
user.DistinguishedName = distinguishedName;

if (string.IsNullOrEmpty(user.CN))
user.CN = user.DisplayName;

if (string.IsNullOrEmpty(user.Name))
user.Name = user.DisplayName;

await usersRepository.AddAsync(distinguishedName, user);
}
}
}

private static async Task GenerateContact(string distinguishedName)
{
var testContacts = new Faker<ContactEntry>()

.RuleFor(u => u.FirstName, (f, u) => f.Name.FirstName())
.RuleFor(u => u.LastName, (f, u) => f.Name.LastName())
.RuleFor(u => u.DisplayName, (f, u) => u.FirstName + " " + u.LastName)

.FinishWith((f, u) =>
{
Console.WriteLine(u.DisplayName);
});

using (var ldap = new LdapService(Server, Credential))
{
using (var contactsRepository = new ContactsRepository(ldap))
{
var contact = testContacts.Generate();
contact.DistinguishedName = distinguishedName;

if (string.IsNullOrEmpty(contact.CN))
contact.CN = contact.DisplayName;

if (string.IsNullOrEmpty(contact.Name))
contact.Name = contact.DisplayName;

await contactsRepository.AddAsync(contact);
}
}
}

private static async Task GenerateComputer(string distinguishedName)
{
var testComputers = new Faker<ComputerEntry>()

.RuleFor(u => u.CN, (f, u) => f.Random.Word())
.RuleFor(u => u.Description, (f, u) => f.Random.Words())

.FinishWith((f, u) =>
{
Console.WriteLine(u.Name);
});

using (var ldap = new LdapService(Server, Credential))
{
using (var computersRepository = new ComputersRepository(ldap))
{
var computer = testComputers.Generate();
computer.DistinguishedName = distinguishedName;

await computersRepository.AddAsync(computer, false);
}
}
}

}
6 changes: 3 additions & 3 deletions src/IntegrationsTests/IntegrationsTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion src/SetupProject/Product.wxs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"><?define Sysadmin_TargetDir=$(var.Sysadmin.TargetDir)?>
<Product Id="*" Name="Sysadmin" Language="1033" Version="10.3.0.0" Manufacturer="Sysadmin Anywhere" UpgradeCode="ef8f7793-3a07-4cbb-ac9b-52eff7e8fb22">
<Product Id="*" Name="Sysadmin" Language="1033" Version="10.4.0.0" Manufacturer="Sysadmin Anywhere" UpgradeCode="ef8f7793-3a07-4cbb-ac9b-52eff7e8fb22">
<Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />

<MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
Expand Down
33 changes: 33 additions & 0 deletions src/SysAdmin.ActiveDirectory/Repositories/UsersRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,39 @@ public async Task<List<UserEntry>> ListAsync()
return null;
}

public async Task AddAsync(string distinguishedName, UserEntry user)
{
if (user == null)
throw new ArgumentNullException(nameof(user));

if (string.IsNullOrEmpty(user.CN))
throw new ArgumentNullException(nameof(user.CN));

if (string.IsNullOrEmpty(user.UserPrincipalName))
user.UserPrincipalName = user.SamAccountName + "@" + ldapService.DomainName;

List<string> attributes = new List<string>
{
"displayName",
"initials",
"givenName",
"sn",
"sAMAccountName",
"userPrincipalName"
};

if (string.IsNullOrEmpty(distinguishedName))
{
string cn = "cn=" + user.CN + "," + new ADContainers(ldapService).GetUsersContainer();
await ldapService.AddAsync(LdapResolver.GetLdapEntry(cn, user, attributes));
}
else
{
string cn = "cn=" + user.CN + "," + distinguishedName;
await ldapService.AddAsync(LdapResolver.GetLdapEntry(cn, user, attributes));
}
}

public async Task<UserEntry?> ModifyAsync(UserEntry user)
{
if (user == null)
Expand Down
8 changes: 8 additions & 0 deletions src/SysAdmin.ActiveDirectory/Services/Ldap/Credential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
public class Credential : ICredential
{

public Credential() { }

public Credential(string username, string password)
{
UserName = username;
Password = password;
}

public string UserName { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;

Expand Down
36 changes: 22 additions & 14 deletions src/SysAdmin.ActiveDirectory/Services/Ldap/LdapService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

using LdapForNet;
using System.Collections.Generic;
using static LdapForNet.Native.Native;

namespace SysAdmin.ActiveDirectory.Services.Ldap
Expand Down Expand Up @@ -101,7 +102,7 @@ public async Task<List<LdapEntry>> SearchAsync(string filter)
return await SearchAsync(DefaultNamingContext, filter);
}

public async Task<List<LdapEntry>> SearchAsync(string path, string filter)
public async Task<List<LdapEntry>> SearchAsync(string path, string filter, LdapSearchScope scope = LdapSearchScope.LDAP_SCOPE_SUBTREE)
{
if (ldapConnection == null)
throw new ArgumentNullException(nameof(ldapConnection));
Expand All @@ -112,25 +113,32 @@ public async Task<List<LdapEntry>> SearchAsync(string path, string filter)
if (string.IsNullOrEmpty(filter))
throw new ArgumentNullException(nameof(filter));

var entries = await ldapConnection.SearchAsync(path, filter);
//var entries = await ldapConnection.SearchAsync(path, filter, scope: scope);
//return entries.ToList();

return entries.ToList();
}
var directoryRequest = new SearchRequest(path, filter, scope);
var pageSize = 100;

public async Task<List<LdapEntry>> SearchAsync(string path, string filter, LdapSearchScope scope = LdapSearchScope.LDAP_SCOPE_SUBTREE)
{
if (ldapConnection == null)
throw new ArgumentNullException(nameof(ldapConnection));
var vlvRequestControl = new VlvRequestControl(0, pageSize - 1, 1);
directoryRequest.Controls.Add(new SortRequestControl("cn", false));
directoryRequest.Controls.Add(vlvRequestControl);

if (string.IsNullOrEmpty(path))
throw new ArgumentNullException(nameof(path));
List<LdapEntry> results = new List<LdapEntry>();

if (string.IsNullOrEmpty(filter))
throw new ArgumentNullException(nameof(filter));
while (true)
{
var response = (SearchResponse)ldapConnection.SendRequest(directoryRequest);
results.AddRange(response.Entries.Select(_ => _.ToLdapEntry()).ToList());

var entries = await ldapConnection.SearchAsync(path, filter, scope: scope);
var vlvResponseControl = (VlvResponseControl)response.Controls.Single(_ => _.GetType() == typeof(VlvResponseControl));
vlvRequestControl.Offset += pageSize;
if (vlvRequestControl.Offset > vlvResponseControl.ContentCount)
{
break;
}
}

return entries.ToList();
return results;
}

public async Task<ModifyResponse> SendRequestAsync(ModifyRequest modifyRequest)
Expand Down
6 changes: 6 additions & 0 deletions src/SysAdmin.ActiveDirectory/Services/Ldap/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public Server(string server)
ServerName = server;
}

public Server(string server, int port)
{
ServerName = server;
Port = port;
}

public string ServerName { get; set; } = string.Empty;
public int Port { get; set; } = 389;
public bool IsSSL { get; set; } = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="LdapForNet" Version="2.7.15" />
<PackageReference Include="System.DirectoryServices.AccountManagement" Version="7.0.0" />
<PackageReference Include="System.DirectoryServices.AccountManagement" Version="7.0.1" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Sysadmin.WMI/Sysadmin.WMI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Management" Version="7.0.0" />
<PackageReference Include="System.Management" Version="7.0.2" />
</ItemGroup>

</Project>
27 changes: 27 additions & 0 deletions src/Sysadmin.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sysadmin", "Sysadmin\Sysadm
EndProject
Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "SetupProject", "SetupProject\SetupProject.wixproj", "{E940BCD5-6B6C-4C3D-AAC2-D906F17FB184}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GenerateADObjects", "GenerateADObjects\GenerateADObjects.csproj", "{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
All|Any CPU = All|Any CPU
Expand Down Expand Up @@ -185,6 +187,30 @@ Global
{E940BCD5-6B6C-4C3D-AAC2-D906F17FB184}.Release|x64.Build.0 = Release|x86
{E940BCD5-6B6C-4C3D-AAC2-D906F17FB184}.Release|x86.ActiveCfg = Release|x86
{E940BCD5-6B6C-4C3D-AAC2-D906F17FB184}.Release|x86.Build.0 = Release|x86
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.All|Any CPU.ActiveCfg = Debug|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.All|Any CPU.Build.0 = Debug|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.All|arm64.ActiveCfg = Debug|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.All|arm64.Build.0 = Debug|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.All|x64.ActiveCfg = Debug|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.All|x64.Build.0 = Debug|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.All|x86.ActiveCfg = Debug|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.All|x86.Build.0 = Debug|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.Debug|arm64.ActiveCfg = Debug|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.Debug|arm64.Build.0 = Debug|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.Debug|x64.ActiveCfg = Debug|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.Debug|x64.Build.0 = Debug|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.Debug|x86.ActiveCfg = Debug|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.Debug|x86.Build.0 = Debug|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.Release|Any CPU.Build.0 = Release|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.Release|arm64.ActiveCfg = Release|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.Release|arm64.Build.0 = Release|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.Release|x64.ActiveCfg = Release|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.Release|x64.Build.0 = Release|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.Release|x86.ActiveCfg = Release|Any CPU
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -196,6 +222,7 @@ Global
{14BD1EBC-81DA-48E5-ACE8-BCFFD16C40C8} = {68C6B0AE-673B-4A5B-8596-6993081E7965}
{3A0A75EF-D7F0-4788-A177-F8BED84CDA6A} = {68C6B0AE-673B-4A5B-8596-6993081E7965}
{E940BCD5-6B6C-4C3D-AAC2-D906F17FB184} = {68C6B0AE-673B-4A5B-8596-6993081E7965}
{DAEE3FE2-FBAE-4E76-8BDE-5B9967D58ACE} = {011E69B9-B02B-4226-B558-85D222133CCE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {ABCBFF47-219B-4192-920A-5F6EEB0CD40F}
Expand Down
7 changes: 6 additions & 1 deletion src/Sysadmin/Properties/PublishProfiles/FolderProfile.pubxml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net6.0-windows\publish\</PublishDir>
<PublishDir>bin\Release\net6.0-windows\publish\win-x64\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
<TargetFramework>net6.0-windows</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>false</PublishSingleFile>
<PublishReadyToRun>false</PublishReadyToRun>
</PropertyGroup>
</Project>
Loading

0 comments on commit baa2e0a

Please sign in to comment.