1   /*
2    * $Id: InMemoryUserManager.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.ftp.server;
12  
13  import java.util.ArrayList;
14  import java.util.Collections;
15  import java.util.Enumeration;
16  import java.util.List;
17  
18  import org.apache.ftpserver.ftplet.Authentication;
19  import org.apache.ftpserver.ftplet.AuthenticationFailedException;
20  import org.apache.ftpserver.ftplet.Authority;
21  import org.apache.ftpserver.ftplet.User;
22  import org.apache.ftpserver.usermanager.AbstractUserManager;
23  import org.apache.ftpserver.usermanager.AnonymousAuthentication;
24  import org.apache.ftpserver.usermanager.BaseUser;
25  import org.apache.ftpserver.usermanager.ConcurrentLoginPermission;
26  import org.apache.ftpserver.usermanager.TransferRatePermission;
27  import org.apache.ftpserver.usermanager.UsernamePasswordAuthentication;
28  import org.apache.ftpserver.usermanager.WritePermission;
29  import org.apache.ftpserver.util.BaseProperties;
30  import org.apache.ftpserver.util.EncryptUtils;
31  
32  
33  /**
34   * This class is needed to avoid creating unnesessary configuration files while running ftp transport tests.
35   * Based on org.apache.ftpserver.usermanager.PropertiesUserManager
36   */
37  
38  public class InMemoryUserManager extends AbstractUserManager
39  {
40  
41      private static final String PREFIX = "ftpserver.user.";
42  
43      private BaseProperties userDataProp = new BaseProperties();
44      private boolean isPasswordEncrypt = true;
45  
46  
47      public InMemoryUserManager()
48      {
49          // this is just copied from default file ./res/user.gen
50          userDataProp.setProperty("ftpserver.user.admin.userpassword", "21232F297A57A5A743894A0E4A801FC3");
51          userDataProp.setProperty("ftpserver.user.admin.idletime", 0);
52          userDataProp.setProperty("ftpserver.user.anonymous.enableflag", true);
53          userDataProp.setProperty("ftpserver.user.anonymous.uploadrate", 4800);
54          userDataProp.setProperty("ftpserver.user.admin.writepermission", true);
55          userDataProp.setProperty("ftpserver.user.anonymous.userpassword", "D41D8CD98F00B204E9800998ECF8427E");
56          userDataProp.setProperty("ftpserver.user.anonymous.maxloginperip", 2);
57          userDataProp.setProperty("ftpserver.user.anonymous.idletime", 300);
58          userDataProp.setProperty("ftpserver.user.anonymous.homedirectory", "./res/home");
59          userDataProp.setProperty("ftpserver.user.admin.enableflag", true);
60          userDataProp.setProperty("ftpserver.user.anonymous.downloadrate", 4800);
61          userDataProp.setProperty("ftpserver.user.anonymous.maxloginnumber", 20);
62          userDataProp.setProperty("ftpserver.user.admin.homedirectory", "./res/home");
63          userDataProp.setProperty("ftpserver.user.anonymous.writepermission", false);
64  
65      }
66  
67      /**
68       * Load user data.
69       */
70      public User getUserByName(String userName)
71      {
72          if (!doesExist(userName))
73          {
74              return null;
75          }
76  
77          String baseKey = PREFIX + userName + '.';
78          BaseUser user = new BaseUser();
79          user.setName(userName);
80          user.setEnabled(userDataProp.getBoolean(baseKey + ATTR_ENABLE, true));
81          user.setHomeDirectory(userDataProp.getProperty(baseKey + ATTR_HOME, "/"));
82  
83          List authorities = new ArrayList();
84  
85          if (userDataProp.getBoolean(baseKey + ATTR_WRITE_PERM, false))
86          {
87              authorities.add(new WritePermission());
88          }
89  
90          int maxLogin = userDataProp.getInteger(baseKey + ATTR_MAX_LOGIN_NUMBER, 0);
91          int maxLoginPerIP = userDataProp.getInteger(baseKey + ATTR_MAX_LOGIN_PER_IP, 0);
92  
93          authorities.add(new ConcurrentLoginPermission(maxLogin, maxLoginPerIP));
94  
95          int uploadRate = userDataProp.getInteger(baseKey + ATTR_MAX_UPLOAD_RATE, 0);
96          int downloadRate = userDataProp.getInteger(baseKey + ATTR_MAX_DOWNLOAD_RATE, 0);
97  
98          authorities.add(new TransferRatePermission(downloadRate, uploadRate));
99  
100         user.setAuthorities((Authority[]) authorities.toArray(new Authority[authorities.size()]));
101 
102         user.setMaxIdleTime(userDataProp.getInteger(baseKey + ATTR_MAX_IDLE_TIME, 0));
103 
104         return user;
105     }
106 
107     /**
108      * Get all user names.
109      */
110     public String[] getAllUserNames()
111     {
112         // get all user names
113         String suffix = '.' + ATTR_HOME;
114         ArrayList ulst = new ArrayList();
115         Enumeration allKeys = userDataProp.propertyNames();
116         int prefixlen = PREFIX.length();
117         int suffixlen = suffix.length();
118         while (allKeys.hasMoreElements())
119         {
120             String key = (String) allKeys.nextElement();
121             if (key.endsWith(suffix))
122             {
123                 String name = key.substring(prefixlen);
124                 int endIndex = name.length() - suffixlen;
125                 name = name.substring(0, endIndex);
126                 ulst.add(name);
127             }
128         }
129 
130         Collections.sort(ulst);
131         return (String[]) ulst.toArray(new String[ulst.size()]);
132     }
133 
134     /**
135      * Delete an user. Removes all this user entries from the properties.
136      * After removing the corresponding from the properties, save the data.
137      */
138     public void delete(String login)
139     {
140         // we don't need this
141     }
142 
143     /**
144      * Save user data. Store the properties.
145      */
146     public void save(User user)
147     {
148         // we don't need this
149     }
150 
151     /**
152      * User existance check
153      */
154     public boolean doesExist(String name)
155     {
156         String key = PREFIX + name + '.' + ATTR_HOME;
157         return userDataProp.containsKey(key);
158     }
159 
160     /**
161      * User authenticate method
162      */
163     public synchronized User authenticate(Authentication authentication) throws AuthenticationFailedException
164     {
165         if (authentication instanceof UsernamePasswordAuthentication)
166         {
167             UsernamePasswordAuthentication upauth = (UsernamePasswordAuthentication) authentication;
168 
169             String user = upauth.getUsername();
170             String password = upauth.getPassword();
171 
172             if (user == null)
173             {
174                 throw new AuthenticationFailedException("Authentication failed");
175             }
176 
177             if (password == null)
178             {
179                 password = "";
180             }
181 
182             String passVal = userDataProp.getProperty(PREFIX + user + '.' + ATTR_PASSWORD);
183             if (isPasswordEncrypt)
184             {
185                 password = EncryptUtils.encryptMD5(password);
186             }
187             if (password.equals(passVal))
188             {
189                 return getUserByName(user);
190             } else
191             {
192                 throw new AuthenticationFailedException("Authentication failed");
193             }
194 
195         } else if (authentication instanceof AnonymousAuthentication)
196         {
197             if (doesExist("anonymous"))
198             {
199                 return getUserByName("anonymous");
200             } else
201             {
202                 throw new AuthenticationFailedException("Authentication failed");
203             }
204         } else
205         {
206             throw new IllegalArgumentException("Authentication not supported by this user manager");
207         }
208     }
209 }