forked from jbd/httpfs2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpfs2.c
1567 lines (1415 loc) · 46.5 KB
/
httpfs2.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
/*
* HTTPFS: import a file from a web server to local file system
* the main use is, to mount an iso on a web server with loop device
*
* depends on:
* FUSE: Filesystem in Userspace
* Copyright (C) 2001-2007 Miklos Szeredi <[email protected]>
*
* This program can be distributed under the terms of the GNU GPL.
*
*/
/*
* (c) 2006 hmb marionraven at users.sourceforge.net
*
*/
/*
* Modified to work with fuse 2.7.
* Added keepalive
* The passthru functionality removed to simplify the code.
* (c) 2008-2012 Michal Suchanek <[email protected]>
*
*/
#define FUSE_USE_VERSION 26
#include <fuse_lowlevel.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/dir.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <stddef.h>
#include <inttypes.h>
#include <locale.h>
#include <time.h>
#ifdef USE_THREAD
#include <pthread.h>
static pthread_key_t url_key;
#define FUSE_LOOP fuse_session_loop_mt
#else
#define FUSE_LOOP fuse_session_loop
#endif
#ifdef USE_SSL
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#endif
#define TIMEOUT 30
#define CONSOLE "/dev/console"
#define HEADER_SIZE 1024
#define VERSION "0.1.5 \"The Message\""
static char* argv0;
#define MAX_REQUEST (32*1024)
#define SOCK_CLOSED 0
#define SOCK_OPEN 1
#define SOCK_KEEPALIVE 2
typedef struct url {
int proto;
long timeout;
char * host; /*hostname*/
int port;
char * path; /*get path*/
char * name; /*file name*/
char * header;
#ifdef USE_AUTH
char * auth; /*encoded auth data*/
#endif
#ifdef RETRY_ON_RESET
int retry_reset; /*retry reset connections*/
#endif
int sockfd;
int sock_type;
#ifdef USE_SSL
long ssl_log_level;
unsigned md5;
unsigned md2;
int ssl_initialized;
int ssl_connected;
gnutls_certificate_credentials_t sc;
gnutls_session_t ss;
const char * cafile;
#endif
char * req_buf;
size_t req_buf_size;
off_t file_size;
time_t last_modified;
} struct_url;
static struct_url main_url;
static off_t get_stat(struct_url*, struct stat * stbuf);
static ssize_t get_data(struct_url*, off_t start, size_t size);
static int open_client_socket(struct_url *url);
static int close_client_socket(struct_url *url);
static int close_client_force(struct_url *url);
static struct_url * thread_setup(void);
static void destroy_url_copy(void *);
/* Protocol symbols. */
#define PROTO_HTTP 0
#ifdef USE_SSL
#define PROTO_HTTPS 1
#endif
#ifdef USE_AUTH
static char b64_encode_table[64] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', /* 0-7 */
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', /* 8-15 */
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', /* 16-23 */
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', /* 24-31 */
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 32-39 */
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', /* 40-47 */
'w', 'x', 'y', 'z', '0', '1', '2', '3', /* 48-55 */
'4', '5', '6', '7', '8', '9', '+', '/' /* 56-63 */
};
/* Do base-64 encoding on a hunk of bytes. Return pointer to the
** bytes generated. Base-64 encoding takes up 4/3 the space of the original,
** plus a bit for end-padding. 3/2+5 gives a safe margin.
*/
static char * b64_encode(unsigned const char* ptr, long len) {
char * space;
int ptr_idx;
int c = 0;
int d = 0;
int space_idx = 0;
int phase = 0;
/*FIXME calculate the occupied space properly*/
size_t size = ((size_t)len * 3) /2 + 5;
space = malloc(size+1);
space[size] = 0;
for (ptr_idx = 0; ptr_idx < len; ++ptr_idx) {
switch (phase++) {
case 0:
c = ptr[ptr_idx] >> 2;
d = (ptr[ptr_idx] & 0x3) << 4;
break;
case 1:
c = d | (ptr[ptr_idx] >> 4);
d = (ptr[ptr_idx] & 0xf) << 2;
break;
case 2:
c = d | (ptr[ptr_idx] >> 6);
if (space_idx < size) space[space_idx++] = b64_encode_table[c];
c = ptr[ptr_idx] & 0x3f;
break;
}
space[space_idx++] = b64_encode_table[c];
if (space_idx == size) return space;
phase %= 3;
}
if (phase != 0) {
space[space_idx++] = b64_encode_table[d];
if (space_idx == size) return space;
/* Pad with ='s. */
while (phase++ > 0) {
space[space_idx++] = '=';
if (space_idx == size) return space;
phase %= 3;
}
}
return space;
}
#endif /* USE_AUTH */
/*
* The FUSE operations originally ripped from the hello_ll sample.
*/
static long httpfs_stat(fuse_ino_t ino, struct stat *stbuf)
{
stbuf->st_ino = ino;
switch (ino) {
case 1:
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
break;
case 2: {
struct_url * url = thread_setup();
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
return (long) get_stat(url, stbuf);
}; break;
default:
errno = ENOENT;
return -1;
}
return 0;
}
static void httpfs_getattr(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *fi)
{
struct stat stbuf;
(void) fi;
memset(&stbuf, 0, sizeof(stbuf));
if (httpfs_stat(ino, &stbuf) < 0)
assert(errno),fuse_reply_err(req, errno);
else
fuse_reply_attr(req, &stbuf, 1.0);
}
static void httpfs_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
{
struct fuse_entry_param e;
memset(&e, 0, sizeof(e));
e.attr_timeout = 1.0;
e.entry_timeout = 1.0;
if (parent != 1 || strcmp(name, main_url.name) != 0){
e.ino = 0;
} else {
e.ino = 2;
if(httpfs_stat(e.ino, &e.attr) < 0){
assert(errno);
fuse_reply_err(req, errno);
return;
}
}
fuse_reply_entry(req, &e);
}
struct dirbuf {
char *p;
size_t size;
};
static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name,
fuse_ino_t ino)
{
struct stat stbuf;
size_t oldsize = b->size;
b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
b->p = (char *) realloc(b->p, b->size);
memset(&stbuf, 0, sizeof(stbuf));
stbuf.st_ino = ino;
fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
(off_t) b->size);
}
#define min(x, y) ((x) < (y) ? (x) : (y))
static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
off_t off, size_t maxsize)
{
assert(off >= 0);
if (off < bufsize)
return fuse_reply_buf(req, buf + off,
min(bufsize - (size_t)off, maxsize));
else
return fuse_reply_buf(req, NULL, 0);
}
static void httpfs_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
off_t off, struct fuse_file_info *fi)
{
(void) fi;
if (ino != 1)
fuse_reply_err(req, ENOTDIR);
else {
struct dirbuf b;
memset(&b, 0, sizeof(b));
dirbuf_add(req, &b, ".", 1);
dirbuf_add(req, &b, "..", 1);
dirbuf_add(req, &b, main_url.name, 2);
reply_buf_limited(req, b.p, b.size, off, size);
free(b.p);
}
}
static void httpfs_open(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *fi)
{
if (ino != 2)
fuse_reply_err(req, EISDIR);
else if ((fi->flags & 3) != O_RDONLY)
fuse_reply_err(req, EACCES);
else{
/* direct_io is supposed to allow partial reads. However, setting
* the flag causes read length max at 4096 bytes which leads to
* *many* requests, poor performance, and errors. Some resources
* like TCP ports are recycled too fast for Linux to cope.
*/
//fi->direct_io = 1;
fuse_reply_open(req, fi);
}
}
static void httpfs_read(fuse_req_t req, fuse_ino_t ino, size_t size,
off_t off, struct fuse_file_info *fi)
{
(void) fi;
struct_url * url = thread_setup();
ssize_t res;
assert(ino == 2);
assert(url->file_size >= off);
size=min(size, (size_t)(url->file_size - off));
if(url->file_size == off) {
/* Handling of EOF is not well documented, returning EOF as error
* does not work but this does. */
fuse_reply_buf(req, NULL, 0);
return;
}
/* since we have to return all stuff requested the buffer cannot be
* allocated in advance */
if(url->req_buf
&& ( (url->req_buf_size < size )
|| ( (url->req_buf_size > size )
&& (url->req_buf_size > MAX_REQUEST) ) ) ){
free(url->req_buf);
url->req_buf = 0;
}
if(! url->req_buf){
url->req_buf_size = size;
url->req_buf = malloc(size);
}
if((res = get_data(url, off, size)) < 0){
assert(errno);
fuse_reply_err(req, errno);
}else{
fuse_reply_buf(req, url->req_buf, (size_t)res);
}
}
static struct fuse_lowlevel_ops httpfs_oper = {
.lookup = httpfs_lookup,
.getattr = httpfs_getattr,
.readdir = httpfs_readdir,
.open = httpfs_open,
.read = httpfs_read,
};
/*
* A few utility functions
*/
#ifdef NEED_STRNDUP
static char * strndup(const char * str, size_t n){
if(n > strlen(str)) n = strlen(str);
char * res = malloc(n + 1);
memcpy(res, str, n);
res[n] = 0;
return res;
}
#endif
static int mempref(const char * mem, const char * pref, size_t size, int case_sensitive)
{
/* return true if found */
if (size < strlen(pref)) return 0;
if (case_sensitive)
return ! memcmp(mem, pref, strlen(pref));
else {
int i;
for (i = 0; i < strlen(pref); i++)
/* Unless somebody calling setlocale() behind our back locale should be C. */
/* It is important to not uppercase in languages like Turkish. */
if (tolower(mem[i]) != tolower(pref[i]))
return 0;
return 1;
}
}
#ifdef USE_SSL
static void errno_report(const char * where);
static void ssl_error(int error, gnutls_session_t ss, const char * where);
/* Functions to deal with gnutls_datum_t stolen from gnutls docs.
* The structure does not seem documented otherwise.
*/
static gnutls_datum_t
load_file (const char *file)
{
FILE *f;
gnutls_datum_t loaded_file = { NULL, 0 };
long filelen;
void *ptr;
f = fopen (file, "r");
if (!f)
errno_report(file);
else if (fseek (f, 0, SEEK_END) != 0)
errno_report(file);
else if ((filelen = ftell (f)) < 0)
errno_report(file);
else if (fseek (f, 0, SEEK_SET) != 0)
errno_report(file);
else if (!(ptr = malloc ((size_t) filelen)))
errno_report(file);
else if (fread (ptr, 1, (size_t) filelen, f) < (size_t) filelen)
errno_report(file);
else {
loaded_file.data = ptr;
loaded_file.size = (unsigned int) filelen;
fprintf(stderr, "Loaded '%s' %ld bytes\n", file, filelen);
/* fwrite(ptr, filelen, 1, stderr); */
}
return loaded_file;
}
static void
unload_file (gnutls_datum_t data)
{
free (data.data);
}
/* This function will print some details of the
* given session.
*
* Stolen from the GNUTLS docs.
*/
int
print_ssl_info (gnutls_session_t session)
{
const char *tmp;
gnutls_credentials_type_t cred;
gnutls_kx_algorithm_t kx;
int dhe, ecdh;
dhe = ecdh = 0;
/* print the key exchange’s algorithm name
*/
kx = gnutls_kx_get (session);
tmp = gnutls_kx_get_name (kx);
printf ("- Key Exchange: %s\n", tmp);
/* Check the authentication type used and switch
* to the appropriate.
*/
cred = gnutls_auth_get_type (session);
switch (cred)
{
case GNUTLS_CRD_CERTIFICATE:
/* certificate authentication */
/* Check if we have been using ephemeral Diffie-Hellman.
*/
if (kx == GNUTLS_KX_DHE_RSA || kx == GNUTLS_KX_DHE_DSS)
dhe = 1;
#if (GNUTLS_VERSION_MAJOR > 3 )
else if (kx == GNUTLS_KX_ECDHE_RSA || kx == GNUTLS_KX_ECDHE_ECDSA)
ecdh = 1;
#endif
/* cert should have been printed when it was verified */
break;
default:
printf("Not a x509 sesssion !?!\n");
}
#if (GNUTLS_VERSION_MAJOR > 3 )
/* switch */
if (ecdh != 0)
printf ("- Ephemeral ECDH using curve %s\n",
gnutls_ecc_curve_get_name (gnutls_ecc_curve_get (session)));
else
#endif
if (dhe != 0)
printf ("- Ephemeral DH using prime of %d bits\n",
gnutls_dh_get_prime_bits (session));
/* print the protocol’s name (ie TLS 1.0)
*/
tmp = gnutls_protocol_get_name (gnutls_protocol_get_version (session));
printf ("- Protocol: %s\n", tmp);
/* print the certificate type of the peer.
* ie X.509
*/
tmp =
gnutls_certificate_type_get_name (gnutls_certificate_type_get (session));
printf ("- Certificate Type: %s\n", tmp);
/* print the compression algorithm (if any)
*/
tmp = gnutls_compression_get_name (gnutls_compression_get (session));
printf ("- Compression: %s\n", tmp);
/* print the name of the cipher used.
* ie 3DES.
*/
tmp = gnutls_cipher_get_name (gnutls_cipher_get (session));
printf ("- Cipher: %s\n", tmp);
/* Print the MAC algorithms name.
* ie SHA1
*/
tmp = gnutls_mac_get_name (gnutls_mac_get (session));
printf ("- MAC: %s\n", tmp);
printf ("Note: SSL paramaters may change as new connections are established to the server.\n");
return 0;
}
/* This function will try to verify the peer’s certificate, and
* also check if the hostname matches, and the activation, expiration dates.
*
* Stolen from the gnutls manual.
*/
static int
verify_certificate_callback (gnutls_session_t session)
{
unsigned int status;
const gnutls_datum_t *cert_list;
unsigned int cert_list_size;
int ret;
gnutls_x509_crt_t cert;
gnutls_datum_t data = {0};
struct_url * url = gnutls_session_get_ptr (session);
const char *hostname = url->host;
/* This verification function uses the trusted CAs in the credentials
* structure. So you must have installed one or more CA certificates.
*/
ret = gnutls_certificate_verify_peers2 (session, &status);
if (ret < 0)
{
ssl_error(ret, session, "verify certificate");
return GNUTLS_E_CERTIFICATE_ERROR;
}
if (status & GNUTLS_CERT_INVALID)
printf ("The server certificate is NOT trusted.\n");
if (status & GNUTLS_CERT_INSECURE_ALGORITHM)
printf ("The server certificate uses an insecure algorithm.\n");
if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
printf ("The server certificate hasn’t got a known issuer.\n");
if (status & GNUTLS_CERT_REVOKED)
printf ("The server certificate has been revoked.\n");
if (status & GNUTLS_CERT_EXPIRED)
printf ("The server certificate has expired\n");
if (status & GNUTLS_CERT_NOT_ACTIVATED)
printf ("The server certificate is not yet activated\n");
/* Up to here the process is the same for X.509 certificates and
* OpenPGP keys. From now on X.509 certificates are assumed. This can
* be easily extended to work with openpgp keys as well.
*/
if (gnutls_certificate_type_get (session) != GNUTLS_CRT_X509)
return GNUTLS_E_CERTIFICATE_ERROR;
if (gnutls_x509_crt_init (&cert) < 0)
{
ssl_error(ret, session, "verify certificate");
return GNUTLS_E_CERTIFICATE_ERROR;
}
cert_list = gnutls_certificate_get_peers (session, &cert_list_size);
if (cert_list == NULL)
{
printf ("No server certificate was found!\n");
return GNUTLS_E_CERTIFICATE_ERROR;
}
/* Check the hostname matches the certificate.
*/
ret = gnutls_x509_crt_import (cert, &cert_list[0], GNUTLS_X509_FMT_DER);
if (ret < 0)
{
ssl_error(ret, session, "parsing certificate");
return GNUTLS_E_CERTIFICATE_ERROR;
}
if (!(url->ssl_connected)) if (!gnutls_x509_crt_print (cert, GNUTLS_CRT_PRINT_FULL, &data)) {
printf("%s", data.data);
gnutls_free(data.data);
}
if (!hostname || !gnutls_x509_crt_check_hostname (cert, hostname))
{
int found = 0;
if (hostname) {
int i;
size_t len = strlen(hostname);
if (*(hostname+len-1) == '.') len--;
if (!(url->ssl_connected)) printf ("Server hostname verification failed. Trying to peek into the cert.\n");
for (i=0;;i++) {
char * dn = NULL;
size_t dn_size = 0;
int dn_ret = 0;
int match=0;
gnutls_x509_crt_get_dn_by_oid(cert, GNUTLS_OID_X520_COMMON_NAME, i, 0, dn, &dn_size);
if (dn_size) dn = malloc(dn_size + 1); /* nul not counted */
if (dn)
dn_ret = gnutls_x509_crt_get_dn_by_oid(cert, GNUTLS_OID_X520_COMMON_NAME, i, 0, dn, &dn_size);
if (!dn_ret){
if (dn) {
if (*(dn+dn_size-1) == '.') dn_size--;
if (len == dn_size)
match = ! strncmp(dn, hostname, len);
if (match) found = 1;
if (!(url->ssl_connected)) printf("Cert CN(%i): %s: %c\n", i, dn, match?'*':'X');
}}
else
ssl_error(dn_ret, session, "getting cert subject data");
if (dn) free(dn);
if (dn_ret || !dn)
break;
}
}
if(!found){
printf ("The server certificate’s owner does not match hostname ’%s’\n",
hostname);
return GNUTLS_E_CERTIFICATE_ERROR;
}
}
gnutls_x509_crt_deinit (cert);
/*
* It the status includes GNUTLS_CERT_INVALID whenever
* there is a problem and the other flags are just informative.
*/
if (status & GNUTLS_CERT_INVALID)
return GNUTLS_E_CERTIFICATE_ERROR;
/* notify gnutls to continue handshake normally */
return 0;
}
static void logfunc(int level, const char * str)
{
fputs(str, stderr);
}
static void ssl_error(int error, gnutls_session_t ss, const char * where)
{
const char * err_desc;
if((error == GNUTLS_E_FATAL_ALERT_RECEIVED) || (error == GNUTLS_E_WARNING_ALERT_RECEIVED))
err_desc = gnutls_alert_get_name(gnutls_alert_get(ss));
else
err_desc = gnutls_strerror(error);
fprintf(stderr, "%s: %s: %d %s.\n", argv0, where, error, err_desc);
errno = EIO; /* FIXME is this used anywhere? */
}
#endif
static void errno_report(const char * where)
{
int e = errno;
fprintf(stderr, "%s: %s: %d %s.\n", argv0, where, errno, strerror(errno));
errno = e;
}
static char * url_encode(char * path) {
return strdup(path); /*FIXME encode*/
}
/*
* functions for handling struct_url
*/
static int init_url(struct_url* url)
{
memset(url, 0, sizeof(url));
url->sock_type = SOCK_CLOSED;
url->timeout = TIMEOUT;
#ifdef USE_SSL
url->cafile = CERT_STORE;
#endif
return 0;
}
static int free_url(struct_url* url)
{
if(url->host) free(url->host);
url->host = 0;
if(url->path) free(url->path);
url->path = 0;
if(url->name) free(url->name);
url->name = 0;
#ifdef USE_AUTH
if(url->auth) free(url->auth);
url->auth = 0;
#endif
if(url->sock_type != SOCK_CLOSED)
close_client_force(url);
url->port = 0;
url->proto = 0; /* only after socket closed */
url->file_size=0;
url->last_modified=0;
return 0;
}
static void print_url(FILE *f, const struct_url * url)
{
char * protocol = "?!?";
switch(url->proto){
case PROTO_HTTP:
protocol = "http";
break;;
#ifdef USE_SSL
case PROTO_HTTPS:
protocol = "https";
break;;
#endif
}
fprintf(f, "file name: \t%s\n", url->name);
fprintf(f, "host name: \t%s\n", url->host);
fprintf(f, "port number: \t%d\n", url->port);
fprintf(f, "protocol: \t%s\n", protocol);
if (url->header)
fprintf(f, "header: \t%s\n", url->header);
fprintf(f, "request path: \t%s\n", url->path);
#ifdef USE_AUTH
fprintf(f, "auth data: \t%s\n", url->auth ? "(present)" : "(null)");
#endif
}
static int parse_url(const char * url, struct_url* res)
{
const char * url_orig = url;
char* http = "http://";
#ifdef USE_SSL
char* https = "https://";
#endif /* USE_SSL */
int path_start = '/';
assert(url);
if (strncmp(http, url, strlen(http)) == 0) {
url += strlen(http);
res->proto = PROTO_HTTP;
res->port = 80;
#ifdef USE_SSL
} else if (strncmp(https, url, strlen(https)) == 0) {
url += strlen(https);
res->proto = PROTO_HTTPS;
res->port = 443;
#endif /* USE_SSL */
} else {
fprintf(stderr, "Invalid protocol in url: %s\n", url_orig);
return -1;
}
/* determine if path was given */
if(strchr(url, path_start))
res->path = url_encode(strchr(url, path_start));
else{
path_start = 0;
res->path = strdup("/");
}
#ifdef USE_AUTH
/* Get user and password */
if(strchr(url, '@') && (strchr(url, '@') < strchr(url, path_start))){
res->auth = b64_encode((unsigned char *)url, strchr(url, '@') - url);
url = strchr(url, '@') + 1;
}else{
res->auth = 0;
}
#endif /* USE_AUTH */
/* Get port number. */
int host_end = path_start;
if(strchr(url, ':') && (strchr(url, ':') < strchr(url, path_start))){
/* FIXME check that port is a valid numeric value */
res->port = atoi(strchr(url, ':') + 1);
if (! res->port) {
fprintf(stderr, "Invalid port in url: %s\n", url_orig);
return -1;
}
host_end = ':';
}
/* Get the host name. */
if (url == strchr(url, host_end)){ /*no hastname in the url */
fprintf(stderr, "No hostname in url: %s\n", url_orig);
return -1;
}
res->host = strndup(url, (size_t)(strchr(url, host_end) - url));
/* Get the file name. */
url = strchr(url, path_start);
const char * end = url + strlen(url);
end--;
/* Handle broken urls with multiple slashes. */
while((end > url) && (*end == '/')) end--;
end++;
if((path_start == 0) || (end == url)
|| (strncmp(url, "/", (size_t)(end - url)) == 0)){
res->name = strdup(res->host);
}else{
while(strchr(url, '/') && (strchr(url, '/') < end))
url = strchr(url, '/') + 1;
res->name = strndup(url, (size_t)(end - url));
}
return res->proto;
}
static void usage(void)
{
fprintf(stderr, "%s >>> Version: %s <<<\n", __FILE__, VERSION);
fprintf(stderr, "usage: %s [-c [console]] "
#ifdef USE_SSL
"[-a file] [-d n] [-5] [-2] "
#endif
"[-f] [-h] [-t timeout] [-r] url mount-parameters\n\n", argv0);
fprintf(stderr, "\t -c \tuse console for standard input/output/error (default: %s)\n", CONSOLE);
fprintf(stderr, "\t -h \tadditional some HTTP header, which will be added to all requests \n");
#ifdef USE_SSL
fprintf(stderr, "\t -a \tCA file used to verify server certificate\n");
fprintf(stderr, "\t -d \tGNUTLS debug level\n");
fprintf(stderr, "\t -5 \tAllow RSA-MD5 cert\n");
fprintf(stderr, "\t -2 \tAllow RSA-MD2 cert\n");
#endif
fprintf(stderr, "\t -f \tstay in foreground - do not fork\n");
#ifdef RETRY_ON_RESET
fprintf(stderr, "\t -r \tretry connection on reset\n");
#endif
fprintf(stderr, "\t -t \tset socket timeout in seconds (default: %i)\n", TIMEOUT);
fprintf(stderr, "\tmount-parameters should include the mount point\n");
}
#define shift { if(!argv[1]) { usage(); return 4; };\
argc--; argv[1] = argv[0]; argv = argv + 1;}
static int convert_num(long * num, char ** argv)
{
char * end = " ";
if( isdigit(*(argv[1]))) {
*num = strtol(argv[1], &end, 0);
/* now end should point to '\0' */
}
if(*end){
usage();
fprintf(stderr, "'%s' is not a number.\n",
argv[1]);
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
char * fork_terminal = CONSOLE;
int do_fork = 1;
setlocale(LC_ALL, "en_US.UTF-8");
putenv("TZ=");/*UTC*/
argv0 = argv[0];
init_url(&main_url);
while( argv[1] && (*(argv[1]) == '-') )
{
char * arg = argv[1]; shift;
while (*++arg){
switch (*arg){
case 'h': main_url.header = argv[1];
shift;
break;
case 'c': if( *(argv[1]) != '-' ) {
fork_terminal = argv[1]; shift;
}else{
fork_terminal = 0;
}
break;
#ifdef USE_SSL
case '2': main_url.md2 = GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD2;
break;
case '5': main_url.md5 = GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5;
break;
case 'a': main_url.cafile = argv[1];
shift;
break;
case 'd': if (convert_num(&main_url.ssl_log_level, argv))
return 4;
shift;
break;
#endif
#ifdef RETRY_ON_RESET
case 'r': main_url.retry_reset = 1;
break;
#endif
case 't': if (convert_num(&main_url.timeout, argv))
return 4;
shift;
break;
case 'f': do_fork = 0;
break;
default:
usage();
fprintf(stderr, "Unknown option '%c'.\n", *arg);
return 4;
}
}
}
if (argc < 3) {
usage();
return 1;
}
if(parse_url(argv[1], &main_url) == -1){
fprintf(stderr, "invalid url: %s\n", argv[1]);
return 2;
}
print_url(stderr, &main_url);
int sockfd = open_client_socket(&main_url);
if(sockfd < 0) {
fprintf(stderr, "Connection failed.\n");
return 3;
}
#ifdef USE_SSL
else {
print_ssl_info(main_url.ss);
}
#endif
close_client_socket(&main_url);
struct stat st;
off_t size = get_stat(&main_url, &st);
if(size >= 0) {
fprintf(stderr, "file size: \t%" PRIdMAX "\n", (intmax_t)size);
}else{
return 3;
}
shift;
if(fork_terminal && access(fork_terminal, O_RDWR)){
errno_report(fork_terminal);
fork_terminal=0;
}
#ifdef USE_THREAD
close_client_force(&main_url); /* each thread should open its own socket */
pthread_key_create(&url_key, &destroy_url_copy);
#endif
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
struct fuse_chan *ch;
char *mountpoint;
int err = -1;
int fork_res = 0;
if (fuse_parse_cmdline(&args, &mountpoint, NULL, NULL) != -1 &&
(ch = fuse_mount(mountpoint, &args)) != NULL) {
fprintf(stderr, "resource ready\n");
/* try to fork at some point where the setup is mostly done */
/* FIXME try to close std* and the like ? */
if(do_fork) fork_res = fork();
switch (fork_res) {
case 0:
{
if(fork_terminal){
/* if we can access the console use it */
int fd = open(fork_terminal, O_RDONLY);
dup2(fd, 0);
close (fd);
fd = open(fork_terminal, O_WRONLY);
dup2(fd, 1);
close (fd);
fd = open(fork_terminal, O_WRONLY|O_SYNC);
dup2(fd, 2);
close (fd);
}
struct fuse_session *se;
se = fuse_lowlevel_new(&args, &httpfs_oper,
sizeof(httpfs_oper), NULL);
if (se != NULL) {
if (fuse_set_signal_handlers(se) != -1) {
fuse_session_add_chan(se, ch);
err = FUSE_LOOP(se);
fuse_remove_signal_handlers(se);
fuse_session_remove_chan(ch);
}
fuse_session_destroy(se);
}
fuse_unmount(mountpoint, ch);
}
break;;
case -1:
errno_report("fork");
break;;
default:
err = 0;
break;;
}
}
fuse_opt_free_args(&args);