1 24 25 package org.objectweb.cjdbc.controller.jmx; 26 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Set ; 32 33 import javax.management.InstanceNotFoundException ; 34 import javax.management.MBeanException ; 35 import javax.management.MBeanServer ; 36 import javax.management.ObjectName ; 37 import javax.management.ReflectionException ; 38 import javax.security.auth.Subject ; 39 40 import org.objectweb.cjdbc.common.jmx.JmxConstants; 41 import org.objectweb.cjdbc.common.log.Trace; 42 import org.objectweb.cjdbc.common.users.AbstractDatabaseUser; 43 44 51 public class AuthenticatingMBeanServer extends ChainedMBeanServer 52 { 53 54 55 static Trace logger = Trace 56 .getLogger("org.objectweb.cjdbc.controller.jmx.AuthenticatingMBeanServer"); 57 58 64 public void setMBeanServer(MBeanServer server) 65 { 66 super.setMBeanServer(server); 67 } 68 69 73 public Object invoke(ObjectName name, String operationName, Object [] params, 74 String [] signature) throws InstanceNotFoundException , MBeanException , 75 ReflectionException 76 { 77 if (JmxConstants.mbeanNeedAuthentication(name) 78 && (operationName.equalsIgnoreCase("checkAdminAuthentication") == false)) 79 { 80 boolean authenticationOk = false; 84 String username = null; 85 String password = null; 86 87 Subject subject = Subject.getSubject(java.security.AccessController 88 .getContext()); 89 if (subject == null || subject.getPrincipals().size() == 0) 90 { 91 username = (String ) params[0]; 92 password = (String ) params[1]; 93 authenticationOk = authenticate(name, username, password); 94 if (!authenticationOk) 95 throw new MBeanException (new Exception ( 96 "Authentication failed (username,password) invalid")); 97 98 if (logger.isDebugEnabled()) 99 logger 100 .debug("Authentication with username and password was successfull"); 101 102 return super.invoke(name, operationName, cleanO(params), 105 cleanS(signature)); 106 } 107 else 108 { 109 Set principals = subject.getPrincipals(AbstractDatabaseUser.class); 110 for (Iterator it = principals.iterator(); it.hasNext();) 111 { 112 AbstractDatabaseUser user = (AbstractDatabaseUser) it.next(); 113 username = user.getName(); 114 password = user.getPassword(); 115 authenticationOk = authenticate(name, username, password); 116 if (authenticationOk) 117 break; 118 } 119 120 if (principals.size() == 0 && logger.isDebugEnabled()) 121 throw new MBeanException (new Exception ( 122 "Authentication failed : no principal")); 123 124 if (!authenticationOk) 125 throw new MBeanException (new Exception ( 126 "Authentication failed : principal invalid")); 127 if (logger.isDebugEnabled()) 128 logger.debug("Authentication with principal was successfull"); 129 return super.invoke(name, operationName, params, signature); 130 } 131 } 132 else 133 { 134 if (logger.isDebugEnabled()) 135 logger.debug("no authentication required"); 136 137 return super.invoke(name, operationName, params, signature); 138 } 139 } 140 141 private boolean authenticate(ObjectName name, String username, String password) 142 { 143 try 144 { 145 boolean vdb = name.toString().indexOf( 146 JmxConstants.CJDBC_TYPE_VIRTUALDATABASE) != -1; 147 if (vdb) 148 return ((Boolean ) invoke(name, "checkAdminAuthentication", 149 new Object []{username, password}, new String []{"java.lang.String", 150 "java.lang.String"})).booleanValue(); 151 else 152 { 153 boolean backend = name.toString().indexOf( 154 JmxConstants.CJDBC_TYPE_BACKEND) != -1; 155 if (backend) 156 { 157 ObjectName vdbName = JmxConstants 159 .getVirtualDbObjectNameFromBackend(name); 160 return ((Boolean ) invoke(vdbName, "checkAdminAuthentication", 161 new Object []{username, password}, new String []{ 162 "java.lang.String", "java.lang.String"})).booleanValue(); 163 } 164 else 165 return true; 167 } 168 } 169 catch (Exception e) 170 { 171 if (logger.isDebugEnabled()) 172 { 173 logger.debug("authentication failed with exception ", e); 174 } 175 return false; 176 } 177 } 178 179 private static Object [] cleanO(Object [] params) 180 { 181 List o = Arrays.asList(params); 182 o = o.subList(2, o.size()); 183 return (new ArrayList (o).toArray()); 184 } 185 186 private static String [] cleanS(String [] params) 187 { 188 List o = Arrays.asList(params); 189 o = o.subList(2, o.size()); 190 String [] s = new String [o.size()]; 191 return (String []) new ArrayList (o).toArray(s); 192 } 193 } | Popular Tags |