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