[go: up one dir, main page]

Menu

[9b8379]: / lat / ServerData.cs  Maximize  Restore  History

Download this file

612 lines (502 with data), 17.2 kB

  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
//
// lat - ServerData.cs
// Author: Loren Bandiera
// Copyright 2005-2006 MMG Security, Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; Version 2
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
//
using System;
using System.Collections.Generic;
using Novell.Directory.Ldap;
using Novell.Directory.Ldap.Utilclass;
namespace lat
{
public class ServerData
{
LdapServer server;
public ServerData (LdapServer server)
{
this.server = server;
}
public void Add (LdapEntry entry)
{
server.Add (entry);
}
/// <summary>Adds an entry to the directory
///
/// </summary>
/// <param name="dn">The distinguished name of the new entry.</param>
/// <param name="attributes">An arraylist of string attributes for the
/// new ldap entry.</param>
public void Add (string dn, List<LdapAttribute> attributes)
{
Log.Debug ("START Connection.Add ()");
Log.Debug ("dn: {0}", dn);
LdapAttributeSet attributeSet = new LdapAttributeSet();
foreach (LdapAttribute attr in attributes) {
foreach (string v in attr.StringValueArray)
Log.Debug ("{0}:{1}", attr.Name, v);
attributeSet.Add (attr);
}
LdapEntry newEntry = new LdapEntry( dn, attributeSet );
Add (newEntry);
Log.Debug ("END Connection.Add ()");
}
/// <summary>Copy a directory entry
/// </summary>
/// <param name="oldDN">Distinguished name of the entry to copy</param>
/// <param name="newRDN">New name for entry</param>
/// <param name="parentDN">Parent name</param>
public void Copy (string oldDN, string newRDN, string parentDN)
{
server.Copy (oldDN, newRDN, parentDN);
}
/// <summary>Deletes a directory entry
/// </summary>
/// <param name="dn">Distinguished name of the entry to delete</param>
public void Delete (string dn)
{
server.Delete (dn);
}
/// <summary>Gets a list of all attributes for the given object class
/// </summary>
/// <param name="objClass">Name of object class</param>
public string[] GetAllAttributes (string objClass)
{
try {
LdapSchema schema = server.GetSchema ();
LdapObjectClassSchema ocs = schema.getObjectClassSchema ( objClass );
List<string> attrs = new List<string> ();
if (ocs.RequiredAttributes != null) {
foreach (string r in ocs.RequiredAttributes)
if (!attrs.Contains (r))
attrs.Add (r);
}
if (ocs.OptionalAttributes != null) {
foreach (string o in ocs.OptionalAttributes)
if (!attrs.Contains (o))
attrs.Add (o);
}
attrs.Sort ();
return attrs.ToArray ();
} catch (Exception e) {
Log.Debug (e);
return null;
}
}
/// <summary>Gets a list of required and optional attributes for
/// the given object classes.
/// </summary>
/// <param name="objClass">List of object classes</param>
/// <param name="required">Required attributes</param>
/// <param name="optional">Optional attributes</param>
public void GetAllAttributes (List<string> objClass, out string[] required, out string[] optional)
{
try {
LdapSchema schema = server.GetSchema ();
LdapObjectClassSchema ocs;
List<string> r_attrs = new List<string> ();
List<string> o_attrs = new List<string> ();
foreach (string c in objClass) {
ocs = schema.getObjectClassSchema ( c );
if (ocs.RequiredAttributes != null) {
foreach (string r in ocs.RequiredAttributes)
if (!r_attrs.Contains (r))
r_attrs.Add (r);
}
if (ocs.OptionalAttributes != null) {
foreach (string o in ocs.OptionalAttributes)
if (!o_attrs.Contains (o))
o_attrs.Add (o);
}
}
required = r_attrs.ToArray ();
optional = o_attrs.ToArray ();
} catch (Exception e) {
required = null;
optional = null;
Log.Debug (e);
}
}
/// <summary>Gets a list of attribute types supported on the
/// directory.
/// </summary>
/// <returns>An array of LdapEntry objects</returns>
public string[] GetAttributeTypes ()
{
LdapEntry[] entries = server.Search (
server.GetSchemaDN (),
LdapConnection.SCOPE_BASE,
server.DefaultSearchFilter,
new string[] { "attributetypes" });
if (entries == null)
return null;
List<string> tmp = new List<string> ();
LdapAttribute la = entries[0].getAttribute ("attributetypes");
foreach (string s in la.StringValueArray) {
SchemaParser sp = new SchemaParser (s);
tmp.Add (sp.Names[0]);
}
tmp.Sort ();
return tmp.ToArray ();
}
/// <summary>Gets the schema for a given attribute type
/// </summary>
/// <param name="attrType">Attribute type</param>
/// <returns>A SchemaParser object</returns>
public SchemaParser GetAttributeTypeSchema (string attrType)
{
LdapEntry[] entries = server.Search (
server.GetSchemaDN (),
LdapConnection.SCOPE_BASE,
server.DefaultSearchFilter,
new string[] { "attributetypes" });
if (entries == null)
return null;
foreach (LdapEntry entry in entries) {
LdapAttribute la = entry.getAttribute ("attributetypes");
foreach (string s in la.StringValueArray) {
SchemaParser sp = new SchemaParser (s);
foreach (string a in sp.Names)
if (attrType.Equals (a))
return sp;
}
}
return null;
}
/// <summary>Gets the value of an attribute for the given
/// entry.
/// </summary>
/// <param name="le">LdapEntry</param>
/// <param name="attr">Attribute to lookup type</param>
/// <returns>The value of the attribute (or an empty string if there is
/// no value).</returns>
public string GetAttributeValueFromEntry (LdapEntry le, string attr)
{
LdapAttribute la = le.getAttribute (attr);
if (la != null)
return la.StringValue;
return "";
}
/// <summary>Gets the value of the given attribute for the given
/// entry.
/// </summary>
/// <param name="le">LdapEntry</param>
/// <param name="attrs">List of attributes to lookup</param>
/// <returns>A list of attribute values</returns>
public string[] GetAttributeValuesFromEntry (LdapEntry le, string[] attrs)
{
if (le == null || attrs == null)
throw new ArgumentNullException ();
List<string> retVal = new List<string> ();
foreach (string n in attrs) {
LdapAttribute la = le.getAttribute (n);
if (la != null)
retVal.Add (la.StringValue);
else
retVal.Add ("");
}
return retVal.ToArray ();
}
/// <summary>Gets an entry in the directory.
/// </summary>
/// <param name="dn">The distinguished name of the entry</param>
public LdapEntry GetEntry (string dn)
{
LdapEntry[] entry = server.Search (dn, LdapConnection.SCOPE_BASE, "objectclass=*", null);
if (entry.Length > 0)
return entry[0];
return null;
}
/// <summary>Gets the children of a given entry.
/// </summary>
/// <param name="entryDN">Distiguished name of entry</param>
/// <returns>A list of children (if any)</returns>
public LdapEntry[] GetEntryChildren (string entryDN)
{
return server.Search (entryDN, LdapConnection.SCOPE_ONE, "objectclass=*", null);
}
/// <summary>Gets the schema information for a given ldap syntax
/// </summary>
/// <param name="attrType">LDAP syntax</param>
/// <returns>schema information</returns>
public SchemaParser GetLdapSyntax (string synName)
{
LdapEntry[] entries = server.Search (
server.GetSchemaDN (),
LdapConnection.SCOPE_BASE,
"",
new string[] { "ldapSyntaxes" });
if (entries == null)
return null;
LdapAttribute la = entries[0].getAttribute ("ldapSyntaxes");
foreach (string s in la.StringValueArray) {
SchemaParser sp = new SchemaParser (s);
if (synName.Equals (sp.Description))
return sp;
}
return null;
}
/// <summary>Gets the servers LDAP syntaxes (if available).
/// </summary>
/// <returns>matching rules</returns>
public string[] GetLdapSyntaxes ()
{
LdapEntry[] entries = server.Search (
server.GetSchemaDN (),
LdapConnection.SCOPE_BASE,
"",
new string[] { "ldapSyntaxes" });
if (entries == null)
return null;
List<string> tmp = new List<string> ();
LdapAttribute la = entries[0].getAttribute ("ldapSyntaxes");
foreach (string s in la.StringValueArray) {
SchemaParser sp = new SchemaParser (s);
tmp.Add (sp.Description);
}
tmp.Sort ();
return tmp.ToArray ();
}
/// <summary>Gets the local Samba SID (if available).
/// </summary>
/// <returns>sambaSID</returns>
public string GetLocalSID ()
{
LdapEntry[] sid = server.Search (server.DirectoryRoot, LdapConnection.SCOPE_SUB, "objectclass=sambaDomain", null);
if (sid.Length > 0) {
LdapAttribute a = sid[0].getAttribute ("sambaSID");
return a.StringValue;
}
return null;
}
/// <summary>Gets the schema information for a given matching rule
/// </summary>
/// <param name="attrType">Matching rule</param>
/// <returns>schema information</returns>
public SchemaParser GetMatchingRule (string matName)
{
LdapEntry[] entries = server.Search (
server.GetSchemaDN (),
LdapConnection.SCOPE_BASE,
"",
new string[] { "matchingRules" });
if (entries == null)
return null;
LdapAttribute la = entries[0].getAttribute ("matchingRules");
foreach (string s in la.StringValueArray) {
SchemaParser sp = new SchemaParser (s);
foreach (string a in sp.Names)
if (matName.Equals (a))
return sp;
}
return null;
}
/// <summary>Gets the servers matching rules (if available).
/// </summary>
/// <returns>matching rules</returns>
public string[] GetMatchingRules ()
{
LdapEntry[] entries = server.Search (
server.GetSchemaDN (),
LdapConnection.SCOPE_BASE,
"",
new string[] { "matchingRules" });
if (entries == null)
return null;
List<string> tmp = new List<string> ();
LdapAttribute la = entries[0].getAttribute ("matchingRules");
foreach (string s in la.StringValueArray) {
SchemaParser sp = new SchemaParser (s);
tmp.Add (sp.Names[0]);
}
tmp.Sort ();
return tmp.ToArray ();
}
/// <summary>Gets the next available gidNumber
/// </summary>
/// <returns>The next group number</returns>
public int GetNextGID ()
{
List<int> gids = new List<int> ();
LdapEntry[] groups = server.Search (server.DirectoryRoot, LdapConnection.SCOPE_SUB, "gidNumber=*", null);
foreach (LdapEntry entry in groups) {
LdapAttribute a = entry.getAttribute ("gidNumber");
gids.Add (int.Parse(a.StringValue));
}
gids.Sort ();
if (gids.Count == 0)
return 1000;
else
return (gids [gids.Count - 1]) + 1;
}
/// <summary>Gets the next available uidNumber
/// </summary>
/// <returns>The next user number</returns>
public int GetNextUID ()
{
List<int> uids = new List<int> ();
LdapEntry[] users = server.Search (server.DirectoryRoot, LdapConnection.SCOPE_SUB, "uidNumber=*", null);
foreach (LdapEntry entry in users) {
LdapAttribute a = entry.getAttribute ("uidNumber");
uids.Add (int.Parse(a.StringValue));
}
uids.Sort ();
if (uids.Count == 0)
return 1000;
else
return (uids [uids.Count - 1]) + 1;
}
/// <summary>Gets the schema of a given object class.
/// </summary>
/// <param name="objClass">Name of object class</param>
/// <returns>A SchemaParser object</returns>
public SchemaParser GetObjectClassSchema (string objClass)
{
LdapEntry[] entries;
entries = server.Search (
server.GetSchemaDN (),
LdapConnection.SCOPE_BASE,
server.DefaultSearchFilter,
new string[] { "objectclasses" });
foreach (LdapEntry entry in entries) {
LdapAttribute la = entry.getAttribute ("objectclasses");
foreach (string s in la.StringValueArray) {
SchemaParser sp = new SchemaParser (s);
foreach (string a in sp.Names)
if (objClass.Equals (a))
return sp;
}
}
return null;
}
/// <summary>Gets a list of requried attributes for a given object class.
/// </summary>
/// <param name="objClass">Name of object class</param>
/// <returns>An array of required attribute names</returns>
public string[] GetRequiredAttrs (string objClass)
{
if (objClass == null)
return null;
LdapSchema schema = server.GetSchema ();
LdapObjectClassSchema ocs = schema.getObjectClassSchema ( objClass );
if (ocs != null)
return ocs.RequiredAttributes;
return null;
}
/// <summary>Gets a list of requried attributes for a list of given
/// object classes.
/// </summary>
/// <param name="objClasses">Array of objectclass names</param>
/// <returns>An array of required attribute names</returns>
public string[] GetRequiredAttrs (string[] objClasses)
{
if (objClasses == null)
return null;
List<string> retVal = new List<string> ();
LdapSchema schema = server.GetSchema ();
foreach (string oc in objClasses) {
LdapObjectClassSchema ocs = schema.getObjectClassSchema ( oc );
foreach (string c in ocs.RequiredAttributes)
if (!retVal.Contains (c))
retVal.Add (c);
}
return retVal.ToArray ();
}
/// <summary>Modifies the specified entry
/// </summary>
/// <param name="dn">Distinguished name of entry to modify</param>
/// <param name="mods">Array of LdapModification objects</param>
public void Modify (string dn, LdapModification[] mods)
{
server.Modify (dn, mods);
}
/// <summary>Moves the specified entry
/// </summary>
/// <param name="oldDN">Distinguished name of entry to move</param>
/// <param name="newRDN">New name of entry</param>
/// <param name="parentDN">Name of parent entry</param>
public void Move (string oldDN, string newRDN, string parentDN)
{
server.Move (oldDN, newRDN, parentDN);
}
/// <summary>Renames the specified entry
/// </summary>
/// <param name="oldDN">Distinguished name of entry to rename</param>
/// <param name="newDN">New to rename entry to</param>
/// <param name="saveOld">Save old entry</param>
public void Rename (string oldDN, string newDN, bool saveOld)
{
server.Rename (oldDN, newDN, saveOld);
}
/// <summary>Searches the directory
/// </summary>
/// <param name="searchFilter">filter to search for</param>
/// <returns>List of entries matching filter</returns>
public LdapEntry[] Search (string searchFilter)
{
return server.Search (server.DirectoryRoot, LdapConnection.SCOPE_SUB, searchFilter, null);
}
/// <summary>Searches the directory
/// </summary>
/// <param name="searchBase">Where to start the search</param>
/// <param name="searchFilter">Filter to search for</param>
/// <returns>List of entries matching filter</returns>
public LdapEntry[] Search (string searchBase, string searchFilter)
{
return server.Search (searchBase, LdapConnection.SCOPE_SUB, searchFilter, null);
}
/// <summary>Searches the directory
/// </summary>
/// <param name="searchBase">Where to start the search</param>
/// <param name="searchScope">Scope of search</param>
/// <param name="searchFilter">Filter to search for</param>
/// <param name="searchAttrs">Attributes to search for</param>
/// <returns>List of entries matching filter</returns>
public LdapEntry[] Search (string searchBase, int searchScope, string searchFilter, string[] searchAttrs)
{
return server.Search (searchBase, searchScope, searchFilter, searchAttrs);
}
/// <summary>Searches the directory for all entries of a given object
/// class.
/// </summary>
/// <param name="objectClass">Name of objectclass</param>
/// <returns>List of entries matching objectclass</returns>
public LdapEntry[] SearchByClass (string objectClass)
{
return Search (server.DirectoryRoot, String.Format ("objectclass={0}", objectClass));
}
public string[] ObjectClasses
{
get {
LdapEntry[] le = server.Search (
server.GetSchemaDN (),
LdapConnection.SCOPE_BASE,
server.DefaultSearchFilter,
new string[] { "objectclasses" });
if (le == null)
return null;
List<string> tmp = new List<string> ();
LdapAttribute la = le[0].getAttribute ("objectclasses");
foreach (string s in la.StringValueArray) {
SchemaParser sp = new SchemaParser (s);
tmp.Add (sp.Names[0]);
}
tmp.Sort ();
return tmp.ToArray ();
}
}
}
}