KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > permission > GlobServerSocket


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.permission;
19
20 import java.io.IOException JavaDoc;
21 import java.net.InetAddress JavaDoc;
22 import java.net.ServerSocket JavaDoc;
23 import java.net.Socket JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
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 /**
38  * @author mog
39  * @version $Id: GlobServerSocket.java,v 1.9 2004/04/29 18:32:24 zombiewoof64 Exp $
40  */

41 public class GlobServerSocket extends ServerSocket JavaDoc {
42
43     public GlobServerSocket(Collection JavaDoc rslaves) throws IOException JavaDoc {
44         super();
45         this.rslaves = rslaves;
46     }
47
48     public GlobServerSocket(int port, Collection JavaDoc rslaves) throws IOException JavaDoc {
49         super(port);
50         this.rslaves = rslaves;
51     }
52
53     public GlobServerSocket(int arg0, int arg1, List JavaDoc rslaves)
54         throws IOException JavaDoc {
55         super(arg0, arg1);
56         this.rslaves = rslaves;
57     }
58
59     public GlobServerSocket(int arg0, int arg1, InetAddress JavaDoc arg2)
60         throws IOException JavaDoc {
61         super(arg0, arg1, arg2);
62     }
63
64     private static final Logger logger =
65         Logger.getLogger(GlobServerSocket.class);
66
67     private Collection JavaDoc rslaves;
68
69     public Socket JavaDoc accept() throws IOException JavaDoc {
70         Perl5Matcher m = new Perl5Matcher();
71         // Try until a valid peer tries to connect.
72
while (true) {
73             Socket JavaDoc sock = super.accept();
74             Ident identObj = new Ident(sock);
75             String JavaDoc ident;
76             if (identObj.successful) {
77                 ident = identObj.userName;
78             } else {
79                 ident = "";
80             }
81                 logger.warn("Ident: " + ident + ".");
82
83             String JavaDoc ipmask =
84                 ident + "@" + sock.getInetAddress().getHostAddress();
85             String JavaDoc hostmask = ident + "@" + sock.getInetAddress().getHostName();
86                 logger.warn("Ipmask: " + ipmask + ".");
87                 logger.warn("Hostmask: " + hostmask + ".");
88             for (Iterator JavaDoc i =
89                 SlaveManagerImpl.rslavesToMasks(this.rslaves).iterator();
90                 i.hasNext();
91                 ) {
92                 String JavaDoc mask = (String JavaDoc) 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 JavaDoc(
99                         "Invalid glob pattern: " + mask,
100                         ex);
101                 }
102
103                 // ip
104
if (m.matches(ipmask, p)) {
105                             logger.warn("Match ipmask !!");
106                     return sock;
107                 }
108                 // host
109
if (m.matches(hostmask, p)) {
110                             logger.warn("Match hostmask !!");
111                     return sock;
112                 }
113             } //for
114
logger.warn(
115                 "Rejecting RMI connection: " + hostmask + "/" + ipmask + ".");
116             sock.close();
117         }
118     }
119 }
120
Popular Tags