-
Notifications
You must be signed in to change notification settings - Fork 174
/
auth-plugin.c
159 lines (136 loc) · 4.76 KB
/
auth-plugin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <mosquitto_broker.h>
#include <mosquitto_plugin.h>
#include <mosquitto.h>
#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
int mosquitto_auth_plugin_version(void) {
#ifdef MOSQ_AUTH_PLUGIN_VERSION
#if MOSQ_AUTH_PLUGIN_VERSION == 5
return 4; // This is v2.0, use the backwards compatibility
#else
return MOSQ_AUTH_PLUGIN_VERSION;
#endif
#else
return 4;
#endif
}
int mosquitto_auth_plugin_init(void **user_data, struct mosquitto_auth_opt *auth_opts, int auth_opt_count) {
/*
Pass auth_opts hash as keys and values char* arrays to Go in order to initialize them there.
*/
GoInt32 opts_count = auth_opt_count;
char *keys[auth_opt_count];
char *values[auth_opt_count];
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;
}
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);
return MOSQ_ERR_SUCCESS;
}
int mosquitto_auth_plugin_cleanup(void *user_data, struct mosquitto_auth_opt *auth_opts, int auth_opt_count) {
AuthPluginCleanup();
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
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
{
#if MOSQ_AUTH_PLUGIN_VERSION >= 3
const char* clientid = mosquitto_client_id(client);
#else
const char* clientid = "";
#endif
if (username == NULL || password == NULL) {
printf("error: received null username or password for unpwd check\n");
fflush(stdout);
return MOSQ_ERR_AUTH;
}
GoUint8 ret = AuthUnpwdCheck((char *)username, (char *)password, (char *)clientid);
switch (ret)
{
case AuthGranted:
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;
}
}
#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
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
const char* clientid = mosquitto_client_id(client);
const char* username = mosquitto_client_username(client);
const char* topic = msg->topic;
#endif
if (clientid == NULL || username == NULL || topic == NULL || access < 1) {
printf("error: received null username, clientid or topic, or access is equal or less than 0 for acl check\n");
fflush(stdout);
return MOSQ_ERR_ACL_DENIED;
}
GoUint8 ret = AuthAclCheck((char *)clientid, (char *)username, (char *)topic, access);
switch (ret)
{
case AuthGranted:
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;
}
}
#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
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;
}