summaryrefslogtreecommitdiffstats
path: root/xlators/features/cloudsync/src/cloudsync-plugins/src/cloudsyncs3/src/libcloudsyncs3.c
blob: e36bf71e4ae74a594279bcefe0247803d9b78bb0 (plain)
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
/*
  Copyright (c) 2018 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

#include <stdlib.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/crypto.h>
#include <curl/curl.h>
#include "xlator.h"
#include "glusterfs.h"
#include "libcloudsyncs3.h"
#include "cloudsync-common.h"

#define RESOURCE_SIZE 4096

store_methods_t store_ops = {
        .fop_download = aws_download_s3,
        .fop_init     = aws_init,
        .fop_reconfigure = aws_reconfigure,
        .fop_fini     = aws_fini,
};

typedef struct aws_private {
        char *hostname;
        char *bucketid;
        char *awssekey;
        char *awskeyid;
        gf_boolean_t abortdl;
        pthread_spinlock_t lock;
} aws_private_t;

void *
aws_init (xlator_t *this)
{
        aws_private_t  *priv = NULL;
        char           *temp_str = NULL;
        int             ret = 0;

        priv = GF_CALLOC (1, sizeof (aws_private_t),
                          gf_libaws_mt_aws_private_t);
        if (!priv) {
                gf_msg (this->name, GF_LOG_ERROR, 0, 0, "insufficient memory");
                return NULL;
        }

        priv->abortdl = _gf_false;

        pthread_spin_init (&priv->lock, PTHREAD_PROCESS_PRIVATE);

        pthread_spin_lock (&(priv->lock));
        {
        if (dict_get_str (this->options, "s3plugin-seckey",
            &temp_str) == 0) {
                priv->awssekey = gf_strdup (temp_str);
                if (!priv->awssekey) {
                        gf_msg (this->name, GF_LOG_ERROR, ENOMEM, 0,
                                "initializing aws secret key failed");
                        ret = -1;
                        goto unlock;
                }
        }

        if (dict_get_str (this->options, "s3plugin-keyid",
            &temp_str) == 0) {
                priv->awskeyid = gf_strdup (temp_str);
                if (!priv->awskeyid) {
                        gf_msg (this->name, GF_LOG_ERROR, ENOMEM, 0,
                                "initializing aws key ID failed");
                        ret = -1;
                        goto unlock;
                }
        }

        if (dict_get_str (this->options, "s3plugin-bucketid",
            &temp_str) == 0) {
                priv->bucketid = gf_strdup (temp_str);
                if (!priv->bucketid) {
                        gf_msg (this->name, GF_LOG_ERROR, ENOMEM, 0,
                                "initializing aws bucketid failed");

                        ret = -1;
                        goto unlock;
                }
        }

        if (dict_get_str (this->options, "s3plugin-hostname",
            &temp_str) == 0) {
                priv->hostname = gf_strdup (temp_str);
                if (!priv->hostname) {
                        gf_msg (this->name, GF_LOG_ERROR, ENOMEM, 0,
                                "initializing aws hostname failed");

                        ret = -1;
                        goto unlock;
                }
        }

        gf_msg_debug (this->name,  0, "stored key: %s id: %s "
                      "bucketid %s hostname: %s", priv->awssekey,
                      priv->awskeyid, priv->bucketid, priv->hostname);

        }
unlock:
        pthread_spin_unlock (&(priv->lock));

        if (ret  == -1) {
                GF_FREE (priv->awskeyid);
                GF_FREE (priv->awssekey);
                GF_FREE (priv->bucketid);
                GF_FREE (priv->hostname);
                GF_FREE (priv);
                priv = NULL;
        }

        return (void *)priv;
}

int
aws_reconfigure (xlator_t *this, dict_t *options)
{
        aws_private_t  *priv = NULL;
        char           *temp_str = NULL;
        int             ret = 0;
        cs_private_t   *cspriv = NULL;

        cspriv = this->private;

        priv = cspriv->stores->config;

        if (!priv) {
                gf_msg (this->name, GF_LOG_ERROR, 0, 0, "null priv");
                return -1;
        }

        pthread_spin_lock (&(priv->lock));
        {
        if (dict_get_str (options, "s3plugin-seckey",
            &temp_str) == 0) {
                priv->awssekey = gf_strdup (temp_str);
                if (!priv->awssekey) {
                        gf_msg (this->name, GF_LOG_ERROR, ENOMEM, 0,
                                "initializing aws secret key failed");
                        ret = -1;
                        goto out;
                }
        }

        if (dict_get_str (options, "s3plugin-keyid",
            &temp_str) == 0) {
                priv->awskeyid = gf_strdup (temp_str);
                if (!priv->awskeyid) {
                        gf_msg (this->name, GF_LOG_ERROR, ENOMEM, 0,
                                "initializing aws key ID failed");
                        ret = -1;
                        goto out;
                }
        }

        if (dict_get_str (options, "s3plugin-bucketid",
            &temp_str) == 0) {
                priv->bucketid = gf_strdup (temp_str);
                if (!priv->bucketid) {
                        gf_msg (this->name, GF_LOG_ERROR, ENOMEM, 0,
                                "initializing aws bucketid failed");
                        ret = -1;
                        goto out;
                }
        }

        if (dict_get_str (options, "s3plugin-hostname",
            &temp_str) == 0) {
                priv->hostname = gf_strdup (temp_str);
                if (!priv->hostname) {
                        gf_msg (this->name, GF_LOG_ERROR, ENOMEM, 0,
                                "initializing aws hostname failed");
                        ret = -1;
                        goto out;
                }
        }

        }
out:
        pthread_spin_unlock (&(priv->lock));

        gf_msg_debug (this->name,  0, "stored key: %s id: %s "
                      "bucketid %s hostname: %s", priv->awssekey,
                      priv->awskeyid, priv->bucketid, priv->hostname);

        return ret;
}

void
aws_fini (void *config)
{
        aws_private_t *priv = NULL;

        priv = (aws_private_t *)priv;

        if (priv) {
                GF_FREE (priv->hostname);
                GF_FREE (priv->bucketid);
                GF_FREE (priv->awssekey);
                GF_FREE (priv->awskeyid);

                pthread_spin_destroy (&priv->lock);
                GF_FREE (priv);
        }
}

int32_t
mem_acct_init (xlator_t *this)
{
        int     ret = -1;

        GF_VALIDATE_OR_GOTO ("dht", this, out);

        ret = xlator_mem_acct_init (this, gf_libaws_mt_end + 1);

        if (ret != 0) {
                gf_msg (this->name, GF_LOG_ERROR, 0, 0,
                        "Memory accounting init failed");
                return ret;
        }
out:
        return ret;
}
char *
aws_form_request (char *resource, char **date, char *reqtype, char *bucketid,
                  char *filepath)
{
        char            httpdate[256];
        time_t          ctime;
        struct tm      *gtime = NULL;
        char           *sign_req = NULL;
        int             signreq_len = -1;
        int             date_len = -1;
        int             res_len = -1;

        ctime = time(NULL);
        gtime = gmtime(&ctime);

        date_len = strftime (httpdate, sizeof(httpdate),
                             "%a, %d %b %Y %H:%M:%S +0000", gtime);

        *date = gf_strndup (httpdate, date_len);
        if (*date == NULL) {
                gf_msg ("CS", GF_LOG_ERROR, ENOMEM, 0, "memory allocation "
                        "failure for date");
                goto out;
        }

        res_len = snprintf(resource, RESOURCE_SIZE, "%s/%s", bucketid,
                           filepath);

        gf_msg_debug ("CS", 0, "resource %s", resource);

        /* 6 accounts for the 4 new line chars, one forward slash and
         * one null char */
        signreq_len = res_len + date_len + strlen(reqtype) + 6;

        sign_req = GF_MALLOC (signreq_len, gf_common_mt_char);
        if (sign_req == NULL) {
                gf_msg ("CS", GF_LOG_ERROR, ENOMEM, 0, "memory allocation "
                        "failure for sign_req");
                goto out;
        }

        snprintf(sign_req, signreq_len, "%s\n\n%s\n%s\n/%s",
                 reqtype,
                 "",
                 *date,
                 resource);

out:
        return sign_req;
}

char*
aws_b64_encode(const unsigned char *input, int length)
{
        BIO     *bio, *b64;
        BUF_MEM *bptr;
        char    *buff = NULL;

        b64 = BIO_new(BIO_f_base64());
        bio = BIO_new(BIO_s_mem());
        b64 = BIO_push(b64, bio);
        BIO_write(b64, input, length);
        BIO_flush(b64);
        BIO_get_mem_ptr(b64, &bptr);

        buff = GF_MALLOC(bptr->length, gf_common_mt_char);
        memcpy(buff, bptr->data, bptr->length - 1);
        buff[bptr->length - 1] = 0;

        BIO_free_all(b64);

        return buff;
}

char *
aws_sign_request(char *const str, char *awssekey)
{
#if (OPENSSL_VERSION_NUMBER < 0x1010002f)
        HMAC_CTX  ctx;
#endif
        HMAC_CTX *pctx = NULL;;

        unsigned char md[256];
        unsigned len;
        char *base64 = NULL;

#if (OPENSSL_VERSION_NUMBER < 0x1010002f)
        HMAC_CTX_init (&ctx);
        pctx = &ctx;
#else
        pctx = HMAC_CTX_new ();
#endif
        HMAC_Init_ex (pctx, awssekey, strlen(awssekey), EVP_sha1(), NULL);
        HMAC_Update (pctx, (unsigned char *)str, strlen(str));
        HMAC_Final (pctx, (unsigned char *)md, &len);

#if (OPENSSL_VERSION_NUMBER < 0x1010002f)
        HMAC_CTX_cleanup (pctx);
#else
        HMAC_CTX_free (pctx);
#endif
        base64 = aws_b64_encode(md, len);

        return base64;
}

int
aws_dlwritev_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                 int op_ret, int op_errno, struct iatt *prebuf,
                 struct iatt *postbuf, dict_t *xdata)
{
        aws_private_t    *priv = NULL;

        if (op_ret == -1) {
                gf_msg (this->name, GF_LOG_ERROR, 0, op_errno, "write failed "
                        ". Aborting Download");

                priv = this->private;
                pthread_spin_lock (&(priv->lock));
                {
                        priv->abortdl = _gf_true;
                }
                pthread_spin_unlock (&(priv->lock));
        }

        CS_STACK_DESTROY (frame);

        return op_ret;
}

size_t
aws_write_callback (void *dlbuf, size_t size, size_t nitems, void *mainframe)
{
        call_frame_t            *frame = NULL;
        fd_t                    *dlfd = NULL;
        int                      ret = 0;
        cs_local_t              *local = NULL;
        struct iovec             iov = {0,};
        struct iobref           *iobref = NULL;
        struct iobuf            *iobuf = NULL;
        struct iovec             dliov = {0, };
        size_t                   tsize = 0;
        xlator_t                *this = NULL;
        cs_private_t            *xl_priv = NULL;
        aws_private_t           *priv = NULL;
        call_frame_t            *dlframe = NULL;

        frame = (call_frame_t *)mainframe;
        this = frame->this;
        xl_priv = this->private;
        priv = xl_priv->stores->config;

        pthread_spin_lock (&(priv->lock));
        {
                /* returning size other than the size passed from curl will
                 * abort further download*/
                if (priv->abortdl) {
                        gf_msg (this->name, GF_LOG_ERROR, 0, 0,
                                "aborting download");
                        pthread_spin_unlock (&(priv->lock));
                        return 0;
                }
        }
        pthread_spin_unlock (&(priv->lock));

        local = frame->local;
        dlfd = local->dlfd;
        tsize = size * nitems;

        dliov.iov_base = (void *)dlbuf;
        dliov.iov_len = tsize;

        ret = iobuf_copy (this->ctx->iobuf_pool, &dliov, 1, &iobref, &iobuf,
                          &iov);
        if (ret) {
                gf_msg (this->name, GF_LOG_ERROR, 0, 0, "iobuf_copy failed");
                goto out;
        }

        /* copy frame */
        dlframe = copy_frame (frame);
        if (!dlframe) {
                gf_msg (this->name, GF_LOG_ERROR, 0, 0, "copy_frame failed");
                tsize = 0;
                goto out;
        }

        STACK_WIND (dlframe, aws_dlwritev_cbk,  FIRST_CHILD (this),
                    FIRST_CHILD (this)->fops->writev, dlfd,
                    &iov, 1, local->dloffset, 0, iobref, NULL);

        local->dloffset += tsize;

out:
        if (iobuf)
                iobuf_unref (iobuf);
        if (iobref)
                iobref_unref (iobref);

        return tsize;
}

int
aws_download_s3 (call_frame_t *frame, void *config)
{
        char                   *buf;
        int                     bufsize = -1;
        CURL                   *handle = NULL;
        struct curl_slist      *slist = NULL;
        struct curl_slist      *tmp = NULL;
        xlator_t               *this = NULL;
        int                     ret = 0;
        int                     debug = 1;
        CURLcode                res;
        char                    errbuf[CURL_ERROR_SIZE];
        size_t                  len = 0;
        long                    responsecode;
        char                    *sign_req       = NULL;
        char                    *date           = NULL;
        char                    *const reqtype  = "GET";
        char                    *signature      = NULL;
        cs_local_t              *local          = NULL;
        char                    resource[RESOURCE_SIZE] = {0,};
        aws_private_t           *priv           = NULL;

        local = frame->local;

        priv = (aws_private_t *)config;

        if (!priv->bucketid || !priv->hostname || !priv->awssekey ||
            !priv->awskeyid) {
                ret = -1;
                goto out;
        }

        sign_req = aws_form_request (resource, &date, reqtype, priv->bucketid,
                                    local->remotepath);
        if (!sign_req) {
                gf_msg (this->name, GF_LOG_ERROR, 0, 0, "null sign_req, "
                        "aborting download");
                ret = -1;
                goto out;
        }

        gf_msg_debug ("CS", 0, "sign_req %s date %s", sign_req, date);

        signature = aws_sign_request (sign_req, priv->awssekey);
        if (!signature) {
                gf_msg ("CS", GF_LOG_ERROR, 0, 0, "null signature, "
                        "aborting download");
                ret = -1;
                goto out;
        }

        handle = curl_easy_init();
        this = frame->this;

        /* special numbers 6, 20, 10 accounts for static characters in the
         * below snprintf string format arguments*/
        bufsize = strlen(date) + 6 + strlen(priv->awskeyid) + strlen(signature)
                  + 20 + strlen(priv->hostname) + 10;

        buf = (char *)alloca(bufsize);
        if (!buf) {
                gf_msg ("CS", GF_LOG_ERROR, ENOMEM, 0, "mem allocation "
                        "failed for buf");
                ret = -1;
                goto out;
        }

        snprintf (buf, bufsize, "Date: %s", date);
        slist = curl_slist_append(slist, buf);
        snprintf (buf, bufsize, "Authorization: AWS %s:%s", priv->awskeyid,
                  signature);
        slist = curl_slist_append(slist, buf);
        snprintf(buf, bufsize, "https://%s/%s", priv->hostname, resource);

        if (gf_log_get_loglevel () >= GF_LOG_DEBUG) {
                tmp = slist;
                while (tmp) {
                        gf_msg_debug (this->name, 0, "slist for curl - %s",
                                      tmp->data);
                        tmp = tmp->next;
                }
        }

        curl_easy_setopt (handle, CURLOPT_HTTPHEADER, slist);
        curl_easy_setopt (handle, CURLOPT_URL, buf);
        curl_easy_setopt (handle, CURLOPT_WRITEFUNCTION, aws_write_callback);
        curl_easy_setopt (handle, CURLOPT_WRITEDATA, frame);
        curl_easy_setopt (handle, CURLOPT_VERBOSE, debug);
        curl_easy_setopt (handle, CURLOPT_ERRORBUFFER, errbuf);

        res = curl_easy_perform(handle);
        if (res != CURLE_OK) {
                gf_msg (this->name, GF_LOG_ERROR, 0, 0,
                        "download failed. err: %s\n", curl_easy_strerror(res));
                ret = -1;
                len = strlen(errbuf);
                if (len) {
                        gf_msg (this->name, GF_LOG_ERROR, 0, 0,
                                "curl failure %s", errbuf);
                } else {
                        gf_msg (this->name, GF_LOG_ERROR, 0, 0, "curl error "
                                "%s\n", curl_easy_strerror(res));
                }
        }

        if (res == CURLE_OK) {
                curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE,
                                   &responsecode);
                gf_msg_debug (this->name, 0, "response code %ld", responsecode);
                if (responsecode != 200) {
                        ret = -1;
                        gf_msg (this->name, GF_LOG_ERROR, 0, 0,
                                "curl download failed");
                }
        }

        curl_slist_free_all(slist);
        curl_easy_cleanup(handle);

out:
        if (sign_req)
                GF_FREE (sign_req);
        if (date)
                GF_FREE (date);
        if (signature)
                GF_FREE (signature);

        return ret;
}

struct volume_options cs_options[] = {
        { .key        = {"s3plugin-seckey"},
          .type       =  GF_OPTION_TYPE_STR,
          .description = "aws secret key"
        },
        { .key        = {"s3plugin-keyid"},
          .type       =  GF_OPTION_TYPE_STR,
          .description = "aws key ID"

        },
        { .key        = {"s3plugin-bucketid"},
          .type       =  GF_OPTION_TYPE_STR,
          .description = "aws bucketid"
        },
        { .key        = {"s3plugin-hostname"},
          .type       =  GF_OPTION_TYPE_STR,
          .description = "aws hostname e.g. s3.amazonaws.com"
        },
        { .key         = {NULL} },
};