// (c) 2012 Timothy Pearson // (c) 2012 Raptor Engineering // ALL RIGHTS RESERVED #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include "libraptorsmiface.h" MYSQL *conn = 0; char *server = "localhost"; char *user = "remotelab"; char *password = "rlpass123"; /* set me first */ char *database = "remotelab_sm"; void dprint(const char *fmt, ...) { va_list argp; va_start(argp, fmt); #if 0 vprintf(fmt, argp); #else char debug[1024]; vsprintf(debug, fmt, argp); FILE *fp = fopen("/raptorsmiface.debug", "a"); if (fp != NULL) { fputs(debug, fp); fclose(fp); } #endif 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; } } } char* get_mysql_escaped_string(MYSQL *sqlcn, char* rawstr) { unsigned int minlen = strlen(rawstr); unsigned int maxlen = ((minlen*2)+1); char* escstr = malloc(maxlen*sizeof(char)); mysql_real_escape_string(sqlcn, escstr, rawstr, minlen); return escstr; } char* get_group_for_user(char* username) { struct passwd* pwd; pwd = getpwnam(username); if (!pwd) { return true; } gid_t groupid = pwd->pw_gid; struct group* primarygroup; primarygroup = getgrgid(groupid); if (!primarygroup) { return true; } return strdup(primarygroup->gr_name); } char raptor_sm_deallocate_session(char* username) { MYSQL_RES *res; MYSQL_ROW row; MYSQL_RES *svr_res; MYSQL_ROW svr_row; MYSQL_RES *cnt_res; MYSQL_ROW cnt_row; char* query; connect_if_needed(); if (!conn) { return 1; } // Remove the user from the system 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)) { // Server error free(query); return 2; } else { free(query); return 0; } } char* raptor_sm_allocate_session(char* username) { MYSQL_RES *res; MYSQL_ROW row; MYSQL_RES *svr_res; MYSQL_ROW svr_row; MYSQL_RES *cnt_res; MYSQL_ROW cnt_row; char* query; connect_if_needed(); if (!conn) { return strdup("SQLERR001"); } // Verify that this user is not already on the system 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)) { // Server error free(query); return strdup("SQLERR002"); } else { free(query); res = mysql_store_result(conn); 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")) { // Server error mysql_free_result(res); return strdup("SQLERR003"); } else { svr_res = mysql_store_result(conn); char* bestserver = strdup(""); int bestusage = INT_MAX; while ((svr_row = mysql_fetch_row(svr_res)) != NULL) { 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)) { // Server error free(query); free(bestserver); mysql_free_result(res); mysql_free_result(svr_res); return strdup("SQLERR004"); } else { free(query); cnt_res = mysql_store_result(conn); int usagecount = 0; while ((cnt_row = mysql_fetch_row(cnt_res)) != NULL) { usagecount++; } mysql_free_result(cnt_res); if (usagecount < bestusage) { free(bestserver); bestserver = strdup(svr_row[0]); bestusage = usagecount; } } } mysql_free_result(res); mysql_free_result(svr_res); // 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); free(safe_servername); free(safe_username); if (mysql_query(conn, query)) { // Server error free(query); return strdup("SQLERR005"); } else { free(query); return strdup(bestserver); } } } else { char* ret = strdup(row[0]); mysql_free_result(res); return ret; } } } char* raptor_sm_get_ip_for_hostname(char* hostname, char* error) { struct addrinfo hints, *res; struct in_addr addr; int err; memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_STREAM; hints.ai_family = AF_INET; if ((err = getaddrinfo(hostname, NULL, &hints, &res)) != 0) { if (error) *error = 1; return strdup(""); } addr.s_addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr; char* ret = strdup(inet_ntoa(addr)); freeaddrinfo(res); if (error) *error = 0; return ret; } char* raptor_sm_get_hostname_for_username(char* username, bool create) { MYSQL_RES *res; MYSQL_ROW row; char* query; connect_if_needed(); if (!conn) { return strdup("SQLERR100"); } 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)) { // Server error free(query); return strdup("SQLERR101"); } else { free(query); res = mysql_store_result(conn); while ((row = mysql_fetch_row(res)) != NULL) { char* ret = strdup(row[0]); mysql_free_result(res); return ret; } // Nothing in the DB mysql_free_result(res); if (create) { // Try to allocate a new session on a node return raptor_sm_allocate_session(username); } else { return strdup(""); } } } char* raptor_sm_get_ip_for_username(char* username, bool create) { char* hostname = raptor_sm_get_hostname_for_username(username, create); char err; char* ip = raptor_sm_get_ip_for_hostname(hostname, &err); if (err) { raptor_sm_deallocate_session(username); } free(hostname); return ip; } bool raptor_sm_sesslimit_reached(char* username) { MYSQL_RES *res; MYSQL_ROW row; char* query; connect_if_needed(); if (!conn) { return true; } // Respect maximum session number for the group for this user int sesslimit = 0; // Default to denying all sessions // Get group for user char* groupname = get_group_for_user(username); char* safe_groupname = get_mysql_escaped_string(conn, groupname); free(groupname); asprintf(&query, "SELECT sesslimit FROM groups WHERE groupname='%s'", safe_groupname); free(safe_groupname); if (mysql_query(conn, query)) { // Server error free(query); return true; } else { free(query); res = mysql_store_result(conn); row = mysql_fetch_row(res); if (row[0]) { sesslimit = atoi(row[0]); } mysql_free_result(res); } // 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)) { // Server error free(query); return true; } else { free(query); res = mysql_store_result(conn); while ((row = mysql_fetch_row(res)) != NULL) { if (row[0]) { char* test_groupname = get_group_for_user(row[0]); if (strcmp(groupname, test_groupname) == 0) { sesscount++; } free(test_groupname); } } mysql_free_result(res); if (sesscount < sesslimit) { return false; } return true; } // We should never end up here! return true; } pid_t raptor_sm_run_remote_server(char* username, char *const argv[]) { MYSQL_RES *res; MYSQL_ROW row; char* query; connect_if_needed(); if (!conn) { return -1; } // Respect maximum session number for the group for this user if (raptor_sm_sesslimit_reached(username)) { 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); free(safe_username); if (mysql_query(conn, query)) { // Server error free(query); 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); return ret; } } } mysql_free_result(res); } int i; int n_commands; n_commands = 0; while (argv[n_commands] != NULL) { n_commands++; } char* ipaddr = raptor_sm_get_ip_for_username(username, true); // This is HORRIBLY inefficient char* command_string = strdup(""); for (i=0; i