| 1 | #!/usr/bin/env perl |
|---|
| 2 | # |
|---|
| 3 | # GHNS Signature - manage digital signatures on files in SVN-backed repository |
|---|
| 4 | # Copyright (C) 2007 Josef Spillner <josef@kstuff.org> |
|---|
| 5 | # Published under 'GNU AGPLv3 or later' conditions |
|---|
| 6 | |
|---|
| 7 | use strict; |
|---|
| 8 | use XML::DOM; |
|---|
| 9 | |
|---|
| 10 | if(($ARGV[0] eq "-h") || ($ARGV[0] eq "--help") || (!$ARGV[0])){ |
|---|
| 11 | print "Synopsis: ghnssignature --sign|--verify|--auto [<payloadfile>]\n"; |
|---|
| 12 | print "\n"; |
|---|
| 13 | print "If no payloadfile is given, it will scan all *.meta files to find\n"; |
|---|
| 14 | print "them on its own.\n"; |
|---|
| 15 | print "The --auto option will sign if needed, and add signature to SVN.\n"; |
|---|
| 16 | exit; |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | my $mode = $ARGV[0]; |
|---|
| 20 | if(($mode eq "--sign") || ($mode eq "--verify") || ($mode eq "--auto")){ |
|---|
| 21 | }else{ |
|---|
| 22 | print "Error: action must be --sign or --verify or --auto.\n"; |
|---|
| 23 | exit 1; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | my @files; |
|---|
| 27 | if($ARGV[1]){ |
|---|
| 28 | my $file = $ARGV[1]; |
|---|
| 29 | push @files, $file; |
|---|
| 30 | }else{ |
|---|
| 31 | my @metafiles = glob("*.meta"); |
|---|
| 32 | my $parser = new XML::DOM::Parser; |
|---|
| 33 | foreach my $metafile(@metafiles){ |
|---|
| 34 | #print "??? $metafile\n"; |
|---|
| 35 | my $doc = $parser->parsefile($metafile); |
|---|
| 36 | my $root = $doc->getDocumentElement(); |
|---|
| 37 | my @payloads = $root->getElementsByTagName("payload"); |
|---|
| 38 | foreach my $payload(@payloads){ |
|---|
| 39 | my $file = $payload->getFirstChild->getData; |
|---|
| 40 | #print "!!! $file\n"; |
|---|
| 41 | push @files, $file; |
|---|
| 42 | } |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | my $identityfile = "identity.txt"; |
|---|
| 47 | if(!(-f $identityfile)){ |
|---|
| 48 | print "Error: no file '$identityfile' found.\n"; |
|---|
| 49 | exit 1; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | open(F, $identityfile); |
|---|
| 53 | my $id = <F>; |
|---|
| 54 | close(F); |
|---|
| 55 | |
|---|
| 56 | chomp $id; |
|---|
| 57 | if(!$id){ |
|---|
| 58 | print "Error: couldn't read identity from '$identityfile'.\n"; |
|---|
| 59 | exit 1; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | sub verify{ |
|---|
| 63 | my $file = shift(@_); |
|---|
| 64 | my $x = system("gpg --verify $file.asc"); |
|---|
| 65 | return $x; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | sub sign{ |
|---|
| 69 | my $file = shift(@_); |
|---|
| 70 | my $id = shift(@_); |
|---|
| 71 | my $x = system("gpg --local-user $id --armor --detach-sign $file"); |
|---|
| 72 | return $x; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | if($mode eq "--verify"){ |
|---|
| 76 | foreach my $file(@files){ |
|---|
| 77 | my $x = verify($file); |
|---|
| 78 | if($x != 0){ |
|---|
| 79 | print "Error: cannot verify $file.asc.\n"; |
|---|
| 80 | exit $x; |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | if($mode eq "--sign"){ |
|---|
| 86 | foreach my $file(@files){ |
|---|
| 87 | my $x = sign($file, $id); |
|---|
| 88 | if($x != 0){ |
|---|
| 89 | print "Error: signature could not be created.\n"; |
|---|
| 90 | exit $x; |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | if($mode eq "--auto"){ |
|---|
| 96 | foreach my $file(@files){ |
|---|
| 97 | my $newsignature = 0; |
|---|
| 98 | my $x = -1; |
|---|
| 99 | if(!(-f "$file.asc")){ |
|---|
| 100 | $newsignature = 1; |
|---|
| 101 | }else{ |
|---|
| 102 | $x = verify($file); |
|---|
| 103 | } |
|---|
| 104 | if($x != 0){ |
|---|
| 105 | print "File $file needs to be signed...\n"; |
|---|
| 106 | my $x = sign($file, $id); |
|---|
| 107 | if($x != 0){ |
|---|
| 108 | print "Error: signature could not be created.\n"; |
|---|
| 109 | exit $x; |
|---|
| 110 | } |
|---|
| 111 | if($newsignature){ |
|---|
| 112 | system("svn add $file.asc"); |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|