Store the dbus connection while launching the service
[platform/core/context/context-service.git] / src / agent / legacy / access_control / PeerCreds.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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 #include <cynara-creds-gdbus.h>
18 #include <cynara-session.h>
19 #include <app_manager.h>
20 #include <package_manager.h>
21 #include <Types.h>
22 #include "PeerCreds.h"
23
24 ctx::Credentials::Credentials(char *pkgId, char *cli, char *sess, char *usr) :
25         packageId(pkgId),
26         client(cli),
27         session(sess),
28         user(usr)
29 {
30 }
31
32 ctx::Credentials::~Credentials()
33 {
34         g_free(packageId);
35         g_free(client);
36         g_free(session);
37         g_free(user);
38 }
39
40 bool ctx::peer_creds::get(GDBusConnection *connection, const char *uniqueName, const char *cookie, ctx::Credentials **creds)
41 {
42         pid_t pid = 0;
43         char *app_id = NULL;
44         char *packageId = NULL;
45         gchar *client = NULL;
46         char *session = NULL;
47         gchar *user = NULL;
48
49         int err = cynara_creds_gdbus_get_pid(connection, uniqueName, &pid);
50         IF_FAIL_RETURN_TAG(err == CYNARA_API_SUCCESS, false, _E, "Peer credentialing failed");
51
52         session = cynara_session_from_pid(pid);
53         IF_FAIL_CATCH_TAG(session, _E, "Peer credentialing failed");
54
55         err = cynara_creds_gdbus_get_client(connection, uniqueName, CLIENT_METHOD_DEFAULT, &client);
56         IF_FAIL_CATCH_TAG(err == CYNARA_API_SUCCESS, _E, "Peer credentialing failed");
57
58         err = cynara_creds_gdbus_get_user(connection, uniqueName, USER_METHOD_DEFAULT, &user);
59         IF_FAIL_CATCH_TAG(err == CYNARA_API_SUCCESS, _E, "Peer credentialing failed");
60
61         app_manager_get_app_id(pid, &app_id);
62         package_manager_get_package_id_by_app_id(app_id, &packageId);
63         _D("AppId: %s, PackageId: %s", app_id, packageId);
64         g_free(app_id);
65
66         *creds = new(std::nothrow) Credentials(packageId, client, session, user);
67         IF_FAIL_CATCH_TAG(*creds, _E, "Memory allocation failed");
68
69         return true;
70
71 CATCH:
72         g_free(packageId);
73         g_free(client);
74         g_free(session);
75         g_free(user);
76         return false;
77 }