Fixed invalid smack label for cynara check
[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] = {0, };
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                 int ch = 0;
56                 int idx = 0;
57                 while (EOF != (ch = fgetc(fp))) {
58                         smack_label[idx] = ch;
59                         idx++;
60                 }
61                 fclose(fp);
62         }
63
64         pid_t pid = getpid();
65         session = cynara_session_from_pid(pid);
66         snprintf(uid, 16, "%d", getuid());
67         ret = cynara_check(cynara, smack_label, session, uid, privilege_name);
68
69         if (session) {
70                 free(session);
71         }
72
73         if (cynara) {
74                 cynara_finish(cynara);
75         }
76
77         if (ret != CYNARA_API_ACCESS_ALLOWED) {
78                 LOCATION_LOGE("cynara_check failed [%d]", ret);
79                 return LOCATION_ERROR_NOT_ALLOWED;
80         }
81
82         return LOCATION_ERROR_NONE;
83 }