View Javadoc
1   /**
2    * Copyright 2009, Frederic Bregier, and individual contributors by the @author tags. See the
3    * COPYRIGHT.txt in the distribution for a full listing of individual contributors.
4    * 
5    * This is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
6    * General Public License as published by the Free Software Foundation; either version 3.0 of the
7    * License, or (at your option) any later version.
8    * 
9    * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10   * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11   * GNU Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public License along with this
14   * software; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
15   * Boston, MA 02110-1301 USA, or see the FSF site: http://www.fsf.org.
16   */
17  package org.waarp.gateway.ftp.file;
18  
19  import org.waarp.common.logging.WaarpLogger;
20  import org.waarp.common.logging.WaarpLoggerFactory;
21  import org.waarp.gateway.kernel.exec.AbstractExecutor.CommandExecutor;
22  
23  /**
24   * Simple Authentication based on a previously load XML file.
25   * 
26   * @author Frederic Bregier
27   * 
28   */
29  public class SimpleAuth {
30      /**
31       * Internal Logger
32       */
33      private static final WaarpLogger logger = WaarpLoggerFactory
34              .getLogger(SimpleAuth.class);
35  
36      /**
37       * User name
38       */
39      private String user = null;
40  
41      /**
42       * Password
43       */
44      private String password = null;
45  
46      /**
47       * Multiple accounts
48       */
49      private String[] accounts = null;
50  
51      /**
52       * Is the current user an administrator (which can shutdown or change bandwidth limitation)
53       */
54      private boolean isAdmin = false;
55      /**
56       * Specific Store command for this user
57       */
58      private String storCmd = null;
59      /**
60       * Specific Store command delay for this user
61       */
62      private long storDelay = 0;
63      /**
64       * Specific Retrieve command for this user
65       */
66      private String retrCmd = null;
67      /**
68       * Specific Retrieve command delay for this user
69       */
70      private long retrDelay = 0;
71  
72      private CommandExecutor commandExecutor = null;
73  
74      /**
75       * @param user
76       * @param password
77       * @param accounts
78       * @param storCmd
79       * @param storDelay
80       * @param retrCmd
81       * @param retrDelay
82       */
83      public SimpleAuth(String user, String password, String[] accounts,
84              String storCmd, long storDelay, String retrCmd, long retrDelay) {
85          this.setUser(user);
86          this.setPassword(password);
87          this.setAccounts(accounts);
88          this.setStorCmd(storCmd);
89          this.setStorDelay(storDelay);
90          this.setRetrCmd(retrCmd);
91          this.setRetrDelay(retrDelay);
92          this.setCommandExecutor(new CommandExecutor(retrCmd, retrDelay, storCmd, storDelay));
93          logger.info("Executor for " + user + " configured as [RETR: " +
94                  getCommandExecutor().getRetrType() + ":" + getCommandExecutor().pretrCMD + ":" + getCommandExecutor().pretrDelay + ":" +
95                  getCommandExecutor().pretrRefused +
96                  "] [STOR: " + getCommandExecutor().getStorType() + ":" + getCommandExecutor().pstorCMD + ":" +
97                  getCommandExecutor().pstorDelay + ":" + getCommandExecutor().pstorRefused + "]");
98      }
99  
100     /**
101      * Is the given password a valid one
102      * 
103      * @param newpassword
104      * @return True if the password is valid (or any password is valid)
105      */
106     public boolean isPasswordValid(String newpassword) {
107         if (getPassword() == null) {
108             return true;
109         }
110         if (newpassword == null) {
111             return false;
112         }
113         return getPassword().equals(newpassword);
114     }
115 
116     /**
117      * Is the given account a valid one
118      * 
119      * @param account
120      * @return True if the account is valid (or any account is valid)
121      */
122     public boolean isAccountValid(String account) {
123         if (getAccounts() == null) {
124             logger.debug("No account needed");
125             return true;
126         }
127         if (account == null) {
128             logger.debug("No account given");
129             return false;
130         }
131         for (String acct : getAccounts()) {
132             if (acct.equals(account)) {
133                 logger.debug("Account found");
134                 return true;
135             }
136         }
137         logger.debug("No account found");
138         return false;
139     }
140 
141     /**
142      * 
143      * @param isAdmin
144      *            True if the user should be an administrator
145      */
146     public void setAdmin(boolean isAdmin) {
147         this.isAdmin = isAdmin;
148     }
149 
150     /**
151      * @return the user
152      */
153     public String getUser() {
154         return user;
155     }
156 
157     /**
158      * @param user the user to set
159      */
160     private void setUser(String user) {
161         this.user = user;
162     }
163 
164     /**
165      * @return the password
166      */
167     public String getPassword() {
168         return password;
169     }
170 
171     /**
172      * @param password the password to set
173      */
174     private void setPassword(String password) {
175         this.password = password;
176     }
177 
178     /**
179      * @return the accounts
180      */
181     public String[] getAccounts() {
182         return accounts;
183     }
184 
185     /**
186      * @param accounts the accounts to set
187      */
188     private void setAccounts(String[] accounts) {
189         this.accounts = accounts;
190     }
191 
192     /**
193      * @return the isAdmin
194      */
195     public boolean isAdmin() {
196         return isAdmin;
197     }
198 
199     /**
200      * @return the storCmd
201      */
202     public String getStorCmd() {
203         return storCmd;
204     }
205 
206     /**
207      * @param storCmd the storCmd to set
208      */
209     private void setStorCmd(String storCmd) {
210         this.storCmd = storCmd;
211     }
212 
213     /**
214      * @return the storDelay
215      */
216     public long getStorDelay() {
217         return storDelay;
218     }
219 
220     /**
221      * @param storDelay the storDelay to set
222      */
223     private void setStorDelay(long storDelay) {
224         this.storDelay = storDelay;
225     }
226 
227     /**
228      * @return the retrCmd
229      */
230     public String getRetrCmd() {
231         return retrCmd;
232     }
233 
234     /**
235      * @param retrCmd the retrCmd to set
236      */
237     private void setRetrCmd(String retrCmd) {
238         this.retrCmd = retrCmd;
239     }
240 
241     /**
242      * @return the retrDelay
243      */
244     public long getRetrDelay() {
245         return retrDelay;
246     }
247 
248     /**
249      * @param retrDelay the retrDelay to set
250      */
251     private void setRetrDelay(long retrDelay) {
252         this.retrDelay = retrDelay;
253     }
254 
255     /**
256      * @return the commandExecutor
257      */
258     public CommandExecutor getCommandExecutor() {
259         return commandExecutor;
260     }
261 
262     /**
263      * @param commandExecutor the commandExecutor to set
264      */
265     private void setCommandExecutor(CommandExecutor commandExecutor) {
266         this.commandExecutor = commandExecutor;
267     }
268 }