diff options
author | Aaron Ball <nullspoon@oper.io> | 2018-04-14 14:23:46 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@oper.io> | 2018-04-14 14:23:46 -0600 |
commit | ac0e400b2d7cdd7ecec2c9bed47fa0a4c49c5dae (patch) | |
tree | e4522996d81d79df3a27a283141af0adecd48b20 | |
parent | e1e47e71c325f4283fa5befe094757aca4d1f1dc (diff) | |
download | gpgsecure-ac0e400b2d7cdd7ecec2c9bed47fa0a4c49c5dae.tar.gz gpgsecure-ac0e400b2d7cdd7ecec2c9bed47fa0a4c49c5dae.tar.xz |
Initial commit
No encryption is supported yet. Only supports archive decompression and
extraction and enumeration.
-rw-r--r-- | c/Makefile | 12 | ||||
-rw-r--r-- | c/src/encarchive.c | 102 | ||||
-rw-r--r-- | c/src/encarchive.h | 44 | ||||
-rw-r--r-- | c/src/main.c | 50 |
4 files changed, 208 insertions, 0 deletions
diff --git a/c/Makefile b/c/Makefile new file mode 100644 index 0000000..9938411 --- /dev/null +++ b/c/Makefile @@ -0,0 +1,12 @@ +out = gpgsecure +cc = cc + +std = --std=c99 +warnings = -Wall -Wpedantic +ccopts = $(std) $(warnings) +larchive = $(shell pkg-config --libs libarchive) + +all: + if [ ! -d obj ]; then mkdir obj; fi + $(cc) $(ccopts) $(larchive) -c src/encarchive.c -o obj/encarchive.o + $(cc) $(ccopts) $(larchive) src/main.c obj/*.o -o $(out) diff --git a/c/src/encarchive.c b/c/src/encarchive.c new file mode 100644 index 0000000..45e840f --- /dev/null +++ b/c/src/encarchive.c @@ -0,0 +1,102 @@ +/** + * GPGSecure manages GPG encrypted archives + * Copyright (C) 2018 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "encarchive.h" + +int cd_open(struct archive *a, void *client_data) { + struct clientdata *cd = client_data; + cd->fd = open(cd->name, O_RDONLY); + return (cd->fd >= 0 ? ARCHIVE_OK : ARCHIVE_FATAL); +} + +la_ssize_t cd_read(struct archive *a, void *client_data, const void **buff) { + struct clientdata *cd = client_data; + *buff = cd->buff; + return (read(cd->fd, cd->buff, 10240)); +} + +int cd_close(struct archive *a, void *client_data) { + struct clientdata *cd = client_data; + + if(cd->fd > 0) + close(cd->fd); + return(ARCHIVE_OK); +} + +int enc_archive_enum(char* path) { + struct clientdata *cd; + struct archive *a; + struct archive_entry *entry; + + cd = malloc(sizeof(struct clientdata)); + a = archive_read_new(); + cd->name = path; + + // Add read support options + int r; + r = archive_read_support_filter_all(a); + if(r > 0) + return r; + r = archive_read_support_format_all(a); + if(r > 0) + return r; + + // Open the archive + archive_read_open(a, cd, cd_open, cd_read, cd_close); + while(archive_read_next_header(a, &entry) == ARCHIVE_OK) { + // Print archive filename + printf("%s\n", archive_entry_pathname(entry)); + archive_read_data_skip(a); + } + + archive_read_free(a); + free(cd); + return 0; +} + + +int enc_archive_extract(char* srcarchive, char* dest) { + struct clientdata *cd; + struct archive *a; + struct archive_entry *entry; + + cd = malloc(sizeof(struct clientdata)); + a = archive_read_new(); + cd->name = srcarchive; + + // Add read support options + int r; + r = archive_read_support_filter_all(a); + if(r > 0) + return r; + r = archive_read_support_format_all(a); + if(r > 0) + return r; + + // Open the archive + archive_read_open(a, cd, cd_open, cd_read, cd_close); + while(archive_read_next_header(a, &entry) == ARCHIVE_OK) { + archive_read_extract(a, entry, ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS); + archive_read_data_skip(a); + // Print archive filename + printf("extracting %s\n", archive_entry_pathname(entry)); + } + + archive_read_free(a); + free(cd); + return 0; +} diff --git a/c/src/encarchive.h b/c/src/encarchive.h new file mode 100644 index 0000000..1649e07 --- /dev/null +++ b/c/src/encarchive.h @@ -0,0 +1,44 @@ +/** + * GPGSecure manages GPG encrypted archives + * Copyright (C) 2018 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include <stdio.h> +#include <stdlib.h> + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + +#include <archive.h> +// TODO: man 3 archive_read example only includes <archive.h>, which won't +// build due to missing archive_entry.h. +#include <archive_entry.h> + +struct clientdata { + char* name; + int fd; + char buff[10240]; +}; + +/** + * TODO + */ +int enc_archive_enum(char*); + +/** + * TODO + */ +int enc_archive_extract(char*, char*); diff --git a/c/src/main.c b/c/src/main.c new file mode 100644 index 0000000..1c2aa16 --- /dev/null +++ b/c/src/main.c @@ -0,0 +1,50 @@ +/** + * GPGSecure manages GPG encrypted archives + * Copyright (C) 2018 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "encarchive.h" + +int main(int argc, char* argv[]) { + char* action; // Action to perform on the archive + char* archive; // Path to the archive to open. + char* tmp; // Temp (memory) path to extract into + + if(argc == 1) { + printf("Archive action required (enum, extract)\n"); + return 1; + } + + if(argc == 2) { + printf("Path to archive required\n"); + return 1; + } + action = argv[1]; + archive = argv[2]; + tmp = "/tmp/gpgsecure/"; + + if(strcmp(action, "enum") == 0) { + return enc_archive_enum(archive); + } else if(strcmp(action, "extract") == 0) { + return enc_archive_extract(archive, tmp); + } + + printf("Unknown action \"%s\"\n", action); + return 1; +} |