1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "json_parser.h"
17 
18 #include <fcntl.h>
19 #include <memory>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 
24 #include "accesstoken_log.h"
25 #include "access_token_error.h"
26 #include "access_token.h"
27 
28 namespace OHOS {
29 namespace Security {
30 namespace AccessToken {
31 namespace {
32 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "JsonParser"};
33 constexpr int MAX_NATIVE_CONFIG_FILE_SIZE = 5 * 1024 * 1024; // 5M
34 constexpr size_t BUFFER_SIZE = 1024;
35 }
36 
GetStringFromJson(const nlohmann::json & j,const std::string & tag,std::string & out)37 bool JsonParser::GetStringFromJson(const nlohmann::json& j, const std::string& tag, std::string& out)
38 {
39     if (j.find(tag) != j.end() && j.at(tag).is_string()) {
40         out = j.at(tag).get<std::string>();
41         return true;
42     }
43     return false;
44 }
45 
GetIntFromJson(const nlohmann::json & j,const std::string & tag,int & out)46 bool JsonParser::GetIntFromJson(const nlohmann::json& j, const std::string& tag, int& out)
47 {
48     if (j.find(tag) != j.end() && j.at(tag).is_number()) {
49         out = j.at(tag).get<int>();
50         return true;
51     }
52     return false;
53 }
54 
GetUnsignedIntFromJson(const nlohmann::json & j,const std::string & tag,unsigned int & out)55 bool JsonParser::GetUnsignedIntFromJson(const nlohmann::json& j, const std::string& tag, unsigned int& out)
56 {
57     if (j.find(tag) != j.end() && j.at(tag).is_number()) {
58         out = j.at(tag).get<unsigned int>();
59         return true;
60     }
61     return false;
62 }
63 
GetBoolFromJson(const nlohmann::json & j,const std::string & tag,bool & out)64 bool JsonParser::GetBoolFromJson(const nlohmann::json& j, const std::string& tag, bool& out)
65 {
66     if (j.find(tag) != j.end() && j.at(tag).is_boolean()) {
67         out = j.at(tag).get<bool>();
68         return true;
69     }
70     return false;
71 }
72 
ReadCfgFile(const std::string & file,std::string & rawData)73 int32_t JsonParser::ReadCfgFile(const std::string& file, std::string& rawData)
74 {
75     char filePath[PATH_MAX + 1] = {0};
76     if (realpath(file.c_str(), filePath) == NULL) {
77         return ERR_FILE_OPERATE_FAILED;
78     }
79     int32_t fd = open(filePath, O_RDONLY);
80     if (fd < 0) {
81         ACCESSTOKEN_LOG_ERROR(LABEL, "Open failed errno %{public}d.", errno);
82         return ERR_FILE_OPERATE_FAILED;
83     }
84     struct stat statBuffer;
85 
86     if (fstat(fd, &statBuffer) != 0) {
87         ACCESSTOKEN_LOG_ERROR(LABEL, "Fstat failed.");
88         close(fd);
89         return ERR_FILE_OPERATE_FAILED;
90     }
91 
92     if (statBuffer.st_size == 0) {
93         ACCESSTOKEN_LOG_ERROR(LABEL, "Config file size is invalid.");
94         close(fd);
95         return ERR_PARAM_INVALID;
96     }
97     if (statBuffer.st_size > MAX_NATIVE_CONFIG_FILE_SIZE) {
98         ACCESSTOKEN_LOG_ERROR(LABEL, "Config file size is too large.");
99         close(fd);
100         return ERR_OVERSIZE;
101     }
102     rawData.reserve(statBuffer.st_size);
103 
104     char buff[BUFFER_SIZE] = { 0 };
105     ssize_t readLen = 0;
106     while ((readLen = read(fd, buff, BUFFER_SIZE)) > 0) {
107         rawData.append(buff, readLen);
108     }
109     close(fd);
110     if (readLen == 0) {
111         return RET_SUCCESS;
112     }
113     return ERR_FILE_OPERATE_FAILED;
114 }
115 
IsDirExsit(const std::string & file)116 bool JsonParser::IsDirExsit(const std::string& file)
117 {
118     if (file.empty()) {
119         ACCESSTOKEN_LOG_ERROR(LABEL, "File path is empty");
120         return false;
121     }
122 
123     struct stat buf;
124     if (stat(file.c_str(), &buf) != 0) {
125         ACCESSTOKEN_LOG_ERROR(LABEL, "Get file attributes failed, errno %{public}d.", errno);
126         return false;
127     }
128 
129     if (!S_ISDIR(buf.st_mode)) {
130         ACCESSTOKEN_LOG_ERROR(LABEL, "File mode is not directory.");
131         return false;
132     }
133 
134     return true;
135 }
136 } // namespace AccessToken
137 } // namespace Security
138 } // namespace OHOS