From 4a67c30be6dc4fcc6e3662a32c98a28dcdf8b0ab Mon Sep 17 00:00:00 2001 From: "jiseob.jang" Date: Mon, 23 Mar 2015 21:47:33 +0900 Subject: [PATCH 02/11] merged a service daemon of daemonized account from tizen_2.4. Change-Id: Ic90c628a72a255a0fbde9faa11f90debb7fec10e Signed-off-by: jiseob.jang --- CMakeLists.txt | 11 + INSTALL | 33 + LICENSE | 206 ++ libaccounts-svc.manifest | 29 + packaging/account-manager.spec | 128 + packaging/accounts-service.service | 16 + src/accounts/CMakeLists.txt | 1 + src/accounts/account_mgr.xml | 166 + src/accounts/server/CMakeLists.txt | 30 + src/accounts/server/account-server-db.c | 6082 +++++++++++++++++++++++++++++++ src/accounts/server/account-server-db.h | 131 + src/accounts/server/account-server.c | 2139 +++++++++++ src/accounts/server/account-svcd.sh | 1 + 13 files changed, 8973 insertions(+) create mode 100755 CMakeLists.txt create mode 100755 INSTALL create mode 100755 LICENSE create mode 100755 libaccounts-svc.manifest create mode 100755 packaging/account-manager.spec create mode 100644 packaging/accounts-service.service create mode 100755 src/accounts/CMakeLists.txt create mode 100644 src/accounts/account_mgr.xml create mode 100644 src/accounts/server/CMakeLists.txt create mode 100644 src/accounts/server/account-server-db.c create mode 100644 src/accounts/server/account-server-db.h create mode 100644 src/accounts/server/account-server.c create mode 100755 src/accounts/server/account-svcd.sh diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100755 index 0000000..64ce5f5 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,11 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(account-manager C) + +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +SET(EXEC_PREFIX "\${prefix}") +SET(LIBDIR "\${prefix}/lib") +SET(INCLUDEDIR "\${prefix}/include ") +SET(VERSION_MAJOR 0) +SET(VERSION "${VERSION_MAJOR}.0.1") + +ADD_SUBDIRECTORY(src/accounts) diff --git a/INSTALL b/INSTALL new file mode 100755 index 0000000..ddf4e78 --- /dev/null +++ b/INSTALL @@ -0,0 +1,33 @@ +1. make the build directory + + ex) + + $ mkdir build + + +2. change the working directory to the build directory + + ex) + + $ cd build + + +3. run 'cmake' + + $ cmake ${SOURCE_DIR} -DCMAKE_INSTALL_PREFIX=/usr + + ex) + + $ cmake .. -DCMAKE_INSTALL_PREFIX=/usr + + or + + $ cmake .. + + +4. make & make install + + ex) + + $ make -j 2 && make install + diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..f94008a --- /dev/null +++ b/LICENSE @@ -0,0 +1,206 @@ +Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + + diff --git a/libaccounts-svc.manifest b/libaccounts-svc.manifest new file mode 100755 index 0000000..7232dc1 --- /dev/null +++ b/libaccounts-svc.manifest @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/packaging/account-manager.spec b/packaging/account-manager.spec new file mode 100755 index 0000000..989b243 --- /dev/null +++ b/packaging/account-manager.spec @@ -0,0 +1,128 @@ + +Name: account-manager +Summary: Account Manager +Version: 0.0.1 +Release: 1 +Group: Social & Content/Other +License: Apache-2.0 +Source0: account-manager-%{version}.tar.gz +Source1: accounts-service.service + +BuildRequires: cmake +BuildRequires: pkgconfig(sqlite3) +BuildRequires: pkgconfig(glib-2.0) +BuildRequires: pkgconfig(dlog) +BuildRequires: pkgconfig(db-util) +BuildRequires: pkgconfig(dbus-1) +BuildRequires: pkgconfig(capi-base-common) +BuildRequires: pkgconfig(pkgmgr-info) +BuildRequires: pkgconfig(aul) +BuildRequires: pkgconfig(glib-2.0) >= 2.26 +BuildRequires: pkgconfig(gio-2.0) +BuildRequires: pkgconfig(gio-unix-2.0) +BuildRequires: pkgconfig(security-server) +BuildRequires: pkgconfig(accounts-svc) +BuildRequires: dbus-python +BuildRequires: python-xml +BuildRequires: python-devel + +Requires(post): /sbin/ldconfig +Requires(post): /usr/bin/sqlite3 +Requires(postun): /sbin/ldconfig + +%description +Account Daemon: no + +%package devel +Summary: Development files for %{name} +Group: Development/Libraries +Requires: %{name} = %{version}-%{release} +%description devel +Development files for %{name} + +%prep +%setup -q + +%build +#export CFLAGS+=" -Wextra -Wcast-align -Wcast-qual -Wshadow -Wwrite-strings -Wswitch-default" +#export CXXFLAGS+=" -Wextra -Wcast-align -Wcast-qual -Wshadow -Wwrite-strings -Wswitch-default -Wnon-virtual-dtor -Wno-c++0x-compat" +#export CFLAGS+=" -Wno-unused-parameter -Wno-empty-body" +#export CXXFLAGS+=" -Wno-unused-parameter -Wno-empty-body" + +#export CFLAGS+=" -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-strict-aliasing -fno-unroll-loops -fsigned-char -fstrict-overflow -fno-common" +#export CXXFLAGS+=" -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-strict-aliasing -fno-unroll-loops -fsigned-char -fstrict-overflow" + +export CFLAGS="${CFLAGS} -fPIC -fvisibility=hidden" +cmake . -DCMAKE_INSTALL_PREFIX=/usr + +make %{?jobs:-j%jobs} + +%install +rm -rf %{buildroot} +%make_install + +mkdir -p %{buildroot}%{_libdir}/systemd/system/multi-user.target.wants +install -m 0644 %SOURCE1 %{buildroot}%{_libdir}/systemd/system/accounts-service.service +ln -s ../accounts-service.service %{buildroot}%{_libdir}/systemd/system/multi-user.target.wants/accounts-service.service + +rm -rf %{buildroot}/usr/lib/account-manager + +%post +/sbin/ldconfig +if [ ! -d /opt/usr/dbspace ] +then + mkdir -p /opt/usr/dbspace +fi +if [ ! -f /opt/usr/dbspace/.account.db ] +#rm -rf /opt/usr/dbspace/.account.db* +then + sqlite3 /opt/usr/dbspace/.account.db 'PRAGMA journal_mode = PERSIST; + CREATE TABLE if not exists label (AppId TEXT, Label TEXT, Locale TEXT); + CREATE TABLE if not exists account_type (_id INTEGER PRIMARY KEY AUTOINCREMENT, AppId TEXT, + ServiceProviderId TEXT, IconPath TEXT, SmallIconPath TEXT, MultipleAccountSupport INT); + CREATE TABLE if not exists account_custom (AccountId INTEGER, AppId TEXT, Key TEXT, Value TEXT); + CREATE TABLE if not exists account (id INTEGER PRIMARY KEY AUTOINCREMENT, user_name TEXT, email_address TEXT, display_name TEXT, icon_path TEXT, + source TEXT, package_name TEXT, access_token TEXT, domain_name TEXT, auth_type INTEGER, secret INTEGER, sync_support INTEGER, + txt_custom0 TEXT, txt_custom1 TEXT, txt_custom2 TEXT, txt_custom3 TEXT, txt_custom4 TEXT, + int_custom0 INTEGER, int_custom1 INTEGER, int_custom2 INTEGER, int_custom3 INTEGER, int_custom4 INTEGER); + CREATE TABLE if not exists capability (_id INTEGER PRIMARY KEY AUTOINCREMENT, key TEXT, value INTEGER, + package_name TEXT, user_name TEXT, account_id INTEGER, FOREIGN KEY (account_id) REFERENCES account(id)); + CREATE TABLE if not exists provider_feature (app_id TEXT, key TEXT); +' +fi + +mkdir -p /opt/usr/share/account +chown system:system /opt/usr/dbspace/.account.db +chown system:system /opt/usr/dbspace/.account.db-journal + +chmod 600 /opt/usr/dbspace/.account.db +chmod 600 /opt/usr/dbspace/.account.db-journal + +#set message key value to NULL +#vconftool set -t string db/account/msg '' -g 6514 +vconftool set -tf string db/account/msg '' -s libaccounts-svc -u 200 -g 5000 + +#smack labeling +if [ -f /usr/lib/rpm-plugins/msm.so ] +then + chsmack -a 'libaccounts-svc::db' /opt/usr/dbspace/.account.db-journal + chsmack -a 'libaccounts-svc::db' /opt/usr/dbspace/.account.db +fi + + +%postun -p /sbin/ldconfig + + + +%files +%manifest libaccounts-svc.manifest +%defattr(-,system,system,-) +%attr(0700,system,system) %{_bindir}/account-svcd +%attr(0600,system,system) %{_libdir}/systemd/system/accounts-service.service +%attr(0600,system,system) %{_libdir}/systemd/system/multi-user.target.wants/accounts-service.service + +%files devel +%defattr(-,system,system,-) +%attr(0700,system,system) %{_bindir}/account-svcd +%attr(0600,system,system) %{_libdir}/systemd/system/accounts-service.service +%attr(0600,system,system) %{_libdir}/systemd/system/multi-user.target.wants/accounts-service.service diff --git a/packaging/accounts-service.service b/packaging/accounts-service.service new file mode 100644 index 0000000..5d16913 --- /dev/null +++ b/packaging/accounts-service.service @@ -0,0 +1,16 @@ +[Unit] +Description=Accounts service +After=tizen-runtime.target +Requires=tizen-runtime.target + +[Service] +User=system +Gruop=system +SmackProcessLabel=libaccounts-svc +Type=simple +ExecStart=/usr/bin/account-svcd +Restart=always +RestartSec=1 + +[Install] +WantedBy=multi-user.target diff --git a/src/accounts/CMakeLists.txt b/src/accounts/CMakeLists.txt new file mode 100755 index 0000000..7c2af46 --- /dev/null +++ b/src/accounts/CMakeLists.txt @@ -0,0 +1 @@ +ADD_SUBDIRECTORY(server) diff --git a/src/accounts/account_mgr.xml b/src/accounts/account_mgr.xml new file mode 100644 index 0000000..32cf6bf --- /dev/null +++ b/src/accounts/account_mgr.xml @@ -0,0 +1,166 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/accounts/server/CMakeLists.txt b/src/accounts/server/CMakeLists.txt new file mode 100644 index 0000000..2272a1f --- /dev/null +++ b/src/accounts/server/CMakeLists.txt @@ -0,0 +1,30 @@ +SET(DAEMON account-svcd) + +INCLUDE(FindPkgConfig) +pkg_check_modules(pkgs REQUIRED python sqlite3 dlog db-util glib-2.0 gobject-2.0 capi-base-common pkgmgr-info aul dbus-1 gio-2.0 gio-unix-2.0 security-server accounts-svc) + +FOREACH(flag ${pkgs_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(SERVER_SRCS + account-server.c + account-server-db.c +) + +ADD_CUSTOM_COMMAND( + WORKING_DIRECTORY + OUTPUT account-mgr-stub.c + COMMAND gdbus-codegen --interface-prefix org.tizen. + --generate-c-code account-mgr-stub ../account_mgr.xml + COMMENT "Generating Account SVC GDBus .c/.h") + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall -Werror") +SET(CMAKE_LDFLAGS "-Wl,-zdefs") + +ADD_EXECUTABLE(${DAEMON} ${SERVER_SRCS} account-mgr-stub.c) + +TARGET_LINK_LIBRARIES(${DAEMON} ${pkgs_LDFLAGS}) + +INSTALL(TARGETS ${DAEMON} DESTINATION bin) + diff --git a/src/accounts/server/account-server-db.c b/src/accounts/server/account-server-db.c new file mode 100644 index 0000000..39d4ac1 --- /dev/null +++ b/src/accounts/server/account-server-db.c @@ -0,0 +1,6082 @@ +/* + * + * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "account-private.h" +#include "account-server-db.h" +#include "dbg.h" +#include "account_ipc_marshal.h" + +typedef sqlite3_stmt* account_stmt; + +#define TEST_APP_ID "org.tizen.MyAccountCoreTest" +#define EAS_CMDLINE "/usr/bin/eas-engine" +#define EMAIL_SERVICE_CMDLINE "/usr/bin/email-service" +#define IMS_ENGINE_CMDLINE "/usr/bin/ims-srv" +#define IMS_AGENT_CMDLINE "/usr/bin/ims-agent" +#define MDM_SERVER_CMDLINE "/usr/bin/mdm-server" + +#define RCS_APPID "com.samsung.rcs-im" +#define IMS_SERVICE_APPID "ims-service" +#define ACTIVESYNC_APPID "activesync-ui" +#define EMAIL_APPID "email-setting-efl" +#define SYNCHRONISE_APPID "setting-synchronise-efl" +#define DS_AGENT_CMDLINE "/usr/bin/oma-ds-agent" + +#define FACEBOOK_SDK_APPID "com.samsung.facebook-service" +#define FACEBOOK_APPID "com.samsung.facebook" + +#define ACCOUNT_DB_OPEN_READONLY 0 +#define ACCOUNT_DB_OPEN_READWRITE 1 + +#define _TIZEN_PUBLIC_ +#ifndef _TIZEN_PUBLIC_ +#include + +#endif + +static sqlite3* g_hAccountDB = NULL; +static sqlite3* g_hAccountDB2 = NULL; +pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER; + +static char *_account_get_text(const char *text_data); +static int _account_gslist_free(GSList* list); +static int _account_glist_free(GList* list); +static const char *_account_query_table_column_text(account_stmt pStmt, int pos); +static int _account_insert_custom(account_s *account, int account_id); +static int _account_update_custom(account_s *account, int account_id); +static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data ); +static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id); + +int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data ); + +static void _account_insert_delete_update_notification_send(char *noti_name) +{ + if (!noti_name) { + _ERR("Noti Name is NULL!!!!!!\n"); + return; + } + + _INFO("noti_type = %s", noti_name); + + if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) != 0) { + _ERR("Vconf MSG Str set FAILED !!!!!!\n");; + } +} + +int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data) +{ + char* appid = NULL; + char* item = NULL; + GSList** appid_list = (GSList**)user_data; + int pkgmgr_ret = -1; + + pkgmgr_ret = pkgmgrinfo_appinfo_get_appid(handle, &appid); + + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appid(%d)", pkgmgr_ret); + } + + item = _account_get_text(appid); + *appid_list = g_slist_append(*appid_list, item); + + return 0; +} + +static inline int __read_proc(const char *path, char *buf, int size) +{ + int fd = 0, ret = 0; + + if (buf == NULL || path == NULL) { + ACCOUNT_ERROR("path and buffer is mandatory\n"); + return -1; + } + + fd = open(path, O_RDONLY); + if (fd < 0) { + ACCOUNT_ERROR("fd open error(%d)\n", fd); + return -1; + } + + ret = read(fd, buf, size - 1); + if (ret <= 0) { + ACCOUNT_ERROR("fd read error(%d)\n", fd); + close(fd); + return -1; + } else + buf[ret] = 0; + + close(fd); + + return ret; +} + +char *_account_get_proc_cmdline_bypid(int pid) +{ + char buf[128]; + int ret = 0; + + ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid); + ret = __read_proc(buf, buf, sizeof(buf)); + if (ret <= 0) { + ACCOUNT_DEBUG("No proc directory (%d)\n", pid); + return NULL; + } + + return strdup(buf); +} + + +static char* _account_get_current_appid(int pid) +{ + _INFO("getting caller appid with pid=[%d]", pid); + + int ret=0; + char appid[128]={0,}; + char* appid_ret = NULL; + + ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid)); + + if(ret < 0){ + ACCOUNT_ERROR("fail to get current appid\n"); + } + + _INFO(""); + + /* SLP platform core exception */ + if(strlen(appid) == 0){ + _INFO(""); + char* cmdline = NULL; + cmdline = _account_get_proc_cmdline_bypid(pid); + ACCOUNT_SLOGD("cmdline (%s)!!!!!!\n", cmdline); + if(!g_strcmp0(cmdline, EAS_CMDLINE)) { + appid_ret = _account_get_text(ACTIVESYNC_APPID); + _ACCOUNT_FREE(cmdline); + return appid_ret; + } else if (!g_strcmp0(cmdline, EMAIL_SERVICE_CMDLINE) || !g_strcmp0(cmdline, MDM_SERVER_CMDLINE)) { + appid_ret = _account_get_text(EMAIL_APPID); + _ACCOUNT_FREE(cmdline); + return appid_ret; + } else if (!g_strcmp0(cmdline, IMS_ENGINE_CMDLINE) || !g_strcmp0(cmdline, IMS_AGENT_CMDLINE)) { + if(_account_type_query_app_id_exist(RCS_APPID)==ACCOUNT_ERROR_NONE){ + appid_ret = _account_get_text(RCS_APPID); + } else if(_account_type_query_app_id_exist(IMS_SERVICE_APPID)==ACCOUNT_ERROR_NONE){ + appid_ret = _account_get_text(IMS_SERVICE_APPID); + } else { + appid_ret = _account_get_text(RCS_APPID); + } + _ACCOUNT_FREE(cmdline); + return appid_ret; + } else if (!g_strcmp0(cmdline, DS_AGENT_CMDLINE)) { + appid_ret = _account_get_text(SYNCHRONISE_APPID); + _ACCOUNT_FREE(cmdline); + return appid_ret; + } else { + ACCOUNT_DEBUG("No app id\n"); + _ACCOUNT_FREE(cmdline); + return NULL; + } + } + + _INFO(""); + /* temporary exception */ + if(!g_strcmp0(appid, "com.samsung.gallery")){ + appid_ret = _account_get_text("com.samsung.facebook"); + } else if(!g_strcmp0(appid, FACEBOOK_SDK_APPID)){ + appid_ret = _account_get_text(FACEBOOK_APPID); + } else { + appid_ret = _account_get_text(appid); + } + + return appid_ret; +} + +static int _account_check_account_type_with_appid_group(const char* appid, char** verified_appid) +{ + int error_code = ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER; + pkgmgrinfo_appinfo_h ahandle=NULL; + pkgmgrinfo_pkginfo_h phandle=NULL; + char* package_id = NULL; + GSList* appid_list = NULL; + GSList* iter = NULL; + + if(!appid){ + ACCOUNT_ERROR("input param is null\n"); + return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER; + } + + if(!verified_appid){ + ACCOUNT_ERROR("output param is null\n"); + return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER; + } + + if(!strcmp(appid, "com.samsung.setting")){ + ACCOUNT_DEBUG("Setting exception\n"); + *verified_appid = _account_get_text("com.samsung.setting"); + return ACCOUNT_ERROR_NONE; + } + + if(!strcmp(appid, "com.samsung.samsung-account-front")){ + ACCOUNT_DEBUG("Setting exception\n"); + *verified_appid = _account_get_text("com.samsung.samsung-account-front"); + return ACCOUNT_ERROR_NONE; + } + + if(!strcmp(appid, IMS_SERVICE_APPID) || !strcmp(appid, RCS_APPID)){ + ACCOUNT_DEBUG("ims service exception\n"); + *verified_appid = _account_get_text(appid); + return ACCOUNT_ERROR_NONE; + } + + /* Get app id family which is stored in account database */ + int pkgmgr_ret = -1; + pkgmgr_ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appinfo(%d)", pkgmgr_ret); + } + pkgmgr_ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_pkgid(%d)", pkgmgr_ret); + } + pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_get_pkginfo(%d)", pkgmgr_ret); + } + pkgmgr_ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_list(%d)", pkgmgr_ret); + } + + /* Compare current app id with the stored app id family */ + for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){ + char* tmp = (char*)iter->data; + if(tmp) { + if(_account_type_query_app_id_exist(tmp) == ACCOUNT_ERROR_NONE) { + *verified_appid = _account_get_text(tmp); + error_code = ACCOUNT_ERROR_NONE; + _ACCOUNT_FREE(tmp); + break; + } else { + ACCOUNT_SLOGD("not matched owner group app id(%s), current appid(%s)\n", tmp, appid); + } + } + _ACCOUNT_FREE(tmp); + } + + g_slist_free(appid_list); + pkgmgr_ret = pkgmgrinfo_pkginfo_destroy_pkginfo(phandle); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_destroy_pkginfo(%d)", pkgmgr_ret); + } + + pkgmgr_ret = pkgmgrinfo_appinfo_destroy_appinfo(ahandle); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_appinfo_destroy_appinfo(%d)", pkgmgr_ret); + } + + return error_code; +} + +static int _account_check_appid_group_with_package_name(const char* appid, char* package_name) +{ + int error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + pkgmgrinfo_appinfo_h ahandle=NULL; + pkgmgrinfo_pkginfo_h phandle=NULL; + char* package_id = NULL; + GSList* appid_list = NULL; + GSList* iter = NULL; + + if(!appid){ + ACCOUNT_ERROR("input param -appid is null\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if(!package_name){ + ACCOUNT_ERROR("input param - package name is null\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + /* ims-service Exception */ + if ( strcmp(appid, "ims-service") == 0 && strcmp(package_name, "ims-service") == 0 ) { + ACCOUNT_DEBUG("ims exception."); // TODO: NEED TO REMOVE, debug log. + return ACCOUNT_ERROR_NONE; + } + + /* Get app id family which is stored in account database */ + int pkgmgr_ret = -1; + pkgmgr_ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appinfo(%d)", pkgmgr_ret); + } + pkgmgr_ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_pkgid(%d)", pkgmgr_ret); + } + pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_get_pkginfo(%d)", pkgmgr_ret); + } + pkgmgr_ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_list(%d)", pkgmgr_ret); + } + + /* Compare current app id with the stored app id family */ + for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){ + char* tmp = (char*)iter->data; + if(tmp) { + //ACCOUNT_ERROR("tmp(%s)package_name(%s)\n\n", tmp, package_name); // TODO: NEED TO REMOVE, debug log. + if( strcmp(tmp, package_name) == 0) { + error_code = ACCOUNT_ERROR_NONE; + _ACCOUNT_FREE(tmp); + break; + } else if ( strcmp(tmp, "com.samsung.samsung-account-front") == 0 && + strcmp(package_name, "gr47by21a5.SamsungAccount") == 0 ) { + /* Samung Account Exception */ + error_code = ACCOUNT_ERROR_NONE; + _ACCOUNT_FREE(tmp); + break; + } else { + ACCOUNT_SLOGD("not matched owner group app id(%s), current appid(%s)\n", tmp, appid); + } + } + _ACCOUNT_FREE(tmp); + } + + g_slist_free(appid_list); + pkgmgr_ret = pkgmgrinfo_pkginfo_destroy_pkginfo(phandle); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_destroy_pkginfo(%d)", pkgmgr_ret); + } + + pkgmgr_ret = pkgmgrinfo_appinfo_destroy_appinfo(ahandle); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_appinfo_destroy_appinfo(%d)", pkgmgr_ret); + } + + return error_code; +} + +static int _remove_sensitive_info_from_non_owning_account(int caller_pid, account_s *account) +{ + if (account == NULL) + { + _ERR("Null input"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (account->package_name) + { + char *caller_package_name = _account_get_current_appid(caller_pid); + if (caller_package_name == NULL) + { + _ERR("Could not get caller app id, so removing sensitive info from account id [%d]", account->id); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (g_strcmp0(caller_package_name, account->package_name) != 0) + { + // packages dont match, so remove sensitive info + _INFO("Removing sensitive info from account id [%d]", account->id); + free (account->access_token); + account->access_token = NULL; + + } + return ACCOUNT_ERROR_NONE; + } + return ACCOUNT_ERROR_INVALID_PARAMETER; +} + +static int _remove_sensitive_info_from_non_owning_account_list(int caller_pid, GList *account_list) +{ + if (account_list == NULL) + { + _ERR("Null input"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + GList *list_iter = NULL; + for (list_iter = account_list; list_iter != NULL; list_iter = g_list_next(list_iter)) + { + account_s *account = (account_s *) list_iter->data; + return _remove_sensitive_info_from_non_owning_account(caller_pid, account); + } + return ACCOUNT_ERROR_INVALID_PARAMETER; +} + +static int _remove_sensitive_info_from_non_owning_account_slist(int caller_pid, GSList *account_list) +{ + if (account_list == NULL) + { + _ERR("Null input"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + GSList *list_iter = NULL; + for (list_iter = account_list; list_iter != NULL; list_iter = g_slist_next(list_iter)) + { + account_s *account = (account_s *) list_iter->data; + return _remove_sensitive_info_from_non_owning_account(caller_pid, account); + } + return ACCOUNT_ERROR_INVALID_PARAMETER; +} + +static const char *_account_db_err_msg() +{ + return sqlite3_errmsg(g_hAccountDB); +} + +static int _account_db_err_code() +{ + return sqlite3_errcode(g_hAccountDB); +} + +static int _account_get_record_count(char* query) +{ + _INFO("_account_get_record_count"); + + int rc = -1; + int ncount = 0; + account_stmt pStmt = NULL; + + if(!query){ + _ERR("NULL query\n"); + return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR; + } + + if(!g_hAccountDB){ + _ERR("DB is not opened\n"); + return ACCOUNT_ERROR_DB_NOT_OPENED; + } + + rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL); + + if (SQLITE_BUSY == rc){ + _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg()); + sqlite3_finalize(pStmt); + return ACCOUNT_ERROR_DATABASE_BUSY; + } else if (SQLITE_OK != rc) { + _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg()); + sqlite3_finalize(pStmt); + return ACCOUNT_ERROR_DB_FAILED; + } + + rc = sqlite3_step(pStmt); + if (SQLITE_BUSY == rc) { + _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg()); + sqlite3_finalize(pStmt); + return ACCOUNT_ERROR_DATABASE_BUSY; + } else if (SQLITE_ROW != rc) { + _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg()); + sqlite3_finalize(pStmt); + return ACCOUNT_ERROR_DB_FAILED; + } + + ncount = sqlite3_column_int(pStmt, 0); + + _INFO("account record count [%d]", ncount); + sqlite3_finalize(pStmt); + + return ncount; +} + +static int _account_execute_query(const char *query) +{ + int rc = -1; + char* pszErrorMsg = NULL; + + if(!query){ + ACCOUNT_ERROR("NULL query\n"); + return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR; + } + + if(!g_hAccountDB){ + ACCOUNT_ERROR("DB is not opened\n"); + return ACCOUNT_ERROR_DB_NOT_OPENED; + } + + rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg); + if (SQLITE_OK != rc) { + ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg); + sqlite3_free(pszErrorMsg); + } + + return rc; +} + +static int _account_begin_transaction(void) +{ + ACCOUNT_DEBUG("_account_begin_transaction start"); + int ret = -1; + + ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION"); + + if (ret == SQLITE_BUSY){ + ACCOUNT_ERROR(" sqlite3 busy = %d", ret); + return ACCOUNT_ERROR_DATABASE_BUSY; + } else if(ret != SQLITE_OK) { + ACCOUNT_ERROR("_account_svc_begin_transaction fail :: %d", ret); + return ACCOUNT_ERROR_DB_FAILED; + } + + ACCOUNT_DEBUG("_account_begin_transaction end"); + return ACCOUNT_ERROR_NONE; +} + +static int _account_end_transaction(bool is_success) +{ + ACCOUNT_DEBUG("_account_end_transaction start"); + + int ret = -1; + + if (is_success == true) { + ret = _account_execute_query("COMMIT TRANSACTION"); + ACCOUNT_DEBUG("_account_end_transaction COMMIT"); + } else { + ret = _account_execute_query("ROLLBACK TRANSACTION"); + ACCOUNT_DEBUG("_account_end_transaction ROLLBACK"); + } + + if(ret == SQLITE_PERM){ + ACCOUNT_ERROR("Account permission denied :: %d", ret); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (ret == SQLITE_BUSY){ + ACCOUNT_DEBUG(" sqlite3 busy = %d", ret); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + if (ret != SQLITE_OK) { + ACCOUNT_ERROR("_account_svc_end_transaction fail :: %d", ret); + return ACCOUNT_ERROR_DB_FAILED; + } + + ACCOUNT_DEBUG("_account_end_transaction end"); + return ACCOUNT_ERROR_NONE; +} + +static bool _account_check_add_more_account(const char* app_id) +{ + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + + ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id); + rc = _account_get_record_count(query); + + /* multiple account support case */ + if(rc > 0) { + ACCOUNT_SLOGD("app id (%s) supports multiple account. rc(%d)\n", app_id, rc); + return TRUE; + } + + /* multiple account not support case */ + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id); + rc = _account_get_record_count(query); + + if(rc <= 0) { + ACCOUNT_SLOGD("app id (%s) supports single account. and there is no account of the app id\n", app_id); + return TRUE; + } + + return FALSE; +} + +//TODO: Need to enable creating db on the first connect for +//a) multi-user cases +//b) to ensure db exist in every connect call + +//static int _account_create_all_tables(void) +//{ +// int rc = -1; +// int error_code = ACCOUNT_ERROR_NONE; +// char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + +// ACCOUNT_DEBUG("create all table - BEGIN"); +// ACCOUNT_MEMSET(query, 0, sizeof(query)); + +// /*Create the account table*/ +// ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TABLE); +// rc = _account_get_record_count(query); +// if (rc <= 0) { +// rc = _account_execute_query(ACCOUNT_SCHEMA); +// if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; +// ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", ACCOUNT_SCHEMA, rc, _account_db_err_msg())); + +//#ifndef _TIZEN_PUBLIC_ +// if (CSC_FEATURE_BOOL_TRUE == csc_feature_get_bool(CSC_FEATURE_DEF_BOOL_CONTACTS_DOCOMO_SOCIAL_PHONEBOOK)) { +// /* NTT docomo specific area */ +// rc = _account_execute_query(DOCOMO_DEFAULT_VAL_INSERT_QUERY); +// if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; +// ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", DOCOMO_DEFAULT_VAL_INSERT_QUERY, rc, _account_db_err_msg())); +// /* END of NTT docomo specific area */ +// } +//#endif +// } + +// /*Create capability table*/ +// ACCOUNT_MEMSET(query, 0, sizeof(query)); +// ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", CAPABILITY_TABLE); +// rc = _account_get_record_count(query); +// if (rc <= 0) { +// rc = _account_execute_query(CAPABILITY_SCHEMA); +// if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; +// ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", CAPABILITY_SCHEMA, rc, _account_db_err_msg())); +// } + +// /* Create account custom table */ +// ACCOUNT_MEMSET(query, 0, sizeof(query)); +// ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_CUSTOM_TABLE); +// rc = _account_get_record_count(query); +// if (rc <= 0) { +// rc = _account_execute_query(ACCOUNT_CUSTOM_SCHEMA); +// if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; +// ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg())); +// } + +// /* Create account type table */ +// ACCOUNT_MEMSET(query, 0, sizeof(query)); +// ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TYPE_TABLE); +// rc = _account_get_record_count(query); +// if (rc <= 0) { +// rc = _account_execute_query(ACCOUNT_TYPE_SCHEMA); +// if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; +// ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", ACCOUNT_TYPE_SCHEMA, rc, _account_db_err_msg())); +// } + +// /* Create label table */ +// ACCOUNT_MEMSET(query, 0, sizeof(query)); +// ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", LABEL_TABLE); +// rc = _account_get_record_count(query); +// if (rc <= 0) { +// rc = _account_execute_query(LABEL_SCHEMA); +// if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; +// ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", LABEL_SCHEMA, rc, _account_db_err_msg())); +// } + +// /* Create account feature table */ +// ACCOUNT_MEMSET(query, 0, sizeof(query)); +// ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", PROVIDER_FEATURE_TABLE); +// rc = _account_get_record_count(query); +// if (rc <= 0) { +// rc = _account_execute_query(PROVIDER_FEATURE_SCHEMA); +// if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; +// ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", PROVIDER_FEATURE_SCHEMA, rc, _account_db_err_msg())); +// } + +// ACCOUNT_DEBUG("create all table - END"); +// return error_code; +//} + +//static int _account_check_is_all_table_exists() +//{ +// int rc = 0; +// char query[ACCOUNT_SQL_LEN_MAX] = {0,}; +// ACCOUNT_MEMSET(query, 0, sizeof(query)); + +// ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s', '%s', '%s', '%s', '%s', '%s')", +// ACCOUNT_TABLE, CAPABILITY_TABLE, ACCOUNT_CUSTOM_TABLE, ACCOUNT_TYPE_TABLE, LABEL_TABLE, PROVIDER_FEATURE_TABLE); +// rc = _account_get_record_count(query); + +// if (rc != ACCOUNT_TABLE_TOTAL_COUNT) { +// ACCOUNT_ERROR("Table count is not matched rc=%d\n", rc); +// } + +// return rc; +//} + +int _account_db_handle_close(sqlite3* hDB) +{ + int rc = 0; + int ret = ACCOUNT_ERROR_NONE; + if(hDB) + { + rc = db_util_close(hDB); + if( rc == SQLITE_OK ) + ret = ACCOUNT_ERROR_NONE; + else if( rc == SQLITE_PERM ) + ret = ACCOUNT_ERROR_PERMISSION_DENIED; + else if ( rc == SQLITE_BUSY ) + ret = ACCOUNT_ERROR_DATABASE_BUSY; + else + ret = ACCOUNT_ERROR_DB_FAILED; + } + return ret; +} + +int _account_db_open(int mode, const char* account_db_path) +{ + int rc = 0; + int ret = -1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + if( g_hAccountDB ) { + ACCOUNT_ERROR( "Account database is using in another app. %x", g_hAccountDB ); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + ret = _account_db_handle_close(g_hAccountDB2); + if( ret != ACCOUNT_ERROR_NONE ) + ACCOUNT_DEBUG( "db_util_close(g_hAccountDB2) fail ret = %d", ret); + + ACCOUNT_DEBUG( "before db_util_open()"); + if(mode == ACCOUNT_DB_OPEN_READWRITE) + rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD); + else if(mode == ACCOUNT_DB_OPEN_READONLY) + rc = db_util_open_with_options(account_db_path, &g_hAccountDB, SQLITE_OPEN_READONLY, NULL); + else + return ACCOUNT_ERROR_DB_NOT_OPENED; + ACCOUNT_DEBUG( "after db_util_open() sqlite_rc = %d", rc); + + if( rc == SQLITE_PERM || _account_db_err_code() == SQLITE_PERM ) { + ACCOUNT_ERROR( "Account permission denied"); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if( rc == SQLITE_BUSY ) { + ACCOUNT_ERROR( "busy handler fail."); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + if( rc != SQLITE_OK ) { + ACCOUNT_ERROR( "The database isn't connected." ); + return ACCOUNT_ERROR_DB_NOT_OPENED; + } + + return ACCOUNT_ERROR_NONE; +} + +int _account_db_close(void) +{ + ACCOUNT_DEBUG( "start db_util_close()"); + int ret = -1; +/* + ret = _account_db_handle_close(g_hAccountDB2); + if( ret != ACCOUNT_ERROR_NONE ) + ACCOUNT_DEBUG( "db_util_close(g_hAccountDB2) fail ret = %d", ret); +*/ + ret = _account_db_handle_close(g_hAccountDB); + if( ret != ACCOUNT_ERROR_NONE ) + { + ACCOUNT_ERROR( "db_util_close(g_hAccountDB) fail ret = %d", ret); + g_hAccountDB2 = g_hAccountDB; + } + g_hAccountDB = NULL; + + return ret; +} + +static int _account_free_capability_items(account_capability_s *data) +{ + _ACCOUNT_FREE(data->type); + _ACCOUNT_FREE(data->package_name); + _ACCOUNT_FREE(data->user_name); + + return ACCOUNT_ERROR_NONE; +} + +static int _account_custom_item_free(account_custom_s *data) +{ + _ACCOUNT_FREE(data->app_id); + _ACCOUNT_FREE(data->key); + _ACCOUNT_FREE(data->value); + + return ACCOUNT_ERROR_NONE; +} + +static int _account_custom_gslist_free(GSList* list) +{ + if(!list){ + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + GSList* iter; + + for (iter = list; iter != NULL; iter = g_slist_next(iter)) { + account_custom_s *custom_data = (account_custom_s*)iter->data; + _account_custom_item_free(custom_data); + _ACCOUNT_FREE(custom_data); + } + + g_slist_free(list); + list = NULL; + + return ACCOUNT_ERROR_NONE; +} + +static int _account_free_account_items(account_s *data) +{ + _ACCOUNT_FREE(data->user_name); + _ACCOUNT_FREE(data->email_address); + _ACCOUNT_FREE(data->display_name); + _ACCOUNT_FREE(data->icon_path); + _ACCOUNT_FREE(data->source); + _ACCOUNT_FREE(data->package_name); + _ACCOUNT_FREE(data->domain_name); + _ACCOUNT_FREE(data->access_token); + + int i; + for(i=0;iuser_data_txt[i]); + + _account_gslist_free(data->capablity_list); + _account_glist_free(data->account_list); + _account_custom_gslist_free(data->custom_list); + + return ACCOUNT_ERROR_NONE; +} + +static int _account_gslist_free(GSList* list) +{ + if(!list){ + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + GSList* iter; + + for (iter = list; iter != NULL; iter = g_slist_next(iter)) { + account_capability_s *cap_data = (account_capability_s*)iter->data; + _account_free_capability_items(cap_data); + _ACCOUNT_FREE(cap_data); + } + + g_slist_free(list); + list = NULL; + + return ACCOUNT_ERROR_NONE; +} + +static int _account_glist_free(GList* list) +{ + if(!list){ + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + GList* iter; + + for (iter = list; iter != NULL; iter = g_list_next(iter)) { + account_s *account_record = (account_s*)iter->data; + _account_free_account_items(account_record); + _ACCOUNT_FREE(account_record); + } + + g_list_free(list); + list = NULL; + + return ACCOUNT_ERROR_NONE; +} + +static int _account_check_duplicated(account_s *data, const char* verified_appid) +{ + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int count = 0; + int ret = -1; + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where package_name='%s' and (user_name='%s' or display_name='%s' or email_address='%s')" + , ACCOUNT_TABLE, verified_appid, data->user_name, data->display_name, data->email_address); + + count = _account_get_record_count(query); + + if (count<=0) { + return ACCOUNT_ERROR_NONE; + } + + //check whether duplicated account or not. + //1. check user_name + //2. check display_name + //3. check email_address + GList* account_list_temp = _account_query_account_by_package_name(getpid(), verified_appid, &ret); + if (account_list_temp == NULL) + { + _ERR("_account_query_account_by_package_name returned NULL"); + return ACCOUNT_ERROR_DB_FAILED; + } + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if(ret != ACCOUNT_ERROR_NONE){ + return ret; + } + + account_list_temp = g_list_first(account_list_temp); + _INFO("account_list_temp length=[%d]",g_list_length(account_list_temp)); + + GList* iter = NULL; + for (iter = account_list_temp; iter != NULL; iter = g_list_next(iter)) + { + _INFO("iterating account_list_temp"); + account_s *account = NULL; + _INFO("Before iter->data"); + account = (account_s*)iter->data; + _INFO("After iter->data"); + if (account != NULL) + { + if(account->user_name!=NULL && data->user_name!=NULL && strcmp(account->user_name, data->user_name)==0) + { + _INFO("duplicated account(s) exist!, same user_name=%s", data->user_name); + return ACCOUNT_ERROR_DUPLICATED; + } + //when user_name is not NULL and display_name is same. + if(account->user_name==NULL && data->user_name==NULL && account->display_name!=NULL && data->display_name!=NULL && strcmp(account->display_name, data->display_name)==0) + { + _INFO("duplicated account(s) exist!, same display_name=%s", data->display_name); + return ACCOUNT_ERROR_DUPLICATED; + } + //when user_name and display_name are not NULL and email_address is same. + if(account->user_name==NULL && data->user_name==NULL && account->display_name==NULL && data->display_name==NULL && account->email_address!=NULL && data->email_address!=NULL && strcmp(account->email_address, data->email_address)==0) + { + _INFO("duplicated account(s) exist!, same email_address=%s", data->email_address); + return ACCOUNT_ERROR_DUPLICATED; + } + } + } + + return ACCOUNT_ERROR_NONE; +} + +static int _account_get_next_sequence(const char *pszName) +{ + int rc = 0; + account_stmt pStmt = NULL; + int max_seq = 0; + char szQuery[ACCOUNT_SQL_LEN_MAX] = {0,}; + + ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery)); + ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery), "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName); + rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL); + if (SQLITE_OK != rc) { + ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg()); + sqlite3_finalize(pStmt); + return ACCOUNT_ERROR_DB_FAILED; + } + + rc = sqlite3_step(pStmt); + max_seq = sqlite3_column_int(pStmt, 0); + max_seq++; + + /*Finalize Statement*/ + rc = sqlite3_finalize(pStmt); + pStmt = NULL; + + return max_seq; +} + +static account_stmt _account_prepare_query(char *query) +{ + int rc = -1; + account_stmt pStmt = NULL; + + ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL")); + + rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL); + + ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg())); + + return pStmt; +} + +static int _account_query_bind_int(account_stmt pStmt, int pos, int num) +{ + if(!pStmt){ + ACCOUNT_ERROR("statement is null"); + return -1; + } + + if(pos < 0){ + ACCOUNT_ERROR("invalid pos"); + return -1; + } + + return sqlite3_bind_int(pStmt, pos, num); +} + +static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str) +{ + _INFO("_account_query_bind_text"); + + if(!pStmt) + { + _ERR("statement is null"); + return -1; + } + + if(str) + { + _INFO("sqlite3_bind_text"); + return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC); + } + else + { + _INFO("sqlite3_bind_null"); + return sqlite3_bind_null(pStmt, pos); + } +} + +static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value) +{ + _INFO("start"); + + int count = 1; + + /*Caution : Keep insert query orders.*/ + + /* 1. user name*/ + _account_query_bind_text(hstmt, count++, (char*)account->user_name); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name); + + /* 2. email address*/ + _account_query_bind_text(hstmt, count++, (char*)account->email_address); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], email_address=%s", account->id, account->email_address); + + /* 3. display name*/ + _account_query_bind_text(hstmt, count++, (char*)account->display_name); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], display_name=%s", account->id, account->display_name); + + /* 4. icon path*/ + _account_query_bind_text(hstmt, count++, (char*)account->icon_path); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], icon_path=%s", account->id, account->icon_path); + + /* 5. source*/ + _account_query_bind_text(hstmt, count++, (char*)account->source); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], source=%s", account->id, account->source); + + /* 6. package name*/ + _account_query_bind_text(hstmt, count++, (char*)account->package_name); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], package_name=%s", account->id, account->package_name); + + /* 7. access token*/ + _account_query_bind_text(hstmt, count++, (char*)account->access_token); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], access_token=%s", account->id, account->access_token); + + /* 8. domain name*/ + _account_query_bind_text(hstmt, count++, (char*)account->domain_name); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], domain_name=%s", account->id, account->domain_name); + + /* 9. auth type*/ + _account_query_bind_int(hstmt, count++, account->auth_type); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], auth_type=%d", account->id, account->auth_type); + + /* 10. secret */ + _account_query_bind_int(hstmt, count++, account->secret); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], secret=%d", account->id, account->secret); + + /* 11. sync_support */ + _account_query_bind_int(hstmt, count++, account->sync_support); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], sync_support=%d", account->id, account->sync_support); + + int i; + + /* 12. user text*/ + for(i=0; i< USER_TXT_CNT; i++) + _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]); + + /* 13. user integer */ + for(i=0; i< USER_INT_CNT; i++) + { + _account_query_bind_int(hstmt, count++, account->user_data_int[i]); + _INFO("convert user_data_int : marshal_user_int data_int[%d]=%d", i, account->user_data_int[i]); + } + + _INFO("end"); + + return count; +} + +static int _account_query_finalize(account_stmt pStmt) +{ + int rc = -1; + + if (!pStmt) { + ACCOUNT_ERROR( "pStmt is NULL"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + rc = sqlite3_finalize(pStmt); + if (rc == SQLITE_BUSY){ + ACCOUNT_ERROR(" sqlite3 busy = %d", rc); + return ACCOUNT_ERROR_DATABASE_BUSY; + } else if (rc != SQLITE_OK) { + ACCOUNT_ERROR( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_DB_FAILED; + } + + return ACCOUNT_ERROR_NONE; +} + +static int _account_query_step(account_stmt pStmt) +{ + if(!pStmt){ + ACCOUNT_ERROR( "pStmt is NULL"); + return -1; + } + + return sqlite3_step(pStmt); +} + +static int _account_execute_insert_query(account_s *account) +{ + _INFO("_account_execute_insert_query start"); + + int rc = 0; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + + /* check whether app id exist in account type db */ + + if (!account->user_name && !account->display_name && !account->email_address) { + _INFO(""); + ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + _INFO(""); + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (user_name, email_address , display_name , icon_path , source , package_name , " + "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, " + "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values " // to do urusa + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ACCOUNT_TABLE); + + hstmt = _account_prepare_query(query); + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + _INFO(""); + _account_convert_account_to_sql(account, hstmt, query); + + _INFO(""); + rc = _account_query_step(hstmt); + if (rc != SQLITE_DONE) { + _INFO(""); + ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + + if( _account_db_err_code() == SQLITE_PERM ) + error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + else + error_code = ACCOUNT_ERROR_DB_FAILED; + } + + _INFO(""); + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + _INFO("_account_execute_insert_query end"); + return error_code; +} + +static int _account_insert_capability(account_s *account, int account_id) +{ + _INFO("_account_insert_capability start"); + int rc, count = 1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + + if (g_slist_length( account->capablity_list)==0) { + ACCOUNT_DEBUG( "_account_insert_capability, no capability\n"); + return ACCOUNT_ERROR_NONE; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where id=%d", ACCOUNT_TABLE, account_id); + + _INFO("_account_insert_capability _account_get_record_count [%s]", query); + rc = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + _ERR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + if (rc <= 0) { + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + /* insert query*/ + + GSList *iter; + + for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) { + int ret; + count = 1; + + account_capability_s* cap_data = NULL; + cap_data = (account_capability_s*)iter->data; + + _INFO("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value); + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES " + "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE); + hstmt = _account_prepare_query(query); + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + ret = _account_query_bind_text(hstmt, count++, cap_data->type); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_int(hstmt, count++, cap_data->value); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_int(hstmt, count++, (int)account_id); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); + + rc = _account_query_step(hstmt); + _INFO("_account_insert_capability _account_query_step[%d]", rc); + + if (rc != SQLITE_DONE) { + _ERR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + break; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + } + + _INFO("_account_insert_capability end"); + return ACCOUNT_ERROR_NONE; +} + +static int _account_update_capability(account_s *account, int account_id) +{ + int rc, count = 1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + + if (g_slist_length( account->capablity_list)==0) { + ACCOUNT_ERROR( "_account_update_capability, no capability\n"); + return ACCOUNT_ERROR_NONE; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where id=%d", ACCOUNT_TABLE, account_id); + + rc = _account_get_record_count(query); + + if (rc <= 0) { + ACCOUNT_SLOGI( "_account_update_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE); + hstmt = _account_prepare_query(query); + count = 1; + _account_query_bind_int(hstmt, count++, (int)account_id); + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_DB_FAILED; + } + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + GSList *iter; + + for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) { + int ret; + count = 1; + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES " + "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE); + + hstmt = _account_prepare_query(query); + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + account_capability_s* cap_data = NULL; + cap_data = (account_capability_s*)iter->data; + + ret = _account_query_bind_text(hstmt, count++, cap_data->type); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_int(hstmt, count++, cap_data->value); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_int(hstmt, count++, (int)account_id); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); + + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + break; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + } + + return ACCOUNT_ERROR_NONE; +} + +static int _account_update_capability_by_user_name(account_s *account, const char *user_name, const char *package_name ) +{ + int rc, count = 1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + + if (g_slist_length( account->capablity_list)==0) { + ACCOUNT_ERROR( "_account_update_capability_by_user_name, no capability\n"); + return ACCOUNT_ERROR_NONE; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name= '%s' and user_name='%s'", ACCOUNT_TABLE, package_name, user_name); + + rc = _account_get_record_count(query); + + if (rc <= 0) { + ACCOUNT_SLOGI( "_account_update_capability_by_user_name : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE); + hstmt = _account_prepare_query(query); + count = 1; + _account_query_bind_text(hstmt, count++, (char*)account->package_name); + _account_query_bind_text(hstmt, count++, (char*)account->user_name); + rc = _account_query_step(hstmt); + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_DB_FAILED; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + GSList* iter; + + for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) { + int ret; + count = 1; + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES " + "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE); + + hstmt = _account_prepare_query(query); + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + account_capability_s* cap_data = NULL; + cap_data = (account_capability_s*)iter->data; + + ret = _account_query_bind_text(hstmt, count++, cap_data->type); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_int(hstmt, count++, cap_data->value); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_int(hstmt, count++, (int)account->id); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); + + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + break; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + } + + return ACCOUNT_ERROR_NONE; +} + +static int _account_query_table_column_int(account_stmt pStmt, int pos) +{ + if(!pStmt){ + ACCOUNT_ERROR("statement is null"); + return -1; + } + + if(pos < 0){ + ACCOUNT_ERROR("invalid pos"); + return -1; + } + + return sqlite3_column_int(pStmt, pos); +} + +static const char *_account_query_table_column_text(account_stmt pStmt, int pos) +{ + if(!pStmt){ + ACCOUNT_ERROR("statement is null"); + return NULL; + } + + if(pos < 0){ + ACCOUNT_ERROR("invalid pos"); + return NULL; + } + + return (const char*)sqlite3_column_text(pStmt, pos); +} + +static void _account_db_data_to_text(const char *textbuf, char **output) +{ + if (textbuf && strlen(textbuf)>0) { + if (*output) { + free(*output); + *output = NULL; + } + *output = strdup(textbuf); + } +} + +static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record) +{ + const char *textbuf = NULL; + + account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID); + ACCOUNT_DEBUG("account_record->id =[%d]", account_record->id); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME); + _account_db_data_to_text(textbuf, &(account_record->user_name)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS); + _account_db_data_to_text(textbuf, &(account_record->email_address)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME); + _account_db_data_to_text(textbuf, &(account_record->display_name)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH); + _account_db_data_to_text(textbuf, &(account_record->icon_path)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE); + _account_db_data_to_text(textbuf, &(account_record->source)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME); + _account_db_data_to_text(textbuf, &(account_record->package_name)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN); + _account_db_data_to_text(textbuf, &(account_record->access_token)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME); + _account_db_data_to_text(textbuf, &(account_record->domain_name)); + + account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE); + + account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET); + + account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0); + _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0])); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1); + _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1])); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2); + _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2])); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3); + _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3])); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4); + _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4])); + + account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0); + account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1); + account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2); + account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3); + account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4); +} + +static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record) +{ + const char *textbuf = NULL; + + _INFO("start _account_convert_column_to_capability()"); + capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID); + + textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY); + _account_db_data_to_text(textbuf, &(capability_record->type)); + + capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE); + + textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME); + _account_db_data_to_text(textbuf, &(capability_record->package_name)); + + textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME); + _account_db_data_to_text(textbuf, &(capability_record->user_name)); + + capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID); + _INFO("type = %s, value = %d", capability_record->type, capability_record->value); + _INFO("end _account_convert_column_to_capability()"); +} + +static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record) +{ + _INFO("start _account_convert_column_to_custom()"); + const char *textbuf = NULL; + + custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID); + _account_db_data_to_text(textbuf, &(custom_record->app_id)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY); + _account_db_data_to_text(textbuf, &(custom_record->key)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE); + _account_db_data_to_text(textbuf, &(custom_record->value)); + _INFO("key = %s, value = %s", custom_record->key, custom_record->value); + _INFO("end _account_convert_column_to_custom()"); +} + +bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data) +{ + account_s *data = (account_s*)user_data; + + account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s)); + + if (cap_data == NULL) + return FALSE; + ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s)); + + cap_data->type = _account_get_text(capability_type); + cap_data->value = capability_value; + _INFO("cap_data->type = %s, cap_data->value = %d", cap_data->type, cap_data->value); + + data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data); + + return TRUE; +} + + +bool _account_get_custom_text_cb(char* key, char* value, void *user_data) +{ + account_s *data = (account_s*)user_data; + + account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s)); + + if (custom_data == NULL) { + ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n"); + return FALSE; + } + ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s)); + + custom_data->account_id = data->id; + custom_data->app_id = _account_get_text(data->package_name); + custom_data->key = _account_get_text(key); + custom_data->value = _account_get_text(value); + _INFO("custom_data->key = %s, custom_data->value = %s", custom_data->key, custom_data->value); + + data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data); + + return TRUE; +} + + +static char *_account_get_text(const char *text_data) +{ + char *text_value = NULL; + + if (text_data != NULL) { + text_value = strdup(text_data); + } + return text_value; +} + +static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + account_s *old_account = NULL; + + ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL")); + ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL")); + ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + old_account = (account_s*)calloc(1, sizeof(account_s)); + if(!old_account) { + ACCOUNT_FATAL("Memory alloc fail\n"); + return ACCOUNT_ERROR_OUT_OF_MEMORY; + } + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name); + hstmt = _account_prepare_query(query); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + while (rc == SQLITE_ROW) { + _account_convert_column_to_account(hstmt, old_account); + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + // get capability + error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account); + ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error")); + + // get custom text + error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account); + ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error")); + + // compare + new_account->id = old_account->id; + + //user name + if(!new_account->user_name) { + if(old_account->user_name) + new_account->user_name = _account_get_text(old_account->user_name); + } + + // display name + if(!new_account->display_name) { + if(old_account->display_name) + new_account->display_name = _account_get_text(old_account->display_name); + } + + // email address + if(!new_account->email_address) { + if(old_account->email_address) + new_account->email_address = _account_get_text(old_account->email_address); + } + + // domain name + if(!new_account->domain_name) { + if(old_account->domain_name) + new_account->domain_name = _account_get_text(old_account->domain_name); + } + + // icon path + if(!new_account->icon_path) { + if(old_account->icon_path) + new_account->icon_path = _account_get_text(old_account->icon_path); + } + + // source + if(!new_account->source) { + if(old_account->source) + new_account->source = _account_get_text(old_account->source); + } + + _ACCOUNT_FREE(new_account->package_name); + new_account->package_name = _account_get_text(old_account->package_name); + + // access token + if(!new_account->access_token) { + if(old_account->access_token) + new_account->access_token = _account_get_text(old_account->access_token); + } + + // auth type + if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) { + new_account->auth_type = old_account->auth_type; + } + + //secret + if(new_account->secret== ACCOUNT_SECRECY_INVALID) { + new_account->secret = old_account->secret; + } + + // sync support + if(new_account->sync_support == ACCOUNT_SYNC_INVALID) { + new_account->sync_support = old_account->sync_support; + } + + // TODO user text + int i; + for(i=0;iuser_data_txt[i]) { + if(old_account->user_data_txt[i]) + new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]); + } + } + + // TODO user int + for(i=0;iuser_data_int[i] == 0) { + new_account->user_data_int[i] = old_account->user_data_int[i]; + } + } + + // capability + + // user custom table + +CATCH: + if (old_account) { + _account_free_account_items(old_account); + _ACCOUNT_FREE(old_account); + } + + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + return ACCOUNT_ERROR_NONE; +} + + + +static int _account_update_account_by_user_name(int pid, account_s *account, const char *user_name, const char *package_name) +{ + int rc = 0, binding_count = 0, count = 0; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n")); + ACCOUNT_RETURN_VAL((package_name!= NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n")); + + char* current_appid = NULL; + char* verified_appid = NULL; + + current_appid = _account_get_current_appid(pid); + error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid); + + _ACCOUNT_FREE(current_appid); + _ACCOUNT_FREE(verified_appid); + + if(error_code != ACCOUNT_ERROR_NONE){ + ACCOUNT_ERROR("No permission to update\n"); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _account_compare_old_record_by_user_name(account, user_name, package_name); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (!account->package_name) { + ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!account->user_name && !account->display_name && !account->email_address) { + ACCOUNT_ERROR("One field should be set among user name, display name, email address\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'" + , ACCOUNT_TABLE, user_name, package_name); + + count = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (count <= 0) { + ACCOUNT_SLOGI("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n", + count, user_name, package_name); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + //TODO: Is it required to update id ? As of now I can only think of falied rollback cases (between account and gSSO DB) + ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, " + "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?," + "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, " + "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE); + + hstmt = _account_prepare_query(query); + if( _account_db_err_code() == SQLITE_PERM ){ + _account_end_transaction(FALSE); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg())); + + binding_count = _account_convert_account_to_sql(account, hstmt, query); + + _account_query_bind_text(hstmt, binding_count++, user_name); + _account_query_bind_text(hstmt, binding_count++, package_name); + rc = _account_query_step(hstmt); + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + } + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + /*update capability*/ + error_code = _account_update_capability_by_user_name(account, user_name, package_name); + + /* update custom */ + error_code = _account_update_custom(account, account->id); + + return error_code; +} + +int _account_insert_to_db(account_s* account, int pid, int *account_id) +{ + _INFO(""); + int error_code = ACCOUNT_ERROR_NONE; + int ret_transaction = 0; + + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((account_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL")); + + if (!account->user_name && !account->display_name && !account->email_address) { + ACCOUNT_ERROR("One field should be set among user name, display name, email address\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_s *data = (account_s*)account; + ACCOUNT_SLOGD("(%s)-(%d) account_insert_to_db: begin_transaction.\n", __FUNCTION__, __LINE__); + + pthread_mutex_lock(&account_mutex); + + /* transaction control required*/ + ret_transaction = _account_begin_transaction(); + + if(_account_db_err_code() == SQLITE_PERM){ + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY) { + ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DATABASE_BUSY; + }else if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + return ret_transaction; + } + + _INFO(""); + *account_id = _account_get_next_sequence(ACCOUNT_TABLE); + data->id = *account_id; + + char* appid = NULL; + appid = _account_get_current_appid(pid); + _INFO(""); + + if(!appid) + { + _INFO(""); + // API caller cannot be recognized + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER; + } + + _INFO(""); + char* verified_appid = NULL; + error_code = _account_check_account_type_with_appid_group(appid, &verified_appid);//FIX + if(error_code != ACCOUNT_ERROR_NONE) + { + _INFO(""); + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction); + _ACCOUNT_FREE(verified_appid); + _ACCOUNT_FREE(appid); + pthread_mutex_unlock(&account_mutex); + return error_code; + } + + if(verified_appid) + { + _INFO(""); + error_code = _account_check_duplicated(data, verified_appid); + if (error_code != ACCOUNT_ERROR_NONE) { + _INFO(""); + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_DEBUG("_account_check_duplicated(), rollback insert query(%x)!!!!\n", ret_transaction); + *account_id = -1; + pthread_mutex_unlock(&account_mutex); + return error_code; + } + if(!_account_check_add_more_account(verified_appid)) { + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + _ACCOUNT_FREE(verified_appid); + return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE; + } + + _ACCOUNT_FREE(data->package_name); + data->package_name = _account_get_text(verified_appid); + _ACCOUNT_FREE(verified_appid); + } + + if(!_account_check_add_more_account(data->package_name)) + { + _INFO(""); + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE; + } + + error_code = _account_execute_insert_query(data); + + if (error_code != ACCOUNT_ERROR_NONE) + { + _INFO(""); + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction); + *account_id = -1; + pthread_mutex_unlock(&account_mutex); + return error_code; + } + + _INFO(""); + error_code = _account_insert_capability(data, *account_id); + if (error_code != ACCOUNT_ERROR_NONE) + { + _INFO(""); + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction); + *account_id = -1; + pthread_mutex_unlock(&account_mutex); + return error_code; + } + + _INFO(""); + error_code = _account_insert_custom(data, *account_id); + if (error_code != ACCOUNT_ERROR_NONE) + { + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction); + *account_id = -1; + pthread_mutex_unlock(&account_mutex); + return error_code; + } + + _INFO(""); + + pthread_mutex_unlock(&account_mutex); + _account_end_transaction(TRUE); + ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__); + + char buf[64]={0,}; + ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id); + _account_insert_delete_update_notification_send(buf); + _INFO("(%s)-(%d) account _notification_send."); + + return ACCOUNT_ERROR_NONE; + +} + +int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data ) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + + ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + account_capability_s* capability_record = NULL; + + while (rc == SQLITE_ROW) { + bool cb_ret = FALSE; + capability_record = (account_capability_s*) malloc(sizeof(account_capability_s)); + + if (capability_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s)); + + _account_convert_column_to_capability(hstmt, capability_record); + + cb_ret = callback(capability_record->type, capability_record->value, user_data); + + _account_free_capability_items(capability_record); + _ACCOUNT_FREE(capability_record); + + ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n")); + + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + return error_code; +} + +GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code) +{ + *error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + GSList* capability_list = NULL; + + ACCOUNT_RETURN_VAL((account_id > 0), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("ACCOUNT INDEX IS LESS THAN 0")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + *error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + return NULL; + } + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + account_capability_s* capability_record = NULL; + + while (rc == SQLITE_ROW) { + capability_record = (account_capability_s*) malloc(sizeof(account_capability_s)); + + if (capability_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s)); + + _account_convert_column_to_capability(hstmt, capability_record); + + //cb_ret = callback(capability_record->type, capability_record->value, user_data); + + //_account_free_capability_items(capability_record); + //_ACCOUNT_FREE(capability_record); + + //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n")); + + capability_list = g_slist_append(capability_list, capability_record); + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + + *error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) + { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + return capability_list; +} + +static int _account_compare_old_record(account_s *new_account, int account_id) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + account_s *old_account = NULL; + + ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); + ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + old_account = (account_s*)calloc(1, sizeof(account_s)); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id); + hstmt = _account_prepare_query(query); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + while (rc == SQLITE_ROW) { + _account_convert_column_to_account(hstmt, old_account); + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + // get capability + error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account); + ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error")); + + // get custom text + error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account); + ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error")); + + // compare + + new_account->id = old_account->id; + + //user name + if(!new_account->user_name) { + if(old_account->user_name) + new_account->user_name = _account_get_text(old_account->user_name); + } + + // display name + if(!new_account->display_name) { + if(old_account->display_name) + new_account->display_name = _account_get_text(old_account->display_name); + } + + // email address + if(!new_account->email_address) { + if(old_account->email_address) + new_account->email_address = _account_get_text(old_account->email_address); + } + + // domain name + if(!new_account->domain_name) { + if(old_account->domain_name) + new_account->domain_name = _account_get_text(old_account->domain_name); + } + + // icon path + if(!new_account->icon_path) { + if(old_account->icon_path) + new_account->icon_path = _account_get_text(old_account->icon_path); + } + + // source + if(!new_account->source) { + if(old_account->source) + new_account->source = _account_get_text(old_account->source); + } + + _ACCOUNT_FREE(new_account->package_name); + new_account->package_name = _account_get_text(old_account->package_name); + + // access token + if(!new_account->access_token) { + if(old_account->access_token) + new_account->access_token = _account_get_text(old_account->access_token); + } + + // user text + int i; + for(i=0;iuser_data_txt[i]) { + if(old_account->user_data_txt[i]) + new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]); + } + } + + // auth type + if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) { + new_account->auth_type = old_account->auth_type; + } + + //secret + if(new_account->secret== ACCOUNT_SECRECY_INVALID) { + new_account->secret = old_account->secret; + } + + // sync support + if(new_account->sync_support == ACCOUNT_SYNC_INVALID) { + new_account->sync_support = old_account->sync_support; + } + + // user int + for(i=0;iuser_data_int[i] == 0) { + new_account->user_data_int[i] = old_account->user_data_int[i]; + } + } + + // capability + + // user custom table + +CATCH: + if (old_account) { + _account_free_account_items(old_account); + _ACCOUNT_FREE(old_account); + } + + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + return ACCOUNT_ERROR_NONE; +} + +static int _account_get_package_name_from_account_id(int account_id, char **package_name) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + account_s *old_account = NULL; + + ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + old_account = (account_s*)calloc(1, sizeof(account_s)); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE id = %d", ACCOUNT_TABLE, account_id); + hstmt = _account_prepare_query(query); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + while (rc == SQLITE_ROW) { + _account_convert_column_to_account(hstmt, old_account); + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + // get package name. + *package_name = _account_get_text(old_account->package_name); + + + CATCH: + if (old_account) { + _account_free_account_items(old_account); + _ACCOUNT_FREE(old_account); + } + + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + return error_code; + +} + +static int _account_update_account(int pid, account_s *account, int account_id) +{ + int rc = 0, binding_count =0; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0; + account_stmt hstmt = NULL; + + if (!account->package_name) { + ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + /* Check permission of requested appid */ + char* current_appid = NULL; + char *package_name = NULL; + + current_appid = _account_get_current_appid(pid); + error_code = _account_get_package_name_from_account_id(account_id, &package_name); + + if(error_code != ACCOUNT_ERROR_NONE || package_name == NULL){ + ACCOUNT_ERROR("No package name with account_id\n"); + _ACCOUNT_FREE(current_appid); + _ACCOUNT_FREE(package_name); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + error_code = _account_check_appid_group_with_package_name(current_appid, package_name); + ACCOUNT_DEBUG( "UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); // TODO: remove the log later. + + _ACCOUNT_FREE(current_appid); + _ACCOUNT_FREE(package_name); + + if(error_code != ACCOUNT_ERROR_NONE){ + ACCOUNT_ERROR("No permission to update\n"); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _account_compare_old_record(account, account_id); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } else if( _account_db_err_code() == SQLITE_BUSY ){ + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + if (!account->user_name && !account->display_name && !account->email_address) { + ACCOUNT_ERROR("One field should be set among user name, display name, email address\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE id = %d ", ACCOUNT_TABLE, account_id); + + count = _account_get_record_count(query); + if (count <= 0) { + ACCOUNT_DEBUG(" Account record not found, count = %d\n", count); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + /* transaction control required*/ + ret_transaction = _account_begin_transaction(); + if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, " + "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?," + "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, " + "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE id=? ", ACCOUNT_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s)(%x).\n", _account_db_err_msg(), _account_end_transaction(FALSE))); + + binding_count = _account_convert_account_to_sql(account, hstmt, query); + _account_query_bind_int(hstmt, binding_count++, account_id); + + rc = _account_query_step(hstmt); + if (rc != SQLITE_DONE) { + ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + _INFO("update query=%s", query); + + /*update capability*/ + error_code = _account_update_capability(account, account_id); + if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){ + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction); + return error_code; + } + + /* update custom */ + error_code = _account_update_custom(account, account_id); + if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){ + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction); + return error_code; + } + + ret_transaction = _account_end_transaction(TRUE); + + _INFO("update end"); + return error_code; +} + + +static int _account_update_account_ex(account_s *account, int account_id) +{ + int rc = 0, binding_count =0; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0; + account_stmt hstmt = NULL; + + if (!account->package_name) { + ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + _account_compare_old_record(account, account_id); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (!account->user_name && !account->display_name && !account->email_address) { + ACCOUNT_ERROR("One field should be set among user name, display name, email address\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE id = %d ", ACCOUNT_TABLE, account_id); + + count = _account_get_record_count(query); + if (count <= 0) { + ACCOUNT_DEBUG(" Account record not found, count = %d\n", count); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + /* transaction control required*/ + ret_transaction = _account_begin_transaction(); + if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, " + "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?," + "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, " + "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE id=? ", ACCOUNT_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s)(%x).\n", _account_db_err_msg(), _account_end_transaction(FALSE))); + + _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name); + binding_count = _account_convert_account_to_sql(account, hstmt, query); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name); + _INFO("account_update_to_db_by_id_ex_p : before bind()"); + rc = _account_query_bind_int(hstmt, binding_count++, account_id); + _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc); + + rc = _account_query_step(hstmt); + if (rc != SQLITE_DONE) { + ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + } + _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc); + + _INFO("account_update_to_db_by_id_ex_p : before update_capability()"); + /*update capability*/ + error_code = _account_update_capability(account, account_id); + if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){ + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction); + return error_code; + } + _INFO("account_update_to_db_by_id_ex_p : after update_capability()"); + + _INFO("account_update_to_db_by_id_ex_p : before update_custom()"); + /* update custom */ + error_code = _account_update_custom(account, account_id); + if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){ + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction); + return error_code; + } + _INFO("account_update_to_db_by_id_ex_p : after update_custom()"); + + ret_transaction = _account_end_transaction(TRUE); + + return error_code; +} + + +int _account_update_to_db_by_id(int pid, account_s* account, int account_id) +{ + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL")); + ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + int error_code = ACCOUNT_ERROR_NONE; + account_s* data = (account_s*)account; + + pthread_mutex_lock(&account_mutex); + + error_code = _account_update_account(pid, data, account_id); + + if(error_code != ACCOUNT_ERROR_NONE) { + pthread_mutex_unlock(&account_mutex); + return error_code; + } + + pthread_mutex_unlock(&account_mutex); + + char buf[64]={0,}; + ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id); + _account_insert_delete_update_notification_send(buf); + + return ACCOUNT_ERROR_NONE; +} + +int _account_update_to_db_by_id_ex(account_s* account, int account_id) +{ + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL")); + ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + int error_code = ACCOUNT_ERROR_NONE; + account_s* data = account; + + pthread_mutex_lock(&account_mutex); + + _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name); + error_code = _account_update_account_ex(data, account_id); + _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name); + + if(error_code != ACCOUNT_ERROR_NONE) { + pthread_mutex_unlock(&account_mutex); + return error_code; + } + + pthread_mutex_unlock(&account_mutex); + + char buf[64]={0,}; + ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id); + _account_insert_delete_update_notification_send(buf); + + return ACCOUNT_ERROR_NONE; +} + + +int _account_update_to_db_by_user_name(int pid, account_s* account, const char *user_name, const char *package_name) +{ + ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL")); + ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + int error_code = ACCOUNT_ERROR_NONE; + account_s *data = (account_s*)account; + + pthread_mutex_lock(&account_mutex); + + error_code = _account_update_account_by_user_name(pid, data, user_name, package_name); + + pthread_mutex_unlock(&account_mutex); + + char buf[64]={0,}; + ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, data->id); + _account_insert_delete_update_notification_send(buf); + + return error_code; +} + +GSList* _account_db_query_all(int pid) +{ + //int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + GSList *account_list = NULL; + + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return NULL; + } + + rc = _account_query_step(hstmt); + + account_s *account_record = NULL; + + if (rc != SQLITE_ROW) + { + _ERR("The record isn't found"); + goto CATCH; + } + + while(rc == SQLITE_ROW) { + account_record = (account_s*) malloc(sizeof(account_s)); + + if (account_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s)); + _account_convert_column_to_account(hstmt, account_record); + account_list = g_slist_append(account_list, account_record); + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error")); + hstmt = NULL; + + GSList* iter; + + for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) { + account_s *account = NULL; + account = (account_s*)iter->data; + _account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account); + _account_query_custom_by_account_id(_account_get_custom_text_cb, account->id, (void*)account); + } + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_gslist_free(account_list);}, NULL, ("finalize error")); + hstmt = NULL; + } + if (account_list) + { + _remove_sensitive_info_from_non_owning_account_slist(pid, account_list); + } + return account_list; +} + +int _account_update_sync_status_by_id(int account_db_id, const int sync_status) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + int count =1; + + ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + if ( (sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) { + ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&account_mutex); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where id=%d", ACCOUNT_TABLE, account_db_id); + + rc = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (rc <= 0) { + ACCOUNT_SLOGE( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE id = %d", ACCOUNT_TABLE, account_db_id); + hstmt = _account_prepare_query(query); + + _account_query_bind_int(hstmt, count, sync_status); + + rc = _account_query_step(hstmt); + + if( _account_db_err_code() == SQLITE_PERM ){ + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED, + ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg())); + + rc = _account_query_finalize(hstmt); + if (rc != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("_account_query_finalize error"); + pthread_mutex_unlock(&account_mutex); + return rc; + } + char buf[64]={0,}; + ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id); + _account_insert_delete_update_notification_send(buf); + + hstmt = NULL; + error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + pthread_mutex_unlock(&account_mutex); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + return error_code; +} + +int _account_query_account_by_account_id(int pid, int account_db_id, account_s *account_record) +{ + ACCOUNT_DEBUG("account_db_id=[%d]", account_db_id); + + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + + ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); + ACCOUNT_RETURN_VAL(account_record != NULL, {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_DEBUG("starting db operations"); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE id = %d", ACCOUNT_TABLE, account_db_id); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_DEBUG("before _account_query_step"); + rc = _account_query_step(hstmt); + ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + while (rc == SQLITE_ROW) { + ACCOUNT_DEBUG("before _account_convert_column_to_account"); + _account_convert_column_to_account(hstmt, account_record); + ACCOUNT_DEBUG("after _account_convert_column_to_account"); + ACCOUNT_DEBUG("user_name = %s, user_txt[0] = %s, user_int[1] = %d", account_record->user_name, account_record->user_data_txt[0], account_record->user_data_int[1]); + rc = _account_query_step(hstmt); + } + + ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + + ACCOUNT_DEBUG("before _account_query_capability_by_account_id"); + _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record); + ACCOUNT_DEBUG("after _account_query_capability_by_account_id"); + + ACCOUNT_DEBUG("before _account_query_custom_by_account_id"); + _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record); + ACCOUNT_DEBUG("after _account_query_custom_by_account_id"); + + hstmt = NULL; + error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + if (account_record) + { + _remove_sensitive_info_from_non_owning_account(pid, account_record); + } + pthread_mutex_unlock(&account_mutex); + ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code); + return error_code; +} + +GList* _account_query_account_by_user_name(int pid, const char *user_name, int *error_code) +{ + *error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + account_s *account_head = NULL; + + if (user_name == NULL) + { + _ERR("USER NAME IS NULL"); + *error_code = ACCOUNT_ERROR_INVALID_PARAMETER; + goto CATCH; + } + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE); + + hstmt = _account_prepare_query(query); + + if (_account_db_err_code() == SQLITE_PERM) + { + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + *error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + goto CATCH; + } + + int binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, user_name); + + rc = _account_query_step(hstmt); + + if (rc != SQLITE_ROW) + { + _ERR("The record isn't found"); + *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + goto CATCH; + } + + int tmp = 0; + + account_head = (account_s*) malloc(sizeof(account_s)); + if (account_head == NULL) { + ACCOUNT_FATAL("malloc Failed"); + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + + if (rc != ACCOUNT_ERROR_NONE) + { + _ERR("finalize error"); + *error_code = rc; + goto CATCH; + } + hstmt = NULL; + } + *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY; + goto CATCH; + } + ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s)); + + while (rc == SQLITE_ROW) { + account_s* account_record = NULL; + + account_record = (account_s*) malloc(sizeof(account_s)); + + if (account_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s)); + + _account_convert_column_to_account(hstmt, account_record); + + account_head->account_list = g_list_append(account_head->account_list, account_record); + + rc = _account_query_step(hstmt); + tmp++; + } + + rc = _account_query_finalize(hstmt); + + if (rc != ACCOUNT_ERROR_NONE) + { + _ERR("finalize error"); + *error_code = rc; + goto CATCH; + } + + hstmt = NULL; + + GList *iter; + + + tmp = g_list_length(account_head->account_list); + + for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) { + account_h account; + account = (account_h)iter->data; + + account_s *testaccount = (account_s*)account; + + _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount); + _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount); + + } + + *error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + if (rc != ACCOUNT_ERROR_NONE) + { + _ERR("finalize error"); + *error_code = rc; + } + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + if (account_head) + { + _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list); + return account_head->account_list; + } + return NULL; +} + +GList* +_account_query_account_by_capability(int pid, const char* capability_type, const int capability_value, int *error_code) +{ + *error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + + ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL")); + + if ((capability_value < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) { + ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__); + *error_code = ACCOUNT_ERROR_INVALID_PARAMETER; + return NULL; + } + + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + *error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + return NULL; + } + + int binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, capability_type); + _account_query_bind_int(hstmt, binding_count++, capability_value); + + rc = _account_query_step(hstmt); + + account_s* account_head = NULL; + + ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + int tmp = 0; + + account_head = (account_s*) malloc(sizeof(account_s)); + if (account_head == NULL) { + ACCOUNT_FATAL("malloc Failed"); + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + } + *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY; + return NULL; + } + ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s)); + + while (rc == SQLITE_ROW) { + account_s* account_record = NULL; + + account_record = (account_s*) malloc(sizeof(account_s)); + + if (account_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s)); + + _account_convert_column_to_account(hstmt, account_record); + + account_head->account_list = g_list_append(account_head->account_list, account_record); + + rc = _account_query_step(hstmt); + tmp++; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + GList *iter; + + + tmp = g_list_length(account_head->account_list); + + for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) { + account_h account = NULL; + account = (account_h)iter->data; + account_s* testaccount = (account_s*)account; + + _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount); + _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount); + + } + + + error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + + if (account_head) + { + _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list); + return account_head->account_list; + } + return NULL; +} + +GList* _account_query_account_by_capability_type(int pid, const char* capability_type, int *error_code) +{ + *error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + + ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, + NULL, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + *error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + return NULL; + } + + int binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, capability_type); + + rc = _account_query_step(hstmt); + + account_s* account_head = NULL; + + ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + int tmp = 0; + + account_head = (account_s*) malloc(sizeof(account_s)); + if (account_head == NULL) { + ACCOUNT_FATAL("malloc Failed"); + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + } + *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY; + return NULL; + } + ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s)); + + while (rc == SQLITE_ROW) { + account_s* account_record = NULL; + + account_record = (account_s*) malloc(sizeof(account_s)); + + if (account_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s)); + + _account_convert_column_to_account(hstmt, account_record); + + account_head->account_list = g_list_append(account_head->account_list, account_record); + + rc = _account_query_step(hstmt); + tmp++; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + GList *iter; + + + tmp = g_list_length(account_head->account_list); + + for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) { + account_s* testaccount = (account_s*)iter->data; + + _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount); + _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount); + + } + + *error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) + { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + + if (account_head) + { + _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list); + return account_head->account_list; + } + return NULL; +} + +GList* _account_query_account_by_package_name(int pid,const char* package_name, int *error_code) +{ + _INFO("_account_query_account_by_package_name"); + + *error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + + ACCOUNT_RETURN_VAL((package_name != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("PACKAGE NAME IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + *error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + return NULL; + } + + int binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, package_name); + + rc = _account_query_step(hstmt); + + account_s* account_head = NULL; + + ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name)); + + int tmp = 0; + + account_head = (account_s*) malloc(sizeof(account_s)); + if (account_head == NULL) { + ACCOUNT_FATAL("malloc Failed"); + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + } + *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY; + return NULL; + } + ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s)); + + while (rc == SQLITE_ROW) { + account_s* account_record = NULL; + + account_record = (account_s*) malloc(sizeof(account_s)); + + if (account_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s)); + + _account_convert_column_to_account(hstmt, account_record); + + _INFO("Adding account_list"); + account_head->account_list = g_list_append(account_head->account_list, account_record); + + rc = _account_query_step(hstmt); + tmp++; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + GList *iter; + + tmp = g_list_length(account_head->account_list); + + for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) { + account_s* testaccount = (account_s*)iter->data; + + _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount); + _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount); + } + + *error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) + { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + if ((*error_code == ACCOUNT_ERROR_NONE) && account_head != NULL) + { + _INFO("Returning account_list"); + _remove_sensitive_info_from_non_owning_account_list(pid,account_head->account_list); + return account_head->account_list; + } + return NULL; +} + +int _account_delete(int pid, int account_id) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + int ret_transaction = 0; + bool is_success = FALSE; + + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + int count = -1; + /* Check requested ID to delete */ + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE id=%d", ACCOUNT_TABLE, account_id); + + count = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (count <= 0) { + ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + /* Check permission of requested appid */ + char* current_appid = NULL; + char *package_name = NULL; + + current_appid = _account_get_current_appid(pid); + + error_code = _account_get_package_name_from_account_id(account_id, &package_name); + + if(error_code != ACCOUNT_ERROR_NONE){ + ACCOUNT_ERROR("No package name with account_id\n"); + _ACCOUNT_FREE(current_appid); + _ACCOUNT_FREE(package_name); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + ACCOUNT_DEBUG( "DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); + + error_code = _account_check_appid_group_with_package_name(current_appid, package_name); + + _ACCOUNT_FREE(current_appid); + _ACCOUNT_FREE(package_name); + + if(error_code != ACCOUNT_ERROR_NONE){ + ACCOUNT_ERROR("No permission to delete\n"); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + /* transaction control required*/ + ret_transaction = _account_begin_transaction(); + + if( _account_db_err_code() == SQLITE_PERM ){ + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + return ret_transaction; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + rc = _account_query_finalize(hstmt); + + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + ACCOUNT_MEMSET(query, 0, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE id = %d", ACCOUNT_TABLE, account_id); + + hstmt = _account_prepare_query(query); + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc)); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + /* delete custom data */ + ACCOUNT_MEMSET(query, 0, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id); + + hstmt = _account_prepare_query(query); + + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc)); + + rc = _account_query_finalize(hstmt); + ACCOUNT_CATCH_ERROR(rc == ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc)); + hstmt = NULL; + + is_success = TRUE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + if(rc != ACCOUNT_ERROR_NONE ){ + ACCOUNT_ERROR("rc (%d)", rc); + is_success = FALSE; + } + + hstmt = NULL; + } + + ret_transaction = _account_end_transaction(is_success); + + if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success); + } else { + if (is_success == true) { + char buf[64]={0,}; + ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id); + _account_insert_delete_update_notification_send(buf); + } + } + + pthread_mutex_unlock(&account_mutex); + + return error_code; + +} + +static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account) +{ + //FIXME + //return -1; + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + int binding_count = 1; + + ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL")); + ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL")); + ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _account_query_bind_text(hstmt, binding_count++, username); + _account_query_bind_text(hstmt, binding_count++, package_name); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + account_s *account_record = (account_s *)(*account); + + while (rc == SQLITE_ROW) { + _account_convert_column_to_account(hstmt, account_record); + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record); + _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record); + + hstmt = NULL; + error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + return error_code; +} + +int _account_create(account_h *account) +{ + if (!account) { + ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_s *data = (account_s*)malloc(sizeof(account_s)); + + if (data == NULL) { + ACCOUNT_FATAL("Memory Allocation Failed"); + return ACCOUNT_ERROR_OUT_OF_MEMORY; + } + ACCOUNT_MEMSET(data, 0, sizeof(account_s)); + + /*Setting account as visible by default*/ + data->secret = ACCOUNT_SECRECY_VISIBLE; + + /*Setting account as not supporting sync by default*/ + data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT; + + *account = (account_h)data; + + return ACCOUNT_ERROR_NONE; +} + +int _account_destroy(account_h account) +{ + account_s *data = (account_s*)account; + + ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!")); + + _account_free_account_items(data); + _ACCOUNT_FREE(data); + + return ACCOUNT_ERROR_NONE; +} + +int _account_get_account_id(account_s* account, int *account_id) +{ + if (!account) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + if (!account_id) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + *account_id = account->id; + + return ACCOUNT_ERROR_NONE; +} + +int _account_delete_from_db_by_user_name(int pid, const char *user_name, const char *package_name) +{ + _INFO("[%s][%s]", user_name, package_name); + + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + int ret_transaction = 0; + bool is_success = FALSE; + account_h account = NULL; + int binding_count = 1; + int account_id = -1; + + ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!")); + ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + /* Check permission of requested appid */ + char* current_appid = NULL; + char* package_name_temp = NULL; + + current_appid = _account_get_current_appid(pid); + + package_name_temp = _account_get_text(package_name); + + ACCOUNT_DEBUG( "DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp); + + error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp); + + _ACCOUNT_FREE(current_appid); + _ACCOUNT_FREE(package_name_temp); + + if(error_code != ACCOUNT_ERROR_NONE){ + ACCOUNT_ERROR("No permission to delete\n"); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + rc = _account_create(&account); + rc = _account_query_account_by_username_and_package(user_name, package_name, &account); + + _INFO(""); + + if( _account_db_err_code() == SQLITE_PERM ) + { + _account_destroy(account); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _INFO(""); + account_s* account_data = (account_s*)account; + + rc = _account_get_account_id(account_data, &account_id); + + rc = _account_destroy(account); + + /* transaction control required*/ + ret_transaction = _account_begin_transaction(); + + if( _account_db_err_code() == SQLITE_PERM ) + { + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _INFO(""); + if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ) + { + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + else if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + return ret_transaction; + } + + /* delete custom data */ + ACCOUNT_MEMSET(query, 0, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + _account_end_transaction(FALSE); + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + _account_query_bind_int(hstmt, binding_count++, account_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + /* delete capability */ + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE); + + hstmt = _account_prepare_query(query); + + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, user_name); + _account_query_bind_text(hstmt, binding_count++, package_name); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + ACCOUNT_MEMSET(query, 0, sizeof(query)); + + _INFO(""); + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE); + + hstmt = _account_prepare_query(query); + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + _INFO(""); + binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, user_name); + _account_query_bind_text(hstmt, binding_count++, package_name); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. user_name=%s, package_name=%s, rc=%d\n", user_name, package_name, rc)); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + is_success = TRUE; + + hstmt = NULL; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + ret_transaction = _account_end_transaction(is_success); + + if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success); + } else { + if (is_success == true) { + char buf[64]={0,}; + ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id); + _account_insert_delete_update_notification_send(buf); + } + } + + pthread_mutex_unlock(&account_mutex); + + return error_code; +} + +int _account_delete_from_db_by_package_name(int pid, const char *package_name, gboolean permission) +{ + _INFO("_account_delete_from_db_by_package_name"); + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + int ret_transaction = 0; + bool is_success = FALSE; + int binding_count = 1; + GSList *account_id_list = NULL; + int ret = -1; + + ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + /* Check permission of requested appid */ + if(permission){ + char* current_appid = NULL; + char* package_name_temp = NULL; + + current_appid = _account_get_current_appid(pid); + + package_name_temp = _account_get_text(package_name); + + ACCOUNT_DEBUG( "DELETE: current_appid[%s], package_name[%s]", current_appid, package_name_temp); + + error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp); + + _ACCOUNT_FREE(current_appid); + _ACCOUNT_FREE(package_name_temp); + + if(error_code != ACCOUNT_ERROR_NONE){ + ACCOUNT_ERROR("No permission to delete\n"); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + } + + // It only needs list of ids, does not need to query sensitive info. So sending 0 + GList* account_list_temp = _account_query_account_by_package_name(getpid(), package_name, &ret); + if (account_list_temp == NULL) + { + _ERR("_account_query_account_by_package_name returned NULL"); + return ACCOUNT_ERROR_DB_FAILED; + } + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if(ret != ACCOUNT_ERROR_NONE){ + return ret; + } + + account_list_temp = g_list_first(account_list_temp); + _INFO("account_list_temp length=[%d]",g_list_length(account_list_temp)); + + GList* iter = NULL; + for (iter = account_list_temp; iter != NULL; iter = g_list_next(iter)) + { + _INFO("iterating account_list_temp"); + account_s *account = NULL; + _INFO("Before iter->data"); + account = (account_s*)iter->data; + _INFO("After iter->data"); + if (account != NULL) + { + char* id = (char*) calloc(1, sizeof(account->id)); + + sprintf(id, "%d", account->id); + + _INFO("Adding account id [%s]", id); + account_id_list = g_slist_append(account_id_list, g_strdup(id)); + } + } + + /* transaction control required*/ + ret_transaction = _account_begin_transaction(); + + if( _account_db_err_code() == SQLITE_PERM ){ + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DATABASE_BUSY; + }else if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + return ret_transaction; + } + + /* delete custom table */ + ACCOUNT_MEMSET(query, 0, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + _account_end_transaction(FALSE); + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, package_name); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + /* delete capability table */ + ACCOUNT_MEMSET(query, 0, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE); + + hstmt = _account_prepare_query(query); + + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, package_name); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + /* delete account table */ + ACCOUNT_MEMSET(query, 0, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE); + + hstmt = _account_prepare_query(query); + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, package_name); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. package_name=%s, rc=%d\n", package_name, rc)); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + is_success = TRUE; + + hstmt = NULL; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + ret_transaction = _account_end_transaction(is_success); + + if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success); + } else { + if (is_success == true) { + GSList* gs_iter = NULL; + for (gs_iter = account_id_list; gs_iter != NULL; gs_iter = g_slist_next(gs_iter)) { + char* p_tmpid = NULL; + p_tmpid = (char*)gs_iter->data; + char buf[64]={0,}; + ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%s", ACCOUNT_NOTI_NAME_DELETE, p_tmpid); + ACCOUNT_SLOGD("%s", buf); + _account_insert_delete_update_notification_send(buf); + _ACCOUNT_FREE(p_tmpid); + } + g_slist_free(account_id_list); + } + } + + pthread_mutex_unlock(&account_mutex); + + _INFO("_account_delete_from_db_by_package_name end"); + return error_code; +} + +int _account_get_total_count_from_db(gboolean include_hidden, int *count) +{ + if (!count) { + ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if(!g_hAccountDB){ + ACCOUNT_ERROR("DB is not opened\n"); + return ACCOUNT_ERROR_DB_NOT_OPENED; + } + + char query[1024] = {0, }; + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + if (include_hidden) + { + ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE); + } + else + { + ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, ACCOUNT_SECRECY_VISIBLE); + } + + *count = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + int rc = -1; + int ncount = 0; + account_stmt pStmt = NULL; + + rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL); + if (SQLITE_OK != rc) { + ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg()); + sqlite3_finalize(pStmt); + return ACCOUNT_ERROR_DB_FAILED; + } + + rc = sqlite3_step(pStmt); + if (SQLITE_ROW != rc) { + ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n"); + sqlite3_finalize(pStmt); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + ncount = sqlite3_column_int(pStmt, 0); + + *count = ncount; + + sqlite3_finalize(pStmt); + + if (ncount < 0) { + ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount); + return ACCOUNT_ERROR_DB_FAILED; + } + + return ACCOUNT_ERROR_NONE; +} + +static int _account_type_free_label_items(label_s *data) +{ + _ACCOUNT_FREE(data->app_id); + _ACCOUNT_FREE(data->label); + _ACCOUNT_FREE(data->locale); + + return ACCOUNT_ERROR_NONE; +} + +static int _account_type_free_feature_items(provider_feature_s *data) +{ + _ACCOUNT_FREE(data->app_id); + _ACCOUNT_FREE(data->key); + + return ACCOUNT_ERROR_NONE; +} + +static int _account_type_gslist_free(GSList* list) +{ + ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL")); + + GSList* iter; + + for (iter = list; iter != NULL; iter = g_slist_next(iter)) { + label_s *label_data = (label_s*)iter->data; + _account_type_free_label_items(label_data); + _ACCOUNT_FREE(label_data); + } + + g_slist_free(list); + list = NULL; + + return ACCOUNT_ERROR_NONE; +} + +static int _account_type_item_free(account_type_s *data) +{ + _ACCOUNT_FREE(data->app_id); + _ACCOUNT_FREE(data->service_provider_id); + _ACCOUNT_FREE(data->icon_path); + _ACCOUNT_FREE(data->small_icon_path); + + return ACCOUNT_ERROR_NONE; +} + +static int _account_type_glist_free(GList* list) +{ + ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL")); + + GList* iter; + + for (iter = list; iter != NULL; iter = g_list_next(iter)) { + account_type_s *account_type_record = (account_type_s*)iter->data; + _account_type_item_free(account_type_record); + _ACCOUNT_FREE(account_type_record); + } + + g_list_free(list); + list = NULL; + + return ACCOUNT_ERROR_NONE; +} + +static int _account_type_free_account_type_items(account_type_s *data) +{ + _account_type_item_free(data); + + _account_type_gslist_free(data->label_list); + _account_type_glist_free(data->account_type_list); + + return ACCOUNT_ERROR_NONE; +} + +int account_type_create(account_type_h *account_type) +{ + if (!account_type) { + ACCOUNT_SLOGE("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s)); + + if (data == NULL) { + ACCOUNT_ERROR("Memory Allocation Failed"); + return ACCOUNT_ERROR_OUT_OF_MEMORY; + } + + ACCOUNT_MEMSET(data, 0, sizeof(account_type_s)); + + *account_type = (account_type_h)data; + + return ACCOUNT_ERROR_NONE; +} + +int account_type_destroy(account_type_h account_type) +{ + account_type_s *data = (account_type_s*)account_type; + + ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!")); + + _account_type_free_account_type_items(data); + _ACCOUNT_FREE(data); + + return ACCOUNT_ERROR_NONE; +} + +//app_id mandatory field +int account_type_set_app_id(account_type_h account_type, const char *app_id) +{ + if (!account_type) { + ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!app_id) { + ACCOUNT_SLOGE("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + + _ACCOUNT_FREE(data->app_id); + data->app_id = _account_get_text(app_id); + + return ACCOUNT_ERROR_NONE; +} + +//service_provider_id mandatory field +int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!service_provider_id) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + + _ACCOUNT_FREE(data->service_provider_id); + data->service_provider_id = _account_get_text(service_provider_id); + + return ACCOUNT_ERROR_NONE; +} + +int account_type_set_icon_path(account_type_h account_type, const char *icon_path) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!icon_path) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + + _ACCOUNT_FREE(data->icon_path); + data->icon_path = _account_get_text(icon_path); + + return ACCOUNT_ERROR_NONE; +} + +int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!small_icon_path) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + + _ACCOUNT_FREE(data->small_icon_path); + data->small_icon_path = _account_get_text(small_icon_path); + + return ACCOUNT_ERROR_NONE; +} + +int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support) +{ + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__)); + + account_type_s *data = (account_type_s*)account_type; + + data->multiple_account_support = multiple_account_support; + + return ACCOUNT_ERROR_NONE; +} + +// unset? +int account_type_set_label(account_type_h account_type, const char* label, const char* locale) +{ + if (!account_type) { + ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if(!label || !locale) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + label_s *label_data = (label_s*)malloc(sizeof(label_s)); + + if (label_data == NULL) { + return ACCOUNT_ERROR_OUT_OF_MEMORY; + } + ACCOUNT_MEMSET(label_data, 0, sizeof(label_s)); + + label_data->label = _account_get_text(label); + label_data->locale = _account_get_text(locale); + + data->label_list = g_slist_append(data->label_list, (gpointer)label_data); + + return ACCOUNT_ERROR_NONE; +} + +int account_type_get_app_id(account_type_h account_type, char **app_id) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!app_id) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + + (*app_id) = NULL; + *app_id = _account_get_text(data->app_id); + + return ACCOUNT_ERROR_NONE; +} + +int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!service_provider_id) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + + (*service_provider_id) = NULL; + *service_provider_id = _account_get_text(data->service_provider_id); + + return ACCOUNT_ERROR_NONE; +} + +int account_type_get_icon_path(account_type_h account_type, char **icon_path) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!icon_path) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + + (*icon_path) = NULL; + *icon_path = _account_get_text(data->icon_path); + + return ACCOUNT_ERROR_NONE; +} + +int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!small_icon_path) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + + (*small_icon_path) = NULL; + *small_icon_path = _account_get_text(data->small_icon_path); + + return ACCOUNT_ERROR_NONE; +} + +int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + if (!multiple_account_support) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + + *multiple_account_support = data->multiple_account_support; + + return ACCOUNT_ERROR_NONE; +} + +int account_type_get_label_by_locale(account_type_h account_type, const char* locale, char** label) +{ + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID PARAMETER")); + + GSList *iter; + account_type_s *data = (account_type_s*)account_type; + + for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) { + label_s *label_data = NULL; + + label_data = (label_s*)iter->data; + + *label = NULL; + + if(!strcmp(locale, label_data->locale)) { + *label = _account_get_text(label_data->label); + return ACCOUNT_ERROR_NONE; + } + } + + return ACCOUNT_ERROR_RECORD_NOT_FOUND; +} + +int account_type_get_label(account_type_h account_type, account_label_cb callback, void *user_data) +{ + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); + + GSList *iter; + account_type_s *data = (account_type_s*)account_type; + + for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) { + label_s *label_data = NULL; + + label_data = (label_s*)iter->data; + + if(callback(label_data->app_id, label_data->label, label_data->locale, user_data)!=TRUE) { + ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n"); + return ACCOUNT_ERROR_NONE; + } + } + + return ACCOUNT_ERROR_NONE; +} + +static gboolean _account_type_check_duplicated(account_type_s *data) +{ + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int count = 0; + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'" + , ACCOUNT_TYPE_TABLE, data->app_id); + + count = _account_get_record_count(query); + if (count > 0) { + return TRUE; + } + + return FALSE; +} + +static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value) +{ + _INFO(""); + + int count = 1; + + /*Caution : Keep insert query orders.*/ + + /* 1. app id*/ + _account_query_bind_text(hstmt, count++, (char*)account_type->app_id); + + /* 2. service provider id*/ + _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id); + + /* 3. icon path*/ + _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path); + + /* 4. small icon path*/ + _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path); + + /* 5. multiple accont support*/ + _account_query_bind_int(hstmt, count++, account_type->multiple_account_support); + + _INFO(""); + + return count; +} + + +static int _account_type_execute_insert_query(account_type_s *account_type) +{ + _INFO(""); + + int rc = 0; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + + /* check mandatory field */ + // app id & service provider id + if (!account_type->app_id) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values " + "(?, ?, ?, ?, ?)", ACCOUNT_TYPE_TABLE); + + _INFO(""); + hstmt = _account_prepare_query(query); + _INFO(""); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } else if( _account_db_err_code() == SQLITE_BUSY ){ + ACCOUNT_ERROR( "Database Busy(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + _INFO(""); + _account_type_convert_account_to_sql(account_type, hstmt, query); + _INFO(""); + + rc = _account_query_step(hstmt); + if (rc == SQLITE_BUSY) { + ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + error_code = ACCOUNT_ERROR_DATABASE_BUSY; + } else if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + error_code = ACCOUNT_ERROR_DB_FAILED; + } + + _INFO(""); + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + _INFO(""); + return error_code; +} + +static int _account_type_insert_label(account_type_s *account_type) +{ + int rc, count = 1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + + if (g_slist_length( account_type->label_list)==0) { + ACCOUNT_ERROR( "_account_type_insert_label, no label\n"); + return ACCOUNT_ERROR_NONE; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id); + + rc = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (rc <= 0) { + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + /* insert query*/ + GSList *iter; + + for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) { + int ret; + count = 1; + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES " + "(?, ?, ?) ", LABEL_TABLE); + + hstmt = _account_prepare_query(query); + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + label_s* label_data = NULL; + label_data = (label_s*)iter->data; + + ret = _account_query_bind_text(hstmt, count++, account_type->app_id); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, label_data->label); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + break; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + } + + return ACCOUNT_ERROR_NONE; +} + +static void _account_type_convert_column_to_provider_feature(account_stmt hstmt, provider_feature_s *feature_record) +{ + const char *textbuf = NULL; + + textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_APP_ID); + _account_db_data_to_text(textbuf, &(feature_record->app_id)); + + textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_KEY); + _account_db_data_to_text(textbuf, &(feature_record->key)); + +} + +GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code) +{ + _INFO("_account_type_query_provider_feature_by_app_id"); + *error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0, binding_count = 1; + GSList* feature_list = NULL; + + ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE); + _INFO("account query=[%s]", query); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + *error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + return NULL; + } + + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {*error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;}, NULL, ("The record isn't found.\n")); + + provider_feature_s* feature_record = NULL; + + while (rc == SQLITE_ROW) { + + feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s)); + + if (feature_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s)); + + _account_type_convert_column_to_provider_feature(hstmt, feature_record); + + _INFO("Adding account feature_list"); + feature_list = g_slist_append(feature_list, feature_record); + + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("account finalize error")); + hstmt = NULL; + + *error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("account finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + + _INFO("Returning account feature_list"); + return feature_list; +} + +int account_type_query_provider_feature_by_app_id(provider_feature_cb callback, const char* app_id, void *user_data ) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0, binding_count = 1; + + ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + provider_feature_s* feature_record = NULL; + + while (rc == SQLITE_ROW) { + bool cb_ret = FALSE; + feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s)); + + if (feature_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s)); + + _account_type_convert_column_to_provider_feature(hstmt, feature_record); + + cb_ret = callback(feature_record->app_id, feature_record->key, user_data); + + _account_type_free_feature_items(feature_record); + _ACCOUNT_FREE(feature_record); + + ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n")); + + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + return error_code; +} + +bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code) +{ + _INFO("_account_type_query_supported_feature start"); + + *error_code = ACCOUNT_ERROR_NONE; + + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int record_count = 0; + + if (app_id == NULL || capability == NULL) + { + *error_code = ACCOUNT_ERROR_INVALID_PARAMETER; + return false; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability); + + record_count = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + *error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + return false; + } + + if (record_count <= 0) + { + *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + return false; + } + + _INFO("_account_type_query_supported_feature end"); + return true; + +} + + +int account_type_get_provider_feature_all(account_type_h account_type, provider_feature_cb callback, void* user_data) +{ + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); + + GSList *iter; + account_type_s *data = (account_type_s*)account_type; + + for (iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) { + provider_feature_s *feature_data = NULL; + + feature_data = (provider_feature_s*)iter->data; + + if(callback(feature_data->app_id, feature_data->key, user_data)!=TRUE) { + ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n"); + return ACCOUNT_ERROR_NONE; + } + } + + return ACCOUNT_ERROR_NONE; +} + +int account_type_set_provider_feature(account_type_h account_type, const char* provider_feature) +{ + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type handle is null")); + ACCOUNT_RETURN_VAL((provider_feature != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("provider_feature is null")); + + account_type_s *data = (account_type_s*)account_type; + + GSList *iter = NULL; + bool b_is_new = TRUE; + + for(iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) { + provider_feature_s *feature_data = NULL; + feature_data = (provider_feature_s*)iter->data; + + if(!strcmp(feature_data->key, provider_feature)) { + b_is_new = FALSE; + break; + } + } + + if(b_is_new) { + provider_feature_s* feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s)); + + if (feature_data == NULL) + return ACCOUNT_ERROR_OUT_OF_MEMORY; + ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s)); + + feature_data->key = _account_get_text(provider_feature); + data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data); + } + + return ACCOUNT_ERROR_NONE; +} + +static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id) +{ + int rc, count = 1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); + + if (g_slist_length( account_type->provider_feature_list)==0) { + ACCOUNT_ERROR( "no capability\n"); + return ACCOUNT_ERROR_NONE; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id); + + rc = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (rc <= 0) { + ACCOUNT_SLOGI( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + /* insert query*/ + + GSList *iter; + + for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) { + int ret; + count = 1; + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES " + "(?, ?) ", PROVIDER_FEATURE_TABLE); + + hstmt = _account_prepare_query(query); + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + provider_feature_s* feature_data = NULL; + feature_data = (provider_feature_s*)iter->data; + + ret = _account_query_bind_text(hstmt, count++, app_id); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, feature_data->key); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); + + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + break; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + } + + return ACCOUNT_ERROR_NONE; +} + +int _account_type_insert_to_db(account_type_s* account_type, int* account_type_id) +{ + _INFO(""); + + int error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0; + + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL")); + + account_type_s *data = (account_type_s*)account_type; + + pthread_mutex_lock(&account_mutex); + + + /* transaction control required*/ + ret_transaction = _account_begin_transaction(); + + _INFO(""); + + if( _account_db_err_code() == SQLITE_PERM ){ + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _INFO(""); + if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DATABASE_BUSY; + } else if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + return ret_transaction; + } + + _INFO(""); + if (_account_type_check_duplicated(data)) { + _INFO(""); + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction); + *account_type_id = -1; + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DUPLICATED; + } else { + _INFO(""); + *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE); + + error_code = _account_type_execute_insert_query(data); + + if (error_code != ACCOUNT_ERROR_NONE){ + error_code = ACCOUNT_ERROR_DUPLICATED; + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction); + *account_type_id = -1; + pthread_mutex_unlock(&account_mutex); + return error_code; + } + } + + _INFO(""); + error_code = _account_type_insert_provider_feature(data, data->app_id); + if(error_code != ACCOUNT_ERROR_NONE) { + _INFO(""); + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction); + pthread_mutex_unlock(&account_mutex); + return error_code; + } + _INFO(""); + error_code = _account_type_insert_label(data); + if(error_code != ACCOUNT_ERROR_NONE) { + _INFO(""); + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction); + pthread_mutex_unlock(&account_mutex); + return error_code; + } + + ret_transaction = _account_end_transaction(TRUE); + _INFO(""); + pthread_mutex_unlock(&account_mutex); + + _INFO(""); + return ACCOUNT_ERROR_NONE; +} + +static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id) +{ + int rc, count = 1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + + if (g_slist_length( account_type->provider_feature_list)==0) { + ACCOUNT_ERROR( "no feature\n"); + return ACCOUNT_ERROR_NONE; + } + + ACCOUNT_DEBUG( "app id", app_id); + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + count = 1; + _account_query_bind_text(hstmt, count++, app_id); + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_DB_FAILED; + } + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + GSList *iter; + + for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) { + int ret; + count = 1; + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES " + "(?, ?) ", PROVIDER_FEATURE_TABLE); + + hstmt = _account_prepare_query(query); + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + provider_feature_s* feature_data = NULL; + feature_data = (provider_feature_s*)iter->data; + + ret = _account_query_bind_text(hstmt, count++, account_type->app_id); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, feature_data->key); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + break; + } + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + return ACCOUNT_ERROR_NONE; +} + +static int _account_type_update_label(account_type_s *account_type, const char* app_id) +{ + int rc, count = 1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + + if (g_slist_length( account_type->label_list)==0) { + return ACCOUNT_ERROR_NONE; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + count = 1; + _account_query_bind_text(hstmt, count++, app_id); + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_DB_FAILED; + } + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + GSList *iter; + + for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) { + int ret; + count = 1; + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES " + "(?, ?, ?) ", LABEL_TABLE); + + hstmt = _account_prepare_query(query); + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + label_s* label_data = NULL; + label_data = (label_s*)iter->data; + + ret = _account_query_bind_text(hstmt, count++, account_type->app_id); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, label_data->label); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, label_data->locale); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + break; + } + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + return ACCOUNT_ERROR_NONE; +} + + +static int _account_type_update_account(account_type_s *account_type, const char* app_id) +{ + int rc = 0, binding_count =1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + + if (!account_type->app_id) { + ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, " + "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } else if (_account_db_err_code() == SQLITE_BUSY){ + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg())); + + binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query); + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + /*update label*/ + error_code = _account_type_update_label(account_type, app_id); + /* update provider feature */ + error_code = _account_type_update_provider_feature(account_type, app_id); + + return error_code; +} + +int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id) +{ + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL")); + ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + int error_code = ACCOUNT_ERROR_NONE; + account_type_s* data = account_type; + + pthread_mutex_lock(&account_mutex); + + error_code = _account_type_update_account(data, app_id); + + pthread_mutex_unlock(&account_mutex); + + return error_code; +} + +int _account_type_delete_by_app_id(const char* app_id) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0, count = -1; + int ret_transaction = 0; + int binding_count = 1; + bool is_success = FALSE; + + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected.")); + + /* Check requested ID to delete */ + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id); + + count = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (count <= 0) { + ACCOUNT_SLOGE("app id(%s) is not exist. count(%d)\n", app_id, count); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + /* transaction control required*/ + ret_transaction = _account_begin_transaction(); + + if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DATABASE_BUSY; + }else if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + return ret_transaction; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + binding_count = 1; + ACCOUNT_MEMSET(query, 0, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE); + + hstmt = _account_prepare_query(query); + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc)); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + is_success = TRUE; + + hstmt = NULL; + + binding_count = 1; + ACCOUNT_MEMSET(query, 0, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE); + + hstmt = _account_prepare_query(query); + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc)); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + is_success = TRUE; + + hstmt = NULL; + + CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + ret_transaction = _account_end_transaction(is_success); + + if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success); + } + + pthread_mutex_unlock(&account_mutex); + + return error_code; +} + +static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record) +{ + const char *textbuf = NULL; + + account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID); + _account_db_data_to_text(textbuf, &(account_type_record->app_id)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID); + _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH); + _account_db_data_to_text(textbuf, &(account_type_record->icon_path)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH); + _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path)); + + account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT); + +} + +static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record) +{ + const char *textbuf = NULL; + + textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID); + _account_db_data_to_text(textbuf, &(label_record->app_id)); + + textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL); + _account_db_data_to_text(textbuf, &(label_record->label)); + + textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE); + _account_db_data_to_text(textbuf, &(label_record->locale)); + +} + +GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code ) +{ + *error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0, binding_count = 1; + GSList* label_list = NULL; + + ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + *error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + return NULL; + } + + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + label_s* label_record = NULL; + + while (rc == SQLITE_ROW) { + label_record = (label_s*) malloc(sizeof(label_s)); + + if (label_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s)); + + _account_type_convert_column_to_label(hstmt, label_record); + + _INFO("Adding account label_list"); + label_list = g_slist_append (label_list, label_record); + + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + + *error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + _INFO("Returning account label_list"); + return label_list; +} + +int account_type_query_label_by_app_id(account_label_cb callback, const char* app_id, void *user_data ) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0, binding_count = 1; + + ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + label_s* label_record = NULL; + + while (rc == SQLITE_ROW) { + bool cb_ret = FALSE; + label_record = (label_s*) malloc(sizeof(label_s)); + + if (label_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s)); + + _account_type_convert_column_to_label(hstmt, label_record); + + cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data); + + _account_type_free_label_items(label_record); + _ACCOUNT_FREE(label_record); + + ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n")); + + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + return error_code; +} + +int _account_type_label_get_app_id(label_h label, char **app_id) +{ + if (!label) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!app_id) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + label_s *data = (label_s*)label; + + (*app_id) = NULL; + + *app_id = _account_get_text(data->app_id); + + return ACCOUNT_ERROR_NONE; +} + +bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data) +{ + account_type_s *data = (account_type_s*)user_data; + + label_s *label_data = (label_s*)malloc(sizeof(label_s)); + + if (label_data == NULL) { + ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n"); + return FALSE; + } + ACCOUNT_MEMSET(label_data, 0, sizeof(label_s)); + + label_data->app_id = _account_get_text(app_id); + label_data->label = _account_get_text(label); + label_data->locale = _account_get_text(locale); + + data->label_list = g_slist_append(data->label_list, (gpointer)label_data); + + return TRUE; +} + +bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data) +{ + account_type_s *data = (account_type_s*)user_data; + + provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s)); + + if (feature_data == NULL) { + ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n"); + return FALSE; + } + ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s)); + + feature_data->app_id = _account_get_text(app_id); + feature_data->key = _account_get_text(key); + + data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data); + + return TRUE; +} + +int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record) +{ + _INFO("_account_type_query_by_app_id start"); + + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0, binding_count = 1; + + ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + *account_type_record = create_empty_account_type_instance(); + + while (rc == SQLITE_ROW) { + _account_type_convert_column_to_account_type(hstmt, *account_type_record); + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record)); + account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record)); + + hstmt = NULL; + error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + _INFO("_account_type_query_by_app_id end [%d]", error_code); + return error_code; +} + +int _account_type_query_app_id_exist(const char* app_id) +{ + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + + ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id); + rc = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (rc <= 0) { + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + return ACCOUNT_ERROR_NONE; +} + +GSList* _account_type_query_by_provider_feature(const char* key, int *error_code) +{ + *error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + GSList *account_type_list = NULL; + + if(key == NULL) + { + ACCOUNT_ERROR("capability_type IS NULL."); + *error_code = ACCOUNT_ERROR_INVALID_PARAMETER; + goto CATCH; + } + + if(g_hAccountDB == NULL) + { + ACCOUNT_ERROR("The database isn't connected."); + *error_code = ACCOUNT_ERROR_DB_NOT_OPENED; + goto CATCH; + } + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ) + { + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + *error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + goto CATCH; + } + + int binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, key); + + rc = _account_query_step(hstmt); + + account_type_s *account_type_record = NULL; + + if(rc != SQLITE_ROW) + { + ACCOUNT_ERROR("The record isn't found."); + *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + goto CATCH; + } + + while(rc == SQLITE_ROW) { + account_type_record = (account_type_s*) malloc(sizeof(account_type_s)); + + if (account_type_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s)); + _account_type_convert_column_to_account_type(hstmt, account_type_record); + account_type_list = g_slist_append(account_type_list, account_type_record); + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + if (rc != ACCOUNT_ERROR_NONE ) + { + _account_type_gslist_free(account_type_list); + ACCOUNT_ERROR("finalize error(%s)", rc); + *error_code = rc; + goto CATCH; + } + hstmt = NULL; + + GSList* iter; + + for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) { + account_type_s *account_type = NULL; + account_type = (account_type_s*)iter->data; + account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type); + account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type); + } + + *error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + if (rc != ACCOUNT_ERROR_NONE) + { + *error_code = rc; + return NULL; + } + hstmt = NULL; + } + + return account_type_list; +} + + +GSList* _account_type_query_all(void) +{ + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + GSList *account_type_list = NULL; + + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE); + hstmt = _account_prepare_query(query); + + rc = _account_query_step(hstmt); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return NULL; + } + + account_type_s *account_type_record = NULL; + + if (rc != SQLITE_ROW) + { + _INFO("[ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found."); + goto CATCH; + } + + while(rc == SQLITE_ROW) { + account_type_record = (account_type_s*) malloc(sizeof(account_type_s)); + + if (account_type_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s)); + _account_type_convert_column_to_account_type(hstmt, account_type_record); + account_type_list = g_slist_append(account_type_list, account_type_record); + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error")); + hstmt = NULL; + + GSList* iter; + + for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) { + account_type_s *account_type = NULL; + account_type = (account_type_s*)iter->data; + account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type); + account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type); + } + +CATCH: + if (hstmt != NULL) + { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_type_gslist_free(account_type_list);}, NULL, ("finalize error")); + hstmt = NULL; + } + + return account_type_list; +} + +// output parameter label must be free +int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0, binding_count = 1; + char* converted_locale = NULL; + + ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null")); + ACCOUNT_RETURN_VAL((locale != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null")); + //Making label newly created + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + converted_locale = _account_get_text(locale); + gchar** tokens = g_strsplit(converted_locale, "-", 2); + + if(tokens != NULL) { + if((char*)(tokens[1]) != NULL) { + char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1])); + if(upper_token != NULL) { + _ACCOUNT_FREE(converted_locale); + converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token); + } + _ACCOUNT_FREE(upper_token); + } + } + g_strfreev(tokens); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale); + _ACCOUNT_FREE(converted_locale); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + label_s* label_record = NULL; + + while (rc == SQLITE_ROW) { + label_record = (label_s*) malloc(sizeof(label_s)); + + if (label_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s)); + + _account_type_convert_column_to_label(hstmt,label_record); + + _ACCOUNT_FREE(*label); + //Making label newly created + *label = _account_get_text(label_record->label); + + _account_type_free_label_items(label_record); + _ACCOUNT_FREE(label_record); + + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + error_code = ACCOUNT_ERROR_NONE; + + CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code); + return error_code; +} + +static int _account_insert_custom(account_s *account, int account_id) +{ + _INFO("_account_insert_custom start"); + + int rc, count = 1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + + if (g_slist_length( account->custom_list)==0) { + ACCOUNT_DEBUG( "_account_insert_custom, no custom data\n"); + return ACCOUNT_ERROR_NONE; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where id=%d", ACCOUNT_TABLE, account_id); + + rc = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%d, %s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (rc <= 0) { + ACCOUNT_SLOGE( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + /* insert query*/ + + GSList *iter; + + for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) { + int ret; + count = 1; + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES " + "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + account_custom_s* custom_data = NULL; + custom_data = (account_custom_s*)iter->data; + + ret = _account_query_bind_int(hstmt, count++, account_id); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail")); + ret = _account_query_bind_text(hstmt, count++, account->package_name); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + break; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + } + + _INFO("_account_insert_custom end"); + return ACCOUNT_ERROR_NONE; +} + +static int _account_update_custom(account_s *account, int account_id) +{ + int rc, count = 1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + + if (g_slist_length( account->custom_list)==0) { + ACCOUNT_DEBUG( "_account_update_custom, no custom data\n"); + return ACCOUNT_ERROR_NONE; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where id=%d", ACCOUNT_TABLE, account_id); + + rc = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } else if( _account_db_err_code() == SQLITE_BUSY ){ + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + if (rc <= 0) { + ACCOUNT_SLOGE( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE); + hstmt = _account_prepare_query(query); + count = 1; + _account_query_bind_int(hstmt, count++, (int)account_id); + rc = _account_query_step(hstmt); + + if (rc == SQLITE_BUSY) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_DATABASE_BUSY; + } else if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_DB_FAILED; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + GSList *iter; + + for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) { + int ret; + count = 1; + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES " + "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + account_custom_s* custom_data = NULL; + custom_data = (account_custom_s*)iter->data; + + ret = _account_query_bind_int(hstmt, count++, (int)account_id); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + break; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + } + + return ACCOUNT_ERROR_NONE; +} + +static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data ) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + + ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + rc = _account_query_step(hstmt); + + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + account_custom_s* custom_record = NULL; + + while (rc == SQLITE_ROW) { + bool cb_ret = FALSE; + custom_record = (account_custom_s*) malloc(sizeof(account_custom_s)); + + if (custom_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s)); + + _account_convert_column_to_custom(hstmt, custom_record); + + cb_ret = callback(custom_record->key, custom_record->value, user_data); + + _account_custom_item_free(custom_record); + _ACCOUNT_FREE(custom_record); + + ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n")); + + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + return error_code; +} diff --git a/src/accounts/server/account-server-db.h b/src/accounts/server/account-server-db.h new file mode 100644 index 0000000..41b3e6c --- /dev/null +++ b/src/accounts/server/account-server-db.h @@ -0,0 +1,131 @@ +/* + * + * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef __ACC_SERVER_DB_H__ + +#include +#include "account-private.h" + +typedef sqlite3_stmt* account_stmt; + +//ACCOUNT_TABLE +#define ACCOUNT_SCHEMA "create table account \n"\ + "(\n"\ +"id INTEGER PRIMARY KEY, "\ +"user_name TEXT, "\ +"email_address TEXT, "\ +"display_name TEXT, "\ +"icon_path TEXT, "\ +"source TEXT, "\ +"package_name TEXT, "\ +"access_token TEXT, "\ +"domain_name TEXT, "\ +"auth_method TEXT, "\ +"auth_type INTEGER, "\ +"secret INTEGER, "\ +"sync_support INTEGER, "\ +"txt_custom0 TEXT, "\ +"txt_custom1 TEXT, "\ +"txt_custom2 TEXT, "\ +"txt_custom3 TEXT, "\ +"txt_custom4 TEXT, "\ +"int_custom0 INTEGER, "\ +"int_custom1 INTEGER, "\ +"int_custom2 INTEGER, "\ +"int_custom3 INTEGER, "\ +"int_custom4 INTEGER "\ +");" + +//CAPABILITY_TABLE +#define CAPABILITY_SCHEMA "create table capability \n"\ + "(\n"\ +"_id INTEGER PRIMARY KEY AUTOINCREMENT, "\ +"key TEXT, "\ +"value INTEGER, "\ +"package_name TEXT, "\ +"user_name TEXT, "\ +"account_id INTEGER "\ +");" + +//ACCOUNT_CUSTOM_TABLE +#define ACCOUNT_CUSTOM_SCHEMA "create table account_custom \n"\ + "(\n"\ +"AccountId INTEGER, "\ +"AppId TEXT, "\ +"Key TEXT, "\ +"Value TEXT "\ +");" + +//ACCOUNT_TYPE_TABLE +#define ACCOUNT_TYPE_SCHEMA "create table account_type \n"\ + "(\n"\ +"_id INTEGER PRIMARY KEY AUTOINCREMENT, "\ +"AppId TEXT, "\ +"ServiceProviderId TEXT, "\ +"IconPath TEXT, "\ +"SmallIconPath TEXT, "\ +"MultipleAccountSupport INTEGER "\ +");" + +//LABEL_TABLE +#define LABEL_SCHEMA "create table label \n"\ + "(\n"\ +"AppId TEXT, "\ +"Label TEXT, "\ +"Locale TEXT"\ +");" + +//PROVIDER_FEATURE_TABLE +#define PROVIDER_FEATURE_SCHEMA "create table provider_feature \n"\ + "(\n"\ +"app_id TEXT, "\ +"key TEXT "\ +");" + + +int _account_insert_to_db(account_s* account, int pid, int *account_id); +int _account_db_open(int mode, const char* account_db_path); +int _account_db_close(void); +int _account_type_insert_to_db(account_type_s* account_type, int* account_type_id); +GSList* _account_db_query_all(int pid); +GSList* _account_type_query_all(void); +int _account_delete(int pid, int account_id); +int _account_delete_from_db_by_user_name(int pid, const char *user_name, const char *package_name); +int _account_delete_from_db_by_package_name(int pid, const char *package_name, gboolean permission); +int _account_update_to_db_by_id(int pid, account_s *account, int account_id); +int _account_get_total_count_from_db(gboolean include_hidden, int *count); +int _account_query_account_by_account_id(int pid, int account_db_id, account_s *account_record); +int _account_update_to_db_by_user_name(int pid, account_s* account, const char *user_name, const char *package_name); +int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label); +GSList* _account_type_query_by_provider_feature(const char* key, int *error_code); +GList* _account_query_account_by_user_name(int pid, const char *user_name, int *error_code); +GList* _account_query_account_by_package_name(int pid, const char* package_name, int *error_code); +GList* _account_query_account_by_capability(int pid, const char* capability_type, const int capability_value, int *error_code); +GList* _account_query_account_by_capability_type(int pid, const char* capability_type, int *error_code); +GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code); +int _account_update_sync_status_by_id(int account_db_id, const int sync_status); +GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code); +bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code); +int _account_type_update_to_db_by_app_id(account_type_s *account_type, const char* app_id); +int _account_type_delete_by_app_id(const char* app_id); +GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code ); +int _account_type_query_by_app_id(const char* app_id, account_type_s **account_type_record); +int _account_type_query_app_id_exist(const char* app_id); +int _account_update_to_db_by_id_ex(account_s *account, int account_id); + +#endif diff --git a/src/accounts/server/account-server.c b/src/accounts/server/account-server.c new file mode 100644 index 0000000..f582772 --- /dev/null +++ b/src/accounts/server/account-server.c @@ -0,0 +1,2139 @@ +/* + * + * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#define _DEFAULT_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if !GLIB_CHECK_VERSION (2, 31, 0) +#include +#endif +#include + +#include +#include "dbg.h" +#include "account-server-db.h" +#include "account_ipc_marshal.h" +#include "account-mgr-stub.h" +#include "account-private.h" +#include "account-error.h" + +#define _CHECK_READ_LABEL "libaccounts-svc::check_read" +#define _DB_LABEL "libaccounts-svc::db" +#define _READ_LABEL "r" +#define _WRITE_LABEL "w" +#define _READ_WRITE_LABEL "rw" + +#define ACCOUNT_MGR_DBUS_PATH "/org/tizen/account/manager" +static guint owner_id = 0; +GDBusObjectManagerServer *account_mgr_server_mgr = NULL; +static AccountManager* account_mgr_server_obj = NULL; +//static gboolean has_owner = FALSE; + +// pid-mode, TODO: make it sessionId-mode, were session id is mix of pid and some rand no, so that +// one client can have multiple connections having different modes +//static GHashTable* mode_table = NULL; + +GDBusErrorEntry _account_svc_errors[] = +{ + {ACCOUNT_ERROR_NONE, _ACCOUNT_SVC_ERROR_PREFIX".NoError"}, + {ACCOUNT_ERROR_OUT_OF_MEMORY, _ACCOUNT_SVC_ERROR_PREFIX".OutOfMemory"}, + {ACCOUNT_ERROR_INVALID_PARAMETER, _ACCOUNT_SVC_ERROR_PREFIX".InvalidParameter"}, + {ACCOUNT_ERROR_DUPLICATED, _ACCOUNT_SVC_ERROR_PREFIX".Duplicated"}, + {ACCOUNT_ERROR_NO_DATA, _ACCOUNT_SVC_ERROR_PREFIX".NoData"}, + {ACCOUNT_ERROR_RECORD_NOT_FOUND, _ACCOUNT_SVC_ERROR_PREFIX".RecordNotFound"}, + {ACCOUNT_ERROR_DB_FAILED, _ACCOUNT_SVC_ERROR_PREFIX".DBFailed"}, + {ACCOUNT_ERROR_DB_NOT_OPENED, _ACCOUNT_SVC_ERROR_PREFIX".DBNotOpened"}, + {ACCOUNT_ERROR_QUERY_SYNTAX_ERROR, _ACCOUNT_SVC_ERROR_PREFIX".QuerySynTaxError"}, + {ACCOUNT_ERROR_ITERATOR_END, _ACCOUNT_SVC_ERROR_PREFIX".IteratorEnd"}, + {ACCOUNT_ERROR_NOTI_FAILED, _ACCOUNT_SVC_ERROR_PREFIX".NotiFalied"}, + {ACCOUNT_ERROR_PERMISSION_DENIED, _ACCOUNT_SVC_ERROR_PREFIX".PermissionDenied"}, + {ACCOUNT_ERROR_XML_PARSE_FAILED, _ACCOUNT_SVC_ERROR_PREFIX".XMLParseFailed"}, + {ACCOUNT_ERROR_XML_FILE_NOT_FOUND, _ACCOUNT_SVC_ERROR_PREFIX".FileNotFound"}, + {ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL, _ACCOUNT_SVC_ERROR_PREFIX".SubscriptionFailed"}, + {ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER, _ACCOUNT_SVC_ERROR_PREFIX".NotRegisteredProvider"}, + {ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE, _ACCOUNT_SVC_ERROR_PREFIX".NotAllowMultiple"}, + {ACCOUNT_ERROR_DATABASE_BUSY, _ACCOUNT_SVC_ERROR_PREFIX".database_busy"}, +}; + +static guint +_get_client_pid(GDBusMethodInvocation* invoc) +{ + const char *name = NULL; + name = g_dbus_method_invocation_get_sender(invoc); + if (name == NULL) + { + _ERR("g_dbus_method_invocation_get_sender failed"); + return -1; + } + _INFO("sender=[%s]", name); + + + guint pid = -1; + GError *error = NULL; + GVariant *_ret; + + _INFO("calling GetConnectionUnixProcessID"); + + GDBusConnection* conn = g_dbus_method_invocation_get_connection(invoc); + _ret = g_dbus_connection_call_sync(conn, + "org.freedesktop.DBus", + "/org/freedesktop/DBus", + "org.freedesktop.DBus", + "GetConnectionUnixProcessID", + g_variant_new("(s)", name), + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + + if (_ret != NULL) + { + g_variant_get(_ret, "(u)", &pid); + g_variant_unref(_ret); + } + + _INFO("process Id = [%u]", pid); + return pid; +} + +static GQuark +_account_error_quark (void) +{ + static volatile gsize quark_volatile = 0; + + g_dbus_error_register_error_domain (_ACCOUNT_SVC_ERROR_DOMAIN, + &quark_volatile, + _account_svc_errors, + G_N_ELEMENTS (_account_svc_errors)); + + return (GQuark) quark_volatile; +} + +static int _check_privilege_by_cookie(char *e_cookie, const char *label, const char *access_perm, bool check_root, int pid) { + guchar *cookie = NULL; + gsize size = 0; + int retval = 0; + char buf[128] = {0,}; + FILE *fp = NULL; + char title[128] = {0,}; + int uid = -1; + + if (check_root) { + // Gets the userID from /proc/pid/status to check if the process is the root or not. + snprintf(buf, sizeof(buf), "/proc/%d/status", pid); + fp = fopen(buf, "r"); + if(fp) { + while (fgets(buf, sizeof(buf), fp) != NULL) { + if(strncmp(buf, "Uid:", 4) == 0) { + sscanf(buf, "%s %d", title, &uid); + break; + } + } + fclose(fp); + } + + _INFO("uid : %d", uid); + } + + if (uid != 0) { // Checks the cookie only when the process is not the root + cookie = g_base64_decode(e_cookie, &size); + if (cookie == NULL) { + _ERR("Unable to decode cookie!!!"); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + retval = security_server_check_privilege_by_cookie((const char *)cookie, label, access_perm); + g_free(cookie); + + if (retval < 0) { + if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) { + _ERR("Access to account-svcd has been denied by smack."); + } + _ERR("Error has occurred in security_server_check_privilege_by_cookie() : %d.", retval); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + } + + _INFO("The process(%d) was authenticated successfully.", pid); + return ACCOUNT_ERROR_NONE; +} + +gboolean account_manager_account_add(AccountManager *obj, GDBusMethodInvocation *invocation, gchar* account_db_path, GVariant* account_data, gchar *cookie, gpointer user_data) +{ + _INFO("account_manager_account_add start"); + int db_id = -1; + + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + account_s* account = umarshal_account(account_data); + if (account == NULL) + { + _ERR("account unmarshalling failed"); + return_code = ACCOUNT_ERROR_DB_FAILED; + + goto RETURN; + } + + return_code = _account_insert_to_db(account, pid, &db_id); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_insert_to_db() error"); + + goto RETURN; + } + +RETURN: + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_add(obj, invocation, db_id); + } + _INFO("account_manager_account_add end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_account_query_all(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, gchar *cookie) +{ + _INFO("account_manager_account_query_all start"); + + GVariant* account_list_variant = NULL; + + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + //Mode checking not required, since default mode is read. + + GSList* account_list = NULL; + account_list = _account_db_query_all(pid); + + if (account_list == NULL) + { + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + _ERR("No account found."); + goto RETURN; + } + + _INFO("account_list length= [%d]", g_slist_length(account_list)); + + return_code = 0; + _INFO("before calling marshal_account_list"); + account_list_variant = marshal_account_list(account_list); + _INFO("after calling marshal_account_list"); + +RETURN: + + if (account_list_variant == NULL) + { + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_query_all(obj, invocation, account_list_variant); + } + _INFO("account_manager_account_query_all end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_account_type_query_all(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, gchar *cookie) +{ + _INFO("account_manager_account_query_all start"); + + GVariant* account_type_list_variant = NULL; + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + //Mode checking not required, since default mode is read. + + GSList* account_type_list = NULL; + account_type_list = _account_type_query_all(); + + if (account_type_list == NULL) + { + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + _ERR("No account type found."); + goto RETURN; + } + + _INFO("account_type_list length= [%d]", g_slist_length(account_type_list)); + + return_code = 0; + _INFO("before calling marshal_account_type_list"); + account_type_list_variant = marshal_account_type_list(account_type_list); + _INFO("after calling marshal_account_type_list"); + +RETURN: + + if (account_type_list_variant == NULL) + { + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_type_query_all(obj, invocation, account_type_list_variant); + } + _INFO("account_manager_account_query_all end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_account_type_add(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, GVariant *account_type_data, gchar *cookie, gpointer user_data) +{ + int db_id = -1; + + _INFO("account_manager_account_type_add start"); + + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + account_type_s* account_type = umarshal_account_type(account_type_data); + if (account_type == NULL) + { + _ERR("account_type unmarshalling failed"); + return_code = ACCOUNT_ERROR_DB_FAILED; + goto RETURN; + } + + _INFO("before _account_type_insert_to_db"); + return_code = _account_type_insert_to_db(account_type, &db_id); + _INFO("after _account_type_insert_to_db"); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_insert_to_db error"); + goto RETURN; + } + +RETURN: + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_type_add(obj, invocation, db_id); + } + _INFO("account_manager_account_type_add end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_account_delete_from_db_by_id(AccountManager *object, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + gint account_db_id, gchar *cookie) +{ + _INFO("account_manager_account_delete_from_db_by_id start"); + + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_delete"); + return_code = _account_delete(pid, account_db_id); + _INFO("after _account_delete=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_delete error"); + goto RETURN; + } + +RETURN: + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_delete_from_db_by_id(object, invocation); + } + _INFO("account_manager_account_delete_from_db_by_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_account_delete_from_db_by_user_name(AccountManager *object, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar *user_name, + const gchar *package_name, gchar *cookie) +{ + _INFO("account_manager_account_delete_from_db_by_user_name start"); + + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_delete_from_db_by_user_name"); + return_code = _account_delete_from_db_by_user_name(pid, user_name, package_name); + _INFO("after _account_delete_from_db_by_user_name=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_delete_from_db_by_user_name error"); + goto RETURN; + } + +RETURN: + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_delete_from_db_by_id(object, invocation); + } + _INFO("account_manager_account_delete_from_db_by_user_name end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_account_delete_from_db_by_package_name(AccountManager *object, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar *package_name, gboolean permission, gchar *cookie) +{ + _INFO("account_manager_account_delete_from_db_by_package_name start"); + + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before account_delete_from_db_by_package_name"); + return_code = _account_delete_from_db_by_package_name(pid, package_name, permission); + _INFO("after account_delete_from_db_by_package_name=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_delete_from_db_by_package_name error"); + goto RETURN; + } + +RETURN: + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_delete_from_db_by_package_name(object, invocation); + } + _INFO("account_manager_account_delete_from_db_by_package_name end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_account_update_to_db_by_id(AccountManager *object, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + GVariant *account_data, + gint account_id, gchar *cookie) +{ + _INFO("account_manager_account_update_to_db_by_id start"); + + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + account_s* account = umarshal_account(account_data); + if (account == NULL) + { + _ERR("Unmarshal failed"); + return_code = ACCOUNT_ERROR_DB_FAILED; + goto RETURN; + } + + _INFO("before account_update_to_db_by_id"); + return_code = _account_update_to_db_by_id(pid, account, account_id); + _INFO("after account_update_to_db_by_id=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_update_to_db_by_id error"); + goto RETURN; + } + +RETURN: + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_update_to_db_by_id(object, invocation); + } + _INFO("account_manager_account_update_to_db_by_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_update_to_db_by_user_name(AccountManager *object, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + GVariant *account_data, + const gchar *user_name, + const gchar *package_name, gchar *cookie) +{ + _INFO("account_manager_handle_account_update_to_db_by_user_name start"); + + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + account_s* account = umarshal_account(account_data); + if (account == NULL) + { + _ERR("Unmarshal failed"); + return_code = ACCOUNT_ERROR_DB_FAILED; + goto RETURN; + } + + _INFO("before account_update_to_db_by_id"); + return_code = _account_update_to_db_by_user_name(pid, account, user_name, package_name); + _INFO("after account_update_to_db_by_id=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_update_to_db_by_id error"); + goto RETURN; + } + +RETURN: + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_update_to_db_by_id(object, invocation); + } + _INFO("account_manager_handle_account_update_to_db_by_user_name end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean +account_manager_handle_account_type_query_label_by_locale(AccountManager *object, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar *app_id, + const gchar *locale, gchar *cookie) +{ + _INFO("account_manager_handle_account_type_query_label_by_locale start"); + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_type_query_label_by_locale"); + char *label_name = NULL; + return_code = _account_type_query_label_by_locale(app_id, locale, &label_name); + _INFO("after _account_type_query_label_by_locale=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_query_label_by_locale error"); + goto RETURN; + } + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_type_query_label_by_locale(object, invocation, label_name); + } + _INFO("account_manager_handle_account_type_query_label_by_locale end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean +account_manager_handle_account_type_query_by_provider_feature(AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, const gchar *key, gchar *cookie) +{ + _INFO("account_manager_handle_account_type_query_by_provider_feature start"); + GVariant* account_type_list_variant = NULL; + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + //Mode checking not required, since default mode is read. + + GSList* account_type_list = NULL; + account_type_list = _account_type_query_by_provider_feature(key, &return_code); + if (return_code != 0) + { + _ERR("_account_type_query_by_provider_feature=[%d]", return_code); + goto RETURN; + } + + if (account_type_list == NULL) + { + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + _ERR("No account type found."); + goto RETURN; + } + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_query_by_provider_feature error"); + goto RETURN; + } + + _INFO("account_type_list length= [%d]", g_slist_length(account_type_list)); + + _INFO("before calling marshal_account_type_list"); + account_type_list_variant = marshal_account_type_list(account_type_list); + _INFO("after calling marshal_account_type_list"); + +RETURN: + + if (account_type_list_variant == NULL) + { + GError* error = g_error_new (_account_error_quark(), ACCOUNT_ERROR_RECORD_NOT_FOUND, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_type_query_by_provider_feature(obj, invocation, account_type_list_variant); + } + _INFO("account_manager_handle_account_type_query_by_provider_feature end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_account_get_total_count_from_db(AccountManager *object, GDBusMethodInvocation *invocation, gchar *account_db_path, gboolean include_hidden, gchar *cookie) +{ + _INFO("account_manager_account_get_total_count_from_db start"); + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + + _INFO("before account_get_total_count_from_db"); + int count = -1; + return_code = _account_get_total_count_from_db(include_hidden, &count); + _INFO("before account_get_total_count_from_db=[%d], return_code"); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_get_total_count_from_db error"); + goto RETURN; + } + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_get_total_count_from_db(object, invocation, count); + } + _INFO("account_manager_account_get_total_count_from_db end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_query_account_by_account_id(AccountManager *object, GDBusMethodInvocation *invocation, + gchar *account_db_path, gint account_db_id, gchar *cookie) +{ + _INFO("account_manager_handle_account_query_account_by_account_id start"); + GVariant* account_variant = NULL; + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + account_s* account_data = create_empty_account_instance(); + if (account_data == NULL) + { + _ERR("out of memory"); + return_code = ACCOUNT_ERROR_DB_FAILED; + goto RETURN; + } + _INFO("before _account_query_account_by_account_id"); + return_code = _account_query_account_by_account_id(pid, account_db_id, account_data); + _INFO("after _account_query_account_by_return_code=[%d]", return_code); + _INFO("user_name = %s, user_data_txt[0] = %s, user_data_int[1] = %d", account_data->user_name, account_data->user_data_txt[0], account_data->user_data_int[1]); + + if (return_code == ACCOUNT_ERROR_NONE) + { + account_variant = marshal_account(account_data); + } + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_query_label_by_locale error"); + goto RETURN; + } + +RETURN: + if (account_variant == NULL || return_code != ACCOUNT_ERROR_NONE) + { + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_query_account_by_account_id(object, invocation, account_variant); + } + _INFO("account_manager_handle_account_query_account_by_account_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean +account_manager_handle_account_query_account_by_user_name(AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, const gchar *user_name, gchar *cookie) +{ + _INFO("account_manager_handle_account_query_account_by_user_name start"); + + GVariant* account_list_variant = NULL; + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + //Mode checking not required, since default mode is read. + + GList* account_list = NULL; + + account_list = _account_query_account_by_user_name(pid, user_name, &return_code); + + if (account_list == NULL) + { + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + _ERR("No account found."); + goto RETURN; + } + + _INFO("account_list length= [%d]", g_list_length(account_list)); + + _INFO("before calling marshal_account_list_double"); + account_list_variant = marshal_account_list_double(account_list); + _INFO("after calling marshal_account_list_double"); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_query_account_by_user_name error"); + goto RETURN; + } + +RETURN: + if (account_list_variant == NULL) + { + GError* error = g_error_new (_account_error_quark(), ACCOUNT_ERROR_RECORD_NOT_FOUND, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_query_account_by_user_name(obj, invocation, account_list_variant); + } + _INFO("account_manager_handle_account_query_account_by_user_name end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean +account_manager_handle_account_query_account_by_package_name(AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, const gchar *package_name, gchar *cookie) +{ + _INFO("account_manager_handle_account_query_account_by_package_name start"); + + GVariant* account_list_variant = NULL; + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + //Mode checking not required, since default mode is read. + + GList* account_list = NULL; + + account_list = _account_query_account_by_package_name(pid, package_name, &return_code); + + if (account_list == NULL) + { + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + _ERR("No account found."); + goto RETURN; + } + + _INFO("account_list length= [%d]", g_list_length(account_list)); + + account_list_variant = marshal_account_list_double(account_list); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_query_account_by_package_name error"); + goto RETURN; + } + +RETURN: + + if (account_list_variant == NULL) + { + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + _INFO("sending error Domain[%d] Message[%s] Code[%d]", error->domain, error->message, error->code); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_query_account_by_package_name(obj, invocation, account_list_variant); + } + _INFO("account_manager_handle_account_query_account_by_package_name start"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean +account_manager_handle_account_query_account_by_capability(AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar *capability_type, + gint capability_value, gchar *cookie) +{ + _INFO("account_manager_handle_account_query_account_by_capability start"); + + GVariant* account_list_variant = NULL; + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + //Mode checking not required, since default mode is read. + + GList* account_list = NULL; + + _INFO("before _account_query_account_by_capability"); + account_list = _account_query_account_by_capability(pid, capability_type, capability_value, &return_code); + _INFO("after _account_query_account_by_capability"); + + if (account_list == NULL) + { + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + _ERR("No account found."); + goto RETURN; + } + + _INFO("account_list length= [%d]", g_list_length(account_list)); + + account_list_variant = marshal_account_list_double(account_list); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_query_account_by_capability error"); + goto RETURN; + } + +RETURN: + + if (account_list_variant == NULL) + { + GError* error = g_error_new (_account_error_quark(), ACCOUNT_ERROR_RECORD_NOT_FOUND, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_query_account_by_capability(obj, invocation, account_list_variant); + } + _INFO("account_manager_handle_account_query_account_by_capability end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean +account_manager_handle_account_query_account_by_capability_type(AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar *capability_type, gchar *cookie) +{ + _INFO("account_manager_handle_account_query_account_by_capability_type start"); + + GVariant* account_list_variant = NULL; + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + //Mode checking not required, since default mode is read. + + GList* account_list = NULL; + + account_list = _account_query_account_by_capability_type(pid, capability_type, &return_code); + + if (account_list == NULL) + { + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + _ERR("No account found."); + goto RETURN; + } + + _INFO("account_list length= [%d]", g_list_length(account_list)); + + _INFO("before calling marshal_account_list_double"); + account_list_variant = marshal_account_list_double(account_list); + _INFO("after calling marshal_account_list_double"); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_query_account_by_capability_type error"); + goto RETURN; + } + +RETURN: + + if (account_list_variant == NULL) + { + GError* error = g_error_new (_account_error_quark(), ACCOUNT_ERROR_RECORD_NOT_FOUND, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_query_account_by_capability(obj, invocation, account_list_variant); + } + _INFO("account_manager_handle_account_query_account_by_capability_type end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean +account_manager_handle_account_query_capability_by_account_id(AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const int account_id, gchar *cookie) +{ + _INFO("account_manager_handle_account_query_capability_by_account_id start"); + + GVariant* capability_list_variant = NULL; + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + //Mode checking not required, since default mode is read. + + GSList* capability_list = NULL; + + capability_list = _account_get_capability_list_by_account_id(account_id, &return_code); + + if (capability_list == NULL) + { + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + _ERR("No capability found."); + goto RETURN; + } + + _INFO("capability_list length= [%d]", g_slist_length(capability_list)); + + _INFO("before calling marshal_capability_list"); + capability_list_variant = marshal_capability_list(capability_list); + _INFO("after calling marshal_capability_list"); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_get_capability_list_by_account_id error"); + goto RETURN; + } + +RETURN: + + if (capability_list_variant == NULL) + { + GError* error = g_error_new (_account_error_quark(), ACCOUNT_ERROR_RECORD_NOT_FOUND, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_query_capability_by_account_id(obj, invocation, capability_list_variant); + } + _INFO("account_manager_handle_account_query_capability_by_account_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_update_sync_status_by_id(AccountManager *object, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const int account_db_id, + const int sync_status, gchar *cookie) +{ + _INFO("account_manager_handle_account_update_sync_status_by_id start"); + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_update_sync_status_by_id"); + return_code = _account_update_sync_status_by_id(account_db_id, sync_status); + _INFO("after _account_update_sync_status_by_id=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_update_sync_status_by_id error"); + goto RETURN; + } + +RETURN: + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_update_sync_status_by_id(object, invocation); + } + _INFO("account_manager_handle_account_update_sync_status_by_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_type_query_provider_feature_by_app_id(AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar* app_id, gchar *cookie) +{ + GSList* feature_record_list = NULL; + GVariant* feature_record_list_variant = NULL; + + _INFO("account_manager_handle_account_type_query_provider_feature_by_app_id start"); + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_type_query_provider_feature_by_app_id"); + feature_record_list = _account_type_query_provider_feature_by_app_id(app_id, &return_code); + _INFO("after account_type_query_provider_feature_by_app_id=[%d]", return_code); + + if (feature_record_list == NULL) + { + _ERR("account feature_record_list is NULL"); + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + goto RETURN; + } + + feature_record_list_variant = provider_feature_list_to_variant(feature_record_list); + _INFO("%s", g_variant_print(feature_record_list_variant, true)); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_query_provider_feature_by_app_id error"); + goto RETURN; + } + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + _INFO("Calling account_manager_complete_account_type_query_provider_feature_by_app_id"); + account_manager_complete_account_type_query_provider_feature_by_app_id(obj, invocation, feature_record_list_variant); + } + _INFO("account_manager_handle_account_type_query_provider_feature_by_app_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_type_query_supported_feature(AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar* app_id, + const gchar* capability, gchar *cookie) +{ + int is_supported = 0; + + _INFO("account_manager_handle_account_type_query_supported_feature start"); + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_type_query_supported_feature"); + is_supported = _account_type_query_supported_feature(app_id, capability, &return_code); + _INFO("after _account_type_query_supported_feature=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_query_supported_feature error"); + goto RETURN; + } + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + _INFO("Calling account_manager_complete_account_type_query_provider_feature_by_app_id"); + account_manager_complete_account_type_query_supported_feature(obj, invocation, is_supported); + } + _INFO("account_manager_handle_account_type_query_supported_feature end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_type_update_to_db_by_app_id (AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + GVariant *account_type_variant, + const gchar *app_id, gchar *cookie) +{ + _INFO("account_manager_handle_account_type_update_to_db_by_app_id start"); + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + account_type_s* account_type = umarshal_account_type(account_type_variant); + + _INFO("before _account_type_update_to_db_by_app_id"); + return_code = _account_type_update_to_db_by_app_id(account_type, app_id); + _INFO("after _account_type_update_to_db_by_app_id=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_update_to_db_by_app_id error"); + goto RETURN; + } + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + _INFO("Calling account_manager_complete_account_type_update_to_db_by_app_id"); + account_manager_complete_account_type_update_to_db_by_app_id(obj, invocation); + } + _INFO("account_manager_handle_account_type_update_to_db_by_app_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_type_delete_by_app_id (AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar *app_id, gchar *cookie) +{ + _INFO("account_manager_handle_account_type_delete_by_app_id start"); + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_type_delete_by_app_id"); + return_code = _account_type_delete_by_app_id (app_id); + _INFO("after _account_type_delete_by_app_id=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_delete_by_app_id error"); + goto RETURN; + } + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + _ERR("Calling account_manager_complete_account_type_update_to_db_by_app_id"); + account_manager_complete_account_type_delete_by_app_id (obj, invocation); + } + _INFO("account_manager_handle_account_type_delete_by_app_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_type_query_label_by_app_id (AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar *app_id, gchar *cookie) +{ + _INFO("account_manager_handle_account_type_query_label_by_app_id start"); + GSList* label_list = NULL; + GVariant* label_list_variant = NULL; + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_type_get_label_list_by_app_id"); + label_list = _account_type_get_label_list_by_app_id (app_id, &return_code); + _INFO("after _account_type_get_label_list_by_app_id=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_get_label_list_by_app_id = [%d]", return_code); + goto RETURN; + } + + label_list_variant = label_list_to_variant (label_list); + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + _ERR("Calling account_manager_complete_account_type_query_label_by_app_id"); + account_manager_complete_account_type_query_label_by_app_id (obj, invocation, label_list_variant); + } + _INFO("account_manager_handle_account_type_query_label_by_app_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_type_query_by_app_id (AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar *app_id, gchar *cookie) +{ + _INFO("account_manager_handle_account_type_query_by_app_id start"); + GVariant* account_type_variant = NULL; + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_type_query_by_app_id"); + account_type_s* account_type = NULL; + return_code = _account_type_query_by_app_id (app_id, &account_type); + _INFO("after _account_type_query_by_app_id=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_query_by_app_id = [%d]", return_code); + goto RETURN; + } + if (account_type == NULL) + { + _ERR("account_type read is NULL"); + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + goto RETURN; + } + + account_type_variant = marshal_account_type( account_type); + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + _INFO("Calling account_manager_complete_account_type_query_by_app_id"); + account_manager_complete_account_type_query_by_app_id (obj, invocation, account_type_variant); + } + _INFO("account_manager_handle_account_type_query_by_app_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_type_query_app_id_exist (AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, const gchar *app_id, gchar *cookie) +{ + _INFO("account_manager_handle_account_type_query_app_id_exist start"); + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_type_query_app_id_exist"); + return_code = _account_type_query_app_id_exist (app_id); + _INFO("after _account_type_query_app_id_exist=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_query_app_id_exist = [%d]", return_code); + goto RETURN; + } + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + _INFO("Calling account_manager_complete_account_type_query_by_app_id"); + account_manager_complete_account_type_query_app_id_exist (obj, invocation); + } + _INFO("account_manager_handle_account_type_query_app_id_exist end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_update_to_db_by_id_ex (AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + GVariant *account_data, + gint account_id, gchar *cookie) +{ + _INFO("account_manager_handle_account_update_to_db_by_id_ex start"); + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + account_s* account = umarshal_account(account_data); + if (account == NULL) + { + _ERR("Unmarshal failed"); + return_code = ACCOUNT_ERROR_INVALID_PARAMETER; + goto RETURN; + } + + _INFO("before _account_update_to_db_by_id_ex"); + return_code = _account_update_to_db_by_id_ex (account, account_id); + _INFO("after _account_update_to_db_by_id_ex()=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_update_to_db_by_id_ex error"); + goto RETURN; + } + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + _INFO("Calling account_manager_complete_account_update_to_db_by_id_ex"); + account_manager_complete_account_update_to_db_by_id_ex (obj, invocation); + } + _INFO("in account_manager_handle_account_update_to_db_by_id_ex_p end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +static void +on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data) +{ + _INFO("on_bus_acquired [%s]", name); + + GDBusInterfaceSkeleton* interface = NULL; + account_mgr_server_obj = account_manager_skeleton_new(); + if (account_mgr_server_obj == NULL) + { + _ERR("account_mgr_server_obj NULL!!"); + return; + } + + interface = G_DBUS_INTERFACE_SKELETON(account_mgr_server_obj); + if (!g_dbus_interface_skeleton_export(interface, connection, ACCOUNT_MGR_DBUS_PATH, NULL)) + { + _ERR("export failed!!"); + return; + } + + _INFO("connecting account signals start"); + + g_signal_connect(account_mgr_server_obj, "handle_account_add", + G_CALLBACK(account_manager_account_add), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_query_all", + G_CALLBACK(account_manager_account_query_all), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_type_add", + G_CALLBACK(account_manager_account_type_add), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_type_query_all", + G_CALLBACK(account_manager_account_type_query_all), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_delete_from_db_by_id", + G_CALLBACK(account_manager_account_delete_from_db_by_id), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_delete_from_db_by_user_name", + G_CALLBACK(account_manager_account_delete_from_db_by_user_name), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_delete_from_db_by_package_name", + G_CALLBACK(account_manager_account_delete_from_db_by_package_name), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_update_to_db_by_id", + G_CALLBACK(account_manager_account_update_to_db_by_id), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_get_total_count_from_db", + G_CALLBACK(account_manager_account_get_total_count_from_db), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_query_account_by_account_id", + G_CALLBACK(account_manager_handle_account_query_account_by_account_id), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_update_to_db_by_user_name", + G_CALLBACK(account_manager_handle_account_update_to_db_by_user_name), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_type_query_label_by_locale", + G_CALLBACK(account_manager_handle_account_type_query_label_by_locale), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_type_query_by_provider_feature", + G_CALLBACK(account_manager_handle_account_type_query_by_provider_feature), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_query_account_by_user_name", + G_CALLBACK(account_manager_handle_account_query_account_by_user_name), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_query_account_by_package_name", + G_CALLBACK(account_manager_handle_account_query_account_by_package_name), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_query_account_by_capability", + G_CALLBACK(account_manager_handle_account_query_account_by_capability), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_query_account_by_capability_type", + G_CALLBACK(account_manager_handle_account_query_account_by_capability_type), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_query_capability_by_account_id", + G_CALLBACK(account_manager_handle_account_query_capability_by_account_id), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_update_sync_status_by_id", + G_CALLBACK(account_manager_handle_account_update_sync_status_by_id), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_type_query_provider_feature_by_app_id", + G_CALLBACK(account_manager_handle_account_type_query_provider_feature_by_app_id), NULL); + + + g_signal_connect(account_mgr_server_obj, "handle_account_type_query_supported_feature", + G_CALLBACK(account_manager_handle_account_type_query_supported_feature), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_type_update_to_db_by_app_id", + G_CALLBACK(account_manager_handle_account_type_update_to_db_by_app_id), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_type_delete_by_app_id", + G_CALLBACK(account_manager_handle_account_type_delete_by_app_id), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_type_query_label_by_app_id", + G_CALLBACK(account_manager_handle_account_type_query_label_by_app_id), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_type_query_by_app_id", + G_CALLBACK(account_manager_handle_account_type_query_by_app_id), NULL); + + + g_signal_connect(account_mgr_server_obj, "handle_account_type_query_app_id_exist", + G_CALLBACK(account_manager_handle_account_type_query_app_id_exist), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_update_to_db_by_id_ex", + G_CALLBACK(account_manager_handle_account_update_to_db_by_id_ex), NULL); + + _INFO("connecting account signals end"); + + g_dbus_object_manager_server_set_connection(account_mgr_server_mgr, connection); +} + +static void +on_name_acquired (GDBusConnection *connection, + const gchar *name, + gpointer user_data) +{ + _INFO("on_name_acquired"); + +} + +static void +on_name_lost (GDBusConnection *connection, + const gchar *name, + gpointer user_data) +{ + _INFO("on_name_lost"); + exit (1); +} + +static bool _initialize_dbus() +{ + _INFO("__initialize_dbus Enter"); + + owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM, + "org.tizen.account.manager", + G_BUS_NAME_OWNER_FLAGS_NONE, + on_bus_acquired, + on_name_acquired, + on_name_lost, + NULL, + NULL); + + _INFO("owner_id=[%d]", owner_id); + + if(owner_id == 0) + { + _INFO("gdbus own failed!!"); + return false; + } + + _INFO("g_bus_own_name SUCCESS"); + return true; +} + +static void _initialize() +{ +#if !GLIB_CHECK_VERSION(2,35,0) + g_type_init(); +#endif + + if (_initialize_dbus() == false) + { /* because dbus's initialize + failed, we cannot continue any more. */ + _ERR("DBUS Initialization Failed"); + exit(1); + } +} + +int main() +{ + GMainLoop *mainloop = NULL; + + _INFO("Starting Accounts SVC"); + + mainloop = g_main_loop_new(NULL, FALSE); + + _initialize(); + + g_main_loop_run(mainloop); + + _INFO("Ending Accounts SVC"); + return 0; +} diff --git a/src/accounts/server/account-svcd.sh b/src/accounts/server/account-svcd.sh new file mode 100755 index 0000000..a1b3be2 --- /dev/null +++ b/src/accounts/server/account-svcd.sh @@ -0,0 +1 @@ +/usr/bin/account-svcd & -- 2.7.4 From 0bf51d43e6e94528e75fcadd1025c603a55f75c8 Mon Sep 17 00:00:00 2001 From: "jiseob.jang" Date: Wed, 8 Apr 2015 15:11:24 +0900 Subject: [PATCH 03/11] Applied cynara in account's Daemon(remove security-server). Change-Id: I0f6875a1f952ec41055254c9afd2cbef9bf5f227 Signed-off-by: jiseob.jang --- packaging/account-manager.spec | 40 ++-- src/accounts/account_mgr.xml | 29 +-- src/accounts/server/CMakeLists.txt | 16 +- src/accounts/server/account-server.c | 380 +++++++++++++++++++++++++---------- 4 files changed, 305 insertions(+), 160 deletions(-) diff --git a/packaging/account-manager.spec b/packaging/account-manager.spec index 989b243..fea9e3a 100755 --- a/packaging/account-manager.spec +++ b/packaging/account-manager.spec @@ -9,20 +9,19 @@ Source0: account-manager-%{version}.tar.gz Source1: accounts-service.service BuildRequires: cmake -BuildRequires: pkgconfig(sqlite3) BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(db-util) -BuildRequires: pkgconfig(dbus-1) BuildRequires: pkgconfig(capi-base-common) BuildRequires: pkgconfig(pkgmgr-info) BuildRequires: pkgconfig(aul) BuildRequires: pkgconfig(glib-2.0) >= 2.26 BuildRequires: pkgconfig(gio-2.0) BuildRequires: pkgconfig(gio-unix-2.0) -BuildRequires: pkgconfig(security-server) +BuildRequires: pkgconfig(cynara-client) +BuildRequires: pkgconfig(cynara-session) +BuildRequires: pkgconfig(cynara-creds-gdbus) BuildRequires: pkgconfig(accounts-svc) -BuildRequires: dbus-python BuildRequires: python-xml BuildRequires: python-devel @@ -69,14 +68,13 @@ rm -rf %{buildroot}/usr/lib/account-manager %post /sbin/ldconfig -if [ ! -d /opt/usr/dbspace ] +#if [ ! -d /opt/usr/dbspace ] +#then +# mkdir -p /opt/usr/dbspace +#fi +if [ ! -f %{TZ_SYS_DB}/.account.db ] then - mkdir -p /opt/usr/dbspace -fi -if [ ! -f /opt/usr/dbspace/.account.db ] -#rm -rf /opt/usr/dbspace/.account.db* -then - sqlite3 /opt/usr/dbspace/.account.db 'PRAGMA journal_mode = PERSIST; + sqlite3 %{TZ_SYS_DB}/.account.db 'PRAGMA journal_mode = PERSIST; CREATE TABLE if not exists label (AppId TEXT, Label TEXT, Locale TEXT); CREATE TABLE if not exists account_type (_id INTEGER PRIMARY KEY AUTOINCREMENT, AppId TEXT, ServiceProviderId TEXT, IconPath TEXT, SmallIconPath TEXT, MultipleAccountSupport INT); @@ -92,8 +90,8 @@ then fi mkdir -p /opt/usr/share/account -chown system:system /opt/usr/dbspace/.account.db -chown system:system /opt/usr/dbspace/.account.db-journal +chown root:root %{TZ_SYS_DB}/.account.db +chown root:root %{TZ_SYS_DB}/.account.db-journal chmod 600 /opt/usr/dbspace/.account.db chmod 600 /opt/usr/dbspace/.account.db-journal @@ -116,13 +114,13 @@ fi %files %manifest libaccounts-svc.manifest -%defattr(-,system,system,-) -%attr(0700,system,system) %{_bindir}/account-svcd -%attr(0600,system,system) %{_libdir}/systemd/system/accounts-service.service -%attr(0600,system,system) %{_libdir}/systemd/system/multi-user.target.wants/accounts-service.service +%defattr(-,root,root,-) +%attr(0755,root,root) %{_bindir}/account-svcd +%attr(-,root,root) %{_libdir}/systemd/system/accounts-service.service +%attr(-,root,root) %{_libdir}/systemd/system/multi-user.target.wants/accounts-service.service %files devel -%defattr(-,system,system,-) -%attr(0700,system,system) %{_bindir}/account-svcd -%attr(0600,system,system) %{_libdir}/systemd/system/accounts-service.service -%attr(0600,system,system) %{_libdir}/systemd/system/multi-user.target.wants/accounts-service.service +%defattr(-,root,root,-) +%attr(0755,root,root) %{_bindir}/account-svcd +%attr(-,root,root) %{_libdir}/systemd/system/accounts-service.service +%attr(-,root,root) %{_libdir}/systemd/system/multi-user.target.wants/accounts-service.service diff --git a/src/accounts/account_mgr.xml b/src/accounts/account_mgr.xml index 32cf6bf..a8a95d5 100644 --- a/src/accounts/account_mgr.xml +++ b/src/accounts/account_mgr.xml @@ -4,58 +4,48 @@ - - - - - - - - + - - - @@ -63,104 +53,87 @@ - - - - - - - - - - - - - - - - - diff --git a/src/accounts/server/CMakeLists.txt b/src/accounts/server/CMakeLists.txt index 2272a1f..c4aae52 100644 --- a/src/accounts/server/CMakeLists.txt +++ b/src/accounts/server/CMakeLists.txt @@ -1,7 +1,21 @@ SET(DAEMON account-svcd) INCLUDE(FindPkgConfig) -pkg_check_modules(pkgs REQUIRED python sqlite3 dlog db-util glib-2.0 gobject-2.0 capi-base-common pkgmgr-info aul dbus-1 gio-2.0 gio-unix-2.0 security-server accounts-svc) +pkg_check_modules(pkgs REQUIRED + dlog + db-util + glib-2.0 + capi-base-common + pkgmgr-info + aul + dbus-1 + gio-2.0 + gio-unix-2.0 + cynara-client + cynara-session + cynara-creds-gdbus + accounts-svc +) FOREACH(flag ${pkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") diff --git a/src/accounts/server/account-server.c b/src/accounts/server/account-server.c index f582772..cf2d0e0 100644 --- a/src/accounts/server/account-server.c +++ b/src/accounts/server/account-server.c @@ -19,24 +19,17 @@ #define _DEFAULT_SOURCE #include #include -#include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include #include #if !GLIB_CHECK_VERSION (2, 31, 0) #include #endif -#include +#include +#include +#include #include + #include "dbg.h" #include "account-server-db.h" #include "account_ipc_marshal.h" @@ -44,16 +37,16 @@ #include "account-private.h" #include "account-error.h" -#define _CHECK_READ_LABEL "libaccounts-svc::check_read" -#define _DB_LABEL "libaccounts-svc::db" -#define _READ_LABEL "r" -#define _WRITE_LABEL "w" -#define _READ_WRITE_LABEL "rw" +#define _PRIVILEGE_ACCOUNT_READ "http://tizen.org/privilege/account.read" +#define _PRIVILEGE_ACCOUNT_WRITE "http://tizen.org/privilege/account.write" #define ACCOUNT_MGR_DBUS_PATH "/org/tizen/account/manager" static guint owner_id = 0; GDBusObjectManagerServer *account_mgr_server_mgr = NULL; static AccountManager* account_mgr_server_obj = NULL; + +static cynara *p_cynara; + //static gboolean has_owner = FALSE; // pid-mode, TODO: make it sessionId-mode, were session id is mix of pid and some rand no, so that @@ -137,56 +130,117 @@ _account_error_quark (void) return (GQuark) quark_volatile; } -static int _check_privilege_by_cookie(char *e_cookie, const char *label, const char *access_perm, bool check_root, int pid) { - guchar *cookie = NULL; - gsize size = 0; - int retval = 0; - char buf[128] = {0,}; - FILE *fp = NULL; - char title[128] = {0,}; - int uid = -1; - - if (check_root) { - // Gets the userID from /proc/pid/status to check if the process is the root or not. - snprintf(buf, sizeof(buf), "/proc/%d/status", pid); - fp = fopen(buf, "r"); - if(fp) { - while (fgets(buf, sizeof(buf), fp) != NULL) { - if(strncmp(buf, "Uid:", 4) == 0) { - sscanf(buf, "%s %d", title, &uid); - break; - } - } - fclose(fp); - } +static int __check_privilege_by_cynara(const char *client, const char *session, const char *user, const char *privilege) +{ + int ret; + char err_buf[128] = {0,}; + + ret = cynara_check(p_cynara, client, session, user, privilege); + switch (ret) { + case CYNARA_API_ACCESS_ALLOWED: + _DBG("cynara_check success"); + return ACCOUNT_ERROR_NONE; + case CYNARA_API_ACCESS_DENIED: + _ERR("cynara_check permission deined, privilege=%s, error = CYNARA_API_ACCESS_DENIED", privilege); + return ACCOUNT_ERROR_PERMISSION_DENIED; + default: + cynara_strerror(ret, err_buf, sizeof(err_buf)); + _ERR("cynara_check error : %s, privilege=%s, ret = %d", err_buf, privilege, ret); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } +} + +int __get_information_for_cynara_check(GDBusMethodInvocation *invocation, char **client, char **user, char **session) +{ + GDBusConnection *gdbus_conn = NULL; + char* sender = NULL; + int ret = -1; - _INFO("uid : %d", uid); + //get GDBusConnection + gdbus_conn = g_dbus_method_invocation_get_connection(invocation); + if(gdbus_conn == NULL) + { + _ERR("g_dbus_method_invocation_get_connection failed"); + return -1; } - if (uid != 0) { // Checks the cookie only when the process is not the root - cookie = g_base64_decode(e_cookie, &size); - if (cookie == NULL) { - _ERR("Unable to decode cookie!!!"); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + //get sender(unique_name) + sender = (char*) g_dbus_method_invocation_get_sender(invocation); + if (sender == NULL) + { + _ERR("g_dbus_method_invocation_get_sender failed"); + return -1; + } - retval = security_server_check_privilege_by_cookie((const char *)cookie, label, access_perm); - g_free(cookie); + ret = cynara_creds_gdbus_get_user(gdbus_conn, sender, CLIENT_METHOD_SMACK, user); + if (ret != CYNARA_API_SUCCESS) + { + _ERR("cynara_creds_gdbus_get_user failed, ret = %d", ret); + return -1; + } - if (retval < 0) { - if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) { - _ERR("Access to account-svcd has been denied by smack."); - } - _ERR("Error has occurred in security_server_check_privilege_by_cookie() : %d.", retval); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } + ret = cynara_creds_gdbus_get_client(gdbus_conn, sender, CLIENT_METHOD_SMACK, client); + if (ret != CYNARA_API_SUCCESS) + { + _ERR("cynara_creds_gdbus_get_client failed, ret = %d", ret); + return -1; } - _INFO("The process(%d) was authenticated successfully.", pid); + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + *session = cynara_session_from_pid(pid); + if (*session == NULL) + { + _ERR("cynara_session_from_pid failed"); + return -1; + } return ACCOUNT_ERROR_NONE; } -gboolean account_manager_account_add(AccountManager *obj, GDBusMethodInvocation *invocation, gchar* account_db_path, GVariant* account_data, gchar *cookie, gpointer user_data) +int _check_privilege(GDBusMethodInvocation *invocation, const char* privilege) +{ + int ret = -1; + char *client = NULL; + char *session = NULL; + char *user = NULL; + + ret = __get_information_for_cynara_check(invocation, &client, &user, &session); + if ( ret != ACCOUNT_ERROR_NONE ) + { + _ERR("__get_information_for_cynara_check failed"); + g_free(client); + g_free(user); + _ACCOUNT_FREE(session); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ret = __check_privilege_by_cynara(client, session, user, privilege); + if ( ret != ACCOUNT_ERROR_NONE ) + { + _ERR("__check_privilege_by_cynara failed, ret = %d", ret); + g_free(client); + g_free(user); + _ACCOUNT_FREE(session); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + g_free(client); + g_free(user); + _ACCOUNT_FREE(session); + return ACCOUNT_ERROR_NONE; +} + +int _check_priviliege_account_read(GDBusMethodInvocation *invocation) +{ + return _check_privilege(invocation, _PRIVILEGE_ACCOUNT_READ); +} + +int _check_priviliege_account_write(GDBusMethodInvocation *invocation) +{ + return _check_privilege(invocation, _PRIVILEGE_ACCOUNT_WRITE); +} + +gboolean account_manager_account_add(AccountManager *obj, GDBusMethodInvocation *invocation, gchar* account_db_path, GVariant* account_data, gpointer user_data) { _INFO("account_manager_account_add start"); int db_id = -1; @@ -194,9 +248,17 @@ gboolean account_manager_account_add(AccountManager *obj, GDBusMethodInvocation guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); + goto RETURN; + } + + return_code = _check_priviliege_account_write(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_write failed, ret = %d", return_code); goto RETURN; } @@ -250,7 +312,7 @@ RETURN: return true; } -gboolean account_manager_account_query_all(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, gchar *cookie) +gboolean account_manager_account_query_all(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path) { _INFO("account_manager_account_query_all start"); @@ -259,9 +321,10 @@ gboolean account_manager_account_query_all(AccountManager *obj, GDBusMethodInvoc guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); goto RETURN; } @@ -315,7 +378,7 @@ RETURN: return true; } -gboolean account_manager_account_type_query_all(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, gchar *cookie) +gboolean account_manager_account_type_query_all(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path) { _INFO("account_manager_account_query_all start"); @@ -323,9 +386,10 @@ gboolean account_manager_account_type_query_all(AccountManager *obj, GDBusMethod guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); goto RETURN; } @@ -379,7 +443,7 @@ RETURN: return true; } -gboolean account_manager_account_type_add(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, GVariant *account_type_data, gchar *cookie, gpointer user_data) +gboolean account_manager_account_type_add(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, GVariant *account_type_data, gpointer user_data) { int db_id = -1; @@ -388,9 +452,16 @@ gboolean account_manager_account_type_add(AccountManager *obj, GDBusMethodInvoca guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); + goto RETURN; + } + return_code = _check_priviliege_account_write(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_write failed, ret = %d", return_code); goto RETURN; } @@ -446,16 +517,23 @@ RETURN: gboolean account_manager_account_delete_from_db_by_id(AccountManager *object, GDBusMethodInvocation *invocation, gchar *account_db_path, - gint account_db_id, gchar *cookie) + gint account_db_id) { _INFO("account_manager_account_delete_from_db_by_id start"); guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); + goto RETURN; + } + return_code = _check_priviliege_account_write(invocation); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_check_priviliege_account_write failed, ret = %d", return_code); goto RETURN; } @@ -505,16 +583,23 @@ gboolean account_manager_account_delete_from_db_by_user_name(AccountManager *obj GDBusMethodInvocation *invocation, gchar *account_db_path, const gchar *user_name, - const gchar *package_name, gchar *cookie) + const gchar *package_name) { _INFO("account_manager_account_delete_from_db_by_user_name start"); guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); + goto RETURN; + } + return_code = _check_priviliege_account_write(invocation); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_check_priviliege_account_write failed, ret = %d", return_code); goto RETURN; } @@ -563,16 +648,23 @@ RETURN: gboolean account_manager_account_delete_from_db_by_package_name(AccountManager *object, GDBusMethodInvocation *invocation, gchar *account_db_path, - const gchar *package_name, gboolean permission, gchar *cookie) + const gchar *package_name, gboolean permission) { _INFO("account_manager_account_delete_from_db_by_package_name start"); guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); + goto RETURN; + } + return_code = _check_priviliege_account_write(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_write failed, ret = %d", return_code); goto RETURN; } @@ -622,16 +714,23 @@ gboolean account_manager_account_update_to_db_by_id(AccountManager *object, GDBusMethodInvocation *invocation, gchar *account_db_path, GVariant *account_data, - gint account_id, gchar *cookie) + gint account_id) { _INFO("account_manager_account_update_to_db_by_id start"); guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); + goto RETURN; + } + return_code = _check_priviliege_account_write(invocation); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_check_priviliege_account_write failed, ret = %d", return_code); goto RETURN; } @@ -690,16 +789,23 @@ gboolean account_manager_handle_account_update_to_db_by_user_name(AccountManager gchar *account_db_path, GVariant *account_data, const gchar *user_name, - const gchar *package_name, gchar *cookie) + const gchar *package_name) { _INFO("account_manager_handle_account_update_to_db_by_user_name start"); guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); + goto RETURN; + } + return_code = _check_priviliege_account_write(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_write failed, ret = %d", return_code); goto RETURN; } @@ -758,16 +864,17 @@ account_manager_handle_account_type_query_label_by_locale(AccountManager *object GDBusMethodInvocation *invocation, gchar *account_db_path, const gchar *app_id, - const gchar *locale, gchar *cookie) + const gchar *locale) { _INFO("account_manager_handle_account_type_query_label_by_locale start"); guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); goto RETURN; } @@ -816,7 +923,7 @@ RETURN: gboolean account_manager_handle_account_type_query_by_provider_feature(AccountManager *obj, GDBusMethodInvocation *invocation, - gchar *account_db_path, const gchar *key, gchar *cookie) + gchar *account_db_path, const gchar *key) { _INFO("account_manager_handle_account_type_query_by_provider_feature start"); GVariant* account_type_list_variant = NULL; @@ -825,9 +932,10 @@ account_manager_handle_account_type_query_by_provider_feature(AccountManager *ob _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); goto RETURN; } @@ -891,16 +999,17 @@ RETURN: return true; } -gboolean account_manager_account_get_total_count_from_db(AccountManager *object, GDBusMethodInvocation *invocation, gchar *account_db_path, gboolean include_hidden, gchar *cookie) +gboolean account_manager_account_get_total_count_from_db(AccountManager *object, GDBusMethodInvocation *invocation, gchar *account_db_path, gboolean include_hidden) { _INFO("account_manager_account_get_total_count_from_db start"); guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); goto RETURN; } @@ -948,7 +1057,7 @@ RETURN: } gboolean account_manager_handle_account_query_account_by_account_id(AccountManager *object, GDBusMethodInvocation *invocation, - gchar *account_db_path, gint account_db_id, gchar *cookie) + gchar *account_db_path, gint account_db_id) { _INFO("account_manager_handle_account_query_account_by_account_id start"); GVariant* account_variant = NULL; @@ -957,9 +1066,10 @@ gboolean account_manager_handle_account_query_account_by_account_id(AccountManag _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); goto RETURN; } @@ -1019,7 +1129,7 @@ RETURN: gboolean account_manager_handle_account_query_account_by_user_name(AccountManager *obj, GDBusMethodInvocation *invocation, - gchar *account_db_path, const gchar *user_name, gchar *cookie) + gchar *account_db_path, const gchar *user_name) { _INFO("account_manager_handle_account_query_account_by_user_name start"); @@ -1028,9 +1138,10 @@ account_manager_handle_account_query_account_by_user_name(AccountManager *obj, _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); goto RETURN; } @@ -1092,7 +1203,7 @@ RETURN: gboolean account_manager_handle_account_query_account_by_package_name(AccountManager *obj, GDBusMethodInvocation *invocation, - gchar *account_db_path, const gchar *package_name, gchar *cookie) + gchar *account_db_path, const gchar *package_name) { _INFO("account_manager_handle_account_query_account_by_package_name start"); @@ -1101,9 +1212,10 @@ account_manager_handle_account_query_account_by_package_name(AccountManager *obj _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); goto RETURN; } @@ -1167,7 +1279,7 @@ account_manager_handle_account_query_account_by_capability(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, const gchar *capability_type, - gint capability_value, gchar *cookie) + gint capability_value) { _INFO("account_manager_handle_account_query_account_by_capability start"); @@ -1177,9 +1289,10 @@ account_manager_handle_account_query_account_by_capability(AccountManager *obj, _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); goto RETURN; } @@ -1243,7 +1356,7 @@ gboolean account_manager_handle_account_query_account_by_capability_type(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, - const gchar *capability_type, gchar *cookie) + const gchar *capability_type) { _INFO("account_manager_handle_account_query_account_by_capability_type start"); @@ -1253,9 +1366,10 @@ account_manager_handle_account_query_account_by_capability_type(AccountManager * _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); goto RETURN; } @@ -1319,7 +1433,7 @@ gboolean account_manager_handle_account_query_capability_by_account_id(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, - const int account_id, gchar *cookie) + const int account_id) { _INFO("account_manager_handle_account_query_capability_by_account_id start"); @@ -1329,12 +1443,14 @@ account_manager_handle_account_query_capability_by_account_id(AccountManager *ob _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); goto RETURN; } + return_code = _account_db_open(0, (const char*)account_db_path); if (return_code != ACCOUNT_ERROR_NONE) { @@ -1395,16 +1511,23 @@ gboolean account_manager_handle_account_update_sync_status_by_id(AccountManager GDBusMethodInvocation *invocation, gchar *account_db_path, const int account_db_id, - const int sync_status, gchar *cookie) + const int sync_status) { _INFO("account_manager_handle_account_update_sync_status_by_id start"); guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); + goto RETURN; + } + return_code = _check_priviliege_account_write(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_write failed, ret = %d", return_code); goto RETURN; } @@ -1453,7 +1576,7 @@ RETURN: gboolean account_manager_handle_account_type_query_provider_feature_by_app_id(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, - const gchar* app_id, gchar *cookie) + const gchar* app_id) { GSList* feature_record_list = NULL; GVariant* feature_record_list_variant = NULL; @@ -1464,9 +1587,10 @@ gboolean account_manager_handle_account_type_query_provider_feature_by_app_id(Ac _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); goto RETURN; } @@ -1525,7 +1649,7 @@ gboolean account_manager_handle_account_type_query_supported_feature(AccountMana GDBusMethodInvocation *invocation, gchar *account_db_path, const gchar* app_id, - const gchar* capability, gchar *cookie) + const gchar* capability) { int is_supported = 0; @@ -1534,9 +1658,10 @@ gboolean account_manager_handle_account_type_query_supported_feature(AccountMana _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); goto RETURN; } @@ -1585,7 +1710,7 @@ gboolean account_manager_handle_account_type_update_to_db_by_app_id (AccountMana GDBusMethodInvocation *invocation, gchar *account_db_path, GVariant *account_type_variant, - const gchar *app_id, gchar *cookie) + const gchar *app_id) { _INFO("account_manager_handle_account_type_update_to_db_by_app_id start"); @@ -1593,9 +1718,16 @@ gboolean account_manager_handle_account_type_update_to_db_by_app_id (AccountMana _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); + goto RETURN; + } + return_code = _check_priviliege_account_write(invocation); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_check_priviliege_account_write failed, ret = %d", return_code); goto RETURN; } @@ -1645,7 +1777,7 @@ RETURN: gboolean account_manager_handle_account_type_delete_by_app_id (AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, - const gchar *app_id, gchar *cookie) + const gchar *app_id) { _INFO("account_manager_handle_account_type_delete_by_app_id start"); @@ -1653,9 +1785,16 @@ gboolean account_manager_handle_account_type_delete_by_app_id (AccountManager *o _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); + goto RETURN; + } + return_code = _check_priviliege_account_write(invocation); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_check_priviliege_account_write failed, ret = %d", return_code); goto RETURN; } @@ -1703,7 +1842,7 @@ RETURN: gboolean account_manager_handle_account_type_query_label_by_app_id (AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, - const gchar *app_id, gchar *cookie) + const gchar *app_id) { _INFO("account_manager_handle_account_type_query_label_by_app_id start"); GSList* label_list = NULL; @@ -1713,9 +1852,10 @@ gboolean account_manager_handle_account_type_query_label_by_app_id (AccountManag _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); goto RETURN; } @@ -1765,7 +1905,7 @@ RETURN: gboolean account_manager_handle_account_type_query_by_app_id (AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, - const gchar *app_id, gchar *cookie) + const gchar *app_id) { _INFO("account_manager_handle_account_type_query_by_app_id start"); GVariant* account_type_variant = NULL; @@ -1774,9 +1914,10 @@ gboolean account_manager_handle_account_type_query_by_app_id (AccountManager *ob _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); goto RETURN; } @@ -1833,16 +1974,17 @@ RETURN: gboolean account_manager_handle_account_type_query_app_id_exist (AccountManager *obj, GDBusMethodInvocation *invocation, - gchar *account_db_path, const gchar *app_id, gchar *cookie) + gchar *account_db_path, const gchar *app_id) { _INFO("account_manager_handle_account_type_query_app_id_exist start"); guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); goto RETURN; } @@ -1892,16 +2034,23 @@ gboolean account_manager_handle_account_update_to_db_by_id_ex (AccountManager *o GDBusMethodInvocation *invocation, gchar *account_db_path, GVariant *account_data, - gint account_id, gchar *cookie) + gint account_id) { _INFO("account_manager_handle_account_update_to_db_by_id_ex start"); guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); - int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + int return_code = _check_priviliege_account_read(invocation); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); + goto RETURN; + } + return_code = _check_priviliege_account_write(invocation); if (return_code != ACCOUNT_ERROR_NONE) { + _ERR("_check_priviliege_account_write failed, ret = %d", return_code); goto RETURN; } @@ -2101,18 +2250,21 @@ static bool _initialize_dbus() if(owner_id == 0) { _INFO("gdbus own failed!!"); - return false; + return false; } _INFO("g_bus_own_name SUCCESS"); return true; } + + static void _initialize() { #if !GLIB_CHECK_VERSION(2,35,0) g_type_init(); #endif + int ret = -1; if (_initialize_dbus() == false) { /* because dbus's initialize @@ -2120,6 +2272,12 @@ static void _initialize() _ERR("DBUS Initialization Failed"); exit(1); } + + ret = cynara_initialize(&p_cynara, NULL); + if(ret != CYNARA_API_SUCCESS) { + _ERR("CYNARA Initialization fail"); + exit(1); + } } int main() @@ -2134,6 +2292,8 @@ int main() g_main_loop_run(mainloop); + cynara_finish(p_cynara); + _INFO("Ending Accounts SVC"); return 0; } -- 2.7.4 From ca65e32c15c1b835736534bc8ca40536551ef7d8 Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Tue, 14 Apr 2015 20:15:43 +0200 Subject: [PATCH 04/11] Fix application of Tizen 3.0 security model Adjust Smack manifest file to ThreeDomainsModel. Correct DAC and Smack settings for databases in spec file. Use proper folders in spec file. Correct service file (typo + remove explicit Smack labeling). Use default methods of getting client and user credentials for use in cynara_check(). Change-Id: I1a2dd9423d41a2b4133a5c9e3a2666ba4aefba33 --- libaccounts-svc.manifest | 26 +------------------------- packaging/account-manager.spec | 24 ++++++++++-------------- packaging/accounts-service.service | 6 ++---- src/accounts/server/account-server.c | 4 ++-- 4 files changed, 15 insertions(+), 45 deletions(-) diff --git a/libaccounts-svc.manifest b/libaccounts-svc.manifest index 7232dc1..97e8c31 100755 --- a/libaccounts-svc.manifest +++ b/libaccounts-svc.manifest @@ -1,29 +1,5 @@ - - - - - - - - - - - - - - - - - + - - - - - - diff --git a/packaging/account-manager.spec b/packaging/account-manager.spec index fea9e3a..587ab5a 100755 --- a/packaging/account-manager.spec +++ b/packaging/account-manager.spec @@ -89,23 +89,19 @@ then ' fi -mkdir -p /opt/usr/share/account -chown root:root %{TZ_SYS_DB}/.account.db -chown root:root %{TZ_SYS_DB}/.account.db-journal +chown system:system %{TZ_SYS_DB}/.account.db +chown system:system %{TZ_SYS_DB}/.account.db-journal -chmod 600 /opt/usr/dbspace/.account.db -chmod 600 /opt/usr/dbspace/.account.db-journal +chmod 600 %{TZ_SYS_DB}/.account.db +chmod 600 %{TZ_SYS_DB}/.account.db-journal #set message key value to NULL #vconftool set -t string db/account/msg '' -g 6514 vconftool set -tf string db/account/msg '' -s libaccounts-svc -u 200 -g 5000 #smack labeling -if [ -f /usr/lib/rpm-plugins/msm.so ] -then - chsmack -a 'libaccounts-svc::db' /opt/usr/dbspace/.account.db-journal - chsmack -a 'libaccounts-svc::db' /opt/usr/dbspace/.account.db -fi +chsmack -a 'System' %{TZ_SYS_DB}/.account.db-journal +chsmack -a 'System' %{TZ_SYS_DB}/.account.db %postun -p /sbin/ldconfig @@ -116,11 +112,11 @@ fi %manifest libaccounts-svc.manifest %defattr(-,root,root,-) %attr(0755,root,root) %{_bindir}/account-svcd -%attr(-,root,root) %{_libdir}/systemd/system/accounts-service.service -%attr(-,root,root) %{_libdir}/systemd/system/multi-user.target.wants/accounts-service.service +%attr(-,root,root) %{_unitdir}/accounts-service.service +%attr(-,root,root) %{_unitdir}/multi-user.target.wants/accounts-service.service %files devel %defattr(-,root,root,-) %attr(0755,root,root) %{_bindir}/account-svcd -%attr(-,root,root) %{_libdir}/systemd/system/accounts-service.service -%attr(-,root,root) %{_libdir}/systemd/system/multi-user.target.wants/accounts-service.service +%attr(-,root,root) %{_unitdir}/accounts-service.service +%attr(-,root,root) %{_unitdir}/multi-user.target.wants/accounts-service.service diff --git a/packaging/accounts-service.service b/packaging/accounts-service.service index 5d16913..0e2fbc8 100644 --- a/packaging/accounts-service.service +++ b/packaging/accounts-service.service @@ -1,12 +1,10 @@ [Unit] Description=Accounts service -After=tizen-runtime.target -Requires=tizen-runtime.target [Service] User=system -Gruop=system -SmackProcessLabel=libaccounts-svc +Group=system + Type=simple ExecStart=/usr/bin/account-svcd Restart=always diff --git a/src/accounts/server/account-server.c b/src/accounts/server/account-server.c index cf2d0e0..06b1602 100644 --- a/src/accounts/server/account-server.c +++ b/src/accounts/server/account-server.c @@ -172,14 +172,14 @@ int __get_information_for_cynara_check(GDBusMethodInvocation *invocation, char * return -1; } - ret = cynara_creds_gdbus_get_user(gdbus_conn, sender, CLIENT_METHOD_SMACK, user); + ret = cynara_creds_gdbus_get_user(gdbus_conn, sender, USER_METHOD_DEFAULT, user); if (ret != CYNARA_API_SUCCESS) { _ERR("cynara_creds_gdbus_get_user failed, ret = %d", ret); return -1; } - ret = cynara_creds_gdbus_get_client(gdbus_conn, sender, CLIENT_METHOD_SMACK, client); + ret = cynara_creds_gdbus_get_client(gdbus_conn, sender, CLIENT_METHOD_DEFAULT, client); if (ret != CYNARA_API_SUCCESS) { _ERR("cynara_creds_gdbus_get_client failed, ret = %d", ret); -- 2.7.4 From 39a80365358590a739b433a1cb93157816efa57f Mon Sep 17 00:00:00 2001 From: "horiho.kim" Date: Wed, 13 May 2015 14:40:30 +0900 Subject: [PATCH 05/11] change systemd unit file location %{_libdir} >> %{_unitdir} Change-Id: Ia0d3b7fd72924e851e75e4643a751de1cbf89c6c Signed-off-by: horiho.kim --- packaging/account-manager.spec | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packaging/account-manager.spec b/packaging/account-manager.spec index 587ab5a..49a54d3 100755 --- a/packaging/account-manager.spec +++ b/packaging/account-manager.spec @@ -60,9 +60,9 @@ make %{?jobs:-j%jobs} rm -rf %{buildroot} %make_install -mkdir -p %{buildroot}%{_libdir}/systemd/system/multi-user.target.wants -install -m 0644 %SOURCE1 %{buildroot}%{_libdir}/systemd/system/accounts-service.service -ln -s ../accounts-service.service %{buildroot}%{_libdir}/systemd/system/multi-user.target.wants/accounts-service.service +mkdir -p %{buildroot}%{_unitdir}/multi-user.target.wants +install -m 0644 %SOURCE1 %{buildroot}%{_unitdir}/accounts-service.service +ln -s ../accounts-service.service %{buildroot}%{_unitdir}/multi-user.target.wants/accounts-service.service rm -rf %{buildroot}/usr/lib/account-manager -- 2.7.4 From 765e1a37c96cd5e7c34f61dd6315d6aa23d32f49 Mon Sep 17 00:00:00 2001 From: Stephane Desneux Date: Tue, 2 Jun 2015 16:30:47 +0200 Subject: [PATCH 06/11] packaging: remove erroneous devel package Change-Id: Ia2d17c35b9e65929f1a63ef3dc08258eb91d9786 Signed-off-by: Stephane Desneux --- packaging/account-manager.spec | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/packaging/account-manager.spec b/packaging/account-manager.spec index 49a54d3..436cab3 100755 --- a/packaging/account-manager.spec +++ b/packaging/account-manager.spec @@ -32,13 +32,6 @@ Requires(postun): /sbin/ldconfig %description Account Daemon: no -%package devel -Summary: Development files for %{name} -Group: Development/Libraries -Requires: %{name} = %{version}-%{release} -%description devel -Development files for %{name} - %prep %setup -q @@ -115,8 +108,3 @@ chsmack -a 'System' %{TZ_SYS_DB}/.account.db %attr(-,root,root) %{_unitdir}/accounts-service.service %attr(-,root,root) %{_unitdir}/multi-user.target.wants/accounts-service.service -%files devel -%defattr(-,root,root,-) -%attr(0755,root,root) %{_bindir}/account-svcd -%attr(-,root,root) %{_unitdir}/accounts-service.service -%attr(-,root,root) %{_unitdir}/multi-user.target.wants/accounts-service.service -- 2.7.4 From 122ede72fbfd2c0cc6e3867efb6720a28d18f17b Mon Sep 17 00:00:00 2001 From: "jiseob.jang" Date: Mon, 6 Jul 2015 20:30:42 +0900 Subject: [PATCH 07/11] merge from tizen 2.4 Change-Id: I8b7adecb6a23d3aa1e358d5b37f3626d5076b41c Signed-off-by: jiseob.jang --- CMakeLists.txt | 2 +- INSTALL | 33 --- LICENSE | 0 libaccounts-svc.manifest => account-svcd.manifest | 0 packaging/account-manager.spec | 45 ++- packaging/accounts-service.service | 8 +- packaging/org.tizen.account.manager.conf | 25 ++ packaging/org.tizen.account.manager.service | 5 + {src/accounts/server => server}/CMakeLists.txt | 18 +- .../server => server/include}/account-server-db.h | 9 +- server/include/account-server-private.h | 51 ++++ .../server => server/src}/account-server-db.c | 322 +++++++-------------- server/src/account-server-private.c | 217 ++++++++++++++ .../server => server/src}/account-server.c | 166 ++++++----- src/accounts/CMakeLists.txt | 1 - src/accounts/account_mgr.xml | 139 --------- src/accounts/server/account-svcd.sh | 1 - 17 files changed, 540 insertions(+), 502 deletions(-) mode change 100755 => 100644 CMakeLists.txt delete mode 100755 INSTALL mode change 100755 => 100644 LICENSE rename libaccounts-svc.manifest => account-svcd.manifest (100%) mode change 100755 => 100644 mode change 100755 => 100644 packaging/account-manager.spec create mode 100644 packaging/org.tizen.account.manager.conf create mode 100644 packaging/org.tizen.account.manager.service rename {src/accounts/server => server}/CMakeLists.txt (60%) rename {src/accounts/server => server/include}/account-server-db.h (96%) create mode 100644 server/include/account-server-private.h rename {src/accounts/server => server/src}/account-server-db.c (96%) create mode 100644 server/src/account-server-private.c rename {src/accounts/server => server/src}/account-server.c (92%) delete mode 100755 src/accounts/CMakeLists.txt delete mode 100644 src/accounts/account_mgr.xml delete mode 100755 src/accounts/server/account-svcd.sh diff --git a/CMakeLists.txt b/CMakeLists.txt old mode 100755 new mode 100644 index 64ce5f5..6d4b164 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,4 +8,4 @@ SET(INCLUDEDIR "\${prefix}/include ") SET(VERSION_MAJOR 0) SET(VERSION "${VERSION_MAJOR}.0.1") -ADD_SUBDIRECTORY(src/accounts) +ADD_SUBDIRECTORY(server) diff --git a/INSTALL b/INSTALL deleted file mode 100755 index ddf4e78..0000000 --- a/INSTALL +++ /dev/null @@ -1,33 +0,0 @@ -1. make the build directory - - ex) - - $ mkdir build - - -2. change the working directory to the build directory - - ex) - - $ cd build - - -3. run 'cmake' - - $ cmake ${SOURCE_DIR} -DCMAKE_INSTALL_PREFIX=/usr - - ex) - - $ cmake .. -DCMAKE_INSTALL_PREFIX=/usr - - or - - $ cmake .. - - -4. make & make install - - ex) - - $ make -j 2 && make install - diff --git a/LICENSE b/LICENSE old mode 100755 new mode 100644 diff --git a/libaccounts-svc.manifest b/account-svcd.manifest old mode 100755 new mode 100644 similarity index 100% rename from libaccounts-svc.manifest rename to account-svcd.manifest diff --git a/packaging/account-manager.spec b/packaging/account-manager.spec old mode 100755 new mode 100644 index 436cab3..e80a097 --- a/packaging/account-manager.spec +++ b/packaging/account-manager.spec @@ -7,9 +7,10 @@ Group: Social & Content/Other License: Apache-2.0 Source0: account-manager-%{version}.tar.gz Source1: accounts-service.service +Source2: org.tizen.account.manager.service +Source3: org.tizen.account.manager.conf BuildRequires: cmake -BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(db-util) BuildRequires: pkgconfig(capi-base-common) @@ -17,13 +18,12 @@ BuildRequires: pkgconfig(pkgmgr-info) BuildRequires: pkgconfig(aul) BuildRequires: pkgconfig(glib-2.0) >= 2.26 BuildRequires: pkgconfig(gio-2.0) -BuildRequires: pkgconfig(gio-unix-2.0) +BuildRequires: pkgconfig(vconf) BuildRequires: pkgconfig(cynara-client) BuildRequires: pkgconfig(cynara-session) BuildRequires: pkgconfig(cynara-creds-gdbus) +BuildRequires: pkgconfig(account-common) BuildRequires: pkgconfig(accounts-svc) -BuildRequires: python-xml -BuildRequires: python-devel Requires(post): /sbin/ldconfig Requires(post): /usr/bin/sqlite3 @@ -34,6 +34,7 @@ Account Daemon: no %prep %setup -q +cp %{SOURCE2} . %build #export CFLAGS+=" -Wextra -Wcast-align -Wcast-qual -Wshadow -Wwrite-strings -Wswitch-default" @@ -44,8 +45,9 @@ Account Daemon: no #export CFLAGS+=" -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-strict-aliasing -fno-unroll-loops -fsigned-char -fstrict-overflow -fno-common" #export CXXFLAGS+=" -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-strict-aliasing -fno-unroll-loops -fsigned-char -fstrict-overflow" -export CFLAGS="${CFLAGS} -fPIC -fvisibility=hidden" -cmake . -DCMAKE_INSTALL_PREFIX=/usr +export CFLAGS="${CFLAGS} -fvisibility=hidden -fPIE" +export LDFLAGS="${LDFLAGS} -pie" +cmake . -DCMAKE_INSTALL_PREFIX=/usr -DLIBDIR=%{_libdir} -DBINDIR=%{_bindir} make %{?jobs:-j%jobs} @@ -53,12 +55,20 @@ make %{?jobs:-j%jobs} rm -rf %{buildroot} %make_install -mkdir -p %{buildroot}%{_unitdir}/multi-user.target.wants +mkdir -p %{buildroot}%{_unitdir}/default.target.wants install -m 0644 %SOURCE1 %{buildroot}%{_unitdir}/accounts-service.service -ln -s ../accounts-service.service %{buildroot}%{_unitdir}/multi-user.target.wants/accounts-service.service +ln -s ../accounts-service.service %{buildroot}%{_unitdir}/default.target.wants/accounts-service.service +#ln -s ../accounts-service.service %{buildroot}%{_unitdir}/multi-user.target.wants/accounts-service.service rm -rf %{buildroot}/usr/lib/account-manager + +mkdir -p %{buildroot}%{_unitdir}/dbus-1/system-services +install -m 0644 %SOURCE2 %{buildroot}%{_unitdir}/dbus-1/system-services/org.tizen.account.manager.service + +mkdir -p %{buildroot}%{_sysconfdir}/dbus-1/system.d +install -m 0644 %{SOURCE3} %{buildroot}%{_sysconfdir}/dbus-1/system.d/ + %post /sbin/ldconfig #if [ ! -d /opt/usr/dbspace ] @@ -72,12 +82,12 @@ then CREATE TABLE if not exists account_type (_id INTEGER PRIMARY KEY AUTOINCREMENT, AppId TEXT, ServiceProviderId TEXT, IconPath TEXT, SmallIconPath TEXT, MultipleAccountSupport INT); CREATE TABLE if not exists account_custom (AccountId INTEGER, AppId TEXT, Key TEXT, Value TEXT); - CREATE TABLE if not exists account (id INTEGER PRIMARY KEY AUTOINCREMENT, user_name TEXT, email_address TEXT, display_name TEXT, icon_path TEXT, + CREATE TABLE if not exists account (_id INTEGER PRIMARY KEY AUTOINCREMENT, user_name TEXT, email_address TEXT, display_name TEXT, icon_path TEXT, source TEXT, package_name TEXT, access_token TEXT, domain_name TEXT, auth_type INTEGER, secret INTEGER, sync_support INTEGER, txt_custom0 TEXT, txt_custom1 TEXT, txt_custom2 TEXT, txt_custom3 TEXT, txt_custom4 TEXT, int_custom0 INTEGER, int_custom1 INTEGER, int_custom2 INTEGER, int_custom3 INTEGER, int_custom4 INTEGER); CREATE TABLE if not exists capability (_id INTEGER PRIMARY KEY AUTOINCREMENT, key TEXT, value INTEGER, - package_name TEXT, user_name TEXT, account_id INTEGER, FOREIGN KEY (account_id) REFERENCES account(id)); + package_name TEXT, user_name TEXT, account_id INTEGER, FOREIGN KEY (account_id) REFERENCES account(_id)); CREATE TABLE if not exists provider_feature (app_id TEXT, key TEXT); ' fi @@ -90,11 +100,11 @@ chmod 600 %{TZ_SYS_DB}/.account.db-journal #set message key value to NULL #vconftool set -t string db/account/msg '' -g 6514 -vconftool set -tf string db/account/msg '' -s libaccounts-svc -u 200 -g 5000 +#vconftool set -tf string db/account/msg '' -s libaccounts-svc -u 200 -g 5000 #smack labeling -chsmack -a 'System' %{TZ_SYS_DB}/.account.db-journal -chsmack -a 'System' %{TZ_SYS_DB}/.account.db +#chsmack -a 'System' %{TZ_SYS_DB}/.account.db-journal +#chsmack -a 'System' %{TZ_SYS_DB}/.account.db %postun -p /sbin/ldconfig @@ -102,9 +112,12 @@ chsmack -a 'System' %{TZ_SYS_DB}/.account.db %files -%manifest libaccounts-svc.manifest -%defattr(-,root,root,-) +%manifest account-svcd.manifest +%defattr(-,system,system,-) +%config %{_sysconfdir}/dbus-1/system.d/org.tizen.account.manager.conf %attr(0755,root,root) %{_bindir}/account-svcd %attr(-,root,root) %{_unitdir}/accounts-service.service -%attr(-,root,root) %{_unitdir}/multi-user.target.wants/accounts-service.service +#%attr(-,root,root) %{_unitdir}/multi-user.target.wants/accounts-service.service +%attr(-,root,root) %{_unitdir}/default.target.wants/accounts-service.service +%attr(-,root,root) %{_unitdir}/dbus-1/system-services/org.tizen.account.manager.service diff --git a/packaging/accounts-service.service b/packaging/accounts-service.service index 0e2fbc8..26ee5e8 100644 --- a/packaging/accounts-service.service +++ b/packaging/accounts-service.service @@ -2,13 +2,13 @@ Description=Accounts service [Service] -User=system -Group=system - +#User=system +#Group=system Type=simple ExecStart=/usr/bin/account-svcd Restart=always RestartSec=1 + [Install] -WantedBy=multi-user.target +WantedBy=default.target diff --git a/packaging/org.tizen.account.manager.conf b/packaging/org.tizen.account.manager.conf new file mode 100644 index 0000000..3fb0ac4 --- /dev/null +++ b/packaging/org.tizen.account.manager.conf @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/packaging/org.tizen.account.manager.service b/packaging/org.tizen.account.manager.service new file mode 100644 index 0000000..aedd241 --- /dev/null +++ b/packaging/org.tizen.account.manager.service @@ -0,0 +1,5 @@ +[D-BUS Service] +Name=org.tizen.account.manager +Exec=/usr/bin/account-svcd +User=system +Group=system diff --git a/src/accounts/server/CMakeLists.txt b/server/CMakeLists.txt similarity index 60% rename from src/accounts/server/CMakeLists.txt rename to server/CMakeLists.txt index c4aae52..ff73019 100644 --- a/src/accounts/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -8,12 +8,12 @@ pkg_check_modules(pkgs REQUIRED capi-base-common pkgmgr-info aul - dbus-1 gio-2.0 - gio-unix-2.0 + vconf cynara-client cynara-session cynara-creds-gdbus + account-common accounts-svc ) @@ -22,21 +22,17 @@ FOREACH(flag ${pkgs_CFLAGS}) ENDFOREACH(flag) SET(SERVER_SRCS - account-server.c - account-server-db.c + src/account-server-private.c + src/account-server.c + src/account-server-db.c ) -ADD_CUSTOM_COMMAND( - WORKING_DIRECTORY - OUTPUT account-mgr-stub.c - COMMAND gdbus-codegen --interface-prefix org.tizen. - --generate-c-code account-mgr-stub ../account_mgr.xml - COMMENT "Generating Account SVC GDBus .c/.h") +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/server/include) SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall -Werror") SET(CMAKE_LDFLAGS "-Wl,-zdefs") -ADD_EXECUTABLE(${DAEMON} ${SERVER_SRCS} account-mgr-stub.c) +ADD_EXECUTABLE(${DAEMON} ${SERVER_SRCS}) TARGET_LINK_LIBRARIES(${DAEMON} ${pkgs_LDFLAGS}) diff --git a/src/accounts/server/account-server-db.h b/server/include/account-server-db.h similarity index 96% rename from src/accounts/server/account-server-db.h rename to server/include/account-server-db.h index 41b3e6c..dbfb400 100644 --- a/src/accounts/server/account-server-db.h +++ b/server/include/account-server-db.h @@ -18,15 +18,12 @@ #ifndef __ACC_SERVER_DB_H__ -#include -#include "account-private.h" - -typedef sqlite3_stmt* account_stmt; +#include //ACCOUNT_TABLE #define ACCOUNT_SCHEMA "create table account \n"\ "(\n"\ -"id INTEGER PRIMARY KEY, "\ +"_id INTEGER PRIMARY KEY, "\ "user_name TEXT, "\ "email_address TEXT, "\ "display_name TEXT, "\ @@ -99,7 +96,7 @@ typedef sqlite3_stmt* account_stmt; int _account_insert_to_db(account_s* account, int pid, int *account_id); -int _account_db_open(int mode, const char* account_db_path); +int _account_db_open(int mode, int pid); int _account_db_close(void); int _account_type_insert_to_db(account_type_s* account_type, int* account_type_id); GSList* _account_db_query_all(int pid); diff --git a/server/include/account-server-private.h b/server/include/account-server-private.h new file mode 100644 index 0000000..adef72e --- /dev/null +++ b/server/include/account-server-private.h @@ -0,0 +1,51 @@ +/* + * account + * + * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jiseob Jang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef __ACCOUNT_SERVER_PRIVATE_H__ +#define __ACCOUNT_SERVER_PRIVATE_H__ + + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include +#include + +int _account_gslist_free(GSList* list); +int _account_glist_free(GList* list); +int _account_free_capability_items(account_capability_s *data); +int _account_custom_item_free(account_custom_s *data); +int _account_custom_gslist_free(GSList* list); +int _account_free_account_items(account_s *data); +int _account_type_free_label_items(label_s *data); +int _account_type_free_feature_items(provider_feature_s *data); +int _account_type_gslist_free(GSList* list); +int _account_type_item_free(account_type_s *data); +//int _account_type_glist_free(GList* list); +int _account_type_free_account_type_items(account_type_s *data); + +#ifdef __cplusplus +} +#endif + +#endif /* __ACCOUNT_SERVER_PRIVATE_H__*/ diff --git a/src/accounts/server/account-server-db.c b/server/src/account-server-db.c similarity index 96% rename from src/accounts/server/account-server-db.c rename to server/src/account-server-db.c index 39d4ac1..42678c7 100644 --- a/src/accounts/server/account-server-db.c +++ b/server/src/account-server-db.c @@ -20,7 +20,6 @@ #include #include #include -#include #include #include #include @@ -30,10 +29,13 @@ #include #include -#include "account-private.h" +#include +#include +#include +#include +#include +#include "account-server-private.h" #include "account-server-db.h" -#include "dbg.h" -#include "account_ipc_marshal.h" typedef sqlite3_stmt* account_stmt; @@ -57,9 +59,11 @@ typedef sqlite3_stmt* account_stmt; #define ACCOUNT_DB_OPEN_READONLY 0 #define ACCOUNT_DB_OPEN_READWRITE 1 +#define MAX_TEXT 4096 + #define _TIZEN_PUBLIC_ #ifndef _TIZEN_PUBLIC_ -#include +//#include #endif @@ -68,8 +72,6 @@ static sqlite3* g_hAccountDB2 = NULL; pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER; static char *_account_get_text(const char *text_data); -static int _account_gslist_free(GSList* list); -static int _account_glist_free(GList* list); static const char *_account_query_table_column_text(account_stmt pStmt, int pos); static int _account_insert_custom(account_s *account, int account_id); static int _account_update_custom(account_s *account, int account_id); @@ -166,7 +168,7 @@ static char* _account_get_current_appid(int pid) ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid)); if(ret < 0){ - ACCOUNT_ERROR("fail to get current appid\n"); + ACCOUNT_ERROR("fail to get current appid ret=[%d], appid=%s\n", ret, appid); } _INFO(""); @@ -410,6 +412,7 @@ static int _remove_sensitive_info_from_non_owning_account(int caller_pid, accoun account->access_token = NULL; } + _ACCOUNT_FREE(caller_package_name); return ACCOUNT_ERROR_NONE; } return ACCOUNT_ERROR_INVALID_PARAMETER; @@ -417,6 +420,8 @@ static int _remove_sensitive_info_from_non_owning_account(int caller_pid, accoun static int _remove_sensitive_info_from_non_owning_account_list(int caller_pid, GList *account_list) { + int return_code = ACCOUNT_ERROR_NONE; + if (account_list == NULL) { _ERR("Null input"); @@ -427,13 +432,17 @@ static int _remove_sensitive_info_from_non_owning_account_list(int caller_pid, G for (list_iter = account_list; list_iter != NULL; list_iter = g_list_next(list_iter)) { account_s *account = (account_s *) list_iter->data; - return _remove_sensitive_info_from_non_owning_account(caller_pid, account); + int ret = _remove_sensitive_info_from_non_owning_account(caller_pid, account); + if( ret != ACCOUNT_ERROR_NONE) + return_code = ret; } - return ACCOUNT_ERROR_INVALID_PARAMETER; + return return_code; } static int _remove_sensitive_info_from_non_owning_account_slist(int caller_pid, GSList *account_list) { + int return_code = ACCOUNT_ERROR_NONE; + if (account_list == NULL) { _ERR("Null input"); @@ -444,9 +453,11 @@ static int _remove_sensitive_info_from_non_owning_account_slist(int caller_pid, for (list_iter = account_list; list_iter != NULL; list_iter = g_slist_next(list_iter)) { account_s *account = (account_s *) list_iter->data; - return _remove_sensitive_info_from_non_owning_account(caller_pid, account); + int ret = _remove_sensitive_info_from_non_owning_account(caller_pid, account); + if( ret != ACCOUNT_ERROR_NONE) + return_code = ret; } - return ACCOUNT_ERROR_INVALID_PARAMETER; + return return_code; } static const char *_account_db_err_msg() @@ -738,16 +749,19 @@ int _account_db_handle_close(sqlite3* hDB) return ret; } -int _account_db_open(int mode, const char* account_db_path) +int _account_db_open(int mode, int pid) { int rc = 0; int ret = -1; - char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + char account_db_path[256] = {0, }; - ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + _INFO( "start _account_db_open()"); + + ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path)); + ACCOUNT_SNPRINTF(account_db_path, sizeof(account_db_path), "%s", ACCOUNT_DB_PATH); if( g_hAccountDB ) { - ACCOUNT_ERROR( "Account database is using in another app. %x", g_hAccountDB ); + _ERR( "Account database is using in another app. %x", g_hAccountDB ); return ACCOUNT_ERROR_DATABASE_BUSY; } @@ -779,6 +793,7 @@ int _account_db_open(int mode, const char* account_db_path) return ACCOUNT_ERROR_DB_NOT_OPENED; } + _INFO( "end _account_db_open()"); return ACCOUNT_ERROR_NONE; } @@ -802,106 +817,6 @@ int _account_db_close(void) return ret; } -static int _account_free_capability_items(account_capability_s *data) -{ - _ACCOUNT_FREE(data->type); - _ACCOUNT_FREE(data->package_name); - _ACCOUNT_FREE(data->user_name); - - return ACCOUNT_ERROR_NONE; -} - -static int _account_custom_item_free(account_custom_s *data) -{ - _ACCOUNT_FREE(data->app_id); - _ACCOUNT_FREE(data->key); - _ACCOUNT_FREE(data->value); - - return ACCOUNT_ERROR_NONE; -} - -static int _account_custom_gslist_free(GSList* list) -{ - if(!list){ - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - GSList* iter; - - for (iter = list; iter != NULL; iter = g_slist_next(iter)) { - account_custom_s *custom_data = (account_custom_s*)iter->data; - _account_custom_item_free(custom_data); - _ACCOUNT_FREE(custom_data); - } - - g_slist_free(list); - list = NULL; - - return ACCOUNT_ERROR_NONE; -} - -static int _account_free_account_items(account_s *data) -{ - _ACCOUNT_FREE(data->user_name); - _ACCOUNT_FREE(data->email_address); - _ACCOUNT_FREE(data->display_name); - _ACCOUNT_FREE(data->icon_path); - _ACCOUNT_FREE(data->source); - _ACCOUNT_FREE(data->package_name); - _ACCOUNT_FREE(data->domain_name); - _ACCOUNT_FREE(data->access_token); - - int i; - for(i=0;iuser_data_txt[i]); - - _account_gslist_free(data->capablity_list); - _account_glist_free(data->account_list); - _account_custom_gslist_free(data->custom_list); - - return ACCOUNT_ERROR_NONE; -} - -static int _account_gslist_free(GSList* list) -{ - if(!list){ - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - GSList* iter; - - for (iter = list; iter != NULL; iter = g_slist_next(iter)) { - account_capability_s *cap_data = (account_capability_s*)iter->data; - _account_free_capability_items(cap_data); - _ACCOUNT_FREE(cap_data); - } - - g_slist_free(list); - list = NULL; - - return ACCOUNT_ERROR_NONE; -} - -static int _account_glist_free(GList* list) -{ - if(!list){ - return ACCOUNT_ERROR_INVALID_PARAMETER; - } - - GList* iter; - - for (iter = list; iter != NULL; iter = g_list_next(iter)) { - account_s *account_record = (account_s*)iter->data; - _account_free_account_items(account_record); - _ACCOUNT_FREE(account_record); - } - - g_list_free(list); - list = NULL; - - return ACCOUNT_ERROR_NONE; -} - static int _account_check_duplicated(account_s *data, const char* verified_appid) { char query[ACCOUNT_SQL_LEN_MAX] = {0, }; @@ -1219,7 +1134,7 @@ static int _account_insert_capability(account_s *account, int account_id) return ACCOUNT_ERROR_NONE; } - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where id=%d", ACCOUNT_TABLE, account_id); + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id); _INFO("_account_insert_capability _account_get_record_count [%s]", query); rc = _account_get_record_count(query); @@ -1294,7 +1209,7 @@ static int _account_update_capability(account_s *account, int account_id) return ACCOUNT_ERROR_NONE; } - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where id=%d", ACCOUNT_TABLE, account_id); + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id); rc = _account_get_record_count(query); @@ -1938,13 +1853,13 @@ int _account_insert_to_db(account_s* account, int pid, int *account_id) _INFO(""); char* verified_appid = NULL; error_code = _account_check_account_type_with_appid_group(appid, &verified_appid);//FIX + _ACCOUNT_FREE(appid); if(error_code != ACCOUNT_ERROR_NONE) { - _INFO(""); + _ERR("error_code = %d", error_code); ret_transaction = _account_end_transaction(FALSE); ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction); _ACCOUNT_FREE(verified_appid); - _ACCOUNT_FREE(appid); pthread_mutex_unlock(&account_mutex); return error_code; } @@ -2027,7 +1942,7 @@ int _account_insert_to_db(account_s* account, int pid, int *account_id) char buf[64]={0,}; ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id); _account_insert_delete_update_notification_send(buf); - _INFO("(%s)-(%d) account _notification_send."); + _INFO("account _notification_send end."); return ACCOUNT_ERROR_NONE; @@ -2320,7 +2235,7 @@ static int _account_get_package_name_from_account_id(int account_id, char **pack ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE id = %d", ACCOUNT_TABLE, account_id); + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id); hstmt = _account_prepare_query(query); rc = _account_query_step(hstmt); @@ -2409,7 +2324,7 @@ static int _account_update_account(int pid, account_s *account, int account_id) ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE id = %d ", ACCOUNT_TABLE, account_id); + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id); count = _account_get_record_count(query); if (count <= 0) { @@ -2429,7 +2344,7 @@ static int _account_update_account(int pid, account_s *account, int account_id) ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, " "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?," "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, " - "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE id=? ", ACCOUNT_TABLE); + "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE); hstmt = _account_prepare_query(query); @@ -2504,7 +2419,7 @@ static int _account_update_account_ex(account_s *account, int account_id) ACCOUNT_MEMSET(query, 0x00, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE id = %d ", ACCOUNT_TABLE, account_id); + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id); count = _account_get_record_count(query); if (count <= 0) { @@ -2524,7 +2439,7 @@ static int _account_update_account_ex(account_s *account, int account_id) ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, " "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?," "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, " - "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE id=? ", ACCOUNT_TABLE); + "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE); hstmt = _account_prepare_query(query); @@ -2746,7 +2661,7 @@ int _account_update_sync_status_by_id(int account_db_id, const int sync_status) ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where id=%d", ACCOUNT_TABLE, account_db_id); + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id); rc = _account_get_record_count(query); @@ -2764,7 +2679,7 @@ int _account_update_sync_status_by_id(int account_db_id, const int sync_status) ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE id = %d", ACCOUNT_TABLE, account_db_id); + ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id); hstmt = _account_prepare_query(query); _account_query_bind_int(hstmt, count, sync_status); @@ -2807,7 +2722,7 @@ CATCH: int _account_query_account_by_account_id(int pid, int account_db_id, account_s *account_record) { - ACCOUNT_DEBUG("account_db_id=[%d]", account_db_id); + _INFO("_account_query_account_by_account_id() start, account_db_id=[%d]", account_db_id); int error_code = ACCOUNT_ERROR_NONE; account_stmt hstmt = NULL; @@ -2822,10 +2737,12 @@ int _account_query_account_by_account_id(int pid, int account_db_id, account_s * ACCOUNT_DEBUG("starting db operations"); - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE id = %d", ACCOUNT_TABLE, account_db_id); + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id); hstmt = _account_prepare_query(query); + rc = _account_db_err_code(); + _INFO("after _account_prepare_query, rc=[%d]", rc); - if( _account_db_err_code() == SQLITE_PERM ){ + if( rc == SQLITE_PERM ){ ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); return ACCOUNT_ERROR_PERMISSION_DENIED; } @@ -2999,7 +2916,9 @@ CATCH: if (account_head) { _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list); - return account_head->account_list; + GList* result = account_head->account_list; + _ACCOUNT_FREE(account_head); + return result; } return NULL; } @@ -3024,7 +2943,7 @@ _account_query_account_by_capability(int pid, const char* capability_type, const ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE); + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE); hstmt = _account_prepare_query(query); @@ -3098,21 +3017,32 @@ _account_query_account_by_capability(int pid, const char* capability_type, const } - error_code = ACCOUNT_ERROR_NONE; + *error_code = ACCOUNT_ERROR_NONE; CATCH: if (hstmt != NULL) { rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + if ( rc != ACCOUNT_ERROR_NONE ) { + *error_code = rc; + _ERR("finalize error"); + } hstmt = NULL; } + if( *error_code != ACCOUNT_ERROR_NONE && account_head ) { + _account_glist_free(account_head->account_list); + _ACCOUNT_FREE(account_head); + account_head = NULL; + } + pthread_mutex_unlock(&account_mutex); if (account_head) { _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list); - return account_head->account_list; + GList* result = account_head->account_list; + _ACCOUNT_FREE(account_head); + return result; } return NULL; } @@ -3130,7 +3060,7 @@ GList* _account_query_account_by_capability_type(int pid, const char* capability ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE); + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE); hstmt = _account_prepare_query(query); @@ -3206,16 +3136,27 @@ CATCH: if (hstmt != NULL) { rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + if (rc != ACCOUNT_ERROR_NONE) { + *error_code = rc; + _ERR("finalize error"); + } hstmt = NULL; } + if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) { + _account_glist_free(account_head->account_list); + _ACCOUNT_FREE(account_head); + account_head = NULL; + } + pthread_mutex_unlock(&account_mutex); if (account_head) { _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list); - return account_head->account_list; + GList* result = account_head->account_list; + _ACCOUNT_FREE(account_head); + return result; } return NULL; } @@ -3309,16 +3250,28 @@ CATCH: if (hstmt != NULL) { rc = _account_query_finalize(hstmt); - ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + if (rc != ACCOUNT_ERROR_NONE) { + *error_code = rc; + _ERR("finalize error"); + } hstmt = NULL; } pthread_mutex_unlock(&account_mutex); + + if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) { + _account_glist_free(account_head->account_list); + _ACCOUNT_FREE(account_head); + account_head = NULL; + } + if ((*error_code == ACCOUNT_ERROR_NONE) && account_head != NULL) { _INFO("Returning account_list"); _remove_sensitive_info_from_non_owning_account_list(pid,account_head->account_list); - return account_head->account_list; + GList* result = account_head->account_list; + _ACCOUNT_FREE(account_head); + return result; } return NULL; } @@ -3336,7 +3289,7 @@ int _account_delete(int pid, int account_id) int count = -1; /* Check requested ID to delete */ - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE id=%d", ACCOUNT_TABLE, account_id); + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id); count = _account_get_record_count(query); @@ -3422,7 +3375,7 @@ int _account_delete(int pid, int account_id) ACCOUNT_MEMSET(query, 0, sizeof(query)); - ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE id = %d", ACCOUNT_TABLE, account_id); + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id); hstmt = _account_prepare_query(query); ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, @@ -3831,9 +3784,11 @@ int _account_delete_from_db_by_package_name(int pid, const char *package_name, g _INFO("After iter->data"); if (account != NULL) { - char* id = (char*) calloc(1, sizeof(account->id)); + char id[256] = {0, }; + + ACCOUNT_MEMSET(id, 0, 256); - sprintf(id, "%d", account->id); + ACCOUNT_SNPRINTF(id, 256, "%d", account->id); _INFO("Adding account id [%s]", id); account_id_list = g_slist_append(account_id_list, g_strdup(id)); @@ -4022,79 +3977,6 @@ int _account_get_total_count_from_db(gboolean include_hidden, int *count) return ACCOUNT_ERROR_NONE; } -static int _account_type_free_label_items(label_s *data) -{ - _ACCOUNT_FREE(data->app_id); - _ACCOUNT_FREE(data->label); - _ACCOUNT_FREE(data->locale); - - return ACCOUNT_ERROR_NONE; -} - -static int _account_type_free_feature_items(provider_feature_s *data) -{ - _ACCOUNT_FREE(data->app_id); - _ACCOUNT_FREE(data->key); - - return ACCOUNT_ERROR_NONE; -} - -static int _account_type_gslist_free(GSList* list) -{ - ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL")); - - GSList* iter; - - for (iter = list; iter != NULL; iter = g_slist_next(iter)) { - label_s *label_data = (label_s*)iter->data; - _account_type_free_label_items(label_data); - _ACCOUNT_FREE(label_data); - } - - g_slist_free(list); - list = NULL; - - return ACCOUNT_ERROR_NONE; -} - -static int _account_type_item_free(account_type_s *data) -{ - _ACCOUNT_FREE(data->app_id); - _ACCOUNT_FREE(data->service_provider_id); - _ACCOUNT_FREE(data->icon_path); - _ACCOUNT_FREE(data->small_icon_path); - - return ACCOUNT_ERROR_NONE; -} - -static int _account_type_glist_free(GList* list) -{ - ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL")); - - GList* iter; - - for (iter = list; iter != NULL; iter = g_list_next(iter)) { - account_type_s *account_type_record = (account_type_s*)iter->data; - _account_type_item_free(account_type_record); - _ACCOUNT_FREE(account_type_record); - } - - g_list_free(list); - list = NULL; - - return ACCOUNT_ERROR_NONE; -} - -static int _account_type_free_account_type_items(account_type_s *data) -{ - _account_type_item_free(data); - - _account_type_gslist_free(data->label_list); - _account_type_glist_free(data->account_type_list); - - return ACCOUNT_ERROR_NONE; -} - int account_type_create(account_type_h *account_type) { if (!account_type) { @@ -5853,7 +5735,7 @@ static int _account_insert_custom(account_s *account, int account_id) return ACCOUNT_ERROR_NONE; } - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where id=%d", ACCOUNT_TABLE, account_id); + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id); rc = _account_get_record_count(query); @@ -5929,7 +5811,7 @@ static int _account_update_custom(account_s *account, int account_id) return ACCOUNT_ERROR_NONE; } - ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where id=%d", ACCOUNT_TABLE, account_id); + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id); rc = _account_get_record_count(query); diff --git a/server/src/account-server-private.c b/server/src/account-server-private.c new file mode 100644 index 0000000..3611491 --- /dev/null +++ b/server/src/account-server-private.c @@ -0,0 +1,217 @@ +/* + * + * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include +#include +#include "account-server-private.h" + +int _account_glist_free(GList* list) +{ + if(!list){ + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + GList* iter; + + for (iter = list; iter != NULL; iter = g_list_next(iter)) { + account_s *account_record = (account_s*)iter->data; + _account_free_account_items(account_record); + _ACCOUNT_FREE(account_record); + } + + g_list_free(list); + list = NULL; + + return ACCOUNT_ERROR_NONE; +} + +int _account_gslist_free(GSList* list) +{ + if(!list){ + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + GSList* iter; + + for (iter = list; iter != NULL; iter = g_slist_next(iter)) { + account_capability_s *cap_data = (account_capability_s*)iter->data; + _account_free_capability_items(cap_data); + _ACCOUNT_FREE(cap_data); + } + + g_slist_free(list); + list = NULL; + + return ACCOUNT_ERROR_NONE; +} + +int _account_custom_gslist_free(GSList* list) +{ + if(!list){ + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + GSList* iter; + + for (iter = list; iter != NULL; iter = g_slist_next(iter)) { + account_custom_s *custom_data = (account_custom_s*)iter->data; + _account_custom_item_free(custom_data); + _ACCOUNT_FREE(custom_data); + } + + g_slist_free(list); + list = NULL; + + return ACCOUNT_ERROR_NONE; +} + +int _account_free_capability_items(account_capability_s *data) +{ + if(!data) + return ACCOUNT_ERROR_INVALID_PARAMETER; + + _ACCOUNT_FREE(data->type); + _ACCOUNT_FREE(data->package_name); + _ACCOUNT_FREE(data->user_name); + + return ACCOUNT_ERROR_NONE; +} + +int _account_custom_item_free(account_custom_s *data) +{ + if(!data) + return ACCOUNT_ERROR_INVALID_PARAMETER; + + _ACCOUNT_FREE(data->app_id); + _ACCOUNT_FREE(data->key); + _ACCOUNT_FREE(data->value); + + return ACCOUNT_ERROR_NONE; +} + +int _account_free_account_items(account_s *data) +{ + if(!data) + return ACCOUNT_ERROR_INVALID_PARAMETER; + + _ACCOUNT_FREE(data->user_name); + _ACCOUNT_FREE(data->email_address); + _ACCOUNT_FREE(data->display_name); + _ACCOUNT_FREE(data->icon_path); + _ACCOUNT_FREE(data->source); + _ACCOUNT_FREE(data->package_name); + _ACCOUNT_FREE(data->domain_name); + _ACCOUNT_FREE(data->access_token); + + int i; + for(i=0;iuser_data_txt[i]); + + _account_gslist_free(data->capablity_list); + _account_glist_free(data->account_list); + _account_custom_gslist_free(data->custom_list); + + return ACCOUNT_ERROR_NONE; +} + +int _account_type_free_label_items(label_s *data) +{ + if(!data) + return ACCOUNT_ERROR_INVALID_PARAMETER; + + _ACCOUNT_FREE(data->app_id); + _ACCOUNT_FREE(data->label); + _ACCOUNT_FREE(data->locale); + + return ACCOUNT_ERROR_NONE; +} + +int _account_type_free_feature_items(provider_feature_s *data) +{ + if(!data) + return ACCOUNT_ERROR_INVALID_PARAMETER; + + _ACCOUNT_FREE(data->app_id); + _ACCOUNT_FREE(data->key); + + return ACCOUNT_ERROR_NONE; +} + +int _account_type_gslist_free(GSList* list) +{ + ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL")); + + GSList* iter; + + for (iter = list; iter != NULL; iter = g_slist_next(iter)) { + label_s *label_data = (label_s*)iter->data; + _account_type_free_label_items(label_data); + _ACCOUNT_FREE(label_data); + } + + g_slist_free(list); + list = NULL; + + return ACCOUNT_ERROR_NONE; +} + +int _account_type_item_free(account_type_s *data) +{ + if(!data) + return ACCOUNT_ERROR_INVALID_PARAMETER; + + _ACCOUNT_FREE(data->app_id); + _ACCOUNT_FREE(data->service_provider_id); + _ACCOUNT_FREE(data->icon_path); + _ACCOUNT_FREE(data->small_icon_path); + + return ACCOUNT_ERROR_NONE; +} +/* +int _account_type_glist_free(GList* list) +{ + ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL")); + + GList* iter; + + for (iter = list; iter != NULL; iter = g_list_next(iter)) { + account_type_s *account_type_record = (account_type_s*)iter->data; + _account_type_item_free(account_type_record); + _ACCOUNT_FREE(account_type_record); + } + + g_list_free(list); + list = NULL; + + return ACCOUNT_ERROR_NONE; +} +*/ +int _account_type_free_account_type_items(account_type_s *data) +{ + if(!data) + return ACCOUNT_ERROR_INVALID_PARAMETER; + + _account_type_item_free(data); + + _account_type_gslist_free(data->label_list); +// _account_type_glist_free(data->account_type_list); + + return ACCOUNT_ERROR_NONE; +} + diff --git a/src/accounts/server/account-server.c b/server/src/account-server.c similarity index 92% rename from src/accounts/server/account-server.c rename to server/src/account-server.c index 06b1602..c9793de 100644 --- a/src/accounts/server/account-server.c +++ b/server/src/account-server.c @@ -28,15 +28,14 @@ #include #include -#include +#include +#include +#include +#include +#include -#include "dbg.h" +#include "account-server-private.h" #include "account-server-db.h" -#include "account_ipc_marshal.h" -#include "account-mgr-stub.h" -#include "account-private.h" -#include "account-error.h" - #define _PRIVILEGE_ACCOUNT_READ "http://tizen.org/privilege/account.read" #define _PRIVILEGE_ACCOUNT_WRITE "http://tizen.org/privilege/account.write" @@ -244,6 +243,7 @@ gboolean account_manager_account_add(AccountManager *obj, GDBusMethodInvocation { _INFO("account_manager_account_add start"); int db_id = -1; + account_s* account = NULL; guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); @@ -262,7 +262,7 @@ gboolean account_manager_account_add(AccountManager *obj, GDBusMethodInvocation goto RETURN; } - return_code = _account_db_open(1, (const char*)account_db_path); + return_code = _account_db_open(1, pid); if (return_code != ACCOUNT_ERROR_NONE) { _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); @@ -270,7 +270,7 @@ gboolean account_manager_account_add(AccountManager *obj, GDBusMethodInvocation goto RETURN; } - account_s* account = umarshal_account(account_data); + account = umarshal_account(account_data); if (account == NULL) { _ERR("account unmarshalling failed"); @@ -309,6 +309,8 @@ RETURN: return_code = ACCOUNT_ERROR_NONE; } + _account_free_account_items(account); + return true; } @@ -328,10 +330,10 @@ gboolean account_manager_account_query_all(AccountManager *obj, GDBusMethodInvoc goto RETURN; } - return_code = _account_db_open(0, (const char*)account_db_path); + return_code = _account_db_open(0, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -393,10 +395,10 @@ gboolean account_manager_account_type_query_all(AccountManager *obj, GDBusMethod goto RETURN; } - return_code = _account_db_open(0, (const char*)account_db_path); + return_code = _account_db_open(0, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -446,6 +448,7 @@ RETURN: gboolean account_manager_account_type_add(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, GVariant *account_type_data, gpointer user_data) { int db_id = -1; + account_type_s* account_type = NULL; _INFO("account_manager_account_type_add start"); @@ -465,15 +468,15 @@ gboolean account_manager_account_type_add(AccountManager *obj, GDBusMethodInvoca goto RETURN; } - return_code = _account_db_open(1, (const char*)account_db_path); + return_code = _account_db_open(1, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } - account_type_s* account_type = umarshal_account_type(account_type_data); + account_type = umarshal_account_type(account_type_data); if (account_type == NULL) { _ERR("account_type unmarshalling failed"); @@ -511,6 +514,7 @@ RETURN: return_code = ACCOUNT_ERROR_NONE; } + _account_type_free_account_type_items(account_type); return true; } @@ -537,10 +541,10 @@ gboolean account_manager_account_delete_from_db_by_id(AccountManager *object, goto RETURN; } - return_code = _account_db_open(1, (const char*)account_db_path); + return_code = _account_db_open(1, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -603,10 +607,10 @@ gboolean account_manager_account_delete_from_db_by_user_name(AccountManager *obj goto RETURN; } - return_code = _account_db_open(1, (const char*)account_db_path); + return_code = _account_db_open(1, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -668,10 +672,10 @@ gboolean account_manager_account_delete_from_db_by_package_name(AccountManager * goto RETURN; } - return_code = _account_db_open(1, (const char*)account_db_path); + return_code = _account_db_open(1, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -717,6 +721,7 @@ gboolean account_manager_account_update_to_db_by_id(AccountManager *object, gint account_id) { _INFO("account_manager_account_update_to_db_by_id start"); + account_s* account = NULL; guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); @@ -734,15 +739,15 @@ gboolean account_manager_account_update_to_db_by_id(AccountManager *object, goto RETURN; } - return_code = _account_db_open(1, (const char*)account_db_path); + return_code = _account_db_open(1, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } - account_s* account = umarshal_account(account_data); + account = umarshal_account(account_data); if (account == NULL) { _ERR("Unmarshal failed"); @@ -781,6 +786,8 @@ RETURN: return_code = ACCOUNT_ERROR_NONE; } + _account_free_account_items(account); + return true; } @@ -792,6 +799,7 @@ gboolean account_manager_handle_account_update_to_db_by_user_name(AccountManager const gchar *package_name) { _INFO("account_manager_handle_account_update_to_db_by_user_name start"); + account_s* account = NULL; guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); @@ -809,15 +817,15 @@ gboolean account_manager_handle_account_update_to_db_by_user_name(AccountManager goto RETURN; } - return_code = _account_db_open(1, (const char*)account_db_path); + return_code = _account_db_open(1, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } - account_s* account = umarshal_account(account_data); + account = umarshal_account(account_data); if (account == NULL) { _ERR("Unmarshal failed"); @@ -856,6 +864,8 @@ RETURN: return_code = ACCOUNT_ERROR_NONE; } + _account_free_account_items(account); + return true; } @@ -878,10 +888,10 @@ account_manager_handle_account_type_query_label_by_locale(AccountManager *object goto RETURN; } - return_code = _account_db_open(0, (const char*)account_db_path); + return_code = _account_db_open(0, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -939,10 +949,10 @@ account_manager_handle_account_type_query_by_provider_feature(AccountManager *ob goto RETURN; } - return_code = _account_db_open(0, (const char*)account_db_path); + return_code = _account_db_open(0, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -1013,10 +1023,10 @@ gboolean account_manager_account_get_total_count_from_db(AccountManager *object, goto RETURN; } - return_code = _account_db_open(0, (const char*)account_db_path); + return_code = _account_db_open(0, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -1061,6 +1071,7 @@ gboolean account_manager_handle_account_query_account_by_account_id(AccountManag { _INFO("account_manager_handle_account_query_account_by_account_id start"); GVariant* account_variant = NULL; + account_s* account_data = NULL; guint pid = _get_client_pid(invocation); @@ -1073,15 +1084,15 @@ gboolean account_manager_handle_account_query_account_by_account_id(AccountManag goto RETURN; } - return_code = _account_db_open(0, (const char*)account_db_path); + return_code = _account_db_open(0, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } - account_s* account_data = create_empty_account_instance(); + account_data = create_empty_account_instance(); if (account_data == NULL) { _ERR("out of memory"); @@ -1123,6 +1134,8 @@ RETURN: return_code = ACCOUNT_ERROR_NONE; } + _account_free_account_items(account_data); + return true; } @@ -1145,10 +1158,10 @@ account_manager_handle_account_query_account_by_user_name(AccountManager *obj, goto RETURN; } - return_code = _account_db_open(0, (const char*)account_db_path); + return_code = _account_db_open(0, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -1219,10 +1232,10 @@ account_manager_handle_account_query_account_by_package_name(AccountManager *obj goto RETURN; } - return_code = _account_db_open(0, (const char*)account_db_path); + return_code = _account_db_open(0, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -1296,10 +1309,10 @@ account_manager_handle_account_query_account_by_capability(AccountManager *obj, goto RETURN; } - return_code = _account_db_open(0, (const char*)account_db_path); + return_code = _account_db_open(0, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -1373,10 +1386,10 @@ account_manager_handle_account_query_account_by_capability_type(AccountManager * goto RETURN; } - return_code = _account_db_open(0, (const char*)account_db_path); + return_code = _account_db_open(0, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -1450,11 +1463,10 @@ account_manager_handle_account_query_capability_by_account_id(AccountManager *ob goto RETURN; } - - return_code = _account_db_open(0, (const char*)account_db_path); + return_code = _account_db_open(0, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -1531,10 +1543,10 @@ gboolean account_manager_handle_account_update_sync_status_by_id(AccountManager goto RETURN; } - return_code = _account_db_open(1, (const char*)account_db_path); + return_code = _account_db_open(1, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -1594,10 +1606,10 @@ gboolean account_manager_handle_account_type_query_provider_feature_by_app_id(Ac goto RETURN; } - return_code = _account_db_open(0, (const char*)account_db_path); + return_code = _account_db_open(0, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -1665,10 +1677,10 @@ gboolean account_manager_handle_account_type_query_supported_feature(AccountMana goto RETURN; } - return_code = _account_db_open(0, (const char*)account_db_path); + return_code = _account_db_open(0, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -1713,6 +1725,7 @@ gboolean account_manager_handle_account_type_update_to_db_by_app_id (AccountMana const gchar *app_id) { _INFO("account_manager_handle_account_type_update_to_db_by_app_id start"); + account_type_s* account_type = NULL; guint pid = _get_client_pid(invocation); @@ -1731,15 +1744,15 @@ gboolean account_manager_handle_account_type_update_to_db_by_app_id (AccountMana goto RETURN; } - return_code = _account_db_open(1, (const char*)account_db_path); + return_code = _account_db_open(1, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } - account_type_s* account_type = umarshal_account_type(account_type_variant); + account_type = umarshal_account_type(account_type_variant); _INFO("before _account_type_update_to_db_by_app_id"); return_code = _account_type_update_to_db_by_app_id(account_type, app_id); @@ -1771,6 +1784,8 @@ RETURN: return_code = ACCOUNT_ERROR_NONE; } + _account_type_free_account_type_items(account_type); + return true; } @@ -1798,10 +1813,10 @@ gboolean account_manager_handle_account_type_delete_by_app_id (AccountManager *o goto RETURN; } - return_code = _account_db_open(1, (const char*)account_db_path); + return_code = _account_db_open(1, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -1859,10 +1874,10 @@ gboolean account_manager_handle_account_type_query_label_by_app_id (AccountManag goto RETURN; } - return_code = _account_db_open(0, (const char*)account_db_path); + return_code = _account_db_open(0, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -1921,10 +1936,10 @@ gboolean account_manager_handle_account_type_query_by_app_id (AccountManager *ob goto RETURN; } - return_code = _account_db_open(0, (const char*)account_db_path); + return_code = _account_db_open(0, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -1988,10 +2003,10 @@ gboolean account_manager_handle_account_type_query_app_id_exist (AccountManager goto RETURN; } - return_code = _account_db_open(0, (const char*)account_db_path); + return_code = _account_db_open(0, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -2037,6 +2052,7 @@ gboolean account_manager_handle_account_update_to_db_by_id_ex (AccountManager *o gint account_id) { _INFO("account_manager_handle_account_update_to_db_by_id_ex start"); + account_s* account = NULL; guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); @@ -2054,15 +2070,15 @@ gboolean account_manager_handle_account_update_to_db_by_id_ex (AccountManager *o goto RETURN; } - return_code = _account_db_open(1, (const char*)account_db_path); + return_code = _account_db_open(1, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } - account_s* account = umarshal_account(account_data); + account = umarshal_account(account_data); if (account == NULL) { _ERR("Unmarshal failed"); @@ -2101,6 +2117,8 @@ RETURN: return_code = ACCOUNT_ERROR_NONE; } + _account_free_account_items(account); + return true; } @@ -2212,6 +2230,10 @@ on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_d _INFO("connecting account signals end"); g_dbus_object_manager_server_set_connection(account_mgr_server_mgr, connection); + if( connection == NULL ) { + _INFO("g_dbus_object_manager_server_set_connection failed"); + } + _INFO("on_bus_acquired end [%s]", name); } static void @@ -2257,8 +2279,6 @@ static bool _initialize_dbus() return true; } - - static void _initialize() { #if !GLIB_CHECK_VERSION(2,35,0) @@ -2288,10 +2308,16 @@ int main() mainloop = g_main_loop_new(NULL, FALSE); + _INFO("g_main_loop_new"); + _initialize(); + _INFO("_initialize"); + g_main_loop_run(mainloop); + _INFO("g_main_loop_run"); + cynara_finish(p_cynara); _INFO("Ending Accounts SVC"); diff --git a/src/accounts/CMakeLists.txt b/src/accounts/CMakeLists.txt deleted file mode 100755 index 7c2af46..0000000 --- a/src/accounts/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -ADD_SUBDIRECTORY(server) diff --git a/src/accounts/account_mgr.xml b/src/accounts/account_mgr.xml deleted file mode 100644 index a8a95d5..0000000 --- a/src/accounts/account_mgr.xml +++ /dev/null @@ -1,139 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/accounts/server/account-svcd.sh b/src/accounts/server/account-svcd.sh deleted file mode 100755 index a1b3be2..0000000 --- a/src/accounts/server/account-svcd.sh +++ /dev/null @@ -1 +0,0 @@ -/usr/bin/account-svcd & -- 2.7.4 From 6b264e2273144a6ad90c03b83db528e32c63165f Mon Sep 17 00:00:00 2001 From: "jiseob.jang" Date: Wed, 8 Jul 2015 16:31:30 +0900 Subject: [PATCH 08/11] add checking permission in account_delete_from_db_by_package_name. Change-Id: I201aca32ab5518bbab037c962eaf260018924191 Signed-off-by: jiseob.jang --- packaging/accounts-service.service | 4 ++-- server/src/account-server-db.c | 13 +++---------- server/src/account-server.c | 26 ++++++++++++++------------ 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/packaging/accounts-service.service b/packaging/accounts-service.service index 26ee5e8..fd63fe4 100644 --- a/packaging/accounts-service.service +++ b/packaging/accounts-service.service @@ -2,8 +2,8 @@ Description=Accounts service [Service] -#User=system -#Group=system +User=system +Group=system Type=simple ExecStart=/usr/bin/account-svcd Restart=always diff --git a/server/src/account-server-db.c b/server/src/account-server-db.c index 42678c7..dccc8b9 100644 --- a/server/src/account-server-db.c +++ b/server/src/account-server-db.c @@ -1832,13 +1832,11 @@ int _account_insert_to_db(account_s* account, int pid, int *account_id) return ret_transaction; } - _INFO(""); *account_id = _account_get_next_sequence(ACCOUNT_TABLE); data->id = *account_id; char* appid = NULL; appid = _account_get_current_appid(pid); - _INFO(""); if(!appid) { @@ -3746,28 +3744,23 @@ int _account_delete_from_db_by_package_name(int pid, const char *package_name, g error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp); _ACCOUNT_FREE(current_appid); - _ACCOUNT_FREE(package_name_temp); + _ACCOUNT_FREE(package_name_temp); if(error_code != ACCOUNT_ERROR_NONE){ ACCOUNT_ERROR("No permission to delete\n"); return ACCOUNT_ERROR_PERMISSION_DENIED; - } + } } // It only needs list of ids, does not need to query sensitive info. So sending 0 GList* account_list_temp = _account_query_account_by_package_name(getpid(), package_name, &ret); - if (account_list_temp == NULL) - { - _ERR("_account_query_account_by_package_name returned NULL"); - return ACCOUNT_ERROR_DB_FAILED; - } - if( _account_db_err_code() == SQLITE_PERM ){ ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); return ACCOUNT_ERROR_PERMISSION_DENIED; } if(ret != ACCOUNT_ERROR_NONE){ + _ERR("_account_query_account_by_package_name failed ret=[%d]", ret); return ret; } diff --git a/server/src/account-server.c b/server/src/account-server.c index c9793de..e1ff9e8 100644 --- a/server/src/account-server.c +++ b/server/src/account-server.c @@ -655,28 +655,30 @@ gboolean account_manager_account_delete_from_db_by_package_name(AccountManager * const gchar *package_name, gboolean permission) { _INFO("account_manager_account_delete_from_db_by_package_name start"); + int return_code = ACCOUNT_ERROR_NONE; guint pid = _get_client_pid(invocation); _INFO("client Id = [%u]", pid); - int return_code = _check_priviliege_account_read(invocation); - if (return_code != ACCOUNT_ERROR_NONE) - { - _ERR("_check_priviliege_account_read failed, ret = %d", return_code); - goto RETURN; - } - return_code = _check_priviliege_account_write(invocation); - if (return_code != ACCOUNT_ERROR_NONE) - { - _ERR("_check_priviliege_account_write failed, ret = %d", return_code); - goto RETURN; + if( permission ) { + return_code = _check_priviliege_account_read(invocation); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_check_priviliege_account_read failed, ret = %d", return_code); + goto RETURN; + } + return_code = _check_priviliege_account_write(invocation); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_check_priviliege_account_write failed, ret = %d", return_code); + goto RETURN; + } } return_code = _account_db_open(1, pid); if (return_code != ACCOUNT_ERROR_NONE) { _ERR("_account_db_open() error, ret = %d", return_code); - goto RETURN; } -- 2.7.4 From bcac5653b20c347a7505aa58382839149aeca229 Mon Sep 17 00:00:00 2001 From: "jiseob.jang" Date: Mon, 13 Jul 2015 20:52:02 +0900 Subject: [PATCH 09/11] change the order of returning error value. - account_delete_from_db_by_package_name Change-Id: Iacbb80d578954194284abde0a1012bd3edc12b55 Signed-off-by: jiseob.jang --- server/src/account-server-db.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/server/src/account-server-db.c b/server/src/account-server-db.c index dccc8b9..7c5926e 100644 --- a/server/src/account-server-db.c +++ b/server/src/account-server-db.c @@ -3730,6 +3730,18 @@ int _account_delete_from_db_by_package_name(int pid, const char *package_name, g ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!")); ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + // It only needs list of ids, does not need to query sensitive info. So sending 0 + GList* account_list_temp = _account_query_account_by_package_name(getpid(), package_name, &ret); + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if(ret != ACCOUNT_ERROR_NONE){ + _ERR("_account_query_account_by_package_name failed ret=[%d]", ret); + return ret; + } + /* Check permission of requested appid */ if(permission){ char* current_appid = NULL; @@ -3748,29 +3760,18 @@ int _account_delete_from_db_by_package_name(int pid, const char *package_name, g if(error_code != ACCOUNT_ERROR_NONE){ ACCOUNT_ERROR("No permission to delete\n"); + _account_glist_free(account_list_temp); return ACCOUNT_ERROR_PERMISSION_DENIED; } } - // It only needs list of ids, does not need to query sensitive info. So sending 0 - GList* account_list_temp = _account_query_account_by_package_name(getpid(), package_name, &ret); - if( _account_db_err_code() == SQLITE_PERM ){ - ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); - return ACCOUNT_ERROR_PERMISSION_DENIED; - } - - if(ret != ACCOUNT_ERROR_NONE){ - _ERR("_account_query_account_by_package_name failed ret=[%d]", ret); - return ret; - } - - account_list_temp = g_list_first(account_list_temp); - _INFO("account_list_temp length=[%d]",g_list_length(account_list_temp)); + GList *account_list = g_list_first(account_list_temp); + _INFO("account_list_temp length=[%d]",g_list_length(account_list)); GList* iter = NULL; - for (iter = account_list_temp; iter != NULL; iter = g_list_next(iter)) + for (iter = account_list; iter != NULL; iter = g_list_next(iter)) { - _INFO("iterating account_list_temp"); + _INFO("iterating account_list"); account_s *account = NULL; _INFO("Before iter->data"); account = (account_s*)iter->data; @@ -3788,6 +3789,8 @@ int _account_delete_from_db_by_package_name(int pid, const char *package_name, g } } + _account_glist_free(account_list_temp); + /* transaction control required*/ ret_transaction = _account_begin_transaction(); -- 2.7.4 From 90716ddcd5e1c6804e15bfa1fa29552f8335b3d1 Mon Sep 17 00:00:00 2001 From: "jiseob.jang" Date: Wed, 15 Jul 2015 19:01:40 +0900 Subject: [PATCH 10/11] change accounts-service target to multi-user.target and remove transmitted data unneccessarily Change-Id: Ibab7baea0b8974fb9bd99ce8af55967b6d88453d Signed-off-by: jiseob.jang --- packaging/account-manager.spec | 33 +++++++++++--------------------- packaging/accounts-service.service | 5 +---- server/src/account-server.c | 39 +++++++++++--------------------------- 3 files changed, 23 insertions(+), 54 deletions(-) diff --git a/packaging/account-manager.spec b/packaging/account-manager.spec index e80a097..2c5f6e6 100644 --- a/packaging/account-manager.spec +++ b/packaging/account-manager.spec @@ -55,16 +55,15 @@ make %{?jobs:-j%jobs} rm -rf %{buildroot} %make_install -mkdir -p %{buildroot}%{_unitdir}/default.target.wants +mkdir -p %{buildroot}%{_unitdir}/multi-user.target.wants install -m 0644 %SOURCE1 %{buildroot}%{_unitdir}/accounts-service.service -ln -s ../accounts-service.service %{buildroot}%{_unitdir}/default.target.wants/accounts-service.service -#ln -s ../accounts-service.service %{buildroot}%{_unitdir}/multi-user.target.wants/accounts-service.service +#ln -s ../accounts-service.service %{buildroot}%{_unitdir}/default.target.wants/accounts-service.service +ln -s ../accounts-service.service %{buildroot}%{_unitdir}/multi-user.target.wants/accounts-service.service -rm -rf %{buildroot}/usr/lib/account-manager +#rm -rf %{buildroot}/usr/lib/account-manager - -mkdir -p %{buildroot}%{_unitdir}/dbus-1/system-services -install -m 0644 %SOURCE2 %{buildroot}%{_unitdir}/dbus-1/system-services/org.tizen.account.manager.service +mkdir -p %{buildroot}/usr/share/dbus-1/system-services +install -m 0644 %SOURCE2 %{buildroot}/usr/share/dbus-1/system-services/org.tizen.account.manager.service mkdir -p %{buildroot}%{_sysconfdir}/dbus-1/system.d install -m 0644 %{SOURCE3} %{buildroot}%{_sysconfdir}/dbus-1/system.d/ @@ -98,26 +97,16 @@ chown system:system %{TZ_SYS_DB}/.account.db-journal chmod 600 %{TZ_SYS_DB}/.account.db chmod 600 %{TZ_SYS_DB}/.account.db-journal -#set message key value to NULL -#vconftool set -t string db/account/msg '' -g 6514 -#vconftool set -tf string db/account/msg '' -s libaccounts-svc -u 200 -g 5000 - -#smack labeling -#chsmack -a 'System' %{TZ_SYS_DB}/.account.db-journal -#chsmack -a 'System' %{TZ_SYS_DB}/.account.db - - %postun -p /sbin/ldconfig - %files %manifest account-svcd.manifest %defattr(-,system,system,-) %config %{_sysconfdir}/dbus-1/system.d/org.tizen.account.manager.conf -%attr(0755,root,root) %{_bindir}/account-svcd -%attr(-,root,root) %{_unitdir}/accounts-service.service -#%attr(-,root,root) %{_unitdir}/multi-user.target.wants/accounts-service.service -%attr(-,root,root) %{_unitdir}/default.target.wants/accounts-service.service -%attr(-,root,root) %{_unitdir}/dbus-1/system-services/org.tizen.account.manager.service +%{_bindir}/account-svcd +%{_unitdir}/accounts-service.service +%{_unitdir}/multi-user.target.wants/accounts-service.service +#%attr(-,root,root) %{_unitdir}/default.target.wants/accounts-service.service +/usr/share/dbus-1/system-services/org.tizen.account.manager.service diff --git a/packaging/accounts-service.service b/packaging/accounts-service.service index fd63fe4..863afaf 100644 --- a/packaging/accounts-service.service +++ b/packaging/accounts-service.service @@ -6,9 +6,6 @@ User=system Group=system Type=simple ExecStart=/usr/bin/account-svcd -Restart=always -RestartSec=1 - [Install] -WantedBy=default.target +WantedBy=multi-user.target diff --git a/server/src/account-server.c b/server/src/account-server.c index e1ff9e8..b79ff68 100644 --- a/server/src/account-server.c +++ b/server/src/account-server.c @@ -239,7 +239,7 @@ int _check_priviliege_account_write(GDBusMethodInvocation *invocation) return _check_privilege(invocation, _PRIVILEGE_ACCOUNT_WRITE); } -gboolean account_manager_account_add(AccountManager *obj, GDBusMethodInvocation *invocation, gchar* account_db_path, GVariant* account_data, gpointer user_data) +gboolean account_manager_account_add(AccountManager *obj, GDBusMethodInvocation *invocation, GVariant* account_data, gpointer user_data) { _INFO("account_manager_account_add start"); int db_id = -1; @@ -265,7 +265,7 @@ gboolean account_manager_account_add(AccountManager *obj, GDBusMethodInvocation return_code = _account_db_open(1, pid); if (return_code != ACCOUNT_ERROR_NONE) { - _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + _ERR("_account_db_open() error, ret = %d", return_code); goto RETURN; } @@ -314,7 +314,7 @@ RETURN: return true; } -gboolean account_manager_account_query_all(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path) +gboolean account_manager_account_query_all(AccountManager *obj, GDBusMethodInvocation *invocation) { _INFO("account_manager_account_query_all start"); @@ -380,7 +380,7 @@ RETURN: return true; } -gboolean account_manager_account_type_query_all(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path) +gboolean account_manager_account_type_query_all(AccountManager *obj, GDBusMethodInvocation *invocation) { _INFO("account_manager_account_query_all start"); @@ -445,7 +445,7 @@ RETURN: return true; } -gboolean account_manager_account_type_add(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, GVariant *account_type_data, gpointer user_data) +gboolean account_manager_account_type_add(AccountManager *obj, GDBusMethodInvocation *invocation, GVariant *account_type_data, gpointer user_data) { int db_id = -1; account_type_s* account_type = NULL; @@ -520,7 +520,6 @@ RETURN: gboolean account_manager_account_delete_from_db_by_id(AccountManager *object, GDBusMethodInvocation *invocation, - gchar *account_db_path, gint account_db_id) { _INFO("account_manager_account_delete_from_db_by_id start"); @@ -585,7 +584,6 @@ RETURN: gboolean account_manager_account_delete_from_db_by_user_name(AccountManager *object, GDBusMethodInvocation *invocation, - gchar *account_db_path, const gchar *user_name, const gchar *package_name) { @@ -651,7 +649,6 @@ RETURN: gboolean account_manager_account_delete_from_db_by_package_name(AccountManager *object, GDBusMethodInvocation *invocation, - gchar *account_db_path, const gchar *package_name, gboolean permission) { _INFO("account_manager_account_delete_from_db_by_package_name start"); @@ -718,7 +715,6 @@ RETURN: gboolean account_manager_account_update_to_db_by_id(AccountManager *object, GDBusMethodInvocation *invocation, - gchar *account_db_path, GVariant *account_data, gint account_id) { @@ -795,7 +791,6 @@ RETURN: gboolean account_manager_handle_account_update_to_db_by_user_name(AccountManager *object, GDBusMethodInvocation *invocation, - gchar *account_db_path, GVariant *account_data, const gchar *user_name, const gchar *package_name) @@ -874,7 +869,6 @@ RETURN: gboolean account_manager_handle_account_type_query_label_by_locale(AccountManager *object, GDBusMethodInvocation *invocation, - gchar *account_db_path, const gchar *app_id, const gchar *locale) { @@ -935,7 +929,7 @@ RETURN: gboolean account_manager_handle_account_type_query_by_provider_feature(AccountManager *obj, GDBusMethodInvocation *invocation, - gchar *account_db_path, const gchar *key) + const gchar *key) { _INFO("account_manager_handle_account_type_query_by_provider_feature start"); GVariant* account_type_list_variant = NULL; @@ -1011,7 +1005,7 @@ RETURN: return true; } -gboolean account_manager_account_get_total_count_from_db(AccountManager *object, GDBusMethodInvocation *invocation, gchar *account_db_path, gboolean include_hidden) +gboolean account_manager_account_get_total_count_from_db(AccountManager *object, GDBusMethodInvocation *invocation, gboolean include_hidden) { _INFO("account_manager_account_get_total_count_from_db start"); guint pid = _get_client_pid(invocation); @@ -1069,7 +1063,7 @@ RETURN: } gboolean account_manager_handle_account_query_account_by_account_id(AccountManager *object, GDBusMethodInvocation *invocation, - gchar *account_db_path, gint account_db_id) + gint account_db_id) { _INFO("account_manager_handle_account_query_account_by_account_id start"); GVariant* account_variant = NULL; @@ -1144,7 +1138,7 @@ RETURN: gboolean account_manager_handle_account_query_account_by_user_name(AccountManager *obj, GDBusMethodInvocation *invocation, - gchar *account_db_path, const gchar *user_name) + const gchar *user_name) { _INFO("account_manager_handle_account_query_account_by_user_name start"); @@ -1218,7 +1212,7 @@ RETURN: gboolean account_manager_handle_account_query_account_by_package_name(AccountManager *obj, GDBusMethodInvocation *invocation, - gchar *account_db_path, const gchar *package_name) + const gchar *package_name) { _INFO("account_manager_handle_account_query_account_by_package_name start"); @@ -1292,7 +1286,6 @@ RETURN: gboolean account_manager_handle_account_query_account_by_capability(AccountManager *obj, GDBusMethodInvocation *invocation, - gchar *account_db_path, const gchar *capability_type, gint capability_value) { @@ -1370,7 +1363,6 @@ RETURN: gboolean account_manager_handle_account_query_account_by_capability_type(AccountManager *obj, GDBusMethodInvocation *invocation, - gchar *account_db_path, const gchar *capability_type) { _INFO("account_manager_handle_account_query_account_by_capability_type start"); @@ -1447,7 +1439,6 @@ RETURN: gboolean account_manager_handle_account_query_capability_by_account_id(AccountManager *obj, GDBusMethodInvocation *invocation, - gchar *account_db_path, const int account_id) { _INFO("account_manager_handle_account_query_capability_by_account_id start"); @@ -1523,7 +1514,6 @@ RETURN: gboolean account_manager_handle_account_update_sync_status_by_id(AccountManager *object, GDBusMethodInvocation *invocation, - gchar *account_db_path, const int account_db_id, const int sync_status) { @@ -1589,7 +1579,6 @@ RETURN: gboolean account_manager_handle_account_type_query_provider_feature_by_app_id(AccountManager *obj, GDBusMethodInvocation *invocation, - gchar *account_db_path, const gchar* app_id) { GSList* feature_record_list = NULL; @@ -1661,7 +1650,6 @@ RETURN: gboolean account_manager_handle_account_type_query_supported_feature(AccountManager *obj, GDBusMethodInvocation *invocation, - gchar *account_db_path, const gchar* app_id, const gchar* capability) { @@ -1722,7 +1710,6 @@ RETURN: gboolean account_manager_handle_account_type_update_to_db_by_app_id (AccountManager *obj, GDBusMethodInvocation *invocation, - gchar *account_db_path, GVariant *account_type_variant, const gchar *app_id) { @@ -1793,7 +1780,6 @@ RETURN: gboolean account_manager_handle_account_type_delete_by_app_id (AccountManager *obj, GDBusMethodInvocation *invocation, - gchar *account_db_path, const gchar *app_id) { _INFO("account_manager_handle_account_type_delete_by_app_id start"); @@ -1858,7 +1844,6 @@ RETURN: gboolean account_manager_handle_account_type_query_label_by_app_id (AccountManager *obj, GDBusMethodInvocation *invocation, - gchar *account_db_path, const gchar *app_id) { _INFO("account_manager_handle_account_type_query_label_by_app_id start"); @@ -1921,7 +1906,6 @@ RETURN: gboolean account_manager_handle_account_type_query_by_app_id (AccountManager *obj, GDBusMethodInvocation *invocation, - gchar *account_db_path, const gchar *app_id) { _INFO("account_manager_handle_account_type_query_by_app_id start"); @@ -1991,7 +1975,7 @@ RETURN: gboolean account_manager_handle_account_type_query_app_id_exist (AccountManager *obj, GDBusMethodInvocation *invocation, - gchar *account_db_path, const gchar *app_id) + const gchar *app_id) { _INFO("account_manager_handle_account_type_query_app_id_exist start"); guint pid = _get_client_pid(invocation); @@ -2049,7 +2033,6 @@ RETURN: gboolean account_manager_handle_account_update_to_db_by_id_ex (AccountManager *obj, GDBusMethodInvocation *invocation, - gchar *account_db_path, GVariant *account_data, gint account_id) { -- 2.7.4 From b738db1c4842a72695d083ae27c989c9291c3fa3 Mon Sep 17 00:00:00 2001 From: "jiseob.jang" Date: Tue, 4 Aug 2015 19:18:23 +0900 Subject: [PATCH 11/11] merge from tizen_2.4 Change-Id: I3b0b5addec4ae5b1b708e7b443403c70f670ce26 Signed-off-by: jiseob.jang --- CMakeLists.txt | 2 -- packaging/account-manager.spec | 28 +++++++++++----------------- packaging/accounts-service.service | 11 ----------- server/src/account-server-db.c | 23 ++++++++++++++++++++--- 4 files changed, 31 insertions(+), 33 deletions(-) delete mode 100644 packaging/accounts-service.service diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d4b164..f9a03f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,5 @@ SET(PREFIX ${CMAKE_INSTALL_PREFIX}) SET(EXEC_PREFIX "\${prefix}") SET(LIBDIR "\${prefix}/lib") SET(INCLUDEDIR "\${prefix}/include ") -SET(VERSION_MAJOR 0) -SET(VERSION "${VERSION_MAJOR}.0.1") ADD_SUBDIRECTORY(server) diff --git a/packaging/account-manager.spec b/packaging/account-manager.spec index 2c5f6e6..d17cc6c 100644 --- a/packaging/account-manager.spec +++ b/packaging/account-manager.spec @@ -1,14 +1,13 @@ Name: account-manager Summary: Account Manager -Version: 0.0.1 +Version: 0.0.4 Release: 1 Group: Social & Content/Other License: Apache-2.0 Source0: account-manager-%{version}.tar.gz -Source1: accounts-service.service -Source2: org.tizen.account.manager.service -Source3: org.tizen.account.manager.conf +Source1: org.tizen.account.manager.service +Source2: org.tizen.account.manager.conf BuildRequires: cmake BuildRequires: pkgconfig(dlog) @@ -34,7 +33,7 @@ Account Daemon: no %prep %setup -q -cp %{SOURCE2} . +cp %{SOURCE1} . %build #export CFLAGS+=" -Wextra -Wcast-align -Wcast-qual -Wshadow -Wwrite-strings -Wswitch-default" @@ -55,18 +54,13 @@ make %{?jobs:-j%jobs} rm -rf %{buildroot} %make_install -mkdir -p %{buildroot}%{_unitdir}/multi-user.target.wants -install -m 0644 %SOURCE1 %{buildroot}%{_unitdir}/accounts-service.service -#ln -s ../accounts-service.service %{buildroot}%{_unitdir}/default.target.wants/accounts-service.service -ln -s ../accounts-service.service %{buildroot}%{_unitdir}/multi-user.target.wants/accounts-service.service - -#rm -rf %{buildroot}/usr/lib/account-manager +rm -rf %{buildroot}/usr/lib/account-manager mkdir -p %{buildroot}/usr/share/dbus-1/system-services -install -m 0644 %SOURCE2 %{buildroot}/usr/share/dbus-1/system-services/org.tizen.account.manager.service +install -m 0644 %SOURCE1 %{buildroot}/usr/share/dbus-1/system-services/org.tizen.account.manager.service mkdir -p %{buildroot}%{_sysconfdir}/dbus-1/system.d -install -m 0644 %{SOURCE3} %{buildroot}%{_sysconfdir}/dbus-1/system.d/ +install -m 0644 %{SOURCE2} %{buildroot}%{_sysconfdir}/dbus-1/system.d/ %post /sbin/ldconfig @@ -97,6 +91,9 @@ chown system:system %{TZ_SYS_DB}/.account.db-journal chmod 600 %{TZ_SYS_DB}/.account.db chmod 600 %{TZ_SYS_DB}/.account.db-journal +#smack labeling +chsmack -a 'System' %{TZ_SYS_DB}/.account.db-journal +chsmack -a 'System' %{TZ_SYS_DB}/.account.db %postun -p /sbin/ldconfig @@ -105,8 +102,5 @@ chmod 600 %{TZ_SYS_DB}/.account.db-journal %defattr(-,system,system,-) %config %{_sysconfdir}/dbus-1/system.d/org.tizen.account.manager.conf %{_bindir}/account-svcd -%{_unitdir}/accounts-service.service -%{_unitdir}/multi-user.target.wants/accounts-service.service -#%attr(-,root,root) %{_unitdir}/default.target.wants/accounts-service.service -/usr/share/dbus-1/system-services/org.tizen.account.manager.service +%attr(0644,system,system) /usr/share/dbus-1/system-services/org.tizen.account.manager.service diff --git a/packaging/accounts-service.service b/packaging/accounts-service.service deleted file mode 100644 index 863afaf..0000000 --- a/packaging/accounts-service.service +++ /dev/null @@ -1,11 +0,0 @@ -[Unit] -Description=Accounts service - -[Service] -User=system -Group=system -Type=simple -ExecStart=/usr/bin/account-svcd - -[Install] -WantedBy=multi-user.target diff --git a/server/src/account-server-db.c b/server/src/account-server-db.c index 7c5926e..43c5443 100644 --- a/server/src/account-server-db.c +++ b/server/src/account-server-db.c @@ -48,7 +48,7 @@ typedef sqlite3_stmt* account_stmt; #define RCS_APPID "com.samsung.rcs-im" #define IMS_SERVICE_APPID "ims-service" -#define ACTIVESYNC_APPID "activesync-ui" +#define ACTIVESYNC_APPID "eas-ui" #define EMAIL_APPID "email-setting-efl" #define SYNCHRONISE_APPID "setting-synchronise-efl" #define DS_AGENT_CMDLINE "/usr/bin/oma-ds-agent" @@ -56,6 +56,9 @@ typedef sqlite3_stmt* account_stmt; #define FACEBOOK_SDK_APPID "com.samsung.facebook-service" #define FACEBOOK_APPID "com.samsung.facebook" +#define EASYSIGNUP_CMDLINE "/usr/bin/esu-agent" +#define EASYSIGNUP_APPID "com.samsung.esu-agent" + #define ACCOUNT_DB_OPEN_READONLY 0 #define ACCOUNT_DB_OPEN_READWRITE 1 @@ -201,6 +204,10 @@ static char* _account_get_current_appid(int pid) appid_ret = _account_get_text(SYNCHRONISE_APPID); _ACCOUNT_FREE(cmdline); return appid_ret; + } else if (!g_strcmp0(cmdline, EASYSIGNUP_CMDLINE)) { + appid_ret = _account_get_text(EASYSIGNUP_APPID); + _ACCOUNT_FREE(cmdline); + return appid_ret; } else { ACCOUNT_DEBUG("No app id\n"); _ACCOUNT_FREE(cmdline); @@ -258,6 +265,11 @@ static int _account_check_account_type_with_appid_group(const char* appid, char* return ACCOUNT_ERROR_NONE; } + if(!strcmp(appid, EASYSIGNUP_APPID)){ + ACCOUNT_DEBUG("easysignup exception\n"); + *verified_appid = _account_get_text(appid); + return ACCOUNT_ERROR_NONE; + } /* Get app id family which is stored in account database */ int pkgmgr_ret = -1; pkgmgr_ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle); @@ -327,11 +339,16 @@ static int _account_check_appid_group_with_package_name(const char* appid, char* } /* ims-service Exception */ - if ( strcmp(appid, "ims-service") == 0 && strcmp(package_name, "ims-service") == 0 ) { + if ( strcmp(appid, IMS_SERVICE_APPID) == 0 && strcmp(package_name, IMS_SERVICE_APPID) == 0 ) { ACCOUNT_DEBUG("ims exception."); // TODO: NEED TO REMOVE, debug log. return ACCOUNT_ERROR_NONE; } + /* easysignup Exception */ + if ( strcmp(appid, EASYSIGNUP_APPID) == 0 && strcmp(package_name, EASYSIGNUP_APPID) == 0 ) { + ACCOUNT_DEBUG("easysignup exception."); // TODO: NEED TO REMOVE, debug log. + return ACCOUNT_ERROR_NONE; + } /* Get app id family which is stored in account database */ int pkgmgr_ret = -1; pkgmgr_ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle); @@ -361,7 +378,7 @@ static int _account_check_appid_group_with_package_name(const char* appid, char* _ACCOUNT_FREE(tmp); break; } else if ( strcmp(tmp, "com.samsung.samsung-account-front") == 0 && - strcmp(package_name, "gr47by21a5.SamsungAccount") == 0 ) { + strcmp(package_name, "com.samsung.samsungaccount") == 0 ) { /* Samung Account Exception */ error_code = ACCOUNT_ERROR_NONE; _ACCOUNT_FREE(tmp); -- 2.7.4