Generate a pub file

This commit is contained in:
Quentin Dufour 2019-02-11 09:53:00 +01:00
parent 1af4e69999
commit 81aac5259f
4 changed files with 91 additions and 41 deletions

View File

@ -16,7 +16,7 @@ int main(int argc, char** argv) {
int sock;
struct tor_os_str tos;
tor_os_create (&tos, "onion_services.txt", 10);
tor_os_create (&tos, "onion_services.pub", "onion_services.txt", 10);
tor_os_read (&tos);
int ports[10] = { 7500, 7501, 7502, 7503, 7504, 7505, 7506, 7507, 7508, 7509};

View File

@ -32,19 +32,20 @@ int tor_ctl_connect(struct tor_ctl* ctx, char* addr, char* service) {
int tor_ctl_add_onion(struct tor_ctl* ctx, struct tor_os_str* tos, int* port) {
int err = 0;
char buffer[1024] = {0};
char buffer1[1024] = {0};
char buffer2[1024] = {0};
int to_create = tos->size - tos->filled;
/* Add onion services loaded from file */
for (int i = 0; i < tos->filled; i++) {
fprintf(ctx->wsock, "add_onion %s Port=%d\n", tos->list[i], port[i]);
fprintf(ctx->wsock, "add_onion %s Port=%d\n", tos->keys[i].priv, port[i]);
fscanf(ctx->rsock, "%d", &err);
if (err != 250) {
printf("err: %d\n", err);
return -1;
}
fscanf(ctx->rsock, "-ServiceID=%s", buffer);
printf("Added onion service %s from file\n", buffer);
fscanf(ctx->rsock, "-ServiceID=%s", buffer1);
printf("Added onion service %s.onion from file\n", buffer1);
fscanf(ctx->rsock, "250 OK");
}
@ -55,13 +56,13 @@ int tor_ctl_add_onion(struct tor_ctl* ctx, struct tor_os_str* tos, int* port) {
fscanf(ctx->rsock, "%d", &err);
if (err != 250) return -2;
err = fscanf(ctx->rsock, "-ServiceID=%s\n", buffer);
err = fscanf(ctx->rsock, "-ServiceID=%s\n", buffer1);
if (err <= 0) return -3;
printf("Created onion service %s\n", buffer);
err = fscanf(ctx->rsock, "250-PrivateKey=%s\n", buffer);
printf("Created onion service %s.onion\n", buffer1);
err = fscanf(ctx->rsock, "250-PrivateKey=%s\n", buffer2);
if (err <= 0) return -4;
printf("Onion service private key: %s\n", buffer);
if (tor_os_append(tos, buffer) != 0) return -5;
//printf("Onion service private key: %s\n", buffer);
if (tor_os_append(tos, buffer1, buffer2) != 0) return -5;
err = fscanf(ctx->rsock, "250 OK");
if (err < 0) return -6;
}

View File

@ -1,15 +1,21 @@
#include "tor_os.h"
void tor_os_create(struct tor_os_str* os, char* file, size_t size) {
void tor_os_create(struct tor_os_str* os, char* pub_file, char* priv_file, size_t size) {
os->size = size;
os->filled = 0;
os->file = malloc(sizeof(char) * (strlen(file) + 1));
if (os->file == NULL) goto mem_error;
strcpy(os->file, file);
if (priv_file != NULL) {
os->priv_file = malloc(sizeof(char) * (strlen(priv_file) + 1));
if (os->priv_file == NULL) goto mem_error;
strcpy(os->priv_file, priv_file);
}
os->list = malloc(size * sizeof(char*));
if (os->list == NULL) goto mem_error;
os->pub_file = malloc(sizeof(char) * (strlen(pub_file) + 1));
if (os->pub_file == NULL) goto mem_error;
strcpy(os->pub_file, pub_file);
os->keys = malloc(size * sizeof(struct keypair));
if (os->keys == NULL) goto mem_error;
return;
mem_error:
@ -17,33 +23,47 @@ mem_error:
exit(EXIT_FAILURE);
}
char** tor_os_append_cursor(struct tor_os_str* os) {
struct keypair* tor_os_append_cursor(struct tor_os_str* os) {
if (os->filled == os->size) {
return NULL;
}
char** cur = os->list + os->filled;
struct keypair* cur = os->keys + os->filled;
os->filled++;
*cur = NULL;
cur->priv = NULL;
cur->pub = NULL;
return cur;
}
void tor_os_read (struct tor_os_str* os) {
FILE* fd = NULL;
fd = fopen(os->file, "r");
if (os->priv_file != NULL) {
fd = fopen(os->priv_file, "r");
} else {
fd = fopen(os->pub_file, "r");
}
if (fd == NULL) {
return;
}
size_t len = 0;
size_t str_len = 0;
int n = 0;
char* last_key = NULL;
while (1) {
char** dst = tor_os_append_cursor(os);
struct keypair* dst = tor_os_append_cursor(os);
if (dst == NULL) break;
if (getline(dst,&len,fd) == -1) break;
str_len = strlen(*dst);
if (os->priv_file != NULL) {
if (fscanf(fd, "%ms %ms", &(dst->pub), &(dst->priv)) == -1) break;
last_key = dst->priv;
} else {
if (fscanf(fd, "%ms", &(dst->pub)) == -1) break;
last_key = dst->pub;
}
str_len = strlen(last_key);
if (str_len < 1) break;
if ((*dst)[str_len - 1] == '\n') {
(*dst)[str_len - 1] = '\0';
if (last_key[str_len - 1] == '\n') {
last_key[str_len - 1] = '\0';
}
}
fclose(fd);
@ -51,32 +71,55 @@ void tor_os_read (struct tor_os_str* os) {
void tor_os_free(struct tor_os_str* os) {
for (int i = 0; i < os->filled; i++) {
free(os->list[i]);
free(os->keys[i].pub);
if (os->priv_file != NULL) free(os->keys[i].priv);
}
free(os->list);
free(os->file);
free(os->keys);
if (os->priv_file != NULL) free(os->priv_file);
free(os->pub_file);
}
void tor_os_persist(struct tor_os_str* os) {
FILE* fd = NULL;
fd = fopen(os->file, "w");
fd = fopen(os->pub_file, "w");
if (fd == NULL) {
fprintf(stderr, "unable to open file for writing\n");
fprintf(stderr, "unable to open pub file %s for writing\n", os->pub_file);
exit(EXIT_FAILURE);
}
for (int i = 0; i < os->filled; i++) {
fprintf(fd, "%s\n", os->list[i]);
fprintf(fd, "%s\n", os->keys[i].pub);
}
fclose(fd);
if (os->priv_file == NULL) return;
fd = fopen(os->priv_file, "w");
if (fd == NULL) {
fprintf(stderr, "unable to open priv file %s for writing\n", os->priv_file);
exit(EXIT_FAILURE);
}
for (int i = 0; i < os->filled; i++) {
fprintf(fd, "%s %s\n", os->keys[i].pub, os->keys[i].priv);
}
fclose(fd);
}
int tor_os_append(struct tor_os_str* os, char* entry) {
char** target = tor_os_append_cursor (os);
int tor_os_append(struct tor_os_str* os, char* pub, char* priv) {
struct keypair* target = tor_os_append_cursor (os);
if (target == NULL) return -1;
*target = malloc(sizeof(char) * (strlen(entry) + 1));
if (*target == NULL) return -1;
strcpy (*target, entry);
target->pub = malloc(sizeof(char) * (strlen(pub) + 1));
if (target->pub == NULL) return -1;
strcpy (target->pub, pub);
if (os->priv_file == NULL) return 0;
target->priv = malloc(sizeof(char) * (strlen(priv) + 1));
if (target->priv == NULL) return -1;
strcpy (target->priv, priv);
return 0;
}

View File

@ -5,16 +5,22 @@
#include <fcntl.h>
#include <string.h>
struct keypair {
char* priv;
char* pub;
};
struct tor_os_str {
size_t filled;
size_t size;
char* file;
char** list;
char* pub_file;
char* priv_file;
struct keypair *keys;
};
void tor_os_create(struct tor_os_str* os, char* file, size_t size);
char** tor_os_append_cursor(struct tor_os_str* os);
int tor_os_append(struct tor_os_str* os, char* entry);
void tor_os_create(struct tor_os_str* os, char* pub_file, char* priv_file, size_t size);
struct keypair* tor_os_append_cursor(struct tor_os_str* os);
int tor_os_append(struct tor_os_str* os, char* pub, char* priv);
void tor_os_read (struct tor_os_str* os);
void tor_os_persist(struct tor_os_str* os);
void tor_os_free(struct tor_os_str* os);