| 1 |
/* |
| 2 |
* Copyright (C) 2013 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "cockpithandlers.h" |
| 9 |
|
| 10 |
#include "cockpitbranding.h" |
| 11 |
#include "cockpitchannelresponse.h" |
| 12 |
#include "cockpitchannelsocket.h" |
| 13 |
#include "cockpitwebservice.h" |
| 14 |
#include "cockpitws.h" |
| 15 |
|
| 16 |
#include "common/cockpitconf.h" |
| 17 |
#include "cockpitjson.h" |
| 18 |
#include "common/cockpitwebcertificate.h" |
| 19 |
#include "cockpitwebinject.h" |
| 20 |
|
| 21 |
#include "websocket.h" |
| 22 |
|
| 23 |
#include <json-glib/json-glib.h> |
| 24 |
|
| 25 |
#include <gio/gio.h> |
| 26 |
|
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
/* For overriding during tests */ |
| 30 |
const gchar *cockpit_ws_shell_component = "/shell/index.html"; |
| 31 |
|
| 32 |
static gchar * |
| 33 |
locate_selfsign_ca (void) |
| 34 |
{ |
| 35 |
g_autofree gchar *cert_path = NULL; |
| 36 |
gchar *ca_path = NULL; |
| 37 |
gchar *error = NULL; |
| 38 |
|
| 39 |
cert_path = cockpit_certificate_locate (true, &error); |
| 40 |
if (cert_path && g_str_has_suffix (cert_path, "/0-self-signed.cert")) |
| 41 |
{ |
| 42 |
g_autofree gchar *dir = g_path_get_dirname (cert_path); |
| 43 |
ca_path = g_build_filename (dir, "0-self-signed-ca.pem", NULL); |
| 44 |
if (!g_file_test (ca_path, G_FILE_TEST_EXISTS)) |
| 45 |
{ |
| 46 |
g_free (ca_path); |
| 47 |
ca_path = NULL; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return ca_path; |
| 52 |
} |
| 53 |
|
| 54 |
static void |
| 55 |
on_web_socket_noauth (WebSocketConnection *connection, |
| 56 |
gpointer data) |
| 57 |
{ |
| 58 |
GBytes *payload; |
| 59 |
GBytes *prefix; |
| 60 |
|
| 61 |
g_debug ("closing unauthenticated web socket"); |
| 62 |
|
| 63 |
payload = cockpit_transport_build_control ("command", "init", "problem", "no-session", NULL); |
| 64 |
prefix = g_bytes_new_static ("\n", 1); |
| 65 |
|
| 66 |
web_socket_connection_send (connection, WEB_SOCKET_DATA_TEXT, prefix, payload); |
| 67 |
web_socket_connection_close (connection, WEB_SOCKET_CLOSE_GOING_AWAY, "no-session"); |
| 68 |
|
| 69 |
g_bytes_unref (prefix); |
| 70 |
g_bytes_unref (payload); |
| 71 |
} |
| 72 |
|
| 73 |
static void |
| 74 |
handle_noauth_socket (CockpitWebRequest *request) |
| 75 |
{ |
| 76 |
WebSocketConnection *connection; |
| 77 |
|
| 78 |
connection = cockpit_web_service_create_socket (NULL, request); |
| 79 |
|
| 80 |
g_signal_connect (connection, "open", G_CALLBACK (on_web_socket_noauth), NULL); |
| 81 |
|
| 82 |
/* Unreferences connection when it closes */ |
| 83 |
g_signal_connect (connection, "close", G_CALLBACK (g_object_unref), NULL); |
| 84 |
} |
| 85 |
|
| 86 |
/* Called by @server when handling HTTP requests to /cockpit/socket */ |
| 87 |
gboolean |
| 88 |
cockpit_handler_socket (CockpitWebServer *server, |
| 89 |
CockpitWebRequest *request, |
| 90 |
CockpitHandlerData *ws) |
| 91 |
{ |
| 92 |
const gchar *path = cockpit_web_request_get_path (request); |
| 93 |
const gchar *method = cockpit_web_request_get_method (request); |
| 94 |
GHashTable *headers = cockpit_web_request_get_headers (request); |
| 95 |
|
| 96 |
CockpitWebService *service = NULL; |
| 97 |
const gchar *segment = NULL; |
| 98 |
|
| 99 |
/* |
| 100 |
* Socket requests should come in on /cockpit/socket or /cockpit+app/socket. |
| 101 |
* However older javascript may connect on /socket, so we continue to support that. |
| 102 |
*/ |
| 103 |
|
| 104 |
if (path && path[0]) |
| 105 |
segment = strchr (path + 1, '/'); |
| 106 |
if (!segment) |
| 107 |
segment = path; |
| 108 |
|
| 109 |
if (!segment || !g_str_equal (segment, "/socket")) |
| 110 |
return FALSE; |
| 111 |
|
| 112 |
/* don't support HEAD on a socket, it makes little sense */ |
| 113 |
if (g_strcmp0 (method, "GET") != 0) |
| 114 |
return FALSE; |
| 115 |
|
| 116 |
if (headers && ws) |
| 117 |
service = cockpit_auth_check_cookie (ws->auth, request); |
| 118 |
if (service) |
| 119 |
{ |
| 120 |
cockpit_web_service_socket (service, request); |
| 121 |
} |
| 122 |
else |
| 123 |
{ |
| 124 |
handle_noauth_socket (request); |
| 125 |
} |
| 126 |
|
| 127 |
return TRUE; |
| 128 |
} |
| 129 |
|
| 130 |
gboolean |
| 131 |
cockpit_handler_external (CockpitWebServer *server, |
| 132 |
CockpitWebRequest *request, |
| 133 |
CockpitHandlerData *ws) |
| 134 |
{ |
| 135 |
const gchar *path = cockpit_web_request_get_path (request); |
| 136 |
GHashTable *headers = cockpit_web_request_get_headers (request); |
| 137 |
|
| 138 |
CockpitWebResponse *response = NULL; |
| 139 |
CockpitWebService *service = NULL; |
| 140 |
const gchar *segment = NULL; |
| 141 |
JsonObject *open = NULL; |
| 142 |
CockpitCreds *creds; |
| 143 |
const gchar *expected; |
| 144 |
const gchar *upgrade; |
| 145 |
guchar *decoded; |
| 146 |
GBytes *bytes; |
| 147 |
gsize length; |
| 148 |
|
| 149 |
/* The path must start with /cockpit+xxx/channel/csrftoken? or similar */ |
| 150 |
if (path && path[0]) |
| 151 |
segment = strchr (path + 1, '/'); |
| 152 |
if (!segment) |
| 153 |
return FALSE; |
| 154 |
if (!g_str_has_prefix (segment, "/channel/")) |
| 155 |
return FALSE; |
| 156 |
segment += 9; |
| 157 |
|
| 158 |
/* Make sure we are authenticated, otherwise 404 */ |
| 159 |
service = cockpit_auth_check_cookie (ws->auth, request); |
| 160 |
if (!service) |
| 161 |
return FALSE; |
| 162 |
|
| 163 |
creds = cockpit_web_service_get_creds (service); |
| 164 |
g_return_val_if_fail (creds != NULL, FALSE); |
| 165 |
|
| 166 |
expected = cockpit_creds_get_csrf_token (creds); |
| 167 |
g_return_val_if_fail (expected != NULL, FALSE); |
| 168 |
|
| 169 |
/* No such path is valid */ |
| 170 |
if (!g_str_equal (segment, expected)) |
| 171 |
{ |
| 172 |
g_message ("invalid csrf token"); |
| 173 |
return FALSE; |
| 174 |
} |
| 175 |
|
| 176 |
decoded = g_base64_decode (cockpit_web_request_get_query (request), &length); |
| 177 |
if (decoded) |
| 178 |
{ |
| 179 |
bytes = g_bytes_new_take (decoded, length); |
| 180 |
if (!cockpit_transport_parse_command (bytes, NULL, NULL, &open)) |
| 181 |
{ |
| 182 |
open = NULL; |
| 183 |
g_message ("invalid external channel query"); |
| 184 |
} |
| 185 |
g_bytes_unref (bytes); |
| 186 |
} |
| 187 |
|
| 188 |
if (!open) |
| 189 |
{ |
| 190 |
response = cockpit_web_request_respond (request); |
| 191 |
cockpit_web_response_error (response, 400, NULL, NULL); |
| 192 |
g_object_unref (response); |
| 193 |
} |
| 194 |
else |
| 195 |
{ |
| 196 |
upgrade = g_hash_table_lookup (headers, "Upgrade"); |
| 197 |
if (upgrade && g_ascii_strcasecmp (upgrade, "websocket") == 0) |
| 198 |
{ |
| 199 |
cockpit_channel_socket_open (service, open, request); |
| 200 |
} |
| 201 |
else |
| 202 |
{ |
| 203 |
cockpit_channel_response_open (service, request, open); |
| 204 |
} |
| 205 |
json_object_unref (open); |
| 206 |
} |
| 207 |
|
| 208 |
g_object_unref (service); |
| 209 |
|
| 210 |
return TRUE; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
static void |
| 215 |
add_oauth_to_environment (JsonObject *environment) |
| 216 |
{ |
| 217 |
static const gchar *url; |
| 218 |
JsonObject *object; |
| 219 |
|
| 220 |
url = cockpit_conf_string ("OAuth", "URL"); |
| 221 |
|
| 222 |
if (url) |
| 223 |
{ |
| 224 |
object = json_object_new (); |
| 225 |
json_object_set_string_member (object, "URL", url); |
| 226 |
json_object_set_string_member (object, "ErrorParam", |
| 227 |
cockpit_conf_string ("oauth", "ErrorParam")); |
| 228 |
json_object_set_string_member (object, "TokenParam", |
| 229 |
cockpit_conf_string ("oauth", "TokenParam")); |
| 230 |
json_object_set_object_member (environment, "OAuth", object); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
static bool have_command (const char *name) |
| 235 |
{ |
| 236 |
gint status; |
| 237 |
g_autoptr(GError) error = NULL; |
| 238 |
g_autofree gchar *command = g_strdup_printf ("command -v %s", name); |
| 239 |
if (g_spawn_sync (NULL, |
| 240 |
(gchar * []){ "/bin/sh", "-ec", command, NULL }, |
| 241 |
NULL, |
| 242 |
G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL, |
| 243 |
NULL, NULL, NULL, NULL, &status, &error)) |
| 244 |
return status == 0; |
| 245 |
else |
| 246 |
{ |
| 247 |
g_warning ("Failed to check for %s: %s", name, error->message); |
| 248 |
return FALSE; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
static void |
| 253 |
add_page_to_environment (JsonObject *object, |
| 254 |
gboolean is_cockpit_client) |
| 255 |
{ |
| 256 |
static gint page_login_to = -1; |
| 257 |
gboolean require_host = FALSE; |
| 258 |
gboolean allow_multihost; |
| 259 |
JsonObject *page; |
| 260 |
const gchar *value; |
| 261 |
|
| 262 |
page = json_object_new (); |
| 263 |
|
| 264 |
value = cockpit_conf_string ("WebService", "LoginTitle"); |
| 265 |
if (value) |
| 266 |
json_object_set_string_member (page, "title", value); |
| 267 |
|
| 268 |
if (page_login_to < 0) |
| 269 |
{ |
| 270 |
/* cockpit.beiboot is part of cockpit-bridge package */ |
| 271 |
gboolean have_ssh = have_command ("ssh") && have_command ("cockpit-bridge"); |
| 272 |
if (!have_ssh) |
| 273 |
g_info ("cockpit-bridge or ssh are not available, disabling remote logins"); |
| 274 |
page_login_to = cockpit_conf_bool ("WebService", "LoginTo", have_ssh); |
| 275 |
} |
| 276 |
|
| 277 |
require_host = is_cockpit_client || cockpit_conf_bool ("WebService", "RequireHost", FALSE); |
| 278 |
allow_multihost = cockpit_conf_bool ("WebService", "AllowMultiHost", ALLOW_MULTIHOST_DEFAULT); |
| 279 |
|
| 280 |
json_object_set_boolean_member (page, "connect", page_login_to); |
| 281 |
json_object_set_boolean_member (page, "require_host", require_host); |
| 282 |
json_object_set_boolean_member (page, "allow_multihost", allow_multihost); |
| 283 |
json_object_set_object_member (object, "page", page); |
| 284 |
} |
| 285 |
|
| 286 |
static void |
| 287 |
add_logged_into_to_environment (JsonObject *object, |
| 288 |
CockpitAuth *auth, |
| 289 |
GHashTable *request_headers) |
| 290 |
{ |
| 291 |
JsonArray *logged_into = json_array_new (); |
| 292 |
|
| 293 |
gchar *h = g_hash_table_lookup (request_headers, "Cookie"); |
| 294 |
while (h && *h) { |
| 295 |
const gchar *start = h; |
| 296 |
while (*h && *h != '=') |
| 297 |
h++; |
| 298 |
const gchar *equal = h; |
| 299 |
while (*h && *h != ';') |
| 300 |
h++; |
| 301 |
const gchar *end = h; |
| 302 |
if (*h) |
| 303 |
h++; |
| 304 |
while (*h && *h == ' ') |
| 305 |
h++; |
| 306 |
|
| 307 |
if (*equal != '=') |
| 308 |
continue; |
| 309 |
|
| 310 |
g_autofree gchar *value = g_strndup (equal + 1, end - equal - 1); |
| 311 |
|
| 312 |
if (!cockpit_auth_is_valid_cookie_value (auth, value)) |
| 313 |
continue; |
| 314 |
|
| 315 |
g_autofree gchar *name = g_strndup (start, equal - start); |
| 316 |
if (g_str_equal (name, "cockpit")) |
| 317 |
json_array_add_string_element(logged_into, "."); |
| 318 |
else if (g_str_has_prefix (name, "machine-cockpit+")) |
| 319 |
json_array_add_string_element(logged_into, name + strlen("machine-cockpit+")); |
| 320 |
} |
| 321 |
|
| 322 |
json_object_set_array_member (object, "logged_into", logged_into); |
| 323 |
} |
| 324 |
|
| 325 |
static GBytes * |
| 326 |
build_environment (CockpitAuth *auth, GHashTable *request_headers) |
| 327 |
{ |
| 328 |
static const gchar *prefix = "\n <script>\nvar environment = "; |
| 329 |
static const gchar *suffix = ";\n </script>"; |
| 330 |
|
| 331 |
GByteArray *buffer; |
| 332 |
GBytes *bytes; |
| 333 |
JsonObject *object; |
| 334 |
gchar *hostname; |
| 335 |
|
| 336 |
object = json_object_new (); |
| 337 |
|
| 338 |
gboolean is_cockpit_client = cockpit_conf_bool ("WebService", "X-For-CockpitClient", FALSE); |
| 339 |
json_object_set_boolean_member (object, "is_cockpit_client", is_cockpit_client); |
| 340 |
|
| 341 |
add_page_to_environment (object, is_cockpit_client); |
| 342 |
add_logged_into_to_environment (object, auth, request_headers); |
| 343 |
|
| 344 |
hostname = g_malloc0 (HOST_NAME_MAX + 1); |
| 345 |
gethostname (hostname, HOST_NAME_MAX); |
| 346 |
hostname[HOST_NAME_MAX] = '\0'; |
| 347 |
json_object_set_string_member (object, "hostname", hostname); |
| 348 |
g_free (hostname); |
| 349 |
|
| 350 |
add_oauth_to_environment (object); |
| 351 |
|
| 352 |
g_autofree gchar *ca_path = locate_selfsign_ca (); |
| 353 |
if (ca_path) |
| 354 |
json_object_set_string_member (object, "CACertUrl", "/ca.cer"); |
| 355 |
|
| 356 |
g_autofree gchar *contents = NULL; |
| 357 |
g_autoptr(GError) error = NULL; |
| 358 |
gsize len; |
| 359 |
|
| 360 |
const gchar *banner = cockpit_conf_string ("Session", "Banner"); |
| 361 |
if (banner) |
| 362 |
{ |
| 363 |
// TODO: parse macros (see `man agetty` for possible macros) |
| 364 |
g_file_get_contents (banner, &contents, &len, &error); |
| 365 |
if (error) |
| 366 |
g_message ("error loading contents of banner: %s", error->message); |
| 367 |
else |
| 368 |
json_object_set_string_member (object, "banner", contents); |
| 369 |
} |
| 370 |
|
| 371 |
bytes = cockpit_json_write_bytes (object); |
| 372 |
json_object_unref (object); |
| 373 |
|
| 374 |
buffer = g_bytes_unref_to_array (bytes); |
| 375 |
g_byte_array_prepend (buffer, (const guint8 *)prefix, strlen (prefix)); |
| 376 |
g_byte_array_append (buffer, (const guint8 *)suffix, strlen (suffix)); |
| 377 |
return g_byte_array_free_to_bytes (buffer); |
| 378 |
} |
| 379 |
|
| 380 |
static void |
| 381 |
send_login_html (CockpitWebResponse *response, |
| 382 |
CockpitHandlerData *ws, |
| 383 |
const gchar *path, |
| 384 |
GHashTable *headers) |
| 385 |
{ |
| 386 |
static const gchar *marker = "<meta insert=\"dynamic_content_here\" />"; |
| 387 |
static const gchar *po_marker = "/*insert_translations_here*/"; |
| 388 |
|
| 389 |
CockpitWebFilter *filter; |
| 390 |
GBytes *environment; |
| 391 |
GError *error = NULL; |
| 392 |
GBytes *bytes; |
| 393 |
|
| 394 |
GBytes *url_bytes = NULL; |
| 395 |
CockpitWebFilter *filter2 = NULL; |
| 396 |
const gchar *url_root = NULL; |
| 397 |
const gchar *accept = NULL; |
| 398 |
gchar *content_security_policy = NULL; |
| 399 |
gchar *cookie_line = NULL; |
| 400 |
gchar *base; |
| 401 |
|
| 402 |
gchar *language = NULL; |
| 403 |
gchar **languages = NULL; |
| 404 |
GBytes *po_bytes; |
| 405 |
CockpitWebFilter *filter3 = NULL; |
| 406 |
|
| 407 |
environment = build_environment (ws->auth, headers); |
| 408 |
filter = cockpit_web_inject_new (marker, environment, 1); |
| 409 |
g_bytes_unref (environment); |
| 410 |
cockpit_web_response_add_filter (response, filter); |
| 411 |
g_object_unref (filter); |
| 412 |
|
| 413 |
url_root = cockpit_web_response_get_url_root (response); |
| 414 |
if (url_root) |
| 415 |
base = g_strdup_printf ("<base href=\"%s/\">", url_root); |
| 416 |
else |
| 417 |
base = g_strdup ("<base href=\"/\">"); |
| 418 |
|
| 419 |
url_bytes = g_bytes_new_take (base, strlen(base)); |
| 420 |
filter2 = cockpit_web_inject_new (marker, url_bytes, 1); |
| 421 |
g_bytes_unref (url_bytes); |
| 422 |
cockpit_web_response_add_filter (response, filter2); |
| 423 |
g_object_unref (filter2); |
| 424 |
|
| 425 |
cockpit_web_response_set_cache_type (response, COCKPIT_WEB_RESPONSE_NO_CACHE); |
| 426 |
|
| 427 |
if (ws->login_po_js) |
| 428 |
{ |
| 429 |
language = cockpit_web_server_parse_cookie (headers, "CockpitLang"); |
| 430 |
if (!language) |
| 431 |
{ |
| 432 |
accept = g_hash_table_lookup (headers, "Accept-Language"); |
| 433 |
languages = cockpit_web_server_parse_accept_list (accept, NULL); |
| 434 |
language = languages[0]; |
| 435 |
} |
| 436 |
|
| 437 |
po_bytes = cockpit_web_response_negotiation (ws->login_po_js, NULL, language, NULL, NULL, &error); |
| 438 |
if (error) |
| 439 |
{ |
| 440 |
g_message ("%s", error->message); |
| 441 |
g_clear_error (&error); |
| 442 |
} |
| 443 |
else if (po_bytes) |
| 444 |
{ |
| 445 |
filter3 = cockpit_web_inject_new (po_marker, po_bytes, 1); |
| 446 |
g_bytes_unref (po_bytes); |
| 447 |
cockpit_web_response_add_filter (response, filter3); |
| 448 |
g_object_unref (filter3); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
bytes = cockpit_web_response_negotiation (ws->login_html, NULL, NULL, NULL, NULL, &error); |
| 453 |
if (error) |
| 454 |
{ |
| 455 |
g_message ("%s", error->message); |
| 456 |
cockpit_web_response_error (response, 500, NULL, NULL); |
| 457 |
g_error_free (error); |
| 458 |
} |
| 459 |
else if (!bytes) |
| 460 |
{ |
| 461 |
cockpit_web_response_error (response, 404, NULL, NULL); |
| 462 |
} |
| 463 |
else |
| 464 |
{ |
| 465 |
/* The login Content-Security-Policy allows the page to have inline <script> and <style> tags. */ |
| 466 |
gboolean secure = g_strcmp0 (cockpit_web_response_get_protocol (response), "https") == 0; |
| 467 |
cookie_line = cockpit_auth_empty_cookie_value (path, secure); |
| 468 |
content_security_policy = cockpit_web_response_security_policy ("default-src 'self' 'unsafe-inline'", |
| 469 |
cockpit_web_response_get_origin (response)); |
| 470 |
|
| 471 |
cockpit_web_response_headers (response, 200, "OK", -1, |
| 472 |
"Content-Type", "text/html", |
| 473 |
"Content-Security-Policy", content_security_policy, |
| 474 |
"Set-Cookie", cookie_line, |
| 475 |
NULL); |
| 476 |
cockpit_web_response_set_cache_type (response, COCKPIT_WEB_RESPONSE_NO_CACHE); |
| 477 |
if (cockpit_web_response_queue (response, bytes)) |
| 478 |
cockpit_web_response_complete (response); |
| 479 |
|
| 480 |
g_bytes_unref (bytes); |
| 481 |
} |
| 482 |
|
| 483 |
g_free (cookie_line); |
| 484 |
g_free (content_security_policy); |
| 485 |
if (languages) |
| 486 |
g_strfreev (languages); |
| 487 |
else |
| 488 |
g_free (language); |
| 489 |
} |
| 490 |
|
| 491 |
static void |
| 492 |
send_login_response (CockpitWebResponse *response, |
| 493 |
JsonObject *object, |
| 494 |
GHashTable *headers) |
| 495 |
{ |
| 496 |
GBytes *content; |
| 497 |
|
| 498 |
content = cockpit_json_write_bytes (object); |
| 499 |
|
| 500 |
g_hash_table_replace (headers, g_strdup ("Content-Type"), g_strdup ("application/json")); |
| 501 |
cockpit_web_response_content (response, headers, content, NULL); |
| 502 |
g_bytes_unref (content); |
| 503 |
} |
| 504 |
|
| 505 |
static void |
| 506 |
on_login_complete (GObject *object, |
| 507 |
GAsyncResult *result, |
| 508 |
gpointer user_data) |
| 509 |
{ |
| 510 |
// consumes the reference on user_data |
| 511 |
g_autoptr(CockpitWebResponse) response = g_steal_pointer (&user_data); |
| 512 |
|
| 513 |
/* Never cache a login response */ |
| 514 |
g_autoptr(GHashTable) headers = cockpit_web_server_new_table (); |
| 515 |
cockpit_web_response_set_cache_type (response, COCKPIT_WEB_RESPONSE_NO_CACHE); |
| 516 |
|
| 517 |
g_autoptr(GError) error = NULL; |
| 518 |
g_autoptr(JsonObject) response_data = cockpit_auth_login_finish (COCKPIT_AUTH (object), result, |
| 519 |
cockpit_web_response_get_stream (response), |
| 520 |
headers, &error); |
| 521 |
|
| 522 |
if (error) |
| 523 |
{ |
| 524 |
g_autoptr(GBytes) body = NULL; |
| 525 |
|
| 526 |
if (response_data) |
| 527 |
{ |
| 528 |
g_hash_table_insert (headers, g_strdup ("Content-Type"), g_strdup ("application/json")); |
| 529 |
body = cockpit_json_write_bytes (response_data); |
| 530 |
} |
| 531 |
|
| 532 |
cockpit_web_response_gerror (response, headers, body, error); |
| 533 |
} |
| 534 |
else |
| 535 |
{ |
| 536 |
send_login_response (response, response_data, headers); |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
static void |
| 541 |
handle_login (CockpitHandlerData *data, |
| 542 |
CockpitWebService *service, |
| 543 |
CockpitWebRequest *request, |
| 544 |
CockpitWebResponse *response) |
| 545 |
{ |
| 546 |
GHashTable *out_headers; |
| 547 |
CockpitCreds *creds; |
| 548 |
JsonObject *creds_json = NULL; |
| 549 |
|
| 550 |
if (service) |
| 551 |
{ |
| 552 |
out_headers = cockpit_web_server_new_table (); |
| 553 |
creds = cockpit_web_service_get_creds (service); |
| 554 |
creds_json = cockpit_creds_to_json (creds); |
| 555 |
send_login_response (response, creds_json, out_headers); |
| 556 |
g_hash_table_unref (out_headers); |
| 557 |
json_object_unref (creds_json); |
| 558 |
return; |
| 559 |
} |
| 560 |
|
| 561 |
cockpit_auth_login_async (data->auth, request, on_login_complete, g_object_ref (response)); |
| 562 |
} |
| 563 |
|
| 564 |
static void |
| 565 |
handle_resource (CockpitHandlerData *data, |
| 566 |
CockpitWebService *service, |
| 567 |
const gchar *path, |
| 568 |
GHashTable *headers, |
| 569 |
CockpitWebResponse *response) |
| 570 |
{ |
| 571 |
gchar *where; |
| 572 |
|
| 573 |
where = cockpit_web_response_pop_path (response); |
| 574 |
if (where && (where[0] == '@' || where[0] == '$') && where[1] != '\0') |
| 575 |
{ |
| 576 |
if (service) |
| 577 |
{ |
| 578 |
cockpit_channel_response_serve (service, headers, response, where, |
| 579 |
cockpit_web_response_get_path (response)); |
| 580 |
} |
| 581 |
else if (g_str_has_suffix (path, ".html")) |
| 582 |
{ |
| 583 |
send_login_html (response, data, path, headers); |
| 584 |
} |
| 585 |
else |
| 586 |
{ |
| 587 |
cockpit_web_response_error (response, 401, NULL, NULL); |
| 588 |
} |
| 589 |
} |
| 590 |
else |
| 591 |
{ |
| 592 |
cockpit_web_response_error (response, 404, NULL, NULL); |
| 593 |
} |
| 594 |
|
| 595 |
g_free (where); |
| 596 |
} |
| 597 |
|
| 598 |
static void |
| 599 |
handle_shell (CockpitHandlerData *data, |
| 600 |
CockpitWebService *service, |
| 601 |
const gchar *path, |
| 602 |
GHashTable *headers, |
| 603 |
CockpitWebResponse *response) |
| 604 |
{ |
| 605 |
gboolean valid; |
| 606 |
const gchar *shell_path; |
| 607 |
|
| 608 |
/* Check if a valid path for a shell to be served at */ |
| 609 |
valid = g_str_equal (path, "/") || |
| 610 |
g_str_has_prefix (path, "/@") || |
| 611 |
g_str_has_prefix (path, "/=") || |
| 612 |
strspn (path + 1, COCKPIT_RESOURCE_PACKAGE_VALID) == strcspn (path + 1, "/"); |
| 613 |
|
| 614 |
if (g_str_has_prefix (path, "/=/") || |
| 615 |
g_str_has_prefix (path, "/@/") || |
| 616 |
g_str_has_prefix (path, "//")) |
| 617 |
{ |
| 618 |
valid = FALSE; |
| 619 |
} |
| 620 |
|
| 621 |
if (!valid) |
| 622 |
{ |
| 623 |
cockpit_web_response_error (response, 404, NULL, NULL); |
| 624 |
} |
| 625 |
else if (service) |
| 626 |
{ |
| 627 |
shell_path = cockpit_conf_string ("WebService", "Shell"); |
| 628 |
cockpit_channel_response_serve (service, headers, response, NULL, |
| 629 |
shell_path ? shell_path : cockpit_ws_shell_component); |
| 630 |
cockpit_web_response_set_cache_type (response, COCKPIT_WEB_RESPONSE_NO_CACHE); |
| 631 |
} |
| 632 |
else |
| 633 |
{ |
| 634 |
send_login_html (response, data, path, headers); |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
gboolean |
| 639 |
cockpit_handler_default (CockpitWebServer *server, |
| 640 |
CockpitWebRequest *request, |
| 641 |
const gchar *path, |
| 642 |
GHashTable *headers, |
| 643 |
CockpitWebResponse *response, |
| 644 |
CockpitHandlerData *data) |
| 645 |
{ |
| 646 |
CockpitWebService *service; |
| 647 |
const gchar *remainder = NULL; |
| 648 |
gboolean resource; |
| 649 |
|
| 650 |
path = cockpit_web_response_get_path (response); |
| 651 |
g_return_val_if_fail (path != NULL, FALSE); |
| 652 |
|
| 653 |
/* robots.txt is unauthorized and works on any directory */ |
| 654 |
if (g_str_has_suffix (path, "/robots.txt")) |
| 655 |
{ |
| 656 |
g_autoptr(GHashTable) out_headers = cockpit_web_server_new_table (); |
| 657 |
g_hash_table_insert (out_headers, g_strdup ("Content-Type"), g_strdup ("text/plain")); |
| 658 |
const char *body ="User-agent: *\nDisallow: /\n"; |
| 659 |
g_autoptr(GBytes) content = g_bytes_new_static (body, strlen (body)); |
| 660 |
cockpit_web_response_content (response, out_headers, content, NULL); |
| 661 |
return TRUE; |
| 662 |
} |
| 663 |
|
| 664 |
resource = g_str_has_prefix (path, "/cockpit/") || |
| 665 |
g_str_has_prefix (path, "/cockpit+") || |
| 666 |
g_str_equal (path, "/cockpit"); |
| 667 |
|
| 668 |
// Check for auth |
| 669 |
service = cockpit_auth_check_cookie (data->auth, request); |
| 670 |
|
| 671 |
/* Stuff in /cockpit or /cockpit+xxx */ |
| 672 |
if (resource) |
| 673 |
{ |
| 674 |
g_assert (cockpit_web_response_skip_path (response)); |
| 675 |
remainder = cockpit_web_response_get_path (response); |
| 676 |
|
| 677 |
if (!remainder) |
| 678 |
{ |
| 679 |
cockpit_web_response_error (response, 404, NULL, NULL); |
| 680 |
return TRUE; |
| 681 |
} |
| 682 |
else if (g_str_has_prefix (remainder, "/static/")) |
| 683 |
{ |
| 684 |
cockpit_branding_serve (service, response, path, remainder + 8, |
| 685 |
data->os_release, data->branding_roots); |
| 686 |
return TRUE; |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
if (resource) |
| 691 |
{ |
| 692 |
if (g_str_equal (remainder, "/login")) |
| 693 |
{ |
| 694 |
handle_login (data, service, request, response); |
| 695 |
} |
| 696 |
else |
| 697 |
{ |
| 698 |
handle_resource (data, service, path, headers, response); |
| 699 |
} |
| 700 |
} |
| 701 |
else |
| 702 |
{ |
| 703 |
handle_shell (data, service, path, headers, response); |
| 704 |
} |
| 705 |
|
| 706 |
return TRUE; |
| 707 |
} |
| 708 |
|
| 709 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 710 |
|
| 711 |
gboolean |
| 712 |
cockpit_handler_root (CockpitWebServer *server, |
| 713 |
CockpitWebRequest *request, |
| 714 |
const gchar *path, |
| 715 |
GHashTable *headers, |
| 716 |
CockpitWebResponse *response, |
| 717 |
CockpitHandlerData *ws) |
| 718 |
{ |
| 719 |
/* Don't cache forever */ |
| 720 |
cockpit_web_response_file (response, path, ws->branding_roots); |
| 721 |
return TRUE; |
| 722 |
} |
| 723 |
|
| 724 |
gboolean |
| 725 |
cockpit_handler_ping (CockpitWebServer *server, |
| 726 |
CockpitWebRequest *request, |
| 727 |
const gchar *path, |
| 728 |
GHashTable *headers, |
| 729 |
CockpitWebResponse *response, |
| 730 |
CockpitHandlerData *ws) |
| 731 |
{ |
| 732 |
GHashTable *out_headers; |
| 733 |
const gchar *body; |
| 734 |
GBytes *content; |
| 735 |
|
| 736 |
out_headers = cockpit_web_server_new_table (); |
| 737 |
|
| 738 |
/* |
| 739 |
* The /ping request has unrestricted CORS enabled on it. This allows javascript |
| 740 |
* in the browser on embedding websites to check if Cockpit is available. These |
| 741 |
* websites could do this in another way (such as loading an image from Cockpit) |
| 742 |
* but this does it in the correct manner. |
| 743 |
* |
| 744 |
* See: http://www.w3.org/TR/cors/ |
| 745 |
*/ |
| 746 |
g_hash_table_insert (out_headers, g_strdup ("Access-Control-Allow-Origin"), g_strdup ("*")); |
| 747 |
|
| 748 |
g_hash_table_insert (out_headers, g_strdup ("Content-Type"), g_strdup ("application/json")); |
| 749 |
body ="{ \"service\": \"cockpit\" }"; |
| 750 |
content = g_bytes_new_static (body, strlen (body)); |
| 751 |
|
| 752 |
cockpit_web_response_content (response, out_headers, content, NULL); |
| 753 |
|
| 754 |
g_bytes_unref (content); |
| 755 |
g_hash_table_unref (out_headers); |
| 756 |
|
| 757 |
return TRUE; |
| 758 |
} |
| 759 |
|
| 760 |
gboolean |
| 761 |
cockpit_handler_ca_cert (CockpitWebServer *server, |
| 762 |
CockpitWebRequest *request, |
| 763 |
const gchar *path, |
| 764 |
GHashTable *headers, |
| 765 |
CockpitWebResponse *response, |
| 766 |
CockpitHandlerData *ws) |
| 767 |
{ |
| 768 |
g_autofree gchar *ca_path = NULL; |
| 769 |
|
| 770 |
ca_path = locate_selfsign_ca (); |
| 771 |
if (ca_path == NULL) { |
| 772 |
cockpit_web_response_error (response, 404, NULL, "CA certificate not found"); |
| 773 |
return TRUE; |
| 774 |
} |
| 775 |
|
| 776 |
const gchar *root_dir[] = { "/", NULL }; |
| 777 |
cockpit_web_response_file (response, ca_path, root_dir); |
| 778 |
return TRUE; |
| 779 |
} |