| 1 |
/* eurephia_init.c -- program which initialises the eurephia database. |
| 2 |
* It will add a administrator account, setting |
| 3 |
* a password for it and set other needed |
| 4 |
* configuration settings. |
| 5 |
* |
| 6 |
* GPLv2 only - Copyright (C) 2009 - 2019 |
| 7 |
* David Sommerseth <[email protected]> |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms of the GNU General Public License |
| 11 |
* as published by the Free Software Foundation; version 2 |
| 12 |
* of the License. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program; if not, write to the Free Software |
| 21 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
/** |
| 26 |
* @file eurephia_init.c |
| 27 |
* @author David Sommerseth <[email protected]> |
| 28 |
* @date 2009-03-22 |
| 29 |
* |
| 30 |
* @brief A little console based wizard helping configuring new eurephia databases. |
| 31 |
* |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <stdio.h> |
| 35 |
#include <string.h> |
| 36 |
#include <libgen.h> |
| 37 |
#include <assert.h> |
| 38 |
|
| 39 |
#ifdef HAVE_LIBXML2 |
| 40 |
#include <libxml/tree.h> |
| 41 |
#include <libxml/xpath.h> |
| 42 |
#include <libxml/xmlstring.h> |
| 43 |
#endif |
| 44 |
|
| 45 |
#include <eurephia_nullsafe.h> |
| 46 |
#include <eurephia_context.h> |
| 47 |
#include <eurephiadb.h> |
| 48 |
#include <eurephiadb_driver.h> |
| 49 |
#include <eurephia_values.h> |
| 50 |
#include <eurephia_xml.h> |
| 51 |
|
| 52 |
#define MODULE "eurephia_init" /**< Set the module name to eurephia_init */ |
| 53 |
#include <client_context.h> |
| 54 |
#include <client_config.h> |
| 55 |
#include <argparser.h> |
| 56 |
#include <get_console_input.h> |
| 57 |
|
| 58 |
|
| 59 |
// Found in benchmark.c |
| 60 |
int benchmark(int *min, int *max, int thr_min, int thr_max); |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Prints program version information to stdout |
| 65 |
* |
| 66 |
* @param fprg String containing full path to this binary (argv[0]) |
| 67 |
* |
| 68 |
* @return Returns the basename value of the binary |
| 69 |
*/ |
| 70 |
char *print_version(char *fprg) { |
| 71 |
char *prg = basename(fprg); |
| 72 |
|
| 73 |
fprintf(stdout, "%s (v%s) - eurephia initialisation utility\n" |
| 74 |
"Copyright (C) 2009-2012 David Sommerseth <[email protected]>\n", |
| 75 |
prg, EUREPHIAVERSION); |
| 76 |
return prg; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
/** |
| 81 |
* Prints a help screen for all arguments eurephia_init can process |
| 82 |
* |
| 83 |
* @param fprg String containing full path to this binary (argv[0]) |
| 84 |
*/ |
| 85 |
void print_help(char *fprg) { |
| 86 |
print_version(fprg); |
| 87 |
|
| 88 |
printf("\n This utility is only supposed to be used when installing eurephia. The\n" |
| 89 |
" purpose is to initialise the database eurephia will use, by setting up\n" |
| 90 |
" some standard configuration values and create an administrator account\n\n"); |
| 91 |
|
| 92 |
printf(" Valid arguments:\n" |
| 93 |
" -V | --version : Show version information\n" |
| 94 |
" -h | --help : This help screen\n" |
| 95 |
" -l | --log <filename> : Log file for debugging\n" |
| 96 |
" -L | --log-level <log level> : Sets the log level\n" |
| 97 |
" -N | --hash-threshold-min <ms> : Benchmarking parameter, see below. Default 95ms\n" |
| 98 |
" -M | --hash-threshold-max <ms> : Benchmarking parameter, see below. Default 200ms\n" |
| 99 |
" -D | --database-driver <path> : Full path to the database driver\n" |
| 100 |
" -d | --database-args <args> : Required database arguments for the driver\n\n"); |
| 101 |
|
| 102 |
printf("* Benchmarking\n" |
| 103 |
" During the initialisation this utility will benchmark the CPU power by doing\n" |
| 104 |
" multiple SHA512 hash calculations. This is to determinate the optimal rounds\n" |
| 105 |
" the password hashes should use on the current computer. To make it more difficult\n" |
| 106 |
" to brute force passwords, eurephia implements dynamic password hash rounds, based\n" |
| 107 |
" on a random number within in a given range. This benchmark will suggest an optimal\n" |
| 108 |
" range. To do this, two limits are defined, the shortest time and the longest time\n" |
| 109 |
" to be used for calculating a hash. The default values are 95ms and 200ms.\n" |
| 110 |
"\n" |
| 111 |
" If you want to modify those thresholds, you can do so with the --hash-threshold-min\n" |
| 112 |
" and --hash-threshold-max options. By increasing these numbers, you will allow the\n" |
| 113 |
" number of rounds to be increased.\n\n"); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Establishes a connection to the defined database. The eurephiaCTX will contain the database connection. |
| 118 |
* |
| 119 |
* @param ctx eurephiaCTX |
| 120 |
* @param cfg eurephiaVALUES stack containing the database parameters |
| 121 |
* |
| 122 |
* @return Returns 1 on success, otherwise 0. |
| 123 |
*/ |
| 124 |
int eurephia_ConnectDB(eurephiaCTX *ctx, eurephiaVALUES *cfg) { |
| 125 |
char *delims = " "; |
| 126 |
char *cp = NULL; |
| 127 |
const char *dbargv[MAX_ARGUMENTS]; |
| 128 |
int dbargc = 0; |
| 129 |
char *argstr = NULL; |
| 130 |
|
| 131 |
argstr = eGet_value(cfg, "database_params"); |
| 132 |
if( (argstr == NULL) || (strlen(argstr) < 1) ) { |
| 133 |
eurephia_log(ctx, LOG_FATAL, 0, "No database connection string given"); |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
// Split up argstr into separate arguments |
| 138 |
cp = strdup(argstr); |
| 139 |
assert(cp != NULL); |
| 140 |
|
| 141 |
dbargc = 0; |
| 142 |
dbargv[dbargc] = strtok(cp, delims); |
| 143 |
while( (dbargv[dbargc] != NULL) && (dbargc <= MAX_ARGUMENTS) ) { |
| 144 |
dbargv[++dbargc] = strtok(NULL, delims); |
| 145 |
} |
| 146 |
|
| 147 |
if( !eDBconnect(ctx, dbargc, dbargv) ) { |
| 148 |
eurephia_log(ctx, LOG_PANIC, 0, "Could not connect to the database"); |
| 149 |
eDBlink_close(ctx); |
| 150 |
return 0; |
| 151 |
} |
| 152 |
free_nullsafe(ctx, cp); |
| 153 |
|
| 154 |
return 1; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
/** |
| 159 |
* Internal eurephia_init function. Sets a configuration parameter in the database. |
| 160 |
* |
| 161 |
* @param ctx eurephiaCTX |
| 162 |
* @param key String containing the key name |
| 163 |
* @param val String containing the value of the key |
| 164 |
* |
| 165 |
* @return Returns 1 on success, otherwise 0. |
| 166 |
*/ |
| 167 |
static int config_set(eurephiaCTX *ctx, const char *key, const char *val) { |
| 168 |
xmlDoc *cfgxml = NULL, *resxml = NULL; |
| 169 |
xmlNode *cfg_n = NULL; |
| 170 |
eurephiaRESULT *res = NULL; |
| 171 |
int ret = 0; |
| 172 |
|
| 173 |
eurephiaXML_CreateDoc(ctx, 1, "configuration", &cfgxml, &cfg_n); |
| 174 |
assert( (cfgxml != NULL) && (cfg_n != NULL) ); |
| 175 |
|
| 176 |
cfg_n = xmlNewChild(cfg_n, NULL, (xmlChar *) "set", (xmlChar *) val); |
| 177 |
xmlNewProp(cfg_n, (xmlChar *) "key", (xmlChar *) key); |
| 178 |
resxml = eDBadminConfiguration(ctx, cfgxml); |
| 179 |
xmlFreeDoc(cfgxml); |
| 180 |
if( resxml == NULL ) { |
| 181 |
fprintf(stderr, "%s: Error updating the configuration.\n", MODULE); |
| 182 |
return 0; |
| 183 |
} |
| 184 |
|
| 185 |
res = eurephiaXML_ParseResultMsg(ctx, resxml); |
| 186 |
if( res == NULL ) { |
| 187 |
fprintf(stderr, "%s: Error updating the configuration. No results returned.\n", |
| 188 |
MODULE); |
| 189 |
xmlFreeDoc(resxml); |
| 190 |
return 0; |
| 191 |
} |
| 192 |
|
| 193 |
if( res->resultType == exmlERROR ) { |
| 194 |
fprintf(stderr, "%s: %s\n", MODULE, res->message); |
| 195 |
ret = 0; |
| 196 |
} else { |
| 197 |
ret = 1; |
| 198 |
} |
| 199 |
free_nullsafe(ctx, res); |
| 200 |
xmlFreeDoc(resxml); |
| 201 |
return ret; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Generate a suggestion for a ~/.eurephia/eurephiadm.cfg file |
| 206 |
* |
| 207 |
* @param fp A file pointer where to write this suggestion |
| 208 |
* @param cfg eurephiaVALUES point to the configuration given via the command line. |
| 209 |
* |
| 210 |
*/ |
| 211 |
static void write_eurephiadmcfg_file(FILE *fp, eurephiaVALUES *cfg) { |
| 212 |
fprintf(fp, "# Database settings\n"); |
| 213 |
fprintf(fp, "database_driver = %s\n", eGet_value(cfg, "database_driver")); |
| 214 |
fprintf(fp, "database_params = %s\n", eGet_value(cfg, "database_params")); |
| 215 |
fprintf(fp, "\n# Disabling logging\n"); |
| 216 |
fprintf(fp, "log_level = 0\n"); |
| 217 |
fprintf(fp, "log = none:\n"); |
| 218 |
} |
| 219 |
/** |
| 220 |
* Guides the user through setting up the first eurephia administrator account. |
| 221 |
* |
| 222 |
* @param ctx eurephiaCTX |
| 223 |
* |
| 224 |
* @return Returns 1 on success, otherwise 0. |
| 225 |
*/ |
| 226 |
int setup_admin_account(eurephiaCTX *ctx) { |
| 227 |
xmlDoc *xmldoc = NULL, *resxml = NULL; |
| 228 |
xmlNode *node = NULL, *node2 = NULL; |
| 229 |
int uid = 0, i; |
| 230 |
char uname[66], pwd1[66], pwd2[66]; |
| 231 |
|
| 232 |
printf("------------------------------------------------------------------------------\n"); |
| 233 |
printf(" eurephia :: ADMINISTRATOR ACCOUNT\n"); |
| 234 |
printf("------------------------------------------------------------------------------\n\n"); |
| 235 |
printf("Checking database for user accounts ... "); |
| 236 |
|
| 237 |
eurephiaXML_CreateDoc(ctx, 1, "UserAccount", &xmldoc, &node); |
| 238 |
assert( (xmldoc != NULL) && (node != NULL) ); |
| 239 |
xmlNewProp(node, (xmlChar *) "mode", (xmlChar *) "view"); |
| 240 |
|
| 241 |
node = xmlNewChild(node, NULL, (xmlChar *) "fieldMapping", NULL); |
| 242 |
assert( node != NULL ); |
| 243 |
xmlNewProp(node, (xmlChar *) "table", (xmlChar *) "users"); |
| 244 |
|
| 245 |
resxml = eDBadminUserAccount(ctx, xmldoc); |
| 246 |
node = eurephiaXML_getRoot(ctx, resxml, "UserAccount", 1); |
| 247 |
if( node == NULL ) { |
| 248 |
fprintf(stderr, "Could not retrieve valid data\n"); |
| 249 |
xmlFreeDoc(xmldoc); |
| 250 |
xmlFreeDoc(resxml); |
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
node = xmlFindNode(node, "Account"); |
| 255 |
if( (node != NULL) ) { |
| 256 |
printf("User accounts found, aborting. eurephia is already initialised\n"); |
| 257 |
xmlFreeDoc(xmldoc); |
| 258 |
xmlFreeDoc(resxml); |
| 259 |
return 0; |
| 260 |
} |
| 261 |
xmlFreeDoc(xmldoc); xmldoc = NULL; |
| 262 |
xmlFreeDoc(resxml); resxml = NULL; |
| 263 |
printf("None found. Good!\n"); |
| 264 |
|
| 265 |
get_console_input(uname, 64, "Admin username: ", 0); |
| 266 |
if( strlen_nullsafe(uname) < 4 ) { |
| 267 |
fprintf(stderr, "Username is too short. Minimum 4 characters\n"); |
| 268 |
return 0; |
| 269 |
} |
| 270 |
get_console_input(pwd1, 64, "Password: ", 1); |
| 271 |
if( strlen_nullsafe(pwd1) < 5 ) { |
| 272 |
fprintf(stderr, "Password is too short\n"); |
| 273 |
return 0; |
| 274 |
} |
| 275 |
get_console_input(pwd2, 64, "Confirm password: ", 1); |
| 276 |
if( strcmp(pwd1, pwd2) != 0 ) { |
| 277 |
fprintf(stderr, "Passwords do not match\n"); |
| 278 |
return 0; |
| 279 |
} |
| 280 |
memset(pwd2, 0, 66); |
| 281 |
|
| 282 |
eurephiaXML_CreateDoc(ctx, 1, "UserAccount", &xmldoc, &node); |
| 283 |
xmlNewProp(node, (xmlChar *) "mode", (xmlChar *) "add"); |
| 284 |
node = xmlNewChild(node, NULL, (xmlChar *) "fieldMapping", NULL); |
| 285 |
xmlNewProp(node, (xmlChar *) "table", (xmlChar *) "users"); |
| 286 |
|
| 287 |
xmlNewChild(node, NULL, (xmlChar *) "username", (xmlChar *) uname); |
| 288 |
node2 = xmlNewChild(node, NULL, (xmlChar *) "password", (xmlChar *) pwd1); |
| 289 |
xmlNewProp(node2, (xmlChar *) "pwhash", (xmlChar *) "none"); |
| 290 |
|
| 291 |
// Add the user |
| 292 |
resxml = eDBadminUserAccount(ctx, xmldoc); |
| 293 |
memset(pwd1, 0, 66); |
| 294 |
xmlFreeDoc(xmldoc); |
| 295 |
|
| 296 |
if( !eurephiaXML_IsResultMsg(ctx, resxml) ) { |
| 297 |
fprintf(stderr, "Failed to register user\n"); |
| 298 |
if( resxml ) { |
| 299 |
xmlFreeDoc(resxml); |
| 300 |
} |
| 301 |
return 0; |
| 302 |
} else { |
| 303 |
eurephiaRESULT *res = NULL; |
| 304 |
|
| 305 |
res = eurephiaXML_ParseResultMsg(ctx, resxml); |
| 306 |
switch( res->resultType ) { |
| 307 |
case exmlERROR: |
| 308 |
fprintf(stderr, "** ERROR ** %s\n", res->message); |
| 309 |
uid = 0; |
| 310 |
break; |
| 311 |
|
| 312 |
case exmlRESULT: |
| 313 |
fprintf(stdout, "%s\n", res->message); |
| 314 |
node = xmlFindNode(res->details, "UserAccount"); |
| 315 |
uid = atoi_nullsafe(xmlGetAttrValue(node->properties, "uid")); |
| 316 |
break; |
| 317 |
} |
| 318 |
xmlFreeDoc(resxml); |
| 319 |
free_nullsafe(ctx, res); |
| 320 |
|
| 321 |
if( uid < 1 ) { |
| 322 |
fprintf(stderr, "Failed to register user\n"); |
| 323 |
return 0; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
// Grant all available access levels to the admin account |
| 328 |
static char *grants[] = { "config", "useradmin", "certadmin", "fwprofiles", |
| 329 |
"attempts", "blacklist", NULL }; |
| 330 |
|
| 331 |
printf("Granting access to user account:"); |
| 332 |
|
| 333 |
eurephiaXML_CreateDoc(ctx, 1, "admin_access", &xmldoc, &node); |
| 334 |
xmlNewProp(node, (xmlChar *) "mode", (xmlChar *) "grant"); |
| 335 |
|
| 336 |
node = xmlNewChild(node, NULL, (xmlChar *) "fieldMapping", NULL); |
| 337 |
xmlNewProp(node, (xmlChar *) "table", (xmlChar *) "eurephia_adminaccess"); |
| 338 |
|
| 339 |
snprintf(uname, 64, "%i", uid); // borrow uname variable for uid int -> string |
| 340 |
xmlNewChild(node, NULL, (xmlChar *) "uid", (xmlChar *) uname); |
| 341 |
xmlNewChild(node, NULL, (xmlChar *) "interface", (xmlChar *) "C"); |
| 342 |
node2 = xmlNewChild(node, NULL, (xmlChar *) "accesslevel", (xmlChar *)""); |
| 343 |
|
| 344 |
for( i = 0; grants[i] != NULL; i++ ) { |
| 345 |
xmlNode *new_n = NULL; |
| 346 |
|
| 347 |
new_n = xmlNewChild(node, NULL, (xmlChar *) "accesslevel", (xmlChar *)grants[i]); |
| 348 |
xmlReplaceNode(node2, new_n); |
| 349 |
node2 = new_n; |
| 350 |
printf(" %s", grants[i]); |
| 351 |
if( !eDBadminAccessLevel(ctx, xmldoc) ) { |
| 352 |
fprintf(stderr, "\n** ERROR: Could not grant %s access. Initialisation failed\n", |
| 353 |
grants[i]); |
| 354 |
xmlFreeDoc(xmldoc); |
| 355 |
return 0; |
| 356 |
} |
| 357 |
} |
| 358 |
xmlFreeDoc(xmldoc); |
| 359 |
node = NULL; node2 = NULL; |
| 360 |
printf("\n"); |
| 361 |
|
| 362 |
|
| 363 |
// Activate the user account |
| 364 |
printf("Activating the user account ... "); |
| 365 |
eurephiaXML_CreateDoc(ctx, 1, "UserAccount", &xmldoc, &node); |
| 366 |
xmlNewProp(node, (xmlChar *) "mode", (xmlChar *) "update"); |
| 367 |
xmlNewProp(node, (xmlChar *) "uid", (xmlChar *) uname); // uid should still be in uname as string |
| 368 |
|
| 369 |
// Add fieldMapping - to correctly map eurephia fields into the database fields |
| 370 |
node2 = xmlNewChild(node, NULL, (xmlChar *) "fieldMapping", NULL); |
| 371 |
xmlNewProp(node2, (xmlChar *) "table", (xmlChar *) "users"); |
| 372 |
xmlNewChild(node2, NULL, (xmlChar *) "activated", (xmlChar *) "CURRENT_TIMESTAMP"); |
| 373 |
|
| 374 |
resxml = eDBadminUserAccount(ctx, xmldoc); |
| 375 |
if( !eurephiaXML_IsResultMsg(ctx, resxml) ) { |
| 376 |
printf("FAILED\n"); |
| 377 |
xmlFreeDoc(xmldoc); |
| 378 |
xmlFreeDoc(resxml); |
| 379 |
return 0; |
| 380 |
} else { |
| 381 |
eurephiaRESULT *res = NULL; |
| 382 |
|
| 383 |
res = eurephiaXML_ParseResultMsg(ctx, resxml); |
| 384 |
switch( res->resultType ) { |
| 385 |
case exmlERROR: |
| 386 |
fprintf(stderr, "** ERROR ** %s\n", res->message); |
| 387 |
break; |
| 388 |
|
| 389 |
case exmlRESULT: |
| 390 |
break; |
| 391 |
} |
| 392 |
xmlFreeDoc(resxml); |
| 393 |
free_nullsafe(ctx, res); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
printf("Done"); |
| 399 |
xmlFreeDoc(xmldoc); |
| 400 |
|
| 401 |
printf("\n==============================================================================\n\n"); |
| 402 |
return 1; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
/** |
| 407 |
* Guides the user through setting up hashing algorithm parameters. This function will try to find |
| 408 |
* the ideal boundaries for hashing rounds, based on how quick the computer can calculate hashes. |
| 409 |
* |
| 410 |
* @param ctx eurephiaCTX |
| 411 |
* @param hash_thr_min Minimum time it should take to calculate a SHA512 hash (in ms) |
| 412 |
* @param hash_thr_max Maximum time it should take to calculate a SHA512 hash (in ms) |
| 413 |
* |
| 414 |
* @return Returns 1 on success, otherwise 0. |
| 415 |
*/ |
| 416 |
int setup_password_params(eurephiaCTX *ctx, const int hash_thr_min, const int hash_thr_max) { |
| 417 |
int rounds_min = 0, rounds_max = 0; |
| 418 |
char buffer[22], prompt[80], value[22]; |
| 419 |
|
| 420 |
memset(&buffer, 0, 22); |
| 421 |
|
| 422 |
printf("------------------------------------------------------------------------------\n"); |
| 423 |
printf(" eurephia :: PASSWORD PARAMETERS\n"); |
| 424 |
printf("------------------------------------------------------------------------------\n\n"); |
| 425 |
|
| 426 |
get_console_input(buffer, 10, "Salt length for password hashes [32] ", 0); |
| 427 |
memset(&value, 0, 22); |
| 428 |
snprintf(value, 20, "%i", (atoi_nullsafe(buffer) > 0 ? atoi_nullsafe(buffer) : 32)); |
| 429 |
if( !config_set(ctx, "passwordhash_salt_length", value) ) { |
| 430 |
fprintf(stderr, "Failed to set configuration settings in database\n"); |
| 431 |
return 0; |
| 432 |
} |
| 433 |
|
| 434 |
printf("\n** Preparing for SHA512 performance benchmark.\n"); |
| 435 |
printf("Aiming for minimum hashing calculation time: %ims\n", hash_thr_min); |
| 436 |
printf("Aiming for maximum hashing calculation time: %ims\n", hash_thr_max); |
| 437 |
if( !benchmark(&rounds_min, &rounds_max, hash_thr_min, hash_thr_max) ) { |
| 438 |
fprintf(stderr, "Failed to meassure SHA512 hash performance\n"); |
| 439 |
return 0; |
| 440 |
} |
| 441 |
printf("\n"); |
| 442 |
snprintf(prompt, 78, "Minimum hashing rounds: [%i] ", rounds_min); |
| 443 |
get_console_input(buffer, 20, prompt, 0); |
| 444 |
memset(&value, 0, 22); |
| 445 |
snprintf(value, 20, "%i", (atoi_nullsafe(buffer) > 1 ? atoi_nullsafe(buffer) : rounds_min)); |
| 446 |
if( !config_set(ctx, "passwordhash_rounds_min", value) ) { |
| 447 |
fprintf(stderr, "Failed to set configuration settings in database\n"); |
| 448 |
return 0; |
| 449 |
} |
| 450 |
|
| 451 |
snprintf(prompt, 78, "Maximum hashing rounds: [%i] ", rounds_max); |
| 452 |
get_console_input(buffer, 20, prompt, 0); |
| 453 |
memset(&value, 0, 22); |
| 454 |
snprintf(value, 20, "%i", (atoi_nullsafe(buffer) > 1 ? atoi_nullsafe(buffer) : rounds_max)); |
| 455 |
if( !config_set(ctx, "passwordhash_rounds_max", value) ) { |
| 456 |
fprintf(stderr, "Failed to set configuration settings in database\n"); |
| 457 |
return 0; |
| 458 |
} |
| 459 |
|
| 460 |
printf("\n==============================================================================\n\n"); |
| 461 |
|
| 462 |
return 1; |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
/** |
| 467 |
* Guides the user through setting up attempts limits |
| 468 |
* |
| 469 |
* @param ctx eurephiaCTX |
| 470 |
* |
| 471 |
* @return Returns 1 on success, otherwise 0. |
| 472 |
*/ |
| 473 |
int setup_attempt_limits(eurephiaCTX *ctx) { |
| 474 |
char buffer[22], value[22]; |
| 475 |
memset(&buffer, 0, 22); |
| 476 |
|
| 477 |
printf("------------------------------------------------------------------------------\n"); |
| 478 |
printf(" eurephia :: ATTEMPTS LIMITS\n"); |
| 479 |
printf("------------------------------------------------------------------------------\n\n"); |
| 480 |
printf("These parameters here will decide when eurephia should block access, based on\n" |
| 481 |
"how many registered failed attempts. Normally, you should be strict regarding\n" |
| 482 |
"number of attempts on usernames, less strict on certificates and even less on\n" |
| 483 |
"IP addresses. Remember that the user might connect via a proxy or a firewall\n" |
| 484 |
"with NAT enabled.\n\n"); |
| 485 |
|
| 486 |
get_console_input(buffer, 10, "How many failed attempts will you allow per user name? [3]", 0); |
| 487 |
memset(&value, 0, 22); |
| 488 |
snprintf(value, 20, "%i", (atoi_nullsafe(buffer) > 0 ? atoi_nullsafe(buffer) : 3)); |
| 489 |
if( !config_set(ctx, "allow_username_attempts", value) ) { |
| 490 |
fprintf(stderr, "Failed to set configuration settings in database\n"); |
| 491 |
return 0; |
| 492 |
} |
| 493 |
|
| 494 |
get_console_input(buffer, 10, "How many failed attempts will you allow per certificate? [5]", 0); |
| 495 |
memset(&value, 0, 22); |
| 496 |
snprintf(value, 20, "%i", (atoi_nullsafe(buffer) > 0 ? atoi_nullsafe(buffer) : 5)); |
| 497 |
if( !config_set(ctx, "allow_cert_attempts", value) ) { |
| 498 |
fprintf(stderr, "Failed to set configuration settings in database\n"); |
| 499 |
return 0; |
| 500 |
} |
| 501 |
|
| 502 |
get_console_input(buffer, 10, "How many failed attempts will you allow per IP address? [10]", 0); |
| 503 |
memset(&value, 0, 22); |
| 504 |
snprintf(value, 20, "%i", (atoi_nullsafe(buffer) > 0 ? atoi_nullsafe(buffer) : 10)); |
| 505 |
if( !config_set(ctx, "allow_ipaddr_attempts", value) ) { |
| 506 |
fprintf(stderr, "Failed to set configuration settings in database\n"); |
| 507 |
return 0; |
| 508 |
} |
| 509 |
printf("\n==============================================================================\n\n"); |
| 510 |
return 1; |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
/** |
| 515 |
* Guides the user through setting up session specific parameters, mainly for the admin utilities |
| 516 |
* |
| 517 |
* @param ctx eurephiaCTX |
| 518 |
* @param cfg Configuration arguments, given via the command line |
| 519 |
* |
| 520 |
* @return Returns 1 on success, otherwise 0. |
| 521 |
*/ |
| 522 |
int setup_session_params(eurephiaCTX *ctx, eurephiaVALUES *cfg) { |
| 523 |
char buffer[22], value[22]; |
| 524 |
char *fname = get_config_filename(NULL, "eurephiadm.cfg"); |
| 525 |
memset(&buffer, 0, 22); |
| 526 |
|
| 527 |
printf("------------------------------------------------------------------------------\n"); |
| 528 |
printf(" eurephia :: SESSION PARAMETERS\n"); |
| 529 |
printf("------------------------------------------------------------------------------\n\n"); |
| 530 |
|
| 531 |
get_console_input(buffer, 10, |
| 532 |
"eurephiadmin: How many minutes before a session is auto logged out: [10]", 0); |
| 533 |
memset(&value, 0, 22); |
| 534 |
snprintf(value, 20, "%i", (atoi_nullsafe(buffer) > 0 ? atoi_nullsafe(buffer) : 10)); |
| 535 |
if( !config_set(ctx, "eurephiadmin_autologout", value) ) { |
| 536 |
fprintf(stderr, "Failed to set configuration settings in database\n"); |
| 537 |
return 0; |
| 538 |
} |
| 539 |
|
| 540 |
if( fname != NULL ) { |
| 541 |
printf("\nGenerating %s\n", fname); |
| 542 |
printf("---------------------------------------------------------------\n"); |
| 543 |
write_eurephiadmcfg_file(stdout, cfg); |
| 544 |
printf("---------------------------------------------------------------\n"); |
| 545 |
memset(&buffer, 0, 22); |
| 546 |
get_console_input(buffer, 8, "Do you want this to be written to disk? [Yes]", 0); |
| 547 |
if( (buffer[0] == '\n') || (buffer[0] == 'y') || (buffer[0] == 'Y') ) { |
| 548 |
FILE *f; |
| 549 |
|
| 550 |
if( (f = fopen(fname, "w")) == NULL ) { |
| 551 |
fprintf(stderr, "Failed to create %s\n", fname); |
| 552 |
return 0; |
| 553 |
} |
| 554 |
write_eurephiadmcfg_file(f, cfg); |
| 555 |
fclose(f); |
| 556 |
printf("Wrote %s\n", fname); |
| 557 |
} else { |
| 558 |
printf("Configuration file was not written to disk\n"); |
| 559 |
} |
| 560 |
} else { |
| 561 |
printf("\n** ERROR ** Could not generate eurephiadm.cfg file due to file system restrictions.\n"); |
| 562 |
printf(" Skipping this part.\n"); |
| 563 |
} |
| 564 |
printf("\n==============================================================================\n\n"); |
| 565 |
|
| 566 |
return 1; |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
#ifdef FW_IPTABLES |
| 571 |
/** |
| 572 |
* Guides the user through setting up iptables firewall support, if enabled at compile time. |
| 573 |
* |
| 574 |
* @param ctx eurephiaCTX |
| 575 |
* |
| 576 |
* @return Returns 1 on success, otherwise 0. |
| 577 |
*/ |
| 578 |
int setup_iptables(eurephiaCTX *ctx) { |
| 579 |
char buffer[1026], value[1026], prompt[2048]; |
| 580 |
memset(&buffer, 0, 1026); |
| 581 |
memset(&value, 0, 1026); |
| 582 |
|
| 583 |
printf("------------------------------------------------------------------------------\n"); |
| 584 |
printf(" eurephia :: FIREWALL SUPPORT - iptables\n"); |
| 585 |
printf("------------------------------------------------------------------------------\n\n"); |
| 586 |
|
| 587 |
get_console_input(buffer, 8, "Do you want to load the iptables module? [No]", 0); |
| 588 |
if( (strlen_nullsafe(buffer) > 0) && ((buffer[0] != 'y') && (buffer[0] != 'Y')) ) { |
| 589 |
printf("\niptables firewall support is not configured\n"); |
| 590 |
goto ipt_done; |
| 591 |
} |
| 592 |
|
| 593 |
snprintf(value, 1024, "%s/efw-iptables.so", PLUGINDIR); |
| 594 |
snprintf(prompt, 2048, "\nFull path to the efw-iptables.so library:\n[%s]", value); |
| 595 |
get_console_input(buffer, 1024, prompt, 0); |
| 596 |
if( !config_set(ctx, "firewall_interface", (strlen_nullsafe(buffer) > 1 ? buffer : value)) ) { |
| 597 |
fprintf(stderr, "Failed to set configuration settings in database\n"); |
| 598 |
return 0; |
| 599 |
} |
| 600 |
|
| 601 |
snprintf(value, 1024, "/sbin/iptables"); |
| 602 |
snprintf(prompt, 2048, "\nFull path to the iptables command: [%s]", value); |
| 603 |
get_console_input(buffer, 1024, prompt, 0); |
| 604 |
if( !config_set(ctx, "firewall_command", (strlen_nullsafe(buffer) > 1 ? buffer : value)) ) { |
| 605 |
fprintf(stderr, "Failed to set configuration settings in database\n"); |
| 606 |
return 0; |
| 607 |
} |
| 608 |
|
| 609 |
snprintf(value, 1024, "vpn_users"); |
| 610 |
snprintf(prompt, 2048, "\nWhich iptables chain should eurephia use? [%s]", value); |
| 611 |
get_console_input(buffer, 1024, prompt, 0); |
| 612 |
if( !config_set(ctx, "firewall_destination", (strlen_nullsafe(buffer) > 1 ? buffer : value)) ) { |
| 613 |
fprintf(stderr, "Failed to set configuration settings in database\n"); |
| 614 |
return 0; |
| 615 |
} |
| 616 |
|
| 617 |
get_console_input(buffer, 8, "\nDo you want you eurephia to block blacklisted IP addresses\n" |
| 618 |
"in iptables too? [No]", 0); |
| 619 |
if( (strlen_nullsafe(buffer) < 1) || (buffer[0] == 'y') || (buffer[0] == 'Y') ) { |
| 620 |
snprintf(value, 1024, "vpn_blacklist"); |
| 621 |
snprintf(prompt, 2048, "\nWhich iptables chain should eurephia use for" |
| 622 |
"\nblacklisted IP addresses? [%s]", value); |
| 623 |
get_console_input(buffer, 1024, prompt, 0); |
| 624 |
if( !config_set(ctx, "firewall_blacklist_destination", |
| 625 |
(strlen_nullsafe(buffer) > 1 ? buffer : value)) ) { |
| 626 |
fprintf(stderr, "Failed to set configuration settings in database\n"); |
| 627 |
return 0; |
| 628 |
} |
| 629 |
|
| 630 |
memset(&value, 0, 1024); |
| 631 |
snprintf(prompt, 2048, "\nWhich iptables chain should eurephia send blacklisted" |
| 632 |
"\nIP addresses to (iptables '-j' argument) ? [DROP]"); |
| 633 |
get_console_input(buffer, 1024, prompt, 0); |
| 634 |
if( (strlen_nullsafe(buffer) > 1) |
| 635 |
&& !config_set(ctx, "firewall_blacklist_send_to", buffer) ) { |
| 636 |
fprintf(stderr, "Failed to set configuration settings in database\n"); |
| 637 |
return 0; |
| 638 |
} |
| 639 |
} |
| 640 |
ipt_done: |
| 641 |
printf("\n==============================================================================\n\n"); |
| 642 |
|
| 643 |
return 1; |
| 644 |
} |
| 645 |
#endif |
| 646 |
|
| 647 |
|
| 648 |
/** |
| 649 |
* Main function of eurephia_init |
| 650 |
* |
| 651 |
* @param argc |
| 652 |
* @param argv |
| 653 |
* |
| 654 |
* @return Returns 0 on success, otherwise a positive integer on failure. |
| 655 |
*/ |
| 656 |
int main(int argc, char **argv) { |
| 657 |
// Default hash calculation thresholds for benchmarking |
| 658 |
int hash_thr_min = 95; // 95ms |
| 659 |
int hash_thr_max = 200; // 200ms |
| 660 |
int argi = 0; |
| 661 |
eurephiaVALUES *cfg = NULL; |
| 662 |
eurephiaCTX *ctx = NULL; |
| 663 |
int rc = 0; |
| 664 |
|
| 665 |
static e_options argopts[] = { |
| 666 |
{"--version", "-V", 0}, |
| 667 |
{"--help", "-h", 0}, |
| 668 |
{"--log", "-l", 1}, |
| 669 |
{"--log-level", "-L", 1}, |
| 670 |
{"--hash-threshold-min", "-N", 1}, |
| 671 |
{"--hash-threshold-max", "-M", 1}, |
| 672 |
{"--database-driver", "-D", 1}, |
| 673 |
{"--database-args", "-d", 1}, |
| 674 |
{NULL, NULL, 0} |
| 675 |
}; |
| 676 |
|
| 677 |
|
| 678 |
cfg = eCreate_value_space(NULL, 30); |
| 679 |
for( argi = 1; argi < argc; argi++ ) { |
| 680 |
switch( eurephia_getopt(&argi, argc, argv, argopts) ) { |
| 681 |
case 'V': |
| 682 |
print_version(argv[0]); |
| 683 |
rc = 0; |
| 684 |
goto exit; |
| 685 |
|
| 686 |
case 'h': |
| 687 |
print_help(argv[0]); |
| 688 |
rc = 0; |
| 689 |
goto exit; |
| 690 |
|
| 691 |
case 'l': |
| 692 |
eAdd_value(NULL, cfg, "log", optargs[0]); |
| 693 |
break; |
| 694 |
|
| 695 |
case 'L': |
| 696 |
eAdd_value(NULL, cfg, "log_level", optargs[0]); |
| 697 |
break; |
| 698 |
|
| 699 |
case 'D': |
| 700 |
eAdd_value(NULL, cfg, "database_driver", optargs[0]); |
| 701 |
break; |
| 702 |
|
| 703 |
case 'd': |
| 704 |
eAdd_value(NULL, cfg, "database_params", optargs[0]); |
| 705 |
break; |
| 706 |
|
| 707 |
case 'N': |
| 708 |
hash_thr_min = atoi_nullsafe(optargs[0]); |
| 709 |
break; |
| 710 |
|
| 711 |
case 'M': |
| 712 |
hash_thr_max = atoi_nullsafe(optargs[0]); |
| 713 |
break; |
| 714 |
|
| 715 |
default: |
| 716 |
rc = 1; |
| 717 |
goto exit; |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
if( eGet_value(cfg, "database_driver") == NULL ) { |
| 722 |
fprintf(stderr, "Missing required database driver (--database-driver)\n"); |
| 723 |
rc = 2; |
| 724 |
goto exit; |
| 725 |
|
| 726 |
} |
| 727 |
|
| 728 |
if( eGet_value(cfg, "database_params") == NULL ) { |
| 729 |
fprintf(stderr, "Missing required database driver parameters (--database-args)\n"); |
| 730 |
rc = 2; |
| 731 |
goto exit; |
| 732 |
|
| 733 |
} |
| 734 |
|
| 735 |
ctx = eurephiaCTX_init("eurephia_init", NULL, 0, cfg); |
| 736 |
if( ctx == NULL ) { |
| 737 |
fprintf(stderr, "Failed to initialise an eurephia context.\n"); |
| 738 |
rc = 3; |
| 739 |
goto exit; |
| 740 |
} |
| 741 |
|
| 742 |
if( !eurephia_ConnectDB(ctx, cfg) ) { |
| 743 |
fprintf(stderr, "Failed to access the database.\n"); |
| 744 |
rc = 4; |
| 745 |
goto exit; |
| 746 |
} |
| 747 |
|
| 748 |
if( !setup_password_params(ctx, hash_thr_min, hash_thr_max) ) { |
| 749 |
rc = 11; |
| 750 |
goto exit; |
| 751 |
} |
| 752 |
|
| 753 |
if( !setup_admin_account(ctx) ) { |
| 754 |
rc = 12; |
| 755 |
goto exit; |
| 756 |
} |
| 757 |
|
| 758 |
if( !setup_session_params(ctx, cfg) ) { |
| 759 |
rc = 13; |
| 760 |
goto exit; |
| 761 |
} |
| 762 |
|
| 763 |
if( !setup_attempt_limits(ctx) ) { |
| 764 |
rc = 14; |
| 765 |
goto exit; |
| 766 |
} |
| 767 |
|
| 768 |
#ifdef FW_IPTABLES |
| 769 |
if( !setup_iptables(ctx) ){ |
| 770 |
rc = 15; |
| 771 |
goto exit; |
| 772 |
} |
| 773 |
#endif |
| 774 |
|
| 775 |
printf("\neurephia is now configured. For further changes, please use the eurephiadm utility.\n\n"); |
| 776 |
exit: |
| 777 |
eFree_values(ctx, cfg); |
| 778 |
eurephiaCTX_destroy(ctx); |
| 779 |
return rc; |
| 780 |
} |