11 #include "pkgmgr-info.h"
12 #include "pkgmgrinfo_debug.h"
13 #include "pkgmgrinfo_internal.h"
14 #include "pkgmgrinfo_private.h"
15 #include "pkgmgr_parser.h"
17 API int pkgmgrinfo_pkginfo_create_certinfo(pkgmgrinfo_certinfo_h *handle) {
18 retvm_if(handle == NULL, PMINFO_R_EINVAL,
19 "Argument supplied to hold return value is NULL\n");
20 pkgmgr_certinfo_x *certinfo = NULL;
21 certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
23 retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
24 *handle = (void *)certinfo;
29 API int pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(
30 const char *lhs_package_id, const char *rhs_package_id, uid_t uid,
31 pkgmgrinfo_cert_compare_result_type_e *compare_result) {
32 // TODO: need to use pkginfo-client APIs
38 if (lhs_package_id == NULL || rhs_package_id == NULL ||
39 compare_result == NULL) {
40 _LOGE("invalid parameter");
41 return PMINFO_R_EINVAL;
44 /* open unified global cert db */
45 dbpath = getUserPkgCertDBPath();
46 if (dbpath == NULL) return PMINFO_R_ERROR;
48 ret = __open_db(dbpath, &db, SQLITE_OPEN_READONLY);
49 if (ret != SQLITE_OK) {
50 _LOGE("failed to open db: %d", ret);
52 return PMINFO_R_ERROR;
56 if (_pkginfo_compare_certinfo(db, lhs_package_id, rhs_package_id,
58 _LOGE("failed to compare certinfo");
60 return PMINFO_R_ERROR;
69 API int pkgmgrinfo_pkginfo_compare_pkg_cert_info(
70 const char *lhs_package_id, const char *rhs_package_id,
71 pkgmgrinfo_cert_compare_result_type_e *compare_result) {
72 return pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(
73 lhs_package_id, rhs_package_id, _getuid(), compare_result);
76 static int _pkginfo_get_pkgid_from_appid(uid_t uid, const char *appid,
80 query = sqlite3_mprintf(
81 "SELECT package FROM package_app_info WHERE app_id=%Q", appid);
83 _LOGE("Out of memory");
87 // TODO: need to use pkginfo-client APIs
93 API int pkgmgrinfo_pkginfo_compare_usr_app_cert_info(
94 const char *lhs_app_id, const char *rhs_app_id, uid_t uid,
95 pkgmgrinfo_cert_compare_result_type_e *compare_result) {
100 if (lhs_app_id == NULL || rhs_app_id == NULL || compare_result == NULL) {
101 _LOGE("invalid parameter");
102 return PMINFO_R_EINVAL;
105 ret = _pkginfo_get_pkgid_from_appid(uid, lhs_app_id, &l_pkgid);
106 if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
107 ret = _pkginfo_get_pkgid_from_appid(GLOBAL_USER, lhs_app_id, &l_pkgid);
109 if (ret != PMINFO_R_OK) return ret;
111 ret = _pkginfo_get_pkgid_from_appid(uid, rhs_app_id, &r_pkgid);
112 if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
113 ret = _pkginfo_get_pkgid_from_appid(GLOBAL_USER, rhs_app_id, &r_pkgid);
115 if (ret != PMINFO_R_OK) {
120 ret = pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(l_pkgid, r_pkgid, uid,
129 API int pkgmgrinfo_pkginfo_compare_app_cert_info(
130 const char *lhs_app_id, const char *rhs_app_id,
131 pkgmgrinfo_cert_compare_result_type_e *compare_result) {
132 return pkgmgrinfo_pkginfo_compare_usr_app_cert_info(
133 lhs_app_id, rhs_app_id, _getuid(), compare_result);
136 static int _pkginfo_get_certinfo(const char *pkgid, pkgmgr_certinfo_x *info) {
138 // TODO: need to use pkginfo-client APIs
143 API int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid,
144 pkgmgrinfo_certinfo_h handle,
147 pkgmgr_certinfo_x *info = (pkgmgr_certinfo_x *)handle;
149 if (pkgid == NULL || handle == NULL) {
150 _LOGE("invalid parameter");
151 return PMINFO_R_EINVAL;
154 ret = _pkginfo_get_certinfo(pkgid, info);
155 if (ret != PMINFO_R_OK) _LOGE("failed to get certinfo of %s ", pkgid);
160 API int pkgmgrinfo_pkginfo_get_cert_value(pkgmgrinfo_certinfo_h handle,
161 pkgmgrinfo_cert_type cert_type,
162 const char **cert_value) {
163 retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
164 retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
165 retvm_if(cert_type < PMINFO_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL,
166 "Invalid certificate type\n");
167 retvm_if(cert_type > PMINFO_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL,
168 "Invalid certificate type\n");
169 pkgmgr_certinfo_x *certinfo = NULL;
170 certinfo = (pkgmgr_certinfo_x *)handle;
171 if ((certinfo->cert_info)[cert_type])
172 *cert_value = (certinfo->cert_info)[cert_type];
178 API int pkgmgrinfo_pkginfo_destroy_certinfo(pkgmgrinfo_certinfo_h handle) {
179 retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
181 pkgmgr_certinfo_x *certinfo = NULL;
182 certinfo = (pkgmgr_certinfo_x *)handle;
183 if (certinfo->pkgid) {
184 free(certinfo->pkgid);
185 certinfo->pkgid = NULL;
187 for (i = 0; i < MAX_CERT_TYPE; i++) {
188 if ((certinfo->cert_info)[i]) {
189 free((certinfo->cert_info)[i]);
190 (certinfo->cert_info)[i] = NULL;
198 API int pkgmgrinfo_create_certinfo_set_handle(
199 pkgmgrinfo_instcertinfo_h *handle) {
200 retvm_if(handle == NULL, PMINFO_R_EINVAL,
201 "Argument supplied to hold return value is NULL\n");
202 pkgmgr_certinfo_x *certinfo = NULL;
204 certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
205 retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
206 *handle = (void *)certinfo;
210 API int pkgmgrinfo_set_cert_value(pkgmgrinfo_instcertinfo_h handle,
211 pkgmgrinfo_instcert_type cert_type,
213 retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
214 retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
215 retvm_if(cert_type < PMINFO_SET_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL,
216 "Invalid certificate type\n");
217 retvm_if(cert_type > PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL,
218 "Invalid certificate type\n");
219 pkgmgr_certinfo_x *certinfo = NULL;
220 certinfo = (pkgmgr_certinfo_x *)handle;
221 if (certinfo->cert_info[cert_type]) free(certinfo->cert_info[cert_type]);
222 (certinfo->cert_info)[cert_type] = strdup(cert_value);
226 API int pkgmgrinfo_save_certinfo(const char *pkgid,
227 pkgmgrinfo_instcertinfo_h handle, uid_t uid) {
228 pkgmgr_certinfo_x *info = (pkgmgr_certinfo_x *)handle;
230 if (pkgid == NULL || handle == NULL) {
231 _LOGE("invalid parameter");
232 return PMINFO_R_EINVAL;
235 // TODO: database should be created?
236 //_check_create_cert_db();
238 // TODO: use pkginfo-client APIs
243 API int pkgmgrinfo_destroy_certinfo_set_handle(
244 pkgmgrinfo_instcertinfo_h handle) {
245 retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
247 pkgmgr_certinfo_x *certinfo = NULL;
248 certinfo = (pkgmgr_certinfo_x *)handle;
249 if (certinfo->pkgid) {
250 free(certinfo->pkgid);
251 certinfo->pkgid = NULL;
253 for (i = 0; i < MAX_CERT_TYPE; i++) {
254 if ((certinfo->cert_info)[i]) {
255 free((certinfo->cert_info)[i]);
256 (certinfo->cert_info)[i] = NULL;
264 API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid) {
266 _LOGE("invalid parameter");
267 return PMINFO_R_EINVAL;
270 // TODO: use pkginfo-client APIs
275 API int pkgmgrinfo_delete_certinfo(const char *pkgid) {
276 return pkgmgrinfo_delete_usr_certinfo(pkgid, _getuid());