Hi, I'm trying to write my own remote shell program, It simply connects to a remote server then copy the sock fd to stdin, stdout and stderr. And it works. But I found several problems: 1. It doesn't handle characters like ^A ^E or tab (any bash/readline stuff) -> openssh itself doesn't seem to incorporate readline, so the magic is not on readline 2. If I run command like VIM, it doesn't run. And buttons like "<ESC>:q" won't work Can someone enlighten me about what SSH did, so it all works perfectly? Attached the prototype: int sockfd = socket(PF_INET, SOCK_STREAM, 0); struct sockaddr_in remaddr = { 0 }; remaddr.sin_family = AF_INET; remaddr.sin_port = htons(9999); remaddr.sin_addr.s_addr = inet_addr("192.168.56.1"); if (connect(sockfd, (struct sockaddr *)&remaddr, sizeof(struct sockaddr))) ERR_QUIT("connect"); for (int i = 0; i < 2; ++i ) dup2 (sockfd, i); execl("/bin/bash", "/bin/bash", "-i", NULL); -- Best Regards, Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/ Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33
Darren Tucker
2014-Feb-10 08:59 UTC
What magic did openssh do to handle programs like VIM?
On Mon, Feb 10, 2014 at 7:39 PM, Aaron Lewis <the.warl0ck.1989 at gmail.com> wrote:> I'm trying to write my own remote shell program,[...]> 2. If I run command like VIM, it doesn't run. > And buttons like "<ESC>:q" won't work > > Can someone enlighten me about what SSH did, so it all works perfectly?Pseudo terminals: http://en.wikipedia.org/wiki/Pseudo_terminal. ssh asks sshd for one when you run an interactive shell (eg "ssh yourserver") or you use the -t flag. You can run vim via ssh without a pty and it'll behave like your application: "ssh server vim somefile". If you can find a copy of Stevens' Advanced Programming in the Unix Environment it's got a chapter on PTYs that's pretty good. Failing that, take a look at the openpty man page and the code in openssh's sshpty.c. if you don't have openpty then your task just got harder because it's quite platform-specific; take a look in openbsd-compat/bsd-openpty.c. -- Darren Tucker (dtucker at zip.com.au) GPG key 8FF4FA69 / D9A3 86E9 7EEE AF4B B2D4 37C9 C982 80C7 8FF4 FA69 Good judgement comes with experience. Unfortunately, the experience usually comes from bad judgement.
Hi, I'm not familiar with openssh internals, but in order to "catch" the specials characters, you may want to use ioctl(2) with the requests TCGETS and TCSETS. * On Monday, 10 February 2014 09:45, Aaron Lewis <the.warl0ck.1989 at gmail.com> wrote:> Hi, > > I'm trying to write my own remote shell program, > > It simply connects to a remote server then copy the sock fd to stdin, > stdout and stderr. And it works. > > But I found several problems: > 1. It doesn't handle characters like ^A ^E or tab (any bash/readline stuff) > -> openssh itself doesn't seem to incorporate readline, so the > magic is not on readline > 2. If I run command like VIM, it doesn't run. > And buttons like "<ESC>:q" won't work > > Can someone enlighten me about what SSH did, so it all works perfectly? > > Attached the prototype: > > int sockfd = socket(PF_INET, SOCK_STREAM, 0); > struct sockaddr_in remaddr = { 0 }; > > remaddr.sin_family = AF_INET; > remaddr.sin_port = htons(9999); > remaddr.sin_addr.s_addr = inet_addr("192.168.56.1"); > > if (connect(sockfd, (struct sockaddr *)&remaddr, sizeof(struct sockaddr))) > ERR_QUIT("connect"); > > for (int i = 0; i < 2; ++i ) > dup2 (sockfd, i); > execl("/bin/bash", "/bin/bash", "-i", NULL); > > > > -- > Best Regards, > Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/ > Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33 > _______________________________________________ > openssh-unix-dev mailing list > openssh-unix-dev at mindrot.org > https://lists.mindrot.org/mailman/listinfo/openssh-unix-dev
Nico Kadel-Garcia
2014-Feb-10 09:10 UTC
What magic did openssh do to handle programs like VIM?
On Mon, Feb 10, 2014 at 3:39 AM, Aaron Lewis <the.warl0ck.1989 at gmail.com> wrote:> Hi, > > I'm trying to write my own remote shell program,*Why*? Seriously, why re-invent the wheel? If you need an insecure version of such a toolkit, look back into the old telnet source code. For secure versions, use OpenSSH.