001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.drftpd.commands;
019:
020: import net.sf.drftpd.ObjectNotFoundException;
021: import net.sf.drftpd.master.BaseFtpConnection;
022: import net.sf.drftpd.master.command.CommandManager;
023: import net.sf.drftpd.master.command.CommandManagerFactory;
024: import net.sf.drftpd.master.command.plugins.DataConnectionHandler;
025:
026: import org.apache.log4j.Logger;
027: import org.drftpd.remotefile.LinkedRemoteFileInterface;
028: import org.drftpd.remotefile.ListUtils;
029: import org.drftpd.remotefile.MLSTSerialize;
030: import org.drftpd.remotefile.RemoteFileInterface;
031:
032: import java.io.FileNotFoundException;
033: import java.io.IOException;
034: import java.io.OutputStreamWriter;
035: import java.io.PrintWriter;
036: import java.io.Writer;
037:
038: import java.net.Socket;
039:
040: import java.util.Iterator;
041: import java.util.List;
042:
043: /**
044: * @author mog
045: * @version $Id: MLST.java 1205 2005-06-29 19:06:34Z zubov $
046: */
047: public class MLST implements CommandHandler, CommandHandlerFactory {
048: private static final Logger logger = Logger.getLogger(MLST.class);
049:
050: public Reply execute(BaseFtpConnection conn)
051: throws UnhandledCommandException {
052: String command = conn.getRequest().getCommand();
053:
054: LinkedRemoteFileInterface dir = conn.getCurrentDirectory();
055:
056: if (conn.getRequest().hasArgument()) {
057: try {
058: dir = dir.lookupFile(conn.getRequest().getArgument());
059: } catch (FileNotFoundException e) {
060: return Reply.RESPONSE_550_REQUESTED_ACTION_NOT_TAKEN;
061: }
062: }
063:
064: if (!conn.getGlobalContext().getConnectionManager()
065: .getGlobalContext().getConfig().checkPathPermission(
066: "privpath", conn.getUserNull(), dir, true)) {
067: return Reply.RESPONSE_550_REQUESTED_ACTION_NOT_TAKEN;
068: }
069:
070: PrintWriter out = conn.getControlWriter();
071:
072: if ("MLST".equals(command)) {
073: out.print("250- Begin\r\n");
074: out.print(toMLST(dir) + "\r\n");
075: out.print("250 End.\r\n");
076:
077: return null;
078: } else if ("MLSD".equals(command)) {
079: DataConnectionHandler dataConnHnd;
080:
081: try {
082: dataConnHnd = (DataConnectionHandler) conn
083: .getCommandManager().getCommandHandler(
084: DataConnectionHandler.class);
085: } catch (ObjectNotFoundException e) {
086: return new Reply(500, e.getMessage());
087: }
088: if (!dataConnHnd.isPasv() && !dataConnHnd.isPort()) {
089: return Reply.RESPONSE_503_BAD_SEQUENCE_OF_COMMANDS;
090: }
091: out.print(Reply.RESPONSE_150_OK);
092: out.flush();
093:
094: try {
095: Socket sock = dataConnHnd.getDataSocket();
096: List files = ListUtils.list(dir, conn);
097: Writer os = new OutputStreamWriter(sock
098: .getOutputStream());
099:
100: for (Iterator iter = files.iterator(); iter.hasNext();) {
101: RemoteFileInterface file = (RemoteFileInterface) iter
102: .next();
103: os.write(toMLST(file) + "\r\n");
104: }
105:
106: os.close();
107: } catch (IOException e1) {
108: logger.warn("", e1);
109:
110: //425 Can't open data connection
111: return new Reply(425, e1.getMessage());
112: }
113:
114: return Reply.RESPONSE_226_CLOSING_DATA_CONNECTION;
115: }
116:
117: return Reply.RESPONSE_500_SYNTAX_ERROR;
118: }
119:
120: public String[] getFeatReplies() {
121: return new String[] { "MLST type*,x.crc32*,size*,modify*,unix.owner*,unix.group*,x.slaves*,x.xfertime*" };
122: }
123:
124: public CommandHandler initialize(BaseFtpConnection conn,
125: CommandManager initializer) {
126: return this ;
127: }
128:
129: public void load(CommandManagerFactory initializer) {
130: }
131:
132: private String toMLST(RemoteFileInterface file) {
133: String ret = MLSTSerialize.toMLST(file);
134:
135: //add perm=
136: //add
137: return ret;
138: }
139:
140: public void unload() {
141: }
142: }
|