コード例 #1
0
 private void prepareConfigData(Packet result, String comp_name) throws ConfigurationException {
   if (comp_name.equals("--none--")) {
     newComponentCommand(result);
     return;
   } // end of if (comp_name.equals("--none--"))
   Command.setStatus(result, Command.Status.executing);
   Command.addAction(result, Command.Action.complete);
   // Let's try to sort them to make it easier to find options on
   // configuration page.
   Map<String, Object> allprop = new TreeMap<String, Object>(getAllProperties(comp_name));
   for (Map.Entry<String, Object> entry : allprop.entrySet()) {
     Command.addFieldValue(
         result,
         XMLUtils.escape(entry.getKey()),
         XMLUtils.escape(objectToString(entry.getValue())));
   } // end of for (Map.Entry entry: prop.entrySet())
   Command.addFieldValue(
       result,
       XMLUtils.escape("new-prop-name"),
       XMLUtils.escape(comp_name + "/"),
       "text-single",
       "New property name");
   Command.addFieldValue(
       result, XMLUtils.escape("new-prop-value"), "", "text-single", "New property value");
 }
コード例 #2
0
 private boolean checkComponentName(Packet result, String name) {
   String msg = name;
   if (msg != null) {
     Command.addFieldValue(
         result, "Info", "Note!! " + msg + ", please provide valid component name.", "fixed");
     newComponentCommand(result);
     return false;
   } // end of if (new_comp_name == null || new_comp_name.length() == 0)
   String[] comp_names = getComponents();
   for (String comp_name : comp_names) {
     if (comp_name.equals(name)) {
       Command.addFieldValue(
           result, "Info", "Note!! Component with provided name already exists.", "fixed");
       Command.addFieldValue(result, "Info", "Please provide different component name.", "fixed");
       newComponentCommand(result);
       return false;
     } // end of if (comp_name.equals(new_comp_name))
   } // end of for (String comp_name: comp_names)
   return true;
 }
コード例 #3
0
 public Object setPropertyValue(String key, String val, Packet result_pack, boolean admin) {
   Object result = null;
   try {
     if (admin) {
       result = setValue(key, val, false, false, null);
     }
     if (result != null) {
       Command.addFieldValue(result_pack, XMLUtils.escape(key), XMLUtils.escape(val));
     } else {
       Command.addFieldValue(
           result_pack,
           "Note",
           "You can not set new properties yet, you can just modify existing ones.",
           "fixed");
     }
   } catch (Exception e) {
     Command.addFieldValue(result_pack, "Note", "Error setting property: " + e, "fixed");
   }
   return result;
 }
コード例 #4
0
    @Override
    public void responseReceived(Packet packet, Packet response) {
      String pb = Command.getFieldValue(packet, PRE_BIND_ATTR);
      boolean prebind = Boolean.valueOf(pb);

      String sessionId = Command.getFieldValue(packet, SESSION_ID_ATTR);
      String userID = Command.getFieldValue(packet, USER_ID_ATTR);

      if (prebind) {
        // we are doing pre-bind, send user-login command, bind resource
        Packet packetOut =
            Command.USER_STATUS.getPacket(
                packet.getFrom(), packet.getTo(), StanzaType.get, UUID.randomUUID().toString());

        //				Element presence = new Element( "presence" );
        Command.addFieldValue(packetOut, USER_ID_ATTR, userID);
        if (null != sessionId) {
          Command.addFieldValue(packetOut, SESSION_ID_ATTR, sessionId);
        }
        Command.addFieldValue(packetOut, PRE_BIND_ATTR, String.valueOf(prebind));

        addOutPacket(packetOut);
      } else {

        // We are now ready to ask for features....
        addOutPacket(
            Command.GETFEATURES.getPacket(
                packet.getFrom(),
                packet.getTo(),
                StanzaType.get,
                UUID.randomUUID().toString(),
                null));
      }
    }
コード例 #5
0
 private void updateConfigChanges(Packet packet, Packet result, String comp_name, boolean admin)
     throws ConfigurationException {
   if (comp_name.equals("--none--")) {
     newComponentCommand(packet, result, admin);
     return;
   } // end of if (comp_name.equals("--none--"))
   Command.addNote(result, "You changed following settings:");
   Command.addFieldValue(result, "Note", "You changed following settings:", "fixed");
   Map<String, Object> allprop = getAllProperties(comp_name);
   boolean changed = false;
   for (Map.Entry<String, Object> entry : allprop.entrySet()) {
     String tmp_val = Command.getFieldValue(packet, XMLUtils.escape(entry.getKey()));
     String old_val = objectToString(entry.getValue());
     String new_val = old_val;
     if (tmp_val != null) {
       new_val = XMLUtils.unescape(tmp_val);
     }
     if (new_val != null && old_val != null && !new_val.equals(old_val)) {
       defConfigParams.put(
           entry.getKey(), setPropertyValue(entry.getKey(), new_val, result, admin));
       changed = true;
     }
   } // end of for (Map.Entry entry: prop.entrySet())
   String prop_value = Command.getFieldValue(packet, "new-prop-value");
   if (prop_value != null && prop_value.trim().length() > 0) {
     setPropertyValue(
         XMLUtils.unescape(Command.getFieldValue(packet, "new-prop-name")),
         XMLUtils.unescape(prop_value),
         result,
         admin);
     changed = true;
   }
   if (changed && admin) {
     setup(comp_name);
   }
 }
コード例 #6
0
ファイル: StartTLS.java プロジェクト: hbaaron/ikan
  /**
   * Method description
   *
   * @param packet
   * @param session
   * @param repo
   * @param results
   * @param settings
   */
  @Override
  public void process(
      final Packet packet,
      final XMPPResourceConnection session,
      final NonAuthUserRepository repo,
      final Queue<Packet> results,
      final Map<String, Object> settings) {
    if (session == null) {
      return;
    } // end of if (session == null)
    if (packet.isElement("starttls", XMLNS)) {
      if (session.getSessionData(ID) != null) {

        // Somebody tries to activate multiple TLS layers.
        // This is possible and can even work but this can also be
        // a DOS attack. Blocking it now, unless someone requests he wants
        // to have multiple layers of TLS for his connection
        log.log(
            Level.WARNING,
            "Multiple TLS requests, possible DOS attack, closing connection: {0}",
            packet);
        results.offer(packet.swapFromTo(failure, null, null));
        results.offer(
            Command.CLOSE.getPacket(
                packet.getTo(), packet.getFrom(), StanzaType.set, session.nextStanzaId()));

        return;
      }
      session.putSessionData(ID, "true");

      Packet result =
          Command.STARTTLS.getPacket(
              packet.getTo(),
              packet.getFrom(),
              StanzaType.set,
              session.nextStanzaId(),
              Command.DataType.submit);

      Command.setData(result, proceed);
      results.offer(result);
    } else {
      log.log(Level.WARNING, "Unknown TLS element: {0}", packet);
      results.offer(packet.swapFromTo(failure, null, null));
      results.offer(
          Command.CLOSE.getPacket(
              packet.getTo(), packet.getFrom(), StanzaType.set, session.nextStanzaId()));
    } // end of if (packet.getElement().getName().equals("starttls")) else
  }
コード例 #7
0
 private void newComponentCommand(Packet result) {
   Command.addFieldValue(result, "Info", "Press:", "fixed");
   Command.addFieldValue(
       result, "Info", "'Next' to set all parameters for the new component.", "fixed");
   Command.setStatus(result, Command.Status.executing);
   Command.addAction(result, Command.Action.next);
   Command.addFieldValue(result, "Component name", "", "text-single", "Component name");
   try {
     Set<Class<MessageReceiver>> receiv_cls =
         ClassUtil.getClassesImplementing(MessageReceiver.class);
     // All message receivers except MessageRouter
     String[] receiv_cls_names = new String[receiv_cls.size() - 1];
     String[] receiv_cls_simple = new String[receiv_cls.size() - 1];
     int idx = 0;
     for (Class<MessageReceiver> reciv : receiv_cls) {
       if (!reciv.getName().equals(ROUTER_COMP_CLASS_NAME)) {
         receiv_cls_names[idx] = reciv.getName();
         receiv_cls_simple[idx++] = reciv.getSimpleName();
       } // end of if (!reciv.getName().equals(ROUTER_COMP_CLASS_NAME))
     } // end of for (MessageReceiver.class reciv: receiv_cls)
     Command.addFieldValue(
         result,
         "Component class",
         EXT_COMP_CLASS_NAME,
         "Component class",
         receiv_cls_simple,
         receiv_cls_names);
   } catch (Exception e) {
     log.log(Level.SEVERE, "Problem loading MessageReceiver implementations", e);
     Command.addFieldValue(
         result,
         "Component class",
         "ERROR!! Problem loading MessageReceiver implementations, "
             + "look in log file for details...",
         "text-single",
         "Component class");
   } // end of try-catch
 }
コード例 #8
0
 private void newComponentCommand(Packet packet, Packet result, boolean admin) {
   String params_set = Command.getFieldValue(packet, "Params set");
   if (Command.getAction(packet) != null && Command.getAction(packet).equals("prev")) {
     newComponentCommand(result);
     return;
   } // end of if ()
   if (params_set != null) {
     createNewComponent(packet, result, admin);
     return;
   } // end of if (params_set != null)
   String new_comp_name = Command.getFieldValue(packet, "Component name");
   String new_comp_class = Command.getFieldValue(packet, "Component class");
   if (!checkComponentName(result, new_comp_name)) {
     return;
   } // end of if (!checkComponentName(new_comp_name))
   Command.setStatus(result, Command.Status.executing);
   Command.addFieldValue(result, "Component name", new_comp_name, "hidden");
   Command.addFieldValue(result, "Component class", new_comp_class, "hidden");
   Command.addFieldValue(result, "Info1", "Press:", "fixed");
   try {
     MessageReceiver mr = (MessageReceiver) Class.forName(new_comp_class).newInstance();
     Command.addFieldValue(
         result,
         "Info4",
         "Component name: " + new_comp_name + ", class: " + mr.getClass().getSimpleName(),
         "fixed");
     if (mr instanceof ConnectionManager) {
       String ports = Command.getFieldValue(packet, "TCP/IP ports");
       if (ports == null) {
         Command.addFieldValue(
             result, "Info2", "1. 'Next' to set more component parameters.", "fixed");
         Command.addFieldValue(
             result, "Info3", "2. 'Previous' to go back and select different component.", "fixed");
         Command.addAction(result, Command.Action.next);
         Command.addAction(result, Command.Action.prev);
         Command.addFieldValue(
             result,
             "Info4",
             "This component uses TCP/IP ports, please provide port numbers:",
             "fixed");
         Command.addFieldValue(result, "TCP/IP ports", "5557");
         return;
       } else {
         String[] ports_arr = ports.split(",");
         int[] ports_i = new int[ports_arr.length];
         try {
           for (int i = 0; i < ports_arr.length; i++) {
             ports_i[i] = Integer.decode(ports_arr[i].trim());
           } // end of for (int i = 0; i < ports_arr.length; i++)
           defConfigParams.put(new_comp_name + "/connections/ports", ports_i);
         } catch (Exception e) {
           Command.addFieldValue(
               result, "Info2", "1. 'Next' to set more component parameters.", "fixed");
           Command.addFieldValue(
               result,
               "Info3",
               "2. 'Previous' to go back and select different component.",
               "fixed");
           Command.addAction(result, Command.Action.next);
           Command.addAction(result, Command.Action.prev);
           Command.addFieldValue(
               result,
               "Info4",
               "Incorrect TCP/IP ports provided, please provide port numbers:",
               "fixed");
           Command.addFieldValue(result, "TCP/IP ports", ports);
           return;
         } // end of try-catch
       } // end of else
     }
     Command.addFieldValue(
         result, "Info2", "1. 'Finish' to create component with this parameters.", "fixed");
     Command.addFieldValue(
         result, "Info3", "2. 'Previous' to go back and select different component.", "fixed");
     Command.addAction(result, Command.Action.complete);
     Command.addAction(result, Command.Action.prev);
     mr.setName(new_comp_name);
     if (mr instanceof Configurable) {
       // Load defaults into sorted Map:
       Map<String, Object> comp_props =
           new TreeMap<String, Object>(((Configurable) mr).getDefaults(defConfigParams));
       for (Map.Entry<String, Object> entry : comp_props.entrySet()) {
         Command.addFieldValue(
             result,
             XMLUtils.escape(entry.getKey()),
             XMLUtils.escape(objectToString(entry.getValue())));
       } // end of for (Map.Entry entry: prop.entrySet())
     } else {
       Command.addFieldValue(
           result, "Info6", "Component is not configurable, do you want to create it?", "fixed");
     } // end of else
     Command.addFieldValue(result, "Params set", "true", "hidden");
   } catch (Exception e) {
     log.log(Level.SEVERE, "Problem instantiating component:", e);
     Command.addFieldValue(
         result,
         "Component class",
         "ERROR!! Problem instantiating component, " + "look in log file for details...",
         "text-single",
         "Component class");
   } // end of try-catch
 }
コード例 #9
0
 private void createNewComponent(Packet packet, Packet result, boolean admin) {
   String new_comp_name = Command.getFieldValue(packet, "Component name");
   String new_comp_class = Command.getFieldValue(packet, "Component class");
   try {
     MessageReceiver mr = (MessageReceiver) Class.forName(new_comp_class).newInstance();
     mr.setName(new_comp_name);
     if (mr instanceof Configurable) {
       Map<String, Object> comp_props = ((Configurable) mr).getDefaults(defConfigParams);
       Map<String, Object> new_params = new LinkedHashMap<String, Object>(comp_props);
       // Convert String values to proper Objecy values
       for (Map.Entry<String, Object> entry : comp_props.entrySet()) {
         String val = Command.getFieldValue(packet, XMLUtils.escape(entry.getKey()));
         if (val == null) {
           val = "";
         }
         val = XMLUtils.unescape(val);
         log.info("New component value: " + entry.getKey() + "=" + val);
         setValue(entry.getKey(), val, false, false, new_params);
       } // end of for (Map.Entry entry: prop.entrySet())
       if (admin) {
         // Now we can save all properties to config repository:
         for (Map.Entry<String, Object> entry : new_params.entrySet()) {
           String key = entry.getKey();
           String subnode = null;
           int key_idx = entry.getKey().lastIndexOf('/');
           if (key_idx > 0) {
             key = entry.getKey().substring(key_idx + 1);
             subnode = entry.getKey().substring(0, key_idx);
           }
           log.info(
               "Saving property to repository: "
                   + "root="
                   + new_comp_name
                   + ", subnode="
                   + subnode
                   + ", key="
                   + key
                   + ", value="
                   + entry.getValue());
           repository.set(new_comp_name, subnode, key, entry.getValue());
         } // end of for (Map.Entry entry: prop.entrySet())
         // And load the component itself.....
         // Set class name for the component
         repository.set(
             routerCompName,
             "/components/msg-receivers",
             new_comp_name + ".class",
             new_comp_class);
         // Activate the component
         repository.set(
             routerCompName, "/components/msg-receivers", new_comp_name + ".active", true);
         // Add to the list of automaticaly loaded components
         setValue(
             routerCompName + "/components/msg-receivers/id-names",
             new_comp_name,
             true,
             false,
             null);
         // repository.sync();
         setup(routerCompName);
       } // end of if (admin)
     }
     Command.addNote(result, "New component created: " + new_comp_name);
     Command.addFieldValue(result, "Note", "New component created: " + new_comp_name, "fixed");
   } catch (Exception e) {
     log.log(Level.SEVERE, "Problem instantiating component:", e);
     Command.addFieldValue(
         result,
         "Component class",
         "ERROR!! Problem instantiating component, " + "look in log file for details...",
         "text-single",
         "Component class");
   } // end of try-catch
 }
コード例 #10
0
  @Override
  public void processPacket(final Packet packet, final Queue<Packet> results) {

    if (!packet.isCommand()) {
      return;
    }
    Iq iqc = (Iq) packet;

    if (iqc.getType() != null && iqc.getType() == StanzaType.error) {
      log.info("Ignoring error packet: " + iqc.toString());
      return;
    }

    String nick = iqc.getTo().getLocalpart();
    if (nick == null || !getName().equals(nick)) return;

    String msg =
        "Please be careful, you are service admin and all changes"
            + " you make are instantly applied to live system!";
    boolean admin = true;
    if (iqc.getPermissions() != Permissions.ADMIN) {
      if (demoMode) {
        admin = false;
        msg =
            "You are not admin. You can safely play with the settings as"
                + " you can not change anything.";
        if (iqc.getStrCommand() != null && iqc.getStrCommand().endsWith(DEF_SM_NAME)) {
          Packet result = iqc.commandResult(Command.DataType.result);
          Command.addFieldValue(result, "Note", msg, "fixed");
          Command.addFieldValue(
              result, "Note", "Restricted area, only admin can see these settings.", "fixed");
          results.offer(result);
          return;
        }
      } else {
        try {
          results.offer(
              Authorization.NOT_AUTHORIZED.getResponseMessage(
                  packet, "You are not authorized for this action.", true));
        } catch (PacketErrorTypeException e) {
          log.warning("Packet processing exception: " + e);
        }
        return;
      }
    }

    if (log.isLoggable(Level.FINEST)) {
      log.finest("Command received: " + iqc.toString());
    }

    Command.Action action = Command.getAction(iqc);
    if (action == Command.Action.cancel) {
      Packet result = iqc.commandResult(null);
      results.offer(result);
      return;
    }

    switch (iqc.getCommand()) {
      case OTHER:
        if (iqc.getStrCommand() != null) {
          if (iqc.getStrCommand().startsWith("config/list/")) {
            try {
              String[] spl = iqc.getStrCommand().split("/");
              Packet result = iqc.commandResult(Command.DataType.result);
              Command.addFieldValue(result, "Note", msg, "fixed");
              Map<String, Object> allprop = getAllProperties(spl[2]);
              for (Map.Entry<String, Object> entry : allprop.entrySet()) {
                Command.addFieldValue(
                    result,
                    XMLUtils.escape(entry.getKey()),
                    XMLUtils.escape(objectToString(entry.getValue())));
              } // end of for (Map.Entry entry: prop.entrySet())
              results.offer(result);
            } catch (ConfigurationException ex) {
              Logger.getLogger(ConfiguratorOld.class.getName()).log(Level.SEVERE, null, ex);
            }
          }
          if (iqc.getStrCommand().startsWith("config/set/")) {
            try {
              String[] spl = iqc.getStrCommand().split("/");
              Packet result = iqc.commandResult(Command.DataType.result);
              Command.addFieldValue(result, "Note", msg, "fixed");
              if (Command.getData(packet) == null) {
                prepareConfigData(result, spl[2]);
                results.offer(result);
              } else {
                updateConfigChanges(packet, result, spl[2], admin);
                results.offer(result);
              }
            } catch (ConfigurationException ex) {
              Logger.getLogger(ConfiguratorOld.class.getName()).log(Level.SEVERE, null, ex);
            }
          }
        }
        break;
      default:
        break;
    }
  }
コード例 #11
0
  @Override
  protected void processCommand(Packet packet) {
    BoshSession session = getBoshSession(packet.getTo());

    switch (packet.getCommand()) {
      case USER_LOGIN:
        String jid = Command.getFieldValue(packet, "user-jid");

        if (jid != null) {
          if (session != null) {
            try {
              BareJID fromJID = BareJID.bareJIDInstance(jid);
              BareJID hostJid = getSeeOtherHostForJID(fromJID, Phase.LOGIN);

              if (hostJid != null) {
                Element streamErrorElement =
                    see_other_host_strategy.getStreamError(
                        "urn:ietf:params:xml:ns:xmpp-streams", hostJid);
                Packet redirectPacket = Packet.packetInstance(streamErrorElement);

                redirectPacket.setPacketTo(packet.getTo());
                writePacketToSocket(redirectPacket);
                session.sendWaitingPackets();
                session.close();
                if (log.isLoggable(Level.FINE)) {
                  log.log(
                      Level.FINE,
                      "{0} : {1} ({2})",
                      new Object[] {
                        BOSH_OPERATION_TYPE.REMOVE, session.getSid(), "See other host"
                      });
                }
                sessions.remove(session.getSid());
              } else {
                session.setUserJid(jid);
              }
            } catch (TigaseStringprepException ex) {
              log.log(Level.SEVERE, "user JID violates RFC6122 (XMPP:Address Format): ", ex);
            }
          } else {
            if (log.isLoggable(Level.FINE)) {
              log.log(Level.FINE, "Missing XMPPIOService for USER_LOGIN command: {0}", packet);
            }
          }
        } else {
          log.log(Level.WARNING, "Missing user-jid for USER_LOGIN command: {0}", packet);
        }

        break;

      case CLOSE:
        if (session != null) {
          if (log.isLoggable(Level.FINER)) {
            log.log(Level.FINER, "Closing session for command CLOSE: {0}", session.getSid());
          }
          try {
            List<Element> err_el = packet.getElement().getChildrenStaticStr(Iq.IQ_COMMAND_PATH);

            if ((err_el != null) && (err_el.size() > 0)) {
              Element error = new Element("stream:error");

              error.addChild(err_el.get(0));

              Packet condition = Packet.packetInstance(error);

              condition.setPacketTo(packet.getTo());
              writePacketToSocket(condition);
              session.sendWaitingPackets();
              bosh_session_close_delay = 100;
            }
          } catch (TigaseStringprepException ex) {
            Logger.getLogger(BoshConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
          }
          if (bosh_session_close_delay > 0) {
            try {
              Thread.sleep(bosh_session_close_delay);
            } catch (InterruptedException ex) {

              // Intentionally left blank
            }
          }
          session.close();
          if (log.isLoggable(Level.FINE)) {
            log.log(
                Level.FINE,
                "{0} : {1} ({2})",
                new Object[] {
                  BOSH_OPERATION_TYPE.REMOVE, session.getSid(), "Closing session for command CLOSE"
                });
          }
          sessions.remove(session.getSid());
        } else {
          if (log.isLoggable(Level.FINE)) {
            log.log(Level.FINE, "Session does not exist for packet: {0}", packet);
          }
        }

        break;

      case CHECK_USER_CONNECTION:
        if (session != null) {

          // It's ok, the session has been found, respond with OK.
          addOutPacket(packet.okResult((String) null, 0));
        } else {

          // Session is no longer active, respond with an error.
          try {
            addOutPacket(
                Authorization.ITEM_NOT_FOUND.getResponseMessage(packet, "Connection gone.", false));
          } catch (PacketErrorTypeException e) {

            // Hm, error already, ignoring...
            log.log(Level.INFO, "Error packet is not really expected here: {0}", packet);
          }
        }

        break;

      default:
        super.processCommand(packet);

        break;
    } // end of switch (pc.getCommand())
  }