consider an extra icon_path for only RO package
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_db.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <ctype.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <grp.h>
10 #include <dirent.h>
11 #include <libgen.h>
12
13 #include <sqlite3.h>
14
15 #include <tzplatform_config.h>
16 #include <db-util.h>
17
18 #include "pkgmgr-info.h"
19 #include "pkgmgrinfo_debug.h"
20 #include "pkgmgrinfo_private.h"
21 #include "pkgmgr_parser.h"
22 #include "pkgmgr_parser_internal.h"
23
24 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO \
25         "CREATE TABLE IF NOT EXISTS package_cert_index_info( " \
26         " cert_info TEXT UNIQUE, " \
27         " cert_id INTEGER PRIMARY KEY, " \
28         " cert_ref_count INTEGER NOT NULL)"
29
30 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO \
31         "CREATE TABLE IF NOT EXISTS package_cert_info( " \
32         " package TEXT PRIMARY KEY, " \
33         " package_count INTEGER, " \
34         " author_root_cert INTEGER, " \
35         " author_im_cert INTEGER, " \
36         " author_signer_cert INTEGER, " \
37         " dist_root_cert INTEGER, " \
38         " dist_im_cert INTEGER, " \
39         " dist_signer_cert INTEGER, " \
40         " dist2_root_cert INTEGER, " \
41         " dist2_im_cert INTEGER, " \
42         " dist2_signer_cert INTEGER)"
43
44 #define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO \
45         "CREATE TRIGGER IF NOT EXISTS update_cert_info " \
46         "AFTER UPDATE ON package_cert_info " \
47         "WHEN (NEW.package_count = 0) " \
48         "BEGIN" \
49         " DELETE FROM package_cert_info WHERE package=OLD.package;" \
50         "END;"
51
52 #define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2 \
53         "CREATE TRIGGER IF NOT EXISTS update_cert_info2 " \
54         "AFTER UPDATE ON package_cert_info " \
55         "WHEN (NEW.package_count = OLD.package_count + 1) " \
56         "BEGIN" \
57         " UPDATE package_cert_index_info SET" \
58         "  cert_ref_count = cert_ref_count - 1" \
59         " WHERE cert_id = OLD.author_root_cert" \
60         "  OR cert_id = OLD.author_im_cert" \
61         "  OR cert_id = OLD.author_signer_cert" \
62         "  OR cert_id = OLD.dist_root_cert" \
63         "  OR cert_id = OLD.dist_im_cert" \
64         "  OR cert_id = OLD.dist_signer_cert" \
65         "  OR cert_id = OLD.dist2_root_cert" \
66         "  OR cert_id = OLD.dist2_im_cert" \
67         "  OR cert_id = OLD.dist2_signer_cert;" \
68         "END;"
69
70 #define QUERY_CREATE_TRIGGER_DELETE_CERT_INFO \
71         "CREATE TRIGGER IF NOT EXISTS delete_cert_info " \
72         "AFTER DELETE ON package_cert_info " \
73         "BEGIN" \
74         " UPDATE package_cert_index_info SET" \
75         "  cert_ref_count = cert_ref_count - 1" \
76         " WHERE cert_id = OLD.author_root_cert" \
77         "  OR cert_id = OLD.author_im_cert" \
78         "  OR cert_id = OLD.author_signer_cert" \
79         "  OR cert_id = OLD.dist_root_cert" \
80         "  OR cert_id = OLD.dist_im_cert" \
81         "  OR cert_id = OLD.dist_signer_cert" \
82         "  OR cert_id = OLD.dist2_root_cert" \
83         "  OR cert_id = OLD.dist2_im_cert" \
84         "  OR cert_id = OLD.dist2_signer_cert;" \
85         "END;"
86
87 #define QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO \
88         "CREATE TRIGGER IF NOT EXISTS update_cert_index_info " \
89         "AFTER UPDATE ON package_cert_index_info " \
90         "WHEN ((SELECT cert_ref_count FROM package_cert_index_info " \
91         "       WHERE cert_id = OLD.cert_id) = 0) "\
92         "BEGIN" \
93         " DELETE FROM package_cert_index_info WHERE cert_id = OLD.cert_id;" \
94         "END;"
95
96 __thread db_handle manifest_db;
97 __thread db_handle cert_db;
98
99 typedef int (*sqlite_query_callback)(void *data, int ncols, char **coltxt, char **colname);
100
101 static int _mkdir_for_user(const char* dir, uid_t uid, gid_t gid)
102 {
103         int ret;
104         char *fullpath;
105         char *subpath;
106         char buf[1024];
107         int fd;
108         struct stat sb;
109
110         fullpath = strdup(dir);
111         if (fullpath == NULL)
112                 return -1;
113         subpath = dirname(fullpath);
114         if (strlen(subpath) > 1 && strcmp(subpath, fullpath) != 0) {
115                 ret = _mkdir_for_user(fullpath, uid, gid);
116                 if (ret == -1) {
117                         free(fullpath);
118                         return ret;
119                 }
120         }
121
122         ret = mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
123         if (ret && errno != EEXIST) {
124                 free(fullpath);
125                 return ret;
126         } else if (ret && errno == EEXIST) {
127                 free(fullpath);
128                 return 0;
129         }
130
131         if (getuid() == ROOT_UID) {
132                 fd = open(dir, O_RDONLY);
133                 if (fd == -1) {
134                         _LOGE("FAIL : open %s : %s", dir,
135                                         strerror_r(errno, buf, sizeof(buf)));
136                         return -1;
137                 }
138                 ret = fstat(fd, &sb);
139                 if (ret == -1) {
140                         _LOGE("FAIL : fstat %s : %s", dir,
141                                         strerror_r(errno, buf, sizeof(buf)));
142                         close(fd);
143                         return -1;
144                 }
145                 if (S_ISLNK(sb.st_mode)) {
146                         _LOGE("FAIL : %s is symlink!", dir);
147                         close(fd);
148                         return -1;
149                 }
150                 ret = fchown(fd, uid, gid);
151                 if (ret == -1) {
152                         _LOGE("FAIL : fchown %s %d.%d, because %s", dir, uid,
153                                         gid, strerror_r(errno, buf, sizeof(buf)));
154                         close(fd);
155                         return -1;
156                 }
157                 close(fd);
158         }
159
160         free(fullpath);
161
162         return 0;
163 }
164
165 static char *_get_db_path(uid_t uid)
166 {
167         const char *db_path;
168         char path[PATH_MAX];
169
170         db_path = tzplatform_getenv(TZ_SYS_DB);
171         if (db_path == NULL) {
172                 _LOGE("Failed to get TZ_SYS_DB path");
173                 return NULL;
174         }
175
176         if (uid == GLOBAL_USER || uid == ROOT_UID)
177                 return strdup(db_path);
178
179         snprintf(path, sizeof(path), "%s/user/%d", db_path, uid);
180
181         return strdup(path);
182 }
183
184 static int __attach_and_create_view(sqlite3 *handle, const char *db, const char *tables[], uid_t uid)
185 {
186         int i;
187         char *err;
188         char query[MAX_QUERY_LEN];
189
190         if (uid != GLOBAL_USER && uid != ROOT_UID) {
191                 snprintf(query, sizeof(query), "ATTACH DATABASE '%s' AS Global", db);
192                 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
193                         _LOGD("Don't execute query = %s error message = %s\n", query, err);
194                         sqlite3_free(err);
195                         return SQLITE_ERROR;
196                 }
197         }
198
199         for (i = 0; tables[i]; i++) {
200                 if (uid != GLOBAL_USER && uid != ROOT_UID)
201                         snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
202                                         FROM (SELECT *,0 AS for_all_users FROM main.'%s' UNION \
203                                         SELECT *,1 AS for_all_users FROM Global.'%s')",
204                                         tables[i], tables[i], tables[i]);
205                 else
206                         snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
207                                         FROM (SELECT *,1 AS for_all_users FROM main.'%s')",
208                                         tables[i], tables[i]);
209                 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
210                         _LOGD("Don't execute query = %s error message = %s\n", query, err);
211                         sqlite3_free(err);
212                 }
213         }
214
215         return SQLITE_OK;
216 }
217
218 static int __exec_db_query(sqlite3 *db, char *query, sqlite_query_callback callback, void *data)
219 {
220         char *error_message = NULL;
221         int ret = sqlite3_exec(db, query, callback, data, &error_message);
222         if (SQLITE_OK != ret) {
223                 _LOGE("Don't execute query = %s error message = %s   ret = %d\n", query,
224                        error_message, ret);
225                 sqlite3_free(error_message);
226                 return -1;
227         }
228         sqlite3_free(error_message);
229         return 0;
230 }
231
232 int _check_create_cert_db(sqlite3 *certdb)
233 {
234         int ret = 0;
235         ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO, NULL, NULL);
236         if (ret < 0)
237                 return ret;
238         ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO, NULL, NULL);
239         if (ret < 0)
240                 return ret;
241         ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO, NULL, NULL);
242         if (ret < 0)
243                 return ret;
244         ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2, NULL, NULL);
245         if (ret < 0)
246                 return ret;
247         ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_DELETE_CERT_INFO, NULL, NULL);
248         if (ret < 0)
249                 return ret;
250         ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO, NULL, NULL);
251         return ret;
252 }
253 static gid_t _get_gid(const char *name)
254 {
255         char buf[BUFSIZE];
256         struct group entry;
257         struct group *ge;
258         int ret;
259
260         ret = getgrnam_r(name, &entry, buf, sizeof(buf), &ge);
261         if (ret || ge == NULL) {
262                 _LOGE("fail to get gid of %s", name);
263                 return -1;
264         }
265
266         return entry.gr_gid;
267 }
268
269 API const char *getIconPath(uid_t uid, bool readonly)
270 {
271         const char *path = NULL;
272         uid_t uid_caller = getuid();
273         gid_t gid = ROOT_UID;
274
275         if (uid != GLOBAL_USER && uid != ROOT_UID) {
276                 _LOGD("not supported target user");
277                 return NULL;
278         }
279
280         if (readonly)
281                 path = tzplatform_mkpath(TZ_SYS_RO_ICONS, "/");
282
283         /* just allow certain users to create the icon directory if needed. */
284         if (path && (uid_caller == ROOT_UID  ||
285                 uid_caller == APPFW_UID || uid_caller == uid))
286                 _mkdir_for_user(path, uid, gid);
287
288         return path;
289 }
290
291 API char *getUserPkgParserDBPath(void)
292 {
293         return getUserPkgParserDBPathUID(_getuid());
294 }
295
296 API char *getUserPkgParserDBPathUID(uid_t uid)
297 {
298         char pkgmgr_parser_db[PATH_MAX];
299         uid_t uid_caller = getuid();
300         gid_t gid = ROOT_UID;
301         char *db_path;
302
303         db_path = _get_db_path(uid);
304         if (db_path == NULL) {
305                 _LOGE("Failed to get db path %d", uid);
306                 return NULL;
307         }
308         snprintf(pkgmgr_parser_db, sizeof(pkgmgr_parser_db),
309                         "%s/.pkgmgr_parser.db", db_path);
310
311         if (uid != GLOBAL_USER && uid != ROOT_UID) {
312                 tzplatform_set_user(uid);
313                 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
314                 tzplatform_reset_user();
315         }
316
317         // just allow certain users to create the dbspace directory if needed.
318         if (uid_caller == ROOT_UID  || uid_caller == APPFW_UID || uid_caller == uid)
319                 _mkdir_for_user(db_path, uid, gid);
320
321         free(db_path);
322
323         return strdup(pkgmgr_parser_db);
324 }
325
326 API char *getUserPkgCertDBPath(void)
327 {
328          return getUserPkgCertDBPathUID(_getuid());
329 }
330
331 API char *getUserPkgCertDBPathUID(uid_t uid)
332 {
333         char pkgmgr_cert_db[PATH_MAX];
334         uid_t uid_caller = getuid();
335         gid_t gid = ROOT_UID;
336         char *db_path;
337
338         db_path = _get_db_path(uid);
339         if (db_path == NULL) {
340                 _LOGE("Failed to get db path %d", uid);
341                 return NULL;
342         }
343         snprintf(pkgmgr_cert_db, sizeof(pkgmgr_cert_db),
344                         "%s/.pkgmgr_cert.db", db_path);
345
346         if (uid != GLOBAL_USER && uid != ROOT_UID) {
347                 tzplatform_set_user(uid);
348                 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
349                 tzplatform_reset_user();
350         }
351
352         // just allow certain users to create the dbspace directory if needed.
353         if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
354                 _mkdir_for_user(db_path, uid, gid);
355
356         free(db_path);
357
358         return strdup(pkgmgr_cert_db);
359 }
360
361 API const char *getUserManifestPath(uid_t uid, bool readonly)
362 {
363         const char *path = NULL;
364         uid_t uid_caller = getuid();
365         gid_t gid = ROOT_UID;
366
367         if (uid != GLOBAL_USER && uid != ROOT_UID) {
368                 tzplatform_set_user(uid);
369                 path = tzplatform_mkpath(TZ_USER_PACKAGES, "/");
370                 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
371                 tzplatform_reset_user();
372         } else {
373                 if (readonly)
374                         path = tzplatform_mkpath(TZ_SYS_RO_PACKAGES, "/");
375                 else
376                         path = tzplatform_mkpath(TZ_SYS_RW_PACKAGES, "/");
377         }
378
379         /* just allow certain users to create the icon directory if needed. */
380         if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
381                 _mkdir_for_user(path, uid, gid);
382
383         return path;
384 }
385
386 int __close_manifest_db(void)
387 {
388         if (manifest_db.ref) {
389                 if (--manifest_db.ref == 0)
390                         sqlite3_close(GET_DB(manifest_db));
391                 return 0;
392         }
393         return -1;
394 }
395
396 static const char *parserdb_tables[] = {
397         "package_app_app_category",
398         "package_app_info",
399         "package_app_app_control",
400         "package_app_localized_info",
401         "package_app_app_metadata",
402         "package_app_share_allowed",
403         "package_app_app_permission",
404         "package_app_share_request",
405         "package_info",
406         "package_app_data_control",
407         "package_localized_info",
408         "package_app_icon_section_info",
409         "package_privilege_info",
410         "package_app_image_info",
411         NULL
412 };
413
414 int __open_manifest_db(uid_t uid, bool readonly)
415 {
416         int ret;
417         char *user_pkg_parser;
418         int flags;
419
420         if (manifest_db.ref) {
421                 manifest_db.ref++;
422                 return 0;
423         }
424
425         user_pkg_parser = getUserPkgParserDBPathUID(uid);
426         if (user_pkg_parser == NULL) {
427                 _LOGE("Failed to get pkg parser db path - %d", uid);
428                 return -1;
429         }
430
431         if (access(user_pkg_parser, F_OK) != 0) {
432                 _LOGE("Manifest DB does not exists !!");
433                 free(user_pkg_parser);
434                 return -1;
435         }
436
437         flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
438         ret = db_util_open_with_options(user_pkg_parser, &GET_DB(manifest_db),
439                         flags, NULL);
440         if (ret != SQLITE_OK) {
441                 _LOGE("connect db [%s] failed!\n", user_pkg_parser);
442                 free(user_pkg_parser);
443                 return -1;
444         }
445
446         manifest_db.ref++;
447         if (readonly) {
448                 ret = __attach_and_create_view(GET_DB(manifest_db), MANIFEST_DB,
449                                 parserdb_tables, uid);
450                 if (ret != SQLITE_OK) {
451                         _LOGE("attach db [%s] failed!\n", user_pkg_parser);
452                         free(user_pkg_parser);
453                         return -1;
454                 }
455         }
456
457         free(user_pkg_parser);
458
459         return 0;
460 }
461
462 int __close_cert_db(void)
463 {
464         if (cert_db.ref) {
465                 if (--cert_db.ref == 0)
466                         sqlite3_close_v2(GET_DB(cert_db));
467                         return 0;
468         }
469         _LOGE("Certificate DB is already closed !!\n");
470         return -1;
471 }
472
473 static const char *certdb_tables[] = {
474         "package_cert_index_info",
475         "package_cert_info",
476         NULL
477 };
478
479 int __open_cert_db(uid_t uid, bool readonly)
480 {
481         int ret;
482         char *user_cert_parser;
483         int flags;
484
485         if (cert_db.ref) {
486                 cert_db.ref++;
487                 return 0;
488         }
489
490         user_cert_parser = getUserPkgCertDBPathUID(uid);
491         if (user_cert_parser == NULL) {
492                 _LOGE("Failed to get pkg cert db path - %d", uid);
493                 return -1;
494         }
495
496         if (access(user_cert_parser, F_OK) != 0) {
497                 _LOGE("Cert DB does not exists !!");
498                 free(user_cert_parser);
499                 return -1;
500         }
501
502         flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
503         ret = db_util_open_with_options(user_cert_parser, &GET_DB(cert_db),
504                         flags, NULL);
505         if (ret != SQLITE_OK) {
506                 _LOGE("connect db [%s] failed!", user_cert_parser);
507                 free(user_cert_parser);
508                 return -1;
509         }
510         cert_db.ref++;
511         if (readonly) {
512                 ret = __attach_and_create_view(GET_DB(cert_db), CERT_DB,
513                                 certdb_tables, uid);
514                 if (ret != SQLITE_OK) {
515                         _LOGE("attach db [%s] failed!", user_cert_parser);
516                         free(user_cert_parser);
517                         return -1;
518                 }
519         }
520
521         free(user_cert_parser);
522
523         return 0;
524 }
525
526 void _save_column_int(sqlite3_stmt *stmt, int idx, int *i)
527 {
528         *i = sqlite3_column_int(stmt, idx);
529 }
530
531 void _save_column_str(sqlite3_stmt *stmt, int idx, char **str)
532 {
533         const char *val;
534
535         val = (const char *)sqlite3_column_text(stmt, idx);
536         if (val)
537                 *str = strdup(val);
538 }
539
540 API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path, uid_t uid)
541 {
542         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
543         int ret = -1;
544         sqlite3 *pkgmgr_parser_db = NULL;
545         char *query = NULL;
546         char *db_path;
547
548         db_path = getUserPkgParserDBPathUID(uid);
549         if (db_path == NULL) {
550                 _LOGE("Failed to get pkg parser db path - %d", uid);
551                 return PMINFO_R_ERROR;
552         }
553
554         ret = db_util_open_with_options(db_path, &pkgmgr_parser_db,
555                         SQLITE_OPEN_READWRITE, NULL);
556         if (ret != SQLITE_OK) {
557                 _LOGE("connect db failed!");
558                 free(db_path);
559                 return PMINFO_R_ERROR;
560         }
561         free(db_path);
562
563         /*Begin transaction*/
564         /* Setting Manifest DB */
565         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
566         tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
567         _LOGD("Transaction Begin\n");
568
569         /* pkgcakge_info table */
570         query = sqlite3_mprintf(
571                         "update package_info set installed_storage=%Q, external_path=%Q where package=%Q",
572                         location ? "installed_external" : "installed_internal", external_pkg_path, pkgid);
573
574         ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
575         tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
576         sqlite3_free(query);
577
578         /* package_app_info table */
579         query = sqlite3_mprintf(
580                         "update package_app_info set app_installed_storage=%Q, app_external_path=%Q where package=%Q",
581                         location ? "installed_external" : "installed_internal", external_pkg_path, pkgid);
582
583         ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
584         tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
585
586         /*Commit transaction*/
587         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
588         if (ret != SQLITE_OK) {
589                 _LOGE("Failed to commit transaction. Rollback now\n");
590                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
591                 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
592         }
593         _LOGD("Transaction Commit and End\n");
594
595         ret = PMINFO_R_OK;
596 catch:
597         sqlite3_close(pkgmgr_parser_db);
598         sqlite3_free(query);
599         return ret;
600 }
601
602 API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path)
603 {
604         return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, external_pkg_path, _getuid());
605 }