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, 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, 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);     // leave this on, since libusb uses
00099                                         // stdio for debug messages
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                 // process command line options
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':       // show dbname
00119                                 dbNames.push_back(string(optarg));
00120                                 break;
00121 
00122                         case 'i':       // Include IDs
00123                                 include_ids = true;
00124                                 break;
00125 
00126                         case 'p':       // Blackberry PIN
00127                                 pin = strtoul(optarg, NULL, 16);
00128                                 break;
00129 
00130                         case 'P':       // Device password
00131                                 password = optarg;
00132                                 break;
00133 
00134                         case 'v':       // data dump on
00135                                 data_dump = true;
00136                                 break;
00137 
00138                         case 'h':       // help
00139                         default:
00140                                 Usage();
00141                                 return 0;
00142                         }
00143                 }
00144 
00145                 // Display usage info if user appears confused
00146                 if( !dbNames.size() ) {
00147                         Usage();
00148                         return 0;
00149                 }
00150 
00151                 // Initialize the barry library.  Must be called before
00152                 // anything else.
00153                 Barry::Init(data_dump);
00154 
00155                 // Probe the USB bus for Blackberry devices and display.
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                 // Create our controller object
00164                 Barry::Controller con(probe.Get(activeDevice));
00165                 Barry::Mode::Desktop desktop(con);
00166 
00167                 // Sum all specified databases
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 

Generated on Wed Sep 24 21:27:31 2008 for Barry by  doxygen 1.5.1