Example #1
0
  public boolean logout() {
    PieUser user = userService.getUser();
    user.setPassword(null);
    user.setIsLoggedIn(false);
    try {
      File unencryptedFile =
          new File(
              String.format(
                  unsafeFile, user.getPieShareConfiguration().getPwdFile().getCanonicalPath()));
      unencryptedFile.delete();

      File ivF = new File(user.getPieShareConfiguration().getPwdFile().getParent(), ivFile);
      if (ivF.exists()) {
        ivF.delete();
      }

      this.clusterManagementService.disconnect(user.getCloudName());
    } catch (IOException e) {
      PieLogger.error(
          this.getClass(), String.format("Error in Logout. Message: %s", e.getMessage()));
      return false;
    } catch (ClusterServiceException e) {
      PieLogger.error(
          this.getClass(), String.format("Error in Logout. Message: %s", e.getMessage()));
      return false;
    }
    PieLogger.info(this.getClass(), "Logout Successful!");
    return true;
  }
 @Override
 public int getNewPortFrom(int port) {
   for (int i = port; i <= MAX_PORT; i++) {
     try {
       DatagramSocket s = new DatagramSocket(i);
       s.close();
       return i;
     } catch (BindException ex) {
       PieLogger.info(this.getClass(), "Port could not be found. Try next one.", ex);
     } catch (SocketException ex) {
       PieLogger.info(this.getClass(), "Port could not be found. Try next one.", ex);
     }
   }
   return -1;
 }
Example #3
0
 public JsonObject processInput(byte[] input) {
   ByteArrayInputStream byteInStream = new ByteArrayInputStream(input);
   JsonReader jsonReader = Json.createReader(byteInStream);
   JsonObject ob = jsonReader.readObject();
   PieLogger.info(this.getClass(), String.format("ConnectionText: %s", ob.toString()));
   return ob;
 }
Example #4
0
 public void resetPassword() {
   logout();
   PieUser user = userService.getUser();
   user.getPieShareConfiguration().getPwdFile().delete();
   databaseService.removePieUser(user);
   user.setHasPasswordFile(false);
   PieLogger.info(this.getClass(), "Reset Password Successful!");
 }
Example #5
0
  public boolean createUser(PlainTextPassword pp) {
    EncryptedPassword pwd1 = passwordEncryptionService.encryptPassword(pp);
    pp = null;
    try {
      PieLogger.trace(this.getClass(), "Creating user {}!", userService.getUser().getUserName());
      createNewPwdFile(pwd1, userService.getUser().getPieShareConfiguration().getPwdFile());
      userService.getUser().setHasPasswordFile(true);

      // This is only for Android
      if (userService.getUser().getUserName() != null) {
        databaseService.persistPieUser(userService.getUser());
      }
    } catch (Exception e) {
      PieLogger.error(
          this.getClass(),
          String.format("Error creating password file. Message: %s", e.getMessage()));
      return false;
    }
    return true;
  }
Example #6
0
  private synchronized void newPacketReceived() {
    byte[] bytes = new byte[1024];
    bytes = Arrays.copyOfRange(bytes, 0, packet.getLength());
    PieLogger.info(this.getClass(), String.format("Input Message: %s", new String(bytes)));
    JsonObject input = processInput(bytes);

    if (input.getString("type").equals("connection")) {
      JsonObject newClient = input.getJsonObject("client");
      callback.Handle(newClient);
    }
    if (input.getString("type").equals("msg")) {
      System.out.println("Message Arrived: " + input.getString("msg"));
    }
    if (input.getString("type").equals("punch")) {
      sendTask.send(ackMsg.getBytes(), packet.getAddress().getHostAddress(), packet.getPort());
    }
    if (input.getString("type").equals("ACK")) {
      sendTask.setACK(true);
    }
  }
 @Override
 public void run() {
   PieLogger.info(this.getClass(), String.format("ACK Arrived from: %s", msg.getSenderID()));
   loopHoleFactory.getLoopHoleService(msg.getLocalLoopID()).ackArrived(msg.getSenderID());
 }
Example #8
0
  public boolean Login(PlainTextPassword pp, String userName) {
    EncryptedPassword pwd1 = null;

    PieUser user = userService.getUser();

    if (user == null || user.getPieShareConfiguration() == null) {
      return false;
    }

    File pwdFile = user.getPieShareConfiguration().getPwdFile();

    if (pp != null) {
      pwd1 = passwordEncryptionService.encryptPassword(pp);
      pp = null;
    } else {
      try {
        File unencryptedFile =
            new File(
                String.format(
                    unsafeFile, user.getPieShareConfiguration().getPwdFile().getCanonicalPath()));
        if (!unencryptedFile.exists()) {
          return false;
        }
        pwd1 =
            passwordEncryptionService.getEncryptedPasswordFromExistingSecretKey(
                FileUtils.readFileToByteArray(unencryptedFile));

        File ivF = new File(user.getPieShareConfiguration().getPwdFile().getParent(), ivFile);
        if (ivF.exists()) {
          pwd1.setIv(FileUtils.readFileToByteArray(ivF));
        }
      } catch (IOException e) {
        PieLogger.error(
            this.getClass(), String.format("error during auto login! Message: %s", e.getMessage()));
        return false;
      }
    }
    pwd1.setUseIv(useIv);

    if (pwdFile.exists()) {
      try {
        if (!Arrays.equals(
            encodeService.decrypt(pwd1, FileUtils.readFileToByteArray(pwdFile)),
            pwd1.getPassword())) {
          return false; // throw new WrongPasswordException("The given password was wrong.");
        }

        File unencryptedFile =
            new File(
                String.format(
                    unsafeFile, user.getPieShareConfiguration().getPwdFile().getCanonicalPath()));
        FileUtils.writeByteArrayToFile(unencryptedFile, pwd1.getPassword());

        if (pwd1.getIv() != null) {
          FileUtils.writeByteArrayToFile(
              new File(
                  userService.getUser().getPieShareConfiguration().getPwdFile().getParent(),
                  ivFile),
              pwd1.getIv());
        }

      } catch (Exception ex) {
        PieLogger.info(
            this.getClass(),
            String.format(
                "Wrong password, not possible to encrypt file! Messgae %s", ex.getMessage()));
        return false;
      }
    } else {
      PieLogger.info(this.getClass(), "Tried to login but no passwordFile was avaliable!");
      return false;
    }

    if (userService.getUser().getUserName() == null && userName != null) {
      userService.getUser().setUserName(userName);
    }

    databaseService.mergePieUser(userService.getUser());

    user.setPassword(pwd1);
    user.setHasPasswordFile(true);
    user.setIsLoggedIn(true);

    SymmetricEncryptedChannel channel = this.symmetricEncryptedChannelProvider.get();
    channel.setChannelId(user.getUserName());
    channel.setEncPwd(user.getPassword());

    try {
      this.clusterManagementService.registerChannel(user.getCloudName(), channel);

    } catch (ClusterManagmentServiceException e) {
      PieLogger.error(
          this.getClass(),
          String.format("Error in Register Channel. Message:  %s", e.getMessage()));
    }

    PieLogger.info(this.getClass(), "Login Successful");

    this.historyService.syncLocalFilders();
    // todo-mr3: now we have to send a list with everything

    // todo-after-ase: ultimatively instead of making an initial sync we have to perform a list
    // request when ever
    // the cluster state changes
    try {
      // todo: change this maybe in future to different aproach
      // should probably be coupled with network chang detection!
      // this is needed to recognize local changes on this node
      IFileListMessage fileList = this.messageFactoryService.getFileListMessage();
      fileList.getAddress().setClusterName(userService.getUser().getCloudName());
      fileList.getAddress().setChannelId(userService.getUser().getUserName());
      fileList.setFileList(this.historyService.getPieFiles());
      fileList.setFolderList(this.historyService.getPieFolders());
      this.clusterManagementService.sendMessage(fileList);

      // send file list request message to cluster
      FileListRequestMessage msg = this.messageFactoryService.getFileListRequestMessage();
      msg.getAddress().setClusterName(userService.getUser().getCloudName());
      msg.getAddress().setChannelId(userService.getUser().getUserName());
      this.clusterManagementService.sendMessage(msg);
    } catch (ClusterManagmentServiceException ex) {
      PieLogger.error(this.getClass(), "Connect failed!", ex);
    }

    return true;
  }