-
Notifications
You must be signed in to change notification settings - Fork 39
/
Program.cs
130 lines (122 loc) · 4.95 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using LdapForNet;
using static LdapForNet.Native.Native;
namespace LdapExample
{
class Program
{
/// <summary>
/// LdapSearch
/// </summary>
/// <example>
/// LdapExample --auth=GSSAPI --host=v04.example.com --base="dc=v04,dc=example,dc=com" --filter="(objectclass=*)" --port=389
/// </example>
/// <example>
/// LdapExample --auth=Simple --host=ldap.forumsys.com --base="dc=example,dc=com" --filter="(objectclass=*)" --who="cn=read-only-admin,dc=example,dc=com" --password=password --port=389
/// </example>
/// <param name="args"></param>
static void Main(string[] args)
{
var cmds = ParseCommandLine(args);
cmds.TryGetValue("host", out var host);
cmds.TryGetValue("auth", out var authString);
cmds.TryGetValue("base", out var @base);
cmds.TryGetValue("filter", out var filter);
cmds.TryGetValue("port", out var portStr);
int.TryParse(portStr, out var port);
var auth = authString == LdapAuthMechanism.GSSAPI ? LdapAuthMechanism.GSSAPI : LdapAuthMechanism.SIMPLE;
host = host ?? "ldap.forumsys.com";
@base = @base ?? "dc=example,dc=com";
filter = filter ?? "(objectclass=*)";
port = port > 0 ? port : 389;
try
{
var token = new CancellationTokenSource();
Console.CancelKeyPress+=(sender, eventArgs) => token.Cancel();
while (!token.IsCancellationRequested)
{
UsingOpenLdap(auth, host, @base, port, filter, cmds).Wait();
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.WriteLine("End");
}
private static Dictionary<string, string> ParseCommandLine(string[] args)
{
var pattern = "^--([^=\"]*)=\"?(.*)\"?$";
return args.Select(_ => Regex.Matches(_, pattern, RegexOptions.IgnoreCase).Cast<Match>().FirstOrDefault()?.Groups)
.Where(_ => _ != null)
.ToDictionary(_ => _[1].Value, _ => _[2].Value);
}
private static async Task UsingOpenLdap(string authType, string host, string @base, int port, string filter, IDictionary<string, string> cmds)
{
Console.WriteLine($"{nameof(authType)}:{authType}; {nameof(host)}:{host}; {nameof(@base)}:{@base}; {nameof(port)}:{port} ");
using (var cn = new LdapConnection())
{
cn.Connect(host, port);
if (authType == LdapAuthMechanism.GSSAPI)
{
await cn.BindAsync();
}
else
{
cmds.TryGetValue("who", out var who);
cmds.TryGetValue("password", out var password);
who = who ?? "cn=read-only-admin,dc=example,dc=com";
password = password ?? "password";
cn.Bind(LdapAuthMechanism.SIMPLE,who,password);
}
IList<LdapEntry> entries;
if (cmds.TryGetValue("sid", out var sid))
{
entries = await cn.SearchBySidAsync(@base, sid);
}
else
{
var rootDse = cn.GetRootDse();
PrintEntry(rootDse);
var searchRequest = new SearchRequest(@base, filter,
LdapSearchScope.LDAP_SCOPE_SUBTREE)
{
AttributesOnly = false,
TimeLimit = TimeSpan.Zero,
Controls =
{
new PageResultRequestControl(500)
{
IsCritical = true
}
}
};
var searchResponse = ((SearchResponse) (await cn.SendRequestAsync(searchRequest)));
entries = searchResponse.Entries.Select(_=>_.ToLdapEntry()).ToList();
}
foreach (var ldapEntry in entries)
{
PrintEntry(ldapEntry);
}
}
}
private static void PrintEntry(LdapEntry entry)
{
if (entry == null)
{
return;
}
Console.WriteLine($"dn: {entry.Dn}");
foreach (var pair in entry.Attributes.SelectMany(_ => _.Value.Select(x => new KeyValuePair<string, string>(_.Key, x))))
{
Console.WriteLine($"{pair.Key}: {pair.Value}");
}
Console.WriteLine();
}
}
}