From: Sooyoung Ha Date: Tue, 27 Jun 2017 13:32:18 +0000 (+0900) Subject: source: align indent X-Git-Tag: submit/tizen/20170628.025840~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=01531ec45d3292cd266f41e3812e240d02d6c585;p=sdk%2Ftarget%2Fsdbd.git source: align indent I change the tabs to spaces and remove the redundant spaces. Change-Id: Iaed7c815f2d9e04582b1a68bc65d678261824378 Signed-off-by: Sooyoung Ha --- diff --git a/src/TizenConfig.h b/src/TizenConfig.h index 81d36f3..7da4f99 100644 --- a/src/TizenConfig.h +++ b/src/TizenConfig.h @@ -15,7 +15,7 @@ */ /* - * Android config -- "CYGWIN_NT-5.1". + * Android config -- "CYGWIN_NT-5.1". * * Cygwin has pthreads, but GDB seems to get confused if you use it to * create threads. By "confused", I mean it freezes up the first time the diff --git a/src/commandline_sdbd.c b/src/commandline_sdbd.c index 63850f3..eee6ac2 100644 --- a/src/commandline_sdbd.c +++ b/src/commandline_sdbd.c @@ -53,115 +53,115 @@ static void print_sdbd_command(FILE *stream, SdbdCommandlineArgs *sdbd_args) { } int parse_sdbd_commandline(SdbdCommandlineArgs *sdbd_args, int argc, char *argv[]) { - int split_retval; - - int opt; - int long_index = 0; - - static struct option long_options[] = { - { ARG_EMULATOR_VM_NAME, required_argument, NULL, ARG_S_EMULATOR_VM_NAME }, - { ARG_SENSORS, required_argument, NULL, ARG_S_SENSORS }, - { ARG_SDB, required_argument, NULL, ARG_S_SDB }, - { ARG_SDBD_LISTEN_PORT, required_argument, NULL, ARG_S_SDBD_LISTEN_PORT }, - { ARG_HELP, no_argument, NULL, ARG_S_HELP }, - { ARG_USAGE, no_argument, NULL, ARG_S_USAGE }, - { NULL, 0, NULL, 0 } - }; - - optind = 1; /* the index of the next element to be processed in argv */ - - while ((opt = getopt_long(argc, argv, "", long_options, &long_index)) != -1) { - switch (opt) { - case ARG_S_EMULATOR_VM_NAME: - split_retval = split_host_port(optarg, - &sdbd_args->emulator.host, - &sdbd_args->emulator.port); - if (split_retval != SDBD_COMMANDLINE_SUCCESS) { - return split_retval; - } - /* if we are on emulator we listen using local transport - * so we should set port to default value but this can - * be overwritten by command line options */ - if (sdbd_args->sdbd_port < 0) { - sdbd_args->sdbd_port = DEFAULT_SDB_LOCAL_TRANSPORT_PORT; - } - print_sdbd_command(stdout, sdbd_args); - break; - case ARG_S_SENSORS: - split_retval = split_host_port(optarg, - &sdbd_args->sensors.host, - &sdbd_args->sensors.port); - if (split_retval != SDBD_COMMANDLINE_SUCCESS) { - return split_retval; - } - print_sdbd_command(stdout, sdbd_args); - break; - case ARG_S_SDB: - split_retval = split_host_port(optarg, - &sdbd_args->sdb.host, - &sdbd_args->sdb.port); - if (split_retval != SDBD_COMMANDLINE_SUCCESS) { - return split_retval; - } - print_sdbd_command(stdout, sdbd_args); - break; - case ARG_S_SDBD_LISTEN_PORT: - if (sscanf(optarg, "%d", &sdbd_args->sdbd_port) < 1) { - return SDBD_COMMANDLINE_FAILURE; - } - print_sdbd_command(stdout, sdbd_args); - break; - case ARG_S_HELP: - return SDBD_COMMANDLINE_HELP; - case ARG_S_USAGE: - return SDBD_COMMANDLINE_USAGE; - case 1: - return SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT; - case '?': - return SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT; - default: - return SDBD_COMMANDLINE_FAILURE; - } - } - - print_sdbd_command(stdout, sdbd_args); - - return SDBD_COMMANDLINE_SUCCESS; + int split_retval; + + int opt; + int long_index = 0; + + static struct option long_options[] = { + { ARG_EMULATOR_VM_NAME, required_argument, NULL, ARG_S_EMULATOR_VM_NAME }, + { ARG_SENSORS, required_argument, NULL, ARG_S_SENSORS }, + { ARG_SDB, required_argument, NULL, ARG_S_SDB }, + { ARG_SDBD_LISTEN_PORT, required_argument, NULL, ARG_S_SDBD_LISTEN_PORT }, + { ARG_HELP, no_argument, NULL, ARG_S_HELP }, + { ARG_USAGE, no_argument, NULL, ARG_S_USAGE }, + { NULL, 0, NULL, 0 } + }; + + optind = 1; /* the index of the next element to be processed in argv */ + + while ((opt = getopt_long(argc, argv, "", long_options, &long_index)) != -1) { + switch (opt) { + case ARG_S_EMULATOR_VM_NAME: + split_retval = split_host_port(optarg, + &sdbd_args->emulator.host, + &sdbd_args->emulator.port); + if (split_retval != SDBD_COMMANDLINE_SUCCESS) { + return split_retval; + } + /* if we are on emulator we listen using local transport + * so we should set port to default value but this can + * be overwritten by command line options */ + if (sdbd_args->sdbd_port < 0) { + sdbd_args->sdbd_port = DEFAULT_SDB_LOCAL_TRANSPORT_PORT; + } + print_sdbd_command(stdout, sdbd_args); + break; + case ARG_S_SENSORS: + split_retval = split_host_port(optarg, + &sdbd_args->sensors.host, + &sdbd_args->sensors.port); + if (split_retval != SDBD_COMMANDLINE_SUCCESS) { + return split_retval; + } + print_sdbd_command(stdout, sdbd_args); + break; + case ARG_S_SDB: + split_retval = split_host_port(optarg, + &sdbd_args->sdb.host, + &sdbd_args->sdb.port); + if (split_retval != SDBD_COMMANDLINE_SUCCESS) { + return split_retval; + } + print_sdbd_command(stdout, sdbd_args); + break; + case ARG_S_SDBD_LISTEN_PORT: + if (sscanf(optarg, "%d", &sdbd_args->sdbd_port) < 1) { + return SDBD_COMMANDLINE_FAILURE; + } + print_sdbd_command(stdout, sdbd_args); + break; + case ARG_S_HELP: + return SDBD_COMMANDLINE_HELP; + case ARG_S_USAGE: + return SDBD_COMMANDLINE_USAGE; + case 1: + return SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT; + case '?': + return SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT; + default: + return SDBD_COMMANDLINE_FAILURE; + } + } + + print_sdbd_command(stdout, sdbd_args); + + return SDBD_COMMANDLINE_SUCCESS; } void apply_sdbd_commandline_defaults(SdbdCommandlineArgs *sdbd_args) { - sdbd_args->emulator.port = -1; + sdbd_args->emulator.port = -1; - sdbd_args->sensors.host = strdup(QEMU_FORWARD_IP); - sdbd_args->sensors.port = DEFAULT_SENSORS_LOCAL_TRANSPORT_PORT; + sdbd_args->sensors.host = strdup(QEMU_FORWARD_IP); + sdbd_args->sensors.port = DEFAULT_SENSORS_LOCAL_TRANSPORT_PORT; - sdbd_args->sdb.host = strdup(QEMU_FORWARD_IP); - sdbd_args->sdb.port = DEFAULT_SDB_PORT; + sdbd_args->sdb.host = strdup(QEMU_FORWARD_IP); + sdbd_args->sdb.port = DEFAULT_SDB_PORT; - // by default don't listen on local transport - sdbd_args->sdbd_port = -1; + // by default don't listen on local transport + sdbd_args->sdbd_port = -1; } int split_host_port(const char *optarg, char **host, int *port) { - const char *colon = strchr(optarg, ':'); - char *old_val = NULL; - - if (colon) { - old_val = *host; - *host = strndup(optarg, colon - optarg); - if (sscanf(colon + 1, "%d", port) < 1) { - return SDBD_COMMANDLINE_FAILURE; - } - } else { - return SDBD_COMMANDLINE_FAILURE; - } - - if (old_val) { - free(old_val); - } - return SDBD_COMMANDLINE_SUCCESS; + const char *colon = strchr(optarg, ':'); + char *old_val = NULL; + + if (colon) { + old_val = *host; + *host = strndup(optarg, colon - optarg); + if (sscanf(colon + 1, "%d", port) < 1) { + return SDBD_COMMANDLINE_FAILURE; + } + } else { + return SDBD_COMMANDLINE_FAILURE; + } + + if (old_val) { + free(old_val); + } + return SDBD_COMMANDLINE_SUCCESS; } @@ -175,7 +175,7 @@ void clear_sdbd_commandline_args(SdbdCommandlineArgs *sdbd_args) { free(sdbd_args->sensors.host); sdbd_args->sensors.host = NULL; - memset(sdbd_args, 0, sizeof(SdbdCommandlineArgs)); + memset(sdbd_args, 0, sizeof(SdbdCommandlineArgs)); } diff --git a/src/commandline_sdbd.h b/src/commandline_sdbd.h index 598b478..2b026ca 100644 --- a/src/commandline_sdbd.h +++ b/src/commandline_sdbd.h @@ -46,8 +46,8 @@ * @brief A simple host:port tuple */ typedef struct { - char *host; - int port; + char *host; + int port; } HostPort; /*! @@ -55,10 +55,10 @@ typedef struct { * @brief Contains all values, which are read from commandline. */ typedef struct { - HostPort emulator; ///< emulator name and forward port - HostPort sdb; ///< sdb address - HostPort sensors; ///< sensors address - int sdbd_port; ///< Port to listen on in tcp mode + HostPort emulator; ///< emulator name and forward port + HostPort sdb; ///< sdb address + HostPort sensors; ///< sensors address + int sdbd_port; ///< Port to listen on in tcp mode } SdbdCommandlineArgs; #include diff --git a/src/default_plugin_auth.c b/src/default_plugin_auth.c index 8fa743a..103603b 100644 --- a/src/default_plugin_auth.c +++ b/src/default_plugin_auth.c @@ -41,7 +41,6 @@ int auth_support ( parameters* in, parameters* out ) int auth_get_key_file_paths ( parameters* in, parameters* out ) { return PLUGIN_CMD_FAIL; - } int confirm_public_key( parameters* in, int out_fd ) diff --git a/src/fdevent.h b/src/fdevent.h index a6db9ea..d383c1f 100644 --- a/src/fdevent.h +++ b/src/fdevent.h @@ -64,8 +64,7 @@ void fdevent_set_timeout(fdevent *fde, int64_t timeout_ms); */ void fdevent_loop(); -struct fdevent -{ +struct fdevent { fdevent *next; fdevent *prev; diff --git a/src/file_sync_service.h b/src/file_sync_service.h index 94a52b6..089a212 100644 --- a/src/file_sync_service.h +++ b/src/file_sync_service.h @@ -18,7 +18,7 @@ #define _FILE_SYNC_SERVICE_H_ #ifdef HAVE_BIG_ENDIAN -static inline unsigned __swap_uint32(unsigned x) +static inline unsigned __swap_uint32(unsigned x) { return (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) @@ -72,7 +72,7 @@ typedef union { struct { unsigned id; unsigned msglen; - } status; + } status; } syncmsg; void init_sdk_sync_permit_rule_regx(void); diff --git a/src/init.c b/src/init.c index bec59f2..7fcc70b 100644 --- a/src/init.c +++ b/src/init.c @@ -50,7 +50,7 @@ char *smack_mnt = NULL; void set_smackmnt(const char *mnt) { - smack_mnt = strdup(mnt); + smack_mnt = strdup(mnt); } /* Verify the mount point for smack file system has a smackfs. @@ -62,126 +62,126 @@ void set_smackmnt(const char *mnt) */ static int verify_smackmnt(const char *mnt) { - struct statfs sfbuf; - int rc; - - do { - rc = statfs(mnt, &sfbuf); - } while (rc < 0 && errno == EINTR); - - if (rc == 0) { - if ((uint32_t)sfbuf.f_type == (uint32_t)SMACK_MAGIC) { - struct statvfs vfsbuf; - rc = statvfs(mnt, &vfsbuf); - if (rc == 0) { - if (!(vfsbuf.f_flag & ST_RDONLY)) { - set_smackmnt(mnt); - } - return 0; - } - } - } - - return -1; + struct statfs sfbuf; + int rc; + + do { + rc = statfs(mnt, &sfbuf); + } while (rc < 0 && errno == EINTR); + + if (rc == 0) { + if ((uint32_t)sfbuf.f_type == (uint32_t)SMACK_MAGIC) { + struct statvfs vfsbuf; + rc = statvfs(mnt, &vfsbuf); + if (rc == 0) { + if (!(vfsbuf.f_flag & ST_RDONLY)) { + set_smackmnt(mnt); + } + return 0; + } + } + } + + return -1; } int smackfs_exists(void) { - int exists = 0; - FILE *fp = NULL; - char *buf = NULL; - size_t len; - ssize_t num; - - fp = fopen("/proc/filesystems", "r"); - if (!fp) - return 1; /* Fail as if it exists */ - - __fsetlocking(fp, FSETLOCKING_BYCALLER); - - num = getline(&buf, &len, fp); - while (num != -1) { - if (strstr(buf, SMACKFS)) { - exists = 1; - break; - } - num = getline(&buf, &len, fp); - } - - free(buf); - fclose(fp); - return exists; + int exists = 0; + FILE *fp = NULL; + char *buf = NULL; + size_t len; + ssize_t num; + + fp = fopen("/proc/filesystems", "r"); + if (!fp) + return 1; /* Fail as if it exists */ + + __fsetlocking(fp, FSETLOCKING_BYCALLER); + + num = getline(&buf, &len, fp); + while (num != -1) { + if (strstr(buf, SMACKFS)) { + exists = 1; + break; + } + num = getline(&buf, &len, fp); + } + + free(buf); + fclose(fp); + return exists; } static void init_smackmnt(void) { - char *buf=NULL, *p; - FILE *fp=NULL; - size_t len; - ssize_t num; - - if (smack_mnt) - return; - - if (verify_smackmnt(SMACKFSMNT) == 0) - return; - - if (verify_smackmnt(OLDSMACKFSMNT) == 0) - return; - - /* Drop back to detecting it the long way. */ - if (!smackfs_exists()) - goto out; - - /* At this point, the usual spot doesn't have an smackfs so - * we look around for it */ - fp = fopen("/proc/mounts", "r"); - if (!fp) - goto out; - - __fsetlocking(fp, FSETLOCKING_BYCALLER); - while ((num = getline(&buf, &len, fp)) != -1) { - char *tmp; - p = strchr(buf, ' '); - if (!p) - goto out; - p++; - - tmp = strchr(p, ' '); - if (!tmp) - goto out; - - if (!strncmp(tmp + 1, SMACKFS" ", strlen(SMACKFS)+1)) { - *tmp = '\0'; - break; - } - } - - /* If we found something, dup it */ - if (num > 0) - verify_smackmnt(p); + char *buf=NULL, *p; + FILE *fp=NULL; + size_t len; + ssize_t num; + + if (smack_mnt) + return; + + if (verify_smackmnt(SMACKFSMNT) == 0) + return; + + if (verify_smackmnt(OLDSMACKFSMNT) == 0) + return; + + /* Drop back to detecting it the long way. */ + if (!smackfs_exists()) + goto out; + + /* At this point, the usual spot doesn't have an smackfs so + * we look around for it */ + fp = fopen("/proc/mounts", "r"); + if (!fp) + goto out; + + __fsetlocking(fp, FSETLOCKING_BYCALLER); + while ((num = getline(&buf, &len, fp)) != -1) { + char *tmp; + p = strchr(buf, ' '); + if (!p) + goto out; + p++; + + tmp = strchr(p, ' '); + if (!tmp) + goto out; + + if (!strncmp(tmp + 1, SMACKFS" ", strlen(SMACKFS)+1)) { + *tmp = '\0'; + break; + } + } + + /* If we found something, dup it */ + if (num > 0) + verify_smackmnt(p); out: - free(buf); - if (fp) - fclose(fp); - return; + free(buf); + if (fp) + fclose(fp); + return; } void fini_smackmnt(void) { - free(smack_mnt); - smack_mnt = NULL; + free(smack_mnt); + smack_mnt = NULL; } static void init_lib(void) __attribute__ ((constructor)); static void init_lib(void) { - init_smackmnt(); + init_smackmnt(); } static void fini_lib(void) __attribute__ ((destructor)); static void fini_lib(void) { - fini_smackmnt(); + fini_smackmnt(); } diff --git a/src/plugin_encrypt.c b/src/plugin_encrypt.c index 51df016..b7fc3ab 100644 --- a/src/plugin_encrypt.c +++ b/src/plugin_encrypt.c @@ -1,238 +1,238 @@ - -#include - -//#define LOG_TAG "SDBD" -//#include -#define TRACE_TAG TRACE_SDB -#include "log.h" - -#include "plugin.h" -#include "plugin_encrypt.h" -#include "parameter.h" -#include "sdbd_plugin.h" - -#define SAKEP_AKE_MSG_RECORD_FIXED_LEN 36 -#define SAKEP_AES_ECB_ADDED_PADDING_SIZE 16 - -// return 1 if success -// return 0 otherwise -int security_init(const int nSessionID, const char* pUserID) -{ - int success = 0; - int ret; - parameters in, out; - - if (pUserID == NULL) { - 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 = nSessionID; - } else { - in.number_of_parameter = 2; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - in.array_of_parameter[1].type = type_string; - in.array_of_parameter[1].v_string.length = strlen(pUserID); - in.array_of_parameter[1].v_string.data = strdup(pUserID); - } - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_INIT, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - success = 1; - release_parameters ( &out ); - } - - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_deinit(const int nSessionID) -{ - 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 = nSessionID; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_DEINIT, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - success = 1; - release_parameters ( &out ); - } - - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_parse_server_hello(const int nSessionID, apacket* pApacket) -{ - int success = 0; - int ret; - parameters in, out; - - in.number_of_parameter = 2; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - in.array_of_parameter[1].type = type_chunk; - in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; - in.array_of_parameter[1].v_chunk.data = pApacket->data; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_PARSE_SERVER_HELLO, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - success = 1; - release_parameters ( &out ); - } - - // avoid to free - in.array_of_parameter[1].v_chunk.data = NULL; - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_gen_client_hello(const int nSessionID, apacket* pApacket) -{ - 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 = nSessionID; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_GEN_CLIENT_HELLO, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); - pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; - success = 1; - release_parameters ( &out ); - } - - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_parse_server_ack(const int nSessionID, apacket* pApacket) -{ - int success = 0; - int ret; - parameters in, out; - - in.number_of_parameter = 2; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - in.array_of_parameter[1].type = type_chunk; - in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; - in.array_of_parameter[1].v_chunk.data = pApacket->data; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_PARSE_SERVER_ACK, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - success = 1; - release_parameters ( &out ); - } - - // avoid to free - in.array_of_parameter[1].v_chunk.data = NULL; - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_gen_client_ack(const int nSessionID, apacket* pApacket) -{ - 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 = nSessionID; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_GEN_CLIENT_ACK, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); - pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; - success = 1; - release_parameters ( &out ); - } - - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_encrypt(const int nSessionID, apacket* pApacket) -{ - int success = 0; - int ret; - parameters in, out; - - in.number_of_parameter = 2; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - in.array_of_parameter[1].type = type_chunk; - in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; - in.array_of_parameter[1].v_chunk.data = pApacket->data; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_ENCRYPT, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); - pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; - success = 1; - release_parameters ( &out ); - } - - // avoid to free - in.array_of_parameter[1].v_chunk.data = NULL; - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_decrypt(const int nSessionID, apacket* pApacket) -{ - int success = 0; - int ret; - parameters in, out; - - in.number_of_parameter = 2; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - in.array_of_parameter[1].type = type_chunk; - in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; - in.array_of_parameter[1].v_chunk.data = pApacket->data; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_DECRYPT, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); - pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; - success = 1; - release_parameters ( &out ); - } - - // avoid to free - in.array_of_parameter[1].v_chunk.data = NULL; - release_parameters ( &in ); - return success; -} - + +#include + +//#define LOG_TAG "SDBD" +//#include +#define TRACE_TAG TRACE_SDB +#include "log.h" + +#include "plugin.h" +#include "plugin_encrypt.h" +#include "parameter.h" +#include "sdbd_plugin.h" + +#define SAKEP_AKE_MSG_RECORD_FIXED_LEN 36 +#define SAKEP_AES_ECB_ADDED_PADDING_SIZE 16 + +// return 1 if success +// return 0 otherwise +int security_init(const int nSessionID, const char* pUserID) +{ + int success = 0; + int ret; + parameters in, out; + + if (pUserID == NULL) { + 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 = nSessionID; + } else { + in.number_of_parameter = 2; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + in.array_of_parameter[1].type = type_string; + in.array_of_parameter[1].v_string.length = strlen(pUserID); + in.array_of_parameter[1].v_string.data = strdup(pUserID); + } + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_INIT, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + success = 1; + release_parameters ( &out ); + } + + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_deinit(const int nSessionID) +{ + 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 = nSessionID; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_DEINIT, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + success = 1; + release_parameters ( &out ); + } + + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_parse_server_hello(const int nSessionID, apacket* pApacket) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 2; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + in.array_of_parameter[1].type = type_chunk; + in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; + in.array_of_parameter[1].v_chunk.data = pApacket->data; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_PARSE_SERVER_HELLO, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + success = 1; + release_parameters ( &out ); + } + + // avoid to free + in.array_of_parameter[1].v_chunk.data = NULL; + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_gen_client_hello(const int nSessionID, apacket* pApacket) +{ + 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 = nSessionID; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_GEN_CLIENT_HELLO, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); + pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; + success = 1; + release_parameters ( &out ); + } + + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_parse_server_ack(const int nSessionID, apacket* pApacket) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 2; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + in.array_of_parameter[1].type = type_chunk; + in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; + in.array_of_parameter[1].v_chunk.data = pApacket->data; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_PARSE_SERVER_ACK, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + success = 1; + release_parameters ( &out ); + } + + // avoid to free + in.array_of_parameter[1].v_chunk.data = NULL; + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_gen_client_ack(const int nSessionID, apacket* pApacket) +{ + 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 = nSessionID; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_GEN_CLIENT_ACK, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); + pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; + success = 1; + release_parameters ( &out ); + } + + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_encrypt(const int nSessionID, apacket* pApacket) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 2; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + in.array_of_parameter[1].type = type_chunk; + in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; + in.array_of_parameter[1].v_chunk.data = pApacket->data; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_ENCRYPT, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); + pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; + success = 1; + release_parameters ( &out ); + } + + // avoid to free + in.array_of_parameter[1].v_chunk.data = NULL; + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_decrypt(const int nSessionID, apacket* pApacket) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 2; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + in.array_of_parameter[1].type = type_chunk; + in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; + in.array_of_parameter[1].v_chunk.data = pApacket->data; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_DECRYPT, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); + pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; + success = 1; + release_parameters ( &out ); + } + + // avoid to free + in.array_of_parameter[1].v_chunk.data = NULL; + release_parameters ( &in ); + return success; +} + diff --git a/src/plugin_encrypt.h b/src/plugin_encrypt.h index c54ff2b..93d2363 100644 --- a/src/plugin_encrypt.h +++ b/src/plugin_encrypt.h @@ -1,15 +1,15 @@ -#ifndef __TRANSPORT_SECURITY_H__ -#define __TRANSPORT_SECURITY_H__ - -#include "sdb.h" - -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 +#ifndef __TRANSPORT_SECURITY_H__ +#define __TRANSPORT_SECURITY_H__ + +#include "sdb.h" + +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 diff --git a/src/sdb.c b/src/sdb.c index 39ea547..21653e3 100644 --- a/src/sdb.c +++ b/src/sdb.c @@ -369,141 +369,141 @@ void print_packet(const char *label, apacket *p) /* desc. : 암호화 실패 메시지 전송 parameter : [in] apacket* p : sdbd로 들어온 메시지 - [in] atransport *t : 현재 연결에 대한 atransport - [in] unsigned failed_value : 실패 값 + [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); + 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] atransport *t : 현재 연결에 대한 atransport + [in/out] atransport *t : 현재 연결에 대한 atransport ret : 0 : 정상적으로 메시지 전송 - -1: 메시지 전송 실패 + -1: 메시지 전송 실패 */ int handle_encr_packet(apacket* p, atransport *t){ - static int sessionID = 0; - int retVal = 0; + static int sessionID = 0; + int retVal = 0; apacket* enc_p = NULL; - 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(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"); enc_p = get_apacket(); - 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_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 메시지 파싱 enc_p = get_apacket(); - 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; + 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 @@ -892,11 +892,11 @@ void handle_packet(apacket *p, atransport *t) } break; #ifdef SUPPORT_ENCRYPT - case A_ENCR: // 암호화 메시지인 경우 - if(t->connection_state != CS_OFFLINE) { - handle_encr_packet(p, t); - } - break; + case A_ENCR: // 암호화 메시지인 경우 + if(t->connection_state != CS_OFFLINE) { + handle_encr_packet(p, t); + } + break; #endif default: @@ -1301,91 +1301,91 @@ int should_drop_privileges() { #define SDBD_BOOT_INFO_FILE "/tmp/sdbd_boot_info" static DBusHandlerResult __sdbd_dbus_signal_filter(DBusConnection *conn, - DBusMessage *message, void *user_data) { - D("got dbus message\n"); - const char *interface; - - DBusError error; - dbus_error_init(&error); - - interface = dbus_message_get_interface(message); - if (interface == NULL) { - D("reject by security issue - no interface\n"); - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; - } - - if (dbus_message_is_signal(message, DEVICED_CORE_INTERFACE, - BOOTING_DONE_SIGNAL)) { - booting_done = 1; - if (access(SDBD_BOOT_INFO_FILE, F_OK) == 0) { - D("booting is done before\n"); - } else { - FILE *f = fopen(SDBD_BOOT_INFO_FILE, "w"); - if (f != NULL) { - fprintf(f, "%d", 1); - fclose(f); - } - } - D("booting is done\n"); - } - - D("handled dbus message\n"); - return DBUS_HANDLER_RESULT_HANDLED; + DBusMessage *message, void *user_data) { + D("got dbus message\n"); + const char *interface; + + DBusError error; + dbus_error_init(&error); + + interface = dbus_message_get_interface(message); + if (interface == NULL) { + D("reject by security issue - no interface\n"); + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + if (dbus_message_is_signal(message, DEVICED_CORE_INTERFACE, + BOOTING_DONE_SIGNAL)) { + booting_done = 1; + if (access(SDBD_BOOT_INFO_FILE, F_OK) == 0) { + D("booting is done before\n"); + } else { + FILE *f = fopen(SDBD_BOOT_INFO_FILE, "w"); + if (f != NULL) { + fprintf(f, "%d", 1); + fclose(f); + } + } + D("booting is done\n"); + } + + D("handled dbus message\n"); + return DBUS_HANDLER_RESULT_HANDLED; } static void *bootdone_cb(void *x) { - int MAX_LOCAL_BUFSZ = 128; - DBusError error; - DBusConnection *bus; - char rule[MAX_LOCAL_BUFSZ]; - GMainLoop *mainloop; + int MAX_LOCAL_BUFSZ = 128; + DBusError error; + DBusConnection *bus; + char rule[MAX_LOCAL_BUFSZ]; + GMainLoop *mainloop; /* g_type_init() is deprecated for glib version 2.35.0 or greater, */ #if !GLIB_CHECK_VERSION(2,35,0) - g_type_init(); + g_type_init(); #endif - dbus_error_init(&error); - bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error); - if (!bus) { - D("Failed to connect to the D-BUS daemon: %s", error.message); - dbus_error_free(&error); - return NULL; - } - dbus_connection_setup_with_g_main(bus, NULL); - - snprintf(rule, MAX_LOCAL_BUFSZ, "type='signal',interface='%s'", - DEVICED_CORE_INTERFACE); - /* listening to messages */ - dbus_bus_add_match(bus, rule, &error); - if (dbus_error_is_set(&error)) { - D("Fail to rule set: %s", error.message); - dbus_error_free(&error); - return NULL; - } - - if (dbus_connection_add_filter(bus, __sdbd_dbus_signal_filter, NULL, NULL) - == FALSE) - return NULL; - - D("booting signal initialized\n"); - mainloop = g_main_loop_new(NULL, FALSE); - g_main_loop_run(mainloop); - - D("dbus loop exited"); + dbus_error_init(&error); + bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error); + if (!bus) { + D("Failed to connect to the D-BUS daemon: %s", error.message); + dbus_error_free(&error); + return NULL; + } + dbus_connection_setup_with_g_main(bus, NULL); + + snprintf(rule, MAX_LOCAL_BUFSZ, "type='signal',interface='%s'", + DEVICED_CORE_INTERFACE); + /* listening to messages */ + dbus_bus_add_match(bus, rule, &error); + if (dbus_error_is_set(&error)) { + D("Fail to rule set: %s", error.message); + dbus_error_free(&error); + return NULL; + } + + if (dbus_connection_add_filter(bus, __sdbd_dbus_signal_filter, NULL, NULL) + == FALSE) + return NULL; + + D("booting signal initialized\n"); + mainloop = g_main_loop_new(NULL, FALSE); + g_main_loop_run(mainloop); + + D("dbus loop exited"); dbus_connection_unref(bus); - return NULL; + return NULL; } void register_bootdone_cb() { - D("registerd bootdone callback\n"); + D("registerd bootdone callback\n"); - sdb_thread_t t; - if (sdb_thread_create(&t, bootdone_cb, NULL)) { - D("cannot create service thread\n"); - return; - } + sdb_thread_t t; + if (sdb_thread_create(&t, bootdone_cb, NULL)) { + D("cannot create service thread\n"); + return; + } } static int sdbd_set_groups(const char *name, int gid, struct group_info default_groups[], int default_groups_size) { diff --git a/src/sdb.h b/src/sdb.h index 12b3105..1516455 100644 --- a/src/sdb.h +++ b/src/sdb.h @@ -146,7 +146,7 @@ struct asocket { /* socket-type-specific extradata */ void *extra; - /* A socket is bound to atransport */ + /* A socket is bound to atransport */ atransport *transport; }; @@ -216,8 +216,8 @@ struct atransport size_t max_payload; #ifdef SUPPORT_ENCRYPT - unsigned encryption; // 해당 연결이 암호화 모드인지 확인하는 flag , 0 = no-encryption / 1 = encryption - int sessionID; // 암호화 세션 ID, 암호화 map에 대한 key + unsigned encryption; // 해당 연결이 암호화 모드인지 확인하는 flag , 0 = no-encryption / 1 = encryption + int sessionID; // 암호화 세션 ID, 암호화 map에 대한 key #endif }; diff --git a/src/services.c b/src/services.c index 6c9d1d6..ea991c2 100644 --- a/src/services.c +++ b/src/services.c @@ -139,7 +139,7 @@ void rootshell_service(int fd, void *cookie) writex(fd, buf, strlen(buf)); } } else { - snprintf(buf, sizeof(buf), "Unknown command option : %s\n", mode); + snprintf(buf, sizeof(buf), "Unknown command option : %s\n", mode); writex(fd, buf, strlen(buf)); } D("set rootshell to %s\n", rootshell_mode == 1 ? "root" : SDK_USER_NAME); diff --git a/src/socket_inaddr_any_server.c b/src/socket_inaddr_any_server.c index 10a84e6..00aa19b 100644 --- a/src/socket_inaddr_any_server.c +++ b/src/socket_inaddr_any_server.c @@ -64,7 +64,7 @@ int socket_inaddr_any_server(int port, int type) if (ret < 0) { close(s); - return -1; + return -1; } } diff --git a/src/socket_local.h b/src/socket_local.h index dc274ac..056d090 100644 --- a/src/socket_local.h +++ b/src/socket_local.h @@ -17,7 +17,7 @@ #ifndef __SOCKET_LOCAL_H #define __SOCKET_LOCAL_H -#define FILESYSTEM_SOCKET_PREFIX "/tmp/" +#define FILESYSTEM_SOCKET_PREFIX "/tmp/" #define ANDROID_RESERVED_SOCKET_PREFIX "/dev/socket/" /* @@ -30,10 +30,10 @@ * socklen_t is set to indicate the final length. This function * will fail if the namespace is invalid (not one of the indicated * constants) or if the name is too long. - * + * * @return 0 on success or -1 on failure - */ -int socket_make_sockaddr_un(const char *name, int namespaceId, + */ +int socket_make_sockaddr_un(const char *name, int namespaceId, struct sockaddr_un *p_addr, socklen_t *alen); #endif diff --git a/src/socket_local_server.c b/src/socket_local_server.c index 8f2ed9f..5cba92d 100644 --- a/src/socket_local_server.c +++ b/src/socket_local_server.c @@ -88,7 +88,7 @@ int socket_local_server_bind(int s, const char *name, int namespaceId) } -/** Open a server-side UNIX domain datagram socket in the Linux non-filesystem +/** Open a server-side UNIX domain datagram socket in the Linux non-filesystem * namespace * * Returns fd on success, -1 on fail @@ -98,7 +98,7 @@ int socket_local_server(const char *name, int namespace, int type) { int err; int s; - + s = socket(AF_LOCAL, type, 0); if (s < 0) return -1; diff --git a/src/socket_loopback_server.c b/src/socket_loopback_server.c index 73e5670..f397a3e 100644 --- a/src/socket_loopback_server.c +++ b/src/socket_loopback_server.c @@ -133,7 +133,7 @@ int socket_loopback_server(int port, int type) if (ret < 0) { sdb_close(s); - return -1; + return -1; } } diff --git a/src/sockets.c b/src/sockets.c index 9711613..12baade 100644 --- a/src/sockets.c +++ b/src/sockets.c @@ -319,11 +319,11 @@ static void local_socket_event_func(int fd, unsigned ev, void *_s) 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 = asock_get_max_payload(s) - 200; + // 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 = asock_get_max_payload(s) - 200; #else const size_t max_payload = asock_get_max_payload(s); #endif diff --git a/src/sockets.h b/src/sockets.h index de2269f..e358a19 100644 --- a/src/sockets.h +++ b/src/sockets.h @@ -28,8 +28,8 @@ typedef int socklen_t; #include #endif -#define ANDROID_SOCKET_ENV_PREFIX "ANDROID_SOCKET_" -#define ANDROID_SOCKET_DIR "/dev/socket" +#define ANDROID_SOCKET_ENV_PREFIX "ANDROID_SOCKET_" +#define ANDROID_SOCKET_DIR "/dev/socket" #ifdef __cplusplus extern "C" { @@ -45,32 +45,32 @@ extern "C" { */ static inline int android_get_control_socket(const char *name) { - char key[64] = ANDROID_SOCKET_ENV_PREFIX; - const char *val; - int fd; + char key[64] = ANDROID_SOCKET_ENV_PREFIX; + const char *val; + int fd; - /* build our environment variable, counting cycles like a wolf ... */ + /* build our environment variable, counting cycles like a wolf ... */ #if HAVE_STRLCPY - strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, - name, - sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); -#else /* for the host, which may lack the almightly strncpy ... */ - strncpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, - name, - sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); - key[sizeof(key)-1] = '\0'; + strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, + name, + sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); +#else /* for the host, which may lack the almightly strncpy ... */ + strncpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, + name, + sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); + key[sizeof(key)-1] = '\0'; #endif - val = getenv(key); - if (!val) - return -1; + val = getenv(key); + if (!val) + return -1; - errno = 0; - fd = strtol(val, NULL, 10); - if (errno) - return -1; + errno = 0; + fd = strtol(val, NULL, 10); + if (errno) + return -1; - return fd; + return fd; } /* diff --git a/src/transport.c b/src/transport.c index 7eb9e95..faf5482 100644 --- a/src/transport.c +++ b/src/transport.c @@ -304,10 +304,10 @@ static void *output_thread(void *_t) t->serial); #ifdef SUPPORT_ENCRYPT - if (t->encryption == ENCR_ON && p->msg.command != A_ENCR) // 현재 연결이 암호화 모드이고, 암호화 관련 메시지가 아닌 경우, 메시지 복호화 - { - security_decrypt(t->sessionID, p); - } + if (t->encryption == ENCR_ON && p->msg.command != A_ENCR) // 현재 연결이 암호화 모드이고, 암호화 관련 메시지가 아닌 경우, 메시지 복호화 + { + security_decrypt(t->sessionID, p); + } #endif @@ -382,14 +382,14 @@ static void *input_thread(void *_t) D("%s: transport got packet, sending to remote, state(%s)\n", t->serial, statename(t)); #ifdef SUPPORT_ENCRYPT - if (t->encryption == ENCR_ON && p->msg.command != A_ENCR) // 현재 연결이 암호화 모드이고, 암호화 관련 메시지가 아닌 경우, 메시지를 암호화 - { - security_encrypt(t->sessionID, p); - } - else if(t->encryption == ENCR_OFF) - { + if (t->encryption == ENCR_ON && p->msg.command != A_ENCR) // 현재 연결이 암호화 모드이고, 암호화 관련 메시지가 아닌 경우, 메시지를 암호화 + { + security_encrypt(t->sessionID, p); + } + else if(t->encryption == ENCR_OFF) + { - } + } #endif @@ -812,7 +812,7 @@ void broadcast_transport(apacket *p) apacket* ap = get_apacket(); copy_packet(ap, p); send_packet(ap, t); - + if (ap->msg.command == A_STAT && ap->msg.arg1 == 0) { // lock state message if (ap->msg.arg0 == 0) { diff --git a/src/transport_local.c b/src/transport_local.c index d1b569e..58a07a4 100644 --- a/src/transport_local.c +++ b/src/transport_local.c @@ -41,7 +41,7 @@ #include "plugin.h" #ifdef HAVE_BIG_ENDIAN -#define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24) +#define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24) static inline void fix_endians(apacket *p) { p->msg.command = H4(p->msg.command); @@ -450,9 +450,9 @@ static void* notify_sdbd_startup_thread(void* ptr) { } if (get_emulator_guestip(guest_ip, sizeof guest_ip) == -1) { - D("failed to get emulator guest ip\n"); - goto sleep_and_continue; - } + D("failed to get emulator guest ip\n"); + goto sleep_and_continue; + } // tell qemu sdbd is just started with udp if (send_msg_to_localhost_from_guest(host_ip, sensors_port, "2\n", 1) < 0) { diff --git a/src/transport_usb.c b/src/transport_usb.c index 32789ec..471aa87 100644 --- a/src/transport_usb.c +++ b/src/transport_usb.c @@ -26,7 +26,7 @@ #include "sdb.h" #ifdef HAVE_BIG_ENDIAN -#define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24) +#define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24) static inline void fix_endians(apacket *p) { p->msg.command = H4(p->msg.command); diff --git a/src/usb_funcfs_client.c b/src/usb_funcfs_client.c index 3ca92a3..00b25c3 100644 --- a/src/usb_funcfs_client.c +++ b/src/usb_funcfs_client.c @@ -35,8 +35,8 @@ #include "sdb.h" -#define MAX_PACKET_SIZE_FS 64 -#define MAX_PACKET_SIZE_HS 512 +#define MAX_PACKET_SIZE_FS 64 +#define MAX_PACKET_SIZE_HS 512 #define cpu_to_le16(x) htole16(x) #define cpu_to_le32(x) htole32(x) @@ -250,7 +250,7 @@ static void *usb_open_thread(void *x) sdb_mutex_unlock(&usb->lock); D("[ usb_thread - registering device ]\n"); - register_usb_transport(usb, NULL, 1); /* writable transport */ + register_usb_transport(usb, NULL, 1); /* writable transport */ while (1) { /* wait until the USB device needs reset */ @@ -269,7 +269,7 @@ static void *usb_open_thread(void *x) sdb_mutex_unlock(&usb->lock); D("[ usb_thread - registering device ]\n"); - register_usb_transport(usb, NULL, 1); /* writable transport */ + register_usb_transport(usb, NULL, 1); /* writable transport */ } /* never gets here */ @@ -312,34 +312,34 @@ static int read_control(struct usb_handle *usb) } /* dispatch read event */ - switch (read_event.type) { - case FUNCTIONFS_RESUME: - case FUNCTIONFS_ENABLE: - D("FFSEvent %s\n", names[read_event.type]); - sdb_mutex_lock(&usb->lock); - usb->ffs_enabled = 1; - sdb_cond_signal(&usb->notify); - sdb_mutex_unlock(&usb->lock); - break; - - case FUNCTIONFS_SUSPEND: - case FUNCTIONFS_DISABLE: - D("FFSEvent %s\n", names[read_event.type]); - sdb_mutex_lock(&usb->lock); - usb->ffs_enabled = 0; - sdb_mutex_unlock(&usb->lock); - break; - - case FUNCTIONFS_BIND: - case FUNCTIONFS_UNBIND: - case FUNCTIONFS_SETUP: - D("FFSEvent %s\n", names[read_event.type]); - break; - - default: - D("FFSEvent event (type=%d) is unknown -- ignored\n", read_event.type); - break; - } + switch (read_event.type) { + case FUNCTIONFS_RESUME: + case FUNCTIONFS_ENABLE: + D("FFSEvent %s\n", names[read_event.type]); + sdb_mutex_lock(&usb->lock); + usb->ffs_enabled = 1; + sdb_cond_signal(&usb->notify); + sdb_mutex_unlock(&usb->lock); + break; + + case FUNCTIONFS_SUSPEND: + case FUNCTIONFS_DISABLE: + D("FFSEvent %s\n", names[read_event.type]); + sdb_mutex_lock(&usb->lock); + usb->ffs_enabled = 0; + sdb_mutex_unlock(&usb->lock); + break; + + case FUNCTIONFS_BIND: + case FUNCTIONFS_UNBIND: + case FUNCTIONFS_SETUP: + D("FFSEvent %s\n", names[read_event.type]); + break; + + default: + D("FFSEvent event (type=%d) is unknown -- ignored\n", read_event.type); + break; + } return ret; } @@ -431,9 +431,9 @@ static int bulk_read(int bulkout_fd, void *buf, size_t length) if (errno != EINTR) { return ret; } - } else { + } else { count += ret; - } + } } while (count < length); @@ -446,8 +446,8 @@ static int bulk_read(int bulkout_fd, void *buf, size_t length) */ static int ep0_exists() { - struct stat statb; - return stat(ep0_path, &statb) == 0; + struct stat statb; + return stat(ep0_path, &statb) == 0; } @@ -464,9 +464,9 @@ static int autoconfig(struct usb_handle *h) return -ENODEV; } - h->EP0_NAME = ep0_path; - h->EP_OUT_NAME = ep1_path; - h->EP_IN_NAME = ep2_path; + h->EP0_NAME = ep0_path; + h->EP_OUT_NAME = ep1_path; + h->EP_IN_NAME = ep2_path; return 0; } diff --git a/src/utils.c b/src/utils.c index 6f48ba2..3a27e16 100644 --- a/src/utils.c +++ b/src/utils.c @@ -117,41 +117,41 @@ buff_add (char* buff, char* buffEnd, const char* format, ... ) } char *str_trim(char *str) { - size_t len = 0; - char *frontp = str; - char *endp = NULL; - - if (str == NULL) { - return NULL; - } - if (str[0] == '\0') { - return str; - } - - len = strlen(str); - endp = str + len; - - while (isspace(*frontp)) { - ++frontp; - } - if (endp != frontp) { - while (isspace(*(--endp)) && endp != frontp) { - } - } - - if (str + len - 1 != endp) - *(endp + 1) = '\0'; - else if (frontp != str && endp == frontp) - *str = '\0'; - - endp = str; - if (frontp != str) { - while (*frontp) { - *endp++ = *frontp++; - } - *endp = '\0'; - } - return str; + size_t len = 0; + char *frontp = str; + char *endp = NULL; + + if (str == NULL) { + return NULL; + } + if (str[0] == '\0') { + return str; + } + + len = strlen(str); + endp = str + len; + + while (isspace(*frontp)) { + ++frontp; + } + if (endp != frontp) { + while (isspace(*(--endp)) && endp != frontp) { + } + } + + if (str + len - 1 != endp) + *(endp + 1) = '\0'; + else if (frontp != str && endp == frontp) + *str = '\0'; + + endp = str; + if (frontp != str) { + while (*frontp) { + *endp++ = *frontp++; + } + *endp = '\0'; + } + return str; } int spawn(const char* program, char* const arg_list[]) @@ -182,47 +182,47 @@ int spawn(const char* program, char* const arg_list[]) } char** str_split(char* a_str, const char a_delim) { - char** result = 0; - size_t count = 0; - char* tmp = a_str; - char* last_comma = 0; - char delim[2]; - delim[0] = a_delim; - delim[1] = 0; - char *ptr; - - /* Count how many elements will be extracted. */ - while (*tmp) { - if (a_delim == *tmp) { - count++; - last_comma = tmp; - } - tmp++; - } - - /* Add space for trailing token. */ - count += last_comma < (a_str + strlen(a_str) - 1); - - /* Add space for terminating null string so caller - knows where the list of returned strings ends. */ - count++; - - result = malloc(sizeof(char*) * count); - - if (result) { - size_t idx = 0; - char* token = strtok_r(a_str, delim, &ptr); - - while (token) { - //assert(idx < count); - *(result + idx++) = strdup(token); - token = strtok_r(0, delim, &ptr); - } - //assert(idx == count - 1); - *(result + idx) = 0; - } - - return result; + char** result = 0; + size_t count = 0; + char* tmp = a_str; + char* last_comma = 0; + char delim[2]; + delim[0] = a_delim; + delim[1] = 0; + char *ptr; + + /* Count how many elements will be extracted. */ + while (*tmp) { + if (a_delim == *tmp) { + count++; + last_comma = tmp; + } + tmp++; + } + + /* Add space for trailing token. */ + count += last_comma < (a_str + strlen(a_str) - 1); + + /* Add space for terminating null string so caller + knows where the list of returned strings ends. */ + count++; + + result = malloc(sizeof(char*) * count); + + if (result) { + size_t idx = 0; + char* token = strtok_r(a_str, delim, &ptr); + + while (token) { + //assert(idx < count); + *(result + idx++) = strdup(token); + token = strtok_r(0, delim, &ptr); + } + //assert(idx == count - 1); + *(result + idx) = 0; + } + + return result; } int keep_alive(int fd, int onoff, int cnt, int idle, int interval)