2 * Copyright (c) 2000 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 * @file ckm_db_tool.cpp
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
29 #include "db-wrapper.h"
37 cout << "Usage: ckm_db_tool [OPTION]\n";
38 cout << "The command line tool for accessing key-manager encrypted databases.\n";
40 cout << " -u, --uid UID User id as in <TZ_SYS_DATA>/ckm/db-<uid> - default value 0\n";
41 cout << " -p, --pass PASSWORD Password used for database encryption. For system database (uid < 5000) no password should be used.\n";
42 cout << " -c, --cmd SQLCOMMAND Sqlite3 command to execute on database. If command not provided tool will enter interactive mode.\n";
43 cout << " --decrypt Enables automatic decryption of the data column.\n";
44 cout << " -h, --help Shows this help.\n";
45 cout << "Example: Open database for user 5000 and select all data from table names\n";
46 cout << " cmd_db_tool -u 5000 -p P45W0RD \"select * from names\"\n";
47 cout << "Example: Open database for user 5001 in interactive mode\n";
48 cout << " cmd_db_tool -uid 5001 -p user-strong-password\n";
49 cout << "Example: Open database for user 0 in interactive mode\n";
50 cout << " cmd_db_tool" << endl;
55 cout << "[sqlite_command] executes sqlite command on database" << endl;
56 cout << ".tables shows a list of table names" << endl;
57 cout << ".schema shows Sqlite3 command used to create tables in the database"
59 cout << "help shows this help" << endl;
60 cout << "exit (Ctrl-D) quits the program" << endl;
63 int main(int argc, char *argv[])
69 bool shouldDecrypt = false;
73 static struct option long_options[] = {
74 {"uid", required_argument, 0, 'u'},
75 {"cmd", required_argument, 0, 'c'},
76 {"pass", required_argument, 0, 'p'},
77 {"decrypt", no_argument, 0, 'd'},
78 {"help", no_argument, 0, 'h'},
82 int c = getopt_long(argc, argv, "u:c:p:h", long_options, &option_index);
95 uid = std::stoi(optarg);
104 shouldDecrypt = true;
110 DbWrapper dbw(uid, pass);
113 if (CKM_API_SUCCESS != (retCode = dbw.unlock())) {
114 UI::error() << "unlocking database failed: " << APICodeToString(retCode) << endl;
118 UI::info() << "database unlocked" << endl;
123 if (argcmd.empty()) {
124 cmd = UI::promptLine(">");
127 cout << "exit" << endl;
142 dbw.process(cmd, shouldDecrypt);
149 UI::info() << "database locked" << endl;
152 } catch (const invalid_argument &e) {
153 UI::error() << "argument could not be converted: " << e.what() << endl;
154 } catch (const out_of_range &e) {
155 UI::error() << "argument out of range: " << e.what() << endl;
156 } catch (const exception &e) {
157 UI::error() << "unexpected error: " << e.what() << endl;
159 UI::error() << "unknown exception" << endl;