| 1 |
/* |
| 2 |
* Copyright (C) 2016 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "cockpitwebserver.h" |
| 9 |
#include "cockpittransport.h" |
| 10 |
#include "cockpitsystem.h" |
| 11 |
#include "common/cockpitconf.h" |
| 12 |
|
| 13 |
#include "cockpitauth.h" |
| 14 |
#include "cockpitbranding.h" |
| 15 |
#include "cockpitwebservice.h" |
| 16 |
|
| 17 |
static void |
| 18 |
add_config_branding_dirs (GPtrArray *dirs) |
| 19 |
{ |
| 20 |
const gchar * const* config_dirs; |
| 21 |
|
| 22 |
config_dirs = cockpit_conf_get_dirs (); |
| 23 |
while (config_dirs && config_dirs[0]) |
| 24 |
{ |
| 25 |
g_autofree gchar *config_path = g_build_filename (config_dirs[0], "cockpit", "branding", NULL); |
| 26 |
|
| 27 |
if (g_file_test (config_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) |
| 28 |
g_ptr_array_add (dirs, g_steal_pointer (&config_path)); |
| 29 |
|
| 30 |
config_dirs++; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
static const gchar * const* |
| 35 |
get_system_data_dirs (void) |
| 36 |
{ |
| 37 |
const gchar *env; |
| 38 |
|
| 39 |
env = g_getenv ("XDG_DATA_DIRS"); |
| 40 |
if (env && env[0]) |
| 41 |
return g_get_system_data_dirs (); |
| 42 |
|
| 43 |
return NULL; |
| 44 |
} |
| 45 |
|
| 46 |
static void |
| 47 |
add_system_dirs (GPtrArray *dirs) |
| 48 |
{ |
| 49 |
const gchar * const* system; |
| 50 |
system = get_system_data_dirs (); |
| 51 |
while (system && system[0]) |
| 52 |
{ |
| 53 |
g_ptr_array_add (dirs, g_build_filename (system[0], "cockpit", "static", NULL)); |
| 54 |
system++; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
static void |
| 59 |
add_system_branding_dirs (GPtrArray *dirs, const gchar *branding_path) |
| 60 |
{ |
| 61 |
const gchar * const* system; |
| 62 |
system = get_system_data_dirs (); |
| 63 |
while (system && system[0]) |
| 64 |
{ |
| 65 |
g_ptr_array_add (dirs, g_build_filename (system[0], "cockpit", "branding", branding_path, NULL)); |
| 66 |
system++; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
static gboolean |
| 71 |
is_valid_path_component (const gchar *str) |
| 72 |
{ |
| 73 |
return str && str[0] != '\0' && str[0] != '.' && !strchr (str, '/'); |
| 74 |
} |
| 75 |
|
| 76 |
gchar ** |
| 77 |
cockpit_branding_calculate_static_roots (const gchar *os_id, |
| 78 |
const gchar *os_variant_id, |
| 79 |
const gchar *os_id_like, |
| 80 |
gboolean is_local) |
| 81 |
{ |
| 82 |
GPtrArray *dirs; |
| 83 |
gchar **roots; |
| 84 |
const gchar *static_dir; |
| 85 |
|
| 86 |
dirs = g_ptr_array_new_with_free_func (g_free); |
| 87 |
|
| 88 |
add_config_branding_dirs (dirs); |
| 89 |
|
| 90 |
if (is_local) |
| 91 |
add_system_dirs (dirs); |
| 92 |
|
| 93 |
if (is_valid_path_component (os_id)) |
| 94 |
{ |
| 95 |
if (is_valid_path_component (os_variant_id)) |
| 96 |
{ |
| 97 |
g_autofree gchar *variant_path = g_strdup_printf ("%s-%s", os_id, os_variant_id); |
| 98 |
add_system_branding_dirs (dirs, variant_path); |
| 99 |
g_ptr_array_add (dirs, g_strdup_printf (DATADIR "/cockpit/branding/%s", variant_path)); |
| 100 |
} |
| 101 |
add_system_branding_dirs (dirs, os_id); |
| 102 |
g_ptr_array_add (dirs, g_strdup_printf (DATADIR "/cockpit/branding/%s", os_id)); |
| 103 |
} |
| 104 |
|
| 105 |
if (os_id_like) |
| 106 |
{ |
| 107 |
gchar **ids; |
| 108 |
|
| 109 |
ids = g_strsplit_set (os_id_like, " ", -1); |
| 110 |
for (gint i = 0; ids[i]; i += 1) |
| 111 |
{ |
| 112 |
if (!is_valid_path_component (ids[i])) |
| 113 |
continue; |
| 114 |
add_system_branding_dirs (dirs, ids[i]); |
| 115 |
g_ptr_array_add (dirs, g_strdup_printf (DATADIR "/cockpit/branding/%s", ids[i])); |
| 116 |
} |
| 117 |
|
| 118 |
g_strfreev (ids); |
| 119 |
} |
| 120 |
|
| 121 |
if (!is_local) |
| 122 |
add_system_dirs (dirs); |
| 123 |
|
| 124 |
add_system_branding_dirs (dirs, "default"); |
| 125 |
g_ptr_array_add (dirs, g_strdup (DATADIR "/cockpit/branding/default")); |
| 126 |
static_dir = cockpit_conf_string ("WebService", "CustomLoginPage"); |
| 127 |
if (static_dir) |
| 128 |
g_ptr_array_add (dirs, g_strdup (static_dir)); |
| 129 |
g_ptr_array_add (dirs, g_strdup (DATADIR "/cockpit/static")); |
| 130 |
g_ptr_array_add (dirs, NULL); |
| 131 |
|
| 132 |
roots = cockpit_web_response_resolve_roots ((const gchar **)dirs->pdata); |
| 133 |
|
| 134 |
g_ptr_array_free (dirs, TRUE); |
| 135 |
return roots; |
| 136 |
} |
| 137 |
|
| 138 |
static void |
| 139 |
serve_branding_css_file (CockpitWebResponse *response, |
| 140 |
const gchar *path, |
| 141 |
const gchar **roots, |
| 142 |
GHashTable *os_release) |
| 143 |
{ |
| 144 |
if (os_release) |
| 145 |
cockpit_web_response_template (response, path, roots, os_release); |
| 146 |
else |
| 147 |
cockpit_web_response_file (response, path, roots); |
| 148 |
} |
| 149 |
|
| 150 |
typedef struct { |
| 151 |
const gchar *path; |
| 152 |
CockpitWebResponse *response; |
| 153 |
} CockpitBrandingData; |
| 154 |
|
| 155 |
|
| 156 |
static void |
| 157 |
serve_branding_css_with_init_data (CockpitWebService *service, |
| 158 |
CockpitWebResponse *response, |
| 159 |
const gchar *path) |
| 160 |
{ |
| 161 |
CockpitTransport *transport = NULL; |
| 162 |
GHashTable *os_release = NULL; |
| 163 |
gchar **roots = NULL; |
| 164 |
JsonObject *os = NULL; |
| 165 |
gboolean responded = FALSE; |
| 166 |
JsonObject *init = NULL; |
| 167 |
|
| 168 |
init = cockpit_web_service_get_init (service); |
| 169 |
if (!init) |
| 170 |
goto out; |
| 171 |
|
| 172 |
transport = cockpit_web_service_get_transport (service); |
| 173 |
if (!transport) |
| 174 |
goto out; |
| 175 |
|
| 176 |
roots = g_object_get_data (G_OBJECT (transport), "static-roots"); |
| 177 |
if (!roots) |
| 178 |
{ |
| 179 |
if (cockpit_json_get_object (init, "os-release", NULL, &os) && os) |
| 180 |
os_release = cockpit_json_to_hash_table (os, cockpit_system_os_release_fields ()); |
| 181 |
|
| 182 |
if (os_release) |
| 183 |
{ |
| 184 |
roots = cockpit_branding_calculate_static_roots (g_hash_table_lookup (os_release, "ID"), |
| 185 |
g_hash_table_lookup (os_release, "VARIANT_ID"), |
| 186 |
g_hash_table_lookup (os_release, "ID_LIKE"), |
| 187 |
FALSE); |
| 188 |
g_object_set_data_full (G_OBJECT (transport), "os-release", os_release, |
| 189 |
(GDestroyNotify) g_hash_table_unref); |
| 190 |
} |
| 191 |
else |
| 192 |
{ |
| 193 |
roots = cockpit_branding_calculate_static_roots (NULL, NULL, NULL, FALSE); |
| 194 |
} |
| 195 |
|
| 196 |
g_object_set_data_full (G_OBJECT (transport), "static-roots", roots, |
| 197 |
(GDestroyNotify) g_strfreev); |
| 198 |
} |
| 199 |
else |
| 200 |
{ |
| 201 |
os_release = g_object_get_data (G_OBJECT (transport), "os-release"); |
| 202 |
} |
| 203 |
|
| 204 |
serve_branding_css_file (response, path, (const gchar **)roots, os_release); |
| 205 |
responded = TRUE; |
| 206 |
|
| 207 |
out: |
| 208 |
if (!responded) |
| 209 |
cockpit_web_response_error (response, 502, NULL, NULL); |
| 210 |
} |
| 211 |
|
| 212 |
void |
| 213 |
cockpit_branding_serve (CockpitWebService *service, |
| 214 |
CockpitWebResponse *response, |
| 215 |
const gchar *full_path, |
| 216 |
const gchar *static_path, |
| 217 |
GHashTable *local_os_release, |
| 218 |
const gchar **local_roots) |
| 219 |
{ |
| 220 |
gboolean is_host = FALSE; |
| 221 |
gchar *application = cockpit_auth_parse_application (full_path, &is_host); |
| 222 |
|
| 223 |
/* Must be logged in to use a host url */ |
| 224 |
if (is_host && !service) |
| 225 |
{ |
| 226 |
cockpit_web_response_error (response, 403, NULL, NULL); |
| 227 |
goto out; |
| 228 |
} |
| 229 |
|
| 230 |
cockpit_web_response_set_cache_type (response, COCKPIT_WEB_RESPONSE_CACHE); |
| 231 |
|
| 232 |
if (g_str_has_suffix (static_path, ".css")) |
| 233 |
{ |
| 234 |
if (!is_host) |
| 235 |
{ |
| 236 |
serve_branding_css_file (response, static_path, local_roots, local_os_release); |
| 237 |
} |
| 238 |
else |
| 239 |
{ |
| 240 |
serve_branding_css_with_init_data (service, response, static_path); |
| 241 |
} |
| 242 |
} |
| 243 |
else |
| 244 |
{ |
| 245 |
cockpit_web_response_file (response, static_path, local_roots); |
| 246 |
} |
| 247 |
|
| 248 |
out: |
| 249 |
g_free (application); |
| 250 |
} |