diff --git a/src/common-ssh/guac_sftp.c b/src/common-ssh/guac_sftp.c index 24dd841b..71f1e5b1 100644 --- a/src/common-ssh/guac_sftp.c +++ b/src/common-ssh/guac_sftp.c @@ -716,9 +716,22 @@ static int guac_common_ssh_sftp_put_handler(guac_user* user, 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_common_ssh_sftp_filesystem* filesystem, guac_user* user, - const char* name) { + guac_common_ssh_sftp_filesystem* filesystem, guac_user* user) { /* Init filesystem */ 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; /* 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); 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_session* session) { + guac_common_ssh_session* session, const char* name) { /* Request SFTP */ 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)); /* Associate SSH session with SFTP data and user */ + filesystem->name = strdup(name); filesystem->ssh_session = session; filesystem->sftp_session = sftp_session; @@ -765,6 +779,7 @@ void guac_common_ssh_destroy_sftp_filesystem( libssh2_sftp_shutdown(filesystem->sftp_session); /* Free associated memory */ + free(filesystem->name); free(filesystem); } diff --git a/src/common-ssh/guac_sftp.h b/src/common-ssh/guac_sftp.h index 4952cca1..a1f4e332 100644 --- a/src/common-ssh/guac_sftp.h +++ b/src/common-ssh/guac_sftp.h @@ -42,6 +42,11 @@ */ typedef struct guac_common_ssh_sftp_filesystem { + /** + * The human-readable display name of this filesystem. + */ + char* name; + /** * 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 * 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 * A new SFTP filesystem object, not yet exposed to users. */ 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 @@ -129,16 +138,39 @@ void guac_common_ssh_destroy_sftp_filesystem( * @param user * The user that the SFTP filesystem should be exposed to. * - * @param name - * The name to send as the name of the filesystem. - * * @return * A new Guacamole filesystem object, configured to use SFTP for uploading * and downloading files. */ guac_object* guac_common_ssh_alloc_sftp_filesystem_object( - guac_common_ssh_sftp_filesystem* filesystem, guac_user* user, - const char* name); + guac_common_ssh_sftp_filesystem* filesystem, guac_user* user); + +/** + * 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"