fix TC-1516
[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 #ifdef _E
36 #undef _E
37 #endif
38 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
39
40 #ifdef _D
41 #undef _D
42 #endif
43 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
44
45 static int initdb_count_app(uid_t uid)
46 {
47         ail_filter_h filter;
48         ail_error_e ret;
49         int total = 0;
50
51         ret = ail_filter_new(&filter);
52         if (ret != AIL_ERROR_OK) {
53                 return -1;
54         }
55
56         ret = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false);
57         if (ret != AIL_ERROR_OK) {
58                 ail_filter_destroy(filter);
59                 return -1;
60         }
61 //__isadmin
62         if (uid != GLOBAL_USER)
63           ret = ail_filter_count_usr_appinfo(filter,  &total, uid);
64         else
65           ret = ail_filter_count_appinfo(filter,  &total);
66         if (ret != AIL_ERROR_OK) {
67                 ail_filter_destroy(filter);
68                 return -1;
69         }
70
71         ail_filter_destroy(filter);
72
73         return total;
74 }
75
76
77
78 char* _desktop_to_package(const char* desktop)
79 {
80         char *package, *tmp;
81
82         retv_if(!desktop, NULL);
83
84         package = strdup(desktop);
85         retv_if(!package, NULL);
86
87         tmp = strrchr(package, '.');
88         if(tmp == NULL) {
89                 _E("[%s] is not a desktop file", package);
90                 free(package);
91                 return NULL;
92         }
93
94         if (strcmp(tmp, ".desktop")) {
95                 _E("%s is not a desktop file", desktop);
96                 free(package);
97                 return NULL;
98         }
99
100         *tmp = '\0';
101
102         return package;
103 }
104
105
106
107 int initdb_load_directory(const char *directory)
108 {
109         DIR *dir;
110         struct dirent entry, *result;
111         int len, ret;
112         char buf[BUFSZE];
113         int total_cnt = 0;
114         int ok_cnt = 0;
115
116         // desktop file
117         dir = opendir(directory);
118         if (!dir) {
119                 if (strerror_r(errno, buf, sizeof(buf)) == 0)
120                         _E("Failed to access the [%s] because %s\n", directory, buf);
121                 return AIL_ERROR_FAIL;
122         }
123
124         len = strlen(directory) + 1;
125         _D("Loading desktop files from %s", directory);
126
127         for (ret = readdir_r(dir, &entry, &result);
128                         ret == 0 && result != NULL;
129                         ret = readdir_r(dir, &entry, &result)) {
130                 char *package;
131
132                 if (entry.d_name[0] == '.') continue;
133                 total_cnt++;
134                 package = _desktop_to_package(entry.d_name);
135                 if (!package) {
136                         _E("Failed to convert file to package[%s]", entry.d_name);
137                         continue;
138                 }
139
140                 if (ail_desktop_fota(package) != AIL_ERROR_OK) {
141                         _E("Failed to add a package[%s]", package);
142                 } else {
143                         ok_cnt++;
144                 }
145                 free(package);
146         }
147
148         _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt);
149         closedir(dir);
150
151         return AIL_ERROR_OK;
152 }
153
154
155
156 static int initdb_change_perm(const char *db_file)
157 {
158         char buf[BUFSZE];
159         char journal_file[BUFSZE];
160         char *files[3];
161         int ret, i;
162
163         files[0] = (char *)db_file;
164         files[1] = journal_file;
165         files[2] = NULL;
166
167         retv_if(!db_file, AIL_ERROR_FAIL);
168
169         snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
170
171         for (i = 0; files[i]; i++) {
172                 ret = chown(files[i], OWNER_ROOT, OWNER_ROOT);
173                 if (ret == -1) {
174                         strerror_r(errno, buf, sizeof(buf));
175                         _E("FAIL : chown %s %d.%d, because %s", db_file, OWNER_ROOT, OWNER_ROOT, buf);
176                         return AIL_ERROR_FAIL;
177                 }
178
179                 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
180                 if (ret == -1) {
181                         strerror_r(errno, buf, sizeof(buf));
182                         _E("FAIL : chmod %s 0664, because %s", db_file, buf);
183                         return AIL_ERROR_FAIL;
184                 }
185         }
186
187         return AIL_ERROR_OK;
188 }
189
190
191 static int __is_authorized()
192 {
193         /* ail_init db should be called by as root privilege. */
194
195         uid_t uid = getuid();
196         if ((uid_t) 0 == uid)
197                 return 1;
198         else
199                 return 0;
200 }
201
202 int xsystem(const char *argv[])
203 {
204         int status = 0;
205         pid_t pid;
206         pid = fork();
207         switch (pid) {
208         case -1:
209                 perror("fork failed");
210                 return -1;
211         case 0:
212                 /* child */
213                 execvp(argv[0], (char *const *)argv);
214                 _exit(-1);
215         default:
216                 /* parent */
217                 break;
218         }
219         if (waitpid(pid, &status, 0) == -1) {
220                 perror("waitpid failed");
221                 return -1;
222         }
223         if (WIFSIGNALED(status)) {
224                 perror("signal");
225                 return -1;
226         }
227         if (!WIFEXITED(status)) {
228                 /* shouldn't happen */
229                 perror("should not happen");
230                 return -1;
231         }
232         return WEXITSTATUS(status);
233 }
234
235 int main(int argc, char *argv[])
236 {
237         int ret;
238
239         if (!__is_authorized()) {
240                 fprintf(stderr, "You are not an authorized user!\n");
241                 _D("You are not root user!\n");
242     }
243     else {
244         const char *argv_rm[] = { "/bin/rm", APP_INFO_DB_FILE, NULL };
245         xsystem(argv_rm);
246         const char *argv_rmjn[] = { "/bin/rm", APP_INFO_DB_FILE_JOURNAL, NULL };
247         xsystem(argv_rmjn);
248     }
249         ret = setenv("AIL_INITDB", "1", 1);
250         _D("AIL_INITDB : %d", ret);
251
252         ret = initdb_count_app(getuid());
253         if (ret > 0) {
254                 _D("Some Apps in the App Info DB.");
255         }
256
257         ret = initdb_load_directory(OPT_DESKTOP_DIRECTORY);
258         if (ret == AIL_ERROR_FAIL) {
259                 _E("cannot load opt desktop directory.");
260         }
261
262         ret = initdb_load_directory(USR_DESKTOP_DIRECTORY);
263         if (ret == AIL_ERROR_FAIL) {
264                 _E("cannot load usr desktop directory.");
265         }
266
267         if (__is_authorized()) {
268                 ret = initdb_change_perm(APP_INFO_DB_FILE);
269                 if (ret == AIL_ERROR_FAIL) {
270                         _E("cannot chown.");
271                 }
272                 const char *argv_smack[] = { "/usr/bin/chsmack", "-a", APP_INFO_DB_LABEL, APP_INFO_DB_FILE, NULL };
273                 xsystem(argv_smack);
274                 const char *argv_smackjn[] = { "/usr/bin/chsmack", "-a", APP_INFO_DB_LABEL, APP_INFO_DB_FILE_JOURNAL, NULL };
275                 xsystem(argv_smackjn);
276         }
277         return AIL_ERROR_OK;
278 }
279
280
281
282 // END