Newer
Older
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
/* The get_pathname() function is being borrowed from OpenSSH sftp.c
version 4.6p1. */
/*
* Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
static int
get_pathname(const char **cpp, char **path)
{
const char *cp = *cpp, *end;
char quot;
u_int i, j;
static const char * const WHITESPACE = " \t\r\n";
cp += strspn(cp, WHITESPACE);
if (!*cp) {
*cpp = cp;
*path = NULL;
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
}
*path = malloc(strlen(cp) + 1);
if (*path == NULL)
return CURLE_OUT_OF_MEMORY;
/* Check for quoted filenames */
if (*cp == '\"' || *cp == '\'') {
quot = *cp++;
/* Search for terminating quote, unescape some chars */
for (i = j = 0; i <= strlen(cp); i++) {
if (cp[i] == quot) { /* Found quote */
i++;
(*path)[j] = '\0';
break;
}
if (cp[i] == '\0') { /* End of string */
/*error("Unterminated quote");*/
goto fail;
}
if (cp[i] == '\\') { /* Escaped characters */
i++;
if (cp[i] != '\'' && cp[i] != '\"' &&
cp[i] != '\\') {
/*error("Bad escaped character '\\%c'",
cp[i]);*/
goto fail;
}
}
(*path)[j++] = cp[i];
}
if (j == 0) {
/*error("Empty quotes");*/
goto fail;
}
*cpp = cp + i + strspn(cp + i, WHITESPACE);
}
else {
/* Read to end of filename */
end = strpbrk(cp, WHITESPACE);
if (end == NULL)
end = strchr(cp, '\0');
*cpp = end + strspn(end, WHITESPACE);
memcpy(*path, cp, end - cp);
(*path)[end - cp] = '\0';
}
return (0);
fail:
James Housley
committed
Curl_safefree(*path);
*path = NULL;
return CURLE_FTP_QUOTE_ERROR;
}
static const char *sftp_libssh2_strerror(unsigned long err)
{
switch (err) {
James Housley
committed
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
case LIBSSH2_FX_NO_SUCH_FILE:
return "No such file or directory";
case LIBSSH2_FX_PERMISSION_DENIED:
return "Permission denied";
case LIBSSH2_FX_FAILURE:
return "Operation failed";
case LIBSSH2_FX_BAD_MESSAGE:
return "Bad message from SFTP server";
case LIBSSH2_FX_NO_CONNECTION:
return "Not connected to SFTP server";
case LIBSSH2_FX_CONNECTION_LOST:
return "Connection to SFTP server lost";
case LIBSSH2_FX_OP_UNSUPPORTED:
return "Operation not supported by SFTP server";
case LIBSSH2_FX_INVALID_HANDLE:
return "Invalid handle";
case LIBSSH2_FX_NO_SUCH_PATH:
return "No such file or directory";
case LIBSSH2_FX_FILE_ALREADY_EXISTS:
return "File already exists";
case LIBSSH2_FX_WRITE_PROTECT:
return "File is write protected";
case LIBSSH2_FX_NO_MEDIA:
return "No media";
case LIBSSH2_FX_NO_SPACE_ON_FILESYSTEM:
return "Disk full";
case LIBSSH2_FX_QUOTA_EXCEEDED:
return "User quota exceeded";
case LIBSSH2_FX_UNKNOWN_PRINCIPLE:
return "Unknown principle";
case LIBSSH2_FX_LOCK_CONFlICT:
return "File lock conflict";
case LIBSSH2_FX_DIR_NOT_EMPTY:
return "Directory not empty";
case LIBSSH2_FX_NOT_A_DIRECTORY:
return "Not a directory";
case LIBSSH2_FX_INVALID_FILENAME:
return "Invalid filename";
case LIBSSH2_FX_LINK_LOOP:
return "Link points to itself";
}
return "Unknown error in libssh2";
}
Guenter Knauf
committed
#if (LIBSSH2_APINO < 200706012030L)
/* BLOCKING */
static CURLcode sftp_sendquote(struct connectdata *conn,
struct curl_slist *quote)
{
James Housley
committed
struct curl_slist *item = quote;
const char *cp;
long err;
struct SessionHandle *data = conn->data;
LIBSSH2_SFTP *sftp_session = data->reqdata.proto.ssh->sftp_session;
while (item) {
James Housley
committed
/*
* Support some of the "FTP" commands
*/
if (curl_strnequal(item->data, "PWD", 3)) {
/* output debug output if that is requested */
if (data->set.verbose) {
char tmp[PATH_MAX+1];
Curl_debug(data, CURLINFO_HEADER_OUT, (char *)"PWD\n", 4, conn);
snprintf(tmp, PATH_MAX, "257 \"%s\" is current directory.\n",
data->reqdata.proto.ssh->path);
Curl_debug(data, CURLINFO_HEADER_IN, tmp, strlen(tmp), conn);
}
}
else if (item->data) {
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
char *path1 = NULL;
char *path2 = NULL;
/* the arguments following the command must be separated from the
command with a space so we can check for it unconditionally */
cp = strchr(item->data, ' ');
if (cp == NULL) {
failf(data, "Syntax error in SFTP command. Supply parameter(s)!");
return CURLE_FTP_QUOTE_ERROR;
}
/* also, every command takes at least one argument so we get that first
argument right now */
err = get_pathname(&cp, &path1);
if (err) {
if (err == CURLE_OUT_OF_MEMORY)
failf(data, "Out of memory");
else
failf(data, "Syntax error: Bad first parameter");
return err;
}
/* SFTP is a binary protocol, so we don't send text commands to the
server. Instead, we scan for commands for commands used by OpenSSH's
sftp program and call the appropriate libssh2 functions. */
if (curl_strnequal(item->data, "chgrp ", 6) ||
curl_strnequal(item->data, "chmod ", 6) ||
curl_strnequal(item->data, "chown ", 6) ) { /* attribute change */
LIBSSH2_SFTP_ATTRIBUTES attrs;
/* path1 contains the mode to set */
err = get_pathname(&cp, &path2); /* get the destination */
if (err) {
if (err == CURLE_OUT_OF_MEMORY)
failf(data, "Out of memory");
else
failf(data,
"Syntax error in chgrp/chmod/chown: Bad second parameter");
James Housley
committed
Curl_safefree(path1);
return err;
}
memset(&attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
if (libssh2_sftp_stat(sftp_session,
path2, &attrs) != 0) { /* get those attributes */
err = libssh2_sftp_last_error(sftp_session);
James Housley
committed
Curl_safefree(path1);
Curl_safefree(path2);
failf(data, "Attempt to get SFTP stats failed: %s",
sftp_libssh2_strerror(err));
return CURLE_FTP_QUOTE_ERROR;
}
/* Now set the new attributes... */
if (curl_strnequal(item->data, "chgrp", 5)) {
attrs.gid = strtol(path1, NULL, 10);
if (attrs.gid == 0 && !ISDIGIT(path1[0])) {
James Housley
committed
Curl_safefree(path1);
Curl_safefree(path2);
failf(data, "Syntax error: chgrp gid not a number");
return CURLE_FTP_QUOTE_ERROR;
}
}
else if (curl_strnequal(item->data, "chmod", 5)) {
attrs.permissions = strtol(path1, NULL, 8);/* permissions are octal */
if (attrs.permissions == 0 && !ISDIGIT(path1[0])) {
James Housley
committed
Curl_safefree(path1);
Curl_safefree(path2);
failf(data, "Syntax error: chmod permissions not a number");
return CURLE_FTP_QUOTE_ERROR;
}
}
else if (curl_strnequal(item->data, "chown", 5)) {
attrs.uid = strtol(path1, NULL, 10);
if (attrs.uid == 0 && !ISDIGIT(path1[0])) {
James Housley
committed
Curl_safefree(path1);
Curl_safefree(path2);
failf(data, "Syntax error: chown uid not a number");
return CURLE_FTP_QUOTE_ERROR;
}
}
/* Now send the completed structure... */
if (libssh2_sftp_setstat(sftp_session, path2, &attrs) != 0) {
err = libssh2_sftp_last_error(sftp_session);
James Housley
committed
Curl_safefree(path1);
Curl_safefree(path2);
failf(data, "Attempt to set SFTP stats failed: %s",
sftp_libssh2_strerror(err));
return CURLE_FTP_QUOTE_ERROR;
}
}
else if (curl_strnequal(item->data, "ln ", 3) ||
curl_strnequal(item->data, "symlink ", 8)) {
/* symbolic linking */
/* path1 is the source */
err = get_pathname(&cp, &path2); /* get the destination */
if (err) {
if (err == CURLE_OUT_OF_MEMORY)
failf(data, "Out of memory");
else
failf(data,
"Syntax error in ln/symlink: Bad second parameter");
James Housley
committed
Curl_safefree(path1);
return err;
}
if (libssh2_sftp_symlink(sftp_session, path1, path2) != 0) {
err = libssh2_sftp_last_error(sftp_session);
James Housley
committed
Curl_safefree(path1);
Curl_safefree(path2);
failf(data, "symlink command failed: %s",
sftp_libssh2_strerror(err));
return CURLE_FTP_QUOTE_ERROR;
}
}
else if (curl_strnequal(item->data, "mkdir ", 6)) { /* create dir */
if (libssh2_sftp_mkdir(sftp_session, path1, 0744) != 0) {
err = libssh2_sftp_last_error(sftp_session);
James Housley
committed
Curl_safefree(path1);
failf(data, "mkdir command failed: %s",
sftp_libssh2_strerror(err));
return CURLE_FTP_QUOTE_ERROR;
}
}
else if (curl_strnequal(item->data, "rename ", 7)) { /* rename file */
/* first param is the source path */
err = get_pathname(&cp, &path2); /* second param is the dest. path */
if (err) {
if (err == CURLE_OUT_OF_MEMORY)
failf(data, "Out of memory");
else
failf(data,
"Syntax error in rename: Bad second parameter");
James Housley
committed
Curl_safefree(path1);
return err;
}
James Housley
committed
if (libssh2_sftp_rename(sftp_session, path1, path2) != 0) {
err = libssh2_sftp_last_error(sftp_session);
James Housley
committed
Curl_safefree(path1);
Curl_safefree(path2);
failf(data, "rename command failed: %s",
sftp_libssh2_strerror(err));
return CURLE_FTP_QUOTE_ERROR;
}
}
else if (curl_strnequal(item->data, "rmdir ", 6)) { /* delete dir */
if (libssh2_sftp_rmdir(sftp_session,
path1) != 0) {
err = libssh2_sftp_last_error(sftp_session);
James Housley
committed
Curl_safefree(path1);
failf(data, "rmdir command failed: %s",
sftp_libssh2_strerror(err));
return CURLE_FTP_QUOTE_ERROR;
}
}
else if (curl_strnequal(item->data, "rm ", 3)) { /* delete file */
if (libssh2_sftp_unlink(sftp_session, path1) != 0) {
err = libssh2_sftp_last_error(sftp_session);
James Housley
committed
Curl_safefree(path1);
failf(data, "rm command failed: %s",
sftp_libssh2_strerror(err));
return CURLE_FTP_QUOTE_ERROR;
}
}
if (path1)
James Housley
committed
Curl_safefree(path1);
if (path2)
James Housley
committed
Curl_safefree(path2);
}
item = item->next;
}
(void)ret; /* possibly unused */
return CURLE_OK;
}
* Create the path sftp->path on the remote site
* returns CURL_OK on success, -1 on failure
James Housley
committed
*
* NOTE: This version of sftp_create_dirs() is only used when
Guenter Knauf
committed
* LIBSSH2_APINO < 200706012030L. After that version the code is in
James Housley
committed
* ssh_statemachine_act()
static CURLcode sftp_create_dirs(struct connectdata *conn) {
CURLcode result = CURLE_OK;
unsigned int sftp_err = 0;
int rc;
struct SSHPROTO *sftp = conn->data->reqdata.proto.ssh;
if (strlen(sftp->path) > 1) {
char *slash_pos = sftp->path + 1; /* ignore the leading '/' */
while ((slash_pos = strchr(slash_pos, '/')) != NULL) {
*slash_pos = 0;
infof(conn->data, "Creating directory '%s'\n", sftp->path);
/* 'mode' - parameter is preliminary - default to 0644 */
rc = libssh2_sftp_mkdir(sftp->sftp_session, sftp->path,
conn->data->set.new_directory_perms);
*slash_pos = '/';
++slash_pos;
if (rc == -1) {
/* 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->sftp_session);
if ((sftp_err != LIBSSH2_FX_FILE_ALREADY_EXISTS) &&
(sftp_err != LIBSSH2_FX_FAILURE) &&
(sftp_err != LIBSSH2_FX_PERMISSION_DENIED)) {
result = -1;
break;
}
}
}
}
return result;
Guenter Knauf
committed
#endif /* !(LIBSSH2_APINO < 200706012030L) */
#endif /* USE_LIBSSH2 */