Reduced minimum password retry level from 6 to 3
[barry/pauldeden.git] / tools / brecsum.cc
blob17dc7dbb838b8bffe76a177053b362f1d1169c6d
1 ///
2 /// \file brecsum.cc
3 /// Generate SHA1 sums of raw Blackberry database records.
4 /// This is mostly useful for data verification during testing.
5 ///
7 /*
8 Copyright (C) 2008, Net Direct Inc. (http://www.netdirect.ca/)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #include <barry/barry.h>
24 #include <iomanip>
25 #include <iostream>
26 #include <vector>
27 #include <string>
28 #include <getopt.h>
30 using namespace std;
31 using namespace Barry;
33 void Usage()
35 int major, minor;
36 const char *Version = Barry::Version(major, minor);
38 cerr
39 << "brecsum - Generate SHA1 sums of raw Blackberry database records.\n"
40 << " Copyright 2008, Net Direct Inc. (http://www.netdirect.ca/)\n"
41 << " Using: " << Version << "\n"
42 << "\n"
43 << " -d db Read database 'db' and sum all its records.\n"
44 << " Can be used multiple times to fetch more than one DB\n"
45 << " -h This help\n"
46 << " -i Include Type and Unique record IDs in the checksums\n"
47 << " -p pin PIN of device to talk with\n"
48 << " If only one device is plugged in, this flag is optional\n"
49 << " -P pass Simplistic method to specify device password\n"
50 << " -v Dump protocol data during operation\n"
51 << endl;
54 class ChecksumParser : public Barry::Parser
56 bool m_IncludeIds;
57 SHA_CTX m_ctx;
59 public:
60 explicit ChecksumParser(bool IncludeIds)
61 : m_IncludeIds(IncludeIds)
64 virtual void Clear()
66 SHA1_Init(&m_ctx);
69 virtual void SetIds(uint8_t RecType, uint32_t UniqueId)
71 if( m_IncludeIds ) {
72 SHA1_Update(&m_ctx, &RecType, sizeof(RecType));
73 SHA1_Update(&m_ctx, &UniqueId, sizeof(UniqueId));
76 virtual void ParseFields(const Barry::Data &data, size_t &offset)
78 int len = data.GetSize() - offset;
79 SHA1_Update(&m_ctx, data.GetData() + offset, len);
80 offset += len;
83 virtual void Store()
85 unsigned char sha1[SHA_DIGEST_LENGTH];
86 SHA1_Final(sha1, &m_ctx);
88 for( int i = 0; i < SHA_DIGEST_LENGTH; i++ ) {
89 cout << hex << setfill('0') << setw(2)
90 << (unsigned int) sha1[i];
92 cout << endl;
96 int main(int argc, char *argv[])
98 cout.sync_with_stdio(true); // leave this on, since libusb uses
99 // stdio for debug messages
101 try {
103 uint32_t pin = 0;
104 bool
105 data_dump = false,
106 include_ids = false;
107 string password;
108 vector<string> dbNames;
110 // process command line options
111 for(;;) {
112 int cmd = getopt(argc, argv, "d:hip:P:v");
113 if( cmd == -1 )
114 break;
116 switch( cmd )
118 case 'd': // show dbname
119 dbNames.push_back(string(optarg));
120 break;
122 case 'i': // Include IDs
123 include_ids = true;
124 break;
126 case 'p': // Blackberry PIN
127 pin = strtoul(optarg, NULL, 16);
128 break;
130 case 'P': // Device password
131 password = optarg;
132 break;
134 case 'v': // data dump on
135 data_dump = true;
136 break;
138 case 'h': // help
139 default:
140 Usage();
141 return 0;
145 // Display usage info if user appears confused
146 if( !dbNames.size() ) {
147 Usage();
148 return 0;
151 // Initialize the barry library. Must be called before
152 // anything else.
153 Barry::Init(data_dump);
155 // Probe the USB bus for Blackberry devices and display.
156 Barry::Probe probe;
157 int activeDevice = probe.FindActive(pin);
158 if( activeDevice == -1 ) {
159 cerr << "No device selected, or PIN not found" << endl;
160 return 1;
163 // Create our controller object
164 Barry::Controller con(probe.Get(activeDevice));
165 Barry::Mode::Desktop desktop(con);
167 // Sum all specified databases
168 if( dbNames.size() ) {
169 vector<string>::iterator b = dbNames.begin();
170 ChecksumParser parser(include_ids);
172 desktop.Open(password.c_str());
173 for( ; b != dbNames.end(); b++ ) {
174 unsigned int id = desktop.GetDBID(*b);
175 desktop.LoadDatabase(id, parser);
180 catch( std::exception &e ) {
181 std::cerr << e.what() << endl;
182 return 1;
185 return 0;