I now start the listener in the main.c file rather than proxy given that I didn't feel proxy was the right place if a normal (non-proxied) request came in. webserver.{c,h} and proxy.{c,h} had some changes relating to this. The config changed slightly - we now create a folder in ~/.config/ called yaip. This is where certificates and so on will be stored along with the user configuration I created a helper function to get files inside this directory (it changes based on xdg_config_home) and updated relevant tests. In ssl.{c,h} I have started work. If they don't exist, the tool now creates and stores a key and certificate for the CA that this tool will need to pretend to be. I still need to write tests for this.
98 lines
2.5 KiB
C
98 lines
2.5 KiB
C
#include "config.h"
|
|
|
|
/*
|
|
* Checks if the given path exists by calling stat().
|
|
*
|
|
*/
|
|
bool path_exists(const char *path) {
|
|
struct stat buf;
|
|
return (stat(path, &buf) == 0);
|
|
}
|
|
|
|
/*
|
|
* This function resolves ~ in pathnames.
|
|
*/
|
|
char* resolveTilde(const char *path) {
|
|
static glob_t globbuf;
|
|
static char *ret = NULL;
|
|
|
|
int res = glob(path, GLOB_TILDE, NULL, &globbuf);
|
|
/* no match, or many wildcard matches are bad */
|
|
if (res == GLOB_NOMATCH)
|
|
ret = strdup(path);
|
|
else if (res != 0) {
|
|
printf("glob() failed\n");
|
|
} else {
|
|
ret = strdup(globbuf.gl_pathv[0]);
|
|
}
|
|
globfree(&globbuf);
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* Returns a pointer to a config object containing defaults
|
|
*/
|
|
Config* configDefaults(){
|
|
Config *conf = malloc( sizeof( Config ) );
|
|
conf->database = strdup("proxy.sqlite");
|
|
conf->port = 8080;
|
|
conf->localConfig = strdup("proxy.conf");
|
|
conf->userConfig = getUserConfigFile("proxy.conf");
|
|
conf->certfile = getUserConfigFile("cert.pem");
|
|
conf->keyfile = getUserConfigFile("key.pem");
|
|
|
|
return conf;
|
|
}
|
|
|
|
void maybeMakeDir(const char *path){
|
|
if ( path_exists( path ) ) return;
|
|
mkdir( path, 0700 );
|
|
}
|
|
|
|
char* getConfigDir(){
|
|
char *xdg_config_home;
|
|
if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
|
|
xdg_config_home = resolveTilde("~/.config");
|
|
|
|
char configDir[strlen(xdg_config_home) + 6];
|
|
memset(configDir, '\0', strlen(xdg_config_home) + 6);
|
|
|
|
sprintf( configDir, "%s/yaip", xdg_config_home );
|
|
|
|
return strdup(configDir);
|
|
}
|
|
|
|
char* getUserConfigFile(char *filename){
|
|
//This doesn't end in a slash
|
|
char *configDir = getConfigDir();
|
|
//Make sure our filename doesn't start with a slash
|
|
while ( filename[0] == '/' ) filename++;
|
|
// Allocate space for both parts, a slash and a \0
|
|
char retFile[strlen(configDir) + strlen(filename) + 2];
|
|
|
|
memset(retFile, '\0', strlen(configDir) + strlen(filename) + 2);
|
|
|
|
sprintf(retFile, "%s/%s", configDir, filename);
|
|
|
|
return strdup(retFile);
|
|
}
|
|
|
|
|
|
void setConfig(Config *config, char option[], char value[]){
|
|
if ( strcmp( option, "database" ) == 0 ){
|
|
config->database = value;
|
|
} else if ( strcmp( option, "localConfig" ) == 0 ){
|
|
config->localConfig = value;
|
|
} else if ( strcmp( option, "userConfig" ) == 0 ){
|
|
config->userConfig = value;
|
|
} else if ( strcmp( option, "port" ) == 0 ){
|
|
config->port = atoi(value);
|
|
}
|
|
}
|
|
|
|
void printConfig(Config *config){
|
|
printf("Database: %s\n", config->database);
|
|
printf("localConfig: %s\n", config->localConfig);
|
|
printf("userConfig: %s\n", config->userConfig);
|
|
printf("port: %i\n", config->port);
|
|
}
|