Yet Another Intercepting Proxy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

98 lines
2.5 KiB

#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);
}