Setting smack labels to User with vconftool option
[platform/core/appfw/ail.git] / tool / src / syncdb_user.c
1 /*
2  * ail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  * Copyright (C) 2013-2014 Intel Corporation.
6  *
7  * Contact: Sabera Djelti <sabera.djelti@open.eurogiciel.org>,
8  * Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24
25
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <dirent.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #include <errno.h>
33 #include <sys/smack.h>
34
35 #include "ail.h"
36 #include "ail_db.h"
37 #include "ail_private.h"
38
39
40 #ifdef _E
41 #undef _E
42 #endif
43 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
44
45 #ifdef _D
46 #undef _D
47 #endif
48 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
49
50 #define SET_DEFAULT_LABEL(x) \
51         if(smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) _E("failed chsmack -a \"*\" %s", x) \
52         else _D("chsmack -a \"*\" %s", x)
53
54 static int syncdb_user_count_app(void)
55 {
56         ail_filter_h filter;
57         ail_error_e ret;
58         int total = 0;
59
60         ret = ail_filter_new(&filter);
61         if (ret != AIL_ERROR_OK) {
62                 return -1;
63         }
64
65         ret = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false);
66         if (ret != AIL_ERROR_OK) {
67                 ail_filter_destroy(filter);
68                 return -1;
69         }
70         ret = ail_filter_count_usr_appinfo(filter, &total, getuid());
71         if (ret != AIL_ERROR_OK) {
72                 ail_filter_destroy(filter);
73                 return -1;
74         }
75
76         ail_filter_destroy(filter);
77
78         return total;
79 }
80
81
82
83 char* _desktop_to_package(const char* desktop)
84 {
85         char *package, *tmp;
86
87         retv_if(!desktop, NULL);
88
89         package = strdup(desktop);
90         retv_if(!package, NULL);
91
92         tmp = strrchr(package, '.');
93         if(tmp == NULL) {
94                 _E("[%s] is not a desktop file", package);
95                 free(package);
96                 return NULL;
97         }
98
99         if (strcmp(tmp, ".desktop")) {
100                 _E("%s is not a desktop file", desktop);
101                 free(package);
102                 return NULL;
103         }
104
105         *tmp = '\0';
106
107         return package;
108 }
109
110
111
112 int syncdb_user_load_directory(const char *directory)
113 {
114         DIR *dir;
115         struct dirent entry, *result;
116         int len, ret;
117         char buf[BUFSZE];
118         int total_cnt = 0;
119         int ok_cnt = 0;
120
121         // desktop file
122         dir = opendir(directory);
123         if (!dir) {
124                 if (strerror_r(errno, buf, sizeof(buf)) == 0)
125                         _E("Failed to access the [%s] because %s\n", directory, buf);
126                 return AIL_ERROR_FAIL;
127         }
128
129         len = strlen(directory) + 1;
130         _D("Loading desktop files from %s", directory);
131
132         for (ret = readdir_r(dir, &entry, &result);
133                         ret == 0 && result != NULL;
134                         ret = readdir_r(dir, &entry, &result)) {
135                 char *package;
136
137                 if (entry.d_name[0] == '.') continue;
138                 total_cnt++;
139                 package = _desktop_to_package(entry.d_name);
140                 if (!package) {
141                         _E("Failed to convert file to package[%s]", entry.d_name);
142                         continue;
143                 }
144
145                 if (ail_usr_desktop_add(package, getuid()) != AIL_ERROR_OK) {
146                         _E("Failed to add a package[%s]", package);
147                 } else {
148                         ok_cnt++;
149                 }
150                 free(package);
151         }
152
153         _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt);
154         closedir(dir);
155
156         return AIL_ERROR_OK;
157 }
158
159 static int __is_authorized()
160 {
161         /* ail_init db should be called by an user. */
162
163         uid_t uid = getuid();
164         if ((uid_t) OWNER_ROOT != uid)
165                 return 1;
166         else
167                 return 0;
168 }
169
170
171 int main(int argc, char *argv[])
172 {
173         int ret;
174
175         if (!__is_authorized()) {
176                 fprintf(stderr, "You are not an authorized user!\n");
177                 _D("You are root user! Please switch to a regular user\n");
178                 return -1;
179         }
180         if (access(ail_get_app_DB(getuid()), F_OK)) {
181                 fprintf(stderr, "Application database is missing, please use ail_createdb to create one before\n");
182                 return AIL_ERROR_FAIL;
183         }
184         ret = setenv("AIL_INITDB", "1", 1);
185         _D("AIL_INITDB : %d", ret);
186         ret = syncdb_user_count_app();
187         if (ret > 0) {
188                 _D("Some Apps in the App Info DB.");
189         }
190
191         ret = syncdb_user_load_directory(ail_get_desktop_path(getuid()));
192         if (ret == AIL_ERROR_FAIL) {
193                 _E("cannot load usr desktop directory.");
194         }
195
196         return AIL_ERROR_OK;
197 }
198
199
200
201 // END