Add server/group mapping

ulab-next-nosound
Timothy Pearson 13 years ago committed by Timothy Pearson
parent 46a8e6373e
commit d385c08f3a

@ -125,13 +125,15 @@ char* raptor_sm_allocate_session(char* username) {
MYSQL_ROW row; MYSQL_ROW row;
MYSQL_RES *svr_res; MYSQL_RES *svr_res;
MYSQL_ROW svr_row; MYSQL_ROW svr_row;
MYSQL_RES *per_res;
MYSQL_ROW per_row;
MYSQL_RES *cnt_res; MYSQL_RES *cnt_res;
MYSQL_ROW cnt_row; MYSQL_ROW cnt_row;
char* query; char* query;
MYSQL *conn = connect_if_needed(); MYSQL *conn = connect_if_needed();
if (!conn) { if (!conn) {
return strdup("SQLERR001"); return strdup("ERROR");
} }
// Verify that this user is not already on the system // Verify that this user is not already on the system
@ -142,7 +144,7 @@ char* raptor_sm_allocate_session(char* username) {
// Server error // Server error
free(query); free(query);
mysql_close(conn); mysql_close(conn);
return strdup("SQLERR002"); return strdup("ERROR");
} }
else { else {
free(query); free(query);
@ -154,13 +156,39 @@ char* raptor_sm_allocate_session(char* username) {
// Server error // Server error
mysql_free_result(res); mysql_free_result(res);
mysql_close(conn); mysql_close(conn);
return strdup("SQLERR003"); return strdup("ERROR");
} }
else { else {
svr_res = mysql_store_result(conn); svr_res = mysql_store_result(conn);
// Get group for user
char* groupname = get_group_for_user(username);
char* safe_groupname = get_mysql_escaped_string(conn, groupname);
free(groupname);
// Get the list of allowed nodes for this group
asprintf(&query, "SELECT server FROM allowed_servers WHERE groupname='%s'", safe_groupname);
free(safe_groupname);
if (mysql_query_internal(conn, query)) {
// Server error
mysql_free_result(res);
mysql_free_result(svr_res);
mysql_close(conn);
return strdup("ERROR");
}
else {
per_res = mysql_store_result(conn);
char* bestserver = strdup(""); char* bestserver = strdup("");
int bestusage = INT_MAX; int bestusage = INT_MAX;
while ((svr_row = mysql_fetch_row(svr_res)) != NULL) { while ((svr_row = mysql_fetch_row(svr_res)) != NULL) {
// Am I allowed to use this server?
bool can_use_server = false;
while ((per_row = mysql_fetch_row(per_res)) != NULL) {
if (strcmp(per_row[0], svr_row[0]) == 0) {
can_use_server = true;
}
}
mysql_data_seek(per_res, 0);
if (can_use_server) {
char* safe_servername = get_mysql_escaped_string(conn, svr_row[0]); char* safe_servername = get_mysql_escaped_string(conn, svr_row[0]);
asprintf(&query, "SELECT username FROM sessions WHERE servername='%s'", safe_servername); asprintf(&query, "SELECT username FROM sessions WHERE servername='%s'", safe_servername);
free(safe_servername); free(safe_servername);
@ -171,7 +199,7 @@ char* raptor_sm_allocate_session(char* username) {
mysql_free_result(res); mysql_free_result(res);
mysql_free_result(svr_res); mysql_free_result(svr_res);
mysql_close(conn); mysql_close(conn);
return strdup("SQLERR004"); return strdup("ERROR");
} }
else { else {
free(query); free(query);
@ -188,9 +216,12 @@ char* raptor_sm_allocate_session(char* username) {
} }
} }
} }
}
mysql_free_result(res); mysql_free_result(res);
mysql_free_result(svr_res); mysql_free_result(svr_res);
mysql_free_result(per_res);
if (strcmp(bestserver, "") != 0) {
// Insert new information into the sessions database and set status to ALLOCATED // Insert new information into the sessions database and set status to ALLOCATED
char* safe_servername = get_mysql_escaped_string(conn, bestserver); char* safe_servername = get_mysql_escaped_string(conn, bestserver);
char* safe_username = get_mysql_escaped_string(conn, username); char* safe_username = get_mysql_escaped_string(conn, username);
@ -201,7 +232,7 @@ char* raptor_sm_allocate_session(char* username) {
// Server error // Server error
free(query); free(query);
mysql_close(conn); mysql_close(conn);
return strdup("SQLERR005"); return strdup("ERROR");
} }
else { else {
free(query); free(query);
@ -209,6 +240,13 @@ char* raptor_sm_allocate_session(char* username) {
return strdup(bestserver); return strdup(bestserver);
} }
} }
else {
// No usable server found!
mysql_close(conn);
return strdup("ERROR");
}
}
}
} }
else { else {
char* ret = strdup(row[0]); char* ret = strdup(row[0]);
@ -287,10 +325,11 @@ char* raptor_sm_get_ip_for_username(char* username, bool create) {
char* hostname = raptor_sm_get_hostname_for_username(username, create); char* hostname = raptor_sm_get_hostname_for_username(username, create);
char err; char err;
char* ip = raptor_sm_get_ip_for_hostname(hostname, &err); char* ip = raptor_sm_get_ip_for_hostname(hostname, &err);
free(hostname);
if (err) { if (err) {
raptor_sm_deallocate_session(username); raptor_sm_deallocate_session(username);
return strdup("ERROR");
} }
free(hostname);
return ip; return ip;
} }

@ -522,6 +522,15 @@ xrdp_mm_setup_mod2(struct xrdp_mm *self)
else if (self->code == 10 || self->code == 20) /* X11rdp/Xorg */ else if (self->code == 10 || self->code == 20) /* X11rdp/Xorg */
{ {
char* rsmip = raptor_sm_get_ip_for_username(self->login_username, true); char* rsmip = raptor_sm_get_ip_for_username(self->login_username, true);
if (strcmp(rsmip, "ERROR") == 0) {
g_snprintf(raptortext, 255, "[LICENSE] Instantaneous limit exceeded.");
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);
g_free(rsmip);
raptor_sm_session_terminated(self->login_username);
return 1;
}
int allocdisplay = raptor_sm_get_display_for_username(self->login_username); int allocdisplay = raptor_sm_get_display_for_username(self->login_username);
if ((raptor_sm_sesslimit_reached(self->login_username)) && (allocdisplay < 0)) { if ((raptor_sm_sesslimit_reached(self->login_username)) && (allocdisplay < 0)) {
g_snprintf(raptortext, 255, "[LICENSE] Maximum concurrent session"); g_snprintf(raptortext, 255, "[LICENSE] Maximum concurrent session");
@ -530,6 +539,7 @@ xrdp_mm_setup_mod2(struct xrdp_mm *self)
xrdp_wm_log_msg(self->wm, raptortext); xrdp_wm_log_msg(self->wm, raptortext);
g_snprintf(raptortext, 255, "[LICENSE] Login for user %s denied.", self->login_username); g_snprintf(raptortext, 255, "[LICENSE] Login for user %s denied.", self->login_username);
xrdp_wm_log_msg(self->wm, raptortext); xrdp_wm_log_msg(self->wm, raptortext);
g_free(rsmip);
raptor_sm_session_terminated(self->login_username); raptor_sm_session_terminated(self->login_username);
return 1; return 1;
} }

Loading…
Cancel
Save