[ba41e6]: osoft.c Maximize Restore History

Download this file

osoft.c    852 lines (725 with data), 27.5 kB

  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
#include <stdio.h>
#include <stdlib.h>
#include <libconfig.h>
#include <string.h>
#include <regex.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pwd.h>
#include <cgi.h>
#include "osoft.h"
int language = GERMAN;
/*
* Print escaped html characters
*/
/* Not Tested
void print_html_escaped(char* str)
{
while(*str)
{
if( *str >= 0 && *str <= 127)
{
putchar(*str);
} else {
printf("&#%d",(unsigned char) *str);
}
str++;
};
}
*/
/*
* Reads the settings from file into the settings array
*/
int readSettingsFromFile(config_t cfg, struct configSetting* settings) {
const char *stringResult;
char str[255];
int boolResult;
int intResult;
int i;
config_setting_t *setting;
//insert settings from file into structs
for (i = 0; i < NUMBEROFSETTINGS; i++) {
setting = config_lookup(&cfg, settings[i].parent);
if (setting != NULL) {
switch(settings[i].dataType) {
case DATATYPE_BOOL:
if (config_setting_lookup_bool(setting, settings[i].internName, &boolResult) == CONFIG_TRUE) {
if (boolResult == 0) {
settings[i].value = "FALSE";
} else {
settings[i].value = "TRUE";
}
} else {
printf("<p class = 'error'>Setting '%s' was not found</p>", settings[i].internName);
}
break;
case DATATYPE_INT:
if (config_setting_lookup_int(setting, settings[i].internName, &intResult) == CONFIG_TRUE) {
sprintf(str, "%i", intResult);
settings[i].value = strdup(str);
} else {
printf("<p class = 'error'>Setting '%s' was not found</p>", settings[i].internName);
}
break;
case DATATYPE_STRING:
if (config_setting_lookup_string(setting, settings[i].internName, &stringResult) == CONFIG_TRUE) {
settings[i].value = strdup(stringResult);
} else {
printf("<p class = 'error'>Setting '%s' was not found</p>", settings[i].internName);
}
default:
break;
}
} else {
printf("<p class = 'error'>Settinggroup '%s' was not found!</p>", settings[i].parent);
}
}
return 1;
}
/*
* Opens the config file and
*/
int readFile(config_t* cfg) {
if(!config_read_file(cfg, CONFIGFILENAME))
{
printf("<p class='error'>CONFIG FILE '%s' NOT FOUND!</p>", CONFIGFILENAME);
config_destroy(cfg);
return 0;
}
return 1;
}
/*
* Writes the HTML header
*/
void writeHeader() {
printf("\n \
\n <!DOCTYPE html> \
\n <html> \
\n <head> \
\n <title>Oggstreamer - Settings Configuration</title> \
\n <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'> \
\n <style type = 'text/css'> \
\n p, div, body, h1 { \
\n margin: 0; \
\n padding: 0; \
\n }\
\n #wrapper { \
\n width: 1024px; \
\n margin: 0 auto; \
\n } \
\n #header { \
\n background-color: #aaa; \
\n height: 186px; \
\n border-top-left-radius: 25px;\
\n overflow: hidden;\
\n border-top-right-radius: 25px;\
\n } \
\n #menu { \
\n background-color: #ccc; \
\n position: absolute; \
\n height: 1400px; \
\n width: 180px; \
\n } \
\n .content { \
\n background-color: #eee; \
\n height: 1360px; \
\n position: relative; \
\n left: 180px; \
\n width:804px; \
\n padding: 20px ; \
\n } \
\n .error { \
\n color: #ff0000; \
\n } \
\n .worked { \
\n color: #00ff00; \
\n } \
\n label { \
\n display: block; \
\n float: left; \
\n width: 150px; \
\n } \
\n input, select,input[type='file'] { \
\n float: left; \
\n width: 200px; \
\n margin-right: 10px; \
\n padding-left:5px;\
\n border-radius:12px;\
\n } \
\n select { \
\n margin-right: 15px; \
\n } \
\n fieldset { \
\n margin-bottom: 10px; \
\n border-radius:15px;\
\n } \
\n legend{font-weight:bold;}\
\n ul { \
\n list-style:none; \
\n }\
\n input[type='submit']:hover, input[type='file']:hover{background-color:black;color:white;}\
\n input[type='checkbox']{margin-right: 15px!important;}\
\n .link-left{\
\n margin-top:10px;margin-left:25px;\
\n float:left;width:200px;color:black;text-decoration:none;font-size:17px;font-weight:bold;}\
\n ul, li{float:left;margin:0px;padding:0px;}\
\n #ogg{text-align:center;font-weight:bold;font-size:48px;font-family:'Helvetica Neue', Arial, Helvetica, 'Nimbus Sans L', sans-serif;margin-top:20px;}\
\n #ogg2{text-align:center;font-size:20px;font-family:'Helvetica Neue', Arial, Helvetica, 'Nimbus Sans L', sans-serif;margin:12px;}\
\n .link-left:hover{ font-size:19px;}\
\n </style>");
printf("<script> \
\n function checkRegex(regex, id, errorMessage) { \
\n var pattern = new RegExp(regex); \
\n if (!pattern.test(document.getElementById(id).value)) { \
\n if (document.getElementById(id + 'error') == null) { \
\n var newSpan = document.createElement('span'); \
\n var add = document.createTextNode('errorMessage'); \
\n newSpan.id = id + 'error'; \
\n newSpan.appendChild(add); \
\n document.getElementById(id).parentNode.insertBefore(newSpan, document.getElementById(id).nextSibling); \
\n document.getElementById(id+'error').className = 'error'; \
\n } \
\n } else { \
\n if (document.getElementById(id + 'error') != null) { \
\n document.getElementById(id + 'error').parentNode.removeChild(document.getElementById(id + 'error')); \
\n } \
\n } \
\n } \
\n function setSettings() { \
\n document.getElementById('settings').setAttribute('style', 'display:block'); \
\n document.getElementById('security').setAttribute('style', 'display:none'); \
\n document.getElementById('restart').setAttribute('style', 'display:none'); \
\n document.getElementById('firmwareupdate').setAttribute('style', 'display:none'); \
\n } \
\n function setSecurity() { \
\n document.getElementById('settings').setAttribute('style', 'display:none'); \
\n document.getElementById('security').setAttribute('style', 'display:block'); \
\n document.getElementById('firmwareupdate').setAttribute('style', 'display:none'); \
\n document.getElementById('restart').setAttribute('style', 'display:none'); \
\n } \
\n function setFirmwareupdate() { \
\n document.getElementById('settings').setAttribute('style', 'display:none'); \
\n document.getElementById('security').setAttribute('style', 'display:none'); \
\n document.getElementById('firmwareupdate').setAttribute('style', 'display:block'); \
\n document.getElementById('restart').setAttribute('style', 'display:none'); \
\n } \
\n function setRestart() { \
\n document.getElementById('settings').setAttribute('style', 'display:none'); \
\n document.getElementById('security').setAttribute('style', 'display:none'); \
\n document.getElementById('firmwareupdate').setAttribute('style', 'display:none'); \
\n document.getElementById('restart').setAttribute('style', 'display:block'); \
\n } \
\n function checkPassword() { \
\n var password1 = document.getElementById('newpassword1').value; \
\n var password2 = document.getElementById('newpassword2').value; \
\n var statusElement = document.getElementById('passwordStatus'); \
\n if (password1 != password2) { \
\n statusElement.innerHTML = 'Please enter 2 same passwords!'; \
\n statusElement.className = 'error'; \
\n } else { \
\n statusElement.innerHTML = 'Match!'; \
\n statusElement.className = 'worked'; \
\n } \
\n } \
\n function initializeSelectFields() { \
\n var selectFields = document.getElementsByTagName('select'); \
\n for (i = 0; i < selectFields.length; i++) { \
\n var optionFields = selectFields[i]; \
\n for (y = 0; y < optionFields.length; y++) { \
\n var optionName = optionFields[y].innerHTML; \
\n var element = document.getElementById(optionName + 'div'); \
\n if (element != null) { \
\n console.log(optionFields[y]); \
\n if (optionFields[y].selected) { \
\n element.setAttribute('style', 'display:block'); \
\n } else { \
\n element.setAttribute('style', 'display:none'); \
\n } \
\n } \
\n } \
\n } \
\n } \
\n </script> \
\n </head> \
\n <body> \
\n <div id='wrapper'> \
\n <p id='ogg'>OggStreamer</p>\
\n <p id='ogg2'> An OpenHardware Streaming Device</p>\
\n <div id='header'> \
\n <img src='/header.png'> \
\n </div> \
\n <div id='menu'> \
\n <ul> \
\n <li><a href='#' class='link-left' id='settingsM' onclick='setSettings()'>Settings</a></li> \
\n <li><a href='#' class='link-left' id='securityM' onclick='setSecurity()'>Security</a></li> \
\n <li><a href='#' class='link-left' id='firmwareupdateM' onclick='setFirmwareupdate()'>Firmware<br>Update</a></li> \
\n <li><a href='#' class='link-left' id='restartM' onclick='setRestart()'>Restart</a></li> \
\n </ul> \
\n </div> \
\n <div class='content' id='settings'> \
");
/*
\n <div id='menu'> \
\n <ul> \
\n <li><a href='#' class='link-left' id='settingsM' onclick='setSettings()'>Settings</a></li> \
\n <li><a href='#' class='link-left' id='securityM' onclick='setSecurity()'>Security</a></li> \
\n </ul> \
\n </div> \
\n <div class='content' id='settings'> \
");
*/
// FIXME: Firmware Update is diabled at the Moment / replace div id='menu' above to reenable
}
/*
* Writes the footer of the HTML page
*/
void writeFooter() {
printf("\n \
\n </div> \
\n <div class='content' id='security' style='display:none'><h1>Security-Stuff</h1> \
\n <form action='%s' method='GET'> \
\n <input type='hidden' value='security' name='destination'> \
\n <div><label for='oldpassword'>Old password</label><input type='password' id='oldpassword' name='oldpassword'><div style='clear:both;'></div></div> \
\n <div><label for='newpassword1'>New password</label><input type='password' id='newpassword1' name='newpassword1' onchange='checkPassword()'><div style='clear:both;'></div></div> \
\n <div><label for='newpassword2'>New password again</label><input type='password' id='newpassword2' name='newpassword2' onchange='checkPassword()'><p id='passwordStatus'></p><div style='clear:both;'></div></div> \
\n <input type='submit' value='Change'> \
\n </form> \
\n </div> \
\n <div class='content' id='firmwareupdate' style='display:none'><h1>Firmware-Update</h1> \
\n <form action='%s' method='POST' enctype=\"multipart/form-data\"> \
\n <input type='hidden' value='fileupload' name='destination'> \
\n <div><label for='updatefile'>Update file</label><input name='updatefile' id='updatefile' type='file' size='50' maxlength='%d'><div style='clear:both;'></div></div> \
\n <input type='submit' value='Upload' onClick=\"alert('Wait a few minutes for Upload to finish and restart the OggStreamer for the update!\\nWarten Sie ein paar Minuten und f&uuml;hren Sie einen Restart aus damit das Update gestartet wird!')\"> \
\n </form> \
\n </div> \
\n <div class='content' id='restart' style='display:none'><h1>Restart</h1> \
\n <form action='%s' method='GET'> \
\n <input type='hidden' value='restart' name='destination'> \
\n <input type='submit' value='Restart'> \
\n </form> \
\n </div> \
\n <script>initializeSelectFields();</script> \
\n </body> \
\n </html> \
", CGIFILENAME, CGIFILENAME, MAXUPLOADSIZE, CGIFILENAME);
}
/*
* Writes the settings as HTML-form
*/
void writeHTML(struct configSetting* settings) {
char *pch;
char *dependendPart;
char *parent;
int i = 0;
printf("<h1>Settings</h1>");
printf("<form action='%s' method='GET'>", CGIFILENAME);
parent = settings[0].parent;
printf("<input type='hidden' value='config' name='destination'>");
printf("<fieldset>");
printf("<legend>%s</legend>", settings[0].parent);
for (i = 0; i < NUMBEROFSETTINGS; i++) {
if (strcmp(parent, settings[i].parent) != 0) {
parent = settings[i].parent;
printf("</fieldset>");
printf("<fieldset>");
printf("<legend>%s</legend>", settings[i].parent);
}
if (settings[i].dataType == DATATYPE_BOOL) {
if (strcmp(settings[i].value, "TRUE") == 0) {
printf("\n<div><label for='%s'>%s</label><input type='checkbox' id='%s' name='%s' value='TRUE' checked>", settings[i].id, settings[i].internName, settings[i].id, settings[i].id);
} else {
printf("\n<div><label for='%s'>%s</label><input type='checkbox' id='%s' name='%s' value='TRUE'>", settings[i].id, settings[i].internName, settings[i].id, settings[i].id);
}
} else if (strlen(settings[i].defaultValues) != 0) {
char *string = strdup(settings[i].defaultValues);
pch = strtok(string, ",");
printf("\n<div id='%sdiv'><label for='%s'>%s</label><select id='%s' name='%s' onchange='showHide%s()'>", settings[i].id, settings[i].id, settings[i].internName, settings[i].id, settings[i].id, settings[i].id);
while (pch != NULL) {
if (settings[i].value != NULL && strcmp(pch, settings[i].value) == 0) {
printf("\n<option selected>%s</option>", pch);
} else {
printf("\n<option>%s</option>", pch);
}
pch = strtok (NULL, ",");
}
printf("\n</select>");
printf("\n<script> function showHide%s() {", settings[i].id);
if (strlen(settings[i].dependendChild_ids) != 0) {
printf("\nvar value = document.getElementById('%s').value;", settings[i].id);
char *string = strdup(settings[i].defaultValues);
dependendPart = strtok(string, ",");
while (dependendPart != NULL) {
printf("\nif (value == '%s') {", dependendPart);
printf("\ndocument.getElementById('%sdiv').setAttribute('style', 'display:block');", dependendPart);
printf("\n} else {");
printf("\ndocument.getElementById('%sdiv').setAttribute('style', 'display:none');", dependendPart);
printf("\n}");
dependendPart = strtok (NULL, ",");
}
}
printf("\n}\nwindow.onload = showHide%s; \n</script>", settings[i].id);
} else {
printf("\n<div><label for='%s'>%s</label><input type='text' id='%s' name='%s' value='%s' onchange='checkRegex(\"%s\", \"%s\", \"%s\");'>", settings[i].id, settings[i].internName, settings[i].id, settings[i].id,settings[i].value, settings[i].regex, settings[i].id, settings[i].errorMessage[language]);
/* printf("\n<div><label for='%s'>%s</label><input type='text' id='%s' name='%s' value='%s' onchange='checkRegex(\"%s\", \"%s\", \"", settings[i].id, settings[i].internName, settings[i].id, settings[i].id,settings[i].value, settings[i].regex, settings[i].id);
print_html_escaped(settings[i].errorMessage[language]);
printf("\");'>");
*/
}
printf("<p>%s</p>", settings[i].description[language]);
/* printf("<p>");
print_html_escaped(settings[i].description[language]);
printf("<p>"); */
printf("<div style='clear:both;'></div>");
printf("</div>");
}
printf("</fieldset>");
printf("<input type='submit' value='Save' onClick=\"alert('Restart the OggStreamer for your changes to take effect!\\nF&uuml;hren Sie einen Restart aus damit die &Auml;nderungen wirksam werden!')\">");
printf("</form>");
}
// DEBUG
void printSettings(struct configSetting *settings) {
int i = 0;
for (i=0; i < NUMBEROFSETTINGS; i++) {
printf("\n--------------------------------------------------");
printf("\nSETTING #%i", i);
printf("\nPARENT: %s", settings[i].parent);
printf("\nID: %s", settings[i].id);
printf("\nREGEX: %s", settings[i].regex);
printf("\nINTERNNAME: %s", settings[i].internName);
printf("\nDEFAULTVALUES: %s", settings[i].defaultValues);
printf("\nDATATYPE: %i", settings[i].dataType);
printf("\nVALUE: %s", settings[i].value);
}
}
/*
* Checks if either bitrate or quality are entered
*/
/*
int checkSpecialRequirements(config_t* cfg, config_setting_t *setting, struct configSetting* settings) {
return 1;
}
*/
/*
* Checks if the values that are read (either from file or html) do match with the regular expressions
*/
int checkRegex (struct configSetting* settings) {
regex_t regex;
int reti;
char msgbuf[100];
int ret=1;
int i = 0;
for (i=0; i < NUMBEROFSETTINGS; i++) {
if (regcomp(&regex, settings[i].regex,REG_EXTENDED)!=0) {
printf("Error compiling RE: ('%s') \n", settings[i].regex);
ret=0;
}
reti= regexec(&regex,settings[i].value,0,NULL,0);
if( !reti ){
//just for debbuging
//printf("\n<p class='error'>Setting '%s' (%s) DOES match the regular expression!!!</p>", settings[i].internName, settings[i].value);
//m��sste alles gehen
}
else if( reti == REG_NOMATCH ){
printf("\n<p class='error'>Setting '%s' (%s) does not match the regular expression!-! '%s'</p>", settings[i].internName, settings[i].value, settings[i].regex);
ret=0;
}
else{
regerror(reti, &regex, msgbuf, sizeof(msgbuf));
//fprintf(stderr, "Regex match failed: %s\n", msgbuf);
printf("\n<p class='error'>Setting '%s' (%s) does not match the regular expression! ELSE!!</p>", settings[i].internName, settings[i].value);
ret=0;
}
//struct slre slre;
//struct cap captures[4 + 1];
//printf("<p>Check regex: %s, %s, %i</p>", settings[i].regex, settings[i].value, strlen(settings[i].value));
/*if (!slre_compile(&slre, settings[i].regex)) {
printf("Error compiling RE: %s\n", slre.err_str);
}
else if (!slre_match(&slre, settings[i].value, strlen(settings[i].value), captures)) {
printf("\n<p class='error'>Setting '%s' (%s) does not match the regular expression!</p>", settings[i].internName, settings[i].value);
return 0;
}*/
regfree(&regex);
}
return ret;
}
/*
* Reads the language that is specified in settings file
*/
void readLanguage(config_t* cfg) {
const char *str;
if (config_lookup_string(cfg, "language.language", &str)) {
if (strcmp(str, "german") == 0) {
language = GERMAN;
} else if (strcmp(str, "english") == 0) {
language = ENGLISH;
} else {
printf("<p class='error'>Language '%s' is not supported yet</p>", str);
}
} else {
printf("<p class='error'>Setting 'language' was not found in settings file!</p>");
}
}
/*
* Writes the settings that were read (either from file or HTML) to the config file.
*/
void writeSettingsToFile(config_t *cfg, struct configSetting* settings) {
config_setting_t *setting;
char str[100];
int i = 0;
for (i=0; i < NUMBEROFSETTINGS; i++) {
str[0] = '\0';
strncat (str, settings[i].parent,sizeof(str));
strncat (str, ".",sizeof(str));
strncat (str, settings[i].internName,sizeof(str));
setting = config_lookup(cfg, str);
switch(settings[i].dataType) {
case DATATYPE_BOOL:
config_setting_set_bool(setting, strcmp(settings[i].value, "TRUE") == 0 ? 1 : 0);
break;
case DATATYPE_INT:
config_setting_set_int(setting, atoi(settings[i].value));
break;
case DATATYPE_STRING:
config_setting_set_string(setting, settings[i].value);
break;
}
}
if (config_write_file (cfg, CONFIGFILENAME) == CONFIG_TRUE) {
printf("\n<font size=+1 color=\"green\">SUCCESS</font>");
} else {
printf("\n<font size=+1 color=\"red\">NO SUCCESS</font>");
}
}
char x2c(char *what) {
register char digit;
digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
digit *= 16;
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
return(digit);
}
/** Reduce any %xx escape sequences to the characters they represent. **/
void unescape_url(char *url) {
register int i,j;
for(i=0,j=0; url[j]; ++i,++j) {
if((url[i] = url[j]) == '%') {
url[i] = x2c(&url[j+1]) ;
j+= 2 ;
}
}
url[i] = '\0' ;
}
/** Read the CGI input and place all name/val pairs into list. **/
/** Returns list containing name1, value1, name2, value2, ... , NULL **/
char **getcgivars() {
register int i ;
char *request_method ;
int content_length;
char *cgiinput ;
char **cgivars ;
char **pairlist ;
int paircount ;
char *nvpair ;
char *eqpos ;
/** Depending on the request method, read all CGI input into cgiinput. **/
request_method= getenv("REQUEST_METHOD") ;
if (!strcmp(request_method, "GET") || !strcmp(request_method, "HEAD") ) {
/* Some servers apparently don't provide QUERY_STRING if it's empty, */
/* so avoid strdup()'ing a NULL pointer here. */
char *qs ;
qs= getenv("QUERY_STRING") ;
cgiinput= strdup(qs ? qs : "") ;
}
else if (!strcmp(request_method, "POST")) {
/* strcasecmp() is not supported in Windows-- use strcmpi() instead */
if ( strcasecmp(getenv("CONTENT_TYPE"), "application/x-www-form-urlencoded")) {
//printf("getcgivars(): Unsupported Content-Type!!.\n") ;
// exit(1) ;
}
if ( !(content_length = atoi(getenv("CONTENT_LENGTH"))) ) {
//
printf("<font size=+1 color=\"red\">getcgivars(): No Content-Length was sent with the POST request.\n</font>") ;
exit(1) ;
}
if ( !(cgiinput= (char *) malloc(content_length+1)) ) {
//
printf("<font size=+1 color=\"red\">getcgivars(): Couldn't malloc for cgiinput.\n</font>") ;
exit(1) ;
}
if (!fread(cgiinput, content_length, 1, stdin)) {
//printf("getcgivars(): Couldn't read CGI input from STDIN.\n") ;
//exit(1) ;
}
cgiinput[content_length]='\0' ;
}
else {
printf("<font size=+1 color=\"red\">getcgivars(): Unsupported REQUEST_METHOD.\n</font>") ;
exit(1) ;
}
/** Change all plusses back to spaces. **/
for (i=0; cgiinput[i]; i++) if (cgiinput[i] == '+') cgiinput[i] = ' ' ;
/** First, split on "&" and ";" to extract the name-value pairs into **/
/** pairlist. **/
pairlist= (char **) malloc(256*sizeof(char **)) ;
paircount= 0 ;
nvpair= strtok(cgiinput, "&;") ;
while (nvpair) {
pairlist[paircount++]= strdup(nvpair) ;
if (!(paircount%256))
pairlist= (char **) realloc(pairlist,(paircount+256)*sizeof(char **)) ;
nvpair= strtok(NULL, "&;") ;
}
pairlist[paircount]= 0 ; /* terminate the list with NULL */
/** Then, from the list of pairs, extract the names and values. **/
cgivars= (char **) malloc((paircount*2+1)*sizeof(char **)) ;
for (i= 0; i<paircount; i++) {
if ( (eqpos=strchr(pairlist[i], '=')) ) {
*eqpos= '\0' ;
unescape_url(cgivars[i*2+1]= strdup(eqpos+1)) ;
} else {
unescape_url(cgivars[i*2+1]= strdup("")) ;
}
unescape_url(cgivars[i*2]= strdup(pairlist[i])) ;
}
cgivars[paircount*2]= 0 ; /* terminate the list with NULL */
/** Free anything that needs to be freed. **/
free(cgiinput) ;
for (i=0; pairlist[i]; i++) free(pairlist[i]) ;
free(pairlist) ;
/** Return the list of name-value strings. **/
return cgivars ;
}
/***************** end of the getcgivars() module ********************/
/*
* Handle the passwordchange
*/
void handlePasswordChange(char* oldPassword, char* newPassword1, char* newPassword2) {
struct passwd *user;
char cmdstring[500];
//Password Change Code HERE
//printf("Old password: %s, new password 1: %s, new password 2: %s", oldPassword, newPassword1, newPassword2);
if(strcmp(newPassword1, newPassword2))
printf("<font color=\"red\" size=+1>Passwords don't match!</font>");
else if ((user= getpwnam("root")) == NULL)
printf("<font color=\"red\" size=+1>No such user!</font>");
else if (strcmp(user->pw_passwd, crypt(oldPassword, user->pw_passwd)))
printf("<font color=\"red\" size=+1>Old Password incorrect!</font>");
else if (strlen(newPassword1) < 4)
printf("<font color=\"red\" size=+1>Password too short!</font>");
else
{
cmdstring[0]='\0';
strncat(cmdstring,"/mnt/flash/root/bin/htpasswd_batch root ", 100);
strncat(cmdstring,newPassword1, 100);
strncat(cmdstring," > /mnt/flash/www/cgi-bin/.htpasswd", 100);
system(cmdstring);
system("cp /mnt/flash/www/cgi-bin/.htpasswd /mnt/flash/backups/.htpasswd_backup");
cmdstring[0]='\0';
strncat(cmdstring,"echo root:", 100);
strncat(cmdstring,newPassword1, 100);
strncat(cmdstring," | /mnt/flash/root/bin/busybox chpasswd -m", 100);
system(cmdstring);
system("cp /etc/passwd /etc/passwd.bak");
printf("<font color=\"green\" size=+1>Password changed!</font>");
}
}
void handleFileUpload() {
int len;
len = get_multipart_filelenght();
if (len == 0 ) {
printf("<font color=\"red\" size=+1>File has zero length!</font>");
}
else if ( len > MAXUPLOADSIZE)
{
printf("<font color=\"red\" size=+1>File to big!</font>");
} else {
if ( get_multipart_transmit_status() == FILE_OK )
{
printf("<font color=\"green\" size=+1>File uploaded! %d bytes</font>",len);
} else {
printf("<font color=\"red\" size=+1>Could not create file!</font>");
}
}
}
/***************** end of the getcgivars() module ********************/
void readCgiVars(config_t cfg, struct configSetting* settings) {
int i = 1;
int y = 2;
char** cgivars;
char *request_method;
request_method= getenv("REQUEST_METHOD") ;
if((0!=strcmp(request_method, "GET"))&&(0!=strcmp(request_method, "POST"))){
cgivars = getcgivars();
if (cgivars[0] == 0) {
readSettingsFromFile(cfg, settings);
}
}
if (0==strcmp(request_method, "GET")) {
cgivars = getcgivars();
if (cgivars[0] == 0) {
readSettingsFromFile(cfg, settings);
}
else if (strcmp(cgivars[1],"config") == 0){
for(i = 0; i < NUMBEROFSETTINGS; i++) {
if(settings[i].dataType == DATATYPE_BOOL)
{
settings[i].value = "FALSE";
}
//Just for debbuging printf("settings: %s i:%s y:%s",settings[i].id,cgivars[i],cgivars[y]);
if (strcmp(settings[i].id, cgivars[y]) != 0) {
if (strcmp("quality", cgivars[y]) == 0) {
settings[i].value = "1";
} else if (strcmp("bitrate", cgivars[y]) == 0) {
settings[i].value = "128";
} else {
//settings[i].value = "FALSE";
}
} else {
settings[i].value = cgivars[y+1];
y += 2;
}
}
//int checkSpecial1 = checkSpecialRequirements(&cfg, setting, settings);
int checkRegex1 = checkRegex(settings);
//if (checkSpecial1 != 0 && checkRegex1 != 0) {
if ( checkRegex1 != 0) {
writeSettingsToFile(&cfg, settings);
system(UPDATECONFIGSCRIPT);
}
} else if (strcmp(cgivars[1], "security") == 0) {
handlePasswordChange(cgivars[3], cgivars[5], cgivars[7]);
readSettingsFromFile(cfg, settings);
}
else if (strcmp(cgivars[1], "restart") == 0) {
system(RESTART_CMD);
}
}
else // if (strcmp(cgivars[1], "fileupload") == 0)
{
handleFileUpload();
readSettingsFromFile(cfg, settings);
}
}
int main() {
struct configSetting settings[NUMBEROFSETTINGS];
config_t cfg;
memset(&settings,0,sizeof(struct configSetting)*NUMBEROFSETTINGS);
config_init(&cfg);
set_multipart_filename( UPLOADFILENAME );
cgi_init();
cgi_process_form();
//cgi_init_headers();
printf("Content-Type:text/html;charset=iso-8859-1\n\n");
writeHeader();
if (readFile(&cfg) == 1) {
readLanguage(&cfg);
prepareSettingsArray(settings);
readCgiVars(cfg, settings);
writeHTML(settings);
config_destroy(&cfg);
}
writeFooter();
cgi_end();
return(EXIT_SUCCESS);
}