forked from gentoo/portage-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qmanifest.c
1867 lines (1667 loc) · 47 KB
/
qmanifest.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 2018-2024 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
*
* Copyright 2018- Fabian Groffen - <grobian@gentoo.org>
*
* The contents of this file was taken from:
* https://github.com/grobian/hashgen
* which was discontinued at the time the sources were incorporated into
* portage-utils as qmanifest.
*/
#include "main.h"
#ifdef ENABLE_QMANIFEST
#include "applets.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <fcntl.h>
#include <ctype.h>
#include <dirent.h>
#include <time.h>
#include <errno.h>
#include <termios.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <openssl/sha.h>
#include <blake2.h>
#include <zlib.h>
#include <gpgme.h>
#include "eat_file.h"
#include "hash.h"
#define QMANIFEST_FLAGS "gs:pdo" COMMON_FLAGS
static struct option const qmanifest_long_opts[] = {
{"generate", no_argument, NULL, 'g'},
{"signas", a_argument, NULL, 's'},
{"passphrase", no_argument, NULL, 'p'},
{"dir", no_argument, NULL, 'd'},
{"overlay", no_argument, NULL, 'o'},
COMMON_LONG_OPTS
};
static const char * const qmanifest_opts_help[] = {
"Generate thick Manifests",
"Sign generated Manifest using GPG key",
"Ask for GPG key password (instead of relying on gpg-agent)",
"Treat arguments as directories",
"Treat arguments as overlay names",
COMMON_OPTS_HELP
};
#define qmanifest_usage(ret) usage(ret, QMANIFEST_FLAGS, qmanifest_long_opts, qmanifest_opts_help, NULL, lookup_applet_idx("qmanifest"))
static int hashes = HASH_DEFAULT;
static char *gpg_sign_key = NULL;
static bool gpg_get_password = false;
/* linked list structure to hold verification complaints */
typedef struct verify_msg {
char *msg;
struct verify_msg *next;
} verify_msg;
typedef struct _gpg_signature {
char *algo;
char *fingerprint;
char isgood:1;
char *timestamp;
char *signer;
char *pkfingerprint;
char *reason;
} gpg_sig;
gpg_sig *verify_gpg_sig(const char *path, verify_msg **msgs);
char *verify_timestamp(const char *ts);
char verify_manifest(const char *dir, const char *manifest, verify_msg **msgs);
/* Generate thick Manifests based on thin Manifests, or verify a tree. */
/* In order to build this program, the following packages are required:
* - sys-libs/zlib (for compressing/decompressing Manifest files)
* - app-crypt/gpgme (for signing/verifying the top level manifest)
*/
static inline void
update_times(struct timeval *tv, struct stat *s)
{
#if defined (__MACH__) && defined __APPLE__
# define st_mtim st_mtimespec
# define st_atim st_atimespec
#endif
if (tv[1].tv_sec < s->st_mtim.tv_sec ||
(tv[1].tv_sec == s->st_mtim.tv_sec &&
tv[1].tv_usec < s->st_mtim.tv_nsec / 1000))
{
tv[0].tv_sec = s->st_atim.tv_sec;
tv[0].tv_usec = s->st_atim.tv_nsec / 1000;
tv[1].tv_sec = s->st_mtim.tv_sec;
tv[1].tv_usec = s->st_mtim.tv_nsec / 1000;
}
}
#define LISTSZ 64
/**
* qsort comparator which runs strcmp.
*/
static int
compare_strings(const void *l, const void *r)
{
const char **strl = (const char **)l;
const char **strr = (const char **)r;
return strcmp(*strl, *strr);
}
/**
* Return a sorted list of entries in the given directory. All entries
* starting with a dot are ignored, and not present in the returned
* list. The list and all entries are allocated using xmalloc() and need
* to be freed.
* This function returns 0 when everything is fine, non-zero otherwise.
*/
static char
list_dir(char ***retlist, size_t *retcnt, const char *path)
{
DIR *d;
struct dirent *e;
size_t rlen = 0;
size_t rsize = 0;
char **rlist = NULL;
if ((d = opendir(path)) != NULL) {
while ((e = readdir(d)) != NULL) {
/* skip all dotfiles */
if (e->d_name[0] == '.')
continue;
if (rlen == rsize) {
rsize += LISTSZ;
rlist = xrealloc(rlist,
rsize * sizeof(rlist[0]));
if (rlist == NULL) {
fprintf(stderr, "out of memory\n");
return 1;
}
}
rlist[rlen] = xstrdup(e->d_name);
if (rlist[rlen] == NULL) {
fprintf(stderr, "out of memory\n");
return 1;
}
rlen++;
}
closedir(d);
if (rlen > 1)
qsort(rlist, rlen, sizeof(rlist[0]), compare_strings);
*retlist = rlist;
*retcnt = rlen;
return 0;
} else {
return 1;
}
}
/**
* Write hashes in Manifest format to the file open for writing m, or
* gzipped file open for writing gm. The hashes written are for a file
* in root found by name. The Manifest entry will be using type as
* first component.
*/
static void
write_hashes(
struct timeval *tv,
const char *root,
const char *name,
const char *type,
FILE *m,
gzFile gm)
{
size_t flen = 0;
char sha256[(SHA256_DIGEST_LENGTH * 2) + 1];
char sha512[(SHA512_DIGEST_LENGTH * 2) + 1];
char blak2b[(BLAKE2B_OUTBYTES * 2) + 1];
char data[8192];
char fname[8192];
size_t len;
struct stat s;
snprintf(fname, sizeof(fname), "%s/%s", root, name);
if (stat(fname, &s) != 0)
return;
update_times(tv, &s);
hash_compute_file(fname, sha256, sha512, blak2b, &flen, hashes);
len = snprintf(data, sizeof(data), "%s %s %zd", type, name, flen);
if (hashes & HASH_BLAKE2B)
len += snprintf(data + len, sizeof(data) - len,
" BLAKE2B %s", blak2b);
if (hashes & HASH_SHA256)
len += snprintf(data + len, sizeof(data) - len,
" SHA256 %s", sha256);
if (hashes & HASH_SHA512)
len += snprintf(data + len, sizeof(data) - len,
" SHA512 %s", sha512);
len += snprintf(data + len, sizeof(data) - len, "\n");
if (m != NULL)
fwrite(data, len, 1, m);
if (gm != NULL && gzwrite(gm, data, len) == 0)
fprintf(stderr, "failed to write to compressed stream\n");
}
/**
* Walk through a directory recursively and write hashes for each file
* found to the gzipped open stream for writing zm. The Manifest
* entries generated will all be of DATA type.
*/
static char
write_hashes_dir(
struct timeval *tv,
const char *root,
const char *name,
gzFile zm)
{
char path[8192];
char **dentries;
size_t dentrieslen;
size_t i;
snprintf(path, sizeof(path), "%s/%s", root, name);
if (list_dir(&dentries, &dentrieslen, path) == 0) {
for (i = 0; i < dentrieslen; i++) {
snprintf(path, sizeof(path), "%s/%s", name, dentries[i]);
free(dentries[i]);
if (write_hashes_dir(tv, root, path, zm) == 0)
continue;
/* regular file */
write_hashes(tv, root, path, "DATA", NULL, zm);
}
free(dentries);
return 0;
} else {
return 1;
}
}
/**
* Walk through directory recursively and write hashes for each file
* found to the open stream for writing m. All files will not use the
* "files/" prefix and Manifest entries will be of AUX type.
*/
static char
process_files(struct timeval *tv, const char *dir, const char *off, FILE *m)
{
char path[8192];
char **dentries;
size_t dentrieslen;
size_t i;
snprintf(path, sizeof(path), "%s/%s", dir, off);
if (list_dir(&dentries, &dentrieslen, path) == 0) {
for (i = 0; i < dentrieslen; i++) {
snprintf(path, sizeof(path), "%s%s%s",
off, *off == '\0' ? "" : "/", dentries[i]);
free(dentries[i]);
if (process_files(tv, dir, path, m) == 0)
continue;
/* regular file */
write_hashes(tv, dir, path, "AUX", m, NULL);
}
free(dentries);
return 0;
} else {
return 1;
}
}
/**
* Read layout.conf file specified by path and extract the
* manifest-hashes property from this file. The hash set specified for
* this property will be returned. When the property isn't found the
* returned hash set will be the HASH_DEFAULT set. When the file isn't
* found, 0 will be returned (which means /no/ hashes).
*/
static int
parse_layout_conf(const char *path)
{
FILE *f;
char buf[8192];
size_t len = 0;
size_t sz;
char *p;
char *q;
char *tok;
char *last_nl;
char *start;
int ret = 0;
if ((f = fopen(path, "r")) == NULL)
return 0;
/* read file, examine lines after encountering a newline, that is,
* if the file doesn't end with a newline, the final bit is ignored */
while ((sz = fread(buf + len, 1, sizeof(buf) - len, f)) > 0) {
len += sz;
start = buf;
last_nl = NULL;
for (p = buf; (size_t)(p - buf) < len; p++) {
if (*p == '\n') {
if (last_nl != NULL)
start = last_nl + 1;
last_nl = p;
do {
sz = strlen("manifest-hashes");
if (strncmp(start, "manifest-hashes", sz))
break;
if ((q = strchr(start + sz, '=')) == NULL)
break;
q++;
while (isspace((int)*q))
q++;
/* parse the tokens, whitespace separated */
tok = q;
do {
while (!isspace((int)*q))
q++;
sz = q - tok;
if (strncmp(tok, "SHA256", sz) == 0) {
ret |= HASH_SHA256;
} else if (strncmp(tok, "SHA512", sz) == 0) {
ret |= HASH_SHA512;
} else if (strncmp(tok, "WHIRLPOOL", sz) == 0) {
ret |= HASH_WHIRLPOOL;
} else if (strncmp(tok, "BLAKE2B", sz) == 0) {
ret |= HASH_BLAKE2B;
} else {
fprintf(stderr, "warning: unsupported hash from "
"layout.conf: %.*s\n", (int)sz, tok);
}
while (isspace((int)*q) && *q != '\n')
q++;
tok = q;
} while (*q != '\n');
/* got it, expect only once, so stop processing */
fclose(f);
return ret;
} while (0);
}
}
if (last_nl != NULL) {
last_nl++; /* skip \n */
len = last_nl - buf;
memmove(buf, last_nl, len);
} else {
/* skip too long line */
len = 0;
}
}
fclose(f);
/* if we didn't find anything, return the default set */
return HASH_DEFAULT;
}
static const char *str_manifest = "Manifest";
static const char *str_manifest_gz = "Manifest.gz";
static const char *str_manifest_files_gz = "Manifest.files.gz";
enum type_manifest {
GLOBAL_MANIFEST, /* Manifest.files.gz + Manifest */
SUBTREE_MANIFEST, /* Manifest.gz for recursive list of files */
EBUILD_MANIFEST, /* Manifest thick from thin */
CATEGORY_MANIFEST /* Manifest.gz with Manifest entries */
};
static const char *
generate_dir(const char *dir, enum type_manifest mtype)
{
FILE *f;
char path[8192];
struct stat s;
struct timeval tv[2];
char **dentries;
size_t dentrieslen;
size_t i;
/* our timestamp strategy is as follows:
* - when a Manifest exists, use its timestamp
* - when a meta-Manifest is written (non-ebuilds) use the timestamp
* of the latest Manifest referenced
* - when a Manifest is written for something like eclasses, use the
* timestamp of the latest file in the dir
* this way we should keep updates limited to where changes are, and
* also get reproducible mtimes. */
tv[0].tv_sec = 0;
tv[0].tv_usec = 0;
tv[1].tv_sec = 0;
tv[1].tv_usec = 0;
if (mtype == GLOBAL_MANIFEST) {
const char *mfest;
size_t len;
gzFile mf;
time_t rtime;
snprintf(path, sizeof(path), "%s/%s", dir, str_manifest_files_gz);
if ((mf = gzopen(path, "wb9")) == NULL) {
fprintf(stderr, "failed to open file '%s' for writing: %s\n",
path, strerror(errno));
return NULL;
}
/* These "IGNORE" entries are taken from gx86, there is no
* standardisation on this, on purpose, apparently. */
len = snprintf(path, sizeof(path),
"IGNORE distfiles\n"
"IGNORE local\n"
"IGNORE lost+found\n"
"IGNORE packages\n"
"IGNORE snapshots\n");
if (gzwrite(mf, path, len) == 0) {
fprintf(stderr, "failed to write to file '%s/%s': %s\n",
dir, str_manifest_files_gz, strerror(errno));
gzclose(mf);
return NULL;
}
if (list_dir(&dentries, &dentrieslen, dir) != 0) {
gzclose(mf);
return NULL;
}
for (i = 0; i < dentrieslen; i++) {
/* ignore existing Manifests */
if (strcmp(dentries[i], str_manifest_files_gz) == 0 ||
strcmp(dentries[i], str_manifest) == 0)
{
free(dentries[i]);
continue;
}
snprintf(path, sizeof(path), "%s/%s", dir, dentries[i]);
mfest = NULL;
if (!stat(path, &s)) {
if (s.st_mode & S_IFDIR) {
if (
strcmp(dentries[i], "eclass") == 0 ||
strcmp(dentries[i], "licenses") == 0 ||
strcmp(dentries[i], "metadata") == 0 ||
strcmp(dentries[i], "profiles") == 0 ||
strcmp(dentries[i], "scripts") == 0
)
{
mfest = generate_dir(path, SUBTREE_MANIFEST);
} else {
mfest = generate_dir(path, CATEGORY_MANIFEST);
}
if (mfest == NULL) {
fprintf(stderr, "generating Manifest for %s failed!\n",
path);
gzclose(mf);
for (; i < dentrieslen; i++)
free(dentries[i]);
free(dentries);
return NULL;
}
snprintf(path, sizeof(path), "%s/%s",
dentries[i], mfest);
write_hashes(tv, dir, path, "MANIFEST", NULL, mf);
} else if (s.st_mode & S_IFREG) {
write_hashes(tv, dir, dentries[i], "DATA", NULL, mf);
} /* ignore other "things" (like symlinks) as they
don't belong in a tree */
} else {
fprintf(stderr, "stat(%s) failed: %s\n",
path, strerror(errno));
}
free(dentries[i]);
}
free(dentries);
gzclose(mf);
if (tv[0].tv_sec != 0) {
snprintf(path, sizeof(path), "%s/%s", dir, str_manifest_files_gz);
utimes(path, tv);
}
/* create global Manifest */
snprintf(path, sizeof(path), "%s/%s", dir, str_manifest);
if ((f = fopen(path, "w")) == NULL) {
fprintf(stderr, "failed to open file '%s' for writing: %s\n",
path, strerror(errno));
return NULL;
}
write_hashes(tv, dir, str_manifest_files_gz, "MANIFEST", f, NULL);
time(&rtime);
len = strftime(path, sizeof(path),
"TIMESTAMP %Y-%m-%dT%H:%M:%SZ\n", gmtime(&rtime));
fwrite(path, len, 1, f);
fflush(f);
fclose(f);
/* because we write a timestamp in Manifest, we don't mess with
* its mtime, else it would obviously lie */
return str_manifest_files_gz;
} else if (mtype == SUBTREE_MANIFEST) {
const char *ldir;
gzFile mf;
snprintf(path, sizeof(path), "%s/%s", dir, str_manifest_gz);
if ((mf = gzopen(path, "wb9")) == NULL) {
fprintf(stderr, "failed to open file '%s' for writing: %s\n",
path, strerror(errno));
return NULL;
}
ldir = strrchr(dir, '/');
if (ldir == NULL)
ldir = dir;
if (strcmp(ldir, "metadata") == 0) {
size_t len;
len = snprintf(path, sizeof(path),
"IGNORE timestamp\n"
"IGNORE timestamp.chk\n"
"IGNORE timestamp.commit\n"
"IGNORE timestamp.x\n");
if (gzwrite(mf, path, len) == 0) {
fprintf(stderr, "failed to write to file '%s/%s': %s\n",
dir, str_manifest_gz, strerror(errno));
gzclose(mf);
return NULL;
}
}
if (list_dir(&dentries, &dentrieslen, dir) != 0) {
gzclose(mf);
return NULL;
}
for (i = 0; i < dentrieslen; i++) {
/* ignore existing Manifests */
if (strcmp(dentries[i], str_manifest_gz) == 0) {
free(dentries[i]);
continue;
}
if (write_hashes_dir(tv, dir, dentries[i], mf) != 0)
write_hashes(tv, dir, dentries[i], "DATA", NULL, mf);
free(dentries[i]);
}
free(dentries);
gzclose(mf);
if (tv[0].tv_sec != 0) {
/* set Manifest and dir mtime to most recent file found */
snprintf(path, sizeof(path), "%s/%s", dir, str_manifest_gz);
utimes(path, tv);
utimes(dir, tv);
}
return str_manifest_gz;
} else if (mtype == CATEGORY_MANIFEST) {
const char *mfest;
gzFile mf;
const char *ret = str_manifest_gz;
snprintf(path, sizeof(path), "%s/%s", dir, str_manifest_gz);
if ((mf = gzopen(path, "wb9")) == NULL) {
fprintf(stderr, "failed to open file '%s' for writing: %s\n",
path, strerror(errno));
return NULL;
}
if (list_dir(&dentries, &dentrieslen, dir) != 0) {
gzclose(mf);
return NULL;
}
for (i = 0; i < dentrieslen; i++) {
/* ignore existing Manifests */
if (strcmp(dentries[i], str_manifest_gz) == 0) {
free(dentries[i]);
continue;
}
snprintf(path, sizeof(path), "%s/%s", dir, dentries[i]);
if (!stat(path, &s)) {
if (s.st_mode & S_IFDIR) {
mfest = generate_dir(path, EBUILD_MANIFEST);
if (mfest == NULL) {
fprintf(stderr, "generating Manifest for %s failed!\n",
path);
tv[0].tv_sec = 0;
ret = NULL;
} else {
snprintf(path, sizeof(path), "%s/%s",
dentries[i], mfest);
write_hashes(tv, dir, path, "MANIFEST", NULL, mf);
}
} else if (s.st_mode & S_IFREG) {
write_hashes(tv, dir, dentries[i], "DATA", NULL, mf);
} /* ignore other "things" (like symlinks) as they
don't belong in a tree */
} else {
fprintf(stderr, "stat(%s) failed: %s\n",
path, strerror(errno));
}
free(dentries[i]);
}
free(dentries);
gzclose(mf);
if (tv[0].tv_sec != 0) {
/* set Manifest and dir mtime to most ebuild dir found */
snprintf(path, sizeof(path), "%s/%s", dir, str_manifest_gz);
utimes(path, tv);
utimes(dir, tv);
}
return ret;
} else if (mtype == EBUILD_MANIFEST) {
char newmanifest[8192];
FILE *m;
snprintf(newmanifest, sizeof(newmanifest), "%s/.Manifest.new", dir);
if ((m = fopen(newmanifest, "w")) == NULL) {
fprintf(stderr, "failed to open file '%s' for writing: %s\n",
newmanifest, strerror(errno));
return NULL;
}
/* we know the Manifest is sorted, and stuff in files/ is
* prefixed with AUX, hence, if it exists, we need to do it
* first */
snprintf(path, sizeof(path), "%s/files", dir);
process_files(tv, path, "", m);
/* the Manifest file may be missing in case there are no DIST
* entries to be stored */
snprintf(path, sizeof(path), "%s/%s", dir, str_manifest);
if (!stat(path, &s))
update_times(tv, &s);
f = fopen(path, "r");
if (f != NULL) {
/* copy the DIST entries, we could do it unconditional, but this
* way we can re-run without producing invalid Manifests */
while (fgets(path, sizeof(path), f) != NULL) {
if (strncmp(path, "DIST ", 5) == 0)
if (fwrite(path, strlen(path), 1, m) != 1) {
fprintf(stderr, "failed to write to "
"%s/.Manifest.new: %s\n",
dir, strerror(errno));
fclose(f);
fclose(m);
return NULL;
}
}
fclose(f);
}
if (list_dir(&dentries, &dentrieslen, dir) == 0) {
for (i = 0; i < dentrieslen; i++) {
if (strcmp(dentries[i] + strlen(dentries[i]) - 7,
".ebuild") != 0)
{
free(dentries[i]);
continue;
}
write_hashes(tv, dir, dentries[i], "EBUILD", m, NULL);
free(dentries[i]);
}
free(dentries);
}
write_hashes(tv, dir, "ChangeLog", "MISC", m, NULL);
write_hashes(tv, dir, "metadata.xml", "MISC", m, NULL);
fflush(m);
fclose(m);
snprintf(path, sizeof(path), "%s/%s", dir, str_manifest);
if (rename(newmanifest, path) == -1) {
fprintf(stderr, "failed to rename file '%s' to '%s': %s\n",
newmanifest, path, strerror(errno));
return NULL;
}
if (tv[0].tv_sec != 0) {
/* set Manifest and dir mtime to most recent file we found */
utimes(path, tv);
utimes(dir, tv);
}
return str_manifest;
} else {
return NULL;
}
}
static gpgme_error_t
gpgme_pw_cb(void *opaque, const char *uid_hint, const char *pw_info,
int last_was_bad, int fd)
{
char *pass = (char *)opaque;
size_t passlen = strlen(pass);
ssize_t ret;
(void)uid_hint;
(void)pw_info;
(void)last_was_bad;
do {
ret = write(fd, pass, passlen);
if (ret > 0) {
pass += ret;
passlen -= ret;
}
} while (passlen > 0 && ret > 0);
return passlen == 0 ? GPG_ERR_NO_ERROR : gpgme_error_from_errno(errno);
}
static const char *
process_dir_gen(void)
{
char path[_Q_PATH_MAX];
int newhashes;
struct termios termio;
char *gpg_pass;
if ((newhashes = parse_layout_conf("metadata/layout.conf")) != 0) {
hashes = newhashes;
} else {
return "generation must be done on a full tree";
}
if (generate_dir(".\0", GLOBAL_MANIFEST) == NULL)
return "generation failed";
if (gpg_sign_key != NULL) {
gpgme_ctx_t gctx;
gpgme_error_t gerr;
gpgme_key_t gkey;
gpgme_data_t manifest;
gpgme_data_t out;
FILE *f;
size_t dlen;
gerr = gpgme_new(&gctx);
if (gerr != GPG_ERR_NO_ERROR)
return "GPG setup failed";
gerr = gpgme_get_key(gctx, gpg_sign_key, &gkey, 0);
if (gerr != GPG_ERR_NO_ERROR)
return "failed to get GPG key";
gerr = gpgme_signers_add(gctx, gkey);
if (gerr != GPG_ERR_NO_ERROR)
return "failed to add GPG key to sign list, is it a suitable key?";
gpgme_key_unref(gkey);
gpg_pass = NULL;
if (gpg_get_password) {
if (isatty(fileno(stdin))) {
/* disable terminal echo; the printing of what you type */
tcgetattr(fileno(stdin), &termio);
termio.c_lflag &= ~ECHO;
tcsetattr(fileno(stdin), TCSANOW, &termio);
printf("Password for GPG-key %s: ", gpg_sign_key);
}
gpg_pass = fgets(path, sizeof(path), stdin);
if (isatty(fileno(stdin))) {
printf("\n");
/* restore echoing, for what it's worth */
termio.c_lflag |= ECHO;
tcsetattr(fileno(stdin), TCSANOW, &termio);
}
if (gpg_pass == NULL || *gpg_pass == '\0')
warn("no GPG password given, gpg might ask for it again");
/* continue for the case where gpg-agent holds the pass */
else {
gpgme_set_pinentry_mode(gctx, GPGME_PINENTRY_MODE_LOOPBACK);
gpgme_set_passphrase_cb(gctx, gpgme_pw_cb, gpg_pass);
}
}
if ((f = fopen(str_manifest, "r+")) == NULL)
return "could not open top-level Manifest file";
/* finally, sign the Manifest */
if (gpgme_data_new_from_stream(&manifest, f) != GPG_ERR_NO_ERROR)
return "failed to create GPG data from Manifest";
if (gpgme_data_new(&out) != GPG_ERR_NO_ERROR)
return "failed to create GPG output buffer";
gerr = gpgme_op_sign(gctx, manifest, out, GPGME_SIG_MODE_CLEAR);
if (gerr != GPG_ERR_NO_ERROR) {
warn("%s: %s", gpgme_strsource(gerr), gpgme_strerror(gerr));
return "failed to GPG sign Manifest";
}
/* write back signed Manifest */
rewind(f);
gpgme_data_seek(out, 0, SEEK_SET);
do {
dlen = gpgme_data_read(out, path, sizeof(path));
fwrite(path, dlen, 1, f);
} while (dlen == sizeof(path));
fclose(f);
gpgme_data_release(out);
gpgme_data_release(manifest);
gpgme_release(gctx);
}
return NULL;
}
static void
msgs_add(
verify_msg **msgs,
const char *manifest,
const char *ebuild,
const char *fmt, ...)
{
char buf[4096];
int len;
va_list ap;
verify_msg *msg;
if (msgs == NULL || *msgs == NULL)
return;
msg = (*msgs)->next = xmalloc(sizeof(verify_msg));
len = snprintf(buf, sizeof(buf), "%s:%s:",
manifest ? manifest : "",
ebuild ? ebuild : "");
va_start(ap, fmt);
vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
va_end(ap);
msg->msg = xstrdup(buf);
msg->next = NULL;
*msgs = msg;
}
gpg_sig *
verify_gpg_sig(const char *path, verify_msg **msgs)
{
gpgme_ctx_t g_ctx;
gpgme_data_t manifest;
gpgme_data_t out;
gpgme_verify_result_t vres;
gpgme_signature_t sig;
gpgme_key_t key;
char buf[64];
FILE *f;
struct tm *ctime;
gpg_sig *ret = NULL;
if (gpgme_new(&g_ctx) != GPG_ERR_NO_ERROR) {
msgs_add(msgs, path, NULL, "failed to create gpgme context");
return NULL;
}
if (gpgme_data_new(&out) != GPG_ERR_NO_ERROR) {
msgs_add(msgs, path, NULL, "failed to create gpgme data");
gpgme_release(g_ctx);
return NULL;
}
if ((f = fopen(path, "r")) == NULL) {
msgs_add(msgs, path, NULL, "failed to open: %s", strerror(errno));
gpgme_data_release(out);
gpgme_release(g_ctx);
return NULL;
}
if (gpgme_data_new_from_stream(&manifest, f) != GPG_ERR_NO_ERROR) {
msgs_add(msgs, path, NULL,
"failed to create new gpgme data from stream");
gpgme_data_release(out);
gpgme_release(g_ctx);
fclose(f);
return NULL;
}
if (gpgme_op_verify(g_ctx, manifest, NULL, out) != GPG_ERR_NO_ERROR) {
msgs_add(msgs, path, NULL, "failed to verify signature");
gpgme_data_release(out);
gpgme_data_release(manifest);
gpgme_release(g_ctx);
fclose(f);
return NULL;
}
vres = gpgme_op_verify_result(g_ctx);
fclose(f);
if (vres == NULL || vres->signatures == NULL) {
msgs_add(msgs, path, NULL,
"verification failed due to a missing gpg keyring");
gpgme_data_release(out);
gpgme_data_release(manifest);
gpgme_release(g_ctx);
return NULL;
}
/* we only check/return the first signature */
if ((sig = vres->signatures) != NULL) {
ret = xmalloc(sizeof(gpg_sig));
if (sig->fpr != NULL) {
snprintf(buf, sizeof(buf),
"%.4s %.4s %.4s %.4s %.4s %.4s %.4s %.4s %.4s %.4s",
sig->fpr + 0, sig->fpr + 4, sig->fpr + 8, sig->fpr + 12,
sig->fpr + 16, sig->fpr + 20, sig->fpr + 24, sig->fpr + 28,
sig->fpr + 32, sig->fpr + 36);
} else {
snprintf(buf, sizeof(buf), "<fingerprint not found>");
}
if (sig->status != GPG_ERR_NO_PUBKEY) {
ret->algo = xstrdup(gpgme_pubkey_algo_name(sig->pubkey_algo));
ret->fingerprint = xstrdup(buf);
ret->isgood = sig->status == GPG_ERR_NO_ERROR ? 1 : 0;
ctime = gmtime((time_t *)&sig->timestamp);
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S UTC", ctime);
ret->timestamp = xstrdup(buf);
if (gpgme_get_key(g_ctx, sig->fpr, &key, 0) == GPG_ERR_NO_ERROR) {
if (key->uids != NULL)
ret->signer = xstrdup(key->uids->uid);
if (key->subkeys != NULL) {
snprintf(buf, sizeof(buf),
"%.4s %.4s %.4s %.4s %.4s "
"%.4s %.4s %.4s %.4s %.4s",
key->subkeys->fpr + 0, key->subkeys->fpr + 4,
key->subkeys->fpr + 8, key->subkeys->fpr + 12,
key->subkeys->fpr + 16, key->subkeys->fpr + 20,
key->subkeys->fpr + 24, key->subkeys->fpr + 28,
key->subkeys->fpr + 32, key->subkeys->fpr + 36);
ret->pkfingerprint = xstrdup(buf);
}
gpgme_key_release(key);
}
}
switch (sig->status) {
case GPG_ERR_NO_ERROR:
/* nothing */
ret->reason = NULL;
break;
case GPG_ERR_SIG_EXPIRED:
ret->reason = xstrdup("the signature is valid but expired");
break;
case GPG_ERR_KEY_EXPIRED:
ret->reason = xstrdup("the signature is valid but the key "
"used to verify the signature has expired");
break;
case GPG_ERR_CERT_REVOKED:
ret->reason = xstrdup("the signature is valid but the key "
"used to verify the signature has been revoked");
break;
case GPG_ERR_BAD_SIGNATURE:
free(ret);
ret = NULL;
printf("the signature is invalid\n");
break;
case GPG_ERR_NO_PUBKEY:
free(ret);
ret = NULL;
printf("the signature could not be verified due to a "
"missing key for:\n %s\n", buf);
break;
default:
free(ret);
ret = NULL;
printf("there was some error which prevented the "