1
0
Fork 0

Compare commits

...

3 Commits

2 changed files with 41 additions and 13 deletions

View File

@ -4,9 +4,6 @@
#include "crypt.h"
#include <netinet/in.h>
#define ADDRBOOK_NAME_MAXLEN 64
#define ADDRBOOK_PORT_MAXLEN 5
typedef struct addrbook_entry_t {
char pubk[CRYPT_PKEY_HEXLEN];
char *alias;

View File

@ -1,8 +1,8 @@
#include <bits/endian.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include "addrbook.h"
#include "audio.h"
@ -47,12 +47,12 @@ int main(int argc, const char **argv) {
struct options opts = {0};
addrbook_t ab = {0};
bool is_ab_changed = false;
net_t n = {0};
crypt_session_t s = {0};
crypt_key_t ok = {0}, rk = {0};
result = parse_argv(argc, argv, &opts);
switch (result) {
switch (parse_argv(argc, argv, &opts)) {
case -1: usage(); return 1;
case -2: version(); return 0;
case -3: usage(); return 0;
@ -88,6 +88,7 @@ int main(int argc, const char **argv) {
FILE *skey_file = fopen("tat_own_sec_key", "r");
if (pkey_file == NULL || skey_file == NULL) {
if ((result = crypt_key_gen(&ok)) == -1) {
fprintf(stderr, "Failed to generate the keys.\n");
result = 1;
goto cleanup;
}
@ -100,7 +101,15 @@ int main(int argc, const char **argv) {
pkey_file = fopen("tat_own_pub_key", "w+");
skey_file = fopen("tat_own_sec_key", "w+");
crypt_store_key(&ok, pkey_file, skey_file);
if (pkey_file == NULL || skey_file == NULL) {
fprintf(stderr, "Cannot open tat_own_*_key files to write: %s\n", strerror(errno));
return 1;
}
if (crypt_store_key(&ok, pkey_file, skey_file) == -1) {
fprintf(stderr, "Failed to save the keys.\n");
return 1;
}
fclose(pkey_file);
fclose(skey_file);
@ -108,7 +117,7 @@ int main(int argc, const char **argv) {
printf("The new cryptographical keys was written.\n");
} else {
if (crypt_load_key(&ok, pkey_file, skey_file) == -1) {
fprintf(stderr, "Cannot load the keys.");
fprintf(stderr, "Cannot load the keys.\n");
result = 1;
goto cleanup;
}
@ -161,6 +170,15 @@ int main(int argc, const char **argv) {
cleanup:
if (is_ab_changed) {
addrbook_file = fopen((opts.addressbook_path == NULL) ? "addressbook" : opts.addressbook_path, "w+");
if (addrbook_file != NULL) {
if (addrbook_store(&ab, addrbook_file) == -1)
fprintf(stderr, "Failed to store an addressbook: %s\n", strerror(errno));
fclose(addrbook_file);
}
}
addrbook_destroy(&ab);
audio_destroy(&aud);
net_destroy(&n);
@ -168,8 +186,8 @@ cleanup:
crypt_key_destroy(&ok);
crypt_key_destroy(&rk);
if (audio_terminate_soundsystem() == -1)
return 1;
// if (audio_terminate_soundsystem() == -1)
// return 1;
return result;
}
@ -267,9 +285,22 @@ int parse_argv(int argc, const char **argv, struct options *opts) {
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
if (argv[i][1] == '-') {
if (strncmp(&argv[i][2], "keys", 4) == 0) {
opts->key_path = argv[i+1];
++i;
} else if (strncmp(&argv[i][2], "addressbook", 11) == 0) {
opts->addressbook_path = argv[i+1];
++i;
} else {
return -1;
}
continue;
}
switch (argv[i][1]) {
case 'k': opts->key_path = argv[i+1]; ++i; continue;
case 'a': opts->addressbook_path = argv[i+1]; ++i; continue;
case 'K': opts->key_path = argv[i+1]; ++i; continue;
case 'A': opts->addressbook_path = argv[i+1]; ++i; continue;
case 'v': return -2;
case 'h': return -3;
default: return -1;