cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

client.c (5006B)


      1/*
      2 * Licensed to the Apache Software Foundation (ASF) under one
      3 * or more contributor license agreements.  See the NOTICE file
      4 * distributed with this work for additional information
      5 * regarding copyright ownership.  The ASF licenses this file
      6 * to you under the Apache License, Version 2.0 (the
      7 * "License"); you may not use this file except in compliance
      8 * with the License.  You may obtain a copy of the License at
      9 *
     10 *   http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing,
     13 * software distributed under the License is distributed on an
     14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     15 * KIND, either express or implied.  See the License for the
     16 * specific language governing permissions and limitations
     17 * under the License.
     18 */
     19
     20#include "config.h"
     21
     22#include "argv.h"
     23#include "client.h"
     24#include "common-ssh/sftp.h"
     25#include "ssh.h"
     26#include "terminal/terminal.h"
     27#include "user.h"
     28
     29#include <langinfo.h>
     30#include <locale.h>
     31#include <stdlib.h>
     32#include <string.h>
     33
     34#include <guacamole/argv.h>
     35#include <guacamole/client.h>
     36#include <guacamole/mem.h>
     37#include <guacamole/recording.h>
     38#include <guacamole/socket.h>
     39
     40
     41/**
     42 * A pending join handler implementation that will synchronize the connection
     43 * state for all pending users prior to them being promoted to full user.
     44 *
     45 * @param client
     46 *     The client whose pending users are about to be promoted.
     47 *
     48 * @return
     49 *     Always zero.
     50 */
     51static int guac_ssh_join_pending_handler(guac_client* client) {
     52
     53    guac_ssh_client* ssh_client = (guac_ssh_client*) client->data;
     54
     55    /* Synchronize the terminal state to all pending users */
     56    if (ssh_client->term != NULL) {
     57        guac_socket* broadcast_socket = client->pending_socket;
     58        guac_terminal_sync_users(ssh_client->term, client, broadcast_socket);
     59        guac_ssh_send_current_argv_batch(client, broadcast_socket);
     60        guac_socket_flush(broadcast_socket);
     61    }
     62
     63    return 0;
     64
     65}
     66
     67int guac_client_init(guac_client* client) {
     68
     69    /* Set client args */
     70    client->args = GUAC_SSH_CLIENT_ARGS;
     71
     72    /* Allocate client instance data */
     73    guac_ssh_client* ssh_client = guac_mem_zalloc(sizeof(guac_ssh_client));
     74    client->data = ssh_client;
     75
     76    /* Set handlers */
     77    client->join_handler = guac_ssh_user_join_handler;
     78    client->join_pending_handler = guac_ssh_join_pending_handler;
     79    client->free_handler = guac_ssh_client_free_handler;
     80    client->leave_handler = guac_ssh_user_leave_handler;
     81
     82    /* Register handlers for argument values that may be sent after the handshake */
     83    guac_argv_register(GUAC_SSH_ARGV_COLOR_SCHEME, guac_ssh_argv_callback, NULL, GUAC_ARGV_OPTION_ECHO);
     84    guac_argv_register(GUAC_SSH_ARGV_FONT_NAME, guac_ssh_argv_callback, NULL, GUAC_ARGV_OPTION_ECHO);
     85    guac_argv_register(GUAC_SSH_ARGV_FONT_SIZE, guac_ssh_argv_callback, NULL, GUAC_ARGV_OPTION_ECHO);
     86
     87    /* Set locale and warn if not UTF-8 */
     88    setlocale(LC_CTYPE, "");
     89    if (strcmp(nl_langinfo(CODESET), "UTF-8") != 0) {
     90        guac_client_log(client, GUAC_LOG_INFO,
     91                "Current locale does not use UTF-8. Some characters may "
     92                "not render correctly.");
     93    }
     94
     95    /* Success */
     96    return 0;
     97
     98}
     99
    100int guac_ssh_client_free_handler(guac_client* client) {
    101
    102    guac_ssh_client* ssh_client = (guac_ssh_client*) client->data;
    103
    104    /* Close SSH channel */
    105    if (ssh_client->term_channel != NULL) {
    106        libssh2_channel_send_eof(ssh_client->term_channel);
    107        libssh2_channel_close(ssh_client->term_channel);
    108    }
    109
    110    /* Free terminal (which may still be using term_channel) */
    111    if (ssh_client->term != NULL) {
    112        /* Stop the terminal to unblock any pending reads/writes */
    113        guac_terminal_stop(ssh_client->term);
    114
    115        /* Wait ssh_client_thread to finish before freeing the terminal */
    116        pthread_join(ssh_client->client_thread, NULL);
    117        guac_terminal_free(ssh_client->term);
    118    }
    119
    120    /* Free terminal channel now that the terminal is finished */
    121    if (ssh_client->term_channel != NULL)
    122        libssh2_channel_free(ssh_client->term_channel);
    123
    124    /* Clean up the SFTP filesystem object and session */
    125    if (ssh_client->sftp_filesystem) {
    126        guac_common_ssh_destroy_sftp_filesystem(ssh_client->sftp_filesystem);
    127        guac_common_ssh_destroy_session(ssh_client->sftp_session);
    128    }
    129
    130    /* Clean up recording, if in progress */
    131    if (ssh_client->recording != NULL)
    132        guac_recording_free(ssh_client->recording);
    133
    134    /* Free interactive SSH session */
    135    if (ssh_client->session != NULL)
    136        guac_common_ssh_destroy_session(ssh_client->session);
    137
    138    /* Free SSH client credentials */
    139    if (ssh_client->user != NULL)
    140        guac_common_ssh_destroy_user(ssh_client->user);
    141
    142    /* Free parsed settings */
    143    if (ssh_client->settings != NULL)
    144        guac_ssh_settings_free(ssh_client->settings);
    145
    146    /* Free client structure */
    147    guac_mem_free(ssh_client);
    148
    149    guac_common_ssh_uninit();
    150    return 0;
    151}
    152