summaryrefslogtreecommitdiff
path: root/tools/ckm_tool.cpp
blob: ff9f6da98a6ff61fb465706de5da5cbf30029a4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 *  Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */
/*
 * @file       ckm_tool.cpp
 * @author     Maciej J. Karpiuk (m.karpiuk2@samsung.com)
 * @version    1.0
 */

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <errno.h>
#include <ckm/ckm-error.h>
#include <ckm/ckm-control.h>

using namespace std;

bool parseLong(const char *buf_ptr, long int &val)
{
    char *temp;
    errno = 0;
    long int val_tmp = strtol(buf_ptr, &temp, 0);
    if(errno)
        return true;
    val = val_tmp;
    return false;
}

int main(int argc, char* argv[])
{
    if (argc < 3) {
        cerr << "Usage: ckm_tool [option] [opt_arg]" << endl;
        cerr << "option: " << endl;
        cerr << "\t-d\tdelete user database, opt_arg specified the user UID" << endl;
        cerr << "Example: ckm_tool -l 5000" << endl;
        return -1;
    }

    // simple input arg parser
    for (int i=1; i<argc-1; i++)
    {
        if(!strcmp(argv[i], "-d"))
        {
            long int uid;
            if(parseLong(argv[i+1], uid) || uid<0) {
                cerr << "parameter error: invalid UID provided to the -d option" << endl;
                exit(-2);
            }

            // lock the database
            auto control = CKM::Control::create();
            int ec = control->lockUserKey(static_cast<uid_t>(uid));
            if(ec != CKM_API_SUCCESS) {
                cerr << "Failed, lock DB error: " << ec << endl;
                exit(ec);
            }

            // remove the user content
            ec = control->removeUserData(static_cast<uid_t>(uid));
            if(ec != CKM_API_SUCCESS) {
                cerr << "Failed, remove user data error: " << ec << endl;
                exit(ec);
            }
        }
        else {
            std::cout << "Not enough or invalid arguments, please try again.\n";
            exit(-1);
        }
    }

    return 0;
}