1 package com.knowgate.ldap; 2 3 34 35 import java.io.UnsupportedEncodingException ; 36 import java.util.Vector ; 37 import java.util.Properties ; 38 39 import com.novell.ldap.LDAPAttribute; 40 import com.novell.ldap.LDAPAttributeSet; 41 import com.novell.ldap.LDAPEntry; 42 import com.novell.ldap.LDAPConnection; 43 import com.novell.ldap.LDAPSearchResults; 44 import com.novell.ldap.LDAPException; 45 46 import java.sql.Connection ; 47 import java.sql.Statement ; 48 import java.sql.PreparedStatement ; 49 import java.sql.ResultSet ; 50 import java.sql.SQLException ; 51 import java.sql.ResultSetMetaData ; 52 53 import com.knowgate.debug.DebugFile; 54 import com.knowgate.misc.Gadgets; 55 import com.knowgate.dataobjs.*; 56 57 62 63 public class LDAPNovell extends LDAPModel { 64 private LDAPConnection oConn; 65 private String sBase; 66 67 public LDAPNovell() { 68 oConn = null; 69 sBase = null; 70 } 71 72 74 80 public void connect(String sConnStr) throws com.knowgate.ldap.LDAPException { 81 82 if (DebugFile.trace) { 83 DebugFile.writeln("Begin LDAPNovell.connect(" + sConnStr + ")"); 84 DebugFile.incIdent(); 85 } 86 87 if (sConnStr.startsWith("ldap://")) sConnStr = sConnStr.substring(7); 88 89 String sService = sConnStr.substring(0, sConnStr.indexOf('/')); 90 String [] aService = Gadgets.split2(sService, ':'); 91 92 try { 93 oConn = new LDAPConnection(); 94 if (aService.length<2) 95 oConn.connect(aService[0].trim(), 389); 96 else 97 oConn.connect(aService[0].trim(), Integer.parseInt(aService[1])); 98 sBase = sConnStr.substring(sConnStr.indexOf('/')+1); 99 } 100 catch (com.novell.ldap.LDAPException xcpt) { 101 if (DebugFile.trace) 102 DebugFile.decIdent(); 103 sBase = null; 104 if (DebugFile.trace) { 105 DebugFile.writeln("LDAPNovell.connect() LDAPException "+xcpt.getMessage()); 106 DebugFile.decIdent(); 107 } 108 throw new com.knowgate.ldap.LDAPException(xcpt.getMessage(), xcpt); 109 } 110 catch (java.lang.NumberFormatException nfe) { 111 if (DebugFile.trace) { 112 DebugFile.writeln("LDAPNovell.connect() NumberFormatException Invalid port number"); 113 DebugFile.decIdent(); 114 } 115 sBase = null; 116 throw new com.knowgate.ldap.LDAPException("Invalid port number", nfe); 117 } 118 119 if (DebugFile.trace) { 120 DebugFile.decIdent(); 121 DebugFile.writeln("End LDAPNovell.connect()"); 122 } 123 } 125 127 136 public void connectAndBind (Properties oProps) 137 throws com.knowgate.ldap.LDAPException { 138 139 connect (oProps.getProperty("ldapconnect")); 140 bind(oProps.getProperty("ldapuser"), oProps.getProperty("ldappassword")); 141 } 142 143 145 150 public void disconnect() throws com.knowgate.ldap.LDAPException { 151 152 if (DebugFile.trace) { 153 DebugFile.writeln("Begin LDAPNovell.disconnect()"); 154 DebugFile.incIdent(); 155 } 156 157 try { 158 if (oConn!=null) oConn.disconnect(); 159 oConn = null; 160 } 161 catch (com.novell.ldap.LDAPException xcpt) { 162 if (DebugFile.trace) 163 DebugFile.decIdent(); 164 throw new com.knowgate.ldap.LDAPException(xcpt.getMessage(), xcpt); 165 } 166 167 if (DebugFile.trace) { 168 DebugFile.decIdent(); 169 DebugFile.writeln("End LDAPNovell.disconnect()"); 170 } 171 } 173 175 183 public void bind(String sUser, String sPass) throws com.knowgate.ldap.LDAPException,IllegalStateException { 184 185 if (DebugFile.trace) { 186 DebugFile.writeln("Begin LDAPNovell.bind(" + sUser + ",...)"); 187 DebugFile.incIdent(); 188 } 189 190 if (null==oConn) 191 throw new IllegalStateException ("Not connected to LDAP"); 192 193 try { 194 oConn.bind(LDAPConnection.LDAP_V3, sUser, sPass.getBytes("UTF8")); 195 } 196 catch (com.novell.ldap.LDAPException xcpt) { 197 throw new com.knowgate.ldap.LDAPException(xcpt.getMessage(), xcpt); 198 } 199 catch (java.io.UnsupportedEncodingException xcpt) { 200 } 202 203 if (DebugFile.trace) { 204 DebugFile.decIdent(); 205 DebugFile.writeln("End LDAPNovell.bind()"); 206 } 207 } 209 211 219 public boolean exists (String sSearchString) 220 throws com.knowgate.ldap.LDAPException { 221 222 if (DebugFile.trace) { 223 DebugFile.writeln("Begin LDAPNovell.exists(" + sSearchString + ")"); 224 DebugFile.incIdent(); 225 } 226 227 LDAPSearchResults searchResults = null; 228 229 try { 230 searchResults = oConn.search(sBase, LDAPConnection.SCOPE_SUB, sSearchString, new String [] {"dn"}, true); 231 } 232 catch (com.novell.ldap.LDAPException e) { 233 throw new com.knowgate.ldap.LDAPException(e.getMessage(), e); 234 } 235 236 boolean bExists = searchResults.hasMore(); 237 238 if (DebugFile.trace) { 239 DebugFile.decIdent(); 240 DebugFile.writeln("End LDAPNovell.exists() : " + String.valueOf(bExists)); 241 } 242 243 return bExists; 244 } 246 248 private void addHive(String sDN, String sCN) 249 throws com.knowgate.ldap.LDAPException { 250 LDAPAttributeSet attrs; 251 252 if (DebugFile.trace) { 253 DebugFile.writeln("LDAPNovell.addHive(" + sDN + "," + sCN + ")"); 254 } 255 256 try { 257 attrs = new LDAPAttributeSet(); 258 attrs.add(new LDAPAttribute("objectClass", new String [] {"dcObject", 259 "organizationalUnit"})); 260 attrs.add(new LDAPAttribute("dc", sCN)); 261 attrs.add(new LDAPAttribute("ou", sCN)); 262 oConn.add(new LDAPEntry(sDN, attrs)); 263 } 264 catch (com.novell.ldap.LDAPException xcpt) { 265 throw new com.knowgate.ldap.LDAPException(xcpt.getMessage(), xcpt); 266 } 267 } 268 269 271 private void addLeaf(String sDN, LDAPAttributeSet attrs) 272 throws com.knowgate.ldap.LDAPException { 273 274 if (DebugFile.trace) { 275 DebugFile.writeln("LDAPNovell.addLeaf(" + sDN + ", ...)"); 276 } 277 278 try { 279 attrs.add(new LDAPAttribute("objectClass", new String [] {"inetOrgPerson", 280 "organizationalPerson"})); 281 oConn.add(new LDAPEntry(sDN, attrs)); 282 } 283 catch (com.novell.ldap.LDAPException xcpt) { 284 throw new com.knowgate.ldap.LDAPException(xcpt.getMessage() + " " + sDN, xcpt); 285 } 286 } 287 288 290 private LDAPAttributeSet mapJdbcToLdap (ResultSet oRSet, ResultSetMetaData oMDat) 291 throws SQLException { 292 293 Object oFld; 294 String sFld; 295 String sCol; 296 LDAPAttributeSet oAttrs = new LDAPAttributeSet(); 297 298 int iCols = oMDat.getColumnCount(); 299 300 for (int c=1; c<=iCols; c++) { 301 oFld = oRSet.getObject(c); 302 303 if (!oRSet.wasNull()) { 304 sFld = oFld.toString(); 305 sCol = oMDat.getColumnName(c).toLowerCase(); 306 if (!sCol.startsWith("control_")) { 307 oAttrs.add(new LDAPAttribute(sCol, sFld)); 308 } 309 } 310 } 312 return oAttrs; 313 } 314 315 317 331 public void addAddress (Connection oJdbc, String sAddrId) 332 throws com.knowgate.ldap.LDAPException, java.sql.SQLException , java.lang.IllegalStateException { 333 334 PreparedStatement oStmt; 335 ResultSet oRSet; 336 ResultSetMetaData oMDat; 337 LDAPAttributeSet oAttrs = null; 338 boolean bPrivate = true; 339 String sDN = null, sOwner = null, sTxEmail = null; 340 341 if (null==oConn) 342 throw new IllegalStateException ("LDAPNovell.addAddress() Not connected to LDAP"); 343 344 if (DebugFile.trace) { 345 DebugFile.writeln("Begin LDAPNovell.addAddress([Connection], " + sAddrId + ")"); 346 DebugFile.incIdent(); 347 } 348 349 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT * FROM v_ldap_contacts WHERE \"uid\"=" + sAddrId + ")"); 350 351 oStmt = oJdbc.prepareStatement("SELECT * FROM v_ldap_contacts WHERE \"uid\"=?",ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); 352 oStmt.setString(1, sAddrId); 353 oRSet = oStmt.executeQuery(); 354 oMDat = oRSet.getMetaData(); 355 boolean bFound = oRSet.next(); 356 357 if (bFound) { 358 sDN = "dc=" + oRSet.getString("control_workarea_name") + ",dc=" + oRSet.getString("control_domain_name") + "," + sBase; 359 oAttrs = mapJdbcToLdap(oRSet, oMDat); 360 bPrivate = (oRSet.getShort("control_priv") != (short) 0); 361 sOwner = oRSet.getString("control_owner"); 362 sTxEmail = oRSet.getString("mail"); 363 } 364 365 oRSet.close(); 366 oStmt.close(); 367 368 if (!bFound) 369 throw new SQLException ("Address " + sAddrId + " could not be found at v_ldap_contacts view", "01S06"); 370 371 if (bPrivate) { 372 addLeaf ("cn=" + sTxEmail + "dc=privateContacts,cn=" + sOwner + ",dc=users," + sDN, oAttrs); 374 } else { 375 addLeaf ("cn=" + sTxEmail + ",dc=publicContacts," + sDN, oAttrs); 377 } 378 379 if (DebugFile.trace) { 380 DebugFile.decIdent(); 381 DebugFile.writeln("End LDAPNovell.addAddress()"); 382 } 383 } 385 387 396 public void addOrReplaceAddress (Connection oJdbc, String sAddrId) 397 throws com.knowgate.ldap.LDAPException, java.sql.SQLException { 398 399 PreparedStatement oStmt; 400 ResultSet oRSet; 401 ResultSetMetaData oMDat; 402 LDAPAttributeSet oAttrs = null; 403 boolean bPrivate = true; 404 String sDN = null, sOwner = null, sTxEmail = null; 405 406 if (null==oConn) 407 throw new IllegalStateException ("LDAPNovell.addOrReplaceAddress() Not connected to LDAP"); 408 409 if (DebugFile.trace) { 410 DebugFile.writeln("Begin LDAPNovell.addOrReplaceAddress([Connection], " + sAddrId + ")"); 411 DebugFile.incIdent(); 412 } 413 414 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT * FROM v_ldap_contacts WHERE \"uid\"=" + sAddrId + ")"); 415 416 oStmt = oJdbc.prepareStatement("SELECT * FROM v_ldap_contacts WHERE \"uid\"=?",ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); 417 oStmt.setString(1, sAddrId); 418 oRSet = oStmt.executeQuery(); 419 oMDat = oRSet.getMetaData(); 420 boolean bFound = oRSet.next(); 421 422 if (bFound) { 423 sDN = "dc=" + oRSet.getString("control_workarea_name") + ",dc=" + oRSet.getString("control_domain_name"); 424 oAttrs = mapJdbcToLdap(oRSet, oMDat); 425 bPrivate = (oRSet.getShort("control_priv") != (short) 0); 426 sOwner = oRSet.getString("control_owner"); 427 sTxEmail = oRSet.getString("mail"); 428 } 429 430 oRSet.close(); 431 oStmt.close(); 432 433 if (!bFound) 434 throw new SQLException ("Address " + sAddrId + " could not be found at v_ldap_contacts view", "01S06"); 435 436 if (bPrivate) { 437 438 if (exists("cn=" + sTxEmail + "dc=privateContacts,cn=" + sOwner + ",dc=users," + sDN + "," + sBase)) 440 deleteAddress (oJdbc, sAddrId); 441 442 addLeaf ("cn=" + sTxEmail + "dc=privateContacts,cn=" + sOwner + ",dc=users," + sDN + "," + sBase, oAttrs); 443 } else { 444 445 if (exists("cn=" + sTxEmail + ",dc=publicContacts," + sDN + "," + sBase)) 447 deleteAddress (oJdbc, sAddrId); 448 449 addLeaf ("cn=" + sTxEmail + ",dc=publicContacts," + sDN + "," + sBase, oAttrs); 450 } 451 452 if (DebugFile.trace) { 453 DebugFile.decIdent(); 454 DebugFile.writeln("End LDAPNovell.addOrReplaceAddress()"); 455 } 456 } 457 458 460 468 public void deleteAddress (Connection oJdbc, String sAddrId) 469 throws com.knowgate.ldap.LDAPException, java.sql.SQLException , java.lang.IllegalStateException { 470 471 if (null==oConn) 472 throw new IllegalStateException ("LDAPNovell.deleteAddress() Not connected to LDAP"); 473 474 if (DebugFile.trace) { 475 DebugFile.writeln("Begin LDAPNovell.deleteAddress([Connection], " + sAddrId + ")"); 476 DebugFile.incIdent(); 477 } 478 479 LDAPAttributeSet oAttrs; 480 PreparedStatement oStmt; 481 ResultSet oRSet; 482 boolean bPrivate = true; 483 String sDN = null, sOwner = null, sTxEmail = null; 484 485 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT * FROM v_ldap_contacts WHERE \"uid\"=" + sAddrId + ")"); 486 487 oStmt = oJdbc.prepareStatement("SELECT * FROM v_ldap_contacts WHERE \"uid\"=?",ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); 488 oStmt.setString(1, sAddrId); 489 oRSet = oStmt.executeQuery(); 490 boolean bFound = oRSet.next(); 491 492 if (bFound) { 493 sDN = "dc=" + oRSet.getString("control_workarea_name") + ",dc=" + oRSet.getString("control_domain_name") + "," + sBase; 494 bPrivate = (oRSet.getShort("control_priv") != (short) 0); 495 sOwner = oRSet.getString("control_owner"); 496 sTxEmail = oRSet.getString("mail"); 497 } 498 499 oRSet.close(); 500 oStmt.close(); 501 502 if (!bFound) 503 throw new SQLException ("Address " + sAddrId + " could not be found at v_ldap_contacts view", "01S06"); 504 505 if (bPrivate) 506 sDN = "cn=" + sTxEmail + "dc=privateContacts,cn=" + sOwner + ",dc=users," + sDN; 507 else 508 sDN = "cn=" + sTxEmail + ",dc=publicContacts," + sDN; 509 510 if (DebugFile.trace) DebugFile.writeln("LDAPConnection.delete(" + sDN + ")"); 511 512 try { 513 oConn.delete(sDN); 514 } 515 catch (com.novell.ldap.LDAPException xcpt) { 516 if (DebugFile.trace) DebugFile.decIdent(); 517 throw new com.knowgate.ldap.LDAPException (xcpt.getMessage(), xcpt); 518 } 519 520 if (DebugFile.trace) { 521 DebugFile.decIdent(); 522 DebugFile.writeln("End LDAPNovell.deleteAddress()"); 523 } 524 } 526 528 529 538 public void addUser (Connection oJdbc, String sUserId) 539 throws com.knowgate.ldap.LDAPException, java.sql.SQLException ,IllegalStateException { 540 541 PreparedStatement oStmt; 542 ResultSet oRSet; 543 ResultSetMetaData oMDat; 544 LDAPAttributeSet oAttrs = null; 545 String sDN = null, sTxEmail = null; 546 547 if (DebugFile.trace) { 548 DebugFile.writeln("Begin LDAPNovell.addUser([Connection], " + sUserId + ")"); 549 DebugFile.incIdent(); 550 } 551 552 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT * FROM v_ldap_users WHERE \"uid\"=" + sUserId + ")"); 553 554 oStmt = oJdbc.prepareStatement("SELECT * FROM v_ldap_users WHERE \"uid\"=?",ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); 555 oStmt.setString(1, sUserId); 556 oRSet = oStmt.executeQuery(); 557 oMDat = oRSet.getMetaData(); 558 boolean bFound = oRSet.next(); 559 560 if (bFound) { 561 sDN = "dc=" + oRSet.getString("control_workarea_name") + ",dc=" + oRSet.getString("control_domain_name") + "," + sBase; 562 563 oAttrs = mapJdbcToLdap(oRSet, oMDat); 564 565 sTxEmail = oRSet.getString("mail"); 566 } 567 568 oRSet.close(); 569 oStmt.close(); 570 571 if (!bFound) 572 throw new SQLException ("User " + sUserId + " could not be found at v_ldap_users view", "01S06"); 573 574 addLeaf ("cn=" + sTxEmail + ",dc=users," + sDN, oAttrs); 575 576 if (DebugFile.trace) { 577 DebugFile.decIdent(); 578 DebugFile.writeln("End LDAPNovell.addUser()"); 579 } 580 } 582 584 591 public void addOrReplaceUser (Connection oJdbc, String sUserId) 592 throws com.knowgate.ldap.LDAPException, java.sql.SQLException { 593 594 595 PreparedStatement oStmt; 596 ResultSet oRSet; 597 ResultSetMetaData oMDat; 598 LDAPAttributeSet oAttrs = null; 599 String sDN = null, sTxEmail = null; 600 601 if (DebugFile.trace) { 602 DebugFile.writeln("Begin LDAPNovell.addOrReplaceUser([Connection], " + sUserId + ")"); 603 DebugFile.incIdent(); 604 } 605 606 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT * FROM v_ldap_users WHERE \"uid\"=" + sUserId + ")"); 607 608 oStmt = oJdbc.prepareStatement("SELECT * FROM v_ldap_users WHERE \"uid\"=?",ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); 609 oStmt.setString(1, sUserId); 610 oRSet = oStmt.executeQuery(); 611 oMDat = oRSet.getMetaData(); 612 boolean bFound = oRSet.next(); 613 614 if (bFound) { 615 sDN = "dc=" + oRSet.getString("control_workarea_name") + ",dc=" + oRSet.getString("control_domain_name"); 616 617 oAttrs = mapJdbcToLdap(oRSet, oMDat); 618 619 sTxEmail = oRSet.getString("mail"); 620 } 621 622 oRSet.close(); 623 oStmt.close(); 624 625 if (!bFound) 626 throw new SQLException ("User " + sUserId + " could not be found at v_ldap_users view", "01S06"); 627 628 if (exists("cn=" + sTxEmail + ",dc=users," + sDN)) 629 deleteUser(oJdbc, sUserId); 630 631 addLeaf ("cn=" + sTxEmail + ",dc=users," + sDN + "," + sBase, oAttrs); 632 633 if (DebugFile.trace) { 634 DebugFile.decIdent(); 635 DebugFile.writeln("End LDAPNovell.addOrReplaceUser()"); 636 } 637 } 638 639 641 648 public void deleteUser (Connection oJdbc, String sUserId) 649 throws com.knowgate.ldap.LDAPException, java.sql.SQLException { 650 651 LDAPAttributeSet oAttrs; 652 PreparedStatement oStmt; 653 ResultSet oRSet; 654 String sTxEmail = null, sDN = null; 655 656 if (DebugFile.trace) { 657 DebugFile.writeln("Begin LDAPNovell.deleteUser([Connection], " + sUserId + ")"); 658 DebugFile.incIdent(); 659 } 660 661 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT * FROM v_ldap_users WHERE \"uid\"=" + sUserId + ")"); 662 663 oStmt = oJdbc.prepareStatement("SELECT * FROM v_ldap_users WHERE \"uid\"=?",ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); 664 oStmt.setString(1, sUserId); 665 oRSet = oStmt.executeQuery(); 666 boolean bFound = oRSet.next(); 667 668 if (bFound) { 669 sTxEmail = oRSet.getString("mail"); 670 sDN = "cn=" + sTxEmail + ",dc=users," + "dc=" + oRSet.getString("control_workarea_name") + ",dc=" + oRSet.getString("control_domain_name") + "," + sBase; 671 } 672 673 oRSet.close(); 674 oStmt.close(); 675 676 if (!bFound) 677 throw new SQLException ("User " + sUserId + " could not be found at v_ldap_users view", "01S06"); 678 679 if (DebugFile.trace) DebugFile.writeln("LDAPConnection.delete(" + sDN + ")"); 680 681 try { 682 oConn.delete(sDN); 683 } 684 catch (com.novell.ldap.LDAPException xcpt) { 685 if (DebugFile.trace) DebugFile.decIdent(); 686 throw new com.knowgate.ldap.LDAPException (xcpt.getMessage(), xcpt); 687 } 688 689 if (DebugFile.trace) { 690 DebugFile.decIdent(); 691 DebugFile.writeln("End LDAPNovell.deleteUser()"); 692 } 693 } 695 697 704 public void loadDomain (Connection oJdbc, int iDomainId) 705 throws com.knowgate.ldap.LDAPException, java.sql.SQLException { 706 707 LDAPAttributeSet oAttrs; 708 PreparedStatement oStmt; 709 ResultSet oRSet; 710 ResultSetMetaData oMDat; 711 String sDN, sDomainNm, sWorkAreaNm; 712 LDAPSearchResults searchResults = null; 713 714 if (DebugFile.trace) { 715 DebugFile.writeln("Begin LDAPNovell.loadDomain([Connection]" + String.valueOf(iDomainId) + ",...)"); 716 DebugFile.incIdent(); 717 } 718 719 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT nm_domain FROM k_domains WHERE id_domain=" + String.valueOf(iDomainId) + ")"); 720 721 oStmt = oJdbc.prepareStatement("SELECT nm_domain FROM k_domains WHERE id_domain=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 722 oStmt.setInt(1, iDomainId); 723 oRSet = oStmt.executeQuery(); 724 oRSet.next(); 725 sDomainNm = oRSet.getString(1); 726 oRSet.close(); 727 oStmt.close(); 728 729 732 sDN = "dc=" + sDomainNm + "," + sBase; 733 734 try { 735 searchResults = oConn.search(sBase, LDAPConnection.SCOPE_ONE,"(dc=" + sDomainNm + ")", new String [] {"dn"}, true); 736 } 737 catch (com.novell.ldap.LDAPException e) { 738 throw new com.knowgate.ldap.LDAPException(e.getMessage(), e); 739 } 740 741 if (!searchResults.hasMore()) 742 addHive(sDN, sDomainNm); 743 744 747 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT nm_workarea FROM k_workareas WHERE id_domain=" + String.valueOf(iDomainId) + ")"); 748 749 oStmt = oJdbc.prepareStatement("SELECT nm_workarea FROM k_workareas WHERE id_domain=?",ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 750 oStmt.setInt(1, iDomainId); 751 oRSet = oStmt.executeQuery(); 752 753 while (oRSet.next()) { 754 sWorkAreaNm = oRSet.getString(1); 755 756 sDN = "dc=" + sWorkAreaNm + ",dc=" + sDomainNm + "," + sBase; 757 758 try { 759 searchResults = oConn.search("dc=" + sDomainNm + "," + sBase, LDAPConnection.SCOPE_ONE,"(dc=" + sWorkAreaNm + ")", new String [] {"dn"}, true); 760 } 761 catch (com.novell.ldap.LDAPException e) { 762 throw new com.knowgate.ldap.LDAPException(e.getMessage(), e); 763 } 764 765 if (!searchResults.hasMore()) { 766 addHive (sDN, sWorkAreaNm); 768 769 addHive ("dc=users," + sDN, "users"); 771 addHive ("dc=publicContacts," + sDN, "publicContacts"); 772 addHive ("dc=employees," + sDN, "employees"); 773 } 774 } 776 oRSet.close(); 777 oStmt.close(); 778 779 782 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT * FROM v_ldap_users WHERE control_domain_guid=" + String.valueOf(iDomainId) + ")"); 783 784 oStmt = oJdbc.prepareStatement("SELECT * FROM v_ldap_users WHERE control_domain_guid=?",ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); 785 oStmt.setInt(1, iDomainId); 786 oRSet = oStmt.executeQuery(); 787 oMDat = oRSet.getMetaData(); 788 789 while (oRSet.next()) { 790 sWorkAreaNm = oRSet.getString("control_workarea_name"); 791 sDN = "dc=" + sWorkAreaNm + ",dc=" + sDomainNm + "," + sBase; 792 793 oAttrs = mapJdbcToLdap(oRSet, oMDat); 794 795 addLeaf ("cn=" + oRSet.getString("mail") + ",dc=users," + sDN, oAttrs); 797 addHive ("dc=privateContacts,cn=" + oRSet.getString("mail") + ",dc=users," + sDN, "privateContacts"); 799 } 801 oRSet.close(); 802 oStmt.close(); 803 804 807 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT * FROM v_ldap_contacts ld WHERE control_domain_guid=" + String.valueOf(iDomainId) + ")"); 808 809 oStmt = oJdbc.prepareStatement("SELECT * FROM v_ldap_contacts ld WHERE control_domain_guid=?",ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); 810 oStmt.setInt(1, iDomainId); 811 oRSet = oStmt.executeQuery(); 812 oMDat = oRSet.getMetaData(); 813 814 while (oRSet.next()) { 815 sWorkAreaNm = oRSet.getString("control_workarea_name"); 816 sDN = "dc=" + sWorkAreaNm + ",dc=" + sDomainNm + "," + sBase; 817 818 oAttrs = mapJdbcToLdap(oRSet, oMDat); 819 820 if (oRSet.getShort("control_priv")!=(short)0) { 821 addLeaf("cn=" + oRSet.getString("mail") + "dc=privateContacts,cn=" + oRSet.getString("control_owner") + ",dc=users," + sDN, oAttrs); 823 } else { 824 addLeaf("cn=" + oRSet.getString("mail") + ",dc=publicContacts," + sDN, oAttrs); 826 } 827 } 829 if (DebugFile.trace) { 830 DebugFile.decIdent(); 831 DebugFile.writeln("End LDAPNovell.loadDomain()"); 832 } 833 } 835 837 845 public void loadWorkArea (Connection oJdbc, String sDomainNm, String sWorkAreaNm) 846 throws com.knowgate.ldap.LDAPException, java.sql.SQLException { 847 848 LDAPAttributeSet oAttrs; 849 String sDN; 850 LDAPSearchResults searchResults = null; 851 852 if (DebugFile.trace) { 853 DebugFile.writeln("Begin LDAPNovell.loadWorkArea([Connection]" + sDomainNm + "," + sWorkAreaNm + ",...)"); 854 DebugFile.incIdent(); 855 } 856 857 860 sDN = "dc=" + sDomainNm + "," + sBase; 861 862 try { 863 searchResults = oConn.search(sBase, LDAPConnection.SCOPE_ONE,"(dc=" + sDomainNm + ")", new String [] {"dn"}, true); 864 } 865 catch (com.novell.ldap.LDAPException e) { 866 throw new com.knowgate.ldap.LDAPException(e.getMessage(), e); 867 } 868 869 if (!searchResults.hasMore()) 870 addHive(sDN, sDomainNm); 871 872 875 sDN = "dc=" + sWorkAreaNm + ",dc=" + sDomainNm + "," + sBase; 876 877 try { 878 searchResults = oConn.search("dc=" + sDomainNm + "," + sBase, LDAPConnection.SCOPE_ONE,"(dc=" + sWorkAreaNm + ")", new String [] {"dn"}, true); 879 } 880 catch (com.novell.ldap.LDAPException e) { 881 throw new com.knowgate.ldap.LDAPException(e.getMessage(), e); 882 } 883 884 if (!searchResults.hasMore()) { 885 addHive (sDN, sWorkAreaNm); 887 888 addHive ("dc=users," + sDN, "users"); 890 addHive ("dc=publicContacts," + sDN, "publicContacts"); 891 addHive ("dc=employees," + sDN, "employees"); 892 } 894 895 898 PreparedStatement oStmt = oJdbc.prepareStatement("SELECT * FROM v_ldap_users WHERE control_domain_name=? AND control_workarea_name=?",ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); 899 oStmt.setString(1, sDomainNm); 900 oStmt.setString(2, sWorkAreaNm); 901 ResultSet oRSet = oStmt.executeQuery(); 902 ResultSetMetaData oMDat = oRSet.getMetaData(); 903 904 while (oRSet.next()) { 905 sWorkAreaNm = oRSet.getString("control_workarea_name"); 906 sDN = "dc=" + sWorkAreaNm + ",dc=" + sDomainNm + "," + sBase; 907 908 oAttrs = mapJdbcToLdap(oRSet, oMDat); 909 910 addLeaf ("cn=" + oRSet.getString("mail") + ",dc=users," + sDN, oAttrs); 912 addHive ("dc=privateContacts,cn=" + oRSet.getString("mail") + ",dc=users," + sDN, "privateContacts"); 914 } 916 oRSet.close(); 917 oStmt.close(); 918 919 922 oStmt = oJdbc.prepareStatement("SELECT * FROM v_ldap_contacts ld WHERE control_domain_name=? AND control_workarea_name=?",ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); 923 oStmt.setString(1, sDomainNm); 924 oStmt.setString(2, sWorkAreaNm); 925 oRSet = oStmt.executeQuery(); 926 oMDat = oRSet.getMetaData(); 927 928 while (oRSet.next()) { 929 sWorkAreaNm = oRSet.getString("control_workarea_name"); 930 sDN = "dc=" + sWorkAreaNm + ",dc=" + sDomainNm + "," + sBase; 931 932 oAttrs = mapJdbcToLdap(oRSet, oMDat); 933 934 if (oRSet.getShort("control_priv")!=(short)0) { 935 addLeaf("cn=" + oRSet.getString("mail") + "dc=privateContacts,cn=" + oRSet.getString("control_owner") + ",dc=users," + sDN, oAttrs); 937 } else { 938 addLeaf("cn=" + oRSet.getString("mail") + ",dc=publicContacts," + sDN, oAttrs); 940 } 941 } 943 if (DebugFile.trace) { 944 DebugFile.decIdent(); 945 DebugFile.writeln("End LDAPNovell.loadWorkArea()"); 946 } 947 } 949 951 957 private void deleteEntry (LDAPEntry oEntry) 958 throws com.knowgate.ldap.LDAPException, IllegalStateException { 959 960 if (null==oConn) 961 throw new IllegalStateException ("LDAPNovell.deleteEntry() Not connected to LDAP"); 962 963 String sDN = oEntry.getDN(); 964 LDAPEntry nextEntry; 965 966 try { 967 968 LDAPSearchResults searchResults = oConn.search(sDN, LDAPConnection.SCOPE_ONE,"(objectClass=*)",new String [] {"dn"}, true); 969 970 while (searchResults.hasMore()) { 971 try { 972 nextEntry = searchResults.next(); 973 } 974 catch (com.novell.ldap.LDAPException e) { continue; } 975 976 deleteEntry (nextEntry); 977 } 979 if (DebugFile.trace) DebugFile.writeln("LDAPConnection.delete(" + sDN + ")"); 980 981 oConn.delete(sDN); 982 } 983 catch (com.novell.ldap.LDAPException xcpt) { 984 throw new com.knowgate.ldap.LDAPException (xcpt.getMessage(), xcpt); 985 } 986 } 988 990 998 public void deleteWorkArea (String sDomainNm, String sWorkAreaNm) 999 throws com.knowgate.ldap.LDAPException, IllegalStateException { 1000 1001 String sDN = "dc=" + sDomainNm + "," + sBase; 1002 LDAPEntry oWrkAHive = null; 1003 1004 try { 1005 LDAPSearchResults searchResults = oConn.search(sDN, LDAPConnection.SCOPE_ONE,"(dc=" + sWorkAreaNm + ")", new String [] {"dn"}, true); 1006 1007 if (searchResults.hasMore()) { 1008 oWrkAHive = searchResults.next(); 1009 } 1010 } 1011 catch (com.novell.ldap.LDAPException e) { 1012 throw new com.knowgate.ldap.LDAPException(e.getMessage(), e); 1013 } 1014 1015 if (oWrkAHive!=null) 1016 deleteEntry (oWrkAHive); 1017 } 1019 1021 1026 public void dropAll () 1027 throws com.knowgate.ldap.LDAPException, IllegalStateException { 1028 1029 if (null==oConn) 1030 throw new IllegalStateException ("LDAPNovell.dropAll() Not connected to LDAP"); 1031 1032 if (DebugFile.trace) { 1033 DebugFile.writeln("Begin LDAPNovell.dropAll()"); 1034 DebugFile.incIdent(); 1035 } 1036 1037 LDAPSearchResults searchResults = null; 1038 LDAPEntry nextEntry = null; 1039 1040 try { 1041 1043 searchResults = oConn.search(sBase, LDAPConnection.SCOPE_ONE,"(objectClass=*)",new String [] {"dn"}, true); 1044 1045 while (searchResults.hasMore()) { 1046 try { 1047 nextEntry = searchResults.next(); 1048 } 1049 catch (LDAPException e) { continue; } 1050 1051 if (!sBase.equals(nextEntry.getDN())) 1052 deleteEntry(nextEntry); } } 1055 catch (com.novell.ldap.LDAPException xcpt) { 1056 throw new com.knowgate.ldap.LDAPException (xcpt.getMessage(), xcpt); 1057 } 1058 1059 if (DebugFile.trace) { 1060 DebugFile.decIdent(); 1061 DebugFile.writeln("End LDAPNovell.dropAll()"); 1062 } 1063 } 1065 1067 private static void printUsage() { 1068 System.out.println(""); 1069 System.out.println("Usage:"); 1070 System.out.println("LDAPNovell path load all"); 1071 System.out.println("path: path to hipergate.cnf file ej. /opt/knowgate/hipergate.cnf"); 1072 } 1073 1074 public static void main(String [] argv) 1075 throws java.lang.ClassNotFoundException , java.io.IOException , java.sql.SQLException , com.knowgate.ldap.LDAPException { 1076 1077 if (argv.length<3 || argv.length>3) 1078 printUsage(); 1079 else { 1080 java.util.Properties oProps = new java.util.Properties (); 1081 java.io.FileInputStream ioProps = new java.io.FileInputStream (argv[0]); 1082 oProps.load(ioProps); 1083 ioProps.close(); 1084 1085 LDAPNovell oLDP = new LDAPNovell(); 1086 1087 oLDP.connectAndBind(oProps); 1088 1089 Class oDriver = Class.forName(oProps.getProperty("driver")); 1090 1091 Connection oCon = java.sql.DriverManager.getConnection(oProps.getProperty("dburl"), oProps.getProperty("dbuser"), oProps.getProperty("dbpassword")); 1092 1093 oLDP.dropAll(); 1094 1095 Statement oStm = oCon.createStatement(); 1096 ResultSet oRst = oStm.executeQuery("SELECT id_domain FROM k_domains WHERE bo_active<>0"); 1097 1098 while (oRst.next()) { 1099 oLDP.loadDomain(oCon, oRst.getInt(1)); 1100 } 1101 1102 oRst.close(); 1103 oStm.close(); 1104 1105 oCon.close(); 1106 oLDP.disconnect(); 1107 } 1108 } 1109} 1110 | Popular Tags |