Improvements and more sanity
This commit is contained in:
parent
5021e5a86b
commit
c0cf20b869
20
inc/fex.h
Normal file
20
inc/fex.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
//
|
||||||
|
// Created by auric on 2/25/24.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef FEX_H
|
||||||
|
#define FEX_H
|
||||||
|
|
||||||
|
enum Flags {
|
||||||
|
FLAG_INSTALL = 1 << 0,
|
||||||
|
FLAG_SOURCE = 1 << 1,
|
||||||
|
FLAG_UNINSTALL = 1 << 2,
|
||||||
|
FLAG_LIST = 1 << 3,
|
||||||
|
FLAG_SEARCH = 1 << 4,
|
||||||
|
FLAG_FORCE = 1 << 5,
|
||||||
|
FLAG_UPGRADE = 1 << 6,
|
||||||
|
};
|
||||||
|
|
||||||
|
extern char* flagNames[];
|
||||||
|
|
||||||
|
#endif //FEX_H
|
14
inc/sanity.h
14
inc/sanity.h
@ -5,13 +5,15 @@
|
|||||||
#ifndef SANITY_H
|
#ifndef SANITY_H
|
||||||
#define SANITY_H
|
#define SANITY_H
|
||||||
|
|
||||||
#define SANITY_SUCCESS 0 // 0000 - All checks passed
|
#define SANITY_SUCCESS 0 // 0b00000 - All checks passed
|
||||||
#define SANITY_CON_FAIL 1 // 0001 - /con check failed
|
#define SANITY_CON_FAIL 1 // 0b00001 - /con check failed
|
||||||
#define SANITY_EXE_FAIL 2 // 0010 - /exe check failed
|
#define SANITY_EXE_FAIL 2 // 0b00010 - /exe check failed
|
||||||
#define SANITY_UTI_FAIL 4 // 0100 - /uti check failed
|
#define SANITY_UTI_FAIL 4 // 0b00100 - /uti check failed
|
||||||
#define SANITY_LIB_FAIL 8 // 1000 - /uti/lib check failed
|
#define SANITY_LIB_FAIL 8 // 0b01000 - /uti/lib check failed
|
||||||
#define SANITY_HDR_FAIL 16 // 100000 - /uti/hdr check failed
|
#define SANITY_HDR_FAIL 16 // 0b10000 - /uti/hdr check failed
|
||||||
|
|
||||||
|
|
||||||
int isSysSane();
|
int isSysSane();
|
||||||
|
int isSyntaxSane(unsigned int flags, int pkgCount);
|
||||||
|
|
||||||
#endif //SANITY_H
|
#endif //SANITY_H
|
||||||
|
BIN
obj/sanity.o
BIN
obj/sanity.o
Binary file not shown.
59
src/fex.c
59
src/fex.c
@ -6,30 +6,22 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <nprint.h>
|
#include <nprint.h>
|
||||||
#include <stdbool.h>
|
|
||||||
#include <sanity.h>
|
#include <sanity.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
#include "fex.h"
|
||||||
enum Flags {
|
|
||||||
FLAG_INSTALL = 1 << 0,
|
|
||||||
FLAG_UPDATE = 1 << 1,
|
|
||||||
FLAG_SOURCE = 1 << 2,
|
|
||||||
FLAG_UNINSTALL = 1 << 3,
|
|
||||||
FLAG_LIST = 1 << 4,
|
|
||||||
FLAG_SEARCH = 1 << 5,
|
|
||||||
FLAG_FORCE = 1 << 6,
|
|
||||||
FLAG_UPGRADE = 1 << 7,
|
|
||||||
};
|
|
||||||
|
|
||||||
void help() {
|
void help() {
|
||||||
nprint("E", "Usage: fex (OPT) (PACKAGES)");
|
nprint("E", "Usage: fex (OPT) (PACKAGES)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char* flagNames[] = {"INSTALL", "SOURCE", "UNINSTALL", "LIST", "SEARCH", "FORCE", "UPGRADE"};
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
unsigned short flags = 0;
|
unsigned int flags = 0;
|
||||||
char* packages[argc - 1];
|
char* packages[argc - 1];
|
||||||
int pkgCount = 0;
|
int pkgCount = 0;
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
help();
|
help();
|
||||||
return 1;
|
return 1;
|
||||||
@ -42,25 +34,22 @@ int main(int argc, char** argv) {
|
|||||||
case 'i': // install, 0
|
case 'i': // install, 0
|
||||||
flags |= FLAG_INSTALL;
|
flags |= FLAG_INSTALL;
|
||||||
break;
|
break;
|
||||||
case 'u': // update, 1
|
case 'S': // source, 1
|
||||||
flags |= FLAG_UPDATE;
|
|
||||||
break;
|
|
||||||
case 'S': // source, 2
|
|
||||||
flags |= FLAG_SOURCE;
|
flags |= FLAG_SOURCE;
|
||||||
break;
|
break;
|
||||||
case 'r': // remove, 3
|
case 'r': // remove, 2
|
||||||
flags |= FLAG_UNINSTALL;
|
flags |= FLAG_UNINSTALL;
|
||||||
break;
|
break;
|
||||||
case 'l': // list, 4
|
case 'l': // list, 3
|
||||||
flags |= FLAG_LIST;
|
flags |= FLAG_LIST;
|
||||||
break;
|
break;
|
||||||
case 's': // search, 5
|
case 's': // search, 4
|
||||||
flags |= FLAG_SEARCH;
|
flags |= FLAG_SEARCH;
|
||||||
break;
|
break;
|
||||||
case 'f': // force, 6
|
case 'f': // force, 5
|
||||||
flags |= FLAG_FORCE;
|
flags |= FLAG_FORCE;
|
||||||
break;
|
break;
|
||||||
case 'c': // upgrade; don't ask why I'm using 'c', 7
|
case 'u': // upgrade, 6
|
||||||
flags |= FLAG_UPGRADE;
|
flags |= FLAG_UPGRADE;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -74,22 +63,32 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const int sysSanity = isSysSane();
|
const int sysSanity = isSysSane();
|
||||||
|
if ((isSyntaxSane(flags, pkgCount))) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef debugEnabled
|
#ifdef debugEnabled
|
||||||
DEBUG_PRINT("sysSanity: %d", sysSanity);
|
DEBUG_PRINT("sysSanity: %d", sysSanity);
|
||||||
unsigned int flag_values[] = {0b00000001, 0b00000010, 0b00000100, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000};
|
unsigned int flagValues[] = {0b0000001, 0b0000010, 0b0000100, 0b0001000, 0b0010000, 0b0100000, 0b1000000};
|
||||||
char* flag_names[] = {"Install", "Update", "Source", "Uninstall", "List", "Search", "Force", "Upgrade"};
|
int numFlags = sizeof(flagValues) / sizeof(flagValues[0]);
|
||||||
int num_flags = sizeof(flag_values) / sizeof(flag_values[0]);
|
for(int i = 0; i < numFlags; i++) {
|
||||||
for(int i = 0; i < num_flags; i++) {
|
if (flags & flagValues[i]) {
|
||||||
if (flags & flag_values[i]) {
|
nprint("D", "%s flag is set.", flagNames[i]);
|
||||||
nprint("D", "%s flag is set.", flag_names[i]);
|
|
||||||
} else {
|
} else {
|
||||||
nprint("D", "%s flag is not set.", flag_names[i]);
|
nprint("D", "%s flag is not set.", flagNames[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for(int i = 0; i < pkgCount; i++) {
|
for(int i = 0; i < pkgCount; i++) {
|
||||||
nprint("D", "Package %d: %s", i, packages[i]);
|
nprint("D", "Package %d: %s", i, packages[i]);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* End Prelogic */
|
||||||
|
DEBUG_PRINT("[!] Break parselogic");
|
||||||
|
|
||||||
|
for(int i = 0; i < numFlags; i++) {
|
||||||
|
DEBUG_PRINT("Iterating over %s", flagNames[i]);
|
||||||
|
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
61
src/sanity.c
61
src/sanity.c
@ -1,33 +1,58 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include "../inc/sanity.h"
|
#include "../inc/sanity.h"
|
||||||
|
#include "../inc/fex.h"
|
||||||
|
#include "../inc/nprint.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 0: success
|
* Uses bitwise operations to determine the sanity of the system's directory hierarchy. In particular, it checks if the directories of
|
||||||
* 1: con failure
|
* /con
|
||||||
* 2: exe failure
|
* /exe
|
||||||
* 3: con and exe failure
|
* /uti
|
||||||
* 4: uti failure
|
* /uti/lib
|
||||||
* 5: uti and con failure
|
* /uti/hdr
|
||||||
* 6: uti and exe failure
|
* exist, and if the owner can write to them. This notably does not check if the program is run as superuser.
|
||||||
* 7: total failure
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* For a root check - simply use conditional checking for a zero value in some way, such as !(getuid()) */
|
||||||
|
|
||||||
int isSysSane() {
|
int isSysSane() {
|
||||||
struct stat conStat, exeStat, utiStat;
|
struct stat conStat, exeStat, utiStat, libStat, hdrStat;
|
||||||
int result = 0;
|
int result = 0;
|
||||||
|
|
||||||
if (stat("/con", &conStat) != 0 || !(conStat.st_mode & S_IWUSR)) {
|
char* directoryNames[] = {"/con", "/exe", "/uti", "/uti/lib", "/uti/hdr"};
|
||||||
result |= SANITY_CON_FAIL;
|
struct stat* statVars[] = {&conStat, &exeStat, &utiStat, &libStat, &hdrStat};
|
||||||
}
|
int flags[] = {SANITY_CON_FAIL, SANITY_EXE_FAIL, SANITY_UTI_FAIL, SANITY_LIB_FAIL, SANITY_HDR_FAIL};
|
||||||
|
const int numDirectories = sizeof(directoryNames) / sizeof(directoryNames[0]);
|
||||||
|
|
||||||
if (stat("/exe", &exeStat) != 0 || !(exeStat.st_mode & S_IWUSR)) {
|
for(int i = 0; i < numDirectories; i++) {
|
||||||
result |= SANITY_EXE_FAIL;
|
if(stat(directoryNames[i], statVars[i]) != 0 || !(statVars[i]->st_mode & S_IWUSR)) {
|
||||||
}
|
result |= flags[i];
|
||||||
|
|
||||||
if (stat("/uti", &utiStat) != 0 || !(utiStat.st_mode & S_IWUSR)) {
|
|
||||||
result |= SANITY_UTI_FAIL;
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int isSyntaxSane(unsigned int flags, int pkgCount) {
|
||||||
|
if((flags & FLAG_INSTALL) && (flags & FLAG_UNINSTALL)) {
|
||||||
|
nprint("E", "You cannot install and uninstall at the same time"); // TODO: allow for this to happen by parsing the flags in order. e.g fex -i japes -u japes2 should install japes and uninstall japes2
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(((flags & FLAG_INSTALL) || (flags & FLAG_UNINSTALL)) && (pkgCount == 0)) {
|
||||||
|
nprint("E", "You must specify at least one package");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(flags == FLAG_FORCE) {
|
||||||
|
nprint("E", "Force may not be used alone");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if((flags == FLAG_LIST) || (flags == FLAG_SEARCH)) {
|
||||||
|
nprint("E", "List and search must be used alone");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user