summaryrefslogtreecommitdiff
path: root/src/SftpNegotiationCommand.cc
blob: 70d50a0c925a27f69548c5969158798cb1d4a810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* <!-- copyright */
/*
 * aria2 - The high speed download utility
 *
 * Copyright (C) 2015 Tatsuhiro Tsujikawa
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */
/* copyright --> */
#include "SftpNegotiationCommand.h"

#include <cassert>
#include <utility>

#include "Request.h"
#include "DownloadEngine.h"
#include "RequestGroup.h"
#include "PieceStorage.h"
#include "FileEntry.h"
#include "message.h"
#include "util.h"
#include "Option.h"
#include "Logger.h"
#include "LogFactory.h"
#include "Segment.h"
#include "DownloadContext.h"
#include "DefaultBtProgressInfoFile.h"
#include "RequestGroupMan.h"
#include "SocketCore.h"
#include "fmt.h"
#include "DiskAdaptor.h"
#include "SegmentMan.h"
#include "AuthConfigFactory.h"
#include "AuthConfig.h"
#include "a2functional.h"
#include "URISelector.h"
#include "CheckIntegrityEntry.h"
#include "NullProgressInfoFile.h"
#include "ChecksumCheckIntegrityEntry.h"
#include "SftpDownloadCommand.h"

namespace aria2 {

SftpNegotiationCommand::SftpNegotiationCommand(
    cuid_t cuid, const std::shared_ptr<Request>& req,
    const std::shared_ptr<FileEntry>& fileEntry, RequestGroup* requestGroup,
    DownloadEngine* e, const std::shared_ptr<SocketCore>& socket, Seq seq)
    : AbstractCommand(cuid, req, fileEntry, requestGroup, e, socket),
      sequence_(seq),
      authConfig_(e->getAuthConfigFactory()->createAuthConfig(
          req, requestGroup->getOption().get()))

{
  path_ = getPath();
  setWriteCheckSocket(getSocket());

  const std::string& checksum = getOption()->get(PREF_SSH_HOST_KEY_MD);
  if (!checksum.empty()) {
    auto p = util::divide(std::begin(checksum), std::end(checksum), '=');
    hashType_.assign(p.first.first, p.first.second);
    util::lowercase(hashType_);
    digest_ = util::fromHex(p.second.first, p.second.second);
  }
}

SftpNegotiationCommand::~SftpNegotiationCommand() {}

bool SftpNegotiationCommand::executeInternal()
{
  disableWriteCheckSocket();
  for (;;) {
    switch (sequence_) {
    case SEQ_HANDSHAKE:
      setReadCheckSocket(getSocket());
      if (!getSocket()->sshHandshake(hashType_, digest_)) {
        goto again;
      }
      A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - SSH handshake success", getCuid()));
      sequence_ = SEQ_AUTH_PASSWORD;
      break;
    case SEQ_AUTH_PASSWORD:
      if (!getSocket()->sshAuthPassword(authConfig_->getUser(),
                                        authConfig_->getPassword())) {
        goto again;
      }
      A2_LOG_DEBUG(
          fmt("CUID#%" PRId64 " - SSH authentication success", getCuid()));
      sequence_ = SEQ_SFTP_OPEN;
      break;
    case SEQ_SFTP_OPEN:
      if (!getSocket()->sshSFTPOpen(path_)) {
        goto again;
      }
      A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - SFTP file %s opened", getCuid(),
                       path_.c_str()));
      sequence_ = SEQ_SFTP_STAT;
      break;
    case SEQ_SFTP_STAT: {
      int64_t totalLength;
      time_t mtime;
      if (!getSocket()->sshSFTPStat(totalLength, mtime, path_)) {
        goto again;
      }
      Time t(mtime);
      A2_LOG_INFO(
          fmt("CUID#%" PRId64 " - SFTP File %s, size=%" PRId64 ", mtime=%s",
              getCuid(), path_.c_str(), totalLength, t.toHTTPDate().c_str()));
      if (!getPieceStorage()) {
        getRequestGroup()->updateLastModifiedTime(Time(mtime));
        onFileSizeDetermined(totalLength);
      }
      else {
        getRequestGroup()->validateTotalLength(getFileEntry()->getLength(),
                                               totalLength);
        sequence_ = SEQ_SFTP_SEEK;
      }
      break;
    }
    case SEQ_SFTP_SEEK: {
      sequence_ = SEQ_NEGOTIATION_COMPLETED;
      if (getSegments().empty()) {
        break;
      }

      auto& segment = getSegments().front();

      A2_LOG_INFO(fmt("CUID#%" PRId64 " - SFTP seek to %" PRId64, getCuid(),
                      segment->getPositionToWrite()));
      getSocket()->sshSFTPSeek(segment->getPositionToWrite());

      break;
    }
    case SEQ_FILE_PREPARATION:
      sequence_ = SEQ_SFTP_SEEK;
      disableReadCheckSocket();
      disableWriteCheckSocket();
      return false;
    case SEQ_NEGOTIATION_COMPLETED: {
      auto command = make_unique<SftpDownloadCommand>(
          getCuid(), getRequest(), getFileEntry(), getRequestGroup(),
          getDownloadEngine(), getSocket(), std::move(authConfig_));
      command->setStartupIdleTime(
          std::chrono::seconds(getOption()->getAsInt(PREF_STARTUP_IDLE_TIME)));
      command->setLowestDownloadSpeedLimit(
          getOption()->getAsInt(PREF_LOWEST_SPEED_LIMIT));
      command->setStatus(Command::STATUS_ONESHOT_REALTIME);

      getDownloadEngine()->setNoWait(true);

      if (getFileEntry()->isUniqueProtocol()) {
        getFileEntry()->removeURIWhoseHostnameIs(getRequest()->getHost());
      }
      getRequestGroup()->getURISelector()->tuneDownloadCommand(
          getFileEntry()->getRemainingUris(), command.get());
      getDownloadEngine()->addCommand(std::move(command));
      return true;
    }
    case SEQ_DOWNLOAD_ALREADY_COMPLETED:
    case SEQ_HEAD_OK:
    case SEQ_EXIT:
      return true;
    };
  }
again:
  addCommandSelf();
  if (getSocket()->wantWrite()) {
    setWriteCheckSocket(getSocket());
  }
  return false;
}

void SftpNegotiationCommand::onFileSizeDetermined(int64_t totalLength)
{
  getFileEntry()->setLength(totalLength);
  if (getFileEntry()->getPath().empty()) {
    auto suffixPath = util::createSafePath(
        util::percentDecode(std::begin(getRequest()->getFile()),
                            std::end(getRequest()->getFile())));

    getFileEntry()->setPath(
        util::applyDir(getOption()->get(PREF_DIR), suffixPath));
    getFileEntry()->setSuffixPath(suffixPath);
  }
  getRequestGroup()->preDownloadProcessing();

  if (totalLength == 0) {
    sequence_ = SEQ_NEGOTIATION_COMPLETED;

    if (getOption()->getAsBool(PREF_DRY_RUN)) {
      getRequestGroup()->initPieceStorage();
      onDryRunFileFound();
      return;
    }

    if (getDownloadContext()->knowsTotalLength() &&
        getRequestGroup()->downloadFinishedByFileLength()) {
      // TODO Known issue: if .aria2 file exists, it will not be
      // deleted on successful verification, because .aria2 file is
      // not loaded.  See also
      // HttpResponseCommand::handleOtherEncoding()
      getRequestGroup()->initPieceStorage();
      if (getDownloadContext()->isChecksumVerificationNeeded()) {
        A2_LOG_DEBUG("Zero length file exists. Verify checksum.");
        auto entry =
            make_unique<ChecksumCheckIntegrityEntry>(getRequestGroup());
        entry->initValidator();
        getPieceStorage()->getDiskAdaptor()->openExistingFile();
        getDownloadEngine()->getCheckIntegrityMan()->pushEntry(
            std::move(entry));
        sequence_ = SEQ_EXIT;
      }
      else {
        getPieceStorage()->markAllPiecesDone();
        getDownloadContext()->setChecksumVerified(true);
        sequence_ = SEQ_DOWNLOAD_ALREADY_COMPLETED;
        A2_LOG_NOTICE(fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
                          GroupId::toHex(getRequestGroup()->getGID()).c_str(),
                          getRequestGroup()->getFirstFilePath().c_str()));
      }
      poolConnection();
      return;
    }

    getRequestGroup()->adjustFilename(std::make_shared<NullProgressInfoFile>());
    getRequestGroup()->initPieceStorage();
    getPieceStorage()->getDiskAdaptor()->initAndOpenFile();

    if (getDownloadContext()->knowsTotalLength()) {
      A2_LOG_DEBUG("File length becomes zero and it means download completed.");
      // TODO Known issue: if .aria2 file exists, it will not be
      // deleted on successful verification, because .aria2 file is
      // not loaded.  See also
      // HttpResponseCommand::handleOtherEncoding()
      if (getDownloadContext()->isChecksumVerificationNeeded()) {
        A2_LOG_DEBUG("Verify checksum for zero-length file");
        auto entry =
            make_unique<ChecksumCheckIntegrityEntry>(getRequestGroup());
        entry->initValidator();
        getDownloadEngine()->getCheckIntegrityMan()->pushEntry(
            std::move(entry));
        sequence_ = SEQ_EXIT;
      }
      else {
        sequence_ = SEQ_DOWNLOAD_ALREADY_COMPLETED;
        getPieceStorage()->markAllPiecesDone();
      }
      poolConnection();
      return;
    }
    // We have to make sure that command that has Request object must
    // have segment after PieceStorage is initialized. See
    // AbstractCommand::execute()
    getSegmentMan()->getSegmentWithIndex(getCuid(), 0);
    return;
  }
  else {
    auto progressInfoFile = std::make_shared<DefaultBtProgressInfoFile>(
        getDownloadContext(), nullptr, getOption().get());
    getRequestGroup()->adjustFilename(progressInfoFile);
    getRequestGroup()->initPieceStorage();

    if (getOption()->getAsBool(PREF_DRY_RUN)) {
      onDryRunFileFound();
      return;
    }

    auto checkIntegrityEntry = getRequestGroup()->createCheckIntegrityEntry();
    if (!checkIntegrityEntry) {
      sequence_ = SEQ_DOWNLOAD_ALREADY_COMPLETED;
      poolConnection();
      return;
    }
    // We have to make sure that command that has Request object must
    // have segment after PieceStorage is initialized. See
    // AbstractCommand::execute()
    getSegmentMan()->getSegmentWithIndex(getCuid(), 0);

    checkIntegrityEntry->pushNextCommand(std::unique_ptr<Command>(this));
    prepareForNextAction(std::move(checkIntegrityEntry));

    disableReadCheckSocket();

    sequence_ = SEQ_FILE_PREPARATION;
  }
}

void SftpNegotiationCommand::poolConnection() const
{
  if (getOption()->getAsBool(PREF_FTP_REUSE_CONNECTION)) {
    // TODO we don't need options.  Probably, we need to pool socket
    // using scheme, port and auth info as key
    getDownloadEngine()->poolSocket(getRequest(), authConfig_->getUser(),
                                    createProxyRequest(), getSocket(), "");
  }
}

void SftpNegotiationCommand::onDryRunFileFound()
{
  getPieceStorage()->markAllPiecesDone();
  getDownloadContext()->setChecksumVerified(true);
  poolConnection();
  sequence_ = SEQ_HEAD_OK;
}

std::string SftpNegotiationCommand::getPath() const
{
  auto& req = getRequest();
  auto path = req->getDir() + req->getFile();
  return util::percentDecode(std::begin(path), std::end(path));
}

} // namespace aria2