| 1 |
/* |
| 2 |
* Copyright (c) 2008-2012 Zmanda, Inc. All Rights Reserved. |
| 3 |
* Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU General Public License |
| 7 |
* as published by the Free Software Foundation; either version 2 |
| 8 |
* of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 12 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
* for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 |
* |
| 19 |
* Contact information: Carbonite Inc., 756 N Pastoria Ave |
| 20 |
* Sunnyvale, CA 94085, or: http://www.zmanda.com |
| 21 |
*/ |
| 22 |
|
| 23 |
/* TODO |
| 24 |
* - collect speed statistics |
| 25 |
* - debugging mode |
| 26 |
*/ |
| 27 |
|
| 28 |
#ifdef HAVE_CONFIG_H |
| 29 |
/* use a relative path here to avoid conflicting with Perl's config.h. */ |
| 30 |
#include "../config/config.h" |
| 31 |
#endif |
| 32 |
#include <string.h> |
| 33 |
#include "s3.h" |
| 34 |
#include "s3-util.h" |
| 35 |
#ifdef HAVE_REGEX_H |
| 36 |
#include <regex.h> |
| 37 |
#endif |
| 38 |
#ifdef HAVE_SYS_TYPES_H |
| 39 |
#include <sys/types.h> |
| 40 |
#endif |
| 41 |
#ifdef HAVE_SYS_STAT_H |
| 42 |
#include <sys/stat.h> |
| 43 |
#endif |
| 44 |
#ifdef HAVE_UNISTD_H |
| 45 |
#include <unistd.h> |
| 46 |
#endif |
| 47 |
#ifdef HAVE_DIRENT_H |
| 48 |
#include <dirent.h> |
| 49 |
#endif |
| 50 |
#ifdef HAVE_TIME_H |
| 51 |
#include <time.h> |
| 52 |
#endif |
| 53 |
#ifdef HAVE_UTIL_H |
| 54 |
#include "amutil.h" |
| 55 |
#endif |
| 56 |
#ifdef HAVE_AMANDA_H |
| 57 |
#include "amanda.h" |
| 58 |
#endif |
| 59 |
#include "amjson.h" |
| 60 |
#include <curl/curl.h> |
| 61 |
|
| 62 |
/* Constant renamed after version 7.10.7 */ |
| 63 |
#ifndef CURLINFO_RESPONSE_CODE |
| 64 |
#define CURLINFO_RESPONSE_CODE CURLINFO_HTTP_CODE |
| 65 |
#endif |
| 66 |
|
| 67 |
/* We don't need OpenSSL's kerberos support, and it's broken in |
| 68 |
* RHEL 3 anyway. */ |
| 69 |
#define OPENSSL_NO_KRB5 |
| 70 |
|
| 71 |
#ifdef HAVE_OPENSSL_HMAC_H |
| 72 |
# include <openssl/hmac.h> |
| 73 |
#else |
| 74 |
# ifdef HAVE_CRYPTO_HMAC_H |
| 75 |
# include <crypto/hmac.h> |
| 76 |
# else |
| 77 |
# ifdef HAVE_HMAC_H |
| 78 |
# include <hmac.h> |
| 79 |
# endif |
| 80 |
# endif |
| 81 |
#endif |
| 82 |
|
| 83 |
#include <openssl/err.h> |
| 84 |
#include <openssl/ssl.h> |
| 85 |
#include <openssl/md5.h> |
| 86 |
|
| 87 |
char *S3_name[] = { |
| 88 |
"UNKNOWN", |
| 89 |
"S3", |
| 90 |
"OpenStack", |
| 91 |
"OpenStack", |
| 92 |
"OpenStack", |
| 93 |
"Google Storage", |
| 94 |
"Castor", |
| 95 |
"S3", |
| 96 |
}; |
| 97 |
|
| 98 |
char *S3_bucket_name[] = { |
| 99 |
"UNKNOWN", |
| 100 |
"S3 bucket", |
| 101 |
"OpenStack bucket", |
| 102 |
"OpenStack bucket", |
| 103 |
"OpenStack bucket", |
| 104 |
"Google Storage bucket", |
| 105 |
"Castor bucket", |
| 106 |
"S3 bucket", |
| 107 |
}; |
| 108 |
|
| 109 |
/* Maximum key length as specified in the S3 documentation |
| 110 |
* (*excluding* null terminator) */ |
| 111 |
#define S3_MAX_KEY_LENGTH 1024 |
| 112 |
|
| 113 |
#define AMAZON_SECURITY_HEADER "x-amz-security-token" |
| 114 |
|
| 115 |
#define AMAZON_STORAGE_CLASS_HEADER "x-amz-storage-class" |
| 116 |
|
| 117 |
#define AMAZON_SERVER_SIDE_ENCRYPTION_HEADER "x-amz-server-side-encryption" |
| 118 |
|
| 119 |
#define AMAZON_WILDCARD_LOCATION "*" |
| 120 |
#define AMZ_QUOTA_CLOUD_URL ".s3.amazonaws.com" |
| 121 |
|
| 122 |
/* parameters for exponential backoff in the face of retriable errors */ |
| 123 |
|
| 124 |
/* start at 0.01s */ |
| 125 |
#define EXPONENTIAL_BACKOFF_START_USEC G_USEC_PER_SEC/100 |
| 126 |
/* double at each retry */ |
| 127 |
#define EXPONENTIAL_BACKOFF_BASE 2 |
| 128 |
/* retry 14 times (for a total of about 3 minutes spent waiting) */ |
| 129 |
#define EXPONENTIAL_BACKOFF_MAX_RETRIES 14 |
| 130 |
|
| 131 |
/* general "reasonable size" parameters */ |
| 132 |
#define MAX_ERROR_RESPONSE_LEN (100*1024) |
| 133 |
|
| 134 |
// CURLE_SSL_CACERT_BADFILE is defined in 7.16.0 |
| 135 |
#if LIBCURL_VERSION_NUM >= 0x071000 |
| 136 |
#define AMAMDA_CURLE_SSL_CACERT_BADFILE CURLE_SSL_CACERT_BADFILE |
| 137 |
#else |
| 138 |
# define AMAMDA_CURLE_SSL_CACERT_BADFILE CURLE_GOT_NOTHING |
| 139 |
#endif |
| 140 |
/* Results which should always be retried */ |
| 141 |
#define RESULT_HANDLING_ALWAYS_RETRY \ |
| 142 |
{ 400, S3_ERROR_RequestTimeout, 0, S3_RESULT_RETRY }, \ |
| 143 |
{ 403, S3_ERROR_RequestTimeTooSkewed,0, S3_RESULT_RETRY }, \ |
| 144 |
{ 409, S3_ERROR_OperationAborted, 0, S3_RESULT_RETRY }, \ |
| 145 |
{ 412, S3_ERROR_PreconditionFailed, 0, S3_RESULT_RETRY }, \ |
| 146 |
{ 429, 0, 0, S3_RESULT_RETRY }, \ |
| 147 |
{ 500, S3_ERROR_None, 0, S3_RESULT_RETRY }, \ |
| 148 |
{ 500, S3_ERROR_InternalError, 0, S3_RESULT_RETRY }, \ |
| 149 |
{ 501, S3_ERROR_NotImplemented, 0, S3_RESULT_RETRY }, \ |
| 150 |
{ 503, S3_ERROR_ServiceUnavailable, 0, S3_RESULT_RETRY }, \ |
| 151 |
{ 503, S3_ERROR_SlowDown, 0, S3_RESULT_RETRY }, \ |
| 152 |
{ 503, 0, 0, S3_RESULT_RETRY }, \ |
| 153 |
{ 0, 0, CURLE_COULDNT_CONNECT, S3_RESULT_RETRY }, \ |
| 154 |
{ 0, 0, CURLE_COULDNT_RESOLVE_HOST, S3_RESULT_RETRY }, \ |
| 155 |
{ 0, 0, CURLE_PARTIAL_FILE, S3_RESULT_RETRY }, \ |
| 156 |
{ 0, 0, CURLE_OPERATION_TIMEOUTED, S3_RESULT_RETRY }, \ |
| 157 |
{ 0, 0, CURLE_SSL_CONNECT_ERROR, S3_RESULT_RETRY }, \ |
| 158 |
{ 0, 0, CURLE_SEND_ERROR, S3_RESULT_RETRY }, \ |
| 159 |
{ 0, 0, CURLE_RECV_ERROR, S3_RESULT_RETRY }, \ |
| 160 |
{ 0, 0, CURLE_ABORTED_BY_CALLBACK, S3_RESULT_RETRY }, \ |
| 161 |
{ 0, 0, CURLE_GOT_NOTHING, S3_RESULT_RETRY }, \ |
| 162 |
{ 0, 0, AMAMDA_CURLE_SSL_CACERT_BADFILE, S3_RESULT_RETRY } |
| 163 |
|
| 164 |
/* |
| 165 |
* Data structures and associated functions |
| 166 |
*/ |
| 167 |
|
| 168 |
struct S3Handle { |
| 169 |
/* (all strings in this struct are freed by s3_free()) */ |
| 170 |
|
| 171 |
char *access_key; |
| 172 |
char *secret_key; |
| 173 |
char *session_token; |
| 174 |
char *user_token; |
| 175 |
char *swift_account_id; |
| 176 |
char *swift_access_key; |
| 177 |
char *username; |
| 178 |
char *password; |
| 179 |
char *tenant_id; |
| 180 |
char *tenant_name; |
| 181 |
char *project_name; |
| 182 |
char *domain_name; |
| 183 |
char *client_id; |
| 184 |
char *client_secret; |
| 185 |
char *refresh_token; |
| 186 |
char *access_token; |
| 187 |
time_t expires; |
| 188 |
gboolean getting_oauth2_access_token; |
| 189 |
gboolean getting_swift_2_token; |
| 190 |
gboolean getting_swift_3_token; |
| 191 |
|
| 192 |
/* attributes for new objects */ |
| 193 |
char *bucket_location; |
| 194 |
char *storage_class; |
| 195 |
char *server_side_encryption; |
| 196 |
char *proxy; |
| 197 |
char *host; |
| 198 |
char *host_without_port; |
| 199 |
char *service_path; |
| 200 |
gboolean use_subdomain; |
| 201 |
S3_api s3_api; |
| 202 |
char *ca_info; |
| 203 |
char *x_auth_token; |
| 204 |
char *x_storage_url; |
| 205 |
char *x_amz_expiration; |
| 206 |
char *x_amz_restore; |
| 207 |
|
| 208 |
CURL *curl; |
| 209 |
|
| 210 |
gboolean verbose; |
| 211 |
gboolean use_ssl; |
| 212 |
gboolean server_side_encryption_header; |
| 213 |
|
| 214 |
guint64 max_send_speed; |
| 215 |
guint64 max_recv_speed; |
| 216 |
|
| 217 |
/* information from the last request */ |
| 218 |
char *last_message; |
| 219 |
guint last_response_code; |
| 220 |
s3_error_code_t last_s3_error_code; |
| 221 |
CURLcode last_curl_code; |
| 222 |
guint last_num_retries; |
| 223 |
void *last_response_body; |
| 224 |
guint last_response_body_size; |
| 225 |
char *uploadId; |
| 226 |
char *etag; |
| 227 |
|
| 228 |
/* offset with s3 */ |
| 229 |
time_t time_offset_with_s3; |
| 230 |
char *content_type; |
| 231 |
|
| 232 |
gboolean reuse_connection; |
| 233 |
gboolean read_from_glacier; |
| 234 |
char *transfer_encoding; |
| 235 |
long timeout; |
| 236 |
|
| 237 |
/* CAStor */ |
| 238 |
char *reps; |
| 239 |
char *reps_bucket; |
| 240 |
}; |
| 241 |
|
| 242 |
typedef struct { |
| 243 |
CurlBuffer resp_buf; |
| 244 |
s3_write_func write_func; |
| 245 |
s3_reset_func reset_func; |
| 246 |
gpointer write_data; |
| 247 |
|
| 248 |
gboolean headers_done; |
| 249 |
gboolean int_write_done; |
| 250 |
char *etag; |
| 251 |
/* Points to current handle: Added to get hold of s3 offset */ |
| 252 |
struct S3Handle *hdl; |
| 253 |
} S3InternalData; |
| 254 |
|
| 255 |
/* Callback function to examine headers one-at-a-time |
| 256 |
* |
| 257 |
* @note this is the same as CURLOPT_HEADERFUNCTION |
| 258 |
* |
| 259 |
* @param data: The pointer to read data from |
| 260 |
* @param size: The size of each "element" of the data buffer in bytes |
| 261 |
* @param nmemb: The number of elements in the data buffer. |
| 262 |
* So, the buffer's size is size*nmemb bytes. |
| 263 |
* @param stream: the header_data (an opaque pointer) |
| 264 |
* |
| 265 |
* @return The number of bytes written to the buffer or |
| 266 |
* CURL_WRITEFUNC_PAUSE to pause. |
| 267 |
* If it's the number of bytes written, it should match the buffer size |
| 268 |
*/ |
| 269 |
typedef size_t (*s3_header_func)(void *data, size_t size, size_t nmemb, void *stream); |
| 270 |
|
| 271 |
|
| 272 |
/* |
| 273 |
* S3 errors */ |
| 274 |
|
| 275 |
/* (see preprocessor magic in s3.h) */ |
| 276 |
|
| 277 |
static char * s3_error_code_names[] = { |
| 278 |
#define S3_ERROR(NAME) #NAME |
| 279 |
S3_ERROR_LIST |
| 280 |
#undef S3_ERROR |
| 281 |
}; |
| 282 |
|
| 283 |
/* Convert an s3 error name to an error code. This function |
| 284 |
* matches strings case-insensitively, and is appropriate for use |
| 285 |
* on data from the network. |
| 286 |
* |
| 287 |
* @param s3_error_code: the error name |
| 288 |
* @returns: the error code (see constants in s3.h) |
| 289 |
*/ |
| 290 |
static s3_error_code_t |
| 291 |
s3_error_code_from_name(char *s3_error_name); |
| 292 |
|
| 293 |
/* Convert an s3 error code to a string |
| 294 |
* |
| 295 |
* @param s3_error_code: the error code to convert |
| 296 |
* @returns: statically allocated string |
| 297 |
*/ |
| 298 |
static const char * |
| 299 |
s3_error_name_from_code(s3_error_code_t s3_error_code); |
| 300 |
|
| 301 |
|
| 302 |
static void s3_new_curl(S3Handle *hdl); |
| 303 |
|
| 304 |
/* |
| 305 |
* result handling */ |
| 306 |
|
| 307 |
/* result handling is specified by a static array of result_handling structs, |
| 308 |
* which match based on response_code (from HTTP) and S3 error code. The result |
| 309 |
* given for the first match is used. 0 acts as a wildcard for both response_code |
| 310 |
* and s3_error_code. The list is terminated with a struct containing 0 for both |
| 311 |
* response_code and s3_error_code; the result for that struct is the default |
| 312 |
* result. |
| 313 |
* |
| 314 |
* See RESULT_HANDLING_ALWAYS_RETRY for an example. |
| 315 |
*/ |
| 316 |
typedef enum { |
| 317 |
S3_RESULT_RETRY = -1, |
| 318 |
S3_RESULT_FAIL = 0, |
| 319 |
S3_RESULT_OK = 1, |
| 320 |
S3_RESULT_NOTIMPL = 2 |
| 321 |
} s3_result_t; |
| 322 |
|
| 323 |
typedef struct result_handling { |
| 324 |
guint response_code; |
| 325 |
s3_error_code_t s3_error_code; |
| 326 |
CURLcode curl_code; |
| 327 |
s3_result_t result; |
| 328 |
} result_handling_t; |
| 329 |
|
| 330 |
/* |
| 331 |
* get the access token for OAUTH2 |
| 332 |
*/ |
| 333 |
static gboolean oauth2_get_access_token(S3Handle *hdl); |
| 334 |
|
| 335 |
/* Lookup a result in C{result_handling}. |
| 336 |
* |
| 337 |
* @param result_handling: array of handling specifications |
| 338 |
* @param response_code: response code from operation |
| 339 |
* @param s3_error_code: s3 error code from operation, if any |
| 340 |
* @param curl_code: the CURL error, if any |
| 341 |
* @returns: the matching result |
| 342 |
*/ |
| 343 |
static s3_result_t |
| 344 |
lookup_result(const result_handling_t *result_handling, |
| 345 |
guint response_code, |
| 346 |
s3_error_code_t s3_error_code, |
| 347 |
CURLcode curl_code); |
| 348 |
|
| 349 |
/* |
| 350 |
* Precompiled regular expressions */ |
| 351 |
static regex_t etag_regex, error_name_regex, message_regex, subdomain_regex, |
| 352 |
location_con_regex, date_sync_regex, x_auth_token_regex, |
| 353 |
x_subject_token_regex, |
| 354 |
x_storage_url_regex, access_token_regex, expires_in_regex, |
| 355 |
content_type_regex, details_regex, code_regex, uploadId_regex, |
| 356 |
json_message_regex, html_error_name_regex, html_message_regex, |
| 357 |
transfer_encoding_regex, x_amz_expiration_regex, x_amz_restore_regex; |
| 358 |
|
| 359 |
|
| 360 |
/* |
| 361 |
* Utility functions |
| 362 |
*/ |
| 363 |
|
| 364 |
/* Check if a string is non-empty |
| 365 |
* |
| 366 |
* @param str: string to check |
| 367 |
* @returns: true iff str is non-NULL and not "\0" |
| 368 |
*/ |
| 369 |
static gboolean is_non_empty_string(const char *str); |
| 370 |
char *am_strrmspace(char *str); |
| 371 |
|
| 372 |
/* Construct the URL for an Amazon S3 REST request. |
| 373 |
* |
| 374 |
* A new string is allocated and returned; it is the responsiblity of the caller. |
| 375 |
* |
| 376 |
* @param hdl: the S3Handle object |
| 377 |
* @param service_path: A path to add in the URL, or NULL for none. |
| 378 |
* @param bucket: the bucket being accessed, or NULL for none |
| 379 |
* @param key: the key being accessed, or NULL for none |
| 380 |
* @param subresource: the sub-resource being accessed (e.g. "acl"), or NULL for none |
| 381 |
* @param query: the query being accessed (e.g. "acl"), or NULL for none |
| 382 |
* |
| 383 |
* !use_subdomain: http://host/service_path/bucket/key |
| 384 |
* use_subdomain : http://bucket.host/service_path/key |
| 385 |
* |
| 386 |
*/ |
| 387 |
static char * |
| 388 |
build_url( |
| 389 |
S3Handle *hdl, |
| 390 |
const char *bucket, |
| 391 |
const char *key, |
| 392 |
const char *subresource, |
| 393 |
const char **query); |
| 394 |
|
| 395 |
/* Create proper authorization headers for an Amazon S3 REST |
| 396 |
* request to C{headers}. |
| 397 |
* |
| 398 |
* @note: C{X-Amz} headers (in C{headers}) must |
| 399 |
* - be in lower-case |
| 400 |
* - be in alphabetical order |
| 401 |
* - have no spaces around the colon |
| 402 |
* (don't yell at me -- see the Amazon Developer Guide) |
| 403 |
* |
| 404 |
* @param hdl: the S3Handle object |
| 405 |
* @param verb: capitalized verb for this request ('PUT', 'GET', etc.) |
| 406 |
* @param bucket: the bucket being accessed, or NULL for none |
| 407 |
* @param key: the key being accessed, or NULL for none |
| 408 |
* @param subresource: the sub-resource being accessed (e.g. "acl"), or NULL for none |
| 409 |
* @param md5_hash: the MD5 hash of the request body, or NULL for none |
| 410 |
*/ |
| 411 |
static struct curl_slist * |
| 412 |
authenticate_request(S3Handle *hdl, |
| 413 |
const char *verb, |
| 414 |
const char *bucket, |
| 415 |
const char *key, |
| 416 |
const char *subresource, |
| 417 |
const char **query, |
| 418 |
const char *md5_hash, |
| 419 |
const char *data_SHA256Hash, |
| 420 |
const char *content_type, |
| 421 |
const size_t content_length, |
| 422 |
const char *project_id); |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
/* Interpret the response to an S3 operation, assuming CURL completed its request |
| 427 |
* successfully. This function fills in the relevant C{hdl->last*} members. |
| 428 |
* |
| 429 |
* @param hdl: The S3Handle object |
| 430 |
* @param body: the response body |
| 431 |
* @param body_len: the length of the response body |
| 432 |
* @param etag: The response's ETag header |
| 433 |
* @param content_md5: The hex-encoded MD5 hash of the request body, |
| 434 |
* which will be checked against the response's ETag header. |
| 435 |
* If NULL, the header is not checked. |
| 436 |
* If non-NULL, then the body should have the response headers at its beginnning. |
| 437 |
* @returns: TRUE if the response should be retried (e.g., network error) |
| 438 |
*/ |
| 439 |
static gboolean |
| 440 |
interpret_response(S3Handle *hdl, |
| 441 |
CURLcode curl_code, |
| 442 |
char *curl_error_buffer, |
| 443 |
gchar *body, |
| 444 |
guint body_len, |
| 445 |
const char *etag, |
| 446 |
const char *content_md5); |
| 447 |
|
| 448 |
/* Perform an S3 operation. This function handles all of the details |
| 449 |
* of retryig requests and so on. |
| 450 |
* |
| 451 |
* The concepts of bucket and keys are defined by the Amazon S3 API. |
| 452 |
* See: "Components of Amazon S3" - API Version 2006-03-01 pg. 8 |
| 453 |
* |
| 454 |
* Individual sub-resources are defined in several places. In the REST API, |
| 455 |
* they they are represented by a "flag" in the "query string". |
| 456 |
* See: "Constructing the CanonicalizedResource Element" - API Version 2006-03-01 pg. 60 |
| 457 |
* |
| 458 |
* @param hdl: the S3Handle object |
| 459 |
* @param verb: the HTTP request method |
| 460 |
* @param bucket: the bucket to access, or NULL for none |
| 461 |
* @param key: the key to access, or NULL for none |
| 462 |
* @param subresource: the "sub-resource" to request (e.g. "acl") or NULL for none |
| 463 |
* @param query: NULL terminated array of query string, the must be in alphabetid order |
| 464 |
* or NULL for none |
| 465 |
* @param read_func: the callback for reading data |
| 466 |
* Will use s3_empty_read_func if NULL is passed in. |
| 467 |
* @param read_reset_func: the callback for to reset reading data |
| 468 |
* @param size_func: the callback to get the number of bytes to upload |
| 469 |
* @param md5_func: the callback to get the MD5 hash of the data to upload |
| 470 |
* @param read_data: pointer to pass to the above functions |
| 471 |
* @param write_func: the callback for writing data. |
| 472 |
* Will use s3_counter_write_func if NULL is passed in. |
| 473 |
* @param write_reset_func: the callback for to reset writing data |
| 474 |
* @param write_data: pointer to pass to C{write_func} |
| 475 |
* @param progress_func: the callback for progress information |
| 476 |
* @param progress_data: pointer to pass to C{progress_func} |
| 477 |
* @param result_handling: instructions for handling the results; see above. |
| 478 |
* @returns: the result specified by result_handling; details of the response |
| 479 |
* are then available in C{hdl->last*} |
| 480 |
*/ |
| 481 |
static s3_result_t |
| 482 |
perform_request(S3Handle *hdl, |
| 483 |
const char *verb, |
| 484 |
const char *bucket, |
| 485 |
const char *key, |
| 486 |
const char *subresource, |
| 487 |
const char **query, |
| 488 |
const char *content_type, |
| 489 |
const char *project_id, |
| 490 |
struct curl_slist *user_headers, |
| 491 |
s3_read_func read_func, |
| 492 |
s3_reset_func read_reset_func, |
| 493 |
s3_size_func size_func, |
| 494 |
s3_md5_func md5_func, |
| 495 |
gpointer read_data, |
| 496 |
s3_write_func write_func, |
| 497 |
s3_reset_func write_reset_func, |
| 498 |
gpointer write_data, |
| 499 |
s3_progress_func progress_func, |
| 500 |
gpointer progress_data, |
| 501 |
const result_handling_t *result_handling, |
| 502 |
gboolean chunked); |
| 503 |
|
| 504 |
/* |
| 505 |
* a CURLOPT_WRITEFUNCTION to save part of the response in memory and |
| 506 |
* call an external function if one was provided. |
| 507 |
*/ |
| 508 |
static size_t |
| 509 |
s3_internal_write_func(void *ptr, size_t size, size_t nmemb, void * stream); |
| 510 |
|
| 511 |
/* |
| 512 |
* a function to reset to our internal buffer |
| 513 |
*/ |
| 514 |
static void |
| 515 |
s3_internal_reset_func(void * stream); |
| 516 |
|
| 517 |
/* |
| 518 |
* a CURLOPT_HEADERFUNCTION to save the ETag header only. |
| 519 |
*/ |
| 520 |
static size_t |
| 521 |
s3_internal_header_func(void *ptr, size_t size, size_t nmemb, void * stream); |
| 522 |
|
| 523 |
static gboolean |
| 524 |
compile_regexes(void); |
| 525 |
|
| 526 |
static gboolean get_openstack_swift_api_v1_setting(S3Handle *hdl); |
| 527 |
static gboolean get_openstack_swift_api_v2_setting(S3Handle *hdl); |
| 528 |
static gboolean get_openstack_swift_api_v3_setting(S3Handle *hdl); |
| 529 |
|
| 530 |
/* |
| 531 |
* Static function implementations |
| 532 |
*/ |
| 533 |
static s3_error_code_t |
| 534 |
s3_error_code_from_name(char *s3_error_name) |
| 535 |
{ |
| 536 |
int i; |
| 537 |
|
| 538 |
if (!s3_error_name) return S3_ERROR_Unknown; |
| 539 |
|
| 540 |
/* do a brute-force search through the list, since it's not sorted */ |
| 541 |
for (i = 0; i < S3_ERROR_END; i++) { |
| 542 |
if (g_ascii_strcasecmp(s3_error_name, s3_error_code_names[i]) == 0) |
| 543 |
return i; |
| 544 |
} |
| 545 |
|
| 546 |
return S3_ERROR_Unknown; |
| 547 |
} |
| 548 |
|
| 549 |
static const char * |
| 550 |
s3_error_name_from_code(s3_error_code_t s3_error_code) |
| 551 |
{ |
| 552 |
if (s3_error_code >= S3_ERROR_END) |
| 553 |
s3_error_code = S3_ERROR_Unknown; |
| 554 |
|
| 555 |
return s3_error_code_names[s3_error_code]; |
| 556 |
} |
| 557 |
|
| 558 |
gboolean |
| 559 |
s3_curl_supports_ssl(void) |
| 560 |
{ |
| 561 |
static int supported = -1; |
| 562 |
if (supported == -1) { |
| 563 |
#if defined(CURL_VERSION_SSL) |
| 564 |
curl_version_info_data *info = curl_version_info(CURLVERSION_NOW); |
| 565 |
if (info->features & CURL_VERSION_SSL) |
| 566 |
supported = 1; |
| 567 |
else |
| 568 |
supported = 0; |
| 569 |
#else |
| 570 |
supported = 0; |
| 571 |
#endif |
| 572 |
} |
| 573 |
|
| 574 |
return supported; |
| 575 |
} |
| 576 |
|
| 577 |
static gboolean |
| 578 |
s3_curl_throttling_compat(void) |
| 579 |
{ |
| 580 |
/* CURLOPT_MAX_SEND_SPEED_LARGE added in 7.15.5 */ |
| 581 |
#if LIBCURL_VERSION_NUM >= 0x070f05 |
| 582 |
curl_version_info_data *info; |
| 583 |
|
| 584 |
/* check the runtime version too */ |
| 585 |
info = curl_version_info(CURLVERSION_NOW); |
| 586 |
return info->version_num >= 0x070f05; |
| 587 |
#else |
| 588 |
return FALSE; |
| 589 |
#endif |
| 590 |
} |
| 591 |
|
| 592 |
static s3_result_t |
| 593 |
lookup_result(const result_handling_t *result_handling, |
| 594 |
guint response_code, |
| 595 |
s3_error_code_t s3_error_code, |
| 596 |
CURLcode curl_code) |
| 597 |
{ |
| 598 |
while (result_handling->response_code |
| 599 |
|| result_handling->s3_error_code |
| 600 |
|| result_handling->curl_code) { |
| 601 |
if ((result_handling->response_code && result_handling->response_code != response_code) |
| 602 |
|| (result_handling->s3_error_code && result_handling->s3_error_code != s3_error_code) |
| 603 |
|| (result_handling->curl_code && result_handling->curl_code != curl_code)) { |
| 604 |
result_handling++; |
| 605 |
continue; |
| 606 |
} |
| 607 |
|
| 608 |
return result_handling->result; |
| 609 |
} |
| 610 |
|
| 611 |
/* return the result for the terminator, as the default */ |
| 612 |
return result_handling->result; |
| 613 |
} |
| 614 |
|
| 615 |
static time_t |
| 616 |
rfc3339_date( |
| 617 |
const char *date) |
| 618 |
{ |
| 619 |
gint year, month, day, hour, minute, seconds; |
| 620 |
const char *atz; |
| 621 |
|
| 622 |
if (strlen(date) < 19) |
| 623 |
return 1073741824; |
| 624 |
|
| 625 |
year = atoi(date); |
| 626 |
month = atoi(date+5); |
| 627 |
day = atoi(date+8); |
| 628 |
hour = atoi(date+11); |
| 629 |
minute = atoi(date+14); |
| 630 |
seconds = atoi(date+17); |
| 631 |
atz = date+19; |
| 632 |
if (*atz == '.') { /* skip decimal seconds */ |
| 633 |
atz++; |
| 634 |
while (*atz >= '0' && *atz <= '9') { |
| 635 |
atz++; |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
#if GLIB_CHECK_VERSION(2,26,0) |
| 640 |
if (!glib_check_version(2,26,0)) { |
| 641 |
GTimeZone *tz; |
| 642 |
GDateTime *dt; |
| 643 |
time_t a; |
| 644 |
|
| 645 |
tz = g_time_zone_new(atz); |
| 646 |
dt = g_date_time_new(tz, year, month, day, hour, minute, seconds); |
| 647 |
a = g_date_time_to_unix(dt); |
| 648 |
g_time_zone_unref(tz); |
| 649 |
g_date_time_unref(dt); |
| 650 |
return a; |
| 651 |
} else |
| 652 |
#endif |
| 653 |
{ |
| 654 |
struct tm tm; |
| 655 |
time_t t; |
| 656 |
|
| 657 |
tm.tm_year = year - 1900; |
| 658 |
tm.tm_mon = month - 1; |
| 659 |
tm.tm_mday = day; |
| 660 |
tm.tm_hour = hour; |
| 661 |
tm.tm_min = minute; |
| 662 |
tm.tm_sec = seconds; |
| 663 |
tm.tm_wday = 0; |
| 664 |
tm.tm_yday = 0; |
| 665 |
tm.tm_isdst = -1; |
| 666 |
t = time(NULL); |
| 667 |
|
| 668 |
if (*atz == '-' || *atz == '+') { /* numeric timezone */ |
| 669 |
time_t lt, gt; |
| 670 |
time_t a; |
| 671 |
struct tm ltt, gtt; |
| 672 |
gint Hour = atoi(atz); |
| 673 |
gint Min = atoi(atz+4); |
| 674 |
|
| 675 |
if (Hour < 0) |
| 676 |
Min = -Min; |
| 677 |
tm.tm_hour -= Hour; |
| 678 |
tm.tm_min -= Min; |
| 679 |
tm.tm_isdst = 0; |
| 680 |
localtime_r(&t, <t); |
| 681 |
lt = mktime(<t); |
| 682 |
gmtime_r(&t, >t); |
| 683 |
gt = mktime(>t); |
| 684 |
tm.tm_sec += lt - gt; |
| 685 |
a = mktime(&tm); |
| 686 |
return a; |
| 687 |
} else if (*atz == 'Z' && *(atz+1) == '\0') { /* Z timezone */ |
| 688 |
time_t lt, gt; |
| 689 |
time_t a; |
| 690 |
struct tm ltt, gtt; |
| 691 |
|
| 692 |
tm.tm_isdst = 0; |
| 693 |
localtime_r(&t, <t); |
| 694 |
lt = mktime(<t); |
| 695 |
gmtime_r(&t, >t); |
| 696 |
gt = mktime(>t); |
| 697 |
tm.tm_sec += lt - gt; |
| 698 |
a = mktime(&tm); |
| 699 |
return a; |
| 700 |
} else { /* named timezone */ |
| 701 |
int pid; |
| 702 |
int fd[2]; |
| 703 |
char buf[101]; |
| 704 |
time_t a; |
| 705 |
ssize_t size; |
| 706 |
|
| 707 |
if (pipe(fd) == -1) |
| 708 |
return 1073741824; |
| 709 |
pid = fork(); |
| 710 |
switch (pid) { |
| 711 |
case -1: |
| 712 |
close(fd[0]); |
| 713 |
close(fd[1]); |
| 714 |
return 1073741824; |
| 715 |
break; |
| 716 |
case 0: |
| 717 |
close(fd[0]); |
| 718 |
setenv("TZ", atz, 1); |
| 719 |
tzset(); |
| 720 |
a = mktime(&tm); |
| 721 |
g_snprintf(buf, 100, "%d", (int)a); |
| 722 |
full_write(fd[1], buf, strlen(buf)); |
| 723 |
close(fd[1]); |
| 724 |
exit(0); |
| 725 |
default: |
| 726 |
close(fd[1]); |
| 727 |
size = full_read(fd[0], buf, 100); |
| 728 |
if (size < 0) size = 0; |
| 729 |
close(fd[0]); |
| 730 |
buf[size] = '\0'; |
| 731 |
waitpid(pid, NULL, 0); |
| 732 |
break; |
| 733 |
} |
| 734 |
return atoi(buf); |
| 735 |
} |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
static gboolean |
| 741 |
is_non_empty_string(const char *str) |
| 742 |
{ |
| 743 |
return str && str[0] != '\0'; |
| 744 |
} |
| 745 |
|
| 746 |
static char * |
| 747 |
build_url( |
| 748 |
S3Handle *hdl, |
| 749 |
const char *bucket, |
| 750 |
const char *key, |
| 751 |
const char *subresource, |
| 752 |
const char **query) |
| 753 |
{ |
| 754 |
GString *url = NULL; |
| 755 |
char *esc_bucket = NULL, *esc_key = NULL; |
| 756 |
|
| 757 |
if ((hdl->s3_api == S3_API_SWIFT_1 || hdl->s3_api == S3_API_SWIFT_2 || |
| 758 |
hdl->s3_api == S3_API_SWIFT_3 || hdl->s3_api == S3_API_OAUTH2) && |
| 759 |
hdl->x_storage_url) { |
| 760 |
url = g_string_new(hdl->x_storage_url); |
| 761 |
g_string_append(url, "/"); |
| 762 |
} else { |
| 763 |
/* scheme */ |
| 764 |
url = g_string_new("http"); |
| 765 |
if (hdl->use_ssl) |
| 766 |
g_string_append(url, "s"); |
| 767 |
|
| 768 |
g_string_append(url, "://"); |
| 769 |
|
| 770 |
/* domain */ |
| 771 |
if (hdl->use_subdomain && bucket) |
| 772 |
g_string_append_printf(url, "%s.%s", bucket, hdl->host); |
| 773 |
else |
| 774 |
g_string_append_printf(url, "%s", hdl->host); |
| 775 |
|
| 776 |
if (hdl->service_path) { |
| 777 |
g_string_append_printf(url, "%s/", hdl->service_path); |
| 778 |
} else { |
| 779 |
g_string_append(url, "/"); |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
/* path */ |
| 784 |
if (!hdl->use_subdomain && bucket) { |
| 785 |
/* curl_easy_escape addeded in 7.15.4 */ |
| 786 |
#if LIBCURL_VERSION_NUM >= 0x070f04 |
| 787 |
curl_version_info_data *info; |
| 788 |
/* check the runtime version too */ |
| 789 |
info = curl_version_info(CURLVERSION_NOW); |
| 790 |
if (info->version_num >= 0x070f04) |
| 791 |
esc_bucket = curl_easy_escape(hdl->curl, bucket, 0); |
| 792 |
else |
| 793 |
esc_bucket = curl_escape(bucket, 0); |
| 794 |
#else |
| 795 |
esc_bucket = curl_escape(bucket, 0); |
| 796 |
#endif |
| 797 |
if (!esc_bucket) goto cleanup; |
| 798 |
g_string_append_printf(url, "%s", esc_bucket); |
| 799 |
if (key) |
| 800 |
g_string_append(url, "/"); |
| 801 |
curl_free(esc_bucket); |
| 802 |
} |
| 803 |
|
| 804 |
if (key) { |
| 805 |
/* curl_easy_escape addeded in 7.15.4 */ |
| 806 |
#if LIBCURL_VERSION_NUM >= 0x070f04 |
| 807 |
curl_version_info_data *info; |
| 808 |
/* check the runtime version too */ |
| 809 |
info = curl_version_info(CURLVERSION_NOW); |
| 810 |
if (info->version_num >= 0x070f04) |
| 811 |
esc_key = curl_easy_escape(hdl->curl, key, 0); |
| 812 |
else |
| 813 |
esc_key = curl_escape(key, 0); |
| 814 |
#else |
| 815 |
esc_key = curl_escape(key, 0); |
| 816 |
#endif |
| 817 |
if (!esc_key) goto cleanup; |
| 818 |
g_string_append_printf(url, "%s", esc_key); |
| 819 |
curl_free(esc_key); |
| 820 |
} |
| 821 |
|
| 822 |
if (url->str[strlen(url->str)-1] == '/') { |
| 823 |
g_string_truncate(url, strlen(url->str)-1); |
| 824 |
} |
| 825 |
|
| 826 |
/* query string */ |
| 827 |
if (subresource || query || (hdl->s3_api == S3_API_CASTOR && hdl->tenant_name)) |
| 828 |
g_string_append(url, "?"); |
| 829 |
|
| 830 |
if (subresource) |
| 831 |
g_string_append(url, subresource); |
| 832 |
|
| 833 |
if (subresource && query) |
| 834 |
g_string_append(url, "&"); |
| 835 |
|
| 836 |
if (query) { |
| 837 |
const char **q; |
| 838 |
gboolean first = TRUE; |
| 839 |
|
| 840 |
for (q = query; *q != NULL; q++) { |
| 841 |
if (!first) { |
| 842 |
g_string_append_c(url, '&'); |
| 843 |
} |
| 844 |
g_string_append(url, *q); |
| 845 |
first = FALSE; |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
/* add CAStor tenant domain override query arg */ |
| 850 |
if (hdl->s3_api == S3_API_CASTOR && hdl->tenant_name) { |
| 851 |
if (subresource || query) { |
| 852 |
g_string_append(url, "&"); |
| 853 |
} |
| 854 |
g_string_append_printf(url, "domain=%s", hdl->tenant_name); |
| 855 |
} |
| 856 |
|
| 857 |
cleanup: |
| 858 |
|
| 859 |
return g_string_free(url, FALSE); |
| 860 |
} |
| 861 |
|
| 862 |
static struct curl_slist * |
| 863 |
authenticate_request(S3Handle *hdl, |
| 864 |
const char *verb, |
| 865 |
const char *bucket, |
| 866 |
const char *key, |
| 867 |
const char *subresource, |
| 868 |
const char **query, |
| 869 |
const char *md5_hash, |
| 870 |
const char *data_SHA256Hash, |
| 871 |
const char *content_type, |
| 872 |
const size_t content_length, |
| 873 |
const char *project_id) |
| 874 |
{ |
| 875 |
time_t t; |
| 876 |
struct tm tmp; |
| 877 |
char *date = NULL; |
| 878 |
char *szS3Date = NULL; |
| 879 |
char *zulu_date = NULL; |
| 880 |
char *buf = NULL; |
| 881 |
#if (defined OPENSSL_VERSION_NUMBER && OPENSSL_VERSION_NUMBER < 0x10100000L) \ |
| 882 |
|| defined LIBRESSL_VERSION_NUMBER |
| 883 |
HMAC_CTX ctx; |
| 884 |
#else |
| 885 |
HMAC_CTX *ctx; |
| 886 |
#endif |
| 887 |
GByteArray *md = NULL; |
| 888 |
char *auth_base64 = NULL; |
| 889 |
struct curl_slist *headers = NULL; |
| 890 |
char *esc_bucket = NULL, *esc_key = NULL; |
| 891 |
GString *auth_string = NULL; |
| 892 |
char *reps = NULL; |
| 893 |
|
| 894 |
/* From RFC 2616 */ |
| 895 |
static const char *wkday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; |
| 896 |
static const char *month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 897 |
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
| 898 |
|
| 899 |
/* calculate the date */ |
| 900 |
t = time(NULL); |
| 901 |
|
| 902 |
/* sync clock with amazon s3 */ |
| 903 |
t = t + hdl->time_offset_with_s3; |
| 904 |
|
| 905 |
#ifdef _WIN32 |
| 906 |
if (!gmtime_s(&tmp, &t)) g_debug("localtime error"); |
| 907 |
#else |
| 908 |
if (!gmtime_r(&t, &tmp)) perror("localtime"); |
| 909 |
#endif |
| 910 |
|
| 911 |
|
| 912 |
date = g_strdup_printf("%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 913 |
wkday[tmp.tm_wday], tmp.tm_mday, month[tmp.tm_mon], 1900+tmp.tm_year, |
| 914 |
tmp.tm_hour, tmp.tm_min, tmp.tm_sec); |
| 915 |
|
| 916 |
szS3Date = g_strdup_printf("%04d%02d%02d",1900+tmp.tm_year,tmp.tm_mon+1,tmp.tm_mday); |
| 917 |
|
| 918 |
zulu_date = g_strdup_printf("%04d%02d%02dT%02d%02d%02dZ", |
| 919 |
1900+tmp.tm_year, tmp.tm_mon+1, tmp.tm_mday, |
| 920 |
tmp.tm_hour, tmp.tm_min, tmp.tm_sec); |
| 921 |
if (hdl->s3_api == S3_API_SWIFT_1) { |
| 922 |
if (!bucket) { |
| 923 |
buf = g_strdup_printf("X-Auth-User: %s", hdl->swift_account_id); |
| 924 |
headers = curl_slist_append(headers, buf); |
| 925 |
g_free(buf); |
| 926 |
buf = g_strdup_printf("X-Auth-Key: %s", hdl->swift_access_key); |
| 927 |
headers = curl_slist_append(headers, buf); |
| 928 |
g_free(buf); |
| 929 |
} else { |
| 930 |
buf = g_strdup_printf("X-Auth-Token: %s", hdl->x_auth_token); |
| 931 |
headers = curl_slist_append(headers, buf); |
| 932 |
g_free(buf); |
| 933 |
} |
| 934 |
} else if (hdl->s3_api == S3_API_SWIFT_2) { |
| 935 |
if (bucket) { |
| 936 |
buf = g_strdup_printf("X-Auth-Token: %s", hdl->x_auth_token); |
| 937 |
headers = curl_slist_append(headers, buf); |
| 938 |
g_free(buf); |
| 939 |
buf = g_strdup_printf("Accept: %s", "application/xml"); |
| 940 |
headers = curl_slist_append(headers, buf); |
| 941 |
g_free(buf); |
| 942 |
} else { |
| 943 |
buf = g_strdup_printf("Accept: %s", "application/json"); |
| 944 |
headers = curl_slist_append(headers, buf); |
| 945 |
g_free(buf); |
| 946 |
} |
| 947 |
} else if (hdl->s3_api == S3_API_SWIFT_3) { |
| 948 |
if (bucket) { |
| 949 |
buf = g_strdup_printf("X-Auth-Token: %s", hdl->x_auth_token); |
| 950 |
headers = curl_slist_append(headers, buf); |
| 951 |
g_free(buf); |
| 952 |
buf = g_strdup_printf("Accept: %s", "application/xml"); |
| 953 |
headers = curl_slist_append(headers, buf); |
| 954 |
g_free(buf); |
| 955 |
} else { |
| 956 |
buf = g_strdup_printf("Accept: %s", "application/json"); |
| 957 |
headers = curl_slist_append(headers, buf); |
| 958 |
g_free(buf); |
| 959 |
} |
| 960 |
} else if (hdl->s3_api == S3_API_OAUTH2) { |
| 961 |
if (bucket) { |
| 962 |
buf = g_strdup_printf("Authorization: Bearer %s", hdl->access_token); |
| 963 |
headers = curl_slist_append(headers, buf); |
| 964 |
g_free(buf); |
| 965 |
} |
| 966 |
} else if (hdl->s3_api == S3_API_CASTOR) { |
| 967 |
if (g_str_equal(verb, "PUT") || g_str_equal(verb, "POST")) { |
| 968 |
if (key) { |
| 969 |
buf = g_strdup("CAStor-Application: Amanda"); |
| 970 |
headers = curl_slist_append(headers, buf); |
| 971 |
g_free(buf); |
| 972 |
reps = g_strdup(hdl->reps); /* object replication level */ |
| 973 |
} else { |
| 974 |
reps = g_strdup(hdl->reps_bucket); /* bucket replication level */ |
| 975 |
} |
| 976 |
|
| 977 |
/* set object replicas in lifepoint */ |
| 978 |
buf = g_strdup_printf("lifepoint: [] reps=%s", reps); |
| 979 |
headers = curl_slist_append(headers, buf); |
| 980 |
g_free(buf); |
| 981 |
g_free(reps); |
| 982 |
} |
| 983 |
} else if (hdl->s3_api == S3_API_AWS4) { |
| 984 |
/* http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html */ |
| 985 |
/* http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-auth-using-authorization-header.html */ |
| 986 |
|
| 987 |
GString *strSignedHeaders = g_string_new(""); |
| 988 |
GString *string_to_sign; |
| 989 |
char *canonical_hash = NULL; |
| 990 |
unsigned char *strSecretKey = NULL; |
| 991 |
unsigned char *signkey1 = NULL; |
| 992 |
unsigned char *signkey2 = NULL; |
| 993 |
unsigned char *signkey3 = NULL; |
| 994 |
unsigned char *signingKey = NULL; |
| 995 |
unsigned char *signature = NULL; |
| 996 |
unsigned char *signatureHex = NULL; |
| 997 |
|
| 998 |
/* verb */ |
| 999 |
auth_string = g_string_new(verb); |
| 1000 |
g_string_append(auth_string, "\n"); |
| 1001 |
|
| 1002 |
/* CanonicalizedResource */ |
| 1003 |
g_string_append(auth_string, "/"); |
| 1004 |
|
| 1005 |
if (hdl->use_subdomain) { |
| 1006 |
if (key) { |
| 1007 |
char *esc_key = s3_uri_encode(key, 0); |
| 1008 |
g_string_append(auth_string, esc_key); |
| 1009 |
g_free(esc_key); |
| 1010 |
} |
| 1011 |
} else { |
| 1012 |
g_string_append(auth_string, bucket); |
| 1013 |
if (key) { |
| 1014 |
char *esc_key = s3_uri_encode(key, 0); |
| 1015 |
g_string_append(auth_string, "/"); |
| 1016 |
g_string_append(auth_string, esc_key); |
| 1017 |
g_free(esc_key); |
| 1018 |
} |
| 1019 |
} |
| 1020 |
g_string_append(auth_string, "\n"); |
| 1021 |
|
| 1022 |
if (query) { |
| 1023 |
gboolean sub_done = !subresource; |
| 1024 |
gboolean first = TRUE; |
| 1025 |
const char **q; |
| 1026 |
for (q = query; *q != NULL; q++) { |
| 1027 |
if (!first) { |
| 1028 |
g_string_append_c(auth_string, '&'); |
| 1029 |
} |
| 1030 |
if (!sub_done && strcmp(subresource, *q) < 0) { |
| 1031 |
g_string_append(auth_string, subresource); |
| 1032 |
g_string_append_c(auth_string, '='); |
| 1033 |
g_string_append_c(auth_string, '&'); |
| 1034 |
sub_done = TRUE; |
| 1035 |
} |
| 1036 |
g_string_append(auth_string, *q); |
| 1037 |
first = FALSE; |
| 1038 |
} |
| 1039 |
if (!sub_done) { |
| 1040 |
g_string_append_c(auth_string, '&'); |
| 1041 |
g_string_append(auth_string, subresource); |
| 1042 |
g_string_append_c(auth_string, '='); |
| 1043 |
} |
| 1044 |
} else if (subresource) { |
| 1045 |
g_string_append(auth_string, subresource); |
| 1046 |
g_string_append(auth_string, "="); |
| 1047 |
} |
| 1048 |
g_string_append(auth_string, "\n"); |
| 1049 |
|
| 1050 |
if (subresource && strcmp(subresource, "lifecycle") == 0 && md5_hash && '\0' != md5_hash[0]) { |
| 1051 |
g_string_append(auth_string, "content-md5:"); |
| 1052 |
g_string_append(auth_string, md5_hash); |
| 1053 |
g_string_append(auth_string, "\n"); |
| 1054 |
|
| 1055 |
buf = g_strdup_printf("Content-MD5: %s", md5_hash); |
| 1056 |
headers = curl_slist_append(headers, buf); |
| 1057 |
|
| 1058 |
g_string_append(strSignedHeaders, "content-md5;"); |
| 1059 |
g_free(buf); |
| 1060 |
} |
| 1061 |
|
| 1062 |
/* Header must be in alphebetic order */ |
| 1063 |
if (hdl->use_subdomain) { |
| 1064 |
g_string_append(auth_string, "host:"); |
| 1065 |
g_string_append(auth_string, bucket); |
| 1066 |
g_string_append(auth_string, "."); |
| 1067 |
g_string_append(auth_string, hdl->host_without_port); |
| 1068 |
g_string_append(auth_string, "\n"); |
| 1069 |
} else { |
| 1070 |
g_string_append(auth_string, "host:"); |
| 1071 |
g_string_append(auth_string, hdl->host_without_port); |
| 1072 |
g_string_append(auth_string, "\n"); |
| 1073 |
} |
| 1074 |
g_string_append(strSignedHeaders, "host"); |
| 1075 |
|
| 1076 |
g_string_append(auth_string, "x-amz-content-sha256:"); |
| 1077 |
g_string_append(auth_string, data_SHA256Hash); |
| 1078 |
g_string_append(auth_string, "\n"); |
| 1079 |
g_string_append(strSignedHeaders, ";x-amz-content-sha256"); |
| 1080 |
|
| 1081 |
g_string_append(auth_string, "x-amz-date:"); |
| 1082 |
g_string_append(auth_string, zulu_date); |
| 1083 |
g_string_append(auth_string, "\n"); |
| 1084 |
g_string_append(strSignedHeaders, ";x-amz-date"); |
| 1085 |
|
| 1086 |
if (hdl->server_side_encryption_header && |
| 1087 |
is_non_empty_string(hdl->server_side_encryption)) { |
| 1088 |
g_string_append(auth_string, AMAZON_SERVER_SIDE_ENCRYPTION_HEADER); |
| 1089 |
g_string_append(auth_string, ":"); |
| 1090 |
g_string_append(auth_string, hdl->server_side_encryption); |
| 1091 |
g_string_append(auth_string, "\n"); |
| 1092 |
g_string_append(strSignedHeaders, ";"AMAZON_SERVER_SIDE_ENCRYPTION_HEADER); |
| 1093 |
|
| 1094 |
buf = g_strdup_printf(AMAZON_SERVER_SIDE_ENCRYPTION_HEADER ": %s", |
| 1095 |
hdl->server_side_encryption); |
| 1096 |
headers = curl_slist_append(headers, buf); |
| 1097 |
g_free(buf); |
| 1098 |
} |
| 1099 |
|
| 1100 |
if ((!subresource || !g_strstr_len(subresource, -1, "uploadId")) && |
| 1101 |
(!query || !query[0] || !g_strstr_len(query[0], -1, "uploadId")) && |
| 1102 |
(!query || !query[1] || !g_strstr_len(query[1], -1, "uploadId")) && |
| 1103 |
is_non_empty_string(hdl->storage_class)) { |
| 1104 |
g_string_append(auth_string, AMAZON_STORAGE_CLASS_HEADER); |
| 1105 |
g_string_append(auth_string, ":"); |
| 1106 |
g_string_append(auth_string, hdl->storage_class); |
| 1107 |
g_string_append(auth_string, "\n"); |
| 1108 |
g_string_append(strSignedHeaders, ";"AMAZON_STORAGE_CLASS_HEADER); |
| 1109 |
|
| 1110 |
buf = g_strdup_printf(AMAZON_STORAGE_CLASS_HEADER ": %s", |
| 1111 |
hdl->storage_class); |
| 1112 |
headers = curl_slist_append(headers, buf); |
| 1113 |
g_free(buf); |
| 1114 |
} |
| 1115 |
|
| 1116 |
/* no more header */ |
| 1117 |
g_string_append(auth_string, "\n"); |
| 1118 |
|
| 1119 |
g_string_append(auth_string, strSignedHeaders->str); |
| 1120 |
g_string_append(auth_string, "\n"); |
| 1121 |
g_string_append(auth_string, data_SHA256Hash); |
| 1122 |
|
| 1123 |
canonical_hash = s3_compute_sha256_hash((unsigned char *)auth_string->str, auth_string->len); |
| 1124 |
|
| 1125 |
if (!hdl->bucket_location) { |
| 1126 |
hdl->bucket_location = g_strdup("us-east-1"); |
| 1127 |
} |
| 1128 |
string_to_sign = g_string_new("AWS4-HMAC-SHA256\n"); |
| 1129 |
g_string_append(string_to_sign, zulu_date); |
| 1130 |
g_string_append(string_to_sign, "\n"); |
| 1131 |
g_string_append(string_to_sign, szS3Date); |
| 1132 |
g_string_append(string_to_sign, "/"); |
| 1133 |
g_string_append(string_to_sign, hdl->bucket_location); |
| 1134 |
g_string_append(string_to_sign, "/s3/aws4_request"); |
| 1135 |
g_string_append(string_to_sign, "\n"); |
| 1136 |
g_string_append(string_to_sign, canonical_hash); |
| 1137 |
|
| 1138 |
//Calculate the AWS Signature Version 4 |
| 1139 |
strSecretKey = (unsigned char *)g_strdup_printf("AWS4%s", hdl->secret_key); |
| 1140 |
|
| 1141 |
signkey1 = EncodeHMACSHA256(strSecretKey, strlen((char *)strSecretKey), |
| 1142 |
szS3Date, strlen(szS3Date)); |
| 1143 |
|
| 1144 |
signkey2 = EncodeHMACSHA256(signkey1, 32, hdl->bucket_location, strlen(hdl->bucket_location)); |
| 1145 |
|
| 1146 |
signkey3 = EncodeHMACSHA256(signkey2, 32, "s3", 2); |
| 1147 |
|
| 1148 |
signingKey = EncodeHMACSHA256(signkey3, 32, "aws4_request", 12); |
| 1149 |
|
| 1150 |
signature = EncodeHMACSHA256(signingKey, 32, string_to_sign->str, (int)string_to_sign->len); |
| 1151 |
signatureHex = s3_tohex(signature, 32); |
| 1152 |
|
| 1153 |
buf = g_strdup_printf("x-amz-content-sha256: %s", data_SHA256Hash); |
| 1154 |
headers = curl_slist_append(headers, buf); |
| 1155 |
g_free(buf); |
| 1156 |
|
| 1157 |
buf = g_strdup_printf("x-amz-date: %s", zulu_date); |
| 1158 |
headers = curl_slist_append(headers, buf); |
| 1159 |
g_free(buf); |
| 1160 |
|
| 1161 |
buf = g_strdup_printf("Authorization: AWS4-HMAC-SHA256 Credential=%s/%s/%s/s3/aws4_request,SignedHeaders=%s,Signature=%s", hdl->access_key, szS3Date, hdl->bucket_location, strSignedHeaders->str, signatureHex); |
| 1162 |
headers = curl_slist_append(headers, buf); |
| 1163 |
g_free(buf); |
| 1164 |
|
| 1165 |
if (hdl->verbose) { |
| 1166 |
g_debug("bucket: %s", bucket); |
| 1167 |
g_debug("auth_string->str: %s", auth_string->str); |
| 1168 |
g_debug("string_to_sign: %s", string_to_sign->str); |
| 1169 |
g_debug("strSignedHeaders: %s", strSignedHeaders->str); |
| 1170 |
g_debug("canonical_hash: %s", canonical_hash); |
| 1171 |
g_debug("strSecretKey: %s", strSecretKey); |
| 1172 |
g_debug("signatureHex: %s", signatureHex); |
| 1173 |
} |
| 1174 |
|
| 1175 |
g_free(canonical_hash); |
| 1176 |
g_free(strSecretKey); |
| 1177 |
g_free(signkey1); |
| 1178 |
g_free(signkey2); |
| 1179 |
g_free(signkey3); |
| 1180 |
g_free(signingKey); |
| 1181 |
g_free(signature); |
| 1182 |
g_free(signatureHex); |
| 1183 |
md5_hash = NULL; |
| 1184 |
|
| 1185 |
} else { /* hdl->s3_api == S3_API_S3 */ |
| 1186 |
/* Build the string to sign, per the S3 spec. |
| 1187 |
* See: "Authenticating REST Requests" - API Version 2006-03-01 pg 58 |
| 1188 |
* http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html |
| 1189 |
*/ |
| 1190 |
|
| 1191 |
/* verb */ |
| 1192 |
auth_string = g_string_new(verb); |
| 1193 |
g_string_append(auth_string, "\n"); |
| 1194 |
|
| 1195 |
/* Content-MD5 header */ |
| 1196 |
if (md5_hash) |
| 1197 |
g_string_append(auth_string, md5_hash); |
| 1198 |
g_string_append(auth_string, "\n"); |
| 1199 |
|
| 1200 |
if (content_type) { |
| 1201 |
g_string_append(auth_string, content_type); |
| 1202 |
} |
| 1203 |
g_string_append(auth_string, "\n"); |
| 1204 |
|
| 1205 |
/* Date */ |
| 1206 |
g_string_append(auth_string, date); |
| 1207 |
g_string_append(auth_string, "\n"); |
| 1208 |
|
| 1209 |
/* CanonicalizedAmzHeaders, sorted lexicographically */ |
| 1210 |
if (is_non_empty_string(hdl->user_token)) { |
| 1211 |
g_string_append(auth_string, AMAZON_SECURITY_HEADER); |
| 1212 |
g_string_append(auth_string, ":"); |
| 1213 |
g_string_append(auth_string, hdl->user_token); |
| 1214 |
g_string_append(auth_string, ","); |
| 1215 |
g_string_append(auth_string, STS_PRODUCT_TOKEN); |
| 1216 |
g_string_append(auth_string, "\n"); |
| 1217 |
} |
| 1218 |
|
| 1219 |
/* CanonicalizedAmzHeaders, sorted lexicographically */ |
| 1220 |
if (is_non_empty_string(hdl->session_token)) { |
| 1221 |
g_string_append(auth_string, AMAZON_SECURITY_HEADER); |
| 1222 |
g_string_append(auth_string, ":"); |
| 1223 |
g_string_append(auth_string, hdl->session_token); |
| 1224 |
g_string_append(auth_string, "\n"); |
| 1225 |
} |
| 1226 |
|
| 1227 |
if (hdl->server_side_encryption_header && |
| 1228 |
is_non_empty_string(hdl->server_side_encryption)) { |
| 1229 |
g_string_append(auth_string, AMAZON_SERVER_SIDE_ENCRYPTION_HEADER); |
| 1230 |
g_string_append(auth_string, ":"); |
| 1231 |
g_string_append(auth_string, hdl->server_side_encryption); |
| 1232 |
g_string_append(auth_string, "\n"); |
| 1233 |
} |
| 1234 |
|
| 1235 |
if ((!subresource || !g_strstr_len(subresource, -1, "uploadId")) && |
| 1236 |
hdl->s3_api == S3_API_S3 && |
| 1237 |
is_non_empty_string(hdl->storage_class)) { |
| 1238 |
g_string_append(auth_string, AMAZON_STORAGE_CLASS_HEADER); |
| 1239 |
g_string_append(auth_string, ":"); |
| 1240 |
g_string_append(auth_string, hdl->storage_class); |
| 1241 |
g_string_append(auth_string, "\n"); |
| 1242 |
} |
| 1243 |
|
| 1244 |
/* CanonicalizedResource */ |
| 1245 |
if (hdl->service_path) { |
| 1246 |
g_string_append(auth_string, hdl->service_path); |
| 1247 |
} |
| 1248 |
g_string_append(auth_string, "/"); |
| 1249 |
if (bucket) { |
| 1250 |
if (hdl->use_subdomain) |
| 1251 |
g_string_append(auth_string, bucket); |
| 1252 |
else { |
| 1253 |
esc_bucket = curl_escape(bucket, 0); |
| 1254 |
if (!esc_bucket) goto cleanup; |
| 1255 |
g_string_append(auth_string, esc_bucket); |
| 1256 |
} |
| 1257 |
} |
| 1258 |
|
| 1259 |
if (bucket && (hdl->use_subdomain || key)) |
| 1260 |
g_string_append(auth_string, "/"); |
| 1261 |
|
| 1262 |
if (key) { |
| 1263 |
esc_key = curl_escape(key, 0); |
| 1264 |
if (!esc_key) goto cleanup; |
| 1265 |
g_string_append(auth_string, esc_key); |
| 1266 |
} |
| 1267 |
|
| 1268 |
if (subresource) { |
| 1269 |
g_string_append(auth_string, "?"); |
| 1270 |
g_string_append(auth_string, subresource); |
| 1271 |
} |
| 1272 |
|
| 1273 |
/* run HMAC-SHA1 on the canonicalized string */ |
| 1274 |
md = g_byte_array_sized_new(EVP_MAX_MD_SIZE+1); |
| 1275 |
#if (defined OPENSSL_VERSION_NUMBER && OPENSSL_VERSION_NUMBER < 0x10100000L) \ |
| 1276 |
|| defined LIBRESSL_VERSION_NUMBER |
| 1277 |
HMAC_CTX_init(&ctx); |
| 1278 |
HMAC_Init_ex(&ctx, hdl->secret_key, (int) strlen(hdl->secret_key), |
| 1279 |
EVP_sha1(), NULL); |
| 1280 |
HMAC_Update(&ctx, (unsigned char*) auth_string->str, auth_string->len); |
| 1281 |
HMAC_Final(&ctx, md->data, &md->len); |
| 1282 |
HMAC_CTX_cleanup(&ctx); |
| 1283 |
#else |
| 1284 |
ctx = HMAC_CTX_new(); |
| 1285 |
HMAC_CTX_reset(ctx); |
| 1286 |
HMAC_Init_ex(ctx, hdl->secret_key, (int) strlen(hdl->secret_key), |
| 1287 |
EVP_sha1(), NULL); |
| 1288 |
HMAC_Update(ctx, (unsigned char*) auth_string->str, auth_string->len); |
| 1289 |
HMAC_Final(ctx, md->data, &md->len); |
| 1290 |
HMAC_CTX_free(ctx); |
| 1291 |
#endif |
| 1292 |
auth_base64 = s3_base64_encode(md); |
| 1293 |
/* append the new headers */ |
| 1294 |
if (is_non_empty_string(hdl->user_token)) { |
| 1295 |
/* Devpay headers are included in hash. */ |
| 1296 |
buf = g_strdup_printf(AMAZON_SECURITY_HEADER ": %s", |
| 1297 |
hdl->user_token); |
| 1298 |
headers = curl_slist_append(headers, buf); |
| 1299 |
g_free(buf); |
| 1300 |
|
| 1301 |
buf = g_strdup_printf(AMAZON_SECURITY_HEADER ": %s", |
| 1302 |
STS_PRODUCT_TOKEN); |
| 1303 |
headers = curl_slist_append(headers, buf); |
| 1304 |
g_free(buf); |
| 1305 |
} |
| 1306 |
|
| 1307 |
if (is_non_empty_string(hdl->session_token)) { |
| 1308 |
/* Devpay headers are included in hash. */ |
| 1309 |
buf = g_strdup_printf(AMAZON_SECURITY_HEADER ": %s", |
| 1310 |
hdl->session_token); |
| 1311 |
headers = curl_slist_append(headers, buf); |
| 1312 |
g_free(buf); |
| 1313 |
} |
| 1314 |
|
| 1315 |
if (hdl->server_side_encryption_header && |
| 1316 |
is_non_empty_string(hdl->server_side_encryption)) { |
| 1317 |
buf = g_strdup_printf(AMAZON_SERVER_SIDE_ENCRYPTION_HEADER ": %s", |
| 1318 |
hdl->server_side_encryption); |
| 1319 |
headers = curl_slist_append(headers, buf); |
| 1320 |
g_free(buf); |
| 1321 |
} |
| 1322 |
|
| 1323 |
if ((!subresource || !g_strstr_len(subresource, -1, "uploadId")) && |
| 1324 |
hdl->s3_api != S3_API_OAUTH2 && |
| 1325 |
is_non_empty_string(hdl->storage_class)) { |
| 1326 |
buf = g_strdup_printf(AMAZON_STORAGE_CLASS_HEADER ": %s", |
| 1327 |
hdl->storage_class); |
| 1328 |
headers = curl_slist_append(headers, buf); |
| 1329 |
g_free(buf); |
| 1330 |
} |
| 1331 |
|
| 1332 |
/* Remove Content-Type header */ |
| 1333 |
buf = g_strdup("Content-Type:"); |
| 1334 |
headers = curl_slist_append(headers, buf); |
| 1335 |
g_free(buf); |
| 1336 |
|
| 1337 |
buf = g_strdup_printf("Authorization: AWS %s:%s", |
| 1338 |
hdl->access_key, auth_base64); |
| 1339 |
headers = curl_slist_append(headers, buf); |
| 1340 |
g_free(buf); |
| 1341 |
} |
| 1342 |
|
| 1343 |
if (md5_hash && '\0' != md5_hash[0]) { |
| 1344 |
buf = g_strdup_printf("Content-MD5: %s", md5_hash); |
| 1345 |
headers = curl_slist_append(headers, buf); |
| 1346 |
g_free(buf); |
| 1347 |
} |
| 1348 |
if (content_length > 0) { |
| 1349 |
buf = g_strdup_printf("Content-Length: %zu", content_length); |
| 1350 |
headers = curl_slist_append(headers, buf); |
| 1351 |
g_free(buf); |
| 1352 |
} |
| 1353 |
|
| 1354 |
if (content_type) { |
| 1355 |
buf = g_strdup_printf("Content-Type: %s", content_type); |
| 1356 |
headers = curl_slist_append(headers, buf); |
| 1357 |
g_free(buf); |
| 1358 |
} |
| 1359 |
|
| 1360 |
if (hdl->s3_api == S3_API_OAUTH2) { |
| 1361 |
buf = g_strdup_printf("x-goog-api-version: 2"); |
| 1362 |
headers = curl_slist_append(headers, buf); |
| 1363 |
g_free(buf); |
| 1364 |
} |
| 1365 |
|
| 1366 |
if (project_id && hdl->s3_api == S3_API_OAUTH2) { |
| 1367 |
buf = g_strdup_printf("x-goog-project-id: %s", project_id); |
| 1368 |
headers = curl_slist_append(headers, buf); |
| 1369 |
g_free(buf); |
| 1370 |
} |
| 1371 |
|
| 1372 |
buf = g_strdup_printf("Date: %s", date); |
| 1373 |
headers = curl_slist_append(headers, buf); |
| 1374 |
g_free(buf); |
| 1375 |
|
| 1376 |
cleanup: |
| 1377 |
g_free(date); |
| 1378 |
g_free(szS3Date); |
| 1379 |
g_free(esc_bucket); |
| 1380 |
g_free(esc_key); |
| 1381 |
if (md) g_byte_array_free(md, TRUE); |
| 1382 |
g_free(auth_base64); |
| 1383 |
if (auth_string) g_string_free(auth_string, TRUE); |
| 1384 |
|
| 1385 |
return headers; |
| 1386 |
} |
| 1387 |
|
| 1388 |
/* Functions for a SAX parser to parse the XML failure from Amazon */ |
| 1389 |
|
| 1390 |
/* Private structure for our "thunk", which tracks where the user is in the list |
| 1391 |
* * of keys. */ |
| 1392 |
struct failure_thunk { |
| 1393 |
gboolean want_text; |
| 1394 |
|
| 1395 |
gboolean in_title; |
| 1396 |
gboolean in_body; |
| 1397 |
gboolean in_code; |
| 1398 |
gboolean in_message; |
| 1399 |
gboolean in_details; |
| 1400 |
gboolean in_access; |
| 1401 |
gboolean in_token; |
| 1402 |
gboolean in_serviceCatalog; |
| 1403 |
gboolean in_service; |
| 1404 |
gboolean in_endpoint; |
| 1405 |
gboolean in_uploadId; |
| 1406 |
gint in_others; |
| 1407 |
|
| 1408 |
gchar *text; |
| 1409 |
gsize text_len; |
| 1410 |
|
| 1411 |
gchar *message; |
| 1412 |
gchar *details; |
| 1413 |
gchar *error_name; |
| 1414 |
gchar *token_id; |
| 1415 |
gchar *service_type; |
| 1416 |
gchar *service_public_url; |
| 1417 |
gint64 expires; |
| 1418 |
gchar *uploadId; |
| 1419 |
|
| 1420 |
gchar *bucket_location; |
| 1421 |
}; |
| 1422 |
|
| 1423 |
static void |
| 1424 |
failure_start_element(GMarkupParseContext *context G_GNUC_UNUSED, |
| 1425 |
const gchar *element_name, |
| 1426 |
const gchar **attribute_names, |
| 1427 |
const gchar **attribute_values, |
| 1428 |
gpointer user_data, |
| 1429 |
GError **error G_GNUC_UNUSED) |
| 1430 |
{ |
| 1431 |
struct failure_thunk *thunk = (struct failure_thunk *)user_data; |
| 1432 |
const gchar **att_name, **att_value; |
| 1433 |
|
| 1434 |
if (g_ascii_strcasecmp(element_name, "title") == 0) { |
| 1435 |
thunk->in_title = 1; |
| 1436 |
thunk->in_others = 0; |
| 1437 |
thunk->want_text = 1; |
| 1438 |
} else if (g_ascii_strcasecmp(element_name, "body") == 0) { |
| 1439 |
thunk->in_body = 1; |
| 1440 |
thunk->in_others = 0; |
| 1441 |
thunk->want_text = 1; |
| 1442 |
} else if (g_ascii_strcasecmp(element_name, "code") == 0) { |
| 1443 |
thunk->in_code = 1; |
| 1444 |
thunk->in_others = 0; |
| 1445 |
thunk->want_text = 1; |
| 1446 |
} else if (g_ascii_strcasecmp(element_name, "message") == 0) { |
| 1447 |
thunk->in_message = 1; |
| 1448 |
thunk->in_others = 0; |
| 1449 |
thunk->want_text = 1; |
| 1450 |
} else if (g_ascii_strcasecmp(element_name, "details") == 0) { |
| 1451 |
thunk->in_details = 1; |
| 1452 |
thunk->in_others = 0; |
| 1453 |
thunk->want_text = 1; |
| 1454 |
} else if (g_ascii_strcasecmp(element_name, "access") == 0) { |
| 1455 |
thunk->in_access = 1; |
| 1456 |
thunk->in_others = 0; |
| 1457 |
} else if (g_ascii_strcasecmp(element_name, "token") == 0) { |
| 1458 |
thunk->in_token = 1; |
| 1459 |
thunk->in_others = 0; |
| 1460 |
for (att_name=attribute_names, att_value=attribute_values; |
| 1461 |
*att_name != NULL; |
| 1462 |
att_name++, att_value++) { |
| 1463 |
if (g_str_equal(*att_name, "id")) { |
| 1464 |
thunk->token_id = g_strdup(*att_value); |
| 1465 |
} |
| 1466 |
if (g_str_equal(*att_name, "expires") && strlen(*att_value) >= 19) { |
| 1467 |
thunk->expires = rfc3339_date(*att_value) - 600; |
| 1468 |
} |
| 1469 |
} |
| 1470 |
} else if (g_ascii_strcasecmp(element_name, "serviceCatalog") == 0) { |
| 1471 |
thunk->in_serviceCatalog = 1; |
| 1472 |
thunk->in_others = 0; |
| 1473 |
} else if (g_ascii_strcasecmp(element_name, "service") == 0) { |
| 1474 |
thunk->in_service = 1; |
| 1475 |
thunk->in_others = 0; |
| 1476 |
for (att_name=attribute_names, att_value=attribute_values; |
| 1477 |
*att_name != NULL; |
| 1478 |
att_name++, att_value++) { |
| 1479 |
if (g_str_equal(*att_name, "type")) { |
| 1480 |
thunk->service_type = g_strdup(*att_value); |
| 1481 |
} |
| 1482 |
} |
| 1483 |
} else if (g_ascii_strcasecmp(element_name, "endpoint") == 0) { |
| 1484 |
thunk->in_endpoint = 1; |
| 1485 |
thunk->in_others = 0; |
| 1486 |
if (thunk->service_type && |
| 1487 |
g_str_equal(thunk->service_type, "object-store")) { |
| 1488 |
char *service_public_url = NULL; |
| 1489 |
char *region = NULL; |
| 1490 |
for (att_name=attribute_names, att_value=attribute_values; |
| 1491 |
*att_name != NULL; |
| 1492 |
att_name++, att_value++) { |
| 1493 |
if (g_str_equal(*att_name, "publicURL")) { |
| 1494 |
service_public_url = g_strdup(*att_value); |
| 1495 |
} |
| 1496 |
if (g_str_equal(*att_name, "region")) { |
| 1497 |
region = g_strdup(*att_value); |
| 1498 |
} |
| 1499 |
} |
| 1500 |
if (region && service_public_url) { |
| 1501 |
if (!thunk->bucket_location || |
| 1502 |
strcmp(thunk->bucket_location, region) == 0) { |
| 1503 |
thunk->service_public_url = service_public_url; |
| 1504 |
} else { |
| 1505 |
g_free(service_public_url); |
| 1506 |
} |
| 1507 |
} else { |
| 1508 |
thunk->service_public_url = service_public_url; |
| 1509 |
} |
| 1510 |
g_free(region); |
| 1511 |
} |
| 1512 |
} else if (g_ascii_strcasecmp(element_name, "error") == 0) { |
| 1513 |
for (att_name=attribute_names, att_value=attribute_values; |
| 1514 |
*att_name != NULL; |
| 1515 |
att_name++, att_value++) { |
| 1516 |
if (g_str_equal(*att_name, "message")) { |
| 1517 |
thunk->message = g_strdup(*att_value); |
| 1518 |
}else if (g_str_equal(*att_name, "title")) { |
| 1519 |
thunk->error_name = g_strdup(*att_value); |
| 1520 |
// hdl->last_s3_error_code = s3_error_code_from_name(thunk->error_name); |
| 1521 |
} |
| 1522 |
} |
| 1523 |
} else if (g_ascii_strcasecmp(element_name, "uploadid") == 0) { |
| 1524 |
thunk->in_uploadId = 1; |
| 1525 |
thunk->in_others = 0; |
| 1526 |
thunk->want_text = 1; |
| 1527 |
} else { |
| 1528 |
thunk->in_others++; |
| 1529 |
} |
| 1530 |
} |
| 1531 |
|
| 1532 |
static void |
| 1533 |
failure_end_element(GMarkupParseContext *context G_GNUC_UNUSED, |
| 1534 |
const gchar *element_name, |
| 1535 |
gpointer user_data, |
| 1536 |
GError **error G_GNUC_UNUSED) |
| 1537 |
{ |
| 1538 |
struct failure_thunk *thunk = (struct failure_thunk *)user_data; |
| 1539 |
|
| 1540 |
if (g_ascii_strcasecmp(element_name, "title") == 0) { |
| 1541 |
char *p = strchr(thunk->text, ' '); |
| 1542 |
if (p) { |
| 1543 |
p++; |
| 1544 |
if (*p) { |
| 1545 |
thunk->error_name = g_strdup(p); |
| 1546 |
} |
| 1547 |
} |
| 1548 |
g_free(thunk->text); |
| 1549 |
thunk->text = NULL; |
| 1550 |
thunk->in_title = 0; |
| 1551 |
} else if (g_ascii_strcasecmp(element_name, "body") == 0) { |
| 1552 |
g_free(thunk->message); |
| 1553 |
thunk->message = thunk->text; |
| 1554 |
g_strstrip(thunk->message); |
| 1555 |
thunk->text = NULL; |
| 1556 |
thunk->in_body = 0; |
| 1557 |
} else if (g_ascii_strcasecmp(element_name, "code") == 0) { |
| 1558 |
g_free(thunk->error_name); |
| 1559 |
thunk->error_name = thunk->text; |
| 1560 |
thunk->text = NULL; |
| 1561 |
thunk->in_code = 0; |
| 1562 |
} else if (g_ascii_strcasecmp(element_name, "message") == 0) { |
| 1563 |
g_free(thunk->message); |
| 1564 |
thunk->message = thunk->text; |
| 1565 |
thunk->text = NULL; |
| 1566 |
thunk->in_message = 0; |
| 1567 |
} else if (g_ascii_strcasecmp(element_name, "details") == 0) { |
| 1568 |
g_free(thunk->details); |
| 1569 |
thunk->details = thunk->text; |
| 1570 |
thunk->text = NULL; |
| 1571 |
thunk->in_details = 0; |
| 1572 |
} else if (g_ascii_strcasecmp(element_name, "access") == 0) { |
| 1573 |
g_free(thunk->message); |
| 1574 |
thunk->message = thunk->text; |
| 1575 |
thunk->text = NULL; |
| 1576 |
thunk->in_access = 0; |
| 1577 |
} else if (g_ascii_strcasecmp(element_name, "token") == 0) { |
| 1578 |
g_free(thunk->message); |
| 1579 |
thunk->message = thunk->text; |
| 1580 |
thunk->text = NULL; |
| 1581 |
thunk->in_token = 0; |
| 1582 |
} else if (g_ascii_strcasecmp(element_name, "serviceCatalog") == 0) { |
| 1583 |
g_free(thunk->message); |
| 1584 |
thunk->message = thunk->text; |
| 1585 |
thunk->text = NULL; |
| 1586 |
thunk->in_serviceCatalog = 0; |
| 1587 |
} else if (g_ascii_strcasecmp(element_name, "service") == 0) { |
| 1588 |
g_free(thunk->message); |
| 1589 |
thunk->message = thunk->text; |
| 1590 |
thunk->text = NULL; |
| 1591 |
g_free(thunk->service_type); |
| 1592 |
thunk->service_type = NULL; |
| 1593 |
thunk->in_service = 0; |
| 1594 |
} else if (g_ascii_strcasecmp(element_name, "endpoint") == 0) { |
| 1595 |
g_free(thunk->message); |
| 1596 |
thunk->message = thunk->text; |
| 1597 |
thunk->text = NULL; |
| 1598 |
thunk->in_endpoint = 0; |
| 1599 |
} else if (g_ascii_strcasecmp(element_name, "uploadid") == 0) { |
| 1600 |
g_free(thunk->uploadId); |
| 1601 |
thunk->uploadId = thunk->text; |
| 1602 |
thunk->text = NULL; |
| 1603 |
thunk->in_uploadId = 0; |
| 1604 |
} else { |
| 1605 |
thunk->in_others--; |
| 1606 |
g_free(thunk->text); |
| 1607 |
thunk->text = NULL; |
| 1608 |
} |
| 1609 |
} |
| 1610 |
|
| 1611 |
static void |
| 1612 |
failure_text(GMarkupParseContext *context G_GNUC_UNUSED, |
| 1613 |
const gchar *text, |
| 1614 |
gsize text_len, |
| 1615 |
gpointer user_data, |
| 1616 |
GError **error G_GNUC_UNUSED) |
| 1617 |
{ |
| 1618 |
struct failure_thunk *thunk = (struct failure_thunk *)user_data; |
| 1619 |
|
| 1620 |
if (thunk->want_text && thunk->in_others == 0) { |
| 1621 |
char *new_text; |
| 1622 |
|
| 1623 |
new_text = g_strndup(text, text_len); |
| 1624 |
if (thunk->text) { |
| 1625 |
strappend(thunk->text, new_text); |
| 1626 |
g_free(new_text); |
| 1627 |
} else { |
| 1628 |
thunk->text = new_text; |
| 1629 |
} |
| 1630 |
} |
| 1631 |
} |
| 1632 |
|
| 1633 |
static void |
| 1634 |
parse_swift_v2_endpoints( |
| 1635 |
gpointer data, |
| 1636 |
gpointer user_data) |
| 1637 |
{ |
| 1638 |
amjson_t *json = data; |
| 1639 |
struct failure_thunk *pthunk = user_data; |
| 1640 |
|
| 1641 |
assert(json); |
| 1642 |
if (get_json_type(json) == JSON_HASH) { |
| 1643 |
amjson_t *endpoint_region = get_json_hash_from_key(json, "region"); |
| 1644 |
amjson_t *endpoint_publicURL = get_json_hash_from_key(json, "publicURL"); |
| 1645 |
char *region = NULL; |
| 1646 |
char *service_public_url = NULL; |
| 1647 |
if (endpoint_region && get_json_type(endpoint_region) == JSON_STRING) { |
| 1648 |
region = get_json_string(endpoint_region); |
| 1649 |
} |
| 1650 |
if (endpoint_publicURL && get_json_type(endpoint_publicURL) == JSON_STRING) { |
| 1651 |
service_public_url = get_json_string(endpoint_publicURL); |
| 1652 |
} |
| 1653 |
if (region && service_public_url) { |
| 1654 |
if (!pthunk->bucket_location || |
| 1655 |
strcmp(pthunk->bucket_location, region) == 0) { |
| 1656 |
pthunk->service_public_url = g_strdup(service_public_url); |
| 1657 |
} |
| 1658 |
} else { |
| 1659 |
pthunk->service_public_url = g_strdup(service_public_url); |
| 1660 |
} |
| 1661 |
} |
| 1662 |
} |
| 1663 |
|
| 1664 |
static void |
| 1665 |
parse_swift_v2_serviceCatalog( |
| 1666 |
gpointer data, |
| 1667 |
gpointer user_data) |
| 1668 |
{ |
| 1669 |
amjson_t *json = data; |
| 1670 |
|
| 1671 |
assert(json); |
| 1672 |
if (get_json_type(json) == JSON_HASH) { |
| 1673 |
amjson_t *catalog_type = get_json_hash_from_key(json, "type"); |
| 1674 |
if (get_json_type(catalog_type) == JSON_STRING && g_str_equal(get_json_string(catalog_type), "object-store")) { |
| 1675 |
amjson_t *catalog_endpoints = get_json_hash_from_key(json, "endpoints"); |
| 1676 |
if (get_json_type(catalog_endpoints) == JSON_ARRAY) { |
| 1677 |
foreach_json_array(catalog_endpoints, parse_swift_v2_endpoints, user_data); |
| 1678 |
} |
| 1679 |
} |
| 1680 |
} |
| 1681 |
} |
| 1682 |
|
| 1683 |
static void |
| 1684 |
parse_swift_v3_endpoints( |
| 1685 |
gpointer data, |
| 1686 |
gpointer user_data) |
| 1687 |
{ |
| 1688 |
amjson_t *json = data; |
| 1689 |
struct failure_thunk *pthunk = user_data; |
| 1690 |
|
| 1691 |
assert(json); |
| 1692 |
if (get_json_type(json) == JSON_HASH) { |
| 1693 |
amjson_t *endpoint_region = get_json_hash_from_key(json, "region_id"); |
| 1694 |
amjson_t *endpoint_interface = get_json_hash_from_key(json, "interface"); |
| 1695 |
amjson_t *endpoint_url = get_json_hash_from_key(json, "url"); |
| 1696 |
char *region = NULL; |
| 1697 |
char *service_public_url = NULL; |
| 1698 |
if (endpoint_region && get_json_type(endpoint_region) == JSON_STRING) { |
| 1699 |
region = get_json_string(endpoint_region); |
| 1700 |
} |
| 1701 |
if (endpoint_interface && get_json_type(endpoint_interface) == JSON_STRING) { |
| 1702 |
char *interface = get_json_string(endpoint_interface); |
| 1703 |
if (g_str_equal(interface, "public")) { |
| 1704 |
if (endpoint_url && get_json_type(endpoint_url) == JSON_STRING) { |
| 1705 |
service_public_url = get_json_string(endpoint_url); |
| 1706 |
} |
| 1707 |
} |
| 1708 |
} |
| 1709 |
if (region && service_public_url) { |
| 1710 |
if (!pthunk->bucket_location || |
| 1711 |
strcmp(pthunk->bucket_location, region) == 0) { |
| 1712 |
pthunk->service_public_url = g_strdup(service_public_url); |
| 1713 |
} |
| 1714 |
} else if (!pthunk->service_public_url && service_public_url) { |
| 1715 |
pthunk->service_public_url = g_strdup(service_public_url); |
| 1716 |
} |
| 1717 |
} |
| 1718 |
} |
| 1719 |
|
| 1720 |
static void |
| 1721 |
parse_swift_v3_catalog( |
| 1722 |
gpointer data, |
| 1723 |
gpointer user_data) |
| 1724 |
{ |
| 1725 |
amjson_t *json = data; |
| 1726 |
|
| 1727 |
assert(json); |
| 1728 |
if (get_json_type(json) == JSON_HASH) { |
| 1729 |
amjson_t *catalog_type = get_json_hash_from_key(json, "type"); |
| 1730 |
if (get_json_type(catalog_type) == JSON_STRING && g_str_equal(get_json_string(catalog_type), "object-store")) { |
| 1731 |
amjson_t *catalog_endpoints = get_json_hash_from_key(json, "endpoints"); |
| 1732 |
if (get_json_type(catalog_endpoints) == JSON_ARRAY) { |
| 1733 |
foreach_json_array(catalog_endpoints, parse_swift_v3_endpoints, user_data); |
| 1734 |
} |
| 1735 |
} |
| 1736 |
} |
| 1737 |
} |
| 1738 |
|
| 1739 |
static gboolean |
| 1740 |
interpret_response(S3Handle *hdl, |
| 1741 |
CURLcode curl_code, |
| 1742 |
char *curl_error_buffer, |
| 1743 |
gchar *body, |
| 1744 |
guint body_len, |
| 1745 |
const char *etag, |
| 1746 |
const char *content_md5) |
| 1747 |
{ |
| 1748 |
long response_code = 0; |
| 1749 |
gboolean ret = TRUE; |
| 1750 |
struct failure_thunk thunk; |
| 1751 |
GMarkupParseContext *ctxt = NULL; |
| 1752 |
static GMarkupParser parser = { failure_start_element, failure_end_element, failure_text, NULL, NULL }; |
| 1753 |
GError *err = NULL; |
| 1754 |
|
| 1755 |
if (!hdl) return FALSE; |
| 1756 |
|
| 1757 |
if (hdl->last_message) g_free(hdl->last_message); |
| 1758 |
hdl->last_message = NULL; |
| 1759 |
hdl->last_response_code = 0; |
| 1760 |
hdl->last_s3_error_code = 0; |
| 1761 |
hdl->last_curl_code = 0; |
| 1762 |
|
| 1763 |
curl_easy_getinfo(hdl->curl, CURLINFO_RESPONSE_CODE, &response_code); |
| 1764 |
hdl->last_response_code = response_code; |
| 1765 |
|
| 1766 |
/* bail out from a CURL error */ |
| 1767 |
if (curl_code != CURLE_OK) { |
| 1768 |
hdl->last_curl_code = curl_code; |
| 1769 |
hdl->last_message = g_strdup_printf("CURL error: %s", curl_error_buffer); |
| 1770 |
if (response_code == 0) |
| 1771 |
return FALSE; |
| 1772 |
} |
| 1773 |
|
| 1774 |
/* check ETag, if present and not CAStor */ |
| 1775 |
if (etag && content_md5 && 200 == response_code && |
| 1776 |
hdl->s3_api != S3_API_CASTOR) { |
| 1777 |
if (etag && g_ascii_strcasecmp(etag, content_md5)) |
| 1778 |
hdl->last_message = g_strdup("S3 Error: Possible data corruption (ETag returned by Amazon did not match the MD5 hash of the data sent)"); |
| 1779 |
else |
| 1780 |
ret = FALSE; |
| 1781 |
return ret; |
| 1782 |
} |
| 1783 |
|
| 1784 |
/* Now look at the body to try to get the actual Amazon error message. */ |
| 1785 |
|
| 1786 |
/* impose a reasonable limit on body size */ |
| 1787 |
if (body_len > MAX_ERROR_RESPONSE_LEN) { |
| 1788 |
hdl->last_message = g_strdup("S3 Error: Unknown (response body too large to parse)"); |
| 1789 |
return FALSE; |
| 1790 |
} else if (!body || body_len == 0) { |
| 1791 |
if (response_code < 100 || response_code >= 400) { |
| 1792 |
hdl->last_message = |
| 1793 |
g_strdup("S3 Error: Unknown (empty response body)"); |
| 1794 |
return TRUE; /* perhaps a network error; retry the request */ |
| 1795 |
} else { |
| 1796 |
/* 2xx and 3xx codes without body are good result */ |
| 1797 |
hdl->last_s3_error_code = S3_ERROR_None; |
| 1798 |
return FALSE; |
| 1799 |
} |
| 1800 |
} |
| 1801 |
|
| 1802 |
if (hdl->verbose) { |
| 1803 |
char *body_copy = g_strndup(body, body_len); |
| 1804 |
g_debug("data in %d: %s", (int)body_len, body_copy); |
| 1805 |
amfree(body_copy); |
| 1806 |
} |
| 1807 |
|
| 1808 |
thunk.in_title = FALSE; |
| 1809 |
thunk.in_body = FALSE; |
| 1810 |
thunk.in_code = FALSE; |
| 1811 |
thunk.in_message = FALSE; |
| 1812 |
thunk.in_details = FALSE; |
| 1813 |
thunk.in_access = FALSE; |
| 1814 |
thunk.in_token = FALSE; |
| 1815 |
thunk.in_serviceCatalog = FALSE; |
| 1816 |
thunk.in_service = FALSE; |
| 1817 |
thunk.in_endpoint = FALSE; |
| 1818 |
thunk.in_uploadId = FALSE; |
| 1819 |
thunk.in_others = 0; |
| 1820 |
thunk.text = NULL; |
| 1821 |
thunk.want_text = FALSE; |
| 1822 |
thunk.text_len = 0; |
| 1823 |
thunk.message = NULL; |
| 1824 |
thunk.details = NULL; |
| 1825 |
thunk.error_name = NULL; |
| 1826 |
thunk.token_id = NULL; |
| 1827 |
thunk.service_type = NULL; |
| 1828 |
thunk.service_public_url = NULL; |
| 1829 |
thunk.expires = 0; |
| 1830 |
thunk.uploadId = NULL; |
| 1831 |
thunk.bucket_location = hdl->bucket_location; |
| 1832 |
|
| 1833 |
if ((hdl->s3_api == S3_API_SWIFT_1 || |
| 1834 |
hdl->s3_api == S3_API_SWIFT_2) && |
| 1835 |
hdl->content_type && |
| 1836 |
(g_str_equal(hdl->content_type, "text/html") || |
| 1837 |
g_str_equal(hdl->content_type, "text/plain"))) { |
| 1838 |
|
| 1839 |
char *body_copy = g_strndup(body, body_len); |
| 1840 |
char *b = body_copy; |
| 1841 |
char *p = strchr(b, '\n'); |
| 1842 |
char *p1; |
| 1843 |
if (p) { /* first line: error code */ |
| 1844 |
*p = '\0'; |
| 1845 |
p++; |
| 1846 |
p1 = strchr(b, ' '); |
| 1847 |
if (p1) { |
| 1848 |
p1++; |
| 1849 |
if (*p1) { |
| 1850 |
thunk.error_name = g_strdup(p1); |
| 1851 |
} |
| 1852 |
} |
| 1853 |
b = p; |
| 1854 |
} |
| 1855 |
p = strchr(b, '\n'); |
| 1856 |
if (p) { /* second line: error message */ |
| 1857 |
*p = '\0'; |
| 1858 |
p++; |
| 1859 |
thunk.message = g_strdup(p); |
| 1860 |
g_strstrip(thunk.message); |
| 1861 |
b = p; |
| 1862 |
} |
| 1863 |
goto parsing_done; |
| 1864 |
} else if ((hdl->s3_api == S3_API_SWIFT_3) && |
| 1865 |
hdl->content_type && |
| 1866 |
(g_str_equal(hdl->content_type, "text/html") || |
| 1867 |
g_str_equal(hdl->content_type, "text/plain"))) { |
| 1868 |
|
| 1869 |
char *body_copy = g_strndup(body, body_len); |
| 1870 |
regmatch_t pmatch[2]; |
| 1871 |
if (!s3_regexec_wrap(&html_error_name_regex, body_copy, 2, pmatch, 0)) { |
| 1872 |
thunk.error_name = find_regex_substring(body_copy, pmatch[1]); |
| 1873 |
am_strrmspace(thunk.error_name); |
| 1874 |
} |
| 1875 |
if (!s3_regexec_wrap(&html_message_regex, body_copy, 2, pmatch, 0)) { |
| 1876 |
thunk.message = find_regex_substring(body_copy, pmatch[1]); |
| 1877 |
} |
| 1878 |
goto parsing_done; |
| 1879 |
} else if ((hdl->s3_api == S3_API_SWIFT_1 || |
| 1880 |
hdl->s3_api == S3_API_SWIFT_2 || |
| 1881 |
hdl->s3_api == S3_API_SWIFT_3) && |
| 1882 |
hdl->content_type && |
| 1883 |
g_str_equal(hdl->content_type, "application/json")) { |
| 1884 |
char *body_copy = g_strndup(body, body_len); |
| 1885 |
char *code = NULL; |
| 1886 |
char *details = NULL; |
| 1887 |
regmatch_t pmatch[2]; |
| 1888 |
|
| 1889 |
if (hdl->getting_swift_2_token) { |
| 1890 |
amjson_t *json = parse_json(body_copy); |
| 1891 |
if (get_json_type(json) == JSON_HASH) { |
| 1892 |
amjson_t *json_access = get_json_hash_from_key(json, "access"); |
| 1893 |
if (json_access && get_json_type(json_access) == JSON_HASH) { |
| 1894 |
amjson_t *json_token = get_json_hash_from_key(json_access, "token"); |
| 1895 |
amjson_t *json_catalog = get_json_hash_from_key(json_access, "serviceCatalog"); |
| 1896 |
if (json_token && get_json_type(json_token) == JSON_HASH) { |
| 1897 |
amjson_t *json_token_id = get_json_hash_from_key(json_token, "id"); |
| 1898 |
amjson_t *json_token_expires = get_json_hash_from_key(json_token, "expires"); |
| 1899 |
if (json_token_id && get_json_type(json_token_id) == JSON_STRING) { |
| 1900 |
thunk.token_id = g_strdup(get_json_string(json_token_id)); |
| 1901 |
} |
| 1902 |
if (json_token_expires && get_json_type(json_token_expires) == JSON_STRING) { |
| 1903 |
thunk.expires = rfc3339_date(get_json_string(json_token_expires)); |
| 1904 |
} |
| 1905 |
} |
| 1906 |
if (json_catalog && get_json_type(json_catalog) == JSON_ARRAY) { |
| 1907 |
foreach_json_array(json_catalog, parse_swift_v2_serviceCatalog, &thunk); |
| 1908 |
} |
| 1909 |
if (thunk.token_id && thunk.expires && thunk.service_public_url) { |
| 1910 |
g_free(body_copy); |
| 1911 |
goto parse_done; |
| 1912 |
} |
| 1913 |
} |
| 1914 |
} |
| 1915 |
} else if (hdl->getting_swift_3_token) { |
| 1916 |
amjson_t *json = parse_json(body_copy); |
| 1917 |
// g_debug("json: %s", json_to_string(json)); |
| 1918 |
if (get_json_type(json) == JSON_HASH) { |
| 1919 |
amjson_t *json_token = get_json_hash_from_key(json, "token"); |
| 1920 |
if (json_token && get_json_type(json_token) == JSON_HASH) { |
| 1921 |
amjson_t *json_catalog = get_json_hash_from_key(json_token, "catalog"); |
| 1922 |
amjson_t *json_expires_at = get_json_hash_from_key(json_token, "expires_at"); |
| 1923 |
if (json_catalog && get_json_type(json_catalog) == JSON_ARRAY) { |
| 1924 |
foreach_json_array(json_catalog, parse_swift_v3_catalog, &thunk); |
| 1925 |
} |
| 1926 |
if (json_expires_at && get_json_type(json_expires_at) == JSON_STRING) { |
| 1927 |
thunk.expires = rfc3339_date(get_json_string(json_expires_at)); |
| 1928 |
} |
| 1929 |
if (thunk.expires && thunk.service_public_url) { |
| 1930 |
g_free(body_copy); |
| 1931 |
goto parse_done; |
| 1932 |
} |
| 1933 |
} |
| 1934 |
} |
| 1935 |
} |
| 1936 |
|
| 1937 |
if (!s3_regexec_wrap(&code_regex, body_copy, 2, pmatch, 0)) { |
| 1938 |
code = find_regex_substring(body_copy, pmatch[1]); |
| 1939 |
} |
| 1940 |
if (!s3_regexec_wrap(&details_regex, body_copy, 2, pmatch, 0)) { |
| 1941 |
details = find_regex_substring(body_copy, pmatch[1]); |
| 1942 |
} |
| 1943 |
if (!details) { |
| 1944 |
if (!s3_regexec_wrap(&json_message_regex, body_copy, 2, pmatch, 0)) { |
| 1945 |
details = find_regex_substring(body_copy, pmatch[1]); |
| 1946 |
} |
| 1947 |
} |
| 1948 |
if (code && details) { |
| 1949 |
hdl->last_message = g_strdup_printf("%s (%s)", details, code); |
| 1950 |
} else if (code) { |
| 1951 |
hdl->last_message = g_strdup_printf("(%s)", code); |
| 1952 |
} else if (details) { |
| 1953 |
hdl->last_message = g_strdup_printf("%s", details); |
| 1954 |
} else { |
| 1955 |
hdl->last_message = NULL; |
| 1956 |
} |
| 1957 |
g_free(code); |
| 1958 |
g_free(details); |
| 1959 |
g_free(body_copy); |
| 1960 |
return FALSE; |
| 1961 |
} else if (hdl->s3_api == S3_API_CASTOR) { |
| 1962 |
/* The error mesage is the body */ |
| 1963 |
hdl->last_message = g_strndup(body, body_len); |
| 1964 |
return FALSE; |
| 1965 |
} else if ((hdl->content_type && |
| 1966 |
!g_str_equal(hdl->content_type, "application/xml")) || |
| 1967 |
(!hdl->content_type && hdl->transfer_encoding && |
| 1968 |
!g_str_equal(hdl->transfer_encoding, "chunked") && |
| 1969 |
!g_strstr_len(body, body_len, "xml version"))) { |
| 1970 |
return FALSE; |
| 1971 |
} |
| 1972 |
|
| 1973 |
/* run the parser over it */ |
| 1974 |
ctxt = g_markup_parse_context_new(&parser, 0, (gpointer)&thunk, NULL); |
| 1975 |
if (!g_markup_parse_context_parse(ctxt, body, body_len, &err)) { |
| 1976 |
if (hdl->last_message) g_free(hdl->last_message); |
| 1977 |
hdl->last_message = g_strdup(err->message); |
| 1978 |
goto cleanup; |
| 1979 |
} |
| 1980 |
|
| 1981 |
if (!g_markup_parse_context_end_parse(ctxt, &err)) { |
| 1982 |
if (hdl->last_message) g_free(hdl->last_message); |
| 1983 |
hdl->last_message = g_strdup(err->message); |
| 1984 |
goto cleanup; |
| 1985 |
} |
| 1986 |
|
| 1987 |
g_markup_parse_context_free(ctxt); |
| 1988 |
ctxt = NULL; |
| 1989 |
|
| 1990 |
parse_done: |
| 1991 |
if (hdl->s3_api == S3_API_SWIFT_2) { |
| 1992 |
if (!hdl->x_auth_token && thunk.token_id) { |
| 1993 |
hdl->x_auth_token = thunk.token_id; |
| 1994 |
g_debug("x_auth_token: %s", hdl->x_auth_token); |
| 1995 |
thunk.token_id = NULL; |
| 1996 |
} |
| 1997 |
if (!hdl->x_storage_url && thunk.service_public_url) { |
| 1998 |
hdl->x_storage_url = thunk.service_public_url; |
| 1999 |
g_debug("x_storage_url: %s", hdl->x_storage_url); |
| 2000 |
thunk.service_public_url = NULL; |
| 2001 |
} else if (!thunk.message && !thunk.error_name) { |
| 2002 |
if (!hdl->x_storage_url && !thunk.service_public_url) { |
| 2003 |
if (thunk.bucket_location) { |
| 2004 |
thunk.message = g_strdup_printf("Did not find the publicURL for the '%s' region", thunk.bucket_location); |
| 2005 |
} else { |
| 2006 |
thunk.message = g_strdup_printf("Did not find the publicURL"); |
| 2007 |
} |
| 2008 |
thunk.error_name =g_strdup("RegionNotFound"); |
| 2009 |
} |
| 2010 |
} |
| 2011 |
} else if (hdl->s3_api == S3_API_SWIFT_3) { |
| 2012 |
if (!hdl->x_storage_url && thunk.service_public_url) { |
| 2013 |
hdl->x_storage_url = thunk.service_public_url; |
| 2014 |
g_debug("x_storage_url: %s", hdl->x_storage_url); |
| 2015 |
thunk.service_public_url = NULL; |
| 2016 |
} else if (!thunk.message && !thunk.error_name) { |
| 2017 |
if (!hdl->x_storage_url && !thunk.service_public_url) { |
| 2018 |
if (thunk.bucket_location) { |
| 2019 |
thunk.message = g_strdup_printf("Did not find the public url for the '%s' region", thunk.bucket_location); |
| 2020 |
} else { |
| 2021 |
thunk.message = g_strdup_printf("Did not find the public url"); |
| 2022 |
} |
| 2023 |
thunk.error_name =g_strdup("RegionNotFound"); |
| 2024 |
} |
| 2025 |
} |
| 2026 |
} |
| 2027 |
|
| 2028 |
if (thunk.expires > 0) { |
| 2029 |
hdl->expires = thunk.expires; |
| 2030 |
} |
| 2031 |
parsing_done: |
| 2032 |
if (thunk.error_name) { |
| 2033 |
hdl->last_s3_error_code = s3_error_code_from_name(thunk.error_name); |
| 2034 |
g_free(thunk.error_name); |
| 2035 |
thunk.error_name = NULL; |
| 2036 |
} |
| 2037 |
|
| 2038 |
if (thunk.message) { |
| 2039 |
g_free(hdl->last_message); |
| 2040 |
if (thunk.details) { |
| 2041 |
hdl->last_message = g_strdup_printf("%s: %s", thunk.message, |
| 2042 |
thunk.details); |
| 2043 |
amfree(thunk.message); |
| 2044 |
amfree(thunk.details); |
| 2045 |
} else { |
| 2046 |
hdl->last_message = thunk.message; |
| 2047 |
thunk.message = NULL; /* steal the reference to the string */ |
| 2048 |
} |
| 2049 |
} else if (thunk.details) { |
| 2050 |
hdl->last_message = thunk.details; |
| 2051 |
thunk.details = NULL; /* steal the reference to the string */ |
| 2052 |
} |
| 2053 |
|
| 2054 |
if (thunk.uploadId) { |
| 2055 |
g_free(hdl->uploadId); |
| 2056 |
hdl->uploadId = thunk.uploadId; |
| 2057 |
thunk.uploadId = NULL; /* steal the reference to the string */ |
| 2058 |
} |
| 2059 |
|
| 2060 |
cleanup: |
| 2061 |
g_free(thunk.text); |
| 2062 |
g_free(thunk.message); |
| 2063 |
g_free(thunk.details); |
| 2064 |
g_free(thunk.error_name); |
| 2065 |
g_free(thunk.token_id); |
| 2066 |
g_free(thunk.service_type); |
| 2067 |
g_free(thunk.service_public_url); |
| 2068 |
g_free(thunk.uploadId); |
| 2069 |
return FALSE; |
| 2070 |
} |
| 2071 |
|
| 2072 |
/* a CURLOPT_READFUNCTION to read data from a buffer. */ |
| 2073 |
size_t |
| 2074 |
s3_buffer_read_func(void *ptr, size_t size, size_t nmemb, void * stream) |
| 2075 |
{ |
| 2076 |
CurlBuffer *data = stream; |
| 2077 |
guint bytes_desired = (guint) size * nmemb; |
| 2078 |
guint avail; |
| 2079 |
|
| 2080 |
if (data->mutex) { /* chunked transfer */ |
| 2081 |
g_mutex_lock(data->mutex); |
| 2082 |
|
| 2083 |
while (1) { |
| 2084 |
if (data->buffer_len == data->buffer_pos) { |
| 2085 |
avail = 0; |
| 2086 |
} else if (data->buffer_len > data->buffer_pos) { |
| 2087 |
avail = data->buffer_len - data->buffer_pos; |
| 2088 |
} else { |
| 2089 |
avail = data->max_buffer_size - data->buffer_pos + data->buffer_len; |
| 2090 |
} |
| 2091 |
if (avail > bytes_desired || data->end_of_buffer) |
| 2092 |
break; |
| 2093 |
|
| 2094 |
g_cond_wait(data->cond, data->mutex); |
| 2095 |
} |
| 2096 |
|
| 2097 |
if (bytes_desired > avail) |
| 2098 |
bytes_desired = avail; |
| 2099 |
if (bytes_desired > 0) { |
| 2100 |
if (data->buffer_len > data->buffer_pos) { |
| 2101 |
memcpy((char *)ptr, data->buffer + data->buffer_pos, bytes_desired); |
| 2102 |
data->buffer_pos += bytes_desired; |
| 2103 |
} else { |
| 2104 |
guint count_end = data->max_buffer_size - data->buffer_pos; |
| 2105 |
guint count_begin; |
| 2106 |
|
| 2107 |
if (count_end > bytes_desired) |
| 2108 |
count_end = bytes_desired; |
| 2109 |
memcpy((char *)ptr, data->buffer + data->buffer_pos, count_end); |
| 2110 |
data->buffer_pos += count_end; |
| 2111 |
count_begin = bytes_desired - count_end; |
| 2112 |
if (count_begin > 0) { |
| 2113 |
memcpy((char *)ptr + count_end, data->buffer, count_begin); |
| 2114 |
data->buffer_pos = count_begin; |
| 2115 |
} |
| 2116 |
} |
| 2117 |
} |
| 2118 |
/* signal to add more data to the buffer */ |
| 2119 |
g_cond_broadcast(data->cond); |
| 2120 |
g_mutex_unlock(data->mutex); |
| 2121 |
|
| 2122 |
} else { |
| 2123 |
/* check the number of bytes remaining, just to be safe */ |
| 2124 |
if (bytes_desired > data->buffer_len - data->buffer_pos) |
| 2125 |
bytes_desired = data->buffer_len - data->buffer_pos; |
| 2126 |
|
| 2127 |
memcpy((char *)ptr, data->buffer + data->buffer_pos, bytes_desired); |
| 2128 |
data->buffer_pos += bytes_desired; |
| 2129 |
} |
| 2130 |
|
| 2131 |
return bytes_desired; |
| 2132 |
} |
| 2133 |
|
| 2134 |
size_t |
| 2135 |
s3_buffer_size_func(void *stream) |
| 2136 |
{ |
| 2137 |
CurlBuffer *data = stream; |
| 2138 |
return data->buffer_len; |
| 2139 |
} |
| 2140 |
|
| 2141 |
GByteArray* |
| 2142 |
s3_buffer_md5_func(void *stream) |
| 2143 |
{ |
| 2144 |
CurlBuffer *data = stream; |
| 2145 |
GByteArray req_body_gba = {(guint8 *)data->buffer, data->buffer_len}; |
| 2146 |
|
| 2147 |
return s3_compute_md5_hash(&req_body_gba); |
| 2148 |
} |
| 2149 |
|
| 2150 |
void |
| 2151 |
s3_buffer_reset_func(void *stream) |
| 2152 |
{ |
| 2153 |
CurlBuffer *data = stream; |
| 2154 |
data->buffer_pos = 0; |
| 2155 |
} |
| 2156 |
|
| 2157 |
/* a CURLOPT_WRITEFUNCTION to write data to a buffer. */ |
| 2158 |
size_t |
| 2159 |
s3_buffer_write_func(void *ptr, size_t size, size_t nmemb, void *stream) |
| 2160 |
{ |
| 2161 |
CurlBuffer * data = stream; |
| 2162 |
guint new_bytes = (guint) size * nmemb; |
| 2163 |
guint bytes_needed = data->buffer_pos + new_bytes; |
| 2164 |
|
| 2165 |
|
| 2166 |
if (data->mutex) { /* chunked transfer */ |
| 2167 |
g_mutex_lock(data->mutex); |
| 2168 |
|
| 2169 |
/* error out if 2 block can't fit in the buffer */ |
| 2170 |
if (new_bytes*2 > data->max_buffer_size) { |
| 2171 |
g_mutex_unlock(data->mutex); |
| 2172 |
return 0; |
| 2173 |
} |
| 2174 |
|
| 2175 |
// Check for enough space in the buffer |
| 2176 |
while (1) { |
| 2177 |
guint avail; |
| 2178 |
if (data->buffer_len == data->buffer_pos) { |
| 2179 |
avail = data->max_buffer_size; |
| 2180 |
} else if (data->buffer_len > data->buffer_pos) { |
| 2181 |
avail = data->buffer_pos + data->max_buffer_size - data->buffer_len; |
| 2182 |
} else { |
| 2183 |
avail = data->buffer_pos - data->buffer_len; |
| 2184 |
} |
| 2185 |
if (avail > new_bytes) { |
| 2186 |
break; |
| 2187 |
} |
| 2188 |
g_cond_wait(data->cond, data->mutex); |
| 2189 |
} |
| 2190 |
|
| 2191 |
// Copy the new data to the buffer |
| 2192 |
if (data->buffer_len > data->buffer_pos) { |
| 2193 |
guint count_end = data->max_buffer_size - data->buffer_len; |
| 2194 |
guint count_begin; |
| 2195 |
if (count_end > new_bytes) |
| 2196 |
count_end = new_bytes; |
| 2197 |
memcpy(data->buffer + data->buffer_len, ptr, count_end); |
| 2198 |
data->buffer_len += count_end; |
| 2199 |
count_begin = new_bytes - count_end; |
| 2200 |
if (count_begin > 0) { |
| 2201 |
memcpy(data->buffer, ptr + count_end, count_begin); |
| 2202 |
data->buffer_len = count_begin; |
| 2203 |
} |
| 2204 |
} else { |
| 2205 |
memcpy(data->buffer + data->buffer_len, ptr, new_bytes); |
| 2206 |
data->buffer_len += new_bytes; |
| 2207 |
} |
| 2208 |
|
| 2209 |
g_cond_broadcast(data->cond); |
| 2210 |
g_mutex_unlock(data->mutex); |
| 2211 |
return new_bytes; |
| 2212 |
} |
| 2213 |
|
| 2214 |
/* error out if the new size is greater than the maximum allowed */ |
| 2215 |
if (data->max_buffer_size && bytes_needed > data->max_buffer_size) |
| 2216 |
return 0; |
| 2217 |
|
| 2218 |
|
| 2219 |
/* reallocate if necessary. We use exponential sizing to make this |
| 2220 |
* happen less often. */ |
| 2221 |
if (bytes_needed > data->buffer_len) { |
| 2222 |
guint new_size = MAX(bytes_needed, data->buffer_len * 2); |
| 2223 |
if (data->max_buffer_size) { |
| 2224 |
new_size = MIN(new_size, data->max_buffer_size); |
| 2225 |
} |
| 2226 |
data->buffer = g_realloc(data->buffer, new_size); |
| 2227 |
data->buffer_len = new_size; |
| 2228 |
} |
| 2229 |
if (!data->buffer) |
| 2230 |
return 0; /* returning zero signals an error to libcurl */ |
| 2231 |
|
| 2232 |
/* actually copy the data to the buffer */ |
| 2233 |
memcpy(data->buffer + data->buffer_pos, ptr, new_bytes); |
| 2234 |
data->buffer_pos += new_bytes; |
| 2235 |
|
| 2236 |
/* signal success to curl */ |
| 2237 |
return new_bytes; |
| 2238 |
} |
| 2239 |
|
| 2240 |
/* a CURLOPT_READFUNCTION that writes nothing. */ |
| 2241 |
size_t |
| 2242 |
s3_empty_read_func(G_GNUC_UNUSED void *ptr, G_GNUC_UNUSED size_t size, G_GNUC_UNUSED size_t nmemb, G_GNUC_UNUSED void * stream) |
| 2243 |
{ |
| 2244 |
return 0; |
| 2245 |
} |
| 2246 |
|
| 2247 |
size_t |
| 2248 |
s3_empty_size_func(G_GNUC_UNUSED void *stream) |
| 2249 |
{ |
| 2250 |
return 0; |
| 2251 |
} |
| 2252 |
|
| 2253 |
GByteArray* |
| 2254 |
s3_empty_md5_func(G_GNUC_UNUSED void *stream) |
| 2255 |
{ |
| 2256 |
static const GByteArray empty = {(guint8 *) "", 0}; |
| 2257 |
|
| 2258 |
return s3_compute_md5_hash(&empty); |
| 2259 |
} |
| 2260 |
|
| 2261 |
/* a CURLOPT_WRITEFUNCTION to write data that just counts data. |
| 2262 |
* s3_write_data should be NULL or a pointer to an gint64. |
| 2263 |
*/ |
| 2264 |
size_t |
| 2265 |
s3_counter_write_func(G_GNUC_UNUSED void *ptr, size_t size, size_t nmemb, void *stream) |
| 2266 |
{ |
| 2267 |
gint64 *count = (gint64*) stream, inc = nmemb*size; |
| 2268 |
|
| 2269 |
if (count) *count += inc; |
| 2270 |
return inc; |
| 2271 |
} |
| 2272 |
|
| 2273 |
void |
| 2274 |
s3_counter_reset_func(void *stream) |
| 2275 |
{ |
| 2276 |
gint64 *count = (gint64*) stream; |
| 2277 |
|
| 2278 |
if (count) *count = 0; |
| 2279 |
} |
| 2280 |
|
| 2281 |
#ifdef _WIN32 |
| 2282 |
/* a CURLOPT_READFUNCTION to read data from a file. */ |
| 2283 |
size_t |
| 2284 |
s3_file_read_func(void *ptr, size_t size, size_t nmemb, void * stream) |
| 2285 |
{ |
| 2286 |
HANDLE *hFile = (HANDLE *) stream; |
| 2287 |
DWORD bytes_read; |
| 2288 |
|
| 2289 |
ReadFile(hFile, ptr, (DWORD) size*nmemb, &bytes_read, NULL); |
| 2290 |
return bytes_read; |
| 2291 |
} |
| 2292 |
|
| 2293 |
size_t |
| 2294 |
s3_file_size_func(void *stream) |
| 2295 |
{ |
| 2296 |
HANDLE *hFile = (HANDLE *) stream; |
| 2297 |
DWORD size = GetFileSize(hFile, NULL); |
| 2298 |
|
| 2299 |
if (INVALID_FILE_SIZE == size) { |
| 2300 |
return -1; |
| 2301 |
} else { |
| 2302 |
return size; |
| 2303 |
} |
| 2304 |
} |
| 2305 |
|
| 2306 |
GByteArray* |
| 2307 |
s3_file_md5_func(void *stream) |
| 2308 |
{ |
| 2309 |
#define S3_MD5_BUF_SIZE (10*1024) |
| 2310 |
HANDLE *hFile = (HANDLE *) stream; |
| 2311 |
guint8 buf[S3_MD5_BUF_SIZE]; |
| 2312 |
DWORD bytes_read; |
| 2313 |
MD5_CTX md5_ctx; |
| 2314 |
GByteArray *ret = NULL; |
| 2315 |
|
| 2316 |
g_assert(INVALID_SET_FILE_POINTER != SetFilePointer(hFile, 0, NULL, FILE_BEGIN)); |
| 2317 |
|
| 2318 |
ret = g_byte_array_sized_new(S3_MD5_HASH_BYTE_LEN); |
| 2319 |
g_byte_array_set_size(ret, S3_MD5_HASH_BYTE_LEN); |
| 2320 |
MD5_Init(&md5_ctx); |
| 2321 |
|
| 2322 |
while (ReadFile(hFile, buf, S3_MD5_BUF_SIZE, &bytes_read, NULL)) { |
| 2323 |
MD5_Update(&md5_ctx, buf, bytes_read); |
| 2324 |
} |
| 2325 |
MD5_Final(ret->data, &md5_ctx); |
| 2326 |
|
| 2327 |
g_assert(INVALID_SET_FILE_POINTER != SetFilePointer(hFile, 0, NULL, FILE_BEGIN)); |
| 2328 |
return ret; |
| 2329 |
#undef S3_MD5_BUF_SIZE |
| 2330 |
} |
| 2331 |
|
| 2332 |
GByteArray* |
| 2333 |
s3_file_reset_func(void *stream) |
| 2334 |
{ |
| 2335 |
g_assert(INVALID_SET_FILE_POINTER != SetFilePointer(hFile, 0, NULL, FILE_BEGIN)); |
| 2336 |
} |
| 2337 |
|
| 2338 |
/* a CURLOPT_WRITEFUNCTION to write data to a file. */ |
| 2339 |
size_t |
| 2340 |
s3_file_write_func(void *ptr, size_t size, size_t nmemb, void *stream) |
| 2341 |
{ |
| 2342 |
HANDLE *hFile = (HANDLE *) stream; |
| 2343 |
DWORD bytes_written; |
| 2344 |
|
| 2345 |
WriteFile(hFile, ptr, (DWORD) size*nmemb, &bytes_written, NULL); |
| 2346 |
return bytes_written; |
| 2347 |
} |
| 2348 |
#endif |
| 2349 |
|
| 2350 |
static int |
| 2351 |
curl_debug_message(CURL *curl G_GNUC_UNUSED, |
| 2352 |
curl_infotype type, |
| 2353 |
char *s, |
| 2354 |
size_t len, |
| 2355 |
void *unused G_GNUC_UNUSED) |
| 2356 |
{ |
| 2357 |
char *lineprefix; |
| 2358 |
char *message; |
| 2359 |
char **lines, **line; |
| 2360 |
size_t i; |
| 2361 |
|
| 2362 |
switch (type) { |
| 2363 |
case CURLINFO_TEXT: |
| 2364 |
lineprefix=""; |
| 2365 |
break; |
| 2366 |
|
| 2367 |
case CURLINFO_HEADER_IN: |
| 2368 |
lineprefix="Hdr In: "; |
| 2369 |
break; |
| 2370 |
|
| 2371 |
case CURLINFO_HEADER_OUT: |
| 2372 |
lineprefix="Hdr Out: "; |
| 2373 |
break; |
| 2374 |
|
| 2375 |
case CURLINFO_DATA_IN: |
| 2376 |
if (len > 3000) return 0; |
| 2377 |
for (i=0;i<len;i++) { |
| 2378 |
if (!g_ascii_isprint(s[i])) { |
| 2379 |
return 0; |
| 2380 |
} |
| 2381 |
} |
| 2382 |
lineprefix="Data In: "; |
| 2383 |
break; |
| 2384 |
|
| 2385 |
case CURLINFO_DATA_OUT: |
| 2386 |
if (len > 3000) return 0; |
| 2387 |
for (i=0;i<len;i++) { |
| 2388 |
if (!g_ascii_isprint(s[i])) { |
| 2389 |
return 0; |
| 2390 |
} |
| 2391 |
} |
| 2392 |
lineprefix="Data Out: "; |
| 2393 |
break; |
| 2394 |
|
| 2395 |
default: |
| 2396 |
/* ignore data in/out -- nobody wants to see that in the |
| 2397 |
* debug logs! */ |
| 2398 |
return 0; |
| 2399 |
} |
| 2400 |
|
| 2401 |
/* split the input into lines */ |
| 2402 |
message = g_strndup(s, (gsize) len); |
| 2403 |
lines = g_strsplit(message, "\n", -1); |
| 2404 |
g_free(message); |
| 2405 |
|
| 2406 |
for (line = lines; *line; line++) { |
| 2407 |
if (**line == '\0') continue; /* skip blank lines */ |
| 2408 |
g_debug("%s%s", lineprefix, *line); |
| 2409 |
} |
| 2410 |
g_strfreev(lines); |
| 2411 |
|
| 2412 |
return 0; |
| 2413 |
} |
| 2414 |
static s3_result_t |
| 2415 |
perform_request(S3Handle *hdl, |
| 2416 |
const char *verb, |
| 2417 |
const char *bucket, |
| 2418 |
const char *key, |
| 2419 |
const char *subresource, |
| 2420 |
const char **query, |
| 2421 |
const char *content_type, |
| 2422 |
const char *project_id, |
| 2423 |
struct curl_slist *user_headers, |
| 2424 |
s3_read_func read_func, |
| 2425 |
s3_reset_func read_reset_func, |
| 2426 |
s3_size_func size_func, |
| 2427 |
s3_md5_func md5_func, |
| 2428 |
gpointer read_data, |
| 2429 |
s3_write_func write_func, |
| 2430 |
s3_reset_func write_reset_func, |
| 2431 |
gpointer write_data, |
| 2432 |
s3_progress_func progress_func, |
| 2433 |
gpointer progress_data, |
| 2434 |
const result_handling_t *result_handling, |
| 2435 |
gboolean chunked) |
| 2436 |
{ |
| 2437 |
char *url = NULL; |
| 2438 |
s3_result_t result = S3_RESULT_FAIL; /* assume the worst.. */ |
| 2439 |
CURLcode curl_code = CURLE_OK; |
| 2440 |
char curl_error_buffer[CURL_ERROR_SIZE] = ""; |
| 2441 |
struct curl_slist *headers = NULL; |
| 2442 |
/* Set S3Internal Data */ |
| 2443 |
S3InternalData int_writedata = {{NULL, 0, 0, MAX_ERROR_RESPONSE_LEN, TRUE, NULL, NULL}, NULL, NULL, NULL, FALSE, FALSE, NULL, hdl}; |
| 2444 |
gboolean should_retry; |
| 2445 |
gint retries = 0; |
| 2446 |
gint retry_after_close = 0; |
| 2447 |
gulong backoff = EXPONENTIAL_BACKOFF_START_USEC; |
| 2448 |
/* corresponds to PUT, HEAD, GET, and POST */ |
| 2449 |
int curlopt_upload = 0, curlopt_nobody = 0, curlopt_httpget = 0, curlopt_post = 0; |
| 2450 |
/* do we want to examine the headers */ |
| 2451 |
const char *curlopt_customrequest = NULL; |
| 2452 |
/* for MD5 calculation */ |
| 2453 |
GByteArray *md5_hash = NULL; |
| 2454 |
gchar *md5_hash_hex = NULL, *md5_hash_b64 = NULL; |
| 2455 |
size_t request_body_size = 0; |
| 2456 |
char *data_SHA256Hash = NULL; |
| 2457 |
|
| 2458 |
g_assert(hdl != NULL && hdl->curl != NULL); |
| 2459 |
|
| 2460 |
if (hdl->s3_api == S3_API_OAUTH2 && !hdl->getting_oauth2_access_token && |
| 2461 |
(!hdl->access_token || hdl->expires < time(NULL))) { |
| 2462 |
result = oauth2_get_access_token(hdl); |
| 2463 |
if (!result) { |
| 2464 |
g_debug("oauth2_get_access_token returned %d", result); |
| 2465 |
return result; |
| 2466 |
} |
| 2467 |
} else if (hdl->s3_api == S3_API_SWIFT_2 && !hdl->getting_swift_2_token && |
| 2468 |
(!hdl->x_auth_token || hdl->expires < time(NULL))) { |
| 2469 |
result = get_openstack_swift_api_v2_setting(hdl); |
| 2470 |
if (!result) { |
| 2471 |
g_debug("get_openstack_swift_api_v2_setting returned %d", result); |
| 2472 |
return result; |
| 2473 |
} |
| 2474 |
} else if (hdl->s3_api == S3_API_SWIFT_3 && !hdl->getting_swift_3_token && |
| 2475 |
(!hdl->x_auth_token || hdl->expires < time(NULL))) { |
| 2476 |
result = get_openstack_swift_api_v3_setting(hdl); |
| 2477 |
if (!result) { |
| 2478 |
g_debug("get_openstack_swift_api_v3_setting returned %d", result); |
| 2479 |
return result; |
| 2480 |
} |
| 2481 |
} |
| 2482 |
|
| 2483 |
s3_reset(hdl); |
| 2484 |
|
| 2485 |
url = build_url(hdl, bucket, key, subresource, query); |
| 2486 |
if (!url) goto cleanup; |
| 2487 |
|
| 2488 |
/* libcurl may behave strangely if these are not set correctly */ |
| 2489 |
if (g_str_has_prefix(verb, "PUT")) { |
| 2490 |
curlopt_upload = 1; |
| 2491 |
} else if (g_str_has_prefix(verb, "GET")) { |
| 2492 |
curlopt_httpget = 1; |
| 2493 |
} else if (g_str_has_prefix(verb, "POST")) { |
| 2494 |
curlopt_post = 1; |
| 2495 |
} else if (g_str_has_prefix(verb, "HEAD")) { |
| 2496 |
curlopt_nobody = 1; |
| 2497 |
} else { |
| 2498 |
curlopt_customrequest = verb; |
| 2499 |
} |
| 2500 |
|
| 2501 |
if (size_func) { |
| 2502 |
request_body_size = size_func(read_data); |
| 2503 |
} |
| 2504 |
|
| 2505 |
if (hdl->s3_api == S3_API_AWS4) { |
| 2506 |
if (read_data) { |
| 2507 |
data_SHA256Hash = s3_compute_sha256_hash_ba(read_data); |
| 2508 |
} else { |
| 2509 |
data_SHA256Hash = s3_compute_sha256_hash((unsigned char *)"", 0); |
| 2510 |
} |
| 2511 |
if (subresource && strcmp(subresource, "lifecycle") == 0 && md5_func) { |
| 2512 |
md5_hash = md5_func(read_data); |
| 2513 |
if (md5_hash) { |
| 2514 |
md5_hash_b64 = s3_base64_encode(md5_hash); |
| 2515 |
md5_hash_hex = s3_hex_encode(md5_hash); |
| 2516 |
g_byte_array_free(md5_hash, TRUE); |
| 2517 |
} |
| 2518 |
} |
| 2519 |
} else if (md5_func) { |
| 2520 |
md5_hash = md5_func(read_data); |
| 2521 |
if (md5_hash) { |
| 2522 |
md5_hash_b64 = s3_base64_encode(md5_hash); |
| 2523 |
md5_hash_hex = s3_hex_encode(md5_hash); |
| 2524 |
g_byte_array_free(md5_hash, TRUE); |
| 2525 |
} |
| 2526 |
} |
| 2527 |
if (!read_func) { |
| 2528 |
/* Curl will use fread() otherwise */ |
| 2529 |
read_func = s3_empty_read_func; |
| 2530 |
} |
| 2531 |
|
| 2532 |
if (write_func) { |
| 2533 |
int_writedata.write_func = write_func; |
| 2534 |
int_writedata.reset_func = write_reset_func; |
| 2535 |
int_writedata.write_data = write_data; |
| 2536 |
} else { |
| 2537 |
/* Curl will use fwrite() otherwise */ |
| 2538 |
int_writedata.write_func = s3_counter_write_func; |
| 2539 |
int_writedata.reset_func = s3_counter_reset_func; |
| 2540 |
int_writedata.write_data = NULL; |
| 2541 |
} |
| 2542 |
|
| 2543 |
while (1) { |
| 2544 |
struct curl_slist *header; |
| 2545 |
|
| 2546 |
/* reset things */ |
| 2547 |
if (headers) { |
| 2548 |
curl_slist_free_all(headers); |
| 2549 |
} |
| 2550 |
curl_error_buffer[0] = '\0'; |
| 2551 |
if (read_reset_func) { |
| 2552 |
read_reset_func(read_data); |
| 2553 |
} |
| 2554 |
/* calls write_reset_func */ |
| 2555 |
s3_internal_reset_func(&int_writedata); |
| 2556 |
|
| 2557 |
/* set up the request */ |
| 2558 |
headers = authenticate_request(hdl, verb, bucket, key, subresource, query, |
| 2559 |
md5_hash_b64, data_SHA256Hash, content_type, request_body_size, project_id); |
| 2560 |
|
| 2561 |
/* add user header to headers */ |
| 2562 |
for (header = user_headers; header != NULL; header = header->next) { |
| 2563 |
headers = curl_slist_append(headers, header->data); |
| 2564 |
} |
| 2565 |
|
| 2566 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ))) { |
| 2567 |
goto curl_error; |
| 2568 |
|
| 2569 |
} |
| 2570 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_NOSIGNAL, TRUE))) |
| 2571 |
goto curl_error; |
| 2572 |
|
| 2573 |
if (hdl->ca_info) { |
| 2574 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_CAINFO, hdl->ca_info))) |
| 2575 |
goto curl_error; |
| 2576 |
} |
| 2577 |
|
| 2578 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_VERBOSE, hdl->verbose))) |
| 2579 |
goto curl_error; |
| 2580 |
if (hdl->verbose) { |
| 2581 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_DEBUGFUNCTION, |
| 2582 |
curl_debug_message))) |
| 2583 |
goto curl_error; |
| 2584 |
} |
| 2585 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_ERRORBUFFER, |
| 2586 |
curl_error_buffer))) |
| 2587 |
goto curl_error; |
| 2588 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_NOPROGRESS, 1))) |
| 2589 |
goto curl_error; |
| 2590 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_FOLLOWLOCATION, 1))) |
| 2591 |
goto curl_error; |
| 2592 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_URL, url))) |
| 2593 |
goto curl_error; |
| 2594 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_HTTPHEADER, |
| 2595 |
headers))) |
| 2596 |
goto curl_error; |
| 2597 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_WRITEFUNCTION, s3_internal_write_func))) |
| 2598 |
goto curl_error; |
| 2599 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_WRITEDATA, &int_writedata))) |
| 2600 |
goto curl_error; |
| 2601 |
/* Note: we always have to set this apparently, for consistent "end of header" detection */ |
| 2602 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_HEADERFUNCTION, s3_internal_header_func))) |
| 2603 |
goto curl_error; |
| 2604 |
/* Note: if set, CURLOPT_HEADERDATA seems to also be used for CURLOPT_WRITEDATA ? */ |
| 2605 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_HEADERDATA, &int_writedata))) |
| 2606 |
goto curl_error; |
| 2607 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_PROGRESSFUNCTION, progress_func))) |
| 2608 |
goto curl_error; |
| 2609 |
if (progress_func) { |
| 2610 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_NOPROGRESS,0))) |
| 2611 |
goto curl_error; |
| 2612 |
} |
| 2613 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_PROGRESSDATA, progress_data))) |
| 2614 |
goto curl_error; |
| 2615 |
|
| 2616 |
if (!chunked) { |
| 2617 |
/* CURLOPT_INFILESIZE_LARGE added in 7.11.0 */ |
| 2618 |
#if LIBCURL_VERSION_NUM >= 0x070b00 |
| 2619 |
if ((curl_code = curl_easy_setopt(hdl->curl, |
| 2620 |
CURLOPT_INFILESIZE_LARGE, |
| 2621 |
(curl_off_t)request_body_size))) |
| 2622 |
goto curl_error; |
| 2623 |
#else |
| 2624 |
if ((curl_code = curl_easy_setopt(hdl->curl, |
| 2625 |
CURLOPT_INFILESIZE, |
| 2626 |
(long)request_body_size))) |
| 2627 |
goto curl_error; |
| 2628 |
#endif |
| 2629 |
|
| 2630 |
/* CURLOPT_POSTFIELDSIZE_LARGE added in 7.11.1 */ |
| 2631 |
#if LIBCURL_VERSION_NUM >= 0x070b01 |
| 2632 |
if ((curl_code = curl_easy_setopt(hdl->curl, |
| 2633 |
CURLOPT_POSTFIELDSIZE_LARGE, |
| 2634 |
(curl_off_t)request_body_size))) |
| 2635 |
goto curl_error; |
| 2636 |
#else |
| 2637 |
if ((curl_code = curl_easy_setopt(hdl->curl, |
| 2638 |
CURLOPT_POSTFIELDSIZE, |
| 2639 |
(long)request_body_size))) |
| 2640 |
goto curl_error; |
| 2641 |
#endif |
| 2642 |
} |
| 2643 |
|
| 2644 |
/* CURLOPT_MAX_{RECV,SEND}_SPEED_LARGE added in 7.15.5 */ |
| 2645 |
#if LIBCURL_VERSION_NUM >= 0x070f05 |
| 2646 |
if (s3_curl_throttling_compat()) { |
| 2647 |
if (hdl->max_send_speed) |
| 2648 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t)hdl->max_send_speed))) |
| 2649 |
goto curl_error; |
| 2650 |
|
| 2651 |
if (hdl->max_recv_speed) |
| 2652 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t)hdl->max_recv_speed))) |
| 2653 |
goto curl_error; |
| 2654 |
} |
| 2655 |
#endif |
| 2656 |
|
| 2657 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_HTTPGET, curlopt_httpget))) |
| 2658 |
goto curl_error; |
| 2659 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_UPLOAD, curlopt_upload))) |
| 2660 |
goto curl_error; |
| 2661 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_POST, curlopt_post))) |
| 2662 |
goto curl_error; |
| 2663 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_NOBODY, curlopt_nobody))) |
| 2664 |
goto curl_error; |
| 2665 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_CUSTOMREQUEST, |
| 2666 |
curlopt_customrequest))) |
| 2667 |
goto curl_error; |
| 2668 |
|
| 2669 |
|
| 2670 |
if (curlopt_upload || curlopt_post) { |
| 2671 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_READFUNCTION, read_func))) |
| 2672 |
goto curl_error; |
| 2673 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_READDATA, read_data))) |
| 2674 |
goto curl_error; |
| 2675 |
} else { |
| 2676 |
/* Clear request_body options. */ |
| 2677 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_READFUNCTION, |
| 2678 |
NULL))) |
| 2679 |
goto curl_error; |
| 2680 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_READDATA, |
| 2681 |
NULL))) |
| 2682 |
goto curl_error; |
| 2683 |
} |
| 2684 |
if (hdl->proxy) { |
| 2685 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_PROXY, |
| 2686 |
hdl->proxy))) |
| 2687 |
goto curl_error; |
| 2688 |
} |
| 2689 |
|
| 2690 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_FRESH_CONNECT, |
| 2691 |
(long)(hdl->reuse_connection && retry_after_close == 0 ? 0 : 1)))) { |
| 2692 |
goto curl_error; |
| 2693 |
} |
| 2694 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_FORBID_REUSE, |
| 2695 |
(long)(hdl->reuse_connection? 0 : 1)))) { |
| 2696 |
goto curl_error; |
| 2697 |
} |
| 2698 |
if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_TIMEOUT, |
| 2699 |
(long)hdl->timeout))) { |
| 2700 |
goto curl_error; |
| 2701 |
} |
| 2702 |
|
| 2703 |
/* Perform the request */ |
| 2704 |
curl_code = curl_easy_perform(hdl->curl); |
| 2705 |
|
| 2706 |
|
| 2707 |
/* interpret the response into hdl->last* */ |
| 2708 |
curl_error: /* (label for short-circuiting the curl_easy_perform call) */ |
| 2709 |
should_retry = interpret_response(hdl, curl_code, curl_error_buffer, |
| 2710 |
int_writedata.resp_buf.buffer, int_writedata.resp_buf.buffer_pos, int_writedata.etag, md5_hash_hex); |
| 2711 |
|
| 2712 |
if (hdl->last_response_code == 503) { |
| 2713 |
s3_new_curl(hdl); |
| 2714 |
} |
| 2715 |
|
| 2716 |
if (hdl->s3_api == S3_API_OAUTH2 && |
| 2717 |
hdl->last_response_code == 401 && |
| 2718 |
hdl->last_s3_error_code == S3_ERROR_AuthenticationRequired) { |
| 2719 |
should_retry = oauth2_get_access_token(hdl); |
| 2720 |
} |
| 2721 |
/* and, unless we know we need to retry, see what we're to do now */ |
| 2722 |
if (!should_retry) { |
| 2723 |
result = lookup_result(result_handling, hdl->last_response_code, |
| 2724 |
hdl->last_s3_error_code, hdl->last_curl_code); |
| 2725 |
|
| 2726 |
/* break out of the while(1) unless we're retrying */ |
| 2727 |
if (result != S3_RESULT_RETRY) |
| 2728 |
break; |
| 2729 |
} |
| 2730 |
|
| 2731 |
if (retries >= EXPONENTIAL_BACKOFF_MAX_RETRIES && |
| 2732 |
retry_after_close < 3 && |
| 2733 |
hdl->last_s3_error_code == S3_ERROR_RequestTimeout) { |
| 2734 |
retries = -1; |
| 2735 |
retry_after_close++; |
| 2736 |
g_debug("Retry on a new connection"); |
| 2737 |
} |
| 2738 |
if (retries >= EXPONENTIAL_BACKOFF_MAX_RETRIES) { |
| 2739 |
/* we're out of retries, so annotate hdl->last_message appropriately and bail |
| 2740 |
* out. */ |
| 2741 |
char *m = g_strdup_printf("Too many retries; last message was '%s'", hdl->last_message); |
| 2742 |
if (hdl->last_message) g_free(hdl->last_message); |
| 2743 |
hdl->last_message = m; |
| 2744 |
result = S3_RESULT_FAIL; |
| 2745 |
break; |
| 2746 |
} |
| 2747 |
|
| 2748 |
g_usleep(backoff); |
| 2749 |
retries++; |
| 2750 |
backoff *= EXPONENTIAL_BACKOFF_BASE; |
| 2751 |
} |
| 2752 |
|
| 2753 |
if (result != S3_RESULT_OK) { |
| 2754 |
g_debug(_("%s %s failed with %d/%s"), verb, url, |
| 2755 |
hdl->last_response_code, |
| 2756 |
s3_error_name_from_code(hdl->last_s3_error_code)); |
| 2757 |
} |
| 2758 |
|
| 2759 |
cleanup: |
| 2760 |
g_free(url); |
| 2761 |
if (headers) curl_slist_free_all(headers); |
| 2762 |
g_free(md5_hash_b64); |
| 2763 |
g_free(md5_hash_hex); |
| 2764 |
g_free(data_SHA256Hash); |
| 2765 |
|
| 2766 |
/* we don't deallocate the response body -- we keep it for later */ |
| 2767 |
g_free(hdl->etag); |
| 2768 |
hdl->etag = int_writedata.etag; |
| 2769 |
hdl->last_response_body = int_writedata.resp_buf.buffer; |
| 2770 |
hdl->last_response_body_size = int_writedata.resp_buf.buffer_pos; |
| 2771 |
hdl->last_num_retries = retries; |
| 2772 |
|
| 2773 |
return result; |
| 2774 |
} |
| 2775 |
|
| 2776 |
|
| 2777 |
static size_t |
| 2778 |
s3_internal_write_func(void *ptr, size_t size, size_t nmemb, void * stream) |
| 2779 |
{ |
| 2780 |
S3InternalData *data = (S3InternalData *) stream; |
| 2781 |
size_t bytes_saved; |
| 2782 |
|
| 2783 |
if (!data->headers_done) |
| 2784 |
return size*nmemb; |
| 2785 |
|
| 2786 |
/* call write on internal buffer (if not full) */ |
| 2787 |
if (data->int_write_done) { |
| 2788 |
bytes_saved = 0; |
| 2789 |
} else { |
| 2790 |
bytes_saved = s3_buffer_write_func(ptr, size, nmemb, &data->resp_buf); |
| 2791 |
if (!bytes_saved) { |
| 2792 |
data->int_write_done = TRUE; |
| 2793 |
} |
| 2794 |
} |
| 2795 |
/* call write on user buffer */ |
| 2796 |
if (data->write_func) { |
| 2797 |
return data->write_func(ptr, size, nmemb, data->write_data); |
| 2798 |
} else { |
| 2799 |
return bytes_saved; |
| 2800 |
} |
| 2801 |
} |
| 2802 |
|
| 2803 |
static void |
| 2804 |
s3_internal_reset_func(void * stream) |
| 2805 |
{ |
| 2806 |
S3InternalData *data = (S3InternalData *) stream; |
| 2807 |
|
| 2808 |
s3_buffer_reset_func(&data->resp_buf); |
| 2809 |
data->headers_done = FALSE; |
| 2810 |
data->int_write_done = FALSE; |
| 2811 |
amfree(data->etag); |
| 2812 |
if (data->reset_func) { |
| 2813 |
data->reset_func(data->write_data); |
| 2814 |
} |
| 2815 |
} |
| 2816 |
|
| 2817 |
static size_t |
| 2818 |
s3_internal_header_func(void *ptr, size_t size, size_t nmemb, void * stream) |
| 2819 |
{ |
| 2820 |
static const char *final_header = "\r\n"; |
| 2821 |
time_t remote_time_in_sec,local_time; |
| 2822 |
char *header; |
| 2823 |
regmatch_t pmatch[2]; |
| 2824 |
S3InternalData *data = (S3InternalData *) stream; |
| 2825 |
|
| 2826 |
header = g_strndup((gchar *) ptr, (gsize) size*nmemb); |
| 2827 |
|
| 2828 |
if (header[strlen(header)-1] == '\n') |
| 2829 |
header[strlen(header)-1] = '\0'; |
| 2830 |
if (header[strlen(header)-1] == '\r') |
| 2831 |
header[strlen(header)-1] = '\0'; |
| 2832 |
if (!s3_regexec_wrap(&etag_regex, header, 2, pmatch, 0)) { |
| 2833 |
g_free(data->etag); |
| 2834 |
data->etag = find_regex_substring(header, pmatch[1]); |
| 2835 |
} |
| 2836 |
if (!s3_regexec_wrap(&x_auth_token_regex, header, 2, pmatch, 0)) { |
| 2837 |
g_free(data->hdl->x_auth_token); |
| 2838 |
data->hdl->x_auth_token = find_regex_substring(header, pmatch[1]); |
| 2839 |
} |
| 2840 |
|
| 2841 |
if (!s3_regexec_wrap(&x_subject_token_regex, header, 2, pmatch, 0)) { |
| 2842 |
g_free(data->hdl->x_auth_token); |
| 2843 |
data->hdl->x_auth_token = find_regex_substring(header, pmatch[1]); |
| 2844 |
} |
| 2845 |
|
| 2846 |
if (!s3_regexec_wrap(&x_storage_url_regex, header, 2, pmatch, 0)) { |
| 2847 |
g_free(data->hdl->x_storage_url); |
| 2848 |
data->hdl->x_storage_url = find_regex_substring(header, pmatch[1]); |
| 2849 |
} |
| 2850 |
|
| 2851 |
if (!s3_regexec_wrap(&content_type_regex, header, 2, pmatch, 0)) { |
| 2852 |
g_free(data->hdl->content_type); |
| 2853 |
data->hdl->content_type = find_regex_substring(header, pmatch[1]); |
| 2854 |
} |
| 2855 |
|
| 2856 |
if (!s3_regexec_wrap(&transfer_encoding_regex, header, 2, pmatch, 0)) { |
| 2857 |
g_free(data->hdl->transfer_encoding); |
| 2858 |
data->hdl->transfer_encoding = find_regex_substring(header, pmatch[1]); |
| 2859 |
} |
| 2860 |
|
| 2861 |
if (!s3_regexec_wrap(&x_amz_expiration_regex, header, 2, pmatch, 0)) { |
| 2862 |
g_free(data->hdl->x_amz_expiration); |
| 2863 |
data->hdl->x_amz_expiration = find_regex_substring(header, pmatch[1]); |
| 2864 |
} |
| 2865 |
|
| 2866 |
if (!s3_regexec_wrap(&x_amz_restore_regex, header, 2, pmatch, 0)) { |
| 2867 |
g_free(data->hdl->x_amz_restore); |
| 2868 |
data->hdl->x_amz_restore = find_regex_substring(header, pmatch[1]); |
| 2869 |
} |
| 2870 |
|
| 2871 |
if (strlen(header) == 0) |
| 2872 |
data->headers_done = TRUE; |
| 2873 |
if (g_str_equal(final_header, header)) |
| 2874 |
data->headers_done = TRUE; |
| 2875 |
if (g_str_equal("\n", header)) |
| 2876 |
data->headers_done = TRUE; |
| 2877 |
|
| 2878 |
/* If date header is found */ |
| 2879 |
if (!s3_regexec_wrap(&date_sync_regex, header, 2, pmatch, 0)){ |
| 2880 |
char *date = find_regex_substring(header, pmatch[1]); |
| 2881 |
|
| 2882 |
/* Remote time is always in GMT: RFC 2616 */ |
| 2883 |
/* both curl_getdate and time operate in UTC, so no timezone math is necessary */ |
| 2884 |
if ( (remote_time_in_sec = curl_getdate(date, NULL)) < 0 ){ |
| 2885 |
g_debug("Error: Conversion of remote time to seconds failed."); |
| 2886 |
data->hdl->time_offset_with_s3 = 0; |
| 2887 |
}else{ |
| 2888 |
local_time = time(NULL); |
| 2889 |
/* Offset time */ |
| 2890 |
data->hdl->time_offset_with_s3 = remote_time_in_sec - local_time; |
| 2891 |
|
| 2892 |
if (data->hdl->verbose) |
| 2893 |
g_debug("Time Offset (remote - local) :%ld",(long)data->hdl->time_offset_with_s3); |
| 2894 |
} |
| 2895 |
|
| 2896 |
g_free(date); |
| 2897 |
} |
| 2898 |
|
| 2899 |
g_free(header); |
| 2900 |
return size*nmemb; |
| 2901 |
} |
| 2902 |
|
| 2903 |
static gboolean |
| 2904 |
compile_regexes(void) |
| 2905 |
{ |
| 2906 |
#ifdef HAVE_REGEX_H |
| 2907 |
|
| 2908 |
/* using POSIX regular expressions */ |
| 2909 |
struct {const char * str; int flags; regex_t *regex;} regexes[] = { |
| 2910 |
{"<Code>[[:space:]]*([^<]*)[[:space:]]*</Code>", REG_EXTENDED | REG_ICASE, &error_name_regex}, |
| 2911 |
{"^ETag:[[:space:]]*\"([^\"]+)\"[[:space:]]*$", REG_EXTENDED | REG_ICASE | REG_NEWLINE, &etag_regex}, |
| 2912 |
{"^X-Auth-Token:[[:space:]]*([^ ]+)[[:space:]]*$", REG_EXTENDED | REG_ICASE | REG_NEWLINE, &x_auth_token_regex}, |
| 2913 |
{"^X-Subject-Token:[[:space:]]*([^ ]+)[[:space:]]*$", REG_EXTENDED | REG_ICASE | REG_NEWLINE, &x_subject_token_regex}, |
| 2914 |
{"^X-Storage-Url:[[:space:]]*([^ ]+)[[:space:]]*$", REG_EXTENDED | REG_ICASE | REG_NEWLINE, &x_storage_url_regex}, |
| 2915 |
{"^Content-Type:[[:space:]]*([^ ;]+).*$", REG_EXTENDED | REG_ICASE | REG_NEWLINE, &content_type_regex}, |
| 2916 |
{"^Transfer-Encoding:[[:space:]]*([^ ;]+).*$", REG_EXTENDED | REG_ICASE | REG_NEWLINE, &transfer_encoding_regex}, |
| 2917 |
{"<Message>[[:space:]]*([^<]*)[[:space:]]*</Message>", REG_EXTENDED | REG_ICASE, &message_regex}, |
| 2918 |
{"^[a-z0-9](-*[a-z0-9]){2,62}$", REG_EXTENDED | REG_NOSUB, &subdomain_regex}, |
| 2919 |
{"(/>)|(>([^<]*)</LocationConstraint>)", REG_EXTENDED | REG_ICASE, &location_con_regex}, |
| 2920 |
{"^Date:(.*)$",REG_EXTENDED | REG_ICASE | REG_NEWLINE, &date_sync_regex}, |
| 2921 |
{"\"access_token\" : \"([^\"]*)\",", REG_EXTENDED | REG_ICASE | REG_NEWLINE, &access_token_regex}, |
| 2922 |
{"\"expires_in\" : (.*)", REG_EXTENDED | REG_ICASE | REG_NEWLINE, &expires_in_regex}, |
| 2923 |
{"\"details\": \"([^\"]*)\",", REG_EXTENDED | REG_ICASE | REG_NEWLINE, &details_regex}, |
| 2924 |
{"\"code\": (.*),", REG_EXTENDED | REG_ICASE | REG_NEWLINE, &code_regex}, |
| 2925 |
{"\"message\": \"([^\"]*)\",", REG_EXTENDED | REG_ICASE | REG_NEWLINE, &json_message_regex}, |
| 2926 |
{"<UploadId>[[:space:]]*([^<]*)[[:space:]]*</UploadId>", REG_EXTENDED | REG_ICASE, &uploadId_regex}, |
| 2927 |
{"<h1>[[:space:]]*([^<]*)[[:space:]]*</h1>", REG_EXTENDED | REG_ICASE, &html_error_name_regex}, |
| 2928 |
{"<p>[[:space:]]*([^<]*)[[:space:]]*</p>", REG_EXTENDED | REG_ICASE, &html_message_regex}, |
| 2929 |
{"^x-amz-expiration:[[:space:]]*([^ ]+)[[:space:]]*$", REG_EXTENDED | REG_ICASE | REG_NEWLINE, &x_amz_expiration_regex}, |
| 2930 |
{"^x-amz-restore:[[:space:]]*([^ ]+)[[:space:]]*$", REG_EXTENDED | REG_ICASE | REG_NEWLINE, &x_amz_restore_regex}, |
| 2931 |
{NULL, 0, NULL} |
| 2932 |
}; |
| 2933 |
char regmessage[1024]; |
| 2934 |
int i; |
| 2935 |
int reg_result; |
| 2936 |
|
| 2937 |
for (i = 0; regexes[i].str; i++) { |
| 2938 |
reg_result = regcomp(regexes[i].regex, regexes[i].str, regexes[i].flags); |
| 2939 |
if (reg_result != 0) { |
| 2940 |
regerror(reg_result, regexes[i].regex, regmessage, sizeof(regmessage)); |
| 2941 |
g_error(_("Regex error: %s"), regmessage); |
| 2942 |
return FALSE; |
| 2943 |
} |
| 2944 |
} |
| 2945 |
#else /* ! HAVE_REGEX_H */ |
| 2946 |
/* using PCRE via GLib */ |
| 2947 |
struct {const char * str; int flags; regex_t *regex;} regexes[] = { |
| 2948 |
{"<Code>\\s*([^<]*)\\s*</Code>", |
| 2949 |
G_REGEX_OPTIMIZE | G_REGEX_CASELESS, |
| 2950 |
&error_name_regex}, |
| 2951 |
{"^ETag:\\s*\"([^\"]+)\"\\s*$", |
| 2952 |
G_REGEX_OPTIMIZE | G_REGEX_CASELESS, |
| 2953 |
&etag_regex}, |
| 2954 |
{"^X-Auth-Token:\\s*([^ ]+)\\s*$", |
| 2955 |
G_REGEX_OPTIMIZE | G_REGEX_CASELESS, |
| 2956 |
&x_auth_token_regex}, |
| 2957 |
{"^X-Subject-Token:\\s*([^ ]+)\\s*$", |
| 2958 |
G_REGEX_OPTIMIZE | G_REGEX_CASELESS, |
| 2959 |
&x_subject_token_regex}, |
| 2960 |
{"^X-Storage-Url:\\s*([^ ]+)\\s*$", |
| 2961 |
G_REGEX_OPTIMIZE | G_REGEX_CASELESS, |
| 2962 |
&x_storage_url_regex}, |
| 2963 |
{"^Content-Type:\\s*([^ ]+)\\s*$", |
| 2964 |
G_REGEX_OPTIMIZE | G_REGEX_CASELESS, |
| 2965 |
&content_type_regex}, |
| 2966 |
{"^Transfer-Encoding:\\s*([^ ]+)\\s*$", |
| 2967 |
G_REGEX_OPTIMIZE | G_REGEX_CASELESS, |
| 2968 |
&transfer_encoding_regex}, |
| 2969 |
{"<Message>\\s*([^<]*)\\s*</Message>", |
| 2970 |
G_REGEX_OPTIMIZE | G_REGEX_CASELESS, |
| 2971 |
&message_regex}, |
| 2972 |
{"^[a-z0-9]((-*[a-z0-9])|(\\.[a-z0-9])){2,62}$", |
| 2973 |
G_REGEX_OPTIMIZE | G_REGEX_NO_AUTO_CAPTURE, |
| 2974 |
&subdomain_regex}, |
| 2975 |
{"(/>)|(>([^<]*)</LocationConstraint>)", |
| 2976 |
G_REGEX_CASELESS, |
| 2977 |
&location_con_regex}, |
| 2978 |
{"^Date:(.*)$", |
| 2979 |
G_REGEX_OPTIMIZE | G_REGEX_CASELESS, |
| 2980 |
&date_sync_regex}, |
| 2981 |
{"\"access_token\" : \"([^\"]*)\"", |
| 2982 |
G_REGEX_OPTIMIZE | G_REGEX_CASELESS, |
| 2983 |
&access_token_regex}, |
| 2984 |
{"\"expires_n\" : (.*)", |
| 2985 |
G_REGEX_OPTIMIZE | G_REGEX_CASELESS, |
| 2986 |
&expires_in_regex}, |
| 2987 |
{"\"details\" : \"([^\"]*)\"", |
| 2988 |
G_REGEX_OPTIMIZE | G_REGEX_CASELESS, |
| 2989 |
&details_regex}, |
| 2990 |
{"\"code\" : (.*)", |
| 2991 |
G_REGEX_OPTIMIZE | G_REGEX_CASELESS, |
| 2992 |
&code_regex}, |
| 2993 |
{"\"message\" : \"([^\"]*)\"", |
| 2994 |
G_REGEX_OPTIMIZE | G_REGEX_CASELESS, |
| 2995 |
&json_message_regex}, |
| 2996 |
{"(/>)|(>([^<]*)</UploadId>)", |
| 2997 |
G_REGEX_CASELESS, |
| 2998 |
&uploadId_regex}, |
| 2999 |
{"(/>)|(<h1>([^<]*)</h1>)", |
| 3000 |
G_REGEX_CASELESS, |
| 3001 |
&html_error_name_regex}, |
| 3002 |
{"(/>)|(<p>([^<]*)</p>)", |
| 3003 |
G_REGEX_CASELESS, |
| 3004 |
&html_message_regex}, |
| 3005 |
{NULL, 0, NULL} |
| 3006 |
}; |
| 3007 |
int i; |
| 3008 |
GError *err = NULL; |
| 3009 |
|
| 3010 |
for (i = 0; regexes[i].str; i++) { |
| 3011 |
*(regexes[i].regex) = g_regex_new(regexes[i].str, regexes[i].flags, 0, &err); |
| 3012 |
if (err) { |
| 3013 |
g_error(_("Regex error: %s"), err->message); |
| 3014 |
g_error_free(err); |
| 3015 |
return FALSE; |
| 3016 |
} |
| 3017 |
} |
| 3018 |
#endif |
| 3019 |
return TRUE; |
| 3020 |
} |
| 3021 |
|
| 3022 |
/* |
| 3023 |
* Public function implementations |
| 3024 |
*/ |
| 3025 |
|
| 3026 |
#if (GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 31)) |
| 3027 |
# pragma GCC diagnostic push |
| 3028 |
# pragma GCC diagnostic ignored "-Wmissing-field-initializers" |
| 3029 |
#endif |
| 3030 |
gboolean s3_init(void) |
| 3031 |
{ |
| 3032 |
static GStaticMutex mutex = G_STATIC_MUTEX_INIT; |
| 3033 |
static gboolean init = FALSE, ret; |
| 3034 |
|
| 3035 |
/* n.b. curl_global_init is called in common-src/glib-util.c:glib_init() */ |
| 3036 |
|
| 3037 |
g_static_mutex_lock (&mutex); |
| 3038 |
if (!init) { |
| 3039 |
ret = compile_regexes(); |
| 3040 |
init = TRUE; |
| 3041 |
} |
| 3042 |
g_static_mutex_unlock(&mutex); |
| 3043 |
return ret; |
| 3044 |
} |
| 3045 |
#if (GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 31)) |
| 3046 |
# pragma GCC diagnostic pop |
| 3047 |
#endif |
| 3048 |
|
| 3049 |
gboolean |
| 3050 |
s3_curl_location_compat(void) |
| 3051 |
{ |
| 3052 |
curl_version_info_data *info; |
| 3053 |
|
| 3054 |
info = curl_version_info(CURLVERSION_NOW); |
| 3055 |
return info->version_num > 0x070a02; |
| 3056 |
} |
| 3057 |
|
| 3058 |
gboolean |
| 3059 |
s3_bucket_location_compat(const char *bucket) |
| 3060 |
{ |
| 3061 |
return !s3_regexec_wrap(&subdomain_regex, bucket, 0, NULL, 0); |
| 3062 |
} |
| 3063 |
|
| 3064 |
static gboolean |
| 3065 |
get_openstack_swift_api_v1_setting( |
| 3066 |
S3Handle *hdl) |
| 3067 |
{ |
| 3068 |
s3_result_t result = S3_RESULT_FAIL; |
| 3069 |
static result_handling_t result_handling[] = { |
| 3070 |
{ 200, S3_ERROR_RegionNotFound, 0, S3_RESULT_FAIL }, |
| 3071 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 3072 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 3073 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 3074 |
}; |
| 3075 |
|
| 3076 |
s3_verbose(hdl, 1); |
| 3077 |
result = perform_request(hdl, "GET", NULL, NULL, NULL, NULL, NULL, NULL, |
| 3078 |
NULL, |
| 3079 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 3080 |
NULL, NULL, result_handling, FALSE); |
| 3081 |
|
| 3082 |
return result == S3_RESULT_OK; |
| 3083 |
} |
| 3084 |
|
| 3085 |
static gboolean |
| 3086 |
get_openstack_swift_api_v2_setting( |
| 3087 |
S3Handle *hdl) |
| 3088 |
{ |
| 3089 |
s3_result_t result = S3_RESULT_FAIL; |
| 3090 |
static result_handling_t result_handling[] = { |
| 3091 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 3092 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 3093 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 3094 |
}; |
| 3095 |
|
| 3096 |
CurlBuffer buf = {NULL, 0, 0, 0, TRUE, NULL, NULL}; |
| 3097 |
GString *body = g_string_new(""); |
| 3098 |
|
| 3099 |
g_string_append_printf(body, "{ \"auth\": {\n"); |
| 3100 |
if (hdl->tenant_id) { |
| 3101 |
g_string_append_printf(body, "\"tenantId\":\"%s\"", hdl->tenant_id); |
| 3102 |
} else if (hdl->tenant_name) { |
| 3103 |
g_string_append_printf(body, "\"tenantName\":\"%s\"", hdl->tenant_name); |
| 3104 |
} |
| 3105 |
if (hdl->username && hdl->password) { |
| 3106 |
g_string_append_printf(body, ",\"passwordCredentials\": { \"username\":\"%s\", \"password\":\"%s\" }", hdl->username, hdl->password); |
| 3107 |
} else { |
| 3108 |
g_string_append_printf(body, ",\"apiAccessKeyCredentialsi\":{ \"accessKey\":\"%s\", \"secretKey\":\"%s\" }", hdl->access_key, hdl->secret_key); |
| 3109 |
} |
| 3110 |
g_string_append(body, "}}"); |
| 3111 |
|
| 3112 |
buf.buffer = g_string_free(body, FALSE); |
| 3113 |
buf.buffer_len = strlen(buf.buffer); |
| 3114 |
s3_verbose(hdl, 1); |
| 3115 |
hdl->getting_swift_2_token = TRUE; |
| 3116 |
g_free(hdl->x_auth_token); |
| 3117 |
hdl->x_auth_token = NULL; |
| 3118 |
g_free(hdl->x_storage_url); |
| 3119 |
hdl->x_storage_url = NULL; |
| 3120 |
result = perform_request(hdl, "POST", NULL, NULL, NULL, NULL, |
| 3121 |
"application/json", NULL, NULL, |
| 3122 |
S3_BUFFER_READ_FUNCS, &buf, |
| 3123 |
NULL, NULL, NULL, |
| 3124 |
NULL, NULL, result_handling, FALSE); |
| 3125 |
hdl->getting_swift_2_token = FALSE; |
| 3126 |
|
| 3127 |
return result == S3_RESULT_OK; |
| 3128 |
} |
| 3129 |
|
| 3130 |
static gboolean |
| 3131 |
get_openstack_swift_api_v3_setting( |
| 3132 |
S3Handle *hdl) |
| 3133 |
{ |
| 3134 |
s3_result_t result = S3_RESULT_FAIL; |
| 3135 |
static result_handling_t result_handling[] = { |
| 3136 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 3137 |
{ 201, 0, 0, S3_RESULT_OK }, |
| 3138 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 3139 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 3140 |
}; |
| 3141 |
|
| 3142 |
CurlBuffer buf = {NULL, 0, 0, 0, TRUE, NULL, NULL}; |
| 3143 |
GString *body = g_string_new(""); |
| 3144 |
|
| 3145 |
g_string_append_printf(body, "{ \"auth\": {\n"); |
| 3146 |
g_string_append_printf(body, " \"scope\": {\n"); |
| 3147 |
g_string_append_printf(body, " \"project\": {\n"); |
| 3148 |
g_string_append_printf(body, " \"domain\": {\n"); |
| 3149 |
g_string_append_printf(body, " \"name\": \"%s\" },\n", hdl->domain_name); |
| 3150 |
g_string_append_printf(body, " \"name\": \"%s\" }},\n", hdl->project_name); |
| 3151 |
g_string_append_printf(body, " \"identity\": {\n"); |
| 3152 |
g_string_append_printf(body, " \"methods\": [ \"password\" ],\n"); |
| 3153 |
g_string_append_printf(body, " \"password\": {\n"); |
| 3154 |
g_string_append_printf(body, " \"user\": {\n"); |
| 3155 |
g_string_append_printf(body, " \"name\": \"%s\",\n", hdl->username); |
| 3156 |
g_string_append_printf(body, " \"domain\": {\n"); |
| 3157 |
g_string_append_printf(body, " \"name\": \"%s\" },\n", hdl->domain_name); |
| 3158 |
g_string_append_printf(body, " \"password\": \"%s\" }}}}}\n", hdl->password); |
| 3159 |
|
| 3160 |
buf.buffer = g_string_free(body, FALSE); |
| 3161 |
// g_debug("buffer: %s", buf.buffer); |
| 3162 |
buf.buffer_len = strlen(buf.buffer); |
| 3163 |
s3_verbose(hdl, 1); |
| 3164 |
hdl->getting_swift_3_token = TRUE; |
| 3165 |
g_free(hdl->x_auth_token); |
| 3166 |
hdl->x_auth_token = NULL; |
| 3167 |
g_free(hdl->x_storage_url); |
| 3168 |
hdl->x_storage_url = NULL; |
| 3169 |
result = perform_request(hdl, "POST", NULL, NULL, NULL, NULL, |
| 3170 |
"application/json", NULL, NULL, |
| 3171 |
S3_BUFFER_READ_FUNCS, &buf, |
| 3172 |
NULL, NULL, NULL, |
| 3173 |
NULL, NULL, result_handling, FALSE); |
| 3174 |
hdl->getting_swift_3_token = FALSE; |
| 3175 |
|
| 3176 |
return result == S3_RESULT_OK; |
| 3177 |
} |
| 3178 |
|
| 3179 |
S3Handle * |
| 3180 |
s3_open(const char *access_key, |
| 3181 |
const char *secret_key, |
| 3182 |
const char *session_token, |
| 3183 |
const char *swift_account_id, |
| 3184 |
const char *swift_access_key, |
| 3185 |
const char *host, |
| 3186 |
const char *service_path, |
| 3187 |
const gboolean use_subdomain, |
| 3188 |
const char *user_token, |
| 3189 |
const char *bucket_location, |
| 3190 |
const char *storage_class, |
| 3191 |
const char *ca_info, |
| 3192 |
const char *server_side_encryption, |
| 3193 |
const char *proxy, |
| 3194 |
const S3_api s3_api, |
| 3195 |
const char *username, |
| 3196 |
const char *password, |
| 3197 |
const char *tenant_id, |
| 3198 |
const char *tenant_name, |
| 3199 |
const char *project_name, |
| 3200 |
const char *domain_name, |
| 3201 |
const char *client_id, |
| 3202 |
const char *client_secret, |
| 3203 |
const char *refresh_token, |
| 3204 |
const gboolean reuse_connection, |
| 3205 |
const gboolean read_from_glacier, |
| 3206 |
const long timeout, |
| 3207 |
const char *reps, |
| 3208 |
const char *reps_bucket) |
| 3209 |
{ |
| 3210 |
S3Handle *hdl; |
| 3211 |
char *hwp; |
| 3212 |
|
| 3213 |
hdl = g_new0(S3Handle, 1); |
| 3214 |
if (!hdl) goto error; |
| 3215 |
|
| 3216 |
hdl->verbose = TRUE; |
| 3217 |
hdl->use_ssl = s3_curl_supports_ssl(); |
| 3218 |
hdl->reuse_connection = reuse_connection; |
| 3219 |
hdl->read_from_glacier = read_from_glacier; |
| 3220 |
hdl->timeout = timeout; |
| 3221 |
|
| 3222 |
if (s3_api == S3_API_S3) { |
| 3223 |
g_assert(access_key); |
| 3224 |
hdl->access_key = g_strdup(access_key); |
| 3225 |
g_assert(secret_key); |
| 3226 |
hdl->secret_key = g_strdup(secret_key); |
| 3227 |
/* NULL is okay */ |
| 3228 |
hdl->session_token = g_strdup(session_token); |
| 3229 |
} else if (s3_api == S3_API_AWS4) { |
| 3230 |
g_assert(access_key); |
| 3231 |
hdl->access_key = g_strdup(access_key); |
| 3232 |
g_assert(secret_key); |
| 3233 |
hdl->secret_key = g_strdup(secret_key); |
| 3234 |
/* NULL is okay */ |
| 3235 |
hdl->session_token = g_strdup(session_token); |
| 3236 |
} else if (s3_api == S3_API_SWIFT_1) { |
| 3237 |
g_assert(swift_account_id); |
| 3238 |
hdl->swift_account_id = g_strdup(swift_account_id); |
| 3239 |
g_assert(swift_access_key); |
| 3240 |
hdl->swift_access_key = g_strdup(swift_access_key); |
| 3241 |
} else if (s3_api == S3_API_SWIFT_2) { |
| 3242 |
g_assert((username && password) || (access_key && secret_key)); |
| 3243 |
hdl->username = g_strdup(username); |
| 3244 |
hdl->password = g_strdup(password); |
| 3245 |
hdl->access_key = g_strdup(access_key); |
| 3246 |
hdl->secret_key = g_strdup(secret_key); |
| 3247 |
g_assert(tenant_id || tenant_name); |
| 3248 |
hdl->tenant_id = g_strdup(tenant_id); |
| 3249 |
hdl->tenant_name = g_strdup(tenant_name); |
| 3250 |
} else if (s3_api == S3_API_SWIFT_3) { |
| 3251 |
g_assert((username && password) || (access_key && secret_key)); |
| 3252 |
hdl->username = g_strdup(username); |
| 3253 |
hdl->password = g_strdup(password); |
| 3254 |
hdl->access_key = g_strdup(access_key); |
| 3255 |
hdl->secret_key = g_strdup(secret_key); |
| 3256 |
hdl->tenant_id = g_strdup(tenant_id); |
| 3257 |
hdl->tenant_name = g_strdup(tenant_name); |
| 3258 |
if (project_name) { |
| 3259 |
hdl->project_name = g_strdup(project_name); |
| 3260 |
} else { |
| 3261 |
hdl->project_name = g_strdup(username); |
| 3262 |
} |
| 3263 |
if (domain_name) { |
| 3264 |
hdl->domain_name = g_strdup(domain_name); |
| 3265 |
} else { |
| 3266 |
hdl->domain_name = g_strdup("Default"); |
| 3267 |
} |
| 3268 |
} else if (s3_api == S3_API_OAUTH2) { |
| 3269 |
hdl->client_id = g_strdup(client_id); |
| 3270 |
hdl->client_secret = g_strdup(client_secret); |
| 3271 |
hdl->refresh_token = g_strdup(refresh_token); |
| 3272 |
} else if (s3_api == S3_API_CASTOR) { |
| 3273 |
hdl->username = g_strdup(username); |
| 3274 |
hdl->password = g_strdup(password); |
| 3275 |
hdl->tenant_name = g_strdup(tenant_name); |
| 3276 |
hdl->reps = g_strdup(reps); |
| 3277 |
hdl->reps_bucket = g_strdup(reps_bucket); |
| 3278 |
} |
| 3279 |
|
| 3280 |
/* NULL is okay */ |
| 3281 |
hdl->user_token = g_strdup(user_token); |
| 3282 |
|
| 3283 |
/* NULL is okay */ |
| 3284 |
hdl->bucket_location = g_strdup(bucket_location); |
| 3285 |
|
| 3286 |
/* NULL is ok */ |
| 3287 |
hdl->storage_class = g_strdup(storage_class); |
| 3288 |
|
| 3289 |
/* NULL is ok */ |
| 3290 |
hdl->server_side_encryption = g_strdup(server_side_encryption); |
| 3291 |
|
| 3292 |
/* NULL is ok */ |
| 3293 |
hdl->proxy = g_strdup(proxy); |
| 3294 |
|
| 3295 |
/* NULL is okay */ |
| 3296 |
hdl->ca_info = g_strdup(ca_info); |
| 3297 |
|
| 3298 |
if (!is_non_empty_string(host)) |
| 3299 |
host = "s3.amazonaws.com"; |
| 3300 |
hdl->host = g_ascii_strdown(host, -1); |
| 3301 |
hwp = strchr(hdl->host, ':'); |
| 3302 |
if (hwp) { |
| 3303 |
*hwp = '\0'; |
| 3304 |
hdl->host_without_port = g_strdup(hdl->host); |
| 3305 |
*hwp = ':'; |
| 3306 |
} else { |
| 3307 |
hdl->host_without_port = g_strdup(hdl->host); |
| 3308 |
} |
| 3309 |
|
| 3310 |
hdl->use_subdomain = use_subdomain || |
| 3311 |
(g_str_equal(hdl->host, "s3.amazonaws.com") && |
| 3312 |
is_non_empty_string(hdl->bucket_location)); |
| 3313 |
hdl->s3_api = s3_api; |
| 3314 |
if (service_path) { |
| 3315 |
if (strlen(service_path) == 0 || |
| 3316 |
(strlen(service_path) == 1 && service_path[0] == '/')) { |
| 3317 |
hdl->service_path = NULL; |
| 3318 |
} else if (service_path[0] != '/') { |
| 3319 |
hdl->service_path = g_strdup_printf("/%s", service_path); |
| 3320 |
} else { |
| 3321 |
hdl->service_path = g_strdup(service_path); |
| 3322 |
} |
| 3323 |
if (hdl->service_path) { |
| 3324 |
/* remove trailling / */ |
| 3325 |
size_t len = strlen(hdl->service_path) - 1; |
| 3326 |
if (hdl->service_path[len] == '/') |
| 3327 |
hdl->service_path[len] = '\0'; |
| 3328 |
} |
| 3329 |
} else { |
| 3330 |
hdl->service_path = NULL; |
| 3331 |
} |
| 3332 |
|
| 3333 |
s3_new_curl(hdl); |
| 3334 |
if (!hdl->curl) goto error; |
| 3335 |
return hdl; |
| 3336 |
|
| 3337 |
error: |
| 3338 |
s3_free(hdl); |
| 3339 |
return NULL; |
| 3340 |
} |
| 3341 |
|
| 3342 |
static void |
| 3343 |
s3_new_curl( |
| 3344 |
S3Handle *hdl) |
| 3345 |
{ |
| 3346 |
if (hdl->curl) { |
| 3347 |
curl_easy_cleanup(hdl->curl); |
| 3348 |
} |
| 3349 |
|
| 3350 |
hdl->curl = curl_easy_init(); |
| 3351 |
if (!hdl->curl) return; |
| 3352 |
|
| 3353 |
/* Set HTTP handling options for CAStor */ |
| 3354 |
if (hdl->s3_api == S3_API_CASTOR) { |
| 3355 |
#if LIBCURL_VERSION_NUM >= 0x071301 |
| 3356 |
curl_version_info_data *info; |
| 3357 |
/* check the runtime version too */ |
| 3358 |
info = curl_version_info(CURLVERSION_NOW); |
| 3359 |
if (info->version_num >= 0x071301) { |
| 3360 |
curl_easy_setopt(hdl->curl, CURLOPT_FOLLOWLOCATION, 1); |
| 3361 |
curl_easy_setopt(hdl->curl, CURLOPT_UNRESTRICTED_AUTH, 1); |
| 3362 |
curl_easy_setopt(hdl->curl, CURLOPT_MAXREDIRS, 5); |
| 3363 |
curl_easy_setopt(hdl->curl, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL); |
| 3364 |
curl_easy_setopt(hdl->curl, CURLOPT_HTTP_VERSION, |
| 3365 |
CURL_HTTP_VERSION_1_1); |
| 3366 |
if (hdl->username) |
| 3367 |
curl_easy_setopt(hdl->curl, CURLOPT_USERNAME, hdl->username); |
| 3368 |
if (hdl->password) |
| 3369 |
curl_easy_setopt(hdl->curl, CURLOPT_PASSWORD, hdl->password); |
| 3370 |
curl_easy_setopt(hdl->curl, CURLOPT_HTTPAUTH, |
| 3371 |
(CURLAUTH_BASIC | CURLAUTH_DIGEST)); |
| 3372 |
} |
| 3373 |
#endif |
| 3374 |
} |
| 3375 |
} |
| 3376 |
|
| 3377 |
gboolean |
| 3378 |
s3_open2( |
| 3379 |
S3Handle *hdl) |
| 3380 |
{ |
| 3381 |
gboolean ret = TRUE; |
| 3382 |
|
| 3383 |
/* get the X-Storage-Url and X-Auth-Token */ |
| 3384 |
if (hdl->s3_api == S3_API_SWIFT_1) { |
| 3385 |
ret = get_openstack_swift_api_v1_setting(hdl); |
| 3386 |
} else if (hdl->s3_api == S3_API_SWIFT_2) { |
| 3387 |
ret = get_openstack_swift_api_v2_setting(hdl); |
| 3388 |
} else if (hdl->s3_api == S3_API_SWIFT_3) { |
| 3389 |
ret = get_openstack_swift_api_v3_setting(hdl); |
| 3390 |
} |
| 3391 |
|
| 3392 |
return ret; |
| 3393 |
} |
| 3394 |
|
| 3395 |
void |
| 3396 |
s3_free(S3Handle *hdl) |
| 3397 |
{ |
| 3398 |
s3_reset(hdl); |
| 3399 |
|
| 3400 |
if (hdl) { |
| 3401 |
g_free(hdl->access_key); |
| 3402 |
g_free(hdl->secret_key); |
| 3403 |
g_free(hdl->session_token); |
| 3404 |
g_free(hdl->swift_account_id); |
| 3405 |
g_free(hdl->swift_access_key); |
| 3406 |
g_free(hdl->content_type); |
| 3407 |
g_free(hdl->ca_info); |
| 3408 |
g_free(hdl->proxy); |
| 3409 |
g_free(hdl->username); |
| 3410 |
g_free(hdl->password); |
| 3411 |
g_free(hdl->tenant_id); |
| 3412 |
g_free(hdl->tenant_name); |
| 3413 |
g_free(hdl->project_name); |
| 3414 |
g_free(hdl->domain_name); |
| 3415 |
g_free(hdl->client_id); |
| 3416 |
g_free(hdl->client_secret); |
| 3417 |
g_free(hdl->refresh_token); |
| 3418 |
g_free(hdl->access_token); |
| 3419 |
g_free(hdl->etag); |
| 3420 |
g_free(hdl->transfer_encoding); |
| 3421 |
if (hdl->user_token) g_free(hdl->user_token); |
| 3422 |
if (hdl->bucket_location) g_free(hdl->bucket_location); |
| 3423 |
if (hdl->storage_class) g_free(hdl->storage_class); |
| 3424 |
if (hdl->server_side_encryption) g_free(hdl->server_side_encryption); |
| 3425 |
if (hdl->host) g_free(hdl->host); |
| 3426 |
if (hdl->service_path) g_free(hdl->service_path); |
| 3427 |
if (hdl->curl) curl_easy_cleanup(hdl->curl); |
| 3428 |
|
| 3429 |
g_free(hdl); |
| 3430 |
} |
| 3431 |
} |
| 3432 |
|
| 3433 |
void |
| 3434 |
s3_reset(S3Handle *hdl) |
| 3435 |
{ |
| 3436 |
if (hdl) { |
| 3437 |
/* We don't call curl_easy_reset here, because doing that in curl |
| 3438 |
* < 7.16 blanks the default CA certificate path, and there's no way |
| 3439 |
* to get it back. */ |
| 3440 |
if (hdl->last_message) { |
| 3441 |
g_free(hdl->last_message); |
| 3442 |
hdl->last_message = NULL; |
| 3443 |
} |
| 3444 |
|
| 3445 |
hdl->last_response_code = 0; |
| 3446 |
hdl->last_curl_code = 0; |
| 3447 |
hdl->last_s3_error_code = 0; |
| 3448 |
hdl->last_num_retries = 0; |
| 3449 |
|
| 3450 |
if (hdl->last_response_body) { |
| 3451 |
g_free(hdl->last_response_body); |
| 3452 |
hdl->last_response_body = NULL; |
| 3453 |
} |
| 3454 |
if (hdl->content_type) { |
| 3455 |
g_free(hdl->content_type); |
| 3456 |
hdl->content_type = NULL; |
| 3457 |
} |
| 3458 |
|
| 3459 |
hdl->last_response_body_size = 0; |
| 3460 |
} |
| 3461 |
} |
| 3462 |
|
| 3463 |
void |
| 3464 |
s3_error(S3Handle *hdl, |
| 3465 |
const char **message, |
| 3466 |
guint *response_code, |
| 3467 |
s3_error_code_t *s3_error_code, |
| 3468 |
const char **s3_error_name, |
| 3469 |
CURLcode *curl_code, |
| 3470 |
guint *num_retries) |
| 3471 |
{ |
| 3472 |
if (hdl) { |
| 3473 |
if (message) *message = hdl->last_message; |
| 3474 |
if (response_code) *response_code = hdl->last_response_code; |
| 3475 |
if (s3_error_code) *s3_error_code = hdl->last_s3_error_code; |
| 3476 |
if (s3_error_name) *s3_error_name = s3_error_name_from_code(hdl->last_s3_error_code); |
| 3477 |
if (curl_code) *curl_code = hdl->last_curl_code; |
| 3478 |
if (num_retries) *num_retries = hdl->last_num_retries; |
| 3479 |
} else { |
| 3480 |
/* no hdl? return something coherent, anyway */ |
| 3481 |
if (message) *message = "NULL S3Handle"; |
| 3482 |
if (response_code) *response_code = 0; |
| 3483 |
if (s3_error_code) *s3_error_code = 0; |
| 3484 |
if (s3_error_name) *s3_error_name = NULL; |
| 3485 |
if (curl_code) *curl_code = 0; |
| 3486 |
if (num_retries) *num_retries = 0; |
| 3487 |
} |
| 3488 |
} |
| 3489 |
|
| 3490 |
void |
| 3491 |
s3_verbose(S3Handle *hdl, gboolean verbose) |
| 3492 |
{ |
| 3493 |
hdl->verbose = verbose; |
| 3494 |
} |
| 3495 |
|
| 3496 |
gboolean |
| 3497 |
s3_set_max_send_speed(S3Handle *hdl, guint64 max_send_speed) |
| 3498 |
{ |
| 3499 |
if (!s3_curl_throttling_compat()) |
| 3500 |
return FALSE; |
| 3501 |
|
| 3502 |
hdl->max_send_speed = max_send_speed; |
| 3503 |
|
| 3504 |
return TRUE; |
| 3505 |
} |
| 3506 |
|
| 3507 |
gboolean |
| 3508 |
s3_set_max_recv_speed(S3Handle *hdl, guint64 max_recv_speed) |
| 3509 |
{ |
| 3510 |
if (!s3_curl_throttling_compat()) |
| 3511 |
return FALSE; |
| 3512 |
|
| 3513 |
hdl->max_recv_speed = max_recv_speed; |
| 3514 |
|
| 3515 |
return TRUE; |
| 3516 |
} |
| 3517 |
|
| 3518 |
gboolean |
| 3519 |
s3_use_ssl(S3Handle *hdl, gboolean use_ssl) |
| 3520 |
{ |
| 3521 |
gboolean ret = TRUE; |
| 3522 |
if (use_ssl & !s3_curl_supports_ssl()) { |
| 3523 |
ret = FALSE; |
| 3524 |
} else { |
| 3525 |
hdl->use_ssl = use_ssl; |
| 3526 |
} |
| 3527 |
return ret; |
| 3528 |
} |
| 3529 |
|
| 3530 |
char * |
| 3531 |
s3_strerror(S3Handle *hdl) |
| 3532 |
{ |
| 3533 |
const char *message; |
| 3534 |
guint response_code; |
| 3535 |
const char *s3_error_name; |
| 3536 |
CURLcode curl_code; |
| 3537 |
guint num_retries; |
| 3538 |
|
| 3539 |
char s3_info[256] = ""; |
| 3540 |
char response_info[16] = ""; |
| 3541 |
char curl_info[32] = ""; |
| 3542 |
char retries_info[32] = ""; |
| 3543 |
|
| 3544 |
s3_error(hdl, &message, &response_code, NULL, &s3_error_name, &curl_code, &num_retries); |
| 3545 |
|
| 3546 |
if (!message) |
| 3547 |
message = "Unknown S3 error"; |
| 3548 |
if (s3_error_name) |
| 3549 |
g_snprintf(s3_info, sizeof(s3_info), " (%s)", s3_error_name); |
| 3550 |
if (response_code) |
| 3551 |
g_snprintf(response_info, sizeof(response_info), " (HTTP %d)", response_code); |
| 3552 |
if (curl_code) |
| 3553 |
g_snprintf(curl_info, sizeof(curl_info), " (CURLcode %d)", curl_code); |
| 3554 |
if (num_retries) |
| 3555 |
g_snprintf(retries_info, sizeof(retries_info), " (after %d retries)", num_retries); |
| 3556 |
|
| 3557 |
return g_strdup_printf("%s%s%s%s%s", message, s3_info, curl_info, response_info, retries_info); |
| 3558 |
} |
| 3559 |
|
| 3560 |
/* Perform an upload. When this function returns, KEY and |
| 3561 |
* BUFFER remain the responsibility of the caller. |
| 3562 |
* |
| 3563 |
* @param self: the s3 device |
| 3564 |
* @param bucket: the bucket to which the upload should be made |
| 3565 |
* @param key: the key to which the upload should be made |
| 3566 |
* @param buffer: the data to be uploaded |
| 3567 |
* @param buffer_len: the length of the data to upload |
| 3568 |
* @returns: false if an error ocurred |
| 3569 |
*/ |
| 3570 |
gboolean |
| 3571 |
s3_upload(S3Handle *hdl, |
| 3572 |
const char *bucket, |
| 3573 |
const char *key, |
| 3574 |
const gboolean chunked, |
| 3575 |
s3_read_func read_func, |
| 3576 |
s3_reset_func reset_func, |
| 3577 |
s3_size_func size_func, |
| 3578 |
s3_md5_func md5_func, |
| 3579 |
gpointer read_data, |
| 3580 |
s3_progress_func progress_func, |
| 3581 |
gpointer progress_data) |
| 3582 |
{ |
| 3583 |
s3_result_t result = S3_RESULT_FAIL; |
| 3584 |
static result_handling_t result_handling[] = { |
| 3585 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 3586 |
{ 201, 0, 0, S3_RESULT_OK }, |
| 3587 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 3588 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 3589 |
}; |
| 3590 |
char *verb = "PUT"; |
| 3591 |
char *content_type = NULL; |
| 3592 |
struct curl_slist *headers = NULL; |
| 3593 |
|
| 3594 |
g_assert(hdl != NULL); |
| 3595 |
|
| 3596 |
if (hdl->s3_api == S3_API_CASTOR) { |
| 3597 |
verb = "POST"; |
| 3598 |
content_type = "application/x-amanda-backup-data"; |
| 3599 |
} |
| 3600 |
|
| 3601 |
if (chunked) { |
| 3602 |
headers = curl_slist_append(headers, "Transfer-Encoding: chunked"); |
| 3603 |
size_func = NULL; |
| 3604 |
md5_func = NULL; |
| 3605 |
} |
| 3606 |
|
| 3607 |
hdl->server_side_encryption_header = TRUE; |
| 3608 |
result = perform_request(hdl, verb, bucket, key, NULL, |
| 3609 |
NULL, content_type, NULL, headers, |
| 3610 |
read_func, reset_func, size_func, md5_func, read_data, |
| 3611 |
NULL, NULL, NULL, progress_func, progress_data, |
| 3612 |
result_handling, chunked); |
| 3613 |
hdl->server_side_encryption_header = FALSE; |
| 3614 |
|
| 3615 |
return result == S3_RESULT_OK; |
| 3616 |
} |
| 3617 |
|
| 3618 |
|
| 3619 |
/* Perform an upload. When this function returns, KEY and |
| 3620 |
* BUFFER remain the responsibility of the caller. |
| 3621 |
* |
| 3622 |
* @param self: the s3 device |
| 3623 |
* @param bucket: the bucket to which the upload should be made |
| 3624 |
* @param key: the key to which the upload should be made |
| 3625 |
* @param buffer: the data to be uploaded |
| 3626 |
* @param buffer_len: the length of the data to upload |
| 3627 |
* @returns: false if an error ocurred |
| 3628 |
*/ |
| 3629 |
gboolean |
| 3630 |
s3_part_upload(S3Handle *hdl, |
| 3631 |
const char *bucket, |
| 3632 |
const char *key, |
| 3633 |
const char *uploadId, |
| 3634 |
int partNumber, |
| 3635 |
char **etag, |
| 3636 |
s3_read_func read_func, |
| 3637 |
s3_reset_func reset_func, |
| 3638 |
s3_size_func size_func, |
| 3639 |
s3_md5_func md5_func, |
| 3640 |
gpointer read_data, |
| 3641 |
s3_progress_func progress_func, |
| 3642 |
gpointer progress_data) |
| 3643 |
{ |
| 3644 |
char *subresource = NULL; |
| 3645 |
char **query = NULL; |
| 3646 |
s3_result_t result = S3_RESULT_FAIL; |
| 3647 |
static result_handling_t result_handling[] = { |
| 3648 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 3649 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 3650 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 3651 |
}; |
| 3652 |
|
| 3653 |
g_assert(hdl != NULL); |
| 3654 |
|
| 3655 |
if (uploadId) { |
| 3656 |
if (hdl->s3_api == S3_API_AWS4) { |
| 3657 |
query = g_new0(char *, 3); |
| 3658 |
query[0] = g_strdup_printf("partNumber=%d", partNumber); |
| 3659 |
query[1] = g_strdup_printf("uploadId=%s", uploadId); |
| 3660 |
query[2] = NULL; |
| 3661 |
} else { |
| 3662 |
subresource = g_strdup_printf("partNumber=%d&uploadId=%s", |
| 3663 |
partNumber, uploadId); |
| 3664 |
} |
| 3665 |
} |
| 3666 |
|
| 3667 |
result = perform_request(hdl, "PUT", bucket, key, subresource, |
| 3668 |
(const char **)query, NULL, NULL, |
| 3669 |
NULL, |
| 3670 |
read_func, reset_func, size_func, md5_func, read_data, |
| 3671 |
NULL, NULL, NULL, progress_func, progress_data, |
| 3672 |
result_handling, FALSE); |
| 3673 |
|
| 3674 |
g_free(subresource); |
| 3675 |
if (query) { |
| 3676 |
g_free(query[0]); |
| 3677 |
g_free(query[1]); |
| 3678 |
g_free(query); |
| 3679 |
} |
| 3680 |
if (etag) { |
| 3681 |
*etag = hdl->etag; |
| 3682 |
hdl->etag = NULL; |
| 3683 |
} |
| 3684 |
|
| 3685 |
return result == S3_RESULT_OK; |
| 3686 |
} |
| 3687 |
|
| 3688 |
|
| 3689 |
char * |
| 3690 |
s3_initiate_multi_part_upload( |
| 3691 |
S3Handle *hdl, |
| 3692 |
const char *bucket, |
| 3693 |
const char *key) |
| 3694 |
{ |
| 3695 |
char *subresource; |
| 3696 |
s3_result_t result = S3_RESULT_FAIL; |
| 3697 |
static result_handling_t result_handling[] = { |
| 3698 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 3699 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 3700 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 3701 |
}; |
| 3702 |
|
| 3703 |
subresource = g_strdup_printf("uploads"); |
| 3704 |
hdl->server_side_encryption_header = TRUE; |
| 3705 |
result = perform_request(hdl, "POST", bucket, key, subresource, NULL, |
| 3706 |
NULL, NULL, NULL, |
| 3707 |
NULL, NULL, NULL, NULL, NULL, |
| 3708 |
NULL, NULL, NULL, NULL, NULL, |
| 3709 |
result_handling, FALSE); |
| 3710 |
hdl->server_side_encryption_header = FALSE; |
| 3711 |
|
| 3712 |
g_free(subresource); |
| 3713 |
|
| 3714 |
if (result == S3_RESULT_OK) { |
| 3715 |
return hdl->uploadId; |
| 3716 |
} else { |
| 3717 |
return NULL; |
| 3718 |
} |
| 3719 |
} |
| 3720 |
|
| 3721 |
gboolean |
| 3722 |
s3_complete_multi_part_upload( |
| 3723 |
S3Handle *hdl, |
| 3724 |
const char *bucket, |
| 3725 |
const char *key, |
| 3726 |
const char *uploadId, |
| 3727 |
s3_read_func read_func, |
| 3728 |
s3_reset_func reset_func, |
| 3729 |
s3_size_func size_func, |
| 3730 |
s3_md5_func md5_func, |
| 3731 |
gpointer read_data) |
| 3732 |
{ |
| 3733 |
char *subresource = NULL; |
| 3734 |
char **query = NULL; |
| 3735 |
s3_result_t result = S3_RESULT_FAIL; |
| 3736 |
static result_handling_t result_handling[] = { |
| 3737 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 3738 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 3739 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 3740 |
}; |
| 3741 |
|
| 3742 |
if (hdl->s3_api == S3_API_AWS4) { |
| 3743 |
query = g_new0(char *, 2); |
| 3744 |
query[0] = g_strdup_printf("uploadId=%s", uploadId); |
| 3745 |
query[1] = NULL; |
| 3746 |
} else { |
| 3747 |
subresource = g_strdup_printf("uploadId=%s", uploadId); |
| 3748 |
} |
| 3749 |
result = perform_request(hdl, "POST", bucket, key, subresource, |
| 3750 |
(const char **)query, |
| 3751 |
"application/xml", NULL, NULL, |
| 3752 |
read_func, reset_func, size_func, md5_func, read_data, |
| 3753 |
NULL, NULL, NULL, NULL, NULL, |
| 3754 |
result_handling, FALSE); |
| 3755 |
|
| 3756 |
g_free(subresource); |
| 3757 |
|
| 3758 |
return (result == S3_RESULT_OK); |
| 3759 |
} |
| 3760 |
|
| 3761 |
void |
| 3762 |
free_s3_object( |
| 3763 |
gpointer data) |
| 3764 |
{ |
| 3765 |
s3_object *object = data; |
| 3766 |
g_free(object->key); |
| 3767 |
g_free(object->uploadId); |
| 3768 |
g_free(object->prefix); |
| 3769 |
g_free(object); |
| 3770 |
} |
| 3771 |
|
| 3772 |
gboolean |
| 3773 |
s3_abort_multi_part_upload( |
| 3774 |
S3Handle *hdl, |
| 3775 |
const char *bucket, |
| 3776 |
const char *key, |
| 3777 |
const char *uploadId) |
| 3778 |
{ |
| 3779 |
char *subresource = NULL; |
| 3780 |
char **query = NULL; |
| 3781 |
s3_result_t result = S3_RESULT_FAIL; |
| 3782 |
static result_handling_t result_handling[] = { |
| 3783 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 3784 |
{ 204, 0, 0, S3_RESULT_OK }, |
| 3785 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 3786 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 3787 |
}; |
| 3788 |
|
| 3789 |
if (hdl->s3_api == S3_API_AWS4) { |
| 3790 |
query = g_new0(char *, 2); |
| 3791 |
query[0] = g_strdup_printf("uploadId=%s", uploadId); |
| 3792 |
query[1] = NULL; |
| 3793 |
} else { |
| 3794 |
subresource = g_strdup_printf("uploadId=%s", uploadId); |
| 3795 |
} |
| 3796 |
|
| 3797 |
result = perform_request(hdl, "DELETE", bucket, key, subresource, |
| 3798 |
(const char **)query, |
| 3799 |
"application/xml", NULL, NULL, |
| 3800 |
NULL, NULL, NULL, NULL, NULL, |
| 3801 |
NULL, NULL, NULL, NULL, NULL, |
| 3802 |
result_handling, FALSE); |
| 3803 |
|
| 3804 |
if (hdl->s3_api == S3_API_AWS4) { |
| 3805 |
g_free(query[0]); |
| 3806 |
g_free(query); |
| 3807 |
} else { |
| 3808 |
g_free(subresource); |
| 3809 |
} |
| 3810 |
|
| 3811 |
return (result == S3_RESULT_OK); |
| 3812 |
|
| 3813 |
|
| 3814 |
} |
| 3815 |
|
| 3816 |
/* Private structure for our "thunk", which tracks where the user is in the list |
| 3817 |
* of keys. */ |
| 3818 |
struct list_keys_thunk { |
| 3819 |
GSList *object_list; /* all pending filenames */ |
| 3820 |
s3_object *object; |
| 3821 |
|
| 3822 |
gboolean in_contents; /* look for "key" entities in here */ |
| 3823 |
gboolean in_common_prefixes; /* look for "prefix" entities in here */ |
| 3824 |
|
| 3825 |
gboolean is_truncated; |
| 3826 |
gchar *next_marker; |
| 3827 |
guint64 size; |
| 3828 |
|
| 3829 |
gboolean want_text; |
| 3830 |
|
| 3831 |
gchar *text; |
| 3832 |
gsize text_len; |
| 3833 |
}; |
| 3834 |
|
| 3835 |
/* Functions for a SAX parser to parse the XML from Amazon */ |
| 3836 |
|
| 3837 |
static void |
| 3838 |
list_start_element(GMarkupParseContext *context G_GNUC_UNUSED, |
| 3839 |
const gchar *element_name, |
| 3840 |
const gchar **attribute_names G_GNUC_UNUSED, |
| 3841 |
const gchar **attribute_values G_GNUC_UNUSED, |
| 3842 |
gpointer user_data, |
| 3843 |
GError **error G_GNUC_UNUSED) |
| 3844 |
{ |
| 3845 |
struct list_keys_thunk *thunk = (struct list_keys_thunk *)user_data; |
| 3846 |
|
| 3847 |
thunk->want_text = 0; |
| 3848 |
if (g_ascii_strcasecmp(element_name, "contents") == 0 || |
| 3849 |
g_ascii_strcasecmp(element_name, "object") == 0 || |
| 3850 |
g_ascii_strcasecmp(element_name, "upload") == 0) { |
| 3851 |
thunk->in_contents = 1; |
| 3852 |
thunk->object = g_new0(s3_object, 1); |
| 3853 |
} else if (g_ascii_strcasecmp(element_name, "commonprefixes") == 0) { |
| 3854 |
thunk->in_common_prefixes = 1; |
| 3855 |
thunk->object = g_new0(s3_object, 1); |
| 3856 |
} else if (g_ascii_strcasecmp(element_name, "prefix") == 0 && thunk->in_common_prefixes) { |
| 3857 |
thunk->want_text = 1; |
| 3858 |
} else if ((g_ascii_strcasecmp(element_name, "key") == 0 || |
| 3859 |
g_ascii_strcasecmp(element_name, "name") == 0) && |
| 3860 |
thunk->in_contents) { |
| 3861 |
thunk->want_text = 1; |
| 3862 |
} else if ((g_ascii_strcasecmp(element_name, "size") == 0 || |
| 3863 |
g_ascii_strcasecmp(element_name, "bytes") == 0) && |
| 3864 |
thunk->in_contents) { |
| 3865 |
thunk->want_text = 1; |
| 3866 |
} else if (g_ascii_strcasecmp(element_name, "uploadid") == 0 && thunk->in_contents) { |
| 3867 |
thunk->want_text = 1; |
| 3868 |
} else if (g_ascii_strcasecmp(element_name, "istruncated")) { |
| 3869 |
thunk->want_text = 1; |
| 3870 |
} else if (g_ascii_strcasecmp(element_name, "nextmarker")) { |
| 3871 |
thunk->want_text = 1; |
| 3872 |
} else if (g_ascii_strcasecmp(element_name, "storageclass")) { |
| 3873 |
thunk->want_text = 1; |
| 3874 |
} |
| 3875 |
} |
| 3876 |
|
| 3877 |
static void |
| 3878 |
list_end_element(GMarkupParseContext *context G_GNUC_UNUSED, |
| 3879 |
const gchar *element_name, |
| 3880 |
gpointer user_data, |
| 3881 |
GError **error G_GNUC_UNUSED) |
| 3882 |
{ |
| 3883 |
struct list_keys_thunk *thunk = (struct list_keys_thunk *)user_data; |
| 3884 |
|
| 3885 |
if (g_ascii_strcasecmp(element_name, "contents") == 0 || |
| 3886 |
g_ascii_strcasecmp(element_name, "object") == 0 || |
| 3887 |
g_ascii_strcasecmp(element_name, "upload") == 0) { |
| 3888 |
thunk->in_contents = 0; |
| 3889 |
thunk->object_list = g_slist_prepend(thunk->object_list, thunk->object); |
| 3890 |
thunk->object = NULL; |
| 3891 |
} else if (g_ascii_strcasecmp(element_name, "commonprefixes") == 0) { |
| 3892 |
thunk->object_list = g_slist_prepend(thunk->object_list, thunk->object); |
| 3893 |
thunk->object = NULL; |
| 3894 |
thunk->in_common_prefixes = 0; |
| 3895 |
} else if ((g_ascii_strcasecmp(element_name, "key") == 0 || |
| 3896 |
g_ascii_strcasecmp(element_name, "name") == 0) && |
| 3897 |
thunk->in_contents) { |
| 3898 |
thunk->object->key = thunk->text; |
| 3899 |
if (thunk->is_truncated) { |
| 3900 |
if (thunk->next_marker) g_free(thunk->next_marker); |
| 3901 |
thunk->next_marker = g_strdup(thunk->text); |
| 3902 |
} |
| 3903 |
thunk->text = NULL; |
| 3904 |
} else if ((g_ascii_strcasecmp(element_name, "size") == 0 || |
| 3905 |
g_ascii_strcasecmp(element_name, "bytes") == 0) && |
| 3906 |
thunk->in_contents) { |
| 3907 |
thunk->object->size = g_ascii_strtoull (thunk->text, NULL, 10); |
| 3908 |
thunk->size += thunk->object->size; |
| 3909 |
g_free(thunk->text); |
| 3910 |
thunk->text = NULL; |
| 3911 |
} else if (g_ascii_strcasecmp(element_name, "uploadid") == 0 && thunk->in_contents) { |
| 3912 |
thunk->object->uploadId = thunk->text; |
| 3913 |
thunk->text = NULL; |
| 3914 |
} else if (g_ascii_strcasecmp(element_name, "prefix") == 0 && thunk->in_common_prefixes) { |
| 3915 |
thunk->object->prefix = thunk->text; |
| 3916 |
thunk->text = NULL; |
| 3917 |
} else if (g_ascii_strcasecmp(element_name, "istruncated") == 0) { |
| 3918 |
if (thunk->text && g_ascii_strncasecmp(thunk->text, "false", 5) != 0) |
| 3919 |
thunk->is_truncated = TRUE; |
| 3920 |
} else if (g_ascii_strcasecmp(element_name, "nextmarker") == 0) { |
| 3921 |
if (thunk->next_marker) g_free(thunk->next_marker); |
| 3922 |
thunk->next_marker = thunk->text; |
| 3923 |
thunk->text = NULL; |
| 3924 |
} else if (g_ascii_strcasecmp(element_name, "storageclass") == 0) { |
| 3925 |
if (g_str_equal(thunk->text, "STANDARD")) { |
| 3926 |
thunk->object->storage_class = S3_SC_STANDARD; |
| 3927 |
} else if (g_str_equal(thunk->text, "STANDARD_IA")) { |
| 3928 |
thunk->object->storage_class = S3_SC_STANDARD_IA; |
| 3929 |
} else if (g_str_equal(thunk->text, "REDUCED_REDUNDANCY")) { |
| 3930 |
thunk->object->storage_class = S3_SC_REDUCED_REDUNDANCY; |
| 3931 |
} else if (g_str_equal(thunk->text, "GLACIER")) { |
| 3932 |
thunk->object->storage_class = S3_SC_GLACIER; |
| 3933 |
} |
| 3934 |
g_free(thunk->text); |
| 3935 |
thunk->text = NULL; |
| 3936 |
} |
| 3937 |
} |
| 3938 |
|
| 3939 |
static void |
| 3940 |
list_text(GMarkupParseContext *context G_GNUC_UNUSED, |
| 3941 |
const gchar *text, |
| 3942 |
gsize text_len, |
| 3943 |
gpointer user_data, |
| 3944 |
GError **error G_GNUC_UNUSED) |
| 3945 |
{ |
| 3946 |
struct list_keys_thunk *thunk = (struct list_keys_thunk *)user_data; |
| 3947 |
|
| 3948 |
if (thunk->want_text) { |
| 3949 |
if (thunk->text) g_free(thunk->text); |
| 3950 |
thunk->text = g_strndup(text, text_len); |
| 3951 |
} |
| 3952 |
} |
| 3953 |
|
| 3954 |
/* Perform a fetch from S3; several fetches may be involved in a |
| 3955 |
* single listing operation */ |
| 3956 |
static s3_result_t |
| 3957 |
list_fetch(S3Handle *hdl, |
| 3958 |
const char *bucket, |
| 3959 |
const char *subresource, |
| 3960 |
const char *prefix, |
| 3961 |
const char *delimiter, |
| 3962 |
const char *marker, |
| 3963 |
const char *max_keys, |
| 3964 |
CurlBuffer *buf) |
| 3965 |
{ |
| 3966 |
s3_result_t result = S3_RESULT_FAIL; |
| 3967 |
static result_handling_t result_handling[] = { |
| 3968 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 3969 |
{ 204, 0, 0, S3_RESULT_OK }, |
| 3970 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 3971 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 3972 |
}; |
| 3973 |
const char* pos_parts[][2] = { |
| 3974 |
{"delimiter", delimiter}, |
| 3975 |
{"marker", marker}, |
| 3976 |
{"max-keys", max_keys}, |
| 3977 |
{"prefix", prefix}, |
| 3978 |
{NULL, NULL} |
| 3979 |
}; |
| 3980 |
char *esc_value; |
| 3981 |
char **query = g_new0(char *, 6); |
| 3982 |
char **q = query; |
| 3983 |
guint i; |
| 3984 |
|
| 3985 |
/* loop over possible parts to build query string */ |
| 3986 |
for (i = 0; pos_parts[i][0]; i++) { |
| 3987 |
if (pos_parts[i][1]) { |
| 3988 |
const char *keyword; |
| 3989 |
esc_value = curl_escape(pos_parts[i][1], 0); |
| 3990 |
keyword = pos_parts[i][0]; |
| 3991 |
if ((hdl->s3_api == S3_API_SWIFT_1 || |
| 3992 |
hdl->s3_api == S3_API_SWIFT_2 || |
| 3993 |
hdl->s3_api == S3_API_SWIFT_3) && |
| 3994 |
strcmp(keyword, "max-keys") == 0) { |
| 3995 |
keyword = "limit"; |
| 3996 |
} else if ((hdl->s3_api == S3_API_CASTOR) && |
| 3997 |
strcmp(keyword, "max-keys") == 0) { |
| 3998 |
keyword = "size"; |
| 3999 |
} |
| 4000 |
*q++ = g_strdup_printf("%s=%s", keyword, esc_value); |
| 4001 |
curl_free(esc_value); |
| 4002 |
} |
| 4003 |
} |
| 4004 |
if (hdl->s3_api == S3_API_SWIFT_1 || |
| 4005 |
hdl->s3_api == S3_API_SWIFT_2 || |
| 4006 |
hdl->s3_api == S3_API_SWIFT_3 || |
| 4007 |
hdl->s3_api == S3_API_CASTOR) { |
| 4008 |
*q++ = g_strdup("format=xml"); |
| 4009 |
} |
| 4010 |
|
| 4011 |
/* and perform the request on that URI */ |
| 4012 |
result = perform_request(hdl, "GET", bucket, NULL, subresource, |
| 4013 |
(const char **)query, NULL, |
| 4014 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 4015 |
S3_BUFFER_WRITE_FUNCS, buf, NULL, NULL, |
| 4016 |
result_handling, FALSE); |
| 4017 |
|
| 4018 |
for (q = query; *q != NULL; q++) { |
| 4019 |
g_free(*q); |
| 4020 |
} |
| 4021 |
|
| 4022 |
return result; |
| 4023 |
} |
| 4024 |
|
| 4025 |
|
| 4026 |
gboolean |
| 4027 |
s3_list_keys(S3Handle *hdl, |
| 4028 |
const char *bucket, |
| 4029 |
const char *subresource, |
| 4030 |
const char *prefix, |
| 4031 |
const char *delimiter, |
| 4032 |
GSList **list, |
| 4033 |
guint64 *total_size) |
| 4034 |
{ |
| 4035 |
/* |
| 4036 |
* max len of XML variables: |
| 4037 |
* bucket: 255 bytes (p12 API Version 2006-03-01) |
| 4038 |
* key: 1024 bytes (p15 API Version 2006-03-01) |
| 4039 |
* size per key: 5GB bytes (p6 API Version 2006-03-01) |
| 4040 |
* size of size 10 bytes (i.e. 10 decimal digits) |
| 4041 |
* etag: 44 (observed+assumed) |
| 4042 |
* owner ID: 64 (observed+assumed) |
| 4043 |
* owner DisplayName: 255 (assumed) |
| 4044 |
* StorageClass: STANDARD | REDUCED_REDUNDANCY | GLACIER |
| 4045 |
*/ |
| 4046 |
static const guint MAX_RESPONSE_LEN = 1000*2000; |
| 4047 |
static const char *MAX_KEYS = "1000"; |
| 4048 |
struct list_keys_thunk thunk; |
| 4049 |
GMarkupParseContext *ctxt = NULL; |
| 4050 |
static GMarkupParser parser = { list_start_element, list_end_element, list_text, NULL, NULL }; |
| 4051 |
GError *err = NULL; |
| 4052 |
s3_result_t result = S3_RESULT_FAIL; |
| 4053 |
CurlBuffer buf = {NULL, 0, 0, MAX_RESPONSE_LEN, TRUE, NULL, NULL}; |
| 4054 |
|
| 4055 |
g_assert(list); |
| 4056 |
*list = NULL; |
| 4057 |
thunk.object_list = NULL; |
| 4058 |
thunk.object = NULL; |
| 4059 |
thunk.text = NULL; |
| 4060 |
thunk.next_marker = NULL; |
| 4061 |
thunk.size = 0; |
| 4062 |
|
| 4063 |
/* Loop until S3 has given us the entire picture */ |
| 4064 |
do { |
| 4065 |
s3_buffer_reset_func(&buf); |
| 4066 |
/* get some data from S3 */ |
| 4067 |
result = list_fetch(hdl, bucket, subresource, prefix, delimiter, thunk.next_marker, MAX_KEYS, &buf); |
| 4068 |
if (result != S3_RESULT_OK) goto cleanup; |
| 4069 |
if (buf.buffer_pos == 0) goto cleanup; /* no body */ |
| 4070 |
|
| 4071 |
/* run the parser over it */ |
| 4072 |
thunk.in_contents = FALSE; |
| 4073 |
thunk.in_common_prefixes = FALSE; |
| 4074 |
thunk.is_truncated = FALSE; |
| 4075 |
if (thunk.next_marker) g_free(thunk.next_marker); |
| 4076 |
thunk.next_marker = NULL; |
| 4077 |
thunk.want_text = FALSE; |
| 4078 |
|
| 4079 |
ctxt = g_markup_parse_context_new(&parser, 0, (gpointer)&thunk, NULL); |
| 4080 |
|
| 4081 |
if (!g_markup_parse_context_parse(ctxt, buf.buffer, buf.buffer_pos, &err)) { |
| 4082 |
if (hdl->last_message) g_free(hdl->last_message); |
| 4083 |
hdl->last_message = g_strdup(err->message); |
| 4084 |
result = S3_RESULT_FAIL; |
| 4085 |
goto cleanup; |
| 4086 |
} |
| 4087 |
|
| 4088 |
if (!g_markup_parse_context_end_parse(ctxt, &err)) { |
| 4089 |
if (hdl->last_message) g_free(hdl->last_message); |
| 4090 |
hdl->last_message = g_strdup(err->message); |
| 4091 |
result = S3_RESULT_FAIL; |
| 4092 |
goto cleanup; |
| 4093 |
} |
| 4094 |
|
| 4095 |
g_markup_parse_context_free(ctxt); |
| 4096 |
ctxt = NULL; |
| 4097 |
} while (thunk.next_marker); |
| 4098 |
|
| 4099 |
cleanup: |
| 4100 |
if (err) g_error_free(err); |
| 4101 |
if (thunk.text) g_free(thunk.text); |
| 4102 |
if (thunk.next_marker) g_free(thunk.next_marker); |
| 4103 |
if (ctxt) g_markup_parse_context_free(ctxt); |
| 4104 |
if (buf.buffer) g_free(buf.buffer); |
| 4105 |
|
| 4106 |
if (result != S3_RESULT_OK) { |
| 4107 |
slist_free_full(thunk.object_list, free_s3_object); |
| 4108 |
return FALSE; |
| 4109 |
} else { |
| 4110 |
*list = thunk.object_list; |
| 4111 |
if(total_size) { |
| 4112 |
*total_size = thunk.size; |
| 4113 |
} |
| 4114 |
return TRUE; |
| 4115 |
} |
| 4116 |
} |
| 4117 |
|
| 4118 |
gboolean |
| 4119 |
s3_init_restore( |
| 4120 |
S3Handle *hdl, |
| 4121 |
const char *bucket, |
| 4122 |
const char *key) |
| 4123 |
{ |
| 4124 |
CurlBuffer data; |
| 4125 |
s3_result_t result = S3_RESULT_FAIL; |
| 4126 |
static result_handling_t result_handling[] = { |
| 4127 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 4128 |
{ 202, 0, 0, S3_RESULT_OK }, |
| 4129 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 4130 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 4131 |
}; |
| 4132 |
|
| 4133 |
data.buffer = "<RestoreRequest xmlns=\"http://s3.amazonaws.com/doc/2006-3-01\"> <Days>4</Days> </RestoreRequest>"; |
| 4134 |
data.buffer_len = strlen(data.buffer); |
| 4135 |
data.buffer_pos = 0; |
| 4136 |
data.max_buffer_size = data.buffer_len; |
| 4137 |
data.end_of_buffer = TRUE; |
| 4138 |
data.mutex = NULL; |
| 4139 |
data.cond = NULL; |
| 4140 |
|
| 4141 |
result = perform_request(hdl, "POST", bucket, key, "restore", NULL, |
| 4142 |
"application/xml", NULL, NULL, |
| 4143 |
s3_buffer_read_func, s3_buffer_reset_func, |
| 4144 |
s3_buffer_size_func, s3_buffer_md5_func, |
| 4145 |
&data, |
| 4146 |
NULL, NULL, NULL, |
| 4147 |
NULL, NULL, result_handling, FALSE); |
| 4148 |
|
| 4149 |
return (result == S3_RESULT_OK); |
| 4150 |
} |
| 4151 |
|
| 4152 |
s3_head_t * |
| 4153 |
s3_head( |
| 4154 |
S3Handle *hdl, |
| 4155 |
const char *bucket, |
| 4156 |
const char *key) |
| 4157 |
{ |
| 4158 |
s3_result_t result = S3_RESULT_FAIL; |
| 4159 |
s3_head_t *head; |
| 4160 |
static result_handling_t result_handling[] = { |
| 4161 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 4162 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 4163 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 4164 |
}; |
| 4165 |
|
| 4166 |
amfree(hdl->x_amz_expiration); |
| 4167 |
amfree(hdl->x_amz_restore); |
| 4168 |
result = perform_request(hdl, "HEAD", bucket, key, NULL, NULL, NULL, NULL, |
| 4169 |
NULL, |
| 4170 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 4171 |
NULL, NULL, result_handling, FALSE); |
| 4172 |
|
| 4173 |
if (result != S3_RESULT_OK) { |
| 4174 |
return NULL; |
| 4175 |
} |
| 4176 |
head = g_new0(s3_head_t, 1); |
| 4177 |
head->key = g_strdup(key); |
| 4178 |
head->x_amz_expiration = g_strdup(hdl->x_amz_expiration); |
| 4179 |
head->x_amz_restore = g_strdup(hdl->x_amz_restore); |
| 4180 |
return head; |
| 4181 |
} |
| 4182 |
|
| 4183 |
void |
| 4184 |
free_s3_head( |
| 4185 |
s3_head_t *head) |
| 4186 |
{ |
| 4187 |
g_free(head->key); |
| 4188 |
g_free(head->x_amz_expiration); |
| 4189 |
g_free(head->x_amz_restore); |
| 4190 |
g_free(head); |
| 4191 |
} |
| 4192 |
|
| 4193 |
gboolean |
| 4194 |
s3_read(S3Handle *hdl, |
| 4195 |
const char *bucket, |
| 4196 |
const char *key, |
| 4197 |
s3_write_func write_func, |
| 4198 |
s3_reset_func reset_func, |
| 4199 |
gpointer write_data, |
| 4200 |
s3_progress_func progress_func, |
| 4201 |
gpointer progress_data) |
| 4202 |
{ |
| 4203 |
s3_result_t result = S3_RESULT_FAIL; |
| 4204 |
static result_handling_t result_handling[] = { |
| 4205 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 4206 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 4207 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 4208 |
}; |
| 4209 |
int timeout = 300; /* 5 minutes */ |
| 4210 |
|
| 4211 |
g_assert(hdl != NULL); |
| 4212 |
g_assert(write_func != NULL); |
| 4213 |
|
| 4214 |
while (1) { |
| 4215 |
result = perform_request(hdl, "GET", bucket, key, NULL, NULL, NULL, |
| 4216 |
NULL, NULL, |
| 4217 |
NULL, NULL, NULL, NULL, NULL, write_func, reset_func, write_data, |
| 4218 |
progress_func, progress_data, result_handling, FALSE); |
| 4219 |
if (!hdl->read_from_glacier || |
| 4220 |
result != S3_RESULT_FAIL || |
| 4221 |
hdl->last_response_code != 403 || |
| 4222 |
hdl->last_s3_error_code != S3_ERROR_InvalidObjectState) { |
| 4223 |
break; |
| 4224 |
} |
| 4225 |
/* retry if a restore from glacier is ongoing */ |
| 4226 |
sleep(timeout); |
| 4227 |
} |
| 4228 |
|
| 4229 |
return result == S3_RESULT_OK; |
| 4230 |
} |
| 4231 |
|
| 4232 |
gboolean |
| 4233 |
s3_read_range(S3Handle *hdl, |
| 4234 |
const char *bucket, |
| 4235 |
const char *key, |
| 4236 |
const guint64 range_begin, |
| 4237 |
const guint64 range_end, |
| 4238 |
s3_write_func write_func, |
| 4239 |
s3_reset_func reset_func, |
| 4240 |
gpointer write_data, |
| 4241 |
s3_progress_func progress_func, |
| 4242 |
gpointer progress_data) |
| 4243 |
{ |
| 4244 |
s3_result_t result = S3_RESULT_FAIL; |
| 4245 |
static result_handling_t result_handling[] = { |
| 4246 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 4247 |
{ 206, 0, 0, S3_RESULT_OK }, |
| 4248 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 4249 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 4250 |
}; |
| 4251 |
struct curl_slist *headers = NULL; |
| 4252 |
char *buf; |
| 4253 |
int timeout = 300; /* 5 minutes */ |
| 4254 |
|
| 4255 |
g_assert(hdl != NULL); |
| 4256 |
g_assert(write_func != NULL); |
| 4257 |
|
| 4258 |
buf = g_strdup_printf("Range: bytes=%llu-%llu", |
| 4259 |
(long long unsigned)range_begin, |
| 4260 |
(long long unsigned) range_end); |
| 4261 |
headers = curl_slist_append(headers, buf); |
| 4262 |
g_free(buf); |
| 4263 |
|
| 4264 |
while(1) { |
| 4265 |
result = perform_request(hdl, "GET", bucket, key, NULL, NULL, NULL, |
| 4266 |
NULL, headers, |
| 4267 |
NULL, NULL, NULL, NULL, NULL, write_func, reset_func, write_data, |
| 4268 |
progress_func, progress_data, result_handling, FALSE); |
| 4269 |
|
| 4270 |
if (!hdl->read_from_glacier || |
| 4271 |
result != S3_RESULT_FAIL || |
| 4272 |
hdl->last_response_code != 403 || |
| 4273 |
hdl->last_s3_error_code != S3_ERROR_InvalidObjectState) { |
| 4274 |
break; |
| 4275 |
} |
| 4276 |
/* retry if a restore from glacier is ongoing */ |
| 4277 |
sleep(timeout); |
| 4278 |
} |
| 4279 |
|
| 4280 |
curl_slist_free_all(headers); |
| 4281 |
return result == S3_RESULT_OK; |
| 4282 |
} |
| 4283 |
|
| 4284 |
gboolean |
| 4285 |
s3_delete(S3Handle *hdl, |
| 4286 |
const char *bucket, |
| 4287 |
const char *key) |
| 4288 |
{ |
| 4289 |
s3_result_t result = S3_RESULT_FAIL; |
| 4290 |
static result_handling_t result_handling[] = { |
| 4291 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 4292 |
{ 204, 0, 0, S3_RESULT_OK }, |
| 4293 |
{ 404, 0, 0, S3_RESULT_OK }, |
| 4294 |
{ 404, S3_ERROR_NoSuchBucket, 0, S3_RESULT_OK }, |
| 4295 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 4296 |
{ 409, 0, 0, S3_RESULT_OK }, |
| 4297 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 4298 |
}; |
| 4299 |
|
| 4300 |
g_assert(hdl != NULL); |
| 4301 |
|
| 4302 |
result = perform_request(hdl, "DELETE", bucket, key, NULL, NULL, |
| 4303 |
"application/xml", NULL, NULL, |
| 4304 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 4305 |
result_handling, FALSE); |
| 4306 |
|
| 4307 |
return result == S3_RESULT_OK; |
| 4308 |
} |
| 4309 |
|
| 4310 |
int |
| 4311 |
s3_multi_delete(S3Handle *hdl, |
| 4312 |
const char *bucket, |
| 4313 |
GSList *objects) |
| 4314 |
{ |
| 4315 |
GString *query; |
| 4316 |
CurlBuffer data; |
| 4317 |
s3_result_t result = S3_RESULT_FAIL; |
| 4318 |
static result_handling_t result_handling[] = { |
| 4319 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 4320 |
{ 204, 0, 0, S3_RESULT_OK }, |
| 4321 |
{ 400, 0, 0, S3_RESULT_NOTIMPL }, |
| 4322 |
{ 403, 0, 0, S3_RESULT_NOTIMPL }, |
| 4323 |
{ 404, S3_ERROR_NoSuchBucket, 0, S3_RESULT_OK }, |
| 4324 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 4325 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 4326 |
}; |
| 4327 |
|
| 4328 |
g_assert(hdl != NULL); |
| 4329 |
|
| 4330 |
query = g_string_new(NULL); |
| 4331 |
if (hdl->s3_api == S3_API_SWIFT_1 || |
| 4332 |
hdl->s3_api == S3_API_SWIFT_2 || |
| 4333 |
hdl->s3_api == S3_API_SWIFT_3) { |
| 4334 |
char *container = s3_uri_encode(bucket, TRUE); |
| 4335 |
char *cmd = "DELETE"; |
| 4336 |
while (objects != NULL) { |
| 4337 |
s3_object *object = objects->data; |
| 4338 |
char *name = s3_uri_encode(object->key, TRUE); |
| 4339 |
g_string_append_printf(query, "%s/%s\n", container, name); |
| 4340 |
objects = objects->next; |
| 4341 |
} |
| 4342 |
|
| 4343 |
data.buffer_len = query->len; |
| 4344 |
data.buffer = query->str; |
| 4345 |
data.buffer_pos = 0; |
| 4346 |
data.max_buffer_size = data.buffer_len; |
| 4347 |
data.end_of_buffer = TRUE; |
| 4348 |
data.mutex = NULL; |
| 4349 |
data.cond = NULL; |
| 4350 |
|
| 4351 |
if (hdl->s3_api == S3_API_SWIFT_3) |
| 4352 |
cmd = "POST"; |
| 4353 |
result = perform_request(hdl, cmd, "", NULL, "bulk-delete", NULL, |
| 4354 |
"text/plain", NULL, NULL, |
| 4355 |
s3_buffer_read_func, s3_buffer_reset_func, |
| 4356 |
s3_buffer_size_func, s3_buffer_md5_func, |
| 4357 |
&data, NULL, NULL, NULL, NULL, NULL, |
| 4358 |
result_handling, FALSE); |
| 4359 |
|
| 4360 |
} else { |
| 4361 |
g_string_append(query, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); |
| 4362 |
g_string_append(query, "<Delete>\n"); |
| 4363 |
if (!hdl->verbose) { |
| 4364 |
g_string_append(query, " <Quiet>true</Quiet>\n"); |
| 4365 |
} |
| 4366 |
while (objects != NULL) { |
| 4367 |
s3_object *object = objects->data; |
| 4368 |
g_string_append(query, " <Object>\n"); |
| 4369 |
g_string_append(query, " <Key>"); |
| 4370 |
g_string_append(query, object->key); |
| 4371 |
g_string_append(query, "</Key>\n"); |
| 4372 |
g_string_append(query, " </Object>\n"); |
| 4373 |
objects = objects->next; |
| 4374 |
} |
| 4375 |
g_string_append(query, "</Delete>\n"); |
| 4376 |
|
| 4377 |
data.buffer_len = query->len; |
| 4378 |
data.buffer = query->str; |
| 4379 |
data.buffer_pos = 0; |
| 4380 |
data.max_buffer_size = data.buffer_len; |
| 4381 |
data.end_of_buffer = TRUE; |
| 4382 |
data.mutex = NULL; |
| 4383 |
data.cond = NULL; |
| 4384 |
|
| 4385 |
result = perform_request(hdl, "POST", bucket, NULL, "delete", NULL, |
| 4386 |
"application/xml", NULL, NULL, |
| 4387 |
s3_buffer_read_func, s3_buffer_reset_func, |
| 4388 |
s3_buffer_size_func, s3_buffer_md5_func, |
| 4389 |
&data, NULL, NULL, NULL, NULL, NULL, |
| 4390 |
result_handling, FALSE); |
| 4391 |
} |
| 4392 |
|
| 4393 |
g_string_free(query, TRUE); |
| 4394 |
if (result == S3_RESULT_OK) { |
| 4395 |
return 1; |
| 4396 |
} else if (result == S3_RESULT_NOTIMPL) { |
| 4397 |
s3_new_curl(hdl); |
| 4398 |
return 2; |
| 4399 |
} else { |
| 4400 |
return 0; |
| 4401 |
} |
| 4402 |
} |
| 4403 |
|
| 4404 |
gboolean |
| 4405 |
s3_make_bucket(S3Handle *hdl, |
| 4406 |
const char *bucket, |
| 4407 |
const char *project_id) |
| 4408 |
{ |
| 4409 |
char *body = NULL; |
| 4410 |
char *verb = "PUT"; |
| 4411 |
char *content_type = NULL; |
| 4412 |
s3_result_t result = S3_RESULT_FAIL; |
| 4413 |
static result_handling_t result_handling[] = { |
| 4414 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 4415 |
{ 201, 0, 0, S3_RESULT_OK }, |
| 4416 |
{ 202, 0, 0, S3_RESULT_OK }, |
| 4417 |
{ 204, 0, 0, S3_RESULT_OK }, |
| 4418 |
{ 404, S3_ERROR_NoSuchBucket, 0, S3_RESULT_RETRY }, |
| 4419 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 4420 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 4421 |
}; |
| 4422 |
regmatch_t pmatch[4]; |
| 4423 |
CurlBuffer buf = {NULL, 0, 0, 0, TRUE, NULL, NULL}, *ptr = NULL; |
| 4424 |
s3_read_func read_func = NULL; |
| 4425 |
s3_reset_func reset_func = NULL; |
| 4426 |
s3_md5_func md5_func = NULL; |
| 4427 |
s3_size_func size_func = NULL; |
| 4428 |
GString *CreateBucketConfiguration; |
| 4429 |
gboolean add_create = FALSE; |
| 4430 |
|
| 4431 |
g_assert(hdl != NULL); |
| 4432 |
|
| 4433 |
CreateBucketConfiguration = g_string_new("<CreateBucketConfiguration"); |
| 4434 |
if (g_str_equal(hdl->host, "gss.iijgio.com")) { |
| 4435 |
g_string_append(CreateBucketConfiguration, |
| 4436 |
" xmlns=\"http://acs.iijgio.com/doc/2006-03-01/\""); |
| 4437 |
} |
| 4438 |
g_string_append(CreateBucketConfiguration, ">"); |
| 4439 |
if (is_non_empty_string(hdl->bucket_location) && |
| 4440 |
strcmp(hdl->bucket_location, "us-east-1") != 0 && |
| 4441 |
!g_str_equal(AMAZON_WILDCARD_LOCATION, hdl->bucket_location)) { |
| 4442 |
if (s3_bucket_location_compat(bucket)) { |
| 4443 |
g_string_append_printf(CreateBucketConfiguration, |
| 4444 |
"<LocationConstraint>%s</LocationConstraint>", |
| 4445 |
hdl->bucket_location); |
| 4446 |
add_create = TRUE; |
| 4447 |
} else { |
| 4448 |
hdl->last_message = g_strdup_printf(_( |
| 4449 |
"Location constraint given for %s, " |
| 4450 |
"but the bucket name (%s) is not usable as a subdomain."), S3_bucket_name[hdl->s3_api], bucket); |
| 4451 |
g_string_free(CreateBucketConfiguration, TRUE); |
| 4452 |
return FALSE; |
| 4453 |
} |
| 4454 |
} |
| 4455 |
if (hdl->s3_api == S3_API_OAUTH2 && hdl->storage_class) { |
| 4456 |
g_string_append_printf(CreateBucketConfiguration, |
| 4457 |
"<StorageClass>%s</StorageClass>", |
| 4458 |
hdl->storage_class); |
| 4459 |
add_create = TRUE; |
| 4460 |
} |
| 4461 |
g_string_append(CreateBucketConfiguration, "</CreateBucketConfiguration>"); |
| 4462 |
|
| 4463 |
if (hdl->s3_api == S3_API_CASTOR) { |
| 4464 |
verb = "POST"; |
| 4465 |
content_type = "application/castorcontext"; |
| 4466 |
} |
| 4467 |
|
| 4468 |
if (add_create) { |
| 4469 |
ptr = &buf; |
| 4470 |
buf.buffer = g_string_free(CreateBucketConfiguration, FALSE); |
| 4471 |
buf.buffer_len = (guint) strlen(buf.buffer); |
| 4472 |
buf.buffer_pos = 0; |
| 4473 |
buf.max_buffer_size = buf.buffer_len; |
| 4474 |
read_func = s3_buffer_read_func; |
| 4475 |
reset_func = s3_buffer_reset_func; |
| 4476 |
size_func = s3_buffer_size_func; |
| 4477 |
md5_func = s3_buffer_md5_func; |
| 4478 |
} else { |
| 4479 |
g_string_free(CreateBucketConfiguration, TRUE); |
| 4480 |
} |
| 4481 |
result = perform_request(hdl, verb, bucket, NULL, NULL, |
| 4482 |
NULL, content_type, project_id, NULL, |
| 4483 |
read_func, reset_func, size_func, md5_func, ptr, |
| 4484 |
NULL, NULL, NULL, NULL, NULL, result_handling, FALSE); |
| 4485 |
if (ptr) |
| 4486 |
g_free(ptr->buffer); |
| 4487 |
|
| 4488 |
if (result == S3_RESULT_OK || |
| 4489 |
(result != S3_RESULT_OK && |
| 4490 |
hdl->last_s3_error_code == S3_ERROR_BucketAlreadyOwnedByYou)) { |
| 4491 |
/* verify the that the location constraint on the existing bucket matches |
| 4492 |
* the one that's configured. |
| 4493 |
*/ |
| 4494 |
if (is_non_empty_string(hdl->bucket_location)) { |
| 4495 |
result = perform_request(hdl, "GET", bucket, NULL, "location", NULL, NULL, NULL, NULL, |
| 4496 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 4497 |
NULL, NULL, result_handling, FALSE); |
| 4498 |
} else { |
| 4499 |
result = perform_request(hdl, "GET", bucket, NULL, NULL, NULL, NULL, NULL, NULL, |
| 4500 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 4501 |
NULL, NULL, result_handling, FALSE); |
| 4502 |
} |
| 4503 |
|
| 4504 |
if (result == S3_RESULT_OK && is_non_empty_string(hdl->bucket_location)) { |
| 4505 |
/* return to the default state of failure */ |
| 4506 |
result = S3_RESULT_FAIL; |
| 4507 |
|
| 4508 |
/* use strndup to get a null-terminated string */ |
| 4509 |
body = g_strndup(hdl->last_response_body, hdl->last_response_body_size); |
| 4510 |
if (!body) { |
| 4511 |
hdl->last_message = g_strdup(_("No body received for location request")); |
| 4512 |
goto cleanup; |
| 4513 |
} else if ('\0' == body[0]) { |
| 4514 |
hdl->last_message = g_strdup(_("Empty body received for location request")); |
| 4515 |
goto cleanup; |
| 4516 |
} |
| 4517 |
|
| 4518 |
if (!s3_regexec_wrap(&location_con_regex, body, 4, pmatch, 0)) { |
| 4519 |
char *loc_end_open = find_regex_substring(body, pmatch[1]); |
| 4520 |
char *loc_content = find_regex_substring(body, pmatch[3]); |
| 4521 |
|
| 4522 |
/* The case of an empty string is special because XML allows |
| 4523 |
* "self-closing" tags |
| 4524 |
*/ |
| 4525 |
if (g_str_equal(AMAZON_WILDCARD_LOCATION, |
| 4526 |
hdl->bucket_location) && |
| 4527 |
'/' != loc_end_open[0]) |
| 4528 |
hdl->last_message = g_strdup(_("A wildcard location constraint is " |
| 4529 |
"configured, but the bucket has a non-empty location constraint")); |
| 4530 |
else if (!g_str_equal(AMAZON_WILDCARD_LOCATION, |
| 4531 |
hdl->bucket_location)? |
| 4532 |
strncmp(loc_content, hdl->bucket_location, strlen(hdl->bucket_location)) : |
| 4533 |
('\0' != loc_content[0])) |
| 4534 |
hdl->last_message = g_strdup_printf("The location constraint configured (%s) " |
| 4535 |
"does not match the constraint currently on the bucket (%s)", hdl->bucket_location, loc_content); |
| 4536 |
else |
| 4537 |
result = S3_RESULT_OK; |
| 4538 |
g_free(loc_end_open); |
| 4539 |
g_free(loc_content); |
| 4540 |
} else { |
| 4541 |
hdl->last_message = g_strdup_printf(_("Unexpected location response from %s"), S3_name[hdl->s3_api]); |
| 4542 |
} |
| 4543 |
} |
| 4544 |
} |
| 4545 |
|
| 4546 |
cleanup: |
| 4547 |
if (body) g_free(body); |
| 4548 |
|
| 4549 |
return result == S3_RESULT_OK; |
| 4550 |
|
| 4551 |
} |
| 4552 |
|
| 4553 |
static s3_result_t |
| 4554 |
oauth2_get_access_token( |
| 4555 |
S3Handle *hdl) |
| 4556 |
{ |
| 4557 |
GString *query; |
| 4558 |
CurlBuffer data; |
| 4559 |
s3_result_t result = S3_RESULT_FAIL; |
| 4560 |
static result_handling_t result_handling[] = { |
| 4561 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 4562 |
{ 204, 0, 0, S3_RESULT_OK }, |
| 4563 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 4564 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 4565 |
}; |
| 4566 |
char *body; |
| 4567 |
regmatch_t pmatch[2]; |
| 4568 |
|
| 4569 |
g_assert(hdl != NULL); |
| 4570 |
|
| 4571 |
query = g_string_new(NULL); |
| 4572 |
g_string_append(query, "client_id="); |
| 4573 |
g_string_append(query, hdl->client_id); |
| 4574 |
g_string_append(query, "&client_secret="); |
| 4575 |
g_string_append(query, hdl->client_secret); |
| 4576 |
g_string_append(query, "&refresh_token="); |
| 4577 |
g_string_append(query, hdl->refresh_token); |
| 4578 |
g_string_append(query, "&grant_type=refresh_token"); |
| 4579 |
|
| 4580 |
data.buffer_len = query->len; |
| 4581 |
data.buffer = query->str; |
| 4582 |
data.buffer_pos = 0; |
| 4583 |
data.max_buffer_size = data.buffer_len; |
| 4584 |
data.end_of_buffer = TRUE; |
| 4585 |
data.mutex = NULL; |
| 4586 |
data.cond = NULL; |
| 4587 |
|
| 4588 |
hdl->x_storage_url = "https://accounts.google.com/o/oauth2/token"; |
| 4589 |
hdl->getting_oauth2_access_token = 1; |
| 4590 |
result = perform_request(hdl, "POST", NULL, NULL, NULL, NULL, |
| 4591 |
"application/x-www-form-urlencoded", NULL, NULL, |
| 4592 |
s3_buffer_read_func, s3_buffer_reset_func, |
| 4593 |
s3_buffer_size_func, s3_buffer_md5_func, |
| 4594 |
&data, NULL, NULL, NULL, NULL, NULL, |
| 4595 |
result_handling, FALSE); |
| 4596 |
hdl->x_storage_url = NULL; |
| 4597 |
hdl->getting_oauth2_access_token = 0; |
| 4598 |
|
| 4599 |
/* use strndup to get a null-terminated string */ |
| 4600 |
body = g_strndup(hdl->last_response_body, hdl->last_response_body_size); |
| 4601 |
if (!body) { |
| 4602 |
hdl->last_message = g_strdup(_("No body received for location request")); |
| 4603 |
goto cleanup; |
| 4604 |
} else if ('\0' == body[0]) { |
| 4605 |
hdl->last_message = g_strdup(_("Empty body received for location request")); |
| 4606 |
goto cleanup; |
| 4607 |
} |
| 4608 |
|
| 4609 |
if (!s3_regexec_wrap(&access_token_regex, body, 2, pmatch, 0)) { |
| 4610 |
hdl->access_token = find_regex_substring(body, pmatch[1]); |
| 4611 |
hdl->x_auth_token = g_strdup(hdl->access_token); |
| 4612 |
} |
| 4613 |
if (!s3_regexec_wrap(&expires_in_regex, body, 2, pmatch, 0)) { |
| 4614 |
char *expires_in = find_regex_substring(body, pmatch[1]); |
| 4615 |
hdl->expires = time(NULL) + atoi(expires_in) - 600; |
| 4616 |
g_free(expires_in); |
| 4617 |
} |
| 4618 |
|
| 4619 |
cleanup: |
| 4620 |
g_free(body); |
| 4621 |
return result == S3_RESULT_OK; |
| 4622 |
} |
| 4623 |
|
| 4624 |
gboolean |
| 4625 |
s3_is_bucket_exists(S3Handle *hdl, |
| 4626 |
const char *bucket, |
| 4627 |
const char *prefix, |
| 4628 |
const char *project_id) |
| 4629 |
{ |
| 4630 |
s3_result_t result = S3_RESULT_FAIL; |
| 4631 |
char **query = g_new0(char *, 3); |
| 4632 |
char **q = query; |
| 4633 |
static result_handling_t result_handling[] = { |
| 4634 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 4635 |
{ 204, 0, 0, S3_RESULT_OK }, |
| 4636 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 4637 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 4638 |
}; |
| 4639 |
|
| 4640 |
if (hdl->s3_api == S3_API_SWIFT_1 || |
| 4641 |
hdl->s3_api == S3_API_SWIFT_2 || |
| 4642 |
hdl->s3_api == S3_API_SWIFT_3) { |
| 4643 |
*q++ = g_strdup("limit=1"); |
| 4644 |
} else if (hdl->s3_api == S3_API_CASTOR) { |
| 4645 |
*q++ = g_strdup("format=xml"); |
| 4646 |
*q++ = g_strdup("size=0"); |
| 4647 |
} else if (prefix) { |
| 4648 |
char *q_prefix = curl_escape(prefix, 0); |
| 4649 |
*q++ = g_strdup("max-keys=1"); |
| 4650 |
*q++ = g_strdup_printf("prefix=%s", q_prefix); |
| 4651 |
g_free(q_prefix); |
| 4652 |
} else { |
| 4653 |
*q++ = g_strdup("max-keys=1"); |
| 4654 |
} |
| 4655 |
|
| 4656 |
result = perform_request(hdl, "GET", bucket, NULL, NULL, |
| 4657 |
(const char **)query, |
| 4658 |
NULL, project_id, NULL, |
| 4659 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 4660 |
NULL, NULL, result_handling, FALSE); |
| 4661 |
|
| 4662 |
for (q = query; *q != NULL; q++) { |
| 4663 |
g_free(*q); |
| 4664 |
} |
| 4665 |
|
| 4666 |
return result == S3_RESULT_OK; |
| 4667 |
} |
| 4668 |
|
| 4669 |
gboolean |
| 4670 |
s3_delete_bucket(S3Handle *hdl, |
| 4671 |
const char *bucket) |
| 4672 |
{ |
| 4673 |
return s3_delete(hdl, bucket, NULL); |
| 4674 |
} |
| 4675 |
|
| 4676 |
/* Private structure for our "thunk", which tracks where the user is in the list |
| 4677 |
* of keys. */ |
| 4678 |
|
| 4679 |
typedef struct lifecycle_thunk { |
| 4680 |
GSList *lifecycle; /* all rules */ |
| 4681 |
lifecycle_rule *rule; |
| 4682 |
lifecycle_action *action; |
| 4683 |
|
| 4684 |
gboolean in_LifecycleConfiguration; |
| 4685 |
gboolean in_Rule; |
| 4686 |
gboolean in_ID; |
| 4687 |
gboolean in_Filter; |
| 4688 |
gboolean in_Prefix; |
| 4689 |
gboolean in_Status; |
| 4690 |
gboolean in_Transition; |
| 4691 |
gboolean in_Expiration; |
| 4692 |
gboolean in_Days; |
| 4693 |
gboolean in_Date; |
| 4694 |
gboolean in_StorageClass; |
| 4695 |
|
| 4696 |
gboolean want_text; |
| 4697 |
|
| 4698 |
gchar *text; |
| 4699 |
gsize text_len; |
| 4700 |
|
| 4701 |
gchar *error; |
| 4702 |
} lifecycle_thunk; |
| 4703 |
|
| 4704 |
void |
| 4705 |
free_lifecycle_rule( |
| 4706 |
gpointer data) |
| 4707 |
{ |
| 4708 |
lifecycle_rule *rule = (lifecycle_rule *)data; |
| 4709 |
|
| 4710 |
g_free(rule->id); |
| 4711 |
g_free(rule->filter); |
| 4712 |
g_free(rule->prefix); |
| 4713 |
g_free(rule->status); |
| 4714 |
if (rule->transition) { |
| 4715 |
g_free(rule->transition->date); |
| 4716 |
g_free(rule->transition->storage_class); |
| 4717 |
g_free(rule->transition); |
| 4718 |
} |
| 4719 |
if (rule->expiration) { |
| 4720 |
g_free(rule->expiration->date); |
| 4721 |
g_free(rule->expiration->storage_class); |
| 4722 |
g_free(rule->expiration); |
| 4723 |
} |
| 4724 |
g_free(rule); |
| 4725 |
} |
| 4726 |
|
| 4727 |
void |
| 4728 |
free_lifecycle( |
| 4729 |
GSList *lifecycle) |
| 4730 |
{ |
| 4731 |
slist_free_full(lifecycle, free_lifecycle_rule); |
| 4732 |
} |
| 4733 |
|
| 4734 |
/* Functions for a SAX parser to parse the XML from Amazon */ |
| 4735 |
|
| 4736 |
static void |
| 4737 |
lifecycle_start_element(GMarkupParseContext *context G_GNUC_UNUSED, |
| 4738 |
const gchar *element_name, |
| 4739 |
const gchar **attribute_names G_GNUC_UNUSED, |
| 4740 |
const gchar **attribute_values G_GNUC_UNUSED, |
| 4741 |
gpointer user_data, |
| 4742 |
GError **error G_GNUC_UNUSED) |
| 4743 |
{ |
| 4744 |
struct lifecycle_thunk *thunk = (lifecycle_thunk *)user_data; |
| 4745 |
|
| 4746 |
thunk->want_text = FALSE; |
| 4747 |
if (g_ascii_strcasecmp(element_name, "lifecycleconfiguration") == 0) { |
| 4748 |
thunk->in_LifecycleConfiguration = TRUE; |
| 4749 |
} else if (g_ascii_strcasecmp(element_name, "rule") == 0) { |
| 4750 |
thunk->in_Rule = TRUE; |
| 4751 |
thunk->rule = g_new0(lifecycle_rule, 1); |
| 4752 |
} else if (g_ascii_strcasecmp(element_name, "id") == 0) { |
| 4753 |
thunk->in_ID = TRUE; |
| 4754 |
thunk->want_text = TRUE; |
| 4755 |
} else if (g_ascii_strcasecmp(element_name, "filter") == 0) { |
| 4756 |
thunk->in_Filter = TRUE; |
| 4757 |
thunk->want_text = TRUE; |
| 4758 |
} else if (g_ascii_strcasecmp(element_name, "prefix") == 0) { |
| 4759 |
thunk->in_Prefix = TRUE; |
| 4760 |
thunk->want_text = TRUE; |
| 4761 |
} else if (g_ascii_strcasecmp(element_name, "status") == 0) { |
| 4762 |
thunk->in_Status = TRUE; |
| 4763 |
thunk->want_text = TRUE; |
| 4764 |
} else if (g_ascii_strcasecmp(element_name, "transition") == 0) { |
| 4765 |
thunk->in_Transition = TRUE; |
| 4766 |
thunk->action = g_new0(lifecycle_action, 1); |
| 4767 |
} else if (g_ascii_strcasecmp(element_name, "expiration") == 0) { |
| 4768 |
thunk->in_Expiration = TRUE; |
| 4769 |
thunk->action = g_new0(lifecycle_action, 1); |
| 4770 |
} else if (g_ascii_strcasecmp(element_name, "days") == 0) { |
| 4771 |
thunk->in_Days = TRUE; |
| 4772 |
thunk->want_text = TRUE; |
| 4773 |
} else if (g_ascii_strcasecmp(element_name, "date") == 0) { |
| 4774 |
thunk->in_Date = TRUE; |
| 4775 |
thunk->want_text = TRUE; |
| 4776 |
} else if (g_ascii_strcasecmp(element_name, "storageclass") == 0) { |
| 4777 |
thunk->in_StorageClass = TRUE; |
| 4778 |
thunk->want_text = TRUE; |
| 4779 |
} else { |
| 4780 |
g_free(thunk->error); |
| 4781 |
thunk->error = g_strdup("Unknown element name in lifecycle get"); |
| 4782 |
} |
| 4783 |
} |
| 4784 |
|
| 4785 |
static void |
| 4786 |
lifecycle_end_element(GMarkupParseContext *context G_GNUC_UNUSED, |
| 4787 |
const gchar *element_name, |
| 4788 |
gpointer user_data, |
| 4789 |
GError **error G_GNUC_UNUSED) |
| 4790 |
{ |
| 4791 |
lifecycle_thunk *thunk = (lifecycle_thunk *)user_data; |
| 4792 |
|
| 4793 |
if (g_ascii_strcasecmp(element_name, "lifecycleconfiguration") == 0) { |
| 4794 |
thunk->in_LifecycleConfiguration = FALSE; |
| 4795 |
} else if (g_ascii_strcasecmp(element_name, "rule") == 0) { |
| 4796 |
thunk->in_Rule = FALSE; |
| 4797 |
thunk->lifecycle = g_slist_prepend(thunk->lifecycle, thunk->rule); |
| 4798 |
thunk->rule = NULL; |
| 4799 |
} else if (g_ascii_strcasecmp(element_name, "id") == 0) { |
| 4800 |
thunk->in_ID = FALSE; |
| 4801 |
thunk->rule->id = thunk->text; |
| 4802 |
thunk->text = NULL; |
| 4803 |
thunk->want_text = FALSE; |
| 4804 |
} else if (g_ascii_strcasecmp(element_name, "filter") == 0) { |
| 4805 |
thunk->in_Filter = FALSE; |
| 4806 |
thunk->rule->filter = thunk->text; |
| 4807 |
thunk->text = NULL; |
| 4808 |
thunk->want_text = FALSE; |
| 4809 |
} else if (g_ascii_strcasecmp(element_name, "prefix") == 0) { |
| 4810 |
thunk->in_Prefix = FALSE; |
| 4811 |
thunk->rule->prefix = thunk->text; |
| 4812 |
thunk->text = NULL; |
| 4813 |
thunk->want_text = FALSE; |
| 4814 |
} else if (g_ascii_strcasecmp(element_name, "status") == 0) { |
| 4815 |
thunk->in_Status = FALSE; |
| 4816 |
thunk->rule->status = thunk->text; |
| 4817 |
thunk->text = NULL; |
| 4818 |
thunk->want_text = FALSE; |
| 4819 |
} else if (g_ascii_strcasecmp(element_name, "transition") == 0) { |
| 4820 |
thunk->in_Transition = FALSE; |
| 4821 |
thunk->rule->transition = thunk->action; |
| 4822 |
thunk->action = NULL; |
| 4823 |
} else if (g_ascii_strcasecmp(element_name, "expiration") == 0) { |
| 4824 |
thunk->in_Expiration = FALSE; |
| 4825 |
thunk->rule->expiration = thunk->action; |
| 4826 |
thunk->action = NULL; |
| 4827 |
} else if (g_ascii_strcasecmp(element_name, "days") == 0) { |
| 4828 |
thunk->in_Days = FALSE; |
| 4829 |
thunk->action->days = atoi(thunk->text); |
| 4830 |
g_free(thunk->text); |
| 4831 |
thunk->text = NULL; |
| 4832 |
thunk->want_text = FALSE; |
| 4833 |
} else if (g_ascii_strcasecmp(element_name, "date") == 0) { |
| 4834 |
thunk->in_Date = FALSE; |
| 4835 |
thunk->action->date = thunk->text; |
| 4836 |
thunk->text = NULL; |
| 4837 |
thunk->want_text = FALSE; |
| 4838 |
} else if (g_ascii_strcasecmp(element_name, "storageclass") == 0) { |
| 4839 |
thunk->in_StorageClass = FALSE; |
| 4840 |
thunk->action->storage_class = thunk->text; |
| 4841 |
thunk->text = NULL; |
| 4842 |
thunk->want_text = FALSE; |
| 4843 |
} |
| 4844 |
} |
| 4845 |
|
| 4846 |
static void |
| 4847 |
lifecycle_text(GMarkupParseContext *context G_GNUC_UNUSED, |
| 4848 |
const gchar *text, |
| 4849 |
gsize text_len, |
| 4850 |
gpointer user_data, |
| 4851 |
GError **error G_GNUC_UNUSED) |
| 4852 |
{ |
| 4853 |
lifecycle_thunk *thunk = (lifecycle_thunk *)user_data; |
| 4854 |
|
| 4855 |
if (thunk->want_text) { |
| 4856 |
if (thunk->text) g_free(thunk->text); |
| 4857 |
thunk->text = g_strndup(text, text_len); |
| 4858 |
} |
| 4859 |
} |
| 4860 |
|
| 4861 |
|
| 4862 |
gboolean |
| 4863 |
s3_get_lifecycle( |
| 4864 |
S3Handle *hdl, |
| 4865 |
const char *bucket, |
| 4866 |
GSList **lifecycle) |
| 4867 |
{ |
| 4868 |
s3_result_t result = S3_RESULT_FAIL; |
| 4869 |
CurlBuffer buf = {NULL, 0, 0, 100000, TRUE, NULL, NULL}; |
| 4870 |
struct lifecycle_thunk thunk; |
| 4871 |
GMarkupParseContext *ctxt = NULL; |
| 4872 |
static GMarkupParser parser = { lifecycle_start_element, lifecycle_end_element, lifecycle_text, NULL, NULL }; |
| 4873 |
GError *err = NULL; |
| 4874 |
static result_handling_t result_handling[] = { |
| 4875 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 4876 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 4877 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 4878 |
}; |
| 4879 |
|
| 4880 |
thunk.lifecycle = NULL; |
| 4881 |
thunk.rule = NULL; |
| 4882 |
thunk.action = NULL; |
| 4883 |
|
| 4884 |
thunk.in_LifecycleConfiguration = FALSE; |
| 4885 |
thunk.in_Rule = FALSE; |
| 4886 |
thunk.in_ID = FALSE; |
| 4887 |
thunk.in_Filter = FALSE; |
| 4888 |
thunk.in_Prefix = FALSE; |
| 4889 |
thunk.in_Status = FALSE; |
| 4890 |
thunk.in_Transition = FALSE; |
| 4891 |
thunk.in_Expiration = FALSE; |
| 4892 |
thunk.in_Days = FALSE; |
| 4893 |
thunk.in_Date = FALSE; |
| 4894 |
thunk.in_StorageClass = FALSE; |
| 4895 |
thunk.want_text = FALSE; |
| 4896 |
thunk.text = NULL; |
| 4897 |
thunk.text_len = 0; |
| 4898 |
thunk.error = NULL; |
| 4899 |
|
| 4900 |
result = perform_request(hdl, "GET", bucket, NULL, "lifecycle", NULL, |
| 4901 |
NULL, NULL, NULL, |
| 4902 |
NULL, NULL, NULL, NULL, NULL, |
| 4903 |
S3_BUFFER_WRITE_FUNCS, &buf, |
| 4904 |
NULL, NULL, result_handling, FALSE); |
| 4905 |
|
| 4906 |
if (result == S3_RESULT_FAIL && |
| 4907 |
hdl->last_response_code == 404 && |
| 4908 |
hdl->last_s3_error_code == S3_ERROR_NoSuchLifecycleConfiguration) { |
| 4909 |
result = S3_RESULT_OK; |
| 4910 |
return TRUE; |
| 4911 |
} |
| 4912 |
if (result != S3_RESULT_OK) goto cleanup; |
| 4913 |
if (buf.buffer_pos == 0) goto cleanup; |
| 4914 |
|
| 4915 |
/* run the parser over it */ |
| 4916 |
|
| 4917 |
ctxt = g_markup_parse_context_new(&parser, 0, (gpointer)&thunk, NULL); |
| 4918 |
|
| 4919 |
if (!g_markup_parse_context_parse(ctxt, buf.buffer, buf.buffer_pos, &err)) { |
| 4920 |
if (hdl->last_message) g_free(hdl->last_message); |
| 4921 |
hdl->last_message = g_strdup(err->message); |
| 4922 |
result = S3_RESULT_FAIL; |
| 4923 |
goto cleanup; |
| 4924 |
} |
| 4925 |
|
| 4926 |
if (!g_markup_parse_context_end_parse(ctxt, &err)) { |
| 4927 |
if (hdl->last_message) g_free(hdl->last_message); |
| 4928 |
hdl->last_message = g_strdup(err->message); |
| 4929 |
result = S3_RESULT_FAIL; |
| 4930 |
goto cleanup; |
| 4931 |
} |
| 4932 |
|
| 4933 |
g_markup_parse_context_free(ctxt); |
| 4934 |
ctxt = NULL; |
| 4935 |
|
| 4936 |
if (thunk.error) { |
| 4937 |
if (hdl->last_message) g_free(hdl->last_message); |
| 4938 |
hdl->last_message = thunk.error; |
| 4939 |
thunk.error = NULL; |
| 4940 |
result = S3_RESULT_FAIL; |
| 4941 |
goto cleanup; |
| 4942 |
} |
| 4943 |
|
| 4944 |
cleanup: |
| 4945 |
if (err) g_error_free(err); |
| 4946 |
if (thunk.text) g_free(thunk.text); |
| 4947 |
if (ctxt) g_markup_parse_context_free(ctxt); |
| 4948 |
if (buf.buffer) g_free(buf.buffer); |
| 4949 |
|
| 4950 |
if (result == S3_RESULT_OK) { |
| 4951 |
*lifecycle = thunk.lifecycle; |
| 4952 |
} else { |
| 4953 |
free_lifecycle(thunk.lifecycle); |
| 4954 |
} |
| 4955 |
return result == S3_RESULT_OK; |
| 4956 |
} |
| 4957 |
|
| 4958 |
gboolean |
| 4959 |
s3_put_lifecycle( |
| 4960 |
S3Handle *hdl, |
| 4961 |
const char *bucket, |
| 4962 |
GSList *lifecycle) |
| 4963 |
{ |
| 4964 |
s3_result_t result = S3_RESULT_FAIL; |
| 4965 |
CurlBuffer buf = {NULL, 0, 0, 0, TRUE, NULL, NULL}; |
| 4966 |
GString *body = g_string_new("<LifecycleConfiguration>"); |
| 4967 |
GSList *life; |
| 4968 |
lifecycle_rule *rule; |
| 4969 |
static result_handling_t result_handling[] = { |
| 4970 |
{ 200, 0, 0, S3_RESULT_OK }, |
| 4971 |
RESULT_HANDLING_ALWAYS_RETRY, |
| 4972 |
{ 0, 0, 0, /* default: */ S3_RESULT_FAIL } |
| 4973 |
}; |
| 4974 |
|
| 4975 |
for (life = lifecycle; life != NULL; life = life->next) { |
| 4976 |
rule = (lifecycle_rule *)life->data; |
| 4977 |
g_string_append_printf(body, |
| 4978 |
"<Rule><ID>%s</ID><Filter><Prefix>%s</Prefix></Filter><Status>%s</Status>", |
| 4979 |
rule->id, rule->prefix, rule->status); |
| 4980 |
if (rule->transition) { |
| 4981 |
g_string_append(body, "<Transition>"); |
| 4982 |
if (rule->transition->date) { |
| 4983 |
g_string_append_printf(body, "<Date>%s</Date>", |
| 4984 |
rule->transition->date); |
| 4985 |
} else { |
| 4986 |
g_string_append_printf(body, "<Days>%u</Days>", |
| 4987 |
rule->transition->days); |
| 4988 |
} |
| 4989 |
g_string_append_printf(body, |
| 4990 |
"<StorageClass>%s</StorageClass></Transition>", |
| 4991 |
rule->transition->storage_class); |
| 4992 |
} |
| 4993 |
if (rule->expiration) { |
| 4994 |
g_string_append(body, "<Expiration>"); |
| 4995 |
if (rule->expiration->date) { |
| 4996 |
g_string_append_printf(body, "<Date>%s</Date>", |
| 4997 |
rule->expiration->date); |
| 4998 |
} else { |
| 4999 |
g_string_append_printf(body, "<Days>%u</Days>", |
| 5000 |
rule->expiration->days); |
| 5001 |
} |
| 5002 |
g_string_append(body, "</Expiration>"); |
| 5003 |
} |
| 5004 |
g_string_append_printf(body, "</Rule>"); |
| 5005 |
} |
| 5006 |
g_string_append(body, "</LifecycleConfiguration>"); |
| 5007 |
|
| 5008 |
buf.buffer = g_string_free(body, FALSE); |
| 5009 |
buf.buffer_len = strlen(buf.buffer); |
| 5010 |
|
| 5011 |
s3_verbose(hdl, 1); |
| 5012 |
result = perform_request(hdl, "PUT", bucket, NULL, "lifecycle", NULL, |
| 5013 |
"application/xml", NULL, NULL, |
| 5014 |
S3_BUFFER_READ_FUNCS, &buf, |
| 5015 |
NULL, NULL, NULL, |
| 5016 |
NULL, NULL, result_handling, FALSE); |
| 5017 |
|
| 5018 |
return result == S3_RESULT_OK; |
| 5019 |
|
| 5020 |
} |
| 5021 |
|
| 5022 |
char * |
| 5023 |
am_strrmspace( |
| 5024 |
char *str) |
| 5025 |
{ |
| 5026 |
|
| 5027 |
char *s, *t; |
| 5028 |
|
| 5029 |
for(s=str,t=str; *s != '\0'; s++) { |
| 5030 |
if (*s != ' ') { |
| 5031 |
*t++ = *s; |
| 5032 |
} |
| 5033 |
} |
| 5034 |
*t = '\0'; |
| 5035 |
return t; |
| 5036 |
} |