Compare commits
14 Commits
8140187d22
...
v0.2.0/imp
| Author | SHA1 | Date | |
|---|---|---|---|
| 0b03b9b954 | |||
| 25a55609ec | |||
| bb6bc22935 | |||
| 1a32ab4009 | |||
| 923df644a0 | |||
| 7258ef5810 | |||
| 60996e3de2 | |||
| f94ed28d93 | |||
| 1f746e77ed | |||
| e5af8df970 | |||
| 1a18d642e0 | |||
| 92d25f7efe | |||
| 0c3f2db582 | |||
| 87e9f06315 |
71
Makefile
71
Makefile
@@ -1,40 +1,69 @@
|
||||
#
|
||||
# Makefile
|
||||
#
|
||||
|
||||
IDIR = ./include
|
||||
BDIR = ./build
|
||||
SDIR = ./src
|
||||
TDIR = ./tests
|
||||
|
||||
include config.mk
|
||||
|
||||
COLOR_GREEN=\033[0;32m
|
||||
COLOR_BLUE=\033[0;34m
|
||||
COLOR_RESET=\033[0m
|
||||
|
||||
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."
|
||||
|
||||
help:
|
||||
@echo "Make options for libglacier:"
|
||||
@echo ""
|
||||
@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"
|
||||
|
||||
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
|
||||
cp etc/example.cfg ./glacier.cfg
|
||||
|
||||
lib:
|
||||
lib: $(SDIR)/libglacier.c
|
||||
mkdir build
|
||||
mkdir build/lib
|
||||
$(CC) libglacier.c -c $(LIBFLAGS) -o build/lib/libglacier.o
|
||||
$(AR) -rc build/lib/libglacier.a build/lib/libglacier.o
|
||||
$(CC) $(SDIR)/libglacier.c -c $(LIBFLAGS) -o $(BDIR)/lib/libglacier.o
|
||||
$(AR) -rc $(BDIR)/lib/libglacier.a build/lib/libglacier.o
|
||||
|
||||
install:
|
||||
@echo "$(COLOR_BLUE)[i]$(COLOR_RESET) Installing static library to $(PREFIX)/lib..."
|
||||
install lib/libglacier.a $(PREFIX)/lib
|
||||
install include/config.h $(PREFIX)/include/glacier
|
||||
install include/data.h $(PREFIX)/include/glacier
|
||||
install include/log.h $(PREFIX)/include/glacier
|
||||
install include/pkgops.h $(PREFIX)/include/glacier
|
||||
install include/runtime.h $(PREFIX)/include/glacier
|
||||
install include/security.h $(PREFIX)/include/glacier
|
||||
@echo "$(COLOR_GREEN)[*]$(COLOR_RESET) Finished installing static library to $(PREFIX)/lib."
|
||||
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
|
||||
@echo "[INFO]"
|
||||
@echo "[INFO] Installing library to PREFIX/lib..."
|
||||
@echo "[INFO]"
|
||||
install $(BDIR)/lib/libglacier.a $(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:
|
||||
rm -rf build
|
||||
rm -rf $(BDIR) ./chroot
|
||||
|
||||
distclean:
|
||||
rm -rf $(BDIR) $(TDIR)/test-suite ./chroot
|
||||
|
||||
5233
chroot-utils/SAVED_CONFIG
Normal file
5233
chroot-utils/SAVED_CONFIG
Normal file
File diff suppressed because it is too large
Load Diff
29
chroot-utils/build_in_chroot.sh
Normal file
29
chroot-utils/build_in_chroot.sh
Normal 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 "$@"
|
||||
@@ -5,6 +5,6 @@
|
||||
CC = /bin/gcc
|
||||
AR = /bin/ar
|
||||
|
||||
LIBFLAGS = -lconfig
|
||||
LIBFLAGS = -lconfig -lcrypto
|
||||
|
||||
PREFIX = /usr
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* config.h - Function declarations for libglacier
|
||||
* config.h - Config loading for Glacier
|
||||
*
|
||||
* This file is part of Glacier.
|
||||
*
|
||||
@@ -18,83 +18,75 @@
|
||||
#ifndef GLACIERCONFIG_H_
|
||||
#define GLACIERCONFIG_H_
|
||||
|
||||
#include <libconfig.h>
|
||||
|
||||
/* Constants */
|
||||
#ifndef LG_VERBOSE
|
||||
#define LG_VERBOSE 0
|
||||
#endif
|
||||
|
||||
/*
|
||||
* 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) {
|
||||
* errlog("Failed to initialize libconfig");
|
||||
* return(EXIT_FAILURE); // fatal error requiring termination of program execution
|
||||
* }
|
||||
* else {
|
||||
* successlog("Initialized libconfig"); // output automatically if LG_VERBOSE = 1
|
||||
* }
|
||||
* DESCRIPTION: Initialize libconfig with required configs
|
||||
* PARAMETERS:
|
||||
* None.
|
||||
* RETURN VALUES:
|
||||
* 0 on success, 1 on failure
|
||||
* CAVEATS:
|
||||
* This MUST be called before ANY other config function is.
|
||||
* EXAMPLE:
|
||||
* init_config();
|
||||
*/
|
||||
|
||||
int init_config(void);
|
||||
|
||||
/**************************************************************************************************************/
|
||||
|
||||
/*
|
||||
* 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();
|
||||
*/
|
||||
|
||||
int die_config(void);
|
||||
|
||||
/**************************************************************************************************************/
|
||||
|
||||
/*
|
||||
* load_all_from_config
|
||||
* DESCRIPTION: Initialize all settings from glacier.cfg.
|
||||
*
|
||||
* DESCRIPTION: load_all_from_config loads all settings from the config file.
|
||||
* PARAMETERS:
|
||||
* None. (void)
|
||||
* None.
|
||||
* RETURN VALUES:
|
||||
* 0 on success, 1 on file does not exist, 2 on library error
|
||||
* 0 on success, 1 on failure
|
||||
* CAVEATS:
|
||||
* None.
|
||||
* EXAMPLE:
|
||||
* load_all_from_config();
|
||||
*/
|
||||
|
||||
int load_all_from_config();
|
||||
|
||||
/**************************************************************************************************************/
|
||||
int load_all_from_config(void);
|
||||
|
||||
/*
|
||||
* [[[ DEPRECATED ]]]
|
||||
* load_setting_from_config
|
||||
* DESCRIPTION: Initialize a specified from glacier.cfg.
|
||||
* load_all_from_profile
|
||||
*
|
||||
* DESCRIPTION: load_all_from_profile loads all settings from the profile file.
|
||||
* PARAMETERS:
|
||||
* char SETTING[] -> The setting to initialize
|
||||
* None.
|
||||
* 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:
|
||||
* None.
|
||||
* EXAMPLE:
|
||||
* load_setting_from_config();
|
||||
* load_all_from_profile();
|
||||
*/
|
||||
|
||||
/* int load_setting_from_config(char SETTING[]); */
|
||||
int load_all_from_profile(void);
|
||||
|
||||
#endif
|
||||
|
||||
198
include/data.h
198
include/data.h
@@ -20,6 +20,21 @@
|
||||
|
||||
#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
|
||||
*
|
||||
@@ -27,16 +42,30 @@
|
||||
* PARAMETERS:
|
||||
* char *data -> The name of the node to create
|
||||
* RETURN VALUES:
|
||||
* None.
|
||||
* A pointer to the created node on success, NULL on failure
|
||||
* CAVEATS:
|
||||
* None.
|
||||
* Caller must free the node using free_node when done
|
||||
* EXAMPLE:
|
||||
* struct node package = create_node("Package");
|
||||
* struct node *package = create_node("Package");
|
||||
*/
|
||||
|
||||
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
|
||||
@@ -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 *child -> The child node which will be added to the parent node
|
||||
* RETURN VALUES:
|
||||
* 1 on maximum children exceeded
|
||||
* 0 on success, 1-3 for different error conditions
|
||||
* CAVEATS:
|
||||
* None.
|
||||
* EXAMPLE:
|
||||
* add_child(package, dep1);
|
||||
*/
|
||||
|
||||
void add_child(struct node *parent, struct node *child);
|
||||
|
||||
/**************************************************************************************************************/
|
||||
int add_child(struct node *parent, struct node *child);
|
||||
|
||||
/*
|
||||
* print_tree
|
||||
@@ -65,164 +92,13 @@ void add_child(struct node *parent, struct node *child);
|
||||
* struct node *root -> The tree to print
|
||||
* int level -> The number of levels to descend
|
||||
* RETURN VALUES:
|
||||
* None.
|
||||
* 0 on success, non-zero on error
|
||||
* CAVEATS:
|
||||
* None.
|
||||
* EXAMPLE:
|
||||
* print_tree(package, 0);
|
||||
*/
|
||||
|
||||
void 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);
|
||||
int print_tree(struct node *root, int level);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -18,22 +18,34 @@
|
||||
#ifndef 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_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_VERBOSE;
|
||||
|
||||
/* Profile variables */
|
||||
extern const char *GLACIER_REPO;
|
||||
extern const char *GLACIER_ARCH;
|
||||
extern const char *GLACIER_TARGET;
|
||||
extern const char *GLACIER_LOCALDB;
|
||||
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;
|
||||
|
||||
|
||||
@@ -18,6 +18,12 @@
|
||||
#ifndef 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
|
||||
*
|
||||
@@ -25,14 +31,14 @@
|
||||
* PARAMETERS:
|
||||
* None.
|
||||
* RETURN VAUES:
|
||||
* 0 on success, 2 on library error
|
||||
* 0 on workspace already exists, 1 on workspace created, -1 on error
|
||||
* CAVEATS:
|
||||
* None.
|
||||
* EXAMPLE:
|
||||
* mkworkspace();
|
||||
*/
|
||||
|
||||
int mkworkspace();
|
||||
int mkworkspace(void);
|
||||
|
||||
/*
|
||||
* prepare_pkg
|
||||
@@ -41,7 +47,7 @@ int mkworkspace();
|
||||
* PARAMETERS:
|
||||
* char PACKAGE[] -> The package file to prepare
|
||||
* 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:
|
||||
* The example presented is bad. You should be calling the system profile variable
|
||||
* rather than manually specifying one.
|
||||
@@ -58,7 +64,7 @@ int prepare_pkg(char PACKAGE[]);
|
||||
* PARAMETERS:
|
||||
* char TASK[] -> The make task to run
|
||||
* RETURN VAUES:
|
||||
* 0 on success, 1 on failure
|
||||
* 0 on success, other values for specific errors
|
||||
* CAVEATS:
|
||||
* 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.
|
||||
|
||||
@@ -18,6 +18,9 @@
|
||||
#ifndef GLACIERSECURITY_H_
|
||||
#define GLACIERSECURITY_H_
|
||||
|
||||
typedef unsigned int uint;
|
||||
typedef unsigned char uchar;
|
||||
|
||||
/*
|
||||
* compare_file_hash
|
||||
*
|
||||
@@ -35,4 +38,61 @@
|
||||
|
||||
/* 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
|
||||
|
||||
680
libglacier.c
680
libglacier.c
@@ -1,680 +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
|
||||
*
|
||||
* Descriptions are given as comments after the variable declaration.
|
||||
* DEFINED IN: globals.h
|
||||
*
|
||||
*/
|
||||
|
||||
config_t cfg; /* Context for libconfig */
|
||||
config_setting_t *setting; /* 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 */
|
||||
char GLACIER_ALLOWED_LICENSES; /* Declaration of GLACIER_ALLOWED_LICENSES as given in glacier.cfg */
|
||||
int GLACIER_DO_INT_CHECK; /* Declaration of GLACIER_DO_INT_CHECK as given in glacier.cfg */
|
||||
int GLACIER_VERBOSE; /* Declaration of GLACIER_VERBOSE as given in glacier.cfg */
|
||||
|
||||
const char *GLACIER_REPO; /* Declaration of GLACIER_REPO as defined in profile.cfg */
|
||||
const char *GLACIER_ARCH; /* Declaration of GLACIER_ARCH as defined in profile.cfg */
|
||||
const char *GLACIER_TARGET; /* Declaration of GLACIER_TARGET as defined in profile.cfg */
|
||||
const char *GLACIER_LOCALDB; /* Declaration of GLACIER_LOCALDB as defined in profile.cfg */
|
||||
const char *GLACIER_SYSTEM_PROFILE; /* 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;
|
||||
};
|
||||
|
||||
/*
|
||||
* 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)
|
||||
{
|
||||
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: 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: 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);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 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;
|
||||
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");
|
||||
}
|
||||
|
||||
/*
|
||||
* mkworkspace
|
||||
*
|
||||
* DESCRIPTION: Creates a new Glacier workspace in /tmp.
|
||||
* PARAMETERS: None.
|
||||
* DEFINED IN: 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: 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);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 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[])
|
||||
{
|
||||
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: 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]);
|
||||
}
|
||||
}
|
||||
*/
|
||||
27
src/config.h
Normal file
27
src/config.h
Normal 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
851
src/libglacier.c
Normal 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
83
tests/unit-tests.c
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user