forked from justinsb/cloudfuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudfsapi.c
596 lines (542 loc) · 18.3 KB
/
cloudfsapi.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
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/stat.h>
#ifdef __linux__
#include <alloca.h>
#endif
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <libxml/tree.h>
#include "cloudfsapi.h"
#include "config.h"
#define REQUEST_RETRIES 4
static char storage_url[MAX_URL_SIZE];
static char storage_token[MAX_HEADER_SIZE];
static int storage_use_openstack = 0;
static pthread_mutex_t pool_mut;
static CURL *curl_pool[1024];
static int curl_pool_count = 0;
static int debug = 0;
#ifdef HAVE_OPENSSL
#include <openssl/crypto.h>
static pthread_mutex_t *ssl_lockarray;
static void lock_callback(int mode, int type, char *file, int line)
{
if (mode & CRYPTO_LOCK)
pthread_mutex_lock(&(ssl_lockarray[type]));
else
pthread_mutex_unlock(&(ssl_lockarray[type]));
}
static unsigned long thread_id()
{
return (unsigned long)pthread_self();
}
#endif
void init_locks()
{
pthread_mutex_init(&pool_mut, NULL);
#ifdef HAVE_OPENSSL
int i;
ssl_lockarray = (pthread_mutex_t *)OPENSSL_malloc(CRYPTO_num_locks() *
sizeof(pthread_mutex_t));
for (i = 0; i < CRYPTO_num_locks(); i++)
pthread_mutex_init(&(ssl_lockarray[i]), NULL);
CRYPTO_set_id_callback((unsigned long (*)())thread_id);
CRYPTO_set_locking_callback((void (*)())lock_callback);
#endif
}
static void rewrite_url_snet(char *url)
{
char protocol[MAX_URL_SIZE];
char rest[MAX_URL_SIZE];
sscanf(url, "%[a-z]://%s", protocol, rest);
if (strncasecmp(rest, "snet-", 5))
sprintf(url, "%s://snet-%s", protocol, rest);
}
static size_t xml_dispatch(void *ptr, size_t size, size_t nmemb, void *stream)
{
debugf("xml data: %s", ptr);
xmlParseChunk((xmlParserCtxtPtr)stream, (char *)ptr, size * nmemb, 0);
return size * nmemb;
}
static CURL *get_connection(const char *path)
{
char url[MAX_URL_SIZE];
pthread_mutex_lock(&pool_mut);
if (!storage_url[0])
{
debugf("get_connection with no storage_url?");
abort();
}
CURL *curl = curl_pool_count ? curl_pool[--curl_pool_count] : curl_easy_init();
if (!curl)
{
debugf("curl alloc failed");
abort();
}
pthread_mutex_unlock(&pool_mut);
char *slash;
while ((slash = strstr(path, "%2F")) || (slash = strstr(path, "%2f")))
{
*slash = '/';
memmove(slash+1, slash+3, strlen(slash+3)+1);
}
while (*path == '/')
path++;
snprintf(url, sizeof(url), "%s/%s", storage_url, path);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_VERBOSE, debug);
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(curl, CURLOPT_USERAGENT, USER_AGENT);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
return curl;
}
static void return_connection(CURL *curl)
{
curl_easy_reset(curl);
pthread_mutex_lock(&pool_mut);
curl_pool[curl_pool_count++] = curl;
pthread_mutex_unlock(&pool_mut);
}
void add_header(curl_slist **headers, const char *name, const char *value)
{
char x_header[MAX_HEADER_SIZE];
snprintf(x_header, sizeof(x_header), "%s: %s", name, value);
*headers = curl_slist_append(*headers, x_header);
}
static int send_request(char *method, const char *path, FILE *fp, xmlParserCtxtPtr xmlctx, curl_slist *extra_headers)
{
long response = -1;
int tries = 0;
// retry on failures
for (tries = 0; tries < REQUEST_RETRIES; tries++)
{
CURL *curl = get_connection(path);
curl_slist *headers = NULL;
add_header(&headers, "X-Auth-Token", storage_token);
curl_easy_setopt(curl, CURLOPT_VERBOSE, debug);
if (!strcasecmp(method, "MKDIR"))
{
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
curl_easy_setopt(curl, CURLOPT_INFILESIZE, 0);
add_header(&headers, "Content-Type", "application/directory");
}
else if (!strcasecmp(method, "PUT") && fp)
{
rewind(fp);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
curl_easy_setopt(curl, CURLOPT_INFILESIZE, file_size(fileno(fp)));
curl_easy_setopt(curl, CURLOPT_READDATA, fp);
}
else if (!strcasecmp(method, "GET"))
{
if (fp)
{
rewind(fp); // make sure the file is ready for a-writin'
fflush(fp);
ftruncate(fileno(fp), 0);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
}
else if (xmlctx)
{
curl_easy_setopt(curl, CURLOPT_WRITEDATA, xmlctx);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &xml_dispatch);
}
}
else
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method);
/* add the headers from extra_headers if any */
curl_slist *extra;
for (extra = extra_headers; extra; extra = extra->next)
{
debugf("adding header: %s", extra->data);
headers = curl_slist_append(headers, extra->data);
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
curl_slist_free_all(headers);
return_connection(curl);
if (response >= 200 && response < 400)
return response;
sleep(8 << tries); // backoff
if (response == 401 && !cloudfs_connect(0, 0, 0, 0, 0, 0)) // re-authenticate on 401s
return response;
if (xmlctx)
xmlCtxtResetPush(xmlctx, NULL, 0, NULL, NULL);
}
return response;
}
/*
* Public interface
*/
int object_read_fp(const char *path, FILE *fp)
{
fflush(fp);
rewind(fp);
char *encoded = curl_escape(path, 0);
int response = send_request("PUT", encoded, fp, NULL, NULL);
curl_free(encoded);
return (response >= 200 && response < 300);
}
int object_write_fp(const char *path, FILE *fp)
{
char *encoded = curl_escape(path, 0);
int response = send_request("GET", encoded, fp, NULL, NULL);
curl_free(encoded);
fflush(fp);
if ((response >= 200 && response < 300) || ftruncate(fileno(fp), 0))
return 1;
rewind(fp);
return 0;
}
int object_truncate(const char *path, off_t size)
{
char *encoded = curl_escape(path, 0);
int response;
if (size == 0)
{
FILE *fp = fopen("/dev/null", "r");
response = send_request("PUT", encoded, fp, NULL, NULL);
fclose(fp);
}
else
{//TODO: this is busted
response = send_request("GET", encoded, NULL, NULL, NULL);
}
curl_free(encoded);
return (response >= 200 && response < 300);
}
int list_directory(const char *path, dir_entry **dir_list)
{
char container[MAX_PATH_SIZE * 3] = "";
char object[MAX_PATH_SIZE] = "";
int prefix_length = 0;
int response = 0;
int retval = 0;
int entry_count = 0;
*dir_list = NULL;
xmlNode *onode = NULL, *anode = NULL, *text_node = NULL;
xmlParserCtxtPtr xmlctx = xmlCreatePushParserCtxt(NULL, NULL, "", 0, NULL);
if (!strcmp(path, "") || !strcmp(path, "/"))
{
path = "";
strncpy(container, "/?format=xml", sizeof(container));
}
else
{
sscanf(path, "/%[^/]/%[^\n]", container, object);
char *encoded_container = curl_escape(container, 0);
char *encoded_object = curl_escape(object, 0);
// The empty path doesn't get a trailing slash, everything else does
char *trailing_slash;
prefix_length = strlen(object);
if (object[0] == 0) {
trailing_slash = "";
} else {
trailing_slash = "/";
prefix_length++;
}
snprintf(container, sizeof(container), "%s?format=xml&delimiter=/&prefix=%s%s",
encoded_container, encoded_object, trailing_slash);
curl_free(encoded_container);
curl_free(encoded_object);
}
response = send_request("GET", container, NULL, xmlctx, NULL);
xmlParseChunk(xmlctx, "", 0, 1);
if (xmlctx->wellFormed && response >= 200 && response < 300)
{
xmlNode *root_element = xmlDocGetRootElement(xmlctx->myDoc);
for (onode = root_element->children; onode; onode = onode->next)
{
if (onode->type != XML_ELEMENT_NODE) continue;
char is_object = !strcasecmp((const char *)onode->name, "object");
char is_container = !strcasecmp((const char *)onode->name, "container");
char is_subdir = !strcasecmp((const char *)onode->name, "subdir");
if (is_object || is_container || is_subdir)
{
entry_count++;
dir_entry *de = (dir_entry *)malloc(sizeof(dir_entry));
de->size = 0;
de->last_modified = time(NULL);
if (is_container || is_subdir) {
de->content_type = strdup("application/directory");
}
for (anode = onode->children; anode; anode = anode->next)
{
char *content = "<?!?>";
for (text_node = anode->children; text_node; text_node = text_node->next)
if (text_node->type == XML_TEXT_NODE)
content = (char *)text_node->content;
if (!strcasecmp((const char *)anode->name, "name"))
{
de->name = strdup(content + prefix_length);
// Remove trailing slash
char * slash = strrchr(de->name, '/');
if (slash && (0 == *(slash + 1))) {
*slash = 0;
}
if (asprintf(&(de->full_name), "%s/%s", path, de->name) < 0)
de->full_name = NULL;
}
if (!strcasecmp((const char *)anode->name, "bytes"))
de->size = strtoll(content, NULL, 10);
if (!strcasecmp((const char *)anode->name, "content_type"))
{
de->content_type = strdup(content);
char *semicolon = strchr(de->content_type, ';');
if (semicolon)
*semicolon = '\0';
}
if (!strcasecmp((const char *)anode->name, "last_modified"))
{
struct tm last_modified;
strptime(content, "%FT%T", &last_modified);
de->last_modified = mktime(&last_modified);
}
}
de->isdir = de->content_type &&
((strstr(de->content_type, "application/folder") != NULL) ||
(strstr(de->content_type, "application/directory") != NULL));
de->next = *dir_list;
*dir_list = de;
} else {
debugf("unknown element: %s", onode->name);
}
}
retval = 1;
}
debugf("entry count: %d", entry_count);
xmlFreeDoc(xmlctx->myDoc);
xmlFreeParserCtxt(xmlctx);
return retval;
}
void free_dir_list(dir_entry *dir_list)
{
while (dir_list)
{
dir_entry *de = dir_list;
dir_list = dir_list->next;
free(de->name);
free(de->content_type);
free(de);
}
}
int delete_object(const char *path)
{
char *encoded = curl_escape(path, 0);
int response = send_request("DELETE", encoded, NULL, NULL, NULL);
curl_free(encoded);
return (response >= 200 && response < 300);
}
int copy_object(const char *src, const char *dst)
{
char *dst_encoded = curl_escape(dst, 0);
curl_slist *headers = NULL;
add_header(&headers, "X-Copy-From", src);
add_header(&headers, "Content-Length", "0");
int response = send_request("PUT", dst_encoded, NULL, NULL, headers);
curl_free(dst_encoded);
curl_slist_free_all(headers);
return (response >= 200 && response < 300);
}
int create_directory(const char *path)
{
char *encoded = curl_escape(path, 0);
int response = send_request("MKDIR", encoded, NULL, NULL, NULL);
curl_free(encoded);
return (response >= 200 && response < 300);
}
void cloudfs_debug(int dbg)
{
debug = dbg;
}
off_t file_size(int fd)
{
struct stat buf;
fstat(fd, &buf);
return buf.st_size;
}
int cloudfs_connect(char *username, char *tenant, char *password, char *authurl, int use_snet, int use_openstack)
{
static struct {
char username[MAX_HEADER_SIZE], password[MAX_HEADER_SIZE],
tenant[MAX_HEADER_SIZE],
authurl[MAX_URL_SIZE], use_snet, use_openstack;
} reconnect_args;
long response = -1;
static int initialized = 0;
xmlNode *top_node = NULL, *service_node = NULL, *endpoint_node = NULL;
xmlParserCtxtPtr xmlctx = NULL;
if (!initialized)
{
LIBXML_TEST_VERSION
init_locks();
curl_global_init(CURL_GLOBAL_ALL);
strncpy(reconnect_args.username, username, sizeof(reconnect_args.username));
strncpy(reconnect_args.tenant, tenant, sizeof(reconnect_args.tenant));
strncpy(reconnect_args.password, password, sizeof(reconnect_args.password));
strncpy(reconnect_args.authurl, authurl, sizeof(reconnect_args.authurl));
reconnect_args.use_snet = use_snet;
reconnect_args.use_openstack = use_openstack;
initialized = 1;
}
else
{
username = reconnect_args.username;
tenant = reconnect_args.tenant;
password = reconnect_args.password;
authurl = reconnect_args.authurl;
use_snet = reconnect_args.use_snet;
use_openstack = reconnect_args.use_openstack;
}
char postdata[2048];
if (use_openstack) {
int count = snprintf(postdata, sizeof(postdata),
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
"<auth xmlns=\"http://docs.openstack.org/identity/api/v2.0\" tenantName=\"%s\">"
"<passwordCredentials username=\"%s\" password=\"%s\"/>"
"</auth>",
tenant, username, password);
if (count >= sizeof(postdata)) {
debugf("Authentication data too large for buffer");
return 0;
}
}
pthread_mutex_lock(&pool_mut);
debugf("Authenticating...");
storage_token[0] = storage_url[0] = '\0';
storage_use_openstack = use_openstack;
CURL *curl = curl_easy_init();
curl_slist *headers = NULL;
if (!use_openstack) {
add_header(&headers, "X-Auth-User", username);
add_header(&headers, "X-Auth-Key", password);
} else {
add_header(&headers, "Content-Type", "application/xml");
add_header(&headers, "Accept", "application/xml");
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(postdata));
}
curl_easy_setopt(curl, CURLOPT_VERBOSE, debug);
curl_easy_setopt(curl, CURLOPT_URL, authurl);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, &header_dispatch);
curl_easy_setopt(curl, CURLOPT_USERAGENT, USER_AGENT);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
if (use_openstack) {
xmlctx = xmlCreatePushParserCtxt(NULL, NULL, "", 0, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, xmlctx);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &xml_dispatch);
}
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
if (use_openstack) {
xmlParseChunk(xmlctx, "", 0, 1);
if (xmlctx->wellFormed && response >= 200 && response < 300)
{
xmlNode *root_element = xmlDocGetRootElement(xmlctx->myDoc);
for (top_node = root_element->children; top_node; top_node = top_node->next) {
if ((top_node->type == XML_ELEMENT_NODE) &&
(!strcasecmp((const char *)top_node->name, "serviceCatalog")))
{
for (service_node = top_node->children; service_node; service_node = service_node->next)
if ((service_node->type == XML_ELEMENT_NODE) &&
(!strcasecmp((const char *)service_node->name, "service")))
{
xmlChar * serviceType = xmlGetProp(service_node, "type");
int isObjectStore = serviceType && !strcasecmp(serviceType, "object-store");
xmlFree(serviceType);
if (!isObjectStore) continue;
for (endpoint_node = service_node->children; endpoint_node; endpoint_node = endpoint_node->next)
if ((endpoint_node->type == XML_ELEMENT_NODE) &&
(!strcasecmp((const char *)endpoint_node->name, "endpoint")))
{
xmlChar * publicURL = xmlGetProp(endpoint_node, "publicURL");
if (publicURL) {
char copy = 1;
if (storage_url[0]) {
if (strstr(publicURL, "cdn")) {
copy = 0;
debugf("Warning - found multiple object-store services; keeping %s, ignoring %s",
storage_url, publicURL);
} else {
debugf("Warning - found multiple object-store services; using %s instead of %s",
publicURL, storage_url);
}
}
if (copy) {
strncpy(storage_url, publicURL, sizeof(storage_url));
}
}
xmlFree(publicURL);
}
}
}
if ((top_node->type == XML_ELEMENT_NODE) &&
(!strcasecmp((const char *)top_node->name, "token")))
{
xmlChar * tokenId = xmlGetProp(top_node, "id");
if (tokenId) {
if (storage_token[0]) {
debugf("Warning - found multiple authentication tokens.");
}
strncpy(storage_token, tokenId, sizeof(storage_token));
}
xmlFree(tokenId);
}
}
}
xmlFreeParserCtxt(xmlctx);
} else {
if (use_snet && storage_url[0])
rewrite_url_snet(storage_url);
}
pthread_mutex_unlock(&pool_mut);
return (response >= 200 && response < 300 && storage_token[0] && storage_url[0]);
}
size_t header_dispatch(void *ptr, size_t size, size_t nmemb, void *stream)
{
char *header = (char *)alloca(size * nmemb + 1);
char *head = (char *)alloca(size * nmemb + 1);
char *value = (char *)alloca(size * nmemb + 1);
memcpy(header, (char *)ptr, size * nmemb);
header[size * nmemb] = '\0';
if (sscanf(header, "%[^:]: %[^\r\n]", head, value) == 2)
{
if (!strncasecmp(head, "x-auth-token", size * nmemb))
strncpy(storage_token, value, sizeof(storage_token));
if (!strncasecmp(head, "x-storage-url", size * nmemb))
strncpy(storage_url, value, sizeof(storage_url));
}
return size * nmemb;
}
void debugf(char *fmt, ...)
{
if (debug)
{
va_list args;
va_start(args, fmt);
fputs("!!! ", stderr);
vfprintf(stderr, fmt, args);
va_end(args);
putc('\n', stderr);
}
}