From f7c6cd72bf05dd4117aa444e11d3283c50705432 Mon Sep 17 00:00:00 2001 From: Toby Date: Thu, 2 Jan 2025 01:42:53 +0100 Subject: [PATCH] wip --- Makefile | 8 ++-- README.md | 5 +++ src/config.c | 28 ++++++++++++ src/config.h | 8 ++++ src/main.c | 26 +++++++++++- src/server.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/server.h | 6 +++ 7 files changed, 194 insertions(+), 5 deletions(-) create mode 100644 README.md create mode 100644 src/config.c create mode 100644 src/config.h create mode 100644 src/server.c create mode 100644 src/server.h diff --git a/Makefile b/Makefile index ff189b3..bdde0bf 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,16 @@ CC=clang -CFLAGS=-O3 -Wall -Wextra -pedantic -lexif -SRC=src/main.c +CFLAGS=-Wall -Wextra -pedantic -lyaml -lssh2 +SRC=src/main.c src/config.c src/server.c DEBUG=dist/debug/sync RELEASE=dist/release/sync default: $(SRC) mkdir -p dist/debug - $(CC) $(CFLAGS) -o $(DEBUG) -ggdb $(SRC) + $(CC) $(CFLAGS) -O0 -g -o $(DEBUG) $(SRC) release: $(SRC) mkdir -p dist/release - $(CC) $(CFLAGS) -o $(RELEASE) $(SRC) + $(CC) $(CFLAGS) -O3 -o $(RELEASE) $(SRC) debug: $(DEBUG) lldb $(DEBUG) diff --git a/README.md b/README.md new file mode 100644 index 0000000..0c89745 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Sync SSH Keys + +### Dependencies +- `libyaml` +- `libssh2` diff --git a/src/config.c b/src/config.c new file mode 100644 index 0000000..3d37375 --- /dev/null +++ b/src/config.c @@ -0,0 +1,28 @@ +#include +#include + +#include "config.h" + +#define CONFIG_BUF_SIZE 128 + +char *config_generate_authorized_keys(FILE *config) { + yaml_parser_t parser; + yaml_token_t token; + + yaml_parser_initialize(&parser); + + yaml_parser_set_input_file(&parser, config); + + //if (!yaml_parser_load(&parser, &config)) { + // fprintf(stderr, "Error parsing config: %s\n", config_path); + // goto shutdown; + //} + + // TODO + +shutdown: + + yaml_parser_delete(&parser); + + return ""; +} diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..35929bc --- /dev/null +++ b/src/config.h @@ -0,0 +1,8 @@ +#ifndef _H_CONFIG +#define _H_CONFIG + +#include + +char *config_generate_authorized_keys(FILE *config); + +#endif diff --git a/src/main.c b/src/main.c index 063e5e7..d4fd019 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,30 @@ #include +#include "config.h" +#include "server.h" + +//void generate_authorized_keys(); +//void write_authorized_keys_to_server(); + +const char *config_path = "/Users/toby/Developer/python/sync-ssh-keys/config.yaml"; +const char *host = "10.0.0.13"; +const int port = 22; +const char *username = "toby"; +const char *pubkey = "/Users/toby/.ssh/id_ed25519.pub"; +const char *privkey = "/Users/toby/.ssh/id_ed25519"; +const char *password = ""; +const char *remote_path = "/home/toby/test.txt"; + int main(int argc, char *argv[]) { - printf("Hello World!\n"); + FILE *config = fopen(config_path, "r"); + + char *authorized_keys = config_generate_authorized_keys(config); + + fclose(config); + + printf("----- BEGIN AUTHORIZED KEYS -----\n%s\n----- END AUTHORIZED KEYS -----\n", authorized_keys); + + //server_write_file(host, port, username, pubkey, privkey, password, remote_path, authorized_keys); + return 0; } diff --git a/src/server.c b/src/server.c new file mode 100644 index 0000000..3a9ef33 --- /dev/null +++ b/src/server.c @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "server.h" + +int server_write_file(char *host, int port, char *username, char *pubkey, char *privkey, char *password, char *remote_path, char *content) { + int rc; + libssh2_socket_t sock; + struct sockaddr_in sin; + LIBSSH2_SESSION *session = NULL; + LIBSSH2_SFTP *sftp_session; + LIBSSH2_SFTP_HANDLE *sftp_handle; + struct addrinfo *res = NULL; + struct addrinfo *i; + + if (getaddrinfo(host, NULL, 0, &res) != 0) { + fprintf(stderr, "Cloudn't resolve host \"%s\"\n", host); + return 1; + } + + for (i = res; i != NULL; i = i->ai_next) { + if (i->ai_addr->sa_family == AF_INET) { + sin = *(struct sockaddr_in*)i->ai_addr; + break; + } + } + + rc = libssh2_init(0); + + if(rc) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); + return 1; + } + + sock = socket(AF_INET, SOCK_STREAM, 0); + + if (sock == LIBSSH2_INVALID_SOCKET) { + fprintf(stderr, "failed to create socket.\n"); + goto shutdown; + } + + //sin.sin_family = AF_INET; + sin.sin_port = htons(port); + //sin.sin_addr.s_addr = inet_addr(host); + + if (connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { + fprintf(stderr, "failed to connect.\n"); + goto shutdown; + } + + session = libssh2_session_init(); + + if (!session) { + fprintf(stderr, "Could not initialize SSH session.\n"); + goto shutdown; + } + + libssh2_session_set_blocking(session, 1); + + rc = libssh2_session_handshake(session, sock); + + if (rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + goto shutdown; + } + + if (libssh2_userauth_publickey_fromfile(session, username, pubkey, privkey, password)) { + fprintf(stderr, "Authentication by public key failed.\n"); + goto shutdown; + } + + sftp_session = libssh2_sftp_init(session); + + if (!sftp_session) { + fprintf(stderr, "Unable to init SFTP session\n"); + goto shutdown; + } + + sftp_handle = libssh2_sftp_open(sftp_session, remote_path, LIBSSH2_FXF_WRITE | LIBSSH2_FXF_CREAT | LIBSSH2_FXF_TRUNC, LIBSSH2_SFTP_S_IRUSR | LIBSSH2_SFTP_S_IWUSR | LIBSSH2_SFTP_S_IRGRP | LIBSSH2_SFTP_S_IROTH); + + if (!sftp_handle) { + fprintf(stderr, "Unable to open file with SFTP: %ld\n", libssh2_sftp_last_error(sftp_session)); + goto shutdown; + } + + libssh2_sftp_write(sftp_handle, content, strlen(content)); + + libssh2_sftp_close(sftp_handle); + + libssh2_sftp_shutdown(sftp_session); + +shutdown: + + if(session) { + libssh2_session_disconnect(session, "Normal Shutdown"); + + libssh2_session_free(session); + + } + + if(sock != LIBSSH2_INVALID_SOCKET) { + shutdown(sock, 2); + close(sock); + } + + fprintf(stderr, "all done\n"); + + libssh2_exit(); + + return 0; +} diff --git a/src/server.h b/src/server.h new file mode 100644 index 0000000..c71518c --- /dev/null +++ b/src/server.h @@ -0,0 +1,6 @@ +#ifndef _H_SERVER +#define _H_SERVER + +int server_write_file(char *host, int port, char *username, char *pubkey, char *privkey, char *password, char *remote_path, char *content); + +#endif