brecsum.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       brecsum.cc
00003 ///             Generate SHA1 sums of raw Blackberry database records.
00004 ///             This is mostly useful for data verification during testing.
00005 ///
00006 
00007 /*
00008     Copyright (C) 2008-2009, Net Direct Inc. (http://www.netdirect.ca/)
00009 
00010     This program is free software; you can redistribute it and/or modify
00011     it under the terms of the GNU General Public License as published by
00012     the Free Software Foundation; either version 2 of the License, or
00013     (at your option) any later version.
00014 
00015     This program is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00018 
00019     See the GNU General Public License in the COPYING file at the
00020     root directory of this project for more details.
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-2009, 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 
00077         virtual void ParseHeader(const Barry::Data &, size_t &)
00078         {
00079                 // do nothing here, parse it all at once in ParseFields
00080         }
00081 
00082         virtual void ParseFields(const Barry::Data &data, size_t &offset,
00083                                 const Barry::IConverter *ic)
00084         {
00085                 int len = data.GetSize() - offset;
00086                 SHA1_Update(&m_ctx, data.GetData() + offset, len);
00087                 offset += len;
00088         }
00089 
00090         virtual void Store()
00091         {
00092                 unsigned char sha1[SHA_DIGEST_LENGTH];
00093                 SHA1_Final(sha1, &m_ctx);
00094 
00095                 for( int i = 0; i < SHA_DIGEST_LENGTH; i++ ) {
00096                         cout << hex << setfill('0') << setw(2)
00097                                 << (unsigned int) sha1[i];
00098                 }
00099                 cout << endl;
00100         }
00101 };
00102 
00103 int main(int argc, char *argv[])
00104 {
00105         cout.sync_with_stdio(true);     // leave this on, since libusb uses
00106                                         // stdio for debug messages
00107 
00108         try {
00109 
00110                 uint32_t pin = 0;
00111                 bool
00112                         data_dump = false,
00113                         include_ids = false;
00114                 string password;
00115                 vector<string> dbNames;
00116 
00117                 // process command line options
00118                 for(;;) {
00119                         int cmd = getopt(argc, argv, "d:hip:P:v");
00120                         if( cmd == -1 )
00121                                 break;
00122 
00123                         switch( cmd )
00124                         {
00125                         case 'd':       // show dbname
00126                                 dbNames.push_back(string(optarg));
00127                                 break;
00128 
00129                         case 'i':       // Include IDs
00130                                 include_ids = true;
00131                                 break;
00132 
00133                         case 'p':       // Blackberry PIN
00134                                 pin = strtoul(optarg, NULL, 16);
00135                                 break;
00136 
00137                         case 'P':       // Device password
00138                                 password = optarg;
00139                                 break;
00140 
00141                         case 'v':       // data dump on
00142                                 data_dump = true;
00143                                 break;
00144 
00145                         case 'h':       // help
00146                         default:
00147                                 Usage();
00148                                 return 0;
00149                         }
00150                 }
00151 
00152                 // Display usage info if user appears confused
00153                 if( !dbNames.size() ) {
00154                         Usage();
00155                         return 0;
00156                 }
00157 
00158                 // Initialize the barry library.  Must be called before
00159                 // anything else.
00160                 Barry::Init(data_dump);
00161 
00162                 // Probe the USB bus for Blackberry devices and display.
00163                 Barry::Probe probe;
00164                 int activeDevice = probe.FindActive(pin);
00165                 if( activeDevice == -1 ) {
00166                         cerr << "No device selected, or PIN not found" << endl;
00167                         return 1;
00168                 }
00169 
00170                 // Create our controller object
00171                 Barry::Controller con(probe.Get(activeDevice));
00172                 Barry::Mode::Desktop desktop(con);
00173 
00174                 // Sum all specified databases
00175                 if( dbNames.size() ) {
00176                         vector<string>::iterator b = dbNames.begin();
00177                         ChecksumParser parser(include_ids);
00178 
00179                         desktop.Open(password.c_str());
00180                         for( ; b != dbNames.end(); b++ ) {
00181                                 unsigned int id = desktop.GetDBID(*b);
00182                                 desktop.LoadDatabase(id, parser);
00183                         }
00184                 }
00185 
00186         }
00187         catch( std::exception &e ) {
00188                 std::cerr << e.what() << endl;
00189                 return 1;
00190         }
00191 
00192         return 0;
00193 }
00194 

Generated on Tue Jun 30 16:08:13 2009 for Barry by  doxygen 1.5.8