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