-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathengine.cpp
3556 lines (3221 loc) · 77.2 KB
/
engine.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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
description: unix hacker simulator
written by Unix-Ninja
original code february, 2010
*/
#include "engine.h"
#include "md5.h"
#include "tinycon.h"
#include <ctime>
#include <iomanip>
#include <fstream>
#include <typeinfo>
#if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32)
#include <libgen.h>
#endif
bool debug_on;
vector<VM> vPC; // available vMachines
vector<VM*> shell; // current shell chain
vector<RT> vhashes; // list of users' password hashes
int cEC = 0; // command exit code
string mission_file; // filename for mission pack
char last_key; // last hotkey pressed
std::map<std::string, std::string> dns_map; // global DNS records
std::map<int, std::string> netd_map; // map of Network Domain IDs/Names
boost::thread *cmd_thread; // reference to running cmd thread
lua_State *lua; // Lua state object
int lua_vdump(vector<string> args);
int lua_restore(vector<string> args);
int lua_save(vector<string> args);
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32)
void sleep(int t)
{
Sleep(t*1000);
}
#endif
// setup console
class tcon : public tinyConsole
{
public:
tcon (std::string s): tinyConsole(s) {;}
int trigger (std::string s);
int hotkeys(char c)
{
if (c == TAB)
{
// save buffer length
int length;
length = buffer.size();
// convert buffer to string
string s;
s.assign(buffer.begin(),buffer.end());
// explode string buffer
vector<string> cmd;
explode(s, " ", cmd);
if (!cmd.size())
{
cmd.push_back(s);
}
// try tab complete on last segment of commandline
vector<string> matches;
matches = shell.back()->tabComplete(cmd.back());
// return if no matches
if (!matches.size())
{
if (cmd.size() == 1)
{
matches = shell.back()->tabComplete("/bin/" + cmd.back());
if (!matches.size()) return 1;
for (int i=0; i<matches.size(); i++)
{
matches[i] = matches[i].substr(5);
}
} else {
return 1;
}
}
// clear commandline if matched
for (int i=0; i<length; i++)
{
cout << "\b \b";
}
// reconstruct commandline
if (cmd.size() > 1)
{
s = ""; // reset s before reconstruction
for (int i=0; i<cmd.size()-1; i++)
{
if (s.length() > 0) s += " ";
s += cmd[i];
}
s += " ";
} else {
s = "";
}
// process matches
string buf;
if (matches.size() == 1)
{
// do we need to replace the ./ ?
if (pcrecpp::RE("^\\./[^\\/]+").PartialMatch(cmd.back()))
{
buf = s + "./" + matches[0];
}
else
{
buf = s + matches[0];
}
if (buf.substr(buf.length()-1) != "/") buf += " ";
// set new buffer
setBuffer(buf);
length = buffer.size();
// copy buffer to screen
std::cout << buf;
} else {
// on multiple matches, find the common chars
// we will loop through the set and match against
// the first index
length = 0;
int l = 0;
for (int i=1; i<matches.size(); i++)
{
for (int pos=0; pos<matches[0].length(); pos++)
{
if (matches[0][pos] == matches[i][pos])
{
l++;
} else {
break;
}
}
if (length)
{
length = l > length ? length : l;
} else {
length = l;
}
}
buf = s + matches[0].substr(0,length);
setBuffer(buf);
cout << buf;
if (last_key == TAB) {
cout << endl;
for (int i=0; i< matches.size(); i++)
{
cout << " " << matches[i] << endl;
}
cout << _prompt << buf;
}
}
last_key = TAB;
return 1;
} else if (c == CTRLC) {
raise(SIGINT);
return 1;
}
last_key = c;
return E_OK;
}
};
tcon tc (std::string("$ "));
// explode the string into words, each in an index of a vector
int explode(std::string line,std::string delimiter,std::vector<std::string> &store)
{
store.clear();
// if line is empty, return fail
if (line.empty()) return -1;
//unsigned int start = 0;
signed int start = -1;
int len = 0;
int i = 0;
std::vector<std::string> temp;
len = delimiter.length();
// if no instance of delimiter exist, return a single index
if ( line.find(delimiter) == std::string::npos )
{
store.push_back(line);
return 1;
}
while ((start = line.find(delimiter)) != std::string::npos) // move along finding delimiter, inc start
{
store.push_back( line.substr( 0, start) );
line.erase( 0, start+len );
i++;
}
if (!line.empty()) store.push_back(line);
i++;
return i;
}
int explode(std::string line, std::string delimiter, std::vector<std::string> &store, int limit)
{
store.clear();
// if line is empty, return fail
if (line.empty()) return -1;
//unsigned int start = 0;
signed int start = -1;
int len = 0;
int i = 0;
std::vector<std::string> temp;
len = delimiter.length();
// if no instance of delimiter exist, return a single index
if ( line.find(delimiter) == std::string::npos )
{
store.push_back(line);
return 1;
}
while ((start = line.find(delimiter)) != std::string::npos) // move along finding delimiter, inc start
{
store.push_back( line.substr( 0, start) );
line.erase( 0, start+len );
i++;
if (i == limit) break;
}
if (!line.empty()) store.push_back(line);
i++;
return i;
}
/*
void replace_all(std::string& str, const std::string& from, const std::string& to)
{
if (from.empty()) return;
size_t pos = 0;
while ((pos = str.find(from, pos)) != std::string::npos)
{
str.replace(pos, from.length(), to);
pos += to.length();
}
}
*/
string realpath(string path)
{
if (path == "/") return path;
if (path.substr(0,1) != "/")
{
// if relative path, append cwd
path = shell.back()->getCwd() + path;
}
// sanitize path
vector<string> store, realp;
explode(path, "/", store);
for (int i=1; i<store.size(); i++)
{
if (!store.empty())
{
if (store[i] == "..")
{
if (realp.size()) realp.pop_back();
}
else if (store[i] != ".")
{
realp.push_back(store[i]);
}
}
}
// reconstruct real path
path = "";
for (int i=0; i<realp.size(); i++)
{
path += "/" + realp[i];
}
if (path.empty()) path = "/";
// get pointer to file
File *fp = shell.back()->pFile(path);
// if no file or directory found, return nothing
if (!fp) return "";
if (fp->getType() == T_FOLDER)
{
// remember to add trialing slash
if (path.length()>1 && path.substr(path.length()-1) != "/")
path += "/";
}
return path;
}
void addDNS(string name, string record)
{
dns_map.insert(pair<string, string> (name, record));
}
string getDNS(string name)
{
if (name.empty()) return "";
// if we are given an IP, just return it
if (pcrecpp::RE("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}").FullMatch(name)) return name;
// return the record from the DNS map
return dns_map[name];
}
int registerNetDomain(int domain_id)
{
std::map<int, string>::iterator it;
if ((it=netd_map.find(domain_id)) == netd_map.end())
{
netd_map[domain_id] = "";
}
return domain_id;
}
int registerNetDomain(string domain_name)
{
int domain_id = 0;
std::map<int, string>::iterator it;
// do we need to set the domain?
domain_id = 1;
for (it=netd_map.begin(); it!=netd_map.end(); it++)
{
if (it->second == domain_name) break; else domain_id++;
}
if (it==netd_map.end()) netd_map[domain_id] = domain_name;
return domain_id;
}
int getNetDomainId(string domain_name)
{
int domain_id = 1;
std::map<int, string>::iterator it;
for (it=netd_map.begin(); it!=netd_map.end(); it++)
{
if (it->second == domain_name) return domain_id; else domain_id++;
}
return E_OK;
}
string execFile(string f)
{
File *fp = shell.back()->pFile(f);
if (fp)
{
if (fp->isExecutable())
{
return fp->getExec();
} else {
return "!";
}
}
return "";
}
bool chainVM(string remote, int port, string user = "")
{
string svc_name;
string password;
string host = getDNS(remote);
if (remote == "0.0.0.0")
{
cout << "no route to host!" << endl;
return false;
}
for (int i=0; i < vPC.size(); i++)
{
if (vPC[i].getIp() == host && vPC[i].getIp() != "0.0.0.0" && shell.back()->getNetRoute(host))
{
vector<Daemon*> svc = vPC[i].getDaemons();
for (int ii=0; ii<svc.size(); ii++)
{
svc_name = svc[ii]->name;
if ( svc_name == "telnet" || svc_name == "telnetd" || svc_name == "ssh" || svc_name == "sshd")
{
if (port == svc[ii]->port)
{
// get usrname
if (user.empty())
{
cout << "Login:";
user = tc.getLine();
}
// get password
cout << "Password:";
password = tc.getLine(M_PASSWORD);
// try login
shell.push_back(&(vPC[i]));
if (vPC[i].login(user, password))
{
// add VM to chain
// make broken user
shell.back()->startBrokenCounter();
cout << "connected." << endl;
return true;
} else {
shell.pop_back();
cout << "invalid username or password." << endl;
return false;
}
} else {
sleep(3);
cout << "connection timed out." << endl;
return false;
}
}
}
}
}
cout << remote << ": nodename nor servname provided, or not known." << endl;
return false;
}
VM* getVM(string hostname)
{
for (int i=0; i < vPC.size(); i++)
{
if (vPC[i].getHostname() == hostname) return &vPC[i];
}
return NULL;
}
std::string ab64_encode(char const* bytes_to_encode, unsigned int in_len)
{
// slight base64 varient
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+.";
std::string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
while (in_len--) {
char_array_3[i++] = *(bytes_to_encode++);
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (i = 0; (i <4) ; i++)
ret += base64_chars[char_array_4[i]];
i = 0;
}
}
if (i)
{
for (j = i; j < 3; j++)
char_array_3[j] = '\0';
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++)
ret += base64_chars[char_array_4[j]];
while ((i++ < 3))
ret += '=';
}
return ret;
}
static inline bool is_ab64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '.'));
}
std::string ab64_decode(std::string const& encoded_string)
{
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+.";
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string ret;
while (in_len-- && ( encoded_string[in_] != '=') && is_ab64(encoded_string[in_]))
{
char_array_4[i++] = encoded_string[in_]; in_++;
if (i ==4)
{
for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
ret += char_array_3[i];
i = 0;
}
}
if (i)
{
for (j = i; j <4; j++)
char_array_4[j] = 0;
for (j = 0; j <4; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
}
return ret;
}
string vhash(string input)
{
string hash;
char t[] = { 'x', 'a', 'r', 'b', 'p', 'w', 'm', 'l', 'f', 'j', 'v', 'd', 'q', 'k', 'z', 'h' };
int len = 15;
for (int i=0; i<input.length(); i++)
{
for (int n=0; n<len; n++)
{
if (n%4 == 0)
{
t[n] = t[n] | input.at(i);
}
else if ((n>1) && (i%2 == 0))
{
t[n] = t[n] & t[n-1];
} else {
t[n] = t[n] ^ input.at(i);
}
}
}
for (int n=0; n<len; n++) hash += t[n];
hash = ab64_encode(hash.c_str(), len);
return hash;
}
int can_access(string s)
{
// are we allowed to see these files?
vector<string> store;
explode(s, "/", store);
string path = "/";
File *fp;
for (int i=0; i<store.size(); i++)
{
if (store[i].empty()) continue;
path += store[i];
fp = shell.back()->pFile(path);
if (!fp)
{
// file doesn't exist!
return E_NO_FILE;
}
if (!fp->isReadable())
{
// no permission to access
return E_DENIED;
}
if (fp->getType() == T_FOLDER)
{
path += "/";
}
}
return E_OK;
}
// *********** built-in commands ************** //
int cmd_cat(vector<string> args)
{
string f;
int ret =0;
File *fp;
for (int i=1; i<args.size(); i++)
{
if (args[i].substr(0,1)=="-") continue;
f = realpath(args[i]);
fp = shell.back()->pFile(f);
if (!fp) {
cout << "cat: " << args[i] << ": No such file or directory." << endl;
ret = 1;
} else {
if (can_access(f) == E_OK)
{
cout << fp->getContent();
} else {
cout << "-sh: cat: " << args[i] << ": Permission denied." << endl;
ret = 1;
}
}
}
return ret;
}
int cmd_cd(vector<string> args)
{
string path;
if (args.size() < 2)
{
// if no args, set to home
path = "/home/";
return 1;
} else {
path = args[1];
path = realpath(path);
}
// try to change dir
int status = shell.back()->setCwd(path);
switch (status)
{
case E_NO_FILE:
cout << "-sh: cd: " << args[1] << ": No such directory." << endl;
return 1;
break;
case E_DENIED:
cout << "-sh: cd: " << args[1] << ": Permission denied." << endl;
return 1;
break;
}
// dir changed
return E_OK;
}
int cmd_clear()
{
// we're going to cheat here. this should probably be re-made properly at some point
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32)
system("cls");
#else
system("clear");
#endif
return E_OK;
}
int cmd_date(vector<string> &args)
{
// current date/time based on current system
time_t now = time(0);
if (args.size() > 1 && args[1] == "+%s")
{
cout << now << endl;
} else {
// convert now to string form
char* dt = ctime(&now);
cout << dt;
}
return E_OK;
}
int cmd_echo(vector<string> &args)
{
args.erase(args.begin());
if (args.size() == 0)
{
cout << endl;
return E_OK;
}
for (int i=0; i<args.size(); i++)
{
if (args[i].at(0)=='$')
{
string var = args[i].substr(1,args[i].length()-1);
if (var == "PATH")
{
cout << "/bin/:./";
}
else if (var == "SHELL")
{
cout << "/bin/sh";
}
else if (var == "?")
{
cout << cEC;
}
}
else
{
cout << args[i] << " ";
}
}
cout << endl;
return E_OK;
}
int cmd_exit()
{
// logout and check for a shell chain break
if (shell.back()->logout() < 1 )
{
// leave VM
if (shell.size() > 1)
{
shell.pop_back();
cout << "connection closed." << endl;
} else {
// if no VMs in shell chain, ask for init login again
for (;;)
{
string user;
string password;
cout << endl;
if (shell.back()->getUserCount()>0) break;
cout << "Login: ";
user = tc.getLine();
cout << "Password: ";
password = tc.getLine(M_PASSWORD);
if (shell.back()->login(user, password))
{
break;
}
}
}
} else {
cout << "logout." << endl;
}
return E_OK;
}
int cmd_quit()
{
cout << "Are you sure you want to quit the game? (y/N) ";
string quit_game = tc.getLine();
if (quit_game == "y" || quit_game == "Y")
{
cout << "[Thanks for playing! Come back soon!]";
tc.quit();
}
return E_OK;
}
int cmd_help()
{
cout << "Currently supported built-in commands:\n cat\n cd\n clear\n date\n echo\n exit\n help\n hint\n hostname\n id\n ifconfig\n load\n ls\n lspci\n mail\n ping\n pwd\n quit\n rm\n save\n su\n telnet\n uname\n whoami" << endl;
return E_OK;
}
int cmd_hint()
{
if (!shell.back()->getHint().empty())
{
cout << shell.back()->getHint() << endl;
} else {
cout << "Sorry, no hints are available for this VM." << endl;
}
return E_OK;
}
int cmd_hashcat(vector<string> args)
{
// interruptable threads set error exits at the head
cEC = 1;
string plain;
cout << "Initializing hashcat-micro (for virtual PCs)..." << endl;
if (args.size() != 2 || args[1] == "-h")
{
cout << "usage: " << args[0] << " hash" << endl;
cout << " Please note: hashcat-micro does not support hashfiles at this time." << endl;
return cEC;
}
cout << "Added 1 hash" << endl << "Activating quick-digest mode for single-hash" << endl;
cout << endl << "Running." << flush;
// find plain in rainbow table
for (int i=0; i<vhashes.size(); i++)
{
if (vhashes[i].hash == args[1])
{
plain = vhashes[i].plain;
break;
}
}
// let's use the compute functions to simulate crack time
int cracktime = plain.length();
if (cracktime)
{
cracktime /= 4;
if (pcrecpp::RE("[0-9]").PartialMatch(plain)) { cracktime *= 2; }
if (pcrecpp::RE("[a-z]").PartialMatch(plain)) { cracktime *= 4; }
if (pcrecpp::RE("[A-Z]").PartialMatch(plain)) { cracktime *= 4; }
if (pcrecpp::RE("[^0-9a-zA-Z]").PartialMatch(plain)) { cracktime *= 2; }
cracktime = cracktime / (int) sqrt((double) shell.back()->getCPower());
} else {
cracktime = 32;
}
for (int i=0; i<cracktime; i++)
{
cout << "." << flush;
boost::this_thread::sleep(boost::posix_time::seconds(1));
}
cout << endl << endl;
if (!plain.empty())
{
cout << args[1] << ":" << plain << endl;
cout << "All hashes have been recovered" << endl;
cEC = 0;
return E_OK;
}
cout << "Index: 1/1 (segment)" << endl;
cout << "Progress: 100%" << endl;
cout << "Recovered: 0/1 hashes" << endl;
return cEC;
}
int cmd_hostname()
{
cout << shell.back()->getHostname() << endl;
return E_OK;
}
int cmd_id(vector<string> args)
{
string username;
if (args.size() < 2)
{
username = shell.back()->getUsername();
} else {
username = args[1];
}
int cEC = E_OK;
User* usr;
usr = shell.back()->getUser(username);
if (!usr)
{
cout << "id: " << username <<": no such user" << endl;
return 1;
} else {
cout << "uid=" << username << endl;
}
return cEC;
}
int cmd_ifconfig()
{
if (shell.back()->getIp() == "0.0.0.0") return 1;
cout << "en0 UP BROADCAST SMART SIMPLEX MULTICAST MTU 1500 Metric 0" << endl;
cout << " inet addr " << shell.back()->getIp() << endl;
cout << " media autoselect" << endl;
return E_OK;
}
int cmd_ls(vector<string> args)
{
string cmd;
string n (shell.back()->getCwd()); // set name to cwd
string op;
string owners;
bool longlist = false;
File *fp, *fp2;
// parse arguments. first non option is target
for (int i=1; i<args.size(); i ++)
{
op = args[i].substr(0,1);
if (op != "-")
{
cmd = args[i];
n = realpath(cmd);
break;
}
else if (args[i] == "-l")
{
longlist = true;
}
}
if (n.empty())
{
cout << "ls: " << cmd << ": No such file or directory." << endl;
return 1;
}
// are we allowed to see these files?
vector<string> store;
explode(n, "/", store);
string path = "/";
for (int i=0; i<store.size(); i++)
{
if (store[i].empty()) continue;
fp = shell.back()->pFile(path + store[i]);
if (!fp)
{
cout << "ls: " << cmd << ": No such file or directory." << endl;
return 1;
}
if (fp->getType() == T_FOLDER)
{
path += store[i] + "/";
}
fp2 = shell.back()->pFile(path);
if (!fp->isReadable() || !fp2->isReadable())
{
cout << "ls: " << cmd << ": permission denied." << endl;
return 1;
}
}
// get pointer to target
fp = shell.back()->pFile(n);
// get list of files
vector<string> files = shell.back()->getFiles(n);
// sort files
sort(files.begin(), files.end());
// loop through files
for (int i=0; i < files.size(); i++)
{
if (longlist)
{
fp = shell.back()->pFile(path + files[i]);
if (fp->getType() == T_FOLDER)
{
files[i] = files[i].substr(0, files[i].length()-1);
}
owners = " " + fp->getOwner() + " " + fp->getOwner();
cout << fp->drawAcl() << left << setw(20) << owners << files[i] << endl;
} else {
cout << files[i] << endl;
}
}
return E_OK;
}
int cmd_lspci()
{
cout << "00:00.0 Host bridge: 82443BX/ZX" << endl;
cout << "00:0f.0 VGA compatible controller (rev 01)" << endl;
cout << "00:10.0 Storage controller: AB Logic / Fusion-X UltraStorage (rev 01)" << endl;
cout << "00:20.0 Ethernet controller: FastNet Gigabit Ethernet Controller (rev 01)" << endl;
vector<string> gpus = shell.back()->getGPUs();
for (int i=0; i<gpus.size(); i++)
{
cout << setw(2) << setfill('0') << i+1;;
cout << ":00.0 " << gpus.at(i) << endl;;
}
return E_OK;
}
int cmd_mail()
{
User *up = shell.back()->getUser(shell.back()->getUsername());
if ( up->inboxSize() )
{
cout << "Mail version 3.2 6/6/91. Type h for help." << endl;
string cmd = "f";
string buffer;
vector<string> args;
Mail *m;
while (cmd != "q" && cmd != "quit")
{
if (cmd == "f") {
for (int i=0; i<up->inboxSize(); i++)
{