Merge pull request 'v0.2.0/implement_integrity_checking' (#14) from v0.2.0/implement_integrity_checking into main

Reviewed-on: #14
This commit is contained in:
Liam Waldron 2025-03-22 13:00:58 -04:00
commit 1c52dab7b0
12 changed files with 5913 additions and 442 deletions

View File

@ -5,6 +5,7 @@
IDIR = ./include IDIR = ./include
BDIR = ./build BDIR = ./build
SDIR = ./src SDIR = ./src
TDIR = ./tests
include config.mk include config.mk
@ -34,6 +35,10 @@ lib: $(SDIR)/libglacier.c
$(CC) $(SDIR)/libglacier.c -c $(LIBFLAGS) -o $(BDIR)/lib/libglacier.o $(CC) $(SDIR)/libglacier.c -c $(LIBFLAGS) -o $(BDIR)/lib/libglacier.o
$(AR) -rc $(BDIR)/lib/libglacier.a build/lib/libglacier.o $(AR) -rc $(BDIR)/lib/libglacier.a build/lib/libglacier.o
check: lib $(BDIR) $(TDIR)/unit-tests.c
$(CC) tests/unit-tests.c -o tests/test-suite -lcunit -Wall -Wextra build/lib/libglacier.a $(LIBFLAGS)
tests/test-suite
install_lib: $(BDIR)/lib/libglacier.a install_lib: $(BDIR)/lib/libglacier.a
@echo "[INFO]" @echo "[INFO]"
@echo "[INFO] Installing library to PREFIX/lib..." @echo "[INFO] Installing library to PREFIX/lib..."
@ -58,4 +63,7 @@ install: install_lib install_head
@echo "[INFO]" @echo "[INFO]"
clean: clean:
rm -rf $(BDIR) rm -rf $(BDIR) ./chroot
distclean:
rm -rf $(BDIR) $(TDIR)/test-suite ./chroot

View File

@ -1 +1 @@
0.1.0-rc 0.2.0-rc

5233
chroot-utils/SAVED_CONFIG Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,29 @@
#!/bin/bash
#
# build_in_chroot.sh - Build libglacier and install it in a simple chroot
#
# This file is part of Glacier.
#
# Glacier 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.
#
# Glacier 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 Glacier. If
# not, see <https://www.gnu.org/licenses/>.
#
init_chroot() {
mkdir -v ./chroot
cd ./chroot
git clone https://gitlab.com/buildroot.org/buildroot.git
cd buildroot
cp ../../SAVED_CONFIG ./.config
make -j$(nproc)
}
init_chroot "$@"

View File

@ -5,6 +5,6 @@
CC = /bin/gcc CC = /bin/gcc
AR = /bin/ar AR = /bin/ar
LIBFLAGS = -lconfig LIBFLAGS = -lconfig -lcrypto
PREFIX = /usr PREFIX = /usr

View File

@ -1,5 +1,5 @@
/* /*
* config.h - Function declarations for libglacier * config.h - Config loading for Glacier
* *
* This file is part of Glacier. * This file is part of Glacier.
* *
@ -18,83 +18,75 @@
#ifndef GLACIERCONFIG_H_ #ifndef GLACIERCONFIG_H_
#define GLACIERCONFIG_H_ #define GLACIERCONFIG_H_
#include <libconfig.h>
/* Constants */
#ifndef LG_VERBOSE
#define LG_VERBOSE 0
#endif
/* /*
* init_config * init_config
* DESCRIPTION: Init_config initializes the libconfig library, so it can read the required runtime files
* PARAMETERS:
* None. (void)
* RETURN VAUES:
* 0 on success, EXIT_FAILURE on failure
* CAVEATS:
* None.
* EXAMPLE:
* // It is best practice to check for ALL non-zero return values, rather than specific ones,
* // as init_config() returns EXIT_FAILURE
* *
* if (init_config() != 0) { * DESCRIPTION: Initialize libconfig with required configs
* errlog("Failed to initialize libconfig"); * PARAMETERS:
* return(EXIT_FAILURE); // fatal error requiring termination of program execution * None.
* } * RETURN VALUES:
* else { * 0 on success, 1 on failure
* successlog("Initialized libconfig"); // output automatically if LG_VERBOSE = 1 * CAVEATS:
* } * This MUST be called before ANY other config function is.
* EXAMPLE:
* init_config();
*/ */
int init_config(void); int init_config(void);
/**************************************************************************************************************/
/* /*
* die_config * die_config
* DESCRPTION: Die_config destroys the loaded libconfig library.
* PARAMETERS:
* None. (void)
* RETURN VALUES:
* EXIT_SUCCESS on success
* CAVEATS:
* None.
* EXAMPLE:
* // die_config() is unlikely to fail unless you tried to destroy an invalid object,
* // so checking for non-zero return values is unnecessary
* *
* DESCRIPTION: Die_config brings down libconfig gracefully.
* PARAMETERS:
* None.
* RETURN VALUES:
* 0 on success, 1 on failure
* CAVEATS:
* This MUST be called after ALL other config functions have completed.
* EXAMPLE:
* die_config(); * die_config();
*/ */
int die_config(void); int die_config(void);
/**************************************************************************************************************/
/* /*
* load_all_from_config * load_all_from_config
* DESCRIPTION: Initialize all settings from glacier.cfg. *
* DESCRIPTION: load_all_from_config loads all settings from the config file.
* PARAMETERS: * PARAMETERS:
* None. (void) * None.
* RETURN VALUES: * RETURN VALUES:
* 0 on success, 1 on file does not exist, 2 on library error * 0 on success, 1 on failure
* CAVEATS: * CAVEATS:
* None. * None.
* EXAMPLE: * EXAMPLE:
* load_all_from_config(); * load_all_from_config();
*/ */
int load_all_from_config(); int load_all_from_config(void);
/**************************************************************************************************************/
/* /*
* [[[ DEPRECATED ]]] * load_all_from_profile
* load_setting_from_config *
* DESCRIPTION: Initialize a specified from glacier.cfg. * DESCRIPTION: load_all_from_profile loads all settings from the profile file.
* PARAMETERS: * PARAMETERS:
* char SETTING[] -> The setting to initialize * None.
* RETURN VALUES: * RETURN VALUES:
* 0 on success, 1 on setting not found, 2 on file does not exist, 3 on library error * 0 on success, 1 on failure
* CAVEATS: * CAVEATS:
* None. * None.
* EXAMPLE: * EXAMPLE:
* load_setting_from_config(); * load_all_from_profile();
*/ */
/* int load_setting_from_config(char SETTING[]); */ int load_all_from_profile(void);
#endif #endif

View File

@ -20,6 +20,21 @@
#include <stdbool.h> #include <stdbool.h>
/* Maximum number of children a node can have */
#define MAX_CHILDREN 64
/* Maximum recursion depth for tree operations */
#define MAX_RECURSION_DEPTH 100
/* Node structure definition */
struct node {
char *data;
struct node *children[MAX_CHILDREN];
int numChildren;
struct node *left;
struct node *right;
};
/* /*
* create_node * create_node
* *
@ -27,16 +42,30 @@
* PARAMETERS: * PARAMETERS:
* char *data -> The name of the node to create * char *data -> The name of the node to create
* RETURN VALUES: * RETURN VALUES:
* None. * A pointer to the created node on success, NULL on failure
* CAVEATS: * CAVEATS:
* None. * Caller must free the node using free_node when done
* EXAMPLE: * EXAMPLE:
* struct node package = create_node("Package"); * struct node *package = create_node("Package");
*/ */
struct node *create_node(char *data); struct node *create_node(char *data);
/**************************************************************************************************************/ /*
* free_node
*
* DESCRIPTION: Free_node recursively frees all memory allocated for a node and its children.
* PARAMETERS:
* struct node *root -> The root node to free
* RETURN VALUES:
* None.
* CAVEATS:
* Will free all child nodes recursively.
* EXAMPLE:
* free_node(package);
*/
void free_node(struct node *root);
/* /*
* add_child * add_child
@ -46,16 +75,14 @@ struct node *create_node(char *data);
* struct node *parent -> The parent node which the child will be added to * struct node *parent -> The parent node which the child will be added to
* struct node *child -> The child node which will be added to the parent node * struct node *child -> The child node which will be added to the parent node
* RETURN VALUES: * RETURN VALUES:
* 1 on maximum children exceeded * 0 on success, 1-3 for different error conditions
* CAVEATS: * CAVEATS:
* None. * None.
* EXAMPLE: * EXAMPLE:
* add_child(package, dep1); * add_child(package, dep1);
*/ */
void add_child(struct node *parent, struct node *child); int add_child(struct node *parent, struct node *child);
/**************************************************************************************************************/
/* /*
* print_tree * print_tree
@ -65,164 +92,13 @@ void add_child(struct node *parent, struct node *child);
* struct node *root -> The tree to print * struct node *root -> The tree to print
* int level -> The number of levels to descend * int level -> The number of levels to descend
* RETURN VALUES: * RETURN VALUES:
* None. * 0 on success, non-zero on error
* CAVEATS: * CAVEATS:
* None. * None.
* EXAMPLE: * EXAMPLE:
* print_tree(package, 0); * print_tree(package, 0);
*/ */
void print_tree(struct node *root, int level); int print_tree(struct node *root, int level);
/**************************************************************************************************************/
/*
* queue
*
* DESCRIPTION: Queue is a type definition for the queue data structure.
*/
typedef struct queue;
/**************************************************************************************************************/
/*
* init_queue
*
* DESCRIPTION: Init_queue initializes a queue data structue.
* PARAMETERS:
* queue *q -> The name of the queue to initialize
* RETURN VALUES:
* None.
* CAVEATS:
* None.
* EXAMPLE:
* init_queue("operation_queue");
*/
void init_queue(queue *q);
/**************************************************************************************************************/
/*
* queue_is_empty
*
* DESCRIPTION: Queue_is_empty checks if queue *q is empty.
* PARAMETERS:
* queue *q -> The queue to check
* RETURN VALUES:
* true on empty, false on not empty
* CAVEATS:
* None.
* EXAMPLE:
* if (queue_is_empty(q)) {
* printf("Queue is empty\n");
* return;
* }
*/
bool queue_is_empty(queue *q);
/**************************************************************************************************************/
/*
* queue_is_full
*
* DESCRIPTION: Queue_is_full checks if queue *q is full.
* PARAMETERS:
* queue *q -> The queue to check
* RETURN VALUES:
* true on full, false on not full
* CAVEATS:
* None.
* EXAMPLE:
* if (queue_is_full(q)) {
* printf("Queue is full\n");
* return;
* }
*/
bool queue_is_full(queue *q);
/**************************************************************************************************************/
/*
* enqueue
*
* DESCRIPTION: Enqueue enqueues an element at the back of the queue.
* PARAMETERS:
* queue *q -> The queue to enqueue to
* int value -> The value of the element to enqueue
* RETURN VALUES:
* None.
* CAVEATS:
* None.
* EXAMPLE:
* if (! queue_is_full(q)) {
* enqueue(q, 15);
* }
*/
void enqueue(queue *q, int value);
/**************************************************************************************************************/
/*
* dequeue
*
* DESCRIPTION: Dequeue dequeues an element at the front of the queue.
* PARAMETERS:
* queue *q -> The queue to dequeue to
* RETURN VALUES:
* None.
* CAVEATS:
* None.
* EXAMPLE:
* if (! queue_is_empty(q)) {
* dequeue(q);
* }
*/
void dequeue(queue *q);
/**************************************************************************************************************/
/*
* peek
*
* DESCRIPTION: Peek displays the first element in a specified queue.
* PARAMETERS:
* queue *q -> The queue to peek at
* RETURN VALUES:
* -1 on queue is empty, element value (!= -1) on success
* CAVEATS:
* None.
* EXAMPLE:
* if (! queue_is_full(q)) {
* peek(q);
* }
*/
int peek(queue *q);
/**************************************************************************************************************/
/*
* print_queue
*
* DESCRIPTION: Print_queue displays the contents of the queue in order.
* PARAMETERS:
* queue *q -> The queue to print
* RETURN VALUES:
* None.
* CAVEATS:
* None.
* EXAMPLE:
* if (! queue_is_full(q)) {
* print_queue(q);
* }
*/
void print_queue(queue *q);
#endif #endif

View File

@ -18,22 +18,34 @@
#ifndef GLOBALS_H_ #ifndef GLOBALS_H_
#define GLOBALS_H_ #define GLOBALS_H_
#include <libconfig.h>
#include <stdio.h>
/* Constants */
#define BUFFER_SIZE 1024
#define MAX_SIZE 256
/* libconfig context */
extern config_t cfg; extern config_t cfg;
extern config_setting_t *setting; extern config_setting_t *setting;
extern const char str;
extern char GLACIER_ALLOWED_LICENSES; /* Configuration variables */
extern int GLACIER_ALLOW_SERVICES;
extern char *GLACIER_ALLOWED_LICENSES;
extern int GLACIER_DO_INT_CHECK; extern int GLACIER_DO_INT_CHECK;
extern int GLACIER_VERBOSE; extern int GLACIER_VERBOSE;
/* Profile variables */
extern const char *GLACIER_REPO; extern const char *GLACIER_REPO;
extern const char *GLACIER_ARCH; extern const char *GLACIER_ARCH;
extern const char *GLACIER_TARGET; extern const char *GLACIER_TARGET;
extern const char *GLACIER_LOCALDB; extern const char *GLACIER_LOCALDB;
extern const char *GLACIER_SYSTEM_PROFILE; extern const char *GLACIER_SYSTEM_PROFILE;
const char *runtime_files[]; /* Required runtime files */
extern const char *runtime_files[];
/* File pointers for hashing operations */
extern FILE *expected_hash; extern FILE *expected_hash;
extern FILE *pkg; extern FILE *pkg;

View File

@ -18,6 +18,12 @@
#ifndef GLACIERPKGOPS_H_ #ifndef GLACIERPKGOPS_H_
#define GLACIERPKGOPS_H_ #define GLACIERPKGOPS_H_
/* Permission constant for workspace directory creation */
#define DEFAULT_PERMISSIONS 0750
/* Maximum size for path buffers */
#define PATH_MAX_SIZE 512
/* /*
* mkworkspace * mkworkspace
* *
@ -25,14 +31,14 @@
* PARAMETERS: * PARAMETERS:
* None. * None.
* RETURN VAUES: * RETURN VAUES:
* 0 on success, 2 on library error * 0 on workspace already exists, 1 on workspace created, -1 on error
* CAVEATS: * CAVEATS:
* None. * None.
* EXAMPLE: * EXAMPLE:
* mkworkspace(); * mkworkspace();
*/ */
int mkworkspace(); int mkworkspace(void);
/* /*
* prepare_pkg * prepare_pkg
@ -41,7 +47,7 @@ int mkworkspace();
* PARAMETERS: * PARAMETERS:
* char PACKAGE[] -> The package file to prepare * char PACKAGE[] -> The package file to prepare
* RETURN VAUES: * RETURN VAUES:
* 0 on success, 1 on package does not exist, or error untarring * 0 on success, 1 on package does not exist or error, other values for specific errors
* CAVEATS: * CAVEATS:
* The example presented is bad. You should be calling the system profile variable * The example presented is bad. You should be calling the system profile variable
* rather than manually specifying one. * rather than manually specifying one.
@ -58,7 +64,7 @@ int prepare_pkg(char PACKAGE[]);
* PARAMETERS: * PARAMETERS:
* char TASK[] -> The make task to run * char TASK[] -> The make task to run
* RETURN VAUES: * RETURN VAUES:
* 0 on success, 1 on failure * 0 on success, other values for specific errors
* CAVEATS: * CAVEATS:
* MUST be run after prepare_pkg(), or else errors will occur * MUST be run after prepare_pkg(), or else errors will occur
* Same caveat as above. Do not manually specify the system profile, use its variable. * Same caveat as above. Do not manually specify the system profile, use its variable.

View File

@ -18,6 +18,9 @@
#ifndef GLACIERSECURITY_H_ #ifndef GLACIERSECURITY_H_
#define GLACIERSECURITY_H_ #define GLACIERSECURITY_H_
typedef unsigned int uint;
typedef unsigned char uchar;
/* /*
* compare_file_hash * compare_file_hash
* *
@ -35,4 +38,61 @@
/* int compare_file_hash(char ORIG_HASH[], char FILE[]); */ /* int compare_file_hash(char ORIG_HASH[], char FILE[]); */
/*
* hash_file
*
* DESCRIPTION: Performs a hashing operation on a file and stores the result
* PARAMETERS:
* const char *filename -> The file to hash
* unsigned char *out_hash -> Buffer to store the resulting hash
* unsigned int *out_length -> Will contain the length of the hash
* RETURN VALUES:
* 0 on success, other values for specific errors
* CAVEATS:
* out_hash buffer must be large enough to hold the hash (EVP_MAX_MD_SIZE recommended)
* EXAMPLE:
* unsigned char hash[EVP_MAX_MD_SIZE];
* unsigned int hash_len;
* hash_file("file.txt", hash, &hash_len);
*/
int hash_file(const char *filename, unsigned char *out_hash, unsigned int *out_length);
/*
* print_hash
*
* DESCRIPTION: Prints a specified hash string to stdout
* PARAMETERS:
* unsigned char *hash -> The hash to print
* unsigned int length -> Length of the hash
* RETURN VALUES:
* 0 on success, 1 on error
* CAVEATS:
* None
* EXAMPLE:
* print_hash(hash, hash_len);
*/
int print_hash(uchar *hash, uint length);
/*
* stash_hash
*
* DESCRIPTION: Stores a hash inside a string as hexadecimal representation
* PARAMETERS:
* char *stored_hash -> Buffer to store the resulting hash string
* unsigned int stored_hash_size -> Size of the stored_hash buffer
* const uchar *hash -> The hash to convert to string
* uint length -> Length of the hash
* RETURN VALUES:
* 0 on success, 1 on error
* CAVEATS:
* stored_hash buffer must be at least (length*2)+1 bytes in size
* EXAMPLE:
* char hash_str[65]; // 32 bytes SHA-256 = 64 hex chars + null terminator
* stash_hash(hash_str, sizeof(hash_str), hash, hash_len);
*/
int stash_hash(char *stored_hash, unsigned int stored_hash_size, const uchar *hash, uint length);
#endif #endif

View File

@ -20,21 +20,34 @@
#include <errno.h> #include <errno.h>
#include <libconfig.h> #include <libconfig.h>
#include <locale.h> #include <locale.h>
#include <openssl/sha.h> #include <openssl/evp.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#include <wchar.h> #include <wchar.h>
#include "config.h" #include "config.h"
/* Buffer and size constants */
#define BUFFER_SIZE 1024 #define BUFFER_SIZE 1024
#define MAX_CHILDREN 64 #define MAX_CHILDREN 64
#define MAX_SIZE 256 #define MAX_SIZE 256
#define PATH_MAX_SIZE 512
#define MAX_RECURSION_DEPTH 100
#define DEFAULT_PERMISSIONS 0750
/* Define LG_VERBOSE if not defined elsewhere */
#ifndef LG_VERBOSE
#define LG_VERBOSE 0
#endif
typedef unsigned int uint;
typedef unsigned char uchar;
/* /*
* Global Variables * Global Variables
@ -45,19 +58,20 @@
*/ */
config_t cfg; /* Context for libconfig */ config_t cfg; /* Context for libconfig */
config_setting_t *setting; /* Pointer for setting */ config_setting_t *setting = NULL; /* Pointer for setting */
const char str; /* Unsure what this does, will possibly remove later */
int GLACIER_ALLOW_SERVICES; /* Declaration of GLACIER_ALLOW_SERVICES as given in glacier.cfg */ /* Configuration variables with default values */
char GLACIER_ALLOWED_LICENSES; /* Declaration of GLACIER_ALLOWED_LICENSES as given in glacier.cfg */ int GLACIER_ALLOW_SERVICES = 0; /* Declaration of GLACIER_ALLOW_SERVICES as given in glacier.cfg */
int GLACIER_DO_INT_CHECK; /* Declaration of GLACIER_DO_INT_CHECK as given in glacier.cfg */ char *GLACIER_ALLOWED_LICENSES = NULL; /* Declaration of GLACIER_ALLOWED_LICENSES as given in glacier.cfg */
int GLACIER_VERBOSE; /* Declaration of GLACIER_VERBOSE as given in glacier.cfg */ int GLACIER_DO_INT_CHECK = 1; /* Declaration of GLACIER_DO_INT_CHECK as given in glacier.cfg */
int GLACIER_VERBOSE = 0; /* Declaration of GLACIER_VERBOSE as given in glacier.cfg */
const char *GLACIER_REPO; /* Declaration of GLACIER_REPO as defined in profile.cfg */ /* Profile variables */
const char *GLACIER_ARCH; /* Declaration of GLACIER_ARCH as defined in profile.cfg */ const char *GLACIER_REPO = NULL; /* Declaration of GLACIER_REPO as defined in profile.cfg */
const char *GLACIER_TARGET; /* Declaration of GLACIER_TARGET as defined in profile.cfg */ const char *GLACIER_ARCH = NULL; /* Declaration of GLACIER_ARCH as defined in profile.cfg */
const char *GLACIER_LOCALDB; /* Declaration of GLACIER_LOCALDB as defined in profile.cfg */ const char *GLACIER_TARGET = NULL; /* Declaration of GLACIER_TARGET as defined in profile.cfg */
const char *GLACIER_SYSTEM_PROFILE; /* Declaration of GLACIER_SYSTEM_PROFILE as defined in profile.cfg */ const char *GLACIER_LOCALDB = NULL; /* Declaration of GLACIER_LOCALDB as defined in profile.cfg */
const char *GLACIER_SYSTEM_PROFILE = NULL; /* Declaration of GLACIER_SYSTEM_PROFILE as defined in profile.cfg */
/* Required runtime files */ /* Required runtime files */
const char *runtime_files[] = { const char *runtime_files[] = {
@ -85,6 +99,8 @@ struct node {
char *data; char *data;
struct node *children[MAX_CHILDREN]; struct node *children[MAX_CHILDREN];
int numChildren; int numChildren;
struct node *left;
struct node *right;
}; };
/* /*
@ -328,12 +344,65 @@ load_setting_from_config(char SETTING[])
struct node struct node
*create_node(char *data) *create_node(char *data)
{ {
if (data == NULL) {
if (LG_VERBOSE == 1) { errlog("NULL data passed to create_node()"); }
return NULL;
}
struct node *newNode = (struct node*)malloc(sizeof(struct node)); struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = strdup(data); if (newNode == NULL) {
if (LG_VERBOSE == 1) { errlog("Memory allocation failed in create_node()"); }
return NULL;
}
newNode->data = strdup(data);
if (newNode->data == NULL) {
if (LG_VERBOSE == 1) { errlog("String duplication failed in create_node()"); }
free(newNode);
return NULL;
}
newNode->numChildren = 0; newNode->numChildren = 0;
newNode->left = NULL;
newNode->right = NULL;
for (int i = 0; i < MAX_CHILDREN; i++) {
newNode->children[i] = NULL;
}
return newNode; return newNode;
} }
/*
* free_node
*
* DESCRIPTION: Recursively free a node and all its children.
* PARAMETERS: struct node *root
* DEFINED IN: data.h
*
*/
void
free_node(struct node *root)
{
if (root == NULL) {
return;
}
/* Free all children recursively */
for (int i = 0; i < root->numChildren; i++) {
free_node(root->children[i]);
}
/* Free data string */
if (root->data != NULL) {
free(root->data);
}
/* Free node itself */
free(root);
}
/* /*
* add_child * add_child
* *
@ -343,14 +412,31 @@ struct node
* *
*/ */
void int
add_child(struct node *parent, struct node *child) add_child(struct node *parent, struct node *child)
{ {
if (parent == NULL || child == NULL) {
if (LG_VERBOSE == 1) {
errlog("NULL pointer passed to add_child()");
}
return 1;
}
if (parent->numChildren < 0 || parent->numChildren >= MAX_CHILDREN) {
if (LG_VERBOSE == 1) {
errlog("Invalid numChildren value in parent node");
}
return 2;
}
if (parent->numChildren < MAX_CHILDREN) { if (parent->numChildren < MAX_CHILDREN) {
parent->children[parent->numChildren++] = child; parent->children[parent->numChildren++] = child;
return 0;
} else { } else {
if (LG_VERBOSE == 1) { errlog("Maximum number of children exceeded"); } if (LG_VERBOSE == 1) {
exit(1); errlog("Maximum number of children exceeded");
}
return 3;
} }
} }
@ -363,160 +449,53 @@ add_child(struct node *parent, struct node *child)
* *
*/ */
void int
print_tree(struct node *root, int level) print_tree(struct node *root, int level)
{ {
if (root == NULL) { if (root == NULL) {
return; return 0;
}
if (level < 0) {
if (LG_VERBOSE == 1) {
errlog("Invalid level value in print_tree()");
}
return 1;
}
if (level > MAX_RECURSION_DEPTH) {
/* Safety check to prevent stack overflow from recursive calls */
if (LG_VERBOSE == 1) {
errlog("Maximum recursion depth exceeded in print_tree()");
}
return 2;
} }
for (int i = 0; i < level; i++) { for (int i = 0; i < level; i++) {
printf(" "); printf(" ");
} }
printf("%s\n", root->data); if (root->data == NULL) {
printf("(NULL)\n");
} else {
printf("%s\n", root->data);
}
if (root->numChildren < 0 || root->numChildren > MAX_CHILDREN) {
if (LG_VERBOSE == 1) {
errlog("Invalid numChildren value in node");
}
return 3;
}
for (int i = 0; i < root->numChildren; i++) { for (int i = 0; i < root->numChildren; i++) {
print_tree(root->children[i], level + 1); if (print_tree(root->children[i], level + 1) != 0) {
} /* Propagate errors up the call stack */
} return 4;
}
/*
* queue
*
* DESCRIPTION: Definition of queue type.
* DEFINED IN: data.h
*
*/
typedef struct {
int items[MAX_SIZE];
int front;
int rear;
} queue;
/*
* init_queue
*
* DESCRIPTION: Initialize a queue.
* PARAMETERS: queue *q
* DEFINED IN: data.h
*
*/
void
init_queue(queue *q)
{
q -> front = -1;
q -> rear = 0;
}
/*
* queue_is_empty
*
* DESCRIPTION: Check if queue is empty.
* PARAMETERS: struct node *root, int level
* DEFINED IN: data.h
*
*/
bool queue_is_empty(queue *q) { return (q -> front == q -> rear -1); }
/*
* queue_is_full
*
* DESCRIPTION: Check if queue is full.
* PARAMETERS: queue *q
* DEFINED IN: data.h
*
*/
bool queue_is_full(queue *q) { return (q -> rear == MAX_SIZE); }
/*
* enqueue
*
* DESCRIPTION: Enqueue an element at the back of the queue.
* PARAMETERS: queue *q, int value
* DEFINED IN: data.h
*
*/
void
enqueue (queue *q, int value)
{
if (queue_is_full(q)) {
printf("Queue is full\n");
return;
} }
q -> items[q -> rear] = value; return 0;
q -> rear++;
}
/*
* dequeue
*
* DESCRIPTION: Dequeue the element at the front of the queue.
* PARAMETERS: queue *q, int value
* DEFINED IN: data.h
*
*/
void
dequeue(queue *q)
{
if (queue_is_empty(q)) {
printf("Queue is empty\n");
return;
}
q -> front++;
}
/*
* peek
*
* DESCRIPTION: View the element at the front of the queue.
* PARAMETERS: struct node *root, int level
* DEFINED IN: data.h
*
*/
int
peek(queue *q)
{
if (queue_is_empty(q)) {
printf("Queue is empty\n");
return -1;
}
return q -> items[q -> front + 1];
}
/*
* print_queue
*
* DESCRIPTION: Print the queue.
* PARAMETERS: queue *q
* DEFINED IN: data.h
*
*/
void
print_queue(queue *q)
{
if (queue_is_empty(q)) {
printf("Queue is empty\n");
return;
}
printf("Current Queue: ");
for (int i = q -> front + 1; i < q -> rear; i++) {
printf("%d ", q -> items[i]);
}
printf("\n");
} }
/* /*
@ -531,18 +510,31 @@ print_queue(queue *q)
int int
mkworkspace(void) mkworkspace(void)
{ {
DIR* workspace = opendir("/tmp/glacier-workspace"); const char *workspace_path = "/tmp/glacier-workspace";
DIR* workspace = opendir(workspace_path);
if (workspace) { if (workspace) {
/* Workspace exists */
closedir(workspace);
/* infolog("Not creating new workspace, valid workspace already exists."); */ /* infolog("Not creating new workspace, valid workspace already exists."); */
return 0; return 0;
} else if (ENOENT == errno) { } else if (ENOENT == errno) {
/* infolog("Creating new Glacier workspace..."); */ /* infolog("Creating new Glacier workspace..."); */
mkdir("/tmp/glacier-workspace", 0777); if (mkdir(workspace_path, DEFAULT_PERMISSIONS) != 0) {
if (LG_VERBOSE == 1) {
errlog("in mkworkspace()");
errlog("mkdir() failed to create workspace directory");
fprintf(stderr, "Error: %s\n", strerror(errno));
}
return -1;
}
return 1; return 1;
} else { } else {
/* Some other error occurred */
if (LG_VERBOSE == 1) { if (LG_VERBOSE == 1) {
errlog("in mkworkspace()"); errlog("in mkworkspace()");
errlog("mkdir() failed to run"); errlog("opendir() failed to check workspace");
fprintf(stderr, "Error: %s\n", strerror(errno));
} }
return -1; return -1;
} }
@ -566,46 +558,87 @@ prepare_pkg(char PACKAGE[])
errlog("This can be done by running 'glacier-update-pkgdb' as root."); errlog("This can be done by running 'glacier-update-pkgdb' as root.");
return 1; return 1;
} else { } else {
char PKG_NEW[512]; char PKG_NEW[PATH_MAX_SIZE];
strcat(PKG_NEW, "/tmp/glacier-workspace/"); PKG_NEW[0] = '\0'; /* Initialize the string buffer */
strcat(PKG_NEW, PACKAGE); snprintf(PKG_NEW, sizeof(PKG_NEW), "/tmp/glacier-workspace/%s.tar", PACKAGE);
strcat(PKG_NEW, ".tar");
FILE *pkg_old, *pkg_new; FILE *pkg_old, *pkg_new;
char filename[100], contents; char buffer[BUFFER_SIZE];
size_t bytes_read;
pkg_old = fopen(PACKAGE, "r"); pkg_old = fopen(PACKAGE, "r");
pkg_new = fopen(PKG_NEW, "a+"); if (pkg_old == NULL) {
errlog("Failed to open source package file");
return 1;
}
pkg_new = fopen(PKG_NEW, "w");
if (pkg_new == NULL) {
errlog("Failed to create destination package file");
fclose(pkg_old);
return 1;
}
contents = fgetc(pkg_old); /* Use buffered I/O for better efficiency */
while ((bytes_read = fread(buffer, 1, sizeof(buffer), pkg_old)) > 0) {
while (contents != EOF) { if (fwrite(buffer, 1, bytes_read, pkg_new) != bytes_read) {
fputc(contents, pkg_new); errlog("Failed to write to destination file");
contents = fgetc(pkg_old); fclose(pkg_old);
fclose(pkg_new);
return 1;
}
} }
fclose(pkg_old); fclose(pkg_old);
fclose(pkg_new); fclose(pkg_new);
char *tar_args[] = {
"/bin/tar", /* This should be changed to /glacier/bin/tar later on */ /* Fork before exec to prevent process replacement */
"-xvf", pid_t pid = fork();
PKG_NEW, if (pid < 0) {
NULL, errlog("Failed to fork process");
}; return 1;
} else if (pid == 0) {
/* Child process */
char *tar_args[] = {
"/bin/tar", /* This should be changed to /glacier/bin/tar later on */
"-xvf",
PKG_NEW,
NULL,
};
execvp( execvp("/bin/tar", tar_args);
"/bin/tar", /* Above comment applies here */ /* If we get here, execvp failed */
tar_args fprintf(stderr, COL_RED "[x] " COL_RESET "Failed to execute tar: %s\n", strerror(errno));
); exit(EXIT_FAILURE);
if (errno != 0) { } else {
printf(COL_RED "[x] " COL_RESET "Error while unpacking archive for package %s.\n", PACKAGE); /* Parent process */
return errno; int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
printf(COL_RED "[x] " COL_RESET "Error while unpacking archive for package %s.\n", PACKAGE);
return WEXITSTATUS(status);
}
if (remove(PKG_NEW) != 0) {
warnlog("Failed to remove temporary package file");
}
char pkg_dir[PATH_MAX_SIZE];
pkg_dir[0] = '\0'; /* Initialize the string buffer */
snprintf(pkg_dir, sizeof(pkg_dir), "/tmp/glacier-workspace/%s", PACKAGE);
if (chdir(pkg_dir) != 0) {
errlog("Failed to change directory");
return 1;
}
return 0;
} }
remove(PKG_NEW);
char pkg_dir[512];
strcat(pkg_dir, "/tmp/glacier-workspace/");
strcat(pkg_dir, PACKAGE);
chdir(pkg_dir);
} }
/* Should never reach here, but added for completeness */
return 1;
} }
/* /*
@ -620,60 +653,199 @@ prepare_pkg(char PACKAGE[])
int int
run_make_task(char TASK[]) run_make_task(char TASK[])
{ {
char *build_args[] = { if (TASK == NULL) {
"/bin/make", /* This should be changed to /glacier/bin/make later on */ errlog("No task specified for make");
TASK, return 1;
NULL
};
execvp(
"/bin/make", /* Above comment applies here */
build_args
);
if (errno != 0) {
errlog("An error occurred while running the make task.");
return errno;
} }
/* Fork before exec to prevent process replacement */
pid_t pid = fork();
if (pid < 0) {
errlog("Failed to fork process");
return 1;
} else if (pid == 0) {
/* Child process */
char *build_args[] = {
"/bin/make", /* This should be changed to /glacier/bin/make later on */
TASK,
NULL
};
execvp("/bin/make", build_args);
/* If we get here, execvp failed */
errlog("Failed to execute make");
fprintf(stderr, "Error: %s\n", strerror(errno));
exit(EXIT_FAILURE);
} else {
/* Parent process */
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
return WEXITSTATUS(status);
} else {
errlog("Make process terminated abnormally");
return 1;
}
}
/* Should never reach here, but added for completeness */
return 1;
} }
/* /*
* compare_file_hash * print_hash
* *
* DESCRIPTION: Compare two file hashes * DESCRIPTION: Prints a specified hash string
* PARAMETERS: char ORIG_HASH[], char FILE[] * PARAMETERS: unsigned char *hash, unsigned int length
* DEFINED IN: security.h * DEFINED IN: security.h
* *
*/ */
/*
int int
compare_file_hash(char ORIG_HASH[], char FILE[]) print_hash(uchar *hash, uint length)
{ {
FILE *pkg; if (hash == NULL) {
errlog("NULL hash pointer passed to print_hash()");
pkg = fopen(FILE, "rb"); return 1;
if (pkg == NULL) {
return -1;
} }
unsigned char hash[SHA256_DIGEST_LENGTH]; if (length == 0) {
SHA256_CTX sha256; warnlog("Zero-length hash passed to print_hash()");
}
SHA256_Init(&sha256); for (uint index = 0; index < length; index++) {
printf("%02x", hash[index]);
}
const int hashBufferSize = 1024; printf("\n");
unsigned char hashBuffer[hashBufferSize];
int bytesRead;
while ((byesRead = fread(hashBuffer, 1, hashBufferSize, pkg)) != 0) { return 0;
SHA256_Update(&sha256, hashBuffer, bytesRead); }
/*
* stash_hash
*
* DESCRIPTION: Stores a hash inside a string
* PARAMETERS: unsigned char *stored_hash, unsigned char *hash, unsigned int length
* DEFINED IN: security.h
*
*/
int
stash_hash(char *stored_hash, unsigned int stored_hash_size, const uchar *hash, uint length)
{
if (stored_hash == NULL || hash == NULL) {
if (LG_VERBOSE == 1) {
errlog("in stash_hash()");
errlog("NULL pointer provided");
}
return 1;
} }
SHA256_Final(hash, &sha256); if (stored_hash_size < (length * 2 + 1)) {
fclose(pkg); if (LG_VERBOSE == 1) {
errlog("in stash_hash()");
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { errlog("Output buffer too small for hash");
printf("%02x", hash[i]); }
return 1;
} }
/* Clear the buffer first */
stored_hash[0] = '\0';
/* Format each byte as a 2-character hex value */
for (uint index = 0; index < length; index++) {
char hex[3];
snprintf(hex, sizeof(hex), "%02x", hash[index]);
strncat(stored_hash, hex, stored_hash_size - strlen(stored_hash) - 1);
}
return 0;
}
/*
* hash_file
*
* DESCRIPTION: Performs a hashing operation on a file and stores the result
* PARAMETERS: const char *filename
* DEFINED IN: security.h
*
*/
int
hash_file(const char *filename, unsigned char *out_hash, unsigned int *out_length)
{
FILE *data = fopen(filename, "rb");
if (! data && LG_VERBOSE == 1) {
errlog("in hash_file()");
errlog("error opening file handle");
return 1;
}
else if (! data) {
return 1;
}
EVP_MD_CTX *context = EVP_MD_CTX_new();
if (! context && LG_VERBOSE == 1) {
errlog("in hash_file()");
errlog("error creating envelope context");
fclose(data);
return 2;
}
else if (! context) {
fclose(data);
return 2;
}
if (EVP_DigestInit_ex(context, EVP_sha256(), NULL) != 1) {
if (LG_VERBOSE == 1) {
errlog("in hash_file()");
errlog("error initializing digest");
}
EVP_MD_CTX_free(context);
fclose(data);
return 3;
}
// Read and hash file content
unsigned char buffer[BUFFER_SIZE];
size_t bytes_read;
while ((bytes_read = fread(buffer, 1, BUFFER_SIZE, data)) > 0) {
if (EVP_DigestUpdate(context, buffer, bytes_read) != 1) {
if (LG_VERBOSE == 1) {
errlog("in hash_file()");
errlog("error updating digest");
}
EVP_MD_CTX_free(context);
fclose(data);
return 4;
}
}
if (ferror(data)) {
if (LG_VERBOSE == 1) {
errlog("in hash_file()");
errlog("error reading file");
}
EVP_MD_CTX_free(context);
fclose(data);
return 5;
}
fclose(data);
if (EVP_DigestFinal_ex(context, out_hash, out_length) != 1) {
if (LG_VERBOSE == 1) {
errlog("in hash_file()");
errlog("error finalizing digest");
}
EVP_MD_CTX_free(context);
return 6;
}
EVP_MD_CTX_free(context);
return 0;
} }
*/

83
tests/unit-tests.c Normal file
View File

@ -0,0 +1,83 @@
/*
* unit-tests.c - Unit tests for libglacier
*
* This file is part of Glacier.
*
* Glacier 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.
*
* Glacier 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 Glacier. If
* not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <libconfig.h>
#include <unistd.h>
#include "../include/config.h"
#include "../include/log.h"
#include "../include/pkgops.h"
#include "../include/runtime.h"
#include "../include/security.h"
#include <CUnit/Basic.h>
void
test_is_process_root(void)
{
if (is_process_root() != 0) {
CU_FAIL("is_process_root() with UID != 0 failed");
}
else if (is_process_root() == 0) {
CU_PASS("is_process_root() with UID != 0 passed");
}
}
void
test_init_config(void)
{
CU_ASSERT_TRUE(init_config());
}
int
main(void)
{
if (CUE_SUCCESS != CU_initialize_registry()) {
return CU_get_error();
}
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_pSuite runtime_tests = CU_add_suite("Runtime Functions Suite", NULL, NULL);
if (! runtime_tests) {
CU_cleanup_registry();
return CU_get_error();
}
CU_pSuite config_tests = CU_add_suite("Configuration Functions Suite", NULL, NULL);
if (! config_tests) {
CU_cleanup_registry();
return CU_get_error();
}
if (! CU_add_test(runtime_tests, "test of is_process_root()", test_is_process_root)) {
CU_cleanup_registry();
return CU_get_error();
}
if (! CU_add_test(config_tests, "test of init_config()", test_init_config)) {
CU_cleanup_registry();
return CU_get_error();
}
CU_basic_run_tests();
CU_cleanup_registry();
return CU_get_error();
}