1 11 12 package org.jivesoftware.messenger.server; 13 14 import org.jivesoftware.messenger.*; 15 import org.jivesoftware.messenger.auth.UnauthorizedException; 16 import org.jivesoftware.util.LocaleUtils; 17 import org.jivesoftware.util.Log; 18 import org.xmpp.packet.JID; 19 import org.xmpp.packet.Packet; 20 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.Collections ; 24 import java.util.regex.Pattern ; 25 26 47 public class OutgoingServerSession extends Session { 48 49 52 private static Pattern pattern = Pattern.compile("[a-zA-Z]"); 53 54 private Collection <String > authenticatedDomains = new ArrayList <String >(); 55 private Collection <String > hostnames = new ArrayList <String >(); 56 private OutgoingServerSocketReader socketReader; 57 58 75 public static boolean authenticateDomain(String domain, String hostname) { 76 if (hostname == null || hostname.length() == 0 || hostname.trim().indexOf(' ') > -1) { 77 return false; 79 } 80 try { 81 if (!RemoteServerManager.canAccess(hostname)) { 83 return false; 84 } 85 86 SessionManager sessionManager = SessionManager.getInstance(); 90 OutgoingServerSession session = sessionManager.getOutgoingServerSession(hostname); 91 if (session == null) { 92 IncomingServerSession incomingSession = sessionManager.getIncomingServerSession( 94 hostname); 95 if (incomingSession != null) { 96 for (String otherHostname : incomingSession.getValidatedDomains()) { 97 session = sessionManager.getOutgoingServerSession(otherHostname); 98 if (session != null) { 99 session.addHostname(hostname); 102 break; 103 } 104 } 105 } 106 } 107 if (session == null) { 108 int port = RemoteServerManager.getPortForServer(hostname); 109 synchronized (hostname.intern()) { 111 session = sessionManager.getOutgoingServerSession(hostname); 112 if (session == null) { 113 session = 114 new ServerDialback().createOutgoingSession(domain, hostname, port); 115 if (session != null) { 116 session.addHostname(hostname); 118 session.addAuthenticatedDomain(domain); 120 sessionManager.outgoingServerSessionCreated(session); 122 return true; 123 } 124 else { 125 if (!pattern.matcher(hostname).find()) { 127 return false; 128 } 129 for (String otherHost : sessionManager.getOutgoingServers()) { 131 if (hostname.contains(otherHost)) { 132 session = sessionManager.getOutgoingServerSession(otherHost); 133 session.addHostname(hostname); 135 return true; 136 } 137 } 138 int index = hostname.indexOf('.'); 146 while (index > -1 && index < hostname.length()) { 147 String newHostname = hostname.substring(index + 1); 148 String serverName = XMPPServer.getInstance().getServerInfo() 149 .getName(); 150 if ("com".equals(newHostname) || "net".equals(newHostname) || 151 "org".equals(newHostname) || 152 "gov".equals(newHostname) || 153 "edu".equals(newHostname) || 154 serverName.equals(newHostname)) { 155 return false; 156 } 157 session = 158 new ServerDialback().createOutgoingSession(domain, newHostname, port); 159 if (session != null) { 160 session.addHostname(hostname); 162 session.addAuthenticatedDomain(domain); 164 sessionManager.outgoingServerSessionCreated(session); 166 session.addHostname(newHostname); 168 return true; 169 } 170 else { 171 index = hostname.indexOf('.', index + 1); 172 } 173 } 174 return false; 175 } 176 } 177 } 178 } 179 if (session.getAuthenticatedDomains().contains(domain)) { 180 return true; 182 } 183 ServerDialback method = new ServerDialback(session.getConnection(), domain); 185 if (method.authenticateDomain(session.socketReader, domain, hostname, 186 session.getStreamID().getID())) { 187 session.addAuthenticatedDomain(domain); 189 return true; 190 } 191 } 192 catch (Exception e) { 193 Log.error("Error authenticating domain with remote server: " + hostname, e); 194 } 195 return false; 196 } 197 198 OutgoingServerSession(String serverName, Connection connection, 199 OutgoingServerSocketReader socketReader, StreamID streamID) { 200 super(serverName, connection, streamID); 201 this.socketReader = socketReader; 202 socketReader.setSession(this); 203 } 204 205 public void process(Packet packet) throws UnauthorizedException, PacketException { 206 if (conn != null && !conn.isClosed()) { 207 try { 208 conn.deliver(packet); 209 } 210 catch (Exception e) { 211 Log.error(LocaleUtils.getLocalizedString("admin.error"), e); 212 } 213 } 214 } 215 216 223 public Collection <String > getAuthenticatedDomains() { 224 return Collections.unmodifiableCollection(authenticatedDomains); 225 } 226 227 234 public void addAuthenticatedDomain(String domain) { 235 authenticatedDomains.add(domain); 236 } 237 238 246 public void removeAuthenticatedDomain(String domain) { 247 authenticatedDomains.remove(domain); 248 } 249 250 256 public Collection <String > getHostnames() { 257 return Collections.unmodifiableCollection(hostnames); 258 } 259 260 267 private void addHostname(String hostname) { 268 if (hostnames.add(hostname)) { 269 sessionManager.registerOutgoingServerSession(hostname, this); 272 XMPPServer.getInstance().getRoutingTable().addRoute(new JID(hostname), this); 274 } 275 } 276 277 } 278 | Popular Tags |