mosquitto-go-auth/auth-plugin.c

160 lines
4.8 KiB
C
Raw Permalink Normal View History

2017-11-16 09:00:45 +08:00
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
2021-01-13 03:34:00 +08:00
#include <mosquitto_broker.h>
2017-11-16 09:00:45 +08:00
#include <mosquitto_plugin.h>
2021-01-13 03:34:00 +08:00
#include <mosquitto.h>
2018-10-05 05:15:22 +08:00
#if MOSQ_AUTH_PLUGIN_VERSION >= 3
# define mosquitto_auth_opt mosquitto_opt
#endif
#include "go-auth.h"
// Same constant as one in go-auth.go.
#define AuthRejected 0
#define AuthGranted 1
#define AuthError 2
2017-11-16 09:00:45 +08:00
int mosquitto_auth_plugin_version(void) {
2020-10-24 07:17:24 +08:00
#ifdef MOSQ_AUTH_PLUGIN_VERSION
#if MOSQ_AUTH_PLUGIN_VERSION == 5
2020-10-24 07:17:46 +08:00
return 4; // This is v2.0, use the backwards compatibility
#else
2020-10-24 07:17:46 +08:00
return MOSQ_AUTH_PLUGIN_VERSION;
#endif
2020-10-24 07:17:24 +08:00
#else
return 4;
2020-10-24 07:17:24 +08:00
#endif
2017-11-16 09:00:45 +08:00
}
int mosquitto_auth_plugin_init(void **user_data, struct mosquitto_auth_opt *auth_opts, int auth_opt_count) {
/*
2017-11-23 10:26:40 +08:00
Pass auth_opts hash as keys and values char* arrays to Go in order to initialize them there.
2017-11-16 09:00:45 +08:00
*/
2017-11-23 10:26:40 +08:00
2017-11-17 07:53:26 +08:00
GoInt32 opts_count = auth_opt_count;
2017-11-23 10:26:40 +08:00
char *keys[auth_opt_count];
char *values[auth_opt_count];
2017-11-23 10:26:40 +08:00
int i;
struct mosquitto_auth_opt *o;
for (i = 0, o = auth_opts; i < auth_opt_count; i++, o++) {
keys[i] = o->key;
values[i] = o->value;
2017-11-23 10:26:40 +08:00
}
GoSlice keysSlice = {keys, auth_opt_count, auth_opt_count};
GoSlice valuesSlice = {values, auth_opt_count, auth_opt_count};
char versionArray[10];
sprintf(versionArray, "%i.%i.%i", LIBMOSQUITTO_MAJOR, LIBMOSQUITTO_MINOR, LIBMOSQUITTO_REVISION);
AuthPluginInit(keysSlice, valuesSlice, opts_count, versionArray);
2017-11-16 09:00:45 +08:00
return MOSQ_ERR_SUCCESS;
}
int mosquitto_auth_plugin_cleanup(void *user_data, struct mosquitto_auth_opt *auth_opts, int auth_opt_count) {
AuthPluginCleanup();
2017-11-16 09:00:45 +08:00
return MOSQ_ERR_SUCCESS;
}
int mosquitto_auth_security_init(void *user_data, struct mosquitto_auth_opt *auth_opts, int auth_opt_count, bool reload) {
return MOSQ_ERR_SUCCESS;
}
int mosquitto_auth_security_cleanup(void *user_data, struct mosquitto_auth_opt *auth_opts, int auth_opt_count, bool reload) {
return MOSQ_ERR_SUCCESS;
}
#if MOSQ_AUTH_PLUGIN_VERSION >= 4
int mosquitto_auth_unpwd_check(void *user_data, struct mosquitto *client, const char *username, const char *password)
#elif MOSQ_AUTH_PLUGIN_VERSION >=3
2018-10-05 05:15:22 +08:00
int mosquitto_auth_unpwd_check(void *userdata, const struct mosquitto *client, const char *username, const char *password)
#else
int mosquitto_auth_unpwd_check(void *userdata, const char *username, const char *password)
#endif
{
2020-02-22 10:27:51 +08:00
#if MOSQ_AUTH_PLUGIN_VERSION >= 3
const char* clientid = mosquitto_client_id(client);
#else
const char* clientid = "";
#endif
2018-02-06 22:31:08 +08:00
if (username == NULL || password == NULL) {
2018-03-07 02:51:59 +08:00
printf("error: received null username or password for unpwd check\n");
2018-03-06 22:28:40 +08:00
fflush(stdout);
return MOSQ_ERR_AUTH;
2018-02-06 22:31:08 +08:00
}
GoUint8 ret = AuthUnpwdCheck((char *)username, (char *)password, (char *)clientid);
switch (ret)
{
case AuthGranted:
2017-11-16 09:00:45 +08:00
return MOSQ_ERR_SUCCESS;
break;
case AuthRejected:
return MOSQ_ERR_AUTH;
break;
case AuthError:
return MOSQ_ERR_UNKNOWN;
break;
default:
fprintf(stderr, "unknown plugin error: %d\n", ret);
return MOSQ_ERR_UNKNOWN;
}
2017-11-16 09:00:45 +08:00
}
#if MOSQ_AUTH_PLUGIN_VERSION >= 4
int mosquitto_auth_acl_check(void *user_data, int access, struct mosquitto *client, const struct mosquitto_acl_msg *msg)
#elif MOSQ_AUTH_PLUGIN_VERSION >= 3
2018-10-05 05:15:22 +08:00
int mosquitto_auth_acl_check(void *userdata, int access, const struct mosquitto *client, const struct mosquitto_acl_msg *msg)
#else
int mosquitto_auth_acl_check(void *userdata, const char *clientid, const char *username, const char *topic, int access)
#endif
{
#if MOSQ_AUTH_PLUGIN_VERSION >= 3
2018-10-05 05:20:40 +08:00
const char* clientid = mosquitto_client_id(client);
const char* username = mosquitto_client_username(client);
const char* topic = msg->topic;
#endif
2018-03-06 22:28:40 +08:00
if (clientid == NULL || username == NULL || topic == NULL || access < 1) {
2018-03-07 02:51:59 +08:00
printf("error: received null username, clientid or topic, or access is equal or less than 0 for acl check\n");
2018-03-06 22:28:40 +08:00
fflush(stdout);
return MOSQ_ERR_ACL_DENIED;
2018-03-06 22:28:40 +08:00
}
GoUint8 ret = AuthAclCheck((char *)clientid, (char *)username, (char *)topic, access);
switch (ret)
{
case AuthGranted:
2017-11-16 09:00:45 +08:00
return MOSQ_ERR_SUCCESS;
break;
case AuthRejected:
return MOSQ_ERR_ACL_DENIED;
break;
case AuthError:
return MOSQ_ERR_UNKNOWN;
break;
default:
fprintf(stderr, "unknown plugin error: %d\n", ret);
return MOSQ_ERR_UNKNOWN;
}
2017-11-16 09:00:45 +08:00
}
#if MOSQ_AUTH_PLUGIN_VERSION >= 4
int mosquitto_auth_psk_key_get(void *user_data, struct mosquitto *client, const char *hint, const char *identity, char *key, int max_key_len)
#elif MOSQ_AUTH_PLUGIN_VERSION >= 3
2018-10-05 05:15:22 +08:00
int mosquitto_auth_psk_key_get(void *userdata, const struct mosquitto *client, const char *hint, const char *identity, char *key, int max_key_len)
#else
int mosquitto_auth_psk_key_get(void *userdata, const char *hint, const char *identity, char *key, int max_key_len)
#endif
{
return MOSQ_ERR_AUTH;
2017-11-16 09:00:45 +08:00
}