Release 0.1.67
[platform/core/security/key-manager.git] / misc / ckm_db_tool / ckm_db_tool.cpp
1 /*
2  *  Copyright (c) 2000 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16 /*
17  * @file       ckm_db_tool.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include <iostream>
23 #include <sstream>
24 #include <exception>
25
26 #include <getopt.h>
27 #include <unistd.h>
28
29 #include "db-wrapper.h"
30 #include "ui.h"
31
32 using namespace std;
33 using namespace CKM;
34
35 void usage()
36 {
37         cout << "Usage: ckm_db_tool [OPTION]\n";
38         cout << "The command line tool for accessing key-manager encrypted databases.\n";
39         cout << "Options:\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;
51 }
52
53 void internalHelp()
54 {
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"
58                  << endl;
59         cout << "help              shows this help" << endl;
60         cout << "exit (Ctrl-D)     quits the program" << endl;
61 }
62
63 int main(int argc, char *argv[])
64 {
65         try {
66                 uid_t uid = 0;
67                 Password pass;
68                 std::string argcmd;
69                 bool shouldDecrypt = false;
70                 while(1) {
71                         int option_index = 0;
72
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'},
79                                 {0,         0,                 0, 0  }
80                         };
81
82                         int c = getopt_long(argc, argv, "u:c:p:h", long_options, &option_index);
83
84                         if (-1 == c)
85                                 break;
86
87                         switch (c) {
88                                 default:
89                                 case ':':
90                                 case '?':
91                                 case 'h':
92                                         usage();
93                                         return 0;
94                                 case 'u':
95                                         uid = std::stoi(optarg);
96                                         break;
97                                 case 'c':
98                                         argcmd = optarg;
99                                         break;
100                                 case 'p':
101                                         pass = optarg;
102                                         break;
103                                 case 'd':
104                                         shouldDecrypt = true;
105                                         break;
106                         }
107                 }
108
109                 // unlock db
110                 DbWrapper dbw(uid, pass);
111                 int retCode;
112
113                 if (CKM_API_SUCCESS != (retCode = dbw.unlock())) {
114                         UI::error() << "unlocking database failed: " << APICodeToString(retCode) << endl;
115                         return -1;
116                 }
117
118                 UI::info() << "database unlocked" << endl;
119
120                 while (true) {
121                         string cmd;
122
123                         if (argcmd.empty()) {
124                                 cmd = UI::promptLine(">");
125
126                                 if (!std::cin) {
127                                         cout << "exit" << endl;
128                                         break; // EOF
129                                 }
130                         } else {
131                                 cmd = argcmd;
132                         }
133
134                         if (cmd == "exit")
135                                 break;
136
137                         if (cmd == "help") {
138                                 internalHelp();
139                                 continue;
140                         }
141
142                         dbw.process(cmd, shouldDecrypt);
143
144                         if (!argcmd.empty())
145                                 break;
146                 }
147
148                 dbw.lock();
149                 UI::info() << "database locked" << endl;
150
151                 return 0;
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;
158         } catch (...) {
159                 UI::error() << "unknown exception" << endl;
160         }
161         return -1;
162 }
163