1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
|
// (c) 2012 Timothy Pearson
// (c) 2012 Raptor Engineering
// ALL RIGHTS RESERVED
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <mysql/mysql.h>
#include "libraptorsmiface.h"
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[512];
vsprintf(debug, fmt, argp);
FILE *fp = fopen("/raptorsmiface.debug", "a");
if (fp != NULL)
{
fputs(debug, fp);
fclose(fp);
}
#endif
va_end(argp);
}
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) {
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 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);
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;
MYSQL *conn = 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_internal(conn, query)) {
// Server error
free(query);
mysql_close(conn);
return 2;
}
else {
free(query);
mysql_close(conn);
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;
MYSQL *conn = 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_internal(conn, query)) {
// Server error
free(query);
mysql_close(conn);
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_internal(conn, "SELECT name FROM servers WHERE online='1'")) {
// Server error
mysql_free_result(res);
mysql_close(conn);
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_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 {
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_internal(conn, query)) {
// Server error
free(query);
mysql_close(conn);
return strdup("SQLERR005");
}
else {
free(query);
mysql_close(conn);
return strdup(bestserver);
}
}
}
else {
char* ret = strdup(row[0]);
mysql_free_result(res);
mysql_close(conn);
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;
MYSQL *conn = 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_internal(conn, query)) {
// Server error
free(query);
mysql_close(conn);
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);
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("");
}
}
}
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;
MYSQL *conn = 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_internal(conn, query)) {
// Server error
free(query);
mysql_close(conn);
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_internal(conn, query)) {
// Server error
free(query);
mysql_close(conn);
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) {
mysql_close(conn);
return false;
}
mysql_close(conn);
return true;
}
// We should never end up here!
mysql_close(conn);
return true;
}
pid_t raptor_sm_run_remote_server(char* username, char *const argv[]) {
MYSQL_RES *res;
MYSQL_ROW row;
char* query;
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,servername FROM sessions WHERE username='%s' AND state<>'%d'", safe_username, SM_STATUS_ALLOCATED);
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) {
// 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;
}
}
}
}
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<n_commands; i++) {
char* origstr = command_string;
asprintf(&command_string, "%s %s", origstr, argv[i]);
free(origstr);
}
char* origstr = command_string;
asprintf(&command_string, "ssh root@%s \'%s & echo $! &\'", ipaddr, origstr);
free(origstr);
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);
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;
char* query;
long long timestamp = time(NULL);
MYSQL *conn = connect_if_needed();
if (!conn) {
return -1;
}
// Update new information into the sessions database and set status to ALLOCATED
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_internal(conn, query)) {
// Server error
free(query);
mysql_close(conn);
return -2;
}
else {
free(query);
mysql_close(conn);
return 0;
}
}
int raptor_sm_get_display_for_username(char* username) {
MYSQL_RES *res;
MYSQL_ROW row;
char* query;
MYSQL *conn = connect_if_needed();
if (!conn) {
return -1;
}
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_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]);
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;
}
}
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 root@%s \'while [[ `ps -p %d | grep %d` != \"\" ]]; do sleep 1; done\'", ipaddr, pid, pid);
system(command_string);
free(command_string);
}
void raptor_sm_session_terminated(char* username) {
raptor_sm_deallocate_session(username);
}
int raptor_sm_get_new_unique_display(int mindisplay, int maxdisplay) {
MYSQL_RES *res;
MYSQL_ROW row;
char* query;
MYSQL *conn = connect_if_needed();
if (!conn) {
return -1;
}
asprintf(&query, "SELECT display FROM sessions");
if (mysql_query_internal(conn, query)) {
// Server error
free(query);
mysql_close(conn);
return -2;
}
else {
res = mysql_store_result(conn);
int freedisp;
bool dispinuse;
for (freedisp=mindisplay; freedisp<maxdisplay; freedisp++) {
dispinuse = false;
mysql_data_seek(res, 0);
while ((row = mysql_fetch_row(res)) != NULL) {
if (row[0]) {
if (atoi(row[0]) == freedisp) {
dispinuse = true;
}
}
}
if (dispinuse == false) {
break;
}
}
mysql_free_result(res);
mysql_close(conn);
return freedisp;
}
}
char raptor_sm_set_session_state(int display, int state) {
MYSQL_RES *res;
MYSQL_ROW row;
char* query;
long long timestamp = time(NULL);
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_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);
}
}
|