Software /
code /
prosody
Comparison
prosodyctl @ 4826:1c4852da78c8
prosodyctl: Replace hack with lfs for checking if a file exists
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 09 May 2012 01:02:00 +0200 |
parent | 4824:73e261ed00a9 |
child | 4827:fefbfd76d2d3 |
comparison
equal
deleted
inserted
replaced
4825:5fdc36bd866c | 4826:1c4852da78c8 |
---|---|
612 show_message(error_messages[msg]) | 612 show_message(error_messages[msg]) |
613 return 1; | 613 return 1; |
614 end | 614 end |
615 | 615 |
616 local openssl = require "util.openssl"; | 616 local openssl = require "util.openssl"; |
617 local lfs = require "lfs"; | |
617 | 618 |
618 local cert_commands = {}; | 619 local cert_commands = {}; |
620 | |
621 local function ask_overwrite(filename) | |
622 return lfs.attributes(filename) and not show_yesno("Overwrite "..filename .. "?"); | |
623 end | |
619 | 624 |
620 function cert_commands.config(arg) | 625 function cert_commands.config(arg) |
621 if #arg >= 1 and arg[1] ~= "--help" then | 626 if #arg >= 1 and arg[1] ~= "--help" then |
622 local conf_filename = (CFG_DATADIR or ".") .. "/" .. arg[1] .. ".cnf"; | 627 local conf_filename = (CFG_DATADIR or ".") .. "/" .. arg[1] .. ".cnf"; |
623 if os.execute("test -f "..conf_filename) == 0 | 628 if ask_overwrite(conf_filename) then |
624 and not show_yesno("Overwrite "..conf_filename .. "?") then | |
625 return nil, conf_filename; | 629 return nil, conf_filename; |
626 end | 630 end |
627 local conf = openssl.config.new(); | 631 local conf = openssl.config.new(); |
628 conf:from_prosody(hosts, config, arg); | 632 conf:from_prosody(hosts, config, arg); |
629 for k, v in pairs(conf.distinguished_name) do | 633 for k, v in pairs(conf.distinguished_name) do |
649 end | 653 end |
650 | 654 |
651 function cert_commands.key(arg) | 655 function cert_commands.key(arg) |
652 if #arg >= 1 and arg[1] ~= "--help" then | 656 if #arg >= 1 and arg[1] ~= "--help" then |
653 local key_filename = (CFG_DATADIR or ".") .. "/" .. arg[1] .. ".key"; | 657 local key_filename = (CFG_DATADIR or ".") .. "/" .. arg[1] .. ".key"; |
654 if os.execute("test -f "..key_filename) == 0 then | 658 if ask_overwrite(key_filename) then |
655 if not show_yesno("Overwrite "..key_filename .. "?") then | 659 return nil, key_filename; |
656 return nil, key_filename; | 660 end |
657 end | 661 os.remove(key_filename); -- We chmod this file to not have write permissions |
658 os.remove(key_filename); -- We chmod this file to not have write permissions | |
659 end | |
660 local key_size = tonumber(arg[2] or show_prompt("Choose key size (2048):") or 2048); | 662 local key_size = tonumber(arg[2] or show_prompt("Choose key size (2048):") or 2048); |
661 if openssl.genrsa{out=key_filename, key_size} then | 663 if openssl.genrsa{out=key_filename, key_size} then |
662 os.execute(("chmod 400 '%s'"):format(key_filename)); | 664 os.execute(("chmod 400 '%s'"):format(key_filename)); |
663 show_message("Key written to ".. key_filename); | 665 show_message("Key written to ".. key_filename); |
664 return nil, key_filename; | 666 return nil, key_filename; |
670 end | 672 end |
671 | 673 |
672 function cert_commands.request(arg) | 674 function cert_commands.request(arg) |
673 if #arg >= 1 and arg[1] ~= "--help" then | 675 if #arg >= 1 and arg[1] ~= "--help" then |
674 local req_filename = (CFG_DATADIR or ".") .. "/" .. arg[1] .. ".req"; | 676 local req_filename = (CFG_DATADIR or ".") .. "/" .. arg[1] .. ".req"; |
675 if os.execute("test -f "..req_filename) == 0 | 677 if ask_overwrite(req_filename) then |
676 and not show_yesno("Overwrite "..req_filename .. "?") then | |
677 return nil, req_filename; | 678 return nil, req_filename; |
678 end | 679 end |
679 local _, key_filename = cert_commands.key({arg[1]}); | 680 local _, key_filename = cert_commands.key({arg[1]}); |
680 local _, conf_filename = cert_commands.config({arg[1]}); | 681 local _, conf_filename = cert_commands.config({arg[1]}); |
681 if openssl.req{new=true, key=key_filename, utf8=true, config=conf_filename, out=req_filename} then | 682 if openssl.req{new=true, key=key_filename, utf8=true, config=conf_filename, out=req_filename} then |
689 end | 690 end |
690 | 691 |
691 function cert_commands.generate(arg) | 692 function cert_commands.generate(arg) |
692 if #arg >= 1 and arg[1] ~= "--help" then | 693 if #arg >= 1 and arg[1] ~= "--help" then |
693 local cert_filename = (CFG_DATADIR or ".") .. "/" .. arg[1] .. ".cert"; | 694 local cert_filename = (CFG_DATADIR or ".") .. "/" .. arg[1] .. ".cert"; |
694 if os.execute("test -f "..cert_filename) == 0 | 695 if ask_overwrite(cert_filename) then |
695 and not show_yesno("Overwrite "..cert_filename .. "?") then | 696 return nil, conf_filename; |
696 return nil, cert_filename; | |
697 end | 697 end |
698 local _, key_filename = cert_commands.key({arg[1]}); | 698 local _, key_filename = cert_commands.key({arg[1]}); |
699 local _, conf_filename = cert_commands.config({arg[1]}); | 699 local _, conf_filename = cert_commands.config({arg[1]}); |
700 local ret; | 700 local ret; |
701 if key_filename and conf_filename and cert_filename | 701 if key_filename and conf_filename and cert_filename |