From 0d2f3e2dc4d61536ae91204f46bc0b4eb57e926e Mon Sep 17 00:00:00 2001 From: Takeshi Nakatani Date: Sat, 24 Jan 2015 16:36:30 +0000 Subject: [PATCH] Fixed bugs, segfault and signature error at listing. --- src/curl.cpp | 66 +++++++++++++++++++++++++++++----------------------- src/s3fs.cpp | 27 ++++++++++++--------- 2 files changed, 53 insertions(+), 40 deletions(-) diff --git a/src/curl.cpp b/src/curl.cpp index 14b51d6..3fde319 100644 --- a/src/curl.cpp +++ b/src/curl.cpp @@ -1839,8 +1839,7 @@ string S3fsCurl::CalcSignaturev2(string method, string strMD5, string content_ty return Signature; } -string S3fsCurl::CalcSignatureReal(string method, string canonical_uri, string query_string , string date2, - string canonical_headers, string payload_hash, string signed_headers, string date3) +string S3fsCurl::CalcSignatureReal(string method, string canonical_uri, string query_string, string date2, string canonical_headers, string payload_hash, string signed_headers, string date3) { string Signature, StringCQ, StringToSign; string uriencode; @@ -1854,54 +1853,63 @@ string S3fsCurl::CalcSignatureReal(string method, string canonical_uri, string q } uriencode = urlEncode(canonical_uri); - StringCQ = method + "\n"; + StringCQ = method + "\n"; if(0 == strcmp(method.c_str(),"HEAD") || 0 == strcmp(method.c_str(),"PUT") || 0 == strcmp(method.c_str(),"DELETE")){ StringCQ += uriencode + "\n" + query_string + "\n"; }else if (0 == strcmp(method.c_str(), "GET") && 0 == strcmp(uriencode.c_str(), "")) { StringCQ +="/\n\n"; - }else if (0 == strcmp(method.c_str(), "GET") && 0 == strncmp(uriencode.c_str(), "/",1)) { + }else if (0 == strcmp(method.c_str(), "GET") && 0 == strncmp(uriencode.c_str(), "/", 1)) { StringCQ += uriencode +"\n\n"; - }else if (0 == strcmp(method.c_str(), "GET") && 0 != strncmp(uriencode.c_str(), "/",1)) { + }else if (0 == strcmp(method.c_str(), "GET") && 0 != strncmp(uriencode.c_str(), "/", 1)) { StringCQ += "/\n" + urlEncode2(canonical_uri) +"\n"; }else if (0 == strcmp(method.c_str(), "POST")) { - StringCQ += uriencode +"\n" + query_string +"\n"; + StringCQ += uriencode +"\n" + query_string +"\n"; } StringCQ += canonical_headers + "\n"; StringCQ += signed_headers + "\n"; StringCQ += payload_hash; - unsigned char * cRequest = (unsigned char *)StringCQ.c_str(); - unsigned int cRequest_len= StringCQ.size(); + + char kSecret[128]; + unsigned char *kDate, *kRegion, *kService, *kSigning, *sRequest = NULL; + unsigned int kDate_len,kRegion_len, kService_len, kSigning_len, sRequest_len = 0; + char hexsRequest[64 + 1]; + int kSecret_len = snprintf(kSecret, sizeof(kSecret), "AWS4%s", S3fsCurl::AWSSecretAccessKey.c_str()); + unsigned int i; + + s3fs_HMAC(kSecret, kSecret_len, reinterpret_cast(date2.data()), date2.size(), &kDate, &kDate_len); + s3fs_HMAC(kDate, kDate_len, reinterpret_cast(endpoint.c_str()), endpoint.size(), &kRegion, &kRegion_len); + s3fs_HMAC(kRegion, kRegion_len, reinterpret_cast("s3"), sizeof("s3") - 1, &kService, &kService_len); + s3fs_HMAC(kService, kService_len, reinterpret_cast("aws4_request"), sizeof("aws4_request") - 1, &kSigning, &kSigning_len); + free(kDate); + free(kRegion); + free(kService); + + const unsigned char* cRequest = reinterpret_cast(StringCQ.c_str()); + unsigned int cRequest_len = StringCQ.size(); // DPRN("SHUNDEBUGXXXPUT: %s", cRequest); - char kSecret[128]; - unsigned char *kDate, *kRegion, *kService, *kSigning, *sRequest = NULL; - unsigned int kDate_len,kRegion_len, kService_len, kSigning_len, sRequest_len = 0; - char hexsRequest[64]; - int kSecret_len = snprintf(kSecret, sizeof(kSecret), "AWS4%s", S3fsCurl::AWSSecretAccessKey.c_str()); - unsigned int i; - - s3fs_HMAC(kSecret, kSecret_len, (unsigned char*)date2.data(), date2.size(), &kDate, &kDate_len); - s3fs_HMAC(kDate, kDate_len, (unsigned char *)endpoint.c_str(), endpoint.size(), &kRegion, &kRegion_len); - s3fs_HMAC(kRegion, kRegion_len, (unsigned char *)"s3", sizeof("s3")-1, &kService, &kService_len); - s3fs_HMAC(kService, kService_len, (unsigned char *)"aws4_request", sizeof("aws4_request")-1, &kSigning, &kSigning_len); - s3fs_sha256(cRequest, cRequest_len, &sRequest, &sRequest_len); - //for (i=0;i < sRequest_len;i++) printf("%02x", sRequest[i]); - - for (i=0;i < sRequest_len;i++) sprintf(hexsRequest+(i*2), "%02x", sRequest[i]); + for(i = 0; i < sRequest_len; i++){ + sprintf(&hexsRequest[i * 2], "%02x", sRequest[i]); + } + free(sRequest); StringToSign = "AWS4-HMAC-SHA256\n"; StringToSign += date3+"\n"; StringToSign += date2+"/" + endpoint + "/s3/aws4_request\n"; StringToSign += hexsRequest; + const unsigned char* cscope = reinterpret_cast(StringToSign.c_str()); + unsigned int cscope_len = StringToSign.size(); + unsigned char* md = NULL; + unsigned int md_len = 0; + char hexSig[64 + 1]; - unsigned char* cscope = (unsigned char*)StringToSign.c_str(); - unsigned int cscope_len = StringToSign.size(); - unsigned char* md = NULL; - unsigned int md_len = 0; - char hexSig[64]; s3fs_HMAC(kSigning, kSigning_len, cscope, cscope_len, &md, &md_len); - for (i=0; i < md_len; i++) sprintf(hexSig+(i*2), "%02x", md[i]); + for(i = 0; i < md_len; i++){ + sprintf(&hexSig[i * 2], "%02x", md[i]); + } + free(kSigning); + free(md); Signature = hexSig; diff --git a/src/s3fs.cpp b/src/s3fs.cpp index 82d3c1d..e69f5fd 100644 --- a/src/s3fs.cpp +++ b/src/s3fs.cpp @@ -2264,7 +2264,9 @@ static int list_bucket(const char* path, S3ObjList& head, const char* delimiter, { int result; string s3_realpath; - string query; + string query_delimiter;; + string query_prefix;; + string query_maxkey;; string next_marker = ""; bool truncated = true; S3fsCurl s3fscurl; @@ -2274,31 +2276,34 @@ static int list_bucket(const char* path, S3ObjList& head, const char* delimiter, FPRNN("[path=%s]", path); if(delimiter && 0 < strlen(delimiter)){ - query += "delimiter="; - query += delimiter; - query += "&"; + query_delimiter += "delimiter="; + query_delimiter += delimiter; + query_delimiter += "&"; } - query += "prefix="; + query_prefix += "&prefix="; s3_realpath = get_realpath(path); if(0 == s3_realpath.length() || '/' != s3_realpath[s3_realpath.length() - 1]){ // last word must be "/" - query += urlEncode(s3_realpath.substr(1) + "/"); + query_prefix += urlEncode(s3_realpath.substr(1) + "/"); }else{ - query += urlEncode(s3_realpath.substr(1)); + query_prefix += urlEncode(s3_realpath.substr(1)); } if (check_content_only){ - query += "&max-keys=1"; + query_maxkey += "max-keys=1"; }else{ - query += "&max-keys=1000"; + query_maxkey += "max-keys=1000"; } while(truncated){ - string each_query = query; + string each_query = query_delimiter; if(next_marker != ""){ - each_query += "&marker=" + urlEncode(next_marker); + each_query += "marker=" + urlEncode(next_marker) + "&"; next_marker = ""; } + each_query += query_maxkey; + each_query += query_prefix; + // request if(0 != (result = s3fscurl.ListBucketRequest(path, each_query.c_str()))){ DPRN("ListBucketRequest returns with error.");