Remove warnings and performance problems reported by cppcheck.
[platform/core/security/key-manager.git] / tools / 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     if(errno)
38         return true;
39     val = val_tmp;
40     return false;
41 }
42
43 int main(int argc, char* argv[])
44 {
45     if (argc < 3) {
46         cerr << "Usage: ckm_tool [option] [opt_arg]" << endl;
47         cerr << "option: " << endl;
48         cerr << "\t-d\tdelete user database, opt_arg specified the user UID" << endl;
49         cerr << "Example: ckm_tool -l 5000" << endl;
50         return -1;
51     }
52
53     // simple input arg parser
54     for (int i=1; i<argc-1; i++)
55     {
56         if(!strcmp(argv[i], "-d"))
57         {
58             long int uid;
59             if(parseLong(argv[i+1], uid) || uid<0) {
60                 cerr << "parameter error: invalid UID provided to the -d option" << endl;
61                 exit(-2);
62             }
63
64             // lock the database
65             auto control = CKM::Control::create();
66             int ec = control->lockUserKey(static_cast<uid_t>(uid));
67             if(ec != CKM_API_SUCCESS) {
68                 cerr << "Failed, lock DB error: " << ec << endl;
69                 exit(ec);
70             }
71
72             // remove the user content
73             ec = control->removeUserData(static_cast<uid_t>(uid));
74             if(ec != CKM_API_SUCCESS) {
75                 cerr << "Failed, remove user data error: " << ec << endl;
76                 exit(ec);
77             }
78         }
79         else {
80             std::cout << "Not enough or invalid arguments, please try again.\n";
81             exit(-1);
82         }
83     }
84
85     return 0;
86 }