From: Sunghyun Kwon Date: Tue, 14 Apr 2015 11:33:04 +0000 (+0900) Subject: Apply the cynara api X-Git-Tag: accepted/tizen/common/20150416.120414^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fheads%2Ftizen_3.0.2015.q2_common;p=platform%2Fcore%2Fmessaging%2Femail-service.git Apply the cynara api Change-Id: I1931b4366b1346505c291cb3bc5c6d110d35eef5 --- diff --git a/email-core/CMakeLists.txt b/email-core/CMakeLists.txt index e07534a..107d734 100755 --- a/email-core/CMakeLists.txt +++ b/email-core/CMakeLists.txt @@ -178,6 +178,34 @@ SET_TARGET_PROPERTIES(${CORE-SOUND-LIB} PROPERTIES VERSION ${VERSION}) INSTALL(TARGETS ${CORE-SOUND-LIB} DESTINATION ${LIB_INSTALL_DIR} COMPONENT RuntimeLibraries) ########################################################## +# Define cynara lib +########################################################## + +SET(CYNARA-LIB "email-cynara") +SET(CYNARA-SRCS + ${CMAKE_SOURCE_DIR}/email-core/email-core-cynara.c +) + +INCLUDE_DIRECTORIES( + ${CMAKE_SOURCE_DIR}/email-common-use/include +) + +INCLUDE(FindPkgConfig) +SET(PKG_MODULES cynara-client cynara-creds-socket cynara-session) + +pkg_check_modules(cynara_pkgs REQUIRED ${PKG_MODULES}) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${cynara_pkgs_CFLAGS}") +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed") + +ADD_LIBRARY(${CYNARA-LIB} SHARED ${CYNARA-SRCS}) +TARGET_LINK_LIBRARIES(${CYNARA-LIB} ${cynara_pkgs_LDFLAGS} dl) +SET_TARGET_PROPERTIES(${CYNARA-LIB} PROPERTIES SOVERSION ${VERSION_MAJOR}) +SET_TARGET_PROPERTIES(${CYNARA-LIB} PROPERTIES VERSION ${VERSION}) + +INSTALL(TARGETS ${CYNARA-LIB} DESTINATION ${LIB_INSTALL_DIR} COMPONENT RuntimeLibraries) + +########################################################## # Define Core lib ########################################################## @@ -215,9 +243,6 @@ INCLUDE_DIRECTORIES( INCLUDE(FindPkgConfig) pkg_check_modules(core_pkgs REQUIRED glib-2.0 drm-client dlog dbus-1 gthread-2.0 uw-imap-toolkit vconf vconf-internal-keys secure-storage openssl accounts-svc mm-player mm-session feedback alarm-service notification libcurl libxml-2.0 cert-svc cert-svc-vcore badge capi-appfw-application icu-i18n libtzplatform-config) - -#pkg_check_modules(core_pkgs REQUIRED glib-2.0 drm-client dlog dbus-1 gthread-2.0 uw-imap-toolkit vconf vconf-internal-keys contacts-service2 secure-storage openssl accounts-svc mm-player mm-session feedback alarm-service notification libcurl libxml-2.0 cert-svc cert-svc-vcore badge capi-appfw-application icu-i18n libtzplatform-config) - set(EXTRA_CFLAGS "") FOREACH(flag ${core_pkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") @@ -248,7 +273,7 @@ SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed") ADD_LIBRARY(${CORE-LIB} SHARED ${CORE-SRCS}) -TARGET_LINK_LIBRARIES(${CORE-LIB} ${core_pkgs_LDFLAGS} ${STORAGE-LIB} ${NETWORK-LIB} ${DEVICE-LIB} ${CORE-SOUND-LIB}) +TARGET_LINK_LIBRARIES(${CORE-LIB} ${core_pkgs_LDFLAGS} ${STORAGE-LIB} ${NETWORK-LIB} ${DEVICE-LIB} ${CORE-SOUND-LIB} ${CYNARA-LIB}) SET_TARGET_PROPERTIES(${CORE-LIB} PROPERTIES SOVERSION ${VERSION_MAJOR}) SET_TARGET_PROPERTIES(${CORE-LIB} PROPERTIES VERSION ${VERSION}) diff --git a/email-core/email-core-cynara.c b/email-core/email-core-cynara.c new file mode 100644 index 0000000..42aa047 --- /dev/null +++ b/email-core/email-core-cynara.c @@ -0,0 +1,215 @@ +/* +* email-service +* +* Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. +* +* Contact: Sunghyun Kwon , Minsoo Kim +* +* 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. +* +*/ + +/** + * + * This file contains functionality related to cynara(privilege) + * to interact with email-service. + * @file email-core-cynara.c + * @author sh0701.kwon@samsung.com + * @version 0.1 + * @brief This file contains functionality to provide cynara support in email-service. + */ + +#include +#include +#include +#include +#include + +#include "email-debug-log.h" +#include "email-utilities.h" + +typedef struct _cynara_info_t { + cynara *email_cynara; + enum cynara_client_creds client_method; + enum cynara_user_creds user_method; +} cynara_info_t; + +static cynara_info_t *cynara_info = NULL; +pthread_mutex_t cynara_mutex = PTHREAD_MUTEX_INITIALIZER; + +INTERNAL_FUNC int emcore_init_cynara() +{ + EM_DEBUG_FUNC_BEGIN(); + int ret = CYNARA_API_SUCCESS; + int err = EMAIL_ERROR_NONE; + char errno_buf[ERRNO_BUF_SIZE] = {0}; + + ENTER_CRITICAL_SECTION(cynara_mutex); + cynara_info = (cynara_info_t *)em_malloc(sizeof(cynara_info)); + if (cynara_info == NULL) { + EM_DEBUG_EXCEPTION("em_malloc failed"); + err = EMAIL_ERROR_OUT_OF_MEMORY; + goto FINISH_OFF; + } + + ret = cynara_initialize(&(cynara_info->email_cynara), NULL); + if (ret != CYNARA_API_SUCCESS) { + cynara_strerror(ret, errno_buf, ERRNO_BUF_SIZE); + EM_DEBUG_EXCEPTION("cynara_initialize failed : [%d], error : [%s]", + ret, + errno_buf); + err = EMAIL_ERROR_NOT_INITIALIZED; + goto FINISH_OFF; + } + + ret = cynara_creds_get_default_client_method(&(cynara_info->client_method)); + if (ret != CYNARA_API_SUCCESS) { + cynara_strerror(ret, errno_buf, ERRNO_BUF_SIZE); + EM_DEBUG_EXCEPTION("cynara_creds_get_default_client_method failed : [%d], error : [%s]", + ret, + errno_buf); + err = EMAIL_ERROR_NOT_INITIALIZED; + goto FINISH_OFF; + } + + ret = cynara_creds_get_default_user_method(&(cynara_info->user_method)); + if (ret != CYNARA_API_SUCCESS) { + cynara_strerror(ret, errno_buf, ERRNO_BUF_SIZE); + EM_DEBUG_EXCEPTION("cynara_creds_get_default_user_method failed : [%d], error : [%s]", + ret, + errno_buf); + err = EMAIL_ERROR_NOT_INITIALIZED; + goto FINISH_OFF; + } + +FINISH_OFF: + + LEAVE_CRITICAL_SECTION(cynara_mutex); + + EM_DEBUG_FUNC_END(); + return err; +} + +INTERNAL_FUNC void emcore_finish_cynara() +{ + EM_DEBUG_FUNC_BEGIN(); + int ret = CYNARA_API_SUCCESS; + char errno_buf[ERRNO_BUF_SIZE] = {0}; + + if (cynara_info == NULL) { + EM_DEBUG_EXCEPTION("cynara did not initialize"); + return; + } + + ENTER_CRITICAL_SECTION(cynara_mutex); + ret = cynara_finish(cynara_info->email_cynara); + if (ret != CYNARA_API_SUCCESS) { + cynara_strerror(ret, errno_buf, ERRNO_BUF_SIZE); + EM_DEBUG_EXCEPTION("cynara_finish failed : [%d], error : [%s]", + ret, + errno_buf); + } + EM_SAFE_FREE(cynara_info); + LEAVE_CRITICAL_SECTION(cynara_mutex); + + EM_DEBUG_FUNC_END(); +} + +INTERNAL_FUNC int emcore_check_privilege(int socket_fd) +{ + EM_DEBUG_FUNC_BEGIN(); + int ret = CYNARA_API_SUCCESS; + int err = EMAIL_ERROR_NONE; + char errno_buf[ERRNO_BUF_SIZE] = {0}; + + if (socket_fd < 0) { + EM_DEBUG_EXCEPTION("Invalid parameter"); + err = EMAIL_ERROR_INVALID_PARAM; + return err; + } + + if (cynara_info->email_cynara == NULL) { + err = emcore_init_cynara(); + if (err != EMAIL_ERROR_NONE) { + EM_DEBUG_EXCEPTION("emcore_init_cynara failed : [%d]", err); + return err; + } + } + + err = EMAIL_ERROR_PERMISSION_DENIED; + + pid_t client_pid = 0; + char *client_uid = NULL; + char *client_smack = NULL; + char *client_session = NULL; + + ENTER_CRITICAL_SECTION(cynara_mutex); + + ret = cynara_creds_socket_get_client(socket_fd, cynara_info->client_method, &client_smack); + if (ret != CYNARA_API_SUCCESS) { + cynara_strerror(ret, errno_buf, ERRNO_BUF_SIZE); + EM_DEBUG_EXCEPTION("cynara_creds_socket_get_client failed : [%d], error : [%s]", + ret, + errno_buf); + goto FINISH_OFF; + } + + ret = cynara_creds_socket_get_user(socket_fd, cynara_info->user_method, &client_uid); + if (ret != CYNARA_API_SUCCESS) { + cynara_strerror(ret, errno_buf, ERRNO_BUF_SIZE); + EM_DEBUG_EXCEPTION("cynara_creds_socket_get_user failed : [%d], error : [%s]", + ret, + errno_buf); + goto FINISH_OFF; + } + + ret = cynara_creds_socket_get_pid(socket_fd, &client_pid); + if (ret != CYNARA_API_SUCCESS) { + cynara_strerror(ret, errno_buf, ERRNO_BUF_SIZE); + EM_DEBUG_EXCEPTION("cynara_creds_socket_get_pid failed : [%d], error : [%s]", + ret, + errno_buf); + goto FINISH_OFF; + } + + client_session = cynara_session_from_pid(client_pid); + if (client_session == NULL) { + cynara_strerror(ret, errno_buf, ERRNO_BUF_SIZE); + EM_DEBUG_EXCEPTION("cynara_session_from_pid failed error : [%s]", + errno_buf); + goto FINISH_OFF; + } + + ret = cynara_check(cynara_info->email_cynara, client_smack, client_session, client_uid, + "http://tizen.org/privilege/email"); + if (ret != CYNARA_API_ACCESS_ALLOWED) { + cynara_strerror(ret, errno_buf, ERRNO_BUF_SIZE); + EM_DEBUG_EXCEPTION("cynara_check failed : [%d], error : [%s]", + ret, + errno_buf); + goto FINISH_OFF; + } + + err = EMAIL_ERROR_NONE; + +FINISH_OFF: + + LEAVE_CRITICAL_SECTION(cynara_mutex); + + EM_SAFE_FREE(client_uid); + EM_SAFE_FREE(client_smack); + EM_SAFE_FREE(client_session); + + EM_DEBUG_FUNC_END(); + return err; +} diff --git a/email-core/include/email-core-cynara.h b/email-core/include/email-core-cynara.h new file mode 100644 index 0000000..2f844c3 --- /dev/null +++ b/email-core/include/email-core-cynara.h @@ -0,0 +1,36 @@ +/* +* email-service +* +* Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. +* +* Contact: Sunghyun Kwon , Minsoo Kim +* +* 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. +* +*/ + +/** + * + * This file contains functionality related to cynara(privilege) + * to interact with email-service. + * @file email-core-cynara.h + * @author sh0701.kwon@samsung.com + * @version 0.1 + * @brief This file contains functionality to provide cynara support in email-service. + */ + +INTERNAL_FUNC int emcore_init_cynara(); + +INTERNAL_FUNC void emcore_finish_cynara(); + +INTERNAL_FUNC int emcore_check_privilege(int socket_fd); diff --git a/email-daemon/email-daemon-init.c b/email-daemon/email-daemon-init.c index c54aa02..a9ce80f 100755 --- a/email-daemon/email-daemon-init.c +++ b/email-daemon/email-daemon-init.c @@ -61,6 +61,7 @@ #include "email-daemon-emn.h" #include "email-network.h" #include "email-device.h" +#include "email-core-cynara.h" #include "c-client.h" extern void * @@ -452,6 +453,12 @@ INTERNAL_FUNC int emdaemon_initialize(int* err_code) g_type_init(); + err = emcore_init_cynara(); + if (err != EMAIL_ERROR_NONE) { + EM_DEBUG_EXCEPTION("emcore_init_cynara failed : [%d]", err); + goto FINISH_OFF; + } + emstorage_shm_file_init(SHM_FILE_FOR_DB_LOCK); #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ @@ -542,7 +549,10 @@ INTERNAL_FUNC int emdaemon_finalize(int* err_code) EM_DEBUG_EXCEPTION("_emdaemon_unload_email_core failed [%d]", err); goto FINISH_OFF; } - + + /* Finish cynara */ + emcore_finish_cynara(); + /* free account reference list */ emcore_free_account_reference(); diff --git a/email-daemon/main.c b/email-daemon/main.c index 1900f03..232920a 100755 --- a/email-daemon/main.c +++ b/email-daemon/main.c @@ -3222,6 +3222,20 @@ void stb_API_mapper(HIPC_API a_hAPI) EM_DEBUG_FUNC_BEGIN(); int err = EMAIL_ERROR_NONE; int nAPIID = emipc_get_api_id(a_hAPI); + int client_fd = emipc_get_response_id(a_hAPI); + + err = emcore_check_privilege(client_fd); + if (err != EMAIL_ERROR_NONE) { + EM_DEBUG_EXCEPTION("emcore_check_privilege failed : [%d]", err); + + if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) + EM_DEBUG_EXCEPTION("emipc_add_paramter failed"); + + if (!emipc_execute_stub_api(a_hAPI)) + EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed"); + + return; + } switch(nAPIID) { case _EMAIL_API_ADD_ACCOUNT: diff --git a/email-ipc/email-ipc-api.c b/email-ipc/email-ipc-api.c index c2d527f..d1cc2a5 100755 --- a/email-ipc/email-ipc-api.c +++ b/email-ipc/email-ipc-api.c @@ -82,6 +82,13 @@ EXPORT_API long emipc_get_app_id(HIPC_API api) return api_info->app_id; } +EXPORT_API long emipc_get_response_id(HIPC_API api) +{ + EM_DEBUG_FUNC_BEGIN(); + emipc_email_api_info *api_info = (emipc_email_api_info *)api; + return api_info->response_id; +} + /* note: there incurs additional cost (malloc & memcpy). */ /* if data is a dynamic variable, please use emipc_dynamic_parameter instead */ EXPORT_API bool emipc_add_parameter(HIPC_API api, EPARAMETER_DIRECTION direction, void *data, int data_length) diff --git a/email-ipc/email-stub/email-stub-socket.c b/email-ipc/email-stub/email-stub-socket.c index 9450d20..fcddee1 100755 --- a/email-ipc/email-stub/email-stub-socket.c +++ b/email-ipc/email-stub/email-stub-socket.c @@ -107,7 +107,7 @@ static void *emipc_stub_socket_thread_proc() static int emipc_check_connected(int fd) { EM_DEBUG_FUNC_BEGIN ("fd[%d]", fd); - int found = (g_list_find (connected_fd, (gpointer)fd))? true : false; + int found = (g_list_find(connected_fd, (gpointer)fd)) ? true : false; EM_DEBUG_FUNC_END ("fd found?? [%d]", found); return found; } @@ -158,19 +158,22 @@ EXPORT_API void emipc_wait_for_ipc_request() } else { for (i = 0; i < event_num; i++) { int event_fd = events[i].data.fd; + GList *tmp_list = NULL; if (event_fd == stub_socket) { /* if it is socket connection request */ int cfd = emipc_accept_email_socket (stub_socket); if (cfd < 0) { EM_DEBUG_EXCEPTION ("emipc_accept_email_socket failed [%d]", cfd); - /* EM_DEBUG_CRITICAL_EXCEPTION ("accept failed: %s[%d]", EM_STRERROR(errno_buf), errno);*/ + continue; } ev.events = EPOLLIN; ev.data.fd = cfd; if (epoll_ctl(epfd, EPOLL_CTL_ADD, cfd, &ev) == -1) { EM_DEBUG_EXCEPTION("epoll_ctl failed [%s][%d]", EM_STRERROR(errno_buf), errno); - /*EM_DEBUG_CRITICAL_EXCEPTION("epoll_ctl failed:%s[%d]", EM_STRERROR(errno_buf), errno);*/ + close(cfd); + continue; } + connected_fd = g_list_prepend (connected_fd, (gpointer)cfd); } else { int recv_len; @@ -178,7 +181,7 @@ EXPORT_API void emipc_wait_for_ipc_request() recv_len = emipc_recv_email_socket(event_fd, &sz_buf); - if(recv_len > 0) { + if (recv_len > 0) { EM_DEBUG_LOG("[IPCLib]Stub Socket Recv [Socket ID = %d], [recv_len = %d]", event_fd, recv_len); /* IPC request stream is at least 16byte */ @@ -186,13 +189,14 @@ EXPORT_API void emipc_wait_for_ipc_request() emipc_create_task((unsigned char *)sz_buf, event_fd); } else EM_DEBUG_LOG("[IPCLib] Stream size is less than default size"); - } else if( recv_len == 0 ) { + } else if (recv_len == 0) { EM_DEBUG_LOG("[IPCLib] Client closed connection [%d]", event_fd); if (epoll_ctl(epfd, EPOLL_CTL_DEL, event_fd, events) == -1) { EM_DEBUG_EXCEPTION("epoll_ctl failed: %s[%d]", EM_STRERROR(errno_buf), errno); EM_DEBUG_CRITICAL_EXCEPTION("epoll_ctl failed: %s[%d]", EM_STRERROR(errno_buf), errno); } - connected_fd = g_list_remove (connected_fd, (gpointer)event_fd); + + connected_fd = g_list_remove(connected_fd, (gpointer)event_fd); close(event_fd); } EM_SAFE_FREE(sz_buf); diff --git a/email-ipc/email-stub/email-stub-task-manager.c b/email-ipc/email-stub/email-stub-task-manager.c index 3774d2a..1229066 100755 --- a/email-ipc/email-stub/email-stub-task-manager.c +++ b/email-ipc/email-stub/email-stub-task-manager.c @@ -1,7 +1,7 @@ /* * email-service * -* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved. +* Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd. All rights reserved. * * Contact: Kyuho Jo , Sunghyun Kwon * diff --git a/email-ipc/email-stub/email-stub-task.c b/email-ipc/email-stub/email-stub-task.c index 45937f4..02c17cd 100755 --- a/email-ipc/email-stub/email-stub-task.c +++ b/email-ipc/email-stub/email-stub-task.c @@ -62,6 +62,7 @@ EXPORT_API bool emipc_parse_stream_email_task(emipc_email_task *task, void *stre EM_DEBUG_EXCEPTION("emipc_deserialize_api_info failed"); return false; } + task->api_info->response_id = response_id; return true; } diff --git a/email-ipc/include/email-ipc.h b/email-ipc/include/email-ipc.h index 5af460c..b167255 100755 --- a/email-ipc/include/email-ipc.h +++ b/email-ipc/include/email-ipc.h @@ -72,6 +72,7 @@ EXPORT_API void emipc_destroy_email_api(HIPC_API input_api_handle); EXPORT_API long emipc_get_api_id(HIPC_API input_api_handle); EXPORT_API long emipc_get_app_id(HIPC_API input_api_handle); +EXPORT_API long emipc_get_response_id(HIPC_API input_api_handle); EXPORT_API bool emipc_add_parameter(HIPC_API api, EPARAMETER_DIRECTION direction, void *data, int data_length); EXPORT_API bool emipc_add_dynamic_parameter(HIPC_API api, EPARAMETER_DIRECTION direction, void *data, int data_length); diff --git a/packaging/email-service.spec b/packaging/email-service.spec index 624ac9b..f9a8ea3 100755 --- a/packaging/email-service.spec +++ b/packaging/email-service.spec @@ -56,6 +56,10 @@ BuildRequires: pkgconfig(pmapi) BuildRequires: pkgconfig(libsmack) BuildRequires: pkgconfig(deviced) BuildRequires: pkgconfig(icu-i18n) +BuildRequires: pkgconfig(cynara-client) +BuildRequires: pkgconfig(cynara-creds-socket) +BuildRequires: pkgconfig(cynara-session) +BuildRequires: pkgconfig(cynara-creds-commons) BuildRequires: pkgconfig(libtzplatform-config) Requires: libtzplatform-config