? .gdb_history ? cURL ? compile ? libssh2_patch.txt ? makePatch.sh ? rc.conf2 ? lib/scp.c ? lib/sftp.c ? src/.gdb_history Index: lib/sendf.c =================================================================== RCS file: /cvsroot/curl/curl/lib/sendf.c,v retrieving revision 1.114 diff -u -r1.114 sendf.c --- lib/sendf.c 11 Nov 2006 21:34:43 -0000 1.114 +++ lib/sendf.c 14 Nov 2006 00:21:42 -0000 @@ -364,6 +364,8 @@ #ifdef USE_LIBSSH2 else if (conn->protocol & PROT_SCP) bytes_written = Curl_scp_send(conn, num, mem, len); + else if (conn->protocol & PROT_SFTP) + bytes_written = Curl_sftp_send(conn, num, mem, len); #endif /* !USE_LIBSSH2 */ else if(conn->sec_complete) /* only TRUE if krb4 enabled */ @@ -522,6 +524,9 @@ /* TODO: return CURLE_OK also for nread <= 0 read failures and timeouts ? */ } + else if (conn->protocol & PROT_SFTP) { + nread = Curl_sftp_recv(conn, num, conn->master_buffer, bytesfromsocket); + } #endif /* !USE_LIBSSH2 */ else { if(conn->sec_complete) Index: lib/ssh.c =================================================================== RCS file: /cvsroot/curl/curl/lib/ssh.c,v retrieving revision 1.3 diff -u -r1.3 ssh.c --- lib/ssh.c 6 Nov 2006 18:26:36 -0000 1.3 +++ lib/ssh.c 14 Nov 2006 00:21:43 -0000 @@ -51,6 +51,10 @@ #include #endif +#ifdef HAVE_TIME_H +#include +#endif + #ifdef WIN32 #else /* probably some kind of unix */ @@ -130,14 +134,6 @@ #include "memdebug.h" #endif -#ifndef S_IRGRP -#define S_IRGRP 0 -#endif - -#ifndef S_IROTH -#define S_IROTH 0 -#endif - static LIBSSH2_ALLOC_FUNC(libssh2_malloc); static LIBSSH2_REALLOC_FUNC(libssh2_realloc); static LIBSSH2_FREE_FUNC(libssh2_free); @@ -174,10 +170,10 @@ static CURLcode libssh2_error_to_CURLE(struct connectdata *conn) { int errorcode; - struct SCPPROTO *scp = conn->data->reqdata.proto.scp; + struct SSHPROTO *scp = conn->data->reqdata.proto.ssh; /* Get the libssh2 error code and string */ - errorcode = libssh2_session_last_error(scp->scpSession, &scp->errorstr, NULL, + errorcode = libssh2_session_last_error(scp->sshSession, &scp->errorstr, NULL, 0); if (errorcode == LIBSSH2_FX_OK) return CURLE_OK; @@ -209,111 +205,102 @@ (void)abstract; } -static CURLcode scp_init(struct connectdata *conn) +static CURLcode ssh_init(struct connectdata *conn) { struct SessionHandle *data = conn->data; - struct SCPPROTO *scp; - if (data->reqdata.proto.scp) + struct SSHPROTO *ssh; + if (data->reqdata.proto.ssh) return CURLE_OK; - - scp = (struct SCPPROTO *)calloc(sizeof(struct SCPPROTO), 1); - if (!scp) + + ssh = (struct SSHPROTO *)calloc(sizeof(struct SSHPROTO), 1); + if (!ssh) return CURLE_OUT_OF_MEMORY; - - data->reqdata.proto.scp = scp; - - /* get some initial data into the scp struct */ - scp->bytecountp = &data->reqdata.keep.bytecount; - + + 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 */ - scp->user = conn->user; - scp->passwd = conn->passwd; - - scp->errorstr = NULL; - + ssh->user = conn->user; + ssh->passwd = conn->passwd; + + ssh->errorstr = NULL; + + ssh->sshSession = NULL; + ssh->sshChannel = NULL; + ssh->sftpSession = NULL; + ssh->sftpHandle = NULL; + return CURLE_OK; } /* - * Curl_scp_connect() gets called from Curl_protocol_connect() to allow us to + * Curl_ssh_connect() gets called from Curl_protocol_connect() to allow us to * do protocol-specific actions at connect-time. */ -CURLcode Curl_scp_connect(struct connectdata *conn, bool *done) +CURLcode Curl_ssh_connect(struct connectdata *conn, bool *done) { int i; - struct SCPPROTO *scp; + struct SSHPROTO *ssh; const char *fingerprint; const char *authlist; char *home; char rsa_pub[PATH_MAX]; char rsa[PATH_MAX]; + char tempHome[PATH_MAX]; curl_socket_t sock; char *real_path; char *working_path; bool authed = FALSE; CURLcode result; struct SessionHandle *data = conn->data; - - result = scp_init(conn); + + rsa_pub[0] = rsa[0] = '\0'; + + result = ssh_init(conn); if (result) return result; - - rsa_pub[0] = rsa[0] = '\0'; - - scp = data->reqdata.proto.scp; - + + ssh = data->reqdata.proto.ssh; + working_path = curl_easy_unescape(data, data->reqdata.path, 0, NULL); if (!working_path) return CURLE_OUT_OF_MEMORY; - - real_path = (char *)malloc(strlen(working_path)+1); - if (real_path == NULL) { - Curl_safefree(working_path); - return CURLE_OUT_OF_MEMORY; - } - /* Check for /~/ , indicating realative to the users home directory */ - if (working_path[1] == '~') - /* It is referenced to the home directory, so strip the leading '/' */ - memcpy(real_path, working_path+1, 1+strlen(working_path)-1); - else - memcpy(real_path, working_path, 1+strlen(working_path)); - - Curl_safefree(working_path); - scp->path = real_path; - + #ifdef CURL_LIBSSH2_DEBUG - if (scp->user) { - infof(data, "User: %s\n", scp->user); + if (ssh->user) { + infof(data, "User: %s\n", ssh->user); } - if (scp->passwd) { - infof(data, "Password: %s\n", scp->passwd); + if (ssh->passwd) { + infof(data, "Password: %s\n", ssh->passwd); } #endif /* CURL_LIBSSH2_DEBUG */ sock = conn->sock[FIRSTSOCKET]; - scp->scpSession = libssh2_session_init_ex(libssh2_malloc, libssh2_free, + ssh->sshSession = libssh2_session_init_ex(libssh2_malloc, libssh2_free, libssh2_realloc, NULL); - if (scp->scpSession == NULL) { + if (ssh->sshSession == NULL) { failf(data, "Failure initialising ssh session\n"); return CURLE_FAILED_INIT; } #ifdef CURL_LIBSSH2_DEBUG infof(data, "Socket: %d\n", sock); #endif /* CURL_LIBSSH2_DEBUG */ - - if (libssh2_session_startup(scp->scpSession, sock)) { + + if (libssh2_session_startup(ssh->sshSession, sock)) { failf(data, "Failure establishing ssh session\n"); return CURLE_FAILED_INIT; } - + /* * Before we authenticate we should check the hostkey's fingerprint against * our known hosts. How that is handled (reading from file, whatever) is * up to us. As for know not much is implemented, besides showing how to * get the fingerprint. */ - fingerprint = libssh2_hostkey_hash(scp->scpSession, + fingerprint = libssh2_hostkey_hash(ssh->sshSession, LIBSSH2_HOSTKEY_HASH_MD5); - + #ifdef CURL_LIBSSH2_DEBUG /* The fingerprint points to static storage (!), don't free() it. */ for (i = 0; i < 16; i++) { @@ -321,9 +308,9 @@ } infof(data, "\n"); #endif /* CURL_LIBSSH2_DEBUG */ - + /* TBD - methods to check the host keys need to be done */ - + /* * Figure out authentication methods * NB: As soon as we have provided a username to an openssh server we must @@ -333,9 +320,9 @@ * presumably with a blank username. That won't work in my experience. * So always specify it here. */ - authlist = libssh2_userauth_list(scp->scpSession, scp->user, - strlen(scp->user)); - + authlist = libssh2_userauth_list(ssh->sshSession, ssh->user, + strlen(ssh->user)); + /* * Check the supported auth types in the order I feel is most secure with the * requested type of authentication @@ -343,26 +330,25 @@ if ((data->set.ssh_auth_types & CURLSSH_AUTH_PUBLICKEY) && (strstr(authlist, "publickey") != NULL)) { /* To ponder about: should really the lib be messing about with the HOME - environment variable etc? */ + environment variable etc? */ home = curl_getenv("HOME"); - + if (data->set.ssh_public_key) snprintf(rsa_pub, sizeof(rsa_pub), "%s", data->set.ssh_public_key); - else if(home) + else if (home) snprintf(rsa_pub, sizeof(rsa_pub), "%s/.ssh/id_dsa.pub", home); - - if(data->set.ssh_private_key) + + if (data->set.ssh_private_key) snprintf(rsa, sizeof(rsa), "%s", data->set.ssh_private_key); - else if(home) { + else if (home) snprintf(rsa, sizeof(rsa), "%s/.ssh/id_dsa", home); - } - + curl_free(home); - + if (rsa_pub[0]) { /* The function below checks if the files exists, no need to stat() here. - */ - if (libssh2_userauth_publickey_fromfile(scp->scpSession, scp->user, + */ + if (libssh2_userauth_publickey_fromfile(ssh->sshSession, ssh->user, rsa_pub, rsa, "") == 0) { authed = TRUE; } @@ -371,7 +357,7 @@ if (!authed && (data->set.ssh_auth_types & CURLSSH_AUTH_PASSWORD) && (strstr(authlist, "password") != NULL)) { - if (libssh2_userauth_password(scp->scpSession, scp->user, scp->passwd) + if (libssh2_userauth_password(ssh->sshSession, ssh->user, ssh->passwd) == 0) { authed = TRUE; } @@ -382,26 +368,99 @@ if (!authed && (data->set.ssh_auth_types & CURLSSH_AUTH_KEYBOARD) && (strstr(authlist, "keyboard-interactive") != NULL)) { /* Authentication failed. Continue with keyboard-interactive now. */ - auth.user = scp->user; - auth.pw = scp->passwd; - if (libssh2_userauth_keyboard_interactive_ex(scp->scpSession, scp->user, - strlen(scp->user), + auth.user = ssh->user; + auth.pw = ssh->passwd; + if (libssh2_userauth_keyboard_interactive_ex(ssh->sshSession, ssh->user, + strlen(ssh->user), &kbd_callback) == 0) { authed = TRUE; } } - + if (!authed) { failf(data, "Authentication failure\n"); return CURLE_FAILED_INIT; } - + /* * At this point we have an authenticated ssh session. */ conn->sockfd = sock; conn->writesockfd = CURL_SOCKET_BAD; - + + if (conn->protocol == PROT_SFTP) { + /* + * Start the libssh2 sftp session + */ + ssh->sftpSession = libssh2_sftp_init(ssh->sshSession); + if (ssh->sftpSession == NULL) { + failf(data, "Failure initialising sftp session\n"); + return CURLE_FAILED_INIT; + } + + /* + * Get the "home" directory + */ + i = libssh2_sftp_realpath(ssh->sftpSession, ".", tempHome, PATH_MAX-1); + if (i > 0) { + /* It seems that this string is not always NULL terminated */ + tempHome[i] = '\0'; + if ((ssh->homeDirectory = (char *)malloc(i + 1)) == NULL) { + return CURLE_OUT_OF_MEMORY; + } + memcpy(ssh->homeDirectory, tempHome, i+1); + } else { + /* Return the error type */ + i = libssh2_sftp_last_error(ssh->sftpSession); + fprintf(stderr, "error = %d\n", i); + } + } + + /* Check for /~/ , indicating realative to the users home directory */ + if (conn->protocol == PROT_SCP) { + real_path = (char *)malloc(strlen(working_path)+1); + if (real_path == NULL) { + Curl_safefree(working_path); + return CURLE_OUT_OF_MEMORY; + } + if (working_path[1] == '~') + /* It is referenced to the home directory, so strip the leading '/' */ + memcpy(real_path, working_path+1, 1+strlen(working_path)-1); + else + memcpy(real_path, working_path, 1+strlen(working_path)); + } + else if (conn->protocol == PROT_SFTP) { + if (working_path[1] == '~') { + real_path = (char *)malloc(strlen(ssh->homeDirectory) + + strlen(working_path)+1); + if (real_path == NULL) { + Curl_safefree(working_path); + return CURLE_OUT_OF_MEMORY; + } + /* It is referenced to the home directory, so strip the leading '/' */ + memcpy(real_path, ssh->homeDirectory, strlen(ssh->homeDirectory)); + real_path[strlen(ssh->homeDirectory)] = '/'; + real_path[strlen(ssh->homeDirectory)+1] = '\0'; + if (strlen(working_path) > 3) { + memcpy(real_path+strlen(ssh->homeDirectory)+1, working_path+3, + 1+strlen(working_path)-3); + } + } + else { + real_path = (char *)malloc(strlen(working_path)+1); + if (real_path == NULL) { + Curl_safefree(working_path); + return CURLE_OUT_OF_MEMORY; + } + memcpy(real_path, working_path, 1+strlen(working_path)); + } + } else { + return CURLE_FAILED_INIT; + } + + Curl_safefree(working_path); + ssh->path = real_path; + *done = TRUE; return CURLE_OK; } @@ -409,7 +468,7 @@ CURLcode Curl_scp_do(struct connectdata *conn, bool *done) { struct stat sb; - struct SCPPROTO *scp = conn->data->reqdata.proto.scp; + struct SSHPROTO *scp = conn->data->reqdata.proto.ssh; CURLcode res = CURLE_OK; *done = TRUE; /* unconditionally */ @@ -421,10 +480,13 @@ * If this is not done the destination file will be named the * same name as the last directory in the path. */ - scp->scpChannel = libssh2_scp_send_ex(scp->scpSession, scp->path, - S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, + scp->sshChannel = libssh2_scp_send_ex(scp->sshSession, scp->path, + LIBSSH2_SFTP_S_IRUSR| + LIBSSH2_SFTP_S_IWUSR| + LIBSSH2_SFTP_S_IRGRP| + LIBSSH2_SFTP_S_IROTH, conn->data->set.infilesize, 0, 0); - if (scp->scpChannel == NULL) { + if (scp->sshChannel == NULL) { return CURLE_FAILED_INIT; } conn->writesockfd = conn->sockfd; @@ -432,11 +494,11 @@ } else { /* - * We must check the remote file, if it is a directory I have no idea - * what I will do until the scp "-r" option is supported + * We must check the remote file, if it is a directory no vaules will + * be set in sb */ memset(&sb, 0, sizeof(struct stat)); - if ((scp->scpChannel = libssh2_scp_recv(scp->scpSession, scp->path, &sb)) + if ((scp->sshChannel = libssh2_scp_recv(scp->sshSession, scp->path, &sb)) == NULL) { if ((sb.st_mode == 0) && (sb.st_atime == 0) && (sb.st_mtime == 0) && (sb.st_size == 0)) { @@ -454,24 +516,24 @@ CURLcode Curl_scp_done(struct connectdata *conn, CURLcode status) { - struct SCPPROTO *scp = conn->data->reqdata.proto.scp; + struct SSHPROTO *scp = conn->data->reqdata.proto.ssh; Curl_safefree(scp->freepath); scp->freepath = NULL; - if (scp->scpChannel) { - if (libssh2_channel_close(scp->scpChannel) < 0) { + if (scp->sshChannel) { + if (libssh2_channel_close(scp->sshChannel) < 0) { failf(conn->data, "Failed to stop libssh2 channel subsystem\n"); } } - if (scp->scpSession) { - libssh2_session_disconnect(scp->scpSession, "Shutdown"); - libssh2_session_free(scp->scpSession); + if (scp->sshSession) { + libssh2_session_disconnect(scp->sshSession, "Shutdown"); + libssh2_session_free(scp->sshSession); } - free(conn->data->reqdata.proto.scp); - conn->data->reqdata.proto.scp = NULL; + free(conn->data->reqdata.proto.ssh); + conn->data->reqdata.proto.ssh = NULL; Curl_pgrsDone(conn); (void)status; /* unused */ @@ -485,7 +547,7 @@ { ssize_t nwrite; - nwrite = libssh2_channel_write(conn->data->reqdata.proto.scp->scpChannel, + nwrite = libssh2_channel_write(conn->data->reqdata.proto.ssh->sshChannel, mem, len); (void)sockindex; return nwrite; @@ -500,7 +562,273 @@ { ssize_t nread; - nread = libssh2_channel_read(conn->data->reqdata.proto.scp->scpChannel, + nread = libssh2_channel_read(conn->data->reqdata.proto.ssh->sshChannel, + mem, len); + (void)sockindex; + return nread; +} + +/* + * =============== SFTP =============== + */ + +CURLcode Curl_sftp_do(struct connectdata *conn, bool *done) +{ + LIBSSH2_SFTP_ATTRIBUTES attrs; + struct SSHPROTO *sftp = conn->data->reqdata.proto.ssh; + CURLcode res = CURLE_OK; + + *done = TRUE; /* unconditionally */ + + if (conn->data->set.upload) { + /* + * 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->sftpHandle = + libssh2_sftp_open(sftp->sftpSession, sftp->path, + LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT, + LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| + LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); + if (sftp->sftpHandle == NULL) { + return CURLE_FAILED_INIT; + } + conn->writesockfd = conn->sockfd; + conn->sockfd = CURL_SOCKET_BAD; + } + else { + if (sftp->path[strlen(sftp->path)-1] == '/') { + /* + * This is a directory that we are trying to get, so produce a + * directory listing + */ + char filename[PATH_MAX+1]; + int len, totalLen, currLen; + char *line; + + if ((sftp->sftpHandle = + libssh2_sftp_opendir(sftp->sftpSession, sftp->path)) == NULL) + { + return CURLE_SSH; + } + + while ((len = libssh2_sftp_readdir(sftp->sftpHandle, filename, + PATH_MAX, &attrs)) > 0) + { + filename[len] = '\0'; + + if (conn->data->set.ftp_list_only) { + if ((attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) && + ((attrs.permissions & LIBSSH2_SFTP_S_IFMT) == + LIBSSH2_SFTP_S_IFDIR)) { + infof(conn->data, "%s\n", filename); + } + } else { + totalLen = 80 + len; + if ((line = (char *)malloc(totalLen)) == NULL) { + return CURLE_OUT_OF_MEMORY; + } + + if (attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) { + currLen = snprintf(line, totalLen, "---------- 1 %5d %5d", + attrs.uid, attrs.gid); + } else { + currLen = snprintf(line, totalLen, "---------- 1 %5d %5d", 0, 0); + } + if (attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) { + if ((attrs.permissions & LIBSSH2_SFTP_S_IFMT) == + LIBSSH2_SFTP_S_IFDIR) { + line[0] = 'd'; + } + else if ((attrs.permissions & LIBSSH2_SFTP_S_IFMT) == + LIBSSH2_SFTP_S_IFLNK) { + line[0] = 'l'; + } + else if ((attrs.permissions & LIBSSH2_SFTP_S_IFMT) == + LIBSSH2_SFTP_S_IFSOCK) { + line[0] = 's'; + } + else if ((attrs.permissions & LIBSSH2_SFTP_S_IFMT) == + LIBSSH2_SFTP_S_IFCHR) { + line[0] = 'c'; + } + else if ((attrs.permissions & LIBSSH2_SFTP_S_IFMT) == + LIBSSH2_SFTP_S_IFBLK) { + line[0] = 'b'; + } + if (attrs.permissions & LIBSSH2_SFTP_S_IRUSR) { + line[1] = 'r'; + } + if (attrs.permissions & LIBSSH2_SFTP_S_IWUSR) { + line[2] = 'w'; + } + if (attrs.permissions & LIBSSH2_SFTP_S_IXUSR) { + line[3] = 'x'; + } + if (attrs.permissions & LIBSSH2_SFTP_S_IRGRP) { + line[4] = 'r'; + } + if (attrs.permissions & LIBSSH2_SFTP_S_IWGRP) { + line[5] = 'w'; + } + if (attrs.permissions & LIBSSH2_SFTP_S_IXGRP) { + line[6] = 'x'; + } + if (attrs.permissions & LIBSSH2_SFTP_S_IROTH) { + line[7] = 'r'; + } + if (attrs.permissions & LIBSSH2_SFTP_S_IWOTH) { + line[8] = 'w'; + } + if (attrs.permissions & LIBSSH2_SFTP_S_IXOTH) { + line[9] = 'x'; + } + } + if (attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) { + currLen += snprintf(line+currLen, totalLen-currLen, "%11lld", + attrs.filesize); + } + if (attrs.flags & LIBSSH2_SFTP_ATTR_ACMODTIME) { + const char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; + struct tm *nowParts; + time_t now, remoteTime; + + now = time(NULL); + remoteTime = (time_t)attrs.mtime; + nowParts = localtime(&remoteTime); + + if ((time_t)attrs.mtime > (now - (3600 * 24 * 180))) { + currLen += snprintf(line+currLen, totalLen-currLen, + " %s %2d %2d:%02d", months[nowParts->tm_mon], + nowParts->tm_mday, nowParts->tm_hour, + nowParts->tm_min); + } else { + currLen += snprintf(line+currLen, totalLen-currLen, + " %s %2d %5d", months[nowParts->tm_mon], + nowParts->tm_mday, 1900+nowParts->tm_year); + } + } + currLen += snprintf(line+currLen, totalLen-currLen, " %s", filename); + if ((attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) && + ((attrs.permissions & LIBSSH2_SFTP_S_IFMT) == + LIBSSH2_SFTP_S_IFLNK)) { + char linkPath[PATH_MAX + 1]; + + snprintf(linkPath, PATH_MAX, "%s%s", sftp->path, filename); + len = libssh2_sftp_readlink(sftp->sftpSession, linkPath, filename, + PATH_MAX); + if ((line = realloc(line, totalLen + 4 + len)) == NULL) { + return CURLE_OUT_OF_MEMORY; + } + currLen += snprintf(line+currLen, totalLen-currLen, " -> %s", + filename); + } + + infof(conn->data, "%s\n", line); + free(line); + } + } + libssh2_sftp_closedir(sftp->sftpHandle); + sftp->sftpHandle = NULL; + + /* no data to transfer */ + res = Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL); + } + else { + /* + * Work on getting the specified file + */ + sftp->sftpHandle = + libssh2_sftp_open(sftp->sftpSession, sftp->path, LIBSSH2_FXF_READ, + LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| + LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); + if (sftp->sftpHandle == NULL) { + return CURLE_SSH; + } + if (libssh2_sftp_stat(sftp->sftpSession, sftp->path, &attrs)) { + /* + * libssh2_sftp_open() didn't return an error, so maybe the server + * just doesn't support stat() + */ + conn->data->reqdata.size = -1; + conn->data->reqdata.maxdownload = -1; + } else { + conn->data->reqdata.size = attrs.filesize; + conn->data->reqdata.maxdownload = attrs.filesize; + } + } + } + + return res; +} + +CURLcode Curl_sftp_done(struct connectdata *conn, CURLcode status) +{ + struct SSHPROTO *sftp = conn->data->reqdata.proto.ssh; + + Curl_safefree(sftp->freepath); + sftp->freepath = NULL; + + Curl_safefree(sftp->homeDirectory); + sftp->homeDirectory = NULL; + + if (sftp->sftpHandle) { + if (libssh2_sftp_close(sftp->sftpHandle) < 0) { + failf(conn->data, "Failed to close libssh2 file\n"); + } + } + + if (sftp->sftpSession) { + if (libssh2_sftp_shutdown(sftp->sftpSession) < 0) { + failf(conn->data, "Failed to stop libssh2 sftp subsystem\n"); + } + } + + if (sftp->sshChannel) { + if (libssh2_channel_close(sftp->sshChannel) < 0) { + failf(conn->data, "Failed to stop libssh2 channel subsystem\n"); + } + } + + if (sftp->sshSession) { + libssh2_session_disconnect(sftp->sshSession, "Shutdown"); + libssh2_session_free(sftp->sshSession); + } + + free(conn->data->reqdata.proto.ssh); + conn->data->reqdata.proto.ssh = NULL; + Curl_pgrsDone(conn); + + (void)status; /* unused */ + + return CURLE_OK; +} + +/* return number of received (decrypted) bytes */ +int Curl_sftp_send(struct connectdata *conn, int sockindex, + void *mem, size_t len) +{ + ssize_t nwrite; + + nwrite = libssh2_sftp_write(conn->data->reqdata.proto.ssh->sftpHandle, + mem, len); + (void)sockindex; + return nwrite; +} + +/* + * If the read would block (EWOULDBLOCK) we return -1. Otherwise we return + * a regular CURLcode value. + */ +int Curl_sftp_recv(struct connectdata *conn, int sockindex, + char *mem, size_t len) +{ + ssize_t nread; + + nread = libssh2_sftp_read(conn->data->reqdata.proto.ssh->sftpHandle, mem, len); (void)sockindex; return nread; Index: lib/ssh.h =================================================================== RCS file: /cvsroot/curl/curl/lib/ssh.h,v retrieving revision 1.1 diff -u -r1.1 ssh.h --- lib/ssh.h 2 Nov 2006 21:56:43 -0000 1.1 +++ lib/ssh.h 14 Nov 2006 00:21:43 -0000 @@ -1,5 +1,5 @@ -#ifndef __SFTP_H -#define __SFTP_H +#ifndef __SSH_H +#define __SSH_H /*************************************************************************** * _ _ ____ _ @@ -26,7 +26,8 @@ #ifdef USE_LIBSSH2 -CURLcode Curl_scp_connect(struct connectdata *conn, bool *done); +CURLcode Curl_ssh_connect(struct connectdata *conn, bool *done); + CURLcode Curl_scp_do(struct connectdata *conn, bool *done); CURLcode Curl_scp_done(struct connectdata *conn, CURLcode); @@ -35,6 +36,14 @@ int Curl_scp_recv(struct connectdata *conn, int sockindex, char *mem, size_t len); -#endif +CURLcode Curl_sftp_do(struct connectdata *conn, bool *done); +CURLcode Curl_sftp_done(struct connectdata *conn, CURLcode); + +int Curl_sftp_send(struct connectdata *conn, int sockindex, + void *mem, size_t len); +int Curl_sftp_recv(struct connectdata *conn, int sockindex, + char *mem, size_t len); #endif /* USE_LIBSSH2 */ + +#endif /* __SSH_H */ Index: lib/url.c =================================================================== RCS file: /cvsroot/curl/curl/lib/url.c,v retrieving revision 1.565 diff -u -r1.565 url.c --- lib/url.c 9 Nov 2006 21:54:33 -0000 1.565 +++ lib/url.c 14 Nov 2006 00:21:46 -0000 @@ -3246,7 +3246,7 @@ conn->port = PORT_SSH; conn->remote_port = PORT_SSH; conn->protocol = PROT_SCP; - conn->curl_connect = Curl_scp_connect; /* ssh_connect? */ + conn->curl_connect = Curl_ssh_connect; /* ssh_connect? */ conn->curl_do = Curl_scp_do; conn->curl_done = Curl_scp_done; conn->curl_do_more = (Curl_do_more_func)ZERO_NULL; @@ -3256,7 +3256,22 @@ return CURLE_UNSUPPORTED_PROTOCOL; #endif } - else { + else if (strequal(conn->protostr, "SFTP")) { +#ifdef USE_LIBSSH2 + conn->port = PORT_SSH; + conn->remote_port = PORT_SSH; + conn->protocol = PROT_SFTP; + conn->curl_connect = Curl_ssh_connect; /* ssh_connect? */ + conn->curl_do = Curl_sftp_do; + conn->curl_done = Curl_sftp_done; + conn->curl_do_more = (Curl_do_more_func)NULL; +#else + failf(data, LIBCURL_NAME + " was built without LIBSSH2, scp: not supported!"); + return CURLE_UNSUPPORTED_PROTOCOL; +#endif +} +else { /* We fell through all checks and thus we don't support the specified protocol */ failf(data, "Unsupported protocol: %s", conn->protostr); @@ -3422,9 +3437,9 @@ user[0] =0; /* to make everything well-defined */ passwd[0]=0; - if (conn->protocol & (PROT_FTP|PROT_HTTP|PROT_SCP)) { - /* This is a FTP or HTTP URL, we will now try to extract the possible - * user+password pair in a string like: + if (conn->protocol & (PROT_FTP|PROT_HTTP|PROT_SCP|PROT_SFTP)) { + /* This is a FTP, HTTP, SCP or SFTP URL, we will now try to extract the + * possible user+password pair in a string like: * ftp://user:password@ftp.my.site:8021/README */ char *ptr=strchr(conn->host.name, '@'); char *userpass = conn->host.name; Index: lib/urldata.h =================================================================== RCS file: /cvsroot/curl/curl/lib/urldata.h,v retrieving revision 1.308 diff -u -r1.308 urldata.h --- lib/urldata.h 3 Nov 2006 12:43:56 -0000 1.308 +++ lib/urldata.h 14 Nov 2006 00:21:47 -0000 @@ -398,7 +398,7 @@ ftpstate state; /* always use ftp.c:state() to change state! */ }; -struct SCPPROTO { +struct SSHPROTO { curl_off_t *bytecountp; char *user; char *passwd; @@ -406,10 +406,13 @@ char *freepath; /* pointer to the allocated block we must free, this might differ from the 'path' pointer */ + char *homeDirectory; char *errorstr; #ifdef USE_LIBSSH2 - LIBSSH2_SESSION *scpSession; /* Secure Shell session */ - LIBSSH2_CHANNEL *scpChannel; /* SCP channel handle */ + LIBSSH2_SESSION *sshSession; /* Secure Shell session */ + LIBSSH2_CHANNEL *sshChannel; /* Secure Shell channel handle */ + LIBSSH2_SFTP *sftpSession; /* SFTP handle */ + LIBSSH2_SFTP_HANDLE *sftpHandle; #endif /* USE_LIBSSH2 */ }; @@ -673,7 +676,7 @@ struct FILEPROTO *file; void *telnet; /* private for telnet.c-eyes only */ void *generic; - struct SCPPROTO *scp; + struct SSHPROTO *ssh; } proto; }; @@ -709,6 +712,7 @@ #define PROT_SSL (1<<10) /* protocol requires SSL */ #define PROT_TFTP (1<<11) #define PROT_SCP (1<<12) +#define PROT_SFTP (1<<13) /* 'dns_entry' is the particular host we use. This points to an entry in the DNS cache and it will not get pruned while locked. It gets unlocked in Index: lib/version.c =================================================================== RCS file: /cvsroot/curl/curl/lib/version.c,v retrieving revision 1.51 diff -u -r1.51 version.c --- lib/version.c 2 Nov 2006 21:56:44 -0000 1.51 +++ lib/version.c 14 Nov 2006 00:21:47 -0000 @@ -138,6 +138,7 @@ #ifdef USE_LIBSSH2 "scp", + "sftp", #endif NULL