1 package org.jivesoftware.messenger.server; 2 3 import org.jivesoftware.database.DbConnectionManager; 4 import org.jivesoftware.messenger.Session; 5 import org.jivesoftware.messenger.SessionManager; 6 import org.jivesoftware.messenger.net.SocketAcceptThread; 7 import org.jivesoftware.messenger.server.RemoteServerConfiguration.Permission; 8 import org.jivesoftware.util.JiveGlobals; 9 import org.jivesoftware.util.Log; 10 11 import java.sql.PreparedStatement ; 12 import java.sql.ResultSet ; 13 import java.sql.SQLException ; 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 17 24 public class RemoteServerManager { 25 26 private static final String ADD_CONFIGURATION = 27 "INSERT INTO jiveRemoteServerConf (domain,remotePort,permission) VALUES (?,?,?)"; 28 private static final String DELETE_CONFIGURATION = 29 "DELETE FROM jiveRemoteServerConf WHERE domain=?"; 30 private static final String LOAD_CONFIGURATION = 31 "SELECT remotePort,permission FROM jiveRemoteServerConf where domain=?"; 32 private static final String LOAD_CONFIGURATIONS = 33 "SELECT domain,remotePort FROM jiveRemoteServerConf where permission=?"; 34 35 40 public static void allowAccess(RemoteServerConfiguration configuration) { 41 deleteConfiguration(configuration.getDomain()); 43 configuration.setPermission(Permission.allowed); 45 addConfiguration(configuration); 46 } 47 48 54 public static void blockAccess(String domain) { 55 deleteConfiguration(domain); 57 RemoteServerConfiguration config = new RemoteServerConfiguration(domain); 59 config.setPermission(Permission.blocked); 60 addConfiguration(config); 61 Session session = SessionManager.getInstance().getIncomingServerSession(domain); 63 if (session != null) { 64 session.getConnection().close(); 65 } 66 session = SessionManager.getInstance().getOutgoingServerSession(domain); 67 if (session != null) { 68 session.getConnection().close(); 69 } 70 } 71 72 80 public static boolean canAccess(String domain) { 81 Permission permission = null; 83 84 RemoteServerConfiguration config = getConfiguration(domain); 85 if (config != null) { 86 permission = config.getPermission(); 87 } 88 89 if (PermissionPolicy.blacklist == getPermissionPolicy()) { 90 if (Permission.blocked == permission) { 92 return false; 93 } 94 else { 95 return true; 96 } 97 } 98 else { 99 if (Permission.allowed == permission) { 101 return true; 102 } 103 else { 104 return false; 105 } 106 } 107 } 108 109 117 public static Collection <RemoteServerConfiguration> getAllowedServers() { 118 return getConfigurations(Permission.allowed); 119 } 120 121 127 public static Collection <RemoteServerConfiguration> getBlockedServers() { 128 return getConfigurations(Permission.blocked); 129 } 130 131 137 public static void deleteConfiguration(String domain) { 138 java.sql.Connection con = null; 140 PreparedStatement pstmt = null; 141 try { 142 con = DbConnectionManager.getConnection(); 143 pstmt = con.prepareStatement(DELETE_CONFIGURATION); 144 pstmt.setString(1, domain); 145 pstmt.executeUpdate(); 146 } 147 catch (SQLException sqle) { 148 Log.error(sqle); 149 } 150 finally { 151 try { if (pstmt != null) pstmt.close(); } 152 catch (Exception e) { Log.error(e); } 153 try { if (con != null) con.close(); } 154 catch (Exception e) { Log.error(e); } 155 } 156 } 157 158 161 private static void addConfiguration(RemoteServerConfiguration configuration) { 162 java.sql.Connection con = null; 164 PreparedStatement pstmt = null; 165 try { 166 con = DbConnectionManager.getConnection(); 167 pstmt = con.prepareStatement(ADD_CONFIGURATION); 168 pstmt.setString(1, configuration.getDomain()); 169 pstmt.setInt(2, configuration.getRemotePort()); 170 pstmt.setString(3, configuration.getPermission().toString()); 171 pstmt.executeUpdate(); 172 } 173 catch (SQLException sqle) { 174 Log.error(sqle); 175 } 176 finally { 177 try { if (pstmt != null) pstmt.close(); } 178 catch (Exception e) { Log.error(e); } 179 try { if (con != null) con.close(); } 180 catch (Exception e) { Log.error(e); } 181 } 182 } 183 184 190 public static RemoteServerConfiguration getConfiguration(String domain) { 191 RemoteServerConfiguration configuration = null; 192 java.sql.Connection con = null; 193 PreparedStatement pstmt = null; 194 try { 195 con = DbConnectionManager.getConnection(); 196 pstmt = con.prepareStatement(LOAD_CONFIGURATION); 197 pstmt.setString(1, domain); 198 ResultSet rs = pstmt.executeQuery(); 199 200 while (rs.next()) { 201 configuration = new RemoteServerConfiguration(domain); 202 configuration.setRemotePort(rs.getInt(1)); 203 configuration.setPermission(Permission.valueOf(rs.getString(2))); 204 } 205 rs.close(); 206 } 207 catch (SQLException sqle) { 208 Log.error(sqle); 209 } 210 finally { 211 try { if (pstmt != null) pstmt.close(); } 212 catch (Exception e) { Log.error(e); } 213 try { if (con != null) con.close(); } 214 catch (Exception e) { Log.error(e); } 215 } 216 return configuration; 217 } 218 219 private static Collection <RemoteServerConfiguration> getConfigurations( 220 Permission permission) { 221 Collection <RemoteServerConfiguration> answer = 222 new ArrayList <RemoteServerConfiguration>(); 223 java.sql.Connection con = null; 224 PreparedStatement pstmt = null; 225 try { 226 con = DbConnectionManager.getConnection(); 227 pstmt = con.prepareStatement(LOAD_CONFIGURATIONS); 228 pstmt.setString(1, permission.toString()); 229 ResultSet rs = pstmt.executeQuery(); 230 RemoteServerConfiguration configuration; 231 while (rs.next()) { 232 configuration = new RemoteServerConfiguration(rs.getString(1)); 233 configuration.setRemotePort(rs.getInt(2)); 234 configuration.setPermission(permission); 235 answer.add(configuration); 236 } 237 rs.close(); 238 } 239 catch (SQLException sqle) { 240 Log.error(sqle); 241 } 242 finally { 243 try { if (pstmt != null) pstmt.close(); } 244 catch (Exception e) { Log.error(e); } 245 try { if (con != null) con.close(); } 246 catch (Exception e) { Log.error(e); } 247 } 248 return answer; 249 } 250 251 258 public static int getPortForServer(String domain) { 259 int port = JiveGlobals.getIntProperty("xmpp.server.socket.remotePort", 260 SocketAcceptThread.DEFAULT_SERVER_PORT); 261 RemoteServerConfiguration config = getConfiguration(domain); 262 if (config != null) { 263 port = config.getRemotePort(); 264 if (port == 0) { 265 port = 266 JiveGlobals.getIntProperty("xmpp.server.socket.remotePort", 267 SocketAcceptThread.DEFAULT_SERVER_PORT); 268 } 269 } 270 return port; 271 } 272 273 283 public static PermissionPolicy getPermissionPolicy() { 284 try { 285 return PermissionPolicy.valueOf(JiveGlobals.getProperty("xmpp.server.permission", 286 PermissionPolicy.blacklist.toString())); 287 } 288 catch (Exception e) { 289 Log.error(e); 290 return PermissionPolicy.blacklist; 291 } 292 } 293 294 303 public static void setPermissionPolicy(PermissionPolicy policy) { 304 JiveGlobals.setProperty("xmpp.server.permission", policy.toString()); 305 for (String hostname : SessionManager.getInstance().getIncomingServers()) { 307 if (!canAccess(hostname)) { 308 Session session = SessionManager.getInstance().getIncomingServerSession(hostname); 309 session.getConnection().close(); 310 } 311 } 312 for (String hostname : SessionManager.getInstance().getOutgoingServers()) { 313 if (!canAccess(hostname)) { 314 Session session = SessionManager.getInstance().getOutgoingServerSession(hostname); 315 session.getConnection().close(); 316 } 317 } 318 } 319 320 329 public static void setPermissionPolicy(String policy) { 330 setPermissionPolicy(PermissionPolicy.valueOf(policy)); 331 } 332 333 public enum PermissionPolicy { 334 338 blacklist, 339 340 344 whitelist; 345 } 346 } 347 | Popular Tags |