| 1 |
/* administration.c -- Functions needed for administration tasks |
| 2 |
* |
| 3 |
* GPLv2 only - Copyright (C) 2008 - 2012 |
| 4 |
* David Sommerseth <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; version 2 |
| 9 |
* of the License. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
/** |
| 23 |
* @file authentication.c |
| 24 |
* @author David Sommerseth <[email protected]> |
| 25 |
* @date 2008-12-03 |
| 26 |
* |
| 27 |
* @brief Functions used for authentication of administration sessions. |
| 28 |
* |
| 29 |
*/ |
| 30 |
|
| 31 |
#include <string.h> |
| 32 |
#include <unistd.h> |
| 33 |
#include <assert.h> |
| 34 |
|
| 35 |
#include <libxml/tree.h> |
| 36 |
|
| 37 |
/** |
| 38 |
* @{ |
| 39 |
*/ |
| 40 |
#ifndef DRIVERAPIVERSION |
| 41 |
# define DRIVERAPIVERSION 2 |
| 42 |
#endif |
| 43 |
/** |
| 44 |
* @} |
| 45 |
*/ |
| 46 |
|
| 47 |
#include <sqlite3.h> |
| 48 |
|
| 49 |
#include <eurephia_nullsafe.h> |
| 50 |
#include <eurephia_context.h> |
| 51 |
#include <eurephia_log.h> |
| 52 |
#include <eurephia_xml.h> |
| 53 |
#include <eurephia_values.h> |
| 54 |
#include <eurephiadb_session_struct.h> |
| 55 |
#include <eurephiadb_mapping.h> |
| 56 |
#include <passwd.h> |
| 57 |
|
| 58 |
#ifndef DRIVER_MODE |
| 59 |
#define DRIVER_MODE |
| 60 |
#endif |
| 61 |
#include <eurephiadb_driver.h> |
| 62 |
|
| 63 |
#include "../sqlite.h" |
| 64 |
|
| 65 |
#if (DRIVERAPIVERSION > 1) || defined(DOXYGEN) |
| 66 |
/* |
| 67 |
* API Version 2 functions |
| 68 |
* |
| 69 |
*/ |
| 70 |
|
| 71 |
|
| 72 |
/** |
| 73 |
* Authenticate a user for the administration interface. This interface do not |
| 74 |
* require any certificate validation and is intended for administration utilities |
| 75 |
* for eurephia. The eurephia context type must be either ECTX_ADMIN_CONSOLE or |
| 76 |
* ECTX_ADMIN_WEB. |
| 77 |
* |
| 78 |
* @param ctx eurephiaCTX - context used for administration task |
| 79 |
* @param req_access String containing the requested administration access level |
| 80 |
* @param uname username of the user being authenticated |
| 81 |
* @param pwd password from the user |
| 82 |
* |
| 83 |
* @return Returns an eurephia ResultMsg XML document with the result. On fatal errors, NULL is returned |
| 84 |
*/ |
| 85 |
static xmlDoc *auth_user(eurephiaCTX *ctx, const char *req_access, const char *uname, const char *pwd) { |
| 86 |
xmlDoc *res_d = NULL; |
| 87 |
xmlNode *info_n = NULL; |
| 88 |
dbresult *res = NULL; |
| 89 |
char *crpwd = NULL, *dbpwd = NULL; |
| 90 |
char *activated = NULL, *deactivated = NULL, *blid = NULL, *uid = NULL; |
| 91 |
int access = 0; |
| 92 |
char interface; |
| 93 |
|
| 94 |
DEBUG(ctx, 21, "Function call: auth_user(ctx, '%s, '%s', 'xxxxxxxx')", req_access, uname); |
| 95 |
|
| 96 |
assert(ctx != NULL); |
| 97 |
|
| 98 |
switch( ctx->context_type ) { |
| 99 |
case ECTX_ADMIN_CONSOLE: |
| 100 |
interface = 'C'; |
| 101 |
break; |
| 102 |
case ECTX_ADMIN_WEB: |
| 103 |
interface = 'W'; |
| 104 |
break; |
| 105 |
default: |
| 106 |
eurephia_log(ctx, LOG_ERROR, 0, "Wrong eurephia context type (0x%04x)", ctx->context_type); |
| 107 |
return NULL; |
| 108 |
} |
| 109 |
|
| 110 |
if( (strlen_nullsafe(uname) < 4) || (strlen_nullsafe(pwd) < 4) ) { |
| 111 |
eurephia_log(ctx, LOG_WARNING, 0, |
| 112 |
"Username and/or password is either null or less than 4 bytes"); |
| 113 |
return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Username or password is too short"); |
| 114 |
} |
| 115 |
|
| 116 |
// |
| 117 |
// Authenticate user and password |
| 118 |
// |
| 119 |
res = sqlite_query(ctx, |
| 120 |
"SELECT activated, deactivated, bl.blid, " |
| 121 |
" password, uid " |
| 122 |
" FROM openvpn_users ou" |
| 123 |
" LEFT JOIN openvpn_blacklist bl USING (username)" |
| 124 |
" WHERE ou.username = '%q'", |
| 125 |
uname); |
| 126 |
|
| 127 |
if( sqlite_query_status(res) != dbSUCCESS ) { |
| 128 |
xmlNode *err_n = NULL; |
| 129 |
|
| 130 |
eurephia_log(ctx, LOG_FATAL, 0, "Could not authenticate user against the database"); |
| 131 |
err_n = sqlite_log_error_xml(ctx, res); |
| 132 |
res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, "Authentication failed"); |
| 133 |
xmlFreeNode(err_n); |
| 134 |
sqlite_free_results(res); |
| 135 |
return res_d; |
| 136 |
} |
| 137 |
|
| 138 |
if( sqlite_get_numtuples(res) == 1 ) { |
| 139 |
activated = sqlite_get_value(res, 0, 0); |
| 140 |
deactivated = sqlite_get_value(res, 0, 1); |
| 141 |
blid = sqlite_get_value(res, 0, 2); |
| 142 |
dbpwd = sqlite_get_value(res, 0, 3); |
| 143 |
uid = strdup_nullsafe(sqlite_get_value(res, 0, 4)); |
| 144 |
|
| 145 |
if( blid != NULL ) { |
| 146 |
eurephia_log(ctx, LOG_WARNING, 0, |
| 147 |
"User account '%s' is BLACKLISTED. You have no access.", uname); |
| 148 |
sqlite_free_results(res); |
| 149 |
return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Authentication failed"); |
| 150 |
} |
| 151 |
|
| 152 |
if( activated == NULL ) { |
| 153 |
eurephia_log(ctx, LOG_WARNING, 0, "User account '%s' is not yet activated.", uname); |
| 154 |
sqlite_free_results(res); |
| 155 |
return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Authentication failed"); |
| 156 |
} |
| 157 |
|
| 158 |
if( deactivated != NULL ) { |
| 159 |
eurephia_log(ctx, LOG_WARNING, 0, "User account '%s' is deactivated.", uname); |
| 160 |
sqlite_free_results(res); |
| 161 |
return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Authentication failed"); |
| 162 |
} |
| 163 |
|
| 164 |
if( dbpwd == NULL ) { |
| 165 |
eurephia_log(ctx, LOG_WARNING, 0, "Authentication failed. DB error."); |
| 166 |
sqlite_free_results(res); |
| 167 |
return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Authentication failed"); |
| 168 |
} else { |
| 169 |
int pwdok = 0; |
| 170 |
// Verify the password |
| 171 |
crpwd = eurephia_pwd_crypt(ctx, pwd, dbpwd); |
| 172 |
pwdok = ((crpwd != NULL) && (strcmp(crpwd, dbpwd) == 0) ? 1 : 0); |
| 173 |
memset(crpwd, 0, strlen_nullsafe(crpwd)); |
| 174 |
memset(dbpwd, 0, strlen_nullsafe(dbpwd)); |
| 175 |
free_nullsafe(ctx, crpwd); |
| 176 |
if( pwdok == 0 ) { |
| 177 |
eurephia_log(ctx, LOG_WARNING, 0, "Authentication failed."); |
| 178 |
sleep(2); |
| 179 |
sqlite_free_results(res); |
| 180 |
return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Authentication failed"); |
| 181 |
} |
| 182 |
} |
| 183 |
sqlite_free_results(res); |
| 184 |
|
| 185 |
// Check if access level is granted |
| 186 |
// (SQLite do not handle advanced joins so well, so we need to |
| 187 |
// do this check with an extra query) |
| 188 |
res = sqlite_query(ctx, |
| 189 |
"SELECT (count(*) = 1) AS access " |
| 190 |
" FROM eurephia_adminaccess" |
| 191 |
" WHERE uid = '%q' AND interface = '%c' AND access = '%q'", |
| 192 |
uid, interface, req_access); |
| 193 |
|
| 194 |
if( sqlite_query_status(res) != dbSUCCESS ) { |
| 195 |
xmlNode *err_n = NULL; |
| 196 |
|
| 197 |
eurephia_log(ctx, LOG_FATAL, 0, "Could not check access level"); |
| 198 |
err_n = sqlite_log_error_xml(ctx, res); |
| 199 |
res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n,"Failed to validate access level"); |
| 200 |
xmlFreeNode(err_n); |
| 201 |
sqlite_free_results(res); |
| 202 |
return res_d; |
| 203 |
} |
| 204 |
access = atoi_nullsafe(sqlite_get_value(res, 0, 0)); |
| 205 |
sqlite_free_results(res); |
| 206 |
|
| 207 |
if( access == 0 ) { |
| 208 |
eurephia_log(ctx, LOG_WARNING, 0, |
| 209 |
"User account '%s' is lacking privileges for this operation", uname); |
| 210 |
return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Authentication failed"); |
| 211 |
} |
| 212 |
} else { |
| 213 |
eurephia_log(ctx, LOG_WARNING, 0, "Authentication failed. No unique records found."); |
| 214 |
sqlite_free_results(res); |
| 215 |
sleep(2); |
| 216 |
return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Authentication failed"); |
| 217 |
} |
| 218 |
|
| 219 |
// If we reach this place, authentication was successful. Return users uid |
| 220 |
info_n = xmlNewNode(NULL, (xmlChar *) "UserAccount"); |
| 221 |
assert( info_n != NULL ); |
| 222 |
xmlNewProp(info_n, (xmlChar *) "uid", (xmlChar *) uid); |
| 223 |
res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, info_n, "Successful authentication"); |
| 224 |
xmlFreeNode(info_n); |
| 225 |
free_nullsafe(ctx, uid); |
| 226 |
return res_d; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
/** |
| 231 |
* Validates a session key, to see if it still is valid (not auto-logged out or invalid session key) |
| 232 |
* and to check if they have access to a different access level. The eurephia context type must be |
| 233 |
* either ECTX_ADMIN_CONSOLE or ECTX_ADMIN_WEB. |
| 234 |
* |
| 235 |
* @param ctx eurephiaCTX |
| 236 |
* @param sesskey String containing the session key to validate |
| 237 |
* @param req_access String containing the required administration access level |
| 238 |
* |
| 239 |
* @return Returns an eurephia ResultMsg XML document with the result. On fatal errors, NULL is returned |
| 240 |
*/ |
| 241 |
static xmlDoc *auth_session(eurephiaCTX *ctx, const char *sesskey, const char *req_access) { |
| 242 |
dbresult *res = NULL; |
| 243 |
int valid = 0, access = 0, expire_time = 0; |
| 244 |
char interface; |
| 245 |
xmlDoc *ret_d = NULL; |
| 246 |
xmlNode *err_n = NULL; |
| 247 |
|
| 248 |
DEBUG(ctx, 21, "Function call: auth_session(ctx, '%s, '%s')", sesskey, req_access); |
| 249 |
assert( (ctx != NULL) && (sesskey != NULL) ); |
| 250 |
|
| 251 |
switch( ctx->context_type ) { |
| 252 |
case ECTX_ADMIN_CONSOLE: |
| 253 |
interface = 'C'; |
| 254 |
break; |
| 255 |
case ECTX_ADMIN_WEB: |
| 256 |
interface = 'W'; |
| 257 |
break; |
| 258 |
default: |
| 259 |
eurephia_log(ctx, LOG_ERROR, 0, "Wrong eurephia context type (0x%04x)", ctx->context_type); |
| 260 |
return NULL; |
| 261 |
} |
| 262 |
|
| 263 |
// Check if the session is still valid (not expired) and that this session are allowed to access |
| 264 |
// the requested access level. |
| 265 |
expire_time = (60 * atoi_nullsafe(defaultValue(eGet_value(ctx->dbc->config, |
| 266 |
"eurephiadmin_autologout"), |
| 267 |
"10") |
| 268 |
) |
| 269 |
); |
| 270 |
res = sqlite_query(ctx, |
| 271 |
"SELECT (strftime('%%s',CURRENT_TIMESTAMP)-strftime('%%s',last_action)) > %i AS exp," |
| 272 |
" (access IS NOT NULL) AS access" |
| 273 |
" FROM eurephia_adminlog" |
| 274 |
" LEFT JOIN eurephia_adminaccess USING(uid,interface)" |
| 275 |
" WHERE status IN (1,2)" |
| 276 |
" AND sessionkey = '%q'" |
| 277 |
" AND access = '%q'" |
| 278 |
" AND interface = '%c'", |
| 279 |
expire_time, sesskey, req_access, interface); |
| 280 |
|
| 281 |
if( sqlite_query_status(res) != dbSUCCESS ) { |
| 282 |
eurephia_log(ctx, LOG_FATAL, 0, "Could not validate session"); |
| 283 |
err_n = sqlite_log_error_xml(ctx, res); |
| 284 |
ret_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Session authentication failed"); |
| 285 |
sqlite_free_results(res); |
| 286 |
xmlFreeNode(err_n); |
| 287 |
return ret_d; |
| 288 |
} |
| 289 |
|
| 290 |
valid = (atoi_nullsafe(sqlite_get_value(res, 0, 0)) == 0); |
| 291 |
access = (atoi_nullsafe(sqlite_get_value(res, 0, 1)) == 1); |
| 292 |
sqlite_free_results(res); |
| 293 |
|
| 294 |
// If still valid, update last_action |
| 295 |
if( valid && access ) { |
| 296 |
res = sqlite_query(ctx, |
| 297 |
"UPDATE eurephia_adminlog" |
| 298 |
" SET last_action = CURRENT_TIMESTAMP, status = 2" |
| 299 |
" WHERE sessionkey = '%q'", sesskey); |
| 300 |
if( sqlite_query_status(res) != dbSUCCESS ) { |
| 301 |
eurephia_log(ctx, LOG_ERROR, 0, "Could not register session activity"); |
| 302 |
err_n = sqlite_log_error_xml(ctx, res); |
| 303 |
} |
| 304 |
sqlite_free_results(res); |
| 305 |
|
| 306 |
} else { |
| 307 |
// If not valid, register session as auto-logged out |
| 308 |
res = sqlite_query(ctx, |
| 309 |
"UPDATE eurephia_adminlog" |
| 310 |
" SET logout = CURRENT_TIMESTAMP, status = %i" |
| 311 |
" WHERE sessionkey = '%q'", |
| 312 |
(access ? 4 : 5), sesskey); |
| 313 |
if( sqlite_query_status(res) != dbSUCCESS ) { |
| 314 |
eurephia_log(ctx, LOG_ERROR, 0, "Could not register old session as logged out"); |
| 315 |
err_n = sqlite_log_error_xml(ctx, res); |
| 316 |
} |
| 317 |
sqlite_free_results(res); |
| 318 |
|
| 319 |
// Delete session variables |
| 320 |
res = sqlite_query(ctx, "DELETE FROM openvpn_sessions WHERE sessionkey = '%q'", |
| 321 |
sesskey); |
| 322 |
if( sqlite_query_status(res) != dbSUCCESS ) { |
| 323 |
eurephia_log(ctx, LOG_ERROR, 0, |
| 324 |
"Could not delete session variables (%s))", sesskey); |
| 325 |
sqlite_log_error(ctx, res); |
| 326 |
} else if( !access ) { |
| 327 |
eurephia_log(ctx, LOG_WARNING, 0, "User account is lacking privileges"); |
| 328 |
} |
| 329 |
sqlite_free_results(res); |
| 330 |
} |
| 331 |
|
| 332 |
if (valid && access) { |
| 333 |
ret_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, err_n, "Session authenticated"); |
| 334 |
} else { |
| 335 |
ret_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, "Session authentication failed"); |
| 336 |
} |
| 337 |
if( err_n != NULL ) { |
| 338 |
xmlFreeNode(err_n); |
| 339 |
} |
| 340 |
return ret_d; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
/** |
| 345 |
* Registers the user as logged in after a successful authentication. The user must |
| 346 |
* be registered as logged in to have a valid session. |
| 347 |
* |
| 348 |
* @param ctx eurephiaCTX |
| 349 |
* @param uid Numeric value if the user ID the session belongs to |
| 350 |
* @param sesskey String containing the session key |
| 351 |
* |
| 352 |
* @return Returns an eurephia ResultMsg XML document with the result. On fatal errors, NULL is returned |
| 353 |
*/ |
| 354 |
static xmlDoc *register_login(eurephiaCTX *ctx, const int uid, const char *sesskey) { |
| 355 |
xmlDoc *ret_d = NULL; |
| 356 |
dbresult *res = NULL; |
| 357 |
char interface; |
| 358 |
|
| 359 |
DEBUG(ctx, 21, "Function call: register_login(ctx, %i, '%s')", uid, sesskey); |
| 360 |
assert( ctx != NULL ); |
| 361 |
|
| 362 |
if( (sesskey == NULL) || (uid < 1) ) { |
| 363 |
return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Invalid data for login registration"); |
| 364 |
} |
| 365 |
|
| 366 |
switch( ctx->context_type ) { |
| 367 |
case ECTX_ADMIN_CONSOLE: |
| 368 |
interface = 'C'; break; |
| 369 |
case ECTX_ADMIN_WEB: |
| 370 |
interface = 'W'; break; |
| 371 |
default: |
| 372 |
eurephia_log(ctx, LOG_ERROR, 0, "Wrong eurephia context type (0x%04x)", ctx->context_type); |
| 373 |
return NULL; |
| 374 |
} |
| 375 |
|
| 376 |
// Register login into eurephia_adminlog ... uid, login, interface, sessionkey |
| 377 |
res = sqlite_query(ctx, |
| 378 |
"INSERT INTO eurephia_adminlog " |
| 379 |
" (uid, interface, status, login, last_action, sessionkey) " |
| 380 |
"VALUES ('%i','%c',1,CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, '%q')", |
| 381 |
uid, interface, sesskey); |
| 382 |
if( sqlite_query_status(res) == dbSUCCESS ) { |
| 383 |
ret_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "Session is registered as logged in"); |
| 384 |
} else { |
| 385 |
xmlNode *err_n = NULL; |
| 386 |
|
| 387 |
eurephia_log(ctx, LOG_FATAL, 0, "Failed to register the session in the database"); |
| 388 |
err_n = sqlite_log_error_xml(ctx, res); |
| 389 |
ret_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, |
| 390 |
"Failed to register the session in the database"); |
| 391 |
xmlFreeNode(err_n); |
| 392 |
} |
| 393 |
sqlite_free_results(res); |
| 394 |
return ret_d; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
/** |
| 399 |
* Registers a session as logged out. This will require the user to do a new authentication |
| 400 |
* on next access via the administration interface |
| 401 |
* |
| 402 |
* @param ctx eurephiaCTX |
| 403 |
* @param sessionkey String containing the session key |
| 404 |
* |
| 405 |
* @return Returns an eurephia ResultMsg XML document with the result. On fatal errors, NULL is returned |
| 406 |
*/ |
| 407 |
static xmlDoc *register_logout(eurephiaCTX *ctx, const char *sessionkey) { |
| 408 |
dbresult *res = NULL; |
| 409 |
xmlDoc *ret_d = NULL; |
| 410 |
xmlNode *err_n = NULL; |
| 411 |
|
| 412 |
DEBUG(ctx, 21, "Function call: register_logout(ctx, '%s')", sessionkey); |
| 413 |
assert((ctx != NULL) && (sessionkey != NULL)); |
| 414 |
|
| 415 |
if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { |
| 416 |
eurephia_log(ctx, LOG_CRITICAL, 0, |
| 417 |
"eurephia admin function call attempted with wrong context type"); |
| 418 |
return NULL; |
| 419 |
} |
| 420 |
|
| 421 |
// Update session as logged out |
| 422 |
res = sqlite_query(ctx, |
| 423 |
"UPDATE eurephia_adminlog " |
| 424 |
" SET logout = CURRENT_TIMESTAMP, status = 3" |
| 425 |
" WHERE sessionkey = '%q'", |
| 426 |
sessionkey); |
| 427 |
if( (sqlite_query_status(res) != dbSUCCESS) || (sqlite_get_affected_rows(res) == 0) ) { |
| 428 |
eurephia_log(ctx, LOG_FATAL, 0, |
| 429 |
"Failed to register the session as logged out (updated %i rows)", |
| 430 |
sqlite_get_affected_rows(res)); |
| 431 |
if( sqlite_query_status(res) == dbERROR ) { |
| 432 |
err_n = sqlite_log_error_xml(ctx, res); |
| 433 |
} |
| 434 |
ret_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, |
| 435 |
"Failed to register the session as logged out"); |
| 436 |
xmlFreeNode(err_n); |
| 437 |
goto exit; |
| 438 |
} |
| 439 |
sqlite_free_results(res); |
| 440 |
|
| 441 |
// Delete session variables |
| 442 |
res = sqlite_query(ctx, "DELETE FROM openvpn_sessions WHERE sessionkey = '%q'", sessionkey); |
| 443 |
if( (sqlite_query_status(res) == dbSUCCESS) && (sqlite_get_affected_rows(res) > 0) ) { |
| 444 |
ret_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "Session is logged out"); |
| 445 |
} else { |
| 446 |
eurephia_log(ctx, LOG_ERROR, 0, |
| 447 |
"Could not delete session variables (%s))", sessionkey); |
| 448 |
if( sqlite_query_status(res) == dbERROR ) { |
| 449 |
err_n = sqlite_log_error_xml(ctx, res); |
| 450 |
} |
| 451 |
ret_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, |
| 452 |
"Could not delete session variables (%s)", sessionkey); |
| 453 |
xmlFreeNode(err_n); |
| 454 |
} |
| 455 |
|
| 456 |
exit: |
| 457 |
sqlite_free_results(res); |
| 458 |
return ret_d; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
/** |
| 463 |
* @copydoc eDBadminAuthenticate() |
| 464 |
*/ |
| 465 |
xmlDoc *eDBadminAuthenticate(eurephiaCTX *ctx, xmlDoc *qryxml) { |
| 466 |
xmlDoc *res_d = NULL; |
| 467 |
xmlNode *qry_n = NULL; |
| 468 |
char *mode = NULL; |
| 469 |
int type = 0; |
| 470 |
|
| 471 |
DEBUG(ctx, 20, "Function call: eDBadminAuthenticate(ctx, xmlDoc)"); |
| 472 |
assert( (ctx != NULL) && (qryxml != NULL) ); |
| 473 |
|
| 474 |
if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { |
| 475 |
eurephia_log(ctx, LOG_CRITICAL, 0, |
| 476 |
"eurephia admin function call attempted with wrong context type"); |
| 477 |
return NULL; |
| 478 |
} |
| 479 |
|
| 480 |
qry_n = eurephiaXML_getRoot(ctx, qryxml, "Authenticate", 1); |
| 481 |
if( qry_n != NULL ) { |
| 482 |
type = 1; |
| 483 |
goto accept; |
| 484 |
} |
| 485 |
|
| 486 |
qry_n = eurephiaXML_getRoot(ctx, qryxml, "Register", 1); |
| 487 |
if( qry_n != NULL ) { |
| 488 |
type = 2; |
| 489 |
goto accept; |
| 490 |
} |
| 491 |
|
| 492 |
eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML request for eDBadminAuthenticate()"); |
| 493 |
return NULL; |
| 494 |
|
| 495 |
accept: |
| 496 |
mode = xmlGetAttrValue(qry_n->properties, "mode"); |
| 497 |
if( mode == NULL ) { |
| 498 |
eurephia_log(ctx, LOG_ERROR, 0, "Invalid authentication request"); |
| 499 |
return NULL; |
| 500 |
} |
| 501 |
|
| 502 |
switch( type ) { |
| 503 |
case 1: // Authenticate tag |
| 504 |
if( strcmp(mode, "user") == 0 ) { |
| 505 |
// const char *req_access, const char *uname, const char *pwd |
| 506 |
const char *reqacc = NULL, *uname = NULL, *pwd = NULL; |
| 507 |
uname = xmlGetNodeContent(qry_n, "username"); |
| 508 |
pwd = xmlGetNodeContent(qry_n, "password"); |
| 509 |
reqacc = xmlGetNodeContent(qry_n, "accesslevel"); |
| 510 |
res_d = auth_user(ctx, reqacc, uname, pwd); |
| 511 |
} else if ( strcmp(mode, "session") == 0 ) { |
| 512 |
const char *sesskey = NULL, *reqacc = NULL; |
| 513 |
sesskey = xmlGetNodeContent(qry_n, "sessionkey"); |
| 514 |
reqacc = xmlGetNodeContent(qry_n, "accesslevel"); |
| 515 |
res_d = auth_session(ctx, sesskey, reqacc); |
| 516 |
} |
| 517 |
break; |
| 518 |
|
| 519 |
case 2: // Register tag |
| 520 |
if( strcmp(mode, "login") == 0 ) { |
| 521 |
const char *sesskey = NULL; |
| 522 |
unsigned int uid = 0; |
| 523 |
|
| 524 |
uid = atoi_nullsafe(xmlGetAttrValue(qry_n->properties, "uid")); |
| 525 |
sesskey = xmlExtractContent(qry_n); |
| 526 |
res_d = register_login(ctx, uid, sesskey); |
| 527 |
} else if( strcmp(mode, "logout") == 0 ) { |
| 528 |
const char *sesskey = NULL; |
| 529 |
|
| 530 |
sesskey = xmlExtractContent(qry_n); |
| 531 |
res_d = register_logout(ctx, sesskey); |
| 532 |
} |
| 533 |
break; |
| 534 |
|
| 535 |
default: |
| 536 |
eurephia_log(ctx, LOG_FATAL, 0, "The unthinkable has just happened (type %i)", type); |
| 537 |
res_d = NULL; |
| 538 |
break; |
| 539 |
} |
| 540 |
return res_d; |
| 541 |
} |
| 542 |
|
| 543 |
#endif |