Skip to content

Git server doesn't work with Win32-OpenSSH. Enclosed, please find three defects which I isolated and worked around.. #752

Open

Description

@razrjk

I pulled version: 0.0.14.0 on May 19. I just fetched version: 0.0.15.0 (June 4) today to compare. I don't believe the below issues have been corrected.

Git using Win32-OpenSSH ssh server runs as a nopty service. There are two defects in the nopty code:

  1. shell-host.c passes through to cmd.exe, single quotes as literals. The literal single quotes become part of the file path name, resulting in an incorrect file path.
  2. The service loop in shell-host.c, which passes its stdin to the child stdin is coded with incorrect file handles. Part of the input is currently passed back to sshd.

A temporary workaround for issue 1 is to replace all single quotes with double quotes just before the call to invoke cmd /c:

@@ -1136,7 +1136,21 @@ start_withno_pty(wchar_t *command)
	if (command) {
		GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, MAX_CMD_LEN, L" /c"));
		GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, MAX_CMD_LEN, L" "));

	+	int i = 0;
	+	while (command[i] != L'\0')
	+	{
	+		if (command[i] == L'\'' )
	+			command[i] = L'\"';
	+		i++;
	+	}
		GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, MAX_CMD_LEN, command));

A fix for issue 2 is to use the child process file handle instead of the sshd file handle:

@@ -1184,7 +1198,7 @@ start_withno_pty(wchar_t *command)
			/* for backspace, we need to send space and another backspace for visual erase */
			if (buf[i] == '\b') {
				if (in_cmd_len > 0) {
-					GOTO_CLEANUP_ON_FALSE(WriteFile(pipe_out, "\b \b", 3, &wr, NULL));
+					GOTO_CLEANUP_ON_FALSE(WriteFile(child_pipe_write, "\b \b", 3, &wr, NULL));
					in_cmd_len--;
				}
				i++;
@@ -1194,10 +1208,10 @@ start_withno_pty(wchar_t *command)
			/* For CR and LF */
			if ((buf[i] == '\r') || (buf[i] == '\n')) {
				/* TODO - do a much accurate mapping */
-				GOTO_CLEANUP_ON_FALSE(WriteFile(pipe_out, buf + i, 1, &wr, NULL));
+				GOTO_CLEANUP_ON_FALSE(WriteFile(child_pipe_write, buf + i, 1, &wr, NULL));
				if ((buf[i] == '\r') && ((i == rd - 1) || (buf[i + 1] != '\n'))) {
					buf[i] = '\n';
-					GOTO_CLEANUP_ON_FALSE(WriteFile(pipe_out, buf + i, 1, &wr, NULL));
+					GOTO_CLEANUP_ON_FALSE(WriteFile(child_pipe_write, buf + i, 1, &wr, NULL));
				}
				in_cmd[in_cmd_len] = buf[i];
				in_cmd_len++;
@@ -1207,7 +1221,7 @@ start_withno_pty(wchar_t *command)
				continue;
			}
 
-			GOTO_CLEANUP_ON_FALSE(WriteFile(pipe_out, buf + i, 1, &wr, NULL));
+			GOTO_CLEANUP_ON_FALSE(WriteFile(child_pipe_write, buf + i, 1, &wr, NULL));
			in_cmd[in_cmd_len] = buf[i];
			in_cmd_len++;
			if (in_cmd_len == MAX_CMD_LEN - 1) {

Finally, there is defect and debug issue in signal_sigchld.c. The zombied children count can become too large, which causes a seg fault. When debugging sshd.exe /ddd, debug3() is writing during signal processing which corrupts the stack. Debug3() should not write while running on an exception frame. A check for incorrect zombie count should be added:

@@ -104,14 +104,20 @@ sw_child_to_zombie(DWORD index)
	DWORD last_non_zombie, zombie_pid;
	HANDLE zombie_handle;
 
-	debug3("zombie'ing child at index %d, %d zombies of %d", index,
-		children.num_zombies, children.num_children);
+	//debug3("zombie'ing child at index %d, %d zombies of %d", index,
+		//children.num_zombies, children.num_children);
 
	if (index >= children.num_children) {
		errno = EINVAL;
		return -1;
	}
-
+	//debug3(" %x zombies,  %x children",children.num_zombies, children.num_children);
+	if((children.num_children - children.num_zombies) == 0)
+	{
+		//debug2("children == zombies, can't make more zombies");
+		errno = EINVAL;
+		return -1;
+	}
	last_non_zombie = children.num_children - children.num_zombies - 1;
	if (last_non_zombie != index) {
		/* swap */

I'm running on Windows 10 and Windows 7 systems.

Extreme care must be taken with search paths when debugging a git server using ssh protocol on Windows, especially if cygwin and mingw, etc., are also installed on the system This is true for the client and server side. Multiple executables for ssh, sshd, and git commands can cause problems if close attention is not paid to search paths.

Activity

  1. manojampalam commented on Jun 5, 2017

    @manojampalam
    Contributor

    Thanks for the detailed report and proposed changes. Found some details here on how to setup Git server using over SSH - https://www.systutorials.com/366/set-up-git-server-through-ssh-connection/

  2. razrjk commented on Jun 5, 2017

    @razrjk
    Author

    Re: shell-host.c
    Git push was still failing.

    It appears that the whole nopty implementation assumes a pty control stream. Yet nopty may be a binary data stream.
    While I'm uncertain about the defaults and all the possible options of ssh (for instance, assuming pty control for a nopty connection), tty control should not be processed on a binary data stream.

    After removing the pty processing from:
    start_withno_pty(wchar_t *command)
    Git push works.

    I can now clone repos over ssh and push changes to them.
    I will continue to test.

    The whole send to child inner loop simplifies to:

    		while(rd){
    			GOTO_CLEANUP_ON_FALSE(WriteFile(child_pipe_write, buf + i, rd, &wr, NULL));
    			rd = rd - wr;
    			i = i + wr;		
    		}
    
  3. manojampalam commented on Jun 5, 2017

    @manojampalam
    Contributor

    What repo are you working on? There were changes around this logic as part of fixing #658
    Take a look at the PR, binary streaming over stdin should work with the latest changes included in v0.0.15.0.

  4. razrjk commented on Jun 6, 2017

    @razrjk
    Author

    The repo is [email protected]:PowerShell/Win32-OpenSSH.git.
    I cloned, built, and tried to test v.0.0.15.0.

    Issue 1) from my initial post is still a problem. The file path sent by git contains literal single quote characters which become part of the file path, so the path is invalid.

    The remote repo in the following case is: localhost:D:\work\git_repo.git. Note the single quotes below. They may look like a double quote, but they are single quotes around single quotes. The inside single quotes are literals in the file path.

    $ git ls-remote
    fatal: ''d:/work/git_repo.git'' does not appear to be a git repository
    fatal: Could not read from remote repository.
    

    I also originally tried powershell.exe and bash.exe. None of the command line interpreters liked the literal single quotes when passed with the "/c" or "-c" option. cmd.exe works okay with double quotes, so I stopped looking for another solution to the problem.

    Regarding issue 2) of my original post: I do see quite a few changes, but the writes to pipe_out are still going back to the sshd process, not the child process child_pipe_write handle. Next, unless ssh options are mapped directly to cmd.exe options, I don't see how you can possibly detect any sub-mode of nopty, When the pipe is nopty, it's binary. A shell with tty capabilities would need to look at the binary stream and implement any tty escapes, not shell-host.c.
    After starting the process (cmd.exe), shell-host.c must simply get out of the way and let the binary data flow, in both directions.

    Also, sshd.exe /ddd is still broken.

  5. reopened this on Jun 6, 2017
  6. razrjk commented on Jun 7, 2017

    @razrjk
    Author

    I finally resolved another problem with large binary transfers, typical of a big git push.

    shell-host.c is currently using cmd.exe. cmd.exe was dropping the end of large binary streams over slow network connection. I tried bash.exe and powershell.exe as command interpreters and they do not drop any of the stream. It took quite a long time to discover that cmd.exe was the problem.

    I settled on using powershell.exe because it acts like cmd.exe for gitExtension file path resolution and resolves the common forms of path for Windows:

    ssh://<host1>://<host2>/<repo-path>
    <host2>:<drive>:/<repo-path>
    
  7. joeyaiello commented on Jun 13, 2017

    @joeyaiello

    razrjk as an FYI, we do code work out of https://github.com/PowerShell/openssh-portable, and only take drops back to this repo when we do releases.

  8. manojampalam commented on Jun 14, 2017

    @manojampalam
    Contributor

    razrjk please submit a PR on https://github.com/PowerShell/openssh-portable for the proposed changes. Also open a new issue for sshd.exe -ddd

    Also add details on where you installed Git for Windows from.

  9. diogocp commented on Jul 12, 2017

    @diogocp

    To work around this issue, you can run the following commands in your local repository:

    git config --local remote.origin.receivepack "powershell git receive-pack"
    git config --local remote.origin.uploadpack "powershell git upload-pack"
    

    Replace origin with the name of the Windows-hosted remote.

    After this you should be able to pull and push normally (works for me, anyway).

    Make sure you don't select "Use Git from Git Bash only" when installing Git on the Windows server, otherwise Git won't be in the PowerShell path.

  10. diogocp commented on Jul 13, 2017

    @diogocp

    After further testing, I still get an error (early EOF) when pushing binary files. This is with v0.0.17.0.

  11. 36 remaining items

  12. benvindo commented on Oct 3, 2017

    @benvindo

    Hi, sorry for my lack of attention, I was focused on connecting the apps and forgetting the minimally details, Now its work perfectly. Thanks!!

  13. bingbing8 commented on Oct 5, 2017

    @bingbing8
    Contributor
  14. diogocp commented on Oct 6, 2017

    @diogocp

    Yanbing (@bingbing8) yes, same error with 0.0.21.0.

  15. bingbing8 commented on Oct 17, 2017

    @bingbing8
    Contributor

    Diogo Pereira (@diogocp), I really can't repro it. what do you get in the client log?

  16. modified the milestones: Oct-Mid, on Oct 17, 2017
  17. benvindo commented on Oct 19, 2017

    @benvindo

    I have a similar error to Diogo Pereira (@diogocp), at some point when a member of my team sends commits to a TFS repository and after some time we try to send all the commits to the production happens the error:

    `git push serve_prod homol:prod
    Multiprodutos@[email protected]'s password:
    Counting objects: 97, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (47/47), done.
    Writing objects: 100% (97/97), 53.53 KiB | 0 bytes/s, done.
    Total 97 (delta 73), reused 69 (delta 48)
    remote: fatal: early EOF
    error: remote unpack failed: unpack-objects abnormal exit
    To [email protected]:C:/multiprodutos/web/repo/ui-multiprodutos-web.git
     ! [remote rejected] homol -> prod (unpacker error)
    error: failed to push some refs to 'Multiprodutos@[email protected]:C:/multiprodutos/web/repo/ui-multiprodutos-web.git'
    `
    
  18. larry191994 commented on Dec 5, 2018

    @larry191994

    Bruno Benvindo da Costa (@benvindo) which step? this?
    To check out a repository, go to where you want to put your local repo,

     cd c:\mygitrepros
     # initialize a local repo folder
     git init mylocalrepo
     cd mylocalrepo
     # add the remote repro
     git remote add origin user@domain@servermachine:C:/test/myrepo.git
     # work around the known issue by launching powershell to run the git commands
     git config --local remote.origin.uploadpack "powershell git-upload-pack"
     git config --local remote.origin.receivepack "powershell git-receive-pack"
     git fetch origin
    

    I follow your approach but I still get the fatal: ''/C:/git/A.git'' does not appear to be a git repository when I try to git clone the remote repository from another server. I am really confused about this ....

  19. diogocp commented on Dec 5, 2018

    @diogocp

    larry191994 you cannot use git clone directly. You have to follow those instructions every time you want to fetch the repository.

  20. bingbing8 commented on Dec 6, 2018

    @bingbing8
    Contributor

    larry191994 , Diogo Pereira (@diogocp) , after supporting default shell, you don't need the above workaround if you can set up Default Shell. Please refer to the link.

  21. removed this from the milestone on May 21, 2019
  22. yuliu commented on Aug 8, 2019

    @yuliu

    TL;DR:
    Alter the shell for Windows 10's built-in openssh (Sorry, I only installed it from Microsoft, not this repo), either cmd or powershell, to bash that comes with Git for Windows by changing a register key's value (change the path to your Git installation):

    New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Program Files\Git\bin\bash.exe" -PropertyType String -Force
    

    I'm sorry that I'm not commenting for this repo, but having the same problem hosting a Git repo's files on Windows 10 and want to work with them in another system through SSH. The problem looks the same as Yanbing (@bingbing8)'s

    git clone ssh://username@domain@hostmachine/D:/testgit/myrepo.git
    Cloning into 'myrepo'...
    fatal: ''/D:/testgit/myrepo.git'' does not appear to be a git repository
    fatal: Could not read from remote repository.
    
    Please make sure you have the correct access rights
    and the repository exists.
    

    and Bruno Benvindo da Costa (@benvindo)'s

    + git-receive-pack '/........'
    + ~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (git-receive-pack:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
    
    fatal: Could not read from remote repository.
    

    The URL is a trick to make the path to the repo valid. And we also have to alter/add configurations of remote.origin.uploadpack and remote.origin.receivepack to have a workaround for pull/push. But we have to initialize a bare repo first and then do this and do that, which complicates our work.

    Since we have Git installed on Windows 10 (or server...), we might have git-bash installed as well. And we can use Git's bash as the default bash for ssh, so that we can use git normally, without tweaking settings. The way to do so is to change the default shell for Windows 10's built-in openssh to git-bash, read TL;DR in the beginning. Then we should git clone without any issue.

    I don't really know how ssh works internally, but I just guess all these problems in this issue are so related to the host system's shell, openssh should not be blamed for and responsible for them. Probably, Git and Powershell can work together to get git working under powershell and/or even cmd.

  23. essadek commented on Mar 2, 2021

    @essadek

    I come cross the above workaround and it solved the below issue in 2021.

    does not appear to be a git repository
    fatal: Could not read from remote repository.

    Please make sure you have the correct access rights
    and the repository exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions