static void initialize_list_part(ListPart *part) { string_buffer_initialize(part->eTag); string_buffer_initialize(part->partNumber); string_buffer_initialize(part->size); string_buffer_initialize(part->lastModified); }
static S3Status convert_bls(char *blsXml, char *targetBucketReturn, char *targetPrefixReturn, int *aclGrantCountReturn, S3AclGrant *aclGrants) { ConvertBlsData data; data.targetBucketReturn = targetBucketReturn; data.targetBucketReturn[0] = 0; data.targetBucketReturnLen = 0; data.targetPrefixReturn = targetPrefixReturn; data.targetPrefixReturn[0] = 0; data.targetPrefixReturnLen = 0; data.aclGrantCountReturn = aclGrantCountReturn; data.aclGrants = aclGrants; *aclGrantCountReturn = 0; string_buffer_initialize(data.emailAddress); string_buffer_initialize(data.userId); string_buffer_initialize(data.userDisplayName); string_buffer_initialize(data.groupUri); string_buffer_initialize(data.permission); // Use a simplexml parser SimpleXml simpleXml; simplexml_initialize(&simpleXml, &convertBlsXmlCallback, &data); S3Status status = simplexml_add(&simpleXml, blsXml, strlen(blsXml)); simplexml_deinitialize(&simpleXml); return status; }
S3Status S3_convert_acl(char *aclXml, char *ownerId, char *ownerDisplayName, int *aclGrantCountReturn, S3AclGrant *aclGrants) { ConvertAclData data; data.ownerId = ownerId; data.ownerIdLen = 0; data.ownerId[0] = 0; data.ownerDisplayName = ownerDisplayName; data.ownerDisplayNameLen = 0; data.ownerDisplayName[0] = 0; data.aclGrantCountReturn = aclGrantCountReturn; data.aclGrants = aclGrants; *aclGrantCountReturn = 0; string_buffer_initialize(data.emailAddress); string_buffer_initialize(data.userId); string_buffer_initialize(data.userDisplayName); string_buffer_initialize(data.groupUri); string_buffer_initialize(data.permission); // Use a simplexml parser SimpleXml simpleXml; simplexml_initialize(&simpleXml, &convertAclXmlCallback, &data); S3Status status = simplexml_add(&simpleXml, aclXml, strlen(aclXml)); simplexml_deinitialize(&simpleXml); return status; }
void S3_list_service(S3Protocol protocol, S3STSDate stsDate, const char *accessKeyId, const char *secretAccessKey, const char *securityToken, const char *hostName, S3RequestContext *requestContext, const S3ListServiceHandler *handler, void *callbackData) { // Create and set up the callback data XmlCallbackData *data = (XmlCallbackData *) malloc(sizeof(XmlCallbackData)); if (!data) { (*(handler->responseHandler.completeCallback)) (S3StatusOutOfMemory, 0, callbackData); return; } simplexml_initialize(&(data->simpleXml), &xmlCallback, data); data->responsePropertiesCallback = handler->responseHandler.propertiesCallback; data->listServiceCallback = handler->listServiceCallback; data->responseCompleteCallback = handler->responseHandler.completeCallback; data->callbackData = callbackData; string_buffer_initialize(data->ownerId); string_buffer_initialize(data->ownerDisplayName); string_buffer_initialize(data->bucketName); string_buffer_initialize(data->creationDate); // Set up the RequestParams RequestParams params = { HttpRequestTypeGET, // httpRequestType { hostName, // hostName 0, // bucketName protocol, // protocol S3UriStylePath, // uriStyle accessKeyId, // accessKeyId secretAccessKey, // secretAccessKey securityToken, // securityToken stsDate }, // stsDate 0, // key 0, // queryParams 0, // subResource 0, // copySourceBucketName 0, // copySourceKey 0, // getConditions 0, // startByte 0, // byteCount 0, // requestProperties &propertiesCallback, // propertiesCallback 0, // toS3Callback 0, // toS3CallbackTotalSize &dataCallback, // fromS3Callback &completeCallback, // completeCallback data // callbackData }; // Perform the request request_perform(¶ms, requestContext); }
static STSStatus compute_signature(char* buffer, uint32_t bufferSize, char* paramsArray[], const uint32_t paramsCount, const char* accessKeySecret) { ////////////////////////////////////////////////////////////////////////// // sign uint32_t canonLen = 0; char canonicalizedQueryString[2048 * 3]; compose_canonicalized_query_string(canonicalizedQueryString, 2048 * 3, &canonLen, paramsArray, paramsCount); string_buffer(strToSign, 2048 * 3); string_buffer_initialize(strToSign); int fit; string_buffer_append(strToSign, "POST&%2F&", 9, fit); if (!fit) { return STSStatusUriTooLong; } string_buffer(percentTwice, 2048 * 3); string_buffer_initialize(percentTwice); percentEncode(percentTwice, canonicalizedQueryString, canonLen); string_buffer_append(strToSign, percentTwice, strlen(percentTwice), fit); //fprintf(stdout, "strToSign(%lu): %s\n", strlen(strToSign), strToSign); // Generate an HMAC-SHA-1 of the strToSign size_t akSecretLen = strlen(accessKeySecret); char newAccessKeySecret[akSecretLen + 1]; snprintf(newAccessKeySecret, akSecretLen+2, "%s&", accessKeySecret); unsigned char hmac[20]; STS_HMAC_SHA1(hmac, (unsigned char *) newAccessKeySecret, strlen(newAccessKeySecret), (unsigned char *) strToSign, strlen(strToSign)); // Now base-64 encode the results char b64[((20 + 1) * 4) / 3]; int b64Len = base64Encode(hmac, 20, b64); char b64Encoded[256]; if (!urlEncode(b64Encoded, b64, b64Len)) { return STSStatusUriTooLong; } snprintf(buffer, strlen(b64Encoded)+1, "%s", b64Encoded); return STSStatusOK; }
void S3_complete_multipart_upload(S3BucketContext *bucketContext, const char *key, S3MultipartCommitHandler *handler, const char *upload_id, int contentLength, S3RequestContext *requestContext, void *callbackData) { char subResource[512]; snprintf(subResource, 512, "uploadId=%s", upload_id); CommitMultiPartData *data = (CommitMultiPartData *) malloc(sizeof(CommitMultiPartData)); data->userdata = callbackData; data->handler = handler; string_buffer_initialize(data->location); string_buffer_initialize(data->etag); simplexml_initialize(&(data->simplexml), commitMultipartResponseXMLcallback, data); RequestParams params = { HttpRequestTypePOST, // httpRequestType { bucketContext->hostName, // hostName bucketContext->bucketName, // bucketName bucketContext->protocol, // protocol bucketContext->uriStyle, // uriStyle bucketContext->accessKeyId, // accessKeyId bucketContext->secretAccessKey, //secretAccessKey bucketContext->securityToken }, // secretToken key, // key 0, // queryParams subResource, // subResource 0, // copySourceBucketName 0, // copySourceKey 0, // getConditions 0, // startByte 0, // byteCount 0, // putProperties commitMultipartPropertiesCallback, // propertiesCallback commitMultipartPutObject, // toS3Callback contentLength, // toS3CallbackTotalSize commitMultipartCallback, // fromS3Callback commitMultipartCompleteCallback, // completeCallback data // callbackData }; request_perform(¶ms, requestContext); }
void S3_copy_object(const S3BucketContext *bucketContext, const char *key, const char *destinationBucket, const char *destinationKey, const S3PutProperties *putProperties, int64_t *lastModifiedReturn, int eTagReturnSize, char *eTagReturn, S3RequestContext *requestContext, const S3ResponseHandler *handler, void *callbackData) { // Create the callback data CopyObjectData *data = (CopyObjectData *) malloc(sizeof(CopyObjectData)); if (!data) { (*(handler->completeCallback))(S3StatusOutOfMemory, 0, callbackData); return; } simplexml_initialize(&(data->simpleXml), ©ObjectXmlCallback, data); data->responsePropertiesCallback = handler->propertiesCallback; data->responseCompleteCallback = handler->completeCallback; data->callbackData = callbackData; data->lastModifiedReturn = lastModifiedReturn; data->eTagReturnSize = eTagReturnSize; data->eTagReturn = eTagReturn; if (data->eTagReturnSize && data->eTagReturn) { data->eTagReturn[0] = 0; } data->eTagReturnLen = 0; string_buffer_initialize(data->lastModified); // Set up the RequestParams RequestParams params = { HttpRequestTypeCOPY, // httpRequestType { destinationBucket ? destinationBucket : bucketContext->bucketName, // bucketName bucketContext->protocol, // protocol bucketContext->uriStyle, // uriStyle bucketContext->accessKeyId, // accessKeyId bucketContext->secretAccessKey }, // secretAccessKey destinationKey ? destinationKey : key, // key 0, // queryParams 0, // subResource bucketContext->bucketName, // copySourceBucketName key, // copySourceKey 0, // getConditions 0, // startByte 0, // byteCount putProperties, // putProperties ©ObjectPropertiesCallback, // propertiesCallback 0, // toS3Callback 0, // toS3CallbackTotalSize ©ObjectDataCallback, // fromS3Callback ©ObjectCompleteCallback, // completeCallback data // callbackData }; // Perform the request request_perform(¶ms, requestContext); }
static S3Status xmlCallback(const char *elementPath, const char *data, int dataLen, void *callbackData) { XmlCallbackData *cbData = (XmlCallbackData *) callbackData; int fit; if (data) { if (!strcmp(elementPath, "ListAllMyBucketsResult/Owner/ID")) { string_buffer_append(cbData->ownerId, data, dataLen, fit); } else if (!strcmp(elementPath, "ListAllMyBucketsResult/Owner/DisplayName")) { string_buffer_append(cbData->ownerDisplayName, data, dataLen, fit); } else if (!strcmp(elementPath, "ListAllMyBucketsResult/Buckets/Bucket/Name")) { string_buffer_append(cbData->bucketName, data, dataLen, fit); } else if (!strcmp (elementPath, "ListAllMyBucketsResult/Buckets/Bucket/CreationDate")) { string_buffer_append(cbData->creationDate, data, dataLen, fit); } } else { if (!strcmp(elementPath, "ListAllMyBucketsResult/Buckets/Bucket")) { // Parse date. Assume ISO-8601 date format. time_t creationDate = parseIso8601Time(cbData->creationDate); // Make the callback - a bucket just finished S3Status status = (*(cbData->listServiceCallback)) (cbData->ownerId, cbData->ownerDisplayName, cbData->bucketName, creationDate, cbData->callbackData); string_buffer_initialize(cbData->bucketName); string_buffer_initialize(cbData->creationDate); return status; } } /* Avoid compiler error about variable set but not used */ (void) fit; return S3StatusOK; }
void S3_get_server_access_logging(const S3BucketContext *bucketContext, char *targetBucketReturn, char *targetPrefixReturn, int *aclGrantCountReturn, S3AclGrant *aclGrants, S3RequestContext *requestContext, const S3ResponseHandler *handler, void *callbackData) { // Create the callback data GetBlsData *gsData = (GetBlsData *) malloc(sizeof(GetBlsData)); if (!gsData) { (*(handler->completeCallback))(S3StatusOutOfMemory, 0, callbackData); return; } gsData->responsePropertiesCallback = handler->propertiesCallback; gsData->responseCompleteCallback = handler->completeCallback; gsData->callbackData = callbackData; gsData->targetBucketReturn = targetBucketReturn; gsData->targetPrefixReturn = targetPrefixReturn; gsData->aclGrantCountReturn = aclGrantCountReturn; gsData->aclGrants = aclGrants; string_buffer_initialize(gsData->blsXmlDocument); *aclGrantCountReturn = 0; // Set up the RequestParams RequestParams params = { HttpRequestTypeGET, // httpRequestType { bucketContext->hostName, // hostName bucketContext->bucketName, // bucketName bucketContext->protocol, // protocol bucketContext->uriStyle, // uriStyle bucketContext->accessKeyId, // accessKeyId bucketContext->secretAccessKey, // secretAccessKey bucketContext->securityToken, // securityToken bucketContext->authRegion }, // authRegion 0, // key 0, // queryParams "logging", // subResource 0, // copySourceBucketName 0, // copySourceKey 0, // getConditions 0, // startByte 0, // byteCount 0, // putProperties &getBlsPropertiesCallback, // propertiesCallback 0, // toS3Callback 0, // toS3CallbackTotalSize &getBlsDataCallback, // fromS3Callback &getBlsCompleteCallback, // completeCallback gsData // callbackData }; // Perform the request request_perform(¶ms, requestContext); }
void S3_test_bucket(S3Protocol protocol, S3UriStyle uriStyle, const char *accessKeyId, const char *secretAccessKey, const char *securityToken, const char *hostName, const char *bucketName, int locationConstraintReturnSize, char *locationConstraintReturn, S3RequestContext *requestContext, const S3ResponseHandler *handler, void *callbackData) { // Create the callback data TestBucketData *tbData = (TestBucketData *) malloc(sizeof(TestBucketData)); if (!tbData) { (*(handler->completeCallback))(S3StatusOutOfMemory, 0, callbackData); return; } simplexml_initialize(&(tbData->simpleXml), &testBucketXmlCallback, tbData); tbData->responsePropertiesCallback = handler->propertiesCallback; tbData->responseCompleteCallback = handler->completeCallback; tbData->callbackData = callbackData; tbData->locationConstraintReturnSize = locationConstraintReturnSize; tbData->locationConstraintReturn = locationConstraintReturn; string_buffer_initialize(tbData->locationConstraint); // Set up the RequestParams RequestParams params = { HttpRequestTypeGET, // httpRequestType { hostName, // hostName bucketName, // bucketName protocol, // protocol uriStyle, // uriStyle accessKeyId, // accessKeyId secretAccessKey, // secretAccessKey securityToken }, // securityToken 0, // key 0, // queryParams "location", // subResource 0, // copySourceBucketName 0, // copySourceKey 0, // getConditions 0, // startByte 0, // byteCount 0, // putProperties &testBucketPropertiesCallback, // propertiesCallback 0, // toS3Callback 0, // toS3CallbackTotalSize &testBucketDataCallback, // fromS3Callback &testBucketCompleteCallback, // completeCallback tbData // callbackData }; // Perform the request request_perform(¶ms, requestContext); }
static void initialize_list_multipart_upload(ListMultipartUpload *upload) { string_buffer_initialize(upload->key); string_buffer_initialize(upload->uploadId); string_buffer_initialize(upload->initiatorId); string_buffer_initialize(upload->initiatorDisplayName); string_buffer_initialize(upload->ownerId); string_buffer_initialize(upload->ownerDisplayName); string_buffer_initialize(upload->storageClass); string_buffer_initialize(upload->initiated); }
void S3_get_acl(const S3BucketContext *bucketContext, const char *key, char *ownerId, char *ownerDisplayName, int *aclGrantCountReturn, S3AclGrant *aclGrants, S3RequestContext *requestContext, const S3ResponseHandler *handler, void *callbackData) { // Create the callback data GetAclData *gaData = (GetAclData *) malloc(sizeof(GetAclData)); if (!gaData) { (*(handler->completeCallback))(S3StatusOutOfMemory, 0, callbackData); return; } gaData->responsePropertiesCallback = handler->propertiesCallback; gaData->responseCompleteCallback = handler->completeCallback; gaData->callbackData = callbackData; gaData->aclGrantCountReturn = aclGrantCountReturn; gaData->aclGrants = aclGrants; gaData->ownerId = ownerId; gaData->ownerDisplayName = ownerDisplayName; string_buffer_initialize(gaData->aclXmlDocument); *aclGrantCountReturn = 0; // Set up the RequestParams RequestParams params = { HttpRequestTypeGET, // httpRequestType { bucketContext->hostName, // hostName bucketContext->bucketName, // bucketName bucketContext->protocol, // protocol bucketContext->uriStyle, // uriStyle bucketContext->accessKeyId, // accessKeyId bucketContext->secretAccessKey }, // secretAccessKey key, // key 0, // queryParams "acl", // subResource 0, // copySourceBucketName 0, // copySourceKey 0, // getConditions 0, // startByte 0, // byteCount 0, // putProperties &getAclPropertiesCallback, // propertiesCallback 0, // toS3Callback 0, // toS3CallbackTotalSize &getAclDataCallback, // fromS3Callback &getAclCompleteCallback, // completeCallback gaData // callbackData }; // Perform the request request_perform(¶ms, requestContext); }
static void initialize_list_bucket_contents(ListBucketContents *contents) { string_buffer_initialize(contents->key); string_buffer_initialize(contents->lastModified); string_buffer_initialize(contents->eTag); string_buffer_initialize(contents->size); string_buffer_initialize(contents->ownerId); string_buffer_initialize(contents->ownerDisplayName); }
void S3_initiate_multipart(S3BucketContext *bucketContext, const char *key, S3PutProperties *putProperties, S3MultipartInitialHandler *handler, S3RequestContext *requestContext, void *callbackData) { InitialMultipartData *mdata = (InitialMultipartData *) malloc(sizeof(InitialMultipartData)); simplexml_initialize(&(mdata->simpleXml), &initialMultipartXmlCallback, mdata); string_buffer_initialize(mdata->upload_id); mdata->handler= handler; mdata->userdata = callbackData; RequestParams params = { HttpRequestTypePOST, // httpRequestType { bucketContext->hostName, // hostName bucketContext->bucketName, // bucketName bucketContext->protocol, // protocol bucketContext->uriStyle, // uriStyle bucketContext->accessKeyId, // accessKeyId bucketContext->secretAccessKey, //secretAccessKey bucketContext->securityToken }, // secretToken key, // key 0, // queryParams "uploads", // subResource 0, // copySourceBucketName 0, // copySourceKey 0, // getConditions 0, // startByte 0, // byteCount putProperties, // putProperties handler->responseHandler.propertiesCallback, // propertiesCallback 0, // toS3Callback 0, // toS3CallbackTotalSize InitialMultipartCallback, // fromS3Callback InitialMultipartCompleteCallback, // completeCallback mdata // callbackData }; // Perform the request request_perform(¶ms, requestContext); }
static STSStatus compose_post_data(char* buffer, uint32_t bufferSize, CommonRequestContext* commonContext, AssumeRoleRequestContext* assumeRoleContext) { string_buffer(queryParams, 2048*3); string_buffer_initialize(queryParams); char* queryParamsArray[32]; int paramsCount = 0; int len = 0; int start = 0; int amp = 0; #define safe_append(isNewParam, name, value) \ do { \ int fit; \ start = len; \ if (amp) { start++; } \ if (isNewParam) { \ queryParamsArray[paramsCount++] = &(queryParams[start]); \ } \ if (amp) { \ string_buffer_append(queryParams, "&", 1, fit); \ if (!fit) { \ return STSStatusUriTooLong; \ } \ len++; \ } \ string_buffer_append(queryParams, name "=", \ sizeof(name "=") - 1, fit); \ if (!fit) { \ return STSStatusUriTooLong; \ } \ len += strlen(name) + 1; \ amp = 1; \ string_buffer_append(queryParams, value, strlen(value), fit); \ len += strlen(value); \ } while (0) time_t now = time(NULL); char timebuf[256]; strftime(timebuf, sizeof(timebuf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&now)); STSUUID uuid; char* uuidString; uuid_create(&uuid); uuid_to_string(&uuid, &uuidString); char durationSeconds[10]; snprintf(durationSeconds, 10, "%d", assumeRoleContext->durationSeconds); safe_append(1, "Format", "JSON"); safe_append(1, "Version", "2015-04-01"); safe_append(1, "SignatureVersion", "1.0"); safe_append(1, "SignatureMethod", "HMAC-SHA1"); safe_append(1, "SignatureNonce", uuidString); safe_append(1, "Timestamp", timebuf); safe_append(1, "Action", commonContext->action); safe_append(1, "AccessKeyId", commonContext->accessKeyId); safe_append(1, "RoleArn", assumeRoleContext->RoleArn); safe_append(1, "RoleSessionName", assumeRoleContext->RoleSessionName); safe_append(1, "Policy", assumeRoleContext->policy); safe_append(1, "DurationSeconds", durationSeconds); params_array_sort(queryParamsArray, paramsCount); string_buffer(signature, 256); string_buffer_initialize(signature); compute_signature(signature, 256, queryParamsArray, paramsCount, commonContext->accessKeySecret); compose_url_encoded_post_data(buffer, bufferSize, queryParams, strlen(queryParams), signature); free(uuidString); return STSStatusOK; }
static S3Status convertAclXmlCallback(const char *elementPath, const char *data, int dataLen, void *callbackData) { ConvertAclData *caData = (ConvertAclData *) callbackData; int fit; if (data) { if (!strcmp(elementPath, "AccessControlPolicy/Owner/ID")) { caData->ownerIdLen += snprintf(&(caData->ownerId[caData->ownerIdLen]), S3_MAX_GRANTEE_USER_ID_SIZE - caData->ownerIdLen - 1, "%.*s", dataLen, data); if (caData->ownerIdLen >= S3_MAX_GRANTEE_USER_ID_SIZE) { return S3StatusUserIdTooLong; } } else if (!strcmp(elementPath, "AccessControlPolicy/Owner/" "DisplayName")) { caData->ownerDisplayNameLen += snprintf(&(caData->ownerDisplayName [caData->ownerDisplayNameLen]), S3_MAX_GRANTEE_DISPLAY_NAME_SIZE - caData->ownerDisplayNameLen - 1, "%.*s", dataLen, data); if (caData->ownerDisplayNameLen >= S3_MAX_GRANTEE_DISPLAY_NAME_SIZE) { return S3StatusUserDisplayNameTooLong; } } else if (!strcmp(elementPath, "AccessControlPolicy/AccessControlList/Grant/" "Grantee/EmailAddress")) { // AmazonCustomerByEmail string_buffer_append(caData->emailAddress, data, dataLen, fit); if (!fit) { return S3StatusEmailAddressTooLong; } } else if (!strcmp(elementPath, "AccessControlPolicy/AccessControlList/Grant/" "Grantee/ID")) { // CanonicalUser string_buffer_append(caData->userId, data, dataLen, fit); if (!fit) { return S3StatusUserIdTooLong; } } else if (!strcmp(elementPath, "AccessControlPolicy/AccessControlList/Grant/" "Grantee/DisplayName")) { // CanonicalUser string_buffer_append(caData->userDisplayName, data, dataLen, fit); if (!fit) { return S3StatusUserDisplayNameTooLong; } } else if (!strcmp(elementPath, "AccessControlPolicy/AccessControlList/Grant/" "Grantee/URI")) { // Group string_buffer_append(caData->groupUri, data, dataLen, fit); if (!fit) { return S3StatusGroupUriTooLong; } } else if (!strcmp(elementPath, "AccessControlPolicy/AccessControlList/Grant/" "Permission")) { // Permission string_buffer_append(caData->permission, data, dataLen, fit); if (!fit) { return S3StatusPermissionTooLong; } } } else { if (!strcmp(elementPath, "AccessControlPolicy/AccessControlList/" "Grant")) { // A grant has just been completed; so add the next S3AclGrant // based on the values read if (*(caData->aclGrantCountReturn) == S3_MAX_ACL_GRANT_COUNT) { return S3StatusTooManyGrants; } S3AclGrant *grant = &(caData->aclGrants [*(caData->aclGrantCountReturn)]); if (caData->emailAddress[0]) { grant->granteeType = S3GranteeTypeAmazonCustomerByEmail; strcpy(grant->grantee.amazonCustomerByEmail.emailAddress, caData->emailAddress); } else if (caData->userId[0] && caData->userDisplayName[0]) { grant->granteeType = S3GranteeTypeCanonicalUser; strcpy(grant->grantee.canonicalUser.id, caData->userId); strcpy(grant->grantee.canonicalUser.displayName, caData->userDisplayName); } else if (caData->groupUri[0]) { if (!strcmp(caData->groupUri, ACS_GROUP_AWS_USERS)) { grant->granteeType = S3GranteeTypeAllAwsUsers; } else if (!strcmp(caData->groupUri, ACS_GROUP_ALL_USERS)) { grant->granteeType = S3GranteeTypeAllUsers; } else if (!strcmp(caData->groupUri, ACS_GROUP_LOG_DELIVERY)) { grant->granteeType = S3GranteeTypeLogDelivery; } else { return S3StatusBadGrantee; } } else { return S3StatusBadGrantee; } if (!strcmp(caData->permission, "READ")) { grant->permission = S3PermissionRead; } else if (!strcmp(caData->permission, "WRITE")) { grant->permission = S3PermissionWrite; } else if (!strcmp(caData->permission, "READ_ACP")) { grant->permission = S3PermissionReadACP; } else if (!strcmp(caData->permission, "WRITE_ACP")) { grant->permission = S3PermissionWriteACP; } else if (!strcmp(caData->permission, "FULL_CONTROL")) { grant->permission = S3PermissionFullControl; } else { return S3StatusBadPermission; } (*(caData->aclGrantCountReturn))++; string_buffer_initialize(caData->emailAddress); string_buffer_initialize(caData->userId); string_buffer_initialize(caData->userDisplayName); string_buffer_initialize(caData->groupUri); string_buffer_initialize(caData->permission); } } return S3StatusOK; }
void S3_copy_object_range(const S3BucketContext *bucketContext, const char *key, const char *destinationBucket, const char *destinationKey, const int partNo, const char *uploadId, const unsigned long startOffset, const unsigned long count, const S3PutProperties *putProperties, int64_t *lastModifiedReturn, int eTagReturnSize, char *eTagReturn, S3RequestContext *requestContext, const S3ResponseHandler *handler, void *callbackData) { // Create the callback data CopyObjectData *data = (CopyObjectData *) malloc(sizeof(CopyObjectData)); if (!data) { (*(handler->completeCallback))(S3StatusOutOfMemory, 0, callbackData); return; } simplexml_initialize(&(data->simpleXml), ©ObjectXmlCallback, data); data->responsePropertiesCallback = handler->propertiesCallback; data->responseCompleteCallback = handler->completeCallback; data->callbackData = callbackData; data->lastModifiedReturn = lastModifiedReturn; data->eTagReturnSize = eTagReturnSize; data->eTagReturn = eTagReturn; if (data->eTagReturnSize && data->eTagReturn) { data->eTagReturn[0] = 0; } data->eTagReturnLen = 0; string_buffer_initialize(data->lastModified); // If there's a sequence ID > 0 then add a subResource, OTW pass in NULL char subResource[512]; char *subRsrc = NULL; if (partNo > 0) { snprintf(subResource, 512, "partNumber=%d&uploadId=%s", partNo, uploadId); subRsrc = subResource; } // Set up the RequestParams RequestParams params = { HttpRequestTypeCOPY, // httpRequestType { bucketContext->hostName, // hostName destinationBucket ? destinationBucket : bucketContext->bucketName, // bucketName bucketContext->protocol, // protocol bucketContext->uriStyle, // uriStyle bucketContext->accessKeyId, // accessKeyId bucketContext->secretAccessKey, // secretAccessKey bucketContext->securityToken }, // securityToken destinationKey ? destinationKey : key, // key 0, // queryParams subRsrc, // subResource bucketContext->bucketName, // copySourceBucketName key, // copySourceKey 0, // getConditions startOffset, // startByte count, // byteCount putProperties, // putProperties ©ObjectPropertiesCallback, // propertiesCallback 0, // toS3Callback 0, // toS3CallbackTotalSize ©ObjectDataCallback, // fromS3Callback ©ObjectCompleteCallback, // completeCallback data // callbackData }; // Perform the request request_perform(¶ms, requestContext); }
void S3_list_bucket(const S3BucketContext *bucketContext, const char *prefix, const char *marker, const char *delimiter, int maxkeys, S3RequestContext *requestContext, const S3ListBucketHandler *handler, void *callbackData) { // Compose the query params string_buffer(queryParams, 4096); string_buffer_initialize(queryParams); #define safe_append(name, value) \ do { \ int fit; \ if (amp) { \ string_buffer_append(queryParams, "&", 1, fit); \ if (!fit) { \ (*(handler->responseHandler.completeCallback)) \ (S3StatusQueryParamsTooLong, 0, callbackData); \ return; \ } \ } \ string_buffer_append(queryParams, name "=", \ sizeof(name "=") - 1, fit); \ if (!fit) { \ (*(handler->responseHandler.completeCallback)) \ (S3StatusQueryParamsTooLong, 0, callbackData); \ return; \ } \ amp = 1; \ char encoded[3 * 1024]; \ if (!urlEncode(encoded, value, 1024)) { \ (*(handler->responseHandler.completeCallback)) \ (S3StatusQueryParamsTooLong, 0, callbackData); \ return; \ } \ string_buffer_append(queryParams, encoded, strlen(encoded), \ fit); \ if (!fit) { \ (*(handler->responseHandler.completeCallback)) \ (S3StatusQueryParamsTooLong, 0, callbackData); \ return; \ } \ } while (0) int amp = 0; if (prefix) { safe_append("prefix", prefix); } if (marker) { safe_append("marker", marker); } if (delimiter) { safe_append("delimiter", delimiter); } if (maxkeys) { char maxKeysString[64]; snprintf(maxKeysString, sizeof(maxKeysString), "%d", maxkeys); safe_append("max-keys", maxKeysString); } ListBucketData *lbData = (ListBucketData *) malloc(sizeof(ListBucketData)); if (!lbData) { (*(handler->responseHandler.completeCallback)) (S3StatusOutOfMemory, 0, callbackData); return; } simplexml_initialize(&(lbData->simpleXml), &listBucketXmlCallback, lbData); lbData->responsePropertiesCallback = handler->responseHandler.propertiesCallback; lbData->listBucketCallback = handler->listBucketCallback; lbData->responseCompleteCallback = handler->responseHandler.completeCallback; lbData->callbackData = callbackData; string_buffer_initialize(lbData->isTruncated); string_buffer_initialize(lbData->nextMarker); initialize_list_bucket_data(lbData); // Set up the RequestParams RequestParams params = { HttpRequestTypeGET, // httpRequestType { bucketContext->hostName, // hostName bucketContext->bucketName, // bucketName bucketContext->protocol, // protocol bucketContext->uriStyle, // uriStyle bucketContext->accessKeyId, // accessKeyId bucketContext->secretAccessKey, // secretAccessKey bucketContext->securityToken }, // securityToken 0, // key queryParams[0] ? queryParams : 0, // queryParams 0, // subResource 0, // copySourceBucketName 0, // copySourceKey 0, // getConditions 0, // startByte 0, // byteCount 0, // putProperties &listBucketPropertiesCallback, // propertiesCallback 0, // toS3Callback 0, // toS3CallbackTotalSize &listBucketDataCallback, // fromS3Callback &listBucketCompleteCallback, // completeCallback lbData // callbackData }; // Perform the request request_perform(¶ms, requestContext); }
STSStatus STS_assume_role(const char* roleArn, const char* roleSessionName, const char* policy, const uint32_t durationSeconds, const char* accessKeyId, const char* accessKeySecret, STSData* assumeRoleData, char* errorMessage) { int status; int httpResponseCode; CommonRequestContext commonContext = {STSProtocolHTTPS, STS_DEFAULT_HOSTNAME, accessKeyId, accessKeySecret, "AssumeRole"}; AssumeRoleRequestContext assumeRoleContext = {roleArn, roleSessionName, policy, durationSeconds}; string_buffer(response, MAX_RESPONSE_SIZE); string_buffer_initialize(response); string_buffer(postData, 2048*3); string_buffer_initialize(postData); compose_post_data(postData, 2048*3, &commonContext, &assumeRoleContext); RequestParams params = { HttpRequestTypePOST, // httpRequestType commonContext, // commonContext assumeRoleContext, // assumeRoleContext postData // postData }; status = request_perform(¶ms, &httpResponseCode, response); if (200 == httpResponseCode) { parse_sts_result(strlen(response), response, assumeRoleData); } else if( 0 != httpResponseCode) { if (400 == httpResponseCode) { status = STSStatusInvalidParameter; } else if (403 == httpResponseCode) { status = STSStatusNoPermission; } else if (500 == httpResponseCode) { status = STSStatusInternalError; } else if (404 == httpResponseCode) { status = STSStatusInvalidAcccessKeyId; } snprintf(errorMessage, strlen(response)+1, "%s", response); } else if (0 == httpResponseCode) { switch (status) { case STSStatusOutOfMemory: snprintf(errorMessage, 256, "%s.", "Out of memory"); break; case STSStatusNameLookupError: snprintf(errorMessage, 256, "%s %s.", "Failed to lookup STS server:", commonContext.hostname); break; case STSStatusFailedToConnect: snprintf(errorMessage, 256, "%s %s.", "Failed to connect to STS server:", commonContext.hostname); break; case STSStatusConnectionFailed: snprintf(errorMessage, 256, "%s.", "Write Error or Operation Timeout"); break; case STSStatusServerFailedVerification: snprintf(errorMessage, 256, "%s.", "SSL verification failed"); break; case STSStatusInternalError: snprintf(errorMessage, 256, "%s.", "Internal Error"); break; default: break; } } return status; }
void S3_list_parts(S3BucketContext *bucketContext, const char *key, const char *partnumbermarker, const char *uploadid, const char *encodingtype, int maxparts, S3RequestContext *requestContext, const S3ListPartsHandler *handler, void *callbackData) { // Compose the query params string_buffer(queryParams, 4096); string_buffer_initialize(queryParams); #define safe_append(name, value) \ do { \ int fit; \ if (amp) { \ string_buffer_append(queryParams, "&", 1, fit); \ if (!fit) { \ (*(handler->responseHandler.completeCallback)) \ (S3StatusQueryParamsTooLong, 0, callbackData); \ return; \ } \ } \ string_buffer_append(queryParams, name "=", \ sizeof(name "=") - 1, fit); \ if (!fit) { \ (*(handler->responseHandler.completeCallback)) \ (S3StatusQueryParamsTooLong, 0, callbackData); \ return; \ } \ amp = 1; \ char encoded[3 * 1024]; \ if (!urlEncode(encoded, value, 1024)) { \ (*(handler->responseHandler.completeCallback)) \ (S3StatusQueryParamsTooLong, 0, callbackData); \ return; \ } \ string_buffer_append(queryParams, encoded, strlen(encoded), \ fit); \ if (!fit) { \ (*(handler->responseHandler.completeCallback)) \ (S3StatusQueryParamsTooLong, 0, callbackData); \ return; \ } \ } while (0) char subResource[512]; snprintf(subResource, 512, "uploadId=%s", uploadid); int amp = 0; if (partnumbermarker && *partnumbermarker) { safe_append("part-number-marker", partnumbermarker); } if (encodingtype && *encodingtype) { safe_append("encoding-type", encodingtype); } if (maxparts) { char maxPartsString[64]; snprintf(maxPartsString, sizeof(maxPartsString), "%d", maxparts); safe_append("max-parts", maxPartsString); } ListPartsData *lpData = (ListPartsData *) malloc(sizeof(ListPartsData)); if (!lpData) { (*(handler->responseHandler.completeCallback)) (S3StatusOutOfMemory, 0, callbackData); return; } simplexml_initialize(&(lpData->simpleXml), &listPartsXmlCallback, lpData); lpData->responsePropertiesCallback = handler->responseHandler.propertiesCallback; lpData->listPartsCallback = handler->responseXmlCallback; lpData->responseCompleteCallback = handler->responseHandler.completeCallback; lpData->callbackData = callbackData; string_buffer_initialize(lpData->isTruncated); string_buffer_initialize(lpData->nextPartNumberMarker); string_buffer_initialize(lpData->initiatorId); string_buffer_initialize(lpData->initiatorDisplayName); string_buffer_initialize(lpData->ownerId); string_buffer_initialize(lpData->ownerDisplayName); string_buffer_initialize(lpData->storageClass); initialize_list_parts_data(lpData); lpData->handlePartsStart = 0; // Set up the RequestParams RequestParams params = { HttpRequestTypeGET, // httpRequestType { bucketContext->hostName, // hostName bucketContext->bucketName, // bucketName bucketContext->protocol, // protocol bucketContext->uriStyle, // uriStyle bucketContext->accessKeyId, // accessKeyId bucketContext->secretAccessKey, // secretAccessKey bucketContext->securityToken }, // securityToken key, // key queryParams[0] ? queryParams : 0, // queryParams subResource, // subResource 0, // copySourceBucketName 0, // copySourceKey 0, // getConditions 0, // startByte 0, // byteCount 0, // putProperties &listPartsPropertiesCallback, // propertiesCallback 0, // toS3Callback 0, // toS3CallbackTotalSize &listPartsDataCallback, // fromS3Callback &listPartsCompleteCallback, // completeCallback lpData // callbackData }; // Perform the request request_perform(¶ms, requestContext); }