1 package freecs.external; 2 3 import java.net.InetAddress ; 4 import java.nio.channels.SelectionKey ; 5 import java.nio.channels.SocketChannel ; 6 7 import freecs.Server; 8 import freecs.content.ContentContainer; 9 import freecs.interfaces.IRequest; 10 import freecs.interfaces.IResponseHeaders; 11 import freecs.util.HttpAuth; 12 13 public abstract class AbstractRequestHandler implements IRequestHandler { 14 15 private String handlerName; 16 17 20 public AbstractRequestHandler(String handlerName) { 21 this.handlerName = handlerName; 22 } 23 24 public void checkAccessIp(IRequest req, ContentContainer c) throws AccessForbiddenException { 25 SelectionKey key = req.getKey (); 26 InetAddress ia = null; 27 try { 28 SocketChannel sc = (SocketChannel ) key.channel (); 29 ia = sc.socket ().getInetAddress (); 30 } catch (Exception e) { 31 Server.debug (this, "" + ia.toString (), e, Server.MSG_STATE, Server.LVL_MAJOR); 32 throw new AccessForbiddenException(true); 33 } 34 if (!Server.srv.isAdminHost(ia)) { 35 Server.log (this, "access to " + getHandlerName() + " denied for " + ia.toString (), Server.MSG_STATE, Server.LVL_MAJOR); 36 throw new AccessForbiddenException(true); 37 } 38 } 39 40 public void checkAccessAuth(IRequest req, ContentContainer c) throws AccessForbiddenException { 41 if (Server.srv.ADMIN_HTTP_USERNAME == null || Server.srv.ADMIN_HTTP_PASSWORD == null) { 42 Server.log (this, "authentication not properly configured in server config! access denied.", Server.MSG_CONFIG, Server.LVL_MAJOR); 43 throw new AccessForbiddenException(true); 45 } 46 HttpAuth auth = HttpAuth.parse(req.getProperty("authorization")); 47 if (auth == null || auth.username == null || auth.password == null || 48 !auth.username.equals(Server.srv.ADMIN_HTTP_USERNAME) || 49 !auth.password.equals(Server.srv.ADMIN_HTTP_PASSWORD)) { 50 c.setResCode(IResponseHeaders.AUTHENTICATE_CODE); 51 c.wrap("Access denied."); 52 throw new AccessForbiddenException(false); 53 } 54 } 55 56 59 public String getHandlerName() { 60 return handlerName; 61 } 62 63 } 64 | Popular Tags |