Changed functions about reading passwd file.

This commit is contained in:
Takeshi Nakatani 2017-11-19 11:49:11 +00:00
parent 7d9ac0163b
commit 97fc845a6a

View File

@ -135,8 +135,8 @@ static bool is_specified_endpoint = false;
static int s3fs_init_deferred_exit_status = 0;
static bool support_compat_dir = true;// default supports compatibility directory type
static const std::string allbucket_fields_type = ""; // special key for mapping(This name is absolutely not used as a bucket name)
static const std::string keyval_fields_type = "\t"; // special key for mapping(This name is absolutely not used as a bucket name)
static const std::string allbucket_fields_type = ""; // special key for mapping(This name is absolutely not used as a bucket name)
static const std::string keyval_fields_type = "\t"; // special key for mapping(This name is absolutely not used as a bucket name)
static const std::string aws_accesskeyid = "AWSAccessKeyId";
static const std::string aws_secretkey = "AWSSecretKey";
@ -3827,46 +3827,46 @@ static int s3fs_check_service(void)
return EXIT_SUCCESS;
}
//
// Read and Parse passwd file
//
// The line of the password file is one of the following formats:
// (1) "accesskey:secretkey" : AWS format for default(all) access key/secret key
// (2) "bucket:accesskey:secretkey" : AWS format for bucket's access key/secret key
// (3) "key=value" : Content-dependent KeyValue contents
//
// This function sets result into bucketkvmap_t, it bucket name and key&value mapping.
// If bucket name is empty(1 or 3 format), bucket name for mapping is set "\t" or "".
//
//
// Read and Parse passwd file
//
// The line of the password file is one of the following formats:
// (1) "accesskey:secretkey" : AWS format for default(all) access key/secret key
// (2) "bucket:accesskey:secretkey" : AWS format for bucket's access key/secret key
// (3) "key=value" : Content-dependent KeyValue contents
//
// This function sets result into bucketkvmap_t, it bucket name and key&value mapping.
// If bucket name is empty(1 or 3 format), bucket name for mapping is set "\t" or "".
//
// Return: 1 - OK(could parse and set mapping etc.)
// 0 - NG(could not read any value)
// -1 - Should shutdown immediately
//
//
static int parse_passwd_file(bucketkvmap_t& resmap)
{
string line;
size_t first_pos;
size_t last_pos;
size_t first_pos;
size_t last_pos;
readline_t linelist;
readline_t::iterator iter;
// open passwd file
// open passwd file
ifstream PF(passwd_file.c_str());
if(!PF.good()){
if(!PF.good()){
S3FS_PRN_EXIT("could not open passwd file : %s", passwd_file.c_str());
return -1;
return -1;
}
// read each line
// read each line
while(getline(PF, line)){
line = trim(line);
line = trim(line);
if(0 == line.size()){
continue;
}
if('#' == line[0]){
continue;
}
if(string::npos != line.find_first_of(" \t")){
if(string::npos != line.find_first_of(" \t")){
S3FS_PRN_EXIT("invalid line in passwd file, found whitespace character.");
return -1;
}
@ -3874,89 +3874,89 @@ static int parse_passwd_file(bucketkvmap_t& resmap)
S3FS_PRN_EXIT("invalid line in passwd file, found a bracket \"[\" character.");
return -1;
}
linelist.push_back(line);
}
// read '=' type
linelist.push_back(line);
}
// read '=' type
kvmap_t kv;
for(iter = linelist.begin(); iter != linelist.end(); ++iter){
first_pos = iter->find_first_of("=");
if(first_pos == string::npos){
continue;
}
// formatted by "key=val"
if(first_pos == string::npos){
continue;
}
// formatted by "key=val"
string key = trim(iter->substr(0, first_pos));
string val = trim(iter->substr(first_pos + 1, string::npos));
if(key.empty()){
continue;
}
if(kv.end() != kv.find(key)){
if(key.empty()){
continue;
}
if(kv.end() != kv.find(key)){
S3FS_PRN_WARN("same key name(%s) found in passwd file, skip this.", key.c_str());
continue;
}
kv[key] = val;
}
// set special key name
resmap[string(keyval_fields_type)] = kv;
// read ':' type
continue;
}
kv[key] = val;
}
// set special key name
resmap[string(keyval_fields_type)] = kv;
// read ':' type
for(iter = linelist.begin(); iter != linelist.end(); ++iter){
first_pos = iter->find_first_of(":");
last_pos = iter->find_last_of(":");
if(first_pos == string::npos){
continue;
}
string bucket;
string accesskey;
string secret;
if(first_pos != last_pos){
// formatted by "bucket:accesskey:secretkey"
bucket = trim(iter->substr(0, first_pos));
accesskey = trim(iter->substr(first_pos + 1, last_pos - first_pos - 1));
secret = trim(iter->substr(last_pos + 1, string::npos));
}else{
// formatted by "accesskey:secretkey"
bucket = allbucket_fields_type;
accesskey = trim(iter->substr(0, first_pos));
secret = trim(iter->substr(first_pos + 1, string::npos));
}
if(resmap.end() != resmap.find(bucket)){
if(first_pos == string::npos){
continue;
}
string bucket;
string accesskey;
string secret;
if(first_pos != last_pos){
// formatted by "bucket:accesskey:secretkey"
bucket = trim(iter->substr(0, first_pos));
accesskey = trim(iter->substr(first_pos + 1, last_pos - first_pos - 1));
secret = trim(iter->substr(last_pos + 1, string::npos));
}else{
// formatted by "accesskey:secretkey"
bucket = allbucket_fields_type;
accesskey = trim(iter->substr(0, first_pos));
secret = trim(iter->substr(first_pos + 1, string::npos));
}
if(resmap.end() != resmap.find(bucket)){
S3FS_PRN_EXIT("same bucket(%s) passwd setting found in passwd file.", ("" == bucket ? "default" : bucket.c_str()));
return -1;
}
kv.clear();
kv[string(aws_accesskeyid)] = accesskey;
kv[string(aws_secretkey)] = secret;
resmap[bucket] = kv;
}
return (0 < resmap.size() ? 1 : 0);
}
//
return -1;
}
kv.clear();
kv[string(aws_accesskeyid)] = accesskey;
kv[string(aws_secretkey)] = secret;
resmap[bucket] = kv;
}
return (resmap.empty() ? 0 : 1);
}
//
// Return: 1 - OK(could read and set accesskey etc.)
// 0 - NG(could not read)
// -1 - Should shutdown immediately
//
//
static int check_for_aws_format(const kvmap_t& kvmap)
{
string str1(aws_accesskeyid);
string str2(aws_secretkey);
if(0 == kvmap.size()){
return 0;
}
if(kvmap.end() == kvmap.find(str1) && kvmap.end() == kvmap.find(str2)){
return 0;
}
if(kvmap.end() == kvmap.find(str1) || kvmap.end() == kvmap.find(str2)){
if(kvmap.empty()){
return 0;
}
if(kvmap.end() == kvmap.find(str1) && kvmap.end() == kvmap.find(str2)){
return 0;
}
if(kvmap.end() == kvmap.find(str1) || kvmap.end() == kvmap.find(str2)){
S3FS_PRN_EXIT("AWSAccesskey or AWSSecretkey is not specified.");
return -1;
}
return -1;
}
if(!S3fsCurl::SetAccessKey(kvmap.at(str1).c_str(), kvmap.at(str2).c_str())){
S3FS_PRN_EXIT("failed to set access key/secret key.");
return -1;
return -1;
}
return 1;
return 1;
}
//
@ -4029,10 +4029,10 @@ static int check_passwd_file_perms(void)
//
static int read_passwd_file(void)
{
bucketkvmap_t bucketmap;
kvmap_t keyval;
bucketkvmap_t bucketmap;
kvmap_t keyval;
int result;
// if you got here, the password file
// exists and is readable by the
// current user, check for permissions
@ -4040,41 +4040,41 @@ static int read_passwd_file(void)
return EXIT_FAILURE;
}
//
// parse passwd file
//
//
// parse passwd file
//
result = parse_passwd_file(bucketmap);
if(-1 == result){
return EXIT_FAILURE;
}
//
// check key=value type format.
//
//
// check key=value type format.
//
if(bucketmap.end() != bucketmap.find(keyval_fields_type)){
// aws format
// aws format
result = check_for_aws_format(bucketmap[keyval_fields_type]);
if(-1 == result){
return EXIT_FAILURE;
}else if(1 == result){
// success to set
}else if(1 == result){
// success to set
return EXIT_SUCCESS;
}
}
}
string bucket_key = allbucket_fields_type;
if(0 < bucket.size() && bucketmap.end() != bucketmap.find(bucket)){
bucket_key = bucket;
}
if(bucketmap.end() == bucketmap.find(bucket_key)){
string bucket_key = allbucket_fields_type;
if(0 < bucket.size() && bucketmap.end() != bucketmap.find(bucket)){
bucket_key = bucket;
}
if(bucketmap.end() == bucketmap.find(bucket_key)){
S3FS_PRN_EXIT("Not found access key/secret key in passwd file.");
return EXIT_FAILURE;
}
keyval = bucketmap[bucket_key];
if(keyval.end() == keyval.find(string(aws_accesskeyid)) || keyval.end() == keyval.find(string(aws_secretkey))){
}
keyval = bucketmap[bucket_key];
if(keyval.end() == keyval.find(string(aws_accesskeyid)) || keyval.end() == keyval.find(string(aws_secretkey))){
S3FS_PRN_EXIT("Not found access key/secret key in passwd file.");
return EXIT_FAILURE;
}
}
if(!S3fsCurl::SetAccessKey(keyval.at(string(aws_accesskeyid)).c_str(), keyval.at(string(aws_secretkey)).c_str())){
S3FS_PRN_EXIT("failed to set internal data for access key/secret key from passwd file.");
return EXIT_FAILURE;