11 #include "manager/pkginfo_manager.h"
12 #include "pkgmgr-info.h"
13 #include "pkgmgrinfo_debug.h"
14 #include "pkgmgrinfo_internal.h"
15 #include "pkgmgrinfo_private.h"
16 #include "pkgmgr_parser.h"
18 API int pkgmgrinfo_pkginfo_create_certinfo(pkgmgrinfo_certinfo_h *handle) {
19 retvm_if(handle == NULL, PMINFO_R_EINVAL,
20 "Argument supplied to hold return value is NULL\n");
21 pkgmgr_certinfo_x *certinfo = NULL;
22 certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
24 retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
25 *handle = (void *)certinfo;
30 API int pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(
31 const char *lhs_package_id, const char *rhs_package_id, uid_t uid,
32 pkgmgrinfo_cert_compare_result_type_e *compare_result) {
33 // TODO: need to use pkginfo-client APIs
39 if (lhs_package_id == NULL || rhs_package_id == NULL ||
40 compare_result == NULL) {
41 _LOGE("invalid parameter");
42 return PMINFO_R_EINVAL;
45 /* open unified global cert db */
46 dbpath = getUserPkgCertDBPath();
47 if (dbpath == NULL) return PMINFO_R_ERROR;
49 ret = __open_db(dbpath, &db, SQLITE_OPEN_READONLY);
50 if (ret != SQLITE_OK) {
51 _LOGE("failed to open db: %d", ret);
53 return PMINFO_R_ERROR;
57 if (_pkginfo_compare_certinfo(db, lhs_package_id, rhs_package_id,
59 _LOGE("failed to compare certinfo");
61 return PMINFO_R_ERROR;
70 API int pkgmgrinfo_pkginfo_compare_pkg_cert_info(
71 const char *lhs_package_id, const char *rhs_package_id,
72 pkgmgrinfo_cert_compare_result_type_e *compare_result) {
73 return pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(
74 lhs_package_id, rhs_package_id, _getuid(), compare_result);
77 static int _pkginfo_get_pkgid_from_appid(uid_t uid, const char *appid,
81 query = sqlite3_mprintf(
82 "SELECT package FROM package_app_info WHERE app_id=%Q", appid);
84 _LOGE("Out of memory");
88 // TODO: need to use pkginfo-client APIs
94 API int pkgmgrinfo_pkginfo_compare_usr_app_cert_info(
95 const char *lhs_app_id, const char *rhs_app_id, uid_t uid,
96 pkgmgrinfo_cert_compare_result_type_e *compare_result) {
101 if (lhs_app_id == NULL || rhs_app_id == NULL || compare_result == NULL) {
102 _LOGE("invalid parameter");
103 return PMINFO_R_EINVAL;
106 ret = _pkginfo_get_pkgid_from_appid(uid, lhs_app_id, &l_pkgid);
107 if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
108 ret = _pkginfo_get_pkgid_from_appid(GLOBAL_USER, lhs_app_id, &l_pkgid);
110 if (ret != PMINFO_R_OK) return ret;
112 ret = _pkginfo_get_pkgid_from_appid(uid, rhs_app_id, &r_pkgid);
113 if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
114 ret = _pkginfo_get_pkgid_from_appid(GLOBAL_USER, rhs_app_id, &r_pkgid);
116 if (ret != PMINFO_R_OK) {
121 ret = pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(l_pkgid, r_pkgid, uid,
130 API int pkgmgrinfo_pkginfo_compare_app_cert_info(
131 const char *lhs_app_id, const char *rhs_app_id,
132 pkgmgrinfo_cert_compare_result_type_e *compare_result) {
133 return pkgmgrinfo_pkginfo_compare_usr_app_cert_info(
134 lhs_app_id, rhs_app_id, _getuid(), compare_result);
137 static int _pkginfo_get_certinfo(const char *pkgid, pkgmgr_certinfo_x *info) {
139 // TODO: need to use pkginfo-client APIs
144 API int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid,
145 pkgmgrinfo_certinfo_h handle,
148 pkgmgr_certinfo_x *info = (pkgmgr_certinfo_x *)handle;
150 if (pkgid == NULL || handle == NULL) {
151 _LOGE("invalid parameter");
152 return PMINFO_R_EINVAL;
155 ret = _pkginfo_get_certinfo(pkgid, info);
156 if (ret != PMINFO_R_OK) _LOGE("failed to get certinfo of %s ", pkgid);
161 API int pkgmgrinfo_pkginfo_get_cert_value(pkgmgrinfo_certinfo_h handle,
162 pkgmgrinfo_cert_type cert_type,
163 const char **cert_value) {
164 retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
165 retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
166 retvm_if(cert_type < PMINFO_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL,
167 "Invalid certificate type\n");
168 retvm_if(cert_type > PMINFO_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL,
169 "Invalid certificate type\n");
170 pkgmgr_certinfo_x *certinfo = NULL;
171 certinfo = (pkgmgr_certinfo_x *)handle;
172 if ((certinfo->cert_info)[cert_type])
173 *cert_value = (certinfo->cert_info)[cert_type];
179 API int pkgmgrinfo_pkginfo_destroy_certinfo(pkgmgrinfo_certinfo_h handle) {
180 retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
182 pkgmgr_certinfo_x *certinfo = NULL;
183 certinfo = (pkgmgr_certinfo_x *)handle;
184 if (certinfo->pkgid) {
185 free(certinfo->pkgid);
186 certinfo->pkgid = NULL;
188 for (i = 0; i < MAX_CERT_TYPE; i++) {
189 if ((certinfo->cert_info)[i]) {
190 free((certinfo->cert_info)[i]);
191 (certinfo->cert_info)[i] = NULL;
199 API int pkgmgrinfo_create_certinfo_set_handle(
200 pkgmgrinfo_instcertinfo_h *handle) {
201 retvm_if(handle == NULL, PMINFO_R_EINVAL,
202 "Argument supplied to hold return value is NULL\n");
203 pkgmgr_certinfo_x *certinfo = NULL;
205 certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
206 retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
207 *handle = (void *)certinfo;
211 API int pkgmgrinfo_set_cert_value(pkgmgrinfo_instcertinfo_h handle,
212 pkgmgrinfo_instcert_type cert_type,
214 retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
215 retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
216 retvm_if(cert_type < PMINFO_SET_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL,
217 "Invalid certificate type\n");
218 retvm_if(cert_type > PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL,
219 "Invalid certificate type\n");
220 pkgmgr_certinfo_x *certinfo = NULL;
221 certinfo = (pkgmgr_certinfo_x *)handle;
222 if (certinfo->cert_info[cert_type]) free(certinfo->cert_info[cert_type]);
223 (certinfo->cert_info)[cert_type] = strdup(cert_value);
227 API int pkgmgrinfo_save_certinfo(const char *pkgid,
228 pkgmgrinfo_instcertinfo_h handle, uid_t uid) {
229 if (pkgid == NULL || handle == NULL) {
230 _LOGE("invalid parameter");
231 return PMINFO_R_EINVAL;
233 pkgmgr_certinfo_x *certinfo = (pkgmgr_certinfo_x *)handle;
234 return _pkginfo_insert_certinfo(pkgid, certinfo, uid);
237 API int pkgmgrinfo_destroy_certinfo_set_handle(
238 pkgmgrinfo_instcertinfo_h handle) {
239 retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
241 pkgmgr_certinfo_x *certinfo = NULL;
242 certinfo = (pkgmgr_certinfo_x *)handle;
243 if (certinfo->pkgid) {
244 free(certinfo->pkgid);
245 certinfo->pkgid = NULL;
247 for (i = 0; i < MAX_CERT_TYPE; i++) {
248 if ((certinfo->cert_info)[i]) {
249 free((certinfo->cert_info)[i]);
250 (certinfo->cert_info)[i] = NULL;
258 API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid) {
260 _LOGE("invalid parameter");
261 return PMINFO_R_EINVAL;
264 return _pkginfo_delete_certinfo(pkgid);
267 API int pkgmgrinfo_delete_certinfo(const char *pkgid) {
268 return pkgmgrinfo_delete_usr_certinfo(pkgid, _getuid());