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