-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
520 lines (483 loc) · 15.2 KB
/
init.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
--[[
mt2fa server side mod
--]]
-- allocate API stuff
local S = minetest.settings
assert(S)
local req_reg = S:get_bool("mt2fa.require_registration") or false
local req_auth = S:get_bool("mt2fa.require_authentication") or false
local reg_api = S:get("mt2fa.api_server") or "https://mt2fa.foo-projects.org/mt2fa"
local reg_id = S:get("mt2fa.server_id") --nil when first starting server
local reg_grace = S:get("mt2fa.grace") or 300 -- period before enforcement starts
minetest.log("verbose", "mt2fa: requiring: registration(" .. tostring(req_reg) .. "), " ..
"authentication(" .. tostring(req_auth) .. ")")
local http = minetest.request_http_api()
assert(http, "HTTP API unavailable. Please add `mt2fa` to secure.trusted_mods in minetest.conf!")
local message = {
REG = "Registration request sent.",
AUTH = "Authentication request sent.",
ACCT = "Verifying account information, please wait.",
SERVER = "Server registration request sent.",
SERVERIP = "Server IP change request sent.",
}
local forms = {
email =
"size[10,8]"..
"label[1,1;" ..
"Please type your email address. You will need to be\n" ..
"able to receive email and click on a link that is sent\n" ..
"to you at the address. If you lose the email address,\n" ..
"you may lose all access to this server. You can find the\n" ..
"privacy policy here: mt2fa.foo-projects.org/privacy.html\n" ..
"By providing your email address, you consent to its use\n" ..
"for the purpose of providing you this service.\n" ..
"]" ..
"button_exit[6,7;3,1;OK;OK]"..
"field[1,6;8,1;email;email address;]",
register =
"size[10,8]"..
"label[1,1;" ..
"This server allows you to register your account using\n" ..
"2-factor authentication and link your account to an\n" ..
"email address. This allows you to recover your account\n" ..
"on this server in case you forget your password. It also\n" ..
"may prevent unauthorized access to your account on this\n" ..
"server or any other server that uses this service. This\n" ..
"process is voluntary. If you decide to opt out, you can\n" ..
"at any time later opt in.]" ..
"button_exit[1,7;3,1;nothanks;No thanks]"..
"button_exit[6,7;3,1;register;Register]",
}
local logins = {}
local privs = {}
--
-- loop our waiting message. If the player presses `escape` it will just restart it
-- as long as our context is valid. If the network code callback is reached, we
-- invalidate the context and abort this wait screen.
--
local function do_wait(player, context)
fsc.show(player:get_player_name(),
"size[10,8]label[1,1;" .. context.message .. "]",
context,
function(p, fields, c)
minetest.after(0.1, do_wait, p, c)
return true
end)
end
local function do_msg(player, context)
fsc.show(player:get_player_name(),
"size[10,8]label[1,1;" .. context.message .. "]" ..
"button_exit[6,7;3,1;OK;OK]",
context,
function(p, fields, c)
return true
end)
end
local function do_allow(name)
logins[name] = nil
if privs[name] then
minetest.set_player_privs(name, privs[name])
privs[name] = nil
end
end
local function send_request(request_type, player, context)
-- rate limiting code
if not context.count then
context.count = 1
else
context.count = context.count + 1
if context.count >= 60 then
-- stop causing more packets to the server when it's not going to succeed.
context.message = "Error: too many packets sent to server. If you're " ..
"not logged in or authenticated by now, something has gone wrong badly."
do_msg(player, context)
return
end
end
minetest.log("action", "mt2fa: sending " .. request_type .. " for " .. player:get_player_name())
-- initial player message may be needed
if message[request_type] then
context.message = message[request_type]
end
do_wait(player, context)
-- perform the request
http.fetch({
url = reg_api,
post_data = minetest.write_json({
request_type = request_type,
player = player:get_player_name(),
server_id = reg_id,
email = context.email,
cookie = context.cookie,
server_data = context.server_data,
}),
timeout = 15,
}, function(res)
assert(res.completed)
if not player then
-- the player could have logged off
minetest.log("verbose", "player left, discarding packet result from mt2fa server")
return
end
if not res.succeeded then
-- major problem, retry
minetest.log("error", "mt2fa: server replied with an error code")
do_wait(player, context)
minetest.after(15, send_request, request_type, player, context)
return
end
local data = minetest.parse_json(res.data)
if not data then
-- major problem, retry
minetest.log("error", "mt2fa: failed to parse JSON from mt2fa server")
do_wait(player, context)
minetest.after(15, send_request, request_type, player, context)
return
end
minetest.log("action", "mt2fa: received " .. data.result .. " for " .. player:get_player_name())
-- update user message
context.message = data.info
-- process result
if request_type == "REG" then
if data.result == "REGPEND" then
context.cookie = data.data.Cookie
minetest.after(10, send_request, "REGSTAT", player, context)
elseif data.result == "REGOK" then
-- record email address for this user in a user attribute, permanently
player:set_attribute("mt2fa.registered", "1")
local reg_privs = S:get("mt2fa.registration_grants")
local name = player:get_player_name()
if reg_privs and reg_privs ~= "" then
local pr = minetest.get_player_privs(name)
for _, v in pairs(reg_privs:split(",")) do
pr[v] = true
end
minetest.set_player_privs(name, pr)
end
-- signal success to the player
fsc.show(name,
"size[10,8]label[1,1;" .. context.message .. "]" ..
"button_exit[6,7;3,1;OK;OK]",
context,
function(p, fields, c)
-- proceed to authentication if needed
if req_auth or p:get_attribute("mt2fa.auth_required") then
send_request("AUTH", p, c)
else
send_request("ACCT", p, c)
end
return true
end)
else
-- display error, retry new email?
do_wait(player, context)
minetest.after(15, send_request, request_type, player, context)
return
end
elseif request_type == "REGSTAT" then
if data.result == "REGPEND" then
minetest.after(10, send_request, "REGSTAT", player, context)
do_wait(player, context)
elseif data.result == "REGOK" then
-- record email address for this user in a user attribute, permanently
player:set_attribute("mt2fa.registered", "1")
-- signal success to the player
fsc.show(player:get_player_name(),
"size[10,8]label[1,1;" .. context.message .. "]" ..
"button_exit[6,7;3,1;OK;OK]",
context,
function(p, fields, c)
-- proceed to authentication if needed
if req_auth or p:get_attribute("mt2fa.auth_required") then
send_request("AUTH", p, c)
else
send_request("ACCT", p, c)
end
return true
end)
else
-- error, retry?
do_wait(player, context)
minetest.after(15, send_request, request_type, player, context)
return
end
elseif request_type == "SERVER" then
if data.result == "SERVERPEND" then
context.cookie = data.data.Cookie
minetest.after(10, send_request, "SERVERSTAT", player, context)
else
-- error, retry?
do_wait(player, context)
minetest.after(15, send_request, request_type, player, context)
return
end
elseif request_type == "SERVERSTAT" then
if data.result == "SERVEROK" then
-- signal success to the player
S:set("mt2fa.server_id", data.data.Server_id)
reg_id = data.data.Server_id
minetest.log("action", "Your server id is: " .. reg_id ..
". It will be written to your minetest.conf variable." ..
" Please make note of it in case you want to move your server.")
do_msg(player, context)
minetest.chat_send_player(player:get_player_name(), "Your server id is: " .. reg_id ..
". It will be written to your minetest.conf variable. " ..
"Please make note of it in case you want to move your server.")
elseif data.result == "SERVERPEND" then
minetest.after(10, send_request, "SERVERSTAT", player, context)
do_wait(player, context)
else
-- unknown data error
do_msg(player, context)
return
end
elseif request_type == "SERVERIP" then
if data.result == "SERVERIPPEND" then
context.cookie = data.data.Cookie
minetest.after(10, send_request, "SERVERIPSTAT", player, context)
else
-- error, retry?
do_wait(player, context)
minetest.after(15, send_request, request_type, player, context)
return
end
elseif request_type == "SERVERIPSTAT" then
if data.result == "SERVERIPOK" then
-- signal success to the player
do_msg(player, context)
elseif data.result == "SERVERIPPEND" then
minetest.after(10, send_request, "SERVERIPSTAT", player, context)
do_wait(player, context)
else
-- unknown data error
do_msg(player, context)
return
end
elseif request_type == "ACCT" then
if data.result == "ACCTOK" then
if data.auth_required == "1" then
-- do auth
send_request("AUTH", player, context)
else
local name = player:get_player_name()
minetest.close_formspec(player:get_player_name(), "")
minetest.chat_send_player(name, context.message)
do_allow(name)
end
else
do_msg(player, context)
end
elseif request_type == "AUTH" then
if data.result == "AUTHPEND" then
context.cookie = data.data.Cookie
-- no need to send this right away so fast
minetest.after(10, send_request, "AUTHSTAT", player, context)
else
do_msg(player, context)
end
elseif request_type == "AUTHSTAT" then
if data.result == "AUTHOK" then
local name = player:get_player_name()
minetest.close_formspec(player:get_player_name(), "")
minetest.chat_send_player(name, context.message)
do_allow(name)
elseif data.result == "AUTHPEND" then
minetest.after(10, send_request, "AUTHSTAT", player, context)
else
do_msg(player, context)
-- boot player?
end
else
assert("we sent an incorrect request, our bad")
end
end)
end
-- join/leave stuff
minetest.after(0, function()
--
-- warn through log entries that registration has not been completed yet
--
if not reg_id then
minetest.log("error", "mt2fa: This server needs to be registered with a mt2fa server in order to" ..
"allow players without the \"server\" priv to log on. To register, use `/mt2fa register <email>` " ..
"to start the registation process.")
end
end)
minetest.register_on_joinplayer(function(player)
--
-- if uninitialized, reject non-admin players
--
if not reg_id then
if minetest.check_player_privs(player, {server = true}) then
minetest.chat_send_player(player:get_player_name(),
"This server needs to be registered with a mt2fa server in order to allow " ..
"players without the \"server\" priv to log on. To register, use `/mt2fa register <email>` " ..
"to start the registration process.")
return
else
minetest.kick_player(player:get_player_name(),
"This server is not properly initialized yet. Come back later.")
return
end
end
--
-- Player registration and Authentication
--
-- start the process
local name = player:get_player_name()
logins[name] = os.time() + reg_grace
privs[name] = minetest.get_player_privs(name)
minetest.set_player_privs(name, {})
local reg = player:get_attribute("mt2fa.registered")
if req_reg or player:get_attribute("mt2fa.auth_required") == "1" then
if reg ~= "1" then
-- ask for email
fsc.show(name, forms.email, {},
function(p, fields, c)
if not fields.email or fields.email == "" or
not fields.email:find("@") then
fields.quit = nil
return
--FIXME reopen
end
send_request("REG", p, {
required = true,
email = fields.email,
})
end
)
elseif player:get_attribute("mt2fa.auth_required") == "1" then
-- already registered, must auth
send_request("AUTH", player, {required = true})
else
-- maybe-auth if server asks for it
send_request("ACCT", player, {})
end
else
-- offer to register
fsc.show(name, forms.register, {required = false},
function(p, fields, c)
if not fields.register then
-- if the user opts out, return privs
do_allow(name)
return true
end
-- ask for email
fsc.show(name, forms.email, c,
function(pl, f, co)
if not f.email or f.email == "" or
not f.email:find("@") then
do_allow(name)
return
end
send_request("REG", pl, {email = f.email})
end
)
return true
end)
end
end)
minetest.register_on_leaveplayer(function(player)
do_allow(player:get_player_name())
end)
local function do_grace()
minetest.after(7, do_grace)
local t = os.time()
for k, v in pairs(logins) do
if v > t then
minetest.kick_player(k, "You did not authenticate or register within the allowed time limit.")
end
end
end
minetest.after(reg_grace, do_grace)
local function do_updates(first)
minetest.after(15 * 60, do_updates, false)
if not reg_id then
return
end
local post_data = {
request_type = "UPDATES",
server_id = reg_id,
}
if first then
post_data.server_data = {
owner = S:get("name"),
name = S:get("server_name"),
address = S:get("server_address"),
url = S:get("server_url"),
announce = S:get("server_announce"),
announce_url = S:get("serverlist_url"),
}
end
http.fetch({
url = reg_api,
post_data = minetest.write_json(post_data),
timeout = 15,
}, function(res)
assert(res.completed)
if not res.succeeded then
-- major problem, retry
minetest.log("error", "mt2fa: server replied with an error code")
return
end
local data = minetest.parse_json(res.data)
if not data then
-- major problem, retry
minetest.log("error", "mt2fa: failed to parse JSON from mt2fa server")
return
end
minetest.log("action", "mt2fa: received " .. data.result)
end)
end
minetest.after(3, do_updates, true)
--
-- admin/server command
--
minetest.register_chatcommand("mt2fa", {
params = "mt2fa server",
description = "Administrate 2-factor authentication services",
privs = {server = true},
func = function(name, param)
if param:sub(1,9) == "register " then
if reg_id then
return false, "This server already has a server ID - you have already registered it."
end
local email = param:sub(10)
local player = minetest.get_player_by_name(name)
assert(player)
send_request("SERVER", player, {
email = email,
server_data = {
owner = S:get("name"),
name = S:get("server_name"),
address = S:get("server_address"),
url = S:get("server_url"),
announce = S:get("server_announce"),
announce_url = S:get("serverlist_url"),
},
})
elseif param:sub(1,9) == "ipchange " then
local email = param:sub(10)
local player = minetest.get_player_by_name(name)
assert(player)
send_request("SERVERIP", player, {
email = email,
server_data = {
owner = S:get("name"),
name = S:get("server_name"),
address = S:get("server_address"),
url = S:get("server_url"),
announce = S:get("server_announce"),
announce_url = S:get("serverlist_url"),
},
})
else
return true, "Usage: /mt2fa [register|ipchange] <email>"
end
end
})
--
-- TODO: add cli commands for server operator to obtain and renew mt2fa tokens
-- for the server.
--
-- TODO: cli for modifying auth_required for local players