merge tizen 2.4 & add test bin
[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 <app2sd_internals.h>
25 #include <app2sd_interface.h>
26
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <dirent.h>
33 #include <sys/stat.h>
34 #include <dlog.h>
35 #include <time.h>
36 #include <db-util.h>
37
38 /* For multi-user support */
39 #include <tzplatform_config.h>
40
41 #define MAX_QUERY_LEN 4096
42 #define PASSWORD_LENGTH 64
43 /*
44 ########### Internal APIs ##################
45  */
46
47 /*sqlite  db code*/
48 #define APP2SD_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".app2sd.db")
49 sqlite3 *app2sd_db;
50 #define QUERY_CREATE_TABLE_APP2SD "create table app2sd \
51         (pkgid text primary key,\
52          password text\
53         )"
54
55 /*
56  *@_app2sd_initialize_db
57  *This function is to initialize sqlite db.
58  * return: On success, it will return zero else  if fail then return val<0.
59  */
60 int _app2sd_initialize_db()
61 {
62         char *error_message = NULL;
63         int ret;
64         FILE * fp = NULL;
65         fp = fopen(APP2SD_DB_FILE, "r");
66         if (fp != NULL) {
67                 fclose(fp);
68                 ret =
69                     db_util_open(APP2SD_DB_FILE, &app2sd_db,
70                                  DB_UTIL_REGISTER_HOOK_METHOD);
71
72                 if (ret != SQLITE_OK) {
73                         app2ext_print("====>>>> connect menu_db [%s] failed!\n",
74                                      APP2SD_DB_FILE);
75                         return -1;
76                 }
77                 return 0;
78         }
79
80         ret =
81             db_util_open(APP2SD_DB_FILE, &app2sd_db,
82                          DB_UTIL_REGISTER_HOOK_METHOD);
83
84         if (ret != SQLITE_OK) {
85                 app2ext_print("====>>>> connect menu_db [%s] failed!\n",
86                              APP2SD_DB_FILE);
87                 return -1;
88         }
89
90         if (SQLITE_OK !=
91             sqlite3_exec(app2sd_db, QUERY_CREATE_TABLE_APP2SD,
92                          NULL, NULL, &error_message)) {
93                 app2ext_print("Don't execute query = %s, "
94                              "error message = %s\n",
95                              QUERY_CREATE_TABLE_APP2SD, error_message);
96                 return -1;
97         }
98
99         app2ext_print("\n db_initialize_done ");
100         return 0;
101 }
102
103 /*
104  *@_app2sd_set_password_in_db
105  *This function is to store password into  db.
106  * param[in]: pkgid: package id
107  * param[in]: password: password string
108  * return: On success, it will return 0.
109  * Else appropriate error will be returned.
110  */
111 int _app2sd_set_password_in_db(const char *pkgid,
112                                       const char *passwd)
113 {
114         char *error_message = NULL;
115
116         char *query = sqlite3_mprintf("insert into app2sd(pkgid,password) values (%Q, %Q)", pkgid, passwd);
117
118         if (SQLITE_OK != sqlite3_exec(app2sd_db, query, NULL, NULL,
119                                       &error_message)) {
120                 app2ext_print("Don't execute query = %s, error message = %s\n",
121                              query, error_message);
122
123                 sqlite3_free(query);
124                 return APP2EXT_ERROR_SQLITE_REGISTRY;
125         }
126         sqlite3_free(query);
127         app2ext_print("\n sqlite insertion done ");
128         return APP2EXT_SUCCESS;
129 }
130
131 /*
132  *@_app2sd_remove_password_from_db
133  *This function is to remove passwod from  db.
134  * param[in]: pkgid: package id
135  * return: On success, it will return 0.
136  * Else appropriate error will be returned.
137  */
138 int _app2sd_remove_password_from_db(const char *pkgid)
139 {
140         char *error_message = NULL;
141
142         char *query = sqlite3_mprintf("delete from app2sd where pkgid LIKE %Q", pkgid);
143         app2ext_print("\n deletion querys is %s ", query);
144
145         if (SQLITE_OK != sqlite3_exec(app2sd_db, query, NULL,
146                                       NULL, &error_message)) {
147                 app2ext_print("Don't execute query = %s, error message = %s\n",
148                              query, error_message);
149                 sqlite3_free(query);
150                 return APP2EXT_ERROR_SQLITE_REGISTRY;
151         }
152
153         sqlite3_free(query);
154         app2ext_print("\n app2sd password deletion done ");
155         return APP2EXT_SUCCESS;
156
157 }
158
159 /*
160  *@_app2sd_get_password_from_db
161  *This function is to retrive password from DB
162  * param[in]: pkgid: package id
163  * return: On success, it will return the password, else NULL.
164  */
165 char *_app2sd_get_password_from_db(const char *pkgid)
166 {
167         char query[MAX_QUERY_LEN] = { 0 };
168         sqlite3_stmt *stmt = NULL;
169         const char *tail = NULL;
170         int rc = 0;
171         char *passwd = NULL;
172
173         sqlite3_snprintf(MAX_QUERY_LEN, query,
174                  "select * from app2sd where pkgid LIKE '%s'", pkgid);
175         app2ext_print("\n access querys is %s ", query);
176
177         if (SQLITE_OK != sqlite3_prepare(app2sd_db, query,
178                                          strlen(query), &stmt, &tail)) {
179                 app2ext_print("sqlite3_prepare error\n");
180                 return NULL;
181         }
182
183         rc = sqlite3_step(stmt);
184         if (rc != SQLITE_ROW || rc == SQLITE_DONE) {
185                 app2ext_print("No records found");
186                 goto FINISH_OFF;
187         }
188         passwd = malloc(PASSWORD_LENGTH + 1);
189         if (passwd == NULL) {
190                 app2ext_print("memory allocation failed\n");
191                 goto FINISH_OFF;
192         }
193
194         app2ext_print("\n entry available in sqlite");
195         strncpy(passwd, (const char*)sqlite3_column_text(stmt, 1),
196                 PASSWORD_LENGTH);
197         if (passwd == NULL) {
198                 app2ext_print("\n password is NULL ");
199                 goto FINISH_OFF;
200         }
201         app2ext_print("\n passwd is %s ", passwd);
202         if (SQLITE_OK != sqlite3_finalize(stmt)) {
203                 app2ext_print("error : sqlite3_finalize\n");
204                 goto FINISH_OFF;
205         }
206         return passwd;
207
208 FINISH_OFF:
209         rc = sqlite3_finalize(stmt);
210         if (rc != SQLITE_OK) {
211                 app2ext_print(" sqlite3_finalize failed - %d", rc);
212         }
213         if (passwd)
214                 free(passwd);
215         return NULL;
216 }