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.
This commit is contained in:
Sandy Mossgrave 2023-06-19 08:56:49 +00:00
parent 980784ea29
commit 39a69c947e
11 changed files with 276 additions and 67 deletions

8
.gitignore vendored
View File

@ -2,6 +2,14 @@
build/ build/
objects/ 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 # Autosave files
*~ *~
*# *#

View File

@ -17,8 +17,8 @@ INSTDIR := /usr/local/bin
REFACTOR := refactor.h REFACTOR := refactor.h
TESTCFLAGS:= $(pkg-config --cflags criterion) TESTCFLAGS:= $(shell pkg-config --cflags criterion)
TESTLIBS := $(pkg-config --libs criterion) TESTLIBS := $(shell pkg-config --libs criterion)
TESTDIR := $(SRCDIR)/test TESTDIR := $(SRCDIR)/test
TESTOD := $(OBJDIR)/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 CPPFLAGS := -I$(abspath $(INCDIR)) -MMD -MP -DUSE_LIBNOTIFY -DUSE_SYSLOG
CFLAGS := -Wall -Werror -Wpedantic $(shell pkg-config --cflags libnotify) -ggdb CFLAGS := -Wall -Werror -Wpedantic $(shell pkg-config --cflags libnotify) -ggdb
LDFLAGS := #-Lmath or whatever LDFLAGS := #-Lmath or whatever
LDLIBS := $(shell pkg-config --libs libnotify libcrypto) #-lm or whatever LDLIBS := $(shell pkg-config --libs libnotify libcrypto) #-lm or whatever

6
local.schema Normal file
View File

@ -0,0 +1,6 @@
attributeType ( 69.420 NAME ( '' '' )
DESC ''
EQUALITY caseIgnoreMatch
SUBSTR caseIgnoreSubstringsMatch
SYNTAX
)

79
manifesto.tex Normal file
View File

@ -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}

View File

@ -10,23 +10,23 @@ static int hasher_initialized = 0;
int init_hasher(void){ int init_hasher(void){
if(hasher_initialized){ if(hasher_initialized){
printf("hasher already initialized"); printf("hasher already initialized");
return 0; return 1;
} }
else{ else{
md = EVP_MD_fetch(NULL, "SHA256", NULL); md = EVP_MD_fetch(NULL, "SHA256", NULL);
if(md == NULL){ if(md == NULL){
ERR_print_errors_fp(stderr); ERR_print_errors_fp(stderr);
return 1; return 0;
} }
md_ctx = EVP_MD_CTX_new(); md_ctx = EVP_MD_CTX_new();
/*TODO: see whether EVP_MD_CTX_new() ever fails?*/ /*TODO: see whether EVP_MD_CTX_new() ever fails?*/
if(md_ctx == NULL){ if(md_ctx == NULL){
ERR_print_errors_fp(stderr); ERR_print_errors_fp(stderr);
return 1; return 0;
} }
} }
return 0; return 1;
} }
int deinit_hasher(void){ int deinit_hasher(void){

View File

@ -22,7 +22,7 @@ const char * currtime(void);
#define log(Y, ...) syslog(Y, __VA_ARGS__) #define log(Y, ...) syslog(Y, __VA_ARGS__)
#endif #endif
#ifndef USE_SYSLOG #ifndef USE_SYSLOG
#define log(Y, ...) printf("%s %s", currtime(), __VA_ARGS__) #define log(Y, ...) fprintf(stderr, "%s %s", currtime(), __VA_ARGS__)
#endif #endif
#define verb(...) if(run_verbose)log(LOG_DEBUG, __VA_ARGS__) #define verb(...) if(run_verbose)log(LOG_DEBUG, __VA_ARGS__)

View File

@ -2,6 +2,9 @@
* Main.h: contains definitions for ldaphotos.c * Main.h: contains definitions for ldaphotos.c
*/ */
#ifndef LDAPHOTOS
#define LDAPHOTOS
#define VERSION "v0.0" #define VERSION "v0.0"
#define TITLE "LDAP Photo Album, by Catluck Kettlemerry" #define TITLE "LDAP Photo Album, by Catluck Kettlemerry"
#define PROGNAME "ldaphotos" #define PROGNAME "ldaphotos"
@ -18,3 +21,14 @@
#define OPTIONS8 "" #define OPTIONS8 ""
#define OPTIONS9 "" #define OPTIONS9 ""
#define OPTIONS10 "\t-p Print the effects of the given options for this invocation" #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

View File

@ -0,0 +1,6 @@
#ifndef MANIPULATIONS
#define MANIPULATIONS
char * atohex(unsigned char * a, size_t len, unsigned int * aalen);
#endif

View File

@ -1,6 +1,9 @@
#include <stdio.h> // printf #include <stdio.h> // printf
#include <string.h> // memcpy #include <string.h> // memcpy
#include <unistd.h> // getopt #include <unistd.h> // getopt
#include <libgen.h> // dirname/basename
#include <limits.h> // realpath
#include <stdlib.h> // realpath
#ifdef USE_SYSLOG #ifdef USE_SYSLOG
#include <syslog.h> #include <syslog.h>
@ -9,6 +12,7 @@
#include "hasher.h" #include "hasher.h"
#include "log.h" #include "log.h"
#include "main.h" #include "main.h"
#include "manipulations.h"
#define PATHBUF 4097 #define PATHBUF 4097
#define BUFLEN 2048 #define BUFLEN 2048
@ -18,13 +22,73 @@ int run_terse = 0;
int force_print_parameters = 0; int force_print_parameters = 0;
void print_help(void){ FILE * get_outfile(FILE *infile, const char * filepath){
printf("%s %s\n", TITLE, VERSION); FILE* outfile;
printf("%s %s %s\n", USAGE0, PROGNAME, USAGE1); if(infile == stdin){
printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", outfile = stdout;
OPTIONS0, OPTIONS1, OPTIONS2, OPTIONS3, OPTIONS4, } else {
OPTIONS5, OPTIONS6, OPTIONS7, OPTIONS8, OPTIONS9, //Taken from man dirname(3)
OPTIONS10);
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[]){ int main(int argc, char *const argv[]){
@ -33,9 +97,9 @@ int main(int argc, char *const argv[]){
openlog(PROGNAME, 0, LOG_USER); openlog(PROGNAME, 0, LOG_USER);
#endif #endif
char *filepath = (char*)malloc(sizeof(char) * PATHBUF); char *filepath = (char*)malloc(PATHBUF * sizeof *filepath);
int opt; int opt;
while((opt = getopt(argc, argv, "vhtpf:")) != -1) { while((opt = getopt(argc, argv, "vhtpf:")) != -1) {
switch(opt){ switch(opt){
case 'v': case 'v':
@ -76,28 +140,18 @@ int main(int argc, char *const argv[]){
printf(PROGNAME " will read input from standard input\n"); printf(PROGNAME " will read input from standard input\n");
} }
} }
}
}
if(init_hasher()){ if(!init_hasher()){
log(LOG_INFO, "Error encountered while initializing hasher"); log(LOG_INFO, "Error encountered while initializing hasher");
} }
begin_digest(); 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 Set up the input stream
Try to open file pointed to by filepath, or else use stdin Try to open file pointed to by filepath, or else use stdin
*/ */
FILE * infile; FILE * infile;
if(strlen(filepath) >= 0){ if(strlen(filepath) >= 0){
log(LOG_DEBUG, "Trying to open file %s for input", filepath); 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 */ /* End set up the input stream */
/* size_t bytesdigested = digest_file(infile);
Give blocks of data to the message digest algorithm. (void)bytesdigested;
*/
size_t bytesdigested = 0; if(infile != stdin){
unsigned int round = 0; fclose(infile);
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 int len;
unsigned char * a = get_digest(&len); unsigned char * a = get_digest(&len);
char * aa = (char*)malloc(sizeof(char) * 2 * len); unsigned int aalen;
printf("Output:\n\t"); char *aa = atohex(a, len, &aalen);
for(int i = 0; i < len; i++){
sprintf(aa + (2 * i), "%02x", a[i]); 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); OPENSSL_free(a);
free(aa); free(aa);
if(deinit_hasher()){ if(deinit_hasher()){
log(LOG_INFO, "Error encountered while deinitializing hasher"); log(LOG_INFO, "Error encountered while deinitializing hasher");
} }
log(LOG_INFO, "hi");
#ifdef USE_SYSLOG #ifdef USE_SYSLOG
closelog(); closelog();
#endif #endif
if(infile != stdin){
fclose(infile);
}
return 0; return 0;
} }

15
sources/manipulations.c Normal file
View File

@ -0,0 +1,15 @@
#include <stdlib.h> // malloc
#include <stdio.h> // 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;
}

38
sources/test/test.c Normal file
View File

@ -0,0 +1,38 @@
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <criterion/criterion.h>
#include "main.h"
#include "manipulations.h"
#include "hasher.h"
#include "log.h"
#ifdef USE_LIBNOTIFY
#include <libnotify/notify.h>
#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);
}