Add WRT condition to apply smack label
[platform/core/appfw/ail.git] / tool / src / ail_fota.c
1 /*
2  * ail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22
23
24 #include <string.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <dirent.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <errno.h>
31
32 #include "ail.h"
33 #include "ail_private.h"
34
35 #define OWNER_ROOT 0
36 #define GROUP_MENU 6010
37 #define BUFSZE 1024
38 #define OPT_DESKTOP_DIRECTORY tzplatform_getenv(TZ_SYS_RW_DESKTOP_APP)
39 #define USR_DESKTOP_DIRECTORY tzplatform_getenv(TZ_SYS_RO_DESKTOP_APP)
40 #define APP_INFO_DB_FILE tzplatform_mkpath(TZ_SYS_DB,".app_info.db")
41 #define APP_INFO_DB_FILE_JOURNAL tzplatform_mkpath(TZ_SYS_DB,".app_info.db-journal")
42 #define APP_INFO_DB_LABEL "ail::db"
43
44 #ifdef _E
45 #undef _E
46 #endif
47 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
48
49 #ifdef _D
50 #undef _D
51 #endif
52 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
53
54 static int initdb_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
71         ret = ail_filter_count_appinfo(filter, &total);
72         if (ret != AIL_ERROR_OK) {
73                 ail_filter_destroy(filter);
74                 return -1;
75         }
76
77         ail_filter_destroy(filter);
78
79         return total;
80 }
81
82
83
84 char* _desktop_to_package(const char* desktop)
85 {
86         char *package, *tmp;
87
88         retv_if(!desktop, NULL);
89
90         package = strdup(desktop);
91         retv_if(!package, NULL);
92
93         tmp = strrchr(package, '.');
94         if(tmp == NULL) {
95                 _E("[%s] is not a desktop file", package);
96                 free(package);
97                 return NULL;
98         }
99
100         if (strcmp(tmp, ".desktop")) {
101                 _E("%s is not a desktop file", desktop);
102                 free(package);
103                 return NULL;
104         }
105
106         *tmp = '\0';
107
108         return package;
109 }
110
111
112
113 int initdb_load_directory(const char *directory)
114 {
115         DIR *dir;
116         struct dirent entry, *result;
117         int len, ret;
118         char buf[BUFSZE];
119         int total_cnt = 0;
120         int ok_cnt = 0;
121
122         // desktop file
123         dir = opendir(directory);
124         if (!dir) {
125                 if (strerror_r(errno, buf, sizeof(buf)) == 0)
126                         _E("Failed to access the [%s] because %s\n", directory, buf);
127                 return AIL_ERROR_FAIL;
128         }
129
130         len = strlen(directory) + 1;
131         _D("Loading desktop files from %s", directory);
132
133         for (ret = readdir_r(dir, &entry, &result);
134                         ret == 0 && result != NULL;
135                         ret = readdir_r(dir, &entry, &result)) {
136                 char *package;
137
138                 if (entry.d_name[0] == '.') continue;
139                 total_cnt++;
140                 package = _desktop_to_package(entry.d_name);
141                 if (!package) {
142                         _E("Failed to convert file to package[%s]", entry.d_name);
143                         continue;
144                 }
145
146                 if (ail_desktop_fota(package) != AIL_ERROR_OK) {
147                         _E("Failed to add a package[%s]", package);
148                 } else {
149                         ok_cnt++;
150                 }
151                 free(package);
152         }
153
154         _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt);
155         closedir(dir);
156
157         return AIL_ERROR_OK;
158 }
159
160
161
162 static int initdb_change_perm(const char *db_file)
163 {
164         char buf[BUFSZE];
165         char journal_file[BUFSZE];
166         char *files[3];
167         int ret, i;
168
169         files[0] = (char *)db_file;
170         files[1] = journal_file;
171         files[2] = NULL;
172
173         retv_if(!db_file, AIL_ERROR_FAIL);
174
175         snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
176
177         for (i = 0; files[i]; i++) {
178                 ret = chown(files[i], OWNER_ROOT, OWNER_ROOT);
179                 if (ret == -1) {
180                         strerror_r(errno, buf, sizeof(buf));
181                         _E("FAIL : chown %s %d.%d, because %s", db_file, OWNER_ROOT, OWNER_ROOT, buf);
182                         return AIL_ERROR_FAIL;
183                 }
184
185                 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
186                 if (ret == -1) {
187                         strerror_r(errno, buf, sizeof(buf));
188                         _E("FAIL : chmod %s 0664, because %s", db_file, buf);
189                         return AIL_ERROR_FAIL;
190                 }
191         }
192
193         return AIL_ERROR_OK;
194 }
195
196
197 static int __is_authorized()
198 {
199         /* ail_init db should be called by as root privilege. */
200
201         uid_t uid = getuid();
202         if ((uid_t) 0 == uid)
203                 return 1;
204         else
205                 return 0;
206 }
207
208 int xsystem(const char *argv[])
209 {
210         int status = 0;
211         pid_t pid;
212         pid = fork();
213         switch (pid) {
214         case -1:
215                 perror("fork failed");
216                 return -1;
217         case 0:
218                 /* child */
219                 execvp(argv[0], (char *const *)argv);
220                 _exit(-1);
221         default:
222                 /* parent */
223                 break;
224         }
225         if (waitpid(pid, &status, 0) == -1) {
226                 perror("waitpid failed");
227                 return -1;
228         }
229         if (WIFSIGNALED(status)) {
230                 perror("signal");
231                 return -1;
232         }
233         if (!WIFEXITED(status)) {
234                 /* shouldn't happen */
235                 perror("should not happen");
236                 return -1;
237         }
238         return WEXITSTATUS(status);
239 }
240
241 int main(int argc, char *argv[])
242 {
243         int ret;
244
245         if (!__is_authorized()) {
246                 fprintf(stderr, "You are not an authorized user!\n");
247                 _D("You are not an authorized user!\n");
248                 return AIL_ERROR_FAIL;
249         }
250
251         const char *argv_rm[] = { "/bin/rm", APP_INFO_DB_FILE, NULL };
252         xsystem(argv_rm);
253         const char *argv_rmjn[] = { "/bin/rm", APP_INFO_DB_FILE_JOURNAL, NULL };
254         xsystem(argv_rmjn);
255
256         ret = setenv("AIL_INITDB", "1", 1);
257         _D("AIL_INITDB : %d", ret);
258
259         ret = initdb_count_app();
260         if (ret > 0) {
261                 _D("Some Apps in the App Info DB.");
262         }
263
264         ret = initdb_load_directory(OPT_DESKTOP_DIRECTORY);
265         if (ret == AIL_ERROR_FAIL) {
266                 _E("cannot load opt desktop directory.");
267         }
268
269         ret = initdb_load_directory(USR_DESKTOP_DIRECTORY);
270         if (ret == AIL_ERROR_FAIL) {
271                 _E("cannot load usr desktop directory.");
272         }
273
274         ret = initdb_change_perm(APP_INFO_DB_FILE);
275         if (ret == AIL_ERROR_FAIL) {
276                 _E("cannot chown.");
277         }
278
279 #ifdef WRT_SMACK_ENABLED
280         const char *argv_smack[] = { "/usr/bin/chsmack", "-a", APP_INFO_DB_LABEL, APP_INFO_DB_FILE, NULL };
281         xsystem(argv_smack);
282         const char *argv_smackjn[] = { "/usr/bin/chsmack", "-a", APP_INFO_DB_LABEL, APP_INFO_DB_FILE_JOURNAL, NULL };
283         xsystem(argv_smackjn);
284 #endif
285         return AIL_ERROR_OK;
286 }
287
288
289
290 // END