Software /
code /
prosody
Comparison
plugins/mod_admin_telnet.lua @ 6054:7a5ddbaf758d
Merge 0.9->0.10
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 02 Apr 2014 17:41:38 +0100 |
parent | 6017:ac0879a8190a |
child | 6067:dab7ad6fa23c |
comparison
equal
deleted
inserted
replaced
6053:2f93a04564b2 | 6054:7a5ddbaf758d |
---|---|
1 -- Prosody IM | 1 -- Prosody IM |
2 -- Copyright (C) 2008-2010 Matthew Wild | 2 -- Copyright (C) 2008-2010 Matthew Wild |
3 -- Copyright (C) 2008-2010 Waqas Hussain | 3 -- Copyright (C) 2008-2010 Waqas Hussain |
4 -- | 4 -- |
5 -- This project is MIT/X11 licensed. Please see the | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
7 -- | 7 -- |
8 | 8 |
9 module:set_global(); | 9 module:set_global(); |
15 | 15 |
16 local _G = _G; | 16 local _G = _G; |
17 | 17 |
18 local prosody = _G.prosody; | 18 local prosody = _G.prosody; |
19 local hosts = prosody.hosts; | 19 local hosts = prosody.hosts; |
20 local incoming_s2s = prosody.incoming_s2s; | |
21 | 20 |
22 local console_listener = { default_port = 5582; default_mode = "*a"; interface = "127.0.0.1" }; | 21 local console_listener = { default_port = 5582; default_mode = "*a"; interface = "127.0.0.1" }; |
23 | 22 |
24 local iterators = require "util.iterators"; | 23 local iterators = require "util.iterators"; |
25 local keys, values = iterators.keys, iterators.values; | 24 local keys, values = iterators.keys, iterators.values; |
58 w("| "..table.concat(t, "\t").."\n"); | 57 w("| "..table.concat(t, "\t").."\n"); |
59 end; | 58 end; |
60 disconnect = function () conn:close(); end; | 59 disconnect = function () conn:close(); end; |
61 }; | 60 }; |
62 session.env = setmetatable({}, default_env_mt); | 61 session.env = setmetatable({}, default_env_mt); |
63 | 62 |
64 -- Load up environment with helper objects | 63 -- Load up environment with helper objects |
65 for name, t in pairs(def_env) do | 64 for name, t in pairs(def_env) do |
66 if type(t) == "table" then | 65 if type(t) == "table" then |
67 session.env[name] = setmetatable({ session = session }, { __index = t }); | 66 session.env[name] = setmetatable({ session = session }, { __index = t }); |
68 end | 67 end |
69 end | 68 end |
70 | 69 |
71 return session; | 70 return session; |
72 end | 71 end |
73 | 72 |
74 function console:process_line(session, line) | 73 function console:process_line(session, line) |
75 local useglobalenv; | 74 local useglobalenv; |
76 | 75 |
77 if line:match("^>") then | 76 if line:match("^>") then |
78 line = line:gsub("^>", ""); | 77 line = line:gsub("^>", ""); |
79 useglobalenv = true; | 78 useglobalenv = true; |
80 elseif line == "\004" then | 79 elseif line == "\004" then |
81 commands["bye"](session, line); | 80 commands["bye"](session, line); |
85 if commands[command] then | 84 if commands[command] then |
86 commands[command](session, line); | 85 commands[command](session, line); |
87 return; | 86 return; |
88 end | 87 end |
89 end | 88 end |
90 | 89 |
91 session.env._ = line; | 90 session.env._ = line; |
92 | 91 |
93 local chunkname = "=console"; | 92 local chunkname = "=console"; |
94 local env = (useglobalenv and redirect_output(_G, session)) or session.env or nil | 93 local env = (useglobalenv and redirect_output(_G, session)) or session.env or nil |
95 local chunk, err = envload("return "..line, chunkname, env); | 94 local chunk, err = envload("return "..line, chunkname, env); |
96 if not chunk then | 95 if not chunk then |
97 chunk, err = envload(line, chunkname, env); | 96 chunk, err = envload(line, chunkname, env); |
101 err = err:gsub("'<eof>'", "the end of the line"); | 100 err = err:gsub("'<eof>'", "the end of the line"); |
102 session.print("Sorry, I couldn't understand that... "..err); | 101 session.print("Sorry, I couldn't understand that... "..err); |
103 return; | 102 return; |
104 end | 103 end |
105 end | 104 end |
106 | 105 |
107 local ranok, taskok, message = pcall(chunk); | 106 local ranok, taskok, message = pcall(chunk); |
108 | 107 |
109 if not (ranok or message or useglobalenv) and commands[line:lower()] then | 108 if not (ranok or message or useglobalenv) and commands[line:lower()] then |
110 commands[line:lower()](session, line); | 109 commands[line:lower()](session, line); |
111 return; | 110 return; |
112 end | 111 end |
113 | 112 |
114 if not ranok then | 113 if not ranok then |
115 session.print("Fatal error while running command, it did not complete"); | 114 session.print("Fatal error while running command, it did not complete"); |
116 session.print("Error: "..taskok); | 115 session.print("Error: "..taskok); |
117 return; | 116 return; |
118 end | 117 end |
119 | 118 |
120 if not message then | 119 if not message then |
121 session.print("Result: "..tostring(taskok)); | 120 session.print("Result: "..tostring(taskok)); |
122 return; | 121 return; |
123 elseif (not taskok) and message then | 122 elseif (not taskok) and message then |
124 session.print("Command completed with a problem"); | 123 session.print("Command completed with a problem"); |
125 session.print("Message: "..tostring(message)); | 124 session.print("Message: "..tostring(message)); |
126 return; | 125 return; |
127 end | 126 end |
128 | 127 |
129 session.print("OK: "..tostring(message)); | 128 session.print("OK: "..tostring(message)); |
130 end | 129 end |
131 | 130 |
132 local sessions = {}; | 131 local sessions = {}; |
133 | 132 |
342 end | 341 end |
343 end | 342 end |
344 | 343 |
345 function def_env.module:load(name, hosts, config) | 344 function def_env.module:load(name, hosts, config) |
346 local mm = require "modulemanager"; | 345 local mm = require "modulemanager"; |
347 | 346 |
348 hosts = get_hosts_set(hosts); | 347 hosts = get_hosts_set(hosts); |
349 | 348 |
350 -- Load the module for each host | 349 -- Load the module for each host |
351 local ok, err, count, mod = true, nil, 0, nil; | 350 local ok, err, count, mod = true, nil, 0, nil; |
352 for host in hosts do | 351 for host in hosts do |
353 if (not mm.is_loaded(host, name)) then | 352 if (not mm.is_loaded(host, name)) then |
354 mod, err = mm.load(host, name, config); | 353 mod, err = mm.load(host, name, config); |
365 count = count + 1; | 364 count = count + 1; |
366 self.session.print("Loaded for "..mod.module.host); | 365 self.session.print("Loaded for "..mod.module.host); |
367 end | 366 end |
368 end | 367 end |
369 end | 368 end |
370 | 369 |
371 return ok, (ok and "Module loaded onto "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err)); | 370 return ok, (ok and "Module loaded onto "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err)); |
372 end | 371 end |
373 | 372 |
374 function def_env.module:unload(name, hosts) | 373 function def_env.module:unload(name, hosts) |
375 local mm = require "modulemanager"; | 374 local mm = require "modulemanager"; |
376 | 375 |
377 hosts = get_hosts_set(hosts, name); | 376 hosts = get_hosts_set(hosts, name); |
378 | 377 |
379 -- Unload the module for each host | 378 -- Unload the module for each host |
380 local ok, err, count = true, nil, 0; | 379 local ok, err, count = true, nil, 0; |
381 for host in hosts do | 380 for host in hosts do |
382 if mm.is_loaded(host, name) then | 381 if mm.is_loaded(host, name) then |
383 ok, err = mm.unload(host, name); | 382 ok, err = mm.unload(host, name); |
431 hosts = { hosts }; | 430 hosts = { hosts }; |
432 end | 431 end |
433 if type(hosts) ~= "table" then | 432 if type(hosts) ~= "table" then |
434 return false, "Please supply a host or a list of hosts you would like to see"; | 433 return false, "Please supply a host or a list of hosts you would like to see"; |
435 end | 434 end |
436 | 435 |
437 local print = self.session.print; | 436 local print = self.session.print; |
438 for _, host in ipairs(hosts) do | 437 for _, host in ipairs(hosts) do |
439 print((host == "*" and "Global" or host)..":"); | 438 print((host == "*" and "Global" or host)..":"); |
440 local modules = array.collect(keys(modulemanager.get_modules(host) or {})):sort(); | 439 local modules = array.collect(keys(modulemanager.get_modules(host) or {})):sort(); |
441 if #modules == 0 then | 440 if #modules == 0 then |
481 end | 480 end |
482 | 481 |
483 function def_env.hosts:add(name) | 482 function def_env.hosts:add(name) |
484 end | 483 end |
485 | 484 |
485 local function session_flags(session, line) | |
486 line = line or {}; | |
487 if session.cert_identity_status == "valid" then | |
488 line[#line+1] = "(secure)"; | |
489 elseif session.secure then | |
490 line[#line+1] = "(encrypted)"; | |
491 end | |
492 if session.compressed then | |
493 line[#line+1] = "(compressed)"; | |
494 end | |
495 if session.smacks then | |
496 line[#line+1] = "(sm)"; | |
497 end | |
498 if session.ip and session.ip:match(":") then | |
499 line[#line+1] = "(IPv6)"; | |
500 end | |
501 return table.concat(line, " "); | |
502 end | |
503 | |
486 def_env.c2s = {}; | 504 def_env.c2s = {}; |
487 | 505 |
488 local function show_c2s(callback) | 506 local function show_c2s(callback) |
489 for hostname, host in pairs(hosts) do | 507 for hostname, host in pairs(hosts) do |
490 for username, user in pairs(host.sessions or {}) do | 508 for username, user in pairs(host.sessions or {}) do |
499 function def_env.c2s:count(match_jid) | 517 function def_env.c2s:count(match_jid) |
500 local count = 0; | 518 local count = 0; |
501 show_c2s(function (jid, session) | 519 show_c2s(function (jid, session) |
502 if (not match_jid) or jid:match(match_jid) then | 520 if (not match_jid) or jid:match(match_jid) then |
503 count = count + 1; | 521 count = count + 1; |
504 end | 522 end |
505 end); | 523 end); |
506 return true, "Total: "..count.." clients"; | 524 return true, "Total: "..count.." clients"; |
507 end | 525 end |
508 | 526 |
509 function def_env.c2s:show(match_jid) | 527 function def_env.c2s:show(match_jid) |
516 end | 534 end |
517 if (not match_jid) or jid:match(match_jid) then | 535 if (not match_jid) or jid:match(match_jid) then |
518 count = count + 1; | 536 count = count + 1; |
519 local status, priority = "unavailable", tostring(session.priority or "-"); | 537 local status, priority = "unavailable", tostring(session.priority or "-"); |
520 if session.presence then | 538 if session.presence then |
521 status = session.presence:child_with_name("show"); | 539 status = session.presence:get_child_text("show") or "available"; |
522 if status then | 540 end |
523 status = status:get_text() or "[invalid!]"; | 541 print(session_flags(session, { " "..jid.." - "..status.."("..priority..")" })); |
524 else | 542 end |
525 status = "available"; | |
526 end | |
527 end | |
528 print(" "..jid.." - "..status.."("..priority..")"); | |
529 end | |
530 end); | 543 end); |
531 return true, "Total: "..count.." clients"; | 544 return true, "Total: "..count.." clients"; |
532 end | 545 end |
533 | 546 |
534 function def_env.c2s:show_insecure(match_jid) | 547 function def_env.c2s:show_insecure(match_jid) |
535 local print, count = self.session.print, 0; | 548 local print, count = self.session.print, 0; |
536 show_c2s(function (jid, session) | 549 show_c2s(function (jid, session) |
537 if ((not match_jid) or jid:match(match_jid)) and not session.secure then | 550 if ((not match_jid) or jid:match(match_jid)) and not session.secure then |
538 count = count + 1; | 551 count = count + 1; |
539 print(jid); | 552 print(jid); |
540 end | 553 end |
541 end); | 554 end); |
542 return true, "Total: "..count.." insecure client connections"; | 555 return true, "Total: "..count.." insecure client connections"; |
543 end | 556 end |
544 | 557 |
545 function def_env.c2s:show_secure(match_jid) | 558 function def_env.c2s:show_secure(match_jid) |
546 local print, count = self.session.print, 0; | 559 local print, count = self.session.print, 0; |
547 show_c2s(function (jid, session) | 560 show_c2s(function (jid, session) |
548 if ((not match_jid) or jid:match(match_jid)) and session.secure then | 561 if ((not match_jid) or jid:match(match_jid)) and session.secure then |
549 count = count + 1; | 562 count = count + 1; |
550 print(jid); | 563 print(jid); |
551 end | 564 end |
552 end); | 565 end); |
553 return true, "Total: "..count.." secure client connections"; | 566 return true, "Total: "..count.." secure client connections"; |
554 end | 567 end |
555 | 568 |
556 function def_env.c2s:close(match_jid) | 569 function def_env.c2s:close(match_jid) |
562 end | 575 end |
563 end); | 576 end); |
564 return true, "Total: "..count.." sessions closed"; | 577 return true, "Total: "..count.." sessions closed"; |
565 end | 578 end |
566 | 579 |
567 local function session_flags(session, line) | |
568 if session.cert_identity_status == "valid" then | |
569 line[#line+1] = "(secure)"; | |
570 elseif session.secure then | |
571 line[#line+1] = "(encrypted)"; | |
572 end | |
573 if session.compressed then | |
574 line[#line+1] = "(compressed)"; | |
575 end | |
576 if session.smacks then | |
577 line[#line+1] = "(sm)"; | |
578 end | |
579 if session.conn and session.conn:ip():match(":") then | |
580 line[#line+1] = "(IPv6)"; | |
581 end | |
582 return table.concat(line, " "); | |
583 end | |
584 | 580 |
585 def_env.s2s = {}; | 581 def_env.s2s = {}; |
586 function def_env.s2s:show(match_jid) | 582 function def_env.s2s:show(match_jid) |
587 local _print = self.session.print; | |
588 local print = self.session.print; | 583 local print = self.session.print; |
589 | 584 |
590 local count_in, count_out = 0,0; | 585 local count_in, count_out = 0,0; |
591 | 586 local s2s_list = { }; |
592 for host, host_session in pairs(hosts) do | 587 |
593 print = function (...) _print(host); _print(...); print = _print; end | 588 local s2s_sessions = module:shared"/*/s2s/sessions"; |
594 for remotehost, session in pairs(host_session.s2sout) do | 589 for _, session in pairs(s2s_sessions) do |
595 if (not match_jid) or remotehost:match(match_jid) or host:match(match_jid) then | 590 local remotehost, localhost, direction; |
596 count_out = count_out + 1; | 591 if session.direction == "outgoing" then |
597 print(session_flags(session, {" ", host, "->", remotehost})); | 592 direction = "->"; |
598 if session.sendq then | 593 count_out = count_out + 1; |
599 print(" There are "..#session.sendq.." queued outgoing stanzas for this connection"); | 594 remotehost, localhost = session.to_host or "?", session.from_host or "?"; |
595 else | |
596 direction = "<-"; | |
597 count_in = count_in + 1; | |
598 remotehost, localhost = session.from_host or "?", session.to_host or "?"; | |
599 end | |
600 local sess_lines = { l = localhost, r = remotehost, | |
601 session_flags(session, { "", direction, remotehost or "?", | |
602 "["..session.type..tostring(session):match("[a-f0-9]*$").."]" })}; | |
603 | |
604 if (not match_jid) or remotehost:match(match_jid) or localhost:match(match_jid) then | |
605 table.insert(s2s_list, sess_lines); | |
606 local print = function (s) table.insert(sess_lines, " "..s); end | |
607 if session.sendq then | |
608 print("There are "..#session.sendq.." queued outgoing stanzas for this connection"); | |
609 end | |
610 if session.type == "s2sout_unauthed" then | |
611 if session.connecting then | |
612 print("Connection not yet established"); | |
613 if not session.srv_hosts then | |
614 if not session.conn then | |
615 print("We do not yet have a DNS answer for this host's SRV records"); | |
616 else | |
617 print("This host has no SRV records, using A record instead"); | |
618 end | |
619 elseif session.srv_choice then | |
620 print("We are on SRV record "..session.srv_choice.." of "..#session.srv_hosts); | |
621 local srv_choice = session.srv_hosts[session.srv_choice]; | |
622 print("Using "..(srv_choice.target or ".")..":"..(srv_choice.port or 5269)); | |
623 end | |
624 elseif session.notopen then | |
625 print("The <stream> has not yet been opened"); | |
626 elseif not session.dialback_key then | |
627 print("Dialback has not been initiated yet"); | |
628 elseif session.dialback_key then | |
629 print("Dialback has been requested, but no result received"); | |
600 end | 630 end |
601 if session.type == "s2sout_unauthed" then | 631 end |
602 if session.connecting then | 632 if session.type == "s2sin_unauthed" then |
603 print(" Connection not yet established"); | 633 print("Connection not yet authenticated"); |
604 if not session.srv_hosts then | 634 elseif session.type == "s2sin" then |
605 if not session.conn then | 635 for name in pairs(session.hosts) do |
606 print(" We do not yet have a DNS answer for this host's SRV records"); | 636 if name ~= session.from_host then |
607 else | 637 print("also hosts "..tostring(name)); |
608 print(" This host has no SRV records, using A record instead"); | |
609 end | |
610 elseif session.srv_choice then | |
611 print(" We are on SRV record "..session.srv_choice.." of "..#session.srv_hosts); | |
612 local srv_choice = session.srv_hosts[session.srv_choice]; | |
613 print(" Using "..(srv_choice.target or ".")..":"..(srv_choice.port or 5269)); | |
614 end | |
615 elseif session.notopen then | |
616 print(" The <stream> has not yet been opened"); | |
617 elseif not session.dialback_key then | |
618 print(" Dialback has not been initiated yet"); | |
619 elseif session.dialback_key then | |
620 print(" Dialback has been requested, but no result received"); | |
621 end | 638 end |
622 end | 639 end |
623 end | 640 end |
624 end | 641 end |
625 local subhost_filter = function (h) | 642 end |
626 return (match_jid and h:match(match_jid)); | 643 |
627 end | 644 -- Sort by local host, then remote host |
628 for session in pairs(incoming_s2s) do | 645 table.sort(s2s_list, function(a,b) |
629 if session.to_host == host and ((not match_jid) or host:match(match_jid) | 646 if a.l == b.l then return a.r < b.r; end |
630 or (session.from_host and session.from_host:match(match_jid)) | 647 return a.l < b.l; |
631 -- Pft! is what I say to list comprehensions | 648 end); |
632 or (session.hosts and #array.collect(keys(session.hosts)):filter(subhost_filter)>0)) then | 649 local lasthost; |
633 count_in = count_in + 1; | 650 for _, sess_lines in ipairs(s2s_list) do |
634 print(session_flags(session, {" ", host, "<-", session.from_host or "(unknown)"})); | 651 if sess_lines.l ~= lasthost then print(sess_lines.l); lasthost=sess_lines.l end |
635 if session.type == "s2sin_unauthed" then | 652 for _, line in ipairs(sess_lines) do print(line); end |
636 print(" Connection not yet authenticated"); | 653 end |
637 end | |
638 for name in pairs(session.hosts) do | |
639 if name ~= session.from_host then | |
640 print(" also hosts "..tostring(name)); | |
641 end | |
642 end | |
643 end | |
644 end | |
645 | |
646 print = _print; | |
647 end | |
648 | |
649 for session in pairs(incoming_s2s) do | |
650 if not session.to_host and ((not match_jid) or session.from_host and session.from_host:match(match_jid)) then | |
651 count_in = count_in + 1; | |
652 print("Other incoming s2s connections"); | |
653 print(" (unknown) <- "..(session.from_host or "(unknown)")); | |
654 end | |
655 end | |
656 | |
657 return true, "Total: "..count_out.." outgoing, "..count_in.." incoming connections"; | 654 return true, "Total: "..count_out.." outgoing, "..count_in.." incoming connections"; |
658 end | 655 end |
659 | 656 |
660 local function print_subject(print, subject) | 657 local function print_subject(print, subject) |
661 for _, entry in ipairs(subject) do | 658 for _, entry in ipairs(subject) do |
683 end | 680 end |
684 | 681 |
685 function def_env.s2s:showcert(domain) | 682 function def_env.s2s:showcert(domain) |
686 local ser = require "util.serialization".serialize; | 683 local ser = require "util.serialization".serialize; |
687 local print = self.session.print; | 684 local print = self.session.print; |
688 local domain_sessions = set.new(array.collect(keys(incoming_s2s))) | 685 local s2s_sessions = module:shared"/*/s2s/sessions"; |
689 /function(session) return session.from_host == domain and session or nil; end; | 686 local domain_sessions = set.new(array.collect(values(s2s_sessions))) |
690 for local_host in values(prosody.hosts) do | 687 /function(session) return (session.to_host == domain or session.from_host == domain) and session or nil; end; |
691 local s2sout = local_host.s2sout; | |
692 if s2sout and s2sout[domain] then | |
693 domain_sessions:add(s2sout[domain]); | |
694 end | |
695 end | |
696 local cert_set = {}; | 688 local cert_set = {}; |
697 for session in domain_sessions do | 689 for session in domain_sessions do |
698 local conn = session.conn; | 690 local conn = session.conn; |
699 conn = conn and conn:socket(); | 691 conn = conn and conn:socket(); |
700 if not conn.getpeerchain then | 692 if not conn.getpeerchain then |
729 end | 721 end |
730 end | 722 end |
731 local domain_certs = array.collect(values(cert_set)); | 723 local domain_certs = array.collect(values(cert_set)); |
732 -- Phew. We now have a array of unique certificates presented by domain. | 724 -- Phew. We now have a array of unique certificates presented by domain. |
733 local n_certs = #domain_certs; | 725 local n_certs = #domain_certs; |
734 | 726 |
735 if n_certs == 0 then | 727 if n_certs == 0 then |
736 return "No certificates found for "..domain; | 728 return "No certificates found for "..domain; |
737 end | 729 end |
738 | 730 |
739 local function _capitalize_and_colon(byte) | 731 local function _capitalize_and_colon(byte) |
740 return string.upper(byte)..":"; | 732 return string.upper(byte)..":"; |
741 end | 733 end |
742 local function pretty_fingerprint(hash) | 734 local function pretty_fingerprint(hash) |
743 return hash:gsub("..", _capitalize_and_colon):sub(1, -2); | 735 return hash:gsub("..", _capitalize_and_colon):sub(1, -2); |
744 end | 736 end |
745 | 737 |
746 for cert_info in values(domain_certs) do | 738 for cert_info in values(domain_certs) do |
747 local certs = cert_info.certs; | 739 local certs = cert_info.certs; |
748 local cert = certs[1]; | 740 local cert = certs[1]; |
749 print("---") | 741 print("---") |
750 print("Fingerprint (SHA1): "..pretty_fingerprint(cert:digest("sha1"))); | 742 print("Fingerprint (SHA1): "..pretty_fingerprint(cert:digest("sha1"))); |
781 .." presented by "..domain.."."); | 773 .." presented by "..domain.."."); |
782 end | 774 end |
783 | 775 |
784 function def_env.s2s:close(from, to) | 776 function def_env.s2s:close(from, to) |
785 local print, count = self.session.print, 0; | 777 local print, count = self.session.print, 0; |
786 | 778 local s2s_sessions = module:shared"/*/s2s/sessions"; |
787 if not (from and to) then | 779 |
780 local match_id; | |
781 if from and not to then | |
782 match_id, from = from; | |
783 elseif not to then | |
788 return false, "Syntax: s2s:close('from', 'to') - Closes all s2s sessions from 'from' to 'to'"; | 784 return false, "Syntax: s2s:close('from', 'to') - Closes all s2s sessions from 'from' to 'to'"; |
789 elseif from == to then | 785 elseif from == to then |
790 return false, "Both from and to are the same... you can't do that :)"; | 786 return false, "Both from and to are the same... you can't do that :)"; |
791 end | 787 end |
792 | 788 |
793 if hosts[from] and not hosts[to] then | 789 for _, session in pairs(s2s_sessions) do |
794 -- Is an outgoing connection | 790 local id = session.type..tostring(session):match("[a-f0-9]+$"); |
795 local session = hosts[from].s2sout[to]; | 791 if (match_id and match_id == id) |
796 if not session then | 792 or (session.from_host == from and session.to_host == to) then |
797 print("No outgoing connection from "..from.." to "..to) | 793 print(("Closing connection from %s to %s [%s]"):format(session.from_host, session.to_host, id)); |
798 else | |
799 (session.close or s2smanager.destroy_session)(session); | 794 (session.close or s2smanager.destroy_session)(session); |
800 count = count + 1; | 795 count = count + 1 ; |
801 print("Closed outgoing session from "..from.." to "..to); | 796 end |
802 end | 797 end |
803 elseif hosts[to] and not hosts[from] then | |
804 -- Is an incoming connection | |
805 for session in pairs(incoming_s2s) do | |
806 if session.to_host == to and session.from_host == from then | |
807 (session.close or s2smanager.destroy_session)(session); | |
808 count = count + 1; | |
809 end | |
810 end | |
811 | |
812 if count == 0 then | |
813 print("No incoming connections from "..from.." to "..to); | |
814 else | |
815 print("Closed "..count.." incoming session"..((count == 1 and "") or "s").." from "..from.." to "..to); | |
816 end | |
817 elseif hosts[to] and hosts[from] then | |
818 return false, "Both of the hostnames you specified are local, there are no s2s sessions to close"; | |
819 else | |
820 return false, "Neither of the hostnames you specified are being used on this server"; | |
821 end | |
822 | |
823 return true, "Closed "..count.." s2s session"..((count == 1 and "") or "s"); | 798 return true, "Closed "..count.." s2s session"..((count == 1 and "") or "s"); |
824 end | 799 end |
825 | 800 |
826 function def_env.s2s:closeall(host) | 801 function def_env.s2s:closeall(host) |
827 local count = 0; | 802 local count = 0; |
828 | 803 local s2s_sessions = module:shared"/*/s2s/sessions"; |
829 if not host or type(host) ~= "string" then return false, "wrong syntax: please use s2s:closeall('hostname.tld')"; end | 804 for _,session in pairs(s2s_sessions) do |
830 if hosts[host] then | 805 if not host or session.from_host == host or session.to_host == host then |
831 for session in pairs(incoming_s2s) do | 806 session:close(); |
832 if session.to_host == host then | |
833 (session.close or s2smanager.destroy_session)(session); | |
834 count = count + 1; | 807 count = count + 1; |
835 end | 808 end |
836 end | 809 end |
837 for _, session in pairs(hosts[host].s2sout) do | |
838 (session.close or s2smanager.destroy_session)(session); | |
839 count = count + 1; | |
840 end | |
841 else | |
842 for session in pairs(incoming_s2s) do | |
843 if session.from_host == host then | |
844 (session.close or s2smanager.destroy_session)(session); | |
845 count = count + 1; | |
846 end | |
847 end | |
848 for _, h in pairs(hosts) do | |
849 if h.s2sout[host] then | |
850 (h.s2sout[host].close or s2smanager.destroy_session)(h.s2sout[host]); | |
851 count = count + 1; | |
852 end | |
853 end | |
854 end | |
855 | |
856 if count == 0 then return false, "No sessions to close."; | 810 if count == 0 then return false, "No sessions to close."; |
857 else return true, "Closed "..count.." s2s session"..((count == 1 and "") or "s"); end | 811 else return true, "Closed "..count.." s2s session"..((count == 1 and "") or "s"); end |
858 end | 812 end |
859 | 813 |
860 def_env.host = {}; def_env.hosts = def_env.host; | 814 def_env.host = {}; def_env.hosts = def_env.host; |
1074 | 1028 |
1075 function printbanner(session) | 1029 function printbanner(session) |
1076 local option = module:get_option("console_banner"); | 1030 local option = module:get_option("console_banner"); |
1077 if option == nil or option == "full" or option == "graphic" then | 1031 if option == nil or option == "full" or option == "graphic" then |
1078 session.print [[ | 1032 session.print [[ |
1079 ____ \ / _ | 1033 ____ \ / _ |
1080 | _ \ _ __ ___ ___ _-_ __| |_ _ | 1034 | _ \ _ __ ___ ___ _-_ __| |_ _ |
1081 | |_) | '__/ _ \/ __|/ _ \ / _` | | | | | 1035 | |_) | '__/ _ \/ __|/ _ \ / _` | | | | |
1082 | __/| | | (_) \__ \ |_| | (_| | |_| | | 1036 | __/| | | (_) \__ \ |_| | (_| | |_| | |
1083 |_| |_| \___/|___/\___/ \__,_|\__, | | 1037 |_| |_| \___/|___/\___/ \__,_|\__, | |
1084 A study in simplicity |___/ | 1038 A study in simplicity |___/ |
1085 | 1039 |
1086 ]] | 1040 ]] |
1087 end | 1041 end |
1088 if option == nil or option == "short" or option == "full" then | 1042 if option == nil or option == "short" or option == "full" then |
1089 session.print("Welcome to the Prosody administration console. For a list of commands, type: help"); | 1043 session.print("Welcome to the Prosody administration console. For a list of commands, type: help"); |