1 6 7 package org.drftpd.slave.async; 8 9 import java.io.BufferedReader ; 10 import java.io.EOFException ; 11 import java.io.IOException ; 12 import java.io.InputStreamReader ; 13 import java.io.PrintWriter ; 14 import java.io.StringReader ; 15 16 import java.net.InetAddress ; 17 import java.net.ServerSocket ; 18 import java.net.Socket ; 19 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 23 import java.security.MessageDigest ; 24 25 import net.sf.drftpd.FatalException; 26 import net.sf.drftpd.master.ConnectionManager; 27 import net.sf.drftpd.master.RemoteSlave; 28 29 import org.apache.log4j.Logger; 30 import org.apache.oro.text.GlobCompiler; 31 import org.apache.oro.text.regex.MalformedPatternException; 32 import org.apache.oro.text.regex.Pattern; 33 import org.apache.oro.text.regex.Perl5Matcher; 34 35 import socks.server.Ident; 36 37 41 public class AsyncSlaveListener extends Thread { 42 43 private static final Logger logger = Logger.getLogger(AsyncSlaveListener.class.getName()); 44 45 private int _port; 46 private ServerSocket sock; 47 static ConnectionManager _conman; 48 static String _pass = ""; 49 50 static void invalidSlave(String msg, Socket sock) throws IOException { 51 BufferedReader _sinp = null; 52 PrintWriter _sout = null; 53 try { 54 _sout = new PrintWriter (sock.getOutputStream(), true); 55 _sinp = new BufferedReader ( 56 new InputStreamReader (sock.getInputStream()) 57 ); 58 _sout.println(msg); 59 logger.info("NEW< " + msg); 60 String txt = AsyncSlaveListener.readLine(_sinp, 30); 61 String sname = ""; 62 String spass = ""; 63 String shash = ""; 64 try { 65 String [] items = txt.split(" "); 66 sname = items[1].trim(); 67 spass = items[2].trim(); 68 shash = items[3].trim(); 69 } catch (Exception e) { 70 throw new IOException ("Slave Inititalization Faailed"); 71 } 72 String pass = sname + spass + _pass; 74 MessageDigest md5 = MessageDigest.getInstance("MD5"); 75 md5.reset(); 76 md5.update(pass.getBytes()); 77 String hash = AsyncSlaveListener.hash2hex(md5.digest()).toLowerCase(); 78 if (!hash.equals(shash)) { 79 throw new IOException ("Slave Inititalization Faailed"); 80 } 81 82 84 } catch (Exception e) { 85 86 } 87 throw new IOException ("Slave Inititalization Faailed"); 88 } 89 90 static String hash2hex(byte[] bytes) { 91 String res = ""; 92 for (int i = 0; i < 16; i++) { 93 String hex = Integer.toHexString((int) bytes[i]); 94 if (hex.length() < 2) 95 hex = "0" + hex; 96 res += hex.substring(hex.length() - 2); 97 } 98 return res; 99 } 100 101 102 static String readLine(BufferedReader _sinp, int secs) { 103 int cnt = secs * 10; 104 try { 105 while (true) { 106 while (!_sinp.ready()) { 107 if (cnt < 1) 108 return null; 109 sleep(100); 110 cnt--; 111 if (cnt == 0) 112 return null; 113 } 114 String txt = _sinp.readLine(); 115 logger.info("NEW> " + txt); 116 return txt; 117 } 118 } catch (Exception e) { 119 return null; 120 } 121 } 122 123 124 public AsyncSlaveListener(ConnectionManager conman, int port, String pass) { 125 _conman = conman; 126 _port = port; 127 _pass = pass; 128 start(); 129 } 130 131 public void run() { 132 try { 133 sock = new ServerSocket (_port); 134 } catch (Exception e) { 135 throw new FatalException(e); 136 } 137 Socket slave; 138 while (true) { 139 try { 140 slave = sock.accept(); 141 } catch (Exception e) { 142 throw new FatalException(e); 143 } 144 InetAddress addr = slave.getInetAddress(); 145 logger.info("AsyncSlaveListener: accepting " + addr); 146 Ident identObj = new Ident(slave); 147 String ident; 148 if (identObj.successful) { 149 ident = identObj.userName; 150 } else { 151 ident = ""; 152 } 153 Perl5Matcher m = new Perl5Matcher(); 154 155 String ipmask = ident + "@" + addr.getHostAddress(); 156 String hostmask = ident + "@" + addr.getHostName(); 157 logger.info("AsyncSlaveListener: ipmask " + ipmask); 158 logger.info("AsyncSlaveListener: hostmask " + hostmask); 159 Collection slaves = _conman.getSlaveManager().getSlaves(); 160 boolean match = false; 161 RemoteSlave thisone = null; 162 for (Iterator i=slaves.iterator(); i.hasNext();) { 163 RemoteSlave rslave = (RemoteSlave)i.next(); 164 if (rslave.isAvailable()) { 165 logger.info("AsyncSlaveListener: online> " + rslave.getName()); 166 continue; } 168 String saddr = (String )rslave.getConfig().get("addr"); 169 if (saddr == null) { 170 logger.info("AsyncSlaveListener: noaddr> " + rslave.getName()); 171 continue; } 173 if (!saddr.equals("Dynamic")) { 174 logger.info("AsyncSlaveListener: static> " + rslave.getName()); 175 continue; } 177 logger.info("AsyncSlaveListener: testing " + rslave.getName()); 179 for (Iterator i2 = rslave.getMasks().iterator(); i2.hasNext(); ) { 180 String mask = (String ) i2.next(); 181 logger.info("SockSlaveListener: mask = " + mask); 182 Pattern p; 183 try { 184 p = new GlobCompiler().compile(mask); 185 } catch (MalformedPatternException ex) { 186 throw new RuntimeException ( 187 "Invalid glob pattern: " + mask, 188 ex 189 ); 190 } 191 192 if (m.matches(ipmask, p) || m.matches(hostmask, p)) { 194 match = true; 195 thisone = rslave; 196 break; 197 } 198 } if (match) break; 200 } if (match) { 202 try { 204 AsyncSlave tmp = new AsyncSlave(_conman, thisone.getConfig(), slave); 205 } catch (Exception e) { 206 } 207 } else { 208 try { 210 AsyncSlaveListener.invalidSlave("INITFAIL Unregistered", slave); 211 } catch (Exception e) { 212 } 213 } 214 } 215 } 216 } 217 | Popular Tags |