tizen 2.4 release
[framework/security/key-manager.git] / src / manager / client-capi / ckmc-control.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 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        ckmc-control.cpp
18  * @author      Yuseok Jeon(yuseok.jeon@samsung.com)
19  * @version     1.0
20  * @brief       provides conversion methods to C from C++ for key-manager control functions.
21  */
22
23 #include <ckm/ckm-control.h>
24 #include <ckmc/ckmc-control.h>
25 #include <ckmc/ckmc-error.h>
26 #include <ckmc-type-converter.h>
27 #include <ckm/ckm-type.h>
28 #include <client-common.h>
29
30 CKM::Password _toPasswordStr(const char *str)
31 {
32     if (str == NULL)
33         return CKM::Password();
34     return CKM::Password(str);
35 }
36
37 KEY_MANAGER_CAPI
38 int ckmc_unlock_user_key(uid_t user, const char *password)
39 {
40     auto control = CKM::Control::create();
41     int ret = control->unlockUserKey(
42                 CKM::ClientInfo(user),
43                 _toPasswordStr(password));
44     return to_ckmc_error(ret);
45 }
46
47 KEY_MANAGER_CAPI
48 int ckmc_lock_user_key(uid_t user)
49 {
50     auto control = CKM::Control::create();
51     int ret = control->lockUserKey(CKM::ClientInfo(user));
52     return to_ckmc_error(ret);
53 }
54
55 KEY_MANAGER_CAPI
56 int ckmc_remove_user_data(uid_t user)
57 {
58     auto control = CKM::Control::create();
59     int ret = control->removeUserData(CKM::ClientInfo(user));
60     return to_ckmc_error(ret);
61 }
62
63 KEY_MANAGER_CAPI
64 int ckmc_change_user_password(uid_t user, const char *oldPassword, const char *newPassword)
65 {
66     auto control = CKM::Control::create();
67     int ret = control->changeUserPassword(
68                 CKM::ClientInfo(user),
69                 _toPasswordStr(oldPassword),
70                 _toPasswordStr(newPassword));
71     return to_ckmc_error(ret);
72 }
73
74 KEY_MANAGER_CAPI
75 int ckmc_reset_user_password(uid_t user, const char *newPassword)
76 {
77     auto control = CKM::Control::create();
78     int ret = control->resetUserPassword(
79                 CKM::ClientInfo(user),
80                 _toPasswordStr(newPassword));
81     return to_ckmc_error(ret);
82 }
83
84 KEY_MANAGER_CAPI
85 int ckmc_allow_access_by_adm(uid_t user, const char* owner, const char *alias, const char *accessor, ckmc_access_right_e granted)
86 {
87     if(!owner || !alias)
88         return CKMC_ERROR_INVALID_PARAMETER;
89
90     int ec, permissionMask;
91     ec = access_to_permission_mask(granted, permissionMask);
92     if(ec != CKMC_ERROR_NONE)
93         return ec;
94
95     // if label given twice, service will return an error
96     return ckmc_set_permission_by_adm(user, CKM::AliasSupport::merge(CKM::Label(owner), CKM::Name(alias)).c_str(), accessor, permissionMask);
97 }
98
99 KEY_MANAGER_CAPI
100 int ckmc_set_permission_by_adm(uid_t user, const char *alias, const char *accessor, int permissions)
101 {
102     if (!alias || !accessor)
103         return CKMC_ERROR_INVALID_PARAMETER;
104
105     auto control = CKM::Control::create();
106     return to_ckmc_error(control->setPermission(
107                 CKM::ClientInfo(user),
108                 alias,
109                 accessor,
110                 permissions));
111 }
112
113 KEY_MANAGER_CAPI
114 int ckmc_deny_access_by_adm(uid_t user, const char* owner, const char *alias, const char *accessor)
115 {
116     if(!owner || !alias)
117         return CKMC_ERROR_INVALID_PARAMETER;
118
119     // if label given twice, service will return an error
120     auto control = CKM::Control::create();
121     return to_ckmc_error(control->setPermission(
122                 CKM::ClientInfo(user),
123                 CKM::AliasSupport::merge(CKM::Label(owner),
124                 CKM::Name(alias)).c_str(),
125                 accessor,
126                 CKM::Permission::NONE));
127 }