19 Commits

Author SHA1 Message Date
0b03b9b954 v0.2.0-rc - implement basic integrity checking framework 2025-03-22 11:19:41 -04:00
25a55609ec Restructure chroot directory 2025-03-19 11:37:30 -04:00
bb6bc22935 Begin implementing chroot 2025-03-18 17:48:59 -04:00
1a32ab4009 remove queue data structure and begin to implement basic chroot script for testing 2025-03-18 14:59:44 -04:00
923df644a0 UNIT TESTING WORKS 2025-03-10 22:26:24 -04:00
7258ef5810 implement basic unit testing 2025-03-10 21:21:03 -04:00
60996e3de2 3/8 2025-03-08 22:27:53 -05:00
f94ed28d93 add two initial functions 2025-03-07 22:57:32 -05:00
1f746e77ed Merge pull request 'fix Makefile' (#13) from hotfix/fix_makefile into main
Reviewed-on: #13
2025-02-18 18:58:03 -05:00
e5af8df970 fix Makefile 2025-02-18 18:56:27 -05:00
1a18d642e0 Merge pull request 'lw-everestlinux/feature/new-makefile' (#11) from lw-everestlinux/feature/new-makefile into main
Reviewed-on: #11
2025-02-18 11:51:54 -05:00
92d25f7efe Implement new Makefile as well as clean up source code 2025-02-18 11:38:17 -05:00
0c3f2db582 init 2025-01-23 10:59:22 -05:00
87e9f06315 begin splitting source code files 2025-01-04 12:34:35 -05:00
8140187d22 Merge pull request 'v4.0.4-rc' (#7) from release/v4.0.4-rc into main
Reviewed-on: #7
2024-12-30 14:30:59 -05:00
dddbe4b320 v4.0.4-rc 2024-12-30 14:27:44 -05:00
73df7a5e74 Merge pull request 'release/v4.0.3-rc' (#5) from release/v4.0.3-rc into main
Reviewed-on: #5
2024-12-28 13:29:25 -05:00
38f6c5adf7 fix compilation error on line 131 2024-12-28 13:06:52 -05:00
e69b9c1197 add finishing touches for v4.0.3-rc 2024-12-28 13:04:11 -05:00
17 changed files with 6465 additions and 873 deletions

View File

@@ -1,40 +1,69 @@
#
# Makefile
#
IDIR = ./include
BDIR = ./build
SDIR = ./src
TDIR = ./tests
include config.mk include config.mk
COLOR_GREEN=\033[0;32m
COLOR_BLUE=\033[0;34m
COLOR_RESET=\033[0m
all: all:
@echo "Run 'make test' for the test binary, or 'make lib' to generate a static library." @echo "No make option specified."
@echo "Run 'make help' for a full list of options." @echo "Run 'make help' for a full list of options."
help: help:
@echo "Make options for libglacier:" @echo "Make options for libglacier:"
@echo "" @echo ""
@echo "lib - generate the static library" @echo "lib - generate the static library"
@echo "test - generate a test binary" @echo "install - install the static library and header files to PREFIX"
@echo "clean - remove all generated files" @echo "clean - remove all generated files"
test: deprecate:
@echo "[WARN]"
@echo "[WARN] This rule has been deprecated, proceed with caution."
@echo "[WARN]"
test: deprecate
$(CC) libglacier.c $(LIBFLAGS) -o libglacier.test $(CC) libglacier.c $(LIBFLAGS) -o libglacier.test
cp etc/example.cfg ./glacier.cfg cp etc/example.cfg ./glacier.cfg
lib: lib: $(SDIR)/libglacier.c
mkdir build mkdir build
mkdir build/lib mkdir build/lib
$(CC) libglacier.c -c $(LIBFLAGS) -o build/lib/libglacier.o $(CC) $(SDIR)/libglacier.c -c $(LIBFLAGS) -o $(BDIR)/lib/libglacier.o
$(AR) -rc build/lib/libglacier.a build/lib/libglacier.o $(AR) -rc $(BDIR)/lib/libglacier.a build/lib/libglacier.o
install: check: lib $(BDIR) $(TDIR)/unit-tests.c
@echo "$(COLOR_BLUE)[i]$(COLOR_RESET) Installing static library to $(PREFIX)/lib..." $(CC) tests/unit-tests.c -o tests/test-suite -lcunit -Wall -Wextra build/lib/libglacier.a $(LIBFLAGS)
install lib/libglacier.a $(PREFIX)/lib tests/test-suite
install include/config.h $(PREFIX)/include/glacier
install include/data.h $(PREFIX)/include/glacier install_lib: $(BDIR)/lib/libglacier.a
install include/log.h $(PREFIX)/include/glacier @echo "[INFO]"
install include/pkgops.h $(PREFIX)/include/glacier @echo "[INFO] Installing library to PREFIX/lib..."
install include/runtime.h $(PREFIX)/include/glacier @echo "[INFO]"
install include/security.h $(PREFIX)/include/glacier install $(BDIR)/lib/libglacier.a $(PREFIX)/lib
@echo "$(COLOR_GREEN)[*]$(COLOR_RESET) Finished installing static library to $(PREFIX)/lib."
install_head: $(IDIR)/config.h $(IDIR)/data.h $(IDIR)/log.h $(IDIR)/pkgops.h $(IDIR)/runtime.h $(IDIR)/security.h
@echo "[INFO]"
@echo "[INFO] Installing header files to PREFIX/include/glacier..."
@echo "[INFO]"
mkdir -p $(PREFIX)/include/glacier
install $(IDIR)/config.h -t $(PREFIX)/include/glacier -m 644
install $(IDIR)/data.h -t $(PREFIX)/include/glacier -m 644
install $(IDIR)/log.h -t $(PREFIX)/include/glacier -m 644
install $(IDIR)/pkgops.h -t $(PREFIX)/include/glacier -m 644
install $(IDIR)/runtime.h -t $(PREFIX)/include/glacier -m 644
install $(IDIR)/security.h -t $(PREFIX)/include/glacier -m 644
install: install_lib install_head
@echo "[INFO]"
@echo "[INFO] Finished installing library and header files to PREFIX."
@echo "[INFO]"
clean: clean:
rm -rf build rm -rf $(BDIR) ./chroot
distclean:
rm -rf $(BDIR) $(TDIR)/test-suite ./chroot

View File

@@ -1 +1 @@
4.0.0-rc7 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

21
etc/make.conf Normal file
View File

@@ -0,0 +1,21 @@
#
# make.conf
# The settings defined in this file will be loaded when 'make' is called during a Glacier operation.
# For more information on this file, see https://www.everestlinux.org/docs/intro-to-glacier
#
# Number of parallel makejobs. By default this is set by nproc and should be optimal for your system.
MAKEFLAGS="-j$(nproc)"
# Build and compile flags for C and C++ programs.
# Using too many build flags will break packages. Disable these if packages are breaking.
CFLAGS="-O2"
CXXFLAGS="${CFLAGS}"
# Treat all warnings as errors
# Not recommended, as many packages with larger codebases may refuse to compile.
#CFLAGS="-Werror"
# Prefix for packages to be installed.
# Not all packages will honor this.
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

@@ -18,6 +18,23 @@
#ifndef GLACIERDATA_H_ #ifndef GLACIERDATA_H_
#define GLACIERDATA_H_ #define GLACIERDATA_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
* *
@@ -25,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
@@ -44,113 +75,30 @@ 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
* *
* DESCRIPTION: Print_tree prints a dependency tree specified at its root node * DESCRIPTION: Print_tree prints a dependency tree specified at its root node.
* PARAMETERS: * PARAMETERS:
* 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);
/**************************************************************************************************************/
/*
* 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 ->
* RETURN VALUES:
* true on full, false on not full
* CAVEATS:
* None.
* EXAMPLE:
* if (queue_is_full(q)) {
* printf("Queue is full\n");
* return;
* }
*/
#endif #endif

View File

@@ -18,16 +18,35 @@
#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;
/* Configuration variables */
extern int GLACIER_ALLOW_SERVICES;
extern char *GLACIER_ALLOWED_LICENSES;
extern int GLACIER_DO_INT_CHECK;
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 *pkg;
#endif #endif

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
* *
@@ -33,6 +36,63 @@
* compare_file_hash("pkg.sha256sum", "pkg.tar.xz"); * compare_file_hash("pkg.sha256sum", "pkg.tar.xz");
*/ */
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

@@ -1,679 +0,0 @@
/*
* libglacier.c - Backend C library for Glacier
*
* 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 <color.h>
#include <dirent.h>
#include <errno.h>
#include <libconfig.h>
#include <locale.h>
#include <openssl/sha.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <wchar.h>
#include "config.h"
#include "include/globals.h"
#define BUFFER_SIZE 1024
#define MAX_CHILDREN 64
#define MAX_SIZE 256
/* Global variables */
config_t cfg;
config_setting_t *setting;
const char str;
int GLACIER_ALLOW_SERVICES;
char GLACIER_ALLOWED_LICENSES;
int GLACIER_DO_INT_CHECK;
int GLACIER_VERBOSE;
const char *GLACIER_REPO;
const char *GLACIER_ARCH;
const char *GLACIER_TARGET;
const char *GLACIER_LOCALDB;
const char *GLACIER_SYSTEM_PROFILE;
const char *runtime_files[] = {
"/etc/glacier.cfg",
"/etc/glacier/call-hooks",
"/etc/make.conf"
};
struct node {
char *data;
struct node *children[MAX_CHILDREN];
int numChildren;
};
FILE *expected_hash;
FILE *pkg;
/*
* infolog
*
* DESCRIPTION: Output a stylized info message.
* PARAMETERS: char MSG[]
* DEFINED IN: glacier_log.h
*
*/
void
infolog(char MSG[])
{
if (MSG == NULL) {
return;
}
printf(COL_BLUE "[i]" COL_RESET " %s\n", MSG);
return;
}
/*
* warnlog
*
* DESCRIPTION: Output a stylized warning message.
* Parameters: char MSG[]
* DEFINED IN: glacier_log.h
*
*/
void
warnlog(char MSG[])
{
if (MSG == NULL) {
return;
}
printf(COL_YELLOW "[!]" COL_RESET " %s\n", MSG);
return;
}
/*
* errlog
*
* DESCRIPTION: Output a stylized error message.
* PARAMETERS: char MSG[]
* DEFINED IN: glacier_log.h
*
*/
void
errlog(char MSG[])
{
if (MSG == NULL) {
return;
}
fprintf(stderr, COL_RED "[x]" COL_RESET " %s\n", MSG);
return;
}
/*
* successlog
*
* DESCRIPTION: Output a stylized success message.
* PARAMETERS: char MSG[]
* DEFINED IN: glacier_log.h
*
*/
void
successlog(char MSG[])
{
if (MSG == NULL) {
return;
}
printf(COL_GREEN "[*]" COL_RESET " %s\n", MSG);
return;
}
/*
* runtime_exists
*
* DESCRIPTION: Check if necesary runtime files exist.
* PARAMETERS: None.
* DEFINED IN: glacier_runtime.h
*
*/
int
runtime_exists(void)
{
int f;
for (f = 0; f < sizeof(runtime_files); f++) {
if (access(runtime_files[f], F_OK) == 0) {
continue;
} else {
/* might keep this message, might not, idk */
/* errlog("Runtime files are missing, cannot continue with operation.");
printf(COL_RED "[x]" COL_RESET " The following files are missing:\n");
printf(COL_RED "[x]" COL_RESET " \t%s\n", runtime_files[f]); */
return 0;
}
}
return 1;
}
/*
* is_process_root
*
* DESCRIPTION: Check if process is running as root.
* PARAMETERS: char MSG[]
* DEFINED IN: glacier_runtime.h
*
*/
int
is_process_root(void)
{
if (getuid() != 0) {
return 0; /* process is not running as root */
}
else {
return 1; /* process is running as root */
}
}
/*
* init_config
*
* DESCRIPTION: Initialize libconfig.
* PARAMETERS: None.
* DEFINED IN: glacier_config.h
*
*/
int
init_config(void)
{
config_init(&cfg);
if (! config_read_file(&cfg, runtime_files[0])) {
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
return(EXIT_FAILURE);
}
if (LG_VERBOSE == 1) {
infolog("Initialized libconfig");
}
return 0;
}
/*
* die_config
*
* DESCRIPTION: Kill libconfig.
* PARAMETERS: None.
* DEFINED IN: glacier_config.h
*
*/
int
die_config(void)
{
config_destroy(&cfg);
if (LG_VERBOSE == 1) {
infolog("Destroyed libconfig");
}
return(EXIT_SUCCESS);
}
/*
* load_all_from_config
*
* DESCRIPTION: Loads all settings from the Glacier config file.
* PARAMETERS: None.
* DEFINED IN: glacier_config.h
*
*/
int
load_all_from_config(void)
{
config_lookup_bool(&cfg, "GLACIER_DO_INT_CHECK", &GLACIER_DO_INT_CHECK);
config_lookup_bool(&cfg, "GLACIER_VERBOSE", &GLACIER_VERBOSE);
config_lookup_string(&cfg, "GLACIER_REPO", &GLACIER_REPO);
config_lookup_string(&cfg, "GLACIER_ARCH", &GLACIER_ARCH);
config_lookup_string(&cfg, "GLACIER_TARGET", &GLACIER_TARGET);
config_lookup_string(&cfg, "GLACIER_SYSTEM_PROFILE", &GLACIER_SYSTEM_PROFILE);
}
/* issues with this idk
int
load_all_from_config()
{
if(config_lookup_bool(&cfg, "GLACIER_ALLOW_SERVICES", &GLACIER_ALLOW_SERVICES))
printf("Services allowed: %s\n", GLACIER_ALLOW_SERVICES ? "true" : "false");
else
warnlog("GLACIER_SERVICES_ALLOWED is not defined in glacier.cfg.");
if(config_lookup_bool(&cfg, "GLACIER_DO_INT_CHECK", &GLACIER_DO_INT_CHECK))
printf("Integrity checking enabled: %s\n", GLACIER_DO_INT_CHECK ? "true" : "false");
else
warnlog("GLACIER_DO_INT_CHECK is not defined in glacier.cfg.");
if(config_lookup_bool(&cfg, "GLACIER_VERBOSE", &GLACIER_VERBOSE))
printf("Verbose logging: %s\n", GLACIER_VERBOSE ? "true" : "false");
else
warnlog("GLACIER_VERBOSE is not defined in glacier.cfg.");
}
*/
/*
* load_setting_from_config
*
* DESCRIPTION: Load a specified setting from the Glacier config file.
* PARAMETERS: char SETTING[]
* DEFINED IN: glacier_config.h
*
*/
/* int
load_setting_from_config(char SETTING[])
{} */
/*
* create_node
*
* DESCRIPTION: Create a dependency tree node.
* PARAMETERS: char *data
* DEFINED IN: glacier_data.h
*
*/
struct node
*create_node(char *data)
{
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = strdup(data);
newNode->numChildren = 0;
return newNode;
}
/*
* add_child
*
* DESCRIPTION: Add a child node to a parent node.
* PARAMETERS: struct node *parent, struct node *child
* DEFINED IN: glacier_data.h
*
*/
void
add_child(struct node *parent, struct node *child)
{
if (parent->numChildren < MAX_CHILDREN) {
parent->children[parent->numChildren++] = child;
} else {
if (LG_VERBOSE == 1) { errlog("Maximum number of children exceeded"); }
exit(1);
}
}
/*
* print_tree
*
* DESCRIPTION: Print a dependency tree.
* PARAMETERS: struct node *root, int level
* DEFINED IN: glacier_data.h
*
*/
void
print_tree(struct node *root, int level)
{
if (root == NULL) {
return;
}
for (int i = 0; i < level; i++) {
printf(" ");
}
printf("%s\n", root->data);
for (int i = 0; i < root->numChildren; i++) {
print_tree(root->children[i], level + 1);
}
}
/* Definition of queue data type */
typedef struct {
int items[MAX_SIZE];
int front;
int rear;
} queue;
/*
* init_queue
*
* DESCRIPTION: Initialize a queue.
* PARAMETERS: queue *q
* DEFINED IN: glacier_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: glacier_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: glacier_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: glacier_data.h
*
*/
void
enqueue (queue *q, int value)
{
if (queue_is_full(q)) {
printf("Queue is full\n");
return;
}
q -> items[q -> rear] = value;
q -> rear++;
}
/*
* dequeue
*
* DESCRIPTION: Dequeue the element at the front of the queue.
* PARAMETERS: queue *q, int value
* DEFINED IN: glacier_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: glacier_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: glacier_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");
}
/*
* mkworkspace
*
* DESCRIPTION: Creates a new Glacier workspace in /tmp.
* PARAMETERS: None.
* DEFINED IN: glacier_pkgops.h
*
*/
int
mkworkspace(void)
{
DIR* workspace = opendir("/tmp/glacier-workspace");
if (workspace) {
/* infolog("Not creating new workspace, valid workspace already exists."); */
return 0;
} else if (ENOENT == errno) {
/* infolog("Creating new Glacier workspace..."); */
mkdir("/tmp/glacier-workspace", 0777);
return 1;
} else {
if (LG_VERBOSE == 1) {
errlog("in mkworkspace()");
errlog("mkdir() failed to run");
}
return -1;
}
}
/*
* prepare_pkg
*
* DESCRIPTION: Copies a package archive from the localdb to the workspace, and unpacks it.
* PARAMETERS: char PACKAGE[]
* DEFINED IN: glacier_pkgops.h
*
*/
int
prepare_pkg(char PACKAGE[])
{
if (PACKAGE == NULL) {
printf(COL_RED "[x] " COL_RESET "Package '%s' does not exist in the local database.\n", PACKAGE);
errlog("Ensure your local database is up to date and try again.");
errlog("This can be done by running 'glacier-update-pkgdb' as root.");
return 1;
} else {
char PKG_NEW[512];
strcat(PKG_NEW, "/tmp/glacier-workspace/");
strcat(PKG_NEW, PACKAGE);
strcat(PKG_NEW, ".tar");
FILE *pkg_old, *pkg_new;
char filename[100], contents;
pkg_old = fopen(PACKAGE, "r");
pkg_new = fopen(PKG_NEW, "a+");
contents = fgetc(pkg_old);
while (contents != EOF) {
fputc(contents, pkg_new);
contents = fgetc(pkg_old);
}
fclose(pkg_old);
fclose(pkg_new);
char *tar_args[] = {
"/bin/tar", /* This should be changed to /glacier/bin/tar later on */
"-xvf",
PKG_NEW,
NULL,
};
execvp(
"/bin/tar", /* Above comment applies here */
tar_args
);
if (errno != 0) {
printf(COL_RED "[x] " COL_RESET "Error while unpacking archive for package %s.\n", PACKAGE);
return errno;
}
remove(PKG_NEW);
char pkg_dir[512];
strcat(pkg_dir, "/tmp/glacier-workspace/");
strcat(pkg_dir, PACKAGE);
chdir(pkg_dir);
}
}
/* download_package_sources() is deprecated and will be removed in a later release. */
/* int
download_package_sources(char PACKAGE[])
{
printf(COL_BLUE "[i] " COL_RESET "Downloading package archive for %s...\n", PACKAGE);
if (GLACIER_SYSTEM_PROFILE == "x86-musl") {
execl("/glacier/tools/wget", "https://git.everestlinux.org/EverestLinux/epkgs-x86-musl/", NULL);
} else {
printf(COL_RED "[x] " COL_RESET "System profile '%s' is not supported.\n", GLACIER_SYSTEM_PROFILE);
}
} */
void
TEST_chdir(char dir[])
{
char *buf;
chdir(dir);
buf = (char *) malloc(100*sizeof(char));
getcwd(buf, 100);
printf("%s\n", buf);
}
/*
* run_make_task
*
* DESCRIPTION: Runs a make task in current working directory
* PARAMETERS: char TASK[]
* DEFINED IN: glacier_pkgops.h
*
*/
int
run_make_task(char TASK[])
{
char *build_args[] = {
"/bin/make", /* This should be changed to /glacier/bin/make later on */
TASK,
NULL
};
execvp(
"/bin/make", /* Above comment applies here */
build_args
);
if (errno != 0) {
errlog("An error occurred while running the make task.");
return errno;
}
}
/*
* compare_file_hash
*
* DESCRIPTION: Compare two file hashes
* PARAMETERS: char ORIG_HASH[], char FILE[]
* DEFINED IN: glacier_security.h
*
*/
int
compare_file_hash(char ORIG_HASH[], char FILE[])
{
FILE *pkg;
pkg = fopen(FILE, "rb");
if (pkg == NULL) {
return -1;
}
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
const int hashBufferSize = 1024;
unsigned char hashBuffer[hashBufferSize];
int bytesRead;
while ((byesRead = fread(hashBuffer, 1, hashBufferSize, pkg)) != 0) {
SHA256_Update(&sha256, hashBuffer, bytesRead);
}
SHA256_Final(hash, &sha256);
fclose(pkg);
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
printf("%02x", hash[i]);
}
}

View File

@@ -1,27 +0,0 @@
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
int
run_make_task(char task[])
{
char *build_args[] = {
"/bin/make",
task,
NULL
};
execvp(
"/bin/make",
build_args
);
if (errno != 0 ) {
return errno;
}
}
int
main()
{
run_make_task("help");
}

27
src/config.h Normal file
View File

@@ -0,0 +1,27 @@
/*
* config.h - Various compile-time settings 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/>.
*/
#ifndef CONFIG_H_
#define CONFIG_H_
/* LG_VERBOSE - whether to use verbose logging for library events or not
*
* 0 is false, 1 is true
*/
#define LG_VERBOSE 0
#endif

851
src/libglacier.c Normal file
View File

@@ -0,0 +1,851 @@
/*
* libglacier.c - Backend C library for Glacier
*
* 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 <color.h>
#include <dirent.h>
#include <errno.h>
#include <libconfig.h>
#include <locale.h>
#include <openssl/evp.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <wchar.h>
#include "config.h"
/* Buffer and size constants */
#define BUFFER_SIZE 1024
#define MAX_CHILDREN 64
#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
*
* Descriptions are given as comments after the variable declaration.
* DEFINED IN: globals.h
*
*/
config_t cfg; /* Context for libconfig */
config_setting_t *setting = NULL; /* Pointer for setting */
/* Configuration variables with default values */
int GLACIER_ALLOW_SERVICES = 0; /* Declaration of GLACIER_ALLOW_SERVICES as given in glacier.cfg */
char *GLACIER_ALLOWED_LICENSES = NULL; /* Declaration of GLACIER_ALLOWED_LICENSES 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 */
/* Profile variables */
const char *GLACIER_REPO = NULL; /* Declaration of GLACIER_REPO as defined in profile.cfg */
const char *GLACIER_ARCH = NULL; /* Declaration of GLACIER_ARCH as defined in profile.cfg */
const char *GLACIER_TARGET = NULL; /* Declaration of GLACIER_TARGET 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 */
const char *runtime_files[] = {
"/etc/glacier.cfg",
"/etc/glacier/call-hooks",
"/etc/make.conf"
};
FILE *expected_hash; /* File pointer for expected hash */
FILE *pkg; /* File pointer for package hash */
/*
* END GLOBAL VARIABLES
*/
/*
* node
*
* DESCRIPTION: Definition of node type.
* DEFINED IN: data.h
*
*/
struct node {
char *data;
struct node *children[MAX_CHILDREN];
int numChildren;
struct node *left;
struct node *right;
};
/*
* infolog
*
* DESCRIPTION: Output a stylized info message.
* PARAMETERS: char MSG[]
* DEFINED IN: log.h
*
*/
void
infolog(char MSG[])
{
if (MSG == NULL) {
return;
}
printf(COL_BLUE "[i]" COL_RESET " %s\n", MSG);
return;
}
/*
* warnlog
*
* DESCRIPTION: Output a stylized warning message.
* Parameters: char MSG[]
* DEFINED IN: log.h
*
*/
void
warnlog(char MSG[])
{
if (MSG == NULL) {
return;
}
printf(COL_YELLOW "[!]" COL_RESET " %s\n", MSG);
return;
}
/*
* errlog
*
* DESCRIPTION: Output a stylized error message.
* PARAMETERS: char MSG[]
* DEFINED IN: log.h
*
*/
void
errlog(char MSG[])
{
if (MSG == NULL) {
return;
}
fprintf(stderr, COL_RED "[x]" COL_RESET " %s\n", MSG);
return;
}
/*
* successlog
*
* DESCRIPTION: Output a stylized success message.
* PARAMETERS: char MSG[]
* DEFINED IN: log.h
*
*/
void
successlog(char MSG[])
{
if (MSG == NULL) {
return;
}
printf(COL_GREEN "[*]" COL_RESET " %s\n", MSG);
return;
}
/*
* runtime_exists
*
* DESCRIPTION: Check if necesary runtime files exist.
* PARAMETERS: None.
* DEFINED IN: runtime.h
*
*/
int
runtime_exists(void)
{
int f;
for (f = 0; f < sizeof(runtime_files); f++) {
if (access(runtime_files[f], F_OK) == 0) {
continue;
} else {
/* might keep this message, might not, idk */
/* errlog("Runtime files are missing, cannot continue with operation.");
printf(COL_RED "[x]" COL_RESET " The following files are missing:\n");
printf(COL_RED "[x]" COL_RESET " \t%s\n", runtime_files[f]); */
return 0;
}
}
return 1;
}
/*
* is_process_root
*
* DESCRIPTION: Check if process is running as root.
* PARAMETERS: char MSG[]
* DEFINED IN: runtime.h
*
*/
int
is_process_root(void)
{
if (getuid() != 0) {
return 0; /* process is not running as root */
}
else {
return 1; /* process is running as root */
}
}
/*
* init_config
*
* DESCRIPTION: Initialize libconfig.
* PARAMETERS: None.
* DEFINED IN: config.h
*
*/
int
init_config(void)
{
config_init(&cfg);
if (! config_read_file(&cfg, runtime_files[0])) {
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
return(EXIT_FAILURE);
}
if (LG_VERBOSE == 1) {
infolog("Initialized libconfig");
}
return 0;
}
/*
* die_config
*
* DESCRIPTION: Kill libconfig.
* PARAMETERS: None.
* DEFINED IN: config.h
*
*/
int
die_config(void)
{
config_destroy(&cfg);
if (LG_VERBOSE == 1) {
infolog("Destroyed libconfig");
}
return(EXIT_SUCCESS);
}
/*
* load_all_from_config
*
* DESCRIPTION: Loads all settings from the Glacier config file.
* PARAMETERS: None.
* DEFINED IN: config.h
*
*/
int
load_all_from_config(void)
{
/* this is probably really ugly but it works */
if (! config_lookup_bool(&cfg, "GLACIER_DO_INT_CHECK", &GLACIER_DO_INT_CHECK)) { return 1; }
if (! config_lookup_bool(&cfg, "GLACIER_VERBOSE", &GLACIER_VERBOSE)) { return 1; }
return 0;
}
/*
* load_all_from_profile
*
* DESCRIPTION: Loads all settings from the Glacier system profile.
* PARAMETERS: None.
* DEFINED IN: config.h
*
*/
int
load_all_from_profile(void)
{
if (! config_lookup_string(&cfg, "GLACIER_REPO", &GLACIER_REPO)) { return 1; }
if (! config_lookup_string(&cfg, "GLACIER_ARCH", &GLACIER_ARCH)) { return 1; }
if (! config_lookup_string(&cfg, "GLACIER_TARGET", &GLACIER_TARGET)) { return 1; }
if (! config_lookup_string(&cfg, "GLACIER_SYSTEM_PROFILE", &GLACIER_SYSTEM_PROFILE)) { return 1; }
return 0;
}
/*
* load_setting_from_config
*
* DESCRIPTION: Load a specified setting from the Glacier config file.
* PARAMETERS: char SETTING[]
* DEFINED IN: config.h
*
*/
/* int
load_setting_from_config(char SETTING[])
{} */
/*
* create_node
*
* DESCRIPTION: Create a dependency tree node.
* PARAMETERS: char *data
* DEFINED IN: data.h
*
*/
struct node
*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));
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->left = NULL;
newNode->right = NULL;
for (int i = 0; i < MAX_CHILDREN; i++) {
newNode->children[i] = NULL;
}
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
*
* DESCRIPTION: Add a child node to a parent node.
* PARAMETERS: struct node *parent, struct node *child
* DEFINED IN: data.h
*
*/
int
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) {
parent->children[parent->numChildren++] = child;
return 0;
} else {
if (LG_VERBOSE == 1) {
errlog("Maximum number of children exceeded");
}
return 3;
}
}
/*
* print_tree
*
* DESCRIPTION: Print a dependency tree.
* PARAMETERS: struct node *root, int level
* DEFINED IN: data.h
*
*/
int
print_tree(struct node *root, int level)
{
if (root == NULL) {
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++) {
printf(" ");
}
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++) {
if (print_tree(root->children[i], level + 1) != 0) {
/* Propagate errors up the call stack */
return 4;
}
}
return 0;
}
/*
* mkworkspace
*
* DESCRIPTION: Creates a new Glacier workspace in /tmp.
* PARAMETERS: None.
* DEFINED IN: pkgops.h
*
*/
int
mkworkspace(void)
{
const char *workspace_path = "/tmp/glacier-workspace";
DIR* workspace = opendir(workspace_path);
if (workspace) {
/* Workspace exists */
closedir(workspace);
/* infolog("Not creating new workspace, valid workspace already exists."); */
return 0;
} else if (ENOENT == errno) {
/* infolog("Creating new Glacier workspace..."); */
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;
} else {
/* Some other error occurred */
if (LG_VERBOSE == 1) {
errlog("in mkworkspace()");
errlog("opendir() failed to check workspace");
fprintf(stderr, "Error: %s\n", strerror(errno));
}
return -1;
}
}
/*
* prepare_pkg
*
* DESCRIPTION: Copies a package archive from the localdb to the workspace, and unpacks it.
* PARAMETERS: char PACKAGE[]
* DEFINED IN: pkgops.h
*
*/
int
prepare_pkg(char PACKAGE[])
{
if (PACKAGE == NULL) {
printf(COL_RED "[x] " COL_RESET "Package '%s' does not exist in the local database.\n", PACKAGE);
errlog("Ensure your local database is up to date and try again.");
errlog("This can be done by running 'glacier-update-pkgdb' as root.");
return 1;
} else {
char PKG_NEW[PATH_MAX_SIZE];
PKG_NEW[0] = '\0'; /* Initialize the string buffer */
snprintf(PKG_NEW, sizeof(PKG_NEW), "/tmp/glacier-workspace/%s.tar", PACKAGE);
FILE *pkg_old, *pkg_new;
char buffer[BUFFER_SIZE];
size_t bytes_read;
pkg_old = fopen(PACKAGE, "r");
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;
}
/* Use buffered I/O for better efficiency */
while ((bytes_read = fread(buffer, 1, sizeof(buffer), pkg_old)) > 0) {
if (fwrite(buffer, 1, bytes_read, pkg_new) != bytes_read) {
errlog("Failed to write to destination file");
fclose(pkg_old);
fclose(pkg_new);
return 1;
}
}
fclose(pkg_old);
fclose(pkg_new);
/* 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 *tar_args[] = {
"/bin/tar", /* This should be changed to /glacier/bin/tar later on */
"-xvf",
PKG_NEW,
NULL,
};
execvp("/bin/tar", tar_args);
/* If we get here, execvp failed */
fprintf(stderr, COL_RED "[x] " COL_RESET "Failed to execute tar: %s\n", strerror(errno));
exit(EXIT_FAILURE);
} else {
/* Parent process */
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;
}
}
/* Should never reach here, but added for completeness */
return 1;
}
/*
* run_make_task
*
* DESCRIPTION: Runs a make task in current working directory
* PARAMETERS: char TASK[]
* DEFINED IN: pkgops.h
*
*/
int
run_make_task(char TASK[])
{
if (TASK == NULL) {
errlog("No task specified for make");
return 1;
}
/* 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;
}
/*
* print_hash
*
* DESCRIPTION: Prints a specified hash string
* PARAMETERS: unsigned char *hash, unsigned int length
* DEFINED IN: security.h
*
*/
int
print_hash(uchar *hash, uint length)
{
if (hash == NULL) {
errlog("NULL hash pointer passed to print_hash()");
return 1;
}
if (length == 0) {
warnlog("Zero-length hash passed to print_hash()");
}
for (uint index = 0; index < length; index++) {
printf("%02x", hash[index]);
}
printf("\n");
return 0;
}
/*
* 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;
}
if (stored_hash_size < (length * 2 + 1)) {
if (LG_VERBOSE == 1) {
errlog("in stash_hash()");
errlog("Output buffer too small for hash");
}
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();
}