Release version 0.24.4
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_certinfo.c
index 225b8b9..8024fc2 100644 (file)
 #include <unistd.h>
 #include <sys/types.h>
 
-#include <sqlite3.h>
 #include <glib.h>
 
+#include "manager/pkginfo_manager.h"
 #include "pkgmgr-info.h"
 #include "pkgmgrinfo_debug.h"
-#include "pkgmgrinfo_internal.h"
 #include "pkgmgrinfo_private.h"
-#include "pkgmgr_parser.h"
 
-API int pkgmgrinfo_pkginfo_create_certinfo(pkgmgrinfo_certinfo_h *handle) {
-  retvm_if(handle == NULL, PMINFO_R_EINVAL,
-           "Argument supplied to hold return value is NULL\n");
-  pkgmgr_certinfo_x *certinfo = NULL;
-  certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
-  *handle = NULL;
-  retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
-  *handle = (void *)certinfo;
-  return PMINFO_R_OK;
-}
-
-
-API int pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(
-    const char *lhs_package_id, const char *rhs_package_id, uid_t uid,
-    pkgmgrinfo_cert_compare_result_type_e *compare_result) {
-  // TODO: need to use pkginfo-client APIs
-  return 0;
-#if 0
-  sqlite3 *db;
-  char *dbpath;
-
-  if (lhs_package_id == NULL || rhs_package_id == NULL ||
-      compare_result == NULL) {
-    _LOGE("invalid parameter");
-    return PMINFO_R_EINVAL;
-  }
-
-  /* open unified global cert db */
-  dbpath = getUserPkgCertDBPath();
-  if (dbpath == NULL) return PMINFO_R_ERROR;
+API int pkgmgrinfo_pkginfo_create_certinfo(pkgmgrinfo_certinfo_h *handle)
+{
+       retvm_if(handle == NULL, PMINFO_R_EINVAL,
+                       "Argument supplied to hold return value is NULL\n");
+       pkgmgr_certinfo_x *certinfo = NULL;
 
-  ret = __open_db(dbpath, &db, SQLITE_OPEN_READONLY);
-  if (ret != SQLITE_OK) {
-    _LOGE("failed to open db: %d", ret);
-    free(dbpath);
-    return PMINFO_R_ERROR;
-  }
-  free(dbpath);
+       certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
+       *handle = NULL;
+       retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
+       *handle = (void *)certinfo;
+       return PMINFO_R_OK;
+}
 
-  if (_pkginfo_compare_certinfo(db, lhs_package_id, rhs_package_id,
-                                compare_result)) {
-    _LOGE("failed to compare certinfo");
-    sqlite3_close_v2(db);
-    return PMINFO_R_ERROR;
-  }
 
-  sqlite3_close_v2(db);
+API int pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(const char *lhs_package_id,
+               const char *rhs_package_id, uid_t uid,
+               pkgmgrinfo_cert_compare_result_type_e *compare_result)
+{
+       if (lhs_package_id == NULL || rhs_package_id == NULL ||
+                       compare_result == NULL) {
+               _LOGE("invalid parameter");
+               return PMINFO_R_EINVAL;
+       }
 
-  return PMINFO_R_OK;
-#endif
+       return _certinfo_compare_pkg_certinfo(lhs_package_id,
+                       rhs_package_id, compare_result);
 }
 
 API int pkgmgrinfo_pkginfo_compare_pkg_cert_info(
-    const char *lhs_package_id, const char *rhs_package_id,
-    pkgmgrinfo_cert_compare_result_type_e *compare_result) {
-  return pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(
-      lhs_package_id, rhs_package_id, _getuid(), compare_result);
-}
-
-static int _pkginfo_get_pkgid_from_appid(uid_t uid, const char *appid,
-                                         char **pkgid) {
-  char *query = NULL;
-
-  query = sqlite3_mprintf(
-      "SELECT package FROM package_app_info WHERE app_id=%Q", appid);
-  if (query == NULL) {
-    _LOGE("Out of memory");
-    return -1;
-  }
-
-  // TODO: need to use pkginfo-client APIs
-
-  sqlite3_free(query);
-  return 0;
+               const char *lhs_package_id, const char *rhs_package_id,
+               pkgmgrinfo_cert_compare_result_type_e *compare_result)
+{
+       return pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(lhs_package_id,
+                       rhs_package_id, _getuid(), compare_result);
 }
 
-API int pkgmgrinfo_pkginfo_compare_usr_app_cert_info(
-    const char *lhs_app_id, const char *rhs_app_id, uid_t uid,
-    pkgmgrinfo_cert_compare_result_type_e *compare_result) {
-  int ret;
-  char *l_pkgid = NULL;
-  char *r_pkgid = NULL;
-
-  if (lhs_app_id == NULL || rhs_app_id == NULL || compare_result == NULL) {
-    _LOGE("invalid parameter");
-    return PMINFO_R_EINVAL;
-  }
-
-  ret = _pkginfo_get_pkgid_from_appid(uid, lhs_app_id, &l_pkgid);
-  if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
-    ret = _pkginfo_get_pkgid_from_appid(GLOBAL_USER, lhs_app_id, &l_pkgid);
-
-  if (ret != PMINFO_R_OK) return ret;
-
-  ret = _pkginfo_get_pkgid_from_appid(uid, rhs_app_id, &r_pkgid);
-  if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
-    ret = _pkginfo_get_pkgid_from_appid(GLOBAL_USER, rhs_app_id, &r_pkgid);
-
-  if (ret != PMINFO_R_OK) {
-    free(l_pkgid);
-    return ret;
-  }
-
-  ret = pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(l_pkgid, r_pkgid, uid,
-                                                     compare_result);
-
-  free(l_pkgid);
-  free(r_pkgid);
+API int pkgmgrinfo_pkginfo_compare_usr_app_cert_info(const char *lhs_app_id,
+               const char *rhs_app_id, uid_t uid,
+               pkgmgrinfo_cert_compare_result_type_e *compare_result)
+{
+       if (lhs_app_id == NULL || rhs_app_id == NULL ||
+                       compare_result == NULL) {
+               _LOGE("invalid parameter");
+               return PMINFO_R_EINVAL;
+       }
 
-  return ret;
+       return _certinfo_compare_app_certinfo(uid,
+                       lhs_app_id, rhs_app_id, compare_result);
 }
 
-API int pkgmgrinfo_pkginfo_compare_app_cert_info(
-    const char *lhs_app_id, const char *rhs_app_id,
-    pkgmgrinfo_cert_compare_result_type_e *compare_result) {
-  return pkgmgrinfo_pkginfo_compare_usr_app_cert_info(
-      lhs_app_id, rhs_app_id, _getuid(), compare_result);
-}
-
-static int _pkginfo_get_certinfo(const char *pkgid, pkgmgr_certinfo_x *info) {
-
-  // TODO: need to use pkginfo-client APIs
-
-  return PMINFO_R_OK;
+API int pkgmgrinfo_pkginfo_compare_app_cert_info(const char *lhs_app_id,
+               const char *rhs_app_id,
+               pkgmgrinfo_cert_compare_result_type_e *compare_result)
+{
+       return pkgmgrinfo_pkginfo_compare_usr_app_cert_info(lhs_app_id,
+                       rhs_app_id, _getuid(), compare_result);
 }
 
 API int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid,
-                                         pkgmgrinfo_certinfo_h handle,
-                                         uid_t uid) {
-  int ret;
-  pkgmgr_certinfo_x *info = (pkgmgr_certinfo_x *)handle;
+               pkgmgrinfo_certinfo_h handle, uid_t uid)
+{
+       int ret;
 
-  if (pkgid == NULL || handle == NULL) {
-    _LOGE("invalid parameter");
-    return PMINFO_R_EINVAL;
-  }
+       if (pkgid == NULL || handle == NULL) {
+               _LOGE("invalid parameter");
+               return PMINFO_R_EINVAL;
+       }
 
-  ret = _pkginfo_get_certinfo(pkgid, info);
-  if (ret != PMINFO_R_OK) _LOGE("failed to get certinfo of %s ", pkgid);
+       ret = _pkginfo_get_certinfo(pkgid, handle, uid);
+       if (ret != PMINFO_R_OK)
+               _LOGE("failed to get certinfo of %s ", pkgid);
 
-  return ret;
+       return ret;
 }
 
 API int pkgmgrinfo_pkginfo_get_cert_value(pkgmgrinfo_certinfo_h handle,
-                                          pkgmgrinfo_cert_type cert_type,
-                                          const char **cert_value) {
-  retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
-  retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
-  retvm_if(cert_type < PMINFO_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL,
-           "Invalid certificate type\n");
-  retvm_if(cert_type > PMINFO_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL,
-           "Invalid certificate type\n");
-  pkgmgr_certinfo_x *certinfo = NULL;
-  certinfo = (pkgmgr_certinfo_x *)handle;
-  if ((certinfo->cert_info)[cert_type])
-    *cert_value = (certinfo->cert_info)[cert_type];
-  else
-    *cert_value = NULL;
-  return PMINFO_R_OK;
-}
-
-API int pkgmgrinfo_pkginfo_destroy_certinfo(pkgmgrinfo_certinfo_h handle) {
-  retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
-  int i = 0;
-  pkgmgr_certinfo_x *certinfo = NULL;
-  certinfo = (pkgmgr_certinfo_x *)handle;
-  if (certinfo->pkgid) {
-    free(certinfo->pkgid);
-    certinfo->pkgid = NULL;
-  }
-  for (i = 0; i < MAX_CERT_TYPE; i++) {
-    if ((certinfo->cert_info)[i]) {
-      free((certinfo->cert_info)[i]);
-      (certinfo->cert_info)[i] = NULL;
-    }
-  }
-  free(certinfo);
-  certinfo = NULL;
-  return PMINFO_R_OK;
+               pkgmgrinfo_cert_type cert_type, const char **cert_value)
+{
+       retvm_if(handle == NULL, PMINFO_R_EINVAL,
+                       "Argument supplied is NULL\n");
+       retvm_if(cert_value == NULL, PMINFO_R_EINVAL,
+                       "Argument supplied is NULL\n");
+       retvm_if(cert_type < PMINFO_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL,
+                       "Invalid certificate type\n");
+       retvm_if(cert_type > PMINFO_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL,
+                       "Invalid certificate type\n");
+       pkgmgr_certinfo_x *certinfo = NULL;
+
+       certinfo = (pkgmgr_certinfo_x *)handle;
+       if ((certinfo->cert_info)[cert_type])
+               *cert_value = (certinfo->cert_info)[cert_type];
+       else
+               *cert_value = NULL;
+       return PMINFO_R_OK;
+}
+
+API int pkgmgrinfo_pkginfo_destroy_certinfo(pkgmgrinfo_certinfo_h handle)
+{
+       retvm_if(handle == NULL, PMINFO_R_EINVAL,
+                       "Argument supplied is NULL\n");
+       int i = 0;
+       pkgmgr_certinfo_x *certinfo = NULL;
+
+       certinfo = (pkgmgr_certinfo_x *)handle;
+       if (certinfo->pkgid) {
+               free(certinfo->pkgid);
+               certinfo->pkgid = NULL;
+       }
+       for (i = 0; i < MAX_CERT_TYPE; i++) {
+               if ((certinfo->cert_info)[i]) {
+                       free((certinfo->cert_info)[i]);
+                       (certinfo->cert_info)[i] = NULL;
+               }
+       }
+       free(certinfo);
+       certinfo = NULL;
+       return PMINFO_R_OK;
 }
 
 API int pkgmgrinfo_create_certinfo_set_handle(
-    pkgmgrinfo_instcertinfo_h *handle) {
-  retvm_if(handle == NULL, PMINFO_R_EINVAL,
-           "Argument supplied to hold return value is NULL\n");
-  pkgmgr_certinfo_x *certinfo = NULL;
-  *handle = NULL;
-  certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
-  retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
-  *handle = (void *)certinfo;
-  return PMINFO_R_OK;
+               pkgmgrinfo_instcertinfo_h *handle)
+{
+       retvm_if(handle == NULL, PMINFO_R_EINVAL,
+                       "Argument supplied to hold return value is NULL\n");
+       pkgmgr_certinfo_x *certinfo = NULL;
+       *handle = NULL;
+       certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
+       retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
+       *handle = (void *)certinfo;
+       return PMINFO_R_OK;
 }
 
 API int pkgmgrinfo_set_cert_value(pkgmgrinfo_instcertinfo_h handle,
-                                  pkgmgrinfo_instcert_type cert_type,
-                                  char *cert_value) {
-  retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
-  retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
-  retvm_if(cert_type < PMINFO_SET_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL,
-           "Invalid certificate type\n");
-  retvm_if(cert_type > PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL,
-           "Invalid certificate type\n");
-  pkgmgr_certinfo_x *certinfo = NULL;
-  certinfo = (pkgmgr_certinfo_x *)handle;
-  if (certinfo->cert_info[cert_type]) free(certinfo->cert_info[cert_type]);
-  (certinfo->cert_info)[cert_type] = strdup(cert_value);
-  return PMINFO_R_OK;
+               pkgmgrinfo_instcert_type cert_type, char *cert_value)
+{
+       retvm_if(handle == NULL, PMINFO_R_EINVAL,
+                       "Argument supplied is NULL\n");
+       retvm_if(cert_value == NULL, PMINFO_R_EINVAL,
+                       "Argument supplied is NULL\n");
+       retvm_if(cert_type < PMINFO_SET_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL,
+                       "Invalid certificate type\n");
+       retvm_if(cert_type > PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT,
+                       PMINFO_R_EINVAL, "Invalid certificate type\n");
+       pkgmgr_certinfo_x *certinfo = NULL;
+
+       certinfo = (pkgmgr_certinfo_x *)handle;
+       if (certinfo->cert_info[cert_type])
+               free(certinfo->cert_info[cert_type]);
+       (certinfo->cert_info)[cert_type] = strdup(cert_value);
+       return PMINFO_R_OK;
 }
 
 API int pkgmgrinfo_save_certinfo(const char *pkgid,
-                                pkgmgrinfo_instcertinfo_h handle, uid_t uid) {
-  pkgmgr_certinfo_x *info = (pkgmgr_certinfo_x *)handle;
-
-  if (pkgid == NULL || handle == NULL) {
-    _LOGE("invalid parameter");
-    return PMINFO_R_EINVAL;
-  }
-
-  // TODO: database should be created?
-  //_check_create_cert_db();
-
-  // TODO: use pkginfo-client APIs
-
-  return PMINFO_R_OK;
-}
-
-API int pkgmgrinfo_destroy_certinfo_set_handle(
-    pkgmgrinfo_instcertinfo_h handle) {
-  retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
-  int i = 0;
-  pkgmgr_certinfo_x *certinfo = NULL;
-  certinfo = (pkgmgr_certinfo_x *)handle;
-  if (certinfo->pkgid) {
-    free(certinfo->pkgid);
-    certinfo->pkgid = NULL;
-  }
-  for (i = 0; i < MAX_CERT_TYPE; i++) {
-    if ((certinfo->cert_info)[i]) {
-      free((certinfo->cert_info)[i]);
-      (certinfo->cert_info)[i] = NULL;
-    }
-  }
-  free(certinfo);
-  certinfo = NULL;
-  return PMINFO_R_OK;
-}
-
-API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid) {
-  if (pkgid == NULL) {
-    _LOGE("invalid parameter");
-    return PMINFO_R_EINVAL;
-  }
-
-  // TODO: use pkginfo-client APIs
-
-  return PMINFO_R_OK;
-}
-
-API int pkgmgrinfo_delete_certinfo(const char *pkgid) {
-  return pkgmgrinfo_delete_usr_certinfo(pkgid, _getuid());
+               pkgmgrinfo_instcertinfo_h handle, uid_t uid)
+{
+       if (pkgid == NULL || handle == NULL) {
+               _LOGE("invalid parameter");
+               return PMINFO_R_EINVAL;
+       }
+       pkgmgr_certinfo_x *certinfo = (pkgmgr_certinfo_x *)handle;
+
+       if (certinfo->pkgid == NULL) {
+               certinfo->pkgid = strdup(pkgid);
+               if (certinfo->pkgid == NULL) {
+                       _LOGE("Out of memory");
+                       return PMINFO_R_ERROR;
+               }
+       }
+       return _pkginfo_insert_certinfo(pkgid, certinfo, uid);
+}
+
+API int pkgmgrinfo_destroy_certinfo_set_handle(pkgmgrinfo_instcertinfo_h handle)
+{
+       retvm_if(handle == NULL, PMINFO_R_EINVAL,
+                       "Argument supplied is NULL\n");
+       int i = 0;
+       pkgmgr_certinfo_x *certinfo = NULL;
+
+       certinfo = (pkgmgr_certinfo_x *)handle;
+       if (certinfo->pkgid) {
+               free(certinfo->pkgid);
+               certinfo->pkgid = NULL;
+       }
+       for (i = 0; i < MAX_CERT_TYPE; i++) {
+               if ((certinfo->cert_info)[i]) {
+                       free((certinfo->cert_info)[i]);
+                       (certinfo->cert_info)[i] = NULL;
+               }
+       }
+       free(certinfo);
+       certinfo = NULL;
+       return PMINFO_R_OK;
+}
+
+API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid)
+{
+       if (pkgid == NULL) {
+               _LOGE("invalid parameter");
+               return PMINFO_R_EINVAL;
+       }
+
+       return _pkginfo_delete_certinfo(pkgid);
+}
+
+API int pkgmgrinfo_delete_certinfo(const char *pkgid)
+{
+       return pkgmgrinfo_delete_usr_certinfo(pkgid, _getuid());
 }