14e1c0206cf70a22e9d43b103b5e38a08c6542d7
[platform/core/appfw/app2sd.git] / plugin / app2sd / src / 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 PRIMARY KEY NOT NULL, password TEXT NOT NULL, " \
41         "filename TEXT NOT NULL, uid INTEGER)"
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 char *_app2sd_get_password_from_db(const char *pkgid, uid_t uid)
240 {
241         char *query = NULL;
242         char *passwd = NULL;
243         sqlite3_stmt *stmt = NULL;
244         int ret = 0;
245
246         query = sqlite3_mprintf("select * from app2sd_info " \
247                 "where pkgid=%Q and uid=%d", pkgid, uid);
248         if (query == NULL) {
249                 _E("failed to make a query");
250                 return NULL;
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 NULL;
258         }
259
260         ret = sqlite3_step(stmt);
261         if (ret != SQLITE_ROW || ret == SQLITE_DONE) {
262                 _W("no records found");
263                 goto FINISH_OFF;
264         }
265         passwd = malloc(PASSWORD_LENGTH + 1);
266         if (passwd == NULL) {
267                 _E("memory allocation failed");
268                 goto FINISH_OFF;
269         }
270
271         strncpy(passwd, (const char *)sqlite3_column_text(stmt, 1),
272                 PASSWORD_LENGTH);
273         if (passwd == NULL) {
274                 _E("data is NULL");
275                 goto FINISH_OFF;
276         }
277         if (SQLITE_OK != sqlite3_finalize(stmt)) {
278                 _E("error : sqlite3_finalize");
279                 goto FINISH_OFF;
280         }
281         sqlite3_free(query);
282
283         return passwd;
284
285 FINISH_OFF:
286         if (passwd)
287                 free(passwd);
288
289         sqlite3_finalize(stmt);
290         sqlite3_free(query);
291
292         return NULL;
293 }