This repository was archived by the owner on Mar 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlua_auth_plugin.c
More file actions
173 lines (147 loc) · 5.1 KB
/
lua_auth_plugin.c
File metadata and controls
173 lines (147 loc) · 5.1 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <mosquitto.h>
#include <mosquitto_plugin.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#define unused __attribute__((unused))
int topic_matches_sub(lua_State *lua)
{
const char *sub = luaL_checkstring(lua, -2);
const char *topic = luaL_checkstring(lua, -1);
bool result;
mosquitto_topic_matches_sub(sub, topic, &result);
lua_pushboolean(lua, result);
return 1;
}
void push_auth_opts(lua_State *lua, struct mosquitto_auth_opt *auth_opts, int auth_opt_count)
{
lua_newtable(lua);
for (int i = 0; i < auth_opt_count; i++) {
lua_pushstring(lua, auth_opts[i].value);
lua_setfield(lua, -2, auth_opts[i].key);
}
}
int mosquitto_auth_plugin_version(void)
{
return MOSQ_AUTH_PLUGIN_VERSION;
}
int mosquitto_auth_plugin_init(void **user_data, struct mosquitto_auth_opt *auth_opts, int auth_opt_count)
{
char *module_name;
for (int i = 0; i < auth_opt_count; i++) {
if (!strcmp(auth_opts[i].key, "auth_file")) {
module_name = strdup(auth_opts[i].value);
}
}
if (module_name == NULL) {
printf("auth_opt_auth_file config param missing\n");
exit(1);
}
lua_State *lua = luaL_newstate();
luaL_openlibs(lua);
if( luaL_loadfile(lua, module_name) || lua_pcall(lua, 0, 0, 0) ) {
printf("cant open\n");
printf("error : %s\n", lua_tostring(lua, -1) );
return MOSQ_ERR_UNKNOWN;
}
lua_register(lua, "topic_matches_sub", topic_matches_sub);
lua_pushinteger(lua, MOSQ_ACL_NONE);
lua_setglobal(lua, "MOSQ_ACL_NONE");
lua_pushinteger(lua, MOSQ_ACL_READ);
lua_setglobal(lua, "MOSQ_ACL_READ");
lua_pushinteger(lua, MOSQ_ACL_WRITE);
lua_setglobal(lua, "MOSQ_ACL_WRITE");
lua_getglobal(lua, "plugin_init");
push_auth_opts(lua, auth_opts, auth_opt_count);
if(lua_pcall(lua, 1, 0, 0) != 0) {
printf("plugin_init call failed\n");
printf("error : %s\n", lua_tostring(lua, -1) );
lua_pop(lua,1);
return MOSQ_ERR_UNKNOWN;
}
*user_data = lua;
return MOSQ_ERR_SUCCESS;
}
int mosquitto_auth_plugin_cleanup(void *user_data, struct mosquitto_auth_opt *auth_opts unused, int auth_opt_count unused)
{
lua_State *lua = (lua_State *)user_data;
lua_getglobal(lua, "plugin_cleanup");
push_auth_opts(lua, auth_opts, auth_opt_count);
if(lua_pcall(lua, 1, 0, 0) != 0) {
printf("plugin_cleanup call failed\n");
printf("error : %s\n", lua_tostring(lua, -1) );
lua_pop(lua,1);
return MOSQ_ERR_UNKNOWN;
}
lua_close(lua);
return MOSQ_ERR_SUCCESS;
}
int mosquitto_auth_security_init(void *user_data, struct mosquitto_auth_opt *auth_opts unused, int auth_opt_count unused, bool reload)
{
lua_State *lua = (lua_State *)user_data;
lua_getglobal(lua, "security_init");
push_auth_opts(lua, auth_opts, auth_opt_count);
lua_pushboolean(lua, reload);
if(lua_pcall(lua, 2, 0, 0) != 0) {
printf("security_init call failed\n");
printf("error : %s\n", lua_tostring(lua, -1) );
lua_pop(lua,1);
return MOSQ_ERR_UNKNOWN;
}
return MOSQ_ERR_SUCCESS;
}
int mosquitto_auth_security_cleanup(void *user_data, struct mosquitto_auth_opt *auth_opts unused, int auth_opt_count unused, bool reload)
{
lua_State *lua = (lua_State *)user_data;
lua_getglobal(lua, "security_cleanup");
push_auth_opts(lua, auth_opts, auth_opt_count);
lua_pushboolean(lua, reload);
if(lua_pcall(lua, 2, 0, 0) != 0) {
printf("security_cleanup call failed\n");
printf("error : %s\n", lua_tostring(lua, -1) );
lua_pop(lua,1);
return MOSQ_ERR_UNKNOWN;
}
return MOSQ_ERR_SUCCESS;
}
int mosquitto_auth_acl_check(void *user_data, const char *clientid, const char *username, const char *topic, int access)
{
lua_State *lua = (lua_State *)user_data;
lua_getglobal(lua, "acl_check");
lua_pushstring(lua, clientid);
lua_pushstring(lua, username);
lua_pushstring(lua, topic);
lua_pushinteger(lua, access);
if(lua_pcall(lua, 4, 1, 0) != 0) {
printf("acl_check call failed\n");
printf("error : %s\n", lua_tostring(lua, -1) );
lua_pop(lua,1);
return MOSQ_ERR_UNKNOWN;
}
int result = lua_toboolean(lua, -1);
lua_pop(lua,1);
return result ? MOSQ_ERR_SUCCESS : MOSQ_ERR_ACL_DENIED;
}
int mosquitto_auth_unpwd_check(void *user_data, const char *username, const char *password)
{
lua_State *lua = (lua_State *)user_data;
lua_getglobal(lua, "unpwd_check");
lua_pushstring(lua, username);
lua_pushstring(lua, password);
if(lua_pcall(lua, 2, 1, 0) != 0) {
printf("unpwd_check call failed\n");
printf("error : %s\n", lua_tostring(lua, -1) );
lua_pop(lua,1);
return MOSQ_ERR_UNKNOWN;
}
int result = lua_toboolean(lua, -1);
lua_pop(lua,1);
return result ? MOSQ_ERR_SUCCESS : MOSQ_ERR_ACL_DENIED;
}
int mosquitto_auth_psk_key_get(void *user_data unused, const char *hint unused, const char *identity unused, char *key unused, int max_key_len unused)
{
return MOSQ_ERR_AUTH;
}