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