187 lines
5.0 KiB
C
187 lines
5.0 KiB
C
#include "config.h"
|
|
|
|
#include <libssh/libssh.h>
|
|
#include <libssh/server.h>
|
|
#include <libssh/callbacks.h>
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
#include "priv_key.h"
|
|
|
|
#define BUF_SIZE 2049
|
|
|
|
#define USER "odroid"
|
|
#define PASSWORD "odroid"
|
|
|
|
static int authenticated=0;
|
|
static int tries = 0;
|
|
static int error = 0;
|
|
static ssh_channel chan=NULL;
|
|
|
|
static int auth_none(ssh_session session,
|
|
const char *user,
|
|
void *userdata)
|
|
{
|
|
char buf[100];
|
|
ssh_string banner = NULL;
|
|
|
|
(void)userdata; /* unused */
|
|
|
|
ssh_set_auth_methods(session,
|
|
SSH_AUTH_METHOD_PASSWORD);
|
|
|
|
sprintf(buf, "hello [%s]!\n", user);
|
|
banner = ssh_string_from_char(buf);
|
|
if (banner != NULL) {
|
|
ssh_send_issue_banner(session, banner);
|
|
}
|
|
ssh_string_free(banner);
|
|
|
|
return SSH_AUTH_DENIED;
|
|
}
|
|
|
|
static int auth_password(ssh_session session, const char *user,
|
|
const char *password, void *userdata){
|
|
(void)userdata;
|
|
ssh_string banner = NULL;
|
|
printf("Authenticating user %s pwd %s\n",user, password);
|
|
if(strcmp(user,USER) == 0 && strcmp(password, PASSWORD) == 0){
|
|
authenticated = 1;
|
|
printf("Authenticated\n");
|
|
banner = ssh_string_from_char("Authenticated\n");
|
|
ssh_send_issue_banner(session, banner);
|
|
ssh_string_free(banner);
|
|
return SSH_AUTH_SUCCESS;
|
|
}
|
|
if (tries >= 3){
|
|
printf("Too many authentication tries\n");
|
|
ssh_disconnect(session);
|
|
error = 1;
|
|
return SSH_AUTH_DENIED;
|
|
}
|
|
tries++;
|
|
return SSH_AUTH_DENIED;
|
|
}
|
|
|
|
static int pty_request(ssh_session session, ssh_channel channel, const char *term,
|
|
int x,int y, int px, int py, void *userdata){
|
|
(void) session;
|
|
(void) channel;
|
|
(void) userdata;
|
|
printf("Allocated terminal\n");
|
|
printf("trem: %s, x: %d, y: %d, px: %d, py %d\n", term, x, y, px, py);
|
|
return 0;
|
|
}
|
|
|
|
static int shell_request(ssh_session session, ssh_channel channel, void *userdata){
|
|
(void)session;
|
|
(void)channel;
|
|
(void)userdata;
|
|
printf("Allocated shell\n");
|
|
return 0;
|
|
}
|
|
struct ssh_channel_callbacks_struct channel_cb = {
|
|
.channel_pty_request_function = pty_request,
|
|
.channel_shell_request_function = shell_request
|
|
};
|
|
|
|
static ssh_channel new_session_channel(ssh_session session, void *userdata){
|
|
(void) session;
|
|
(void) userdata;
|
|
if(chan != NULL)
|
|
return NULL;
|
|
printf("Allocated session channel\n");
|
|
chan = ssh_channel_new(session);
|
|
ssh_callbacks_init(&channel_cb);
|
|
ssh_set_channel_callbacks(chan, &channel_cb);
|
|
return chan;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
ssh_session session = NULL;
|
|
ssh_bind sshbind = NULL;
|
|
ssh_event mainloop = NULL;
|
|
struct ssh_server_callbacks_struct cb = {
|
|
.userdata = NULL,
|
|
.auth_none_function = auth_none,
|
|
.auth_password_function = auth_password,
|
|
.channel_open_request_session_function = new_session_channel
|
|
};
|
|
|
|
char buf[BUF_SIZE];
|
|
int i;
|
|
int r;
|
|
|
|
sshbind=ssh_bind_new();
|
|
session=ssh_new();
|
|
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT_STR, "11422");
|
|
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, "0.0.0.0");
|
|
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_IMPORT_KEY_STR, HOST_KEY);
|
|
|
|
(void) argc;
|
|
(void) argv;
|
|
|
|
if(ssh_bind_listen(sshbind)<0){
|
|
printf("Error listening to socket: %s\n",ssh_get_error(sshbind));
|
|
return 1;
|
|
}
|
|
r=ssh_bind_accept(sshbind,session);
|
|
if(r==SSH_ERROR){
|
|
printf("error accepting a connection : %s\n",ssh_get_error(sshbind));
|
|
return 1;
|
|
}
|
|
ssh_callbacks_init(&cb);
|
|
ssh_set_server_callbacks(session, &cb);
|
|
|
|
if (ssh_handle_key_exchange(session)) {
|
|
printf("ssh_handle_key_exchange: %s\n", ssh_get_error(session));
|
|
return 1;
|
|
}
|
|
ssh_set_auth_methods(session,SSH_AUTH_METHOD_PASSWORD);
|
|
mainloop = ssh_event_new();
|
|
ssh_event_add_session(mainloop, session);
|
|
|
|
while (!(authenticated && chan != NULL)){
|
|
if(error)
|
|
break;
|
|
r = ssh_event_dopoll(mainloop, -1);
|
|
if (r == SSH_ERROR){
|
|
printf("Error : %s\n",ssh_get_error(session));
|
|
ssh_disconnect(session);
|
|
return 1;
|
|
}
|
|
}
|
|
if(error){
|
|
printf("Error, exiting loop\n");
|
|
} else
|
|
printf("Authenticated and got a channel\n");
|
|
do{
|
|
i=ssh_channel_read(chan, buf, sizeof(buf) - 1, 0);
|
|
if(i>0) {
|
|
if (ssh_channel_write(chan, buf, i) == SSH_ERROR) {
|
|
printf("error writing to channel\n");
|
|
return 1;
|
|
}
|
|
|
|
buf[i] = '\0';
|
|
printf("%s", buf);
|
|
fflush(stdout);
|
|
|
|
if (buf[0] == '\x0d') {
|
|
if (ssh_channel_write(chan, "\n", 1) == SSH_ERROR) {
|
|
printf("error writing to channel\n");
|
|
return 1;
|
|
}
|
|
|
|
printf("\n");
|
|
}
|
|
}
|
|
} while (i>0);
|
|
ssh_disconnect(session);
|
|
ssh_bind_free(sshbind);
|
|
ssh_finalize();
|
|
return 0;
|
|
} |