ec3eedeb8a0984dbadfe8d2ab8a88de99f08deef
[platform/core/location/lbs-location.git] / location / manager / location-privacy.c
1 /*
2  * libslp-location
3  *
4  * Copyright (c) 2010-2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngae Kang <youngae.kang@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
7  *          Genie Kim <daejins.kim@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <glib.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include <app_manager.h>
29 #include <cynara-client.h>
30 #include <cynara-session.h>
31
32 #include "location-common-util.h"
33 #include "location-types.h"
34 #include "location-log.h"
35 #include "location-privacy.h"
36
37 int location_check_cynara(const char *privilege_name)
38 {
39         cynara *cynara = NULL;
40         int ret = LOCATION_ERROR_NONE;
41         FILE *fp = NULL;
42         char uid[16];
43         char *session = NULL;
44         char smack_label[100] = "/proc/self/attr/current";
45
46         if (cynara_initialize(&cynara, NULL) != CYNARA_API_SUCCESS)
47         {
48                 LOCATION_LOGE("cynara initialize failed");
49                 cynara = NULL;
50                 return LOCATION_ERROR_NOT_ALLOWED;
51         }
52
53         fp = fopen("/proc/self/attr/current", "r");
54         if (fp != NULL) {
55                 if (fread(smack_label, 1, sizeof(smack_label), fp) <= 0) {
56                         LOCATION_LOGE("fread failed");
57                 }
58                 fclose(fp);
59         }
60
61         pid_t pid = getpid();
62         session = cynara_session_from_pid(pid);
63         snprintf(uid, 16, "%d", getuid());
64         ret = cynara_check(cynara, smack_label, session, uid, privilege_name);
65
66         if (session) {
67                 free(session);
68         }
69
70         if (cynara) {
71                 cynara_finish(cynara);
72         }
73
74         if (ret != CYNARA_API_ACCESS_ALLOWED) {
75                 LOCATION_LOGE("cynara_check failed [%d]", ret);
76                 return LOCATION_ERROR_NOT_ALLOWED;
77         }
78
79         return LOCATION_ERROR_NONE;
80 }