Software /
code /
prosody
Comparison
plugins/mod_admin_telnet.lua @ 5776:bd0ff8ae98a8
Remove all trailing whitespace
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Fri, 09 Aug 2013 17:48:21 +0200 |
parent | 5770:7722372aa087 |
child | 6017:ac0879a8190a |
comparison
equal
deleted
inserted
replaced
5775:a6c2b8933507 | 5776:bd0ff8ae98a8 |
---|---|
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(); |
58 w("| "..table.concat(t, "\t").."\n"); | 58 w("| "..table.concat(t, "\t").."\n"); |
59 end; | 59 end; |
60 disconnect = function () conn:close(); end; | 60 disconnect = function () conn:close(); end; |
61 }; | 61 }; |
62 session.env = setmetatable({}, default_env_mt); | 62 session.env = setmetatable({}, default_env_mt); |
63 | 63 |
64 -- Load up environment with helper objects | 64 -- Load up environment with helper objects |
65 for name, t in pairs(def_env) do | 65 for name, t in pairs(def_env) do |
66 if type(t) == "table" then | 66 if type(t) == "table" then |
67 session.env[name] = setmetatable({ session = session }, { __index = t }); | 67 session.env[name] = setmetatable({ session = session }, { __index = t }); |
68 end | 68 end |
69 end | 69 end |
70 | 70 |
71 return session; | 71 return session; |
72 end | 72 end |
73 | 73 |
74 function console:process_line(session, line) | 74 function console:process_line(session, line) |
75 local useglobalenv; | 75 local useglobalenv; |
76 | 76 |
77 if line:match("^>") then | 77 if line:match("^>") then |
78 line = line:gsub("^>", ""); | 78 line = line:gsub("^>", ""); |
79 useglobalenv = true; | 79 useglobalenv = true; |
80 elseif line == "\004" then | 80 elseif line == "\004" then |
81 commands["bye"](session, line); | 81 commands["bye"](session, line); |
85 if commands[command] then | 85 if commands[command] then |
86 commands[command](session, line); | 86 commands[command](session, line); |
87 return; | 87 return; |
88 end | 88 end |
89 end | 89 end |
90 | 90 |
91 session.env._ = line; | 91 session.env._ = line; |
92 | 92 |
93 local chunkname = "=console"; | 93 local chunkname = "=console"; |
94 local env = (useglobalenv and redirect_output(_G, session)) or session.env or nil | 94 local env = (useglobalenv and redirect_output(_G, session)) or session.env or nil |
95 local chunk, err = envload("return "..line, chunkname, env); | 95 local chunk, err = envload("return "..line, chunkname, env); |
96 if not chunk then | 96 if not chunk then |
97 chunk, err = envload(line, chunkname, env); | 97 chunk, err = envload(line, chunkname, env); |
101 err = err:gsub("'<eof>'", "the end of the line"); | 101 err = err:gsub("'<eof>'", "the end of the line"); |
102 session.print("Sorry, I couldn't understand that... "..err); | 102 session.print("Sorry, I couldn't understand that... "..err); |
103 return; | 103 return; |
104 end | 104 end |
105 end | 105 end |
106 | 106 |
107 local ranok, taskok, message = pcall(chunk); | 107 local ranok, taskok, message = pcall(chunk); |
108 | 108 |
109 if not (ranok or message or useglobalenv) and commands[line:lower()] then | 109 if not (ranok or message or useglobalenv) and commands[line:lower()] then |
110 commands[line:lower()](session, line); | 110 commands[line:lower()](session, line); |
111 return; | 111 return; |
112 end | 112 end |
113 | 113 |
114 if not ranok then | 114 if not ranok then |
115 session.print("Fatal error while running command, it did not complete"); | 115 session.print("Fatal error while running command, it did not complete"); |
116 session.print("Error: "..taskok); | 116 session.print("Error: "..taskok); |
117 return; | 117 return; |
118 end | 118 end |
119 | 119 |
120 if not message then | 120 if not message then |
121 session.print("Result: "..tostring(taskok)); | 121 session.print("Result: "..tostring(taskok)); |
122 return; | 122 return; |
123 elseif (not taskok) and message then | 123 elseif (not taskok) and message then |
124 session.print("Command completed with a problem"); | 124 session.print("Command completed with a problem"); |
125 session.print("Message: "..tostring(message)); | 125 session.print("Message: "..tostring(message)); |
126 return; | 126 return; |
127 end | 127 end |
128 | 128 |
129 session.print("OK: "..tostring(message)); | 129 session.print("OK: "..tostring(message)); |
130 end | 130 end |
131 | 131 |
132 local sessions = {}; | 132 local sessions = {}; |
133 | 133 |
342 end | 342 end |
343 end | 343 end |
344 | 344 |
345 function def_env.module:load(name, hosts, config) | 345 function def_env.module:load(name, hosts, config) |
346 local mm = require "modulemanager"; | 346 local mm = require "modulemanager"; |
347 | 347 |
348 hosts = get_hosts_set(hosts); | 348 hosts = get_hosts_set(hosts); |
349 | 349 |
350 -- Load the module for each host | 350 -- Load the module for each host |
351 local ok, err, count, mod = true, nil, 0, nil; | 351 local ok, err, count, mod = true, nil, 0, nil; |
352 for host in hosts do | 352 for host in hosts do |
353 if (not mm.is_loaded(host, name)) then | 353 if (not mm.is_loaded(host, name)) then |
354 mod, err = mm.load(host, name, config); | 354 mod, err = mm.load(host, name, config); |
365 count = count + 1; | 365 count = count + 1; |
366 self.session.print("Loaded for "..mod.module.host); | 366 self.session.print("Loaded for "..mod.module.host); |
367 end | 367 end |
368 end | 368 end |
369 end | 369 end |
370 | 370 |
371 return ok, (ok and "Module loaded onto "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err)); | 371 return ok, (ok and "Module loaded onto "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err)); |
372 end | 372 end |
373 | 373 |
374 function def_env.module:unload(name, hosts) | 374 function def_env.module:unload(name, hosts) |
375 local mm = require "modulemanager"; | 375 local mm = require "modulemanager"; |
376 | 376 |
377 hosts = get_hosts_set(hosts, name); | 377 hosts = get_hosts_set(hosts, name); |
378 | 378 |
379 -- Unload the module for each host | 379 -- Unload the module for each host |
380 local ok, err, count = true, nil, 0; | 380 local ok, err, count = true, nil, 0; |
381 for host in hosts do | 381 for host in hosts do |
382 if mm.is_loaded(host, name) then | 382 if mm.is_loaded(host, name) then |
383 ok, err = mm.unload(host, name); | 383 ok, err = mm.unload(host, name); |
431 hosts = { hosts }; | 431 hosts = { hosts }; |
432 end | 432 end |
433 if type(hosts) ~= "table" then | 433 if type(hosts) ~= "table" then |
434 return false, "Please supply a host or a list of hosts you would like to see"; | 434 return false, "Please supply a host or a list of hosts you would like to see"; |
435 end | 435 end |
436 | 436 |
437 local print = self.session.print; | 437 local print = self.session.print; |
438 for _, host in ipairs(hosts) do | 438 for _, host in ipairs(hosts) do |
439 print((host == "*" and "Global" or host)..":"); | 439 print((host == "*" and "Global" or host)..":"); |
440 local modules = array.collect(keys(modulemanager.get_modules(host) or {})):sort(); | 440 local modules = array.collect(keys(modulemanager.get_modules(host) or {})):sort(); |
441 if #modules == 0 then | 441 if #modules == 0 then |
518 function def_env.c2s:count(match_jid) | 518 function def_env.c2s:count(match_jid) |
519 local count = 0; | 519 local count = 0; |
520 show_c2s(function (jid, session) | 520 show_c2s(function (jid, session) |
521 if (not match_jid) or jid:match(match_jid) then | 521 if (not match_jid) or jid:match(match_jid) then |
522 count = count + 1; | 522 count = count + 1; |
523 end | 523 end |
524 end); | 524 end); |
525 return true, "Total: "..count.." clients"; | 525 return true, "Total: "..count.." clients"; |
526 end | 526 end |
527 | 527 |
528 function def_env.c2s:show(match_jid) | 528 function def_env.c2s:show(match_jid) |
538 local status, priority = "unavailable", tostring(session.priority or "-"); | 538 local status, priority = "unavailable", tostring(session.priority or "-"); |
539 if session.presence then | 539 if session.presence then |
540 status = session.presence:get_child_text("show") or "available"; | 540 status = session.presence:get_child_text("show") or "available"; |
541 end | 541 end |
542 print(session_flags(session, { " "..jid.." - "..status.."("..priority..")" })); | 542 print(session_flags(session, { " "..jid.." - "..status.."("..priority..")" })); |
543 end | 543 end |
544 end); | 544 end); |
545 return true, "Total: "..count.." clients"; | 545 return true, "Total: "..count.." clients"; |
546 end | 546 end |
547 | 547 |
548 function def_env.c2s:show_insecure(match_jid) | 548 function def_env.c2s:show_insecure(match_jid) |
549 local print, count = self.session.print, 0; | 549 local print, count = self.session.print, 0; |
550 show_c2s(function (jid, session) | 550 show_c2s(function (jid, session) |
551 if ((not match_jid) or jid:match(match_jid)) and not session.secure then | 551 if ((not match_jid) or jid:match(match_jid)) and not session.secure then |
552 count = count + 1; | 552 count = count + 1; |
553 print(jid); | 553 print(jid); |
554 end | 554 end |
555 end); | 555 end); |
556 return true, "Total: "..count.." insecure client connections"; | 556 return true, "Total: "..count.." insecure client connections"; |
557 end | 557 end |
558 | 558 |
559 function def_env.c2s:show_secure(match_jid) | 559 function def_env.c2s:show_secure(match_jid) |
560 local print, count = self.session.print, 0; | 560 local print, count = self.session.print, 0; |
561 show_c2s(function (jid, session) | 561 show_c2s(function (jid, session) |
562 if ((not match_jid) or jid:match(match_jid)) and session.secure then | 562 if ((not match_jid) or jid:match(match_jid)) and session.secure then |
563 count = count + 1; | 563 count = count + 1; |
564 print(jid); | 564 print(jid); |
565 end | 565 end |
566 end); | 566 end); |
567 return true, "Total: "..count.." secure client connections"; | 567 return true, "Total: "..count.." secure client connections"; |
568 end | 568 end |
569 | 569 |
570 function def_env.c2s:close(match_jid) | 570 function def_env.c2s:close(match_jid) |
580 | 580 |
581 | 581 |
582 def_env.s2s = {}; | 582 def_env.s2s = {}; |
583 function def_env.s2s:show(match_jid) | 583 function def_env.s2s:show(match_jid) |
584 local print = self.session.print; | 584 local print = self.session.print; |
585 | 585 |
586 local count_in, count_out = 0,0; | 586 local count_in, count_out = 0,0; |
587 local s2s_list = { }; | 587 local s2s_list = { }; |
588 | 588 |
589 local s2s_sessions = module:shared"/*/s2s/sessions"; | 589 local s2s_sessions = module:shared"/*/s2s/sessions"; |
590 for _, session in pairs(s2s_sessions) do | 590 for _, session in pairs(s2s_sessions) do |
591 local remotehost, localhost, direction; | 591 local remotehost, localhost, direction; |
592 if session.direction == "outgoing" then | 592 if session.direction == "outgoing" then |
593 direction = "->"; | 593 direction = "->"; |
722 end | 722 end |
723 end | 723 end |
724 local domain_certs = array.collect(values(cert_set)); | 724 local domain_certs = array.collect(values(cert_set)); |
725 -- Phew. We now have a array of unique certificates presented by domain. | 725 -- Phew. We now have a array of unique certificates presented by domain. |
726 local n_certs = #domain_certs; | 726 local n_certs = #domain_certs; |
727 | 727 |
728 if n_certs == 0 then | 728 if n_certs == 0 then |
729 return "No certificates found for "..domain; | 729 return "No certificates found for "..domain; |
730 end | 730 end |
731 | 731 |
732 local function _capitalize_and_colon(byte) | 732 local function _capitalize_and_colon(byte) |
733 return string.upper(byte)..":"; | 733 return string.upper(byte)..":"; |
734 end | 734 end |
735 local function pretty_fingerprint(hash) | 735 local function pretty_fingerprint(hash) |
736 return hash:gsub("..", _capitalize_and_colon):sub(1, -2); | 736 return hash:gsub("..", _capitalize_and_colon):sub(1, -2); |
737 end | 737 end |
738 | 738 |
739 for cert_info in values(domain_certs) do | 739 for cert_info in values(domain_certs) do |
740 local certs = cert_info.certs; | 740 local certs = cert_info.certs; |
741 local cert = certs[1]; | 741 local cert = certs[1]; |
742 print("---") | 742 print("---") |
743 print("Fingerprint (SHA1): "..pretty_fingerprint(cert:digest("sha1"))); | 743 print("Fingerprint (SHA1): "..pretty_fingerprint(cert:digest("sha1"))); |
775 end | 775 end |
776 | 776 |
777 function def_env.s2s:close(from, to) | 777 function def_env.s2s:close(from, to) |
778 local print, count = self.session.print, 0; | 778 local print, count = self.session.print, 0; |
779 local s2s_sessions = module:shared"/*/s2s/sessions"; | 779 local s2s_sessions = module:shared"/*/s2s/sessions"; |
780 | 780 |
781 local match_id; | 781 local match_id; |
782 if from and not to then | 782 if from and not to then |
783 match_id, from = from; | 783 match_id, from = from; |
784 elseif not to then | 784 elseif not to then |
785 return false, "Syntax: s2s:close('from', 'to') - Closes all s2s sessions from 'from' to 'to'"; | 785 return false, "Syntax: s2s:close('from', 'to') - Closes all s2s sessions from 'from' to 'to'"; |
786 elseif from == to then | 786 elseif from == to then |
787 return false, "Both from and to are the same... you can't do that :)"; | 787 return false, "Both from and to are the same... you can't do that :)"; |
788 end | 788 end |
789 | 789 |
790 for _, session in pairs(s2s_sessions) do | 790 for _, session in pairs(s2s_sessions) do |
791 local id = session.type..tostring(session):match("[a-f0-9]+$"); | 791 local id = session.type..tostring(session):match("[a-f0-9]+$"); |
792 if (match_id and match_id == id) | 792 if (match_id and match_id == id) |
793 or (session.from_host == from and session.to_host == to) then | 793 or (session.from_host == from and session.to_host == to) then |
794 print(("Closing connection from %s to %s [%s]"):format(session.from_host, session.to_host, id)); | 794 print(("Closing connection from %s to %s [%s]"):format(session.from_host, session.to_host, id)); |
1029 | 1029 |
1030 function printbanner(session) | 1030 function printbanner(session) |
1031 local option = module:get_option("console_banner"); | 1031 local option = module:get_option("console_banner"); |
1032 if option == nil or option == "full" or option == "graphic" then | 1032 if option == nil or option == "full" or option == "graphic" then |
1033 session.print [[ | 1033 session.print [[ |
1034 ____ \ / _ | 1034 ____ \ / _ |
1035 | _ \ _ __ ___ ___ _-_ __| |_ _ | 1035 | _ \ _ __ ___ ___ _-_ __| |_ _ |
1036 | |_) | '__/ _ \/ __|/ _ \ / _` | | | | | 1036 | |_) | '__/ _ \/ __|/ _ \ / _` | | | | |
1037 | __/| | | (_) \__ \ |_| | (_| | |_| | | 1037 | __/| | | (_) \__ \ |_| | (_| | |_| | |
1038 |_| |_| \___/|___/\___/ \__,_|\__, | | 1038 |_| |_| \___/|___/\___/ \__,_|\__, | |
1039 A study in simplicity |___/ | 1039 A study in simplicity |___/ |
1040 | 1040 |
1041 ]] | 1041 ]] |
1042 end | 1042 end |
1043 if option == nil or option == "short" or option == "full" then | 1043 if option == nil or option == "short" or option == "full" then |
1044 session.print("Welcome to the Prosody administration console. For a list of commands, type: help"); | 1044 session.print("Welcome to the Prosody administration console. For a list of commands, type: help"); |