1 22 package org.jboss.mq.security; 23 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.Set ; 28 import javax.jms.Destination ; 29 import javax.jms.JMSException ; 30 import javax.jms.JMSSecurityException ; 31 import javax.jms.InvalidDestinationException ; 32 import javax.jms.TemporaryQueue ; 33 import javax.jms.TemporaryTopic ; 34 import org.jboss.mq.ConnectionToken; 35 import org.jboss.mq.SpyMessage; 36 import org.jboss.mq.SpyDestination; 37 import org.jboss.mq.SpyTopic; 38 import org.jboss.mq.DurableSubscriptionID; 39 import org.jboss.mq.Subscription; 40 import org.jboss.mq.TransactionRequest; 41 import org.jboss.mq.server.JMSServerInterceptorSupport; 42 48 public class ServerSecurityInterceptor extends JMSServerInterceptorSupport 49 { 50 SecurityManager manager; 51 52 55 private HashMap tempDests = new HashMap (); 56 57 public ServerSecurityInterceptor(SecurityManager manager) 58 { 59 super(); 60 this.manager = manager; 61 } 62 63 public String authenticate(String name, String password) throws JMSException 64 { 65 log.trace("Authenticating user " + name); 66 return manager.authenticate(name, password); 67 } 68 69 75 public void connectionClosing(ConnectionToken dc) throws JMSException 76 { 77 super.connectionClosing(dc); 78 manager.logout(dc); 79 removeTemporaryDestinations(dc); 80 } 81 82 90 public SpyMessage[] browse(ConnectionToken dc, Destination dest, String selector) throws JMSException 94 { 95 if (log.isTraceEnabled()) 96 log.trace("Checking browse authorize on " + dc + " dest=" + dest); 97 if (!authorizeRead(dc, ((SpyDestination) dest).getName())) 98 throw new JMSSecurityException ("Connection not authorized to browse to destination: " + dest); 99 return super.browse(dc, dest, selector); 100 } 101 102 public SpyMessage receive(ConnectionToken dc, int subscriberId, long wait) throws JMSException 107 { 108 if (log.isTraceEnabled()) 109 log.trace("Checking receive authorize on " + dc + " subId=" + subscriberId); 110 Subscription sub = super.getSubscription(dc, subscriberId); 112 String destName = sub.destination.getName(); 113 if (!authorizeRead(dc, destName)) 114 throw new JMSSecurityException ("Connection not authorized to receive from destination: " + destName); 115 return super.receive(dc, subscriberId, wait); 116 } 117 118 public void subscribe(org.jboss.mq.ConnectionToken dc, org.jboss.mq.Subscription sub) throws JMSException 128 { 129 if (log.isTraceEnabled()) 130 log.trace("Checking subscribe authorize on " + dc + " sub=" + sub); 131 if (sub == null) 133 throw new JMSException ("The subscription is not allowed to be null"); 134 else if (sub.destination == null) 135 throw new InvalidDestinationException ("Destination is not allowed to be null"); 136 SpyDestination dest = sub.destination; 141 String destName = dest.getName(); 142 if (dest instanceof SpyTopic) 143 { 144 DurableSubscriptionID id = ((SpyTopic) dest).getDurableSubscriptionID(); 146 if (id != null) 147 { 148 if (!authorizeCreate(dc, destName)) 150 throw new JMSSecurityException ("Connection not authorized to do durable subscription on topic: " 151 + destName); 152 } 153 } 154 if (!authorizeRead(dc, destName)) 156 throw new JMSSecurityException ("Connection not authorized to subscribe to destination: " + destName); 157 super.subscribe(dc, sub); 158 } 159 160 public void addMessage(ConnectionToken dc, SpyMessage message) throws JMSException 164 { 165 String dest = ((SpyDestination) message.getJMSDestination()).getName(); 166 if (!authorizeWrite(dc, dest)) 167 throw new JMSSecurityException ("Connection not authorized to addMessages to destination: " + dest); 168 super.addMessage(dc, message); 169 } 170 171 public void transact(ConnectionToken dc, TransactionRequest t) throws JMSException 173 { 174 if (t.messages != null) 175 { 176 if (t.messages.length == 1) 178 { 179 String dest = ((SpyDestination) t.messages[0].getJMSDestination()).getName(); 180 if (authorizeWrite(dc, dest) == false) 181 throw new JMSSecurityException ("Connection not authorized to addMessages to destination: " + dest); 182 } 183 else if (t.messages.length > 0) 184 { 185 HashSet destinations = new HashSet (); 186 for (int i = 0; i < t.messages.length; ++i) 187 destinations.add(((SpyDestination) t.messages[i].getJMSDestination()).getName()); 188 189 for (Iterator i = destinations.iterator(); i.hasNext();) 190 { 191 String destinationName = (String ) i.next(); 192 if (authorizeWrite(dc, destinationName) == false) 193 throw new JMSSecurityException ("Connection not authorized to addMessages to destination: " + destinationName); 194 } 195 } 196 } 197 super.transact(dc, t); 198 } 199 200 public void destroySubscription(ConnectionToken dc, DurableSubscriptionID id) throws JMSException 204 { 205 SpyTopic t = super.getDurableTopic(id); 208 if (t == null) 209 throw new InvalidDestinationException ("No durable topic found for subscription " + id.getSubscriptionName()); 210 if (!authorizeCreate(dc, t.getName())) 211 throw new JMSSecurityException ("Connection not authorized to unsubscribe from subscription: " + t.getName()); 212 super.destroySubscription(dc, id); 213 } 214 215 public TemporaryTopic getTemporaryTopic(ConnectionToken dc) throws JMSException 216 { 217 TemporaryTopic result = super.getTemporaryTopic(dc); 218 addTemporaryDestination(dc, result); 219 return result; 220 } 221 222 public TemporaryQueue getTemporaryQueue(ConnectionToken dc) throws JMSException 223 { 224 TemporaryQueue result = super.getTemporaryQueue(dc); 225 addTemporaryDestination(dc, result); 226 return result; 227 } 228 229 public void deleteTemporaryDestination(ConnectionToken dc, SpyDestination destination) throws JMSException 230 { 231 removeTemporaryDestination(dc, destination); 232 super.deleteTemporaryDestination(dc, destination); 233 } 234 235 public boolean authorizeRead(ConnectionToken dc, String destination) throws JMSException 239 { 240 SecurityMetadata m = manager.getSecurityMetadata(destination); 242 if (m == null) 243 { 244 log.warn("No security configuration avaliable for " + destination); 245 return false; } 247 Set readPrincipals = m.getReadPrincipals(); 248 if (manager.authorize(dc, readPrincipals)) 249 return true; 250 else 251 return false; 252 } 253 254 public boolean authorizeWrite(ConnectionToken dc, String destination) throws JMSException 255 { 256 SecurityMetadata m = manager.getSecurityMetadata(destination); 258 if (m == null) 259 { 260 log.warn("No security configuration avaliable for " + destination); 261 return false; } 263 Set writePrincipals = m.getWritePrincipals(); 264 if (manager.authorize(dc, writePrincipals)) 265 return true; 266 else 267 return false; 268 } 269 270 public boolean authorizeCreate(ConnectionToken dc, String destination) throws JMSException 271 { 272 SecurityMetadata m = manager.getSecurityMetadata(destination); 274 if (m == null) 275 { 276 log.warn("No security configuration avaliable for " + destination); 277 return false; } 279 Set createPrincipals = m.getCreatePrincipals(); 280 if (manager.authorize(dc, createPrincipals)) 281 return true; 282 else 283 return false; 284 } 285 286 289 public void addTemporaryDestination(ConnectionToken dc, Destination destination) 290 { 291 synchronized (tempDests) 292 { 293 HashSet set = (HashSet ) tempDests.get(dc); 294 if (set == null) 295 { 296 set = new HashSet (); 297 tempDests.put(dc, set); 298 } 299 set.add(destination); 300 } 301 } 302 303 306 public void removeTemporaryDestination(ConnectionToken dc, SpyDestination destination) 307 { 308 synchronized (tempDests) 309 { 310 HashSet set = (HashSet ) tempDests.get(dc); 311 if (set == null) 312 return; 313 set.remove(destination); 314 } 315 try 316 { 317 manager.removeDestination(destination.getName()); 318 } 319 catch (Exception e) 320 { 321 log.warn("Unable to remove temporary destination " + destination, e); 322 } 323 } 324 325 328 public void removeTemporaryDestinations(ConnectionToken dc) 329 { 330 synchronized (tempDests) 331 { 332 HashSet set = (HashSet ) tempDests.remove(dc); 333 if (set == null) 334 return; 335 for (Iterator iterator = set.iterator(); iterator.hasNext();) 336 { 337 SpyDestination destination = (SpyDestination) iterator.next(); 338 try 339 { 340 manager.removeDestination(destination.getName()); 341 } 342 catch (Exception e) 343 { 344 log.warn("Unable to remove temporary destination " + destination, e); 345 } 346 } 347 } 348 } 349 } | Popular Tags |