1 46 47 package org.mr.kernel.security.impl.ldap; 48 49 import org.mr.kernel.security.authorization.permissions.MantaPermissionWithParameter; 50 import org.mr.kernel.security.authorization.permissions.MantaPermission; 51 import org.mr.kernel.security.impl.ldap.management.Crypt; 52 import org.mr.kernel.security.MantaSecurityException; 53 import org.mr.kernel.security.GroupPrincipal; 54 import org.mr.kernel.security.*; 55 56 import java.io.UnsupportedEncodingException ; 57 58 67 public class LDAPUtilities implements SecurityConstants { 68 69 88 public static LDAPDN buildPermissionPath(MantaPermission permission, MantaPrincipal principal) throws MantaSecurityException { 89 String searchBase = null; 90 Object param = null; 91 if (permission instanceof MantaPermissionWithParameter){ 92 MantaPermissionWithParameter p = (MantaPermissionWithParameter) permission; 93 param = p.getParam(); 94 } 95 96 String strParam = (String ) param; 97 if (principal instanceof UserPrincipal){ 98 searchBase = (String ) permission.getPermissionPathForUser(); 99 searchBase = replace(searchBase, USERNAME_PLACEHOLDER, principal.getName()); 100 } 101 else if (principal instanceof GroupPrincipal){ 102 searchBase = (String ) permission.getPermissionPathForGroup(); 103 searchBase = replace(searchBase, GROUP_NAME_PLACEHOLDER, principal.getName()); 104 } 105 searchBase = replace(searchBase, TOPIC_NAME_PLACEHOLDER, strParam); 106 searchBase = replace(searchBase, QUEUE_NAME_PLACEHOLDER, strParam); 107 searchBase = replace(searchBase, MANAGED_USERNAME_PLACEHOLDER, strParam); 108 searchBase = replace(searchBase, MANAGED_GROUP_NAME_PLACEHOLDER, strParam); 109 return new LDAPDN(searchBase); 110 } 111 112 121 public static String removePlaceHolders(String source, char placeHolder) throws MantaSecurityException { 122 StringBuffer buf = new StringBuffer (); 123 try { 124 for (int i=0 ; i<source.length() ; i++){ 125 if (source.charAt(i) != placeHolder) 126 buf.append(source.charAt(i)); 127 } 128 } 129 catch (Exception e){ 130 throw new MantaSecurityException("Error removing placeholders"); 131 } 132 return new String (buf); 133 } 134 135 146 public static String crypt(String str){ 147 Crypt c = new Crypt(); 148 byte[] encypted; 149 String ret = null; 150 try { 151 encypted = c.crypt(str.getBytes(UTF_8_CHARSET), str.getBytes(UTF_8_CHARSET)); 152 ret = new String (encypted, UTF_8_CHARSET); 153 } catch (UnsupportedEncodingException e) {} 154 return CRYPT + ret; 155 } 156 157 private static String replace(String source, String key, String value) throws MantaSecurityException { 158 String ret; 159 try { 160 int start = source.indexOf(key); 161 int keyLength = key.length(); 162 if (start == -1) 163 return source; 164 if (start == 0) 165 return value + source.substring(keyLength); 166 ret = source.substring(0, start) + value + source.substring(start+keyLength); 167 } 168 catch (Exception e){ 169 throw new MantaSecurityException("Error building permission LDAP search string"); 170 } 171 return ret; 172 } 173 } 174 | Popular Tags |