This commit is contained in:
2025-01-02 01:42:53 +01:00
parent eb734a78a7
commit f7c6cd72bf
7 changed files with 194 additions and 5 deletions

View File

@@ -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)

5
README.md Normal file
View File

@@ -0,0 +1,5 @@
# Sync SSH Keys
### Dependencies
- `libyaml`
- `libssh2`

28
src/config.c Normal file
View File

@@ -0,0 +1,28 @@
#include <stdio.h>
#include <yaml.h>
#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 "";
}

8
src/config.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef _H_CONFIG
#define _H_CONFIG
#include <stdio.h>
char *config_generate_authorized_keys(FILE *config);
#endif

View File

@@ -1,6 +1,30 @@
#include <stdio.h>
#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;
}

118
src/server.c Normal file
View File

@@ -0,0 +1,118 @@
#include <netdb.h>
#include <stdio.h>
#include <libssh2.h>
#include <libssh2_sftp.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#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;
}

6
src/server.h Normal file
View File

@@ -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