2015-03-23 11:17:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2014 Red Hat, Inc
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Alexander Larsson <alexl@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2014-12-17 13:05:44 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <locale.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2015-03-20 15:21:19 +00:00
|
|
|
#include <string.h>
|
2014-12-17 13:05:44 +00:00
|
|
|
|
2015-09-17 13:27:49 +00:00
|
|
|
#include <gio/gunixinputstream.h>
|
|
|
|
|
2014-12-17 13:05:44 +00:00
|
|
|
#include "libgsystem.h"
|
2015-03-20 15:21:19 +00:00
|
|
|
#include "libglnx/libglnx.h"
|
2014-12-17 13:05:44 +00:00
|
|
|
|
2016-05-06 16:03:47 +00:00
|
|
|
#include "flatpak-builtins.h"
|
|
|
|
#include "flatpak-utils.h"
|
|
|
|
#include "flatpak-chain-input-stream.h"
|
2014-12-17 13:05:44 +00:00
|
|
|
|
|
|
|
static gboolean opt_no_gpg_verify;
|
2015-09-17 09:37:29 +00:00
|
|
|
static gboolean opt_do_gpg_verify;
|
2015-12-04 10:33:30 +00:00
|
|
|
static gboolean opt_do_enumerate;
|
|
|
|
static gboolean opt_no_enumerate;
|
2014-12-17 13:05:44 +00:00
|
|
|
static gboolean opt_if_not_exists;
|
2016-04-27 11:34:14 +00:00
|
|
|
static gboolean opt_enable;
|
|
|
|
static gboolean opt_disable;
|
2015-12-21 21:40:31 +00:00
|
|
|
static int opt_prio = -1;
|
2015-02-06 10:11:10 +00:00
|
|
|
static char *opt_title;
|
2015-09-17 09:37:29 +00:00
|
|
|
static char *opt_url;
|
2015-09-17 13:27:49 +00:00
|
|
|
static char **opt_gpg_import;
|
|
|
|
|
2014-12-17 13:05:44 +00:00
|
|
|
|
2015-09-17 09:37:29 +00:00
|
|
|
static GOptionEntry add_options[] = {
|
2014-12-17 13:05:44 +00:00
|
|
|
{ "if-not-exists", 0, 0, G_OPTION_ARG_NONE, &opt_if_not_exists, "Do nothing if the provided remote exists", NULL },
|
2015-09-17 09:37:29 +00:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static GOptionEntry modify_options[] = {
|
|
|
|
{ "gpg-verify", 0, 0, G_OPTION_ARG_NONE, &opt_do_gpg_verify, "Enable GPG verification", NULL },
|
2015-12-04 10:33:30 +00:00
|
|
|
{ "enumerate", 0, 0, G_OPTION_ARG_NONE, &opt_do_enumerate, "Mark the remote as enumerate", NULL },
|
2015-09-17 09:37:29 +00:00
|
|
|
{ "url", 0, 0, G_OPTION_ARG_STRING, &opt_url, "Set a new url", NULL },
|
2016-04-27 11:34:14 +00:00
|
|
|
{ "enable", 0, 0, G_OPTION_ARG_NONE, &opt_enable, "Enable the remote", },
|
2015-09-17 09:37:29 +00:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static GOptionEntry common_options[] = {
|
|
|
|
{ "no-gpg-verify", 0, 0, G_OPTION_ARG_NONE, &opt_no_gpg_verify, "Disable GPG verification", NULL },
|
2015-12-21 21:40:48 +00:00
|
|
|
{ "no-enumerate", 0, 0, G_OPTION_ARG_NONE, &opt_no_enumerate, "Mark the remote as don't enumerate", NULL },
|
2015-12-21 09:25:41 +00:00
|
|
|
{ "prio", 0, 0, G_OPTION_ARG_INT, &opt_prio, "Set priority (default 1, higher is more prioritized)", NULL },
|
2015-02-06 10:11:10 +00:00
|
|
|
{ "title", 0, 0, G_OPTION_ARG_STRING, &opt_title, "A nice name to use for this remote", "TITLE" },
|
2015-09-17 13:27:49 +00:00
|
|
|
{ "gpg-import", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &opt_gpg_import, "Import GPG key from FILE (- for stdin)", "FILE" },
|
2016-04-27 11:34:14 +00:00
|
|
|
{ "disable", 0, 0, G_OPTION_ARG_NONE, &opt_disable, "Disable the remote", },
|
2014-12-17 13:05:44 +00:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2015-09-17 13:27:49 +00:00
|
|
|
static gboolean
|
|
|
|
open_source_stream (GInputStream **out_source_stream,
|
2016-05-06 14:03:27 +00:00
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2015-09-17 13:27:49 +00:00
|
|
|
{
|
|
|
|
g_autoptr(GInputStream) source_stream = NULL;
|
|
|
|
guint n_keyrings = 0;
|
|
|
|
g_autoptr(GPtrArray) streams = NULL;
|
|
|
|
|
|
|
|
if (opt_gpg_import != NULL)
|
|
|
|
n_keyrings = g_strv_length (opt_gpg_import);
|
|
|
|
|
|
|
|
guint ii;
|
|
|
|
|
|
|
|
streams = g_ptr_array_new_with_free_func (g_object_unref);
|
|
|
|
|
|
|
|
for (ii = 0; ii < n_keyrings; ii++)
|
|
|
|
{
|
|
|
|
GInputStream *input_stream = NULL;
|
|
|
|
|
|
|
|
if (strcmp (opt_gpg_import[ii], "-") == 0)
|
|
|
|
{
|
|
|
|
input_stream = g_unix_input_stream_new (STDIN_FILENO, FALSE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_autoptr(GFile) file = g_file_new_for_path (opt_gpg_import[ii]);
|
2016-05-06 14:03:27 +00:00
|
|
|
input_stream = G_INPUT_STREAM (g_file_read (file, cancellable, error));
|
2015-09-17 13:27:49 +00:00
|
|
|
|
|
|
|
if (input_stream == NULL)
|
2016-04-13 10:59:21 +00:00
|
|
|
{
|
|
|
|
g_prefix_error (error, "The file %s specified for --gpg-import was not found: ", opt_gpg_import[ii]);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-09-17 13:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Takes ownership. */
|
|
|
|
g_ptr_array_add (streams, input_stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Chain together all the --keyring options as one long stream. */
|
2016-05-06 14:37:47 +00:00
|
|
|
source_stream = (GInputStream *) flatpak_chain_input_stream_new (streams);
|
2015-09-17 13:27:49 +00:00
|
|
|
|
|
|
|
*out_source_stream = g_steal_pointer (&source_stream);
|
|
|
|
|
2015-09-28 14:40:19 +00:00
|
|
|
return TRUE;
|
2015-09-17 13:27:49 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 13:43:40 +00:00
|
|
|
GBytes *
|
|
|
|
load_keys (GCancellable *cancellable,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
g_autoptr(GInputStream) input_stream = NULL;
|
|
|
|
g_autoptr(GOutputStream) output_stream = NULL;
|
|
|
|
gssize n_bytes_written;
|
|
|
|
|
|
|
|
if (!open_source_stream (&input_stream, cancellable, error))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
output_stream = g_memory_output_stream_new_resizable ();
|
|
|
|
|
|
|
|
n_bytes_written = g_output_stream_splice (output_stream, input_stream,
|
|
|
|
G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
|
|
|
|
G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
|
|
|
|
NULL, error);
|
|
|
|
if (n_bytes_written < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (output_stream));
|
|
|
|
}
|
|
|
|
|
2016-05-11 14:27:43 +00:00
|
|
|
static GKeyFile *
|
|
|
|
get_config_from_opts (FlatpakDir *dir, const char *remote_name)
|
2015-09-17 13:27:49 +00:00
|
|
|
{
|
2016-05-11 14:27:43 +00:00
|
|
|
GKeyFile *config = ostree_repo_copy_config (flatpak_dir_get_repo (dir));
|
|
|
|
g_autofree char *group = g_strdup_printf ("remote \"%s\"", remote_name);
|
|
|
|
|
|
|
|
if (opt_no_gpg_verify)
|
2015-09-17 13:27:49 +00:00
|
|
|
{
|
2016-05-11 14:27:43 +00:00
|
|
|
g_key_file_set_boolean (config, group, "gpg-verify", FALSE);
|
|
|
|
g_key_file_set_boolean (config, group, "gpg-verify-summary", FALSE);
|
|
|
|
}
|
2015-09-17 13:27:49 +00:00
|
|
|
|
2016-05-11 14:27:43 +00:00
|
|
|
if (opt_do_gpg_verify)
|
|
|
|
{
|
|
|
|
g_key_file_set_boolean (config, group, "gpg-verify", TRUE);
|
|
|
|
g_key_file_set_boolean (config, group, "gpg-verify-summary", TRUE);
|
|
|
|
}
|
2015-09-17 13:27:49 +00:00
|
|
|
|
2016-05-11 14:27:43 +00:00
|
|
|
if (opt_url)
|
2016-05-11 14:31:33 +00:00
|
|
|
{
|
|
|
|
if (g_str_has_prefix (opt_url, "metalink="))
|
|
|
|
g_key_file_set_string (config, group, "metalink", opt_url + strlen ("metalink="));
|
|
|
|
else
|
|
|
|
g_key_file_set_string (config, group, "url", opt_url);
|
|
|
|
}
|
2016-05-11 14:27:43 +00:00
|
|
|
|
|
|
|
if (opt_title)
|
|
|
|
g_key_file_set_string (config, group, "xa.title", opt_title);
|
|
|
|
|
|
|
|
if (opt_no_enumerate)
|
|
|
|
g_key_file_set_boolean (config, group, "xa.noenumerate", TRUE);
|
2015-09-17 13:27:49 +00:00
|
|
|
|
2016-05-11 14:27:43 +00:00
|
|
|
if (opt_do_enumerate)
|
|
|
|
g_key_file_set_boolean (config, group, "xa.noenumerate", FALSE);
|
|
|
|
|
|
|
|
if (opt_disable)
|
|
|
|
g_key_file_set_boolean (config, group, "xa.disable", TRUE);
|
|
|
|
else if (opt_enable)
|
|
|
|
g_key_file_set_boolean (config, group, "xa.disable", FALSE);
|
|
|
|
|
|
|
|
if (opt_prio != -1)
|
|
|
|
{
|
|
|
|
g_autofree char *prio_as_string = g_strdup_printf ("%d", opt_prio);
|
|
|
|
g_key_file_set_string (config, group, "xa.prio", prio_as_string);
|
2015-09-17 13:27:49 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 14:27:43 +00:00
|
|
|
return config;
|
2015-09-17 13:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2016-05-06 14:37:47 +00:00
|
|
|
flatpak_builtin_add_remote (int argc, char **argv,
|
2015-09-17 13:27:49 +00:00
|
|
|
GCancellable *cancellable, GError **error)
|
2014-12-17 13:05:44 +00:00
|
|
|
{
|
2015-09-17 09:37:29 +00:00
|
|
|
g_autoptr(GOptionContext) context = NULL;
|
2016-05-06 14:37:47 +00:00
|
|
|
g_autoptr(FlatpakDir) dir = NULL;
|
2015-09-23 12:52:56 +00:00
|
|
|
g_autoptr(GFile) file = NULL;
|
2016-05-11 14:27:43 +00:00
|
|
|
g_auto(GStrv) remotes = NULL;
|
2015-03-20 15:21:19 +00:00
|
|
|
g_autofree char *title = NULL;
|
2015-09-23 12:52:56 +00:00
|
|
|
g_autofree char *remote_url = NULL;
|
2014-12-17 13:05:44 +00:00
|
|
|
const char *remote_name;
|
2015-09-23 12:52:56 +00:00
|
|
|
const char *url_or_path;
|
2016-05-11 14:27:43 +00:00
|
|
|
g_autoptr(GKeyFile) config = NULL;
|
|
|
|
g_autoptr(GBytes) gpg_data = NULL;
|
2014-12-17 13:05:44 +00:00
|
|
|
|
2015-09-23 12:52:56 +00:00
|
|
|
context = g_option_context_new ("NAME LOCATION - Add a remote repository");
|
2014-12-17 13:05:44 +00:00
|
|
|
|
2015-09-17 09:37:29 +00:00
|
|
|
g_option_context_add_main_entries (context, common_options, NULL);
|
|
|
|
|
2016-05-06 14:37:47 +00:00
|
|
|
if (!flatpak_option_context_parse (context, add_options, &argc, &argv, 0, &dir, cancellable, error))
|
2015-09-17 09:37:29 +00:00
|
|
|
return FALSE;
|
2014-12-17 13:05:44 +00:00
|
|
|
|
|
|
|
if (argc < 3)
|
2015-09-28 14:40:19 +00:00
|
|
|
return usage_error (context, "NAME and LOCATION must be specified", error);
|
2014-12-17 13:05:44 +00:00
|
|
|
|
|
|
|
remote_name = argv[1];
|
2015-09-23 12:52:56 +00:00
|
|
|
url_or_path = argv[2];
|
|
|
|
|
2016-05-11 14:27:43 +00:00
|
|
|
remotes = flatpak_dir_list_remotes (dir, cancellable, error);
|
|
|
|
if (remotes == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (g_strv_contains ((const char **)remotes, remote_name))
|
|
|
|
{
|
|
|
|
if (opt_if_not_exists)
|
|
|
|
return TRUE; /* Do nothing */
|
|
|
|
|
|
|
|
return flatpak_fail (error, "Remote %s already exists", remote_name);
|
|
|
|
}
|
|
|
|
|
2015-09-23 12:52:56 +00:00
|
|
|
file = g_file_new_for_commandline_arg (url_or_path);
|
|
|
|
if (g_file_is_native (file))
|
|
|
|
remote_url = g_file_get_uri (file);
|
|
|
|
else
|
|
|
|
remote_url = g_strdup (url_or_path);
|
2014-12-17 13:05:44 +00:00
|
|
|
|
2016-05-11 14:27:43 +00:00
|
|
|
/* Default to gpg verify */
|
|
|
|
if (!opt_no_gpg_verify)
|
|
|
|
opt_do_gpg_verify = TRUE;
|
2015-02-09 16:18:10 +00:00
|
|
|
|
2016-05-11 14:27:43 +00:00
|
|
|
opt_url = remote_url;
|
2015-09-17 09:37:29 +00:00
|
|
|
|
2016-05-11 14:27:43 +00:00
|
|
|
if (opt_title == NULL)
|
2015-12-04 10:09:00 +00:00
|
|
|
{
|
2016-05-06 14:37:47 +00:00
|
|
|
title = flatpak_dir_fetch_remote_title (dir,
|
2015-12-04 10:09:00 +00:00
|
|
|
remote_name,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
if (title)
|
2016-05-11 14:27:43 +00:00
|
|
|
opt_title = title;
|
2015-12-04 10:09:00 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 14:27:43 +00:00
|
|
|
config = get_config_from_opts (dir, remote_name);
|
2015-09-17 13:27:49 +00:00
|
|
|
|
2016-05-11 14:27:43 +00:00
|
|
|
if (opt_gpg_import != NULL)
|
|
|
|
{
|
|
|
|
gpg_data = load_keys (cancellable, error);
|
|
|
|
if (gpg_data == NULL)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-12-21 10:12:25 +00:00
|
|
|
|
2016-05-13 11:59:48 +00:00
|
|
|
return flatpak_dir_modify_remote (dir, remote_name, config, gpg_data, cancellable, error);
|
2015-09-17 09:37:29 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 11:06:00 +00:00
|
|
|
gboolean
|
|
|
|
flatpak_complete_add_remote (FlatpakCompletion *completion)
|
|
|
|
{
|
|
|
|
g_autoptr(GOptionContext) context = NULL;
|
|
|
|
g_autoptr(FlatpakDir) dir = NULL;
|
|
|
|
|
|
|
|
context = g_option_context_new ("");
|
|
|
|
g_option_context_add_main_entries (context, common_options, NULL);
|
|
|
|
if (!flatpak_option_context_parse (context, add_options, &completion->argc, &completion->argv, 0, &dir, NULL, NULL))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
switch (completion->argc)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
flatpak_complete_options (completion, global_entries);
|
|
|
|
flatpak_complete_options (completion, common_options);
|
|
|
|
flatpak_complete_options (completion, add_options);
|
|
|
|
flatpak_complete_options (completion, user_entries);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-09-17 09:37:29 +00:00
|
|
|
gboolean
|
2016-05-06 14:37:47 +00:00
|
|
|
flatpak_builtin_modify_remote (int argc, char **argv, GCancellable *cancellable, GError **error)
|
2015-09-17 09:37:29 +00:00
|
|
|
{
|
|
|
|
g_autoptr(GOptionContext) context = NULL;
|
2016-05-06 14:37:47 +00:00
|
|
|
g_autoptr(FlatpakDir) dir = NULL;
|
2015-09-17 09:37:29 +00:00
|
|
|
g_autoptr(GKeyFile) config = NULL;
|
2016-05-11 13:43:40 +00:00
|
|
|
g_autoptr(GBytes) gpg_data = NULL;
|
2015-09-17 09:37:29 +00:00
|
|
|
const char *remote_name;
|
|
|
|
|
|
|
|
context = g_option_context_new ("NAME - Modify a remote repository");
|
|
|
|
|
|
|
|
g_option_context_add_main_entries (context, common_options, NULL);
|
|
|
|
|
2016-05-06 14:37:47 +00:00
|
|
|
if (!flatpak_option_context_parse (context, modify_options, &argc, &argv, 0, &dir, cancellable, error))
|
2015-09-17 09:37:29 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (argc < 2)
|
2015-09-28 14:40:19 +00:00
|
|
|
return usage_error (context, "remote NAME must be specified", error);
|
2015-09-17 09:37:29 +00:00
|
|
|
|
|
|
|
remote_name = argv[1];
|
|
|
|
|
2016-05-06 14:37:47 +00:00
|
|
|
if (!ostree_repo_remote_get_url (flatpak_dir_get_repo (dir), remote_name, NULL, NULL))
|
|
|
|
return flatpak_fail (error, "No remote %s", remote_name);
|
2015-09-17 09:37:29 +00:00
|
|
|
|
2016-05-11 14:27:43 +00:00
|
|
|
config = get_config_from_opts (dir, remote_name);
|
2015-12-21 09:25:41 +00:00
|
|
|
|
2016-05-11 13:43:40 +00:00
|
|
|
if (opt_gpg_import != NULL)
|
|
|
|
{
|
|
|
|
gpg_data = load_keys (cancellable, error);
|
|
|
|
if (gpg_data == NULL)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-12-21 10:12:25 +00:00
|
|
|
|
2016-05-13 11:59:48 +00:00
|
|
|
return flatpak_dir_modify_remote (dir, remote_name, config, gpg_data, cancellable, error);
|
2014-12-17 13:05:44 +00:00
|
|
|
}
|
2016-05-30 11:06:00 +00:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
flatpak_complete_modify_remote (FlatpakCompletion *completion)
|
|
|
|
{
|
|
|
|
g_autoptr(GOptionContext) context = NULL;
|
|
|
|
g_autoptr(FlatpakDir) dir = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
context = g_option_context_new ("");
|
|
|
|
g_option_context_add_main_entries (context, common_options, NULL);
|
|
|
|
if (!flatpak_option_context_parse (context, modify_options, &completion->argc, &completion->argv, 0, &dir, NULL, NULL))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
switch (completion->argc)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case 1: /* REMOTE */
|
|
|
|
flatpak_complete_options (completion, global_entries);
|
|
|
|
flatpak_complete_options (completion, common_options);
|
|
|
|
flatpak_complete_options (completion, modify_options);
|
|
|
|
flatpak_complete_options (completion, user_entries);
|
|
|
|
|
|
|
|
{
|
|
|
|
g_auto(GStrv) remotes = flatpak_dir_list_remotes (dir, NULL, NULL);
|
|
|
|
if (remotes == NULL)
|
|
|
|
return FALSE;
|
|
|
|
for (i = 0; remotes[i] != NULL; i++)
|
|
|
|
flatpak_complete_word (completion, "%s ", remotes[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|