-
Notifications
You must be signed in to change notification settings - Fork 25
/
ya-errata-import.pl
executable file
·1364 lines (1238 loc) · 49.5 KB
/
ya-errata-import.pl
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
#!/usr/bin/perl
# This script imports CentOS or RedHat Errata into your Spacewalk
# It relies on preformatted information since parsing email
# is the road to madness...
#
# To run this script you need perl-Text-Unidecode
#
# Based on: centos-errata-import.pl and eva-direct-errata-sync.pl
# Authors: Steve Meier (centos-errata-import.pl)
# Paul Roberts (eva-direct-errata-sync.pl)
# Author: Franky Van Liedekerke
# 20121010 - add option to use RHN to get details
# - only consider one channel and architecture at a time, to avoid
# redhat/centos issues and cross-basechannel copies of packages
# - add support for redhat errata and servers
# - time ranges
# 20130125 - added commandline options for usernames and pwds
# 20130xxx - added support for Oracle Linux
# 20130424 - added support for Scientific Linux (thanks to Bryan Casto)
# 20130630 - Fix for multiple OS messages in one CentOS digest with the same advisory ID
# 20130726 - Support for spacewalk API 2.0
# 20130818 - disable ssl cert verification for newer versions of libwww
# - session logout when connected to redhat
# 20130905 - better XML parsing
# 20130911 - CentOS Xen errata parsing corrected
# 20130925 - Catch a hashref error
# - some better debug output
# 20140108 - Catch more rhn errors
# 20140304 - Support Centos Software Collections errata
# 20140727 - Support Spacewalk 2.2
# 20140918 - Better centos digest parsing
# 20150303 - Again more resilient centos message parsing
# 20150813 - Support IBM-Z series s390x architecture
# 20150817 - Make sure Net:ssl is used
# 20160401 - Show redhat channels found in the debug output
# 20161221 - Support Spacewalk API 2.6
# 20170930 - Support Spacewalk API 2.7
# 20180627 - Support Spacewalk API 2.8
# Load modules
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use Text::Unidecode;
use Frontier::Client;
use XML::Simple;
use Time::Local;
#######################################################################
### GLOBAL VARIABLES
#######################################################################
# Version information
my $version = "20180627";
my @supportedapi = ( '10.9','10.11','11.00','11.1','12','13','14','15','16','17','19','20','21' );
# Just to be sure: disable SSL certificate verification for libwww>6.0
$ENV{PERL_NET_HTTPS_SSL_SOCKET_CLASS} = "Net::SSL";
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
# Spacewalk Version => API cheatsheet
# 0.6 => 10.9 == TESTED
# 0.7 => ??
# 0.8 => ??
# 1.0 => 10.11
# 1.1 => 10.11 == TESTED
# 1.2 => 10.15
# 1.3 => ??
# 1.4 => 10.16
# 1.5 => 11.00 == TESTED
# 1.6 => 11.1 == TESTED
# 1.7 => 11.1 == TESTED
# 1.9 => 12 == TESTED
# 2.0 => 13 == TESTED
# 2.6 => 19 == TESTED
# Variable declaration
$| = 1;
my ($client,$rhn_client);
my $apiversion;
my $apisupport = 0;
my ($xml, $rhsaxml);
my ($session, $username, $password,$rhn_session,$rhn_username,$rhn_password);
#my %name2channel;
my %name2id;
my %existing_errata;
my ($channellist, $channel, @excludechannels);
my ($channeldetails, $lastmodified, $trackmodified, $lastsync, $synctimestamp);
my ($reference, %erratadetails, %erratainfo);
my $result;
my $undopush;
my ($pkg, $pkgdetails, $package);
my ($advisory, $ovalid);
my $getdetails;
my $os_variant; # One of 'C' for CentOS, 'R' for RedHat, or 'S' for Scientific
my $opt_syncchannels = 0;
my $opt_synccounter = 0;
my $opt_synctimeout = 600;
my $opt_channel;
my $opt_rhsaovalfile="";
my $opt_erratadir;
my $opt_server;
my $opt_publish = 0; # do not publish by default
my $opt_get_from_rhn = 0;
my $opt_security = 0;
my $opt_bugfix = 0;
my $opt_enhancement = 0;
my $opt_debug = 0;
my $opt_quiet = 0;
my $opt_os_version = 0;
my $opt_help;
my $opt_testrun;
my $opt_autopush = 0;
my $opt_architecture;
my $opt_redhat = 0;
my $opt_rhn_proxy="";
my $opt_rhn_server="rhn.redhat.com";
my $opt_bugzilla_url="https://bugzilla.redhat.com/";
my $opt_proxy="";
my $opt_startfromprevious;
my $opt_startdate;
my $opt_enddate;
my $opt_redhat_channel;
my $opt_spacewalk_user;
my $opt_spacewalk_pwd;
my $opt_rhn_user;
my $opt_rhn_pwd;
my $opt_epel_erratafile;
my $opt_oel_erratafile;
my $opt_sl_erratafile;
my $opt_suffix;
#######################################################################
### PROCEDURES
#######################################################################
sub debug($) {
print "DEBUG: @_" if ($opt_debug);
}
sub info($) {
print "INFO: @_" if (!$opt_quiet);
}
sub warning($) {
print "WARNING: @_";
}
sub error($) {
print "ERROR: @_";
}
sub usage() {
print "Script to clone errata for CentOS or RedHat into Spacewalk\n";
print "\nUsernames and passwords are best taken from the environment\n";
print "variables SPACEWALK_USER, SPACEWALK_PASS, RHN_USER, RHN_PASS\n";
print "You can also provide them using the available options (see below),\n";
print "if you do so, the commandline options take precedence over the ENV variables\n";
print "Otherwise you'll be asked for it if not given but needed.\n\n";
print "Usage: $0 --server <SERVER> --erratadir <ERRATA-DIR> \n";
print " --channel=<CHANNEL> --os-version <VERSION>\n";
print " [ --rhsa-oval <REDHAT-OVAL-XML> |\n";
# print " --sync-channels | --sync-timeout=<TIMEOUT> |\n";
print " --bugfix | --security | --enhancement |\n";
# print " --autopush ]\n";
print " --epel_errata <PATH to EPEL updateinfo.xml> |\n";
print " --oel_errata <PATH to OEL updateinfo.xml> |\n";
print " --sl_errata <PATH to SL updateinfo.xml> |\n";
print " --publish | --quiet | --get-from-rhn | -- architecture <ARCH> |\n";
print " --rhn-proxy <PROXY> | --rhn-server <RHNSERVER> |\n";
print " --spacewalk-user <USER> | --spacewalk-pass <PWD> |\n";
print " --rhn-user <RHNUSER> | --rhn-pass <RHNPWD> |\n";
print " --proxy <PROXY> | --bugzilla-url <URL> |\n";
print " --suffix <SUFFIX> |\n";
print " --help | --testrun | --debug ]\n";
print "\n";
print "REQUIRED:\n";
print " --server\t\tThe hostname or IP address of your spacewalk server\n";
print " --channel\t\tThe spacewalk channel you want to publish errata to\n";
print " --os-version\t\tThe OS major version we're dealing with, used as part of the errata advisory name\n";
print "\n";
print "REQUIRED for CentOS errata:\n";
print " --erratadir\t\tThe dir containing CentOS errata announcement digest archives\n";
print " \t\tSee the accompanying file centos-clone-errata.sh for an example on how to use this\n";
print "\n";
print "REQUIRED for EPEL errata:\n";
print " --epel_errata\t\tPath to the unzipped updateinfo.xml file\n";
print "\t\t\tIf using epel errata, use the --redhat option to indicate that the spacewalk channel\n";
print "\t\t\tyou're pushing erratas in, is a redhat based channel, this just influences the name of the errata in spacewalk\n";
print "\n";
print "REQUIRED for OEL errata:\n";
print " --oel_errata\t\tPath to the unzipped updateinfo.xml file\n";
print "\n";
print "REQUIRED for SL errata:\n";
print " --sl_errata\t\tPath to the unzipped updateinfo.xml file\n";
print "\n";
print "OPTIONAL:\n";
print " --quiet\t\tSuppresses all informational messages\n";
# print " --sync-channels\tSync channel with associated repository before scanning\n";
# print " --sync-timeout\tAbort sync after n seconds stalled (default: 600)\n";
# print " --autopush\t\tAllow server to copy packages around (NOT recommended)\n";
print " --publish\t\tPublish errata after creation (default: unpublished)\n";
print " --architecture\tEither 'i386','x86_64','s390x' or not specified, in which case the architecture of the channel will be taken\n";
print " --bugfix\t\tImport only Bug Fix Advisories (default: all)\n";
print " --security\t\tImport only Security Advisories (default: all)\n";
print " --enhancement\t\tImport only Enhancement Advisories (default: all)\n";
print " --rhn-proxy\t\tProxy needed to connect to RHN network\n";
print " --rhn-server\t\tRHN server (defaults to $opt_rhn_server)\n";
print " --proxy\t\tProxy to connect to spacewalk server\n";
print " --bugzilla-url\tPrefix for errata bugfix url, defaults to '$opt_bugzilla_url'\n";
print " --spacewalk-user\tthe username to connect to spacewalk (see also comments at the top)\n";
print " --spacewalk-pass\tthe password to connect to spacewalk (see also comments at the top)\n";
print " --rhn-user\t\tthe username to connect to RHN (see also comments at the top)\n";
print " --rhn-pass\t\tthe password to connect to RHN (see also comments at the top)\n";
print " --suffix\t\tAn optional suffix to the advisory name in spacewalk\n";
print "\n";
print "OPTIONAL for CentOS errata:\n";
print " --rhsa-oval\t\tOVAL XML file from Red Hat\n";
print " --get-from-rhn\tIndicate that you want to get extra info from RHN for CentOS errata\n";
print "\n";
print "OPTIONAL for RedHat errata:\n";
print " --redhat\t\tUse RedHat as source for errata, if not set CentOS is assumed\n";
print "\t\t\tSee the remark above when using the --epel_errata option\n";
print " --redhat-channel\tRedHat channel to get errata from, only usefull with --redhat, defaults to the same as --channel\n";
print "\n";
print "OPTIONAL for RedHat or EPEL errata:\n";
print " --startdate\tThe start date for the errata\n";
print " --startfromprevious\tStart from previous hour,day,week,twoweeks,month,year (these are the possible values)\n";
print " --enddate\tThe end date for the errata\n";
print "\n";
print "DEBUGGING:\n";
print " --debug\t\tSet verbosity to debug (use this when reporting issues!)\n";
print " --testrun\t\tWill print the errata details that would be created, but will not create anything in spacewalk\n";
print "\n";
}
sub uniq(@) {
my %all = ();
@all{@_} = 1;
return (keys %all);
}
sub to_epoch ($) {
my ($year, $month, $day, $hour, $min, $sec) = split /\W+/, $_[0];
return timelocal($sec,$min,$hour,$day,$month-1,$year-1900);
}
sub api_sanitize ($) {
my $tmp_text=$_[0];
# Remove Umlauts -- API throws errors if they are included
$tmp_text = unidecode($tmp_text);
# Limit to length of 4000 bytes (see https://www.redhat.com/archives/spacewalk-list/2012-June/msg00128.html)
if (length($tmp_text) >= 4000) {
$tmp_text = substr($tmp_text, 0, 4000);
}
return $tmp_text;
}
sub parse_updatexml($) {
my $xml;
my $updatexml;
my $update_xml_file=$_[0];
if (!-r "$update_xml_file") {
die "Can't open XML Update file $update_xml_file\n";
}
info("Loading XML Update file $update_xml_file...\n");
if (not($updatexml = XMLin($update_xml_file, ForceArray => [ 'reference', 'package' ], KeyAttr => [''] ))) {
error("Could not parse XML Update file!\n");
exit 4;
}
debug("XML Update file loaded successfully\n");
foreach my $errata (@{$updatexml->{'update'}}) {
my $advid = $errata->{'id'};
my $errata_type;
# It's possible that the errata naming convention excludes the expected SA|BA|EA keywords
# Which means the earlier pre-filter for optimization may not have worked
# So double check that we intend to process this errata type
if ($opt_security || $opt_bugfix || $opt_enhancement) {
if ( ($errata->{'type'} eq "security") && (not($opt_security)) ) {
debug("Skipping $advid. Security Errata not selected.\n");
next;
}
if ( ($errata->{'type'} eq "bugfix") && (not($opt_bugfix)) ) {
debug("Skipping $advid. Bugfix Errata not selected.\n");
next;
}
if ( ($errata->{'type'} eq "enhancement") && (not($opt_enhancement)) ) {
debug("Skipping $advid. Enhancement Errata not selected.\n");
next;
}
}
if ($errata->{'type'} eq "bugfix") {
$errata_type="Bug Fix Advisory";
} elsif ($errata->{'type'} eq "security") {
$errata_type="Security Advisory";
} elsif ($errata->{'type'} eq "enhancement") {
$errata_type="Product Enhancement Advisory";
} else {
next;
}
my $errata_time = to_epoch($errata->{'issued'}->{'date'});
if (defined($opt_startfromprevious)) {
my $startdate=get_previous_startdate($opt_startfromprevious);
if ($errata_time<to_epoch($startdate)) {
next;
}
} elsif (defined($opt_startdate) && defined($opt_enddate)) {
if ($errata_time<to_epoch($opt_startdate) || $errata_time>to_epoch($opt_enddate)) {
next;
}
} elsif (defined($opt_startdate)) {
if ($errata_time<to_epoch($opt_startdate)) {
next;
}
}
my @packages;
foreach my $pkg_info (@{$errata->{'pkglist'}->{'collection'}->{'package'}}) {
# Sometimes, there is only one package and they didn't bother to put it correctly in the XML as a subhash
if (ref($pkg_info) ne "HASH") {
$pkg_info=$errata->{'pkglist'}->{'collection'}->{'package'};
}
# we don't care about src or debug rpm
if ($pkg_info->{'arch'} ne 'src' && $pkg_info->{'filename'} !~ /src\.rpm$|debuginfo/) {
my $pkg_filename = $pkg_info->{'filename'};
# noarch rpm are for all architectures
# for the rest the arch in the errata should be equal to the arch desired
# unless for x86_64, there you can also have i686 rpms
if ($pkg_info->{'arch'} eq 'noarch' || $pkg_info->{'arch'} eq ${opt_architecture} ||
(${opt_architecture} eq "x86_64" && $pkg_info->{'arch'} eq "i686")) {
push (@packages,$pkg_filename);
debug("Errata $advid has package $pkg_filename\n");
}
}
}
if (!@packages) {
debug("Skipping $advid, no packages for the corresponding arch\n");
next;
}
$xml->{$advid}={};
$xml->{$advid}->{'type'}=$errata_type;
$xml->{$advid}->{${opt_architecture}.'_packages'}=\@packages;
$xml->{$advid}->{'synopsis'}=$errata->{'title'};
$xml->{$advid}->{'release'}=$errata->{'version'};
$xml->{$advid}->{'advisory_name'}=$advid;
$xml->{$advid}->{'product'}=$errata->{'release'};
$xml->{$advid}->{'topic'}=$errata->{'description'};
$xml->{$advid}->{'description'}=$errata->{'description'};
$xml->{$advid}->{'solution'}="not available";
$xml->{$advid}->{'notes'}="not available";
$xml->{$advid}->{'references'}='';
my $bugs_found=0;
my @bugs;
if (defined($errata->{'references'}->{'reference'})) {
foreach my $reference (@{$errata->{'references'}->{'reference'}}) {
if ($reference->{'type'} =~ /bugzilla|cve/) {
# in spacewalk, the bug id needs to be an integer, so if not we just make a bug reference line instead
if ($reference->{'id'} =~ /\D/) {
$xml->{$advid}->{'references'}.=$reference->{'href'}."\n";
} else {
my $bug;
$bug->{'id'}=$reference->{'id'};
$bug->{'summary'}=$reference->{'title'};
$bug->{'url'}=$reference->{'href'};
$bugs_found=1;
push(@bugs,$bug);
}
} else {
$xml->{$advid}->{'references'}.=$reference->{'href'}."\n";
}
}
}
if ($bugs_found) {
$xml->{$advid}->{'bugs'}=\@bugs;
}
chomp($xml->{$advid}->{'references'});
}
return $xml;
}
sub parse_redhat_errata($$) {
my ($client,$sessionid)=@_;
my $xml;
my $rhn_erratas;
set_proxy($opt_rhn_proxy);
if (defined($opt_startfromprevious)) {
my $startdate=get_previous_startdate($opt_startfromprevious);
info("Getting erratas from date $startdate till now\n");
eval {$rhn_erratas = $client->call('channel.software.listErrata',$sessionid,$opt_redhat_channel,$startdate)};
} elsif (defined($opt_startdate) && defined($opt_enddate)) {
info("Getting erratas from date $opt_startdate till $opt_enddate\n");
eval {$rhn_erratas = $client->call('channel.software.listErrata',$sessionid,$opt_redhat_channel,$opt_startdate,$opt_enddate)};
} elsif (defined($opt_startdate)) {
info("Getting erratas from date $opt_startdate till now\n");
eval {$rhn_erratas = $client->call('channel.software.listErrata',$sessionid,$opt_redhat_channel,$opt_startdate)};
} else {
info("Getting ALL erratas (this make take a while)\n");
eval {$rhn_erratas = $client->call('channel.software.listErrata',$sessionid,$opt_redhat_channel)};
}
if ($@) {
warning ("channel.software.listErrata returned an error: $@\n");
}
foreach my $errata (@$rhn_erratas) {
my $advid=$errata->{'errata_advisory'};
my $rhn_errata_details=rhn_get_details($rhn_client,$rhn_session,$advid);
my @rhn_errata_packages=rhn_get_packages($rhn_client,$rhn_session,$advid);
# the Redhat API call returns errata_notes and such, spacewalk needs just "notes" and alike
$xml->{$advid}={};
$xml->{$advid}->{'synopsis'}=$errata->{'errata_synopsis'};
$xml->{$advid}->{'release'}=1;
$xml->{$advid}->{'type'}=$errata->{'errata_advisory_type'};
$xml->{$advid}->{'advisory_name'}=$errata->{'errata_advisory'};
$xml->{$advid}->{'product'}="RHEL Linux";
$xml->{$advid}->{'solution'}="not available";
if (ref($rhn_errata_details) eq "HASH") {
$xml->{$advid}->{'topic'}=$rhn_errata_details->{'errata_topic'};
$xml->{$advid}->{'description'}=$rhn_errata_details->{'errata_description'};
$xml->{$advid}->{'notes'}=$rhn_errata_details->{'errata_notes'};
$xml->{$advid}->{'references'}=$rhn_errata_details->{'errata_references'};
} else {
$xml->{$advid}->{'topic'}='';
$xml->{$advid}->{'description'}='';
$xml->{$advid}->{'notes'}='';
$xml->{$advid}->{'references'}='';
}
#$xml->{$advid}->{'os_release'}=$os_release;
$xml->{$advid}->{${opt_architecture}.'_packages'}=\@rhn_errata_packages;
}
set_proxy($opt_proxy);
return $xml;
}
sub parse_message($$) {
my ($part, $subject) = @_;
debug("Parsing errata $subject\n");
(my $upstream_details = $part) =~ s/.*Upstream details at : (.*?)\n.*/$1/s;
$upstream_details =~ s/.*\>(.*)\<.*/$1/;
$subject =~ s/\n//gs;
$subject =~ s/\s+/ /g;
(my $advid = $subject) =~ s/(.*?) .*/$1/;
(my $synopsis = $subject) =~ s/.*? (.*)/$1/;
my $os_release = undef;
if ($subject =~ /.* (\d+) .*/) {
$os_release=$1;
} elsif ($subject =~ /.*\-(\d+) .*/) {
$os_release=$1;
}
my $centos_xen_errata=0;
if (!defined($os_release) || $os_release =~ /\D/) {
# OS release is not an integer, happens for xen and CSL errata
# so we just set it to the OS release, the package details will later point
# out if the advisory can be applied or not
$os_release = $opt_os_version;
if ($subject =~ /xen/i) {
# xen errata need special treatment because of different format
$centos_xen_errata=1;
}
}
if ($os_release != $opt_os_version) {
return;
}
# now get the packages per architecture
my $i386_packages="";
my $x86_64_packages="";
my $s390x_packages="";
if ($centos_xen_errata) {
($part =~ /I.86/s) && (($i386_packages = $part) =~ s/.*I.86\s*\n\-+\n(.*?)\n\n.*/$1/s);
($part =~ /X86_64/s) && (($x86_64_packages = $part) =~ s/.*X86_64\s*\n\-+\n(.*?)\n\n.*/$1/s);
} else {
($part =~ /i.86:/s) && (($i386_packages = $part) =~ s/.*i.86:\n(.*?)\n\n.*/$1/s);
($part =~ /x86_64:/s) && (($x86_64_packages = $part) =~ s/.*x86_64:\n(.*?)\n\n.*/$1/s);
($part =~ /s390x:/s) && (($s390x_packages = $part) =~ s/.*s390x:\n(.*?)\n\n.*/$1/s);
}
# remove first empty line
$i386_packages =~ s/^\n//;
$x86_64_packages =~ s/^\n//;
$s390x_packages =~ s/^\n//;
# remove emtpy lines
$i386_packages =~ s/\n\s*\n/\n/g;
$x86_64_packages =~ s/\n\s*\n/\n/g;
$s390x_packages =~ s/\n\s*\n/\n/g;
# remove last empty line
$i386_packages =~ s/\n\s*$//;
$x86_64_packages =~ s/\n\s*$//;
$s390x_packages =~ s/\n\s*$//;
debug("$advid i386 packages info found:\n$i386_packages\n");
debug("$advid x86_64 packages info found:\n$x86_64_packages\n");
debug("$advid s390x packages info found:\n$s390x_packages\n");
my @i386_packages = split(/\n/s,$i386_packages);
my @x86_64_packages = split(/\n/s,$x86_64_packages);
my @s390x_packages = split(/\n/s,$s390x_packages);
# remove the checksum info
s/\S+\s+// for @i386_packages;
s/\S+\s+// for @x86_64_packages;
s/\S+\s+// for @s390x_packages;
my $adv_type="";
if (substr($advid,2,2) eq "SA") { $adv_type="Security Advisory";}
elsif (substr($advid,2,2) eq "BA") { $adv_type="Bug Fix Advisory";}
elsif (substr($advid,2,2) eq "EA") { $adv_type="Product Enhancement Advisory";}
else {
# something undetermined
return;
}
my $adv={};
$adv->{'synopsis'}=$synopsis;
$adv->{'release'}=1;
$adv->{'type'}=$adv_type;
$adv->{'advisory_name'}=$advid;
$adv->{'product'}="CentOS Linux";
$adv->{'topic'}="not available";
$adv->{'description'}="not available";
$adv->{'notes'}="not available";
$adv->{'solution'}="not available";
$adv->{'os_release'}=$os_release;
$adv->{'references'}="$upstream_details";
# depending on the value off opt_architecture, one of the following will be used
$adv->{'i386_packages'}=\@i386_packages;
$adv->{'x86_64_packages'}=\@x86_64_packages;
$adv->{'s390x_packages'}=\@s390x_packages;
# the next is just to be able to skip looking for xen errata in rhn, when that option is choosen
if ($centos_xen_errata) {
$adv->{'centos_xen_errata'}=1;
}
return $adv;
}
sub parse_archivedir() {
opendir(my $dh, $opt_erratadir) || die "can't opendir $opt_erratadir: $!";
my @files = grep { !/^\./ && -f "$opt_erratadir/$_" } readdir($dh);
closedir $dh;
my $xml = {};
foreach my $file (@files) {
local $/=undef;
open(IN,"$opt_erratadir/$file");
my $string = <IN>;
close(IN);
local $/="\n";
if($string =~ /\<TITLE\>\s+\[CentOS-announce\]\s+CE/) {
debug("Single archive: $opt_erratadir/$file\n");
my $part = $string;
(my $subject = $part) =~ s/.*\<TITLE\> \[CentOS-announce\] (CE.*?)\<\/TITLE\>.*/$1/s;
$part =~ s/.*\<PRE\>(.*?)\<\/PRE\>.*/$1/s;
my $adv = parse_message($part, $subject);
if(!defined $adv) {
next;
}
$xml->{$adv->{'advisory_name'}}=$adv;
} elsif($string =~ /\<TITLE\>\s+\[CentOS-CR-announce\]\s+CE/) {
debug("Single archive: $opt_erratadir/$file\n");
my $part = $string;
(my $subject = $part) =~ s/.*\<TITLE\> \[CentOS-CR-announce\] (CE.*?)\<\/TITLE\>.*/$1/s;
$part =~ s/.*\<PRE\>(.*?)\<\/PRE\>.*/$1/s;
my $adv = parse_message($part, $subject);
if(!defined $adv) {
next;
}
$xml->{$adv->{'advisory_name'}}=$adv;
} elsif($string =~ /\<TITLE\>\s+\[CentOS\]\s+CentOS-announce\s+Digest/) {
debug("Multiple archive: $opt_erratadir/$file\n");
my @parts = split(/Message:/,$string);
# skip the first part, since it's general info
shift(@parts);
foreach my $part (@parts) {
my $subject=$part;
# concat the lines starting with white spaces to the previous line
$subject =~ s/\n\s+/ /gs;
if ($subject !~ /Subject: \[CentOS-announce\] CE/s) {
next;
}
$subject =~ s/.*Subject: \[CentOS-announce\] (CE.*?)\n.*/$1/s;
#print "$subject\n";
my $adv = parse_message($part, $subject);
if (!defined($adv->{'advisory_name'})) {
next;
}
$xml->{$adv->{'advisory_name'}}=$adv;
}
}
}
return $xml;
}
sub channel_get_details($$$) {
my ($client,$sessionid,$channel_label)=@_;
my $details=$client->call('channel.software.getDetails',$sessionid,$channel_label);
return $details;
}
sub rhn_get_details($$$) {
my ($client,$sessionid,$advisory_name)=@_;
$advisory_name =~ s/CE|SL/RH/g;
my $details;
eval {$details = $client->call('errata.getDetails', $sessionid, $advisory_name)};
if ($@) {
warning ("rhn_get_details returned an error: $@\n");
return 0;
} else {
return $details;
}
}
sub rhn_get_keywords($$$) {
my ($client,$sessionid,$advisory_name)=@_;
$advisory_name =~ s/CE|SL/RH/g;
#my @keywords=eval($client->call('errata.listKeywords',$sessionid,$advisory_name));
my $keywords;
my @keywords;
eval {$keywords=$client->call('errata.listKeywords',$sessionid,$advisory_name)};
if ($@) {
warning ("rhn_get_keywords returned an error: $@\n");
return @keywords;
}
return @{$keywords};
}
sub rhn_get_bugs($$$$) {
my ($client,$sessionid,$advisory_name,$bugzilla_url)=@_;
$advisory_name =~ s/CE|SL/RH/g;
my $bugzilla='';
if ($bugzilla_url) {
$bugzilla=$bugzilla_url . 'show_bug.cgi?id=';
}
my $raw_bugs;
my @bugs;
eval {$raw_bugs=$client->call('errata.bugzillaFixes',$sessionid,$advisory_name)};
if ($@) {
warning ("rhn_get_bugs returned an error: $@\n");
return @bugs;
}
foreach my $key (keys %{$raw_bugs}) {
my $bug= {
'id'=>$key,
'summary'=>$raw_bugs->{$key},
'url'=>''
};
if ($bugzilla) {
$bug->{'url'}=$bugzilla . $key;
}
push(@bugs,$bug);
}
return @bugs;
}
sub rhn_get_packages($$$) {
my ($client,$sessionid,$advisory_name)=@_;
$advisory_name =~ s/CE|SL/RH/g;
my $packages;
my @packages;
eval { $packages=$client->call('errata.listPackages',$sessionid,$advisory_name)};
if ($@) {
warning ("rhn_get_packages returned an error: $@\n");
return @packages;
}
foreach my $pkg (@$packages) {
my $found=0;
foreach my $providing_channel (@{$pkg->{'providing_channels'}}) {
if ((!$opt_redhat && $providing_channel eq $opt_channel) ||
($opt_redhat && $providing_channel eq $opt_redhat_channel)) {
$found=1;
}
}
(!$found) && (next);
push(@packages,$pkg->{'package_file'});
}
return @packages;
}
sub rhn_get_cves($$$) {
my ($client,$sessionid,$advisory_name)=@_;
$advisory_name =~ s/CE|SL/RH/g;
my $cve;
my @cve;
eval {$cve=$client->call('errata.listCves',$sessionid,$advisory_name)};
if ($@) {
warning ("rhn_get_cves returned an error: $@\n");
return @cve;
}
return @{$cve};
}
sub get_previous_startdate($) {
my $previous=$_[0];
my $minute=60;
my $hour=60*$minute;
my $day=24*$hour;
my $week=7*$day;
my $month=31*$day; # 31 days better safe than sorry
my $year=366*$day; # 365 days plus 1 for leap year
my $current_time=time;
if ($previous=~/^(m|minute)$/i){
$current_time=$current_time - $minute;
}
elsif ($previous=~/^hour$/i){
$current_time=$current_time - $hour;
}
elsif ($previous=~/^day$/i){
$current_time=$current_time - $day;
}
elsif ($previous=~/^week$/i){
$current_time=$current_time - $week;
}
elsif ($previous=~/^twoweeks$/i){
$current_time=$current_time - 2*$week;
}
elsif ($previous=~/^month$/i){
$current_time=$current_time - $month;
}
elsif ($previous=~/^year$/i){
$current_time=$current_time - $year;
}
else{
return "";
}
my @time=localtime($current_time);
my $old_year=$time[5]+1900;
my $old_month=$time[4] + 1;
#my $previousdate=$old_year . '-' . $old_month . '-' . pad_time($time[3]) . ' ' . pad_time($time[2]) . ':' . pad_time($time[1]) . ':' . pad_time($time[0]);
my $previousdate=sprintf("%d-%02d-%02d %02d:%02d:%02d",$old_year,$old_month,$time[3],$time[2],$time[1],$time[0]);
return $previousdate;
}
sub set_proxy($) {
my $proxy=$_[0];
if ($proxy ne "") {
$ENV{'HTTPS_PROXY'}=$proxy;
$ENV{'HTTP_PROXY'}=$proxy;
$ENV{'https_proxy'}=$proxy;
$ENV{'http_proxy'}=$proxy;
} else {
delete($ENV{'HTTPS_PROXY'});
delete($ENV{'HTTP_PROXY'});
delete($ENV{'https_proxy'});
delete($ENV{'http_proxy'});
}
}
#######################################################################
### MAIN
#######################################################################
# Print call and parameters if in debug mode (GetOptions will clear @ARGV)
if (join(' ',@ARGV) =~ /--debug/) { print STDERR "DEBUG: Called as $0 ".join(' ',@ARGV)."\n"; }
# Parse arguments
my $getopt = GetOptions( 'server=s' => \$opt_server,
'erratadir=s' => \$opt_erratadir,
'rhsa-oval=s' => \$opt_rhsaovalfile,
'epel_errata=s' => \$opt_epel_erratafile,
'oel_errata=s' => \$opt_oel_erratafile,
'sl_errata=s' => \$opt_sl_erratafile,
'debug' => \$opt_debug,
'publish' => \$opt_publish,
'security' => \$opt_security,
'bugfix' => \$opt_bugfix,
'help' => \$opt_help,
'testrun' => \$opt_testrun,
'architecture=s' => \$opt_architecture,
'enhancement' => \$opt_enhancement,
'sync-channels' => \$opt_syncchannels,
'sync-timeout=i' => \$opt_synctimeout,
'os-version=i' => \$opt_os_version,
'channel=s' => \$opt_channel,
'rhn-proxy=s' => \$opt_rhn_proxy,
'rhn-server=s' => \$opt_rhn_server,
'get-from-rhn' => \$opt_get_from_rhn,
'bugzilla-url' => \$opt_bugzilla_url,
'proxy=s' => \$opt_proxy,
'autopush' => \$opt_autopush,
'redhat' => \$opt_redhat,
'startdate=s' => \$opt_startdate,
'enddate=s' => \$opt_enddate,
'redhat-channel=s' => \$opt_redhat_channel,
'startfromprevious=s' => \$opt_startfromprevious,
'quiet' => \$opt_quiet,
'spacewalk-user=s' => \$opt_spacewalk_user,
'spacewalk-pass=s' => \$opt_spacewalk_pwd,
'rhn-user=s' => \$opt_rhn_user,
'rhn-pass=s' => \$opt_rhn_pwd,
'suffix=s' => \$opt_suffix
);
# Check for arguments
if ( defined($opt_help) ) {
usage;
exit 1;
}
if ( not(defined($opt_server)) || not(defined($opt_channel)) ) {
usage;
exit 1;
}
#if ( not(defined($opt_erratadir)) && !$opt_redhat ) {
if ( !$opt_redhat && !defined($opt_erratadir) && !defined($opt_epel_erratafile) && !defined($opt_oel_erratafile) && !defined($opt_sl_erratafile)) {
usage;
exit 1;
}
if (defined($opt_erratadir) && !$opt_redhat && not(-d $opt_erratadir)) {
# Do we have a proper errata dir?
error("$opt_erratadir is not a directory!\n");
exit 1;
}
if (!$opt_os_version) {
error("option 'os-version' must be defined!\n");
exit 1;
}
# check the architecture
if (defined($opt_architecture) && $opt_architecture ne "i386" && $opt_architecture ne "x86_64" && $opt_architecture ne "s390x") {
error("Architecture is not correctly set, please use 'i386' or 'x86_64' for values!\n");
exit 1;
} elsif (!defined($opt_architecture)) {
info("Architecture is not specified, will try to determine it based on the channel properties of '$opt_channel'\n");
}
if ($opt_redhat && !defined($opt_redhat_channel) && !defined($opt_epel_erratafile) && !defined($opt_oel_erratafile) && !defined($opt_sl_erratafile)) {
info("No redhat channel specified, assuming the name '$opt_channel'\n\n");
$opt_redhat_channel=$opt_channel;
}
# Set the OS variant
if ($opt_oel_erratafile) {
$os_variant=":O";
info("Setting the OS variant to OEL, if this is wrong please remove the --oel_errata option\n");
} elsif ($opt_sl_erratafile) {
$os_variant=":S";
info("Setting the OS variant to Scientific, if this is wrong please remove the --sl_errata option\n");
} elsif ($opt_redhat) {
$os_variant=":R";
info("Setting the OS variant to Redhat, if this is wrong please remove the --redhat option\n");
} else {
$os_variant=":C";
info("Setting the OS variant to CentOS, if this is wrong please add the --redhat option\n");
}
# Output $version string in debug mode
debug("Version is $version\n");
#############################
# Initialize API connection #
#############################
set_proxy($opt_proxy);
$client = new Frontier::Client(url => "http://$opt_server/rpc/api");
#########################################
# Get the API version we are talking to #
#########################################
if ($apiversion = $client->call('api.get_version')) {
info("Server $opt_server is running API version $apiversion\n");
} else {
error("Could not determine API version on server $opt_server\n");
exit 1;
}
#####################################
# Check if API version is supported #
#####################################
foreach (@supportedapi) {
if ($apiversion eq $_) {
info("Your API version is supported\n");
$apisupport = 1;
}
}
# In case we found an unsupported API
if (not($apisupport)) {
error("Your API version ($apiversion) is not supported. Try upgrading this script.\n");
exit 2;
}
###########################
# Authenticate to the API #
###########################
if (defined($opt_spacewalk_user)) {
$username = $opt_spacewalk_user;
} elsif (defined($ENV{'SPACEWALK_USER'})) {
$username = $ENV{'SPACEWALK_USER'};
} else {
print "Please enter username: ";
chop($username=<STDIN>);
}
if (defined($opt_spacewalk_pwd)) {
$password = $opt_spacewalk_pwd;
} elsif (defined($ENV{'SPACEWALK_PASS'})) {
$password = $ENV{'SPACEWALK_PASS'};
} else {
print "Please enter password: ";
system('stty','-echo');
chop($password=<STDIN>);
system('stty','echo');
print "\n";
}
eval {$session = $client->call('auth.login', $username, $password);};
if ($@) {
error("Authentication on $opt_server FAILED!\n");
exit 3;
} else {
info("Authentication on $opt_server successful\n");
}
##############################
# For RedHat: connect to RHN #
##############################
if ($opt_get_from_rhn || ($opt_redhat && !defined($opt_epel_erratafile) && !defined($opt_oel_erratafile) && !defined($opt_sl_erratafile))) {
set_proxy($opt_rhn_proxy);
if (defined($opt_rhn_user)) {
$rhn_username = $opt_rhn_user;
} elsif (defined($ENV{'RHN_USER'})) {
$rhn_username = $ENV{'RHN_USER'};
} else {
print "Please enter RHN username: ";
chop($rhn_username=<STDIN>);
}
if (defined($opt_rhn_pwd)) {
$rhn_password = $opt_rhn_pwd;
} elsif (defined($ENV{'RHN_PASS'})) {
$rhn_password = $ENV{'RHN_PASS'};
} else {
print "Please enter RHN password: ";
system('stty','-echo');
chop($rhn_password=<STDIN>);
system('stty','echo');
print "\n";
}
$rhn_client = new Frontier::Client(url => "https://$opt_rhn_server/rpc/api");
eval {$rhn_session = $rhn_client->call('auth.login', $rhn_username, $rhn_password)};
if ($@) {
error("Couldn't log in to redhat: $@\n");
# clean up our spacewalk session and exit
set_proxy($opt_proxy);
$client->call('auth.logout', $session);
exit 3;
}
info("Authentication on $opt_rhn_server successful\n");
if ($opt_debug) {
my $channels = $rhn_client->call('channel.listSoftwareChannels', $rhn_session);
foreach my $channel (@$channels) {
print "RHN channel found: ".$channel->{"channel_label"}."\n";
}
}
set_proxy($opt_proxy);
}
##########################
# Check user permissions #
##########################
if ($opt_publish) {
# Publishing Errata requires Satellite or Org Administrator role
my $userroles = $client->call('user.list_roles', $session, $username);
debug("User is assigned these roles: ".join(' ', @{$userroles})."\n");
if ( (join(' ', @{$userroles}) =~ /satellite_admin/) ||
(join(' ', @{$userroles}) =~ /org_admin/) ||
(join(' ', @{$userroles}) =~ /channel_admin/) ) {
info("User has administrator access to this server\n");
} else {
error("User does NOT have administrator access\n");
error("You have set --publish but your user has insufficient access rights\n");
error("Either use an account that is Satellite/Org/Channel Administator or omit --publish\n");
exit 1;
}
}