-
Notifications
You must be signed in to change notification settings - Fork 16
/
vm.cpp
986 lines (885 loc) · 17.8 KB
/
vm.cpp
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
#include "engine.h"
VM::VM (string hn)
{
hostname = hn;
uname = "VNX Kernel 0.1: Tue Dec 21 14:11 EST 2010; x86";
users.push_back(User("root",""));
ip = "0.0.0.0";
cpu = "Octium II";
cwd = "/";
broken_counter = 0;
has_root = false;
fs_maxSize = 64;
compute_power = 1;
}
/*
string VM::serialize()
{
ostringstream num;
string data;
data += "vm:" + getHostname() + "\n";
// save filesystem
vector<File>::iterator f;
for(f=filesystem.begin(); f!=filesystem.end(); f++)
{
string type;
if(f->getType() == T_FOLDER) type = "d";
else type = "f";
data += "file:" + type + ",parent=" + f->getParent() + ",name=" + f->getName() + ",acl=" + f->getAcl() + ",owner=" + f->getOwner() + ",exec=" + f->getExec();
if (!f->getOnDelete().empty()) data += ",on_delete=" + f->getOnDelete();
data += ",content=" + ab64_encode(f->getContent().c_str(), f->getContent().length()) + "\n";
}
// save user chain
{
data.append("chain:");
vector<User*>::iterator it;
for(it=user_chain.begin(); it!=user_chain.end(); it++)
{
if(it!=user_chain.begin()) data.append(",");
data.append((*it)->getName());
}
data.append("\n");
}
// save server properties
data += "p:cwd=" + getCwd() + "\n";
num << broken_counter;
data += "p:bc=" + num.str() + "\n";
if(has_root) data += "p:has_root=true\n";
else data += "p:has_root=false\n";
// save net domains
if(net_domains.size())
{
data += "netd:";
vector<int>::iterator ii;
for(ii=net_domains.begin(); ii!=net_domains.end(); ii++)
{
if(ii!=net_domains.begin()) data += ",";
num.str(string());
num << *ii;
data += num.str();
}
data += "\n";
}
return ab64_encode(data.c_str(), data.length());
}
*/
//string VM::serialize()
pt::ptree VM::serialize()
{
ostringstream num;
ostringstream output;
pt::ptree json;
string data;
json.put("hostname", getHostname());
// save filesystem
vector<File>::iterator f;
for(f=filesystem.begin(); f!=filesystem.end(); f++)
{
pt::ptree file;
string type;
if(f->getType() == T_FOLDER) type = "d";
else type = "f";
data += "type:" + type + ",parent=" + f->getParent() + ",name=" + f->getName() + ",acl=" + f->getAcl() + ",owner=" + f->getOwner() + ",exec=" + f->getExec();
file.put("type", type);
file.put("parent", f->getParent());
file.put("name", f->getName());
file.put("acl", f->getAcl());
file.put("owner", f->getOwner());
file.put("exec", f->getExec());
if (!f->getOnDelete().empty())
{
file.put("on_delete", f->getOnDelete());
}
file.put("content", f->getContent());
json.push_back(std::make_pair("file", file));
}
// save user chain
json.put("chain", printUserChain());
// save server properties
json.put("cwd", getCwd());
num << broken_counter;
json.put("bc", num.str());
if(has_root)
{
json.put("has_root", true);
} else {
json.put("has_root", false);
}
// save net domains
if(net_domains.size())
{
data = "";
vector<int>::iterator ii;
for(ii=net_domains.begin(); ii!=net_domains.end(); ii++)
{
if(ii!=net_domains.begin()) data += ",";
num.str(string());
num << *ii;
data += num.str();
}
json.put("netd", data);
}
//return ab64_encode(data.c_str(), data.length());
pt::write_json(output, json, false);
//return output.str();
return json;
}
string VM::getCpu()
{
return cpu;
}
string VM::getCwd()
{
return cwd;
}
const vector<string> VM::getFiles(string parent)
{
vector<string> files;
for(int i=0; i < filesystem.size(); i++)
{
if(!files.size() && (filesystem[i].getType() == T_FILE) && (filesystem[i].getParent() + filesystem[i].getName() == parent))
{
files.push_back(filesystem[i].getName());
return files;
}
if(filesystem[i].getParent() == parent)
{
files.push_back(filesystem[i].getName());
}
}
return files;
}
File* VM::pFile (string s)
{
if ( s.empty() ) return NULL;
for(int i=0; i < filesystem.size(); i++)
{
if(filesystem[i].getParent()+filesystem[i].getName() == s ||
filesystem[i].getParent()+filesystem[i].getName() == s+"/")
{
return &(filesystem[i]);
}
}
return NULL;
}
string VM::getHostname ()
{
return hostname;
}
string VM::getHint()
{
return hint;
}
string VM::getIp ()
{
return ip;
}
string VM::getUname ()
{
return uname;
}
string VM::getUsername ()
{
return user_chain.back()->getName();
}
User* VM::getUser (string u)
{
for (int i=0; i<users.size(); i++ )
{
if (users[i].getName() == u) return &(users[i]);
}
return NULL;
}
int VM::getUserCount()
{
return user_chain.size();
}
int VM::setCwd (string s)
{
File *fp;
// are we allowed to see these files?
vector<string> store;
explode(s, "/", store);
string path = "/";
for (int i=0; i<store.size(); i++)
{
if (store[i].empty()) continue;
path += store[i] + "/";
fp = pFile(path);
if (!fp) {
return E_NO_FILE;
}
if (!fp->isReadable())
{
return E_DENIED;
}
}
for (int i=0; i<filesystem.size(); i++)
{
if ((filesystem[i].getType() == T_FOLDER) && (filesystem[i].getParent() + filesystem[i].getName() == s))
{
cwd = s;
return E_OK;
}
}
return E_NO_FILE;
}
void VM::setHint(string s)
{
hint = s;
}
void VM::setIp (string s)
{
ip = s;
}
void VM::setUname (string s)
{
uname = s;
}
bool VM::addFile (File f)
{
if (pcrecpp::RE("[^a-zA-Z0-9/.+_-]").PartialMatch(f.getName()))
{
return false;
}
if(filesystem.size() == fs_maxSize)
{
return false;
}
for (int i=0; i<filesystem.size(); i++)
{
if (filesystem[i].getParent() + filesystem[i].getName() == f.getParent() + f.getName())
{
filesystem[i] = f;
return true;
}
}
filesystem.push_back(f);
return true;
}
bool VM::deleteFile (string name)
{
string run_on_delete = "";
for (int i=0; i<filesystem.size(); i++)
{
if ((filesystem[i].getParent() + filesystem[i].getName() == name) && filesystem[i].getType() == T_FILE)
{
run_on_delete = filesystem[i].getOnDelete()+"()";
filesystem.erase(filesystem.begin()+i);
if (!run_on_delete.empty())
{
luaL_dostring(lua, run_on_delete.c_str());
}
return true;
}
}
return false;
}
bool VM::addUser (User u)
{
if (pcrecpp::RE("[^a-zA-Z0-9.]").PartialMatch(u.getName()))
{
return false;
}
if(users.size() == 64)
{
return false;
}
// if the user already exists, replace it (good for overriding root)
for (int i=0; i<users.size(); i++)
{
if (users[i].getName() == u.getName())
{
users[i] = u;
break;
}
// we can add our user now, but we need to make sure to preserve
// the chain
string str_chain = printUserChain();
user_chain.clear();
// now we can add the new user (this will reallocate memory)
users.push_back(u);
// finally, reset the pointers to the new memory locations
vector<string> store;
explode(str_chain, ",", store);
for(int i=0; i<store.size(); i++)
{
user_chain.push_back(getUser(store[i]));
}
}
return true;
}
bool VM::login ( string u, string p )
{
if (u.empty()) return false;
// loop through users
for (int i=0; i<users.size(); i++)
{
// check for username match
if (users[i].getName() == u)
{
// check for password match
if (users[i].isPassword(p))
{
// add user to chain
user_chain.push_back(&(users[i]));
// update broken counter
if (broken_counter) broken_counter++;
// did we hit root?
if (u == "root")
{
if (!has_root)
{
has_root = true;
if (!on_root.empty())
{
luaL_dostring(lua, (on_root+"()").c_str());
}
}
setCwd("/root/");
} else {
setCwd("/home/" + u + "/");
}
// let's run post login code
if (!on_login.empty())
{
// escape the password
size_t n = 0;
while ( (n=p.find("\"",n)) != std::string::npos )
{
p.insert(n,"\\");
n+=2;
}
// launch post login code and pass creds
luaL_dostring(lua, (on_login+"(\""+u+"\",\""+p+"\")").c_str());
}
return true;
} else {
return false;
}
}
}
return false;
}
bool VM::login ( string u )
{
if (u.empty()) return false;
// loop through users
for (int i=0; i<users.size(); i++)
{
// check for username match
if (users[i].getName() == u)
{
// add user to chain
user_chain.push_back(&(users[i]));
if (u == "root")
{
if (!has_root)
{
has_root = true;
if (!on_root.empty())
{
luaL_dostring(lua, (on_root+"()").c_str());
}
}
setCwd("/root/");
} else {
setCwd("/home/" + u + "/");
}
// update broken counter
if (broken_counter)
{
broken_counter++;
}
return true;
}
}
return false;
}
int VM::logout ()
{
int ret = user_chain.size();
// if users in chain, log out of the last user
if (user_chain.size())
{
string u = shell.back()->getUsername();
user_chain.pop_back();
if (broken_counter)
{
broken_counter--;
if (broken_counter == 0 ) return -1;
}
// let's run post logout code
if (!on_logout.empty())
{
luaL_dostring(lua, (on_logout+"(\""+u+"\")").c_str());
}
}
// return user chain size
return user_chain.size();
}
void VM::setBrokenCounter (int count)
{
broken_counter = count;
}
bool VM::startBrokenCounter ()
{
broken_counter = 1;
return true;
}
bool VM::startDaemon (string daemon)
{
for (int i=0; i<services.size(); i++)
{
// check is daemon exists
if (services[i].name == daemon)
{
// start daemon
services[i].started = true;
return true;
}
}
return false;
}
bool VM::stopDaemon (string daemon)
{
for (int i=0; i<services.size(); i++)
{
// check is daemon exists
if (services[i].name == daemon)
{
// stop daemon
services[i].started = false;
return true;
}
}
return false;
}
bool VM::addDaemon (Daemon d)
{
services.push_back(d);
return true;
}
vector<Daemon*> VM::getDaemons ()
{
vector<Daemon*> inetd;
for (int i=0; i<services.size(); i++)
{
if (services[i].started) inetd.push_back(&(services[i]));
}
return inetd;
}
void VM::setRoot (bool hr)
{
has_root = hr;
}
bool VM::hasRoot ()
{
return has_root;
}
void VM::onRoot (string s)
{
on_root = s;
}
void VM::onLogin (string s)
{
on_login = s;
}
void VM::onLogout (string s)
{
on_logout = s;
}
void VM::addGPU(string desc, int power)
{
gpus.push_back(desc);
compute_power += power;
}
vector<string> VM::getGPUs()
{
return gpus;
}
int VM::getCPower()
{
return compute_power;
}
vector<string> VM::tabComplete (string s)
{
vector<string> matches;
string filename;
string path;
string cwd = shell.back()->getCwd();
// check to see if a path was given
if (pcrecpp::RE("(.*/)").PartialMatch(s, &path))
{
s = s.substr(path.length());
path = realpath(path);
if(path.empty()) return matches;
} else {
// if no path, assign cwd
path = cwd;
}
// sanitize tab input
string in = s;
s = "";
for (int i=0; i<in.length(); i++)
{
s += "[";
s += in[i];
s += "]";
}
// loop through files
for(int i=0; i < filesystem.size(); i++)
{
filename = filesystem[i].getName();
// make sure file is in the working path
if( filesystem[i].getParent() == path )
{
// does it match our search text?
if (pcrecpp::RE(s + ".*").FullMatch(filename))
{
// make sure we have permission to see this file
if (pFile(filesystem[i].getParent())->isReadable())
{
if (cwd == path)
{
matches.push_back(filesystem[i].getName());
} else {
matches.push_back(path + filesystem[i].getName());
}
}
}
}
}
return matches;
}
void VM::setDefaultNetDomain()
{
if(ip != "0.0.0.0" && net_domains.size() == 0)
{
addNetDomain(1);
}
}
void VM::addNetDomain(int domain)
{
// only positive int IDs are allowed
if(domain>0) net_domains.push_back(domain);
}
void VM::listNetDomains()
{
for(int j=0; j<net_domains.size(); j++)
{
cout << ":netd+ " << net_domains[j] << endl;
}
}
bool VM::inNetDomain(int domain)
{
if(std::find(net_domains.begin(), net_domains.end(), domain) != net_domains.end())
return true;
return false;
}
bool VM::getNetRoute(string ip)
{
for(int i=0; i<vPC.size(); i++)
{
if(vPC[i].getIp() == ip)
{
for(int j=0; j<net_domains.size(); j++)
{
if(vPC[i].inNetDomain(net_domains[j]))
{
return true;
}
}
}
}
return false;
}
string VM::printUserChain()
{
string data = "";
vector<User*>::iterator it;
for(it=user_chain.begin(); it!=user_chain.end(); it++)
{
if(it!=user_chain.begin()) data.append(",");
data.append((*it)->getName());
}
return data;
}
void VM::reset()
{
filesystem.clear();
net_domains.clear();
user_chain.clear();
}
int VM::FSgetFree()
{
return fs_maxSize - filesystem.size();
}
void VM::FSsetMax(int fs_sz)
{
fs_maxSize = fs_sz;
}
Daemon::Daemon (int p, string n, string e, string q)
{
port = p;
name = n;
exec = e;
query = q;
started = false;
}
File::File () {}
File::File (string n, int t, string c, string e)
{
if ( n != "/" )
{
if ( n.substr(n.length()-1) != "/" )
{
pcrecpp::RE("(.*/)").PartialMatch(n, &parent);
} else {
pcrecpp::RE("(.*/).*/").PartialMatch(n, &parent);
}
filename = n.substr(parent.length());
}
else
{
parent = "";
filename = "/";
}
filetype = t;
// set content
if(!c.empty()) content = c;
// set exec function
if(!e.empty()) exec = e;
//sanitize name
if (t == T_FOLDER && filename.substr(filename.length()-1) != "/")
filename += "/";
// set permissions
if (t == T_FILE && e.empty())
{
acl = "644";
}
else
{
acl = "755";
}
owner = "root";
setuid = false;
}
string File::getAcl()
{
return acl;
}
vector <bitset<3> > File::getBitset ()
{
vector <bitset<3> > bset;
int acl_u;
int acl_g;
int acl_o;
acl_u = acl.at(0) - '0';
acl_g = acl.at(1) - '0';
acl_o = acl.at(2) - '0';
bset.push_back(acl_u);
bset.push_back(acl_g);
bset.push_back(acl_o);
return bset;
}
string File::getContent ()
{
if(isReadable()) return content;
return "";
}
string File::getExec ()
{
return exec;
}
string File::getName ()
{
return filename;
}
string File::getOwner ()
{
return owner;
}
string File::getParent ()
{
return parent;
}
bool File::setAcl (string acl)
{
this->acl = acl;
return true;
}
bool File::setOwner (string s)
{
owner = s;
return true;
}
string File::getOnDelete ()
{
return on_delete;
}
bool File::setOnDelete (string s)
{
on_delete = s;
return true;
}
bool File::isReadable ()
{
string user = setuid ? owner : shell.back()->getUsername();
vector <bitset<3> > bset = getBitset ();
// root should ALWAYS be allowed access
if (user == "root") { return true; }
// world
if ( bset[2][2] == 1 )
{
return true;
} else
// owner
if ( user == owner && bset[0][2] == 1 )
{
return true;
}
return false;
}
bool File::isWritable ()
{
string user = setuid ? owner : shell.back()->getUsername();
vector <bitset<3> > bset = getBitset ();
// world
if ( bset[2][1] == 1 )
{
return true;
}
// owner
if ( user == owner && bset[0][1] == 1 )
{
return true;
}
return false;
}
bool File::isExecutable ()
{
string user = setuid ? owner : shell.back()->getUsername();
vector <bitset<3> > bset = getBitset ();
// world
if ( bset[2][0] == 1 )
{
return true;
}
// owner
if ( user == owner && bset[0][0] == 1 )
{
return true;
}
return false;
}
int File::getType ()
{
return filetype;
}
string File::drawAcl ()
{
string acl("");
vector <bitset<3> > bset = getBitset ();
acl += getType() == T_FOLDER ? "d" : "-";
acl += ( bset[0][2] == 1 ) ? "r" : "-";
acl += ( bset[0][1] == 1 ) ? "w" : "-";
if ( setuid == true )
{
acl += "s";
} else {
acl += ( bset[0][0] == 1 ) ? "x" : "-";
}
acl += bset[1][2] ? "r" : "-";
acl += bset[1][1] ? "w" : "-";
acl += bset[1][0] ? "x" : "-";
acl += bset[2][2] ? "r" : "-";
acl += bset[2][1] ? "w" : "-";
acl += bset[2][0] ? "x" : "-";
return acl;
}
void File::setSUID(bool b)
{
setuid = b;
}
User::User (string u, string p)
{
name = u;
password = p;
registerPassword(p);
}
string User::getName()
{
return name;
}
bool User::isPassword (string s)
{
if (password.empty()) return false;
if (password == "*") return true;
if (s == password) return true;
return false;
}
bool registerPassword (string s)
{
string h = vhash(s);
for(int i=0; i<vhashes.size(); i++)
{
if (vhashes[i].hash == h) return true;
}
vhashes.push_back(RT(h,s));
return true;
}
bool User::setPassword (string s)
{
password = s;
registerPassword(s);
return true;
}
bool User::addMail (Mail m)
{
inbox.push_back(m);
return true;
}
bool User::deleteMail (int index)
{
int sz = inbox.size();
inbox.erase(inbox.begin()+index);
if (inbox.size()<sz) return true;
return false;
}
Mail* User::getMail (int index)
{
if (index > inbox.size() || --index < 0 ) return NULL;
return &(inbox[index]);
}
vector<Mail> User::listInbox ()
{
return inbox;
}
int User::inboxSize()
{
return inbox.size();
}
Mail::Mail(string t, string s, string f, string b)
{
header_to = t;
header_subject = s;
header_from = f;
msg_body = b;
}
string Mail::to()
{
return header_to;
}
string Mail::subject()
{
return header_subject;
}
string Mail::from()
{
return header_from;
}
string Mail::body()
{
return msg_body;
}