*
*/
#include <glib.h>
-#include <tzplatform_config.h>
#include <openssl/evp.h>
#include <openssl/sha.h>
+#include <tzplatform_config.h>
+#include <ckmc/ckmc-manager.h>
#include <stdio.h>
#include <stdlib.h>
#include "wmesh-service.h"
#include "wmesh-softap.h"
-#define MOBILE_AP_WIFI_KEY_MAX_LEN 64 /**< Maximum length of wifi hash key */
+#define WIFI_MESH_NET_INF_MAX_LEN 15 /**< Maximum length of nnetwork interface name */
+#define WIFI_MESH_HOSTAP_SSID_MAX_LEN 32 /**< Maximum length of ssid key */
+#define WIFI_MESH_HOSTAP_KEY_MAX_LEN 64 /**< Maximum length of pre-shared key */
+
+#define WIFI_MESH_SOFTAP_PASSPHRASE_STORE_KEY "wiif_mesh_softap_passphrase" /**< Key for softap */
+#define MOBILE_AP_WIFI_VALUE_MAX_LEN 1024 /**< Maximum length key manager buffer */
-#define HOSTAPD_VENDOR_ELEMENTS_WIFI_AP "DD050016321000" /* Specific application mode AP (e.g. GroupPlay) */
-#define HOSTAPD_CONF "interface=%s\n" \
+#define HOSTAPD_VENDOR_ELEMENTS_WIFI_AP "DD050016321000" /**< Specific application mode AP (e.g. GroupPlay) */
+#define HOSTAPD_CONF "interface=%s\n" \
"driver=nl80211\n" \
"ctrl_interface=%s\n" \
"ssid=%s\n" \
"deny_mac_file=%s\n" \
"ieee80211n=1\n" \
"rrm_neighbor_report=1\n"
-#define HOSTAPD_CONF_LEN 1024
-#define HOSTAPD_BIN "/usr/bin/hostapd"
-#define HOSTAPD_DEBUG_FILE tzplatform_mkpath(TZ_SYS_GLOBALUSER_DATA, "/network/mesh_hostapd.log")
-#define HOSTAPD_ENTROPY_FILE tzplatform_mkpath(TZ_SYS_VAR, "/lib/misc/hostapd.bin")
-#define HOSTAPD_MESH_CONF_FILE tzplatform_mkpath(TZ_SYS_GLOBALUSER_DATA, "/network/mesh_hostapd.conf")
-#define HOSTAPD_CTRL_INTF_DIR tzplatform_mkpath(TZ_SYS_RUN, "/hostapd")
-#define HOSTAPD_PID_FILE tzplatform_mkpath(TZ_SYS_RUN, "/.mesh_hostapd.pid")
-#define HOSTAPD_ALLOWED_LIST tzplatform_mkpath(TZ_SYS_VAR, "/lib/hostapd/hostapd.accept")
-#define HOSTAPD_BLOCKED_LIST tzplatform_mkpath(TZ_SYS_VAR, "/lib/hostapd/hostapd.deny")
-#define HOSTAPD_RETRY_MAX 5
-#define HOSTAPD_RETRY_DELAY 500000 /* us */
-
-#define MH_CTRL_INTF "/tmp/mesh_hostapd_wpa_ctrl"
+#define HOSTAPD_CONF_LEN 1024
+#define HOSTAPD_BIN "/usr/bin/hostapd"
+#define HOSTAPD_DEBUG_FILE tzplatform_mkpath(TZ_SYS_GLOBALUSER_DATA, "/network/wmesh_hostapd.log")
+#define HOSTAPD_ENTROPY_FILE tzplatform_mkpath(TZ_SYS_VAR, "/lib/misc/hostapd.bin")
+#define HOSTAPD_DEFAULT_CONF_FILE tzplatform_mkpath(TZ_SYS_GLOBALUSER_DATA, "/network/def_hostapd.conf")
+#define HOSTAPD_WMESH_CONF_FILE tzplatform_mkpath(TZ_SYS_GLOBALUSER_DATA, "/network/wmesh_hostapd.conf")
+#define HOSTAPD_CTRL_INTF_DIR tzplatform_mkpath(TZ_SYS_RUN, "/hostapd")
+#define HOSTAPD_PID_FILE tzplatform_mkpath(TZ_SYS_RUN, "/.wmesh_hostapd.pid")
+#define HOSTAPD_ALLOWED_LIST tzplatform_mkpath(TZ_SYS_VAR, "/lib/hostapd/hostapd.accept")
+#define HOSTAPD_BLOCKED_LIST tzplatform_mkpath(TZ_SYS_VAR, "/lib/hostapd/hostapd.deny")
+#define HOSTAPD_RETRY_MAX 5
+#define HOSTAPD_RETRY_DELAY 500000 /**< us */
+
+#define MH_CTRL_INTF "/tmp/mesh_hostapd_wpa_ctrl"
#define PSK_ITERATION_COUNT 4096
#define MAX_BUF_SIZE (256u)
static int hostapd_ctrl_fd = 0;
-static char *g_passphrase = NULL;
+
+static char *__get_key_manager_alias(const char* name)
+{
+ int ret = 0;
+ char *ckmc_alias = NULL;
+
+ ret = ckmc_alias_new(ckmc_owner_id_system, name, &ckmc_alias);
+ if (ret != CKMC_ERROR_NONE) {
+ WMESH_LOGE("Fail to create ckmc_alias!"); //LCOV_EXCL_LINE
+ return NULL;
+ }
+
+ return ckmc_alias;
+}
+
+static int __set_key_manager_value(const char *key,
+ const char *value, const unsigned int size)
+{
+ int ret = -1;
+ char *alias;
+ ckmc_raw_buffer_s ckmc_buf;
+ ckmc_policy_s ckmc_policy;
+
+ ckmc_policy.password = NULL;
+ ckmc_policy.extractable = true;
+
+ ckmc_buf.data = (unsigned char *) value;
+ ckmc_buf.size = strnlen(value, WIFI_MESH_HOSTAP_KEY_MAX_LEN) + 1;
+
+ if (ckmc_buf.data == NULL) {
+ WMESH_LOGE("Invalid parameter"); //LCOV_EXCL_LINE
+ return WMESHD_ERROR_INVALID_PARAMETER;
+ }
+
+ alias = __get_key_manager_alias(key);
+
+ ret = ckmc_remove_alias(alias);
+ if (ret != CKMC_ERROR_NONE && ret != CKMC_ERROR_DB_ALIAS_UNKNOWN) {
+ WMESH_LOGE("Fail to remove old data : %d", ret); //LCOV_EXCL_LINE
+ if (alias) {
+ free(alias);
+ alias = NULL;
+ }
+ return WMESHD_ERROR_OPERATION_FAILED;
+ }
+
+ ret = ckmc_save_data(alias, ckmc_buf, ckmc_policy);
+ if (ret != CKMC_ERROR_NONE) {
+ WMESH_LOGE("Fail to save the passphrase : %d", ret); //LCOV_EXCL_LINE
+ if (alias) {
+ free(alias);
+ alias = NULL;
+ }
+ return WMESHD_ERROR_OPERATION_FAILED;
+ }
+
+ if (alias) {
+ free(alias);
+ alias = NULL;
+ }
+
+
+ return WMESHD_ERROR_NONE;
+}
+
+static int __get_key_manager_value(const char *key,
+ char *value, unsigned int size, unsigned int *len)
+{
+ int ret = 0;
+ char *alias = NULL;
+ char *passwd = NULL;
+ ckmc_raw_buffer_s *ckmc_buf = NULL;
+
+ if (key == NULL || value == NULL || size == 0) {
+ WMESH_LOGE("Invalid parameter"); //LCOV_EXCL_LINE
+ return WMESHD_ERROR_INVALID_PARAMETER;
+ }
+
+ alias = __get_key_manager_alias(key);
+ ret = ckmc_get_data(alias, passwd, &ckmc_buf);
+ if (ret < 0) {
+ WMESH_LOGE("key/value is empty = %d", ret);
+ } else {
+ *len = ckmc_buf->size;
+ g_strlcpy(value, (char *)ckmc_buf->data, (*len) + 1);
+ }
+
+ if (alias) {
+ free(alias);
+ alias = NULL;
+ }
+
+ return WMESHD_ERROR_NONE;
+}
static int __get_psk_hexascii(const char *pass, const unsigned char *salt,
char *psk, unsigned int psk_len)
return WMESHD_ERROR_NONE;
}
-static int __config_hostapd(const char *softap_interface, const char *ssid,
+static int __set_config_hostapd(const char *softap_interface, const char *ssid,
const char *security, const char *passphrase, const char* mode,
int channel, int visibility, int mac_filter, int max_sta)
{
- char *conf = NULL;
- char *old_conf;
- char buf[HOSTAPD_CONF_LEN] = "";
int fd = -1;
int ret;
- char key[MOBILE_AP_WIFI_KEY_MAX_LEN + 1];
+ char *conf = NULL;
+ char *old_conf = NULL;
char *hw_mode = NULL;
+ char buf[HOSTAPD_CONF_LEN] = "";
if (mode == NULL)
hw_mode = g_strdup("g"); /* LCOV_EXCL_LINE */
/* Security conf. */
if (security != NULL && g_strcmp0(security, "wpa2-psk") == 0) {
- ret = __get_psk_hexascii(passphrase,
- (const unsigned char *)ssid, key, sizeof(key));
- if (ret != WMESHD_ERROR_NONE) {
- /* LCOV_EXCL_START */
- g_free(conf);
- WMESH_LOGE("hex conversion failed");
+
+ int len = strnlen(passphrase, WIFI_MESH_HOSTAP_KEY_MAX_LEN);
+ ret = __set_key_manager_value(WIFI_MESH_SOFTAP_PASSPHRASE_STORE_KEY,
+ passphrase, len);
+ if (WMESHD_ERROR_NONE != ret) {
+ WMESH_LOGE("__set_key_manager_value is failed");
return WMESHD_ERROR_OPERATION_FAILED;
- /* LCOV_EXCL_STOP */
}
+
snprintf(buf, sizeof(buf),
- "wpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", key);
+ "wpa=2\nrsn_pairwise=CCMP\nwpa_psk=\n");
old_conf = conf;
conf = g_strconcat(old_conf, buf, NULL);
WMESH_LOGD("Open connection [%s]", security); /* LCOV_EXCL_LINE */
}
- fd = open(HOSTAPD_MESH_CONF_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0640);
+ fd = open(HOSTAPD_DEFAULT_CONF_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (fd < 0) {
/* LCOV_EXCL_START */
- WMESH_LOGE("Could not create the file [%s].", HOSTAPD_MESH_CONF_FILE);
+ WMESH_LOGE("Could not create the file [%s].", HOSTAPD_WMESH_CONF_FILE);
g_free(conf);
return WMESHD_ERROR_IO_ERROR;
/* LCOV_EXCL_STOP */
{
FILE *fp = NULL;
char buf[256];
+ int ret = WMESHD_ERROR_NONE;
+ char passphrase_buf[WIFI_MESH_HOSTAP_KEY_MAX_LEN + 1] = {0, };
+ unsigned int len = 0;
- fp = fopen(HOSTAPD_MESH_CONF_FILE, "r");
+ fp = fopen(HOSTAPD_DEFAULT_CONF_FILE, "r");
if (fp == NULL) {
/* LCOV_EXCL_START */
WMESH_LOGE("Failed to read file");
*security = 0;
while (1) {
+ size_t ln = 0;
if (!fgets(buf, sizeof(buf), fp) || ferror(fp) || feof(fp))
break;
+ ln = strlen(buf) - 1;
+ if (buf[ln] == '\n')
+ buf[ln] = '\0';
+
if (strncmp(buf, "interface", strlen("interface")) == 0) {
*softap_interface = g_strdup(strrchr(buf, '=') + 1);
WMESH_LOGD("Interface: %s", *softap_interface);
WMESH_LOGD("Max Station: %d", *max_sta);
} else if (strncmp(buf, "wpa=", strlen("wpa=")) == 0) {
*security = 1;
- *passphrase = g_strdup(g_passphrase);
+ ret = __get_key_manager_value(WIFI_MESH_SOFTAP_PASSPHRASE_STORE_KEY,
+ passphrase_buf, sizeof(passphrase_buf), &len);
+ if (ret != WMESHD_ERROR_NONE) {
+ WMESH_LOGE("__get_passphrase is failed"); //LCOV_EXCL_LINE
+ len = 0;
+ }
+ *passphrase = g_strdup(passphrase_buf);
WMESH_LOGD("Security: %d", *security);
WMESH_LOGD("Passphrase: %s", *passphrase);
}
return WMESHD_ERROR_NONE;
}
+static int __write_hostapd_config()
+{
+ char buf[HOSTAPD_CONF_LEN] = {0, };
+ int ret = WMESHD_ERROR_NONE;
+
+ int fd = -1;
+ char *softap_interface = NULL;
+ char *ssid = NULL;
+ char *hw_mode = NULL;
+ int channel = 0;
+ int visibility = 0;
+ int max_sta = 0;
+ int mac_filter = 0;
+ int security = 0;
+ char *passphrase = NULL;
+
+ char *conf = NULL;
+ char *old_conf;
+ char key[WIFI_MESH_HOSTAP_KEY_MAX_LEN + 1] = {0, };
+
+ __read_hostapd_config(&softap_interface, &ssid, &hw_mode, &channel, &visibility,
+ &max_sta, &security, &passphrase);
+
+ memset(buf, 0, sizeof(buf));
+ snprintf(buf, sizeof(buf), HOSTAPD_CONF,
+ softap_interface,
+ HOSTAPD_CTRL_INTF_DIR,
+ ssid,
+ channel,
+ (visibility ? 0 : 2),
+ hw_mode,
+ max_sta,
+ mac_filter,
+ HOSTAPD_ALLOWED_LIST,
+ HOSTAPD_BLOCKED_LIST);
+ conf = g_strdup(buf);
+
+ /* Vendor elements conf. */
+ snprintf(buf, sizeof(buf),
+ "vendor_elements=%s\n", HOSTAPD_VENDOR_ELEMENTS_WIFI_AP);
+ old_conf = conf;
+ conf = g_strconcat(old_conf, buf, NULL);
+ g_free(old_conf);
+
+ /* Security conf. */
+ if (security) {
+ ret = __get_psk_hexascii(passphrase,
+ (const unsigned char *)ssid, key, sizeof(key));
+ if (ret != WMESHD_ERROR_NONE) {
+ /* LCOV_EXCL_START */
+ g_free(conf);
+ WMESH_LOGE("hex conversion failed");
+ return WMESHD_ERROR_OPERATION_FAILED;
+ /* LCOV_EXCL_STOP */
+ }
+ snprintf(buf, sizeof(buf),
+ "wpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", key);
+
+ old_conf = conf;
+ conf = g_strconcat(old_conf, buf, NULL);
+ g_free(old_conf);
+ } else {
+ WMESH_LOGD("Open connection [%s]", security); /* LCOV_EXCL_LINE */
+ }
+
+ fd = open(HOSTAPD_WMESH_CONF_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0640);
+ if (fd < 0) {
+ /* LCOV_EXCL_START */
+ WMESH_LOGE("Could not create the file [%s].", HOSTAPD_WMESH_CONF_FILE);
+ g_free(conf);
+ return WMESHD_ERROR_IO_ERROR;
+ /* LCOV_EXCL_STOP */
+ }
+
+ if (conf) {
+ write(fd, conf, strlen(conf));
+ g_free(conf);
+ conf = NULL;
+ }
+
+ if (softap_interface) {
+ free(softap_interface);
+ softap_interface = NULL;
+ }
+ if (ssid) {
+ free(ssid);
+ ssid = NULL;
+ }
+ if (hw_mode) {
+ free(hw_mode);
+ hw_mode = NULL;
+ }
+ if (passphrase) {
+ free(passphrase);
+ passphrase = NULL;
+ }
+
+ close(fd);
+
+ return WMESHD_ERROR_NONE;
+}
static int __open_hostapd_intf(const char* softap_interface, int *fd,
const char *intf)
{
/* LCOV_EXCL_STOP */
}
+ ret = __write_hostapd_config();
+ if (WMESHD_ERROR_NONE != ret) {
+ /* LCOV_EXCL_START */
+ WMESH_LOGE("Fail to create hostapd config file");
+ return WMESHD_ERROR_NONE;
+ /* LCOV_EXCL_STOP */
+ }
+
pid = fork();
if (pid < 0) {
/* LCOV_EXCL_START */
if (pid == 0) {
if (execl(HOSTAPD_BIN, HOSTAPD_BIN, "-e", HOSTAPD_ENTROPY_FILE,
- HOSTAPD_MESH_CONF_FILE,
+ HOSTAPD_WMESH_CONF_FILE,
"-f", HOSTAPD_DEBUG_FILE,
"-P", HOSTAPD_PID_FILE,
"-ddd", "-B",
const char *sec = (security == 0) ? NULL : "wpa2-psk";
int mac_filter = 0;
- ret = __config_hostapd(softap_interface, ssid, sec, passphrase,
+ ret = __set_config_hostapd(softap_interface, ssid, sec, passphrase,
mode, channel, visibility, mac_filter, max_sta);
- if (ret == WMESHD_ERROR_NONE) {
- g_free(g_passphrase);
- g_passphrase = g_strdup(passphrase);
- }
-
return ret;
}
/* LCOV_EXCL_STOP */
}
- if (remove(HOSTAPD_MESH_CONF_FILE) == -1)
+ if (remove(HOSTAPD_WMESH_CONF_FILE) == -1)
WMESH_LOGE("hostapd conf remove is failed");
return ret;