Ejemplo n.º 1
0
  public void startServer() {
    FtpServerFactory serverFactory = new FtpServerFactory();
    ListenerFactory factory = new ListenerFactory();

    BaseUser user = new BaseUser();
    user.setName("test");
    user.setPassword("123456");
    user.setHomeDirectory("D:/test");

    int port = 2221;

    factory.setPort(port);
    // replace the default listener
    serverFactory.addListener("default", factory.createListener());

    List<Authority> authorities = new ArrayList<Authority>();
    authorities.add(new WritePermission());
    user.setAuthorities(authorities);

    try {
      serverFactory.getUserManager().save(user);
      FtpServer server = serverFactory.createServer();
      server.start();
    } catch (FtpException e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 2
0
  /**
   * Maps a GeoServer user to an ftp {@link User} by means of the provided Spring Security's {@link
   * UserDetailsService}.
   *
   * <p>The user's home directory is set to the root geoserver data dir in the case of
   * administrators or to {@code <data dir>/incoming/<user name>} in case of non administrators.
   *
   * @see org.apache.ftpserver.ftplet.UserManager#getUserByName(java.lang.String)
   */
  public BaseUser getUserByName(String username) throws FtpException {
    // basic ftp user setup
    BaseUser user = new BaseUser();
    user.setName(username);
    user.setPassword(null);
    user.setEnabled(true);
    // allow writing
    List<Authority> authorities = new ArrayList<Authority>();
    authorities.add(new WritePermission());
    authorities.add(new ConcurrentLoginPermission(Integer.MAX_VALUE, Integer.MAX_VALUE));
    user.setAuthorities(authorities);

    return user;
  }
  static {
    try {
      homeDirectory = TestFileUtils.createTmpDir();
      serverFactory = new FtpServerFactory();
      listenerFactory = new ListenerFactory();
      userManagerFactory = new PropertiesUserManagerFactory();
      userManager = userManagerFactory.createUserManager();
      user = new BaseUser();

      listenerFactory.setPort(2121);
      serverFactory.addListener("default", listenerFactory.createListener());

      user.setName("flumetest");
      user.setPassword("flumetest");
      user.setHomeDirectory(homeDirectory.toFile().getAbsolutePath());
      userManager.save(user);
      serverFactory.setUserManager(userManager);

      ftpServer = serverFactory.createServer();

    } catch (IOException | FtpException e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 4
0
  /**
   * @param ftpAuthRequest one of {@link org.apache.ftpserver.usermanager.AnonymousAuthentication}
   *     or {@link org.apache.ftpserver.usermanager.UsernamePasswordAuthentication}
   * @throws AuthenticationFailedException if given an {@code AnonymousAuthentication}, or an
   *     invalid/disabled user credentials
   * @see UserManager#authenticate(Authentication)
   */
  public User authenticate(final Authentication ftpAuthRequest)
      throws AuthenticationFailedException {
    if (!(ftpAuthRequest instanceof UsernamePasswordAuthentication)) {
      throw new AuthenticationFailedException();
    }
    final UsernamePasswordAuthentication upa = (UsernamePasswordAuthentication) ftpAuthRequest;
    final String principal = upa.getUsername();
    final String credentials = upa.getPassword();
    org.springframework.security.core.Authentication gsAuth =
        new UsernamePasswordAuthenticationToken(principal, credentials);
    try {
      gsAuth = authManager.authenticate(gsAuth);
    } catch (org.springframework.security.core.AuthenticationException authEx) {
      throw new AuthenticationFailedException(authEx);
    }

    try {
      // gather the user
      BaseUser user = getUserByName(principal);
      user.setPassword(credentials);
      // is the user enabled?
      if (!user.getEnabled()) {
        throw new AuthenticationFailedException();
      }

      // scary message for admins if the username/password has not
      // been changed
      if (DEFAULT_USER.equals(user.getName()) && DEFAULT_PASSWORD.equals(credentials)) {
        LOGGER.log(
            Level.SEVERE,
            "The default admin/password combination has not been "
                + "modified, this makes the embedded FTP server an "
                + "open file host for everybody to use!!!");
      }

      final File dataRoot = dataDir.findOrCreateDataRoot();

      // enable only admins and non anonymous users
      boolean isGSAdmin = false;
      for (GrantedAuthority authority : gsAuth.getAuthorities()) {
        final String userRole = authority.getAuthority();
        if (ADMIN_ROLE.equals(userRole)) {
          isGSAdmin = true;
          break;
        }
      }

      final File homeDirectory;
      if (isGSAdmin) {
        homeDirectory = dataRoot;
      } else {
        /*
         * This resolves the user's home directory to data/incoming/<user name> but does not
         * create the directory if it does not already exist. That is left to when the user
         * is authenticated, check the authenticate() method above.
         */
        homeDirectory = new File(new File(dataRoot, "incoming"), user.getName());
      }
      String normalizedPath = homeDirectory.getAbsolutePath();
      normalizedPath = FilenameUtils.normalize(normalizedPath);
      user.setHomeDirectory(normalizedPath);
      if (!homeDirectory.exists()) {
        LOGGER.fine(
            "Creating FTP home directory for user " + user.getName() + " at " + normalizedPath);
        homeDirectory.mkdirs();
      }

      return user;
    } catch (AuthenticationFailedException e) {
      throw e;
    } catch (Exception e) {
      LOGGER.log(Level.INFO, "FTP authentication failure", e);
      throw new AuthenticationFailedException(e);
    }
  }