-
Notifications
You must be signed in to change notification settings - Fork 17
/
mod_admin_rest.lua
907 lines (697 loc) · 22.7 KB
/
mod_admin_rest.lua
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
local url = require "socket.url";
local jid = require "util.jid";
local stanza = require "util.stanza";
local b64 = require "util.encodings".base64;
local sp = require "util.encodings".stringprep;
local JSON = { };
-- Use lua-cjson if it is available
local ok, error = pcall(function() JSON = require "cjson.safe" end);
-- Fall back to util.json
if not ok or error then JSON = require "util.json" end
local um = require "core.usermanager";
local rm = require "core.rostermanager";
local mm = require "core.modulemanager";
local hm = require "core.hostmanager";
local sm = require "core.storagemanager";
local hostname = module:get_host();
local secure = module:get_option_boolean("admin_rest_secure", false);
local base_path = module:get_option_string("admin_rest_base", "/admin_rest");
local whitelist = module:get_option_array("admin_rest_whitelist", nil);
local function to_set(list)
local l = #list;
if l == 0 then return nil end
local set = { };
for i=1, l do set[list[i]] = true end
return set;
end
-- Convert whitelist into a whiteset for efficient lookup
if whitelist then whitelist = to_set(whitelist) end
local function split_path(path)
local result = {};
local pattern = "(.-)/";
local last_end = 1;
local s, e, cap = path:find(pattern, 1);
while s do
if s ~= 1 or cap ~= "" then
table.insert(result, cap);
end
last_end = e + 1;
s, e, cap = path:find(pattern, last_end);
end
if last_end <= #path then
cap = path:sub(last_end);
table.insert(result, cap);
end
return result;
end
local function parse_path(path)
local split = split_path(url.unescape(path));
return {
route = split[2];
resource = split[3];
attribute = split[4];
};
end
-- Parse request Authentication headers. Return username, password
local function parse_auth(auth)
return b64.decode(auth:match("[^ ]*$") or ""):match("([^:]*):(.*)");
end
-- Make a *one-way* subscription. User will see when contact is online,
-- contact will not see when user is online.
local function subscribe(user_jid, contact_jid)
local user_node, user_host = jid.split(user_jid);
local contact_username, contact_host = jid.split(contact_jid);
-- Update user's roster to say subscription request is pending...
rm.set_contact_pending_out(user_node, user_host, contact_jid);
-- Update contact's roster to say subscription request is pending...
rm.set_contact_pending_in(contact_username, contact_host, user_jid);
-- Update contact's roster to say subscription request approved...
rm.subscribed(contact_username, contact_host, user_jid);
-- Update user's roster to say subscription request approved...
rm.process_inbound_subscription_approval(user_node, user_host, contact_jid);
end
-- Unsubscribes user from contact (not contact from user, if subscribed).
function unsubscribe(user_jid, contact_jid)
local user_node, user_host = jid.split(user_jid);
local contact_username, contact_host = jid.split(contact_jid);
-- Update user's roster to say subscription is cancelled...
rm.unsubscribe(user_node, user_host, contact_jid);
-- Update contact's roster to say subscription is cancelled...
rm.unsubscribed(contact_username, contact_host, user_jid);
end
local function Response(status_code, message, array)
local response = { };
local ok, error = pcall(function()
message = JSON.encode({ result = message });
end);
if not ok or error then
response.status_code = 500
response.body = "Failed to encode JSON response";
else
response.status_code = status_code;
response.body = message;
end
return response;
end
-- Build static responses
local RESPONSES = {
missing_auth = Response(400, "Missing authorization header");
invalid_auth = Response(400, "Invalid authentication details");
auth_failure = Response(401, "Authentication failure");
unauthorized = Response(401, "User must be an administrator");
decode_failure = Response(400, "Request body is not valid JSON");
invalid_path = Response(404, "Invalid request path");
invalid_method = Response(405, "Invalid request method");
invalid_body = Response(400, "Body does not exist or is malformed");
invalid_host = Response(404, "Host does not exist or is malformed");
invalid_user = Response(404, "User does not exist or is malformed");
invalid_contact = Response(404, "Contact does not exist or is malformed");
drop_message = Response(501, "Message dropped per configuration");
internal_error = Response(500, "Internal server error");
pong = Response(200, "PONG");
};
local function respond(event, res, headers)
local response = event.response;
if headers then
for header, data in pairs(headers) do
response.headers[header] = data;
end
end
response.headers.content_type = "application/json";
response.status_code = res.status_code;
response:send(res.body);
end
local function get_host(hostname)
return hosts[hostname];
end
local function get_sessions(hostname)
local host = get_host(hostname);
return host and host.sessions;
end
local function get_session(hostname, username)
local sessions = get_sessions(hostname);
return sessions and sessions[username];
end
local function get_connected_users(hostname)
local sessions = get_sessions(hostname);
local users = { };
for username, user in pairs(sessions or {}) do
for resource, _ in pairs(user.sessions or {}) do
table.insert(users, {
username = username,
resource = resource
});
end
end
return users;
end
local function get_recipient(hostname, username)
local session = get_session(hostname, username)
local offline = not session and um.user_exists(username, hostname);
return session, offline;
end
local function normalize_user(user)
local cleaned = { };
cleaned.connected = user.connected or false;
cleaned.sessions = { };
cleaned.roster = { };
for resource, session in pairs(user.sessions or {}) do
local c_session = {
resource = resource;
id = session.conn.id;
ip = session.conn._ip;
port = session.conn._port;
secure = session.secure;
}
table.insert(cleaned.sessions, c_session);
end
if user.roster and #user.roster > 0 then
cleaned.roster = user.roster;
end
return cleaned;
end
local function get_user_connected(event, path, body)
local username = sp.nodeprep(path.resource);
if not username then
return respond(event, RESPONSES.invalid_user);
end
local jid = jid.join(username, hostname);
local connected = get_session(hostname, username);
local response;
if connected then
response = Response(200, { connected = true });
else
response = Response(404, { connected = false });
end
respond(event, response);
end
local function get_user(event, path, body)
local username = sp.nodeprep(path.resource);
if not username then
return respond(event, RESPONSES.invalid_user);
end
if not um.user_exists(username, hostname) then
local joined = jid.join(username, hostname)
return respond(event, Response(404, "User does not exist: " .. joined));
end
if path.attribute == "connected" then
return get_user_connected(event, path, body);
end
local user = { hostname = hostname, username = username };
local session = get_session(hostname, username);
if session then
user.connected = true;
user.roster = session.roster;
user.sessions = session.sessions;
else
user.roster = rm.load_roster(username, hostname);
end
local response = { user = normalize_user(user) };
respond(event, Response(200, response));
end
local function get_users(event, path, body)
local sessions = get_sessions(hostname);
if path.resource == "count" then
local count = 0;
for _ in pairs(sessions or {}) do
count = count + 1;
end
respond(event, Response(200, { count = count }));
else
local users = { };
for username, user in pairs(sessions or {}) do
for resource, _ in pairs(user.sessions or {}) do
table.insert(users, {
username = username,
resource = resource
});
end
end
respond(event, Response(200, { users = users, count = #users }));
end
end
local function get_roster(event, path, body)
local username = sp.nodeprep(path.resource);
if not username then
return respond(event, RESPONSES.invalid_user);
end
if not um.user_exists(username, hostname) then
local joined = jid.join(username, hostname)
return respond(event, Response(404, "User does not exist: " .. joined));
end
local roster = rm.load_roster(username, hostname);
local query = {};
for jid in pairs(roster) do
if jid then
local grouplist = {};
for group in pairs(roster[jid].groups) do
table.insert(grouplist, group);
end
table.insert(query, {"item", {
jid = jid,
subscription = roster[jid].subscription,
ask = roster[jid].ask,
name = roster[jid].name,
group = grouplist
}});
end
end
respond(event, Response(200, { roster = query, count = #query }));
end
local function add_roster(event, path, body)
local username = sp.nodeprep(path.resource);
if not username then
return respond(event, RESPONSES.invalid_user);
end
local user_jid = jid.join(username, hostname);
local contact_jid = body["contact"];
if not contact_jid then
return respond(event, RESPONSES.invalid_contact);
end
if not um.user_exists(username, hostname) then
return respond(event, Response(404, "User does not exist: " .. user_jid));
end
-- Make a mutual subscription between jid1 and jid2. Each JID will see
-- when the other one is online.
subscribe(user_jid, contact_jid);
subscribe(contact_jid, user_jid);
local result = 'Roster registered: ' .. user_jid .. ' and ' .. contact_jid;
respond(event, Response(200, result));
module:fire_event("roster-registered", {
username = username;
hostname = hostname;
contact_jid = contact_jid;
source = "mod_admin_rest";
})
module:log("info", result);
end
local function remove_roster(event, path, body)
local username = sp.nodeprep(path.resource);
if not username then
return respond(event, RESPONSES.invalid_user);
end
local user_jid = jid.join(username, hostname)
if not um.user_exists(username, hostname) then
return respond(event, Response(404, "User does not exist: " .. user_jid));
end
local contact_jid = body["contact"];
if not contact_jid then
return respond(event, RESPONSES.invalid_contact);
end
-- Make a mutual subscription between jid1 and jid2. Each JID will see
-- when the other one is online.
unsubscribe(user_jid, contact_jid);
unsubscribe(contact_jid, user_jid);
local roster = rm.load_roster(username, hostname);
roster[contact_jid] = nil;
rm.save_roster(username, hostname, roster);
local result = 'Roster deleted: ' .. user_jid .. ' and ' .. contact_jid;
respond(event, Response(200, result));
module:fire_event("roster-deleted", {
username = username;
hostname = hostname;
contact_jid = contact_jid;
source = "mod_admin_rest";
})
module:log("info", result);
end
local function add_user(event, path, body)
local username = sp.nodeprep(path.resource);
local password = body["password"];
local regip = body["regip"];
if not username then
return respond(event, RESPONSES.invalid_path);
end
if not password then
return respond(event, RESPONSES.invalid_body);
end
local jid = jid.join(username, hostname);
if um.user_exists(username, hostname) then
return respond(event, Response(409, "User already exists: " .. jid));
end
if not um.create_user(username, password, hostname) then
return respond(event, RESPONSES.internal_error);
end
local result = "User registered: " .. jid;
respond(event, Response(201, result));
module:fire_event("user-registered", {
username = username,
host = hostname,
ip = regip,
source = "mod_admin_rest"
})
module:log("info", result);
end
local function remove_user(event, path, body)
local username = sp.nodeprep(path.resource);
if not username then
return respond(event, RESPONSES.invalid_user);
end
local jid = jid.join(username, hostname);
if not um.user_exists(username, hostname) then
return respond(event, Response(404, "User does not exist: " .. jid));
end
if not um.delete_user(username, hostname) then
return respond(event, RESPONSES.internal_error);
end
respond(event, Response(200, "User deleted: " .. jid));
module:fire_event("user-deleted", {
username = username;
hostname = hostname;
source = "mod_admin_rest";
});
module:log("info", "Deregistered user: " .. jid);
end
local function patch_user(event, path, body)
local username = sp.nodeprep(path.resource);
local attribute = path.attribute;
if not (username and attribute) then
return respond(event, RESPONSES.invalid_path);
end
local jid = jid.join(username, hostname);
if not um.user_exists(username, hostname) then
return respond(event, Response(404, "User does not exist: " .. jid));
end
if attribute == "password" then
local password = body.password;
if not password then
return respond(event, RESPONSES.invalid_body);
end
if not um.set_password(username, password, hostname) then
return respond(event, RESPONSES.internal_error);
end
end
local result = "User modified: " .. jid;
respond(event, Response(200, result));
module:log("info", result);
end
local function offline_enabled()
return mm.is_loaded(hostname, "offline")
or mm.is_loaded(hostname, "offline_bind")
or false;
end
local multicast_prefix = module:get_option_string("admin_rest_multicast_prefix", nil);
local function send_multicast(event, path, body)
local recipients = body.recipients;
local sent = 0;
local delayed = 0;
for i=1, #recipients do
repeat
local recipient = recipients[i];
local msg = recipient.message;
local node = recipient.to;
if not node or not msg then break end
if multicast_prefix then
msg = multicast_prefix .. msg;
end
local session, offline = get_recipient(hostname, node);
if not session and not offline then break end
local attrs = { from = hostname, to = jid.join(node, hostname) };
local message = stanza.message(attrs, msg);
if offline and offline_enabled() then
module:fire_event("message/offline/handle", {
stanza = stanza.deserialize(message);
});
delayed = delayed + 1;
elseif session then
for _, session in pairs(session.sessions or {}) do
session.send(message);
end
sent = sent + 1;
end
until true
end
local result;
if sent + delayed > 0 then
result = "Message multicasted to users: " .. sent .. "/" .. delayed;
respond(event, Response(200, result));
else
result = "No multicast recipients";
respond(event, Response(404, result));
end
module:log("info", result);
end
local message_prefix = module:get_option_string("admin_rest_message_prefix", nil);
local function send_message(event, path, body)
local username = sp.nodeprep(path.resource);
if not username then
if body.recipients then
return send_multicast(event, path, body);
else
return respond(event, RESPONSES.invalid_user);
end
end
local session, offline = get_recipient(hostname, username);
if not session and not offline then
return respond(event, RESPONSES.invalid_user);
end
if message_prefix then
body.message = message_prefix .. body.message;
end
local jid = jid.join(username, hostname);
local message = stanza.message({ to = jid, from = hostname}, body.message);
if offline then
if not offline_enabled() then
respond(event, RESPONSES.drop_message);
return
else
respond(event, Response(202, "Message sent to offline queue: " .. jid));
module:fire_event("message/offline/handle", {
stanza = stanza.deserialize(message)
});
return
end
end
for resource, session in pairs(session.sessions or {}) do
session.send(message)
end
local result = "Message sent to user: " .. jid;
respond(event, Response(200, result));
module:log("info", result);
end
local broadcast_prefix = module:get_option_string("admin_rest_broadcast_prefix", nil);
local function broadcast_message(event, path, body)
local attrs = { from = hostname };
local count = 0;
if broadcast_prefix then
body.message = broadcast_prefix .. body.message;
end
for username, session in pairs(get_sessions(hostname) or {}) do
attrs.to = jid.join(username, hostname);
local message = stanza.message(attrs, body.message);
for _, session in pairs(session.sessions or {}) do
session.send(message);
end
count = count + 1;
end
respond(event, Response(200, { count = count }));
if count > 0 then
module:log("info", "Message broadcasted to users: " .. count);
end
end
function get_module(event, path, body)
local modulename = path.resource;
if not modulename then
return respond(event, RESPONSES.invalid_path);
end
local result = { module = modulename };
local status;
if not mm.is_loaded(hostname, modulename) then
result.loaded = false;
status = 404;
else
result.loaded = true;
status = 200;
end
respond(event, Response(status, result))
end
function get_modules(event, path, body)
local modules = mm.get_modules(hostname);
local list = { }
for name in pairs(modules or {}) do
table.insert(list, name);
end
local result = { count = #list };
if path.resource ~= "count" then
result.modules = list;
end
respond(event, Response(200, result));
end
function load_module(event, path, body)
local modulename = path.resource;
local fn = "load";
if mm.is_loaded(hostname, modulename) then fn = "reload" end
if not mm[fn](hostname, modulename) then
return respond(event, RESPONSES.internal_error);
end
local result = "Module loaded: " .. modulename;
respond(event, Response(200, result));
module:log("info", result);
end
function unload_module(event, path, body)
local modulename = path.resource;
if not mm.is_loaded(hostname, modulename) then
return respond(event, Response(404, "Module is not loaded:" .. modulename));
end
mm.unload(hostname, modulename);
local result = "Module unloaded: " .. modulname;
respond(event, Response(200, result));
module:log("info", result);
end
local function get_whitelist(event, path, body)
local list = { };
if whitelist then
for ip, _ in pairs(whitelist) do
table.insert(list, ip);
end
end
respond(event, Response(200, { whitelist = list, count = #list }));
end
local function add_whitelisted(event, path, body)
local ip = path.resource;
if not whitelist then whitelist = { } end
whitelist[ip] = true;
local result = "IP added to whitelist: " .. ip;
respond(event, Response(200, result));
module:log("warn", result);
end
local function remove_whitelisted(event, path, body)
local ip = path.resource;
if not whitelist or not whitelist[ip] then
return respond(event, Response(404, "IP is not whitelisted: " .. ip));
end
local new_list = { };
for whitelisted, _ in pairs(whitelist) do
if whitelisted ~= ip then
new_list[whitelisted] = true;
end
end
whitelist = new_list;
local result = "IP removed from whtielist: " .. ip;
respond(event, Response(200, result));
module:log("warn", result);
end
local function ping(event, path, body)
return respond(event, RESPONSES.pong);
end
--Routes and suitable request methods
local ROUTES = {
ping = {
GET = ping;
};
user = {
GET = get_user;
POST = add_user;
DELETE = remove_user;
PATCH = patch_user;
};
user_connected = {
GET = get_user_connected;
};
users = {
GET = get_users;
};
roster = {
GET = get_roster;
POST = add_roster;
DELETE = remove_roster;
};
message = {
POST = send_message;
};
broadcast = {
POST = broadcast_message;
};
modules = {
GET = get_modules;
};
module = {
GET = get_module;
PUT = load_module;
DELETE = unload_module;
};
whitelist = {
GET = get_whitelist;
PUT = add_whitelisted;
DELETE = remove_whitelisted;
}
};
--Reserved top-level request routes
local RESERVED = to_set({ "admin" });
--Entry point for incoming requests.
--Authenticate admin and route request.
local function handle_request(event)
local request = event.request;
-- Check whitelist for IP
if whitelist and not whitelist[request.conn._ip] then
return respond(event, { status_code = 401, message = nil });
end
-- ********** Authenticate ********** --
-- Prevent insecure requests
if secure and not request.secure then return end
-- Request must have authorization header
if not request.headers["authorization"] then
return respond(event, RESPONSES.missing_auth);
end
local auth = request.headers.authorization;
local username, password = parse_auth(auth);
username = jid.prep(username);
-- Validate authentication details
if not username or not password then
return respond(event, RESPONSES.invalid_auth);
end
local user_node, user_host = jid.split(username);
-- Validate host
if not hosts[user_host] then
return respond(event, RESPONSES.invalid_host);
end
-- Authenticate user
if not um.test_password(user_node, user_host, password) then
return respond(event, RESPONSES.auth_failure);
end
-- ********** Route ********** --
local path = parse_path(request.path);
local route, hostname = path.route, hostname;
-- Restrict to admin
if not um.is_admin(username, hostname) then
return respond(event, RESPONSES.unauthorized);
end
local handlers = ROUTES[route];
-- Confirm that route exists
if not route or not handlers then
return respond(event, RESPONSES.invalid_path);
end
-- Confirm that the host exists
if not RESERVED[route] then
if not hostname or not hosts[hostname] then
return respond(event, RESPONSES.invalid_host);
end
end
local handler = handlers[request.method];
-- Confirm that handler exists for method
if not handler then
return respond(event, RESPONSES.invalid_method);
end
local body = { };
-- Parse JSON request body
if request.body and #request.body > 0 then
if not pcall(function() body = JSON.decode(request.body) end) then
return respond(event, RESPONSES.decode_failure);
end
if not body["regip"] then
body["regip"] = request.conn._ip;
end
end
return handler(event, path, body);
end
module:depends("http");
module:provides("http", {
name = base_path:gsub("^/", "");
route = {
["GET /*"] = handle_request;
["POST /*"] = handle_request;
["PUT /*"] = handle_request;
["DELETE /*"] = handle_request;
["PATCH /*"] = handle_request;
};
})