Fix static analysis tool issue
[platform/core/appfw/app2sd.git] / plugin / app2sd / server / app2sd_internals_registry.c
1 /*
2  * app2ext
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Garima Shrivastava<garima.s@samsung.com>
7  *      Jyotsna Dhumale <jyotsna.a@samsung.com>
8  *      Venkatesha Sarpangala <sarpangala.v@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 <dirent.h>
25 #include <time.h>
26 #include <db-util.h>
27
28 #include "app2sd_internals.h"
29
30 #define MAX_QUERY_LEN 4096
31 #define PASSWORD_LENGTH 64
32 /*
33 ########### Internal APIs ##################
34  */
35
36 /*sqlite  db code*/
37 #define APP2SD_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".app2sd.db")
38 sqlite3 *app2sd_db;
39 #define QUERY_CREATE_TABLE_APP2SD "CREATE TABLE IF NOT EXISTS app2sd_info " \
40         "(pkgid TEXT NOT NULL, password TEXT NOT NULL, " \
41         "filename TEXT NOT NULL, uid INTEGER, PRIMARY KEY(pkgid, uid))"
42
43 int _app2sd_initialize_db()
44 {
45         char *error_message = NULL;
46         int ret;
47         FILE *fp = NULL;
48
49         fp = fopen(APP2SD_DB_FILE, "r");
50         if (fp != NULL) {
51                 fclose(fp);
52                 ret = db_util_open(APP2SD_DB_FILE, &app2sd_db,
53                         DB_UTIL_REGISTER_HOOK_METHOD);
54
55                 if (ret != SQLITE_OK) {
56                         _E("connect menu_db [%s] failed",
57                                 APP2SD_DB_FILE);
58                         return -1;
59                 }
60                 return 0;
61         }
62
63         ret = db_util_open(APP2SD_DB_FILE, &app2sd_db,
64                  DB_UTIL_REGISTER_HOOK_METHOD);
65
66         if (ret != SQLITE_OK) {
67                 _E("connect menu_db [%s] failed",
68                         APP2SD_DB_FILE);
69                 return -1;
70         }
71
72         if (SQLITE_OK != sqlite3_exec(app2sd_db,
73                 QUERY_CREATE_TABLE_APP2SD, NULL, NULL,
74                 &error_message)) {
75                 _E("don't execute query = (%s), " \
76                         "error message = (%s)",
77                         QUERY_CREATE_TABLE_APP2SD, error_message);
78                 return -1;
79         }
80
81         return 0;
82 }
83
84 static int _app2sd_check_existing_info(const char *pkgid, uid_t uid)
85 {
86         char *query = NULL;
87         const char *val = NULL;
88         sqlite3_stmt *stmt = NULL;
89         int ret = 0;
90
91         query = sqlite3_mprintf("select count(*) from app2sd_info " \
92                 "where pkgid=%Q and uid=%d", pkgid, uid);
93         if (query == NULL) {
94                 _E("failed to make a query");
95                 return -1;
96         }
97
98         ret = sqlite3_prepare_v2(app2sd_db, query, strlen(query), &stmt, NULL);
99         if (ret != SQLITE_OK) {
100                 _E("prepare failed (%s)", sqlite3_errmsg(app2sd_db));
101                 sqlite3_free(query);
102                 return -1;
103         }
104
105         if (sqlite3_step(stmt) != SQLITE_ROW) {
106                 _E("failed to step");
107                 sqlite3_finalize(stmt);
108                 return -1;
109         }
110
111         val = (const char *)sqlite3_column_text(stmt, 0);
112         ret = atoi(val);
113         sqlite3_finalize(stmt);
114
115         return ret;
116 }
117
118 int _app2sd_set_info_in_db(const char *pkgid, const char *passwd,
119                 const char *loopback_device, uid_t uid)
120 {
121         char *error_message = NULL;
122         char *query = NULL;
123         int ret = 0;
124
125         ret = _app2sd_check_existing_info(pkgid, uid);
126         if (ret < 0) {
127                 _E("failed to get existing info");
128                 return APP2EXT_ERROR_SQLITE_REGISTRY;
129         }
130
131         if (ret == 0)
132                 query = sqlite3_mprintf("insert into app2sd_info " \
133                         "(pkgid, password, filename, uid) values (%Q, %Q, %Q, %d)",
134                         pkgid, passwd, loopback_device, uid);
135         else
136                 query = sqlite3_mprintf("update app2sd_info " \
137                         "set password=%Q, filename=%Q where pkgid=%Q and uid=%d",
138                         passwd, loopback_device, pkgid, uid);
139
140         ret = sqlite3_exec(app2sd_db, query, NULL, NULL, &error_message);
141         if (ret != SQLITE_OK) {
142                 _E("failed to execute query(%s), error message(%s)",
143                         query, error_message);
144                 sqlite3_free(query);
145                 return APP2EXT_ERROR_SQLITE_REGISTRY;
146         }
147         sqlite3_free(query);
148
149         return APP2EXT_SUCCESS;
150 }
151
152 int _app2sd_remove_info_from_db(const char *pkgid, uid_t uid)
153 {
154         char *error_message = NULL;
155         char *query = NULL;
156         int ret = 0;
157
158         query = sqlite3_mprintf("delete from app2sd_info " \
159                 "where pkgid=%Q and uid=%d", pkgid, uid);
160
161         ret = sqlite3_exec(app2sd_db, query, NULL, NULL, &error_message);
162         if (ret != SQLITE_OK) {
163                 _E("failed to execute query(%s), error message(%s)",
164                         query, error_message);
165                 sqlite3_free(query);
166                 return APP2EXT_ERROR_SQLITE_REGISTRY;
167         }
168         sqlite3_free(query);
169
170         return APP2EXT_SUCCESS;
171 }
172
173 int _app2sd_get_info_from_db(const char *filename, char **pkgid, uid_t *uid)
174 {
175         char *query = NULL;
176         sqlite3_stmt *stmt = NULL;
177         int ret = APP2EXT_SUCCESS;
178
179         _D("filename(%s)", filename);
180         query = sqlite3_mprintf("select * from app2sd_info " \
181                 "where filename=%Q", filename);
182         if (query == NULL) {
183                 _E("failed to make a query");
184                 return APP2EXT_ERROR_SQLITE_REGISTRY;
185         }
186
187         ret = sqlite3_prepare_v2(app2sd_db, query, strlen(query), &stmt, NULL);
188         if (ret != SQLITE_OK) {
189                 _E("prepare failed (%s)", sqlite3_errmsg(app2sd_db));
190                 sqlite3_free(query);
191                 *pkgid = NULL;
192                 *uid = 0;
193                 return APP2EXT_ERROR_SQLITE_REGISTRY;
194         }
195
196         ret = sqlite3_step(stmt);
197         if (ret != SQLITE_ROW || ret == SQLITE_DONE) {
198                 _W("no records found");
199                 ret = APP2EXT_SUCCESS;
200                 goto FINISH_OFF;
201         }
202
203         *pkgid = strdup((const char *)sqlite3_column_text(stmt, 0));
204         if (*pkgid == NULL) {
205                 _E("out of memory");
206                 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
207                 goto FINISH_OFF;
208         }
209
210         *uid = sqlite3_column_int(stmt, 3);
211         if (*uid != GLOBAL_USER && *uid < REGULAR_USER) {
212                 _E("invalid uid");
213                 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
214                 goto FINISH_OFF;
215         }
216
217         if (SQLITE_OK != sqlite3_finalize(stmt)) {
218                 _E("error : sqlite3_finalize");
219                 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
220                 goto FINISH_OFF;
221         }
222         sqlite3_free(query);
223
224         return APP2EXT_SUCCESS;
225
226 FINISH_OFF:
227         if (*pkgid) {
228                 free(*pkgid);
229                 *pkgid = NULL;
230         }
231         *uid = 0;
232
233         sqlite3_finalize(stmt);
234         sqlite3_free(query);
235
236         return ret;
237 }
238
239 int _app2sd_get_foreach_info_from_db(app2sd_info_cb cb_func)
240 {
241         char *query = NULL;
242         sqlite3_stmt *stmt = NULL;
243         const char *pkgid = NULL;
244         int uid = 0;
245         int ret = 0;
246
247         query = sqlite3_mprintf("select * from app2sd_info");
248         if (query == NULL) {
249                 _E("failed to make a query");
250                 return APP2EXT_ERROR_SQLITE_REGISTRY;
251         }
252
253         ret = sqlite3_prepare_v2(app2sd_db, query, strlen(query), &stmt, NULL);
254         if (ret != SQLITE_OK) {
255                 _E("prepare failed (%s)", sqlite3_errmsg(app2sd_db));
256                 sqlite3_free(query);
257                 return APP2EXT_ERROR_SQLITE_REGISTRY;
258         }
259
260         ret = APP2EXT_SUCCESS;
261         while (sqlite3_step(stmt) == SQLITE_ROW) {
262                 pkgid = (const char *)sqlite3_column_text(stmt, 0);
263                 uid = sqlite3_column_int(stmt, 3);
264
265                 ret = cb_func(pkgid, (uid_t)uid);
266                 if (ret) {
267                         _E("app2sd info callback error");
268                         /* continue */
269                 }
270         }
271
272         if (SQLITE_OK != sqlite3_finalize(stmt)) {
273                 _E("error : sqlite3_finalize");
274                 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
275         }
276         sqlite3_free(query);
277
278         return ret;
279 }
280
281 char *_app2sd_get_password_from_db(const char *pkgid, uid_t uid)
282 {
283         char *query = NULL;
284         char *passwd = NULL;
285         sqlite3_stmt *stmt = NULL;
286         int ret = 0;
287
288         query = sqlite3_mprintf("select * from app2sd_info " \
289                 "where pkgid=%Q and uid=%d", pkgid, uid);
290         if (query == NULL) {
291                 _E("failed to make a query");
292                 return NULL;
293         }
294
295         ret = sqlite3_prepare_v2(app2sd_db, query, strlen(query), &stmt, NULL);
296         if (ret != SQLITE_OK) {
297                 _E("prepare failed (%s)", sqlite3_errmsg(app2sd_db));
298                 sqlite3_free(query);
299                 return NULL;
300         }
301
302         ret = sqlite3_step(stmt);
303         if (ret != SQLITE_ROW || ret == SQLITE_DONE) {
304                 _W("no records found");
305                 goto FINISH_OFF;
306         }
307         passwd = malloc(PASSWORD_LENGTH + 1);
308         if (passwd == NULL) {
309                 _E("memory allocation failed");
310                 goto FINISH_OFF;
311         }
312
313         strncpy(passwd, (const char *)sqlite3_column_text(stmt, 1),
314                 PASSWORD_LENGTH);
315         if (strlen(passwd) == 0) {
316                 _E("data is empty");
317                 goto FINISH_OFF;
318         }
319         if (SQLITE_OK != sqlite3_finalize(stmt)) {
320                 _E("error : sqlite3_finalize");
321                 goto FINISH_OFF;
322         }
323         sqlite3_free(query);
324
325         return passwd;
326
327 FINISH_OFF:
328         if (passwd)
329                 free(passwd);
330
331         sqlite3_finalize(stmt);
332         sqlite3_free(query);
333
334         return NULL;
335 }