2 * Copyright (c) 2014 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "ClientListener.h"
19 #include <app_manager.h>
26 #include <sys/types.h>
31 #include "AsmOpFactory.h"
32 #include "AsmHelper.h"
33 #include "TCUiAdaptor.h"
34 #include "AcUiAdaptor.h"
35 #include "AuthUiFactory.h"
36 #include "RoamingUtil.h"
40 #define ASM_DBUS_PATH "/org/tizen/fidoasm"
41 #define ASM_DBUS_NAME "org.tizen.fidoasm"
43 #define _FREEDESKTOP_SERVICE "org.freedesktop.DBus"
44 #define _FREEDESKTOP_PATH "/org/freedesktop/DBus"
45 #define _FREEDESKTOP_INTERFACE "org.freedesktop.DBus"
47 #define FIDO_CLIENT_SVC_PATH "/usr/bin/fido-service"
48 #define FIDO_RAGENT_SVC_PATH "/usr/bin/fido-roaming-agent-service"
49 #define FIDO_BLE_RAGENT_SVC_PATH "/usr/bin/fido-ble-ragent-service"
50 #define FIDO_BT_RAGENT_SVC_PATH "/usr/bin/fido-bt-ragent-service"
52 guint ClientListner::__ownerId = 0;
53 Fidoasm* ClientListner::__dbusObj = NULL;
55 ClientListner* ClientListner::__this = NULL;
57 #define RESP_NEG "{\"statusCode\":1}"
59 ClientListner::ClientListner(void) {}
61 ClientListner::~ClientListner(void) {}
64 ClientListner::getInstance(void)
69 /*TODO: thread-safe singleton*/
70 ClientListner *thisTemp = new ClientListner();
80 ClientListner::readProc(const char *path, char *buf, int size)
85 if (buf == NULL || path == NULL) {
86 _ERR("path and buffer is mandatory\n");
90 fd = open(path, O_RDONLY);
92 _ERR("fd open error(%d)\n", fd);
96 ret = read(fd, buf, size - 1);
98 _ERR("fd read error(%d)\n", fd);
111 ClientListner::getCallerId(GDBusMethodInvocation *invocation)
113 pid_t remote_pid = 0;
114 GError *error = NULL;
115 GDBusConnection *connection = NULL;
116 GVariant *response = NULL;
118 const gchar *sender = NULL;
120 sender = g_dbus_method_invocation_get_sender(invocation);
122 _ERR("Failed to get sender");
126 connection = g_dbus_method_invocation_get_connection(invocation);
127 if (connection == NULL) {
128 _ERR("Failed to open connection for the invocation");
133 response = g_dbus_connection_call_sync(connection,
134 _FREEDESKTOP_SERVICE, _FREEDESKTOP_PATH,
135 _FREEDESKTOP_INTERFACE, "GetConnectionUnixProcessID",
136 g_variant_new("(s)", sender), ((const GVariantType *) "(u)"),
137 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
140 if (response == NULL) {
141 _ERR("Failed to get caller id [%s]", error->message);
146 g_variant_get(response, "(u)", &upid);
147 _INFO("Remote msg-bus peer service=%s pid=%u", sender, upid);
148 remote_pid = (pid_t) upid;
150 g_variant_unref(response);
153 int ret = app_manager_get_app_id(remote_pid, &app_id);
155 if (app_id == NULL) {
156 _ERR("app_manager_get_app_id for %d failed = %d", remote_pid, ret);
158 /* Exception case : Daemons will not have app-ids, for them path will be set : /usr/bin/sample-service */
162 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", upid);
163 ret = readProc(buf, buf, sizeof(buf));
165 _ERR("No proc directory (%d)\n", upid);
169 _INFO("Caller=[%s]", buf);
171 app_id = strdup(buf);
175 std::string appIdStr(app_id, strlen(app_id));
180 #define ASM_RESP_ACCESS_DENIED "{\"statusCode\":2}"
181 #define ASM_RESP_DEREG "{\"statusCode\":0}"
184 ClientListner::onClientRequest(Fidoasm *object, GDBusMethodInvocation *invocation,
185 const gchar *asmReqJson)
189 /*TODO: Caller Id check*/
191 AsmRequest *req = NULL;
193 std::string callerId = FIDO_CLIENT_SVC_PATH;
195 _INFO("FIDO client request");
196 req = JsonUtil::parseAsmRequestJson(asmReqJson);
199 fidoasm_complete_asm_request(__dbusObj, invocation, -1, ASM_RESP_ACCESS_DENIED);
202 req->setCallerId(callerId);
204 AsmOp *operation = AsmOpFactory::createOperation(req);
205 if (operation == NULL) {
206 _ERR("AsmOp creation failed");
207 fidoasm_complete_asm_request(__dbusObj, invocation, -1, ASM_RESP_ACCESS_DENIED);
211 _INFO("Before operation exec ");
212 char *asmResp = operation->execute();
213 /*For Dereg request coming from Client, there should not be any indication about success/failure*/
214 if ((req->getRequesttype() == "Deregister")) {
216 fidoasm_complete_asm_request(__dbusObj, invocation, -1, ASM_RESP_DEREG);
219 if (asmResp == NULL) {
221 fidoasm_complete_asm_request(__dbusObj, invocation, -1, ASM_RESP_ACCESS_DENIED);
225 _INFO("After operation exec ");
226 _INFO("%s", asmResp);
227 fidoasm_complete_asm_request(__dbusObj, invocation, 0, asmResp);
234 ClientListner::onAgentRequest(Fidoasm *object, GDBusMethodInvocation *invocation,
235 const gchar *tlvReqB64)
239 /*TODO: Caller Id check*/
241 AsmRequest *req = NULL;
243 std::string callerId = FIDO_BT_RAGENT_SVC_PATH;
245 _INFO("Roaming Agent request");
246 if (RoamingUtil::isRASupported() == false) {
247 _ERR("RA feature not supported");
249 fidoasm_complete_ra_request(__dbusObj, invocation, -1, ASM_RESP_ACCESS_DENIED);
253 req = RoamingUtil::createAuthReq(tlvReqB64);
256 fidoasm_complete_ra_request(__dbusObj, invocation, -1, ASM_RESP_ACCESS_DENIED);
259 req->setCallerId(callerId);
262 AsmOp *operation = AsmOpFactory::createOperation(req);
263 if (operation == NULL) {
264 _ERR("AsmOp creation failed");
265 fidoasm_complete_ra_request(__dbusObj, invocation, -1, ASM_RESP_ACCESS_DENIED);
269 _INFO("Before operation exec ");
270 char *asmResp = operation->execute();
271 if (asmResp == NULL) {
273 fidoasm_complete_ra_request(__dbusObj, invocation, -1, ASM_RESP_ACCESS_DENIED);
277 _INFO("After operation exec ");
278 _INFO("%s", asmResp);
279 fidoasm_complete_asm_request(__dbusObj, invocation, 0, asmResp);
286 ClientListner::onTCUiResponse(Fidoasm *object, GDBusMethodInvocation *invocation,
287 const gchar *nonce, int result)
289 /*TODO: Allow only from org.tizen.asmui*/
290 TcUiAdaptor::OnTcResponseFromUi(nonce, result);
296 ClientListner::onAccountUiResponse(Fidoasm *object, GDBusMethodInvocation *invocation,
297 const gchar *nonce, int result, const gchar *account)
299 /*TODO: Allow only from org.tizen.asmui*/
300 AcUiAdaptor::OnAccountResponseFromUi(nonce, result, account);
306 ClientListner::onAuthUiResponse(Fidoasm *object, GDBusMethodInvocation *invocation,
307 int uiType, const gchar *nonce, int error,
310 /*TODO: Allow only from org.tizen.asmui*/
311 IAuthUiAdaptor *uiAd = AuthUiFactory::getAuthUiAdaptor((auth_ui_type_e)uiType);
312 RET_IF_FAIL(uiAd != NULL, false);
315 _INFO("Nonce received from to UI=[NULL]");
317 _INFO("Nonce received from to UI=[%s]", nonce);
319 uiAd->OnAuthResponseFromUi(nonce, token, error);
325 ClientListner::onBusAcquired(GDBusConnection *connection, const gchar *name, gpointer user_data)
329 GDBusInterfaceSkeleton* interface = NULL;
330 __dbusObj = fidoasm_skeleton_new();
331 if (__dbusObj == NULL) {
335 interface = G_DBUS_INTERFACE_SKELETON(__dbusObj);
336 if (!g_dbus_interface_skeleton_export(interface, connection, ASM_DBUS_PATH, NULL)) {
340 g_signal_connect(__dbusObj, "handle_asm_request",
341 G_CALLBACK(ClientListner::onClientRequest), NULL);
343 g_signal_connect(__dbusObj, "handle_ra_request",
344 G_CALLBACK(ClientListner::onAgentRequest), NULL);
346 g_signal_connect(__dbusObj, "handle_asm_ui_confirm_tc",
347 G_CALLBACK(ClientListner::onTCUiResponse), NULL);
349 g_signal_connect(__dbusObj, "handle_asm_ui_confirm_acc",
350 G_CALLBACK(ClientListner::onAccountUiResponse), NULL);
352 g_signal_connect(__dbusObj, "handle_auth_ui_result",
353 G_CALLBACK(ClientListner::onAuthUiResponse), NULL);
359 ClientListner::onNameAcquired(GDBusConnection *connection,
366 ClientListner::onNameLost(GDBusConnection *connection,
374 ClientListner::initDbus(void)
378 __ownerId = g_bus_own_name(G_BUS_TYPE_SYSTEM,
380 G_BUS_NAME_OWNER_FLAGS_NONE,
381 ClientListner::onBusAcquired,
382 ClientListner::onNameAcquired,
383 ClientListner::onNameAcquired,
388 if (__ownerId == 0) {
398 ClientListner::initialize(void)
400 #if !GLIB_CHECK_VERSION(2, 35, 0)
404 if (initDbus() == false) {
410 ClientListner::start(void)
417 GMainLoop *mainloop = NULL;
419 mainloop = g_main_loop_new(NULL, FALSE);
423 g_main_loop_run(mainloop);