|
3 | 3 | * |
4 | 4 | * SPDX-License-Identifier: EPL-2.0 |
5 | 5 | * |
6 | | - * SPDX-FileContributor: 2024 Espressif Systems (Shanghai) CO LTD |
| 6 | + * SPDX-FileContributor: 2024-2025 Espressif Systems (Shanghai) CO LTD |
7 | 7 | */ |
| 8 | +#include <string.h> |
8 | 9 | #include "mosquitto_internal.h" |
9 | 10 | #include "mosquitto_broker.h" |
10 | 11 | #include "memory_mosq.h" |
|
16 | 17 | #include "mosq_broker.h" |
17 | 18 |
|
18 | 19 | mosq_message_cb_t g_mosq_message_callback = NULL; |
| 20 | +mosq_connect_cb_t g_mosq_connect_callback = NULL; |
19 | 21 |
|
20 | 22 | int mosquitto_callback_register( |
21 | 23 | mosquitto_plugin_id_t *identifier, |
@@ -51,3 +53,39 @@ int plugin__handle_message(struct mosquitto *context, struct mosquitto_msg_store |
51 | 53 | } |
52 | 54 | return MOSQ_ERR_SUCCESS; |
53 | 55 | } |
| 56 | + |
| 57 | +int __real_mosquitto_unpwd_check(struct mosquitto *context); |
| 58 | + |
| 59 | +/* Wrapper function to intercept mosquitto_unpwd_check calls via linker wrapping */ |
| 60 | +int __wrap_mosquitto_unpwd_check(struct mosquitto *context) |
| 61 | +{ |
| 62 | + int rc; |
| 63 | + int password_len = 0; |
| 64 | + |
| 65 | + /* Call user's connect callback if set */ |
| 66 | + if (g_mosq_connect_callback) { |
| 67 | + /* Extract password length if password is present. |
| 68 | + * Note: MQTT passwords are binary data, but mosquitto stores them as null-terminated strings. |
| 69 | + * If password contains null bytes, strlen() will not return the full length. |
| 70 | + * This matches how mosquitto itself handles passwords in some security functions. */ |
| 71 | + if (context->password) { |
| 72 | + password_len = (int)strlen(context->password); |
| 73 | + } |
| 74 | + |
| 75 | + /* Call user callback */ |
| 76 | + rc = g_mosq_connect_callback( |
| 77 | + context->id ? context->id : "", |
| 78 | + context->username ? context->username : NULL, |
| 79 | + context->password ? context->password : NULL, |
| 80 | + password_len |
| 81 | + ); |
| 82 | + |
| 83 | + /* If callback rejects (returns non-zero), return AUTH error immediately */ |
| 84 | + if (rc != 0) { |
| 85 | + return MOSQ_ERR_AUTH; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /* Call the original function */ |
| 90 | + return __real_mosquitto_unpwd_check(context); |
| 91 | +} |
0 commit comments