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