20d17c58cf10e83e1a130e16372ab70aa1783eec
[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 #include <sys/smack.h>
32
33 #include "ail.h"
34 #include "ail_private.h"
35
36 #ifdef _E
37 #undef _E
38 #endif
39 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
40
41 #ifdef _D
42 #undef _D
43 #endif
44 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
45
46 #define SET_DEFAULT_LABEL(x) \
47         if(smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) _E("failed chsmack -a \"*\" %s", x) \
48         else _D("chsmack -a \"*\" %s", x)
49
50 static int initdb_count_app(uid_t uid)
51 {
52         ail_filter_h filter;
53         ail_error_e ret;
54         int total = 0;
55
56         ret = ail_filter_new(&filter);
57         if (ret != AIL_ERROR_OK) {
58                 return -1;
59         }
60
61         ret = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false);
62         if (ret != AIL_ERROR_OK) {
63                 ail_filter_destroy(filter);
64                 return -1;
65         }
66 //__isadmin
67         if (uid != GLOBAL_USER)
68           ret = ail_filter_count_usr_appinfo(filter,  &total, uid);
69         else
70           ret = ail_filter_count_appinfo(filter,  &total);
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 initdb_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_desktop_fota(package) != 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
160
161 static int initdb_change_perm(const char *db_file)
162 {
163         char buf[BUFSZE];
164         char journal_file[BUFSZE];
165         char *files[3];
166         int ret, i;
167
168         files[0] = (char *)db_file;
169         files[1] = journal_file;
170         files[2] = NULL;
171
172         retv_if(!db_file, AIL_ERROR_FAIL);
173
174         snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
175
176         for (i = 0; files[i]; i++) {
177                 ret = chown(files[i], OWNER_ROOT, OWNER_ROOT);
178                 if (ret == -1) {
179                         strerror_r(errno, buf, sizeof(buf));
180                         _E("FAIL : chown %s %d.%d, because %s", db_file, OWNER_ROOT, OWNER_ROOT, buf);
181                         return AIL_ERROR_FAIL;
182                 }
183
184                 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
185                 if (ret == -1) {
186                         strerror_r(errno, buf, sizeof(buf));
187                         _E("FAIL : chmod %s 0664, because %s", db_file, buf);
188                         return AIL_ERROR_FAIL;
189                 }
190         }
191
192         return AIL_ERROR_OK;
193 }
194
195
196 static int __is_authorized()
197 {
198         /* ail_init db should be called by as root privilege. */
199
200         uid_t uid = getuid();
201         uid_t euid = geteuid();
202         //euid need to be root to allow smack label changes during initialization
203         if (((uid_t) GLOBAL_USER == uid) && (euid == OWNER_ROOT) )
204                 return 1;
205         else
206                 return 0;
207 }
208
209 int xsystem(const char *argv[])
210 {
211         int status = 0;
212         pid_t pid;
213         pid = fork();
214         switch (pid) {
215         case -1:
216                 perror("fork failed");
217                 return -1;
218         case 0:
219                 /* child */
220                 execvp(argv[0], (char *const *)argv);
221                 _exit(-1);
222         default:
223                 /* parent */
224                 break;
225         }
226         if (waitpid(pid, &status, 0) == -1) {
227                 perror("waitpid failed");
228                 return -1;
229         }
230         if (WIFSIGNALED(status)) {
231                 perror("signal");
232                 return -1;
233         }
234         if (!WIFEXITED(status)) {
235                 /* shouldn't happen */
236                 perror("should not happen");
237                 return -1;
238         }
239         return WEXITSTATUS(status);
240 }
241
242 int main(int argc, char *argv[])
243 {
244         int ret;
245
246         if (!__is_authorized()) {
247                 fprintf(stderr, "You are not an authorized user!\n");
248                 _D("You are not root user!\n");
249         }
250         else {
251                 if(remove(APP_INFO_DB_FILE))
252                         _E(" %s is not removed",APP_INFO_DB_FILE);
253                 if(remove(APP_INFO_DB_FILE_JOURNAL))
254                         _E(" %s is not removed",APP_INFO_DB_FILE_JOURNAL);
255         }
256         ret = setenv("AIL_INITDB", "1", 1);
257         _D("AIL_INITDB : %d", ret);
258
259         ret = initdb_count_app(getuid());
260         if (ret > 0) {
261                 _D("Some Apps in the App Info DB.");
262         }
263
264         ret = initdb_load_directory(USR_DESKTOP_DIRECTORY);
265         if (ret == AIL_ERROR_FAIL) {
266                 _E("cannot load usr desktop directory.");
267         }
268
269         if (__is_authorized()) {
270                 ret = initdb_change_perm(APP_INFO_DB_FILE);
271                 if (ret == AIL_ERROR_FAIL) {
272                         _E("cannot chown.");
273                 }
274                 SET_DEFAULT_LABEL(APP_INFO_DB_FILE);
275                 SET_DEFAULT_LABEL(APP_INFO_DB_FILE_JOURNAL);
276         }
277         return AIL_ERROR_OK;
278 }
279
280
281
282 // END