summaryrefslogtreecommitdiff
path: root/SPICEConsoleAPI.cpp
blob: 9861cdaca74bc71ad7583a48bf03a5fdaf5994f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* ***** BEGIN LICENSE BLOCK *****
 *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 *   The contents of this file are subject to the Mozilla Public License Version
 *   1.1 (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.mozilla.org/MPL/
 *
 *   Software distributed under the License is distributed on an "AS IS" basis,
 *   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 *   for the specific language governing rights and limitations under the
 *   License.
 *
 *   Copyright 2009-2012, Red Hat Inc.
 *   Based on firebreath plugin example
 *
 *   Contributor(s):
 *   Uri Lublin
 *   Martin Stransky
 *   Peter Hatina
 *
 *   Alternatively, the contents of this file may be used under the terms of
 *   either the GNU General Public License Version 2 or later (the "GPL"), or
 *   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 *   in which case the provisions of the GPL or the LGPL are applicable instead
 *   of those above. If you wish to allow use of your version of this file only
 *   under the terms of either the GPL or the LGPL, and not to allow others to
 *   use your version of this file under the terms of the MPL, indicate your
 *   decision by deleting the provisions above and replace them with the notice
 *   and other provisions required by the GPL or the LGPL. If you do not delete
 *   the provisions above, a recipient may use your version of this file under
 *   the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include <sys/stat.h>

#include <glib.h>

#include "JSObject.h"
#include "variant_list.h"
#include "DOM/Document.h"
#include "global/config.h"

#include "SPICEConsoleAPI.h"

// helper function for tcp/udp range conversion and validation
static int portToInt(const std::string &port)
{
    errno = 0;
    char *end;
    const long int min = 0;
    const long int max = 65535;
    long int conv = strtol(port.c_str(), &end, 10);
    return (errno || *end != '\0' || end == port.c_str() || conv < min || conv > max)
    ? -1 : static_cast<int>(conv);
}

///////////////////////////////////////////////////////////////////////////////
/// @fn SPICEConsolePtr SPICEConsoleAPI::getPlugin()
///
/// @brief  Gets a reference to the plugin that was passed in when the object
///         was created.  If the plugin has already been released then this
///         will throw a FB::script_error that will be translated into a
///         javascript exception in the page.
///////////////////////////////////////////////////////////////////////////////
SPICEConsolePtr SPICEConsoleAPI::getPlugin()
{
    SPICEConsolePtr plugin(m_plugin.lock());
    if (!plugin) {
        throw FB::script_error("The plugin is invalid");
    }
    return plugin;
}


// Methods
bool SPICEConsoleAPI::createTrustStore(void)
{
    std::string trust_store;
    try {
        trust_store = getAttribute("TrustStore").cast<std::string>();
    } catch (FB::bad_variant_cast &e) {
        g_warning("invalid trust store value");
    }

    m_trust_store_file = m_external_controller.WriteCAFile(trust_store);

    return (!m_trust_store_file.empty());
}

void SPICEConsoleAPI::connect()
{
    try {
        FB::variant port = getAttribute("port");
        FB::variant sport = getAttribute("sport");

        if (!port.empty() && (port.convert_cast<int>() < 0)) {
            setAttribute("port", FB::variant());
        }
        if (!sport.empty() && (sport.convert_cast<int>() < 0)) {
            setAttribute("SecurePort", FB::variant());
        }
    } catch (FB::bad_variant_cast &e) {
        g_warning("invalid port/SecurePort value %s", e.what());
    }


    if (getAttribute("port").empty() && getAttribute("SecurePort").empty())
    {
        m_connected_status = 1;
        fire_disconnected(m_connected_status);
        return;
    }

    if (!m_external_controller.InitSocket()) {
        g_critical("could not initialize controller socket");
        return;
    }

    if (!m_external_controller.StartClient()) {
        g_critical("failed to start SPICE client");
        return;
    }

    if (m_external_controller.Connect(10) != 0) {
        g_critical("could not connect to spice client controller");
        return;
    }

    if (!this->createTrustStore()) {
        g_critical("could not create trust store");
        return;
    }

    SendInit();

    SendValue<std::string>(CONTROLLER_HOST, "hostIP");
    SendValue<unsigned int>(CONTROLLER_PORT, "port");
    SendValue<unsigned int>(CONTROLLER_SPORT, "SecurePort");
    /*
       SendValue(CONTROLLER_FULL_SCREEN,
       (m_fullscreen == PR_TRUE ? CONTROLLER_SET_FULL_SCREEN : 0) |
       (m_admin_console == PR_FALSE ? CONTROLLER_AUTO_DISPLAY_RES : 0));
       */
    SendValue<bool>(CONTROLLER_ENABLE_SMARTCARD, "Smartcard");
    SendValue<std::string>(CONTROLLER_PASSWORD, "Password");
    SendValue<std::string>(CONTROLLER_TLS_CIPHERS, "CipherSuite");
    SendValue<std::string>(CONTROLLER_SET_TITLE, "Title");
    SendValue<bool>(CONTROLLER_SEND_CAD, "SendCtrlAltDelete");
    SendValue<bool>(CONTROLLER_ENABLE_USB_AUTOSHARE, "UsbAutoShare");
    SendMsg(CONTROLLER_USB_FILTER, m_usb_filter);
    SendMsg(CONTROLLER_SECURE_CHANNELS, m_SSLChannels);
    SendMsg(CONTROLLER_CA_FILE, m_trust_store_file);
    SendValue<std::string>(CONTROLLER_HOST_SUBJECT, "HostSubject");
    SendValue<std::string>(CONTROLLER_HOTKEYS, "HotKey");
    //SendValue(CONTROLLER_COLOR_DEPTH, atoi(m_color_depth.c_str()));
    SendValue<std::string>(CONTROLLER_DISABLE_EFFECTS, "DisableEffects");
    SendMsg(CONTROLLER_CONNECT);
    SendMsg(CONTROLLER_SHOW);

    // set connected status
    m_connected_status = -1;
}

void SPICEConsoleAPI::show()
{
    g_debug("sending show message");
    SendMsg(CONTROLLER_SHOW);
}

void SPICEConsoleAPI::disconnect()
{
    m_external_controller.StopClient();
}

void SPICEConsoleAPI::SetLanguageStrings(const std::string &section,
                                         const std::string &lang)
{
    if ((section.size() == 0) || (lang.size() == 0)) {
        return;
    }

    m_languages[section] = lang;
}

void SPICEConsoleAPI::SetUsbFilter(const std::string &filter)
{
    m_usb_filter = filter;
}

int SPICEConsoleAPI::ConnectedStatus()
{
    return m_connected_status;
}

/* attribute string SSLChannels; */
std::string SPICEConsoleAPI::get_SSLChannels() const
{
    return m_SSLChannels;
}

void SPICEConsoleAPI::set_SSLChannels(const std::string &aSSLChannels)
{
    m_SSLChannels = aSSLChannels;

    /*
     * Backward Compatibility: Begin
     * Remove leading 's' from m_SSLChannels, e.g. "main" not "smain"
     * RHEL5 uses 'smain' and 'sinpusts
     * RHEL6 uses 'main'  and 'inputs'
     */
    const char* chan_names[] = {
        "smain", "sdisplay", "sinputs",
        "scursor", "splayback", "srecord",
        "susbredir", "ssmartcard", "stunnel"
    };
    const int nnames = sizeof(chan_names) / sizeof(chan_names[0]);

    for (int i = 0; i < nnames; i++) {
        const char *name = chan_names[i];
        size_t found = 0;
        while ((found = m_SSLChannels.find(name, found)) != std::string::npos)
            m_SSLChannels.replace(found, strlen(name), name + 1);
    }
    /* Backward Compatibility: End */
}

void SPICEConsoleAPI::WriteToPipe(const void *data, uint32_t size)
{
    m_external_controller.Write(data, size);
}

void SPICEConsoleAPI::SendInit()
{
    ControllerInit msg = { {CONTROLLER_MAGIC, CONTROLLER_VERSION, sizeof(msg)},
                           0, CONTROLLER_FLAG_EXCLUSIVE };
    WriteToPipe(&msg, sizeof(msg));
}

void SPICEConsoleAPI::SendMsg(uint32_t id)
{
    ControllerMsg msg = {id, sizeof(msg)};
    WriteToPipe(&msg, sizeof(msg));
}

void SPICEConsoleAPI::SendMsg(uint32_t id, uint32_t value)
{
    if (!value)
        return;

    ControllerValue msg = { {id, sizeof(msg)}, value };
    WriteToPipe(&msg, sizeof(msg));
}

void SPICEConsoleAPI::SendMsg(uint32_t id, bool value)
{
    ControllerValue msg = { {id, sizeof(msg)}, value };
    WriteToPipe(&msg, sizeof(msg));
}

void SPICEConsoleAPI::SendMsg(uint32_t id, const std::string &str)
{
    if (str.empty())
        return;

    size_t size = sizeof(ControllerData) + str.size() + 1;
    ControllerData *msg = static_cast<ControllerData *>(malloc(size));
    msg->base.id = id;
    msg->base.size = size;
    strcpy(reinterpret_cast<char *>(msg->data), str.c_str());
    WriteToPipe(msg, size);
    free(msg);
}

template<typename T> void SPICEConsoleAPI::SendValue(uint32_t id,
                                                     std::string attributeName)
{
    FB::variant variant = getAttribute(attributeName);
    T value;

    if (variant.empty())
        return;
    try {
        value = variant.convert_cast<T>();
        std::ostringstream msg;
        msg << "sending [" << value << "] for " << attributeName;
        g_warning("%s", msg.str().c_str());
    } catch (FB::bad_variant_cast &e) {
        g_warning("Invalid value for %s", attributeName.c_str());
        return;
    }
    SendMsg(id, value);
}