Implement pkgmgrinfo_delete_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   // TODO: need to use pkginfo-client APIs
34   return 0;
35 #if 0
36   sqlite3 *db;
37   char *dbpath;
38
39   if (lhs_package_id == NULL || rhs_package_id == NULL ||
40       compare_result == NULL) {
41     _LOGE("invalid parameter");
42     return PMINFO_R_EINVAL;
43   }
44
45   /* open unified global cert db */
46   dbpath = getUserPkgCertDBPath();
47   if (dbpath == NULL) return PMINFO_R_ERROR;
48
49   ret = __open_db(dbpath, &db, SQLITE_OPEN_READONLY);
50   if (ret != SQLITE_OK) {
51     _LOGE("failed to open db: %d", ret);
52     free(dbpath);
53     return PMINFO_R_ERROR;
54   }
55   free(dbpath);
56
57   if (_pkginfo_compare_certinfo(db, lhs_package_id, rhs_package_id,
58                                 compare_result)) {
59     _LOGE("failed to compare certinfo");
60     sqlite3_close_v2(db);
61     return PMINFO_R_ERROR;
62   }
63
64   sqlite3_close_v2(db);
65
66   return PMINFO_R_OK;
67 #endif
68 }
69
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);
75 }
76
77 static int _pkginfo_get_pkgid_from_appid(uid_t uid, const char *appid,
78                                          char **pkgid) {
79   char *query = NULL;
80
81   query = sqlite3_mprintf(
82       "SELECT package FROM package_app_info WHERE app_id=%Q", appid);
83   if (query == NULL) {
84     _LOGE("Out of memory");
85     return -1;
86   }
87
88   // TODO: need to use pkginfo-client APIs
89
90   sqlite3_free(query);
91   return 0;
92 }
93
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) {
97   int ret;
98   char *l_pkgid = NULL;
99   char *r_pkgid = NULL;
100
101   if (lhs_app_id == NULL || rhs_app_id == NULL || compare_result == NULL) {
102     _LOGE("invalid parameter");
103     return PMINFO_R_EINVAL;
104   }
105
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);
109
110   if (ret != PMINFO_R_OK) return ret;
111
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);
115
116   if (ret != PMINFO_R_OK) {
117     free(l_pkgid);
118     return ret;
119   }
120
121   ret = pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(l_pkgid, r_pkgid, uid,
122                                                      compare_result);
123
124   free(l_pkgid);
125   free(r_pkgid);
126
127   return ret;
128 }
129
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);
135 }
136
137 static int _pkginfo_get_certinfo(const char *pkgid, pkgmgr_certinfo_x *info) {
138
139   // TODO: need to use pkginfo-client APIs
140
141   return PMINFO_R_OK;
142 }
143
144 API int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid,
145                                          pkgmgrinfo_certinfo_h handle,
146                                          uid_t uid) {
147   int ret;
148   pkgmgr_certinfo_x *info = (pkgmgr_certinfo_x *)handle;
149
150   if (pkgid == NULL || handle == NULL) {
151     _LOGE("invalid parameter");
152     return PMINFO_R_EINVAL;
153   }
154
155   ret = _pkginfo_get_certinfo(pkgid, info);
156   if (ret != PMINFO_R_OK) _LOGE("failed to get certinfo of %s ", pkgid);
157
158   return ret;
159 }
160
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];
174   else
175     *cert_value = NULL;
176   return PMINFO_R_OK;
177 }
178
179 API int pkgmgrinfo_pkginfo_destroy_certinfo(pkgmgrinfo_certinfo_h handle) {
180   retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
181   int i = 0;
182   pkgmgr_certinfo_x *certinfo = NULL;
183   certinfo = (pkgmgr_certinfo_x *)handle;
184   if (certinfo->pkgid) {
185     free(certinfo->pkgid);
186     certinfo->pkgid = NULL;
187   }
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;
192     }
193   }
194   free(certinfo);
195   certinfo = NULL;
196   return PMINFO_R_OK;
197 }
198
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;
204   *handle = NULL;
205   certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
206   retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
207   *handle = (void *)certinfo;
208   return PMINFO_R_OK;
209 }
210
211 API int pkgmgrinfo_set_cert_value(pkgmgrinfo_instcertinfo_h handle,
212                                   pkgmgrinfo_instcert_type cert_type,
213                                   char *cert_value) {
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);
224   return PMINFO_R_OK;
225 }
226
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;
232   }
233
234   // TODO: database should be created?
235   //_check_create_cert_db();
236
237   // TODO: use pkginfo-client APIs
238
239   return PMINFO_R_OK;
240 }
241
242 API int pkgmgrinfo_destroy_certinfo_set_handle(
243     pkgmgrinfo_instcertinfo_h handle) {
244   retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
245   int i = 0;
246   pkgmgr_certinfo_x *certinfo = NULL;
247   certinfo = (pkgmgr_certinfo_x *)handle;
248   if (certinfo->pkgid) {
249     free(certinfo->pkgid);
250     certinfo->pkgid = NULL;
251   }
252   for (i = 0; i < MAX_CERT_TYPE; i++) {
253     if ((certinfo->cert_info)[i]) {
254       free((certinfo->cert_info)[i]);
255       (certinfo->cert_info)[i] = NULL;
256     }
257   }
258   free(certinfo);
259   certinfo = NULL;
260   return PMINFO_R_OK;
261 }
262
263 API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid) {
264   if (pkgid == NULL) {
265     _LOGE("invalid parameter");
266     return PMINFO_R_EINVAL;
267   }
268
269   return _pkginfo_delete_certinfo(pkgid);
270 }
271
272 API int pkgmgrinfo_delete_certinfo(const char *pkgid) {
273   return pkgmgrinfo_delete_usr_certinfo(pkgid, _getuid());
274 }