|
|
|
@ -53,11 +53,15 @@
|
|
|
|
|
#define RAPTORSMIFACE_CFG_DATABASE_USER "User"
|
|
|
|
|
#define RAPTORSMIFACE_CFG_DATABASE_PASSWORD "Password"
|
|
|
|
|
|
|
|
|
|
#define RAPTORSMIFACE_CFG_DATABASE_SCHEMA_VERSION 1
|
|
|
|
|
|
|
|
|
|
char *server = NULL;
|
|
|
|
|
char *user = NULL;
|
|
|
|
|
char *password = NULL;
|
|
|
|
|
char *database = NULL;
|
|
|
|
|
|
|
|
|
|
char *local_machine_fqdn = NULL;
|
|
|
|
|
|
|
|
|
|
void dprint(const char *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list argp;
|
|
|
|
@ -126,6 +130,10 @@ void read_ini_configuration() {
|
|
|
|
|
struct list* param_n;
|
|
|
|
|
struct list* param_v;
|
|
|
|
|
|
|
|
|
|
/* set global variables */
|
|
|
|
|
local_machine_fqdn = raptor_sm_get_local_machine_fqdn();
|
|
|
|
|
|
|
|
|
|
/* open configuration file */
|
|
|
|
|
char cfg_file[256];
|
|
|
|
|
g_snprintf(cfg_file, 255, "%s/xrdp.ini", XRDP_CFG_PATH);
|
|
|
|
|
|
|
|
|
@ -160,6 +168,39 @@ MYSQL * connect_if_needed() {
|
|
|
|
|
dprint("[ERROR] MySQL connection FAILED [%s]\n\r", mysql_error(conn));
|
|
|
|
|
conn = 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// Check schema version
|
|
|
|
|
char* query;
|
|
|
|
|
MYSQL_RES *res;
|
|
|
|
|
MYSQL_ROW row;
|
|
|
|
|
asprintf(&query, "SELECT value FROM dbschema WHERE skey='revision'");
|
|
|
|
|
if (mysql_query_internal(conn, query)) {
|
|
|
|
|
// Server error
|
|
|
|
|
free(query);
|
|
|
|
|
mysql_close(conn);
|
|
|
|
|
dprint("[ERROR] MySQL connection FAILED [%s]\n\r", mysql_error(conn));
|
|
|
|
|
conn = 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
free(query);
|
|
|
|
|
res = mysql_store_result(conn);
|
|
|
|
|
if ((row = mysql_fetch_row(res)) == NULL) {
|
|
|
|
|
dprint("[ERROR] Mandatory schema version key not found\n\r");
|
|
|
|
|
conn = 0;
|
|
|
|
|
}
|
|
|
|
|
else if (!row[0]) {
|
|
|
|
|
dprint("[ERROR] Mandatory schema version key not found\n\r");
|
|
|
|
|
conn = 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
int schema_version = atoi(row[0]);
|
|
|
|
|
if (schema_version != RAPTORSMIFACE_CFG_DATABASE_SCHEMA_VERSION) {
|
|
|
|
|
dprint("[ERROR] Schema version key mismatch (%d:%d)\n\r", schema_version, RAPTORSMIFACE_CFG_DATABASE_SCHEMA_VERSION);
|
|
|
|
|
conn = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return conn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -285,10 +326,12 @@ char raptor_sm_deallocate_session(char* username) {
|
|
|
|
|
// Insert connection information into the statistics database
|
|
|
|
|
char* safe_servername = get_mysql_escaped_string(conn, hostname);
|
|
|
|
|
char* safe_username = get_mysql_escaped_string(conn, username);
|
|
|
|
|
char* safe_local_machine_fqdn = get_mysql_escaped_string(conn, local_machine_fqdn);
|
|
|
|
|
long long timestamp = time(NULL);
|
|
|
|
|
asprintf(&query, "INSERT INTO statistics (timestamp, eventtypeid, servername, display, typeid, username) VALUES ('%lld', '%d', '%s', '%d', '%d', '%s')", timestamp, STATISTICS_DISCONNECTION_EVENT, safe_servername, display, -1, safe_username);
|
|
|
|
|
asprintf(&query, "INSERT INTO statistics (timestamp, eventtypeid, arbiter, servername, display, typeid, username) VALUES ('%lld', '%d', '%s', '%s', '%d', '%d', '%s')", timestamp, STATISTICS_DISCONNECTION_EVENT, safe_local_machine_fqdn, safe_servername, display, -1, safe_username);
|
|
|
|
|
free(safe_servername);
|
|
|
|
|
free(safe_username);
|
|
|
|
|
free(safe_local_machine_fqdn);
|
|
|
|
|
free(hostname);
|
|
|
|
|
if (mysql_query_internal(conn, query)) {
|
|
|
|
|
// Server error
|
|
|
|
@ -409,7 +452,9 @@ char* raptor_sm_allocate_session(char* username) {
|
|
|
|
|
// Insert new information into the sessions database and set status to ALLOCATED
|
|
|
|
|
char* safe_servername = get_mysql_escaped_string(conn, bestserver);
|
|
|
|
|
char* safe_username = get_mysql_escaped_string(conn, username);
|
|
|
|
|
asprintf(&query, "INSERT INTO sessions (username, servername, state) VALUES ('%s', '%s', '%d')", safe_username, safe_servername, SM_STATUS_ALLOCATED);
|
|
|
|
|
char* safe_local_machine_fqdn = get_mysql_escaped_string(conn, local_machine_fqdn);
|
|
|
|
|
asprintf(&query, "INSERT INTO sessions (username, arbiter, servername, state) VALUES ('%s', '%s', '%s', '%d')", safe_username, safe_local_machine_fqdn, safe_servername, SM_STATUS_ALLOCATED);
|
|
|
|
|
free(safe_local_machine_fqdn);
|
|
|
|
|
free(safe_servername);
|
|
|
|
|
free(safe_username);
|
|
|
|
|
if (mysql_query_internal(conn, query)) {
|
|
|
|
@ -424,8 +469,10 @@ char* raptor_sm_allocate_session(char* username) {
|
|
|
|
|
// Insert connection information into the statistics database
|
|
|
|
|
char* safe_servername = get_mysql_escaped_string(conn, bestserver);
|
|
|
|
|
char* safe_username = get_mysql_escaped_string(conn, username);
|
|
|
|
|
char* safe_local_machine_fqdn = get_mysql_escaped_string(conn, local_machine_fqdn);
|
|
|
|
|
long long timestamp = time(NULL);
|
|
|
|
|
asprintf(&query, "INSERT INTO statistics (timestamp, eventtypeid, servername, display, typeid, username) VALUES ('%lld', '%d', '%s', '%d', '%d', '%s')", timestamp, STATISTICS_NEW_CONNECTION_EVENT, safe_servername, -1, SM_STATUS_ALLOCATED, safe_username);
|
|
|
|
|
asprintf(&query, "INSERT INTO statistics (timestamp, eventtypeid, arbiter, servername, display, typeid, username) VALUES ('%lld', '%d', '%s', '%s', '%d', '%d', '%s')", timestamp, STATISTICS_NEW_CONNECTION_EVENT, safe_local_machine_fqdn, safe_servername, -1, SM_STATUS_ALLOCATED, safe_username);
|
|
|
|
|
free(safe_local_machine_fqdn);
|
|
|
|
|
free(safe_servername);
|
|
|
|
|
free(safe_username);
|
|
|
|
|
if (mysql_query_internal(conn, query)) {
|
|
|
|
@ -455,6 +502,28 @@ char* raptor_sm_allocate_session(char* username) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* raptor_sm_get_local_machine_fqdn() {
|
|
|
|
|
struct addrinfo hints, *res;
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
char hostname[1024];
|
|
|
|
|
hostname[1023] = '\0';
|
|
|
|
|
gethostname(hostname, 1023);
|
|
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof hints);
|
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
|
hints.ai_flags = AI_CANONNAME;
|
|
|
|
|
|
|
|
|
|
if ((err = getaddrinfo(hostname, NULL, &hints, &res)) != 0) {
|
|
|
|
|
return strdup("");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* ret = strdup(res->ai_canonname);
|
|
|
|
|
freeaddrinfo(res);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* raptor_sm_get_ip_for_hostname(char* hostname, char* error) {
|
|
|
|
|
struct addrinfo hints, *res;
|
|
|
|
|
struct in_addr addr;
|
|
|
|
@ -854,10 +923,12 @@ int raptor_sm_server_started(char* username, pid_t pid, int display, char* dbfie
|
|
|
|
|
char* current_server = raptor_sm_get_hostname_for_display(display);
|
|
|
|
|
char* safe_servername = get_mysql_escaped_string(conn, current_server);
|
|
|
|
|
char* safe_username = get_mysql_escaped_string(conn, username);
|
|
|
|
|
char* safe_local_machine_fqdn = get_mysql_escaped_string(conn, local_machine_fqdn);
|
|
|
|
|
long long timestamp = time(NULL);
|
|
|
|
|
asprintf(&query, "INSERT INTO statistics (timestamp, eventtypeid, servername, display, typeid, username) VALUES ('%lld', '%d', '%s', '%d', '%d', '%s')", timestamp, STATISTICS_CONNECTION_STATUS_EVENT, safe_servername, display, SM_STATUS_RUNNING, safe_username);
|
|
|
|
|
asprintf(&query, "INSERT INTO statistics (timestamp, eventtypeid, arbiter, servername, display, typeid, username) VALUES ('%lld', '%d', '%s', '%s', '%d', '%d', '%s')", timestamp, STATISTICS_CONNECTION_STATUS_EVENT, safe_local_machine_fqdn, safe_servername, display, SM_STATUS_RUNNING, safe_username);
|
|
|
|
|
free(safe_servername);
|
|
|
|
|
free(safe_username);
|
|
|
|
|
free(safe_local_machine_fqdn);
|
|
|
|
|
free(current_server);
|
|
|
|
|
if (mysql_query_internal(conn, query)) {
|
|
|
|
|
// Server error
|
|
|
|
@ -1093,10 +1164,12 @@ char raptor_sm_set_session_state(int display, int state) {
|
|
|
|
|
char* username = raptor_sm_get_username_for_display_and_hostname(display, hostname);
|
|
|
|
|
char* safe_servername = get_mysql_escaped_string(conn, hostname);
|
|
|
|
|
char* safe_username = get_mysql_escaped_string(conn, username);
|
|
|
|
|
char* safe_local_machine_fqdn = get_mysql_escaped_string(conn, local_machine_fqdn);
|
|
|
|
|
long long timestamp = time(NULL);
|
|
|
|
|
asprintf(&query, "INSERT INTO statistics (timestamp, eventtypeid, servername, display, typeid, username) VALUES ('%lld', '%d', '%s', '%d', '%d', '%s')", timestamp, STATISTICS_CONNECTION_STATUS_EVENT, safe_servername, display, state, safe_username);
|
|
|
|
|
asprintf(&query, "INSERT INTO statistics (timestamp, eventtypeid, arbiter, servername, display, typeid, username) VALUES ('%lld', '%d', '%s', '%s', '%d', '%d', '%s')", timestamp, STATISTICS_CONNECTION_STATUS_EVENT, safe_local_machine_fqdn, safe_servername, display, state, safe_username);
|
|
|
|
|
free(safe_servername);
|
|
|
|
|
free(safe_username);
|
|
|
|
|
free(safe_local_machine_fqdn);
|
|
|
|
|
free(hostname);
|
|
|
|
|
free(username);
|
|
|
|
|
if (mysql_query_internal(conn, query)) {
|
|
|
|
@ -1143,9 +1216,11 @@ int raptor_sm_stats_report_server_start(char* hostname) {
|
|
|
|
|
|
|
|
|
|
// Insert information into the statistics database
|
|
|
|
|
char* safe_servername = get_mysql_escaped_string(conn, hostname);
|
|
|
|
|
char* safe_local_machine_fqdn = get_mysql_escaped_string(conn, local_machine_fqdn);
|
|
|
|
|
long long timestamp = time(NULL);
|
|
|
|
|
asprintf(&query, "INSERT INTO statistics (timestamp, eventtypeid, servername, display, typeid) VALUES ('%lld', '%d', '%s', '%d', '%d')", timestamp, STATISTICS_SERVER_START_EVENT, safe_servername, -1, -1);
|
|
|
|
|
asprintf(&query, "INSERT INTO statistics (timestamp, eventtypeid, arbiter, servername, display, typeid) VALUES ('%lld', '%d', '%s', '%s', '%d', '%d')", timestamp, STATISTICS_SERVER_START_EVENT, safe_local_machine_fqdn, safe_servername, -1, -1);
|
|
|
|
|
free(safe_servername);
|
|
|
|
|
free(safe_local_machine_fqdn);
|
|
|
|
|
if (mysql_query_internal(conn, query)) {
|
|
|
|
|
// Server error
|
|
|
|
|
dprint("Unable to insert data into statistics database! [%s]\n\r", mysql_error(conn));
|
|
|
|
@ -1168,9 +1243,11 @@ int raptor_sm_stats_report_server_stop(char* hostname) {
|
|
|
|
|
|
|
|
|
|
// Insert information into the statistics database
|
|
|
|
|
char* safe_servername = get_mysql_escaped_string(conn, hostname);
|
|
|
|
|
char* safe_local_machine_fqdn = get_mysql_escaped_string(conn, local_machine_fqdn);
|
|
|
|
|
long long timestamp = time(NULL);
|
|
|
|
|
asprintf(&query, "INSERT INTO statistics (timestamp, eventtypeid, servername, display, typeid) VALUES ('%lld', '%d', '%s', '%d', '%d')", timestamp, STATISTICS_SERVER_STOP_EVENT, safe_servername, -1, -1);
|
|
|
|
|
asprintf(&query, "INSERT INTO statistics (timestamp, eventtypeid, arbiter, servername, display, typeid) VALUES ('%lld', '%d', '%s', '%s', '%d', '%d')", timestamp, STATISTICS_SERVER_STOP_EVENT, safe_local_machine_fqdn, safe_servername, -1, -1);
|
|
|
|
|
free(safe_servername);
|
|
|
|
|
free(safe_local_machine_fqdn);
|
|
|
|
|
if (mysql_query_internal(conn, query)) {
|
|
|
|
|
// Server error
|
|
|
|
|
dprint("Unable to insert data into statistics database! [%s]\n\r", mysql_error(conn));
|
|
|
|
|