monotone

monotone Mtn Source Tree

Root/cmd_packet.cc

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 "database.hh"
17#include "key_store.hh"
18#include "packet.hh"
19
20using std::cin;
21using std::cout;
22using std::istringstream;
23using std::vector;
24
25CMD(pubkey, "pubkey", "", CMD_REF(packet_io), N_("ID"),
26 N_("Prints a public key packet"),
27 "",
28 options::opts::none)
29{
30 database db(app);
31 key_store keys(app);
32
33 if (args.size() != 1)
34 throw usage(execid);
35
36 rsa_keypair_id ident(idx(args, 0)());
37 bool exists(false);
38 rsa_pub_key key;
39 if (db.database_specified() && db.public_key_exists(ident))
40 {
41 db.get_key(ident, key);
42 exists = true;
43 }
44 if (keys.key_pair_exists(ident))
45 {
46 keypair kp;
47 keys.get_key_pair(ident, kp);
48 key = kp.pub;
49 exists = true;
50 }
51 N(exists,
52 F("public key '%s' does not exist") % idx(args, 0)());
53
54 packet_writer pw(cout);
55 pw.consume_public_key(ident, key);
56}
57
58CMD(privkey, "privkey", "", CMD_REF(packet_io), N_("ID"),
59 N_("Prints a private key packet"),
60 "",
61 options::opts::none)
62{
63 key_store keys(app);
64
65 if (args.size() != 1)
66 throw usage(execid);
67
68 rsa_keypair_id ident(idx(args, 0)());
69 N(keys.key_pair_exists(ident),
70 F("public and private key '%s' do not exist in keystore")
71 % idx(args, 0)());
72
73 packet_writer pw(cout);
74 keypair kp;
75 keys.get_key_pair(ident, kp);
76 pw.consume_key_pair(ident, kp);
77}
78
79namespace
80{
81 // this writer injects packets it receives to the database
82 // and/or keystore.
83
84 struct packet_db_writer : public packet_consumer
85 {
86 database & db;
87 key_store & keys;
88
89 public:
90 packet_db_writer(database & db, key_store & keys)
91 : db(db), keys(keys)
92 {}
93 virtual ~packet_db_writer() {}
94 virtual void consume_file_data(file_id const & ident,
95 file_data const & dat)
96 {
97 transaction_guard guard(db);
98 db.put_file(ident, dat);
99 guard.commit();
100 }
101
102 virtual void consume_file_delta(file_id const & old_id,
103 file_id const & new_id,
104 file_delta const & del)
105 {
106 transaction_guard guard(db);
107 db.put_file_version(old_id, new_id, del);
108 guard.commit();
109 }
110
111 virtual void consume_revision_data(revision_id const & ident,
112 revision_data const & dat)
113 {
114 transaction_guard guard(db);
115 db.put_revision(ident, dat);
116 guard.commit();
117 }
118
119 virtual void consume_revision_cert(revision<cert> const & t)
120 {
121 transaction_guard guard(db);
122 db.put_revision_cert(t);
123 guard.commit();
124 }
125
126 virtual void consume_public_key(rsa_keypair_id const & ident,
127 rsa_pub_key const & k)
128 {
129 transaction_guard guard(db);
130 db.put_key(ident, k);
131 guard.commit();
132 }
133
134 virtual void consume_key_pair(rsa_keypair_id const & ident,
135 keypair const & kp)
136 {
137 keys.put_key_pair(ident, kp);
138 }
139
140 virtual void consume_old_private_key(rsa_keypair_id const & ident,
141 old_arc4_rsa_priv_key const & k)
142 {
143 rsa_pub_key dummy;
144 keys.migrate_old_key_pair(ident, k, dummy);
145 }
146 };
147}
148
149// Name : read_packets
150// Arguments:
151// packet-data
152// Added in: 8.1
153// Purpose:
154// Store public keys (and incidentally anything else that can be
155// represented as a packet) into the database.
156// Input format:
157// The format of the packet-data argument is identical to the output
158// of "mtn pubkey <keyname>" (or other packet output commands).
159// Output format:
160// No output.
161// Error conditions:
162// Invalid input formatting.
163CMD_AUTOMATE(read_packets, N_("PACKET-DATA"),
164 N_("Load the given packets into the database."),
165 "",
166 options::opts::none)
167{
168 N(args.size() == 1,
169 F("wrong argument count"));
170
171 database db(app);
172 key_store keys(app);
173 packet_db_writer dbw(db, keys);
174
175 istringstream ss(idx(args,0)());
176 read_packets(ss, dbw);
177}
178
179CMD(read, "read", "", CMD_REF(packet_io), "[FILE1 [FILE2 [...]]]",
180 N_("Reads packets from files"),
181 N_("If no files are provided, the standard input is used."),
182 options::opts::none)
183{
184 database db(app);
185 key_store keys(app);
186 packet_db_writer dbw(db, keys);
187 size_t count = 0;
188 if (args.empty())
189 {
190 count += read_packets(cin, dbw);
191 N(count != 0, F("no packets found on stdin"));
192 }
193 else
194 {
195 for (args_vector::const_iterator i = args.begin();
196 i != args.end(); ++i)
197 {
198 data dat;
199 read_data(system_path(*i), dat);
200 istringstream ss(dat());
201 count += read_packets(ss, dbw);
202 }
203 N(count != 0, FP("no packets found in given file",
204 "no packets found in given files",
205 args.size()));
206 }
207 P(FP("read %d packet", "read %d packets", count) % count);
208}
209
210
211// Local Variables:
212// mode: C++
213// fill-column: 76
214// c-file-style: "gnu"
215// indent-tabs-mode: nil
216// End:
217// 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