From f17729d36596c1bb347a3ade5d86f6d05bd931e3 Mon Sep 17 00:00:00 2001 From: greatim Date: Thu, 25 Aug 2016 20:05:12 +0900 Subject: [PATCH 01/16] change umask value to 022 from 000 because of security reason change umask value to 022 from 000 because of security reason add S_IWOTH for pushed file to owner's content directory Change-Id: Ie0677c4bfa8c494c13ab0ac32cdb730f90d22864 Signed-off-by: greatim --- src/file_sync_service.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++-- src/sdb.c | 10 +++---- src/sdb.h | 2 ++ 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/src/file_sync_service.c b/src/file_sync_service.c index 5fc6642..b0a0f07 100644 --- a/src/file_sync_service.c +++ b/src/file_sync_service.c @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include #include #include @@ -58,6 +59,8 @@ struct sync_permit_rule sdk_sync_permit_rule[] = { */ #define DIR_PERMISSION 0777 +static struct tzplatform_context* sdk_user_context = NULL; + void init_sdk_sync_permit_rule_regx(void) { int ret; @@ -334,6 +337,62 @@ static void sync_mediadb(char *path) { return; } +static void deinit_sdk_user_context(void) { + if (sdk_user_context != NULL) { + tzplatform_context_destroy(sdk_user_context); + sdk_user_context = NULL; + } +} + +// return 0 if success to initialize +// return negative value otherwise +static int init_sdk_user_context() { + if (sdk_user_context == NULL) { + int ret = tzplatform_context_create(&sdk_user_context); + if (ret < 0) { + D ("failed to create tzplatform context by error (%d)\n", ret); + return ret; + } + + ret = tzplatform_context_set_user(sdk_user_context, g_sdk_user_id); + if (ret < 0) { + D ("failed to set user to sdk_user_context\n"); + return ret; + } + + atexit(deinit_sdk_user_context); + } + + return 0; +} + +// return 1 if given directory is writable by others +// return 0 otherwise +static int is_writable_by_others(char* path) { + int ret = 0; + + if ( init_sdk_user_context() == 0 ) { + const char* content_path = tzplatform_context_getenv(sdk_user_context, TZ_USER_CONTENT); + char* abpath = realpath(path, NULL); + D ("tzplatform getenv : %s\n", content_path); + + if (abpath != NULL) { + if (strncmp(abpath, content_path, strlen(content_path)) == 0) { + D("path (%s) is writable by others\n", path); + ret = 1; + } + free(abpath); + } else { + D("failed to get realpath of (%s)\n", path); + } + } else { + // do nothing + // no directory is writable by others + } + + return ret; +} + static int handle_send_file(int s, int noti_fd, char *path, mode_t mode, char *buffer) { syncmsg msg; @@ -495,6 +554,8 @@ static int do_send(int s, int noti_fd, char *path, char *buffer) return -1; } + D("send path (%s)\n", path); + tmp = strrchr(path,','); if(tmp) { *tmp = 0; @@ -505,7 +566,6 @@ static int do_send(int s, int noti_fd, char *path, char *buffer) #endif // extracts file permission from stat.mode. (ex 100644 & 0777 = 644); mode &= 0777; // combination of (S_IRWXU | S_IRWXG | S_IRWXO) - mode |= S_IWOTH; // SDK requirement from N_SE-43337 } if(!tmp || errno) { mode = 0644; // set default permission value in most of unix system. @@ -523,7 +583,6 @@ static int do_send(int s, int noti_fd, char *path, char *buffer) // sdb does not allow to check that file exists or not. After deleting old file and creating new file again unconditionally. sdb_unlink(path); - #ifdef HAVE_SYMLINKS if(is_link) ret = handle_send_link(s, noti_fd, path, buffer); @@ -539,6 +598,13 @@ static int do_send(int s, int noti_fd, char *path, char *buffer) //mode |= ((mode >> 3) & 0070); //mode |= ((mode >> 3) & 0007); ret = handle_send_file(s, noti_fd, path, mode, buffer); + if (is_writable_by_others(path)) { + mode_t realmode = mode & ~(SDBD_UMASK); + realmode |= S_IWOTH; + if (chmod(path, realmode) != 0) { + D ("failed to chmod of writable path by others\n"); + } + } } return ret; diff --git a/src/sdb.c b/src/sdb.c index 22d2794..a30b3c4 100644 --- a/src/sdb.c +++ b/src/sdb.c @@ -1084,9 +1084,6 @@ void start_device_log(void) int daemonize(void) { - // set file creation mask to 0 - umask(0); - switch (fork()) { case -1: return -1; @@ -1095,6 +1092,11 @@ int daemonize(void) { default: _exit(0); } + + // 2016-08-25 : modified umask to 022 from 000 because of security reason + // there is a problem that the file created by sdb shell command could be written by 'others' + umask(SDBD_UMASK); + #ifdef SDB_PIDPATH FILE *f = fopen(SDB_PIDPATH, "w"); @@ -2200,8 +2202,6 @@ int sdb_main(int is_daemon, int server_port) D("sdbd should be launched in develop mode.\n"); return -1; } - - umask(000); #endif atexit(sdb_cleanup); diff --git a/src/sdb.h b/src/sdb.h index c9a9e0c..36f28e1 100644 --- a/src/sdb.h +++ b/src/sdb.h @@ -47,6 +47,8 @@ #define SDB_SERVER_VERSION 0 // Increment this when we want to force users to start a new sdb server +#define SDBD_UMASK 0022 // default file creation mask of sdbd + typedef struct amessage amessage; typedef struct apacket apacket; typedef struct asocket asocket; -- 2.7.4 From 32d8f697b178247d59333685e2b476ae91a91606 Mon Sep 17 00:00:00 2001 From: "shingil.kang" Date: Fri, 23 Sep 2016 17:50:37 +0900 Subject: [PATCH 02/16] Fixed a buffer underflow which could happen during processing sdb shell command. Change-Id: I0faa48855632d00e18b9e054b4f83e786b24bdd9 Signed-off-by: shingil.kang --- src/services.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services.c b/src/services.c index 2fbcaa7..64cc80b 100644 --- a/src/services.c +++ b/src/services.c @@ -563,7 +563,7 @@ static void get_env(char *key, char **env) e = buf + (strlen(buf) - 1); // trim string - while(*e == ' ' || *e == '\n' || *e == '\t') { + while( (e > s) && (*e == ' ' || *e == '\n' || *e == '\t')) { e--; } *(e+1) ='\0'; -- 2.7.4 From 8f64f2f8e369cb60aa96fd90193c7438043738a7 Mon Sep 17 00:00:00 2001 From: Kim Gunsoo Date: Mon, 26 Sep 2016 15:21:56 +0900 Subject: [PATCH 03/16] Fixed execve EFAULT error on the asan environment. - the envp array of pointers must be terminated by a NULL pointer. Change-Id: Ieb64b20c1fdf24aec16ae248faf051889ddb76d4 Signed-off-by: Kim Gunsoo --- src/services.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/services.c b/src/services.c index 2fbcaa7..6fe6b71 100644 --- a/src/services.c +++ b/src/services.c @@ -597,9 +597,12 @@ static int create_subproc_thread(const char *name, int lines, int columns) char *value = NULL; char *trim_value = NULL; char path[PATH_MAX]; - memset(path, 0, sizeof(path)); char *envp[MAX_TOKENS]; int envp_cnt = 0; + + memset(path, 0, sizeof(path)); + memset(envp, 0, sizeof(envp)); + envp[envp_cnt++] = strdup("TERM=linux"); envp[envp_cnt++] = strdup("DISPLAY=:0"); -- 2.7.4 From b194539380929a0535f336677086c7a978d5d01f Mon Sep 17 00:00:00 2001 From: Kim Gunsoo Date: Fri, 26 Feb 2016 14:41:50 +0900 Subject: [PATCH 04/16] Add KEEP_ALIVE option to the socket connected with sdb server. - After connected with sdb server through TCP socket, add KEEP_ALIVE option to the socket. Change-Id: I16ff01721dd8f919782c666871053a6e7599ec0e Signed-off-by: Kim Gunsoo --- src/transport_local.c | 10 ++++++++++ src/utils.c | 33 +++++++++++++++++++++++++++++++++ src/utils.h | 5 +++++ 3 files changed, 48 insertions(+) diff --git a/src/transport_local.c b/src/transport_local.c index ff4aa36..16a595c 100644 --- a/src/transport_local.c +++ b/src/transport_local.c @@ -36,6 +36,7 @@ #if !SDB_HOST #include "commandline_sdbd.h" #endif +#include "utils.h" #ifdef HAVE_BIG_ENDIAN #define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24) @@ -294,6 +295,15 @@ static void *server_socket_thread(void * arg) && !request_plugin_verification(SDBD_CMD_VERIFY_PEERIP, inet_ntoa(addr.sin_addr))) { sdb_close(fd); } else { + int ret = -1; + ret = keep_alive(fd, 1, SDB_KEEPALIVE_CNT, SDB_KEEPALIVE_IDLE, SDB_KEEPALIVE_INTVL); + if (ret < 0) { + D("failed to set keep alive option. FD(%d), error=%s\n", fd, strerror(errno)); + } else { + D("Success to set keep alive option. FD(%d), cnt=%d, idle=%d(sec), interval=%d(sec)\n", + fd, SDB_KEEPALIVE_CNT, SDB_KEEPALIVE_IDLE, SDB_KEEPALIVE_INTVL); + } + register_socket_transport(fd, "host", port, 1, NULL); } } else { diff --git a/src/utils.c b/src/utils.c index acf8c5a..6f48ba2 100644 --- a/src/utils.c +++ b/src/utils.c @@ -25,6 +25,9 @@ #include #include +#include +#include + #define STRING_MAXLEN 1024 char* buff_addc (char* buff, char* buffEnd, int c) @@ -222,3 +225,33 @@ char** str_split(char* a_str, const char a_delim) { return result; } +int keep_alive(int fd, int onoff, int cnt, int idle, int interval) +{ + int ret = -1; + + ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &onoff, sizeof(onoff)); + if (ret == -1) { + return ret; + } + + /* override /proc/sys/net/ipv4/tcp_keepalive_probes */ + ret = setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt)); + if (ret == -1) { + return ret; + } + + /* override /proc/sys/net/ipv4/tcp_keepalive_time */ + ret = setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &idle, sizeof(idle)); + if (ret == -1) { + return ret; + } + + /* override /proc/sys/net/ipv4/tcp_keepalive_intvl */ + ret = setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &interval, sizeof(interval)); + if (ret == -1) { + return ret; + } + + return 0; +} + diff --git a/src/utils.h b/src/utils.h index ce17442..7e78b6e 100644 --- a/src/utils.h +++ b/src/utils.h @@ -76,4 +76,9 @@ int spawn(const char* program, char * const arg_list[]); char** str_split(char* a_str, const char a_delim); +#define SDB_KEEPALIVE_CNT (9) +#define SDB_KEEPALIVE_IDLE (1) +#define SDB_KEEPALIVE_INTVL (1) +int keep_alive(int fd, int onoff, int cnt, int idle, int interval); + #endif /* _SDB_UTILS_H */ -- 2.7.4 From 679d5a6846cc20d290d35cc6d7b091cc3936c807 Mon Sep 17 00:00:00 2001 From: Slava Barinov Date: Mon, 26 Sep 2016 18:06:29 +0300 Subject: [PATCH 05/16] Fix the sdb shell malfunction in ASan build Accordingly to exec(3) manual the envp argument is an array of pointers to null-terminated strings and must be terminated by a null pointer. Without the terminator execvp fails and errno is set to 'Bad address' Change-Id: I00e6927fd62323b7a9bea918cc79b0c4e02d77fd Signed-off-by: Slava Barinov --- src/services.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/services.c b/src/services.c index 2fbcaa7..c3f72c1 100644 --- a/src/services.c +++ b/src/services.c @@ -653,6 +653,9 @@ static int create_subproc_thread(const char *name, int lines, int columns) } free(envp_plugin); + /* Last element of envp must be the NULL-terminator to prevent execvp fail */ + envp[envp_cnt] = NULL; + if(name) { // in case of shell execution directly // Check the shell command validation. if (!request_plugin_verification(SDBD_CMD_VERIFY_SHELLCMD, name)) { -- 2.7.4 From 6c50e44f5468b1b905a5d8692942361a8e41b103 Mon Sep 17 00:00:00 2001 From: Jaewon Lim Date: Tue, 4 Oct 2016 04:13:51 -0700 Subject: [PATCH 06/16] Revert "change umask value to 022 from 000 because of security reason" This reverts commit f17729d36596c1bb347a3ade5d86f6d05bd931e3. Change-Id: Id9368d876bf0c3cfe068d7e4567dff8b4bec135b --- src/file_sync_service.c | 70 ++----------------------------------------------- src/sdb.c | 10 +++---- src/sdb.h | 2 -- 3 files changed, 7 insertions(+), 75 deletions(-) diff --git a/src/file_sync_service.c b/src/file_sync_service.c index b0a0f07..5fc6642 100644 --- a/src/file_sync_service.c +++ b/src/file_sync_service.c @@ -14,7 +14,6 @@ * limitations under the License. */ -#include #include #include #include @@ -59,8 +58,6 @@ struct sync_permit_rule sdk_sync_permit_rule[] = { */ #define DIR_PERMISSION 0777 -static struct tzplatform_context* sdk_user_context = NULL; - void init_sdk_sync_permit_rule_regx(void) { int ret; @@ -337,62 +334,6 @@ static void sync_mediadb(char *path) { return; } -static void deinit_sdk_user_context(void) { - if (sdk_user_context != NULL) { - tzplatform_context_destroy(sdk_user_context); - sdk_user_context = NULL; - } -} - -// return 0 if success to initialize -// return negative value otherwise -static int init_sdk_user_context() { - if (sdk_user_context == NULL) { - int ret = tzplatform_context_create(&sdk_user_context); - if (ret < 0) { - D ("failed to create tzplatform context by error (%d)\n", ret); - return ret; - } - - ret = tzplatform_context_set_user(sdk_user_context, g_sdk_user_id); - if (ret < 0) { - D ("failed to set user to sdk_user_context\n"); - return ret; - } - - atexit(deinit_sdk_user_context); - } - - return 0; -} - -// return 1 if given directory is writable by others -// return 0 otherwise -static int is_writable_by_others(char* path) { - int ret = 0; - - if ( init_sdk_user_context() == 0 ) { - const char* content_path = tzplatform_context_getenv(sdk_user_context, TZ_USER_CONTENT); - char* abpath = realpath(path, NULL); - D ("tzplatform getenv : %s\n", content_path); - - if (abpath != NULL) { - if (strncmp(abpath, content_path, strlen(content_path)) == 0) { - D("path (%s) is writable by others\n", path); - ret = 1; - } - free(abpath); - } else { - D("failed to get realpath of (%s)\n", path); - } - } else { - // do nothing - // no directory is writable by others - } - - return ret; -} - static int handle_send_file(int s, int noti_fd, char *path, mode_t mode, char *buffer) { syncmsg msg; @@ -554,8 +495,6 @@ static int do_send(int s, int noti_fd, char *path, char *buffer) return -1; } - D("send path (%s)\n", path); - tmp = strrchr(path,','); if(tmp) { *tmp = 0; @@ -566,6 +505,7 @@ static int do_send(int s, int noti_fd, char *path, char *buffer) #endif // extracts file permission from stat.mode. (ex 100644 & 0777 = 644); mode &= 0777; // combination of (S_IRWXU | S_IRWXG | S_IRWXO) + mode |= S_IWOTH; // SDK requirement from N_SE-43337 } if(!tmp || errno) { mode = 0644; // set default permission value in most of unix system. @@ -583,6 +523,7 @@ static int do_send(int s, int noti_fd, char *path, char *buffer) // sdb does not allow to check that file exists or not. After deleting old file and creating new file again unconditionally. sdb_unlink(path); + #ifdef HAVE_SYMLINKS if(is_link) ret = handle_send_link(s, noti_fd, path, buffer); @@ -598,13 +539,6 @@ static int do_send(int s, int noti_fd, char *path, char *buffer) //mode |= ((mode >> 3) & 0070); //mode |= ((mode >> 3) & 0007); ret = handle_send_file(s, noti_fd, path, mode, buffer); - if (is_writable_by_others(path)) { - mode_t realmode = mode & ~(SDBD_UMASK); - realmode |= S_IWOTH; - if (chmod(path, realmode) != 0) { - D ("failed to chmod of writable path by others\n"); - } - } } return ret; diff --git a/src/sdb.c b/src/sdb.c index a30b3c4..22d2794 100644 --- a/src/sdb.c +++ b/src/sdb.c @@ -1084,6 +1084,9 @@ void start_device_log(void) int daemonize(void) { + // set file creation mask to 0 + umask(0); + switch (fork()) { case -1: return -1; @@ -1092,11 +1095,6 @@ int daemonize(void) { default: _exit(0); } - - // 2016-08-25 : modified umask to 022 from 000 because of security reason - // there is a problem that the file created by sdb shell command could be written by 'others' - umask(SDBD_UMASK); - #ifdef SDB_PIDPATH FILE *f = fopen(SDB_PIDPATH, "w"); @@ -2202,6 +2200,8 @@ int sdb_main(int is_daemon, int server_port) D("sdbd should be launched in develop mode.\n"); return -1; } + + umask(000); #endif atexit(sdb_cleanup); diff --git a/src/sdb.h b/src/sdb.h index 36f28e1..c9a9e0c 100644 --- a/src/sdb.h +++ b/src/sdb.h @@ -47,8 +47,6 @@ #define SDB_SERVER_VERSION 0 // Increment this when we want to force users to start a new sdb server -#define SDBD_UMASK 0022 // default file creation mask of sdbd - typedef struct amessage amessage; typedef struct apacket apacket; typedef struct asocket asocket; -- 2.7.4 From d4cba1fb28283edc518e28be6b03d523688b38c2 Mon Sep 17 00:00:00 2001 From: Kim Gunsoo Date: Fri, 7 Oct 2016 13:50:46 +0900 Subject: [PATCH 07/16] Fixed SVACE issue. - Remove the vulnerable function 'strerror()'. Change-Id: I6f056b85a6ec55ab7221fd977e6da61558432904 Signed-off-by: Kim Gunsoo --- src/transport_local.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport_local.c b/src/transport_local.c index 16a595c..32e1f49 100644 --- a/src/transport_local.c +++ b/src/transport_local.c @@ -298,7 +298,7 @@ static void *server_socket_thread(void * arg) int ret = -1; ret = keep_alive(fd, 1, SDB_KEEPALIVE_CNT, SDB_KEEPALIVE_IDLE, SDB_KEEPALIVE_INTVL); if (ret < 0) { - D("failed to set keep alive option. FD(%d), error=%s\n", fd, strerror(errno)); + D("failed to set keep alive option. FD(%d), errno=%d\n", fd, errno); } else { D("Success to set keep alive option. FD(%d), cnt=%d, idle=%d(sec), interval=%d(sec)\n", fd, SDB_KEEPALIVE_CNT, SDB_KEEPALIVE_IDLE, SDB_KEEPALIVE_INTVL); -- 2.7.4 From 918bde735548931f72c357760aeffa3ed5848e78 Mon Sep 17 00:00:00 2001 From: greatim Date: Thu, 13 Oct 2016 16:30:08 +0900 Subject: [PATCH 08/16] modify spec file to make dev package To support pkgconfig for sdbd plugin, modify spec file to make dev package Change-Id: Ic9095cab424273d1ca3c526a779f5a4c23556abb Signed-off-by: greatim --- packaging/sdbd.pc | 9 +++++++++ packaging/sdbd.spec | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100755 packaging/sdbd.pc diff --git a/packaging/sdbd.pc b/packaging/sdbd.pc new file mode 100755 index 0000000..8221e52 --- /dev/null +++ b/packaging/sdbd.pc @@ -0,0 +1,9 @@ +prefix=/usr +exec_prefix=${prefix} +libdir=${exec_prefix}/lib +includedir=${prefix}/include + +Name: sdbd +Description: sdb daemon +Version: 3.0 +Cflags: -I${includedir}/sdb diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index a880c80..1cf6758 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -30,6 +30,12 @@ Requires: dbus %description Description: SDB daemon. +%package -n sdbd-devel +Summary: SDBD plugin API +Group: Development/Libraries + +%description -n sdbd-devel +SDBD plugin API library %prep %setup -q @@ -85,6 +91,12 @@ install -m 755 script/sdk_launch %{buildroot}%{_prefix}/sbin/ mkdir -p %{buildroot}%{TZ_SYS_BIN} install -m 755 script/profile_command %{buildroot}%{TZ_SYS_BIN}/ +mkdir -p %{buildroot}%{_includedir}/sdb/ +install -m 0644 src/sdbd_plugin.h %{buildroot}%{_includedir}/sdb/ + +mkdir -p %{buildroot}%{_libdir}/pkgconfig/ +install -m 0644 packaging/sdbd.pc %{buildroot}%{_libdir}/pkgconfig/ + %post . %{_sysconfdir}/tizen-platform.conf if ! getent passwd "${TZ_SDK_USER_NAME}" > /dev/null; then @@ -113,4 +125,8 @@ fi /usr/share/license/%{name} %{TZ_SYS_BIN}/profile_command +%files -n sdbd-devel +%{_includedir}/sdb/sdbd_plugin.h +%{_libdir}/pkgconfig/sdbd.pc + %changelog -- 2.7.4 From 0ddb7fa5e37bc43decb91c09597bbc08eef78f0a Mon Sep 17 00:00:00 2001 From: Kim Gunsoo Date: Thu, 20 Oct 2016 13:59:50 +0900 Subject: [PATCH 09/16] Adds error handling when a change of user privilege has failed. - When the authority to change the shell and sync service process has failed, add the error handling. Change-Id: I78a5ee314cad9a881a16dc7817ab6c85e11f0d57 Signed-off-by: Kim Gunsoo --- src/file_sync_service.c | 5 ++++- src/sdb.c | 3 +++ src/services.c | 5 ++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/file_sync_service.c b/src/file_sync_service.c index 5fc6642..bd0bf98 100644 --- a/src/file_sync_service.c +++ b/src/file_sync_service.c @@ -685,7 +685,10 @@ void file_sync_service(int fd, void *cookie) D("sync: '%s' '%s'\n", (char*) &msg.req, name); if (should_drop_privileges() && !verify_sync_rule(name)) { - set_sdk_user_privileges(); + if (getuid() != g_sdk_user_id && set_sdk_user_privileges() < 0) { + fail_message(fd, "failed to set SDK user privileges."); + goto fail; + } } switch(msg.req.id) { diff --git a/src/sdb.c b/src/sdb.c index f257a0f..d421ba7 100644 --- a/src/sdb.c +++ b/src/sdb.c @@ -1538,14 +1538,17 @@ int set_sdk_user_privileges() { if (sdbd_set_groups() < 0) { D("set groups failed (errno: %d)\n", errno); + return -1; } if (setgid(g_sdk_group_id) != 0) { D("set group id failed (errno: %d)\n", errno); + return -1; } if (setuid(g_sdk_user_id) != 0) { D("set user id failed (errno: %d)\n", errno); + return -1; } if (chdir(g_sdk_home_dir) < 0) { diff --git a/src/services.c b/src/services.c index b6d960a..2478680 100644 --- a/src/services.c +++ b/src/services.c @@ -493,7 +493,10 @@ static int create_subprocess(const char *cmd, pid_t *pid, char * const argv[], c // do nothing D("sdb: executes root commands!!:%s\n", argv[2]); } else { - set_sdk_user_privileges(); + if (getuid() != g_sdk_user_id && set_sdk_user_privileges() < 0) { + fprintf(stderr, "failed to set SDK user privileges\n"); + exit(-1); + } } } redirect_and_exec(pts, cmd, argv, envp); -- 2.7.4 From 0e947799e154f0d5e3cbf4a695a05d446dbc2370 Mon Sep 17 00:00:00 2001 From: Kim Gunsoo Date: Thu, 3 Nov 2016 12:23:34 +0900 Subject: [PATCH 10/16] Modify the type reading CPU architecture type from sysinfo. - To support 64bit CPU architecture, the type reading from sysinfo was changed to string. Change-Id: I88ac5d7ad2e76d4c2a83cb2912fa2db49d3cc809 Signed-off-by: Kim Gunsoo --- src/sdb.c | 41 ++++++++++++----------------------------- src/sdb.h | 3 --- 2 files changed, 12 insertions(+), 32 deletions(-) diff --git a/src/sdb.c b/src/sdb.c index d421ba7..0d8cf47 100644 --- a/src/sdb.c +++ b/src/sdb.c @@ -1943,33 +1943,6 @@ int request_plugin_verification(const char* cmd, const char* in_buf) { return 1; } -static char* get_cpu_architecture() -{ - int ret = 0; - bool b_value = false; - - ret = system_info_get_platform_bool( - "http://tizen.org/feature/platform.core.cpu.arch.armv6", &b_value); - if (ret == SYSTEM_INFO_ERROR_NONE && b_value) { - return CPUARCH_ARMV6; - } - - ret = system_info_get_platform_bool( - "http://tizen.org/feature/platform.core.cpu.arch.armv7", &b_value); - if (ret == SYSTEM_INFO_ERROR_NONE && b_value) { - return CPUARCH_ARMV7; - } - - ret = system_info_get_platform_bool( - "http://tizen.org/feature/platform.core.cpu.arch.x86", &b_value); - if (ret == SYSTEM_INFO_ERROR_NONE && b_value) { - return CPUARCH_X86; - } - - D("fail to get the CPU architecture of model:%d\n", errno); - return UNKNOWN; -} - static void init_capabilities(void) { int ret = -1; char *value = NULL; @@ -1977,8 +1950,18 @@ static void init_capabilities(void) { memset(&g_capabilities, 0, sizeof(g_capabilities)); // CPU Architecture of model - snprintf(g_capabilities.cpu_arch, sizeof(g_capabilities.cpu_arch), - "%s", get_cpu_architecture()); + ret = system_info_get_platform_string("http://tizen.org/feature/platform.core.cpu.arch", &value); + if (ret != SYSTEM_INFO_ERROR_NONE) { + snprintf(g_capabilities.cpu_arch, sizeof(g_capabilities.cpu_arch), + "%s", UNKNOWN); + D("fail to get the CPU architecture of model:%d\n", errno); + } else { + snprintf(g_capabilities.cpu_arch, sizeof(g_capabilities.cpu_arch), + "%s", value); + if (value != NULL) { + free(value); + } + } // Secure protocol support diff --git a/src/sdb.h b/src/sdb.h index 67f365f..b3b78f3 100644 --- a/src/sdb.h +++ b/src/sdb.h @@ -240,9 +240,6 @@ typedef struct platform_info { #define ENABLED "enabled" #define DISABLED "disabled" -#define CPUARCH_ARMV6 "armv6" -#define CPUARCH_ARMV7 "armv7" -#define CPUARCH_X86 "x86" #define CAPBUF_SIZE 4096 #define CAPBUF_ITEMSIZE 32 #define CAPBUF_L_ITEMSIZE 256 -- 2.7.4 From 4290e4be7477c1bfec81607cd17c3907c5b315c6 Mon Sep 17 00:00:00 2001 From: greatim Date: Fri, 5 Aug 2016 21:35:48 +0900 Subject: [PATCH 11/16] improve plugin architecture add default_plugin_*.c file for default plugin code add plugin.c/h file for plugin related code (plugin loading, unloading) refactoring plugin related codes Conflicts: src/sdb.c src/sdbd_plugin.h src/transport_local.c Change-Id: I3cc8858364f0121819c693713aa9772e4c633047 Signed-off-by: greatim --- CMakeLists.txt | 6 + src/default_plugin.h | 37 ++++ src/default_plugin_auth.c | 50 ++++++ src/default_plugin_basic.c | 202 ++++++++++++++++++++++ src/default_plugin_event.c | 138 +++++++++++++++ src/default_plugin_main.c | 80 +++++++++ src/file_sync_service.c | 10 +- src/hashtable.c | 168 ++++++++++++++++++ src/hashtable.h | 52 ++++++ src/log.h | 104 ++++++++++++ src/parameter.h | 107 ++++++++++++ src/plugin.c | 295 ++++++++++++++++++++++++++++++++ src/plugin.h | 52 ++++++ src/properties.c | 2 + src/qemu_pipe.h | 1 + src/sdb.c | 411 +++++---------------------------------------- src/sdb.h | 94 +---------- src/sdbd_plugin.h | 162 +++++++++++------- src/sdktools.c | 2 + src/services.c | 17 +- src/sockets.c | 2 + src/transport.c | 19 ++- src/transport_local.c | 7 +- src/transport_usb.c | 2 + src/usb_funcfs_client.c | 2 + src/usb_linux.c | 2 + src/usb_linux_client.c | 2 + 27 files changed, 1482 insertions(+), 544 deletions(-) create mode 100644 src/default_plugin.h create mode 100644 src/default_plugin_auth.c create mode 100644 src/default_plugin_basic.c create mode 100644 src/default_plugin_event.c create mode 100644 src/default_plugin_main.c create mode 100644 src/hashtable.c create mode 100644 src/hashtable.h create mode 100644 src/log.h create mode 100644 src/parameter.h create mode 100644 src/plugin.c create mode 100644 src/plugin.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f160f95..ae6f310 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,6 +51,12 @@ SET(SDBD_SRCS src/commandline_sdbd.c src/usb_linux_client.c src/usb_funcfs_client.c + src/default_plugin_auth.c + src/default_plugin_basic.c + src/default_plugin_main.c + src/default_plugin_event.c + src/hashtable.c + src/plugin.c ) include(FindPkgConfig) diff --git a/src/default_plugin.h b/src/default_plugin.h new file mode 100644 index 0000000..b8d44f3 --- /dev/null +++ b/src/default_plugin.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2011 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 __DEFAULT_PLUGIN_H +#define __DEFAULT_PLUGIN_H + +extern eventfunc event_handler; + +int get_plugin_capability ( parameters* in, parameters* out ); +int verify_shell_cmd ( parameters* in, parameters* out ); +int convert_shell_cmd ( parameters* in, parameters* out ); +int verify_peer_ip ( parameters* in, parameters* out ); +int verify_sdbd_launch ( parameters* in, parameters* out ); +int verify_root_cmd ( parameters* in, parameters* out ); +int get_lock_state ( parameters* in, parameters* out ); + +int auth_support ( parameters* in, parameters* out ); +int auth_get_key_file_paths ( parameters* in, parameters* out ); + +int confirm_public_key ( parameters* in, int out_fd ); + +void create_pwlock_thread(); + +#endif // __DEFAULT_PLUGIN_H diff --git a/src/default_plugin_auth.c b/src/default_plugin_auth.c new file mode 100644 index 0000000..8fa743a --- /dev/null +++ b/src/default_plugin_auth.c @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2011 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 + +#define TRACE_TAG TRACE_SDB +#include "log.h" + +#include "parameter.h" +#include "sdbd_plugin.h" +#include "default_plugin.h" + +int auth_support ( parameters* in, parameters* out ) +{ + if ( out == NULL ) { + D ( "Invalid argument\n" ); + return PLUGIN_CMD_FAIL; + } + + out->number_of_parameter = 1; + out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + out->array_of_parameter[0].type = type_int32; + out->array_of_parameter[0].v_int32 = PLUGIN_RET_INVALID; + + return PLUGIN_CMD_SUCCESS; +} + +int auth_get_key_file_paths ( parameters* in, parameters* out ) +{ + return PLUGIN_CMD_FAIL; + +} + +int confirm_public_key( parameters* in, int out_fd ) +{ + return PLUGIN_CMD_FAIL; +} diff --git a/src/default_plugin_basic.c b/src/default_plugin_basic.c new file mode 100644 index 0000000..58464c7 --- /dev/null +++ b/src/default_plugin_basic.c @@ -0,0 +1,202 @@ +/* + * Copyright (c) 2011 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 + +#define TRACE_TAG TRACE_SDB +#include "log.h" + +#include "sdb.h" +#include "parameter.h" +#include "sdbd_plugin.h" +#include "sdktools.h" + +#define LOG_DIRECTORY "/tmp" + +int get_plugin_capability ( parameters* in, parameters* out ) +{ + int capability; + + if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL + || in->array_of_parameter[0].type != type_int32 ) { + D ( "Invalid argument\n" ); + return PLUGIN_CMD_FAIL; + } + + if ( out == NULL ) { + D ( "Invalid argument\n" ); + return PLUGIN_CMD_FAIL; + } + + out->number_of_parameter = 1; + out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + + capability = in->array_of_parameter[0].v_int32; + + if ( capability == CAPABILITY_SECURE ) { + make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_DISABLED ); + } else if ( capability == CAPABILITY_INTER_SHELL ) { + make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_ENABLED ); + } else if ( capability == CAPABILITY_FILESYNC ) { + make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_PUSHPULL ); + } else if ( capability == CAPABILITY_USB_PROTOCOL ) { + if ( is_emulator() ) { + make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_DISABLED ); + } else { + make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_ENABLED ); + } + } else if ( capability == CAPABILITY_SOCK_PROTOCOL ) { + make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_ENABLED ); + } else if ( capability == CAPABILITY_ROOT_ONOFF ) { + if ( access ( "/bin/su", F_OK ) == 0 ) { + make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_ENABLED ); + } else { + make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_DISABLED ); + } + } else if ( capability == CAPABILITY_CAN_LAUNCH ) { + make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_UNKNOWN ); + } else if ( capability == CAPABILITY_PLUGIN_VER ) { + make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_UNKNOWN ); + } else if ( capability == CAPABILITY_PRODUCT_VER ) { + make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_UNKNOWN ); + } else if ( capability == CAPABILITY_LOG_ENABLE ) { + make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_DISABLED ); + } else if ( capability == CAPABILITY_LOG_PATH ) { + make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", LOG_DIRECTORY ); + } else { + out->number_of_parameter = 0; + free ( out->array_of_parameter ); + out->array_of_parameter = NULL; + return PLUGIN_CMD_NOT_SUPPORT; + } + + return PLUGIN_CMD_SUCCESS; +} + +int verify_shell_cmd ( parameters* in, parameters* out ) +{ + if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL + || in->array_of_parameter[0].type != type_string ) { + D ( "Invalid argument\n" ); + return PLUGIN_CMD_FAIL; + } + + if ( out == NULL ) { + D ( "Invalid argument\n" ); + return PLUGIN_CMD_FAIL; + } + + D ( "shell command : %s\n", in->array_of_parameter[0].v_string.data ); + + out->number_of_parameter = 1; + out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + out->array_of_parameter[0].type = type_int32; + out->array_of_parameter[0].v_int32 = PLUGIN_RET_VALID; + + return PLUGIN_CMD_SUCCESS; +} + +int convert_shell_cmd ( parameters* in, parameters* out ) +{ + if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL + || in->array_of_parameter[0].type != type_string ) { + D ( "Invalid argument\n" ); + return PLUGIN_CMD_FAIL; + } + + if ( out == NULL ) { + D ( "Invalid argument\n" ); + return PLUGIN_CMD_FAIL; + } + + D ( "shell command : %s\n", in->array_of_parameter[0].v_string.data ); + + out->number_of_parameter = 1; + out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + + make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", in->array_of_parameter[0].v_string.data ); + return PLUGIN_CMD_SUCCESS; +} + +int verify_peer_ip ( parameters* in, parameters* out ) +{ + if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL + || in->array_of_parameter[0].type != type_string ) { + D ( "Invalid argument\n" ); + return PLUGIN_CMD_FAIL; + } + + if ( out == NULL ) { + D ( "Invalid argument\n" ); + return PLUGIN_CMD_FAIL; + } + + D ( "shell command : %s\n", in->array_of_parameter[0].v_string.data ); + + out->number_of_parameter = 1; + out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + out->array_of_parameter[0].type = type_int32; + out->array_of_parameter[0].v_int32 = PLUGIN_RET_VALID; + + return PLUGIN_CMD_SUCCESS; +} + +int verify_sdbd_launch ( parameters* in, parameters* out ) +{ + if ( out == NULL ) { + D ( "Invalid argument\n" ); + return PLUGIN_CMD_FAIL; + } + + out->number_of_parameter = 1; + out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + out->array_of_parameter[0].type = type_int32; + out->array_of_parameter[0].v_int32 = PLUGIN_RET_VALID; + + return PLUGIN_CMD_SUCCESS; +} + +int verify_root_cmd ( parameters* in, parameters* out ) +{ + if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL + || in->array_of_parameter[0].type != type_string ) { + D ( "Invalid argument\n" ); + return PLUGIN_CMD_FAIL; + } + + if ( out == NULL ) { + D ( "Invalid argument\n" ); + return PLUGIN_CMD_FAIL; + } + + D ( "shell command : %s\n", in->array_of_parameter[0].v_string.data ); + + out->number_of_parameter = 1; + out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + out->array_of_parameter[0].type = type_int32; + + if ( verify_root_commands ( in->array_of_parameter[0].v_string.data ) ) { + out->array_of_parameter[0].v_int32 = PLUGIN_RET_VALID; + } else { + out->array_of_parameter[0].v_int32 = PLUGIN_RET_INVALID; + } + + return PLUGIN_CMD_SUCCESS; +} diff --git a/src/default_plugin_event.c b/src/default_plugin_event.c new file mode 100644 index 0000000..a336f94 --- /dev/null +++ b/src/default_plugin_event.c @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2011 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 + +#define TRACE_TAG TRACE_SDB +#include "log.h" + +#include "sysdeps.h" +#include "sdbd_plugin.h" +#include "default_plugin.h" + +int get_lock_state ( parameters* in, parameters* out ) +{ + if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL + || in->array_of_parameter[0].type != type_int32 ) { + D ( "Invalid argument\n" ); + return PLUGIN_CMD_FAIL; + } + + if ( out == NULL ) { + D ( "Invalid argument\n" ); + return PLUGIN_CMD_FAIL; + } + + D ( "shell command : %s\n", in->array_of_parameter[0].v_string.data ); + + out->number_of_parameter = 1; + out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + out->array_of_parameter[0].type = type_int32; + out->array_of_parameter[0].v_int32 = ( plugin_pwlocked() == 1 ) ? PLUGIN_RET_ON : PLUGIN_RET_OFF; + + return PLUGIN_CMD_SUCCESS; +} + +int plugin_pwlocked ( void ) +{ + int pwlock_status = 0; + int pwlock_type = 0; + + if ( vconf_get_int ( VCONFKEY_IDLE_LOCK_STATE, &pwlock_status ) ) { + pwlock_status = 0; + PLUGIN_LOG ( "failed to get pw lock status\n" ); + } +#ifdef _WEARABLE + PLUGIN_LOG ( "wearable lock applied\n" ); + // for wearable which uses different VCONF key (lock type) + if ( vconf_get_int ( VCONFKEY_SETAPPL_PRIVACY_LOCK_TYPE_INT, &pwlock_type ) ) { + pwlock_type = 0; + PLUGIN_LOG ( "failed to get pw lock type\n" ); + } + if ( ( pwlock_status == VCONFKEY_IDLE_LOCK ) && ( pwlock_type != SETTING_PRIVACY_LOCK_TYPE_NONE ) ) { + PLUGIN_LOG ( "device has been locked\n" ); + return 1; // locked! + } +#else + PLUGIN_LOG ( "mobile lock applied\n" ); + // for mobile + if ( vconf_get_int ( VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT, &pwlock_type ) ) { + pwlock_type = 0; + PLUGIN_LOG ( "failed to get pw lock type\n" ); + } + if ( pwlock_status == VCONFKEY_IDLE_LOCK && ( ( pwlock_type != SETTING_SCREEN_LOCK_TYPE_NONE ) && ( pwlock_type != SETTING_SCREEN_LOCK_TYPE_SWIPE ) ) ) { + PLUGIN_LOG ( "device has been locked\n" ); + return 1; // locked! + } +#endif + return 0; // unlocked! +} + +static void pwlock_cb ( keynode_t *key, void* data ) +{ + PLUGIN_LOG ( "pwlock callback is issued\n" ); + int pwlocked = plugin_pwlocked(); + + parameters* out = ( parameters* ) malloc ( sizeof ( parameters ) ); + out->number_of_parameter = 1; + out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + out->array_of_parameter[0].type = type_int32; + out->array_of_parameter[0].v_int32 = ( pwlocked == 1 ) ? PLUGIN_RET_ON : PLUGIN_RET_OFF; + + event_handler ( PLUGIN_EVENT_PWLOCK, out ); +} + +static void register_pwlock_cb() +{ + int ret = vconf_notify_key_changed ( VCONFKEY_IDLE_LOCK_STATE, pwlock_cb, NULL ); + if ( ret != 0 ) { + PLUGIN_LOG ( "cannot register vconf callback.\n" ); + return; + } + PLUGIN_LOG ( "registered vconf callback\n" ); +} + +static void unregister_pwlock_cb() +{ + int ret = vconf_ignore_key_changed ( VCONFKEY_IDLE_LOCK_STATE, pwlock_cb ); + if ( ret != 0 ) { + PLUGIN_LOG ( "cannot unregister vconf callback.\n" ); + return; + } + PLUGIN_LOG ( "unregistered vconf callback\n" ); +} + +static void *pwlock_thread ( void *x ) +{ + GMainLoop *loop; + loop = g_main_loop_new ( NULL, FALSE ); + register_pwlock_cb(); + g_main_loop_run ( loop ); + g_main_loop_unref ( loop ); + unregister_pwlock_cb(); + return NULL; +} + +void create_pwlock_thread() +{ + sdb_thread_t t; + if ( sdb_thread_create ( &t, pwlock_thread, NULL ) ) { + PLUGIN_LOG ( "cannot create_pwlock_thread.\n" ); + return; + } + PLUGIN_LOG ( "created pwlock_thread\n" ); +} diff --git a/src/default_plugin_main.c b/src/default_plugin_main.c new file mode 100644 index 0000000..4bcecb0 --- /dev/null +++ b/src/default_plugin_main.c @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2011 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 "parameter.h" +#include "sdbd_plugin.h" +#include "default_plugin.h" +#include "plugin.h" + +#define MAX_OUT_BUF_SIZE 128 + +eventfunc event_handler = NULL; + +int default_plugin_init ( eventfunc event_cb, registerfunc register_func ) +{ + // default plugin do not need to register command + // because unsupported command by plugin should be called with default plugin anyway + event_handler = event_cb; + + if (is_supported_by_plugin(PLUGIN_EVENT_PWLOCK) == 0) { + create_pwlock_thread(); + } + + return PLUGIN_CMD_SUCCESS; +} + +int default_plugin_sync_proc ( int cmd, parameters* in, parameters* out ) +{ + int ret = PLUGIN_CMD_NOT_SUPPORT; + + if ( cmd == PLUGIN_SYNC_CMD_CAPABILITY ) { + ret = get_plugin_capability ( in, out ); + } else if ( cmd == PLUGIN_SYNC_CMD_VERIFY_SHELLCMD ) { + ret = verify_shell_cmd ( in, out ); + } else if ( cmd == PLUGIN_SYNC_CMD_CONVERT_SHELLCMD ) { + ret = convert_shell_cmd ( in, out ); + } else if ( cmd == PLUGIN_SYNC_CMD_VERIFY_PEERIP ) { + ret = verify_peer_ip ( in, out ); + } else if ( cmd == PLUGIN_SYNC_CMD_VERIFY_LAUNCH ) { + ret = verify_sdbd_launch ( in, out ); + } else if ( cmd == PLUGIN_SYNC_CMD_VERIFY_ROOTCMD ) { + ret = verify_root_cmd ( in, out ); + } else if ( cmd == PLUGIN_SYNC_CMD_AUTH_SUPPORT ) { + ret = auth_support ( in, out ); + } else if ( cmd == PLUGIN_SYNC_CMD_AUTH_GET_KEY_FILEPATHS ) { + ret = auth_get_key_file_paths ( in, out ); + } else if ( cmd == PLUGIN_SYNC_CMD_GET_LOCK_STATE ) { + ret = get_lock_state ( in, out ); + } else { + ret = PLUGIN_CMD_NOT_SUPPORT; + } + + return ret; +} + +int default_plugin_async_proc ( int cmd, parameters* in, int out_fd ) +{ + int ret = PLUGIN_CMD_NOT_SUPPORT; + + if ( cmd == PLUGIN_ASYNC_CMD_AUTH_CONFIRM_PUBLIC ) { + ret = confirm_public_key ( in, out_fd ); + } else { + ret = PLUGIN_CMD_NOT_SUPPORT; + } + + return ret; +} + diff --git a/src/file_sync_service.c b/src/file_sync_service.c index bd0bf98..ffaf0bc 100644 --- a/src/file_sync_service.c +++ b/src/file_sync_service.c @@ -31,6 +31,8 @@ #include #define TRACE_TAG TRACE_SYNC +#include "log.h" + #include "sdb.h" #include "file_sync_service.h" #include "sdktools.h" @@ -470,14 +472,14 @@ static int handle_send_link(int s, int noti_fd, char *path, char *buffer) static int is_support_push() { - return (!strncmp(g_capabilities.filesync_support, SDBD_CAP_RET_PUSHPULL, strlen(SDBD_CAP_RET_PUSHPULL)) - || !strncmp(g_capabilities.filesync_support, SDBD_CAP_RET_PUSH, strlen(SDBD_CAP_RET_PUSH))); + return (!strncmp(g_capabilities.filesync_support, PLUGIN_RET_PUSHPULL, strlen(PLUGIN_RET_PUSHPULL)) + || !strncmp(g_capabilities.filesync_support, PLUGIN_RET_PUSH, strlen(PLUGIN_RET_PUSH))); } static int is_support_pull() { - return (!strncmp(g_capabilities.filesync_support, SDBD_CAP_RET_PUSHPULL, strlen(SDBD_CAP_RET_PUSHPULL)) - || !strncmp(g_capabilities.filesync_support, SDBD_CAP_RET_PULL, strlen(SDBD_CAP_RET_PULL))); + return (!strncmp(g_capabilities.filesync_support, PLUGIN_RET_PUSHPULL, strlen(PLUGIN_RET_PUSHPULL)) + || !strncmp(g_capabilities.filesync_support, PLUGIN_RET_PULL, strlen(PLUGIN_RET_PULL))); } static int do_send(int s, int noti_fd, char *path, char *buffer) diff --git a/src/hashtable.c b/src/hashtable.c new file mode 100644 index 0000000..fee79fd --- /dev/null +++ b/src/hashtable.c @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2011 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 "hashtable.h" + +static int get_hash_value ( hashtable* ht_table, int key ) +{ + return key % ht_table->size; +} + +hashtable* hashtable_create ( ht_size_t tablesize ) +{ + hashtable* htable = NULL; + int size = 0; + int i; + + switch ( tablesize ) { + case ht_size_7: + size = 7; + break; + case ht_size_13: + size = 13; + break; + case ht_size_31: + size = 31; + break; + case ht_size_127: + size = 127; + break; + case ht_size_1021: + size = 1021; + break; + case ht_size_8191: + size = 8191; + break; + default: + size = 31; + break; + } + + // allocate hash table + htable = ( hashtable* ) malloc ( sizeof ( hashtable ) ); + if ( htable == NULL ) { + return NULL; + } + + // allocate hash table entries + htable->table = ( ht_entry** ) malloc ( sizeof ( ht_entry* ) * size ); + if ( htable->table == NULL ) { + free ( htable ); + return NULL; + } + + // initialize hash table entries + for ( i = 0; i < size; i++ ) { + htable->table[i] = NULL; + } + + htable->size = size; + + return htable; +} + +void hashtable_destroy ( hashtable* ht_table ) +{ + if ( ht_table != NULL ) { + if ( ht_table->table != NULL ) { + int i; + for ( i = 0; i < ht_table->size; i++ ) { + ht_entry* entry = ht_table->table[i]; + while ( entry != NULL ) { + ht_entry* tmp = entry; + entry = entry->next; + free ( tmp ); + } + } + + free ( ht_table->table ); + } + + free ( ht_table ); + } +} + +// return 1 if put is successful +// return 0 if put is failed +int hashtable_put ( hashtable* ht_table, int key, int value ) +{ + int hash; + ht_entry* entry = NULL; + + hash = get_hash_value ( ht_table, key ); + entry = ht_table->table[hash]; + + while ( entry != NULL ) { + if ( entry->key != key ) { + entry = entry->next; + } else { + break; + } + } + + if ( entry != NULL ) { + // there is a entry which has same key + entry->value = value; + } else { + // there is no entry which has same key + + // make new entry + ht_entry* new_entry = ( ht_entry* ) malloc ( sizeof ( ht_entry ) ); + if ( new_entry == NULL ) { + return 0; + } + new_entry->key = key; + new_entry->value = value; + + // add to entry list at first position + new_entry->next = ht_table->table[hash]; + ht_table->table[hash] = new_entry; + } + + return 1; +} + +// return 1 if a entry is found in hash table +// return 0 if a entry is not found in hash table +// output value is stored in (*value) +int hashtable_get ( hashtable* ht_table, int key, int* value ) +{ + int hash; + ht_entry* entry = NULL; + + hash = get_hash_value ( ht_table, key ); + entry = ht_table->table[hash]; + + while ( entry != NULL ) { + if ( entry->key != key ) { + entry = entry->next; + } else { + break; + } + } + + if ( entry != NULL ) { + // there is a entry which has same key + *value = entry->value; + return 1; + } else { + // there is no entry which has same key + return 0; + } +} + diff --git a/src/hashtable.h b/src/hashtable.h new file mode 100644 index 0000000..25cded0 --- /dev/null +++ b/src/hashtable.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2011 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 __HASHTABLE_H +#define __HASHTABLE_H + +typedef enum _hashtable_size { + ht_size_7, + ht_size_13, + ht_size_31, + ht_size_127, + ht_size_1021, + ht_size_8191 +} ht_size_t; + +typedef struct _ht_entry { + int key; + int value; + struct _ht_entry* next; +} ht_entry; + +typedef struct _hashtable { + int size; + ht_entry** table; +} hashtable; + +hashtable* hashtable_create ( ht_size_t tablesize ); +void hashtable_destroy ( hashtable* ht_table ); + +// return 1 if put is successful +// return 0 if put is failed +int hashtable_put ( hashtable* ht_table, int key, int value ); + +// return 1 if a entry is found in hash table +// return 0 if a entry is not found in hash table +// output value is stored in (*value) +int hashtable_get ( hashtable* ht_table, int key, int* value ); + +#endif // __HASHTABLE_H diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..f910f90 --- /dev/null +++ b/src/log.h @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2011 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 __LOG_H +#define __LOG_H + +#include + +#include "sysdeps.h" + +/* define SDB_TRACE to 1 to enable tracing support, or 0 to disable it */ + +#define SDB_TRACE 1 + +/* IMPORTANT: if you change the following list, don't + * forget to update the corresponding 'tags' table in + * the sdb_trace_init() function implemented in sdb.c + */ +typedef enum { + TRACE_SDB = 0, + TRACE_SOCKETS, + TRACE_PACKETS, + TRACE_TRANSPORT, + TRACE_RWX, + TRACE_USB, + TRACE_SYNC, + TRACE_SYSDEPS, + TRACE_JDWP, + TRACE_SERVICES, + TRACE_PROPERTIES, + TRACE_SDKTOOLS +} SdbTrace; + +#if SDB_TRACE + +#if !SDB_HOST +/* + * When running inside the emulator, guest's sdbd can connect to 'sdb-debug' + * qemud service that can display sdb trace messages (on condition that emulator + * has been started with '-debug sdb' option). + */ + +/* Delivers a trace message to the emulator via QEMU pipe. */ +void sdb_qemu_trace(const char* fmt, ...); + +/* Macro to use to send SDB trace messages to the emulator. */ +#define DQ(...) sdb_qemu_trace(__VA_ARGS__) +#else +#define DQ(...) ((void)0) +#endif /* !SDB_HOST */ + + extern int sdb_trace_mask; + extern unsigned char sdb_trace_output_count; + void sdb_trace_init(void); + +# define SDB_TRACING ((sdb_trace_mask & (1 << TRACE_TAG)) != 0) + + /* you must define TRACE_TAG before using this macro */ +# define D(...) \ + do { \ + if (SDB_TRACING) { \ + int save_errno = errno; \ + sdb_mutex_lock(&D_lock); \ + fprintf(stderr, "%s::%s():", \ + __FILE__, __FUNCTION__); \ + errno = save_errno; \ + fprintf(stderr, __VA_ARGS__ ); \ + fflush(stderr); \ + sdb_mutex_unlock(&D_lock); \ + errno = save_errno; \ + } \ + } while (0) +# define DR(...) \ + do { \ + if (SDB_TRACING) { \ + int save_errno = errno; \ + sdb_mutex_lock(&D_lock); \ + errno = save_errno; \ + fprintf(stderr, __VA_ARGS__ ); \ + fflush(stderr); \ + sdb_mutex_unlock(&D_lock); \ + errno = save_errno; \ + } \ + } while (0) +#else +# define D(...) ((void)0) +# define DR(...) ((void)0) +# define SDB_TRACING 0 +#endif + +#endif // __LOG_H diff --git a/src/parameter.h b/src/parameter.h new file mode 100644 index 0000000..c526bb7 --- /dev/null +++ b/src/parameter.h @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2011 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 SRC_PARAMETER_H_ +#define SRC_PARAMETER_H_ + +#include +#include +#include +#include +#include + +typedef enum _datatype { + type_int8, + type_int16, + type_int32, + type_uint8, + type_uint16, + type_uint32, + type_voidp, + type_string, + type_chunk +} datatype; + +typedef struct _sstring { + size_t length; + char* data; +} sstring; + +typedef struct _chunk { + size_t size; // byte size of data + unsigned char* data; // byte array of data +} chunk; + +typedef struct _parameter { + datatype type; + union { + int8_t v_int8; + int16_t v_int16; + int32_t v_int32; + uint8_t v_uint8; + uint16_t v_uint16; + uint32_t v_uint32; + void* v_pointer; + sstring v_string; + chunk v_chunk; + }; +} parameter; + +typedef struct _parameters { + size_t number_of_parameter; + parameter* array_of_parameter; +} parameters; + +#define MAX_OUT_BUF_SIZE 128 + +static __inline__ void make_string_parameter ( parameter* pstring, const char* format, ... ) +{ + va_list args; + char buf[MAX_OUT_BUF_SIZE]; + + va_start ( args, format ); + vsnprintf ( buf, MAX_OUT_BUF_SIZE, format, args ); + va_end ( args ); + + pstring->type = type_string; + pstring->v_string.length = strlen ( buf ); + pstring->v_string.data = ( char* ) malloc ( pstring->v_string.length + 1 ); + strcpy ( pstring->v_string.data, buf ); +} + +static __inline__ void release_parameters ( parameters* param ) +{ + if ( param != NULL ) { + int i; + for ( i = 0; i < param->number_of_parameter; i++ ) { + if ( param->array_of_parameter[i].type == type_string + && param->array_of_parameter[i].v_string.data != NULL ) { + free ( param->array_of_parameter[i].v_string.data ); + } else if ( param->array_of_parameter[i].type == type_chunk + && param->array_of_parameter[i].v_chunk.data != NULL ) { + free ( param->array_of_parameter[i].v_chunk.data ); + } else if ( param->array_of_parameter[i].type == type_voidp + && param->array_of_parameter[i].v_pointer != NULL ) { + free ( param->array_of_parameter[i].v_pointer ); + } + } + if ( param->array_of_parameter != NULL ) { + free ( param->array_of_parameter ); + } + } +} + +#endif /* SRC_PARAMETER_H_ */ diff --git a/src/plugin.c b/src/plugin.c new file mode 100644 index 0000000..7ef796f --- /dev/null +++ b/src/plugin.c @@ -0,0 +1,295 @@ +/* + * Copyright (c) 2011 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 + +#define TRACE_TAG TRACE_SDB +#include "log.h" + +#include "sdb.h" + +#include "hashtable.h" +#include "parameter.h" +#include "plugin.h" +#include "sdbd_plugin.h" + +static void* plugin_handle = NULL; + +PLUGIN_INIT plugin_init_proc = NULL; +PLUGIN_SYNCHRONOUS_CMD_PROC plugin_sync_proc = NULL; +PLUGIN_ASYNCHRONOUS_CMD_PROC plugin_async_proc = NULL; + +hashtable* plugin_cmd_hashtable = NULL; + +// handler of event to be detected by plugin +int plugin_event_handler ( int event_type, parameters* data ) +{ + // TODO : implement event handler + if ( event_type == PLUGIN_EVENT_PWLOCK || event_type == PLUGIN_EVENT_FMMLOCK ) { + if ( data != NULL && data->number_of_parameter == 1 && data->array_of_parameter != NULL + && data->array_of_parameter[0].type == type_int32 ) { + + if ( data->array_of_parameter[0].v_int32 == PLUGIN_RET_ON ) { + // locked + is_pwlocked = 1; + } else { + // unlocked + is_pwlocked = 0; + } + + send_device_status(); + + if ( data != NULL ) { + release_parameters ( data ); + free ( data ); + } + } + } + + return 0; +} + +// register commands that are supported by plugin +static int plugin_register_command ( int command, priority cmd_priority ) +{ + if ( plugin_cmd_hashtable ) { + hashtable_put ( plugin_cmd_hashtable, command, cmd_priority ); + } + return 0; +} + +// return 0 if fail to load sdbd plugin (use default plugin) +// return 1 if succeed to load sdbd plugin and get function pointer of plugin_init +static int load_plugin_not_default() +{ + plugin_init_proc = NULL; + plugin_sync_proc = NULL; + plugin_async_proc = NULL; + + plugin_handle = dlopen ( PLUGIN_PATH, RTLD_NOW ); + if ( plugin_handle == NULL ) { + D ( "failed to dlopen(%s). error: %s\n", PLUGIN_PATH, dlerror() ); + return 0; + } + + plugin_init_proc = dlsym ( plugin_handle, PLUGIN_PROC_NAME_INIT ); + if ( plugin_init_proc == NULL ) { + D ( "failed to get the sdbd plugin init function. error: %s\n", dlerror() ); + dlclose ( plugin_handle ); + plugin_handle = NULL; + return 0; + } + + // if there is no implementation of plugin_sync_proc and plugin_async_proc, + // then use default_plugin_sync_proc and default_plugin_async_proc + plugin_sync_proc = dlsym ( plugin_handle, PLUGIN_PROC_NAME_SYNC_CMD ); + if ( plugin_sync_proc == NULL ) { + plugin_sync_proc = default_plugin_sync_proc; + } + + plugin_async_proc = dlsym ( plugin_handle, PLUGIN_PROC_NAME_ASYNC_CMD ); + if ( plugin_async_proc == NULL ) { + plugin_async_proc = default_plugin_async_proc; + } + + return 1; +} + +void load_sdbd_plugin() +{ + int ret; + + plugin_cmd_hashtable = hashtable_create ( ht_size_31 ); + + ret = load_plugin_not_default(); + if ( ret == 0 ) { + // use default plugin + plugin_init_proc = default_plugin_init; + plugin_sync_proc = default_plugin_sync_proc; + plugin_async_proc = default_plugin_async_proc; + + D ( "using default plugin interface.\n" ); + } else { + D ( "using sdbd plugin interface.(%s)\n", PLUGIN_PATH ); + + plugin_init_proc ( plugin_event_handler, plugin_register_command ); + } + + // use default plugin feature for the commands or events that are not supported by plugin + default_plugin_init ( plugin_event_handler, plugin_register_command ); +} + +void unload_sdbd_plugin() +{ + if ( plugin_cmd_hashtable ) { + hashtable_destroy ( plugin_cmd_hashtable ); + plugin_cmd_hashtable = NULL; + } + + if ( plugin_handle ) { + dlclose ( plugin_handle ); + plugin_handle = NULL; + } +} + +// return 1 if plugin support given command +// return 0 if plugin does not support given command +int is_supported_by_plugin ( int cmd ) +{ + int ret = 0; + + if ( plugin_cmd_hashtable ) { + int value; + ret = hashtable_get ( plugin_cmd_hashtable, cmd, &value ); + } + + return ret; +} + +static int request_sync_cmd ( int cmd, parameters* in, parameters* out ) +{ + int ret, pr; + + out->number_of_parameter = 0; + out->array_of_parameter = NULL; + + ret = hashtable_get ( plugin_cmd_hashtable, cmd, &pr ); + if ( ret == 1 ) { + // supported by plugin + ret = plugin_sync_proc ( cmd, in, out ); + if ( ret == PLUGIN_CMD_NOT_SUPPORT ) { + // not supported by plugin + ret = default_plugin_sync_proc ( cmd, in, out ); + } + } else { + // not supported by plugin + ret = default_plugin_sync_proc ( cmd, in, out ); + } + + return ret; +} + + +// return 1 if succeed to get capability from plugin +// return 0 otherwise +int request_capability_to_plugin ( int cap, char* out_buf, unsigned int out_len ) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 1; + in.array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = cap; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_CAPABILITY, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + strncpy ( out_buf, out.array_of_parameter[0].v_string.data, out_len - 1 ); + out_buf[out_len - 1] = '\0'; + success = 1; + release_parameters ( &out ); + } + + release_parameters ( &in ); + return success; +} + +// return 1 if allowed by plugin (valid) +// return 0 if disallowed by plugin (invalid) +int request_validity_to_plugin ( int cmd, const char* in_buf ) +{ + int success = 0; + int ret; + parameters in, out; + + if ( in_buf != NULL ) { + in.number_of_parameter = 1; + in.array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + in.array_of_parameter[0].type = type_string; + in.array_of_parameter[0].v_string.length = strlen ( in_buf ); + in.array_of_parameter[0].v_string.data = strdup ( in_buf ); + } else { + in.number_of_parameter = 0; + in.array_of_parameter = NULL; + } + + ret = request_sync_cmd ( cmd, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + success = ( out.array_of_parameter[0].v_int32 == PLUGIN_RET_VALID ) ? 1 : 0; + release_parameters ( &out ); + } + + release_parameters ( &in ); + return success; +} + +// return 1 if succeed to convert +// return 0 otherwise +int request_conversion_to_plugin ( int cmd, const char* in_buf, char* out_buf, unsigned int out_len ) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 1; + in.array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + in.array_of_parameter[0].type = type_string; + in.array_of_parameter[0].v_string.length = strlen ( in_buf ); + in.array_of_parameter[0].v_string.data = strdup ( in_buf ); + + ret = request_sync_cmd ( cmd, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + strncpy ( out_buf, out.array_of_parameter[0].v_string.data, out_len - 1 ); + out_buf[out_len - 1] = '\0'; + success = 1; + release_parameters ( &out ); + } + + release_parameters ( &in ); + return success; +} + +// return 1 if locked +// return 0 if unlocked +// return -1 if request failed +int request_lock_state_to_plugin ( int lock_type ) +{ + int result = -1; + int ret; + parameters in, out; + + in.number_of_parameter = 1; + in.array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = lock_type; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_GET_LOCK_STATE, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + if ( out.array_of_parameter[0].v_int32 == PLUGIN_RET_ON ) { + result = 1; + } else { + result = 0; + } + is_pwlocked = result; + release_parameters ( &out ); + } + + release_parameters ( &in ); + + return result; +} diff --git a/src/plugin.h b/src/plugin.h new file mode 100644 index 0000000..a553e95 --- /dev/null +++ b/src/plugin.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2011 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 __PLUGIN_H +#define __PLUGIN_H + +#include "sdbd_plugin.h" + +#define PLUGIN_PATH "/usr/lib/libsdbd_plugin.so" + +int default_plugin_init ( eventfunc event_cb, registerfunc register_func ); +int default_plugin_sync_proc ( int cmd, parameters* in, parameters* out ); +int default_plugin_async_proc ( int cmd, parameters* in, int out_fd ); + +void load_sdbd_plugin(); +void unload_sdbd_plugin(); + +// return 1 if plugin support given command +// return 0 if plugin does not support given command +int is_supported_by_plugin ( int cmd ); + +// return 1 if succeed to get capability from plugin +// return 0 otherwise +int request_capability_to_plugin ( int cap, char* out_buf, unsigned int out_len ); + +// return 1 if allowed by plugin (valid) +// return 0 if disallowed by plugin (invalid) +int request_validity_to_plugin ( int cmd, const char* in_buf ); + +// return 1 if succeed to convert +// return 0 otherwise +int request_conversion_to_plugin ( int cmd, const char* in_buf, char* out_buf, unsigned int out_len ); + +// return 1 if locked +// return 0 if unlocked +// return -1 if request failed +int request_lock_state_to_plugin ( int lock_type ); + +#endif //__PLUGIN_H diff --git a/src/properties.c b/src/properties.c index 1127655..aa14fe7 100644 --- a/src/properties.c +++ b/src/properties.c @@ -27,6 +27,8 @@ //#include "loghack.h" #include "sysdeps.h" #define TRACE_TAG TRACE_PROPERTIES +#include "log.h" + #include "sdb.h" #define HAVE_TIZEN_PROPERTY diff --git a/src/qemu_pipe.h b/src/qemu_pipe.h index 0cb5f93..715476d 100644 --- a/src/qemu_pipe.h +++ b/src/qemu_pipe.h @@ -23,6 +23,7 @@ #include /* for pthread_once() */ #include #include +#include #include #ifndef D diff --git a/src/sdb.c b/src/sdb.c index 0d8cf47..727a7aa 100644 --- a/src/sdb.c +++ b/src/sdb.c @@ -33,6 +33,7 @@ #include #include "sysdeps.h" +#include "log.h" #include "sdb.h" #include "strutils.h" #if !SDB_HOST @@ -40,6 +41,8 @@ #endif #include "utils.h" #include "sdktools.h" +#include "plugin.h" +#include "sdbd_plugin.h" #if !SDB_HOST #include @@ -48,7 +51,6 @@ #include #include #include -#include "utils.h" #define PROC_CMDLINE_PATH "/proc/cmdline" #define USB_SERIAL_PATH "/sys/class/usb_mode/usb0/iSerial" @@ -88,6 +90,7 @@ struct group_info g_default_groups[] = { #define SDB_DEFAULT_GROUPS_CNT ((sizeof(g_default_groups)/sizeof(g_default_groups[0]))-1) int is_init_sdk_userinfo = 0; +int is_pwlocked = 0; // 0 if unlocked, 1 otherwise #if !SDB_HOST SdbdCommandlineArgs sdbd_commandline_args; @@ -134,9 +137,6 @@ int is_container_enabled(void) { } } -void* g_sdbd_plugin_handle = NULL; -SDBD_PLUGIN_CMD_PROC_PTR sdbd_plugin_cmd_proc = NULL; - void handle_sig_term(int sig) { #ifdef SDB_PIDPATH if (access(SDB_PIDPATH, F_OK) == 0) @@ -170,7 +170,7 @@ void fatal_errno(const char *fmt, ...) static int is_enable_sdbd_log() { - return (!strncmp(g_capabilities.log_enable, SDBD_CAP_RET_ENABLED, strlen(SDBD_CAP_RET_ENABLED))); + return (!strncmp(g_capabilities.log_enable, PLUGIN_RET_ENABLED, strlen(PLUGIN_RET_ENABLED))); } int sdb_trace_mask; @@ -398,7 +398,7 @@ static void send_connect(atransport *t) char device_name[256]={0,}; int r = 0; int status = 0; - if (is_pwlocked()) { + if (request_lock_state_to_plugin(LOCKTYPE_PASSWORD) == 1) { status = 1; t->connection_state = CS_PWLOCK; } @@ -425,12 +425,12 @@ static void send_connect(atransport *t) #endif } -static void send_device_status() +void send_device_status() { D("broadcast device status\n"); apacket* cp = get_apacket(); cp->msg.command = A_STAT; - cp->msg.arg0 = is_pwlocked(); + cp->msg.arg0 = is_pwlocked; cp->msg.arg1 = 0; broadcast_transport(cp); @@ -683,7 +683,8 @@ void handle_packet(apacket *p, atransport *t) break; case A_OPEN: /* OPEN(local-id, 0, "destination") */ - if (is_pwlocked() && t->connection_state == CS_PWLOCK) { // in case of already locked before get A_CNXN + if (request_lock_state_to_plugin(LOCKTYPE_PASSWORD) == 1 && t->connection_state == CS_PWLOCK) { + // in case of already locked before get A_CNXN D("open failed due to password locked before get A_CNXN:%d\n", t->connection_state); send_close(0, p->msg.arg0, t); } else { @@ -967,10 +968,8 @@ static void sdb_cleanup(void) // if(required_pid > 0) { // kill(required_pid, SIGKILL); // } - if (g_sdbd_plugin_handle) { - dlclose(g_sdbd_plugin_handle); - g_sdbd_plugin_handle = NULL; - } + + unload_sdbd_plugin(); } void start_logging(void) @@ -1272,40 +1271,6 @@ static void init_drop_privileges() { #endif } -int is_pwlocked(void) { - int pwlock_status = 0; - int pwlock_type = 0; - - if (vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &pwlock_status)) { - pwlock_status = 0; - D("failed to get pw lock status\n"); - } -#ifdef _WEARABLE - D("wearable lock applied\n"); - // for wearable which uses different VCONF key (lock type) - if (vconf_get_int(VCONFKEY_SETAPPL_PRIVACY_LOCK_TYPE_INT, &pwlock_type)) { - pwlock_type = 0; - D("failed to get pw lock type\n"); - } - if ((pwlock_status == VCONFKEY_IDLE_LOCK) && (pwlock_type != SETTING_PRIVACY_LOCK_TYPE_NONE)) { - D("device has been locked\n"); - return 1; // locked! - } -#else - D("mobile lock applied\n"); - // for mobile - if (vconf_get_int(VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT, &pwlock_type)) { - pwlock_type = 0; - D("failed to get pw lock type\n"); - } - if (pwlock_status == VCONFKEY_IDLE_LOCK && ((pwlock_type != SETTING_SCREEN_LOCK_TYPE_NONE) && (pwlock_type != SETTING_SCREEN_LOCK_TYPE_SWIPE))) { - D("device has been locked\n"); - return 1; // locked! - } -#endif - return 0; // unlocked! -} - int should_drop_privileges() { if (rootshell_mode == 1) { // if root, then don't drop return 0; @@ -1313,48 +1278,6 @@ int should_drop_privileges() { return 1; } -static void pwlock_cb(keynode_t *key, void* data) { - D("pwlock callback is issued\n"); - send_device_status(); -} - -void register_pwlock_cb() { - int ret = vconf_notify_key_changed(VCONFKEY_IDLE_LOCK_STATE, pwlock_cb, NULL); - if(ret != 0) { - D("cannot register vconf callback.\n"); - return; - } - D("registered vconf callback\n"); -} - -void unregister_pwlock_cb() { - int ret = vconf_ignore_key_changed(VCONFKEY_IDLE_LOCK_STATE, pwlock_cb); - if(ret != 0) { - D("cannot unregister vconf callback.\n"); - return; - } - D("unregistered vconf callback\n"); -} - -static void *pwlock_thread(void *x) { - GMainLoop *loop; - loop = g_main_loop_new(NULL, FALSE); - register_pwlock_cb(); - g_main_loop_run(loop); - g_main_loop_unref(loop); - unregister_pwlock_cb(); - return 0; -} - -void create_pwlock_thread() { - sdb_thread_t t; - if(sdb_thread_create( &t, pwlock_thread, NULL)) { - D("cannot create_pwlock_thread.\n"); - return; - } - D("created pwlock_thread\n"); -} - #include #include #include @@ -1563,237 +1486,6 @@ int set_sdk_user_privileges() { return 0; } -/* default plugin proc */ -static int get_plugin_capability(const char* in_buf, sdbd_plugin_param out) { - int ret = SDBD_PLUGIN_RET_NOT_SUPPORT; - - if (in_buf == NULL) { - D("Invalid argument\n"); - return SDBD_PLUGIN_RET_FAIL; - } - - if (SDBD_CMP_CAP(in_buf, SECURE)) { - snprintf(out.data, out.len, "%s", SDBD_CAP_RET_DISABLED); - ret = SDBD_PLUGIN_RET_SUCCESS; - } else if (SDBD_CMP_CAP(in_buf, INTER_SHELL)) { - snprintf(out.data, out.len, "%s", SDBD_CAP_RET_ENABLED); - ret = SDBD_PLUGIN_RET_SUCCESS; - } else if (SDBD_CMP_CAP(in_buf, FILESYNC)) { - // - push : SDBD_CAP_RET_PUSH - // - pull : SDBD_CAP_RET_PULL - // - both : SDBD_CAP_RET_PUSHPULL - // - disabled : SDBD_CAP_RET_DISABLED - snprintf(out.data, out.len, "%s", SDBD_CAP_RET_PUSHPULL); - ret = SDBD_PLUGIN_RET_SUCCESS; - } else if (SDBD_CMP_CAP(in_buf, USBPROTO)) { - if (is_emulator()) { - snprintf(out.data, out.len, "%s", SDBD_CAP_RET_DISABLED); - } else { - snprintf(out.data, out.len, "%s", SDBD_CAP_RET_ENABLED); - } - ret = SDBD_PLUGIN_RET_SUCCESS; - } else if (SDBD_CMP_CAP(in_buf, SOCKPROTO)) { - if (is_emulator()) { - snprintf(out.data, out.len, "%s", SDBD_CAP_RET_ENABLED); - } else { - snprintf(out.data, out.len, "%s", SDBD_CAP_RET_ENABLED); - } - ret = SDBD_PLUGIN_RET_SUCCESS; - } else if (SDBD_CMP_CAP(in_buf, ROOTONOFF)) { - if (access("/bin/su", F_OK) == 0) { - snprintf(out.data, out.len, "%s", SDBD_CAP_RET_ENABLED); - } else { - snprintf(out.data, out.len, "%s", SDBD_CAP_RET_DISABLED); - } - ret = SDBD_PLUGIN_RET_SUCCESS; - } else if (SDBD_CMP_CAP(in_buf, CANLAUNCH)) { - snprintf(out.data, out.len, "%s", UNKNOWN); - ret = SDBD_PLUGIN_RET_SUCCESS; - } else if (SDBD_CMP_CAP(in_buf, PLUGIN_VER)) { - snprintf(out.data, out.len, "%s", UNKNOWN); - ret = SDBD_PLUGIN_RET_SUCCESS; - } else if (SDBD_CMP_CAP(in_buf, PRODUCT_VER)) { - snprintf(out.data, out.len, "%s", UNKNOWN); - ret = SDBD_PLUGIN_RET_SUCCESS; - } else if (SDBD_CMP_CAP(in_buf, LOG_ENABLE)) { - snprintf(out.data, out.len, "%s", SDBD_CAP_RET_DISABLED); - ret = SDBD_PLUGIN_RET_SUCCESS; - } else if (SDBD_CMP_CAP(in_buf, LOG_PATH)) { - snprintf(out.data, out.len, "%s", "/tmp"); - ret = SDBD_PLUGIN_RET_SUCCESS; - } - - return ret; -} - -static int verify_shell_cmd(const char* in_buf, sdbd_plugin_param out) { - int ret = SDBD_PLUGIN_RET_FAIL; - - if (in_buf == NULL) { - D("Invalid argument\n"); - return SDBD_PLUGIN_RET_FAIL; - } - - D("shell command : %s\n", in_buf); - - snprintf(out.data, out.len, "%s", SDBD_RET_VALID); - ret = SDBD_PLUGIN_RET_SUCCESS; - - return ret; -} - -static int convert_shell_cmd(const char* in_buf, sdbd_plugin_param out) { - int ret = SDBD_PLUGIN_RET_FAIL; - - if (in_buf == NULL) { - D("Invalid argument\n"); - return SDBD_PLUGIN_RET_FAIL; - } - - snprintf(out.data, out.len, "%s", in_buf); - ret = SDBD_PLUGIN_RET_SUCCESS; - - return ret; -} - -static int verify_peer_ip(const char* in_buf, sdbd_plugin_param out) { - int ret = SDBD_PLUGIN_RET_FAIL; - - if (in_buf == NULL) { - D("Invalid argument\n"); - return SDBD_PLUGIN_RET_FAIL; - } - - D("peer ip : %s\n", in_buf); - - snprintf(out.data, out.len, "%s", SDBD_RET_VALID); - ret = SDBD_PLUGIN_RET_SUCCESS; - - return ret; -} - -static int verify_sdbd_launch(const char* in_buf, sdbd_plugin_param out) { - snprintf(out.data, out.len, "%s", SDBD_RET_VALID); - return SDBD_PLUGIN_RET_SUCCESS; -} - -static int verify_root_cmd(const char* in_buf, sdbd_plugin_param out) { - int ret = SDBD_PLUGIN_RET_FAIL; - - if (in_buf == NULL) { - D("Invalid argument\n"); - return SDBD_PLUGIN_RET_FAIL; - } - - D("shell command : %s\n", in_buf); - - if (verify_root_commands(in_buf)) { - snprintf(out.data, out.len, "%s", SDBD_RET_VALID); - } else { - snprintf(out.data, out.len, "%s", SDBD_RET_INVALID); - } - ret = SDBD_PLUGIN_RET_SUCCESS; - - return ret; -} - -static int get_shell_env(const char* in_buf, sdbd_plugin_param out) { - snprintf(out.data, out.len, "%s", ""); - return SDBD_PLUGIN_RET_SUCCESS; -} - -int default_cmd_proc(const char* cmd, - const char* in_buf, sdbd_plugin_param out) { - int ret = SDBD_PLUGIN_RET_NOT_SUPPORT; - - /* Check the arguments */ - if (cmd == NULL || out.data == NULL) { - D("Invalid argument\n"); - return SDBD_PLUGIN_RET_FAIL; - } - - D("handle the command : %s\n", cmd); - - /* Handle the request from sdbd */ - if (SDBD_CMP_CMD(cmd, PLUGIN_CAP)) { - ret = get_plugin_capability(in_buf, out); - } else if (SDBD_CMP_CMD(cmd, VERIFY_SHELLCMD)) { - ret = verify_shell_cmd(in_buf, out); - } else if (SDBD_CMP_CMD(cmd, CONV_SHELLCMD)) { - ret = convert_shell_cmd(in_buf, out); - } else if (SDBD_CMP_CMD(cmd, VERIFY_PEERIP)) { - ret = verify_peer_ip(in_buf, out); - } else if (SDBD_CMP_CMD(cmd, VERIFY_LAUNCH)) { - ret = verify_sdbd_launch(in_buf, out); - } else if (SDBD_CMP_CMD(cmd, VERIFY_ROOTCMD)) { - ret = verify_root_cmd(in_buf, out); - } else if (SDBD_CMP_CMD(cmd, SHELL_ENVVAR)) { - ret = get_shell_env(in_buf, out); - } else { - D("Not supported command : %s\n", cmd); - ret = SDBD_PLUGIN_RET_NOT_SUPPORT; - } - - return ret; -} - -int request_plugin_cmd(const char* cmd, const char* in_buf, - char *out_buf, unsigned int out_len) -{ - int ret = SDBD_PLUGIN_RET_FAIL; - sdbd_plugin_param out; - - if (out_len > SDBD_PLUGIN_OUTBUF_MAX) { - D("invalid parameter : %s\n", cmd); - return 0; - } - - out.data = out_buf; - out.len = out_len; - - ret = sdbd_plugin_cmd_proc(cmd, in_buf, out); - if (ret == SDBD_PLUGIN_RET_FAIL) { - D("failed to request : %s\n", cmd); - return 0; - } - if (ret == SDBD_PLUGIN_RET_NOT_SUPPORT) { - // retry in default handler - ret = default_cmd_proc(cmd, in_buf, out); - if (ret == SDBD_PLUGIN_RET_FAIL) { - D("failed to request : %s\n", cmd); - return 0; - } - } - - // add null character. - out_buf[out_len-1] = '\0'; - D("return value: %s\n", out_buf); - - return 1; -} - -static void load_sdbd_plugin() { - sdbd_plugin_cmd_proc = NULL; - - g_sdbd_plugin_handle = dlopen(SDBD_PLUGIN_PATH, RTLD_NOW); - if (!g_sdbd_plugin_handle) { - D("failed to dlopen(%s). error: %s\n", SDBD_PLUGIN_PATH, dlerror()); - sdbd_plugin_cmd_proc = default_cmd_proc; - return; - } - - sdbd_plugin_cmd_proc = dlsym(g_sdbd_plugin_handle, SDBD_PLUGIN_INTF); - if (!sdbd_plugin_cmd_proc) { - D("failed to get the sdbd plugin interface. error: %s\n", dlerror()); - dlclose(g_sdbd_plugin_handle); - g_sdbd_plugin_handle = NULL; - sdbd_plugin_cmd_proc = default_cmd_proc; - return; - } - - D("using sdbd plugin interface.(%s)\n", SDBD_PLUGIN_PATH); -} - #define SDB_PW_GR_DEFAULT_SIZE (16*1024) static long get_passwd_bufsize() { long bufsize = 0; @@ -1921,28 +1613,9 @@ static void init_sdk_requirements() { if (is_emulator()) { register_bootdone_cb(); } - - create_pwlock_thread(); } #endif /* !SDB_HOST */ -int request_plugin_verification(const char* cmd, const char* in_buf) { - char out_buf[32] = {0,}; - - if(!request_plugin_cmd(cmd, in_buf, out_buf, sizeof(out_buf))) { - D("failed to request plugin command. : %s\n", SDBD_CMD_VERIFY_LAUNCH); - return 0; - } - - if (strlen(out_buf) == 7 && !strncmp(out_buf, SDBD_RET_INVALID, 7)) { - D("[%s] is NOT verified.\n", cmd); - return 0; - } - - D("[%s] is verified.\n", cmd); - return 1; -} - static void init_capabilities(void) { int ret = -1; char *value = NULL; @@ -1965,60 +1638,54 @@ static void init_capabilities(void) { // Secure protocol support - if(!request_plugin_cmd(SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_SECURE, - g_capabilities.secure_protocol, + if(!request_capability_to_plugin(CAPABILITY_SECURE, g_capabilities.secure_protocol, sizeof(g_capabilities.secure_protocol))) { - D("failed to request. (%s:%s) \n", SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_SECURE); + D("failed to request. (%d:%d) \n", PLUGIN_SYNC_CMD_CAPABILITY, CAPABILITY_SECURE); snprintf(g_capabilities.secure_protocol, sizeof(g_capabilities.secure_protocol), "%s", DISABLED); } // Interactive shell support - if(!request_plugin_cmd(SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_INTER_SHELL, - g_capabilities.intershell_support, + if(!request_capability_to_plugin(CAPABILITY_INTER_SHELL, g_capabilities.intershell_support, sizeof(g_capabilities.intershell_support))) { - D("failed to request. (%s:%s) \n", SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_INTER_SHELL); + D("failed to request. (%d:%d) \n", PLUGIN_SYNC_CMD_CAPABILITY, CAPABILITY_INTER_SHELL); snprintf(g_capabilities.intershell_support, sizeof(g_capabilities.intershell_support), "%s", DISABLED); } // File push/pull support - if(!request_plugin_cmd(SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_FILESYNC, - g_capabilities.filesync_support, + if(!request_capability_to_plugin(CAPABILITY_FILESYNC, g_capabilities.filesync_support, sizeof(g_capabilities.filesync_support))) { - D("failed to request. (%s:%s) \n", SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_FILESYNC); + D("failed to request. (%d:%d) \n", PLUGIN_SYNC_CMD_CAPABILITY, CAPABILITY_FILESYNC); snprintf(g_capabilities.filesync_support, sizeof(g_capabilities.filesync_support), "%s", DISABLED); } // USB protocol support - if(!request_plugin_cmd(SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_USBPROTO, - g_capabilities.usbproto_support, + if(!request_capability_to_plugin(CAPABILITY_USB_PROTOCOL, g_capabilities.usbproto_support, sizeof(g_capabilities.usbproto_support))) { - D("failed to request. (%s:%s) \n", SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_USBPROTO); + D("failed to request. (%d:%d) \n", PLUGIN_SYNC_CMD_CAPABILITY, CAPABILITY_USB_PROTOCOL); snprintf(g_capabilities.usbproto_support, sizeof(g_capabilities.usbproto_support), "%s", DISABLED); } // Socket protocol support - if(!request_plugin_cmd(SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_SOCKPROTO, - g_capabilities.sockproto_support, + if(!request_capability_to_plugin(CAPABILITY_SOCK_PROTOCOL, g_capabilities.sockproto_support, sizeof(g_capabilities.sockproto_support))) { - D("failed to request. (%s:%s) \n", SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_SOCKPROTO); + D("failed to request. (%d:%d) \n", PLUGIN_SYNC_CMD_CAPABILITY, CAPABILITY_SOCK_PROTOCOL); snprintf(g_capabilities.sockproto_support, sizeof(g_capabilities.sockproto_support), "%s", DISABLED); } // Root command support - if(!request_plugin_cmd(SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_ROOTONOFF, - g_capabilities.rootonoff_support, + if(!request_capability_to_plugin(CAPABILITY_ROOT_ONOFF, g_capabilities.rootonoff_support, sizeof(g_capabilities.rootonoff_support))) { - D("failed to request. (%s:%s) \n", SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_ROOTONOFF); + D("failed to request. (%d:%d) \n", PLUGIN_SYNC_CMD_CAPABILITY, CAPABILITY_ROOT_ONOFF); snprintf(g_capabilities.rootonoff_support, sizeof(g_capabilities.rootonoff_support), "%s", DISABLED); } @@ -2112,10 +1779,9 @@ static void init_capabilities(void) { // Product version - if(!request_plugin_cmd(SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_PRODUCT_VER, - g_capabilities.product_version, + if(!request_capability_to_plugin(CAPABILITY_PRODUCT_VER, g_capabilities.product_version, sizeof(g_capabilities.product_version))) { - D("failed to request. (%s:%s) \n", SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_PRODUCT_VER); + D("failed to request. (%d:%d) \n", PLUGIN_SYNC_CMD_CAPABILITY, CAPABILITY_PRODUCT_VER); snprintf(g_capabilities.product_version, sizeof(g_capabilities.product_version), "%s", UNKNOWN); } @@ -2127,29 +1793,26 @@ static void init_capabilities(void) { // Sdbd plugin version - if(!request_plugin_cmd(SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_PLUGIN_VER, - g_capabilities.sdbd_plugin_version, + if(!request_capability_to_plugin(CAPABILITY_PLUGIN_VER, g_capabilities.sdbd_plugin_version, sizeof(g_capabilities.sdbd_plugin_version))) { - D("failed to request. (%s:%s) \n", SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_PLUGIN_VER); + D("failed to request. (%d:%d) \n", PLUGIN_SYNC_CMD_CAPABILITY, CAPABILITY_PLUGIN_VER); snprintf(g_capabilities.sdbd_plugin_version, sizeof(g_capabilities.sdbd_plugin_version), "%s", UNKNOWN); } // sdbd log enable - if(!request_plugin_cmd(SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_LOG_ENABLE, - g_capabilities.log_enable, + if(!request_capability_to_plugin(CAPABILITY_LOG_ENABLE, g_capabilities.log_enable, sizeof(g_capabilities.log_enable))) { - D("failed to request. (%s:%s) \n", SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_LOG_ENABLE); + D("failed to request. (%d:%d) \n", PLUGIN_SYNC_CMD_CAPABILITY, CAPABILITY_LOG_ENABLE); snprintf(g_capabilities.log_enable, sizeof(g_capabilities.log_enable), "%s", DISABLED); } - // sdbd log path - if(!request_plugin_cmd(SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_LOG_PATH, - g_capabilities.log_path, + // sdbd log path + if(!request_capability_to_plugin(CAPABILITY_LOG_PATH, g_capabilities.log_path, sizeof(g_capabilities.log_path))) { - D("failed to request. (%s:%s) \n", SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_LOG_PATH); + D("failed to request. (%d:%d) \n", PLUGIN_SYNC_CMD_CAPABILITY, CAPABILITY_LOG_PATH); snprintf(g_capabilities.log_path, sizeof(g_capabilities.log_path), "%s", UNKNOWN); } @@ -2162,12 +1825,12 @@ static void init_capabilities(void) { static int is_support_usbproto() { - return (!strncmp(g_capabilities.usbproto_support, SDBD_CAP_RET_ENABLED, strlen(SDBD_CAP_RET_ENABLED))); + return (!strncmp(g_capabilities.usbproto_support, PLUGIN_RET_ENABLED, strlen(PLUGIN_RET_ENABLED))); } static int is_support_sockproto() { - return (!strncmp(g_capabilities.sockproto_support, SDBD_CAP_RET_ENABLED, strlen(SDBD_CAP_RET_ENABLED))); + return (!strncmp(g_capabilities.sockproto_support, PLUGIN_RET_ENABLED, strlen(PLUGIN_RET_ENABLED))); } #define EMULATOR_MODEL_NAME "Emulator" @@ -2198,6 +1861,7 @@ int sdb_main(int is_daemon, int server_port) check_emulator_or_device(); load_sdbd_plugin(); + init_capabilities(); sdb_trace_init(); @@ -2205,7 +1869,7 @@ int sdb_main(int is_daemon, int server_port) init_drop_privileges(); init_sdk_requirements(); - if (!request_plugin_verification(SDBD_CMD_VERIFY_LAUNCH, NULL)) { + if (!request_validity_to_plugin(PLUGIN_SYNC_CMD_VERIFY_LAUNCH, NULL)) { D("sdbd should be launched in develop mode.\n"); return -1; } @@ -2713,3 +2377,4 @@ int main(int argc, char **argv) return sdb_main(0, DEFAULT_SDB_PORT); #endif } + diff --git a/src/sdb.h b/src/sdb.h index b3b78f3..5d7bd94 100644 --- a/src/sdb.h +++ b/src/sdb.h @@ -23,7 +23,6 @@ #include "transport.h" /* readx(), writex() */ #include "fdevent.h" -#include "sdbd_plugin.h" #if !SDB_HOST #include "commandline_sdbd.h" #endif @@ -275,13 +274,6 @@ typedef struct platform_capabilities } pcap; extern pcap g_capabilities; -#define SDBD_PLUGIN_PATH "/usr/lib/libsdbd_plugin.so" -#define SDBD_PLUGIN_INTF "sdbd_plugin_cmd_proc" -typedef int (*SDBD_PLUGIN_CMD_PROC_PTR)(const char*, const char*, sdbd_plugin_param); -extern SDBD_PLUGIN_CMD_PROC_PTR sdbd_plugin_cmd_proc; -int request_plugin_cmd(const char* cmd, const char* in_buf, char *out_buf, unsigned int out_len); -int request_plugin_verification(const char* cmd, const char* in_buf); - void print_packet(const char *label, apacket *p); asocket *find_local_socket(unsigned id); @@ -384,6 +376,9 @@ char * get_log_file_path(const char * log_name); extern int rootshell_mode; // 0: sdk user, 1: root extern int booting_done; // 0: platform booting is in progess 1: platform booting is done +// 1 if locked, 0 if unlocked +extern int is_pwlocked; + // This is the users and groups config for the platform #define SID_ROOT 0 /* traditional unix root user */ @@ -399,10 +394,10 @@ extern char* g_sdk_home_dir; extern char* g_sdk_home_dir_env; #endif -int is_pwlocked(void); int should_drop_privileges(void); int set_sdk_user_privileges(); void set_root_privileges(); +void send_device_status(); int get_emulator_forward_port(void); int get_emulator_name(char str[], int str_size); @@ -417,86 +412,6 @@ void put_apacket(apacket *p); int check_header(apacket *p); int check_data(apacket *p); -/* define SDB_TRACE to 1 to enable tracing support, or 0 to disable it */ - -#define SDB_TRACE 1 - -/* IMPORTANT: if you change the following list, don't - * forget to update the corresponding 'tags' table in - * the sdb_trace_init() function implemented in sdb.c - */ -typedef enum { - TRACE_SDB = 0, - TRACE_SOCKETS, - TRACE_PACKETS, - TRACE_TRANSPORT, - TRACE_RWX, - TRACE_USB, - TRACE_SYNC, - TRACE_SYSDEPS, - TRACE_JDWP, - TRACE_SERVICES, - TRACE_PROPERTIES, - TRACE_SDKTOOLS -} SdbTrace; - -#if SDB_TRACE - -#if !SDB_HOST -/* - * When running inside the emulator, guest's sdbd can connect to 'sdb-debug' - * qemud service that can display sdb trace messages (on condition that emulator - * has been started with '-debug sdb' option). - */ - -/* Delivers a trace message to the emulator via QEMU pipe. */ -void sdb_qemu_trace(const char* fmt, ...); -/* Macro to use to send SDB trace messages to the emulator. */ -#define DQ(...) sdb_qemu_trace(__VA_ARGS__) -#else -#define DQ(...) ((void)0) -#endif /* !SDB_HOST */ - - extern int sdb_trace_mask; - extern unsigned char sdb_trace_output_count; - void sdb_trace_init(void); - -# define SDB_TRACING ((sdb_trace_mask & (1 << TRACE_TAG)) != 0) - - /* you must define TRACE_TAG before using this macro */ -# define D(...) \ - do { \ - if (SDB_TRACING) { \ - int save_errno = errno; \ - sdb_mutex_lock(&D_lock); \ - fprintf(stderr, "%s::%s():", \ - __FILE__, __FUNCTION__); \ - errno = save_errno; \ - fprintf(stderr, __VA_ARGS__ ); \ - fflush(stderr); \ - sdb_mutex_unlock(&D_lock); \ - errno = save_errno; \ - } \ - } while (0) -# define DR(...) \ - do { \ - if (SDB_TRACING) { \ - int save_errno = errno; \ - sdb_mutex_lock(&D_lock); \ - errno = save_errno; \ - fprintf(stderr, __VA_ARGS__ ); \ - fflush(stderr); \ - sdb_mutex_unlock(&D_lock); \ - errno = save_errno; \ - } \ - } while (0) -#else -# define D(...) ((void)0) -# define DR(...) ((void)0) -# define SDB_TRACING 0 -#endif - - #if !TRACE_PACKETS #define print_packet(tag,p) do {} while (0) #endif @@ -586,6 +501,7 @@ extern SdbdCommandlineArgs sdbd_commandline_args; #endif #define CHUNK_SIZE (64*1024) +#define SDBD_SHELL_CMD_MAX 4096 int sendfailmsg(int fd, const char *reason); int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s); diff --git a/src/sdbd_plugin.h b/src/sdbd_plugin.h index dce56e1..45f2be2 100644 --- a/src/sdbd_plugin.h +++ b/src/sdbd_plugin.h @@ -17,70 +17,102 @@ #ifndef __SDBD_PLUGIN_H #define __SDBD_PLUGIN_H -#include - -/* plugin commands */ -#define SDBD_CMD_PLUGIN_CAP "plugin_capability" -#define SDBD_CMD_VERIFY_SHELLCMD "verify_shell_cmd" -#define SDBD_CMD_CONV_SHELLCMD "convert_shell_cmd" -#define SDBD_CMD_VERIFY_PEERIP "verify_peer_ip" -#define SDBD_CMD_VERIFY_LAUNCH "verify_sdbd_launch" -#define SDBD_CMD_VERIFY_ROOTCMD "verify_root_cmd" -#define SDBD_CMD_SHELL_ENVVAR "sdbd_shell_env_var" - -/* plugin capabilities */ -#define SDBD_CAP_TYPE_SECURE "secure_protocol_support" -#define SDBD_CAP_TYPE_INTER_SHELL "interactive_shell_support" -#define SDBD_CAP_TYPE_FILESYNC "file_sync_support" -#define SDBD_CAP_TYPE_USBPROTO "usb_protocol_support" -#define SDBD_CAP_TYPE_SOCKPROTO "socket_protocol_support" -#define SDBD_CAP_TYPE_ROOTONOFF "root_onoff_support" -#define SDBD_CAP_TYPE_CANLAUNCH "can_launch_target" -#define SDBD_CAP_TYPE_PLUGIN_VER "sdbd_plugin_version" -#define SDBD_CAP_TYPE_PRODUCT_VER "product_version" -#define SDBD_CAP_TYPE_LOG_ENABLE "sdbd_log_enable" -#define SDBD_CAP_TYPE_LOG_PATH "sdbd_log_path" - -/* capability return string */ -#define SDBD_CAP_RET_ENABLED "enabled" -#define SDBD_CAP_RET_DISABLED "disabled" -#define SDBD_CAP_RET_PUSH "push" -#define SDBD_CAP_RET_PULL "pull" -#define SDBD_CAP_RET_PUSHPULL "pushpull" - -/* verification return string */ -#define SDBD_RET_VALID "valid" -#define SDBD_RET_INVALID "invalid" - -/* proc interface return value */ -#define SDBD_PLUGIN_RET_SUCCESS (0) -#define SDBD_PLUGIN_RET_FAIL (-1) -#define SDBD_PLUGIN_RET_NOT_SUPPORT (-2) - -/* utility macro */ -#define SDBD_CMP_CMD(cmd, type) \ - ((strlen(cmd) == strlen(SDBD_CMD_##type) \ - && !strncmp(cmd, SDBD_CMD_##type, strlen(cmd)))?1:0) - -#define SDBD_CMP_CAP(cap, type) \ - ((strlen(cap) == (strlen(SDBD_CAP_TYPE_##type)) \ - && !strncmp(cap, SDBD_CAP_TYPE_##type, strlen(cap)))?1:0) - -/* out parameter structure */ -#define SDBD_SHELL_CMD_MAX 4096 -#define SDBD_PLUGIN_OUTBUF_MAX 4096 -typedef struct sdbd_plugin_param { - unsigned int len; - char *data; -} sdbd_plugin_param; - -/* log system */ -// 1. set the environment value. : SDB_TRACE=all -// 2. restart the sdbd deamon. -// 3. log is output to the /tmp/sdbd-[date].txt -#define SDBD_PLUGIN_LOG(...) \ - fprintf(stderr, "%s::%s():", \ - __FILE__, __FUNCTION__); \ +#include "parameter.h" + +// =========================================================================== +// command && event definition +// =========================================================================== + +// synchronous command +#define PLUGIN_SYNC_CMD_CAPABILITY 1000 +#define PLUGIN_SYNC_CMD_VERIFY_SHELLCMD 1001 +#define PLUGIN_SYNC_CMD_CONVERT_SHELLCMD 1002 +#define PLUGIN_SYNC_CMD_VERIFY_PEERIP 1003 +#define PLUGIN_SYNC_CMD_VERIFY_LAUNCH 1004 +#define PLUGIN_SYNC_CMD_VERIFY_ROOTCMD 1005 +#define PLUGIN_SYNC_CMD_AUTH_SUPPORT 1006 +#define PLUGIN_SYNC_CMD_AUTH_GET_KEY_FILEPATHS 1007 +#define PLUGIN_SYNC_CMD_GET_LOCK_STATE 1008 + +// asynchronous command +#define PLUGIN_ASYNC_CMD_AUTH_CONFIRM_PUBLIC 2000 + +// event +#define PLUGIN_EVENT_PWLOCK 3000 +#define PLUGIN_EVENT_FMMLOCK 3001 + +// message +#define PLUGIN_MESSAGE_CONFIRM_PUBLIC 4000 + +// ============================================================================== +// capability definition +// ============================================================================== +#define CAPABILITY_SECURE 10000 +#define CAPABILITY_INTER_SHELL 10001 +#define CAPABILITY_FILESYNC 10002 +#define CAPABILITY_USB_PROTOCOL 10003 +#define CAPABILITY_SOCK_PROTOCOL 10004 +#define CAPABILITY_ROOT_ONOFF 10005 +#define CAPABILITY_CAN_LAUNCH 10006 +#define CAPABILITY_PLUGIN_VER 10007 +#define CAPABILITY_PRODUCT_VER 10008 +#define CAPABILITY_LOG_ENABLE 10009 +#define CAPABILITY_LOG_PATH 10010 + +// =============================================================================== +// priority definition +// =============================================================================== +typedef enum _priority { + PRIORITY_VERYLOW, + PRIORITY_LOW, + PRIORITY_NORMAL, + PRIORITY_HIGH, + PRIORITY_VERYHIGH +} priority; + +// =============================================================================== +// misc. +// =============================================================================== +#define LOCKTYPE_PASSWORD 20000 +#define LOCKTYPE_FMM 20001 + +// =============================================================================== +// return value definition +// =============================================================================== +#define PLUGIN_RET_ON 10 +#define PLUGIN_RET_OFF 11 +#define PLUGIN_RET_VALID 12 +#define PLUGIN_RET_INVALID 13 +#define PLUGIN_CMD_SUCCESS 101 +#define PLUGIN_CMD_FAIL 102 +#define PLUGIN_CMD_NOT_SUPPORT 103 + +#define PLUGIN_RET_UNKNOWN "unknown" +#define PLUGIN_RET_ENABLED "enabled" +#define PLUGIN_RET_DISABLED "disabled" +#define PLUGIN_RET_PUSH "push" +#define PLUGIN_RET_PULL "pull" +#define PLUGIN_RET_PUSHPULL "pushpull" + +// ================================================================================ +// callback and interface function definition +// ================================================================================ + +// definition of callback functions (implemented by sdbd) +typedef int ( *eventfunc ) ( int event_type, parameters* data ); +typedef int ( *registerfunc ) ( int command, priority cmd_priority ); + +// definition of interface functions (implemented by plugin) +#define PLUGIN_PROC_NAME_INIT "plugin_init" +#define PLUGIN_PROC_NAME_SYNC_CMD "plugin_synchronous_cmd_proc" +#define PLUGIN_PROC_NAME_ASYNC_CMD "plugin_asynchronous_cmd_proc" + +typedef int ( *PLUGIN_INIT ) ( eventfunc event_func_ptr, registerfunc register_cmd_func_ptr ); +typedef int ( *PLUGIN_SYNCHRONOUS_CMD_PROC ) ( int cmd, parameters* in, parameters* out ); +typedef int ( *PLUGIN_ASYNCHRONOUS_CMD_PROC ) ( int cmd, parameters* in, int out_fd ); + +#define PLUGIN_LOG(...) \ + fprintf(stderr, "%s::%s():", __FILE__, __FUNCTION__); \ fprintf(stderr, __VA_ARGS__); -#endif +#endif // __SDBD_PLUGIN_H diff --git a/src/sdktools.c b/src/sdktools.c index 7780101..ac7d21e 100644 --- a/src/sdktools.c +++ b/src/sdktools.c @@ -12,6 +12,8 @@ #include "sdktools.h" #define TRACE_TAG TRACE_SERVICES +#include "log.h" + #include "sdb.h" #include "sdktools.h" #include "strutils.h" diff --git a/src/services.c b/src/services.c index 2478680..22b9dda 100644 --- a/src/services.c +++ b/src/services.c @@ -14,8 +14,8 @@ * limitations under the License. */ -#include #include +#include #include #include #include @@ -24,6 +24,8 @@ #include "sysdeps.h" #define TRACE_TAG TRACE_SERVICES +#include "log.h" + #include "sdb.h" #include "file_sync_service.h" @@ -49,6 +51,9 @@ #include #include +#include "sdbd_plugin.h" +#include "plugin.h" + typedef struct stinfo stinfo; struct stinfo { @@ -89,7 +94,7 @@ static void dns_service(int fd, void *cookie) static int is_support_interactive_shell() { - return (!strncmp(g_capabilities.intershell_support, SDBD_CAP_RET_ENABLED, strlen(SDBD_CAP_RET_ENABLED))); + return (!strncmp(g_capabilities.intershell_support, PLUGIN_RET_ENABLED, strlen(PLUGIN_RET_ENABLED))); } #if 0 @@ -174,7 +179,7 @@ void restart_tcp_service(int fd, void *cookie) static int is_support_rootonoff() { - return (!strncmp(g_capabilities.rootonoff_support, SDBD_CAP_RET_ENABLED, strlen(SDBD_CAP_RET_ENABLED))); + return (!strncmp(g_capabilities.rootonoff_support, PLUGIN_RET_ENABLED, strlen(PLUGIN_RET_ENABLED))); } void rootshell_service(int fd, void *cookie) @@ -489,7 +494,7 @@ static int create_subprocess(const char *cmd, pid_t *pid, char * const argv[], c } if (should_drop_privileges()) { - if (argv[2] != NULL && getuid() == 0 && request_plugin_verification(SDBD_CMD_VERIFY_ROOTCMD, argv[2])) { + if (argv[2] != NULL && getuid() == 0 && request_validity_to_plugin(PLUGIN_SYNC_CMD_VERIFY_ROOTCMD, argv[2])) { // do nothing D("sdb: executes root commands!!:%s\n", argv[2]); } else { @@ -664,7 +669,7 @@ static int create_subproc_thread(const char *name, int lines, int columns) if(name) { // in case of shell execution directly // Check the shell command validation. - if (!request_plugin_verification(SDBD_CMD_VERIFY_SHELLCMD, name)) { + if (!request_validity_to_plugin(PLUGIN_SYNC_CMD_VERIFY_SHELLCMD, name)) { D("This shell command is invalid. (%s)\n", name); return -1; } @@ -678,7 +683,7 @@ static int create_subproc_thread(const char *name, int lines, int columns) } memset(new_cmd, 0, SDBD_SHELL_CMD_MAX); - if(!request_plugin_cmd(SDBD_CMD_CONV_SHELLCMD, name, new_cmd, SDBD_SHELL_CMD_MAX)) { + if(!request_conversion_to_plugin(PLUGIN_SYNC_CMD_CONVERT_SHELLCMD, name, new_cmd, SDBD_SHELL_CMD_MAX)) { D("Failed to convert the shell command. (%s)\n", name); free(new_cmd); return -1; diff --git a/src/sockets.c b/src/sockets.c index 4f3ec83..574820a 100644 --- a/src/sockets.c +++ b/src/sockets.c @@ -24,6 +24,8 @@ #include "sysdeps.h" #define TRACE_TAG TRACE_SOCKETS +#include "log.h" + #include "sdb.h" #include "strutils.h" diff --git a/src/transport.c b/src/transport.c index 2083fbc..7e2af39 100644 --- a/src/transport.c +++ b/src/transport.c @@ -23,6 +23,8 @@ #include "sysdeps.h" #define TRACE_TAG TRACE_TRANSPORT +#include "log.h" + #include "sdb.h" static void transport_unref(atransport *t); @@ -1042,13 +1044,19 @@ void broadcast_transport(apacket *p) D("broadcast device transport:%d\n", t->connection_state); apacket* ap = get_apacket(); copy_packet(ap, p); - send_packet(ap, t); - if (is_pwlocked()) { - t->connection_state = CS_PWLOCK; - } else { - t->connection_state = CS_DEVICE; + + if (ap->msg.command == A_STAT && ap->msg.arg1 == 0) { + // lock state message + if (ap->msg.arg0 == 0) { + // unlocked + t->connection_state = CS_DEVICE; + } else { + // locked + t->connection_state = CS_PWLOCK; + } } + } sdb_mutex_unlock(&transport_lock); } @@ -1186,3 +1194,4 @@ int check_data(apacket *p) return 0; } } + diff --git a/src/transport_local.c b/src/transport_local.c index 32e1f49..849fe05 100644 --- a/src/transport_local.c +++ b/src/transport_local.c @@ -31,12 +31,15 @@ #endif #define TRACE_TAG TRACE_TRANSPORT +#include "log.h" + #include "sdb.h" #include "strutils.h" #if !SDB_HOST #include "commandline_sdbd.h" #endif -#include "utils.h" +#include "sdbd_plugin.h" +#include "plugin.h" #ifdef HAVE_BIG_ENDIAN #define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24) @@ -292,7 +295,7 @@ static void *server_socket_thread(void * arg) // Check the peer ip validation. if (!is_emulator() - && !request_plugin_verification(SDBD_CMD_VERIFY_PEERIP, inet_ntoa(addr.sin_addr))) { + && !request_validity_to_plugin(PLUGIN_SYNC_CMD_VERIFY_PEERIP, inet_ntoa(addr.sin_addr))) { sdb_close(fd); } else { int ret = -1; diff --git a/src/transport_usb.c b/src/transport_usb.c index 0eafc4d..932e170 100644 --- a/src/transport_usb.c +++ b/src/transport_usb.c @@ -21,6 +21,8 @@ #include #define TRACE_TAG TRACE_TRANSPORT +#include "log.h" + #include "sdb.h" #ifdef HAVE_BIG_ENDIAN diff --git a/src/usb_funcfs_client.c b/src/usb_funcfs_client.c index 75d90aa..241d368 100644 --- a/src/usb_funcfs_client.c +++ b/src/usb_funcfs_client.c @@ -31,6 +31,8 @@ #include "sysdeps.h" #define TRACE_TAG TRACE_USB +#include "log.h" + #include "sdb.h" #define MAX_PACKET_SIZE_FS 64 diff --git a/src/usb_linux.c b/src/usb_linux.c index 95f2ee9..7bf435b 100644 --- a/src/usb_linux.c +++ b/src/usb_linux.c @@ -39,6 +39,8 @@ #include "sysdeps.h" #define TRACE_TAG TRACE_USB +#include "log.h" + #include "sdb.h" diff --git a/src/usb_linux_client.c b/src/usb_linux_client.c index ec8c159..5d722b4 100644 --- a/src/usb_linux_client.c +++ b/src/usb_linux_client.c @@ -27,6 +27,8 @@ #include "sysdeps.h" #define TRACE_TAG TRACE_USB +#include "log.h" + #include "sdb.h" -- 2.7.4 From e30355d93562b2edc8ad596149ccf3aa7140a906 Mon Sep 17 00:00:00 2001 From: Kim Gunsoo Date: Wed, 4 May 2016 13:33:23 +0900 Subject: [PATCH 12/16] Add the appcmd protocol for product extended routine. - In order to reduce the platform-dependent implementation, it is added to appcmd protocol that is a product extension structure. Conflicts: src/sdb.c Change-Id: I5ad12f8f3db0afdb588dbcaf49f98c7390cb794f Signed-off-by: Kim Gunsoo --- CMakeLists.txt | 1 + src/default_plugin.h | 1 + src/default_plugin_appcmd.c | 779 ++++++++++++++++++++++++++++++++++++++++++++ src/default_plugin_basic.c | 2 + src/default_plugin_event.c | 2 +- src/default_plugin_main.c | 8 +- src/log.h | 3 +- src/plugin.c | 91 ++++++ src/plugin.h | 5 + src/sdb.c | 29 +- src/sdb.h | 9 +- src/sdbd_plugin.h | 6 +- src/services.c | 21 +- 13 files changed, 939 insertions(+), 18 deletions(-) create mode 100644 src/default_plugin_appcmd.c diff --git a/CMakeLists.txt b/CMakeLists.txt index ae6f310..6c88b77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,6 +55,7 @@ SET(SDBD_SRCS src/default_plugin_basic.c src/default_plugin_main.c src/default_plugin_event.c + src/default_plugin_appcmd.c src/hashtable.c src/plugin.c ) diff --git a/src/default_plugin.h b/src/default_plugin.h index b8d44f3..800e495 100644 --- a/src/default_plugin.h +++ b/src/default_plugin.h @@ -31,6 +31,7 @@ int auth_support ( parameters* in, parameters* out ); int auth_get_key_file_paths ( parameters* in, parameters* out ); int confirm_public_key ( parameters* in, int out_fd ); +int appcmd_service ( parameters* in, int out_fd ); void create_pwlock_thread(); diff --git a/src/default_plugin_appcmd.c b/src/default_plugin_appcmd.c new file mode 100644 index 0000000..947a8e5 --- /dev/null +++ b/src/default_plugin_appcmd.c @@ -0,0 +1,779 @@ +/* + * Copyright (c) 2011 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 + +#define TRACE_TAG TRACE_APPCMD + +#include "sysdeps.h" +#include "sdb.h" +#include "strutils.h" +#include "utils.h" +#include "log.h" + +#include "parameter.h" +#include "sdbd_plugin.h" + +#if APPCMD_USING_PKGMGR +#include +#include +#endif + +#include + +#define APPCMD_RESULT_BUFSIZE (4096) + +typedef struct appcmd_info appcmd_info; +typedef int (*appcmd_gen_shellcmd)(appcmd_info*); +typedef void (*appcmd_receiver)(int, int); +struct appcmd_info { + int fd; + + char* args[MAX_TOKENS]; + size_t args_cnt; + char* raw_command; + + appcmd_gen_shellcmd gen_cmd_func; + appcmd_receiver receiver_func; + + char shell_cmd[SDBD_SHELL_CMD_MAX]; + + int exitcode; +}; + +static int appcmd_install_gen_shellcmd(appcmd_info* p_info) { + char *type = NULL; + char *pkgpath = NULL; + char *pkgid = NULL; + char *teppath = NULL; + char *buf = p_info->shell_cmd; + int len = sizeof(p_info->shell_cmd); + + if (p_info->args_cnt != 5) { + D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + return -1; + } + + type = p_info->args[1]; + pkgpath = p_info->args[2]; + pkgid = p_info->args[3]; + teppath = p_info->args[4]; + + D("args: type=%s, pkgpath=%s, pkgid=%s, teppath=%s\n", type, pkgpath, pkgid, teppath); + + if (strncmp(pkgid, "null", 4) == 0) { + if (strncmp(teppath, "null", 4) == 0) { + /* Normal install case */ + snprintf(buf, len, "pkgcmd -i -q -t %s -p %s -G", type, pkgpath); + } else { + /* TEP install case */ + snprintf(buf, len, "pkgcmd -i -q -p %s -e %s -G", pkgpath, teppath); + } + } else { + /* Re-install case */ + snprintf(buf, len, "pkgcmd -r -q -t %s -n %s", type, pkgid); + } + + return 0; +} + +static int appcmd_uninstall_gen_shellcmd(appcmd_info* p_info) { + char *type = NULL; + char *pkgid = NULL; + char *buf = p_info->shell_cmd; + int len = sizeof(p_info->shell_cmd); + + if (p_info->args_cnt != 3) { + D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + return -1; + } + + type = p_info->args[1]; + pkgid = p_info->args[2]; + + D("args: type=%s, pkgid=%s\n", type, pkgid); + + snprintf(buf, len, "pkgcmd -u -q -t %s -n %s", type, pkgid); + + return 0; +} + +static int appcmd_runapp_gen_shellcmd(appcmd_info* p_info) { + char *appid = NULL; + char *buf = p_info->shell_cmd; + int len = sizeof(p_info->shell_cmd); + + if (p_info->args_cnt != 2) { + D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + return -1; + } + + appid = p_info->args[1]; + + D("args: appid=%s\n", appid); + + snprintf(buf, len, "/usr/bin/app_launcher --start %s", appid); + + return 0; +} + +static int appcmd_rununittestapp_gen_shellcmd(appcmd_info* p_info) { + char *appid = NULL; + char *usr_args = NULL; + char *buf = p_info->shell_cmd; + int len = sizeof(p_info->shell_cmd); + char *ptr = NULL; + char *p_service = NULL; + char *p_appid = NULL; + + free_strings(p_info->args, p_info->args_cnt); + + p_service = strtok_r(p_info->raw_command, ":", &ptr); + p_appid = strtok_r(NULL, ":", &ptr); + if (p_service == NULL || p_appid == NULL) { + D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + return -1; + } + + p_info->args_cnt = 3; + p_info->args[0] = strdup(p_service); + p_info->args[1] = strdup(p_appid); + p_info->args[2] = strdup(ptr); + + appid = p_info->args[1]; + usr_args = p_info->args[2]; + + D("args: appid=%s, usr_args=%s\n", appid, usr_args); + + snprintf(buf, len, "/usr/bin/app_launcher -s %s __AUL_SDK__ UNIT_TEST __LAUNCH_APP_MODE__ SYNC __DLP_UNIT_TEST_ARG__ \'%s\'", appid, usr_args); + + return 0; +} + +static int appcmd_killapp_gen_shellcmd(appcmd_info* p_info) { + char *appid = NULL; + char *buf = p_info->shell_cmd; + int len = sizeof(p_info->shell_cmd); + + if (p_info->args_cnt != 2) { + D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + return -1; + } + + appid = p_info->args[1]; + + D("args: appid=%s\n", appid); + + snprintf(buf, len, "/usr/bin/app_launcher --kill %s", appid); + + return 0; +} + +static int appcmd_packagelist_gen_shellcmd(appcmd_info* p_info) { + char *type = NULL; + char *buf = p_info->shell_cmd; + int len = sizeof(p_info->shell_cmd); + + if (p_info->args_cnt != 2) { + D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + return -1; + } + + type = p_info->args[1]; + + D("args: type=%s\n", type); + + snprintf(buf, len, "/usr/bin/pkgcmd -l -t %s", type); + + return 0; +} + +static int appcmd_debugwebapp_gen_shellcmd(appcmd_info* p_info) { + char *appid = NULL; + char *buf = p_info->shell_cmd; + int len = sizeof(p_info->shell_cmd); + + if (p_info->args_cnt != 2) { + D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + return -1; + } + + appid = p_info->args[1]; + + D("args: appid=%s\n", appid); + + snprintf(buf, len, "/usr/bin/app_launcher --start %s -w", appid); + + return 0; +} + +static int appcmd_debugnativeapp_gen_shellcmd(appcmd_info* p_info) { + char *debug_port = NULL; + char *appid= NULL; + char *pid_str = NULL; + char *gdbserver_path = NULL; + char *buf = p_info->shell_cmd; + int pid = -1; + int len = sizeof(p_info->shell_cmd); + + if (p_info->args_cnt != 5) { + D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + return -1; + } + + debug_port = p_info->args[1]; + appid= p_info->args[2]; + pid_str = p_info->args[3]; + gdbserver_path = p_info->args[4]; // not used. for 3.0 platform. + + pid = atoi(pid_str); + D("args: debug_port=%s, appid=%s, pid=%d, gdbserver_path=%s\n", debug_port, appid, pid, gdbserver_path); + + if (pid == -1) { + snprintf(buf, len, "/usr/bin/app_launcher --start %s __AUL_SDK__ DEBUG __DLP_DEBUG_ARG__ :%s __DLP_GDBSERVER_PATH__ %s", appid, debug_port, gdbserver_path); + } else { + /* attach mode */ + snprintf(buf, len, "/usr/bin/launch_debug %s __AUL_SDK__ ATTACH __DLP_GDBSERVER_PATH__ %s __DLP_ATTACH_ARG__ --attach,:%s,%d", appid, gdbserver_path, debug_port, pid); + } + + return 0; +} + +static int appcmd_appinfo_gen_shellcmd(appcmd_info* p_info) { + char *pkgid = NULL; + char *buf = p_info->shell_cmd; + int len = sizeof(p_info->shell_cmd); + + if (p_info->args_cnt != 2) { + D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + return -1; + } + + pkgid = p_info->args[1]; + + D("args: pkgid=%s\n", pkgid); + + snprintf(buf, len, "/usr/bin/pkginfo --list %s", pkgid); + + return 0; +} + +static void appcmd_receiver_debugwebapp(int fd_in, int fd_out) +{ + char buf[4096] = {0,}; + char port_str[32] = {0,}; + char out_buf[128] = {0,}; + char* sub_str = NULL; + int r; + + for(;;) { + memset(buf, 0, sizeof(buf)); + r = read_line(fd_in, buf, sizeof(buf)); + if (r == 0) { + break; + } else if(r < 0) { + if (errno == EINTR) { + continue; + } else { + break; + } + } + + D("debug webapp output : %s\n", buf); + sub_str = strstr(buf, "port: "); + if (sub_str != NULL && sscanf(sub_str, "port: %s", port_str) == 1) { + snprintf(out_buf, sizeof(out_buf), "\n%s:%s\n", MESSAGE_PREFIX_APPCMD_RETURN, port_str); + writex(fd_out, out_buf, strlen(out_buf)+1); + break; + } + } +} + +static void appcmd_receiver_default(int fd_in, int fd_out) +{ + char buf[4096] = {0,}; + int r; + + for(;;) { + memset(buf, 0, sizeof(buf)); + r = sdb_read(fd_in, buf, sizeof(buf)); + if (r == 0) { + break; + } else if(r < 0) { + if (errno == EINTR) { + continue; + } else { + break; + } + } + + writex(fd_out, buf, strlen(buf)+1); + } +} + +static void appcmd_receiver_packagelist(int fd_in, int fd_out) +{ + char buf[4096] = {0,}; + char out_buf[4096] = {0,}; + int out_ptr = 0; + int r; + + snprintf(out_buf, sizeof(out_buf), "\n%s", MESSAGE_PREFIX_APPCMD_RETURN); + out_ptr = strlen(out_buf); + + for(;;) { + memset(buf, 0, sizeof(buf)); + r = read_line(fd_in, buf, sizeof(buf)); + if (r == 0) { + break; + } else if(r < 0) { + if (errno == EINTR) { + continue; + } else { + break; + } + } + + D("pkgcmd output : %s\n", buf); + char* sub1 = NULL; + char* sub2 = NULL; + sub1 = strstr(buf, "pkgid ["); + if (sub1 != NULL) { + sub1 = strstr(sub1, "[")+1; + sub2 = strstr(sub1, "]"); + sub2[0] = '\0'; + + snprintf(out_buf+out_ptr, sizeof(out_buf)-out_ptr, ":%s", sub1); + out_ptr += strlen(sub1)+1; + } + } + + snprintf(out_buf+out_ptr, sizeof(out_buf)-out_ptr, "\n"); + + D("package list: %s\n", out_buf); + writex(fd_out, out_buf, strlen(out_buf)+1); +} + +static void appcmd_receiver_appinfo(int fd_in, int fd_out) +{ + char buf[4096] = {0,}; + char out_buf[4096] = {0,}; + char appid[128] = {0,}; + char apptype[128] = {0,}; + int out_ptr = 0; + int r; + + snprintf(out_buf, sizeof(out_buf), "\n%s", MESSAGE_PREFIX_APPCMD_RETURN); + out_ptr = strlen(out_buf); + + for(;;) { + memset(buf, 0, sizeof(buf)); + r = read_line(fd_in, buf, sizeof(buf)); + if (r == 0) { + break; + } else if(r < 0) { + if (errno == EINTR) { + continue; + } else { + break; + } + } + + D("pkginfo output : %s\n", buf); + + if (!strncmp(buf, "Appid: ", 7)) { + memset(appid, 0, sizeof(appid)); + sscanf(buf, "Appid: %s", appid); + + snprintf(out_buf+out_ptr, sizeof(out_buf)-out_ptr, ":%s", appid); + out_ptr += strlen(appid)+1; + } else if (!strncmp(buf, "Apptype: ", 9)) { + memset(apptype, 0, sizeof(apptype)); + sscanf(buf, "Apptype: %s", apptype); + + snprintf(out_buf+out_ptr, sizeof(out_buf)-out_ptr, ":%s", apptype); + out_ptr += strlen(apptype)+1; + } + } + + snprintf(out_buf+out_ptr, sizeof(out_buf)-out_ptr, "\n"); + + D("app info: %s\n", out_buf); + writex(fd_out, out_buf, strlen(out_buf)+1); +} + +static int exec_appcmd_shell_process(appcmd_info* p_info) { + int ptm_fd = -1; + pid_t pid; + char *value = NULL; + char *trim_value = NULL; + char path[PATH_MAX]; + memset(path, 0, sizeof(path)); + + char *envp[] = { + "TERM=linux", /* without this, some programs based on screen can't work, e.g. top */ + "DISPLAY=:0", /* without this, some programs based on without launchpad can't work */ + NULL, + NULL, + NULL, + NULL, + NULL + }; + + // For the SDK user privilege. + envp[2] = "HOME=/home/developer"; + get_env("ENV_PATH", &value); + if (value != NULL) { + trim_value = str_trim(value); + if (trim_value != NULL) { + // if string is not including 'PATH=', append it. + if (strncmp(trim_value, "PATH", 4)) { + snprintf(path, sizeof(path), "PATH=%s", trim_value); + } else { + snprintf(path, sizeof(path), "%s", trim_value); + } + envp[3] = path; + free(trim_value); + } else { + envp[3] = value; + } + } + + D("path env:%s,%s,%s,%s\n", envp[0], envp[1], envp[2], envp[3]); + + char *args[] = { + SHELL_COMMAND, + "-c", + NULL, + NULL, + }; + args[2] = p_info->shell_cmd; + + ptm_fd = create_subprocess(SHELL_COMMAND, &pid, (char * const*)args, (char * const*)envp); + D("create_subprocess() ptm_fd=%d pid=%d\n", ptm_fd, pid); + if (ptm_fd < 0) { + D("cannot create service thread\n"); + return -1; + } + + if (p_info->receiver_func != NULL) { + p_info->receiver_func(ptm_fd, p_info->fd); + } + + // wait for shell process + for (;;) { + int status; + pid_t p = waitpid(pid, &status, 0); + if (p == pid) { + D("fd=%d, post waitpid(pid=%d) status=%04x\n", p_info->fd, p, status); + + if (WIFEXITED(status)) { + p_info->exitcode = WEXITSTATUS(status); + D("*** Exit code %d\n", p_info->exitcode); + break; + } + } + } + D("shell exited fd=%d of pid=%d err=%d\n", p_info->fd, pid, errno); + + return 0; +} + +#if APPCMD_USING_PKGMGR +static int get_pkg_info(char* pkgid, char* pkginfo_buf, int buf_size) { + pkgmgrinfo_pkginfo_h handle; + pkgmgr_client* pc = NULL; + char* pkgname = NULL; + char* type = NULL; + bool is_removable = 0; + int is_running = 0; + int ret = -1; + int pid = -1; + + ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle); + if (ret < 0) { + D("failed to get pkginfo handle.\n"); + return -1; + } + + ret = pkgmgrinfo_pkginfo_get_mainappid(handle, &pkgname); + if (ret < 0) { + D("failed to get pkg name\n"); + return -1; + } + + ret = pkgmgrinfo_pkginfo_get_type(handle, &type); + if (ret < 0) { + D("failed to get pkg type.\n"); + return -1; + } + + ret = pkgmgrinfo_pkginfo_is_removable(handle, &is_removable); + if (ret < 0) { + D("failed to get removable info.\n"); + return -1; + } + + pc = pkgmgr_client_new(PC_REQUEST); + if (pc == NULL) { + D("failed to create pkgmgr client.\n"); + return -1; + } + + ret = pkgmgr_client_request_service(PM_REQUEST_CHECK_APP, 0, pc, NULL, pkgid, NULL, NULL, &pid); + if (ret < 0) { + D("failed to get running state.\n"); + return -1; + } + is_running = ((pid > 0) ? 1:0); + + D("pkginfo: pkgname=%s, type=%s, is_removagle=%d, is_running=%d, pid=%d\n", pkgname, type, is_removable, is_running, pid); + snprintf(pkginfo_buf, buf_size, "%s:%s:%d:%d", pkgname, type, is_removable, is_running); + return 0; +} + +static void run_appcmd_packageinfo(appcmd_info* p_info) { + char result_buf[APPCMD_RESULT_BUFSIZE] = {0,}; + char pkginfo_buf[256] = {0,}; + char *type = NULL; + char *pkgid = NULL; + + p_info->exitcode = -1; + + if (p_info->args_cnt != 3) { + D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + return; + } + + type = p_info->args[1]; + pkgid= p_info->args[2]; + + D("args: type=%s, pkgid=%s\n", type, pkgid); + + if (get_pkg_info(pkgid, pkginfo_buf, sizeof(pkginfo_buf)) == 0) { + D("success to get pkginfo. (%s)\n", pkginfo_buf); + p_info->exitcode = 0; + snprintf(result_buf, sizeof(result_buf), "\n%s:%s\n", MESSAGE_PREFIX_APPCMD_RETURN, pkginfo_buf); + writex(p_info->fd, result_buf, strlen(result_buf)); + } else { + D("failed to get pkginfo.\n"); + } +} +#else +static int appcmd_packageinfo_gen_shellcmd(appcmd_info* p_info) { + char *pkgid = NULL; + char *buf = p_info->shell_cmd; + int len = sizeof(p_info->shell_cmd); + + if (p_info->args_cnt != 2) { + D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + return -1; + } + + pkgid = p_info->args[1]; + + D("args: pkgid=%s\n", pkgid); + + snprintf(buf, len, "/usr/bin/pkginfo --pkg %s;/usr/bin/pkgcmd -C -n %s", pkgid, pkgid); + + return 0; +} + +static void appcmd_receiver_packageinfo(int fd_in, int fd_out) +{ + char buf[4096] = {0,}; + char mainapp_id[128] = {0,}; + char type[128] = {0,}; + int is_removable = 0; + int is_running = 0; + int r; + + for(;;) { + memset(buf, 0, sizeof(buf)); + r = read_line(fd_in, buf, sizeof(buf)); + if (r == 0) { + break; + } else if(r < 0) { + if (errno == EINTR) { + continue; + } else { + break; + } + } + + if (!strncmp(buf, "mainappid : ", 12)) { + sscanf(buf, "mainappid : %s", mainapp_id); + } else if (!strncmp(buf, "Type: ", 6)) { + sscanf(buf, "Type: %s", type); + } else if (!strncmp(buf, "Removable: ", 11)) { + sscanf(buf, "Removable: %d", &is_removable); + } else if (strstr(buf, " is Running") != NULL) { + is_running = 1; + } + } + + memset(buf, 0, sizeof(buf)); + snprintf(buf, sizeof(buf), "\n%s:%s:%s:%d:%d\n", + MESSAGE_PREFIX_APPCMD_RETURN, mainapp_id, type, is_removable, is_running); + + D("package info: %s\n", buf); + writex(fd_out, buf, strlen(buf)+1); +} +#endif + +static void run_appcmd_appinstallpath(appcmd_info* p_info) { + char result_buf[APPCMD_RESULT_BUFSIZE] = {0,}; + + p_info->exitcode = -1; + + const char* path = tzplatform_getenv(TZ_SDK_HOME); + if (path != NULL) { + p_info->exitcode = 0; + snprintf(result_buf, sizeof(result_buf), "\n%s:%s/apps_rw/\n", MESSAGE_PREFIX_APPCMD_RETURN, path); + writex(p_info->fd, result_buf, strlen(result_buf)); + } else { + D("failed to get application install path from tzplatform_getenv."); + } +} + +static void run_appcmd_with_shell_process(appcmd_info* p_info) { + int ret = -1; + + if (p_info == NULL || p_info->gen_cmd_func == NULL) { + D("Invalid arguments.\n"); + p_info->exitcode = -1; + return; + } + + ret = p_info->gen_cmd_func(p_info); + if (ret < 0) { + D("failed to generate install shell command.\n"); + p_info->exitcode = -1; + } else { + ret = exec_appcmd_shell_process(p_info); + D("exec_appcmd_shell_process: ret=%d, exitcode=%d\n", ret, p_info->exitcode); + if (ret < 0) { + D("failed to run shell process\n"); + p_info->exitcode = -1; + } + } +} + +int appcmd_service( parameters* in, int out_fd ) { + appcmd_info info; + char result_buf[APPCMD_RESULT_BUFSIZE] = {0,}; + char* service_name = NULL; + char* command = NULL; + + if (in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL + || in->array_of_parameter[0].type != type_string) { + D ( "Invalid argument\n" ); + return PLUGIN_CMD_FAIL; + } + + command = in->array_of_parameter[0].v_string.data; + D("command=%s(FD:%d)\n", command, out_fd); + + memset(&info, 0, sizeof(info)); + + /* appcmd parameter data map + * "service name:arg1:arg2:...:argN" */ + info.args_cnt = tokenize(command, ":", info.args, MAX_TOKENS); + D("args_cnt=%d\n", info.args_cnt); + if (info.args_cnt < 1) { + D("failed to parse appcmd for install. (%s)\n", command); + info.exitcode = -1; + goto appcmd_done; + } + + info.fd = out_fd; + info.exitcode = -1; + info.raw_command = command; + + service_name = info.args[0]; + D("service name=%s\n", service_name); + + if (strncmp(service_name, "install", 7) == 0) { + info.receiver_func = appcmd_receiver_default; + info.gen_cmd_func = appcmd_install_gen_shellcmd; + run_appcmd_with_shell_process(&info); + // remove pkg files + if (info.args[2] != NULL) { + sdb_unlink(info.args[2]); + } + } else if (strncmp(service_name, "uninstall", 9) == 0) { + info.receiver_func = appcmd_receiver_default; + info.gen_cmd_func = appcmd_uninstall_gen_shellcmd; + run_appcmd_with_shell_process(&info); + } else if (strncmp(service_name, "appinfo", 7) == 0) { + info.gen_cmd_func = appcmd_appinfo_gen_shellcmd; + info.receiver_func = appcmd_receiver_appinfo; + run_appcmd_with_shell_process(&info); + } else if (strncmp(service_name, "packageinfo", 11) == 0) { +#if APPCMD_USING_PKGMGR + run_appcmd_packageinfo(&info); +#else + info.gen_cmd_func = appcmd_packageinfo_gen_shellcmd; + info.receiver_func = appcmd_receiver_packageinfo; + run_appcmd_with_shell_process(&info); +#endif + } else if (strncmp(service_name, "packagelist", 11) == 0) { + info.gen_cmd_func = appcmd_packagelist_gen_shellcmd; + info.receiver_func = appcmd_receiver_packagelist; + run_appcmd_with_shell_process(&info); + } else if (strncmp(service_name, "appinstallpath", 14) == 0) { + run_appcmd_appinstallpath(&info); + } else if (strncmp(service_name, "runapp", 6) == 0) { + info.receiver_func = appcmd_receiver_default; + info.gen_cmd_func = appcmd_runapp_gen_shellcmd; + run_appcmd_with_shell_process(&info); + } else if (strncmp(service_name, "rununittestapp", 14) == 0) { + info.receiver_func = appcmd_receiver_default; + info.gen_cmd_func = appcmd_rununittestapp_gen_shellcmd; + run_appcmd_with_shell_process(&info); + } else if (strncmp(service_name, "killapp", 7) == 0) { + info.receiver_func = appcmd_receiver_default; + info.gen_cmd_func = appcmd_killapp_gen_shellcmd; + run_appcmd_with_shell_process(&info); + } else if (strncmp(service_name, "debugwebapp", 11) == 0) { + info.gen_cmd_func = appcmd_debugwebapp_gen_shellcmd; + info.receiver_func = appcmd_receiver_debugwebapp; + run_appcmd_with_shell_process(&info); + } else if (strncmp(service_name, "debugnativeapp", 14) == 0) { + info.gen_cmd_func = appcmd_debugnativeapp_gen_shellcmd; + run_appcmd_with_shell_process(&info); + } else { + D("not supported appcmd service. (%s)\n", service_name); + info.exitcode = -1; + goto appcmd_done; + } + +appcmd_done: + free_strings(info.args, info.args_cnt); + + snprintf(result_buf, sizeof(result_buf), "\n%s:%d\n", MESSAGE_PREFIX_APPCMD_EXITCODE, info.exitcode); + writex(out_fd, result_buf, strlen(result_buf)); + + if (info.exitcode != 0) { + return PLUGIN_CMD_FAIL; + } + + return PLUGIN_CMD_SUCCESS; +} diff --git a/src/default_plugin_basic.c b/src/default_plugin_basic.c index 58464c7..f8ceba0 100644 --- a/src/default_plugin_basic.c +++ b/src/default_plugin_basic.c @@ -80,6 +80,8 @@ int get_plugin_capability ( parameters* in, parameters* out ) make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_DISABLED ); } else if ( capability == CAPABILITY_LOG_PATH ) { make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", LOG_DIRECTORY ); + } else if ( capability == CAPABILITY_APPCMD ) { + make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_ENABLED ); } else { out->number_of_parameter = 0; free ( out->array_of_parameter ); diff --git a/src/default_plugin_event.c b/src/default_plugin_event.c index a336f94..2f7d745 100644 --- a/src/default_plugin_event.c +++ b/src/default_plugin_event.c @@ -37,7 +37,7 @@ int get_lock_state ( parameters* in, parameters* out ) return PLUGIN_CMD_FAIL; } - D ( "shell command : %s\n", in->array_of_parameter[0].v_string.data ); + D ( "lock type: %d\n", in->array_of_parameter[0].v_int32); out->number_of_parameter = 1; out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); diff --git a/src/default_plugin_main.c b/src/default_plugin_main.c index 4bcecb0..a118fe7 100644 --- a/src/default_plugin_main.c +++ b/src/default_plugin_main.c @@ -28,11 +28,11 @@ int default_plugin_init ( eventfunc event_cb, registerfunc register_func ) // default plugin do not need to register command // because unsupported command by plugin should be called with default plugin anyway event_handler = event_cb; - + if (is_supported_by_plugin(PLUGIN_EVENT_PWLOCK) == 0) { create_pwlock_thread(); - } - + } + return PLUGIN_CMD_SUCCESS; } @@ -71,6 +71,8 @@ int default_plugin_async_proc ( int cmd, parameters* in, int out_fd ) if ( cmd == PLUGIN_ASYNC_CMD_AUTH_CONFIRM_PUBLIC ) { ret = confirm_public_key ( in, out_fd ); + } else if ( cmd == PLUGIN_ASYNC_CMD_APPCMD_SERVICE ) { + ret = appcmd_service ( in, out_fd ); } else { ret = PLUGIN_CMD_NOT_SUPPORT; } diff --git a/src/log.h b/src/log.h index f910f90..3d55707 100644 --- a/src/log.h +++ b/src/log.h @@ -41,7 +41,8 @@ typedef enum { TRACE_JDWP, TRACE_SERVICES, TRACE_PROPERTIES, - TRACE_SDKTOOLS + TRACE_SDKTOOLS, + TRACE_APPCMD } SdbTrace; #if SDB_TRACE diff --git a/src/plugin.c b/src/plugin.c index 7ef796f..1cf11f7 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -35,6 +35,12 @@ PLUGIN_ASYNCHRONOUS_CMD_PROC plugin_async_proc = NULL; hashtable* plugin_cmd_hashtable = NULL; +typedef struct _async_parameter { + int cmd; + parameters* in; + int out_fd; +} async_parameter; + // handler of event to be detected by plugin int plugin_event_handler ( int event_type, parameters* data ) { @@ -183,6 +189,66 @@ static int request_sync_cmd ( int cmd, parameters* in, parameters* out ) return ret; } +static void request_async_cmd ( int cmd, parameters* in, int out_fd ) +{ + int ret, pr; + + ret = hashtable_get ( plugin_cmd_hashtable, cmd, &pr ); + if ( ret == 1 ) { + // supported by plugin + ret = plugin_async_proc ( cmd, in, out_fd ); + if ( ret == PLUGIN_CMD_NOT_SUPPORT ) { + // not supported by plugin + ret = default_plugin_async_proc ( cmd, in, out_fd ); + } + } else { + // not supported by plugin + ret = default_plugin_async_proc ( cmd, in, out_fd ); + } + + release_parameters ( in ); + if ( in != NULL ) { + free( in ); + } + sdb_close(out_fd); +} + +static void *async_proc_bootstrap_func(void *x) +{ + async_parameter *p = x; + request_async_cmd(p->cmd, p->in, p->out_fd); + free(p); + return 0; +} + +static int create_async_proc_thread( int cmd, parameters* in ) +{ + async_parameter* async_param; + sdb_thread_t t; + int s[2]; + + if( sdb_socketpair(s) ) { + D("cannot create async proc socket pair\n"); + return -1; + } + + async_param = ( async_parameter* ) malloc(sizeof(async_parameter)); + if( async_param == NULL ) fatal("cannot allocate async_parameter"); + async_param->cmd = cmd; + async_param->in = in; + async_param->out_fd = s[1]; + + if(sdb_thread_create( &t, async_proc_bootstrap_func, async_param)){ + free(async_param); + sdb_close(s[0]); + sdb_close(s[1]); + D("cannot create async proc thread\n"); + return -1; + } + + D("async proc thread started, %d:%d\n",s[0], s[1]); + return s[0]; +} // return 1 if succeed to get capability from plugin // return 0 otherwise @@ -293,3 +359,28 @@ int request_lock_state_to_plugin ( int lock_type ) return result; } + +// return nonnegative integer that is a socket descriptor for communication +// with async proc thread if success to create async proc thread +// return -1 if failed to create async proc thread +int request_appcmd_to_plugin ( const char* in_buf ) +{ + parameters* in; + int fd; + + in = ( parameters* ) malloc ( sizeof ( parameters ) ); + if ( in_buf != NULL ) { + in->number_of_parameter = 1; + in->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + in->array_of_parameter[0].type = type_string; + in->array_of_parameter[0].v_string.length = strlen ( in_buf ); + in->array_of_parameter[0].v_string.data = strdup ( in_buf ); + } else { + in->number_of_parameter = 0; + in->array_of_parameter = NULL; + } + + fd = create_async_proc_thread( PLUGIN_ASYNC_CMD_APPCMD_SERVICE, in ); + + return fd; +} diff --git a/src/plugin.h b/src/plugin.h index a553e95..fa8288e 100644 --- a/src/plugin.h +++ b/src/plugin.h @@ -49,4 +49,9 @@ int request_conversion_to_plugin ( int cmd, const char* in_buf, char* out_buf, u // return -1 if request failed int request_lock_state_to_plugin ( int lock_type ); +// return nonnegative integer that is a socket descriptor for communication +// with async proc thread if success to create async proc thread +// return -1 if failed to create async proc thread +int request_appcmd_to_plugin ( const char* in_buf ); + #endif //__PLUGIN_H diff --git a/src/sdb.c b/src/sdb.c index 727a7aa..e282ced 100644 --- a/src/sdb.c +++ b/src/sdb.c @@ -203,6 +203,7 @@ void sdb_trace_init(void) { "services", TRACE_SERVICES }, { "properties", TRACE_PROPERTIES }, { "sdktools", TRACE_SDKTOOLS }, + { "appcmd", TRACE_APPCMD }, { NULL, 0 } }; @@ -1809,7 +1810,7 @@ static void init_capabilities(void) { "%s", DISABLED); } - // sdbd log path + // sdbd log path if(!request_capability_to_plugin(CAPABILITY_LOG_PATH, g_capabilities.log_path, sizeof(g_capabilities.log_path))) { D("failed to request. (%d:%d) \n", PLUGIN_SYNC_CMD_CAPABILITY, CAPABILITY_LOG_PATH); @@ -1817,6 +1818,13 @@ static void init_capabilities(void) { "%s", UNKNOWN); } + // Application command support + if(!request_capability_to_plugin(CAPABILITY_APPCMD, g_capabilities.appcmd_support, + sizeof(g_capabilities.appcmd_support))) { + D("failed to request. (%d:%d) \n", PLUGIN_SYNC_CMD_CAPABILITY, CAPABILITY_APPCMD); + snprintf(g_capabilities.appcmd_support, sizeof(g_capabilities.appcmd_support), + "%s", UNKNOWN); + } // Capability version snprintf(g_capabilities.sdbd_cap_version, sizeof(g_capabilities.sdbd_cap_version), @@ -1855,13 +1863,28 @@ static void check_emulator_or_device() } } +static void fork_prepare_handler(void) +{ + sdb_mutex_lock(&D_lock); +} + +static void fork_parent_handler(void) +{ + sdb_mutex_unlock(&D_lock); +} + +static void fork_child_handler(void) +{ + sdb_mutex_unlock(&D_lock); +} + int sdb_main(int is_daemon, int server_port) { #if !SDB_HOST check_emulator_or_device(); load_sdbd_plugin(); - + init_capabilities(); sdb_trace_init(); @@ -1875,6 +1898,8 @@ int sdb_main(int is_daemon, int server_port) } umask(000); + + pthread_atfork(fork_prepare_handler, fork_parent_handler, fork_child_handler); #endif atexit(sdb_cleanup); diff --git a/src/sdb.h b/src/sdb.h index 5d7bd94..fec1a5d 100644 --- a/src/sdb.h +++ b/src/sdb.h @@ -256,9 +256,10 @@ typedef struct platform_capabilities char syncwinsz_support[CAPBUF_ITEMSIZE]; // enabled or disabled char usbproto_support[CAPBUF_ITEMSIZE]; // enabled or disabled char sockproto_support[CAPBUF_ITEMSIZE]; // enabled or disabled + char appcmd_support[CAPBUF_ITEMSIZE]; // enabled or disabled - char log_enable[CAPBUF_ITEMSIZE]; // enabled or disabled - char log_path[CAPBUF_LL_ITEMSIZE]; // path of sdbd log + char log_enable[CAPBUF_ITEMSIZE]; // enabled or disabled + char log_path[CAPBUF_LL_ITEMSIZE]; // path of sdbd log char cpu_arch[CAPBUF_ITEMSIZE]; // cpu architecture (ex. x86) char profile_name[CAPBUF_ITEMSIZE]; // profile name (ex. mobile) @@ -523,3 +524,7 @@ int read_line(const int fd, char* ptr, const size_t maxlen); #define USB_FUNCFS_SDB_PATH "/dev/usbgadget/sdb" #define USB_NODE_FILE "/dev/samsung_sdb" +#define SHELL_COMMAND "/bin/sh" +int create_subprocess(const char *cmd, pid_t *pid, char * const argv[], char * const envp[]); +void get_env(char *key, char **env); + diff --git a/src/sdbd_plugin.h b/src/sdbd_plugin.h index 45f2be2..5159928 100644 --- a/src/sdbd_plugin.h +++ b/src/sdbd_plugin.h @@ -36,13 +36,16 @@ // asynchronous command #define PLUGIN_ASYNC_CMD_AUTH_CONFIRM_PUBLIC 2000 +#define PLUGIN_ASYNC_CMD_APPCMD_SERVICE 2001 // event #define PLUGIN_EVENT_PWLOCK 3000 #define PLUGIN_EVENT_FMMLOCK 3001 // message -#define PLUGIN_MESSAGE_CONFIRM_PUBLIC 4000 +#define MESSAGE_PREFIX_CONFIRM_PUBLIC "auth_confirm_return" +#define MESSAGE_PREFIX_APPCMD_EXITCODE "appcmd_exitcode" +#define MESSAGE_PREFIX_APPCMD_RETURN "appcmd_returnstr" // ============================================================================== // capability definition @@ -58,6 +61,7 @@ #define CAPABILITY_PRODUCT_VER 10008 #define CAPABILITY_LOG_ENABLE 10009 #define CAPABILITY_LOG_PATH 10010 +#define CAPABILITY_APPCMD 10011 // =============================================================================== // priority definition diff --git a/src/services.c b/src/services.c index 22b9dda..5d40ce9 100644 --- a/src/services.c +++ b/src/services.c @@ -310,6 +310,7 @@ void inoti_service(int fd, void *arg) if ( ifd < 0 ) { D( "inotify_init failed\n"); + sdb_close(fd); return; } @@ -437,7 +438,7 @@ static void redirect_and_exec(int pts, const char *cmd, char * const argv[], cha execve(cmd, argv, envp); } -static int create_subprocess(const char *cmd, pid_t *pid, char * const argv[], char * const envp[]) +int create_subprocess(const char *cmd, pid_t *pid, char * const argv[], char * const envp[]) { char devname[64]; int ptm; @@ -518,7 +519,6 @@ static int create_subprocess(const char *cmd, pid_t *pid, char * const argv[], c } #endif /* !SDB_HOST */ -#define SHELL_COMMAND "/bin/sh" #define LOGIN_COMMAND "/bin/login" #define SUPER_USER "root" #define LOGIN_CONFIG "/etc/login.defs" @@ -555,7 +555,7 @@ static void subproc_waiter_service(int fd, void *cookie) } } -static void get_env(char *key, char **env) +void get_env(char *key, char **env) { FILE *fp; char buf[1024]; @@ -842,7 +842,6 @@ static int create_syncproc_thread() return ret_fd; } - #endif static void get_platforminfo(int fd, void *cookie) { @@ -995,12 +994,16 @@ static void get_capability(int fd, void *cookie) { "sdbd_cap_version", g_capabilities.sdbd_cap_version); // Sdbd log enable - offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE, - "log_enable", g_capabilities.log_enable); + offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE, + "log_enable", g_capabilities.log_enable); // Sdbd log path - offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE, - "log_path", g_capabilities.log_path); + offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE, + "log_path", g_capabilities.log_path); + + // Application command support + offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE, + "appcmd_support", g_capabilities.appcmd_support); offset++; // for '\0' character @@ -1165,6 +1168,8 @@ int service_to_fd(const char *name) char* env_variable = NULL; env_variable = strdup(name+14); ret = create_service_thread(get_tzplatform_env, (void *)(env_variable)); + } else if(!strncmp(name, "appcmd:", 7)){ + ret = request_appcmd_to_plugin(name+7); } if (ret >= 0) { -- 2.7.4 From a430ea553950d5a9910cc051c3171da852607c03 Mon Sep 17 00:00:00 2001 From: greatim Date: Thu, 8 Sep 2016 18:15:43 +0900 Subject: [PATCH 13/16] modify default plugin's behavior by emulator modify default plugin's behavior by emulator add more debugging log Change-Id: I2d5fdd4c5a56f63113dc2d34622afcdc2eacb345 Signed-off-by: greatim --- src/default_plugin_basic.c | 8 ++----- src/default_plugin_event.c | 53 ++++++++++++++++++++++------------------------ src/plugin.c | 16 +++++++++++++- 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/default_plugin_basic.c b/src/default_plugin_basic.c index f8ceba0..2072824 100644 --- a/src/default_plugin_basic.c +++ b/src/default_plugin_basic.c @@ -57,11 +57,7 @@ int get_plugin_capability ( parameters* in, parameters* out ) } else if ( capability == CAPABILITY_FILESYNC ) { make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_PUSHPULL ); } else if ( capability == CAPABILITY_USB_PROTOCOL ) { - if ( is_emulator() ) { - make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_DISABLED ); - } else { - make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_ENABLED ); - } + make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_ENABLED ); } else if ( capability == CAPABILITY_SOCK_PROTOCOL ) { make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_ENABLED ); } else if ( capability == CAPABILITY_ROOT_ONOFF ) { @@ -77,7 +73,7 @@ int get_plugin_capability ( parameters* in, parameters* out ) } else if ( capability == CAPABILITY_PRODUCT_VER ) { make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_UNKNOWN ); } else if ( capability == CAPABILITY_LOG_ENABLE ) { - make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_DISABLED ); + make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", PLUGIN_RET_ENABLED ); } else if ( capability == CAPABILITY_LOG_PATH ) { make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", LOG_DIRECTORY ); } else if ( capability == CAPABILITY_APPCMD ) { diff --git a/src/default_plugin_event.c b/src/default_plugin_event.c index 2f7d745..6731746 100644 --- a/src/default_plugin_event.c +++ b/src/default_plugin_event.c @@ -17,36 +17,10 @@ #include #include -#define TRACE_TAG TRACE_SDB -#include "log.h" - #include "sysdeps.h" #include "sdbd_plugin.h" #include "default_plugin.h" -int get_lock_state ( parameters* in, parameters* out ) -{ - if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL - || in->array_of_parameter[0].type != type_int32 ) { - D ( "Invalid argument\n" ); - return PLUGIN_CMD_FAIL; - } - - if ( out == NULL ) { - D ( "Invalid argument\n" ); - return PLUGIN_CMD_FAIL; - } - - D ( "lock type: %d\n", in->array_of_parameter[0].v_int32); - - out->number_of_parameter = 1; - out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); - out->array_of_parameter[0].type = type_int32; - out->array_of_parameter[0].v_int32 = ( plugin_pwlocked() == 1 ) ? PLUGIN_RET_ON : PLUGIN_RET_OFF; - - return PLUGIN_CMD_SUCCESS; -} - int plugin_pwlocked ( void ) { int pwlock_status = 0; @@ -82,6 +56,29 @@ int plugin_pwlocked ( void ) return 0; // unlocked! } +int get_lock_state ( parameters* in, parameters* out ) +{ + if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL + || in->array_of_parameter[0].type != type_int32 ) { + PLUGIN_LOG ( "Invalid argument\n" ); + return PLUGIN_CMD_FAIL; + } + + if ( out == NULL ) { + PLUGIN_LOG ( "Invalid argument\n" ); + return PLUGIN_CMD_FAIL; + } + + PLUGIN_LOG ( "lock type: %d\n", in->array_of_parameter[0].v_int32); + + out->number_of_parameter = 1; + out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + out->array_of_parameter[0].type = type_int32; + out->array_of_parameter[0].v_int32 = ( plugin_pwlocked() == 1 ) ? PLUGIN_RET_ON : PLUGIN_RET_OFF; + + return PLUGIN_CMD_SUCCESS; +} + static void pwlock_cb ( keynode_t *key, void* data ) { PLUGIN_LOG ( "pwlock callback is issued\n" ); @@ -131,8 +128,8 @@ void create_pwlock_thread() { sdb_thread_t t; if ( sdb_thread_create ( &t, pwlock_thread, NULL ) ) { - PLUGIN_LOG ( "cannot create_pwlock_thread.\n" ); + PLUGIN_LOG ( "default plugin : cannot create_pwlock_thread.\n" ); return; } - PLUGIN_LOG ( "created pwlock_thread\n" ); + PLUGIN_LOG ( "default plugin : created pwlock_thread\n" ); } diff --git a/src/plugin.c b/src/plugin.c index 1cf11f7..ff9f19c 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -73,7 +73,10 @@ int plugin_event_handler ( int event_type, parameters* data ) static int plugin_register_command ( int command, priority cmd_priority ) { if ( plugin_cmd_hashtable ) { - hashtable_put ( plugin_cmd_hashtable, command, cmd_priority ); + int ret = hashtable_put ( plugin_cmd_hashtable, command, cmd_priority ); + D ("register plugin command : cmd(%d), result(%d)\n", command, ret); + } else { + D ("hashtable is not created\n"); } return 0; } @@ -161,6 +164,9 @@ int is_supported_by_plugin ( int cmd ) if ( plugin_cmd_hashtable ) { int value; ret = hashtable_get ( plugin_cmd_hashtable, cmd, &value ); + D ("get from hashtable : cmd(%d), result(%d)\n", cmd, ret); + } else { + D ("hashtable is not created\n"); } return ret; @@ -263,12 +269,16 @@ int request_capability_to_plugin ( int cap, char* out_buf, unsigned int out_len in.array_of_parameter[0].type = type_int32; in.array_of_parameter[0].v_int32 = cap; + D ("requested capability : %d\n", cap); + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_CAPABILITY, &in, &out ); if ( ret == PLUGIN_CMD_SUCCESS ) { strncpy ( out_buf, out.array_of_parameter[0].v_string.data, out_len - 1 ); out_buf[out_len - 1] = '\0'; success = 1; release_parameters ( &out ); + + D ("request capability success : %s\n", out_buf); } release_parameters ( &in ); @@ -294,10 +304,14 @@ int request_validity_to_plugin ( int cmd, const char* in_buf ) in.array_of_parameter = NULL; } + D ("requested validity : %d, %s\n", cmd, in_buf); + ret = request_sync_cmd ( cmd, &in, &out ); if ( ret == PLUGIN_CMD_SUCCESS ) { success = ( out.array_of_parameter[0].v_int32 == PLUGIN_RET_VALID ) ? 1 : 0; release_parameters ( &out ); + + D ("request validity success : %d\n", out.array_of_parameter[0].v_int32); } release_parameters ( &in ); -- 2.7.4 From 97f2d53e382e658536c6e38073f695812b57af38 Mon Sep 17 00:00:00 2001 From: greatim Date: Fri, 4 Nov 2016 20:52:51 +0900 Subject: [PATCH 14/16] Merge plugin improvement commit Merge plugin improvement commit Change-Id: Ic0e4f94dd9874a5cea4885a5fda7ff0de2441388 Signed-off-by: greatim --- src/default_plugin.h | 1 + src/default_plugin_basic.c | 15 +++++++++++++++ src/default_plugin_main.c | 2 ++ src/plugin.c | 16 +++++++++++----- src/sdb.c | 5 ++--- src/sdbd_plugin.h | 1 + src/services.c | 9 ++++++--- src/transport_local.c | 1 + 8 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/default_plugin.h b/src/default_plugin.h index 800e495..0c8b5b9 100644 --- a/src/default_plugin.h +++ b/src/default_plugin.h @@ -26,6 +26,7 @@ int verify_peer_ip ( parameters* in, parameters* out ); int verify_sdbd_launch ( parameters* in, parameters* out ); int verify_root_cmd ( parameters* in, parameters* out ); int get_lock_state ( parameters* in, parameters* out ); +int get_shell_env ( parameters* in, parameters* out ); int auth_support ( parameters* in, parameters* out ); int auth_get_key_file_paths ( parameters* in, parameters* out ); diff --git a/src/default_plugin_basic.c b/src/default_plugin_basic.c index 2072824..81f313c 100644 --- a/src/default_plugin_basic.c +++ b/src/default_plugin_basic.c @@ -198,3 +198,18 @@ int verify_root_cmd ( parameters* in, parameters* out ) return PLUGIN_CMD_SUCCESS; } + +int get_shell_env ( parameters* in, parameters* out ) +{ + if ( out == NULL ) { + D ( "Invalid argument\n" ); + return PLUGIN_CMD_FAIL; + } + + out->number_of_parameter = 1; + out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + + make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", "" ); + return PLUGIN_CMD_SUCCESS; +} + diff --git a/src/default_plugin_main.c b/src/default_plugin_main.c index a118fe7..ea71971 100644 --- a/src/default_plugin_main.c +++ b/src/default_plugin_main.c @@ -58,6 +58,8 @@ int default_plugin_sync_proc ( int cmd, parameters* in, parameters* out ) ret = auth_get_key_file_paths ( in, out ); } else if ( cmd == PLUGIN_SYNC_CMD_GET_LOCK_STATE ) { ret = get_lock_state ( in, out ); + } else if ( cmd == PLUGIN_SYNC_CMD_GET_SHELL_ENV ) { + ret = get_shell_env ( in, out ); } else { ret = PLUGIN_CMD_NOT_SUPPORT; } diff --git a/src/plugin.c b/src/plugin.c index ff9f19c..e8c70b9 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -326,11 +326,16 @@ int request_conversion_to_plugin ( int cmd, const char* in_buf, char* out_buf, u int ret; parameters in, out; - in.number_of_parameter = 1; - in.array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); - in.array_of_parameter[0].type = type_string; - in.array_of_parameter[0].v_string.length = strlen ( in_buf ); - in.array_of_parameter[0].v_string.data = strdup ( in_buf ); + if ( in_buf != NULL ) { + in.number_of_parameter = 1; + in.array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + in.array_of_parameter[0].type = type_string; + in.array_of_parameter[0].v_string.length = strlen ( in_buf ); + in.array_of_parameter[0].v_string.data = strdup ( in_buf ); + } else { + in.number_of_parameter = 0; + in.array_of_parameter = NULL; + } ret = request_sync_cmd ( cmd, &in, &out ); if ( ret == PLUGIN_CMD_SUCCESS ) { @@ -398,3 +403,4 @@ int request_appcmd_to_plugin ( const char* in_buf ) return fd; } + diff --git a/src/sdb.c b/src/sdb.c index e282ced..9a0d5cf 100644 --- a/src/sdb.c +++ b/src/sdb.c @@ -1755,10 +1755,9 @@ static void init_capabilities(void) { // Target name of the launch possible - if(!request_plugin_cmd(SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_CANLAUNCH, - g_capabilities.can_launch, + if(!request_capability_to_plugin(CAPABILITY_CAN_LAUNCH, g_capabilities.can_launch, sizeof(g_capabilities.can_launch))) { - D("failed to request. (%s:%s) \n", SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_CANLAUNCH); + D("failed to request. (%d:%d) \n", PLUGIN_SYNC_CMD_CAPABILITY, CAPABILITY_CAN_LAUNCH); snprintf(g_capabilities.can_launch, sizeof(g_capabilities.can_launch), "%s", UNKNOWN); } diff --git a/src/sdbd_plugin.h b/src/sdbd_plugin.h index 5159928..2bfb450 100644 --- a/src/sdbd_plugin.h +++ b/src/sdbd_plugin.h @@ -33,6 +33,7 @@ #define PLUGIN_SYNC_CMD_AUTH_SUPPORT 1006 #define PLUGIN_SYNC_CMD_AUTH_GET_KEY_FILEPATHS 1007 #define PLUGIN_SYNC_CMD_GET_LOCK_STATE 1008 +#define PLUGIN_SYNC_CMD_GET_SHELL_ENV 1009 // asynchronous command #define PLUGIN_ASYNC_CMD_AUTH_CONFIRM_PUBLIC 2000 diff --git a/src/services.c b/src/services.c index 5d40ce9..1c48905 100644 --- a/src/services.c +++ b/src/services.c @@ -54,6 +54,8 @@ #include "sdbd_plugin.h" #include "plugin.h" +#define ENV_BUF_MAX 4096 + typedef struct stinfo stinfo; struct stinfo { @@ -647,13 +649,14 @@ static int create_subproc_thread(const char *name, int lines, int columns) /* get environment variables from plugin */ char *envp_plugin = NULL; - envp_plugin = malloc(SDBD_PLUGIN_OUTBUF_MAX); + envp_plugin = malloc(ENV_BUF_MAX); if (envp_plugin == NULL) { D("Cannot allocate the shell commnad buffer."); return -1; } - memset(envp_plugin, 0, SDBD_PLUGIN_OUTBUF_MAX); - if (!request_plugin_cmd(SDBD_CMD_SHELL_ENVVAR, "", envp_plugin, SDBD_PLUGIN_OUTBUF_MAX)) { + memset(envp_plugin, 0, ENV_BUF_MAX); + if (!request_conversion_to_plugin(PLUGIN_SYNC_CMD_GET_SHELL_ENV, NULL, + envp_plugin, ENV_BUF_MAX)) { D("Failed to convert the shell command. (%s)\n", name); free(envp_plugin); return -1; diff --git a/src/transport_local.c b/src/transport_local.c index 849fe05..a6adb0b 100644 --- a/src/transport_local.c +++ b/src/transport_local.c @@ -38,6 +38,7 @@ #if !SDB_HOST #include "commandline_sdbd.h" #endif +#include "utils.h" #include "sdbd_plugin.h" #include "plugin.h" -- 2.7.4 From 89745858c0ca69210ece6e794d980fc7bfc51f96 Mon Sep 17 00:00:00 2001 From: greatim Date: Thu, 3 Nov 2016 17:17:55 +0900 Subject: [PATCH 15/16] add data encryption feature add data encryption feature - encryption on / off for each device - encryption message handshaking - encryption module Change-Id: I89337af9d9a5bc6fac45401f962392491b3e43ae Signed-off-by: greatim --- CMakeLists.txt | 3 + packaging/sdbd.spec | 1 + src/plugin.c | 22 ++-- src/sdb.c | 168 +++++++++++++++++++++++++++++- src/sdb.h | 18 ++++ src/sdbd_plugin.h | 1 + src/services.c | 4 + src/sockets.c | 21 +++- src/transport.c | 26 +++++ src/transport_security.c | 261 +++++++++++++++++++++++++++++++++++++++++++++++ src/transport_security.h | 18 ++++ 11 files changed, 529 insertions(+), 14 deletions(-) create mode 100644 src/transport_security.c create mode 100644 src/transport_security.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c88b77..47edfd9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,6 +58,7 @@ SET(SDBD_SRCS src/default_plugin_appcmd.c src/hashtable.c src/plugin.c + src/transport_security.c ) include(FindPkgConfig) @@ -69,6 +70,7 @@ pkg_check_modules(pkgs REQUIRED glib-2.0 dbus-1 dbus-glib-1 + dlog ) FOREACH(flag ${pkgs_CFLAGS}) @@ -86,6 +88,7 @@ ADD_DEFINITIONS("-D_GNU_SOURCE") ADD_DEFINITIONS("-DHAVE_FORKEXEC") ADD_DEFINITIONS("-D_DROP_PRIVILEGE") ADD_DEFINITIONS("-D_FILE_OFFSET_BITS=64") +ADD_DEFINITIONS("-DSUPPORT_ENCRYPT") IF (_ARM_TARGET) ADD_DEFINITIONS("-DANDROID_GADGET=1") diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index 1cf6758..bc9408f 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -25,6 +25,7 @@ BuildRequires: pkgconfig(vconf) BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(dbus-1) BuildRequires: pkgconfig(dbus-glib-1) +BuildRequires: pkgconfig(dlog) Requires: dbus %description diff --git a/src/plugin.c b/src/plugin.c index e8c70b9..bee7aa4 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -27,7 +27,7 @@ #include "plugin.h" #include "sdbd_plugin.h" -static void* plugin_handle = NULL; +void* g_plugin_handle = NULL; PLUGIN_INIT plugin_init_proc = NULL; PLUGIN_SYNCHRONOUS_CMD_PROC plugin_sync_proc = NULL; @@ -89,28 +89,28 @@ static int load_plugin_not_default() plugin_sync_proc = NULL; plugin_async_proc = NULL; - plugin_handle = dlopen ( PLUGIN_PATH, RTLD_NOW ); - if ( plugin_handle == NULL ) { + g_plugin_handle = dlopen ( PLUGIN_PATH, RTLD_NOW ); + if ( g_plugin_handle == NULL ) { D ( "failed to dlopen(%s). error: %s\n", PLUGIN_PATH, dlerror() ); return 0; } - plugin_init_proc = dlsym ( plugin_handle, PLUGIN_PROC_NAME_INIT ); + plugin_init_proc = dlsym ( g_plugin_handle, PLUGIN_PROC_NAME_INIT ); if ( plugin_init_proc == NULL ) { D ( "failed to get the sdbd plugin init function. error: %s\n", dlerror() ); - dlclose ( plugin_handle ); - plugin_handle = NULL; + dlclose ( g_plugin_handle ); + g_plugin_handle = NULL; return 0; } // if there is no implementation of plugin_sync_proc and plugin_async_proc, // then use default_plugin_sync_proc and default_plugin_async_proc - plugin_sync_proc = dlsym ( plugin_handle, PLUGIN_PROC_NAME_SYNC_CMD ); + plugin_sync_proc = dlsym ( g_plugin_handle, PLUGIN_PROC_NAME_SYNC_CMD ); if ( plugin_sync_proc == NULL ) { plugin_sync_proc = default_plugin_sync_proc; } - plugin_async_proc = dlsym ( plugin_handle, PLUGIN_PROC_NAME_ASYNC_CMD ); + plugin_async_proc = dlsym ( g_plugin_handle, PLUGIN_PROC_NAME_ASYNC_CMD ); if ( plugin_async_proc == NULL ) { plugin_async_proc = default_plugin_async_proc; } @@ -149,9 +149,9 @@ void unload_sdbd_plugin() plugin_cmd_hashtable = NULL; } - if ( plugin_handle ) { - dlclose ( plugin_handle ); - plugin_handle = NULL; + if ( g_plugin_handle ) { + dlclose ( g_plugin_handle ); + g_plugin_handle = NULL; } } diff --git a/src/sdb.c b/src/sdb.c index 9a0d5cf..50cf9ed 100644 --- a/src/sdb.c +++ b/src/sdb.c @@ -44,6 +44,10 @@ #include "plugin.h" #include "sdbd_plugin.h" +#ifdef SUPPORT_ENCRYPT +#include "transport_security.h" +#endif + #if !SDB_HOST #include #define SDB_PIDPATH "/tmp/.sdbd.pid" @@ -368,6 +372,147 @@ void print_packet(const char *label, apacket *p) } #endif +#ifdef SUPPORT_ENCRYPT +/* +desc. : 암호화 실패 메시지 전송 +parameter : [in] apacket* p : sdbd로 들어온 메시지 + [in] atransport *t : 현재 연결에 대한 atransport + [in] unsigned failed_value : 실패 값 +*/ +void send_encr_fail(apacket* p, atransport *t, unsigned failed_value){ + apacket* enc_p; + enc_p = get_apacket(); + enc_p->msg.command = A_ENCR; // 암호화 메시지 + enc_p->msg.arg0 = failed_value; // 실패값 + enc_p->msg.arg1 = p->msg.arg1; + send_packet(enc_p, t); + //put_apacket(enc_p); +} + +/* +desc. : 암호화 메시지 핸들링 +parameter : [in] apacket* p : sdbd로 들어온 메시지 + [in/out] apacket* enc_p : sdb server로 전송할 메시지 + [in/out] atransport *t : 현재 연결에 대한 atransport +ret : 0 : 정상적으로 메시지 전송 + -1: 메시지 전송 실패 +*/ +int handle_encr_packet(apacket* p, apacket* enc_p, atransport *t){ + static int sessionID = 0; + int retVal = 0; + if(p->msg.arg0 == ENCR_SET_ON_REQ){ // hello 메시지인 경우 + t->sessionID = sessionID; + if((retVal = security_init(t->sessionID, NULL)) == 1){ // 암호화 handshaking을 위한 init + if(security_parse_server_hello(t->sessionID, p) == 1){ // hello 메시지 파싱 + D("security_parse_server_hello success\n"); + if(security_gen_client_hello(t->sessionID, enc_p) == 1){ // hello 메시지 생성 + D("security_gen_client_hello success\n"); + enc_p->msg.command = A_ENCR; + enc_p->msg.arg0 = ENCR_SET_ON_REQ; + enc_p->msg.arg1 = p->msg.arg1; + sessionID++; + send_packet(enc_p, t); + } + else { // hello 메시지 생성 실패 + D("security_gen_client_hello error\n"); + send_encr_fail(p, t, ENCR_ON_FAIL); // 암호화 on 실패 메시지 전송 + t->encryption = ENCR_OFF; // 암호화 모드는 off + security_deinit(t->sessionID); + return -1; + } + } + else{ // hello 메시지 파싱 실패 + D("security_parse_server_hello error\n"); + send_encr_fail(p, t, ENCR_ON_FAIL); + t->encryption = ENCR_OFF; + security_deinit(t->sessionID); + + return -1; + } + } else { // init 실패 + D("security_init error\n"); + send_encr_fail(p, t, ENCR_ON_FAIL); + t->encryption = ENCR_OFF; + if (retVal == -1) + { + security_deinit(t->sessionID); + } + //here!! do security_deinit(), but when plugin pointer is null -> not deinit + return -1; + } + } + else if(p->msg.arg0 == ENCR_SET_ON_OK){ // ack 메시지인 경우 + if(security_parse_server_ack(t->sessionID, p) == 1){ // ack 메시지 파싱 + if(security_gen_client_ack(t->sessionID, enc_p) == 1){ // ack 메시지 생성 + D("security_gen_client_ack success\n"); + enc_p->msg.command = A_ENCR; + enc_p->msg.arg0 = ENCR_SET_ON_OK; + enc_p->msg.arg1 = p->msg.arg1; + t->encryption = ENCR_ON; + send_packet(enc_p, t); + } + else { // ack 메시지 생성에 실패한 경우 + D("security_gen_client_ack error\n"); + send_encr_fail(p, t, ENCR_ON_FAIL); + t->encryption = ENCR_OFF; + security_deinit(t->sessionID); + return -1; + } + } + else { // ack 메시지 파싱에 실패한 경우 + D("security_parse_server_ack error\n"); + send_encr_fail(p, t, ENCR_ON_FAIL); + t->encryption = ENCR_OFF; + security_deinit(t->sessionID); + return -1; + } + } + else if(p->msg.arg0 == ENCR_SET_OFF){ // 암호화 모드 off 요청 메시지 + if(t->encryption == ENCR_ON && security_deinit(t->sessionID) == 1){ // 현재 암호화 모드가 on 상태인 경우 + enc_p = get_apacket(); + t->encryption = ENCR_OFF; // 현재 연결에 대한 암호화 모드 off + enc_p->msg.command = A_ENCR; + enc_p->msg.arg0 = ENCR_SET_OFF; + enc_p->msg.arg1 = p->msg.arg1; + send_packet(enc_p, t); + } + else { // 암호화 모드 off에 실패한 경우 + D("security_deinit error\n"); + send_encr_fail(p, t, ENCR_OFF_FAIL); // 암호화 모드 off 실패 메시지 전송 + return -1; + } + } + else if(p->msg.arg0 == ENCR_GET){ // 암호화 모드의 상태 요청 메시지인 경우 + enc_p = get_apacket(); + enc_p->msg.command = A_ENCR; + enc_p->msg.arg0 = ENCR_GET; // 암호화 모드 status get메시지 + enc_p->msg.arg1 = p->msg.arg1; + if(t->encryption == ENCR_ON){ // 암호화 모드가 on인 경우 + enc_p->msg.data_length = 13; + strncpy((char*)enc_p->data, "encryption:on", enc_p->msg.data_length); // encryption:on 메시지 전송 + } else if(t->encryption == ENCR_OFF){ // 암호화 모드가 off인 경우 + enc_p->msg.data_length = 14; + strncpy((char*)enc_p->data, "encryption:off", enc_p->msg.data_length); // encryption:off 메시지 전송 + } + send_packet(enc_p, t); + } + else if (p->msg.arg0 == ENCR_ON_FAIL) // 암호화 모드를 on 하는 도중 실패한 경우 받는 메시지 + { + t->encryption = ENCR_OFF; // 암호화 모드를 다시 off + D("encryption on failed\n"); + } + else if (p->msg.arg0 == ENCR_OFF_FAIL) // 암호화 모드를 off하는 도중 실패한 경우 받는 메시지 + { + //t->encryption = ENCR_ON; + D("encryption off failed\n"); + } + //put_apacket(enc_p); + return 0; + +} +#endif + + static void send_ready(unsigned local, unsigned remote, atransport *t) { D("Calling send_ready \n"); @@ -394,8 +539,11 @@ static void send_connect(atransport *t) apacket *cp = get_apacket(); cp->msg.command = A_CNXN; cp->msg.arg0 = A_VERSION; +#ifdef SUPPORT_ENCRYPT + cp->msg.arg1 = MAX_PAYLOAD - 100; // connection 시, sdb server의 패킷 크기를 암호화 오버로드 만큼 줄임 +#else cp->msg.arg1 = MAX_PAYLOAD; - +#endif char device_name[256]={0,}; int r = 0; int status = 0; @@ -739,6 +887,15 @@ void handle_packet(apacket *p, atransport *t) } } break; +#ifdef SUPPORT_ENCRYPT + case A_ENCR: // 암호화 메시지인 경우 + if(t->connection_state != CS_OFFLINE) { + apacket* enc_p = get_apacket(); + handle_encr_packet(p, enc_p, t); + //put_apacket(enc_p); + } + break; +#endif default: printf("handle_packet: what is %08x?!\n", p->msg.command); @@ -1692,6 +1849,15 @@ static void init_capabilities(void) { } + // Encryption support + if(!request_capability_to_plugin(CAPABILITY_ENCRYPTION, g_capabilities.encryption_support, + sizeof(g_capabilities.encryption_support))) { + D("failed to request. (%d:%d) \n", PLUGIN_SYNC_CMD_CAPABILITY, CAPABILITY_ENCRYPTION); + snprintf(g_capabilities.encryption_support, sizeof(g_capabilities.encryption_support), + "%s", DISABLED); + } + + // Zone support ret = is_container_enabled(); snprintf(g_capabilities.zone_support, sizeof(g_capabilities.zone_support), diff --git a/src/sdb.h b/src/sdb.h index fec1a5d..348a7eb 100644 --- a/src/sdb.h +++ b/src/sdb.h @@ -37,6 +37,18 @@ #define A_CLSE 0x45534c43 #define A_WRTE 0x45545257 #define A_STAT 0x54415453 +#define A_ENCR 0x40682018 // encryption 메시지 + +#ifdef SUPPORT_ENCRYPT + #define ENCR_SET_ON_REQ 0 // encryption hello 메시지 + #define ENCR_SET_ON_OK 1 // encryption ack 메시지 + #define ENCR_SET_OFF 2 // encryption mode off 메시지 + #define ENCR_GET 3 // encryption status get 메시지 + #define ENCR_ON_FAIL 4 // encryption on 실패 메시지 + #define ENCR_OFF_FAIL 5 // encryption off 실패 메시지 + #define ENCR_ON 1 // encryption on 상태 + #define ENCR_OFF 0 // encryption off 상태 +#endif #define A_VERSION 0x02000000 // SDB protocol version @@ -200,6 +212,11 @@ struct atransport /* a list of adisconnect callbacks called when the transport is kicked */ int kicked; adisconnect disconnects; + +#ifdef SUPPORT_ENCRYPT + unsigned encryption; // 해당 연결이 암호화 모드인지 확인하는 flag , 0 = no-encryption / 1 = encryption + int sessionID; // 암호화 세션 ID, 암호화 map에 대한 key +#endif }; @@ -257,6 +274,7 @@ typedef struct platform_capabilities char usbproto_support[CAPBUF_ITEMSIZE]; // enabled or disabled char sockproto_support[CAPBUF_ITEMSIZE]; // enabled or disabled char appcmd_support[CAPBUF_ITEMSIZE]; // enabled or disabled + char encryption_support[CAPBUF_ITEMSIZE]; // enabled or disabled char log_enable[CAPBUF_ITEMSIZE]; // enabled or disabled char log_path[CAPBUF_LL_ITEMSIZE]; // path of sdbd log diff --git a/src/sdbd_plugin.h b/src/sdbd_plugin.h index 2bfb450..bcaaa8d 100644 --- a/src/sdbd_plugin.h +++ b/src/sdbd_plugin.h @@ -63,6 +63,7 @@ #define CAPABILITY_LOG_ENABLE 10009 #define CAPABILITY_LOG_PATH 10010 #define CAPABILITY_APPCMD 10011 +#define CAPABILITY_ENCRYPTION 10012 // =============================================================================== // priority definition diff --git a/src/services.c b/src/services.c index 1c48905..d2d1500 100644 --- a/src/services.c +++ b/src/services.c @@ -948,6 +948,10 @@ static void get_capability(int fd, void *cookie) { offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE, "rootonoff_support", g_capabilities.rootonoff_support); + // Encryption support + offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE, + "encryption_support", g_capabilities.encryption_support); + // Zone support offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE, "zone_support", g_capabilities.zone_support); diff --git a/src/sockets.c b/src/sockets.c index 574820a..801ff42 100644 --- a/src/sockets.c +++ b/src/sockets.c @@ -317,7 +317,17 @@ static void local_socket_event_func(int fd, unsigned ev, void *_s) if(ev & FDE_READ){ apacket *p = get_apacket(); unsigned char *x = p->data; - size_t avail = MAX_PAYLOAD; +#ifdef SUPPORT_ENCRYPT + // sdb.c:536에서 sdb server의 패킷은 MAX_PAYLOAD-100으로 정하여서, + // sdb server에서 패킷 데이터의 크기를 MAX_PAYLOAD-100보다 작은 지를 체크함. + // sdbd에서 패킷 데이터를 MAX_PAYLOAD - 200로 잡아서 암호화 하게되면 + // 최대 MAX_PAYLOAD - 100 크기의 패킷을 생성하게 됨. + const size_t max_payload = MAX_PAYLOAD - 200; + size_t avail = max_payload; +#else + size_t avail = MAX_PAYLOAD; +#endif + int r = 0; int is_eof = 0; @@ -340,11 +350,18 @@ static void local_socket_event_func(int fd, unsigned ev, void *_s) } D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n", s->id, s->fd, r, is_eof, s->fde.force_eof); +#ifdef SUPPORT_ENCRYPT + //변경된 최대 패킷 크기로 코드 수정 + if((avail == max_payload) || (s->peer == 0)) { + put_apacket(p); + } else { + p->len = max_payload - avail; +#else if((avail == MAX_PAYLOAD) || (s->peer == 0)) { put_apacket(p); } else { p->len = MAX_PAYLOAD - avail; - +#endif r = s->peer->enqueue(s->peer, p); D("LS(%d): fd=%d post peer->enqueue(). r=%d\n", s->id, s->fd, r); diff --git a/src/transport.c b/src/transport.c index 7e2af39..bc471d6 100644 --- a/src/transport.c +++ b/src/transport.c @@ -27,6 +27,10 @@ #include "sdb.h" +#ifdef SUPPORT_ENCRYPT +#include "transport_security.h" +#endif + static void transport_unref(atransport *t); static atransport transport_list = { @@ -286,6 +290,15 @@ static void *output_thread(void *_t) if(t->read_from_remote(p, t) == 0){ D("%s: received remote packet, sending to transport\n", t->serial); + +#ifdef SUPPORT_ENCRYPT + if (t->encryption == ENCR_ON && p->msg.command != A_ENCR) // 현재 연결이 암호화 모드이고, 암호화 관련 메시지가 아닌 경우, 메시지 복호화 + { + security_decrypt(t->sessionID, p); + } + +#endif + if(write_packet(t->fd, t->serial, &p)){ put_apacket(p); D("%s: failed to write apacket to transport\n", t->serial); @@ -355,6 +368,19 @@ static void *input_thread(void *_t) } else { if(active) { D("%s: transport got packet, sending to remote\n", t->serial); + +#ifdef SUPPORT_ENCRYPT + if (t->encryption == ENCR_ON && p->msg.command != A_ENCR) // 현재 연결이 암호화 모드이고, 암호화 관련 메시지가 아닌 경우, 메시지를 암호화 + { + security_encrypt(t->sessionID, p); + } + else if(t->encryption == ENCR_OFF) + { + + } + +#endif + t->write_to_remote(p, t); } else { D("%s: transport ignoring packet while offline\n", t->serial); diff --git a/src/transport_security.c b/src/transport_security.c new file mode 100644 index 0000000..37d3b58 --- /dev/null +++ b/src/transport_security.c @@ -0,0 +1,261 @@ +#include +#include "transport_security.h" + +#define LOG_TAG "SDBD" +#include + +#define SAKEP_AKE_MSG_RECORD_FIXED_LEN 36 +#define SAKEP_AES_ECB_ADDED_PADDING_SIZE 16 + +extern void* g_plugin_handle; + +typedef int (*SDBD_PLUGIN_CMD_SECURITY_INIT_PROC_PTR)(const int nID, const char* pUserID); +typedef int (*SDBD_PLUGIN_CMD_SECURITY_DEINIT_PROC_PTR)(const int nID); +typedef int (*SDBD_PLUGIN_CMD_SECURITY_PARSE_SERVER_HELLO_PROC_PTR)(const int nID, unsigned char* pSrc, unsigned int* nSrcLen); +typedef int (*SDBD_PLUGIN_CMD_SECURITY_GEN_CLIENT_HELLO_PROC_PTR)(const int nID, unsigned char* pSrc, unsigned int* nSrcLen); +typedef int (*SDBD_PLUGIN_CMD_SECURITY_PARSE_SERVER_ACK_PROC_PTR)(const int nID, unsigned char* pSrc, unsigned int* nSrcLen); +typedef int (*SDBD_PLUGIN_CMD_SECURITY_GEN_CLIENT_ACK_PROC_PTR)(const int nID, unsigned char* pSrc, unsigned int* nSrcLen); +typedef int (*SDBD_PLUGIN_CMD_SECURITY_ENCRYPT_PROC_PTR)(const int nID, const unsigned char* pSrc, const unsigned int nSrcLen, + unsigned char* pDst, unsigned int* pnDstLen); +typedef int (*SDBD_PLUGIN_CMD_SECURITY_DECRYPT_PROC_PTR)(const int nID, const unsigned char* pSrc, const unsigned int nSrcLen, + unsigned char* pDst, unsigned int* pnDstLen); + +SDBD_PLUGIN_CMD_SECURITY_INIT_PROC_PTR sdbd_plugin_cmd_security_init = NULL; +SDBD_PLUGIN_CMD_SECURITY_DEINIT_PROC_PTR sdbd_plugin_cmd_security_deinit = NULL; +SDBD_PLUGIN_CMD_SECURITY_PARSE_SERVER_HELLO_PROC_PTR sdbd_plugin_cmd_security_parse_server_hello = NULL; +SDBD_PLUGIN_CMD_SECURITY_GEN_CLIENT_HELLO_PROC_PTR sdbd_plugin_cmd_security_gen_client_hello = NULL; +SDBD_PLUGIN_CMD_SECURITY_PARSE_SERVER_ACK_PROC_PTR sdbd_plugin_cmd_security_parse_server_ack = NULL; +SDBD_PLUGIN_CMD_SECURITY_GEN_CLIENT_ACK_PROC_PTR sdbd_plugin_cmd_security_gen_client_ack = NULL; +SDBD_PLUGIN_CMD_SECURITY_ENCRYPT_PROC_PTR sdbd_plugin_cmd_security_encrypt = NULL; +SDBD_PLUGIN_CMD_SECURITY_DECRYPT_PROC_PTR sdbd_plugin_cmd_security_decrypt = NULL; + +#define SDBD_PLUGIN_CMD_SECURITY_INIT_INTF "sdbd_plugin_cmd_security_init" +#define SDBD_PLUGIN_CMD_SECURITY_DEINIT_INTF "sdbd_plugin_cmd_security_deinit" +#define SDBD_PLUGIN_CMD_SECURITY_PARSE_SERVER_HELLO_INTF "sdbd_plugin_cmd_security_parse_server_hello" +#define SDBD_PLUGIN_CMD_SECURITY_GEN_CLIENT_HELLO_INTF "sdbd_plugin_cmd_security_gen_client_hello" +#define SDBD_PLUGIN_CMD_SECURITY_PARSE_SERVER_ACK_INTF "sdbd_plugin_cmd_security_parse_server_ack" +#define SDBD_PLUGIN_CMD_SECURITY_GEN_CLIENT_ACK_INTF "sdbd_plugin_cmd_security_gen_client_ack" +#define SDBD_PLUGIN_CMD_SECURITY_ENCRYPT_INTF "sdbd_plugin_cmd_security_encrypt" +#define SDBD_PLUGIN_CMD_SECURITY_DECRYPT_INTF "sdbd_plugin_cmd_security_decrypt" + +int load_sdbd_plugin_security() { + + if( sdbd_plugin_cmd_security_init == NULL ) { + LOGI("sdbd_plugin_cmd_security_init == NULL, dlsym sdbd_plugin_cmd_security_init"); + sdbd_plugin_cmd_security_init = dlsym(g_plugin_handle, SDBD_PLUGIN_CMD_SECURITY_INIT_INTF); + if( sdbd_plugin_cmd_security_init == NULL ) { + LOGI("sdbd_plugin_cmd_security_init == NULL, dlerror = [%s]", dlerror()); + } + } + LOGI("sdbd_plugin_cmd_security_init = [0x%p]", sdbd_plugin_cmd_security_init); + + + if( sdbd_plugin_cmd_security_deinit == NULL ) { + LOGI("sdbd_plugin_cmd_security_deinit == NULL, dlsym sdbd_plugin_cmd_security_deinit\n"); + sdbd_plugin_cmd_security_deinit = dlsym(g_plugin_handle, SDBD_PLUGIN_CMD_SECURITY_DEINIT_INTF); + if( sdbd_plugin_cmd_security_deinit == NULL ) { + LOGI("sdbd_plugin_cmd_security_deinit == NULL, dlerror = [%s]\n", dlerror()); + } + } + LOGI("sdbd_plugin_cmd_security_deinit = [0x%p]\n", sdbd_plugin_cmd_security_deinit); +// + if( sdbd_plugin_cmd_security_parse_server_hello == NULL ) { + LOGI("sdbd_plugin_cmd_security_parse_server_hello == NULL, dlsym sdbd_plugin_cmd_security_parse_server_hello\n"); + sdbd_plugin_cmd_security_parse_server_hello = dlsym(g_plugin_handle, SDBD_PLUGIN_CMD_SECURITY_PARSE_SERVER_HELLO_INTF); + if( sdbd_plugin_cmd_security_parse_server_hello == NULL ) { + LOGI("sdbd_plugin_cmd_security_parse_server_hello == NULL, dlerror = [%s]\n", dlerror()); + } + } + LOGI("sdbd_plugin_cmd_security_parse_server_hello = [0x%p]\n", sdbd_plugin_cmd_security_parse_server_hello); +// + if( sdbd_plugin_cmd_security_gen_client_hello == NULL ) { + LOGI("sdbd_plugin_cmd_security_gen_client_hello == NULL, dlsym sdbd_plugin_cmd_security_gen_client_hello\n"); + sdbd_plugin_cmd_security_gen_client_hello = dlsym(g_plugin_handle, SDBD_PLUGIN_CMD_SECURITY_GEN_CLIENT_HELLO_INTF); + if( sdbd_plugin_cmd_security_gen_client_hello == NULL ) { + LOGI("sdbd_plugin_cmd_security_gen_client_hello == NULL, dlerror = [%s]\n", dlerror()); + } + } + LOGI("sdbd_plugin_cmd_security_gen_client_hello = [0x%p]\n", sdbd_plugin_cmd_security_gen_client_hello); +// + if( sdbd_plugin_cmd_security_parse_server_ack == NULL ) { + LOGI("sdbd_plugin_cmd_security_parse_server_ack == NULL, dlsym sdbd_plugin_cmd_security_parse_server_ack\n"); + sdbd_plugin_cmd_security_parse_server_ack = dlsym(g_plugin_handle, SDBD_PLUGIN_CMD_SECURITY_PARSE_SERVER_ACK_INTF); + if( sdbd_plugin_cmd_security_parse_server_ack == NULL ) { + LOGI("sdbd_plugin_cmd_security_parse_server_ack == NULL, dlerror = [%s]\n", dlerror()); + } + } + LOGI("sdbd_plugin_cmd_security_parse_server_ack = [0x%p]\n", sdbd_plugin_cmd_security_parse_server_ack); +// + if( sdbd_plugin_cmd_security_gen_client_ack == NULL ) { + LOGI("sdbd_plugin_cmd_security_gen_client_ack == NULL, dlsym sdbd_plugin_cmd_security_gen_client_ack\n"); + sdbd_plugin_cmd_security_gen_client_ack = dlsym(g_plugin_handle, SDBD_PLUGIN_CMD_SECURITY_GEN_CLIENT_ACK_INTF); + if( sdbd_plugin_cmd_security_gen_client_ack == NULL ) { + LOGI("sdbd_plugin_cmd_security_gen_client_ack == NULL, dlerror = [%s]\n", dlerror()); + } + } + LOGI("sdbd_plugin_cmd_security_gen_client_ack = [0x%p]\n", sdbd_plugin_cmd_security_gen_client_ack); + + if( sdbd_plugin_cmd_security_encrypt == NULL ) { + LOGI("sdbd_plugin_cmd_security_encrypt == NULL, dlsym sdbd_plugin_cmd_security_encrypt"); + sdbd_plugin_cmd_security_encrypt = dlsym(g_plugin_handle, SDBD_PLUGIN_CMD_SECURITY_ENCRYPT_INTF); + if( sdbd_plugin_cmd_security_encrypt == NULL ) { + LOGI("sdbd_plugin_cmd_security_encrypt == NULL, dlerror = [%s]", dlerror()); + } + } + LOGI("sdbd_plugin_cmd_security_encrypt = [0x%p]", sdbd_plugin_cmd_security_encrypt); + + if( sdbd_plugin_cmd_security_decrypt == NULL ) { + LOGI("sdbd_plugin_cmd_security_decrypt == NULL, dlsym sdbd_plugin_cmd_security_decrypt"); + sdbd_plugin_cmd_security_decrypt = dlsym(g_plugin_handle, SDBD_PLUGIN_CMD_SECURITY_DECRYPT_INTF); + if( sdbd_plugin_cmd_security_decrypt == NULL ) { + LOGI("sdbd_plugin_cmd_security_decrypt == NULL, dlerror = [%s]", dlerror()); + } + } + LOGI("sdbd_plugin_cmd_security_decrypt = [0x%p]", sdbd_plugin_cmd_security_decrypt); + + return 1; +} + + +int security_init(const int nSessionID, const char* pUserID) { + + if( sdbd_plugin_cmd_security_init == NULL ) { + LOGI("sdbd_plugin_cmd_security_init == NULL, return 0"); + return 0; + } + + return sdbd_plugin_cmd_security_init(nSessionID, pUserID); +} + +int security_deinit(const int nSessionID) { + if( sdbd_plugin_cmd_security_deinit == NULL ) { + LOGI("sdbd_plugin_cmd_security_deinit == NULL, return 0\n"); + return 0; + } + + return sdbd_plugin_cmd_security_deinit(nSessionID); +} + + +int security_parse_server_hello(const int nSessionID, apacket* pApacket){ + if( sdbd_plugin_cmd_security_parse_server_hello == NULL ) { + LOGI("sdbd_plugin_cmd_security_parse_server_hello == NULL, return 0\n"); + return 0; + } + if( pApacket == NULL ) { + LOGI("pApacket == NULL, return 0\n"); + return 0; + } + + if( 0 == sdbd_plugin_cmd_security_parse_server_hello(nSessionID, pApacket->data, &pApacket->msg.data_length) ) { + LOGI("sdbd_plugin_cmd_security_parse_server_hello return 0\n"); + return 0; + } + return 1; +} + +int security_gen_client_hello(const int nSessionID, apacket* pApacket){ + if( sdbd_plugin_cmd_security_gen_client_hello == NULL ) { + LOGI("sdbd_plugin_cmd_security_gen_client_hello == NULL, return 0\n"); + return 0; + } + if( pApacket == NULL ) { + LOGI("pApacket == NULL, return 0\n"); + return 0; + } + + if( 0 == sdbd_plugin_cmd_security_gen_client_hello(nSessionID, pApacket->data, &pApacket->msg.data_length) ) { + LOGI("sdbd_plugin_cmd_security_gen_client_hello return 0\n"); + return 0; + } + return 1; +} + +int security_parse_server_ack(const int nSessionID, apacket* pApacket){ + if( sdbd_plugin_cmd_security_parse_server_ack == NULL ) { + LOGI("sdbd_plugin_cmd_security_parse_server_ack == NULL, return 0\n"); + return 0; + } + if( pApacket == NULL ) { + LOGI("pApacket == NULL, return 0\n"); + return 0; + } + + if( 0 == sdbd_plugin_cmd_security_parse_server_ack(nSessionID, pApacket->data, &pApacket->msg.data_length) ) { + LOGI("sdbd_plugin_cmd_security_parse_server_ack return 0\n"); + return 0; + } + return 1; +} + +int security_gen_client_ack(const int nSessionID, apacket* pApacket){ + if( sdbd_plugin_cmd_security_gen_client_ack == NULL ) { + LOGI("sdbd_plugin_cmd_security_gen_client_ack == NULL, return 0\n"); + return 0; + } + if( pApacket == NULL ) { + LOGI("pApacket == NULL, return 0\n"); + return 0; + } + + if( 0 == sdbd_plugin_cmd_security_gen_client_ack(nSessionID, pApacket->data, &pApacket->msg.data_length) ) { + LOGI("sdbd_plugin_cmd_security_gen_client_ack return 0\n"); + return 0; + } + return 1; +} + + +int security_encrypt(const int nSessionID, apacket* pApacket) { + + if( pApacket == NULL ) { + LOGI("pApacket == NULL, return 0"); + return 0; + } + + unsigned char *szTemp; + szTemp = (unsigned char *)malloc(pApacket->msg.data_length + SAKEP_AKE_MSG_RECORD_FIXED_LEN + SAKEP_AES_ECB_ADDED_PADDING_SIZE); + memset(szTemp, 0x00, pApacket->msg.data_length + SAKEP_AKE_MSG_RECORD_FIXED_LEN + SAKEP_AES_ECB_ADDED_PADDING_SIZE); + + unsigned int nDstLen = 0; + if( 0 == sdbd_plugin_cmd_security_encrypt(nSessionID, pApacket->data, pApacket->msg.data_length, szTemp, &nDstLen) ) { + LOGI("sdbd_plugin_cmd_security_encrypt return 0"); + return 0; + } + + int i=0; + for(i=0 ; idata[i] = szTemp[i]; + } + + pApacket->msg.data_length = nDstLen; + free(szTemp); + return 1; + +} + +int security_decrypt(const int nSessionID, apacket* pApacket) { + + if( pApacket == NULL ) { + LOGI("pApacket == NULL, return 0"); + return 0; + } + + unsigned char *szTemp; + szTemp = (unsigned char *)malloc(pApacket->msg.data_length); + memset(szTemp, 0x00, pApacket->msg.data_length); + unsigned int nDstLen = 0; + if( 0 == sdbd_plugin_cmd_security_decrypt(nSessionID, pApacket->data, pApacket->msg.data_length, szTemp, &nDstLen) ) { + LOGI("sdbd_plugin_cmd_security_decrypt return 0"); + return 0; + } + + int i = 0; + for(i=0 ; idata[i] = szTemp[i]; + } + + pApacket->msg.data_length = nDstLen; + free(szTemp); + return 1; +} diff --git a/src/transport_security.h b/src/transport_security.h new file mode 100644 index 0000000..12fd689 --- /dev/null +++ b/src/transport_security.h @@ -0,0 +1,18 @@ +#ifndef __TRANSPORT_SECURITY_H__ +#define __TRANSPORT_SECURITY_H__ + +#include +#include "sdb.h" + +int load_sdbd_plugin_security(); + +int security_init(const int nID, const char* pUserID); +int security_deinit(const int nSessionID); +int security_parse_server_hello(const int nSessionID, apacket* pApacket); +int security_gen_client_hello(const int nSessionID, apacket* pApacket); +int security_parse_server_ack(const int nSessionID, apacket* pApacket); +int security_gen_client_ack(const int nSessionID, apacket* pApacket); +int security_encrypt(const int nID, apacket* pApacket); +int security_decrypt(const int nID, apacket* pApacket); + +#endif -- 2.7.4 From e5cb6422d20125dc1896e7d8838c6e7e8b77ab86 Mon Sep 17 00:00:00 2001 From: Sangjin Kim Date: Wed, 9 Nov 2016 04:03:41 -0800 Subject: [PATCH 16/16] Revert "add data encryption feature" This reverts commit 89745858c0ca69210ece6e794d980fc7bfc51f96. Change-Id: Id0fb6cab639e885c802fd956de1bdab820cc8d47 --- CMakeLists.txt | 3 - packaging/sdbd.spec | 1 - src/plugin.c | 22 ++-- src/sdb.c | 168 +----------------------------- src/sdb.h | 18 ---- src/sdbd_plugin.h | 1 - src/services.c | 4 - src/sockets.c | 21 +--- src/transport.c | 26 ----- src/transport_security.c | 261 ----------------------------------------------- src/transport_security.h | 18 ---- 11 files changed, 14 insertions(+), 529 deletions(-) delete mode 100644 src/transport_security.c delete mode 100644 src/transport_security.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 47edfd9..6c88b77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,7 +58,6 @@ SET(SDBD_SRCS src/default_plugin_appcmd.c src/hashtable.c src/plugin.c - src/transport_security.c ) include(FindPkgConfig) @@ -70,7 +69,6 @@ pkg_check_modules(pkgs REQUIRED glib-2.0 dbus-1 dbus-glib-1 - dlog ) FOREACH(flag ${pkgs_CFLAGS}) @@ -88,7 +86,6 @@ ADD_DEFINITIONS("-D_GNU_SOURCE") ADD_DEFINITIONS("-DHAVE_FORKEXEC") ADD_DEFINITIONS("-D_DROP_PRIVILEGE") ADD_DEFINITIONS("-D_FILE_OFFSET_BITS=64") -ADD_DEFINITIONS("-DSUPPORT_ENCRYPT") IF (_ARM_TARGET) ADD_DEFINITIONS("-DANDROID_GADGET=1") diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index bc9408f..1cf6758 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -25,7 +25,6 @@ BuildRequires: pkgconfig(vconf) BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(dbus-1) BuildRequires: pkgconfig(dbus-glib-1) -BuildRequires: pkgconfig(dlog) Requires: dbus %description diff --git a/src/plugin.c b/src/plugin.c index bee7aa4..e8c70b9 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -27,7 +27,7 @@ #include "plugin.h" #include "sdbd_plugin.h" -void* g_plugin_handle = NULL; +static void* plugin_handle = NULL; PLUGIN_INIT plugin_init_proc = NULL; PLUGIN_SYNCHRONOUS_CMD_PROC plugin_sync_proc = NULL; @@ -89,28 +89,28 @@ static int load_plugin_not_default() plugin_sync_proc = NULL; plugin_async_proc = NULL; - g_plugin_handle = dlopen ( PLUGIN_PATH, RTLD_NOW ); - if ( g_plugin_handle == NULL ) { + plugin_handle = dlopen ( PLUGIN_PATH, RTLD_NOW ); + if ( plugin_handle == NULL ) { D ( "failed to dlopen(%s). error: %s\n", PLUGIN_PATH, dlerror() ); return 0; } - plugin_init_proc = dlsym ( g_plugin_handle, PLUGIN_PROC_NAME_INIT ); + plugin_init_proc = dlsym ( plugin_handle, PLUGIN_PROC_NAME_INIT ); if ( plugin_init_proc == NULL ) { D ( "failed to get the sdbd plugin init function. error: %s\n", dlerror() ); - dlclose ( g_plugin_handle ); - g_plugin_handle = NULL; + dlclose ( plugin_handle ); + plugin_handle = NULL; return 0; } // if there is no implementation of plugin_sync_proc and plugin_async_proc, // then use default_plugin_sync_proc and default_plugin_async_proc - plugin_sync_proc = dlsym ( g_plugin_handle, PLUGIN_PROC_NAME_SYNC_CMD ); + plugin_sync_proc = dlsym ( plugin_handle, PLUGIN_PROC_NAME_SYNC_CMD ); if ( plugin_sync_proc == NULL ) { plugin_sync_proc = default_plugin_sync_proc; } - plugin_async_proc = dlsym ( g_plugin_handle, PLUGIN_PROC_NAME_ASYNC_CMD ); + plugin_async_proc = dlsym ( plugin_handle, PLUGIN_PROC_NAME_ASYNC_CMD ); if ( plugin_async_proc == NULL ) { plugin_async_proc = default_plugin_async_proc; } @@ -149,9 +149,9 @@ void unload_sdbd_plugin() plugin_cmd_hashtable = NULL; } - if ( g_plugin_handle ) { - dlclose ( g_plugin_handle ); - g_plugin_handle = NULL; + if ( plugin_handle ) { + dlclose ( plugin_handle ); + plugin_handle = NULL; } } diff --git a/src/sdb.c b/src/sdb.c index 50cf9ed..9a0d5cf 100644 --- a/src/sdb.c +++ b/src/sdb.c @@ -44,10 +44,6 @@ #include "plugin.h" #include "sdbd_plugin.h" -#ifdef SUPPORT_ENCRYPT -#include "transport_security.h" -#endif - #if !SDB_HOST #include #define SDB_PIDPATH "/tmp/.sdbd.pid" @@ -372,147 +368,6 @@ void print_packet(const char *label, apacket *p) } #endif -#ifdef SUPPORT_ENCRYPT -/* -desc. : 암호화 실패 메시지 전송 -parameter : [in] apacket* p : sdbd로 들어온 메시지 - [in] atransport *t : 현재 연결에 대한 atransport - [in] unsigned failed_value : 실패 값 -*/ -void send_encr_fail(apacket* p, atransport *t, unsigned failed_value){ - apacket* enc_p; - enc_p = get_apacket(); - enc_p->msg.command = A_ENCR; // 암호화 메시지 - enc_p->msg.arg0 = failed_value; // 실패값 - enc_p->msg.arg1 = p->msg.arg1; - send_packet(enc_p, t); - //put_apacket(enc_p); -} - -/* -desc. : 암호화 메시지 핸들링 -parameter : [in] apacket* p : sdbd로 들어온 메시지 - [in/out] apacket* enc_p : sdb server로 전송할 메시지 - [in/out] atransport *t : 현재 연결에 대한 atransport -ret : 0 : 정상적으로 메시지 전송 - -1: 메시지 전송 실패 -*/ -int handle_encr_packet(apacket* p, apacket* enc_p, atransport *t){ - static int sessionID = 0; - int retVal = 0; - if(p->msg.arg0 == ENCR_SET_ON_REQ){ // hello 메시지인 경우 - t->sessionID = sessionID; - if((retVal = security_init(t->sessionID, NULL)) == 1){ // 암호화 handshaking을 위한 init - if(security_parse_server_hello(t->sessionID, p) == 1){ // hello 메시지 파싱 - D("security_parse_server_hello success\n"); - if(security_gen_client_hello(t->sessionID, enc_p) == 1){ // hello 메시지 생성 - D("security_gen_client_hello success\n"); - enc_p->msg.command = A_ENCR; - enc_p->msg.arg0 = ENCR_SET_ON_REQ; - enc_p->msg.arg1 = p->msg.arg1; - sessionID++; - send_packet(enc_p, t); - } - else { // hello 메시지 생성 실패 - D("security_gen_client_hello error\n"); - send_encr_fail(p, t, ENCR_ON_FAIL); // 암호화 on 실패 메시지 전송 - t->encryption = ENCR_OFF; // 암호화 모드는 off - security_deinit(t->sessionID); - return -1; - } - } - else{ // hello 메시지 파싱 실패 - D("security_parse_server_hello error\n"); - send_encr_fail(p, t, ENCR_ON_FAIL); - t->encryption = ENCR_OFF; - security_deinit(t->sessionID); - - return -1; - } - } else { // init 실패 - D("security_init error\n"); - send_encr_fail(p, t, ENCR_ON_FAIL); - t->encryption = ENCR_OFF; - if (retVal == -1) - { - security_deinit(t->sessionID); - } - //here!! do security_deinit(), but when plugin pointer is null -> not deinit - return -1; - } - } - else if(p->msg.arg0 == ENCR_SET_ON_OK){ // ack 메시지인 경우 - if(security_parse_server_ack(t->sessionID, p) == 1){ // ack 메시지 파싱 - if(security_gen_client_ack(t->sessionID, enc_p) == 1){ // ack 메시지 생성 - D("security_gen_client_ack success\n"); - enc_p->msg.command = A_ENCR; - enc_p->msg.arg0 = ENCR_SET_ON_OK; - enc_p->msg.arg1 = p->msg.arg1; - t->encryption = ENCR_ON; - send_packet(enc_p, t); - } - else { // ack 메시지 생성에 실패한 경우 - D("security_gen_client_ack error\n"); - send_encr_fail(p, t, ENCR_ON_FAIL); - t->encryption = ENCR_OFF; - security_deinit(t->sessionID); - return -1; - } - } - else { // ack 메시지 파싱에 실패한 경우 - D("security_parse_server_ack error\n"); - send_encr_fail(p, t, ENCR_ON_FAIL); - t->encryption = ENCR_OFF; - security_deinit(t->sessionID); - return -1; - } - } - else if(p->msg.arg0 == ENCR_SET_OFF){ // 암호화 모드 off 요청 메시지 - if(t->encryption == ENCR_ON && security_deinit(t->sessionID) == 1){ // 현재 암호화 모드가 on 상태인 경우 - enc_p = get_apacket(); - t->encryption = ENCR_OFF; // 현재 연결에 대한 암호화 모드 off - enc_p->msg.command = A_ENCR; - enc_p->msg.arg0 = ENCR_SET_OFF; - enc_p->msg.arg1 = p->msg.arg1; - send_packet(enc_p, t); - } - else { // 암호화 모드 off에 실패한 경우 - D("security_deinit error\n"); - send_encr_fail(p, t, ENCR_OFF_FAIL); // 암호화 모드 off 실패 메시지 전송 - return -1; - } - } - else if(p->msg.arg0 == ENCR_GET){ // 암호화 모드의 상태 요청 메시지인 경우 - enc_p = get_apacket(); - enc_p->msg.command = A_ENCR; - enc_p->msg.arg0 = ENCR_GET; // 암호화 모드 status get메시지 - enc_p->msg.arg1 = p->msg.arg1; - if(t->encryption == ENCR_ON){ // 암호화 모드가 on인 경우 - enc_p->msg.data_length = 13; - strncpy((char*)enc_p->data, "encryption:on", enc_p->msg.data_length); // encryption:on 메시지 전송 - } else if(t->encryption == ENCR_OFF){ // 암호화 모드가 off인 경우 - enc_p->msg.data_length = 14; - strncpy((char*)enc_p->data, "encryption:off", enc_p->msg.data_length); // encryption:off 메시지 전송 - } - send_packet(enc_p, t); - } - else if (p->msg.arg0 == ENCR_ON_FAIL) // 암호화 모드를 on 하는 도중 실패한 경우 받는 메시지 - { - t->encryption = ENCR_OFF; // 암호화 모드를 다시 off - D("encryption on failed\n"); - } - else if (p->msg.arg0 == ENCR_OFF_FAIL) // 암호화 모드를 off하는 도중 실패한 경우 받는 메시지 - { - //t->encryption = ENCR_ON; - D("encryption off failed\n"); - } - //put_apacket(enc_p); - return 0; - -} -#endif - - static void send_ready(unsigned local, unsigned remote, atransport *t) { D("Calling send_ready \n"); @@ -539,11 +394,8 @@ static void send_connect(atransport *t) apacket *cp = get_apacket(); cp->msg.command = A_CNXN; cp->msg.arg0 = A_VERSION; -#ifdef SUPPORT_ENCRYPT - cp->msg.arg1 = MAX_PAYLOAD - 100; // connection 시, sdb server의 패킷 크기를 암호화 오버로드 만큼 줄임 -#else cp->msg.arg1 = MAX_PAYLOAD; -#endif + char device_name[256]={0,}; int r = 0; int status = 0; @@ -887,15 +739,6 @@ void handle_packet(apacket *p, atransport *t) } } break; -#ifdef SUPPORT_ENCRYPT - case A_ENCR: // 암호화 메시지인 경우 - if(t->connection_state != CS_OFFLINE) { - apacket* enc_p = get_apacket(); - handle_encr_packet(p, enc_p, t); - //put_apacket(enc_p); - } - break; -#endif default: printf("handle_packet: what is %08x?!\n", p->msg.command); @@ -1849,15 +1692,6 @@ static void init_capabilities(void) { } - // Encryption support - if(!request_capability_to_plugin(CAPABILITY_ENCRYPTION, g_capabilities.encryption_support, - sizeof(g_capabilities.encryption_support))) { - D("failed to request. (%d:%d) \n", PLUGIN_SYNC_CMD_CAPABILITY, CAPABILITY_ENCRYPTION); - snprintf(g_capabilities.encryption_support, sizeof(g_capabilities.encryption_support), - "%s", DISABLED); - } - - // Zone support ret = is_container_enabled(); snprintf(g_capabilities.zone_support, sizeof(g_capabilities.zone_support), diff --git a/src/sdb.h b/src/sdb.h index 348a7eb..fec1a5d 100644 --- a/src/sdb.h +++ b/src/sdb.h @@ -37,18 +37,6 @@ #define A_CLSE 0x45534c43 #define A_WRTE 0x45545257 #define A_STAT 0x54415453 -#define A_ENCR 0x40682018 // encryption 메시지 - -#ifdef SUPPORT_ENCRYPT - #define ENCR_SET_ON_REQ 0 // encryption hello 메시지 - #define ENCR_SET_ON_OK 1 // encryption ack 메시지 - #define ENCR_SET_OFF 2 // encryption mode off 메시지 - #define ENCR_GET 3 // encryption status get 메시지 - #define ENCR_ON_FAIL 4 // encryption on 실패 메시지 - #define ENCR_OFF_FAIL 5 // encryption off 실패 메시지 - #define ENCR_ON 1 // encryption on 상태 - #define ENCR_OFF 0 // encryption off 상태 -#endif #define A_VERSION 0x02000000 // SDB protocol version @@ -212,11 +200,6 @@ struct atransport /* a list of adisconnect callbacks called when the transport is kicked */ int kicked; adisconnect disconnects; - -#ifdef SUPPORT_ENCRYPT - unsigned encryption; // 해당 연결이 암호화 모드인지 확인하는 flag , 0 = no-encryption / 1 = encryption - int sessionID; // 암호화 세션 ID, 암호화 map에 대한 key -#endif }; @@ -274,7 +257,6 @@ typedef struct platform_capabilities char usbproto_support[CAPBUF_ITEMSIZE]; // enabled or disabled char sockproto_support[CAPBUF_ITEMSIZE]; // enabled or disabled char appcmd_support[CAPBUF_ITEMSIZE]; // enabled or disabled - char encryption_support[CAPBUF_ITEMSIZE]; // enabled or disabled char log_enable[CAPBUF_ITEMSIZE]; // enabled or disabled char log_path[CAPBUF_LL_ITEMSIZE]; // path of sdbd log diff --git a/src/sdbd_plugin.h b/src/sdbd_plugin.h index bcaaa8d..2bfb450 100644 --- a/src/sdbd_plugin.h +++ b/src/sdbd_plugin.h @@ -63,7 +63,6 @@ #define CAPABILITY_LOG_ENABLE 10009 #define CAPABILITY_LOG_PATH 10010 #define CAPABILITY_APPCMD 10011 -#define CAPABILITY_ENCRYPTION 10012 // =============================================================================== // priority definition diff --git a/src/services.c b/src/services.c index d2d1500..1c48905 100644 --- a/src/services.c +++ b/src/services.c @@ -948,10 +948,6 @@ static void get_capability(int fd, void *cookie) { offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE, "rootonoff_support", g_capabilities.rootonoff_support); - // Encryption support - offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE, - "encryption_support", g_capabilities.encryption_support); - // Zone support offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE, "zone_support", g_capabilities.zone_support); diff --git a/src/sockets.c b/src/sockets.c index 801ff42..574820a 100644 --- a/src/sockets.c +++ b/src/sockets.c @@ -317,17 +317,7 @@ static void local_socket_event_func(int fd, unsigned ev, void *_s) if(ev & FDE_READ){ apacket *p = get_apacket(); unsigned char *x = p->data; -#ifdef SUPPORT_ENCRYPT - // sdb.c:536에서 sdb server의 패킷은 MAX_PAYLOAD-100으로 정하여서, - // sdb server에서 패킷 데이터의 크기를 MAX_PAYLOAD-100보다 작은 지를 체크함. - // sdbd에서 패킷 데이터를 MAX_PAYLOAD - 200로 잡아서 암호화 하게되면 - // 최대 MAX_PAYLOAD - 100 크기의 패킷을 생성하게 됨. - const size_t max_payload = MAX_PAYLOAD - 200; - size_t avail = max_payload; -#else - size_t avail = MAX_PAYLOAD; -#endif - + size_t avail = MAX_PAYLOAD; int r = 0; int is_eof = 0; @@ -350,18 +340,11 @@ static void local_socket_event_func(int fd, unsigned ev, void *_s) } D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n", s->id, s->fd, r, is_eof, s->fde.force_eof); -#ifdef SUPPORT_ENCRYPT - //변경된 최대 패킷 크기로 코드 수정 - if((avail == max_payload) || (s->peer == 0)) { - put_apacket(p); - } else { - p->len = max_payload - avail; -#else if((avail == MAX_PAYLOAD) || (s->peer == 0)) { put_apacket(p); } else { p->len = MAX_PAYLOAD - avail; -#endif + r = s->peer->enqueue(s->peer, p); D("LS(%d): fd=%d post peer->enqueue(). r=%d\n", s->id, s->fd, r); diff --git a/src/transport.c b/src/transport.c index bc471d6..7e2af39 100644 --- a/src/transport.c +++ b/src/transport.c @@ -27,10 +27,6 @@ #include "sdb.h" -#ifdef SUPPORT_ENCRYPT -#include "transport_security.h" -#endif - static void transport_unref(atransport *t); static atransport transport_list = { @@ -290,15 +286,6 @@ static void *output_thread(void *_t) if(t->read_from_remote(p, t) == 0){ D("%s: received remote packet, sending to transport\n", t->serial); - -#ifdef SUPPORT_ENCRYPT - if (t->encryption == ENCR_ON && p->msg.command != A_ENCR) // 현재 연결이 암호화 모드이고, 암호화 관련 메시지가 아닌 경우, 메시지 복호화 - { - security_decrypt(t->sessionID, p); - } - -#endif - if(write_packet(t->fd, t->serial, &p)){ put_apacket(p); D("%s: failed to write apacket to transport\n", t->serial); @@ -368,19 +355,6 @@ static void *input_thread(void *_t) } else { if(active) { D("%s: transport got packet, sending to remote\n", t->serial); - -#ifdef SUPPORT_ENCRYPT - if (t->encryption == ENCR_ON && p->msg.command != A_ENCR) // 현재 연결이 암호화 모드이고, 암호화 관련 메시지가 아닌 경우, 메시지를 암호화 - { - security_encrypt(t->sessionID, p); - } - else if(t->encryption == ENCR_OFF) - { - - } - -#endif - t->write_to_remote(p, t); } else { D("%s: transport ignoring packet while offline\n", t->serial); diff --git a/src/transport_security.c b/src/transport_security.c deleted file mode 100644 index 37d3b58..0000000 --- a/src/transport_security.c +++ /dev/null @@ -1,261 +0,0 @@ -#include -#include "transport_security.h" - -#define LOG_TAG "SDBD" -#include - -#define SAKEP_AKE_MSG_RECORD_FIXED_LEN 36 -#define SAKEP_AES_ECB_ADDED_PADDING_SIZE 16 - -extern void* g_plugin_handle; - -typedef int (*SDBD_PLUGIN_CMD_SECURITY_INIT_PROC_PTR)(const int nID, const char* pUserID); -typedef int (*SDBD_PLUGIN_CMD_SECURITY_DEINIT_PROC_PTR)(const int nID); -typedef int (*SDBD_PLUGIN_CMD_SECURITY_PARSE_SERVER_HELLO_PROC_PTR)(const int nID, unsigned char* pSrc, unsigned int* nSrcLen); -typedef int (*SDBD_PLUGIN_CMD_SECURITY_GEN_CLIENT_HELLO_PROC_PTR)(const int nID, unsigned char* pSrc, unsigned int* nSrcLen); -typedef int (*SDBD_PLUGIN_CMD_SECURITY_PARSE_SERVER_ACK_PROC_PTR)(const int nID, unsigned char* pSrc, unsigned int* nSrcLen); -typedef int (*SDBD_PLUGIN_CMD_SECURITY_GEN_CLIENT_ACK_PROC_PTR)(const int nID, unsigned char* pSrc, unsigned int* nSrcLen); -typedef int (*SDBD_PLUGIN_CMD_SECURITY_ENCRYPT_PROC_PTR)(const int nID, const unsigned char* pSrc, const unsigned int nSrcLen, - unsigned char* pDst, unsigned int* pnDstLen); -typedef int (*SDBD_PLUGIN_CMD_SECURITY_DECRYPT_PROC_PTR)(const int nID, const unsigned char* pSrc, const unsigned int nSrcLen, - unsigned char* pDst, unsigned int* pnDstLen); - -SDBD_PLUGIN_CMD_SECURITY_INIT_PROC_PTR sdbd_plugin_cmd_security_init = NULL; -SDBD_PLUGIN_CMD_SECURITY_DEINIT_PROC_PTR sdbd_plugin_cmd_security_deinit = NULL; -SDBD_PLUGIN_CMD_SECURITY_PARSE_SERVER_HELLO_PROC_PTR sdbd_plugin_cmd_security_parse_server_hello = NULL; -SDBD_PLUGIN_CMD_SECURITY_GEN_CLIENT_HELLO_PROC_PTR sdbd_plugin_cmd_security_gen_client_hello = NULL; -SDBD_PLUGIN_CMD_SECURITY_PARSE_SERVER_ACK_PROC_PTR sdbd_plugin_cmd_security_parse_server_ack = NULL; -SDBD_PLUGIN_CMD_SECURITY_GEN_CLIENT_ACK_PROC_PTR sdbd_plugin_cmd_security_gen_client_ack = NULL; -SDBD_PLUGIN_CMD_SECURITY_ENCRYPT_PROC_PTR sdbd_plugin_cmd_security_encrypt = NULL; -SDBD_PLUGIN_CMD_SECURITY_DECRYPT_PROC_PTR sdbd_plugin_cmd_security_decrypt = NULL; - -#define SDBD_PLUGIN_CMD_SECURITY_INIT_INTF "sdbd_plugin_cmd_security_init" -#define SDBD_PLUGIN_CMD_SECURITY_DEINIT_INTF "sdbd_plugin_cmd_security_deinit" -#define SDBD_PLUGIN_CMD_SECURITY_PARSE_SERVER_HELLO_INTF "sdbd_plugin_cmd_security_parse_server_hello" -#define SDBD_PLUGIN_CMD_SECURITY_GEN_CLIENT_HELLO_INTF "sdbd_plugin_cmd_security_gen_client_hello" -#define SDBD_PLUGIN_CMD_SECURITY_PARSE_SERVER_ACK_INTF "sdbd_plugin_cmd_security_parse_server_ack" -#define SDBD_PLUGIN_CMD_SECURITY_GEN_CLIENT_ACK_INTF "sdbd_plugin_cmd_security_gen_client_ack" -#define SDBD_PLUGIN_CMD_SECURITY_ENCRYPT_INTF "sdbd_plugin_cmd_security_encrypt" -#define SDBD_PLUGIN_CMD_SECURITY_DECRYPT_INTF "sdbd_plugin_cmd_security_decrypt" - -int load_sdbd_plugin_security() { - - if( sdbd_plugin_cmd_security_init == NULL ) { - LOGI("sdbd_plugin_cmd_security_init == NULL, dlsym sdbd_plugin_cmd_security_init"); - sdbd_plugin_cmd_security_init = dlsym(g_plugin_handle, SDBD_PLUGIN_CMD_SECURITY_INIT_INTF); - if( sdbd_plugin_cmd_security_init == NULL ) { - LOGI("sdbd_plugin_cmd_security_init == NULL, dlerror = [%s]", dlerror()); - } - } - LOGI("sdbd_plugin_cmd_security_init = [0x%p]", sdbd_plugin_cmd_security_init); - - - if( sdbd_plugin_cmd_security_deinit == NULL ) { - LOGI("sdbd_plugin_cmd_security_deinit == NULL, dlsym sdbd_plugin_cmd_security_deinit\n"); - sdbd_plugin_cmd_security_deinit = dlsym(g_plugin_handle, SDBD_PLUGIN_CMD_SECURITY_DEINIT_INTF); - if( sdbd_plugin_cmd_security_deinit == NULL ) { - LOGI("sdbd_plugin_cmd_security_deinit == NULL, dlerror = [%s]\n", dlerror()); - } - } - LOGI("sdbd_plugin_cmd_security_deinit = [0x%p]\n", sdbd_plugin_cmd_security_deinit); -// - if( sdbd_plugin_cmd_security_parse_server_hello == NULL ) { - LOGI("sdbd_plugin_cmd_security_parse_server_hello == NULL, dlsym sdbd_plugin_cmd_security_parse_server_hello\n"); - sdbd_plugin_cmd_security_parse_server_hello = dlsym(g_plugin_handle, SDBD_PLUGIN_CMD_SECURITY_PARSE_SERVER_HELLO_INTF); - if( sdbd_plugin_cmd_security_parse_server_hello == NULL ) { - LOGI("sdbd_plugin_cmd_security_parse_server_hello == NULL, dlerror = [%s]\n", dlerror()); - } - } - LOGI("sdbd_plugin_cmd_security_parse_server_hello = [0x%p]\n", sdbd_plugin_cmd_security_parse_server_hello); -// - if( sdbd_plugin_cmd_security_gen_client_hello == NULL ) { - LOGI("sdbd_plugin_cmd_security_gen_client_hello == NULL, dlsym sdbd_plugin_cmd_security_gen_client_hello\n"); - sdbd_plugin_cmd_security_gen_client_hello = dlsym(g_plugin_handle, SDBD_PLUGIN_CMD_SECURITY_GEN_CLIENT_HELLO_INTF); - if( sdbd_plugin_cmd_security_gen_client_hello == NULL ) { - LOGI("sdbd_plugin_cmd_security_gen_client_hello == NULL, dlerror = [%s]\n", dlerror()); - } - } - LOGI("sdbd_plugin_cmd_security_gen_client_hello = [0x%p]\n", sdbd_plugin_cmd_security_gen_client_hello); -// - if( sdbd_plugin_cmd_security_parse_server_ack == NULL ) { - LOGI("sdbd_plugin_cmd_security_parse_server_ack == NULL, dlsym sdbd_plugin_cmd_security_parse_server_ack\n"); - sdbd_plugin_cmd_security_parse_server_ack = dlsym(g_plugin_handle, SDBD_PLUGIN_CMD_SECURITY_PARSE_SERVER_ACK_INTF); - if( sdbd_plugin_cmd_security_parse_server_ack == NULL ) { - LOGI("sdbd_plugin_cmd_security_parse_server_ack == NULL, dlerror = [%s]\n", dlerror()); - } - } - LOGI("sdbd_plugin_cmd_security_parse_server_ack = [0x%p]\n", sdbd_plugin_cmd_security_parse_server_ack); -// - if( sdbd_plugin_cmd_security_gen_client_ack == NULL ) { - LOGI("sdbd_plugin_cmd_security_gen_client_ack == NULL, dlsym sdbd_plugin_cmd_security_gen_client_ack\n"); - sdbd_plugin_cmd_security_gen_client_ack = dlsym(g_plugin_handle, SDBD_PLUGIN_CMD_SECURITY_GEN_CLIENT_ACK_INTF); - if( sdbd_plugin_cmd_security_gen_client_ack == NULL ) { - LOGI("sdbd_plugin_cmd_security_gen_client_ack == NULL, dlerror = [%s]\n", dlerror()); - } - } - LOGI("sdbd_plugin_cmd_security_gen_client_ack = [0x%p]\n", sdbd_plugin_cmd_security_gen_client_ack); - - if( sdbd_plugin_cmd_security_encrypt == NULL ) { - LOGI("sdbd_plugin_cmd_security_encrypt == NULL, dlsym sdbd_plugin_cmd_security_encrypt"); - sdbd_plugin_cmd_security_encrypt = dlsym(g_plugin_handle, SDBD_PLUGIN_CMD_SECURITY_ENCRYPT_INTF); - if( sdbd_plugin_cmd_security_encrypt == NULL ) { - LOGI("sdbd_plugin_cmd_security_encrypt == NULL, dlerror = [%s]", dlerror()); - } - } - LOGI("sdbd_plugin_cmd_security_encrypt = [0x%p]", sdbd_plugin_cmd_security_encrypt); - - if( sdbd_plugin_cmd_security_decrypt == NULL ) { - LOGI("sdbd_plugin_cmd_security_decrypt == NULL, dlsym sdbd_plugin_cmd_security_decrypt"); - sdbd_plugin_cmd_security_decrypt = dlsym(g_plugin_handle, SDBD_PLUGIN_CMD_SECURITY_DECRYPT_INTF); - if( sdbd_plugin_cmd_security_decrypt == NULL ) { - LOGI("sdbd_plugin_cmd_security_decrypt == NULL, dlerror = [%s]", dlerror()); - } - } - LOGI("sdbd_plugin_cmd_security_decrypt = [0x%p]", sdbd_plugin_cmd_security_decrypt); - - return 1; -} - - -int security_init(const int nSessionID, const char* pUserID) { - - if( sdbd_plugin_cmd_security_init == NULL ) { - LOGI("sdbd_plugin_cmd_security_init == NULL, return 0"); - return 0; - } - - return sdbd_plugin_cmd_security_init(nSessionID, pUserID); -} - -int security_deinit(const int nSessionID) { - if( sdbd_plugin_cmd_security_deinit == NULL ) { - LOGI("sdbd_plugin_cmd_security_deinit == NULL, return 0\n"); - return 0; - } - - return sdbd_plugin_cmd_security_deinit(nSessionID); -} - - -int security_parse_server_hello(const int nSessionID, apacket* pApacket){ - if( sdbd_plugin_cmd_security_parse_server_hello == NULL ) { - LOGI("sdbd_plugin_cmd_security_parse_server_hello == NULL, return 0\n"); - return 0; - } - if( pApacket == NULL ) { - LOGI("pApacket == NULL, return 0\n"); - return 0; - } - - if( 0 == sdbd_plugin_cmd_security_parse_server_hello(nSessionID, pApacket->data, &pApacket->msg.data_length) ) { - LOGI("sdbd_plugin_cmd_security_parse_server_hello return 0\n"); - return 0; - } - return 1; -} - -int security_gen_client_hello(const int nSessionID, apacket* pApacket){ - if( sdbd_plugin_cmd_security_gen_client_hello == NULL ) { - LOGI("sdbd_plugin_cmd_security_gen_client_hello == NULL, return 0\n"); - return 0; - } - if( pApacket == NULL ) { - LOGI("pApacket == NULL, return 0\n"); - return 0; - } - - if( 0 == sdbd_plugin_cmd_security_gen_client_hello(nSessionID, pApacket->data, &pApacket->msg.data_length) ) { - LOGI("sdbd_plugin_cmd_security_gen_client_hello return 0\n"); - return 0; - } - return 1; -} - -int security_parse_server_ack(const int nSessionID, apacket* pApacket){ - if( sdbd_plugin_cmd_security_parse_server_ack == NULL ) { - LOGI("sdbd_plugin_cmd_security_parse_server_ack == NULL, return 0\n"); - return 0; - } - if( pApacket == NULL ) { - LOGI("pApacket == NULL, return 0\n"); - return 0; - } - - if( 0 == sdbd_plugin_cmd_security_parse_server_ack(nSessionID, pApacket->data, &pApacket->msg.data_length) ) { - LOGI("sdbd_plugin_cmd_security_parse_server_ack return 0\n"); - return 0; - } - return 1; -} - -int security_gen_client_ack(const int nSessionID, apacket* pApacket){ - if( sdbd_plugin_cmd_security_gen_client_ack == NULL ) { - LOGI("sdbd_plugin_cmd_security_gen_client_ack == NULL, return 0\n"); - return 0; - } - if( pApacket == NULL ) { - LOGI("pApacket == NULL, return 0\n"); - return 0; - } - - if( 0 == sdbd_plugin_cmd_security_gen_client_ack(nSessionID, pApacket->data, &pApacket->msg.data_length) ) { - LOGI("sdbd_plugin_cmd_security_gen_client_ack return 0\n"); - return 0; - } - return 1; -} - - -int security_encrypt(const int nSessionID, apacket* pApacket) { - - if( pApacket == NULL ) { - LOGI("pApacket == NULL, return 0"); - return 0; - } - - unsigned char *szTemp; - szTemp = (unsigned char *)malloc(pApacket->msg.data_length + SAKEP_AKE_MSG_RECORD_FIXED_LEN + SAKEP_AES_ECB_ADDED_PADDING_SIZE); - memset(szTemp, 0x00, pApacket->msg.data_length + SAKEP_AKE_MSG_RECORD_FIXED_LEN + SAKEP_AES_ECB_ADDED_PADDING_SIZE); - - unsigned int nDstLen = 0; - if( 0 == sdbd_plugin_cmd_security_encrypt(nSessionID, pApacket->data, pApacket->msg.data_length, szTemp, &nDstLen) ) { - LOGI("sdbd_plugin_cmd_security_encrypt return 0"); - return 0; - } - - int i=0; - for(i=0 ; idata[i] = szTemp[i]; - } - - pApacket->msg.data_length = nDstLen; - free(szTemp); - return 1; - -} - -int security_decrypt(const int nSessionID, apacket* pApacket) { - - if( pApacket == NULL ) { - LOGI("pApacket == NULL, return 0"); - return 0; - } - - unsigned char *szTemp; - szTemp = (unsigned char *)malloc(pApacket->msg.data_length); - memset(szTemp, 0x00, pApacket->msg.data_length); - unsigned int nDstLen = 0; - if( 0 == sdbd_plugin_cmd_security_decrypt(nSessionID, pApacket->data, pApacket->msg.data_length, szTemp, &nDstLen) ) { - LOGI("sdbd_plugin_cmd_security_decrypt return 0"); - return 0; - } - - int i = 0; - for(i=0 ; idata[i] = szTemp[i]; - } - - pApacket->msg.data_length = nDstLen; - free(szTemp); - return 1; -} diff --git a/src/transport_security.h b/src/transport_security.h deleted file mode 100644 index 12fd689..0000000 --- a/src/transport_security.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef __TRANSPORT_SECURITY_H__ -#define __TRANSPORT_SECURITY_H__ - -#include -#include "sdb.h" - -int load_sdbd_plugin_security(); - -int security_init(const int nID, const char* pUserID); -int security_deinit(const int nSessionID); -int security_parse_server_hello(const int nSessionID, apacket* pApacket); -int security_gen_client_hello(const int nSessionID, apacket* pApacket); -int security_parse_server_ack(const int nSessionID, apacket* pApacket); -int security_gen_client_ack(const int nSessionID, apacket* pApacket); -int security_encrypt(const int nID, apacket* pApacket); -int security_decrypt(const int nID, apacket* pApacket); - -#endif -- 2.7.4