cscg24-guacamole

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

user.c (3626B)


      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 "clipboard.h"
     24#include "common/display.h"
     25#include "input.h"
     26#include "user.h"
     27#include "pipe.h"
     28#include "terminal/terminal.h"
     29#include "sftp.h"
     30#include "ssh.h"
     31#include "settings.h"
     32
     33#include <guacamole/argv.h>
     34#include <guacamole/client.h>
     35#include <guacamole/socket.h>
     36#include <guacamole/user.h>
     37
     38#include <pthread.h>
     39#include <string.h>
     40
     41int guac_ssh_user_join_handler(guac_user* user, int argc, char** argv) {
     42
     43    guac_client* client = user->client;
     44    guac_ssh_client* ssh_client = (guac_ssh_client*) client->data;
     45
     46    /* Parse provided arguments */
     47    guac_ssh_settings* settings = guac_ssh_parse_args(user,
     48            argc, (const char**) argv);
     49
     50    /* Fail if settings cannot be parsed */
     51    if (settings == NULL) {
     52        guac_user_log(user, GUAC_LOG_INFO,
     53                "Badly formatted client arguments.");
     54        return 1;
     55    }
     56
     57    /* Store settings at user level */
     58    user->data = settings;
     59
     60    /* Connect via SSH if owner */
     61    if (user->owner) {
     62
     63        /* Store owner's settings at client level */
     64        ssh_client->settings = settings;
     65
     66        /* Start client thread */
     67        if (pthread_create(&(ssh_client->client_thread), NULL,
     68                    ssh_client_thread, (void*) client)) {
     69            guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
     70                    "Unable to start SSH client thread");
     71            return 1;
     72        }
     73
     74    }
     75
     76    /* Only handle events if not read-only */
     77    if (!settings->read_only) {
     78
     79        /* General mouse/keyboard events */
     80        user->key_handler = guac_ssh_user_key_handler;
     81        user->mouse_handler = guac_ssh_user_mouse_handler;
     82
     83        /* Inbound (client to server) clipboard transfer */
     84        if (!settings->disable_paste)
     85            user->clipboard_handler = guac_ssh_clipboard_handler;
     86
     87        /* STDIN redirection */
     88        user->pipe_handler = guac_ssh_pipe_handler;
     89
     90        /* Updates to connection parameters */
     91        user->argv_handler = guac_argv_handler;
     92
     93        /* Display size change events */
     94        user->size_handler = guac_ssh_user_size_handler;
     95
     96        /* Set generic (non-filesystem) file upload handler */
     97        if (settings->enable_sftp && !settings->sftp_disable_upload)
     98            user->file_handler = guac_sftp_file_handler;
     99
    100    }
    101
    102    return 0;
    103
    104}
    105
    106int guac_ssh_user_leave_handler(guac_user* user) {
    107
    108    guac_ssh_client* ssh_client = (guac_ssh_client*) user->client->data;
    109
    110    /* Remove the user from the terminal */
    111    guac_terminal_remove_user(ssh_client->term, user);
    112
    113    /* Free settings if not owner (owner settings will be freed with client) */
    114    if (!user->owner) {
    115        guac_ssh_settings* settings = (guac_ssh_settings*) user->data;
    116        guac_ssh_settings_free(settings);
    117    }
    118
    119    return 0;
    120}
    121