monotone

monotone Mtn Source Tree

Root/src/cmd_packet.cc

1// Copyright (C) 2010 Stephen Leake <stephen_leake@stephe-leake.org>
2// Copyright (C) 2002 Graydon Hoare <graydon@pobox.com>
3//
4// This program is made available under the GNU GPL version 2.0 or
5// greater. See the accompanying file COPYING for details.
6//
7// This program is distributed WITHOUT ANY WARRANTY; without even the
8// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9// PURPOSE.
10
11#include "base.hh"
12#include <iostream>
13#include <sstream>
14
15#include "cmd.hh"
16#include "app_state.hh"
17#include "database.hh"
18#include "key_store.hh"
19#include "key_packet.hh"
20#include "packet.hh"
21#include "project.hh"
22#include "vocab_cast.hh"
23
24using std::cin;
25using std::cout;
26using std::istringstream;
27using std::vector;
28
29namespace
30{
31 // this writer injects key packets it receives to the database and/or
32 // keystore.
33
34 struct key_packet_db_writer : public key_packet_consumer
35 {
36 database & db;
37 key_store & keys;
38
39 public:
40 key_packet_db_writer(database & db, key_store & keys)
41 : db(db), keys(keys)
42 {}
43 virtual ~key_packet_db_writer() {}
44 virtual void consume_public_key(key_name const & ident,
45 rsa_pub_key const & k)
46 {
47 transaction_guard guard(db);
48 db.put_key(ident, k);
49 guard.commit();
50 }
51
52 virtual void consume_key_pair(key_name const & ident,
53 keypair const & kp)
54 {
55 keys.put_key_pair(ident, kp);
56 }
57
58 virtual void consume_old_private_key(key_name const & ident,
59 old_arc4_rsa_priv_key const & k)
60 {
61 rsa_pub_key dummy;
62 keys.migrate_old_key_pair(ident, k, dummy);
63 }
64 };
65}
66
67CMD_AUTOMATE(put_public_key, N_("KEY-PACKET-DATA"),
68 N_("Store the public key in the database"),
69 "",
70 options::opts::none)
71{
72 E(args.size() == 1, origin::user,
73 F("wrong argument count"));
74
75 database db(app);
76 key_store keys(app);
77 key_packet_db_writer dbw(db, keys);
78
79 istringstream ss(idx(args,0)());
80 read_key_packets(ss, dbw);
81}
82
83static void
84pubkey_common(app_state & app,
85 args_vector args,
86 std::ostream & output)
87{
88 database db(app, database::maybe_unspecified);
89 key_store keys(app);
90 project_t project(db);
91
92 key_identity_info identity;
93 project.get_key_identity(keys, app.lua,
94 typecast_vocab<external_key_name>(idx(args, 0)),
95 identity);
96 bool exists(false);
97 rsa_pub_key key;
98 if (db.database_specified() && db.public_key_exists(identity.id))
99 {
100 db.get_key(identity.id, key);
101 exists = true;
102 }
103 if (keys.key_pair_exists(identity.id))
104 {
105 keypair kp;
106 keys.get_key_pair(identity.id, kp);
107 key = kp.pub;
108 exists = true;
109 }
110 E(exists, origin::user,
111 F("public key '%s' does not exist") % idx(args, 0)());
112
113 packet_writer pw(output);
114 pw.consume_public_key(identity.given_name, key);
115}
116
117CMD(pubkey, "pubkey", "", CMD_REF(packet_io), N_("KEY_NAME_OR_HASH"),
118 N_("Prints a public key packet"),
119 "",
120 options::opts::none)
121{
122 if (args.size() != 1)
123 throw usage(execid);
124
125 pubkey_common(app, args, cout);
126}
127
128CMD_AUTOMATE(get_public_key, N_("KEY_NAME_OR_HASH"),
129 N_("Prints a public key packet"),
130 "",
131 options::opts::none)
132{
133 E(args.size() == 1, origin::user,
134 F("wrong argument count"));
135
136 pubkey_common(app, args, output);
137}
138
139CMD(privkey, "privkey", "", CMD_REF(packet_io), N_("KEY_NAME_OR_HASH"),
140 N_("Prints a private key packet"),
141 "",
142 options::opts::none)
143{
144 database db(app, database::maybe_unspecified);
145 key_store keys(app);
146 project_t project(db);
147
148 if (args.size() != 1)
149 throw usage(execid);
150
151 key_identity_info identity;
152 project.get_key_identity(keys,
153 app.lua,
154 typecast_vocab<external_key_name>(idx(args, 0)),
155 identity);
156
157 packet_writer pw(cout);
158 keypair kp;
159 key_name given_name;
160 keys.get_key_pair(identity.id, given_name, kp);
161 pw.consume_key_pair(given_name, kp);
162}
163
164namespace
165{
166 // this writer injects packets it receives to the database
167 // and/or keystore.
168
169 struct packet_db_writer : public packet_consumer
170 {
171 database & db;
172 key_store & keys;
173
174 public:
175 packet_db_writer(database & db, key_store & keys)
176 : db(db), keys(keys)
177 {}
178 virtual ~packet_db_writer() {}
179 virtual void consume_file_data(file_id const & ident,
180 file_data const & dat)
181 {
182 transaction_guard guard(db);
183 db.put_file(ident, dat);
184 guard.commit();
185 }
186
187 virtual void consume_file_delta(file_id const & old_id,
188 file_id const & new_id,
189 file_delta const & del)
190 {
191 transaction_guard guard(db);
192 db.put_file_version(old_id, new_id, del);
193 guard.commit();
194 }
195
196 virtual void consume_revision_data(revision_id const & ident,
197 revision_data const & dat)
198 {
199 transaction_guard guard(db);
200 db.put_revision(ident, dat);
201 guard.commit();
202 }
203
204 virtual void consume_revision_cert(cert const & t)
205 {
206 transaction_guard guard(db);
207 db.put_revision_cert(t);
208 guard.commit();
209 }
210
211 virtual void consume_public_key(key_name const & ident,
212 rsa_pub_key const & k)
213 {
214 transaction_guard guard(db);
215 db.put_key(ident, k);
216 guard.commit();
217 }
218
219 virtual void consume_key_pair(key_name const & ident,
220 keypair const & kp)
221 {
222 keys.put_key_pair(ident, kp);
223 }
224
225 virtual void consume_old_private_key(key_name const & ident,
226 old_arc4_rsa_priv_key const & k)
227 {
228 rsa_pub_key dummy;
229 keys.migrate_old_key_pair(ident, k, dummy);
230 }
231 };
232}
233
234// Name : read_packets
235// Arguments:
236// packet-data
237// Added in: 9.0
238// Purpose:
239// Store public keys (and incidentally anything else that can be
240// represented as a packet) into the database.
241// Input format:
242// The format of the packet-data argument is identical to the output
243// of "mtn pubkey <keyname>" (or other packet output commands).
244// Output format:
245// No output.
246// Error conditions:
247// Invalid input formatting.
248CMD_AUTOMATE(read_packets, N_("PACKET-DATA"),
249 N_("Load the given packets into the database"),
250 "",
251 options::opts::none)
252{
253 E(args.size() == 1, origin::user,
254 F("wrong argument count"));
255
256 database db(app);
257 key_store keys(app);
258 packet_db_writer dbw(db, keys);
259
260 istringstream ss(idx(args,0)());
261 read_packets(ss, dbw);
262}
263
264CMD(read, "read", "", CMD_REF(packet_io), "[FILE1 [FILE2 [...]]]",
265 N_("Reads packets from files"),
266 N_("If no files are provided, the standard input is used."),
267 options::opts::none)
268{
269 database db(app);
270 key_store keys(app);
271 packet_db_writer dbw(db, keys);
272 size_t count = 0;
273 if (args.empty())
274 {
275 count += read_packets(cin, dbw);
276 E(count != 0, origin::user, F("no packets found on stdin"));
277 }
278 else
279 {
280 for (args_vector::const_iterator i = args.begin();
281 i != args.end(); ++i)
282 {
283 data dat;
284 read_data(system_path(*i), dat);
285 istringstream ss(dat());
286 count += read_packets(ss, dbw);
287 }
288 E(count != 0, origin::user,
289 FP("no packets found in given file",
290 "no packets found in given files",
291 args.size()));
292 }
293 P(FP("read %d packet", "read %d packets", count) % count);
294}
295
296
297// Local Variables:
298// mode: C++
299// fill-column: 76
300// c-file-style: "gnu"
301// indent-tabs-mode: nil
302// End:
303// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status