Author: niclas Date: Sat Aug 7 23:30:27 2004 New Revision: 36077 Modified: avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/CommandInterpreterImpl.java avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/ConsoleImpl.java Log: Touching up some minor details, and adding allow/deny semantics into ConsoleImpl.
Modified: avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/CommandInterpreterImpl.java ============================================================================== --- avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/CommandInterpreterImpl.java (original) +++ avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/CommandInterpreterImpl.java Sat Aug 7 23:30:27 2004 @@ -88,6 +88,9 @@ execute( "login" ); m_Login = true; //LoginCmd throws an exception if login fails. execute( waitForCommandLine() ); + } catch( LoginException e ) + { + errorMessage( "wrong password" ); } catch( InterruptedException e ) { running = false; @@ -131,9 +134,13 @@ private String waitForCommandLine() throws IOException { - getOutput().write( "[" + m_Socket.getLocalAddress() + " " + m_CurrentNode.getPath() + "]$ " ); - getOutput().flush(); - String cmdline = m_Input.readLine().trim(); + String cmdline = ""; + while( cmdline.length() == 0 ) + { + getOutput().write( "[" + m_Socket.getLocalAddress() + " " + m_CurrentNode.getQualifiedName() + "]$ " ); + getOutput().flush(); + cmdline = m_Input.readLine().trim(); + } return cmdline; } @@ -149,6 +156,7 @@ { try { + getOutput().newLine(); getOutput().write( message ); getOutput().newLine(); getOutput().flush(); Modified: avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/ConsoleImpl.java ============================================================================== --- avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/ConsoleImpl.java (original) +++ avalon/trunk/planet/facilities/console/impl/src/main/org/apache/avalon/facilities/console/impl/ConsoleImpl.java Sat Aug 7 23:30:27 2004 @@ -20,10 +20,12 @@ import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; +import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Collection; import java.util.Hashtable; +import java.util.HashSet; import java.util.Iterator; import org.apache.avalon.composition.model.ContainmentModel; @@ -35,6 +37,10 @@ import org.apache.avalon.framework.activity.Initializable; import org.apache.avalon.framework.activity.Startable; +import org.apache.avalon.framework.configuration.Configurable; +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; + import org.apache.avalon.framework.context.Context; import org.apache.avalon.framework.context.ContextException; import org.apache.avalon.framework.context.Contextualizable; @@ -46,7 +52,42 @@ import org.apache.avalon.framework.parameters.Parameters; -/** +/** This is the main component for the Console facility. + * + * <p>Parameters, default values are shown below.</p> + * <pre> + * <parameters> + * <parameter name="port" value="3333" /> + * <parameter name="allow-deny" value="false" /> + * <parameter name="welcome-text" value="type 'help' form more info." /> + * </parameters> + * </pre> + * <p> If <i>allow-deny</i> is set to true, then the connection is allowed + * if the host does not exist in the deny list, otherwise it is not allowed + * unless the host exist in the allow list. + * </p> + * + * <p>Configuration</p> + * <pre> + * <configuration> + * <connections> + * <allow>127.0.0.1> + * <allow>10.0.0.23> + * <allow>10.0.0.24> + * </connections> + * </configuration> + * </pre> + * <p>or in some circumstances, one sets the <i>allow-deny</i> parameter to + * true, and have a deny list in the configuration.</p> + * <pre> + * <configuration> + * <connections> + * <deny>211.24.132.24> + * <deny>211.24.132.29> + * </connections> + * </configuration> + * </pre> + * * @avalon.component name="console-impl" lifestyle="singleton" * @avalon.service type="org.apache.avalon.facilities.console.Console" */ @@ -62,6 +103,16 @@ private String m_Welcome; private ContainmentModel m_RootModel; + private boolean m_Allow; + private HashSet m_Allows; + private HashSet m_Denies; + + public ConsoleImpl() + { + m_Allows = new HashSet(); + m_Denies = new HashSet(); + } + /** * Contextulaization of the listener by the container during * which we are supplied with the root composition model for @@ -80,12 +131,92 @@ { m_RootModel = (ContainmentModel) ctx.get( "urn:composition:containment.model" ); } - + + /** + * <p>Parameters, default values are shown below.</p> + * <pre> + * <parameters> + * <parameter name="port" value="3333" /> + * <parameter name="allow-deny" value="false" /> + * <parameter name="welcome-text" value="type 'help' form more info." /> + * </parameters> + * </pre> + */ public void parameterize( Parameters params ) throws ParameterException { m_Port = params.getParameterAsInteger( "port", 3333 ); m_Welcome = params.getParameter( "welcome-text", "type 'help' form more info." ); + m_Allow = params.getParameterAsBoolean( "deny-allow", false ); + } + + /** + * <p>Configuration</p> + * <pre> + * <configuration> + * <connections> + * <allow>127.0.0.1> + * <allow>10.0.0.23> + * <allow>10.0.0.24> + * </connections> + * </configuration> + * </pre> + * <p>or in some circumstances, one sets the <i>allow-deny</i> parameter to + * true, and have a deny list in the configuration.</p> + * <pre> + * <configuration> + * <connections> + * <deny>211.24.132.24> + * <deny>211.24.132.29> + * </connections> + * </configuration> + * </pre> + */ + public void configure( Configuration conf ) + throws ConfigurationException + { + Configuration child = conf.getChild( "connections" ); + configureConnections( child ); + } + + private void configureConnections( Configuration conf ) + throws ConfigurationException + { + Configuration[] allows = conf.getChildren( "allow" ); + for( int i=0 ; i < allows.length ; i++ ) + configureAllow( allows[i] ); + + Configuration[] denies = conf.getChildren( "deny" ); + for( int i=0 ; i < denies.length ; i++ ) + configureDeny( denies[i] ); + } + + private void configureAllow( Configuration allow ) + throws ConfigurationException + { + String address = allow.getValue(); + try + { + InetAddress allowAddr = InetAddress.getByName( address ); + m_Allows.add( allowAddr ); + } catch( UnknownHostException e ) + { + throw new ConfigurationException( "Unknown host: " + address, e ); + } + } + + private void configureDeny( Configuration deny ) + throws ConfigurationException + { + String address = deny.getValue(); + try + { + InetAddress denyAddr = InetAddress.getByName( address ); + m_Denies.add( denyAddr ); + } catch( UnknownHostException e ) + { + throw new ConfigurationException( "Unknown host: " + address, e ); + } } public void initialize() @@ -181,6 +312,7 @@ try { Socket socket = m_ServerSocket.accept(); + validateHost( socket.getInetAddress() ); CommandInterpreterImpl intp = new CommandInterpreterImpl( socket, m_Welcome, m_Commands, m_RootModel ); synchronized( m_Interpreters ) { @@ -190,6 +322,9 @@ } catch( IOException e ) { running = false; + } catch( DeniedHostException e ) + { + getLogger().warn( e.getMessage() ); } catch( Exception e ) { getLogger().warn( "", e ); @@ -207,6 +342,21 @@ { CommandInterpreterImpl intp = (CommandInterpreterImpl) list.next(); intp.interrupt(); + } + } + + private void validateHost( InetAddress host ) + throws DeniedHostException + { + if( m_Allow ) + { + if( m_Denies.contains( host ) ) + throw new DeniedHostException( host.toString() ); + } + else + { + if( ! m_Allows.contains( host ) ) + throw new DeniedHostException( host.toString() ); } } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]