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