Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.82 KB | None | 0 0
  1. package org.trf.plugins.UserProfileCheckinHook.ldap;
  2.  
  3. import com.dotmarketing.business.APILocator;
  4. import com.dotmarketing.exception.DotRuntimeException;
  5. import com.dotmarketing.plugin.business.PluginAPI;
  6. import com.dotmarketing.util.Logger;
  7. import com.dotmarketing.util.UtilMethods;
  8. import com.novell.ldap.LDAPAttribute;
  9. import com.novell.ldap.LDAPAttributeSet;
  10. import com.novell.ldap.LDAPConnection;
  11. import com.novell.ldap.LDAPEntry;
  12. import com.novell.ldap.LDAPException;
  13. import com.novell.ldap.LDAPJSSESecureSocketFactory;
  14. import com.novell.ldap.LDAPModification;
  15. import com.novell.ldap.LDAPResponse;
  16. import com.novell.ldap.LDAPResponseQueue;
  17. import com.novell.ldap.LDAPSearchResults;
  18. import com.novell.ldap.LDAPSocketFactory;
  19.  
  20. public class UserProfileLDAPCheckinHook {
  21.     private final static PluginAPI pluginAPI = APILocator.getPluginAPI();
  22.     private final static String pluginId = "org.trf.plugins.UserProfileCheckinHook";
  23.    
  24.     private static String SECURITY_AUTHENTICATION;
  25.     private static String SECURITY_KEYSTORE_PATH;
  26.     private static String HOST;
  27.     private static String PORT;
  28.     private static String USERID;
  29.     private static String PASSWORD;
  30.     private static String DOMAINLOOKUP;
  31.     private static String USER_ATTRIB;
  32.     private static boolean inited = false;
  33.    
  34.     private synchronized static void init() {
  35.         if (inited)
  36.             return;
  37.        
  38.         try {
  39.             SECURITY_AUTHENTICATION = pluginAPI.loadProperty(pluginId, "ldap.security.authentication");
  40.             SECURITY_KEYSTORE_PATH = pluginAPI.loadProperty(pluginId, "ldap.security.keystore.path");
  41.             HOST = pluginAPI.loadProperty(pluginId, "ldap.host");
  42.             PORT = pluginAPI.loadProperty(pluginId, "ldap.port");
  43.             USERID = pluginAPI.loadProperty(pluginId, "ldap.userid");
  44.             PASSWORD = pluginAPI.loadProperty(pluginId, "ldap.password");
  45.             DOMAINLOOKUP = pluginAPI.loadProperty(pluginId, "ldap.domainlookup");
  46.             USER_ATTRIB = pluginAPI.loadProperty(pluginId, "ldap.attrib.user");
  47.            
  48.             inited = true;
  49.         } catch (Exception e) {
  50.             Logger.fatal(UserProfileLDAPCheckinHook.class, e.toString());
  51.         }
  52.     }
  53.    
  54.     public static String getDomainLookup() {
  55.         return DOMAINLOOKUP;
  56.     }
  57.    
  58.     public static String getUserAttrib() {
  59.         return USER_ATTRIB;
  60.     }
  61.    
  62.     public static LDAPConnection getBindedConnection() throws DotRuntimeException {
  63.         if (!inited)
  64.             init();
  65.        
  66.         if (!inited) {
  67.             return null;
  68.         } else {
  69.             if (SECURITY_AUTHENTICATION.equalsIgnoreCase("SSL")) {
  70.                 System.setProperty("javax.net.ssl.trustStore", SECURITY_KEYSTORE_PATH);
  71.                 Logger.debug(UserProfileLDAPCheckinHook.class, "The trust store is " + System.getProperty("javax.net.ssl.trustStore"));
  72.                 LDAPSocketFactory ssf = new LDAPJSSESecureSocketFactory();
  73.  
  74.                 // Set the socket factory as the default for all future connections
  75.                 LDAPConnection.setSocketFactory(ssf);
  76.             }
  77.  
  78.             LDAPConnection lc = new LDAPConnection();
  79.             // connect to the server
  80.  
  81.             try {
  82.                 lc.connect(HOST, Integer.valueOf(PORT));
  83.             } catch (Exception e) {
  84.                 Logger.error(UserProfileLDAPCheckinHook.class, e.getMessage(), e);
  85.             }
  86.  
  87.             LDAPResponseQueue queue = null;
  88.             LDAPResponse rsp = null;
  89.             try {
  90.                 queue = lc.bind(LDAPConnection.LDAP_V3, USERID, PASSWORD.getBytes(), (LDAPResponseQueue) null);
  91.  
  92.                 rsp = (LDAPResponse) queue.getResponse();
  93.             }catch(Exception ex){
  94.                 Logger.error(UserProfileLDAPCheckinHook.class, ex.getMessage(), ex);
  95.             }
  96.  
  97.             int rc = rsp.getResultCode();
  98.  
  99.             String msg = rsp.getErrorMessage();
  100.  
  101.             if (rc == LDAPException.SUCCESS) {
  102.                 Logger.debug(UserProfileLDAPCheckinHook.class, "LDAP connection is now bound");
  103.                 return lc;
  104.             } else {
  105.                 throw new DotRuntimeException("Unable to bind to ldap " + msg);
  106.             }
  107.         }
  108.     }
  109.    
  110.     public static LDAPEntry getUserEntry(LDAPConnection lc, String username) {
  111.         LDAPSearchResults searchResults = null;
  112.        
  113.         try {
  114.             searchResults = lc.search(DOMAINLOOKUP, LDAPConnection.SCOPE_SUB, USER_ATTRIB + "=" + username, null, false);
  115.         } catch(Exception ex) {
  116.             Logger.error(UserProfileLDAPCheckinHook.class, "Unable to search for username " + username + " : ", ex);
  117.         }
  118.        
  119.         if (searchResults.hasMore()) {
  120.  
  121.             LDAPEntry ldapEntry = null;
  122.  
  123.             try{
  124.                 ldapEntry = searchResults.next();
  125.             } catch(Exception e) {
  126.                 Logger.error(UserProfileLDAPCheckinHook.class, "Error while trying to bind user " + username + " : ", e);
  127.                 return null;
  128.             }
  129.  
  130.             return ldapEntry;
  131.         } else {
  132.             Logger.info(UserProfileLDAPCheckinHook.class, "Cannot find username: " + username);
  133.             return null;
  134.         }
  135.     }
  136.    
  137.     public static boolean addEntry(LDAPConnection lc, String DN, LDAPAttributeSet attributeSet) {
  138.         if ((attributeSet == null) || (attributeSet.size() == 0))
  139.             return false;
  140.        
  141.         try {
  142.             LDAPEntry newEntry = new LDAPEntry(DN, attributeSet);
  143.             lc.add(newEntry);
  144.            
  145.             return true;
  146.         } catch (Exception e) {
  147.             Logger.warn(UserProfileLDAPCheckinHook.class, e.toString());
  148.         }
  149.        
  150.         return false;
  151.     }
  152.    
  153.     public static boolean modifyEntryAttribute(LDAPConnection lc, String DN, String attrName, String attrValue) {
  154.         if ((lc == null) || !UtilMethods.isSet(DN) || !UtilMethods.isSet(attrName))
  155.             return false;
  156.        
  157.         try {
  158.             LDAPAttribute ldapAttribute = new LDAPAttribute(attrName, attrValue);
  159.             LDAPModification ldapModification = new LDAPModification(LDAPModification.REPLACE, ldapAttribute);
  160.             lc.modify(DN, ldapModification);
  161.            
  162.             return true;
  163.         } catch (Exception e) {
  164.             Logger.warn(UserProfileLDAPCheckinHook.class, e.toString());
  165.         }
  166.        
  167.         return false;
  168.     }
  169.    
  170.     public static boolean modifyEntryAttributes(LDAPConnection lc, String DN, LDAPModification[] ldapModifications) {
  171.         if ((ldapModifications == null) || (ldapModifications.length == 0))
  172.             return false;
  173.        
  174.         try {
  175.             lc.modify(DN, ldapModifications);
  176.            
  177.             return true;
  178.         } catch (Exception e) {
  179.             Logger.warn(UserProfileLDAPCheckinHook.class, e.toString());
  180.         }
  181.        
  182.         return false;
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement