Repository navigation
Expand file tree
/
Copy pathhttpd.c
More file actions
2205 lines (1970 loc) · 55.6 KB
/
Copy pathhttpd.c
File metadata and controls
2205 lines (1970 loc) · 55.6 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
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
/* milli_httpd - pretty small HTTP server
** A combination of
** micro_httpd - really small HTTP server
** and
** mini_httpd - small HTTP server
**
** Copyright 1999,2000 by Jef Poskanzer <jef@acme.com>.
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE.
*/
#define _GNU_SOURCE
#include "modules/combine.c"
static int wfsendfile(int fd, off_t offset, size_t nbytes, webs_t wp);
static char *wfgets(char *buf, int len, webs_t fp, int *eof);
static int wfprintf(webs_t fp, char *fmt, ...);
static size_t wfwrite(void *buf, size_t size, size_t n, webs_t fp);
static size_t wfread(void *buf, size_t size, size_t n, webs_t fp);
static int wfclose(webs_t fp);
static int wfflush(webs_t fp);
#ifndef VALIDSOURCE
#ifndef VISUALSOURCE
int wfputs(char *buf, webs_t fp);
#endif
#endif
/* Basic authorization userid and passwd limit */
static void send_authenticate(webs_t conn_fp);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <getopt.h>
#include <stdarg.h>
#include <syslog.h>
#include <cy_conf.h>
#include "httpd.h"
#include <bcmnvram.h>
#include <code_pattern.h>
#include <utils.h>
#include <shutils.h>
#include <sys/time.h>
#include <signal.h>
#include <airbag.h>
#include <sys/wait.h>
#include <sys/sendfile.h>
#include <wlutils.h>
#ifndef HAVE_MICRO
#include <semaphore.h>
#endif
#ifdef HAVE_OPENSSL
#include <openssl/ssl.h>
#endif
#ifdef HAVE_MATRIXSSL
#include <matrixSsl.h>
#include <matrixssl_xface.h>
#endif
#ifdef HAVE_POLARSSL
#include "polarssl/entropy.h"
#include <polarssl/ctr_drbg.h>
#include <polarssl/certs.h>
#include <polarssl/x509.h>
#include <polarssl/ssl.h>
#include <polarssl/net.h>
//#include <xyssl_xface.h>
#endif
#ifdef __UCLIBC__
#include <error.h>
#endif
#include <sys/signal.h>
#include <ucontext.h>
#ifdef HAVE_IAS
#include <sys/sysinfo.h>
#endif
#ifndef HAVE_MICRO
#include <pthread.h>
#endif
#include <crypt.h>
#define SERVER_NAME "httpd"
#define PROTOCOL "HTTP/1.1"
#define RFC1123FMT "%a, %d %b %Y %H:%M:%S GMT"
#define TIMEOUT 5
#ifdef HAVE_IPV6
#define USE_IPV6 1
#endif
/* A multi-family sockaddr. */
typedef union {
struct sockaddr sa;
struct sockaddr_in sa_in;
#ifdef USE_IPV6
struct sockaddr_in6 sa_in6;
struct sockaddr_storage sa_stor;
#endif /* USE_IPV6 */
} usockaddr;
/* Globals. */
#ifdef FILTER_DEBUG
FILE *debout;
#endif
#if defined(TCP_CORK) && !defined(TCP_NOPUSH)
#define TCP_NOPUSH TCP_CORK
/* (Linux's TCP_CORK is basically the same as BSD's TCP_NOPUSH.) */
#endif
#if defined(HAVE_OPENSSL) || defined(HAVE_MATRIXSSL) || defined(HAVE_POLARSSL)
#define DEFAULT_HTTPS_PORT 443
#define CERT_FILE "/etc/cert.pem"
#define KEY_FILE "/etc/key.pem"
#endif
#ifdef HAVE_POLARSSL
static int my_ciphers[] = {
TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
TLS_RSA_WITH_AES_256_CBC_SHA,
TLS_RSA_WITH_RC4_128_MD5,
TLS_RSA_WITH_RC4_128_SHA,
TLS_RSA_WITH_AES_128_CBC_SHA256,
TLS_RSA_WITH_AES_256_CBC_SHA256,
TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,
TLS_RSA_WITH_AES_128_GCM_SHA256,
TLS_RSA_WITH_AES_256_GCM_SHA384,
0
};
static char *dhm_P =
"E4004C1F94182000103D883A448B3F80"
"2CE4B44A83301270002C20D0321CFD00"
"11CCEF784C26A400F43DFB901BCA7538" "F2C6B176001CF5A0FD16D2C48B1D0C1C" "F6AC8E1DA6BCC3B4E1F96B0564965300" "FFA1D0B601EB2800F489AA512C4B248C" "01F76949A60BB7F00A40B1EAB64BDD48" "E8A700D60B7F1200FA8E77B0A979DABF";
static char *dhm_G = "4";
//unsigned char session_table[SSL_SESSION_TBL_LEN];
#endif
#ifdef HAVE_REGISTER
static int registered = -1;
static int registered_real = -1;
#endif
#ifdef HAVE_SUPERCHANNEL
static int superchannel = -1;
#endif
#define DEFAULT_HTTP_PORT 80
static char pid_file[80];
static char *server_dir = NULL;
extern char *get_mac_from_ip(char *mac, char *ip);
/* Forwards. */
static int initialize_listen_socket(usockaddr * usaP);
static int auth_check(webs_t conn_fp);
static void send_error(webs_t conn_fp, int status, char *title, char *extra_header, const char *fmt, ...);
static void send_headers(webs_t conn_fp, int status, char *title, char *extra_header, char *mime_type, int length, char *attach_file, int nocache);
static int b64_decode(const char *str, unsigned char *space, int size);
static int match(const char *pattern, const char *string);
static int match_one(const char *pattern, int patternlen, const char *string);
static void *handle_request(void *conn_fp);
#if !defined(HAVE_MICRO) && !defined(__UCLIBC__)
#define PTHREAD_MUTEX_INIT pthread_mutex_init
#define PTHREAD_MUTEX_LOCK pthread_mutex_lock
#define PTHREAD_MUTEX_UNLOCK pthread_mutex_unlock
#define SEM_WAIT sem_wait
#define SEM_POST sem_post
#define SEM_INIT sem_init
#define HTTP_MAXCONN 5
static sem_t semaphore;
#ifdef __UCLIBC__
static pthread_mutex_t crypt_mutex;
#endif
static pthread_mutex_t httpd_mutex;
static pthread_mutex_t input_mutex;
#ifdef __UCLIBC__
#define CRYPT_MUTEX_INIT pthread_mutex_init
#define CRYPT_MUTEX_LOCK pthread_mutex_lock
#define CRYPT_MUTEX_UNLOCK pthread_mutex_unlock
#else
#define CRYPT_MUTEX_INIT(m, v) do {} while(0)
#define CRYPT_MUTEX_LOCK(m) do {} while(0)
#define CRYPT_MUTEX_UNLOCK(m) do {} while(0)
#endif
#else
#define PTHREAD_MUTEX_INIT(m, v) do {} while(0)
#define PTHREAD_MUTEX_LOCK(m) do {} while(0)
#define PTHREAD_MUTEX_UNLOCK(m) do {} while(0)
#define SEM_WAIT(sem) do {} while(0)
#define SEM_POST(sem) do {} while(0)
#define SEM_INIT(sem, p, v) do {} while(0)
#define CRYPT_MUTEX_INIT(m, v) do {} while(0)
#define CRYPT_MUTEX_LOCK(m) do {} while(0)
#define CRYPT_MUTEX_UNLOCK(m) do {} while(0)
#endif
static void lookup_hostname(usockaddr * usa4P, size_t sa4_len, int *gotv4P, usockaddr * usa6P, size_t sa6_len, int *gotv6P, int port)
{
#ifdef USE_IPV6
struct addrinfo hints;
char portstr[10];
int gaierr;
struct addrinfo *ai;
struct addrinfo *ai2;
struct addrinfo *aiv6;
struct addrinfo *aiv4;
(void)memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_flags = AI_PASSIVE;
hints.ai_socktype = SOCK_STREAM;
(void)snprintf(portstr, sizeof(portstr), "%d", (int)port);
if ((gaierr = getaddrinfo(NULL, portstr, &hints, &ai)) != 0) {
exit(1);
}
/* Find the first IPv6 and IPv4 entries. */
aiv6 = NULL;
aiv4 = NULL;
for (ai2 = ai; ai2 != NULL; ai2 = ai2->ai_next) {
switch (ai2->ai_family) {
case AF_INET6:
if (!aiv6)
aiv6 = ai2;
break;
case AF_INET:
if (!aiv4)
aiv4 = ai2;
break;
}
}
if (!aiv6)
*gotv6P = 0;
else {
if (sa6_len < aiv6->ai_addrlen) {
exit(1);
}
(void)memset(usa6P, 0, sa6_len);
(void)memmove(usa6P, aiv6->ai_addr, aiv6->ai_addrlen);
*gotv6P = 1;
}
if (!aiv4)
*gotv4P = 0;
else {
if (sa4_len < aiv4->ai_addrlen) {
exit(1);
}
(void)memset(usa4P, 0, sa4_len);
(void)memmove(usa4P, aiv4->ai_addr, aiv4->ai_addrlen);
*gotv4P = 1;
}
freeaddrinfo(ai);
#else /* USE_IPV6 */
struct hostent *he;
*gotv6P = 0;
(void)memset(usa4P, 0, sa4_len);
usa4P->sa.sa_family = AF_INET;
usa4P->sa_in.sin_addr.s_addr = htonl(INADDR_ANY);
usa4P->sa_in.sin_port = htons(port);
*gotv4P = 1;
#endif /* USE_IPV6 */
}
static int sockaddr_check(usockaddr * usaP)
{
switch (usaP->sa.sa_family) {
case AF_INET:
return 1;
#ifdef USE_IPV6
case AF_INET6:
return 1;
#endif /* USE_IPV6 */
default:
return 0;
}
}
static size_t sockaddr_len(usockaddr * usaP)
{
switch (usaP->sa.sa_family) {
case AF_INET:
return sizeof(struct sockaddr_in);
#ifdef USE_IPV6
case AF_INET6:
return sizeof(struct sockaddr_in6);
#endif /* USE_IPV6 */
default:
return 0; /* shouldn't happen */
}
}
static void setnaggle(webs_t wp, int on)
{
int r;
if (!DO_SSL(wp)) {
#if defined(TCP_NOPUSH)
/* Set the TCP_NOPUSH socket option, to try and avoid the 0.2 second
** delay between sending the headers and sending the data. A better
** solution is writev() (as used in thttpd), or send the headers with
** send(MSG_MORE) (only available in Linux so far).
*/
r = on;
(void)setsockopt(wp->conn_fd, IPPROTO_TCP, TCP_NOPUSH, (void *)&r, sizeof(r));
#endif
if (on) {
r = 1;
(void)setsockopt(wp->conn_fd, IPPROTO_TCP, TCP_NODELAY, (void *)&r, sizeof(r));
}
}
}
static int initialize_listen_socket(usockaddr * usaP)
{
int listen_fd;
int i;
/* Check sockaddr. */
if (!sockaddr_check(usaP)) {
return -1;
}
listen_fd = socket(usaP->sa.sa_family, SOCK_STREAM, 0);
if (listen_fd < 0) {
return -1;
}
(void)fcntl(listen_fd, F_SETFD, 1);
i = 1;
if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&i, sizeof(i)) < 0) {
dd_logerror("httpd", "setsockopt SO_REUSEADDR - %m");
perror("setsockopt SO_REUSEADDR");
return -1;
}
if (bind(listen_fd, &usaP->sa, sockaddr_len(usaP)) < 0) {
return -1;
}
if (listen(listen_fd, 1024) < 0) {
dd_logerror("httpd", "listen - %m");
perror("listen");
return -1;
}
return listen_fd;
}
static int auth_check(webs_t conn_fp)
{
CRYPT_MUTEX_LOCK(&crypt_mutex);
char *authinfo;
char *authpass;
int l;
int ret = 0;
authinfo = malloc(500);
/* Is this directory unprotected? */
if (!*(conn_fp->auth_passwd)) {
/* Yes, let the request go through. */
ret = 1;
goto out;
}
/* Basic authorization info? */
if (!conn_fp->authorization || strncmp(conn_fp->authorization, "Basic ", 6) != 0) {
dd_loginfo("httpd", "Authentication fail");
goto out;
}
bzero(authinfo, 500);
/* Decode it. */
l = b64_decode(&(conn_fp->authorization[6]), (unsigned char *)authinfo, 499);
authinfo[l] = '\0';
/* Split into user and password. */
authpass = strchr((char *)authinfo, ':');
if (authpass == NULL) {
/* No colon? Bogus auth info. */
goto out;
}
*authpass++ = '\0';
char *enc1;
char *enc2;
#ifdef __UCLIBC__
enc1 = crypt(authinfo, (const char *)conn_fp->auth_userid);
#else
struct crypt_data data;
enc1 = crypt_r(authinfo, (const char *)conn_fp->auth_userid, &data);
#endif
char dummy[128];
if (!enc1 || strcmp(enc1, conn_fp->auth_userid)) {
dd_loginfo("httpd", "httpd login failure for %s", conn_fp->http_client_ip);
add_blocklist("httpd", conn_fp->http_client_ip);
while (wfgets(dummy, 64, conn_fp, NULL) > 0) {
}
goto out;
}
#ifdef __UCLIBC__
enc2 = crypt(authpass, (const char *)conn_fp->auth_passwd);
#else
enc2 = crypt_r(authpass, (const char *)conn_fp->auth_passwd, &data);
#endif
if (!enc2 || strcmp(enc2, conn_fp->auth_passwd)) {
dd_loginfo("httpd", "httpd login failure for %s", conn_fp->http_client_ip);
add_blocklist("httpd", conn_fp->http_client_ip);
while (wfgets(dummy, 64, conn_fp, NULL) > 0) {
}
goto out;
}
ret = 1;
out:;
free(authinfo);
CRYPT_MUTEX_UNLOCK(&crypt_mutex);
return ret;
}
static void send_authenticate(webs_t conn_fp)
{
char *header;
(void)asprintf(&header, "WWW-Authenticate: Basic realm=\"%s\"", conn_fp->auth_realm);
send_error(conn_fp, 401, "Unauthorized", header,
#if defined(HAVE_BUFFALO) && defined(HAVE_IAS)
"Authorization required. please note that the default username is \"admin\" in all newer releases");
#else
"Authorization required. please note that the default username is \"root\" in all newer releases");
#endif
free(header);
}
static void send_error(webs_t conn_fp, int status, char *title, char *extra_header, const char *fmt, ...)
{
char *text;
va_list args;
va_start(args, (char *)fmt);
vasprintf(&text, fmt, args);
va_end(args);
dd_logerror("httpd", "Request Error Code %d: %s\n", status, text);
// jimmy, https, 8/4/2003, fprintf -> wfprintf, fflush -> wfflush
send_headers(conn_fp, status, title, extra_header, "text/html", -1, NULL, 1);
(void)wfprintf(conn_fp, "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>\n<BODY BGCOLOR=\"#cc9999\"><H4>%d %s</H4>\n", status, title, status, title);
(void)wfprintf(conn_fp, "%s\n", text);
(void)wfprintf(conn_fp, "</BODY></HTML>\n");
(void)wfflush(conn_fp);
free(text);
}
static void send_headers(webs_t conn_fp, int status, char *title, char *extra_header, char *mime_type, int length, char *attach_file, int nocache)
{
time_t now;
char timebuf[100];
wfprintf(conn_fp, "%s %d %s\r\n", PROTOCOL, status, title);
if (mime_type != NULL)
wfprintf(conn_fp, "Content-Type: %s\r\n", mime_type);
wfprintf(conn_fp, "Server: %s\r\n", SERVER_NAME);
now = time(NULL);
strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&now));
wfprintf(conn_fp, "Date: %s\r\n", timebuf);
wfprintf(conn_fp, "Connection: close\r\n");
if (nocache) {
wfprintf(conn_fp, "Cache-Control: no-store, no-cache, must-revalidate\r\n");
wfprintf(conn_fp, "Cache-Control: post-check=0, pre-check=0\r\n");
wfprintf(conn_fp, "Pragma: no-cache\r\n");
} else {
wfprintf(conn_fp, "Cache-Control: private, max-age=600\r\n");
}
if (attach_file) {
int i, len = strlen(attach_file);
int cnt = 0;
char *newname = malloc((len * 3) + 1);
for (i = 0; i < len; i++) {
if (attach_file[i] == ' ') {
newname[cnt++] = '%';
newname[cnt++] = '2';
newname[cnt++] = '0';
} else {
newname[cnt++] = attach_file[i];
}
}
newname[cnt++] = 0;
wfprintf(conn_fp, "Content-Disposition: attachment; filename=%s\r\n", newname);
free(newname);
}
if (extra_header != NULL && *extra_header)
wfprintf(conn_fp, "%s\r\n", extra_header);
if (length != -1)
wfprintf(conn_fp, "Content-Length: %ld\r\n", length);
wfprintf(conn_fp, "\r\n");
}
/* Base-64 decoding. This represents binary data as printable ASCII
** characters. Three 8-bit binary bytes are turned into four 6-bit
** values, like so:
**
** [11111111] [22222222] [33333333]
**
** [111111] [112222] [222233] [333333]
**
** Then the 6-bit values are represented using the characters "A-Za-z0-9+/".
*/
static const int b64_decode_table[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00-0F */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10-1F */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 20-2F */
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 30-3F */
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 40-4F */
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 50-5F */
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 60-6F */
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, /* 70-7F */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80-8F */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90-9F */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* A0-AF */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* B0-BF */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* C0-CF */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* D0-DF */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* E0-EF */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* F0-FF */
};
/* Do base-64 decoding on a string. Ignore any non-base64 bytes.
** Return the actual number of bytes generated. The decoded size will
** be at most 3/4 the size of the encoded, and may be smaller if there
** are padding characters (blanks, newlines).
*/
static int b64_decode(const char *str, unsigned char *space, int size)
{
const char *cp;
int space_idx, phase;
int d, prev_d = 0;
unsigned char c;
space_idx = 0;
phase = 0;
for (cp = str; *cp != '\0'; ++cp) {
d = b64_decode_table[(int)*cp];
if (d != -1) {
switch (phase) {
case 0:
++phase;
break;
case 1:
c = ((prev_d << 2) | ((d & 0x30) >> 4));
if (space_idx < size)
space[space_idx++] = c;
++phase;
break;
case 2:
c = (((prev_d & 0xf) << 4) | ((d & 0x3c) >> 2));
if (space_idx < size)
space[space_idx++] = c;
++phase;
break;
case 3:
c = (((prev_d & 0x03) << 6) | d);
if (space_idx < size)
space[space_idx++] = c;
phase = 0;
break;
}
prev_d = d;
}
}
return space_idx;
}
/* Simple shell-style filename matcher. Only does ? * and **, and multiple
** patterns separated by |. Returns 1 or 0.
*/
static int match(const char *pattern, const char *string)
{
const char *or;
char *path = strdup(string);
char *query = strchr(path, '?');
if (query)
*query++ = 0;
for (;;) {
or = strchr(pattern, '|');
if (or == NULL) {
int ret = match_one(pattern, strlen(pattern), path);
free(path);
return ret;
}
if (match_one(pattern, or - pattern, path)) {
free(path);
return 1;
}
pattern = or + 1;
}
}
static int match_one(const char *pattern, int patternlen, const char *string)
{
const char *p;
for (p = pattern; p - pattern < patternlen; ++p, ++string) {
if (*p == '?' && *string != '\0')
continue;
if (*p == '*') {
int i, pl;
++p;
if (*p == '*') {
/* Double-wildcard matches anything. */
++p;
i = strlen(string);
} else
/* Single-wildcard matches anything but slash. */
i = strcspn(string, "/");
pl = patternlen - (p - pattern);
for (; i >= 0; --i)
if (match_one(p, pl, &(string[i])))
return 1;
return 0;
}
if (*p != *string)
return 0;
}
if (*string == '\0')
return 1;
return 0;
}
static void do_file_2(struct mime_handler *handler, char *path, webs_t stream, char *attach) //jimmy, https, 8/4/2003
{
size_t len;
FILE *web = getWebsFile(stream, path);
if (web == NULL) {
if (!(web = fopen(path, "rb")))
return;
fseek(web, 0, SEEK_END);
len = ftell(web);
fseek(web, 0, SEEK_SET);
} else {
len = getWebsFileLen(stream, path);
}
if (!handler->send_headers)
send_headers(stream, 200, "Ok", handler->extra_header, handler->mime_type, len, attach, 0);
if (DO_SSL(stream)) {
char *buffer = malloc(4096);
while (len) {
size_t ret = fread(buffer, 1, len > 4096 ? 4096 : len, web);
if (!ret) {
dd_loginfo("httpd", "%s: cannot ret from stream (%s)\n", __func__, strerror(errno));
break; // deadlock prevention
}
len -= ret;
wfwrite(buffer, ret, 1, stream);
}
free(buffer);
} else {
wfflush(stream);
wfsendfile(fileno(web), ftell(web), len, stream);
}
fclose(web);
}
static void
//do_file(char *path, FILE *stream)
do_file(unsigned char method, struct mime_handler *handler, char *path, webs_t stream) //jimmy, https, 8/4/2003
{
do_file_2(handler, path, stream, NULL);
}
static void do_file_attach(struct mime_handler *handler, char *path, webs_t stream, char *attachment) //jimmy, https, 8/4/2003
{
do_file_2(handler, path, stream, attachment);
}
static int check_connect_type(webs_t wp)
{
struct wl_assoc_mac *wlmac = NULL;
int count_wl = 0;
int i, j;
char temp[32];
int c = getdevicecount();
for (j = 0; j < c; j++) {
sprintf(temp, "wl%d_web_filter", j);
if (nvram_invmatchi(temp, 1))
continue;
wlmac = get_wl_assoc_mac(j, &count_wl);
for (i = 0; i < count_wl; i++) {
if (!strcmp(wlmac[i].mac, wp->http_client_mac)) {
cprintf("Can't accept wireless access\n");
free(wlmac);
return -1;
}
}
free(wlmac);
}
return 0;
}
#ifdef HAVE_IAS
char ias_sid[20];
int ias_sid_timeout;
void ias_sid_set(webs_t wp);
int ias_sid_valid(webs_t wp);
#endif
static persistent_vars global_vars;
#define LINE_LEN 10000
static void *handle_request(void *arg)
{
webs_t conn_fp = (webs_t)arg;
char *cur = NULL;
char *method, *path, *protocol, *authorization, *boundary, *referer, *host;
#ifdef HAVE_IAS
char *language, *useragent;
#endif
char *cp = NULL;
char *file = NULL;
int len;
struct mime_handler *handler;
size_t content_length = 0;
int flags;
char *line;
long method_type;
conn_fp->p = &global_vars;
PTHREAD_MUTEX_LOCK(&input_mutex); // barrier. block until input is done. otherwise global members get async
PTHREAD_MUTEX_UNLOCK(&input_mutex);
PTHREAD_MUTEX_LOCK(&httpd_mutex);
#ifdef HAVE_REGISTER
conn_fp->isregistered = registered;
conn_fp->isregistered_real = registered_real;
#endif
#ifdef HAVE_SUPERCHANNEL
conn_fp->issuperchannel = superchannel;
#endif
PTHREAD_MUTEX_UNLOCK(&httpd_mutex);
setnaggle(conn_fp, 1);
line = malloc(LINE_LEN);
/* Initialize the request variables. */
authorization = referer = boundary = host = NULL;
bzero(line, LINE_LEN);
/* Parse the first line of the request. */
int cnt = 0;
int eof = 0;
errno = 0; //make sure errno was not set by any other instance since we have no return code to check here
for (;;) {
if (cnt == 5000)
break;
wfgets(line, LINE_LEN, conn_fp, &eof);
if (eof) {
send_error(conn_fp, 408, "TCP Error", NULL, "Unexpected connection close in intitial request");
goto out;
}
if (!*(line) && (errno == EINTR || errno == EAGAIN)) {
struct timespec tim, tim2;
tim.tv_sec = 0;
tim.tv_nsec = 1000000L;
nanosleep(&tim, &tim2);
cnt++;
continue;
}
break;
}
if (!*(line)) {
send_error(conn_fp, 408, "Request Timeout", NULL, "No request appeared within a reasonable time period.");
goto out;
}
/* To prevent http receive https packets, cause http crash (by honor 2003/09/02) */
if (strncasecmp(line, "GET", 3) && strncasecmp(line, "POST", 4) && strncasecmp(line, "OPTIONS", 7)) {
goto out;
}
method = path = line;
strsep(&path, " ");
if (!path) { // Avoid http server crash, added by honor 2003-12-08
send_error(conn_fp, 400, "Bad Request", NULL, "Can't parse request. (no path given)");
goto out;
}
while (*path == ' ')
path++;
protocol = path;
strsep(&protocol, " ");
if (!protocol) { // Avoid http server crash, added by honor 2003-12-08
send_error(conn_fp, 400, "Bad Request", NULL, "Can't parse request. (no protocol given)");
goto out;
}
while (*protocol == ' ')
protocol++;
cp = protocol;
strsep(&cp, " ");
cur = protocol + strlen(protocol) + 1;
/* Parse the rest of the request headers. */
while ((line + LINE_LEN - cur) > 1 && wfgets(cur, line + LINE_LEN - cur, conn_fp, &eof) != 0) //jimmy,https,8/4/2003
{
if (eof) {
send_error(conn_fp, 408, "TCP Error", NULL, "Unexpected connection close");
goto out;
}
if (strcmp(cur, "\n") == 0 || strcmp(cur, "\r\n") == 0) {
break;
} else if (strncasecmp(cur, "Authorization:", 14) == 0) {
cp = &cur[14];
cp += strspn(cp, " \t");
authorization = cp;
cur = cp + strlen(cp) + 1;
} else if (strncasecmp(cur, "Referer:", 8) == 0) {
cp = &cur[8];
cp += strspn(cp, " \t");
referer = cp;
cur = cp + strlen(cp) + 1;
} else if (strncasecmp(cur, "Host:", 5) == 0) {
cp = &cur[5];
cp += strspn(cp, " \t");
host = cp;
cur = cp + strlen(cp) + 1;
} else if (strncasecmp(cur, "Content-Length:", 15) == 0) {
cp = &cur[15];
cp += strspn(cp, " \t");
content_length = strtoul(cp, NULL, 0);
} else if ((cp = strstr(cur, "boundary="))) {
boundary = &cp[9];
for (cp = cp + 9; *cp && *cp != '\r' && *cp != '\n'; cp++) ;
*cp = '\0';
cur = ++cp;
}
#ifdef HAVE_IAS
else if (strncasecmp(cur, "User-Agent:", 11) == 0) {
cp = &cur[11];
cp += strspn(cp, " \t");
useragent = cp;
cur = cp + strlen(cp) + 1;
} else if (strncasecmp(cur, "Accept-Language:", 16) == 0) {
cp = &cur[17];
cp += strspn(cp, " \t");
language = cp;
cur = cp + strlen(cp) + 1;
}
#endif
}
method_type = METHOD_INVALID;
if (!strcasecmp(method, "get"))
method_type = METHOD_GET;
if (!strcasecmp(method, "post"))
method_type = METHOD_POST;
if (!strcasecmp(method, "options"))
method_type = METHOD_OPTIONS;
if (method_type == METHOD_INVALID) {
send_error(conn_fp, 501, "Not Implemented", NULL, "Method %s is not implemented.", method);
goto out;
}
if (path[0] != '/') {
send_error(conn_fp, 400, "Bad Request", NULL, "Bad filename. (no leading slash)");
goto out;
}
file = &(path[1]);
len = strlen(file);
if (file[0] == '/' || strcmp(file, "..") == 0 || strncmp(file, "../", 3) == 0 || strstr(file, "/../") != NULL || strcmp(&(file[len - 3]), "/..") == 0) {
send_error(conn_fp, 400, "Bad Request", NULL, "Illegal filename. (filename will treat local filesystem)");
goto out;
}
int nodetect = 0;
if (nvram_matchi("status_auth", 0) && endswith(file, "Info.htm"))
nodetect = 1;
if (nvram_matchi("no_crossdetect", 1))
nodetect = 1;
#ifdef HAVE_IAS
if (nvram_matchi("ias_startup", 3) || nvram_matchi("ias_startup", 2)) {
nodetect = 1;
typedef struct {
char *iso;
char *mapping;
char *countryext;
} ISOMAP;
ISOMAP isomap[] = {
{
"de", "german" }, //
{
"es", "spanish" }, //
{
"fr", "french" }, //
{
"hr", "croatian" }, //
{
"hu", "hungarian" }, //
{
"nl", "dutch" }, //
{
"it", "italian" }, //
{
"lv", "latvian" }, //
{
"jp", "japanese" }, //
{
"pl", "polish" }, //
{
"pt", "portuguese_braz" }, //
{
"ro", "romanian" }, //
{
"ru", "russian", "RU" }, //
{
"sl", "slovenian" }, //
{
"sr", "serbian" }, //
{
"sv", "swedish" }, //
{
"zh", "chinese_simplified" }, //
{
"tr", "turkish" }, //
NULL
};
if (nvram_match("langprop", "") || nvram_get("langprop") == NULL) {
int cnt = 0;
nvram_set("langprop", "english");
while (isomap[cnt].iso != NULL) {
if (strncasecmp(language, isomap[cnt].iso, 2) == 0) {
nvram_set("langprop", isomap[cnt].mapping);
if (isomap[cnt].countryext)
nvram_set("country", isomap[cnt].countryext);
break;
}
cnt++;