cscg24-guacamole

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

auth.c (4256B)


      1/*
      2 * Licensed to the Apache Software Foundation (ASF) under one
      3 * or more contributor license agreements.  See the NOTICE file
      4 * distributed with this work for additional information
      5 * regarding copyright ownership.  The ASF licenses this file
      6 * to you under the Apache License, Version 2.0 (the
      7 * "License"); you may not use this file except in compliance
      8 * with the License.  You may obtain a copy of the License at
      9 *
     10 *   http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing,
     13 * software distributed under the License is distributed on an
     14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     15 * KIND, either express or implied.  See the License for the
     16 * specific language governing permissions and limitations
     17 * under the License.
     18 */
     19
     20#include "config.h"
     21
     22#include "argv.h"
     23#include "auth.h"
     24#include "vnc.h"
     25
     26#include <guacamole/argv.h>
     27#include <guacamole/client.h>
     28#include <guacamole/protocol.h>
     29#include <guacamole/socket.h>
     30#include <guacamole/string.h>
     31#include <rfb/rfbclient.h>
     32#include <rfb/rfbproto.h>
     33
     34#include <pthread.h>
     35#include <string.h>
     36
     37char* guac_vnc_get_password(rfbClient* client) {
     38    guac_client* gc = rfbClientGetClientData(client, GUAC_VNC_CLIENT_KEY);
     39    guac_vnc_client* vnc_client = ((guac_vnc_client*) gc->data);
     40    guac_vnc_settings* settings = vnc_client->settings;
     41    
     42    /* If the client does not support the "required" instruction, just return
     43        the configuration data. */
     44    if (!guac_client_owner_supports_required(gc))
     45        return guac_strdup(settings->password);
     46    
     47    /* If password isn't around, prompt for it. */
     48    if (settings->password == NULL) {
     49        
     50        guac_argv_register(GUAC_VNC_ARGV_PASSWORD, guac_vnc_argv_callback, NULL, 0);
     51        
     52        const char* params[] = {GUAC_VNC_ARGV_PASSWORD, NULL};
     53        
     54        /* Send the request for password to the owner. */
     55        guac_client_owner_send_required(gc, params);
     56                
     57        /* Wait for the arguments to be returned */
     58        guac_argv_await(params);
     59
     60    }
     61    
     62    return guac_strdup(settings->password);
     63    
     64}
     65
     66#ifdef ENABLE_VNC_GENERIC_CREDENTIALS
     67rfbCredential* guac_vnc_get_credentials(rfbClient* client, int credentialType) {
     68    
     69    guac_client* gc = rfbClientGetClientData(client, GUAC_VNC_CLIENT_KEY);
     70    guac_vnc_client* vnc_client = ((guac_vnc_client*) gc->data);
     71    guac_vnc_settings* settings = vnc_client->settings;
     72    
     73    /* Handle request for Username/Password credentials */
     74    if (credentialType == rfbCredentialTypeUser) {
     75        rfbCredential *creds = malloc(sizeof(rfbCredential));
     76        
     77        /* If the client supports the "required" instruction, prompt for and
     78           update those. */
     79        if (guac_client_owner_supports_required(gc)) {
     80            char* params[3] = {NULL};
     81            int i = 0;
     82
     83            /* Check if username is not provided. */
     84            if (settings->username == NULL) {
     85                guac_argv_register(GUAC_VNC_ARGV_USERNAME, guac_vnc_argv_callback, NULL, 0);
     86                params[i] = GUAC_VNC_ARGV_USERNAME;
     87                i++;
     88            }
     89
     90            /* Check if password is not provided. */
     91            if (settings->password == NULL) {
     92                guac_argv_register(GUAC_VNC_ARGV_PASSWORD, guac_vnc_argv_callback, NULL, 0);
     93                params[i] = GUAC_VNC_ARGV_PASSWORD;
     94                i++;
     95            }
     96
     97            params[i] = NULL;
     98
     99            /* If we have empty parameters, request them and await response. */
    100            if (i > 0) {
    101                guac_client_owner_send_required(gc, (const char**) params);
    102                guac_argv_await((const char**) params);
    103            }
    104        }
    105        
    106        /* Copy the values and return the credential set. */
    107        creds->userCredential.username = guac_strdup(settings->username);
    108        creds->userCredential.password = guac_strdup(settings->password);
    109        return creds;
    110        
    111    }
    112
    113    guac_client_abort(gc, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
    114            "Unsupported credential type requested.");
    115    guac_client_log(gc, GUAC_LOG_DEBUG,
    116            "Unable to provide requested type of credential: %d.",
    117            credentialType);
    118    return NULL;
    119    
    120}
    121#endif