Add missing filter checker
[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 <glib.h>
9
10 #include "manager/pkginfo_manager.h"
11 #include "pkgmgr-info.h"
12 #include "pkgmgrinfo_debug.h"
13 #include "pkgmgrinfo_private.h"
14 #include "pkgmgr_parser.h"
15
16 API int pkgmgrinfo_pkginfo_create_certinfo(pkgmgrinfo_certinfo_h *handle)
17 {
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
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(const char *lhs_package_id,
31                 const char *rhs_package_id, uid_t uid,
32                 pkgmgrinfo_cert_compare_result_type_e *compare_result)
33 {
34         if (lhs_package_id == NULL || rhs_package_id == NULL ||
35                         compare_result == NULL) {
36                 _LOGE("invalid parameter");
37                 return PMINFO_R_EINVAL;
38         }
39
40         return _certinfo_compare_pkg_certinfo(lhs_package_id,
41                         rhs_package_id, compare_result);
42 }
43
44 API int pkgmgrinfo_pkginfo_compare_pkg_cert_info(
45                 const char *lhs_package_id, const char *rhs_package_id,
46                 pkgmgrinfo_cert_compare_result_type_e *compare_result)
47 {
48         return pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(lhs_package_id,
49                         rhs_package_id, _getuid(), compare_result);
50 }
51
52 API int pkgmgrinfo_pkginfo_compare_usr_app_cert_info(const char *lhs_app_id,
53                 const char *rhs_app_id, uid_t uid,
54                 pkgmgrinfo_cert_compare_result_type_e *compare_result)
55 {
56         if (lhs_app_id == NULL || rhs_app_id == NULL ||
57                         compare_result == NULL) {
58                 _LOGE("invalid parameter");
59                 return PMINFO_R_EINVAL;
60         }
61
62         return _certinfo_compare_app_certinfo(uid,
63                         lhs_app_id, rhs_app_id, compare_result);
64 }
65
66 API int pkgmgrinfo_pkginfo_compare_app_cert_info(const char *lhs_app_id,
67                 const char *rhs_app_id,
68                 pkgmgrinfo_cert_compare_result_type_e *compare_result)
69 {
70         return pkgmgrinfo_pkginfo_compare_usr_app_cert_info(lhs_app_id,
71                         rhs_app_id, _getuid(), compare_result);
72 }
73
74 API int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid,
75                 pkgmgrinfo_certinfo_h handle, uid_t uid)
76 {
77         int ret;
78
79         if (pkgid == NULL || handle == NULL) {
80                 _LOGE("invalid parameter");
81                 return PMINFO_R_EINVAL;
82         }
83
84         ret = _pkginfo_get_certinfo(pkgid, handle, uid);
85         if (ret != PMINFO_R_OK)
86                 _LOGE("failed to get certinfo of %s ", pkgid);
87
88         return ret;
89 }
90
91 API int pkgmgrinfo_pkginfo_get_cert_value(pkgmgrinfo_certinfo_h handle,
92                 pkgmgrinfo_cert_type cert_type, const char **cert_value)
93 {
94         retvm_if(handle == NULL, PMINFO_R_EINVAL,
95                         "Argument supplied is NULL\n");
96         retvm_if(cert_value == NULL, PMINFO_R_EINVAL,
97                         "Argument supplied is NULL\n");
98         retvm_if(cert_type < PMINFO_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL,
99                         "Invalid certificate type\n");
100         retvm_if(cert_type > PMINFO_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL,
101                         "Invalid certificate type\n");
102         pkgmgr_certinfo_x *certinfo = NULL;
103
104         certinfo = (pkgmgr_certinfo_x *)handle;
105         if ((certinfo->cert_info)[cert_type])
106                 *cert_value = (certinfo->cert_info)[cert_type];
107         else
108                 *cert_value = NULL;
109         return PMINFO_R_OK;
110 }
111
112 API int pkgmgrinfo_pkginfo_destroy_certinfo(pkgmgrinfo_certinfo_h handle)
113 {
114         retvm_if(handle == NULL, PMINFO_R_EINVAL,
115                         "Argument supplied is NULL\n");
116         int i = 0;
117         pkgmgr_certinfo_x *certinfo = NULL;
118
119         certinfo = (pkgmgr_certinfo_x *)handle;
120         if (certinfo->pkgid) {
121                 free(certinfo->pkgid);
122                 certinfo->pkgid = NULL;
123         }
124         for (i = 0; i < MAX_CERT_TYPE; i++) {
125                 if ((certinfo->cert_info)[i]) {
126                         free((certinfo->cert_info)[i]);
127                         (certinfo->cert_info)[i] = NULL;
128                 }
129         }
130         free(certinfo);
131         certinfo = NULL;
132         return PMINFO_R_OK;
133 }
134
135 API int pkgmgrinfo_create_certinfo_set_handle(
136                 pkgmgrinfo_instcertinfo_h *handle)
137 {
138         retvm_if(handle == NULL, PMINFO_R_EINVAL,
139                         "Argument supplied to hold return value is NULL\n");
140         pkgmgr_certinfo_x *certinfo = NULL;
141         *handle = NULL;
142         certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
143         retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
144         *handle = (void *)certinfo;
145         return PMINFO_R_OK;
146 }
147
148 API int pkgmgrinfo_set_cert_value(pkgmgrinfo_instcertinfo_h handle,
149                 pkgmgrinfo_instcert_type cert_type, char *cert_value)
150 {
151         retvm_if(handle == NULL, PMINFO_R_EINVAL,
152                         "Argument supplied is NULL\n");
153         retvm_if(cert_value == NULL, PMINFO_R_EINVAL,
154                         "Argument supplied is NULL\n");
155         retvm_if(cert_type < PMINFO_SET_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL,
156                         "Invalid certificate type\n");
157         retvm_if(cert_type > PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT,
158                         PMINFO_R_EINVAL, "Invalid certificate type\n");
159         pkgmgr_certinfo_x *certinfo = NULL;
160
161         certinfo = (pkgmgr_certinfo_x *)handle;
162         if (certinfo->cert_info[cert_type])
163                 free(certinfo->cert_info[cert_type]);
164         (certinfo->cert_info)[cert_type] = strdup(cert_value);
165         return PMINFO_R_OK;
166 }
167
168 API int pkgmgrinfo_save_certinfo(const char *pkgid,
169                 pkgmgrinfo_instcertinfo_h handle, uid_t uid)
170 {
171         if (pkgid == NULL || handle == NULL) {
172                 _LOGE("invalid parameter");
173                 return PMINFO_R_EINVAL;
174         }
175         pkgmgr_certinfo_x *certinfo = (pkgmgr_certinfo_x *)handle;
176
177         if (certinfo->pkgid == NULL) {
178                 certinfo->pkgid = strdup(pkgid);
179                 if (certinfo->pkgid == NULL) {
180                         _LOGE("Out of memory");
181                         return PMINFO_R_ERROR;
182                 }
183         }
184         return _pkginfo_insert_certinfo(pkgid, certinfo, uid);
185 }
186
187 API int pkgmgrinfo_destroy_certinfo_set_handle(pkgmgrinfo_instcertinfo_h handle)
188 {
189         retvm_if(handle == NULL, PMINFO_R_EINVAL,
190                         "Argument supplied is NULL\n");
191         int i = 0;
192         pkgmgr_certinfo_x *certinfo = NULL;
193
194         certinfo = (pkgmgr_certinfo_x *)handle;
195         if (certinfo->pkgid) {
196                 free(certinfo->pkgid);
197                 certinfo->pkgid = NULL;
198         }
199         for (i = 0; i < MAX_CERT_TYPE; i++) {
200                 if ((certinfo->cert_info)[i]) {
201                         free((certinfo->cert_info)[i]);
202                         (certinfo->cert_info)[i] = NULL;
203                 }
204         }
205         free(certinfo);
206         certinfo = NULL;
207         return PMINFO_R_OK;
208 }
209
210 API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid)
211 {
212         if (pkgid == NULL) {
213                 _LOGE("invalid parameter");
214                 return PMINFO_R_EINVAL;
215         }
216
217         return _pkginfo_delete_certinfo(pkgid);
218 }
219
220 API int pkgmgrinfo_delete_certinfo(const char *pkgid)
221 {
222         return pkgmgrinfo_delete_usr_certinfo(pkgid, _getuid());
223 }