You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@guacamole.apache.org by jm...@apache.org on 2017/02/28 18:40:37 UTC

[1/9] incubator-guacamole-server git commit: GUACAMOLE-169: Move common-ssh headers to namespaced directory.

Repository: incubator-guacamole-server
Updated Branches:
  refs/heads/master eb5ef5a02 -> c4903a8e3


http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/guac_ssh_buffer.c
----------------------------------------------------------------------
diff --git a/src/common-ssh/guac_ssh_buffer.c b/src/common-ssh/guac_ssh_buffer.c
deleted file mode 100644
index b3d8d57..0000000
--- a/src/common-ssh/guac_ssh_buffer.c
+++ /dev/null
@@ -1,135 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#include "config.h"
-
-#include <openssl/bn.h>
-#include <openssl/ossl_typ.h>
-
-#include <stdint.h>
-#include <string.h>
-#include <stdlib.h>
-
-void guac_common_ssh_buffer_write_byte(char** buffer, uint8_t value) {
-
-    uint8_t* data = (uint8_t*) *buffer;
-    *data = value;
-
-    (*buffer)++;
-
-}
-
-void guac_common_ssh_buffer_write_uint32(char** buffer, uint32_t value) {
-
-    uint8_t* data = (uint8_t*) *buffer;
-
-    data[0] = (value & 0xFF000000) >> 24;
-    data[1] = (value & 0x00FF0000) >> 16;
-    data[2] = (value & 0x0000FF00) >> 8;
-    data[3] =  value & 0x000000FF;
-
-    *buffer += 4;
-
-}
-
-void guac_common_ssh_buffer_write_data(char** buffer, const char* data,
-        int length) {
-    memcpy(*buffer, data, length);
-    *buffer += length;
-}
-
-void guac_common_ssh_buffer_write_bignum(char** buffer, BIGNUM* value) {
-
-    unsigned char* bn_buffer;
-    int length;
-
-    /* If zero, just write zero length */
-    if (BN_is_zero(value)) {
-        guac_common_ssh_buffer_write_uint32(buffer, 0);
-        return;
-    }
-
-    /* Allocate output buffer, add padding byte */
-    length = BN_num_bytes(value);
-    bn_buffer = malloc(length);
-
-    /* Convert BIGNUM */
-    BN_bn2bin(value, bn_buffer);
-
-    /* If first byte has high bit set, write padding byte */
-    if (bn_buffer[0] & 0x80) {
-        guac_common_ssh_buffer_write_uint32(buffer, length+1);
-        guac_common_ssh_buffer_write_byte(buffer, 0);
-    }
-    else
-        guac_common_ssh_buffer_write_uint32(buffer, length);
-
-    /* Write data */
-    memcpy(*buffer, bn_buffer, length);
-    *buffer += length;
-
-    free(bn_buffer);
-
-}
-
-void guac_common_ssh_buffer_write_string(char** buffer, const char* string,
-        int length) {
-    guac_common_ssh_buffer_write_uint32(buffer, length);
-    guac_common_ssh_buffer_write_data(buffer, string, length);
-}
-
-uint8_t guac_common_ssh_buffer_read_byte(char** buffer) {
-
-    uint8_t* data = (uint8_t*) *buffer;
-    uint8_t value = *data;
-
-    (*buffer)++;
-
-    return value;
-
-}
-
-uint32_t guac_common_ssh_buffer_read_uint32(char** buffer) {
-
-    uint8_t* data = (uint8_t*) *buffer;
-    uint32_t value =
-          (data[0] << 24)
-        | (data[1] << 16)
-        | (data[2] <<  8)
-        |  data[3];
-
-    *buffer += 4;
-
-    return value;
-
-}
-
-char* guac_common_ssh_buffer_read_string(char** buffer, int* length) {
-
-    char* value;
-
-    *length = guac_common_ssh_buffer_read_uint32(buffer);
-    value = *buffer;
-
-    *buffer += *length;
-
-    return value;
-
-}
-

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/guac_ssh_buffer.h
----------------------------------------------------------------------
diff --git a/src/common-ssh/guac_ssh_buffer.h b/src/common-ssh/guac_ssh_buffer.h
deleted file mode 100644
index b42a2ac..0000000
--- a/src/common-ssh/guac_ssh_buffer.h
+++ /dev/null
@@ -1,134 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef GUAC_COMMON_SSH_BUFFER_H
-#define GUAC_COMMON_SSH_BUFFER_H
-
-#include "config.h"
-
-#include <openssl/bn.h>
-#include <stdint.h>
-
-/**
- * Writes the given byte to the given buffer, advancing the buffer pointer by
- * one byte.
- *
- * @param buffer
- *     The buffer to write to.
- *
- * @param value
- *     The value to write.
- */
-void guac_common_ssh_buffer_write_byte(char** buffer, uint8_t value);
-
-/**
- * Writes the given integer to the given buffer, advancing the buffer pointer
- * four bytes.
- *
- * @param buffer
- *     The buffer to write to.
- *
- * @param value
- *     The value to write.
- */
-void guac_common_ssh_buffer_write_uint32(char** buffer, uint32_t value);
-
-/**
- * Writes the given string and its length to the given buffer, advancing the
- * buffer pointer by the size of the length (four bytes) and the size of the
- * string.
- *
- * @param buffer
- *     The buffer to write to.
- *
- * @param string
- *     The string value to write.
- *
- * @param length
- *     The length of the string to write, in bytes.
- */
-void guac_common_ssh_buffer_write_string(char** buffer, const char* string,
-        int length);
-
-/**
- * Writes the given BIGNUM the given buffer, advancing the buffer pointer by
- * the size of the length (four bytes) and the size of the BIGNUM.
- *
- * @param buffer
- *     The buffer to write to.
- *
- * @param value
- *     The value to write.
- */
-void guac_common_ssh_buffer_write_bignum(char** buffer, BIGNUM* value);
-
-/**
- * Writes the given data the given buffer, advancing the buffer pointer by the
- * given length.
- *
- * @param data
- *     The arbitrary data to write.
- *
- * @param length
- *     The length of data to write, in bytes.
- */
-void guac_common_ssh_buffer_write_data(char** buffer, const char* data, int length);
-
-/**
- * Reads a single byte from the given buffer, advancing the buffer by one byte.
- *
- * @param buffer
- *     The buffer to read from.
- *
- * @return
- *     The value read from the buffer.
- */
-uint8_t guac_common_ssh_buffer_read_byte(char** buffer);
-
-/**
- * Reads an integer from the given buffer, advancing the buffer by four bytes.
- *
- * @param buffer
- *     The buffer to read from.
- *
- * @return
- *     The value read from the buffer.
- */
-uint32_t guac_common_ssh_buffer_read_uint32(char** buffer);
-
-/**
- * Reads a string and its length from the given buffer, advancing the buffer
- * by the size of the length (four bytes) and the size of the string, and
- * returning a pointer to the buffer. The length of the string is stored in
- * the given int.
- *
- * @param buffer
- *     The buffer to read from.
- *
- * @param length
- *     A pointer to an integer into which the length of the read string will
- *     be stored.
- *
- * @return
- *     A pointer to the value within the buffer.
- */
-char* guac_common_ssh_buffer_read_string(char** buffer, int* length);
-
-#endif
-

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/guac_ssh_key.c
----------------------------------------------------------------------
diff --git a/src/common-ssh/guac_ssh_key.c b/src/common-ssh/guac_ssh_key.c
deleted file mode 100644
index 7594b84..0000000
--- a/src/common-ssh/guac_ssh_key.c
+++ /dev/null
@@ -1,217 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#include "config.h"
-
-#include "guac_ssh_buffer.h"
-#include "guac_ssh_key.h"
-
-#include <openssl/bio.h>
-#include <openssl/bn.h>
-#include <openssl/dsa.h>
-#include <openssl/err.h>
-#include <openssl/evp.h>
-#include <openssl/obj_mac.h>
-#include <openssl/pem.h>
-#include <openssl/rsa.h>
-
-#include <stdlib.h>
-#include <string.h>
-
-guac_common_ssh_key* guac_common_ssh_key_alloc(char* data, int length,
-        char* passphrase) {
-
-    guac_common_ssh_key* key;
-    BIO* key_bio;
-
-    char* public_key;
-    char* pos;
-
-    /* Create BIO for reading key from memory */
-    key_bio = BIO_new_mem_buf(data, length);
-
-    /* If RSA key, load RSA */
-    if (length > sizeof(SSH_RSA_KEY_HEADER)-1
-            && memcmp(SSH_RSA_KEY_HEADER, data,
-                      sizeof(SSH_RSA_KEY_HEADER)-1) == 0) {
-
-        RSA* rsa_key;
-
-        /* Read key */
-        rsa_key = PEM_read_bio_RSAPrivateKey(key_bio, NULL, NULL, passphrase);
-        if (rsa_key == NULL)
-            return NULL;
-
-        /* Allocate key */
-        key = malloc(sizeof(guac_common_ssh_key));
-        key->rsa = rsa_key;
-
-        /* Set type */
-        key->type = SSH_KEY_RSA;
-
-        /* Allocate space for public key */
-        public_key = malloc(4096);
-        pos = public_key;
-
-        /* Derive public key */
-        guac_common_ssh_buffer_write_string(&pos, "ssh-rsa", sizeof("ssh-rsa")-1);
-        guac_common_ssh_buffer_write_bignum(&pos, rsa_key->e);
-        guac_common_ssh_buffer_write_bignum(&pos, rsa_key->n);
-
-        /* Save public key to structure */
-        key->public_key = public_key;
-        key->public_key_length = pos - public_key;
-
-    }
-
-    /* If DSA key, load DSA */
-    else if (length > sizeof(SSH_DSA_KEY_HEADER)-1
-            && memcmp(SSH_DSA_KEY_HEADER, data,
-                      sizeof(SSH_DSA_KEY_HEADER)-1) == 0) {
-
-        DSA* dsa_key;
-
-        /* Read key */
-        dsa_key = PEM_read_bio_DSAPrivateKey(key_bio, NULL, NULL, passphrase);
-        if (dsa_key == NULL)
-            return NULL;
-
-        /* Allocate key */
-        key = malloc(sizeof(guac_common_ssh_key));
-        key->dsa = dsa_key;
-
-        /* Set type */
-        key->type = SSH_KEY_DSA;
-
-        /* Allocate space for public key */
-        public_key = malloc(4096);
-        pos = public_key;
-
-        /* Derive public key */
-        guac_common_ssh_buffer_write_string(&pos, "ssh-dss", sizeof("ssh-dss")-1);
-        guac_common_ssh_buffer_write_bignum(&pos, dsa_key->p);
-        guac_common_ssh_buffer_write_bignum(&pos, dsa_key->q);
-        guac_common_ssh_buffer_write_bignum(&pos, dsa_key->g);
-        guac_common_ssh_buffer_write_bignum(&pos, dsa_key->pub_key);
-
-        /* Save public key to structure */
-        key->public_key = public_key;
-        key->public_key_length = pos - public_key;
-
-    }
-
-    /* Otherwise, unsupported type */
-    else {
-        BIO_free(key_bio);
-        return NULL;
-    }
-
-    /* Copy private key to structure */
-    key->private_key_length = length;
-    key->private_key = malloc(length);
-    memcpy(key->private_key, data, length);
-
-    BIO_free(key_bio);
-    return key;
-
-}
-
-const char* guac_common_ssh_key_error() {
-
-    /* Return static error string */
-    return ERR_reason_error_string(ERR_get_error());
-
-}
-
-void guac_common_ssh_key_free(guac_common_ssh_key* key) {
-
-    /* Free key-specific data */
-    if (key->type == SSH_KEY_RSA)
-        RSA_free(key->rsa);
-    else if (key->type == SSH_KEY_DSA)
-        DSA_free(key->dsa);
-
-    free(key->private_key);
-    free(key->public_key);
-    free(key);
-}
-
-int guac_common_ssh_key_sign(guac_common_ssh_key* key, const char* data,
-        int length, unsigned char* sig) {
-
-    const EVP_MD* md;
-    EVP_MD_CTX md_ctx;
-
-    unsigned char digest[EVP_MAX_MD_SIZE];
-    unsigned int dlen, len;
-
-    /* Get SHA1 digest */
-    if ((md = EVP_get_digestbynid(NID_sha1)) == NULL)
-        return -1;
-
-    /* Digest data */
-    EVP_DigestInit(&md_ctx, md);
-    EVP_DigestUpdate(&md_ctx, data, length);
-    EVP_DigestFinal(&md_ctx, digest, &dlen);
-
-    /* Sign with key */
-    switch (key->type) {
-
-        case SSH_KEY_RSA:
-            if (RSA_sign(NID_sha1, digest, dlen, sig, &len, key->rsa) == 1)
-                return len;
-            break;
-
-        case SSH_KEY_DSA: {
-
-            DSA_SIG* dsa_sig = DSA_do_sign(digest, dlen, key->dsa);
-            if (dsa_sig != NULL) {
-
-                /* Compute size of each half of signature */
-                int rlen = BN_num_bytes(dsa_sig->r);
-                int slen = BN_num_bytes(dsa_sig->s);
-
-                /* Ensure each number is within the required size */
-                if (rlen > DSA_SIG_NUMBER_SIZE || slen > DSA_SIG_NUMBER_SIZE)
-                    return -1;
-
-                /* Init to all zeroes */
-                memset(sig, 0, DSA_SIG_SIZE);
-
-                /* Add R at the end of the first block of the signature */
-                BN_bn2bin(dsa_sig->r, sig + DSA_SIG_SIZE
-                                          - DSA_SIG_NUMBER_SIZE - rlen);
-
-                /* Add S at the end of the second block of the signature */
-                BN_bn2bin(dsa_sig->s, sig + DSA_SIG_SIZE - slen);
-
-                /* Done */
-                DSA_SIG_free(dsa_sig);
-                return DSA_SIG_SIZE;
-
-            }
-
-        }
-
-    }
-
-    return -1;
-
-}
-

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/guac_ssh_key.h
----------------------------------------------------------------------
diff --git a/src/common-ssh/guac_ssh_key.h b/src/common-ssh/guac_ssh_key.h
deleted file mode 100644
index 1754dea..0000000
--- a/src/common-ssh/guac_ssh_key.h
+++ /dev/null
@@ -1,170 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef GUAC_COMMON_SSH_KEY_H
-#define GUAC_COMMON_SSH_KEY_H
-
-#include "config.h"
-
-#include <openssl/ossl_typ.h>
-
-/**
- * The expected header of RSA private keys.
- */
-#define SSH_RSA_KEY_HEADER "-----BEGIN RSA PRIVATE KEY-----"
-
-/**
- * The expected header of DSA private keys.
- */
-#define SSH_DSA_KEY_HEADER "-----BEGIN DSA PRIVATE KEY-----"
-
-/**
- * The size of single number within a DSA signature, in bytes.
- */
-#define DSA_SIG_NUMBER_SIZE 20
-
-/**
- * The size of a DSA signature, in bytes.
- */
-#define DSA_SIG_SIZE DSA_SIG_NUMBER_SIZE*2 
-
-/**
- * The type of an SSH key.
- */
-typedef enum guac_common_ssh_key_type {
-
-    /**
-     * RSA key.
-     */
-    SSH_KEY_RSA,
-
-    /**
-     * DSA key.
-     */
-    SSH_KEY_DSA
-
-} guac_common_ssh_key_type;
-
-/**
- * Abstraction of a key used for SSH authentication.
- */
-typedef struct guac_common_ssh_key {
-
-    /**
-     * The type of this key.
-     */
-    guac_common_ssh_key_type type;
-
-    /**
-     * Underlying RSA private key, if any.
-     */
-    RSA* rsa;
-
-    /**
-     * Underlying DSA private key, if any.
-     */
-    DSA* dsa;
-
-    /**
-     * The associated public key, encoded as necessary for SSH.
-     */
-    char* public_key;
-
-    /**
-     * The length of the public key, in bytes.
-     */
-    int public_key_length;
-
-    /**
-     * The private key, encoded as necessary for SSH.
-     */
-    char* private_key;
-
-    /**
-     * The length of the private key, in bytes.
-     */
-    int private_key_length;
-
-} guac_common_ssh_key;
-
-/**
- * Allocates a new key containing the given private key data and specified
- * passphrase. If unable to read the key, NULL is returned.
- *
- * @param data
- *     The base64-encoded data to decode when reading the key.
- *
- * @param length
- *     The length of the provided data, in bytes.
- *
- * @param passphrase
- *     The passphrase to use when decrypting the key, if any, or an empty
- *     string or NULL if no passphrase is needed.
- *
- * @return
- *     The decoded, decrypted private key, or NULL if the key could not be
- *     decoded.
- */
-guac_common_ssh_key* guac_common_ssh_key_alloc(char* data, int length,
-        char* passphrase);
-
-/**
- * Returns a statically-allocated string describing the most recent SSH key
- * error.
- *
- * @return
- *     A statically-allocated string describing the most recent SSH key error.
- */
-const char* guac_common_ssh_key_error();
-
-/**
- * Frees all memory associated with the given key.
- *
- * @param key
- *     The key to free.
- */
-void guac_common_ssh_key_free(guac_common_ssh_key* key);
-
-/**
- * Signs the given data using the given key, returning the length of the
- * signature in bytes, or a value less than zero on error.
- *
- * @param key
- *     The key to use when signing the given data.
- *
- * @param data
- *     The arbitrary data to sign.
- *
- * @param length
- *     The length of the arbitrary data being signed, in bytes.
- *
- * @param sig
- *     The buffer into which the signature should be written. The buffer must
- *     be at least DSA_SIG_SIZE for DSA keys. For RSA keys, the signature size
- *     is dependent only on key size, and is equal to the length of the
- *     modulus, in bytes.
- *
- * @return
- *     The number of bytes in the resulting signature.
- */
-int guac_common_ssh_key_sign(guac_common_ssh_key* key, const char* data,
-        int length, unsigned char* sig);
-
-#endif
-

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/guac_ssh_user.c
----------------------------------------------------------------------
diff --git a/src/common-ssh/guac_ssh_user.c b/src/common-ssh/guac_ssh_user.c
deleted file mode 100644
index 73319c0..0000000
--- a/src/common-ssh/guac_ssh_user.c
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#include "guac_ssh_key.h"
-#include "guac_ssh_user.h"
-
-#include <stdlib.h>
-#include <string.h>
-
-guac_common_ssh_user* guac_common_ssh_create_user(const char* username) {
-
-    guac_common_ssh_user* user = malloc(sizeof(guac_common_ssh_user));
-
-    /* Init user */
-    user->username = strdup(username);
-    user->password = NULL;
-    user->private_key = NULL;
-
-    return user;
-
-}
-
-void guac_common_ssh_destroy_user(guac_common_ssh_user* user) {
-
-    /* Free private key, if present */
-    if (user->private_key != NULL)
-        guac_common_ssh_key_free(user->private_key);
-
-    /* Free all other data */
-    free(user->password);
-    free(user->username);
-    free(user);
-
-}
-
-void guac_common_ssh_user_set_password(guac_common_ssh_user* user,
-        const char* password) {
-
-    /* Replace current password with given value */
-    free(user->password);
-    user->password = strdup(password);
-
-}
-
-int guac_common_ssh_user_import_key(guac_common_ssh_user* user,
-        char* private_key, char* passphrase) {
-
-    /* Free existing private key, if present */
-    if (user->private_key != NULL)
-        guac_common_ssh_key_free(user->private_key);
-
-    /* Attempt to read key without passphrase if none given */
-    if (passphrase == NULL)
-        user->private_key = guac_common_ssh_key_alloc(private_key,
-                strlen(private_key), "");
-
-    /* Otherwise, use provided passphrase */
-    else
-        user->private_key = guac_common_ssh_key_alloc(private_key,
-                strlen(private_key), passphrase);
-
-    /* Fail if key could not be read */
-    return user->private_key == NULL;
-
-}
-

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/guac_ssh_user.h
----------------------------------------------------------------------
diff --git a/src/common-ssh/guac_ssh_user.h b/src/common-ssh/guac_ssh_user.h
deleted file mode 100644
index eeaa460..0000000
--- a/src/common-ssh/guac_ssh_user.h
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef GUAC_COMMON_SSH_USER_H
-#define GUAC_COMMON_SSH_USER_H
-
-#include "guac_ssh_key.h"
-
-/**
- * Data describing an SSH user, including their credentials.
- */
-typedef struct guac_common_ssh_user {
-
-    /**
-     * The username of this user.
-     */
-    char* username;
-
-    /**
-     * The password which should be used to authenticate this user, if any, or
-     * NULL if a private key will be used instead.
-     */
-    char* password;
-
-    /**
-     * The private key which should be used to authenticate this user, if any,
-     * or NULL if a password will be used instead.
-     */
-    guac_common_ssh_key* private_key;
-
-} guac_common_ssh_user;
-
-/**
- * Creates a new SSH user with the given username. When additionally populated
- * with a password or private key, this user can then be used for
- * authentication.
- *
- * @param username
- *     The username of the user being created.
- *
- * @return
- *     A new SSH user having the given username, but no associated password
- *     or private key.
- */
-guac_common_ssh_user* guac_common_ssh_create_user(const char* username);
-
-/**
- * Destroys the given user object, releasing all associated resources.
- *
- * @param user
- *     The user to destroy.
- */
-void guac_common_ssh_destroy_user(guac_common_ssh_user* user);
-
-/**
- * Associates the given user with the given password, such that that password
- * is used for future authentication attempts.
- *
- * @param user
- *     The user to associate with the given password.
- *
- * @param password
- *     The password to associate with the given user.
- */
-void guac_common_ssh_user_set_password(guac_common_ssh_user* user,
-        const char* password);
-
-/**
- * Imports the given private key, associating that key with the given user. If
- * necessary to decrypt the key, a passphrase may be specified. The private key
- * must be provided in base64 form. If the private key is imported
- * successfully, it will be used for future authentication attempts.
- *
- * @param user
- *     The user to associate with the given private key.
- *
- * @param private_key
- *     The base64-encoded private key to import.
- *
- * @param passphrase
- *     The passphrase to use to decrypt the given private key, or NULL if no
- *     passphrase should be used.
- *
- * @return
- *     Zero if the private key is successfully imported, or non-zero if the
- *     private key could not be imported due to an error.
- */
-int guac_common_ssh_user_import_key(guac_common_ssh_user* user,
-        char* private_key, char* passphrase);
-
-#endif
-

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/key.c
----------------------------------------------------------------------
diff --git a/src/common-ssh/key.c b/src/common-ssh/key.c
new file mode 100644
index 0000000..82a3252
--- /dev/null
+++ b/src/common-ssh/key.c
@@ -0,0 +1,217 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "config.h"
+
+#include "common-ssh/buffer.h"
+#include "common-ssh/key.h"
+
+#include <openssl/bio.h>
+#include <openssl/bn.h>
+#include <openssl/dsa.h>
+#include <openssl/err.h>
+#include <openssl/evp.h>
+#include <openssl/obj_mac.h>
+#include <openssl/pem.h>
+#include <openssl/rsa.h>
+
+#include <stdlib.h>
+#include <string.h>
+
+guac_common_ssh_key* guac_common_ssh_key_alloc(char* data, int length,
+        char* passphrase) {
+
+    guac_common_ssh_key* key;
+    BIO* key_bio;
+
+    char* public_key;
+    char* pos;
+
+    /* Create BIO for reading key from memory */
+    key_bio = BIO_new_mem_buf(data, length);
+
+    /* If RSA key, load RSA */
+    if (length > sizeof(SSH_RSA_KEY_HEADER)-1
+            && memcmp(SSH_RSA_KEY_HEADER, data,
+                      sizeof(SSH_RSA_KEY_HEADER)-1) == 0) {
+
+        RSA* rsa_key;
+
+        /* Read key */
+        rsa_key = PEM_read_bio_RSAPrivateKey(key_bio, NULL, NULL, passphrase);
+        if (rsa_key == NULL)
+            return NULL;
+
+        /* Allocate key */
+        key = malloc(sizeof(guac_common_ssh_key));
+        key->rsa = rsa_key;
+
+        /* Set type */
+        key->type = SSH_KEY_RSA;
+
+        /* Allocate space for public key */
+        public_key = malloc(4096);
+        pos = public_key;
+
+        /* Derive public key */
+        guac_common_ssh_buffer_write_string(&pos, "ssh-rsa", sizeof("ssh-rsa")-1);
+        guac_common_ssh_buffer_write_bignum(&pos, rsa_key->e);
+        guac_common_ssh_buffer_write_bignum(&pos, rsa_key->n);
+
+        /* Save public key to structure */
+        key->public_key = public_key;
+        key->public_key_length = pos - public_key;
+
+    }
+
+    /* If DSA key, load DSA */
+    else if (length > sizeof(SSH_DSA_KEY_HEADER)-1
+            && memcmp(SSH_DSA_KEY_HEADER, data,
+                      sizeof(SSH_DSA_KEY_HEADER)-1) == 0) {
+
+        DSA* dsa_key;
+
+        /* Read key */
+        dsa_key = PEM_read_bio_DSAPrivateKey(key_bio, NULL, NULL, passphrase);
+        if (dsa_key == NULL)
+            return NULL;
+
+        /* Allocate key */
+        key = malloc(sizeof(guac_common_ssh_key));
+        key->dsa = dsa_key;
+
+        /* Set type */
+        key->type = SSH_KEY_DSA;
+
+        /* Allocate space for public key */
+        public_key = malloc(4096);
+        pos = public_key;
+
+        /* Derive public key */
+        guac_common_ssh_buffer_write_string(&pos, "ssh-dss", sizeof("ssh-dss")-1);
+        guac_common_ssh_buffer_write_bignum(&pos, dsa_key->p);
+        guac_common_ssh_buffer_write_bignum(&pos, dsa_key->q);
+        guac_common_ssh_buffer_write_bignum(&pos, dsa_key->g);
+        guac_common_ssh_buffer_write_bignum(&pos, dsa_key->pub_key);
+
+        /* Save public key to structure */
+        key->public_key = public_key;
+        key->public_key_length = pos - public_key;
+
+    }
+
+    /* Otherwise, unsupported type */
+    else {
+        BIO_free(key_bio);
+        return NULL;
+    }
+
+    /* Copy private key to structure */
+    key->private_key_length = length;
+    key->private_key = malloc(length);
+    memcpy(key->private_key, data, length);
+
+    BIO_free(key_bio);
+    return key;
+
+}
+
+const char* guac_common_ssh_key_error() {
+
+    /* Return static error string */
+    return ERR_reason_error_string(ERR_get_error());
+
+}
+
+void guac_common_ssh_key_free(guac_common_ssh_key* key) {
+
+    /* Free key-specific data */
+    if (key->type == SSH_KEY_RSA)
+        RSA_free(key->rsa);
+    else if (key->type == SSH_KEY_DSA)
+        DSA_free(key->dsa);
+
+    free(key->private_key);
+    free(key->public_key);
+    free(key);
+}
+
+int guac_common_ssh_key_sign(guac_common_ssh_key* key, const char* data,
+        int length, unsigned char* sig) {
+
+    const EVP_MD* md;
+    EVP_MD_CTX md_ctx;
+
+    unsigned char digest[EVP_MAX_MD_SIZE];
+    unsigned int dlen, len;
+
+    /* Get SHA1 digest */
+    if ((md = EVP_get_digestbynid(NID_sha1)) == NULL)
+        return -1;
+
+    /* Digest data */
+    EVP_DigestInit(&md_ctx, md);
+    EVP_DigestUpdate(&md_ctx, data, length);
+    EVP_DigestFinal(&md_ctx, digest, &dlen);
+
+    /* Sign with key */
+    switch (key->type) {
+
+        case SSH_KEY_RSA:
+            if (RSA_sign(NID_sha1, digest, dlen, sig, &len, key->rsa) == 1)
+                return len;
+            break;
+
+        case SSH_KEY_DSA: {
+
+            DSA_SIG* dsa_sig = DSA_do_sign(digest, dlen, key->dsa);
+            if (dsa_sig != NULL) {
+
+                /* Compute size of each half of signature */
+                int rlen = BN_num_bytes(dsa_sig->r);
+                int slen = BN_num_bytes(dsa_sig->s);
+
+                /* Ensure each number is within the required size */
+                if (rlen > DSA_SIG_NUMBER_SIZE || slen > DSA_SIG_NUMBER_SIZE)
+                    return -1;
+
+                /* Init to all zeroes */
+                memset(sig, 0, DSA_SIG_SIZE);
+
+                /* Add R at the end of the first block of the signature */
+                BN_bn2bin(dsa_sig->r, sig + DSA_SIG_SIZE
+                                          - DSA_SIG_NUMBER_SIZE - rlen);
+
+                /* Add S at the end of the second block of the signature */
+                BN_bn2bin(dsa_sig->s, sig + DSA_SIG_SIZE - slen);
+
+                /* Done */
+                DSA_SIG_free(dsa_sig);
+                return DSA_SIG_SIZE;
+
+            }
+
+        }
+
+    }
+
+    return -1;
+
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/sftp.c
----------------------------------------------------------------------
diff --git a/src/common-ssh/sftp.c b/src/common-ssh/sftp.c
new file mode 100644
index 0000000..79db35e
--- /dev/null
+++ b/src/common-ssh/sftp.c
@@ -0,0 +1,783 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "common-ssh/sftp.h"
+#include "common-ssh/ssh.h"
+
+#include <guacamole/client.h>
+#include <guacamole/object.h>
+#include <guacamole/protocol.h>
+#include <guacamole/socket.h>
+#include <guacamole/user.h>
+#include <libssh2.h>
+
+#include <fcntl.h>
+#include <libgen.h>
+#include <stdlib.h>
+#include <string.h>
+
+/**
+ * Translates the last error message received by the SFTP layer of an SSH
+ * session into a Guacamole protocol status code.
+ *
+ * @param filesystem
+ *     The object (not guac_object) defining the filesystem associated with the
+ *     SFTP and SSH sessions.
+ *
+ * @return
+ *     The Guacamole protocol status code corresponding to the last reported
+ *     error of the SFTP layer, if nay, or GUAC_PROTOCOL_STATUS_SUCCESS if no
+ *     error has occurred.
+ */
+static guac_protocol_status guac_sftp_get_status(
+        guac_common_ssh_sftp_filesystem* filesystem) {
+
+    /* Get libssh2 objects */
+    LIBSSH2_SFTP*    sftp    = filesystem->sftp_session;
+    LIBSSH2_SESSION* session = filesystem->ssh_session->session;
+
+    /* Return success code if no error occurred */
+    if (libssh2_session_last_errno(session) != LIBSSH2_ERROR_SFTP_PROTOCOL)
+        return GUAC_PROTOCOL_STATUS_SUCCESS;
+
+    /* Translate SFTP error codes defined by
+     * https://tools.ietf.org/html/draft-ietf-secsh-filexfer-02 (the most
+     * commonly-implemented standard) */
+    switch (libssh2_sftp_last_error(sftp)) {
+
+        /* SSH_FX_OK (not an error) */
+        case 0:
+            return GUAC_PROTOCOL_STATUS_SUCCESS;
+
+        /* SSH_FX_EOF (technically not an error) */
+        case 1:
+            return GUAC_PROTOCOL_STATUS_SUCCESS;
+
+        /* SSH_FX_NO_SUCH_FILE */
+        case 2:
+            return GUAC_PROTOCOL_STATUS_RESOURCE_NOT_FOUND;
+
+        /* SSH_FX_PERMISSION_DENIED */
+        case 3:
+            return GUAC_PROTOCOL_STATUS_CLIENT_FORBIDDEN;
+
+        /* SSH_FX_FAILURE */
+        case 4:
+            return GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR;
+
+        /* SSH_FX_BAD_MESSAGE */
+        case 5:
+            return GUAC_PROTOCOL_STATUS_SERVER_ERROR;
+
+        /* SSH_FX_NO_CONNECTION / SSH_FX_CONNECTION_LOST */
+        case 6:
+        case 7:
+            return GUAC_PROTOCOL_STATUS_UPSTREAM_TIMEOUT;
+
+        /* SSH_FX_OP_UNSUPPORTED */
+        case 8:
+            return GUAC_PROTOCOL_STATUS_UNSUPPORTED;
+
+        /* Return generic error if cause unknown */
+        default:
+            return GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR;
+
+    }
+
+}
+
+/**
+ * Concatenates the given filename with the given path, separating the two
+ * with a single forward slash. The full result must be no more than
+ * GUAC_COMMON_SSH_SFTP_MAX_PATH bytes long, counting null terminator.
+ *
+ * @param fullpath
+ *     The buffer to store the result within. This buffer must be at least
+ *     GUAC_COMMON_SSH_SFTP_MAX_PATH bytes long.
+ *
+ * @param path
+ *     The path to append the filename to.
+ *
+ * @param filename
+ *     The filename to append to the path.
+ *
+ * @return
+ *     Non-zero if the filename is valid and was successfully appended to the
+ *     path, zero otherwise.
+ */
+static int guac_ssh_append_filename(char* fullpath, const char* path,
+        const char* filename) {
+
+    int i;
+
+    /* Disallow "." as a filename */
+    if (strcmp(filename, ".") == 0)
+        return 0;
+
+    /* Disallow ".." as a filename */
+    if (strcmp(filename, "..") == 0)
+        return 0;
+
+    /* Copy path, append trailing slash */
+    for (i=0; i<GUAC_COMMON_SSH_SFTP_MAX_PATH; i++) {
+
+        /*
+         * Append trailing slash only if:
+         *  1) Trailing slash is not already present
+         *  2) Path is non-empty
+         */
+
+        char c = path[i];
+        if (c == '\0') {
+            if (i > 0 && path[i-1] != '/')
+                fullpath[i++] = '/';
+            break;
+        }
+
+        /* Copy character if not end of string */
+        fullpath[i] = c;
+
+    }
+
+    /* Append filename */
+    for (; i<GUAC_COMMON_SSH_SFTP_MAX_PATH; i++) {
+
+        char c = *(filename++);
+        if (c == '\0')
+            break;
+
+        /* Filenames may not contain slashes */
+        if (c == '\\' || c == '/')
+            return 0;
+
+        /* Append each character within filename */
+        fullpath[i] = c;
+
+    }
+
+    /* Verify path length is within maximum */
+    if (i == GUAC_COMMON_SSH_SFTP_MAX_PATH)
+        return 0;
+
+    /* Terminate path string */
+    fullpath[i] = '\0';
+
+    /* Append was successful */
+    return 1;
+
+}
+
+/**
+ * Handler for blob messages which continue an inbound SFTP data transfer
+ * (upload). The data associated with the given stream is expected to be a
+ * pointer to an open LIBSSH2_SFTP_HANDLE for the file to which the data
+ * should be written.
+ *
+ * @param user
+ *     The user receiving the blob message.
+ *
+ * @param stream
+ *     The Guacamole protocol stream associated with the received blob message.
+ *
+ * @param data
+ *     The data received within the blob.
+ *
+ * @param length
+ *     The length of the received data, in bytes.
+ *
+ * @return
+ *     Zero if the blob is handled successfully, or non-zero on error.
+ */
+static int guac_common_ssh_sftp_blob_handler(guac_user* user,
+        guac_stream* stream, void* data, int length) {
+
+    /* Pull file from stream */
+    LIBSSH2_SFTP_HANDLE* file = (LIBSSH2_SFTP_HANDLE*) stream->data;
+
+    /* Attempt write */
+    if (libssh2_sftp_write(file, data, length) == length) {
+        guac_user_log(user, GUAC_LOG_DEBUG, "%i bytes written", length);
+        guac_protocol_send_ack(user->socket, stream, "SFTP: OK",
+                GUAC_PROTOCOL_STATUS_SUCCESS);
+        guac_socket_flush(user->socket);
+    }
+
+    /* Inform of any errors */
+    else {
+        guac_user_log(user, GUAC_LOG_INFO, "Unable to write to file");
+        guac_protocol_send_ack(user->socket, stream, "SFTP: Write failed",
+                GUAC_PROTOCOL_STATUS_SERVER_ERROR);
+        guac_socket_flush(user->socket);
+    }
+
+    return 0;
+
+}
+
+/**
+ * Handler for end messages which terminate an inbound SFTP data transfer
+ * (upload). The data associated with the given stream is expected to be a
+ * pointer to an open LIBSSH2_SFTP_HANDLE for the file to which the data
+ * has been written and which should now be closed.
+ *
+ * @param user
+ *     The user receiving the end message.
+ *
+ * @param stream
+ *     The Guacamole protocol stream associated with the received end message.
+ *
+ * @return
+ *     Zero if the file is closed successfully, or non-zero on error.
+ */
+static int guac_common_ssh_sftp_end_handler(guac_user* user,
+        guac_stream* stream) {
+
+    /* Pull file from stream */
+    LIBSSH2_SFTP_HANDLE* file = (LIBSSH2_SFTP_HANDLE*) stream->data;
+
+    /* Attempt to close file */
+    if (libssh2_sftp_close(file) == 0) {
+        guac_user_log(user, GUAC_LOG_DEBUG, "File closed");
+        guac_protocol_send_ack(user->socket, stream, "SFTP: OK",
+                GUAC_PROTOCOL_STATUS_SUCCESS);
+        guac_socket_flush(user->socket);
+    }
+    else {
+        guac_user_log(user, GUAC_LOG_INFO, "Unable to close file");
+        guac_protocol_send_ack(user->socket, stream, "SFTP: Close failed",
+                GUAC_PROTOCOL_STATUS_SERVER_ERROR);
+        guac_socket_flush(user->socket);
+    }
+
+    return 0;
+
+}
+
+int guac_common_ssh_sftp_handle_file_stream(
+        guac_common_ssh_sftp_filesystem* filesystem, guac_user* user,
+        guac_stream* stream, char* mimetype, char* filename) {
+
+    char fullpath[GUAC_COMMON_SSH_SFTP_MAX_PATH];
+    LIBSSH2_SFTP_HANDLE* file;
+
+    /* Concatenate filename with path */
+    if (!guac_ssh_append_filename(fullpath, filesystem->upload_path,
+                filename)) {
+
+        guac_user_log(user, GUAC_LOG_DEBUG,
+                "Filename \"%s\" is invalid or resulting path is too long",
+                filename);
+
+        /* Abort transfer - invalid filename */
+        guac_protocol_send_ack(user->socket, stream, 
+                "SFTP: Illegal filename",
+                GUAC_PROTOCOL_STATUS_CLIENT_BAD_REQUEST);
+
+        guac_socket_flush(user->socket);
+        return 0;
+    }
+
+    /* Open file via SFTP */
+    file = libssh2_sftp_open(filesystem->sftp_session, fullpath,
+            LIBSSH2_FXF_WRITE | LIBSSH2_FXF_CREAT | LIBSSH2_FXF_TRUNC,
+            S_IRUSR | S_IWUSR);
+
+    /* Inform of status */
+    if (file != NULL) {
+
+        guac_user_log(user, GUAC_LOG_DEBUG,
+                "File \"%s\" opened",
+                fullpath);
+
+        guac_protocol_send_ack(user->socket, stream, "SFTP: File opened",
+                GUAC_PROTOCOL_STATUS_SUCCESS);
+        guac_socket_flush(user->socket);
+    }
+    else {
+        guac_user_log(user, GUAC_LOG_INFO,
+                "Unable to open file \"%s\"", fullpath);
+        guac_protocol_send_ack(user->socket, stream, "SFTP: Open failed",
+                guac_sftp_get_status(filesystem));
+        guac_socket_flush(user->socket);
+    }
+
+    /* Set handlers for file stream */
+    stream->blob_handler = guac_common_ssh_sftp_blob_handler;
+    stream->end_handler = guac_common_ssh_sftp_end_handler;
+
+    /* Store file within stream */
+    stream->data = file;
+    return 0;
+
+}
+
+/**
+ * Handler for ack messages which continue an outbound SFTP data transfer
+ * (download), signalling the current status and requesting additional data.
+ * The data associated with the given stream is expected to be a pointer to an
+ * open LIBSSH2_SFTP_HANDLE for the file from which the data is to be read.
+ *
+ * @param user
+ *     The user receiving the ack message.
+ *
+ * @param stream
+ *     The Guacamole protocol stream associated with the received ack message.
+ *
+ * @param message
+ *     An arbitrary human-readable message describing the nature of the
+ *     success or failure denoted by the ack message.
+ *
+ * @param status
+ *     The status code associated with the ack message, which may indicate
+ *     success or an error.
+ *
+ * @return
+ *     Zero if the file is read from successfully, or non-zero on error.
+ */
+static int guac_common_ssh_sftp_ack_handler(guac_user* user,
+        guac_stream* stream, char* message, guac_protocol_status status) {
+
+    /* Pull file from stream */
+    LIBSSH2_SFTP_HANDLE* file = (LIBSSH2_SFTP_HANDLE*) stream->data;
+
+    /* If successful, read data */
+    if (status == GUAC_PROTOCOL_STATUS_SUCCESS) {
+
+        /* Attempt read into buffer */
+        char buffer[4096];
+        int bytes_read = libssh2_sftp_read(file, buffer, sizeof(buffer)); 
+
+        /* If bytes read, send as blob */
+        if (bytes_read > 0) {
+            guac_protocol_send_blob(user->socket, stream,
+                    buffer, bytes_read);
+
+            guac_user_log(user, GUAC_LOG_DEBUG, "%i bytes sent to user",
+                    bytes_read);
+
+        }
+
+        /* If EOF, send end */
+        else if (bytes_read == 0) {
+            guac_user_log(user, GUAC_LOG_DEBUG, "File sent");
+            guac_protocol_send_end(user->socket, stream);
+            guac_user_free_stream(user, stream);
+        }
+
+        /* Otherwise, fail stream */
+        else {
+            guac_user_log(user, GUAC_LOG_INFO, "Error reading file");
+            guac_protocol_send_end(user->socket, stream);
+            guac_user_free_stream(user, stream);
+        }
+
+        guac_socket_flush(user->socket);
+
+    }
+
+    /* Otherwise, return stream to user */
+    else
+        guac_user_free_stream(user, stream);
+
+    return 0;
+}
+
+guac_stream* guac_common_ssh_sftp_download_file(
+        guac_common_ssh_sftp_filesystem* filesystem, guac_user* user,
+        char* filename) {
+
+    guac_stream* stream;
+    LIBSSH2_SFTP_HANDLE* file;
+
+    /* Attempt to open file for reading */
+    file = libssh2_sftp_open(filesystem->sftp_session, filename,
+            LIBSSH2_FXF_READ, 0);
+    if (file == NULL) {
+        guac_user_log(user, GUAC_LOG_INFO, 
+                "Unable to read file \"%s\"", filename);
+        return NULL;
+    }
+
+    /* Allocate stream */
+    stream = guac_user_alloc_stream(user);
+    stream->ack_handler = guac_common_ssh_sftp_ack_handler;
+    stream->data = file;
+
+    /* Send stream start, strip name */
+    filename = basename(filename);
+    guac_protocol_send_file(user->socket, stream,
+            "application/octet-stream", filename);
+    guac_socket_flush(user->socket);
+
+    guac_user_log(user, GUAC_LOG_DEBUG, "Sending file \"%s\"", filename);
+    return stream;
+
+}
+
+void guac_common_ssh_sftp_set_upload_path(
+        guac_common_ssh_sftp_filesystem* filesystem, const char* path) {
+
+    guac_client* client = filesystem->ssh_session->client;
+
+    /* Ignore requests which exceed maximum-allowed path */
+    int length = strnlen(path, GUAC_COMMON_SSH_SFTP_MAX_PATH)+1;
+    if (length > GUAC_COMMON_SSH_SFTP_MAX_PATH) {
+        guac_client_log(client, GUAC_LOG_ERROR,
+                "Submitted path exceeds limit of %i bytes",
+                GUAC_COMMON_SSH_SFTP_MAX_PATH);
+        return;
+    }
+
+    /* Copy path */
+    memcpy(filesystem->upload_path, path, length);
+    guac_client_log(client, GUAC_LOG_DEBUG, "Upload path set to \"%s\"", path);
+
+}
+
+/**
+ * Handler for ack messages received due to receipt of a "body" or "blob"
+ * instruction associated with a SFTP directory list operation.
+ *
+ * @param user
+ *     The user receiving the ack message.
+ *
+ * @param stream
+ *     The Guacamole protocol stream associated with the received ack message.
+ *
+ * @param message
+ *     An arbitrary human-readable message describing the nature of the
+ *     success or failure denoted by this ack message.
+ *
+ * @param status
+ *     The status code associated with this ack message, which may indicate
+ *     success or an error.
+ *
+ * @return
+ *     Zero on success, non-zero on error.
+ */
+static int guac_common_ssh_sftp_ls_ack_handler(guac_user* user,
+        guac_stream* stream, char* message, guac_protocol_status status) {
+
+    int bytes_read;
+    int blob_written = 0;
+
+    char filename[GUAC_COMMON_SSH_SFTP_MAX_PATH];
+    LIBSSH2_SFTP_ATTRIBUTES attributes;
+
+    guac_common_ssh_sftp_ls_state* list_state =
+        (guac_common_ssh_sftp_ls_state*) stream->data;
+
+    guac_common_ssh_sftp_filesystem* filesystem = list_state->filesystem;
+
+    LIBSSH2_SFTP* sftp = filesystem->sftp_session;
+
+    /* If unsuccessful, free stream and abort */
+    if (status != GUAC_PROTOCOL_STATUS_SUCCESS) {
+        libssh2_sftp_closedir(list_state->directory);
+        guac_user_free_stream(user, stream);
+        free(list_state);
+        return 0;
+    }
+
+    /* While directory entries remain */
+    while ((bytes_read = libssh2_sftp_readdir(list_state->directory,
+                filename, sizeof(filename), &attributes)) > 0
+            && !blob_written) {
+
+        char absolute_path[GUAC_COMMON_SSH_SFTP_MAX_PATH];
+
+        /* Skip current and parent directory entries */
+        if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0)
+            continue;
+
+        /* Concatenate into absolute path - skip if invalid */
+        if (!guac_ssh_append_filename(absolute_path, 
+                    list_state->directory_name, filename)) {
+
+            guac_user_log(user, GUAC_LOG_DEBUG,
+                    "Skipping filename \"%s\" - filename is invalid or "
+                    "resulting path is too long", filename);
+
+            continue;
+        }
+
+        /* Stat explicitly if symbolic link (might point to directory) */
+        if (LIBSSH2_SFTP_S_ISLNK(attributes.permissions))
+            libssh2_sftp_stat(sftp, absolute_path, &attributes);
+
+        /* Determine mimetype */
+        const char* mimetype;
+        if (LIBSSH2_SFTP_S_ISDIR(attributes.permissions))
+            mimetype = GUAC_USER_STREAM_INDEX_MIMETYPE;
+        else
+            mimetype = "application/octet-stream";
+
+        /* Write entry */
+        blob_written |= guac_common_json_write_property(user, stream,
+                &list_state->json_state, absolute_path, mimetype);
+
+    }
+
+    /* Complete JSON and cleanup at end of directory */
+    if (bytes_read <= 0) {
+
+        /* Complete JSON object */
+        guac_common_json_end_object(user, stream, &list_state->json_state);
+        guac_common_json_flush(user, stream, &list_state->json_state);
+
+        /* Clean up resources */
+        libssh2_sftp_closedir(list_state->directory);
+        free(list_state);
+
+        /* Signal of stream */
+        guac_protocol_send_end(user->socket, stream);
+        guac_user_free_stream(user, stream);
+
+    }
+
+    guac_socket_flush(user->socket);
+    return 0;
+
+}
+
+/**
+ * Handler for get messages. In context of SFTP and the filesystem exposed via
+ * the Guacamole protocol, get messages request the body of a file within the
+ * filesystem.
+ *
+ * @param user
+ *     The user who sent the get message.
+ *
+ * @param object
+ *     The Guacamole protocol object associated with the get request itself.
+ *
+ * @param name
+ *     The name of the input stream (file) being requested.
+ *
+ * @return
+ *     Zero on success, non-zero on error.
+ */
+static int guac_common_ssh_sftp_get_handler(guac_user* user,
+        guac_object* object, char* name) {
+
+    guac_common_ssh_sftp_filesystem* filesystem =
+        (guac_common_ssh_sftp_filesystem*) object->data;
+
+    LIBSSH2_SFTP* sftp = filesystem->sftp_session;
+    LIBSSH2_SFTP_ATTRIBUTES attributes;
+
+    /* Attempt to read file information */
+    if (libssh2_sftp_stat(sftp, name, &attributes)) {
+        guac_user_log(user, GUAC_LOG_INFO, "Unable to read file \"%s\"",
+                name);
+        return 0;
+    }
+
+    /* If directory, send contents of directory */
+    if (LIBSSH2_SFTP_S_ISDIR(attributes.permissions)) {
+
+        /* Open as directory */
+        LIBSSH2_SFTP_HANDLE* dir = libssh2_sftp_opendir(sftp, name);
+        if (dir == NULL) {
+            guac_user_log(user, GUAC_LOG_INFO,
+                    "Unable to read directory \"%s\"", name);
+            return 0;
+        }
+
+        /* Init directory listing state */
+        guac_common_ssh_sftp_ls_state* list_state =
+            malloc(sizeof(guac_common_ssh_sftp_ls_state));
+
+        list_state->directory = dir;
+        list_state->filesystem = filesystem;
+        strncpy(list_state->directory_name, name,
+                sizeof(list_state->directory_name) - 1);
+
+        /* Allocate stream for body */
+        guac_stream* stream = guac_user_alloc_stream(user);
+        stream->ack_handler = guac_common_ssh_sftp_ls_ack_handler;
+        stream->data = list_state;
+
+        /* Init JSON object state */
+        guac_common_json_begin_object(user, stream, &list_state->json_state);
+
+        /* Associate new stream with get request */
+        guac_protocol_send_body(user->socket, object, stream,
+                GUAC_USER_STREAM_INDEX_MIMETYPE, name);
+
+    }
+
+    /* Otherwise, send file contents */
+    else {
+
+        /* Open as normal file */
+        LIBSSH2_SFTP_HANDLE* file = libssh2_sftp_open(sftp, name,
+            LIBSSH2_FXF_READ, 0);
+        if (file == NULL) {
+            guac_user_log(user, GUAC_LOG_INFO,
+                    "Unable to read file \"%s\"", name);
+            return 0;
+        }
+
+        /* Allocate stream for body */
+        guac_stream* stream = guac_user_alloc_stream(user);
+        stream->ack_handler = guac_common_ssh_sftp_ack_handler;
+        stream->data = file;
+
+        /* Associate new stream with get request */
+        guac_protocol_send_body(user->socket, object, stream,
+                "application/octet-stream", name);
+
+    }
+
+    guac_socket_flush(user->socket);
+    return 0;
+}
+
+/**
+ * Handler for put messages. In context of SFTP and the filesystem exposed via
+ * the Guacamole protocol, put messages request write access to a file within
+ * the filesystem.
+ *
+ * @param user
+ *     The user who sent the put message.
+ *
+ * @param object
+ *     The Guacamole protocol object associated with the put request itself.
+ *
+ * @param stream
+ *     The Guacamole protocol stream along which the user will be sending
+ *     file data.
+ *
+ * @param mimetype
+ *     The mimetype of the data being send along the stream.
+ *
+ * @param name
+ *     The name of the input stream (file) being requested.
+ *
+ * @return
+ *     Zero on success, non-zero on error.
+ */
+static int guac_common_ssh_sftp_put_handler(guac_user* user,
+        guac_object* object, guac_stream* stream, char* mimetype, char* name) {
+
+    guac_common_ssh_sftp_filesystem* filesystem =
+        (guac_common_ssh_sftp_filesystem*) object->data;
+
+    LIBSSH2_SFTP* sftp = filesystem->sftp_session;
+
+    /* Open file via SFTP */
+    LIBSSH2_SFTP_HANDLE* file = libssh2_sftp_open(sftp, name,
+            LIBSSH2_FXF_WRITE | LIBSSH2_FXF_CREAT | LIBSSH2_FXF_TRUNC,
+            S_IRUSR | S_IWUSR);
+
+    /* Acknowledge stream if successful */
+    if (file != NULL) {
+        guac_user_log(user, GUAC_LOG_DEBUG, "File \"%s\" opened", name);
+        guac_protocol_send_ack(user->socket, stream, "SFTP: File opened",
+                GUAC_PROTOCOL_STATUS_SUCCESS);
+    }
+
+    /* Abort on failure */
+    else {
+        guac_user_log(user, GUAC_LOG_INFO,
+                "Unable to open file \"%s\"", name);
+        guac_protocol_send_ack(user->socket, stream, "SFTP: Open failed",
+                guac_sftp_get_status(filesystem));
+    }
+
+    /* Set handlers for file stream */
+    stream->blob_handler = guac_common_ssh_sftp_blob_handler;
+    stream->end_handler = guac_common_ssh_sftp_end_handler;
+
+    /* Store file within stream */
+    stream->data = file;
+
+    guac_socket_flush(user->socket);
+    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) {
+
+    /* Init filesystem */
+    guac_object* fs_object = guac_user_alloc_object(user);
+    fs_object->get_handler = guac_common_ssh_sftp_get_handler;
+    fs_object->put_handler = guac_common_ssh_sftp_put_handler;
+    fs_object->data = filesystem;
+
+    /* Send filesystem to user */
+    guac_protocol_send_filesystem(user->socket, fs_object, filesystem->name);
+    guac_socket_flush(user->socket);
+
+    return fs_object;
+
+}
+
+guac_common_ssh_sftp_filesystem* guac_common_ssh_create_sftp_filesystem(
+        guac_common_ssh_session* session, const char* name) {
+
+    /* Request SFTP */
+    LIBSSH2_SFTP* sftp_session = libssh2_sftp_init(session->session);
+    if (sftp_session == NULL)
+        return NULL;
+
+    /* Allocate data for SFTP session */
+    guac_common_ssh_sftp_filesystem* 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;
+
+    /* Initially upload files to current directory */
+    strcpy(filesystem->upload_path, ".");
+
+    /* Return allocated filesystem */
+    return filesystem;
+
+}
+
+void guac_common_ssh_destroy_sftp_filesystem(
+        guac_common_ssh_sftp_filesystem* filesystem) {
+
+    /* Shutdown SFTP session */
+    libssh2_sftp_shutdown(filesystem->sftp_session);
+
+    /* Free associated memory */
+    free(filesystem->name);
+    free(filesystem);
+
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/ssh.c
----------------------------------------------------------------------
diff --git a/src/common-ssh/ssh.c b/src/common-ssh/ssh.c
new file mode 100644
index 0000000..9d3de19
--- /dev/null
+++ b/src/common-ssh/ssh.c
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "common-ssh/key.h"
+#include "common-ssh/ssh.h"
+#include "common-ssh/user.h"
+
+#include <guacamole/client.h>
+#include <libssh2.h>
+
+#ifdef LIBSSH2_USES_GCRYPT
+#include <gcrypt.h>
+#endif
+
+#include <openssl/err.h>
+#include <openssl/ssl.h>
+
+#include <errno.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <pthread.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#ifdef LIBSSH2_USES_GCRYPT
+GCRY_THREAD_OPTION_PTHREAD_IMPL;
+#endif
+
+/**
+ * Array of mutexes, used by OpenSSL.
+ */
+static pthread_mutex_t* guac_common_ssh_openssl_locks = NULL;
+
+/**
+ * Called by OpenSSL when locking or unlocking the Nth mutex.
+ *
+ * @param mode
+ *     A bitmask denoting the action to be taken on the Nth lock, such as
+ *     CRYPTO_LOCK or CRYPTO_UNLOCK.
+ *
+ * @param n
+ *     The index of the lock to lock or unlock.
+ *
+ * @param file
+ *     The filename of the function setting the lock, for debugging purposes.
+ *
+ * @param line
+ *     The line number of the function setting the lock, for debugging
+ *     purposes.
+ */
+static void guac_common_ssh_openssl_locking_callback(int mode, int n,
+        const char* file, int line){
+
+    /* Lock given mutex upon request */
+    if (mode & CRYPTO_LOCK)
+        pthread_mutex_lock(&(guac_common_ssh_openssl_locks[n]));
+
+    /* Unlock given mutex upon request */
+    else if (mode & CRYPTO_UNLOCK)
+        pthread_mutex_unlock(&(guac_common_ssh_openssl_locks[n]));
+
+}
+
+/**
+ * Called by OpenSSL when determining the current thread ID.
+ *
+ * @return
+ *     An ID which uniquely identifies the current thread.
+ */
+static unsigned long guac_common_ssh_openssl_id_callback() {
+    return (unsigned long) pthread_self();
+}
+
+/**
+ * Creates the given number of mutexes, such that OpenSSL will have at least
+ * this number of mutexes at its disposal.
+ *
+ * @param count
+ *     The number of mutexes (locks) to create.
+ */
+static void guac_common_ssh_openssl_init_locks(int count) {
+
+    int i;
+
+    /* Allocate required number of locks */
+    guac_common_ssh_openssl_locks =
+        malloc(sizeof(pthread_mutex_t) * count);
+
+    /* Initialize each lock */
+    for (i=0; i < count; i++)
+        pthread_mutex_init(&(guac_common_ssh_openssl_locks[i]), NULL);
+
+}
+
+/**
+ * Frees the given number of mutexes.
+ *
+ * @param count
+ *     The number of mutexes (locks) to free.
+ */
+static void guac_common_ssh_openssl_free_locks(int count) {
+
+    int i;
+
+    /* SSL lock array was not initialized */
+    if (guac_common_ssh_openssl_locks == NULL)
+        return;
+
+    /* Free all locks */
+    for (i=0; i < count; i++)
+        pthread_mutex_destroy(&(guac_common_ssh_openssl_locks[i]));
+
+    /* Free lock array */
+    free(guac_common_ssh_openssl_locks);
+
+}
+
+int guac_common_ssh_init(guac_client* client) {
+
+#ifdef LIBSSH2_USES_GCRYPT
+    /* Init threadsafety in libgcrypt */
+    gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
+    if (!gcry_check_version(GCRYPT_VERSION)) {
+        guac_client_log(client, GUAC_LOG_ERROR, "libgcrypt version mismatch.");
+        return 1;
+    }
+#endif
+
+    /* Init threadsafety in OpenSSL */
+    guac_common_ssh_openssl_init_locks(CRYPTO_num_locks());
+    CRYPTO_set_id_callback(guac_common_ssh_openssl_id_callback);
+    CRYPTO_set_locking_callback(guac_common_ssh_openssl_locking_callback);
+
+    /* Init OpenSSL */
+    SSL_library_init();
+    ERR_load_crypto_strings();
+
+    /* Init libssh2 */
+    libssh2_init(0);
+
+    /* Success */
+    return 0;
+
+}
+
+void guac_common_ssh_uninit() {
+    guac_common_ssh_openssl_free_locks(CRYPTO_num_locks());
+}
+
+/**
+ * Callback invoked by libssh2 when libssh2_userauth_publickkey() is invoked.
+ * This callback must sign the given data, returning the signature as newly-
+ * allocated buffer space.
+ *
+ * @param session
+ *     The SSH session for which the signature is being generated.
+ *
+ * @param sig
+ *     A pointer to the buffer space containing the signature. This callback
+ *     MUST allocate and assign this space.
+ *
+ * @param sig_len
+ *     The length of the signature within the allocated buffer space, in bytes.
+ *     This value must be set to the size of the signature after the signing
+ *     operation completes.
+ *
+ * @param data
+ *     The arbitrary data that must be signed.
+ *
+ * @param data_len
+ *     The length of the arbitrary data to be signed, in bytes.
+ *
+ * @param abstract
+ *     The value of the abstract parameter provided with the corresponding call
+ *     to libssh2_userauth_publickey().
+ *
+ * @return
+ *     Zero on success, non-zero if the signing operation failed.
+ */
+static int guac_common_ssh_sign_callback(LIBSSH2_SESSION* session,
+        unsigned char** sig, size_t* sig_len,
+        const unsigned char* data, size_t data_len, void **abstract) {
+
+    guac_common_ssh_key* key = (guac_common_ssh_key*) abstract;
+    int length;
+
+    /* Allocate space for signature */
+    *sig = malloc(4096);
+
+    /* Sign with key */
+    length = guac_common_ssh_key_sign(key, (const char*) data, data_len, *sig);
+    if (length < 0)
+        return 1;
+
+    *sig_len = length;
+    return 0;
+}
+
+/**
+ * Callback for the keyboard-interactive authentication method. Currently
+ * supports just one prompt for the password. This callback is invoked as
+ * needed to fullfill a call to libssh2_userauth_keyboard_interactive().
+ *
+ * @param name
+ *     An arbitrary name which should be printed to the terminal for the
+ *     benefit of the user. This is currently ignored.
+ *
+ * @param name_len
+ *     The length of the name string, in bytes.
+ *
+ * @param instruction
+ *     Arbitrary instructions which should be printed to the terminal for the
+ *     benefit of the user. This is currently ignored.
+ *
+ * @param instruction_len
+ *     The length of the instruction string, in bytes.
+ *
+ * @param num_prompts
+ *     The number of keyboard-interactive prompts for which responses are
+ *     requested. This callback currently only supports one prompt, and assumes
+ *     that this prompt is requesting the password.
+ *
+ * @param prompts
+ *     An array of all keyboard-interactive prompts for which responses are
+ *     requested.
+ *
+ * @param responses
+ *     A parallel array into which all prompt responses should be stored. Each
+ *     entry within this array corresponds to the entry in the prompts array
+ *     with the same index.
+ *
+ * @param abstract
+ *     The value of the abstract parameter provided when the SSH session was
+ *     created with libssh2_session_init_ex().
+ */
+static void guac_common_ssh_kbd_callback(const char *name, int name_len,
+        const char *instruction, int instruction_len, int num_prompts,
+        const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
+        LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses,
+        void **abstract) {
+
+    guac_common_ssh_session* common_session =
+        (guac_common_ssh_session*) *abstract;
+
+    guac_client* client = common_session->client;
+
+    /* Send password if only one prompt */
+    if (num_prompts == 1) {
+        char* password = common_session->user->password;
+        responses[0].text = strdup(password);
+        responses[0].length = strlen(password);
+    }
+
+    /* If more than one prompt, a single password is not enough */
+    else
+        guac_client_log(client, GUAC_LOG_WARNING,
+                "Unsupported number of keyboard-interactive prompts: %i",
+                num_prompts);
+
+}
+
+/**
+ * Authenticates the user associated with the given session over SSH. All
+ * required credentials must already be present within the user object
+ * associated with the given session.
+ *
+ * @param session
+ *     The session associated with the user to be authenticated.
+ *
+ * @return
+ *     Zero if authentication succeeds, or non-zero if authentication has
+ *     failed.
+ */
+static int guac_common_ssh_authenticate(guac_common_ssh_session* common_session) {
+
+    guac_client* client = common_session->client;
+    guac_common_ssh_user* user = common_session->user;
+    LIBSSH2_SESSION* session = common_session->session;
+
+    /* Get user credentials */
+    char* username = user->username;
+    char* password = user->password;
+    guac_common_ssh_key* key = user->private_key;
+
+    /* Validate username provided */
+    if (username == NULL) {
+        guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
+                "SSH authentication requires a username.");
+        return 1;
+    }
+
+    /* Get list of supported authentication methods */
+    char* user_authlist = libssh2_userauth_list(session, username,
+            strlen(username));
+    guac_client_log(client, GUAC_LOG_DEBUG,
+            "Supported authentication methods: %s", user_authlist);
+
+    /* Authenticate with private key, if provided */
+    if (key != NULL) {
+
+        /* Check if public key auth is supported on the server */
+        if (strstr(user_authlist, "publickey") == NULL) {
+            guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
+                    "Public key authentication is not supported by "
+                    "the SSH server");
+            return 1;
+        }
+
+        /* Attempt public key auth */
+        if (libssh2_userauth_publickey(session, username,
+                    (unsigned char*) key->public_key, key->public_key_length,
+                    guac_common_ssh_sign_callback, (void**) key)) {
+
+            /* Abort on failure */
+            char* error_message;
+            libssh2_session_last_error(session, &error_message, NULL, 0);
+            guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
+                    "Public key authentication failed: %s", error_message);
+
+            return 1;
+
+        }
+
+        /* Private key authentication succeeded */
+        return 0;
+
+    }
+
+    /* Authenticate with password, if provided */
+    else if (password != NULL) {
+
+        /* Check if password auth is supported on the server */
+        if (strstr(user_authlist, "password") != NULL) {
+
+            /* Attempt password authentication */
+            if (libssh2_userauth_password(session, username, password)) {
+
+                /* Abort on failure */
+                char* error_message;
+                libssh2_session_last_error(session, &error_message, NULL, 0);
+                guac_client_abort(client,
+                        GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
+                        "Password authentication failed: %s", error_message);
+
+                return 1;
+            }
+
+            /* Password authentication succeeded */
+            return 0;
+
+        }
+
+        /* Check if keyboard-interactive auth is supported on the server */
+        if (strstr(user_authlist, "keyboard-interactive") != NULL) {
+
+            /* Attempt keyboard-interactive auth using provided password */
+            if (libssh2_userauth_keyboard_interactive(session, username,
+                        &guac_common_ssh_kbd_callback)) {
+
+                /* Abort on failure */
+                char* error_message;
+                libssh2_session_last_error(session, &error_message, NULL, 0);
+                guac_client_abort(client,
+                        GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
+                        "Keyboard-interactive authentication failed: %s",
+                        error_message);
+
+                return 1;
+            }
+
+            /* Keyboard-interactive authentication succeeded */
+            return 0;
+
+        }
+
+        /* No known authentication types available */
+        guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
+                "Password and keyboard-interactive authentication are not "
+                "supported by the SSH server");
+        return 1;
+
+    }
+
+    /* No credentials provided */
+    guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
+            "SSH authentication requires either a private key or a password.");
+    return 1;
+
+}
+
+guac_common_ssh_session* guac_common_ssh_create_session(guac_client* client,
+        const char* hostname, const char* port, guac_common_ssh_user* user) {
+
+    int retval;
+
+    int fd;
+    struct addrinfo* addresses;
+    struct addrinfo* current_address;
+
+    char connected_address[1024];
+    char connected_port[64];
+
+    struct addrinfo hints = {
+        .ai_family   = AF_UNSPEC,
+        .ai_socktype = SOCK_STREAM,
+        .ai_protocol = IPPROTO_TCP
+    };
+
+    /* Get socket */
+    fd = socket(AF_INET, SOCK_STREAM, 0);
+    if (fd < 0) {
+        guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
+                "Unable to create socket: %s", strerror(errno));
+        return NULL;
+    }
+
+    /* Get addresses connection */
+    if ((retval = getaddrinfo(hostname, port, &hints, &addresses))) {
+        guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
+                "Error parsing given address or port: %s",
+                gai_strerror(retval));
+        close(fd);
+        return NULL;
+    }
+
+    /* Attempt connection to each address until success */
+    current_address = addresses;
+    while (current_address != NULL) {
+
+        /* Resolve hostname */
+        if ((retval = getnameinfo(current_address->ai_addr,
+                current_address->ai_addrlen,
+                connected_address, sizeof(connected_address),
+                connected_port, sizeof(connected_port),
+                NI_NUMERICHOST | NI_NUMERICSERV)))
+            guac_client_log(client, GUAC_LOG_DEBUG,
+                    "Unable to resolve host: %s", gai_strerror(retval));
+
+        /* Connect */
+        if (connect(fd, current_address->ai_addr,
+                        current_address->ai_addrlen) == 0) {
+
+            guac_client_log(client, GUAC_LOG_DEBUG,
+                    "Successfully connected to host %s, port %s",
+                    connected_address, connected_port);
+
+            /* Done if successful connect */
+            break;
+
+        }
+
+        /* Otherwise log information regarding bind failure */
+        else
+            guac_client_log(client, GUAC_LOG_DEBUG, "Unable to connect to "
+                    "host %s, port %s: %s",
+                    connected_address, connected_port, strerror(errno));
+
+        current_address = current_address->ai_next;
+
+    }
+
+    /* Free addrinfo */
+    freeaddrinfo(addresses);
+
+    /* If unable to connect to anything, fail */
+    if (current_address == NULL) {
+        guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR,
+                "Unable to connect to any addresses.");
+        close(fd);
+        return NULL;
+    }
+
+    /* Allocate new session */
+    guac_common_ssh_session* common_session =
+        malloc(sizeof(guac_common_ssh_session));
+
+    /* Open SSH session */
+    LIBSSH2_SESSION* session = libssh2_session_init_ex(NULL, NULL,
+            NULL, common_session);
+    if (session == NULL) {
+        guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
+                "Session allocation failed.");
+        free(common_session);
+        close(fd);
+        return NULL;
+    }
+
+    /* Perform handshake */
+    if (libssh2_session_handshake(session, fd)) {
+        guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR,
+                "SSH handshake failed.");
+        free(common_session);
+        close(fd);
+        return NULL;
+    }
+
+    /* Store basic session data */
+    common_session->client = client;
+    common_session->user = user;
+    common_session->session = session;
+    common_session->fd = fd;
+
+    /* Attempt authentication */
+    if (guac_common_ssh_authenticate(common_session)) {
+        free(common_session);
+        close(fd);
+        return NULL;
+    }
+
+    /* Return created session */
+    return common_session;
+
+}
+
+void guac_common_ssh_destroy_session(guac_common_ssh_session* session) {
+
+    /* Disconnect and clean up libssh2 */
+    libssh2_session_disconnect(session->session, "Bye");
+    libssh2_session_free(session->session);
+
+    /* Free all other data */
+    free(session);
+
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/user.c
----------------------------------------------------------------------
diff --git a/src/common-ssh/user.c b/src/common-ssh/user.c
new file mode 100644
index 0000000..92c8d96
--- /dev/null
+++ b/src/common-ssh/user.c
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "common-ssh/key.h"
+#include "common-ssh/user.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+guac_common_ssh_user* guac_common_ssh_create_user(const char* username) {
+
+    guac_common_ssh_user* user = malloc(sizeof(guac_common_ssh_user));
+
+    /* Init user */
+    user->username = strdup(username);
+    user->password = NULL;
+    user->private_key = NULL;
+
+    return user;
+
+}
+
+void guac_common_ssh_destroy_user(guac_common_ssh_user* user) {
+
+    /* Free private key, if present */
+    if (user->private_key != NULL)
+        guac_common_ssh_key_free(user->private_key);
+
+    /* Free all other data */
+    free(user->password);
+    free(user->username);
+    free(user);
+
+}
+
+void guac_common_ssh_user_set_password(guac_common_ssh_user* user,
+        const char* password) {
+
+    /* Replace current password with given value */
+    free(user->password);
+    user->password = strdup(password);
+
+}
+
+int guac_common_ssh_user_import_key(guac_common_ssh_user* user,
+        char* private_key, char* passphrase) {
+
+    /* Free existing private key, if present */
+    if (user->private_key != NULL)
+        guac_common_ssh_key_free(user->private_key);
+
+    /* Attempt to read key without passphrase if none given */
+    if (passphrase == NULL)
+        user->private_key = guac_common_ssh_key_alloc(private_key,
+                strlen(private_key), "");
+
+    /* Otherwise, use provided passphrase */
+    else
+        user->private_key = guac_common_ssh_key_alloc(private_key,
+                strlen(private_key), passphrase);
+
+    /* Fail if key could not be read */
+    return user->private_key == NULL;
+
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/protocols/rdp/client.c
----------------------------------------------------------------------
diff --git a/src/protocols/rdp/client.c b/src/protocols/rdp/client.c
index 2dcb401..d12efba 100644
--- a/src/protocols/rdp/client.c
+++ b/src/protocols/rdp/client.c
@@ -27,9 +27,9 @@
 #include "user.h"
 
 #ifdef ENABLE_COMMON_SSH
-#include <guac_sftp.h>
-#include <guac_ssh.h>
-#include <guac_ssh_user.h>
+#include "common-ssh/sftp.h"
+#include "common-ssh/ssh.h"
+#include "common-ssh/user.h"
 #endif
 
 #include <freerdp/cache/cache.h>

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/protocols/rdp/rdp.c
----------------------------------------------------------------------
diff --git a/src/protocols/rdp/rdp.c b/src/protocols/rdp/rdp.c
index a48230a..8d49c83 100644
--- a/src/protocols/rdp/rdp.c
+++ b/src/protocols/rdp/rdp.c
@@ -40,9 +40,9 @@
 #include "rdp_svc.h"
 
 #ifdef ENABLE_COMMON_SSH
-#include <guac_sftp.h>
-#include <guac_ssh.h>
-#include <guac_ssh_user.h>
+#include "common-ssh/sftp.h"
+#include "common-ssh/ssh.h"
+#include "common-ssh/user.h"
 #endif
 
 #include <freerdp/cache/bitmap.h>

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/protocols/rdp/rdp.h
----------------------------------------------------------------------
diff --git a/src/protocols/rdp/rdp.h b/src/protocols/rdp/rdp.h
index e3e3311..943155d 100644
--- a/src/protocols/rdp/rdp.h
+++ b/src/protocols/rdp/rdp.h
@@ -39,9 +39,9 @@
 #include <guacamole/client.h>
 
 #ifdef ENABLE_COMMON_SSH
-#include "guac_sftp.h"
-#include "guac_ssh.h"
-#include "guac_ssh_user.h"
+#include "common-ssh/sftp.h"
+#include "common-ssh/ssh.h"
+#include "common-ssh/user.h"
 #endif
 
 #include <pthread.h>

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/protocols/rdp/sftp.c
----------------------------------------------------------------------
diff --git a/src/protocols/rdp/sftp.c b/src/protocols/rdp/sftp.c
index efda47d..ecfe35f 100644
--- a/src/protocols/rdp/sftp.c
+++ b/src/protocols/rdp/sftp.c
@@ -19,7 +19,7 @@
 
 #include "config.h"
 
-#include "guac_sftp.h"
+#include "common-ssh/sftp.h"
 #include "rdp.h"
 #include "sftp.h"
 

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/protocols/ssh/client.c
----------------------------------------------------------------------
diff --git a/src/protocols/ssh/client.c b/src/protocols/ssh/client.c
index 13dd1c8..2110ba9 100644
--- a/src/protocols/ssh/client.c
+++ b/src/protocols/ssh/client.c
@@ -20,7 +20,7 @@
 #include "config.h"
 
 #include "client.h"
-#include "guac_sftp.h"
+#include "common-ssh/sftp.h"
 #include "ssh.h"
 #include "terminal.h"
 #include "user.h"

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/protocols/ssh/sftp.c
----------------------------------------------------------------------
diff --git a/src/protocols/ssh/sftp.c b/src/protocols/ssh/sftp.c
index 9fddc30..f4173a6 100644
--- a/src/protocols/ssh/sftp.c
+++ b/src/protocols/ssh/sftp.c
@@ -19,7 +19,7 @@
 
 #include "config.h"
 
-#include "guac_sftp.h"
+#include "common-ssh/sftp.h"
 #include "sftp.h"
 #include "ssh.h"
 

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/protocols/ssh/ssh.c
----------------------------------------------------------------------
diff --git a/src/protocols/ssh/ssh.c b/src/protocols/ssh/ssh.c
index 53835c8..dd0a30f 100644
--- a/src/protocols/ssh/ssh.c
+++ b/src/protocols/ssh/ssh.c
@@ -20,8 +20,8 @@
 #include "config.h"
 
 #include "common/recording.h"
-#include "guac_sftp.h"
-#include "guac_ssh.h"
+#include "common-ssh/sftp.h"
+#include "common-ssh/ssh.h"
 #include "settings.h"
 #include "sftp.h"
 #include "ssh.h"

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/protocols/ssh/ssh.h
----------------------------------------------------------------------
diff --git a/src/protocols/ssh/ssh.h b/src/protocols/ssh/ssh.h
index 44027ee..d6dbdaf 100644
--- a/src/protocols/ssh/ssh.h
+++ b/src/protocols/ssh/ssh.h
@@ -22,9 +22,9 @@
 
 #include "config.h"
 
-#include "guac_sftp.h"
-#include "guac_ssh.h"
-#include "guac_ssh_user.h"
+#include "common-ssh/sftp.h"
+#include "common-ssh/ssh.h"
+#include "common-ssh/user.h"
 #include "settings.h"
 #include "terminal.h"
 

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/protocols/vnc/client.c
----------------------------------------------------------------------
diff --git a/src/protocols/vnc/client.c b/src/protocols/vnc/client.c
index 516d93f..00b9015 100644
--- a/src/protocols/vnc/client.c
+++ b/src/protocols/vnc/client.c
@@ -24,9 +24,9 @@
 #include "vnc.h"
 
 #ifdef ENABLE_COMMON_SSH
-#include "guac_sftp.h"
-#include "guac_ssh.h"
-#include "sftp.h"
+#include "common-ssh/sftp.h"
+#include "common-ssh/ssh.h"
+#include "common-ssh/user.h"
 #endif
 
 #ifdef ENABLE_PULSE

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/protocols/vnc/sftp.c
----------------------------------------------------------------------
diff --git a/src/protocols/vnc/sftp.c b/src/protocols/vnc/sftp.c
index fc90668..e5e768a 100644
--- a/src/protocols/vnc/sftp.c
+++ b/src/protocols/vnc/sftp.c
@@ -19,7 +19,7 @@
 
 #include "config.h"
 
-#include "guac_sftp.h"
+#include "common-ssh/sftp.h"
 #include "sftp.h"
 #include "vnc.h"
 

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/protocols/vnc/vnc.c
----------------------------------------------------------------------
diff --git a/src/protocols/vnc/vnc.c b/src/protocols/vnc/vnc.c
index 9b0bd76..710372f 100644
--- a/src/protocols/vnc/vnc.c
+++ b/src/protocols/vnc/vnc.c
@@ -37,8 +37,8 @@
 #endif
 
 #ifdef ENABLE_COMMON_SSH
-#include "guac_sftp.h"
-#include "guac_ssh.h"
+#include "common-ssh/sftp.h"
+#include "common-ssh/ssh.h"
 #include "sftp.h"
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/protocols/vnc/vnc.h
----------------------------------------------------------------------
diff --git a/src/protocols/vnc/vnc.h b/src/protocols/vnc/vnc.h
index 7ae8b72..0edbcd4 100644
--- a/src/protocols/vnc/vnc.h
+++ b/src/protocols/vnc/vnc.h
@@ -37,9 +37,9 @@
 #endif
 
 #ifdef ENABLE_COMMON_SSH
-#include "guac_sftp.h"
-#include "guac_ssh.h"
-#include "guac_ssh_user.h"
+#include "common-ssh/sftp.h"
+#include "common-ssh/ssh.h"
+#include "common-ssh/user.h"
 #endif
 
 #include <pthread.h>


[7/9] incubator-guacamole-server git commit: GUACAMOLE-225: Merge updated version numbers.

Posted by jm...@apache.org.
GUACAMOLE-225: Merge updated version numbers.


Project: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/commit/08ce4331
Tree: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/tree/08ce4331
Diff: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/diff/08ce4331

Branch: refs/heads/master
Commit: 08ce4331bb15d5a42fb00c6fee0ee09f8577f517
Parents: a808a6b f85978e
Author: James Muehlner <ja...@guac-dev.org>
Authored: Tue Feb 28 10:26:38 2017 -0800
Committer: James Muehlner <ja...@guac-dev.org>
Committed: Tue Feb 28 10:26:38 2017 -0800

----------------------------------------------------------------------
 configure.ac            | 2 +-
 doc/Doxyfile            | 2 +-
 src/libguac/Makefile.am | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
----------------------------------------------------------------------



[3/9] incubator-guacamole-server git commit: GUACAMOLE-169: Move terminal headers to namespaced directory.

Posted by jm...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/terminal/scrollbar.h
----------------------------------------------------------------------
diff --git a/src/terminal/terminal/scrollbar.h b/src/terminal/terminal/scrollbar.h
new file mode 100644
index 0000000..bf0da87
--- /dev/null
+++ b/src/terminal/terminal/scrollbar.h
@@ -0,0 +1,357 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef GUAC_TERMINAL_SCROLLBAR_H
+#define GUAC_TERMINAL_SCROLLBAR_H
+
+#include "config.h"
+
+#include <guacamole/client.h>
+#include <guacamole/layer.h>
+
+/**
+ * The width of the scrollbar, in pixels.
+ */
+#define GUAC_TERMINAL_SCROLLBAR_WIDTH 16 
+
+/**
+ * The number of pixels between the draggable handle of the scrollbar and the
+ * boundary of the containing layer.
+ */
+#define GUAC_TERMINAL_SCROLLBAR_PADDING 2
+
+/**
+ * The minimum height of the draggable handle of the scrollbar, in pixels.
+ */
+#define GUAC_TERMINAL_SCROLLBAR_MIN_HEIGHT 64
+
+/**
+ * The state of all scrollbar components, describing all variable aspects of
+ * the scrollbar's appearance.
+ */
+typedef struct guac_terminal_scrollbar_render_state {
+
+    /**
+     * The current X-coordinate of the upper-left corner of the scrollbar's
+     * handle. This value will be relative to the scrollbar's containing layer.
+     */
+    int handle_x;
+
+    /**
+     * The current Y-coordinate of the upper-left corner of the scrollbar's
+     * handle. This value will be relative to the scrollbar's containing layer.
+     */
+    int handle_y;
+
+    /**
+     * The width of the scrollbar's handle.
+     */
+    int handle_width;
+
+    /**
+     * The height of the scrollbar's handle.
+     */
+    int handle_height;
+
+    /**
+     * The current X-coordinate of the upper-left corner of the scrollbar's
+     * containing layer.
+     */
+    int container_x;
+
+    /**
+     * The current Y-coordinate of the upper-left corner of the scrollbar's
+     * containing layer.
+     */
+    int container_y;
+
+    /**
+     * The width of the scrollbar's containing layer.
+     */
+    int container_width;
+
+    /**
+     * The height of the scrollbar's containing layer.
+     */
+    int container_height;
+
+} guac_terminal_scrollbar_render_state;
+
+typedef struct guac_terminal_scrollbar guac_terminal_scrollbar;
+
+/**
+ * Handler which is called whenever the scrollbar value changes outside a call
+ * to guac_terminal_scrollbar_set_value().
+ */
+typedef void guac_terminal_scrollbar_scroll_handler(
+        guac_terminal_scrollbar* scrollbar, int value);
+
+/**
+ * A scrollbar, made up of a containing layer and inner draggable handle. The
+ * position of the handle within the layer represents the value of the
+ * scrollbar.
+ */
+struct guac_terminal_scrollbar {
+
+    /**
+     * The client associated with this scrollbar.
+     */
+    guac_client* client;
+
+    /**
+     * The layer containing the scrollbar.
+     */
+    const guac_layer* parent;
+
+    /**
+     * The width of the parent layer, in pixels.
+     */
+    int parent_width;
+
+    /**
+     * The height of the parent layer, in pixels.
+     */
+    int parent_height;
+
+    /**
+     * The scrollbar itself.
+     */
+    guac_layer* container;
+
+    /**
+     * The draggable handle within the scrollbar, representing the current
+     * scroll value.
+     */
+    guac_layer* handle;
+
+    /**
+     * The minimum scroll value.
+     */
+    int min;
+
+    /**
+     * The maximum scroll value.
+     */
+    int max;
+
+    /**
+     * The size of the visible area, in the same units as min and max.
+     */
+    int visible_area;
+
+    /**
+     * The current scroll value.
+     */
+    int value;
+
+    /**
+     * The current state of all variable, visible parts of the scrollbar.
+     */
+    guac_terminal_scrollbar_render_state render_state;
+
+    /**
+     * Whether the scrollbar handle is currently being dragged.
+     */
+    int dragging_handle;
+
+    /**
+     * The offset of the Y location of the mouse pointer when the dragging
+     * began, relative to the top of the scrollbar handle. If dragging is not
+     * in progress, this value is undefined.
+     */
+    int drag_offset_y;
+
+    /**
+     * The current Y location of the mouse pointer if dragging is in progress.
+     * If dragging is not in progress, this value is undefined.
+     */
+    int drag_current_y;
+
+    /**
+     * The function to call when the scrollbar handle is being dragged, and
+     * the new scrollbar value needs to be handled and assigned.
+     */
+    guac_terminal_scrollbar_scroll_handler* scroll_handler;
+
+    /**
+     * Arbitrary reference to data related to this scrollbar.
+     */
+    void* data;
+
+};
+
+/**
+ * Allocates a new scrollbar, associating that scrollbar with the given client
+ * and parent layer. The dimensions of the parent layer dictate the initial
+ * position of the scrollbar. Currently, the scrollbar is always anchored to
+ * the right edge of the parent layer.
+ *
+ * This will cause instructions to be written to the client's socket, but the
+ * client's socket will not be automatically flushed.
+ *
+ * @param client
+ *     The client to associate with the new scrollbar.
+ *
+ * @param parent
+ *     The layer which will contain the newly-allocated scrollbar.
+ *
+ * @param parent_width
+ *     The width of the parent layer, in pixels.
+ *
+ * @param parent_height
+ *     The height of the parent layer, in pixels.
+ *
+ * @param visible_area
+ *     The amount of scrollable data that can be shown within the parent layer
+ *     at any given time. This value uses the same units as min, max, and the
+ *     current scroll value.
+ *
+ * @return
+ *     A newly allocated scrollbar.
+ */
+guac_terminal_scrollbar* guac_terminal_scrollbar_alloc(guac_client* client,
+        const guac_layer* parent, int parent_width, int parent_height,
+        int visible_area);
+
+/**
+ * Frees the given scrollbar.
+ *
+ * @param scrollbar
+ *     The scrollbar to free.
+ */
+void guac_terminal_scrollbar_free(guac_terminal_scrollbar* scrollbar);
+
+/**
+ * Flushes the render state of the given scrollbar, updating the remote display
+ * accordingly.
+ *
+ * This may cause instructions to be written to the client's socket, but the
+ * client's socket will not be automatically flushed.
+ *
+ * @param scrollbar
+ *     The scrollbar whose render state is to be flushed.
+ */
+void guac_terminal_scrollbar_flush(guac_terminal_scrollbar* scrollbar);
+
+/**
+ * Forces a complete redraw / resync of scrollbar state for the given user that
+ * has just joined the connection, sending the necessary instructions to
+ * completely recreate and redraw the scrollbar rendering over the given
+ * socket.
+ *
+ * @param scrollbar
+ *     The scrollbar to sync to the given user.
+ *
+ * @param user
+ *     The user that has just joined the connection.
+ *
+ * @param socket
+ *     The socket over which any necessary instructions should be sent.
+ */
+void guac_terminal_scrollbar_dup(guac_terminal_scrollbar* scrollbar,
+        guac_user* user, guac_socket* socket);
+
+/**
+ * Sets the minimum and maximum allowed scroll values of the given scrollbar
+ * to the given values. If necessary, the current value of the scrollbar will
+ * be adjusted to fit within the new bounds.
+ *
+ * This may cause instructions to be written to the client's socket, but the
+ * client's socket will not be automatically flushed.
+ *
+ * @param scrollbar
+ *     The scrollbar whose bounds are changing.
+ *
+ * @param min
+ *     The new minimum value of the scrollbar.
+ *
+ * @param max
+ *     The new maximum value of the scrollbar.
+ */
+void guac_terminal_scrollbar_set_bounds(guac_terminal_scrollbar* scrollbar,
+        int min, int max);
+
+/**
+ * Sets the current value of the given scrollbar. If the value specified does
+ * not fall within the scrollbar's defined minimum and maximum values, the
+ * value will be adjusted to fit.
+ *
+ * This may cause instructions to be written to the client's socket, but the
+ * client's socket will not be automatically flushed.
+ *
+ * @param scrollbar
+ *     The scrollbar whose value is changing.
+ *
+ * @param value
+ *     The value to assign to the scrollbar. If the value if out of bounds, it
+ *     will be automatically adjusted to fit.
+ */
+void guac_terminal_scrollbar_set_value(guac_terminal_scrollbar* scrollbar,
+        int value);
+
+/**
+ * Notifies the scrollbar that the parent layer has been resized, and that the
+ * scrollbar may need to be repositioned or resized accordingly.
+ *
+ * This may cause instructions to be written to the client's socket, but the
+ * client's socket will not be automatically flushed.
+ *
+ * @param scrollbar
+ *     The scrollbar whose parent layer has been resized.
+ *
+ * @param parent_width
+ *     The new width of the parent layer, in pixels.
+ *
+ * @param parent_height
+ *     The new height of the parent layer, in pixels.
+ *
+ * @param visible_area
+ *     The amount of scrollable data that can be shown within the parent layer
+ *     at any given time. This value uses the same units as min, max, and the
+ *     current scroll value.
+ */
+void guac_terminal_scrollbar_parent_resized(guac_terminal_scrollbar* scrollbar,
+        int parent_width, int parent_height, int visible_area);
+
+/**
+ * Notifies the scrollbar of the current mouse state, allowing it to update
+ * itself with respect to button state and dragging.
+ *
+ * @param scrollbar
+ *     The scrollbar to notify of the current mouse state.
+ *
+ * @param x
+ *     The X coordinate of the mouse pointer.
+ *
+ * @param y
+ *     The Y coordinate of the mouse pointer.
+ *
+ * @param mask
+ *     The button mask, where the Nth bit of the button mask represents the
+ *     pressed state of the Nth mouse button, where button 0 is the left
+ *     mouse button, button 1 is the middle button, etc.
+ *
+ * @return
+ *     Zero if the mouse event was not handled by the scrollbar, non-zero
+ *     otherwise.
+ */
+int guac_terminal_scrollbar_handle_mouse(guac_terminal_scrollbar* scrollbar,
+        int x, int y, int mask);
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/terminal/terminal.h
----------------------------------------------------------------------
diff --git a/src/terminal/terminal/terminal.h b/src/terminal/terminal/terminal.h
new file mode 100644
index 0000000..f37e870
--- /dev/null
+++ b/src/terminal/terminal/terminal.h
@@ -0,0 +1,839 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+#ifndef _GUAC_TERMINAL_H
+#define _GUAC_TERMINAL_H
+
+#include "config.h"
+
+#include "buffer.h"
+#include "common/clipboard.h"
+#include "common/cursor.h"
+#include "display.h"
+#include "scrollbar.h"
+#include "types.h"
+#include "typescript.h"
+
+#include <pthread.h>
+#include <stdbool.h>
+
+#include <guacamole/client.h>
+#include <guacamole/stream.h>
+
+/**
+ * The maximum duration of a single frame, in milliseconds.
+ */
+#define GUAC_TERMINAL_FRAME_DURATION 40
+
+/**
+ * The maximum amount of time to wait for more data before declaring a frame
+ * complete, in milliseconds.
+ */
+#define GUAC_TERMINAL_FRAME_TIMEOUT 10
+
+/**
+ * The maximum number of custom tab stops.
+ */
+#define GUAC_TERMINAL_MAX_TABS       16
+
+/**
+ * The number of rows to scroll per scroll wheel event.
+ */
+#define GUAC_TERMINAL_WHEEL_SCROLL_AMOUNT 3
+
+/**
+ * The maximum number of bytes to allow within the clipboard.
+ */
+#define GUAC_TERMINAL_CLIPBOARD_MAX_LENGTH 262144
+
+/**
+ * The name of the color scheme having black foreground and white background.
+ */
+#define GUAC_TERMINAL_SCHEME_BLACK_WHITE "black-white"
+
+/**
+ * The name of the color scheme having gray foreground and black background.
+ */
+#define GUAC_TERMINAL_SCHEME_GRAY_BLACK "gray-black"
+
+/**
+ * The name of the color scheme having green foreground and black background.
+ */
+#define GUAC_TERMINAL_SCHEME_GREEN_BLACK "green-black"
+
+/**
+ * The name of the color scheme having white foreground and black background.
+ */
+#define GUAC_TERMINAL_SCHEME_WHITE_BLACK "white-black"
+
+typedef struct guac_terminal guac_terminal;
+
+/**
+ * All possible mouse cursors used by the terminal emulator.
+ */
+typedef enum guac_terminal_cursor_type {
+
+    /**
+     * A transparent (blank) cursor.
+     */
+    GUAC_TERMINAL_CURSOR_BLANK,
+
+    /**
+     * A standard I-bar cursor for selecting text, etc.
+     */
+    GUAC_TERMINAL_CURSOR_IBAR,
+
+    /**
+     * A standard triangular mouse pointer for manipulating non-text objects.
+     */
+    GUAC_TERMINAL_CURSOR_POINTER
+
+} guac_terminal_cursor_type;
+
+/**
+ * Handler for characters printed to the terminal. When a character is printed,
+ * the current char handler for the terminal is called and given that
+ * character.
+ */
+typedef int guac_terminal_char_handler(guac_terminal* term, unsigned char c);
+
+/**
+ * Handler for setting the destination path for file uploads.
+ */
+typedef void guac_terminal_upload_path_handler(guac_client* client, char* path);
+
+/**
+ * Handler for creating an outbound file download stream for a specified file.
+ */
+typedef guac_stream* guac_terminal_file_download_handler(guac_client* client, char* filename);
+
+/**
+ * Represents a terminal emulator which uses a given Guacamole client to
+ * render itself.
+ */
+struct guac_terminal {
+
+    /**
+     * The Guacamole client associated with this terminal emulator.
+     */
+    guac_client* client;
+
+    /**
+     * The terminal render thread.
+     */
+    pthread_t thread;
+
+    /**
+     * Called whenever the necessary terminal codes are sent to change
+     * the path for future file uploads.
+     */
+    guac_terminal_upload_path_handler* upload_path_handler;
+
+    /**
+     * Called whenever the necessary terminal codes are sent to initiate
+     * a download of a given remote file.
+     */
+    guac_terminal_file_download_handler* file_download_handler;
+
+    /**
+     * Lock which restricts simultaneous access to this terminal via the root
+     * guac_terminal_* functions.
+     */
+    pthread_mutex_t lock;
+
+    /**
+     * The mutex associated with the modified condition and flag, locked
+     * whenever a thread is waiting on the modified condition, the modified
+     * condition is being signalled, or the modified flag is being changed.
+     */
+    pthread_mutex_t modified_lock;
+
+    /**
+     * Flag set whenever an operation has affected the terminal in a way that
+     * will require a frame flush. When this flag is set, the modified_cond
+     * condition will be signalled. The modified_lock will always be
+     * acquired before this flag is altered.
+     */
+    int modified;
+
+    /**
+     * Condition which is signalled when the modified flag has been set
+     */
+    pthread_cond_t modified_cond;
+
+    /**
+     * Pipe which will be the source of user input. When a terminal code
+     * generates synthesized user input, that data will be written to
+     * this pipe.
+     */
+    int stdin_pipe_fd[2];
+
+    /**
+     * The currently-open pipe stream to which all terminal output should be
+     * written, if any. If no pipe stream is open, terminal output will be
+     * written to the terminal display, and this value will be NULL.
+     */
+    guac_stream* pipe_stream;
+
+    /**
+     * Buffer of data pending write to the pipe_stream. Data within this buffer
+     * will be flushed to the pipe_stream when either (1) the buffer is full
+     * and another character needs to be written or (2) the pipe_stream is
+     * closed.
+     */
+    char pipe_buffer[6048];
+
+    /**
+     * The number of bytes currently stored within the pipe_buffer.
+     */
+    int pipe_buffer_length;
+
+    /**
+     * The currently-active typescript recording all terminal output, or NULL
+     * if no typescript is being used for the terminal session.
+     */
+    guac_terminal_typescript* typescript;
+
+    /**
+     * Terminal-wide mouse cursor, synchronized across all users.
+     */
+    guac_common_cursor* cursor;
+
+    /**
+     * Graphical representation of the current scroll state.
+     */
+    guac_terminal_scrollbar* scrollbar;
+
+    /**
+     * The relative offset of the display. A positive value indicates that
+     * many rows have been scrolled into view, zero indicates that no
+     * scrolling has occurred. Negative values are illegal.
+     */
+    int scroll_offset;
+
+    /**
+     * The width of the terminal, in pixels.
+     */
+    int width;
+
+    /**
+     * The height of the terminal, in pixels.
+     */
+    int height;
+
+    /**
+     * The width of the terminal, in characters.
+     */
+    int term_width;
+
+    /**
+     * The height of the terminal, in characters.
+     */
+    int term_height;
+
+    /**
+     * The index of the first row in the scrolling region.
+     */
+    int scroll_start;
+
+    /**
+     * The index of the last row in the scrolling region.
+     */
+    int scroll_end;
+
+    /**
+     * The current row location of the cursor. Note that while most terminal
+     * operations will clip the cursor location within the bounds of the
+     * terminal, this is not guaranteed.
+     */
+    int cursor_row;
+
+    /**
+     * The current column location of the cursor. Note that while most
+     * terminal operations will clip the cursor location within the bounds
+     * of the terminal, this is not guaranteed. There are times when the
+     * cursor is legitimately outside the terminal bounds (such as when the
+     * end of a line is reached, but it is not yet necessary to scroll up).
+     */
+    int cursor_col;
+
+    /**
+     * The row of the rendered cursor.
+     */
+    int visible_cursor_row;
+
+    /**
+     * The column of the rendered cursor.
+     */
+    int visible_cursor_col;
+
+    /**
+     * The row of the saved cursor (ESC 7).
+     */
+    int saved_cursor_row;
+
+    /**
+     * The column of the saved cursor (ESC 7).
+     */
+    int saved_cursor_col;
+
+    /**
+     * The attributes which will be applied to future characters.
+     */
+    guac_terminal_attributes current_attributes;
+
+    /**
+     * The character whose attributes dictate the default attributes
+     * of all characters. When new screen space is allocated, this
+     * character fills the gaps.
+     */
+    guac_terminal_char default_char;
+
+    /**
+     * Handler which will receive all printed characters, updating the terminal
+     * accordingly.
+     */
+    guac_terminal_char_handler* char_handler;
+
+    /**
+     * The difference between the currently-rendered screen and the current
+     * state of the terminal.
+     */
+    guac_terminal_display* display;
+
+    /**
+     * Current terminal display state. All characters present on the screen
+     * are within this buffer. This has nothing to do with the display, which
+     * facilitates transfer of a set of changes to the remote display.
+     */
+    guac_terminal_buffer* buffer;
+
+    /**
+     * Automatically place a tabstop every N characters. If zero, then no
+     * tabstops exist automatically.
+     */
+    int tab_interval;
+
+    /**
+     * Array of all tabs set. Each entry is the column number of a tab + 1,
+     * or 0 if that tab cell is unset.
+     */
+    int custom_tabs[GUAC_TERMINAL_MAX_TABS];
+
+    /**
+     * Array of arrays of mapped characters, where the character N is located at the N-32
+     * position within the array. Each element in a contained array is the corresponding Unicode
+     * codepoint. If NULL, a direct mapping from Unicode is used. The entries of the main array
+     * correspond to the character set in use (G0, G1, etc.)
+     */
+    const int* char_mapping[2];
+
+    /**
+     * The active character set. For example, 0 for G0, 1 for G1, etc.
+     */
+    int active_char_set;
+
+    /**
+     * Whether text is being selected.
+     */
+    bool text_selected;
+
+    /**
+     * The row that the selection starts at.
+     */
+    int selection_start_row;
+
+    /**
+     * The column that the selection starts at.
+     */
+    int selection_start_column;
+
+    /**
+     * The width of the character at selection start.
+     */
+    int selection_start_width;
+
+    /**
+     * The row that the selection ends at.
+     */
+    int selection_end_row;
+
+    /**
+     * The column that the selection ends at.
+     */
+    int selection_end_column;
+
+    /**
+     * The width of the character at selection end.
+     */
+    int selection_end_width;
+
+    /**
+     * Whether the cursor (arrow) keys should send cursor sequences
+     * or application sequences (DECCKM).
+     */
+    bool application_cursor_keys;
+
+    /**
+     * Whether a CR should automatically follow a LF, VT, or FF.
+     */
+    bool automatic_carriage_return;
+
+    /**
+     * Whether insert mode is enabled (DECIM).
+     */
+    bool insert_mode;
+
+    /**
+     * Whether the alt key is currently being held down.
+     */
+    int mod_alt;
+
+    /**
+     * Whether the control key is currently being held down.
+     */
+    int mod_ctrl;
+
+    /**
+     * Whether the shift key is currently being held down.
+     */
+    int mod_shift;
+
+    /**
+     * The current mouse button state.
+     */
+    int mouse_mask;
+
+    /**
+     * The current mouse cursor, to avoid re-setting the cursor image.
+     */
+    guac_terminal_cursor_type current_cursor;
+
+    /**
+     * The current contents of the clipboard.
+     */
+    guac_common_clipboard* clipboard;
+
+};
+
+/**
+ * Creates a new guac_terminal, having the given width and height, and
+ * rendering to the given client.
+ *
+ * @param client
+ *     The client to which the terminal will be rendered.
+ *
+ * @param font_name
+ *     The name of the font to use when rendering glyphs.
+ *
+ * @param font_size
+ *     The size of each glyph, in points.
+ *
+ * @param dpi
+ *     The DPI of the display. The given font size will be adjusted to produce
+ *     glyphs at the given DPI.
+ *
+ * @param width
+ *     The width of the terminal, in pixels.
+ *
+ * @param height
+ *     The height of the terminal, in pixels.
+ *
+ * @param color_scheme
+ *     The name of the color scheme to use. This string must be one of the
+ *     names defined by the GUAC_TERMINAL_SCHEME_* constants. If blank or NULL,
+ *     the default scheme of GUAC_TERMINAL_SCHEME_GRAY_BLACK will be used. If
+ *     invalid, a warning will be logged, and the terminal will fall back on
+ *     GUAC_TERMINAL_SCHEME_GRAY_BLACK.
+ *
+ * @return
+ *     A new guac_terminal having the given font, dimensions, and attributes
+ *     which renders all text to the given client.
+ */
+guac_terminal* guac_terminal_create(guac_client* client,
+        const char* font_name, int font_size, int dpi,
+        int width, int height, const char* color_scheme);
+
+/**
+ * Frees all resources associated with the given terminal.
+ */
+void guac_terminal_free(guac_terminal* term);
+
+/**
+ * Renders a single frame of terminal data. If data is not yet available,
+ * this function will block until data is written.
+ */
+int guac_terminal_render_frame(guac_terminal* terminal);
+
+/**
+ * Reads from this terminal's STDIN. Input comes from key and mouse events
+ * supplied by calls to guac_terminal_send_key() and
+ * guac_terminal_send_mouse(). If input is not yet available, this function
+ * will block.
+ */
+int guac_terminal_read_stdin(guac_terminal* terminal, char* c, int size);
+
+/**
+ * Notifies the terminal that an event has occurred and the terminal should
+ * flush itself when reasonable.
+ *
+ * @param terminal
+ *     The terminal to notify.
+ */
+void guac_terminal_notify(guac_terminal* terminal);
+
+/**
+ * Reads a single line from this terminal's STDIN, storing the result in a
+ * newly-allocated string. Input is retrieved in the same manner as
+ * guac_terminal_read_stdin() and the same restrictions apply.
+ *
+ * @param terminal
+ *     The terminal to which the provided title should be output, and from
+ *     whose STDIN the single line of input should be read.
+ *
+ * @param title
+ *     The human-readable title to output to the terminal just prior to reading
+ *     from STDIN.
+ *
+ * @param echo
+ *     Non-zero if the characters read from STDIN should be echoed back as
+ *     terminal output, or zero if asterisks should be displayed instead.
+ *
+ * @return
+ *     A newly-allocated string containing a single line of input read from the
+ *     provided terminal's STDIN. This string must eventually be manually
+ *     freed with a call to free().
+ */
+char* guac_terminal_prompt(guac_terminal* terminal, const char* title,
+        bool echo);
+
+/**
+ * Writes the given format string and arguments to this terminal's STDOUT in
+ * the same manner as printf(). This function may block until space is
+ * freed in the output buffer by guac_terminal_render_frame().
+ */
+int guac_terminal_printf(guac_terminal* terminal, const char* format, ...);
+
+/**
+ * Handles the given key event, sending data, scrolling, pasting clipboard
+ * data, etc. as necessary.
+ */
+int guac_terminal_send_key(guac_terminal* term, int keysym, int pressed);
+
+/**
+ * Handles the given mouse event, sending data, scrolling, pasting clipboard
+ * data, etc. as necessary.
+ */
+int guac_terminal_send_mouse(guac_terminal* term, guac_user* user,
+        int x, int y, int mask);
+
+/**
+ * Handles a scroll event received from the scrollbar associated with a
+ * terminal.
+ *
+ * @param scrollbar
+ *     The scrollbar that has been scrolled.
+ *
+ * @param value
+ *     The new value that should be stored within the scrollbar, and
+ *     represented within the terminal display.
+ */
+void guac_terminal_scroll_handler(guac_terminal_scrollbar* scrollbar, int value);
+
+/**
+ * Clears the current clipboard contents and sets the mimetype for future
+ * contents.
+ */
+void guac_terminal_clipboard_reset(guac_terminal* term, const char* mimetype);
+
+/**
+ * Appends the given data to the current clipboard.
+ */
+void guac_terminal_clipboard_append(guac_terminal* term, const void* data, int length);
+
+/**
+ * Replicates the current display state to a user that has just joined the
+ * connection. All instructions necessary to replicate state are sent over the
+ * given socket.
+ *
+ * @param term
+ *     The terminal emulator associated with the connection being joined.
+ *
+ * @param user
+ *     The user joining the connection.
+ *
+ * @param socket
+ *     The guac_socket specific to the joining user and across which messages
+ *     synchronizing the current display state should be sent.
+ */
+void guac_terminal_dup(guac_terminal* term, guac_user* user,
+        guac_socket* socket);
+
+/* INTERNAL FUNCTIONS */
+
+
+/**
+ * Acquires exclusive access to the terminal. Note that enforcing this
+ * exclusive access requires that ALL users of the terminal call this
+ * function before making further calls to the terminal.
+ */
+void guac_terminal_lock(guac_terminal* terminal);
+
+/**
+ * Releases exclusive access to the terminal.
+ */
+void guac_terminal_unlock(guac_terminal* terminal);
+
+/**
+ * Resets the state of the given terminal, as if it were just allocated.
+ */
+void guac_terminal_reset(guac_terminal* term);
+
+/**
+ * Writes the given string of characters to the terminal.
+ */
+int guac_terminal_write(guac_terminal* term, const char* c, int size);
+
+/**
+ * Sets the character at the given row and column to the specified value.
+ */
+int guac_terminal_set(guac_terminal* term, int row, int col, int codepoint);
+
+/**
+ * Clears the given region within a single row.
+ */
+int guac_terminal_clear_columns(guac_terminal* term,
+        int row, int start_col, int end_col);
+
+/**
+ * Clears the given region from right-to-left, top-to-bottom, replacing
+ * all characters with the current background color and attributes.
+ */
+int guac_terminal_clear_range(guac_terminal* term,
+        int start_row, int start_col,
+        int end_row, int end_col);
+
+/**
+ * Scrolls the terminal's current scroll region up by one row.
+ */
+int guac_terminal_scroll_up(guac_terminal* term,
+        int start_row, int end_row, int amount);
+
+/**
+ * Scrolls the terminal's current scroll region down by one row.
+ */
+int guac_terminal_scroll_down(guac_terminal* term,
+        int start_row, int end_row, int amount);
+
+/**
+ * Commits the current cursor location, updating the visible cursor
+ * on the screen.
+ */
+void guac_terminal_commit_cursor(guac_terminal* term);
+
+/**
+ * Scroll down the display by the given amount, replacing the new space with
+ * data from the buffer. If not enough data is available, the maximum
+ * amount will be scrolled.
+ */
+void guac_terminal_scroll_display_down(guac_terminal* terminal, int amount);
+
+/**
+ * Scroll up the display by the given amount, replacing the new space with data
+ * from either the buffer or the terminal buffer.  If not enough data is
+ * available, the maximum amount will be scrolled.
+ */
+void guac_terminal_scroll_display_up(guac_terminal* terminal, int amount);
+
+/**
+ * Marks the start of text selection at the given row and column.
+ */
+void guac_terminal_select_start(guac_terminal* terminal, int row, int column);
+
+/**
+ * Updates the end of text selection at the given row and column.
+ */
+void guac_terminal_select_update(guac_terminal* terminal, int row, int column);
+
+/**
+ * Ends text selection, removing any highlight. Character data is stored in the
+ * string buffer provided.
+ */
+void guac_terminal_select_end(guac_terminal* terminal, char* string);
+
+
+/* LOW-LEVEL TERMINAL OPERATIONS */
+
+
+/**
+ * Copies the given range of columns to a new location, offset from
+ * the original by the given number of columns.
+ */
+void guac_terminal_copy_columns(guac_terminal* terminal, int row,
+        int start_column, int end_column, int offset);
+
+/**
+ * Copies the given range of rows to a new location, offset from the
+ * original by the given number of rows.
+ */
+void guac_terminal_copy_rows(guac_terminal* terminal,
+        int start_row, int end_row, int offset);
+
+/**
+ * Sets the given range of columns within the given row to the given
+ * character.
+ */
+void guac_terminal_set_columns(guac_terminal* terminal, int row,
+        int start_column, int end_column, guac_terminal_char* character);
+
+/**
+ * Resize the terminal to the given dimensions.
+ */
+int guac_terminal_resize(guac_terminal* term, int width, int height);
+
+/**
+ * Flushes all pending operations within the given guac_terminal.
+ */
+void guac_terminal_flush(guac_terminal* terminal);
+
+/**
+ * Sends the given string as if typed by the user. 
+ */
+int guac_terminal_send_data(guac_terminal* term, const char* data, int length);
+
+/**
+ * Sends the given string as if typed by the user. 
+ */
+int guac_terminal_send_string(guac_terminal* term, const char* data);
+
+/**
+ * Sends data through STDIN as if typed by the user, using the format
+ * string given and any args (similar to printf).
+ */
+int guac_terminal_sendf(guac_terminal* term, const char* format, ...);
+
+/**
+ * Sets a tabstop in the given column.
+ */
+void guac_terminal_set_tab(guac_terminal* term, int column);
+
+/**
+ * Removes the tabstop at the given column.
+ */
+void guac_terminal_unset_tab(guac_terminal* term, int column);
+
+/**
+ * Removes all tabstops.
+ */
+void guac_terminal_clear_tabs(guac_terminal* term);
+
+/**
+ * Given a column within the given terminal, returns the location of the
+ * next tabstop (or the rightmost character, if no more tabstops exist).
+ */
+int guac_terminal_next_tab(guac_terminal* term, int column);
+
+/**
+ * Opens a new pipe stream, redirecting all output from the given terminal to
+ * that pipe stream. If a pipe stream is already open, that pipe stream will
+ * be flushed and closed prior to opening the new pipe stream.
+ *
+ * @param term
+ *     The terminal which should redirect output to a new pipe stream having
+ *     the given name.
+ *
+ * @param name
+ *     The name of the pipe stream to open.
+ */
+void guac_terminal_pipe_stream_open(guac_terminal* term, const char* name);
+
+/**
+ * Writes a single byte of data to the pipe stream currently open and
+ * associated with the given terminal. The pipe stream must already have been
+ * opened via guac_terminal_pipe_stream_open(). If no pipe stream is currently
+ * open, this function has no effect. Data written through this function may
+ * be buffered.
+ *
+ * @param term
+ *     The terminal whose currently-open pipe stream should be written to.
+ *
+ * @param c
+ *     The byte of data to write to the pipe stream.
+ */
+void guac_terminal_pipe_stream_write(guac_terminal* term, char c);
+
+/**
+ * Flushes any data currently buffered for the currently-open pipe stream
+ * associated with the given terminal. The pipe stream must already have been
+ * opened via guac_terminal_pipe_stream_open(). If no pipe stream is currently
+ * open or no data is in the buffer, this function has no effect.
+ *
+ * @param term
+ *     The terminal whose pipe stream buffer should be flushed.
+ */
+void guac_terminal_pipe_stream_flush(guac_terminal* term);
+
+/**
+ * Closes the currently-open pipe stream associated with the given terminal,
+ * redirecting all output back to the terminal display.  Any data currently
+ * buffered for output to the pipe stream will be flushed prior to closure. The
+ * pipe stream must already have been opened via
+ * guac_terminal_pipe_stream_open(). If no pipe stream is currently open, this
+ * function has no effect.
+ *
+ * @param term
+ *     The terminal whose currently-open pipe stream should be closed.
+ */
+void guac_terminal_pipe_stream_close(guac_terminal* term);
+
+/**
+ * Requests that the terminal write all output to a new pair of typescript
+ * files within the given path and using the given base name. Terminal output
+ * will be written to these new files, along with timing information. If the
+ * create_path flag is non-zero, the given path will be created if it does not
+ * yet exist. If creation of the typescript files or path fails, error messages
+ * will automatically be logged, and no typescript will be written. The
+ * typescript will automatically be closed once the terminal is freed.
+ *
+ * @param term
+ *     The terminal whose output should be written to a typescript.
+ *
+ * @param path
+ *     The full absolute path to a directory in which the typescript files
+ *     should be created.
+ *
+ * @param name
+ *     The base name to use for the typescript files created within the
+ *     specified path.
+ *
+ * @param create_path
+ *     Zero if the specified path MUST exist for typescript files to be
+ *     written, or non-zero if the path should be created if it does not yet
+ *     exist.
+ *
+ * @return
+ *     Zero if the typescript files have been successfully created and a
+ *     typescript will be written, non-zero otherwise.
+ */
+int guac_terminal_create_typescript(guac_terminal* term, const char* path,
+        const char* name, int create_path);
+
+#endif
+

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/terminal/terminal_handlers.h
----------------------------------------------------------------------
diff --git a/src/terminal/terminal/terminal_handlers.h b/src/terminal/terminal/terminal_handlers.h
new file mode 100644
index 0000000..cc97686
--- /dev/null
+++ b/src/terminal/terminal/terminal_handlers.h
@@ -0,0 +1,165 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+#ifndef _GUAC_TERMINAL_HANDLERS
+#define _GUAC_TERMINAL_HANDLERS
+
+#include "config.h"
+
+#include "terminal.h"
+
+/**
+ * The default mode of the terminal. This character handler simply echoes
+ * received characters to the terminal display, entering other terminal modes
+ * if control characters are received.
+ *
+ * @param term
+ *     The terminal that received the given character of data.
+ *
+ * @param c
+ *     The character that was received by the given terminal.
+ */
+int guac_terminal_echo(guac_terminal* term, unsigned char c);
+
+/**
+ * Handles any characters which follow an ANSI ESC (0x1B) character.
+ *
+ * @param term
+ *     The terminal that received the given character of data.
+ *
+ * @param c
+ *     The character that was received by the given terminal.
+ */
+int guac_terminal_escape(guac_terminal* term, unsigned char c);
+
+/**
+ * Selects the G0 character mapping from the provided character mapping
+ * specifier (such as B, 0, U, or K).
+ *
+ * @param term
+ *     The terminal that received the given character of data.
+ *
+ * @param c
+ *     The character that was received by the given terminal.
+ */
+int guac_terminal_g0_charset(guac_terminal* term, unsigned char c);
+
+/**
+ * Selects the G1 character mapping from the provided character mapping
+ * specifier (such as B, 0, U, or K).
+ *
+ * @param term
+ *     The terminal that received the given character of data.
+ *
+ * @param c
+ *     The character that was received by the given terminal.
+ */
+int guac_terminal_g1_charset(guac_terminal* term, unsigned char c);
+
+/**
+ * Handles characters within a CSI sequence. CSI sequences are most often
+ * introduced with "ESC [".
+ *
+ * @param term
+ *     The terminal that received the given character of data.
+ *
+ * @param c
+ *     The character that was received by the given terminal.
+ */
+int guac_terminal_csi(guac_terminal* term, unsigned char c);
+
+/**
+ * Parses the remainder of the download initiation OSC specific to the
+ * Guacamole terminal emulator. A download will be initiated for the specified
+ * file once the OSC sequence is complete.
+ *
+ * @param term
+ *     The terminal that received the given character of data.
+ *
+ * @param c
+ *     The character that was received by the given terminal.
+ */
+int guac_terminal_download(guac_terminal* term, unsigned char c);
+
+/**
+ * Parses the remainder of the set directory OSC specific to the Guacamole
+ * terminal emulator. The upload directory will be set to the specified path
+ * once the OSC sequence is complete.
+ *
+ * @param term
+ *     The terminal that received the given character of data.
+ *
+ * @param c
+ *     The character that was received by the given terminal.
+ */
+int guac_terminal_set_directory(guac_terminal* term, unsigned char c);
+
+/**
+ * Parses the remainder of the open pipe OSC specific to the
+ * Guacamole terminal emulator. Terminal output will be redirected to a new
+ * named pipe having the given name once the OSC sequence is complete.
+ *
+ * @param term
+ *     The terminal that received the given character of data.
+ *
+ * @param c
+ *     The character that was received by the given terminal.
+ */
+int guac_terminal_open_pipe_stream(guac_terminal* term, unsigned char c);
+
+/**
+ * Parses the remainder of the close pipe OSC specific to the Guacamole
+ * terminal emulator. Terminal output will be redirected back to the terminal
+ * display and any open named pipe will be closed once the OSC sequence is
+ * complete.
+ *
+ * @param term
+ *     The terminal that received the given character of data.
+ *
+ * @param c
+ *     The character that was received by the given terminal.
+ */
+int guac_terminal_close_pipe_stream(guac_terminal* term, unsigned char c);
+
+/**
+ * Handles the remaining characters of an Operating System Code (OSC) sequence,
+ * typically initiated with "ESC ]".
+ *
+ * @param term
+ *     The terminal that received the given character of data.
+ *
+ * @param c
+ *     The character that was received by the given terminal.
+ */
+int guac_terminal_osc(guac_terminal* term, unsigned char c);
+
+/**
+ * Handles terminal control function sequences initiated with "ESC #".
+ *
+ * @param term
+ *     The terminal that received the given character of data.
+ *
+ * @param c
+ *     The character that was received by the given terminal.
+ */
+int guac_terminal_ctrl_func(guac_terminal* term, unsigned char c);
+
+#endif
+

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/terminal/types.h
----------------------------------------------------------------------
diff --git a/src/terminal/terminal/types.h b/src/terminal/terminal/types.h
new file mode 100644
index 0000000..ccb862f
--- /dev/null
+++ b/src/terminal/terminal/types.h
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+#ifndef _GUAC_TERMINAL_TYPES_H
+#define _GUAC_TERMINAL_TYPES_H
+
+#include "config.h"
+
+#include <stdbool.h>
+
+/**
+ * A character which is not truly a character, but rather part of an
+ * existing character which spans multiple columns. The original
+ * character will be somewhere earlier in the row, separated from
+ * this character by a contiguous string of zero of more
+ * GUAC_CHAR_CONTINUATION characters.
+ */
+#define GUAC_CHAR_CONTINUATION -1
+
+/**
+ * An RGB color, where each component ranges from 0 to 255.
+ */
+typedef struct guac_terminal_color {
+
+    /**
+     * The red component of this color.
+     */
+    int red;
+
+    /**
+     * The green component of this color.
+     */
+    int green;
+
+    /**
+     * The blue component of this color.
+     */
+    int blue;
+
+} guac_terminal_color;
+
+/**
+ * Terminal attributes, as can be applied to a single character.
+ */
+typedef struct guac_terminal_attributes {
+
+    /**
+     * Whether the character should be rendered bold.
+     */
+    bool bold;
+
+    /**
+     * Whether the character should be rendered with reversed colors
+     * (background becomes foreground and vice-versa).
+     */
+    bool reverse;
+
+    /**
+     * Whether the associated character is highlighted by the cursor.
+     */
+    bool cursor;
+
+    /**
+     * Whether to render the character with underscore.
+     */
+    bool underscore;
+
+    /**
+     * The foreground color of this character, as a palette index.
+     */
+    int foreground;
+
+    /**
+     * The background color of this character, as a palette index.
+     */
+    int background;
+
+} guac_terminal_attributes;
+
+/**
+ * Represents a single character for display in a terminal, including actual
+ * character value, foreground color, and background color.
+ */
+typedef struct guac_terminal_char {
+
+    /**
+     * The Unicode codepoint of the character to display, or
+     * GUAC_CHAR_CONTINUATION if this character is part of
+     * another character which spans multiple columns.
+     */
+    int value;
+
+    /**
+     * The attributes of the character to display.
+     */
+    guac_terminal_attributes attributes;
+
+    /**
+     * The number of columns this character occupies. If the character is
+     * GUAC_CHAR_CONTINUATION, this value is undefined and not applicable.
+     */
+    int width;
+
+} guac_terminal_char;
+
+#endif
+

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/terminal/typescript.h
----------------------------------------------------------------------
diff --git a/src/terminal/terminal/typescript.h b/src/terminal/terminal/typescript.h
new file mode 100644
index 0000000..9fce9e5
--- /dev/null
+++ b/src/terminal/terminal/typescript.h
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+#ifndef GUAC_TERMINAL_TYPESCRIPT_H
+#define GUAC_TERMINAL_TYPESCRIPT_H
+
+#include "config.h"
+
+#include <guacamole/timestamp.h>
+
+/**
+ * A NULL-terminated string of raw bytes which should be written at the
+ * beginning of any typescript.
+ */
+#define GUAC_TERMINAL_TYPESCRIPT_HEADER "[BEGIN TYPESCRIPT]\n"
+
+/**
+ * A NULL-terminated string of raw bytes which should be written at the
+ * end of any typescript.
+ */
+#define GUAC_TERMINAL_TYPESCRIPT_FOOTER "\n[END TYPESCRIPT]\n"
+
+/**
+ * The maximum amount of time to allow for a particular timing entry, in
+ * milliseconds. Any timing entries exceeding this value will be written as
+ * exactly this value instead.
+ */
+#define GUAC_TERMINAL_TYPESCRIPT_MAX_DELAY 86400000
+
+/**
+ * The maximum numeric value allowed for the .1, .2, .3, etc. suffix appended
+ * to the end of the typescript filename if a typescript having the requested
+ * name already exists.
+ */
+#define GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX 255
+
+/**
+ * The maximum length of the string containing a sequential numeric suffix
+ * between 1 and GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX inclusive, in bytes,
+ * including NULL terminator.
+ */
+#define GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX_LENGTH 4
+
+/**
+ * The maximum overall length of the full path to the typescript file,
+ * including any additional suffix and NULL terminator, in bytes.
+ */
+#define GUAC_TERMINAL_TYPESCRIPT_MAX_NAME_LENGTH 2048
+
+/**
+ * The suffix which will be appended to the typescript data file's name to
+ * produce the name of the timing file.
+ */
+#define GUAC_TERMINAL_TYPESCRIPT_TIMING_SUFFIX "timing"
+
+/**
+ * An active typescript, consisting of a data file (raw terminal output) and
+ * timing file (related timestamps and byte counts).
+ */
+typedef struct guac_terminal_typescript {
+
+    /**
+     * Buffer of raw terminal output which has not yet been written to the
+     * data file.
+     */
+    char buffer[4096];
+
+    /**
+     * The number of bytes currently stored in the buffer.
+     */
+    int length;
+
+    /**
+     * The full path to the file which will contain the raw terminal output for
+     * this typescript.
+     */
+    char data_filename[GUAC_TERMINAL_TYPESCRIPT_MAX_NAME_LENGTH];
+
+    /**
+     * The full path to the file which will contain the timing information for
+     * this typescript.
+     */
+    char timing_filename[GUAC_TERMINAL_TYPESCRIPT_MAX_NAME_LENGTH];
+
+    /**
+     * The file descriptor of the file into which raw terminal output should be
+     * written.
+     */
+    int data_fd;
+
+    /**
+     * The file descriptor of the file into which timing information
+     * (timestamps and byte counts) related to the raw terminal output in the
+     * data file should be written.
+     */
+    int timing_fd;
+
+    /**
+     * The last time that this typescript was flushed. If this typescript was
+     * never flushed, this will be the time the typescripe was created.
+     */
+    guac_timestamp last_flush;
+
+} guac_terminal_typescript;
+
+/**
+ * Creates a new pair of typescript files within the given path and using the
+ * given base name, returning an abstraction which represents those files.
+ * Terminal output will be written to these new files, along with timing
+ * information. If the create_path flag is non-zero, the given path will be
+ * created if it does not yet exist.
+ *
+ * @param path
+ *     The full absolute path to a directory in which the typescript files
+ *     should be created.
+ *
+ * @param name
+ *     The base name to use for the typescript files created within the
+ *     specified path.
+ *
+ * @param create_path
+ *     Zero if the specified path MUST exist for typescript files to be
+ *     written, or non-zero if the path should be created if it does not yet
+ *     exist.
+ *
+ * @return
+ *     A new guac_terminal_typescript representing the typescript files
+ *     requested, or NULL if creation of the typescript files failed.
+ */
+guac_terminal_typescript* guac_terminal_typescript_alloc(const char* path,
+        const char* name, int create_path);
+
+/**
+ * Writes a single byte of terminal data to the typescript, flushing and
+ * writing a new timestamp if necessary.
+ *
+ * @param typescript
+ *     The typescript that the given byte of raw terminal data should be
+ *     written to.
+ *
+ * @param c
+ *     The single byte of raw terminal data to write to the typescript.
+ */
+void guac_terminal_typescript_write(guac_terminal_typescript* typescript,
+        char c);
+
+/**
+ * Flushes any pending data to the typescript, writing a new timestamp to the
+ * timing file if any data was flushed.
+ *
+ * @param typescript
+ *     The typescript which should be flushed.
+ */
+void guac_terminal_typescript_flush(guac_terminal_typescript* typescript);
+
+/**
+ * Frees all resources associated with the given typescript, flushing and
+ * closing the data and timing files and freeing all related memory. If the
+ * provided typescript is NULL, this function has no effect.
+ *
+ * @param typescript
+ *     The typescript to free.
+ */
+void guac_terminal_typescript_free(guac_terminal_typescript* typescript);
+
+#endif
+

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/terminal_handlers.c
----------------------------------------------------------------------
diff --git a/src/terminal/terminal_handlers.c b/src/terminal/terminal_handlers.c
index 2bae0f2..328400c 100644
--- a/src/terminal/terminal_handlers.c
+++ b/src/terminal/terminal_handlers.c
@@ -19,10 +19,10 @@
 
 #include "config.h"
 
-#include "char_mappings.h"
-#include "terminal.h"
-#include "terminal_handlers.h"
-#include "types.h"
+#include "terminal/char_mappings.h"
+#include "terminal/terminal.h"
+#include "terminal/terminal_handlers.h"
+#include "terminal/types.h"
 
 #include <guacamole/client.h>
 #include <guacamole/protocol.h>

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/terminal_handlers.h
----------------------------------------------------------------------
diff --git a/src/terminal/terminal_handlers.h b/src/terminal/terminal_handlers.h
deleted file mode 100644
index cc97686..0000000
--- a/src/terminal/terminal_handlers.h
+++ /dev/null
@@ -1,165 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-#ifndef _GUAC_TERMINAL_HANDLERS
-#define _GUAC_TERMINAL_HANDLERS
-
-#include "config.h"
-
-#include "terminal.h"
-
-/**
- * The default mode of the terminal. This character handler simply echoes
- * received characters to the terminal display, entering other terminal modes
- * if control characters are received.
- *
- * @param term
- *     The terminal that received the given character of data.
- *
- * @param c
- *     The character that was received by the given terminal.
- */
-int guac_terminal_echo(guac_terminal* term, unsigned char c);
-
-/**
- * Handles any characters which follow an ANSI ESC (0x1B) character.
- *
- * @param term
- *     The terminal that received the given character of data.
- *
- * @param c
- *     The character that was received by the given terminal.
- */
-int guac_terminal_escape(guac_terminal* term, unsigned char c);
-
-/**
- * Selects the G0 character mapping from the provided character mapping
- * specifier (such as B, 0, U, or K).
- *
- * @param term
- *     The terminal that received the given character of data.
- *
- * @param c
- *     The character that was received by the given terminal.
- */
-int guac_terminal_g0_charset(guac_terminal* term, unsigned char c);
-
-/**
- * Selects the G1 character mapping from the provided character mapping
- * specifier (such as B, 0, U, or K).
- *
- * @param term
- *     The terminal that received the given character of data.
- *
- * @param c
- *     The character that was received by the given terminal.
- */
-int guac_terminal_g1_charset(guac_terminal* term, unsigned char c);
-
-/**
- * Handles characters within a CSI sequence. CSI sequences are most often
- * introduced with "ESC [".
- *
- * @param term
- *     The terminal that received the given character of data.
- *
- * @param c
- *     The character that was received by the given terminal.
- */
-int guac_terminal_csi(guac_terminal* term, unsigned char c);
-
-/**
- * Parses the remainder of the download initiation OSC specific to the
- * Guacamole terminal emulator. A download will be initiated for the specified
- * file once the OSC sequence is complete.
- *
- * @param term
- *     The terminal that received the given character of data.
- *
- * @param c
- *     The character that was received by the given terminal.
- */
-int guac_terminal_download(guac_terminal* term, unsigned char c);
-
-/**
- * Parses the remainder of the set directory OSC specific to the Guacamole
- * terminal emulator. The upload directory will be set to the specified path
- * once the OSC sequence is complete.
- *
- * @param term
- *     The terminal that received the given character of data.
- *
- * @param c
- *     The character that was received by the given terminal.
- */
-int guac_terminal_set_directory(guac_terminal* term, unsigned char c);
-
-/**
- * Parses the remainder of the open pipe OSC specific to the
- * Guacamole terminal emulator. Terminal output will be redirected to a new
- * named pipe having the given name once the OSC sequence is complete.
- *
- * @param term
- *     The terminal that received the given character of data.
- *
- * @param c
- *     The character that was received by the given terminal.
- */
-int guac_terminal_open_pipe_stream(guac_terminal* term, unsigned char c);
-
-/**
- * Parses the remainder of the close pipe OSC specific to the Guacamole
- * terminal emulator. Terminal output will be redirected back to the terminal
- * display and any open named pipe will be closed once the OSC sequence is
- * complete.
- *
- * @param term
- *     The terminal that received the given character of data.
- *
- * @param c
- *     The character that was received by the given terminal.
- */
-int guac_terminal_close_pipe_stream(guac_terminal* term, unsigned char c);
-
-/**
- * Handles the remaining characters of an Operating System Code (OSC) sequence,
- * typically initiated with "ESC ]".
- *
- * @param term
- *     The terminal that received the given character of data.
- *
- * @param c
- *     The character that was received by the given terminal.
- */
-int guac_terminal_osc(guac_terminal* term, unsigned char c);
-
-/**
- * Handles terminal control function sequences initiated with "ESC #".
- *
- * @param term
- *     The terminal that received the given character of data.
- *
- * @param c
- *     The character that was received by the given terminal.
- */
-int guac_terminal_ctrl_func(guac_terminal* term, unsigned char c);
-
-#endif
-

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/types.h
----------------------------------------------------------------------
diff --git a/src/terminal/types.h b/src/terminal/types.h
deleted file mode 100644
index ccb862f..0000000
--- a/src/terminal/types.h
+++ /dev/null
@@ -1,124 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-#ifndef _GUAC_TERMINAL_TYPES_H
-#define _GUAC_TERMINAL_TYPES_H
-
-#include "config.h"
-
-#include <stdbool.h>
-
-/**
- * A character which is not truly a character, but rather part of an
- * existing character which spans multiple columns. The original
- * character will be somewhere earlier in the row, separated from
- * this character by a contiguous string of zero of more
- * GUAC_CHAR_CONTINUATION characters.
- */
-#define GUAC_CHAR_CONTINUATION -1
-
-/**
- * An RGB color, where each component ranges from 0 to 255.
- */
-typedef struct guac_terminal_color {
-
-    /**
-     * The red component of this color.
-     */
-    int red;
-
-    /**
-     * The green component of this color.
-     */
-    int green;
-
-    /**
-     * The blue component of this color.
-     */
-    int blue;
-
-} guac_terminal_color;
-
-/**
- * Terminal attributes, as can be applied to a single character.
- */
-typedef struct guac_terminal_attributes {
-
-    /**
-     * Whether the character should be rendered bold.
-     */
-    bool bold;
-
-    /**
-     * Whether the character should be rendered with reversed colors
-     * (background becomes foreground and vice-versa).
-     */
-    bool reverse;
-
-    /**
-     * Whether the associated character is highlighted by the cursor.
-     */
-    bool cursor;
-
-    /**
-     * Whether to render the character with underscore.
-     */
-    bool underscore;
-
-    /**
-     * The foreground color of this character, as a palette index.
-     */
-    int foreground;
-
-    /**
-     * The background color of this character, as a palette index.
-     */
-    int background;
-
-} guac_terminal_attributes;
-
-/**
- * Represents a single character for display in a terminal, including actual
- * character value, foreground color, and background color.
- */
-typedef struct guac_terminal_char {
-
-    /**
-     * The Unicode codepoint of the character to display, or
-     * GUAC_CHAR_CONTINUATION if this character is part of
-     * another character which spans multiple columns.
-     */
-    int value;
-
-    /**
-     * The attributes of the character to display.
-     */
-    guac_terminal_attributes attributes;
-
-    /**
-     * The number of columns this character occupies. If the character is
-     * GUAC_CHAR_CONTINUATION, this value is undefined and not applicable.
-     */
-    int width;
-
-} guac_terminal_char;
-
-#endif
-

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/typescript.c
----------------------------------------------------------------------
diff --git a/src/terminal/typescript.c b/src/terminal/typescript.c
index aa922f0..69c8a1d 100644
--- a/src/terminal/typescript.c
+++ b/src/terminal/typescript.c
@@ -19,7 +19,7 @@
 
 #include "config.h"
 #include "common/io.h"
-#include "typescript.h"
+#include "terminal/typescript.h"
 
 #include <guacamole/timestamp.h>
 

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/typescript.h
----------------------------------------------------------------------
diff --git a/src/terminal/typescript.h b/src/terminal/typescript.h
deleted file mode 100644
index 9fce9e5..0000000
--- a/src/terminal/typescript.h
+++ /dev/null
@@ -1,184 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-#ifndef GUAC_TERMINAL_TYPESCRIPT_H
-#define GUAC_TERMINAL_TYPESCRIPT_H
-
-#include "config.h"
-
-#include <guacamole/timestamp.h>
-
-/**
- * A NULL-terminated string of raw bytes which should be written at the
- * beginning of any typescript.
- */
-#define GUAC_TERMINAL_TYPESCRIPT_HEADER "[BEGIN TYPESCRIPT]\n"
-
-/**
- * A NULL-terminated string of raw bytes which should be written at the
- * end of any typescript.
- */
-#define GUAC_TERMINAL_TYPESCRIPT_FOOTER "\n[END TYPESCRIPT]\n"
-
-/**
- * The maximum amount of time to allow for a particular timing entry, in
- * milliseconds. Any timing entries exceeding this value will be written as
- * exactly this value instead.
- */
-#define GUAC_TERMINAL_TYPESCRIPT_MAX_DELAY 86400000
-
-/**
- * The maximum numeric value allowed for the .1, .2, .3, etc. suffix appended
- * to the end of the typescript filename if a typescript having the requested
- * name already exists.
- */
-#define GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX 255
-
-/**
- * The maximum length of the string containing a sequential numeric suffix
- * between 1 and GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX inclusive, in bytes,
- * including NULL terminator.
- */
-#define GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX_LENGTH 4
-
-/**
- * The maximum overall length of the full path to the typescript file,
- * including any additional suffix and NULL terminator, in bytes.
- */
-#define GUAC_TERMINAL_TYPESCRIPT_MAX_NAME_LENGTH 2048
-
-/**
- * The suffix which will be appended to the typescript data file's name to
- * produce the name of the timing file.
- */
-#define GUAC_TERMINAL_TYPESCRIPT_TIMING_SUFFIX "timing"
-
-/**
- * An active typescript, consisting of a data file (raw terminal output) and
- * timing file (related timestamps and byte counts).
- */
-typedef struct guac_terminal_typescript {
-
-    /**
-     * Buffer of raw terminal output which has not yet been written to the
-     * data file.
-     */
-    char buffer[4096];
-
-    /**
-     * The number of bytes currently stored in the buffer.
-     */
-    int length;
-
-    /**
-     * The full path to the file which will contain the raw terminal output for
-     * this typescript.
-     */
-    char data_filename[GUAC_TERMINAL_TYPESCRIPT_MAX_NAME_LENGTH];
-
-    /**
-     * The full path to the file which will contain the timing information for
-     * this typescript.
-     */
-    char timing_filename[GUAC_TERMINAL_TYPESCRIPT_MAX_NAME_LENGTH];
-
-    /**
-     * The file descriptor of the file into which raw terminal output should be
-     * written.
-     */
-    int data_fd;
-
-    /**
-     * The file descriptor of the file into which timing information
-     * (timestamps and byte counts) related to the raw terminal output in the
-     * data file should be written.
-     */
-    int timing_fd;
-
-    /**
-     * The last time that this typescript was flushed. If this typescript was
-     * never flushed, this will be the time the typescripe was created.
-     */
-    guac_timestamp last_flush;
-
-} guac_terminal_typescript;
-
-/**
- * Creates a new pair of typescript files within the given path and using the
- * given base name, returning an abstraction which represents those files.
- * Terminal output will be written to these new files, along with timing
- * information. If the create_path flag is non-zero, the given path will be
- * created if it does not yet exist.
- *
- * @param path
- *     The full absolute path to a directory in which the typescript files
- *     should be created.
- *
- * @param name
- *     The base name to use for the typescript files created within the
- *     specified path.
- *
- * @param create_path
- *     Zero if the specified path MUST exist for typescript files to be
- *     written, or non-zero if the path should be created if it does not yet
- *     exist.
- *
- * @return
- *     A new guac_terminal_typescript representing the typescript files
- *     requested, or NULL if creation of the typescript files failed.
- */
-guac_terminal_typescript* guac_terminal_typescript_alloc(const char* path,
-        const char* name, int create_path);
-
-/**
- * Writes a single byte of terminal data to the typescript, flushing and
- * writing a new timestamp if necessary.
- *
- * @param typescript
- *     The typescript that the given byte of raw terminal data should be
- *     written to.
- *
- * @param c
- *     The single byte of raw terminal data to write to the typescript.
- */
-void guac_terminal_typescript_write(guac_terminal_typescript* typescript,
-        char c);
-
-/**
- * Flushes any pending data to the typescript, writing a new timestamp to the
- * timing file if any data was flushed.
- *
- * @param typescript
- *     The typescript which should be flushed.
- */
-void guac_terminal_typescript_flush(guac_terminal_typescript* typescript);
-
-/**
- * Frees all resources associated with the given typescript, flushing and
- * closing the data and timing files and freeing all related memory. If the
- * provided typescript is NULL, this function has no effect.
- *
- * @param typescript
- *     The typescript to free.
- */
-void guac_terminal_typescript_free(guac_terminal_typescript* typescript);
-
-#endif
-


[4/9] incubator-guacamole-server git commit: GUACAMOLE-169: Move terminal headers to namespaced directory.

Posted by jm...@apache.org.
GUACAMOLE-169: Move terminal headers to namespaced directory.


Project: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/commit/3f7ccb6b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/tree/3f7ccb6b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/diff/3f7ccb6b

Branch: refs/heads/master
Commit: 3f7ccb6b9a6212c7e3c6fde57ccd4dbb5b3b6ab7
Parents: d371f2d
Author: Michael Jumper <mj...@apache.org>
Authored: Mon Feb 27 14:34:46 2017 -0800
Committer: Michael Jumper <mj...@apache.org>
Committed: Mon Feb 27 14:34:46 2017 -0800

----------------------------------------------------------------------
 src/protocols/ssh/client.c                |   2 +-
 src/protocols/ssh/clipboard.c             |   2 +-
 src/protocols/ssh/input.c                 |   2 +-
 src/protocols/ssh/ssh.c                   |   2 +-
 src/protocols/ssh/ssh.h                   |   2 +-
 src/protocols/telnet/client.c             |   2 +-
 src/protocols/telnet/client.h             |   2 +-
 src/protocols/telnet/clipboard.c          |   2 +-
 src/protocols/telnet/input.c              |   2 +-
 src/protocols/telnet/telnet.c             |   2 +-
 src/protocols/telnet/telnet.h             |   2 +-
 src/protocols/telnet/user.c               |   2 +-
 src/terminal/Makefile.am                  |  20 +-
 src/terminal/buffer.c                     |   4 +-
 src/terminal/buffer.h                     | 129 ----
 src/terminal/char_mappings.h              |  48 --
 src/terminal/common.c                     |   2 +-
 src/terminal/common.h                     |  53 --
 src/terminal/display.c                    |   6 +-
 src/terminal/display.h                    | 363 -----------
 src/terminal/scrollbar.c                  |   2 +-
 src/terminal/scrollbar.h                  | 357 -----------
 src/terminal/terminal.c                   |  14 +-
 src/terminal/terminal.h                   | 839 -------------------------
 src/terminal/terminal/buffer.h            | 129 ++++
 src/terminal/terminal/char_mappings.h     |  48 ++
 src/terminal/terminal/common.h            |  53 ++
 src/terminal/terminal/display.h           | 363 +++++++++++
 src/terminal/terminal/scrollbar.h         | 357 +++++++++++
 src/terminal/terminal/terminal.h          | 839 +++++++++++++++++++++++++
 src/terminal/terminal/terminal_handlers.h | 165 +++++
 src/terminal/terminal/types.h             | 124 ++++
 src/terminal/terminal/typescript.h        | 184 ++++++
 src/terminal/terminal_handlers.c          |   8 +-
 src/terminal/terminal_handlers.h          | 165 -----
 src/terminal/types.h                      | 124 ----
 src/terminal/typescript.c                 |   2 +-
 src/terminal/typescript.h                 | 184 ------
 38 files changed, 2303 insertions(+), 2303 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/protocols/ssh/client.c
----------------------------------------------------------------------
diff --git a/src/protocols/ssh/client.c b/src/protocols/ssh/client.c
index 2110ba9..c0a26b9 100644
--- a/src/protocols/ssh/client.c
+++ b/src/protocols/ssh/client.c
@@ -22,7 +22,7 @@
 #include "client.h"
 #include "common-ssh/sftp.h"
 #include "ssh.h"
-#include "terminal.h"
+#include "terminal/terminal.h"
 #include "user.h"
 
 #include <langinfo.h>

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/protocols/ssh/clipboard.c
----------------------------------------------------------------------
diff --git a/src/protocols/ssh/clipboard.c b/src/protocols/ssh/clipboard.c
index 72a2b6c..0e23632 100644
--- a/src/protocols/ssh/clipboard.c
+++ b/src/protocols/ssh/clipboard.c
@@ -20,7 +20,7 @@
 #include "config.h"
 #include "clipboard.h"
 #include "ssh.h"
-#include "terminal.h"
+#include "terminal/terminal.h"
 
 #include <guacamole/client.h>
 #include <guacamole/stream.h>

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/protocols/ssh/input.c
----------------------------------------------------------------------
diff --git a/src/protocols/ssh/input.c b/src/protocols/ssh/input.c
index e0267e2..95e0188 100644
--- a/src/protocols/ssh/input.c
+++ b/src/protocols/ssh/input.c
@@ -22,7 +22,7 @@
 #include "common/cursor.h"
 #include "common/display.h"
 #include "ssh.h"
-#include "terminal.h"
+#include "terminal/terminal.h"
 
 #include <guacamole/client.h>
 #include <guacamole/user.h>

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/protocols/ssh/ssh.c
----------------------------------------------------------------------
diff --git a/src/protocols/ssh/ssh.c b/src/protocols/ssh/ssh.c
index dd0a30f..43e66f8 100644
--- a/src/protocols/ssh/ssh.c
+++ b/src/protocols/ssh/ssh.c
@@ -25,7 +25,7 @@
 #include "settings.h"
 #include "sftp.h"
 #include "ssh.h"
-#include "terminal.h"
+#include "terminal/terminal.h"
 
 #ifdef ENABLE_SSH_AGENT
 #include "ssh_agent.h"

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/protocols/ssh/ssh.h
----------------------------------------------------------------------
diff --git a/src/protocols/ssh/ssh.h b/src/protocols/ssh/ssh.h
index d6dbdaf..032e920 100644
--- a/src/protocols/ssh/ssh.h
+++ b/src/protocols/ssh/ssh.h
@@ -26,7 +26,7 @@
 #include "common-ssh/ssh.h"
 #include "common-ssh/user.h"
 #include "settings.h"
-#include "terminal.h"
+#include "terminal/terminal.h"
 
 #ifdef ENABLE_SSH_AGENT
 #include "ssh_agent.h"

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/protocols/telnet/client.c
----------------------------------------------------------------------
diff --git a/src/protocols/telnet/client.c b/src/protocols/telnet/client.c
index a039322..2a53d26 100644
--- a/src/protocols/telnet/client.c
+++ b/src/protocols/telnet/client.c
@@ -21,7 +21,7 @@
 #include "client.h"
 #include "settings.h"
 #include "telnet.h"
-#include "terminal.h"
+#include "terminal/terminal.h"
 #include "user.h"
 
 #include <langinfo.h>

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/protocols/telnet/client.h
----------------------------------------------------------------------
diff --git a/src/protocols/telnet/client.h b/src/protocols/telnet/client.h
index a7141e0..8921a9f 100644
--- a/src/protocols/telnet/client.h
+++ b/src/protocols/telnet/client.h
@@ -21,7 +21,7 @@
 #define GUAC_TELNET__CLIENT_H
 
 #include "config.h"
-#include "terminal.h"
+#include "terminal/terminal.h"
 
 #include <pthread.h>
 #include <regex.h>

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/protocols/telnet/clipboard.c
----------------------------------------------------------------------
diff --git a/src/protocols/telnet/clipboard.c b/src/protocols/telnet/clipboard.c
index 44bcf1c..08d28b1 100644
--- a/src/protocols/telnet/clipboard.c
+++ b/src/protocols/telnet/clipboard.c
@@ -20,7 +20,7 @@
 #include "config.h"
 #include "clipboard.h"
 #include "telnet.h"
-#include "terminal.h"
+#include "terminal/terminal.h"
 
 #include <guacamole/client.h>
 #include <guacamole/stream.h>

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/protocols/telnet/input.c
----------------------------------------------------------------------
diff --git a/src/protocols/telnet/input.c b/src/protocols/telnet/input.c
index bb88751..be50360 100644
--- a/src/protocols/telnet/input.c
+++ b/src/protocols/telnet/input.c
@@ -19,7 +19,7 @@
 
 #include "config.h"
 #include "input.h"
-#include "terminal.h"
+#include "terminal/terminal.h"
 #include "telnet.h"
 
 #include <guacamole/client.h>

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/protocols/telnet/telnet.c
----------------------------------------------------------------------
diff --git a/src/protocols/telnet/telnet.c b/src/protocols/telnet/telnet.c
index b80c959..0bf4bbb 100644
--- a/src/protocols/telnet/telnet.c
+++ b/src/protocols/telnet/telnet.c
@@ -20,7 +20,7 @@
 #include "config.h"
 #include "common/recording.h"
 #include "telnet.h"
-#include "terminal.h"
+#include "terminal/terminal.h"
 
 #include <guacamole/client.h>
 #include <guacamole/protocol.h>

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/protocols/telnet/telnet.h
----------------------------------------------------------------------
diff --git a/src/protocols/telnet/telnet.h b/src/protocols/telnet/telnet.h
index 3a48a80..8259507 100644
--- a/src/protocols/telnet/telnet.h
+++ b/src/protocols/telnet/telnet.h
@@ -22,7 +22,7 @@
 
 #include "config.h"
 #include "settings.h"
-#include "terminal.h"
+#include "terminal/terminal.h"
 
 #include <libtelnet.h>
 

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/protocols/telnet/user.c
----------------------------------------------------------------------
diff --git a/src/protocols/telnet/user.c b/src/protocols/telnet/user.c
index aab8f62..d5458e2 100644
--- a/src/protocols/telnet/user.c
+++ b/src/protocols/telnet/user.c
@@ -23,7 +23,7 @@
 #include "input.h"
 #include "settings.h"
 #include "telnet.h"
-#include "terminal.h"
+#include "terminal/terminal.h"
 #include "user.h"
 
 #include <guacamole/client.h>

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/Makefile.am
----------------------------------------------------------------------
diff --git a/src/terminal/Makefile.am b/src/terminal/Makefile.am
index 59bcd12..53bc74d 100644
--- a/src/terminal/Makefile.am
+++ b/src/terminal/Makefile.am
@@ -22,16 +22,16 @@ ACLOCAL_AMFLAGS = -I m4
 
 noinst_LTLIBRARIES = libguac_terminal.la
 
-noinst_HEADERS =                \
-    buffer.h                    \
-    char_mappings.h             \
-    common.h                    \
-    display.h                   \
-    scrollbar.h                 \
-    terminal.h                  \
-    terminal_handlers.h         \
-    types.h                     \
-    typescript.h
+noinst_HEADERS =                 \
+    terminal/buffer.h            \
+    terminal/char_mappings.h     \
+    terminal/common.h            \
+    terminal/display.h           \
+    terminal/scrollbar.h         \
+    terminal/terminal.h          \
+    terminal/terminal_handlers.h \
+    terminal/types.h             \
+    terminal/typescript.h
 
 libguac_terminal_la_SOURCES =   \
     buffer.c                    \

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/buffer.c
----------------------------------------------------------------------
diff --git a/src/terminal/buffer.c b/src/terminal/buffer.c
index e536d92..2985592 100644
--- a/src/terminal/buffer.c
+++ b/src/terminal/buffer.c
@@ -19,8 +19,8 @@
 
 #include "config.h"
 
-#include "buffer.h"
-#include "common.h"
+#include "terminal/buffer.h"
+#include "terminal/common.h"
 
 #include <stdlib.h>
 #include <string.h>

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/buffer.h
----------------------------------------------------------------------
diff --git a/src/terminal/buffer.h b/src/terminal/buffer.h
deleted file mode 100644
index db2a4a1..0000000
--- a/src/terminal/buffer.h
+++ /dev/null
@@ -1,129 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-#ifndef _GUAC_TERMINAL_BUFFER_H
-#define _GUAC_TERMINAL_BUFFER_H
-
-#include "config.h"
-
-#include "types.h"
-
-/**
- * A single variable-length row of terminal data.
- */
-typedef struct guac_terminal_buffer_row {
-
-    /**
-     * Array of guac_terminal_char representing the contents of the row.
-     */
-    guac_terminal_char* characters;
-
-    /**
-     * The length of this row in characters. This is the number of initialized
-     * characters in the buffer, usually equal to the number of characters
-     * in the screen width at the time this row was created.
-     */
-    int length;
-
-    /**
-     * The number of elements in the characters array. After the length
-     * equals this value, the array must be resized.
-     */
-    int available;
-
-} guac_terminal_buffer_row;
-
-/**
- * A buffer containing a constant number of arbitrary-length rows.
- * New rows can be appended to the buffer, with the oldest row replaced with
- * the new row.
- */
-typedef struct guac_terminal_buffer {
-
-    /**
-     * The character to assign to newly-allocated cells.
-     */
-    guac_terminal_char default_character;
-
-    /**
-     * Array of buffer rows. This array functions as a ring buffer.
-     * When a new row needs to be appended, the top reference is moved down
-     * and the old top row is replaced.
-     */
-    guac_terminal_buffer_row* rows;
-
-    /**
-     * The row to replace when adding a new row to the buffer.
-     */
-    int top;
-
-    /**
-     * The number of rows currently stored in the buffer.
-     */
-    int length;
-
-    /**
-     * The number of rows in the buffer. This is the total capacity
-     * of the buffer.
-     */
-    int available;
-
-} guac_terminal_buffer;
-
-/**
- * Allocates a new buffer having the given maximum number of rows. New character cells will
- * be initialized to the given character.
- */
-guac_terminal_buffer* guac_terminal_buffer_alloc(int rows, guac_terminal_char* default_character);
-
-/**
- * Frees the given buffer.
- */
-void guac_terminal_buffer_free(guac_terminal_buffer* buffer);
-
-/**
- * Returns the row at the given location. The row returned is guaranteed to be at least the given
- * width.
- */
-guac_terminal_buffer_row* guac_terminal_buffer_get_row(guac_terminal_buffer* buffer, int row, int width);
-
-/**
- * Copies the given range of columns to a new location, offset from
- * the original by the given number of columns.
- */
-void guac_terminal_buffer_copy_columns(guac_terminal_buffer* buffer, int row,
-        int start_column, int end_column, int offset);
-
-/**
- * Copies the given range of rows to a new location, offset from the
- * original by the given number of rows.
- */
-void guac_terminal_buffer_copy_rows(guac_terminal_buffer* buffer,
-        int start_row, int end_row, int offset);
-
-/**
- * Sets the given range of columns within the given row to the given
- * character.
- */
-void guac_terminal_buffer_set_columns(guac_terminal_buffer* buffer, int row,
-        int start_column, int end_column, guac_terminal_char* character);
-
-#endif
-

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/char_mappings.h
----------------------------------------------------------------------
diff --git a/src/terminal/char_mappings.h b/src/terminal/char_mappings.h
deleted file mode 100644
index 5e0e474..0000000
--- a/src/terminal/char_mappings.h
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-#ifndef _GUAC_TERMINAL_CHAR_MAPPINGS_H
-#define _GUAC_TERMINAL_CHAR_MAPPINGS_H
-
-#include "config.h"
-
-/**
- * VT100 graphics mapping. Each entry is the corresponding Unicode codepoint
- * for the character N+32, where N is the index of the element in the array.
- * All characters less than 32 are universally mapped to themselves.
- */
-extern const int vt100_map[];
-
-/**
- * Null graphics mapping. Each entry is the corresponding Unicode codepoint
- * for the character N+32, where N is the index of the element in the array.
- * All characters less than 32 are universally mapped to themselves.
- */
-extern const int null_map[];
-
-/**
- * User graphics mapping. Each entry is the corresponding Unicode codepoint
- * for the character N+32, where N is the index of the element in the array.
- * All characters less than 32 are universally mapped to themselves.
- */
-extern const int user_map[];
-
-#endif
-

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/common.c
----------------------------------------------------------------------
diff --git a/src/terminal/common.c b/src/terminal/common.c
index 4018e77..9913753 100644
--- a/src/terminal/common.c
+++ b/src/terminal/common.c
@@ -18,7 +18,7 @@
  */
 
 #include "config.h"
-#include "types.h"
+#include "terminal/types.h"
 
 #include <stdbool.h>
 #include <unistd.h>

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/common.h
----------------------------------------------------------------------
diff --git a/src/terminal/common.h b/src/terminal/common.h
deleted file mode 100644
index f7eda61..0000000
--- a/src/terminal/common.h
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-#ifndef _GUAC_TERMINAL_COMMON_H
-#define _GUAC_TERMINAL_COMMON_H
-
-#include "config.h"
-
-#include <stdbool.h>
-
-/**
- * Returns the closest value to the value given that is also
- * within the given range.
- */
-int guac_terminal_fit_to_range(int value, int min, int max);
-
-/**
- * Encodes the given codepoint as UTF-8, storing the result within the
- * provided buffer, and returning the number of bytes stored.
- */
-int guac_terminal_encode_utf8(int codepoint, char* utf8);
-
-/**
- * Returns whether a codepoint has a corresponding glyph, or is rendered
- * as a blank space.
- */
-bool guac_terminal_has_glyph(int codepoint);
-
-/**
- * Similar to write, but automatically retries the write operation until
- * an error occurs.
- */
-int guac_terminal_write_all(int fd, const char* buffer, int size);
-
-#endif
-

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/display.c
----------------------------------------------------------------------
diff --git a/src/terminal/display.c b/src/terminal/display.c
index e671c60..1a0aeb5 100644
--- a/src/terminal/display.c
+++ b/src/terminal/display.c
@@ -19,10 +19,10 @@
 
 #include "config.h"
 
-#include "common.h"
 #include "common/surface.h"
-#include "display.h"
-#include "types.h"
+#include "terminal/common.h"
+#include "terminal/display.h"
+#include "terminal/types.h"
 
 #include <math.h>
 #include <stdlib.h>

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/display.h
----------------------------------------------------------------------
diff --git a/src/terminal/display.h b/src/terminal/display.h
deleted file mode 100644
index 655d1aa..0000000
--- a/src/terminal/display.h
+++ /dev/null
@@ -1,363 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-#ifndef _GUAC_TERMINAL_DISPLAY_H
-#define _GUAC_TERMINAL_DISPLAY_H
-
-#include "config.h"
-
-#include "common/surface.h"
-#include "types.h"
-
-#include <guacamole/client.h>
-#include <guacamole/layer.h>
-#include <pango/pangocairo.h>
-
-#include <stdbool.h>
-
-/**
- * The maximum width of any character, in columns.
- */
-#define GUAC_TERMINAL_MAX_CHAR_WIDTH 2
-
-/**
- * The index of black within the terminal color palette.
- */
-#define GUAC_TERMINAL_COLOR_BLACK 0
-
-/**
- * The index of low-intensity red within the terminal color palette.
- */
-#define GUAC_TERMINAL_COLOR_DARK_RED 1
-
-/**
- * The index of low-intensity green within the terminal color palette.
- */
-#define GUAC_TERMINAL_COLOR_DARK_GREEN 2
-
-/**
- * The index of brown within the terminal color palette.
- */
-#define GUAC_TERMINAL_COLOR_BROWN 3
-
-/**
- * The index of low-intensity blue within the terminal color palette.
- */
-#define GUAC_TERMINAL_COLOR_DARK_BLUE 4
-
-/**
- * The index of low-intensity magenta (purple) within the terminal color
- * palette.
- */
-#define GUAC_TERMINAL_COLOR_PURPLE 5
-
-/**
- * The index of low-intensity cyan (teal) within the terminal color palette.
- */
-#define GUAC_TERMINAL_COLOR_TEAL 6
-
-/**
- * The index of low-intensity white (gray) within the terminal color palette.
- */
-#define GUAC_TERMINAL_COLOR_GRAY 7
-
-/**
- * The index of bright black (dark gray) within the terminal color palette.
- */
-#define GUAC_TERMINAL_COLOR_DARK_GRAY 8
-
-/**
- * The index of bright red within the terminal color palette.
- */
-#define GUAC_TERMINAL_COLOR_RED 9
-
-/**
- * The index of bright green within the terminal color palette.
- */
-#define GUAC_TERMINAL_COLOR_GREEN 10
-
-/**
- * The index of bright brown (yellow) within the terminal color palette.
- */
-#define GUAC_TERMINAL_COLOR_YELLOW 11
-
-/**
- * The index of bright blue within the terminal color palette.
- */
-#define GUAC_TERMINAL_COLOR_BLUE 12
-
-/**
- * The index of bright magenta within the terminal color palette.
- */
-#define GUAC_TERMINAL_COLOR_MAGENTA 13
-
-/**
- * The index of bright cyan within the terminal color palette.
- */
-#define GUAC_TERMINAL_COLOR_CYAN 14
-
-/**
- * The index of bright white within the terminal color palette.
- */
-#define GUAC_TERMINAL_COLOR_WHITE 15
-
-/**
- * The available color palette. All integer colors within structures
- * here are indices into this palette.
- */
-extern const guac_terminal_color guac_terminal_palette[16];
-
-/**
- * All available terminal operations which affect character cells.
- */
-typedef enum guac_terminal_operation_type {
-
-    /**
-     * Operation which does nothing.
-     */
-    GUAC_CHAR_NOP,
-
-    /**
-     * Operation which copies a character from a given row/column coordinate.
-     */
-    GUAC_CHAR_COPY,
-
-    /**
-     * Operation which sets the character and attributes.
-     */
-    GUAC_CHAR_SET
-
-} guac_terminal_operation_type;
-
-/**
- * A pairing of a guac_terminal_operation_type and all parameters required by
- * that operation type.
- */
-typedef struct guac_terminal_operation {
-
-    /**
-     * The type of operation to perform.
-     */
-    guac_terminal_operation_type type;
-
-    /**
-     * The character (and attributes) to set the current location to. This is
-     * only applicable to GUAC_CHAR_SET.
-     */
-    guac_terminal_char character;
-
-    /**
-     * The row to copy a character from. This is only applicable to
-     * GUAC_CHAR_COPY.
-     */
-    int row;
-
-    /**
-     * The column to copy a character from. This is only applicable to
-     * GUAC_CHAR_COPY.
-     */
-    int column;
-
-} guac_terminal_operation;
-
-/**
- * Set of all pending operations for the currently-visible screen area.
- */
-typedef struct guac_terminal_display {
-
-    /**
-     * The Guacamole client this display will use for rendering.
-     */
-    guac_client* client;
-
-    /**
-     * Array of all operations pending for the visible screen area.
-     */
-    guac_terminal_operation* operations;
-
-    /**
-     * The width of the screen, in characters.
-     */
-    int width;
-
-    /**
-     * The height of the screen, in characters.
-     */
-    int height;
-
-    /**
-     * The description of the font to use for rendering.
-     */
-    PangoFontDescription* font_desc;
-
-    /**
-     * The width of each character, in pixels.
-     */
-    int char_width;
-
-    /**
-     * The height of each character, in pixels.
-     */
-    int char_height;
-
-    /**
-     * Default foreground color for all glyphs.
-     */
-    int default_foreground;
-
-    /**
-     * Default background color for all glyphs and the terminal itself.
-     */
-    int default_background;
-
-    /**
-     * Color of glyphs in copy buffer
-     */
-    int glyph_foreground;
-
-    /**
-     * Color of glyphs in copy buffer
-     */
-    int glyph_background;
-
-    /**
-     * The surface containing the actual terminal.
-     */
-    guac_common_surface* display_surface;
-
-    /**
-     * Layer which contains the actual terminal.
-     */
-    guac_layer* display_layer;
-
-    /**
-     * Sub-layer of display layer which highlights selected text.
-     */
-    guac_layer* select_layer;
-
-    /**
-     * Whether text is being selected.
-     */
-    bool text_selected;
-
-    /**
-     * Whether the selection is finished, and will no longer be modified. A
-     * committed selection remains highlighted for reference, but the
-     * highlight will be removed when the display changes.
-     */
-    bool selection_committed;
-
-    /**
-     * The row that the selection starts at.
-     */
-    int selection_start_row;
-
-    /**
-     * The column that the selection starts at.
-     */
-    int selection_start_column;
-
-    /**
-     * The row that the selection ends at.
-     */
-    int selection_end_row;
-
-    /**
-     * The column that the selection ends at.
-     */
-    int selection_end_column;
-
-} guac_terminal_display;
-
-/**
- * Allocates a new display having the given default foreground and background
- * colors.
- */
-guac_terminal_display* guac_terminal_display_alloc(guac_client* client,
-        const char* font_name, int font_size, int dpi,
-        int foreground, int background);
-
-/**
- * Frees the given display.
- */
-void guac_terminal_display_free(guac_terminal_display* display);
-
-/**
- * Copies the given range of columns to a new location, offset from
- * the original by the given number of columns.
- */
-void guac_terminal_display_copy_columns(guac_terminal_display* display, int row,
-        int start_column, int end_column, int offset);
-
-/**
- * Copies the given range of rows to a new location, offset from the
- * original by the given number of rows.
- */
-void guac_terminal_display_copy_rows(guac_terminal_display* display,
-        int start_row, int end_row, int offset);
-
-/**
- * Sets the given range of columns within the given row to the given
- * character.
- */
-void guac_terminal_display_set_columns(guac_terminal_display* display, int row,
-        int start_column, int end_column, guac_terminal_char* character);
-
-/**
- * Resize the terminal to the given dimensions.
- */
-void guac_terminal_display_resize(guac_terminal_display* display, int width, int height);
-
-/**
- * Flushes all pending operations within the given guac_terminal_display.
- */
-void guac_terminal_display_flush(guac_terminal_display* display);
-
-/**
- * Initializes and syncs the current terminal display state for the given user
- * that has just joined the connection, sending the necessary instructions to
- * completely recreate and redraw the terminal rendering over the given socket.
- *
- * @param display
- *     The terminal display to sync to the given user.
- *
- * @param user
- *     The user that has just joined the connection.
- *
- * @param socket
- *     The socket over which any necessary instructions should be sent.
- */
-void guac_terminal_display_dup(guac_terminal_display* display, guac_user* user,
-        guac_socket* socket);
-
-/**
- * Draws the text selection rectangle from the given coordinates to the given end coordinates.
- */
-void guac_terminal_display_select(guac_terminal_display* display,
-        int start_row, int start_col, int end_row, int end_col);
-
-/**
- * Commits the select rectangle, allowing the display to clear it when
- * necessary.
- */
-void guac_terminal_display_commit_select(guac_terminal_display* display);
-
-#endif
-

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/scrollbar.c
----------------------------------------------------------------------
diff --git a/src/terminal/scrollbar.c b/src/terminal/scrollbar.c
index f8cd99d..ddff299 100644
--- a/src/terminal/scrollbar.c
+++ b/src/terminal/scrollbar.c
@@ -18,7 +18,7 @@
  */
 
 #include "config.h"
-#include "scrollbar.h"
+#include "terminal/scrollbar.h"
 
 #include <guacamole/client.h>
 #include <guacamole/layer.h>

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/scrollbar.h
----------------------------------------------------------------------
diff --git a/src/terminal/scrollbar.h b/src/terminal/scrollbar.h
deleted file mode 100644
index bf0da87..0000000
--- a/src/terminal/scrollbar.h
+++ /dev/null
@@ -1,357 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef GUAC_TERMINAL_SCROLLBAR_H
-#define GUAC_TERMINAL_SCROLLBAR_H
-
-#include "config.h"
-
-#include <guacamole/client.h>
-#include <guacamole/layer.h>
-
-/**
- * The width of the scrollbar, in pixels.
- */
-#define GUAC_TERMINAL_SCROLLBAR_WIDTH 16 
-
-/**
- * The number of pixels between the draggable handle of the scrollbar and the
- * boundary of the containing layer.
- */
-#define GUAC_TERMINAL_SCROLLBAR_PADDING 2
-
-/**
- * The minimum height of the draggable handle of the scrollbar, in pixels.
- */
-#define GUAC_TERMINAL_SCROLLBAR_MIN_HEIGHT 64
-
-/**
- * The state of all scrollbar components, describing all variable aspects of
- * the scrollbar's appearance.
- */
-typedef struct guac_terminal_scrollbar_render_state {
-
-    /**
-     * The current X-coordinate of the upper-left corner of the scrollbar's
-     * handle. This value will be relative to the scrollbar's containing layer.
-     */
-    int handle_x;
-
-    /**
-     * The current Y-coordinate of the upper-left corner of the scrollbar's
-     * handle. This value will be relative to the scrollbar's containing layer.
-     */
-    int handle_y;
-
-    /**
-     * The width of the scrollbar's handle.
-     */
-    int handle_width;
-
-    /**
-     * The height of the scrollbar's handle.
-     */
-    int handle_height;
-
-    /**
-     * The current X-coordinate of the upper-left corner of the scrollbar's
-     * containing layer.
-     */
-    int container_x;
-
-    /**
-     * The current Y-coordinate of the upper-left corner of the scrollbar's
-     * containing layer.
-     */
-    int container_y;
-
-    /**
-     * The width of the scrollbar's containing layer.
-     */
-    int container_width;
-
-    /**
-     * The height of the scrollbar's containing layer.
-     */
-    int container_height;
-
-} guac_terminal_scrollbar_render_state;
-
-typedef struct guac_terminal_scrollbar guac_terminal_scrollbar;
-
-/**
- * Handler which is called whenever the scrollbar value changes outside a call
- * to guac_terminal_scrollbar_set_value().
- */
-typedef void guac_terminal_scrollbar_scroll_handler(
-        guac_terminal_scrollbar* scrollbar, int value);
-
-/**
- * A scrollbar, made up of a containing layer and inner draggable handle. The
- * position of the handle within the layer represents the value of the
- * scrollbar.
- */
-struct guac_terminal_scrollbar {
-
-    /**
-     * The client associated with this scrollbar.
-     */
-    guac_client* client;
-
-    /**
-     * The layer containing the scrollbar.
-     */
-    const guac_layer* parent;
-
-    /**
-     * The width of the parent layer, in pixels.
-     */
-    int parent_width;
-
-    /**
-     * The height of the parent layer, in pixels.
-     */
-    int parent_height;
-
-    /**
-     * The scrollbar itself.
-     */
-    guac_layer* container;
-
-    /**
-     * The draggable handle within the scrollbar, representing the current
-     * scroll value.
-     */
-    guac_layer* handle;
-
-    /**
-     * The minimum scroll value.
-     */
-    int min;
-
-    /**
-     * The maximum scroll value.
-     */
-    int max;
-
-    /**
-     * The size of the visible area, in the same units as min and max.
-     */
-    int visible_area;
-
-    /**
-     * The current scroll value.
-     */
-    int value;
-
-    /**
-     * The current state of all variable, visible parts of the scrollbar.
-     */
-    guac_terminal_scrollbar_render_state render_state;
-
-    /**
-     * Whether the scrollbar handle is currently being dragged.
-     */
-    int dragging_handle;
-
-    /**
-     * The offset of the Y location of the mouse pointer when the dragging
-     * began, relative to the top of the scrollbar handle. If dragging is not
-     * in progress, this value is undefined.
-     */
-    int drag_offset_y;
-
-    /**
-     * The current Y location of the mouse pointer if dragging is in progress.
-     * If dragging is not in progress, this value is undefined.
-     */
-    int drag_current_y;
-
-    /**
-     * The function to call when the scrollbar handle is being dragged, and
-     * the new scrollbar value needs to be handled and assigned.
-     */
-    guac_terminal_scrollbar_scroll_handler* scroll_handler;
-
-    /**
-     * Arbitrary reference to data related to this scrollbar.
-     */
-    void* data;
-
-};
-
-/**
- * Allocates a new scrollbar, associating that scrollbar with the given client
- * and parent layer. The dimensions of the parent layer dictate the initial
- * position of the scrollbar. Currently, the scrollbar is always anchored to
- * the right edge of the parent layer.
- *
- * This will cause instructions to be written to the client's socket, but the
- * client's socket will not be automatically flushed.
- *
- * @param client
- *     The client to associate with the new scrollbar.
- *
- * @param parent
- *     The layer which will contain the newly-allocated scrollbar.
- *
- * @param parent_width
- *     The width of the parent layer, in pixels.
- *
- * @param parent_height
- *     The height of the parent layer, in pixels.
- *
- * @param visible_area
- *     The amount of scrollable data that can be shown within the parent layer
- *     at any given time. This value uses the same units as min, max, and the
- *     current scroll value.
- *
- * @return
- *     A newly allocated scrollbar.
- */
-guac_terminal_scrollbar* guac_terminal_scrollbar_alloc(guac_client* client,
-        const guac_layer* parent, int parent_width, int parent_height,
-        int visible_area);
-
-/**
- * Frees the given scrollbar.
- *
- * @param scrollbar
- *     The scrollbar to free.
- */
-void guac_terminal_scrollbar_free(guac_terminal_scrollbar* scrollbar);
-
-/**
- * Flushes the render state of the given scrollbar, updating the remote display
- * accordingly.
- *
- * This may cause instructions to be written to the client's socket, but the
- * client's socket will not be automatically flushed.
- *
- * @param scrollbar
- *     The scrollbar whose render state is to be flushed.
- */
-void guac_terminal_scrollbar_flush(guac_terminal_scrollbar* scrollbar);
-
-/**
- * Forces a complete redraw / resync of scrollbar state for the given user that
- * has just joined the connection, sending the necessary instructions to
- * completely recreate and redraw the scrollbar rendering over the given
- * socket.
- *
- * @param scrollbar
- *     The scrollbar to sync to the given user.
- *
- * @param user
- *     The user that has just joined the connection.
- *
- * @param socket
- *     The socket over which any necessary instructions should be sent.
- */
-void guac_terminal_scrollbar_dup(guac_terminal_scrollbar* scrollbar,
-        guac_user* user, guac_socket* socket);
-
-/**
- * Sets the minimum and maximum allowed scroll values of the given scrollbar
- * to the given values. If necessary, the current value of the scrollbar will
- * be adjusted to fit within the new bounds.
- *
- * This may cause instructions to be written to the client's socket, but the
- * client's socket will not be automatically flushed.
- *
- * @param scrollbar
- *     The scrollbar whose bounds are changing.
- *
- * @param min
- *     The new minimum value of the scrollbar.
- *
- * @param max
- *     The new maximum value of the scrollbar.
- */
-void guac_terminal_scrollbar_set_bounds(guac_terminal_scrollbar* scrollbar,
-        int min, int max);
-
-/**
- * Sets the current value of the given scrollbar. If the value specified does
- * not fall within the scrollbar's defined minimum and maximum values, the
- * value will be adjusted to fit.
- *
- * This may cause instructions to be written to the client's socket, but the
- * client's socket will not be automatically flushed.
- *
- * @param scrollbar
- *     The scrollbar whose value is changing.
- *
- * @param value
- *     The value to assign to the scrollbar. If the value if out of bounds, it
- *     will be automatically adjusted to fit.
- */
-void guac_terminal_scrollbar_set_value(guac_terminal_scrollbar* scrollbar,
-        int value);
-
-/**
- * Notifies the scrollbar that the parent layer has been resized, and that the
- * scrollbar may need to be repositioned or resized accordingly.
- *
- * This may cause instructions to be written to the client's socket, but the
- * client's socket will not be automatically flushed.
- *
- * @param scrollbar
- *     The scrollbar whose parent layer has been resized.
- *
- * @param parent_width
- *     The new width of the parent layer, in pixels.
- *
- * @param parent_height
- *     The new height of the parent layer, in pixels.
- *
- * @param visible_area
- *     The amount of scrollable data that can be shown within the parent layer
- *     at any given time. This value uses the same units as min, max, and the
- *     current scroll value.
- */
-void guac_terminal_scrollbar_parent_resized(guac_terminal_scrollbar* scrollbar,
-        int parent_width, int parent_height, int visible_area);
-
-/**
- * Notifies the scrollbar of the current mouse state, allowing it to update
- * itself with respect to button state and dragging.
- *
- * @param scrollbar
- *     The scrollbar to notify of the current mouse state.
- *
- * @param x
- *     The X coordinate of the mouse pointer.
- *
- * @param y
- *     The Y coordinate of the mouse pointer.
- *
- * @param mask
- *     The button mask, where the Nth bit of the button mask represents the
- *     pressed state of the Nth mouse button, where button 0 is the left
- *     mouse button, button 1 is the middle button, etc.
- *
- * @return
- *     Zero if the mouse event was not handled by the scrollbar, non-zero
- *     otherwise.
- */
-int guac_terminal_scrollbar_handle_mouse(guac_terminal_scrollbar* scrollbar,
-        int x, int y, int mask);
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/terminal.c
----------------------------------------------------------------------
diff --git a/src/terminal/terminal.c b/src/terminal/terminal.c
index 5c32774..c5df648 100644
--- a/src/terminal/terminal.c
+++ b/src/terminal/terminal.c
@@ -19,15 +19,15 @@
 
 #include "config.h"
 
-#include "buffer.h"
-#include "common.h"
 #include "common/clipboard.h"
 #include "common/cursor.h"
-#include "display.h"
-#include "terminal.h"
-#include "terminal_handlers.h"
-#include "types.h"
-#include "typescript.h"
+#include "terminal/buffer.h"
+#include "terminal/common.h"
+#include "terminal/display.h"
+#include "terminal/terminal.h"
+#include "terminal/terminal_handlers.h"
+#include "terminal/types.h"
+#include "terminal/typescript.h"
 
 #include <errno.h>
 #include <pthread.h>

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/terminal.h
----------------------------------------------------------------------
diff --git a/src/terminal/terminal.h b/src/terminal/terminal.h
deleted file mode 100644
index f37e870..0000000
--- a/src/terminal/terminal.h
+++ /dev/null
@@ -1,839 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-#ifndef _GUAC_TERMINAL_H
-#define _GUAC_TERMINAL_H
-
-#include "config.h"
-
-#include "buffer.h"
-#include "common/clipboard.h"
-#include "common/cursor.h"
-#include "display.h"
-#include "scrollbar.h"
-#include "types.h"
-#include "typescript.h"
-
-#include <pthread.h>
-#include <stdbool.h>
-
-#include <guacamole/client.h>
-#include <guacamole/stream.h>
-
-/**
- * The maximum duration of a single frame, in milliseconds.
- */
-#define GUAC_TERMINAL_FRAME_DURATION 40
-
-/**
- * The maximum amount of time to wait for more data before declaring a frame
- * complete, in milliseconds.
- */
-#define GUAC_TERMINAL_FRAME_TIMEOUT 10
-
-/**
- * The maximum number of custom tab stops.
- */
-#define GUAC_TERMINAL_MAX_TABS       16
-
-/**
- * The number of rows to scroll per scroll wheel event.
- */
-#define GUAC_TERMINAL_WHEEL_SCROLL_AMOUNT 3
-
-/**
- * The maximum number of bytes to allow within the clipboard.
- */
-#define GUAC_TERMINAL_CLIPBOARD_MAX_LENGTH 262144
-
-/**
- * The name of the color scheme having black foreground and white background.
- */
-#define GUAC_TERMINAL_SCHEME_BLACK_WHITE "black-white"
-
-/**
- * The name of the color scheme having gray foreground and black background.
- */
-#define GUAC_TERMINAL_SCHEME_GRAY_BLACK "gray-black"
-
-/**
- * The name of the color scheme having green foreground and black background.
- */
-#define GUAC_TERMINAL_SCHEME_GREEN_BLACK "green-black"
-
-/**
- * The name of the color scheme having white foreground and black background.
- */
-#define GUAC_TERMINAL_SCHEME_WHITE_BLACK "white-black"
-
-typedef struct guac_terminal guac_terminal;
-
-/**
- * All possible mouse cursors used by the terminal emulator.
- */
-typedef enum guac_terminal_cursor_type {
-
-    /**
-     * A transparent (blank) cursor.
-     */
-    GUAC_TERMINAL_CURSOR_BLANK,
-
-    /**
-     * A standard I-bar cursor for selecting text, etc.
-     */
-    GUAC_TERMINAL_CURSOR_IBAR,
-
-    /**
-     * A standard triangular mouse pointer for manipulating non-text objects.
-     */
-    GUAC_TERMINAL_CURSOR_POINTER
-
-} guac_terminal_cursor_type;
-
-/**
- * Handler for characters printed to the terminal. When a character is printed,
- * the current char handler for the terminal is called and given that
- * character.
- */
-typedef int guac_terminal_char_handler(guac_terminal* term, unsigned char c);
-
-/**
- * Handler for setting the destination path for file uploads.
- */
-typedef void guac_terminal_upload_path_handler(guac_client* client, char* path);
-
-/**
- * Handler for creating an outbound file download stream for a specified file.
- */
-typedef guac_stream* guac_terminal_file_download_handler(guac_client* client, char* filename);
-
-/**
- * Represents a terminal emulator which uses a given Guacamole client to
- * render itself.
- */
-struct guac_terminal {
-
-    /**
-     * The Guacamole client associated with this terminal emulator.
-     */
-    guac_client* client;
-
-    /**
-     * The terminal render thread.
-     */
-    pthread_t thread;
-
-    /**
-     * Called whenever the necessary terminal codes are sent to change
-     * the path for future file uploads.
-     */
-    guac_terminal_upload_path_handler* upload_path_handler;
-
-    /**
-     * Called whenever the necessary terminal codes are sent to initiate
-     * a download of a given remote file.
-     */
-    guac_terminal_file_download_handler* file_download_handler;
-
-    /**
-     * Lock which restricts simultaneous access to this terminal via the root
-     * guac_terminal_* functions.
-     */
-    pthread_mutex_t lock;
-
-    /**
-     * The mutex associated with the modified condition and flag, locked
-     * whenever a thread is waiting on the modified condition, the modified
-     * condition is being signalled, or the modified flag is being changed.
-     */
-    pthread_mutex_t modified_lock;
-
-    /**
-     * Flag set whenever an operation has affected the terminal in a way that
-     * will require a frame flush. When this flag is set, the modified_cond
-     * condition will be signalled. The modified_lock will always be
-     * acquired before this flag is altered.
-     */
-    int modified;
-
-    /**
-     * Condition which is signalled when the modified flag has been set
-     */
-    pthread_cond_t modified_cond;
-
-    /**
-     * Pipe which will be the source of user input. When a terminal code
-     * generates synthesized user input, that data will be written to
-     * this pipe.
-     */
-    int stdin_pipe_fd[2];
-
-    /**
-     * The currently-open pipe stream to which all terminal output should be
-     * written, if any. If no pipe stream is open, terminal output will be
-     * written to the terminal display, and this value will be NULL.
-     */
-    guac_stream* pipe_stream;
-
-    /**
-     * Buffer of data pending write to the pipe_stream. Data within this buffer
-     * will be flushed to the pipe_stream when either (1) the buffer is full
-     * and another character needs to be written or (2) the pipe_stream is
-     * closed.
-     */
-    char pipe_buffer[6048];
-
-    /**
-     * The number of bytes currently stored within the pipe_buffer.
-     */
-    int pipe_buffer_length;
-
-    /**
-     * The currently-active typescript recording all terminal output, or NULL
-     * if no typescript is being used for the terminal session.
-     */
-    guac_terminal_typescript* typescript;
-
-    /**
-     * Terminal-wide mouse cursor, synchronized across all users.
-     */
-    guac_common_cursor* cursor;
-
-    /**
-     * Graphical representation of the current scroll state.
-     */
-    guac_terminal_scrollbar* scrollbar;
-
-    /**
-     * The relative offset of the display. A positive value indicates that
-     * many rows have been scrolled into view, zero indicates that no
-     * scrolling has occurred. Negative values are illegal.
-     */
-    int scroll_offset;
-
-    /**
-     * The width of the terminal, in pixels.
-     */
-    int width;
-
-    /**
-     * The height of the terminal, in pixels.
-     */
-    int height;
-
-    /**
-     * The width of the terminal, in characters.
-     */
-    int term_width;
-
-    /**
-     * The height of the terminal, in characters.
-     */
-    int term_height;
-
-    /**
-     * The index of the first row in the scrolling region.
-     */
-    int scroll_start;
-
-    /**
-     * The index of the last row in the scrolling region.
-     */
-    int scroll_end;
-
-    /**
-     * The current row location of the cursor. Note that while most terminal
-     * operations will clip the cursor location within the bounds of the
-     * terminal, this is not guaranteed.
-     */
-    int cursor_row;
-
-    /**
-     * The current column location of the cursor. Note that while most
-     * terminal operations will clip the cursor location within the bounds
-     * of the terminal, this is not guaranteed. There are times when the
-     * cursor is legitimately outside the terminal bounds (such as when the
-     * end of a line is reached, but it is not yet necessary to scroll up).
-     */
-    int cursor_col;
-
-    /**
-     * The row of the rendered cursor.
-     */
-    int visible_cursor_row;
-
-    /**
-     * The column of the rendered cursor.
-     */
-    int visible_cursor_col;
-
-    /**
-     * The row of the saved cursor (ESC 7).
-     */
-    int saved_cursor_row;
-
-    /**
-     * The column of the saved cursor (ESC 7).
-     */
-    int saved_cursor_col;
-
-    /**
-     * The attributes which will be applied to future characters.
-     */
-    guac_terminal_attributes current_attributes;
-
-    /**
-     * The character whose attributes dictate the default attributes
-     * of all characters. When new screen space is allocated, this
-     * character fills the gaps.
-     */
-    guac_terminal_char default_char;
-
-    /**
-     * Handler which will receive all printed characters, updating the terminal
-     * accordingly.
-     */
-    guac_terminal_char_handler* char_handler;
-
-    /**
-     * The difference between the currently-rendered screen and the current
-     * state of the terminal.
-     */
-    guac_terminal_display* display;
-
-    /**
-     * Current terminal display state. All characters present on the screen
-     * are within this buffer. This has nothing to do with the display, which
-     * facilitates transfer of a set of changes to the remote display.
-     */
-    guac_terminal_buffer* buffer;
-
-    /**
-     * Automatically place a tabstop every N characters. If zero, then no
-     * tabstops exist automatically.
-     */
-    int tab_interval;
-
-    /**
-     * Array of all tabs set. Each entry is the column number of a tab + 1,
-     * or 0 if that tab cell is unset.
-     */
-    int custom_tabs[GUAC_TERMINAL_MAX_TABS];
-
-    /**
-     * Array of arrays of mapped characters, where the character N is located at the N-32
-     * position within the array. Each element in a contained array is the corresponding Unicode
-     * codepoint. If NULL, a direct mapping from Unicode is used. The entries of the main array
-     * correspond to the character set in use (G0, G1, etc.)
-     */
-    const int* char_mapping[2];
-
-    /**
-     * The active character set. For example, 0 for G0, 1 for G1, etc.
-     */
-    int active_char_set;
-
-    /**
-     * Whether text is being selected.
-     */
-    bool text_selected;
-
-    /**
-     * The row that the selection starts at.
-     */
-    int selection_start_row;
-
-    /**
-     * The column that the selection starts at.
-     */
-    int selection_start_column;
-
-    /**
-     * The width of the character at selection start.
-     */
-    int selection_start_width;
-
-    /**
-     * The row that the selection ends at.
-     */
-    int selection_end_row;
-
-    /**
-     * The column that the selection ends at.
-     */
-    int selection_end_column;
-
-    /**
-     * The width of the character at selection end.
-     */
-    int selection_end_width;
-
-    /**
-     * Whether the cursor (arrow) keys should send cursor sequences
-     * or application sequences (DECCKM).
-     */
-    bool application_cursor_keys;
-
-    /**
-     * Whether a CR should automatically follow a LF, VT, or FF.
-     */
-    bool automatic_carriage_return;
-
-    /**
-     * Whether insert mode is enabled (DECIM).
-     */
-    bool insert_mode;
-
-    /**
-     * Whether the alt key is currently being held down.
-     */
-    int mod_alt;
-
-    /**
-     * Whether the control key is currently being held down.
-     */
-    int mod_ctrl;
-
-    /**
-     * Whether the shift key is currently being held down.
-     */
-    int mod_shift;
-
-    /**
-     * The current mouse button state.
-     */
-    int mouse_mask;
-
-    /**
-     * The current mouse cursor, to avoid re-setting the cursor image.
-     */
-    guac_terminal_cursor_type current_cursor;
-
-    /**
-     * The current contents of the clipboard.
-     */
-    guac_common_clipboard* clipboard;
-
-};
-
-/**
- * Creates a new guac_terminal, having the given width and height, and
- * rendering to the given client.
- *
- * @param client
- *     The client to which the terminal will be rendered.
- *
- * @param font_name
- *     The name of the font to use when rendering glyphs.
- *
- * @param font_size
- *     The size of each glyph, in points.
- *
- * @param dpi
- *     The DPI of the display. The given font size will be adjusted to produce
- *     glyphs at the given DPI.
- *
- * @param width
- *     The width of the terminal, in pixels.
- *
- * @param height
- *     The height of the terminal, in pixels.
- *
- * @param color_scheme
- *     The name of the color scheme to use. This string must be one of the
- *     names defined by the GUAC_TERMINAL_SCHEME_* constants. If blank or NULL,
- *     the default scheme of GUAC_TERMINAL_SCHEME_GRAY_BLACK will be used. If
- *     invalid, a warning will be logged, and the terminal will fall back on
- *     GUAC_TERMINAL_SCHEME_GRAY_BLACK.
- *
- * @return
- *     A new guac_terminal having the given font, dimensions, and attributes
- *     which renders all text to the given client.
- */
-guac_terminal* guac_terminal_create(guac_client* client,
-        const char* font_name, int font_size, int dpi,
-        int width, int height, const char* color_scheme);
-
-/**
- * Frees all resources associated with the given terminal.
- */
-void guac_terminal_free(guac_terminal* term);
-
-/**
- * Renders a single frame of terminal data. If data is not yet available,
- * this function will block until data is written.
- */
-int guac_terminal_render_frame(guac_terminal* terminal);
-
-/**
- * Reads from this terminal's STDIN. Input comes from key and mouse events
- * supplied by calls to guac_terminal_send_key() and
- * guac_terminal_send_mouse(). If input is not yet available, this function
- * will block.
- */
-int guac_terminal_read_stdin(guac_terminal* terminal, char* c, int size);
-
-/**
- * Notifies the terminal that an event has occurred and the terminal should
- * flush itself when reasonable.
- *
- * @param terminal
- *     The terminal to notify.
- */
-void guac_terminal_notify(guac_terminal* terminal);
-
-/**
- * Reads a single line from this terminal's STDIN, storing the result in a
- * newly-allocated string. Input is retrieved in the same manner as
- * guac_terminal_read_stdin() and the same restrictions apply.
- *
- * @param terminal
- *     The terminal to which the provided title should be output, and from
- *     whose STDIN the single line of input should be read.
- *
- * @param title
- *     The human-readable title to output to the terminal just prior to reading
- *     from STDIN.
- *
- * @param echo
- *     Non-zero if the characters read from STDIN should be echoed back as
- *     terminal output, or zero if asterisks should be displayed instead.
- *
- * @return
- *     A newly-allocated string containing a single line of input read from the
- *     provided terminal's STDIN. This string must eventually be manually
- *     freed with a call to free().
- */
-char* guac_terminal_prompt(guac_terminal* terminal, const char* title,
-        bool echo);
-
-/**
- * Writes the given format string and arguments to this terminal's STDOUT in
- * the same manner as printf(). This function may block until space is
- * freed in the output buffer by guac_terminal_render_frame().
- */
-int guac_terminal_printf(guac_terminal* terminal, const char* format, ...);
-
-/**
- * Handles the given key event, sending data, scrolling, pasting clipboard
- * data, etc. as necessary.
- */
-int guac_terminal_send_key(guac_terminal* term, int keysym, int pressed);
-
-/**
- * Handles the given mouse event, sending data, scrolling, pasting clipboard
- * data, etc. as necessary.
- */
-int guac_terminal_send_mouse(guac_terminal* term, guac_user* user,
-        int x, int y, int mask);
-
-/**
- * Handles a scroll event received from the scrollbar associated with a
- * terminal.
- *
- * @param scrollbar
- *     The scrollbar that has been scrolled.
- *
- * @param value
- *     The new value that should be stored within the scrollbar, and
- *     represented within the terminal display.
- */
-void guac_terminal_scroll_handler(guac_terminal_scrollbar* scrollbar, int value);
-
-/**
- * Clears the current clipboard contents and sets the mimetype for future
- * contents.
- */
-void guac_terminal_clipboard_reset(guac_terminal* term, const char* mimetype);
-
-/**
- * Appends the given data to the current clipboard.
- */
-void guac_terminal_clipboard_append(guac_terminal* term, const void* data, int length);
-
-/**
- * Replicates the current display state to a user that has just joined the
- * connection. All instructions necessary to replicate state are sent over the
- * given socket.
- *
- * @param term
- *     The terminal emulator associated with the connection being joined.
- *
- * @param user
- *     The user joining the connection.
- *
- * @param socket
- *     The guac_socket specific to the joining user and across which messages
- *     synchronizing the current display state should be sent.
- */
-void guac_terminal_dup(guac_terminal* term, guac_user* user,
-        guac_socket* socket);
-
-/* INTERNAL FUNCTIONS */
-
-
-/**
- * Acquires exclusive access to the terminal. Note that enforcing this
- * exclusive access requires that ALL users of the terminal call this
- * function before making further calls to the terminal.
- */
-void guac_terminal_lock(guac_terminal* terminal);
-
-/**
- * Releases exclusive access to the terminal.
- */
-void guac_terminal_unlock(guac_terminal* terminal);
-
-/**
- * Resets the state of the given terminal, as if it were just allocated.
- */
-void guac_terminal_reset(guac_terminal* term);
-
-/**
- * Writes the given string of characters to the terminal.
- */
-int guac_terminal_write(guac_terminal* term, const char* c, int size);
-
-/**
- * Sets the character at the given row and column to the specified value.
- */
-int guac_terminal_set(guac_terminal* term, int row, int col, int codepoint);
-
-/**
- * Clears the given region within a single row.
- */
-int guac_terminal_clear_columns(guac_terminal* term,
-        int row, int start_col, int end_col);
-
-/**
- * Clears the given region from right-to-left, top-to-bottom, replacing
- * all characters with the current background color and attributes.
- */
-int guac_terminal_clear_range(guac_terminal* term,
-        int start_row, int start_col,
-        int end_row, int end_col);
-
-/**
- * Scrolls the terminal's current scroll region up by one row.
- */
-int guac_terminal_scroll_up(guac_terminal* term,
-        int start_row, int end_row, int amount);
-
-/**
- * Scrolls the terminal's current scroll region down by one row.
- */
-int guac_terminal_scroll_down(guac_terminal* term,
-        int start_row, int end_row, int amount);
-
-/**
- * Commits the current cursor location, updating the visible cursor
- * on the screen.
- */
-void guac_terminal_commit_cursor(guac_terminal* term);
-
-/**
- * Scroll down the display by the given amount, replacing the new space with
- * data from the buffer. If not enough data is available, the maximum
- * amount will be scrolled.
- */
-void guac_terminal_scroll_display_down(guac_terminal* terminal, int amount);
-
-/**
- * Scroll up the display by the given amount, replacing the new space with data
- * from either the buffer or the terminal buffer.  If not enough data is
- * available, the maximum amount will be scrolled.
- */
-void guac_terminal_scroll_display_up(guac_terminal* terminal, int amount);
-
-/**
- * Marks the start of text selection at the given row and column.
- */
-void guac_terminal_select_start(guac_terminal* terminal, int row, int column);
-
-/**
- * Updates the end of text selection at the given row and column.
- */
-void guac_terminal_select_update(guac_terminal* terminal, int row, int column);
-
-/**
- * Ends text selection, removing any highlight. Character data is stored in the
- * string buffer provided.
- */
-void guac_terminal_select_end(guac_terminal* terminal, char* string);
-
-
-/* LOW-LEVEL TERMINAL OPERATIONS */
-
-
-/**
- * Copies the given range of columns to a new location, offset from
- * the original by the given number of columns.
- */
-void guac_terminal_copy_columns(guac_terminal* terminal, int row,
-        int start_column, int end_column, int offset);
-
-/**
- * Copies the given range of rows to a new location, offset from the
- * original by the given number of rows.
- */
-void guac_terminal_copy_rows(guac_terminal* terminal,
-        int start_row, int end_row, int offset);
-
-/**
- * Sets the given range of columns within the given row to the given
- * character.
- */
-void guac_terminal_set_columns(guac_terminal* terminal, int row,
-        int start_column, int end_column, guac_terminal_char* character);
-
-/**
- * Resize the terminal to the given dimensions.
- */
-int guac_terminal_resize(guac_terminal* term, int width, int height);
-
-/**
- * Flushes all pending operations within the given guac_terminal.
- */
-void guac_terminal_flush(guac_terminal* terminal);
-
-/**
- * Sends the given string as if typed by the user. 
- */
-int guac_terminal_send_data(guac_terminal* term, const char* data, int length);
-
-/**
- * Sends the given string as if typed by the user. 
- */
-int guac_terminal_send_string(guac_terminal* term, const char* data);
-
-/**
- * Sends data through STDIN as if typed by the user, using the format
- * string given and any args (similar to printf).
- */
-int guac_terminal_sendf(guac_terminal* term, const char* format, ...);
-
-/**
- * Sets a tabstop in the given column.
- */
-void guac_terminal_set_tab(guac_terminal* term, int column);
-
-/**
- * Removes the tabstop at the given column.
- */
-void guac_terminal_unset_tab(guac_terminal* term, int column);
-
-/**
- * Removes all tabstops.
- */
-void guac_terminal_clear_tabs(guac_terminal* term);
-
-/**
- * Given a column within the given terminal, returns the location of the
- * next tabstop (or the rightmost character, if no more tabstops exist).
- */
-int guac_terminal_next_tab(guac_terminal* term, int column);
-
-/**
- * Opens a new pipe stream, redirecting all output from the given terminal to
- * that pipe stream. If a pipe stream is already open, that pipe stream will
- * be flushed and closed prior to opening the new pipe stream.
- *
- * @param term
- *     The terminal which should redirect output to a new pipe stream having
- *     the given name.
- *
- * @param name
- *     The name of the pipe stream to open.
- */
-void guac_terminal_pipe_stream_open(guac_terminal* term, const char* name);
-
-/**
- * Writes a single byte of data to the pipe stream currently open and
- * associated with the given terminal. The pipe stream must already have been
- * opened via guac_terminal_pipe_stream_open(). If no pipe stream is currently
- * open, this function has no effect. Data written through this function may
- * be buffered.
- *
- * @param term
- *     The terminal whose currently-open pipe stream should be written to.
- *
- * @param c
- *     The byte of data to write to the pipe stream.
- */
-void guac_terminal_pipe_stream_write(guac_terminal* term, char c);
-
-/**
- * Flushes any data currently buffered for the currently-open pipe stream
- * associated with the given terminal. The pipe stream must already have been
- * opened via guac_terminal_pipe_stream_open(). If no pipe stream is currently
- * open or no data is in the buffer, this function has no effect.
- *
- * @param term
- *     The terminal whose pipe stream buffer should be flushed.
- */
-void guac_terminal_pipe_stream_flush(guac_terminal* term);
-
-/**
- * Closes the currently-open pipe stream associated with the given terminal,
- * redirecting all output back to the terminal display.  Any data currently
- * buffered for output to the pipe stream will be flushed prior to closure. The
- * pipe stream must already have been opened via
- * guac_terminal_pipe_stream_open(). If no pipe stream is currently open, this
- * function has no effect.
- *
- * @param term
- *     The terminal whose currently-open pipe stream should be closed.
- */
-void guac_terminal_pipe_stream_close(guac_terminal* term);
-
-/**
- * Requests that the terminal write all output to a new pair of typescript
- * files within the given path and using the given base name. Terminal output
- * will be written to these new files, along with timing information. If the
- * create_path flag is non-zero, the given path will be created if it does not
- * yet exist. If creation of the typescript files or path fails, error messages
- * will automatically be logged, and no typescript will be written. The
- * typescript will automatically be closed once the terminal is freed.
- *
- * @param term
- *     The terminal whose output should be written to a typescript.
- *
- * @param path
- *     The full absolute path to a directory in which the typescript files
- *     should be created.
- *
- * @param name
- *     The base name to use for the typescript files created within the
- *     specified path.
- *
- * @param create_path
- *     Zero if the specified path MUST exist for typescript files to be
- *     written, or non-zero if the path should be created if it does not yet
- *     exist.
- *
- * @return
- *     Zero if the typescript files have been successfully created and a
- *     typescript will be written, non-zero otherwise.
- */
-int guac_terminal_create_typescript(guac_terminal* term, const char* path,
-        const char* name, int create_path);
-
-#endif
-

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/terminal/buffer.h
----------------------------------------------------------------------
diff --git a/src/terminal/terminal/buffer.h b/src/terminal/terminal/buffer.h
new file mode 100644
index 0000000..db2a4a1
--- /dev/null
+++ b/src/terminal/terminal/buffer.h
@@ -0,0 +1,129 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+#ifndef _GUAC_TERMINAL_BUFFER_H
+#define _GUAC_TERMINAL_BUFFER_H
+
+#include "config.h"
+
+#include "types.h"
+
+/**
+ * A single variable-length row of terminal data.
+ */
+typedef struct guac_terminal_buffer_row {
+
+    /**
+     * Array of guac_terminal_char representing the contents of the row.
+     */
+    guac_terminal_char* characters;
+
+    /**
+     * The length of this row in characters. This is the number of initialized
+     * characters in the buffer, usually equal to the number of characters
+     * in the screen width at the time this row was created.
+     */
+    int length;
+
+    /**
+     * The number of elements in the characters array. After the length
+     * equals this value, the array must be resized.
+     */
+    int available;
+
+} guac_terminal_buffer_row;
+
+/**
+ * A buffer containing a constant number of arbitrary-length rows.
+ * New rows can be appended to the buffer, with the oldest row replaced with
+ * the new row.
+ */
+typedef struct guac_terminal_buffer {
+
+    /**
+     * The character to assign to newly-allocated cells.
+     */
+    guac_terminal_char default_character;
+
+    /**
+     * Array of buffer rows. This array functions as a ring buffer.
+     * When a new row needs to be appended, the top reference is moved down
+     * and the old top row is replaced.
+     */
+    guac_terminal_buffer_row* rows;
+
+    /**
+     * The row to replace when adding a new row to the buffer.
+     */
+    int top;
+
+    /**
+     * The number of rows currently stored in the buffer.
+     */
+    int length;
+
+    /**
+     * The number of rows in the buffer. This is the total capacity
+     * of the buffer.
+     */
+    int available;
+
+} guac_terminal_buffer;
+
+/**
+ * Allocates a new buffer having the given maximum number of rows. New character cells will
+ * be initialized to the given character.
+ */
+guac_terminal_buffer* guac_terminal_buffer_alloc(int rows, guac_terminal_char* default_character);
+
+/**
+ * Frees the given buffer.
+ */
+void guac_terminal_buffer_free(guac_terminal_buffer* buffer);
+
+/**
+ * Returns the row at the given location. The row returned is guaranteed to be at least the given
+ * width.
+ */
+guac_terminal_buffer_row* guac_terminal_buffer_get_row(guac_terminal_buffer* buffer, int row, int width);
+
+/**
+ * Copies the given range of columns to a new location, offset from
+ * the original by the given number of columns.
+ */
+void guac_terminal_buffer_copy_columns(guac_terminal_buffer* buffer, int row,
+        int start_column, int end_column, int offset);
+
+/**
+ * Copies the given range of rows to a new location, offset from the
+ * original by the given number of rows.
+ */
+void guac_terminal_buffer_copy_rows(guac_terminal_buffer* buffer,
+        int start_row, int end_row, int offset);
+
+/**
+ * Sets the given range of columns within the given row to the given
+ * character.
+ */
+void guac_terminal_buffer_set_columns(guac_terminal_buffer* buffer, int row,
+        int start_column, int end_column, guac_terminal_char* character);
+
+#endif
+

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/terminal/char_mappings.h
----------------------------------------------------------------------
diff --git a/src/terminal/terminal/char_mappings.h b/src/terminal/terminal/char_mappings.h
new file mode 100644
index 0000000..5e0e474
--- /dev/null
+++ b/src/terminal/terminal/char_mappings.h
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+#ifndef _GUAC_TERMINAL_CHAR_MAPPINGS_H
+#define _GUAC_TERMINAL_CHAR_MAPPINGS_H
+
+#include "config.h"
+
+/**
+ * VT100 graphics mapping. Each entry is the corresponding Unicode codepoint
+ * for the character N+32, where N is the index of the element in the array.
+ * All characters less than 32 are universally mapped to themselves.
+ */
+extern const int vt100_map[];
+
+/**
+ * Null graphics mapping. Each entry is the corresponding Unicode codepoint
+ * for the character N+32, where N is the index of the element in the array.
+ * All characters less than 32 are universally mapped to themselves.
+ */
+extern const int null_map[];
+
+/**
+ * User graphics mapping. Each entry is the corresponding Unicode codepoint
+ * for the character N+32, where N is the index of the element in the array.
+ * All characters less than 32 are universally mapped to themselves.
+ */
+extern const int user_map[];
+
+#endif
+

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/terminal/common.h
----------------------------------------------------------------------
diff --git a/src/terminal/terminal/common.h b/src/terminal/terminal/common.h
new file mode 100644
index 0000000..f7eda61
--- /dev/null
+++ b/src/terminal/terminal/common.h
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+#ifndef _GUAC_TERMINAL_COMMON_H
+#define _GUAC_TERMINAL_COMMON_H
+
+#include "config.h"
+
+#include <stdbool.h>
+
+/**
+ * Returns the closest value to the value given that is also
+ * within the given range.
+ */
+int guac_terminal_fit_to_range(int value, int min, int max);
+
+/**
+ * Encodes the given codepoint as UTF-8, storing the result within the
+ * provided buffer, and returning the number of bytes stored.
+ */
+int guac_terminal_encode_utf8(int codepoint, char* utf8);
+
+/**
+ * Returns whether a codepoint has a corresponding glyph, or is rendered
+ * as a blank space.
+ */
+bool guac_terminal_has_glyph(int codepoint);
+
+/**
+ * Similar to write, but automatically retries the write operation until
+ * an error occurs.
+ */
+int guac_terminal_write_all(int fd, const char* buffer, int size);
+
+#endif
+

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/3f7ccb6b/src/terminal/terminal/display.h
----------------------------------------------------------------------
diff --git a/src/terminal/terminal/display.h b/src/terminal/terminal/display.h
new file mode 100644
index 0000000..655d1aa
--- /dev/null
+++ b/src/terminal/terminal/display.h
@@ -0,0 +1,363 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+#ifndef _GUAC_TERMINAL_DISPLAY_H
+#define _GUAC_TERMINAL_DISPLAY_H
+
+#include "config.h"
+
+#include "common/surface.h"
+#include "types.h"
+
+#include <guacamole/client.h>
+#include <guacamole/layer.h>
+#include <pango/pangocairo.h>
+
+#include <stdbool.h>
+
+/**
+ * The maximum width of any character, in columns.
+ */
+#define GUAC_TERMINAL_MAX_CHAR_WIDTH 2
+
+/**
+ * The index of black within the terminal color palette.
+ */
+#define GUAC_TERMINAL_COLOR_BLACK 0
+
+/**
+ * The index of low-intensity red within the terminal color palette.
+ */
+#define GUAC_TERMINAL_COLOR_DARK_RED 1
+
+/**
+ * The index of low-intensity green within the terminal color palette.
+ */
+#define GUAC_TERMINAL_COLOR_DARK_GREEN 2
+
+/**
+ * The index of brown within the terminal color palette.
+ */
+#define GUAC_TERMINAL_COLOR_BROWN 3
+
+/**
+ * The index of low-intensity blue within the terminal color palette.
+ */
+#define GUAC_TERMINAL_COLOR_DARK_BLUE 4
+
+/**
+ * The index of low-intensity magenta (purple) within the terminal color
+ * palette.
+ */
+#define GUAC_TERMINAL_COLOR_PURPLE 5
+
+/**
+ * The index of low-intensity cyan (teal) within the terminal color palette.
+ */
+#define GUAC_TERMINAL_COLOR_TEAL 6
+
+/**
+ * The index of low-intensity white (gray) within the terminal color palette.
+ */
+#define GUAC_TERMINAL_COLOR_GRAY 7
+
+/**
+ * The index of bright black (dark gray) within the terminal color palette.
+ */
+#define GUAC_TERMINAL_COLOR_DARK_GRAY 8
+
+/**
+ * The index of bright red within the terminal color palette.
+ */
+#define GUAC_TERMINAL_COLOR_RED 9
+
+/**
+ * The index of bright green within the terminal color palette.
+ */
+#define GUAC_TERMINAL_COLOR_GREEN 10
+
+/**
+ * The index of bright brown (yellow) within the terminal color palette.
+ */
+#define GUAC_TERMINAL_COLOR_YELLOW 11
+
+/**
+ * The index of bright blue within the terminal color palette.
+ */
+#define GUAC_TERMINAL_COLOR_BLUE 12
+
+/**
+ * The index of bright magenta within the terminal color palette.
+ */
+#define GUAC_TERMINAL_COLOR_MAGENTA 13
+
+/**
+ * The index of bright cyan within the terminal color palette.
+ */
+#define GUAC_TERMINAL_COLOR_CYAN 14
+
+/**
+ * The index of bright white within the terminal color palette.
+ */
+#define GUAC_TERMINAL_COLOR_WHITE 15
+
+/**
+ * The available color palette. All integer colors within structures
+ * here are indices into this palette.
+ */
+extern const guac_terminal_color guac_terminal_palette[16];
+
+/**
+ * All available terminal operations which affect character cells.
+ */
+typedef enum guac_terminal_operation_type {
+
+    /**
+     * Operation which does nothing.
+     */
+    GUAC_CHAR_NOP,
+
+    /**
+     * Operation which copies a character from a given row/column coordinate.
+     */
+    GUAC_CHAR_COPY,
+
+    /**
+     * Operation which sets the character and attributes.
+     */
+    GUAC_CHAR_SET
+
+} guac_terminal_operation_type;
+
+/**
+ * A pairing of a guac_terminal_operation_type and all parameters required by
+ * that operation type.
+ */
+typedef struct guac_terminal_operation {
+
+    /**
+     * The type of operation to perform.
+     */
+    guac_terminal_operation_type type;
+
+    /**
+     * The character (and attributes) to set the current location to. This is
+     * only applicable to GUAC_CHAR_SET.
+     */
+    guac_terminal_char character;
+
+    /**
+     * The row to copy a character from. This is only applicable to
+     * GUAC_CHAR_COPY.
+     */
+    int row;
+
+    /**
+     * The column to copy a character from. This is only applicable to
+     * GUAC_CHAR_COPY.
+     */
+    int column;
+
+} guac_terminal_operation;
+
+/**
+ * Set of all pending operations for the currently-visible screen area.
+ */
+typedef struct guac_terminal_display {
+
+    /**
+     * The Guacamole client this display will use for rendering.
+     */
+    guac_client* client;
+
+    /**
+     * Array of all operations pending for the visible screen area.
+     */
+    guac_terminal_operation* operations;
+
+    /**
+     * The width of the screen, in characters.
+     */
+    int width;
+
+    /**
+     * The height of the screen, in characters.
+     */
+    int height;
+
+    /**
+     * The description of the font to use for rendering.
+     */
+    PangoFontDescription* font_desc;
+
+    /**
+     * The width of each character, in pixels.
+     */
+    int char_width;
+
+    /**
+     * The height of each character, in pixels.
+     */
+    int char_height;
+
+    /**
+     * Default foreground color for all glyphs.
+     */
+    int default_foreground;
+
+    /**
+     * Default background color for all glyphs and the terminal itself.
+     */
+    int default_background;
+
+    /**
+     * Color of glyphs in copy buffer
+     */
+    int glyph_foreground;
+
+    /**
+     * Color of glyphs in copy buffer
+     */
+    int glyph_background;
+
+    /**
+     * The surface containing the actual terminal.
+     */
+    guac_common_surface* display_surface;
+
+    /**
+     * Layer which contains the actual terminal.
+     */
+    guac_layer* display_layer;
+
+    /**
+     * Sub-layer of display layer which highlights selected text.
+     */
+    guac_layer* select_layer;
+
+    /**
+     * Whether text is being selected.
+     */
+    bool text_selected;
+
+    /**
+     * Whether the selection is finished, and will no longer be modified. A
+     * committed selection remains highlighted for reference, but the
+     * highlight will be removed when the display changes.
+     */
+    bool selection_committed;
+
+    /**
+     * The row that the selection starts at.
+     */
+    int selection_start_row;
+
+    /**
+     * The column that the selection starts at.
+     */
+    int selection_start_column;
+
+    /**
+     * The row that the selection ends at.
+     */
+    int selection_end_row;
+
+    /**
+     * The column that the selection ends at.
+     */
+    int selection_end_column;
+
+} guac_terminal_display;
+
+/**
+ * Allocates a new display having the given default foreground and background
+ * colors.
+ */
+guac_terminal_display* guac_terminal_display_alloc(guac_client* client,
+        const char* font_name, int font_size, int dpi,
+        int foreground, int background);
+
+/**
+ * Frees the given display.
+ */
+void guac_terminal_display_free(guac_terminal_display* display);
+
+/**
+ * Copies the given range of columns to a new location, offset from
+ * the original by the given number of columns.
+ */
+void guac_terminal_display_copy_columns(guac_terminal_display* display, int row,
+        int start_column, int end_column, int offset);
+
+/**
+ * Copies the given range of rows to a new location, offset from the
+ * original by the given number of rows.
+ */
+void guac_terminal_display_copy_rows(guac_terminal_display* display,
+        int start_row, int end_row, int offset);
+
+/**
+ * Sets the given range of columns within the given row to the given
+ * character.
+ */
+void guac_terminal_display_set_columns(guac_terminal_display* display, int row,
+        int start_column, int end_column, guac_terminal_char* character);
+
+/**
+ * Resize the terminal to the given dimensions.
+ */
+void guac_terminal_display_resize(guac_terminal_display* display, int width, int height);
+
+/**
+ * Flushes all pending operations within the given guac_terminal_display.
+ */
+void guac_terminal_display_flush(guac_terminal_display* display);
+
+/**
+ * Initializes and syncs the current terminal display state for the given user
+ * that has just joined the connection, sending the necessary instructions to
+ * completely recreate and redraw the terminal rendering over the given socket.
+ *
+ * @param display
+ *     The terminal display to sync to the given user.
+ *
+ * @param user
+ *     The user that has just joined the connection.
+ *
+ * @param socket
+ *     The socket over which any necessary instructions should be sent.
+ */
+void guac_terminal_display_dup(guac_terminal_display* display, guac_user* user,
+        guac_socket* socket);
+
+/**
+ * Draws the text selection rectangle from the given coordinates to the given end coordinates.
+ */
+void guac_terminal_display_select(guac_terminal_display* display,
+        int start_row, int start_col, int end_row, int end_col);
+
+/**
+ * Commits the select rectangle, allowing the display to clear it when
+ * necessary.
+ */
+void guac_terminal_display_commit_select(guac_terminal_display* display);
+
+#endif
+


[6/9] incubator-guacamole-server git commit: GUACAMOLE-225: Bump libtool version info (interface added, none changed/removed).

Posted by jm...@apache.org.
GUACAMOLE-225: Bump libtool version info (interface added, none changed/removed).


Project: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/commit/f85978e4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/tree/f85978e4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/diff/f85978e4

Branch: refs/heads/master
Commit: f85978e43a631d2ac5a5b939e30a9edacc7a3949
Parents: e27996b
Author: Michael Jumper <mj...@apache.org>
Authored: Tue Feb 28 09:43:02 2017 -0800
Committer: Michael Jumper <mj...@apache.org>
Committed: Tue Feb 28 09:47:53 2017 -0800

----------------------------------------------------------------------
 src/libguac/Makefile.am | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/f85978e4/src/libguac/Makefile.am
----------------------------------------------------------------------
diff --git a/src/libguac/Makefile.am b/src/libguac/Makefile.am
index 781f862..787dfe7 100644
--- a/src/libguac/Makefile.am
+++ b/src/libguac/Makefile.am
@@ -104,7 +104,7 @@ libguac_la_CFLAGS = \
     -Werror -Wall -pedantic -Iguacamole
 
 libguac_la_LDFLAGS =     \
-    -version-info 12:1:0 \
+    -version-info 13:0:1 \
     @CAIRO_LIBS@         \
     @JPEG_LIBS@          \
     @PNG_LIBS@           \


[8/9] incubator-guacamole-server git commit: GUACAMOLE-169: Merge namespaced directory reshuffle.

Posted by jm...@apache.org.
GUACAMOLE-169: Merge namespaced directory reshuffle.


Project: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/commit/3ff83231
Tree: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/tree/3ff83231
Diff: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/diff/3ff83231

Branch: refs/heads/master
Commit: 3ff83231534908c744e46744670d3060e2f8240c
Parents: 08ce433 3f7ccb6
Author: James Muehlner <ja...@guac-dev.org>
Authored: Tue Feb 28 10:28:45 2017 -0800
Committer: James Muehlner <ja...@guac-dev.org>
Committed: Tue Feb 28 10:28:45 2017 -0800

----------------------------------------------------------------------
 src/common-ssh/Makefile.am                |  24 +-
 src/common-ssh/buffer.c                   | 135 ++++
 src/common-ssh/common-ssh/buffer.h        | 134 ++++
 src/common-ssh/common-ssh/key.h           | 170 +++++
 src/common-ssh/common-ssh/sftp.h          | 242 +++++++
 src/common-ssh/common-ssh/ssh.h           | 114 ++++
 src/common-ssh/common-ssh/user.h          | 108 ++++
 src/common-ssh/guac_sftp.c                | 783 -----------------------
 src/common-ssh/guac_sftp.h                | 242 -------
 src/common-ssh/guac_ssh.c                 | 544 ----------------
 src/common-ssh/guac_ssh.h                 | 114 ----
 src/common-ssh/guac_ssh_buffer.c          | 135 ----
 src/common-ssh/guac_ssh_buffer.h          | 134 ----
 src/common-ssh/guac_ssh_key.c             | 217 -------
 src/common-ssh/guac_ssh_key.h             | 170 -----
 src/common-ssh/guac_ssh_user.c            |  82 ---
 src/common-ssh/guac_ssh_user.h            | 108 ----
 src/common-ssh/key.c                      | 217 +++++++
 src/common-ssh/sftp.c                     | 783 +++++++++++++++++++++++
 src/common-ssh/ssh.c                      | 544 ++++++++++++++++
 src/common-ssh/user.c                     |  82 +++
 src/protocols/rdp/client.c                |   6 +-
 src/protocols/rdp/rdp.c                   |   6 +-
 src/protocols/rdp/rdp.h                   |   6 +-
 src/protocols/rdp/sftp.c                  |   2 +-
 src/protocols/ssh/client.c                |   4 +-
 src/protocols/ssh/clipboard.c             |   2 +-
 src/protocols/ssh/input.c                 |   2 +-
 src/protocols/ssh/sftp.c                  |   2 +-
 src/protocols/ssh/ssh.c                   |   6 +-
 src/protocols/ssh/ssh.h                   |   8 +-
 src/protocols/telnet/client.c             |   2 +-
 src/protocols/telnet/client.h             |   2 +-
 src/protocols/telnet/clipboard.c          |   2 +-
 src/protocols/telnet/input.c              |   2 +-
 src/protocols/telnet/telnet.c             |   2 +-
 src/protocols/telnet/telnet.h             |   2 +-
 src/protocols/telnet/user.c               |   2 +-
 src/protocols/vnc/client.c                |   6 +-
 src/protocols/vnc/sftp.c                  |   2 +-
 src/protocols/vnc/vnc.c                   |   4 +-
 src/protocols/vnc/vnc.h                   |   6 +-
 src/terminal/Makefile.am                  |  20 +-
 src/terminal/buffer.c                     |   4 +-
 src/terminal/buffer.h                     | 129 ----
 src/terminal/char_mappings.h              |  48 --
 src/terminal/common.c                     |   2 +-
 src/terminal/common.h                     |  53 --
 src/terminal/display.c                    |   6 +-
 src/terminal/display.h                    | 363 -----------
 src/terminal/scrollbar.c                  |   2 +-
 src/terminal/scrollbar.h                  | 357 -----------
 src/terminal/terminal.c                   |  14 +-
 src/terminal/terminal.h                   | 839 -------------------------
 src/terminal/terminal/buffer.h            | 129 ++++
 src/terminal/terminal/char_mappings.h     |  48 ++
 src/terminal/terminal/common.h            |  53 ++
 src/terminal/terminal/display.h           | 363 +++++++++++
 src/terminal/terminal/scrollbar.h         | 357 +++++++++++
 src/terminal/terminal/terminal.h          | 839 +++++++++++++++++++++++++
 src/terminal/terminal/terminal_handlers.h | 165 +++++
 src/terminal/terminal/types.h             | 124 ++++
 src/terminal/terminal/typescript.h        | 184 ++++++
 src/terminal/terminal_handlers.c          |   8 +-
 src/terminal/terminal_handlers.h          | 165 -----
 src/terminal/types.h                      | 124 ----
 src/terminal/typescript.c                 |   2 +-
 src/terminal/typescript.h                 | 184 ------
 68 files changed, 4870 insertions(+), 4870 deletions(-)
----------------------------------------------------------------------



[2/9] incubator-guacamole-server git commit: GUACAMOLE-169: Move common-ssh headers to namespaced directory.

Posted by jm...@apache.org.
GUACAMOLE-169: Move common-ssh headers to namespaced directory.


Project: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/commit/d371f2d9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/tree/d371f2d9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/diff/d371f2d9

Branch: refs/heads/master
Commit: d371f2d9eee689d717dcd0eae4238812a835258c
Parents: a808a6b
Author: Michael Jumper <mj...@apache.org>
Authored: Mon Feb 27 14:28:23 2017 -0800
Committer: Michael Jumper <mj...@apache.org>
Committed: Mon Feb 27 14:28:23 2017 -0800

----------------------------------------------------------------------
 src/common-ssh/Makefile.am         |  24 +-
 src/common-ssh/buffer.c            | 135 ++++++
 src/common-ssh/common-ssh/buffer.h | 134 ++++++
 src/common-ssh/common-ssh/key.h    | 170 +++++++
 src/common-ssh/common-ssh/sftp.h   | 242 ++++++++++
 src/common-ssh/common-ssh/ssh.h    | 114 +++++
 src/common-ssh/common-ssh/user.h   | 108 +++++
 src/common-ssh/guac_sftp.c         | 783 --------------------------------
 src/common-ssh/guac_sftp.h         | 242 ----------
 src/common-ssh/guac_ssh.c          | 544 ----------------------
 src/common-ssh/guac_ssh.h          | 114 -----
 src/common-ssh/guac_ssh_buffer.c   | 135 ------
 src/common-ssh/guac_ssh_buffer.h   | 134 ------
 src/common-ssh/guac_ssh_key.c      | 217 ---------
 src/common-ssh/guac_ssh_key.h      | 170 -------
 src/common-ssh/guac_ssh_user.c     |  82 ----
 src/common-ssh/guac_ssh_user.h     | 108 -----
 src/common-ssh/key.c               | 217 +++++++++
 src/common-ssh/sftp.c              | 783 ++++++++++++++++++++++++++++++++
 src/common-ssh/ssh.c               | 544 ++++++++++++++++++++++
 src/common-ssh/user.c              |  82 ++++
 src/protocols/rdp/client.c         |   6 +-
 src/protocols/rdp/rdp.c            |   6 +-
 src/protocols/rdp/rdp.h            |   6 +-
 src/protocols/rdp/sftp.c           |   2 +-
 src/protocols/ssh/client.c         |   2 +-
 src/protocols/ssh/sftp.c           |   2 +-
 src/protocols/ssh/ssh.c            |   4 +-
 src/protocols/ssh/ssh.h            |   6 +-
 src/protocols/vnc/client.c         |   6 +-
 src/protocols/vnc/sftp.c           |   2 +-
 src/protocols/vnc/vnc.c            |   4 +-
 src/protocols/vnc/vnc.h            |   6 +-
 33 files changed, 2567 insertions(+), 2567 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/Makefile.am
----------------------------------------------------------------------
diff --git a/src/common-ssh/Makefile.am b/src/common-ssh/Makefile.am
index a53f2cf..c05f264 100644
--- a/src/common-ssh/Makefile.am
+++ b/src/common-ssh/Makefile.am
@@ -23,18 +23,18 @@ ACLOCAL_AMFLAGS = -I m4
 noinst_LTLIBRARIES = libguac_common_ssh.la
 
 libguac_common_ssh_la_SOURCES = \
-    guac_sftp.c                 \
-    guac_ssh.c                  \
-    guac_ssh_buffer.c           \
-    guac_ssh_key.c              \
-    guac_ssh_user.c
-
-noinst_HEADERS =      \
-    guac_sftp.h       \
-    guac_ssh.h        \
-    guac_ssh_buffer.h \
-    guac_ssh_key.h    \
-    guac_ssh_user.h
+    buffer.c                    \
+    sftp.c                      \
+    ssh.c                       \
+    key.c                       \
+    user.c
+
+noinst_HEADERS =        \
+    common-ssh/buffer.h \
+    common-ssh/key.h    \
+    common-ssh/sftp.h   \
+    common-ssh/ssh.h    \
+    common-ssh/user.h
 
 libguac_common_ssh_la_CFLAGS = \
     -Werror -Wall -pedantic    \

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/buffer.c
----------------------------------------------------------------------
diff --git a/src/common-ssh/buffer.c b/src/common-ssh/buffer.c
new file mode 100644
index 0000000..b3d8d57
--- /dev/null
+++ b/src/common-ssh/buffer.c
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "config.h"
+
+#include <openssl/bn.h>
+#include <openssl/ossl_typ.h>
+
+#include <stdint.h>
+#include <string.h>
+#include <stdlib.h>
+
+void guac_common_ssh_buffer_write_byte(char** buffer, uint8_t value) {
+
+    uint8_t* data = (uint8_t*) *buffer;
+    *data = value;
+
+    (*buffer)++;
+
+}
+
+void guac_common_ssh_buffer_write_uint32(char** buffer, uint32_t value) {
+
+    uint8_t* data = (uint8_t*) *buffer;
+
+    data[0] = (value & 0xFF000000) >> 24;
+    data[1] = (value & 0x00FF0000) >> 16;
+    data[2] = (value & 0x0000FF00) >> 8;
+    data[3] =  value & 0x000000FF;
+
+    *buffer += 4;
+
+}
+
+void guac_common_ssh_buffer_write_data(char** buffer, const char* data,
+        int length) {
+    memcpy(*buffer, data, length);
+    *buffer += length;
+}
+
+void guac_common_ssh_buffer_write_bignum(char** buffer, BIGNUM* value) {
+
+    unsigned char* bn_buffer;
+    int length;
+
+    /* If zero, just write zero length */
+    if (BN_is_zero(value)) {
+        guac_common_ssh_buffer_write_uint32(buffer, 0);
+        return;
+    }
+
+    /* Allocate output buffer, add padding byte */
+    length = BN_num_bytes(value);
+    bn_buffer = malloc(length);
+
+    /* Convert BIGNUM */
+    BN_bn2bin(value, bn_buffer);
+
+    /* If first byte has high bit set, write padding byte */
+    if (bn_buffer[0] & 0x80) {
+        guac_common_ssh_buffer_write_uint32(buffer, length+1);
+        guac_common_ssh_buffer_write_byte(buffer, 0);
+    }
+    else
+        guac_common_ssh_buffer_write_uint32(buffer, length);
+
+    /* Write data */
+    memcpy(*buffer, bn_buffer, length);
+    *buffer += length;
+
+    free(bn_buffer);
+
+}
+
+void guac_common_ssh_buffer_write_string(char** buffer, const char* string,
+        int length) {
+    guac_common_ssh_buffer_write_uint32(buffer, length);
+    guac_common_ssh_buffer_write_data(buffer, string, length);
+}
+
+uint8_t guac_common_ssh_buffer_read_byte(char** buffer) {
+
+    uint8_t* data = (uint8_t*) *buffer;
+    uint8_t value = *data;
+
+    (*buffer)++;
+
+    return value;
+
+}
+
+uint32_t guac_common_ssh_buffer_read_uint32(char** buffer) {
+
+    uint8_t* data = (uint8_t*) *buffer;
+    uint32_t value =
+          (data[0] << 24)
+        | (data[1] << 16)
+        | (data[2] <<  8)
+        |  data[3];
+
+    *buffer += 4;
+
+    return value;
+
+}
+
+char* guac_common_ssh_buffer_read_string(char** buffer, int* length) {
+
+    char* value;
+
+    *length = guac_common_ssh_buffer_read_uint32(buffer);
+    value = *buffer;
+
+    *buffer += *length;
+
+    return value;
+
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/common-ssh/buffer.h
----------------------------------------------------------------------
diff --git a/src/common-ssh/common-ssh/buffer.h b/src/common-ssh/common-ssh/buffer.h
new file mode 100644
index 0000000..b42a2ac
--- /dev/null
+++ b/src/common-ssh/common-ssh/buffer.h
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef GUAC_COMMON_SSH_BUFFER_H
+#define GUAC_COMMON_SSH_BUFFER_H
+
+#include "config.h"
+
+#include <openssl/bn.h>
+#include <stdint.h>
+
+/**
+ * Writes the given byte to the given buffer, advancing the buffer pointer by
+ * one byte.
+ *
+ * @param buffer
+ *     The buffer to write to.
+ *
+ * @param value
+ *     The value to write.
+ */
+void guac_common_ssh_buffer_write_byte(char** buffer, uint8_t value);
+
+/**
+ * Writes the given integer to the given buffer, advancing the buffer pointer
+ * four bytes.
+ *
+ * @param buffer
+ *     The buffer to write to.
+ *
+ * @param value
+ *     The value to write.
+ */
+void guac_common_ssh_buffer_write_uint32(char** buffer, uint32_t value);
+
+/**
+ * Writes the given string and its length to the given buffer, advancing the
+ * buffer pointer by the size of the length (four bytes) and the size of the
+ * string.
+ *
+ * @param buffer
+ *     The buffer to write to.
+ *
+ * @param string
+ *     The string value to write.
+ *
+ * @param length
+ *     The length of the string to write, in bytes.
+ */
+void guac_common_ssh_buffer_write_string(char** buffer, const char* string,
+        int length);
+
+/**
+ * Writes the given BIGNUM the given buffer, advancing the buffer pointer by
+ * the size of the length (four bytes) and the size of the BIGNUM.
+ *
+ * @param buffer
+ *     The buffer to write to.
+ *
+ * @param value
+ *     The value to write.
+ */
+void guac_common_ssh_buffer_write_bignum(char** buffer, BIGNUM* value);
+
+/**
+ * Writes the given data the given buffer, advancing the buffer pointer by the
+ * given length.
+ *
+ * @param data
+ *     The arbitrary data to write.
+ *
+ * @param length
+ *     The length of data to write, in bytes.
+ */
+void guac_common_ssh_buffer_write_data(char** buffer, const char* data, int length);
+
+/**
+ * Reads a single byte from the given buffer, advancing the buffer by one byte.
+ *
+ * @param buffer
+ *     The buffer to read from.
+ *
+ * @return
+ *     The value read from the buffer.
+ */
+uint8_t guac_common_ssh_buffer_read_byte(char** buffer);
+
+/**
+ * Reads an integer from the given buffer, advancing the buffer by four bytes.
+ *
+ * @param buffer
+ *     The buffer to read from.
+ *
+ * @return
+ *     The value read from the buffer.
+ */
+uint32_t guac_common_ssh_buffer_read_uint32(char** buffer);
+
+/**
+ * Reads a string and its length from the given buffer, advancing the buffer
+ * by the size of the length (four bytes) and the size of the string, and
+ * returning a pointer to the buffer. The length of the string is stored in
+ * the given int.
+ *
+ * @param buffer
+ *     The buffer to read from.
+ *
+ * @param length
+ *     A pointer to an integer into which the length of the read string will
+ *     be stored.
+ *
+ * @return
+ *     A pointer to the value within the buffer.
+ */
+char* guac_common_ssh_buffer_read_string(char** buffer, int* length);
+
+#endif
+

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/common-ssh/key.h
----------------------------------------------------------------------
diff --git a/src/common-ssh/common-ssh/key.h b/src/common-ssh/common-ssh/key.h
new file mode 100644
index 0000000..1754dea
--- /dev/null
+++ b/src/common-ssh/common-ssh/key.h
@@ -0,0 +1,170 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef GUAC_COMMON_SSH_KEY_H
+#define GUAC_COMMON_SSH_KEY_H
+
+#include "config.h"
+
+#include <openssl/ossl_typ.h>
+
+/**
+ * The expected header of RSA private keys.
+ */
+#define SSH_RSA_KEY_HEADER "-----BEGIN RSA PRIVATE KEY-----"
+
+/**
+ * The expected header of DSA private keys.
+ */
+#define SSH_DSA_KEY_HEADER "-----BEGIN DSA PRIVATE KEY-----"
+
+/**
+ * The size of single number within a DSA signature, in bytes.
+ */
+#define DSA_SIG_NUMBER_SIZE 20
+
+/**
+ * The size of a DSA signature, in bytes.
+ */
+#define DSA_SIG_SIZE DSA_SIG_NUMBER_SIZE*2 
+
+/**
+ * The type of an SSH key.
+ */
+typedef enum guac_common_ssh_key_type {
+
+    /**
+     * RSA key.
+     */
+    SSH_KEY_RSA,
+
+    /**
+     * DSA key.
+     */
+    SSH_KEY_DSA
+
+} guac_common_ssh_key_type;
+
+/**
+ * Abstraction of a key used for SSH authentication.
+ */
+typedef struct guac_common_ssh_key {
+
+    /**
+     * The type of this key.
+     */
+    guac_common_ssh_key_type type;
+
+    /**
+     * Underlying RSA private key, if any.
+     */
+    RSA* rsa;
+
+    /**
+     * Underlying DSA private key, if any.
+     */
+    DSA* dsa;
+
+    /**
+     * The associated public key, encoded as necessary for SSH.
+     */
+    char* public_key;
+
+    /**
+     * The length of the public key, in bytes.
+     */
+    int public_key_length;
+
+    /**
+     * The private key, encoded as necessary for SSH.
+     */
+    char* private_key;
+
+    /**
+     * The length of the private key, in bytes.
+     */
+    int private_key_length;
+
+} guac_common_ssh_key;
+
+/**
+ * Allocates a new key containing the given private key data and specified
+ * passphrase. If unable to read the key, NULL is returned.
+ *
+ * @param data
+ *     The base64-encoded data to decode when reading the key.
+ *
+ * @param length
+ *     The length of the provided data, in bytes.
+ *
+ * @param passphrase
+ *     The passphrase to use when decrypting the key, if any, or an empty
+ *     string or NULL if no passphrase is needed.
+ *
+ * @return
+ *     The decoded, decrypted private key, or NULL if the key could not be
+ *     decoded.
+ */
+guac_common_ssh_key* guac_common_ssh_key_alloc(char* data, int length,
+        char* passphrase);
+
+/**
+ * Returns a statically-allocated string describing the most recent SSH key
+ * error.
+ *
+ * @return
+ *     A statically-allocated string describing the most recent SSH key error.
+ */
+const char* guac_common_ssh_key_error();
+
+/**
+ * Frees all memory associated with the given key.
+ *
+ * @param key
+ *     The key to free.
+ */
+void guac_common_ssh_key_free(guac_common_ssh_key* key);
+
+/**
+ * Signs the given data using the given key, returning the length of the
+ * signature in bytes, or a value less than zero on error.
+ *
+ * @param key
+ *     The key to use when signing the given data.
+ *
+ * @param data
+ *     The arbitrary data to sign.
+ *
+ * @param length
+ *     The length of the arbitrary data being signed, in bytes.
+ *
+ * @param sig
+ *     The buffer into which the signature should be written. The buffer must
+ *     be at least DSA_SIG_SIZE for DSA keys. For RSA keys, the signature size
+ *     is dependent only on key size, and is equal to the length of the
+ *     modulus, in bytes.
+ *
+ * @return
+ *     The number of bytes in the resulting signature.
+ */
+int guac_common_ssh_key_sign(guac_common_ssh_key* key, const char* data,
+        int length, unsigned char* sig);
+
+#endif
+

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/common-ssh/sftp.h
----------------------------------------------------------------------
diff --git a/src/common-ssh/common-ssh/sftp.h b/src/common-ssh/common-ssh/sftp.h
new file mode 100644
index 0000000..038a577
--- /dev/null
+++ b/src/common-ssh/common-ssh/sftp.h
@@ -0,0 +1,242 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef GUAC_COMMON_SSH_SFTP_H
+#define GUAC_COMMON_SSH_SFTP_H
+
+#include "common/json.h"
+#include "ssh.h"
+
+#include <guacamole/object.h>
+#include <guacamole/user.h>
+#include <libssh2.h>
+#include <libssh2_sftp.h>
+
+/**
+ * Maximum number of bytes per path.
+ */
+#define GUAC_COMMON_SSH_SFTP_MAX_PATH 2048
+
+/**
+ * Representation of an SFTP-driven filesystem object. Unlike guac_object, this
+ * structure is not tied to any particular user.
+ */
+typedef struct guac_common_ssh_sftp_filesystem {
+
+    /**
+     * The human-readable display name of this filesystem.
+     */
+    char* name;
+
+    /**
+     * The distinct SSH session used for SFTP.
+     */
+    guac_common_ssh_session* ssh_session;
+
+    /**
+     * SFTP session, used for file transfers.
+     */
+    LIBSSH2_SFTP* sftp_session;
+
+    /**
+     * The path files will be sent to, if uploaded directly via a "file"
+     * instruction.
+     */
+    char upload_path[GUAC_COMMON_SSH_SFTP_MAX_PATH];
+
+} guac_common_ssh_sftp_filesystem;
+
+/**
+ * The current state of a directory listing operation.
+ */
+typedef struct guac_common_ssh_sftp_ls_state {
+
+    /**
+     * The SFTP filesystem being listed.
+     */
+    guac_common_ssh_sftp_filesystem* filesystem;
+
+    /**
+     * Reference to the directory currently being listed over SFTP. This
+     * directory must already be open from a call to libssh2_sftp_opendir().
+     */
+    LIBSSH2_SFTP_HANDLE* directory;
+
+    /**
+     * The absolute path of the directory being listed.
+     */
+    char directory_name[GUAC_COMMON_SSH_SFTP_MAX_PATH];
+
+    /**
+     * The current state of the JSON directory object being written.
+     */
+    guac_common_json_state json_state;
+
+} guac_common_ssh_sftp_ls_state;
+
+/**
+ * Creates a new Guacamole filesystem object which provides access to files
+ * and directories via SFTP using the given SSH session. When the filesystem
+ * will no longer be used, it must be explicitly destroyed with
+ * guac_common_ssh_destroy_sftp_filesystem(). The resulting object is not
+ * automatically exposed to users of the connection - filesystem operations
+ * must be mediated either through various handlers or through exposing a
+ * filesystem guac_object via guac_common_ssh_alloc_sftp_filesystem_object().
+ *
+ * @param session
+ *     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, const char* name);
+
+/**
+ * Destroys the given filesystem object, disconnecting from SFTP and freeing
+ * and associated resources. Any associated session or user objects must be
+ * explicitly destroyed.
+ *
+ * @param filesystem
+ *     The filesystem object to destroy.
+ */
+void guac_common_ssh_destroy_sftp_filesystem(
+        guac_common_ssh_sftp_filesystem* filesystem);
+
+/**
+ * Creates and exposes a new filesystem guac_object to the given user,
+ * providing access to the files within the given SFTP filesystem. The
+ * allocated guac_object must eventually be freed via guac_user_free_object().
+ *
+ * @param filesystem
+ *     The filesystem object to expose.
+ *
+ * @param user
+ *     The user that the SFTP filesystem should be exposed to.
+ *
+ * @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);
+
+/**
+ * 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"
+ * instruction. The download will be automatically monitored and continued
+ * after this function terminates in response to "ack" instructions received by
+ * the user.
+ *
+ * @param filesystem
+ *     The filesystem containing the file to be downloaded.
+ *
+ * @param user
+ *     The user that should receive the file (via a "file" instruction).
+ *
+ * @param filename
+ *     The filename of the file to download, relative to the given filesystem.
+ *
+ * @return
+ *     The file stream created for the file download, already configured to
+ *     properly handle "ack" responses, etc. from the user.
+ */
+guac_stream* guac_common_ssh_sftp_download_file(
+        guac_common_ssh_sftp_filesystem* filesystem, guac_user* user,
+        char* filename);
+
+/**
+ * Handles an incoming stream from a Guacamole "file" instruction, saving the
+ * contents of that stream to the file having the given name within the
+ * upload directory set by guac_common_ssh_sftp_set_upload_path().
+ *
+ * @param filesystem
+ *     The filesystem that should receive the uploaded file.
+ *
+ * @param user
+ *     The user who is attempting to open the file stream (the user that sent
+ *     the "file" instruction).
+ *
+ * @param stream
+ *     The stream through which the uploaded file data will be received.
+ *
+ * @param mimetype
+ *     The mimetype of the data being received.
+ *
+ * @param filename
+ *     The filename of the file to write to. This filename will always be taken
+ *     relative to the upload path set by
+ *     guac_common_ssh_sftp_set_upload_path().
+ *
+ * @return
+ *     Zero if the incoming stream has been handled successfully, non-zero on
+ *     failure.
+ */
+int guac_common_ssh_sftp_handle_file_stream(
+        guac_common_ssh_sftp_filesystem* filesystem, guac_user* user,
+        guac_stream* stream, char* mimetype, char* filename);
+
+/**
+ * Set the destination directory for future uploads submitted via
+ * guac_common_ssh_sftp_handle_file_stream(). This function has no bearing
+ * on the destination directories of files uploaded with "put" instructions.
+ *
+ * @param filesystem
+ *     The filesystem to set the upload path of.
+ *
+ * @param path
+ *     The path to use for future uploads submitted via the
+ *     guac_common_ssh_sftp_handle_file_stream() function.
+ */
+void guac_common_ssh_sftp_set_upload_path(
+        guac_common_ssh_sftp_filesystem* filesystem, const char* path);
+
+#endif
+

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/common-ssh/ssh.h
----------------------------------------------------------------------
diff --git a/src/common-ssh/common-ssh/ssh.h b/src/common-ssh/common-ssh/ssh.h
new file mode 100644
index 0000000..d96ce44
--- /dev/null
+++ b/src/common-ssh/common-ssh/ssh.h
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef GUAC_COMMON_SSH_H
+#define GUAC_COMMON_SSH_H
+
+#include "user.h"
+
+#include <guacamole/client.h>
+#include <libssh2.h>
+
+/**
+ * An SSH session, backed by libssh2 and associated with a particular
+ * Guacamole client.
+ */
+typedef struct guac_common_ssh_session {
+
+    /**
+     * The Guacamole client using this SSH session.
+     */
+    guac_client* client;
+
+    /**
+     * The user that will be authenticating via SSH.
+     */
+    guac_common_ssh_user* user;
+
+    /**
+     * The underlying SSH session from libssh2.
+     */
+    LIBSSH2_SESSION* session;
+
+    /**
+     * The file descriptor of the socket being used for the SSH connection.
+     */
+    int fd;
+
+} guac_common_ssh_session;
+
+/**
+ * Initializes the underlying SSH and encryption libraries used by Guacamole.
+ * This function must be called before any other guac_common_ssh_*() functions
+ * are called.
+ *
+ * @param client
+ *     The Guacamole client that will be using SSH.
+ *
+ * @return
+ *     Zero if initialization, or non-zero if an error occurs.
+ */
+int guac_common_ssh_init(guac_client* client);
+
+/**
+ * Cleans up the underlying SSH and encryption libraries used by Guacamole.
+ * This function must be called once no other guac_common_ssh_*() functions
+ * will be used.
+ */
+void guac_common_ssh_uninit();
+
+/**
+ * Connects to the SSH server running at the given hostname and port, and
+ * authenticates as the given user. If an error occurs while connecting or
+ * authenticating, the Guacamole client will automatically and fatally abort.
+ * The user object provided must eventually be explicitly destroyed, but should
+ * not be destroyed until this session is destroyed, assuming the session is
+ * successfully created.
+ *
+ * @param client
+ *     The Guacamole client that will be using SSH.
+ *
+ * @param hostname
+ *     The hostname of the SSH server to connect to.
+ *
+ * @param port
+ *     The port to connect to on the given hostname.
+ *
+ * @param user
+ *     The user to authenticate as, once connected.
+ *
+ * @return
+ *     A new SSH session if the connection and authentication succeed, or NULL
+ *     if the connection or authentication were not successful.
+ */
+guac_common_ssh_session* guac_common_ssh_create_session(guac_client* client,
+        const char* hostname, const char* port, guac_common_ssh_user* user);
+
+/**
+ * Disconnects and destroys the given SSH session, freeing all associated
+ * resources. Any associated user must be explicitly destroyed, and will not
+ * be destroyed automatically.
+ *
+ * @param session
+ *     The SSH session to destroy.
+ */
+void guac_common_ssh_destroy_session(guac_common_ssh_session* session);
+
+#endif
+

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/common-ssh/user.h
----------------------------------------------------------------------
diff --git a/src/common-ssh/common-ssh/user.h b/src/common-ssh/common-ssh/user.h
new file mode 100644
index 0000000..745728f
--- /dev/null
+++ b/src/common-ssh/common-ssh/user.h
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef GUAC_COMMON_SSH_USER_H
+#define GUAC_COMMON_SSH_USER_H
+
+#include "key.h"
+
+/**
+ * Data describing an SSH user, including their credentials.
+ */
+typedef struct guac_common_ssh_user {
+
+    /**
+     * The username of this user.
+     */
+    char* username;
+
+    /**
+     * The password which should be used to authenticate this user, if any, or
+     * NULL if a private key will be used instead.
+     */
+    char* password;
+
+    /**
+     * The private key which should be used to authenticate this user, if any,
+     * or NULL if a password will be used instead.
+     */
+    guac_common_ssh_key* private_key;
+
+} guac_common_ssh_user;
+
+/**
+ * Creates a new SSH user with the given username. When additionally populated
+ * with a password or private key, this user can then be used for
+ * authentication.
+ *
+ * @param username
+ *     The username of the user being created.
+ *
+ * @return
+ *     A new SSH user having the given username, but no associated password
+ *     or private key.
+ */
+guac_common_ssh_user* guac_common_ssh_create_user(const char* username);
+
+/**
+ * Destroys the given user object, releasing all associated resources.
+ *
+ * @param user
+ *     The user to destroy.
+ */
+void guac_common_ssh_destroy_user(guac_common_ssh_user* user);
+
+/**
+ * Associates the given user with the given password, such that that password
+ * is used for future authentication attempts.
+ *
+ * @param user
+ *     The user to associate with the given password.
+ *
+ * @param password
+ *     The password to associate with the given user.
+ */
+void guac_common_ssh_user_set_password(guac_common_ssh_user* user,
+        const char* password);
+
+/**
+ * Imports the given private key, associating that key with the given user. If
+ * necessary to decrypt the key, a passphrase may be specified. The private key
+ * must be provided in base64 form. If the private key is imported
+ * successfully, it will be used for future authentication attempts.
+ *
+ * @param user
+ *     The user to associate with the given private key.
+ *
+ * @param private_key
+ *     The base64-encoded private key to import.
+ *
+ * @param passphrase
+ *     The passphrase to use to decrypt the given private key, or NULL if no
+ *     passphrase should be used.
+ *
+ * @return
+ *     Zero if the private key is successfully imported, or non-zero if the
+ *     private key could not be imported due to an error.
+ */
+int guac_common_ssh_user_import_key(guac_common_ssh_user* user,
+        char* private_key, char* passphrase);
+
+#endif
+

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/guac_sftp.c
----------------------------------------------------------------------
diff --git a/src/common-ssh/guac_sftp.c b/src/common-ssh/guac_sftp.c
deleted file mode 100644
index e7314d2..0000000
--- a/src/common-ssh/guac_sftp.c
+++ /dev/null
@@ -1,783 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#include "guac_sftp.h"
-#include "guac_ssh.h"
-
-#include <guacamole/client.h>
-#include <guacamole/object.h>
-#include <guacamole/protocol.h>
-#include <guacamole/socket.h>
-#include <guacamole/user.h>
-#include <libssh2.h>
-
-#include <fcntl.h>
-#include <libgen.h>
-#include <stdlib.h>
-#include <string.h>
-
-/**
- * Translates the last error message received by the SFTP layer of an SSH
- * session into a Guacamole protocol status code.
- *
- * @param filesystem
- *     The object (not guac_object) defining the filesystem associated with the
- *     SFTP and SSH sessions.
- *
- * @return
- *     The Guacamole protocol status code corresponding to the last reported
- *     error of the SFTP layer, if nay, or GUAC_PROTOCOL_STATUS_SUCCESS if no
- *     error has occurred.
- */
-static guac_protocol_status guac_sftp_get_status(
-        guac_common_ssh_sftp_filesystem* filesystem) {
-
-    /* Get libssh2 objects */
-    LIBSSH2_SFTP*    sftp    = filesystem->sftp_session;
-    LIBSSH2_SESSION* session = filesystem->ssh_session->session;
-
-    /* Return success code if no error occurred */
-    if (libssh2_session_last_errno(session) != LIBSSH2_ERROR_SFTP_PROTOCOL)
-        return GUAC_PROTOCOL_STATUS_SUCCESS;
-
-    /* Translate SFTP error codes defined by
-     * https://tools.ietf.org/html/draft-ietf-secsh-filexfer-02 (the most
-     * commonly-implemented standard) */
-    switch (libssh2_sftp_last_error(sftp)) {
-
-        /* SSH_FX_OK (not an error) */
-        case 0:
-            return GUAC_PROTOCOL_STATUS_SUCCESS;
-
-        /* SSH_FX_EOF (technically not an error) */
-        case 1:
-            return GUAC_PROTOCOL_STATUS_SUCCESS;
-
-        /* SSH_FX_NO_SUCH_FILE */
-        case 2:
-            return GUAC_PROTOCOL_STATUS_RESOURCE_NOT_FOUND;
-
-        /* SSH_FX_PERMISSION_DENIED */
-        case 3:
-            return GUAC_PROTOCOL_STATUS_CLIENT_FORBIDDEN;
-
-        /* SSH_FX_FAILURE */
-        case 4:
-            return GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR;
-
-        /* SSH_FX_BAD_MESSAGE */
-        case 5:
-            return GUAC_PROTOCOL_STATUS_SERVER_ERROR;
-
-        /* SSH_FX_NO_CONNECTION / SSH_FX_CONNECTION_LOST */
-        case 6:
-        case 7:
-            return GUAC_PROTOCOL_STATUS_UPSTREAM_TIMEOUT;
-
-        /* SSH_FX_OP_UNSUPPORTED */
-        case 8:
-            return GUAC_PROTOCOL_STATUS_UNSUPPORTED;
-
-        /* Return generic error if cause unknown */
-        default:
-            return GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR;
-
-    }
-
-}
-
-/**
- * Concatenates the given filename with the given path, separating the two
- * with a single forward slash. The full result must be no more than
- * GUAC_COMMON_SSH_SFTP_MAX_PATH bytes long, counting null terminator.
- *
- * @param fullpath
- *     The buffer to store the result within. This buffer must be at least
- *     GUAC_COMMON_SSH_SFTP_MAX_PATH bytes long.
- *
- * @param path
- *     The path to append the filename to.
- *
- * @param filename
- *     The filename to append to the path.
- *
- * @return
- *     Non-zero if the filename is valid and was successfully appended to the
- *     path, zero otherwise.
- */
-static int guac_ssh_append_filename(char* fullpath, const char* path,
-        const char* filename) {
-
-    int i;
-
-    /* Disallow "." as a filename */
-    if (strcmp(filename, ".") == 0)
-        return 0;
-
-    /* Disallow ".." as a filename */
-    if (strcmp(filename, "..") == 0)
-        return 0;
-
-    /* Copy path, append trailing slash */
-    for (i=0; i<GUAC_COMMON_SSH_SFTP_MAX_PATH; i++) {
-
-        /*
-         * Append trailing slash only if:
-         *  1) Trailing slash is not already present
-         *  2) Path is non-empty
-         */
-
-        char c = path[i];
-        if (c == '\0') {
-            if (i > 0 && path[i-1] != '/')
-                fullpath[i++] = '/';
-            break;
-        }
-
-        /* Copy character if not end of string */
-        fullpath[i] = c;
-
-    }
-
-    /* Append filename */
-    for (; i<GUAC_COMMON_SSH_SFTP_MAX_PATH; i++) {
-
-        char c = *(filename++);
-        if (c == '\0')
-            break;
-
-        /* Filenames may not contain slashes */
-        if (c == '\\' || c == '/')
-            return 0;
-
-        /* Append each character within filename */
-        fullpath[i] = c;
-
-    }
-
-    /* Verify path length is within maximum */
-    if (i == GUAC_COMMON_SSH_SFTP_MAX_PATH)
-        return 0;
-
-    /* Terminate path string */
-    fullpath[i] = '\0';
-
-    /* Append was successful */
-    return 1;
-
-}
-
-/**
- * Handler for blob messages which continue an inbound SFTP data transfer
- * (upload). The data associated with the given stream is expected to be a
- * pointer to an open LIBSSH2_SFTP_HANDLE for the file to which the data
- * should be written.
- *
- * @param user
- *     The user receiving the blob message.
- *
- * @param stream
- *     The Guacamole protocol stream associated with the received blob message.
- *
- * @param data
- *     The data received within the blob.
- *
- * @param length
- *     The length of the received data, in bytes.
- *
- * @return
- *     Zero if the blob is handled successfully, or non-zero on error.
- */
-static int guac_common_ssh_sftp_blob_handler(guac_user* user,
-        guac_stream* stream, void* data, int length) {
-
-    /* Pull file from stream */
-    LIBSSH2_SFTP_HANDLE* file = (LIBSSH2_SFTP_HANDLE*) stream->data;
-
-    /* Attempt write */
-    if (libssh2_sftp_write(file, data, length) == length) {
-        guac_user_log(user, GUAC_LOG_DEBUG, "%i bytes written", length);
-        guac_protocol_send_ack(user->socket, stream, "SFTP: OK",
-                GUAC_PROTOCOL_STATUS_SUCCESS);
-        guac_socket_flush(user->socket);
-    }
-
-    /* Inform of any errors */
-    else {
-        guac_user_log(user, GUAC_LOG_INFO, "Unable to write to file");
-        guac_protocol_send_ack(user->socket, stream, "SFTP: Write failed",
-                GUAC_PROTOCOL_STATUS_SERVER_ERROR);
-        guac_socket_flush(user->socket);
-    }
-
-    return 0;
-
-}
-
-/**
- * Handler for end messages which terminate an inbound SFTP data transfer
- * (upload). The data associated with the given stream is expected to be a
- * pointer to an open LIBSSH2_SFTP_HANDLE for the file to which the data
- * has been written and which should now be closed.
- *
- * @param user
- *     The user receiving the end message.
- *
- * @param stream
- *     The Guacamole protocol stream associated with the received end message.
- *
- * @return
- *     Zero if the file is closed successfully, or non-zero on error.
- */
-static int guac_common_ssh_sftp_end_handler(guac_user* user,
-        guac_stream* stream) {
-
-    /* Pull file from stream */
-    LIBSSH2_SFTP_HANDLE* file = (LIBSSH2_SFTP_HANDLE*) stream->data;
-
-    /* Attempt to close file */
-    if (libssh2_sftp_close(file) == 0) {
-        guac_user_log(user, GUAC_LOG_DEBUG, "File closed");
-        guac_protocol_send_ack(user->socket, stream, "SFTP: OK",
-                GUAC_PROTOCOL_STATUS_SUCCESS);
-        guac_socket_flush(user->socket);
-    }
-    else {
-        guac_user_log(user, GUAC_LOG_INFO, "Unable to close file");
-        guac_protocol_send_ack(user->socket, stream, "SFTP: Close failed",
-                GUAC_PROTOCOL_STATUS_SERVER_ERROR);
-        guac_socket_flush(user->socket);
-    }
-
-    return 0;
-
-}
-
-int guac_common_ssh_sftp_handle_file_stream(
-        guac_common_ssh_sftp_filesystem* filesystem, guac_user* user,
-        guac_stream* stream, char* mimetype, char* filename) {
-
-    char fullpath[GUAC_COMMON_SSH_SFTP_MAX_PATH];
-    LIBSSH2_SFTP_HANDLE* file;
-
-    /* Concatenate filename with path */
-    if (!guac_ssh_append_filename(fullpath, filesystem->upload_path,
-                filename)) {
-
-        guac_user_log(user, GUAC_LOG_DEBUG,
-                "Filename \"%s\" is invalid or resulting path is too long",
-                filename);
-
-        /* Abort transfer - invalid filename */
-        guac_protocol_send_ack(user->socket, stream, 
-                "SFTP: Illegal filename",
-                GUAC_PROTOCOL_STATUS_CLIENT_BAD_REQUEST);
-
-        guac_socket_flush(user->socket);
-        return 0;
-    }
-
-    /* Open file via SFTP */
-    file = libssh2_sftp_open(filesystem->sftp_session, fullpath,
-            LIBSSH2_FXF_WRITE | LIBSSH2_FXF_CREAT | LIBSSH2_FXF_TRUNC,
-            S_IRUSR | S_IWUSR);
-
-    /* Inform of status */
-    if (file != NULL) {
-
-        guac_user_log(user, GUAC_LOG_DEBUG,
-                "File \"%s\" opened",
-                fullpath);
-
-        guac_protocol_send_ack(user->socket, stream, "SFTP: File opened",
-                GUAC_PROTOCOL_STATUS_SUCCESS);
-        guac_socket_flush(user->socket);
-    }
-    else {
-        guac_user_log(user, GUAC_LOG_INFO,
-                "Unable to open file \"%s\"", fullpath);
-        guac_protocol_send_ack(user->socket, stream, "SFTP: Open failed",
-                guac_sftp_get_status(filesystem));
-        guac_socket_flush(user->socket);
-    }
-
-    /* Set handlers for file stream */
-    stream->blob_handler = guac_common_ssh_sftp_blob_handler;
-    stream->end_handler = guac_common_ssh_sftp_end_handler;
-
-    /* Store file within stream */
-    stream->data = file;
-    return 0;
-
-}
-
-/**
- * Handler for ack messages which continue an outbound SFTP data transfer
- * (download), signalling the current status and requesting additional data.
- * The data associated with the given stream is expected to be a pointer to an
- * open LIBSSH2_SFTP_HANDLE for the file from which the data is to be read.
- *
- * @param user
- *     The user receiving the ack message.
- *
- * @param stream
- *     The Guacamole protocol stream associated with the received ack message.
- *
- * @param message
- *     An arbitrary human-readable message describing the nature of the
- *     success or failure denoted by the ack message.
- *
- * @param status
- *     The status code associated with the ack message, which may indicate
- *     success or an error.
- *
- * @return
- *     Zero if the file is read from successfully, or non-zero on error.
- */
-static int guac_common_ssh_sftp_ack_handler(guac_user* user,
-        guac_stream* stream, char* message, guac_protocol_status status) {
-
-    /* Pull file from stream */
-    LIBSSH2_SFTP_HANDLE* file = (LIBSSH2_SFTP_HANDLE*) stream->data;
-
-    /* If successful, read data */
-    if (status == GUAC_PROTOCOL_STATUS_SUCCESS) {
-
-        /* Attempt read into buffer */
-        char buffer[4096];
-        int bytes_read = libssh2_sftp_read(file, buffer, sizeof(buffer)); 
-
-        /* If bytes read, send as blob */
-        if (bytes_read > 0) {
-            guac_protocol_send_blob(user->socket, stream,
-                    buffer, bytes_read);
-
-            guac_user_log(user, GUAC_LOG_DEBUG, "%i bytes sent to user",
-                    bytes_read);
-
-        }
-
-        /* If EOF, send end */
-        else if (bytes_read == 0) {
-            guac_user_log(user, GUAC_LOG_DEBUG, "File sent");
-            guac_protocol_send_end(user->socket, stream);
-            guac_user_free_stream(user, stream);
-        }
-
-        /* Otherwise, fail stream */
-        else {
-            guac_user_log(user, GUAC_LOG_INFO, "Error reading file");
-            guac_protocol_send_end(user->socket, stream);
-            guac_user_free_stream(user, stream);
-        }
-
-        guac_socket_flush(user->socket);
-
-    }
-
-    /* Otherwise, return stream to user */
-    else
-        guac_user_free_stream(user, stream);
-
-    return 0;
-}
-
-guac_stream* guac_common_ssh_sftp_download_file(
-        guac_common_ssh_sftp_filesystem* filesystem, guac_user* user,
-        char* filename) {
-
-    guac_stream* stream;
-    LIBSSH2_SFTP_HANDLE* file;
-
-    /* Attempt to open file for reading */
-    file = libssh2_sftp_open(filesystem->sftp_session, filename,
-            LIBSSH2_FXF_READ, 0);
-    if (file == NULL) {
-        guac_user_log(user, GUAC_LOG_INFO, 
-                "Unable to read file \"%s\"", filename);
-        return NULL;
-    }
-
-    /* Allocate stream */
-    stream = guac_user_alloc_stream(user);
-    stream->ack_handler = guac_common_ssh_sftp_ack_handler;
-    stream->data = file;
-
-    /* Send stream start, strip name */
-    filename = basename(filename);
-    guac_protocol_send_file(user->socket, stream,
-            "application/octet-stream", filename);
-    guac_socket_flush(user->socket);
-
-    guac_user_log(user, GUAC_LOG_DEBUG, "Sending file \"%s\"", filename);
-    return stream;
-
-}
-
-void guac_common_ssh_sftp_set_upload_path(
-        guac_common_ssh_sftp_filesystem* filesystem, const char* path) {
-
-    guac_client* client = filesystem->ssh_session->client;
-
-    /* Ignore requests which exceed maximum-allowed path */
-    int length = strnlen(path, GUAC_COMMON_SSH_SFTP_MAX_PATH)+1;
-    if (length > GUAC_COMMON_SSH_SFTP_MAX_PATH) {
-        guac_client_log(client, GUAC_LOG_ERROR,
-                "Submitted path exceeds limit of %i bytes",
-                GUAC_COMMON_SSH_SFTP_MAX_PATH);
-        return;
-    }
-
-    /* Copy path */
-    memcpy(filesystem->upload_path, path, length);
-    guac_client_log(client, GUAC_LOG_DEBUG, "Upload path set to \"%s\"", path);
-
-}
-
-/**
- * Handler for ack messages received due to receipt of a "body" or "blob"
- * instruction associated with a SFTP directory list operation.
- *
- * @param user
- *     The user receiving the ack message.
- *
- * @param stream
- *     The Guacamole protocol stream associated with the received ack message.
- *
- * @param message
- *     An arbitrary human-readable message describing the nature of the
- *     success or failure denoted by this ack message.
- *
- * @param status
- *     The status code associated with this ack message, which may indicate
- *     success or an error.
- *
- * @return
- *     Zero on success, non-zero on error.
- */
-static int guac_common_ssh_sftp_ls_ack_handler(guac_user* user,
-        guac_stream* stream, char* message, guac_protocol_status status) {
-
-    int bytes_read;
-    int blob_written = 0;
-
-    char filename[GUAC_COMMON_SSH_SFTP_MAX_PATH];
-    LIBSSH2_SFTP_ATTRIBUTES attributes;
-
-    guac_common_ssh_sftp_ls_state* list_state =
-        (guac_common_ssh_sftp_ls_state*) stream->data;
-
-    guac_common_ssh_sftp_filesystem* filesystem = list_state->filesystem;
-
-    LIBSSH2_SFTP* sftp = filesystem->sftp_session;
-
-    /* If unsuccessful, free stream and abort */
-    if (status != GUAC_PROTOCOL_STATUS_SUCCESS) {
-        libssh2_sftp_closedir(list_state->directory);
-        guac_user_free_stream(user, stream);
-        free(list_state);
-        return 0;
-    }
-
-    /* While directory entries remain */
-    while ((bytes_read = libssh2_sftp_readdir(list_state->directory,
-                filename, sizeof(filename), &attributes)) > 0
-            && !blob_written) {
-
-        char absolute_path[GUAC_COMMON_SSH_SFTP_MAX_PATH];
-
-        /* Skip current and parent directory entries */
-        if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0)
-            continue;
-
-        /* Concatenate into absolute path - skip if invalid */
-        if (!guac_ssh_append_filename(absolute_path, 
-                    list_state->directory_name, filename)) {
-
-            guac_user_log(user, GUAC_LOG_DEBUG,
-                    "Skipping filename \"%s\" - filename is invalid or "
-                    "resulting path is too long", filename);
-
-            continue;
-        }
-
-        /* Stat explicitly if symbolic link (might point to directory) */
-        if (LIBSSH2_SFTP_S_ISLNK(attributes.permissions))
-            libssh2_sftp_stat(sftp, absolute_path, &attributes);
-
-        /* Determine mimetype */
-        const char* mimetype;
-        if (LIBSSH2_SFTP_S_ISDIR(attributes.permissions))
-            mimetype = GUAC_USER_STREAM_INDEX_MIMETYPE;
-        else
-            mimetype = "application/octet-stream";
-
-        /* Write entry */
-        blob_written |= guac_common_json_write_property(user, stream,
-                &list_state->json_state, absolute_path, mimetype);
-
-    }
-
-    /* Complete JSON and cleanup at end of directory */
-    if (bytes_read <= 0) {
-
-        /* Complete JSON object */
-        guac_common_json_end_object(user, stream, &list_state->json_state);
-        guac_common_json_flush(user, stream, &list_state->json_state);
-
-        /* Clean up resources */
-        libssh2_sftp_closedir(list_state->directory);
-        free(list_state);
-
-        /* Signal of stream */
-        guac_protocol_send_end(user->socket, stream);
-        guac_user_free_stream(user, stream);
-
-    }
-
-    guac_socket_flush(user->socket);
-    return 0;
-
-}
-
-/**
- * Handler for get messages. In context of SFTP and the filesystem exposed via
- * the Guacamole protocol, get messages request the body of a file within the
- * filesystem.
- *
- * @param user
- *     The user who sent the get message.
- *
- * @param object
- *     The Guacamole protocol object associated with the get request itself.
- *
- * @param name
- *     The name of the input stream (file) being requested.
- *
- * @return
- *     Zero on success, non-zero on error.
- */
-static int guac_common_ssh_sftp_get_handler(guac_user* user,
-        guac_object* object, char* name) {
-
-    guac_common_ssh_sftp_filesystem* filesystem =
-        (guac_common_ssh_sftp_filesystem*) object->data;
-
-    LIBSSH2_SFTP* sftp = filesystem->sftp_session;
-    LIBSSH2_SFTP_ATTRIBUTES attributes;
-
-    /* Attempt to read file information */
-    if (libssh2_sftp_stat(sftp, name, &attributes)) {
-        guac_user_log(user, GUAC_LOG_INFO, "Unable to read file \"%s\"",
-                name);
-        return 0;
-    }
-
-    /* If directory, send contents of directory */
-    if (LIBSSH2_SFTP_S_ISDIR(attributes.permissions)) {
-
-        /* Open as directory */
-        LIBSSH2_SFTP_HANDLE* dir = libssh2_sftp_opendir(sftp, name);
-        if (dir == NULL) {
-            guac_user_log(user, GUAC_LOG_INFO,
-                    "Unable to read directory \"%s\"", name);
-            return 0;
-        }
-
-        /* Init directory listing state */
-        guac_common_ssh_sftp_ls_state* list_state =
-            malloc(sizeof(guac_common_ssh_sftp_ls_state));
-
-        list_state->directory = dir;
-        list_state->filesystem = filesystem;
-        strncpy(list_state->directory_name, name,
-                sizeof(list_state->directory_name) - 1);
-
-        /* Allocate stream for body */
-        guac_stream* stream = guac_user_alloc_stream(user);
-        stream->ack_handler = guac_common_ssh_sftp_ls_ack_handler;
-        stream->data = list_state;
-
-        /* Init JSON object state */
-        guac_common_json_begin_object(user, stream, &list_state->json_state);
-
-        /* Associate new stream with get request */
-        guac_protocol_send_body(user->socket, object, stream,
-                GUAC_USER_STREAM_INDEX_MIMETYPE, name);
-
-    }
-
-    /* Otherwise, send file contents */
-    else {
-
-        /* Open as normal file */
-        LIBSSH2_SFTP_HANDLE* file = libssh2_sftp_open(sftp, name,
-            LIBSSH2_FXF_READ, 0);
-        if (file == NULL) {
-            guac_user_log(user, GUAC_LOG_INFO,
-                    "Unable to read file \"%s\"", name);
-            return 0;
-        }
-
-        /* Allocate stream for body */
-        guac_stream* stream = guac_user_alloc_stream(user);
-        stream->ack_handler = guac_common_ssh_sftp_ack_handler;
-        stream->data = file;
-
-        /* Associate new stream with get request */
-        guac_protocol_send_body(user->socket, object, stream,
-                "application/octet-stream", name);
-
-    }
-
-    guac_socket_flush(user->socket);
-    return 0;
-}
-
-/**
- * Handler for put messages. In context of SFTP and the filesystem exposed via
- * the Guacamole protocol, put messages request write access to a file within
- * the filesystem.
- *
- * @param user
- *     The user who sent the put message.
- *
- * @param object
- *     The Guacamole protocol object associated with the put request itself.
- *
- * @param stream
- *     The Guacamole protocol stream along which the user will be sending
- *     file data.
- *
- * @param mimetype
- *     The mimetype of the data being send along the stream.
- *
- * @param name
- *     The name of the input stream (file) being requested.
- *
- * @return
- *     Zero on success, non-zero on error.
- */
-static int guac_common_ssh_sftp_put_handler(guac_user* user,
-        guac_object* object, guac_stream* stream, char* mimetype, char* name) {
-
-    guac_common_ssh_sftp_filesystem* filesystem =
-        (guac_common_ssh_sftp_filesystem*) object->data;
-
-    LIBSSH2_SFTP* sftp = filesystem->sftp_session;
-
-    /* Open file via SFTP */
-    LIBSSH2_SFTP_HANDLE* file = libssh2_sftp_open(sftp, name,
-            LIBSSH2_FXF_WRITE | LIBSSH2_FXF_CREAT | LIBSSH2_FXF_TRUNC,
-            S_IRUSR | S_IWUSR);
-
-    /* Acknowledge stream if successful */
-    if (file != NULL) {
-        guac_user_log(user, GUAC_LOG_DEBUG, "File \"%s\" opened", name);
-        guac_protocol_send_ack(user->socket, stream, "SFTP: File opened",
-                GUAC_PROTOCOL_STATUS_SUCCESS);
-    }
-
-    /* Abort on failure */
-    else {
-        guac_user_log(user, GUAC_LOG_INFO,
-                "Unable to open file \"%s\"", name);
-        guac_protocol_send_ack(user->socket, stream, "SFTP: Open failed",
-                guac_sftp_get_status(filesystem));
-    }
-
-    /* Set handlers for file stream */
-    stream->blob_handler = guac_common_ssh_sftp_blob_handler;
-    stream->end_handler = guac_common_ssh_sftp_end_handler;
-
-    /* Store file within stream */
-    stream->data = file;
-
-    guac_socket_flush(user->socket);
-    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) {
-
-    /* Init filesystem */
-    guac_object* fs_object = guac_user_alloc_object(user);
-    fs_object->get_handler = guac_common_ssh_sftp_get_handler;
-    fs_object->put_handler = guac_common_ssh_sftp_put_handler;
-    fs_object->data = filesystem;
-
-    /* Send filesystem to user */
-    guac_protocol_send_filesystem(user->socket, fs_object, filesystem->name);
-    guac_socket_flush(user->socket);
-
-    return fs_object;
-
-}
-
-guac_common_ssh_sftp_filesystem* guac_common_ssh_create_sftp_filesystem(
-        guac_common_ssh_session* session, const char* name) {
-
-    /* Request SFTP */
-    LIBSSH2_SFTP* sftp_session = libssh2_sftp_init(session->session);
-    if (sftp_session == NULL)
-        return NULL;
-
-    /* Allocate data for SFTP session */
-    guac_common_ssh_sftp_filesystem* 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;
-
-    /* Initially upload files to current directory */
-    strcpy(filesystem->upload_path, ".");
-
-    /* Return allocated filesystem */
-    return filesystem;
-
-}
-
-void guac_common_ssh_destroy_sftp_filesystem(
-        guac_common_ssh_sftp_filesystem* filesystem) {
-
-    /* Shutdown SFTP session */
-    libssh2_sftp_shutdown(filesystem->sftp_session);
-
-    /* Free associated memory */
-    free(filesystem->name);
-    free(filesystem);
-
-}
-

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/guac_sftp.h
----------------------------------------------------------------------
diff --git a/src/common-ssh/guac_sftp.h b/src/common-ssh/guac_sftp.h
deleted file mode 100644
index fdc1e72..0000000
--- a/src/common-ssh/guac_sftp.h
+++ /dev/null
@@ -1,242 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef GUAC_COMMON_SSH_SFTP_H
-#define GUAC_COMMON_SSH_SFTP_H
-
-#include "common/json.h"
-#include "guac_ssh.h"
-
-#include <guacamole/object.h>
-#include <guacamole/user.h>
-#include <libssh2.h>
-#include <libssh2_sftp.h>
-
-/**
- * Maximum number of bytes per path.
- */
-#define GUAC_COMMON_SSH_SFTP_MAX_PATH 2048
-
-/**
- * Representation of an SFTP-driven filesystem object. Unlike guac_object, this
- * structure is not tied to any particular user.
- */
-typedef struct guac_common_ssh_sftp_filesystem {
-
-    /**
-     * The human-readable display name of this filesystem.
-     */
-    char* name;
-
-    /**
-     * The distinct SSH session used for SFTP.
-     */
-    guac_common_ssh_session* ssh_session;
-
-    /**
-     * SFTP session, used for file transfers.
-     */
-    LIBSSH2_SFTP* sftp_session;
-
-    /**
-     * The path files will be sent to, if uploaded directly via a "file"
-     * instruction.
-     */
-    char upload_path[GUAC_COMMON_SSH_SFTP_MAX_PATH];
-
-} guac_common_ssh_sftp_filesystem;
-
-/**
- * The current state of a directory listing operation.
- */
-typedef struct guac_common_ssh_sftp_ls_state {
-
-    /**
-     * The SFTP filesystem being listed.
-     */
-    guac_common_ssh_sftp_filesystem* filesystem;
-
-    /**
-     * Reference to the directory currently being listed over SFTP. This
-     * directory must already be open from a call to libssh2_sftp_opendir().
-     */
-    LIBSSH2_SFTP_HANDLE* directory;
-
-    /**
-     * The absolute path of the directory being listed.
-     */
-    char directory_name[GUAC_COMMON_SSH_SFTP_MAX_PATH];
-
-    /**
-     * The current state of the JSON directory object being written.
-     */
-    guac_common_json_state json_state;
-
-} guac_common_ssh_sftp_ls_state;
-
-/**
- * Creates a new Guacamole filesystem object which provides access to files
- * and directories via SFTP using the given SSH session. When the filesystem
- * will no longer be used, it must be explicitly destroyed with
- * guac_common_ssh_destroy_sftp_filesystem(). The resulting object is not
- * automatically exposed to users of the connection - filesystem operations
- * must be mediated either through various handlers or through exposing a
- * filesystem guac_object via guac_common_ssh_alloc_sftp_filesystem_object().
- *
- * @param session
- *     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, const char* name);
-
-/**
- * Destroys the given filesystem object, disconnecting from SFTP and freeing
- * and associated resources. Any associated session or user objects must be
- * explicitly destroyed.
- *
- * @param filesystem
- *     The filesystem object to destroy.
- */
-void guac_common_ssh_destroy_sftp_filesystem(
-        guac_common_ssh_sftp_filesystem* filesystem);
-
-/**
- * Creates and exposes a new filesystem guac_object to the given user,
- * providing access to the files within the given SFTP filesystem. The
- * allocated guac_object must eventually be freed via guac_user_free_object().
- *
- * @param filesystem
- *     The filesystem object to expose.
- *
- * @param user
- *     The user that the SFTP filesystem should be exposed to.
- *
- * @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);
-
-/**
- * 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"
- * instruction. The download will be automatically monitored and continued
- * after this function terminates in response to "ack" instructions received by
- * the user.
- *
- * @param filesystem
- *     The filesystem containing the file to be downloaded.
- *
- * @param user
- *     The user that should receive the file (via a "file" instruction).
- *
- * @param filename
- *     The filename of the file to download, relative to the given filesystem.
- *
- * @return
- *     The file stream created for the file download, already configured to
- *     properly handle "ack" responses, etc. from the user.
- */
-guac_stream* guac_common_ssh_sftp_download_file(
-        guac_common_ssh_sftp_filesystem* filesystem, guac_user* user,
-        char* filename);
-
-/**
- * Handles an incoming stream from a Guacamole "file" instruction, saving the
- * contents of that stream to the file having the given name within the
- * upload directory set by guac_common_ssh_sftp_set_upload_path().
- *
- * @param filesystem
- *     The filesystem that should receive the uploaded file.
- *
- * @param user
- *     The user who is attempting to open the file stream (the user that sent
- *     the "file" instruction).
- *
- * @param stream
- *     The stream through which the uploaded file data will be received.
- *
- * @param mimetype
- *     The mimetype of the data being received.
- *
- * @param filename
- *     The filename of the file to write to. This filename will always be taken
- *     relative to the upload path set by
- *     guac_common_ssh_sftp_set_upload_path().
- *
- * @return
- *     Zero if the incoming stream has been handled successfully, non-zero on
- *     failure.
- */
-int guac_common_ssh_sftp_handle_file_stream(
-        guac_common_ssh_sftp_filesystem* filesystem, guac_user* user,
-        guac_stream* stream, char* mimetype, char* filename);
-
-/**
- * Set the destination directory for future uploads submitted via
- * guac_common_ssh_sftp_handle_file_stream(). This function has no bearing
- * on the destination directories of files uploaded with "put" instructions.
- *
- * @param filesystem
- *     The filesystem to set the upload path of.
- *
- * @param path
- *     The path to use for future uploads submitted via the
- *     guac_common_ssh_sftp_handle_file_stream() function.
- */
-void guac_common_ssh_sftp_set_upload_path(
-        guac_common_ssh_sftp_filesystem* filesystem, const char* path);
-
-#endif
-

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/guac_ssh.c
----------------------------------------------------------------------
diff --git a/src/common-ssh/guac_ssh.c b/src/common-ssh/guac_ssh.c
deleted file mode 100644
index 488eea2..0000000
--- a/src/common-ssh/guac_ssh.c
+++ /dev/null
@@ -1,544 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#include "guac_ssh.h"
-#include "guac_ssh_key.h"
-#include "guac_ssh_user.h"
-
-#include <guacamole/client.h>
-#include <libssh2.h>
-
-#ifdef LIBSSH2_USES_GCRYPT
-#include <gcrypt.h>
-#endif
-
-#include <openssl/err.h>
-#include <openssl/ssl.h>
-
-#include <errno.h>
-#include <netdb.h>
-#include <netinet/in.h>
-#include <pthread.h>
-#include <stddef.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/socket.h>
-#include <unistd.h>
-
-#ifdef LIBSSH2_USES_GCRYPT
-GCRY_THREAD_OPTION_PTHREAD_IMPL;
-#endif
-
-/**
- * Array of mutexes, used by OpenSSL.
- */
-static pthread_mutex_t* guac_common_ssh_openssl_locks = NULL;
-
-/**
- * Called by OpenSSL when locking or unlocking the Nth mutex.
- *
- * @param mode
- *     A bitmask denoting the action to be taken on the Nth lock, such as
- *     CRYPTO_LOCK or CRYPTO_UNLOCK.
- *
- * @param n
- *     The index of the lock to lock or unlock.
- *
- * @param file
- *     The filename of the function setting the lock, for debugging purposes.
- *
- * @param line
- *     The line number of the function setting the lock, for debugging
- *     purposes.
- */
-static void guac_common_ssh_openssl_locking_callback(int mode, int n,
-        const char* file, int line){
-
-    /* Lock given mutex upon request */
-    if (mode & CRYPTO_LOCK)
-        pthread_mutex_lock(&(guac_common_ssh_openssl_locks[n]));
-
-    /* Unlock given mutex upon request */
-    else if (mode & CRYPTO_UNLOCK)
-        pthread_mutex_unlock(&(guac_common_ssh_openssl_locks[n]));
-
-}
-
-/**
- * Called by OpenSSL when determining the current thread ID.
- *
- * @return
- *     An ID which uniquely identifies the current thread.
- */
-static unsigned long guac_common_ssh_openssl_id_callback() {
-    return (unsigned long) pthread_self();
-}
-
-/**
- * Creates the given number of mutexes, such that OpenSSL will have at least
- * this number of mutexes at its disposal.
- *
- * @param count
- *     The number of mutexes (locks) to create.
- */
-static void guac_common_ssh_openssl_init_locks(int count) {
-
-    int i;
-
-    /* Allocate required number of locks */
-    guac_common_ssh_openssl_locks =
-        malloc(sizeof(pthread_mutex_t) * count);
-
-    /* Initialize each lock */
-    for (i=0; i < count; i++)
-        pthread_mutex_init(&(guac_common_ssh_openssl_locks[i]), NULL);
-
-}
-
-/**
- * Frees the given number of mutexes.
- *
- * @param count
- *     The number of mutexes (locks) to free.
- */
-static void guac_common_ssh_openssl_free_locks(int count) {
-
-    int i;
-
-    /* SSL lock array was not initialized */
-    if (guac_common_ssh_openssl_locks == NULL)
-        return;
-
-    /* Free all locks */
-    for (i=0; i < count; i++)
-        pthread_mutex_destroy(&(guac_common_ssh_openssl_locks[i]));
-
-    /* Free lock array */
-    free(guac_common_ssh_openssl_locks);
-
-}
-
-int guac_common_ssh_init(guac_client* client) {
-
-#ifdef LIBSSH2_USES_GCRYPT
-    /* Init threadsafety in libgcrypt */
-    gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
-    if (!gcry_check_version(GCRYPT_VERSION)) {
-        guac_client_log(client, GUAC_LOG_ERROR, "libgcrypt version mismatch.");
-        return 1;
-    }
-#endif
-
-    /* Init threadsafety in OpenSSL */
-    guac_common_ssh_openssl_init_locks(CRYPTO_num_locks());
-    CRYPTO_set_id_callback(guac_common_ssh_openssl_id_callback);
-    CRYPTO_set_locking_callback(guac_common_ssh_openssl_locking_callback);
-
-    /* Init OpenSSL */
-    SSL_library_init();
-    ERR_load_crypto_strings();
-
-    /* Init libssh2 */
-    libssh2_init(0);
-
-    /* Success */
-    return 0;
-
-}
-
-void guac_common_ssh_uninit() {
-    guac_common_ssh_openssl_free_locks(CRYPTO_num_locks());
-}
-
-/**
- * Callback invoked by libssh2 when libssh2_userauth_publickkey() is invoked.
- * This callback must sign the given data, returning the signature as newly-
- * allocated buffer space.
- *
- * @param session
- *     The SSH session for which the signature is being generated.
- *
- * @param sig
- *     A pointer to the buffer space containing the signature. This callback
- *     MUST allocate and assign this space.
- *
- * @param sig_len
- *     The length of the signature within the allocated buffer space, in bytes.
- *     This value must be set to the size of the signature after the signing
- *     operation completes.
- *
- * @param data
- *     The arbitrary data that must be signed.
- *
- * @param data_len
- *     The length of the arbitrary data to be signed, in bytes.
- *
- * @param abstract
- *     The value of the abstract parameter provided with the corresponding call
- *     to libssh2_userauth_publickey().
- *
- * @return
- *     Zero on success, non-zero if the signing operation failed.
- */
-static int guac_common_ssh_sign_callback(LIBSSH2_SESSION* session,
-        unsigned char** sig, size_t* sig_len,
-        const unsigned char* data, size_t data_len, void **abstract) {
-
-    guac_common_ssh_key* key = (guac_common_ssh_key*) abstract;
-    int length;
-
-    /* Allocate space for signature */
-    *sig = malloc(4096);
-
-    /* Sign with key */
-    length = guac_common_ssh_key_sign(key, (const char*) data, data_len, *sig);
-    if (length < 0)
-        return 1;
-
-    *sig_len = length;
-    return 0;
-}
-
-/**
- * Callback for the keyboard-interactive authentication method. Currently
- * supports just one prompt for the password. This callback is invoked as
- * needed to fullfill a call to libssh2_userauth_keyboard_interactive().
- *
- * @param name
- *     An arbitrary name which should be printed to the terminal for the
- *     benefit of the user. This is currently ignored.
- *
- * @param name_len
- *     The length of the name string, in bytes.
- *
- * @param instruction
- *     Arbitrary instructions which should be printed to the terminal for the
- *     benefit of the user. This is currently ignored.
- *
- * @param instruction_len
- *     The length of the instruction string, in bytes.
- *
- * @param num_prompts
- *     The number of keyboard-interactive prompts for which responses are
- *     requested. This callback currently only supports one prompt, and assumes
- *     that this prompt is requesting the password.
- *
- * @param prompts
- *     An array of all keyboard-interactive prompts for which responses are
- *     requested.
- *
- * @param responses
- *     A parallel array into which all prompt responses should be stored. Each
- *     entry within this array corresponds to the entry in the prompts array
- *     with the same index.
- *
- * @param abstract
- *     The value of the abstract parameter provided when the SSH session was
- *     created with libssh2_session_init_ex().
- */
-static void guac_common_ssh_kbd_callback(const char *name, int name_len,
-        const char *instruction, int instruction_len, int num_prompts,
-        const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
-        LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses,
-        void **abstract) {
-
-    guac_common_ssh_session* common_session =
-        (guac_common_ssh_session*) *abstract;
-
-    guac_client* client = common_session->client;
-
-    /* Send password if only one prompt */
-    if (num_prompts == 1) {
-        char* password = common_session->user->password;
-        responses[0].text = strdup(password);
-        responses[0].length = strlen(password);
-    }
-
-    /* If more than one prompt, a single password is not enough */
-    else
-        guac_client_log(client, GUAC_LOG_WARNING,
-                "Unsupported number of keyboard-interactive prompts: %i",
-                num_prompts);
-
-}
-
-/**
- * Authenticates the user associated with the given session over SSH. All
- * required credentials must already be present within the user object
- * associated with the given session.
- *
- * @param session
- *     The session associated with the user to be authenticated.
- *
- * @return
- *     Zero if authentication succeeds, or non-zero if authentication has
- *     failed.
- */
-static int guac_common_ssh_authenticate(guac_common_ssh_session* common_session) {
-
-    guac_client* client = common_session->client;
-    guac_common_ssh_user* user = common_session->user;
-    LIBSSH2_SESSION* session = common_session->session;
-
-    /* Get user credentials */
-    char* username = user->username;
-    char* password = user->password;
-    guac_common_ssh_key* key = user->private_key;
-
-    /* Validate username provided */
-    if (username == NULL) {
-        guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
-                "SSH authentication requires a username.");
-        return 1;
-    }
-
-    /* Get list of supported authentication methods */
-    char* user_authlist = libssh2_userauth_list(session, username,
-            strlen(username));
-    guac_client_log(client, GUAC_LOG_DEBUG,
-            "Supported authentication methods: %s", user_authlist);
-
-    /* Authenticate with private key, if provided */
-    if (key != NULL) {
-
-        /* Check if public key auth is supported on the server */
-        if (strstr(user_authlist, "publickey") == NULL) {
-            guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
-                    "Public key authentication is not supported by "
-                    "the SSH server");
-            return 1;
-        }
-
-        /* Attempt public key auth */
-        if (libssh2_userauth_publickey(session, username,
-                    (unsigned char*) key->public_key, key->public_key_length,
-                    guac_common_ssh_sign_callback, (void**) key)) {
-
-            /* Abort on failure */
-            char* error_message;
-            libssh2_session_last_error(session, &error_message, NULL, 0);
-            guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
-                    "Public key authentication failed: %s", error_message);
-
-            return 1;
-
-        }
-
-        /* Private key authentication succeeded */
-        return 0;
-
-    }
-
-    /* Authenticate with password, if provided */
-    else if (password != NULL) {
-
-        /* Check if password auth is supported on the server */
-        if (strstr(user_authlist, "password") != NULL) {
-
-            /* Attempt password authentication */
-            if (libssh2_userauth_password(session, username, password)) {
-
-                /* Abort on failure */
-                char* error_message;
-                libssh2_session_last_error(session, &error_message, NULL, 0);
-                guac_client_abort(client,
-                        GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
-                        "Password authentication failed: %s", error_message);
-
-                return 1;
-            }
-
-            /* Password authentication succeeded */
-            return 0;
-
-        }
-
-        /* Check if keyboard-interactive auth is supported on the server */
-        if (strstr(user_authlist, "keyboard-interactive") != NULL) {
-
-            /* Attempt keyboard-interactive auth using provided password */
-            if (libssh2_userauth_keyboard_interactive(session, username,
-                        &guac_common_ssh_kbd_callback)) {
-
-                /* Abort on failure */
-                char* error_message;
-                libssh2_session_last_error(session, &error_message, NULL, 0);
-                guac_client_abort(client,
-                        GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
-                        "Keyboard-interactive authentication failed: %s",
-                        error_message);
-
-                return 1;
-            }
-
-            /* Keyboard-interactive authentication succeeded */
-            return 0;
-
-        }
-
-        /* No known authentication types available */
-        guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
-                "Password and keyboard-interactive authentication are not "
-                "supported by the SSH server");
-        return 1;
-
-    }
-
-    /* No credentials provided */
-    guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
-            "SSH authentication requires either a private key or a password.");
-    return 1;
-
-}
-
-guac_common_ssh_session* guac_common_ssh_create_session(guac_client* client,
-        const char* hostname, const char* port, guac_common_ssh_user* user) {
-
-    int retval;
-
-    int fd;
-    struct addrinfo* addresses;
-    struct addrinfo* current_address;
-
-    char connected_address[1024];
-    char connected_port[64];
-
-    struct addrinfo hints = {
-        .ai_family   = AF_UNSPEC,
-        .ai_socktype = SOCK_STREAM,
-        .ai_protocol = IPPROTO_TCP
-    };
-
-    /* Get socket */
-    fd = socket(AF_INET, SOCK_STREAM, 0);
-    if (fd < 0) {
-        guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
-                "Unable to create socket: %s", strerror(errno));
-        return NULL;
-    }
-
-    /* Get addresses connection */
-    if ((retval = getaddrinfo(hostname, port, &hints, &addresses))) {
-        guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
-                "Error parsing given address or port: %s",
-                gai_strerror(retval));
-        close(fd);
-        return NULL;
-    }
-
-    /* Attempt connection to each address until success */
-    current_address = addresses;
-    while (current_address != NULL) {
-
-        /* Resolve hostname */
-        if ((retval = getnameinfo(current_address->ai_addr,
-                current_address->ai_addrlen,
-                connected_address, sizeof(connected_address),
-                connected_port, sizeof(connected_port),
-                NI_NUMERICHOST | NI_NUMERICSERV)))
-            guac_client_log(client, GUAC_LOG_DEBUG,
-                    "Unable to resolve host: %s", gai_strerror(retval));
-
-        /* Connect */
-        if (connect(fd, current_address->ai_addr,
-                        current_address->ai_addrlen) == 0) {
-
-            guac_client_log(client, GUAC_LOG_DEBUG,
-                    "Successfully connected to host %s, port %s",
-                    connected_address, connected_port);
-
-            /* Done if successful connect */
-            break;
-
-        }
-
-        /* Otherwise log information regarding bind failure */
-        else
-            guac_client_log(client, GUAC_LOG_DEBUG, "Unable to connect to "
-                    "host %s, port %s: %s",
-                    connected_address, connected_port, strerror(errno));
-
-        current_address = current_address->ai_next;
-
-    }
-
-    /* Free addrinfo */
-    freeaddrinfo(addresses);
-
-    /* If unable to connect to anything, fail */
-    if (current_address == NULL) {
-        guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR,
-                "Unable to connect to any addresses.");
-        close(fd);
-        return NULL;
-    }
-
-    /* Allocate new session */
-    guac_common_ssh_session* common_session =
-        malloc(sizeof(guac_common_ssh_session));
-
-    /* Open SSH session */
-    LIBSSH2_SESSION* session = libssh2_session_init_ex(NULL, NULL,
-            NULL, common_session);
-    if (session == NULL) {
-        guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
-                "Session allocation failed.");
-        free(common_session);
-        close(fd);
-        return NULL;
-    }
-
-    /* Perform handshake */
-    if (libssh2_session_handshake(session, fd)) {
-        guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR,
-                "SSH handshake failed.");
-        free(common_session);
-        close(fd);
-        return NULL;
-    }
-
-    /* Store basic session data */
-    common_session->client = client;
-    common_session->user = user;
-    common_session->session = session;
-    common_session->fd = fd;
-
-    /* Attempt authentication */
-    if (guac_common_ssh_authenticate(common_session)) {
-        free(common_session);
-        close(fd);
-        return NULL;
-    }
-
-    /* Return created session */
-    return common_session;
-
-}
-
-void guac_common_ssh_destroy_session(guac_common_ssh_session* session) {
-
-    /* Disconnect and clean up libssh2 */
-    libssh2_session_disconnect(session->session, "Bye");
-    libssh2_session_free(session->session);
-
-    /* Free all other data */
-    free(session);
-
-}
-

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/d371f2d9/src/common-ssh/guac_ssh.h
----------------------------------------------------------------------
diff --git a/src/common-ssh/guac_ssh.h b/src/common-ssh/guac_ssh.h
deleted file mode 100644
index 36ad176..0000000
--- a/src/common-ssh/guac_ssh.h
+++ /dev/null
@@ -1,114 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef GUAC_COMMON_SSH_H
-#define GUAC_COMMON_SSH_H
-
-#include "guac_ssh_user.h"
-
-#include <guacamole/client.h>
-#include <libssh2.h>
-
-/**
- * An SSH session, backed by libssh2 and associated with a particular
- * Guacamole client.
- */
-typedef struct guac_common_ssh_session {
-
-    /**
-     * The Guacamole client using this SSH session.
-     */
-    guac_client* client;
-
-    /**
-     * The user that will be authenticating via SSH.
-     */
-    guac_common_ssh_user* user;
-
-    /**
-     * The underlying SSH session from libssh2.
-     */
-    LIBSSH2_SESSION* session;
-
-    /**
-     * The file descriptor of the socket being used for the SSH connection.
-     */
-    int fd;
-
-} guac_common_ssh_session;
-
-/**
- * Initializes the underlying SSH and encryption libraries used by Guacamole.
- * This function must be called before any other guac_common_ssh_*() functions
- * are called.
- *
- * @param client
- *     The Guacamole client that will be using SSH.
- *
- * @return
- *     Zero if initialization, or non-zero if an error occurs.
- */
-int guac_common_ssh_init(guac_client* client);
-
-/**
- * Cleans up the underlying SSH and encryption libraries used by Guacamole.
- * This function must be called once no other guac_common_ssh_*() functions
- * will be used.
- */
-void guac_common_ssh_uninit();
-
-/**
- * Connects to the SSH server running at the given hostname and port, and
- * authenticates as the given user. If an error occurs while connecting or
- * authenticating, the Guacamole client will automatically and fatally abort.
- * The user object provided must eventually be explicitly destroyed, but should
- * not be destroyed until this session is destroyed, assuming the session is
- * successfully created.
- *
- * @param client
- *     The Guacamole client that will be using SSH.
- *
- * @param hostname
- *     The hostname of the SSH server to connect to.
- *
- * @param port
- *     The port to connect to on the given hostname.
- *
- * @param user
- *     The user to authenticate as, once connected.
- *
- * @return
- *     A new SSH session if the connection and authentication succeed, or NULL
- *     if the connection or authentication were not successful.
- */
-guac_common_ssh_session* guac_common_ssh_create_session(guac_client* client,
-        const char* hostname, const char* port, guac_common_ssh_user* user);
-
-/**
- * Disconnects and destroys the given SSH session, freeing all associated
- * resources. Any associated user must be explicitly destroyed, and will not
- * be destroyed automatically.
- *
- * @param session
- *     The SSH session to destroy.
- */
-void guac_common_ssh_destroy_session(guac_common_ssh_session* session);
-
-#endif
-



[9/9] incubator-guacamole-server git commit: Merge 0.9.12-incubating changes back to master.

Posted by jm...@apache.org.
Merge 0.9.12-incubating changes back to master.


Project: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/commit/c4903a8e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/tree/c4903a8e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/diff/c4903a8e

Branch: refs/heads/master
Commit: c4903a8e36eabbaec20ee83a422f599b9db77358
Parents: eb5ef5a 3ff8323
Author: James Muehlner <ja...@guac-dev.org>
Authored: Tue Feb 28 10:32:49 2017 -0800
Committer: James Muehlner <ja...@guac-dev.org>
Committed: Tue Feb 28 10:32:49 2017 -0800

----------------------------------------------------------------------
 configure.ac                              |   2 +-
 doc/Doxyfile                              |   2 +-
 src/common-ssh/Makefile.am                |  24 +-
 src/common-ssh/buffer.c                   | 135 ++++
 src/common-ssh/common-ssh/buffer.h        | 134 ++++
 src/common-ssh/common-ssh/key.h           | 170 +++++
 src/common-ssh/common-ssh/sftp.h          | 242 +++++++
 src/common-ssh/common-ssh/ssh.h           | 114 ++++
 src/common-ssh/common-ssh/user.h          | 108 ++++
 src/common-ssh/guac_sftp.c                | 794 -----------------------
 src/common-ssh/guac_sftp.h                | 242 -------
 src/common-ssh/guac_ssh.c                 | 544 ----------------
 src/common-ssh/guac_ssh.h                 | 114 ----
 src/common-ssh/guac_ssh_buffer.c          | 135 ----
 src/common-ssh/guac_ssh_buffer.h          | 134 ----
 src/common-ssh/guac_ssh_key.c             | 217 -------
 src/common-ssh/guac_ssh_key.h             | 170 -----
 src/common-ssh/guac_ssh_user.c            |  82 ---
 src/common-ssh/guac_ssh_user.h            | 108 ----
 src/common-ssh/key.c                      | 217 +++++++
 src/common-ssh/sftp.c                     | 794 +++++++++++++++++++++++
 src/common-ssh/ssh.c                      | 544 ++++++++++++++++
 src/common-ssh/user.c                     |  82 +++
 src/libguac/Makefile.am                   |   2 +-
 src/protocols/rdp/client.c                |   6 +-
 src/protocols/rdp/rdp.c                   |   6 +-
 src/protocols/rdp/rdp.h                   |   6 +-
 src/protocols/rdp/sftp.c                  |   2 +-
 src/protocols/ssh/client.c                |   4 +-
 src/protocols/ssh/clipboard.c             |   2 +-
 src/protocols/ssh/input.c                 |   2 +-
 src/protocols/ssh/sftp.c                  |   2 +-
 src/protocols/ssh/ssh.c                   |   6 +-
 src/protocols/ssh/ssh.h                   |   8 +-
 src/protocols/telnet/client.c             |   2 +-
 src/protocols/telnet/client.h             |   2 +-
 src/protocols/telnet/clipboard.c          |   2 +-
 src/protocols/telnet/input.c              |   2 +-
 src/protocols/telnet/telnet.c             |   2 +-
 src/protocols/telnet/telnet.h             |   2 +-
 src/protocols/telnet/user.c               |   2 +-
 src/protocols/vnc/client.c                |   6 +-
 src/protocols/vnc/sftp.c                  |   2 +-
 src/protocols/vnc/vnc.c                   |   4 +-
 src/protocols/vnc/vnc.h                   |   6 +-
 src/terminal/Makefile.am                  |  20 +-
 src/terminal/buffer.c                     |   4 +-
 src/terminal/buffer.h                     | 129 ----
 src/terminal/char_mappings.h              |  48 --
 src/terminal/common.c                     |   2 +-
 src/terminal/common.h                     |  53 --
 src/terminal/display.c                    |   6 +-
 src/terminal/display.h                    | 363 -----------
 src/terminal/scrollbar.c                  |   2 +-
 src/terminal/scrollbar.h                  | 357 -----------
 src/terminal/terminal.c                   |  14 +-
 src/terminal/terminal.h                   | 839 -------------------------
 src/terminal/terminal/buffer.h            | 129 ++++
 src/terminal/terminal/char_mappings.h     |  48 ++
 src/terminal/terminal/common.h            |  53 ++
 src/terminal/terminal/display.h           | 363 +++++++++++
 src/terminal/terminal/scrollbar.h         | 357 +++++++++++
 src/terminal/terminal/terminal.h          | 839 +++++++++++++++++++++++++
 src/terminal/terminal/terminal_handlers.h | 165 +++++
 src/terminal/terminal/types.h             | 124 ++++
 src/terminal/terminal/typescript.h        | 184 ++++++
 src/terminal/terminal_handlers.c          |   8 +-
 src/terminal/terminal_handlers.h          | 165 -----
 src/terminal/types.h                      | 124 ----
 src/terminal/typescript.c                 |   2 +-
 src/terminal/typescript.h                 | 184 ------
 71 files changed, 4884 insertions(+), 4884 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/c4903a8e/src/common-ssh/sftp.c
----------------------------------------------------------------------
diff --cc src/common-ssh/sftp.c
index 0000000,79db35e..4be1568
mode 000000,100644..100644
--- a/src/common-ssh/sftp.c
+++ b/src/common-ssh/sftp.c
@@@ -1,0 -1,783 +1,794 @@@
+ /*
+  * Licensed to the Apache Software Foundation (ASF) under one
+  * or more contributor license agreements.  See the NOTICE file
+  * distributed with this work for additional information
+  * regarding copyright ownership.  The ASF licenses this file
+  * to you under the Apache License, Version 2.0 (the
+  * "License"); you may not use this file except in compliance
+  * with the License.  You may obtain a copy of the License at
+  *
+  *   http://www.apache.org/licenses/LICENSE-2.0
+  *
+  * Unless required by applicable law or agreed to in writing,
+  * software distributed under the License is distributed on an
+  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  * KIND, either express or implied.  See the License for the
+  * specific language governing permissions and limitations
+  * under the License.
+  */
+ 
+ #include "common-ssh/sftp.h"
+ #include "common-ssh/ssh.h"
+ 
+ #include <guacamole/client.h>
+ #include <guacamole/object.h>
+ #include <guacamole/protocol.h>
+ #include <guacamole/socket.h>
+ #include <guacamole/user.h>
+ #include <libssh2.h>
+ 
+ #include <fcntl.h>
+ #include <libgen.h>
+ #include <stdlib.h>
+ #include <string.h>
+ 
+ /**
+  * Translates the last error message received by the SFTP layer of an SSH
+  * session into a Guacamole protocol status code.
+  *
+  * @param filesystem
+  *     The object (not guac_object) defining the filesystem associated with the
+  *     SFTP and SSH sessions.
+  *
+  * @return
+  *     The Guacamole protocol status code corresponding to the last reported
+  *     error of the SFTP layer, if nay, or GUAC_PROTOCOL_STATUS_SUCCESS if no
+  *     error has occurred.
+  */
+ static guac_protocol_status guac_sftp_get_status(
+         guac_common_ssh_sftp_filesystem* filesystem) {
+ 
+     /* Get libssh2 objects */
+     LIBSSH2_SFTP*    sftp    = filesystem->sftp_session;
+     LIBSSH2_SESSION* session = filesystem->ssh_session->session;
+ 
+     /* Return success code if no error occurred */
+     if (libssh2_session_last_errno(session) != LIBSSH2_ERROR_SFTP_PROTOCOL)
+         return GUAC_PROTOCOL_STATUS_SUCCESS;
+ 
+     /* Translate SFTP error codes defined by
+      * https://tools.ietf.org/html/draft-ietf-secsh-filexfer-02 (the most
+      * commonly-implemented standard) */
+     switch (libssh2_sftp_last_error(sftp)) {
+ 
+         /* SSH_FX_OK (not an error) */
+         case 0:
+             return GUAC_PROTOCOL_STATUS_SUCCESS;
+ 
+         /* SSH_FX_EOF (technically not an error) */
+         case 1:
+             return GUAC_PROTOCOL_STATUS_SUCCESS;
+ 
+         /* SSH_FX_NO_SUCH_FILE */
+         case 2:
+             return GUAC_PROTOCOL_STATUS_RESOURCE_NOT_FOUND;
+ 
+         /* SSH_FX_PERMISSION_DENIED */
+         case 3:
+             return GUAC_PROTOCOL_STATUS_CLIENT_FORBIDDEN;
+ 
+         /* SSH_FX_FAILURE */
+         case 4:
+             return GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR;
+ 
+         /* SSH_FX_BAD_MESSAGE */
+         case 5:
+             return GUAC_PROTOCOL_STATUS_SERVER_ERROR;
+ 
+         /* SSH_FX_NO_CONNECTION / SSH_FX_CONNECTION_LOST */
+         case 6:
+         case 7:
+             return GUAC_PROTOCOL_STATUS_UPSTREAM_TIMEOUT;
+ 
+         /* SSH_FX_OP_UNSUPPORTED */
+         case 8:
+             return GUAC_PROTOCOL_STATUS_UNSUPPORTED;
+ 
+         /* Return generic error if cause unknown */
+         default:
+             return GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR;
+ 
+     }
+ 
+ }
+ 
+ /**
+  * Concatenates the given filename with the given path, separating the two
+  * with a single forward slash. The full result must be no more than
+  * GUAC_COMMON_SSH_SFTP_MAX_PATH bytes long, counting null terminator.
+  *
+  * @param fullpath
+  *     The buffer to store the result within. This buffer must be at least
+  *     GUAC_COMMON_SSH_SFTP_MAX_PATH bytes long.
+  *
+  * @param path
+  *     The path to append the filename to.
+  *
+  * @param filename
+  *     The filename to append to the path.
+  *
+  * @return
+  *     Non-zero if the filename is valid and was successfully appended to the
+  *     path, zero otherwise.
+  */
+ static int guac_ssh_append_filename(char* fullpath, const char* path,
+         const char* filename) {
+ 
+     int i;
+ 
+     /* Disallow "." as a filename */
+     if (strcmp(filename, ".") == 0)
+         return 0;
+ 
+     /* Disallow ".." as a filename */
+     if (strcmp(filename, "..") == 0)
+         return 0;
+ 
+     /* Copy path, append trailing slash */
+     for (i=0; i<GUAC_COMMON_SSH_SFTP_MAX_PATH; i++) {
+ 
+         /*
+          * Append trailing slash only if:
+          *  1) Trailing slash is not already present
+          *  2) Path is non-empty
+          */
+ 
+         char c = path[i];
+         if (c == '\0') {
+             if (i > 0 && path[i-1] != '/')
+                 fullpath[i++] = '/';
+             break;
+         }
+ 
+         /* Copy character if not end of string */
+         fullpath[i] = c;
+ 
+     }
+ 
+     /* Append filename */
+     for (; i<GUAC_COMMON_SSH_SFTP_MAX_PATH; i++) {
+ 
+         char c = *(filename++);
+         if (c == '\0')
+             break;
+ 
+         /* Filenames may not contain slashes */
+         if (c == '\\' || c == '/')
+             return 0;
+ 
+         /* Append each character within filename */
+         fullpath[i] = c;
+ 
+     }
+ 
+     /* Verify path length is within maximum */
+     if (i == GUAC_COMMON_SSH_SFTP_MAX_PATH)
+         return 0;
+ 
+     /* Terminate path string */
+     fullpath[i] = '\0';
+ 
+     /* Append was successful */
+     return 1;
+ 
+ }
+ 
+ /**
+  * Handler for blob messages which continue an inbound SFTP data transfer
+  * (upload). The data associated with the given stream is expected to be a
+  * pointer to an open LIBSSH2_SFTP_HANDLE for the file to which the data
+  * should be written.
+  *
+  * @param user
+  *     The user receiving the blob message.
+  *
+  * @param stream
+  *     The Guacamole protocol stream associated with the received blob message.
+  *
+  * @param data
+  *     The data received within the blob.
+  *
+  * @param length
+  *     The length of the received data, in bytes.
+  *
+  * @return
+  *     Zero if the blob is handled successfully, or non-zero on error.
+  */
+ static int guac_common_ssh_sftp_blob_handler(guac_user* user,
+         guac_stream* stream, void* data, int length) {
+ 
+     /* Pull file from stream */
+     LIBSSH2_SFTP_HANDLE* file = (LIBSSH2_SFTP_HANDLE*) stream->data;
+ 
+     /* Attempt write */
+     if (libssh2_sftp_write(file, data, length) == length) {
+         guac_user_log(user, GUAC_LOG_DEBUG, "%i bytes written", length);
+         guac_protocol_send_ack(user->socket, stream, "SFTP: OK",
+                 GUAC_PROTOCOL_STATUS_SUCCESS);
+         guac_socket_flush(user->socket);
+     }
+ 
+     /* Inform of any errors */
+     else {
+         guac_user_log(user, GUAC_LOG_INFO, "Unable to write to file");
+         guac_protocol_send_ack(user->socket, stream, "SFTP: Write failed",
+                 GUAC_PROTOCOL_STATUS_SERVER_ERROR);
+         guac_socket_flush(user->socket);
+     }
+ 
+     return 0;
+ 
+ }
+ 
+ /**
+  * Handler for end messages which terminate an inbound SFTP data transfer
+  * (upload). The data associated with the given stream is expected to be a
+  * pointer to an open LIBSSH2_SFTP_HANDLE for the file to which the data
+  * has been written and which should now be closed.
+  *
+  * @param user
+  *     The user receiving the end message.
+  *
+  * @param stream
+  *     The Guacamole protocol stream associated with the received end message.
+  *
+  * @return
+  *     Zero if the file is closed successfully, or non-zero on error.
+  */
+ static int guac_common_ssh_sftp_end_handler(guac_user* user,
+         guac_stream* stream) {
+ 
+     /* Pull file from stream */
+     LIBSSH2_SFTP_HANDLE* file = (LIBSSH2_SFTP_HANDLE*) stream->data;
+ 
+     /* Attempt to close file */
+     if (libssh2_sftp_close(file) == 0) {
+         guac_user_log(user, GUAC_LOG_DEBUG, "File closed");
+         guac_protocol_send_ack(user->socket, stream, "SFTP: OK",
+                 GUAC_PROTOCOL_STATUS_SUCCESS);
+         guac_socket_flush(user->socket);
+     }
+     else {
+         guac_user_log(user, GUAC_LOG_INFO, "Unable to close file");
+         guac_protocol_send_ack(user->socket, stream, "SFTP: Close failed",
+                 GUAC_PROTOCOL_STATUS_SERVER_ERROR);
+         guac_socket_flush(user->socket);
+     }
+ 
+     return 0;
+ 
+ }
+ 
+ int guac_common_ssh_sftp_handle_file_stream(
+         guac_common_ssh_sftp_filesystem* filesystem, guac_user* user,
+         guac_stream* stream, char* mimetype, char* filename) {
+ 
+     char fullpath[GUAC_COMMON_SSH_SFTP_MAX_PATH];
+     LIBSSH2_SFTP_HANDLE* file;
+ 
+     /* Concatenate filename with path */
+     if (!guac_ssh_append_filename(fullpath, filesystem->upload_path,
+                 filename)) {
+ 
+         guac_user_log(user, GUAC_LOG_DEBUG,
+                 "Filename \"%s\" is invalid or resulting path is too long",
+                 filename);
+ 
+         /* Abort transfer - invalid filename */
+         guac_protocol_send_ack(user->socket, stream, 
+                 "SFTP: Illegal filename",
+                 GUAC_PROTOCOL_STATUS_CLIENT_BAD_REQUEST);
+ 
+         guac_socket_flush(user->socket);
+         return 0;
+     }
+ 
+     /* Open file via SFTP */
+     file = libssh2_sftp_open(filesystem->sftp_session, fullpath,
+             LIBSSH2_FXF_WRITE | LIBSSH2_FXF_CREAT | LIBSSH2_FXF_TRUNC,
+             S_IRUSR | S_IWUSR);
+ 
+     /* Inform of status */
+     if (file != NULL) {
+ 
+         guac_user_log(user, GUAC_LOG_DEBUG,
+                 "File \"%s\" opened",
+                 fullpath);
+ 
+         guac_protocol_send_ack(user->socket, stream, "SFTP: File opened",
+                 GUAC_PROTOCOL_STATUS_SUCCESS);
+         guac_socket_flush(user->socket);
+     }
+     else {
+         guac_user_log(user, GUAC_LOG_INFO,
+                 "Unable to open file \"%s\"", fullpath);
+         guac_protocol_send_ack(user->socket, stream, "SFTP: Open failed",
+                 guac_sftp_get_status(filesystem));
+         guac_socket_flush(user->socket);
+     }
+ 
+     /* Set handlers for file stream */
+     stream->blob_handler = guac_common_ssh_sftp_blob_handler;
+     stream->end_handler = guac_common_ssh_sftp_end_handler;
+ 
+     /* Store file within stream */
+     stream->data = file;
+     return 0;
+ 
+ }
+ 
+ /**
+  * Handler for ack messages which continue an outbound SFTP data transfer
+  * (download), signalling the current status and requesting additional data.
+  * The data associated with the given stream is expected to be a pointer to an
+  * open LIBSSH2_SFTP_HANDLE for the file from which the data is to be read.
+  *
+  * @param user
+  *     The user receiving the ack message.
+  *
+  * @param stream
+  *     The Guacamole protocol stream associated with the received ack message.
+  *
+  * @param message
+  *     An arbitrary human-readable message describing the nature of the
+  *     success or failure denoted by the ack message.
+  *
+  * @param status
+  *     The status code associated with the ack message, which may indicate
+  *     success or an error.
+  *
+  * @return
+  *     Zero if the file is read from successfully, or non-zero on error.
+  */
+ static int guac_common_ssh_sftp_ack_handler(guac_user* user,
+         guac_stream* stream, char* message, guac_protocol_status status) {
+ 
+     /* Pull file from stream */
+     LIBSSH2_SFTP_HANDLE* file = (LIBSSH2_SFTP_HANDLE*) stream->data;
+ 
+     /* If successful, read data */
+     if (status == GUAC_PROTOCOL_STATUS_SUCCESS) {
+ 
+         /* Attempt read into buffer */
+         char buffer[4096];
+         int bytes_read = libssh2_sftp_read(file, buffer, sizeof(buffer)); 
+ 
+         /* If bytes read, send as blob */
+         if (bytes_read > 0) {
+             guac_protocol_send_blob(user->socket, stream,
+                     buffer, bytes_read);
+ 
+             guac_user_log(user, GUAC_LOG_DEBUG, "%i bytes sent to user",
+                     bytes_read);
+ 
+         }
+ 
 -        /* If EOF, send end */
 -        else if (bytes_read == 0) {
 -            guac_user_log(user, GUAC_LOG_DEBUG, "File sent");
 -            guac_protocol_send_end(user->socket, stream);
 -            guac_user_free_stream(user, stream);
 -        }
 -
 -        /* Otherwise, fail stream */
++        /* If bytes could not be read, handle EOF or error condition */
+         else {
 -            guac_user_log(user, GUAC_LOG_INFO, "Error reading file");
 -            guac_protocol_send_end(user->socket, stream);
 -            guac_user_free_stream(user, stream);
++
++            /* If EOF, send end */
++            if (bytes_read == 0) {
++                guac_user_log(user, GUAC_LOG_DEBUG, "File sent");
++                guac_protocol_send_end(user->socket, stream);
++                guac_user_free_stream(user, stream);
++            }
++
++            /* Otherwise, fail stream */
++            else {
++                guac_user_log(user, GUAC_LOG_INFO, "Error reading file");
++                guac_protocol_send_end(user->socket, stream);
++                guac_user_free_stream(user, stream);
++            }
++
++            /* Close file */
++            if (libssh2_sftp_close(file) == 0)
++                guac_user_log(user, GUAC_LOG_DEBUG, "File closed");
++            else
++                guac_user_log(user, GUAC_LOG_INFO, "Unable to close file");
++
+         }
+ 
+         guac_socket_flush(user->socket);
+ 
+     }
+ 
+     /* Otherwise, return stream to user */
+     else
+         guac_user_free_stream(user, stream);
+ 
+     return 0;
+ }
+ 
+ guac_stream* guac_common_ssh_sftp_download_file(
+         guac_common_ssh_sftp_filesystem* filesystem, guac_user* user,
+         char* filename) {
+ 
+     guac_stream* stream;
+     LIBSSH2_SFTP_HANDLE* file;
+ 
+     /* Attempt to open file for reading */
+     file = libssh2_sftp_open(filesystem->sftp_session, filename,
+             LIBSSH2_FXF_READ, 0);
+     if (file == NULL) {
+         guac_user_log(user, GUAC_LOG_INFO, 
+                 "Unable to read file \"%s\"", filename);
+         return NULL;
+     }
+ 
+     /* Allocate stream */
+     stream = guac_user_alloc_stream(user);
+     stream->ack_handler = guac_common_ssh_sftp_ack_handler;
+     stream->data = file;
+ 
+     /* Send stream start, strip name */
+     filename = basename(filename);
+     guac_protocol_send_file(user->socket, stream,
+             "application/octet-stream", filename);
+     guac_socket_flush(user->socket);
+ 
+     guac_user_log(user, GUAC_LOG_DEBUG, "Sending file \"%s\"", filename);
+     return stream;
+ 
+ }
+ 
+ void guac_common_ssh_sftp_set_upload_path(
+         guac_common_ssh_sftp_filesystem* filesystem, const char* path) {
+ 
+     guac_client* client = filesystem->ssh_session->client;
+ 
+     /* Ignore requests which exceed maximum-allowed path */
+     int length = strnlen(path, GUAC_COMMON_SSH_SFTP_MAX_PATH)+1;
+     if (length > GUAC_COMMON_SSH_SFTP_MAX_PATH) {
+         guac_client_log(client, GUAC_LOG_ERROR,
+                 "Submitted path exceeds limit of %i bytes",
+                 GUAC_COMMON_SSH_SFTP_MAX_PATH);
+         return;
+     }
+ 
+     /* Copy path */
+     memcpy(filesystem->upload_path, path, length);
+     guac_client_log(client, GUAC_LOG_DEBUG, "Upload path set to \"%s\"", path);
+ 
+ }
+ 
+ /**
+  * Handler for ack messages received due to receipt of a "body" or "blob"
+  * instruction associated with a SFTP directory list operation.
+  *
+  * @param user
+  *     The user receiving the ack message.
+  *
+  * @param stream
+  *     The Guacamole protocol stream associated with the received ack message.
+  *
+  * @param message
+  *     An arbitrary human-readable message describing the nature of the
+  *     success or failure denoted by this ack message.
+  *
+  * @param status
+  *     The status code associated with this ack message, which may indicate
+  *     success or an error.
+  *
+  * @return
+  *     Zero on success, non-zero on error.
+  */
+ static int guac_common_ssh_sftp_ls_ack_handler(guac_user* user,
+         guac_stream* stream, char* message, guac_protocol_status status) {
+ 
+     int bytes_read;
+     int blob_written = 0;
+ 
+     char filename[GUAC_COMMON_SSH_SFTP_MAX_PATH];
+     LIBSSH2_SFTP_ATTRIBUTES attributes;
+ 
+     guac_common_ssh_sftp_ls_state* list_state =
+         (guac_common_ssh_sftp_ls_state*) stream->data;
+ 
+     guac_common_ssh_sftp_filesystem* filesystem = list_state->filesystem;
+ 
+     LIBSSH2_SFTP* sftp = filesystem->sftp_session;
+ 
+     /* If unsuccessful, free stream and abort */
+     if (status != GUAC_PROTOCOL_STATUS_SUCCESS) {
+         libssh2_sftp_closedir(list_state->directory);
+         guac_user_free_stream(user, stream);
+         free(list_state);
+         return 0;
+     }
+ 
+     /* While directory entries remain */
+     while ((bytes_read = libssh2_sftp_readdir(list_state->directory,
+                 filename, sizeof(filename), &attributes)) > 0
+             && !blob_written) {
+ 
+         char absolute_path[GUAC_COMMON_SSH_SFTP_MAX_PATH];
+ 
+         /* Skip current and parent directory entries */
+         if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0)
+             continue;
+ 
+         /* Concatenate into absolute path - skip if invalid */
+         if (!guac_ssh_append_filename(absolute_path, 
+                     list_state->directory_name, filename)) {
+ 
+             guac_user_log(user, GUAC_LOG_DEBUG,
+                     "Skipping filename \"%s\" - filename is invalid or "
+                     "resulting path is too long", filename);
+ 
+             continue;
+         }
+ 
+         /* Stat explicitly if symbolic link (might point to directory) */
+         if (LIBSSH2_SFTP_S_ISLNK(attributes.permissions))
+             libssh2_sftp_stat(sftp, absolute_path, &attributes);
+ 
+         /* Determine mimetype */
+         const char* mimetype;
+         if (LIBSSH2_SFTP_S_ISDIR(attributes.permissions))
+             mimetype = GUAC_USER_STREAM_INDEX_MIMETYPE;
+         else
+             mimetype = "application/octet-stream";
+ 
+         /* Write entry */
+         blob_written |= guac_common_json_write_property(user, stream,
+                 &list_state->json_state, absolute_path, mimetype);
+ 
+     }
+ 
+     /* Complete JSON and cleanup at end of directory */
+     if (bytes_read <= 0) {
+ 
+         /* Complete JSON object */
+         guac_common_json_end_object(user, stream, &list_state->json_state);
+         guac_common_json_flush(user, stream, &list_state->json_state);
+ 
+         /* Clean up resources */
+         libssh2_sftp_closedir(list_state->directory);
+         free(list_state);
+ 
+         /* Signal of stream */
+         guac_protocol_send_end(user->socket, stream);
+         guac_user_free_stream(user, stream);
+ 
+     }
+ 
+     guac_socket_flush(user->socket);
+     return 0;
+ 
+ }
+ 
+ /**
+  * Handler for get messages. In context of SFTP and the filesystem exposed via
+  * the Guacamole protocol, get messages request the body of a file within the
+  * filesystem.
+  *
+  * @param user
+  *     The user who sent the get message.
+  *
+  * @param object
+  *     The Guacamole protocol object associated with the get request itself.
+  *
+  * @param name
+  *     The name of the input stream (file) being requested.
+  *
+  * @return
+  *     Zero on success, non-zero on error.
+  */
+ static int guac_common_ssh_sftp_get_handler(guac_user* user,
+         guac_object* object, char* name) {
+ 
+     guac_common_ssh_sftp_filesystem* filesystem =
+         (guac_common_ssh_sftp_filesystem*) object->data;
+ 
+     LIBSSH2_SFTP* sftp = filesystem->sftp_session;
+     LIBSSH2_SFTP_ATTRIBUTES attributes;
+ 
+     /* Attempt to read file information */
+     if (libssh2_sftp_stat(sftp, name, &attributes)) {
+         guac_user_log(user, GUAC_LOG_INFO, "Unable to read file \"%s\"",
+                 name);
+         return 0;
+     }
+ 
+     /* If directory, send contents of directory */
+     if (LIBSSH2_SFTP_S_ISDIR(attributes.permissions)) {
+ 
+         /* Open as directory */
+         LIBSSH2_SFTP_HANDLE* dir = libssh2_sftp_opendir(sftp, name);
+         if (dir == NULL) {
+             guac_user_log(user, GUAC_LOG_INFO,
+                     "Unable to read directory \"%s\"", name);
+             return 0;
+         }
+ 
+         /* Init directory listing state */
+         guac_common_ssh_sftp_ls_state* list_state =
+             malloc(sizeof(guac_common_ssh_sftp_ls_state));
+ 
+         list_state->directory = dir;
+         list_state->filesystem = filesystem;
+         strncpy(list_state->directory_name, name,
+                 sizeof(list_state->directory_name) - 1);
+ 
+         /* Allocate stream for body */
+         guac_stream* stream = guac_user_alloc_stream(user);
+         stream->ack_handler = guac_common_ssh_sftp_ls_ack_handler;
+         stream->data = list_state;
+ 
+         /* Init JSON object state */
+         guac_common_json_begin_object(user, stream, &list_state->json_state);
+ 
+         /* Associate new stream with get request */
+         guac_protocol_send_body(user->socket, object, stream,
+                 GUAC_USER_STREAM_INDEX_MIMETYPE, name);
+ 
+     }
+ 
+     /* Otherwise, send file contents */
+     else {
+ 
+         /* Open as normal file */
+         LIBSSH2_SFTP_HANDLE* file = libssh2_sftp_open(sftp, name,
+             LIBSSH2_FXF_READ, 0);
+         if (file == NULL) {
+             guac_user_log(user, GUAC_LOG_INFO,
+                     "Unable to read file \"%s\"", name);
+             return 0;
+         }
+ 
+         /* Allocate stream for body */
+         guac_stream* stream = guac_user_alloc_stream(user);
+         stream->ack_handler = guac_common_ssh_sftp_ack_handler;
+         stream->data = file;
+ 
+         /* Associate new stream with get request */
+         guac_protocol_send_body(user->socket, object, stream,
+                 "application/octet-stream", name);
+ 
+     }
+ 
+     guac_socket_flush(user->socket);
+     return 0;
+ }
+ 
+ /**
+  * Handler for put messages. In context of SFTP and the filesystem exposed via
+  * the Guacamole protocol, put messages request write access to a file within
+  * the filesystem.
+  *
+  * @param user
+  *     The user who sent the put message.
+  *
+  * @param object
+  *     The Guacamole protocol object associated with the put request itself.
+  *
+  * @param stream
+  *     The Guacamole protocol stream along which the user will be sending
+  *     file data.
+  *
+  * @param mimetype
+  *     The mimetype of the data being send along the stream.
+  *
+  * @param name
+  *     The name of the input stream (file) being requested.
+  *
+  * @return
+  *     Zero on success, non-zero on error.
+  */
+ static int guac_common_ssh_sftp_put_handler(guac_user* user,
+         guac_object* object, guac_stream* stream, char* mimetype, char* name) {
+ 
+     guac_common_ssh_sftp_filesystem* filesystem =
+         (guac_common_ssh_sftp_filesystem*) object->data;
+ 
+     LIBSSH2_SFTP* sftp = filesystem->sftp_session;
+ 
+     /* Open file via SFTP */
+     LIBSSH2_SFTP_HANDLE* file = libssh2_sftp_open(sftp, name,
+             LIBSSH2_FXF_WRITE | LIBSSH2_FXF_CREAT | LIBSSH2_FXF_TRUNC,
+             S_IRUSR | S_IWUSR);
+ 
+     /* Acknowledge stream if successful */
+     if (file != NULL) {
+         guac_user_log(user, GUAC_LOG_DEBUG, "File \"%s\" opened", name);
+         guac_protocol_send_ack(user->socket, stream, "SFTP: File opened",
+                 GUAC_PROTOCOL_STATUS_SUCCESS);
+     }
+ 
+     /* Abort on failure */
+     else {
+         guac_user_log(user, GUAC_LOG_INFO,
+                 "Unable to open file \"%s\"", name);
+         guac_protocol_send_ack(user->socket, stream, "SFTP: Open failed",
+                 guac_sftp_get_status(filesystem));
+     }
+ 
+     /* Set handlers for file stream */
+     stream->blob_handler = guac_common_ssh_sftp_blob_handler;
+     stream->end_handler = guac_common_ssh_sftp_end_handler;
+ 
+     /* Store file within stream */
+     stream->data = file;
+ 
+     guac_socket_flush(user->socket);
+     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) {
+ 
+     /* Init filesystem */
+     guac_object* fs_object = guac_user_alloc_object(user);
+     fs_object->get_handler = guac_common_ssh_sftp_get_handler;
+     fs_object->put_handler = guac_common_ssh_sftp_put_handler;
+     fs_object->data = filesystem;
+ 
+     /* Send filesystem to user */
+     guac_protocol_send_filesystem(user->socket, fs_object, filesystem->name);
+     guac_socket_flush(user->socket);
+ 
+     return fs_object;
+ 
+ }
+ 
+ guac_common_ssh_sftp_filesystem* guac_common_ssh_create_sftp_filesystem(
+         guac_common_ssh_session* session, const char* name) {
+ 
+     /* Request SFTP */
+     LIBSSH2_SFTP* sftp_session = libssh2_sftp_init(session->session);
+     if (sftp_session == NULL)
+         return NULL;
+ 
+     /* Allocate data for SFTP session */
+     guac_common_ssh_sftp_filesystem* 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;
+ 
+     /* Initially upload files to current directory */
+     strcpy(filesystem->upload_path, ".");
+ 
+     /* Return allocated filesystem */
+     return filesystem;
+ 
+ }
+ 
+ void guac_common_ssh_destroy_sftp_filesystem(
+         guac_common_ssh_sftp_filesystem* filesystem) {
+ 
+     /* Shutdown SFTP session */
+     libssh2_sftp_shutdown(filesystem->sftp_session);
+ 
+     /* Free associated memory */
+     free(filesystem->name);
+     free(filesystem);
+ 
+ }
+ 

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/c4903a8e/src/common-ssh/ssh.c
----------------------------------------------------------------------
diff --cc src/common-ssh/ssh.c
index 0000000,9d3de19..e4b0d38
mode 000000,100644..100644
--- a/src/common-ssh/ssh.c
+++ b/src/common-ssh/ssh.c
@@@ -1,0 -1,544 +1,544 @@@
+ /*
+  * Licensed to the Apache Software Foundation (ASF) under one
+  * or more contributor license agreements.  See the NOTICE file
+  * distributed with this work for additional information
+  * regarding copyright ownership.  The ASF licenses this file
+  * to you under the Apache License, Version 2.0 (the
+  * "License"); you may not use this file except in compliance
+  * with the License.  You may obtain a copy of the License at
+  *
+  *   http://www.apache.org/licenses/LICENSE-2.0
+  *
+  * Unless required by applicable law or agreed to in writing,
+  * software distributed under the License is distributed on an
+  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  * KIND, either express or implied.  See the License for the
+  * specific language governing permissions and limitations
+  * under the License.
+  */
+ 
+ #include "common-ssh/key.h"
+ #include "common-ssh/ssh.h"
+ #include "common-ssh/user.h"
+ 
+ #include <guacamole/client.h>
+ #include <libssh2.h>
+ 
+ #ifdef LIBSSH2_USES_GCRYPT
+ #include <gcrypt.h>
+ #endif
+ 
+ #include <openssl/err.h>
+ #include <openssl/ssl.h>
+ 
+ #include <errno.h>
+ #include <netdb.h>
+ #include <netinet/in.h>
+ #include <pthread.h>
+ #include <stddef.h>
+ #include <stdlib.h>
+ #include <string.h>
+ #include <sys/socket.h>
+ #include <unistd.h>
+ 
+ #ifdef LIBSSH2_USES_GCRYPT
+ GCRY_THREAD_OPTION_PTHREAD_IMPL;
+ #endif
+ 
+ /**
+  * Array of mutexes, used by OpenSSL.
+  */
+ static pthread_mutex_t* guac_common_ssh_openssl_locks = NULL;
+ 
+ /**
+  * Called by OpenSSL when locking or unlocking the Nth mutex.
+  *
+  * @param mode
+  *     A bitmask denoting the action to be taken on the Nth lock, such as
+  *     CRYPTO_LOCK or CRYPTO_UNLOCK.
+  *
+  * @param n
+  *     The index of the lock to lock or unlock.
+  *
+  * @param file
+  *     The filename of the function setting the lock, for debugging purposes.
+  *
+  * @param line
+  *     The line number of the function setting the lock, for debugging
+  *     purposes.
+  */
+ static void guac_common_ssh_openssl_locking_callback(int mode, int n,
+         const char* file, int line){
+ 
+     /* Lock given mutex upon request */
+     if (mode & CRYPTO_LOCK)
+         pthread_mutex_lock(&(guac_common_ssh_openssl_locks[n]));
+ 
+     /* Unlock given mutex upon request */
+     else if (mode & CRYPTO_UNLOCK)
+         pthread_mutex_unlock(&(guac_common_ssh_openssl_locks[n]));
+ 
+ }
+ 
+ /**
+  * Called by OpenSSL when determining the current thread ID.
+  *
+  * @return
+  *     An ID which uniquely identifies the current thread.
+  */
+ static unsigned long guac_common_ssh_openssl_id_callback() {
+     return (unsigned long) pthread_self();
+ }
+ 
+ /**
+  * Creates the given number of mutexes, such that OpenSSL will have at least
+  * this number of mutexes at its disposal.
+  *
+  * @param count
+  *     The number of mutexes (locks) to create.
+  */
+ static void guac_common_ssh_openssl_init_locks(int count) {
+ 
+     int i;
+ 
+     /* Allocate required number of locks */
+     guac_common_ssh_openssl_locks =
+         malloc(sizeof(pthread_mutex_t) * count);
+ 
+     /* Initialize each lock */
+     for (i=0; i < count; i++)
+         pthread_mutex_init(&(guac_common_ssh_openssl_locks[i]), NULL);
+ 
+ }
+ 
+ /**
+  * Frees the given number of mutexes.
+  *
+  * @param count
+  *     The number of mutexes (locks) to free.
+  */
+ static void guac_common_ssh_openssl_free_locks(int count) {
+ 
+     int i;
+ 
+     /* SSL lock array was not initialized */
+     if (guac_common_ssh_openssl_locks == NULL)
+         return;
+ 
+     /* Free all locks */
+     for (i=0; i < count; i++)
+         pthread_mutex_destroy(&(guac_common_ssh_openssl_locks[i]));
+ 
+     /* Free lock array */
+     free(guac_common_ssh_openssl_locks);
+ 
+ }
+ 
+ int guac_common_ssh_init(guac_client* client) {
+ 
+ #ifdef LIBSSH2_USES_GCRYPT
+     /* Init threadsafety in libgcrypt */
+     gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
+     if (!gcry_check_version(GCRYPT_VERSION)) {
+         guac_client_log(client, GUAC_LOG_ERROR, "libgcrypt version mismatch.");
+         return 1;
+     }
+ #endif
+ 
+     /* Init threadsafety in OpenSSL */
+     guac_common_ssh_openssl_init_locks(CRYPTO_num_locks());
+     CRYPTO_set_id_callback(guac_common_ssh_openssl_id_callback);
+     CRYPTO_set_locking_callback(guac_common_ssh_openssl_locking_callback);
+ 
+     /* Init OpenSSL */
+     SSL_library_init();
+     ERR_load_crypto_strings();
+ 
+     /* Init libssh2 */
+     libssh2_init(0);
+ 
+     /* Success */
+     return 0;
+ 
+ }
+ 
+ void guac_common_ssh_uninit() {
+     guac_common_ssh_openssl_free_locks(CRYPTO_num_locks());
+ }
+ 
+ /**
+  * Callback invoked by libssh2 when libssh2_userauth_publickkey() is invoked.
+  * This callback must sign the given data, returning the signature as newly-
+  * allocated buffer space.
+  *
+  * @param session
+  *     The SSH session for which the signature is being generated.
+  *
+  * @param sig
+  *     A pointer to the buffer space containing the signature. This callback
+  *     MUST allocate and assign this space.
+  *
+  * @param sig_len
+  *     The length of the signature within the allocated buffer space, in bytes.
+  *     This value must be set to the size of the signature after the signing
+  *     operation completes.
+  *
+  * @param data
+  *     The arbitrary data that must be signed.
+  *
+  * @param data_len
+  *     The length of the arbitrary data to be signed, in bytes.
+  *
+  * @param abstract
+  *     The value of the abstract parameter provided with the corresponding call
+  *     to libssh2_userauth_publickey().
+  *
+  * @return
+  *     Zero on success, non-zero if the signing operation failed.
+  */
+ static int guac_common_ssh_sign_callback(LIBSSH2_SESSION* session,
+         unsigned char** sig, size_t* sig_len,
+         const unsigned char* data, size_t data_len, void **abstract) {
+ 
+     guac_common_ssh_key* key = (guac_common_ssh_key*) abstract;
+     int length;
+ 
+     /* Allocate space for signature */
+     *sig = malloc(4096);
+ 
+     /* Sign with key */
+     length = guac_common_ssh_key_sign(key, (const char*) data, data_len, *sig);
+     if (length < 0)
+         return 1;
+ 
+     *sig_len = length;
+     return 0;
+ }
+ 
+ /**
+  * Callback for the keyboard-interactive authentication method. Currently
+  * supports just one prompt for the password. This callback is invoked as
+  * needed to fullfill a call to libssh2_userauth_keyboard_interactive().
+  *
+  * @param name
+  *     An arbitrary name which should be printed to the terminal for the
+  *     benefit of the user. This is currently ignored.
+  *
+  * @param name_len
+  *     The length of the name string, in bytes.
+  *
+  * @param instruction
+  *     Arbitrary instructions which should be printed to the terminal for the
+  *     benefit of the user. This is currently ignored.
+  *
+  * @param instruction_len
+  *     The length of the instruction string, in bytes.
+  *
+  * @param num_prompts
+  *     The number of keyboard-interactive prompts for which responses are
+  *     requested. This callback currently only supports one prompt, and assumes
+  *     that this prompt is requesting the password.
+  *
+  * @param prompts
+  *     An array of all keyboard-interactive prompts for which responses are
+  *     requested.
+  *
+  * @param responses
+  *     A parallel array into which all prompt responses should be stored. Each
+  *     entry within this array corresponds to the entry in the prompts array
+  *     with the same index.
+  *
+  * @param abstract
+  *     The value of the abstract parameter provided when the SSH session was
+  *     created with libssh2_session_init_ex().
+  */
+ static void guac_common_ssh_kbd_callback(const char *name, int name_len,
+         const char *instruction, int instruction_len, int num_prompts,
+         const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
+         LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses,
+         void **abstract) {
+ 
+     guac_common_ssh_session* common_session =
+         (guac_common_ssh_session*) *abstract;
+ 
+     guac_client* client = common_session->client;
+ 
+     /* Send password if only one prompt */
+     if (num_prompts == 1) {
+         char* password = common_session->user->password;
+         responses[0].text = strdup(password);
+         responses[0].length = strlen(password);
+     }
+ 
+     /* If more than one prompt, a single password is not enough */
+     else
+         guac_client_log(client, GUAC_LOG_WARNING,
+                 "Unsupported number of keyboard-interactive prompts: %i",
+                 num_prompts);
+ 
+ }
+ 
+ /**
+  * Authenticates the user associated with the given session over SSH. All
+  * required credentials must already be present within the user object
+  * associated with the given session.
+  *
+  * @param session
+  *     The session associated with the user to be authenticated.
+  *
+  * @return
+  *     Zero if authentication succeeds, or non-zero if authentication has
+  *     failed.
+  */
+ static int guac_common_ssh_authenticate(guac_common_ssh_session* common_session) {
+ 
+     guac_client* client = common_session->client;
+     guac_common_ssh_user* user = common_session->user;
+     LIBSSH2_SESSION* session = common_session->session;
+ 
+     /* Get user credentials */
+     char* username = user->username;
+     char* password = user->password;
+     guac_common_ssh_key* key = user->private_key;
+ 
+     /* Validate username provided */
+     if (username == NULL) {
+         guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
+                 "SSH authentication requires a username.");
+         return 1;
+     }
+ 
+     /* Get list of supported authentication methods */
+     char* user_authlist = libssh2_userauth_list(session, username,
+             strlen(username));
+     guac_client_log(client, GUAC_LOG_DEBUG,
+             "Supported authentication methods: %s", user_authlist);
+ 
+     /* Authenticate with private key, if provided */
+     if (key != NULL) {
+ 
+         /* Check if public key auth is supported on the server */
+         if (strstr(user_authlist, "publickey") == NULL) {
+             guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
+                     "Public key authentication is not supported by "
+                     "the SSH server");
+             return 1;
+         }
+ 
+         /* Attempt public key auth */
+         if (libssh2_userauth_publickey(session, username,
+                     (unsigned char*) key->public_key, key->public_key_length,
+                     guac_common_ssh_sign_callback, (void**) key)) {
+ 
+             /* Abort on failure */
+             char* error_message;
+             libssh2_session_last_error(session, &error_message, NULL, 0);
+             guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
+                     "Public key authentication failed: %s", error_message);
+ 
+             return 1;
+ 
+         }
+ 
+         /* Private key authentication succeeded */
+         return 0;
+ 
+     }
+ 
+     /* Authenticate with password, if provided */
+     else if (password != NULL) {
+ 
+         /* Check if password auth is supported on the server */
+         if (strstr(user_authlist, "password") != NULL) {
+ 
+             /* Attempt password authentication */
+             if (libssh2_userauth_password(session, username, password)) {
+ 
+                 /* Abort on failure */
+                 char* error_message;
+                 libssh2_session_last_error(session, &error_message, NULL, 0);
+                 guac_client_abort(client,
+                         GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
+                         "Password authentication failed: %s", error_message);
+ 
+                 return 1;
+             }
+ 
+             /* Password authentication succeeded */
+             return 0;
+ 
+         }
+ 
+         /* Check if keyboard-interactive auth is supported on the server */
+         if (strstr(user_authlist, "keyboard-interactive") != NULL) {
+ 
+             /* Attempt keyboard-interactive auth using provided password */
+             if (libssh2_userauth_keyboard_interactive(session, username,
+                         &guac_common_ssh_kbd_callback)) {
+ 
+                 /* Abort on failure */
+                 char* error_message;
+                 libssh2_session_last_error(session, &error_message, NULL, 0);
+                 guac_client_abort(client,
+                         GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
+                         "Keyboard-interactive authentication failed: %s",
+                         error_message);
+ 
+                 return 1;
+             }
+ 
+             /* Keyboard-interactive authentication succeeded */
+             return 0;
+ 
+         }
+ 
+         /* No known authentication types available */
+         guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
+                 "Password and keyboard-interactive authentication are not "
+                 "supported by the SSH server");
+         return 1;
+ 
+     }
+ 
+     /* No credentials provided */
+     guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
+             "SSH authentication requires either a private key or a password.");
+     return 1;
+ 
+ }
+ 
+ guac_common_ssh_session* guac_common_ssh_create_session(guac_client* client,
+         const char* hostname, const char* port, guac_common_ssh_user* user) {
+ 
+     int retval;
+ 
+     int fd;
+     struct addrinfo* addresses;
+     struct addrinfo* current_address;
+ 
+     char connected_address[1024];
+     char connected_port[64];
+ 
+     struct addrinfo hints = {
+         .ai_family   = AF_UNSPEC,
+         .ai_socktype = SOCK_STREAM,
+         .ai_protocol = IPPROTO_TCP
+     };
+ 
+     /* Get socket */
+     fd = socket(AF_INET, SOCK_STREAM, 0);
+     if (fd < 0) {
+         guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
+                 "Unable to create socket: %s", strerror(errno));
+         return NULL;
+     }
+ 
+     /* Get addresses connection */
+     if ((retval = getaddrinfo(hostname, port, &hints, &addresses))) {
+         guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
+                 "Error parsing given address or port: %s",
+                 gai_strerror(retval));
+         close(fd);
+         return NULL;
+     }
+ 
+     /* Attempt connection to each address until success */
+     current_address = addresses;
+     while (current_address != NULL) {
+ 
+         /* Resolve hostname */
+         if ((retval = getnameinfo(current_address->ai_addr,
+                 current_address->ai_addrlen,
+                 connected_address, sizeof(connected_address),
+                 connected_port, sizeof(connected_port),
+                 NI_NUMERICHOST | NI_NUMERICSERV)))
+             guac_client_log(client, GUAC_LOG_DEBUG,
+                     "Unable to resolve host: %s", gai_strerror(retval));
+ 
+         /* Connect */
+         if (connect(fd, current_address->ai_addr,
+                         current_address->ai_addrlen) == 0) {
+ 
+             guac_client_log(client, GUAC_LOG_DEBUG,
+                     "Successfully connected to host %s, port %s",
+                     connected_address, connected_port);
+ 
+             /* Done if successful connect */
+             break;
+ 
+         }
+ 
+         /* Otherwise log information regarding bind failure */
+         else
+             guac_client_log(client, GUAC_LOG_DEBUG, "Unable to connect to "
+                     "host %s, port %s: %s",
+                     connected_address, connected_port, strerror(errno));
+ 
+         current_address = current_address->ai_next;
+ 
+     }
+ 
+     /* Free addrinfo */
+     freeaddrinfo(addresses);
+ 
+     /* If unable to connect to anything, fail */
+     if (current_address == NULL) {
 -        guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR,
++        guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_NOT_FOUND,
+                 "Unable to connect to any addresses.");
+         close(fd);
+         return NULL;
+     }
+ 
+     /* Allocate new session */
+     guac_common_ssh_session* common_session =
+         malloc(sizeof(guac_common_ssh_session));
+ 
+     /* Open SSH session */
+     LIBSSH2_SESSION* session = libssh2_session_init_ex(NULL, NULL,
+             NULL, common_session);
+     if (session == NULL) {
+         guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
+                 "Session allocation failed.");
+         free(common_session);
+         close(fd);
+         return NULL;
+     }
+ 
+     /* Perform handshake */
+     if (libssh2_session_handshake(session, fd)) {
+         guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR,
+                 "SSH handshake failed.");
+         free(common_session);
+         close(fd);
+         return NULL;
+     }
+ 
+     /* Store basic session data */
+     common_session->client = client;
+     common_session->user = user;
+     common_session->session = session;
+     common_session->fd = fd;
+ 
+     /* Attempt authentication */
+     if (guac_common_ssh_authenticate(common_session)) {
+         free(common_session);
+         close(fd);
+         return NULL;
+     }
+ 
+     /* Return created session */
+     return common_session;
+ 
+ }
+ 
+ void guac_common_ssh_destroy_session(guac_common_ssh_session* session) {
+ 
+     /* Disconnect and clean up libssh2 */
+     libssh2_session_disconnect(session->session, "Bye");
+     libssh2_session_free(session->session);
+ 
+     /* Free all other data */
+     free(session);
+ 
+ }
+ 

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/c4903a8e/src/protocols/rdp/rdp.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/c4903a8e/src/protocols/telnet/telnet.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/c4903a8e/src/protocols/vnc/vnc.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/c4903a8e/src/terminal/terminal.c
----------------------------------------------------------------------


[5/9] incubator-guacamole-server git commit: GUACAMOLE-225: Bump version numbers to 0.9.12-incubating.

Posted by jm...@apache.org.
GUACAMOLE-225: Bump version numbers to 0.9.12-incubating.


Project: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/commit/e27996b1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/tree/e27996b1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/diff/e27996b1

Branch: refs/heads/master
Commit: e27996b1997c45c6bae6ad9268c54768aeb5cd3a
Parents: a808a6b
Author: Michael Jumper <mj...@apache.org>
Authored: Tue Feb 28 09:42:26 2017 -0800
Committer: Michael Jumper <mj...@apache.org>
Committed: Tue Feb 28 09:47:49 2017 -0800

----------------------------------------------------------------------
 configure.ac | 2 +-
 doc/Doxyfile | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/e27996b1/configure.ac
----------------------------------------------------------------------
diff --git a/configure.ac b/configure.ac
index d7a4e4c..2996786 100644
--- a/configure.ac
+++ b/configure.ac
@@ -18,7 +18,7 @@
 #
 
 AC_PREREQ([2.61])
-AC_INIT([guacamole-server], [0.9.11-incubating])
+AC_INIT([guacamole-server], [0.9.12-incubating])
 AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects])
 AM_SILENT_RULES([yes])
 

http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/e27996b1/doc/Doxyfile
----------------------------------------------------------------------
diff --git a/doc/Doxyfile b/doc/Doxyfile
index 41f1a3f..23eaf03 100644
--- a/doc/Doxyfile
+++ b/doc/Doxyfile
@@ -22,7 +22,7 @@
 #
 
 PROJECT_NAME   = libguac
-PROJECT_NUMBER = 0.9.11-incubating
+PROJECT_NUMBER = 0.9.12-incubating
 
 #
 # Warn about undocumented parameters and return values, but do not fill output