-
Notifications
You must be signed in to change notification settings - Fork 46
/
web_ex.c
2040 lines (1723 loc) · 46.4 KB
/
web_ex.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
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
/*
* Broadcom Home Gateway Reference Design
* Web Page Configuration Support Routines
*
* Copyright 2001, Broadcom Corporation
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Broadcom Corporation;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Broadcom Corporation.
*
* $Id: web_ex.c,v 1.5 2004/01/05 05:39:18 Cheni_Shen Exp $
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <wlutils.h>
#include <typedefs.h>
#include <shutils.h>
#include <bcmconfig.h>
#include <bcmutils.h>
#include <bcmnvram.h>
#include "bcmnvram_f.h"
#include "common.h"
#include "httpd.h"
#define sys_upgrade(image) eval("write", image, "linux")
#define sys_upload(image) eval("nvram", "restore", image)
#define sys_download(file) eval("nvram", "save", file)
//#define sys_restore(sid) eval("nvram_x", "get", (sid))
#define sys_commit() (flashfs_commit(), nvram_commit())
#define sys_default() eval("erase", "nvram")
#define GROUP_FLAG_REFRESH 0
#define GROUP_FLAG_DELETE 1
#define GROUP_FLAG_ADD 2
#define GROUP_FLAG_REMOVE 3
#define IMAGE_HEADER "HDR0"
#define PROFILE_HEADER "HDR1"
#define MAX_VERSION_LEN 64
#define MAX_PID_LEN 12
#define MAX_HW_COUNT 4
extern void readPrnID();
static int apply_cgi_group(webs_t wp, int sid, const char *groupName, int flag);
static int nvram_add_group_table(webs_t wp, const struct group_variable *gv, int count);
static int ej_select_country(int eid, webs_t wp, int argc, char **argv);
static int wl_channels_in_country(char *abbrev, int channels[]);
static int wl_channels_in_country_asus(char *abbrev, int channels[]);
static int do_upload_file(const char *upload_file, const char *url,
FILE *stream, int len, const char *boundary_t,
int (*validate)(FILE *fifo, int len));
#define MAX_GROUP_COUNT 300
static char urlcache[128];
static const char *next_host;
static int delMap[MAX_GROUP_COUNT];
static char SystemCmd[128];
static char UserID[32]="";
static char UserPass[32]="";
static char ProductID[32]="";
extern int redirect;
#define ROOT_SSH_DIR "/usr/local/root/.ssh"
#define SSH_AUTHORIZED_KEYS ROOT_SSH_DIR"/authorized_keys"
static int
reltime(unsigned int seconds, char *buf)
{
int off = 0;
if (seconds > 60*60*24) {
off += sprintf(buf + off, "%d days, ", seconds / (60*60*24));
seconds %= 60*60*24;
}
if (seconds > 60*60) {
off += sprintf(buf + off, "%d hours, ", seconds / (60*60));
seconds %= 60*60;
}
if (seconds > 60) {
off += sprintf(buf + off, "%d minutes, ", seconds / 60);
seconds %= 60;
}
off += sprintf(buf + off, "%d seconds", seconds);
return off;
}
/******************************************************************************/
/*
* Redirect the user to another webs page
*/
static void websRedirect(webs_t wp, const char *url)
{
//dprintf("Redirect to : %s\n", url);
websWrite(wp, "<html><head>\r\n");
if (next_host && *next_host)
websWrite(wp, "<meta http-equiv=\"refresh\" content=\"0; url=http://%s/%s\">\r\n", next_host, url);
else
websWrite(wp, "<meta http-equiv=\"refresh\" content=\"0; url=%s\">\r\n", url);
websWrite(wp, "<meta http-equiv=\"Content-Type\" content=\"text/html\">\r\n");
websWrite(wp, "</head></html>\r\n");
websDone(wp, 200);
}
static void sys_script(const char *name)
{
char scmd[64] = "/tmp/";
strncat(scmd, name, sizeof(scmd)-1);
dprintf("run '%s'\n", scmd);
//handle special scripts first
if (strcmp(name,"syscmd.sh")==0) {
char *Cmd_argv[] = { "/bin/sh", "-c", SystemCmd, NULL };
if (strcmp(SystemCmd, "")==0)
Cmd_argv[2] = "echo None";
_eval(Cmd_argv, ">/tmp/syscmd.log", 10, NULL);
*SystemCmd = 0;
} else if (strcmp(name, "syslog.sh")==0) {
// to nothing
} else if (strcmp(name, "printer.sh")==0) {
readPrnID();
} else if (strcmp(name, "lpr_remove")==0) {
kill_pidfile_s("/var/run/lpdparent.pid", SIGUSR2);
eval("killall","p910nd");
}
#ifdef __CONFIG_MADWIMAX__
else if (strcmp(name, "madwimax.sh")==0) {
// update status of madwimax
kill_pidfile_s("/var/run/madwimax0.pid", SIGUSR1);
usleep(100);
}
#endif
else if (strcmp(name,"dnsmasq.sh")==0) {
kill_pidfile_s("/var/run/dnsmasq.pid", SIGALRM);
sleep(1);
} else if (strcmp(name,"iptable.sh")==0) {
// TODO
} else if (strcmp(name,"route.sh")==0) {
// TODO
} else if (strcmp(name,"radio_disable")==0) {
eval("wl", "radio", "off");
} else if (strcmp(name,"radio_enable")==0) {
eval("wl", "radio", "on");
} else if (strcmp(name,"dhcpc_renew")==0) {
sys_renew();
sleep(1);
} else if (strcmp(name,"dhcpc_release")==0) {
sys_release();
sleep(1);
} else if (strcmp(name,"eject-usb.sh")==0) {
eval("rmstorage");
} else if (strcmp(name,"ddnsclient")==0) {
eval("start_ddns", "1");
} else if (strcmp(name,"ddnsregister")==0) {
eval("start_ddns", "2");
} else if (strstr(scmd, " ")==0) {
// no parameter, run script with eval
eval(scmd);
} else
system(scmd);
}
/*
* Byte 0~15: Hardware compatible list( 4 pairs)
* 0 + x*4: Minimum Hardware Major
* 1 + x*4: Minimum Hardware Minor
* 2 + x*4: Maximum Hardware Major
* 3 + x*4: Maximum Hardware Minor
* x = 0~3
*/
static char checkVersion(const unsigned char *clist, unsigned char major, unsigned char minor)
{
#define VINT(ver) (((((ver)[0])&0xff)<<8)|(((ver)[1])&0xff))
int i;
unsigned int ver, min, max;
ver = (major << 8) | minor;
if (ver == 0)
return 1;
for (i = 0; VINT(clist+i) && i < MAX_HW_COUNT*4; i += 4) {
min = VINT(clist+i);
max = VINT(clist+i+2);
//dprintf("check 0: %x <= %x <= %x\n", min, ver, max);
if (ver >= min && (max == 0 || ver <= max))
return 1;
}
return (i == 0);
}
static void websScan(const char *str)
{
unsigned int i, flag;
char *v1, *v2, *v3, *sp;
char groupid[64];
char value[MAX_LINE_SIZE];
char name[MAX_LINE_SIZE];
v1 = strchr(str, '?');
i = 0;
flag = 0;
while (v1!=NULL) {
v2 = strchr(v1+1, '=');
v3 = strchr(v1+1, '&');
if (v3!=NULL) {
strncpy(value, v2+1, v3-v2-1);
value[v3-v2-1] = 0;
} else {
strcpy(value, v2+1);
}
strncpy(name, v1+1, v2-v1-1);
name[v2-v1-1] = 0;
/*dprintf("Value: %s %s\n", name, value);*/
if (v2 != NULL && ((sp = strchr(v1+1, ' ')) == NULL || (sp > v2))) {
if (flag && strncmp(v1+1, groupid, strlen(groupid))==0) {
delMap[i] = atoi(value);
//dprintf("Del Scan : %x\n", delMap[i]);
if (delMap[i]==-1) break;
i++;
} else if (strncmp(v1+1,"group_id", 8)==0) {
sprintf(groupid, "%s_s", value);
flag = 1;
}
}
v1 = strchr(v1+1, '&');
}
delMap[i] = -1;
return;
}
static int websWriteEscape(webs_t wp, char *buf)
{
static const char *escapes = "\"&'<>";
char *c;
int ret = 0;
for (c = buf; *c; c++) {
if (strchr(escapes, *c))
ret += websWrite(wp, "&#%d;", *c);
else
ret += websWrite(wp, "%c", *c);
}
return ret;
}
/*
* Example:
* lan_ipaddr=192.168.1.1
* <% nvram_get("lan_ipaddr"); %> produces "192.168.1.1"
* <% nvram_get("undefined"); %> produces ""
*/
static int
ej_nvram_get(int eid, webs_t wp, int argc, char **argv)
{
const char *name;
if (ejArgs(argc, argv, "%s", &name) < 1) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
return websWriteEscape(wp, nvram_safe_get(name));
}
/*
* Example:
* wan_proto=dhcp
* <% nvram_match("wan_proto", "dhcp", "selected"); %> produces "selected"
* <% nvram_match("wan_proto", "static", "selected"); %> does not produce
*/
static int
ej_nvram_match(int eid, webs_t wp, int argc, char **argv)
{
const char *name, *match, *output;
if (ejArgs(argc, argv, "%s %s %s", &name, &match, &output) < 3) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
if (nvram_match(name, match))
return websWrite(wp, output);
return 0;
}
static int
ej_nvram_double_match(int eid, webs_t wp, int argc, char **argv)
{
const char *name, *match, *name2, *match2, *output;
if (ejArgs(argc, argv, "%s %s %s %s %s", &name, &match, &name2, &match2, &output) < 5) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
if (nvram_match(name, match) && nvram_match(name2, match2))
return websWrite(wp, output);
return 0;
}
static int
ej_nvram_match_both(int eid, webs_t wp, int argc, char **argv)
{
const char *name, *match, *output, *output_not;
if (ejArgs(argc, argv, "%s %s %s %s", &name, &match, &output, &output_not) < 4) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
if (nvram_match(name, match))
return websWrite(wp, output);
else
return websWrite(wp, output_not);
}
static int
ej_nvram_get_table(int eid, webs_t wp, int argc, char **argv)
{
const char *name;
const struct group_variable *gv;
int i, groupCount, ret, r;
if (ejArgs(argc, argv, "%s", &name) < 1) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
gv = LookupGroup(name);
if (gv == NULL)
return 0;
groupCount = nvram_get_int(gv->counter_name);
if (groupCount==0)
ret = nvram_add_group_table(wp, gv, -1);
else {
ret = 0;
for (i=0; i<groupCount; i++) {
r = nvram_add_group_table(wp, gv, i);
if (r != 0)
ret += r;
else
break;
}
}
return ret;
}
/*
* Example:
* wan_proto=dhcp;dns
* <% nvram_match_list("wan_proto", "dhcp", "selected", 0); %> produces "selected"
* <% nvram_match_list("wan_proto", "static", "selected", 1); %> does not produce
*/
static int
ej_nvram_match_list(int eid, webs_t wp, int argc, char **argv)
{
const char *name, *match, *output;
int which;
if (ejArgs(argc, argv, "%s %s %s %d", &name, &match, &output, &which) < 4) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
if (nvram_match_list(name, match, which))
return websWrite(wp, output);
else
return 0;
}
static int
ej_select_channel(int eid, webs_t wp, int argc, char **argv)
{
char chstr[32];
int ret = 0;
int i, channel, channels[32];
channel = nvram_get_int("wl_channel");
if (!wl_channels_in_country(nvram_safe_get("wl_country_code"), channels)) {
wl_channels_in_country_asus(nvram_safe_get("wl_country_code"), channels);
}
i = 0;
while (channels[i]!=-1) {
if (channels[i] == 0)
strcpy(chstr, "Auto");
else
sprintf(chstr, "%d", channels[i]);
if (channel==channels[i])
ret += websWrite(wp, "<option value=\"%d\" selected>%s</option>", channels[i], chstr);
else
ret += websWrite(wp, "<option value=\"%d\">%s</option>", channels[i], chstr);
i++;
}
return ret;
}
static int
ej_urlcache(int eid, webs_t wp, int argc, char **argv)
{
int flag;
if (strcmp(urlcache, "Main_Operation.asp")==0)
flag = 2;
else if (strcmp(urlcache, "Main_Content.asp")==0)
flag = 1;
else
flag = 0;
dprintf("Url:%s %d\n", urlcache, flag);
if (nvram_match("wan_route_x", "IP_Routed")) {
if (nvram_match("wan_nat_x", "1")) {
/* disable to redirect page */
if (redirect) {
websWrite(wp, "Basic_GOperation_Content.asp");
redirect=0;
} else {
switch(flag) {
case 2:
websWrite(wp, "Basic_GOperation_Content.asp");
break;
case 1:
websWrite(wp, "Basic_HomeGateway_SaveRestart.asp");
break;
default:
websWrite(wp, "Main_Index_HomeGateway.asp");
break;
}
}
} else {
/* disable to redirect page */
if (redirect) {
websWrite(wp, "Basic_ROperation_Content.asp");
redirect=0;
} else {
switch(flag) {
case 2:
websWrite(wp, "Basic_ROperation_Content.asp");
break;
case 1:
websWrite(wp, "Basic_Router_SaveRestart.asp");
break;
default:
websWrite(wp, "Main_Index_Router.asp");
break;
}
}
}
} else {
switch(flag) {
case 2:
websWrite(wp, "Basic_AOperation_Content.asp");
break;
case 1:
websWrite(wp, "Basic_AccessPoint_SaveRestart.asp");
break;
default:
websWrite(wp, "Main_Index_AccessPoint.asp");
break;
}
}
strcpy(urlcache, "");
return flag;
}
/* Report sys up time */
static int
ej_uptime(int eid, webs_t wp, int argc, char **argv)
{
char buf[MAX_LINE_SIZE];
char *str = file2str("/proc/uptime");
time_t tm;
struct tm tm1;
int len;
time(&tm);
setenv("TZ", nvram_safe_get("time_zone"), 1);
memcpy(&tm1, localtime(&tm), sizeof(tm1));
len = strftime(buf, sizeof(buf)-1, "%a, %d %b %Y %H:%M:%S %z", &tm1);
if (str != NULL) {
unsigned long up = atol(str);
free(str);
strcpy(buf + len, " ("); len += 2;
len += reltime(up, buf + len);
strcpy(buf + len, " since boot)");
}
return websWrite(wp, buf);
}
static int
ej_model(int eid, webs_t wp, int argc, char **argv)
{
char buf[MAX_LINE_SIZE];
int mode;
if (ejArgs(argc, argv, "%d", &mode) < 1)
mode = 0;
get_model_t(buf, sizeof(buf), mode);
return websWrite(wp, buf);
}
static int
dump_file(webs_t wp, const char *filename)
{
int fd, n;
char buf[2048 + 1]; /* aaa2ppp: imho size must be (n * minimum_io_size + 1) */
int ret = 0;
fd = open(filename, O_RDONLY);
if (fd == -1) {
ret = -errno;
dprintf("Can't open %s\n", filename);
return 0; /* aaa2ppp: or return ret ? */
}
while ((n = read(fd, buf, (sizeof buf) - 1)) > 0) {
ret += websWriteData(wp, buf, n);
}
close(fd);
websDone(wp, 200);
return ret;
}
static int
ej_dump(int eid, webs_t wp, int argc, char **argv)
{
char filename[64] = "/tmp/";
const char *file, *script;
int ret;
if (ejArgs(argc, argv, "%s %s", &file, &script) < 2) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
dprintf("Script : %s, File: %s\n", script, file);
// run script first to update some status
if (strcmp(script, "") != 0)
sys_script(script);
ret = 0;
if (strcmp(file, "wlan11a.log")==0)
return ej_wl_status(eid, wp, 0, NULL);
else if (strcmp(file, "iptable.log")==0)
return ej_nat_table(eid, wp, 0, NULL);
else if (strcmp(file, "route.log")==0)
return ej_route_table(eid, wp, 0, NULL);
else if (strcmp(file, "syslog.log") == 0)
ret += dump_file(wp, "/tmp/syslog.log-1");
else if (strcmp(file, "ssh_keys.log") == 0)
return dump_file(wp, SSH_AUTHORIZED_KEYS);
strncat(filename, file, sizeof(filename)-1);
ret += dump_file(wp, filename);
return ret;
}
static int
ej_load(int eid, webs_t wp, int argc, char **argv)
{
char *script;
if (ejArgs(argc, argv, "%s", &script) < 1) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
sys_script(script);
return (websWrite(wp, "%s", ""));
}
static int
ej_include(int eid, webs_t wp, int argc, char **argv)
{
char *file;
if (ejArgs(argc, argv, "%s", &file) < 1) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
do_ej(file, wp);
return 0;
}
static void
validate_cgi(webs_t wp, int sid, int groupFlag)
{
const struct variable *v;
char *value;
char name[64];
/* Validate and set variables in table order */
for (v = GetVariables(sid); v->name != NULL; v++) {
//dprintf("Name: %s %d\n", v->name, sid);
strcpy(name, v->name);
if ((value = websGetVar(wp, name, NULL))) {
nvram_set(v->name, value);
}
}
}
static char *svc_pop_list(char *value)
{
char *v;
if (value == NULL || (*value) == '\0')
return NULL;
v = strchr(value, ';');
if (v != NULL)
{
*v = '\0';
return value;
}
return NULL;
}
/* aaa2ppp: I would like so flashfs_update() to be */
#define flashfs_update() (nvram_set("flashfs_updated", "1"))
static int
flashfs_commit(void)
{
if (nvram_match("flashfs_updated", "1")) {
nvram_unset("flashfs_updated"); /* aaa2ppp: It must be unset on start device too! */
eval("flashfs", "save");
eval("flashfs", "commit");
eval("flashfs", "enable");
}
return 0;
}
static int
update_authorized_keys(webs_t wp)
{
char *flag, *keys;
struct stf_opts f_opts;
int f_flags;
int ret = 0;
/* if user not wants to change keys then do nothing */
flag = websGetVar(wp, "ssh_keys_change", "");
if (strcmp (flag, "1") != 0)
return 0;
/* if keys is't changed then do nothing */
flag = websGetVar(wp, "ssh_keys_changed", "");
if (strcmp(flag, "1") != 0)
return 0;
keys = websGetVar(wp, "ssh_keys", NULL);
/* if keys not defined then do nothing */
if (keys == NULL)
return 0;
/* if keys is empty then delete authorized_keys file */
if (*keys == '\0') {
if (unlink(SSH_AUTHORIZED_KEYS) == 0)
ret = flashfs_update();
return ret;
}
/* else [re]write authorized_keys file */
mkdir(ROOT_SSH_DIR, 0700);
memset(&f_opts, 0, sizeof f_opts);
f_flags = STF_CHMOD|STF_TRIM|STF_SKIP_0LINE;
f_opts.mode = 0600;
if ((ret = save_text_to_file(keys, SSH_AUTHORIZED_KEYS, f_flags, &f_opts)) == 0)
ret = flashfs_update();
return ret;
}
static int
apply_cgi(webs_t wp, const char *url, const char *path, const char *query)
{
const char *value, *value1;
const char *current_url, *next_url;
const char *script, *groupName;
char urlStr[128];
// char *host;
if (!query)
goto footer;
urlStr[0] = 0;
value = websGetVar(wp, "action", "");
//if (strcmp(value, "")==0)
value = websGetVar(wp, "action_mode","");
next_host = websGetVar(wp, "next_host", "");
current_url = websGetVar(wp, "current_page", "");
next_url = websGetVar(wp, "next_page", "");
script = websGetVar(wp, "action_script","");
dprintf("Apply: %s %s %s %s\n", value, current_url, next_url, websGetVar(wp, "group_id", ""));
if ( !strcmp (current_url, "Advanced_Services_Content.asp") &&
(!strcmp (value, " Apply ") || !strcmp (value, " Finish ")) ) {
/* aaa2ppp: How to handle an error here? */
update_authorized_keys(wp);
}
if (!strcmp(value," Refresh ")) {
strcpy(SystemCmd, websGetVar(wp,"SystemCmd",""));
websRedirect(wp, current_url);
return 0;
} else if (!strcmp(value," Clear ")) {
// current only syslog implement this button
unlink("/tmp/syslog.log-1");
unlink("/tmp/syslog.log");
websRedirect(wp, current_url);
return 0;
} else if (!strcmp(value,"NEXT")) {
websRedirect(wp, next_url);
return 0;
} else if (!strcmp(value, "Save&Restart ")) {
/*action = ACTION_SAVE_RESTART;*/
/*websRedirect(wp, "Restarting.asp");*/
dump_file(wp, "Restarting.asp");
nvram_set("x_Setting", "1");
sys_commit();
sys_reboot();
return (0);
} else if(!strcmp(value, "Restore")) {
/* action = ACTION_RESET_DEFAULT;*/
/* websRedirect(wp, "Restarting.asp");*/
dump_file(wp, "Restarting.asp");
sys_default();
sys_reboot();
return (0);
} else if (!strcmp(value, "WlanUpdate ")) {
/*action = ACTION_SAVE_RESTART;*/
/*websRedirect(wp, "Restarting.asp");*/
if (strcmp(script, "")!=0) {
system(script);
}
dump_file(wp, "Restarting.asp");
sys_reboot();
return (0);
} else {
int sid;
char *sid_list;
const char *serviceId;
sid_list = websGetVar(wp, "sid_list", "");
while ((serviceId = svc_pop_list(sid_list))) {
sid_list += strlen(serviceId)+1;
sid = LookupServiceId(serviceId);
if (sid == -1)
continue;
if (!strcmp(value, " Restore ")) {
//sys_restore(serviceId);
} else if(!strcmp(value, " Save ") ||
!strcmp(value, " Apply ")) {
validate_cgi(wp, sid, TRUE);
} else if(!strcmp(value, "Set") ||
!strcmp(value, "Unset") ||
!strcmp(value, "Update") ||
!strcmp(value, "Eject")) {
validate_cgi(wp, sid, TRUE);
} else if(!strcmp(value," Finish ")) {
validate_cgi(wp, sid, TRUE);
} else {
char groupId[64];
dprintf("group ?\n");
strcpy(groupId, websGetVar(wp, "group_id", ""));
if ((value1 = websGetVar(wp, "action_mode", NULL))) {
groupName = groupId;
if (!strncmp(value1, " Delete ", 8)) {
apply_cgi_group(wp, sid, groupName, GROUP_FLAG_DELETE);
} else if (!strncmp(value1, " Add ", 5)) {
apply_cgi_group(wp, sid, groupName, GROUP_FLAG_ADD);
} else if (!strncmp(value1, " Del ", 5)) {
apply_cgi_group(wp, sid, groupName, GROUP_FLAG_REMOVE);
} else if (!strncmp(value1, " Refresh ", 9)) {
apply_cgi_group(wp, sid, groupName, GROUP_FLAG_REFRESH);
}
sprintf(urlStr, "%s#%s", current_url, groupName);
validate_cgi(wp, sid, FALSE);
break;
}
}
}
/* dprintf("apply????\n"); */
/* Add for EMI Test page */
if (strcmp(script, "")!=0) {
sys_script(script);
}
if (!strcmp(value, " Save ")) {
strcpy(urlcache, next_url);
websRedirect(wp, next_url);
} else if (!strcmp(value, " Apply ")) {
strcpy(urlcache, current_url);
websRedirect(wp, current_url);
} else if(!strcmp(value, " Finish ")) {
websRedirect(wp, "SaveRestart.asp");
} else if (urlStr[0]==0) {
websRedirect(wp, current_url);
} else
websRedirect(wp, urlStr);
dprintf("apply ok\n");
return 0;
}
footer:
websError(wp, 400, "Bad Request\n");
websDone(wp, 400);
#ifndef FLASH_EMULATOR
if (action == RESTART)
sys_restart();
else if (action == REBOOT)
sys_reboot();
#endif
return 1;
}
int
save_text_to_file(char *text, char *file, int flags, struct stf_opts *opts)
{
int ret = 0;
char real_path[PATH_MAX];
char temp_file[PATH_MAX];
int fd;
struct stat sb;
mode_t mode;
uid_t uid;
gid_t gid;
FILE *fs;
size_t len;
char *line, *line_end;
int i;
const char *eol = "\n";
/* get real path of file if exists */
if (realpath(file, real_path) != NULL)
file = real_path;
/* Make temp-file in the same directory */
len = PATH_MAX-1;
temp_file[len] = '\0';
strncpy(temp_file, file, len);
len = PATH_MAX-1 - strlen(temp_file);
strncat(temp_file, ".XXXXXX", len);
fd = mkstemp(temp_file);
if (fd == -1) {
ret = errno;
dprintf ("%s: Can't make temp file %s\n", __func__, temp_file);
return ret;
}
/* mode/uid/gid by default */
mode = 0600;
uid = geteuid();
gid = getegid();
/* mode/uid/gid from options */
if (opts != NULL) {
if (flags & STF_USE_MODE) mode = opts->mode;
if (flags & STF_USE_UID) uid = opts->uid;
if (flags & STF_USE_GID) gid = opts->gid;
}
/* mode/uid/gidif if old version of file is exists */
if ((flags & STF_INHERIT_PERM) && stat(file, &sb) == 0) {
if (flags & STF_INHERIT_MODE) mode = sb.st_mode;
if (flags & STF_INHERIT_UID) uid = sb.st_uid;
if (flags & STF_INHERIT_GID) gid = sb.st_gid;
}
/* chmod */
if (/*(flags & STF_CHMOD) &&*/ fchmod(fd, mode) == -1) {
ret = errno;
dprintf("%s: Can't chmod %04o for %s\n", __func__, mode, temp_file);
goto fail;
}
/* chown/chgrp */
if ((flags & STF_CHOWN) && fchown(fd, uid, gid) == -1) {
ret = errno;
dprintf ("%s: Can't chown %d:%d for %s\n", __func__, uid, gid, temp_file);
if (ret != EPERM || (flags & STF_CHOWN_PERM_ERROR))
goto fail;
ret = 0;
}
/* Save new version to temp-file */
fs = fdopen(fd, "w");
if (fs == NULL) {
ret = errno;
dprintf("%s: Can't fdopen for %s\n", __func__, temp_file);
goto fail;
}
if ((flags & STF_USE_EOL) && opts != NULL)
eol = opts->eol;
for (line = line_end = text, i = 1; line_end != NULL && *line != '\0'; line = line_end + 1) {
/* search end of line */
line_end = strchr(line, '\n');
len = (line_end == NULL) ? strlen(line) : line_end - line;
/* trim left blanks */
if (flags & STF_LTRIM)
for (; len > 0 && isspace(line[0]); len--, line++);