Fix pkgmgrinfo_pkginfo_load_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_pkg_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 API int pkgmgrinfo_pkginfo_compare_usr_app_cert_info(
51     const char *lhs_app_id, const char *rhs_app_id, uid_t uid,
52     pkgmgrinfo_cert_compare_result_type_e *compare_result) {
53   if (lhs_app_id == NULL || rhs_app_id == NULL || compare_result == NULL) {
54     _LOGE("invalid parameter");
55     return PMINFO_R_EINVAL;
56   }
57
58   return _certinfo_compare_app_certinfo(
59       uid, lhs_app_id, rhs_app_id, compare_result);
60 }
61
62 API int pkgmgrinfo_pkginfo_compare_app_cert_info(
63     const char *lhs_app_id, const char *rhs_app_id,
64     pkgmgrinfo_cert_compare_result_type_e *compare_result) {
65   return pkgmgrinfo_pkginfo_compare_usr_app_cert_info(
66       lhs_app_id, rhs_app_id, _getuid(), compare_result);
67 }
68
69 API int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid,
70                                          pkgmgrinfo_certinfo_h handle,
71                                          uid_t uid) {
72   int ret;
73
74   if (pkgid == NULL || handle == NULL) {
75     _LOGE("invalid parameter");
76     return PMINFO_R_EINVAL;
77   }
78
79   ret = _pkginfo_get_certinfo(pkgid, handle, uid);
80   if (ret != PMINFO_R_OK) _LOGE("failed to get certinfo of %s ", pkgid);
81
82   return ret;
83 }
84
85 API int pkgmgrinfo_pkginfo_get_cert_value(pkgmgrinfo_certinfo_h handle,
86                                           pkgmgrinfo_cert_type cert_type,
87                                           const char **cert_value) {
88   retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
89   retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
90   retvm_if(cert_type < PMINFO_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL,
91            "Invalid certificate type\n");
92   retvm_if(cert_type > PMINFO_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL,
93            "Invalid certificate type\n");
94   pkgmgr_certinfo_x *certinfo = NULL;
95   certinfo = (pkgmgr_certinfo_x *)handle;
96   if ((certinfo->cert_info)[cert_type])
97     *cert_value = (certinfo->cert_info)[cert_type];
98   else
99     *cert_value = NULL;
100   return PMINFO_R_OK;
101 }
102
103 API int pkgmgrinfo_pkginfo_destroy_certinfo(pkgmgrinfo_certinfo_h handle) {
104   retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
105   int i = 0;
106   pkgmgr_certinfo_x *certinfo = NULL;
107   certinfo = (pkgmgr_certinfo_x *)handle;
108   if (certinfo->pkgid) {
109     free(certinfo->pkgid);
110     certinfo->pkgid = NULL;
111   }
112   for (i = 0; i < MAX_CERT_TYPE; i++) {
113     if ((certinfo->cert_info)[i]) {
114       free((certinfo->cert_info)[i]);
115       (certinfo->cert_info)[i] = NULL;
116     }
117   }
118   free(certinfo);
119   certinfo = NULL;
120   return PMINFO_R_OK;
121 }
122
123 API int pkgmgrinfo_create_certinfo_set_handle(
124     pkgmgrinfo_instcertinfo_h *handle) {
125   retvm_if(handle == NULL, PMINFO_R_EINVAL,
126            "Argument supplied to hold return value is NULL\n");
127   pkgmgr_certinfo_x *certinfo = NULL;
128   *handle = NULL;
129   certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
130   retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
131   *handle = (void *)certinfo;
132   return PMINFO_R_OK;
133 }
134
135 API int pkgmgrinfo_set_cert_value(pkgmgrinfo_instcertinfo_h handle,
136                                   pkgmgrinfo_instcert_type cert_type,
137                                   char *cert_value) {
138   retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
139   retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
140   retvm_if(cert_type < PMINFO_SET_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL,
141            "Invalid certificate type\n");
142   retvm_if(cert_type > PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL,
143            "Invalid certificate type\n");
144   pkgmgr_certinfo_x *certinfo = NULL;
145   certinfo = (pkgmgr_certinfo_x *)handle;
146   if (certinfo->cert_info[cert_type]) free(certinfo->cert_info[cert_type]);
147   (certinfo->cert_info)[cert_type] = strdup(cert_value);
148   return PMINFO_R_OK;
149 }
150
151 API int pkgmgrinfo_save_certinfo(const char *pkgid,
152                                 pkgmgrinfo_instcertinfo_h handle, uid_t uid) {
153   if (pkgid == NULL || handle == NULL) {
154     _LOGE("invalid parameter");
155     return PMINFO_R_EINVAL;
156   }
157   pkgmgr_certinfo_x *certinfo = (pkgmgr_certinfo_x *)handle;
158   if (certinfo->pkgid == NULL) {
159     certinfo->pkgid = strdup(pkgid);
160     if (certinfo->pkgid == NULL) {
161       _LOGE("Out of memory");
162       return PMINFO_R_ERROR;
163     }
164   }
165   return _pkginfo_insert_certinfo(pkgid, certinfo, uid);
166 }
167
168 API int pkgmgrinfo_destroy_certinfo_set_handle(
169     pkgmgrinfo_instcertinfo_h handle) {
170   retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
171   int i = 0;
172   pkgmgr_certinfo_x *certinfo = NULL;
173   certinfo = (pkgmgr_certinfo_x *)handle;
174   if (certinfo->pkgid) {
175     free(certinfo->pkgid);
176     certinfo->pkgid = NULL;
177   }
178   for (i = 0; i < MAX_CERT_TYPE; i++) {
179     if ((certinfo->cert_info)[i]) {
180       free((certinfo->cert_info)[i]);
181       (certinfo->cert_info)[i] = NULL;
182     }
183   }
184   free(certinfo);
185   certinfo = NULL;
186   return PMINFO_R_OK;
187 }
188
189 API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid) {
190   if (pkgid == NULL) {
191     _LOGE("invalid parameter");
192     return PMINFO_R_EINVAL;
193   }
194
195   return _pkginfo_delete_certinfo(pkgid);
196 }
197
198 API int pkgmgrinfo_delete_certinfo(const char *pkgid) {
199   return pkgmgrinfo_delete_usr_certinfo(pkgid, _getuid());
200 }