/
flatpak-oci-registry.c
3383 lines (2789 loc) · 103 KB
/
flatpak-oci-registry.c
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set et sw=2 sts=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e-s:
* Copyright © 2016 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.1 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:https://www.gnu.org/licenses/>.
*
* Authors:
* Alexander Larsson <[email protected]>
*/
#include "config.h"
#include <glib/gi18n-lib.h>
#include <gio/gunixoutputstream.h>
#include <gio/gunixinputstream.h>
#include "libglnx.h"
#include <gpgme.h>
#include "flatpak-oci-registry-private.h"
#include "flatpak-utils-base-private.h"
#include "flatpak-utils-private.h"
#include "flatpak-uri-private.h"
#include "flatpak-dir-private.h"
#include "flatpak-xml-utils-private.h"
#include "flatpak-zstd-decompressor-private.h"
#define MAX_JSON_SIZE (1024 * 1024)
typedef struct archive FlatpakAutoArchiveWrite;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (FlatpakAutoArchiveWrite, archive_write_free)
static void flatpak_oci_registry_initable_iface_init (GInitableIface *iface);
/* A FlatpakOciRegistry represents either:
*
* A local directory with a layout corresponding to the OCI image specification -
* we usually use this to store a single image, but it could be used for multiple
* images.
* A remote docker registry.
*
* This code used to support OCI image layouts on remote HTTP servers, but that's not
* really a thing anybody does. It would be inefficient for storing large numbers of
* images, since all versions need to be listed in index.json.
*/
struct FlatpakOciRegistry
{
GObject parent;
gboolean for_write;
gboolean valid;
gboolean is_docker;
char *uri;
int tmp_dfd;
char *token;
/* Local repos */
int dfd;
/* Remote repos */
FlatpakHttpSession *http_session;
GUri *base_uri;
};
typedef struct
{
GObjectClass parent_class;
} FlatpakOciRegistryClass;
enum {
PROP_0,
PROP_URI,
PROP_FOR_WRITE,
PROP_TMP_DFD,
};
G_DEFINE_TYPE_WITH_CODE (FlatpakOciRegistry, flatpak_oci_registry, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
flatpak_oci_registry_initable_iface_init))
static gchar *
parse_relative_uri (GUri *base_uri,
const char *subpath,
GError **error)
{
g_autoptr(GUri) uri = NULL;
uri = g_uri_parse_relative (base_uri, subpath, FLATPAK_HTTP_URI_FLAGS | G_URI_FLAGS_PARSE_RELAXED, error);
if (uri == NULL)
return NULL;
return g_uri_to_string_partial (uri, G_URI_HIDE_PASSWORD);
}
static void
flatpak_oci_registry_finalize (GObject *object)
{
FlatpakOciRegistry *self = FLATPAK_OCI_REGISTRY (object);
if (self->dfd != -1)
close (self->dfd);
g_clear_pointer (&self->http_session, flatpak_http_session_free);
g_clear_pointer (&self->base_uri, g_uri_unref);
g_free (self->uri);
g_free (self->token);
G_OBJECT_CLASS (flatpak_oci_registry_parent_class)->finalize (object);
}
static void
flatpak_oci_registry_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
FlatpakOciRegistry *self = FLATPAK_OCI_REGISTRY (object);
const char *uri;
switch (prop_id)
{
case PROP_URI:
/* Ensure the base uri ends with a / so relative urls work */
uri = g_value_get_string (value);
if (g_str_has_suffix (uri, "/"))
self->uri = g_strdup (uri);
else
self->uri = g_strconcat (uri, "/", NULL);
break;
case PROP_FOR_WRITE:
self->for_write = g_value_get_boolean (value);
break;
case PROP_TMP_DFD:
self->tmp_dfd = g_value_get_int (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
flatpak_oci_registry_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
FlatpakOciRegistry *self = FLATPAK_OCI_REGISTRY (object);
switch (prop_id)
{
case PROP_URI:
g_value_set_string (value, self->uri);
break;
case PROP_FOR_WRITE:
g_value_set_boolean (value, self->for_write);
break;
case PROP_TMP_DFD:
g_value_set_int (value, self->tmp_dfd);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
flatpak_oci_registry_class_init (FlatpakOciRegistryClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = flatpak_oci_registry_finalize;
object_class->get_property = flatpak_oci_registry_get_property;
object_class->set_property = flatpak_oci_registry_set_property;
g_object_class_install_property (object_class,
PROP_URI,
g_param_spec_string ("uri",
"",
"",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_TMP_DFD,
g_param_spec_int ("tmp-dfd",
"",
"",
-1, G_MAXINT, -1,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_FOR_WRITE,
g_param_spec_boolean ("for-write",
"",
"",
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}
static void
flatpak_oci_registry_init (FlatpakOciRegistry *self)
{
self->dfd = -1;
self->tmp_dfd = -1;
}
gboolean
flatpak_oci_registry_is_local (FlatpakOciRegistry *self)
{
return self->dfd != -1;
}
const char *
flatpak_oci_registry_get_uri (FlatpakOciRegistry *self)
{
return self->uri;
}
void
flatpak_oci_registry_set_token (FlatpakOciRegistry *self,
const char *token)
{
g_free (self->token);
self->token = g_strdup (token);
if (self->token)
(void)glnx_file_replace_contents_at (self->dfd, ".token",
(guchar *)self->token,
strlen (self->token),
0, NULL, NULL);
}
FlatpakOciRegistry *
flatpak_oci_registry_new (const char *uri,
gboolean for_write,
int tmp_dfd,
GCancellable *cancellable,
GError **error)
{
FlatpakOciRegistry *oci_registry;
oci_registry = g_initable_new (FLATPAK_TYPE_OCI_REGISTRY,
cancellable, error,
"uri", uri,
"for-write", for_write,
"tmp-dfd", tmp_dfd,
NULL);
return oci_registry;
}
static int
local_open_file (int dfd,
const char *subpath,
struct stat *st_buf,
GCancellable *cancellable,
GError **error)
{
glnx_autofd int fd = -1;
struct stat tmp_st_buf;
do
fd = openat (dfd, subpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC | O_NOCTTY);
while (G_UNLIKELY (fd == -1 && errno == EINTR));
if (fd == -1)
{
glnx_set_error_from_errno (error);
return -1;
}
if (st_buf == NULL)
st_buf = &tmp_st_buf;
if (fstat (fd, st_buf) != 0)
{
glnx_set_error_from_errno (error);
return -1;
}
if (!S_ISREG (st_buf->st_mode))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"Non-regular file in OCI registry at %s", subpath);
return -1;
}
return g_steal_fd (&fd);
}
static GBytes *
local_load_file (int dfd,
const char *subpath,
GCancellable *cancellable,
GError **error)
{
glnx_autofd int fd = -1;
struct stat st_buf;
GBytes *bytes;
fd = local_open_file (dfd, subpath, &st_buf, cancellable, error);
if (fd == -1)
return NULL;
bytes = glnx_fd_readall_bytes (fd, cancellable, error);
if (bytes == NULL)
return NULL;
return bytes;
}
/* We just support the first http uri for now */
static char *
choose_alt_uri (GUri *base_uri,
const char **alt_uris)
{
int i;
if (alt_uris == NULL)
return NULL;
for (i = 0; alt_uris[i] != NULL; i++)
{
const char *alt_uri = alt_uris[i];
if (g_str_has_prefix (alt_uri, "http:") || g_str_has_prefix (alt_uri, "https:"))
return g_strdup (alt_uri);
}
return NULL;
}
static GBytes *
remote_load_file (FlatpakHttpSession *http_session,
GUri *base,
const char *subpath,
const char **alt_uris,
const char *token,
char **out_content_type,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GBytes) bytes = NULL;
g_autofree char *uri_s = NULL;
uri_s = choose_alt_uri (base, alt_uris);
if (uri_s == NULL)
{
uri_s = parse_relative_uri (base, subpath, error);
if (uri_s == NULL)
return NULL;
}
bytes = flatpak_load_uri (http_session,
uri_s, FLATPAK_HTTP_FLAGS_ACCEPT_OCI,
token,
NULL, NULL, out_content_type,
cancellable, error);
if (bytes == NULL)
return NULL;
return g_steal_pointer (&bytes);
}
static GBytes *
flatpak_oci_registry_load_file (FlatpakOciRegistry *self,
const char *subpath,
const char **alt_uris,
char **out_content_type,
GCancellable *cancellable,
GError **error)
{
if (self->dfd != -1)
return local_load_file (self->dfd, subpath, cancellable, error);
else
return remote_load_file (self->http_session, self->base_uri, subpath, alt_uris, self->token, out_content_type, cancellable, error);
}
static JsonNode *
parse_json (GBytes *bytes, GCancellable *cancellable, GError **error)
{
g_autoptr(JsonParser) parser = NULL;
JsonNode *root = NULL;
parser = json_parser_new ();
if (!json_parser_load_from_data (parser,
g_bytes_get_data (bytes, NULL),
g_bytes_get_size (bytes),
error))
return NULL;
root = json_parser_get_root (parser);
if (root == NULL || !JSON_NODE_HOLDS_OBJECT (root))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, "Invalid json, no root object");
return NULL;
}
return json_node_copy (root);
}
static gboolean
verify_oci_version (GBytes *oci_layout_bytes, gboolean *not_json, GCancellable *cancellable, GError **error)
{
const char *version;
g_autoptr(JsonNode) node = NULL;
JsonObject *oci_layout;
node = parse_json (oci_layout_bytes, cancellable, error);
if (node == NULL)
{
*not_json = TRUE;
return FALSE;
}
*not_json = FALSE;
oci_layout = json_node_get_object (node);
version = json_object_get_string_member (oci_layout, "imageLayoutVersion");
if (version == NULL)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, "Unsupported oci repo: oci-layout version missing");
return FALSE;
}
if (strcmp (version, "1.0.0") != 0)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"Unsupported existing oci-layout version %s (only 1.0.0 supported)", version);
return FALSE;
}
return TRUE;
}
static gboolean
flatpak_oci_registry_ensure_local (FlatpakOciRegistry *self,
gboolean for_write,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GFile) dir = g_file_new_for_uri (self->uri);
glnx_autofd int local_dfd = -1;
int dfd;
g_autoptr(GError) local_error = NULL;
g_autoptr(GBytes) oci_layout_bytes = NULL;
g_autoptr(GBytes) token_bytes = NULL;
gboolean not_json;
if (self->dfd != -1)
dfd = self->dfd;
else
{
if (!glnx_opendirat (AT_FDCWD, flatpak_file_get_path_cached (dir),
TRUE, &local_dfd, &local_error))
{
if (for_write && g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
{
g_clear_error (&local_error);
if (!glnx_shutil_mkdir_p_at (AT_FDCWD, flatpak_file_get_path_cached (dir), 0755, cancellable, error))
return FALSE;
if (!glnx_opendirat (AT_FDCWD, flatpak_file_get_path_cached (dir),
TRUE, &local_dfd, error))
return FALSE;
}
else
{
g_propagate_error (error, g_steal_pointer (&local_error));
return FALSE;
}
}
dfd = local_dfd;
}
if (for_write)
{
if (!glnx_shutil_mkdir_p_at (dfd, "blobs/sha256", 0755, cancellable, error))
return FALSE;
}
oci_layout_bytes = local_load_file (dfd, "oci-layout", cancellable, &local_error);
if (oci_layout_bytes == NULL)
{
if (for_write && g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
{
const char *new_layout_data = "{\"imageLayoutVersion\": \"1.0.0\"}";
g_clear_error (&local_error);
if (!glnx_file_replace_contents_at (dfd, "oci-layout",
(const guchar *) new_layout_data,
strlen (new_layout_data),
0,
cancellable, error))
return FALSE;
}
else
{
g_propagate_error (error, g_steal_pointer (&local_error));
return FALSE;
}
}
else if (!verify_oci_version (oci_layout_bytes, ¬_json, cancellable, error))
return FALSE;
if (self->dfd != -1)
{
token_bytes = local_load_file (self->dfd, ".token", cancellable, NULL);
if (token_bytes != NULL)
self->token = g_strndup (g_bytes_get_data (token_bytes, NULL), g_bytes_get_size (token_bytes));
}
if (self->dfd == -1 && local_dfd != -1)
self->dfd = g_steal_fd (&local_dfd);
return TRUE;
}
static gboolean
flatpak_oci_registry_ensure_remote (FlatpakOciRegistry *self,
gboolean for_write,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GUri) baseuri = NULL;
if (for_write)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"Writes are not supported for remote OCI registries");
return FALSE;
}
self->http_session = flatpak_create_http_session (PACKAGE_STRING);
baseuri = g_uri_parse (self->uri, FLATPAK_HTTP_URI_FLAGS | G_URI_FLAGS_PARSE_RELAXED, NULL);
if (baseuri == NULL)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
"Invalid url %s", self->uri);
return FALSE;
}
self->is_docker = TRUE;
self->base_uri = g_steal_pointer (&baseuri);
return TRUE;
}
static gboolean
flatpak_oci_registry_initable_init (GInitable *initable,
GCancellable *cancellable,
GError **error)
{
FlatpakOciRegistry *self = FLATPAK_OCI_REGISTRY (initable);
gboolean res;
if (self->tmp_dfd == -1 &&
!glnx_opendirat (AT_FDCWD, "/var/tmp", TRUE, &self->tmp_dfd, error))
return FALSE;
if (g_str_has_prefix (self->uri, "file:/"))
res = flatpak_oci_registry_ensure_local (self, self->for_write, cancellable, error);
else
res = flatpak_oci_registry_ensure_remote (self, self->for_write, cancellable, error);
if (!res)
return FALSE;
self->valid = TRUE;
return TRUE;
}
static void
flatpak_oci_registry_initable_iface_init (GInitableIface *iface)
{
iface->init = flatpak_oci_registry_initable_init;
}
FlatpakOciIndex *
flatpak_oci_registry_load_index (FlatpakOciRegistry *self,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GBytes) bytes = NULL;
g_autoptr(GError) local_error = NULL;
g_assert (self->valid);
bytes = flatpak_oci_registry_load_file (self, "index.json", NULL, NULL, cancellable, &local_error);
if (bytes == NULL)
{
g_propagate_error (error, g_steal_pointer (&local_error));
return NULL;
}
return (FlatpakOciIndex *) flatpak_json_from_bytes (bytes, FLATPAK_TYPE_OCI_INDEX, error);
}
gboolean
flatpak_oci_registry_save_index (FlatpakOciRegistry *self,
FlatpakOciIndex *index,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GBytes) bytes = NULL;
g_assert (self->valid);
bytes = flatpak_json_to_bytes (FLATPAK_JSON (index));
if (!glnx_file_replace_contents_at (self->dfd, "index.json",
g_bytes_get_data (bytes, NULL),
g_bytes_get_size (bytes),
0, cancellable, error))
return FALSE;
return TRUE;
}
static gboolean
write_update_checksum (GOutputStream *out,
gconstpointer data,
gsize len,
gsize *out_bytes_written,
GChecksum *checksum,
GCancellable *cancellable,
GError **error)
{
if (out)
{
if (!g_output_stream_write_all (out, data, len, out_bytes_written,
cancellable, error))
return FALSE;
}
else if (out_bytes_written)
{
*out_bytes_written = len;
}
if (checksum)
g_checksum_update (checksum, data, len);
return TRUE;
}
static gboolean
splice_update_checksum (GOutputStream *out,
GInputStream *in,
GChecksum *checksum,
GCancellable *cancellable,
GError **error)
{
g_return_val_if_fail (out != NULL || checksum != NULL, FALSE);
if (checksum != NULL)
{
gsize bytes_read, bytes_written;
char buf[4096];
do
{
if (!g_input_stream_read_all (in, buf, sizeof (buf), &bytes_read, cancellable, error))
return FALSE;
if (!write_update_checksum (out, buf, bytes_read, &bytes_written, checksum,
cancellable, error))
return FALSE;
}
while (bytes_read > 0);
}
else if (out != NULL)
{
if (g_output_stream_splice (out, in, 0, cancellable, error) < 0)
return FALSE;
}
return TRUE;
}
static char *
get_digest_subpath (FlatpakOciRegistry *self,
const char *repository,
gboolean is_manifest,
gboolean allow_tag,
const char *digest,
GError **error)
{
g_autoptr(GString) s = g_string_new ("");
if (!g_str_has_prefix (digest, "sha256:"))
{
if (!allow_tag)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"Unsupported digest type %s", digest);
return NULL;
}
if (!self->is_docker)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"Tags not supported for local oci dirs");
return NULL;
}
}
if (self->is_docker)
g_string_append (s, "v2/");
if (repository)
{
g_string_append (s, repository);
g_string_append (s, "/");
}
if (self->is_docker)
{
if (is_manifest)
g_string_append (s, "manifests/");
else
g_string_append (s, "blobs/");
g_string_append (s, digest);
}
else
{
/* As per above checks this is guaranteed to be a digest */
g_string_append (s, "blobs/sha256/");
g_string_append (s, digest + strlen ("sha256:"));
}
return g_string_free (g_steal_pointer (&s), FALSE);
}
static char *
checksum_fd (int fd, GCancellable *cancellable, GError **error)
{
g_autoptr(GChecksum) checksum = NULL;
g_autoptr(GInputStream) in = g_unix_input_stream_new (fd, FALSE);
checksum = g_checksum_new (G_CHECKSUM_SHA256);
if (!splice_update_checksum (NULL, in, checksum, cancellable, error))
return NULL;
return g_strdup (g_checksum_get_string (checksum));
}
int
flatpak_oci_registry_download_blob (FlatpakOciRegistry *self,
const char *repository,
gboolean manifest,
const char *digest,
const char **alt_uris,
FlatpakLoadUriProgress progress_cb,
gpointer user_data,
GCancellable *cancellable,
GError **error)
{
g_autofree char *subpath = NULL;
glnx_autofd int fd = -1;
g_assert (self->valid);
subpath = get_digest_subpath (self, repository, manifest, FALSE, digest, error);
if (subpath == NULL)
return -1;
if (self->dfd != -1)
{
/* Local case, trust checksum */
fd = local_open_file (self->dfd, subpath, NULL, cancellable, error);
if (fd == -1)
return -1;
}
else
{
g_autofree char *uri_s = NULL;
g_autofree char *checksum = NULL;
g_autofree char *tmpfile_name = g_strdup_printf ("oci-layer-XXXXXX");
g_autoptr(GOutputStream) out_stream = NULL;
/* remote case, download and verify */
uri_s = choose_alt_uri (self->base_uri, alt_uris);
if (uri_s == NULL)
{
uri_s = parse_relative_uri (self->base_uri, subpath, error);
if (uri_s == NULL)
return -1;
}
if (!flatpak_open_in_tmpdir_at (self->tmp_dfd, 0600, tmpfile_name,
&out_stream, cancellable, error))
return -1;
fd = local_open_file (self->tmp_dfd, tmpfile_name, NULL, cancellable, error);
(void) unlinkat (self->tmp_dfd, tmpfile_name, 0);
if (fd == -1)
return -1;
if (!flatpak_download_http_uri (self->http_session, uri_s,
FLATPAK_HTTP_FLAGS_ACCEPT_OCI,
out_stream,
self->token,
progress_cb, user_data,
cancellable, error))
return -1;
if (!g_output_stream_close (out_stream, cancellable, error))
return -1;
checksum = checksum_fd (fd, cancellable, error);
if (checksum == NULL)
return -1;
if (strcmp (checksum, digest + strlen ("sha256:")) != 0)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Checksum digest did not match (%s != %s)", digest, checksum);
return -1;
}
lseek (fd, 0, SEEK_SET);
}
return g_steal_fd (&fd);
}
gboolean
flatpak_oci_registry_mirror_blob (FlatpakOciRegistry *self,
FlatpakOciRegistry *source_registry,
const char *repository,
gboolean manifest,
const char *digest,
const char **alt_uris,
FlatpakLoadUriProgress progress_cb,
gpointer user_data,
GCancellable *cancellable,
GError **error)
{
g_autofree char *src_subpath = NULL;
g_autofree char *dst_subpath = NULL;
g_auto(GLnxTmpfile) tmpf = { 0 };
g_autoptr(GOutputStream) out_stream = NULL;
struct stat stbuf;
g_autofree char *checksum = NULL;
g_assert (self->valid);
if (!self->for_write)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"Write not supported to registry");
return FALSE;
}
src_subpath = get_digest_subpath (source_registry, repository, manifest, FALSE, digest, error);
if (src_subpath == NULL)
return FALSE;
dst_subpath = get_digest_subpath (self, NULL, manifest, FALSE, digest, error);
if (dst_subpath == NULL)
return FALSE;
/* Check if its already available */
if (fstatat (self->dfd, dst_subpath, &stbuf, AT_SYMLINK_) == 0)
return TRUE;
if (!glnx_open_tmpfile_linkable_at (self->dfd, "blobs/sha256",
O_RDWR | O_CLOEXEC | O_NOCTTY,
&tmpf, error))
return FALSE;
if (source_registry->dfd != -1)
{
glnx_autofd int src_fd = -1;
src_fd = local_open_file (source_registry->dfd, src_subpath, NULL, cancellable, error);
if (src_fd == -1)
return FALSE;
if (glnx_regfile_copy_bytes (src_fd, tmpf.fd, (off_t) -1) < 0)
return glnx_throw_errno_prefix (error, "copyfile");
}
else
{
g_autofree char *uri_s = parse_relative_uri (source_registry->base_uri, src_subpath, error);
if (uri_s == NULL)
return FALSE;
out_stream = g_unix_output_stream_new (tmpf.fd, FALSE);
if (!flatpak_download_http_uri (source_registry->http_session, uri_s,
FLATPAK_HTTP_FLAGS_ACCEPT_OCI, out_stream,
self->token,
progress_cb, user_data,
cancellable, error))
return FALSE;
if (!g_output_stream_close (out_stream, cancellable, error))
return FALSE;
}
lseek (tmpf.fd, 0, SEEK_SET);
checksum = checksum_fd (tmpf.fd, cancellable, error);
if (checksum == NULL)
return FALSE;
if (strcmp (checksum, digest + strlen ("sha256:")) != 0)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Checksum digest did not match (%s != %s)", digest, checksum);
return FALSE;
}
if (!glnx_link_tmpfile_at (&tmpf,
GLNX_LINK_TMPFILE_NOREPLACE_IGNORE_EXIST,
self->dfd, dst_subpath,
error))
return FALSE;
return TRUE;
}
static const char *
object_get_string_member_with_default (JsonNode *json,
const char *member_name,
const char *default_value)
{
JsonNode *node;
if (json == NULL || !JSON_NODE_HOLDS_OBJECT(json))
return default_value;
node = json_object_get_member (json_node_get_object (json), member_name);
if (node == NULL || JSON_NODE_HOLDS_NULL (node) || JSON_NODE_TYPE (node) != JSON_NODE_VALUE)
return default_value;
return json_node_get_string (node);
}
static const char *
object_find_error_string (JsonNode *json)
{
const char *error_detail = NULL;
error_detail = object_get_string_member_with_default (json, "details", NULL);
if (error_detail == NULL)
error_detail = object_get_string_member_with_default (json, "message", NULL);
if (error_detail == NULL)
error_detail = object_get_string_member_with_default (json, "error", NULL);
return error_detail;
}
static char *
get_token_for_www_auth (FlatpakOciRegistry *self,
const char *repository,
const char *www_authenticate,
const char *auth,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GHashTable) params = NULL;
g_autoptr(GString) args = NULL;
const char *realm, *service, *scope, *token, *body_data;
g_autofree char *default_scope = NULL;
g_autoptr(GUri) auth_uri = NULL;
g_autofree char *auth_uri_s = NULL;
g_autoptr(GBytes) body = NULL;
g_autoptr(JsonNode) json = NULL;
GUri *tmp_uri;
int http_status;
if (g_ascii_strncasecmp (www_authenticate, "Bearer ", strlen ("Bearer ")) != 0)
{
flatpak_fail (error, _("Only Bearer authentication supported"));
return NULL;
}
params = flatpak_parse_http_header_param_list (www_authenticate + strlen ("Bearer "));