1 /*
2  * Copyright (c) 2021-2022 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 "sqlite_helper.h"
17 
18 #include "accesstoken_log.h"
19 #include "sqlite3ext.h"
20 #include <sys/types.h>
21 
22 namespace OHOS {
23 namespace Security {
24 namespace AccessToken {
25 namespace {
26 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "SqliteHelper"};
27 }
28 
SqliteHelper(const std::string & dbName,const std::string & dbPath,int32_t version)29 SqliteHelper::SqliteHelper(const std::string& dbName, const std::string& dbPath, int32_t version)
30     : dbName_(dbName), dbPath_(dbPath), currentVersion_(version), db_(nullptr)
31 {}
32 
~SqliteHelper()33 SqliteHelper::~SqliteHelper()
34 {}
35 
Open()36 void SqliteHelper::Open() __attribute__((no_sanitize("cfi")))
37 {
38     if (db_ != nullptr) {
39         ACCESSTOKEN_LOG_WARN(LABEL, "Db s already open");
40         return;
41     }
42     if (dbName_.empty() || dbPath_.empty() || currentVersion_ < 0) {
43         ACCESSTOKEN_LOG_ERROR(LABEL, "Param invalid, dbName: %{public}s, "
44                               "dbPath: %{public}s, currentVersion: %{public}d",
45                               dbName_.c_str(), dbPath_.c_str(), currentVersion_);
46         return;
47     }
48     // set soft heap limit as 10KB
49     const int32_t heapLimit = 10 * 1024;
50     sqlite3_soft_heap_limit64(heapLimit);
51     std::string fileName = dbPath_ + dbName_;
52     int32_t res = sqlite3_open(fileName.c_str(), &db_);
53     if (res != SQLITE_OK) {
54         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to open db: %{public}s", sqlite3_errmsg(db_));
55         return;
56     }
57 
58     SetWal();
59 
60     int32_t version = GetVersion();
61     if (version == currentVersion_) {
62         return;
63     }
64 
65     BeginTransaction();
66     if (version == DataBaseVersion::VERISION_0) {
67         OnCreate();
68     } else {
69         if (version < currentVersion_) {
70             OnUpdate(version);
71         }
72     }
73     SetVersion();
74     CommitTransaction();
75 }
76 
Close()77 void SqliteHelper::Close()
78 {
79     if (db_ == nullptr) {
80         ACCESSTOKEN_LOG_WARN(LABEL, "Do open data base first!");
81         return;
82     }
83     int32_t ret = sqlite3_close(db_);
84     if (ret != SQLITE_OK) {
85         ACCESSTOKEN_LOG_WARN(LABEL, "Sqlite3_close error, ret=%{public}d", ret);
86         return;
87     }
88     db_ = nullptr;
89 }
90 
BeginTransaction() const91 int32_t SqliteHelper::BeginTransaction() const
92 {
93     if (db_ == nullptr) {
94         ACCESSTOKEN_LOG_WARN(LABEL, "Do open data base first!");
95         return GENERAL_ERROR;
96     }
97     char* errorMessage = nullptr;
98     int32_t result = 0;
99     int32_t ret = sqlite3_exec(db_, "BEGIN;", nullptr, nullptr, &errorMessage);
100     if (ret != SQLITE_OK) {
101         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed, errorMsg: %{public}s", errorMessage);
102         result = GENERAL_ERROR;
103     }
104     sqlite3_free(errorMessage);
105     return result;
106 }
107 
CommitTransaction() const108 int32_t SqliteHelper::CommitTransaction() const
109 {
110     if (db_ == nullptr) {
111         ACCESSTOKEN_LOG_WARN(LABEL, "Do open data base first!");
112         return GENERAL_ERROR;
113     }
114     char* errorMessage = nullptr;
115     int32_t result = 0;
116     int32_t ret = sqlite3_exec(db_, "COMMIT;", nullptr, nullptr, &errorMessage);
117     if (ret != SQLITE_OK) {
118         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed, errorMsg: %{public}s", errorMessage);
119         result = GENERAL_ERROR;
120     }
121     sqlite3_free(errorMessage);
122     sqlite3_db_cacheflush(db_);
123     return result;
124 }
125 
RollbackTransaction() const126 int32_t SqliteHelper::RollbackTransaction() const
127 {
128     if (db_ == nullptr) {
129         ACCESSTOKEN_LOG_WARN(LABEL, "Do open data base first!");
130         return GENERAL_ERROR;
131     }
132     int32_t result = 0;
133     char* errorMessage = nullptr;
134     int32_t ret = sqlite3_exec(db_, "ROLLBACK;", nullptr, nullptr, &errorMessage);
135     if (ret != SQLITE_OK) {
136         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed, errorMsg: %{public}s", errorMessage);
137         result = GENERAL_ERROR;
138     }
139     sqlite3_free(errorMessage);
140     return result;
141 }
142 
Prepare(const std::string & sql) const143 Statement SqliteHelper::Prepare(const std::string& sql) const
144 {
145     return Statement(db_, sql);
146 }
147 
ExecuteSql(const std::string & sql) const148 int32_t SqliteHelper::ExecuteSql(const std::string& sql) const
149 {
150     if (db_ == nullptr) {
151         ACCESSTOKEN_LOG_WARN(LABEL, "Do open data base first!");
152         return GENERAL_ERROR;
153     }
154     char* errorMessage = nullptr;
155     int32_t result = 0;
156     int32_t res = sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, &errorMessage);
157     if (res != SQLITE_OK) {
158         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed, errorMsg: %{public}s", errorMessage);
159         result = GENERAL_ERROR;
160     }
161     sqlite3_free(errorMessage);
162     return result;
163 }
164 
SetWal() const165 void SqliteHelper::SetWal() const
166 {
167     if (db_ == nullptr) {
168         ACCESSTOKEN_LOG_WARN(LABEL, "Do open data base first!");
169         return;
170     }
171     auto statement = Prepare(PRAGMA_WAL_COMMAND);
172     if (statement.Step() != Statement::State::DONE) {
173         ACCESSTOKEN_LOG_ERROR(LABEL, "Set wal mode failed, errorMsg: %{public}s", SpitError().c_str());
174     } else {
175         ACCESSTOKEN_LOG_INFO(LABEL, "Set wal mode success!");
176     }
177 }
178 
GetVersion() const179 int32_t SqliteHelper::GetVersion() const __attribute__((no_sanitize("cfi")))
180 {
181     if (db_ == nullptr) {
182         ACCESSTOKEN_LOG_WARN(LABEL, "Do open data base first!");
183         return GENERAL_ERROR;
184     }
185     auto statement = Prepare(PRAGMA_VERSION_COMMAND);
186     int32_t version = 0;
187     while (statement.Step() == Statement::State::ROW) {
188         version = statement.GetColumnInt(0);
189     }
190     ACCESSTOKEN_LOG_INFO(LABEL, "Version: %{public}d", version);
191     return version;
192 }
193 
SetVersion() const194 void SqliteHelper::SetVersion() const
195 {
196     if (db_ == nullptr) {
197         ACCESSTOKEN_LOG_WARN(LABEL, "Do open data base first!");
198         return;
199     }
200     auto statement = Prepare(PRAGMA_VERSION_COMMAND + " = " + std::to_string(currentVersion_));
201     statement.Step();
202 }
203 
SpitError() const204 std::string SqliteHelper::SpitError() const
205 {
206     if (db_ == nullptr) {
207         ACCESSTOKEN_LOG_WARN(LABEL, "Do open data base first!");
208         return "";
209     }
210     return sqlite3_errmsg(db_);
211 }
212 } // namespace AccessToken
213 } // namespace Security
214 } // namespace OHOS
215