Added security-manager API
[platform/core/security/security-server.git] / src / server / client / client-security-manager.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Bartlomiej Grzelewski <b.grzelewski@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  *
18  *      Security Manager library header
19  */
20 /*
21  * @file        client-security-manager.cpp
22  * @author      Pawel Polawski (p.polawski@samsung.com)
23  * @version     1.0
24  * @brief       This file contain client side implementation of security-manager API
25  */
26
27 #include <cstdio>
28 #include <utility>
29
30 #include <dpl/log/log.h>
31 #include <dpl/exception.h>
32
33 #include <message-buffer.h>
34 #include <client-common.h>
35 #include <protocols.h>
36
37 #include <security-manager.h>
38 #include <security-server.h>
39
40
41 SECURITY_MANAGER_API
42 int security_manager_app_inst_req_new(app_inst_req **pp_req)
43 {
44     if (!pp_req)
45         return SECURITY_MANAGER_ERROR_INPUT_PARAM;
46
47     try {
48         *pp_req = new app_inst_req;
49     } catch (std::bad_alloc& ex) {
50         return SECURITY_MANAGER_ERROR_MEMORY;
51     }
52
53
54     return SECURITY_MANAGER_SUCCESS;
55 }
56
57 SECURITY_MANAGER_API
58 void security_manager_app_inst_req_free(app_inst_req *p_req)
59 {
60     delete p_req;
61 }
62
63 SECURITY_MANAGER_API
64 int security_manager_app_inst_req_set_app_id(app_inst_req *p_req, const char *app_id)
65 {
66     if (!p_req || !app_id)
67         return SECURITY_MANAGER_ERROR_INPUT_PARAM;
68
69     p_req->appId = app_id;
70
71     return SECURITY_MANAGER_SUCCESS;
72 }
73
74 SECURITY_MANAGER_API
75 int security_manager_app_inst_req_set_pkg_id(app_inst_req *p_req, const char *pkg_id)
76 {
77     if (!p_req || !pkg_id)
78         return SECURITY_MANAGER_ERROR_INPUT_PARAM;
79
80     p_req->pkgId = pkg_id;
81
82     return SECURITY_MANAGER_SUCCESS;
83 }
84
85 SECURITY_MANAGER_API
86 int security_manager_app_inst_req_add_allowed_user(app_inst_req *p_req, const uid_t user_id)
87 {
88     if (!p_req)
89         return SECURITY_MANAGER_ERROR_INPUT_PARAM;
90
91     p_req->allowedUsers.push_back(user_id);
92
93     return SECURITY_MANAGER_SUCCESS;
94 }
95
96 SECURITY_MANAGER_API
97 int security_manager_app_inst_req_add_privilege(app_inst_req *p_req, const char *privilege)
98 {
99     if (!p_req || !privilege)
100         return SECURITY_MANAGER_ERROR_INPUT_PARAM;
101
102     p_req->privileges.push_back(privilege);
103
104     return SECURITY_MANAGER_SUCCESS;
105 }
106
107 SECURITY_MANAGER_API
108 int security_manager_app_inst_req_add_path(app_inst_req *p_req, const char *path, const int path_type)
109 {
110     if (!p_req || !path || (path_type < 0) || (path_type >= SECURITY_MANAGER_ENUM_END))
111         return SECURITY_MANAGER_ERROR_INPUT_PARAM;
112
113     p_req->appPaths.push_back(std::make_pair(path, path_type));
114
115     return SECURITY_MANAGER_SUCCESS;
116 }
117
118 SECURITY_MANAGER_API
119 int security_manager_app_install(const app_inst_req *p_req)
120 {
121     using namespace SecurityServer;
122     MessageBuffer send, recv;
123
124     LogDebug("app_install() called");
125
126     return try_catch([&] {
127         //checking parameters
128         if (!p_req)
129             return SECURITY_MANAGER_ERROR_INPUT_PARAM;
130         if (p_req->appId.empty() || p_req->pkgId.empty())
131             return SECURITY_MANAGER_ERROR_REQ_NOT_COMPLETE;
132
133         //put data into buffer
134         Serialization::Serialize(send, (int)SecurityModuleCall::APP_INSTALL);
135         Serialization::Serialize(send, p_req->appId);
136         Serialization::Serialize(send, p_req->pkgId);
137         Serialization::Serialize(send, p_req->allowedUsers);
138         Serialization::Serialize(send, p_req->privileges);
139         Serialization::Serialize(send, p_req->appPaths);
140
141         //send buffer to server
142         int retval = sendToServer(SERVICE_SOCKET_INSTALLER, send.Pop(), recv);
143         if (retval != SECURITY_SERVER_API_SUCCESS) {
144             LogDebug("Error in sendToServer. Error code: " << retval);
145             return SECURITY_MANAGER_ERROR_UNKNOWN;
146         }
147
148         //receive response from server
149         Deserialization::Deserialize(recv, retval);
150         if (retval != SECURITY_SERVER_API_SUCCESS)
151             return SECURITY_MANAGER_ERROR_UNKNOWN;
152
153         return SECURITY_MANAGER_SUCCESS;;
154     });
155 }
156
157 SECURITY_MANAGER_API
158 int security_manager_app_uninstall(const app_inst_req *p_req)
159 {
160     using namespace SecurityServer;
161     MessageBuffer send, recv;
162
163     LogDebug("app_uninstall() called");
164
165     return try_catch([&] {
166         //checking parameters
167         if (!p_req)
168             return SECURITY_MANAGER_ERROR_INPUT_PARAM;
169         if (p_req->appId.empty())
170             return SECURITY_MANAGER_ERROR_REQ_NOT_COMPLETE;
171
172         //put data into buffer
173         Serialization::Serialize(send, (int)SecurityModuleCall::APP_UNINSTALL);
174         Serialization::Serialize(send, p_req->appId);
175
176         //send buffer to server
177         int retval = sendToServer(SERVICE_SOCKET_INSTALLER, send.Pop(), recv);
178         if (retval != SECURITY_SERVER_API_SUCCESS) {
179             LogDebug("Error in sendToServer. Error code: " << retval);
180             return SECURITY_MANAGER_ERROR_UNKNOWN;
181         }
182
183         //receive response from server
184         Deserialization::Deserialize(recv, retval);
185         if (retval != SECURITY_SERVER_API_SUCCESS)
186             return SECURITY_MANAGER_ERROR_UNKNOWN;
187
188         return SECURITY_MANAGER_SUCCESS;;
189     });
190 }
191
192