| 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 "cockpitauth.h" |
| 9 |
|
| 10 |
#include "cockpitws.h" |
| 11 |
|
| 12 |
#include "websocket.h" |
| 13 |
|
| 14 |
#include "common/cockpitauthorize.h" |
| 15 |
#include "common/cockpitconf.h" |
| 16 |
#include "cockpiterror.h" |
| 17 |
#include "common/cockpithex.h" |
| 18 |
#include "cockpitjson.h" |
| 19 |
#include "common/cockpitmemory.h" |
| 20 |
#include "cockpitpipe.h" |
| 21 |
#include "cockpitpipetransport.h" |
| 22 |
#include "cockpitsystem.h" |
| 23 |
#include "cockpitwebserver.h" |
| 24 |
|
| 25 |
#include <sys/socket.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <string.h> |
| 29 |
#include <errno.h> |
| 30 |
|
| 31 |
#include <gio/gunixsocketaddress.h> |
| 32 |
|
| 33 |
#define ACTION_SSH "remote-login-ssh" |
| 34 |
#define ACTION_NONE "none" |
| 35 |
#define LOCAL_SESSION "local-session" |
| 36 |
|
| 37 |
/* we only support beibooting machines with a known/vetted OS, as it's impossible to guarantee |
| 38 |
* forward compatibility for all pages */ |
| 39 |
const gchar *cockpit_ws_ssh_program = "/usr/bin/env python3 -m cockpit.beiboot --remote-bridge=supported --"; |
| 40 |
|
| 41 |
/* Some tunables that can be set from tests */ |
| 42 |
const gchar *cockpit_ws_session_program = NULL; |
| 43 |
|
| 44 |
#define WS_SESSION_SOCKET "/run/cockpit/session" |
| 45 |
|
| 46 |
/* Timeout of authenticated session when no connections */ |
| 47 |
guint cockpit_ws_service_idle = 15; |
| 48 |
|
| 49 |
/* The amount of time a spawned process has to complete authentication */ |
| 50 |
guint cockpit_ws_auth_process_timeout = 30; |
| 51 |
guint cockpit_ws_auth_response_timeout = 60; |
| 52 |
|
| 53 |
/* Maximum number of pending authentication requests */ |
| 54 |
const gchar *cockpit_ws_max_startups = NULL; |
| 55 |
|
| 56 |
static guint max_startups = 10; |
| 57 |
|
| 58 |
static guint sig__idling = 0; |
| 59 |
|
| 60 |
/* Tristate tracking whether gssapi works properly */ |
| 61 |
static gint gssapi_available = -1; |
| 62 |
|
| 63 |
static guint |
| 64 |
get_process_idle (void) |
| 65 |
{ |
| 66 |
static guint seconds = 0; |
| 67 |
if (seconds == 0) /* lazy initialization */ |
| 68 |
{ |
| 69 |
const char *val = g_getenv ("COCKPIT_WS_PROCESS_IDLE"); |
| 70 |
|
| 71 |
seconds = 90; /* default value */ |
| 72 |
if (val) |
| 73 |
{ |
| 74 |
char *endptr; |
| 75 |
gint64 x = g_ascii_strtoll (val, &endptr, 10); |
| 76 |
if (*endptr == '\0' && x > 0 && x <= G_MAXUINT) |
| 77 |
seconds = (guint) x; |
| 78 |
else |
| 79 |
g_warning ("Invalid value for COCKPIT_WS_PROCESS_IDLE, ignoring: %s", val); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return seconds; |
| 84 |
} |
| 85 |
|
| 86 |
G_DEFINE_TYPE (CockpitAuth, cockpit_auth, G_TYPE_OBJECT) |
| 87 |
|
| 88 |
typedef struct { |
| 89 |
gint refs; |
| 90 |
gchar *name; |
| 91 |
|
| 92 |
gchar *cookie; |
| 93 |
CockpitAuth *auth; |
| 94 |
|
| 95 |
CockpitWebService *service; |
| 96 |
gboolean initialized; |
| 97 |
guint timeout_tag; |
| 98 |
gulong idling_sig; |
| 99 |
gulong destroy_sig; |
| 100 |
|
| 101 |
/* Used during authentication */ |
| 102 |
CockpitTransport *transport; |
| 103 |
gulong control_sig; |
| 104 |
gulong close_sig; |
| 105 |
|
| 106 |
guint client_timeout; |
| 107 |
guint authorize_timeout; |
| 108 |
|
| 109 |
/* An open /login request from client */ |
| 110 |
GTask *login_task; |
| 111 |
|
| 112 |
/* An authorization header from client */ |
| 113 |
gchar *authorization; |
| 114 |
|
| 115 |
/* An authorize challenge from session */ |
| 116 |
JsonObject *authorize; |
| 117 |
|
| 118 |
/* The failing "init" message from the session */ |
| 119 |
JsonObject *init_failure; |
| 120 |
|
| 121 |
/* The conversation in progress */ |
| 122 |
gchar *conversation; |
| 123 |
} CockpitSession; |
| 124 |
|
| 125 |
static void |
| 126 |
cockpit_session_reset (gpointer data) |
| 127 |
{ |
| 128 |
CockpitSession *session = data; |
| 129 |
char *conversation; |
| 130 |
CockpitAuth *self; |
| 131 |
char *cookie; |
| 132 |
|
| 133 |
if (session->login_task) |
| 134 |
{ |
| 135 |
g_autoptr(GTask) task = g_steal_pointer (&session->login_task); |
| 136 |
g_task_return_boolean (task, TRUE); |
| 137 |
} |
| 138 |
|
| 139 |
if (session->authorization) |
| 140 |
{ |
| 141 |
cockpit_memory_clear (session->authorization, -1); |
| 142 |
g_free (session->authorization); |
| 143 |
session->authorization = NULL; |
| 144 |
} |
| 145 |
|
| 146 |
conversation = session->conversation; |
| 147 |
session->conversation = NULL; |
| 148 |
cookie = session->cookie; |
| 149 |
session->cookie = NULL; |
| 150 |
self = session->auth; |
| 151 |
|
| 152 |
g_clear_pointer (&session->init_failure, json_object_unref); |
| 153 |
|
| 154 |
/* No accessing session after this point */ |
| 155 |
|
| 156 |
if (cookie) |
| 157 |
{ |
| 158 |
g_hash_table_remove (self->sessions, cookie); |
| 159 |
g_free (cookie); |
| 160 |
} |
| 161 |
|
| 162 |
if (conversation) |
| 163 |
{ |
| 164 |
g_hash_table_remove (self->conversations, conversation); |
| 165 |
g_free (conversation); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
static CockpitSession * |
| 170 |
cockpit_session_ref (CockpitSession *session) |
| 171 |
{ |
| 172 |
session->refs++; |
| 173 |
return session; |
| 174 |
} |
| 175 |
|
| 176 |
static void |
| 177 |
on_web_service_gone (gpointer data, |
| 178 |
GObject *where_the_object_was) |
| 179 |
{ |
| 180 |
CockpitSession *session = data; |
| 181 |
session->service = NULL; |
| 182 |
cockpit_session_reset (session); |
| 183 |
} |
| 184 |
|
| 185 |
static void |
| 186 |
cockpit_session_unref (gpointer data) |
| 187 |
{ |
| 188 |
CockpitSession *session = data; |
| 189 |
CockpitCreds *creds; |
| 190 |
GObject *object; |
| 191 |
|
| 192 |
session->refs--; |
| 193 |
if (session->refs > 0) |
| 194 |
return; |
| 195 |
|
| 196 |
cockpit_session_reset (data); |
| 197 |
|
| 198 |
g_free (session->name); |
| 199 |
g_free (session->cookie); |
| 200 |
|
| 201 |
if (session->authorize) |
| 202 |
json_object_unref (session->authorize); |
| 203 |
|
| 204 |
if (session->transport) |
| 205 |
{ |
| 206 |
if (session->control_sig) |
| 207 |
g_signal_handler_disconnect (session->transport, session->control_sig); |
| 208 |
if (session->close_sig) |
| 209 |
g_signal_handler_disconnect (session->transport, session->close_sig); |
| 210 |
g_object_unref (session->transport); |
| 211 |
} |
| 212 |
|
| 213 |
if (session->service) |
| 214 |
{ |
| 215 |
creds = cockpit_web_service_get_creds (session->service); |
| 216 |
object = G_OBJECT (session->service); |
| 217 |
session->service = NULL; |
| 218 |
if (creds) |
| 219 |
cockpit_creds_poison (creds); |
| 220 |
if (session->idling_sig) |
| 221 |
g_signal_handler_disconnect (object, session->idling_sig); |
| 222 |
if (session->destroy_sig) |
| 223 |
g_signal_handler_disconnect (object, session->destroy_sig); |
| 224 |
g_object_weak_unref (object, on_web_service_gone, session); |
| 225 |
g_object_run_dispose (object); |
| 226 |
g_object_unref (object); |
| 227 |
} |
| 228 |
|
| 229 |
if (session->timeout_tag) |
| 230 |
g_source_remove (session->timeout_tag); |
| 231 |
|
| 232 |
g_free (session); |
| 233 |
} |
| 234 |
|
| 235 |
static void |
| 236 |
byte_array_clear_and_free (gpointer data) |
| 237 |
{ |
| 238 |
GByteArray *buffer = data; |
| 239 |
cockpit_memory_clear (buffer->data, buffer->len); |
| 240 |
g_byte_array_free (buffer, TRUE); |
| 241 |
} |
| 242 |
|
| 243 |
static void |
| 244 |
cockpit_auth_finalize (GObject *object) |
| 245 |
{ |
| 246 |
CockpitAuth *self = COCKPIT_AUTH (object); |
| 247 |
if (self->timeout_tag) |
| 248 |
g_source_remove (self->timeout_tag); |
| 249 |
g_bytes_unref (self->key); |
| 250 |
g_hash_table_remove_all (self->sessions); |
| 251 |
g_hash_table_remove_all (self->conversations); |
| 252 |
g_hash_table_destroy (self->sessions); |
| 253 |
g_hash_table_destroy (self->conversations); |
| 254 |
G_OBJECT_CLASS (cockpit_auth_parent_class)->finalize (object); |
| 255 |
} |
| 256 |
|
| 257 |
static gboolean |
| 258 |
on_process_timeout (gpointer data) |
| 259 |
{ |
| 260 |
CockpitAuth *self = COCKPIT_AUTH (data); |
| 261 |
|
| 262 |
self->timeout_tag = 0; |
| 263 |
if (g_hash_table_size (self->sessions) == 0) |
| 264 |
{ |
| 265 |
g_debug ("auth is idle"); |
| 266 |
g_signal_emit (self, sig__idling, 0); |
| 267 |
} |
| 268 |
|
| 269 |
return FALSE; |
| 270 |
} |
| 271 |
|
| 272 |
static void |
| 273 |
cockpit_auth_init (CockpitAuth *self) |
| 274 |
{ |
| 275 |
static const gsize key_len = 128; |
| 276 |
gpointer key; |
| 277 |
|
| 278 |
key = cockpit_authorize_nonce (key_len); |
| 279 |
if (!key) |
| 280 |
g_error ("couldn't read random key, startup aborted"); |
| 281 |
|
| 282 |
self->key = g_bytes_new_take (key, key_len); |
| 283 |
|
| 284 |
self->sessions = g_hash_table_new_full (g_str_hash, g_str_equal, |
| 285 |
NULL, cockpit_session_unref); |
| 286 |
|
| 287 |
self->conversations = g_hash_table_new_full (g_str_hash, g_str_equal, |
| 288 |
NULL, cockpit_session_unref); |
| 289 |
|
| 290 |
self->timeout_tag = g_timeout_add_seconds (get_process_idle (), |
| 291 |
on_process_timeout, self); |
| 292 |
|
| 293 |
self->startups = 0; |
| 294 |
self->max_startups = max_startups; |
| 295 |
self->max_startups_begin = max_startups; |
| 296 |
self->max_startups_rate = 100; |
| 297 |
} |
| 298 |
|
| 299 |
static gchar * |
| 300 |
cockpit_auth_nonce (CockpitAuth *self) |
| 301 |
{ |
| 302 |
const guchar *key; |
| 303 |
gsize len; |
| 304 |
guint64 seed; |
| 305 |
|
| 306 |
seed = self->nonce_seed++; |
| 307 |
key = g_bytes_get_data (self->key, &len); |
| 308 |
return g_compute_hmac_for_data (G_CHECKSUM_SHA256, key, len, |
| 309 |
(guchar *)&seed, sizeof (seed)); |
| 310 |
} |
| 311 |
|
| 312 |
static gchar * |
| 313 |
cockpit_auth_steal_authorization (CockpitWebRequest *request, |
| 314 |
gchar **ret_type, |
| 315 |
gchar **ret_conversation) |
| 316 |
{ |
| 317 |
char *type = NULL; |
| 318 |
gchar *ret = NULL; |
| 319 |
gchar *line; |
| 320 |
gpointer key; |
| 321 |
|
| 322 |
g_assert (request != NULL); |
| 323 |
g_assert (ret_conversation != NULL); |
| 324 |
g_assert (ret_type != NULL); |
| 325 |
|
| 326 |
/* Avoid copying as it can contain passwords */ |
| 327 |
GHashTable *headers = cockpit_web_request_get_headers (request); |
| 328 |
g_assert (headers != NULL); |
| 329 |
if (g_hash_table_lookup_extended (headers, "Authorization", &key, (gpointer *)&line)) |
| 330 |
{ |
| 331 |
g_hash_table_steal (headers, "Authorization"); |
| 332 |
g_free (key); |
| 333 |
|
| 334 |
/* This is being parsed heavily, enforce ASCII */ |
| 335 |
if (!g_str_is_ascii (line)) |
| 336 |
{ |
| 337 |
g_message ("received invalid Authorize header, must be ASCII"); |
| 338 |
goto out; |
| 339 |
} |
| 340 |
} |
| 341 |
else |
| 342 |
{ |
| 343 |
/* |
| 344 |
* If we don't yet know that Negotiate authentication is possible |
| 345 |
* or not, then we ask our session to try to do Negotiate auth |
| 346 |
* but without any input data. |
| 347 |
*/ |
| 348 |
if (gssapi_available != 0) |
| 349 |
line = g_strdup ("Negotiate"); |
| 350 |
else |
| 351 |
return NULL; |
| 352 |
} |
| 353 |
|
| 354 |
/* Dig out the authorization type */ |
| 355 |
if (!cockpit_authorize_type (line, &type)) |
| 356 |
goto out; |
| 357 |
|
| 358 |
/* It's never valid for a "tls-cert" to come via Authorization: */ |
| 359 |
if (g_str_equal (type, "tls-cert")) |
| 360 |
{ |
| 361 |
g_message ("received invalid 'Authorization: tls-cert' header"); |
| 362 |
goto out; |
| 363 |
} |
| 364 |
|
| 365 |
GIOStream *connection = cockpit_web_request_get_io_stream (request); |
| 366 |
|
| 367 |
/* If this is a conversation, get that part out too */ |
| 368 |
if (g_str_equal (type, "x-conversation")) |
| 369 |
{ |
| 370 |
if (!cockpit_authorize_subject (line, ret_conversation)) |
| 371 |
goto out; |
| 372 |
} |
| 373 |
|
| 374 |
/* |
| 375 |
* So for negotiate authentication, conversation happens on a |
| 376 |
* single connection. Yes that's right, GSSAPI, NTLM, and all |
| 377 |
* those nice mechanisms are keep-alive based, not HTTP request based. |
| 378 |
*/ |
| 379 |
else if (g_str_equal (type, "negotiate")) |
| 380 |
{ |
| 381 |
/* Resume an already running conversation? */ |
| 382 |
if (ret_conversation && connection) |
| 383 |
*ret_conversation = g_strdup (g_object_get_data (G_OBJECT (connection), type)); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
if (ret_type) |
| 388 |
{ |
| 389 |
*ret_type = type; |
| 390 |
type = NULL; |
| 391 |
} |
| 392 |
|
| 393 |
ret = line; |
| 394 |
line = NULL; |
| 395 |
|
| 396 |
out: |
| 397 |
g_free (line); |
| 398 |
g_free (type); |
| 399 |
return ret; |
| 400 |
} |
| 401 |
|
| 402 |
static guint |
| 403 |
timeout_option (const gchar *name, |
| 404 |
const gchar *type, |
| 405 |
guint default_value) |
| 406 |
{ |
| 407 |
return cockpit_conf_uint (type, name, default_value, |
| 408 |
MAX_AUTH_TIMEOUT, MIN_AUTH_TIMEOUT); |
| 409 |
} |
| 410 |
|
| 411 |
static const gchar * |
| 412 |
application_parse_host (const gchar *application) |
| 413 |
{ |
| 414 |
const gchar *prefix = "cockpit+="; |
| 415 |
gint len = strlen (prefix); |
| 416 |
|
| 417 |
g_return_val_if_fail (application != NULL, NULL); |
| 418 |
|
| 419 |
if (g_str_has_prefix (application, prefix) && application[len] != '\0') |
| 420 |
return application + len; |
| 421 |
else |
| 422 |
return NULL; |
| 423 |
} |
| 424 |
|
| 425 |
static gchar * |
| 426 |
application_cookie_name (const gchar *application) |
| 427 |
{ |
| 428 |
const gchar *host = application_parse_host (application); |
| 429 |
gchar *cookie_name = NULL; |
| 430 |
|
| 431 |
if (host) |
| 432 |
cookie_name = g_strdup_printf ("machine-cockpit+%s", host); |
| 433 |
else |
| 434 |
cookie_name = g_strdup (application); |
| 435 |
|
| 436 |
return cookie_name; |
| 437 |
} |
| 438 |
|
| 439 |
/* Struct for adding tty later */ |
| 440 |
typedef struct { |
| 441 |
int io; |
| 442 |
} ChildData; |
| 443 |
|
| 444 |
static void |
| 445 |
session_child_setup (gpointer data) |
| 446 |
{ |
| 447 |
ChildData *child = data; |
| 448 |
|
| 449 |
if (dup2 (child->io, 0) < 0 || dup2 (child->io, 1) < 0) |
| 450 |
{ |
| 451 |
g_printerr ("couldn't set child stdin/stout file descriptors\n"); |
| 452 |
_exit (127); |
| 453 |
} |
| 454 |
|
| 455 |
close (child->io); |
| 456 |
|
| 457 |
close_range (3, ~0U, 0); |
| 458 |
} |
| 459 |
|
| 460 |
static CockpitPipe * |
| 461 |
session_start_process (const gchar **argv, |
| 462 |
const gchar **env, |
| 463 |
gboolean capture_stderr) |
| 464 |
{ |
| 465 |
GError *error = NULL; |
| 466 |
ChildData child; |
| 467 |
gboolean ret; |
| 468 |
GPid pid = 0; |
| 469 |
int fds[2]; |
| 470 |
|
| 471 |
g_return_val_if_fail (argv[0] != NULL, NULL); |
| 472 |
|
| 473 |
g_debug ("spawning %s", argv[0]); |
| 474 |
|
| 475 |
/* The main stdin/stdout for the socket ... both are read/writable */ |
| 476 |
if (socketpair (PF_LOCAL, SOCK_STREAM, 0, fds) < 0) |
| 477 |
{ |
| 478 |
g_warning ("couldn't create loopback socket: %s", g_strerror (errno)); |
| 479 |
return NULL; |
| 480 |
} |
| 481 |
|
| 482 |
int stderr_fd = -1; |
| 483 |
child.io = fds[0]; |
| 484 |
ret = g_spawn_async_with_pipes (NULL, (gchar **)argv, (gchar **)env, |
| 485 |
G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_LEAVE_DESCRIPTORS_OPEN, |
| 486 |
session_child_setup, &child, |
| 487 |
&pid, NULL, NULL, capture_stderr ? &stderr_fd : NULL, &error); |
| 488 |
|
| 489 |
close (fds[0]); |
| 490 |
|
| 491 |
if (!ret) |
| 492 |
{ |
| 493 |
g_message ("couldn't launch cockpit session: %s: %s", argv[0], error->message); |
| 494 |
g_error_free (error); |
| 495 |
close (fds[1]); |
| 496 |
return NULL; |
| 497 |
} |
| 498 |
|
| 499 |
return g_object_new (COCKPIT_TYPE_PIPE, |
| 500 |
"in-fd", fds[1], |
| 501 |
"out-fd", fds[1], |
| 502 |
"err-fd", stderr_fd, |
| 503 |
"pid", pid, |
| 504 |
"name", argv[0], |
| 505 |
NULL); |
| 506 |
} |
| 507 |
|
| 508 |
static void |
| 509 |
send_authorize_reply (CockpitTransport *transport, |
| 510 |
const gchar *cookie, |
| 511 |
const gchar *authorization, |
| 512 |
const gchar *remote_peer) |
| 513 |
{ |
| 514 |
const gchar *fields[] = { |
| 515 |
"command", "authorize", |
| 516 |
"cookie", cookie, |
| 517 |
"response", authorization, |
| 518 |
remote_peer ? "remote-peer" : NULL, remote_peer, |
| 519 |
NULL |
| 520 |
}; |
| 521 |
|
| 522 |
GBytes *payload; |
| 523 |
const gchar *delim; |
| 524 |
GByteArray *buffer; |
| 525 |
JsonNode *node; |
| 526 |
gchar *encoded; |
| 527 |
gsize length; |
| 528 |
guint i; |
| 529 |
|
| 530 |
buffer = g_byte_array_new (); |
| 531 |
for (i = 0; fields[i] != NULL; i++) |
| 532 |
{ |
| 533 |
if (i % 2 == 0) |
| 534 |
delim = buffer->len == 0 ? "{" : ","; |
| 535 |
else |
| 536 |
delim = ":"; |
| 537 |
g_byte_array_append (buffer, (guchar *)delim, 1); |
| 538 |
|
| 539 |
node = json_node_init_string (json_node_new (JSON_NODE_VALUE), fields[i]); |
| 540 |
encoded = cockpit_json_write (node, &length); |
| 541 |
g_byte_array_append (buffer, (guchar *)encoded, length); |
| 542 |
cockpit_memory_clear ((guchar *)json_node_get_string (node), -1); |
| 543 |
json_node_free (node); |
| 544 |
g_free (encoded); |
| 545 |
} |
| 546 |
|
| 547 |
g_byte_array_append (buffer, (guchar *)"}", 1); |
| 548 |
payload = g_bytes_new_with_free_func (buffer->data, buffer->len, |
| 549 |
byte_array_clear_and_free, buffer); |
| 550 |
|
| 551 |
cockpit_transport_send (transport, NULL, payload); |
| 552 |
g_bytes_unref (payload); |
| 553 |
} |
| 554 |
|
| 555 |
static gboolean |
| 556 |
reply_authorize_challenge (CockpitSession *session) |
| 557 |
{ |
| 558 |
const gchar *challenge = NULL; |
| 559 |
char *authorize_type = NULL; |
| 560 |
char *authorization_type = NULL; |
| 561 |
const gchar *cookie = NULL; |
| 562 |
const gchar *response = NULL; |
| 563 |
JsonObject *login_data = NULL; |
| 564 |
gboolean ret = FALSE; |
| 565 |
|
| 566 |
if (!session->authorize) |
| 567 |
goto out; |
| 568 |
|
| 569 |
if (!cockpit_json_get_string (session->authorize, "cookie", NULL, &cookie) || |
| 570 |
!cockpit_json_get_string (session->authorize, "challenge", NULL, &challenge) || |
| 571 |
!cockpit_json_get_string (session->authorize, "response", NULL, &response)) |
| 572 |
goto out; |
| 573 |
|
| 574 |
if (response && !cookie) |
| 575 |
{ |
| 576 |
if (session->authorization) |
| 577 |
{ |
| 578 |
cockpit_memory_clear (session->authorization, -1); |
| 579 |
g_free (session->authorization); |
| 580 |
} |
| 581 |
|
| 582 |
session->authorization = g_strdup (response); |
| 583 |
ret = TRUE; |
| 584 |
goto out; |
| 585 |
} |
| 586 |
|
| 587 |
if (!challenge || !cookie) |
| 588 |
goto out; |
| 589 |
|
| 590 |
if (!cockpit_authorize_type (challenge, &authorize_type)) |
| 591 |
goto out; |
| 592 |
|
| 593 |
/* Handle prompting for login data */ |
| 594 |
if (g_str_equal (authorize_type, "x-login-data")) |
| 595 |
{ |
| 596 |
if (cockpit_json_get_object (session->authorize, "login-data", NULL, &login_data) && login_data) |
| 597 |
cockpit_creds_set_login_data (cockpit_web_service_get_creds (session->service), login_data); |
| 598 |
ret = TRUE; |
| 599 |
goto out; |
| 600 |
} |
| 601 |
|
| 602 |
if (!session->authorization) |
| 603 |
goto out; |
| 604 |
|
| 605 |
if (cockpit_authorize_type (session->authorization, &authorization_type) && |
| 606 |
(g_str_equal (authorize_type, "*") || g_str_equal (authorize_type, authorization_type))) |
| 607 |
{ |
| 608 |
const gchar *remote_peer = cockpit_creds_get_rhost (cockpit_web_service_get_creds (session->service)); |
| 609 |
send_authorize_reply (session->transport, cookie, session->authorization, remote_peer); |
| 610 |
cockpit_memory_clear (session->authorization, -1); |
| 611 |
g_free (session->authorization); |
| 612 |
session->authorization = NULL; |
| 613 |
ret = TRUE; |
| 614 |
} |
| 615 |
|
| 616 |
out: |
| 617 |
free (authorize_type); |
| 618 |
free (authorization_type); |
| 619 |
return ret; |
| 620 |
} |
| 621 |
|
| 622 |
static gboolean |
| 623 |
on_authorize_timeout (gpointer data) |
| 624 |
{ |
| 625 |
CockpitSession *session = data; |
| 626 |
CockpitTransport *transport = cockpit_web_service_get_transport (session->service); |
| 627 |
session->timeout_tag = 0; |
| 628 |
g_message ("%s: session timed out during authentication", session->name); |
| 629 |
cockpit_transport_close (transport, "timeout"); |
| 630 |
return FALSE; |
| 631 |
} |
| 632 |
|
| 633 |
static void |
| 634 |
reset_authorize_timeout (CockpitSession *session, |
| 635 |
gboolean waiting_for_client) |
| 636 |
{ |
| 637 |
guint seconds = waiting_for_client ? session->client_timeout : session->authorize_timeout; |
| 638 |
if (session->timeout_tag) |
| 639 |
g_source_remove (session->timeout_tag); |
| 640 |
session->timeout_tag = g_timeout_add_seconds (seconds, on_authorize_timeout, session); |
| 641 |
} |
| 642 |
|
| 643 |
static void |
| 644 |
propagate_problem_to_error (CockpitSession *session, |
| 645 |
JsonObject *options, |
| 646 |
const gchar *problem, |
| 647 |
const gchar *message, |
| 648 |
GError **error) |
| 649 |
{ |
| 650 |
char *type = NULL; |
| 651 |
const char *pw_result = NULL; |
| 652 |
JsonObject *auth_results = NULL; |
| 653 |
|
| 654 |
g_return_if_fail (error != NULL); |
| 655 |
|
| 656 |
if (g_str_equal (problem, "authentication-unavailable") && |
| 657 |
cockpit_authorize_type (session->authorization, &type) && |
| 658 |
g_str_equal (type, "negotiate")) |
| 659 |
{ |
| 660 |
g_debug ("%s: negotiate authentication not available", session->name); |
| 661 |
g_set_error (error, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 662 |
"Negotiate authentication not available"); |
| 663 |
gssapi_available = 0; |
| 664 |
} |
| 665 |
else if (g_str_equal (problem, "authentication-failed") || |
| 666 |
g_str_equal (problem, "authentication-unavailable")) |
| 667 |
{ |
| 668 |
cockpit_json_get_object (options, "auth-method-results", NULL, &auth_results); |
| 669 |
if (auth_results) |
| 670 |
{ |
| 671 |
cockpit_json_get_string (auth_results, "password", NULL, &pw_result); |
| 672 |
if (!pw_result || g_strcmp0 (pw_result, "no-server-support") == 0) |
| 673 |
{ |
| 674 |
g_clear_error (error); |
| 675 |
g_set_error (error, COCKPIT_ERROR, |
| 676 |
COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 677 |
"Authentication failed: authentication-not-supported"); |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
if (*error == NULL) |
| 682 |
{ |
| 683 |
g_debug ("%s: %s %s", session->name, problem, message); |
| 684 |
g_set_error (error, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 685 |
"Authentication failed"); |
| 686 |
} |
| 687 |
} |
| 688 |
else if (g_str_equal (problem, "no-host") || |
| 689 |
g_str_equal (problem, "invalid-hostkey") || |
| 690 |
g_str_equal (problem, "unknown-hostkey") || |
| 691 |
g_str_equal (problem, "unknown-host") || |
| 692 |
g_str_equal (problem, "terminated")) |
| 693 |
{ |
| 694 |
g_debug ("%s: %s", session->name, problem); |
| 695 |
g_set_error (error, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 696 |
"Authentication failed: %s", problem); |
| 697 |
} |
| 698 |
else if (g_str_equal (problem, "access-denied")) |
| 699 |
{ |
| 700 |
g_debug ("permission denied %s", message); |
| 701 |
g_set_error_literal (error, COCKPIT_ERROR, COCKPIT_ERROR_PERMISSION_DENIED, |
| 702 |
message ? message : "Permission denied"); |
| 703 |
} |
| 704 |
else |
| 705 |
{ |
| 706 |
g_debug ("%s: errored %s: %s", session->name, problem, message); |
| 707 |
if (message) |
| 708 |
{ |
| 709 |
g_set_error (error, COCKPIT_ERROR, COCKPIT_ERROR_FAILED, |
| 710 |
"Authentication failed: %s: %s", problem, message); |
| 711 |
} |
| 712 |
else |
| 713 |
{ |
| 714 |
g_set_error (error, COCKPIT_ERROR, COCKPIT_ERROR_FAILED, |
| 715 |
"Authentication failed: %s", problem); |
| 716 |
} |
| 717 |
|
| 718 |
} |
| 719 |
|
| 720 |
free (type); |
| 721 |
} |
| 722 |
|
| 723 |
static gboolean |
| 724 |
on_transport_control (CockpitTransport *transport, |
| 725 |
const char *command, |
| 726 |
const gchar *channel, |
| 727 |
JsonObject *options, |
| 728 |
GBytes *payload, |
| 729 |
gpointer user_data) |
| 730 |
{ |
| 731 |
CockpitSession *session = user_data; |
| 732 |
const gchar *problem = NULL; |
| 733 |
const gchar *session_id = NULL; |
| 734 |
const gchar *message = NULL; |
| 735 |
GError *error = NULL; |
| 736 |
gboolean ret = TRUE; |
| 737 |
|
| 738 |
if (g_str_equal (command, "init")) |
| 739 |
{ |
| 740 |
g_debug ("session initialized"); |
| 741 |
g_signal_handler_disconnect (session->transport, session->control_sig); |
| 742 |
g_signal_handler_disconnect (session->transport, session->close_sig); |
| 743 |
session->control_sig = session->close_sig = 0; |
| 744 |
session->initialized = TRUE; |
| 745 |
|
| 746 |
if (cockpit_json_get_string (options, "problem", NULL, &problem) && problem) |
| 747 |
{ |
| 748 |
if (!cockpit_json_get_string (options, "message", NULL, &message)) |
| 749 |
message = NULL; |
| 750 |
propagate_problem_to_error (session, options, problem, message, &error); |
| 751 |
g_clear_pointer (&session->init_failure, json_object_unref); |
| 752 |
session->init_failure = json_object_ref (options); |
| 753 |
if (session->login_task == NULL) |
| 754 |
{ |
| 755 |
g_message ("ignoring failure from session process: %s", error->message); |
| 756 |
g_error_free (error); |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
if (cockpit_json_get_string (options, "session-id", NULL, &session_id) && session_id) |
| 761 |
cockpit_web_service_set_id (session->service, session_id); |
| 762 |
|
| 763 |
ret = FALSE; /* Let this message be handled elsewhere */ |
| 764 |
} |
| 765 |
else if (g_str_equal (command, "authorize")) |
| 766 |
{ |
| 767 |
const gchar *challenge; |
| 768 |
|
| 769 |
/* handle x-host-key challenge */ |
| 770 |
if (cockpit_json_get_string (options, "challenge", NULL, &challenge) && g_strcmp0 (challenge, "x-host-key") == 0) |
| 771 |
{ |
| 772 |
const gchar *cookie; |
| 773 |
g_return_val_if_fail (cockpit_json_get_string (options, "cookie", NULL, &cookie), FALSE); |
| 774 |
|
| 775 |
/* return a negative answer; we handle unknown hosts interactively, or want to fail on them */ |
| 776 |
g_debug ("received x-host-key authorize challenge"); |
| 777 |
send_authorize_reply (session->transport, cookie, "", NULL /* does not need remote_peer here */); |
| 778 |
return TRUE; |
| 779 |
} |
| 780 |
|
| 781 |
/* handle login ("*") challenge */ |
| 782 |
g_debug ("received authorize challenge"); |
| 783 |
if (session->authorize) |
| 784 |
json_object_unref (session->authorize); |
| 785 |
session->authorize = json_object_ref (options); |
| 786 |
|
| 787 |
if (reply_authorize_challenge (session)) |
| 788 |
return TRUE; |
| 789 |
} |
| 790 |
else |
| 791 |
{ |
| 792 |
g_message ("unexpected \"%s\" control message from session before \"init\"", command); |
| 793 |
cockpit_transport_close (transport, "protocol-error"); |
| 794 |
} |
| 795 |
|
| 796 |
if (session->login_task) |
| 797 |
{ |
| 798 |
g_autoptr(GTask) task = g_steal_pointer (&session->login_task); |
| 799 |
if (error) |
| 800 |
g_task_return_error (task, error); |
| 801 |
else |
| 802 |
g_task_return_boolean (task, TRUE); |
| 803 |
} |
| 804 |
|
| 805 |
return ret; |
| 806 |
} |
| 807 |
|
| 808 |
static void |
| 809 |
on_transport_closed (CockpitTransport *transport, |
| 810 |
const gchar *problem, |
| 811 |
gpointer user_data) |
| 812 |
{ |
| 813 |
CockpitSession *session = user_data; |
| 814 |
CockpitPipe *pipe; |
| 815 |
GError *error = NULL; |
| 816 |
gint status = 0; |
| 817 |
|
| 818 |
if (g_strcmp0 (problem, "timeout") == 0) |
| 819 |
{ |
| 820 |
g_message ("%s: authentication timed out", session->name); |
| 821 |
g_set_error (&error, COCKPIT_ERROR, COCKPIT_ERROR_FAILED, |
| 822 |
"Authentication failed: Timeout"); |
| 823 |
} |
| 824 |
else if (!session->initialized) |
| 825 |
{ |
| 826 |
pipe = cockpit_pipe_transport_get_pipe (COCKPIT_PIPE_TRANSPORT (transport)); |
| 827 |
g_autofree gchar *captured_error = cockpit_pipe_take_stderr_as_utf8 (pipe); |
| 828 |
|
| 829 |
if (cockpit_pipe_get_pid (pipe, NULL)) |
| 830 |
status = cockpit_pipe_exit_status (pipe); |
| 831 |
|
| 832 |
g_debug ("%s: authentication process exited: %d; problem %s; have authorize challenge? %i", |
| 833 |
session->name, status, problem, session->authorize != NULL); |
| 834 |
|
| 835 |
if (captured_error) |
| 836 |
{ |
| 837 |
g_set_error (&error, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 838 |
"captured-stderr:%s", captured_error); |
| 839 |
} |
| 840 |
/* we get "access-denied" both if cockpit-session cannot execute cockpit-bridge (common case) |
| 841 |
* and if cockpit-session itself is not executable (corner case, messed up install) */ |
| 842 |
if (!session->authorize) |
| 843 |
{ |
| 844 |
g_set_error (&error, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 845 |
"Authentication not available"); |
| 846 |
} |
| 847 |
else if (g_strcmp0 (problem, "access-denied") == 0) |
| 848 |
{ |
| 849 |
g_set_error (&error, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 850 |
"Authentication failed"); |
| 851 |
} |
| 852 |
else |
| 853 |
{ |
| 854 |
g_set_error (&error, COCKPIT_ERROR, COCKPIT_ERROR_FAILED, |
| 855 |
g_strcmp0 (problem, "no-cockpit") == 0 |
| 856 |
? "The cockpit package is not installed" |
| 857 |
: "Internal error in login process"); |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
if (!error) |
| 862 |
{ |
| 863 |
g_message ("%s: authentication process failed", session->name); |
| 864 |
g_set_error (&error, COCKPIT_ERROR, COCKPIT_ERROR_FAILED, |
| 865 |
"Authentication internal error"); |
| 866 |
} |
| 867 |
|
| 868 |
if (session->login_task) |
| 869 |
{ |
| 870 |
g_autoptr(GTask) task = g_steal_pointer (&session->login_task); |
| 871 |
g_task_return_error (task, error); |
| 872 |
} |
| 873 |
else |
| 874 |
{ |
| 875 |
g_message ("ignoring failure from session process: %s", error->message); |
| 876 |
cockpit_session_reset (session); |
| 877 |
g_error_free (error); |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
static CockpitCreds * |
| 882 |
build_session_credentials (CockpitAuth *self, |
| 883 |
CockpitWebRequest *request, |
| 884 |
const char *application, |
| 885 |
const char *host, |
| 886 |
const char *type, |
| 887 |
const char *authorization) |
| 888 |
{ |
| 889 |
CockpitCreds *creds; |
| 890 |
const gchar *superuser = NULL; |
| 891 |
|
| 892 |
char *user = NULL; |
| 893 |
char *raw = NULL; |
| 894 |
|
| 895 |
GBytes *password = NULL; |
| 896 |
gchar *csrf_token = NULL; |
| 897 |
|
| 898 |
superuser = cockpit_web_request_lookup_header (request, "X-Superuser"); |
| 899 |
if (!superuser) |
| 900 |
superuser = "none"; |
| 901 |
|
| 902 |
/* Prepare various credentials */ |
| 903 |
if (g_strcmp0 (type, "basic") == 0) |
| 904 |
{ |
| 905 |
raw = cockpit_authorize_parse_basic (authorization, &user); |
| 906 |
|
| 907 |
/* If we are not root, we might want to keep the password around |
| 908 |
so that the session can immediately gain admin privileges by |
| 909 |
reusing the password. |
| 910 |
|
| 911 |
We keep it when X-Superuser is not none, but also when |
| 912 |
directly connecting to a remote machine since that machine |
| 913 |
might run a old version of Cockpit that will never do the |
| 914 |
things necessary to set X-Superuser to anything but "none". |
| 915 |
We still want to give it the password because otherwise |
| 916 |
people can never get admin privs on that machine. |
| 917 |
|
| 918 |
When the remote machine is new enough, we will remove the |
| 919 |
password immediately after the session has been initialized. |
| 920 |
*/ |
| 921 |
|
| 922 |
if (user && strcmp (user, "root") != 0 && (strcmp (superuser, "none") != 0 || host) && raw) |
| 923 |
{ |
| 924 |
password = g_bytes_new_take (raw, strlen (raw)); |
| 925 |
raw = NULL; |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
g_autofree gchar *remote_peer = cockpit_web_request_get_remote_address (request); |
| 930 |
csrf_token = cockpit_auth_nonce (self); |
| 931 |
|
| 932 |
creds = cockpit_creds_new (application, |
| 933 |
COCKPIT_CRED_USER, user, |
| 934 |
COCKPIT_CRED_PASSWORD, password, |
| 935 |
COCKPIT_CRED_RHOST, remote_peer, |
| 936 |
COCKPIT_CRED_CSRF_TOKEN, csrf_token, |
| 937 |
COCKPIT_CRED_SUPERUSER, superuser, |
| 938 |
NULL); |
| 939 |
|
| 940 |
if (raw) |
| 941 |
{ |
| 942 |
cockpit_memory_clear (raw, strlen (raw)); |
| 943 |
free (raw); |
| 944 |
} |
| 945 |
g_free (csrf_token); |
| 946 |
if (password) |
| 947 |
g_bytes_unref (password); |
| 948 |
free (user); |
| 949 |
|
| 950 |
return creds; |
| 951 |
} |
| 952 |
|
| 953 |
static gboolean |
| 954 |
on_session_timeout (gpointer data) |
| 955 |
{ |
| 956 |
CockpitSession *session = data; |
| 957 |
|
| 958 |
session->timeout_tag = 0; |
| 959 |
|
| 960 |
if (!session->service || cockpit_web_service_get_idling (session->service)) |
| 961 |
{ |
| 962 |
g_info ("session timed out"); |
| 963 |
cockpit_session_reset (session); |
| 964 |
} |
| 965 |
|
| 966 |
return FALSE; |
| 967 |
} |
| 968 |
|
| 969 |
static void |
| 970 |
on_web_service_idling (CockpitWebService *service, |
| 971 |
gpointer data) |
| 972 |
{ |
| 973 |
CockpitSession *session = data; |
| 974 |
|
| 975 |
if (session->timeout_tag) |
| 976 |
g_source_remove (session->timeout_tag); |
| 977 |
|
| 978 |
if (!g_strcmp0 (session->cookie, LOCAL_SESSION)) |
| 979 |
{ |
| 980 |
g_debug ("local session is idle, keeping"); |
| 981 |
return; |
| 982 |
} |
| 983 |
|
| 984 |
g_debug ("session is idle"); |
| 985 |
|
| 986 |
/* |
| 987 |
* The minimum amount of time before a request uses this new web service, |
| 988 |
* otherwise it will just go away. |
| 989 |
*/ |
| 990 |
session->timeout_tag = g_timeout_add_seconds (cockpit_ws_service_idle, |
| 991 |
on_session_timeout, |
| 992 |
session); |
| 993 |
|
| 994 |
/* |
| 995 |
* Also reset the timer which checks whether anything is going on in the |
| 996 |
* entire process or not. |
| 997 |
*/ |
| 998 |
if (session->auth->timeout_tag) |
| 999 |
g_source_remove (session->auth->timeout_tag); |
| 1000 |
|
| 1001 |
session->auth->timeout_tag = g_timeout_add_seconds (get_process_idle (), |
| 1002 |
on_process_timeout, session->auth); |
| 1003 |
} |
| 1004 |
|
| 1005 |
static void |
| 1006 |
on_web_service_destroy (CockpitWebService *service, |
| 1007 |
gpointer data) |
| 1008 |
{ |
| 1009 |
on_web_service_idling (service, data); |
| 1010 |
cockpit_session_reset (data); |
| 1011 |
} |
| 1012 |
|
| 1013 |
static CockpitSession * |
| 1014 |
cockpit_session_create (CockpitAuth *self, |
| 1015 |
const gchar *name, |
| 1016 |
CockpitCreds *creds, |
| 1017 |
CockpitTransport *transport) |
| 1018 |
{ |
| 1019 |
CockpitSession *session; |
| 1020 |
|
| 1021 |
session = g_new0 (CockpitSession, 1); |
| 1022 |
session->refs = 1; |
| 1023 |
session->name = g_path_get_basename (name); |
| 1024 |
session->auth = self; |
| 1025 |
|
| 1026 |
session->service = cockpit_web_service_new (creds, transport); |
| 1027 |
|
| 1028 |
session->idling_sig = g_signal_connect (session->service, "idling", |
| 1029 |
G_CALLBACK (on_web_service_idling), session); |
| 1030 |
session->destroy_sig = g_signal_connect (session->service, "destroy", |
| 1031 |
G_CALLBACK (on_web_service_destroy), session); |
| 1032 |
|
| 1033 |
g_object_weak_ref (G_OBJECT (session->service), on_web_service_gone, session); |
| 1034 |
|
| 1035 |
session->transport = g_object_ref (transport); |
| 1036 |
session->control_sig = g_signal_connect (transport, "control", G_CALLBACK (on_transport_control), session); |
| 1037 |
session->close_sig = g_signal_connect (transport, "closed", G_CALLBACK (on_transport_closed), session); |
| 1038 |
|
| 1039 |
return session; |
| 1040 |
} |
| 1041 |
|
| 1042 |
static CockpitSession * |
| 1043 |
cockpit_session_launch (CockpitAuth *self, |
| 1044 |
CockpitWebRequest *request, |
| 1045 |
const gchar *type, |
| 1046 |
const gchar *authorization, |
| 1047 |
const gchar *application, |
| 1048 |
GError **error) |
| 1049 |
{ |
| 1050 |
g_return_val_if_fail (type != NULL, NULL); |
| 1051 |
|
| 1052 |
const gchar *host = application_parse_host (application); |
| 1053 |
const gchar *action = cockpit_conf_string (type, "action"); |
| 1054 |
if (g_strcmp0 (action, ACTION_NONE) == 0) |
| 1055 |
{ |
| 1056 |
g_set_error (error, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 1057 |
"Authentication disabled"); |
| 1058 |
return NULL; |
| 1059 |
} |
| 1060 |
|
| 1061 |
if (host && !cockpit_conf_bool ("WebService", "LoginTo", TRUE)) { |
| 1062 |
g_set_error (error, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 1063 |
"Direct remote login is disabled"); |
| 1064 |
return NULL; |
| 1065 |
} |
| 1066 |
|
| 1067 |
/* These are the credentials we'll carry around for this session */ |
| 1068 |
g_autoptr(CockpitCreds) creds = build_session_credentials (self, request, application, host, type, authorization); |
| 1069 |
|
| 1070 |
const gchar *section; |
| 1071 |
if (host) |
| 1072 |
section = COCKPIT_CONF_SSH_SECTION; |
| 1073 |
else if (self->login_loopback && g_str_equal (type, "basic")) |
| 1074 |
section = COCKPIT_CONF_SSH_SECTION; |
| 1075 |
else if (g_strcmp0 (action, ACTION_SSH) == 0) |
| 1076 |
section = COCKPIT_CONF_SSH_SECTION; |
| 1077 |
else |
| 1078 |
section = type; |
| 1079 |
|
| 1080 |
const gchar *command = cockpit_conf_string (section, "Command"); |
| 1081 |
const gchar *unix_path = cockpit_conf_string (section, "UnixPath"); |
| 1082 |
|
| 1083 |
gboolean capture_stderr = FALSE; |
| 1084 |
if (g_str_equal (section, COCKPIT_CONF_SSH_SECTION)) |
| 1085 |
{ |
| 1086 |
if (!host) |
| 1087 |
host = cockpit_conf_string (COCKPIT_CONF_SSH_SECTION, "host") ?: "127.0.0.1"; |
| 1088 |
|
| 1089 |
/* We capture stderr only for Cockpit Client; we don't want to |
| 1090 |
* send log messages to potential remote attackers. |
| 1091 |
* |
| 1092 |
* Only do that if COCKPIT_DEBUG is off, though: otherwise the |
| 1093 |
* stderr is going to be too long to make sense of in the browser. |
| 1094 |
*/ |
| 1095 |
if (g_getenv("COCKPIT_DEBUG") == NULL) |
| 1096 |
capture_stderr = cockpit_conf_bool ("WebService", "X-For-CockpitClient", FALSE); |
| 1097 |
|
| 1098 |
if (command == NULL && unix_path == NULL) |
| 1099 |
command = cockpit_ws_ssh_program; |
| 1100 |
} |
| 1101 |
else if (g_str_equal (type, "basic") || |
| 1102 |
g_str_equal (type, "negotiate") || |
| 1103 |
g_str_equal (type, "tls-cert")) |
| 1104 |
{ |
| 1105 |
if (command == NULL && unix_path == NULL) |
| 1106 |
{ |
| 1107 |
if (cockpit_ws_session_program) |
| 1108 |
command = cockpit_ws_session_program; |
| 1109 |
else |
| 1110 |
unix_path = WS_SESSION_SOCKET; |
| 1111 |
} |
| 1112 |
} |
| 1113 |
|
| 1114 |
g_autoptr(CockpitPipe) pipe = NULL; |
| 1115 |
if (command != NULL) |
| 1116 |
{ |
| 1117 |
g_auto(GStrv) env = g_get_environ (); |
| 1118 |
if (g_strcmp0 (cockpit_web_request_lookup_header (request, "X-SSH-Connect-Unknown-Hosts"), "yes") == 0) |
| 1119 |
{ |
| 1120 |
env = g_environ_setenv (env, "COCKPIT_SSH_CONNECT_TO_UNKNOWN_HOSTS", |
| 1121 |
"1", |
| 1122 |
TRUE); |
| 1123 |
} |
| 1124 |
|
| 1125 |
/* split the command from the config */ |
| 1126 |
gint argc; |
| 1127 |
g_auto(GStrv) argv = NULL; |
| 1128 |
if (!g_shell_parse_argv (command, &argc, &argv, error)) |
| 1129 |
return NULL; |
| 1130 |
|
| 1131 |
/* append the host */ |
| 1132 |
argv = g_renew (char *, argv, argc + 1 + 1); |
| 1133 |
argv[argc++] = g_strdup (host ?: "localhost"); |
| 1134 |
argv[argc] = NULL; |
| 1135 |
|
| 1136 |
pipe = session_start_process ((const gchar **) argv, (const gchar **)env, capture_stderr); |
| 1137 |
} |
| 1138 |
else if (unix_path != NULL) |
| 1139 |
{ |
| 1140 |
g_autoptr(GSocketAddress) address = g_unix_socket_address_new (unix_path); |
| 1141 |
pipe = cockpit_pipe_connect (unix_path, address); |
| 1142 |
} |
| 1143 |
else |
| 1144 |
{ |
| 1145 |
g_set_error (error, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 1146 |
"Authentication disabled"); |
| 1147 |
return NULL; |
| 1148 |
} |
| 1149 |
|
| 1150 |
if (!pipe) |
| 1151 |
{ |
| 1152 |
g_set_error (error, COCKPIT_ERROR, COCKPIT_ERROR_FAILED, |
| 1153 |
"Authentication failed to start"); |
| 1154 |
return NULL; |
| 1155 |
} |
| 1156 |
|
| 1157 |
g_autoptr(CockpitTransport) transport = cockpit_pipe_transport_new (pipe); |
| 1158 |
CockpitSession *session = cockpit_session_create (self, cockpit_pipe_get_name (pipe), creds, transport); |
| 1159 |
|
| 1160 |
/* How long to wait for the auth process to send some data */ |
| 1161 |
session->authorize_timeout = timeout_option ("timeout", section, cockpit_ws_auth_process_timeout); |
| 1162 |
|
| 1163 |
/* How long to wait for a response from the client to a auth prompt */ |
| 1164 |
session->client_timeout = timeout_option ("response-timeout", section, cockpit_ws_auth_response_timeout); |
| 1165 |
|
| 1166 |
return session; |
| 1167 |
} |
| 1168 |
|
| 1169 |
static gboolean |
| 1170 |
build_authorize_challenge (CockpitAuth *self, |
| 1171 |
JsonObject *authorize, |
| 1172 |
GIOStream *connection, |
| 1173 |
GHashTable *headers, |
| 1174 |
JsonObject **body, |
| 1175 |
gchar **conversation) |
| 1176 |
{ |
| 1177 |
const gchar *challenge = NULL; |
| 1178 |
gchar *type = NULL; |
| 1179 |
GList *l, *names; |
| 1180 |
|
| 1181 |
if (!cockpit_json_get_string (authorize, "challenge", NULL, &challenge) || |
| 1182 |
!cockpit_authorize_type (challenge, &type)) |
| 1183 |
{ |
| 1184 |
g_message ("invalid \"challenge\" field in \"authorize\" message"); |
| 1185 |
return FALSE; |
| 1186 |
} |
| 1187 |
|
| 1188 |
g_hash_table_replace (headers, g_strdup ("WWW-Authenticate"), g_strdup (challenge)); |
| 1189 |
*conversation = NULL; |
| 1190 |
|
| 1191 |
if (g_str_equal (type, "negotiate")) |
| 1192 |
{ |
| 1193 |
gssapi_available = 1; |
| 1194 |
*conversation = cockpit_auth_nonce (self); |
| 1195 |
if (connection) |
| 1196 |
g_object_set_data_full (G_OBJECT (connection), "negotiate", g_strdup (*conversation), g_free); |
| 1197 |
} |
| 1198 |
else if (g_str_equal (type, "x-conversation")) |
| 1199 |
{ |
| 1200 |
cockpit_authorize_subject (challenge, conversation); |
| 1201 |
} |
| 1202 |
|
| 1203 |
g_autoptr(JsonObject) object = json_object_new (); |
| 1204 |
names = json_object_get_members (authorize); |
| 1205 |
for (l = names; l != NULL; l = g_list_next (l)) |
| 1206 |
{ |
| 1207 |
if (!g_str_equal (l->data, "challenge") && !g_str_equal (l->data, "cookie")) |
| 1208 |
json_object_set_member (object, l->data, json_object_dup_member (authorize, l->data)); |
| 1209 |
} |
| 1210 |
if (body) |
| 1211 |
*body = g_steal_pointer (&object); |
| 1212 |
|
| 1213 |
g_list_free (names); |
| 1214 |
g_free (type); |
| 1215 |
return TRUE; |
| 1216 |
} |
| 1217 |
|
| 1218 |
static void |
| 1219 |
authorize_logger (const char *data) |
| 1220 |
{ |
| 1221 |
g_message ("%s", data); |
| 1222 |
} |
| 1223 |
|
| 1224 |
static void |
| 1225 |
cockpit_auth_class_init (CockpitAuthClass *klass) |
| 1226 |
{ |
| 1227 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 1228 |
|
| 1229 |
gobject_class->finalize = cockpit_auth_finalize; |
| 1230 |
|
| 1231 |
sig__idling = g_signal_new ("idling", COCKPIT_TYPE_AUTH, G_SIGNAL_RUN_FIRST, |
| 1232 |
0, NULL, NULL, NULL, G_TYPE_NONE, 0); |
| 1233 |
|
| 1234 |
cockpit_authorize_logger (authorize_logger, 0); |
| 1235 |
} |
| 1236 |
|
| 1237 |
static gchar * |
| 1238 |
base64_decode_string (const char *enc) |
| 1239 |
{ |
| 1240 |
gchar *dec; |
| 1241 |
gsize len; |
| 1242 |
|
| 1243 |
if (enc == NULL) |
| 1244 |
return NULL; |
| 1245 |
|
| 1246 |
dec = (gchar *)g_base64_decode (enc, &len); |
| 1247 |
if (dec) |
| 1248 |
dec[len] = '\0'; |
| 1249 |
|
| 1250 |
return dec; |
| 1251 |
} |
| 1252 |
|
| 1253 |
static CockpitSession * |
| 1254 |
session_for_cookie_value (CockpitAuth *self, |
| 1255 |
const gchar *cookie_value) |
| 1256 |
{ |
| 1257 |
gchar *decoded = NULL; |
| 1258 |
const char *prefix = "v=2;k="; |
| 1259 |
CockpitSession *ret = NULL; |
| 1260 |
|
| 1261 |
g_return_val_if_fail (self != NULL, FALSE); |
| 1262 |
|
| 1263 |
if (cookie_value) |
| 1264 |
{ |
| 1265 |
decoded = base64_decode_string (cookie_value); |
| 1266 |
if (decoded != NULL) |
| 1267 |
{ |
| 1268 |
if (g_str_has_prefix (decoded, prefix)) |
| 1269 |
ret = g_hash_table_lookup (self->sessions, decoded); |
| 1270 |
else |
| 1271 |
g_debug ("invalid or unsupported cookie: %s", decoded); |
| 1272 |
|
| 1273 |
g_free (decoded); |
| 1274 |
} |
| 1275 |
} |
| 1276 |
|
| 1277 |
return ret; |
| 1278 |
} |
| 1279 |
|
| 1280 |
static CockpitSession * |
| 1281 |
session_for_request (CockpitAuth *self, |
| 1282 |
CockpitWebRequest *request) |
| 1283 |
{ |
| 1284 |
gchar *raw = NULL; |
| 1285 |
CockpitSession *ret = NULL; |
| 1286 |
gchar *application; |
| 1287 |
gchar *cookie_name = NULL; |
| 1288 |
|
| 1289 |
g_return_val_if_fail (self != NULL, FALSE); |
| 1290 |
|
| 1291 |
application = cockpit_auth_parse_application (cockpit_web_request_get_path (request), NULL); |
| 1292 |
if (!application) |
| 1293 |
return NULL; |
| 1294 |
|
| 1295 |
cookie_name = application_cookie_name (application); |
| 1296 |
raw = cockpit_web_request_parse_cookie (request, cookie_name); |
| 1297 |
ret = session_for_cookie_value (self, raw); |
| 1298 |
|
| 1299 |
/* We must never find the default session based on a cookie */ |
| 1300 |
g_assert (!ret || !g_str_equal (ret->cookie, LOCAL_SESSION)); |
| 1301 |
g_assert (!ret || !g_str_equal (ret->name, LOCAL_SESSION)); |
| 1302 |
g_free (raw); |
| 1303 |
|
| 1304 |
/* Check for a default session for auto-login */ |
| 1305 |
if (!ret) |
| 1306 |
ret = g_hash_table_lookup (self->sessions, LOCAL_SESSION); |
| 1307 |
|
| 1308 |
g_free (application); |
| 1309 |
g_free (cookie_name); |
| 1310 |
return ret; |
| 1311 |
} |
| 1312 |
|
| 1313 |
CockpitWebService * |
| 1314 |
cockpit_auth_check_cookie (CockpitAuth *self, |
| 1315 |
CockpitWebRequest *request) |
| 1316 |
{ |
| 1317 |
CockpitSession *session; |
| 1318 |
CockpitCreds *creds; |
| 1319 |
|
| 1320 |
session = session_for_request (self, request); |
| 1321 |
if (session) |
| 1322 |
{ |
| 1323 |
creds = cockpit_web_service_get_creds (session->service); |
| 1324 |
g_debug ("received %s credential cookie for session", |
| 1325 |
cockpit_creds_get_application (creds)); |
| 1326 |
return g_object_ref (session->service); |
| 1327 |
} |
| 1328 |
else |
| 1329 |
{ |
| 1330 |
g_debug ("received unknown/invalid credential cookie"); |
| 1331 |
return NULL; |
| 1332 |
} |
| 1333 |
} |
| 1334 |
|
| 1335 |
gboolean |
| 1336 |
cockpit_auth_is_valid_cookie_value (CockpitAuth *self, |
| 1337 |
const gchar *cookie_value) |
| 1338 |
{ |
| 1339 |
return session_for_cookie_value (self, cookie_value) != NULL; |
| 1340 |
} |
| 1341 |
|
| 1342 |
void |
| 1343 |
cockpit_auth_local_async (CockpitAuth *self, |
| 1344 |
const gchar *user, |
| 1345 |
CockpitPipe *pipe, |
| 1346 |
GAsyncReadyCallback callback, |
| 1347 |
gpointer user_data) |
| 1348 |
{ |
| 1349 |
CockpitTransport *transport; |
| 1350 |
CockpitSession *session; |
| 1351 |
CockpitCreds *creds; |
| 1352 |
gchar *csrf_token; |
| 1353 |
|
| 1354 |
g_return_if_fail (COCKPIT_IS_AUTH (self)); |
| 1355 |
g_return_if_fail (COCKPIT_IS_PIPE (pipe)); |
| 1356 |
g_return_if_fail (user != NULL); |
| 1357 |
|
| 1358 |
transport = cockpit_pipe_transport_new (pipe); |
| 1359 |
|
| 1360 |
csrf_token = cockpit_auth_nonce (self); |
| 1361 |
creds = cockpit_creds_new ("cockpit", |
| 1362 |
COCKPIT_CRED_USER, user, |
| 1363 |
COCKPIT_CRED_RHOST, "localhost", |
| 1364 |
COCKPIT_CRED_CSRF_TOKEN, csrf_token, |
| 1365 |
NULL); |
| 1366 |
|
| 1367 |
session = cockpit_session_create (self, cockpit_pipe_get_name (pipe), creds, transport); |
| 1368 |
|
| 1369 |
session->cookie = g_strdup (LOCAL_SESSION); |
| 1370 |
g_hash_table_insert (self->sessions, session->cookie, session); |
| 1371 |
|
| 1372 |
session->login_task = g_task_new (self, NULL, callback, user_data); |
| 1373 |
|
| 1374 |
g_free (csrf_token); |
| 1375 |
g_object_unref (transport); |
| 1376 |
cockpit_creds_unref (creds); |
| 1377 |
} |
| 1378 |
|
| 1379 |
/* |
| 1380 |
* returns TRUE if auth can proceed, FALSE otherwise. |
| 1381 |
* dropping starts at connection max_startups_begin with a probability |
| 1382 |
* of (max_startups_rate/100). the probability increases linearly until |
| 1383 |
* all connections are dropped for startups > max_startups |
| 1384 |
*/ |
| 1385 |
|
| 1386 |
static gboolean |
| 1387 |
can_start_auth (CockpitAuth *self) |
| 1388 |
{ |
| 1389 |
int p, r; |
| 1390 |
|
| 1391 |
/* 0 means unlimited */ |
| 1392 |
if (self->max_startups == 0) |
| 1393 |
return TRUE; |
| 1394 |
|
| 1395 |
/* Under soft limit */ |
| 1396 |
if (self->startups <= self->max_startups_begin) |
| 1397 |
return TRUE; |
| 1398 |
|
| 1399 |
/* Over hard limit */ |
| 1400 |
if (self->startups > self->max_startups) |
| 1401 |
return FALSE; |
| 1402 |
|
| 1403 |
/* If rate is 100, soft limit is hard limit */ |
| 1404 |
if (self->max_startups_rate == 100) |
| 1405 |
return FALSE; |
| 1406 |
|
| 1407 |
p = 100 - self->max_startups_rate; |
| 1408 |
p *= self->startups - self->max_startups_begin; |
| 1409 |
p /= self->max_startups - self->max_startups_begin; |
| 1410 |
p += self->max_startups_rate; |
| 1411 |
r = g_random_int_range (0, 100); |
| 1412 |
|
| 1413 |
g_debug ("calculating if auth can start: (%u:%u:%u): p %d, r %d", |
| 1414 |
self->max_startups_begin, self->max_startups_rate, |
| 1415 |
self->max_startups, p, r); |
| 1416 |
return (r < p) ? FALSE : TRUE; |
| 1417 |
} |
| 1418 |
|
| 1419 |
void |
| 1420 |
cockpit_auth_login_async (CockpitAuth *self, |
| 1421 |
CockpitWebRequest *request, |
| 1422 |
GAsyncReadyCallback callback, |
| 1423 |
gpointer user_data) |
| 1424 |
{ |
| 1425 |
CockpitSession *session; |
| 1426 |
GError *error = NULL; |
| 1427 |
g_autofree gchar *type = NULL; |
| 1428 |
g_autofree gchar *conversation = NULL; |
| 1429 |
g_autofree gchar *authorization = NULL; |
| 1430 |
g_autofree gchar *application = NULL; |
| 1431 |
|
| 1432 |
g_return_if_fail (request != NULL); |
| 1433 |
|
| 1434 |
self->startups++; |
| 1435 |
|
| 1436 |
g_autoptr(GTask) task = g_task_new (self, NULL, callback, user_data); |
| 1437 |
|
| 1438 |
if (!can_start_auth (self)) |
| 1439 |
{ |
| 1440 |
g_message ("Request dropped; too many startup connections: %u", self->startups); |
| 1441 |
g_task_return_new_error (task, COCKPIT_ERROR, COCKPIT_ERROR_FAILED, |
| 1442 |
"Connection closed by host"); |
| 1443 |
goto out; |
| 1444 |
} |
| 1445 |
|
| 1446 |
const gchar *path = cockpit_web_request_get_path (request); |
| 1447 |
application = cockpit_auth_parse_application (path, NULL); |
| 1448 |
|
| 1449 |
/* If the client sends a TLS certificate to cockpit-tls, treat this as a |
| 1450 |
* definitive login type, and don't just silently fall back to other types */ |
| 1451 |
const gchar *client_certificate; |
| 1452 |
if ((client_certificate = cockpit_web_request_get_client_certificate (request))) |
| 1453 |
{ |
| 1454 |
g_debug ("TLS connection has peer certificate, using tls-cert auth type"); |
| 1455 |
type = g_strdup ("tls-cert"); |
| 1456 |
/* This is a client certificate *filename*. On its own, it's |
| 1457 |
* insufficient for logging in: cockpit-session will check that |
| 1458 |
* the file exists on disk and is valid. |
| 1459 |
*/ |
| 1460 |
authorization = g_strdup_printf ("tls-cert %s", client_certificate); |
| 1461 |
} |
| 1462 |
else |
| 1463 |
{ |
| 1464 |
g_debug ("No peer certificate"); |
| 1465 |
authorization = cockpit_auth_steal_authorization (request, &type, &conversation); |
| 1466 |
|
| 1467 |
if (!authorization) |
| 1468 |
{ |
| 1469 |
g_task_return_new_error (task, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 1470 |
"Authentication required"); |
| 1471 |
goto out; |
| 1472 |
} |
| 1473 |
} |
| 1474 |
|
| 1475 |
if (!application) |
| 1476 |
{ |
| 1477 |
g_task_return_new_error (task, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 1478 |
"Application required"); |
| 1479 |
goto out; |
| 1480 |
} |
| 1481 |
|
| 1482 |
if (conversation) |
| 1483 |
{ |
| 1484 |
session = g_hash_table_lookup (self->conversations, conversation); |
| 1485 |
if (!session) |
| 1486 |
{ |
| 1487 |
g_task_return_new_error (task, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 1488 |
"Invalid conversation token"); |
| 1489 |
goto out; |
| 1490 |
} |
| 1491 |
|
| 1492 |
g_task_set_task_data (task, cockpit_session_ref (session), cockpit_session_unref); |
| 1493 |
} |
| 1494 |
else |
| 1495 |
{ |
| 1496 |
session = cockpit_session_launch (self, request, type, authorization, application, &error); |
| 1497 |
if (!session) |
| 1498 |
{ |
| 1499 |
g_task_return_error (task, error); |
| 1500 |
goto out; |
| 1501 |
} |
| 1502 |
g_task_set_task_data (task, session, cockpit_session_unref); |
| 1503 |
} |
| 1504 |
|
| 1505 |
cockpit_session_reset (session); |
| 1506 |
session->login_task = g_steal_pointer (&task); |
| 1507 |
|
| 1508 |
session->authorization = authorization; |
| 1509 |
authorization = NULL; |
| 1510 |
|
| 1511 |
if (conversation && !reply_authorize_challenge (session)) |
| 1512 |
{ |
| 1513 |
g_autoptr(GTask) task = g_steal_pointer (&session->login_task); |
| 1514 |
g_task_return_new_error (task, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 1515 |
"Invalid conversation reply"); |
| 1516 |
goto out; |
| 1517 |
} |
| 1518 |
|
| 1519 |
reset_authorize_timeout (session, FALSE); |
| 1520 |
|
| 1521 |
out: |
| 1522 |
if (authorization) |
| 1523 |
cockpit_memory_clear (authorization, -1); |
| 1524 |
} |
| 1525 |
|
| 1526 |
JsonObject * |
| 1527 |
cockpit_auth_login_finish (CockpitAuth *self, |
| 1528 |
GAsyncResult *result, |
| 1529 |
GIOStream *connection, |
| 1530 |
GHashTable *headers, |
| 1531 |
GError **error) |
| 1532 |
{ |
| 1533 |
g_autoptr(JsonObject) body = NULL; |
| 1534 |
|
| 1535 |
g_return_val_if_fail (g_task_is_valid (result, self), NULL); |
| 1536 |
|
| 1537 |
CockpitSession *session = g_task_get_task_data (G_TASK (result)); |
| 1538 |
|
| 1539 |
if (!g_task_propagate_boolean (G_TASK (result), error)) |
| 1540 |
{ |
| 1541 |
if (session != NULL) |
| 1542 |
body = g_steal_pointer (&session->init_failure); |
| 1543 |
goto out; |
| 1544 |
} |
| 1545 |
|
| 1546 |
g_return_val_if_fail (session != NULL, NULL); |
| 1547 |
g_return_val_if_fail (session->login_task == NULL, NULL); |
| 1548 |
cockpit_session_reset (session); |
| 1549 |
|
| 1550 |
if (session->authorize) |
| 1551 |
{ |
| 1552 |
if (build_authorize_challenge (self, session->authorize, connection, |
| 1553 |
headers, &body, &session->conversation)) |
| 1554 |
{ |
| 1555 |
if (session->conversation) |
| 1556 |
{ |
| 1557 |
reset_authorize_timeout (session, TRUE); |
| 1558 |
g_hash_table_replace (self->conversations, session->conversation, cockpit_session_ref (session)); |
| 1559 |
} |
| 1560 |
} |
| 1561 |
} |
| 1562 |
|
| 1563 |
if (session->initialized) |
| 1564 |
{ |
| 1565 |
/* Start off in the idling state, and begin a timeout during which caller must do something else */ |
| 1566 |
on_web_service_idling (session->service, session); |
| 1567 |
CockpitCreds *creds = cockpit_web_service_get_creds (session->service); |
| 1568 |
|
| 1569 |
g_autofree gchar *id = cockpit_auth_nonce (self); |
| 1570 |
session->cookie = g_strdup_printf ("v=2;k=%s", id); |
| 1571 |
g_hash_table_insert (self->sessions, session->cookie, cockpit_session_ref (session)); |
| 1572 |
|
| 1573 |
if (headers) |
| 1574 |
{ |
| 1575 |
gboolean force_secure; |
| 1576 |
|
| 1577 |
if (self->flags & COCKPIT_AUTH_FOR_TLS_PROXY) |
| 1578 |
force_secure = TRUE; |
| 1579 |
else |
| 1580 |
force_secure = connection ? !G_IS_SOCKET_CONNECTION (connection) : TRUE; |
| 1581 |
|
| 1582 |
g_autofree gchar *cookie_name = application_cookie_name (cockpit_creds_get_application (creds)); |
| 1583 |
g_autofree gchar *cookie_b64 = g_base64_encode ((guint8 *)session->cookie, strlen (session->cookie)); |
| 1584 |
g_hash_table_insert (headers, g_strdup ("Set-Cookie"), |
| 1585 |
g_strdup_printf ("%s=%s; Path=/; SameSite=Strict;%s HttpOnly", |
| 1586 |
cookie_name, cookie_b64, force_secure ? " Secure;" : "")); |
| 1587 |
} |
| 1588 |
|
| 1589 |
if (body) |
| 1590 |
json_object_unref (body); |
| 1591 |
body = cockpit_creds_to_json (creds); |
| 1592 |
|
| 1593 |
/* Successful login */ |
| 1594 |
g_info ("User %s logged into session %s", |
| 1595 |
cockpit_creds_get_user (creds), |
| 1596 |
cockpit_web_service_get_id (session->service)); |
| 1597 |
} |
| 1598 |
else |
| 1599 |
{ |
| 1600 |
g_set_error (error, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 1601 |
"Authentication failed"); |
| 1602 |
} |
| 1603 |
|
| 1604 |
out: |
| 1605 |
self->startups--; |
| 1606 |
|
| 1607 |
return g_steal_pointer (&body); |
| 1608 |
} |
| 1609 |
|
| 1610 |
CockpitAuth * |
| 1611 |
cockpit_auth_new (gboolean login_loopback, |
| 1612 |
CockpitAuthFlags flags) |
| 1613 |
{ |
| 1614 |
CockpitAuth *self = g_object_new (COCKPIT_TYPE_AUTH, NULL); |
| 1615 |
const gchar *max_startups_conf; |
| 1616 |
gint count = 0; |
| 1617 |
|
| 1618 |
self->flags = flags; |
| 1619 |
self->login_loopback = login_loopback; |
| 1620 |
|
| 1621 |
if (cockpit_ws_max_startups == NULL) |
| 1622 |
max_startups_conf = cockpit_conf_string ("WebService", "MaxStartups"); |
| 1623 |
else |
| 1624 |
max_startups_conf = cockpit_ws_max_startups; |
| 1625 |
|
| 1626 |
self->max_startups = max_startups; |
| 1627 |
self->max_startups_begin = max_startups; |
| 1628 |
self->max_startups_rate = 100; |
| 1629 |
|
| 1630 |
if (max_startups_conf) |
| 1631 |
{ |
| 1632 |
count = sscanf (max_startups_conf, "%u:%u:%u", |
| 1633 |
&self->max_startups_begin, |
| 1634 |
&self->max_startups_rate, |
| 1635 |
&self->max_startups); |
| 1636 |
|
| 1637 |
/* If all three numbers are not given use the |
| 1638 |
* first as a hard limit */ |
| 1639 |
if (count == 1 || count == 2) |
| 1640 |
{ |
| 1641 |
self->max_startups = self->max_startups_begin; |
| 1642 |
self->max_startups_rate = 100; |
| 1643 |
} |
| 1644 |
|
| 1645 |
if (count < 1 || count > 3 || |
| 1646 |
self->max_startups_begin > self->max_startups || |
| 1647 |
self->max_startups_rate > 100 || self->max_startups_rate < 1) |
| 1648 |
{ |
| 1649 |
g_warning ("Illegal MaxStartups spec: %s. Reverting to defaults", max_startups_conf); |
| 1650 |
self->max_startups = max_startups; |
| 1651 |
self->max_startups_begin = max_startups; |
| 1652 |
self->max_startups_rate = 100; |
| 1653 |
} |
| 1654 |
} |
| 1655 |
|
| 1656 |
return self; |
| 1657 |
} |
| 1658 |
|
| 1659 |
gchar * |
| 1660 |
cockpit_auth_parse_application (const gchar *path, |
| 1661 |
gboolean *is_host) |
| 1662 |
{ |
| 1663 |
const gchar *pos; |
| 1664 |
gchar *tmp = NULL; |
| 1665 |
gchar *val = NULL; |
| 1666 |
|
| 1667 |
g_return_val_if_fail (path != NULL, NULL); |
| 1668 |
g_return_val_if_fail (path[0] == '/', NULL); |
| 1669 |
|
| 1670 |
path += 1; |
| 1671 |
|
| 1672 |
/* We are being embedded as a specific application */ |
| 1673 |
if (g_str_has_prefix (path, "cockpit+") && path[8] != '\0') |
| 1674 |
{ |
| 1675 |
pos = strchr (path, '/'); |
| 1676 |
if (pos) |
| 1677 |
val = g_strndup (path, pos - path); |
| 1678 |
else |
| 1679 |
val = g_strdup (path); |
| 1680 |
} |
| 1681 |
else if (path[0] == '=' && path[1] != '\0') |
| 1682 |
{ |
| 1683 |
pos = strchr (path, '/'); |
| 1684 |
if (pos) |
| 1685 |
{ |
| 1686 |
tmp = g_strndup (path, pos - path); |
| 1687 |
val = g_strdup_printf ("cockpit+%s", tmp); |
| 1688 |
} |
| 1689 |
else |
| 1690 |
{ |
| 1691 |
val = g_strdup_printf ("cockpit+%s", path); |
| 1692 |
} |
| 1693 |
|
| 1694 |
} |
| 1695 |
else |
| 1696 |
{ |
| 1697 |
val = g_strdup ("cockpit"); |
| 1698 |
} |
| 1699 |
|
| 1700 |
if (is_host) |
| 1701 |
*is_host = application_parse_host (val) != NULL; |
| 1702 |
|
| 1703 |
g_free (tmp); |
| 1704 |
return val; |
| 1705 |
} |
| 1706 |
|
| 1707 |
gchar * |
| 1708 |
cockpit_auth_empty_cookie_value (const gchar *path, gboolean secure) |
| 1709 |
{ |
| 1710 |
gchar *application = cockpit_auth_parse_application (path, NULL); |
| 1711 |
gchar *cookie = application_cookie_name (application); |
| 1712 |
|
| 1713 |
/* this is completely security irrelevant, but security scanners complain |
| 1714 |
* about the lack of Secure (rhbz#1677767) */ |
| 1715 |
gchar *cookie_line = g_strdup_printf ("%s=deleted; PATH=/; SameSite=strict;%s HttpOnly", |
| 1716 |
cookie, |
| 1717 |
secure ? " Secure;" : ""); |
| 1718 |
|
| 1719 |
g_free (application); |
| 1720 |
g_free (cookie); |
| 1721 |
|
| 1722 |
return cookie_line; |
| 1723 |
} |