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
|
/*
* Directory Lookup Service *
* author: Florida Estrella
*/
package com.c2kernel.lookup;
import java.util.StringTokenizer;
import com.c2kernel.common.ObjectAlreadyExistsException;
import com.c2kernel.common.ObjectCannotBeUpdated;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.TraceableEntity;
import com.c2kernel.entity.agent.ActiveEntity;
import com.c2kernel.entity.proxy.EntityProxyManager;
import com.c2kernel.entity.proxy.ProxyMessage;
import com.c2kernel.process.Gateway;
import com.c2kernel.utils.Logger;
import com.c2kernel.utils.Resource;
import com.novell.ldap.LDAPAttributeSet;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPDN;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPSearchConstraints;
import com.novell.ldap.LDAPSearchResults;
/**
* The LDAPLookup object, statically accessible through the Gateway, manages
* the LDAP connection for the cristal process. It provides:
* <ul>
* <li>Authentication - returning an AgentProxy object if a user has logged in
* <li>System key generation - through the NextKeyManager
* <li>Agent and Role lookup/modification - through the RoleManager
* <li>
* @version $Revision: 1.113 $ $Date: 2006/03/03 13:52:21 $
* @author $Author: abranson $
*/
public class LDAPLookup
{
private LDAPConnection mLDAPConn;
private final LDAPProperties mLDAPProps;
private final NextKeyManager mNextKeyManager;
private LDAPPropertyManager mPropManager;
private final LDAPRoleManager mRoleManager;
/**
* Creates a new LDAPLookup manager with the properties supplied.
* This should be only done by the Gateway during initialisation.
*
* @param props The LDAP properties object that extracts LDAP connection properties from the global c2kprops
*/
public LDAPLookup(LDAPProperties props) throws LDAPException
{
Logger.msg(8,"LDAPLookup - initialising.");
mLDAPProps = props;
mLDAPConn = createConnection(mLDAPProps);
Path.mGlobalPath=props.mGlobalPath;
Path.mRootPath=props.mRootPath;
Path.mLocalPath=props.mLocalPath;
EntityPath.mTypeRoot = "cn=entity,"+props.mLocalPath;
DomainPath.mTypeRoot = "cn=domain,"+props.mLocalPath;
mNextKeyManager = new NextKeyManager(this, "cn=last,"+EntityPath.mTypeRoot);
Logger.debug("LDAP.useOldProps="+Gateway.getProperty("LDAP.useOldProps", "false"));
if (Gateway.getProperty("LDAP.useOldProps", "false").equals("true")) {
Logger.debug("Using Kernel 2.1 LDAP Property Format");
mPropManager = new LegacyLDAPPropertyManager(this);
}
else {
Logger.debug("Using Kernel 2.2 LDAP Property Format");
mPropManager = new LDAPPropertyManager(this);
}
mRoleManager = new LDAPRoleManager(this, "cn=agent,"+DomainPath.mTypeRoot, EntityPath.mTypeRoot);
}
/**
* Utility method to connect to an LDAP server
* @param lp LDAP properties to connect with
* @return a novell LDAPConnection object
* @throws LDAPException when the connection was unsuccessful
*/
public static LDAPConnection createConnection(LDAPProperties lp) throws LDAPException {
LDAPConnection ld = new LDAPConnection();
Logger.msg(3, "LDAPLookup - connecting to " + lp.mHost);
ld.connect(lp.mHost, Integer.valueOf(lp.mPort).intValue());
Logger.msg(3, "LDAPLookup - authenticating user:" + lp.mUser);
ld.bind( LDAPConnection.LDAP_V3, lp.mUser,
String.valueOf(lp.mPassword).getBytes());
Logger.msg(3, "LDAPLookup - authentication successful");
LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
searchCons.setMaxResults(0);
ld.setConstraints(searchCons);
return ld;
}
/**
* Gets the entity key generator, used to get a unique system key for new entities.
* @return the global NextKeyManager
*/
public NextKeyManager getNextKeyManager()
{
return mNextKeyManager;
}
/**
* Gets the property manager, that is used to read and write cristal properties to the LDAP store.
* @return Returns the global LDAPPropertyManager.
*/
public LDAPPropertyManager getPropManager() {
return mPropManager;
}
/**
* Gets the role manager, that is used to add and remove roles and agents.
* @return Returns the mRoleManager.
*/
public LDAPRoleManager getRoleManager() {
return mRoleManager;
}
/**
* Returns the current LDAP connection, and attempts to reconnect if it has been closed.
* @return
*/
protected LDAPConnection getConnection()
{
if (!mLDAPConn.isConnected()) {
Logger.warning("LDAPLookup - lost connection to LDAP server. Attempting to reconnect.");
try {
mLDAPConn = createConnection(mLDAPProps);
} catch (LDAPException ex) { }
}
return mLDAPConn;
}
/**
* Disconnects the connection with the LDAP server during shutdown
*/
public void disconnect() {
Logger.msg(1, "LDAP Lookup: Shutting down LDAP connection.");
if (mLDAPConn != null) {
try {
mLDAPConn.disconnect();
} catch (LDAPException e) {
Logger.error(e);
}
mLDAPConn = null;
}
}
/**
* Attempts to resolve the CORBA object for a Path, either directly or through an alias.
* @param path the path to resolve
* @return the CORBA object
* @throws ObjectNotFoundException When the path does not exist
*/
public org.omg.CORBA.Object getIOR(Path path)
throws ObjectNotFoundException
{
return resolveObject(path.getFullDN());
}
/**
* Attempts to resolve the CORBA object from the IOR attribute of a DN, either directly or through an alias
* @param dn The String dn
* @throws ObjectNotFoundException when the dn or aliased dn does not exist
*/
private org.omg.CORBA.Object resolveObject(String dn)
throws ObjectNotFoundException
{
Logger.msg(8,"LDAPLookup.resolveObject("+dn+")");
LDAPEntry anEntry = LDAPLookupUtils.getEntry(getConnection(),dn,LDAPSearchConstraints.DEREF_NEVER);
if (anEntry != null)
{
String iorString;
try {
iorString = LDAPLookupUtils.getFirstAttributeValue(anEntry, "ior");
org.omg.CORBA.Object ior=Gateway.getORB().string_to_object(iorString);
if (ior!=null)
return ior;
else
throw new ObjectNotFoundException("LDAPLookup.resolveObject() - " + dn + " has no IOR", "");
} catch (ObjectNotFoundException ex) {
return resolveObject(LDAPLookupUtils.getFirstAttributeValue(anEntry,"aliasedObjectName"));
}
}
else
throw new ObjectNotFoundException("LDAPLookup.resolveObject() LDAP node " + dn + " is not in LDAP or has no IOR.", "");
}
/**
*
* @param domPath
* @return
* @throws InvalidEntityPathException
* @throws ObjectNotFoundException
*/
protected EntityPath resolvePath(DomainPath domPath)
throws InvalidEntityPathException, ObjectNotFoundException {
EntityPath referencedPath = null;
LDAPEntry domEntry = LDAPLookupUtils.getEntry(getConnection(), domPath
.getFullDN(), LDAPSearchConstraints.DEREF_ALWAYS);
String entityKey = LDAPLookupUtils.getFirstAttributeValue(domEntry,
"intsyskey");
Logger.msg(7, "DomainPath " + domPath + " is a reference to "
+ entityKey);
String objClass = LDAPLookupUtils.getFirstAttributeValue(domEntry,
"objectClass");
if (objClass.equals("cristalagent"))
referencedPath = new AgentPath(Integer.parseInt(entityKey));
else
referencedPath = new EntityPath(Integer.parseInt(entityKey));
return referencedPath;
}
public LDAPEntry add(Path path)
throws ObjectCannotBeUpdated, ObjectAlreadyExistsException
{
try {
checkLDAPContext(path);
LDAPAttributeSet attrSet = path.createAttributeSet();
LDAPEntry newEntry = new LDAPEntry(path.getFullDN(),attrSet);
LDAPLookupUtils.addEntry(getConnection(),newEntry);
if (path instanceof DomainPath)
EntityProxyManager.sendProxyEvent(new ProxyMessage(ProxyMessage.NA, path.toString(), ProxyMessage.ADDED));
return newEntry;
} catch (LDAPException ex) {
if (ex.getResultCode() == LDAPException.ENTRY_ALREADY_EXISTS)
throw new ObjectAlreadyExistsException(ex.getLDAPErrorMessage(), "");
else
throw new ObjectCannotBeUpdated(ex.getLDAPErrorMessage(), "");
}
}
//deletes a node
//throws LDAPexception if node cannot be deleted (eg node is not a leaf)
public void delete(Path path) throws ObjectCannotBeUpdated
{
try {
LDAPLookupUtils.delete(getConnection(),path.getDN()+Path.mLocalPath);
} catch (LDAPException ex) {
throw new ObjectCannotBeUpdated(ex.getLDAPErrorMessage(), "");
}
if (path instanceof DomainPath) {
EntityProxyManager.sendProxyEvent(new ProxyMessage(ProxyMessage.NA, path.toString(), ProxyMessage.DELETED));
}
}
//change specs, add boolean alias leaf context
protected void checkLDAPContext(Path path)
{
String dn = path.getFullDN();
if (!LDAPLookupUtils.exists(getConnection(),dn))
{
String listDN[] = path.getPath();
String name = "cn="+ path.getRoot() + "," + Path.mLocalPath;
int i=0;
while (i<listDN.length-1)
{
name= "cn="+listDN[i]+","+name;
if (!LDAPLookupUtils.exists(getConnection(),name))
{
try
{
//create cristalcontext
Logger.msg(8,"LDAPLookup::addLDAPContext() context added " + name);
LDAPLookupUtils.createCristalContext(getConnection(), name);
}
catch (Exception ex)
{
Logger.error("LDAPLookup::addContext() " + ex);
}
}
i++;
}
}
}
public void createBootTree()
{
Logger.msg(8,"Initializing LDAP Boot tree");
//create org
LDAPLookupUtils.createOrganizationContext(getConnection(), Path.mGlobalPath);
//create root
LDAPLookupUtils.createCristalContext(getConnection(), Path.mRootPath);
//create local
LDAPLookupUtils.createCristalContext(getConnection(), Path.mLocalPath);
}
public void install() throws ObjectNotFoundException
{
createBootTree();
initTree( Resource.getTextResource(null, "boot/LDAPboot.txt"));
}
public void initTree(String bootFile)
{
Logger.msg(8,"Verifying Cristal LDAP roots");
StringTokenizer strTokenizer = new StringTokenizer(bootFile, "\n\r");
while (strTokenizer.hasMoreTokens())
{
String line = strTokenizer.nextToken();
Logger.msg(8,"Checking " + line+Path.mLocalPath);
LDAPLookupUtils.createCristalContext(getConnection(), line+Path.mLocalPath);
}
}
//typically search for cn=barcode
public LDAPPathSet search(Path start, String filter)
{
Logger.msg(8,"LDAPLookup::search() From " + start.getDN() + " for cn=" + filter );
return search(start.getFullDN(),"cn="+LDAPLookupUtils.escapeSearchFilter(filter));
}
protected LDAPPathSet search(String startDN, int scope, String filter, LDAPSearchConstraints searchCons)
{
Logger.msg(8,"Searching for "+filter+" in "+startDN);
searchCons.setMaxResults(0);
String[] attr = { LDAPConnection.ALL_USER_ATTRS };
try
{
LDAPSearchResults res = getConnection().search(LDAPLookupUtils.escapeDN(startDN),scope,
filter,attr,false,searchCons);
return new LDAPPathSet(res);
}
catch (LDAPException ex)
{
Logger.error("LDAPException::LDAPLookup::search() " + ex.toString());
return new LDAPPathSet();
}
}
//typically search for (any filter combination)
public LDAPPathSet search(String startDN,String filter)
{
LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
searchCons.setBatchSize(0);
searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER);
return search(startDN,LDAPConnection.SCOPE_SUB,filter,searchCons);
}
public LDAPPathSet searchEntities(Path start) {
LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
searchCons.setBatchSize(0);
searchCons.setDereference(LDAPSearchConstraints.DEREF_SEARCHING);
return search(start.getFullDN(), LDAPConnection.SCOPE_SUB, "objectClass=cristalentity", searchCons);
}
public LDAPPathSet searchAliases(DomainPath start) {
LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
searchCons.setBatchSize(0);
searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER);
return search(start.getFullDN(), LDAPConnection.SCOPE_SUB, "objectClass=aliasObject", searchCons);
}
public boolean exists(Path path) {
return LDAPLookupUtils.exists(getConnection(), path.getFullDN());
}
public Class<?> getEntityClass(Path path) throws ObjectNotFoundException {
String[] attr = { LDAPConnection.ALL_USER_ATTRS };
try {
LDAPEntry anEntry=getConnection().read(path.getDN()+Path.mLocalPath,attr);
String type = LDAPLookupUtils.getFirstAttributeValue(anEntry, "objectClass");
if (type.equals("cristalentity"))
return TraceableEntity.class;
else if (type.equals("cristalagent"))
return ActiveEntity.class;
else
throw new ObjectNotFoundException("Not an entity", "");
} catch (LDAPException ex) {
if (ex.getResultCode() == LDAPException.NO_SUCH_OBJECT)
throw new ObjectNotFoundException("Entity does not exist", "");
Logger.error(ex);
throw new ObjectNotFoundException("Error getting entity class", "");
}
}
/** converts an LDAPentry to a Path object
* Note that the search producing the entry should have retrieved the attrs
* 'ior' and 'uniquemember'
* @throws ObjectNotFoundException
* @throws ObjectNotFoundException
*/
protected Path nodeToPath(LDAPEntry entry) throws InvalidEntityPathException, ObjectNotFoundException
{
String dn = entry.getDN();
// extract syskey
int entityKey = -1;
try {
String entityKeyStr = LDAPLookupUtils.getFirstAttributeValue(entry,"intsyskey");
entityKey = Integer.parseInt(entityKeyStr);
} catch (Exception e) { }
// extract IOR
org.omg.CORBA.Object ior = null;
try {
String stringIOR = LDAPLookupUtils.getFirstAttributeValue(entry,"ior");
ior = Gateway.getORB().string_to_object(stringIOR);
} catch (ObjectNotFoundException e2) { }
/* Find the right path class */
Path thisPath;
if (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalagent"))
{ //cristalagent
String agentID = LDAPLookupUtils.getFirstAttributeValue(entry,"uid");
thisPath = new AgentPath(entityKey, agentID);
}
else if (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalrole"))
{ //cristalrole
thisPath = new RolePath(LDAPDN.explodeDN(dn,true)[0],
LDAPLookupUtils.getFirstAttributeValue(entry, "jobList").equals("TRUE"));
}
else if (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","aliasObject") ||
(LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalcontext") && dn.endsWith(DomainPath.mTypeRoot)))
{
DomainPath domainPath = new DomainPath();
domainPath.setDN(dn);
thisPath = domainPath;
}
else if (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalentity") ||
(LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalcontext") && dn.endsWith(EntityPath.mTypeRoot)))
{
if(dn.endsWith(EntityPath.mTypeRoot)) {
EntityPath entityPath;
if (entityKey != -1)
entityPath = new EntityPath(entityKey);
else {
entityPath = new EntityPath();
entityPath.setDN(dn);
}
thisPath = entityPath;
}
else
throw new ObjectNotFoundException("Entity found outside entity tree");
}
else
{
throw new ObjectNotFoundException("Unrecognised LDAP entry. Not a cristal entry");
}
//set IOR if we have one
if (ior!=null) thisPath.setIOR(ior);
return thisPath;
}
}
|