| 1 |
sysadm |
1.1 |
#include "log.h"
|
| 2 |
|
|
#include <stdio.h>
|
| 3 |
|
|
#include <libssh/libssh.h>
|
| 4 |
|
|
#include <libssh/server.h>
|
| 5 |
|
|
#include <libssh/callbacks.h>
|
| 6 |
|
|
|
| 7 |
|
|
static int ssh_auth_password_cb(ssh_session session, const char *user, const char *password, void *userdata)
|
| 8 |
|
|
{
|
| 9 |
|
|
if (strcmp(user, "test") == 0 && strcmp(password, "123456") == 0)
|
| 10 |
|
|
{
|
| 11 |
|
|
return SSH_AUTH_SUCCESS;
|
| 12 |
|
|
}
|
| 13 |
|
|
|
| 14 |
|
|
log_std("Debug: SSH Auth OK\n");
|
| 15 |
|
|
return SSH_AUTH_DENIED;
|
| 16 |
|
|
}
|
| 17 |
|
|
|
| 18 |
|
|
int ssh_server(const char *hostaddr, unsigned int port)
|
| 19 |
|
|
{
|
| 20 |
|
|
ssh_bind sshbind;
|
| 21 |
|
|
ssh_session session;
|
| 22 |
|
|
ssh_channel channel;
|
| 23 |
|
|
int ssh_log_level = SSH_LOG_PROTOCOL;
|
| 24 |
|
|
struct ssh_server_callbacks_struct cb = {
|
| 25 |
|
|
.userdata = NULL,
|
| 26 |
|
|
.auth_password_function = ssh_auth_password_cb};
|
| 27 |
|
|
|
| 28 |
|
|
ssh_init();
|
| 29 |
|
|
|
| 30 |
|
|
sshbind = ssh_bind_new();
|
| 31 |
|
|
|
| 32 |
|
|
if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
|
| 33 |
|
|
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
|
| 34 |
|
|
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, "ssh_host_rsa_key") < 0 ||
|
| 35 |
|
|
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
|
| 36 |
|
|
{
|
| 37 |
|
|
log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));
|
| 38 |
|
|
return -1;
|
| 39 |
|
|
}
|
| 40 |
|
|
|
| 41 |
|
|
if (ssh_bind_listen(sshbind) < 0)
|
| 42 |
|
|
{
|
| 43 |
|
|
log_error("Error listening at SSH server port: %s\n", ssh_get_error(sshbind));
|
| 44 |
|
|
return -1;
|
| 45 |
|
|
}
|
| 46 |
|
|
|
| 47 |
|
|
while (1)
|
| 48 |
|
|
{
|
| 49 |
|
|
session = ssh_new();
|
| 50 |
|
|
|
| 51 |
|
|
if (ssh_bind_accept(sshbind, session) != SSH_OK)
|
| 52 |
|
|
{
|
| 53 |
|
|
log_error("Error accept SSH connection: %s\n", ssh_get_error(sshbind));
|
| 54 |
|
|
}
|
| 55 |
|
|
|
| 56 |
|
|
ssh_callbacks_init(&cb);
|
| 57 |
|
|
if (ssh_set_server_callbacks(session, &cb) != SSH_OK)
|
| 58 |
|
|
{
|
| 59 |
|
|
log_error("Error set SSH callback: %s\n", ssh_get_error(sshbind));
|
| 60 |
|
|
}
|
| 61 |
|
|
|
| 62 |
|
|
ssh_set_auth_methods(session, SSH_AUTH_METHOD_PASSWORD);
|
| 63 |
|
|
|
| 64 |
|
|
if (ssh_handle_key_exchange(session) != SSH_OK)
|
| 65 |
|
|
{
|
| 66 |
|
|
log_error("Error exchanging SSH keys: %s\n", ssh_get_error(sshbind));
|
| 67 |
|
|
ssh_disconnect(session);
|
| 68 |
|
|
continue;
|
| 69 |
|
|
}
|
| 70 |
|
|
|
| 71 |
|
|
channel = ssh_channel_new(session);
|
| 72 |
|
|
if (channel == NULL || ssh_channel_open_session(channel) != SSH_OK)
|
| 73 |
|
|
{
|
| 74 |
|
|
log_error("Error opening SSH channel: %s\n", ssh_get_error(sshbind));
|
| 75 |
|
|
ssh_channel_free(channel);
|
| 76 |
|
|
continue;
|
| 77 |
|
|
}
|
| 78 |
|
|
|
| 79 |
|
|
log_std("Debug: #\n");
|
| 80 |
|
|
|
| 81 |
|
|
ssh_channel_close(channel);
|
| 82 |
|
|
ssh_disconnect(session);
|
| 83 |
|
|
ssh_free(session);
|
| 84 |
|
|
}
|
| 85 |
|
|
|
| 86 |
|
|
ssh_bind_free(sshbind);
|
| 87 |
|
|
ssh_finalize();
|
| 88 |
|
|
|
| 89 |
|
|
return 0;
|
| 90 |
|
|
}
|
| 91 |
|
|
|
| 92 |
|
|
int main(int argc, char *argv[])
|
| 93 |
|
|
{
|
| 94 |
|
|
if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
|
| 95 |
|
|
{
|
| 96 |
|
|
printf("Open log error\n");
|
| 97 |
|
|
return -1;
|
| 98 |
|
|
}
|
| 99 |
|
|
|
| 100 |
|
|
log_std_redirect(STDOUT_FILENO);
|
| 101 |
|
|
log_err_redirect(STDERR_FILENO);
|
| 102 |
|
|
|
| 103 |
|
|
ssh_server("0.0.0.0", 2322);
|
| 104 |
|
|
|
| 105 |
|
|
log_end();
|
| 106 |
|
|
|
| 107 |
|
|
return 0;
|
| 108 |
|
|
}
|