23efdc8d0ef62314ecc66ee9c43577f2cd69a15d
[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         if(smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) _E("failed chsmack -a \"*\" %s", x) \
49         else _D("chsmack -a \"*\" %s", x)
50
51 static int syncdb_user_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_usr_appinfo(filter, &total, getuid());
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 char* _desktop_to_package(const char* desktop)
79 {
80         char *package;
81         char *tmp;
82
83         retv_if(!desktop, NULL);
84
85         package = strdup(desktop);
86         retv_if(!package, NULL);
87
88         tmp = strrchr(package, '.');
89         if(tmp == NULL) {
90                 _E("[%s] is not a desktop file", package);
91                 free(package);
92                 return NULL;
93         }
94
95         if (strcmp(tmp, ".desktop")) {
96                 _E("%s is not a desktop file", desktop);
97                 free(package);
98                 return NULL;
99         }
100
101         *tmp = '\0';
102
103         return package;
104 }
105
106
107
108 int syncdb_user_load_directory(const char *directory)
109 {
110         DIR *dir;
111         struct dirent entry, *result;
112         int len, ret;
113         char buf[BUFSZE];
114         int total_cnt = 0;
115         int ok_cnt = 0;
116
117         /*  desktop file */
118         dir = opendir(directory);
119         if (!dir) {
120                 if (strerror_r(errno, buf, sizeof(buf)) == 0)
121                         _E("Failed to access the [%s] because %s\n", directory, buf);
122                 return AIL_ERROR_FAIL;
123         }
124
125         len = strlen(directory) + 1;
126         _D("Loading desktop files from %s", directory);
127
128         for (ret = readdir_r(dir, &entry, &result);
129                         ret == 0 && result != NULL;
130                         ret = readdir_r(dir, &entry, &result)) {
131                 char *package;
132
133                 if (entry.d_name[0] == '.') continue;
134                 total_cnt++;
135                 package = _desktop_to_package(entry.d_name);
136                 if (!package) {
137                         _E("Failed to convert file to package[%s]", entry.d_name);
138                         continue;
139                 }
140
141                 if (ail_usr_desktop_add(package, getuid()) != AIL_ERROR_OK) {
142                         _E("Failed to add a package[%s]", package);
143                 } else {
144                         ok_cnt++;
145                 }
146                 free(package);
147         }
148
149         _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt);
150         closedir(dir);
151
152         return AIL_ERROR_OK;
153 }
154
155 static int __is_authorized()
156 {
157         /* ail_init db should be called by an user. */
158         uid_t uid = getuid();
159         if ((uid_t) OWNER_ROOT != uid)
160                 return 1;
161         else
162                 return 0;
163 }
164
165 int main(int argc, char *argv[])
166 {
167         int ret;
168         char *db;
169         char *path;
170
171         if (!__is_authorized()) {
172                 fprintf(stderr, "You are not an authorized user!\n");
173                 _D("You are root user! Please switch to a regular user\n");
174                 return -1;
175         }
176
177         db = ail_get_app_DB(getuid());
178         if (db == NULL) {
179                 fprintf(stderr, "Failed to get DB");
180                 _E("Failed to get DB");
181                 return -1;
182         }
183
184         if (access(db, F_OK)) {
185                 fprintf(stderr, "Application database is missing, please use ail_createdb to create one before\n");
186                 free(db);
187                 return AIL_ERROR_FAIL;
188         }
189
190         ret = setenv("AIL_INITDB", "1", 1);
191         _D("AIL_INITDB : %d", ret);
192
193         ret = syncdb_user_count_app();
194         if (ret > 0)
195                 _D("Some Apps in the App Info DB.");
196
197         path = ail_get_desktop_path(getuid());
198         if (path == NULL) {
199                 _E("Failed to get desktop path");
200                 free(db);
201                 return AIL_ERROR_FAIL;
202         }
203
204         ret = syncdb_user_load_directory(path);
205         if (ret == AIL_ERROR_FAIL)
206                 _E("cannot load usr desktop directory.");
207
208         free(path);
209         free(db);
210
211         return AIL_ERROR_OK;
212 }