Skip to content
Snippets Groups Projects
ssh.c 68.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • 
        case SSH_SFTP_QUOTE_MKDIR:
          rc = libssh2_sftp_mkdir(sftp_scp->sftp_session, sshc->quote_path1, 0755);
          if (rc == LIBSSH2_ERROR_EAGAIN) {
            break;
          }
          else if (rc != 0) {
            err = libssh2_sftp_last_error(sftp_scp->sftp_session);
            Curl_safefree(sshc->quote_path1);
            sshc->quote_path1 = NULL;
            failf(data, "mkdir command failed: %s", sftp_libssh2_strerror(err));
            state(conn, SSH_SFTP_CLOSE);
    
            sshc->actualCode = CURLE_QUOTE_ERROR;
    
            break;
          }
          state(conn, SSH_SFTP_NEXT_QUOTE);
          break;
    
        case SSH_SFTP_QUOTE_RENAME:
          rc = libssh2_sftp_rename(sftp_scp->sftp_session, sshc->quote_path1,
                                   sshc->quote_path2);
          if (rc == LIBSSH2_ERROR_EAGAIN) {
            break;
          } else if (rc != 0) {
            err = libssh2_sftp_last_error(sftp_scp->sftp_session);
            Curl_safefree(sshc->quote_path1);
            sshc->quote_path1 = NULL;
            Curl_safefree(sshc->quote_path2);
            sshc->quote_path2 = NULL;
            failf(data, "rename command failed: %s", sftp_libssh2_strerror(err));
            state(conn, SSH_SFTP_CLOSE);
    
            sshc->actualCode = CURLE_QUOTE_ERROR;
    
            break;
          }
          state(conn, SSH_SFTP_NEXT_QUOTE);
          break;
    
        case SSH_SFTP_QUOTE_RMDIR:
          rc = libssh2_sftp_rmdir(sftp_scp->sftp_session, sshc->quote_path1);
          if (rc == LIBSSH2_ERROR_EAGAIN) {
            break;
          }
          else if (rc != 0) {
            err = libssh2_sftp_last_error(sftp_scp->sftp_session);
            Curl_safefree(sshc->quote_path1);
            sshc->quote_path1 = NULL;
            failf(data, "rmdir command failed: %s", sftp_libssh2_strerror(err));
            state(conn, SSH_SFTP_CLOSE);
    
            sshc->actualCode = CURLE_QUOTE_ERROR;
    
            break;
          }
          state(conn, SSH_SFTP_NEXT_QUOTE);
          break;
    
        case SSH_SFTP_QUOTE_UNLINK:
          rc = libssh2_sftp_unlink(sftp_scp->sftp_session, sshc->quote_path1);
          if (rc == LIBSSH2_ERROR_EAGAIN) {
            break;
          }
          else if (rc != 0) {
            err = libssh2_sftp_last_error(sftp_scp->sftp_session);
            Curl_safefree(sshc->quote_path1);
            sshc->quote_path1 = NULL;
            failf(data, "rm command failed: %s", sftp_libssh2_strerror(err));
            state(conn, SSH_SFTP_CLOSE);
    
            sshc->actualCode = CURLE_QUOTE_ERROR;
    
            break;
          }
          state(conn, SSH_SFTP_NEXT_QUOTE);
          break;
    
        case SSH_SFTP_TRANS_INIT:
          if (data->set.upload) {
            state(conn, SSH_SFTP_UPLOAD_INIT);
            break;
          } else {
            if (sftp_scp->path[strlen(sftp_scp->path)-1] == '/') {
              state(conn, SSH_SFTP_READDIR_INIT);
              break;
            } else {
              state(conn, SSH_SFTP_DOWNLOAD_INIT);
              break;
            }
          }
          break;
    
        case SSH_SFTP_UPLOAD_INIT:
          /*
           * NOTE!!!  libssh2 requires that the destination path is a full path
           *          that includes the destination file and name OR ends in a "/"
           *          If this is not done the destination file will be named the
           *          same name as the last directory in the path.
           */
          sftp_scp->sftp_handle =
    
            libssh2_sftp_open(sftp_scp->sftp_session, sftp_scp->path,
                              LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
                              data->set.new_file_perms);
    
          if (!sftp_scp->sftp_handle) {
            if (libssh2_session_last_errno(sftp_scp->ssh_session) ==
                LIBSSH2_ERROR_EAGAIN) {
              break;
            } else {
              err = libssh2_sftp_last_error(sftp_scp->sftp_session);
    
              failf(data, "Upload failed: %s", sftp_libssh2_strerror(err));
    
    1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
              if (sshc->secondCreateDirs) {
                state(conn, SSH_SFTP_CLOSE);
                sshc->actualCode = err;
                break;
              }
              else if (((err == LIBSSH2_FX_NO_SUCH_FILE) ||
                        (err == LIBSSH2_FX_FAILURE) ||
                        (err == LIBSSH2_FX_NO_SUCH_PATH)) &&
                       (data->set.ftp_create_missing_dirs &&
                        (strlen(sftp_scp->path) > 1))) {
                /* try to create the path remotely */
                sshc->secondCreateDirs = 1;
                state(conn, SSH_SFTP_CREATE_DIRS_INIT);
                break;
              }
              state(conn, SSH_SFTP_CLOSE);
              sshc->actualCode = sftp_libssh2_error_to_CURLE(err);
              break;
            }
          }
    
          /* upload data */
          result = Curl_setup_transfer(conn, -1, -1, FALSE, NULL,
                                       FIRSTSOCKET, NULL);
    
          if (result) {
            state(conn, SSH_SFTP_CLOSE);
            sshc->actualCode = result;
          } else {
            state(conn, SSH_STOP);
          }
          break;
    
        case SSH_SFTP_CREATE_DIRS_INIT:
          if (strlen(sftp_scp->path) > 1) {
            sshc->slash_pos = sftp_scp->path + 1; /* ignore the leading '/' */
            state(conn, SSH_SFTP_CREATE_DIRS);
          } else {
            state(conn, SSH_SFTP_UPLOAD_INIT);
          }
          break;
    
        case SSH_SFTP_CREATE_DIRS:
          if ((sshc->slash_pos = strchr(sshc->slash_pos, '/')) != NULL) {
            *sshc->slash_pos = 0;
    
            infof(data, "Creating directory '%s'\n", sftp_scp->path);
            state(conn, SSH_SFTP_CREATE_DIRS_MKDIR);
            break;
          } else {
            state(conn, SSH_SFTP_UPLOAD_INIT);
          }
          break;
    
        case SSH_SFTP_CREATE_DIRS_MKDIR:
          /* 'mode' - parameter is preliminary - default to 0644 */
          rc = libssh2_sftp_mkdir(sftp_scp->sftp_session, sftp_scp->path,
                                  data->set.new_directory_perms);
          if (rc == LIBSSH2_ERROR_EAGAIN) {
            break;
          }
          *sshc->slash_pos = '/';
          ++sshc->slash_pos;
          if (rc == -1) {
            unsigned int sftp_err = 0;
            /*
             * abort if failure wasn't that the dir already exists or the
             * permission was denied (creation might succeed further
             * down the path) - retry on unspecific FAILURE also
             */
            sftp_err = libssh2_sftp_last_error(sftp_scp->sftp_session);
            if ((sftp_err != LIBSSH2_FX_FILE_ALREADY_EXISTS) &&
                (sftp_err != LIBSSH2_FX_FAILURE) &&
                (sftp_err != LIBSSH2_FX_PERMISSION_DENIED)) {
              result = sftp_libssh2_error_to_CURLE(sftp_err);
              state(conn, SSH_SFTP_CLOSE);
              sshc->actualCode = result;
              break;
            }
          }
          state(conn, SSH_SFTP_CREATE_DIRS);
          break;
    
        case SSH_SFTP_READDIR_INIT:
          /*
           * This is a directory that we are trying to get, so produce a
           * directory listing
           */
          sftp_scp->sftp_handle = libssh2_sftp_opendir(sftp_scp->sftp_session,
                                                       sftp_scp->path);
          if (!sftp_scp->sftp_handle) {
            if (libssh2_session_last_errno(sftp_scp->ssh_session) ==
                LIBSSH2_ERROR_EAGAIN) {
              break;
            } else {
              err = libssh2_sftp_last_error(sftp_scp->sftp_session);
              failf(data, "Could not open directory for reading: %s",
                    sftp_libssh2_strerror(err));
              state(conn, SSH_SFTP_CLOSE);
              sshc->actualCode = sftp_libssh2_error_to_CURLE(err);
              break;
            }
          }
          if ((sshc->readdir_filename = (char *)malloc(PATH_MAX+1)) == NULL) {
            state(conn, SSH_SFTP_CLOSE);
            sshc->actualCode = CURLE_OUT_OF_MEMORY;
            break;
          }
          if ((sshc->readdir_longentry = (char *)malloc(PATH_MAX+1)) == NULL) {
            Curl_safefree(sshc->readdir_filename);
            sshc->readdir_filename = NULL;
            state(conn, SSH_SFTP_CLOSE);
            sshc->actualCode = CURLE_OUT_OF_MEMORY;
            break;
          }
          state(conn, SSH_SFTP_READDIR);
          break;
    
        case SSH_SFTP_READDIR:
          sshc->readdir_len = libssh2_sftp_readdir_ex(sftp_scp->sftp_handle,
                                                      sshc->readdir_filename,
                                                      PATH_MAX,
                                                      sshc->readdir_longentry,
                                                      PATH_MAX,
                                                      &sshc->readdir_attrs);
          if (sshc->readdir_len == LIBSSH2_ERROR_EAGAIN) {
            break;
          }
          if (sshc->readdir_len > 0) {
            sshc->readdir_filename[sshc->readdir_len] = '\0';
    
            if (data->set.ftp_list_only) {
              char *tmpLine;
    
              tmpLine = aprintf("%s\n", sshc->readdir_filename);
              if (tmpLine == NULL) {
                state(conn, SSH_SFTP_CLOSE);
                sshc->actualCode = CURLE_OUT_OF_MEMORY;
                break;
              }
              result = Curl_client_write(conn, CLIENTWRITE_BODY, tmpLine, 0);
              Curl_safefree(tmpLine);
    
              /* output debug output if that is requested */
              if (data->set.verbose) {
                Curl_debug(data, CURLINFO_DATA_OUT, sshc->readdir_filename,
                           sshc->readdir_len, conn);
              }
            } else {
              sshc->readdir_currLen = strlen(sshc->readdir_longentry);
              sshc->readdir_totalLen = 80 + sshc->readdir_currLen;
              sshc->readdir_line = (char *)calloc(sshc->readdir_totalLen, 1);
              if (!sshc->readdir_line) {
                Curl_safefree(sshc->readdir_filename);
                sshc->readdir_filename = NULL;
                Curl_safefree(sshc->readdir_longentry);
                sshc->readdir_longentry = NULL;
                state(conn, SSH_SFTP_CLOSE);
                sshc->actualCode = CURLE_OUT_OF_MEMORY;
                break;
              }
    
              memcpy(sshc->readdir_line, sshc->readdir_longentry,
                     sshc->readdir_currLen);
              if ((sshc->readdir_attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) &&
                  ((sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
                   LIBSSH2_SFTP_S_IFLNK)) {
                sshc->readdir_linkPath = (char *)malloc(PATH_MAX + 1);
                if (sshc->readdir_linkPath == NULL) {
                  Curl_safefree(sshc->readdir_filename);
                  sshc->readdir_filename = NULL;
                  Curl_safefree(sshc->readdir_longentry);
                  sshc->readdir_longentry = NULL;
                  state(conn, SSH_SFTP_CLOSE);
                  sshc->actualCode = CURLE_OUT_OF_MEMORY;
                  break;
                }
    
                snprintf(sshc->readdir_linkPath, PATH_MAX, "%s%s", sftp_scp->path,
                         sshc->readdir_filename);
                state(conn, SSH_SFTP_READDIR_LINK);
                break;
              }
              state(conn, SSH_SFTP_READDIR_BOTTOM);
              break;
            }
          }
          else if (sshc->readdir_len == 0) {
            Curl_safefree(sshc->readdir_filename);
            sshc->readdir_filename = NULL;
            Curl_safefree(sshc->readdir_longentry);
            sshc->readdir_longentry = NULL;
            state(conn, SSH_SFTP_READDIR_DONE);
            break;
          }
          else if (sshc->readdir_len <= 0) {
            err = libssh2_sftp_last_error(sftp_scp->sftp_session);
            sshc->actualCode = err;
            failf(data, "Could not open remote file for reading: %s :: %d",
                  sftp_libssh2_strerror(err),
                  libssh2_session_last_errno(sftp_scp->ssh_session));
            Curl_safefree(sshc->readdir_filename);
            sshc->readdir_filename = NULL;
            Curl_safefree(sshc->readdir_longentry);
            sshc->readdir_longentry = NULL;
            state(conn, SSH_SFTP_CLOSE);
            break;
          }
          break;
    
        case SSH_SFTP_READDIR_LINK:
          sshc->readdir_len = libssh2_sftp_readlink(sftp_scp->sftp_session,
                                                    sshc->readdir_linkPath,
                                                    sshc->readdir_filename,
                                                    PATH_MAX);
          if (sshc->readdir_len == LIBSSH2_ERROR_EAGAIN) {
            break;
          }
          Curl_safefree(sshc->readdir_linkPath);
          sshc->readdir_linkPath = NULL;
          sshc->readdir_line = realloc(sshc->readdir_line,
                                       sshc->readdir_totalLen + 4 +
                                       sshc->readdir_len);
          if (!sshc->readdir_line) {
            Curl_safefree(sshc->readdir_filename);
            sshc->readdir_filename = NULL;
            Curl_safefree(sshc->readdir_longentry);
            sshc->readdir_longentry = NULL;
            state(conn, SSH_SFTP_CLOSE);
            sshc->actualCode = CURLE_OUT_OF_MEMORY;
            break;
          }
    
          sshc->readdir_currLen += snprintf(sshc->readdir_line +
                                            sshc->readdir_currLen,
                                            sshc->readdir_totalLen -
                                            sshc->readdir_currLen,
                                            " -> %s",
                                            sshc->readdir_filename);
    
          state(conn, SSH_SFTP_READDIR_BOTTOM);
          break;
    
        case SSH_SFTP_READDIR_BOTTOM:
          sshc->readdir_currLen += snprintf(sshc->readdir_line +
                                            sshc->readdir_currLen,
                                            sshc->readdir_totalLen -
                                            sshc->readdir_currLen, "\n");
          result = Curl_client_write(conn, CLIENTWRITE_BODY,
                                     sshc->readdir_line, 0);
    
          /* output debug output if that is requested */
          if (data->set.verbose) {
            Curl_debug(data, CURLINFO_DATA_OUT, sshc->readdir_line,
                       sshc->readdir_currLen, conn);
          }
          Curl_safefree(sshc->readdir_line);
          sshc->readdir_line = NULL;
          state(conn, SSH_SFTP_READDIR);
          break;
    
        case SSH_SFTP_READDIR_DONE:
          if (libssh2_sftp_closedir(sftp_scp->sftp_handle) ==
              LIBSSH2_ERROR_EAGAIN) {
            break;
          }
          sftp_scp->sftp_handle = NULL;
          Curl_safefree(sshc->readdir_filename);
          sshc->readdir_filename = NULL;
          Curl_safefree(sshc->readdir_longentry);
          sshc->readdir_longentry = NULL;
    
          /* no data to transfer */
          result = Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
          state(conn, SSH_STOP);
          break;
    
        case SSH_SFTP_DOWNLOAD_INIT:
          /*
           * Work on getting the specified file
           */
          sftp_scp->sftp_handle =
            libssh2_sftp_open(sftp_scp->sftp_session, sftp_scp->path,
                              LIBSSH2_FXF_READ, data->set.new_file_perms);
          if (!sftp_scp->sftp_handle) {
            if (libssh2_session_last_errno(sftp_scp->ssh_session) ==
                 LIBSSH2_ERROR_EAGAIN) {
              break;
            } else {
              err = libssh2_sftp_last_error(sftp_scp->sftp_session);
              failf(data, "Could not open remote file for reading: %s",
                    sftp_libssh2_strerror(err));
              state(conn, SSH_SFTP_CLOSE);
              sshc->actualCode = sftp_libssh2_error_to_CURLE(err);
              break;
            }
          }
          state(conn, SSH_SFTP_DOWNLOAD_STAT);
          break;
    
        case SSH_SFTP_DOWNLOAD_STAT:
          {
            LIBSSH2_SFTP_ATTRIBUTES attrs;
    
            rc = libssh2_sftp_stat(sftp_scp->sftp_session, sftp_scp->path, &attrs);
            if (rc == LIBSSH2_ERROR_EAGAIN) {
              break;
            }
            else if (rc) {
              /*
               * libssh2_sftp_open() didn't return an error, so maybe the server
               * just doesn't support stat()
               */
              data->reqdata.size = -1;
              data->reqdata.maxdownload = -1;
            } else {
              data->reqdata.size = attrs.filesize;
              data->reqdata.maxdownload = attrs.filesize;
              Curl_pgrsSetDownloadSize(data, attrs.filesize);
            }
          }
    
          /* Setup the actual download */
          result = Curl_setup_transfer(conn, FIRSTSOCKET, data->reqdata.size,
                                       FALSE, NULL, -1, NULL);
          if (result) {
            state(conn, SSH_SFTP_CLOSE);
            sshc->actualCode = result;
          } else {
            state(conn, SSH_STOP);
          }
          break;
    
        case SSH_SFTP_CLOSE:
          if (sftp_scp->sftp_handle) {
            rc = libssh2_sftp_close(sftp_scp->sftp_handle);
            if (rc == LIBSSH2_ERROR_EAGAIN) {
              break;
            }
            else if (rc < 0) {
              infof(data, "Failed to close libssh2 file\n");
            }
            sftp_scp->sftp_handle = NULL;
          }
          state(conn, SSH_SFTP_SHUTDOWN);
          break;
    
        case SSH_SFTP_SHUTDOWN:
          if (sftp_scp->sftp_session) {
            rc = libssh2_sftp_shutdown(sftp_scp->sftp_session);
            if (rc == LIBSSH2_ERROR_EAGAIN) {
              break;
            }
            else if (rc < 0) {
              infof(data, "Failed to stop libssh2 sftp subsystem\n");
            }
            sftp_scp->sftp_session = NULL;
          }
    
          Curl_safefree(sftp_scp->path);
          sftp_scp->path = NULL;
    
          Curl_safefree(sftp_scp->homedir);
          sftp_scp->homedir = NULL;
    
          state(conn, SSH_CHANNEL_CLOSE);
          break;
    
        case SSH_SCP_TRANS_INIT:
          if (data->set.upload) {
            if (data->set.infilesize < 0) {
              failf(data, "SCP requires a known file size for upload");
              sshc->actualCode = CURLE_UPLOAD_FAILED;
              state(conn, SSH_SCP_CHANNEL_FREE);
              break;
            }
            state(conn, SSH_SCP_UPLOAD_INIT);
          } else {
            state(conn, SSH_SCP_DOWNLOAD_INIT);
          }
          break;
    
        case SSH_SCP_UPLOAD_INIT:
          /*
           * libssh2 requires that the destination path is a full path that
           * includes the destination file and name OR ends in a "/" .  If this is
           * not done the destination file will be named the same name as the last
           * directory in the path.
           */
          sftp_scp->ssh_channel =
                      libssh2_scp_send_ex(sftp_scp->ssh_session, sftp_scp->path,
                                          data->set.new_file_perms,
                                          data->set.infilesize, 0, 0);
          if (!sftp_scp->ssh_channel) {
            if (libssh2_session_last_errno(sftp_scp->ssh_session) ==
                LIBSSH2_ERROR_EAGAIN) {
              break;
            } else {
              int ssh_err;
              char *err_msg;
    
              ssh_err = libssh2_session_last_error(sftp_scp->ssh_session,
                                                   &err_msg, NULL, 0);
              err = libssh2_session_error_to_CURLE(ssh_err);
              failf(conn->data, "%s", err_msg);
              state(conn, SSH_SCP_CHANNEL_FREE);
              sshc->actualCode = err;
              break;
            }
          }
    
          /* upload data */
          result = Curl_setup_transfer(conn, -1, data->reqdata.size, FALSE, NULL,
                                       FIRSTSOCKET, NULL);
    
          if (result) {
            state(conn, SSH_SCP_CHANNEL_FREE);
            sshc->actualCode = result;
          } else {
            state(conn, SSH_STOP);
          }
          break;
    
        case SSH_SCP_DOWNLOAD_INIT:
          {
            /*
             * We must check the remote file; if it is a directory no values will
             * be set in sb
             */
            struct stat sb;
            curl_off_t bytecount;
    
            memset(&sb, 0, sizeof(struct stat));
            sftp_scp->ssh_channel = libssh2_scp_recv(sftp_scp->ssh_session,
                                                     sftp_scp->path, &sb);
            if (!sftp_scp->ssh_channel) {
              if (libssh2_session_last_errno(sftp_scp->ssh_session) ==
                 LIBSSH2_ERROR_EAGAIN) {
                break;
              } else {
                int ssh_err;
                char *err_msg;
    
                ssh_err = libssh2_session_last_error(sftp_scp->ssh_session,
                                                     &err_msg, NULL, 0);
                err = libssh2_session_error_to_CURLE(ssh_err);
                failf(conn->data, "%s", err_msg);
                state(conn, SSH_SCP_CHANNEL_FREE);
                sshc->actualCode = err;
                break;
              }
            }
    
            /* download data */
            bytecount = (curl_off_t)sb.st_size;
            data->reqdata.maxdownload =  (curl_off_t)sb.st_size;
            result = Curl_setup_transfer(conn, FIRSTSOCKET,
                                         bytecount, FALSE, NULL, -1, NULL);
    
            if (result) {
              state(conn, SSH_SCP_CHANNEL_FREE);
              sshc->actualCode = result;
            } else {
              state(conn, SSH_STOP);
            }
          }
          break;
    
        case SSH_SCP_DONE:
          if (data->set.upload) {
            state(conn, SSH_SCP_SEND_EOF);
          } else {
            state(conn, SSH_SCP_CHANNEL_FREE);
          }
          break;
    
        case SSH_SCP_SEND_EOF:
          if (sftp_scp->ssh_channel) {
            rc = libssh2_channel_send_eof(sftp_scp->ssh_channel);
            if (rc == LIBSSH2_ERROR_EAGAIN) {
              break;
            }
            else if (rc) {
              infof(data, "Failed to send libssh2 channel EOF\n");
            }
          }
          state(conn, SSH_SCP_WAIT_EOF);
          break;
    
        case SSH_SCP_WAIT_EOF:
          if (sftp_scp->ssh_channel) {
            rc = libssh2_channel_wait_eof(sftp_scp->ssh_channel);
            if (rc == LIBSSH2_ERROR_EAGAIN) {
              break;
            }
            else if (rc) {
              infof(data, "Failed to get channel EOF\n");
            }
          }
          state(conn, SSH_SCP_WAIT_CLOSE);
          break;
    
        case SSH_SCP_WAIT_CLOSE:
          if (sftp_scp->ssh_channel) {
            rc = libssh2_channel_wait_closed(sftp_scp->ssh_channel);
            if (rc == LIBSSH2_ERROR_EAGAIN) {
              break;
            }
            else if (rc) {
              infof(data, "Channel failed to close\n");
            }
          }
          state(conn, SSH_SCP_CHANNEL_FREE);
          break;
    
        case SSH_SCP_CHANNEL_FREE:
          if (sftp_scp->ssh_channel) {
            rc = libssh2_channel_free(sftp_scp->ssh_channel);
            if (rc == LIBSSH2_ERROR_EAGAIN) {
              break;
            }
            else if (rc < 0) {
              infof(data, "Failed to free libssh2 scp subsystem\n");
            }
            sftp_scp->ssh_channel = NULL;
          }
          state(conn, SSH_SESSION_DISCONECT);
          break;
    
        case SSH_CHANNEL_CLOSE:
          if (sftp_scp->ssh_channel) {
            rc = libssh2_channel_close(sftp_scp->ssh_channel);
            if (rc == LIBSSH2_ERROR_EAGAIN) {
              break;
            }
            else if (rc < 0) {
              infof(data, "Failed to stop libssh2 channel subsystem\n");
            }
            sftp_scp->ssh_channel = NULL;
          }
          state(conn, SSH_SESSION_DISCONECT);
          break;
    
        case SSH_SESSION_DISCONECT:
          if (sftp_scp->ssh_session) {
            rc = libssh2_session_disconnect(sftp_scp->ssh_session, "Shutdown");
            if (rc == LIBSSH2_ERROR_EAGAIN) {
              break;
            }
            else if (rc < 0) {
              infof(data, "Failed to disconnect libssh2 session\n");
            }
          }
    
          Curl_safefree(sftp_scp->path);
          sftp_scp->path = NULL;
    
          Curl_safefree(sftp_scp->homedir);
          sftp_scp->homedir = NULL;
    
          state(conn, SSH_SESSION_FREE);
          break;
    
        case SSH_SESSION_FREE:
          if (sftp_scp->ssh_session) {
            rc = libssh2_session_free(sftp_scp->ssh_session);
            if (rc == LIBSSH2_ERROR_EAGAIN) {
              break;
            }
            else if (rc < 0) {
              infof(data, "Failed to free libssh2 session\n");
            }
            sftp_scp->ssh_session = NULL;
          }
          sshc->nextState = SSH_NO_STATE;
    
          state(conn, SSH_STOP);
          result = sshc->actualCode;
          break;
    
        case SSH_QUIT:
          /* fallthrough, just stop! */
        default:
          /* internal error */
    
          state(conn, SSH_STOP);
          break;
      }
    
      return result;
    }
    
    /* called repeatedly until done from multi.c */
    CURLcode Curl_ssh_multi_statemach(struct connectdata *conn,
                                      bool *done)
    {
      struct ssh_conn *sshc = &conn->proto.sshc;
      CURLcode result = CURLE_OK;
    
    
      result = ssh_statemach_act(conn);
      *done = (bool)(sshc->state == SSH_STOP);
    
    
      return result;
    }
    
    static CURLcode ssh_easy_statemach(struct connectdata *conn)
    {
      struct ssh_conn *sshc = &conn->proto.sshc;
      CURLcode result = CURLE_OK;
    
      while (sshc->state != SSH_STOP) {
        result = ssh_statemach_act(conn);
        if (result) {
    
    static CURLcode ssh_init(struct connectdata *conn)
    
    {
      struct SessionHandle *data = conn->data;
    
      struct SSHPROTO *ssh;
      if (data->reqdata.proto.ssh)
    
      ssh = (struct SSHPROTO *)calloc(sizeof(struct SSHPROTO), 1);
      if (!ssh)
    
        return CURLE_OUT_OF_MEMORY;
    
    
      data->reqdata.proto.ssh = ssh;
    
      /* get some initial data into the ssh struct */
      ssh->bytecountp = &data->reqdata.keep.bytecount;
    
    
      /* no need to duplicate them, this connectdata struct won't change */
    
      ssh->user = conn->user;
      ssh->passwd = conn->passwd;
    
      ssh->errorstr = NULL;
    
      ssh->ssh_session = NULL;
      ssh->ssh_channel = NULL;
      ssh->sftp_session = NULL;
      ssh->sftp_handle = NULL;
    
     * Curl_ssh_connect() gets called from Curl_protocol_connect() to allow us to
    
     * do protocol-specific actions at connect-time.
     */
    
    CURLcode Curl_ssh_connect(struct connectdata *conn, bool *done)
    
      curl_socket_t sock;
      CURLcode result;
      struct SessionHandle *data = conn->data;
    
    
      ssh = data->reqdata.proto.ssh;
    
    
    #ifdef CURL_LIBSSH2_DEBUG
    
      if (ssh->user) {
        infof(data, "User: %s\n", ssh->user);
    
      if (ssh->passwd) {
        infof(data, "Password: %s\n", ssh->passwd);
    
      }
    #endif /* CURL_LIBSSH2_DEBUG */
      sock = conn->sock[FIRSTSOCKET];
    
      ssh->ssh_session = libssh2_session_init_ex(libssh2_malloc, libssh2_free,
    
      if (ssh->ssh_session == NULL) {
    
        failf(data, "Failure initialising ssh session");
    
        return CURLE_FAILED_INIT;
      }
    
    #ifdef CURL_LIBSSH2_DEBUG
    
      libssh2_trace(ssh->ssh_session, LIBSSH2_TRACE_CONN|LIBSSH2_TRACE_TRANS|
                    LIBSSH2_TRACE_KEX|LIBSSH2_TRACE_AUTH|LIBSSH2_TRACE_SCP|
                    LIBSSH2_TRACE_SFTP|LIBSSH2_TRACE_ERROR|
                    LIBSSH2_TRACE_PUBLICKEY);
    
      infof(data, "SSH socket: %d\n", sock);
    
    #endif /* CURL_LIBSSH2_DEBUG */
    
    
      state(conn, SSH_S_STARTUP);
    
      if (data->state.used_interface == Curl_if_multi)
        result = Curl_ssh_multi_statemach(conn, done);
      else {
        result = ssh_easy_statemach(conn);
        if (!result)
          *done = TRUE;
      }
    
      return result;
    
    /*
     ***********************************************************************
     *
     * scp_perform()
     *
     * This is the actual DO function for SCP. Get a file according to
     * the options previously setup.
     */
    
    static
    CURLcode scp_perform(struct connectdata *conn,
                          bool *connected,
                          bool *dophase_done)
    {
      CURLcode result = CURLE_OK;
    
      DEBUGF(infof(conn->data, "DO phase starts\n"));
    
      *dophase_done = FALSE; /* not done yet */
    
      /* start the first command in the DO phase */
      state(conn, SSH_SCP_TRANS_INIT);
    
      /* run the state-machine */
      if (conn->data->state.used_interface == Curl_if_multi) {
        result = Curl_ssh_multi_statemach(conn, dophase_done);
      } else {
        result = ssh_easy_statemach(conn);
        *dophase_done = TRUE; /* with the easy interface we are done here */
      }
      *connected = conn->bits.tcpconnect;
    
      if (*dophase_done) {
        DEBUGF(infof(conn->data, "DO phase is complete\n"));
      }
    
      return result;
    }
    
    /* called from multi.c while DOing */
    CURLcode Curl_scp_doing(struct connectdata *conn,
                             bool *dophase_done)
    {
      CURLcode result;
      result = Curl_ssh_multi_statemach(conn, dophase_done);
    
      if (*dophase_done) {
        DEBUGF(infof(conn->data, "DO phase is complete\n"));
      }
      return result;
    }
    
    
    
    CURLcode Curl_scp_do(struct connectdata *conn, bool *done)
    {
    
      CURLcode res;
      bool connected = 0;
      struct SessionHandle *data = conn->data;
    
      *done = FALSE; /* default to false */
    
      /*
       * Since connections can be re-used between SessionHandles, this might be a
       * connection already existing but on a fresh SessionHandle struct so we must
       * make sure we have a good 'struct SSHPROTO' to play with. For new
       * connections, the struct SSHPROTO is allocated and setup in the
       * Curl_ssh_connect() function.
       */
      res = ssh_init(conn);
      if (res) {
        return res;
      }
    
      data->reqdata.size = -1; /* make sure this is unknown at this point */
    
      Curl_pgrsSetUploadCounter(data, 0);
      Curl_pgrsSetDownloadCounter(data, 0);
      Curl_pgrsSetUploadSize(data, 0);
      Curl_pgrsSetDownloadSize(data, 0);
    
      res = scp_perform(conn, &connected,  done);
    
      if (CURLE_OK == res) {
    
        if (!done) {
          /* the DO phase has not completed yet */
          return CURLE_OK;
        }
      }
    
      return res;
    
    CURLcode Curl_scp_done(struct connectdata *conn, CURLcode status,
                           bool premature)
    
      CURLcode result = CURLE_OK;
      bool done = FALSE;
    
      (void)premature; /* not used */
      (void)status; /* unused */
    
    
      if (status == CURLE_OK) {
        state(conn, SSH_SCP_DONE);
        /* run the state-machine */
        if (conn->data->state.used_interface == Curl_if_multi) {
          result = Curl_ssh_multi_statemach(conn, &done);
        } else {
          result = ssh_easy_statemach(conn);
          done = TRUE;
        }
      } else {
        result = status;
        done = TRUE;
      }
    
      if (done) {
        Curl_safefree(conn->data->reqdata.proto.ssh);
        conn->data->reqdata.proto.ssh = NULL;
        Curl_pgrsDone(conn);
      }
    
      return result;
    
    
    }
    
    /* return number of received (decrypted) bytes */
    
    ssize_t Curl_scp_send(struct connectdata *conn, int sockindex,
                          void *mem, size_t len)
    
      /* libssh2_channel_write() returns int
       *
       * NOTE: we should not store nor rely on connection-related data to be
       * in the SessionHandle struct
       */
      nwrite = (ssize_t)
        libssh2_channel_write(conn->data->reqdata.proto.ssh->ssh_channel,
                              mem, len);
    
      (void)sockindex;
      return nwrite;
    }
    
    /*
     * If the read would block (EWOULDBLOCK) we return -1. Otherwise we return
     * a regular CURLcode value.
     */
    
    ssize_t Curl_scp_recv(struct connectdata *conn, int sockindex,
    
      (void)sockindex; /* we only support SCP on the fixed known primary socket */
    
      /* libssh2_channel_read() returns int
       *
       * NOTE: we should not store nor rely on connection-related data to be
       * in the SessionHandle struct
       */
      nread = (ssize_t)
        libssh2_channel_read(conn->data->reqdata.proto.ssh->ssh_channel,
                             mem, len);
      return nread;
    }
    
    /*
     * =============== SFTP ===============
     */
    
    
    /*
     ***********************************************************************
     *
     * sftp_perform()
     *
     * This is the actual DO function for SFTP. Get a file/directory according to
     * the options previously setup.
     */
    
    static
    CURLcode sftp_perform(struct connectdata *conn,
                          bool *connected,
                          bool *dophase_done)
    {
      CURLcode result = CURLE_OK;
    
      DEBUGF(infof(conn->data, "DO phase starts\n"));
    
      *dophase_done = FALSE; /* not done yet */