| 1 |
/* |
| 2 |
* Copyright (C) 2013-2014 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "cockpitwebservice.h" |
| 9 |
|
| 10 |
#include "cockpitws.h" |
| 11 |
|
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
#include <json-glib/json-glib.h> |
| 15 |
#include <gio/gunixinputstream.h> |
| 16 |
#include <gio/gunixoutputstream.h> |
| 17 |
|
| 18 |
#include "common/cockpitauthorize.h" |
| 19 |
#include "common/cockpitconf.h" |
| 20 |
#include "common/cockpithex.h" |
| 21 |
#include "cockpitjson.h" |
| 22 |
#include "common/cockpitmemory.h" |
| 23 |
#include "cockpitsystem.h" |
| 24 |
#include "cockpitwebresponse.h" |
| 25 |
#include "cockpitwebserver.h" |
| 26 |
|
| 27 |
#include "websocket.h" |
| 28 |
|
| 29 |
|
| 30 |
#include <stdlib.h> |
| 31 |
|
| 32 |
guint cockpit_ws_ping_interval = 5; |
| 33 |
|
| 34 |
/* ---------------------------------------------------------------------------- |
| 35 |
* Web Socket Info |
| 36 |
*/ |
| 37 |
|
| 38 |
typedef struct { |
| 39 |
gchar *id; |
| 40 |
WebSocketConnection *connection; |
| 41 |
GHashTable *channels; |
| 42 |
JsonObject *init_received; |
| 43 |
} CockpitSocket; |
| 44 |
|
| 45 |
typedef struct { |
| 46 |
GHashTable *by_channel; |
| 47 |
GHashTable *by_connection; |
| 48 |
guint next_socket_id; |
| 49 |
} CockpitSockets; |
| 50 |
|
| 51 |
static void |
| 52 |
cockpit_socket_free (gpointer data) |
| 53 |
{ |
| 54 |
CockpitSocket *socket = data; |
| 55 |
g_hash_table_unref (socket->channels); |
| 56 |
if (socket->init_received) |
| 57 |
json_object_unref (socket->init_received); |
| 58 |
g_object_unref (socket->connection); |
| 59 |
g_free (socket->id); |
| 60 |
g_free (socket); |
| 61 |
} |
| 62 |
|
| 63 |
static void |
| 64 |
cockpit_sockets_init (CockpitSockets *sockets) |
| 65 |
{ |
| 66 |
sockets->next_socket_id = 1; |
| 67 |
|
| 68 |
sockets->by_channel = g_hash_table_new (g_str_hash, g_str_equal); |
| 69 |
|
| 70 |
/* This owns the socket */ |
| 71 |
sockets->by_connection = g_hash_table_new_full (g_direct_hash, g_direct_equal, |
| 72 |
NULL, cockpit_socket_free); |
| 73 |
} |
| 74 |
|
| 75 |
inline static CockpitSocket * |
| 76 |
cockpit_socket_lookup_by_connection (CockpitSockets *sockets, |
| 77 |
WebSocketConnection *connection) |
| 78 |
{ |
| 79 |
return g_hash_table_lookup (sockets->by_connection, connection); |
| 80 |
} |
| 81 |
|
| 82 |
inline static CockpitSocket * |
| 83 |
cockpit_socket_lookup_by_channel (CockpitSockets *sockets, |
| 84 |
const gchar *channel) |
| 85 |
{ |
| 86 |
return g_hash_table_lookup (sockets->by_channel, channel); |
| 87 |
} |
| 88 |
|
| 89 |
static void |
| 90 |
cockpit_socket_remove_channel (CockpitSockets *sockets, |
| 91 |
CockpitSocket *socket, |
| 92 |
const gchar *channel) |
| 93 |
{ |
| 94 |
g_debug ("%s remove channel %s for socket", socket->id, channel); |
| 95 |
g_hash_table_remove (sockets->by_channel, channel); |
| 96 |
g_hash_table_remove (socket->channels, channel); |
| 97 |
} |
| 98 |
|
| 99 |
static void |
| 100 |
cockpit_socket_add_channel (CockpitSockets *sockets, |
| 101 |
CockpitSocket *socket, |
| 102 |
const gchar *channel, |
| 103 |
WebSocketDataType data_type) |
| 104 |
{ |
| 105 |
gchar *chan; |
| 106 |
|
| 107 |
chan = g_strdup (channel); |
| 108 |
g_hash_table_insert (sockets->by_channel, chan, socket); |
| 109 |
g_hash_table_replace (socket->channels, chan, GINT_TO_POINTER (data_type)); |
| 110 |
|
| 111 |
g_debug ("%s added channel %s to socket", socket->id, channel); |
| 112 |
} |
| 113 |
|
| 114 |
static CockpitSocket * |
| 115 |
cockpit_socket_track (CockpitSockets *sockets, |
| 116 |
WebSocketConnection *connection) |
| 117 |
{ |
| 118 |
CockpitSocket *socket; |
| 119 |
|
| 120 |
socket = g_new0 (CockpitSocket, 1); |
| 121 |
socket->id = g_strdup_printf ("%u:", sockets->next_socket_id++); |
| 122 |
socket->connection = g_object_ref (connection); |
| 123 |
socket->channels = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); |
| 124 |
|
| 125 |
g_debug ("%s new socket", socket->id); |
| 126 |
|
| 127 |
/* This owns the socket */ |
| 128 |
g_hash_table_insert (sockets->by_connection, connection, socket); |
| 129 |
|
| 130 |
return socket; |
| 131 |
} |
| 132 |
|
| 133 |
static void |
| 134 |
cockpit_socket_destroy (CockpitSockets *sockets, |
| 135 |
CockpitSocket *socket) |
| 136 |
{ |
| 137 |
GHashTableIter iter; |
| 138 |
const gchar *chan; |
| 139 |
|
| 140 |
g_debug ("%s destroy socket", socket->id); |
| 141 |
|
| 142 |
g_hash_table_iter_init (&iter, socket->channels); |
| 143 |
while (g_hash_table_iter_next (&iter, (gpointer *)&chan, NULL)) |
| 144 |
g_hash_table_remove (sockets->by_channel, chan); |
| 145 |
g_hash_table_remove_all (socket->channels); |
| 146 |
|
| 147 |
/* This owns the socket */ |
| 148 |
g_hash_table_remove (sockets->by_connection, socket->connection); |
| 149 |
} |
| 150 |
|
| 151 |
static void |
| 152 |
cockpit_sockets_close (CockpitSockets *sockets, |
| 153 |
const gchar *problem) |
| 154 |
{ |
| 155 |
GHashTableIter iter; |
| 156 |
CockpitSocket *socket; |
| 157 |
|
| 158 |
if (!problem) |
| 159 |
problem = "terminated"; |
| 160 |
|
| 161 |
g_hash_table_iter_init (&iter, sockets->by_connection); |
| 162 |
while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&socket)) |
| 163 |
{ |
| 164 |
if (web_socket_connection_get_ready_state (socket->connection) < WEB_SOCKET_STATE_CLOSING) |
| 165 |
web_socket_connection_close (socket->connection, WEB_SOCKET_CLOSE_GOING_AWAY, problem); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
static void |
| 170 |
cockpit_sockets_cleanup (CockpitSockets *sockets) |
| 171 |
{ |
| 172 |
g_hash_table_destroy (sockets->by_connection); |
| 173 |
g_hash_table_destroy (sockets->by_channel); |
| 174 |
} |
| 175 |
|
| 176 |
/* ---------------------------------------------------------------------------- |
| 177 |
* Web Socket Routing |
| 178 |
*/ |
| 179 |
|
| 180 |
struct _CockpitWebService { |
| 181 |
GObject parent; |
| 182 |
|
| 183 |
gchar *id; |
| 184 |
|
| 185 |
CockpitCreds *creds; |
| 186 |
CockpitSockets sockets; |
| 187 |
gboolean closing; |
| 188 |
GBytes *control_prefix; |
| 189 |
guint ping_timeout; |
| 190 |
gint callers; |
| 191 |
guint next_internal_id; |
| 192 |
|
| 193 |
CockpitTransport *transport; |
| 194 |
JsonObject *init_received; |
| 195 |
gulong control_sig; |
| 196 |
gulong recv_sig; |
| 197 |
gulong closed_sig; |
| 198 |
gboolean sent_done; |
| 199 |
guint credentials_timeout; |
| 200 |
|
| 201 |
GHashTable *checksum_by_host; |
| 202 |
GHashTable *host_by_checksum; |
| 203 |
}; |
| 204 |
|
| 205 |
typedef struct { |
| 206 |
GObjectClass parent; |
| 207 |
} CockpitWebServiceClass; |
| 208 |
|
| 209 |
static guint sig_idling = 0; |
| 210 |
static guint sig_destroy = 0; |
| 211 |
|
| 212 |
G_DEFINE_TYPE (CockpitWebService, cockpit_web_service, G_TYPE_OBJECT); |
| 213 |
|
| 214 |
static void |
| 215 |
cockpit_web_service_dispose (GObject *object) |
| 216 |
{ |
| 217 |
CockpitWebService *self = COCKPIT_WEB_SERVICE (object); |
| 218 |
gboolean emit = FALSE; |
| 219 |
|
| 220 |
if (self->control_sig) |
| 221 |
g_signal_handler_disconnect (self->transport, self->control_sig); |
| 222 |
self->control_sig = 0; |
| 223 |
|
| 224 |
if (self->recv_sig) |
| 225 |
g_signal_handler_disconnect (self->transport, self->recv_sig); |
| 226 |
self->recv_sig = 0; |
| 227 |
|
| 228 |
if (self->closed_sig) |
| 229 |
g_signal_handler_disconnect (self->transport, self->closed_sig); |
| 230 |
self->closed_sig = 0; |
| 231 |
|
| 232 |
if (self->credentials_timeout) |
| 233 |
g_source_remove (self->credentials_timeout); |
| 234 |
self->credentials_timeout = 0; |
| 235 |
|
| 236 |
if (!self->sent_done) |
| 237 |
{ |
| 238 |
self->sent_done = TRUE; |
| 239 |
cockpit_transport_close (self->transport, NULL); |
| 240 |
} |
| 241 |
|
| 242 |
if (!self->closing) |
| 243 |
{ |
| 244 |
g_debug ("web service closing"); |
| 245 |
emit = TRUE; |
| 246 |
} |
| 247 |
self->closing = TRUE; |
| 248 |
|
| 249 |
cockpit_sockets_close (&self->sockets, NULL); |
| 250 |
|
| 251 |
if (emit) |
| 252 |
g_signal_emit (self, sig_destroy, 0); |
| 253 |
|
| 254 |
G_OBJECT_CLASS (cockpit_web_service_parent_class)->dispose (object); |
| 255 |
} |
| 256 |
|
| 257 |
static void |
| 258 |
cockpit_web_service_finalize (GObject *object) |
| 259 |
{ |
| 260 |
CockpitWebService *self = COCKPIT_WEB_SERVICE (object); |
| 261 |
|
| 262 |
cockpit_sockets_cleanup (&self->sockets); |
| 263 |
|
| 264 |
if (self->transport) |
| 265 |
g_object_unref (self->transport); |
| 266 |
if (self->init_received) |
| 267 |
json_object_unref (self->init_received); |
| 268 |
|
| 269 |
g_bytes_unref (self->control_prefix); |
| 270 |
cockpit_creds_unref (self->creds); |
| 271 |
if (self->ping_timeout) |
| 272 |
g_source_remove (self->ping_timeout); |
| 273 |
|
| 274 |
g_hash_table_destroy (self->host_by_checksum); |
| 275 |
g_hash_table_destroy (self->checksum_by_host); |
| 276 |
g_free (self->id); |
| 277 |
|
| 278 |
G_OBJECT_CLASS (cockpit_web_service_parent_class)->finalize (object); |
| 279 |
} |
| 280 |
|
| 281 |
gchar * |
| 282 |
cockpit_web_service_unique_channel (CockpitWebService *self) |
| 283 |
{ |
| 284 |
return g_strdup_printf ("0:%d", self->next_internal_id++); |
| 285 |
} |
| 286 |
|
| 287 |
static void |
| 288 |
caller_begin (CockpitWebService *self) |
| 289 |
{ |
| 290 |
g_object_ref (self); |
| 291 |
self->callers++; |
| 292 |
} |
| 293 |
|
| 294 |
static void |
| 295 |
caller_end (CockpitWebService *self) |
| 296 |
{ |
| 297 |
g_return_if_fail (self->callers > 0); |
| 298 |
self->callers--; |
| 299 |
if (self->callers == 0) |
| 300 |
g_signal_emit (self, sig_idling, 0); |
| 301 |
g_object_unref (self); |
| 302 |
} |
| 303 |
|
| 304 |
static void |
| 305 |
outbound_protocol_error (CockpitWebService *self, |
| 306 |
CockpitTransport *transport, |
| 307 |
const gchar *problem) |
| 308 |
{ |
| 309 |
if (problem == NULL) |
| 310 |
problem = "protocol-error"; |
| 311 |
cockpit_transport_close (transport, problem); |
| 312 |
} |
| 313 |
|
| 314 |
static gboolean |
| 315 |
process_close (CockpitWebService *self, |
| 316 |
CockpitSocket *socket, |
| 317 |
const gchar *channel) |
| 318 |
{ |
| 319 |
if (socket) |
| 320 |
cockpit_socket_remove_channel (&self->sockets, socket, channel); |
| 321 |
|
| 322 |
return TRUE; |
| 323 |
} |
| 324 |
|
| 325 |
static gboolean |
| 326 |
process_and_relay_close (CockpitWebService *self, |
| 327 |
CockpitSocket *socket, |
| 328 |
const gchar *channel, |
| 329 |
GBytes *payload) |
| 330 |
{ |
| 331 |
gboolean valid; |
| 332 |
|
| 333 |
valid = process_close (self, socket, channel); |
| 334 |
if (valid && !self->sent_done) |
| 335 |
cockpit_transport_send (self->transport, NULL, payload); |
| 336 |
|
| 337 |
return valid; |
| 338 |
} |
| 339 |
|
| 340 |
static gboolean |
| 341 |
process_kill (CockpitWebService *self, |
| 342 |
CockpitSocket *socket, |
| 343 |
JsonObject *options, |
| 344 |
GBytes *payload) |
| 345 |
{ |
| 346 |
if (!self->sent_done) |
| 347 |
cockpit_transport_send (self->transport, NULL, payload); |
| 348 |
|
| 349 |
return TRUE; |
| 350 |
} |
| 351 |
|
| 352 |
static gboolean |
| 353 |
process_ping (CockpitWebService *self, |
| 354 |
CockpitSocket *socket, |
| 355 |
JsonObject *options) |
| 356 |
{ |
| 357 |
GBytes *payload; |
| 358 |
|
| 359 |
/* Respond to a ping without a channel, by saying "pong" */ |
| 360 |
json_object_set_string_member (options, "command", "pong"); |
| 361 |
payload = cockpit_json_write_bytes (options); |
| 362 |
if (web_socket_connection_get_ready_state (socket->connection) == WEB_SOCKET_STATE_OPEN) |
| 363 |
web_socket_connection_send (socket->connection, WEB_SOCKET_DATA_TEXT, self->control_prefix, payload); |
| 364 |
g_bytes_unref (payload); |
| 365 |
|
| 366 |
return TRUE; |
| 367 |
} |
| 368 |
|
| 369 |
static void |
| 370 |
clear_and_free_string (gpointer data) |
| 371 |
{ |
| 372 |
cockpit_memory_clear (data, -1); |
| 373 |
free (data); |
| 374 |
} |
| 375 |
|
| 376 |
static gboolean |
| 377 |
process_socket_authorize (CockpitWebService *self, |
| 378 |
CockpitSocket *socket, |
| 379 |
const gchar *channel, |
| 380 |
JsonObject *options, |
| 381 |
GBytes *payload) |
| 382 |
{ |
| 383 |
const gchar *response = NULL; |
| 384 |
gboolean ret = FALSE; |
| 385 |
GBytes *bytes = NULL; |
| 386 |
char *password = NULL; |
| 387 |
char *user = NULL; |
| 388 |
char *type = NULL; |
| 389 |
gpointer data; |
| 390 |
gsize length; |
| 391 |
|
| 392 |
if (!cockpit_json_get_string (options, "response", NULL, &response)) |
| 393 |
{ |
| 394 |
g_warning ("%s: received invalid \"response\" field in authorize command", socket->id); |
| 395 |
goto out; |
| 396 |
} |
| 397 |
|
| 398 |
ret = TRUE; |
| 399 |
if (response) |
| 400 |
{ |
| 401 |
if (!cockpit_authorize_type (response, &type) || !g_str_equal (type, "basic")) |
| 402 |
goto out; |
| 403 |
|
| 404 |
password = cockpit_authorize_parse_basic (response, &user); |
| 405 |
if (password && !user) |
| 406 |
{ |
| 407 |
cockpit_memory_clear (password, -1); |
| 408 |
free (password); |
| 409 |
password = NULL; |
| 410 |
} |
| 411 |
} |
| 412 |
else |
| 413 |
{ |
| 414 |
goto out; |
| 415 |
} |
| 416 |
|
| 417 |
if (password == NULL) |
| 418 |
{ |
| 419 |
bytes = NULL; |
| 420 |
} |
| 421 |
else |
| 422 |
{ |
| 423 |
bytes = g_bytes_new_with_free_func (password, strlen (password), |
| 424 |
clear_and_free_string, password); |
| 425 |
password = NULL; |
| 426 |
} |
| 427 |
|
| 428 |
cockpit_creds_set_user (self->creds, user); |
| 429 |
cockpit_creds_set_password (self->creds, bytes); |
| 430 |
|
| 431 |
/* Clear out the payload memory */ |
| 432 |
data = (gpointer)g_bytes_get_data (payload, &length); |
| 433 |
cockpit_memory_clear (data, length); |
| 434 |
|
| 435 |
out: |
| 436 |
free (type); |
| 437 |
free (user); |
| 438 |
if (bytes) |
| 439 |
g_bytes_unref (bytes); |
| 440 |
return ret; |
| 441 |
} |
| 442 |
|
| 443 |
static gboolean |
| 444 |
authorize_check_user (CockpitCreds *creds, |
| 445 |
const char *challenge) |
| 446 |
{ |
| 447 |
char *subject = NULL; |
| 448 |
gboolean ret = FALSE; |
| 449 |
const gchar *user; |
| 450 |
|
| 451 |
if (!cockpit_authorize_subject (challenge, &subject)) |
| 452 |
goto out; |
| 453 |
|
| 454 |
if (!subject || g_str_equal (subject, "")) |
| 455 |
{ |
| 456 |
ret = TRUE; |
| 457 |
} |
| 458 |
else |
| 459 |
{ |
| 460 |
user = cockpit_creds_get_user (creds); |
| 461 |
if (user == NULL) |
| 462 |
{ |
| 463 |
ret = TRUE; |
| 464 |
} |
| 465 |
else |
| 466 |
{ |
| 467 |
char *encoded = cockpit_hex_encode (user, -1); |
| 468 |
ret = g_str_equal (encoded, subject); |
| 469 |
free (encoded); |
| 470 |
|
| 471 |
/* domain users are often case insensitive, while NSS/Linux converts them to the canonical lower-case form; |
| 472 |
* accept the lower-case form of the creds user as well */ |
| 473 |
if (!ret) |
| 474 |
{ |
| 475 |
gchar *user_lower = g_ascii_strdown (user, -1); |
| 476 |
encoded = cockpit_hex_encode (user_lower, -1); |
| 477 |
free (user_lower); |
| 478 |
ret = g_str_equal (encoded, subject); |
| 479 |
free (encoded); |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
out: |
| 485 |
free (subject); |
| 486 |
return ret; |
| 487 |
} |
| 488 |
|
| 489 |
static gboolean |
| 490 |
process_transport_authorize (CockpitWebService *self, |
| 491 |
CockpitTransport *transport, |
| 492 |
JsonObject *options) |
| 493 |
{ |
| 494 |
const gchar *cookie = NULL; |
| 495 |
GBytes *payload; |
| 496 |
char *type = NULL; |
| 497 |
char *alloc = NULL; |
| 498 |
const char *response = NULL; |
| 499 |
const gchar *challenge; |
| 500 |
const gchar *password = NULL; |
| 501 |
const gchar *host; |
| 502 |
GBytes *data; |
| 503 |
|
| 504 |
if (!cockpit_json_get_string (options, "challenge", NULL, &challenge) || |
| 505 |
!cockpit_json_get_string (options, "cookie", NULL, &cookie) || |
| 506 |
!cockpit_json_get_string (options, "host", NULL, &host)) |
| 507 |
{ |
| 508 |
g_warning ("received invalid authorize command"); |
| 509 |
return FALSE; |
| 510 |
} |
| 511 |
|
| 512 |
if (!challenge || !cookie) |
| 513 |
{ |
| 514 |
g_message ("unsupported or unknown authorize command"); |
| 515 |
return FALSE; |
| 516 |
} |
| 517 |
|
| 518 |
if (!cockpit_authorize_type (challenge, &type)) |
| 519 |
{ |
| 520 |
g_message ("received invalid authorize challenge command"); |
| 521 |
} |
| 522 |
else if (g_str_equal (type, "plain1") || |
| 523 |
g_str_equal (type, "basic")) |
| 524 |
{ |
| 525 |
data = cockpit_creds_get_password (self->creds); |
| 526 |
if (!data) |
| 527 |
{ |
| 528 |
g_info ("%s: received \"authorize\" %s \"challenge\", but no password", host, type); |
| 529 |
} |
| 530 |
else if (!g_str_equal ("basic", type) && !authorize_check_user (self->creds, challenge)) |
| 531 |
{ |
| 532 |
g_info ("received \"authorize\" %s \"challenge\", but for wrong user", type); |
| 533 |
} |
| 534 |
else |
| 535 |
{ |
| 536 |
password = g_bytes_get_data (data, NULL); |
| 537 |
if (g_str_equal (type, "basic")) |
| 538 |
{ |
| 539 |
alloc = cockpit_authorize_build_basic (cockpit_creds_get_user (self->creds), password); |
| 540 |
response = alloc; |
| 541 |
} |
| 542 |
else |
| 543 |
{ |
| 544 |
response = password; |
| 545 |
} |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
if (cookie && !self->sent_done) |
| 550 |
{ |
| 551 |
payload = cockpit_transport_build_control ("command", "authorize", |
| 552 |
"cookie", cookie, |
| 553 |
"response", response ? response : "", |
| 554 |
"host", host, |
| 555 |
NULL); |
| 556 |
cockpit_transport_send (transport, NULL, payload); |
| 557 |
g_bytes_unref (payload); |
| 558 |
} |
| 559 |
|
| 560 |
/* only give the password once; if the superuser bridge asks again, the password is either wrong, or PAM has |
| 561 |
* follow-up questions (such as a forced password change); we must not respond with the same password again */ |
| 562 |
if (password) |
| 563 |
{ |
| 564 |
g_debug ("authorize: poisoning password after using it once"); |
| 565 |
cockpit_creds_poison (self->creds); |
| 566 |
} |
| 567 |
|
| 568 |
free (type); |
| 569 |
free (alloc); |
| 570 |
return TRUE; |
| 571 |
} |
| 572 |
|
| 573 |
static gboolean |
| 574 |
poison_creds (gpointer user_data) |
| 575 |
{ |
| 576 |
CockpitWebService *self = user_data; |
| 577 |
cockpit_creds_poison (self->creds); |
| 578 |
self->credentials_timeout = 0; |
| 579 |
return G_SOURCE_REMOVE; |
| 580 |
} |
| 581 |
|
| 582 |
static const gchar * |
| 583 |
process_transport_init (CockpitWebService *self, |
| 584 |
CockpitTransport *transport, |
| 585 |
JsonObject *options) |
| 586 |
{ |
| 587 |
JsonObject *object; |
| 588 |
JsonObject *capabilities; |
| 589 |
gboolean explicit_superuser_capability = FALSE; |
| 590 |
GBytes *payload; |
| 591 |
gint64 version; |
| 592 |
|
| 593 |
if (!cockpit_json_get_int (options, "version", -1, &version)) |
| 594 |
{ |
| 595 |
g_warning ("invalid version field in init message"); |
| 596 |
return "protocol-error"; |
| 597 |
} |
| 598 |
|
| 599 |
if (version == 1) |
| 600 |
{ |
| 601 |
g_debug ("received init message"); |
| 602 |
if (self->init_received) |
| 603 |
json_object_unref (self->init_received); |
| 604 |
self->init_received = json_object_ref (options); |
| 605 |
|
| 606 |
if (cockpit_json_get_object (options, "capabilities", NULL, &capabilities) && capabilities) |
| 607 |
{ |
| 608 |
if (!cockpit_json_get_bool (capabilities, "explicit-superuser", FALSE, &explicit_superuser_capability)) |
| 609 |
g_warning ("invalued 'explicit-superuser' value in init message"); |
| 610 |
} |
| 611 |
|
| 612 |
/* If the bridge has the explicit-superuser capability, it will |
| 613 |
send a "superuser-init-done" message once any authorization |
| 614 |
is over. We will poisen our credentials at that time. |
| 615 |
|
| 616 |
For a bridge without the explicit-superuser capability, we |
| 617 |
keep the credentials for two minutes after receiving an |
| 618 |
"init" message. |
| 619 |
*/ |
| 620 |
|
| 621 |
self->credentials_timeout = g_timeout_add (2*60*1000, poison_creds, self); |
| 622 |
|
| 623 |
/* Always send an init message down the new transport */ |
| 624 |
object = cockpit_transport_build_json ("command", "init", NULL); |
| 625 |
json_object_set_int_member (object, "version", 1); |
| 626 |
json_object_set_string_member (object, "host", "localhost"); |
| 627 |
|
| 628 |
if (explicit_superuser_capability) |
| 629 |
{ |
| 630 |
const gchar *superuser = getenv("COCKPIT_SUPERUSER") ?: cockpit_creds_get_superuser (self->creds); |
| 631 |
if (superuser && *superuser && !g_str_equal (superuser, "none")) |
| 632 |
{ |
| 633 |
JsonObject *superuser_options; |
| 634 |
|
| 635 |
superuser_options = json_object_new (); |
| 636 |
json_object_set_string_member (superuser_options, "id", superuser); |
| 637 |
json_object_set_object_member (object, "superuser", superuser_options); |
| 638 |
} |
| 639 |
else |
| 640 |
{ |
| 641 |
json_object_set_boolean_member (object, "superuser", FALSE); |
| 642 |
cockpit_creds_poison (self->creds); |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
payload = cockpit_json_write_bytes (object); |
| 647 |
json_object_unref (object); |
| 648 |
cockpit_transport_send (transport, NULL, payload); |
| 649 |
g_bytes_unref (payload); |
| 650 |
} |
| 651 |
else |
| 652 |
{ |
| 653 |
g_message ("unsupported version of cockpit protocol: %" G_GINT64_FORMAT, version); |
| 654 |
return "not-supported"; |
| 655 |
} |
| 656 |
|
| 657 |
return NULL; |
| 658 |
} |
| 659 |
|
| 660 |
static gboolean |
| 661 |
on_transport_control (CockpitTransport *transport, |
| 662 |
const gchar *command, |
| 663 |
const gchar *channel, |
| 664 |
JsonObject *options, |
| 665 |
GBytes *payload, |
| 666 |
gpointer user_data) |
| 667 |
{ |
| 668 |
const gchar *problem = "protocol-error"; |
| 669 |
CockpitWebService *self = user_data; |
| 670 |
CockpitSocket *socket = NULL; |
| 671 |
gboolean valid = FALSE; |
| 672 |
gboolean forward; |
| 673 |
|
| 674 |
if (!channel) |
| 675 |
{ |
| 676 |
if (g_strcmp0 (command, "init") == 0) |
| 677 |
{ |
| 678 |
problem = process_transport_init (self, transport, options); |
| 679 |
valid = (problem == NULL); |
| 680 |
} |
| 681 |
else if (!self->init_received) |
| 682 |
{ |
| 683 |
g_message ("bridge did not send 'init' message first"); |
| 684 |
valid = FALSE; |
| 685 |
} |
| 686 |
else if (g_strcmp0 (command, "authorize") == 0) |
| 687 |
{ |
| 688 |
valid = process_transport_authorize (self, transport, options); |
| 689 |
} |
| 690 |
else if (g_strcmp0 (command, "superuser-init-done") == 0) |
| 691 |
{ |
| 692 |
cockpit_creds_poison (self->creds); |
| 693 |
valid = TRUE; |
| 694 |
} |
| 695 |
else |
| 696 |
{ |
| 697 |
g_debug ("received a %s unknown control command", command); |
| 698 |
valid = TRUE; |
| 699 |
} |
| 700 |
} |
| 701 |
else |
| 702 |
{ |
| 703 |
socket = cockpit_socket_lookup_by_channel (&self->sockets, channel); |
| 704 |
|
| 705 |
/* Usually all control messages with a channel are forwarded */ |
| 706 |
forward = TRUE; |
| 707 |
|
| 708 |
if (g_strcmp0 (command, "close") == 0) |
| 709 |
{ |
| 710 |
valid = process_close (self, socket, channel); |
| 711 |
} |
| 712 |
else |
| 713 |
{ |
| 714 |
valid = TRUE; |
| 715 |
} |
| 716 |
|
| 717 |
if (forward) |
| 718 |
{ |
| 719 |
/* Forward this message to the right websocket */ |
| 720 |
if (socket && web_socket_connection_get_ready_state (socket->connection) == WEB_SOCKET_STATE_OPEN) |
| 721 |
{ |
| 722 |
web_socket_connection_send (socket->connection, WEB_SOCKET_DATA_TEXT, |
| 723 |
self->control_prefix, payload); |
| 724 |
} |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
if (!valid) |
| 729 |
{ |
| 730 |
outbound_protocol_error (self, transport, problem); |
| 731 |
} |
| 732 |
|
| 733 |
return TRUE; /* handled */ |
| 734 |
} |
| 735 |
|
| 736 |
static gboolean |
| 737 |
on_transport_recv (CockpitTransport *transport, |
| 738 |
const gchar *channel, |
| 739 |
GBytes *payload, |
| 740 |
gpointer user_data) |
| 741 |
{ |
| 742 |
CockpitWebService *self = user_data; |
| 743 |
WebSocketDataType data_type; |
| 744 |
CockpitSocket *socket; |
| 745 |
gchar *string; |
| 746 |
GBytes *prefix; |
| 747 |
|
| 748 |
if (!channel) |
| 749 |
return FALSE; |
| 750 |
|
| 751 |
/* Forward the message to the right socket */ |
| 752 |
socket = cockpit_socket_lookup_by_channel (&self->sockets, channel); |
| 753 |
if (socket && web_socket_connection_get_ready_state (socket->connection) == WEB_SOCKET_STATE_OPEN) |
| 754 |
{ |
| 755 |
string = g_strdup_printf ("%s\n", channel); |
| 756 |
prefix = g_bytes_new_take (string, strlen (string)); |
| 757 |
data_type = GPOINTER_TO_INT (g_hash_table_lookup (socket->channels, channel)); |
| 758 |
web_socket_connection_send (socket->connection, data_type, prefix, payload); |
| 759 |
g_bytes_unref (prefix); |
| 760 |
return TRUE; |
| 761 |
} |
| 762 |
|
| 763 |
return FALSE; |
| 764 |
} |
| 765 |
|
| 766 |
static void |
| 767 |
on_transport_closed (CockpitTransport *transport, |
| 768 |
const gchar *problem, |
| 769 |
gpointer user_data) |
| 770 |
{ |
| 771 |
CockpitWebService *self = user_data; |
| 772 |
|
| 773 |
/* Close all sockets */ |
| 774 |
cockpit_sockets_close (&self->sockets, problem); |
| 775 |
|
| 776 |
/* Dispose web service */ |
| 777 |
g_object_run_dispose (G_OBJECT (self)); |
| 778 |
} |
| 779 |
|
| 780 |
gboolean |
| 781 |
cockpit_web_service_parse_binary (JsonObject *options, |
| 782 |
WebSocketDataType *data_type) |
| 783 |
{ |
| 784 |
const gchar *binary; |
| 785 |
|
| 786 |
if (!cockpit_json_get_string (options, "binary", NULL, &binary)) |
| 787 |
{ |
| 788 |
g_warning ("invalid \"binary\" option"); |
| 789 |
return FALSE; |
| 790 |
} |
| 791 |
|
| 792 |
if (binary && g_str_equal (binary, "raw")) |
| 793 |
*data_type = WEB_SOCKET_DATA_BINARY; |
| 794 |
else |
| 795 |
*data_type = WEB_SOCKET_DATA_TEXT; |
| 796 |
return TRUE; |
| 797 |
} |
| 798 |
|
| 799 |
gboolean |
| 800 |
cockpit_web_service_parse_external (JsonObject *options, |
| 801 |
const gchar **content_type, |
| 802 |
const gchar **content_encoding, |
| 803 |
const gchar **content_disposition, |
| 804 |
const gchar ***protocols) |
| 805 |
{ |
| 806 |
JsonObject *external; |
| 807 |
const gchar *value; |
| 808 |
JsonNode *node; |
| 809 |
|
| 810 |
g_return_val_if_fail (options != NULL, FALSE); |
| 811 |
|
| 812 |
if (!cockpit_json_get_string (options, "channel", NULL, &value) || value != NULL) |
| 813 |
{ |
| 814 |
g_message ("don't specify \"channel\" on external channel"); |
| 815 |
return FALSE; |
| 816 |
} |
| 817 |
if (!cockpit_json_get_string (options, "command", NULL, &value) || value != NULL) |
| 818 |
{ |
| 819 |
g_message ("don't specify \"command\" on external channel"); |
| 820 |
return FALSE; |
| 821 |
} |
| 822 |
|
| 823 |
node = json_object_get_member (options, "external"); |
| 824 |
if (node == NULL) |
| 825 |
{ |
| 826 |
if (content_disposition) |
| 827 |
*content_disposition = NULL; |
| 828 |
if (content_type) |
| 829 |
*content_type = NULL; |
| 830 |
if (content_encoding) |
| 831 |
*content_encoding = NULL; |
| 832 |
if (protocols) |
| 833 |
*protocols = NULL; |
| 834 |
return TRUE; |
| 835 |
} |
| 836 |
|
| 837 |
if (!JSON_NODE_HOLDS_OBJECT (node)) |
| 838 |
{ |
| 839 |
g_message ("invalid \"external\" option"); |
| 840 |
return FALSE; |
| 841 |
} |
| 842 |
|
| 843 |
external = json_node_get_object (node); |
| 844 |
|
| 845 |
if (!cockpit_json_get_string (external, "content-disposition", NULL, &value) || |
| 846 |
(value && !cockpit_web_response_is_header_value (value))) |
| 847 |
{ |
| 848 |
g_message ("invalid \"content-disposition\" external option"); |
| 849 |
return FALSE; |
| 850 |
} |
| 851 |
if (content_disposition) |
| 852 |
*content_disposition = value; |
| 853 |
|
| 854 |
if (!cockpit_json_get_string (external, "content-type", NULL, &value) || |
| 855 |
(value && !cockpit_web_response_is_header_value (value))) |
| 856 |
{ |
| 857 |
g_message ("invalid \"content-type\" external option"); |
| 858 |
return FALSE; |
| 859 |
} |
| 860 |
if (content_type) |
| 861 |
*content_type = value; |
| 862 |
|
| 863 |
if (!cockpit_json_get_string (external, "content-encoding", NULL, &value) || |
| 864 |
(value && !cockpit_web_response_is_header_value (value))) |
| 865 |
{ |
| 866 |
g_message ("invalid \"content-encoding\" external option"); |
| 867 |
return FALSE; |
| 868 |
} |
| 869 |
if (content_encoding) |
| 870 |
*content_encoding = value; |
| 871 |
|
| 872 |
if (!cockpit_json_get_strv (external, "protocols", NULL, protocols)) |
| 873 |
{ |
| 874 |
g_message ("invalid \"protocols\" external option"); |
| 875 |
return FALSE; |
| 876 |
} |
| 877 |
|
| 878 |
return TRUE; |
| 879 |
} |
| 880 |
|
| 881 |
static gboolean |
| 882 |
process_and_relay_open (CockpitWebService *self, |
| 883 |
CockpitSocket *socket, |
| 884 |
const gchar *channel, |
| 885 |
JsonObject *options) |
| 886 |
{ |
| 887 |
WebSocketDataType data_type = WEB_SOCKET_DATA_TEXT; |
| 888 |
GBytes *payload; |
| 889 |
|
| 890 |
if (self->closing) |
| 891 |
{ |
| 892 |
g_debug ("Ignoring open command while web socket is closing"); |
| 893 |
return TRUE; |
| 894 |
} |
| 895 |
|
| 896 |
if (channel == NULL) |
| 897 |
{ |
| 898 |
g_warning ("open command is missing the 'channel' field"); |
| 899 |
return FALSE; |
| 900 |
} |
| 901 |
|
| 902 |
if (cockpit_socket_lookup_by_channel (&self->sockets, channel)) |
| 903 |
{ |
| 904 |
g_warning ("cannot open a channel %s with the same id as another channel", channel); |
| 905 |
return FALSE; |
| 906 |
} |
| 907 |
|
| 908 |
if (!cockpit_web_service_parse_binary (options, &data_type)) |
| 909 |
return FALSE; |
| 910 |
|
| 911 |
if (socket) |
| 912 |
cockpit_socket_add_channel (&self->sockets, socket, channel, data_type); |
| 913 |
|
| 914 |
if (!self->sent_done) |
| 915 |
{ |
| 916 |
payload = cockpit_json_write_bytes (options); |
| 917 |
cockpit_transport_send (self->transport, NULL, payload); |
| 918 |
g_bytes_unref (payload); |
| 919 |
} |
| 920 |
|
| 921 |
return TRUE; |
| 922 |
} |
| 923 |
|
| 924 |
static void |
| 925 |
process_logout (CockpitWebService *self, |
| 926 |
JsonObject *options) |
| 927 |
{ |
| 928 |
/* Makes the credentials unusable */ |
| 929 |
cockpit_creds_poison (self->creds); |
| 930 |
|
| 931 |
/* Destroys our web service, disconnects everything */ |
| 932 |
g_info ("Logging out session %s", self->id); |
| 933 |
g_object_run_dispose (G_OBJECT (self)); |
| 934 |
} |
| 935 |
|
| 936 |
static const gchar * |
| 937 |
process_socket_init (CockpitWebService *self, |
| 938 |
CockpitSocket *socket, |
| 939 |
JsonObject *options) |
| 940 |
{ |
| 941 |
gint64 version; |
| 942 |
|
| 943 |
if (!cockpit_json_get_int (options, "version", -1, &version)) |
| 944 |
{ |
| 945 |
g_warning ("invalid version field in init message"); |
| 946 |
return "protocol-error"; |
| 947 |
} |
| 948 |
|
| 949 |
if (version == 1) |
| 950 |
{ |
| 951 |
g_debug ("received web socket init message"); |
| 952 |
if (socket->init_received) |
| 953 |
json_object_unref (socket->init_received); |
| 954 |
socket->init_received = json_object_ref (options); |
| 955 |
return NULL; |
| 956 |
} |
| 957 |
else |
| 958 |
{ |
| 959 |
g_message ("web socket used unsupported version of cockpit protocol: %" |
| 960 |
G_GINT64_FORMAT, version); |
| 961 |
return "not-supported"; |
| 962 |
} |
| 963 |
} |
| 964 |
|
| 965 |
static void |
| 966 |
inbound_protocol_error (CockpitWebService *self, |
| 967 |
WebSocketConnection *connection, |
| 968 |
const gchar *problem) |
| 969 |
{ |
| 970 |
GBytes *payload; |
| 971 |
|
| 972 |
if (problem == NULL) |
| 973 |
problem = "protocol-error"; |
| 974 |
|
| 975 |
if (web_socket_connection_get_ready_state (connection) == WEB_SOCKET_STATE_OPEN) |
| 976 |
{ |
| 977 |
payload = cockpit_transport_build_control ("command", "close", "problem", problem, NULL); |
| 978 |
web_socket_connection_send (connection, WEB_SOCKET_DATA_TEXT, self->control_prefix, payload); |
| 979 |
g_bytes_unref (payload); |
| 980 |
web_socket_connection_close (connection, WEB_SOCKET_CLOSE_SERVER_ERROR, problem); |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
static void |
| 985 |
dispatch_inbound_command (CockpitWebService *self, |
| 986 |
CockpitSocket *socket, |
| 987 |
GBytes *payload) |
| 988 |
{ |
| 989 |
const gchar *problem = "protocol-error"; |
| 990 |
const gchar *command; |
| 991 |
const gchar *channel; |
| 992 |
JsonObject *options = NULL; |
| 993 |
gboolean valid = FALSE; |
| 994 |
|
| 995 |
valid = cockpit_transport_parse_command (payload, &command, &channel, &options); |
| 996 |
if (!valid) |
| 997 |
goto out; |
| 998 |
|
| 999 |
if (g_strcmp0 (command, "init") == 0) |
| 1000 |
{ |
| 1001 |
problem = process_socket_init (self, socket, options); |
| 1002 |
valid = (problem == NULL); |
| 1003 |
goto out; |
| 1004 |
} |
| 1005 |
|
| 1006 |
if (!socket->init_received) |
| 1007 |
{ |
| 1008 |
g_message ("web socket did not send 'init' message first"); |
| 1009 |
valid = FALSE; |
| 1010 |
goto out; |
| 1011 |
} |
| 1012 |
|
| 1013 |
valid = TRUE; |
| 1014 |
|
| 1015 |
if (g_strcmp0 (command, "open") == 0) |
| 1016 |
{ |
| 1017 |
valid = process_and_relay_open (self, socket, channel, options); |
| 1018 |
} |
| 1019 |
else if (g_strcmp0 (command, "authorize") == 0) |
| 1020 |
{ |
| 1021 |
valid = process_socket_authorize (self, socket, channel, options, payload); |
| 1022 |
} |
| 1023 |
else if (g_strcmp0 (command, "logout") == 0) |
| 1024 |
{ |
| 1025 |
process_logout (self, options); |
| 1026 |
} |
| 1027 |
else if (g_strcmp0 (command, "close") == 0) |
| 1028 |
{ |
| 1029 |
if (channel == NULL) |
| 1030 |
{ |
| 1031 |
g_warning ("got close command without a channel"); |
| 1032 |
valid = FALSE; |
| 1033 |
} |
| 1034 |
else |
| 1035 |
{ |
| 1036 |
valid = process_and_relay_close (self, socket, channel, payload); |
| 1037 |
} |
| 1038 |
} |
| 1039 |
else if (g_strcmp0 (command, "kill") == 0) |
| 1040 |
{ |
| 1041 |
valid = process_kill (self, socket, options, payload); |
| 1042 |
} |
| 1043 |
else if (!channel && g_strcmp0 (command, "ping") == 0) |
| 1044 |
{ |
| 1045 |
valid = process_ping (self, socket, options); |
| 1046 |
} |
| 1047 |
else if (channel) |
| 1048 |
{ |
| 1049 |
/* Relay anything with a channel by default */ |
| 1050 |
if (!self->sent_done) |
| 1051 |
cockpit_transport_send (self->transport, NULL, payload); |
| 1052 |
} |
| 1053 |
|
| 1054 |
out: |
| 1055 |
if (!valid) |
| 1056 |
inbound_protocol_error (self, socket->connection, problem); |
| 1057 |
if (options) |
| 1058 |
json_object_unref (options); |
| 1059 |
} |
| 1060 |
|
| 1061 |
static void |
| 1062 |
on_web_socket_message (WebSocketConnection *connection, |
| 1063 |
WebSocketDataType type, |
| 1064 |
GBytes *message, |
| 1065 |
CockpitWebService *self) |
| 1066 |
{ |
| 1067 |
CockpitSocket *socket; |
| 1068 |
g_autofree gchar *channel = NULL; |
| 1069 |
|
| 1070 |
socket = cockpit_socket_lookup_by_connection (&self->sockets, connection); |
| 1071 |
g_return_if_fail (socket != NULL); |
| 1072 |
|
| 1073 |
g_autoptr(GBytes) payload = cockpit_transport_parse_frame (message, &channel); |
| 1074 |
if (!payload) |
| 1075 |
return; |
| 1076 |
|
| 1077 |
/* A control channel command */ |
| 1078 |
if (!channel) |
| 1079 |
dispatch_inbound_command (self, socket, payload); |
| 1080 |
|
| 1081 |
/* An actual payload message */ |
| 1082 |
else if (!self->closing) |
| 1083 |
{ |
| 1084 |
if (!self->sent_done) |
| 1085 |
cockpit_transport_send (self->transport, channel, payload); |
| 1086 |
} |
| 1087 |
} |
| 1088 |
|
| 1089 |
static void |
| 1090 |
on_web_socket_open (WebSocketConnection *connection, |
| 1091 |
CockpitWebService *self) |
| 1092 |
{ |
| 1093 |
CockpitSocket *socket; |
| 1094 |
JsonArray *capabilities; |
| 1095 |
GBytes *command; |
| 1096 |
JsonObject *object; |
| 1097 |
JsonObject *info; |
| 1098 |
|
| 1099 |
if (cockpit_creds_get_rhost (self->creds)) |
| 1100 |
g_info ("New connection to session %s from %s", self->id, cockpit_creds_get_rhost (self->creds)); |
| 1101 |
else |
| 1102 |
g_info ("New connection to session %s", self->id); |
| 1103 |
|
| 1104 |
socket = cockpit_socket_lookup_by_connection (&self->sockets, connection); |
| 1105 |
g_return_if_fail (socket != NULL); |
| 1106 |
|
| 1107 |
object = json_object_new (); |
| 1108 |
json_object_set_string_member (object, "command", "init"); |
| 1109 |
json_object_set_int_member (object, "version", 1); |
| 1110 |
json_object_set_string_member (object, "channel-seed", socket->id); |
| 1111 |
json_object_set_string_member (object, "host", "localhost"); |
| 1112 |
json_object_set_string_member (object, "csrf-token", cockpit_creds_get_csrf_token (self->creds)); |
| 1113 |
|
| 1114 |
capabilities = json_array_new (); |
| 1115 |
json_array_add_string_element (capabilities, "multi"); |
| 1116 |
json_array_add_string_element (capabilities, "credentials"); |
| 1117 |
json_array_add_string_element (capabilities, "binary"); |
| 1118 |
// https://github.com/cockpit-project/cockpit/pull/22310 |
| 1119 |
// commit a4dfe17769d576d34f226bb653e0a26d1e5156af |
| 1120 |
json_array_add_string_element (capabilities, "websocket-channel-close-fix"); |
| 1121 |
json_object_set_array_member (object, "capabilities", capabilities); |
| 1122 |
|
| 1123 |
info = json_object_new (); |
| 1124 |
json_object_set_string_member (info, "version", PACKAGE_VERSION); |
| 1125 |
json_object_set_object_member (object, "system", info); |
| 1126 |
|
| 1127 |
command = cockpit_json_write_bytes (object); |
| 1128 |
json_object_unref (object); |
| 1129 |
|
| 1130 |
web_socket_connection_send (connection, WEB_SOCKET_DATA_TEXT, self->control_prefix, command); |
| 1131 |
g_bytes_unref (command); |
| 1132 |
|
| 1133 |
g_signal_connect (connection, "message", |
| 1134 |
G_CALLBACK (on_web_socket_message), self); |
| 1135 |
} |
| 1136 |
|
| 1137 |
static gboolean |
| 1138 |
on_web_socket_closing (WebSocketConnection *connection, |
| 1139 |
CockpitWebService *self) |
| 1140 |
{ |
| 1141 |
CockpitSocket *socket; |
| 1142 |
GHashTable *snapshot; |
| 1143 |
GHashTableIter iter; |
| 1144 |
const gchar *channel; |
| 1145 |
GBytes *payload; |
| 1146 |
|
| 1147 |
g_debug ("web socket closing"); |
| 1148 |
|
| 1149 |
if (self->sent_done) |
| 1150 |
return TRUE; |
| 1151 |
|
| 1152 |
/* Close any channels that were opened by this web socket */ |
| 1153 |
snapshot = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); |
| 1154 |
socket = cockpit_socket_lookup_by_connection (&self->sockets, connection); |
| 1155 |
if (socket) |
| 1156 |
{ |
| 1157 |
g_hash_table_iter_init (&iter, socket->channels); |
| 1158 |
while (g_hash_table_iter_next (&iter, (gpointer *)&channel, NULL)) |
| 1159 |
{ |
| 1160 |
g_hash_table_add (snapshot, g_strdup (channel)); |
| 1161 |
} |
| 1162 |
} |
| 1163 |
|
| 1164 |
g_hash_table_iter_init (&iter, snapshot); |
| 1165 |
while (g_hash_table_iter_next (&iter, (gpointer *)&channel, NULL)) |
| 1166 |
{ |
| 1167 |
payload = cockpit_transport_build_control ("command", "close", |
| 1168 |
"channel", channel, |
| 1169 |
"problem", "disconnected", |
| 1170 |
NULL); |
| 1171 |
cockpit_transport_send (self->transport, NULL, payload); |
| 1172 |
g_bytes_unref (payload); |
| 1173 |
} |
| 1174 |
g_hash_table_destroy (snapshot); |
| 1175 |
|
| 1176 |
return TRUE; |
| 1177 |
} |
| 1178 |
|
| 1179 |
static void |
| 1180 |
on_web_socket_close (WebSocketConnection *connection, |
| 1181 |
CockpitWebService *self) |
| 1182 |
{ |
| 1183 |
CockpitSocket *socket; |
| 1184 |
|
| 1185 |
if (cockpit_creds_get_rhost (self->creds)) |
| 1186 |
g_info ("Connection from %s to session %s closed", cockpit_creds_get_rhost (self->creds), self->id); |
| 1187 |
else |
| 1188 |
g_info ("Connection to session %s closed", self->id); |
| 1189 |
|
| 1190 |
g_signal_handlers_disconnect_by_func (connection, on_web_socket_open, self); |
| 1191 |
g_signal_handlers_disconnect_by_func (connection, on_web_socket_closing, self); |
| 1192 |
g_signal_handlers_disconnect_by_func (connection, on_web_socket_close, self); |
| 1193 |
|
| 1194 |
socket = cockpit_socket_lookup_by_connection (&self->sockets, connection); |
| 1195 |
g_return_if_fail (socket != NULL); |
| 1196 |
|
| 1197 |
cockpit_socket_destroy (&self->sockets, socket); |
| 1198 |
|
| 1199 |
caller_end (self); |
| 1200 |
} |
| 1201 |
|
| 1202 |
static gboolean |
| 1203 |
on_ping_time (gpointer user_data) |
| 1204 |
{ |
| 1205 |
CockpitWebService *self = user_data; |
| 1206 |
WebSocketConnection *connection; |
| 1207 |
GHashTableIter iter; |
| 1208 |
GBytes *payload; |
| 1209 |
|
| 1210 |
payload = cockpit_transport_build_control ("command", "ping", NULL); |
| 1211 |
|
| 1212 |
g_hash_table_iter_init (&iter, self->sockets.by_connection); |
| 1213 |
while (g_hash_table_iter_next (&iter, (gpointer *)&connection, NULL)) |
| 1214 |
{ |
| 1215 |
if (web_socket_connection_get_ready_state (connection) == WEB_SOCKET_STATE_OPEN) |
| 1216 |
web_socket_connection_send (connection, WEB_SOCKET_DATA_TEXT, self->control_prefix, payload); |
| 1217 |
} |
| 1218 |
|
| 1219 |
g_bytes_unref (payload); |
| 1220 |
return TRUE; |
| 1221 |
} |
| 1222 |
|
| 1223 |
static void |
| 1224 |
cockpit_web_service_init (CockpitWebService *self) |
| 1225 |
{ |
| 1226 |
self->control_prefix = g_bytes_new_static ("\n", 1); |
| 1227 |
cockpit_sockets_init (&self->sockets); |
| 1228 |
self->ping_timeout = g_timeout_add_seconds (cockpit_ws_ping_interval, on_ping_time, self); |
| 1229 |
self->host_by_checksum = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); |
| 1230 |
self->checksum_by_host = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); |
| 1231 |
} |
| 1232 |
|
| 1233 |
static void |
| 1234 |
cockpit_web_service_class_init (CockpitWebServiceClass *klass) |
| 1235 |
{ |
| 1236 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 1237 |
object_class->dispose = cockpit_web_service_dispose; |
| 1238 |
object_class->finalize = cockpit_web_service_finalize; |
| 1239 |
|
| 1240 |
sig_idling = g_signal_new ("idling", COCKPIT_TYPE_WEB_SERVICE, |
| 1241 |
G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, |
| 1242 |
G_TYPE_NONE, 0); |
| 1243 |
|
| 1244 |
sig_destroy = g_signal_new ("destroy", COCKPIT_TYPE_WEB_SERVICE, |
| 1245 |
G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, |
| 1246 |
G_TYPE_NONE, 0); |
| 1247 |
} |
| 1248 |
|
| 1249 |
/** |
| 1250 |
* cockpit_web_service_new: |
| 1251 |
* @creds: credentials of user |
| 1252 |
* @transport: an new cockpit transport that has not yet |
| 1253 |
* sent an init message. |
| 1254 |
* |
| 1255 |
* Creates a new web service to serve web sockets and pass |
| 1256 |
* messages to the given bridge. |
| 1257 |
* |
| 1258 |
* Returns: (transfer full): the new web service |
| 1259 |
*/ |
| 1260 |
CockpitWebService * |
| 1261 |
cockpit_web_service_new (CockpitCreds *creds, |
| 1262 |
CockpitTransport *transport) |
| 1263 |
{ |
| 1264 |
CockpitWebService *self; |
| 1265 |
|
| 1266 |
g_return_val_if_fail (creds != NULL, NULL); |
| 1267 |
g_return_val_if_fail (transport != NULL, NULL); |
| 1268 |
|
| 1269 |
self = g_object_new (COCKPIT_TYPE_WEB_SERVICE, NULL); |
| 1270 |
self->creds = cockpit_creds_ref (creds); |
| 1271 |
self->transport = g_object_ref (transport); |
| 1272 |
|
| 1273 |
self->control_sig = g_signal_connect_after (self->transport, "control", G_CALLBACK (on_transport_control), self); |
| 1274 |
self->recv_sig = g_signal_connect_after (self->transport, "recv", G_CALLBACK (on_transport_recv), self); |
| 1275 |
self->closed_sig = g_signal_connect_after (self->transport, "closed", G_CALLBACK (on_transport_closed), self); |
| 1276 |
|
| 1277 |
return self; |
| 1278 |
} |
| 1279 |
|
| 1280 |
/* Escape a host string for fnmatch. |
| 1281 |
* IPv6 addresses look like [::1]:9090 which fnmatch treats as character classes. |
| 1282 |
*/ |
| 1283 |
static gchar * |
| 1284 |
escape_host_for_fnmatch (const gchar *host) |
| 1285 |
{ |
| 1286 |
GString* escaped = g_string_new(NULL); |
| 1287 |
for (int i = 0; host[i]; i++) |
| 1288 |
{ |
| 1289 |
if (strchr ("[]?*\\", host[i])) |
| 1290 |
g_string_append_c (escaped, '\\'); |
| 1291 |
g_string_append_c (escaped, host[i]); |
| 1292 |
} |
| 1293 |
|
| 1294 |
return g_string_free (escaped, FALSE); |
| 1295 |
} |
| 1296 |
|
| 1297 |
WebSocketConnection * |
| 1298 |
cockpit_web_service_create_socket (const gchar **protocols, |
| 1299 |
CockpitWebRequest *request) |
| 1300 |
{ |
| 1301 |
WebSocketConnection *connection; |
| 1302 |
const gchar * const *origins; |
| 1303 |
gchar *allocated = NULL; |
| 1304 |
gchar *origin = NULL; |
| 1305 |
gchar *defaults[2]; |
| 1306 |
|
| 1307 |
const gchar *host = cockpit_web_request_get_host (request); |
| 1308 |
const gchar *protocol = cockpit_web_request_get_protocol (request); |
| 1309 |
|
| 1310 |
origins = cockpit_conf_strv ("WebService", "Origins", ' '); |
| 1311 |
if (origins == NULL) |
| 1312 |
{ |
| 1313 |
/* Origins are checked with fnmatch(), escape [..] in IPv6 addresses */ |
| 1314 |
g_autofree gchar *escaped_host = escape_host_for_fnmatch (host); |
| 1315 |
origin = g_strdup_printf ("%s://%s", protocol, escaped_host); |
| 1316 |
|
| 1317 |
defaults[0] = origin; |
| 1318 |
defaults[1] = NULL; |
| 1319 |
origins = (const gchar **)defaults; |
| 1320 |
} |
| 1321 |
|
| 1322 |
connection = web_socket_server_new_for_stream (origins, protocols, |
| 1323 |
cockpit_web_request_get_io_stream (request), |
| 1324 |
cockpit_web_request_get_headers (request), |
| 1325 |
cockpit_web_request_get_buffer (request)); |
| 1326 |
g_free (allocated); |
| 1327 |
g_free (origin); |
| 1328 |
|
| 1329 |
return connection; |
| 1330 |
} |
| 1331 |
|
| 1332 |
/** |
| 1333 |
* cockpit_web_service_socket: |
| 1334 |
* @io_stream: the stream to talk on |
| 1335 |
* @headers: optional headers already parsed |
| 1336 |
* @input_buffer: optional bytes already parsed after headers |
| 1337 |
* @auth: authentication object |
| 1338 |
* @creds: credentials of user or NULL for failed auth |
| 1339 |
* @for_tls_proxy: Assume that the Browser is making TLS connections that are terminated |
| 1340 |
* in a reverse proxy in front of cockpit-ws |
| 1341 |
* |
| 1342 |
* Serves the WebSocket on the given web service. Holds an extra |
| 1343 |
* reference to the web service until the socket is closed. |
| 1344 |
*/ |
| 1345 |
void |
| 1346 |
cockpit_web_service_socket (CockpitWebService *self, |
| 1347 |
CockpitWebRequest *request) |
| 1348 |
{ |
| 1349 |
const gchar *protocols[] = { "cockpit1", NULL }; |
| 1350 |
WebSocketConnection *connection; |
| 1351 |
|
| 1352 |
connection = cockpit_web_service_create_socket (protocols, request); |
| 1353 |
|
| 1354 |
g_signal_connect (connection, "open", G_CALLBACK (on_web_socket_open), self); |
| 1355 |
g_signal_connect (connection, "closing", G_CALLBACK (on_web_socket_closing), self); |
| 1356 |
g_signal_connect (connection, "close", G_CALLBACK (on_web_socket_close), self); |
| 1357 |
|
| 1358 |
cockpit_socket_track (&self->sockets, connection); |
| 1359 |
g_object_unref (connection); |
| 1360 |
|
| 1361 |
caller_begin (self); |
| 1362 |
} |
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
/** |
| 1367 |
* cockpit_web_service_get_creds: |
| 1368 |
* @self: the service |
| 1369 |
* |
| 1370 |
* Returns: (transfer none): the credentials for which this service was opened. |
| 1371 |
*/ |
| 1372 |
CockpitCreds * |
| 1373 |
cockpit_web_service_get_creds (CockpitWebService *self) |
| 1374 |
{ |
| 1375 |
g_return_val_if_fail (COCKPIT_IS_WEB_SERVICE (self), NULL); |
| 1376 |
return self->creds; |
| 1377 |
} |
| 1378 |
|
| 1379 |
/** |
| 1380 |
* cockpit_web_service_get_id: |
| 1381 |
* @self: the service |
| 1382 |
* |
| 1383 |
* Returns: The id of this service, for logging. |
| 1384 |
*/ |
| 1385 |
const gchar * |
| 1386 |
cockpit_web_service_get_id (CockpitWebService *self) |
| 1387 |
{ |
| 1388 |
g_return_val_if_fail (COCKPIT_IS_WEB_SERVICE (self), NULL); |
| 1389 |
return self->id; |
| 1390 |
} |
| 1391 |
|
| 1392 |
/** |
| 1393 |
* cockpit_web_service_set_id: |
| 1394 |
* @self: the service |
| 1395 |
* @id: the id |
| 1396 |
*/ |
| 1397 |
void |
| 1398 |
cockpit_web_service_set_id (CockpitWebService *self, |
| 1399 |
const gchar *id) |
| 1400 |
{ |
| 1401 |
g_return_if_fail (COCKPIT_IS_WEB_SERVICE (self)); |
| 1402 |
if (!self->id) |
| 1403 |
self->id = g_strdup (id); |
| 1404 |
} |
| 1405 |
|
| 1406 |
/** |
| 1407 |
* cockpit_web_service_disconnect: |
| 1408 |
* @self: the service |
| 1409 |
* |
| 1410 |
* Close all sockets that are running in this web |
| 1411 |
* service. |
| 1412 |
*/ |
| 1413 |
void |
| 1414 |
cockpit_web_service_disconnect (CockpitWebService *self) |
| 1415 |
{ |
| 1416 |
g_object_run_dispose (G_OBJECT (self)); |
| 1417 |
} |
| 1418 |
|
| 1419 |
gboolean |
| 1420 |
cockpit_web_service_get_idling (CockpitWebService *self) |
| 1421 |
{ |
| 1422 |
g_return_val_if_fail (COCKPIT_IS_WEB_SERVICE (self), TRUE); |
| 1423 |
return (self->callers == 0); |
| 1424 |
} |
| 1425 |
|
| 1426 |
CockpitTransport * |
| 1427 |
cockpit_web_service_get_transport (CockpitWebService *self) |
| 1428 |
{ |
| 1429 |
g_return_val_if_fail (COCKPIT_IS_WEB_SERVICE (self), NULL); |
| 1430 |
return self->transport; |
| 1431 |
} |
| 1432 |
|
| 1433 |
JsonObject * |
| 1434 |
cockpit_web_service_get_init (CockpitWebService *self) |
| 1435 |
{ |
| 1436 |
g_return_val_if_fail (COCKPIT_IS_WEB_SERVICE (self), NULL); |
| 1437 |
return self->init_received; |
| 1438 |
} |
| 1439 |
|
| 1440 |
const gchar * |
| 1441 |
cockpit_web_service_get_host (CockpitWebService *self, |
| 1442 |
const gchar *checksum) |
| 1443 |
{ |
| 1444 |
return g_hash_table_lookup (self->host_by_checksum, checksum); |
| 1445 |
} |
| 1446 |
|
| 1447 |
const gchar * |
| 1448 |
cockpit_web_service_get_checksum (CockpitWebService *self, |
| 1449 |
const gchar *host) |
| 1450 |
{ |
| 1451 |
return g_hash_table_lookup (self->checksum_by_host, host); |
| 1452 |
} |
| 1453 |
|
| 1454 |
void |
| 1455 |
cockpit_web_service_set_host_checksum (CockpitWebService *self, |
| 1456 |
const gchar *host, |
| 1457 |
const gchar *checksum) |
| 1458 |
{ |
| 1459 |
const gchar *old_checksum = g_hash_table_lookup (self->checksum_by_host, host); |
| 1460 |
const gchar *old_host = g_hash_table_lookup (self->host_by_checksum, checksum); |
| 1461 |
|
| 1462 |
if (g_strcmp0 (checksum, old_checksum) == 0) |
| 1463 |
return; |
| 1464 |
|
| 1465 |
if (old_checksum) |
| 1466 |
g_hash_table_remove (self->host_by_checksum, old_checksum); |
| 1467 |
|
| 1468 |
/* Only replace checksum if the old one wasn't localhost */ |
| 1469 |
if (g_strcmp0 (old_host, "localhost") != 0) |
| 1470 |
g_hash_table_replace (self->host_by_checksum, g_strdup (checksum), g_strdup (host)); |
| 1471 |
|
| 1472 |
g_hash_table_replace (self->checksum_by_host, g_strdup (host), g_strdup (checksum)); |
| 1473 |
} |