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.
162 lines
3.4 KiB
162 lines
3.4 KiB
|
|
#include <string>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include "cfg.h"
|
|
|
|
std::string Config::GetFilenameInHome () {
|
|
std::string res = "";
|
|
char *env = NULL;
|
|
|
|
env = secure_getenv("HOME");
|
|
if (env != NULL)
|
|
res = env;
|
|
|
|
res = res + "/" + FBSH_CFGFILE;
|
|
|
|
return res;
|
|
}
|
|
|
|
|
|
std::string Config::GetDefaultSIDFile () {
|
|
std::string res = "";
|
|
char *env = NULL;
|
|
|
|
env = secure_getenv("HOME");
|
|
if (env != NULL)
|
|
res = env;
|
|
|
|
res = res + "/" + FBSH_SIDFILE;
|
|
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* if saving password is disabled the password will be deleted.
|
|
* The configuration file must be saved manually..
|
|
*/
|
|
void Config::SetSavePassword(bool enable) {
|
|
savepassword = enable;
|
|
if (!savepassword) {
|
|
password = "";
|
|
username = "";
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
* the constructor will need to set all parameters to default,
|
|
* and read the configuration file.
|
|
*
|
|
* Since this class/object is defined as global object, it will
|
|
* do this before the command line parameters are read in the
|
|
* main function.
|
|
*/
|
|
Config::Config() {
|
|
filename = GetFilenameInHome();
|
|
sidfile = GetDefaultSIDFile();
|
|
username = "";
|
|
password = "";
|
|
savepassword = false;
|
|
silent = false;
|
|
LoadConfig();
|
|
}
|
|
|
|
|
|
/*
|
|
* nothing to do here, we have no autosave of the cofiguration file.
|
|
*/
|
|
Config::~Config() {
|
|
}
|
|
|
|
|
|
/*
|
|
* just load all parameters found in the configuration file into the configuration
|
|
*/
|
|
#define BUFSIZE 1024
|
|
int Config::LoadConfig() {
|
|
FILE *f = NULL;
|
|
char buffer[BUFSIZE];
|
|
char *val = NULL;
|
|
int i;
|
|
|
|
if ((f = fopen (filename.c_str(), "r")) == NULL) {
|
|
fprintf (stderr, "error on writing config '%s' : %s\n", filename.c_str(),
|
|
strerror(errno));
|
|
return -1;
|
|
}
|
|
|
|
while (fgets(buffer, BUFSIZE, f) != NULL) {
|
|
//
|
|
// check for lenght of buffer
|
|
// find parametername and value
|
|
buffer[BUFSIZE] = '\0';
|
|
i = strlen (buffer);
|
|
if (i > 0) buffer[i-1] = '\0';
|
|
val = strchr(buffer, '=');
|
|
if (val != NULL) {
|
|
*val = '\0';
|
|
val++;
|
|
}
|
|
else continue;
|
|
|
|
//
|
|
// copy found value into the Config variables
|
|
if (strncmp (buffer, "FBSH_SAVEPASSWORD", BUFSIZE) == 0)
|
|
savepassword = atoi(val);
|
|
else if (strncmp (buffer, "FBSH_SIDFILE", BUFSIZE) == 0)
|
|
sidfile = val;
|
|
else if (strncmp (buffer, "FBSH_USER", BUFSIZE) == 0)
|
|
username = val;
|
|
else if (strncmp (buffer, "FBSH_PASS", BUFSIZE) == 0)
|
|
password = val;
|
|
else if (strncmp (buffer, "FBSH_HOSTNAME", BUFSIZE) == 0)
|
|
fbhostname = val;
|
|
if (strncmp (buffer, "FBSH_SILENTMODE", BUFSIZE) == 0)
|
|
silent = atoi(val);
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
/*
|
|
* save configuration file, it will contains all default parameters and changed
|
|
* parameters given as command line parameter.
|
|
*/
|
|
int Config::SaveConfig() {
|
|
FILE *f = NULL;
|
|
|
|
if ((f = fopen (filename.c_str(), "w")) == NULL) {
|
|
fprintf (stderr, "error on writing config '%s' : %s\n", filename.c_str(),
|
|
strerror(errno));
|
|
return -1;
|
|
}
|
|
|
|
fprintf(f, "#\n");
|
|
fprintf(f, "# configfile for fbsh-cli\n");
|
|
fprintf(f, "# PARAMETERNAME=VALUE no space allowed except for the value.\n");
|
|
fprintf(f, "# but not between parametername, = and value.\n");
|
|
fprintf(f, "#\n");
|
|
fprintf(f, "#\n");
|
|
|
|
fprintf(f, "FBSH_SAVEPASSWORD=%d\n", savepassword);
|
|
if (savepassword) {
|
|
fprintf(f, "FBSH_USER=%s\n", username.c_str());
|
|
fprintf(f, "FBSH_PASS=%s\n", password.c_str());
|
|
}
|
|
fprintf(f, "FBSH_SIDFILE=%s\n", sidfile.c_str());
|
|
fprintf(f, "FBSH_HOSTNAME=%s\n", fbhostname.c_str());
|
|
fprintf(f, "FBSH_SILENTMODE=%d\n", silent);
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
}
|
|
|
|
|