Move SFTP functions to own file. Stub out download function.
This commit is contained in:
parent
273c6a8503
commit
0c86f52ed5
@ -50,6 +50,7 @@ libguac_client_ssh_la_SOURCES = \
|
||||
display.c \
|
||||
guac_handlers.c \
|
||||
ibar.c \
|
||||
sftp.c \
|
||||
ssh_client.c \
|
||||
terminal.c \
|
||||
terminal_handlers.c
|
||||
@ -65,6 +66,7 @@ noinst_HEADERS = \
|
||||
guac_handlers.h \
|
||||
ibar.h \
|
||||
libssh_compat.h \
|
||||
sftp.h \
|
||||
ssh_client.h \
|
||||
terminal.h \
|
||||
terminal_handlers.h \
|
||||
|
@ -456,119 +456,3 @@ int ssh_guac_client_free_handler(guac_client* client) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool __ssh_guac_valid_filename(char* filename) {
|
||||
|
||||
/* Disallow "." as a filename */
|
||||
if (strcmp(filename, ".") == 0)
|
||||
return false;
|
||||
|
||||
/* Disallow ".." as a filename */
|
||||
if (strcmp(filename, "..") == 0)
|
||||
return false;
|
||||
|
||||
/* Search for path separator characters */
|
||||
for (;;) {
|
||||
|
||||
char c = *(filename++);
|
||||
if (c == '\0')
|
||||
break;
|
||||
|
||||
/* Replace slashes with underscores */
|
||||
if (c == '\\' || c == '/')
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
/* If filename does not contain a path, it's ok */
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
int ssh_guac_client_file_handler(guac_client* client, guac_stream* stream,
|
||||
char* mimetype, char* filename) {
|
||||
|
||||
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
|
||||
sftp_file file;
|
||||
|
||||
/* Ensure filename is a valid filename and not a path */
|
||||
if (!__ssh_guac_valid_filename(filename)) {
|
||||
guac_protocol_send_ack(client->socket, stream,
|
||||
"SFTP: Illegal filename",
|
||||
GUAC_PROTOCOL_STATUS_INVALID_PARAMETER);
|
||||
guac_socket_flush(client->socket);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Open file via SFTP */
|
||||
file = sftp_open(client_data->sftp_session, filename,
|
||||
O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
|
||||
|
||||
/* Inform of status */
|
||||
if (file != NULL) {
|
||||
guac_protocol_send_ack(client->socket, stream, "SFTP: File opened",
|
||||
GUAC_PROTOCOL_STATUS_SUCCESS);
|
||||
guac_socket_flush(client->socket);
|
||||
}
|
||||
else {
|
||||
guac_client_log_error(client, "Unable to open file: %s",
|
||||
ssh_get_error(client_data->session));
|
||||
guac_protocol_send_ack(client->socket, stream, "SFTP: Open failed",
|
||||
GUAC_PROTOCOL_STATUS_INTERNAL_ERROR);
|
||||
guac_socket_flush(client->socket);
|
||||
}
|
||||
|
||||
/* Store file within stream */
|
||||
stream->data = file;
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
int ssh_guac_client_blob_handler(guac_client* client, guac_stream* stream,
|
||||
void* data, int length) {
|
||||
|
||||
/* Pull file from stream */
|
||||
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
|
||||
sftp_file file = (sftp_file) stream->data;
|
||||
|
||||
/* Attempt write */
|
||||
if (sftp_write(file, data, length) == length) {
|
||||
guac_protocol_send_ack(client->socket, stream, "SFTP: OK",
|
||||
GUAC_PROTOCOL_STATUS_SUCCESS);
|
||||
guac_socket_flush(client->socket);
|
||||
}
|
||||
|
||||
/* Inform of any errors */
|
||||
else {
|
||||
guac_client_log_error(client, "Unable to write to file: %s",
|
||||
ssh_get_error(client_data->session));
|
||||
guac_protocol_send_ack(client->socket, stream, "SFTP: Write failed",
|
||||
GUAC_PROTOCOL_STATUS_INTERNAL_ERROR);
|
||||
guac_socket_flush(client->socket);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
int ssh_guac_client_end_handler(guac_client* client, guac_stream* stream) {
|
||||
|
||||
/* Pull file from stream */
|
||||
sftp_file file = (sftp_file) stream->data;
|
||||
|
||||
/* Attempt to close file */
|
||||
if (sftp_close(file) == SSH_OK) {
|
||||
guac_protocol_send_ack(client->socket, stream, "SFTP: OK",
|
||||
GUAC_PROTOCOL_STATUS_SUCCESS);
|
||||
guac_socket_flush(client->socket);
|
||||
}
|
||||
else {
|
||||
guac_client_log_error(client, "Unable to close file");
|
||||
guac_protocol_send_ack(client->socket, stream, "SFTP: Close failed",
|
||||
GUAC_PROTOCOL_STATUS_INTERNAL_ERROR);
|
||||
guac_socket_flush(client->socket);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
@ -47,11 +47,6 @@ int ssh_guac_client_mouse_handler(guac_client* client, int x, int y, int mask);
|
||||
int ssh_guac_client_clipboard_handler(guac_client* client, char* data);
|
||||
int ssh_guac_client_size_handler(guac_client* client, int width, int height);
|
||||
int ssh_guac_client_free_handler(guac_client* client);
|
||||
int ssh_guac_client_file_handler(guac_client* client, guac_stream* stream,
|
||||
char* mimetype, char* filename);
|
||||
int ssh_guac_client_blob_handler(guac_client* client, guac_stream* stream,
|
||||
void* data, int length);
|
||||
int ssh_guac_client_end_handler(guac_client* client, guac_stream* stream);
|
||||
|
||||
#endif
|
||||
|
||||
|
174
src/protocols/ssh/sftp.c
Normal file
174
src/protocols/ssh/sftp.c
Normal file
@ -0,0 +1,174 @@
|
||||
|
||||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is libguac-client-ssh.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Michael Jumper.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2011
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <libssh/libssh.h>
|
||||
#include <libssh/sftp.h>
|
||||
|
||||
#include <guacamole/client.h>
|
||||
#include <guacamole/protocol.h>
|
||||
#include <guacamole/stream.h>
|
||||
|
||||
#include "client.h"
|
||||
|
||||
static bool __ssh_guac_valid_filename(char* filename) {
|
||||
|
||||
/* Disallow "." as a filename */
|
||||
if (strcmp(filename, ".") == 0)
|
||||
return false;
|
||||
|
||||
/* Disallow ".." as a filename */
|
||||
if (strcmp(filename, "..") == 0)
|
||||
return false;
|
||||
|
||||
/* Search for path separator characters */
|
||||
for (;;) {
|
||||
|
||||
char c = *(filename++);
|
||||
if (c == '\0')
|
||||
break;
|
||||
|
||||
/* Replace slashes with underscores */
|
||||
if (c == '\\' || c == '/')
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
/* If filename does not contain a path, it's ok */
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
int guac_sftp_file_handler(guac_client* client, guac_stream* stream,
|
||||
char* mimetype, char* filename) {
|
||||
|
||||
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
|
||||
sftp_file file;
|
||||
|
||||
/* Ensure filename is a valid filename and not a path */
|
||||
if (!__ssh_guac_valid_filename(filename)) {
|
||||
guac_protocol_send_ack(client->socket, stream,
|
||||
"SFTP: Illegal filename",
|
||||
GUAC_PROTOCOL_STATUS_INVALID_PARAMETER);
|
||||
guac_socket_flush(client->socket);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Open file via SFTP */
|
||||
file = sftp_open(client_data->sftp_session, filename,
|
||||
O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
|
||||
|
||||
/* Inform of status */
|
||||
if (file != NULL) {
|
||||
guac_protocol_send_ack(client->socket, stream, "SFTP: File opened",
|
||||
GUAC_PROTOCOL_STATUS_SUCCESS);
|
||||
guac_socket_flush(client->socket);
|
||||
}
|
||||
else {
|
||||
guac_client_log_error(client, "Unable to open file: %s",
|
||||
ssh_get_error(client_data->session));
|
||||
guac_protocol_send_ack(client->socket, stream, "SFTP: Open failed",
|
||||
GUAC_PROTOCOL_STATUS_INTERNAL_ERROR);
|
||||
guac_socket_flush(client->socket);
|
||||
}
|
||||
|
||||
/* Store file within stream */
|
||||
stream->data = file;
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
int guac_sftp_blob_handler(guac_client* client, guac_stream* stream,
|
||||
void* data, int length) {
|
||||
|
||||
/* Pull file from stream */
|
||||
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
|
||||
sftp_file file = (sftp_file) stream->data;
|
||||
|
||||
/* Attempt write */
|
||||
if (sftp_write(file, data, length) == length) {
|
||||
guac_protocol_send_ack(client->socket, stream, "SFTP: OK",
|
||||
GUAC_PROTOCOL_STATUS_SUCCESS);
|
||||
guac_socket_flush(client->socket);
|
||||
}
|
||||
|
||||
/* Inform of any errors */
|
||||
else {
|
||||
guac_client_log_error(client, "Unable to write to file: %s",
|
||||
ssh_get_error(client_data->session));
|
||||
guac_protocol_send_ack(client->socket, stream, "SFTP: Write failed",
|
||||
GUAC_PROTOCOL_STATUS_INTERNAL_ERROR);
|
||||
guac_socket_flush(client->socket);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
int guac_sftp_end_handler(guac_client* client, guac_stream* stream) {
|
||||
|
||||
/* Pull file from stream */
|
||||
sftp_file file = (sftp_file) stream->data;
|
||||
|
||||
/* Attempt to close file */
|
||||
if (sftp_close(file) == SSH_OK) {
|
||||
guac_protocol_send_ack(client->socket, stream, "SFTP: OK",
|
||||
GUAC_PROTOCOL_STATUS_SUCCESS);
|
||||
guac_socket_flush(client->socket);
|
||||
}
|
||||
else {
|
||||
guac_client_log_error(client, "Unable to close file");
|
||||
guac_protocol_send_ack(client->socket, stream, "SFTP: Close failed",
|
||||
GUAC_PROTOCOL_STATUS_INTERNAL_ERROR);
|
||||
guac_socket_flush(client->socket);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
guac_stream* guac_sftp_download_file(guac_client* client, const char* filename) {
|
||||
/* STUB */
|
||||
return NULL;
|
||||
}
|
||||
|
67
src/protocols/ssh/sftp.h
Normal file
67
src/protocols/ssh/sftp.h
Normal file
@ -0,0 +1,67 @@
|
||||
|
||||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is libguac-client-ssh.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Michael Jumper.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2011
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef _SSH_GUAC_SFTP_H
|
||||
#define _SSH_GUAC_SFTP_H
|
||||
|
||||
#include <guacamole/client.h>
|
||||
#include <guacamole/stream.h>
|
||||
|
||||
/**
|
||||
* Handler for file messages which begins an SFTP data transfer (upload).
|
||||
*/
|
||||
int guac_sftp_file_handler(guac_client* client, guac_stream* stream,
|
||||
char* mimetype, char* filename);
|
||||
|
||||
/**
|
||||
* Handler for blob messages which continues an SFTP data transfer (upload).
|
||||
*/
|
||||
int guac_sftp_blob_handler(guac_client* client, guac_stream* stream,
|
||||
void* data, int length);
|
||||
|
||||
/**
|
||||
* Handler for end messages which ends an SFTP data transfer (upload).
|
||||
*/
|
||||
int guac_sftp_end_handler(guac_client* client, guac_stream* stream);
|
||||
|
||||
/**
|
||||
* Begins (and automatically continues) an SFTP file download to the user.
|
||||
*/
|
||||
guac_stream* guac_sftp_download_file(guac_client* client, const char* filename);
|
||||
|
||||
#endif
|
||||
|
@ -50,6 +50,7 @@
|
||||
#include "client.h"
|
||||
#include "common.h"
|
||||
#include "guac_handlers.h"
|
||||
#include "sftp.h"
|
||||
|
||||
/**
|
||||
* Reads a single line from STDIN.
|
||||
@ -247,9 +248,9 @@ void* ssh_client_thread(void* data) {
|
||||
}
|
||||
|
||||
/* Set file handlers */
|
||||
client->file_handler = ssh_guac_client_file_handler;
|
||||
client->blob_handler = ssh_guac_client_blob_handler;
|
||||
client->end_handler = ssh_guac_client_end_handler;
|
||||
client->file_handler = guac_sftp_file_handler;
|
||||
client->blob_handler = guac_sftp_blob_handler;
|
||||
client->end_handler = guac_sftp_end_handler;
|
||||
|
||||
guac_client_log_info(client, "SFTP session initialized");
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user