-
Notifications
You must be signed in to change notification settings - Fork 10
/
incoming.c
2469 lines (2261 loc) · 67.1 KB
/
incoming.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
/* This file is part of "reprepro"
* Copyright (C) 2006,2007,2008,2009,2010 Bernhard R. Link
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include <config.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <string.h>
#include <strings.h>
#include <malloc.h>
#include <fcntl.h>
#include <signal.h>
#include <dirent.h>
#include <time.h>
#include <sys/stat.h>
#include "error.h"
#include "ignore.h"
#include "mprintf.h"
#include "filecntl.h"
#include "strlist.h"
#include "dirs.h"
#include "names.h"
#include "checksums.h"
#include "chunks.h"
#include "target.h"
#include "signature.h"
#include "binaries.h"
#include "sources.h"
#include "dpkgversions.h"
#include "uploaderslist.h"
#include "guesscomponent.h"
#include "log.h"
#include "override.h"
#include "tracking.h"
#include "incoming.h"
#include "files.h"
#include "configparser.h"
#include "byhandhook.h"
#include "changes.h"
enum permitflags {
/* do not error out on unused files */
pmf_unused_files = 0,
/* do not error out if there already is a newer package */
pmf_oldpackagenewer,
pmf_COUNT /* must be last */
};
enum cleanupflags {
/* delete everything referenced by a .changes file
* when it is not accepted */
cuf_on_deny = 0,
/* check owner when deleting on_deny */
cuf_on_deny_check_owner,
/* delete everything referenced by a .changes on errors
* after accepting that .changes file*/
cuf_on_error,
/* delete unused files after sucessfully
* processing the used ones */
cuf_unused_files,
cuf_COUNT /* must be last */
};
enum optionsflags {
/* only put _all.deb comes with those of some architecture,
* only put in those architectures */
iof_limit_arch_all = 0,
/* allow .changes file to specify multipe distributions */
iof_multiple_distributions,
iof_COUNT /* must be last */
};
struct incoming {
/* by incoming_parse: */
char *name;
char *directory;
char *morguedir;
char *tempdir;
char *logdir;
struct strlist allow;
struct distribution **allow_into;
struct distribution *default_into;
/* by incoming_prepare: */
struct strlist files;
bool *processed;
bool *delete;
bool permit[pmf_COUNT];
bool cleanup[cuf_COUNT];
bool options[iof_COUNT];
/* only to ease parsing: */
const char *filename; /* only valid while parsing! */
size_t lineno;
};
#define BASENAME(i, ofs) (i)->files.values[ofs]
/* the changes file is always the first one listed */
#define changesfile(c) (c->files)
static void incoming_free(/*@only@*/ struct incoming *i) {
if (i == NULL)
return;
free(i->name);
free(i->morguedir);
free(i->tempdir);
free(i->logdir);
free(i->directory);
strlist_done(&i->allow);
free(i->allow_into);
strlist_done(&i->files);
free(i->processed);
free(i->delete);
free(i);
}
static retvalue incoming_prepare(struct incoming *i) {
DIR *dir;
struct dirent *ent;
retvalue r;
int ret;
/* TODO: decide whether to clean this directory first ... */
r = dirs_make_recursive(i->tempdir);
if (RET_WAS_ERROR(r))
return r;
dir = opendir(i->directory);
if (dir == NULL) {
int e = errno;
fprintf(stderr, "Cannot scan '%s': %s\n",
i->directory, strerror(e));
return RET_ERRNO(e);
}
while ((ent = readdir(dir)) != NULL) {
if (ent->d_name[0] == '.')
continue;
/* this should be impossible to hit.
* but given utf-8 encoding filesystems and
* overlong slashes, better check than be sorry */
if (strchr(ent->d_name, '/') != NULL)
continue;
r = strlist_add_dup(&i->files, ent->d_name) ;
if (RET_WAS_ERROR(r)) {
(void)closedir(dir);
return r;
}
}
ret = closedir(dir);
if (ret != 0) {
int e = errno;
fprintf(stderr, "Error scanning '%s': %s\n",
i->directory, strerror(e));
return RET_ERRNO(e);
}
i->processed = nzNEW(i->files.count, bool);
if (FAILEDTOALLOC(i->processed))
return RET_ERROR_OOM;
i->delete = nzNEW(i->files.count, bool);
if (FAILEDTOALLOC(i->delete))
return RET_ERROR_OOM;
return RET_OK;
}
struct read_incoming_data {
/*@temp@*/const char *name;
/*@temp@*/struct distribution *distributions;
struct incoming *i;
};
static retvalue translate(struct distribution *distributions, struct strlist *names, struct distribution ***r) {
struct distribution **d;
int j;
d = nzNEW(names->count, struct distribution *);
if (FAILEDTOALLOC(d))
return RET_ERROR_OOM;
for (j = 0 ; j < names->count ; j++) {
d[j] = distribution_find(distributions, names->values[j]);
if (d[j] == NULL) {
free(d);
return RET_ERROR;
}
}
*r = d;
return RET_OK;
}
CFstartparse(incoming) {
CFstartparseVAR(incoming, result_p);
struct incoming *i;
i = zNEW(struct incoming);
if (FAILEDTOALLOC(i))
return RET_ERROR_OOM;
*result_p = i;
return RET_OK;
}
CFfinishparse(incoming) {
CFfinishparseVARS(incoming, i, last, d);
if (!complete || strcmp(i->name, d->name) != 0) {
incoming_free(i);
return RET_NOTHING;
}
if (d->i != NULL) {
fprintf(stderr,
"Multiple definitions of '%s': first started at line %u of %s, second at line %u of %s!\n",
d->name,
(unsigned int)d->i->lineno, d->i->filename,
config_firstline(iter), config_filename(iter));
incoming_free(i);
incoming_free(d->i);
d->i = NULL;
return RET_ERROR;
}
if (i->logdir != NULL && i->logdir[0] != '/') {
char *n = calc_dirconcat(global.basedir, i->logdir);
if (FAILEDTOALLOC(n)) {
incoming_free(i);
return RET_ERROR_OOM;
}
free(i->logdir);
i->logdir = n;
}
if (i->morguedir != NULL && i->morguedir[0] != '/') {
char *n = calc_dirconcat(global.basedir, i->morguedir);
if (FAILEDTOALLOC(n)) {
incoming_free(i);
return RET_ERROR_OOM;
}
free(i->morguedir);
i->morguedir = n;
}
if (i->tempdir[0] != '/') {
char *n = calc_dirconcat(global.basedir, i->tempdir);
if (FAILEDTOALLOC(n)) {
incoming_free(i);
return RET_ERROR_OOM;
}
free(i->tempdir);
i->tempdir = n;
}
if (i->directory[0] != '/') {
char *n = calc_dirconcat(global.basedir, i->directory);
if (FAILEDTOALLOC(n)) {
incoming_free(i);
return RET_ERROR_OOM;
}
free(i->directory);
i->directory = n;
}
if (i->default_into == NULL && i->allow.count == 0) {
fprintf(stderr,
"There is neither an 'Allow' nor a 'Default' definition in rule '%s'\n"
"(starting at line %u, ending at line %u of %s)!\n"
"Aborting as nothing would be let in.\n",
d->name,
config_firstline(iter), config_line(iter),
config_filename(iter));
incoming_free(i);
return RET_ERROR;
}
if (i->morguedir != NULL && !i->cleanup[cuf_on_deny]
&& !i->cleanup[cuf_on_error]
&& !i->cleanup[cuf_unused_files]) {
fprintf(stderr,
"Warning: There is a 'MorgueDir' but no 'Cleanup' to act on in rule '%s'\n"
"(starting at line %u, ending at line %u of %s)!\n",
d->name,
config_firstline(iter), config_line(iter),
config_filename(iter));
}
d->i = i;
i->filename = config_filename(iter);
i->lineno = config_firstline(iter);
/* only suppreses the last unused warning: */
*last = i;
return RET_OK;
}
CFSETPROC(incoming, default) {
CFSETPROCVARS(incoming, i, d);
char *default_into;
retvalue r;
r = config_getonlyword(iter, headername, NULL, &default_into);
assert (r != RET_NOTHING);
if (RET_WAS_ERROR(r))
return r;
i->default_into = distribution_find(d->distributions, default_into);
free(default_into);
return (i->default_into == NULL)?RET_ERROR:RET_OK;
}
CFSETPROC(incoming, allow) {
CFSETPROCVARS(incoming, i, d);
struct strlist allow_into;
retvalue r;
r = config_getsplitwords(iter, headername, &i->allow, &allow_into);
assert (r != RET_NOTHING);
if (RET_WAS_ERROR(r))
return r;
assert (i->allow.count == allow_into.count);
r = translate(d->distributions, &allow_into, &i->allow_into);
strlist_done(&allow_into);
if (RET_WAS_ERROR(r))
return r;
return RET_OK;
}
CFSETPROC(incoming, permit) {
CFSETPROCVARS(incoming, i, d);
static const struct constant const permitconstants[] = {
{ "unused_files", pmf_unused_files},
{ "older_version", pmf_oldpackagenewer},
/* not yet implemented:
{ "downgrade", pmf_downgrade},
*/
{ NULL, -1}
};
if (IGNORABLE(unknownfield))
return config_getflags(iter, headername, permitconstants,
i->permit, true, "");
else if (i->name == NULL)
return config_getflags(iter, headername, permitconstants,
i->permit, false,
"\n(try put Name: before Permit: to ignore if it is from the wrong rule");
else if (strcmp(i->name, d->name) != 0)
return config_getflags(iter, headername, permitconstants,
i->permit, true,
" (but not within the rule we are intrested in.)");
else
return config_getflags(iter, headername, permitconstants,
i->permit, false,
" (use --ignore=unknownfield to ignore this)\n");
}
CFSETPROC(incoming, cleanup) {
CFSETPROCVARS(incoming, i, d);
static const struct constant const cleanupconstants[] = {
{ "unused_files", cuf_unused_files},
{ "on_deny", cuf_on_deny},
/* not yet implemented
{ "on_deny_check_owner", cuf_on_deny_check_owner},
*/
{ "on_error", cuf_on_error},
{ NULL, -1}
};
if (IGNORABLE(unknownfield))
return config_getflags(iter, headername, cleanupconstants,
i->cleanup, true, "");
else if (i->name == NULL)
return config_getflags(iter, headername, cleanupconstants,
i->cleanup, false,
"\n(try put Name: before Cleanup: to ignore if it is from the wrong rule");
else if (strcmp(i->name, d->name) != 0)
return config_getflags(iter, headername, cleanupconstants,
i->cleanup, true,
" (but not within the rule we are intrested in.)");
else
return config_getflags(iter, headername, cleanupconstants,
i->cleanup, false,
" (use --ignore=unknownfield to ignore this)\n");
}
CFSETPROC(incoming, options) {
CFSETPROCVARS(incoming, i, d);
static const struct constant const optionsconstants[] = {
{ "limit_arch_all", iof_limit_arch_all},
{ "multiple_distributions", iof_multiple_distributions},
{ NULL, -1}
};
if (IGNORABLE(unknownfield))
return config_getflags(iter, headername, optionsconstants,
i->options, true, "");
else if (i->name == NULL)
return config_getflags(iter, headername, optionsconstants,
i->options, false,
"\n(try put Name: before Options: to ignore if it is from the wrong rule");
else if (strcmp(i->name, d->name) != 0)
return config_getflags(iter, headername, optionsconstants,
i->options, true,
" (but not within the rule we are intrested in.)");
else
return config_getflags(iter, headername, optionsconstants,
i->options, false,
" (use --ignore=unknownfield to ignore this)\n");
}
CFvalueSETPROC(incoming, name)
CFdirSETPROC(incoming, logdir)
CFdirSETPROC(incoming, tempdir)
CFdirSETPROC(incoming, morguedir)
CFdirSETPROC(incoming, directory)
CFtruthSETPROC2(incoming, multiple, options[iof_multiple_distributions])
static const struct configfield incomingconfigfields[] = {
CFr("Name", incoming, name),
CFr("TempDir", incoming, tempdir),
CFr("IncomingDir", incoming, directory),
CF("MorgueDir", incoming, morguedir),
CF("Default", incoming, default),
CF("Allow", incoming, allow),
CF("Multiple", incoming, multiple),
CF("Options", incoming, options),
CF("Cleanup", incoming, cleanup),
CF("Permit", incoming, permit),
CF("Logdir", incoming, logdir)
};
static retvalue incoming_init(struct distribution *distributions, const char *name, /*@out@*/struct incoming **result) {
retvalue r;
struct read_incoming_data imports;
imports.name = name;
imports.distributions = distributions;
imports.i = NULL;
r = configfile_parse("incoming", IGNORABLE(unknownfield),
startparseincoming, finishparseincoming,
"incoming rule",
incomingconfigfields, ARRAYCOUNT(incomingconfigfields),
&imports);
if (RET_WAS_ERROR(r))
return r;
if (imports.i == NULL) {
fprintf(stderr,
"No definition for '%s' found in '%s/incoming'!\n",
name, global.confdir);
return RET_ERROR_MISSING;
}
r = incoming_prepare(imports.i);
if (RET_WAS_ERROR(r)) {
incoming_free(imports.i);
return r;
}
*result = imports.i;
return r;
}
struct candidate {
/* from candidate_read */
int ofs;
char *control;
struct signatures *signatures;
/* from candidate_parse */
char *source, *sourceversion, *changesversion;
struct strlist distributions,
architectures,
binaries;
bool isbinNMU;
struct candidate_file {
/* set by _addfileline */
struct candidate_file *next;
int ofs; /* to basename in struct incoming->files */
filetype type;
/* all NULL if it is the .changes itself,
* otherwise the data from the .changes for this file: */
char *section;
char *priority;
architecture_t architecture;
char *name;
/* like above, but updated once files are copied */
struct checksums *checksums;
/* set later */
bool used;
char *tempfilename;
/* distribution-unspecific contents of the packages */
/* - only for FE_BINARY types: */
struct deb_headers deb;
/* - only for fe_DSC types */
struct dsc_headers dsc;
/* only valid while parsing */
struct hashes h;
} *files;
struct candidate_perdistribution {
struct candidate_perdistribution *next;
struct distribution *into;
bool skip;
struct candidate_package {
/* a package is something installing files, including
* the pseudo-package for the .changes file, if that is
* to be included */
struct candidate_package *next;
const struct candidate_file *master;
component_t component;
packagetype_t packagetype;
struct strlist filekeys;
/* a list of pointers to the files belonging to those
* filekeys, NULL if it does not need linking/copying */
const struct candidate_file **files;
/* only for FE_PACKAGE: */
char *control;
/* only for fe_DSC */
char *directory;
/* true if skipped because already there or newer */
bool skip;
} *packages;
struct byhandfile {
struct byhandfile *next;
const struct candidate_file *file;
const struct byhandhook *hook;
} *byhandhookstocall;
} *perdistribution;
/* the logsubdir, and the list of files to put there,
* otherwise both NULL */
char *logsubdir;
int logcount;
const struct candidate_file **logfiles;
};
static void candidate_file_free(/*@only@*/struct candidate_file *f) {
checksums_free(f->checksums);
free(f->section);
free(f->priority);
free(f->name);
if (FE_BINARY(f->type))
binaries_debdone(&f->deb);
if (f->type == fe_DSC)
sources_done(&f->dsc);
if (f->tempfilename != NULL) {
(void)unlink(f->tempfilename);
free(f->tempfilename);
f->tempfilename = NULL;
}
free(f);
}
static void candidate_package_free(/*@only@*/struct candidate_package *p) {
free(p->control);
free(p->directory);
strlist_done(&p->filekeys);
free(p->files);
free(p);
}
static void candidate_free(/*@only@*/struct candidate *c) {
if (c == NULL)
return;
free(c->control);
signatures_free(c->signatures);
free(c->source);
free(c->sourceversion);
free(c->changesversion);
strlist_done(&c->distributions);
strlist_done(&c->architectures);
strlist_done(&c->binaries);
while (c->perdistribution != NULL) {
struct candidate_perdistribution *d = c->perdistribution;
c->perdistribution = d->next;
while (d->packages != NULL) {
struct candidate_package *p = d->packages;
d->packages = p->next;
candidate_package_free(p);
}
while (d->byhandhookstocall != NULL) {
struct byhandfile *h = d->byhandhookstocall;
d->byhandhookstocall = h->next;
free(h);
}
free(d);
}
while (c->files != NULL) {
struct candidate_file *f = c->files;
c->files = f->next;
candidate_file_free(f);
}
free(c->logsubdir);
free(c->logfiles);
free(c);
}
static retvalue candidate_newdistribution(struct candidate *c, struct distribution *distribution) {
struct candidate_perdistribution *n, **pp = &c->perdistribution;
while (*pp != NULL) {
if ((*pp)->into == distribution)
return RET_NOTHING;
pp = &(*pp)->next;
}
n = zNEW(struct candidate_perdistribution);
if (FAILEDTOALLOC(n))
return RET_ERROR_OOM;
n->into = distribution;
*pp = n;
return RET_OK;
}
static struct candidate_package *candidate_newpackage(struct candidate_perdistribution *fordistribution, const struct candidate_file *master) {
struct candidate_package *n, **pp = &fordistribution->packages;
while (*pp != NULL)
pp = &(*pp)->next;
n = zNEW(struct candidate_package);
if (FAILEDTOALLOC(n))
return NULL;
n->component = atom_unknown;
n->packagetype = atom_unknown;
n->master = master;
*pp = n;
return n;
}
static retvalue candidate_usefile(const struct incoming *i, const struct candidate *c, struct candidate_file *file);
static retvalue candidate_read(struct incoming *i, int ofs, struct candidate **result, bool *broken) {
struct candidate *n;
retvalue r;
n = zNEW(struct candidate);
if (FAILEDTOALLOC(n))
return RET_ERROR_OOM;
n->ofs = ofs;
/* first file of any .changes file is the file itself */
n->files = zNEW(struct candidate_file);
if (FAILEDTOALLOC(n->files)) {
free(n);
return RET_ERROR_OOM;
}
n->files->ofs = n->ofs;
n->files->type = fe_CHANGES;
r = candidate_usefile(i, n, n->files);
assert (r != RET_NOTHING);
if (RET_WAS_ERROR(r)) {
candidate_free(n);
return r;
}
assert (n->files->tempfilename != NULL);
r = signature_readsignedchunk(n->files->tempfilename, BASENAME(i, ofs),
&n->control, &n->signatures, broken);
assert (r != RET_NOTHING);
if (RET_WAS_ERROR(r)) {
candidate_free(n);
return r;
}
*result = n;
return RET_OK;
}
static retvalue candidate_addfileline(struct incoming *i, struct candidate *c, const char *fileline) {
struct candidate_file **p, *n;
char *basefilename;
retvalue r;
n = zNEW(struct candidate_file);
if (FAILEDTOALLOC(n))
return RET_ERROR_OOM;
r = changes_parsefileline(fileline, &n->type, &basefilename,
&n->h.hashes[cs_md5sum], &n->h.hashes[cs_length],
&n->section, &n->priority, &n->architecture,
&n->name);
if (RET_WAS_ERROR(r)) {
free(n);
return r;
}
n->ofs = strlist_ofs(&i->files, basefilename);
if (n->ofs < 0) {
fprintf(stderr,
"In '%s': file '%s' not found in the incoming dir!\n",
i->files.values[c->ofs], basefilename);
free(basefilename);
candidate_file_free(n);
return RET_ERROR_MISSING;
}
free(basefilename);
p = &c->files;
while (*p != NULL)
p = &(*p)->next;
*p = n;
return RET_OK;
}
static retvalue candidate_addhashes(struct incoming *i, struct candidate *c, enum checksumtype cs, const struct strlist *lines) {
int j;
for (j = 0 ; j < lines->count ; j++) {
const char *fileline = lines->values[j];
struct candidate_file *f;
const char *basefilename;
struct hash_data hash, size;
retvalue r;
r = hashline_parse(BASENAME(i, c->ofs), fileline, cs,
&basefilename, &hash, &size);
if (!RET_IS_OK(r))
return r;
f = c->files;
while (f != NULL && strcmp(BASENAME(i, f->ofs), basefilename) != 0)
f = f->next;
if (f == NULL) {
fprintf(stderr,
"Warning: Ignoring file '%s' listed in '%s' but not in '%s' of '%s'!\n",
basefilename, changes_checksum_names[cs],
changes_checksum_names[cs_md5sum],
BASENAME(i, c->ofs));
continue;
}
if (f->h.hashes[cs_length].len != size.len ||
memcmp(f->h.hashes[cs_length].start,
size.start, size.len) != 0) {
fprintf(stderr,
"Error: Different size of '%s' listed in '%s' and '%s' of '%s'!\n",
basefilename, changes_checksum_names[cs],
changes_checksum_names[cs_md5sum],
BASENAME(i, c->ofs));
return RET_ERROR;
}
f->h.hashes[cs] = hash;
}
return RET_OK;
}
static retvalue candidate_finalizechecksums(struct candidate *c) {
struct candidate_file *f;
retvalue r;
/* store collected hashes as checksums structs,
* starting after .changes file: */
for (f = c->files->next ; f != NULL ; f = f->next) {
r = checksums_initialize(&f->checksums, f->h.hashes);
if (RET_WAS_ERROR(r))
return r;
}
return RET_OK;
}
static retvalue candidate_parse(struct incoming *i, struct candidate *c) {
retvalue r;
struct strlist filelines[cs_hashCOUNT];
enum checksumtype cs;
int j;
#define R if (RET_WAS_ERROR(r)) return r;
#define E(err, ...) { \
if (r == RET_NOTHING) { \
fprintf(stderr, "In '%s': " err "\n", \
BASENAME(i, c->ofs), ## __VA_ARGS__); \
r = RET_ERROR; \
} \
if (RET_WAS_ERROR(r)) return r; \
}
r = chunk_getnameandversion(c->control, "Source", &c->source,
&c->sourceversion);
E("Missing 'Source' field!");
r = propersourcename(c->source);
E("Malforce Source name!");
if (c->sourceversion != NULL) {
r = properversion(c->sourceversion);
E("Malforce Source Version number!");
}
r = chunk_getwordlist(c->control, "Binary", &c->binaries);
E("Missing 'Binary' field!");
r = chunk_getwordlist(c->control, "Architecture", &c->architectures);
E("Missing 'Architecture' field!");
r = chunk_getvalue(c->control, "Version", &c->changesversion);
E("Missing 'Version' field!");
r = properversion(c->changesversion);
E("Malforce Version number!");
// TODO: logic to detect binNMUs to warn against sources?
if (c->sourceversion == NULL) {
c->sourceversion = strdup(c->changesversion);
if (FAILEDTOALLOC(c->sourceversion))
return RET_ERROR_OOM;
c->isbinNMU = false;
} else {
int cmp;
r = dpkgversions_cmp(c->sourceversion, c->changesversion, &cmp);
R;
c->isbinNMU = cmp != 0;
}
r = chunk_getwordlist(c->control, "Distribution", &c->distributions);
E("Missing 'Distribution' field!");
r = chunk_getextralinelist(c->control,
changes_checksum_names[cs_md5sum],
&filelines[cs_md5sum]);
E("Missing '%s' field!", changes_checksum_names[cs_md5sum]);
for (j = 0 ; j < filelines[cs_md5sum].count ; j++) {
r = candidate_addfileline(i, c, filelines[cs_md5sum].values[j]);
if (RET_WAS_ERROR(r)) {
strlist_done(&filelines[cs_md5sum]);
return r;
}
}
for (cs = cs_firstEXTENDED ; cs < cs_hashCOUNT ; cs++) {
r = chunk_getextralinelist(c->control,
changes_checksum_names[cs], &filelines[cs]);
if (RET_IS_OK(r))
r = candidate_addhashes(i, c, cs, &filelines[cs]);
else
strlist_init(&filelines[cs]);
if (RET_WAS_ERROR(r)) {
while (cs-- > cs_md5sum)
strlist_done(&filelines[cs]);
return r;
}
}
r = candidate_finalizechecksums(c);
for (cs = cs_md5sum ; cs < cs_hashCOUNT ; cs++)
strlist_done(&filelines[cs]);
R;
if (c->files == NULL || c->files->next == NULL) {
fprintf(stderr, "In '%s': Empty 'Files' section!\n",
BASENAME(i, c->ofs));
return RET_ERROR;
}
return RET_OK;
}
static retvalue candidate_earlychecks(struct incoming *i, struct candidate *c) {
struct candidate_file *file;
retvalue r;
// TODO: allow being more permissive,
// that will need some more checks later, though
r = propersourcename(c->source);
if (RET_WAS_ERROR(r))
return r;
r = properversion(c->sourceversion);
if (RET_WAS_ERROR(r))
return r;
for (file = c->files ; file != NULL ; file = file->next) {
if (file->type != fe_CHANGES && file->type != fe_BYHAND &&
file->type != fe_LOG &&
!atom_defined(file->architecture)) {
fprintf(stderr,
"'%s' contains '%s' not matching an valid architecture in any distribution known!\n",
BASENAME(i, c->ofs),
BASENAME(i, file->ofs));
return RET_ERROR;
}
if (!FE_PACKAGE(file->type))
continue;
assert (atom_defined(file->architecture));
if (strlist_in(&c->architectures,
atoms_architectures[file->architecture]))
continue;
fprintf(stderr,
"'%s' is not listed in the Architecture header of '%s' but file '%s' looks like it!\n",
atoms_architectures[file->architecture],
BASENAME(i, c->ofs), BASENAME(i, file->ofs));
return RET_ERROR;
}
return RET_OK;
}
/* Is used before any other candidate fields are set */
static retvalue candidate_usefile(const struct incoming *i, const struct candidate *c, struct candidate_file *file) {
const char *basefilename;
char *origfile, *tempfilename;
struct checksums *readchecksums;
retvalue r;
bool improves;
const char *p;
if (file->used && file->tempfilename != NULL)
return RET_OK;
assert(file->tempfilename == NULL);
basefilename = BASENAME(i, file->ofs);
for (p = basefilename; *p != '\0' ; p++) {
if ((0x80 & *(const unsigned char *)p) != 0) {
fprintf(stderr,
"Invalid filename '%s' listed in '%s': contains 8-bit characters\n",
basefilename, BASENAME(i, c->ofs));
return RET_ERROR;
}
}
tempfilename = calc_dirconcat(i->tempdir, basefilename);
if (FAILEDTOALLOC(tempfilename))
return RET_ERROR_OOM;
origfile = calc_dirconcat(i->directory, basefilename);
if (FAILEDTOALLOC(origfile)) {
free(tempfilename);
return RET_ERROR_OOM;
}
r = checksums_copyfile(tempfilename, origfile, true, &readchecksums);
free(origfile);
if (RET_WAS_ERROR(r)) {
free(tempfilename);
return r;
}
if (file->checksums == NULL) {
file->checksums = readchecksums;
file->tempfilename = tempfilename;
file->used = true;
return RET_OK;
}
if (!checksums_check(file->checksums, readchecksums, &improves)) {
fprintf(stderr,
"ERROR: File '%s' does not match expectations:\n",
basefilename);
checksums_printdifferences(stderr,
file->checksums, readchecksums);
checksums_free(readchecksums);
deletefile(tempfilename);
free(tempfilename);
return RET_ERROR_WRONG_MD5;
}
if (improves) {
r = checksums_combine(&file->checksums, readchecksums, NULL);
if (RET_WAS_ERROR(r)) {
checksums_free(readchecksums);
deletefile(tempfilename);
free(tempfilename);
return r;
}
}
checksums_free(readchecksums);
file->tempfilename = tempfilename;
file->used = true;
return RET_OK;
}
static inline retvalue getsectionprioritycomponent(const struct incoming *i, const struct candidate *c, const struct distribution *into, const struct candidate_file *file, const char *name, const struct overridedata *oinfo, /*@out@*/const char **section_p, /*@out@*/const char **priority_p, /*@out@*/component_t *component) {
retvalue r;
const char *section, *priority, *forcecomponent;
component_t fc;
section = override_get(oinfo, SECTION_FIELDNAME);
if (section == NULL) {
// TODO: warn about disparities here?
section = file->section;
}
if (section == NULL || strcmp(section, "-") == 0) {
fprintf(stderr, "No section found for '%s' ('%s' in '%s')!\n",
name,
BASENAME(i, file->ofs), BASENAME(i, c->ofs));
return RET_ERROR;
}
priority = override_get(oinfo, PRIORITY_FIELDNAME);
if (priority == NULL) {
// TODO: warn about disparities here?
priority = file->priority;
}
if (priority == NULL || strcmp(priority, "-") == 0) {
fprintf(stderr, "No priority found for '%s' ('%s' in '%s')!\n",
name,
BASENAME(i, file->ofs), BASENAME(i, c->ofs));
return RET_ERROR;
}
forcecomponent = override_get(oinfo, "$Component");
if (forcecomponent != NULL) {
fc = component_find(forcecomponent);
if (!atom_defined(fc)) {
fprintf(stderr,
"Unknown component '%s' (in $Component in override file for '%s'\n",
forcecomponent, name);
return RET_ERROR;
}
/* guess_component will check if that is valid for this
* distribution */
} else
fc = atom_unknown;
r = guess_component(into->codename, &into->components,
BASENAME(i, file->ofs), section,
fc, component);
if (RET_WAS_ERROR(r)) {
return r;
}
*section_p = section;
*priority_p = priority;
return RET_OK;
}
static retvalue candidate_read_deb(struct incoming *i, struct candidate *c, struct candidate_file *file) {
retvalue r;
r = binaries_readdeb(&file->deb, file->tempfilename, true);
if (RET_WAS_ERROR(r))
return r;
if (strcmp(file->name, file->deb.name) != 0) {
// TODO: add permissive thing to ignore this
fprintf(stderr,
"Name part of filename ('%s') and name within the file ('%s') do not match for '%s' in '%s'!\n",
file->name, file->deb.name,
BASENAME(i, file->ofs), BASENAME(i, c->ofs));