summaryrefslogtreecommitdiffstats
path: root/storage/maria/libmarias3/src/response.c
blob: 4e976aba4508b60834ac884c66ebc840706d4070 (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
/* 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 "xml.h"

char *parse_error_message(const char *data, size_t length)
{
  struct xml_document *doc = NULL;
  struct xml_node *node = NULL;
  struct xml_node *child = NULL;
  struct xml_node *root = NULL;

  uint64_t node_it = 0;

  if (!data || !length)
  {
    return NULL;
  }

  doc = xml_parse_document((uint8_t*)data, length);

  if (!doc)
  {
    return NULL;
  }

  root = xml_document_root(doc);

  // First node is Error
  child = xml_node_child(root, node_it);
  // IAM / STS This will be Error and we need next child
  if (!xml_node_name_cmp(child, "Error"))
  {
    node = xml_node_child(child, node_it);
  }
  else
  {
    node = child;
    child = root;
  }

  if (!node)
  {
    xml_document_free(doc, false);
    return NULL;
  }

  while(node)
  {
    if (!xml_node_name_cmp(node, "Message"))
    {
      struct xml_string *content = xml_node_content(node);
      uint8_t *message = ms3_cmalloc(xml_string_length(content) + 1);
      xml_string_copy(content, message, xml_string_length(content));
      xml_document_free(doc, false);
      return (char *)message;
    }

    node_it++;
    node = xml_node_child(child, node_it);
  }

  xml_document_free(doc, false);
  return NULL;
}

static ms3_list_st *get_next_list_ptr(struct ms3_list_container_st *container)
{
  ms3_list_st *new_alloc = NULL;
  struct ms3_pool_alloc_list_st *new_pool_next = NULL;
  struct ms3_pool_alloc_list_st *new_pool_prev = NULL;
  ms3_list_st *ret = NULL;
  if (container->pool_free == 0)
  {
    new_alloc = (ms3_list_st*)ms3_cmalloc(sizeof(ms3_list_st) * 1024);
    new_pool_next = (struct ms3_pool_alloc_list_st*)ms3_cmalloc(sizeof(struct ms3_pool_alloc_list_st));

    if (!new_alloc || !new_pool_next)
    {
        ms3debug("List realloc OOM");
        return NULL;
    }

    new_pool_prev = container->pool_list;
    container->pool_list = new_pool_next;
    if (new_pool_prev)
    {
      container->pool_list->prev = new_pool_prev;
    }
    else
    {
      container->pool_list->prev = NULL;
    }
    container->pool_list->pool = new_alloc;

    container->pool_free = 1024;
    if (!container->start)
    {
      container->start = new_alloc;
    }
    container->pool = container->next = new_alloc;
  }
  else
  {
    container->next++;
  }
  ret = container->next;
  container->pool_free--;
  return ret;
}

uint8_t parse_list_response(const char *data, size_t length, struct ms3_list_container_st *list_container,
                            uint8_t list_version,
                            char **continuation)
{
  struct xml_document *doc;
  struct xml_node *root;
  struct xml_node *node;
  struct xml_node *child;
  char *filename = NULL;
  char *filesize = NULL;
  char *filedate = NULL;
  size_t size = 0;
  struct tm ttmp = {0};
  time_t tout = 0;
  bool truncated = false;
  const char *last_key = NULL;
  ms3_list_st *nextptr = NULL, *lastptr = list_container->next;
  uint64_t node_it = 0;

  // Empty list
  if (!data || !length)
  {
    return 0;
  }

  doc = xml_parse_document((uint8_t*)data, length);

  if (!doc)
  {
    return MS3_ERR_RESPONSE_PARSE;
  }

  /* For version 1:
   * If IsTruncated is set, get the last key in the list, this will be used as
   * "marker" in the next request.
   * For version 2:
   * If NextContinuationToken is set, use this for the next request
   *
   * We use the "continuation" return value for both
   */

  root = xml_document_root(doc);
  // First node is ListBucketResponse
  node = xml_node_child(root, 0);

  do
  {
    if (!xml_node_name_cmp(node, "NextContinuationToken"))
    {
      struct xml_string *content = xml_node_content(node);
      *continuation = ms3_cmalloc(xml_string_length(content) + 1);
      xml_string_copy(content, (uint8_t*)*continuation, xml_string_length(content));
      continue;
    }

    if (list_version == 1)
    {
      if (!xml_node_name_cmp(node, "IsTruncated"))
      {
        struct xml_string *content = xml_node_content(node);
        char *trunc_value = ms3_cmalloc(xml_string_length(content) + 1);
        xml_string_copy(content, (uint8_t*)trunc_value, xml_string_length(content));

        if (!strcmp(trunc_value, "true"))
        {
          truncated = true;
        }

        ms3_cfree(trunc_value);
        continue;
      }
    }

    if (!xml_node_name_cmp(node, "Contents"))
    {
      bool skip = false;
      uint64_t child_it = 0;
      // Found contents
      child = xml_node_child(node, 0);

      do
      {
        if (!xml_node_name_cmp(child, "Key"))
        {
          struct xml_string *content = xml_node_content(child);
          filename = ms3_cmalloc(xml_string_length(content) + 1);
          xml_string_copy(content, (uint8_t*)filename, xml_string_length(content));

          ms3debug("Filename: %s", filename);

          if (filename[strlen((const char *)filename) - 1] == '/')
          {
            skip = true;
            ms3_cfree(filename);
            break;
          }

          continue;
        }

        if (!xml_node_name_cmp(child, "Size"))
        {
          struct xml_string *content = xml_node_content(child);
          filesize = ms3_cmalloc(xml_string_length(content) + 1);
          xml_string_copy(content, (uint8_t*)filesize, xml_string_length(content));

          ms3debug("Size: %s", filesize);
          size = strtoull((const char *)filesize, NULL, 10);
          ms3_cfree(filesize);
          continue;
        }

        if (!xml_node_name_cmp(child, "LastModified"))
        {
          struct xml_string *content = xml_node_content(child);
          filedate = ms3_cmalloc(xml_string_length(content) + 1);
          xml_string_copy(content, (uint8_t*)filedate, xml_string_length(content));

          ms3debug("Date: %s", filedate);
          strptime((const char *)filedate, "%Y-%m-%dT%H:%M:%SZ", &ttmp);
          tout = mktime(&ttmp);
          ms3_cfree(filedate);
          continue;
        }
      }
      while ((child = xml_node_child(node, ++child_it)));

      if (!skip)
      {
        nextptr = get_next_list_ptr(list_container);
        nextptr->next = NULL;

        if (lastptr)
        {
          lastptr->next = nextptr;
        }
        lastptr = nextptr;

        if (filename)
        {
          nextptr->key = (char *)filename;

          if (list_version == 1)
          {
            last_key = nextptr->key;
          }
        }
        else
        {
          nextptr->key = NULL;
        }

        nextptr->length = size;
        nextptr->created = tout;
      }

      continue;
    }

    if (!xml_node_name_cmp(node, "CommonPrefixes"))
    {
      child = xml_node_child(node, 0);

      if (!xml_node_name_cmp(child, "Prefix"))
      {
        struct xml_string *content = xml_node_content(child);
        filename = ms3_cmalloc(xml_string_length(content) + 1);
        xml_string_copy(content, (uint8_t*)filename, xml_string_length(content));

        ms3debug("Filename: %s", filename);
        nextptr = get_next_list_ptr(list_container);
        nextptr->next = NULL;

        if (lastptr)
        {
          lastptr->next = nextptr;
        }
        lastptr = nextptr;

        nextptr->key = (char *)filename;
        nextptr->length = 0;
        nextptr->created = 0;
      }
    }

  }
  while ((node = xml_node_child(root, ++node_it)));

  if (list_version == 1 && truncated && last_key)
  {
    *continuation = ms3_cstrdup(last_key);
  }

  xml_document_free(doc, false);
  return 0;
}

uint8_t parse_role_list_response(const char *data, size_t length, char *role_name, char *arn, char **continuation)
{
    struct xml_document *doc;
    struct xml_node *root;
    struct xml_node *list_role_result;
    struct xml_node *child;
    struct xml_node *roles;
    struct xml_node *member;

    char *response_role_name = NULL;
    char *response_role_arn = NULL;
    uint64_t node_it = 0;

    // Empty list
    if (!data || !length)
    {
      return 0;
    }

    doc = xml_parse_document((uint8_t*)data, length);

    if (!doc)
    {
      return MS3_ERR_RESPONSE_PARSE;
    }

    root = xml_document_root(doc);
    // First node is listRoleResponse
    list_role_result = xml_node_child(root, 0);
    child = xml_node_child(list_role_result, 0);

    do
    {

      if (!xml_node_name_cmp(child, "Marker"))
      {
        struct xml_string *content = xml_node_content(child);
        *continuation = ms3_cmalloc(xml_string_length(content) + 1);
        xml_string_copy(content, (uint8_t*)*continuation, xml_string_length(content));
        continue;
      }

      if (!xml_node_name_cmp(child, "Roles"))
      {
        uint64_t child_it = 0;
        // Found contents
        roles = xml_node_child(child, 0);
        do
        {
          // go down one more child to get members
         uint64_t roles_it = 0;
          member = xml_node_child(roles, 0);
          do
          {
            if (!xml_node_name_cmp(member, "RoleName"))
            {
              struct xml_string *content = xml_node_content(member);
              response_role_name = ms3_cmalloc(xml_string_length(content) + 1);
              xml_string_copy(content, (uint8_t*)response_role_name, xml_string_length(content));
              continue;
            }
            if (!xml_node_name_cmp(member, "Arn"))
            {
              struct xml_string *content = xml_node_content(member);
              response_role_arn = ms3_cmalloc(xml_string_length(content) + 1);
              xml_string_copy(content, (uint8_t*)response_role_arn, xml_string_length(content));
              continue;
            }
          }
          while ((member = xml_node_child(roles, ++roles_it)));
          if (!strcmp(response_role_name, role_name))
          {
              ms3debug("Role Found ARN = %s",response_role_arn);
              sprintf(arn, "%s", response_role_arn);
              ms3_cfree(response_role_name);
              ms3_cfree(response_role_arn);
              xml_document_free(doc, false);
              return MS3_ERR_NONE;
          }
          ms3_cfree(response_role_name);
          ms3_cfree(response_role_arn);
        }
        while ((roles = xml_node_child(child, ++child_it)));
      }
    }
    while ((child = xml_node_child(list_role_result, ++node_it)));

    xml_document_free(doc, false);
    return MS3_ERR_NOT_FOUND;
}

uint8_t parse_assume_role_response(const char *data, size_t length, char *assume_role_key, char *assume_role_secret, char *assume_role_token)
{
    struct xml_document *doc;
    struct xml_node *root;
    struct xml_node *assume_role_result;
    struct xml_node *child;
    struct xml_node *credentials;
    uint64_t node_it = 0;

    // Empty list
    if (!data || !length)
    {
      return 0;
    }

    doc = xml_parse_document((uint8_t*)data, length);

    if (!doc)
    {
      return MS3_ERR_RESPONSE_PARSE;
    }

    root = xml_document_root(doc);
    // First node is AssumeRoleResponse
    assume_role_result = xml_node_child(root, 0);
    child = xml_node_child(assume_role_result, 0);

    do
    {
      if (!xml_node_name_cmp(child, "Credentials"))
      {
        uint64_t child_it = 0;
        // Found contents
        credentials = xml_node_child(child, 0);
        do
        {
          if (!xml_node_name_cmp(credentials, "AccessKeyId"))
          {
            struct xml_string *content = xml_node_content(credentials);
            size_t content_length = xml_string_length(content);
            assume_role_key[0] = '\0';

            if (content_length >= 128)
            {
              ms3debug("AccessKeyId error length = %zu", content_length);
              xml_document_free(doc, false);
              return MS3_ERR_AUTH_ROLE;
            }
            xml_string_copy(content, (uint8_t*)assume_role_key, content_length);

            continue;
          }
          if (!xml_node_name_cmp(credentials, "SecretAccessKey"))
          {
            struct xml_string *content = xml_node_content(credentials);
            size_t content_length = xml_string_length(content);
            assume_role_secret[0] = '\0';

            if (content_length >= 1024)
            {
              ms3debug("SecretAccessKey error length = %zu", content_length);
              xml_document_free(doc, false);
              return MS3_ERR_AUTH_ROLE;
            }
            xml_string_copy(content, (uint8_t*)assume_role_secret, content_length);

            continue;
          }
          if (!xml_node_name_cmp(credentials, "SessionToken"))
          {
            struct xml_string *content = xml_node_content(credentials);
            size_t content_length = xml_string_length(content);
            assume_role_token[0] = '\0';

            if (content_length >= 2048)
            {
              ms3debug("SessionToken error length = %zu", content_length);
              xml_document_free(doc, false);
              return MS3_ERR_AUTH_ROLE;
            }
            xml_string_copy(content, (uint8_t*)assume_role_token, content_length);

            continue;
          }
        }
        while ((credentials = xml_node_child(child, ++child_it)));
      }
    }
    while ((child = xml_node_child(assume_role_result, ++node_it)));

    xml_document_free(doc, false);

    return MS3_ERR_NONE;
}