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