1 18 package net.sf.drftpd.permission; 19 20 import java.io.IOException ; 21 import java.net.InetAddress ; 22 import java.net.ServerSocket ; 23 import java.net.Socket ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import net.sf.drftpd.master.SlaveManagerImpl; 29 30 import org.apache.log4j.Logger; 31 import org.apache.oro.text.GlobCompiler; 32 import org.apache.oro.text.regex.MalformedPatternException; 33 import org.apache.oro.text.regex.Pattern; 34 import org.apache.oro.text.regex.Perl5Matcher; 35 import socks.server.Ident; 36 37 41 public class GlobServerSocket extends ServerSocket { 42 43 public GlobServerSocket(Collection rslaves) throws IOException { 44 super(); 45 this.rslaves = rslaves; 46 } 47 48 public GlobServerSocket(int port, Collection rslaves) throws IOException { 49 super(port); 50 this.rslaves = rslaves; 51 } 52 53 public GlobServerSocket(int arg0, int arg1, List rslaves) 54 throws IOException { 55 super(arg0, arg1); 56 this.rslaves = rslaves; 57 } 58 59 public GlobServerSocket(int arg0, int arg1, InetAddress arg2) 60 throws IOException { 61 super(arg0, arg1, arg2); 62 } 63 64 private static final Logger logger = 65 Logger.getLogger(GlobServerSocket.class); 66 67 private Collection rslaves; 68 69 public Socket accept() throws IOException { 70 Perl5Matcher m = new Perl5Matcher(); 71 while (true) { 73 Socket sock = super.accept(); 74 Ident identObj = new Ident(sock); 75 String ident; 76 if (identObj.successful) { 77 ident = identObj.userName; 78 } else { 79 ident = ""; 80 } 81 logger.warn("Ident: " + ident + "."); 82 83 String ipmask = 84 ident + "@" + sock.getInetAddress().getHostAddress(); 85 String hostmask = ident + "@" + sock.getInetAddress().getHostName(); 86 logger.warn("Ipmask: " + ipmask + "."); 87 logger.warn("Hostmask: " + hostmask + "."); 88 for (Iterator i = 89 SlaveManagerImpl.rslavesToMasks(this.rslaves).iterator(); 90 i.hasNext(); 91 ) { 92 String mask = (String ) i.next(); 93 logger.warn("Mask: " + mask + "."); 94 Pattern p; 95 try { 96 p = new GlobCompiler().compile(mask); 97 } catch (MalformedPatternException ex) { 98 throw new RuntimeException ( 99 "Invalid glob pattern: " + mask, 100 ex); 101 } 102 103 if (m.matches(ipmask, p)) { 105 logger.warn("Match ipmask !!"); 106 return sock; 107 } 108 if (m.matches(hostmask, p)) { 110 logger.warn("Match hostmask !!"); 111 return sock; 112 } 113 } logger.warn( 115 "Rejecting RMI connection: " + hostmask + "/" + ipmask + "."); 116 sock.close(); 117 } 118 } 119 } 120 | Popular Tags |