diff options
| author | Nils Schwabe | 2014-06-05 12:34:12 +0200 |
|---|---|---|
| committer | Nils Schwabe | 2014-06-05 12:34:12 +0200 |
| commit | 98cfa2231cb0931fd1d3f6d6582becfd359881f9 (patch) | |
| tree | 70a0c21ff4ecec49cfea447cabadfed6fd7ef8f4 /src/main/java/org/openslx/imagemaster/ftp | |
| parent | Add webinterface with functionallity (diff) | |
| download | masterserver-98cfa2231cb0931fd1d3f6d6582becfd359881f9.tar.gz masterserver-98cfa2231cb0931fd1d3f6d6582becfd359881f9.tar.xz masterserver-98cfa2231cb0931fd1d3f6d6582becfd359881f9.zip | |
Started to implement download of images
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/ftp')
4 files changed, 143 insertions, 25 deletions
diff --git a/src/main/java/org/openslx/imagemaster/ftp/FtpCredentialsScheduler.java b/src/main/java/org/openslx/imagemaster/ftp/FtpCredentialsScheduler.java index 557cc72..eba3f55 100644 --- a/src/main/java/org/openslx/imagemaster/ftp/FtpCredentialsScheduler.java +++ b/src/main/java/org/openslx/imagemaster/ftp/FtpCredentialsScheduler.java @@ -28,7 +28,7 @@ public class FtpCredentialsScheduler extends TimerTask // List to save the users that need to be deleted after iterating the map List<String> usersToDelete = new LinkedList<>(); // check all folders - for ( Map.Entry<String, Long> entry : App.ftpServer.users.entrySet() ) { + for ( Map.Entry<String, MasterFtpServer.Infos> entry : App.ftpServer.users.entrySet() ) { if ( entry == null ) continue; String username = entry.getKey(); @@ -53,7 +53,7 @@ public class FtpCredentialsScheduler extends TimerTask usersToDelete.add( username ); } else { // check the creation time of the user - if ( ( System.currentTimeMillis() - App.ftpServer.users.get( username ) ) >= timeout ) { + if ( ( System.currentTimeMillis() - App.ftpServer.users.get( username ).getCreateTime() ) >= timeout ) { log.info( username + " did nothing for too long. Deleting him and his folder" ); usersToDelete.add( username ); } diff --git a/src/main/java/org/openslx/imagemaster/ftp/ImageProcessor.java b/src/main/java/org/openslx/imagemaster/ftp/ImageProcessor.java index ffb7e2b..8359e5a 100644 --- a/src/main/java/org/openslx/imagemaster/ftp/ImageProcessor.java +++ b/src/main/java/org/openslx/imagemaster/ftp/ImageProcessor.java @@ -9,7 +9,9 @@ import org.apache.log4j.Logger; import org.openslx.imagemaster.App; import org.openslx.imagemaster.Globals; import org.openslx.imagemaster.Globals.PropInt; +import org.openslx.imagemaster.db.DbFtpUser; import org.openslx.imagemaster.db.DbImage; +import org.openslx.imagemaster.db.DbUser; import org.openslx.imagemaster.thrift.iface.ImageData; import org.openslx.imagemaster.thrift.iface.ImageDataError; import org.openslx.imagemaster.thrift.iface.ImageDataException; @@ -22,7 +24,7 @@ public class ImageProcessor private static HashMap<String, ImageData> images = new HashMap<>(); /** - * After (re)starting server: check dead !uploading! images in database + * After (re)starting server: check for dead !uploading! images in database */ public static void checkUploading() { long timeout = Globals.getPropertyInt( PropInt.FTPTIMEOUT ) * 60L * 1000L; @@ -43,10 +45,8 @@ public class ImageProcessor DbImage.delete( dbImage.UUID ); log.info( "Deleted dbimage from db: " + dbImage.UUID + " due to timeout"); } else { - // add ftpUser to list - synchronized ( App.ftpServer.users ) { - App.ftpServer.users.put( dbImage.ftpUser, dbImage.timestamp.getTime() ); - } + // re add users to ftpserver + App.ftpServer.addUser( dbImage.ftpUser, DbFtpUser.getUserByName( dbImage.ftpUser ).password, MasterFtpServer.Mode.UPLOADING, "" ); log.info( "Added user '" + dbImage.ftpUser + "' to list again." ); // add image to process list again @@ -106,7 +106,7 @@ public class ImageProcessor * the data for the image to add * @exception ImageDataException If the imagedata contains errors */ - public static void addImageDataToProcess( ImageData imageData, String username ) throws ImageDataException + public static void addImageDataToProcess( ImageData imageData, String username, String password ) throws ImageDataException { log.info( "Adding image to process list: " + imageData.imageName + ", submitted by " + username ); @@ -121,7 +121,7 @@ public class ImageProcessor throw new ImageDataException(ImageDataError.INVALID_DATA, "UUID not valid."); } else if (!imageData.imageName.matches( "^[a-zA-Z0-9_\\-]{5,50}$" )) { throw new ImageDataException(ImageDataError.INVALID_DATA, "ImageName not valid. (Length must be 5 to 50)"); - } else if (false) { // TODO: check imageowner + } else if (DbUser.getUserIdByName( imageData.imageOwner ) != 0) { throw new ImageDataException(ImageDataError.INVALID_DATA, "ImageOwner not valid."); } else if (!imageData.conentOperatingSystem.matches( "^[\\w][-0-9a-zA-Z]{3,20}$" )) { throw new ImageDataException(ImageDataError.INVALID_DATA, "ContentOperatingSystem not valid. (Length must be 3 to 20)"); @@ -142,7 +142,7 @@ public class ImageProcessor } // if everything went fine, add image to db - DbImage.insert( imageData, System.currentTimeMillis(), username); + DbImage.insert( imageData, System.currentTimeMillis(), username, password); // and to processinglist images.put( username, imageData ); diff --git a/src/main/java/org/openslx/imagemaster/ftp/MasterFtpServer.java b/src/main/java/org/openslx/imagemaster/ftp/MasterFtpServer.java index 9dae088..f8b8ca1 100644 --- a/src/main/java/org/openslx/imagemaster/ftp/MasterFtpServer.java +++ b/src/main/java/org/openslx/imagemaster/ftp/MasterFtpServer.java @@ -29,14 +29,55 @@ import org.openslx.imagemaster.util.Util; public class MasterFtpServer implements Runnable { + private static Logger log = Logger.getLogger( MasterFtpServer.class ); private FtpServer server; private UserManager userManager; private Listener listener; - // key: ftpUsername, value: createTime - public final HashMap<String, Long> users = new HashMap<>(); + // key: ftpUsername, value: infos + public final HashMap<String, Infos> users = new HashMap<>(); private boolean ini = false; + public enum Mode + { + UPLOADING, DOWNLOADING + } + + /** + * Class to hold infos of a ftp user. + * + * @author nils + * + */ + public class Infos + { + private final Long createTime; + private final Mode mode; + private final String fileName; + + public Infos(Long createTime, Mode mode, String fileName) + { + this.createTime = createTime; + this.mode = mode; + this.fileName = fileName; + } + + public Long getCreateTime() + { + return this.createTime; + } + + public Mode getMode() + { + return this.mode; + } + + public String getFileName() + { + return this.fileName; + } + } + public void init( int port ) { if ( ini ) @@ -77,8 +118,53 @@ public class MasterFtpServer implements Runnable server = serverFactory.createServer(); ini = true; } + + /** + * Add a user with username and password. + * @param username + * @param password + * @param mode + * @param fileName the filename of the file, that is allowed to access (if downloading) + * @return + */ + public boolean addUser( String username, String password, Mode mode, String fileName) + { + String dir = Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) + "/" + username + "/"; + + BaseUser user = new BaseUser(); + user.setName( username ); + user.setPassword( password ); + List<Authority> authorities = new ArrayList<Authority>(); + + String file = ""; + + if (mode == Mode.UPLOADING) { + user.setHomeDirectory( dir ); + // uploading satellite is allowed to write + authorities.add( new WritePermission() ); + } else if (mode == Mode.DOWNLOADING) { + // the downloading satellite may access the whole dir, but this is restricted in MasterFtplet + user.setHomeDirectory( Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) ); + // downloading satellite is only allowed to read + file = fileName; + } + + user.setAuthorities( authorities ); + + try { + userManager.save( user ); + synchronized ( users ) { + users.put( username, new Infos( System.currentTimeMillis(), mode, file) ); + + } + } catch ( FtpException e ) { + return false; + } + + return true; + } - public FtpCredentials addUser( final String serverSessionId ) + public FtpCredentials addUser( final String serverSessionId, Mode mode, String fileName ) { FtpCredentials ftpCredentials = null; @@ -88,26 +174,38 @@ public class MasterFtpServer implements Runnable String dir = Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) + "/" + generatedUser + "/"; - if ( !new File( dir ).mkdir() ) { - return ftpCredentials; - } - BaseUser user = new BaseUser(); user.setName( generatedUser ); user.setPassword( generatedPass ); - user.setHomeDirectory( dir ); - List<Authority> authorities = new ArrayList<Authority>(); - authorities.add( new WritePermission() ); + + String file = ""; + + if (mode == Mode.UPLOADING) { + // uploading satellite needs a folder + if ( !new File( dir ).mkdir() ) { + return null; + } + user.setHomeDirectory( dir ); + // uploading satellite is allowed to write + authorities.add( new WritePermission() ); + } else if (mode == Mode.DOWNLOADING) { + // the downloading satellite may access the whole dir, but this is restricted in MasterFtplet + user.setHomeDirectory( Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) ); + // downloading satellite is only allowed to read + file = fileName; + } + user.setAuthorities( authorities ); try { userManager.save( user ); ftpCredentials = new FtpCredentials( generatedUser, generatedPass ); synchronized ( users ) { - users.put( ftpCredentials.username, System.currentTimeMillis()); + users.put( ftpCredentials.username, new Infos( System.currentTimeMillis(), mode, file) ); } } catch ( FtpException e ) { + // TODO: handle this } log.info( "Generated user/pass: " + generatedUser + "\t" @@ -124,10 +222,11 @@ public class MasterFtpServer implements Runnable try { // first find active session and close it Iterator<FtpIoSession> iter = listener.getActiveSessions().iterator(); - while (iter.hasNext()) { - FtpIoSession session = (FtpIoSession) iter.next(); - if (session.getUser() == null) continue; - if (session.getUser().getName() == username) { + while ( iter.hasNext() ) { + FtpIoSession session = (FtpIoSession)iter.next(); + if ( session.getUser() == null ) + continue; + if ( session.getUser().getName() == username ) { session.close(); } } diff --git a/src/main/java/org/openslx/imagemaster/ftp/MasterFtplet.java b/src/main/java/org/openslx/imagemaster/ftp/MasterFtplet.java index 5e12628..31d39dc 100644 --- a/src/main/java/org/openslx/imagemaster/ftp/MasterFtplet.java +++ b/src/main/java/org/openslx/imagemaster/ftp/MasterFtplet.java @@ -9,7 +9,9 @@ import org.apache.ftpserver.ftplet.FtpSession; import org.apache.ftpserver.ftplet.Ftplet; import org.apache.ftpserver.ftplet.FtpletContext; import org.apache.ftpserver.ftplet.FtpletResult; +import org.apache.ftpserver.ftplet.User; import org.apache.log4j.Logger; +import org.openslx.imagemaster.App; public class MasterFtplet implements Ftplet { @@ -33,6 +35,23 @@ public class MasterFtplet implements Ftplet { if ( session.getUser() != null ) { log.info( session.getUser().getName() + " issued command: " + request.getRequestLine() ); + // check if masterserver is still knowing this user + if (App.ftpServer.users.containsKey( session.getUser().getName() )) { + MasterFtpServer.Infos infos = App.ftpServer.users.get( session.getUser().getName() ); + if (infos.getMode() == MasterFtpServer.Mode.DOWNLOADING) { + if (request.getCommand() == "RETR") { + // check if user is getting the right file + if (infos.getFileName() != request.getArgument()) { + throw new FtpException( "550 File unavailable." ); + } + } + // TODO: block all other commands except login and retrieve + } + } else { + // user is not valid anymore + throw new FtpException( "430 Invalid username or password." ); // ERROR CODE 430 --> invalid username or password + } + } return null; } |
