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