00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <barry/barry.h>
00024 #include <iomanip>
00025 #include <iostream>
00026 #include <vector>
00027 #include <string>
00028 #include <getopt.h>
00029
00030 using namespace std;
00031 using namespace Barry;
00032
00033 void Usage()
00034 {
00035 int major, minor;
00036 const char *Version = Barry::Version(major, minor);
00037
00038 cerr
00039 << "brecsum - Generate SHA1 sums of raw Blackberry database records.\n"
00040 << " Copyright 2008, Net Direct Inc. (http://www.netdirect.ca/)\n"
00041 << " Using: " << Version << "\n"
00042 << "\n"
00043 << " -d db Read database 'db' and sum all its records.\n"
00044 << " Can be used multiple times to fetch more than one DB\n"
00045 << " -h This help\n"
00046 << " -i Include Type and Unique record IDs in the checksums\n"
00047 << " -p pin PIN of device to talk with\n"
00048 << " If only one device is plugged in, this flag is optional\n"
00049 << " -P pass Simplistic method to specify device password\n"
00050 << " -v Dump protocol data during operation\n"
00051 << endl;
00052 }
00053
00054 class ChecksumParser : public Barry::Parser
00055 {
00056 bool m_IncludeIds;
00057 SHA_CTX m_ctx;
00058
00059 public:
00060 explicit ChecksumParser(bool IncludeIds)
00061 : m_IncludeIds(IncludeIds)
00062 {}
00063
00064 virtual void Clear()
00065 {
00066 SHA1_Init(&m_ctx);
00067 }
00068
00069 virtual void SetIds(uint8_t RecType, uint32_t UniqueId)
00070 {
00071 if( m_IncludeIds ) {
00072 SHA1_Update(&m_ctx, &RecType, sizeof(RecType));
00073 SHA1_Update(&m_ctx, &UniqueId, sizeof(UniqueId));
00074 }
00075 }
00076 virtual void ParseFields(const Barry::Data &data, size_t &offset)
00077 {
00078 int len = data.GetSize() - offset;
00079 SHA1_Update(&m_ctx, data.GetData() + offset, len);
00080 offset += len;
00081 }
00082
00083 virtual void Store()
00084 {
00085 unsigned char sha1[SHA_DIGEST_LENGTH];
00086 SHA1_Final(sha1, &m_ctx);
00087
00088 for( int i = 0; i < SHA_DIGEST_LENGTH; i++ ) {
00089 cout << hex << setfill('0') << setw(2)
00090 << (unsigned int) sha1[i];
00091 }
00092 cout << endl;
00093 }
00094 };
00095
00096 int main(int argc, char *argv[])
00097 {
00098 cout.sync_with_stdio(true);
00099
00100
00101 try {
00102
00103 uint32_t pin = 0;
00104 bool
00105 data_dump = false,
00106 include_ids = false;
00107 string password;
00108 vector<string> dbNames;
00109
00110
00111 for(;;) {
00112 int cmd = getopt(argc, argv, "d:hip:P:v");
00113 if( cmd == -1 )
00114 break;
00115
00116 switch( cmd )
00117 {
00118 case 'd':
00119 dbNames.push_back(string(optarg));
00120 break;
00121
00122 case 'i':
00123 include_ids = true;
00124 break;
00125
00126 case 'p':
00127 pin = strtoul(optarg, NULL, 16);
00128 break;
00129
00130 case 'P':
00131 password = optarg;
00132 break;
00133
00134 case 'v':
00135 data_dump = true;
00136 break;
00137
00138 case 'h':
00139 default:
00140 Usage();
00141 return 0;
00142 }
00143 }
00144
00145
00146 if( !dbNames.size() ) {
00147 Usage();
00148 return 0;
00149 }
00150
00151
00152
00153 Barry::Init(data_dump);
00154
00155
00156 Barry::Probe probe;
00157 int activeDevice = probe.FindActive(pin);
00158 if( activeDevice == -1 ) {
00159 cerr << "No device selected, or PIN not found" << endl;
00160 return 1;
00161 }
00162
00163
00164 Barry::Controller con(probe.Get(activeDevice));
00165 Barry::Mode::Desktop desktop(con);
00166
00167
00168 if( dbNames.size() ) {
00169 vector<string>::iterator b = dbNames.begin();
00170 ChecksumParser parser(include_ids);
00171
00172 desktop.Open(password.c_str());
00173 for( ; b != dbNames.end(); b++ ) {
00174 unsigned int id = desktop.GetDBID(*b);
00175 desktop.LoadDatabase(id, parser);
00176 }
00177 }
00178
00179 }
00180 catch( std::exception &e ) {
00181 std::cerr << e.what() << endl;
00182 return 1;
00183 }
00184
00185 return 0;
00186 }
00187