/* * cmd_honeypot.c * Honeypot command registration and dispatch. * Compiled as empty when CONFIG_MODULE_HONEYPOT is not set. * * Commands (8 total, fits within 32-command budget): * hp_svc * hp_wifi * hp_net * hp_config_set * hp_config_get * hp_config_list [type] * hp_config_reset * hp_status */ #include "sdkconfig.h" #ifdef CONFIG_MODULE_HONEYPOT #include #include #include "esp_log.h" #include "utils.h" #include "hp_config.h" #include "hp_tcp_services.h" #include "hp_wifi_monitor.h" #include "hp_net_monitor.h" #define TAG "HONEYPOT" /* ============================================================ * COMMAND: hp_svc * ============================================================ */ static esp_err_t cmd_hp_svc( int argc, char **argv, const char *req, void *ctx) { (void)ctx; const char *svc_name = argv[0]; const char *action = argv[1]; int id = hp_svc_name_to_id(svc_name); if (id < 0) { char buf[64]; snprintf(buf, sizeof(buf), "error=unknown_service service=%s", svc_name); msg_error(TAG, buf, req); return ESP_ERR_INVALID_ARG; } if (strcmp(action, "start") == 0) { hp_svc_start((hp_svc_id_t)id); char buf[64]; snprintf(buf, sizeof(buf), "service=%s action=started", svc_name); msg_info(TAG, buf, req); } else if (strcmp(action, "stop") == 0) { hp_svc_stop((hp_svc_id_t)id); char buf[64]; snprintf(buf, sizeof(buf), "service=%s action=stopped", svc_name); msg_info(TAG, buf, req); } else if (strcmp(action, "status") == 0) { char buf[256]; hp_svc_status((hp_svc_id_t)id, buf, sizeof(buf)); msg_info(TAG, buf, req); } else { msg_error(TAG, "error=invalid_action expected=start|stop|status", req); return ESP_ERR_INVALID_ARG; } return ESP_OK; } /* ============================================================ * COMMAND: hp_wifi * ============================================================ */ static esp_err_t cmd_hp_wifi( int argc, char **argv, const char *req, void *ctx) { (void)ctx; const char *action = argv[0]; if (strcmp(action, "start") == 0) { hp_wifi_monitor_start(); msg_info(TAG, "wifi_monitor=started", req); } else if (strcmp(action, "stop") == 0) { hp_wifi_monitor_stop(); msg_info(TAG, "wifi_monitor=stopped", req); } else if (strcmp(action, "status") == 0) { char buf[256]; hp_wifi_monitor_status(buf, sizeof(buf)); msg_info(TAG, buf, req); } else { msg_error(TAG, "error=invalid_action expected=start|stop|status", req); return ESP_ERR_INVALID_ARG; } return ESP_OK; } /* ============================================================ * COMMAND: hp_net * ============================================================ */ static esp_err_t cmd_hp_net( int argc, char **argv, const char *req, void *ctx) { (void)ctx; const char *action = argv[0]; if (strcmp(action, "start") == 0) { hp_net_monitor_start(); msg_info(TAG, "net_monitor=started", req); } else if (strcmp(action, "stop") == 0) { hp_net_monitor_stop(); msg_info(TAG, "net_monitor=stopped", req); } else if (strcmp(action, "status") == 0) { char buf[256]; hp_net_monitor_status(buf, sizeof(buf)); msg_info(TAG, buf, req); } else { msg_error(TAG, "error=invalid_action expected=start|stop|status", req); return ESP_ERR_INVALID_ARG; } return ESP_OK; } /* ============================================================ * COMMAND: hp_config_set * ============================================================ */ static esp_err_t cmd_hp_config_set( int argc, char **argv, const char *req, void *ctx) { (void)ctx; const char *type = argv[0]; const char *key = argv[1]; const char *value = argv[2]; esp_err_t err; if (strcmp(type, "banner") == 0) { err = hp_config_set_banner(key, value); } else if (strcmp(type, "threshold") == 0) { err = hp_config_set_threshold(key, atoi(value)); } else { msg_error(TAG, "error=invalid_config_type expected=banner|threshold", req); return ESP_ERR_INVALID_ARG; } if (err == ESP_OK) { char buf[128]; snprintf(buf, sizeof(buf), "config_set=%s.%s value=%s", type, key, value); msg_info(TAG, buf, req); } else { msg_error(TAG, "error=config_set_failed", req); } return err; } /* ============================================================ * COMMAND: hp_config_get * ============================================================ */ static esp_err_t cmd_hp_config_get( int argc, char **argv, const char *req, void *ctx) { (void)ctx; const char *type = argv[0]; const char *key = argv[1]; char buf[256]; if (strcmp(type, "banner") == 0) { char val[128]; hp_config_get_banner(key, val, sizeof(val)); /* Strip newlines for display */ for (char *p = val; *p; p++) { if (*p == '\r' || *p == '\n') { *p = '\0'; break; } } snprintf(buf, sizeof(buf), "banner_%s=%s", key, val); } else if (strcmp(type, "threshold") == 0) { int val = hp_config_get_threshold(key); snprintf(buf, sizeof(buf), "threshold_%s=%d", key, val); } else { msg_error(TAG, "error=invalid_config_type expected=banner|threshold", req); return ESP_ERR_INVALID_ARG; } msg_info(TAG, buf, req); return ESP_OK; } /* ============================================================ * COMMAND: hp_config_list [type] * ============================================================ */ static esp_err_t cmd_hp_config_list( int argc, char **argv, const char *req, void *ctx) { (void)ctx; const char *filter = (argc > 0) ? argv[0] : ""; char buf[512]; hp_config_list(filter, buf, sizeof(buf)); msg_info(TAG, buf, req); return ESP_OK; } /* ============================================================ * COMMAND: hp_config_reset * ============================================================ */ static esp_err_t cmd_hp_config_reset( int argc, char **argv, const char *req, void *ctx) { (void)argc; (void)argv; (void)ctx; esp_err_t err = hp_config_reset_all(); if (err == ESP_OK) msg_info(TAG, "config=reset_to_defaults", req); else msg_error(TAG, "error=config_reset_failed", req); return err; } /* ============================================================ * COMMAND: hp_status * ============================================================ */ static esp_err_t cmd_hp_status( int argc, char **argv, const char *req, void *ctx) { (void)argc; (void)argv; (void)ctx; char buf[512]; int off = 0; /* Services */ for (int i = 0; i < HP_SVC_COUNT; i++) { off += snprintf(buf + off, sizeof(buf) - off, "%s=%s ", hp_svc_id_to_name((hp_svc_id_t)i), hp_svc_running((hp_svc_id_t)i) ? "up" : "down"); } /* Monitors */ off += snprintf(buf + off, sizeof(buf) - off, "wifi_mon=%s net_mon=%s", hp_wifi_monitor_running() ? "up" : "down", hp_net_monitor_running() ? "up" : "down"); msg_info(TAG, buf, req); return ESP_OK; } /* ============================================================ * COMMAND: hp_start_all — start all services + monitors * ============================================================ */ static esp_err_t cmd_hp_start_all( int argc, char **argv, const char *req, void *ctx) { (void)argc; (void)argv; (void)ctx; for (int i = 0; i < HP_SVC_COUNT; i++) hp_svc_start((hp_svc_id_t)i); hp_wifi_monitor_start(); hp_net_monitor_start(); msg_info(TAG, "all=started", req); return ESP_OK; } /* ============================================================ * COMMAND: hp_stop_all — stop all services + monitors * ============================================================ */ static esp_err_t cmd_hp_stop_all( int argc, char **argv, const char *req, void *ctx) { (void)argc; (void)argv; (void)ctx; for (int i = 0; i < HP_SVC_COUNT; i++) hp_svc_stop((hp_svc_id_t)i); hp_wifi_monitor_stop(); hp_net_monitor_stop(); msg_info(TAG, "all=stopped", req); return ESP_OK; } /* ============================================================ * COMMAND REGISTRATION * ============================================================ */ static const command_t hp_cmds[] = { { "hp_svc", NULL, "Service control", 2, 2, cmd_hp_svc, NULL, false }, { "hp_wifi", NULL, "WiFi monitor", 1, 1, cmd_hp_wifi, NULL, false }, { "hp_net", NULL, "Network monitor", 1, 1, cmd_hp_net, NULL, false }, { "hp_config_set", NULL, "Set config", 3, 3, cmd_hp_config_set, NULL, false }, { "hp_config_get", NULL, "Get config", 2, 2, cmd_hp_config_get, NULL, false }, { "hp_config_list", NULL, "List config", 0, 1, cmd_hp_config_list, NULL, false }, { "hp_config_reset", NULL, "Reset config", 0, 0, cmd_hp_config_reset, NULL, false }, { "hp_status", NULL, "Honeypot status", 0, 0, cmd_hp_status, NULL, false }, { "hp_start_all", NULL, "Start all services", 0, 0, cmd_hp_start_all, NULL, false }, { "hp_stop_all", NULL, "Stop all services", 0, 0, cmd_hp_stop_all, NULL, false }, }; void mod_honeypot_register_commands(void) { ESPILON_LOGI_PURPLE(TAG, "Registering honeypot commands"); hp_config_init(); for (size_t i = 0; i < sizeof(hp_cmds) / sizeof(hp_cmds[0]); i++) { command_register(&hp_cmds[i]); } } #endif /* CONFIG_MODULE_HONEYPOT */