forked from team-eureka/Eureka-Panel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpanel.c
721 lines (587 loc) · 19.6 KB
/
webpanel.c
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
#include "stdio.h"
#include "cgic.h"
#include "stdlib.h"
#include "time.h"
#include "unistd.h"
#include "assert.h"
#include "string.h"
#include "minIni.h"
#include "malloc.h"
#include "modules/common_func.c"
#include "modules/web_module_home.c"
#include "modules/web_module_logcat.c"
// #include "modules/web_module_settings.c"
#include "modules/web_module_headers.c"
#include "modules/web_module_footer.c"
#include "modules/web_module_status.c"
#include "modules/web_module_debug.c"
#include "modules/web_module_aboutus.c"
#include "modules/web_module_companion.c"
#include "modules/web_module_popup_headers.c"
#include "cards/card_slides.c"
#include "cards/card_layout_header.c"
#include "cards/card_layout_footer.c"
#include "cards/card_layout_settings.c"
#include "cards/card_layout_httpkill.c"
#include "cards/card_links.c"
#include "cards/card_links_big.c"
#include "cards/card_link_status.c"
#include "cards/card_link_settings.c"
#include "cards/card_link_team.c"
#include "cards/card_link_debug.c"
#include "cards/card_network.c"
#include "cards/card_wifi.c"
#include "cards/card_wifi_front.c"
#include "cards/card_wifi_back.c"
#include "cards/card_twitter.c"
#include "cards/card_status.c"
#include "cards/card_status_front.c"
#include "cards/card_status_back.c"
#include "cards/card_playing.c"
#include "cards/card_playing_front.c"
#include "cards/card_playing_back.c"
#include "cards/card_dns.c"
#include "cards/card_dns_front.c"
#include "cards/card_dns_back.c"
#include "cards/card_security.c"
#include "cards/card_security_front.c"
#include "cards/card_security_back.c"
#include "cards/card_services.c"
#include "cards/card_services_front.c"
#include "cards/card_services_back.c"
#include "cards/card_team_rom.c"
#include "cards/card_team_chris.c"
#include "cards/card_team_thomas.c"
#include "cards/card_team_trevor.c"
#include "cards/card_team_simon.c"
#include "cards/card_whitelist.c"
#include "cards/card_whitelist_front.c"
#include "cards/card_whitelist_overlay.c"
#include "cards/card_whitelist_back.c"
#include "cards/card_updates.c"
#include "cards/card_updates_front.c"
#include "cards/card_updates_back.c"
#define sizearray(a) (sizeof(a) / sizeof((a)[0]))
#define QS_LEN 65536
#define BLOCK_SIZE 512
//This is the main web panel application that includes all portal modules
//Please make modules where possible, and include them. Only modify this if necessary
int CookieCheck()
{
char password[1024];
char protected[1024];
char session[1024];
char cookie[1024];
read_config_var("Security", "webprotected", protected);
read_config_var("Security", "pass", password);
if (compStr(protected, "undefined", 1024) || !compStr(protected, "1", 1024) || compStr(password, "undefined", 1024)) {
return 1;
} else {
read_config_var("Security", "session", session);
if (cgiCookieString("session", cookie, sizeof(cookie)) == cgiFormSuccess) {
if (compStr(session, cookie, 1024) && !compStr(session, "undefined", 1024) && !compStr(session, "", 1024))
{return 1;}
else
{return 0;}
} else {
return 0;
}
}
}
void CookieSet()
{
char *cname = "session";
char *cvalue;
int r;
FILE *ptr_file;
char command[300];
char buf[1024];
srand ( (unsigned)time ( NULL ) );
r = rand();
snprintf(command, sizeof command, "echo -n '%d' | busybox sha1sum | busybox awk -F \" \" '{print $1}' | busybox tr '\n' ' ' | busybox tr -d ' '", r);
ptr_file=popen(command,"r");
while (fgets(buf,1000, ptr_file)!=NULL)
{
cvalue = buf;
}
pclose(ptr_file);
write_config_var("Security", "session", buf);
if (strlen(cname) && strlen(cvalue)) {
// cgiHeaderCookieSetString(cname, cvalue, 1200, cgiScriptName, cgiServerName);
// cgiHeaderCookieSetString(cname, cvalue, 1200, path, cgiServerName);
printf("Content-type: text/html\n");
printf("Set-Cookie: %s=%s; Path=/; Domain=%s; HttpOnly\n\n", cname, cvalue, cgiServerName);
}
}
int processLogin()
{
char value[80];
FILE *ptr_file;
char command[200];
char buf[1024];
char *sha1pass;
cgiFormStringNoNewlines("action", value, 80);
if(compStr(value, "login", sizearray(value)))
{
cgiFormStringNoNewlines("welcome_password", value, 80);
snprintf(command, sizeof command, "echo -n '%s' | busybox sha1sum | busybox awk -F \" \" '{print $1}' | busybox tr '\n' ' ' | busybox tr -d ' '", value);
ptr_file=popen(command,"r");
while (fgets(buf,1000, ptr_file)!=NULL)
{
strcpy(sha1pass, buf);
}
pclose(ptr_file);
read_config_var("Security", "pass", buf);
// printf("%s = %s?", buf, sha1pass);
// n = write_config_var("Security", "pass", sha1pass);
if ( compStr(sha1pass, buf, 1024) )
return 1;
else
return 0;
}
}
void writenewPass()
{
char value[80];
int n;
FILE *ptr_file;
char command[200];
char buf[1024];
// char *sha1pass;
cgiFormStringNoNewlines("newpass1", value, 80);
snprintf(command, sizeof command, "echo -n '%s' | busybox sha1sum | busybox awk -F \" \" '{print $1}' | busybox tr '\n' ' ' | busybox tr -d ' '", value);
ptr_file=popen(command,"r");
while (fgets(buf,1000, ptr_file)!=NULL)
{
// printf("sha: %s, buf: %s<p>", sha1pass, buf);
n = write_config_var("Security", "pass", buf);
// strcpy(sha1pass, buf);
}
pclose(ptr_file);
// read_config_var("Security", "pass", buf);
// printf("%s = %s?", buf, sha1pass);
// n = write_config_var("Security", "pass", sha1pass);
}
int writeNewWhitelist()
{
char value[80];
char applications[20000];
char enabledappids[20000];
char configuration[20000];
cgiFormStringNoNewlines("action", value, 80);
if(compStr(value, "update", sizearray(value)))
{
cgiFormStringNoNewlines("applications", applications, 20000);
cgiFormStringNoNewlines("enabled_app_ids", enabledappids, 20000);
cgiFormStringNoNewlines("configuration", configuration, 20000);
FILE *f = fopen("/data/eureka/apps.conf", "w");
fprintf(f, ")]}'\n{\"applications\":[%s],\"enabled_app_ids\":[%s],\"configuration\":{%s}}", applications, enabledappids, configuration);
fclose(f);
}
}
int processPostData()
{
char buf[1024];
char section[80];
char field[80];
char value[80];
int smartDnsUpdatePerformed;
char smartDnsProviderPrimary[1024];
char smartDnsProviderSecondary[1024];
int n;
char **array, **arrayStep;
cgiFormStringNoNewlines("action", value, 80);
// sscanf(postData, "action=%[0-9a-zA-Z]", &action);
if(compStr(value, "update", sizearray(value)))
{
// token = strtok (postData,"&");
// while (token != NULL) {
//GO THROUGH ALL FORM KEY/VALUES
if (cgiFormEntries(&array) == cgiFormSuccess) {
arrayStep = array;
while (*arrayStep) {
//check if contains "/" so we know it's a var to update
// urldecode2(decodedToken, token);
if(strchr(*arrayStep, '/'))
{
cgiFormStringNoNewlines(*arrayStep, value, 80);
sscanf(*arrayStep, "%[0-9a-zA-Z]/%[0-9a-zA-Z]", section, field);
// sscanf(decodedToken, "%[0-9a-zA-Z]/%[0-9a-zA-Z]=%[0-9a-zA-Z.:/_%-]", section, field, value);
//check if DNS update and allowed to perform
if(compStr(section, "DNS", sizearray(section)) && !compStr(field, "useDHCP", sizearray(field)) && (smartDnsUpdatePerformed == 1))
{
//do nothing
}
else
{
n = write_config_var(section, field, value);
}
//Check if an update for smartDNS selected which is not set to other
if(compStr(section, "SmartDNS", sizearray(section)) && compStr(field, "selected", sizearray(section)) && !compStr(value, "other", sizearray(section)))
{
strcpy(buf, "SmartDNS-");
strcat(buf, value);
//get DNS details for SmartDNS provider
read_config_var(buf, "primary", smartDnsProviderPrimary);
read_config_var(buf, "secondary", smartDnsProviderSecondary);
//set DNS details to selected SmartDNS Provider
n = write_config_var("DNS", "primary", smartDnsProviderPrimary);
n = write_config_var("DNS", "secondary", smartDnsProviderSecondary);
//set that DNS update has been performed so any unwanted commits don't occur after
smartDnsUpdatePerformed=1;
}
}
// token = strtok (NULL, "&");
arrayStep++;
}
cgiStringArrayFree(array);
}
}
}
//main function for web services
int cgiMain()
{
char strPage[1023];
char strFooters[1023];
char strHeaders[1023];
char strPopup[1023];
char command[1024] = "echo Welcome to EurekaRom";
char decodedCommand[1024];
char *data;
char *token;
const char *key, *value;
data = malloc(QS_LEN);
token = malloc(QS_LEN);
key = malloc(QS_LEN);
value = malloc(QS_LEN);
FILE *ptr_file;
//Operations if POST detected
if(compStr(cgiRequestMethod, "POST", sizearray(cgiRequestMethod)))
{
if (getenv("QUERY_STRING"))
{
token = strtok (getenv("QUERY_STRING"),"&");
while (token != NULL)
{
sscanf(token, "%[^=]=%65536s", key, value);
if ( compStr(key, "page", sizearray(key) ))
{
strcpy(strPage, value);
}
token = strtok (NULL, "&");
}
}
if (compStr(strPage, "debug", sizearray(strPage) ))
{
cgiHeaderContentType("text/html");
if( CookieCheck() ) {
// sscanf(postData, "page=debug&command=%[^,]", command);
// urldecode2(decodedCommand, command);
card_layout_header();
web_module_debug(decodedCommand);
card_layout_footer();
}
}
else if ( compStr(strPage, "white", sizearray(strPage)) )
{
cgiHeaderContentType("text/html");
if( CookieCheck() ) {
processPostData();
// ptr_file=popen("busybox killall eureka_shell","r");
card_whitelist_front();
}
}
else if ( compStr(strPage, "editwhite", sizearray(strPage)) )
{
cgiHeaderContentType("text/html");
if( CookieCheck() ) {
writeNewWhitelist();
// ptr_file=popen("busybox killall eureka_shell","r");
card_whitelist_overlay();
}
}
else if ( compStr(strPage, "dns", sizearray(strPage)) )
{
cgiHeaderContentType("text/html");
if( CookieCheck() ) {
processPostData();
//reload dhcp for DNS change to take effect
ptr_file=popen("dhcpcd -n mlan0","r");
sleep(1);
pclose(ptr_file);
card_dns_front();
}
}
else if ( compStr(strPage, "theme", sizearray(strPage)) )
{
cgiHeaderContentType("text/html");
if( CookieCheck() ) {
processPostData();
settings_theme();
}
}
else if ( compStr(strPage, "services", sizearray(strPage)) )
{
cgiHeaderContentType("text/html");
if( CookieCheck() ) {
// processPostData(); TOO DANGEROUS (you may switch everything off by accident)
// let's do it one by one and activate the service if there is no value given
// also removed the action update input field
char httpvalue[20];
char sshvalue[20];
char adbvalue[20];
char telnetvalue[20];
cgiFormStringNoNewlines("Services/telnet", telnetvalue, 20);
if(compStr(telnetvalue, "disabled", 20)) {
write_config_var("Services", "telnet", "0");
} else {write_config_var("Services", "telnet", "1");}
cgiFormStringNoNewlines("Services/adb", adbvalue, 20);
if(compStr(adbvalue, "disabled", 20)) {
write_config_var("Services", "adb", "0");
} else {write_config_var("Services", "adb", "1");}
cgiFormStringNoNewlines("Services/ssh", sshvalue, 20);
if(compStr(sshvalue, "disabled", 20)) {
write_config_var("Services", "ssh", "0");
} else {write_config_var("Services", "ssh", "1");}
cgiFormStringNoNewlines("Services/http", httpvalue, 20);
if(compStr(httpvalue, "disabled", 20)) {
write_config_var("Services", "http", "0");
printf("ikilledhttp");
exit(0);
} else {write_config_var("Services", "http", "1");}
card_services_front();
}
}
else if ( compStr(strPage, "newpass", sizearray(strPage)) )
{
if( CookieCheck() ) {
char newpass1[80];
char newpass2[80];
cgiFormStringNoNewlines("newpass1", newpass1, 80);
cgiFormStringNoNewlines("newpass2", newpass2, 80);
if(compStr(newpass1, newpass2, 80) && newpass1[0] != '\0')
{
char protected[80];
cgiFormStringNoNewlines("Security/webprotected", protected, 80);
if (compStr(protected, "1", 80))
{
CookieSet();
printf("\n<script>");
printf("\n(function($)");
printf("\n{");
printf("\n $(document).ready(function()");
printf("\n {");
printf("\n $('#logout').removeClass(\"invisible\");$('#settings').addClass(\"logout\");");
printf("\n });");
printf("\n})(jQuery);");
printf("\n</script>");
}
printf("<div class=\"actiontaken\">Password updated</div>");
writenewPass();
processPostData();
//Todo: RELOAD SSH Server and maybe kill session?
card_security_front();
} else {
cgiHeaderContentType("text/html");
printf("<div class=\"actiontaken\">No match, try again</div>");
card_security_front();
}
}
}
else if ( compStr(strPage, "security", sizearray(strPage)) )
{
cgiHeaderContentType("text/html");
if( CookieCheck() ) {
processPostData();
card_security_front();
}
}
else if (compStr(strPage, "login", sizearray(strPage) ))
{
if( processLogin() ) {
CookieSet();
// cgiHeaderContentType("text/html");
printf("Correct");
} else {
cgiHeaderContentType("text/html");
printf("Wrong");
}
}
else if (compStr(strPage, "killhttp", sizearray(strPage) ))
{
char value[80];
if( CookieCheck() ) {
cgiFormStringNoNewlines("action", value, 80);
if(compStr(value, "killhttp", sizearray(value)))
{
cgiHeaderContentType("text/html");
printf("I got killed");
// restart with: lighttpd -f /system/etc/httpd.conf -m /system/usr/lib/
FILE *ptr_file;
ptr_file=popen("busybox killall lighttpd","r");
pclose(ptr_file);
exit(0);
}
}
}
else
{
cgiHeaderContentType("text/html");
card_layout_header();
printf("You should not be here. Like never. Do you have JavaScript and Cookies enabled?");
card_layout_footer();
}
}
else
{
if (!getenv("QUERY_STRING"))
{
//no query string
card_layout_header();
card_layout_footer();
}
if (getenv("QUERY_STRING"))
{
//LOGOUT and redirect back to start
if ( compStr(getenv("QUERY_STRING"), "logout", 1024 ))
{
char *cname = "session";
char *cvalue = "";
//only log out (kill session) if you own the session
if( CookieCheck() ){write_config_var("Security", "session", "undefined");}
// cgiHeaderCookieSetString(cname, cvalue, 0, path, cgiServerName);
// printf("Content-type: text/html\n");
printf("Set-Cookie: %s=%s; Path=/; Domain=%s; HttpOnly\n", cname, cvalue, cgiServerName);
printf("Location: /\n\n");
// cgiHeaderLocation(location);
exit(0);
}
else if ( compStr(getenv("QUERY_STRING"), "ikilledhttp", 1024 ))
{
if( CookieCheck() ){
cgiHeaderContentType("text/html");
card_httpkill();
// need to do that with a java redirect, else it's killed before shoing the confirmation
// ptr_file=popen("busybox killall lighttpd","r");
// pclose(ptr_file);
exit(0);
}
}
//WITHOUT POST THERE IS NO LOGIN, SO PRINT HEADERS. But: this, if possible, should be all done with ajax and POST.
cgiHeaderContentType("text/html");
//parse Query_STRING for page
//query string present
token = strtok (getenv("QUERY_STRING"),"&");
while (token != NULL)
{
sscanf(token, "%[^=]=%65536s", key, value);
if ( compStr(key, "page", sizearray(key) ))
{
strcpy(strPage, value);
}
if ( compStr(key, "headers", sizearray(key) ))
{
strcpy(strHeaders, value);
}
if ( compStr(key, "footers", sizearray(key) ))
{
strcpy(strFooters, value);
}
if ( compStr(key, "popup", sizearray(key) ))
{
strcpy(strPopup, value);
}
token = strtok (NULL, "&");
}
if ( compStr(strPage, "verify", sizearray(strPage) ))
{
if( CookieCheck() ) {
printf("Correct");
exit(0);
} else {
printf("Wrong");
exit(0);
}
}
// Call this before headers
if ( compStr(strPage, "dumpstate", sizearray(strPage) ))
{
dumpstate();
exit(0);
}
//check for which page to load
if ( compStr(strHeaders, "0", sizearray(strHeaders) ))
{
}
else
{
card_layout_header();
}
if ( compStr(strPopup, "1", sizearray(strHeaders) ))
{
web_module_popup_headers(strPage);
}
// for auto update (ajax)
if( CookieCheck() ) {
if ( compStr(strPage, "wifi_front", sizearray(strPage) ))
{
card_wifi_front();
}
else if ( compStr(strPage, "status_front", sizearray(strPage) ))
{
card_status_front();
}
else if ( compStr(strPage, "playing_front", sizearray(strPage) ))
{
card_playing_front();
}
else if ( compStr(strPage, "cards", sizearray(strPage) ))
{
card_slides();
}
} else {
printf("\n<script type=\"text/javascript\">");
printf("\n$(function() {");
printf("\nwindow.location.replace(\"/\");");
printf("\n});");
printf("\n</script>");
}
if ( compStr(strPage, "logcat", sizearray(strPage) ))
{
web_module_logcat();
}
if ( compStr(strPage, "forceupdatecheck", sizearray(strPage) ))
{
forceupdatecheck();
}
if ( compStr(strPage, "debug", sizearray(strPage) ))
{
web_module_debug(command);
}
if ( compStr(strPage, "companion", sizearray(strPage) ))
{
web_module_companion();
}
if ( compStr(strPage, "aboutus", sizearray(strPage) ))
{
web_module_aboutus();
}
if ( compStr(strPage, "Reboot", sizearray(strPage) ))
{
reboot();
}
if ( compStr(strPage, "factorydatareset", sizearray(strPage) ))
{
factorydatareset();
}
if ( compStr(strFooters, "0", sizearray(strFooters) ))
{
}
else
{
card_layout_footer();
}
}
}
return 0;
}