Replace implementation of comparing pkg certinfo
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_certinfo.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdbool.h>
5 #include <unistd.h>
6 #include <sys/types.h>
7
8 #include <sqlite3.h>
9 #include <glib.h>
10
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"
17
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));
23   *handle = NULL;
24   retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
25   *handle = (void *)certinfo;
26   return PMINFO_R_OK;
27 }
28
29
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   if (lhs_package_id == NULL || rhs_package_id == NULL ||
34       compare_result == NULL) {
35     _LOGE("invalid parameter");
36     return PMINFO_R_EINVAL;
37   }
38
39   return _certinfo_compare_certinfo(lhs_package_id, rhs_package_id,
40       compare_result);
41 }
42
43 API int pkgmgrinfo_pkginfo_compare_pkg_cert_info(
44     const char *lhs_package_id, const char *rhs_package_id,
45     pkgmgrinfo_cert_compare_result_type_e *compare_result) {
46   return pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(
47       lhs_package_id, rhs_package_id, _getuid(), compare_result);
48 }
49
50 static int _pkginfo_get_pkgid_from_appid(uid_t uid, const char *appid,
51                                          char **pkgid) {
52   char *query = NULL;
53
54   query = sqlite3_mprintf(
55       "SELECT package FROM package_app_info WHERE app_id=%Q", appid);
56   if (query == NULL) {
57     _LOGE("Out of memory");
58     return -1;
59   }
60
61   // TODO: need to use pkginfo-client APIs
62
63   sqlite3_free(query);
64   return 0;
65 }
66
67 API int pkgmgrinfo_pkginfo_compare_usr_app_cert_info(
68     const char *lhs_app_id, const char *rhs_app_id, uid_t uid,
69     pkgmgrinfo_cert_compare_result_type_e *compare_result) {
70   int ret;
71   char *l_pkgid = NULL;
72   char *r_pkgid = NULL;
73
74   if (lhs_app_id == NULL || rhs_app_id == NULL || compare_result == NULL) {
75     _LOGE("invalid parameter");
76     return PMINFO_R_EINVAL;
77   }
78
79   ret = _pkginfo_get_pkgid_from_appid(uid, lhs_app_id, &l_pkgid);
80   if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
81     ret = _pkginfo_get_pkgid_from_appid(GLOBAL_USER, lhs_app_id, &l_pkgid);
82
83   if (ret != PMINFO_R_OK) return ret;
84
85   ret = _pkginfo_get_pkgid_from_appid(uid, rhs_app_id, &r_pkgid);
86   if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
87     ret = _pkginfo_get_pkgid_from_appid(GLOBAL_USER, rhs_app_id, &r_pkgid);
88
89   if (ret != PMINFO_R_OK) {
90     free(l_pkgid);
91     return ret;
92   }
93
94   ret = pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(l_pkgid, r_pkgid, uid,
95                                                      compare_result);
96
97   free(l_pkgid);
98   free(r_pkgid);
99
100   return ret;
101 }
102
103 API int pkgmgrinfo_pkginfo_compare_app_cert_info(
104     const char *lhs_app_id, const char *rhs_app_id,
105     pkgmgrinfo_cert_compare_result_type_e *compare_result) {
106   return pkgmgrinfo_pkginfo_compare_usr_app_cert_info(
107       lhs_app_id, rhs_app_id, _getuid(), compare_result);
108 }
109
110 API int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid,
111                                          pkgmgrinfo_certinfo_h handle,
112                                          uid_t uid) {
113   int ret;
114   pkgmgr_certinfo_x *info = (pkgmgr_certinfo_x *)handle;
115
116   if (pkgid == NULL || handle == NULL) {
117     _LOGE("invalid parameter");
118     return PMINFO_R_EINVAL;
119   }
120
121   ret = _pkginfo_get_certinfo(pkgid, &info, uid);
122   if (ret != PMINFO_R_OK) _LOGE("failed to get certinfo of %s ", pkgid);
123
124   return ret;
125 }
126
127 API int pkgmgrinfo_pkginfo_get_cert_value(pkgmgrinfo_certinfo_h handle,
128                                           pkgmgrinfo_cert_type cert_type,
129                                           const char **cert_value) {
130   retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
131   retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
132   retvm_if(cert_type < PMINFO_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL,
133            "Invalid certificate type\n");
134   retvm_if(cert_type > PMINFO_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL,
135            "Invalid certificate type\n");
136   pkgmgr_certinfo_x *certinfo = NULL;
137   certinfo = (pkgmgr_certinfo_x *)handle;
138   if ((certinfo->cert_info)[cert_type])
139     *cert_value = (certinfo->cert_info)[cert_type];
140   else
141     *cert_value = NULL;
142   return PMINFO_R_OK;
143 }
144
145 API int pkgmgrinfo_pkginfo_destroy_certinfo(pkgmgrinfo_certinfo_h handle) {
146   retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
147   int i = 0;
148   pkgmgr_certinfo_x *certinfo = NULL;
149   certinfo = (pkgmgr_certinfo_x *)handle;
150   if (certinfo->pkgid) {
151     free(certinfo->pkgid);
152     certinfo->pkgid = NULL;
153   }
154   for (i = 0; i < MAX_CERT_TYPE; i++) {
155     if ((certinfo->cert_info)[i]) {
156       free((certinfo->cert_info)[i]);
157       (certinfo->cert_info)[i] = NULL;
158     }
159   }
160   free(certinfo);
161   certinfo = NULL;
162   return PMINFO_R_OK;
163 }
164
165 API int pkgmgrinfo_create_certinfo_set_handle(
166     pkgmgrinfo_instcertinfo_h *handle) {
167   retvm_if(handle == NULL, PMINFO_R_EINVAL,
168            "Argument supplied to hold return value is NULL\n");
169   pkgmgr_certinfo_x *certinfo = NULL;
170   *handle = NULL;
171   certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
172   retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
173   *handle = (void *)certinfo;
174   return PMINFO_R_OK;
175 }
176
177 API int pkgmgrinfo_set_cert_value(pkgmgrinfo_instcertinfo_h handle,
178                                   pkgmgrinfo_instcert_type cert_type,
179                                   char *cert_value) {
180   retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
181   retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
182   retvm_if(cert_type < PMINFO_SET_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL,
183            "Invalid certificate type\n");
184   retvm_if(cert_type > PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL,
185            "Invalid certificate type\n");
186   pkgmgr_certinfo_x *certinfo = NULL;
187   certinfo = (pkgmgr_certinfo_x *)handle;
188   if (certinfo->cert_info[cert_type]) free(certinfo->cert_info[cert_type]);
189   (certinfo->cert_info)[cert_type] = strdup(cert_value);
190   return PMINFO_R_OK;
191 }
192
193 API int pkgmgrinfo_save_certinfo(const char *pkgid,
194                                 pkgmgrinfo_instcertinfo_h handle, uid_t uid) {
195   if (pkgid == NULL || handle == NULL) {
196     _LOGE("invalid parameter");
197     return PMINFO_R_EINVAL;
198   }
199   pkgmgr_certinfo_x *certinfo = (pkgmgr_certinfo_x *)handle;
200   return _pkginfo_insert_certinfo(pkgid, certinfo, uid);
201 }
202
203 API int pkgmgrinfo_destroy_certinfo_set_handle(
204     pkgmgrinfo_instcertinfo_h handle) {
205   retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
206   int i = 0;
207   pkgmgr_certinfo_x *certinfo = NULL;
208   certinfo = (pkgmgr_certinfo_x *)handle;
209   if (certinfo->pkgid) {
210     free(certinfo->pkgid);
211     certinfo->pkgid = NULL;
212   }
213   for (i = 0; i < MAX_CERT_TYPE; i++) {
214     if ((certinfo->cert_info)[i]) {
215       free((certinfo->cert_info)[i]);
216       (certinfo->cert_info)[i] = NULL;
217     }
218   }
219   free(certinfo);
220   certinfo = NULL;
221   return PMINFO_R_OK;
222 }
223
224 API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid) {
225   if (pkgid == NULL) {
226     _LOGE("invalid parameter");
227     return PMINFO_R_EINVAL;
228   }
229
230   return _pkginfo_delete_certinfo(pkgid);
231 }
232
233 API int pkgmgrinfo_delete_certinfo(const char *pkgid) {
234   return pkgmgrinfo_delete_usr_certinfo(pkgid, _getuid());
235 }