summaryrefslogtreecommitdiffstats
path: root/storage/maria/libmarias3/src/marias3.c
blob: b146da55b0644c9717e7ee8f5a8365a1d0bd7e05 (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
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
/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 * Copyright 2019 MariaDB Corporation Ab. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */

#include "config.h"
#include "common.h"

#include <pthread.h>
#include <arpa/inet.h>
#include <netinet/in.h>

ms3_malloc_callback ms3_cmalloc = (ms3_malloc_callback)malloc;
ms3_free_callback ms3_cfree = (ms3_free_callback)free;
ms3_realloc_callback ms3_crealloc = (ms3_realloc_callback)realloc;
ms3_strdup_callback ms3_cstrdup = (ms3_strdup_callback)strdup;
ms3_calloc_callback ms3_ccalloc = (ms3_calloc_callback)calloc;


/* Thread locking code for OpenSSL < 1.1.0 */
#include <dlfcn.h>
#ifndef RTLD_DEFAULT
#define RTLD_DEFAULT ((void *)0)
#endif
static pthread_mutex_t *mutex_buf = NULL;
#define CRYPTO_LOCK 1
static void (*openssl_set_id_callback)(unsigned long (*func)(void));
static void (*openssl_set_locking_callback)(void (*func)(int mode,int type, const char *file,int line));
static int (*openssl_num_locks)(void);

static void locking_function(int mode, int n, const char *file, int line)
{
  (void) file;
  (void) line;
  if(mode & CRYPTO_LOCK)
    pthread_mutex_lock(&(mutex_buf[n]));
  else
    pthread_mutex_unlock(&(mutex_buf[n]));
}

static int curl_needs_openssl_locking(void)
{
  curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);

  if (data->ssl_version == NULL)
  {
    return 0;
  }

  if (strncmp(data->ssl_version, "OpenSSL", 7) != 0)
  {
    return 0;
  }
  if (data->ssl_version[8] == '0')
  {
    return 1;
  }
  if ((data->ssl_version[8] == '1') && (data->ssl_version[10] == '0'))
  {
    openssl_set_id_callback = dlsym(RTLD_DEFAULT, "CRYPTO_set_id_callback");
    openssl_set_locking_callback = dlsym(RTLD_DEFAULT, "CRYPTO_set_locking_callback");
    openssl_num_locks = dlsym(RTLD_DEFAULT, "CRYPTO_num_locks");
    return openssl_set_id_callback != NULL &&
           openssl_set_locking_callback != NULL &&
           openssl_num_locks != NULL;
  }
  return 0;
}

static unsigned long __attribute__((unused)) id_function(void)
{
  return ((unsigned long)pthread_self());
}

uint8_t ms3_library_init_malloc(ms3_malloc_callback m,
                                ms3_free_callback f, ms3_realloc_callback r,
                                ms3_strdup_callback s, ms3_calloc_callback c)
{
  if (!m || !f || !r || !s || !c)
  {
    return MS3_ERR_PARAMETER;
  }

  ms3_cmalloc = m;
  ms3_cfree = f;
  ms3_crealloc = r;
  ms3_cstrdup = s;
  ms3_ccalloc = c;

  if (curl_needs_openssl_locking())
  {
    int i;
    mutex_buf = ms3_cmalloc(openssl_num_locks() * sizeof(pthread_mutex_t));
    if(mutex_buf)
    {
      for(i = 0; i < openssl_num_locks(); i++)
        pthread_mutex_init(&(mutex_buf[i]), NULL);
      openssl_set_id_callback(id_function);
      openssl_set_locking_callback(locking_function);
    }
  }

  if (curl_global_init_mem(CURL_GLOBAL_DEFAULT, m, f, r, s, c))
  {
    return MS3_ERR_PARAMETER;
  }

  return 0;
}

void ms3_library_init(void)
{
  if (curl_needs_openssl_locking())
  {
    int i;
    mutex_buf = malloc(openssl_num_locks() * sizeof(pthread_mutex_t));
    if(mutex_buf)
    {
      for(i = 0; i < openssl_num_locks(); i++)
        pthread_mutex_init(&(mutex_buf[i]), NULL);
      openssl_set_id_callback(id_function);
      openssl_set_locking_callback(locking_function);
    }
  }
  curl_global_init(CURL_GLOBAL_DEFAULT);
}

void ms3_library_deinit(void)
{
  int i;
  if (mutex_buf)
  {
    openssl_set_id_callback(NULL);
    openssl_set_locking_callback(NULL);
    for(i = 0;  i < openssl_num_locks();  i++)
      pthread_mutex_destroy(&(mutex_buf[i]));
    ms3_cfree(mutex_buf);
    mutex_buf = NULL;
  }
  curl_global_cleanup();
}

ms3_st *ms3_init(const char *s3key, const char *s3secret,
                 const char *region,
                 const char *base_domain)
{
  ms3_st *ms3;

  if ((s3key == NULL) || (s3secret == NULL))
  {
    return NULL;
  }

  ms3 = ms3_cmalloc(sizeof(ms3_st));

  ms3->s3key = ms3_cstrdup(s3key);
  ms3->s3secret = ms3_cstrdup(s3secret);
  ms3->region = ms3_cstrdup(region);
  ms3->port = 0; /* The default value */

  if (base_domain && strlen(base_domain))
  {
    struct sockaddr_in sa;
    ms3->base_domain = ms3_cstrdup(base_domain);
    if (inet_pton(AF_INET, base_domain, &(sa.sin_addr)))
    {
      ms3->list_version = 1;
      ms3->protocol_version = 1;
    }
    else if (strcmp(base_domain, "s3.amazonaws.com") == 0)
    {
      ms3->list_version = 2;
      ms3->protocol_version = 2;
    }
    else
    {
      // Assume that S3-compatible APIs can't support v2 list
      ms3->list_version = 1;
      ms3->protocol_version = 2;
    }
  }
  else
  {
    ms3->base_domain = NULL;
    ms3->list_version = 2;
    ms3->protocol_version = 2;
  }

  ms3->buffer_chunk_size = READ_BUFFER_DEFAULT_SIZE;

  ms3->curl = curl_easy_init();
  ms3->last_error = NULL;
  ms3->use_http = false;
  ms3->disable_verification = false;
  ms3->first_run = true;
  ms3->path_buffer = ms3_cmalloc(sizeof(char) * 1024);
  ms3->query_buffer = ms3_cmalloc(sizeof(char) * 3072);
  ms3->list_container.pool = NULL;
  ms3->list_container.next = NULL;
  ms3->list_container.start = NULL;
  ms3->list_container.pool_list = NULL;
  ms3->list_container.pool_free = 0;
  ms3->read_cb= 0;
  ms3->user_data= 0;

  ms3->iam_role = NULL;
  ms3->role_key = NULL;
  ms3->role_secret = NULL;
  ms3->role_session_token = NULL;
  ms3->iam_endpoint = NULL;
  ms3->sts_endpoint = NULL;
  ms3->sts_region = NULL;
  ms3->iam_role_arn = NULL;

  return ms3;
}

uint8_t ms3_init_assume_role(ms3_st *ms3, const char *iam_role, const char *sts_endpoint, const char *sts_region)
{
  uint8_t ret=0;

  if (iam_role == NULL)
  {
      return MS3_ERR_PARAMETER;
  }
  ms3->iam_role = ms3_cstrdup(iam_role);

  if (sts_endpoint && strlen(sts_endpoint))
  {
      ms3->sts_endpoint = ms3_cstrdup(sts_endpoint);
  }
  else
  {
      ms3->sts_endpoint = ms3_cstrdup("sts.amazonaws.com");
  }

  if (sts_region && strlen(sts_region))
  {
      ms3->sts_region = ms3_cstrdup(sts_region);
  }
  else
  {
      ms3->sts_region = ms3_cstrdup("us-east-1");
  }

  ms3->iam_endpoint = ms3_cstrdup("iam.amazonaws.com");

  ms3->iam_role_arn = ms3_cmalloc(sizeof(char) * 2048);
  ms3->iam_role_arn[0] = '\0';
  ms3->role_key = ms3_cmalloc(sizeof(char) * 128);
  ms3->role_key[0] = '\0';
  ms3->role_secret = ms3_cmalloc(sizeof(char) * 1024);
  ms3->role_secret[0] = '\0';
  // aws says theres no maximum length here.. 2048 might be overkill
  ms3->role_session_token = ms3_cmalloc(sizeof(char) * 2048);
  ms3->role_session_token[0] = '\0';
  // 0 will uses the default and not set a value in the request
  ms3->role_session_duration = 0;

  ret = ms3_assume_role(ms3);

  return ret;
}

uint8_t ms3_ec2_set_cred(ms3_st *ms3, const char *iam_role,
                     const char *s3key, const char *s3secret,
                     const char *token)
{
  uint8_t ret=0;

  if (iam_role == NULL || token == NULL || s3key == NULL || s3secret == NULL)
  {
      return MS3_ERR_PARAMETER;
  }
  ms3->iam_role = ms3_cstrdup(iam_role);
  ms3->role_key = ms3_cstrdup(s3key);
  ms3->role_secret = ms3_cstrdup(s3secret);
  ms3->role_session_token = ms3_cstrdup(token);

  return ret;
}

static void list_free(ms3_st *ms3)
{
  ms3_list_st *list = ms3->list_container.start;
  struct ms3_pool_alloc_list_st *plist = NULL, *next = NULL;
  while (list)
  {
    ms3_cfree(list->key);
    list = list->next;
  }
  plist = ms3->list_container.pool_list;
  while (plist)
  {
    next = plist->prev;
    ms3_cfree(plist->pool);
    ms3_cfree(plist);
    plist = next;
  }
  ms3->list_container.pool = NULL;
  ms3->list_container.next = NULL;
  ms3->list_container.start = NULL;
  ms3->list_container.pool_list = NULL;
  ms3->list_container.pool_free = 0;
}

void ms3_deinit(ms3_st *ms3)
{
  if (!ms3)
  {
    return;
  }

  ms3debug("deinit: 0x%" PRIXPTR, (uintptr_t)ms3);
  ms3_cfree(ms3->s3secret);
  ms3_cfree(ms3->s3key);
  ms3_cfree(ms3->region);
  ms3_cfree(ms3->base_domain);
  ms3_cfree(ms3->iam_role);
  ms3_cfree(ms3->role_key);
  ms3_cfree(ms3->role_secret);
  ms3_cfree(ms3->role_session_token);
  ms3_cfree(ms3->iam_endpoint);
  ms3_cfree(ms3->sts_endpoint);
  ms3_cfree(ms3->sts_region);
  ms3_cfree(ms3->iam_role_arn);
  curl_easy_cleanup(ms3->curl);
  ms3_cfree(ms3->last_error);
  ms3_cfree(ms3->path_buffer);
  ms3_cfree(ms3->query_buffer);
  list_free(ms3);
  ms3_cfree(ms3);
}

const char *ms3_server_error(ms3_st *ms3)
{
  if (!ms3)
  {
    return NULL;
  }

  return ms3->last_error;
}

void ms3_debug(int debug_state)
{
  bool state = ms3debug_get();
  if (state != (bool) debug_state)
  {
    ms3debug_set((bool) debug_state);
    if (debug_state)
    {
      ms3debug("enabling debug");
    }
  }
}

const char *ms3_error(uint8_t errcode)
{
  if (errcode >= MS3_ERR_MAX)
  {
    return baderror;
  }

  return errmsgs[errcode];
}

uint8_t ms3_list_dir(ms3_st *ms3, const char *bucket, const char *prefix,
                     ms3_list_st **list)
{
  uint8_t res = 0;

  if (!ms3 || !bucket || !list)
  {
    return MS3_ERR_PARAMETER;
  }

  list_free(ms3);
  res = execute_request(ms3, MS3_CMD_LIST, bucket, NULL, NULL, NULL, prefix, NULL,
                        0, NULL,
                        NULL);
  *list = ms3->list_container.start;
  return res;
}

uint8_t ms3_list(ms3_st *ms3, const char *bucket, const char *prefix,
                 ms3_list_st **list)
{
  uint8_t res = 0;

  if (!ms3 || !bucket || !list)
  {
    return MS3_ERR_PARAMETER;
  }

  list_free(ms3);
  res = execute_request(ms3, MS3_CMD_LIST_RECURSIVE, bucket, NULL, NULL, NULL,
                        prefix, NULL,
                        0, NULL,
                        NULL);
  *list = ms3->list_container.start;
  return res;
}

uint8_t ms3_put(ms3_st *ms3, const char *bucket, const char *key,
                const uint8_t *data, size_t length)
{
  uint8_t res;

  if (!ms3 || !bucket || !key || !data)
  {
    return MS3_ERR_PARAMETER;
  }

  if (length == 0)
  {
    return MS3_ERR_NO_DATA;
  }

  // mhash can't hash more than 4GB it seems
  if (length > UINT32_MAX)
  {
    return MS3_ERR_TOO_BIG;
  }

  res = execute_request(ms3, MS3_CMD_PUT, bucket, key, NULL, NULL, NULL, data,
                        length, NULL,
                        NULL);

  return res;
}

uint8_t ms3_get(ms3_st *ms3, const char *bucket, const char *key,
                uint8_t **data, size_t *length)
{
  uint8_t res = 0;
  struct memory_buffer_st buf;

  buf.data = NULL;
  buf.length = 0;

  if (!ms3 || !bucket || !key || key[0] == '\0')
  {
    return MS3_ERR_PARAMETER;
  }
  else if (!ms3->read_cb && (!data || !length))
  {
    return MS3_ERR_PARAMETER;
  }

  res = execute_request(ms3, MS3_CMD_GET, bucket, key, NULL, NULL, NULL, NULL, 0,
                        NULL, &buf);
  if (!ms3->read_cb)
  {
    *data = buf.data;
    *length = buf.length;
  }

  return res;
}

uint8_t ms3_copy(ms3_st *ms3, const char *source_bucket, const char *source_key,
                 const char *dest_bucket, const char *dest_key)
{
  uint8_t res = 0;

  if (!ms3 || !source_bucket || !source_key || !dest_bucket || !dest_key)
  {
    return MS3_ERR_PARAMETER;
  }

  res = execute_request(ms3, MS3_CMD_COPY, dest_bucket, dest_key, source_bucket,
                        source_key, NULL, NULL, 0, NULL, NULL);
  return res;
}

uint8_t ms3_move(ms3_st *ms3, const char *source_bucket, const char *source_key,
                 const char *dest_bucket, const char *dest_key)
{
  uint8_t res = 0;

  if (!ms3 || !source_bucket || !source_key || !dest_bucket || !dest_key)
  {
    return MS3_ERR_PARAMETER;
  }

  res = ms3_copy(ms3, source_bucket, source_key, dest_bucket, dest_key);

  if (res)
  {
    return res;
  }

  res = ms3_delete(ms3, source_bucket, source_key);

  return res;
}

uint8_t ms3_delete(ms3_st *ms3, const char *bucket, const char *key)
{
  uint8_t res;

  if (!ms3 || !bucket || !key)
  {
    return MS3_ERR_PARAMETER;
  }

  res = execute_request(ms3, MS3_CMD_DELETE, bucket, key, NULL, NULL, NULL, NULL,
                        0, NULL,
                        NULL);
  return res;
}

uint8_t ms3_status(ms3_st *ms3, const char *bucket, const char *key,
                   ms3_status_st *status)
{
  uint8_t res;

  if (!ms3 || !bucket || !key || !status)
  {
    return MS3_ERR_PARAMETER;
  }

  res = execute_request(ms3, MS3_CMD_HEAD, bucket, key, NULL, NULL, NULL, NULL, 0,
                        NULL,
                        status);
  return res;
}

void ms3_list_free(ms3_list_st *list)
{
  // Deprecated
  (void) list;
}

void ms3_free(uint8_t *data)
{
  ms3_cfree(data);
}

uint8_t ms3_set_option(ms3_st *ms3, ms3_set_option_t option, void *value)
{
  if (!ms3)
  {
    return MS3_ERR_PARAMETER;
  }

  switch (option)
  {
    case MS3_OPT_USE_HTTP:
    {
      ms3->use_http = ms3->use_http ? 0 : 1;
      break;
    }

    case MS3_OPT_DISABLE_SSL_VERIFY:
    {
      ms3->disable_verification = ms3->disable_verification ? 0 : 1;
      break;
    }

    case MS3_OPT_BUFFER_CHUNK_SIZE:
    {
      size_t new_size;

      if (!value)
      {
        return MS3_ERR_PARAMETER;
      }

      new_size = *(size_t *)value;

      if (new_size < 1)
      {
        return MS3_ERR_PARAMETER;
      }

      ms3->buffer_chunk_size = new_size;
      break;
    }

    case MS3_OPT_FORCE_LIST_VERSION:
    {
      uint8_t list_version;

      if (!value)
      {
        return MS3_ERR_PARAMETER;
      }

      list_version = *(uint8_t *)value;

      if (list_version < 1 || list_version > 2)
      {
        return MS3_ERR_PARAMETER;
      }

      ms3->list_version = list_version;
      break;
    }

    case MS3_OPT_FORCE_PROTOCOL_VERSION:
    {
      uint8_t protocol_version;

      if (!value)
      {
        return MS3_ERR_PARAMETER;
      }

      protocol_version = *(uint8_t *)value;

      if (protocol_version < 1 || protocol_version > 2)
      {
        return MS3_ERR_PARAMETER;
      }

      ms3->protocol_version = protocol_version;
      break;
    }

    case MS3_OPT_PORT_NUMBER:
    {
      int port_number;

      if (!value)
      {
        return MS3_ERR_PARAMETER;
      }
      memcpy(&port_number, (void*)value, sizeof(int));

      ms3->port = port_number;
      break;
    }

    case MS3_OPT_READ_CB:
    {
      if (!value)
      {
        return MS3_ERR_PARAMETER;
      }

      ms3->read_cb = value;
      break;
    }

    case MS3_OPT_USER_DATA:
    {
      ms3->user_data = value;
      break;
    }

    default:
      return MS3_ERR_PARAMETER;
  }

  return 0;
}

uint8_t ms3_assume_role(ms3_st *ms3)
{
    uint8_t res = 0;

    if (!ms3 || !ms3->iam_role)
    {
      return MS3_ERR_PARAMETER;
    }

    if (!strstr(ms3->iam_role_arn, ms3->iam_role))
    {
        ms3debug("Lookup IAM role ARN");
        res = execute_assume_role_request(ms3, MS3_CMD_LIST_ROLE, NULL, 0, NULL);
        if(res)
        {
          return res;
        }

    }
    ms3debug("Assume IAM role");
    res = execute_assume_role_request(ms3, MS3_CMD_ASSUME_ROLE, NULL, 0, NULL);

    return res;
}