113 lines
1.9 KiB
C
113 lines
1.9 KiB
C
#include <esp_littlefs.h>
|
|
#include <libssh/libssh.h>
|
|
#include <libssh/server.h>
|
|
#include <libssh/callbacks.h>
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include <sys/queue.h>
|
|
#include "example.h"
|
|
#include "sshd.h"
|
|
|
|
#define BASE_PATH "/littlefs"
|
|
#define CERT_KEY_PATH BASE_PATH "/key"
|
|
#define CERT_PUB_PATH BASE_PATH "/key.pub"
|
|
|
|
#define BUF_SIZE 256
|
|
|
|
static char key[BUF_SIZE];
|
|
|
|
static struct ssh_user hardcoded_example_users[] = {
|
|
{
|
|
.su_user = "odroid",
|
|
.su_password = "odroid",
|
|
},
|
|
{
|
|
}
|
|
};
|
|
|
|
static esp_err_t init_storage()
|
|
{
|
|
esp_err_t ret;
|
|
esp_vfs_littlefs_conf_t conf = {
|
|
.base_path = "/littlefs",
|
|
.partition_label = "storage",
|
|
.format_if_mount_failed = true,
|
|
.dont_mount = false,
|
|
};
|
|
|
|
ret = esp_vfs_littlefs_register(&conf);
|
|
if (ret != ESP_OK)
|
|
{
|
|
return ret;
|
|
}
|
|
return ESP_OK;
|
|
}
|
|
|
|
static esp_err_t load_key_pair(char *path, char* buf, size_t buf_size)
|
|
{
|
|
FILE *fp;
|
|
size_t size;
|
|
fp = fopen(path, "r");
|
|
esp_err_t ret;
|
|
if (fp == NULL)
|
|
{
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
memset(buf, 0, buf_size);
|
|
|
|
fseek(fp, 0, SEEK_END);
|
|
size = ftell(fp);
|
|
rewind(fp);
|
|
|
|
if (fread(buf, 1, buf_size, fp) == size)
|
|
{
|
|
ret = ESP_OK;
|
|
}
|
|
else
|
|
{
|
|
ret = ESP_FAIL;
|
|
}
|
|
|
|
fclose(fp);
|
|
return ret;
|
|
}
|
|
|
|
static struct ssh_user *
|
|
lookup_user(struct server_ctx *sc, const char *user)
|
|
{
|
|
struct ssh_user *su;
|
|
for (su = hardcoded_example_users; su->su_user; su++) {
|
|
if (strcmp(user, su->su_user) == 0)
|
|
return su;
|
|
}
|
|
return NULL;
|
|
|
|
}
|
|
|
|
void
|
|
sshd_task(void *arg)
|
|
{
|
|
struct server_ctx *sc;
|
|
sc = calloc(1, sizeof(struct server_ctx));
|
|
if (!sc)
|
|
return;
|
|
sc->sc_host_key = key;
|
|
sc->sc_lookup_user = lookup_user;
|
|
sc->sc_begin_interactive_session = minicli_begin_interactive_session;
|
|
sc->sc_auth_methods = SSH_AUTH_METHOD_PASSWORD;
|
|
sshd_main(sc);
|
|
|
|
vTaskDelete(NULL);
|
|
}
|
|
|
|
void
|
|
start_sshd(void)
|
|
{
|
|
init_storage();
|
|
load_key_pair(CERT_KEY_PATH, key, BUF_SIZE);
|
|
|
|
puts(key);
|
|
|
|
xTaskCreate(sshd_task, "sshd", 32768, NULL, 10, NULL);
|
|
} |