diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4221727 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +# Generated directories +build/ +objects/ + +# Autosave files +*~ +*# diff --git a/Makefile b/Makefile index d5abfe3..a9ab6d4 100644 --- a/Makefile +++ b/Makefile @@ -39,8 +39,8 @@ SOURCES := $(wildcard $(SRCDIR)/*.c) OBJECTS := $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(SOURCES)) -CPPFLAGS := -I$(abspath $(INCDIR)) -MMD -MP -DUSE_LIBNOTIFY -CFLAGS := -Wall -Werror -Wpedantic $(shell pkg-config --cflags libnotify) +CPPFLAGS := -I$(abspath $(INCDIR)) -MMD -MP -DUSE_LIBNOTIFY -DUSE_SYSLOG +CFLAGS := -Wall -Werror -Wpedantic $(shell pkg-config --cflags libnotify) -ggdb LDFLAGS := #-Lmath or whatever LDLIBS := $(shell pkg-config --libs libnotify libcrypto) #-lm or whatever diff --git a/sources/hasher.c b/sources/hasher.c index 7210af5..1a4d315 100644 --- a/sources/hasher.c +++ b/sources/hasher.c @@ -47,13 +47,13 @@ int rounduptoblocksize(int a){ unsigned char * digest(const unsigned char *in, size_t inlen, unsigned int * outlen){ if(!EVP_DigestInit_ex(md_ctx, md, NULL)){ - log("Problem encountered while initializing digest"); + log(LOG_INFO, "Problem encountered while initializing digest"); ERR_print_errors_fp(stderr); return NULL; } if(!EVP_DigestUpdate(md_ctx, in, inlen)){ - log("Problem encountered while adding data to digest"); + log(LOG_INFO, "Problem encountered while adding data to digest"); ERR_print_errors_fp(stderr); return NULL; } @@ -61,7 +61,39 @@ unsigned char * digest(const unsigned char *in, size_t inlen, unsigned int * out unsigned char *out = OPENSSL_malloc(EVP_MD_get_size(md)); if(!EVP_DigestFinal_ex(md_ctx, out, outlen)){ - log("Problem encountered while retrieving digest"); + log(LOG_INFO, "Problem encountered while retrieving digest"); + ERR_print_errors_fp(stderr); + return NULL; + } + + return out; +} + +int begin_digest(){ + if(!EVP_DigestInit_ex(md_ctx, md, NULL)){ + log(LOG_INFO, "Problem encountered while initializing digest"); + ERR_print_errors_fp(stderr); + return 0; + } + return 1; +} + +int digest_part(const unsigned char *in, size_t inlen){ + + if(!EVP_DigestUpdate(md_ctx, in, inlen)){ + log(LOG_INFO, "Problem encountered while adding data to digest"); + ERR_print_errors_fp(stderr); + return 0; + } + return 1; +} + +unsigned char * get_digest(unsigned int * outlen){ + + unsigned char *out = OPENSSL_malloc(EVP_MD_get_size(md)); + + if(!EVP_DigestFinal_ex(md_ctx, out, outlen)){ + log(LOG_INFO, "Problem encountered while retrieving digest"); ERR_print_errors_fp(stderr); return NULL; } diff --git a/sources/include/hasher.h b/sources/include/hasher.h index cc23e6f..778a7e8 100644 --- a/sources/include/hasher.h +++ b/sources/include/hasher.h @@ -5,3 +5,7 @@ extern EVP_MD * md; int init_hasher(void); int deinit_hasher(void); unsigned char * digest(const unsigned char *in, size_t inlen, unsigned int * outlen); + +int begin_digest(); +int digest_part(const unsigned char *in, size_t inlen); +unsigned char * get_digest(unsigned int * outlen); diff --git a/sources/include/log.h b/sources/include/log.h index 17db208..017f1e3 100644 --- a/sources/include/log.h +++ b/sources/include/log.h @@ -1,6 +1,8 @@ #ifndef LDAP_LOG #define LDAP_LOG +extern int run_verbose; + const char * currtime(void); /* Great, I remember why I chose to use a macro. @@ -14,6 +16,15 @@ const char * currtime(void); Let's see whether I can split the log(X) macro into this and a function in log.h that supplies the time */ -#define log(X) printf("%s %s", currtime(), X) + +#ifdef USE_SYSLOG +#include +#define log(Y, ...) syslog(Y, __VA_ARGS__) +#endif +#ifndef USE_SYSLOG +#define log(Y, ...) printf("%s %s", currtime(), __VA_ARGS__) +#endif + +#define verb(...) if(run_verbose)log(LOG_DEBUG, __VA_ARGS__) #endif diff --git a/sources/include/main.h b/sources/include/main.h new file mode 100644 index 0000000..ab646be --- /dev/null +++ b/sources/include/main.h @@ -0,0 +1,20 @@ +/** + * Main.h: contains definitions for ldaphotos.c + */ + +#define VERSION "v0.0" +#define TITLE "LDAP Photo Album, by Catluck Kettlemerry" +#define PROGNAME "ldaphotos" +#define USAGE0 "Usage:" +#define USAGE1 "[OPTION...]" +#define OPTIONS0 "Options:" +#define OPTIONS1 "\t-v run verbosely" +#define OPTIONS2 "\t-h show help and exit" +#define OPTIONS3 "\t-t provide terse output" +#define OPTIONS4 "\t-f Read input from file " +#define OPTIONS5 "" +#define OPTIONS6 "" +#define OPTIONS7 "" +#define OPTIONS8 "" +#define OPTIONS9 "" +#define OPTIONS10 "\t-p Print the effects of the given options for this invocation" diff --git a/sources/main/ldaphotos.c b/sources/main/ldaphotos.c new file mode 100644 index 0000000..b0ddd77 --- /dev/null +++ b/sources/main/ldaphotos.c @@ -0,0 +1,164 @@ +#include // printf +#include // memcpy +#include // getopt + +#ifdef USE_SYSLOG +#include +#endif + +#include "hasher.h" +#include "log.h" +#include "main.h" + +#define PATHBUF 4097 +#define BUFLEN 2048 + +int run_verbose = 0; +int run_terse = 0; +int force_print_parameters = 0; + + +void print_help(void){ + printf("%s %s\n", TITLE, VERSION); + printf("%s %s %s\n", USAGE0, PROGNAME, USAGE1); + printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", + OPTIONS0, OPTIONS1, OPTIONS2, OPTIONS3, OPTIONS4, + OPTIONS5, OPTIONS6, OPTIONS7, OPTIONS8, OPTIONS9, + OPTIONS10); +} + +int main(int argc, char *const argv[]){ + +#ifdef USE_SYSLOG + openlog(PROGNAME, 0, LOG_USER); +#endif + + char *filepath = (char*)malloc(sizeof(char) * PATHBUF); + + int opt; + while((opt = getopt(argc, argv, "vhtpf:")) != -1) { + switch(opt){ + case 'v': + run_verbose = 1; + break; + case 'h': + print_help(); + return 0; + case 't': + run_terse = 1; + break; + case 'p': + force_print_parameters = 1; + break; + case 'f': + memcpy(filepath, optarg, strlen(optarg)); + break; + default: + printf("Literally how could you pass an unspecified argument to this program"); + } + } + + /** + Print run parameters at startup + */ + if(force_print_parameters) { + if(run_terse && run_verbose){ + printf(PROGNAME " called with arguments to run both tersely and verbosely, the author legitimately cannot tell what you want her to do\n"); + } else if(run_terse) { + printf(PROGNAME " will run tersely\n"); + } else if(run_verbose) { + printf(PROGNAME " will run verbosely\n"); + } + if(!run_terse){ + if(filepath != NULL){ + printf(PROGNAME " will read input from file %s\n", filepath); + } else { + printf(PROGNAME " will read input from standard input\n"); + } + } + + + } + + if(init_hasher()){ + log(LOG_INFO, "Error encountered while initializing hasher"); + } + + begin_digest(); + + /* + Set up input buffers + */ + // unsigned char * in stores the unsigned version of signin + unsigned char * in = (unsigned char*)malloc(sizeof(unsigned char) * BUFLEN); + char * signin = (char*)malloc(sizeof(char) * BUFLEN); + /* End set up buffers*/ + + /* + Set up the input stream + Try to open file pointed to by filepath, or else use stdin + */ + FILE * infile; + if(strlen(filepath) >= 0){ + log(LOG_DEBUG, "Trying to open file %s for input", filepath); + infile = fopen(filepath, "r"); + } + if(infile == NULL){ + infile = stdin; + } + /* End set up the input stream */ + + /* + Give blocks of data to the message digest algorithm. + */ + size_t bytesdigested = 0; + unsigned int round = 0; + while(!feof(infile) && !ferror(infile)){ + round++; + size_t bytesread = fread(signin, sizeof(char), sizeof(char) * BUFLEN, infile); + if(bytesread==0){ + printf("0 bytes read, breaking"); + break; + } + memcpy(in, signin, bytesread); + digest_part(in, bytesread); + bytesdigested += bytesread; + verb("Passed %lu bytes to digest algorithm in round %u, %lu bytes passed so far", bytesread, round, bytesdigested); + } + /* End give blocks of data to the message digest algorithm */ + + printf("Input:\n\t"); + for(int i = 0; i < bytesdigested; i++){ + printf("%02x ", in[i]); + } + printf("\n\t"); + for(int i = 0; i < bytesdigested; i++){ + printf(" %c ", in[i]); + } + printf("\n"); + + unsigned int len; + unsigned char * a = get_digest(&len); + char * aa = (char*)malloc(sizeof(char) * 2 * len); + printf("Output:\n\t"); + for(int i = 0; i < len; i++){ + sprintf(aa + (2 * i), "%02x", a[i]); + } + printf("%s\n", aa); + + OPENSSL_free(a); + free(aa); + if(deinit_hasher()){ + log(LOG_INFO, "Error encountered while deinitializing hasher"); + } + + log(LOG_INFO, "hi"); + +#ifdef USE_SYSLOG + closelog(); +#endif + if(infile != stdin){ + fclose(infile); + } + return 0; +} diff --git a/sources/main/main.c b/sources/main/main.c deleted file mode 100644 index aa8ec22..0000000 --- a/sources/main/main.c +++ /dev/null @@ -1,47 +0,0 @@ -#include -#include -#include "hasher.h" -#include "log.h" - -#define BUFLEN 2048 - -int main(void){ - log("hi\n"); - - if(init_hasher()){ - log("Error encountered while initializing hasher"); - } - - unsigned char * in = (unsigned char*)malloc(sizeof(unsigned char) * BUFLEN); - size_t inlen = 0; - char * signin = (char*)malloc(sizeof(char) * BUFLEN); - ssize_t bytesread = getline(&signin, &inlen, stdin); - memcpy(in, signin, bytesread); - - printf("Input:\n\t"); - for(int i = 0; i < bytesread; i++){ - printf("%02x ", in[i]); - } - printf("\n\t"); - for(int i = 0; i < bytesread; i++){ - printf(" %c ", in[i]); - } - printf("\n"); - - unsigned int len; - unsigned char * a = digest(in, bytesread, &len); - char * aa = (char*)malloc(sizeof(char) * 2 * len); - printf("Output:\n\t"); - for(int i = 0; i < len; i++){ - sprintf(aa + (2 * i), "%02x", a[i]); - } - printf("%s\n", aa); - - OPENSSL_free(a); - free(aa); - if(deinit_hasher()){ - log("Error encountered while deinitializing hasher"); - } - - return 0; -}