Imported Upstream version 17.0.2
[platform/upstream/libzypp.git] / tools / zypp-pubkey.cc
1 #define INCLUDE_TESTSETUP_WITHOUT_BOOST
2 #include "zypp/../tests/lib/TestSetup.h"
3 #undef  INCLUDE_TESTSETUP_WITHOUT_BOOST
4 #define message cout
5 using std::flush;
6
7 #include <boost/program_options.hpp>
8 namespace opt = boost::program_options;
9
10 #include <zypp/target/rpm/RpmDb.h>
11
12 static std::string appname( "unknown" );
13
14 int errexit( const std::string & msg_r = std::string(), int exit_r = 100 )
15 {
16   if ( ! msg_r.empty() )
17   {
18     cerr << endl << msg_r << endl << endl;
19   }
20   return exit_r;
21 }
22
23 bool byTTL( const PublicKey & lhs, const PublicKey & rhs )
24 {
25   int cmp = lhs.gpgPubkeyVersion().compare( rhs.gpgPubkeyVersion() );
26   if ( cmp ) return cmp < 0;
27   return lhs.gpgPubkeyRelease() > rhs.gpgPubkeyRelease(); // intentionally reverse cdate
28 }
29
30 /******************************************************************
31 **
32 **      FUNCTION NAME : main
33 **      FUNCTION TYPE : int
34 */
35 int main( int argc, char * argv[] )
36 {
37   appname = Pathname::basename( argv[0] );
38   ///////////////////////////////////////////////////////////////////
39
40   opt::options_description options( "Options" );
41   options.add_options()
42       ( "key-file",     opt::value<std::vector<std::string> >(),
43                         "ASCII ascii armored public key file")
44       ( "root",         opt::value<std::string>()->default_value( "/" ),
45                         "Use the rmp database from system rooted at ARG")
46       ( "help,?",       "Produce this help message")
47       ;
48
49   opt::positional_options_description positional;
50   positional.add( "key-file", -1 );
51
52   opt::variables_map vm;
53   opt::store( opt::command_line_parser( argc, argv ).options( options ).positional( positional ).run(), vm );
54   opt::notify( vm );
55
56   if ( vm.count( "help" ) )
57   {
58     cerr << "Usage: " << appname << " [OPTIONS] [key-files...]" << endl;
59     cerr << "If no key files are given, list info about all gpg-pubkeys in the rpm database." << endl;
60     cerr << "Otherwise print info about each key and wheter it is present in the rpm database. " << endl;
61     cerr << options << endl;
62     return 1;
63   }
64   ///////////////////////////////////////////////////////////////////
65
66   if ( ! PathInfo( vm["root"].as<std::string>() ).isDir() )
67       return errexit("--root requires a directory");
68
69   target::rpm::RpmDb rpmdb;
70   rpmdb.initDatabase( vm["root"].as<std::string>() );
71   std::list<PublicKey> rpmpubkeys( rpmdb.pubkeys() );
72   rpmpubkeys.sort( byTTL );
73
74   if ( ! vm.count( "key-file" ) )
75   {
76     std::string last;
77     for_each_( it, rpmpubkeys )
78     {
79       if ( last == it->gpgPubkeyVersion() )
80         cout << *it << endl;
81       else
82       {
83         cout << dump( *it ) << endl;
84         last = it->gpgPubkeyVersion();
85       }
86     }
87     return 0;
88   }
89
90   ///////////////////////////////////////////////////////////////////
91
92   for_each_( it, vm["key-file"].as< std::vector<std::string> >() )
93   {
94     cout << "=== " << PathInfo(*it) << endl;
95     PublicKey pubkey( *it );
96     cout << dump( pubkey ) << endl;
97
98     std::string pubkeyV( pubkey.gpgPubkeyVersion() );
99     std::string pubkeyR( pubkey.gpgPubkeyRelease() );
100     unsigned count = 0;
101     for_each_( rpmpub, rpmpubkeys )
102     {
103       if ( rpmpub->gpgPubkeyVersion() == pubkeyV )
104       {
105         int cmp = rpmpub->gpgPubkeyRelease().compare( pubkeyR );
106         if ( cmp < 0 )
107           cout << "<<< ";
108         else if ( cmp > 0 )
109           cout << ">>> ";
110         else
111         {
112           ++count;
113           cout << "*** ";
114         }
115         cout << "gpg-pubkey-" << rpmpub->gpgPubkeyVersion() << "-" << rpmpub->gpgPubkeyRelease() << " " << rpmpub->daysToLive() << endl;
116       }
117     }
118     if ( ! count )
119     {
120       cout << "*** Not in rpm database." << endl;
121     }
122     cout << endl;
123   }
124
125   ///////////////////////////////////////////////////////////////////
126   return 0;
127 }