summaryrefslogtreecommitdiff
path: root/src/ostree/ot-builtin-gpg-sign.c
blob: bde9180a61b4daca45916b38ff8c1d36ccc0889b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * Copyright (C) 2015 Colin Walters <walters@verbum.org>
 *
 * SPDX-License-Identifier: LGPL-2.0+
 *
 * This library 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Colin Walters <walters@verbum.org>
 */

#include "config.h"

#include "ot-main.h"
#include "ot-builtins.h"
#include "ostree.h"
#include "otutil.h"
#include "ostree-core-private.h"

static gboolean opt_delete;
static char *opt_gpg_homedir;

/* ATTENTION:
 * Please remember to update the bash-completion script (bash/ostree) and
 * man page (man/ostree-gpg-sign.xml) when changing the option list.
 */

static GOptionEntry options[] = {
  { "delete", 'd', 0, G_OPTION_ARG_NONE, &opt_delete, "Delete signatures having any of the GPG KEY-IDs" },
  { "gpg-homedir", 0, 0, G_OPTION_ARG_FILENAME, &opt_gpg_homedir, "GPG Homedir to use when looking for keyrings", "HOMEDIR" },
  { NULL }
};

static void
usage_error (GOptionContext *context, const char *message, GError **error)
{
  g_autofree char *help = g_option_context_get_help (context, TRUE, NULL);
  g_printerr ("%s", help);
  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, message);
}

static gboolean
delete_signatures (OstreeRepo *repo,
                   const char *commit_checksum,
                   const char * const *key_ids,
                   guint n_key_ids,
                   guint *out_n_deleted,
                   GCancellable *cancellable,
                   GError **error)
{
  GVariantDict metadata_dict;
  g_autoptr(OstreeGpgVerifyResult) result = NULL;
  g_autoptr(GVariant) old_metadata = NULL;
  g_autoptr(GVariant) new_metadata = NULL;
  g_autoptr(GVariant) signature_data = NULL;
  GVariantIter iter;
  GVariant *child;
  GQueue signatures = G_QUEUE_INIT;
  GQueue trash = G_QUEUE_INIT;
  guint n_deleted = 0;
  guint ii;
  gboolean ret = FALSE;
  GError *local_error = NULL;

  /* XXX Should this code be a new OstreeRepo function in libostree?
   *     Feels slightly too low-level here, and I have to know about
   *     the metadata key name and format which are both declared in
   *     ostree-core-private.h.
   *
   *     OTOH, would this really be a useful addition to libostree?
   */

  if (!ostree_repo_read_commit_detached_metadata (repo,
                                                  commit_checksum,
                                                  &old_metadata,
                                                  cancellable,
                                                  error))
    goto out;

  g_variant_dict_init (&metadata_dict, old_metadata);

  signature_data = g_variant_dict_lookup_value (&metadata_dict,
                                                _OSTREE_METADATA_GPGSIGS_NAME,
                                                G_VARIANT_TYPE ("aay"));

  /* Taking the approach of deleting whatever matches we find for the
   * provided key IDs, even if we don't find a match for EVERY key ID.
   * So no signatures means no matches, which is okay... I guess. */
  if (signature_data == NULL)
    {
      g_variant_dict_clear (&metadata_dict);
      goto shortcut;
    }

  /* Parse the signatures on this commit by running a verify operation
   * on it.  Use the result to match key IDs to signatures for deletion.
   *
   * XXX Reading detached metadata from disk twice here.  Another reason
   *     to move this into libostree?
   */
  result = ostree_repo_verify_commit_ext (repo, commit_checksum,
                                          NULL, NULL,
                                          cancellable, &local_error);
  if (result == NULL)
    {
      g_variant_dict_clear (&metadata_dict);
      goto out;
    }

  /* Convert the GVariant array to a GQueue. */
  g_variant_iter_init (&iter, signature_data);
  while ((child = g_variant_iter_next_value (&iter)) != NULL)
    {
      /* Takes ownership of the child. */
      g_queue_push_tail (&signatures, child);
    }

  /* Signature count and ordering of signatures in the GQueue and
   * OstreeGpgVerifyResult must agree.  We use this below to mark
   * items in the GQueue for deletion based on the index returned
   * by ostree_gpg_verify_result_lookup(). */
  g_assert_cmpuint (ostree_gpg_verify_result_count_all (result), ==, signatures.length);

  /* Build a trash queue which points at nodes in the signature queue. */
  for (ii = 0; ii < n_key_ids; ii++)
    {
      guint index;

      if (ostree_gpg_verify_result_lookup (result, key_ids[ii], &index))
        {
          GList *link = g_queue_peek_nth_link (&signatures, index);

          /* Avoid duplicates in the trash queue. */
          if (g_queue_find (&trash, link) == NULL)
            g_queue_push_tail (&trash, link);
        }
    }

  n_deleted = trash.length;

  /* Reduce the signature queue by emptying the trash. */
  while (!g_queue_is_empty (&trash))
    {
      GList *link = g_queue_pop_head (&trash);
      g_variant_unref (link->data);
      g_queue_delete_link (&signatures, link);
    }

  /* Update the metadata dictionary. */
  if (g_queue_is_empty (&signatures))
    {
      g_variant_dict_remove (&metadata_dict, _OSTREE_METADATA_GPGSIGS_NAME);
    }
  else
    {
      GVariantBuilder signature_builder;

      g_variant_builder_init (&signature_builder, G_VARIANT_TYPE ("aay"));

      while (!g_queue_is_empty (&signatures))
        {
          g_autoptr(GVariant) sigchild = g_queue_pop_head (&signatures);
          g_variant_builder_add_value (&signature_builder, sigchild);
        }

      g_variant_dict_insert_value (&metadata_dict,
                                   _OSTREE_METADATA_GPGSIGS_NAME,
                                   g_variant_builder_end (&signature_builder));
    }

  /* Commit the new metadata. */
  new_metadata = g_variant_dict_end (&metadata_dict);
  if (!ostree_repo_write_commit_detached_metadata (repo,
                                                   commit_checksum,
                                                   new_metadata,
                                                   cancellable,
                                                   error))
    goto out;

shortcut:

  if (out_n_deleted != NULL)
    *out_n_deleted = n_deleted;

  ret = TRUE;

out:
  return ret;
}

gboolean
ostree_builtin_gpg_sign (int argc, char **argv,OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
{
  g_autoptr(GOptionContext) context = NULL;
  g_autoptr(OstreeRepo) repo = NULL;
  g_autofree char *resolved_commit = NULL;
  const char *commit;
  char **key_ids;
  int n_key_ids, ii;
  gboolean ret = FALSE;

  context = g_option_context_new ("COMMIT KEY-ID...");

  if (!ostree_option_context_parse (context, options, &argc, &argv, invocation, &repo, cancellable, error))
    goto out;

  if (argc < 2)
    {
      usage_error (context, "Need a COMMIT to sign", error);
      goto out;
    }

  if (argc < 3)
    {
      usage_error (context, "Need at least one GPG KEY-ID to sign with", error);
      goto out;
    }

  commit = argv[1];
  key_ids = argv + 2;
  n_key_ids = argc - 2;

  if (!ostree_repo_resolve_rev (repo, commit, FALSE, &resolved_commit, error))
    goto out;

  if (opt_delete)
    {
      guint n_deleted = 0;

      if (delete_signatures (repo, resolved_commit,
                             (const char * const *) key_ids, n_key_ids,
                             &n_deleted, cancellable, error))
        {
          g_print ("Signatures deleted: %u\n", n_deleted);
          ret = TRUE;
        }

      goto out;
    }

  for (ii = 0; ii < n_key_ids; ii++)
    {
      if (!ostree_repo_sign_commit (repo, resolved_commit, key_ids[ii],
                                    opt_gpg_homedir, cancellable, error))
        goto out;
    }

  ret = TRUE;

out:
  return ret;
}