diff options
author | Nils Schwabe | 2014-04-09 15:40:16 +0200 |
---|---|---|
committer | Nils Schwabe | 2014-04-09 15:40:16 +0200 |
commit | b30da021d491ace47c1b6a7b5ad11e595a2399c9 (patch) | |
tree | a90ccc1f2a7833df6727c181a885cc56dc2e463a | |
parent | fixed config dir (examples etc) (diff) | |
download | masterserver-b30da021d491ace47c1b6a7b5ad11e595a2399c9.tar.gz masterserver-b30da021d491ace47c1b6a7b5ad11e595a2399c9.tar.xz masterserver-b30da021d491ace47c1b6a7b5ad11e595a2399c9.zip |
Added Globals.java and global properties
Added Ftpserver test
Created some ftp testign in App.java (needs to be removed)
Added Ftplet to trigger on FTP Commands
-rw-r--r-- | config/ftp.properties | 7 | ||||
-rw-r--r-- | pom.xml | 5 | ||||
-rw-r--r-- | src/main/java/org/openslx/imagemaster/App.java | 83 | ||||
-rw-r--r-- | src/main/java/org/openslx/imagemaster/Globals.java | 7 | ||||
-rw-r--r-- | src/main/java/org/openslx/imagemaster/db/DbSatellite.java | 2 | ||||
-rw-r--r-- | src/main/java/org/openslx/imagemaster/db/LDAPUser.java | 2 | ||||
-rw-r--r-- | src/main/java/org/openslx/imagemaster/server/ApiServer.java | 1 | ||||
-rw-r--r-- | src/main/java/org/openslx/imagemaster/server/MasterFtpServer.java | 54 | ||||
-rw-r--r-- | src/main/java/org/openslx/imagemaster/server/MasterFtplet.java | 61 | ||||
-rw-r--r-- | src/main/properties/ftp.properties | 62 | ||||
-rw-r--r-- | src/test/java/org/openslx/imagemaster/AppTest.java | 57 |
11 files changed, 302 insertions, 39 deletions
diff --git a/config/ftp.properties b/config/ftp.properties deleted file mode 100644 index e870be4..0000000 --- a/config/ftp.properties +++ /dev/null @@ -1,7 +0,0 @@ -#Generated file - don't edit (please) -#Tue Apr 08 14:43:38 CEST 2014 -ftpserver.user.admin.writepermission=true -ftpserver.user.admin.idletime=0 -ftpserver.user.admin.enableflag=true -ftpserver.user.admin.homedirectory=/home/nils/ftp -ftpserver.user.admin.userpassword=7089682\:A20FCF1EF2A9697A3A35401529CF5953 @@ -107,5 +107,10 @@ <type>pom</type> <scope>compile</scope> </dependency> + <dependency> + <groupId>commons-net</groupId> + <artifactId>commons-net</artifactId> + <version>2.0</version> + </dependency> </dependencies> </project> diff --git a/src/main/java/org/openslx/imagemaster/App.java b/src/main/java/org/openslx/imagemaster/App.java index 9a5fd15..62e06a4 100644 --- a/src/main/java/org/openslx/imagemaster/App.java +++ b/src/main/java/org/openslx/imagemaster/App.java @@ -1,5 +1,10 @@ package org.openslx.imagemaster; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.security.SecureRandom; import java.util.ArrayList; import java.util.List; @@ -7,33 +12,78 @@ import org.apache.log4j.Logger; import org.openslx.imagemaster.server.MasterFtpServer; import org.openslx.imagemaster.thrift.server.BinaryListener; -/** - * Hello world! - * - */ -public class App -{ - private static Logger log = Logger.getLogger( App.class ); +public class App { + private static Logger log = Logger.getLogger(App.class); private static List<Thread> servers = new ArrayList<>(); - public static void main( String[] args ) - { + public static void main(String[] args) { // Init logging - log.info( "Starting Application" ); - + log.info("Starting Application"); + + // Load properties + boolean error = false; + BufferedInputStream stream = null; + try { + stream = new BufferedInputStream(new FileInputStream( + "config/global.properties")); + Globals.properties.load(stream); + } catch (IOException e) { + log.error("Could not load config file. Quitting."); + error = true; + } finally { + if (stream != null) { + try { + stream.close(); + } catch (IOException e) { + } + } + if (error) + System.exit(1); + log.info("Loaded config file"); + } + // Create binary listener Thread t; t = new Thread(new BinaryListener(), "BinaryListener"); servers.add(t); t.start(); - + // Create Ftp Server Thread f; - f = new Thread(new MasterFtpServer(2221, "admin", "admin", "/home/nils/ftp"), "FtpServer"); + MasterFtpServer ftpServer = new MasterFtpServer(2221, "admin", + "SI*HoZCC!]V)p>B2", Globals.properties.getProperty("ftp_base_dir")); + f = new Thread(ftpServer, "FtpServer"); servers.add(f); f.start(); + /* + * FTP Users can be added later with: + * ftpServer.addUser(username, password, ftp_root); + * + * And removed with: + * removeUser(username); + */ + + //////////////////////////////////////////////// + SecureRandom random = new SecureRandom(); + String lettersP="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+-$%&/()=?@"; + String lettersU="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; + String generatedUser = ""; + String generatedPass = ""; + for (int i = 0; i < 16; i++) { + int index = (int)(random.nextDouble()*lettersU.length()); + generatedUser += lettersU.substring(index, index + 1); + index = (int)(random.nextDouble()*lettersP.length()); + generatedPass += lettersP.substring(index, index + 1); + } + String dir = Globals.properties.getProperty("ftp_base_dir") + "/" + generatedUser + "/"; + if (new File(dir).mkdir()) { + ftpServer.addUser(generatedUser, generatedPass, dir, true); + log.info("Generated user/pass: " + generatedUser + "\t" + generatedPass + "\n with home dir: " + dir); + } + //////////////////////////////////////////////// + // Run more servers // ... // Wait for all servers to die @@ -43,11 +93,14 @@ public class App try { wait.join(); success = true; - } catch ( InterruptedException e ) { + } catch (InterruptedException e) { // Do nothing... } } } - log.info( "All Servers shut down, exiting..." ); + + ftpServer.removeUser(generatedUser); + + log.info("All Servers shut down, exiting..."); } } diff --git a/src/main/java/org/openslx/imagemaster/Globals.java b/src/main/java/org/openslx/imagemaster/Globals.java new file mode 100644 index 0000000..0cca10d --- /dev/null +++ b/src/main/java/org/openslx/imagemaster/Globals.java @@ -0,0 +1,7 @@ +package org.openslx.imagemaster; + +import java.util.Properties; + +public class Globals { + public static final Properties properties = new Properties(); +} diff --git a/src/main/java/org/openslx/imagemaster/db/DbSatellite.java b/src/main/java/org/openslx/imagemaster/db/DbSatellite.java index 037dfaa..7e9ef54 100644 --- a/src/main/java/org/openslx/imagemaster/db/DbSatellite.java +++ b/src/main/java/org/openslx/imagemaster/db/DbSatellite.java @@ -10,7 +10,7 @@ public class DbSatellite { this.name = name; } - public static DbSatellite createDbSatellite(String organization) { + public static DbSatellite fromOrganization(String organization) { return MySQL .findUniqueOrNull( DbSatellite.class, diff --git a/src/main/java/org/openslx/imagemaster/db/LDAPUser.java b/src/main/java/org/openslx/imagemaster/db/LDAPUser.java index ae8d38d..1034116 100644 --- a/src/main/java/org/openslx/imagemaster/db/LDAPUser.java +++ b/src/main/java/org/openslx/imagemaster/db/LDAPUser.java @@ -104,7 +104,7 @@ public class LDAPUser extends User { lastName = entry.get("sn").getString(); eMail = entry.get("rufPreferredMail").getString(); // get the satellite address from db - DbSatellite dbSatellite = DbSatellite.createDbSatellite(organization); + DbSatellite dbSatellite = DbSatellite.fromOrganization(organization); if (dbSatellite != null) { satelliteAddress = dbSatellite.getAddress(); } else { diff --git a/src/main/java/org/openslx/imagemaster/server/ApiServer.java b/src/main/java/org/openslx/imagemaster/server/ApiServer.java index dd0ec9e..ba15623 100644 --- a/src/main/java/org/openslx/imagemaster/server/ApiServer.java +++ b/src/main/java/org/openslx/imagemaster/server/ApiServer.java @@ -26,7 +26,6 @@ public class ApiServer { @SuppressWarnings( "unused" ) private static Logger log = Logger.getLogger( ApiServer.class ); - // /** * Request for authentication diff --git a/src/main/java/org/openslx/imagemaster/server/MasterFtpServer.java b/src/main/java/org/openslx/imagemaster/server/MasterFtpServer.java index f54b80d..9ec5cf7 100644 --- a/src/main/java/org/openslx/imagemaster/server/MasterFtpServer.java +++ b/src/main/java/org/openslx/imagemaster/server/MasterFtpServer.java @@ -2,23 +2,28 @@ package org.openslx.imagemaster.server; import java.io.File; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import org.apache.ftpserver.FtpServer; import org.apache.ftpserver.FtpServerFactory; import org.apache.ftpserver.ftplet.Authority; import org.apache.ftpserver.ftplet.FtpException; +import org.apache.ftpserver.ftplet.Ftplet; import org.apache.ftpserver.ftplet.UserManager; import org.apache.ftpserver.listener.ListenerFactory; import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory; import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor; import org.apache.ftpserver.usermanager.impl.BaseUser; import org.apache.ftpserver.usermanager.impl.WritePermission; +import org.apache.log4j.Logger; public class MasterFtpServer implements Runnable { + private static Logger log = Logger.getLogger( ApiServer.class ); private FtpServer server; + private UserManager userManager; - public MasterFtpServer(int port, String username, String password, String ftproot) { + public MasterFtpServer(int port, String adminUsername, String adminPassword, String ftproot) { FtpServerFactory serverFactory = new FtpServerFactory(); ListenerFactory factory = new ListenerFactory(); // set the port of the listener @@ -27,18 +32,28 @@ public class MasterFtpServer implements Runnable { // replace the default listener serverFactory.addListener("default", factory.createListener()); - // create new user - serverFactory.setUserManager(addUser(username, password, ftproot)); + // create user manager + PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); + userManagerFactory.setFile(new File("src/main/properties/ftp.properties")); + userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor()); + userManager = userManagerFactory.createUserManager(); + + // create new admin user + addUser(adminUsername, adminPassword, ftproot, true); + serverFactory.setUserManager(userManager); + + // add the Ftplet + HashMap<String, Ftplet> map = new HashMap<String, Ftplet>(); + map.put("Ftplet1", new MasterFtplet()); + serverFactory.setFtplets(map); // start the server - server = serverFactory.createServer(); + server = serverFactory.createServer(); } - private UserManager addUser(final String username, final String password, final String ftproot) { - PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); - userManagerFactory.setFile(new File("config/ftp.properties")); - userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor()); - UserManager um = userManagerFactory.createUserManager(); + public boolean addUser(final String username, final String password, final String ftproot, final boolean writeAccess) { + // TODO: enable SSL + boolean result = true; BaseUser user = new BaseUser(); user.setName(username); @@ -46,21 +61,34 @@ public class MasterFtpServer implements Runnable { user.setHomeDirectory(ftproot); List<Authority> authorities = new ArrayList<Authority>(); - authorities.add(new WritePermission()); + if (writeAccess) authorities.add(new WritePermission()); user.setAuthorities(authorities); try { - um.save(user); + userManager.save(user); } catch (FtpException e) { - e.printStackTrace(); + result = false; } - return um; + return result; + } + + public boolean removeUser(final String username) { + boolean result = true; + + try { + userManager.delete(username); + } catch (FtpException e) { + result = false; + } + + return result; } @Override public void run() { try { + log.info( "Starting FTP Sever" ); server.start(); } catch (FtpException e1) { e1.printStackTrace(); diff --git a/src/main/java/org/openslx/imagemaster/server/MasterFtplet.java b/src/main/java/org/openslx/imagemaster/server/MasterFtplet.java new file mode 100644 index 0000000..73d69d2 --- /dev/null +++ b/src/main/java/org/openslx/imagemaster/server/MasterFtplet.java @@ -0,0 +1,61 @@ +package org.openslx.imagemaster.server; + +import java.io.IOException; + +import org.apache.ftpserver.ftplet.FtpException; +import org.apache.ftpserver.ftplet.FtpReply; +import org.apache.ftpserver.ftplet.FtpRequest; +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.log4j.Logger; +import org.openslx.imagemaster.App; + +public class MasterFtplet implements Ftplet { + private static Logger log = Logger.getLogger(App.class); + + @Override + public void init(FtpletContext ftpletContext) throws FtpException { + // TODO Auto-generated method stub + + } + + @Override + public void destroy() { + // TODO Auto-generated method stub + + } + + @Override + public FtpletResult beforeCommand(FtpSession session, FtpRequest request) + throws FtpException, IOException { + if (session.getUser() != null && request.getCommand() == "QUIT") { + // TODO: tell server that user finished uploading + String user = session.getUser().getName(); + } + return null; + } + + @Override + public FtpletResult afterCommand(FtpSession session, FtpRequest request, + FtpReply reply) throws FtpException, IOException { + // TODO Auto-generated method stub + return null; + } + + @Override + public FtpletResult onConnect(FtpSession session) throws FtpException, + IOException { + // TODO Auto-generated method stub + return null; + } + + @Override + public FtpletResult onDisconnect(FtpSession session) throws FtpException, + IOException { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/main/properties/ftp.properties b/src/main/properties/ftp.properties new file mode 100644 index 0000000..e041deb --- /dev/null +++ b/src/main/properties/ftp.properties @@ -0,0 +1,62 @@ +#Generated file - don't edit (please) +#Wed Apr 09 15:16:39 CEST 2014 +ftpserver.user.ornfvnnRP87Xl7xW.enableflag=true +ftpserver.user.admin.idletime=0 +ftpserver.user.0XtC2vE7hLhc2wGP.homedirectory=/home/nils/ftp/0XtC2vE7hLhc2wGP/ +ftpserver.user.thdIK1sqi14NNSQW.idletime=0 +ftpserver.user.Tdy46R2LxVQ48tnb.userpassword=68185556\:1E8E70C82A0700A3358158E05C995E67 +ftpserver.user.ku9RfSkYXlFoFHDe.writepermission=true +ftpserver.user.admin.userpassword=73151046\:802FD3FB1DF982B2CF7476D31F1C374C +ftpserver.user.Tdy46R2LxVQ48tnb.homedirectory=/home/nils/ftp/Tdy46R2LxVQ48tnb/ +ftpserver.user.TNYUP487WN60FznV.idletime=0 +ftpserver.user.T71vcgAyaJBGta83.userpassword=40642381\:6B54E9ECCF9CDC7399B9D548CD081D5F +ftpserver.user.w64Nt2nX9tCUDZDT.homedirectory=/home/nils/ftp/w64Nt2nX9tCUDZDT/ +ftpserver.user.MrdFXvC6CZM6BBGX.idletime=0 +ftpserver.user.ku9RfSkYXlFoFHDe.idletime=0 +ftpserver.user.T71vcgAyaJBGta83.homedirectory=/home/nils/ftp/T71vcgAyaJBGta83/ +ftpserver.user.Tdy46R2LxVQ48tnb.writepermission=true +ftpserver.user.Tdy46R2LxVQ48tnb.idletime=0 +ftpserver.user.hLEsq2XpYL7LZdfB.userpassword=63925806\:9A310FDBCFD5D9A71074B3BF6374B450 +ftpserver.user.tKInWyoN97Tba26M.userpassword=8792198\:B0BC61AB950ED06EFB40F75771283496 +ftpserver.user.thdIK1sqi14NNSQW.enableflag=true +ftpserver.user.w64Nt2nX9tCUDZDT.idletime=0 +ftpserver.user.0XtC2vE7hLhc2wGP.enableflag=true +ftpserver.user.TNYUP487WN60FznV.writepermission=true +ftpserver.user.w64Nt2nX9tCUDZDT.writepermission=true +ftpserver.user.tKInWyoN97Tba26M.homedirectory=/home/nils/ftptKInWyoN97Tba26M/ +ftpserver.user.ku9RfSkYXlFoFHDe.userpassword=23632020\:466FFCA460B31C726330796F7A153AB6 +ftpserver.user.w64Nt2nX9tCUDZDT.enableflag=true +ftpserver.user.T71vcgAyaJBGta83.idletime=0 +ftpserver.user.TNYUP487WN60FznV.enableflag=true +ftpserver.user.0XtC2vE7hLhc2wGP.writepermission=true +ftpserver.user.TNYUP487WN60FznV.homedirectory=/home/nils/ftpTNYUP487WN60FznV/ +ftpserver.user.0XtC2vE7hLhc2wGP.idletime=0 +ftpserver.user.MrdFXvC6CZM6BBGX.userpassword=8026095\:F5ADB1E3BFB7735B110FF614AE110670 +ftpserver.user.admin.enableflag=true +ftpserver.user.admin.homedirectory=/home/nils/ftp +ftpserver.user.TNYUP487WN60FznV.userpassword=45535869\:3E98BECDE728C848E5AF0641CE954E1B +ftpserver.user.ornfvnnRP87Xl7xW.idletime=0 +ftpserver.user.thdIK1sqi14NNSQW.writepermission=true +ftpserver.user.ku9RfSkYXlFoFHDe.enableflag=true +ftpserver.user.T71vcgAyaJBGta83.writepermission=true +ftpserver.user.tKInWyoN97Tba26M.enableflag=true +ftpserver.user.MrdFXvC6CZM6BBGX.writepermission=true +ftpserver.user.ornfvnnRP87Xl7xW.homedirectory=/home/nils/ftp/ornfvnnRP87Xl7xW/ +ftpserver.user.Tdy46R2LxVQ48tnb.enableflag=true +ftpserver.user.MrdFXvC6CZM6BBGX.homedirectory=/home/nils/ftp/MrdFXvC6CZM6BBGX/ +ftpserver.user.0XtC2vE7hLhc2wGP.userpassword=1495110\:5591172A587F8511E40E02BF728E8A2D +ftpserver.user.ornfvnnRP87Xl7xW.userpassword=38130695\:4083386205AC1D9D0ADB2E646870491D +ftpserver.user.T71vcgAyaJBGta83.enableflag=true +ftpserver.user.w64Nt2nX9tCUDZDT.userpassword=34742693\:A13B18DA84B845DADE2BD60BF55E6340 +ftpserver.user.ornfvnnRP87Xl7xW.writepermission=true +ftpserver.user.admin.writepermission=true +ftpserver.user.thdIK1sqi14NNSQW.homedirectory=/home/nils/ftp/thdIK1sqi14NNSQW/ +ftpserver.user.ku9RfSkYXlFoFHDe.homedirectory=/home/nils/ftp/ku9RfSkYXlFoFHDe/ +ftpserver.user.hLEsq2XpYL7LZdfB.homedirectory=/home/nils/ftp/hLEsq2XpYL7LZdfB/ +ftpserver.user.hLEsq2XpYL7LZdfB.writepermission=true +ftpserver.user.thdIK1sqi14NNSQW.userpassword=97091881\:4B2A5E8286D91A1E4D54E38C6FFE4641 +ftpserver.user.MrdFXvC6CZM6BBGX.enableflag=true +ftpserver.user.hLEsq2XpYL7LZdfB.idletime=0 +ftpserver.user.hLEsq2XpYL7LZdfB.enableflag=true +ftpserver.user.tKInWyoN97Tba26M.writepermission=true +ftpserver.user.tKInWyoN97Tba26M.idletime=0 diff --git a/src/test/java/org/openslx/imagemaster/AppTest.java b/src/test/java/org/openslx/imagemaster/AppTest.java index 0add740..6c1350c 100644 --- a/src/test/java/org/openslx/imagemaster/AppTest.java +++ b/src/test/java/org/openslx/imagemaster/AppTest.java @@ -1,9 +1,17 @@ package org.openslx.imagemaster; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.ConnectException; + import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import org.apache.commons.net.ftp.FTP; +import org.apache.commons.net.ftp.FTPClient; +import org.apache.commons.net.ftp.FTPReply; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; @@ -66,7 +74,7 @@ public class AppTest assertTrue("Could not ping server", false); } try { - SessionData sessionData = client.authenticate("ns202", "xxxxxxxxxxx"); + SessionData sessionData = client.authenticate("ns202", "xxxxxxxxxxxx"); UserInfo userInfo = client.getUserFromToken(sessionData.getAuthToken()); System.out.println("User info: " + userInfo); System.out.println("Server address from MySQL: " + sessionData.serverAddress); @@ -75,4 +83,51 @@ public class AppTest assertTrue("Could not login", false); } } + + /** + * Test FTP connection + */ + public void testFtpConnection() { + FTPClient client = new FTPClient(); + String host = "localhost"; + int port = 2221; + String user = "admin"; + String password = "SI*HoZCC!]V)p>B2"; + String fileName = "/home/nils/file_to_upload.bin"; + + try { + client.connect(host, port); + System.out.println("Connected to " + host + ":" + port + ". Reply code: " + client.getReplyCode()); + if ( !FTPReply.isPositiveCompletion( client.getReplyCode() ) ) { + ConnectException ce = new ConnectException("No positive reply code."); + throw ce; + } + if ( !client.login(user, password) ) { + ConnectException ce = new ConnectException("Could not login."); + throw ce; + } + System.out.println("Logged in with user: " + user); + client.setFileType(FTP.BINARY_FILE_TYPE); + client.enterLocalPassiveMode(); + System.out.println("Entered PASSIVE MODE"); + InputStream input = new FileInputStream(fileName); + client.makeDirectory("myFolder"); + System.out.println("Made directory 'myFolder'"); + System.out.print("Starting file upload ... "); + client.storeFile("myFolder/myFile.txt", input); + System.out.println("done."); + client.noop(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (client.isConnected()) { + try { + client.logout(); + client.disconnect(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } } |