GUAC-1389: Add convenience function for exposing SFTP filesystems to specific users.

This commit is contained in:
Michael Jumper 2016-03-01 14:03:38 -08:00
parent cb70c6e8b9
commit 48143f6a0a
2 changed files with 57 additions and 10 deletions

View File

@ -716,9 +716,22 @@ static int guac_common_ssh_sftp_put_handler(guac_user* user,
return 0; return 0;
} }
void* guac_common_ssh_expose_sftp_filesystem(guac_user* user, void* data) {
guac_common_ssh_sftp_filesystem* filesystem =
(guac_common_ssh_sftp_filesystem*) data;
/* No need to expose if there is no filesystem or the user has left */
if (user == NULL || filesystem == NULL)
return NULL;
/* Allocate and expose filesystem object for user */
return guac_common_ssh_alloc_sftp_filesystem_object(filesystem, user);
}
guac_object* guac_common_ssh_alloc_sftp_filesystem_object( guac_object* guac_common_ssh_alloc_sftp_filesystem_object(
guac_common_ssh_sftp_filesystem* filesystem, guac_user* user, guac_common_ssh_sftp_filesystem* filesystem, guac_user* user) {
const char* name) {
/* Init filesystem */ /* Init filesystem */
guac_object* fs_object = guac_user_alloc_object(user); guac_object* fs_object = guac_user_alloc_object(user);
@ -727,7 +740,7 @@ guac_object* guac_common_ssh_alloc_sftp_filesystem_object(
fs_object->data = filesystem; fs_object->data = filesystem;
/* Send filesystem to user */ /* Send filesystem to user */
guac_protocol_send_filesystem(user->socket, fs_object, name); guac_protocol_send_filesystem(user->socket, fs_object, filesystem->name);
guac_socket_flush(user->socket); guac_socket_flush(user->socket);
return fs_object; return fs_object;
@ -735,7 +748,7 @@ guac_object* guac_common_ssh_alloc_sftp_filesystem_object(
} }
guac_common_ssh_sftp_filesystem* guac_common_ssh_create_sftp_filesystem( guac_common_ssh_sftp_filesystem* guac_common_ssh_create_sftp_filesystem(
guac_common_ssh_session* session) { guac_common_ssh_session* session, const char* name) {
/* Request SFTP */ /* Request SFTP */
LIBSSH2_SFTP* sftp_session = libssh2_sftp_init(session->session); LIBSSH2_SFTP* sftp_session = libssh2_sftp_init(session->session);
@ -747,6 +760,7 @@ guac_common_ssh_sftp_filesystem* guac_common_ssh_create_sftp_filesystem(
malloc(sizeof(guac_common_ssh_sftp_filesystem)); malloc(sizeof(guac_common_ssh_sftp_filesystem));
/* Associate SSH session with SFTP data and user */ /* Associate SSH session with SFTP data and user */
filesystem->name = strdup(name);
filesystem->ssh_session = session; filesystem->ssh_session = session;
filesystem->sftp_session = sftp_session; filesystem->sftp_session = sftp_session;
@ -765,6 +779,7 @@ void guac_common_ssh_destroy_sftp_filesystem(
libssh2_sftp_shutdown(filesystem->sftp_session); libssh2_sftp_shutdown(filesystem->sftp_session);
/* Free associated memory */ /* Free associated memory */
free(filesystem->name);
free(filesystem); free(filesystem);
} }

View File

@ -42,6 +42,11 @@
*/ */
typedef struct guac_common_ssh_sftp_filesystem { typedef struct guac_common_ssh_sftp_filesystem {
/**
* The human-readable display name of this filesystem.
*/
char* name;
/** /**
* The distinct SSH session used for SFTP. * The distinct SSH session used for SFTP.
*/ */
@ -101,11 +106,15 @@ typedef struct guac_common_ssh_sftp_ls_state {
* The session to use to provide SFTP. This session will automatically be * The session to use to provide SFTP. This session will automatically be
* destroyed when this filesystem is destroyed. * destroyed when this filesystem is destroyed.
* *
* @param name
* The name to send as the name of the filesystem whenever it is exposed
* to a user.
*
* @return * @return
* A new SFTP filesystem object, not yet exposed to users. * A new SFTP filesystem object, not yet exposed to users.
*/ */
guac_common_ssh_sftp_filesystem* guac_common_ssh_create_sftp_filesystem( guac_common_ssh_sftp_filesystem* guac_common_ssh_create_sftp_filesystem(
guac_common_ssh_session* session); guac_common_ssh_session* session, const char* name);
/** /**
* Destroys the given filesystem object, disconnecting from SFTP and freeing * Destroys the given filesystem object, disconnecting from SFTP and freeing
@ -129,16 +138,39 @@ void guac_common_ssh_destroy_sftp_filesystem(
* @param user * @param user
* The user that the SFTP filesystem should be exposed to. * The user that the SFTP filesystem should be exposed to.
* *
* @param name
* The name to send as the name of the filesystem.
*
* @return * @return
* A new Guacamole filesystem object, configured to use SFTP for uploading * A new Guacamole filesystem object, configured to use SFTP for uploading
* and downloading files. * and downloading files.
*/ */
guac_object* guac_common_ssh_alloc_sftp_filesystem_object( guac_object* guac_common_ssh_alloc_sftp_filesystem_object(
guac_common_ssh_sftp_filesystem* filesystem, guac_user* user, guac_common_ssh_sftp_filesystem* filesystem, guac_user* user);
const char* name);
/**
* Allocates a new filesystem guac_object for the given user, returning the
* resulting guac_object. This function is provided for convenience, as it is
* can be used as the callback for guac_client_foreach_user() or
* guac_client_for_owner(). Note that this guac_object will be tracked
* internally by libguac, will be provided to us in the parameters of handlers
* related to that guac_object, and will automatically be freed when the
* associated guac_user is freed, so the return value of this function can
* safely be ignored.
*
* If either the given user or the given filesystem are NULL, then this
* function has no effect.
*
* @param user
* The use to expose the filesystem to, or NULL if nothing should be
* exposed.
*
* @param data
* A pointer to the guac_common_ssh_sftp_filesystem instance to expose
* to the given user, or NULL if nothing should be exposed.
*
* @return
* The guac_object allocated for the newly-exposed filesystem, or NULL if
* no filesystem object could be allocated.
*/
void* guac_common_ssh_expose_sftp_filesystem(guac_user* user, void* data);
/** /**
* Initiates an SFTP file download to the user via the Guacamole "file" * Initiates an SFTP file download to the user via the Guacamole "file"