Alias info refactoring
[platform/core/security/key-manager.git] / misc / ckm_tool.cpp
1 /*
2  *  Copyright (c) 2000 - 2015 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_tool.cpp
18  * @author     Maciej J. Karpiuk (m.karpiuk2@samsung.com)
19  * @version    1.0
20  */
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <iostream>
26 #include <errno.h>
27 #include <ckm/ckm-error.h>
28 #include <ckm/ckm-control.h>
29
30 using namespace std;
31
32 bool parseLong(const char *buf_ptr, long int &val)
33 {
34         char *temp;
35         errno = 0;
36         long int val_tmp = strtol(buf_ptr, &temp, 0);
37
38         if (errno)
39                 return true;
40
41         val = val_tmp;
42         return false;
43 }
44
45 int main(int argc, char *argv[])
46 {
47         if (argc < 3) {
48                 cerr << "Usage: ckm_tool [option] [opt_arg]" << endl;
49                 cerr << "option: " << endl;
50                 cerr << "\t-d\tdelete user database, opt_arg specified the user UID" << endl;
51                 cerr << "Example: ckm_tool -l 5000" << endl;
52                 return -1;
53         }
54
55         // simple input arg parser
56         for (int i = 1; i < argc - 1; i++) {
57                 if (!strcmp(argv[i], "-d")) {
58                         long int uid;
59
60                         if (parseLong(argv[i + 1], uid) || uid < 0) {
61                                 cerr << "parameter error: invalid UID provided to the -d option" << endl;
62                                 exit(-2);
63                         }
64
65                         // lock the database
66                         auto control = CKM::Control::create();
67                         int ec = control->lockUserKey(static_cast<uid_t>(uid));
68
69                         if (ec != CKM_API_SUCCESS) {
70                                 cerr << "Failed, lock DB error: " << ec << endl;
71                                 exit(ec);
72                         }
73
74                         // remove the user content
75                         ec = control->removeUserData(static_cast<uid_t>(uid));
76
77                         if (ec != CKM_API_SUCCESS) {
78                                 cerr << "Failed, remove user data error: " << ec << endl;
79                                 exit(ec);
80                         }
81                 } else {
82                         std::cout << "Not enough or invalid arguments, please try again.\n";
83                         exit(-1);
84                 }
85         }
86
87         return 0;
88 }