summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTimothy Pearson <kb9vqf@pearsoncomputing.net>2012-05-22 21:50:48 +0000
committerTimothy Pearson <tpearson@raptorengineering.com>2019-03-17 00:34:02 -0500
commit46a8e6373e95c1b1fe632da47aa87abcc0a379b1 (patch)
tree87abda891c76f830245f1da0ac95c9476b8fdd5b
parentffeda4fd0cf7e6c548da8d273c8a1b4b02ec98f3 (diff)
downloadxrdp-proprietary-46a8e6373e95c1b1fe632da47aa87abcc0a379b1.tar.gz
xrdp-proprietary-46a8e6373e95c1b1fe632da47aa87abcc0a379b1.zip
Fix a number of problems
System is now mostly stabilized
-rw-r--r--common/defines.h2
-rw-r--r--common/trans.c50
-rw-r--r--raptorsmiface/libraptorsmiface.c189
-rw-r--r--raptorsmiface/libraptorsmiface.h4
-rw-r--r--sesman/chansrv/chansrv.c5
-rw-r--r--sesman/scp_v0.c13
-rw-r--r--sesman/scp_v1.c9
-rw-r--r--sesman/session.c168
-rw-r--r--xrdp/xrdp_mm.c25
-rw-r--r--xup/xup.c9
10 files changed, 419 insertions, 55 deletions
diff --git a/common/defines.h b/common/defines.h
index d87ddae7..74723f45 100644
--- a/common/defines.h
+++ b/common/defines.h
@@ -21,6 +21,8 @@
#ifndef DEFINES_H
#define DEFINES_H
+#define DISABLE_UNIX_DOMAIN_SOCKETS 1
+
/* check for debug */
#ifdef XRDP_DEBUG
#define DEBUG(args) g_writeln args;
diff --git a/common/trans.c b/common/trans.c
index 9611f768..525316c5 100644
--- a/common/trans.c
+++ b/common/trans.c
@@ -490,6 +490,7 @@ trans_force_read(struct trans *self, int size)
return trans_force_read_s(self, self->in_s, size);
}
+#if 0
/*****************************************************************************/
int APP_CC
trans_force_write_s(struct trans *self, struct stream *out_s)
@@ -550,6 +551,55 @@ trans_force_write_s(struct trans *self, struct stream *out_s)
}
return 0;
}
+#else
+// DEBUG ONLY
+/*****************************************************************************/
+int APP_CC
+trans_force_write_s(struct trans* self, struct stream* out_s)
+{
+ int size;
+ int total;
+ int sent;
+
+ if (self->status != TRANS_STATUS_UP)
+ {
+ return 1;
+ }
+ size = (int)(out_s->end - out_s->data);
+ total = 0;
+ while (total < size)
+ {
+ sent = g_tcp_send(self->sck, out_s->data + total, size - total, 0);
+ if (sent == -1)
+ {
+ if (g_tcp_last_error_would_block(self->sck))
+ {
+ if (!g_tcp_can_send(self->sck, 10))
+ {
+ /* check for term here */
+ }
+ }
+ else
+ {
+ /* error */
+ self->status = TRANS_STATUS_DOWN;
+ return 2;
+ }
+ }
+ else if (sent == 0)
+ {
+ /* error */
+ self->status = TRANS_STATUS_DOWN;
+ return 3;
+ }
+ else
+ {
+ total = total + sent;
+ }
+ }
+ return 0;
+}
+#endif
/*****************************************************************************/
int APP_CC
diff --git a/raptorsmiface/libraptorsmiface.c b/raptorsmiface/libraptorsmiface.c
index 4d3db02c..933ea98e 100644
--- a/raptorsmiface/libraptorsmiface.c
+++ b/raptorsmiface/libraptorsmiface.c
@@ -23,8 +23,6 @@
#include "libraptorsmiface.h"
-MYSQL *conn = 0;
-
char *server = "localhost";
char *user = "remotelab";
char *password = "rlpass123"; /* set me first */
@@ -38,7 +36,7 @@ void dprint(const char *fmt, ...)
#if 0
vprintf(fmt, argp);
#else
- char debug[1024];
+ char debug[512];
vsprintf(debug, fmt, argp);
FILE *fp = fopen("/raptorsmiface.debug", "a");
if (fp != NULL)
@@ -51,14 +49,13 @@ void dprint(const char *fmt, ...)
va_end(argp);
}
-void connect_if_needed() {
- if (!conn) {
- conn = mysql_init(NULL);
- if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
- dprint("[ERROR] MySQL connection FAILED [%s]\n\r", mysql_error(conn));
- conn = 0;
- }
+MYSQL * connect_if_needed() {
+ MYSQL *conn = mysql_init(NULL);
+ if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
+ dprint("[ERROR] MySQL connection FAILED [%s]\n\r", mysql_error(conn));
+ conn = 0;
}
+ return conn;
}
char* get_mysql_escaped_string(MYSQL *sqlcn, char* rawstr) {
@@ -69,6 +66,13 @@ char* get_mysql_escaped_string(MYSQL *sqlcn, char* rawstr) {
return escstr;
}
+char mutex;
+int mysql_query_internal(MYSQL *conn, const char * query) {
+ // For some reason this can hang rather badly
+ // It might be related to concurrent access to the same conn object though
+ return mysql_query(conn, query);
+}
+
char* get_group_for_user(char* username) {
struct passwd* pwd;
pwd = getpwnam(username);
@@ -94,7 +98,7 @@ char raptor_sm_deallocate_session(char* username) {
MYSQL_ROW cnt_row;
char* query;
- connect_if_needed();
+ MYSQL *conn = connect_if_needed();
if (!conn) {
return 1;
}
@@ -103,13 +107,15 @@ char raptor_sm_deallocate_session(char* username) {
char* safe_username = get_mysql_escaped_string(conn, username);
asprintf(&query, "DELETE FROM sessions WHERE username='%s'", safe_username);
free(safe_username);
- if (mysql_query(conn, query)) {
+ if (mysql_query_internal(conn, query)) {
// Server error
free(query);
+ mysql_close(conn);
return 2;
}
else {
free(query);
+ mysql_close(conn);
return 0;
}
}
@@ -123,7 +129,7 @@ char* raptor_sm_allocate_session(char* username) {
MYSQL_ROW cnt_row;
char* query;
- connect_if_needed();
+ MYSQL *conn = connect_if_needed();
if (!conn) {
return strdup("SQLERR001");
}
@@ -132,9 +138,10 @@ char* raptor_sm_allocate_session(char* username) {
char* safe_username = get_mysql_escaped_string(conn, username);
asprintf(&query, "SELECT servername FROM sessions WHERE username='%s'", safe_username);
free(safe_username);
- if (mysql_query(conn, query)) {
+ if (mysql_query_internal(conn, query)) {
// Server error
free(query);
+ mysql_close(conn);
return strdup("SQLERR002");
}
else {
@@ -143,9 +150,10 @@ char* raptor_sm_allocate_session(char* username) {
if ((row = mysql_fetch_row(res)) == NULL) {
// User is not on a system
// Find the least utilized node
- if (mysql_query(conn, "SELECT name FROM servers")) {
+ if (mysql_query_internal(conn, "SELECT name FROM servers WHERE online='1'")) {
// Server error
mysql_free_result(res);
+ mysql_close(conn);
return strdup("SQLERR003");
}
else {
@@ -156,12 +164,13 @@ char* raptor_sm_allocate_session(char* username) {
char* safe_servername = get_mysql_escaped_string(conn, svr_row[0]);
asprintf(&query, "SELECT username FROM sessions WHERE servername='%s'", safe_servername);
free(safe_servername);
- if (mysql_query(conn, query)) {
+ if (mysql_query_internal(conn, query)) {
// Server error
free(query);
free(bestserver);
mysql_free_result(res);
mysql_free_result(svr_res);
+ mysql_close(conn);
return strdup("SQLERR004");
}
else {
@@ -188,13 +197,15 @@ char* raptor_sm_allocate_session(char* username) {
asprintf(&query, "INSERT INTO sessions (username, servername, state) VALUES ('%s', '%s', '%d')", safe_username, safe_servername, SM_STATUS_ALLOCATED);
free(safe_servername);
free(safe_username);
- if (mysql_query(conn, query)) {
+ if (mysql_query_internal(conn, query)) {
// Server error
free(query);
+ mysql_close(conn);
return strdup("SQLERR005");
}
else {
free(query);
+ mysql_close(conn);
return strdup(bestserver);
}
}
@@ -202,6 +213,7 @@ char* raptor_sm_allocate_session(char* username) {
else {
char* ret = strdup(row[0]);
mysql_free_result(res);
+ mysql_close(conn);
return ret;
}
}
@@ -234,7 +246,7 @@ char* raptor_sm_get_hostname_for_username(char* username, bool create) {
MYSQL_ROW row;
char* query;
- connect_if_needed();
+ MYSQL *conn = connect_if_needed();
if (!conn) {
return strdup("SQLERR100");
}
@@ -242,9 +254,10 @@ char* raptor_sm_get_hostname_for_username(char* username, bool create) {
char* safe_username = get_mysql_escaped_string(conn, username);
asprintf(&query, "SELECT servername FROM sessions WHERE username='%s'", safe_username);
free(safe_username);
- if (mysql_query(conn, query)) {
+ if (mysql_query_internal(conn, query)) {
// Server error
free(query);
+ mysql_close(conn);
return strdup("SQLERR101");
}
else {
@@ -253,15 +266,18 @@ char* raptor_sm_get_hostname_for_username(char* username, bool create) {
while ((row = mysql_fetch_row(res)) != NULL) {
char* ret = strdup(row[0]);
mysql_free_result(res);
+ mysql_close(conn);
return ret;
}
// Nothing in the DB
mysql_free_result(res);
if (create) {
// Try to allocate a new session on a node
+ mysql_close(conn);
return raptor_sm_allocate_session(username);
}
else {
+ mysql_close(conn);
return strdup("");
}
}
@@ -283,7 +299,7 @@ bool raptor_sm_sesslimit_reached(char* username) {
MYSQL_ROW row;
char* query;
- connect_if_needed();
+ MYSQL *conn = connect_if_needed();
if (!conn) {
return true;
}
@@ -297,9 +313,10 @@ bool raptor_sm_sesslimit_reached(char* username) {
free(groupname);
asprintf(&query, "SELECT sesslimit FROM groups WHERE groupname='%s'", safe_groupname);
free(safe_groupname);
- if (mysql_query(conn, query)) {
+ if (mysql_query_internal(conn, query)) {
// Server error
free(query);
+ mysql_close(conn);
return true;
}
else {
@@ -315,9 +332,10 @@ bool raptor_sm_sesslimit_reached(char* username) {
// Figure out how many users are online from this group
int sesscount = 0;
asprintf(&query, "SELECT username FROM sessions WHERE state<>'%d'", SM_STATUS_ALLOCATED);
- if (mysql_query(conn, query)) {
+ if (mysql_query_internal(conn, query)) {
// Server error
free(query);
+ mysql_close(conn);
return true;
}
else {
@@ -335,12 +353,15 @@ bool raptor_sm_sesslimit_reached(char* username) {
mysql_free_result(res);
if (sesscount < sesslimit) {
+ mysql_close(conn);
return false;
}
+ mysql_close(conn);
return true;
}
// We should never end up here!
+ mysql_close(conn);
return true;
}
@@ -349,24 +370,26 @@ pid_t raptor_sm_run_remote_server(char* username, char *const argv[]) {
MYSQL_ROW row;
char* query;
- connect_if_needed();
+ MYSQL *conn = connect_if_needed();
if (!conn) {
return -1;
}
// Respect maximum session number for the group for this user
if (raptor_sm_sesslimit_reached(username)) {
+ mysql_close(conn);
return -5;
}
// Make sure a server is not already running for this user
// Return the existing PID if it is
char* safe_username = get_mysql_escaped_string(conn, username);
- asprintf(&query, "SELECT pid FROM sessions WHERE username='%s' AND state<>'%d'", safe_username, SM_STATUS_ALLOCATED);
+ asprintf(&query, "SELECT pid,servername FROM sessions WHERE username='%s' AND state<>'%d'", safe_username, SM_STATUS_ALLOCATED);
free(safe_username);
- if (mysql_query(conn, query)) {
+ if (mysql_query_internal(conn, query)) {
// Server error
free(query);
+ mysql_close(conn);
return -2;
}
else {
@@ -376,8 +399,31 @@ pid_t raptor_sm_run_remote_server(char* username, char *const argv[]) {
if (row[0]) {
int ret = atoi(row[0]);
if (ret >= 0) {
- mysql_free_result(res);
- return ret;
+ // Verify existence of PID on remote server
+ dprint("Verifying process %d on %s...\n\r", ret, row[1]);
+ char* ip = raptor_sm_get_ip_for_hostname(row[1], 0);
+ char* command_string;
+ asprintf(&command_string, "ssh root@%s \'ps -p %d | grep %d\'", ip, ret, ret);
+ FILE *fp;
+ char output[1024];
+ // Open the command for reading
+ fp = popen(command_string, "r");
+ if (fp == NULL) {
+ mysql_close(conn);
+ return -1;
+ }
+ // Read the output a line at a time
+ fgets(output, sizeof(output)-1, fp);
+ // Close output
+ pclose(fp);
+ free(command_string);
+ free(ip);
+ dprint("...result was %s\n\r", output);
+ if (strcmp(output, "") != 0) {
+ mysql_free_result(res);
+ mysql_close(conn);
+ return ret;
+ }
}
}
}
@@ -402,7 +448,7 @@ pid_t raptor_sm_run_remote_server(char* username, char *const argv[]) {
free(origstr);
}
char* origstr = command_string;
- asprintf(&command_string, "ssh %s \'%s & echo $! &\'", ipaddr, origstr);
+ asprintf(&command_string, "ssh root@%s \'%s & echo $! &\'", ipaddr, origstr);
free(origstr);
FILE *fp;
@@ -411,6 +457,7 @@ pid_t raptor_sm_run_remote_server(char* username, char *const argv[]) {
// Open the command for reading
fp = popen(command_string, "r");
if (fp == NULL) {
+ mysql_close(conn);
return -1;
}
@@ -422,9 +469,51 @@ pid_t raptor_sm_run_remote_server(char* username, char *const argv[]) {
free(command_string);
+ mysql_close(conn);
return atoi(output);
}
+pid_t raptor_sm_get_pid_for_username(char* username) {
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+ char* query;
+
+ MYSQL *conn = connect_if_needed();
+ if (!conn) {
+ return -1;
+ }
+
+ // Make sure a server is not already running for this user
+ // Return the existing PID if it is
+ char* safe_username = get_mysql_escaped_string(conn, username);
+ asprintf(&query, "SELECT pid FROM sessions WHERE username='%s'", safe_username);
+ free(safe_username);
+ if (mysql_query_internal(conn, query)) {
+ // Server error
+ free(query);
+ mysql_close(conn);
+ return -2;
+ }
+ else {
+ free(query);
+ res = mysql_store_result(conn);
+ while ((row = mysql_fetch_row(res)) != NULL) {
+ if (row[0]) {
+ int ret = atoi(row[0]);
+ if (ret >= 0) {
+ mysql_free_result(res);
+ mysql_close(conn);
+ return ret;
+ }
+ }
+ }
+ mysql_free_result(res);
+ }
+
+ mysql_close(conn);
+ return -3;
+}
+
char* raptor_sm_server_started(char* username, pid_t pid, int display) {
MYSQL_RES *res;
MYSQL_ROW row;
@@ -432,7 +521,7 @@ char* raptor_sm_server_started(char* username, pid_t pid, int display) {
long long timestamp = time(NULL);
- connect_if_needed();
+ MYSQL *conn = connect_if_needed();
if (!conn) {
return -1;
}
@@ -441,13 +530,15 @@ char* raptor_sm_server_started(char* username, pid_t pid, int display) {
char* safe_username = get_mysql_escaped_string(conn, username);
asprintf(&query, "UPDATE sessions SET pid='%d', stamp_start='%lld', state='%d', display='%d', stamp_statechange='%lld' WHERE username='%s' AND state='%d'", pid, timestamp, SM_STATUS_RUNNING, display, timestamp, safe_username, SM_STATUS_ALLOCATED);
free(safe_username);
- if (mysql_query(conn, query)) {
+ if (mysql_query_internal(conn, query)) {
// Server error
free(query);
+ mysql_close(conn);
return -2;
}
else {
free(query);
+ mysql_close(conn);
return 0;
}
}
@@ -457,7 +548,7 @@ int raptor_sm_get_display_for_username(char* username) {
MYSQL_ROW row;
char* query;
- connect_if_needed();
+ MYSQL *conn = connect_if_needed();
if (!conn) {
return -1;
}
@@ -465,9 +556,10 @@ int raptor_sm_get_display_for_username(char* username) {
char* safe_username = get_mysql_escaped_string(conn, username);
asprintf(&query, "SELECT display FROM sessions WHERE username='%s'", safe_username);
free(safe_username);
- if (mysql_query(conn, query)) {
+ if (mysql_query_internal(conn, query)) {
// Server error
free(query);
+ mysql_close(conn);
return -2;
}
else {
@@ -477,15 +569,18 @@ int raptor_sm_get_display_for_username(char* username) {
if (row[0]) {
int ret = atoi(row[0]);
mysql_free_result(res);
+ mysql_close(conn);
return ret;
}
else {
mysql_free_result(res);
+ mysql_close(conn);
return -3;
}
}
// Nothing in the DB
mysql_free_result(res);
+ mysql_close(conn);
return -4;
}
}
@@ -494,7 +589,7 @@ void raptor_sm_wait_for_pid_exit(char* username, pid_t pid) {
char* ipaddr = raptor_sm_get_ip_for_username(username, false);
char* command_string;
- asprintf(&command_string, "ssh %s \'while [[ `ps -p %d | grep %d` != \"\" ]]; do sleep 1; done\'", ipaddr, pid, pid);
+ asprintf(&command_string, "ssh root@%s \'while [[ `ps -p %d | grep %d` != \"\" ]]; do sleep 1; done\'", ipaddr, pid, pid);
system(command_string);
free(command_string);
}
@@ -508,15 +603,16 @@ int raptor_sm_get_new_unique_display(int mindisplay, int maxdisplay) {
MYSQL_ROW row;
char* query;
- connect_if_needed();
+ MYSQL *conn = connect_if_needed();
if (!conn) {
return -1;
}
asprintf(&query, "SELECT display FROM sessions");
- if (mysql_query(conn, query)) {
+ if (mysql_query_internal(conn, query)) {
// Server error
free(query);
+ mysql_close(conn);
return -2;
}
else {
@@ -538,6 +634,7 @@ int raptor_sm_get_new_unique_display(int mindisplay, int maxdisplay) {
}
}
mysql_free_result(res);
+ mysql_close(conn);
return freedisp;
}
}
@@ -549,20 +646,38 @@ char raptor_sm_set_session_state(int display, int state) {
long long timestamp = time(NULL);
- connect_if_needed();
+ MYSQL *conn = connect_if_needed();
if (!conn) {
return -1;
}
// Update new state into the sessions database
asprintf(&query, "UPDATE sessions SET state='%d', stamp_statechange='%lld' WHERE display='%d'", state, timestamp, display);
- if (mysql_query(conn, query)) {
+ if (mysql_query_internal(conn, query)) {
// Server error
free(query);
+ mysql_close(conn);
return -2;
}
else {
free(query);
+ mysql_close(conn);
return 0;
}
+}
+
+void raptor_sm_run_remote_desktop(char* username, int display, char* executable) {
+ char* ipaddr = raptor_sm_get_ip_for_username(username, true);
+ char* command_string;
+ asprintf(&command_string, "ssh root@%s \"su %s -c \'export DISPLAY=:%d && %s && exit\' &> /dev/null\"", ipaddr, username, display, executable);
+ system(command_string);
+ free(command_string);
+
+ // Terminate remote X server
+ pid_t pid = raptor_sm_get_pid_for_username(username);
+ if (pid > 0) {
+ asprintf(&command_string, "ssh root@%s \'kill -9 %ld\'", ipaddr, pid);
+ system(command_string);
+ free(command_string);
+ }
} \ No newline at end of file
diff --git a/raptorsmiface/libraptorsmiface.h b/raptorsmiface/libraptorsmiface.h
index 1c53028b..1d56a021 100644
--- a/raptorsmiface/libraptorsmiface.h
+++ b/raptorsmiface/libraptorsmiface.h
@@ -25,10 +25,12 @@ char* raptor_sm_get_hostname_for_username(char* username, bool create);
char* raptor_sm_get_ip_for_username(char* username, bool create);
pid_t raptor_sm_run_remote_server(char* username, char *const argv[]);
+pid_t raptor_sm_get_pid_for_username(char* username);
char* raptor_sm_server_started(char* username, pid_t pid, int display);
int raptor_sm_get_display_for_username(char* username);
void raptor_sm_wait_for_pid_exit(char* username, pid_t pid);
void raptor_sm_session_terminated(char* username);
int raptor_sm_get_new_unique_display(int mindisplay, int maxdisplay);
bool raptor_sm_sesslimit_reached(char* username);
-char raptor_sm_set_session_state(int display, int state); \ No newline at end of file
+char raptor_sm_set_session_state(int display, int state);
+void raptor_sm_run_remote_desktop(char* username, int display, char* executable); \ No newline at end of file
diff --git a/sesman/chansrv/chansrv.c b/sesman/chansrv/chansrv.c
index 918bb832..7b4bf0b5 100644
--- a/sesman/chansrv/chansrv.c
+++ b/sesman/chansrv/chansrv.c
@@ -442,6 +442,7 @@ send_init_response_message(void)
out_uint32_le(s, 2); /* msg id */
out_uint32_le(s, 8); /* size */
s_mark_end(s);
+ LOG(1, ("send_init_response_message: calling trans_force_write"));
return trans_write_copy(g_con_trans);
}
@@ -775,6 +776,7 @@ process_message(void)
if (rv != 0)
{
+ LOG(0, ("process_message: error in process_message: rv %d", rv));
break;
}
@@ -1173,7 +1175,6 @@ channel_thread_loop(void *in_val)
g_con_trans = 0;
// Use the display number to mark session disconnected in the Raptor session management database
raptor_sm_set_session_state(g_display_num, SM_STATUS_RUNNING);
- exit(0); // RAPTOR session management
/* create new listener */
error = setup_listen();
@@ -1409,6 +1410,7 @@ read_ini(void)
name = (char *)list_get_item(names, index);
value = (char *)list_get_item(values, index);
+#ifndef DISABLE_UNIX_DOMAIN_SOCKETS
if (g_strcasecmp(name, "ListenAddress") == 0)
{
if (g_strcasecmp(value, "127.0.0.1") == 0)
@@ -1416,6 +1418,7 @@ read_ini(void)
g_use_unix_socket = 1;
}
}
+#endif
}
}
diff --git a/sesman/scp_v0.c b/sesman/scp_v0.c
index efa9080c..5eb13e05 100644
--- a/sesman/scp_v0.c
+++ b/sesman/scp_v0.c
@@ -26,6 +26,8 @@
#include "sesman.h"
+#include "libraptorsmiface.h"
+
extern struct config_sesman *g_cfg; /* in sesman.c */
/******************************************************************************/
@@ -76,6 +78,17 @@ scp_v0_process(struct SCP_CONNECTION *c, struct SCP_SESSION *s)
s_item = session_get_bydata(s->username, s->width, s->height,
s->bpp, s->type, s->client_ip);
+ // RAPTOR session management
+ pid_t serverpid = raptor_sm_get_pid_for_username(s->username);
+ if (serverpid < 0) {
+ // Session NOT already running
+ if (s_item != 0) {
+ log_message(&(g_cfg->log), LOG_LEVEL_INFO, "++ [FIXME] scp claimed there was an active session, but the authoritative RAPTOR database disagrees: username %s", s->username);
+ }
+ s_item = 0;
+ }
+
+
if (s_item != 0)
{
display = s_item->display;
diff --git a/sesman/scp_v1.c b/sesman/scp_v1.c
index 92e1dad5..008c7c9d 100644
--- a/sesman/scp_v1.c
+++ b/sesman/scp_v1.c
@@ -29,6 +29,8 @@
//#include "libscp_types.h"
#include "libscp.h"
+#include "libraptorsmiface.h"
+
extern struct config_sesman *g_cfg; /* in sesman.c */
static void parseCommonStates(enum SCP_SERVER_STATES_E e, char *f);
@@ -107,6 +109,13 @@ scp_v1_process(struct SCP_CONNECTION *c, struct SCP_SESSION *s)
/* list disconnected sessions */
slist = session_get_byuser(s->username, &scount, SESMAN_SESSION_STATUS_DISCONNECTED);
+ // RAPTOR session management
+ pid_t serverpid = raptor_sm_get_pid_for_username(s->username);
+ if (serverpid < 0) {
+ // Session NOT already running
+ scount = 0;
+ }
+
if (scount == 0)
{
/* no disconnected sessions - start a new one */
diff --git a/sesman/session.c b/sesman/session.c
index 250c8baf..1e6a22ce 100644
--- a/sesman/session.c
+++ b/sesman/session.c
@@ -35,6 +35,15 @@
#include <errno.h>
//#include <time.h>
+#include <arpa/inet.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <netdb.h>
+
#include "libraptorsmiface.h"
extern unsigned char g_fixedkey[8];
@@ -146,6 +155,82 @@ session_get_bydata(char *name, int width, int height, int bpp, int type, char *c
/******************************************************************************/
/**
*
+ * @brief checks if there's a server running on a host and port
+ * @param display the display to check
+ * @return 0 if the port is closed, 1 if it is open
+ *
+ */
+static int DEFAULT_CC
+check_port_status(const char* host, const char* port)
+{
+ char text[256];
+ int x_running;
+ int sck;
+
+ struct sockaddr_in servaddr;
+ int soc = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+
+ g_memset( &servaddr, 0, sizeof(servaddr));
+ servaddr.sin_family = AF_INET;
+ servaddr.sin_port = htons(atoi(port));
+
+ struct hostent* hostaddr;
+ hostaddr = gethostbyname(host);
+ g_memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length);
+
+ int res = connect(soc, (struct sockaddr*)&servaddr, sizeof(servaddr));
+
+ close(soc);
+
+ if (res == -1)
+ {
+ // Port is closed, no server there!
+ return 0;
+ }
+ else {
+ // Port is open
+ return 1;
+ }
+}
+
+/******************************************************************************/
+/**
+ *
+ * @brief checks if there's a server running on a remote display
+ * @param display the display to check
+ * @return 0 if there isn't a display running, nonzero otherwise
+ *
+ */
+static int DEFAULT_CC
+x_server_running_check_remote_ports(const char* host, int display)
+{
+ char text[256];
+ int x_running;
+ int sck;
+
+ x_running = 0;
+ /* check 59xx */
+ {
+ g_sprintf(text, "59%2.2d", display);
+ x_running += check_port_status(host, text);
+ }
+ /* check 60xx */
+ {
+ g_sprintf(text, "60%2.2d", display);
+ x_running += check_port_status(host, text);
+ }
+ /* check 62xx */
+ {
+ g_sprintf(text, "62%2.2d", display);
+ x_running += check_port_status(host, text);
+ }
+
+ return x_running;
+}
+
+/******************************************************************************/
+/**
+ *
* @brief checks if there's a server running on a display
* @param display the display to check
* @return 0 if there isn't a display running, nonzero otherwise
@@ -383,14 +468,14 @@ wait_for_xserver(int display)
int i;
/* give X a bit to start */
- /* wait up to 10 secs for x server to start */
+ /* wait up to 15 secs for x server to start */
i = 0;
- while (!x_server_running(display))
+ while (!x_server_running_check_ports(display))
{
i++;
- if (i > 40)
+ if (i > 60)
{
log_message(LOG_LEVEL_ERROR,
"X server for display %d startup timeout",
@@ -405,6 +490,57 @@ wait_for_xserver(int display)
}
/******************************************************************************/
+static int APP_CC
+wait_for_remote_xserver(const char* host, int display)
+{
+ int i;
+
+ /* give X a bit to start */
+ /* wait up to 15 secs for x server to start */
+ i = 0;
+ //while (!x_server_running(display))
+ while (!x_server_running_check_remote_ports(host, display))
+ {
+ i++;
+ if (i > 60)
+ {
+ log_message(&(g_cfg->log), LOG_LEVEL_ERROR,
+ "X server for host %s and display %d startup timeout",
+ host, display);
+ break;
+ }
+ g_sleep(250);
+ }
+ return 0;
+}
+
+/******************************************************************************/
+static const char * APP_CC
+wait_for_remote_hostname(char* username)
+{
+ int i;
+
+ /* wait up to 5 secs for hostname to appear */
+ i = 0;
+ const char * hostname = raptor_sm_get_hostname_for_username(username, false);
+ while (strcmp(hostname, "") == 0)
+ {
+ g_free(hostname);
+ hostname = raptor_sm_get_hostname_for_username(username, false);
+ i++;
+ if (i > 20)
+ {
+ log_message(&(g_cfg->log), LOG_LEVEL_ERROR,
+ "Hostname allocation timeout");
+ break;
+ }
+ g_sleep(250);
+ }
+
+ return hostname;
+}
+
+/******************************************************************************/
/* called with the main thread */
static int APP_CC
session_start_fork(int width, int height, int bpp, char *username,
@@ -485,6 +621,7 @@ session_start_fork(int width, int height, int bpp, char *username,
if (display == 0)
{
+ log_message(&(g_cfg->log), LOG_LEVEL_ALWAYS, "Unable to allocate display for user %s", username);
g_free(temp->item);
g_free(temp);
return 0;
@@ -547,7 +684,6 @@ session_start_fork(int width, int height, int bpp, char *username,
}
else if (wmpid == 0)
{
- wait_for_xserver(display);
auth_start_session(data, display);
pampid = g_fork(); /* parent waits, todo
child becomes wm */
@@ -556,17 +692,24 @@ session_start_fork(int width, int height, int bpp, char *username,
}
else if (pampid == 0)
{
- env_set_user(username, 0, display,
- g_cfg->session_variables1,
- g_cfg->session_variables2);
if (session_was_already_running) {
g_exit(0);
}
+ char* remote_server = wait_for_remote_hostname(username);
+ wait_for_remote_xserver(remote_server, display);
env_set_user(username, 0, display,
g_cfg->session_variables1,
g_cfg->session_variables2);
- if (x_server_running(display))
+
+ //if (x_server_running(display))
+ if (x_server_running_check_remote_ports(remote_server, display))
{
+ g_free(remote_server);
+
+ // RAPTOR session management
+ raptor_sm_run_remote_desktop(username, display, "/opt/trinity/bin/starttde");
+ g_exit(0);
+
auth_set_env(data);
if (directory != 0)
{
@@ -633,6 +776,7 @@ session_start_fork(int width, int height, int bpp, char *username,
}
else
{
+ g_free(remote_server);
log_message(LOG_LEVEL_ERROR, "another Xserver might "
"already be active on display %d - see log", display);
}
@@ -754,7 +898,7 @@ session_start_fork(int width, int height, int bpp, char *username,
list_add_item(xserver_params, (tintptr)g_strdup("-depth"));
list_add_item(xserver_params, (tintptr)g_strdup(depth));
list_add_item(xserver_params, (tintptr)g_strdup("-reset"));
- list_add_item(xserver_params, (tintptr)g_strdup("-terminate"));
+// list_add_item(xserver_params, (tintptr)g_strdup("-terminate"));
/* additional parameters from sesman.ini file */
//config_read_xserver_params(SESMAN_SESSION_TYPE_XRDP,
@@ -768,6 +912,7 @@ session_start_fork(int width, int height, int bpp, char *username,
pid_t serverpid;
serverpid = raptor_sm_run_remote_server(username, pp1);
+ log_message(&(g_cfg->log), LOG_LEVEL_ALWAYS, "new server pid code was %d during login for user %s", serverpid, username);
if (serverpid >= 0) {
if (!session_was_already_running) {
@@ -818,7 +963,10 @@ session_start_fork(int width, int height, int bpp, char *username,
}
else
{
- wait_for_xserver(display);
+ //wait_for_xserver(display);
+ char* remote_server = wait_for_remote_hostname(username);
+ wait_for_remote_xserver(remote_server, display);
+ free(remote_server);
g_snprintf(text, 255, "%d", display);
g_setenv("XRDP_SESSVC_DISPLAY", text, 1);
g_snprintf(text, 255, ":%d.0", display);
diff --git a/xrdp/xrdp_mm.c b/xrdp/xrdp_mm.c
index 37b51ca3..f19eaada 100644
--- a/xrdp/xrdp_mm.c
+++ b/xrdp/xrdp_mm.c
@@ -489,6 +489,7 @@ static int APP_CC
xrdp_mm_setup_mod2(struct xrdp_mm *self)
{
char text[256];
+ char raptortext[256];
char *name;
char *value;
int i;
@@ -523,12 +524,12 @@ xrdp_mm_setup_mod2(struct xrdp_mm *self)
char* rsmip = raptor_sm_get_ip_for_username(self->login_username, true);
int allocdisplay = raptor_sm_get_display_for_username(self->login_username);
if ((raptor_sm_sesslimit_reached(self->login_username)) && (allocdisplay < 0)) {
- g_snprintf(text, 255, "[LICENSE] Maximum concurrent session");
- xrdp_wm_log_msg(self->wm, text);
- g_snprintf(text, 255, "[LICENSE] limit exceeded for group.");
- xrdp_wm_log_msg(self->wm, text);
- g_snprintf(text, 255, "[LICENSE] Login for user %s denied.", self->login_username);
- xrdp_wm_log_msg(self->wm, text);
+ g_snprintf(raptortext, 255, "[LICENSE] Maximum concurrent session");
+ xrdp_wm_log_msg(self->wm, raptortext);
+ g_snprintf(raptortext, 255, "[LICENSE] limit exceeded for group.");
+ xrdp_wm_log_msg(self->wm, raptortext);
+ g_snprintf(raptortext, 255, "[LICENSE] Login for user %s denied.", self->login_username);
+ xrdp_wm_log_msg(self->wm, raptortext);
raptor_sm_session_terminated(self->login_username);
return 1;
}
@@ -537,10 +538,14 @@ xrdp_mm_setup_mod2(struct xrdp_mm *self)
self->display = allocdisplay;
}
self->mod->mod_set_param(self->mod, "ip", rsmip);
+#ifdef DISABLE_UNIX_DOMAIN_SOCKETS
+ use_uds = 0;
+#else
use_uds = 1;
if (g_strcmp(rsmip, "127.0.0.1") != 0) {
use_uds = 0;
}
+#endif
}
g_free(rsmip);
@@ -1138,7 +1143,11 @@ xrdp_mm_connect_chansrv(struct xrdp_mm *self, char *ip, char *port)
self->usechansrv = 1;
/* connect channel redir */
+#ifdef DISABLE_UNIX_DOMAIN_SOCKETS
+ if (0)
+#else
if ((g_strcmp(ip, "127.0.0.1") == 0) || (ip[0] == 0))
+#endif
{
/* unix socket */
self->chan_trans = trans_create(TRANS_MODE_UNIX, 8192, 8192);
@@ -1235,7 +1244,11 @@ xrdp_mm_process_login_response(struct xrdp_mm *self, struct stream *s)
self->wm->dragging = 0;
/* connect channel redir */
+#ifdef DISABLE_UNIX_DOMAIN_SOCKETS
+ if (0)
+#else
if ((g_strcmp(ip, "127.0.0.1") == 0) || (ip[0] == 0))
+#endif
{
g_snprintf(port, 255, XRDP_CHANSRV_STR, display);
}
diff --git a/xup/xup.c b/xup/xup.c
index b4a5df1e..68b93ba2 100644
--- a/xup/xup.c
+++ b/xup/xup.c
@@ -160,6 +160,15 @@ lib_mod_connect(struct mod *mod)
return 1;
}
+ char text[256];
+ g_snprintf(text, 255, "services starting on %s, please wait...\n\r", mod->ip);
+ mod->server_msg(mod, text, 0);
+
+ // FIXME CRITICAL
+ // Prevent an immediate RDP exit
+ // This delay needs to be long enough for everything to start up 100%
+ g_sleep(5000);
+
if (g_strcmp(mod->ip, "") == 0)
{
mod->server_msg(mod, "error - no ip set", 0);