<?xml version="1.0" encoding="UTF-8"?>
<!--

    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:bp="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
           xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0"
           default-activation="lazy">

    <type-converters>
        <bean class="org.apache.karaf.shell.ssh.ShellFactoryImpl" factory-method="getConverter" />
        <bean class="org.apache.karaf.shell.ssh.UserAuthFactoriesFactory" factory-method="getConverter" />
    </type-converters>

    <ext:property-placeholder placeholder-prefix="$[" placeholder-suffix="]">
        <ext:default-properties>
            <ext:property name="startRemoteShell" value="true" />
            <ext:property name="karaf.admin.role" value="admin" />
        </ext:default-properties>
    </ext:property-placeholder>

    <cm:property-placeholder persistent-id="org.apache.karaf.shell" update-strategy="reload">
        <cm:default-properties>
            <cm:property name="sshPort" value="8101"/>
            <cm:property name="sshHost" value="0.0.0.0"/>
            <cm:property name="sshIdleTimeout" value="1800000"/>
            <cm:property name="sshRealm" value="karaf"/>
            <cm:property name="sshRole" value="$[karaf.admin.role]"/>
            <cm:property name="hostKey" value="$[karaf.base]/etc/host.key"/>
            <cm:property name="authorizedKeys" value="$[karaf.base]/etc/authorized_keys"/>
            <cm:property name="authMethods" value="password,publickey"/>
            <cm:property name="keySize" value="1024"/>
            <cm:property name="algorithm" value="DSA"/>
        </cm:default-properties>
    </cm:property-placeholder>

    <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.0.0">
        <command name="ssh/ssh">
            <action class="org.apache.karaf.shell.ssh.SshAction">
                <property name="sshClientId">
                    <bp:idref component-id="sshClient"/>
                </property>
            </action>
        </command>
        <command name="ssh/sshd">
            <action class="org.apache.karaf.shell.ssh.SshServerAction">
                <property name="sshServerId">
                    <bp:idref component-id="sshServer"/>
                </property>
            </action>
        </command>
    </command-bundle>

    <bean id="sshClient" class="org.apache.sshd.SshClient" factory-method="setUpDefaultClient" scope="prototype">
        <property name="agentFactory" ref="agentFactory" />
    </bean>

    <bean id="userAuthFactoriesFactory" class="org.apache.karaf.shell.ssh.UserAuthFactoriesFactory">
        <property name="authMethods" value="${authMethods}"/>
    </bean>

    <bean id="sshServer" class="org.apache.sshd.SshServer" factory-method="setUpDefaultServer" scope="prototype">
        <property name="port" value="${sshPort}"/>
        <property name="host" value="${sshHost}"/>
        <property name="shellFactory">
            <bean class="org.apache.karaf.shell.ssh.ShellFactoryImpl">
                <property name="commandProcessor" ref="commandProcessor"/>
            </bean>
        </property>
        <property name="commandFactory">
            <bean class="org.apache.sshd.server.command.ScpCommandFactory">
                <argument>
                    <bean class="org.apache.karaf.shell.ssh.ShellCommandFactory">
                        <property name="commandProcessor" ref="commandProcessor"/>
                    </bean>
                </argument>
            </bean>
        </property>
        <property name="keyPairProvider" ref="keyPairProvider"/>
        <property name="passwordAuthenticator" ref="authenticator"/>
        <property name="publickeyAuthenticator" ref="authenticator"/>
        <property name="fileSystemFactory">
            <bean class="org.apache.karaf.shell.ssh.KarafFileSystemFactory"/>
        </property>
        <property name="userAuthFactories">
            <bean factory-ref="userAuthFactoriesFactory" factory-method="getFactories"/>
        </property>
        <property name="agentFactory" ref="agentFactory" />
    </bean>

    <bean id="agentFactory" class="org.apache.karaf.shell.ssh.KarafAgentFactory" />
    <reference-list id="agents" interface="org.apache.sshd.agent.SshAgent" availability="optional" activation="eager">
        <reference-listener ref="agentFactory" bind-method="registerAgent" unbind-method="unregisterAgent" />
    </reference-list>

    <bean id="keyPairProvider" class="org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider">
        <property name="path" value="${hostKey}"/>
        <property name="keySize" value="${keySize}"/>
        <property name="algorithm" value="${algorithm}"/>
    </bean>
    <bean id="authenticator" class="org.apache.karaf.shell.ssh.KarafJaasAuthenticator">
        <property name="realm" value="${sshRealm}"/>
        <property name="role" value="${sshRole}"/>
    </bean>

    <bean id="sshServerFactory" class="org.apache.karaf.shell.ssh.SshServerFactory" init-method="start"
          destroy-method="stop" activation="eager">
        <argument ref="sshServer"/>
        <property name="start" value="$[karaf.startRemoteShell]"/>
        <property name="idleTimeout" value="${sshIdleTimeout}"/>
    </bean>

    <reference id="commandProcessor" interface="org.apache.felix.service.command.CommandProcessor">
    </reference>

</blueprint>
