Search in sources :

Example 1 with SharedGroupException

use of org.jivesoftware.openfire.SharedGroupException in project Openfire by igniterealtime.

the class UserServiceLegacy method userSerivceRequest.

@GET
@Path("/userservice")
public Response userSerivceRequest() throws IOException {
    // Printwriter for writing out responses to browser
    PrintWriter out = response.getWriter();
    if (!plugin.getAllowedIPs().isEmpty()) {
        // Get client's IP address
        String ipAddress = request.getHeader("x-forwarded-for");
        if (ipAddress == null) {
            ipAddress = request.getHeader("X_FORWARDED_FOR");
            if (ipAddress == null) {
                ipAddress = request.getHeader("X-Forward-For");
                if (ipAddress == null) {
                    ipAddress = request.getRemoteAddr();
                }
            }
        }
        if (!plugin.getAllowedIPs().contains(ipAddress)) {
            Log.warn("User service rejected service to IP address: " + ipAddress);
            replyError("RequestNotAuthorised", response, out);
            return Response.status(200).build();
        }
    }
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String name = request.getParameter("name");
    String email = request.getParameter("email");
    String type = request.getParameter("type");
    String secret = request.getParameter("secret");
    String groupNames = request.getParameter("groups");
    String item_jid = request.getParameter("item_jid");
    String sub = request.getParameter("subscription");
    // Check that our plugin is enabled.
    if (!plugin.isEnabled()) {
        Log.warn("User service plugin is disabled: " + request.getQueryString());
        replyError("UserServiceDisabled", response, out);
        return Response.status(200).build();
    }
    // Check this request is authorised
    if (secret == null || !secret.equals(plugin.getSecret())) {
        Log.warn("An unauthorised user service request was received: " + request.getQueryString());
        replyError("RequestNotAuthorised", response, out);
        return Response.status(200).build();
    }
    // Some checking is required on the username
    if (username == null && !"grouplist".equals(type)) {
        replyError("IllegalArgumentException", response, out);
        return Response.status(200).build();
    }
    if ((type.equals("add_roster") || type.equals("update_roster") || type.equals("delete_roster")) && (item_jid == null || !(sub == null || sub.equals("-1") || sub.equals("0") || sub.equals("1") || sub.equals("2") || sub.equals("3")))) {
        replyError("IllegalArgumentException", response, out);
        return Response.status(200).build();
    }
    // Check the request type and process accordingly
    try {
        if ("grouplist".equals(type)) {
            String message = "";
            for (String groupname : userServiceController.getAllGroups()) {
                message += "<groupname>" + groupname + "</groupname>";
            }
            replyMessage(message, response, out);
        } else {
            username = username.trim().toLowerCase();
            username = JID.escapeNode(username);
            username = Stringprep.nodeprep(username);
            if ("add".equals(type)) {
                userServiceController.createUser(username, password, name, email, groupNames);
                replyMessage("ok", response, out);
            } else if ("delete".equals(type)) {
                userServiceController.deleteUser(username);
                replyMessage("ok", response, out);
            } else if ("enable".equals(type)) {
                userServiceController.enableUser(username);
                replyMessage("ok", response, out);
            } else if ("disable".equals(type)) {
                userServiceController.disableUser(username);
                replyMessage("ok", response, out);
            } else if ("update".equals(type)) {
                userServiceController.updateUser(username, password, name, email, groupNames);
                replyMessage("ok", response, out);
            } else if ("add_roster".equals(type)) {
                userServiceController.addRosterItem(username, item_jid, name, sub, groupNames);
                replyMessage("ok", response, out);
            } else if ("update_roster".equals(type)) {
                userServiceController.updateRosterItem(username, item_jid, name, sub, groupNames);
                replyMessage("ok", response, out);
            } else if ("delete_roster".equals(type)) {
                userServiceController.deleteRosterItem(username, item_jid);
                replyMessage("ok", response, out);
            } else if ("usergrouplist".equals(type)) {
                String message = "";
                for (String groupname : userServiceController.getUserGroups(username)) {
                    message += "<groupname>" + groupname + "</groupname>";
                }
                replyMessage(message, response, out);
            } else {
                Log.warn("The userService servlet received an invalid request of type: " + type);
            // TODO Do something
            }
        }
    } catch (UserAlreadyExistsException e) {
        replyError("UserAlreadyExistsException", response, out);
    } catch (UserNotFoundException e) {
        replyError("UserNotFoundException", response, out);
    } catch (IllegalArgumentException e) {
        replyError("IllegalArgumentException", response, out);
    } catch (SharedGroupException e) {
        replyError("SharedGroupException", response, out);
    } catch (Exception e) {
        Log.error("Error: ", e);
        replyError(e.toString(), response, out);
    }
    return Response.status(200).build();
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) IOException(java.io.IOException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException) PrintWriter(java.io.PrintWriter) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with SharedGroupException

use of org.jivesoftware.openfire.SharedGroupException in project Openfire by igniterealtime.

the class IQRosterPayloadProcessor method deleteSubdomainItemsFromRoster.

/**
	 * Searches the users roster for a specific subdomain and deletes all contacts that contain subdomain
	 * 
	 * @param username
	 * @param subdomain
	 */
private void deleteSubdomainItemsFromRoster(String username, String subdomain) {
    try {
        Roster roster = _rosterManager.getRoster(username);
        Collection<RosterItem> items = roster.getRosterItems();
        for (RosterItem item : items) {
            String itemName = item.getJid().toString();
            if (itemName.contains(subdomain)) {
                Log.debug("Removing contact " + item.getJid().toString() + " from contact list because of Unregister.");
                roster.deleteRosterItem(item.getJid(), false);
            }
        }
    } catch (UserNotFoundException e) {
        Log.debug("Couldnt find User!" + e.toString());
    } catch (SharedGroupException e) {
        e.printStackTrace();
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) Roster(org.jivesoftware.openfire.roster.Roster) SharedGroupException(org.jivesoftware.openfire.SharedGroupException)

Example 3 with SharedGroupException

use of org.jivesoftware.openfire.SharedGroupException in project Openfire by igniterealtime.

the class UserServiceLegacy method userSerivceRequest.

@GET
@Path("/userservice")
public Response userSerivceRequest() throws IOException {
    // Printwriter for writing out responses to browser
    PrintWriter out = response.getWriter();
    if (!plugin.getAllowedIPs().isEmpty()) {
        // Get client's IP address
        String ipAddress = request.getHeader("x-forwarded-for");
        if (ipAddress == null) {
            ipAddress = request.getHeader("X_FORWARDED_FOR");
            if (ipAddress == null) {
                ipAddress = request.getHeader("X-Forward-For");
                if (ipAddress == null) {
                    ipAddress = request.getRemoteAddr();
                }
            }
        }
        if (!plugin.getAllowedIPs().contains(ipAddress)) {
            Log.warn("User service rejected service to IP address: " + ipAddress);
            replyError("RequestNotAuthorised", response, out);
            return Response.status(200).build();
        }
    }
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String name = request.getParameter("name");
    String email = request.getParameter("email");
    String type = request.getParameter("type");
    String secret = request.getParameter("secret");
    String groupNames = request.getParameter("groups");
    String item_jid = request.getParameter("item_jid");
    String sub = request.getParameter("subscription");
    // Check that our plugin is enabled.
    if (!plugin.isEnabled()) {
        Log.warn("User service plugin is disabled: " + request.getQueryString());
        replyError("UserServiceDisabled", response, out);
        return Response.status(200).build();
    }
    // Check this request is authorised
    if (secret == null || !secret.equals(plugin.getSecret())) {
        Log.warn("An unauthorised user service request was received: " + request.getQueryString());
        replyError("RequestNotAuthorised", response, out);
        return Response.status(200).build();
    }
    // Some checking is required on the username
    if (username == null && !"grouplist".equals(type)) {
        replyError("IllegalArgumentException", response, out);
        return Response.status(200).build();
    }
    if ((type.equals("add_roster") || type.equals("update_roster") || type.equals("delete_roster")) && (item_jid == null || !(sub == null || sub.equals("-1") || sub.equals("0") || sub.equals("1") || sub.equals("2") || sub.equals("3")))) {
        replyError("IllegalArgumentException", response, out);
        return Response.status(200).build();
    }
    // Check the request type and process accordingly
    try {
        if ("grouplist".equals(type)) {
            String message = "";
            for (String groupname : plugin.getAllGroups()) {
                message += "<groupname>" + groupname + "</groupname>";
            }
            replyMessage(message, response, out);
        } else {
            username = username.trim().toLowerCase();
            username = JID.escapeNode(username);
            username = Stringprep.nodeprep(username);
            if ("add".equals(type)) {
                plugin.createUser(username, password, name, email, groupNames);
                replyMessage("ok", response, out);
            } else if ("delete".equals(type)) {
                plugin.deleteUser(username);
                replyMessage("ok", response, out);
            } else if ("enable".equals(type)) {
                plugin.enableUser(username);
                replyMessage("ok", response, out);
            } else if ("disable".equals(type)) {
                plugin.disableUser(username);
                replyMessage("ok", response, out);
            } else if ("update".equals(type)) {
                plugin.updateUser(username, password, name, email, groupNames);
                replyMessage("ok", response, out);
            } else if ("add_roster".equals(type)) {
                plugin.addRosterItem(username, item_jid, name, sub, groupNames);
                replyMessage("ok", response, out);
            } else if ("update_roster".equals(type)) {
                plugin.updateRosterItem(username, item_jid, name, sub, groupNames);
                replyMessage("ok", response, out);
            } else if ("delete_roster".equals(type)) {
                plugin.deleteRosterItem(username, item_jid);
                replyMessage("ok", response, out);
            } else if ("usergrouplist".equals(type)) {
                String message = "";
                for (String groupname : plugin.getUserGroups(username)) {
                    message += "<groupname>" + groupname + "</groupname>";
                }
                replyMessage(message, response, out);
            } else {
                Log.warn("The userService servlet received an invalid request of type: " + type);
            // TODO Do something
            }
        }
    } catch (UserAlreadyExistsException e) {
        replyError("UserAlreadyExistsException", response, out);
    } catch (UserNotFoundException e) {
        replyError("UserNotFoundException", response, out);
    } catch (IllegalArgumentException e) {
        replyError("IllegalArgumentException", response, out);
    } catch (SharedGroupException e) {
        replyError("SharedGroupException", response, out);
    } catch (Exception e) {
        replyError(e.toString(), response, out);
    }
    return Response.status(200).build();
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) IOException(java.io.IOException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException) PrintWriter(java.io.PrintWriter) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 4 with SharedGroupException

use of org.jivesoftware.openfire.SharedGroupException in project Openfire by igniterealtime.

the class JustMarriedPlugin method addNewUserToOthersRoster.

private static void addNewUserToOthersRoster(User newUser, RosterItem otherItem, String currentUser) {
    otherItem.getJid();
    UserManager userManager = UserManager.getInstance();
    // Is this user registered with our OF server?
    String username = otherItem.getJid().getNode();
    if (username != null && username.length() > 0 && userManager.isRegisteredUser(username) && XMPPServer.getInstance().isLocal(XMPPServer.getInstance().createJID(currentUser, null))) {
        try {
            User otherUser = userManager.getUser(username);
            Roster otherRoster = otherUser.getRoster();
            RosterItem oldUserOnOthersRoster = otherRoster.getRosterItem(XMPPServer.getInstance().createJID(currentUser, null));
            try {
                if (!oldUserOnOthersRoster.isOnlyShared()) {
                    RosterItem justCreated = otherRoster.createRosterItem(XMPPServer.getInstance().createJID(newUser.getUsername(), null), oldUserOnOthersRoster.getNickname(), oldUserOnOthersRoster.getGroups(), true, true);
                    justCreated.setAskStatus(oldUserOnOthersRoster.getAskStatus());
                    justCreated.setRecvStatus(oldUserOnOthersRoster.getRecvStatus());
                    justCreated.setSubStatus(oldUserOnOthersRoster.getSubStatus());
                    otherRoster.updateRosterItem(justCreated);
                }
            } catch (UserAlreadyExistsException e) {
                Log.error("Could not create roster item for user " + newUser.getUsername(), e);
            } catch (SharedGroupException e) {
                Log.error(e);
            }
        } catch (UserNotFoundException e) {
            Log.error("Could not create roster item for user " + newUser.getUsername() + " because it is a contact from a shared group", e);
        }
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) User(org.jivesoftware.openfire.user.User) Roster(org.jivesoftware.openfire.roster.Roster) UserManager(org.jivesoftware.openfire.user.UserManager) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException)

Example 5 with SharedGroupException

use of org.jivesoftware.openfire.SharedGroupException in project Openfire by igniterealtime.

the class IQRosterHandler method handleIQ.

/**
 * Handles all roster queries. There are two major types of queries:
 *
 * <ul>
 *      <li>Roster remove - A forced removal of items from a roster. Roster
 *      removals are the only roster queries allowed to
 *      directly affect the roster from another user.
 *      </li>
 *      <li>Roster management - A local user looking up or updating their
 *      roster.
 *      </li>
 * </ul>
 *
 * @param packet The update packet
 * @return The reply or null if no reply
 */
@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException, PacketException {
    try {
        IQ returnPacket;
        org.xmpp.packet.Roster roster = (org.xmpp.packet.Roster) packet;
        JID recipientJID = packet.getTo();
        // The packet is bound for the server and must be roster management
        if (recipientJID == null || recipientJID.equals(packet.getFrom().asBareJID())) {
            returnPacket = manageRoster(roster);
        } else {
            returnPacket = IQ.createResultIQ(packet);
            // The server MUST return a <forbidden/> stanza error to the client if the sender of the roster set is not authorized to update the roster
            // (where typically only an authenticated resource of the account itself is authorized).
            returnPacket.setError(PacketError.Condition.forbidden);
        }
        return returnPacket;
    } catch (SharedGroupException e) {
        IQ result = IQ.createResultIQ(packet);
        result.setChildElement(packet.getChildElement().createCopy());
        result.setError(PacketError.Condition.not_acceptable);
        return result;
    } catch (UnauthorizedException e) {
        IQ result = IQ.createResultIQ(packet);
        result.setChildElement(packet.getChildElement().createCopy());
        result.setError(PacketError.Condition.not_authorized);
        return result;
    } catch (Exception e) {
        if (e.getCause() instanceof IDNAException || e.getCause() instanceof IllegalArgumentException) {
            Log.warn(LocaleUtils.getLocalizedString("admin.error") + e.getMessage());
            IQ result = IQ.createResultIQ(packet);
            result.setChildElement(packet.getChildElement().createCopy());
            result.setError(PacketError.Condition.jid_malformed);
            return result;
        } else {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
            IQ result = IQ.createResultIQ(packet);
            result.setChildElement(packet.getChildElement().createCopy());
            result.setError(PacketError.Condition.internal_server_error);
            return result;
        }
    }
}
Also used : IDNAException(gnu.inet.encoding.IDNAException) Roster(org.jivesoftware.openfire.roster.Roster) JID(org.xmpp.packet.JID) IQ(org.xmpp.packet.IQ) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException) PacketException(org.jivesoftware.openfire.PacketException) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) IDNAException(gnu.inet.encoding.IDNAException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Aggregations

SharedGroupException (org.jivesoftware.openfire.SharedGroupException)9 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)9 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)8 Roster (org.jivesoftware.openfire.roster.Roster)7 RosterItem (org.jivesoftware.openfire.roster.RosterItem)6 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 PacketException (org.jivesoftware.openfire.PacketException)2 Group (org.jivesoftware.openfire.group.Group)2 ServiceException (org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException)2 User (org.jivesoftware.openfire.user.User)2 UserManager (org.jivesoftware.openfire.user.UserManager)2 JID (org.xmpp.packet.JID)2 IDNAException (gnu.inet.encoding.IDNAException)1 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)1 IQ (org.xmpp.packet.IQ)1 Presence (org.xmpp.packet.Presence)1