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  package org.waarp.gateway.ftp.config;
21  
22  import org.waarp.common.command.ReplyCode;
23  import org.waarp.common.command.exception.CommandAbstractException;
24  import org.waarp.common.command.exception.Reply500Exception;
25  import org.waarp.common.command.exception.Reply501Exception;
26  import org.waarp.common.logging.WaarpLogger;
27  import org.waarp.common.logging.WaarpLoggerFactory;
28  import org.waarp.ftp.core.command.AbstractCommand;
29  
30  import java.io.File;
31  
32  /**
33   * AUTHTUPDATE command: implements the command that will try to update the
34   * authentications from the file given
35   * as argument or the original one if no argument is given.<br>
36   * Two optional arguments exist:<br>
37   * - PURGE: empty first the current authentications before applying the
38   * update<br>
39   * - SAVE: save the final authentications on the original name given at
40   * startup.<br>
41   */
42  public class AUTHUPDATE extends AbstractCommand {
43    /**
44     * Internal Logger
45     */
46    private static final WaarpLogger logger =
47        WaarpLoggerFactory.getLogger(AUTHUPDATE.class);
48  
49    @Override
50    public final void exec() throws CommandAbstractException {
51      if (!getSession().getAuth().isAdmin()) {
52        // not admin
53        throw new Reply500Exception("Command Not Allowed");
54      }
55      String filename = null;
56      boolean purge = false;
57      boolean write = false;
58      if (!hasArg()) {
59        filename =
60            ((FileBasedConfiguration) getConfiguration()).getAuthenticationFile();
61      } else {
62        final String[] authents = getArgs();
63        for (final String authent : authents) {
64          if ("PURGE".equalsIgnoreCase(authent)) {
65            purge = true;
66          } else if ("SAVE".equalsIgnoreCase(authent)) {
67            write = true;
68          } else if (filename == null) {
69            filename = authent;
70          }
71        }
72        if (filename == null) {
73          filename =
74              ((FileBasedConfiguration) getConfiguration()).getAuthenticationFile();
75        }
76        final File file = new File(filename);
77        if (!file.canRead()) {
78          throw new Reply501Exception(
79              "Filename given as parameter is not found: " + filename);
80        }
81      }
82      if (!((FileBasedConfiguration) getConfiguration()).initializeAuthent(
83          filename, purge)) {
84        throw new Reply501Exception("Filename given as parameter is not correct");
85      }
86      if (write &&
87          !((FileBasedConfiguration) getConfiguration()).saveAuthenticationFile(
88              ((FileBasedConfiguration) getConfiguration()).getAuthenticationFile())) {
89        throw new Reply501Exception(
90            "Update is done but Write operation is not correct");
91      }
92      logger.warn("Authentication was updated from " + filename);
93      getSession().setReplyCode(ReplyCode.REPLY_200_COMMAND_OKAY,
94                                "Authentication is updated");
95    }
96  
97  }