componentmanager: Don't error on stanzas to bare component JID
[prosody.git] / prosodyctl
index 098f7475eb905f9af002d20db28c54fb4c1342bc..91ed7432eae25f310398758d0a3d66161e2583e4 100755 (executable)
@@ -61,11 +61,38 @@ do
        end
 end
 
+local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
+require "util.datamanager".set_data_path(data_path);
+
+-- Switch away from root and into the prosody user --
+local switched_user, current_uid;
+local ok, pposix = pcall(require, "util.pposix");
+if ok and pposix then
+       current_uid = pposix.getuid();
+       if current_uid == 0 then
+               -- We haz root!
+               local desired_user = config.get("*", "core", "prosody_user") or "prosody";
+               local ok, err = pposix.setuid(desired_user);
+               if ok then
+                       -- Yay!
+                       switched_user = true;
+               else
+                       -- Boo!
+                       print("Warning: Couldn't switch to Prosody user '"..tostring(desired_user).."': "..tostring(err));
+               end
+       end
+else
+       print("Error: Unable to load pposix module. Check that Prosody is installed correctly.")
+       print("For more help send the below error to us through http://prosody.im/discuss");
+       print(tostring(pposix))
+end
+
 local error_messages = setmetatable({ 
                ["invalid-username"] = "The given username is invalid in a Jabber ID";
                ["invalid-hostname"] = "The given hostname is invalid";
                ["no-password"] = "No password was supplied";
                ["no-such-user"] = "The given user does not exist on the server";
+               ["unable-to-save-data"] = "Unable to store, perhaps you don't have permission?";
                }, { __index = function (t,k) return "Error: "..(tostring(k):gsub("%-", " "):gsub("^.", string.upper)); end });
 
 hosts = {};
@@ -291,6 +318,12 @@ function commands.status(arg)
                return 0;
        else
                show_message("Prosody is not running");
+               if not switched_user and current_uid ~= 0 then
+                       print("\nNote:")
+                       print(" You will also see this if prosodyctl is not running under");
+                       print(" the same user account as Prosody. Try running as root (e.g. ");
+                       print(" with 'sudo' in front) to gain access to Prosody's real status.");
+               end
                return 2
        end
        return 1;
@@ -319,10 +352,12 @@ end
 function commands.register(arg)
        local user, host, password = unpack(arg);
        if (not (user and host)) or arg[1] == "--help" then
-               if not user and user ~= "--help" then
-                       show_message [[No username specified]]
-               elseif not host then
-                       show_message [[Please specify which host you want to register the user on]];
+               if user ~= "--help" then
+                       if not user then
+                               show_message [[No username specified]]
+                       elseif not host then
+                               show_message [[Please specify which host you want to register the user on]];
+                       end
                end
                show_usage("register USER HOST [PASSWORD]", "Register a user on the server, with the given password");
                return 1;
@@ -346,12 +381,14 @@ end
 function commands.unregister(arg)
        local user, host = unpack(arg);
        if (not (user and host)) or arg[1] == "--help" then
-               if not user then
-                       show_message [[No username specified]]
-               elseif not host then
-                       show_message [[Please specify which host you want to unregister the user from]];
+               if user ~= "--help" then
+                       if not user then
+                               show_message [[No username specified]]
+                       elseif not host then
+                               show_message [[Please specify which host you want to unregister the user from]];
+                       end
                end
-               show_usage("register USER HOST [PASSWORD]", "Permanently remove a user account from the server");
+               show_usage("unregister USER HOST [PASSWORD]", "Permanently remove a user account from the server");
                return 1;
        end
 
@@ -378,6 +415,7 @@ if not commands[command] then -- Show help for all commands
        print("");
        print("Where COMMAND may be one of:\n");
 
+       local hidden_commands = require "util.set".new{ "register", "unregister" };
        local commands_order = { "adduser", "passwd", "deluser" };
 
        local done = {};
@@ -392,7 +430,7 @@ if not commands[command] then -- Show help for all commands
        end
 
        for command_name, command in pairs(commands) do
-               if not done[command_name] then
+               if not done[command_name] and not hidden_commands:contains(command_name) then
                        command{ "--help" };
                        print""
                        done[command_name] = true;