static dpl_vec_t * get_canonical_params(const char *subresource, const dpl_dict_t *query_params, unsigned char encode_url) { dpl_status_t ret = DPL_SUCCESS; dpl_vec_t *params; params = dpl_vec_new(1, 0); if (params == NULL) return NULL; if (subresource != NULL) ret = parse_query_params_from_subresource(params, subresource, encode_url); if (ret == DPL_SUCCESS && query_params != NULL) ret = insert_query_params_in_vec(params, query_params, encode_url); if (ret != DPL_SUCCESS) { int item; for (item = 0; item < params->n_items; item++) { dpl_dict_var_t *param = (dpl_dict_var_t *) dpl_vec_get(params, item); free(param->key); dpl_dict_var_free(param); } dpl_vec_free(params); return NULL; } dpl_vec_sort(params, var_cmp); return params; }
static dpl_vec_t * get_canonical_headers(dpl_dict_t *headers) { int bucket; dpl_vec_t *canonical_headers; dpl_dict_var_t *header; canonical_headers = dpl_vec_new(1, 0); if (canonical_headers == NULL) return NULL; for (bucket = 0; bucket < headers->n_buckets; bucket++) { header = headers->buckets[bucket]; while (header != NULL) { dpl_status_t ret; assert(header->val->type == DPL_VALUE_STRING); ret = dpl_vec_add(canonical_headers, header); if (ret != DPL_SUCCESS) { dpl_vec_free(canonical_headers); return NULL; } header = header->prev; } } dpl_vec_sort(canonical_headers, var_cmp); return canonical_headers; }
void dpl_vec_common_prefixes_free(dpl_vec_t *vec) { int i; for (i = 0;i < vec->n_items;i++) dpl_common_prefix_free((dpl_common_prefix_t *) dpl_vec_get(vec, i)); dpl_vec_free(vec); }
void dpl_vec_delete_objects_free(dpl_vec_t *vec) { int i; for (i = 0; i < vec->n_items; i++) dpl_delete_object_free((dpl_delete_object_t *) dpl_vec_get(vec, i)); dpl_vec_free(vec); }
void dpl_vec_buckets_free(dpl_vec_t *vec) { int i; for (i = 0;i < vec->n_items;i++) dpl_bucket_free((dpl_bucket_t *) dpl_vec_get(vec, i)); dpl_vec_free(vec); }
static dpl_status_t dpl_s3_insert_signature_v4_params(const dpl_req_t *req, dpl_dict_t *query_params, struct tm *tm, char *date_str, char *signature) { int item; dpl_status_t ret; dpl_vec_t *canonical_params; char canonical_request[4096] = ""; char sign_request[1024] = ""; canonical_params = get_canonical_params(req->subresource, query_params, 0); if (canonical_params == NULL) return DPL_FAILURE; ret = create_canonical_request(req, NULL, NULL, canonical_params, canonical_request, sizeof(canonical_request)); if (ret == DPL_SUCCESS) { DPRINTF("Canonical request:\n%s\n", canonical_request); ret = create_sign_request(req, canonical_request, tm, date_str, sign_request, sizeof(sign_request)); } if (ret == DPL_SUCCESS) { DPRINTF("Signing request:\n%s\n", sign_request); ret = create_signature(req, tm, sign_request, signature); } if (ret == DPL_SUCCESS) DPRINTF("Signature: %s\n", signature); for (item = 0; item < canonical_params->n_items; item++) { dpl_dict_var_t *param = (dpl_dict_var_t *) dpl_vec_get(canonical_params, item); free(param->key); dpl_dict_var_free(param); } dpl_vec_free(canonical_params); return ret; }
static dpl_status_t convert_obj_to_value(dpl_ctx_t *ctx, struct json_object *obj, int level, dpl_value_t **valp) { int ret, ret2; dpl_value_t *val = NULL; char *key; struct lh_entry *entry; json_object *child; dpl_dict_t *subdict = NULL; dpl_vec_t *vector = NULL; DPRINTF("convert_obj_to_value level=%d type=%d\n", level, json_object_get_type(obj)); val = malloc(sizeof (*val)); if (NULL == val) { ret = DPL_ENOMEM; goto end; } memset(val, 0, sizeof (*val)); switch (json_object_get_type(obj)) { case json_type_null: return DPL_ENOTSUPP; case json_type_array: { int n_items = json_object_array_length(obj); int i; vector = dpl_vec_new(2, 2); if (NULL == vector) { ret = DPL_ENOMEM; goto end; } for (i = 0;i < n_items;i++) { child = json_object_array_get_idx(obj, i); dpl_value_t *subval; ret2 = convert_obj_to_value(ctx, child, level+1, &subval); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } ret2 = dpl_vec_add_value(vector, subval); dpl_value_free(subval); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } } val->type = DPL_VALUE_VECTOR; val->vector = vector; vector = NULL; break ; } case json_type_object: { subdict = dpl_dict_new(13); if (NULL == subdict) { ret = DPL_ENOMEM; goto end; } for (entry = json_object_get_object(obj)->head; (entry ? (key = (char*)entry->k, child = (struct json_object*)entry->v, entry) : 0); entry = entry->next) { dpl_value_t *subval; DPRINTF("key='%s'\n", key); ret2 = convert_obj_to_value(ctx, child, level+1, &subval); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } ret2 = dpl_dict_add_value(subdict, key, subval, 0); dpl_value_free(subval); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } } val->type = DPL_VALUE_SUBDICT; val->subdict = subdict; subdict = NULL; break ; } case json_type_boolean: case json_type_double: case json_type_int: case json_type_string: { pthread_mutex_lock(&ctx->lock); //lock for objects other than string val->string = strdup((char *) json_object_get_string(obj)); pthread_mutex_unlock(&ctx->lock); if (NULL == val->string) { ret = DPL_ENOMEM; goto end; } val->type = DPL_VALUE_STRING; break ; } } if (NULL != valp) { *valp = val; val = NULL; } ret = DPL_SUCCESS; end: if (NULL != vector) dpl_vec_free(vector); if (NULL != subdict) dpl_dict_free(subdict); if (NULL != val) dpl_value_free(val); DPRINTF("level=%d ret=%d\n", level, ret); return ret; }
static dpl_status_t dpl_s3_make_signature_v2(dpl_ctx_t *ctx, const char *method, const char *bucket, const char *resource, const char *subresource, char *date, dpl_dict_t *headers, char *buf, unsigned int len, unsigned int *lenp) { char *p; char *value; int ret; p = buf; //method DPL_APPEND_STR(method); DPL_APPEND_STR("\n"); //md5 if (headers != NULL) { value = dpl_dict_get_value(headers, "Content-MD5"); if (NULL != value) DPL_APPEND_STR(value); } DPL_APPEND_STR("\n"); //content type if (headers != NULL) { value = dpl_dict_get_value(headers, "Content-Type"); if (NULL != value) DPL_APPEND_STR(value); } DPL_APPEND_STR("\n"); //expires or date if (date != NULL) DPL_APPEND_STR(date); DPL_APPEND_STR("\n"); //x-amz headers if (headers != NULL) { int bucket; dpl_dict_var_t *var; dpl_vec_t *vec; int i; vec = dpl_vec_new(2, 2); if (NULL == vec) return DPL_ENOMEM; for (bucket = 0; bucket < headers->n_buckets; bucket++) { for (var = headers->buckets[bucket];var;var = var->prev) { if (!strncmp(var->key, "x-amz-", 6) && strcmp(var->key, "x-amz-date")) { assert(DPL_VALUE_STRING == var->val->type); ret = dpl_vec_add(vec, var); if (DPL_SUCCESS != ret) { dpl_vec_free(vec); return DPL_FAILURE; } } } } dpl_vec_sort(vec, var_cmp); for (i = 0;i < vec->n_items;i++) { var = (dpl_dict_var_t *) dpl_vec_get(vec, i); if (var == NULL) continue; assert(DPL_VALUE_STRING == var->val->type); DPL_APPEND_STR(var->key); DPL_APPEND_STR(":"); DPL_APPEND_STR(dpl_sbuf_get_str(var->val->string)); DPL_APPEND_STR("\n"); } dpl_vec_free(vec); } //resource if (NULL != bucket) { DPL_APPEND_STR("/"); DPL_APPEND_STR(bucket); } if (NULL != resource) DPL_APPEND_STR(resource); if (NULL != subresource) { DPL_APPEND_STR("?"); DPL_APPEND_STR(subresource); } if (NULL != lenp) *lenp = p - buf; return DPL_SUCCESS; }
dpl_status_t dpl_s3_add_authorization_v4_to_headers(const dpl_req_t *req, dpl_dict_t *headers, const dpl_dict_t *query_params, struct tm *i_tm) { int item; dpl_status_t ret; char canonical_request[4096] = ""; char sign_request[1024] = ""; char signature[DPL_HEX_LENGTH(SHA256_DIGEST_LENGTH) + 1]; char authorization[1024] = ""; char date_str[32] = ""; dpl_vec_t *canonical_headers; dpl_vec_t *canonical_params; struct tm tm; ret = add_payload_signature_to_headers(req, headers); if (ret != DPL_SUCCESS) return ret; ret = get_current_utc_date(&tm, i_tm, date_str, sizeof(date_str)); if (ret != DPL_SUCCESS) return ret; ret = dpl_dict_add(headers, "x-amz-date", date_str, 0); if (ret != DPL_SUCCESS) return ret; canonical_headers = get_canonical_headers(headers); if (canonical_headers == NULL) return DPL_FAILURE; canonical_params = get_canonical_params(req->subresource, query_params, 1); if (canonical_params == NULL) { dpl_vec_free(canonical_headers); return DPL_FAILURE; } ret = create_canonical_request(req, headers, canonical_headers, canonical_params, canonical_request, sizeof(canonical_request)); if (ret == DPL_SUCCESS) { DPRINTF("Canonical request:\n%s\n", canonical_request); ret = create_sign_request(req, canonical_request, &tm, date_str, sign_request, sizeof(sign_request)); } if (ret == DPL_SUCCESS) { DPRINTF("Signing request:\n%s\n", sign_request); ret = create_signature(req, &tm, sign_request, signature); } if (ret == DPL_SUCCESS) { DPRINTF("Signature: %s\n", signature); ret = create_authorization(req, &tm, canonical_headers, signature, authorization, sizeof(authorization)); } if (ret == DPL_SUCCESS) ret = dpl_dict_add(headers, "Authorization", authorization, 0); for (item = 0; item < canonical_params->n_items; item++) { dpl_dict_var_t *param = (dpl_dict_var_t *) dpl_vec_get(canonical_params, item); free(param->key); dpl_dict_var_free(param); } dpl_vec_free(canonical_params); dpl_vec_free(canonical_headers); return ret; }