/// <summary>Obtain the JSch used to create new sessions.</summary>
        /// <remarks>Obtain the JSch used to create new sessions.</remarks>
        /// <param name="hc">host configuration</param>
        /// <param name="fs">
        /// the file system abstraction which will be necessary to
        /// perform certain file system operations.
        /// </param>
        /// <returns>the JSch instance to use.</returns>
        /// <exception cref="NSch.JSchException">the user configuration could not be created.
        ///     </exception>
        protected internal virtual JSch GetJSch(OpenSshConfig.Host hc, FS fs)
        {
            if (defaultJSch == null)
            {
                defaultJSch = CreateDefaultJSch(fs);
                foreach (object name in defaultJSch.GetIdentityNames())
                {
                    byIdentityFile.Put((string)name, defaultJSch);
                }
            }
            FilePath identityFile = hc.GetIdentityFile();

            if (identityFile == null)
            {
                return(defaultJSch);
            }
            string identityKey = identityFile.GetAbsolutePath();
            JSch   jsch        = byIdentityFile.Get(identityKey);

            if (jsch == null)
            {
                jsch = new JSch();
                jsch.SetHostKeyRepository(defaultJSch.GetHostKeyRepository());
                jsch.AddIdentity(identityKey);
                byIdentityFile.Put(identityKey, jsch);
            }
            return(jsch);
        }
Exemplo n.º 2
0
 public virtual void TestAlias_PreferredAuthentications()
 {
     Config("Host orcz\n" + "\tPreferredAuthentications publickey\n");
     OpenSshConfig.Host h = osc.Lookup("orcz");
     NUnit.Framework.Assert.IsNotNull(h);
     NUnit.Framework.Assert.AreEqual("publickey", h.GetPreferredAuthentications());
 }
Exemplo n.º 3
0
 public virtual void TestAlias_BatchModeYes()
 {
     Config("Host orcz\n" + "\tBatchMode yes\n");
     OpenSshConfig.Host h = osc.Lookup("orcz");
     NUnit.Framework.Assert.IsNotNull(h);
     NUnit.Framework.Assert.AreEqual(true, h.IsBatchMode());
 }
        /// <exception cref="NSch.JSchException"></exception>
        private Session CreateSession(CredentialsProvider credentialsProvider, FS fs, string
                                      user, string pass, string host, int port, OpenSshConfig.Host hc)
        {
            Session session = CreateSession(hc, user, host, port, fs);

            if (pass != null)
            {
                session.SetPassword(pass);
            }
            string strictHostKeyCheckingPolicy = hc.GetStrictHostKeyChecking();

            if (strictHostKeyCheckingPolicy != null)
            {
                session.SetConfig("StrictHostKeyChecking", strictHostKeyCheckingPolicy);
            }
            string pauth = hc.GetPreferredAuthentications();

            if (pauth != null)
            {
                session.SetConfig("PreferredAuthentications", pauth);
            }
            if (credentialsProvider != null && (!hc.IsBatchMode() || !credentialsProvider.IsInteractive
                                                    ()))
            {
                session.SetUserInfo(new CredentialsProviderUserInfo(session, credentialsProvider)
                                    );
            }
            Configure(hc, session);
            return(session);
        }
Exemplo n.º 5
0
 internal virtual void CopyFrom(OpenSshConfig.Host src)
 {
     if (hostName == null)
     {
         hostName = src.hostName;
     }
     if (port == 0)
     {
         port = src.port;
     }
     if (identityFile == null)
     {
         identityFile = src.identityFile;
     }
     if (user == null)
     {
         user = src.user;
     }
     if (preferredAuthentications == null)
     {
         preferredAuthentications = src.preferredAuthentications;
     }
     if (batchMode == null)
     {
         batchMode = src.batchMode;
     }
     if (strictHostKeyChecking == null)
     {
         strictHostKeyChecking = src.strictHostKeyChecking;
     }
 }
Exemplo n.º 6
0
 public virtual void TestAlias_InheritBatchMode()
 {
     Config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n" + "\tBatchMode yes\n"
            );
     OpenSshConfig.Host h = osc.Lookup("orcz");
     NUnit.Framework.Assert.IsNotNull(h);
     NUnit.Framework.Assert.IsTrue(h.IsBatchMode());
 }
Exemplo n.º 7
0
 public virtual void TestAlias_InheritPreferredAuthentications()
 {
     Config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n" + "\tPreferredAuthentications publickey, hostbased\n"
            );
     OpenSshConfig.Host h = osc.Lookup("orcz");
     NUnit.Framework.Assert.IsNotNull(h);
     NUnit.Framework.Assert.AreEqual("publickey,hostbased", h.GetPreferredAuthentications
                                         ());
 }
Exemplo n.º 8
0
 public virtual void TestNoConfig()
 {
     OpenSshConfig.Host h = osc.Lookup("repo.or.cz");
     NUnit.Framework.Assert.IsNotNull(h);
     NUnit.Framework.Assert.AreEqual("repo.or.cz", h.GetHostName());
     NUnit.Framework.Assert.AreEqual("jex_junit", h.GetUser());
     NUnit.Framework.Assert.AreEqual(22, h.GetPort());
     NUnit.Framework.Assert.IsNull(h.GetIdentityFile());
 }
Exemplo n.º 9
0
 public virtual void TestAlias_DoesNotMatch()
 {
     Config("Host orcz\n" + "\tHostName repo.or.cz\n");
     OpenSshConfig.Host h = osc.Lookup("repo.or.cz");
     NUnit.Framework.Assert.IsNotNull(h);
     NUnit.Framework.Assert.AreEqual("repo.or.cz", h.GetHostName());
     NUnit.Framework.Assert.AreEqual("jex_junit", h.GetUser());
     NUnit.Framework.Assert.AreEqual(22, h.GetPort());
     NUnit.Framework.Assert.IsNull(h.GetIdentityFile());
 }
Exemplo n.º 10
0
 public virtual void TestAlias_OptionsSet()
 {
     Config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\tPort 2222\n" + "\tUser jex\n"
            + "\tIdentityFile .ssh/id_jex\n" + "\tForwardX11 no\n");
     OpenSshConfig.Host h = osc.Lookup("orcz");
     NUnit.Framework.Assert.IsNotNull(h);
     NUnit.Framework.Assert.AreEqual("repo.or.cz", h.GetHostName());
     NUnit.Framework.Assert.AreEqual("jex", h.GetUser());
     NUnit.Framework.Assert.AreEqual(2222, h.GetPort());
     NUnit.Framework.Assert.AreEqual(new FilePath(home, ".ssh/id_jex"), h.GetIdentityFile
                                         ());
 }
Exemplo n.º 11
0
 public virtual void TestAlias_OptionsKeywordCaseInsensitive()
 {
     Config("hOsT orcz\n" + "\thOsTnAmE repo.or.cz\n" + "\tPORT 2222\n" + "\tuser jex\n"
            + "\tidentityfile .ssh/id_jex\n" + "\tForwardX11 no\n");
     OpenSshConfig.Host h = osc.Lookup("orcz");
     NUnit.Framework.Assert.IsNotNull(h);
     NUnit.Framework.Assert.AreEqual("repo.or.cz", h.GetHostName());
     NUnit.Framework.Assert.AreEqual("jex", h.GetUser());
     NUnit.Framework.Assert.AreEqual(2222, h.GetPort());
     NUnit.Framework.Assert.AreEqual(new FilePath(home, ".ssh/id_jex"), h.GetIdentityFile
                                         ());
 }
Exemplo n.º 12
0
 /// <exception cref="NSch.JSchException"></exception>
 public override Session GetSession(string user, string pass, string host, int port
                                    , CredentialsProvider credentialsProvider, FS fs)
 {
     lock (this)
     {
         if (config == null)
         {
             config = OpenSshConfig.Get(fs);
         }
         OpenSshConfig.Host hc = config.Lookup(host);
         host = hc.GetHostName();
         if (port <= 0)
         {
             port = hc.GetPort();
         }
         if (user == null)
         {
             user = hc.GetUser();
         }
         Session session = CreateSession(hc, user, host, port, fs);
         if (pass != null)
         {
             session.SetPassword(pass);
         }
         string strictHostKeyCheckingPolicy = hc.GetStrictHostKeyChecking();
         if (strictHostKeyCheckingPolicy != null)
         {
             session.SetConfig("StrictHostKeyChecking", strictHostKeyCheckingPolicy);
         }
         string pauth = hc.GetPreferredAuthentications();
         if (pauth != null)
         {
             session.SetConfig("PreferredAuthentications", pauth);
         }
         if (credentialsProvider != null && (!hc.IsBatchMode() || !credentialsProvider.IsInteractive
                                                 ()))
         {
             session.SetUserInfo(new CredentialsProviderUserInfo(session, credentialsProvider)
                                 );
         }
         Configure(hc, session);
         return(session);
     }
 }
Exemplo n.º 13
0
        /// <summary>Locate the configuration for a specific host request.</summary>
        /// <remarks>Locate the configuration for a specific host request.</remarks>
        /// <param name="hostName">
        /// the name the user has supplied to the SSH tool. This may be a
        /// real host name, or it may just be a "Host" block in the
        /// configuration file.
        /// </param>
        /// <returns>r configuration for the requested name. Never null.</returns>
        public virtual OpenSshConfig.Host Lookup(string hostName)
        {
            IDictionary <string, OpenSshConfig.Host> cache = Refresh();

            OpenSshConfig.Host h = cache.Get(hostName);
            if (h == null)
            {
                h = new OpenSshConfig.Host();
            }
            if (h.patternsApplied)
            {
                return(h);
            }
            foreach (KeyValuePair <string, OpenSshConfig.Host> e in cache.EntrySet())
            {
                if (!IsHostPattern(e.Key))
                {
                    continue;
                }
                if (!IsHostMatch(e.Key, hostName))
                {
                    continue;
                }
                h.CopyFrom(e.Value);
            }
            if (h.hostName == null)
            {
                h.hostName = hostName;
            }
            if (h.user == null)
            {
                h.user = NGit.Transport.OpenSshConfig.UserName();
            }
            if (h.port == 0)
            {
                h.port = NGit.Transport.OpenSshConfig.SSH_PORT;
            }
            h.patternsApplied = true;
            return(h);
        }
Exemplo n.º 14
0
 /// <exception cref="NGit.Errors.TransportException"></exception>
 public override RemoteSession GetSession(URIish uri, CredentialsProvider credentialsProvider
                                          , FS fs, int tms)
 {
     lock (this)
     {
         string user = uri.GetUser();
         string pass = uri.GetPass();
         string host = uri.GetHost();
         int    port = uri.GetPort();
         try
         {
             if (config == null)
             {
                 config = OpenSshConfig.Get(fs);
             }
             OpenSshConfig.Host hc = config.Lookup(host);
             host = hc.GetHostName();
             if (port <= 0)
             {
                 port = hc.GetPort();
             }
             if (user == null)
             {
                 user = hc.GetUser();
             }
             Session session = CreateSession(hc, user, host, port, fs);
             if (pass != null)
             {
                 session.SetPassword(pass);
             }
             string strictHostKeyCheckingPolicy = hc.GetStrictHostKeyChecking();
             if (strictHostKeyCheckingPolicy != null)
             {
                 session.SetConfig("StrictHostKeyChecking", strictHostKeyCheckingPolicy);
             }
             string pauth = hc.GetPreferredAuthentications();
             if (pauth != null)
             {
                 session.SetConfig("PreferredAuthentications", pauth);
             }
             if (credentialsProvider != null && (!hc.IsBatchMode() || !credentialsProvider.IsInteractive
                                                     ()))
             {
                 session.SetUserInfo(new CredentialsProviderUserInfo(session, credentialsProvider)
                                     );
             }
             Configure(hc, session);
             if (!session.IsConnected())
             {
                 session.Connect(tms);
             }
             return(new JschSession(session, uri));
         }
         catch (JSchException je)
         {
             Exception c = je.InnerException;
             if (c is UnknownHostException)
             {
                 throw new TransportException(uri, JGitText.Get().unknownHost);
             }
             if (c is ConnectException)
             {
                 throw new TransportException(uri, c.Message);
             }
             throw new TransportException(uri, je.Message, je);
         }
     }
 }
Exemplo n.º 15
0
        /// <exception cref="System.IO.IOException"></exception>
        private IDictionary <string, OpenSshConfig.Host> Parse(InputStream @in)
        {
            IDictionary <string, OpenSshConfig.Host> m = new LinkedHashMap <string, OpenSshConfig.Host
                                                                            >();
            BufferedReader             br      = new BufferedReader(new InputStreamReader(@in));
            IList <OpenSshConfig.Host> current = new AList <OpenSshConfig.Host>(4);
            string line;

            while ((line = br.ReadLine()) != null)
            {
                line = line.Trim();
                if (line.Length == 0 || line.StartsWith("#"))
                {
                    continue;
                }
                string[] parts    = line.Split("[ \t]*[= \t]", 2);
                string   keyword  = parts[0].Trim();
                string   argValue = parts[1].Trim();
                if (StringUtils.EqualsIgnoreCase("Host", keyword))
                {
                    current.Clear();
                    foreach (string pattern in argValue.Split("[ \t]"))
                    {
                        string             name = Dequote(pattern);
                        OpenSshConfig.Host c    = m.Get(name);
                        if (c == null)
                        {
                            c = new OpenSshConfig.Host();
                            m.Put(name, c);
                        }
                        current.AddItem(c);
                    }
                    continue;
                }
                if (current.IsEmpty())
                {
                    // We received an option outside of a Host block. We
                    // don't know who this should match against, so skip.
                    //
                    continue;
                }
                if (StringUtils.EqualsIgnoreCase("HostName", keyword))
                {
                    foreach (OpenSshConfig.Host c in current)
                    {
                        if (c.hostName == null)
                        {
                            c.hostName = Dequote(argValue);
                        }
                    }
                }
                else
                {
                    if (StringUtils.EqualsIgnoreCase("User", keyword))
                    {
                        foreach (OpenSshConfig.Host c in current)
                        {
                            if (c.user == null)
                            {
                                c.user = Dequote(argValue);
                            }
                        }
                    }
                    else
                    {
                        if (StringUtils.EqualsIgnoreCase("Port", keyword))
                        {
                            try
                            {
                                int port = System.Convert.ToInt32(Dequote(argValue));
                                foreach (OpenSshConfig.Host c in current)
                                {
                                    if (c.port == 0)
                                    {
                                        c.port = port;
                                    }
                                }
                            }
                            catch (FormatException)
                            {
                            }
                        }
                        else
                        {
                            // Bad port number. Don't set it.
                            if (StringUtils.EqualsIgnoreCase("IdentityFile", keyword))
                            {
                                foreach (OpenSshConfig.Host c in current)
                                {
                                    if (c.identityFile == null)
                                    {
                                        c.identityFile = ToFile(Dequote(argValue));
                                    }
                                }
                            }
                            else
                            {
                                if (StringUtils.EqualsIgnoreCase("PreferredAuthentications", keyword))
                                {
                                    foreach (OpenSshConfig.Host c in current)
                                    {
                                        if (c.preferredAuthentications == null)
                                        {
                                            c.preferredAuthentications = Nows(Dequote(argValue));
                                        }
                                    }
                                }
                                else
                                {
                                    if (StringUtils.EqualsIgnoreCase("BatchMode", keyword))
                                    {
                                        foreach (OpenSshConfig.Host c in current)
                                        {
                                            if (c.batchMode == null)
                                            {
                                                c.batchMode = Yesno(Dequote(argValue));
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (StringUtils.EqualsIgnoreCase("StrictHostKeyChecking", keyword))
                                        {
                                            string value = Dequote(argValue);
                                            foreach (OpenSshConfig.Host c in current)
                                            {
                                                if (c.strictHostKeyChecking == null)
                                                {
                                                    c.strictHostKeyChecking = value;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(m);
        }
 /// <summary>Create a new remote session for the requested address.</summary>
 /// <remarks>Create a new remote session for the requested address.</remarks>
 /// <param name="hc">host configuration</param>
 /// <param name="user">login to authenticate as.</param>
 /// <param name="host">server name to connect to.</param>
 /// <param name="port">port number of the SSH daemon (typically 22).</param>
 /// <param name="fs">
 /// the file system abstraction which will be necessary to
 /// perform certain file system operations.
 /// </param>
 /// <returns>new session instance, but otherwise unconfigured.</returns>
 /// <exception cref="NSch.JSchException">the session could not be created.</exception>
 protected internal virtual Session CreateSession(OpenSshConfig.Host hc, string user
                                                  , string host, int port, FS fs)
 {
     return(GetJSch(hc, fs).GetSession(user, host, port));
 }
 /// <summary>
 /// Provide additional configuration for the session based on the host
 /// information.
 /// </summary>
 /// <remarks>
 /// Provide additional configuration for the session based on the host
 /// information. This method could be used to supply
 /// <see cref="NSch.UserInfo">NSch.UserInfo</see>
 /// .
 /// </remarks>
 /// <param name="hc">host configuration</param>
 /// <param name="session">session to configure</param>
 protected internal abstract void Configure(OpenSshConfig.Host hc, Session session
                                            );
Exemplo n.º 18
0
 public virtual void TestAlias_BatchModeDefault()
 {
     OpenSshConfig.Host h = osc.Lookup("orcz");
     NUnit.Framework.Assert.IsNotNull(h);
     NUnit.Framework.Assert.AreEqual(false, h.IsBatchMode());
 }
 /// <exception cref="NGit.Errors.TransportException"></exception>
 public override RemoteSession GetSession(URIish uri, CredentialsProvider credentialsProvider
                                          , FS fs, int tms)
 {
     lock (this)
     {
         string user = uri.GetUser();
         string pass = uri.GetPass();
         string host = uri.GetHost();
         int    port = uri.GetPort();
         try
         {
             if (config == null)
             {
                 config = OpenSshConfig.Get(fs);
             }
             OpenSshConfig.Host hc = config.Lookup(host);
             host = hc.GetHostName();
             if (port <= 0)
             {
                 port = hc.GetPort();
             }
             if (user == null)
             {
                 user = hc.GetUser();
             }
             Session session = CreateSession(credentialsProvider, fs, user, pass, host, port,
                                             hc);
             int retries = 0;
             while (!session.IsConnected() && retries < 3)
             {
                 try
                 {
                     retries++;
                     session.Connect(tms);
                 }
                 catch (JSchException e)
                 {
                     session.Disconnect();
                     session = null;
                     // if authentication failed maybe credentials changed at the
                     // remote end therefore reset credentials and retry
                     if (credentialsProvider != null && e.InnerException == null && e.Message.Equals("Auth fail"
                                                                                                     ) && retries < 3)
                     {
                         credentialsProvider.Reset(uri);
                         session = CreateSession(credentialsProvider, fs, user, pass, host, port, hc);
                     }
                     else
                     {
                         throw;
                     }
                 }
             }
             return(new JschSession(session, uri));
         }
         catch (JSchException je)
         {
             Exception c = je.InnerException;
             if (c is UnknownHostException)
             {
                 throw new TransportException(uri, JGitText.Get().unknownHost);
             }
             if (c is ConnectException)
             {
                 throw new TransportException(uri, c.Message);
             }
             throw new TransportException(uri, je.Message, je);
         }
     }
 }
Exemplo n.º 20
0
 protected internal override void Configure(OpenSshConfig.Host hc, Session session
                                            )
 {
 }
Exemplo n.º 21
0
 public virtual void TestAlias_PreferredAuthenticationsDefault()
 {
     OpenSshConfig.Host h = osc.Lookup("orcz");
     NUnit.Framework.Assert.IsNotNull(h);
     NUnit.Framework.Assert.IsNull(h.GetPreferredAuthentications());
 }