1038a0d38644a2cc4074369a927ad12c87711722
[platform/core/appfw/ail.git] / tool / src / syncdb_user.c
1 /*
2  * ail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  * Copyright (C) 2013-2014 Intel Corporation.
6  *
7  * Contact: Sabera Djelti <sabera.djelti@open.eurogiciel.org>,
8  * Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
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_db.h"
35 #include "ail_private.h"
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         do { \
49                 if (smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) \
50                         _E("failed chsmack -a \"*\" %s", x); \
51                 else \
52                         _D("chsmack -a \"*\" %s", x); \
53         } while (0)
54
55 static int syncdb_user_count_app(void)
56 {
57         ail_filter_h filter;
58         ail_error_e ret;
59         int total = 0;
60
61         ret = ail_filter_new(&filter);
62         if (ret != AIL_ERROR_OK)
63                 return -1;
64
65         ret = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false);
66         if (ret != AIL_ERROR_OK) {
67                 ail_filter_destroy(filter);
68                 return -1;
69         }
70         ret = ail_filter_count_usr_appinfo(filter, &total, getuid());
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 char *_desktop_to_package(const char* desktop)
82 {
83         char *package;
84         char *tmp;
85
86         retv_if(!desktop, NULL);
87
88         package = strdup(desktop);
89         retv_if(!package, NULL);
90
91         tmp = strrchr(package, '.');
92         if (tmp == NULL) {
93                 _E("[%s] is not a desktop file", package);
94                 free(package);
95                 return NULL;
96         }
97
98         if (strcmp(tmp, ".desktop")) {
99                 _E("%s is not a desktop file", desktop);
100                 free(package);
101                 return NULL;
102         }
103
104         *tmp = '\0';
105
106         return package;
107 }
108
109
110
111 int syncdb_user_load_directory(const char *directory)
112 {
113         DIR *dir;
114         struct dirent entry, *result;
115         int ret;
116         char buf[BUFSZE];
117         int total_cnt = 0;
118         int ok_cnt = 0;
119
120         /* desktop file */
121         dir = opendir(directory);
122         if (!dir) {
123                 if (strerror_r(errno, buf, sizeof(buf)) == 0)
124                         _E("Failed to access the [%s] because %s\n", directory, buf);
125                 return AIL_ERROR_FAIL;
126         }
127
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] == '.')
136                         continue;
137                 total_cnt++;
138                 package = _desktop_to_package(entry.d_name);
139                 if (!package) {
140                         _E("Failed to convert file to package[%s]", entry.d_name);
141                         continue;
142                 }
143
144                 if (ail_usr_desktop_add(package, getuid()) != AIL_ERROR_OK)
145                         _E("Failed to add a package[%s]", package);
146                 else
147                         ok_cnt++;
148
149                 free(package);
150         }
151
152         _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt);
153         closedir(dir);
154
155         return AIL_ERROR_OK;
156 }
157
158 static int __is_authorized()
159 {
160         /* ail_init db should be called by an user. */
161         uid_t uid = getuid();
162         if ((uid_t) OWNER_ROOT != uid)
163                 return 1;
164         else
165                 return 0;
166 }
167
168 int main(int argc, char *argv[])
169 {
170         int ret;
171         char *db;
172         char *path;
173
174         if (!__is_authorized()) {
175                 fprintf(stderr, "You are not an authorized user!\n");
176                 _D("You are root user! Please switch to a regular user\n");
177                 return -1;
178         }
179
180         db = ail_get_app_DB(getuid());
181         if (db == NULL) {
182                 fprintf(stderr, "Failed to get DB");
183                 _E("Failed to get DB");
184                 return -1;
185         }
186
187         if (access(db, F_OK)) {
188                 fprintf(stderr, "Application database is missing, please use ail_createdb to create one before\n");
189                 free(db);
190                 return AIL_ERROR_FAIL;
191         }
192
193         ret = setenv("AIL_INITDB", "1", 1);
194         _D("AIL_INITDB : %d", ret);
195
196         ret = syncdb_user_count_app();
197         if (ret > 0)
198                 _D("Some Apps in the App Info DB.");
199
200         path = ail_get_desktop_path(getuid());
201         if (path == NULL) {
202                 _E("Failed to get desktop path");
203                 free(db);
204                 return AIL_ERROR_FAIL;
205         }
206
207         ret = syncdb_user_load_directory(path);
208         if (ret == AIL_ERROR_FAIL)
209                 _E("cannot load usr desktop directory.");
210
211         free(path);
212         free(db);
213
214         return AIL_ERROR_OK;
215 }