1 | // Copyright (C) 2002 Graydon Hoare <graydon@pobox.com>␊ |
2 | //␊ |
3 | // This program is made available under the GNU GPL version 2.0 or␊ |
4 | // greater. See the accompanying file COPYING for details.␊ |
5 | //␊ |
6 | // This program is distributed WITHOUT ANY WARRANTY; without even the␊ |
7 | // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR␊ |
8 | // PURPOSE.␊ |
9 | ␊ |
10 | #include "base.hh"␊ |
11 | #include <iostream>␊ |
12 | #include <sstream>␊ |
13 | ␊ |
14 | #include "cmd.hh"␊ |
15 | #include "app_state.hh"␊ |
16 | #include "packet.hh"␊ |
17 | ␊ |
18 | using std::cin;␊ |
19 | using std::cout;␊ |
20 | using std::istringstream;␊ |
21 | using std::vector;␊ |
22 | ␊ |
23 | CMD(pubkey, "pubkey", "", CMD_REF(packet_io), N_("ID"), ␊ |
24 | N_("Prints a public key packet"),␊ |
25 | "",␊ |
26 | options::opts::none)␊ |
27 | {␊ |
28 | if (args.size() != 1)␊ |
29 | throw usage(execid);␊ |
30 | ␊ |
31 | rsa_keypair_id ident(idx(args, 0)());␊ |
32 | bool exists(false);␊ |
33 | base64< rsa_pub_key > key;␊ |
34 | if (app.db.database_specified() && app.db.public_key_exists(ident))␊ |
35 | {␊ |
36 | app.db.get_key(ident, key);␊ |
37 | exists = true;␊ |
38 | }␊ |
39 | if (app.keys.key_pair_exists(ident))␊ |
40 | {␊ |
41 | keypair kp;␊ |
42 | app.keys.get_key_pair(ident, kp);␊ |
43 | key = kp.pub;␊ |
44 | exists = true;␊ |
45 | }␊ |
46 | N(exists,␊ |
47 | F("public key '%s' does not exist") % idx(args, 0)());␊ |
48 | ␊ |
49 | packet_writer pw(cout);␊ |
50 | pw.consume_public_key(ident, key);␊ |
51 | }␊ |
52 | ␊ |
53 | CMD(privkey, "privkey", "", CMD_REF(packet_io), N_("ID"), ␊ |
54 | N_("Prints a private key packet"),␊ |
55 | "",␊ |
56 | options::opts::none)␊ |
57 | {␊ |
58 | if (args.size() != 1)␊ |
59 | throw usage(execid);␊ |
60 | ␊ |
61 | rsa_keypair_id ident(idx(args, 0)());␊ |
62 | N(app.keys.key_pair_exists(ident),␊ |
63 | F("public and private key '%s' do not exist in keystore")␊ |
64 | % idx(args, 0)());␊ |
65 | ␊ |
66 | packet_writer pw(cout);␊ |
67 | keypair kp;␊ |
68 | app.keys.get_key_pair(ident, kp);␊ |
69 | pw.consume_key_pair(ident, kp);␊ |
70 | }␊ |
71 | ␊ |
72 | namespace␊ |
73 | {␊ |
74 | // this writer injects packets it receives to the database␊ |
75 | // and/or keystore.␊ |
76 | ␊ |
77 | struct packet_db_writer : public packet_consumer␊ |
78 | {␊ |
79 | app_state & app;␊ |
80 | public:␊ |
81 | packet_db_writer(app_state & app) : app(app) {}␊ |
82 | virtual ~packet_db_writer() {}␊ |
83 | virtual void consume_file_data(file_id const & ident,␊ |
84 | file_data const & dat)␊ |
85 | {␊ |
86 | transaction_guard guard(app.db);␊ |
87 | app.db.put_file(ident, dat);␊ |
88 | guard.commit();␊ |
89 | }␊ |
90 | ␊ |
91 | virtual void consume_file_delta(file_id const & old_id,␊ |
92 | file_id const & new_id,␊ |
93 | file_delta const & del)␊ |
94 | {␊ |
95 | transaction_guard guard(app.db);␊ |
96 | app.db.put_file_version(old_id, new_id, del);␊ |
97 | guard.commit();␊ |
98 | }␊ |
99 | ␊ |
100 | virtual void consume_revision_data(revision_id const & ident,␊ |
101 | revision_data const & dat)␊ |
102 | {␊ |
103 | transaction_guard guard(app.db);␊ |
104 | app.db.put_revision(ident, dat);␊ |
105 | guard.commit();␊ |
106 | }␊ |
107 | ␊ |
108 | virtual void consume_revision_cert(revision<cert> const & t)␊ |
109 | {␊ |
110 | transaction_guard guard(app.db);␊ |
111 | app.db.put_revision_cert(t);␊ |
112 | guard.commit();␊ |
113 | }␊ |
114 | ␊ |
115 | virtual void consume_public_key(rsa_keypair_id const & ident,␊ |
116 | base64< rsa_pub_key > const & k)␊ |
117 | {␊ |
118 | transaction_guard guard(app.db);␊ |
119 | app.db.put_key(ident, k);␊ |
120 | guard.commit();␊ |
121 | }␊ |
122 | ␊ |
123 | virtual void consume_key_pair(rsa_keypair_id const & ident,␊ |
124 | keypair const & kp)␊ |
125 | {␊ |
126 | transaction_guard guard(app.db);␊ |
127 | app.keys.put_key_pair(ident, kp);␊ |
128 | guard.commit();␊ |
129 | }␊ |
130 | };␊ |
131 | }␊ |
132 | ␊ |
133 | ␊ |
134 | CMD(read, "read", "", CMD_REF(packet_io), "[FILE1 [FILE2 [...]]]",␊ |
135 | N_("Reads packets from files"),␊ |
136 | N_("If no files are provided, the standard input is used."),␊ |
137 | options::opts::none)␊ |
138 | {␊ |
139 | packet_db_writer dbw(app);␊ |
140 | size_t count = 0;␊ |
141 | if (args.empty())␊ |
142 | {␊ |
143 | count += read_packets(cin, dbw, app);␊ |
144 | N(count != 0, F("no packets found on stdin"));␊ |
145 | }␊ |
146 | else␊ |
147 | {␊ |
148 | for (args_vector::const_iterator i = args.begin(); ␊ |
149 | i != args.end(); ++i)␊ |
150 | {␊ |
151 | data dat;␊ |
152 | read_data(system_path(*i), dat);␊ |
153 | istringstream ss(dat());␊ |
154 | count += read_packets(ss, dbw, app);␊ |
155 | }␊ |
156 | N(count != 0, FP("no packets found in given file",␊ |
157 | "no packets found in given files",␊ |
158 | args.size()));␊ |
159 | }␊ |
160 | P(FP("read %d packet", "read %d packets", count) % count);␊ |
161 | }␊ |
162 | ␊ |
163 | ␊ |
164 | // Local Variables:␊ |
165 | // mode: C++␊ |
166 | // fill-column: 76␊ |
167 | // c-file-style: "gnu"␊ |
168 | // indent-tabs-mode: nil␊ |
169 | // End:␊ |
170 | // vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:␊ |