/* * For backwards compatibility we need to normalize some host params * so they can be matched against. * * Some sessions shouldn't be shared by multiple channels, such as those that * explicitly specify a host-key or specific user. This changed over time * so modify things to make it a simple match. * * If the given user is the current user, remove it. Preserves the current * behavior. * */ static void cockpit_router_normalize_host_params (JsonObject *options) { const gchar *sharable = NULL; const gchar *user = NULL; gboolean needs_private = FALSE; if (!cockpit_json_get_string (options, "session", NULL, &sharable)) sharable = NULL; if (!cockpit_json_get_string (options, "user", NULL, &user)) user = NULL; if (!sharable) { /* Fallback to older ways of indicating this */ if (user || json_object_has_member (options, "host-key")) needs_private = TRUE; if (json_object_has_member (options, "temp-session")) { if (needs_private && !cockpit_json_get_bool (options, "temp-session", TRUE, &needs_private)) needs_private = TRUE; json_object_remove_member (options, "temp-session"); } } if (g_strcmp0 (user, g_get_user_name ()) == 0) json_object_remove_member (options, "user"); if (needs_private) json_object_set_string_member (options, "session", "private"); }
/** * couchdb_struct_field_remove_field: * @sf: A #CouchdbStructField object * @field: Name of the field to remove * * Remove a field from the given #CouchdbStructField object. */ void couchdb_struct_field_remove_field (CouchdbStructField *sf, const char *field) { g_return_if_fail (sf != NULL); g_return_if_fail (field != NULL); json_object_remove_member (sf->json_object, field); }
void cockpit_channel_response_open (CockpitWebService *service, GHashTable *in_headers, CockpitWebResponse *response, JsonObject *open) { CockpitTransport *transport; WebSocketDataType data_type; GHashTable *headers; const gchar *content_type; const gchar *content_disposition; /* Parse the external */ if (!cockpit_web_service_parse_external (open, &content_type, &content_disposition, NULL)) { cockpit_web_response_error (response, 400, NULL, "Bad channel request"); return; } transport = cockpit_web_service_ensure_transport (service, open); if (!transport) { cockpit_web_response_error (response, 502, NULL, "Failed to open channel transport"); return; } headers = cockpit_web_server_new_table (); if (content_disposition) g_hash_table_insert (headers, g_strdup ("Content-Disposition"), g_strdup (content_disposition)); if (!json_object_has_member (open, "binary")) json_object_set_string_member (open, "binary", "raw"); if (!content_type) { if (!cockpit_web_service_parse_binary (open, &data_type)) g_return_if_reached (); if (data_type == WEB_SOCKET_DATA_TEXT) content_type = "text/plain"; else content_type = "application/octet-stream"; } g_hash_table_insert (headers, g_strdup ("Content-Type"), g_strdup (content_type)); /* We shouldn't need to send this part further */ json_object_remove_member (open, "external"); cockpit_channel_response_create (service, response, transport, NULL, headers, open); g_hash_table_unref (headers); }
static JsonNode *trg_prefs_get_value_inner(JsonObject * obj, const gchar * key, int type, int flags) { if (json_object_has_member(obj, key)) { if ((flags & TRG_PREFS_REPLACENODE)) json_object_remove_member(obj, key); else return json_object_get_member(obj, key); } if ((flags & TRG_PREFS_NEWNODE) || (flags & TRG_PREFS_REPLACENODE)) { JsonNode *newNode = json_node_new(type); json_object_set_member(obj, key, newNode); return newNode; } return NULL; }
static void cockpit_auth_prepare_login_reply (CockpitAuth *self, JsonObject *prompt_data, GHashTable *headers, AuthData *ad) { const gchar *prompt; gchar *encoded_data = NULL; g_return_if_fail (ad->pending_result == NULL); // Will fail if prompt is not present prompt = json_object_get_string_member (prompt_data, "prompt"); encoded_data = g_base64_encode ((guint8 *)prompt, strlen (prompt)); g_hash_table_replace (headers, g_strdup ("WWW-Authenticate"), g_strdup_printf ("%s %s %s", LOGIN_REPLY_HEADER, ad->id, encoded_data)); g_hash_table_insert (self->authentication_pending, ad->id, auth_data_ref (ad)); json_object_remove_member (prompt_data, "prompt"); g_free (encoded_data); }
static gboolean cockpit_router_normalize_host (CockpitRouter *self, JsonObject *options) { const gchar *host; gchar *actual_host = NULL; gchar *key = NULL; gchar **parts = NULL; if (!cockpit_json_get_string (options, "host", self->init_host, &host)) return FALSE; parts = g_strsplit (host, "+", 3); if (g_strv_length (parts) == 3 && !is_empty (parts[0]) && !is_empty (parts[1]) && !is_empty (parts[2])) { key = g_strdup_printf ("host-%s", parts[1]); if (!json_object_has_member (options, key)) { json_object_set_string_member (options, key, parts[2]); actual_host = parts[0]; } } if (!actual_host) actual_host = (gchar *) host; if (g_strcmp0 (self->init_host, actual_host) == 0) json_object_remove_member (options, "host"); else if (g_strcmp0 (host, actual_host) != 0) json_object_set_string_member (options, "host", actual_host); g_strfreev (parts); g_free (key); return TRUE; }
void trg_tree_view_persist(TrgTreeView * tv, guint flags) { JsonObject *props = trg_prefs_get_tree_view_props(tv); GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(tv)); GList *cols, *li; JsonArray *widths, *columns; gint sort_column_id; GtkSortType sort_type; if (flags & TRG_TREE_VIEW_PERSIST_SORT) { gtk_tree_sortable_get_sort_column_id(GTK_TREE_SORTABLE ((flags & TRG_TREE_VIEW_SORTABLE_PARENT) ? gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model)) : model), &sort_column_id, &sort_type); if (json_object_has_member(props, TRG_PREFS_KEY_TV_SORT_COL)) json_object_remove_member(props, TRG_PREFS_KEY_TV_SORT_COL); if (json_object_has_member(props, TRG_PREFS_KEY_TV_SORT_TYPE)) json_object_remove_member(props, TRG_PREFS_KEY_TV_SORT_TYPE); json_object_set_int_member(props, TRG_PREFS_KEY_TV_SORT_COL, (gint64) sort_column_id); json_object_set_int_member(props, TRG_PREFS_KEY_TV_SORT_TYPE, (gint64) sort_type); } if (flags & TRG_TREE_VIEW_PERSIST_LAYOUT) { cols = gtk_tree_view_get_columns(GTK_TREE_VIEW(tv)); if (json_object_has_member(props, TRG_PREFS_KEY_TV_WIDTHS)) json_object_remove_member(props, TRG_PREFS_KEY_TV_WIDTHS); widths = json_array_new(); json_object_set_array_member(props, TRG_PREFS_KEY_TV_WIDTHS, widths); if (json_object_has_member(props, TRG_PREFS_KEY_TV_COLUMNS)) json_object_remove_member(props, TRG_PREFS_KEY_TV_COLUMNS); columns = json_array_new(); json_object_set_array_member(props, TRG_PREFS_KEY_TV_COLUMNS, columns); for (li = cols; li; li = g_list_next(li)) { GtkTreeViewColumn *col = (GtkTreeViewColumn *) li->data; trg_column_description *desc = g_object_get_data(G_OBJECT(li->data), GDATA_KEY_COLUMN_DESC); json_array_add_string_element(columns, desc->id); json_array_add_int_element(widths, gtk_tree_view_column_get_width (col)); } g_list_free(cols); } }
GHashTable * cockpit_package_listing (JsonArray **json) { JsonArray *root = NULL; GHashTable *listing; CockpitPackage *package; GHashTable *ids; JsonObject *object; JsonArray *id; GList *names, *l; GList *packages; const gchar *name; JsonNode *node; JsonArray *array; guint i, length; listing = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, cockpit_package_unref); build_package_listing (listing); /* Add aliases to the listing */ packages = g_hash_table_get_values (listing); packages = g_list_sort (packages, compar_packages); g_list_foreach (packages, (GFunc)cockpit_package_ref, NULL); for (l = packages; l != NULL; l = g_list_next (l)) { package = l->data; node = json_object_get_member (package->manifest, "alias"); if (node) { /* * Process and remove "alias" from the manifest, as it results in * confusing and duplicated information for the front end. */ package->alias = node = json_node_copy (node); json_object_remove_member (package->manifest, "alias"); if (JSON_NODE_HOLDS_ARRAY (node)) { array = json_node_get_array (node); length = json_array_get_length (array); for (i = 0; i < length; i++) add_alias_to_listing (listing, package, json_array_get_element (array, i)); } else { add_alias_to_listing (listing, package, node); } } } g_list_free_full (packages, (GDestroyNotify)cockpit_package_unref); /* Now wrap up the checksums */ finish_checksums (listing); /* Add checksums to the listing */ packages = g_hash_table_get_values (listing); g_list_foreach (packages, (GFunc)cockpit_package_ref, NULL); for (l = packages; l != NULL; l = g_list_next (l)) { package = l->data; if (package->checksum && !g_hash_table_contains (listing, package->checksum)) { g_hash_table_replace (listing, package->checksum, cockpit_package_ref (package)); g_debug ("%s: package has checksum: %s", package->name, package->checksum); } } g_list_free_full (packages, (GDestroyNotify)cockpit_package_unref); /* Build JSON packages block */ if (json) { *json = root = json_array_new (); ids = g_hash_table_new (g_direct_hash, g_direct_equal); names = g_hash_table_get_keys (listing); names = g_list_sort (names, (GCompareFunc)strcmp); for (l = names; l != NULL; l = g_list_next (l)) { name = l->data; package = g_hash_table_lookup (listing, name); id = g_hash_table_lookup (ids, package); if (!id) { object = json_object_new (); id = json_array_new(); /* The actual package name always comes first */ json_object_set_array_member (object, "id", id); json_array_add_string_element (id, package->name); g_hash_table_insert (ids, package, id); json_object_set_object_member (object, "manifest", json_object_ref (package->manifest)); json_array_add_object_element (root, object); } /* Other ways to refer to the package */ if (!g_str_equal (name, package->name)) json_array_add_string_element (id, name); } g_list_free (names); g_hash_table_destroy (ids); } return listing; }
static gboolean process_includes (RpmOstreeTreeComposeContext *self, GFile *treefile_path, guint depth, JsonObject *root, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; const char *include_path; const guint maxdepth = 50; if (depth > maxdepth) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Exceeded maximum include depth of %u", maxdepth); goto out; } { g_autoptr(GFile) parent = g_file_get_parent (treefile_path); gboolean existed = FALSE; if (self->treefile_context_dirs->len > 0) { GFile *prev = self->treefile_context_dirs->pdata[self->treefile_context_dirs->len-1]; if (g_file_equal (parent, prev)) existed = TRUE; } if (!existed) { g_ptr_array_add (self->treefile_context_dirs, parent); parent = NULL; /* Transfer ownership */ } } if (!_rpmostree_jsonutil_object_get_optional_string_member (root, "include", &include_path, error)) goto out; if (include_path) { g_autoptr(GFile) treefile_dirpath = g_file_get_parent (treefile_path); g_autoptr(GFile) parent_path = g_file_resolve_relative_path (treefile_dirpath, include_path); glnx_unref_object JsonParser *parent_parser = json_parser_new (); JsonNode *parent_rootval; JsonObject *parent_root; GList *members; GList *iter; if (!json_parser_load_from_file (parent_parser, gs_file_get_path_cached (parent_path), error)) goto out; parent_rootval = json_parser_get_root (parent_parser); if (!JSON_NODE_HOLDS_OBJECT (parent_rootval)) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Treefile root is not an object"); goto out; } parent_root = json_node_get_object (parent_rootval); if (!process_includes (self, parent_path, depth + 1, parent_root, cancellable, error)) goto out; members = json_object_get_members (parent_root); for (iter = members; iter; iter = iter->next) { const char *name = iter->data; JsonNode *parent_val = json_object_get_member (parent_root, name); JsonNode *val = json_object_get_member (root, name); g_assert (parent_val); if (!val) json_object_set_member (root, name, json_node_copy (parent_val)); else { JsonNodeType parent_type = json_node_get_node_type (parent_val); JsonNodeType child_type = json_node_get_node_type (val); if (parent_type != child_type) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Conflicting element type of '%s'", name); goto out; } if (child_type == JSON_NODE_ARRAY) { JsonArray *parent_array = json_node_get_array (parent_val); JsonArray *child_array = json_node_get_array (val); JsonArray *new_child = json_array_new (); guint i, len; len = json_array_get_length (parent_array); for (i = 0; i < len; i++) json_array_add_element (new_child, json_node_copy (json_array_get_element (parent_array, i))); len = json_array_get_length (child_array); for (i = 0; i < len; i++) json_array_add_element (new_child, json_node_copy (json_array_get_element (child_array, i))); json_object_set_array_member (root, name, new_child); } } } json_object_remove_member (root, "include"); } ret = TRUE; out: return ret; }