Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libcli without telnet #27

Open
jchoi999 opened this issue Jan 23, 2017 · 10 comments
Open

libcli without telnet #27

jchoi999 opened this issue Jan 23, 2017 · 10 comments

Comments

@jchoi999
Copy link

Hi
Is there any way I can use libcli without using telnet?
I'm wondering it is possible that run a program like clitest and it displays login screen directly without using telnet session and access cli commands.

Thanks,

@dparrish
Copy link
Owner

dparrish commented Jan 24, 2017 via email

@jchoi999
Copy link
Author

jchoi999 commented Jan 24, 2017 via email

@dparrish
Copy link
Owner

dparrish commented Jan 25, 2017 via email

@jchoi999
Copy link
Author

jchoi999 commented Jan 25, 2017 via email

@zorun
Copy link

zorun commented Aug 20, 2020

I'm also interested in running libcli-based programs locally, without sockets or telnet.

I tried the "simple" hack of passing stdin as socket to cli_loop(), and it almost works: I can see the output of libcli, and I can type stuff into libcli (e.g. ? is interpreted). However, completion is not working, and pressing Enter doesn't do anything.

Based on #39 I found a workaround with stty, it was just missing -icrnl to convert newlines to \r\n.

Here is the invocation:

stty_orig="$(stty -g)" ; stty -echo -icanon -icrnl ; ./localcli ; stty $stty_orig

And here is the entire C code of localcli.c:

#include "libcli.h"

int main() {
 struct cli_def *cli;

 cli = cli_init();
 cli_set_banner(cli, "libcli test environment");
 cli_set_hostname(cli, "router");
 cli_telnet_protocol(cli, 0);

 cli_register_command(cli, NULL, "simple", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);

 /* Hack: pass stdin as socket */
 cli_loop(cli, 0);
 cli_done(cli);

 return 0;
}

@zorun
Copy link

zorun commented Aug 20, 2020

While I'm happy it works, this is really a hack. Would there be a better way to do this?

I've thought about forking and creating a socket pair to communicate between parent and child (similar to what @RobSanders proposed in #42 ). However, I'm not sure how it would solve issues such as the lack of \r\n? The parent would need to convert \n to \r\n before writing data to the socket, and probably other adaptations?

@RobSanders
Copy link
Collaborator

Libcli was written assuming telnet style I/O, which I believe is different than if you were simply reading stdin or a socket (and may vary OS to OS to boot). Not sure I tested the completions fully in #39 , just basic 'enter the commandline'.
The fork method we're doing in #42 was more for our application. As mentioned there, we have our libcli app as the default shell for certain users. Our 'front end' processes monitors stdin and copies that data to the 'back end' process process socket, where libcli takes over. We were not allowed to have 'direct' access between out application and the outside world.

@zorun
Copy link

zorun commented Aug 23, 2020

Ok, thanks @RobSanders

Letting libcli interpret terminal "special codes" seems to work quite well in the few terminal emulators I tested.

The hack with stty can be done directly in C. It's actually almost equivalent to putting the terminal in "raw" mode. Here is the code, very slightly adapted from http://kirste.userpage.fu-berlin.de/chemnet/use/info/libc/libc_12.html#SEC250 :

#include <termios.h>

/* Saves the original terminal attributes. */
struct termios saved_termios;

void set_input_mode(void)
{
    struct termios tattr;

    /* Make sure stdin is a terminal. */
    if (!isatty(STDIN_FILENO))
    {
	fprintf(stderr, "Not a terminal.\n");
	exit(EXIT_FAILURE);
    }

    /* Save the terminal attributes so we can restore them later. */
    tcgetattr(STDIN_FILENO, &saved_termios);
    atexit(reset_input_mode);

    /* Set the funny terminal modes. */
    tcgetattr(STDIN_FILENO, &tattr);
    tattr.c_lflag &= ~(ICANON|ECHO);       /* Clear ICANON and ECHO. */
    tattr.c_iflag &= ~(ICRNL);             /* Clear ICRNL. */
    tattr.c_cc[VMIN] = 1;
    tattr.c_cc[VTIME] = 0;
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &tattr);
}

I'm still having trouble with restoring the saved terminal attributes on exit. Bash does it automatically, but only when quitting with Ctrl-C. When exiting with Ctrl-D, bash does nothing special and leaves the terminal in a broken state (no echo). Even restoring the saved terminal attributes with tcsetattr on exit does nothing... I guess it depends both on the shell and the terminal emulator.

@cooljake777
Copy link

cooljake777 commented May 24, 2021

Thank you for your posts here @zorun. I figured out why restoring the terminal attributes wasn't working. It turns out that cli_loop() closes the file descriptor on exit! So when you call tcsetattr(STDIN_FILENO,...) to restore the terminal attributes, it will not work. My workaround is to dup() and pass the result to cli_loop() like so:

int fd = dup(STDIN_FILENO); 
set_input_mode();
cli_loop(cli, fd);

@heeplr
Copy link

heeplr commented Nov 4, 2023

@dparrish would you accept a pull request that adds this?

Either we could check if the fd passed to cli_run() is a socket or we could use a flag like telnet_protocol.

But the whole library was designed around handling telnet codes for controlling the cursor

It's libcli tho, not libtelnetcli ;-) It almost works with stdin/stdout/stderr with just minor changes. And I think telnet_protocol is already there for turning telnet stuff off, isn't it?

libcli is awesome and it would be super nice if it worked with non-socket fd's.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants