From 39a69c947e818058f14695c7f00e6440597f6eb4 Mon Sep 17 00:00:00 2001 From: Sandy Mossgrave Date: Mon, 19 Jun 2023 08:56:49 +0000 Subject: [PATCH] Quicksaving before first run on coldspringsharbor. Added manipulations.c, which turns an array of bytes into their hex representation. Added a completely useless test suite. Added manifesto.tex, which is, well, a dramatic name for my notes to self and plan. Added local.schema, which will be used by LDAP later on when actually registering the photos in a database. --- .gitignore | 8 ++ Makefile | 5 +- local.schema | 6 ++ manifesto.tex | 79 ++++++++++++++++ sources/hasher.c | 8 +- sources/include/log.h | 2 +- sources/include/main.h | 14 +++ sources/include/manipulations.h | 6 ++ sources/main/ldaphotos.c | 162 ++++++++++++++++++++------------ sources/manipulations.c | 15 +++ sources/test/test.c | 38 ++++++++ 11 files changed, 276 insertions(+), 67 deletions(-) create mode 100644 local.schema create mode 100644 manifesto.tex create mode 100644 sources/include/manipulations.h create mode 100644 sources/manipulations.c create mode 100644 sources/test/test.c diff --git a/.gitignore b/.gitignore index 4221727..8022539 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,14 @@ build/ objects/ +# Aux files generated by ldaphotos executable +*.aux + +# Aux and log files generated by LaTeX when compiling manifesto.tex +manifesto.aux +manifesto.pdf +manifesto.log + # Autosave files *~ *# diff --git a/Makefile b/Makefile index a9ab6d4..f16bdc4 100644 --- a/Makefile +++ b/Makefile @@ -17,8 +17,8 @@ INSTDIR := /usr/local/bin REFACTOR := refactor.h -TESTCFLAGS:= $(pkg-config --cflags criterion) -TESTLIBS := $(pkg-config --libs criterion) +TESTCFLAGS:= $(shell pkg-config --cflags criterion) +TESTLIBS := $(shell pkg-config --libs criterion) TESTDIR := $(SRCDIR)/test TESTOD := $(OBJDIR)/test @@ -41,6 +41,7 @@ OBJECTS := $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(SOURCES)) 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/local.schema b/local.schema new file mode 100644 index 0000000..081f8a3 --- /dev/null +++ b/local.schema @@ -0,0 +1,6 @@ +attributeType ( 69.420 NAME ( '' '' ) + DESC '' + EQUALITY caseIgnoreMatch + SUBSTR caseIgnoreSubstringsMatch + SYNTAX +) \ No newline at end of file diff --git a/manifesto.tex b/manifesto.tex new file mode 100644 index 0000000..948e728 --- /dev/null +++ b/manifesto.tex @@ -0,0 +1,79 @@ +\documentclass[12pt]{article} + +\def\thetitle{LDAPhotos Manifesto} +\def\myname{Sandy Mossgrave} +\def\theprof{LDAP} +\def\theclass{LDAPhotos} +\title{\thetitle} +\author{\myname} +\def\shorttitle{\thetitle} + +\usepackage{geometry} +\geometry{letterpaper, portrait, margin=1in} + +% Package imports! +\usepackage{hyperref} +\usepackage{fancyhdr} +\usepackage{lastpage} +\usepackage{graphicx} +\usepackage[utf8]{inputenc} +\usepackage{twemojis} +\usepackage{cancel} +\usepackage{tikz} +\usepackage{amsmath} +\usepackage{nicefrac} + +\graphicspath{ {./images} } + +\setlength{\parskip}{1ex} +\setlength{\parindent}{0pt} +\setlength{\jot}{8pt} % Nicer spacing for the lines in an amsmath split environment + +\iffalse +\url{windy.com} +\fi + +% Unnumbered Equation Command - Mimics the \begin{equation} environment without a number +\newcommand{\unneqn}[1]{\center{\Large{$#1$}}} + +\renewcommand{\headrulewidth}{0pt} + +\fancypagestyle{firstpagestyle} + { + \fancyhf{} + \renewcommand{\headrulewidth}{0pt} + \fancyhead[RO,RE]{\thepage/\pageref*{LastPage} \myname \\ \today} + \fancyhead[LO,LE]{\shorttitle} + \fancyfoot[]{} + } + +\fancyhead[RO,RE]{\thepage/\pageref*{LastPage} \myname\\ \today} +\fancyhead[LO,LE]{\shorttitle} +\fancyfoot[]{} + +\begin{document} +\pagestyle{fancy} +\thispagestyle{firstpagestyle} +\begin{center} + \Large{\textbf{ \thetitle }} +\end{center} + +\section*{To-Do List} + +\begin{itemize} +\item Obtain object identifier +\item Choose name prefix +\item create local schema file +\item define custom attribute types +\item define custom object classes +\end{itemize} + +\newcommand\localschema{\textit{local.schema}} + +\begin{itemize} +\item Add a \localschema into \textit{/usr/local/etc/openldap/schema/} +\item In \textit{slapd.conf} add an include directive for the above \localschema +\item +\end{itemize} + +\end{document} diff --git a/sources/hasher.c b/sources/hasher.c index 1a4d315..a50109d 100644 --- a/sources/hasher.c +++ b/sources/hasher.c @@ -10,23 +10,23 @@ static int hasher_initialized = 0; int init_hasher(void){ if(hasher_initialized){ printf("hasher already initialized"); - return 0; + return 1; } else{ md = EVP_MD_fetch(NULL, "SHA256", NULL); if(md == NULL){ ERR_print_errors_fp(stderr); - return 1; + return 0; } md_ctx = EVP_MD_CTX_new(); /*TODO: see whether EVP_MD_CTX_new() ever fails?*/ if(md_ctx == NULL){ ERR_print_errors_fp(stderr); - return 1; + return 0; } } - return 0; + return 1; } int deinit_hasher(void){ diff --git a/sources/include/log.h b/sources/include/log.h index 017f1e3..ba9adc8 100644 --- a/sources/include/log.h +++ b/sources/include/log.h @@ -22,7 +22,7 @@ const char * currtime(void); #define log(Y, ...) syslog(Y, __VA_ARGS__) #endif #ifndef USE_SYSLOG -#define log(Y, ...) printf("%s %s", currtime(), __VA_ARGS__) +#define log(Y, ...) fprintf(stderr, "%s %s", currtime(), __VA_ARGS__) #endif #define verb(...) if(run_verbose)log(LOG_DEBUG, __VA_ARGS__) diff --git a/sources/include/main.h b/sources/include/main.h index ab646be..c8b8b08 100644 --- a/sources/include/main.h +++ b/sources/include/main.h @@ -2,6 +2,9 @@ * Main.h: contains definitions for ldaphotos.c */ +#ifndef LDAPHOTOS +#define LDAPHOTOS + #define VERSION "v0.0" #define TITLE "LDAP Photo Album, by Catluck Kettlemerry" #define PROGNAME "ldaphotos" @@ -18,3 +21,14 @@ #define OPTIONS8 "" #define OPTIONS9 "" #define OPTIONS10 "\t-p Print the effects of the given options for this invocation" + +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); +} + +#endif diff --git a/sources/include/manipulations.h b/sources/include/manipulations.h new file mode 100644 index 0000000..d7381e3 --- /dev/null +++ b/sources/include/manipulations.h @@ -0,0 +1,6 @@ +#ifndef MANIPULATIONS +#define MANIPULATIONS + +char * atohex(unsigned char * a, size_t len, unsigned int * aalen); + +#endif diff --git a/sources/main/ldaphotos.c b/sources/main/ldaphotos.c index b0ddd77..8449523 100644 --- a/sources/main/ldaphotos.c +++ b/sources/main/ldaphotos.c @@ -1,6 +1,9 @@ #include // printf #include // memcpy #include // getopt +#include // dirname/basename +#include // realpath +#include // realpath #ifdef USE_SYSLOG #include @@ -9,6 +12,7 @@ #include "hasher.h" #include "log.h" #include "main.h" +#include "manipulations.h" #define PATHBUF 4097 #define BUFLEN 2048 @@ -18,13 +22,73 @@ 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); +FILE * get_outfile(FILE *infile, const char * filepath){ + FILE* outfile; + if(infile == stdin){ + outfile = stdout; + } else { + //Taken from man dirname(3) + + const char * extension = ".aux"; + size_t extlen = sizeof(extension); + char * filepath2 = realpath(filepath, NULL); + if(run_verbose){ + printf("Realpath for infile is %s\n", filepath2); + } + size_t outpathlen = strlen(filepath2) + 3 + extlen; + char * outpath = (char*)malloc(outpathlen); + char *dirc = strdup(filepath2); + char *basec = strdup(filepath2); + char *dir = dirname(dirc); + char *base = basename(basec); + int pathlen; + snprintf(outpath, outpathlen, "%s/.%s%s", dir, base, extension); + free(dirc); + free(basec); + free(filepath2); + if(run_verbose){ + printf("Committing hash to auxiliary file %s\n", outpath); + } + (void)pathlen; + outfile = fopen(outpath, "w"); + } + return outfile; +} + +/* + Give blocks of data to the message digest algorithm. +*/ +size_t digest_file(FILE * infile){ + + /* + Set up input buffers + */ + // unsigned char * in stores the unsigned version of signin + unsigned char * in = (unsigned char*)malloc(BUFLEN * sizeof *in); + /* End set up buffers*/ + + size_t bytesdigested = 0; + unsigned int round = 0; + while(!feof(infile) && !ferror(infile)){ + round++; + size_t bytesread = fread((unsigned char*)in, sizeof(char), sizeof(char) * BUFLEN, infile); + if(bytesread==0){ + verb("0 bytes read, breaking"); + break; + } + digest_part(in, bytesread); + bytesdigested += bytesread; + verb("Passed %lu bytes to digest algorithm in round %u, %lu bytes passed so far", bytesread, round, bytesdigested); + } + if(run_verbose){ + printf("Input:\n\t"); + for(int i = 0; i < bytesdigested; i++){ + printf("%02x ", in[i]); + } + printf("\n"); + } + free(in); + return bytesdigested; } int main(int argc, char *const argv[]){ @@ -33,9 +97,9 @@ int main(int argc, char *const argv[]){ openlog(PROGNAME, 0, LOG_USER); #endif - char *filepath = (char*)malloc(sizeof(char) * PATHBUF); - - int opt; + char *filepath = (char*)malloc(PATHBUF * sizeof *filepath); + +int opt; while((opt = getopt(argc, argv, "vhtpf:")) != -1) { switch(opt){ case 'v': @@ -76,28 +140,18 @@ int main(int argc, char *const argv[]){ printf(PROGNAME " will read input from standard input\n"); } } - - - } + } - if(init_hasher()){ + 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); @@ -108,57 +162,45 @@ int main(int argc, char *const argv[]){ } /* 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); + size_t bytesdigested = digest_file(infile); + (void)bytesdigested; + + if(infile != stdin){ + fclose(infile); } - /* 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]); + unsigned int aalen; + char *aa = atohex(a, len, &aalen); + + FILE * outfile = get_outfile(infile, filepath); + /* + Commit hash to auxiliary file + */ + if(run_verbose){ + printf("Output:\n\t"); } - printf("%s\n", aa); + if(run_verbose && outfile != stdout){ + fwrite(aa, sizeof(*aa), aalen, stdout); + } + size_t bytes_written = 0; + size_t bytes_round; + while(bytes_written < aalen && !ferror(outfile)){ + bytes_round = fwrite(aa + bytes_written, sizeof(*aa), aalen - bytes_written, outfile); + bytes_written += bytes_round; + verb("Round wrote %lu bytes\n", bytes_round); + } + /* End commit hash to auxiliary file*/ 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/manipulations.c b/sources/manipulations.c new file mode 100644 index 0000000..9a4f538 --- /dev/null +++ b/sources/manipulations.c @@ -0,0 +1,15 @@ +#include // malloc +#include // sprintf + +char * atohex(unsigned char * a, size_t len, unsigned int * aalen){ + char *aa; + //two bytes of output per byte of input, plus newline + (*aalen) = 2 * len * sizeof *aa + 1; + aa = (char*)malloc((*aalen)); + + for(int i = 0; i < len; i++){ + sprintf(aa + (2 * i), "%02x", a[i]); + } + aa[(*aalen)-1] = '\n'; + return aa; +} diff --git a/sources/test/test.c b/sources/test/test.c new file mode 100644 index 0000000..16f87a5 --- /dev/null +++ b/sources/test/test.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +#include + +#include "main.h" +#include "manipulations.h" +#include "hasher.h" +#include "log.h" + +#ifdef USE_LIBNOTIFY +#include +#endif + +Test(misc, passing){ + cr_assert(1, "ya boi"); +} + +Test(misc, hashsomething){ + + if(!init_hasher()){ + log(LOG_INFO, "Error encountered while initializing hasher"); + } + + begin_digest(); + + + unsigned int len; + unsigned int aalen; + unsigned char * a = get_digest(&len); + char *aa = atohex(a, len, &aalen); + + + + free(aa); +}