Debian Codegraph @

src/ws/main.c

raw
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 <gio/gio.h>
9 #include <glib-unix.h>
10 #include <glib/gstdio.h>
11
12 #include <dirent.h>
13 #include <string.h>
14
15 #include <systemd/sd-daemon.h>
16
17 #include "cockpitws.h"
18
19 #include "cockpithandlers.h"
20 #include "cockpitbranding.h"
21
22 #include "common/cockpitconf.h"
23 #include "common/cockpitmemory.h"
24 #include "cockpitsystem.h"
25
26 /* ---------------------------------------------------------------------------------------------------- */
27
28 static gint opt_port = 9090;
29 static gchar *opt_address = NULL;
30 static gboolean opt_no_tls = FALSE;
31 static gboolean opt_for_tls_proxy = FALSE;
32 static gboolean opt_local_ssh = FALSE;
33 static gchar *opt_local_session = NULL;
34 static gboolean opt_version = FALSE;
35
36 static GOptionEntry cmd_entries[] = {
37 {"port", 'p', 0, G_OPTION_ARG_INT, &opt_port, "Local port to bind to (9090 if unset)", NULL},
38 {"address", 'a', 0, G_OPTION_ARG_STRING, &opt_address, "Address to bind to (binds on all addresses if unset)", "ADDRESS"},
39 {"no-tls", 0, 0, G_OPTION_ARG_NONE, &opt_no_tls, "Disable http to https redirection", NULL},
40 {"for-tls-proxy", 0, 0, G_OPTION_ARG_NONE, &opt_for_tls_proxy,
41 "Act behind a https-terminating proxy: accept only https:// origins by default",
42 NULL},
43 {"local-ssh", 0, 0, G_OPTION_ARG_NONE, &opt_local_ssh, "Log in locally via SSH", NULL },
44 {"local-session", 0, 0, G_OPTION_ARG_STRING, &opt_local_session,
45 "Launch a bridge in the local session (path to cockpit-bridge or '-' for stdin/out); implies --no-tls",
46 "BRIDGE" },
47 {"version", 0, 0, G_OPTION_ARG_NONE, &opt_version, "Print version information", NULL },
48 {NULL}
49 };
50
51 /* ---------------------------------------------------------------------------------------------------- */
52
53 static void
54 print_version (void)
55 {
56 g_print ("Version: %s\n", PACKAGE_VERSION);
57 g_print ("Protocol: 1\n");
58 g_print ("Authorization: basic\n");
59 }
60
61 static gchar **
62 setup_static_roots (GHashTable *os_release)
63 {
64 gchar **roots;
65 const gchar *os_variant_id;
66 const gchar *os_id;
67 const gchar *os_id_like;
68
69 if (os_release)
70 {
71 os_id = g_hash_table_lookup (os_release, "ID");
72 os_variant_id = g_hash_table_lookup (os_release, "VARIANT_ID");
73 os_id_like = g_hash_table_lookup (os_release, "ID_LIKE");
74 }
75 else
76 {
77 os_id = NULL;
78 os_variant_id = NULL;
79 os_id_like = NULL;
80 }
81
82 roots = cockpit_branding_calculate_static_roots (os_id, os_variant_id, os_id_like, TRUE);
83
84 return roots;
85 }
86
87 static void
88 on_local_ready (GObject *object,
89 GAsyncResult *result,
90 gpointer data)
91 {
92 cockpit_web_server_start (COCKPIT_WEB_SERVER (data));
93 g_object_unref (data);
94 }
95
96 int
97 main (int argc,
98 char *argv[])
99 {
100 gint ret = 1;
101 g_autoptr(GOptionContext) context = NULL;
102 g_autoptr(GError) error = NULL;
103 g_auto(GStrv) roots = NULL;
104 g_autoptr(GMainLoop) loop = NULL;
105 g_autofree gchar *login_html = NULL;
106 g_autofree gchar *login_po_js = NULL;
107 g_autoptr(CockpitWebServer) server = NULL;
108 CockpitWebServerFlags server_flags = COCKPIT_WEB_SERVER_NONE;
109 CockpitHandlerData data;
110
111 signal (SIGPIPE, SIG_IGN);
112 cockpit_setenv_check ("GSETTINGS_BACKEND", "memory", TRUE);
113 cockpit_setenv_check ("GIO_USE_PROXY_RESOLVER", "dummy", TRUE);
114 cockpit_setenv_check ("GIO_USE_VFS", "local", TRUE);
115
116 /* Any interaction with a krb5 ccache should be explicit */
117 cockpit_setenv_check ("KRB5CCNAME", "FILE:/dev/null", TRUE);
118
119 memset (&data, 0, sizeof (data));
120
121 context = g_option_context_new (NULL);
122 g_option_context_add_main_entries (context, cmd_entries, NULL);
123 if (!g_option_context_parse (context, &argc, &argv, &error))
124 goto out;
125
126 /* check mutually exclusive options */
127 if (opt_for_tls_proxy && opt_no_tls)
128 {
129 g_printerr ("--for-tls-proxy and --no-tls are mutually exclusive\n");
130 goto out;
131 }
132
133 if (opt_version)
134 {
135 print_version ();
136 ret = 0;
137 goto out;
138 }
139
140 if (opt_for_tls_proxy || cockpit_conf_bool ("WebService", "X-For-CockpitClient", FALSE))
141 opt_no_tls = TRUE;
142
143 g_log_writer_default_set_use_stderr (TRUE);
144
145 loop = g_main_loop_new (NULL, FALSE);
146
147 data.os_release = cockpit_system_load_os_release ();
148 data.auth = cockpit_auth_new (opt_local_ssh, opt_for_tls_proxy ? COCKPIT_AUTH_FOR_TLS_PROXY : COCKPIT_AUTH_NONE);
149 roots = setup_static_roots (data.os_release);
150
151 const gchar *static_dir = cockpit_conf_string ("WebService", "CustomLoginPage");
152 const gchar *root_dir = static_dir ? static_dir : DATADIR "/cockpit/static";
153 data.branding_roots = (const gchar **)roots;
154
155 login_html = g_strconcat (root_dir, "/login.html", NULL);
156 data.login_html = (const gchar *)login_html;
157 login_po_js = g_strconcat (root_dir, "/po.js", NULL);
158 data.login_po_js = (const gchar *)login_po_js;
159
160 if (opt_for_tls_proxy)
161 server_flags |= COCKPIT_WEB_SERVER_FOR_TLS_PROXY;
162 if (!opt_no_tls && !cockpit_conf_bool ("WebService", "AllowUnencrypted", FALSE))
163 server_flags |= COCKPIT_WEB_SERVER_REDIRECT_TLS;
164
165 server = cockpit_web_server_new (server_flags);
166
167 const gint n_listen_fds = sd_listen_fds (true);
168 if (n_listen_fds)
169 {
170 for (gint fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n_listen_fds; fd++)
171 if (!cockpit_web_server_add_fd_listener (server, fd, &error))
172 {
173 g_prefix_error (&error, "Unable to acquire LISTEN_FDS: ");
174 goto out;
175 }
176
177 g_signal_connect_swapped (data.auth, "idling", G_CALLBACK (g_main_loop_quit), loop);
178 }
179 else
180 {
181 if (!cockpit_web_server_add_inet_listener (server, opt_address, opt_port, &error))
182 {
183 g_prefix_error (&error, "Error starting web server: ");
184 goto out;
185 }
186 }
187
188 if (cockpit_conf_string ("WebService", "UrlRoot"))
189 {
190 g_object_set (server, "url-root",
191 cockpit_conf_string ("WebService", "UrlRoot"),
192 NULL);
193 }
194
195 cockpit_web_server_set_protocol_header (server, cockpit_conf_string ("WebService", "ProtocolHeader"));
196 cockpit_web_server_set_forwarded_for_header (server, cockpit_conf_string ("WebService", "ForwardedForHeader"));
197
198 /* Ignores stuff it shouldn't handle */
199 g_signal_connect (server, "handle-stream",
200 G_CALLBACK (cockpit_handler_socket), &data);
201
202 /* External channels, ignore stuff they shouldn't handle */
203 g_signal_connect (server, "handle-stream",
204 G_CALLBACK (cockpit_handler_external), &data);
205
206 /* Don't redirect to TLS for /ping */
207 g_object_set (server, "ssl-exception-prefix", "/ping", NULL);
208 g_signal_connect (server, "handle-resource::/ping",
209 G_CALLBACK (cockpit_handler_ping), &data);
210
211 /* Files that cannot be cache-forever, because of well known names */
212 g_signal_connect (server, "handle-resource::/favicon.ico",
213 G_CALLBACK (cockpit_handler_root), &data);
214 g_signal_connect (server, "handle-resource::/apple-touch-icon.png",
215 G_CALLBACK (cockpit_handler_root), &data);
216 g_signal_connect (server, "handle-resource::/ca.cer",
217 G_CALLBACK (cockpit_handler_ca_cert), &data);
218
219 /* The fallback handler for everything else */
220 g_signal_connect (server, "handle-resource",
221 G_CALLBACK (cockpit_handler_default), &data);
222
223 if (opt_local_session)
224 {
225 g_autoptr(CockpitPipe) pipe = NULL;
226 struct passwd *pwd;
227
228 if (g_str_equal (opt_local_session, "-"))
229 {
230 pipe = cockpit_pipe_new (opt_local_session, 0, 1);
231 }
232 else
233 {
234 g_auto(GStrv) args = NULL;
235 if (!g_shell_parse_argv (opt_local_session, NULL, &args, &error))
236 {
237 g_prefix_error (&error, "--local-session: ");
238 goto out;
239 }
240 pipe = cockpit_pipe_spawn ((const gchar **) args, NULL, NULL, COCKPIT_PIPE_FLAGS_NONE);
241 }
242
243 /* Spawn a local session as a bridge */
244 pwd = getpwuid (geteuid ());
245 if (!pwd)
246 {
247 g_printerr ("Failed to resolve current user id %u\n", geteuid ());
248 goto out;
249 }
250 cockpit_auth_local_async (data.auth, pwd->pw_name, pipe, on_local_ready, g_object_ref (server));
251 }
252 else
253 {
254 /* When no local bridge, start serving immediately */
255 cockpit_web_server_start (server);
256 }
257
258 g_main_loop_run (loop);
259
260 ret = 0;
261
262 out:
263 if (error)
264 g_printerr ("cockpit-ws: %s\n", error->message);
265 g_clear_object (&data.auth);
266 if (data.os_release)
267 g_hash_table_unref (data.os_release);
268 g_free (opt_address);
269 g_free (opt_local_session);
270 cockpit_conf_cleanup ();
271 return ret;
272 }
273
274 /* ---------------------------------------------------------------------------------------------------- */