Creating new user with Smack on ejabberd throws XMPP Exception: forbidden(403)
Asked Answered
W

4

6

Hi I am working on ejabberd and I am quite new to this technology.

I am trying to add a user on my ejabberd server using this code:

  try {         
         conf.setSASLAuthenticationEnabled(true);               
         connection.connect();  
         Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
         Log.i("XMPPClient", "Connected to "
                                +connection.getHost());              
         createUser("tester","testerpass");   
    }   
    } catch (XMPPException e1) {
        Log.e("XMPPClient", e1.toString());
        xmppClient.setConnection(null);
    }

    public void createUser(String user, String pass) {      
            try {
                //Admin login
                connection.login(user, pass);
            } catch (XMPPException e) {
              e.printStackTrace();
            }    
Log.i("connection.isAuthenticated() : ",""+connection.isAuthenticated() );   
             if (connection.isAuthenticated()) {
              AccountManager manager = connection.getAccountManager();        
              try {  
                manager.createAccount(user, pass);
              } catch (XMPPException e) {
                Log.w("[create_user] Cannot create new user: XMPP Exception.", "0");
                e.printStackTrace();
              } catch (IllegalStateException e) {
                Log.w("[create_user] Cannot create new user: not logged in.", "0");
                e.printStackTrace();
              }
            }
          }

Its connecting to server and admin login is perfectly But during creating new account it gives forbidden 403 error that is :

06-15 20:01:40.092: I/XMPPClient(1300):Connected to 68.178.255.136
06-15 20:01:41.952: I/connection.isAuthenticated() :(1300): true
06-15 20:01:42.562: W/[create_user] Cannot create new user: XMPP Exception.(1300): 0
06-15 20:01:42.562: W/System.err(1300): forbidden(403)
06-15 20:01:42.562: W/System.err(1300):     at org.jivesoftware.smack.AccountManager.createAccount(AccountManager.java:246)

I would be very thankful if somebody can suggest a workarround for this .

Woodberry answered 15/6, 2013 at 16:0 Comment(1)
remove connection.login(user, pass); and it should work. I found it after 3 hours of searching around. connection.connect() should be called however. When you are creating a new user, you are not supposed to loginBaillie
L
5

Goto C:\Program Files (x86)\ejabberd-2.1.8\conf (in my case) folder & open ejabberd.cfg file using Notepad++ (it is easy to edit using it).

In the file do the following changes:

%% Put this in the section ACCESS RULES
{access, register_from, [{allow, admin}]}.

%% Change mod_register so it contains the new access rule:

{mod_register, [
          {access_from, register_from},
           ...
                    ] ...
Lionfish answered 15/6, 2013 at 16:28 Comment(3)
@kramk did you try my answer.Lionfish
Thanks, osama mohammed saikh i have done two thing 1. edit .cfg file as you wrote 2. HashMap<String, String> attr = new HashMap<String, String>(); attr.put("user", user); attr.put("password", pass); try { manager.createAccount(user, pass, attr);....... Now its working.Woodberry
Thank you a lot! You saved me after a couple of days in the jungle. Your post is the only source I found that explains the connection between the access rules and the mod register.Roanne
M
3

I want to update the answer to reflect the change in Asmack library version 4.0 onward. Connection.getAccountManager() is now AccountManager.getInstance(XMPPConnection)

AccountManager accountManager=AccountManager.getInstance(connection);
try {
    accountManager.createAccount("username", "password");
} catch (XMPPException e1) {
    Log.d(e1.getMessage(), e1);
}
Matriculate answered 17/9, 2014 at 13:3 Comment(0)
B
1

In my case, i need to edit the file EJABERD_HOME/conf/ejabberd.yml, on mod_register change parameters to:

ip_access : all

access_from : all

access: register

To allow users register from another host

Buttock answered 13/1, 2016 at 11:52 Comment(0)
S
0

For yaml configuration as follows.

access_rules:
  local:
    allow: all
  configure:
    allow: all
  trusted_network:
    allow: all
  register:
    allow: all
  .......

modules:
  mod_register:
    ip_access: trusted_network
    access: register
    access_from: all

Note: This is allow all configuration. Make strict configuration as required.

Snowshoe answered 4/2, 2020 at 4:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.