-
Notifications
You must be signed in to change notification settings - Fork 416
/
fwupd-remote.c
1822 lines (1676 loc) · 48.5 KB
/
fwupd-remote.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
/*
* Copyright (C) 2017 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1+
*/
#include "config.h"
#ifdef HAVE_LIBCURL
#include <curl/curl.h>
#endif
#include <jcat.h>
#include "fwupd-common-private.h"
#include "fwupd-enums-private.h"
#include "fwupd-error.h"
#include "fwupd-remote-private.h"
/**
* FwupdRemote:
*
* A source of metadata that provides firmware.
*
* Remotes can be local (e.g. folders on a disk) or remote (e.g. downloaded
* over HTTP or IPFS).
*
* See also: [class@FwupdClient]
*/
static void
fwupd_remote_finalize(GObject *obj);
typedef struct {
FwupdRemoteKind kind;
FwupdKeyringKind keyring_kind;
gchar *id;
gchar *firmware_base_uri;
gchar *report_uri;
gchar *security_report_uri;
gchar *metadata_uri;
gchar *metadata_uri_sig;
gchar *username;
gchar *password;
gchar *title;
gchar *agreement;
gchar *checksum;
gchar *filename_cache;
gchar *filename_cache_sig;
gchar *filename_source;
gboolean enabled;
gboolean approval_required;
gint priority;
guint64 mtime;
gchar **order_after;
gchar **order_before;
gchar *remotes_dir;
gboolean automatic_reports;
gboolean automatic_security_reports;
} FwupdRemotePrivate;
enum {
PROP_0,
PROP_ID,
PROP_ENABLED,
PROP_APPROVAL_REQUIRED,
PROP_AUTOMATIC_REPORTS,
PROP_AUTOMATIC_SECURITY_REPORTS,
PROP_LAST
};
G_DEFINE_TYPE_WITH_PRIVATE(FwupdRemote, fwupd_remote, G_TYPE_OBJECT)
#define GET_PRIVATE(o) (fwupd_remote_get_instance_private(o))
#ifdef HAVE_LIBCURL_7_62_0
typedef gchar curlptr;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(curlptr, curl_free)
G_DEFINE_AUTOPTR_CLEANUP_FUNC(CURLU, curl_url_cleanup)
#endif
/**
* fwupd_remote_to_json:
* @self: a #FwupdRemote
* @builder: a JSON builder
*
* Adds a fwupd remote to a JSON builder
*
* Since: 1.6.2
**/
void
fwupd_remote_to_json(FwupdRemote *self, JsonBuilder *builder)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FWUPD_IS_REMOTE(self));
g_return_if_fail(builder != NULL);
fwupd_common_json_add_string(builder, "Id", priv->id);
if (priv->kind != FWUPD_REMOTE_KIND_UNKNOWN) {
fwupd_common_json_add_string(builder,
"Kind",
fwupd_remote_kind_to_string(priv->kind));
}
if (priv->keyring_kind != FWUPD_KEYRING_KIND_UNKNOWN) {
fwupd_common_json_add_string(builder,
"KeyringKind",
fwupd_keyring_kind_to_string(priv->keyring_kind));
}
fwupd_common_json_add_string(builder, "FirmwareBaseUri", priv->firmware_base_uri);
fwupd_common_json_add_string(builder, "ReportUri", priv->report_uri);
fwupd_common_json_add_string(builder, "SecurityReportUri", priv->security_report_uri);
fwupd_common_json_add_string(builder, "MetadataUri", priv->metadata_uri);
fwupd_common_json_add_string(builder, "MetadataUriSig", priv->metadata_uri_sig);
fwupd_common_json_add_string(builder, "Username", priv->username);
fwupd_common_json_add_string(builder, "Password", priv->password);
fwupd_common_json_add_string(builder, "Title", priv->title);
fwupd_common_json_add_string(builder, "Agreement", priv->agreement);
fwupd_common_json_add_string(builder, "Checksum", priv->checksum);
fwupd_common_json_add_string(builder, "FilenameCache", priv->filename_cache);
fwupd_common_json_add_string(builder, "FilenameCacheSig", priv->filename_cache_sig);
fwupd_common_json_add_string(builder, "FilenameSource", priv->filename_source);
fwupd_common_json_add_boolean(builder, "Enabled", priv->enabled);
fwupd_common_json_add_boolean(builder, "ApprovalRequired", priv->approval_required);
fwupd_common_json_add_boolean(builder, "AutomaticReports", priv->automatic_reports);
fwupd_common_json_add_boolean(builder,
"AutomaticSecurityReports",
priv->automatic_security_reports);
fwupd_common_json_add_int(builder, "Priority", priv->priority);
fwupd_common_json_add_int(builder, "Mtime", priv->mtime);
fwupd_common_json_add_string(builder, "RemotesDir", priv->remotes_dir);
fwupd_common_json_add_stringv(builder, "OrderAfter", priv->order_after);
fwupd_common_json_add_stringv(builder, "OrderBefore", priv->order_before);
}
static gchar *
fwupd_strdup_nonempty(const gchar *text)
{
if (text == NULL || text[0] == '\0')
return NULL;
return g_strdup(text);
}
static void
fwupd_remote_set_username(FwupdRemote *self, const gchar *username)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
/* not changed */
if (g_strcmp0(priv->username, username) == 0)
return;
g_free(priv->username);
priv->username = g_strdup(username);
}
static void
fwupd_remote_set_title(FwupdRemote *self, const gchar *title)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
/* not changed */
if (g_strcmp0(priv->title, title) == 0)
return;
g_free(priv->title);
priv->title = g_strdup(title);
}
/**
* fwupd_remote_set_agreement:
* @self: a #FwupdRemote
* @agreement: (nullable): agreement markup text
*
* Sets the remote agreement in AppStream markup format
*
* Since: 1.0.7
**/
void
fwupd_remote_set_agreement(FwupdRemote *self, const gchar *agreement)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
/* not changed */
if (g_strcmp0(priv->agreement, agreement) == 0)
return;
g_free(priv->agreement);
priv->agreement = g_strdup(agreement);
}
/**
* fwupd_remote_set_checksum:
* @self: a #FwupdRemote
* @checksum: (nullable): checksum string
*
* Sets the remote checksum, typically only useful in the self tests.
*
* Since: 1.8.2
**/
void
fwupd_remote_set_checksum(FwupdRemote *self, const gchar *checksum)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
/* not changed */
if (g_strcmp0(priv->checksum, checksum) == 0)
return;
g_free(priv->checksum);
priv->checksum = g_strdup(checksum);
}
static void
fwupd_remote_set_password(FwupdRemote *self, const gchar *password)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
/* not changed */
if (g_strcmp0(priv->password, password) == 0)
return;
g_free(priv->password);
priv->password = g_strdup(password);
}
static void
fwupd_remote_set_kind(FwupdRemote *self, FwupdRemoteKind kind)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
priv->kind = kind;
}
/**
* fwupd_remote_set_keyring_kind:
* @self: a #FwupdRemote
* @keyring_kind: keyring kind e.g. #FWUPD_KEYRING_KIND_PKCS7
*
* Sets the keyring kind
*
* Since: 1.5.3
**/
void
fwupd_remote_set_keyring_kind(FwupdRemote *self, FwupdKeyringKind keyring_kind)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
priv->keyring_kind = keyring_kind;
}
/* note, this has to be set before url */
static void
fwupd_remote_set_id(FwupdRemote *self, const gchar *id)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
/* not changed */
if (g_strcmp0(priv->id, id) == 0)
return;
g_free(priv->id);
priv->id = g_strdup(id);
g_strdelimit(priv->id, ".", '\0');
}
/**
* fwupd_remote_set_filename_source:
* @self: a #FwupdRemote
* @filename_source: (nullable): filename
*
* Sets the source filename. This is typically a file in `/etc/fwupd/remotes/`.
*
* Since: 1.6.1
**/
void
fwupd_remote_set_filename_source(FwupdRemote *self, const gchar *filename_source)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
if (priv->filename_source == filename_source)
return;
g_free(priv->filename_source);
priv->filename_source = g_strdup(filename_source);
}
static const gchar *
fwupd_remote_get_suffix_for_keyring_kind(FwupdKeyringKind keyring_kind)
{
if (keyring_kind == FWUPD_KEYRING_KIND_JCAT)
return ".jcat";
if (keyring_kind == FWUPD_KEYRING_KIND_GPG)
return ".asc";
if (keyring_kind == FWUPD_KEYRING_KIND_PKCS7)
return ".p7b";
return NULL;
}
static gchar *
fwupd_remote_build_uri(FwupdRemote *self, const gchar *url, GError **error)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
#ifdef HAVE_LIBCURL_7_62_0
g_autoptr(curlptr) tmp_uri = NULL;
g_autoptr(CURLU) uri = curl_url();
/* create URI, substituting if required */
if (priv->firmware_base_uri != NULL) {
g_autofree gchar *basename = NULL;
g_autofree gchar *path_new = NULL;
g_autoptr(curlptr) path = NULL;
g_autoptr(CURLU) uri_tmp = curl_url();
if (curl_url_set(uri_tmp, CURLUPART_URL, url, 0) != CURLUE_OK) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_FILE,
"Failed to parse url '%s'",
url);
return NULL;
}
(void)curl_url_get(uri_tmp, CURLUPART_PATH, &path, 0);
basename = g_path_get_basename(path);
path_new = g_build_filename(priv->firmware_base_uri, basename, NULL);
(void)curl_url_set(uri, CURLUPART_URL, path_new, 0);
/* use the base URI of the metadata to build the full path */
} else if (g_strstr_len(url, -1, "/") == NULL) {
g_autofree gchar *basename = NULL;
g_autofree gchar *path_new = NULL;
g_autoptr(curlptr) path = NULL;
if (curl_url_set(uri, CURLUPART_URL, priv->metadata_uri, 0) != CURLUE_OK) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_FILE,
"Failed to parse url '%s'",
priv->metadata_uri);
return NULL;
}
(void)curl_url_get(uri, CURLUPART_PATH, &path, 0);
basename = g_path_get_dirname(path);
path_new = g_build_filename(basename, url, NULL);
(void)curl_url_set(uri, CURLUPART_URL, path_new, 0);
/* a normal URI */
} else {
if (curl_url_set(uri, CURLUPART_URL, url, 0) != CURLUE_OK) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_FILE,
"Failed to parse URI '%s'",
url);
return NULL;
}
}
/* set the username and password */
if (priv->username != NULL)
(void)curl_url_set(uri, CURLUPART_USER, priv->username, 0);
if (priv->password != NULL)
(void)curl_url_set(uri, CURLUPART_PASSWORD, priv->password, 0);
(void)curl_url_get(uri, CURLUPART_URL, &tmp_uri, 0);
return g_strdup(tmp_uri);
#else
if (priv->firmware_base_uri != NULL) {
g_autofree gchar *basename = g_path_get_basename(url);
return g_build_filename(priv->firmware_base_uri, basename, NULL);
}
if (g_strstr_len(url, -1, "/") == NULL) {
g_autofree gchar *basename = g_path_get_dirname(priv->metadata_uri);
return g_build_filename(basename, url, NULL);
}
return g_strdup(url);
#endif
}
/* note, this has to be set before username and password */
static void
fwupd_remote_set_metadata_uri(FwupdRemote *self, const gchar *metadata_uri)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
const gchar *suffix;
/* save this so we can export the object as a GVariant */
priv->metadata_uri = g_strdup(metadata_uri);
/* generate the signature URI too */
suffix = fwupd_remote_get_suffix_for_keyring_kind(priv->keyring_kind);
if (suffix != NULL)
priv->metadata_uri_sig = g_strconcat(metadata_uri, suffix, NULL);
}
/* note, this has to be set after MetadataURI */
static void
fwupd_remote_set_firmware_base_uri(FwupdRemote *self, const gchar *firmware_base_uri)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
/* not changed */
if (g_strcmp0(priv->firmware_base_uri, firmware_base_uri) == 0)
return;
g_free(priv->firmware_base_uri);
priv->firmware_base_uri = g_strdup(firmware_base_uri);
}
static void
fwupd_remote_set_report_uri(FwupdRemote *self, const gchar *report_uri)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_autofree gchar *report_uri_safe = fwupd_strdup_nonempty(report_uri);
/* not changed */
if (g_strcmp0(priv->report_uri, report_uri_safe) == 0)
return;
g_free(priv->report_uri);
priv->report_uri = g_steal_pointer(&report_uri_safe);
}
static void
fwupd_remote_set_security_report_uri(FwupdRemote *self, const gchar *security_report_uri)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_autofree gchar *security_report_uri_safe = fwupd_strdup_nonempty(security_report_uri);
/* not changed */
if (g_strcmp0(priv->security_report_uri, security_report_uri_safe) == 0)
return;
g_free(priv->security_report_uri);
priv->security_report_uri = g_steal_pointer(&security_report_uri_safe);
}
/**
* fwupd_remote_kind_from_string:
* @kind: (nullable): a string, e.g. `download`
*
* Converts an printable string to an enumerated type.
*
* Returns: a #FwupdRemoteKind, e.g. %FWUPD_REMOTE_KIND_DOWNLOAD
*
* Since: 0.9.6
**/
FwupdRemoteKind
fwupd_remote_kind_from_string(const gchar *kind)
{
if (g_strcmp0(kind, "download") == 0)
return FWUPD_REMOTE_KIND_DOWNLOAD;
if (g_strcmp0(kind, "local") == 0)
return FWUPD_REMOTE_KIND_LOCAL;
if (g_strcmp0(kind, "directory") == 0)
return FWUPD_REMOTE_KIND_DIRECTORY;
return FWUPD_REMOTE_KIND_UNKNOWN;
}
/**
* fwupd_remote_kind_to_string:
* @kind: a #FwupdRemoteKind, e.g. %FWUPD_REMOTE_KIND_DOWNLOAD
*
* Converts an enumerated type to a printable string.
*
* Returns: a string, e.g. `download`
*
* Since: 0.9.6
**/
const gchar *
fwupd_remote_kind_to_string(FwupdRemoteKind kind)
{
if (kind == FWUPD_REMOTE_KIND_DOWNLOAD)
return "download";
if (kind == FWUPD_REMOTE_KIND_LOCAL)
return "local";
if (kind == FWUPD_REMOTE_KIND_DIRECTORY)
return "directory";
return NULL;
}
/**
* fwupd_remote_set_filename_cache:
* @self: a #FwupdRemote
* @filename: (nullable): filename string
*
* Sets the remote filename cache filename, typically only useful in the self tests.
*
* Since: 1.8.2
**/
void
fwupd_remote_set_filename_cache(FwupdRemote *self, const gchar *filename)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
const gchar *suffix;
g_return_if_fail(FWUPD_IS_REMOTE(self));
/* not changed */
if (g_strcmp0(priv->filename_cache, filename) == 0)
return;
g_free(priv->filename_cache);
priv->filename_cache = g_strdup(filename);
/* create for all remote types */
suffix = fwupd_remote_get_suffix_for_keyring_kind(priv->keyring_kind);
if (suffix != NULL) {
g_free(priv->filename_cache_sig);
priv->filename_cache_sig = g_strconcat(filename, suffix, NULL);
}
}
static void
fwupd_remote_set_order_before(FwupdRemote *self, const gchar *order_before)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_clear_pointer(&priv->order_before, g_strfreev);
if (order_before != NULL)
priv->order_before = g_strsplit_set(order_before, ",:;", -1);
}
static void
fwupd_remote_set_order_after(FwupdRemote *self, const gchar *order_after)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_clear_pointer(&priv->order_after, g_strfreev);
if (order_after != NULL)
priv->order_after = g_strsplit_set(order_after, ",:;", -1);
}
/**
* fwupd_remote_setup:
* @self: a #FwupdRemote
* @error: (nullable): optional return location for an error
*
* Sets up the remote ready for use, checking that required parameters have
* been set. Calling this method multiple times has no effect.
*
* Returns: %TRUE for success
*
* Since: 1.6.1
**/
gboolean
fwupd_remote_setup(FwupdRemote *self, GError **error)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FWUPD_IS_REMOTE(self), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* we can override, hence the extra section */
if (priv->kind == FWUPD_REMOTE_KIND_UNKNOWN) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_FILE,
"metadata kind invalid");
return FALSE;
}
/* some validation for DOWNLOAD types */
if (priv->kind == FWUPD_REMOTE_KIND_DOWNLOAD) {
g_autofree gchar *filename_cache = NULL;
if (priv->remotes_dir == NULL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"remotes directory not set");
return FALSE;
}
/* set cache to /var/lib... */
if (g_str_has_suffix(priv->metadata_uri, ".xml.xz")) {
filename_cache =
g_build_filename(priv->remotes_dir, priv->id, "metadata.xml.xz", NULL);
} else {
filename_cache =
g_build_filename(priv->remotes_dir, priv->id, "metadata.xml.gz", NULL);
}
fwupd_remote_set_filename_cache(self, filename_cache);
}
/* some validation for DIRECTORY types */
if (priv->kind == FWUPD_REMOTE_KIND_DIRECTORY) {
if (priv->keyring_kind != FWUPD_KEYRING_KIND_NONE) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_FILE,
"keyring kind %s is not supported with directory remote",
fwupd_keyring_kind_to_string(priv->keyring_kind));
return FALSE;
}
if (priv->firmware_base_uri != NULL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_FILE,
"Directory remotes don't support firmware base URI");
return FALSE;
}
}
/* load the checksum */
if (priv->filename_cache_sig != NULL &&
g_file_test(priv->filename_cache_sig, G_FILE_TEST_EXISTS)) {
gsize sz = 0;
g_autofree gchar *buf = NULL;
g_autoptr(GChecksum) checksum = g_checksum_new(G_CHECKSUM_SHA256);
if (!g_file_get_contents(priv->filename_cache_sig, &buf, &sz, error)) {
g_prefix_error(error, "failed to get checksum: ");
return FALSE;
}
g_checksum_update(checksum, (guchar *)buf, (gssize)sz);
fwupd_remote_set_checksum(self, g_checksum_get_string(checksum));
} else {
fwupd_remote_set_checksum(self, NULL);
}
/* success */
return TRUE;
}
/**
* fwupd_remote_load_from_filename:
* @self: a #FwupdRemote
* @filename: (not nullable): a filename
* @cancellable: (nullable): optional #GCancellable
* @error: (nullable): optional return location for an error
*
* Loads metadata about the remote from a keyfile.
* This can be called zero or multiple times for each remote.
*
* Returns: %TRUE for success
*
* Since: 0.9.3
**/
gboolean
fwupd_remote_load_from_filename(FwupdRemote *self,
const gchar *filename,
GCancellable *cancellable,
GError **error)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
const gchar *group = "fwupd Remote";
g_autofree gchar *id = NULL;
g_autoptr(GKeyFile) kf = NULL;
g_return_val_if_fail(FWUPD_IS_REMOTE(self), FALSE);
g_return_val_if_fail(filename != NULL, FALSE);
g_return_val_if_fail(cancellable == NULL || G_IS_CANCELLABLE(cancellable), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* set ID */
id = g_path_get_basename(filename);
fwupd_remote_set_id(self, id);
/* load file */
kf = g_key_file_new();
if (!g_key_file_load_from_file(kf, filename, G_KEY_FILE_NONE, error))
return FALSE;
/* optional verification type */
if (g_key_file_has_key(kf, group, "Keyring", NULL)) {
g_autofree gchar *tmp = g_key_file_get_string(kf, group, "Keyring", NULL);
priv->keyring_kind = fwupd_keyring_kind_from_string(tmp);
if (priv->keyring_kind == FWUPD_KEYRING_KIND_UNKNOWN) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_FILE,
"keyring kind '%s' unknown",
tmp);
return FALSE;
}
}
/* the first remote sets the URI, even if it's file:// to the cache */
if (g_key_file_has_key(kf, group, "MetadataURI", NULL)) {
g_autofree gchar *tmp = g_key_file_get_string(kf, group, "MetadataURI", NULL);
if (g_str_has_prefix(tmp, "file://")) {
const gchar *filename_cache = tmp;
if (g_str_has_prefix(filename_cache, "file://"))
filename_cache += 7;
fwupd_remote_set_filename_cache(self, filename_cache);
if (g_file_test(filename_cache, G_FILE_TEST_IS_DIR))
priv->kind = FWUPD_REMOTE_KIND_DIRECTORY;
else
priv->kind = FWUPD_REMOTE_KIND_LOCAL;
} else if (g_str_has_prefix(tmp, "http://") || g_str_has_prefix(tmp, "https://") ||
g_str_has_prefix(tmp, "ipfs://") || g_str_has_prefix(tmp, "ipns://")) {
priv->kind = FWUPD_REMOTE_KIND_DOWNLOAD;
fwupd_remote_set_metadata_uri(self, tmp);
}
}
/* all keys are optional */
if (g_key_file_has_key(kf, group, "Enabled", NULL))
priv->enabled = g_key_file_get_boolean(kf, group, "Enabled", NULL);
if (g_key_file_has_key(kf, group, "ApprovalRequired", NULL))
priv->approval_required =
g_key_file_get_boolean(kf, group, "ApprovalRequired", NULL);
if (g_key_file_has_key(kf, group, "Title", NULL)) {
g_autofree gchar *tmp = g_key_file_get_string(kf, group, "Title", NULL);
fwupd_remote_set_title(self, tmp);
}
if (g_key_file_has_key(kf, group, "ReportURI", NULL)) {
g_autofree gchar *tmp = g_key_file_get_string(kf, group, "ReportURI", NULL);
fwupd_remote_set_report_uri(self, tmp);
}
if (g_key_file_has_key(kf, group, "SecurityReportURI", NULL)) {
g_autofree gchar *tmp = g_key_file_get_string(kf, group, "SecurityReportURI", NULL);
fwupd_remote_set_security_report_uri(self, tmp);
}
if (g_key_file_has_key(kf, group, "Username", NULL)) {
g_autofree gchar *tmp = g_key_file_get_string(kf, group, "Username", NULL);
fwupd_remote_set_username(self, tmp);
}
if (g_key_file_has_key(kf, group, "Password", NULL)) {
g_autofree gchar *tmp = g_key_file_get_string(kf, group, "Password", NULL);
fwupd_remote_set_password(self, tmp);
}
if (g_key_file_has_key(kf, group, "FirmwareBaseURI", NULL)) {
g_autofree gchar *tmp = g_key_file_get_string(kf, group, "FirmwareBaseURI", NULL);
fwupd_remote_set_firmware_base_uri(self, tmp);
}
if (g_key_file_has_key(kf, group, "OrderBefore", NULL)) {
g_autofree gchar *tmp = g_key_file_get_string(kf, group, "OrderBefore", NULL);
fwupd_remote_set_order_before(self, tmp);
}
if (g_key_file_has_key(kf, group, "OrderAfter", NULL)) {
g_autofree gchar *tmp = g_key_file_get_string(kf, group, "OrderAfter", NULL);
fwupd_remote_set_order_after(self, tmp);
}
if (g_key_file_has_key(kf, group, "AutomaticReports", NULL))
priv->automatic_reports =
g_key_file_get_boolean(kf, group, "AutomaticReports", NULL);
if (g_key_file_has_key(kf, group, "AutomaticSecurityReports", NULL))
priv->automatic_security_reports =
g_key_file_get_boolean(kf, group, "AutomaticSecurityReports", NULL);
/* success */
fwupd_remote_set_filename_source(self, filename);
return TRUE;
}
/**
* fwupd_remote_get_order_after:
* @self: a #FwupdRemote
*
* Gets the list of remotes this plugin should be ordered after.
*
* Returns: (transfer none): an array
*
* Since: 0.9.5
**/
gchar **
fwupd_remote_get_order_after(FwupdRemote *self)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FWUPD_IS_REMOTE(self), NULL);
return priv->order_after;
}
/**
* fwupd_remote_get_order_before:
* @self: a #FwupdRemote
*
* Gets the list of remotes this plugin should be ordered before.
*
* Returns: (transfer none): an array
*
* Since: 0.9.5
**/
gchar **
fwupd_remote_get_order_before(FwupdRemote *self)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FWUPD_IS_REMOTE(self), NULL);
return priv->order_before;
}
/**
* fwupd_remote_get_filename_cache:
* @self: a #FwupdRemote
*
* Gets the path and filename that the remote is using for a cache.
*
* Returns: a string, or %NULL for unset
*
* Since: 0.9.6
**/
const gchar *
fwupd_remote_get_filename_cache(FwupdRemote *self)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FWUPD_IS_REMOTE(self), NULL);
return priv->filename_cache;
}
/**
* fwupd_remote_get_filename_cache_sig:
* @self: a #FwupdRemote
*
* Gets the path and filename that the remote is using for a signature cache.
*
* Returns: a string, or %NULL for unset
*
* Since: 0.9.7
**/
const gchar *
fwupd_remote_get_filename_cache_sig(FwupdRemote *self)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FWUPD_IS_REMOTE(self), NULL);
return priv->filename_cache_sig;
}
/**
* fwupd_remote_get_filename_source:
* @self: a #FwupdRemote
*
* Gets the path and filename of the remote itself, typically a `.conf` file.
*
* Returns: a string, or %NULL for unset
*
* Since: 0.9.8
**/
const gchar *
fwupd_remote_get_filename_source(FwupdRemote *self)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FWUPD_IS_REMOTE(self), NULL);
return priv->filename_source;
}
/**
* fwupd_remote_get_priority:
* @self: a #FwupdRemote
*
* Gets the priority of the remote, where bigger numbers are better.
*
* Returns: a priority, or 0 for the default value
*
* Since: 0.9.5
**/
gint
fwupd_remote_get_priority(FwupdRemote *self)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FWUPD_IS_REMOTE(self), 0);
return priv->priority;
}
/**
* fwupd_remote_get_kind:
* @self: a #FwupdRemote
*
* Gets the kind of the remote.
*
* Returns: a #FwupdRemoteKind, e.g. #FWUPD_REMOTE_KIND_LOCAL
*
* Since: 0.9.6
**/
FwupdRemoteKind
fwupd_remote_get_kind(FwupdRemote *self)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FWUPD_IS_REMOTE(self), 0);
return priv->kind;
}
/**
* fwupd_remote_get_keyring_kind:
* @self: a #FwupdRemote
*
* Gets the keyring kind of the remote.
*
* Returns: a #FwupdKeyringKind, e.g. #FWUPD_KEYRING_KIND_GPG
*
* Since: 0.9.7
**/
FwupdKeyringKind
fwupd_remote_get_keyring_kind(FwupdRemote *self)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FWUPD_IS_REMOTE(self), 0);
return priv->keyring_kind;
}
/**
* fwupd_remote_get_age:
* @self: a #FwupdRemote
*
* Gets the age of the remote in seconds.
*
* Returns: a age, or %G_MAXUINT64 for unavailable
*
* Since: 0.9.5
**/
guint64
fwupd_remote_get_age(FwupdRemote *self)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
guint64 now;
g_return_val_if_fail(FWUPD_IS_REMOTE(self), 0);
now = (guint64)g_get_real_time() / G_USEC_PER_SEC;
if (priv->mtime > now)
return G_MAXUINT64;
return now - priv->mtime;
}
/**
* fwupd_remote_set_remotes_dir:
* @self: a #FwupdRemote
* @directory: (nullable): Remotes directory
*
* Sets the directory to store remote data
*
* Since: 1.3.1
**/
void
fwupd_remote_set_remotes_dir(FwupdRemote *self, const gchar *directory)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FWUPD_IS_REMOTE(self));
/* not changed */
if (g_strcmp0(priv->remotes_dir, directory) == 0)
return;
g_free(priv->remotes_dir);
priv->remotes_dir = g_strdup(directory);
}
/**
* fwupd_remote_set_priority:
* @self: a #FwupdRemote
* @priority: an integer, where 1 is better
*
* Sets the plugin priority.
*
* Since: 0.9.5
**/
void
fwupd_remote_set_priority(FwupdRemote *self, gint priority)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FWUPD_IS_REMOTE(self));
priv->priority = priority;
}
/**
* fwupd_remote_set_mtime:
* @self: a #FwupdRemote
* @mtime: a UNIX timestamp
*
* Sets the plugin modification time.
*
* Since: 0.9.5
**/
void
fwupd_remote_set_mtime(FwupdRemote *self, guint64 mtime)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FWUPD_IS_REMOTE(self));
priv->mtime = mtime;
}
/**
* fwupd_remote_get_username:
* @self: a #FwupdRemote
*
* Gets the username configured for the remote.
*
* Returns: a string, or %NULL for unset
*
* Since: 0.9.5
**/
const gchar *
fwupd_remote_get_username(FwupdRemote *self)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FWUPD_IS_REMOTE(self), NULL);
return priv->username;
}
/**
* fwupd_remote_get_password:
* @self: a #FwupdRemote
*
* Gets the password configured for the remote.
*
* Returns: a string, or %NULL for unset
*
* Since: 0.9.5
**/
const gchar *
fwupd_remote_get_password(FwupdRemote *self)
{
FwupdRemotePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FWUPD_IS_REMOTE(self), NULL);
return priv->password;
}
/**
* fwupd_remote_get_title:
* @self: a #FwupdRemote
*
* Gets the remote title, e.g. `Linux Vendor Firmware Service`.
*
* Returns: a string, or %NULL if unset