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