tizen 2.4 release
[framework/security/key-manager.git] / src / plugin / password-plugin.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  * @file       password-plugin.cpp
17  * @author     Bartlomiej Grzelewski (b.grzelewski@samsung.com)
18  * @version    1.0
19  */
20 #include <security-server-plugin-api.h>
21
22 #include <ckm/ckm-client-info.h>
23 #include <ckm/ckm-control.h>
24 #include <ckm/ckm-type.h>
25
26 extern "C" {
27 SecurityServer::PasswordPlugin *create(void);
28 void destroy(SecurityServer::PasswordPlugin *obj);
29 } // extern "C"
30
31 namespace SecurityServer {
32
33 class KEY_MANAGER_API Plugin : public PasswordPlugin {
34 public:
35     Plugin() {}
36     virtual int changeUserPassword(const std::string &zone, uid_t user, const std::string &oldPass, const std::string &newPass);
37     virtual int login(const std::string &zone, uid_t user, const std::string &password);
38     virtual int logout(const std::string &zone, uid_t user);
39     virtual int resetUserPassword(const std::string &zone, uid_t user, const std::string &newPass);
40     virtual int removeUserData(const std::string &zone, uid_t user);
41     virtual ~Plugin() {}
42 private:
43     CKM::ClientInfo getClientInfo(const std::string &zone, uid_t user);
44 };
45
46 CKM::ClientInfo Plugin::getClientInfo(const std::string &zone, uid_t user)
47 {
48     return CKM::ClientInfo(zone, user);
49 }
50
51 int Plugin::changeUserPassword(
52     const std::string &zone,
53     uid_t user,
54     const std::string &oldPass,
55     const std::string &newPass)
56 {
57     auto control = CKM::Control::create();
58     if (!control)
59         return SECURITY_SERVER_PLUGIN_FAIL;
60
61     CKM::Password oldPwd(oldPass.begin(), oldPass.end());
62     CKM::Password newPwd(newPass.begin(), newPass.end());
63     CKM::ClientInfo clientInfo = getClientInfo(zone, user);
64
65     // CKM does not allow to change user password if database does
66     // not exists. We must create database before change password.
67     if (CKM_API_SUCCESS != control->unlockUserKey(clientInfo, oldPwd))
68         return SECURITY_SERVER_PLUGIN_FAIL;
69     if (CKM_API_SUCCESS != control->changeUserPassword(clientInfo, oldPwd, newPwd))
70         return SECURITY_SERVER_PLUGIN_FAIL;
71
72     return SECURITY_SERVER_PLUGIN_SUCCESS;
73 }
74
75 int Plugin::login(
76     const std::string &zone,
77     uid_t user,
78     const std::string &password)
79 {
80     auto control = CKM::Control::create();
81     if (!control)
82         return SECURITY_SERVER_PLUGIN_FAIL;
83
84     CKM::Password pwd(password.begin(), password.end());
85
86     if (CKM_API_SUCCESS != control->unlockUserKey(getClientInfo(zone, user), pwd))
87         return SECURITY_SERVER_PLUGIN_FAIL;
88
89     return SECURITY_SERVER_PLUGIN_SUCCESS;
90 }
91
92 int Plugin::logout(const std::string &zone, uid_t user)
93 {
94     auto control = CKM::Control::create();
95     if (!control)
96         return SECURITY_SERVER_PLUGIN_FAIL;
97
98     if (CKM_API_SUCCESS != control->lockUserKey(getClientInfo(zone, user)))
99         return SECURITY_SERVER_PLUGIN_FAIL;
100
101     return SECURITY_SERVER_PLUGIN_SUCCESS;
102 }
103
104 int Plugin::resetUserPassword(
105     const std::string &zone,
106     uid_t user,
107     const std::string &newPass)
108 {
109     auto control = CKM::Control::create();
110     if (!control)
111         return SECURITY_SERVER_PLUGIN_FAIL;
112
113     CKM::Password pwd(newPass.begin(), newPass.end());
114
115     if (CKM_API_SUCCESS != control->resetUserPassword(getClientInfo(zone, user), pwd))
116         return SECURITY_SERVER_PLUGIN_FAIL;
117
118     return SECURITY_SERVER_PLUGIN_SUCCESS;
119 }
120
121 int Plugin::removeUserData(const std::string &zone, uid_t user)
122 {
123     auto control = CKM::Control::create();
124     if (!control)
125         return SECURITY_SERVER_PLUGIN_FAIL;
126
127     if (CKM_API_SUCCESS != control->removeUserData(getClientInfo(zone, user)))
128         return SECURITY_SERVER_PLUGIN_FAIL;
129
130     return SECURITY_SERVER_PLUGIN_SUCCESS;
131 }
132
133 } // namespace SecurityServer
134
135 KEY_MANAGER_API
136 SecurityServer::PasswordPlugin* create(void) {
137     return new SecurityServer::Plugin;
138 }
139
140 KEY_MANAGER_API
141 void destroy(SecurityServer::PasswordPlugin *obj) {
142     delete obj;
143 }