summaryrefslogtreecommitdiffstats
path: root/ldap/servers/ntds/apacheds/org/apache/ldap/server/NetAPIPartition.java
blob: 3580f92395b9b6fb030e11eb4d2c96c3f94f0d86 (plain)
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
/* --- BEGIN COPYRIGHT BLOCK ---
 * 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 of the License.
 * 
 * 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.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * --- END COPYRIGHT BLOCK --- */

/*
 * NetAPIPartition.java
 *
 * Created on February 22, 2005, 9:34 AM
 */
package org.apache.ldap.server;

import java.util.Map;
//import java.util.Collection;
import java.util.Date;
import java.util.Properties;
import java.io.File;
import java.io.FileWriter;
import javax.naming.Name;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.ModificationItem;
import javax.naming.directory.SearchControls;

import org.apache.ldap.common.name.LdapName;
//import org.apache.ldap.common.util.PropertiesUtils;
import org.apache.ldap.common.filter.ExprNode;
import org.apache.ldap.server.ContextPartition;
//import org.apache.ldap.common.message.Control;
import org.apache.ldap.common.filter.PresenceNode;

import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.Attribute;
import javax.naming.directory.SearchResult;
import javax.naming.directory.DirContext;
import java.util.StringTokenizer;
import java.util.HashSet;
import org.bpi.jnetman.*;

/**
 *
 * @author scott
 */
public class NetAPIPartition implements ContextPartition {

    static {
        System.loadLibrary("jnetman");
        System.out.println("dll loaded");
    }
	
    //private LdapName suffix;
    private String suffix;
    private static final String container = new String("cn=users").toLowerCase();
    private static final String logFilename = new String("../logs/usersync.log");
    private static final int GLOBAL_FLAG = 0x00000002;
    private static final int DOMAINLOCAL_FLAG = 0x00000004;
    private FileWriter outLog;
    
    /** Creates a new instance of NetAPIPartition */
    public NetAPIPartition(Name upSuffix, Name normSuffix, String properties) {
        try {
        	outLog = new FileWriter(new File(logFilename));
        }
        catch(Exception e) {
        }
        
        try {
        	outLog.write(new Date() + ": reached NetAPIPartition");
        	outLog.flush();
        }
        catch(Exception e) {
        }
        System.out.println("reached NetAPIPartition");
        suffix = normSuffix.toString();
    }

    /**
     * Deletes a leaf entry from this BackingStore: non-leaf entries cannot be 
     * deleted until this operation has been applied to their children.
     *
     * @param name the normalized distinguished/absolute name of the entry to
     * delete from this BackingStore.
     * @throws NamingException if there are any problems
     */ 
    public void delete( Name name ) throws NamingException {
    	try {
        	outLog.write(new Date() + ": reached NetAPIPartition.delete: " + name);
        	outLog.flush();
        }
        catch(Exception e) {
        }
        System.out.println("reached NetAPIPartition.delete: " + name);
        
        String rdn = getRDN(name.toString());
        boolean deletedSomthing = false;
        NTUser user = new NTUser();
        NTGroup group = new NTGroup();
        NTLocalGroup localGroup = new NTLocalGroup();
        
        if(name.toString().toLowerCase().startsWith(new String("sAMAccountName").toLowerCase())) {
        	if(user.RetriveUserByAccountName(rdn) == 0) {
        		if(user.DeleteUser(user.GetAccountName()) == 0) {
        			deletedSomthing = true;
        		}
        	}
        	if(group.RetriveGroupByAccountName(rdn) == 0) {
        		if(group.DeleteGroup(group.GetAccountName()) == 0) {
        			deletedSomthing = true;
        		}
        	}
        	if(localGroup.RetriveLocalGroupByAccountName(rdn) == 0) {
        		if(localGroup.DeleteLocalGroup(localGroup.GetAccountName()) == 0) {
        			deletedSomthing = true;
        		}
        	}
        }
        else if((name.toString().toLowerCase().startsWith(new String("objectGUID").toLowerCase())) ||
        		(name.toString().toLowerCase().startsWith(new String("GUID").toLowerCase()))) {
        	
        	if(user.RetriveUserBySIDHexStr(rdn) == 0) {
        		if(user.DeleteUser(user.GetAccountName()) == 0) {
        			deletedSomthing = true;
        		}
        	}
        	if(group.RetriveGroupBySIDHexStr(rdn) == 0) {
        		if(group.DeleteGroup(group.GetAccountName()) == 0) {
        			deletedSomthing = true;
        		}
        	}
        	if(localGroup.RetriveLocalGroupBySIDHexStr(rdn) == 0) {
        		if(localGroup.DeleteLocalGroup(localGroup.GetAccountName()) == 0) {
        			deletedSomthing = true;
        		}
        	}
        }
        else {
        	throw new NamingException("Can not delete DN: " + name);
        }
        
        if(!deletedSomthing) {
            throw new NamingException("No matching users or groups: " + rdn);
        }
    }

    /**
     * Adds an entry to this BackingStore.
     *
     * @param upName the user provided distinguished/absolute name of the entry
     * @param normName the normalized distinguished/absolute name of the entry
     * @param entry the entry to add to this BackingStore
     * @throws NamingException if there are any problems
     */
    public void add( String upName, Name normName, Attributes entry ) throws NamingException {
    	try {
        	outLog.write(new Date() + ": reached NetAPIPartition.add: " + normName);
        	outLog.flush();
        }
        catch(Exception e) {
        }
        System.out.println("reached NetAPIPartition.add: " + normName);
        
        String rdn = getRDN(normName.toString());
        Attribute attribute = entry.get("objectClass");
        Attribute groupType;
        ModificationItem[] modItems = new ModificationItem[entry.size()];
        NamingEnumeration modAttributes = entry.getAll();
        NTUser user = new NTUser();
        NTGroup group = new NTGroup();
        NTLocalGroup localGroup = new NTLocalGroup();
        int result;

        for(int i = 0; i < entry.size(); i++) {
        	modItems[i] = new ModificationItem(DirContext.ADD_ATTRIBUTE, (Attribute)modAttributes.next());
        }

        if(normName.toString().compareToIgnoreCase(suffix) == 0) {
        	// Gets us past the CoreContestFactory.startUpAppPartitions
        }
        else if((normName.toString().toLowerCase().endsWith(container + "," + suffix)) &&
        		(normName.toString().toLowerCase().startsWith(new String("sAMAccountName").toLowerCase()))) {
        	
            if(attribute.contains("user")) {
                user.NewUser(rdn);
                modNTUserAttributes(user, modItems);
                result = user.AddUser();
                if(result != 0) {
                	throw new NamingException("Failed to add new user: " + normName + " (" + result + ")");
                }
            }
            else if(attribute.contains("group")) {
            	attribute = entry.get("groupType");
            	if(((new Integer((String)attribute.get())).intValue() & GLOBAL_FLAG) == GLOBAL_FLAG) {
            		group.NewGroup(rdn);
                    modNTGroupAttributes(group, modItems);
                    if(group.AddGroup() != 0) {
                    	throw new NamingException("Failed to add new group: " + normName);
                    }
            	}
            	else if(((new Integer((String)attribute.get())).intValue() & DOMAINLOCAL_FLAG) == DOMAINLOCAL_FLAG) {
                    localGroup.NewLocalGroup(rdn);
                    modNTLocalGroupAttributes(localGroup, modItems);
                    if(localGroup.AddLocalGroup() != 0) {
                    	throw new NamingException("Failed add new local group: " + normName);
                    }
            	}
            	else {
            		throw new NamingException("Unknown group type: " + (Integer)attribute.get());
            	}
            }
            else {
                throw new NamingException("No matching objectClass");
            }
        }
        else {
            throw new NamingException("Attempt to add an entry outside partition scope: " + normName);
        }
    }

    /**
     * Modifies an entry by adding, removing or replacing a set of attributes.
     *
     * @param name the normalized distinguished/absolute name of the entry to
     * modify
     * @param modOp the modification operation to perform on the entry which
     * is one of constants specified by the DirContext interface:
     * <code>ADD_ATTRIBUTE, REMOVE_ATTRIBUTE, REPLACE_ATTRIBUTE</code>.
     * @param mods the attributes and their values used to affect the
     * modification with.
     * @throws NamingException if there are any problems
     * @see javax.naming.directory.DirContext
     * @see javax.naming.directory.DirContext.ADD_ATTRIBUTE
     * @see javax.naming.directory.DirContext.REMOVE_ATTRIBUTE
     * @see javax.naming.directory.DirContext.REPLACE_ATTRIBUTE
     */
    public void modify( Name name, int modOp, Attributes mods ) throws NamingException {
    	try {
        	outLog.write(new Date() + ": reached NetAPIPartition.modify1: " + name);
        	outLog.flush();
        }
        catch(Exception e) {
        }
        System.out.println("reached NetAPIPartition.modify1: " + name);
        
        ModificationItem[] modItems = new ModificationItem[mods.size()];
        NamingEnumeration modAttributes = mods.getAll();

        for(int i = 0; i < mods.size(); i++) {
        	modItems[i] = new ModificationItem(modOp, (Attribute)modAttributes.next());
        }
        
        modify(name, modItems);
    }

    /**
     * Modifies an entry by using a combination of adds, removes or replace 
     * operations using a set of ModificationItems.
     *
     * @param name the normalized distinguished/absolute name of the entry to modify
     * @param mods the ModificationItems used to affect the modification with
     * @throws NamingException if there are any problems
     * @see ModificationItem
     */
    public void modify( Name name, ModificationItem [] mods ) throws NamingException {
    	try {
        	outLog.write(new Date() + ": reached NetAPIPartition.modify2: " + name);
        	outLog.flush();
        }
        catch(Exception e) {
        }
        System.out.println("reached NetAPIPartition.modify2: " + name);

        String rdn = getRDN(name.toString());
        boolean modifiedSomething = false;
        NTUser user = new NTUser();
        NTGroup group = new NTGroup();
        NTLocalGroup localGroup = new NTLocalGroup();

        if(name.toString().toLowerCase().startsWith(new String("sAMAccountName").toLowerCase())) {
            if(user.RetriveUserByAccountName(rdn) == 0) {
                modNTUserAttributes(user, mods);
                if(user.StoreUser() != 0) {
                	throw new NamingException("Failed to commit modified user information: " + name);
                }
                
                modifiedSomething = true;
            }
            else if(group.RetriveGroupByAccountName(rdn) == 0) {
                modNTGroupAttributes(group, mods);
                if(group.StoreGroup() != 0) {
                	throw new NamingException("Failed to commit modified group information: " + name);
                }
                
                modifiedSomething = true;
            }
            else if(localGroup.RetriveLocalGroupByAccountName(rdn) == 0) {
                modNTLocalGroupAttributes(localGroup, mods);
                if(localGroup.StoreLocalGroup() != 0) {
                	throw new NamingException("Failed to commit modified local group information: " + name);
                }
                
                modifiedSomething = true;
            }
        }
        else if((name.toString().toLowerCase().startsWith(new String("objectGUID").toLowerCase())) ||
        		(name.toString().toLowerCase().startsWith(new String("GUID").toLowerCase()))) {
        	
        	if(user.RetriveUserBySIDHexStr(rdn) == 0) {
                modNTUserAttributes(user, mods);
                if(user.StoreUser() != 0) {
                	throw new NamingException("Failed to commit modified user information: " + name);
                }
                
                modifiedSomething = true;
            }
            else if(group.RetriveGroupBySIDHexStr(rdn) == 0) {
                modNTGroupAttributes(group, mods);
                if(group.StoreGroup() != 0) {
                	throw new NamingException("Failed to commit modified group information: " + name);
                }
                
                modifiedSomething = true;
            }
            else if(localGroup.RetriveLocalGroupBySIDHexStr(rdn) == 0) {
                modNTLocalGroupAttributes(localGroup, mods);
                if(localGroup.StoreLocalGroup() != 0) {
                	throw new NamingException("Failed to commit modified local group information: " + name);
                }
                
                modifiedSomething = true;
            }
        }
        else {
            throw new NamingException("Can not delete DN: " + name);
        }
        
        if(!modifiedSomething) {
            throw new NamingException("No matching users or groups: " + rdn);
        }
    }

    /**
     * A specialized form of one level search used to return a minimal set of 
     * information regarding child entries under a base.  Convenience method
     * used to optimize operations rather than conducting a full search with 
     * retrieval.
     *
     * @param base the base distinguished/absolute name for the search/listing
     * @return a NamingEnumeration containing objects of type
     * {@link org.apache.ldap.server.db.DbSearchResult}
     * @throws NamingException if there are any problems
     */
    public NamingEnumeration list( Name base ) throws NamingException {
    	try {
        	outLog.write(new Date() + ": reached NetAPIPartition.list");
        	outLog.flush();
        }
        catch(Exception e) {
        }
        System.out.println("reached NetAPIPartition.list");

        return new BasicAttribute(base.toString()).getAll();
    }
    
    /**
     * Conducts a search against this BackingStore.  Namespace specific
     * parameters for search are contained within the environment using
     * namespace specific keys into the hash.  For example in the LDAP namespace
     * a BackingStore implementation may look for search Controls using a
     * namespace specific or implementation specific key for the set of LDAP
     * Controls.
     *
     * @param base the normalized distinguished/absolute name of the search base
     * @param env the environment under which operation occurs
     * @param filter the root node of the filter expression tree
     * @param searchCtls the search controls
     * @throws NamingException if there are any problems
     * @return a NamingEnumeration containing objects of type 
     * <a href="http://java.sun.com/j2se/1.4.2/docs/api/
     * javax/naming/directory/SearchResult.html">SearchResult</a>.
     */
    public NamingEnumeration search( Name base, Map env, ExprNode filter,
        SearchControls searchCtls ) throws NamingException {
    	try {
        	outLog.write(new Date() + ": reached NetAPIPartition.search: " + base);
        	outLog.flush();
        }
        catch(Exception e) {
        }
        System.out.println("reached NetAPIPartition.search: " + base + " " + filter);
        
        BasicAttribute results = new BasicAttribute(null);
        SearchResult result;
        BasicAttributes attributes;
        BasicAttribute attribute;
        String rdn = getRDN(base.toString());
        NTUser user = new NTUser();
        NTGroup group = new NTGroup();
        NTLocalGroup localGroup = new NTLocalGroup();
        
        // base equals suffix
        if(base.toString().compareToIgnoreCase(suffix) == 0) {
        	// object scope
        	if(((searchCtls.getSearchScope() == SearchControls.OBJECT_SCOPE) ||
        			(searchCtls.getSearchScope() == SearchControls.SUBTREE_SCOPE)) &&
        			(filter.toString().toLowerCase().startsWith(new String("(objectClass=*)").toLowerCase()))) {
        		
                attributes = new BasicAttributes();

                attribute = new BasicAttribute("objectClass");
                attribute.add("top");
                attribute.add("domain");
                attributes.put(attribute);

                result = new SearchResult(suffix, null, attributes);
                results.add(result);
        	}
        	
        	// one level or subtree scope
        	if(((searchCtls.getSearchScope() == SearchControls.ONELEVEL_SCOPE) ||
					(searchCtls.getSearchScope() == SearchControls.SUBTREE_SCOPE)) &&
        			(filter.toString().toLowerCase().startsWith(new String("(objectClass=*)").toLowerCase()))) {
                
                result = new SearchResult(container + "," + suffix, null, new BasicAttributes());
                results.add(result);
        	}
        	
        	// subtree scope
        	if(searchCtls.getSearchScope() == SearchControls.SUBTREE_SCOPE) {
        		searchAccounts(base, env, filter, searchCtls, results);
        	}
        }
        // base equals container plus suffix 
        else if(base.toString().compareToIgnoreCase(container + "," + suffix) == 0) {
        	// object scope
        	if(((searchCtls.getSearchScope() == SearchControls.OBJECT_SCOPE) ||
					(searchCtls.getSearchScope() == SearchControls.SUBTREE_SCOPE)) &&
        			(filter.toString().toLowerCase().startsWith(new String("(objectClass=*)").toLowerCase()))) {
        		
        		attributes = new BasicAttributes();

                attribute = new BasicAttribute("objectClass");
                attribute.add("top");
                attribute.add("domain");
                attributes.put(attribute);

                result = new SearchResult(container + "," + suffix, null, attributes);
                results.add(result);
        	}
        	
        	// one level or subtree scope
        	if((searchCtls.getSearchScope() == SearchControls.ONELEVEL_SCOPE) ||
					(searchCtls.getSearchScope() == SearchControls.SUBTREE_SCOPE)) {
        		
        		searchAccounts(base, env, filter, searchCtls, results);
        	}
        	
        	// subtree scope
        	if(searchCtls.getSearchScope() == SearchControls.SUBTREE_SCOPE) {
        		// Nothing that OVELEVEL_SCOPE || SUBTREE_SCOPE doesn't already cover
        	}
        }
        // base ends with container plus suffix
        else if(base.toString().toLowerCase().endsWith(new String(container + "," + suffix).toLowerCase())) {
        	// object scope
        	if((searchCtls.getSearchScope() == SearchControls.OBJECT_SCOPE) ||
					(searchCtls.getSearchScope() == SearchControls.SUBTREE_SCOPE)) {
        		
        		searchAccounts(base, env, filter, searchCtls, results);
        	}
        	
        	// one level or subtree scope
        	if((searchCtls.getSearchScope() == SearchControls.ONELEVEL_SCOPE) ||
					(searchCtls.getSearchScope() == SearchControls.SUBTREE_SCOPE)) {
        		// Empty set
        	}
        	
        	// subtree scope
        	if(searchCtls.getSearchScope() == SearchControls.SUBTREE_SCOPE) {
        		// Nothing that OBJECT_SCOPE || SUBTREE_SCOPE doesn't already cover
        	}
        }
        // unknown base
        else {
        	throw new NamingException("Attempt to search for an entry outside partition scope: " + base);
        }
        
        return results.getAll();
    }

    /**
     * Looks up an entry by distinguished/absolute name.  This is a simplified
     * version of the search operation used to point read an entry used for
     * convenience.
     *
     * @param name the normalized distinguished name of the object to lookup
     * @return an Attributes object representing the entry
     * @throws NamingException if there are any problems
     */
    public Attributes lookup( Name name ) throws NamingException {
    	try {
        	outLog.write(new Date() + ": reached NetAPIPartition.lookup1: " + name);
        	outLog.flush();
        }
        catch(Exception e) {
        }
        System.out.println("reached NetAPIPartition.lookup1: " + name);
        
        BasicAttributes attributes = null;
        BasicAttribute attribute;
        String rdn = getRDN(name.toString());
        NTUser user = new NTUser();
        NTGroup group = new NTGroup();
        NTLocalGroup localGroup = new NTLocalGroup();
        
        if(name.toString().compareToIgnoreCase(suffix) == 0) {
            attributes = new BasicAttributes();

            attribute = new BasicAttribute("objectClass");
            attribute.add("top");
            attribute.add("domain");
            attributes.put(attribute);
        }
        else if(name.toString().compareToIgnoreCase(container + "," + suffix) == 0) {
            attributes = new BasicAttributes();

            attribute = new BasicAttribute("objectClass");
            attribute.add("top");
            attribute.add("domain");
            attributes.put(attribute);
        }
        else if(name.toString().toLowerCase().endsWith(container + "," + suffix)) {
	        if(user.RetriveUserByAccountName(rdn) == 0) {
	            attributes = getNTUserAttributes(user, rdn);
	        }
	        else if(group.RetriveGroupByAccountName(rdn) == 0) {
	            attributes = getNTGroupAttributes(group, rdn);
	        }
	        else if(localGroup.RetriveLocalGroupByAccountName(rdn) == 0) {
	            attributes = getNTLocalGroupAttributes(localGroup, rdn);
	        }
        }
        else {
            throw new NamingException("Attempt to look up an entry outside partition scope: " + name);
        }
        
        return attributes;
    }

    /**
     * Looks up an entry by distinguished name.  This is a simplified version
     * of the search operation used to point read an entry used for convenience
     * with a set of attributes to return.  If the attributes are null or emty
     * this defaults to the lookup opertion without the attributes.
     *
     * @param dn the normalized distinguished name of the object to lookup
     * @param attrIds the set of attributes to return
     * @return an Attributes object representing the entry
     * @throws NamingException if there are any problems
     */
    public Attributes lookup( Name dn, String [] attrIds ) throws NamingException {
    	try {
        	outLog.write(new Date() + ": reached NetAPIPartition.lookup2: " + dn);
        	outLog.flush();
        }
        catch(Exception e) {
        }
        System.out.println("reached NetAPIPartition.lookup2: " + dn);
        
        return lookup(dn);
    }

    /**
     * Fast operation to check and see if a particular entry exists.
     *
     * @param name the normalized distinguished/absolute name of the object to
     * check for existance
     * @return true if the entry exists, false if it does not
     * @throws NamingException if there are any problems
     */
    public boolean hasEntry( Name name ) throws NamingException {
    	try {
        	outLog.write(new Date() + ": reached NetAPIPartition.hasEntry: " + name);
        	outLog.flush();
        }
        catch(Exception e) {
        }
        System.out.println("reached NetAPIPartition.hasEntry: " + name);

        boolean result = false;
        String rdn = getRDN(name.toString());
        NTUser user = new NTUser();
        NTGroup group = new NTGroup();
        NTLocalGroup localGroup = new NTLocalGroup();

        if(name.toString().compareToIgnoreCase(suffix) == 0) {
            result = true;
        }
        else if(name.toString().compareToIgnoreCase(container + "," + suffix) == 0) {
            result = true;
        }
        
        // Ae exception raised in searchAccounts is treated as a false hasEntry result
        try {
        	if(searchAccounts(name, new Properties(), new PresenceNode(null), new SearchControls(), new BasicAttribute(null)) > 0) {
            	result = true;
        	}
        }
        catch(Exception e) {
        }

        return result;
    }

    /**
     * Checks to see if name is a context suffix.
     *
     * @param name the normalized distinguished/absolute name of the context
     * @return true if the name is a context suffix, false if it is not.
     * @throws NamingException if there are any problems
     */
    public boolean isSuffix( Name name ) throws NamingException {
    	try {
        	outLog.write(new Date() + ": reached NetAPIPartition.isSuffix");
        	outLog.flush();
        }
        catch(Exception e) {
        }
        System.out.println("reached NetAPIPartition.isSuffix");

        return false;
    }

    /**
     * Modifies an entry by changing its relative name. Optionally attributes
     * associated with the old relative name can be removed from the entry.
     * This makes sense only in certain namespaces like LDAP and will be ignored
     * if it is irrelavent.
     *
     * @param name the normalized distinguished/absolute name of the entry to
     * modify the RN of.
     * @param newRn the new RN of the entry specified by name
     * @param deleteOldRn boolean flag which removes the old RN attribute
     * from the entry if set to true, and has no affect if set to false
     * @throws NamingException if there are any problems
     */
    public void modifyRn( Name name, String newRn, boolean deleteOldRn )
        throws NamingException {
    	try {
        	outLog.write(new Date() + ": reached NetAPIPartition.modifyRn");
        	outLog.flush();
        }
        catch(Exception e) {
        }
        System.out.println("reached NetAPIPartition.modifyRn");

    }

    /**
     * Transplants a child entry, to a position in the namespace under a new
     * parent entry.
     *
     * @param newParentName the normalized distinguished/absolute name of the
     * new parent to move the target entry to
     * @param oriChildName the normalized distinguished/absolute name of the
     * original child name representing the child entry to move
     * @throws NamingException if there are any problems
     */
    public void move( Name oriChildName, Name newParentName ) throws NamingException {
    	try {
        	outLog.write(new Date() + ": reached NetAPIPartition.move1");
        	outLog.flush();
        }
        catch(Exception e) {
        }
        System.out.println("reached NetAPIPartition.move1");

    }

    /**
     * Transplants a child entry, to a position in the namespace under a new
     * parent entry and changes the RN of the child entry which can optionally
     * have its old RN attributes removed.  The removal of old RN attributes
     * may not make sense in all namespaces.  If the concept is undefined in a
     * namespace this parameters is ignored.  An example of a namespace where
     * this parameter is significant is the LDAP namespace.
     *
     * @param oriChildName the normalized distinguished/absolute name of the
     * original child name representing the child entry to move
     * @param newParentName the normalized distinguished/absolute name of the
     * new parent to move the targeted entry to
     * @param newRn the new RN of the entry
     * @param deleteOldRn boolean flag which removes the old RN attribute
     * from the entry if set to true, and has no affect if set to false
     * @throws NamingException if there are any problems
     */
    public void move( Name oriChildName, Name newParentName, String newRn,
               boolean deleteOldRn ) throws NamingException {
    	try {
        	outLog.write(new Date() + ": reached NetAPIPartition.move2");
        	outLog.flush();
        }
        catch(Exception e) {
        }
        System.out.println("reached NetAPIPartition.move2");

    }

    /**
     * Cue to BackingStores with caches to flush entry and index changes to disk.
     *
     * @throws NamingException if there are problems flushing caches
     */
    public void sync() throws NamingException {
    }

    /**
     * Closes or shuts down this BackingStore.  Operations against closed
     * BackingStores will fail.
     *
     * @throws NamingException if there are problems shutting down
     */
    public void close() throws NamingException {
    	try {
        	outLog.write(new Date() + ": reached NetAPIPartition.close");
        	outLog.flush();
        }
        catch(Exception e) {
        }
        System.out.println("reached NetAPIPartition.close");

    }

    /**
     * Checks to see if this BackingStore has been closed or shut down.
     * Operations against closed BackingStores will fail.
     *
     * @return true if shut down, false otherwise
     */
    public boolean isClosed() {
    	try {
        	outLog.write(new Date() + ": reached NetAPIPartition.isClosed");
        	outLog.flush();
        }
        catch(Exception e) {
        }
        System.out.println("reached NetAPIPartition.isClosed");

        return true;
    }
    
    /**
     * Gets the distinguished/absolute name of the suffix for all entries
     * stored within this BackingStore.
     *
     * @param normalized boolean value used to control the normalization of the
     * returned Name.  If true the normalized Name is returned, otherwise the 
     * original user provided Name without normalization is returned.
     * @return Name representing the distinguished/absolute name of this
     * BackingStores root context.
     */
    public Name getSuffix( boolean normalized ) {
    	LdapName name = null; 
    	
    	try {
    		name = new LdapName(suffix);
    	}
    	catch(NamingException ne) {
    	}
    	
    	return name;
    }
    
    private String getRDN(String dn) {
        StringTokenizer tokenizer;
        String rdn;

        tokenizer = new StringTokenizer(dn, "(),=<>");
        rdn = tokenizer.nextToken();
        rdn = tokenizer.nextToken();
        
        return rdn;
    }
    
    private int searchAccounts(Name base, Map env, ExprNode filter,
            SearchControls searchCtls, BasicAttribute results) throws NamingException {
    	
    	int resultCount = 0;
    	SearchResult result;
    	BasicAttributes attributes;
        String rdn = getRDN(base.toString());

        NTUser user = new NTUser();
        NTGroup group = new NTGroup();
        NTLocalGroup localGroup = new NTLocalGroup();
        
        if(base.toString().toLowerCase().startsWith(new String("sAMAccountName").toLowerCase())) {
        	if(user.RetriveUserByAccountName(rdn) == 0) {
        		attributes = new BasicAttributes();
	            
	            attributes = getNTUserAttributes(user, rdn);
	            result = new SearchResult("sAMAccountName=" + user.GetAccountName() + "," + container + "," + suffix, null, attributes);
	            results.add(result);
	            resultCount++;
        	}
        	else if(group.RetriveGroupByAccountName(rdn) == 0) {
        		attributes = new BasicAttributes();
	            
	            attributes = getNTGroupAttributes(group, rdn);
	            result = new SearchResult("sAMAccountName=" + group.GetAccountName() + "," + container + "," + suffix, null, attributes);
	            results.add(result);
	            resultCount++;
        	}
        	else if(localGroup.RetriveLocalGroupByAccountName(rdn) == 0) {
        		attributes = new BasicAttributes();
	            
	            attributes = getNTLocalGroupAttributes(localGroup, rdn);
	            result = new SearchResult("sAMAccountName=" + localGroup.GetAccountName() + "," + container + "," + suffix, null, attributes);
	            results.add(result);
	            resultCount++;
        	}
        	else {
        		// empty set
        	}
        }
        else if((base.toString().toLowerCase().startsWith(new String("objectGUID").toLowerCase())) ||
        		(base.toString().toLowerCase().startsWith(new String("GUID").toLowerCase()))) {
        	if(user.RetriveUserBySIDHexStr(rdn) == 0) {
        		attributes = new BasicAttributes();
	            
	            attributes = getNTUserAttributes(user, rdn);
	            result = new SearchResult("sAMAccountName=" + user.GetAccountName() + "," + container + "," + suffix, null, attributes);
	            results.add(result);
	            resultCount++;
        	}
        	else if(group.RetriveGroupBySIDHexStr(rdn) == 0) {
        		attributes = new BasicAttributes();
	            
	            attributes = getNTGroupAttributes(group, rdn);
	            result = new SearchResult("sAMAccountName=" + group.GetAccountName() + "," + container + "," + suffix, null, attributes);
	            results.add(result);
	            resultCount++;
        	}
        	else if(localGroup.RetriveLocalGroupBySIDHexStr(rdn) == 0) {
        		attributes = new BasicAttributes();
	            
	            attributes = getNTLocalGroupAttributes(localGroup, rdn);
	            result = new SearchResult("sAMAccountName=" + localGroup.GetAccountName() + "," + container + "," + suffix, null, attributes);
	            results.add(result);
	            resultCount++;
        	}
        	else {
        		// empty set
        	}
        }
        else if((base.toString().compareToIgnoreCase(suffix) == 0) ||
        		base.toString().compareToIgnoreCase(container + "," + suffix) == 0) {
        	if(filter.toString().toLowerCase().startsWith(new String("(sAMAccountName=").toLowerCase())) {
        		rdn = getRDN(filter.toString());
        		
            	if(user.RetriveUserByAccountName(rdn) == 0) {
            		attributes = new BasicAttributes();
    	            
    	            attributes = getNTUserAttributes(user, rdn);
    	            result = new SearchResult("sAMAccountName=" + user.GetAccountName() + "," + container + "," + suffix, null, attributes);
    	            results.add(result);
    	            resultCount++;
            	}
            	else if(group.RetriveGroupByAccountName(rdn) == 0) {
            		attributes = new BasicAttributes();
    	            
    	            attributes = getNTGroupAttributes(group, rdn);
    	            result = new SearchResult("sAMAccountName=" + group.GetAccountName() + "," + container + "," + suffix, null, attributes);
    	            results.add(result);
    	            resultCount++;
            	}
            	else if(localGroup.RetriveLocalGroupByAccountName(rdn) == 0) {
            		attributes = new BasicAttributes();
    	            
    	            attributes = getNTLocalGroupAttributes(localGroup, rdn);
    	            result = new SearchResult("sAMAccountName=" + localGroup.GetAccountName() + "," + container + "," + suffix, null, attributes);
    	            results.add(result);
    	            resultCount++;
            	}
            	else {
            		// empty set
            	}
        	}
        	else if((filter.toString().toLowerCase().startsWith(new String("(objectGUID=").toLowerCase())) ||
        			(filter.toString().toLowerCase().startsWith(new String("(GUID=").toLowerCase()))) {
        		rdn = getRDN(filter.toString());
        		
            	if(user.RetriveUserBySIDHexStr(rdn) == 0) {
            		attributes = new BasicAttributes();
    	            
    	            attributes = getNTUserAttributes(user, rdn);
    	            result = new SearchResult("sAMAccountName=" + user.GetAccountName() + "," + container + "," + suffix, null, attributes);
    	            results.add(result);
    	            resultCount++;
            	}
            	else if(group.RetriveGroupBySIDHexStr(rdn) == 0) {
            		attributes = new BasicAttributes();
    	            
    	            attributes = getNTGroupAttributes(group, rdn);
    	            result = new SearchResult("sAMAccountName=" + group.GetAccountName() + "," + container + "," + suffix, null, attributes);
    	            results.add(result);
    	            resultCount++;
            	}
            	else if(localGroup.RetriveLocalGroupBySIDHexStr(rdn) == 0) {
            		attributes = new BasicAttributes();
    	            
    	            attributes = getNTLocalGroupAttributes(localGroup, rdn);
    	            result = new SearchResult("sAMAccountName=" + localGroup.GetAccountName() + "," + container + "," + suffix, null, attributes);
    	            results.add(result);
    	            resultCount++;
            	}
            	else {
            		// empty set
            	}
        	}
        	else if(filter.toString().toLowerCase().startsWith(new String("(objectClass=*)").toLowerCase())) {
		    	NTUserList users = new NTUserList();
		        if(users.loadList() != 0) {
		            throw new NamingException("Failed to load user list");
		        }
		        while(users.hasMore()) {
		            attributes = new BasicAttributes();
		            
		            rdn = users.nextUsername();
		            if(!rdn.endsWith("$")) {
			            user.RetriveUserByAccountName(rdn);
			            attributes = getNTUserAttributes(user, rdn);
			            result = new SearchResult("sAMAccountName=" + user.GetAccountName() + "," + container + "," + suffix, null, attributes);
			            results.add(result);
			            resultCount++;
		            }
		        }
		        
		        NTGroupList groups = new NTGroupList();
		        if(groups.loadList() != 0) {
		            throw new NamingException("Failed to load group list");
		        }
		        while(groups.hasMore()) {
		            attributes = new BasicAttributes();
		            
		            rdn = groups.nextGroupName();
		            if(!rdn.endsWith("$")) {
		            	group.RetriveGroupByAccountName(rdn);
		            	attributes = getNTGroupAttributes(group, rdn);
		            	result = new SearchResult("sAMAccountName=" + group.GetAccountName() + "," + container + "," + suffix, null, attributes);
		            	results.add(result);
		            	resultCount++;
		            }
		        }
		        
		        NTLocalGroupList localGroups = new NTLocalGroupList();
		        if(localGroups.loadList() != 0) {
		            throw new NamingException("Failed to load local group list");
		        }
		        while(localGroups.hasMore()) {
		            attributes = new BasicAttributes();
		            
		            if(!rdn.endsWith("$")) {
			            rdn = localGroups.nextLocalGroupName();
			            localGroup.RetriveLocalGroupByAccountName(rdn);
			            attributes = getNTLocalGroupAttributes(localGroup, rdn);
			            result = new SearchResult("sAMAccountName=" + localGroup.GetAccountName() + "," + container + "," + suffix, null, attributes);
			            results.add(result);
			            resultCount++;
		            }
		        }
        	}
        	else {
        		throw new NamingException("Unsupported search filter: " + filter);
        	}
        }
        else {
        	throw new NamingException("Bad base DN: " + base);
        }
        
        return resultCount;
    }
    
    private BasicAttributes getNTUserAttributes(NTUser user, String username) throws NamingException {
    	int result = 0;
        BasicAttributes attributes = new BasicAttributes();
        BasicAttribute attribute;
        String tempName;
        
        attribute = new BasicAttribute("objectClass");
        attribute.add("top");
        attribute.add("person");
        attribute.add("organizationalPerson");
        attribute.add("user");
        attributes.put(attribute);
        
        attribute = new BasicAttribute("objectGUID");
        attribute.add(user.GetSIDHexStr());
        attributes.put(attribute);
        
        attribute = new BasicAttribute("objectSid");
        attribute.add(user.GetSIDHexStr());
        attributes.put(attribute);
        
        attribute = new BasicAttribute("accountExpires");
        attribute.add(new Long(user.GetAccountExpires()).toString());
        attributes.put(attribute);

        attribute = new BasicAttribute("badPwdCount");
        attribute.add(new Long(user.GetBadPasswordCount()).toString());
        attributes.put(attribute);

        attribute = new BasicAttribute("codePage");
        attribute.add(new Long(user.GetCodePage()).toString());
        attributes.put(attribute);

        attribute = new BasicAttribute("description");
        attribute.add(user.GetComment());
        attributes.put(attribute);

        attribute = new BasicAttribute("countryCode");
        attribute.add(new Long(user.GetCountryCode()).toString());
        attributes.put(attribute);
        
        attribute = new BasicAttribute("userAccountControl");
        attribute.add(new Long(user.GetFlags()).toString());
        attributes.put(attribute);
        
        attribute = new BasicAttribute("homeDirectory");
        attribute.add(user.GetHomeDir());
        attributes.put(attribute);
        
        attribute = new BasicAttribute("homeDrive");
        attribute.add(user.GetHomeDirDrive());
        attributes.put(attribute);

        attribute = new BasicAttribute("lastLogoff");
        attribute.add(new Long(user.GetLastLogoff()).toString());
        attributes.put(attribute);
        
        attribute = new BasicAttribute("lastLogon");
        attribute.add(new Long(user.GetLastLogon()).toString());
        attributes.put(attribute);

        attribute = new BasicAttribute("logonHours");
        attribute.add(user.GetLogonHours());
        attributes.put(attribute);

        attribute = new BasicAttribute("maxStorage");
        attribute.add(new Long(user.GetMaxStorage()).toString());
        attributes.put(attribute);
        
        attribute = new BasicAttribute("logonCount");
        attribute.add(new Long(user.GetNumLogons()).toString());
        attributes.put(attribute);
        
        attribute = new BasicAttribute("profilePath");
        attribute.add(user.GetProfile());
        attributes.put(attribute);
        
        attribute = new BasicAttribute("scriptPath");
        attribute.add(user.GetScriptPath());
        attributes.put(attribute);
        
        attribute = new BasicAttribute("sAMAccountName");
        attribute.add(username);
        attributes.put(attribute);

        attribute = new BasicAttribute("userWorkstations");
        attribute.add(user.GetWorkstations());
        attributes.put(attribute);
        
        attribute = new BasicAttribute("cn");
        attribute.add(username);
        attributes.put(attribute);
        
        attribute = new BasicAttribute("name");
        attribute.add(user.GetFullname());
        attributes.put(attribute);
        
        attribute = new BasicAttribute("memberOf");
        result = user.LoadGroups();
        if(result != 0) {
        	throw new NamingException("Could not load groups: " + result);
        }
        while(user.HasMoreGroups()) {
        	tempName = user.NextGroupName();
        	if(!tempName.endsWith("$")) {
        		attribute.add("sAMAccountName=" + tempName + "," + container + "," + suffix);
        	}
        }
        result = user.LoadLocalGroups();
        if(result != 0) {
        	throw new NamingException("Could not load local groups: " + result);
        }
        while(user.HasMoreLocalGroups()) {
        	tempName = user.NextLocalGroupName();
        	if(!tempName.endsWith("$")) {
        		attribute.add("sAMAccountName=" + tempName + "," + container + "," + suffix);
        	}
        }
        attributes.put(attribute);
        
        return attributes;
    }
    
    private BasicAttributes getNTGroupAttributes(NTGroup group, String groupName) throws NamingException {
        BasicAttributes attributes = new BasicAttributes();
        BasicAttribute attribute;
        String tempName;
        int result = 0;
        
        attribute = new BasicAttribute("objectClass");
        attribute.add("top");
        attribute.add("group");
        attributes.put(attribute);
        
        attribute = new BasicAttribute("objectGUID");
        attribute.add(group.GetSIDHexStr());
        attributes.put(attribute);
        
        attribute = new BasicAttribute("objectSid");
        attribute.add(group.GetSIDHexStr());
        attributes.put(attribute);
        
        attribute = new BasicAttribute("name");
        attribute.add(groupName);
        attributes.put(attribute);
        
        attribute = new BasicAttribute("sAMAccountName");
        attribute.add(groupName);
        attributes.put(attribute);
        
        attribute = new BasicAttribute("cn");
        attribute.add(groupName);
        attributes.put(attribute);
        
        attribute = new BasicAttribute("groupType");
        attribute.add(new Long(GLOBAL_FLAG).toString());
        attributes.put(attribute);
        
        attribute = new BasicAttribute("member");
        result = group.LoadUsers();
        if(result != 0) {
        	throw new NamingException("Could not load users: " + result);
        }
        while(group.HasMoreUsers()) {
        	tempName = group.NextUserName();
        	// members that end with '$' are supposed to be hidden
        	if(!tempName.endsWith("$")) {
        		attribute.add("sAMAccountName=" + tempName + "," + container + "," + suffix);
        	}
        }
        attributes.put(attribute);
        
        return attributes;
    }
    
    private BasicAttributes getNTLocalGroupAttributes(NTLocalGroup localGroup, String localGroupName) throws NamingException {
        BasicAttributes attributes = new BasicAttributes();
        BasicAttribute attribute;
        String tempName;
        int result = 0;
        
        attribute = new BasicAttribute("objectClass");
        attribute.add("top");
        attribute.add("group");
        attributes.put(attribute);
        
        attribute = new BasicAttribute("objectGUID");
        attribute.add(localGroup.GetSIDHexStr());
        attributes.put(attribute);
        
        attribute = new BasicAttribute("objectSid");
        attribute.add(localGroup.GetSIDHexStr());
        attributes.put(attribute);
        
        attribute = new BasicAttribute("name");
        attribute.add(localGroupName);
        attributes.put(attribute);
        
        attribute = new BasicAttribute("sAMAccountName");
        attribute.add(localGroupName);
        attributes.put(attribute);
        
        attribute = new BasicAttribute("cn");
        attribute.add(localGroupName);
        attributes.put(attribute);
        
        attribute = new BasicAttribute("groupType");
        attribute.add(new Long(DOMAINLOCAL_FLAG).toString());
        attributes.put(attribute);
        
        attribute = new BasicAttribute("member");
        result = localGroup.LoadUsers();
        if(result != 0) {
        	throw new NamingException("Could not load users: " + result);
        }
        while(localGroup.HasMoreUsers()) {
        	tempName = localGroup.NextUserName();
        	// members that end with '$' are supposed to be hidden
        	if(!tempName.endsWith("$")) {
        		attribute.add("sAMAccountName=" + tempName + "," + container + "," + suffix);
        	}
        }
        attributes.put(attribute);
        
        return attributes;
    }
    
    private void modNTUserAttributes(NTUser user, ModificationItem[] mods) throws NamingException {
        for(int i = 0; i < mods.length; i++) {
        	
        	if(mods[i].getAttribute().getID().compareToIgnoreCase("accountExpires") == 0) {
        		if(mods[i].getModificationOp() == DirContext.ADD_ATTRIBUTE) {
        			user.SetAccountExpires(new Long((String)mods[i].getAttribute().get()).longValue());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
        			user.SetAccountExpires(new Long(-1).longValue());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
        			user.SetAccountExpires(new Long((String)mods[i].getAttribute().get()).longValue());
        		}
        	}
        	else if(mods[i].getAttribute().getID().compareToIgnoreCase("codePage") == 0) {
        		if(mods[i].getModificationOp() == DirContext.ADD_ATTRIBUTE) {
        			user.SetCodePage(new Long((String)mods[i].getAttribute().get()).longValue());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
        			user.SetCodePage(new Long(0).longValue());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
        			user.SetCodePage(new Long((String)mods[i].getAttribute().get()).longValue());
        		}
        	}
        	else if(mods[i].getAttribute().getID().compareToIgnoreCase("description") == 0) {
        		if(mods[i].getModificationOp() == DirContext.ADD_ATTRIBUTE) {
        			user.SetComment((String)mods[i].getAttribute().get());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
        			user.SetComment("");
        		}
        		else if(mods[i].getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
        			user.SetComment((String)mods[i].getAttribute().get());
        		}
        	}
        	else if(mods[i].getAttribute().getID().compareToIgnoreCase("countryCode") == 0) {
        		if(mods[i].getModificationOp() == DirContext.ADD_ATTRIBUTE) {
        			user.SetCountryCode(new Long((String)mods[i].getAttribute().get()).longValue());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
        			user.SetCountryCode(new Long(0).longValue());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
        			user.SetCountryCode(new Long((String)mods[i].getAttribute().get()).longValue());
        		}
        	}
        	else if(mods[i].getAttribute().getID().compareToIgnoreCase("userAccountControl") == 0) {
        		if(mods[i].getModificationOp() == DirContext.ADD_ATTRIBUTE) {
        			user.SetFlags(new Long((String)mods[i].getAttribute().get()).longValue());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
        			user.SetFlags(new Long(1).longValue());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
        			user.SetFlags(new Long((String)mods[i].getAttribute().get()).longValue());
        		}
        	}
        	else if(mods[i].getAttribute().getID().compareToIgnoreCase("homeDirectory") == 0) {
        		if(mods[i].getModificationOp() == DirContext.ADD_ATTRIBUTE) {
        			user.SetHomeDir((String)mods[i].getAttribute().get());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
        			user.SetHomeDir("");
        		}
        		else if(mods[i].getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
        			user.SetHomeDir((String)mods[i].getAttribute().get());
        		}
        	}
        	else if(mods[i].getAttribute().getID().compareToIgnoreCase("homeDrive") == 0) {
        		if(mods[i].getModificationOp() == DirContext.ADD_ATTRIBUTE) {
        			user.SetHomeDirDrive((String)mods[i].getAttribute().get());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
        			user.SetHomeDirDrive("");
        		}
        		else if(mods[i].getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
        			user.SetHomeDirDrive((String)mods[i].getAttribute().get());
        		}
        	}
        	else if(mods[i].getAttribute().getID().compareToIgnoreCase("logonHours") == 0) {
        		if(mods[i].getModificationOp() == DirContext.ADD_ATTRIBUTE) {
        			user.SetLogonHours((String)mods[i].getAttribute().get());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
        			user.SetLogonHours("");
        		}
        		else if(mods[i].getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
        			user.SetLogonHours((String)mods[i].getAttribute().get());
        		}
        	}
        	else if(mods[i].getAttribute().getID().compareToIgnoreCase("maxStorage") == 0) {
        		if(mods[i].getModificationOp() == DirContext.ADD_ATTRIBUTE) {
        			user.SetMaxStorage(new Long((String)mods[i].getAttribute().get()).longValue());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
        			user.SetMaxStorage(new Long(-1).longValue());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
        			user.SetMaxStorage(new Long((String)mods[i].getAttribute().get()).longValue());
        		}
        	}
        	else if(mods[i].getAttribute().getID().compareToIgnoreCase("profilePath") == 0) {
        		if(mods[i].getModificationOp() == DirContext.ADD_ATTRIBUTE) {
        			user.SetProfile((String)mods[i].getAttribute().get());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
        			user.SetProfile("");
        		}
        		else if(mods[i].getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
        			user.SetProfile((String)mods[i].getAttribute().get());
        		}
        	}
        	else if(mods[i].getAttribute().getID().compareToIgnoreCase("scriptPath") == 0) {
        		if(mods[i].getModificationOp() == DirContext.ADD_ATTRIBUTE) {
        			user.SetScriptPath((String)mods[i].getAttribute().get());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
        			user.SetScriptPath((String)mods[i].getAttribute().get());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
        			user.SetScriptPath((String)mods[i].getAttribute().get());
        		}
        	}
        	else if(mods[i].getAttribute().getID().compareToIgnoreCase("userWorkstations") == 0) {
        		if(mods[i].getModificationOp() == DirContext.ADD_ATTRIBUTE) {
        			user.SetWorkstations((String)mods[i].getAttribute().get());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
        			user.SetWorkstations("");
        		}
        		else if(mods[i].getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
        			user.SetWorkstations((String)mods[i].getAttribute().get());
        		}
        	}
            else if(mods[i].getAttribute().getID().compareToIgnoreCase("cn") == 0) {
        		if(mods[i].getModificationOp() == DirContext.ADD_ATTRIBUTE) {
        			user.SetFullname((String)mods[i].getAttribute().get());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
        			user.SetFullname("");
        		}
        		else if(mods[i].getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
        			user.SetFullname((String)mods[i].getAttribute().get());
        		}
            }
            else if(mods[i].getAttribute().getID().compareToIgnoreCase("name") == 0) {
        		if(mods[i].getModificationOp() == DirContext.ADD_ATTRIBUTE) {
        			user.SetFullname((String)mods[i].getAttribute().get());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
        			user.SetFullname("");
        		}
        		else if(mods[i].getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
        			user.SetFullname((String)mods[i].getAttribute().get());
        		}
            }
            else if(mods[i].getAttribute().getID().compareToIgnoreCase("unicodePwd") == 0) {
        		if(mods[i].getModificationOp() == DirContext.ADD_ATTRIBUTE) {
        			user.SetPassword((String)mods[i].getAttribute().get());
        		}
        		else if(mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
        			// Do nothing
        		}
        		else if(mods[i].getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
        			user.SetPassword((String)mods[i].getAttribute().get());
        		}
            }
            else if(mods[i].getAttribute().getID().compareToIgnoreCase("memberOf") == 0) {
            	String tempName;
            	
        		if(mods[i].getModificationOp() == DirContext.ADD_ATTRIBUTE) {
        			for(int j = 0; j < mods[i].getAttribute().size(); j++) {
        				tempName = getRDN((String)mods[i].getAttribute().get(j));
        				user.AddToGroup(tempName);
        				user.AddToLocalGroup(tempName);
            		}
        		}
        		else if(mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
        			for(int j = 0; j < mods[i].getAttribute().size(); j++) {
	    				tempName = getRDN((String)mods[i].getAttribute().get(j));
	        			user.RemoveFromGroup(tempName);
	        			user.RemoveFromLocalGroup(tempName);
        			}
        		}
        		else if(mods[i].getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
        			HashSet groups = new HashSet();
        			Object[] deletedGroups;
        			
        			user.LoadGroups();
        			while(user.HasMoreGroups()) {
        				tempName = user.NextGroupName();
        				if(!tempName.endsWith("$")) {
        					groups.add(tempName);
        				}
        			}
        			
        			user.LoadLocalGroups();
        			while(user.HasMoreLocalGroups()) {
        				tempName = user.NextLocalGroupName();
        				if(!tempName.endsWith("$")) {
        					groups.add(tempName);
        				}
        			}
        			
        			for(int j = 0; j < mods[i].getAttribute().size(); j++) {
        				tempName = getRDN((String)mods[i].getAttribute().get(j));
        				if(groups.contains(tempName)) {
        					groups.remove(tempName);
        				}
        				else {
        					user.AddToGroup(tempName);
            				user.AddToLocalGroup(tempName);
        				}
            		}
        			
        			deletedGroups = groups.toArray();
        			for(int j = 0; j < deletedGroups.length; j++) {
        				user.RemoveFromGroup((String)deletedGroups[j]);
        				user.RemoveFromLocalGroup((String)deletedGroups[j]);
        			}
        		}
            }
        }
    }
    
    private void modNTGroupAttributes(NTGroup group, ModificationItem[] mods) throws NamingException {
    	for(int i = 0; i < mods.length; i++) {
	    	if(mods[i].getAttribute().getID().compareToIgnoreCase("member") == 0) {	
	    		String tempName;
	    		
	    		if(mods[i].getModificationOp() == DirContext.ADD_ATTRIBUTE) {
	    			for(int j = 0; j < mods[i].getAttribute().size(); j++) {
	    				tempName = getRDN((String)mods[i].getAttribute().get(j));
	    				group.AddUser((String)mods[i].getAttribute().get(j));
	        		}
	    		}
	    		else if(mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
	    			tempName = getRDN((String)mods[i].getAttribute().get());
	    			group.RemoveUser(tempName);
	    		}
	    		else if(mods[i].getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
	    			HashSet users = new HashSet();
	    			Object[] deletedUsers;
		    		
	    			group.LoadUsers();
	    			while(group.HasMoreUsers()) {
	    				tempName = group.NextUserName();
						if(!tempName.endsWith("$")) {
							users.add(tempName);
						}
	    			}
	    			
	    			for(int j = 0; j < mods[i].getAttribute().size(); j++) {
	    				tempName = getRDN((String)mods[i].getAttribute().get(j));
	    				if(users.contains(tempName)) {
	    					users.remove(tempName);
	    				}
	    				else {
	    					group.AddUser(tempName);
	    				}
	        		}
	    			
	    			deletedUsers = users.toArray();
	    			for(int j = 0; j < deletedUsers.length; j++) {
        				group.RemoveUser((String)deletedUsers[j]);
        			}
	    		}
	        }
    	}
    }
    
    private void modNTLocalGroupAttributes(NTLocalGroup localGroup, ModificationItem[] mods) throws NamingException {
    	for(int i = 0; i < mods.length; i++) {
    		if(mods[i].getAttribute().getID().compareToIgnoreCase("member") == 0) {	
	    		String tempName;
	    		
	    		if(mods[i].getModificationOp() == DirContext.ADD_ATTRIBUTE) {
	    			for(int j = 0; j < mods[i].getAttribute().size(); j++) {
	    				tempName = getRDN((String)mods[i].getAttribute().get(j));
	    				localGroup.AddUser((String)mods[i].getAttribute().get(j));
	        		}
	    		}
	    		else if(mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
	    			tempName = getRDN((String)mods[i].getAttribute().get());
	    			localGroup.RemoveUser(tempName);
	    		}
	    		else if(mods[i].getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
	    			HashSet users = new HashSet();
	    			Object[] deletedUsers;
		    		
	    			localGroup.LoadUsers();
	    			while(localGroup.HasMoreUsers()) {
	    				tempName = localGroup.NextUserName();
						if(!tempName.endsWith("$")) {
							users.add(tempName);
						}
	    			}
	    			
	    			for(int j = 0; j < mods[i].getAttribute().size(); j++) {
	    				tempName = getRDN((String)mods[i].getAttribute().get(j));
	    				if(users.contains(tempName)) {
	    					users.remove(tempName);
	    				}
	    				else {
	    					localGroup.AddUser(tempName);
	    				}
	        		}
	    			
	    			deletedUsers = users.toArray();
	    			for(int j = 0; j < deletedUsers.length; j++) {
        				localGroup.RemoveUser((String)deletedUsers[j]);
        			}
	    		}
	        }
    	}
    }
}