cscg24-guacamole

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

guacctl (7509B)


      1#!/bin/sh
      2#
      3# Licensed to the Apache Software Foundation (ASF) under one
      4# or more contributor license agreements.  See the NOTICE file
      5# distributed with this work for additional information
      6# regarding copyright ownership.  The ASF licenses this file
      7# to you under the Apache License, Version 2.0 (the
      8# "License"); you may not use this file except in compliance
      9# with the License.  You may obtain a copy of the License at
     10#
     11#   http://www.apache.org/licenses/LICENSE-2.0
     12#
     13# Unless required by applicable law or agreed to in writing,
     14# software distributed under the License is distributed on an
     15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     16# KIND, either express or implied.  See the License for the
     17# specific language governing permissions and limitations
     18# under the License.
     19#
     20
     21#
     22# guacctl
     23# -------
     24#
     25# Utility for sending Guacamole-specific console codes for controlling a
     26# terminal session, such as:
     27#
     28#     * Downloading files (SSH only)
     29#     * Setting the destination directory for uploads (SSH only)
     30#     * Redirecting output to a named pipe stream (SSH or telnet)
     31#
     32# This script may also be run as "guacget", in which case the script accepts
     33# no options and assumes anything given on the commandline is a file to be
     34# downloaded.
     35#
     36
     37##
     38## Given the name of a file, which may be a relative path, produce the full,
     39## real, non-relative path for that same file.
     40##
     41## @param FILENAME
     42##     The name of the file to produce the full path of.
     43##
     44fullpath() {
     45    FILENAME="$1"
     46    DIR=`dirname "$FILENAME"`
     47    FILE=`basename "$FILENAME"`
     48    (cd "$DIR" && echo "$PWD/$FILE")
     49}
     50
     51##
     52## Sends the Guacamole-specific console code for initiating a download.
     53##
     54## @param FILENAME
     55##     The full path of the file to download.
     56##
     57send_download_file() {
     58    FILENAME="$1"
     59    printf "\033]482200;%s\007" "$FILENAME"
     60}
     61
     62##
     63## Sends the Guacamole-specific console code for setting the upload directory.
     64##
     65## @param FILENAME
     66##     The full path to the directory which should receive uploads.
     67##
     68send_set_directory() {
     69    FILENAME="$1"
     70    printf "\033]482201;%s\007" "$FILENAME"
     71}
     72
     73##
     74## Sends the Guacamole-specific console code for redirecting output to a named
     75## pipe stream (instead of the terminal emulator)
     76##
     77## @param NAME
     78##     The name of the pipe stream to open.
     79##
     80send_open_pipe_stream() {
     81    NAME="$1"
     82    printf "\033]482202;%s\007" "$NAME"
     83}
     84
     85##
     86## Sends the Guacamole-specific console code for redirecting output back to the
     87## terminal emulator
     88##
     89send_close_pipe_stream() {
     90    printf "\033]482203;\007"
     91}
     92
     93##
     94## Sends the Guacamole-specific console code for resizing the scrollback
     95## buffer.
     96##
     97## @param ROWS
     98##     The number of rows that the scrollback buffer should contain.
     99##
    100send_resize_scrollback() {
    101    ROWS="$1"
    102    printf "\033]482204;%s\007" "$ROWS"
    103}
    104
    105##
    106## Prints the given error text to STDERR.
    107##
    108## @param ...
    109##     The text to print as an error message.
    110##
    111error() {
    112    echo "$NAME:" "$@" >&2
    113}
    114
    115##
    116## Prints usage documentation for this script.
    117##
    118usage() {
    119    cat >&2 <<END
    120guacctl 1.5.4, Apache Guacamole terminal session control utility.
    121Usage: guacctl [OPTION] [FILE or NAME]...
    122
    123    -d, --download         download each of the files listed.
    124    -s, --set-directory    set the destination directory for future uploaded 
    125                           files.
    126    -o, --open-pipe        redirect output to a new pipe stream with the given
    127                           name.
    128    -c, --close-pipe       close any existing pipe stream and redirect output
    129                           back to the terminal emulator.
    130    -S, --scrollback       request that the scrollback buffer be limited to the
    131                           given number of rows.
    132END
    133}
    134
    135##
    136## Initiates a download for each of the specified files.
    137##
    138## @param ...
    139##     The name of each file that should be downloaded, as originally
    140##     provided to guacctl.
    141##
    142download_files() {
    143
    144    #
    145    # Validate arguments
    146    #
    147
    148    if [ $# -lt 1 ]; then
    149        error "No files specified."
    150        return;
    151    fi
    152
    153    #
    154    # Send download code for each file given
    155    #
    156
    157    for FILENAME in "$@"; do
    158        if [ -e "$FILENAME" ]; then
    159            send_download_file "`fullpath "$FILENAME"`"
    160        else
    161            error "$FILENAME: File does not exist."
    162        fi
    163    done
    164
    165}
    166
    167##
    168## Changes the upload path for future uploads to the given directory.
    169##
    170## @param ...
    171##     The name of the directory to use for uploads, as provided to guacctl.
    172##
    173set_directory() {
    174
    175    #
    176    # Validate arguments
    177    #
    178
    179    if [ $# -lt 1 ]; then
    180        error "No destination directory specified."
    181        return;
    182    fi
    183
    184    if [ $# -gt 1 ]; then
    185        error "Only one destination directory may be given."
    186        return;
    187    fi
    188
    189    #
    190    # Send code for setting the upload directory
    191    #
    192
    193    FILENAME="$1"
    194    if [ -d "$FILENAME" ]; then
    195        send_set_directory "`fullpath "$FILENAME"`"
    196    else
    197        error "$FILENAME: File does not exist or is not a directory."
    198    fi
    199
    200}
    201
    202##
    203## Opens a new pipe stream having the given name and redirects terminal output
    204## to that stream.
    205##
    206## @param ...
    207##     The name of the pipe stream to open, as provided to guacctl.
    208##
    209open_pipe_stream() {
    210
    211    #
    212    # Validate arguments
    213    #
    214
    215    if [ $# -lt 1 ]; then
    216        error "No pipe name specified."
    217        return;
    218    fi
    219
    220    if [ $# -gt 1 ]; then
    221        error "Only one pipe name may be given."
    222        return;
    223    fi
    224
    225    #
    226    # Send code for opening the named pipe stream
    227    #
    228
    229    NAME="$1"
    230    send_open_pipe_stream "$NAME"
    231
    232}
    233
    234##
    235## Closes the currently-open pipe stream and redirects terminal output back to
    236## the terminal emulator
    237##
    238## @param ...
    239##     The arguments provided to guacctl, which should be empty.
    240##
    241close_pipe_stream() {
    242
    243    #
    244    # Validate arguments
    245    #
    246
    247    if [ $# -gt 0 ]; then
    248        error "Closing an open pipe stream does not require any arguments."
    249        return;
    250    fi
    251
    252    #
    253    # Send code for closing the currently-open named pipe stream
    254    #
    255
    256    send_close_pipe_stream
    257
    258}
    259
    260##
    261## Resizes the scrollback buffer to the given number of rows.
    262##
    263## @param ...
    264##     The number of rows that should be contained within the scrollback
    265##     buffer, as provided to guacctl.
    266##
    267resize_scrollback() {
    268
    269    #
    270    # Validate arguments
    271    #
    272
    273    if [ $# -lt 1 ]; then
    274        error "No row count specified."
    275        return;
    276    fi
    277
    278    if [ $# -gt 1 ]; then
    279        error "Only one row count may be given."
    280        return;
    281    fi
    282
    283    #
    284    # Send code for resizing scrollback
    285    #
    286
    287    ROWS="$1"
    288    send_resize_scrollback "$ROWS"
    289
    290}
    291
    292
    293#
    294# Get script name
    295#
    296
    297NAME=`basename "$0"`
    298
    299#
    300# Handle downloads directly if invoked as "guacget"
    301#
    302
    303if [ "x$NAME" = "xguacget" ]; then
    304    download_files "$@"
    305    exit 0;
    306fi
    307
    308#
    309# Parse options
    310#
    311
    312case "$1" in
    313
    314    #
    315    # Download files
    316    #
    317
    318    "--download"|"-d")
    319        shift
    320        download_files "$@"
    321        ;;
    322
    323    #
    324    # Set upload directory
    325    #
    326
    327    "--set-directory"|"-s")
    328        shift
    329        set_directory "$@"
    330        ;;
    331
    332    #
    333    # Redirect to pipe
    334    #
    335
    336    "--open-pipe"|"-o")
    337        shift
    338        open_pipe_stream "$@"
    339        ;;
    340
    341    #
    342    # Redirect back to terminal
    343    #
    344
    345    "--close-pipe"|"-c")
    346        shift
    347        close_pipe_stream "$@"
    348        ;;
    349
    350    #
    351    # Resize scrollback
    352    #
    353
    354    "--scrollback"|"-S")
    355        shift
    356        resize_scrollback "$@"
    357        ;;
    358
    359    #
    360    # Show usage info if options are invalid
    361    #
    362
    363    *)
    364        usage
    365        exit 1
    366        ;;
    367esac
    368