diff options
-rw-r--r-- | examples/sample.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/examples/sample.c b/examples/sample.c index c2aaa66..0a8cc86 100644 --- a/examples/sample.c +++ b/examples/sample.c @@ -28,6 +28,7 @@ clients must be made or how a client should react. #include <sys/ioctl.h> #include <signal.h> #include <errno.h> +#include <libssh/callbacks.h> #include <libssh/libssh.h> #include <libssh/sftp.h> @@ -41,6 +42,32 @@ char *cmds[MAXCMD]; struct termios terminal; void do_sftp(ssh_session session); +static int auth_callback(const char *prompt, char *buf, size_t len, + int echo, int verify, void *userdata) { + char *answer = NULL; + char *ptr; + + (void) verify; + (void) userdata; + + if (echo) { + while ((answer = fgets(buf, len, stdin)) == NULL); + if ((ptr = strchr(buf, '\n'))) { + ptr = '\0'; + } + } else { + answer = getpass(prompt); + } + + if (answer == NULL) { + return -1; + } + + strncpy(buf, answer, len); + + return 0; +} + static void add_cmd(char *cmd){ int n; for(n=0;cmds[n] && (n<MAXCMD);n++); @@ -510,9 +537,18 @@ int main(int argc, char **argv){ char buf[10]; unsigned char *hash = NULL; int hlen; + ssh_callbacks cb; session = ssh_new(); + cb = malloc(sizeof(ssh_callbacks)); + + cb->auth_function = auth_callback; + cb->userdata = NULL; + + ssh_callbacks_init(cb); + ssh_set_callbacks(session, cb); + if(ssh_options_getopt(session, &argc, argv)) { fprintf(stderr, "error parsing command line :%s\n", ssh_get_error(session)); |