-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
634 lines (538 loc) · 20.4 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*
* Created by SharpDevelop.
* User: c3rebro
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*
* credits and many thanks goes to Ryan Dunn: http://dunnry.com/blog/AsynchronousLDAPSearchingWithSystemDirectoryServicesProtocols.aspx
*
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.DirectoryServices.Protocols;
using Microsoft.Win32.SafeHandles;
namespace LDAP2CSV
{
class Program : IDisposable
{
static ManualResetEvent _resetEvent = new ManualResetEvent(false);
static Guid _guid;
static List<string> _arguments;
static int _resultsCounter;
static string[] attr;
public static int Main(string[] args){
try {
Version v = Assembly.GetExecutingAssembly().GetName().Version;
//fill list of arguments
_arguments = new List<string>();
foreach(string arg in args)
{
_arguments.Add(arg.Replace("\"",string.Empty));
}
// some fancy window title
Console.Title = string.Format("LDAP2CSV v{0}.{1}.{2}",v.Major, v.Minor, v.Build);
//want to create a lcv file? exit after job is done
if(_arguments.Contains("-c")){
Crypto.Protect(_arguments[_arguments.IndexOf("-c") + 1]);
return 0;
}
//messing around with parameters? dont try
if(_arguments.Contains("-u") && ! _arguments.Contains("-p")){
throw new ArgumentException("-u and -p parameter must be specified in conjuction");
}
// check existance of desirend amount of parameters at least: server:port/rootdn
if(_arguments.Count < 1) {
Console.WriteLine(
"LDAP2CSV usage:\n" +
"\n" +
"ldap2csv.exe LDAP://10.0.0.4:389/ou=people,dc=example,dc=de \n -u username -p password -f (&(ou=people)) -a sn,givenName,ou " +
"\n -o \"D:\\Path\\To\\Destination.csv -e UTF8 -s ;\"\n" +
"\n" +
"optional parmeter -a: ldap attributes\n" +
"supported attributes are: comma seperated list of attributes\n" +
"Hint: If omitted, attribute will be set to wildcard \"*\"\n" +
"usage: -a sn,givenName\n" +
"\n" +
"optional parmeter -f: ldap search filter\n" +
"supported attributes are: standard ldap query\n" +
"Hint: If omitted, filter is set to \"objectClass=*\"\n" +
"usage: -f ou=people\n" +
"\n" +
"optional parmeter -u: username\n" +
"supported attributes are: ldap username\n" +
"Hint: refer -c and -C parameter for encrypted credentials\n" +
"Hint: Anonymous Login is used if -u, -p and -C are omitted\n" +
"usage: -u cn=admin,dc=example,dc=de\n" +
"\n" +
"optional parmeter -p: plain password\n" +
"supported attributes are: ldap userpassword\n" +
"Hint: -u parameter is mandatory if -p should be used \n" +
"Hint: refer -c and -C parameter for encrypted credentials\n" +
"usage: -p password\n" +
"\n" +
"optional parmeter -e: encoding\n" +
"supported attributes are: UTF8, ASCII(=ANSI) and Unicode\n" +
"Hint: If omitted, encoding remain system default\n" +
"usage: -e UTF8\n" +
"\n" +
"optional parmeter -s: csv seperator char\n" +
"supported attributes: maybe all ASCII characters" +
"Hint: If omitted, comma will be used as seperator\n" +
"usage: -s ;\n" +
"\n" +
"optional parmeter -v: ldap protocol version\n" +
"supported attributes: 2, 3\n" +
"Hint: If omitted, version 3 is assumed\n" +
"usage: -v 2\n" +
"\n" +
"optional parmeter -ssl: use ssl encryption\n" +
"supported attributes: none\n" +
"Hint: If omitted, basic authentication will be used\n" +
"usage: -ssl\n" +
"\n" +
"optional parmeter -tls: use tls encryption\n" +
"supported attributes: none\n" +
"Hint: If omitted, basic authentication will be used\n" +
"usage: -tls\n" +
"\n" +
"optional parmeter -k: use kerberos encryption\n" +
"supported attributes: none\n" +
"Hint: If omitted, basic authentication will be used\n" +
"usage: -k\n" +
"\n" +
"optional parmeter -t: timeout in minutes before a connection\n" +
"or a ldap query will raise a timeout exception\n" +
"supported attributes: 1 - n\n" +
"Hint: If omitted, timeout will be set to 10 minutes\n" +
"usage: -t 5\n" +
"\n" +
"optional parameter -c: encryption of credentials\n" +
"this will create an encrypted \"cred.lcv\" in\n" +
"the root directory of ldap2csv.exe where the credentials\n" +
"will be stored safely. May need elevated privileges \"Run As Administrator\"\n" +
"supported attributes: \"username|password\"\n" +
"Hint: Be aware to use the | character (pipe) to seperate user and password.\n" +
"Hint: Be aware to set the attribute in quotes.\n" +
"Hint: To use the created credential file see \"-C\" parameter.\n" +
"usage: ldap2csv.exe -c \"username|password\"\n" +
"\n" +
"optional paremeter -C: use encrypted credential file\n" +
"this will tell ldap2csv.exe to search for the \"cred.lcv\" file\n" +
"created with the -c parameter above.\n" +
"supported attributes: none\n" +
"usage: ldap2csv.exe LDAP://10.0.0.4:389/ou=people,dc=example,dc=de -C -f (&(ou=people))\n" +
"-a sn,givenName,ou -o D:\\Path\\To\\Destination.csv\\\n" +
"\nImportant: cred.lcv file is bound to the environment where it was created and could not be transferred between different machines"
);
Console.Write(
"\n" +
"Press any key to exit . . . "
);
Console.ReadKey(true);
return 0;
}
else {
try {
//extract server and dn from ldap path attribute
string dn = _arguments[0].ToUpper().Replace("LDAP://",string.Empty)
.Replace("LDAPS://",string.Empty)
.Remove(0,_arguments[0].ToUpper()
.Replace("LDAP://",string.Empty).Replace("LDAPS://",string.Empty)
.IndexOf('/') +1)
.ToLower();
string server = _arguments[0].ToUpper().Replace("LDAP://",string.Empty)
.Replace("LDAPS://",string.Empty)
.Remove(_arguments[0].ToUpper()
.Replace("LDAP://",string.Empty).Replace("LDAPS://",string.Empty)
.IndexOf('/'), dn.Length + 1)
.ToLower();
LdapDirectoryIdentifier ldapDir = new LdapDirectoryIdentifier(server);
//prepare output directory if -o parameter was supplied
if(_arguments.Contains("-o")) {
// the argument right after the "-o" argument should be an existing directory as this will be used for the output file. create if not exists
if(!Directory.GetParent(_arguments[_arguments.IndexOf("-o") +1 ]).Exists)
Directory.CreateDirectory(Directory.GetParent(_arguments[_arguments.IndexOf("-o") +1 ]).ToString());
// try to remove previous csv file if it exists
if(File.Exists(_arguments[_arguments.IndexOf("-o") +1 ])) {
try {
File.Delete(_arguments[_arguments.IndexOf("-o") +1 ]);
}
catch(Exception ex) {
LogWriter.CreateLogEntry(string.Format("ERROR:{0} {1}", ex.Message, (ex.InnerException != null) ? ex.InnerException.Message : string.Empty));
}
}
}
using (LdapConnection ldapConnection = CreateConnection(ldapDir))
{
//use ssl?
ldapConnection.SessionOptions.SecureSocketLayer = _arguments.Contains("-ssl") ? true : false;
//use tls?
if(_arguments.Contains("-tls") && !_arguments.Contains("-ssl"))
ldapConnection.SessionOptions.StartTransportLayerSecurity(null);
//protocol version specified? use version 3 if not
ldapConnection.SessionOptions.ProtocolVersion = _arguments.Contains("-v") ? int.Parse(_arguments[_arguments.IndexOf("-v") +1 ]) : 3;
//do not chase referral
ldapConnection.SessionOptions.ReferralChasing = ReferralChasingOptions.None;
//You may need to try different types of Authentication depending on the setup
ldapConnection.AuthType = (_arguments.Contains("-u")||_arguments.Contains("-C")) ? AuthType.Basic : AuthType.Anonymous;
//use kerberos?
if(_arguments.Contains("-k"))
{
ldapConnection.AuthType = AuthType.Kerberos;
ldapConnection.SessionOptions.Sealing = true;
ldapConnection.SessionOptions.Signing = true;
}
//set or get timeout setting
ldapConnection.Timeout = new TimeSpan(0, _arguments.Contains("-t") ? int.Parse(_arguments[_arguments.IndexOf("-t") +1 ]) : 10, 0);
//use credentials from encrypted credential file?
if(_arguments.Contains("-C")) {
try{
//split back to user|pwd
string[] credentials = Crypto.Unprotect().Split('|');
NetworkCredential cred =
new NetworkCredential(credentials[0], credentials[1]);
//ldap connect
ldapConnection.Bind(cred);
}
catch(Exception e) {
LogWriter.CreateLogEntry(string.Format("ERROR:{0} {1}", e.Message, (e.InnerException != null) ? e.InnerException.Message : string.Empty));
ldapConnection.Dispose();
return 1;
}
}
//use plain authentication?
else if (_arguments.Contains("-u") && _arguments.Contains("-p")){
NetworkCredential cred =
new NetworkCredential(_arguments[_arguments.IndexOf("-u") +1 ],_arguments[_arguments.IndexOf("-p") +1 ]);
ldapConnection.Bind(cred);
}
//or try anonymously?
else {
NetworkCredential cred =
new NetworkCredential();
cred.Password = null;
cred.UserName = null;
ldapConnection.Bind(cred);
}
AsyncSearcher searcher = CreateSearcher(ldapConnection);
//this call is asynch, so we need to keep this main
//thread alive in order to see anything
//we can use the same searcher for multiple requests - we just have to track which one
//is which, so we can interpret the results later in our events.
attr = _arguments.Contains("-a") ? _arguments[_arguments.IndexOf("-a") +1 ].Split(new char[]{',',';'}) : new string[]{"*"};
_guid = searcher.BeginPagedSearch(
//set searchbase
string.IsNullOrWhiteSpace(dn) ? "*" : dn,
//set filter
_arguments.Contains("-f") ? _arguments[_arguments.IndexOf("-f") +1 ] : "(objectClass=*)",
//set attributes
attr,
150);
//we will use a reset event to signal when we are done (using Sleep() on
//current thread would work too...)
_resetEvent.WaitOne(); //wait for signal;
}
}
catch (Exception e) {
LogWriter.CreateLogEntry(string.Format("ERROR:{0} {1}", e.Message, (e.InnerException != null) ? e.InnerException.Message : string.Empty));
return 1;
}
}
return 0;
}
catch (Exception e) {
LogWriter.CreateLogEntry(string.Format("ERROR:{0} {1}", e.Message, (e.InnerException != null) ? e.InnerException.Message : string.Empty));
return 1;
}
}
static AsyncSearcher CreateSearcher(LdapConnection connection)
{
AsyncSearcher searcher = new AsyncSearcher(connection);
//assign some handlers for our events
searcher.PageCompleted += new EventHandler<AsyncEventArgs>(searcher_PageCompleted);
searcher.SearchCompleted += new EventHandler<AsyncEventArgs>(searcher_SearchCompleted);
return searcher;
}
static LdapConnection CreateConnection(LdapDirectoryIdentifier server)
{
LdapConnection connect = new LdapConnection(server);
return connect;
}
static void searcher_SearchCompleted(object sender, AsyncEventArgs e)
{
//this is volatile, so we need check it first or another thread
//could change this from under us
bool lastSearch = (((AsyncSearcher)sender).PendingSearches == 0);
Console.WriteLine(
"{0} Search Complete on thread {1}",
e.RequestID.Equals(_guid) ? "First" : "Second",
Thread.CurrentThread.ManagedThreadId
);
if (lastSearch)
_resetEvent.Set();
}
static void searcher_PageCompleted(object sender, AsyncEventArgs e)
{
try {
Encoding textEncoding;
string attrString = "";
_resultsCounter += e.Results.Count;
//or do something with e.Results here...
Console.WriteLine(
"Found {0} results on search thread {1}",
_resultsCounter,
Thread.CurrentThread.ManagedThreadId
);
// encoding parameter was specified, try to apply
if(_arguments.Contains("-e")) {
switch(_arguments[_arguments.IndexOf("-e") + 1].ToUpper())
{
case "UTF8":
textEncoding = Encoding.UTF8;
break;
case "ANSI":
case "ASCII":
textEncoding = Encoding.ASCII;
break;
case "UNICODE":
textEncoding = Encoding.Unicode;
break;
default:
throw new ArgumentException("Unkown Encoding: Expected one of the following Encoding types: UTF8, ANSI or Unicode");
}
}
// if not specified use system default
else {
textEncoding = Encoding.Default;
}
foreach(SearchResultEntry searchResult in e.Results) {
IDictionaryEnumerator attribEnum = searchResult.Attributes.GetEnumerator();
foreach(string a in attr)
{
//Iterate through the result attributes
while (attribEnum.MoveNext())
{
//Attributes have one or more values so we iterate through all the values of each attribute
DirectoryAttribute subAttrib = (DirectoryAttribute)attribEnum.Value;
if(attribEnum.Key.ToString().Normalize().ToLower() == a.Normalize().ToLower())
{
for (int i = 0; i < subAttrib.Count; i++)
{
//Add Attributes to string and seperate with a space character if more that one value is present
attrString += subAttrib[i].ToString() + ((subAttrib.Count > 1) ? " " : string.Empty);
}
//separate each attribute with comma or supplied separation character
attrString += _arguments.Contains("-s") ? _arguments[_arguments.IndexOf("-s") +1 ] : ",";
}
}
attribEnum.Reset();
}
//remove last seperator
if(!string.IsNullOrEmpty(attrString)){
attrString = attrString.Remove(attrString.Length - 1, 1);
//output to console instead of csv?
if(_arguments.Contains("-o")){
using (FileStream fs = new FileStream(_arguments[_arguments.IndexOf("-o") + 1], FileMode.Append, FileAccess.Write)) {
using (StreamWriter sw = new StreamWriter(fs, textEncoding)) {
sw.WriteLine(attrString);
attrString = string.Empty;
sw.Close();
}
}
}
//prefer console output?
else {
Console.WriteLine(attrString + "\n");
}
}
}
}
catch (Exception ex) {
LogWriter.CreateLogEntry(string.Format("ERROR:{0} {1}", ex.Message, (ex.InnerException != null) ? ex.InnerException.Message : string.Empty));
}
}
/// <summary>
/// Listing 5.10 Modified...
/// </summary>
public class AsyncSearcher
{
LdapConnection _connect;
Hashtable _results = new Hashtable();
public event EventHandler<AsyncEventArgs> SearchCompleted;
public event EventHandler<AsyncEventArgs> PageCompleted;
public AsyncSearcher(LdapConnection connection)
{
this._connect = connection;
}
/// <summary>
/// Volatile count of outstanding searches in process
/// </summary>
public int PendingSearches
{
get
{
return _results.Count;
}
}
private void InternalCallback(IAsyncResult result)
{
SearchResponse response = this._connect.EndSendRequest(result) as SearchResponse;
ProcessResponse(response, ((SearchRequest)result.AsyncState).RequestId);
//find the returned page response control
foreach (DirectoryControl control in response.Controls)
{
if (control is PageResultResponseControl)
{
//call paged search again
NextPage((SearchRequest)result.AsyncState, ((PageResultResponseControl)control).Cookie);
break;
}
}
}
private void ProcessResponse(SearchResponse response, string guid)
{
//only 1 thread at a time gets here...
List<SearchResultEntry> entries = new List<SearchResultEntry>();
foreach (SearchResultEntry entry in response.Entries)
{
entries.Add(entry);
}
//signal our caller that we have a page
EventHandler<AsyncEventArgs> OnPage = PageCompleted;
if (OnPage != null)
{
OnPage(
this,
new AsyncEventArgs(entries, guid)
);
}
//add to the main collection
((List<SearchResultEntry>)_results[guid]).AddRange(entries);
}
public Guid BeginPagedSearch(
string baseDN,
string filter,
string[] attribs,
int pageSize
)
{
Guid guid = Guid.NewGuid();
SearchRequest request = new SearchRequest(
baseDN,
filter,
System.DirectoryServices.Protocols.SearchScope.Subtree,
attribs
);
PageResultRequestControl prc = new PageResultRequestControl(pageSize);
//add the paging control
request.Controls.Add(prc);
//we will use this to distinguish multiple searches.
request.RequestId = guid.ToString();
//create a temporary placeholder for the results
_results.Add(request.RequestId, new List<SearchResultEntry>());
//kick off async
IAsyncResult result = this._connect.BeginSendRequest(
request,
PartialResultProcessing.NoPartialResultSupport,
new AsyncCallback(InternalCallback),
request
);
return guid;
}
private void NextPage(SearchRequest request, byte[] cookie)
{
//our last page is when the cookie is empty
if (cookie != null && cookie.Length != 0)
{
//update the cookie and preserve page size
foreach (DirectoryControl control in request.Controls)
{
if (control is PageResultRequestControl)
{
((PageResultRequestControl)control).Cookie = cookie;
break;
}
}
//call it again to get next page
IAsyncResult result = this._connect.BeginSendRequest(
request,
PartialResultProcessing.NoPartialResultSupport,
new AsyncCallback(InternalCallback),
request
);
}
else
{
List<SearchResultEntry> results = (List<SearchResultEntry>)_results[request.RequestId];
//decrement our collection when we are done
_results.Remove(request.RequestId);
//we have finished, signal the caller
EventHandler<AsyncEventArgs> OnComplete = SearchCompleted;
if (OnComplete != null)
{
OnComplete(
this,
new AsyncEventArgs(results, request.RequestId)
);
}
}
}
}
/// <summary>
/// Just a simple class to hold some results
/// </summary>
public class AsyncEventArgs : EventArgs
{
Guid _id;
List<SearchResultEntry> _entries;
public AsyncEventArgs(List<SearchResultEntry> entries, string requestID)
{
_entries = entries;
_id = new Guid(requestID);
}
public List<SearchResultEntry> Results
{
get
{
return _entries;
}
}
public Guid RequestID
{
get
{
return _id;
}
}
}
// Flag: Has Dispose already been called?
bool disposed = false;
// Instantiate a SafeHandle instance.
SafeHandle handle = new SafeFileHandle(IntPtr.Zero, true);
// Public implementation of Dispose pattern callable by consumers.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// Protected implementation of Dispose pattern.
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing) {
handle.Dispose();
// Free any other managed objects here.
}
disposed = true;
}
}
}