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