• Home
  • Raw
  • Download

Lines Matching +full:- +full:- +full:with +full:- +full:libssh2

21  * SPDX-License-Identifier: curl
33 #include <libssh2.h>
204 struct connectdata *conn = data->conn; in kbd_callback()
205 responses[0].text = strdup(conn->passwd); in kbd_callback()
207 responses[0].text == NULL ? 0 : curlx_uztoui(strlen(conn->passwd)); in kbd_callback()
247 /* Ordered by order of appearance in libssh2.h */ in libssh2_session_error_to_CURLE()
300 if(ptr) /* ssh2 agent sometimes call free with null ptr */ in LIBSSH2_FREE_FUNC()
310 struct connectdata *conn = data->conn; in state()
311 struct ssh_conn *sshc = &conn->proto.sshc; in state()
380 if(sshc->state != nowstate) { in state()
382 (void *)sshc, names[sshc->state], names[nowstate]); in state()
386 sshc->state = nowstate; in state()
408 * Earlier libssh2 versions didn't have the ability to seek to 64bit positions
409 * with 32bit size_t.
418 * Earlier libssh2 versions didn't do SCP properly beyond 32bit sizes on 32bit
429 * libssh2 1.2.8 fixed the problem with 32bit ints used for sockets on win64.
478 if(data->set.str[STRING_SSH_KNOWNHOSTS]) { in ssh_knownhost()
480 struct connectdata *conn = data->conn; in ssh_knownhost()
481 struct ssh_conn *sshc = &conn->proto.sshc; in ssh_knownhost()
483 const char *remotekey = libssh2_session_hostkey(sshc->ssh_session, in ssh_knownhost()
496 data->set.ssh_keyfunc ? data->set.ssh_keyfunc : sshkeycallback; in ssh_knownhost()
538 keycheck = libssh2_knownhost_checkp(sshc->kh, in ssh_knownhost()
539 conn->host.name, in ssh_knownhost()
540 (conn->remote_port != PORT_SSH)? in ssh_knownhost()
541 conn->remote_port:-1, in ssh_knownhost()
548 keycheck = libssh2_knownhost_check(sshc->kh, in ssh_knownhost()
549 conn->host.name, in ssh_knownhost()
559 host->key:"<none>"); in ssh_knownhost()
563 knownkey.key = host->key; in ssh_knownhost()
585 keymatch, data->set.ssh_keyfunc_userp); in ssh_knownhost()
600 result = sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION; in ssh_knownhost()
605 libssh2_knownhost_del(sshc->kh, host); in ssh_knownhost()
613 int addrc = libssh2_knownhost_add(sshc->kh, in ssh_knownhost()
614 conn->host.name, NULL, in ssh_knownhost()
621 conn->host.name); in ssh_knownhost()
624 /* now we write the entire in-memory list of known hosts to the in ssh_knownhost()
627 libssh2_knownhost_writefile(sshc->kh, in ssh_knownhost()
628 data->set.str[STRING_SSH_KNOWNHOSTS], in ssh_knownhost()
632 data->set.str[STRING_SSH_KNOWNHOSTS]); in ssh_knownhost()
647 struct connectdata *conn = data->conn; in ssh_check_fingerprint()
648 struct ssh_conn *sshc = &conn->proto.sshc; in ssh_check_fingerprint()
649 const char *pubkey_md5 = data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5]; in ssh_check_fingerprint()
650 const char *pubkey_sha256 = data->set.str[STRING_SSH_HOST_PUBLIC_KEY_SHA256]; in ssh_check_fingerprint()
666 fingerprint = libssh2_hostkey_hash(sshc->ssh_session, in ssh_check_fingerprint()
673 hostkey = libssh2_session_hostkey(sshc->ssh_session, &len, NULL); in ssh_check_fingerprint()
685 sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION; in ssh_check_fingerprint()
686 return sshc->actualcode; in ssh_check_fingerprint()
694 sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION; in ssh_check_fingerprint()
695 return sshc->actualcode; in ssh_check_fingerprint()
701 sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION; in ssh_check_fingerprint()
702 return sshc->actualcode; in ssh_check_fingerprint()
728 sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION; in ssh_check_fingerprint()
729 return sshc->actualcode; in ssh_check_fingerprint()
741 fingerprint = libssh2_hostkey_hash(sshc->ssh_session, in ssh_check_fingerprint()
768 sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION; in ssh_check_fingerprint()
769 return sshc->actualcode; in ssh_check_fingerprint()
775 if(data->set.ssh_hostkeyfunc) { in ssh_check_fingerprint()
780 const char *remotekey = libssh2_session_hostkey(sshc->ssh_session, in ssh_check_fingerprint()
785 rc = data->set.ssh_hostkeyfunc(data->set.ssh_hostkeyfunc_userp, in ssh_check_fingerprint()
790 sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION; in ssh_check_fingerprint()
791 return sshc->actualcode; in ssh_check_fingerprint()
796 sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION; in ssh_check_fingerprint()
797 return sshc->actualcode; in ssh_check_fingerprint()
823 = "ssh-ed25519"; in ssh_force_knownhost_key_type()
827 = "ecdsa-sha2-nistp521"; in ssh_force_knownhost_key_type()
831 = "ecdsa-sha2-nistp384"; in ssh_force_knownhost_key_type()
835 = "ecdsa-sha2-nistp256"; in ssh_force_knownhost_key_type()
838 = "ssh-rsa"; in ssh_force_knownhost_key_type()
840 = "rsa-sha2-256,rsa-sha2-512,ssh-rsa"; in ssh_force_knownhost_key_type()
842 = "ssh-dss"; in ssh_force_knownhost_key_type()
845 struct connectdata *conn = data->conn; in ssh_force_knownhost_key_type()
846 struct ssh_conn *sshc = &conn->proto.sshc; in ssh_force_knownhost_key_type()
853 if(sshc->kh && !data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5]) { in ssh_force_knownhost_key_type()
855 while(!libssh2_knownhost_get(sshc->kh, &store, store)) { in ssh_force_knownhost_key_type()
856 /* For non-standard ports, the name will be enclosed in */ in ssh_force_knownhost_key_type()
859 if(store->name) { in ssh_force_knownhost_key_type()
860 if(store->name[0] == '[') { in ssh_force_knownhost_key_type()
861 kh_name_end = strstr(store->name, "]:"); in ssh_force_knownhost_key_type()
864 store->name, data->set.str[STRING_SSH_KNOWNHOSTS]); in ssh_force_knownhost_key_type()
868 if(kh_name_end && (port == conn->remote_port)) { in ssh_force_knownhost_key_type()
869 kh_name_size = strlen(store->name) - 1 - strlen(kh_name_end); in ssh_force_knownhost_key_type()
870 if(strncmp(store->name + 1, in ssh_force_knownhost_key_type()
871 conn->host.name, kh_name_size) == 0) { in ssh_force_knownhost_key_type()
877 else if(strcmp(store->name, conn->host.name) == 0) { in ssh_force_knownhost_key_type()
892 conn->host.name, data->set.str[STRING_SSH_KNOWNHOSTS]); in ssh_force_knownhost_key_type()
894 switch(store->typemask & LIBSSH2_KNOWNHOST_KEY_MASK) { in ssh_force_knownhost_key_type()
922 /* old libssh2 which cannot correctly remove unsupported methods due in ssh_force_knownhost_key_type()
935 (store->typemask & LIBSSH2_KNOWNHOST_KEY_MASK)); in ssh_force_knownhost_key_type()
940 rc = libssh2_session_method_pref(sshc->ssh_session, in ssh_force_knownhost_key_type()
945 libssh2_session_last_error(sshc->ssh_session, &errmsg, &errlen, 0); in ssh_force_knownhost_key_type()
946 failf(data, "libssh2: %s", errmsg); in ssh_force_knownhost_key_type()
952 conn->host.name, data->set.str[STRING_SSH_KNOWNHOSTS]); in ssh_force_knownhost_key_type()
964 * to will be set to TRUE if the libssh2 function returns LIBSSH2_ERROR_EAGAIN
971 struct connectdata *conn = data->conn; in ssh_statemach_act()
972 struct SSHPROTO *sshp = data->req.p.ssh; in ssh_statemach_act()
973 struct ssh_conn *sshc = &conn->proto.sshc; in ssh_statemach_act()
974 curl_socket_t sock = conn->sock[FIRSTSOCKET]; in ssh_statemach_act()
983 switch(sshc->state) { in ssh_statemach_act()
985 sshc->secondCreateDirs = 0; in ssh_statemach_act()
986 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
987 sshc->actualcode = CURLE_OK; in ssh_statemach_act()
989 /* Set libssh2 to non-blocking, since everything internally is in ssh_statemach_act()
990 non-blocking */ in ssh_statemach_act()
991 libssh2_session_set_blocking(sshc->ssh_session, 0); in ssh_statemach_act()
996 sshc->actualcode = result; in ssh_statemach_act()
1004 rc = session_startup(sshc->ssh_session, sock); in ssh_statemach_act()
1010 (void)libssh2_session_last_error(sshc->ssh_session, &err_msg, NULL, 0); in ssh_statemach_act()
1014 sshc->actualcode = CURLE_FAILED_INIT; in ssh_statemach_act()
1038 * here, even though the libssh2 docs kind of indicate that it should be in ssh_statemach_act()
1039 * possible to get a 'generic' list (not user-specific) of authentication in ssh_statemach_act()
1040 * methods, presumably with a blank username. That won't work in my in ssh_statemach_act()
1044 sshc->authlist = libssh2_userauth_list(sshc->ssh_session, in ssh_statemach_act()
1045 conn->user, in ssh_statemach_act()
1046 curlx_uztoui(strlen(conn->user))); in ssh_statemach_act()
1048 if(!sshc->authlist) { in ssh_statemach_act()
1049 if(libssh2_userauth_authenticated(sshc->ssh_session)) { in ssh_statemach_act()
1050 sshc->authed = TRUE; in ssh_statemach_act()
1051 infof(data, "SSH user accepted with no authentication"); in ssh_statemach_act()
1055 ssherr = libssh2_session_last_errno(sshc->ssh_session); in ssh_statemach_act()
1060 sshc->actualcode = libssh2_session_error_to_CURLE(ssherr); in ssh_statemach_act()
1065 sshc->authlist); in ssh_statemach_act()
1073 * with the requested type of authentication in ssh_statemach_act()
1075 sshc->authed = FALSE; in ssh_statemach_act()
1077 if((data->set.ssh_auth_types & CURLSSH_AUTH_PUBLICKEY) && in ssh_statemach_act()
1078 (strstr(sshc->authlist, "publickey") != NULL)) { in ssh_statemach_act()
1081 sshc->rsa_pub = sshc->rsa = NULL; in ssh_statemach_act()
1083 if(data->set.str[STRING_SSH_PRIVATE_KEY]) in ssh_statemach_act()
1084 sshc->rsa = strdup(data->set.str[STRING_SSH_PRIVATE_KEY]); in ssh_statemach_act()
1086 /* To ponder about: should really the lib be messing about with the in ssh_statemach_act()
1094 sshc->rsa = aprintf("%s/.ssh/id_rsa", home); in ssh_statemach_act()
1095 if(!sshc->rsa) in ssh_statemach_act()
1097 else if(stat(sshc->rsa, &sbuf)) { in ssh_statemach_act()
1098 Curl_safefree(sshc->rsa); in ssh_statemach_act()
1099 sshc->rsa = aprintf("%s/.ssh/id_dsa", home); in ssh_statemach_act()
1100 if(!sshc->rsa) in ssh_statemach_act()
1102 else if(stat(sshc->rsa, &sbuf)) { in ssh_statemach_act()
1103 Curl_safefree(sshc->rsa); in ssh_statemach_act()
1108 if(!out_of_memory && !sshc->rsa) { in ssh_statemach_act()
1110 sshc->rsa = strdup("id_rsa"); in ssh_statemach_act()
1111 if(sshc->rsa && stat(sshc->rsa, &sbuf)) { in ssh_statemach_act()
1112 Curl_safefree(sshc->rsa); in ssh_statemach_act()
1113 sshc->rsa = strdup("id_dsa"); in ssh_statemach_act()
1114 if(sshc->rsa && stat(sshc->rsa, &sbuf)) { in ssh_statemach_act()
1115 Curl_safefree(sshc->rsa); in ssh_statemach_act()
1118 sshc->rsa = strdup(""); in ssh_statemach_act()
1126 * libssh2 extract the public key from the private key file. in ssh_statemach_act()
1127 * This is done by simply passing sshc->rsa_pub = NULL. in ssh_statemach_act()
1129 if(data->set.str[STRING_SSH_PUBLIC_KEY] in ssh_statemach_act()
1131 && data->set.str[STRING_SSH_PUBLIC_KEY][0]) { in ssh_statemach_act()
1132 sshc->rsa_pub = strdup(data->set.str[STRING_SSH_PUBLIC_KEY]); in ssh_statemach_act()
1133 if(!sshc->rsa_pub) in ssh_statemach_act()
1137 if(out_of_memory || !sshc->rsa) { in ssh_statemach_act()
1138 Curl_safefree(sshc->rsa); in ssh_statemach_act()
1139 Curl_safefree(sshc->rsa_pub); in ssh_statemach_act()
1141 sshc->actualcode = CURLE_OUT_OF_MEMORY; in ssh_statemach_act()
1145 sshc->passphrase = data->set.ssl.key_passwd; in ssh_statemach_act()
1146 if(!sshc->passphrase) in ssh_statemach_act()
1147 sshc->passphrase = ""; in ssh_statemach_act()
1149 if(sshc->rsa_pub) in ssh_statemach_act()
1150 infof(data, "Using SSH public key file '%s'", sshc->rsa_pub); in ssh_statemach_act()
1151 infof(data, "Using SSH private key file '%s'", sshc->rsa); in ssh_statemach_act()
1163 rc = libssh2_userauth_publickey_fromfile_ex(sshc->ssh_session, in ssh_statemach_act()
1164 conn->user, in ssh_statemach_act()
1166 strlen(conn->user)), in ssh_statemach_act()
1167 sshc->rsa_pub, in ssh_statemach_act()
1168 sshc->rsa, sshc->passphrase); in ssh_statemach_act()
1173 Curl_safefree(sshc->rsa_pub); in ssh_statemach_act()
1174 Curl_safefree(sshc->rsa); in ssh_statemach_act()
1177 sshc->authed = TRUE; in ssh_statemach_act()
1183 char unknown[] = "Reason unknown (-1)"; in ssh_statemach_act()
1184 if(rc == -1) { in ssh_statemach_act()
1190 (void)libssh2_session_last_error(sshc->ssh_session, in ssh_statemach_act()
1200 if((data->set.ssh_auth_types & CURLSSH_AUTH_PASSWORD) && in ssh_statemach_act()
1201 (strstr(sshc->authlist, "password") != NULL)) { in ssh_statemach_act()
1211 rc = libssh2_userauth_password_ex(sshc->ssh_session, conn->user, in ssh_statemach_act()
1212 curlx_uztoui(strlen(conn->user)), in ssh_statemach_act()
1213 conn->passwd, in ssh_statemach_act()
1214 curlx_uztoui(strlen(conn->passwd)), in ssh_statemach_act()
1220 sshc->authed = TRUE; in ssh_statemach_act()
1231 if((data->set.ssh_auth_types & CURLSSH_AUTH_HOST) && in ssh_statemach_act()
1232 (strstr(sshc->authlist, "hostbased") != NULL)) { in ssh_statemach_act()
1246 if((data->set.ssh_auth_types & CURLSSH_AUTH_AGENT) in ssh_statemach_act()
1247 && (strstr(sshc->authlist, "publickey") != NULL)) { in ssh_statemach_act()
1249 /* Connect to the ssh-agent */ in ssh_statemach_act()
1252 if(!sshc->ssh_agent) { in ssh_statemach_act()
1253 sshc->ssh_agent = libssh2_agent_init(sshc->ssh_session); in ssh_statemach_act()
1254 if(!sshc->ssh_agent) { in ssh_statemach_act()
1262 rc = libssh2_agent_connect(sshc->ssh_agent); in ssh_statemach_act()
1281 rc = libssh2_agent_list_identities(sshc->ssh_agent); in ssh_statemach_act()
1292 sshc->sshagent_prev_identity = NULL; in ssh_statemach_act()
1302 rc = libssh2_agent_get_identity(sshc->ssh_agent, in ssh_statemach_act()
1303 &sshc->sshagent_identity, in ssh_statemach_act()
1304 sshc->sshagent_prev_identity); in ssh_statemach_act()
1309 rc = libssh2_agent_userauth(sshc->ssh_agent, conn->user, in ssh_statemach_act()
1310 sshc->sshagent_identity); in ssh_statemach_act()
1315 sshc->sshagent_prev_identity = sshc->sshagent_identity; in ssh_statemach_act()
1327 sshc->authed = TRUE; in ssh_statemach_act()
1339 if((data->set.ssh_auth_types & CURLSSH_AUTH_KEYBOARD) in ssh_statemach_act()
1340 && (strstr(sshc->authlist, "keyboard-interactive") != NULL)) { in ssh_statemach_act()
1349 /* Authentication failed. Continue with keyboard-interactive now. */ in ssh_statemach_act()
1350 rc = libssh2_userauth_keyboard_interactive_ex(sshc->ssh_session, in ssh_statemach_act()
1351 conn->user, in ssh_statemach_act()
1353 strlen(conn->user)), in ssh_statemach_act()
1359 sshc->authed = TRUE; in ssh_statemach_act()
1366 if(!sshc->authed) { in ssh_statemach_act()
1369 sshc->actualcode = CURLE_LOGIN_DENIED; in ssh_statemach_act()
1380 conn->sockfd = sock; in ssh_statemach_act()
1381 conn->writesockfd = CURL_SOCKET_BAD; in ssh_statemach_act()
1383 if(conn->handler->protocol == CURLPROTO_SFTP) { in ssh_statemach_act()
1393 * Start the libssh2 sftp session in ssh_statemach_act()
1395 sshc->sftp_session = libssh2_sftp_init(sshc->ssh_session); in ssh_statemach_act()
1396 if(!sshc->sftp_session) { in ssh_statemach_act()
1398 if(libssh2_session_last_errno(sshc->ssh_session) == in ssh_statemach_act()
1404 (void)libssh2_session_last_error(sshc->ssh_session, in ssh_statemach_act()
1408 sshc->actualcode = CURLE_FAILED_INIT; in ssh_statemach_act()
1421 rc = sftp_libssh2_realpath(sshc->sftp_session, ".", in ssh_statemach_act()
1422 tempHome, PATH_MAX-1); in ssh_statemach_act()
1429 sshc->homedir = strdup(tempHome); in ssh_statemach_act()
1430 if(!sshc->homedir) { in ssh_statemach_act()
1432 sshc->actualcode = CURLE_OUT_OF_MEMORY; in ssh_statemach_act()
1435 data->state.most_recent_ftp_entrypath = sshc->homedir; in ssh_statemach_act()
1439 sftperr = libssh2_sftp_last_error(sshc->sftp_session); in ssh_statemach_act()
1444 a time-out or similar */ in ssh_statemach_act()
1446 sshc->actualcode = result; in ssh_statemach_act()
1463 result = Curl_getworkingpath(data, sshc->homedir, &sshp->path); in ssh_statemach_act()
1465 sshc->actualcode = result; in ssh_statemach_act()
1470 if(data->set.quote) { in ssh_statemach_act()
1472 sshc->quote_item = data->set.quote; in ssh_statemach_act()
1481 if(data->set.postquote) { in ssh_statemach_act()
1483 sshc->quote_item = data->set.postquote; in ssh_statemach_act()
1499 * 'sshc->quote_item' is already verified to be non-NULL before it in ssh_statemach_act()
1502 char *cmd = sshc->quote_item->data; in ssh_statemach_act()
1503 sshc->acceptfail = FALSE; in ssh_statemach_act()
1505 /* if a command starts with an asterisk, which a legal SFTP command never in ssh_statemach_act()
1512 sshc->acceptfail = TRUE; in ssh_statemach_act()
1518 sshp->path); in ssh_statemach_act()
1522 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1528 /* this sends an FTP-like "header" to the header callback so that the in ssh_statemach_act()
1535 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1536 sshc->actualcode = result; in ssh_statemach_act()
1545 * command with a space so we can check for it unconditionally in ssh_statemach_act()
1552 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1553 sshc->actualcode = CURLE_QUOTE_ERROR; in ssh_statemach_act()
1561 result = Curl_get_pathname(&cp, &sshc->quote_path1, sshc->homedir); in ssh_statemach_act()
1568 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1569 sshc->actualcode = result; in ssh_statemach_act()
1576 * OpenSSH's sftp program and call the appropriate libssh2 in ssh_statemach_act()
1586 /* sshc->quote_path1 contains the mode to set */ in ssh_statemach_act()
1588 result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir); in ssh_statemach_act()
1594 Curl_safefree(sshc->quote_path1); in ssh_statemach_act()
1596 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1597 sshc->actualcode = result; in ssh_statemach_act()
1600 memset(&sshp->quote_attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES)); in ssh_statemach_act()
1607 /* sshc->quote_path1 is the source */ in ssh_statemach_act()
1609 result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir); in ssh_statemach_act()
1616 Curl_safefree(sshc->quote_path1); in ssh_statemach_act()
1618 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1619 sshc->actualcode = result; in ssh_statemach_act()
1634 result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir); in ssh_statemach_act()
1640 Curl_safefree(sshc->quote_path1); in ssh_statemach_act()
1642 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1643 sshc->actualcode = result; in ssh_statemach_act()
1666 Curl_safefree(sshc->quote_path1); in ssh_statemach_act()
1667 Curl_safefree(sshc->quote_path2); in ssh_statemach_act()
1669 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1670 sshc->actualcode = CURLE_QUOTE_ERROR; in ssh_statemach_act()
1675 Curl_safefree(sshc->quote_path1); in ssh_statemach_act()
1676 Curl_safefree(sshc->quote_path2); in ssh_statemach_act()
1678 sshc->quote_item = sshc->quote_item->next; in ssh_statemach_act()
1680 if(sshc->quote_item) { in ssh_statemach_act()
1684 if(sshc->nextstate != SSH_NO_STATE) { in ssh_statemach_act()
1685 state(data, sshc->nextstate); in ssh_statemach_act()
1686 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1696 char *cmd = sshc->quote_item->data; in ssh_statemach_act()
1697 sshc->acceptfail = FALSE; in ssh_statemach_act()
1699 /* if a command starts with an asterisk, which a legal SFTP command never in ssh_statemach_act()
1706 sshc->acceptfail = TRUE; in ssh_statemach_act()
1710 /* Since chown and chgrp only set owner OR group but libssh2 wants to in ssh_statemach_act()
1714 rc = libssh2_sftp_stat_ex(sshc->sftp_session, sshc->quote_path2, in ssh_statemach_act()
1715 curlx_uztoui(strlen(sshc->quote_path2)), in ssh_statemach_act()
1717 &sshp->quote_attrs); in ssh_statemach_act()
1721 if(rc && !sshc->acceptfail) { /* get those attributes */ in ssh_statemach_act()
1722 sftperr = libssh2_sftp_last_error(sshc->sftp_session); in ssh_statemach_act()
1723 Curl_safefree(sshc->quote_path1); in ssh_statemach_act()
1724 Curl_safefree(sshc->quote_path2); in ssh_statemach_act()
1728 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1729 sshc->actualcode = CURLE_QUOTE_ERROR; in ssh_statemach_act()
1736 sshp->quote_attrs.gid = strtoul(sshc->quote_path1, NULL, 10); in ssh_statemach_act()
1737 sshp->quote_attrs.flags = LIBSSH2_SFTP_ATTR_UIDGID; in ssh_statemach_act()
1738 if(sshp->quote_attrs.gid == 0 && !ISDIGIT(sshc->quote_path1[0]) && in ssh_statemach_act()
1739 !sshc->acceptfail) { in ssh_statemach_act()
1740 Curl_safefree(sshc->quote_path1); in ssh_statemach_act()
1741 Curl_safefree(sshc->quote_path2); in ssh_statemach_act()
1744 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1745 sshc->actualcode = CURLE_QUOTE_ERROR; in ssh_statemach_act()
1750 sshp->quote_attrs.permissions = strtoul(sshc->quote_path1, NULL, 8); in ssh_statemach_act()
1751 sshp->quote_attrs.flags = LIBSSH2_SFTP_ATTR_PERMISSIONS; in ssh_statemach_act()
1753 if(sshp->quote_attrs.permissions == 0 && in ssh_statemach_act()
1754 !ISDIGIT(sshc->quote_path1[0])) { in ssh_statemach_act()
1755 Curl_safefree(sshc->quote_path1); in ssh_statemach_act()
1756 Curl_safefree(sshc->quote_path2); in ssh_statemach_act()
1759 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1760 sshc->actualcode = CURLE_QUOTE_ERROR; in ssh_statemach_act()
1765 sshp->quote_attrs.uid = strtoul(sshc->quote_path1, NULL, 10); in ssh_statemach_act()
1766 sshp->quote_attrs.flags = LIBSSH2_SFTP_ATTR_UIDGID; in ssh_statemach_act()
1767 if(sshp->quote_attrs.uid == 0 && !ISDIGIT(sshc->quote_path1[0]) && in ssh_statemach_act()
1768 !sshc->acceptfail) { in ssh_statemach_act()
1769 Curl_safefree(sshc->quote_path1); in ssh_statemach_act()
1770 Curl_safefree(sshc->quote_path2); in ssh_statemach_act()
1773 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1774 sshc->actualcode = CURLE_QUOTE_ERROR; in ssh_statemach_act()
1780 time_t date = Curl_getdate_capped(sshc->quote_path1); in ssh_statemach_act()
1783 if(date == -1) { in ssh_statemach_act()
1795 Curl_safefree(sshc->quote_path1); in ssh_statemach_act()
1796 Curl_safefree(sshc->quote_path2); in ssh_statemach_act()
1798 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1799 sshc->actualcode = CURLE_QUOTE_ERROR; in ssh_statemach_act()
1803 sshp->quote_attrs.atime = (unsigned long)date; in ssh_statemach_act()
1805 sshp->quote_attrs.mtime = (unsigned long)date; in ssh_statemach_act()
1807 sshp->quote_attrs.flags = LIBSSH2_SFTP_ATTR_ACMODTIME; in ssh_statemach_act()
1816 rc = libssh2_sftp_stat_ex(sshc->sftp_session, sshc->quote_path2, in ssh_statemach_act()
1817 curlx_uztoui(strlen(sshc->quote_path2)), in ssh_statemach_act()
1819 &sshp->quote_attrs); in ssh_statemach_act()
1823 if(rc && !sshc->acceptfail) { in ssh_statemach_act()
1824 sftperr = libssh2_sftp_last_error(sshc->sftp_session); in ssh_statemach_act()
1825 Curl_safefree(sshc->quote_path1); in ssh_statemach_act()
1826 Curl_safefree(sshc->quote_path2); in ssh_statemach_act()
1830 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1831 sshc->actualcode = CURLE_QUOTE_ERROR; in ssh_statemach_act()
1838 rc = libssh2_sftp_symlink_ex(sshc->sftp_session, sshc->quote_path1, in ssh_statemach_act()
1839 curlx_uztoui(strlen(sshc->quote_path1)), in ssh_statemach_act()
1840 sshc->quote_path2, in ssh_statemach_act()
1841 curlx_uztoui(strlen(sshc->quote_path2)), in ssh_statemach_act()
1846 if(rc && !sshc->acceptfail) { in ssh_statemach_act()
1847 sftperr = libssh2_sftp_last_error(sshc->sftp_session); in ssh_statemach_act()
1848 Curl_safefree(sshc->quote_path1); in ssh_statemach_act()
1849 Curl_safefree(sshc->quote_path2); in ssh_statemach_act()
1853 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1854 sshc->actualcode = CURLE_QUOTE_ERROR; in ssh_statemach_act()
1861 rc = libssh2_sftp_mkdir_ex(sshc->sftp_session, sshc->quote_path1, in ssh_statemach_act()
1862 curlx_uztoui(strlen(sshc->quote_path1)), in ssh_statemach_act()
1863 data->set.new_directory_perms); in ssh_statemach_act()
1867 if(rc && !sshc->acceptfail) { in ssh_statemach_act()
1868 sftperr = libssh2_sftp_last_error(sshc->sftp_session); in ssh_statemach_act()
1869 Curl_safefree(sshc->quote_path1); in ssh_statemach_act()
1873 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1874 sshc->actualcode = CURLE_QUOTE_ERROR; in ssh_statemach_act()
1881 rc = libssh2_sftp_rename_ex(sshc->sftp_session, sshc->quote_path1, in ssh_statemach_act()
1882 curlx_uztoui(strlen(sshc->quote_path1)), in ssh_statemach_act()
1883 sshc->quote_path2, in ssh_statemach_act()
1884 curlx_uztoui(strlen(sshc->quote_path2)), in ssh_statemach_act()
1892 if(rc && !sshc->acceptfail) { in ssh_statemach_act()
1893 sftperr = libssh2_sftp_last_error(sshc->sftp_session); in ssh_statemach_act()
1894 Curl_safefree(sshc->quote_path1); in ssh_statemach_act()
1895 Curl_safefree(sshc->quote_path2); in ssh_statemach_act()
1899 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1900 sshc->actualcode = CURLE_QUOTE_ERROR; in ssh_statemach_act()
1907 rc = libssh2_sftp_rmdir_ex(sshc->sftp_session, sshc->quote_path1, in ssh_statemach_act()
1908 curlx_uztoui(strlen(sshc->quote_path1))); in ssh_statemach_act()
1912 if(rc && !sshc->acceptfail) { in ssh_statemach_act()
1913 sftperr = libssh2_sftp_last_error(sshc->sftp_session); in ssh_statemach_act()
1914 Curl_safefree(sshc->quote_path1); in ssh_statemach_act()
1918 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1919 sshc->actualcode = CURLE_QUOTE_ERROR; in ssh_statemach_act()
1926 rc = libssh2_sftp_unlink_ex(sshc->sftp_session, sshc->quote_path1, in ssh_statemach_act()
1927 curlx_uztoui(strlen(sshc->quote_path1))); in ssh_statemach_act()
1931 if(rc && !sshc->acceptfail) { in ssh_statemach_act()
1932 sftperr = libssh2_sftp_last_error(sshc->sftp_session); in ssh_statemach_act()
1933 Curl_safefree(sshc->quote_path1); in ssh_statemach_act()
1936 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1937 sshc->actualcode = CURLE_QUOTE_ERROR; in ssh_statemach_act()
1947 rc = libssh2_sftp_statvfs(sshc->sftp_session, sshc->quote_path1, in ssh_statemach_act()
1948 curlx_uztoui(strlen(sshc->quote_path1)), in ssh_statemach_act()
1954 if(rc && !sshc->acceptfail) { in ssh_statemach_act()
1955 sftperr = libssh2_sftp_last_error(sshc->sftp_session); in ssh_statemach_act()
1956 Curl_safefree(sshc->quote_path1); in ssh_statemach_act()
1960 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1961 sshc->actualcode = CURLE_QUOTE_ERROR; in ssh_statemach_act()
1991 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
1999 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
2000 sshc->actualcode = result; in ssh_statemach_act()
2009 if(data->set.get_filetime) { in ssh_statemach_act()
2022 rc = libssh2_sftp_stat_ex(sshc->sftp_session, sshp->path, in ssh_statemach_act()
2023 curlx_uztoui(strlen(sshp->path)), in ssh_statemach_act()
2029 data->info.filetime = attrs.mtime; in ssh_statemach_act()
2037 if(data->state.upload) in ssh_statemach_act()
2040 if(sshp->path[strlen(sshp->path)-1] == '/') in ssh_statemach_act()
2051 * NOTE!!! libssh2 requires that the destination path is a full path in ssh_statemach_act()
2057 if(data->state.resume_from) { in ssh_statemach_act()
2059 if(data->state.resume_from < 0) { in ssh_statemach_act()
2060 rc = libssh2_sftp_stat_ex(sshc->sftp_session, sshp->path, in ssh_statemach_act()
2061 curlx_uztoui(strlen(sshp->path)), in ssh_statemach_act()
2067 data->state.resume_from = 0; in ssh_statemach_act()
2075 data->state.resume_from = attrs.filesize; in ssh_statemach_act()
2080 if(data->set.remote_append) in ssh_statemach_act()
2083 else if(data->state.resume_from > 0) in ssh_statemach_act()
2090 sshc->sftp_handle = in ssh_statemach_act()
2091 libssh2_sftp_open_ex(sshc->sftp_session, sshp->path, in ssh_statemach_act()
2092 curlx_uztoui(strlen(sshp->path)), in ssh_statemach_act()
2093 flags, data->set.new_file_perms, in ssh_statemach_act()
2096 if(!sshc->sftp_handle) { in ssh_statemach_act()
2097 rc = libssh2_session_last_errno(sshc->ssh_session); in ssh_statemach_act()
2105 sftperr = libssh2_sftp_last_error(sshc->sftp_session); in ssh_statemach_act()
2109 if(sshc->secondCreateDirs) { in ssh_statemach_act()
2111 sshc->actualcode = sftperr != LIBSSH2_FX_OK ? in ssh_statemach_act()
2120 (data->set.ftp_create_missing_dirs && in ssh_statemach_act()
2121 (strlen(sshp->path) > 1))) { in ssh_statemach_act()
2124 sshc->secondCreateDirs = 1; in ssh_statemach_act()
2129 sshc->actualcode = sftperr != LIBSSH2_FX_OK ? in ssh_statemach_act()
2131 if(!sshc->actualcode) { in ssh_statemach_act()
2135 sshc->actualcode = CURLE_SSH; in ssh_statemach_act()
2147 if(data->state.resume_from > 0) { in ssh_statemach_act()
2149 if(data->set.seek_func) { in ssh_statemach_act()
2151 seekerr = data->set.seek_func(data->set.seek_client, in ssh_statemach_act()
2152 data->state.resume_from, SEEK_SET); in ssh_statemach_act()
2167 (data->state.resume_from - passed > in ssh_statemach_act()
2169 sizeof(scratch) : curlx_sotouz(data->state.resume_from - passed); in ssh_statemach_act()
2173 actuallyread = data->state.fread_func(scratch, 1, in ssh_statemach_act()
2175 data->state.in); in ssh_statemach_act()
2180 /* this checks for greater-than only to make sure that the in ssh_statemach_act()
2185 } while(passed < data->state.resume_from); in ssh_statemach_act()
2189 if(data->state.infilesize > 0) { in ssh_statemach_act()
2190 data->state.infilesize -= data->state.resume_from; in ssh_statemach_act()
2191 data->req.size = data->state.infilesize; in ssh_statemach_act()
2192 Curl_pgrsSetUploadSize(data, data->state.infilesize); in ssh_statemach_act()
2195 SFTP_SEEK(sshc->sftp_handle, data->state.resume_from); in ssh_statemach_act()
2197 if(data->state.infilesize > 0) { in ssh_statemach_act()
2198 data->req.size = data->state.infilesize; in ssh_statemach_act()
2199 Curl_pgrsSetUploadSize(data, data->state.infilesize); in ssh_statemach_act()
2202 Curl_xfer_setup(data, -1, -1, FALSE, FIRSTSOCKET); in ssh_statemach_act()
2205 conn->sockfd = conn->writesockfd; in ssh_statemach_act()
2209 sshc->actualcode = result; in ssh_statemach_act()
2214 sshc->orig_waitfor = data->req.keepon; in ssh_statemach_act()
2217 out readable as the underlying libssh2 sftp send function will deal in ssh_statemach_act()
2218 with both accordingly */ in ssh_statemach_act()
2219 data->state.select_bits = CURL_CSELECT_OUT; in ssh_statemach_act()
2232 if(strlen(sshp->path) > 1) { in ssh_statemach_act()
2233 sshc->slash_pos = sshp->path + 1; /* ignore the leading '/' */ in ssh_statemach_act()
2242 sshc->slash_pos = strchr(sshc->slash_pos, '/'); in ssh_statemach_act()
2243 if(sshc->slash_pos) { in ssh_statemach_act()
2244 *sshc->slash_pos = 0; in ssh_statemach_act()
2246 infof(data, "Creating directory '%s'", sshp->path); in ssh_statemach_act()
2254 /* 'mode' - parameter is preliminary - default to 0644 */ in ssh_statemach_act()
2255 rc = libssh2_sftp_mkdir_ex(sshc->sftp_session, sshp->path, in ssh_statemach_act()
2256 curlx_uztoui(strlen(sshp->path)), in ssh_statemach_act()
2257 data->set.new_directory_perms); in ssh_statemach_act()
2261 *sshc->slash_pos = '/'; in ssh_statemach_act()
2262 ++sshc->slash_pos; in ssh_statemach_act()
2267 * path) - retry on unspecific FAILURE also in ssh_statemach_act()
2269 sftperr = libssh2_sftp_last_error(sshc->sftp_session); in ssh_statemach_act()
2275 sshc->actualcode = result?result:CURLE_SSH; in ssh_statemach_act()
2284 Curl_pgrsSetDownloadSize(data, -1); in ssh_statemach_act()
2285 if(data->req.no_body) { in ssh_statemach_act()
2294 sshc->sftp_handle = libssh2_sftp_open_ex(sshc->sftp_session, in ssh_statemach_act()
2295 sshp->path, in ssh_statemach_act()
2297 strlen(sshp->path)), in ssh_statemach_act()
2299 if(!sshc->sftp_handle) { in ssh_statemach_act()
2300 if(libssh2_session_last_errno(sshc->ssh_session) == in ssh_statemach_act()
2305 sftperr = libssh2_sftp_last_error(sshc->sftp_session); in ssh_statemach_act()
2310 sshc->actualcode = result?result:CURLE_SSH; in ssh_statemach_act()
2313 sshp->readdir_filename = malloc(PATH_MAX + 1); in ssh_statemach_act()
2314 if(!sshp->readdir_filename) { in ssh_statemach_act()
2316 sshc->actualcode = CURLE_OUT_OF_MEMORY; in ssh_statemach_act()
2319 sshp->readdir_longentry = malloc(PATH_MAX + 1); in ssh_statemach_act()
2320 if(!sshp->readdir_longentry) { in ssh_statemach_act()
2321 Curl_safefree(sshp->readdir_filename); in ssh_statemach_act()
2323 sshc->actualcode = CURLE_OUT_OF_MEMORY; in ssh_statemach_act()
2326 Curl_dyn_init(&sshp->readdir, PATH_MAX * 2); in ssh_statemach_act()
2331 rc = libssh2_sftp_readdir_ex(sshc->sftp_handle, in ssh_statemach_act()
2332 sshp->readdir_filename, in ssh_statemach_act()
2334 sshp->readdir_longentry, in ssh_statemach_act()
2336 &sshp->readdir_attrs); in ssh_statemach_act()
2342 sshp->readdir_filename[readdir_len] = '\0'; in ssh_statemach_act()
2344 if(data->set.list_only) { in ssh_statemach_act()
2346 sshp->readdir_filename, in ssh_statemach_act()
2358 result = Curl_dyn_add(&sshp->readdir, sshp->readdir_longentry); in ssh_statemach_act()
2361 if((sshp->readdir_attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) && in ssh_statemach_act()
2362 ((sshp->readdir_attrs.permissions & LIBSSH2_SFTP_S_IFMT) == in ssh_statemach_act()
2364 Curl_dyn_init(&sshp->readdir_link, PATH_MAX); in ssh_statemach_act()
2365 result = Curl_dyn_addf(&sshp->readdir_link, "%s%s", sshp->path, in ssh_statemach_act()
2366 sshp->readdir_filename); in ssh_statemach_act()
2376 sshc->actualcode = result; in ssh_statemach_act()
2382 Curl_safefree(sshp->readdir_filename); in ssh_statemach_act()
2383 Curl_safefree(sshp->readdir_longentry); in ssh_statemach_act()
2388 sftperr = libssh2_sftp_last_error(sshc->sftp_session); in ssh_statemach_act()
2390 sshc->actualcode = result?result:CURLE_SSH; in ssh_statemach_act()
2393 libssh2_session_last_errno(sshc->ssh_session)); in ssh_statemach_act()
2394 Curl_safefree(sshp->readdir_filename); in ssh_statemach_act()
2395 Curl_safefree(sshp->readdir_longentry); in ssh_statemach_act()
2403 libssh2_sftp_symlink_ex(sshc->sftp_session, in ssh_statemach_act()
2404 Curl_dyn_ptr(&sshp->readdir_link), in ssh_statemach_act()
2405 (int)Curl_dyn_len(&sshp->readdir_link), in ssh_statemach_act()
2406 sshp->readdir_filename, in ssh_statemach_act()
2411 Curl_dyn_free(&sshp->readdir_link); in ssh_statemach_act()
2414 result = Curl_dyn_addf(&sshp->readdir, " -> %s", sshp->readdir_filename); in ssh_statemach_act()
2417 Curl_safefree(sshp->readdir_filename); in ssh_statemach_act()
2418 Curl_safefree(sshp->readdir_longentry); in ssh_statemach_act()
2420 sshc->actualcode = result; in ssh_statemach_act()
2428 result = Curl_dyn_addn(&sshp->readdir, "\n", 1); in ssh_statemach_act()
2431 Curl_dyn_ptr(&sshp->readdir), in ssh_statemach_act()
2432 Curl_dyn_len(&sshp->readdir)); in ssh_statemach_act()
2435 Curl_dyn_free(&sshp->readdir); in ssh_statemach_act()
2439 Curl_dyn_reset(&sshp->readdir); in ssh_statemach_act()
2445 if(libssh2_sftp_closedir(sshc->sftp_handle) == in ssh_statemach_act()
2450 sshc->sftp_handle = NULL; in ssh_statemach_act()
2451 Curl_safefree(sshp->readdir_filename); in ssh_statemach_act()
2452 Curl_safefree(sshp->readdir_longentry); in ssh_statemach_act()
2455 Curl_xfer_setup(data, -1, -1, FALSE, -1); in ssh_statemach_act()
2463 sshc->sftp_handle = in ssh_statemach_act()
2464 libssh2_sftp_open_ex(sshc->sftp_session, sshp->path, in ssh_statemach_act()
2465 curlx_uztoui(strlen(sshp->path)), in ssh_statemach_act()
2466 LIBSSH2_FXF_READ, data->set.new_file_perms, in ssh_statemach_act()
2468 if(!sshc->sftp_handle) { in ssh_statemach_act()
2469 if(libssh2_session_last_errno(sshc->ssh_session) == in ssh_statemach_act()
2474 sftperr = libssh2_sftp_last_error(sshc->sftp_session); in ssh_statemach_act()
2479 sshc->actualcode = result?result:CURLE_SSH; in ssh_statemach_act()
2489 rc = libssh2_sftp_stat_ex(sshc->sftp_session, sshp->path, in ssh_statemach_act()
2490 curlx_uztoui(strlen(sshp->path)), in ssh_statemach_act()
2501 * OR the server doesn't return a file size with a stat() in ssh_statemach_act()
2504 data->req.size = -1; in ssh_statemach_act()
2505 data->req.maxdownload = -1; in ssh_statemach_act()
2506 Curl_pgrsSetDownloadSize(data, -1); in ssh_statemach_act()
2515 if(data->state.use_range) { in ssh_statemach_act()
2522 from_t = curlx_strtoofft(data->state.range, &ptr, 10, &from); in ssh_statemach_act()
2525 while(*ptr && (ISBLANK(*ptr) || (*ptr == '-'))) in ssh_statemach_act()
2532 to = size - 1; in ssh_statemach_act()
2536 from = size - to; in ssh_statemach_act()
2537 to = size - 1; in ssh_statemach_act()
2551 if((to - from) == CURL_OFF_T_MAX) in ssh_statemach_act()
2553 size = to - from + 1; in ssh_statemach_act()
2556 SFTP_SEEK(sshc->sftp_handle, from); in ssh_statemach_act()
2558 data->req.size = size; in ssh_statemach_act()
2559 data->req.maxdownload = size; in ssh_statemach_act()
2564 if(data->state.resume_from) { in ssh_statemach_act()
2565 if(data->state.resume_from < 0) { in ssh_statemach_act()
2567 if((curl_off_t)attrs.filesize < -data->state.resume_from) { in ssh_statemach_act()
2571 data->state.resume_from, (curl_off_t)attrs.filesize); in ssh_statemach_act()
2575 data->state.resume_from += attrs.filesize; in ssh_statemach_act()
2578 if((curl_off_t)attrs.filesize < data->state.resume_from) { in ssh_statemach_act()
2581 data->state.resume_from, (curl_off_t)attrs.filesize); in ssh_statemach_act()
2586 data->req.size = attrs.filesize - data->state.resume_from; in ssh_statemach_act()
2587 data->req.maxdownload = attrs.filesize - data->state.resume_from; in ssh_statemach_act()
2589 attrs.filesize - data->state.resume_from); in ssh_statemach_act()
2590 SFTP_SEEK(sshc->sftp_handle, data->state.resume_from); in ssh_statemach_act()
2595 if(data->req.size == 0) { in ssh_statemach_act()
2597 Curl_xfer_setup(data, -1, -1, FALSE, -1); in ssh_statemach_act()
2602 Curl_xfer_setup(data, FIRSTSOCKET, data->req.size, FALSE, -1); in ssh_statemach_act()
2605 conn->writesockfd = conn->sockfd; in ssh_statemach_act()
2608 out writableable as the underlying libssh2 recv function will deal in ssh_statemach_act()
2609 with both accordingly */ in ssh_statemach_act()
2610 data->state.select_bits = CURL_CSELECT_IN; in ssh_statemach_act()
2616 sshc->actualcode = result; in ssh_statemach_act()
2624 if(sshc->sftp_handle) { in ssh_statemach_act()
2625 rc = libssh2_sftp_close(sshc->sftp_handle); in ssh_statemach_act()
2631 (void)libssh2_session_last_error(sshc->ssh_session, in ssh_statemach_act()
2633 infof(data, "Failed to close libssh2 file: %d %s", rc, err_msg); in ssh_statemach_act()
2635 sshc->sftp_handle = NULL; in ssh_statemach_act()
2638 Curl_safefree(sshp->path); in ssh_statemach_act()
2645 if(sshc->nextstate != SSH_NO_STATE && in ssh_statemach_act()
2646 sshc->nextstate != SSH_SFTP_CLOSE) { in ssh_statemach_act()
2647 state(data, sshc->nextstate); in ssh_statemach_act()
2648 sshc->nextstate = SSH_SFTP_CLOSE; in ssh_statemach_act()
2652 result = sshc->actualcode; in ssh_statemach_act()
2661 if(sshc->sftp_handle) { in ssh_statemach_act()
2662 rc = libssh2_sftp_close(sshc->sftp_handle); in ssh_statemach_act()
2668 (void)libssh2_session_last_error(sshc->ssh_session, &err_msg, in ssh_statemach_act()
2670 infof(data, "Failed to close libssh2 file: %d %s", rc, err_msg); in ssh_statemach_act()
2672 sshc->sftp_handle = NULL; in ssh_statemach_act()
2674 if(sshc->sftp_session) { in ssh_statemach_act()
2675 rc = libssh2_sftp_shutdown(sshc->sftp_session); in ssh_statemach_act()
2680 infof(data, "Failed to stop libssh2 sftp subsystem"); in ssh_statemach_act()
2682 sshc->sftp_session = NULL; in ssh_statemach_act()
2685 Curl_safefree(sshc->homedir); in ssh_statemach_act()
2686 data->state.most_recent_ftp_entrypath = NULL; in ssh_statemach_act()
2692 result = Curl_getworkingpath(data, sshc->homedir, &sshp->path); in ssh_statemach_act()
2694 sshc->actualcode = result; in ssh_statemach_act()
2699 if(data->state.upload) { in ssh_statemach_act()
2700 if(data->state.infilesize < 0) { in ssh_statemach_act()
2702 sshc->actualcode = CURLE_UPLOAD_FAILED; in ssh_statemach_act()
2715 * libssh2 requires that the destination path is a full path that in ssh_statemach_act()
2720 sshc->ssh_channel = in ssh_statemach_act()
2721 SCP_SEND(sshc->ssh_session, sshp->path, data->set.new_file_perms, in ssh_statemach_act()
2722 data->state.infilesize); in ssh_statemach_act()
2723 if(!sshc->ssh_channel) { in ssh_statemach_act()
2727 if(libssh2_session_last_errno(sshc->ssh_session) == in ssh_statemach_act()
2733 ssh_err = (int)(libssh2_session_last_error(sshc->ssh_session, in ssh_statemach_act()
2737 sshc->actualcode = libssh2_session_error_to_CURLE(ssh_err); in ssh_statemach_act()
2739 if(sshc->actualcode == CURLE_SSH || in ssh_statemach_act()
2740 sshc->actualcode == CURLE_REMOTE_FILE_NOT_FOUND) in ssh_statemach_act()
2741 sshc->actualcode = CURLE_UPLOAD_FAILED; in ssh_statemach_act()
2746 data->req.size = data->state.infilesize; in ssh_statemach_act()
2747 Curl_pgrsSetUploadSize(data, data->state.infilesize); in ssh_statemach_act()
2748 Curl_xfer_setup(data, -1, -1, FALSE, FIRSTSOCKET); in ssh_statemach_act()
2751 conn->sockfd = conn->writesockfd; in ssh_statemach_act()
2755 sshc->actualcode = result; in ssh_statemach_act()
2760 sshc->orig_waitfor = data->req.keepon; in ssh_statemach_act()
2763 out readable as the underlying libssh2 scp send function will deal in ssh_statemach_act()
2764 with both accordingly */ in ssh_statemach_act()
2765 data->state.select_bits = CURL_CSELECT_OUT; in ssh_statemach_act()
2788 sshc->ssh_channel = libssh2_scp_recv(sshc->ssh_session, in ssh_statemach_act()
2789 sshp->path, &sb); in ssh_statemach_act()
2793 sshc->ssh_channel = libssh2_scp_recv2(sshc->ssh_session, in ssh_statemach_act()
2794 sshp->path, &sb); in ssh_statemach_act()
2797 if(!sshc->ssh_channel) { in ssh_statemach_act()
2801 if(libssh2_session_last_errno(sshc->ssh_session) == in ssh_statemach_act()
2808 ssh_err = (int)(libssh2_session_last_error(sshc->ssh_session, in ssh_statemach_act()
2812 sshc->actualcode = libssh2_session_error_to_CURLE(ssh_err); in ssh_statemach_act()
2818 data->req.maxdownload = (curl_off_t)sb.st_size; in ssh_statemach_act()
2819 Curl_xfer_setup(data, FIRSTSOCKET, bytecount, FALSE, -1); in ssh_statemach_act()
2822 conn->writesockfd = conn->sockfd; in ssh_statemach_act()
2825 out writableable as the underlying libssh2 recv function will deal in ssh_statemach_act()
2826 with both accordingly */ in ssh_statemach_act()
2827 data->state.select_bits = CURL_CSELECT_IN; in ssh_statemach_act()
2831 sshc->actualcode = result; in ssh_statemach_act()
2839 if(data->state.upload) in ssh_statemach_act()
2846 if(sshc->ssh_channel) { in ssh_statemach_act()
2847 rc = libssh2_channel_send_eof(sshc->ssh_channel); in ssh_statemach_act()
2853 (void)libssh2_session_last_error(sshc->ssh_session, in ssh_statemach_act()
2855 infof(data, "Failed to send libssh2 channel EOF: %d %s", in ssh_statemach_act()
2863 if(sshc->ssh_channel) { in ssh_statemach_act()
2864 rc = libssh2_channel_wait_eof(sshc->ssh_channel); in ssh_statemach_act()
2870 (void)libssh2_session_last_error(sshc->ssh_session, in ssh_statemach_act()
2879 if(sshc->ssh_channel) { in ssh_statemach_act()
2880 rc = libssh2_channel_wait_closed(sshc->ssh_channel); in ssh_statemach_act()
2886 (void)libssh2_session_last_error(sshc->ssh_session, in ssh_statemach_act()
2895 if(sshc->ssh_channel) { in ssh_statemach_act()
2896 rc = libssh2_channel_free(sshc->ssh_channel); in ssh_statemach_act()
2902 (void)libssh2_session_last_error(sshc->ssh_session, in ssh_statemach_act()
2904 infof(data, "Failed to free libssh2 scp subsystem: %d %s", in ssh_statemach_act()
2907 sshc->ssh_channel = NULL; in ssh_statemach_act()
2914 result = sshc->actualcode; in ssh_statemach_act()
2921 if(sshc->ssh_channel) { in ssh_statemach_act()
2922 rc = libssh2_channel_free(sshc->ssh_channel); in ssh_statemach_act()
2928 (void)libssh2_session_last_error(sshc->ssh_session, in ssh_statemach_act()
2930 infof(data, "Failed to free libssh2 scp subsystem: %d %s", in ssh_statemach_act()
2933 sshc->ssh_channel = NULL; in ssh_statemach_act()
2936 if(sshc->ssh_session) { in ssh_statemach_act()
2937 rc = libssh2_session_disconnect(sshc->ssh_session, "Shutdown"); in ssh_statemach_act()
2943 (void)libssh2_session_last_error(sshc->ssh_session, in ssh_statemach_act()
2945 infof(data, "Failed to disconnect libssh2 session: %d %s", in ssh_statemach_act()
2950 Curl_safefree(sshc->homedir); in ssh_statemach_act()
2951 data->state.most_recent_ftp_entrypath = NULL; in ssh_statemach_act()
2958 if(sshc->kh) { in ssh_statemach_act()
2959 libssh2_knownhost_free(sshc->kh); in ssh_statemach_act()
2960 sshc->kh = NULL; in ssh_statemach_act()
2965 if(sshc->ssh_agent) { in ssh_statemach_act()
2966 rc = libssh2_agent_disconnect(sshc->ssh_agent); in ssh_statemach_act()
2972 (void)libssh2_session_last_error(sshc->ssh_session, in ssh_statemach_act()
2974 infof(data, "Failed to disconnect from libssh2 agent: %d %s", in ssh_statemach_act()
2977 libssh2_agent_free(sshc->ssh_agent); in ssh_statemach_act()
2978 sshc->ssh_agent = NULL; in ssh_statemach_act()
2982 sshc->sshagent_identity = NULL; in ssh_statemach_act()
2983 sshc->sshagent_prev_identity = NULL; in ssh_statemach_act()
2987 if(sshc->ssh_session) { in ssh_statemach_act()
2988 rc = libssh2_session_free(sshc->ssh_session); in ssh_statemach_act()
2994 (void)libssh2_session_last_error(sshc->ssh_session, in ssh_statemach_act()
2996 infof(data, "Failed to free libssh2 session: %d %s", rc, err_msg); in ssh_statemach_act()
2998 sshc->ssh_session = NULL; in ssh_statemach_act()
3001 /* worst-case scenario cleanup */ in ssh_statemach_act()
3003 DEBUGASSERT(sshc->ssh_session == NULL); in ssh_statemach_act()
3004 DEBUGASSERT(sshc->ssh_channel == NULL); in ssh_statemach_act()
3005 DEBUGASSERT(sshc->sftp_session == NULL); in ssh_statemach_act()
3006 DEBUGASSERT(sshc->sftp_handle == NULL); in ssh_statemach_act()
3008 DEBUGASSERT(sshc->kh == NULL); in ssh_statemach_act()
3011 DEBUGASSERT(sshc->ssh_agent == NULL); in ssh_statemach_act()
3014 Curl_safefree(sshc->rsa_pub); in ssh_statemach_act()
3015 Curl_safefree(sshc->rsa); in ssh_statemach_act()
3016 Curl_safefree(sshc->quote_path1); in ssh_statemach_act()
3017 Curl_safefree(sshc->quote_path2); in ssh_statemach_act()
3018 Curl_safefree(sshc->homedir); in ssh_statemach_act()
3021 result = sshc->actualcode; in ssh_statemach_act()
3026 sshc->state = SSH_SESSION_FREE; /* current */ in ssh_statemach_act()
3027 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
3034 sshc->nextstate = SSH_NO_STATE; in ssh_statemach_act()
3039 } while(!rc && (sshc->state != SSH_STOP)); in ssh_statemach_act()
3059 sock[0] = conn->sock[FIRSTSOCKET]; in ssh_getsock()
3061 if(conn->waitfor & KEEP_RECV) in ssh_getsock()
3064 if(conn->waitfor & KEEP_SEND) in ssh_getsock()
3071 * When one of the libssh2 functions has returned LIBSSH2_ERROR_EAGAIN this
3079 struct connectdata *conn = data->conn; in ssh_block2waitfor()
3080 struct ssh_conn *sshc = &conn->proto.sshc; in ssh_block2waitfor()
3083 dir = libssh2_session_block_directions(sshc->ssh_session); in ssh_block2waitfor()
3085 /* translate the libssh2 define bits into our own bit defines */ in ssh_block2waitfor()
3086 conn->waitfor = ((dir&LIBSSH2_SESSION_BLOCK_INBOUND)?KEEP_RECV:0) | in ssh_block2waitfor()
3091 /* It didn't block or libssh2 didn't reveal in which direction, put back in ssh_block2waitfor()
3093 conn->waitfor = sshc->orig_waitfor; in ssh_block2waitfor()
3099 struct connectdata *conn = data->conn; in ssh_multi_statemach()
3100 struct ssh_conn *sshc = &conn->proto.sshc; in ssh_multi_statemach()
3106 *done = (sshc->state == SSH_STOP) ? TRUE : FALSE; in ssh_multi_statemach()
3119 struct ssh_conn *sshc = &conn->proto.sshc; in ssh_block_statemach()
3123 while((sshc->state != SSH_STOP) && !result) { in ssh_block_statemach()
3154 int dir = libssh2_session_block_directions(sshc->ssh_session); in ssh_block_statemach()
3155 curl_socket_t sock = conn->sock[FIRSTSOCKET]; in ssh_block_statemach()
3180 data->req.p.ssh = ssh = calloc(1, sizeof(struct SSHPROTO)); in ssh_setup_connection()
3197 struct connectdata *conn = data->conn; in ssh_tls_recv()
3198 Curl_recv *backup = conn->recv[0]; in ssh_tls_recv()
3199 struct ssh_conn *ssh = &conn->proto.sshc; in ssh_tls_recv()
3205 conn->recv[0] = ssh->tls_recv; in ssh_tls_recv()
3207 conn->recv[0] = backup; in ssh_tls_recv()
3209 return -EAGAIN; /* magic return code for libssh2 */ in ssh_tls_recv()
3211 return -1; /* generic error */ in ssh_tls_recv()
3222 struct connectdata *conn = data->conn; in ssh_tls_send()
3223 Curl_send *backup = conn->send[0]; in ssh_tls_send()
3224 struct ssh_conn *ssh = &conn->proto.sshc; in ssh_tls_send()
3230 conn->send[0] = ssh->tls_send; in ssh_tls_send()
3232 conn->send[0] = backup; in ssh_tls_send()
3234 return -EAGAIN; /* magic return code for libssh2 */ in ssh_tls_send()
3236 return -1; /* error */ in ssh_tls_send()
3244 * do protocol-specific actions at connect-time.
3253 struct connectdata *conn = data->conn; in ssh_connect()
3255 /* initialize per-handle data if not already */ in ssh_connect()
3256 if(!data->req.p.ssh) { in ssh_connect()
3266 sshc = &conn->proto.sshc; in ssh_connect()
3269 if(conn->user) { in ssh_connect()
3270 infof(data, "User: %s", conn->user); in ssh_connect()
3272 if(conn->passwd) { in ssh_connect()
3273 infof(data, "Password: %s", conn->passwd); in ssh_connect()
3275 sock = conn->sock[FIRSTSOCKET]; in ssh_connect()
3280 sshc->ssh_session = libssh2_session_init_ex(my_libssh2_malloc, in ssh_connect()
3284 if(!sshc->ssh_session) { in ssh_connect()
3289 /* Set the packet read timeout if the libssh2 version supports it */ in ssh_connect()
3291 if(data->set.server_response_timeout > 0) { in ssh_connect()
3292 libssh2_session_set_read_timeout(sshc->ssh_session, in ssh_connect()
3293 data->set.server_response_timeout / 1000); in ssh_connect()
3298 if(conn->http_proxy.proxytype == CURLPROXY_HTTPS) { in ssh_connect()
3300 Setup libssh2 callbacks to make it read/write TLS from the socket. in ssh_connect()
3313 libssh2_session_callback_set2(sshc->ssh_session, in ssh_connect()
3316 libssh2_session_callback_set2(sshc->ssh_session, in ssh_connect()
3323 * libssh2 has such an API... in ssh_connect()
3340 libssh2_session_callback_set(sshc->ssh_session, in ssh_connect()
3342 libssh2_session_callback_set(sshc->ssh_session, in ssh_connect()
3348 sshc->tls_recv = conn->recv[FIRSTSOCKET]; in ssh_connect()
3349 sshc->tls_send = conn->send[FIRSTSOCKET]; in ssh_connect()
3353 if(conn->handler->protocol & CURLPROTO_SCP) { in ssh_connect()
3354 conn->recv[FIRSTSOCKET] = scp_recv; in ssh_connect()
3355 conn->send[FIRSTSOCKET] = scp_send; in ssh_connect()
3358 conn->recv[FIRSTSOCKET] = sftp_recv; in ssh_connect()
3359 conn->send[FIRSTSOCKET] = sftp_send; in ssh_connect()
3362 if(data->set.ssh_compression) { in ssh_connect()
3364 if(libssh2_session_flag(sshc->ssh_session, LIBSSH2_FLAG_COMPRESS, 1) < 0) in ssh_connect()
3370 if(data->set.str[STRING_SSH_KNOWNHOSTS]) { in ssh_connect()
3372 sshc->kh = libssh2_knownhost_init(sshc->ssh_session); in ssh_connect()
3373 if(!sshc->kh) { in ssh_connect()
3374 libssh2_session_free(sshc->ssh_session); in ssh_connect()
3375 sshc->ssh_session = NULL; in ssh_connect()
3380 rc = libssh2_knownhost_readfile(sshc->kh, in ssh_connect()
3381 data->set.str[STRING_SSH_KNOWNHOSTS], in ssh_connect()
3385 data->set.str[STRING_SSH_KNOWNHOSTS]); in ssh_connect()
3390 libssh2_trace(sshc->ssh_session, ~0); in ssh_connect()
3424 /* run the state-machine */ in scp_perform()
3427 *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET); in scp_perform()
3458 struct connectdata *conn = data->conn; in ssh_do()
3459 struct ssh_conn *sshc = &conn->proto.sshc; in ssh_do()
3463 data->req.size = -1; /* make sure this is unknown at this point */ in ssh_do()
3465 sshc->actualcode = CURLE_OK; /* reset error code */ in ssh_do()
3466 sshc->secondCreateDirs = 0; /* reset the create dir attempt state in ssh_do()
3471 Curl_pgrsSetUploadSize(data, -1); in ssh_do()
3472 Curl_pgrsSetDownloadSize(data, -1); in ssh_do()
3474 if(conn->handler->protocol & CURLPROTO_SCP) in ssh_do()
3490 struct ssh_conn *sshc = &conn->proto.sshc; in scp_disconnect()
3493 if(sshc->ssh_session) { in scp_disconnect()
3507 struct SSHPROTO *sshp = data->req.p.ssh; in ssh_done()
3508 struct connectdata *conn = data->conn; in ssh_done()
3511 /* run the state-machine */ in ssh_done()
3516 Curl_safefree(sshp->path); in ssh_done()
3517 Curl_safefree(sshp->readdir_filename); in ssh_done()
3518 Curl_safefree(sshp->readdir_longentry); in ssh_done()
3519 Curl_dyn_free(&sshp->readdir); in ssh_done()
3524 data->req.keepon = 0; /* clear all bits */ in ssh_done()
3545 struct connectdata *conn = data->conn; in scp_send()
3546 struct ssh_conn *sshc = &conn->proto.sshc; in scp_send()
3550 nwrite = (ssize_t) libssh2_channel_write(sshc->ssh_channel, mem, len); in scp_send()
3560 nwrite = -1; in scp_send()
3570 struct connectdata *conn = data->conn; in scp_recv()
3571 struct ssh_conn *sshc = &conn->proto.sshc; in scp_recv()
3575 nread = (ssize_t) libssh2_channel_read(sshc->ssh_channel, mem, len); in scp_recv()
3580 nread = -1; in scp_recv()
3613 /* run the state-machine */ in sftp_perform()
3616 *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET); in sftp_perform()
3644 struct ssh_conn *sshc = &conn->proto.sshc; in sftp_disconnect()
3649 if(sshc->ssh_session) { in sftp_disconnect()
3664 struct connectdata *conn = data->conn; in sftp_done()
3665 struct ssh_conn *sshc = &conn->proto.sshc; in sftp_done()
3671 if(!premature && data->set.postquote && !conn->bits.retry) in sftp_done()
3672 sshc->nextstate = SSH_SFTP_POSTQUOTE_INIT; in sftp_done()
3683 struct connectdata *conn = data->conn; in sftp_send()
3684 struct ssh_conn *sshc = &conn->proto.sshc; in sftp_send()
3687 nwrite = libssh2_sftp_write(sshc->sftp_handle, mem, len); in sftp_send()
3697 nwrite = -1; in sftp_send()
3711 struct connectdata *conn = data->conn; in sftp_recv()
3712 struct ssh_conn *sshc = &conn->proto.sshc; in sftp_recv()
3715 nread = libssh2_sftp_read(sshc->sftp_handle, mem, len); in sftp_recv()
3721 nread = -1; in sftp_recv()
3793 return "Unknown error in libssh2"; in sftp_libssh2_strerror()
3816 (void)msnprintf(buffer, buflen, "libssh2/%s", CURL_LIBSSH2_VERSION); in Curl_ssh_version()
3819 /* The SSH session is associated with the *CONNECTION* but the callback user
3827 if(conn->handler->protocol & PROTO_FAMILY_SSH) { in ssh_attach()
3828 struct ssh_conn *sshc = &conn->proto.sshc; in ssh_attach()
3829 if(sshc->ssh_session) { in ssh_attach()
3830 /* only re-attach if the session already exists */ in ssh_attach()
3831 void **abstract = libssh2_session_abstract(sshc->ssh_session); in ssh_attach()